This is an archive of the discontinued LLVM Phabricator instance.

Header guard warning is too agressive
AbandonedPublic

Authored by ismailp on Aug 24 2013, 6:56 AM.

Details

Summary

If the edit distance between the two macros is more than 50%, DefinedMacro may not be header guard, or can be header guard of another header file, or it might be defining something completely different. This can be observed in the wild when handling feature macros or header guards in different files.

// foo.c
#ifndef NO_FLOATING_POINT_SUPPORT
#define USE_SOMETHING_ELSE
#include "foo.h"
/* ... */
#endif /* !defined(NO_FLOATING_POINT_SUPPORT)

Diff Detail

Event Timeline

Ping! This also fixes PR17053.

lib/Lex/PPLexerChange.cpp
293

I haven't tried thoroughly, but I guess it'd be better to print the fix-it on the warning instead of the note, because clang-check will ignore the note (and I need clang-check to apply the fixit).

rtrieu would be a more appropriate reviewer for this.

If I understand the issue correctly, and from reading PR17053, the problem is that the main file in the translation unit would have a structure similar to a header guard but it should not be considered a header guard. This patch does not fix that issue. Instead, it looks at the edit distance between #define and #ifndef macro names. This only coincidentally fixes the examples given. The test case also does not follow the examples as it is still inside a header, not the main file.

It would be better to detect if the lexer is at the top most file and skip the diagnostic then.

lib/Lex/PPLexerChange.cpp
293

This seems like a valid point. There is only one suggestion, so it should be placed in the warning instead of the note, but that change should go in a separate patch.

This warning is turning out to be quite noisy. The edit distance heuristic, while not a complete fix for PR17053, would potentially help make this warning more useful in practice regardless.

For example, I'm seeing a warning on the following code:

#ifndef NO_SYSCALL_LEGACY

#define _NONSTD_SOURCE`

The edit distance heuristic would not warn here. Even if the edit distance heuristic doesn't solve the complete problem, it would be good to make some incremental progress here.

I have no objections to this patch. Keep PR17053 open for now. If you fix the indenting issues I marked, this should be ready for committing.

lib/Lex/PPLexerChange.cpp
278

Add a newline after the "=" and indent four spaces.

286

Indent all the "<<" operators two spaces.

294

80 column violation. Indent these lines 4 past the start of FixItHint

ismailp updated this revision to Unknown Object (????).Oct 11 2013, 2:54 AM

Except line 277, (const size_t MaxHalfLength = std::max...), it's the output from clang-format within range 268:296. Update addresses Richard's comments.

I have seen this warning fired in legacy code. Our case is quite similar to Ted's example. PR17053 is filed after I submitted this. Please don't consider this patch as a fix for PR17053. It seems like this patch, as Richard said previously, coincidentally fixes PR17053 as well.

Committed in r192547.

ismailp abandoned this revision.Aug 10 2014, 3:18 PM

Committed in r192547 (should have been "Close revision", but it wasn't accepted to be closed).