]> granicus.if.org Git - clang/commitdiff
Implement an optimization for finding the comment that occurs just after a
authorDmitri Gribenko <gribozavr@gmail.com>
Tue, 17 Jul 2012 22:01:09 +0000 (22:01 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Tue, 17 Jul 2012 22:01:09 +0000 (22:01 +0000)
given declaration.

It is based on the observation that during parsing the comment that should be
attached to the decl is usually among the last two documentation comments
parsed.

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

lib/AST/ASTContext.cpp

index c83e70a72bf4da0830e749a2dce7d1a0b6a6ae2f..2e13d0cc445485a64f76c0c54e18320ddb1d79a6 100644 (file)
@@ -97,12 +97,30 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
     return NULL;
 
   // Find the comment that occurs just after this declaration.
-  RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc));
-  ArrayRef<RawComment *>::iterator Comment
-      = std::lower_bound(RawComments.begin(),
-                         RawComments.end(),
-                         &CommentAtDeclLoc,
-                         BeforeThanCompare<RawComment>(SourceMgr));
+  ArrayRef<RawComment *>::iterator Comment;
+  {
+    // When searching for comments during parsing, the comment we are looking
+    // for is usually among the last two comments we parsed -- check them
+    // first.
+    RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc));
+    BeforeThanCompare<RawComment> Compare(SourceMgr);
+    ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1;
+    bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
+    if (!Found && RawComments.size() >= 2) {
+      MaybeBeforeDecl--;
+      Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
+    }
+
+    if (Found) {
+      Comment = MaybeBeforeDecl + 1;
+      assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(),
+                                         &CommentAtDeclLoc, Compare));
+    } else {
+      // Slow path.
+      Comment = std::lower_bound(RawComments.begin(), RawComments.end(),
+                                 &CommentAtDeclLoc, Compare);
+    }
+  }
 
   // Decompose the location for the declaration and find the beginning of the
   // file buffer.