[cfe-commits] r95972 - in /cfe/trunk: lib/Lex/PPDirectives.cpp test/Preprocessor/mi_opt2.c test/Preprocessor/mi_opt2.h

Chris Lattner sabre at nondot.org
Fri Feb 12 00:03:27 PST 2010


Author: lattner
Date: Fri Feb 12 02:03:27 2010
New Revision: 95972

URL: http://llvm.org/viewvc/llvm-project?rev=95972&view=rev
Log:
Fix PR6282: the include guard optimization cannot happen if the
guard macro is already defined for the first occurrence of the
header.  If it is, the body will be skipped and not be properly
analyzed for the include guard optimization.

Added:
    cfe/trunk/test/Preprocessor/mi_opt2.c
    cfe/trunk/test/Preprocessor/mi_opt2.h
Modified:
    cfe/trunk/lib/Lex/PPDirectives.cpp

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=95972&r1=95971&r2=95972&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Fri Feb 12 02:03:27 2010
@@ -1514,18 +1514,21 @@
   // Check to see if this is the last token on the #if[n]def line.
   CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
 
+  IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
+  MacroInfo *MI = getMacroInfo(MII);
+  
   if (CurPPLexer->getConditionalStackDepth() == 0) {
-    // If the start of a top-level #ifdef, inform MIOpt.
-    if (!ReadAnyTokensBeforeDirective) {
+    // If the start of a top-level #ifdef and if the macro is not defined,
+    // inform MIOpt that this might be the start of a proper include guard.
+    // Otherwise it is some other form of unknown conditional which we can't
+    // handle.
+    if (!ReadAnyTokensBeforeDirective && MI == 0) {
       assert(isIfndef && "#ifdef shouldn't reach here");
-      CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
+      CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII);
     } else
       CurPPLexer->MIOpt.EnterTopLevelConditional();
   }
 
-  IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
-  MacroInfo *MI = getMacroInfo(MII);
-
   // If there is a macro, process it.
   if (MI)  // Mark it used.
     MI->setIsUsed(true);
@@ -1558,7 +1561,7 @@
   // If this condition is equivalent to #ifndef X, and if this is the first
   // directive seen, handle it for the multiple-include optimization.
   if (CurPPLexer->getConditionalStackDepth() == 0) {
-    if (!ReadAnyTokensBeforeDirective && IfNDefMacro)
+    if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
       CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
     else
       CurPPLexer->MIOpt.EnterTopLevelConditional();

Added: cfe/trunk/test/Preprocessor/mi_opt2.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/mi_opt2.c?rev=95972&view=auto

==============================================================================
--- cfe/trunk/test/Preprocessor/mi_opt2.c (added)
+++ cfe/trunk/test/Preprocessor/mi_opt2.c Fri Feb 12 02:03:27 2010
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -E %s | FileCheck %s
+// PR6282
+// This test should not trigger the include guard optimization since
+// the guard macro is defined on the first include.
+
+#define ITERATING 1
+#define X 1
+#include "mi_opt2.h"
+#undef X
+#define X 2
+#include "mi_opt2.h"
+
+// CHECK: b: 1
+// CHECK: b: 2
+

Added: cfe/trunk/test/Preprocessor/mi_opt2.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/mi_opt2.h?rev=95972&view=auto

==============================================================================
--- cfe/trunk/test/Preprocessor/mi_opt2.h (added)
+++ cfe/trunk/test/Preprocessor/mi_opt2.h Fri Feb 12 02:03:27 2010
@@ -0,0 +1,5 @@
+#ifndef ITERATING
+a: X
+#else
+b: X
+#endif





More information about the cfe-commits mailing list