Index: clang/include/clang/Basic/SourceManager.h =================================================================== --- clang/include/clang/Basic/SourceManager.h +++ 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) { Index: clang/lib/Basic/SourceManager.cpp =================================================================== --- clang/lib/Basic/SourceManager.cpp +++ clang/lib/Basic/SourceManager.cpp @@ -343,12 +343,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) { Index: clang/lib/Lex/PPDirectives.cpp =================================================================== --- clang/lib/Lex/PPDirectives.cpp +++ 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}; Index: clang/unittests/Basic/SourceManagerTest.cpp =================================================================== --- clang/unittests/Basic/SourceManagerTest.cpp +++ 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