Index: clang/include/clang/AST/ASTContext.h =================================================================== --- clang/include/clang/AST/ASTContext.h +++ clang/include/clang/AST/ASTContext.h @@ -792,7 +792,8 @@ /// Return the documentation comment attached to a given declaration, /// without looking into cache. - RawComment *getRawCommentForDeclNoCache(const Decl *D) const; + RawComment *getRawCommentForDeclNoCache(const Decl *D, + bool LoadExternal) const; public: RawCommentList &getRawCommentList() { @@ -810,9 +811,9 @@ /// /// \param OriginalDecl if not nullptr, is set to declaration AST node that /// had the comment, if the comment we found comes from a redeclaration. - const RawComment * - getRawCommentForAnyRedecl(const Decl *D, - const Decl **OriginalDecl = nullptr) const; + const RawComment *getRawCommentForAnyRedecl(const Decl *D, + const Decl **OriginalDecl, + bool LoadExternal) const; /// Return parsed documentation comment attached to a given declaration. /// Returns nullptr if no comment is attached. @@ -820,7 +821,8 @@ /// \param PP the Preprocessor used with this TU. Could be nullptr if /// preprocessor is not available. comments::FullComment *getCommentForDecl(const Decl *D, - const Preprocessor *PP) const; + const Preprocessor *PP, + bool LoadExternal) const; /// Return parsed documentation comment attached to a given declaration. /// Returns nullptr if no comment is attached. Does not look at any Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -98,7 +98,8 @@ Float16Rank, HalfRank, FloatRank, DoubleRank, LongDoubleRank, Float128Rank }; -RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { +RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D, + bool LoadExternal) const { assert(D); // If we already tried to load comments but there are none, @@ -193,7 +194,7 @@ if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) return nullptr; - if (!CommentsLoaded && ExternalSource) { + if (LoadExternal && !CommentsLoaded && ExternalSource) { ExternalSource->ReadComments(); #ifndef NDEBUG @@ -369,9 +370,9 @@ return D; } -const RawComment *ASTContext::getRawCommentForAnyRedecl( - const Decl *D, - const Decl **OriginalDecl) const { +const RawComment * +ASTContext::getRawCommentForAnyRedecl(const Decl *D, const Decl **OriginalDecl, + bool LoadExternal) const { D = adjustDeclToTemplate(D); // Check whether we have cached a comment for this declaration already. @@ -402,7 +403,7 @@ break; } } else { - RC = getRawCommentForDeclNoCache(I); + RC = getRawCommentForDeclNoCache(I, LoadExternal); OriginalDeclForRC = I; RawCommentAndCacheFlags Raw; if (RC) { @@ -473,13 +474,13 @@ } comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { - const RawComment *RC = getRawCommentForDeclNoCache(D); + const RawComment *RC = getRawCommentForDeclNoCache(D, /*LoadExternal=*/true); return RC ? RC->parse(*this, nullptr, D) : nullptr; } -comments::FullComment *ASTContext::getCommentForDecl( - const Decl *D, - const Preprocessor *PP) const { +comments::FullComment *ASTContext::getCommentForDecl(const Decl *D, + const Preprocessor *PP, + bool LoadExternal) const { if (D->isInvalidDecl()) return nullptr; D = adjustDeclToTemplate(D); @@ -499,20 +500,23 @@ const Decl *OriginalDecl; - const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); + const RawComment *RC = + getRawCommentForAnyRedecl(D, &OriginalDecl, LoadExternal); if (!RC) { if (isa(D) || isa(D)) { SmallVector Overridden; const auto *OMD = dyn_cast(D); if (OMD && OMD->isPropertyAccessor()) if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) - if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) + if (comments::FullComment *FC = + getCommentForDecl(PDecl, PP, LoadExternal)) return cloneFullComment(FC, D); if (OMD) addRedeclaredMethods(OMD, Overridden); getOverriddenMethods(dyn_cast(D), Overridden); for (unsigned i = 0, e = Overridden.size(); i < e; i++) - if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) + if (comments::FullComment *FC = + getCommentForDecl(Overridden[i], PP, LoadExternal)) return cloneFullComment(FC, D); } else if (const auto *TD = dyn_cast(D)) { @@ -521,19 +525,20 @@ QualType QT = TD->getUnderlyingType(); if (const auto *TT = QT->getAs()) if (const Decl *TD = TT->getDecl()) - if (comments::FullComment *FC = getCommentForDecl(TD, PP)) + if (comments::FullComment *FC = + getCommentForDecl(TD, PP, LoadExternal)) return cloneFullComment(FC, D); } else if (const auto *IC = dyn_cast(D)) { while (IC->getSuperClass()) { IC = IC->getSuperClass(); - if (comments::FullComment *FC = getCommentForDecl(IC, PP)) + if (comments::FullComment *FC = getCommentForDecl(IC, PP, LoadExternal)) return cloneFullComment(FC, D); } } else if (const auto *CD = dyn_cast(D)) { if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) - if (comments::FullComment *FC = getCommentForDecl(IC, PP)) + if (comments::FullComment *FC = getCommentForDecl(IC, PP, LoadExternal)) return cloneFullComment(FC, D); } else if (const auto *RD = dyn_cast(D)) { @@ -550,7 +555,8 @@ if (!(NonVirtualBase= NonVirtualBase->getDefinition())) continue; - if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) + if (comments::FullComment *FC = + getCommentForDecl((NonVirtualBase), PP, LoadExternal)) return cloneFullComment(FC, D); } } @@ -564,7 +570,8 @@ if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { if (!(VirtualBase= VirtualBase->getDefinition())) continue; - if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) + if (comments::FullComment *FC = + getCommentForDecl((VirtualBase), PP, LoadExternal)) return cloneFullComment(FC, D); } } @@ -577,7 +584,7 @@ // because comments can contain references to parameter names which can be // different across redeclarations. if (D != OriginalDecl) - return getCommentForDecl(OriginalDecl, PP); + return getCommentForDecl(OriginalDecl, PP, LoadExternal); comments::FullComment *FC = RC->parse(*this, PP, D); ParsedComments[Canonical] = FC; Index: clang/lib/Sema/SemaCodeComplete.cpp =================================================================== --- clang/lib/Sema/SemaCodeComplete.cpp +++ clang/lib/Sema/SemaCodeComplete.cpp @@ -3373,7 +3373,8 @@ const NamedDecl *ND) { if (!ND) return nullptr; - if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND)) + if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND, /*OriginalDecl=*/nullptr, + /*LoadExternal=*/true)) return RC; // Try to find comment from a property for ObjC methods. @@ -3384,7 +3385,8 @@ if (!PDecl) return nullptr; - return Ctx.getRawCommentForAnyRedecl(PDecl); + return Ctx.getRawCommentForAnyRedecl(PDecl, /*OriginalDecl=*/nullptr, + /*LoadExternal=*/true); } const RawComment *clang::getPatternCompletionComment(const ASTContext &Ctx, @@ -3402,9 +3404,11 @@ return nullptr; if (PDecl->getGetterName() == M->getSelector() && PDecl->getIdentifier() != M->getIdentifier()) { - if (auto *RC = Ctx.getRawCommentForAnyRedecl(M)) + if (auto *RC = Ctx.getRawCommentForAnyRedecl(M, /*OriginalDecl=*/nullptr, + /*LoadExternal=*/true)) return RC; - if (auto *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) + if (auto *RC = Ctx.getRawCommentForAnyRedecl( + PDecl, /*OriginalDecl=*/nullptr, /*LoadExternal=*/true)) return RC; } return nullptr; @@ -3417,7 +3421,9 @@ if (!FDecl) return nullptr; if (ArgIndex < FDecl->getNumParams()) - return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex)); + return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex), + /*OriginalDecl=*/nullptr, + /*LoadExternal=*/true); return nullptr; } Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -12395,7 +12395,7 @@ // the lookahead in the lexer: we've consumed the semicolon and looked // ahead through comments. for (unsigned i = 0, e = Group.size(); i != e; ++i) - Context.getCommentForDecl(Group[i], &PP); + Context.getCommentForDecl(Group[i], &PP, /*LoadExternal=*/false); } } Index: clang/tools/libclang/CIndex.cpp =================================================================== --- clang/tools/libclang/CIndex.cpp +++ clang/tools/libclang/CIndex.cpp @@ -8172,7 +8172,8 @@ const Decl *D = getCursorDecl(C); ASTContext &Context = getCursorContext(C); - const RawComment *RC = Context.getRawCommentForAnyRedecl(D); + const RawComment *RC = Context.getRawCommentForAnyRedecl( + D, /*OriginalDecl=*/nullptr, /*LoadExternal=*/true); if (!RC) return clang_getNullRange(); @@ -8185,7 +8186,8 @@ const Decl *D = getCursorDecl(C); ASTContext &Context = getCursorContext(C); - const RawComment *RC = Context.getRawCommentForAnyRedecl(D); + const RawComment *RC = Context.getRawCommentForAnyRedecl( + D, /*OriginalDecl=*/nullptr, /*LoadExternal=*/true); StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) : StringRef(); @@ -8200,7 +8202,8 @@ const Decl *D = getCursorDecl(C); const ASTContext &Context = getCursorContext(C); - const RawComment *RC = Context.getRawCommentForAnyRedecl(D); + const RawComment *RC = Context.getRawCommentForAnyRedecl( + D, /*OriginalDecl=*/nullptr, /*LoadExternal=*/true); if (RC) { StringRef BriefText = RC->getBriefText(Context); Index: clang/tools/libclang/CXComment.cpp =================================================================== --- clang/tools/libclang/CXComment.cpp +++ clang/tools/libclang/CXComment.cpp @@ -33,7 +33,8 @@ const Decl *D = getCursorDecl(C); const ASTContext &Context = getCursorContext(C); - const FullComment *FC = Context.getCommentForDecl(D, /*PP=*/nullptr); + const FullComment *FC = + Context.getCommentForDecl(D, /*PP=*/nullptr, /*LoadExternal=*/true); return createCXComment(FC, getCursorTU(C)); }