[cfe-commits] r156557 - in /cfe/trunk: include/clang/Lex/Preprocessor.h lib/Lex/PPMacroExpansion.cpp test/Preprocessor/pp-record.c

Argyrios Kyrtzidis akyrtzi at gmail.com
Thu May 10 11:57:19 PDT 2012


Author: akirtzidis
Date: Thu May 10 13:57:19 2012
New Revision: 156557

URL: http://llvm.org/viewvc/llvm-project?rev=156557&view=rev
Log:
[preprocessor] Make sure that MacroExpands callbacks are always in source order.

Fixes assertion hit in the preprocessing record. rdar://11426523

Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/test/Preprocessor/pp-record.c

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=156557&r1=156556&r2=156557&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Thu May 10 13:57:19 2012
@@ -254,6 +254,15 @@
   /// encountered (e.g. a file is #included, etc).
   PPCallbacks *Callbacks;
 
+  struct MacroExpandsInfo {
+    Token Tok;
+    MacroInfo *MI;
+    SourceRange Range;
+    MacroExpandsInfo(Token Tok, MacroInfo *MI, SourceRange Range)
+      : Tok(Tok), MI(MI), Range(Range) { }
+  };
+  SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
+
   /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping
   /// to the actual definition of the macro.
   llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=156557&r1=156556&r2=156557&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Thu May 10 13:57:19 2012
@@ -242,9 +242,27 @@
 
   // Remember where the token is expanded.
   SourceLocation ExpandLoc = Identifier.getLocation();
+  SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
 
-  if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
-                                         SourceRange(ExpandLoc, ExpansionEnd));
+  if (Callbacks) {
+    if (InMacroArgs) {
+      // We can have macro expansion inside a conditional directive while
+      // reading the function macro arguments. To ensure, in that case, that
+      // MacroExpands callbacks still happen in source order, queue this
+      // callback to have it happen after the function macro callback.
+      DelayedMacroExpandsCallbacks.push_back(
+                              MacroExpandsInfo(Identifier, MI, ExpansionRange));
+    } else {
+      Callbacks->MacroExpands(Identifier, MI, ExpansionRange);
+      if (!DelayedMacroExpandsCallbacks.empty()) {
+        for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
+          MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
+          Callbacks->MacroExpands(Info.Tok, Info.MI, Info.Range);
+        }
+        DelayedMacroExpandsCallbacks.clear();
+      }
+    }
+  }
   
   // If we started lexing a macro, enter the macro expansion body.
 

Modified: cfe/trunk/test/Preprocessor/pp-record.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/pp-record.c?rev=156557&r1=156556&r2=156557&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/pp-record.c (original)
+++ cfe/trunk/test/Preprocessor/pp-record.c Thu May 10 13:57:19 2012
@@ -10,3 +10,14 @@
 #include STRINGIZE(INC)
 
 CAKE;
+
+#define DIR 1
+#define FNM(x) x
+
+FNM(
+#if DIR
+    int a;
+#else
+    int b;
+#endif
+)





More information about the cfe-commits mailing list