From 40388bdfb96f6cae8c1dcd5ca341c9e3dd2c8d31 Mon Sep 17 00:00:00 2001 From: "David L. Jones" Date: Thu, 1 Mar 2018 22:41:53 +0000 Subject: [PATCH] [NFC] Move CommentOpts checks to the call sites that depend on it. When parsing comments, for example, for -Wdocumentation, slightly different behaviour occurs when -fparse-all-comments is specified. However, these differences are subtle: 1. All comments are saved during parsing, regardless of whether they are doc comments or not. 2. "Maybe-doc" comments, like //<, //!, etc, are saved as such, instead of marking them as ordinary comments. The maybe-doc type of comment is never saved otherwise. (Warning on these is the impetus of -Wdocumentation.) 3. All comments are treated as doc comments in ASTContext, even if they are ordinary. This change moves the logic for checking CommentOptions.ParseAllComments closer to where it has an effect. The overall logic is unchanged, but checks of the ParseAllComments flag are now done where the effect will be clearer. Reviewers: rsmith Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D43663 llvm-svn: 326501 --- clang/include/clang/AST/ASTContext.h | 2 +- clang/include/clang/AST/RawCommentList.h | 23 ++++++----------------- clang/lib/AST/ASTContext.cpp | 12 +++++++----- clang/lib/AST/RawCommentList.cpp | 17 +++++++++-------- clang/lib/Sema/Sema.cpp | 3 +-- clang/lib/Serialization/ASTReader.cpp | 3 +-- 6 files changed, 25 insertions(+), 35 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index b002914..a5b090d 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -784,7 +784,7 @@ public: void addComment(const RawComment &RC) { assert(LangOpts.RetainCommentsFromSystemHeaders || !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin())); - Comments.addComment(RC, BumpAlloc); + Comments.addComment(RC, LangOpts.CommentOpts, BumpAlloc); } /// \brief Return the documentation comment attached to a given declaration. diff --git a/clang/include/clang/AST/RawCommentList.h b/clang/include/clang/AST/RawCommentList.h index 2e005dd..f4bd1f3 100644 --- a/clang/include/clang/AST/RawCommentList.h +++ b/clang/include/clang/AST/RawCommentList.h @@ -41,7 +41,7 @@ public: RawComment() : Kind(RCK_Invalid), IsAlmostTrailingComment(false) { } RawComment(const SourceManager &SourceMgr, SourceRange SR, - bool Merged, bool ParseAllComments); + const CommentOptions &CommentOpts, bool Merged); CommentKind getKind() const LLVM_READONLY { return (CommentKind) Kind; @@ -83,8 +83,7 @@ public: /// Returns true if this comment is not a documentation comment. bool isOrdinary() const LLVM_READONLY { - return ((Kind == RCK_OrdinaryBCPL) || (Kind == RCK_OrdinaryC)) && - !ParseAllComments; + return ((Kind == RCK_OrdinaryBCPL) || (Kind == RCK_OrdinaryC)); } /// Returns true if this comment any kind of a documentation comment. @@ -92,11 +91,6 @@ public: return !isInvalid() && !isOrdinary(); } - /// Returns whether we are parsing all comments. - bool isParseAllComments() const LLVM_READONLY { - return ParseAllComments; - } - /// Returns raw comment text with comment markers. StringRef getRawText(const SourceManager &SourceMgr) const { if (RawTextValid) @@ -139,18 +133,12 @@ private: bool IsTrailingComment : 1; bool IsAlmostTrailingComment : 1; - /// When true, ordinary comments starting with "//" and "/*" will be - /// considered as documentation comments. - bool ParseAllComments : 1; - /// \brief Constructor for AST deserialization. RawComment(SourceRange SR, CommentKind K, bool IsTrailingComment, - bool IsAlmostTrailingComment, - bool ParseAllComments) : + bool IsAlmostTrailingComment) : Range(SR), RawTextValid(false), BriefTextValid(false), Kind(K), IsAttached(false), IsTrailingComment(IsTrailingComment), - IsAlmostTrailingComment(IsAlmostTrailingComment), - ParseAllComments(ParseAllComments) + IsAlmostTrailingComment(IsAlmostTrailingComment) { } StringRef getRawTextSlow(const SourceManager &SourceMgr) const; @@ -183,7 +171,8 @@ class RawCommentList { public: RawCommentList(SourceManager &SourceMgr) : SourceMgr(SourceMgr) {} - void addComment(const RawComment &RC, llvm::BumpPtrAllocator &Allocator); + void addComment(const RawComment &RC, const CommentOptions &CommentOpts, + llvm::BumpPtrAllocator &Allocator); ArrayRef getComments() const { return Comments; diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index e93e2ac..5039be3 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -226,8 +226,7 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { // for is usually among the last two comments we parsed -- check them // first. RawComment CommentAtDeclLoc( - SourceMgr, SourceRange(DeclLoc), false, - LangOpts.CommentOpts.ParseAllComments); + SourceMgr, SourceRange(DeclLoc), LangOpts.CommentOpts, false); BeforeThanCompare Compare(SourceMgr); ArrayRef::iterator MaybeBeforeDecl = RawComments.end() - 1; bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); @@ -253,7 +252,8 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { // First check whether we have a trailing comment. if (Comment != RawComments.end() && - (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() && + ((*Comment)->isDocumentation() || LangOpts.CommentOpts.ParseAllComments) + && (*Comment)->isTrailingComment() && (isa(D) || isa(D) || isa(D) || isa(D) || isa(D))) { std::pair CommentBeginDecomp @@ -275,7 +275,9 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { --Comment; // Check that we actually have a non-member Doxygen comment. - if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment()) + if (!((*Comment)->isDocumentation() || + LangOpts.CommentOpts.ParseAllComments) || + (*Comment)->isTrailingComment()) return nullptr; // Decompose the end of the comment. @@ -428,7 +430,7 @@ const RawComment *ASTContext::getRawCommentForAnyRedecl( } // If we found a comment, it should be a documentation comment. - assert(!RC || RC->isDocumentation()); + assert(!RC || RC->isDocumentation() || LangOpts.CommentOpts.ParseAllComments); if (OriginalDecl) *OriginalDecl = OriginalDeclForRC; diff --git a/clang/lib/AST/RawCommentList.cpp b/clang/lib/AST/RawCommentList.cpp index 881a7d9..73a4d9d 100644 --- a/clang/lib/AST/RawCommentList.cpp +++ b/clang/lib/AST/RawCommentList.cpp @@ -107,10 +107,10 @@ static bool isOrdinaryKind(RawComment::CommentKind K) { } RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR, - bool Merged, bool ParseAllComments) : + const CommentOptions &CommentOpts, bool Merged) : Range(SR), RawTextValid(false), BriefTextValid(false), - IsAttached(false), IsTrailingComment(false), IsAlmostTrailingComment(false), - ParseAllComments(ParseAllComments) { + IsAttached(false), IsTrailingComment(false), + IsAlmostTrailingComment(false) { // Extract raw comment text, if possible. if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) { Kind = RCK_Invalid; @@ -118,10 +118,11 @@ RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR, } // Guess comment kind. - std::pair K = getCommentKind(RawText, ParseAllComments); + std::pair K = + getCommentKind(RawText, CommentOpts.ParseAllComments); // Guess whether an ordinary comment is trailing. - if (ParseAllComments && isOrdinaryKind(K.first)) { + if (CommentOpts.ParseAllComments && isOrdinaryKind(K.first)) { FileID BeginFileID; unsigned BeginOffset; std::tie(BeginFileID, BeginOffset) = @@ -270,6 +271,7 @@ static bool onlyWhitespaceBetween(SourceManager &SM, } void RawCommentList::addComment(const RawComment &RC, + const CommentOptions &CommentOpts, llvm::BumpPtrAllocator &Allocator) { if (RC.isInvalid()) return; @@ -284,7 +286,7 @@ void RawCommentList::addComment(const RawComment &RC, } // Ordinary comments are not interesting for us. - if (RC.isOrdinary()) + if (RC.isOrdinary() && !CommentOpts.ParseAllComments) return; // If this is the first Doxygen comment, save it (because there isn't @@ -317,8 +319,7 @@ void RawCommentList::addComment(const RawComment &RC, onlyWhitespaceBetween(SourceMgr, C1.getLocEnd(), C2.getLocStart(), /*MaxNewlinesAllowed=*/1)) { SourceRange MergedRange(C1.getLocStart(), C2.getLocEnd()); - *Comments.back() = RawComment(SourceMgr, MergedRange, true, - RC.isParseAllComments()); + *Comments.back() = RawComment(SourceMgr, MergedRange, CommentOpts, true); } else { Comments.push_back(new (Allocator) RawComment(RC)); } diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 65183b4..1e9e53b 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -1463,8 +1463,7 @@ void Sema::ActOnComment(SourceRange Comment) { if (!LangOpts.RetainCommentsFromSystemHeaders && SourceMgr.isInSystemHeader(Comment.getBegin())) return; - RawComment RC(SourceMgr, Comment, false, - LangOpts.CommentOpts.ParseAllComments); + RawComment RC(SourceMgr, Comment, LangOpts.CommentOpts, false); if (RC.isAlmostTrailingComment()) { SourceRange MagicMarkerRange(Comment.getBegin(), Comment.getBegin().getLocWithOffset(3)); diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 19519b9..b6fc4b2 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -9068,8 +9068,7 @@ void ASTReader::ReadComments() { bool IsTrailingComment = Record[Idx++]; bool IsAlmostTrailingComment = Record[Idx++]; Comments.push_back(new (Context) RawComment( - SR, Kind, IsTrailingComment, IsAlmostTrailingComment, - Context.getLangOpts().CommentOpts.ParseAllComments)); + SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); break; } } -- 2.7.4