[PATCH] D27810: Normalize all filenames before searching FileManager caches

Erik Verbruggen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 15 07:51:14 PST 2016

erikjv created this revision.
erikjv added reviewers: bkramer, klimek.
erikjv added a subscriber: cfe-commits.

The problem on windows is that both forward-slashes and back-slashes are
accepted as path separators. However, the FileManager stores the path as
reported by the OS after opening the file. For example: d:\dev/foo.cc
will be stored with the key d:\dev\foo.cc. Subsequent lookups for the
case with the forward slash then miss that cache entry, and clang ends
up doing many more file accesses than needed.

It also had the nasty side-effect of not closing the file the second
time it was loaded (until the FileManager was destroyed): the lexer
would create a buffer by file-id, and the buffer cache would return the
buffer created the previous time the file was opened. And because
creating the buffer also closes the file, this would not happen. As long
as the FileManager was around (i.e. as long as the translation unit for
e.g. libclang was around), and IDE couldn't save that file.



Index: lib/Basic/FileManager.cpp
--- lib/Basic/FileManager.cpp
+++ lib/Basic/FileManager.cpp
@@ -214,6 +214,12 @@
 const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
                                       bool CacheFailure) {
+#ifdef LLVM_ON_WIN32
+  SmallString<128> NormalizedPath(Filename.str());
+  llvm::sys::path::native(NormalizedPath);
+  Filename = NormalizedPath;
+#endif // LLVM_ON_WIN32
   // See if there is already an entry in the map.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27810.81582.patch
Type: text/x-patch
Size: 567 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161215/e7d82230/attachment.bin>

More information about the cfe-commits mailing list