@@ -82,6 +82,24 @@ namespace clang {
82
82
BackendConsumer *BackendCon;
83
83
};
84
84
85
+ static void reportOptRecordError (Error E, DiagnosticsEngine &Diags,
86
+ const CodeGenOptions CodeGenOpts) {
87
+ handleAllErrors (
88
+ std::move (E),
89
+ [&](const RemarkSetupFileError &E) {
90
+ Diags.Report (diag::err_cannot_open_file)
91
+ << CodeGenOpts.OptRecordFile << E.message ();
92
+ },
93
+ [&](const RemarkSetupPatternError &E) {
94
+ Diags.Report (diag::err_drv_optimization_remark_pattern)
95
+ << E.message () << CodeGenOpts.OptRecordPasses ;
96
+ },
97
+ [&](const RemarkSetupFormatError &E) {
98
+ Diags.Report (diag::err_drv_optimization_remark_format)
99
+ << CodeGenOpts.OptRecordFormat ;
100
+ });
101
+ }
102
+
85
103
class BackendConsumer : public ASTConsumer {
86
104
using LinkModule = CodeGenAction::LinkModule;
87
105
@@ -268,29 +286,16 @@ namespace clang {
268
286
CodeGenOpts, this ));
269
287
270
288
Expected<std::unique_ptr<llvm::ToolOutputFile>> OptRecordFileOrErr =
271
- setupOptimizationRemarks (Ctx, CodeGenOpts.OptRecordFile ,
272
- CodeGenOpts.OptRecordPasses ,
273
- CodeGenOpts.OptRecordFormat ,
274
- CodeGenOpts.DiagnosticsWithHotness ,
275
- CodeGenOpts.DiagnosticsHotnessThreshold );
289
+ setupOptimizationRemarks (
290
+ Ctx, CodeGenOpts.OptRecordFile , CodeGenOpts.OptRecordPasses ,
291
+ CodeGenOpts.OptRecordFormat , CodeGenOpts.DiagnosticsWithHotness ,
292
+ CodeGenOpts.DiagnosticsHotnessThreshold );
276
293
277
294
if (Error E = OptRecordFileOrErr.takeError ()) {
278
- handleAllErrors (
279
- std::move (E),
280
- [&](const RemarkSetupFileError &E) {
281
- Diags.Report (diag::err_cannot_open_file)
282
- << CodeGenOpts.OptRecordFile << E.message ();
283
- },
284
- [&](const RemarkSetupPatternError &E) {
285
- Diags.Report (diag::err_drv_optimization_remark_pattern)
286
- << E.message () << CodeGenOpts.OptRecordPasses ;
287
- },
288
- [&](const RemarkSetupFormatError &E) {
289
- Diags.Report (diag::err_drv_optimization_remark_format)
290
- << CodeGenOpts.OptRecordFormat ;
291
- });
295
+ reportOptRecordError (std::move (E), Diags, CodeGenOpts);
292
296
return ;
293
297
}
298
+
294
299
std::unique_ptr<llvm::ToolOutputFile> OptRecordFile =
295
300
std::move (*OptRecordFileOrErr);
296
301
@@ -1046,6 +1051,8 @@ void CodeGenAction::ExecuteAction() {
1046
1051
if (getCurrentFileKind ().getLanguage () == Language::LLVM_IR) {
1047
1052
BackendAction BA = static_cast <BackendAction>(Act);
1048
1053
CompilerInstance &CI = getCompilerInstance ();
1054
+ auto &CodeGenOpts = CI.getCodeGenOpts ();
1055
+ auto &Diagnostics = CI.getDiagnostics ();
1049
1056
std::unique_ptr<raw_pwrite_stream> OS =
1050
1057
GetOutputStream (CI, getCurrentFile (), BA);
1051
1058
if (BA != Backend_EmitNothing && !OS)
@@ -1064,23 +1071,41 @@ void CodeGenAction::ExecuteAction() {
1064
1071
1065
1072
const TargetOptions &TargetOpts = CI.getTargetOpts ();
1066
1073
if (TheModule->getTargetTriple () != TargetOpts.Triple ) {
1067
- CI. getDiagnostics () .Report (SourceLocation (),
1068
- diag::warn_fe_override_module)
1074
+ Diagnostics .Report (SourceLocation (),
1075
+ diag::warn_fe_override_module)
1069
1076
<< TargetOpts.Triple ;
1070
1077
TheModule->setTargetTriple (TargetOpts.Triple );
1071
1078
}
1072
1079
1073
- EmbedBitcode (TheModule.get (), CI. getCodeGenOpts () ,
1080
+ EmbedBitcode (TheModule.get (), CodeGenOpts ,
1074
1081
MainFile->getMemBufferRef ());
1075
1082
1076
1083
LLVMContext &Ctx = TheModule->getContext ();
1077
1084
Ctx.setInlineAsmDiagnosticHandler (BitcodeInlineAsmDiagHandler,
1078
- &CI.getDiagnostics ());
1085
+ &Diagnostics);
1086
+
1087
+ Expected<std::unique_ptr<llvm::ToolOutputFile>> OptRecordFileOrErr =
1088
+ setupOptimizationRemarks (
1089
+ Ctx, CodeGenOpts.OptRecordFile ,
1090
+ CodeGenOpts.OptRecordPasses ,
1091
+ CodeGenOpts.OptRecordFormat ,
1092
+ CodeGenOpts.DiagnosticsWithHotness ,
1093
+ CodeGenOpts.DiagnosticsHotnessThreshold );
1094
+
1095
+ if (Error E = OptRecordFileOrErr.takeError ()) {
1096
+ reportOptRecordError (std::move (E), Diagnostics, CodeGenOpts);
1097
+ return ;
1098
+ }
1099
+ std::unique_ptr<llvm::ToolOutputFile> OptRecordFile =
1100
+ std::move (*OptRecordFileOrErr);
1079
1101
1080
- EmitBackendOutput (CI. getDiagnostics () , CI.getHeaderSearchOpts (),
1081
- CI. getCodeGenOpts () , TargetOpts, CI.getLangOpts (),
1102
+ EmitBackendOutput (Diagnostics , CI.getHeaderSearchOpts (),
1103
+ CodeGenOpts , TargetOpts, CI.getLangOpts (),
1082
1104
CI.getTarget ().getDataLayout (), TheModule.get (), BA,
1083
1105
std::move (OS));
1106
+
1107
+ if (OptRecordFile)
1108
+ OptRecordFile->keep ();
1084
1109
return ;
1085
1110
}
1086
1111
0 commit comments