Skip to content

Commit 6f3f1e9

Browse files
committed
[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).
1 parent ffe0cc8 commit 6f3f1e9

File tree

9 files changed

+21
-20
lines changed

9 files changed

+21
-20
lines changed

clang-tools-extra/clangd/Diagnostics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,16 @@ bool tryMoveToMainFile(Diag &D, FullSourceLoc DiagLoc) {
196196
return false;
197197

198198
// Add a note that will point to real diagnostic.
199-
const auto *FE = SM.getFileEntryForID(SM.getFileID(DiagLoc));
199+
auto FE = SM.getFileEntryRefForID(SM.getFileID(DiagLoc)).getValue();
200200
D.Notes.emplace(D.Notes.begin());
201201
Note &N = D.Notes.front();
202-
N.AbsFile = std::string(FE->tryGetRealPathName());
203-
N.File = std::string(FE->getName());
202+
N.AbsFile = std::string(FE.getFileEntry().tryGetRealPathName());
203+
N.File = std::string(FE.getName());
204204
N.Message = "error occurred here";
205205
N.Range = D.Range;
206206

207207
// Update diag to point at include inside main file.
208-
D.File = SM.getFileEntryForID(SM.getMainFileID())->getName().str();
208+
D.File = SM.getFileEntryRefForID(SM.getMainFileID())->getName().str();
209209
D.Range = std::move(R);
210210
D.InsideMainFile = true;
211211
// Update message to mention original file.

clang-tools-extra/clangd/IncludeCleaner.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,14 @@ static bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST) {
246246
// Headers without include guards have side effects and are not
247247
// self-contained, skip them.
248248
assert(Inc.HeaderID);
249-
auto FE = AST.getSourceManager().getFileManager().getFile(
249+
auto FE = AST.getSourceManager().getFileManager().getFileRef(
250250
AST.getIncludeStructure().getRealPath(
251251
static_cast<IncludeStructure::HeaderID>(*Inc.HeaderID)));
252252
assert(FE);
253253
if (!AST.getPreprocessor().getHeaderSearchInfo().isFileMultipleIncludeGuarded(
254-
*FE)) {
254+
&FE->getFileEntry())) {
255255
dlog("{0} doesn't have header guard and will not be considered unused",
256-
(*FE)->getName());
256+
FE->getName());
257257
return false;
258258
}
259259
return true;
@@ -418,7 +418,7 @@ std::vector<Diag> issueUnusedIncludesDiagnostics(ParsedAST &AST,
418418
std::vector<Diag> Result;
419419
std::string FileName =
420420
AST.getSourceManager()
421-
.getFileEntryForID(AST.getSourceManager().getMainFileID())
421+
.getFileEntryRefForID(AST.getSourceManager().getMainFileID())
422422
->getName()
423423
.str();
424424
for (const auto *Inc : computeUnusedIncludes(AST)) {

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ PreamblePatch PreamblePatch::unmodified(const PreambleData &Preamble) {
677677
SourceLocation translatePreamblePatchLocation(SourceLocation Loc,
678678
const SourceManager &SM) {
679679
auto DefFile = SM.getFileID(Loc);
680-
if (auto *FE = SM.getFileEntryForID(DefFile)) {
680+
if (auto FE = SM.getFileEntryRefForID(DefFile)) {
681681
auto IncludeLoc = SM.getIncludeLoc(DefFile);
682682
// Preamble patch is included inside the builtin file.
683683
if (IncludeLoc.isValid() && SM.isWrittenInBuiltinFile(IncludeLoc) &&

clang-tools-extra/clangd/index/CanonicalIncludes.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ collectIWYUHeaderMaps(CanonicalIncludes *Includes) {
6666
PP.getSourceManager(), PP.getLangOpts());
6767
if (!Text.consume_front(IWYUPragma))
6868
return false;
69-
// FIXME(ioeric): resolve the header and store actual file path. For now,
70-
// we simply assume the written header is suitable to be #included.
71-
Includes->addMapping(PP.getSourceManager().getFilename(Range.getBegin()),
72-
isLiteralInclude(Text) ? Text.str()
73-
: ("\"" + Text + "\"").str());
69+
// We always insert using the spelling from the pragma.
70+
if (auto *FE = PP.getSourceManager().getFileEntryForID(
71+
PP.getSourceManager().getFileID(Range.getBegin())))
72+
Includes->addMapping(FE->getName(), isLiteralInclude(Text)
73+
? Text.str()
74+
: ("\"" + Text + "\"").str());
7475
return false;
7576
}
7677

clang-tools-extra/clangd/index/FileIndex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ SlabTuple indexSymbols(ASTContext &AST, Preprocessor &PP,
8484
Collector.handleMacros(*MacroRefsToIndex);
8585

8686
const auto &SM = AST.getSourceManager();
87-
const auto *MainFileEntry = SM.getFileEntryForID(SM.getMainFileID());
87+
const auto MainFileEntry = SM.getFileEntryRefForID(SM.getMainFileID());
8888
std::string FileName =
8989
std::string(MainFileEntry ? MainFileEntry->getName() : "");
9090

clang-tools-extra/clangd/index/SymbolCollector.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ bool SymbolCollector::handleDeclOccurrence(
561561
// it's main-file only.
562562
bool IsMainFileOnly =
563563
SM.isWrittenInMainFile(SM.getExpansionLoc(ND->getBeginLoc())) &&
564-
!isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
564+
!isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
565565
ASTCtx->getLangOpts());
566566
// In C, printf is a redecl of an implicit builtin! So check OrigD instead.
567567
if (ASTNode.OrigD->isImplicit() ||
@@ -688,7 +688,7 @@ bool SymbolCollector::handleMacroOccurrence(const IdentifierInfo *Name,
688688
auto SpellingLoc = SM.getSpellingLoc(Loc);
689689
bool IsMainFileOnly =
690690
SM.isInMainFile(SM.getExpansionLoc(DefLoc)) &&
691-
!isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
691+
!isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
692692
ASTCtx->getLangOpts());
693693
// Do not store references to main-file macros.
694694
if ((static_cast<unsigned>(Opts.RefFilter) & Roles) && !IsMainFileOnly &&

clang-tools-extra/clangd/refactor/Tweak.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Tweak::Effect::fileEdit(const SourceManager &SM, FileID FID,
108108
if (auto FilePath = getCanonicalPath(SM.getFileEntryForID(FID), SM))
109109
return std::make_pair(*FilePath, std::move(Ed));
110110
return error("Failed to get absolute path for edited file: {0}",
111-
SM.getFileEntryForID(FID)->getName());
111+
SM.getFileEntryRefForID(FID)->getName());
112112
}
113113

114114
llvm::Expected<Tweak::Effect>

clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ bool AddUsing::prepare(const Selection &Inputs) {
237237
const auto &TB = Inputs.AST->getTokens();
238238

239239
// Do not suggest "using" in header files. That way madness lies.
240-
if (isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
240+
if (isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
241241
Inputs.AST->getLangOpts()))
242242
return false;
243243

clang-tools-extra/clangd/unittests/IndexActionTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ TEST_F(IndexActionTest, SkipFiles) {
271271
auto unskippable2() { return S(); }
272272
)cpp");
273273
Opts.FileFilter = [](const SourceManager &SM, FileID F) {
274-
return !SM.getFileEntryForID(F)->getName().endswith("bad.h");
274+
return !SM.getFileEntryRefForID(F)->getName().endswith("bad.h");
275275
};
276276
IndexFileIn IndexFile = runIndexingAction(MainFilePath, {"-std=c++14"});
277277
EXPECT_THAT(*IndexFile.Symbols,

0 commit comments

Comments
 (0)