diff options
author | Jan Svoboda <jan_svoboda@apple.com> | 2023-12-05 15:35:55 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-05 15:35:55 -0800 |
commit | 07157db81d4421ced9fcf9a2002255c2a3a80d49 (patch) | |
tree | b0c9559979cbe4e811636c46a32c6e02a838670e /clang/lib/Rewrite/Rewriter.cpp | |
parent | 9f87509b1902da2e98254f32bd32cc10eb88bb9e (diff) | |
download | llvm-07157db81d4421ced9fcf9a2002255c2a3a80d49.zip llvm-07157db81d4421ced9fcf9a2002255c2a3a80d49.tar.gz llvm-07157db81d4421ced9fcf9a2002255c2a3a80d49.tar.bz2 |
[clang][tidy] Ensure rewriter has the correct CWD (#67839)
This patch replaces use of the deprecated `FileEntry::getName()` with
`FileEntryRef::getName()`. This means the code now uses the path that
was used to register file entry in `SourceManager` instead of the
absolute 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 some paths can be
relative and thus rely on the VFS CWD. The CWD can change for each TU,
so when we run `clang-tidy` on a compilation database and try to perform
all the replacements at the end, relative paths won't resolve the same.
This patch takes care to reinstate the correct CWD and make the path
reported by `FileEntryRef` absolute before passing it to
`llvm::writeToOutput()`.
Diffstat (limited to 'clang/lib/Rewrite/Rewriter.cpp')
-rw-r--r-- | clang/lib/Rewrite/Rewriter.cpp | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp index ef28589..0896221 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; |