r361598 - Do not resolve directory junctions for `-fdiagnostics-absolute-paths` on Windows.

Igor Kudrin via cfe-commits cfe-commits at lists.llvm.org
Thu May 23 21:46:22 PDT 2019


Author: ikudrin
Date: Thu May 23 21:46:22 2019
New Revision: 361598

URL: http://llvm.org/viewvc/llvm-project?rev=361598&view=rev
Log:
Do not resolve directory junctions for `-fdiagnostics-absolute-paths` on Windows.

If the source file path contains directory junctions, and we resolve them when
printing diagnostic messages, these paths look independent for an IDE.
For example, both Visual Studio and Visual Studio Code open separate editors
for such paths, which is not only inconvenient but might even result in losing
changes made in one of them.

Differential Revision: https://reviews.llvm.org/D59415

Added:
    cfe/trunk/test/Frontend/absolute-paths-windows.test
Modified:
    cfe/trunk/lib/Frontend/TextDiagnostic.cpp
    cfe/trunk/test/Frontend/lit.local.cfg

Modified: cfe/trunk/lib/Frontend/TextDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnostic.cpp?rev=361598&r1=361597&r2=361598&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnostic.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnostic.cpp Thu May 23 21:46:22 2019
@@ -765,7 +765,28 @@ void TextDiagnostic::emitFilename(String
     const DirectoryEntry *Dir = SM.getFileManager().getDirectory(
         llvm::sys::path::parent_path(Filename));
     if (Dir) {
+      // We want to print a simplified absolute path, i. e. without "dots".
+      //
+      // The hardest part here are the paths like "<part1>/<link>/../<part2>".
+      // On Unix-like systems, we cannot just collapse "<link>/..", because
+      // paths are resolved sequentially, and, thereby, the path
+      // "<part1>/<part2>" may point to a different location. That is why
+      // we use FileManager::getCanonicalName(), which expands all indirections
+      // with llvm::sys::fs::real_path() and caches the result.
+      //
+      // On the other hand, it would be better to preserve as much of the
+      // original path as possible, because that helps a user to recognize it.
+      // real_path() expands all links, which sometimes too much. Luckily,
+      // on Windows we can just use llvm::sys::path::remove_dots(), because,
+      // on that system, both aforementioned paths point to the same place.
+#ifdef _WIN32
+      SmallString<4096> DirName = Dir->getName();
+      llvm::sys::fs::make_absolute(DirName);
+      llvm::sys::path::native(DirName);
+      llvm::sys::path::remove_dots(DirName, /* remove_dot_dot */ true);
+#else
       StringRef DirName = SM.getFileManager().getCanonicalName(Dir);
+#endif
       llvm::sys::path::append(AbsoluteFilename, DirName,
                               llvm::sys::path::filename(Filename));
       Filename = StringRef(AbsoluteFilename.data(), AbsoluteFilename.size());

Added: cfe/trunk/test/Frontend/absolute-paths-windows.test
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/absolute-paths-windows.test?rev=361598&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/absolute-paths-windows.test (added)
+++ cfe/trunk/test/Frontend/absolute-paths-windows.test Thu May 23 21:46:22 2019
@@ -0,0 +1,9 @@
+// REQUIRES: system-windows
+// RUN: rm -rf %t.dir
+// RUN: mkdir -p %t.dir\real
+// RUN: cmd /c mklink /j %t.dir\junc %t.dir\real
+// RUN: echo "wrong code" > %t.dir\real\foo.cpp
+// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-absolute-paths %t.dir\junc\foo.cpp 2>&1 | FileCheck %s
+
+// CHECK-NOT: .dir\real\foo.cpp
+// CHECK: .dir\junc\foo.cpp

Modified: cfe/trunk/test/Frontend/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/lit.local.cfg?rev=361598&r1=361597&r2=361598&view=diff
==============================================================================
--- cfe/trunk/test/Frontend/lit.local.cfg (original)
+++ cfe/trunk/test/Frontend/lit.local.cfg Thu May 23 21:46:22 2019
@@ -1 +1 @@
-config.suffixes = ['.c', '.cpp', '.m', '.mm', '.ll', '.cl']
+config.suffixes = ['.c', '.cpp', '.m', '.mm', '.ll', '.cl', '.test']




More information about the cfe-commits mailing list