From 797cad9d32885ca7a0ccfa657eba5c739f189e98 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Sun, 10 Sep 2023 10:13:51 -0700 Subject: [PATCH] [clang] NFCI: Use `FileEntryRef` in 'clang-tools-extra' --- .../Tooling/ApplyReplacements.h | 5 ++-- .../lib/Tooling/ApplyReplacements.cpp | 10 +++---- .../tool/ClangApplyReplacementsMain.cpp | 4 +-- .../find-all-symbols/FindAllMacros.cpp | 4 +-- .../find-all-symbols/FindAllSymbols.cpp | 2 +- clang-tools-extra/clang-move/Move.cpp | 8 +++--- .../ClangTidyDiagnosticConsumer.cpp | 2 +- .../clang-tidy/abseil/AbseilMatcher.h | 4 +-- .../altera/KernelNameRestrictionCheck.cpp | 2 +- .../clang-tidy/misc/UnusedUsingDeclsCheck.cpp | 2 +- .../clang-tidy/utils/HeaderGuard.cpp | 9 +++--- clang-tools-extra/modularize/Modularize.cpp | 28 ++++++++----------- .../modularize/PreprocessorTracker.cpp | 4 +-- .../pp-trace/PPCallbacksTracker.cpp | 3 +- 14 files changed, 42 insertions(+), 45 deletions(-) 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 b0cf7317c0ed..1f0d87371914 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 TUReplacementFiles; typedef std::vector TUDiagnostics; /// Map mapping file name to a set of AtomicChange targeting that file. -typedef llvm::DenseMap> - FileToChangesMap; +using FileToChangesMap = + llvm::DenseMap>; /// 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 a0a510c20131..b490780f4852 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> +static llvm::DenseMap> groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs, const clang::SourceManager &SM) { llvm::StringSet<> Warned; - llvm::DenseMap> + llvm::DenseMap> 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 415ae47ce6d7..9331898bf257 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 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 f07e9c95d107..f38b6889d32e 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 7a6cdf742f42..bb48883f8881 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 905f1f456f4a..94ba73ce9ad5 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 @@ public: 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 09cd9aa2a93d..c1e4437b8894 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 f15026ea50cb..1827f54a2bc0 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 434af463830b..084e44a714d1 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 c0255ea11e5c..14b6aca6f3b8 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 cd5cbdc25e10..80580bc9888a 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp +++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp @@ -35,9 +35,10 @@ public: // 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 @@ public: 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 d6f1520cc40e..a7db1df1a1d2 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 Decomposed = SM.getDecomposedLoc(Loc); - File = SM.getFileEntryForID(Decomposed.first); + File = SM.getFileEntryRefForID(Decomposed.first); if (!File) return; @@ -483,12 +483,12 @@ typedef std::vector HeaderContents; class EntityMap : public std::map> { public: - DenseMap HeaderContentMismatches; + DenseMap 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 &Entries = (*this)[Name]; @@ -503,16 +503,13 @@ public: } void mergeCurHeaderContents() { - for (DenseMap::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::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 @@ public: } private: - DenseMap CurHeaderContents; - DenseMap AllHeaderContents; + DenseMap CurHeaderContents; + DenseMap 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::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 different 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 59517059d2a9..335195c6b199 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 19773f3b07fe..a59a8278682b 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;