From 4c00fcdf98d3d7c4cb47b64f8b770f8f4bff1357 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Sat, 20 Mar 2010 08:01:59 +0000 Subject: [PATCH] Driver: Support CC_PRINT_OPTIONS, used for logging the compile commands (in -v style) to a file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@99054 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticDriverKinds.td | 2 ++ include/clang/Driver/Driver.h | 7 +++++ lib/Driver/Compilation.cpp | 30 ++++++++++++++++++-- lib/Driver/Driver.cpp | 8 +++--- test/Driver/cc-print-options.c | 7 +++++ tools/driver/driver.cpp | 5 ++++ 6 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 test/Driver/cc-print-options.c diff --git a/include/clang/Basic/DiagnosticDriverKinds.td b/include/clang/Basic/DiagnosticDriverKinds.td index 2bce12db53..3dbe47f4cf 100644 --- a/include/clang/Basic/DiagnosticDriverKinds.td +++ b/include/clang/Basic/DiagnosticDriverKinds.td @@ -64,6 +64,8 @@ def err_drv_invalid_remap_file : Error< "invalid option '%0' not of the form ;">; 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">; diff --git a/include/clang/Driver/Driver.h b/include/clang/Driver/Driver.h index 59c3946a2c..f49c3b97c1 100644 --- a/include/clang/Driver/Driver.h +++ b/include/clang/Driver/Driver.h @@ -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. diff --git a/lib/Driver/Compilation.cpp b/lib/Driver/Compilation.cpp index 98c6374425..227f79a75b 100644 --- a/lib/Driver/Compilation.cpp +++ b/lib/Driver/Compilation.cpp @@ -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 = diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 3257ee55a7..acfff386f4 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -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 index 0000000000..7b798cb08a --- /dev/null +++ b/test/Driver/cc-print-options.c @@ -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" + diff --git a/tools/driver/driver.cpp b/tools/driver/driver.cpp index 46f4124031..2108c8fbdb 100644 --- a/tools/driver/driver.cpp +++ b/tools/driver/driver.cpp @@ -218,6 +218,11 @@ int main(int argc, const char **argv) { llvm::OwningPtr 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 SavedStrings; -- 2.40.0