Skip to content

Commit 7b9c8f7

Browse files
committed
[NewPM] Set diagnostic handler in MachineModuleAnalysis
`setDiagnosticHandler` is idempotent.
1 parent f546b6e commit 7b9c8f7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ static unsigned getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
213213
bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
214214
MMI.initialize();
215215
MMI.TheModule = &M;
216-
// FIXME: Do this for new pass manager.
217216
LLVMContext &Ctx = M.getContext();
218217
MMI.getContext().setDiagnosticHandler(
219218
[&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
@@ -240,6 +239,17 @@ AnalysisKey MachineModuleAnalysis::Key;
240239
MachineModuleAnalysis::Result
241240
MachineModuleAnalysis::run(Module &M, ModuleAnalysisManager &) {
242241
MMI.TheModule = &M;
242+
LLVMContext &Ctx = M.getContext();
243+
MMI.getContext().setDiagnosticHandler(
244+
[&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
245+
const SourceMgr &SrcMgr,
246+
std::vector<const MDNode *> &LocInfos) {
247+
unsigned LocCookie = 0;
248+
if (IsInlineAsm)
249+
LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
250+
Ctx.diagnose(
251+
DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
252+
});
243253
MMI.DbgInfoAvailable =
244254
!DisableDebugInfoPrinting && !M.debug_compile_units().empty();
245255
return Result(MMI);

llvm/unittests/CodeGen/PassManagerTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ struct TestMachineModulePass : public PassInfoMixin<TestMachineModulePass> {
125125
std::vector<int> &Counts;
126126
};
127127

128+
struct ReportWarningPass : public PassInfoMixin<ReportWarningPass> {
129+
PreservedAnalyses run(MachineFunction &MF,
130+
MachineFunctionAnalysisManager &MFAM) {
131+
auto &Ctx = MF.getContext();
132+
Ctx.reportWarning(SMLoc(), "Test warning message.");
133+
return PreservedAnalyses::all();
134+
}
135+
};
136+
128137
std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) {
129138
SMDiagnostic Err;
130139
return parseAssemblyString(IR, Err, Context);
@@ -202,12 +211,17 @@ TEST_F(PassManagerTest, Basic) {
202211
TestMachineFunctionPass(Count, Counts)));
203212
MPM.addPass(TestMachineModulePass(Count, Counts));
204213
MFPM.addPass(TestMachineFunctionPass(Count, Counts));
214+
MFPM.addPass(ReportWarningPass());
205215
MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
206216

217+
testing::internal::CaptureStderr();
207218
MPM.run(*M, MAM);
219+
std::string Output = testing::internal::GetCapturedStderr();
208220

209221
EXPECT_EQ((std::vector<int>{10, 16, 18, 20, 30, 36, 38, 40}), Counts);
210222
EXPECT_EQ(40, Count);
223+
EXPECT_TRUE(Output.find("warning: <unknown>:0: Test warning message.") !=
224+
std::string::npos);
211225
}
212226

213227
} // namespace

0 commit comments

Comments
 (0)