[clang-tools-extra] [clang-include-cleaner] Fix incorrect directory issue for writing files (PR #111375)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 9 07:25:59 PDT 2024


================
@@ -305,7 +342,32 @@ int main(int argc, const char **argv) {
     }
   }
 
-  clang::tooling::ClangTool Tool(OptionsParser->getCompilations(),
+  auto &CompilationDatabase = OptionsParser->getCompilations();
----------------
kadircet wrote:

i think this can be simplified with something like:
```
auto VFS = llvm::vfs::getRealFileSystem();
auto &CDB = OptionsParser->getCompilations();
std::map<std::string, std::string> CDBToAbsPaths;
for (auto &Source : OptionsParser->getSourcePathList()) {
  llvm::SmallString<256> AbsPath(Source);
  if (auto Err = VFS.makeAbsolute(AbsPath)) {
      llvm::errs() << "Failed to get absolute path for " << Source << " : " <<  Err.message() << '\n';
      return 1;
  }
  for(auto Cmd : CDB.getCompilecommands(AbsPath)) {
    llvm::SmallString<256> CDBPath(Cmd.Filename);
    llvm::sys::fs::make_absolute(Cmd.Directory, CDBPath);
    CDBToAbsPaths[CDBPath] = AbsPath;
  }
}
....
if (Edit) {
  for (const auto &NameAndContent : Factory.editedFiles()) {
     llvm::StringRef FileName = NameAndContent.first();
     if (auto It = CDBToAbsPaths.find(FileName); It != CDBToAbsPaths.end())
         FileName = It->second;
     ...
  }
}
```

https://github.com/llvm/llvm-project/pull/111375


More information about the cfe-commits mailing list