[lld] r366333 - [lld] Add Visual Studio compatible diagnostics

Chris Jackson via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 17 07:54:03 PDT 2019


Author: chrisj
Date: Wed Jul 17 07:54:02 2019
New Revision: 366333

URL: http://llvm.org/viewvc/llvm-project?rev=366333&view=rev
Log:
[lld] Add Visual Studio compatible diagnostics

Summary:
Add a --vs-diagnostics flag that alters the format of diagnostic output
to enable source hyperlinks in Visual Studio.

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

Reviewed by: ruiu

Added:
    lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate2.s
    lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate3.s
    lld/trunk/test/ELF/vs-diagnostics-duplicate.s
    lld/trunk/test/ELF/vs-diagnostics-dynamic-relocation.s
    lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-1.s
    lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-2.s
    lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-3.s
    lld/trunk/test/ELF/vs-diagnostics-versionscript.s
Modified:
    lld/trunk/Common/ErrorHandler.cpp
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/Options.td
    lld/trunk/include/lld/Common/ErrorHandler.h

Modified: lld/trunk/Common/ErrorHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/Common/ErrorHandler.cpp?rev=366333&r1=366332&r2=366333&view=diff
==============================================================================
--- lld/trunk/Common/ErrorHandler.cpp (original)
+++ lld/trunk/Common/ErrorHandler.cpp Wed Jul 17 07:54:02 2019
@@ -16,6 +16,7 @@
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/raw_ostream.h"
 #include <mutex>
+#include <regex>
 
 #if !defined(_MSC_VER) && !defined(__MINGW32__)
 #include <unistd.h>
@@ -84,8 +85,42 @@ void lld::checkError(Error e) {
                   [&](ErrorInfoBase &eib) { error(eib.message()); });
 }
 
-void ErrorHandler::print(StringRef s, raw_ostream::Colors c) {
-  *errorOS << logName << ": ";
+static std::string getLocation(std::string msg, std::string defaultMsg) {
+  static std::vector<std::regex> Regexes{
+      std::regex(R"(^undefined symbol:.*\n>>> referenced by (\S+):(\d+)\n.*)"),
+      std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)"),
+      std::regex(
+          R"(^duplicate symbol: .*\n>>> defined in (\S+)\n>>> defined in.*)"),
+      std::regex(
+          R"(^duplicate symbol: .*\n>>> defined at (\S+):(\d+).*)"),
+      std::regex(
+          R"(.*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"),
+      std::regex(
+          R"(^undefined (internal|hidden|protected) symbol: .*\n>>> referenced by (\S+):(\d+)\n.*)"),
+      std::regex(R"((\S+):(\d+): unclosed quote)"),
+  };
+
+  std::smatch Match;
+  for (std::regex &Re : Regexes) {
+    if (std::regex_search(msg, Match, Re)) {
+      return Match.size() > 2 ? Match.str(1) + "(" + Match.str(2) + ")"
+                              : Match.str(1);
+    }
+  }
+  return defaultMsg;
+}
+
+void ErrorHandler::printHeader(StringRef s, raw_ostream::Colors c,
+                               const Twine &msg) {
+
+  if (vsDiagnostics) {
+    // A Visual Studio-style error message starts with an error location.
+    // If a location cannot be extracted then we default to LogName.
+    *errorOS << getLocation(msg.str(), logName) << ": ";
+  } else {
+    *errorOS << logName << ": ";
+  }
+
   if (colorDiagnostics) {
     errorOS->changeColor(c, true);
     *errorOS << s;
@@ -116,7 +151,7 @@ void ErrorHandler::warn(const Twine &msg
 
   std::lock_guard<std::mutex> lock(mu);
   newline(errorOS, msg);
-  print("warning: ", raw_ostream::MAGENTA);
+  printHeader("warning: ", raw_ostream::MAGENTA, msg);
   *errorOS << msg << "\n";
 }
 
@@ -125,10 +160,10 @@ void ErrorHandler::error(const Twine &ms
   newline(errorOS, msg);
 
   if (errorLimit == 0 || errorCount < errorLimit) {
-    print("error: ", raw_ostream::RED);
+    printHeader("error: ", raw_ostream::RED, msg);
     *errorOS << msg << "\n";
   } else if (errorCount == errorLimit) {
-    print("error: ", raw_ostream::RED);
+    printHeader("error: ", raw_ostream::RED, msg);
     *errorOS << errorLimitExceededMsg << "\n";
     if (exitEarly)
       exitLld(1);

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=366333&r1=366332&r2=366333&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Wed Jul 17 07:54:02 2019
@@ -786,6 +786,8 @@ static void readConfigs(opt::InputArgLis
   errorHandler().verbose = args.hasArg(OPT_verbose);
   errorHandler().fatalWarnings =
       args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
+  errorHandler().vsDiagnostics =
+      args.hasArg(OPT_visual_studio_diagnostics_format, false);
   threadsEnabled = args.hasFlag(OPT_threads, OPT_no_threads, true);
 
   config->allowMultipleDefinition =

Modified: lld/trunk/ELF/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Options.td?rev=366333&r1=366332&r2=366333&view=diff
==============================================================================
--- lld/trunk/ELF/Options.td (original)
+++ lld/trunk/ELF/Options.td Wed Jul 17 07:54:02 2019
@@ -416,6 +416,9 @@ defm wrap: Eq<"wrap", "Use wrapper funct
 def z: JoinedOrSeparate<["-"], "z">, MetaVarName<"<option>">,
   HelpText<"Linker option extensions">;
 
+def visual_studio_diagnostics_format : F<"vs-diagnostics">,
+HelpText<"Format diagnostics for Visual Studio compatiblity">;
+
 // Aliases
 def: Separate<["-"], "f">, Alias<auxiliary>, HelpText<"Alias for --auxiliary">;
 def: F<"call_shared">, Alias<Bdynamic>, HelpText<"Alias for --Bdynamic">;

Modified: lld/trunk/include/lld/Common/ErrorHandler.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Common/ErrorHandler.h?rev=366333&r1=366332&r2=366333&view=diff
==============================================================================
--- lld/trunk/include/lld/Common/ErrorHandler.h (original)
+++ lld/trunk/include/lld/Common/ErrorHandler.h Wed Jul 17 07:54:02 2019
@@ -91,6 +91,7 @@ public:
   bool exitEarly = true;
   bool fatalWarnings = false;
   bool verbose = false;
+  bool vsDiagnostics = false;
 
   void error(const Twine &msg);
   LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &msg);
@@ -101,7 +102,7 @@ public:
   std::unique_ptr<llvm::FileOutputBuffer> outputBuffer;
 
 private:
-  void print(StringRef s, raw_ostream::Colors c);
+  void printHeader(StringRef s, raw_ostream::Colors c, const Twine &msg);
 };
 
 /// Returns the default error handler.

Added: lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate2.s?rev=366333&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate2.s (added)
+++ lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate2.s Wed Jul 17 07:54:02 2019
@@ -0,0 +1,31 @@
+.global foo, bar
+
+.text
+foo:
+  nop
+
+.file 1 "duplicate2.s"
+.loc 1 20
+bar:
+  nop
+
+.section .debug_abbrev,"", at progbits
+  .byte  1                      # Abbreviation Code
+  .byte 17                      # DW_TAG_compile_unit
+  .byte  0                      # DW_CHILDREN_no
+  .byte 16                      # DW_AT_stmt_list
+  .byte 23                      # DW_FORM_sec_offset
+  .byte  0                      # EOM(1)
+  .byte  0                      # EOM(2)
+  .byte  0                      # EOM(3)
+
+.section .debug_info,"", at progbits
+  .long .Lend0 - .Lbegin0       # Length of Unit
+.Lbegin0:
+  .short 4                      # DWARF version number
+  .long  .debug_abbrev          # Offset Into Abbrev. Section
+  .byte  8                      # Address Size (in bytes)
+  .byte  1                      # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit
+  .long  .debug_line            # DW_AT_stmt_list
+.Lend0:
+  .section .debug_line,"", at progbits

Added: lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate3.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate3.s?rev=366333&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate3.s (added)
+++ lld/trunk/test/ELF/Inputs/vs-diagnostics-duplicate3.s Wed Jul 17 07:54:02 2019
@@ -0,0 +1,6 @@
+.file "duplicate3.s"
+
+.global baz
+.text
+baz:
+  nop

Added: lld/trunk/test/ELF/vs-diagnostics-duplicate.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/vs-diagnostics-duplicate.s?rev=366333&view=auto
==============================================================================
--- lld/trunk/test/ELF/vs-diagnostics-duplicate.s (added)
+++ lld/trunk/test/ELF/vs-diagnostics-duplicate.s Wed Jul 17 07:54:02 2019
@@ -0,0 +1,63 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/vs-diagnostics-duplicate2.s -o %t2.o
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/vs-diagnostics-duplicate3.s -o %t3.o
+// RUN: not ld.lld --vs-diagnostics %t1.o %t2.o %t3.o -o %tout 2>&1 | FileCheck %s
+
+// Case 1. Both symbols have full source location.
+// CHECK:      duplicate.s(15): error: duplicate symbol: bar
+// CHECK-NEXT: >>> defined at duplicate.s:15
+// CHECK-NEXT: >>>{{.*}}1.o:(.text+0x{{.+}})
+// CHECK: >>> defined at duplicate2.s:20
+// CHECK: >>>{{.*}}2.o:(.text+0x{{.+}})
+
+// Case 2. The source locations are unknown for both symbols.
+// CHECK:      {{.*}}ld.lld{{.*}}: error: duplicate symbol: foo
+// CHECK-NEXT: >>> defined at {{.*}}1.o:(.text+0x{{.+}})
+// CHECK-NEXT: >>> defined at {{.*}}2.o:(.text+0x{{.+}})
+
+// Case 3. For the second definition of `baz` we know only the source file found in a STT_FILE symbol.
+// CHECK:      duplicate.s(30): error: duplicate symbol: baz
+// CHECK-NEXT: >>> defined at duplicate.s:30
+// CHECK-NEXT: >>> {{.*}}1.o:(.text+0x{{.+}})
+// CHECK-NEXT: >>> defined at duplicate3.s
+// CHECK-NEXT: >>>            {{.*}}3.o:(.text+0x{{.+}})
+
+.global _start, foo, bar, baz
+.text
+_start:
+  nop
+
+foo:
+  nop
+
+.file 1 "duplicate.s"
+.loc 1 15
+
+bar:
+  nop
+
+.loc 1 30
+baz:
+  nop
+
+.section .debug_abbrev,"", at progbits
+  .byte  1                      # Abbreviation Code
+  .byte 17                      # DW_TAG_compile_unit
+  .byte  0                      # DW_CHILDREN_no
+  .byte 16                      # DW_AT_stmt_list
+  .byte 23                      # DW_FORM_sec_offset
+  .byte  0                      # EOM(1)
+  .byte  0                      # EOM(2)
+  .byte  0                      # EOM(3)
+
+.section .debug_info,"", at progbits
+  .long .Lend0 - .Lbegin0       # Length of Unit
+.Lbegin0:
+  .short 4                      # DWARF version number
+  .long  .debug_abbrev          # Offset Into Abbrev. Section
+  .byte  8                      # Address Size (in bytes)
+  .byte  1                      # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit
+  .long  .debug_line            # DW_AT_stmt_list
+.Lend0:
+  .section .debug_line,"", at progbits

Added: lld/trunk/test/ELF/vs-diagnostics-dynamic-relocation.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/vs-diagnostics-dynamic-relocation.s?rev=366333&view=auto
==============================================================================
--- lld/trunk/test/ELF/vs-diagnostics-dynamic-relocation.s (added)
+++ lld/trunk/test/ELF/vs-diagnostics-dynamic-relocation.s Wed Jul 17 07:54:02 2019
@@ -0,0 +1,35 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: not ld.lld -shared --vs-diagnostics %t.o -o /dev/null 2>&1 | FileCheck %s
+
+// CHECK: dyn.s(15): error: can't create dynamic relocation R_X86_64_64 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
+// CHECK-NEXT: >>> defined in {{.*}}.o
+// CHECK-NEXT: >>> referenced by dyn.s:15
+// CHECK-NEXT: >>>{{.*}}.o:(.text+0x{{.+}})
+
+.file 1 "dyn.s"
+.loc 1 15
+
+foo:
+.quad foo
+
+.section .debug_abbrev,"", at progbits
+  .byte  1                      # Abbreviation Code
+  .byte 17                      # DW_TAG_compile_unit
+  .byte  0                      # DW_CHILDREN_no
+  .byte 16                      # DW_AT_stmt_list
+  .byte 23                      # DW_FORM_sec_offset
+  .byte  0                      # EOM(1)
+  .byte  0                      # EOM(2)
+  .byte  0                      # EOM(3)
+
+.section .debug_info,"", at progbits
+  .long .Lend0 - .Lbegin0       # Length of Unit
+.Lbegin0:
+  .short 4                      # DWARF version number
+  .long  .debug_abbrev          # Offset Into Abbrev. Section
+  .byte  8                      # Address Size (in bytes)
+  .byte  1                      # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit
+  .long  .debug_line            # DW_AT_stmt_list
+.Lend0:
+  .section .debug_line,"", at progbits

Added: lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-1.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-1.s?rev=366333&view=auto
==============================================================================
--- lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-1.s (added)
+++ lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-1.s Wed Jul 17 07:54:02 2019
@@ -0,0 +1,15 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
+// RUN: not ld.lld --vs-diagnostics %t1.o -o %tout 2>&1 \
+// RUN:   | FileCheck -check-prefix=ERR -check-prefix=CHECK -DFILE=%t1.o %s
+// RUN: ld.lld --vs-diagnostics --warn-unresolved-symbols %t1.o -o %tout 2>&1 \
+// RUN:   | FileCheck -check-prefix=WARN -check-prefix=CHECK -DFILE=%t1.o %s
+
+// ERR:        [[FILE]]: error: undefined symbol: foo
+// WARN:       [[FILE]]: warning: undefined symbol: foo
+// CHECK-NEXT: >>> referenced by {{.*}}1.o:(.text+0x{{.+}})
+
+.global _start, foo
+.text
+_start:
+  jmp foo
\ No newline at end of file

Added: lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-2.s?rev=366333&view=auto
==============================================================================
--- lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-2.s (added)
+++ lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-2.s Wed Jul 17 07:54:02 2019
@@ -0,0 +1,18 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
+// RUN: not ld.lld --vs-diagnostics %t1.o -o %tout 2>&1 \
+// RUN:   | FileCheck -check-prefix=ERR -check-prefix=CHECK %s
+// RUN: ld.lld --vs-diagnostics --warn-unresolved-symbols %t1.o -o %tout 2>&1 \
+// RUN:   | FileCheck -check-prefix=WARN -check-prefix=CHECK %s
+
+// ERR:        {{.*}}ld.lld{{.*}}: error: undefined symbol: foo
+// WARN:       {{.*}}ld.lld{{.*}}: warning: undefined symbol: foo
+// CHECK-NEXT: >>> referenced by undef2.s
+// CHECK-NEXT: >>>               {{.*}}1.o:(.text+0x{{.+}})
+
+.file "undef2.s"
+
+.global _start, foo
+.text
+_start:
+  jmp foo

Added: lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-3.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-3.s?rev=366333&view=auto
==============================================================================
--- lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-3.s (added)
+++ lld/trunk/test/ELF/vs-diagnostics-undefined-symbol-3.s Wed Jul 17 07:54:02 2019
@@ -0,0 +1,40 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
+// RUN: not ld.lld --vs-diagnostics %t1.o -o %tout 2>&1 \
+// RUN:   | FileCheck -check-prefix=ERR -check-prefix=CHECK %s
+// RUN: ld.lld --vs-diagnostics --warn-unresolved-symbols %t1.o -o %tout 2>&1 \
+// RUN:   | FileCheck -check-prefix=WARN -check-prefix=CHECK %s
+
+// ERR:        undef3.s(15): error: undefined symbol: foo
+// WARN:       undef3.s(15): warning: undefined symbol: foo
+// CHECK:      >>> referenced by undef3.s:15
+// CHECK-NEXT: >>> {{.*}}1.o:(.text+0x{{.+}})
+
+.file 1 "undef3.s"
+
+.global _start, foo
+.text
+_start:
+.loc 1 15
+  jmp foo
+
+.section .debug_abbrev,"", at progbits
+  .byte  1                      # Abbreviation Code
+  .byte 17                      # DW_TAG_compile_unit
+  .byte  0                      # DW_CHILDREN_no
+  .byte 16                      # DW_AT_stmt_list
+  .byte 23                      # DW_FORM_sec_offset
+  .byte  0                      # EOM(1)
+  .byte  0                      # EOM(2)
+  .byte  0                      # EOM(3)
+
+.section .debug_info,"", at progbits
+  .long .Lend0 - .Lbegin0       # Length of Unit
+.Lbegin0:
+  .short 4                      # DWARF version number
+  .long  .debug_abbrev          # Offset Into Abbrev. Section
+  .byte  8                      # Address Size (in bytes)
+  .byte  1                      # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit
+  .long  .debug_line            # DW_AT_stmt_list
+.Lend0:
+  .section .debug_line,"", at progbits

Added: lld/trunk/test/ELF/vs-diagnostics-versionscript.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/vs-diagnostics-versionscript.s?rev=366333&view=auto
==============================================================================
--- lld/trunk/test/ELF/vs-diagnostics-versionscript.s (added)
+++ lld/trunk/test/ELF/vs-diagnostics-versionscript.s Wed Jul 17 07:54:02 2019
@@ -0,0 +1,7 @@
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: rm -f %/terr1.script
+# RUN: echo  "\"" > %/terr1.script
+# RUN: not ld.lld --vs-diagnostics  --version-script %/terr1.script -shared %/t.o -o %/t.so 2>&1 | \
+# RUN: FileCheck %s -DSCRIPT="%/terr1.script"
+
+# CHECK: [[SCRIPT]](1): error: [[SCRIPT]]:1: unclosed quote




More information about the llvm-commits mailing list