diff options
| author | Alexander Kornienko <alexfh@google.com> | 2014-09-12 08:53:36 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2014-09-12 08:53:36 +0000 |
| commit | 6e0cbc89471c210c59cd080901b1dfe656db117f (patch) | |
| tree | dec7c96c3799366aafa7ddcb51866a8248eb8e31 /clang-tools-extra/clang-tidy/ClangTidy.h | |
| parent | 313f5e2f29c1826f6aa8bc91f5862bf556cac03b (diff) | |
| download | bcm5719-llvm-6e0cbc89471c210c59cd080901b1dfe656db117f.tar.gz bcm5719-llvm-6e0cbc89471c210c59cd080901b1dfe656db117f.zip | |
Implemented clang-tidy-check-specific options.
Summary:
Each check can implement readOptions and storeOptions methods to read
and store custom options. Each check's options are stored in a local namespace
to avoid name collisions and provide some sort of context to the user.
Reviewers: bkramer, klimek
Reviewed By: klimek
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D5296
llvm-svn: 217661
Diffstat (limited to 'clang-tools-extra/clang-tidy/ClangTidy.h')
| -rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidy.h | 96 |
1 files changed, 87 insertions, 9 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.h b/clang-tools-extra/clang-tidy/ClangTidy.h index 268a2d20471..0892298c09f 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.h +++ b/clang-tools-extra/clang-tidy/ClangTidy.h @@ -16,7 +16,10 @@ #include "clang/Basic/Diagnostic.h" #include "clang/Basic/SourceManager.h" #include "clang/Tooling/Refactoring.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/raw_ostream.h" #include <memory> +#include <type_traits> #include <vector> namespace clang { @@ -28,6 +31,55 @@ class CompilationDatabase; namespace tidy { +/// \brief Provides access to the \c ClangTidyCheck options via check-local +/// names. +/// +/// Methods of this class prepend <tt>CheckName + "."</tt> to translate +/// check-local option names to global option names. +class OptionsView { +public: + /// \brief Initializes the instance using \p CheckName + "." as a prefix. + OptionsView(StringRef CheckName, + const ClangTidyOptions::OptionMap &CheckOptions); + + /// \brief Read a named option from the \c Context. + /// + /// Reads the option with the check-local name \p LocalName from the + /// \c CheckOptions. If the corresponding key is not present, returns + /// \p Default. + std::string get(StringRef LocalName, std::string Default) const; + + /// \brief Read a named option from the \c Context and parse it as an integral + /// type \c T. + /// + /// Reads the option with the check-local name \p LocalName from the + /// \c CheckOptions. If the corresponding key is not present, returns + /// \p Default. + template <typename T> + typename std::enable_if<std::is_integral<T>::value, T>::type + get(StringRef LocalName, T Default) const { + std::string Value = get(LocalName, ""); + T Result = Default; + if (!Value.empty()) + StringRef(Value).getAsInteger(10, Result); + return Result; + } + + /// \brief Stores an option with the check-local name \p LocalName with string + /// value \p Value to \p Options. + void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, + StringRef Value) const; + + /// \brief Stores an option with the check-local name \p LocalName with + /// \c int64_t value \p Value to \p Options. + void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, + int64_t Value) const; + +private: + std::string NamePrefix; + const ClangTidyOptions::OptionMap &CheckOptions; +}; + /// \brief Base class for all clang-tidy checks. /// /// To implement a \c ClangTidyCheck, write a subclass and overwrite some of the @@ -49,6 +101,18 @@ namespace tidy { /// useful/necessary. class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { public: + /// \brief Initializes the check with \p CheckName and \p Context. + /// + /// Derived classes must implement the constructor with this signature or + /// delegate it. If a check needs to read options, it can do this in the + /// constructor using the Options.get() methods below. + ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context) + : CheckName(CheckName), Context(Context), + Options(CheckName, Context->getOptions().CheckOptions) { + assert(Context != nullptr); + assert(!CheckName.empty()); + } + virtual ~ClangTidyCheck() {} /// \brief Overwrite this to register \c PPCallbacks with \c Compiler. @@ -75,21 +139,24 @@ public: /// work in here. virtual void check(const ast_matchers::MatchFinder::MatchResult &Result) {} - /// \brief The infrastructure sets the context to \p Ctx with this function. - void setContext(ClangTidyContext *Ctx) { Context = Ctx; } - /// \brief Add a diagnostic with the check's name. DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level = DiagnosticIDs::Warning); - /// \brief Sets the check name. Intended to be used by the clang-tidy - /// framework. Can be called only once. - void setName(StringRef Name); + /// \brief Should store all options supported by this check with their + /// current values or default values for options that haven't been overridden. + /// + /// The check should use \c Options.store() to store each option it supports + /// whether it has the default value or it has been overridden. + virtual void storeOptions(ClangTidyOptions::OptionMap &Options) {} private: void run(const ast_matchers::MatchFinder::MatchResult &Result) override; - ClangTidyContext *Context; std::string CheckName; + ClangTidyContext *Context; + +protected: + OptionsView Options; }; class ClangTidyCheckFactories; @@ -103,10 +170,13 @@ public: CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File); /// \brief Get the list of enabled checks. - std::vector<std::string> getCheckNames(GlobList &Filter); + std::vector<std::string> getCheckNames(); + + /// \brief Get the union of options from all checks. + ClangTidyOptions::OptionMap getCheckOptions(); private: - typedef std::vector<std::pair<std::string, bool> > CheckersList; + typedef std::vector<std::pair<std::string, bool>> CheckersList; CheckersList getCheckersControlList(GlobList &Filter); ClangTidyContext &Context; @@ -117,6 +187,14 @@ private: /// filters are applied. std::vector<std::string> getCheckNames(const ClangTidyOptions &Options); +/// \brief Returns the effective check-specific options. +/// +/// The method configures ClangTidy with the specified \p Options and collects +/// effective options from all created checks. The returned set of options +/// includes default check-specific options for all keys not overridden by \p +/// Options. +ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options); + /// \brief Run a set of clang-tidy checks on a set of files. ClangTidyStats runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider, |

