[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:05:08 PDT 2016
EricWF created this revision.
EricWF added a reviewer: joerg.
EricWF added a subscriber: llvm-commits.
This refactoring was informally requested on IRC.
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,33 @@
// 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 ENameT, class EValT, class EDescT, class... Remaining>
+ void processArgs(ENameT Name, EValT Val, EDescT Desc, Remaining... Args) {
+ static_assert(std::is_same<const char *, ENameT>::value,
+ "expected string literal argument");
+ static_assert(std::is_same<int, EValT>::value, "expected integer argument");
+ static_assert(std::is_same<const char *, EDescT>::value,
+ "expected string literal argument");
+ 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 +606,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.73879.patch
Type: text/x-patch
Size: 2777 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161007/3aa50bee/attachment.bin>
More information about the llvm-commits
mailing list