Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -99,7 +99,7 @@ }; static llvm::Optional -getCandidateCommentLocation(SourceManager &SrcMgr, const Decl *D) { +getDeclLocationForCommentSearch(SourceManager &SrcMgr, const Decl *D) { // Find declaration location. // For Objective-C declarations we generally don't expect to have multiple // declarators, thus use declaration starting location as the "declaration @@ -140,20 +140,20 @@ return DeclLoc; }; -// Assumes the comment is the closest comment before the decl - i. e. there's no -// other comment between them. -static bool IsCommentBeforeDecl(SourceManager &SrcMgr, - std::pair CommentEndLoc, - std::pair DeclLoc) { +// Returns true if the comment which ends at CommentEndLoc is attached to the +// declaration at DeclLoc. The comment must be the closest one before the +// declaration (no other comments in between). +static bool IsCommentAttachedToDecl(SourceManager &SrcMgr, + std::pair CommentEndLoc, + std::pair DeclLoc) { + // We're looking for comments that are BEFORE this declaration + assert(CommentEndLoc.second < DeclLoc.second); + // If the comment and the declaration aren't in the same file, then they // aren't related. if (CommentEndLoc.first != DeclLoc.first) return false; - // We're looking for comments that are BEFORE this declaration - if (CommentEndLoc.second > DeclLoc.second) - return false; - // Get the corresponding buffer. bool Invalid = false; const char *Buffer = SrcMgr.getBufferData(DeclLoc.first, &Invalid).data(); @@ -172,7 +172,7 @@ return false; return true; -}; +} /// Returns true iff \param D can have a documentation comment attached. static bool CanDeclHaveDocComment(const Decl *D) { @@ -242,13 +242,13 @@ if (!CanDeclHaveDocComment(D)) return nullptr; - const llvm::Optional OptCandidateCommentLocation = - getCandidateCommentLocation(SourceMgr, D); - if (!OptCandidateCommentLocation) + const llvm::Optional OptDeclLocForCommentSearch = + getDeclLocationForCommentSearch(SourceMgr, D); + if (!OptDeclLocForCommentSearch) return nullptr; - const SourceLocation &CandidateCommentLocation = - OptCandidateCommentLocation.getValue(); + const SourceLocation &DeclLocForCommentSearch = + OptDeclLocForCommentSearch.getValue(); if (!CommentsLoaded && ExternalSource) { ExternalSource->ReadComments(); @@ -274,8 +274,7 @@ // When searching for comments during parsing, the comment we are looking // for is usually among the last two comments we parsed -- check them // first. - RawComment CommentAtDeclLoc(SourceMgr, - SourceRange(CandidateCommentLocation), + RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLocForCommentSearch), LangOpts.CommentOpts, false); BeforeThanCompare Compare(SourceMgr); ArrayRef::iterator MaybeBeforeDecl = RawComments.end() - 1; @@ -306,7 +305,7 @@ (isa(D) || isa(D) || isa(D) || isa(D) || isa(D))) { - OptDeclLocDecomp = SourceMgr.getDecomposedLoc(CandidateCommentLocation); + OptDeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLocForCommentSearch); const std::pair &DeclLocDecomp = OptDeclLocDecomp.getValue(); @@ -335,14 +334,14 @@ return nullptr; if (!OptDeclLocDecomp) - OptDeclLocDecomp = SourceMgr.getDecomposedLoc(CandidateCommentLocation); + OptDeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLocForCommentSearch); const std::pair &DeclLocDecomp = OptDeclLocDecomp.getValue(); std::pair CommentEndDecomp = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); - if (!IsCommentBeforeDecl(SourceMgr, CommentEndDecomp, DeclLocDecomp)) + if (!IsCommentAttachedToDecl(SourceMgr, CommentEndDecomp, DeclLocDecomp)) return nullptr; return *Comment;