Author: Jan Svoboda
Date: 2020-12-16T09:44:54+01:00
New Revision: f2661bed185e14a8f5aa9a54565a8b938a7a10aa

URL: 
https://github.com/llvm/llvm-project/commit/f2661bed185e14a8f5aa9a54565a8b938a7a10aa
DIFF: 
https://github.com/llvm/llvm-project/commit/f2661bed185e14a8f5aa9a54565a8b938a7a10aa.diff

LOG: [clang][cli] Prevent double denormalization

If both flags created through BoolOption are CC1Option and the keypath has a 
non-default or non-implied value, the denormalizer gets called twice. If the 
denormalizer has the ability to generate both flags, we can end up generating 
the same flag twice.

Reviewed By: dexonsmith, Bigcheese

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

Added: 
    

Modified: 
    clang/include/clang/Driver/Options.td
    clang/lib/Frontend/CompilerInvocation.cpp
    clang/unittests/Frontend/CompilerInvocationTest.cpp
    llvm/include/llvm/Option/OptParser.td

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 0da6a43b6986..a2208f2e2c5c 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -338,7 +338,7 @@ class BoolOptionFlag<FlagDefExpanded flag, FlagDefExpanded 
other,
   : Flag<["-"], flag.Spelling>, Flags<flag.OptionFlags>, HelpText<flag.Help>,
     MarshallingInfoBooleanFlag<keypath, default.Value, flag.ValueAsCode,
                                flag.RecordName, other.ValueAsCode,
-                               other.RecordName, other.Spelling>,
+                               other.RecordName>,
     ImpliedByAnyOf<implied.ImpliedBy, implied.ValueAsCode> {}
 
 // Generates TableGen records for two command line flags that control the same

diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 1a863a739156..bdadaa9a0b5c 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -197,12 +197,11 @@ static auto makeBooleanOptionNormalizer(bool Value, bool 
OtherValue,
   };
 }
 
-static auto makeBooleanOptionDenormalizer(bool Value,
-                                          const char *OtherSpelling) {
-  return [Value, OtherSpelling](
-             SmallVectorImpl<const char *> &Args, const char *Spelling,
-             CompilerInvocation::StringAllocator, unsigned, bool KeyPath) {
-    Args.push_back(KeyPath == Value ? Spelling : OtherSpelling);
+static auto makeBooleanOptionDenormalizer(bool Value) {
+  return [Value](SmallVectorImpl<const char *> &Args, const char *Spelling,
+                 CompilerInvocation::StringAllocator, unsigned, bool KeyPath) {
+    if (KeyPath == Value)
+      Args.push_back(Spelling);
   };
 }
 

diff  --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp 
b/clang/unittests/Frontend/CompilerInvocationTest.cpp
index 156e3393acd6..51b7ba8c147f 100644
--- a/clang/unittests/Frontend/CompilerInvocationTest.cpp
+++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp
@@ -269,7 +269,7 @@ TEST_F(CommandLineTest, BoolOptionCC1ViaLetPresentPos) {
 
   Invocation.generateCC1CommandLine(GeneratedArgs, *this);
 
-  ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fdebug-pass-manager")));
+  ASSERT_EQ(count(GeneratedArgs, StringRef("-fdebug-pass-manager")), 1);
   ASSERT_THAT(GeneratedArgs, Not(Contains(StrEq("-fno-debug-pass-manager"))));
 }
 

diff  --git a/llvm/include/llvm/Option/OptParser.td 
b/llvm/include/llvm/Option/OptParser.td
index 9e0ff1450920..d35a3e5fec7f 100644
--- a/llvm/include/llvm/Option/OptParser.td
+++ b/llvm/include/llvm/Option/OptParser.td
@@ -176,10 +176,10 @@ class MarshallingInfoBitfieldFlag<code keypath, code 
value>
 
 // Marshalling info for booleans. Applied to the flag setting keypath to false.
 class MarshallingInfoBooleanFlag<code keypath, code defaultvalue, code value, 
code name,
-                                 code other_value, code other_name, string 
other_spelling>
+                                 code other_value, code other_name>
   : MarshallingInfoFlag<keypath, defaultvalue> {
   code Normalizer = "makeBooleanOptionNormalizer("#value#", "#other_value#", 
OPT_"#other_name#")";
-  code Denormalizer = "makeBooleanOptionDenormalizer("#value#", 
\""#other_spelling#"\")";
+  code Denormalizer = "makeBooleanOptionDenormalizer("#value#")";
 }
 
 // Mixins for additional marshalling attributes.


        
_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to