Hi klimek, tareqsiraj,

D1771 was supposed to fix this but it didn't, make_absolute can't be use to 
check if a file is virtual. I didn't break anything either.
This patch exposes virtual entries in the FileManager to check if a filepath is 
a virtual file that has already been allocated.


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

Files:
  include/clang/Basic/FileManager.h
  lib/Basic/FileManager.cpp
  lib/Tooling/Refactoring.cpp

Index: include/clang/Basic/FileManager.h
===================================================================
--- include/clang/Basic/FileManager.h
+++ include/clang/Basic/FileManager.h
@@ -273,6 +273,10 @@
   /// required, which is (almost) never.
   StringRef getCanonicalName(const DirectoryEntry *Dir);
 
+
+  /// \brief Check if a FilePath is a virtual file that we have allocated.
+  bool isFileVirtual(StringRef FilePath);
+
   void PrintStats() const;
 };
 
Index: lib/Basic/FileManager.cpp
===================================================================
--- lib/Basic/FileManager.cpp
+++ lib/Basic/FileManager.cpp
@@ -580,6 +580,16 @@
 #endif
 }
 
+bool FileManager::isFileVirtual(StringRef FilePath) {
+  for (SmallVectorImpl<FileEntry *>::const_iterator
+           I = VirtualFileEntries.begin(),
+           E = VirtualFileEntries.end();
+       I != E; ++I)
+    if ((*I)->getName() == FilePath)
+      return true;
+  return false;
+}
+
 void FileManager::PrintStats() const {
   llvm::errs() << "\n*** File Manager Stats:\n";
   llvm::errs() << UniqueRealFiles.size() << " real files found, "
Index: lib/Tooling/Refactoring.cpp
===================================================================
--- lib/Tooling/Refactoring.cpp
+++ lib/Tooling/Refactoring.cpp
@@ -105,15 +105,18 @@
   const std::pair<FileID, unsigned> DecomposedLocation =
       Sources.getDecomposedLoc(Start);
   const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first);
-
-  if (Entry != NULL) {
+  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
+    // relative paths for file are used. But we don't want to change virtual
+    // files.
+    if (Sources.getFileManager().isFileVirtual(Entry->getName())) {
+      this->FilePath = Entry->getName();
+    } else {
+      llvm::SmallString<256> FilePath(Entry->getName());
+      llvm::sys::fs::make_absolute(FilePath);
+      this->FilePath = FilePath.c_str();
+    }
+  else
     this->FilePath = InvalidLocation;
   this->ReplacementRange = Range(DecomposedLocation.second, Length);
   this->ReplacementText = ReplacementText;
Index: include/clang/Basic/FileManager.h
===================================================================
--- include/clang/Basic/FileManager.h
+++ include/clang/Basic/FileManager.h
@@ -273,6 +273,10 @@
   /// required, which is (almost) never.
   StringRef getCanonicalName(const DirectoryEntry *Dir);
 
+
+  /// \brief Check if a FilePath is a virtual file that we have allocated.
+  bool isFileVirtual(StringRef FilePath);
+
   void PrintStats() const;
 };
 
Index: lib/Basic/FileManager.cpp
===================================================================
--- lib/Basic/FileManager.cpp
+++ lib/Basic/FileManager.cpp
@@ -580,6 +580,16 @@
 #endif
 }
 
+bool FileManager::isFileVirtual(StringRef FilePath) {
+  for (SmallVectorImpl<FileEntry *>::const_iterator
+           I = VirtualFileEntries.begin(),
+           E = VirtualFileEntries.end();
+       I != E; ++I)
+    if ((*I)->getName() == FilePath)
+      return true;
+  return false;
+}
+
 void FileManager::PrintStats() const {
   llvm::errs() << "\n*** File Manager Stats:\n";
   llvm::errs() << UniqueRealFiles.size() << " real files found, "
Index: lib/Tooling/Refactoring.cpp
===================================================================
--- lib/Tooling/Refactoring.cpp
+++ lib/Tooling/Refactoring.cpp
@@ -105,15 +105,18 @@
   const std::pair<FileID, unsigned> DecomposedLocation =
       Sources.getDecomposedLoc(Start);
   const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first);
-
-  if (Entry != NULL) {
+  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
+    // relative paths for file are used. But we don't want to change virtual
+    // files.
+    if (Sources.getFileManager().isFileVirtual(Entry->getName())) {
+      this->FilePath = Entry->getName();
+    } else {
+      llvm::SmallString<256> FilePath(Entry->getName());
+      llvm::sys::fs::make_absolute(FilePath);
+      this->FilePath = FilePath.c_str();
+    }
+  else
     this->FilePath = InvalidLocation;
   this->ReplacementRange = Range(DecomposedLocation.second, Length);
   this->ReplacementText = ReplacementText;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to