]> granicus.if.org Git - clang/commitdiff
Do manual binary search for preprocessing entities because their end locations
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 22 Sep 2011 21:17:02 +0000 (21:17 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 22 Sep 2011 21:17:02 +0000 (21:17 +0000)
may be unordered and MSVC's debug-mode doesn't like it.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140337 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Lex/PreprocessingRecord.cpp
lib/Serialization/ASTReader.cpp

index 438eba00ba3992c45b92f46d6332e4e8509eb057..0f5577520d69aa5780a9673075a442fc08abaa85 100644 (file)
@@ -129,12 +129,30 @@ unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
   if (SourceMgr.isLoadedSourceLocation(Loc))
     return 0;
 
+  size_t Count = PreprocessedEntities.size();
+  size_t Half;
   std::vector<PreprocessedEntity *>::const_iterator
-    I = std::lower_bound(PreprocessedEntities.begin(),
-                         PreprocessedEntities.end(),
-                         Loc,
-                         PPEntityComp<&SourceRange::getEnd>(SourceMgr));
-  return I - PreprocessedEntities.begin();
+    First = PreprocessedEntities.begin();
+  std::vector<PreprocessedEntity *>::const_iterator I;
+
+  // Do a binary search manually instead of using std::lower_bound because
+  // The end locations of entities may be unordered (when a macro expansion
+  // is inside another macro argument), but for this case it is not important
+  // whether we get the first macro expansion or its containing macro.
+  while (Count > 0) {
+    Half = Count/2;
+    I = First;
+    std::advance(I, Half);
+    if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
+                                            Loc)){
+      First = I;
+      ++First;
+      Count = Count - Half - 1;
+    } else
+      Count = Half;
+  }
+
+  return First - PreprocessedEntities.begin();
 }
 
 unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
index 5f489d4067864ae596739d93976230dba851684c..f83f709ebef455928b3d662c36a6bb53950dcbf4 100644 (file)
@@ -2947,9 +2947,28 @@ ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
   typedef const PPEntityOffset *pp_iterator;
   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
-  pp_iterator PPI =
-      std::lower_bound(pp_begin, pp_end, BLoc,
-                       PPEntityComp<&PPEntityOffset::End>(*this, M));
+
+  size_t Count = M.NumPreprocessedEntities;
+  size_t Half;
+  pp_iterator First = pp_begin;
+  pp_iterator PPI;
+
+  // Do a binary search manually instead of using std::lower_bound because
+  // The end locations of entities may be unordered (when a macro expansion
+  // is inside another macro argument), but for this case it is not important
+  // whether we get the first macro expansion or its containing macro.
+  while (Count > 0) {
+    Half = Count/2;
+    PPI = First;
+    std::advance(PPI, Half);
+    if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
+                                            BLoc)){
+      First = PPI;
+      ++First;
+      Count = Count - Half - 1;
+    } else
+      Count = Half;
+  }
 
   if (PPI == pp_end)
     return findNextPreprocessedEntity(SLocMapI);