Skip to content

Commit 7554699

Browse files
committed
Fix CompilerInstance::createOutputFile to use proper diagnostics, and (try to) update all clients to be able to handle failure.
llvm-svn: 90437
1 parent 692bc47 commit 7554699

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def err_fe_pch_malformed_block : Error<
5353
"malformed block record in PCH file: '%0'">;
5454
def err_fe_pch_error_at_end_block : Error<
5555
"error at end of module block in PCH file: '%0'">;
56+
def err_fe_unable_to_open_output : Error<
57+
"unable to to open output file '%0': '%1'">;
5658

5759
def err_verify_bogus_characters : Error<
5860
"bogus characters before '{{' in expected string">;

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,16 @@ class CompilerInstance {
482482

483483
/// Create the default output file (from the invocation's options) and add it
484484
/// to the list of tracked output files.
485+
///
486+
/// \return - Null on error.
485487
llvm::raw_fd_ostream *
486488
createDefaultOutputFile(bool Binary = true, llvm::StringRef BaseInput = "",
487489
llvm::StringRef Extension = "");
488490

489491
/// Create a new output file and add it to the list of tracked output files,
490492
/// optionally deriving the output path name.
493+
///
494+
/// \return - Null on error.
491495
llvm::raw_fd_ostream *
492496
createOutputFile(llvm::StringRef OutputPath, bool Binary = true,
493497
llvm::StringRef BaseInput = "",

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
328328
InFile, Extension,
329329
&OutputPathName);
330330
if (!OS) {
331-
// FIXME: Don't fail this way.
332-
llvm::errs() << "error: " << Error << "\n";
333-
::exit(1);
331+
getDiagnostics().Report(diag::err_fe_unable_to_open_output)
332+
<< OutputPath << Error;
333+
return 0;
334334
}
335335

336336
// Add the output file -- but don't try to remove "-", since this means we are

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI,
3535

3636
ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
3737
llvm::StringRef InFile) {
38-
return CreateASTPrinter(CI.createDefaultOutputFile(false, InFile));
38+
if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
39+
return CreateASTPrinter(OS);
40+
return 0;
3941
}
4042

4143
ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
4244
llvm::StringRef InFile) {
43-
return CreateASTPrinterXML(CI.createDefaultOutputFile(false, InFile,
44-
"xml"));
45+
if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
46+
return CreateASTPrinterXML(OS);
47+
return 0;
4548
}
4649

4750
ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
@@ -74,6 +77,9 @@ ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
7477
}
7578

7679
llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
80+
if (!OS)
81+
return 0;
82+
7783
if (CI.getFrontendOpts().RelocatablePCH)
7884
return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str());
7985

@@ -82,8 +88,9 @@ ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
8288

8389
ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
8490
llvm::StringRef InFile) {
85-
return CreateHTMLPrinter(CI.createDefaultOutputFile(false, InFile),
86-
CI.getPreprocessor());
91+
if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
92+
return CreateHTMLPrinter(OS, CI.getPreprocessor());
93+
return 0;
8794
}
8895

8996
ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
@@ -140,10 +147,11 @@ void FixItAction::EndSourceFileAction() {
140147

141148
ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
142149
llvm::StringRef InFile) {
143-
return CreateObjCRewriter(InFile,
144-
CI.createDefaultOutputFile(true, InFile, "cpp"),
145-
CI.getDiagnostics(), CI.getLangOpts(),
146-
CI.getDiagnosticOpts().NoRewriteMacros);
150+
if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
151+
return CreateObjCRewriter(InFile, OS,
152+
CI.getDiagnostics(), CI.getLangOpts(),
153+
CI.getDiagnosticOpts().NoRewriteMacros);
154+
return 0;
147155
}
148156

149157
ASTConsumer *RewriteBlocksAction::CreateASTConsumer(CompilerInstance &CI,
@@ -162,12 +170,21 @@ ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
162170
llvm::StringRef InFile) {
163171
BackendAction BA = static_cast<BackendAction>(Act);
164172
llvm::OwningPtr<llvm::raw_ostream> OS;
165-
if (BA == Backend_EmitAssembly)
173+
switch (BA) {
174+
case Backend_EmitAssembly:
166175
OS.reset(CI.createDefaultOutputFile(false, InFile, "s"));
167-
else if (BA == Backend_EmitLL)
176+
break;
177+
case Backend_EmitLL:
168178
OS.reset(CI.createDefaultOutputFile(false, InFile, "ll"));
169-
else if (BA == Backend_EmitBC)
179+
break;
180+
case Backend_EmitBC:
170181
OS.reset(CI.createDefaultOutputFile(true, InFile, "bc"));
182+
break;
183+
case Backend_EmitNothing:
184+
break;
185+
}
186+
if (BA != Backend_EmitNothing && !OS)
187+
return 0;
171188

172189
return CreateBackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(),
173190
CI.getCodeGenOpts(), CI.getTargetOpts(),
@@ -228,6 +245,8 @@ void GeneratePTHAction::ExecuteAction() {
228245
}
229246
llvm::raw_fd_ostream *OS =
230247
CI.createDefaultOutputFile(true, getCurrentFile());
248+
if (!OS) return;
249+
231250
CacheTokens(CI.getPreprocessor(), OS);
232251
}
233252

@@ -255,6 +274,8 @@ void PrintParseAction::ExecuteAction() {
255274
CompilerInstance &CI = getCompilerInstance();
256275
Preprocessor &PP = getCompilerInstance().getPreprocessor();
257276
llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
277+
if (!OS) return;
278+
258279
llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
259280

260281
Parser P(PP, *PA);
@@ -265,18 +286,24 @@ void PrintParseAction::ExecuteAction() {
265286
void PrintPreprocessedAction::ExecuteAction() {
266287
CompilerInstance &CI = getCompilerInstance();
267288
llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
289+
if (!OS) return;
290+
268291
DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
269292
CI.getPreprocessorOutputOpts());
270293
}
271294

272295
void RewriteMacrosAction::ExecuteAction() {
273296
CompilerInstance &CI = getCompilerInstance();
274297
llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
298+
if (!OS) return;
299+
275300
RewriteMacrosInInput(CI.getPreprocessor(), OS);
276301
}
277302

278303
void RewriteTestAction::ExecuteAction() {
279304
CompilerInstance &CI = getCompilerInstance();
280305
llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
306+
if (!OS) return;
307+
281308
DoRewriteTest(CI.getPreprocessor(), OS);
282309
}

0 commit comments

Comments
 (0)