aboutsummaryrefslogtreecommitdiff
path: root/lib/AST
diff options
context:
space:
mode:
authorDmitri Gribenko <gribozavr@gmail.com>2012-08-11 00:51:43 +0000
committerDmitri Gribenko <gribozavr@gmail.com>2012-08-11 00:51:43 +0000
commitf50555eedef33fd5a67d369aa0ae8a6f1d201543 (patch)
tree8bce783eed109a75cf613bf47cf2e881fac5745c /lib/AST
parentf9c29088a8f64d4af2423fb7b556419597c996df (diff)
Attaching comments to declarations: find comment attached to any redeclaration
Not only look for the comment near the declaration itself, but also walk the redeclaration chain: the previous declaration might have had a documentation comment. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@161722 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/AST')
-rw-r--r--lib/AST/ASTContext.cpp98
-rw-r--r--lib/AST/RawCommentList.cpp34
2 files changed, 88 insertions, 44 deletions
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index 4e8587d191..10cba7a813 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -13,11 +13,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/CharUnits.h"
-#include "clang/AST/Comment.h"
#include "clang/AST/CommentCommandTraits.h"
-#include "clang/AST/CommentLexer.h"
-#include "clang/AST/CommentSema.h"
-#include "clang/AST/CommentParser.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
@@ -149,6 +145,7 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second)
== SourceMgr.getLineNumber(CommentBeginDecomp.first,
CommentBeginDecomp.second)) {
+ (*Comment)->setDecl(D);
return *Comment;
}
}
@@ -188,59 +185,74 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
if (Text.find_first_of(",;{}#@") != StringRef::npos)
return NULL;
+ (*Comment)->setDecl(D);
return *Comment;
}
-const RawComment *ASTContext::getRawCommentForDecl(const Decl *D) const {
- // Check whether we have cached a comment string for this declaration
- // already.
- llvm::DenseMap<const Decl *, RawAndParsedComment>::iterator Pos
- = DeclComments.find(D);
- if (Pos != DeclComments.end()) {
- RawAndParsedComment C = Pos->second;
- return C.first;
+const RawComment *ASTContext::getRawCommentForAnyRedecl(const Decl *D) const {
+ // Check whether we have cached a comment for this declaration already.
+ {
+ llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
+ RedeclComments.find(D);
+ if (Pos != RedeclComments.end()) {
+ const RawCommentAndCacheFlags &Raw = Pos->second;
+ if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl)
+ return Raw.getRaw();
+ }
+ }
+
+ // Search for comments attached to declarations in the redeclaration chain.
+ const RawComment *RC = NULL;
+ for (Decl::redecl_iterator I = D->redecls_begin(),
+ E = D->redecls_end();
+ I != E; ++I) {
+ 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();
+ break;
+ }
+ } else {
+ RC = getRawCommentForDeclNoCache(*I);
+ RawCommentAndCacheFlags Raw;
+ if (RC) {
+ Raw.setRaw(RC);
+ Raw.setKind(RawCommentAndCacheFlags::FromDecl);
+ } else
+ Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl);
+ RedeclComments[*I] = Raw;
+ if (RC)
+ break;
+ }
}
- RawComment *RC = getRawCommentForDeclNoCache(D);
// If we found a comment, it should be a documentation comment.
assert(!RC || RC->isDocumentation());
- DeclComments[D] =
- RawAndParsedComment(RC, static_cast<comments::FullComment *>(NULL));
- if (RC)
- RC->setAttached();
+
+ // Update cache for every declaration in the redeclaration chain.
+ RawCommentAndCacheFlags Raw;
+ Raw.setRaw(RC);
+ Raw.setKind(RawCommentAndCacheFlags::FromRedecl);
+
+ for (Decl::redecl_iterator I = D->redecls_begin(),
+ E = D->redecls_end();
+ I != E; ++I) {
+ RawCommentAndCacheFlags &R = RedeclComments[*I];
+ if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl)
+ R = Raw;
+ }
+
return RC;
}
comments::FullComment *ASTContext::getCommentForDecl(const Decl *D) const {
- llvm::DenseMap<const Decl *, RawAndParsedComment>::iterator Pos
- = DeclComments.find(D);
- const RawComment *RC;
- if (Pos != DeclComments.end()) {
- RawAndParsedComment C = Pos->second;
- if (comments::FullComment *FC = C.second)
- return FC;
- RC = C.first;
- } else
- RC = getRawCommentForDecl(D);
-
+ const RawComment *RC = getRawCommentForAnyRedecl(D);
if (!RC)
return NULL;
- const StringRef RawText = RC->getRawText(SourceMgr);
- comments::CommandTraits Traits;
- comments::Lexer L(getAllocator(), Traits,
- RC->getSourceRange().getBegin(), comments::CommentOptions(),
- RawText.begin(), RawText.end());
-
- comments::Sema S(getAllocator(), getSourceManager(), getDiagnostics(),
- Traits);
- S.setDecl(D);
- comments::Parser P(L, S, getAllocator(), getSourceManager(),
- getDiagnostics(), Traits);
-
- comments::FullComment *FC = P.parseFullComment();
- DeclComments[D].second = FC;
- return FC;
+ return RC->getParsed(*this);
}
void
diff --git a/lib/AST/RawCommentList.cpp b/lib/AST/RawCommentList.cpp
index 4f7165f0e4..c704cabe69 100644
--- a/lib/AST/RawCommentList.cpp
+++ b/lib/AST/RawCommentList.cpp
@@ -9,8 +9,11 @@
#include "clang/AST/RawCommentList.h"
#include "clang/AST/ASTContext.h"
+#include "clang/AST/Comment.h"
#include "clang/AST/CommentLexer.h"
#include "clang/AST/CommentBriefParser.h"
+#include "clang/AST/CommentSema.h"
+#include "clang/AST/CommentParser.h"
#include "clang/AST/CommentCommandTraits.h"
#include "llvm/ADT/STLExtras.h"
@@ -62,7 +65,7 @@ bool mergedCommentIsTrailingComment(StringRef Comment) {
RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
bool Merged) :
Range(SR), RawTextValid(false), BriefTextValid(false),
- IsAttached(false), IsAlmostTrailingComment(false),
+ IsAlmostTrailingComment(false),
BeginLineValid(false), EndLineValid(false) {
// Extract raw comment text, if possible.
if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
@@ -84,6 +87,16 @@ RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
}
}
+const Decl *RawComment::getDecl() const {
+ if (DeclOrParsedComment.isNull())
+ return NULL;
+
+ if (const Decl *D = DeclOrParsedComment.dyn_cast<const Decl *>())
+ return D;
+
+ return DeclOrParsedComment.get<comments::FullComment *>()->getDecl();
+}
+
unsigned RawComment::getBeginLine(const SourceManager &SM) const {
if (BeginLineValid)
return BeginLine;
@@ -156,6 +169,25 @@ const char *RawComment::extractBriefText(const ASTContext &Context) const {
return BriefTextPtr;
}
+comments::FullComment *RawComment::parse(const ASTContext &Context) const {
+ // Make sure that RawText is valid.
+ getRawText(Context.getSourceManager());
+
+ comments::CommandTraits Traits;
+ comments::Lexer L(Context.getAllocator(), Traits,
+ getSourceRange().getBegin(), comments::CommentOptions(),
+ RawText.begin(), RawText.end());
+ comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
+ Context.getDiagnostics(), Traits);
+ S.setDecl(getDecl());
+ comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
+ Context.getDiagnostics(), Traits);
+
+ comments::FullComment *FC = P.parseFullComment();
+ DeclOrParsedComment = FC;
+ return FC;
+}
+
namespace {
bool containsOnlyWhitespace(StringRef Str) {
return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;