Skip to content

Commit 98e6deb

Browse files
committed
[clang][tools] Use FileEntryRef in include_cleaner::Header
1 parent 37b0889 commit 98e6deb

File tree

15 files changed

+68
-64
lines changed

15 files changed

+68
-64
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bool IncludeCleanerCheck::shouldIgnore(const include_cleaner::Header &H) {
101101
case include_cleaner::Header::Verbatim:
102102
return R.match(H.verbatim());
103103
case include_cleaner::Header::Physical:
104-
return R.match(H.physical()->tryGetRealPathName());
104+
return R.match(H.physical().getFileEntry().tryGetRealPathName());
105105
}
106106
llvm_unreachable("Unknown Header kind.");
107107
});
@@ -145,7 +145,7 @@ void IncludeCleanerCheck::check(const MatchFinder::MatchResult &Result) {
145145
for (const include_cleaner::Header &H : Providers) {
146146
if (H.kind() == include_cleaner::Header::Physical &&
147147
(H.physical() == MainFile ||
148-
H.physical()->getDir() == ResourceDir)) {
148+
H.physical().getDir() == ResourceDir)) {
149149
Satisfied = true;
150150
continue;
151151
}

clang-tools-extra/clangd/IncludeCleaner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ IncludeCleanerFindings computeIncludeCleanerFindings(ParsedAST &AST) {
410410
for (const auto &H : Providers) {
411411
if (H.kind() == include_cleaner::Header::Physical &&
412412
(H.physical() == MainFile || H.physical() == PreamblePatch ||
413-
H.physical()->getLastRef().getDir() == ResourceDir)) {
413+
H.physical().getDir() == ResourceDir)) {
414414
Satisfied = true;
415415
continue;
416416
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ TEST(IncludeCleaner, ComputeMissingHeaders) {
167167
size_t End = llvm::cantFail(positionToOffset(MainFile.code(), Range.end));
168168
syntax::FileRange BRange{SM.getMainFileID(), static_cast<unsigned int>(Start),
169169
static_cast<unsigned int>(End)};
170-
include_cleaner::Header Header{*SM.getFileManager().getFile("b.h")};
170+
include_cleaner::Header Header{
171+
*SM.getFileManager().getOptionalFileRef("b.h")};
171172
MissingIncludeDiagInfo BInfo{B, BRange, {Header}};
172173
EXPECT_THAT(Findings.MissingIncludes, ElementsAre(BInfo));
173174
}
@@ -474,8 +475,8 @@ TEST(IncludeCleaner, IsPreferredProvider) {
474475
auto &IncludeDef2 = AST.getIncludeStructure().MainFileIncludes[2];
475476

476477
auto &FM = AST.getSourceManager().getFileManager();
477-
auto *DeclH = &FM.getOptionalFileRef("decl.h")->getFileEntry();
478-
auto *DefH = &FM.getOptionalFileRef("def.h")->getFileEntry();
478+
auto DeclH = *FM.getOptionalFileRef("decl.h");
479+
auto DefH = *FM.getOptionalFileRef("def.h");
479480

480481
auto Includes = convertIncludes(AST);
481482
std::vector<include_cleaner::Header> Providers = {

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class PragmaIncludes {
6767

6868
/// Returns all direct exporter headers for the given header file.
6969
/// Returns empty if there is none.
70-
llvm::SmallVector<const FileEntry *> getExporters(const FileEntry *File,
71-
FileManager &FM) const;
72-
llvm::SmallVector<const FileEntry *> getExporters(tooling::stdlib::Header,
73-
FileManager &FM) const;
70+
llvm::SmallVector<FileEntryRef> getExporters(const FileEntry *File,
71+
FileManager &FM) const;
72+
llvm::SmallVector<FileEntryRef> getExporters(tooling::stdlib::Header,
73+
FileManager &FM) const;
7474

7575
/// Returns true if the given file is a self-contained file.
7676
bool isSelfContained(const FileEntry *File) const;

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ struct Header {
122122
Verbatim,
123123
};
124124

125-
Header(const FileEntry *FE) : Storage(FE) {}
125+
Header(FileEntryRef FE) : Storage(FE) {}
126126
Header(tooling::stdlib::Header H) : Storage(H) {}
127127
Header(StringRef VerbatimSpelling) : Storage(VerbatimSpelling) {}
128128

129129
Kind kind() const { return static_cast<Kind>(Storage.index()); }
130130
bool operator==(const Header &RHS) const { return Storage == RHS.Storage; }
131131
bool operator<(const Header &RHS) const;
132132

133-
const FileEntry *physical() const { return std::get<Physical>(Storage); }
133+
FileEntryRef physical() const { return std::get<Physical>(Storage); }
134134
tooling::stdlib::Header standard() const {
135135
return std::get<Standard>(Storage);
136136
}
@@ -142,7 +142,7 @@ struct Header {
142142

143143
private:
144144
// Order must match Kind enum!
145-
std::variant<const FileEntry *, tooling::stdlib::Header, StringRef> Storage;
145+
std::variant<FileEntryRef, tooling::stdlib::Header, StringRef> Storage;
146146

147147
// Disambiguation tag to make sure we can call the right constructor from
148148
// DenseMapInfo methods.

clang-tools-extra/include-cleaner/lib/Analysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ analyze(llvm::ArrayRef<Decl *> ASTRoots,
9595
for (const Header &H : Providers) {
9696
if (H.kind() == Header::Physical &&
9797
(H.physical() == MainFile ||
98-
H.physical()->getDir() == ResourceDir)) {
98+
H.physical().getDir() == ResourceDir)) {
9999
Satisfied = true;
100100
}
101101
for (const Include *I : Inc.match(H)) {

clang-tools-extra/include-cleaner/lib/FindHeaders.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ llvm::StringRef basename(llvm::StringRef Header) {
6363
bool nameMatch(llvm::StringRef DeclName, Header H) {
6464
switch (H.kind()) {
6565
case Header::Physical:
66-
return basename(H.physical()->getName()).equals_insensitive(DeclName);
66+
return basename(H.physical().getName()).equals_insensitive(DeclName);
6767
case Header::Standard:
6868
return basename(H.standard().name()).equals_insensitive(DeclName);
6969
case Header::Verbatim:
@@ -101,7 +101,7 @@ hintedHeadersForStdHeaders(llvm::ArrayRef<tooling::stdlib::Header> Headers,
101101
Results.emplace_back(H, Hints::PublicHeader | Hints::OriginHeader);
102102
if (!PI)
103103
continue;
104-
for (const auto *Export : PI->getExporters(H, SM.getFileManager()))
104+
for (FileEntryRef Export : PI->getExporters(H, SM.getFileManager()))
105105
Results.emplace_back(Header(Export), isPublicHeader(Export, *PI));
106106
}
107107
// StandardLibrary returns headers in preference order, so only mark the
@@ -186,31 +186,31 @@ llvm::SmallVector<Hinted<Header>> findHeaders(const SymbolLocation &Loc,
186186
switch (Loc.kind()) {
187187
case SymbolLocation::Physical: {
188188
FileID FID = SM.getFileID(SM.getExpansionLoc(Loc.physical()));
189-
const FileEntry *FE = SM.getFileEntryForID(FID);
189+
OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID);
190190
if (!FE)
191191
return {};
192192
if (!PI)
193-
return {{FE, Hints::PublicHeader | Hints::OriginHeader}};
193+
return {{*FE, Hints::PublicHeader | Hints::OriginHeader}};
194194
bool IsOrigin = true;
195-
std::queue<const FileEntry *> Exporters;
195+
std::queue<FileEntryRef> Exporters;
196196
while (FE) {
197-
Results.emplace_back(FE,
198-
isPublicHeader(FE, *PI) |
197+
Results.emplace_back(*FE,
198+
isPublicHeader(*FE, *PI) |
199199
(IsOrigin ? Hints::OriginHeader : Hints::None));
200-
for (const auto *Export : PI->getExporters(FE, SM.getFileManager()))
200+
for (FileEntryRef Export : PI->getExporters(*FE, SM.getFileManager()))
201201
Exporters.push(Export);
202202

203-
if (auto Verbatim = PI->getPublic(FE); !Verbatim.empty()) {
203+
if (auto Verbatim = PI->getPublic(*FE); !Verbatim.empty()) {
204204
Results.emplace_back(Verbatim,
205205
Hints::PublicHeader | Hints::PreferredHeader);
206206
break;
207207
}
208-
if (PI->isSelfContained(FE) || FID == SM.getMainFileID())
208+
if (PI->isSelfContained(*FE) || FID == SM.getMainFileID())
209209
break;
210210

211211
// Walkup the include stack for non self-contained headers.
212212
FID = SM.getDecomposedIncludedLoc(FID).first;
213-
FE = SM.getFileEntryForID(FID);
213+
FE = SM.getFileEntryRefForID(FID);
214214
IsOrigin = false;
215215
}
216216
// Now traverse provider trees rooted at exporters.
@@ -219,12 +219,12 @@ llvm::SmallVector<Hinted<Header>> findHeaders(const SymbolLocation &Loc,
219219
// being exported in this header.
220220
std::set<const FileEntry *> SeenExports;
221221
while (!Exporters.empty()) {
222-
auto *Export = Exporters.front();
222+
FileEntryRef Export = Exporters.front();
223223
Exporters.pop();
224224
if (!SeenExports.insert(Export).second) // In case of cyclic exports
225225
continue;
226226
Results.emplace_back(Export, isPublicHeader(Export, *PI));
227-
for (const auto *Export : PI->getExporters(Export, SM.getFileManager()))
227+
for (FileEntryRef Export : PI->getExporters(Export, SM.getFileManager()))
228228
Exporters.push(Export);
229229
}
230230
return Results;

clang-tools-extra/include-cleaner/lib/HTMLReport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class Reporter {
390390
OS << "<tr><th>Header</th><td>";
391391
switch (H.kind()) {
392392
case Header::Physical:
393-
printFilename(H.physical()->getName());
393+
printFilename(H.physical().getName());
394394
break;
395395
case Header::Standard:
396396
OS << "stdlib " << H.standard().name();

clang-tools-extra/include-cleaner/lib/Record.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler {
224224
IncludedHeader = *StandardHeader;
225225
}
226226
if (!IncludedHeader && File)
227-
IncludedHeader = &File->getFileEntry();
227+
IncludedHeader = *File;
228228
checkForExport(HashFID, HashLine, std::move(IncludedHeader), File);
229229
checkForKeep(HashLine, File);
230230
}
@@ -243,7 +243,7 @@ class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler {
243243
if (IncludedHeader) {
244244
switch (IncludedHeader->kind()) {
245245
case Header::Physical:
246-
Out->IWYUExportBy[IncludedHeader->physical()->getUniqueID()]
246+
Out->IWYUExportBy[IncludedHeader->physical().getUniqueID()]
247247
.push_back(Top.Path);
248248
break;
249249
case Header::Standard:
@@ -393,26 +393,26 @@ llvm::StringRef PragmaIncludes::getPublic(const FileEntry *F) const {
393393
return It->getSecond();
394394
}
395395

396-
static llvm::SmallVector<const FileEntry *>
396+
static llvm::SmallVector<FileEntryRef>
397397
toFileEntries(llvm::ArrayRef<StringRef> FileNames, FileManager &FM) {
398-
llvm::SmallVector<const FileEntry *> Results;
398+
llvm::SmallVector<FileEntryRef> Results;
399399

400400
for (auto FName : FileNames) {
401401
// FIMXE: log the failing cases?
402-
if (auto FE = expectedToOptional(FM.getFileRef(FName)))
402+
if (auto FE = FM.getOptionalFileRef(FName))
403403
Results.push_back(*FE);
404404
}
405405
return Results;
406406
}
407-
llvm::SmallVector<const FileEntry *>
407+
llvm::SmallVector<FileEntryRef>
408408
PragmaIncludes::getExporters(const FileEntry *File, FileManager &FM) const {
409409
auto It = IWYUExportBy.find(File->getUniqueID());
410410
if (It == IWYUExportBy.end())
411411
return {};
412412

413413
return toFileEntries(It->getSecond(), FM);
414414
}
415-
llvm::SmallVector<const FileEntry *>
415+
llvm::SmallVector<FileEntryRef>
416416
PragmaIncludes::getExporters(tooling::stdlib::Header StdHeader,
417417
FileManager &FM) const {
418418
auto It = StdIWYUExportBy.find(StdHeader);

clang-tools-extra/include-cleaner/lib/Types.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S) {
4848
llvm::StringRef Header::resolvedPath() const {
4949
switch (kind()) {
5050
case include_cleaner::Header::Physical:
51-
return physical()->tryGetRealPathName();
51+
return physical().getFileEntry().tryGetRealPathName();
5252
case include_cleaner::Header::Standard:
5353
return standard().name().trim("<>\"");
5454
case include_cleaner::Header::Verbatim:
@@ -60,7 +60,7 @@ llvm::StringRef Header::resolvedPath() const {
6060
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Header &H) {
6161
switch (H.kind()) {
6262
case Header::Physical:
63-
return OS << H.physical()->getName();
63+
return OS << H.physical().getName();
6464
case Header::Standard:
6565
return OS << H.standard().name();
6666
case Header::Verbatim:
@@ -198,7 +198,7 @@ bool Header::operator<(const Header &RHS) const {
198198
return kind() < RHS.kind();
199199
switch (kind()) {
200200
case Header::Physical:
201-
return physical()->getName() < RHS.physical()->getName();
201+
return physical().getName() < RHS.physical().getName();
202202
case Header::Standard:
203203
return standard().name() < RHS.standard().name();
204204
case Header::Verbatim:

0 commit comments

Comments
 (0)