diff options
author | Chris Jackson <snortotter@gmail.com> | 2019-07-17 14:54:02 +0000 |
---|---|---|
committer | Chris Jackson <snortotter@gmail.com> | 2019-07-17 14:54:02 +0000 |
commit | 000acdecae21147e667841e29d348ac4c57cf811 (patch) | |
tree | ad29f411d655960f9a0ad89502a17399372e98fc | |
parent | 3c312d33d3c589c75dd3e18a28b99a23f789b799 (diff) |
[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
git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@366333 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | Common/ErrorHandler.cpp | 45 | ||||
-rw-r--r-- | ELF/Driver.cpp | 2 | ||||
-rw-r--r-- | ELF/Options.td | 3 | ||||
-rw-r--r-- | include/lld/Common/ErrorHandler.h | 3 | ||||
-rw-r--r-- | test/ELF/Inputs/vs-diagnostics-duplicate2.s | 31 | ||||
-rw-r--r-- | test/ELF/Inputs/vs-diagnostics-duplicate3.s | 6 | ||||
-rw-r--r-- | test/ELF/vs-diagnostics-duplicate.s | 63 | ||||
-rw-r--r-- | test/ELF/vs-diagnostics-dynamic-relocation.s | 35 | ||||
-rw-r--r-- | test/ELF/vs-diagnostics-undefined-symbol-1.s | 15 | ||||
-rw-r--r-- | test/ELF/vs-diagnostics-undefined-symbol-2.s | 18 | ||||
-rw-r--r-- | test/ELF/vs-diagnostics-undefined-symbol-3.s | 40 | ||||
-rw-r--r-- | test/ELF/vs-diagnostics-versionscript.s | 7 |
12 files changed, 262 insertions, 6 deletions
diff --git a/Common/ErrorHandler.cpp b/Common/ErrorHandler.cpp index f5d3eb448..c87c0609b 100644 --- a/Common/ErrorHandler.cpp +++ b/Common/ErrorHandler.cpp @@ -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 &msg) { 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); diff --git a/ELF/Driver.cpp b/ELF/Driver.cpp index 98551d2cb..fbfc71d22 100644 --- a/ELF/Driver.cpp +++ b/ELF/Driver.cpp @@ -786,6 +786,8 @@ static void readConfigs(opt::InputArgList &args) { 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 = diff --git a/ELF/Options.td b/ELF/Options.td index d45d9aaad..3ebb46f2e 100644 --- a/ELF/Options.td +++ b/ELF/Options.td @@ -416,6 +416,9 @@ defm wrap: Eq<"wrap", "Use wrapper functions for symbol">, 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">; diff --git a/include/lld/Common/ErrorHandler.h b/include/lld/Common/ErrorHandler.h index 3467fdc74..7126a7bf4 100644 --- a/include/lld/Common/ErrorHandler.h +++ b/include/lld/Common/ErrorHandler.h @@ -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. diff --git a/test/ELF/Inputs/vs-diagnostics-duplicate2.s b/test/ELF/Inputs/vs-diagnostics-duplicate2.s new file mode 100644 index 000000000..4edfae092 --- /dev/null +++ b/test/ELF/Inputs/vs-diagnostics-duplicate2.s @@ -0,0 +1,31 @@ +.global foo, bar + +.text +foo: + nop + +.file 1 "duplicate2.s" +.loc 1 20 +bar: + nop + +.section .debug_abbrev,"",@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,"",@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,"",@progbits diff --git a/test/ELF/Inputs/vs-diagnostics-duplicate3.s b/test/ELF/Inputs/vs-diagnostics-duplicate3.s new file mode 100644 index 000000000..81829c82b --- /dev/null +++ b/test/ELF/Inputs/vs-diagnostics-duplicate3.s @@ -0,0 +1,6 @@ +.file "duplicate3.s" + +.global baz +.text +baz: + nop diff --git a/test/ELF/vs-diagnostics-duplicate.s b/test/ELF/vs-diagnostics-duplicate.s new file mode 100644 index 000000000..efd0cbe5f --- /dev/null +++ b/test/ELF/vs-diagnostics-duplicate.s @@ -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,"",@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,"",@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,"",@progbits diff --git a/test/ELF/vs-diagnostics-dynamic-relocation.s b/test/ELF/vs-diagnostics-dynamic-relocation.s new file mode 100644 index 000000000..6575133ec --- /dev/null +++ b/test/ELF/vs-diagnostics-dynamic-relocation.s @@ -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,"",@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,"",@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,"",@progbits diff --git a/test/ELF/vs-diagnostics-undefined-symbol-1.s b/test/ELF/vs-diagnostics-undefined-symbol-1.s new file mode 100644 index 000000000..908549327 --- /dev/null +++ b/test/ELF/vs-diagnostics-undefined-symbol-1.s @@ -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 diff --git a/test/ELF/vs-diagnostics-undefined-symbol-2.s b/test/ELF/vs-diagnostics-undefined-symbol-2.s new file mode 100644 index 000000000..3c8c80ef6 --- /dev/null +++ b/test/ELF/vs-diagnostics-undefined-symbol-2.s @@ -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 diff --git a/test/ELF/vs-diagnostics-undefined-symbol-3.s b/test/ELF/vs-diagnostics-undefined-symbol-3.s new file mode 100644 index 000000000..3ff9885b7 --- /dev/null +++ b/test/ELF/vs-diagnostics-undefined-symbol-3.s @@ -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,"",@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,"",@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,"",@progbits diff --git a/test/ELF/vs-diagnostics-versionscript.s b/test/ELF/vs-diagnostics-versionscript.s new file mode 100644 index 000000000..2d0be7fc0 --- /dev/null +++ b/test/ELF/vs-diagnostics-versionscript.s @@ -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 |