Skip to content

Commit

Permalink
[clang] NFCI: Use FileEntryRef for FileID creation (#67838)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jansvoboda11 committed Oct 3, 2023
1 parent c6fed74 commit 27254ae
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ 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 ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
auto Entry = llvm::cantFail(FileMgr.getFileRef(*I));
auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
std::string Content;
llvm::raw_string_ostream ContentStream(Content);
Rewrite.getEditBuffer(ID).write(ContentStream);
Expand All @@ -170,9 +170,9 @@ int main(int argc, const char **argv) {
}

for (const auto &File : ChangedFiles) {
const auto Entry = FileMgr.getFile(File);
auto Entry = llvm::cantFail(FileMgr.getFileRef(File));

auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
outs() << "============== " << File << " ==============\n";
Rewrite.getEditBuffer(ID).write(llvm::outs());
outs() << "\n============================================\n";
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-move/Move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ int main(int argc, const char **argv) {
Tool.applyAllReplacements(Rewrite);

for (const auto &File : Files) {
auto Entry = FileMgr.getFile(File);
const auto ID = Sources.getOrCreateFileID(*Entry, SrcMgr::C_User);
auto Entry = llvm::cantFail(FileMgr.getFileRef(File));
const auto ID = Sources.getOrCreateFileID(Entry, SrcMgr::C_User);
Rewrite.getEditBuffer(ID).write(outs());
}

Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};

Expand Down
6 changes: 3 additions & 3 deletions clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ 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);
FileID ID = SM.getOrCreateFileID(*File, SrcMgr::C_User);
FileManager &FM = SM.getFileManager();
FileEntryRef File = llvm::cantFail(FM.getFileRef(Error.Message.FilePath));
FileID ID = SM.getOrCreateFileID(File, SrcMgr::C_User);
SourceLocation FileStartLoc = SM.getLocForStartOfFile(ID);
SourceLocation Loc = FileStartLoc.getLocWithOffset(
static_cast<SourceLocation::IntTy>(Error.Message.FileOffset));
Expand Down
9 changes: 1 addition & 8 deletions clang/include/clang/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -877,13 +877,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,
Expand All @@ -909,7 +902,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
Expand Down
18 changes: 4 additions & 14 deletions clang/lib/Basic/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -2375,8 +2364,9 @@ SourceManagerForFile::SourceManagerForFile(StringRef FileName,
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
new DiagnosticOptions);
SourceMgr = std::make_unique<SourceManager>(*Diagnostics, *FileMgr);
FileID ID = SourceMgr->createFileID(*FileMgr->getFile(FileName),
SourceLocation(), clang::SrcMgr::C_User);
FileEntryRef FE = llvm::cantFail(FileMgr->getFileRef(FileName));
FileID ID =
SourceMgr->createFileID(FE, SourceLocation(), clang::SrcMgr::C_User);
assert(ID.isValid());
SourceMgr->setMainFileID(ID);
}
2 changes: 1 addition & 1 deletion clang/lib/Tooling/Core/Replacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 1 addition & 4 deletions clang/lib/Tooling/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ bool formatAndApplyAllReplacements(
const std::string &FilePath = FileAndReplaces.first;
auto &CurReplaces = FileAndReplaces.second;

const FileEntry *Entry = nullptr;
if (auto File = Files.getFile(FilePath))
Entry = *File;

FileEntryRef Entry = llvm::cantFail(Files.getFileRef(FilePath));
FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
StringRef Code = SM.getBufferData(ID);

Expand Down
2 changes: 1 addition & 1 deletion clang/tools/clang-rename/ClangRename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Analysis/UnsafeBufferUsageTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 27254ae

Please sign in to comment.