[PATCH] Implemented clang-tidy-check-specific options.

Alexander Kornienko alexfh at google.com
Thu Sep 11 06:40:16 PDT 2014


================
Comment at: clang-tidy/ClangTidy.h:99-100
@@ +98,4 @@
+  ///
+  /// The check should use \c storeOption() to store each option it supports
+  /// whether it has the default value or has been overridden.
+  virtual void storeOptions(ClangTidyOptions::OptionMap &Options) {}
----------------
klimek wrote:
> or *it* has been overridden?
Done.

================
Comment at: clang-tidy/ClangTidy.h:104-150
@@ -88,1 +103,49 @@
+protected:
+  /// \brief Read a named option from the \c Context.
+  ///
+  /// Reads the option named <code>CheckName + "." +</code> \p LocalName
+  /// from the current options in the \c Context. If the corresponding key is
+  /// not present, returns \p Default.
+  std::string getOption(StringRef LocalName, std::string Default) const {
+    const auto &Options = Context->getOptions().CheckOptions;
+    const auto &Iter = Options.find(getQualifiedOptionName(LocalName));
+    if (Iter != Options.end())
+      return Iter->second;
+    return Default;
+  }
+
+  /// \brief Read a named option from the \c Context and parse it as \c T.
+  ///
+  /// Reads the option named <code>CheckName + "." +</code> \p LocalName
+  /// from the current options in the \c Context. If the corresponding key is
+  /// not present, returns \p Default.
+  template <typename T>
+  typename std::enable_if<std::is_integral<T>::value, T>::type
+  getOption(StringRef LocalName, T Default) const {
+    std::string Value = getOption(LocalName, "");
+    T Result = Default;
+    if (!Value.empty())
+      StringRef(Value).getAsInteger(10, Result);
+    return Result;
+  }
+
+  /// \brief Stores an option named \p LocalName with string value \p Value to
+  /// \p Options.
+  ///
+  /// The function calls \c getQualifiedOptionName(LocalName) to get the global
+  /// option name by prepending the check name.
+  void storeOption(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
+                   StringRef Value) const {
+    Options[getQualifiedOptionName(LocalName)] = Value;
+  }
+
+  /// \brief Stores an option named \p LocalName with \c int64_t value \p Value
+  /// to \p Options.
+  ///
+  /// The function calls \c getQualifiedOptionName(LocalName) to get the global
+  /// option name by prepending the check name.
+  void storeOption(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
+                   int64_t Value) const {
+    storeOption(Options, LocalName, llvm::itostr(Value));
+  }
 
----------------
klimek wrote:
> I think those should go somewhere else.
> 
> I'd vote for free-standing functions...
These functions call ClangTidyCheck::getQualifiedOptionName which otherwise would need to be called from the user code:

  ...
    : ShortNamespaceLines(getOption<unsigned>(getQualifiedOptionName("ShortNamespaceLines"), 1u)),
      SpacesBeforeComments(getOption<unsigned>(getQualifiedOptionName("SpacesBeforeComments"), 1u)) ...

IMO, we need to reduce boilerplate code here as much as we can.

http://reviews.llvm.org/D5296






More information about the cfe-commits mailing list