Skip to content

Commit 0cb0a48

Browse files
authored
[clang] NFC: Remove OptionalFileEntryRefDegradesToFileEntryPtr (#74899)
1 parent f1e3e8a commit 0cb0a48

File tree

18 files changed

+33
-124
lines changed

18 files changed

+33
-124
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void ExpandModularHeadersPPCallbacks::InclusionDirective(
171171
if (Imported) {
172172
serialization::ModuleFile *MF =
173173
Compiler.getASTReader()->getModuleManager().lookup(
174-
Imported->getASTFile());
174+
*Imported->getASTFile());
175175
handleModuleFile(MF);
176176
}
177177
parseToLocation(DirectiveLoc);

clang/include/clang/Basic/FileEntry.h

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -279,72 +279,6 @@ template <> struct DenseMapInfo<clang::FileEntryRef> {
279279

280280
namespace clang {
281281

282-
/// Wrapper around OptionalFileEntryRef that degrades to 'const FileEntry*',
283-
/// facilitating incremental patches to propagate FileEntryRef.
284-
///
285-
/// This class can be used as return value or field where it's convenient for
286-
/// an OptionalFileEntryRef to degrade to a 'const FileEntry*'. The purpose
287-
/// is to avoid code churn due to dances like the following:
288-
/// \code
289-
/// // Old code.
290-
/// lvalue = rvalue;
291-
///
292-
/// // Temporary code from an incremental patch.
293-
/// OptionalFileEntryRef MaybeF = rvalue;
294-
/// lvalue = MaybeF ? &MaybeF.getFileEntry() : nullptr;
295-
///
296-
/// // Final code.
297-
/// lvalue = rvalue;
298-
/// \endcode
299-
///
300-
/// FIXME: Once FileEntryRef is "everywhere" and FileEntry::LastRef and
301-
/// FileEntry::getName have been deleted, delete this class and replace
302-
/// instances with OptionalFileEntryRef.
303-
class OptionalFileEntryRefDegradesToFileEntryPtr : public OptionalFileEntryRef {
304-
public:
305-
OptionalFileEntryRefDegradesToFileEntryPtr() = default;
306-
OptionalFileEntryRefDegradesToFileEntryPtr(
307-
OptionalFileEntryRefDegradesToFileEntryPtr &&) = default;
308-
OptionalFileEntryRefDegradesToFileEntryPtr(
309-
const OptionalFileEntryRefDegradesToFileEntryPtr &) = default;
310-
OptionalFileEntryRefDegradesToFileEntryPtr &
311-
operator=(OptionalFileEntryRefDegradesToFileEntryPtr &&) = default;
312-
OptionalFileEntryRefDegradesToFileEntryPtr &
313-
operator=(const OptionalFileEntryRefDegradesToFileEntryPtr &) = default;
314-
315-
OptionalFileEntryRefDegradesToFileEntryPtr(std::nullopt_t) {}
316-
OptionalFileEntryRefDegradesToFileEntryPtr(FileEntryRef Ref)
317-
: OptionalFileEntryRef(Ref) {}
318-
OptionalFileEntryRefDegradesToFileEntryPtr(OptionalFileEntryRef MaybeRef)
319-
: OptionalFileEntryRef(MaybeRef) {}
320-
321-
OptionalFileEntryRefDegradesToFileEntryPtr &operator=(std::nullopt_t) {
322-
OptionalFileEntryRef::operator=(std::nullopt);
323-
return *this;
324-
}
325-
OptionalFileEntryRefDegradesToFileEntryPtr &operator=(FileEntryRef Ref) {
326-
OptionalFileEntryRef::operator=(Ref);
327-
return *this;
328-
}
329-
OptionalFileEntryRefDegradesToFileEntryPtr &
330-
operator=(OptionalFileEntryRef MaybeRef) {
331-
OptionalFileEntryRef::operator=(MaybeRef);
332-
return *this;
333-
}
334-
335-
/// Degrade to 'const FileEntry *' to allow FileEntry::LastRef and
336-
/// FileEntry::getName have been deleted, delete this class and replace
337-
/// instances with OptionalFileEntryRef
338-
operator const FileEntry *() const {
339-
return has_value() ? &(*this)->getFileEntry() : nullptr;
340-
}
341-
};
342-
343-
static_assert(
344-
std::is_trivially_copyable<
345-
OptionalFileEntryRefDegradesToFileEntryPtr>::value,
346-
"OptionalFileEntryRefDegradesToFileEntryPtr should be trivially copyable");
347-
348282
inline bool operator==(const FileEntry *LHS, const OptionalFileEntryRef &RHS) {
349283
return LHS == (RHS ? &RHS->getFileEntry() : nullptr);
350284
}

clang/include/clang/Basic/Module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ class alignas(8) Module {
672672
}
673673

674674
/// The serialized AST file for this module, if one was created.
675-
OptionalFileEntryRefDegradesToFileEntryPtr getASTFile() const {
675+
OptionalFileEntryRef getASTFile() const {
676676
return getTopLevelModule()->ASTFile;
677677
}
678678

clang/include/clang/Basic/SourceManager.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class alignas(8) ContentCache {
143143
///
144144
/// FIXME: Make non-optional using a virtual file as needed, remove \c
145145
/// Filename and use \c OrigEntry.getNameAsRequested() instead.
146-
OptionalFileEntryRefDegradesToFileEntryPtr OrigEntry;
146+
OptionalFileEntryRef OrigEntry;
147147

148148
/// References the file which the contents were actually loaded from.
149149
///
@@ -1064,8 +1064,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
10641064

10651065
/// Returns the FileEntry record for the provided FileID.
10661066
const FileEntry *getFileEntryForID(FileID FID) const {
1067-
if (auto *Entry = getSLocEntryForFile(FID))
1068-
return Entry->getFile().getContentCache().OrigEntry;
1067+
if (auto FE = getFileEntryRefForID(FID))
1068+
return *FE;
10691069
return nullptr;
10701070
}
10711071

@@ -1083,9 +1083,11 @@ class SourceManager : public RefCountedBase<SourceManager> {
10831083
std::optional<StringRef> getNonBuiltinFilenameForID(FileID FID) const;
10841084

10851085
/// Returns the FileEntry record for the provided SLocEntry.
1086-
const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
1087-
{
1088-
return sloc.getFile().getContentCache().OrigEntry;
1086+
const FileEntry *
1087+
getFileEntryForSLocEntry(const SrcMgr::SLocEntry &SLocEntry) const {
1088+
if (auto FE = SLocEntry.getFile().getContentCache().OrigEntry)
1089+
return *FE;
1090+
return nullptr;
10891091
}
10901092

10911093
/// Return a StringRef to the source buffer data for the

clang/include/clang/Lex/PreprocessorLexer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class PreprocessorLexer {
157157

158158
/// getFileEntry - Return the FileEntry corresponding to this FileID. Like
159159
/// getFileID(), this only works for lexers with attached preprocessors.
160-
OptionalFileEntryRefDegradesToFileEntryPtr getFileEntry() const;
160+
OptionalFileEntryRef getFileEntry() const;
161161

162162
/// Iterator that traverses the current stack of preprocessor
163163
/// conditional directives (\#if/\#ifdef/\#ifndef).

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class InputFile {
104104
return File;
105105
}
106106

107-
OptionalFileEntryRefDegradesToFileEntryPtr getFile() const {
107+
OptionalFileEntryRef getFile() const {
108108
if (auto *P = Val.getPointer())
109109
return FileEntryRef(*P);
110110
return std::nullopt;

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2260,7 +2260,7 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
22602260
for (ModuleMap::module_iterator I = MMap.module_begin(),
22612261
E = MMap.module_end(); I != E; ++I) {
22622262
Module *TheModule = I->second;
2263-
const FileEntry *Entry = TheModule->getASTFile();
2263+
OptionalFileEntryRef Entry = TheModule->getASTFile();
22642264
if (!Entry) {
22652265
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
22662266
Path.push_back(std::make_pair(

clang/lib/Lex/ModuleMap.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,9 +1067,7 @@ Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir,
10671067
if (!canInfer)
10681068
return nullptr;
10691069
} else {
1070-
OptionalFileEntryRefDegradesToFileEntryPtr ModuleMapRef =
1071-
getModuleMapFileForUniquing(Parent);
1072-
ModuleMapFile = ModuleMapRef;
1070+
ModuleMapFile = getModuleMapFileForUniquing(Parent);
10731071
}
10741072

10751073
// Look for an umbrella header.

clang/lib/Lex/PPDirectives.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,8 @@ Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const {
19341934
// Start looking up in the directory *after* the one in which the current
19351935
// file would be found, if any.
19361936
assert(CurPPLexer && "#include_next directive in macro?");
1937-
LookupFromFile = CurPPLexer->getFileEntry();
1937+
if (auto FE = CurPPLexer->getFileEntry())
1938+
LookupFromFile = *FE;
19381939
Lookup = nullptr;
19391940
} else if (!Lookup) {
19401941
// The current file was not found by walking the include path. Either it

clang/lib/Lex/Pragma.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
548548
return;
549549
}
550550

551-
const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
551+
OptionalFileEntryRef CurFile = getCurrentFileLexer()->getFileEntry();
552552

553553
// If this file is older than the file it depends on, emit a diagnostic.
554554
if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {

clang/lib/Lex/PreprocessorLexer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ void PreprocessorLexer::LexIncludeFilename(Token &FilenameTok) {
4747

4848
/// getFileEntry - Return the FileEntry corresponding to this FileID. Like
4949
/// getFileID(), this only works for lexers with attached preprocessors.
50-
OptionalFileEntryRefDegradesToFileEntryPtr
51-
PreprocessorLexer::getFileEntry() const {
50+
OptionalFileEntryRef PreprocessorLexer::getFileEntry() const {
5251
return PP->getSourceManager().getFileEntryRefForID(getFileID());
5352
}

clang/lib/Serialization/ASTReader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,8 +2531,7 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
25312531
Overridden = false;
25322532
}
25332533

2534-
OptionalFileEntryRefDegradesToFileEntryPtr File = OptionalFileEntryRef(
2535-
expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)));
2534+
auto File = FileMgr.getOptionalFileRef(Filename, /*OpenFile=*/false);
25362535

25372536
// For an overridden file, create a virtual file with the stored
25382537
// size/timestamp.
@@ -2559,7 +2558,8 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
25592558
// PCH.
25602559
SourceManager &SM = getSourceManager();
25612560
// FIXME: Reject if the overrides are different.
2562-
if ((!Overridden && !Transient) && !SkipChecks && SM.isFileOverridden(File)) {
2561+
if ((!Overridden && !Transient) && !SkipChecks &&
2562+
SM.isFileOverridden(*File)) {
25632563
if (Complain)
25642564
Error(diag::err_fe_pch_file_overridden, Filename);
25652565

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,8 +2182,8 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
21822182
"Writing to AST an overridden file is not supported");
21832183

21842184
// The source location entry is a file. Emit input file ID.
2185-
assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2186-
Record.push_back(InputFileIDs[Content->OrigEntry]);
2185+
assert(InputFileIDs[*Content->OrigEntry] != 0 && "Missed file entry");
2186+
Record.push_back(InputFileIDs[*Content->OrigEntry]);
21872187

21882188
Record.push_back(getAdjustedNumCreatedFIDs(FID));
21892189

@@ -4695,7 +4695,7 @@ void ASTWriter::collectNonAffectingInputFiles() {
46954695

46964696
if (!isModuleMap(File.getFileCharacteristic()) ||
46974697
AffectingModuleMaps.empty() ||
4698-
AffectingModuleMaps.find(Cache->OrigEntry) != AffectingModuleMaps.end())
4698+
llvm::is_contained(AffectingModuleMaps, *Cache->OrigEntry))
46994699
continue;
47004700

47014701
IsSLocAffecting[I] = false;

clang/lib/Serialization/ModuleManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {
5252

5353
ModuleFile *ModuleManager::lookupByModuleName(StringRef Name) const {
5454
if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name))
55-
if (const FileEntry *File = Mod->getASTFile())
56-
return lookup(File);
55+
if (OptionalFileEntryRef File = Mod->getASTFile())
56+
return lookup(*File);
5757

5858
return nullptr;
5959
}

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
521521

522522
serialization::ModuleFile *MF =
523523
MDC.ScanInstance.getASTReader()->getModuleManager().lookup(
524-
M->getASTFile());
524+
*M->getASTFile());
525525
MDC.ScanInstance.getASTReader()->visitInputFileInfos(
526526
*MF, /*IncludeSystem=*/true,
527527
[&](const serialization::InputFileInfo &IFI, bool IsSystem) {

clang/tools/libclang/CXIndexDataConsumer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,8 @@ CXIndexDataConsumer::getClientContainerForDC(const DeclContext *DC) const {
10741074
return DC ? ContainerMap.lookup(DC) : nullptr;
10751075
}
10761076

1077-
CXIdxClientFile CXIndexDataConsumer::getIndexFile(const FileEntry *File) {
1078-
return File ? FileMap.lookup(File) : nullptr;
1077+
CXIdxClientFile CXIndexDataConsumer::getIndexFile(OptionalFileEntryRef File) {
1078+
return File ? FileMap.lookup(*File) : nullptr;
10791079
}
10801080

10811081
CXIdxLoc CXIndexDataConsumer::getIndexLoc(SourceLocation Loc) const {
@@ -1104,8 +1104,8 @@ void CXIndexDataConsumer::translateLoc(SourceLocation Loc,
11041104

11051105
if (FID.isInvalid())
11061106
return;
1107-
1108-
OptionalFileEntryRefDegradesToFileEntryPtr FE = SM.getFileEntryRefForID(FID);
1107+
1108+
OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID);
11091109
if (indexFile)
11101110
*indexFile = getIndexFile(FE);
11111111
if (file)

clang/tools/libclang/CXIndexDataConsumer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ class CXIndexDataConsumer : public index::IndexDataConsumer {
460460

461461
const DeclContext *getEntityContainer(const Decl *D) const;
462462

463-
CXIdxClientFile getIndexFile(const FileEntry *File);
464-
463+
CXIdxClientFile getIndexFile(OptionalFileEntryRef File);
464+
465465
CXIdxLoc getIndexLoc(SourceLocation Loc) const;
466466

467467
void getEntityInfo(const NamedDecl *D,

clang/unittests/Basic/FileEntryTest.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,6 @@ TEST(FileEntryTest, FileEntryRef) {
9292
EXPECT_EQ(CE1, &R1.getFileEntry());
9393
}
9494

95-
TEST(FileEntryTest, OptionalFileEntryRefDegradesToFileEntryPtr) {
96-
FileEntryTestHelper Refs;
97-
OptionalFileEntryRefDegradesToFileEntryPtr M0;
98-
OptionalFileEntryRefDegradesToFileEntryPtr M1 = Refs.addFile("1");
99-
OptionalFileEntryRefDegradesToFileEntryPtr M2 = Refs.addFile("2");
100-
OptionalFileEntryRefDegradesToFileEntryPtr M0Also = std::nullopt;
101-
OptionalFileEntryRefDegradesToFileEntryPtr M1Also =
102-
Refs.addFileAlias("1-also", *M1);
103-
104-
EXPECT_EQ(M0, M0Also);
105-
EXPECT_EQ(StringRef("1"), M1->getName());
106-
EXPECT_EQ(StringRef("2"), M2->getName());
107-
EXPECT_EQ(StringRef("1-also"), M1Also->getName());
108-
109-
const FileEntry *CE1 = M1;
110-
EXPECT_EQ(CE1, &M1->getFileEntry());
111-
}
112-
11395
TEST(FileEntryTest, equals) {
11496
FileEntryTestHelper Refs;
11597
FileEntryRef R1 = Refs.addFile("1");
@@ -126,13 +108,6 @@ TEST(FileEntryTest, equals) {
126108
EXPECT_NE(R1, R2);
127109
EXPECT_EQ(R1, R1Redirect);
128110
EXPECT_EQ(R1, R1Redirect2);
129-
130-
OptionalFileEntryRefDegradesToFileEntryPtr M1 = R1;
131-
132-
EXPECT_EQ(M1, &R1.getFileEntry());
133-
EXPECT_EQ(&R1.getFileEntry(), M1);
134-
EXPECT_NE(M1, &R2.getFileEntry());
135-
EXPECT_NE(&R2.getFileEntry(), M1);
136111
}
137112

138113
TEST(FileEntryTest, isSameRef) {

0 commit comments

Comments
 (0)