[clang] 243bc75 - [clang][cli] Accept option spelling as `Twine`

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 3 15:10:23 PDT 2023


Author: Jan Svoboda
Date: 2023-08-03T15:09:52-07:00
New Revision: 243bc7504965b885c34487f358d2a4b4a355a6b5

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

LOG: [clang][cli] Accept option spelling as `Twine`

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

Added: 
    

Modified: 
    clang/lib/Frontend/CompilerInvocation.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 1fba91bed04141..1e990f4663d44f 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -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() {
@@ -232,16 +234,19 @@ 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) {
@@ -249,7 +254,9 @@ static void denormalizeStringImpl(SmallVectorImpl<const char *> &Args,
   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:
@@ -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));
@@ -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) {
@@ -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) {
@@ -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,


        


More information about the cfe-commits mailing list