https://github.com/jansvoboda11 created https://github.com/llvm/llvm-project/pull/67838
This patch removes the `SourceManager` APIs that create `FileID` from a `const FileEntry *` in favor of APIs that take `FileEntryRef`. This also removes a misleading documentation that claims `nullptr` file entry represents stdin. I don't think that's right, since we just try to dereference that pointer anyways. >From 87a590f89f346113fce04ad6e75908a896b3894b Mon Sep 17 00:00:00 2001 From: Jan Svoboda <jan_svob...@apple.com> Date: Tue, 12 Sep 2023 15:02:23 -0700 Subject: [PATCH] [clang] NFCI: Use `FileEntryRef` for `FileID` creation in `SourceManager` --- .../tool/ClangChangeNamespace.cpp | 4 ++-- clang-tools-extra/clang-move/Move.cpp | 2 +- .../tool/ClangReorderFields.cpp | 2 +- clang-tools-extra/clang-tidy/ClangTidy.cpp | 2 +- .../clang-tidy/ClangTidyDiagnosticConsumer.cpp | 4 ++-- clang/include/clang/Basic/SourceManager.h | 9 +-------- clang/lib/Basic/SourceManager.cpp | 15 ++------------- clang/lib/Tooling/Core/Replacement.cpp | 2 +- clang/lib/Tooling/Refactoring.cpp | 7 ++----- clang/tools/clang-rename/ClangRename.cpp | 2 +- .../unittests/Analysis/UnsafeBufferUsageTest.cpp | 2 +- 11 files changed, 15 insertions(+), 36 deletions(-) diff --git a/clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp b/clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp index 5f30cbf8fb4773e..cdff4f452205ed4 100644 --- a/clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp +++ b/clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp @@ -152,7 +152,7 @@ int main(int argc, const char **argv) { for (auto I = ChangedFiles.begin(), E = ChangedFiles.end(); I != E; ++I) { OS << " {\n"; OS << " \"FilePath\": \"" << *I << "\",\n"; - const auto Entry = FileMgr.getFile(*I); + auto Entry = FileMgr.getOptionalFileRef(*I); auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User); std::string Content; llvm::raw_string_ostream ContentStream(Content); @@ -170,7 +170,7 @@ int main(int argc, const char **argv) { } for (const auto &File : ChangedFiles) { - const auto Entry = FileMgr.getFile(File); + auto Entry = FileMgr.getOptionalFileRef(File); auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User); outs() << "============== " << File << " ==============\n"; diff --git a/clang-tools-extra/clang-move/Move.cpp b/clang-tools-extra/clang-move/Move.cpp index 94ba73ce9ad5a15..404acf55e3aa53f 100644 --- a/clang-tools-extra/clang-move/Move.cpp +++ b/clang-tools-extra/clang-move/Move.cpp @@ -843,7 +843,7 @@ void ClangMoveTool::moveDeclsToNewFiles() { // Move all contents from OldFile to NewFile. void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile, StringRef NewFile) { - auto FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile)); + auto FE = SM.getFileManager().getOptionalFileRef(makeAbsolutePath(OldFile)); if (!FE) { llvm::errs() << "Failed to get file: " << OldFile << "\n"; return; diff --git a/clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp b/clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp index 375306765a52b6c..9d5536d27995fe7 100644 --- a/clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp +++ b/clang-tools-extra/clang-reorder-fields/tool/ClangReorderFields.cpp @@ -84,7 +84,7 @@ int main(int argc, const char **argv) { Tool.applyAllReplacements(Rewrite); for (const auto &File : Files) { - auto Entry = FileMgr.getFile(File); + auto Entry = FileMgr.getOptionalFileRef(File); const auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User); Rewrite.getEditBuffer(ID).write(outs()); } diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index 695bfd6e2edf7ef..4b1a67b6dd98a94 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -243,7 +243,7 @@ class ErrorReporter { if (FilePath.empty()) return {}; - auto File = SourceMgr.getFileManager().getFile(FilePath); + auto File = SourceMgr.getFileManager().getOptionalFileRef(FilePath); if (!File) return {}; diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp index c1e4437b88949b9..8d7c1b088f037a9 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -194,8 +194,8 @@ DiagnosticBuilder ClangTidyContext::diag( DiagnosticBuilder ClangTidyContext::diag(const tooling::Diagnostic &Error) { SourceManager &SM = DiagEngine->getSourceManager(); - llvm::ErrorOr<const FileEntry *> File = - SM.getFileManager().getFile(Error.Message.FilePath); + OptionalFileEntryRef File = + SM.getFileManager().getOptionalFileRef(Error.Message.FilePath); FileID ID = SM.getOrCreateFileID(*File, SrcMgr::C_User); SourceLocation FileStartLoc = SM.getLocForStartOfFile(ID); SourceLocation Loc = FileStartLoc.getLocWithOffset( diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index 4abb9a19622871b..62d0435755b083e 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -876,13 +876,6 @@ class SourceManager : public RefCountedBase<SourceManager> { /// Create a new FileID that represents the specified file /// being \#included from the specified IncludePosition. - /// - /// This translates NULL into standard input. - FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, - SrcMgr::CharacteristicKind FileCharacter, - int LoadedID = 0, - SourceLocation::UIntTy LoadedOffset = 0); - FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID = 0, @@ -908,7 +901,7 @@ class SourceManager : public RefCountedBase<SourceManager> { /// Get the FileID for \p SourceFile if it exists. Otherwise, create a /// new FileID for the \p SourceFile. - FileID getOrCreateFileID(const FileEntry *SourceFile, + FileID getOrCreateFileID(FileEntryRef SourceFile, SrcMgr::CharacteristicKind FileCharacter); /// Creates an expansion SLocEntry for the substitution of an argument into a diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index f1a81de329319a0..07197e19098ef39 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -527,17 +527,6 @@ FileID SourceManager::getNextFileID(FileID FID) const { /// Create a new FileID that represents the specified file /// being \#included from the specified IncludePosition. -/// -/// This translates NULL into standard input. -FileID SourceManager::createFileID(const FileEntry *SourceFile, - SourceLocation IncludePos, - SrcMgr::CharacteristicKind FileCharacter, - int LoadedID, - SourceLocation::UIntTy LoadedOffset) { - return createFileID(SourceFile->getLastRef(), IncludePos, FileCharacter, - LoadedID, LoadedOffset); -} - FileID SourceManager::createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, @@ -585,7 +574,7 @@ FileID SourceManager::createFileID(const llvm::MemoryBufferRef &Buffer, /// Get the FileID for \p SourceFile if it exists. Otherwise, create a /// new FileID for the \p SourceFile. FileID -SourceManager::getOrCreateFileID(const FileEntry *SourceFile, +SourceManager::getOrCreateFileID(FileEntryRef SourceFile, SrcMgr::CharacteristicKind FileCharacter) { FileID ID = translateFile(SourceFile); return ID.isValid() ? ID : createFileID(SourceFile, SourceLocation(), @@ -2375,7 +2364,7 @@ SourceManagerForFile::SourceManagerForFile(StringRef FileName, IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), new DiagnosticOptions); SourceMgr = std::make_unique<SourceManager>(*Diagnostics, *FileMgr); - FileID ID = SourceMgr->createFileID(*FileMgr->getFile(FileName), + FileID ID = SourceMgr->createFileID(*FileMgr->getOptionalFileRef(FileName), SourceLocation(), clang::SrcMgr::C_User); assert(ID.isValid()); SourceMgr->setMainFileID(ID); diff --git a/clang/lib/Tooling/Core/Replacement.cpp b/clang/lib/Tooling/Core/Replacement.cpp index 2c472df086d5ec5..269f17a6db4cfc1 100644 --- a/clang/lib/Tooling/Core/Replacement.cpp +++ b/clang/lib/Tooling/Core/Replacement.cpp @@ -67,7 +67,7 @@ bool Replacement::isApplicable() const { bool Replacement::apply(Rewriter &Rewrite) const { SourceManager &SM = Rewrite.getSourceMgr(); - auto Entry = SM.getFileManager().getFile(FilePath); + auto Entry = SM.getFileManager().getOptionalFileRef(FilePath); if (!Entry) return false; diff --git a/clang/lib/Tooling/Refactoring.cpp b/clang/lib/Tooling/Refactoring.cpp index d45cd8c57f10e92..e3ae14f58f9f9eb 100644 --- a/clang/lib/Tooling/Refactoring.cpp +++ b/clang/lib/Tooling/Refactoring.cpp @@ -78,11 +78,8 @@ bool formatAndApplyAllReplacements( const std::string &FilePath = FileAndReplaces.first; auto &CurReplaces = FileAndReplaces.second; - const FileEntry *Entry = nullptr; - if (auto File = Files.getFile(FilePath)) - Entry = *File; - - FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User); + OptionalFileEntryRef Entry = Files.getOptionalFileRef(FilePath); + FileID ID = SM.getOrCreateFileID(*Entry, SrcMgr::C_User); StringRef Code = SM.getBufferData(ID); auto CurStyle = format::getStyle(Style, FilePath, "LLVM"); diff --git a/clang/tools/clang-rename/ClangRename.cpp b/clang/tools/clang-rename/ClangRename.cpp index 24c9d8521dc270f..f2ac0c4360e0dc5 100644 --- a/clang/tools/clang-rename/ClangRename.cpp +++ b/clang/tools/clang-rename/ClangRename.cpp @@ -228,7 +228,7 @@ int main(int argc, const char **argv) { Tool.applyAllReplacements(Rewrite); for (const auto &File : Files) { - auto Entry = FileMgr.getFile(File); + auto Entry = FileMgr.getOptionalFileRef(File); if (!Entry) { errs() << "clang-rename: " << File << " does not exist.\n"; return 1; diff --git a/clang/unittests/Analysis/UnsafeBufferUsageTest.cpp b/clang/unittests/Analysis/UnsafeBufferUsageTest.cpp index 66af5750ef2424b..e48f39bf13f449e 100644 --- a/clang/unittests/Analysis/UnsafeBufferUsageTest.cpp +++ b/clang/unittests/Analysis/UnsafeBufferUsageTest.cpp @@ -25,7 +25,7 @@ class UnsafeBufferUsageTest : public ::testing::Test { } // namespace TEST_F(UnsafeBufferUsageTest, FixItHintsConflict) { - const FileEntry *DummyFile = FileMgr.getVirtualFile("<virtual>", 100, 0); + FileEntryRef DummyFile = FileMgr.getVirtualFileRef("<virtual>", 100, 0); FileID DummyFileID = SourceMgr.getOrCreateFileID(DummyFile, SrcMgr::C_User); SourceLocation StartLoc = SourceMgr.getLocForStartOfFile(DummyFileID); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits