@@ -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