r192547 - Relax header guard mismatch warning with edit distance heuristic.

Ismail Pazarbasi ismail.pazarbasi at gmail.com
Sat Oct 12 16:17:38 PDT 2013


Author: ismailp
Date: Sat Oct 12 18:17:37 2013
New Revision: 192547

URL: http://llvm.org/viewvc/llvm-project?rev=192547&view=rev
Log:
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.

Added:
    cfe/trunk/test/Lexer/Inputs/unlikely-to-be-header-guard.h
Modified:
    cfe/trunk/lib/Lex/PPLexerChange.cpp
    cfe/trunk/test/Lexer/header.cpp

Modified: cfe/trunk/lib/Lex/PPLexerChange.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPLexerChange.cpp?rev=192547&r1=192546&r2=192547&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPLexerChange.cpp (original)
+++ cfe/trunk/lib/Lex/PPLexerChange.cpp Sat Oct 12 18:17:37 2013
@@ -264,19 +264,32 @@ bool Preprocessor::HandleEndOfFile(Token
           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());
+            }
           }
         }
       }

Added: cfe/trunk/test/Lexer/Inputs/unlikely-to-be-header-guard.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/Inputs/unlikely-to-be-header-guard.h?rev=192547&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/Inputs/unlikely-to-be-header-guard.h (added)
+++ cfe/trunk/test/Lexer/Inputs/unlikely-to-be-header-guard.h Sat Oct 12 18:17:37 2013
@@ -0,0 +1,5 @@
+#ifndef data_rep_h
+#define use_alternate_data_rep
+/* #include "data_rep.h" */
+#endif
+

Modified: cfe/trunk/test/Lexer/header.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/header.cpp?rev=192547&r1=192546&r2=192547&view=diff
==============================================================================
--- cfe/trunk/test/Lexer/header.cpp (original)
+++ cfe/trunk/test/Lexer/header.cpp Sat Oct 12 18:17:37 2013
@@ -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]*}}:





More information about the cfe-commits mailing list