[PATCH] [clang-tidy] Add support for boolean check options.

Alexander Kornienko alexfh at google.com
Mon Oct 6 09:52:43 PDT 2014


================
Comment at: clang-tidy/ClangTidy.h:73-80
@@ +72,10 @@
+  /// \p Default.
+  bool get(StringRef LocalName, bool Default) const {
+    std::string Value = get(LocalName, "");
+    if (Value == "true")
+      return true;
+    if (Value == "false")
+      return false;
+    return Default;
+  }
+
----------------
curdeius wrote:
> curdeius wrote:
> > Couldn't we just generalize this approach to all the types that specialize `llvm::yaml::XYZTraits` (XYZ being one of Scalar/ScalarEnumeration/...) ?
> > 
> > An ugly hack (working at least for `enum`s specializing `ScalarEnumerationTraits`) would be:
> > 
> > ```
> >   template <typename T>
> >   typename std::enable_if<!std::is_integral<T>::value &&
> >                               !std::is_convertible<T, std::string>::value,
> >                           T>::type
> >   get(StringRef LocalName, T Default) const {
> >     std::string Value = get(LocalName, "");
> >     T Result = Default;
> >     if (!Value.empty()) {
> >       std::stringstream Content;
> >       Content << LocalName.str() << ": " << Value;
> >       llvm::yaml::Input Input(Content.str());
> >       Input.setCurrentDocument();
> >       Input.mapOptional(LocalName.data(), Result);
> >       if (Input.error())
> >         Result = Default;
> >     }
> >     return Result;
> >   }
> > ```
> > 
> > The hack is ugly since it uses `Input::setCurrentDocument()` (which should probably be private) and `Input::mapOptional()` and not `Input::operator>>`. That is so, because otherwise we would need creating a type specializing `llvm::yaml::MappingTraits` which would essentially do an operation equivalent to `Input.mapOptional(LocalName.str(), Result);`.
> That's essentially the same code as in `llvm/lib/Support/YAMLTraits.cpp::651`.
> Couldn't we just generalize this approach to all the types that specialize llvm::yaml::XYZTraits (XYZ being one of Scalar/ScalarEnumeration/...) ?

That sounds interesting. I'll try something along these lines.

http://reviews.llvm.org/D5602






More information about the cfe-commits mailing list