Skip to content

Commit 797cad9

Browse files
committed
[clang] NFCI: Use FileEntryRef in 'clang-tools-extra'
1 parent b19fe81 commit 797cad9

File tree

14 files changed

+42
-45
lines changed

14 files changed

+42
-45
lines changed

clang-tools-extra/clang-apply-replacements/include/clang-apply-replacements/Tooling/ApplyReplacements.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ typedef std::vector<std::string> TUReplacementFiles;
4040
typedef std::vector<clang::tooling::TranslationUnitDiagnostics> TUDiagnostics;
4141

4242
/// Map mapping file name to a set of AtomicChange targeting that file.
43-
typedef llvm::DenseMap<const clang::FileEntry *,
44-
std::vector<tooling::AtomicChange>>
45-
FileToChangesMap;
43+
using FileToChangesMap =
44+
llvm::DenseMap<clang::FileEntryRef, std::vector<tooling::AtomicChange>>;
4645

4746
/// Recursively descends through a directory structure rooted at \p
4847
/// Directory and attempts to deserialize *.yaml files as

clang-tools-extra/clang-apply-replacements/lib/Tooling/ApplyReplacements.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ std::error_code collectReplacementsFromDirectory(
138138
///
139139
/// \returns A map mapping FileEntry to a set of Replacement targeting that
140140
/// file.
141-
static llvm::DenseMap<const FileEntry *, std::vector<tooling::Replacement>>
141+
static llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
142142
groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
143143
const clang::SourceManager &SM) {
144144
llvm::StringSet<> Warned;
145-
llvm::DenseMap<const FileEntry *, std::vector<tooling::Replacement>>
145+
llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
146146
GroupedReplacements;
147147

148148
// Deduplicate identical replacements in diagnostics unless they are from the
@@ -165,7 +165,7 @@ groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
165165
else
166166
SM.getFileManager().makeAbsolutePath(Path);
167167

168-
if (auto Entry = SM.getFileManager().getFile(Path)) {
168+
if (auto Entry = SM.getFileManager().getOptionalFileRef(Path)) {
169169
if (SourceTU) {
170170
auto &Replaces = DiagReplacements[*Entry];
171171
auto It = Replaces.find(R);
@@ -212,10 +212,10 @@ bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs,
212212
// To report conflicting replacements on corresponding file, all replacements
213213
// are stored into 1 big AtomicChange.
214214
for (const auto &FileAndReplacements : GroupedReplacements) {
215-
const FileEntry *Entry = FileAndReplacements.first;
215+
FileEntryRef Entry = FileAndReplacements.first;
216216
const SourceLocation BeginLoc =
217217
SM.getLocForStartOfFile(SM.getOrCreateFileID(Entry, SrcMgr::C_User));
218-
tooling::AtomicChange FileChange(Entry->getName(), Entry->getName());
218+
tooling::AtomicChange FileChange(Entry.getName(), Entry.getName());
219219
for (const auto &R : FileAndReplacements.second) {
220220
llvm::Error Err =
221221
FileChange.replace(SM, BeginLoc.getLocWithOffset(R.getOffset()),

clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ int main(int argc, char **argv) {
146146
: tooling::ApplyChangesSpec::kNone;
147147

148148
for (const auto &FileChange : Changes) {
149-
const FileEntry *Entry = FileChange.first;
150-
StringRef FileName = Entry->getName();
149+
FileEntryRef Entry = FileChange.first;
150+
StringRef FileName = Entry.getName();
151151
llvm::Expected<std::string> NewFileData =
152152
applyChanges(FileName, FileChange.second, Spec, Diagnostics);
153153
if (!NewFileData) {

clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
6262
}
6363

6464
void FindAllMacros::EndOfMainFile() {
65-
Reporter->reportSymbols(SM->getFileEntryForID(SM->getMainFileID())->getName(),
66-
FileSymbols);
65+
Reporter->reportSymbols(
66+
SM->getFileEntryRefForID(SM->getMainFileID())->getName(), FileSymbols);
6767
FileSymbols.clear();
6868
}
6969

clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void FindAllSymbols::run(const MatchFinder::MatchResult &Result) {
254254
const SourceManager *SM = Result.SourceManager;
255255
if (auto Symbol = CreateSymbolInfo(ND, *SM, Collector)) {
256256
Filename =
257-
std::string(SM->getFileEntryForID(SM->getMainFileID())->getName());
257+
std::string(SM->getFileEntryRefForID(SM->getMainFileID())->getName());
258258
FileSymbols[*Symbol] += Signals;
259259
}
260260
}

clang-tools-extra/clang-move/Move.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ AST_POLYMORPHIC_MATCHER_P(isExpansionInFile,
115115
auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
116116
if (ExpansionLoc.isInvalid())
117117
return false;
118-
auto *FileEntry =
119-
SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
118+
auto FileEntry =
119+
SourceManager.getFileEntryRefForID(SourceManager.getFileID(ExpansionLoc));
120120
if (!FileEntry)
121121
return false;
122122
return MakeAbsolutePath(SourceManager, FileEntry->getName()) ==
@@ -135,7 +135,7 @@ class FindAllIncludes : public PPCallbacks {
135135
StringRef /*RelativePath*/,
136136
const Module * /*Imported*/,
137137
SrcMgr::CharacteristicKind /*FileType*/) override {
138-
if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)))
138+
if (auto FileEntry = SM.getFileEntryRefForID(SM.getFileID(HashLoc)))
139139
MoveTool->addIncludes(FileName, IsAngled, SearchPath,
140140
FileEntry->getName(), FilenameRange, SM);
141141
}
@@ -341,7 +341,7 @@ bool isInHeaderFile(const Decl *D, llvm::StringRef OriginalRunningDirectory,
341341
if (ExpansionLoc.isInvalid())
342342
return false;
343343

344-
if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) {
344+
if (auto FE = SM.getFileEntryRefForID(SM.getFileID(ExpansionLoc))) {
345345
return MakeAbsolutePath(SM, FE->getName()) ==
346346
MakeAbsolutePath(OriginalRunningDirectory, OldHeader);
347347
}

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
552552
// location needed depends on the check (in particular, where this check wants
553553
// to apply fixes).
554554
FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
555-
const FileEntry *File = Sources.getFileEntryForID(FID);
555+
OptionalFileEntryRef File = Sources.getFileEntryRefForID(FID);
556556

557557
// -DMACRO definitions on the command line have locations in a virtual buffer
558558
// that doesn't have a FileEntry. Don't skip these as well.

clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ AST_POLYMORPHIC_MATCHER(
3434
SourceLocation Loc = SourceManager.getSpellingLoc(Node.getBeginLoc());
3535
if (Loc.isInvalid())
3636
return false;
37-
const FileEntry *FileEntry =
38-
SourceManager.getFileEntryForID(SourceManager.getFileID(Loc));
37+
OptionalFileEntryRef FileEntry =
38+
SourceManager.getFileEntryRefForID(SourceManager.getFileID(Loc));
3939
if (!FileEntry)
4040
return false;
4141
// Determine whether filepath contains "absl/[absl-library]" substring, where

clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool KernelNameRestrictionPPCallbacks::fileNameIsRestricted(
7676
void KernelNameRestrictionPPCallbacks::EndOfMainFile() {
7777

7878
// Check main file for restricted names.
79-
const FileEntry *Entry = SM.getFileEntryForID(SM.getMainFileID());
79+
OptionalFileEntryRef Entry = SM.getFileEntryRefForID(SM.getMainFileID());
8080
StringRef FileName = llvm::sys::path::filename(Entry->getName());
8181
if (fileNameIsRestricted(FileName))
8282
Check.diag(SM.getLocForStartOfFile(SM.getMainFileID()),

clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
8484
return;
8585
// We don't emit warnings on unused-using-decls from headers, so bail out if
8686
// the main file is a header.
87-
if (const auto *MainFile = Result.SourceManager->getFileEntryForID(
87+
if (auto MainFile = Result.SourceManager->getFileEntryRefForID(
8888
Result.SourceManager->getMainFileID());
8989
utils::isFileExtension(MainFile->getName(), HeaderFileExtensions))
9090
return;

clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
3535
// guards.
3636
SourceManager &SM = PP->getSourceManager();
3737
if (Reason == EnterFile && FileType == SrcMgr::C_User) {
38-
if (const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc))) {
38+
if (OptionalFileEntryRef FE =
39+
SM.getFileEntryRefForID(SM.getFileID(Loc))) {
3940
std::string FileName = cleanPath(FE->getName());
40-
Files[FileName] = FE;
41+
Files[FileName] = *FE;
4142
}
4243
}
4344
}
@@ -77,8 +78,8 @@ class HeaderGuardPPCallbacks : public PPCallbacks {
7778
if (!MI->isUsedForHeaderGuard())
7879
continue;
7980

80-
const FileEntry *FE =
81-
SM.getFileEntryForID(SM.getFileID(MI->getDefinitionLoc()));
81+
OptionalFileEntryRef FE =
82+
SM.getFileEntryRefForID(SM.getFileID(MI->getDefinitionLoc()));
8283
std::string FileName = cleanPath(FE->getName());
8384
Files.erase(FileName);
8485

clang-tools-extra/modularize/Modularize.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ getModularizeArgumentsAdjuster(DependencyMap &Dependencies) {
380380
// want to design to be applicable to a wider range of tools, and stick it
381381
// somewhere into Tooling/ in mainline
382382
struct Location {
383-
const FileEntry *File;
383+
OptionalFileEntryRef File;
384384
unsigned Line, Column;
385385

386386
Location() : File(), Line(), Column() {}
@@ -391,7 +391,7 @@ struct Location {
391391
return;
392392

393393
std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(Loc);
394-
File = SM.getFileEntryForID(Decomposed.first);
394+
File = SM.getFileEntryRefForID(Decomposed.first);
395395
if (!File)
396396
return;
397397

@@ -483,12 +483,12 @@ typedef std::vector<HeaderEntry> HeaderContents;
483483

484484
class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
485485
public:
486-
DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;
486+
DenseMap<FileEntryRef, HeaderContents> HeaderContentMismatches;
487487

488488
void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
489489
// Record this entity in its header.
490490
HeaderEntry HE = { Name, Loc };
491-
CurHeaderContents[Loc.File].push_back(HE);
491+
CurHeaderContents[*Loc.File].push_back(HE);
492492

493493
// Check whether we've seen this entry before.
494494
SmallVector<Entry, 2> &Entries = (*this)[Name];
@@ -503,16 +503,13 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
503503
}
504504

505505
void mergeCurHeaderContents() {
506-
for (DenseMap<const FileEntry *, HeaderContents>::iterator
507-
H = CurHeaderContents.begin(),
508-
HEnd = CurHeaderContents.end();
506+
for (auto H = CurHeaderContents.begin(), HEnd = CurHeaderContents.end();
509507
H != HEnd; ++H) {
510508
// Sort contents.
511509
llvm::sort(H->second);
512510

513511
// Check whether we've seen this header before.
514-
DenseMap<const FileEntry *, HeaderContents>::iterator KnownH =
515-
AllHeaderContents.find(H->first);
512+
auto KnownH = AllHeaderContents.find(H->first);
516513
if (KnownH == AllHeaderContents.end()) {
517514
// We haven't seen this header before; record its contents.
518515
AllHeaderContents.insert(*H);
@@ -534,8 +531,8 @@ class EntityMap : public std::map<std::string, SmallVector<Entry, 2>> {
534531
}
535532

536533
private:
537-
DenseMap<const FileEntry *, HeaderContents> CurHeaderContents;
538-
DenseMap<const FileEntry *, HeaderContents> AllHeaderContents;
534+
DenseMap<FileEntryRef, HeaderContents> CurHeaderContents;
535+
DenseMap<FileEntryRef, HeaderContents> AllHeaderContents;
539536
};
540537

541538
class CollectEntitiesVisitor
@@ -961,18 +958,17 @@ int main(int Argc, const char **Argv) {
961958
// they are included.
962959
// FIXME: Could we provide information about which preprocessor conditionals
963960
// are involved?
964-
for (DenseMap<const FileEntry *, HeaderContents>::iterator
965-
H = Entities.HeaderContentMismatches.begin(),
966-
HEnd = Entities.HeaderContentMismatches.end();
961+
for (auto H = Entities.HeaderContentMismatches.begin(),
962+
HEnd = Entities.HeaderContentMismatches.end();
967963
H != HEnd; ++H) {
968964
if (H->second.empty()) {
969965
errs() << "internal error: phantom header content mismatch\n";
970966
continue;
971967
}
972968

973969
HadErrors = 1;
974-
ModUtil->addUniqueProblemFile(std::string(H->first->getName()));
975-
errs() << "error: header '" << H->first->getName()
970+
ModUtil->addUniqueProblemFile(std::string(H->first.getName()));
971+
errs() << "error: header '" << H->first.getName()
976972
<< "' has different contents depending on how it was included.\n";
977973
for (unsigned I = 0, N = H->second.size(); I != N; ++I) {
978974
errs() << "note: '" << H->second[I].Name << "' in "

clang-tools-extra/modularize/PreprocessorTracker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,8 @@ void PreprocessorCallbacks::FileChanged(
12921292
PPTracker.handleHeaderEntry(PP, getSourceLocationFile(PP, Loc));
12931293
break;
12941294
case ExitFile: {
1295-
const clang::FileEntry *F =
1296-
PP.getSourceManager().getFileEntryForID(PrevFID);
1295+
clang::OptionalFileEntryRef F =
1296+
PP.getSourceManager().getFileEntryRefForID(PrevFID);
12971297
if (F)
12981298
PPTracker.handleHeaderExit(F->getName());
12991299
} break;

clang-tools-extra/pp-trace/PPCallbacksTracker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ void PPCallbacksTracker::appendArgument(const char *Name, FileID Value) {
476476
appendArgument(Name, "(invalid)");
477477
return;
478478
}
479-
const FileEntry *FileEntry = PP.getSourceManager().getFileEntryForID(Value);
479+
OptionalFileEntryRef FileEntry =
480+
PP.getSourceManager().getFileEntryRefForID(Value);
480481
if (!FileEntry) {
481482
appendArgument(Name, "(getFileEntryForID failed)");
482483
return;

0 commit comments

Comments
 (0)