From a64830ea2b59ce26f062458bd5212075ced43d84 Mon Sep 17 00:00:00 2001
From: Guansong Zhang <zhang.guansong@gmail.com>
Date: Wed, 15 Mar 2017 20:57:11 +0000
Subject: [PATCH] enable -save-temps with -finclude-defult-header

Currently the two flags can not work together.

To illustrate the issue, we can have an one line file a.cl contains only an empty function

cat a.cl

void test(){}

Then use

clang -v -save-temps -x cl -Xclang -cl-std=CL2.0 -Xclang -finclude-default-header -target amdgcn -S -c a.cl

we will get redefinition errors for various things.

The reason is that the -finclude-default-header flag is not meant to be on cc1 command other than the preprocessor.

The fix is modeled after the code just below the change to filter the -finclude-default-header flag out when we are not in the preprocess phase.

Differential Revision: https://reviews.llvm.org/D30743


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@297890 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Driver/ToolChains/Clang.cpp       | 14 +++++++++++++-
 test/Driver/include-default-header.cl |  4 ++++
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 test/Driver/include-default-header.cl

diff --git a/lib/Driver/ToolChains/Clang.cpp b/lib/Driver/ToolChains/Clang.cpp
index d23ccc1244b..ea0603c630f 100644
--- a/lib/Driver/ToolChains/Clang.cpp
+++ b/lib/Driver/ToolChains/Clang.cpp
@@ -4268,7 +4268,19 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
 
   // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
   // parser.
-  Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
+  // -finclude-default-header flag is for preprocessor,
+  // do not pass it to other cc1 commands when save-temps is enabled
+  if (C.getDriver().isSaveTempsEnabled() &&
+      !isa<PreprocessJobAction>(JA)) {
+    for (auto Arg : Args.filtered(options::OPT_Xclang)) {
+      Arg->claim();
+      if (StringRef(Arg->getValue()) != "-finclude-default-header")
+        CmdArgs.push_back(Arg->getValue());
+    }
+  }
+  else {
+    Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
+  }
   for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
     A->claim();
 
diff --git a/test/Driver/include-default-header.cl b/test/Driver/include-default-header.cl
new file mode 100644
index 00000000000..accc6d55db5
--- /dev/null
+++ b/test/Driver/include-default-header.cl
@@ -0,0 +1,4 @@
+// RUN: %clang -v -save-temps -x cl -Xclang -cl-std=CL2.0 -Xclang -finclude-default-header -target amdgcn -S -c %s
+
+void test() {}
+
-- 
GitLab