Skip to content

Commit

Permalink
[clang] NFC: Remove OptionalFileEntryRefDegradesToFileEntryPtr (#74899
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jansvoboda11 committed Dec 9, 2023
1 parent f1e3e8a commit 0cb0a48
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void ExpandModularHeadersPPCallbacks::InclusionDirective(
if (Imported) {
serialization::ModuleFile *MF =
Compiler.getASTReader()->getModuleManager().lookup(
Imported->getASTFile());
*Imported->getASTFile());
handleModuleFile(MF);
}
parseToLocation(DirectiveLoc);
Expand Down
66 changes: 0 additions & 66 deletions clang/include/clang/Basic/FileEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,72 +279,6 @@ template <> struct DenseMapInfo<clang::FileEntryRef> {

namespace clang {

/// Wrapper around OptionalFileEntryRef that degrades to 'const FileEntry*',
/// facilitating incremental patches to propagate FileEntryRef.
///
/// This class can be used as return value or field where it's convenient for
/// an OptionalFileEntryRef to degrade to a 'const FileEntry*'. The purpose
/// is to avoid code churn due to dances like the following:
/// \code
/// // Old code.
/// lvalue = rvalue;
///
/// // Temporary code from an incremental patch.
/// OptionalFileEntryRef MaybeF = rvalue;
/// lvalue = MaybeF ? &MaybeF.getFileEntry() : nullptr;
///
/// // Final code.
/// lvalue = rvalue;
/// \endcode
///
/// FIXME: Once FileEntryRef is "everywhere" and FileEntry::LastRef and
/// FileEntry::getName have been deleted, delete this class and replace
/// instances with OptionalFileEntryRef.
class OptionalFileEntryRefDegradesToFileEntryPtr : public OptionalFileEntryRef {
public:
OptionalFileEntryRefDegradesToFileEntryPtr() = default;
OptionalFileEntryRefDegradesToFileEntryPtr(
OptionalFileEntryRefDegradesToFileEntryPtr &&) = default;
OptionalFileEntryRefDegradesToFileEntryPtr(
const OptionalFileEntryRefDegradesToFileEntryPtr &) = default;
OptionalFileEntryRefDegradesToFileEntryPtr &
operator=(OptionalFileEntryRefDegradesToFileEntryPtr &&) = default;
OptionalFileEntryRefDegradesToFileEntryPtr &
operator=(const OptionalFileEntryRefDegradesToFileEntryPtr &) = default;

OptionalFileEntryRefDegradesToFileEntryPtr(std::nullopt_t) {}
OptionalFileEntryRefDegradesToFileEntryPtr(FileEntryRef Ref)
: OptionalFileEntryRef(Ref) {}
OptionalFileEntryRefDegradesToFileEntryPtr(OptionalFileEntryRef MaybeRef)
: OptionalFileEntryRef(MaybeRef) {}

OptionalFileEntryRefDegradesToFileEntryPtr &operator=(std::nullopt_t) {
OptionalFileEntryRef::operator=(std::nullopt);
return *this;
}
OptionalFileEntryRefDegradesToFileEntryPtr &operator=(FileEntryRef Ref) {
OptionalFileEntryRef::operator=(Ref);
return *this;
}
OptionalFileEntryRefDegradesToFileEntryPtr &
operator=(OptionalFileEntryRef MaybeRef) {
OptionalFileEntryRef::operator=(MaybeRef);
return *this;
}

/// Degrade to 'const FileEntry *' to allow FileEntry::LastRef and
/// FileEntry::getName have been deleted, delete this class and replace
/// instances with OptionalFileEntryRef
operator const FileEntry *() const {
return has_value() ? &(*this)->getFileEntry() : nullptr;
}
};

static_assert(
std::is_trivially_copyable<
OptionalFileEntryRefDegradesToFileEntryPtr>::value,
"OptionalFileEntryRefDegradesToFileEntryPtr should be trivially copyable");

inline bool operator==(const FileEntry *LHS, const OptionalFileEntryRef &RHS) {
return LHS == (RHS ? &RHS->getFileEntry() : nullptr);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ class alignas(8) Module {
}

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

Expand Down
14 changes: 8 additions & 6 deletions clang/include/clang/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class alignas(8) ContentCache {
///
/// FIXME: Make non-optional using a virtual file as needed, remove \c
/// Filename and use \c OrigEntry.getNameAsRequested() instead.
OptionalFileEntryRefDegradesToFileEntryPtr OrigEntry;
OptionalFileEntryRef OrigEntry;

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

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

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

/// Returns the FileEntry record for the provided SLocEntry.
const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
{
return sloc.getFile().getContentCache().OrigEntry;
const FileEntry *
getFileEntryForSLocEntry(const SrcMgr::SLocEntry &SLocEntry) const {
if (auto FE = SLocEntry.getFile().getContentCache().OrigEntry)
return *FE;
return nullptr;
}

/// Return a StringRef to the source buffer data for the
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Lex/PreprocessorLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class PreprocessorLexer {

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

/// Iterator that traverses the current stack of preprocessor
/// conditional directives (\#if/\#ifdef/\#ifndef).
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class InputFile {
return File;
}

OptionalFileEntryRefDegradesToFileEntryPtr getFile() const {
OptionalFileEntryRef getFile() const {
if (auto *P = Val.getPointer())
return FileEntryRef(*P);
return std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2260,7 +2260,7 @@ GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
for (ModuleMap::module_iterator I = MMap.module_begin(),
E = MMap.module_end(); I != E; ++I) {
Module *TheModule = I->second;
const FileEntry *Entry = TheModule->getASTFile();
OptionalFileEntryRef Entry = TheModule->getASTFile();
if (!Entry) {
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Path.push_back(std::make_pair(
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Lex/ModuleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,9 +1067,7 @@ Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir,
if (!canInfer)
return nullptr;
} else {
OptionalFileEntryRefDegradesToFileEntryPtr ModuleMapRef =
getModuleMapFileForUniquing(Parent);
ModuleMapFile = ModuleMapRef;
ModuleMapFile = getModuleMapFileForUniquing(Parent);
}

// Look for an umbrella header.
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,8 @@ Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const {
// Start looking up in the directory *after* the one in which the current
// file would be found, if any.
assert(CurPPLexer && "#include_next directive in macro?");
LookupFromFile = CurPPLexer->getFileEntry();
if (auto FE = CurPPLexer->getFileEntry())
LookupFromFile = *FE;
Lookup = nullptr;
} else if (!Lookup) {
// The current file was not found by walking the include path. Either it
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Lex/Pragma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
return;
}

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

// If this file is older than the file it depends on, emit a diagnostic.
if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Lex/PreprocessorLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void PreprocessorLexer::LexIncludeFilename(Token &FilenameTok) {

/// getFileEntry - Return the FileEntry corresponding to this FileID. Like
/// getFileID(), this only works for lexers with attached preprocessors.
OptionalFileEntryRefDegradesToFileEntryPtr
PreprocessorLexer::getFileEntry() const {
OptionalFileEntryRef PreprocessorLexer::getFileEntry() const {
return PP->getSourceManager().getFileEntryRefForID(getFileID());
}
6 changes: 3 additions & 3 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2531,8 +2531,7 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Overridden = false;
}

OptionalFileEntryRefDegradesToFileEntryPtr File = OptionalFileEntryRef(
expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)));
auto File = FileMgr.getOptionalFileRef(Filename, /*OpenFile=*/false);

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

Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2182,8 +2182,8 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
"Writing to AST an overridden file is not supported");

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

Record.push_back(getAdjustedNumCreatedFIDs(FID));

Expand Down Expand Up @@ -4695,7 +4695,7 @@ void ASTWriter::collectNonAffectingInputFiles() {

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

IsSLocAffecting[I] = false;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Serialization/ModuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {

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

return nullptr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {

serialization::ModuleFile *MF =
MDC.ScanInstance.getASTReader()->getModuleManager().lookup(
M->getASTFile());
*M->getASTFile());
MDC.ScanInstance.getASTReader()->visitInputFileInfos(
*MF, /*IncludeSystem=*/true,
[&](const serialization::InputFileInfo &IFI, bool IsSystem) {
Expand Down
8 changes: 4 additions & 4 deletions clang/tools/libclang/CXIndexDataConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,8 @@ CXIndexDataConsumer::getClientContainerForDC(const DeclContext *DC) const {
return DC ? ContainerMap.lookup(DC) : nullptr;
}

CXIdxClientFile CXIndexDataConsumer::getIndexFile(const FileEntry *File) {
return File ? FileMap.lookup(File) : nullptr;
CXIdxClientFile CXIndexDataConsumer::getIndexFile(OptionalFileEntryRef File) {
return File ? FileMap.lookup(*File) : nullptr;
}

CXIdxLoc CXIndexDataConsumer::getIndexLoc(SourceLocation Loc) const {
Expand Down Expand Up @@ -1104,8 +1104,8 @@ void CXIndexDataConsumer::translateLoc(SourceLocation Loc,

if (FID.isInvalid())
return;
OptionalFileEntryRefDegradesToFileEntryPtr FE = SM.getFileEntryRefForID(FID);

OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID);
if (indexFile)
*indexFile = getIndexFile(FE);
if (file)
Expand Down
4 changes: 2 additions & 2 deletions clang/tools/libclang/CXIndexDataConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ class CXIndexDataConsumer : public index::IndexDataConsumer {

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

CXIdxClientFile getIndexFile(const FileEntry *File);
CXIdxClientFile getIndexFile(OptionalFileEntryRef File);

CXIdxLoc getIndexLoc(SourceLocation Loc) const;

void getEntityInfo(const NamedDecl *D,
Expand Down
25 changes: 0 additions & 25 deletions clang/unittests/Basic/FileEntryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,6 @@ TEST(FileEntryTest, FileEntryRef) {
EXPECT_EQ(CE1, &R1.getFileEntry());
}

TEST(FileEntryTest, OptionalFileEntryRefDegradesToFileEntryPtr) {
FileEntryTestHelper Refs;
OptionalFileEntryRefDegradesToFileEntryPtr M0;
OptionalFileEntryRefDegradesToFileEntryPtr M1 = Refs.addFile("1");
OptionalFileEntryRefDegradesToFileEntryPtr M2 = Refs.addFile("2");
OptionalFileEntryRefDegradesToFileEntryPtr M0Also = std::nullopt;
OptionalFileEntryRefDegradesToFileEntryPtr M1Also =
Refs.addFileAlias("1-also", *M1);

EXPECT_EQ(M0, M0Also);
EXPECT_EQ(StringRef("1"), M1->getName());
EXPECT_EQ(StringRef("2"), M2->getName());
EXPECT_EQ(StringRef("1-also"), M1Also->getName());

const FileEntry *CE1 = M1;
EXPECT_EQ(CE1, &M1->getFileEntry());
}

TEST(FileEntryTest, equals) {
FileEntryTestHelper Refs;
FileEntryRef R1 = Refs.addFile("1");
Expand All @@ -126,13 +108,6 @@ TEST(FileEntryTest, equals) {
EXPECT_NE(R1, R2);
EXPECT_EQ(R1, R1Redirect);
EXPECT_EQ(R1, R1Redirect2);

OptionalFileEntryRefDegradesToFileEntryPtr M1 = R1;

EXPECT_EQ(M1, &R1.getFileEntry());
EXPECT_EQ(&R1.getFileEntry(), M1);
EXPECT_NE(M1, &R2.getFileEntry());
EXPECT_NE(&R2.getFileEntry(), M1);
}

TEST(FileEntryTest, isSameRef) {
Expand Down

0 comments on commit 0cb0a48

Please sign in to comment.