diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -814,7 +814,7 @@ /// Returns true when the given FileEntry corresponds to the main file. /// /// The main file should be set prior to calling this function. - bool isMainFile(FileEntryRef SourceFile); + bool isMainFile(const FileEntry &SourceFile); /// Set the file ID for the precompiled preamble. void setPreambleFileID(FileID Preamble) { diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -345,12 +345,11 @@ createExpansionLoc(SourceLocation(), SourceLocation(), SourceLocation(), 1); } -bool SourceManager::isMainFile(FileEntryRef SourceFile) { +bool SourceManager::isMainFile(const FileEntry &SourceFile) { assert(MainFileID.isValid() && "expected initialized SourceManager"); - auto FE = getFileEntryRefForID(MainFileID); - if (!FE) - return false; - return FE->getUID() == SourceFile.getUID(); + if (auto *FE = getFileEntryForID(MainFileID)) + return FE->getUID() == SourceFile.getUID(); + return false; } void SourceManager::initializeForReplay(const SourceManager &Old) { diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -2061,7 +2061,7 @@ // some directives (e.g. #endif of a header guard) will never be seen. // Since this will lead to confusing errors, avoid the inclusion. if (Action == Enter && File && PreambleConditionalStack.isRecording() && - SourceMgr.isMainFile(*File)) { + SourceMgr.isMainFile(File->getFileEntry())) { Diag(FilenameTok.getLocation(), diag::err_pp_including_mainfile_in_preamble); return {ImportAction::None}; diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp --- a/clang/unittests/Basic/SourceManagerTest.cpp +++ b/clang/unittests/Basic/SourceManagerTest.cpp @@ -509,10 +509,9 @@ FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User); SourceMgr.setMainFileID(MainFileID); - EXPECT_TRUE(SourceMgr.isMainFile(FileEntryRef("mainFile.cpp", *SourceFile))); - EXPECT_TRUE( - SourceMgr.isMainFile(FileEntryRef("anotherName.cpp", *SourceFile))); - EXPECT_FALSE(SourceMgr.isMainFile(FileEntryRef("mainFile.cpp", *SecondFile))); + EXPECT_TRUE(SourceMgr.isMainFile(*SourceFile)); + EXPECT_TRUE(SourceMgr.isMainFile(*SourceFile)); + EXPECT_FALSE(SourceMgr.isMainFile(*SecondFile)); } #endif