[cfe-commits] r141788 - in /cfe/trunk: lib/Lex/PreprocessingRecord.cpp test/Preprocessor/pp-record.c test/Preprocessor/pp-record.h

Argyrios Kyrtzidis akyrtzi at gmail.com
Wed Oct 12 10:36:33 PDT 2011


Author: akirtzidis
Date: Wed Oct 12 12:36:33 2011
New Revision: 141788

URL: http://llvm.org/viewvc/llvm-project?rev=141788&view=rev
Log:
Handle the case where preprocessor entities are not received in order,
fixes http://llvm.org/PR11120

Added:
    cfe/trunk/test/Preprocessor/pp-record.c
    cfe/trunk/test/Preprocessor/pp-record.h
Modified:
    cfe/trunk/lib/Lex/PreprocessingRecord.cpp

Modified: cfe/trunk/lib/Lex/PreprocessingRecord.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PreprocessingRecord.cpp?rev=141788&r1=141787&r2=141788&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PreprocessingRecord.cpp (original)
+++ cfe/trunk/lib/Lex/PreprocessingRecord.cpp Wed Oct 12 12:36:33 2011
@@ -170,11 +170,31 @@
 
 void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
   assert(Entity);
-  assert((PreprocessedEntities.empty() ||
-       !SourceMgr.isBeforeInTranslationUnit(Entity->getSourceRange().getBegin(),
-                   PreprocessedEntities.back()->getSourceRange().getBegin())) &&
-         "Adding a preprocessed entity that is before the previous one in TU");
-  PreprocessedEntities.push_back(Entity);
+  SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
+  
+  // Check normal case, this entity begin location is after the previous one.
+  if (PreprocessedEntities.empty() ||
+      !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
+                   PreprocessedEntities.back()->getSourceRange().getBegin())) {
+    PreprocessedEntities.push_back(Entity);
+    return;
+  }
+
+  // The entity's location is not after the previous one; this can happen rarely
+  // e.g. with "#include MACRO".
+  // Iterate the entities vector in reverse until we find the right place to
+  // insert the new entity.
+  for (std::vector<PreprocessedEntity *>::iterator
+         RI = PreprocessedEntities.end(), Begin = PreprocessedEntities.begin();
+       RI != Begin; --RI) {
+    std::vector<PreprocessedEntity *>::iterator I = RI;
+    --I;
+    if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
+                                           (*I)->getSourceRange().getBegin())) {
+      PreprocessedEntities.insert(RI, Entity);
+      return;
+    }
+  }
 }
 
 void PreprocessingRecord::SetExternalSource(

Added: cfe/trunk/test/Preprocessor/pp-record.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/pp-record.c?rev=141788&view=auto
==============================================================================
--- cfe/trunk/test/Preprocessor/pp-record.c (added)
+++ cfe/trunk/test/Preprocessor/pp-record.c Wed Oct 12 12:36:33 2011
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -detailed-preprocessing-record %s
+
+// http://llvm.org/PR11120
+
+#define FILE_HEADER_NAME "pp-record.h"
+
+#if defined(FILE_HEADER_NAME)
+#include FILE_HEADER_NAME
+#endif

Added: cfe/trunk/test/Preprocessor/pp-record.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/pp-record.h?rev=141788&view=auto
==============================================================================
--- cfe/trunk/test/Preprocessor/pp-record.h (added)
+++ cfe/trunk/test/Preprocessor/pp-record.h Wed Oct 12 12:36:33 2011
@@ -0,0 +1 @@
+// Only useful for #inclusion.





More information about the cfe-commits mailing list