]> granicus.if.org Git - clang/commitdiff
Fix CompilerInstance::createOutputFile to use proper diagnostics, and (try to) update...
authorDaniel Dunbar <daniel@zuster.org>
Thu, 3 Dec 2009 09:13:30 +0000 (09:13 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Thu, 3 Dec 2009 09:13:30 +0000 (09:13 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90437 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticFrontendKinds.td
include/clang/Frontend/CompilerInstance.h
lib/Frontend/CompilerInstance.cpp
lib/Frontend/FrontendActions.cpp

index 835f57b13be867ee5a17d72b3a910ee42eefdae7..c7033561c1ea6ceb48d52a0df8c13202c7974784 100644 (file)
@@ -53,6 +53,8 @@ def err_fe_pch_malformed_block : Error<
     "malformed block record in PCH file: '%0'">;
 def err_fe_pch_error_at_end_block : Error<
     "error at end of module block in PCH file: '%0'">;
+def err_fe_unable_to_open_output : Error<
+    "unable to to open output file '%0': '%1'">;
 
 def err_verify_bogus_characters : Error<
     "bogus characters before '{{' in expected string">;
index 007006d493b488ef0c660467b4fd0b83de97dea9..27153b63c839988d3d95b24ef1d963ab84b93a2d 100644 (file)
@@ -482,12 +482,16 @@ public:
 
   /// Create the default output file (from the invocation's options) and add it
   /// to the list of tracked output files.
+  ///
+  /// \return - Null on error.
   llvm::raw_fd_ostream *
   createDefaultOutputFile(bool Binary = true, llvm::StringRef BaseInput = "",
                           llvm::StringRef Extension = "");
 
   /// Create a new output file and add it to the list of tracked output files,
   /// optionally deriving the output path name.
+  ///
+  /// \return - Null on error.
   llvm::raw_fd_ostream *
   createOutputFile(llvm::StringRef OutputPath, bool Binary = true,
                    llvm::StringRef BaseInput = "",
index b6c79fb7bc465fd4d338b9de80e9e5b6a2cb7250..f36a56032b29dcc95ca37c83cec5de4c5ca0f368 100644 (file)
@@ -328,9 +328,9 @@ CompilerInstance::createOutputFile(llvm::StringRef OutputPath,
                                               InFile, Extension,
                                               &OutputPathName);
   if (!OS) {
-    // FIXME: Don't fail this way.
-    llvm::errs() << "error: " << Error << "\n";
-    ::exit(1);
+    getDiagnostics().Report(diag::err_fe_unable_to_open_output)
+      << OutputPath << Error;
+    return 0;
   }
 
   // Add the output file -- but don't try to remove "-", since this means we are
index 27e194e6f10821a68d2b20f60ddf390bbeb48205..e3c313a422991d2b034b4539bbe674f81322f93d 100644 (file)
@@ -35,13 +35,16 @@ ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI,
 
 ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
                                                llvm::StringRef InFile) {
-  return CreateASTPrinter(CI.createDefaultOutputFile(false, InFile));
+  if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
+    return CreateASTPrinter(OS);
+  return 0;
 }
 
 ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
                                                   llvm::StringRef InFile) {
-  return CreateASTPrinterXML(CI.createDefaultOutputFile(false, InFile,
-                                                        "xml"));
+  if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
+    return CreateASTPrinterXML(OS);
+  return 0;
 }
 
 ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
@@ -74,6 +77,9 @@ ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
   }
 
   llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
+  if (!OS)
+    return 0;
+
   if (CI.getFrontendOpts().RelocatablePCH)
     return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str());
 
@@ -82,8 +88,9 @@ ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
 
 ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
                                                 llvm::StringRef InFile) {
-  return CreateHTMLPrinter(CI.createDefaultOutputFile(false, InFile),
-                           CI.getPreprocessor());
+  if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
+    return CreateHTMLPrinter(OS, CI.getPreprocessor());
+  return 0;
 }
 
 ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
@@ -140,10 +147,11 @@ void FixItAction::EndSourceFileAction() {
 
 ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
                                                   llvm::StringRef InFile) {
-  return CreateObjCRewriter(InFile,
-                            CI.createDefaultOutputFile(true, InFile, "cpp"),
-                            CI.getDiagnostics(), CI.getLangOpts(),
-                            CI.getDiagnosticOpts().NoRewriteMacros);
+  if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
+    return CreateObjCRewriter(InFile, OS,
+                              CI.getDiagnostics(), CI.getLangOpts(),
+                              CI.getDiagnosticOpts().NoRewriteMacros);
+  return 0;
 }
 
 ASTConsumer *RewriteBlocksAction::CreateASTConsumer(CompilerInstance &CI,
@@ -162,12 +170,21 @@ ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
                                               llvm::StringRef InFile) {
   BackendAction BA = static_cast<BackendAction>(Act);
   llvm::OwningPtr<llvm::raw_ostream> OS;
-  if (BA == Backend_EmitAssembly)
+  switch (BA) {
+  case Backend_EmitAssembly:
     OS.reset(CI.createDefaultOutputFile(false, InFile, "s"));
-  else if (BA == Backend_EmitLL)
+    break;
+  case Backend_EmitLL:
     OS.reset(CI.createDefaultOutputFile(false, InFile, "ll"));
-  else if (BA == Backend_EmitBC)
+    break;
+  case Backend_EmitBC:
     OS.reset(CI.createDefaultOutputFile(true, InFile, "bc"));
+    break;
+  case Backend_EmitNothing:
+    break;
+  }
+  if (BA != Backend_EmitNothing && !OS)
+    return 0;
 
   return CreateBackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(),
                                CI.getCodeGenOpts(), CI.getTargetOpts(),
@@ -228,6 +245,8 @@ void GeneratePTHAction::ExecuteAction() {
   }
   llvm::raw_fd_ostream *OS =
     CI.createDefaultOutputFile(true, getCurrentFile());
+  if (!OS) return;
+
   CacheTokens(CI.getPreprocessor(), OS);
 }
 
@@ -255,6 +274,8 @@ void PrintParseAction::ExecuteAction() {
   CompilerInstance &CI = getCompilerInstance();
   Preprocessor &PP = getCompilerInstance().getPreprocessor();
   llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
+  if (!OS) return;
+
   llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
 
   Parser P(PP, *PA);
@@ -265,6 +286,8 @@ void PrintParseAction::ExecuteAction() {
 void PrintPreprocessedAction::ExecuteAction() {
   CompilerInstance &CI = getCompilerInstance();
   llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
+  if (!OS) return;
+
   DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
                            CI.getPreprocessorOutputOpts());
 }
@@ -272,11 +295,15 @@ void PrintPreprocessedAction::ExecuteAction() {
 void RewriteMacrosAction::ExecuteAction() {
   CompilerInstance &CI = getCompilerInstance();
   llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
+  if (!OS) return;
+
   RewriteMacrosInInput(CI.getPreprocessor(), OS);
 }
 
 void RewriteTestAction::ExecuteAction() {
   CompilerInstance &CI = getCompilerInstance();
   llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
+  if (!OS) return;
+
   DoRewriteTest(CI.getPreprocessor(), OS);
 }