Driver: Fix forwarding of -{std,ansi,trigraphs} when there are
multiple instances of an option.
Also, removed direct -ansi support from clang-cc.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68558 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 4e2a7b9..a3cea95 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -350,8 +350,23 @@
Args.AddAllArgs(CmdArgs, options::OPT_clang_W_Group,
options::OPT_pedantic_Group);
Args.AddLastArg(CmdArgs, options::OPT_w);
- Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
- options::OPT_trigraphs);
+
+ // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
+ // (-ansi is equivalent to -std=c89).
+ //
+ // If a std is supplied, only add -trigraphs if it follows the
+ // option.
+ if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
+ if (Std->getOption().matches(options::OPT_ansi))
+ CmdArgs.push_back("-std=c89");
+ else
+ Std->render(Args, CmdArgs);
+
+ if (Arg *A = Args.getLastArg(options::OPT_trigraphs))
+ if (A->getIndex() > Std->getIndex())
+ A->render(Args, CmdArgs);
+ } else
+ Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
CmdArgs.push_back("-ftemplate-depth");
diff --git a/test/Driver/std.c b/test/Driver/std.c
new file mode 100644
index 0000000..ef6d8f1
--- /dev/null
+++ b/test/Driver/std.c
@@ -0,0 +1,8 @@
+// RUN: clang -std=c99 -trigraphs -std=gnu99 %s -E -o %t &&
+// RUN: grep '??(??)' %t &&
+// RUN: clang -ansi %s -E -o %t &&
+// RUN: grep -F '[]' %t &&
+// RUN: clang -std=gnu99 -trigraphs %s -E -o %t &&
+// RUN: grep -F '[]' %t
+
+??(??)
diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp
index 9260afe..2843848 100644
--- a/tools/clang-cc/clang-cc.cpp
+++ b/tools/clang-cc/clang-cc.cpp
@@ -585,9 +585,6 @@
static llvm::cl::opt<bool>
Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences."));
-static llvm::cl::opt<bool>
-Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89."));
-
static llvm::cl::list<std::string>
TargetFeatures("mattr", llvm::cl::CommaSeparated,
llvm::cl::desc("Target specific attributes (-mattr=help for details)"));
@@ -653,9 +650,6 @@
}
}
- if (Ansi) // "The -ansi option is equivalent to -std=c89."
- LangStd = lang_c89;
-
if (LangStd == lang_unspecified) {
// Based on the base language, pick one.
switch (LK) {
@@ -719,8 +713,8 @@
else
Options.ImplicitInt = 0;
- // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi
- // is specified, or -std is set to a conforming mode.
+ // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
+ // is specified, or -std is set to a conforming mode.
Options.Trigraphs = !Options.GNUMode;
if (Trigraphs.getPosition())
Options.Trigraphs = Trigraphs; // Command line option wins if specified.