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.
Showing
- gcc/c-family/c.opt 20 additions, 0 deletionsgcc/c-family/c.opt
- gcc/c-family/c.opt.urls 6 additions, 0 deletionsgcc/c-family/c.opt.urls
- gcc/doc/invoke.texi 17 additions, 1 deletiongcc/doc/invoke.texi
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-1.c 37 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-1.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-10.c 29 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-10.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-2.c 37 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-2.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-3.c 43 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-3.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-4.c 28 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-4.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-5.c 28 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-5.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-6.c 38 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-6.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-7.c 38 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-7.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-8.c 44 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-8.c
- gcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-9.c 29 additions, 0 deletionsgcc/testsuite/c-c++-common/cpp/Wtrailing-whitespace-9.c
- libcpp/include/cpplib.h 5 additions, 1 deletionlibcpp/include/cpplib.h
- libcpp/internal.h 2 additions, 2 deletionslibcpp/internal.h
- libcpp/lex.cc 43 additions, 13 deletionslibcpp/lex.cc
Loading
Please register or sign in to comment