]> granicus.if.org Git - clang/commitdiff
[clang][ASTContext] Try to exit early before loading serialized comments from AST...
authorJan Korous <jkorous@apple.com>
Wed, 10 Apr 2019 20:23:33 +0000 (20:23 +0000)
committerJan Korous <jkorous@apple.com>
Wed, 10 Apr 2019 20:23:33 +0000 (20:23 +0000)
Loading external comments is expensive. This change probably doesn't apply to common cases but is almost for free and would save some work in case none of the declaration needs external comments to be loaded.

Differential Revision: https://reviews.llvm.org/D60493

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

lib/AST/ASTContext.cpp

index dee12a5e41115c09c6455e4d28bfd65ec4a06bf2..364d3b819770288ea23042f1de954fa2f11f35de 100644 (file)
@@ -99,20 +99,13 @@ enum FloatingRank {
 };
 
 RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
-  if (!CommentsLoaded && ExternalSource) {
-    ExternalSource->ReadComments();
-
-#ifndef NDEBUG
-    ArrayRef<RawComment *> RawComments = Comments.getComments();
-    assert(std::is_sorted(RawComments.begin(), RawComments.end(),
-                          BeforeThanCompare<RawComment>(SourceMgr)));
-#endif
-
-    CommentsLoaded = true;
-  }
-
   assert(D);
 
+  // If we already tried to load comments but there are none,
+  // we won't find anything.
+  if (CommentsLoaded && Comments.getComments().empty())
+    return nullptr;
+
   // User can not attach documentation to implicit declarations.
   if (D->isImplicit())
     return nullptr;
@@ -162,12 +155,6 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
       isa<TemplateTemplateParmDecl>(D))
     return nullptr;
 
-  ArrayRef<RawComment *> RawComments = Comments.getComments();
-
-  // If there are no comments anywhere, we won't find anything.
-  if (RawComments.empty())
-    return nullptr;
-
   // Find declaration location.
   // For Objective-C declarations we generally don't expect to have multiple
   // declarators, thus use declaration starting location as the "declaration
@@ -206,6 +193,23 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
   if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
     return nullptr;
 
+  if (!CommentsLoaded && ExternalSource) {
+    ExternalSource->ReadComments();
+
+#ifndef NDEBUG
+    ArrayRef<RawComment *> RawComments = Comments.getComments();
+    assert(std::is_sorted(RawComments.begin(), RawComments.end(),
+                          BeforeThanCompare<RawComment>(SourceMgr)));
+#endif
+
+    CommentsLoaded = true;
+  }
+
+  ArrayRef<RawComment *> RawComments = Comments.getComments();
+  // If there are no comments anywhere, we won't find anything.
+  if (RawComments.empty())
+    return nullptr;
+
   // Find the comment that occurs just after this declaration.
   ArrayRef<RawComment *>::iterator Comment;
   {