[clang-tools-extra] [clang-include-cleaner] Fix incorrect directory issue for writing files (PR #111375)
Byoungchan Lee via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 7 06:22:16 PDT 2024
https://github.com/bc-lee created https://github.com/llvm/llvm-project/pull/111375
If the current working directory of `clang-include-cleaner` differs from the directory of the input files specified in the compilation database, it doesn't adjust the input file paths properly. As a result, `clang-include-cleaner` either writes files to the wrong directory or fails to write files altogether.
This pull request fixes the issue by checking whether the input file path is absolute. If it isn't, the path is concatenated with the directory of the input file specified in the compilation database.
Fixes #110843.
>From 23b90bba12c010e5882e09e9f6b765a7281324aa Mon Sep 17 00:00:00 2001
From: Byoungchan Lee <byoungchan.lee at gmx.com>
Date: Mon, 7 Oct 2024 22:19:38 +0900
Subject: [PATCH] [clang-include-cleaner] Fix incorrect directory issue for
writing files
If the current working directory of `clang-include-cleaner` differs from
the directory of the input files specified in the compilation database,
it doesn't adjust the input file paths properly. As a result,
`clang-include-cleaner` either writes files to the wrong directory or
fails to write files altogether.
This pull request fixes the issue by checking whether the input file
path is absolute. If it isn't, the path is concatenated with
the directory of the input file specified in the compilation database.
Fixes #110843.
---
.../include-cleaner/tool/IncludeCleaner.cpp | 24 +++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp b/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
index 080099adc9a07a..1cb8b9c4ae99b7 100644
--- a/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
+++ b/clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
@@ -173,11 +173,22 @@ class Action : public clang::ASTFrontendAction {
if (!HTMLReportPath.empty())
writeHTML();
+ // Source File's path relative to compilation database.
llvm::StringRef Path =
SM.getFileEntryRefForID(SM.getMainFileID())->getName();
assert(!Path.empty() && "Main file path not known?");
llvm::StringRef Code = SM.getBufferData(SM.getMainFileID());
+ auto Cwd = getCompilerInstance()
+ .getFileManager()
+ .getVirtualFileSystem()
+ .getCurrentWorkingDirectory();
+ if (Cwd.getError()) {
+ llvm::errs() << "Failed to get current working directory: "
+ << Cwd.getError().message() << "\n";
+ return;
+ }
+
auto Results =
analyze(AST.Roots, PP.MacroReferences, PP.Includes, &PI,
getCompilerInstance().getPreprocessor(), HeaderFilter);
@@ -201,8 +212,17 @@ class Action : public clang::ASTFrontendAction {
}
}
- if (!Results.Missing.empty() || !Results.Unused.empty())
- EditedFiles.try_emplace(Path, Final);
+ if (!Results.Missing.empty() || !Results.Unused.empty()) {
+ auto Sept = llvm::sys::path::get_separator();
+ // Adjust the path to be absolute if it is not.
+ std::string FullPath;
+ if (llvm::sys::path::is_absolute(Path))
+ FullPath = Path.str();
+ else
+ FullPath = Cwd.get() + Sept.str() + Path.str();
+
+ EditedFiles.try_emplace(FullPath, Final);
+ }
}
void writeHTML() {
More information about the cfe-commits
mailing list