[cfe-commits] r159845 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/AST/RawCommentList.h lib/AST/ASTContext.cpp lib/AST/RawCommentList.cpp lib/Serialization/ASTReader.cpp lib/Serialization/ASTWriter.cpp

Dmitri Gribenko gribozavr at gmail.com
Fri Jul 6 11:19:34 PDT 2012


Author: gribozavr
Date: Fri Jul  6 13:19:34 2012
New Revision: 159845

URL: http://llvm.org/viewvc/llvm-project?rev=159845&view=rev
Log:
Don't store pointers into a std::vector (RawCommentList::Comments).  Although
currently we take address of std::vector's contents only after we finished
adding all comments (so no reallocation can happen), this will change in
future.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/RawCommentList.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/RawCommentList.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=159845&r1=159844&r2=159845&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Fri Jul  6 13:19:34 2012
@@ -440,7 +440,7 @@
 
 public:
   void addComment(const RawComment &RC) {
-    Comments.addComment(RC);
+    Comments.addComment(RC, BumpAlloc);
   }
 
   /// \brief Return the documentation comment attached to a given declaration.

Modified: cfe/trunk/include/clang/AST/RawCommentList.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/RawCommentList.h?rev=159845&r1=159844&r2=159845&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/RawCommentList.h (original)
+++ cfe/trunk/include/clang/AST/RawCommentList.h Fri Jul  6 13:19:34 2012
@@ -146,6 +146,10 @@
     return SM.isBeforeInTranslationUnit(LHS.getSourceRange().getBegin(),
                                         RHS.getSourceRange().getBegin());
   }
+
+  bool operator()(const RawComment *LHS, const RawComment *RHS) {
+    return operator()(*LHS, *RHS);
+  }
 };
 
 /// \brief This class represents all comments included in the translation unit,
@@ -155,19 +159,19 @@
   RawCommentList(SourceManager &SourceMgr) :
     SourceMgr(SourceMgr), OnlyWhitespaceSeen(true) { }
 
-  void addComment(const RawComment &RC);
+  void addComment(const RawComment &RC, llvm::BumpPtrAllocator &Allocator);
 
-  ArrayRef<RawComment> getComments() const {
+  ArrayRef<RawComment *> getComments() const {
     return Comments;
   }
 
 private:
   SourceManager &SourceMgr;
-  std::vector<RawComment> Comments;
+  std::vector<RawComment *> Comments;
   RawComment LastComment;
   bool OnlyWhitespaceSeen;
 
-  void addCommentsToFront(const std::vector<RawComment> &C) {
+  void addCommentsToFront(const std::vector<RawComment *> &C) {
     size_t OldSize = Comments.size();
     Comments.resize(C.size() + OldSize);
     std::copy_backward(Comments.begin(), Comments.begin() + OldSize,

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=159845&r1=159844&r2=159845&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Fri Jul  6 13:19:34 2012
@@ -72,7 +72,7 @@
   if (isa<ParmVarDecl>(D))
     return NULL;
 
-  ArrayRef<RawComment> RawComments = Comments.getComments();
+  ArrayRef<RawComment *> RawComments = Comments.getComments();
 
   // If there are no comments anywhere, we won't find anything.
   if (RawComments.empty())
@@ -85,10 +85,11 @@
     return NULL;
 
   // Find the comment that occurs just after this declaration.
-  ArrayRef<RawComment>::iterator Comment
+  RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc));
+  ArrayRef<RawComment *>::iterator Comment
       = std::lower_bound(RawComments.begin(),
                          RawComments.end(),
-                         RawComment(SourceMgr, SourceRange(DeclLoc)),
+                         &CommentAtDeclLoc,
                          BeforeThanCompare<RawComment>(SourceMgr));
 
   // Decompose the location for the declaration and find the beginning of the
@@ -97,17 +98,17 @@
 
   // First check whether we have a trailing comment.
   if (Comment != RawComments.end() &&
-      Comment->isDocumentation() && Comment->isTrailingComment() &&
+      (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() &&
       !isa<TagDecl>(D) && !isa<NamespaceDecl>(D)) {
     std::pair<FileID, unsigned> CommentBeginDecomp
-      = SourceMgr.getDecomposedLoc(Comment->getSourceRange().getBegin());
+      = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin());
     // Check that Doxygen trailing comment comes after the declaration, starts
     // on the same line and in the same file as the declaration.
     if (DeclLocDecomp.first == CommentBeginDecomp.first &&
         SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second)
           == SourceMgr.getLineNumber(CommentBeginDecomp.first,
                                      CommentBeginDecomp.second)) {
-      return &*Comment;
+      return *Comment;
     }
   }
 
@@ -118,12 +119,12 @@
   --Comment;
 
   // Check that we actually have a non-member Doxygen comment.
-  if (!Comment->isDocumentation() || Comment->isTrailingComment())
+  if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment())
     return NULL;
 
   // Decompose the end of the comment.
   std::pair<FileID, unsigned> CommentEndDecomp
-    = SourceMgr.getDecomposedLoc(Comment->getSourceRange().getEnd());
+    = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd());
 
   // If the comment and the declaration aren't in the same file, then they
   // aren't related.
@@ -146,7 +147,7 @@
   if (Text.find_first_of(",;{}#") != StringRef::npos)
     return NULL;
 
-  return &*Comment;
+  return *Comment;
 }
 
 const RawComment *ASTContext::getRawCommentForDecl(const Decl *D) const {

Modified: cfe/trunk/lib/AST/RawCommentList.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RawCommentList.cpp?rev=159845&r1=159844&r2=159845&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RawCommentList.cpp (original)
+++ cfe/trunk/lib/AST/RawCommentList.cpp Fri Jul  6 13:19:34 2012
@@ -176,14 +176,15 @@
 }
 } // unnamed namespace
 
-void RawCommentList::addComment(const RawComment &RC) {
+void RawCommentList::addComment(const RawComment &RC,
+                                llvm::BumpPtrAllocator &Allocator) {
   if (RC.isInvalid())
     return;
 
   // Check if the comments are not in source order.
   while (!Comments.empty() &&
          !SourceMgr.isBeforeInTranslationUnit(
-              Comments.back().getSourceRange().getBegin(),
+              Comments.back()->getSourceRange().getBegin(),
               RC.getSourceRange().getBegin())) {
     // If they are, just pop a few last comments that don't fit.
     // This happens if an \#include directive contains comments.
@@ -204,12 +205,12 @@
   // If this is the first Doxygen comment, save it (because there isn't
   // anything to merge it with).
   if (Comments.empty()) {
-    Comments.push_back(RC);
+    Comments.push_back(new (Allocator) RawComment(RC));
     OnlyWhitespaceSeen = true;
     return;
   }
 
-  const RawComment &C1 = Comments.back();
+  const RawComment &C1 = *Comments.back();
   const RawComment &C2 = RC;
 
   // Merge comments only if there is only whitespace between them.
@@ -221,11 +222,9 @@
        C1.getEndLine(SourceMgr) + 1 >= C2.getBeginLine(SourceMgr))) {
     SourceRange MergedRange(C1.getSourceRange().getBegin(),
                             C2.getSourceRange().getEnd());
-    RawComment Merged(SourceMgr, MergedRange, true);
-    Comments.pop_back();
-    Comments.push_back(Merged);
+    *Comments.back() = RawComment(SourceMgr, MergedRange, true);
   } else
-    Comments.push_back(RC);
+    Comments.push_back(new (Allocator) RawComment(RC));
 
   OnlyWhitespaceSeen = true;
 }

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=159845&r1=159844&r2=159845&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Fri Jul  6 13:19:34 2012
@@ -6284,7 +6284,7 @@
 }
 
 void ASTReader::ReadComments() {
-  std::vector<RawComment> Comments;
+  std::vector<RawComment *> Comments;
   for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
                                  serialization::ModuleFile *> >::iterator
        I = CommentsCursors.begin(),
@@ -6325,8 +6325,9 @@
             (RawComment::CommentKind) Record[Idx++];
         bool IsTrailingComment = Record[Idx++];
         bool IsAlmostTrailingComment = Record[Idx++];
-        Comments.push_back(RawComment(SR, Kind, IsTrailingComment,
-                                      IsAlmostTrailingComment));
+        Comments.push_back(new (Context) RawComment(SR, Kind,
+                                                    IsTrailingComment,
+                                                    IsAlmostTrailingComment));
         break;
       }
       }

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=159845&r1=159844&r2=159845&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Fri Jul  6 13:19:34 2012
@@ -2245,16 +2245,16 @@
 
 void ASTWriter::WriteComments() {
   Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
-  ArrayRef<RawComment> RawComments = Context->Comments.getComments();
+  ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
   RecordData Record;
-  for (ArrayRef<RawComment>::iterator I = RawComments.begin(),
-                                      E = RawComments.end();
+  for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
+                                        E = RawComments.end();
        I != E; ++I) {
     Record.clear();
-    AddSourceRange(I->getSourceRange(), Record);
-    Record.push_back(I->getKind());
-    Record.push_back(I->isTrailingComment());
-    Record.push_back(I->isAlmostTrailingComment());
+    AddSourceRange((*I)->getSourceRange(), Record);
+    Record.push_back((*I)->getKind());
+    Record.push_back((*I)->isTrailingComment());
+    Record.push_back((*I)->isAlmostTrailingComment());
     Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
   }
   Stream.ExitBlock();





More information about the cfe-commits mailing list