Skip to content
24 changes: 22 additions & 2 deletions clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,22 @@ class Action : public clang::ASTFrontendAction {
if (!HTMLReportPath.empty())
writeHTML();

// Source File's path relative to compilation database.
llvm::StringRef Path =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep reporting with abs-paths here, i.e:

llvm::SmallString<256> AbsPath(SM.getFileEntryRefForID(SM.getMainFileID())->getName());
SM.getFileManager().makeAbsolutePath(AbsPath);
...

This way we can recognize these paths in main and perform the mapping accordingly.

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);
Expand All @@ -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() {
Expand Down
Loading