Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
[lld] Add Visual Studio compatible diagnostics
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Chris Jackson committed Jul 17, 2019
1 parent 3c312d3 commit 000acde
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 6 deletions.
45 changes: 40 additions & 5 deletions Common/ErrorHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
}

Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
3 changes: 3 additions & 0 deletions ELF/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">;
Expand Down
3 changes: 2 additions & 1 deletion include/lld/Common/ErrorHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ErrorHandler {
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);
Expand All @@ -101,7 +102,7 @@ class ErrorHandler {
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.
Expand Down
31 changes: 31 additions & 0 deletions test/ELF/Inputs/vs-diagnostics-duplicate2.s
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions test/ELF/Inputs/vs-diagnostics-duplicate3.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.file "duplicate3.s"

.global baz
.text
baz:
nop
63 changes: 63 additions & 0 deletions test/ELF/vs-diagnostics-duplicate.s
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions test/ELF/vs-diagnostics-dynamic-relocation.s
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions test/ELF/vs-diagnostics-undefined-symbol-1.s
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/ELF/vs-diagnostics-undefined-symbol-2.s
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions test/ELF/vs-diagnostics-undefined-symbol-3.s
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/ELF/vs-diagnostics-versionscript.s
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 000acde

Please sign in to comment.