Skip to content
Snippets Groups Projects
  • Jakub Jelinek's avatar
    d4499a23
    libcpp: Add -Wleading-whitespace= warning · d4499a23
    Jakub Jelinek authored
    The following patch on top of the r15-4346 patch adds
    -Wleading-whitespace= warning option.
    This warning doesn't care how much one actually indents which line
    in the source (that is something that can't be easily done in the
    preprocessor without doing syntactic analysis), but just simple checks
    on what kind of whitespace is used in the indentation.
    I think it is still useful to get warnings about such issues early,
    while git diagnoses some of it in patches (e.g. the tab after space
    case), getting the warnings earlier might help avoiding such issues
    sooner.
    
    There are projects which ban use of tabs and require just spaces,
    others which require indentation just with horizontal tabs, and finally
    projects which want indentation with tabs for multiples of tabstop size
    followed by spaces (fewer than tabstop size), like GCC.
    For all 3 kinds the warning diagnoses indentation with '\v' or '\f'
    characters (unless line contains just whitespace), and for the last one
    also cases where a space in the indentation is followed by horizontal
    tab or where there are N or more consecutive spaces in the indentation
    (for -ftabstop=N).
    
    BTW, for additional testing I've enabled the warnings (without -Werror
    for them) in stage3.  There are many warnings (both trailing and leading
    whitespace), some of them something that can be easily fixed in the headers
    or source files, but others with whitespace issues in generated sources,
    so if we enable the warnings, either we'd need to adjust the generators
    or disable the warnings in (some of the) generated files.
    
    2024-10-23  Jakub Jelinek  <jakub@redhat.com>
    
    libcpp/
    	* include/cpplib.h (struct cpp_options): Add
    	cpp_warn_leading_whitespace and cpp_tabstop members.
    	(enum cpp_warning_reason): Add CPP_W_LEADING_WHITESPACE.
    	* internal.h (struct _cpp_line_note): Document new
    	line note kinds.
    	* init.cc (cpp_create_reader): Set cpp_tabstop to 8.
    	* lex.cc (find_leading_whitespace_issues): New function.
    	(_cpp_clean_line): Use it.
    	(_cpp_process_line_notes): Handle 'L', 'S' and 'T' line notes.
    	(lex_raw_string): Clear type on 'L', 'S' and 'T' line notes
    	inside of raw string literals.
    gcc/
    	* doc/invoke.texi (Wleading-whitespace=): Document.
    gcc/c-family/
    	* c.opt (Wleading-whitespace=): New option.
    	* c-opts.cc (c_common_post_options): Set cpp_opts->cpp_tabstop
    	to global_dc->m_tabstop.
    gcc/testsuite/
    	* c-c++-common/cpp/Wleading-whitespace-1.c: New test.
    	* c-c++-common/cpp/Wleading-whitespace-2.c: New test.
    	* c-c++-common/cpp/Wleading-whitespace-3.c: New test.
    	* c-c++-common/cpp/Wleading-whitespace-4.c: New test.
    d4499a23
    History
    libcpp: Add -Wleading-whitespace= warning
    Jakub Jelinek authored
    The following patch on top of the r15-4346 patch adds
    -Wleading-whitespace= warning option.
    This warning doesn't care how much one actually indents which line
    in the source (that is something that can't be easily done in the
    preprocessor without doing syntactic analysis), but just simple checks
    on what kind of whitespace is used in the indentation.
    I think it is still useful to get warnings about such issues early,
    while git diagnoses some of it in patches (e.g. the tab after space
    case), getting the warnings earlier might help avoiding such issues
    sooner.
    
    There are projects which ban use of tabs and require just spaces,
    others which require indentation just with horizontal tabs, and finally
    projects which want indentation with tabs for multiples of tabstop size
    followed by spaces (fewer than tabstop size), like GCC.
    For all 3 kinds the warning diagnoses indentation with '\v' or '\f'
    characters (unless line contains just whitespace), and for the last one
    also cases where a space in the indentation is followed by horizontal
    tab or where there are N or more consecutive spaces in the indentation
    (for -ftabstop=N).
    
    BTW, for additional testing I've enabled the warnings (without -Werror
    for them) in stage3.  There are many warnings (both trailing and leading
    whitespace), some of them something that can be easily fixed in the headers
    or source files, but others with whitespace issues in generated sources,
    so if we enable the warnings, either we'd need to adjust the generators
    or disable the warnings in (some of the) generated files.
    
    2024-10-23  Jakub Jelinek  <jakub@redhat.com>
    
    libcpp/
    	* include/cpplib.h (struct cpp_options): Add
    	cpp_warn_leading_whitespace and cpp_tabstop members.
    	(enum cpp_warning_reason): Add CPP_W_LEADING_WHITESPACE.
    	* internal.h (struct _cpp_line_note): Document new
    	line note kinds.
    	* init.cc (cpp_create_reader): Set cpp_tabstop to 8.
    	* lex.cc (find_leading_whitespace_issues): New function.
    	(_cpp_clean_line): Use it.
    	(_cpp_process_line_notes): Handle 'L', 'S' and 'T' line notes.
    	(lex_raw_string): Clear type on 'L', 'S' and 'T' line notes
    	inside of raw string literals.
    gcc/
    	* doc/invoke.texi (Wleading-whitespace=): Document.
    gcc/c-family/
    	* c.opt (Wleading-whitespace=): New option.
    	* c-opts.cc (c_common_post_options): Set cpp_opts->cpp_tabstop
    	to global_dc->m_tabstop.
    gcc/testsuite/
    	* c-c++-common/cpp/Wleading-whitespace-1.c: New test.
    	* c-c++-common/cpp/Wleading-whitespace-2.c: New test.
    	* c-c++-common/cpp/Wleading-whitespace-3.c: New test.
    	* c-c++-common/cpp/Wleading-whitespace-4.c: New test.