Skip to content

Commit c65d828

Browse files
committed
Apply review comments.
- Removed the check for Edit mode. - Removed unnecessary fallback. - Various cleanups. - Make the test work on Windows as well.
1 parent 3a0f02e commit c65d828

File tree

2 files changed

+31
-72
lines changed

2 files changed

+31
-72
lines changed

clang-tools-extra/include-cleaner/test/tool.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,11 @@ int x = foo();
4949
// RUN: FileCheck --match-full-lines --check-prefix=EDIT2 %s < %t.cpp
5050
// EDIT2-NOT: {{^}}#include "foo.h"{{$}}
5151

52-
// This test has commands that rely on shell capabilities that won't execute
53-
// correctly on Windows e.g. subshell execution
54-
// REQUIRES: shell
55-
// RUN: mkdir -p $(dirname %t)/out
52+
// RUN: rm -rf %t.dir && mkdir -p %t.dir
5653
// RUN: cp %s %t.cpp
57-
// RUN: echo "[{\"directory\":\"$(dirname %t)/out\",\"file\":\"../$(basename %t).cpp\",\"command\":\":clang++ -I%S/Inputs/ ../$(basename %t).cpp\"}]" > $(dirname %t)/out/compile_commands.json
58-
// RUN: pushd $(dirname %t)
59-
// RUN: clang-include-cleaner -p out -edit $(basename %t).cpp
54+
// RUN: echo "[{\"directory\":\"%t.dir\",\"file\":\"../%{t:stem}.tmp.cpp\",\"command\":\":clang++ -I%S/Inputs/ ../%{t:stem}.tmp.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t.dir/compile_commands.json
55+
// RUN: pushd %t.dir
56+
// RUN: clang-include-cleaner -p %{t:stem}.tmp.dir -edit ../%{t:stem}.tmp.cpp
6057
// RUN: popd
6158
// RUN: FileCheck --match-full-lines --check-prefix=EDIT3 %s < %t.cpp
6259
// EDIT3: #include "foo.h"

clang-tools-extra/include-cleaner/tool/IncludeCleaner.cpp

Lines changed: 27 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -312,75 +312,37 @@ int main(int argc, const char **argv) {
312312
// CDBToAbsPaths is a map from the path in the compilation database to the
313313
// writable absolute path of the file.
314314
std::map<std::string, std::string> CDBToAbsPaths;
315-
if (Edit) {
316-
// if Edit is enabled, `Factory.editedFiles()` will contain the final code,
317-
// along with the path given in the compilation database. That path can be
318-
// absolute or relative, and if it is relative, it is relative to the
319-
// "Directory" field in the compilation database. We need to make it
320-
// absolute to write the final code to the correct path.
321-
// There are several cases to consider:
322-
// 1. The "Directory" field isn't same as the current working directory.
323-
// 2. The file path resolved from the "Directory" field is not writable.
324-
// For these cases, we need to find a writable path for the file.
325-
// To effectively handle these cases, we only need to consider
326-
// the files from `getSourcePathList()` that are present in the compilation
327-
// database.
328-
for (auto &Source : OptionsParser->getSourcePathList()) {
329-
llvm::SmallString<256> AbsPath(Source);
330-
if (auto Err = VFS->makeAbsolute(AbsPath)) {
331-
llvm::errs() << "Failed to get absolute path for " << Source << " : "
332-
<< Err.message() << '\n';
333-
return 1;
334-
}
335-
std::vector<clang::tooling::CompileCommand> Cmds =
336-
CDB.getCompileCommands(AbsPath);
315+
// if Edit is enabled, `Factory.editedFiles()` will contain the final code,
316+
// along with the path given in the compilation database. That path can be
317+
// absolute or relative, and if it is relative, it is relative to the
318+
// "Directory" field in the compilation database. We need to make it
319+
// absolute to write the final code to the correct path.
320+
for (auto &Source : OptionsParser->getSourcePathList()) {
321+
llvm::SmallString<256> AbsPath(Source);
322+
if (auto Err = VFS->makeAbsolute(AbsPath)) {
323+
llvm::errs() << "Failed to get absolute path for " << Source << " : "
324+
<< Err.message() << '\n';
325+
return 1;
326+
}
327+
std::vector<clang::tooling::CompileCommand> Cmds =
328+
CDB.getCompileCommands(AbsPath);
329+
if (Cmds.empty()) {
330+
// Try with the original path.
331+
Cmds = CDB.getCompileCommands(Source);
337332
if (Cmds.empty()) {
338-
// Try with the original path.
339-
Cmds = CDB.getCompileCommands(Source);
340-
if (Cmds.empty()) {
341-
continue;
342-
}
333+
// It should be found in the compilation database, even user didn't
334+
// specify the compilation database, the `FixedCompilationDatabase` will
335+
// create an entry from the arguments. So it is an error if we can't
336+
// find the compile commands.
337+
llvm::errs() << "No compile commands found for " << Source << '\n';
338+
return 1;
343339
}
344-
// We only need the first command to get the directory.
345-
auto Cmd = Cmds[0];
340+
}
341+
for (const auto &Cmd : Cmds) {
346342
llvm::SmallString<256> CDBPath(Cmd.Filename);
347343
std::string Directory(Cmd.Directory);
348-
349-
if (llvm::sys::path::is_absolute(CDBPath)) {
350-
// If the path in the compilation database is already absolute, we don't
351-
// need to do anything.
352-
CDBToAbsPaths[static_cast<std::string>(CDBPath)] =
353-
static_cast<std::string>(AbsPath);
354-
} else {
355-
auto Sept = llvm::sys::path::get_separator();
356-
// First, try to find the file based on the compilation database.
357-
llvm::Twine FilePathTwine = Directory + Sept + CDBPath;
358-
llvm::SmallString<256> FilePath;
359-
FilePathTwine.toVector(FilePath);
360-
// Check if it is writable.
361-
if (llvm::sys::fs::access(FilePath, llvm::sys::fs::AccessMode::Write) !=
362-
std::error_code()) {
363-
// If not, try to find the file based on the current working
364-
// directory, as some Bazel invocations may not set the compilation
365-
// invocation's filesystem as non-writable. In such cases, we can
366-
// find the file based on the current working directory.
367-
FilePath = Source;
368-
if (auto EC = VFS->makeAbsolute(CDBPath)) {
369-
llvm::errs() << "Failed to get absolute path for " << CDBPath
370-
<< " : " << EC.message() << '\n';
371-
return 1;
372-
}
373-
if (llvm::sys::fs::access(FilePath,
374-
llvm::sys::fs::AccessMode::Write) !=
375-
std::error_code()) {
376-
llvm::errs() << "Failed to find a writable path for " << Source
377-
<< '\n';
378-
return 1;
379-
}
380-
}
381-
CDBToAbsPaths[static_cast<std::string>(CDBPath)] =
382-
static_cast<std::string>(FilePath);
383-
}
344+
llvm::sys::fs::make_absolute(Cmd.Directory, CDBPath);
345+
CDBToAbsPaths[std::string(CDBPath)] = std::string(AbsPath);
384346
}
385347
}
386348

0 commit comments

Comments
 (0)