jkorous created this revision.
jkorous added reviewers: gribozavr, arphaman, dexonsmith.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

- I assume we can cache comments for canonical declarations only, not for every 
redeclaration.
- Caching that we didn't find comments seems to be unnecessary since we try to 
search for comment again again next time if we find this data in cache.
  - We might implement proper cache invalidation in the future and get back to 
using this information.
- Origin of comment (directly from declaration / from some redeclaration) seems 
to not be used anywhere.

I plan to do some performance testing before committing but like to have some 
feedback first.

BTW there's another cache for comments in the `ASTContext` - `ParsedComments`. 
We could try to experiment with different caching approaches to see how it 
affects performance - maybe caching `mapping from canonical declarations to raw 
comments` and separately caching `mapping from raw comments to full comments`.


Repository:
  rC Clang

https://reviews.llvm.org/D60432

Files:
  clang/include/clang/AST/ASTContext.h
  clang/lib/AST/ASTContext.cpp

Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -369,71 +369,43 @@
                                                 const Decl *D,
                                                 const Decl **OriginalDecl) const {
   D = adjustDeclToTemplate(D);
+  const Decl* CanonicalDecl = D->getCanonicalDecl();
 
   // Check whether we have cached a comment for this declaration already.
   {
     llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
-        RedeclComments.find(D);
+        RedeclComments.find(CanonicalDecl);
     if (Pos != RedeclComments.end()) {
       const RawCommentAndCacheFlags &Raw = Pos->second;
-      if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
-        if (OriginalDecl)
-          *OriginalDecl = Raw.getOriginalDecl();
-        return Raw.getRaw();
-      }
+      if (OriginalDecl)
+        *OriginalDecl = Raw.getOriginalDecl();
+      return Raw.getRaw();
     }
   }
 
-  // Search for comments attached to declarations in the redeclaration chain.
-  const RawComment *RC = nullptr;
-  const Decl *OriginalDeclForRC = nullptr;
+  // We don't have comment for D in cache - search for any comment attached to
+  // declarations in the redeclaration chain.
   for (auto I : D->redecls()) {
-    llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
-        RedeclComments.find(I);
-    if (Pos != RedeclComments.end()) {
-      const RawCommentAndCacheFlags &Raw = Pos->second;
-      if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
-        RC = Raw.getRaw();
-        OriginalDeclForRC = Raw.getOriginalDecl();
-        break;
-      }
-    } else {
-      RC = getRawCommentForDeclNoCache(I);
-      OriginalDeclForRC = I;
-      RawCommentAndCacheFlags Raw;
-      if (RC) {
-        // Call order swapped to work around ICE in VS2015 RTM (Release Win32)
-        // https://connect.microsoft.com/VisualStudio/feedback/details/1741530
-        Raw.setKind(RawCommentAndCacheFlags::FromDecl);
-        Raw.setRaw(RC);
-      } else
-        Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl);
-      Raw.setOriginalDecl(I);
-      RedeclComments[I] = Raw;
-      if (RC)
-        break;
-    }
-  }
-
-  // If we found a comment, it should be a documentation comment.
-  assert(!RC || RC->isDocumentation() || LangOpts.CommentOpts.ParseAllComments);
+    if (const RawComment *RC = getRawCommentForDeclNoCache(I)) {
+      // If we find a comment, it should be a documentation comment.
+      assert(RC->isDocumentation() || LangOpts.CommentOpts.ParseAllComments);
 
-  if (OriginalDecl)
-    *OriginalDecl = OriginalDeclForRC;
+      if (OriginalDecl)
+        *OriginalDecl = I;
 
-  // Update cache for every declaration in the redeclaration chain.
-  RawCommentAndCacheFlags Raw;
-  Raw.setRaw(RC);
-  Raw.setKind(RawCommentAndCacheFlags::FromRedecl);
-  Raw.setOriginalDecl(OriginalDeclForRC);
+      RawCommentAndCacheFlags NewCachedComment;
+      // Call order swapped to work around ICE in VS2015 RTM (Release Win32)
+      // https://connect.microsoft.com/VisualStudio/feedback/details/1741530
+      NewCachedComment.setRaw(RC);
+      NewCachedComment.setOriginalDecl(I);
+      RedeclComments[CanonicalDecl] = NewCachedComment;
 
-  for (auto I : D->redecls()) {
-    RawCommentAndCacheFlags &R = RedeclComments[I];
-    if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl)
-      R = Raw;
+      return RC;
+    }
   }
 
-  return RC;
+  // We didn't find any comment attached to any redeclaration of D.
+  return nullptr;
 }
 
 static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod,
Index: clang/include/clang/AST/ASTContext.h
===================================================================
--- clang/include/clang/AST/ASTContext.h
+++ clang/include/clang/AST/ASTContext.h
@@ -727,43 +727,12 @@
 
   class RawCommentAndCacheFlags {
   public:
-    enum Kind {
-      /// We searched for a comment attached to the particular declaration, but
-      /// didn't find any.
-      ///
-      /// getRaw() == 0.
-      NoCommentInDecl = 0,
-
-      /// We have found a comment attached to this particular declaration.
-      ///
-      /// getRaw() != 0.
-      FromDecl,
-
-      /// This declaration does not have an attached comment, and we have
-      /// searched the redeclaration chain.
-      ///
-      /// If getRaw() == 0, the whole redeclaration chain does not have any
-      /// comments.
-      ///
-      /// If getRaw() != 0, it is a comment propagated from other
-      /// redeclaration.
-      FromRedecl
-    };
-
-    Kind getKind() const LLVM_READONLY {
-      return Data.getInt();
-    }
-
-    void setKind(Kind K) {
-      Data.setInt(K);
-    }
-
     const RawComment *getRaw() const LLVM_READONLY {
-      return Data.getPointer();
+      return Comment;
     }
 
     void setRaw(const RawComment *RC) {
-      Data.setPointer(RC);
+      Comment = RC;
     }
 
     const Decl *getOriginalDecl() const LLVM_READONLY {
@@ -775,11 +744,11 @@
     }
 
   private:
-    llvm::PointerIntPair<const RawComment *, 2, Kind> Data;
+    const RawComment * Comment;
     const Decl *OriginalDecl;
   };
 
-  /// Mapping from declarations to comments attached to any
+  /// Mapping from canonical declarations to comments attached to any
   /// redeclaration.
   ///
   /// Raw comments are owned by Comments list.  This mapping is populated
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to