From 31c181b06172be253f3411be8c9cb048dfc71d36 Mon Sep 17 00:00:00 2001 From: "Ariel J. Bernal" Date: Tue, 1 Oct 2013 14:59:00 +0000 Subject: [PATCH] Fixed replacements for files with relative paths are not applied. Replacements were no applied when using a compilation database with paths in the compilation command relative to the compile directory. This patch makes those paths abosulte. llvm-svn: 191776 --- clang/lib/Tooling/Refactoring.cpp | 13 ++++++++++++- clang/test/Tooling/clang-check-rel-path.cpp | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 clang/test/Tooling/clang-check-rel-path.cpp diff --git a/clang/lib/Tooling/Refactoring.cpp b/clang/lib/Tooling/Refactoring.cpp index b03fb50b383e..13811648eef2 100644 --- a/clang/lib/Tooling/Refactoring.cpp +++ b/clang/lib/Tooling/Refactoring.cpp @@ -19,6 +19,8 @@ #include "clang/Rewrite/Core/Rewriter.h" #include "clang/Tooling/Refactoring.h" #include "llvm/Support/raw_os_ostream.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" namespace clang { namespace tooling { @@ -103,7 +105,16 @@ void Replacement::setFromSourceLocation(SourceManager &Sources, const std::pair DecomposedLocation = Sources.getDecomposedLoc(Start); const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first); - this->FilePath = Entry != NULL ? Entry->getName() : InvalidLocation; + + if (Entry != NULL) { + // Make FilePath absolute so replacements can be applied correctly when + // relative paths for files are used. + llvm::SmallString<256> FilePath(Entry->getName()); + llvm::error_code EC = llvm::sys::fs::make_absolute(FilePath); + // Don't change the FilePath if the file is a virtual file. + this->FilePath = EC ? FilePath.c_str() : Entry->getName(); + } else + this->FilePath = InvalidLocation; this->ReplacementRange = Range(DecomposedLocation.second, Length); this->ReplacementText = ReplacementText; } diff --git a/clang/test/Tooling/clang-check-rel-path.cpp b/clang/test/Tooling/clang-check-rel-path.cpp new file mode 100644 index 000000000000..23a9345b99b3 --- /dev/null +++ b/clang/test/Tooling/clang-check-rel-path.cpp @@ -0,0 +1,10 @@ +// This block test a compilation database with files relative to the directory +// RUN: rm -rf %t +// RUN: mkdir %t +// RUN: echo '[{"directory":"%t","command":"clang++ -c test.cpp","file":"test.cpp"}]' | sed -e 's/\\/\//g' > %t/compile_commands.json +// RUN: cp "%s" "%t/test.cpp" +// RUN: not clang-check -p "%t" "%t/test.cpp" 2>&1|FileCheck %s +// FIXME: Make the above easier. + +// CHECK: C++ requires +invalid;