@@ -27,13 +27,6 @@ class CompilerInstance;
2727
2828namespace tidy {
2929
30- // / This class should be specialized by any enum type that needs to be converted
31- // / to and from an \ref llvm::StringRef.
32- template <class T > struct OptionEnumMapping {
33- // Specializations of this struct must implement this function.
34- static ArrayRef<std::pair<T, StringRef>> getEnumMapping () = delete;
35- };
36-
3730template <typename T> class OptionError : public llvm ::ErrorInfo<T> {
3831 std::error_code convertToErrorCode () const override {
3932 return llvm::inconvertibleErrorCode ();
@@ -320,80 +313,77 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
320313 }
321314
322315 // / Read a named option from the ``Context`` and parse it as an
323- // / enum type ``T``.
316+ // / enum type ``T`` using the \p Mapping provided. If \p IgnoreCase is set,
317+ // / it will search the mapping ignoring the case.
324318 // /
325319 // / Reads the option with the check-local name \p LocalName from the
326320 // / ``CheckOptions``. If the corresponding key is not present, returns a
327321 // / ``MissingOptionError``. If the key can't be parsed as a ``T`` returns a
328322 // / ``UnparseableEnumOptionError``.
329- // /
330- // / \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
331- // / supply the mapping required to convert between ``T`` and a string.
332323 template <typename T>
333324 std::enable_if_t <std::is_enum<T>::value, llvm::Expected<T>>
334- get (StringRef LocalName, bool IgnoreCase = false ) {
335- if (llvm::Expected<int64_t > ValueOr =
336- getEnumInt (LocalName, typeEraseMapping<T>(), false , IgnoreCase))
325+ get (StringRef LocalName, ArrayRef<std::pair<StringRef, T>> Mapping,
326+ bool IgnoreCase = false ) {
327+ if (llvm::Expected<int64_t > ValueOr = getEnumInt (
328+ LocalName, typeEraseMapping (Mapping), false , IgnoreCase))
337329 return static_cast <T>(*ValueOr);
338330 else
339331 return std::move (ValueOr.takeError ());
340332 }
341333
342334 // / Read a named option from the ``Context`` and parse it as an
343- // / enum type ``T``.
335+ // / enum type ``T`` using the \p Mapping provided. If \p IgnoreCase is set,
336+ // / it will search the mapping ignoring the case.
344337 // /
345338 // / Reads the option with the check-local name \p LocalName from the
346339 // / ``CheckOptions``. If the corresponding key is not present or it can't be
347340 // / parsed as a ``T``, returns \p Default.
348- // /
349- // / \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
350- // / supply the mapping required to convert between ``T`` and a string.
351341 template <typename T>
352342 std::enable_if_t <std::is_enum<T>::value, T>
353- get (StringRef LocalName, T Default, bool IgnoreCase = false ) {
354- if (auto ValueOr = get<T>(LocalName, IgnoreCase))
343+ get (StringRef LocalName, ArrayRef<std::pair<StringRef, T>> Mapping,
344+ T Default, bool IgnoreCase = false ) {
345+ if (auto ValueOr = get (LocalName, Mapping, IgnoreCase))
355346 return *ValueOr;
356347 else
357348 logErrToStdErr (ValueOr.takeError ());
358349 return Default;
359350 }
360351
361352 // / Read a named option from the ``Context`` and parse it as an
362- // / enum type ``T``.
353+ // / enum type ``T`` using the \p Mapping provided. If \p IgnoreCase is set,
354+ // / it will search the mapping ignoring the case.
363355 // /
364356 // / Reads the option with the check-local name \p LocalName from local or
365357 // / global ``CheckOptions``. Gets local option first. If local is not
366358 // / present, falls back to get global option. If global option is not
367359 // / present either, returns a ``MissingOptionError``. If the key can't be
368360 // / parsed as a ``T`` returns a ``UnparseableEnumOptionError``.
369- // /
370- // / \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
371- // / supply the mapping required to convert between ``T`` and a string.
372361 template <typename T>
373362 std::enable_if_t <std::is_enum<T>::value, llvm::Expected<T>>
374363 getLocalOrGlobal (StringRef LocalName,
364+ ArrayRef<std::pair<StringRef, T>> Mapping,
375365 bool IgnoreCase = false ) {
376- if (llvm::Expected<int64_t > ValueOr =
377- getEnumInt ( LocalName, typeEraseMapping<T>( ), true , IgnoreCase))
366+ if (llvm::Expected<int64_t > ValueOr = getEnumInt (
367+ LocalName, typeEraseMapping (Mapping ), true , IgnoreCase))
378368 return static_cast <T>(*ValueOr);
379369 else
380370 return std::move (ValueOr.takeError ());
381371 }
382372
383373 // / Read a named option from the ``Context`` and parse it as an
384- // / enum type ``T``.
374+ // / enum type ``T`` using the \p Mapping provided. If \p IgnoreCase is set,
375+ // / it will search the mapping ignoring the case.
385376 // /
386377 // / Reads the option with the check-local name \p LocalName from local or
387378 // / global ``CheckOptions``. Gets local option first. If local is not
388379 // / present, falls back to get global option. If global option is not
389380 // / present either or it can't be parsed as a ``T``, returns \p Default.
390- // /
391- // / \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
392- // / supply the mapping required to convert between ``T`` and a string.
393381 template <typename T>
394382 std::enable_if_t <std::is_enum<T>::value, T>
395- getLocalOrGlobal (StringRef LocalName, T Default, bool IgnoreCase = false ) {
396- if (auto ValueOr = getLocalOrGlobal<T>(LocalName, IgnoreCase))
383+ getLocalOrGlobal (StringRef LocalName,
384+ ArrayRef<std::pair<StringRef, T>> Mapping, T Default,
385+ bool IgnoreCase = false ) {
386+ if (auto ValueOr = getLocalOrGlobal (LocalName, Mapping, IgnoreCase))
397387 return *ValueOr;
398388 else
399389 logErrToStdErr (ValueOr.takeError ());
@@ -411,40 +401,34 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
411401 int64_t Value) const ;
412402
413403 // / Stores an option with the check-local name \p LocalName as the string
414- // / representation of the Enum \p Value to \p Options.
415- // /
416- // / \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to
417- // / supply the mapping required to convert between ``T`` and a string.
404+ // / representation of the Enum \p Value using the \p Mapping to \p Options.
418405 template <typename T>
419406 std::enable_if_t <std::is_enum<T>::value>
420- store (ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value) {
421- ArrayRef<std::pair<T, StringRef>> Mapping =
422- OptionEnumMapping<T>::getEnumMapping ();
407+ store (ClangTidyOptions::OptionMap &Options, StringRef LocalName, T Value,
408+ ArrayRef<std::pair<StringRef, T>> Mapping) {
423409 auto Iter = llvm::find_if (
424- Mapping, [&](const std::pair<T, StringRef > &NameAndEnum) {
425- return NameAndEnum.first == Value;
410+ Mapping, [&](const std::pair<StringRef, T > &NameAndEnum) {
411+ return NameAndEnum.second == Value;
426412 });
427413 assert (Iter != Mapping.end () && " Unknown Case Value" );
428- store (Options, LocalName, Iter->second );
414+ store (Options, LocalName, Iter->first );
429415 }
430416
431417 private:
432- using NameAndValue = std::pair<int64_t , StringRef >;
418+ using NameAndValue = std::pair<StringRef, int64_t >;
433419
434420 llvm::Expected<int64_t > getEnumInt (StringRef LocalName,
435421 ArrayRef<NameAndValue> Mapping,
436422 bool CheckGlobal, bool IgnoreCase);
437423
438424 template <typename T>
439425 std::enable_if_t <std::is_enum<T>::value, std::vector<NameAndValue>>
440- typeEraseMapping () {
441- ArrayRef<std::pair<T, StringRef>> Mapping =
442- OptionEnumMapping<T>::getEnumMapping ();
426+ typeEraseMapping (ArrayRef<std::pair<StringRef, T>> Mapping) {
443427 std::vector<NameAndValue> Result;
444428 Result.reserve (Mapping.size ());
445429 for (auto &MappedItem : Mapping) {
446- Result.emplace_back (static_cast < int64_t >( MappedItem.first ) ,
447- MappedItem.second );
430+ Result.emplace_back (MappedItem.first ,
431+ static_cast < int64_t >( MappedItem.second ) );
448432 }
449433 return Result;
450434 }
0 commit comments