[clang-tools-extra] 6f3f1e9 - [clangd] Remove trivial uses of FileEntry::getName

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 4 11:59:58 PDT 2022


Author: Sam McCall
Date: 2022-04-04T20:59:51+02:00
New Revision: 6f3f1e98686575004bef4257cbc6c825de5382af

URL: https://github.com/llvm/llvm-project/commit/6f3f1e98686575004bef4257cbc6c825de5382af
DIFF: https://github.com/llvm/llvm-project/commit/6f3f1e98686575004bef4257cbc6c825de5382af.diff

LOG: [clangd] Remove trivial uses of FileEntry::getName

It's deprecated; migrate to FileEntryRef::getName where it doesn't matter.
Also change one subtle case of implicit FileEntry::getName to be explicit.

After this patch, all the remaining FileEntry::getName calls are subtle
cases where we may be relying on exactly which filename variant is returned
(for indexing, IWYU directive handling, etc).

Added: 
    

Modified: 
    clang-tools-extra/clangd/Diagnostics.cpp
    clang-tools-extra/clangd/IncludeCleaner.cpp
    clang-tools-extra/clangd/Preamble.cpp
    clang-tools-extra/clangd/index/CanonicalIncludes.cpp
    clang-tools-extra/clangd/index/FileIndex.cpp
    clang-tools-extra/clangd/index/SymbolCollector.cpp
    clang-tools-extra/clangd/refactor/Tweak.cpp
    clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
    clang-tools-extra/clangd/unittests/IndexActionTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp
index 762906615f323..46a16a9f2eeb3 100644
--- a/clang-tools-extra/clangd/Diagnostics.cpp
+++ b/clang-tools-extra/clangd/Diagnostics.cpp
@@ -196,16 +196,16 @@ bool tryMoveToMainFile(Diag &D, FullSourceLoc DiagLoc) {
     return false;
 
   // Add a note that will point to real diagnostic.
-  const auto *FE = SM.getFileEntryForID(SM.getFileID(DiagLoc));
+  auto FE = SM.getFileEntryRefForID(SM.getFileID(DiagLoc)).getValue();
   D.Notes.emplace(D.Notes.begin());
   Note &N = D.Notes.front();
-  N.AbsFile = std::string(FE->tryGetRealPathName());
-  N.File = std::string(FE->getName());
+  N.AbsFile = std::string(FE.getFileEntry().tryGetRealPathName());
+  N.File = std::string(FE.getName());
   N.Message = "error occurred here";
   N.Range = D.Range;
 
   // Update diag to point at include inside main file.
-  D.File = SM.getFileEntryForID(SM.getMainFileID())->getName().str();
+  D.File = SM.getFileEntryRefForID(SM.getMainFileID())->getName().str();
   D.Range = std::move(R);
   D.InsideMainFile = true;
   // Update message to mention original file.

diff  --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp
index 04dbf12410cf7..44acd34086af9 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.cpp
+++ b/clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -246,14 +246,14 @@ static bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST) {
   // Headers without include guards have side effects and are not
   // self-contained, skip them.
   assert(Inc.HeaderID);
-  auto FE = AST.getSourceManager().getFileManager().getFile(
+  auto FE = AST.getSourceManager().getFileManager().getFileRef(
       AST.getIncludeStructure().getRealPath(
           static_cast<IncludeStructure::HeaderID>(*Inc.HeaderID)));
   assert(FE);
   if (!AST.getPreprocessor().getHeaderSearchInfo().isFileMultipleIncludeGuarded(
-          *FE)) {
+          &FE->getFileEntry())) {
     dlog("{0} doesn't have header guard and will not be considered unused",
-         (*FE)->getName());
+         FE->getName());
     return false;
   }
   return true;
@@ -418,7 +418,7 @@ std::vector<Diag> issueUnusedIncludesDiagnostics(ParsedAST &AST,
   std::vector<Diag> Result;
   std::string FileName =
       AST.getSourceManager()
-          .getFileEntryForID(AST.getSourceManager().getMainFileID())
+          .getFileEntryRefForID(AST.getSourceManager().getMainFileID())
           ->getName()
           .str();
   for (const auto *Inc : computeUnusedIncludes(AST)) {

diff  --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp
index aab6dc78092f7..b3a3aec985ba0 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -677,7 +677,7 @@ PreamblePatch PreamblePatch::unmodified(const PreambleData &Preamble) {
 SourceLocation translatePreamblePatchLocation(SourceLocation Loc,
                                               const SourceManager &SM) {
   auto DefFile = SM.getFileID(Loc);
-  if (auto *FE = SM.getFileEntryForID(DefFile)) {
+  if (auto FE = SM.getFileEntryRefForID(DefFile)) {
     auto IncludeLoc = SM.getIncludeLoc(DefFile);
     // Preamble patch is included inside the builtin file.
     if (IncludeLoc.isValid() && SM.isWrittenInBuiltinFile(IncludeLoc) &&

diff  --git a/clang-tools-extra/clangd/index/CanonicalIncludes.cpp b/clang-tools-extra/clangd/index/CanonicalIncludes.cpp
index 7067f1771b94f..ce02f97bb7d8c 100644
--- a/clang-tools-extra/clangd/index/CanonicalIncludes.cpp
+++ b/clang-tools-extra/clangd/index/CanonicalIncludes.cpp
@@ -66,11 +66,12 @@ collectIWYUHeaderMaps(CanonicalIncludes *Includes) {
                                PP.getSourceManager(), PP.getLangOpts());
       if (!Text.consume_front(IWYUPragma))
         return false;
-      // FIXME(ioeric): resolve the header and store actual file path. For now,
-      // we simply assume the written header is suitable to be #included.
-      Includes->addMapping(PP.getSourceManager().getFilename(Range.getBegin()),
-                           isLiteralInclude(Text) ? Text.str()
-                                                  : ("\"" + Text + "\"").str());
+      // We always insert using the spelling from the pragma.
+      if (auto *FE = PP.getSourceManager().getFileEntryForID(
+              PP.getSourceManager().getFileID(Range.getBegin())))
+        Includes->addMapping(FE->getName(), isLiteralInclude(Text)
+                                                ? Text.str()
+                                                : ("\"" + Text + "\"").str());
       return false;
     }
 

diff  --git a/clang-tools-extra/clangd/index/FileIndex.cpp b/clang-tools-extra/clangd/index/FileIndex.cpp
index d9b08db5e716b..72f7c0801250b 100644
--- a/clang-tools-extra/clangd/index/FileIndex.cpp
+++ b/clang-tools-extra/clangd/index/FileIndex.cpp
@@ -84,7 +84,7 @@ SlabTuple indexSymbols(ASTContext &AST, Preprocessor &PP,
     Collector.handleMacros(*MacroRefsToIndex);
 
   const auto &SM = AST.getSourceManager();
-  const auto *MainFileEntry = SM.getFileEntryForID(SM.getMainFileID());
+  const auto MainFileEntry = SM.getFileEntryRefForID(SM.getMainFileID());
   std::string FileName =
       std::string(MainFileEntry ? MainFileEntry->getName() : "");
 

diff  --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index ad970a5be85c0..e7923bae0c43c 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -561,7 +561,7 @@ bool SymbolCollector::handleDeclOccurrence(
   // it's main-file only.
   bool IsMainFileOnly =
       SM.isWrittenInMainFile(SM.getExpansionLoc(ND->getBeginLoc())) &&
-      !isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
+      !isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
                     ASTCtx->getLangOpts());
   // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
   if (ASTNode.OrigD->isImplicit() ||
@@ -688,7 +688,7 @@ bool SymbolCollector::handleMacroOccurrence(const IdentifierInfo *Name,
   auto SpellingLoc = SM.getSpellingLoc(Loc);
   bool IsMainFileOnly =
       SM.isInMainFile(SM.getExpansionLoc(DefLoc)) &&
-      !isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
+      !isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
                     ASTCtx->getLangOpts());
   // Do not store references to main-file macros.
   if ((static_cast<unsigned>(Opts.RefFilter) & Roles) && !IsMainFileOnly &&

diff  --git a/clang-tools-extra/clangd/refactor/Tweak.cpp b/clang-tools-extra/clangd/refactor/Tweak.cpp
index bd3fbc602e859..34f2e7960affe 100644
--- a/clang-tools-extra/clangd/refactor/Tweak.cpp
+++ b/clang-tools-extra/clangd/refactor/Tweak.cpp
@@ -108,7 +108,7 @@ Tweak::Effect::fileEdit(const SourceManager &SM, FileID FID,
   if (auto FilePath = getCanonicalPath(SM.getFileEntryForID(FID), SM))
     return std::make_pair(*FilePath, std::move(Ed));
   return error("Failed to get absolute path for edited file: {0}",
-               SM.getFileEntryForID(FID)->getName());
+               SM.getFileEntryRefForID(FID)->getName());
 }
 
 llvm::Expected<Tweak::Effect>

diff  --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
index 5a4e1aa571a74..103e13f44d060 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -237,7 +237,7 @@ bool AddUsing::prepare(const Selection &Inputs) {
   const auto &TB = Inputs.AST->getTokens();
 
   // Do not suggest "using" in header files. That way madness lies.
-  if (isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
+  if (isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
                    Inputs.AST->getLangOpts()))
     return false;
 

diff  --git a/clang-tools-extra/clangd/unittests/IndexActionTests.cpp b/clang-tools-extra/clangd/unittests/IndexActionTests.cpp
index 29300b561ed0e..0fdd0e26812a7 100644
--- a/clang-tools-extra/clangd/unittests/IndexActionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/IndexActionTests.cpp
@@ -271,7 +271,7 @@ TEST_F(IndexActionTest, SkipFiles) {
     auto unskippable2() { return S(); }
   )cpp");
   Opts.FileFilter = [](const SourceManager &SM, FileID F) {
-    return !SM.getFileEntryForID(F)->getName().endswith("bad.h");
+    return !SM.getFileEntryRefForID(F)->getName().endswith("bad.h");
   };
   IndexFileIn IndexFile = runIndexingAction(MainFilePath, {"-std=c++14"});
   EXPECT_THAT(*IndexFile.Symbols,


        


More information about the cfe-commits mailing list