[clang-tools-extra] [clang][tidy] Ensure rewriter has the correct CWD (PR #67839)
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 29 10:51:14 PDT 2023
https://github.com/jansvoboda11 created https://github.com/llvm/llvm-project/pull/67839
This patch replaces use of the deprecated `FileEntry::getName()` with `FileEntryRef::getName()`. This code now uses the path that was used to register file entry in `SourceManager` instead of the path that happened to be used in the last call to `FileManager::getFile()` some place else.
This caused some test failures due to the fact that the `FileManager`'s CWD does not match what the real file system considers to be a CWD. This patch takes care to use the `FileManager` to make the file path absolute before passing it off to the real FS.
>From 3d02a94059a784143895686cf6366fae0cfe3842 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Fri, 8 Sep 2023 16:39:10 -0700
Subject: [PATCH] [clang][tidy] Ensure rewriter has the correct CWD
This reverts commit 65331da0032ab4253a4bc0ddcb2da67664bd86a9.
---
clang-tools-extra/clang-tidy/ClangTidy.cpp | 8 ++++++++
clang/lib/Rewrite/Rewriter.cpp | 13 +++++++------
2 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp
index 695bfd6e2edf7ef..426bb94f835a0de 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -227,6 +227,14 @@ class ErrorReporter {
llvm::errs() << "Can't apply replacements for file " << File << "\n";
}
}
+
+ auto BuildDir = Context.getCurrentBuildDirectory();
+ if (!BuildDir.empty())
+ Rewrite.getSourceMgr()
+ .getFileManager()
+ .getVirtualFileSystem()
+ .setCurrentWorkingDirectory(BuildDir);
+
if (Rewrite.overwriteChangedFiles()) {
llvm::errs() << "clang-tidy failed to apply suggested fixes.\n";
} else {
diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp
index ef2858990dd954e..0896221dd0bdeb5 100644
--- a/clang/lib/Rewrite/Rewriter.cpp
+++ b/clang/lib/Rewrite/Rewriter.cpp
@@ -412,12 +412,13 @@ bool Rewriter::overwriteChangedFiles() {
unsigned OverwriteFailure = Diag.getCustomDiagID(
DiagnosticsEngine::Error, "unable to overwrite file %0: %1");
for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
- const FileEntry *Entry = getSourceMgr().getFileEntryForID(I->first);
- if (auto Error =
- llvm::writeToOutput(Entry->getName(), [&](llvm::raw_ostream &OS) {
- I->second.write(OS);
- return llvm::Error::success();
- })) {
+ OptionalFileEntryRef Entry = getSourceMgr().getFileEntryRefForID(I->first);
+ llvm::SmallString<128> Path(Entry->getName());
+ getSourceMgr().getFileManager().makeAbsolutePath(Path);
+ if (auto Error = llvm::writeToOutput(Path, [&](llvm::raw_ostream &OS) {
+ I->second.write(OS);
+ return llvm::Error::success();
+ })) {
Diag.Report(OverwriteFailure)
<< Entry->getName() << llvm::toString(std::move(Error));
AllWritten = false;
More information about the cfe-commits
mailing list