proposal to add -Wheader-guard option

Prathamesh Kulkarni bilbotheelffriend@gmail.com
Tue Feb 4 07:06:00 GMT 2014


Ping ?

On Fri, Jan 31, 2014 at 12:47 AM, Prathamesh Kulkarni
<bilbotheelffriend@gmail.com> wrote:
> Hi, I was wondering if it's a good idea to add -Wheader-guard option
> that warns on mismatches between #ifndef and #define lines
> in header guard, similar to -Wheader-guard in clang-3.4 ?
> (http://llvm.org/releases/3.4/tools/clang/docs/ReleaseNotes.html)
>
> I have implemented patch for -Wheader-guard (please find it attached).
> Consider a file having the following format:
> #ifndef cmacro (or #if !defined(cmacro) )
> #define dmacro
> // rest of the stuff
> #endif
>
> The warning is triggered if the edit distance
> (http://en.wikipedia.org/wiki/Levenshtein_distance), between cmacro
> and dmacro
> is <= max (len(cmacro), len(dmacro)) / 2
> If the edit distance is more than half, I assume that cmacro
> and dmacro are "very different", and the intent
> was probably not to define header guard (This is what clang does too).
>
> Example:
> #ifndef FOO_H
> #define _FOO_H
> #endif
>
> foo.h:1:0: warning: FOO_H used as header guard followed by #define of
> different macro [-Wheader-guard]
>  #ifndef FOO_H
>  ^
> foo.h:2:0: note: FOO_H is defined here, did you mean _FOO_H ?
>  #define _FOO_H
>  ^
>
> Warning is not triggered in the following cases:
>
> 1] The edit distance between #ifndef (or #!defined) macro
> and #define macro is > half of maximum length between two macros
>
> Example:
> #ifndef FOO
> #define BAR
> #endif
>
> 2] #ifndef and #define are not on consecutive lines (blank lines/ comment lines
> are not ignored).
>
> 3] dmacro gets undefined
> Example:
> #ifndef cmacro
> #define dmacro
> #undef dmacro
> #endif
>
> However the following warning gets generated during the build:
> ../../src/libcpp/directives.c: In function 'void _cpp_pop_buffer(cpp_reader*)':
> ../../src/libcpp/directives.c:
> 2720:59: warning: 'inc_type' may be used
> uninitialized in this function [-Wmaybe-uninitialized]
>        _cpp_pop_file_buffer (pfile, inc, to_free, inc_type);
>                                                            ^
> _cpp_pop_buffer(): http://pastebin.com/aLYLnXJa
> I have defined inc_type only if inc is not null (ie buffer is file
> buffer) in 1st if()
> and used it (passed it to _cpp_pop_file_buffer() ), only if inc is not null
> in 2nd if(). I guess this warning could be considered harmless ?
> How should I should rewrite it to avoid the warning ?
>
> Thanks and Regards,
> Prathamesh



More information about the Gcc mailing list