diff options
author | Jan Svoboda <jan_svoboda@apple.com> | 2020-12-18 13:55:48 +0100 |
---|---|---|
committer | Jan Svoboda <jan_svoboda@apple.com> | 2020-12-21 11:32:47 +0100 |
commit | 70410a264949101ced3ce3458f37dd4cc2f5af85 (patch) | |
tree | a556384161d6a76f1244a6ef7294146ef713dd41 /clang/lib/Frontend/CompilerInvocation.cpp | |
parent | 27b7d646886d499c70dec3481dfc3c82dfc43dd7 (diff) | |
download | llvm-70410a264949101ced3ce3458f37dd4cc2f5af85.zip llvm-70410a264949101ced3ce3458f37dd4cc2f5af85.tar.gz llvm-70410a264949101ced3ce3458f37dd4cc2f5af85.tar.bz2 |
[clang][cli] Let denormalizer decide how to render the option based on the option class
Before this patch, you needed to use `AutoNormalizeEnumJoined` whenever you wanted to **de**normalize joined enum.
Besides the naming confusion, this means the fact the option is joined is specified in two places: in the normalization multiclass and in the `Joined<["-"], ...>` multiclass.
This patch makes this work automatically, taking into account the `OptionClass` of options.
Also, the enum denormalizer now just looks up the spelling of the present enum case in a table and forwards it to the string denormalizer.
I also added more tests that exercise this.
Reviewed By: dexonsmith
Original patch by Daniel Grumberg.
Differential Revision: https://reviews.llvm.org/D84189
Diffstat (limited to 'clang/lib/Frontend/CompilerInvocation.cpp')
-rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 78 |
1 files changed, 39 insertions, 39 deletions
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 00615a7..f71b14e 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -152,8 +152,8 @@ static Optional<bool> normalizeSimpleNegativeFlag(OptSpecifier Opt, unsigned, /// argument. static void denormalizeSimpleFlag(SmallVectorImpl<const char *> &Args, const char *Spelling, - CompilerInvocation::StringAllocator, unsigned, - /*T*/...) { + CompilerInvocation::StringAllocator, + Option::OptionClass, unsigned, /*T*/...) { Args.push_back(Spelling); } @@ -200,12 +200,41 @@ static auto makeBooleanOptionNormalizer(bool Value, bool OtherValue, static auto makeBooleanOptionDenormalizer(bool Value) { return [Value](SmallVectorImpl<const char *> &Args, const char *Spelling, - CompilerInvocation::StringAllocator, unsigned, bool KeyPath) { + CompilerInvocation::StringAllocator, Option::OptionClass, + unsigned, bool KeyPath) { if (KeyPath == Value) Args.push_back(Spelling); }; } +static void denormalizeStringImpl(SmallVectorImpl<const char *> &Args, + const char *Spelling, + CompilerInvocation::StringAllocator SA, + Option::OptionClass OptClass, unsigned, + Twine Value) { + switch (OptClass) { + case Option::SeparateClass: + case Option::JoinedOrSeparateClass: + Args.push_back(Spelling); + Args.push_back(SA(Value)); + break; + case Option::JoinedClass: + Args.push_back(SA(Twine(Spelling) + Value)); + break; + default: + llvm_unreachable("Cannot denormalize an option with option class " + "incompatible with string denormalization."); + } +} + +template <typename T> +static void +denormalizeString(SmallVectorImpl<const char *> &Args, const char *Spelling, + CompilerInvocation::StringAllocator SA, + Option::OptionClass OptClass, unsigned TableIndex, T Value) { + denormalizeStringImpl(Args, Spelling, SA, OptClass, TableIndex, Twine(Value)); +} + static Optional<SimpleEnumValue> findValueTableByName(const SimpleEnumValueTable &Table, StringRef Name) { for (int I = 0, E = Table.Size; I != E; ++I) @@ -247,12 +276,13 @@ static llvm::Optional<unsigned> normalizeSimpleEnum(OptSpecifier Opt, static void denormalizeSimpleEnumImpl(SmallVectorImpl<const char *> &Args, const char *Spelling, CompilerInvocation::StringAllocator SA, + Option::OptionClass OptClass, unsigned TableIndex, unsigned Value) { assert(TableIndex < SimpleEnumValueTablesSize); const SimpleEnumValueTable &Table = SimpleEnumValueTables[TableIndex]; if (auto MaybeEnumVal = findValueTableByValue(Table, Value)) { - Args.push_back(Spelling); - Args.push_back(MaybeEnumVal->Name); + denormalizeString(Args, Spelling, SA, OptClass, TableIndex, + MaybeEnumVal->Name); } else { llvm_unreachable("The simple enum value was not correctly defined in " "the tablegen option description"); @@ -263,24 +293,12 @@ template <typename T> static void denormalizeSimpleEnum(SmallVectorImpl<const char *> &Args, const char *Spelling, CompilerInvocation::StringAllocator SA, + Option::OptionClass OptClass, unsigned TableIndex, T Value) { - return denormalizeSimpleEnumImpl(Args, Spelling, SA, TableIndex, + return denormalizeSimpleEnumImpl(Args, Spelling, SA, OptClass, TableIndex, static_cast<unsigned>(Value)); } -static void denormalizeSimpleEnumJoined(SmallVectorImpl<const char *> &Args, - const char *Spelling, - CompilerInvocation::StringAllocator SA, - unsigned TableIndex, unsigned Value) { - assert(TableIndex < SimpleEnumValueTablesSize); - const SimpleEnumValueTable &Table = SimpleEnumValueTables[TableIndex]; - if (auto MaybeEnumVal = findValueTableByValue(Table, Value)) - Args.push_back(SA(Twine(Spelling) + MaybeEnumVal->Name)); - else - llvm_unreachable("The simple enum value was not correctly defined in " - "the tablegen option description"); -} - static Optional<std::string> normalizeString(OptSpecifier Opt, int TableIndex, const ArgList &Args, DiagnosticsEngine &Diags) { @@ -290,25 +308,6 @@ static Optional<std::string> normalizeString(OptSpecifier Opt, int TableIndex, return std::string(Arg->getValue()); } -static void denormalizeString(SmallVectorImpl<const char *> &Args, - const char *Spelling, - CompilerInvocation::StringAllocator SA, unsigned, - Twine Value) { - Args.push_back(Spelling); - Args.push_back(SA(Value)); -} - -template <typename T, - std::enable_if_t<!std::is_convertible<T, Twine>::value && - std::is_constructible<Twine, T>::value, - bool> = false> -static void denormalizeString(SmallVectorImpl<const char *> &Args, - const char *Spelling, - CompilerInvocation::StringAllocator SA, - unsigned TableIndex, T Value) { - denormalizeString(Args, Spelling, SA, TableIndex, Twine(Value)); -} - template <typename IntTy> static Optional<IntTy> normalizeStringIntegral(OptSpecifier Opt, int, const ArgList &Args, @@ -3267,7 +3266,8 @@ void CompilerInvocation::generateCC1CommandLine( (Extracted != \ static_cast<decltype(this->KEYPATH)>( \ (IMPLIED_CHECK) ? (IMPLIED_VALUE) : (DEFAULT_VALUE)))) \ - DENORMALIZER(Args, SPELLING, SA, TABLE_INDEX, Extracted); \ + DENORMALIZER(Args, SPELLING, SA, Option::KIND##Class, TABLE_INDEX, \ + Extracted); \ }(EXTRACTOR(this->KEYPATH)); \ } |