aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2010-03-20 08:01:59 +0000
committerDaniel Dunbar <daniel@zuster.org>2010-03-20 08:01:59 +0000
commit4c00fcdf98d3d7c4cb47b64f8b770f8f4bff1357 (patch)
tree394f0d19d402eed4645525804bfa682cd976acd3
parentb86c5fc7393f61221686fc56e992ca409dee2a50 (diff)
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
-rw-r--r--include/clang/Basic/DiagnosticDriverKinds.td2
-rw-r--r--include/clang/Driver/Driver.h7
-rw-r--r--lib/Driver/Compilation.cpp30
-rw-r--r--lib/Driver/Driver.cpp8
-rw-r--r--test/Driver/cc-print-options.c7
-rw-r--r--tools/driver/driver.cpp5
6 files changed, 53 insertions, 6 deletions
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 <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">;
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<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;