Author: Hardik Kumar
Date: 2026-06-29T10:35:29+02:00
New Revision: b426dd530b44bb92eaf95777f9401b7fe66a531a

URL: 
https://github.com/llvm/llvm-project/commit/b426dd530b44bb92eaf95777f9401b7fe66a531a
DIFF: 
https://github.com/llvm/llvm-project/commit/b426dd530b44bb92eaf95777f9401b7fe66a531a.diff

LOG: [clang][Lex] Optimize the FileCheckPoints search in Preprocessor (#206356)

Resolves a FIXME and improved linear search by replacing with a binary
search on the FileCheckPoints SmallVector in Lex/Preprocessor.

- used std::upper_bound to find the upper bound of the Start position.
- handle special case if the Start is less than all elements in
FileCheckPoints so returns a nullptr;
- else return the dereferenced value of the CheckPoint just before Start
point.

Added: 
    

Modified: 
    clang/lib/Lex/Preprocessor.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index c69d084d6514f..41bc52174afd8 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -1746,16 +1746,11 @@ void Preprocessor::removePPCallbacks() {
 const char *Preprocessor::getCheckPoint(FileID FID, const char *Start) const {
   if (auto It = CheckPoints.find(FID); It != CheckPoints.end()) {
     const SmallVector<const char *> &FileCheckPoints = It->second;
-    const char *Last = nullptr;
-    // FIXME: Do better than a linear search.
-    for (const char *P : FileCheckPoints) {
-      if (P > Start)
-        break;
-      Last = P;
-    }
-    return Last;
+    auto P = llvm::upper_bound(FileCheckPoints, Start);
+    if (P == FileCheckPoints.begin())
+      return nullptr;
+    return *std::prev(P);
   }
-
   return nullptr;
 }
 


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to