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

Manuel Klimek klimek at google.com
Thu Sep 11 05:35:21 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) {}
----------------
or *it* has been overridden?

================
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));
+  }
 
----------------
I think those should go somewhere else.

I'd vote for free-standing functions...

================
Comment at: clang-tidy/llvm/NamespaceCommentCheck.cpp:21-28
@@ -22,6 +20,10 @@
 
-NamespaceCommentCheck::NamespaceCommentCheck()
-    : NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
+NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
+                                             ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
                               "namespace( +([a-zA-Z0-9_]+))? *(\\*/)?$",
                               llvm::Regex::IgnoreCase),
-      ShortNamespaceLines(1) {}
+      ShortNamespaceLines(getOption<unsigned>("ShortNamespaceLines", 1u)),
+      SpacesBeforeComments(getOption<unsigned>("SpacesBeforeComments", 1u)) {}
+
----------------
\o/

http://reviews.llvm.org/D5296






More information about the cfe-commits mailing list