[PATCH] D25358: [Support] Use variadic templates instead of varargs in CommandLineFlags.h ValueClass

Eric Fiselier via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 6 21:09:05 PDT 2016


EricWF updated this revision to Diff 73881.
EricWF added a comment.

Use fewer templates.


https://reviews.llvm.org/D25358

Files:
  include/llvm/Support/CommandLine.h


Index: include/llvm/Support/CommandLine.h
===================================================================
--- include/llvm/Support/CommandLine.h
+++ include/llvm/Support/CommandLine.h
@@ -570,21 +570,28 @@
   // the overhead is less, and most importantly, it keeps them in the order
   // inserted so we can print our option out nicely.
   SmallVector<std::pair<StringRef , std::pair<int, StringRef >>, 4> Values;
-  void processValues(va_list Vals);
+
+  template <class... Remaining>
+  void processArgs(const char *Name, int Val, const char *Desc, Remaining... Args) {
+    auto EnumVal = static_cast<DataType>(Val);
+    Values.push_back(std::make_pair(StringRef(Name),
+                                    std::make_pair(EnumVal, StringRef(Desc))));
+    processArgs(Args...);
+  }
+
+  template <class SentinalT> void processArgs(SentinalT) {
+    // FIXME: Remove sentinal argument at all of the call sites.
+    static_assert(std::is_same<SentinalT, void *>::value,
+                  "incorrect sentinal type");
+  }
 
 public:
+  template <class... Args>
   ValuesClass(StringRef EnumName, DataType Val, StringRef Desc,
-              va_list ValueArgs) {
+              Args... ValueArgs) {
     // Insert the first value, which is required.
     Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
-
-    // Process the varargs portion of the values...
-    while (const char *enumName = va_arg(ValueArgs, const char * )) {
-      DataType EnumVal = static_cast<DataType>(va_arg(ValueArgs, int));
-      auto EnumDesc = StringRef(va_arg(ValueArgs, const char * ));
-      Values.push_back(std::make_pair(StringRef(enumName), // Add value to value map
-                                      std::make_pair(EnumVal, EnumDesc)));
-    }
+    processArgs(ValueArgs...);
   }
 
   template <class Opt> void apply(Opt &O) const {
@@ -594,13 +601,10 @@
   }
 };
 
-template <class DataType>
-ValuesClass<DataType> LLVM_END_WITH_NULL
-values(StringRef Arg, DataType Val, StringRef Desc, ...) {
-  va_list ValueArgs;
-  va_start(ValueArgs, Desc);
-  ValuesClass<DataType> Vals(Arg, Val, Desc, ValueArgs);
-  va_end(ValueArgs);
+template <class DataType, class... Args>
+ValuesClass<DataType> values(StringRef Arg, DataType Val, StringRef Desc,
+                             Args &&... ValueArgs) {
+  ValuesClass<DataType> Vals(Arg, Val, Desc, std::forward<Args>(ValueArgs)...);
   return Vals;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25358.73881.patch
Type: text/x-patch
Size: 2426 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161007/b1fb2fc6/attachment.bin>


More information about the llvm-commits mailing list