Commit Graph

155 Commits

Author SHA1 Message Date
rillig 84ea33d3df indent: clean up formatting of comments
No functional change.
2023-06-06 07:14:20 +00:00
rillig c147dd9d85 indent: simplify handling of comments
No functional change.
2023-06-06 06:59:39 +00:00
rillig 6f1b23d83f indent: split printing of comments into smaller functions
No functional change.
2023-06-06 06:51:44 +00:00
rillig c6e9afe1fe indent: remove read pointer from buffers that don't need it
The only buffer that needs a read pointer is the current input line in
'inp'.

No functional change.
2023-06-04 20:51:19 +00:00
rillig 3d214a88cc indent: fix out-of-bounds read when reading a comment 2023-06-04 20:23:12 +00:00
rillig c99fb69e72 tests/indent: fix outdated or wrong comments 2023-05-21 10:18:44 +00:00
rillig 4fb425a29f indent: extract the output state from the parser state
The parser state depends on the preprocessing lines, the output state
shouldn't.
2023-05-20 11:53:53 +00:00
rillig 5a9a628018 indent: implement blank line above block comment 2023-05-20 11:19:17 +00:00
rillig 2d18db1bfc indent: manually wrap overly long lines
No functional change.
2023-05-18 05:33:27 +00:00
rillig 3bf5120b21 indent: switch to standard code style
Taken from share/misc/indent.pro.

Indent does not wrap code to fit into the line width, it only does so
for comments.  The 'INDENT OFF' sections and too long lines will be
addressed in a follow-up commit.

No functional change.
2023-05-18 04:23:03 +00:00
rillig 6c7dba2d21 indent: directly access the input buffer
No functional change.
2023-05-16 13:26:26 +00:00
rillig 81c7fd789d indent: remove support for form feed characters inside a line
Form feeds are occasionally used to split code into pages, and this use
is still supported.  Having a form feed in the middle of a line is
exotic.
2023-05-16 11:32:01 +00:00
rillig 2aaa93dadf indent: fix line wrapping of comments to the right of code 2023-05-15 19:55:51 +00:00
rillig c5f73abc75 indent: let indent format its own code
With manual corrections, as indent does not properly indent multi-line
'?:' expressions nor multi-line controlling expressions.
2023-05-15 09:22:53 +00:00
rillig 9a2185a149 indent: clean up memory and buffer management
Remove the need to explicitly initialize the buffers.  To avoid
subtracting null pointers or comparing them using '<', migrate the
buffers from the (start, end) form to the (start, len) form.  This form
also avoids inconsistencies in whether 'buf.e == buf.s' or 'buf.s ==
buf.e' is used.

Make buffer.st const, to avoid accidental modification of the buffer's
content.

Replace '*buf.e++ = ch' with buf_add_char, to avoid having to keep track
how much unwritten space is left in the buffer.  Remove all safety
margins, that is, no more unchecked access to buf.st[-1] or appending
using '*buf.e++'.

Fix line number counting in lex_word for words that contain line breaks.

No functional change.
2023-05-15 07:28:45 +00:00
rillig 506e3bec5c indent: only null-terminate the buffers if necessary
The only case where a buffer is used as a C-style string is when looking
up a keyword.

No functional change.
2023-05-14 22:26:37 +00:00
rillig 942c4f27e1 indent: shorten variable names for indenting comments
No functional change.
2023-05-14 18:05:52 +00:00
rillig 1304bdca7f indent: fix handling of multiple block comments in a line 2023-05-14 17:53:38 +00:00
rillig 217e4d847d indent: in comments, keep a leading tab
This kind of comments is used for the CVS IDs at the top of files.
2023-05-14 17:13:37 +00:00
rillig 0cd0431ee6 indent: fix vertical spacing after declarations
A comment is not supposed to change the state of the 'blank line after
declaration', but it did.  The initialization of saved_just_saw_decl was
wrong though since it tried to capture the previous value after it had
already been overwritten.
2023-05-14 16:47:06 +00:00
rillig eb14db9dab indent: reduce code for scanning tokens
The input line is guaranteed to end with '\n', so there's no need to
carry another pointer around.

No functional change.
2023-05-14 14:14:07 +00:00
rillig 0081badbbf indent: remove foreign RCS IDs 2023-05-14 12:12:02 +00:00
rillig ae0013d277 indent: use enum instead of magic numbers for tracking declarations
No functional change.
2023-05-13 14:30:48 +00:00
rillig ae2b6a8fde indent: rename struct fields for buffers
No binary change except for assertion line numbers.
2023-05-13 12:31:02 +00:00
rillig 0cdf93c9f3 indent: remove statistics
The numbers from the statistics were wrong.
2023-05-12 10:53:33 +00:00
rillig d54ae284ce indent: remove broken code for handling blank lines
This fixes several bugs where blank lines were erroneously added or
removed, treating these old bugs for new bugs in different places.
These new bugs are expected to be easier to fix, as the old bugs will
not interfere anymore.
2023-05-11 18:13:55 +00:00
rillig 115b3170bc indent: clean up control flow, remove Capsicum
No functional change.
2022-05-09 21:41:49 +00:00
rillig c3f896e4dd indent: group global variables related to output control
No functional change.
2022-04-23 06:43:22 +00:00
rillig 9a2baf090b indent: rename dump functions to output
No functional change.
2021-11-27 18:37:17 +00:00
rillig 80501c8f97 indent: add buf_add_range for adding characters to a buffer
No functional change.
2021-11-26 15:18:18 +00:00
rillig 1039243a64 indent: make fits_in_one_line independent of ps
This makes it easier to understand the function. Having the dependency
to the input line handling is already complicated enough.

No functional change.
2021-11-25 21:01:32 +00:00
rillig 5554de13ae indent: fix accidentally joined and broken comments (since 2019-04-04)
The fixed version is not perfect as it gets the indentation of the last
line of the first comment wrong, but at least indent doesn't generate
malformed output anymore.
2021-11-25 20:44:09 +00:00
rillig 3ad751bf18 tests/indent: demonstrate bugs in comment processing 2021-11-25 20:14:00 +00:00
rillig cfa8e8c653 indent: reduce casts to unsigned char for character classification
No functional change.
2021-11-19 20:23:17 +00:00
rillig fc3d71aded indent: use character input API from pr_comment.c
No functional change.
2021-11-19 18:23:59 +00:00
rillig 9c63f6dcf5 indent: replace direct access to the input buffer
This is a preparation for abstracting away all the low-level details of
handling the input.  The goal is to fix the current bugs regarding line
number counting, out of bounds memory access, and generally unreadable
code.

No functional change.
2021-11-19 17:11:46 +00:00
rillig 97fbe38862 indent: rename input buffer variables
From reading the names 'save_com' and 'sc_end', it was not obvious
enough that these two variables are the limits of the same buffer, the
names were just too unrelated.

No functional change.
2021-11-19 15:32:13 +00:00
rillig b964f9e283 indent: group variables for input handling
No functional change.
2021-11-19 15:28:32 +00:00
rillig 06838bf472 indent: various cleanups
Make several comments more precise.

Rename process_end_of_file to process_eof to match the token name.

Change the order of assignments in analyze_comment to keep the com_ind
computations closer together.

In copy_comment_wrap, use pointer difference instead of pointer addition
to stay away from undefined behavior.

No functional change.
2021-11-07 18:26:17 +00:00
rillig 279e7c4216 indent: remove code that accessed out-of-bounds data from buffer
Saving and restoring the exact buffer position had been necessary before
NetBSD pr_comment.c 1.114. The code accessed the buffer data out of the
bounds of [com.s, com.e), which was rather surprising. More
specifically, it accessed com.e[-1] in a case where com.e == com.s,
relying on com.e[-1] being ' ' in most cases and '*' in the case where
the comment delimiter was written to a separate output line.

Make the code easier understandable by only ever accessing the buffer
data in the bounds [buf.s, buf.e).

No functional change.
2021-11-07 13:38:32 +00:00
rillig 29ea02210a indent: only access buffer data in the range [buf.s, buf.e)
No functional change.
2021-11-07 13:30:15 +00:00
rillig fdc83ca880 indent: do not expand comment buffer unnecessarily
This may have been a simple typo or a really tricky optimization that
isn't obvious even after studying the code for several months. Either of
these is bad, so use the standard form of resetting the buffer.

No functional change.
2021-11-07 12:29:58 +00:00
rillig b4d28b3b05 indent: remove dead code in analyze_comment
The case of an otherwise empty line is already handled further above.
2021-11-07 11:30:45 +00:00
rillig 75adb0d60c indent: remove dead code from copy_comment_wrap
C99 comments are not wrapped, therefore there is no need to check for
them in copy_comment_wrap.

No functional change.
2021-11-07 11:08:25 +00:00
rillig e8411929ad indent: move documentation from process_comment to copy_comment_wrap 2021-11-07 10:56:06 +00:00
rillig 71a16e43e6 indent: clean up process_comment
The assignments to the variables were redundant, they are already done
by analyze_comment.

No functional change.
2021-11-07 10:49:31 +00:00
rillig 1cda593661 indent: remove redundant assignment
At that point, ps.next_col_1 is already false.

No functional change.
2021-11-07 10:42:58 +00:00
rillig 67524fb093 indent: make end of comment detection of nowrap comments simpler
No functional change.
2021-11-07 10:34:03 +00:00
rillig dd120ee616 indent: clean up overcomplicated conditions in copy_comment_nowrap
No functional change.
2021-11-07 10:31:12 +00:00
rillig 0a9d941613 indent: skip redundant conditions in copy_comment_nowrap
No functional change.
2021-11-07 10:26:43 +00:00