libcpp: Add -Wtrailing-blanks warning

Trailing blanks is something even git diff diagnoses; while it is a coding
style issue, if it is so common that git diff diagnoses it, I think it could
be useful to various projects to check that at compile time.

Dunno if it should be included in -Wextra, currently it isn't, and due to
tons of trailing whitespace in our sources, haven't enabled it for when
building gcc itself either.

Note, git diff also diagnoses indentation with tab following space, wonder
if we couldn't have trivial warning options where one would simply ask for
checking of indentation with no tabs, just spaces vs. indentation with
tabs followed by spaces (but never tab width or more spaces in the
indentation).  I think that would be easy to do also on the libcpp side.
Checking how much something should be exactly indented requires syntax
analysis (at least some limited one) and can consider columns of first token
on line, but what the exact indentation blanks were is something only libcpp
knows.

On Thu, Sep 19, 2024 at 08:17:24AM +0200, Richard Biener wrote:
> Generally I like diagnosing this early.  For the above I'd say -Wtrailing-whitespace=
> with a set of things to diagnose (and a sane default - just spaces and tabs - for
> -Wtrailiing-whitespace) would be nice.  As for naming possibly follow the
> is{space,blank,cntrl} character classifications?  If those are a good
> fit, that is.

The patch currently allows blank (' ' '\t') and space (' ' '\t' '\f' '\v'),
cntrl not yet added, not anything non-ASCII, but in theory could
be added later (though, non-ASCII would be just for inside of comments,
say non-breaking space etc. in the source is otherwise an error).

2024-10-15  Jakub Jelinek  <jakub@redhat.com>

libcpp/
	* include/cpplib.h (struct cpp_options): Add
	cpp_warn_trailing_whitespace member.
	(enum cpp_warning_reason): Add CPP_W_TRAILING_WHITESPACE.
	* internal.h (struct _cpp_line_note): Document 'W' line note.
	* lex.cc (_cpp_clean_line): Add 'W' line note for trailing whitespace
	except for trailing whitespace after backslash.  Formatting fix.
	(_cpp_process_line_notes): Emit -Wtrailing-whitespace diagnostics.
	Formatting fixes.
	(lex_raw_string): Clear type on 'W' notes.
gcc/
	* doc/invoke.texi (Wtrailing-whitespace): Document.
gcc/c-family/
	* c.opt (Wtrailing-whitespace=): New option.
	(Wtrailing-whitespace): New alias.
	* c.opt.urls: Regenerate.
gcc/testsuite/
	* c-c++-common/cpp/Wtrailing-whitespace-1.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-2.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-3.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-4.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-5.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-6.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-7.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-8.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-9.c: New test.
	* c-c++-common/cpp/Wtrailing-whitespace-10.c: New test.
This commit is contained in:
Jakub Jelinek
2024-10-15 07:53:56 +02:00
committed by Jakub Jelinek
parent 384faebde2
commit ac615e1047
16 changed files with 444 additions and 17 deletions

View File

@@ -318,8 +318,8 @@ struct _cpp_line_note
/* Type of note. The 9 'from' trigraph characters represent those
trigraphs, '\\' an escaped newline, ' ' an escaped newline with
intervening space, 0 represents a note that has already been handled,
and anything else is invalid. */
intervening space, 'W' trailing whitespace, 0 represents a note that
has already been handled, and anything else is invalid. */
unsigned int type;
};