[cfe-commits] r74629 - in /cfe/trunk: include/clang/Basic/DiagnosticDriverKinds.td include/clang/Driver/Action.h include/clang/Driver/Compilation.h lib/Driver/Compilation.cpp

Daniel Dunbar daniel at zuster.org
Wed Jul 1 12:14:53 PDT 2009


Author: ddunbar
Date: Wed Jul  1 14:14:39 2009
New Revision: 74629

URL: http://llvm.org/viewvc/llvm-project?rev=74629&view=rev
Log:
Driver: Improve diagnostics for failed commands.
 - Not all tools give good error messages, and sometimes the tool can fail w/o
   any error (for example, when signalled).

 - We suppress this message when the failing command is the compiler and it
   failed normally (exit code == 1), under the assumption that it gave a good
   diagnostic.

For example, for a linker failure we now get:
--
ddunbar at lordcrumb:tmp$ clang a.c b.c
ld: duplicate symbol _x in /var/folders/cl/clrOX6SaG+moCeRKEI4PtU+++TI/-Tmp-/cc-bXYITq.o and /var/folders/cl/clrOX6SaG+moCeRKEI4PtU+++TI/-Tmp-/cc-6uK4jD.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
--

For a compiler crash we get:
--
ddunbar at lordcrumb:tmp$ clang t.i
Assertion failed: (CGT.getTargetData().getTypeAllocSizeInBits(STy) == RL.getSize()), function layoutStructFields, file CodeGenTypes.cpp, line 573.
0   clang-cc          0x0000000100f1f1f1 PrintStackTrace(void*) + 38
.. stack trace and virtual stack trace follow ...
clang: error: compiler command failed due to signal 6 (use -v to see invocation)
--

But for a regular compilation failure we get the usual:
--
ddunbar at lordcrumb:tmp$ clang c.c
c.c:1:6: error: invalid token after top level declarator
int x
     ^
1 diagnostic generated.
--

 - No test case, not amenable to non-executable testing. :/

 - <rdar://problem/6945613>

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td
    cfe/trunk/include/clang/Driver/Action.h
    cfe/trunk/include/clang/Driver/Compilation.h
    cfe/trunk/lib/Driver/Compilation.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td?rev=74629&r1=74628&r2=74629&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticDriverKinds.td Wed Jul  1 14:14:39 2009
@@ -45,6 +45,10 @@
   "'%0': unable to pass LLVM bit-code files to linker">;
 def err_drv_clang_unsupported : Error<
   "the clang compiler does not support '%0'">;
+def err_drv_command_failed : Error<
+  "%0 command failed with exit code %1 (use -v to see invocation)">;
+def err_drv_command_signalled : Error<
+  "%0 command failed due to signal %1 (use -v to see invocation)">;
 
 def warn_drv_input_file_unused : Warning<
   "%0: '%1' input unused when '%2' is present">;

Modified: cfe/trunk/include/clang/Driver/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Action.h?rev=74629&r1=74628&r2=74629&view=diff

==============================================================================
--- cfe/trunk/include/clang/Driver/Action.h (original)
+++ cfe/trunk/include/clang/Driver/Action.h Wed Jul  1 14:14:39 2009
@@ -75,6 +75,8 @@
 public:
   virtual ~Action();
 
+  const char *getClassName() const { return Action::getClassName(getKind()); }
+
   ActionClass getKind() const { return Kind; }
   types::ID getType() const { return Type; }
 

Modified: cfe/trunk/include/clang/Driver/Compilation.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Compilation.h?rev=74629&r1=74628&r2=74629&view=diff

==============================================================================
--- cfe/trunk/include/clang/Driver/Compilation.h (original)
+++ cfe/trunk/include/clang/Driver/Compilation.h Wed Jul  1 14:14:39 2009
@@ -103,22 +103,26 @@
 
   /// PrintJob - Print one job in -### format.
   ///
-  /// OS - The stream to print on.
-  /// J - The job to print.
-  /// Terminator - A string to print at the end of the line.
-  /// Quote - Should separate arguments be quoted.
+  /// \param OS - The stream to print on.
+  /// \param J - The job to print.
+  /// \param Terminator - A string to print at the end of the line.
+  /// \param Quote - Should separate arguments be quoted.
   void PrintJob(llvm::raw_ostream &OS, const Job &J, 
                 const char *Terminator, bool Quote) const;
 
   /// ExecuteCommand - Execute an actual command.
   ///
+  /// \param FailingCommand - For non-zero results, this will be set to the
+  /// Command which failed, if any.
   /// \return The result code of the subprocess.
-  int ExecuteCommand(const Command &C) const;
+  int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
 
   /// ExecuteJob - Execute a single job.
   ///
+  /// \param FailingCommand - For non-zero results, this will be set to the
+  /// Command which failed, if any.
   /// \return The accumulated result code of the job.
-  int ExecuteJob(const Job &J) const;
+  int ExecuteJob(const Job &J, const Command *&FailingCommand) const;
 };
 
 } // end namespace driver

Modified: cfe/trunk/lib/Driver/Compilation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Compilation.cpp?rev=74629&r1=74628&r2=74629&view=diff

==============================================================================
--- cfe/trunk/lib/Driver/Compilation.cpp (original)
+++ cfe/trunk/lib/Driver/Compilation.cpp Wed Jul  1 14:14:39 2009
@@ -105,7 +105,8 @@
   return Success;
 }
 
-int Compilation::ExecuteCommand(const Command &C) const {
+int Compilation::ExecuteCommand(const Command &C,
+                                const Command *&FailingCommand) const {
   llvm::sys::Path Prog(C.getExecutable());
   const char **Argv = new const char*[C.getArguments().size() + 2];
   Argv[0] = C.getExecutable();
@@ -126,25 +127,30 @@
     getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
   }
   
+  if (Res)
+    FailingCommand = &C;
+
   delete[] Argv;
   return Res;
 }
 
-int Compilation::ExecuteJob(const Job &J) const {
+int Compilation::ExecuteJob(const Job &J, 
+                            const Command *&FailingCommand) const {
   if (const Command *C = dyn_cast<Command>(&J)) {
-    return ExecuteCommand(*C);
+    return ExecuteCommand(*C, FailingCommand);
   } else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
     // Piped commands with a single job are easy.
     if (PJ->size() == 1)
-      return ExecuteCommand(**PJ->begin());
+      return ExecuteCommand(**PJ->begin(), FailingCommand);
       
+    FailingCommand = *PJ->begin();
     getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
     return 1;
   } else {
     const JobList *Jobs = cast<JobList>(&J);
     for (JobList::const_iterator 
            it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
-      if (int Res = ExecuteJob(**it))
+      if (int Res = ExecuteJob(**it, FailingCommand))
         return Res;
     return 0;
   }
@@ -161,7 +167,8 @@
   if (getDriver().getDiags().getNumErrors())
     return 1;
 
-  int Res = ExecuteJob(Jobs);
+  const Command *FailingCommand = 0;
+  int Res = ExecuteJob(Jobs, FailingCommand);
   
   // Remove temp files.
   CleanupFileList(TempFiles);
@@ -170,5 +177,29 @@
   if (Res != 0 && !getArgs().hasArg(options::OPT_save_temps))
     CleanupFileList(ResultFiles, true);
 
+  // Print extra information about abnormal failures, if possible.
+  if (Res) {
+    // This is ad-hoc, but we don't want to be excessively noisy. If the result
+    // status was 1, assume the command failed normally. In particular, if it
+    // was the compiler then assume it gave a reasonable error code. Failures in
+    // other tools are less common, and they generally have worse diagnostics,
+    // so always print the diagnostic there.
+    const Action &Source = FailingCommand->getSource();
+    bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
+                           isa<PrecompileJobAction>(Source) ||
+                           isa<AnalyzeJobAction>(Source) ||
+                           isa<CompileJobAction>(Source));
+
+    if (!IsFriendlyTool || Res != 1) {
+      // FIXME: See FIXME above regarding result code interpretation.
+      if (Res < 0)
+        getDriver().Diag(clang::diag::err_drv_command_signalled) 
+          << Source.getClassName() << -Res;
+      else
+        getDriver().Diag(clang::diag::err_drv_command_failed) 
+          << Source.getClassName() << Res;
+    }
+  }
+
   return Res;
 }





More information about the cfe-commits mailing list