Skip to content

Commit 1c829ce

Browse files
committed
[clang][codegen] Set CurLinkModule in CodeGenAction::ExecuteAction
CodeGenAction::ExecuteAction creates a BackendConsumer for the purpose of handling diagnostics. The BackendConsumer's DiagnosticHandlerImpl method expects CurLinkModule to be set, but this did not happen on the code path that goes through ExecuteAction. This change makes it so that the BackendConsumer constructor used by ExecuteAction requires the Module to be specified and passes the appropriate module in ExecuteAction. The change also adds a test that fails without this change and passes with it. To make the test work, the FIXME in the handling of DK_Linker diagnostics was addressed so that warnings and notes are no longer silently discarded. Since this introduces a new warning diagnostic, a flag to control it (-Wlinker-warnings) has also been added. Reviewed By: xur Differential Revision: https://reviews.llvm.org/D108603
1 parent ec8d87e commit 1c829ce

File tree

6 files changed

+41
-14
lines changed

6 files changed

+41
-14
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ def note_fe_inline_asm_here : Note<"instantiated into assembly here">;
2222
def err_fe_source_mgr : Error<"%0">, CatSourceMgr;
2323
def warn_fe_source_mgr : Warning<"%0">, CatSourceMgr, InGroup<BackendSourceMgr>;
2424
def note_fe_source_mgr : Note<"%0">, CatSourceMgr;
25-
def err_fe_cannot_link_module : Error<"cannot link module '%0': %1">,
26-
DefaultFatal;
25+
def err_fe_linking_module : Error<"cannot link module '%0': %1">, DefaultFatal;
26+
def warn_fe_linking_module : Warning<"linking module '%0': %1">, InGroup<LinkerWarnings>;
27+
def note_fe_linking_module : Note<"linking module '%0': %1">;
2728

2829
def warn_fe_frame_larger_than : Warning<"stack frame size (%0) exceeds limit (%1) in %q2">,
2930
BackendInfo, InGroup<BackendFrameLargerThan>;

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,9 @@ def ASM : DiagGroup<"asm", [
11941194
ASMOperandWidths
11951195
]>;
11961196

1197+
// Linker warnings.
1198+
def LinkerWarnings : DiagGroup<"linker-warnings">;
1199+
11971200
// OpenMP warnings.
11981201
def SourceUsesOpenMP : DiagGroup<"source-uses-openmp">;
11991202
def OpenMPClauses : DiagGroup<"openmp-clauses">;

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ namespace clang {
160160
const PreprocessorOptions &PPOpts,
161161
const CodeGenOptions &CodeGenOpts,
162162
const TargetOptions &TargetOpts,
163-
const LangOptions &LangOpts,
163+
const LangOptions &LangOpts, llvm::Module *Module,
164164
SmallVector<LinkModule, 4> LinkModules, LLVMContext &C,
165165
CoverageSourceInfo *CoverageInfo = nullptr)
166166
: Diags(Diags), Action(Action), HeaderSearchOpts(HeaderSearchOpts),
@@ -170,7 +170,7 @@ namespace clang {
170170
LLVMIRGenerationRefCount(0),
171171
Gen(CreateLLVMCodeGen(Diags, "", HeaderSearchOpts, PPOpts,
172172
CodeGenOpts, C, CoverageInfo)),
173-
LinkModules(std::move(LinkModules)) {
173+
LinkModules(std::move(LinkModules)), CurLinkModule(Module) {
174174
TimerIsEnabled = CodeGenOpts.TimePasses;
175175
llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
176176
llvm::TimePassesPerRun = CodeGenOpts.TimePassesPerRun;
@@ -779,11 +779,7 @@ void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
779779
ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
780780
break;
781781
case DK_Linker:
782-
assert(CurLinkModule);
783-
// FIXME: stop eating the warnings and notes.
784-
if (Severity != DS_Error)
785-
return;
786-
DiagID = diag::err_fe_cannot_link_module;
782+
ComputeDiagID(Severity, linking_module, DiagID);
787783
break;
788784
case llvm::DK_OptimizationRemark:
789785
// Optimization remarks are always handled completely by this
@@ -845,9 +841,9 @@ void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
845841
DI.print(DP);
846842
}
847843

848-
if (DiagID == diag::err_fe_cannot_link_module) {
849-
Diags.Report(diag::err_fe_cannot_link_module)
850-
<< CurLinkModule->getModuleIdentifier() << MsgStorage;
844+
if (DI.getKind() == DK_Linker) {
845+
assert(CurLinkModule && "CurLinkModule must be set for linker diagnostics");
846+
Diags.Report(DiagID) << CurLinkModule->getModuleIdentifier() << MsgStorage;
851847
return;
852848
}
853849

@@ -1088,7 +1084,7 @@ void CodeGenAction::ExecuteAction() {
10881084
// BackendConsumer.
10891085
BackendConsumer Result(BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
10901086
CI.getPreprocessorOpts(), CI.getCodeGenOpts(),
1091-
CI.getTargetOpts(), CI.getLangOpts(),
1087+
CI.getTargetOpts(), CI.getLangOpts(), TheModule.get(),
10921088
std::move(LinkModules), *VMContext, nullptr);
10931089
// PR44896: Force DiscardValueNames as false. DiscardValueNames cannot be
10941090
// true here because the valued names are needed for reading textual IR.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
target triple = "armv4-none-unknown-eabi"
2+
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
3+
4+
declare i32 @foo(i32)
5+
6+
define i32 @bar(i32 %x) {
7+
%1 = tail call i32 @foo(i32 %x)
8+
ret i32 %1
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: mkdir -p %t
2+
; RUN: opt -module-summary -o %t/foo.o %s
3+
; RUN: opt -module-summary -o %t/bar.o %S/Inputs/linker-diagnostic1.ll
4+
; RUN: llvm-lto2 run --thinlto-distributed-indexes -r %t/foo.o,foo,plx -r %t/bar.o,bar,plx \
5+
; RUN: -r %t/bar.o,foo, -o %t/foobar.so %t/foo.o %t/bar.o
6+
; RUN: %clang -c -o %t/lto.bar.o --target=armv4-none-unknown-eabi -O2 \
7+
; RUN: -fthinlto-index=%t/bar.o.thinlto.bc %t/bar.o -Wno-override-module 2>&1 | FileCheck %s
8+
9+
; CHECK: linking module '{{.*}}/bar.o': Linking two modules of different target triples: '{{.*}}/foo.o' is 'thumbv6-unknown-linux-gnueabihf' whereas '{{.*}}/bar.o' is 'armv4-none-unknown-eabi'
10+
11+
target triple = "thumbv6-unknown-linux-gnueabihf"
12+
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
13+
14+
define i32 @foo(i32 %x) {
15+
%1 = add i32 %x, 1
16+
ret i32 %1
17+
}

clang/test/Misc/serialized-diags-driver.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// RUN: %clang -Wx-typoed-warning -Wall -fsyntax-only --serialize-diagnostics %t.diag %s
99
// RUN: c-index-test -read-diagnostics %t.diag 2>&1 | FileCheck %s
1010

11-
// CHECK: warning: unknown warning option '-Wx-typoed-warning' [-Wunknown-warning-option] []
11+
// CHECK: warning: unknown warning option '-Wx-typoed-warning'
12+
// CHECK-SAME: [-Wunknown-warning-option] []
1213

1314
// CHECK: warning: variable 'voodoo' is uninitialized when used here [-Wuninitialized]
1415
// CHECK: note: initialize the variable 'voodoo' to silence this warning []

0 commit comments

Comments
 (0)