]> granicus.if.org Git - clang/commitdiff
Driver: Support CC_PRINT_OPTIONS, used for logging the compile commands (in -v style...
authorDaniel Dunbar <daniel@zuster.org>
Sat, 20 Mar 2010 08:01:59 +0000 (08:01 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Sat, 20 Mar 2010 08:01:59 +0000 (08:01 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@99054 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticDriverKinds.td
include/clang/Driver/Driver.h
lib/Driver/Compilation.cpp
lib/Driver/Driver.cpp
test/Driver/cc-print-options.c [new file with mode: 0644]
tools/driver/driver.cpp

index 2bce12db535bb4abc49cc2a957b7c3828959bc84..3dbe47f4cf549a03373df7a74901994330b017ff 100644 (file)
@@ -64,6 +64,8 @@ def err_drv_invalid_remap_file : Error<
     "invalid option '%0' not of the form <from-file>;<to-file>">;
 def err_drv_invalid_gcc_output_type : Error<
     "invalid output type '%0' for use with gcc tool">;
+def err_drv_cc_print_options_failure : Error<
+    "unable to open CC_PRINT_OPTIONS file: %0">;
 
 def warn_drv_input_file_unused : Warning<
   "%0: '%1' input unused when '%2' is present">;
index 59c3946a2cb5fe04a0b117064b42e9eb4b9ae682..f49c3b97c1c422e667c6304ec57f573961359c2b 100644 (file)
@@ -83,6 +83,9 @@ public:
   /// Name to use when calling the generic gcc.
   std::string CCCGenericGCCName;
 
+  /// The file to log CC_PRINT_OPTIONS output to, if enabled.
+  const char *CCPrintOptionsFilename;
+
   /// Whether the driver should follow g++ like behavior.
   unsigned CCCIsCXX : 1;
 
@@ -92,6 +95,10 @@ public:
   /// Only print tool bindings, don't build any jobs.
   unsigned CCCPrintBindings : 1;
 
+  /// Set CC_PRINT_OPTIONS mode, which is like -v but logs the commands to
+  /// CCPrintOptionsFilename or to stderr.
+  unsigned CCPrintOptions : 1;
+
 private:
   /// Whether to check that input files exist when constructing compilation
   /// jobs.
index 98c637442589e95201b72c3f39029f138eeb84fd..227f79a75b7184a8431db06b00df116321c94a03 100644 (file)
@@ -134,8 +134,34 @@ int Compilation::ExecuteCommand(const Command &C,
   std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
   Argv[C.getArguments().size() + 1] = 0;
 
-  if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v))
-    PrintJob(llvm::errs(), C, "\n", false);
+  if (getDriver().CCCEcho || getDriver().CCPrintOptions ||
+      getArgs().hasArg(options::OPT_v)) {
+    llvm::raw_ostream *OS = &llvm::errs();
+
+    // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
+    // output stream.
+    if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
+      std::string Error;
+      OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
+                                    Error,
+                                    llvm::raw_fd_ostream::F_Append);
+      if (!Error.empty()) {
+        getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
+          << Error;
+        FailingCommand = &C;
+        delete OS;
+        return 1;
+      }
+    }
+
+    if (getDriver().CCPrintOptions)
+      *OS << "[Logging clang options]";
+
+    PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
+
+    if (OS != &llvm::errs())
+      delete OS;
+  }
 
   std::string Error;
   int Res =
index 3257ee55a7b9a65a5bfc0275d818bd9ef18e2885..acfff386f485827b28380006b9cb99d9af2c7f32 100644 (file)
@@ -51,10 +51,10 @@ Driver::Driver(llvm::StringRef _Name, llvm::StringRef _Dir,
     DefaultImageName(_DefaultImageName),
     DriverTitle("clang \"gcc-compatible\" driver"),
     Host(0),
-    CCCGenericGCCName("gcc"), CCCIsCXX(false), CCCEcho(false),
-    CCCPrintBindings(false), CheckInputsExist(true), CCCUseClang(true),
-    CCCUseClangCXX(true), CCCUseClangCPP(true), CCCUsePCH(true),
-    SuppressMissingInputWarning(false) {
+    CCCGenericGCCName("gcc"), CCPrintOptionsFilename(0), CCCIsCXX(false),
+    CCCEcho(false), CCCPrintBindings(false), CCPrintOptions(false),
+    CheckInputsExist(true), CCCUseClang(true), CCCUseClangCXX(true),
+    CCCUseClangCPP(true), CCCUsePCH(true), SuppressMissingInputWarning(false) {
   if (IsProduction) {
     // In a "production" build, only use clang on architectures we expect to
     // work, and don't use clang C++.
diff --git a/test/Driver/cc-print-options.c b/test/Driver/cc-print-options.c
new file mode 100644 (file)
index 0000000..7b798cb
--- /dev/null
@@ -0,0 +1,7 @@
+// RUN: env CC_PRINT_OPTIONS=1 \
+// RUN:     CC_PRINT_OPTIONS_FILE=%t.log \
+// RUN: %clang -S -o %t.s %s
+// RUN: FileCheck %s < %t.log
+
+// CHECK: [Logging clang options]{{.*}}clang{{.*}}"-S"
+
index 46f412403167adf5865384bf1c504695ade18d30..2108c8fbdbfee079e862dbc0ac1376b6452049c4 100644 (file)
@@ -218,6 +218,11 @@ int main(int argc, const char **argv) {
 
   llvm::OwningPtr<Compilation> C;
 
+  // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
+  TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
+  if (TheDriver.CCPrintOptions)
+    TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
+
   // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
   // command line behind the scenes.
   std::set<std::string> SavedStrings;