Commit Graph

295 Commits

Author SHA1 Message Date
rillig 4719ae5c0b indent: rename blankline_requested variables
The words 'prefix' and 'postfix' sounded too much like horizontal
concepts, like in operators. The actual purpose of these variables is to
add blank lines before and after the current line, so use the same
wording as in the command line options.

No functional change.
2021-10-20 05:14:21 +00:00
rillig 4c28bd5551 indent: invert condition in process_newline
It's hard to follow a condition that combines many negated terms with
'||'. Group the conditions by their origin.

The condition '!opt.break_after_comma && break_comma' still sounds like
a contradition, more investigations to follow.

No functional change.
2021-10-20 05:07:08 +00:00
rillig a1a0df58f8 indent: rename next_blank_lines to blank_lines_to_output
The previous name was already an improvement over the name before that
(n_real_blanklines), but didn't express the intended purpose clearly
enough, so try another name.

No functional change.
2021-10-20 05:00:37 +00:00
rillig 54f8de4287 indent: always keep next_blank_lines >= 0
No functional change.
2021-10-19 21:39:19 +00:00
rillig 022f9c1147 indent: use simpler code for copying the input buffer
In debug mode, this reduces the amount of debug output lines.

No functional change in default mode.
2021-10-19 21:21:07 +00:00
rillig e3e725ae28 indent: if a file ends with indent off, don't add space-newline 2021-10-19 18:29:59 +00:00
rillig 11e259efbb indent: parse int command line options strictly
On i386 and other platforms where LONG_MAX == INT_MAX, the test
t_errors/option_tabsize_very_large failed since the behavior on integer
overflow differs between ILP32 and LP64 platforms. Noticed by gson@.

Avoid this unintended difference by adding reasonable limits for each of
the integer options and by replacing atoi with strtol.
2021-10-17 18:13:00 +00:00
rillig d698b88a04 indent: clarify that 25 is a magic number
The extra line width for comments to the very right is just that, an
arbitrarily chosen number. It neither has to be a multiple of 8, nor of
the tabsize nor of the indentation. Since 25 is neither of these, this
makes it a perfect choice, allowing these extreme comments to have 22
characters per line with -sc (leading asterisks in comment
continuations, the default) or 25 without.

No functional change.
2021-10-14 20:23:43 +00:00
rillig a645dc3fe2 indent: turn ps.com_ind into local variable
This makes it immediately clear that ps.com_ind is computed in the first
part of process_comment and then only used from there.

No functional change.
2021-10-14 20:15:18 +00:00
rillig 405d75dbcb indent: check command line options stricter
Previously, bool options were allowed to have trailing garbage. For
example, the option '-bacc' could be spelled '-bacchus' as well.

Check that the exact option name is given in the command line, to
prevent typos in the configuration files and to reduce surprises just in
case a future option is a prefix of an existing option, or vice versa.

Add a new test program for error handling. Most of these tests are so
simple that it would be overkill to create 3 files for each test.
2021-10-13 23:33:52 +00:00
rillig 755f28738f indent: extract fits_in_one_line from process_comment
No functional change.
2021-10-13 22:38:02 +00:00
rillig 418d351385 indent: in process_comment, migrate int variable to bool
No functional change.
2021-10-12 22:22:35 +00:00
rillig d46c2d3aef indent: in process_comment, negate box_com to may_wrap
In the new line 213, may_wrap could only be true and was therefore a
redundant condition.

No functional change.
2021-10-12 22:20:22 +00:00
rillig a7dacd6a19 indent: negate a few condition in process_comment
No functional change.
2021-10-12 22:04:03 +00:00
rillig b5feabda44 indent: fix formatting of single-line comments (since today)
The change in pr_comment.c 1.70 from 3 hours ago did not cover all edge
cases correctly. Now it works for comments that are aligned with tabs.
2021-10-12 21:08:37 +00:00
rillig fbf0f13377 indent: replace unreachable code with assertion 2021-10-12 19:57:53 +00:00
rillig 806a7885c8 indent: use high-level buffer API for processing comments
Document the trickery of copying the last word from the previous line
since that it not obvious at all.

No functional change.
2021-10-12 19:56:07 +00:00
rillig 625980cc9c indent: fix wrapping for comments in otherwise empty lines
The comment above the code was wrong. The leading 3 characters were
indeed ignored, but the first of them was '/', not ' '. Of the trailing
3 characters, 2 were not ignored. The start and end of the comment would
not cancel out, they would rather sum up.
2021-10-12 18:22:01 +00:00
rillig c790c871c9 indent: use separate variables for lexi_alnum and lexi
These two uses of the variable are independent of each other.

No functional change.
2021-10-11 20:31:06 +00:00
rillig 27995fd480 indent: clean up comments in lexi and lexi_alnum
No functional change.
2021-10-11 20:13:46 +00:00
rillig aef7316ce8 indent: extract lexi_alnum from lexi
No functional change.
2021-10-11 19:55:35 +00:00
rillig d93e6b2dd4 indent: use bool for suppress_blanklines
It only ever got assigned the values 0 and 1.

No functional change.
2021-10-11 19:04:47 +00:00
rillig bb2271a63e indent: remove dead code 2021-10-11 18:55:49 +00:00
rillig 2cac233d86 indent: fix lint warning about bsearch discarding 'const'
lexi.c(433): warning: call to 'bsearch' effectively discards 'const'
    from argument [346]
2021-10-09 20:07:37 +00:00
rillig f44a9e8380 indent: condense code for calculating indentations
No functional change.
2021-10-09 11:13:25 +00:00
rillig acc5a6ec1a indent: extract common code for advancing a single tab
No functional change.
2021-10-09 11:00:27 +00:00
rillig f146974875 indent: merge duplicate code in lexer
No functional change.
2021-10-08 23:55:44 +00:00
rillig c196e4acb6 indent: clean up comments, parentheses, debug messages, boolean operator
No functional change.
2021-10-08 23:47:40 +00:00
rillig f577c4493e indent: rename in_or_st to init_or_struct
This makes a few comments redundant.

No functional change.
2021-10-08 23:43:33 +00:00
rillig 4ab3bb0993 indent: remove 'global' from the list of keywords
Since 1978, 'global' has not been a keyword in C. Moreover, it was
declared as a type while its name would rather suggest a storage class.

Removing the keyword fixes the formatting of variables named 'global'.
2021-10-08 23:40:08 +00:00
rillig b0f2835407 indent: replace unreachable code with assertions
The input buffer is always supposed to be terminated with a newline. The
function inbuf_read_line silently skips null characters. Since the input
buffer is redirected to a temporary buffer in some cases, do not simply
remove this supposed dead code, but replace it with assertions.

In any case, if the code for calling inbuf_read_line had been reachable
and actually allocated a different buffer, continuing to use the pointer
p would have invoked undefined behavior anyway.
2021-10-08 22:37:33 +00:00
rillig 3347524ee6 indent: use standard error handling for unterminated comment
Just writing it to stdout is bad, especially when indent is used in
filter mode. Silently continuing after such an error is bad as well.

echo '/*' | indent
2021-10-08 22:27:52 +00:00
rillig c32c18a8ea indent: remove redundant conditions
No functional change.
2021-10-08 22:22:14 +00:00
rillig 3adc1b45c5 indent: convert ps.box_com to local variable
This variable is only used in a single function, and that function does
not call any other function that could replace the parser state or
install a temporary parser state.

No functional change.
2021-10-08 22:17:35 +00:00
rillig 6fcdb6e0ec indent: clean up 'parse', add test for dangling else
No functional change.
2021-10-08 21:48:33 +00:00
rillig 23b49c9303 indent: clean up typename handling
Unexport typenames list.

Replace standard binary search with custom binary search that returns
the inserting position.

In is_typename, take advantage of the buffer type instead of using
the standard C recipe for str_ends_with.

No functional change.
2021-10-08 21:24:40 +00:00
rillig ccf5156179 indent: enhance comments for lex_number state machine
No functional change.
2021-10-08 21:18:43 +00:00
rillig c5ac72a8b6 indent: improve local variable names
No functional change.
2021-10-08 21:16:23 +00:00
rillig f146a32641 indent: rename fill_buffer to inbuf_read_line
No functional change.
2021-10-08 21:13:58 +00:00
rillig 49f722cbf1 indent: clean up process_decl, replace unnecessary strlen
No functional change.
2021-10-08 20:45:22 +00:00
rillig 95c248aac7 indent: remove unnecessary forward declarations
No functional change.
2021-10-08 20:33:18 +00:00
rillig fa67d8b444 indent: reduce negations in main_loop
No functional change.
2021-10-08 20:28:56 +00:00
rillig 7cd6cf9dcf indent: fix parsing of preprocessor lines with comments and strings 2021-10-08 20:14:52 +00:00
rillig 3b1cc7280f indent: clean up argument handling
Sort the macros, remove redundancy from comment.

Remove redundant lint comment. Lint still does not recognize
__attribute__((__noreturn__)), but it also doesn't perform advanced
control flow analysis, so there is no point in having the comment, as it
doesn't suppress any warnings.

No functional change.
2021-10-08 20:07:44 +00:00
rillig 2b3b5f7140 indent: unexport add_typedefs_from_file
No functional change.
2021-10-08 19:27:20 +00:00
rillig 9d3f9ea8b4 indent: constify detection of function names
No functional change.
2021-10-08 19:22:19 +00:00
rillig 68ab1a6bbf indent: run indent on indent.h
The formatting looks mostly OK.

Some struct members had excessively long names, leaving no space for
their corresponding comments. Renamed some of them using well-known
abbreviations.

The formatting for debug_vis_range is messed up, no idea why. It is
clearly a function declaration, not a function definition, so there is
no need to place the function name in column 1.

No functional change.
2021-10-08 19:03:34 +00:00
rillig 7c11d30c8f indent: fix formatting of C99 comments
The first attempt at formatting C99 comments was conceptually wrong. It
accessed the next token in dump_line, even though that function should
only ever look at the buffers for the label, the code and the current
comment. (Understanding that part of the code was difficult at that time
due to the sheer number of global variables.) The complicated and
ever-growing condition for whether to output the token was a hack and in
retrospect doesn't make sense at all, that's why it only came close to
the intended effect.

Some unintended side effects were that the C99 comments had an
additional space in front of them, and that in some cases an empty line
followed the comment, and that the comments were not aligned.

Previously, the newline that terminates the C99 comment was included in
the comment. Separating the newline from the comment fixed all these
unintended side effects. The only downside is that the multi-line
statement is not indented, but that should be easy to fix.
2021-10-08 18:29:36 +00:00
rillig 5540b66491 indent: split dump_line into smaller functions
No functional change.
2021-10-08 17:56:12 +00:00
rillig dbf390913f indent: reduce scope of t_ptr in process_comment
No functional change.
2021-10-08 17:26:56 +00:00