Skip to content

Commit 6de1c25

Browse files
authored
[clang] Don't require FileManager for creating an output file (#164665)
Conceptually, the `CompilerInstance` doesn't need an instance of the `FileManager` to create an output file. This PR enables that, removing an edge-case in `cc1_main()`.
1 parent 224f18e commit 6de1c25

File tree

4 files changed

+11
-17
lines changed

4 files changed

+11
-17
lines changed

clang/include/clang/Basic/FileManager.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,12 @@ class FileManager : public RefCountedBase<FileManager> {
287287
/// If path is not absolute and FileSystemOptions set the working
288288
/// directory, the path is modified to be relative to the given
289289
/// working directory.
290-
/// \returns true if \c path changed.
291-
bool FixupRelativePath(SmallVectorImpl<char> &path) const;
290+
/// \returns true if \c Path changed.
291+
bool FixupRelativePath(SmallVectorImpl<char> &Path) const {
292+
return fixupRelativePath(FileSystemOpts, Path);
293+
}
294+
static bool fixupRelativePath(const FileSystemOptions &FileSystemOpts,
295+
SmallVectorImpl<char> &Path);
292296

293297
/// Makes \c Path absolute taking into account FileSystemOptions and the
294298
/// working directory option.

clang/lib/Basic/FileManager.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,16 +474,17 @@ OptionalFileEntryRef FileManager::getBypassFile(FileEntryRef VF) {
474474
return FileEntryRef(*Insertion.first);
475475
}
476476

477-
bool FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
478-
StringRef pathRef(path.data(), path.size());
477+
bool FileManager::fixupRelativePath(const FileSystemOptions &FileSystemOpts,
478+
SmallVectorImpl<char> &Path) {
479+
StringRef pathRef(Path.data(), Path.size());
479480

480481
if (FileSystemOpts.WorkingDir.empty()
481482
|| llvm::sys::path::is_absolute(pathRef))
482483
return false;
483484

484485
SmallString<128> NewPath(FileSystemOpts.WorkingDir);
485486
llvm::sys::path::append(NewPath, pathRef);
486-
path = NewPath;
487+
Path = NewPath;
487488
return true;
488489
}
489490

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,
882882
"File Manager is required to fix up relative path.\n");
883883

884884
AbsPath.emplace(OutputPath);
885-
FileMgr->FixupRelativePath(*AbsPath);
885+
FileManager::fixupRelativePath(getFileSystemOpts(), *AbsPath);
886886
OutputPath = *AbsPath;
887887
}
888888

clang/tools/driver/cc1_main.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,6 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
313313
llvm::TimerGroup::clearAll();
314314

315315
if (llvm::timeTraceProfilerEnabled()) {
316-
// It is possible that the compiler instance doesn't own a file manager here
317-
// if we're compiling a module unit. Since the file manager are owned by AST
318-
// when we're compiling a module unit. So the file manager may be invalid
319-
// here.
320-
//
321-
// It should be fine to create file manager here since the file system
322-
// options are stored in the compiler invocation and we can recreate the VFS
323-
// from the compiler invocation.
324-
if (!Clang->hasFileManager())
325-
Clang->createFileManager();
326-
327316
if (auto profilerOutput = Clang->createOutputFile(
328317
Clang->getFrontendOpts().TimeTracePath, /*Binary=*/false,
329318
/*RemoveFileOnSignal=*/false,

0 commit comments

Comments
 (0)