From: Ismail Pazarbasi Date: Sat, 12 Oct 2013 23:17:37 +0000 (+0000) Subject: Relax header guard mismatch warning with edit distance heuristic. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=223e7a8b0fadbbc9eaea1fb460cd90c8d9c8dd28;p=clang Relax header guard mismatch warning with edit distance heuristic. 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 set by the build environment. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@192547 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Lex/PPLexerChange.cpp b/lib/Lex/PPLexerChange.cpp index 1193c3ce4e..1f970a4450 100644 --- a/lib/Lex/PPLexerChange.cpp +++ b/lib/Lex/PPLexerChange.cpp @@ -264,19 +264,32 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { if (!ControllingMacro->hasMacroDefinition() && DefinedMacro != ControllingMacro && HeaderInfo.FirstTimeLexingFile(FE)) { - // Emit a warning for a bad header guard. - Diag(CurPPLexer->MIOpt.GetMacroLocation(), - diag::warn_header_guard) - << CurPPLexer->MIOpt.GetMacroLocation() - << ControllingMacro; - Diag(CurPPLexer->MIOpt.GetDefinedLocation(), - diag::note_header_guard) - << CurPPLexer->MIOpt.GetDefinedLocation() - << DefinedMacro - << ControllingMacro - << FixItHint::CreateReplacement( - CurPPLexer->MIOpt.GetDefinedLocation(), - ControllingMacro->getName()); + + // 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. Therefore, it maybe defining something + // completely different. This can be observed in the wild when + // handling feature macros or header guards in different files. + + const StringRef ControllingMacroName = ControllingMacro->getName(); + const StringRef DefinedMacroName = DefinedMacro->getName(); + const size_t MaxHalfLength = std::max(ControllingMacroName.size(), + DefinedMacroName.size()) / 2; + const unsigned ED = ControllingMacroName.edit_distance( + DefinedMacroName, true, MaxHalfLength); + if (ED <= MaxHalfLength) { + // Emit a warning for a bad header guard. + Diag(CurPPLexer->MIOpt.GetMacroLocation(), + diag::warn_header_guard) + << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro; + Diag(CurPPLexer->MIOpt.GetDefinedLocation(), + diag::note_header_guard) + << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro + << ControllingMacro + << FixItHint::CreateReplacement( + CurPPLexer->MIOpt.GetDefinedLocation(), + ControllingMacro->getName()); + } } } } diff --git a/test/Lexer/Inputs/unlikely-to-be-header-guard.h b/test/Lexer/Inputs/unlikely-to-be-header-guard.h new file mode 100644 index 0000000000..098fc3cb7a --- /dev/null +++ b/test/Lexer/Inputs/unlikely-to-be-header-guard.h @@ -0,0 +1,5 @@ +#ifndef data_rep_h +#define use_alternate_data_rep +/* #include "data_rep.h" */ +#endif + diff --git a/test/Lexer/header.cpp b/test/Lexer/header.cpp index 047240ac0f..f02b1e69db 100644 --- a/test/Lexer/header.cpp +++ b/test/Lexer/header.cpp @@ -6,6 +6,7 @@ #include "Inputs/different-define.h" #include "Inputs/out-of-order-define.h" #include "Inputs/tokens-between-ifndef-and-define.h" +#include "Inputs/unlikely-to-be-header-guard.h" #include "Inputs/bad-header-guard.h" // CHECK: In file included from {{.*}}header.cpp:{{[0-9]*}}: