diff --git a/clang-tools-extra/clangd/ClangdUnit.cpp b/clang-tools-extra/clangd/ClangdUnit.cpp --- a/clang-tools-extra/clangd/ClangdUnit.cpp +++ b/clang-tools-extra/clangd/ClangdUnit.cpp @@ -68,7 +68,7 @@ bool HandleTopLevelDecl(DeclGroupRef DG) override { for (Decl *D : DG) { auto &SM = D->getASTContext().getSourceManager(); - if (!SM.isWrittenInMainFile(SM.getExpansionLoc(D->getLocation()))) + if (!isInsideMainFile(D->getLocation(), SM)) continue; // ObjCMethodDecl are not actually top-level decls. @@ -355,8 +355,7 @@ // those might take us into a preamble file as well. bool IsInsideMainFile = Info.hasSourceManager() && - Info.getSourceManager().isWrittenInMainFile( - Info.getSourceManager().getFileLoc(Info.getLocation())); + isInsideMainFile(Info.getLocation(), Info.getSourceManager()); if (IsInsideMainFile && tidy::ShouldSuppressDiagnostic( DiagLevel, Info, *CTContext, /* CheckMacroExpansion = */ false)) { diff --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp --- a/clang-tools-extra/clangd/Diagnostics.cpp +++ b/clang-tools-extra/clangd/Diagnostics.cpp @@ -140,15 +140,11 @@ D.Message = llvm::Twine("in included file: ", D.Message).str(); } -bool isInsideMainFile(const SourceLocation Loc, const SourceManager &M) { - return Loc.isValid() && M.isWrittenInMainFile(M.getFileLoc(Loc)); -} - bool isInsideMainFile(const clang::Diagnostic &D) { if (!D.hasSourceManager()) return false; - return isInsideMainFile(D.getLocation(), D.getSourceManager()); + return clangd::isInsideMainFile(D.getLocation(), D.getSourceManager()); } bool isNote(DiagnosticsEngine::Level L) { diff --git a/clang-tools-extra/clangd/Headers.cpp b/clang-tools-extra/clangd/Headers.cpp --- a/clang-tools-extra/clangd/Headers.cpp +++ b/clang-tools-extra/clangd/Headers.cpp @@ -35,7 +35,7 @@ llvm::StringRef /*RelativePath*/, const Module * /*Imported*/, SrcMgr::CharacteristicKind FileKind) override { - if (SM.isWrittenInMainFile(HashLoc)) { + if (isInsideMainFile(HashLoc, SM)) { Out->MainFileIncludes.emplace_back(); auto &Inc = Out->MainFileIncludes.back(); Inc.R = halfOpenToRange(SM, FilenameRange); diff --git a/clang-tools-extra/clangd/IncludeFixer.cpp b/clang-tools-extra/clangd/IncludeFixer.cpp --- a/clang-tools-extra/clangd/IncludeFixer.cpp +++ b/clang-tools-extra/clangd/IncludeFixer.cpp @@ -318,7 +318,7 @@ assert(SemaPtr && "Sema must have been set."); if (SemaPtr->isSFINAEContext()) return TypoCorrection(); - if (!SemaPtr->SourceMgr.isWrittenInMainFile(Typo.getLoc())) + if (!isInsideMainFile(Typo.getLoc(), SemaPtr->SourceMgr)) return clang::TypoCorrection(); // This is not done lazily because `SS` can get out of scope and it's diff --git a/clang-tools-extra/clangd/Quality.cpp b/clang-tools-extra/clangd/Quality.cpp --- a/clang-tools-extra/clangd/Quality.cpp +++ b/clang-tools-extra/clangd/Quality.cpp @@ -9,6 +9,7 @@ #include "Quality.h" #include "AST.h" #include "FileDistance.h" +#include "SourceCode.h" #include "URI.h" #include "index/Symbol.h" #include "clang/AST/ASTContext.h" @@ -42,8 +43,7 @@ static bool hasDeclInMainFile(const Decl &D) { auto &SourceMgr = D.getASTContext().getSourceManager(); for (auto *Redecl : D.redecls()) { - auto Loc = SourceMgr.getSpellingLoc(Redecl->getLocation()); - if (SourceMgr.isWrittenInMainFile(Loc)) + if (isInsideMainFile(Redecl->getLocation(), SourceMgr)) return true; } return false; @@ -53,8 +53,7 @@ const auto &Context = R.Declaration->getASTContext(); const auto &SourceMgr = Context.getSourceManager(); if (R.ShadowDecl) { - const auto Loc = SourceMgr.getExpansionLoc(R.ShadowDecl->getLocation()); - if (SourceMgr.isWrittenInMainFile(Loc)) + if (isInsideMainFile(R.ShadowDecl->getLocation(), SourceMgr)) return true; } return false; diff --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h --- a/clang-tools-extra/clangd/SourceCode.h +++ b/clang-tools-extra/clangd/SourceCode.h @@ -75,6 +75,16 @@ llvm::Expected sourceLocationInMainFile(const SourceManager &SM, Position P); +/// Returns true iff \p Loc is inside the main file. This function handles +/// file¯o locations. +/// +/// if the Loc is a macro location, the function calls getFileLoc to get the +/// file location before checking. +/// +/// The function is usually used to check whether a declaration is inside the +/// the main file. +bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM); + /// Turns a token range into a half-open range and checks its correctness. /// The resulting range will have only valid source location on both sides, both /// of which are file locations. diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -328,6 +328,10 @@ return FileRange; } +bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM) { + return Loc.isValid() && SM.isWrittenInMainFile(SM.getFileLoc(Loc)); +} + llvm::Optional toHalfOpenFileRange(const SourceManager &SM, const LangOptions &LangOpts, SourceRange R) { diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -406,7 +406,7 @@ assert(D->isCanonicalDecl() && "expect D to be a canonical declaration"); const SourceManager &SM = AST.getSourceManager(); Loc = SM.getFileLoc(Loc); - if (SM.isWrittenInMainFile(Loc) && CanonicalTargets.count(D)) + if (isInsideMainFile(Loc, SM) && CanonicalTargets.count(D)) References.push_back({D, Loc, Roles}); return true; } diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -185,8 +185,7 @@ bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) { const auto &SM = ND.getASTContext().getSourceManager(); return (Roles & static_cast(index::SymbolRole::Definition)) && - isa(&ND) && - !SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getLocation())); + isa(&ND) && !isInsideMainFile(ND.getLocation(), SM); } RefKind toRefKind(index::SymbolRoleSet Roles) { diff --git a/clang-tools-extra/clangd/refactor/Rename.cpp b/clang-tools-extra/clangd/refactor/Rename.cpp --- a/clang-tools-extra/clangd/refactor/Rename.cpp +++ b/clang-tools-extra/clangd/refactor/Rename.cpp @@ -104,8 +104,7 @@ auto &ASTCtx = RenameDecl.getASTContext(); const auto &SM = ASTCtx.getSourceManager(); bool MainFileIsHeader = ASTCtx.getLangOpts().IsHeaderFile; - bool DeclaredInMainFile = - SM.isWrittenInMainFile(SM.getExpansionLoc(RenameDecl.getLocation())); + bool DeclaredInMainFile = isInsideMainFile(RenameDecl.getBeginLoc(), SM); // If the symbol is declared in the main file (which is not a header), we // rename it. diff --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp --- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp +++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp @@ -124,8 +124,7 @@ const NamedDecl &ND = Qualified ? findDecl(*AST, Name) : findUnqualifiedDecl(*AST, Name); const SourceManager &SM = AST->getSourceManager(); - bool MainFile = - SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getBeginLoc())); + bool MainFile = isInsideMainFile(ND.getBeginLoc(), SM); return SymbolCollector::shouldCollectSymbol( ND, AST->getASTContext(), SymbolCollector::Options(), MainFile); }