Commit Graph

155 Commits

Author SHA1 Message Date
rillig 655b7b72fc indent: remove redundant comments
No functional change.
2021-10-07 21:38:25 +00:00
rillig 94fc8b1516 indent: fix outdated comment, add 'const' 2021-10-05 19:58:38 +00:00
rillig ed959c6f7e indent: fix spelling in comments 2021-10-05 06:49:19 +00:00
rillig 5b4a581be4 indent: use proper escape sequence for form feed
This escape sequence has been available since at least 1978.
2021-10-05 06:15:24 +00:00
rillig e26a8816d6 indent: merge duplicate code into is_hspace
No functional change.
2021-10-05 06:09:42 +00:00
rillig abab5cde9c indent: clean up code for appending to buffers
Use *e++ for appending and e[-1] for testing the previously appended
character, like in other places in the code.

No functional change.
2021-10-05 05:56:49 +00:00
rillig 455c76abfa indent: merge duplicate code for reading from input buffer
No functional change.
2021-10-05 05:39:14 +00:00
rillig 630203ff25 indent: let indent format its own code -- in supervised mode
After running indent on the code, I manually selected each change that
now looks better than before. The remaining changes are left for later.
All in all, indent did a pretty good job, except for syntactic additions
from after 1990, but that was to be expected. Examples for such
additions are GCC's __attribute__ and C99 designated initializers.

Indent has only few knobs to tune the indentation. The knob for the
continuation indentation applies to function declarations as well as to
expressions. The knob for indentation of local variable declarations
applies to struct members as well, even if these are members of a
top-level struct.

Several code comments crossed the right margin in column 78. Several
other code comments were correctly broken though. The cause for this
difference was not obvious.

No functional change.
2021-09-26 19:37:11 +00:00
rillig d342482216 indent: merge duplicate code for token buffers
No functional change.
2021-09-25 22:14:21 +00:00
rillig 559f9bf2c0 indent: remove dead code for printing comments after empty lines
This code has been commented out for at least 29 years.

No functional change.
2021-09-25 20:23:42 +00:00
rillig 8ef9c67e78 indent: convert remaining ibool to bool
No functional change intended.
2021-09-25 17:36:51 +00:00
rillig d4717418d0 indent: prepare for lint's strict bool mode
Before C99, C had no boolean type. Instead, indent used int for that,
just like many other programs. Even with C99, bool and int can be used
interchangeably in many situations, such as querying '!i' or '!ptr' or
'cond == 0'.

Since January 2021, lint provides the strict bool mode, which makes bool
a non-arithmetic type that is incompatible with any other type. Having
clearly separate types helps in understanding the code.

To migrate indent to strict bool mode, the first step is to apply all
changes that keep the resulting binary the same. Since sizeof(bool) is
1 and sizeof(int) is 4, the type ibool serves as an intermediate type.
For now it is defined to int, later it will become bool.

The current code compiles cleanly in C99 and C11 mode, as well as in
lint's strict bool mode. There are a few tricky places:

In args.c in 'struct pro', there are two types of options: boolean and
integer. Boolean options point to a bool variable, integer options
point to an int variable. To keep the current structure of the code,
the pointer has been changed to 'void *'. To ensure type safety, the
definition of the options is done via preprocessor magic, which in C11
mode ensures the correct pointer types. (Add CFLAGS+=-std=gnu11 at the
very bottom of the Makefile.)

In indent.c in process_preprocessing, a boolean variable is
post-incremented. That variable is only assigned to another variable,
and that variable is only used in a boolean context. To provoke a
different behavior between the '++' and the '= true', the source code
to be indented would need 1 << 32 preprocessing directives, which is
unlikely to happen in practice.

In io.c in dump_line, the variables ps.in_stmt and ps.in_decl only ever
get the values 0 and 1. For these values, the expressions 'a & ~b' and
'a && !b' are equivalent, in all versions of C. The compiler may
generate different code for them, though.

In io.c in parse_indent_comment, the assignment to inhibit_formatting
takes place in integer context. If the compiler is smart enough to
detect the possible values of on_off, it may generate the same code
before and after the change, but that is rather unlikely.

The second step of the migration will be to replace ibool with bool,
step by step, just in case there are any hidden gotchas in the code,
such as sizeof or pointer casts.

No change to the resulting binary.
2021-09-25 17:11:23 +00:00
rillig 7988d7d989 indent: remove ifdef for lint
NetBSD lint does not need them anymore, FreeBSD does not have lint.
2021-09-25 13:38:32 +00:00
rillig e81ecc57dd indent: move statistical values into a separate struct
No functional change.
2021-09-25 10:41:03 +00:00
rillig be418b31af indent: add nonnull memory allocation functions
The only functional change is a single error message.
2021-09-25 08:23:31 +00:00
rillig 6c6f9b4b1c indent: group global variables for token buffer
No functional change.
2021-09-25 08:04:13 +00:00
rillig d4c22d60c4 indent: group global variables for code buffer
No functional change.
2021-09-25 07:55:24 +00:00
rillig 5ecb489516 indent: group global variables for label buffer into struct
No functional change.
2021-09-24 18:47:29 +00:00
rillig 1a55f0c468 indent: group global variables for the comment buffer
No functional change.
2021-09-24 18:14:06 +00:00
rillig de1cec24a0 indent: clean up check_size_comment
The additional parameter last_bl_ptr was only necessary because the last
blank was stored as a pointer into the buffer.  By storing the index in
the buffer instead, it doesn't need to be updated all the time.

No functional change.
2021-03-14 05:26:42 +00:00
rillig a8e5d6abb4 indent: remove trailing whitespace 2021-03-14 04:52:10 +00:00
rillig f21f69f749 indent: clean up target column computation in process_comment
No functional change.
2021-03-14 04:42:17 +00:00
rillig 1e4c413bac indent: fix off-by-one error in comment wrapping
The manual page says that the default maximum length of a comment line
is 78.  The test 'comments.0' wrongly assumed that this 78 would refer
to the maximum _column_ allowed, which is off by one.

Fix the wording in the test 'comments.0' and remove the (now satisfied)
expectation comments in the test 'token-comment.0'.

Several other tests just happened to hit that limit, fix these as well.
2021-03-14 01:34:13 +00:00
rillig 744982a9b6 indent: fix lint warnings
No functional change.
2021-03-14 00:22:16 +00:00
rillig 56c4653e3f indent: remove the '+ 1' from right margin calculation in comment
No functional change.
2021-03-13 18:11:31 +00:00
rillig 66af9142ab indent: distinguish between 'column' and 'indentation'
column == 1 + indentation.

In addition, indentation is a relative distance while column is an
absolute position.  Therefore, don't confuse these two concepts, to
prevent off-by-one errors.

No functional change.
2021-03-13 13:51:08 +00:00
rillig 7ad6720446 indent: rename pr_comment to process_comment, clean up documentation
No functional change.
2021-03-13 13:25:23 +00:00
rillig acec5beac9 indent: remove redundant parentheses
No functional change.
2021-03-13 11:27:01 +00:00
rillig 0a99ae80ca indent: fix confusing variable names
The word 'col' should only be used for the 1-based column number.  This
name is completely inappropriate for a line length since that provokes
off-by-one errors.  The name 'cols' would be acceptable although
confusing since it sounds so similar to 'col'.

Therefore, rename variables that are related to the maximum line length
to 'line_length' since that makes for obvious code and nicely relates to
the description of the option in the manual page.

No functional change.
2021-03-13 11:19:43 +00:00
rillig 2c2459a1fa indent: document undefined behavior in processing of comments
No functional change.
2021-03-13 10:47:59 +00:00
rillig f3b63c94c8 indent: inline calls to count_spaces and count_spaces_until
These two functions operated on column numbers instead of indentation,
which required adjustments of '+ 1' and '- 1'.  Their names were
completely wrong since these functions did not count anything, instead
they computed the column.

No functional change.
2021-03-13 10:32:25 +00:00
rillig 0c51d9451c indent: replace compute_code_column with compute_code_indent
The goal is to only ever be concerned about the _indentation_ of a
token, never the _column_ it appears in.  Having only one of these
avoids off-by-one errors.

No functional change.
2021-03-13 10:06:47 +00:00
rillig 5888ddac66 indent: replace compute_label_column with compute_label_indent
Using the invariant 'column == 1 + indent'.  This removes several overly
complicated '+ 1' from the code that are not needed conceptually.

No functional change.
2021-03-13 09:54:11 +00:00
rillig 689a1f7922 indent: replace pad_output with output_indent
Calculating the indentation is simpler than calculating the column,
since that saves the constant addition and subtraction of the 1.

No functional change.
2021-03-13 00:26:56 +00:00
rillig a0306e684f indent: replace 'target' with 'indent' in function names
The word 'target' was not as specific as possible.

No functional change.
2021-03-12 23:16:00 +00:00
rillig 2be5ec967d indent: use consistent indentation for 'else'
Half of the code used -ce, the other half the opposite -nce.

No functional change.
2021-03-12 23:10:18 +00:00
rillig 200ea2d398 indent: reduce indentation of check_size functions
No functional change.
2021-03-11 22:32:06 +00:00
rillig c7f0688822 indent: remove redundant cast after allocation functions
No functional change.
2021-03-11 22:28:30 +00:00
rillig 72f722fd46 indent: use consistent array indexing
No functional change.
2021-03-11 22:15:44 +00:00
rillig 6cabfe5cb2 indent: manually indent comments
It's strange that indent's own code is not formatted by indent itself,
which would be a good demonstration of its capabilities.

In its current state, I don't trust indent to get even the tokenization
correct, therefore the only safe way is to format the code manually.
2021-03-09 16:48:28 +00:00
rillig 37bc0e6731 indent: convert big macros to functions
Each of these buffers is only modified in a single file.  This makes it
unnecessary to declare the macros in the global header.
2021-03-08 20:15:42 +00:00
rillig 73071b8322 indent: fix handling of '//' end-of-line comments 2021-03-07 22:11:01 +00:00
rillig 3658a7adc6 indent: use all headers in all files
This is a prerequisite for converting the token types to an enum instead
of a preprocessor define, since the return type of lexi will become
token_type.  Having the enum will make debugging easier.

There was a single naming collision, which forced the variable in
scan_profile to be renamed.  All other token names are used nowhere
else.

No change to the resulting binary.
2021-03-07 10:42:48 +00:00
kamil d6a1bc3f07 Upgrade indent(1)
Merge all the changes from the recent FreeBSD HEAD snapshot
into our local copy.

FreeBSD actively maintains this program in their sources and their
repository contains over 100 commits with changes.

Keep the delta between the FreeBSD and NetBSD versions to absolute
minimum, mostly RCS Id and compatiblity fixes.

Major chages in this import:

 - Added an option -ldi<N> to control indentation of local variable names.
 - Added option -P for loading user-provided files as profiles
 - Added -tsn for setting tabsize
 - Rename -nsac/-sac ("space after cast") to -ncs/-cs
 - Added option -fbs Enables (disables) splitting the function declaration and opening brace across two lines.
 - Respect SIMPLE_BACKUP_SUFFIX environment variable in indent(1)
 - Group global option variables into an options structure
 - Use bsearch() for looking up type keywords.
 - Don't produce unneeded space character in function declarators
 - Don't unnecessarily add a blank before a comment ends.
 - Don't ignore newlines after comments that follow braces.

Merge the FreeBSD intend(1) tests with our ATF framework.
All tests pass.

Upgrade prepared by Manikishan Ghantasala.
Final polishing by myself.
2019-04-04 15:22:13 +00:00
ginsbach 522de9efc9 Fix obvious contraction spelling mistakes by adding missing apostrophes. 2016-02-25 13:23:27 +00:00
agc 89aaa1bb64 Move UCB-licensed code from 4-clause to 3-clause licence.
Patches provided by Joel Baker in PR 22365, verified by myself.
2003-08-07 11:13:06 +00:00
christos fc24d74cc3 PR/21645: Mishka: Localized comments don't work with indent. 2003-06-19 15:45:22 +00:00
wiz 44e7d056da Remove #ifndef'd __STDC__ code. ANSIfy. 2002-05-26 22:53:38 +00:00
lukem b048688222 WARNSify, fix .Nm usage, deprecate register, use <err.h>, KNFify (with indent!;) 1997-10-19 03:17:12 +00:00
mrg 226284be12 merge lite-2. 1997-10-18 16:04:21 +00:00
tls 9d225a1783 RCS ID police 1997-01-09 20:18:21 +00:00
cgd 5d4118791b do block commenting, if comment begins with slash-star-newline. 1993-08-07 06:56:49 +00:00
mycroft e9d867ef50 Add RCS identifiers. 1993-08-01 17:54:45 +00:00
mrg c78c6a22ab 4.4BSD-Lite2 1993-06-06 20:58:02 +00:00
cgd dab5e017db added, from net/2 (patch 124). 1993-04-09 12:58:42 +00:00