Skip to content

Commit

Permalink
[clang][cli] Accept option spelling as Twine
Browse files Browse the repository at this point in the history
This will make it possible to accept the spelling as `StringLiteral` in D157029 and avoid some unnecessary allocations in a later patch.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D157035
  • Loading branch information
jansvoboda11 committed Aug 3, 2023
1 parent c5abddb commit 243bc75
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ static std::optional<bool> normalizeSimpleNegativeFlag(OptSpecifier Opt,
/// unnecessary template instantiations and just ignore it with a variadic
/// argument.
static void denormalizeSimpleFlag(SmallVectorImpl<const char *> &Args,
const char *Spelling,
const Twine &Spelling,
CompilerInvocation::StringAllocator,
Option::OptionClass, unsigned, /*T*/...) {
Args.push_back(Spelling);
// Spelling is already allocated or a static string, no need to call SA.
assert(*Spelling.getSingleStringRef().end() == '\0');
Args.push_back(Spelling.getSingleStringRef().data());
}

template <typename T> static constexpr bool is_uint64_t_convertible() {
Expand Down Expand Up @@ -232,24 +234,29 @@ static auto makeBooleanOptionNormalizer(bool Value, bool OtherValue,
}

static auto makeBooleanOptionDenormalizer(bool Value) {
return [Value](SmallVectorImpl<const char *> &Args, const char *Spelling,
return [Value](SmallVectorImpl<const char *> &Args, const Twine &Spelling,
CompilerInvocation::StringAllocator, Option::OptionClass,
unsigned, bool KeyPath) {
if (KeyPath == Value)
Args.push_back(Spelling);
if (KeyPath == Value) {
// Spelling is already allocated or a static string, no need to call SA.
assert(*Spelling.getSingleStringRef().end() == '\0');
Args.push_back(Spelling.getSingleStringRef().data());
}
};
}

static void denormalizeStringImpl(SmallVectorImpl<const char *> &Args,
const char *Spelling,
const Twine &Spelling,
CompilerInvocation::StringAllocator SA,
Option::OptionClass OptClass, unsigned,
const Twine &Value) {
switch (OptClass) {
case Option::SeparateClass:
case Option::JoinedOrSeparateClass:
case Option::JoinedAndSeparateClass:
Args.push_back(Spelling);
// Spelling is already allocated or a static string, no need to call SA.
assert(*Spelling.getSingleStringRef().end() == '\0');
Args.push_back(Spelling.getSingleStringRef().data());
Args.push_back(SA(Value));
break;
case Option::JoinedClass:
Expand All @@ -264,7 +271,7 @@ static void denormalizeStringImpl(SmallVectorImpl<const char *> &Args,

template <typename T>
static void
denormalizeString(SmallVectorImpl<const char *> &Args, const char *Spelling,
denormalizeString(SmallVectorImpl<const char *> &Args, const Twine &Spelling,
CompilerInvocation::StringAllocator SA,
Option::OptionClass OptClass, unsigned TableIndex, T Value) {
denormalizeStringImpl(Args, Spelling, SA, OptClass, TableIndex, Twine(Value));
Expand Down Expand Up @@ -309,7 +316,7 @@ static std::optional<unsigned> normalizeSimpleEnum(OptSpecifier Opt,
}

static void denormalizeSimpleEnumImpl(SmallVectorImpl<const char *> &Args,
const char *Spelling,
const Twine &Spelling,
CompilerInvocation::StringAllocator SA,
Option::OptionClass OptClass,
unsigned TableIndex, unsigned Value) {
Expand All @@ -326,7 +333,7 @@ static void denormalizeSimpleEnumImpl(SmallVectorImpl<const char *> &Args,

template <typename T>
static void denormalizeSimpleEnum(SmallVectorImpl<const char *> &Args,
const char *Spelling,
const Twine &Spelling,
CompilerInvocation::StringAllocator SA,
Option::OptionClass OptClass,
unsigned TableIndex, T Value) {
Expand Down Expand Up @@ -367,7 +374,7 @@ normalizeStringVector(OptSpecifier Opt, int, const ArgList &Args,
}

static void denormalizeStringVector(SmallVectorImpl<const char *> &Args,
const char *Spelling,
const Twine &Spelling,
CompilerInvocation::StringAllocator SA,
Option::OptionClass OptClass,
unsigned TableIndex,
Expand Down

0 comments on commit 243bc75

Please sign in to comment.