[clang-tools-extra] 797cad9 - [clang] NFCI: Use `FileEntryRef` in 'clang-tools-extra'

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 10 19:54:02 PDT 2023


Author: Jan Svoboda
Date: 2023-09-10T19:53:54-07:00
New Revision: 797cad9d32885ca7a0ccfa657eba5c739f189e98

URL: https://github.com/llvm/llvm-project/commit/797cad9d32885ca7a0ccfa657eba5c739f189e98
DIFF: https://github.com/llvm/llvm-project/commit/797cad9d32885ca7a0ccfa657eba5c739f189e98.diff

LOG: [clang] NFCI: Use `FileEntryRef` in 'clang-tools-extra'

Added: 
    

Modified: 
    clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
    clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
    clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
    clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp
    clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp
    clang-tools-extra/clang-move/Move.cpp
    clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
    clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
    clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp
    clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
    clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
    clang-tools-extra/modularize/Modularize.cpp
    clang-tools-extra/modularize/PreprocessorTracker.cpp
    clang-tools-extra/pp-trace/PPCallbacksTracker.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h b/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
index b0cf7317c0ed899..1f0d87371914023 100644
--- a/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
+++ b/clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h
@@ -40,9 +40,8 @@ typedef std::vector<std::string> TUReplacementFiles;
 typedef std::vector<clang::tooling::TranslationUnitDiagnostics> TUDiagnostics;
 
 /// Map mapping file name to a set of AtomicChange targeting that file.
-typedef llvm::DenseMap<const clang::FileEntry *,
-                       std::vector<tooling::AtomicChange>>
-    FileToChangesMap;
+using FileToChangesMap =
+    llvm::DenseMap<clang::FileEntryRef, std::vector<tooling::AtomicChange>>;
 
 /// Recursively descends through a directory structure rooted at \p
 /// Directory and attempts to deserialize *.yaml files as

diff  --git a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
index a0a510c20131daa..b490780f48529e0 100644
--- a/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
+++ b/clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp
@@ -138,11 +138,11 @@ std::error_code collectReplacementsFromDirectory(
 ///
 /// \returns A map mapping FileEntry to a set of Replacement targeting that
 /// file.
-static llvm::DenseMap<const FileEntry *, std::vector<tooling::Replacement>>
+static llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
 groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
                   const clang::SourceManager &SM) {
   llvm::StringSet<> Warned;
-  llvm::DenseMap<const FileEntry *, std::vector<tooling::Replacement>>
+  llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
       GroupedReplacements;
 
   // Deduplicate identical replacements in diagnostics unless they are from the
@@ -165,7 +165,7 @@ groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
     else
       SM.getFileManager().makeAbsolutePath(Path);
 
-    if (auto Entry = SM.getFileManager().getFile(Path)) {
+    if (auto Entry = SM.getFileManager().getOptionalFileRef(Path)) {
       if (SourceTU) {
         auto &Replaces = DiagReplacements[*Entry];
         auto It = Replaces.find(R);
@@ -212,10 +212,10 @@ bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs,
   // To report conflicting replacements on corresponding file, all replacements
   // are stored into 1 big AtomicChange.
   for (const auto &FileAndReplacements : GroupedReplacements) {
-    const FileEntry *Entry = FileAndReplacements.first;
+    FileEntryRef Entry = FileAndReplacements.first;
     const SourceLocation BeginLoc =
         SM.getLocForStartOfFile(SM.getOrCreateFileID(Entry, SrcMgr::C_User));
-    tooling::AtomicChange FileChange(Entry->getName(), Entry->getName());
+    tooling::AtomicChange FileChange(Entry.getName(), Entry.getName());
     for (const auto &R : FileAndReplacements.second) {
       llvm::Error Err =
           FileChange.replace(SM, BeginLoc.getLocWithOffset(R.getOffset()),

diff  --git a/clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp b/clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
index 415ae47ce6d7dc5..9331898bf2570e6 100644
--- a/clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
+++ b/clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
@@ -146,8 +146,8 @@ int main(int argc, char **argv) {
                          : tooling::ApplyChangesSpec::kNone;
 
   for (const auto &FileChange : Changes) {
-    const FileEntry *Entry = FileChange.first;
-    StringRef FileName = Entry->getName();
+    FileEntryRef Entry = FileChange.first;
+    StringRef FileName = Entry.getName();
     llvm::Expected<std::string> NewFileData =
         applyChanges(FileName, FileChange.second, Spec, Diagnostics);
     if (!NewFileData) {

diff  --git a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp
index f07e9c95d1077f2..f38b6889d32e780 100644
--- a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp
+++ b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp
@@ -62,8 +62,8 @@ void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
 }
 
 void FindAllMacros::EndOfMainFile() {
-  Reporter->reportSymbols(SM->getFileEntryForID(SM->getMainFileID())->getName(),
-                          FileSymbols);
+  Reporter->reportSymbols(
+      SM->getFileEntryRefForID(SM->getMainFileID())->getName(), FileSymbols);
   FileSymbols.clear();
 }
 

diff  --git a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp
index 7a6cdf742f42a05..bb48883f88815c5 100644
--- a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp
+++ b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp
@@ -254,7 +254,7 @@ void FindAllSymbols::run(const MatchFinder::MatchResult &Result) {
   const SourceManager *SM = Result.SourceManager;
   if (auto Symbol = CreateSymbolInfo(ND, *SM, Collector)) {
     Filename =
-        std::string(SM->getFileEntryForID(SM->getMainFileID())->getName());
+        std::string(SM->getFileEntryRefForID(SM->getMainFileID())->getName());
     FileSymbols[*Symbol] += Signals;
   }
 }

diff  --git a/clang-tools-extra/clang-move/Move.cpp b/clang-tools-extra/clang-move/Move.cpp
index 905f1f456f4acce..94ba73ce9ad5a15 100644
--- a/clang-tools-extra/clang-move/Move.cpp
+++ b/clang-tools-extra/clang-move/Move.cpp
@@ -115,8 +115,8 @@ AST_POLYMORPHIC_MATCHER_P(isExpansionInFile,
   auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
   if (ExpansionLoc.isInvalid())
     return false;
-  auto *FileEntry =
-      SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
+  auto FileEntry =
+      SourceManager.getFileEntryRefForID(SourceManager.getFileID(ExpansionLoc));
   if (!FileEntry)
     return false;
   return MakeAbsolutePath(SourceManager, FileEntry->getName()) ==
@@ -135,7 +135,7 @@ class FindAllIncludes : public PPCallbacks {
                           StringRef /*RelativePath*/,
                           const Module * /*Imported*/,
                           SrcMgr::CharacteristicKind /*FileType*/) override {
-    if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)))
+    if (auto FileEntry = SM.getFileEntryRefForID(SM.getFileID(HashLoc)))
       MoveTool->addIncludes(FileName, IsAngled, SearchPath,
                             FileEntry->getName(), FilenameRange, SM);
   }
@@ -341,7 +341,7 @@ bool isInHeaderFile(const Decl *D, llvm::StringRef OriginalRunningDirectory,
   if (ExpansionLoc.isInvalid())
     return false;
 
-  if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) {
+  if (auto FE = SM.getFileEntryRefForID(SM.getFileID(ExpansionLoc))) {
     return MakeAbsolutePath(SM, FE->getName()) ==
            MakeAbsolutePath(OriginalRunningDirectory, OldHeader);
   }

diff  --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
index 09cd9aa2a93db9d..c1e4437b88949b9 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -552,7 +552,7 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
   // location needed depends on the check (in particular, where this check wants
   // to apply fixes).
   FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
-  const FileEntry *File = Sources.getFileEntryForID(FID);
+  OptionalFileEntryRef File = Sources.getFileEntryRefForID(FID);
 
   // -DMACRO definitions on the command line have locations in a virtual buffer
   // that doesn't have a FileEntry. Don't skip these as well.

diff  --git a/clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h b/clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
index f15026ea50cb33e..1827f54a2bc0648 100644
--- a/clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
+++ b/clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
@@ -34,8 +34,8 @@ AST_POLYMORPHIC_MATCHER(
   SourceLocation Loc = SourceManager.getSpellingLoc(Node.getBeginLoc());
   if (Loc.isInvalid())
     return false;
-  const FileEntry *FileEntry =
-      SourceManager.getFileEntryForID(SourceManager.getFileID(Loc));
+  OptionalFileEntryRef FileEntry =
+      SourceManager.getFileEntryRefForID(SourceManager.getFileID(Loc));
   if (!FileEntry)
     return false;
   // Determine whether filepath contains "absl/[absl-library]" substring, where

diff  --git a/clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp b/clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp
index 434af463830ba3a..084e44a714d1ff9 100644
--- a/clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp
@@ -76,7 +76,7 @@ bool KernelNameRestrictionPPCallbacks::fileNameIsRestricted(
 void KernelNameRestrictionPPCallbacks::EndOfMainFile() {
 
   // Check main file for restricted names.
-  const FileEntry *Entry = SM.getFileEntryForID(SM.getMainFileID());
+  OptionalFileEntryRef Entry = SM.getFileEntryRefForID(SM.getMainFileID());
   StringRef FileName = llvm::sys::path::filename(Entry->getName());
   if (fileNameIsRestricted(FileName))
     Check.diag(SM.getLocForStartOfFile(SM.getMainFileID()),

diff  --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
index c0255ea11e5ca63..14b6aca6f3b8fac 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -84,7 +84,7 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
     return;
   // We don't emit warnings on unused-using-decls from headers, so bail out if
   // the main file is a header.
-  if (const auto *MainFile = Result.SourceManager->getFileEntryForID(
+  if (auto MainFile = Result.SourceManager->getFileEntryRefForID(
           Result.SourceManager->getMainFileID());
       utils::isFileExtension(MainFile->getName(), HeaderFileExtensions))
     return;

diff  --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
index cd5cbdc25e10e19..80580bc9888a8cf 100644
--- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
+++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp
@@ -35,9 +35,10 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
     // guards.
     SourceManager &SM = PP->getSourceManager();
     if (Reason == EnterFile && FileType == SrcMgr::C_User) {
-      if (const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc))) {
+      if (OptionalFileEntryRef FE =
+              SM.getFileEntryRefForID(SM.getFileID(Loc))) {
         std::string FileName = cleanPath(FE->getName());
-        Files[FileName] = FE;
+        Files[FileName] = *FE;
       }
     }
   }
@@ -77,8 +78,8 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
       if (!MI->isUsedForHeaderGuard())
         continue;
 
-      const FileEntry *FE =
-          SM.getFileEntryForID(SM.getFileID(MI->getDefinitionLoc()));
+      OptionalFileEntryRef FE =
+          SM.getFileEntryRefForID(SM.getFileID(MI->getDefinitionLoc()));
       std::string FileName = cleanPath(FE->getName());
       Files.erase(FileName);
 

diff  --git a/clang-tools-extra/modularize/Modularize.cpp b/clang-tools-extra/modularize/Modularize.cpp
index d6f1520cc40e7e7..a7db1df1a1d2542 100644
--- a/clang-tools-extra/modularize/Modularize.cpp
+++ b/clang-tools-extra/modularize/Modularize.cpp
@@ -380,7 +380,7 @@ getModularizeArgumentsAdjuster(DependencyMap &Dependencies) {
 // want to design to be applicable to a wider range of tools, and stick it
 // somewhere into Tooling/ in mainline
 struct Location {
-  const FileEntry *File;
+  OptionalFileEntryRef File;
   unsigned Line, Column;
 
   Location() : File(), Line(), Column() {}
@@ -391,7 +391,7 @@ struct Location {
       return;
 
     std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
-    File = SM.getFileEntryForID(Decomposed.first);
+    File = SM.getFileEntryRefForID(Decomposed.first);
     if (!File)
       return;
 
@@ -483,12 +483,12 @@ typedef std::vector<HeaderEntry> HeaderContents;
 
 class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
 public:
-  DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
+  DenseMap<FileEntryRef, HeaderContents> HeaderContentMismatches;
 
   void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
     // Record this entity in its header.
     HeaderEntry HE = { Name, Loc };
-    CurHeaderContents[Loc.File].push_back(HE);
+    CurHeaderContents[*Loc.File].push_back(HE);
 
     // Check whether we've seen this entry before.
     SmallVector<Entry, 2> &Entries = (*this)[Name];
@@ -503,16 +503,13 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
   }
 
   void mergeCurHeaderContents() {
-    for (DenseMap<const FileEntry *, HeaderContents>::iterator
-             H = CurHeaderContents.begin(),
-             HEnd = CurHeaderContents.end();
+    for (auto H = CurHeaderContents.begin(), HEnd = CurHeaderContents.end();
          H != HEnd; ++H) {
       // Sort contents.
       llvm::sort(H->second);
 
       // Check whether we've seen this header before.
-      DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
-          AllHeaderContents.find(H->first);
+      auto KnownH = AllHeaderContents.find(H->first);
       if (KnownH == AllHeaderContents.end()) {
         // We haven't seen this header before; record its contents.
         AllHeaderContents.insert(*H);
@@ -534,8 +531,8 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
   }
 
 private:
-  DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
-  DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
+  DenseMap<FileEntryRef, HeaderContents> CurHeaderContents;
+  DenseMap<FileEntryRef, HeaderContents> AllHeaderContents;
 };
 
 class CollectEntitiesVisitor
@@ -961,9 +958,8 @@ int main(int Argc, const char **Argv) {
   // they are included.
   // FIXME: Could we provide information about which preprocessor conditionals
   // are involved?
-  for (DenseMap<const FileEntry *, HeaderContents>::iterator
-           H = Entities.HeaderContentMismatches.begin(),
-           HEnd = Entities.HeaderContentMismatches.end();
+  for (auto H = Entities.HeaderContentMismatches.begin(),
+            HEnd = Entities.HeaderContentMismatches.end();
        H != HEnd; ++H) {
     if (H->second.empty()) {
       errs() << "internal error: phantom header content mismatch\n";
@@ -971,8 +967,8 @@ int main(int Argc, const char **Argv) {
     }
 
     HadErrors = 1;
-    ModUtil->addUniqueProblemFile(std::string(H->first->getName()));
-    errs() << "error: header '" << H->first->getName()
+    ModUtil->addUniqueProblemFile(std::string(H->first.getName()));
+    errs() << "error: header '" << H->first.getName()
            << "' has 
diff erent contents depending on how it was included.\n";
     for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
       errs() << "note: '" << H->second[I].Name << "' in "

diff  --git a/clang-tools-extra/modularize/PreprocessorTracker.cpp b/clang-tools-extra/modularize/PreprocessorTracker.cpp
index 59517059d2a9e52..335195c6b199e76 100644
--- a/clang-tools-extra/modularize/PreprocessorTracker.cpp
+++ b/clang-tools-extra/modularize/PreprocessorTracker.cpp
@@ -1292,8 +1292,8 @@ void PreprocessorCallbacks::FileChanged(
     PPTracker.handleHeaderEntry(PP, getSourceLocationFile(PP, Loc));
     break;
   case ExitFile: {
-    const clang::FileEntry *F =
-        PP.getSourceManager().getFileEntryForID(PrevFID);
+    clang::OptionalFileEntryRef F =
+        PP.getSourceManager().getFileEntryRefForID(PrevFID);
     if (F)
       PPTracker.handleHeaderExit(F->getName());
   } break;

diff  --git a/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp b/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp
index 19773f3b07fe559..a59a8278682b23a 100644
--- a/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp
+++ b/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp
@@ -476,7 +476,8 @@ void PPCallbacksTracker::appendArgument(const char *Name, FileID Value) {
     appendArgument(Name, "(invalid)");
     return;
   }
-  const FileEntry *FileEntry = PP.getSourceManager().getFileEntryForID(Value);
+  OptionalFileEntryRef FileEntry =
+      PP.getSourceManager().getFileEntryRefForID(Value);
   if (!FileEntry) {
     appendArgument(Name, "(getFileEntryForID failed)");
     return;


        


More information about the cfe-commits mailing list