[PATCH] Fix replacements for files with relative paths are not applied.

Ariel Bernal ariel.j.bernal at intel.com
Mon Sep 30 12:57:46 PDT 2013


  Addressed comments and fixed the case where virtual file paths cannot be made absolute. Also added a regression test.

  Note that make_absolute already checks for the file to exist so it should be equivalent to check whether the file is virtual or not but if this introduces a performance issue then maybe we would want to have a function in the FileManager to query if a FileEntry is a virtual file (in the VirtualFileEntries vector).

Hi tareqsiraj, revane, klimek,

http://llvm-reviews.chandlerc.com/D1771

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1771?vs=4519&id=4553#toc

Files:
  lib/Tooling/Refactoring.cpp
  test/Tooling/clang-check-rel-path.cpp

Index: lib/Tooling/Refactoring.cpp
===================================================================
--- lib/Tooling/Refactoring.cpp
+++ 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 @@
   const std::pair<FileID, unsigned> 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;
 }
Index: test/Tooling/clang-check-rel-path.cpp
===================================================================
--- /dev/null
+++ 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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1771.2.patch
Type: text/x-patch
Size: 1861 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130930/83c5fd7f/attachment.bin>


More information about the cfe-commits mailing list