aboutsummaryrefslogtreecommitdiff
path: root/lib/AST/ASTContext.cpp
diff options
context:
space:
mode:
authorDmitri Gribenko <gribozavr@gmail.com>2012-07-17 22:01:09 +0000
committerDmitri Gribenko <gribozavr@gmail.com>2012-07-17 22:01:09 +0000
commita444f1856459130bd3a1bb8995331c9e367db04f (patch)
tree517492bbe55f32f7bd3f485b2078c0455ba2a4b9 /lib/AST/ASTContext.cpp
parent75d4b1e34b873e368977e09d8a9681988de1a1a9 (diff)
Implement an optimization for finding the comment that occurs just after a
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
Diffstat (limited to 'lib/AST/ASTContext.cpp')
-rw-r--r--lib/AST/ASTContext.cpp30
1 files changed, 24 insertions, 6 deletions
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index c83e70a72b..2e13d0cc44 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -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.