2013-07-29 16:19:24 +08:00
|
|
|
//===--- ClangTidy.h - clang-tidy -------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-10 00:52:33 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-01-08 04:05:01 +08:00
|
|
|
#include "ClangTidyDiagnosticConsumer.h"
|
2014-04-29 23:20:10 +08:00
|
|
|
#include "ClangTidyOptions.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Tooling/Refactoring.h"
|
2014-09-12 16:53:36 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-05 21:31:45 +08:00
|
|
|
#include <memory>
|
2014-09-12 16:53:36 +08:00
|
|
|
#include <type_traits>
|
2014-05-09 20:24:09 +08:00
|
|
|
#include <vector>
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
class CompilerInstance;
|
|
|
|
namespace tooling {
|
|
|
|
class CompilationDatabase;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace tidy {
|
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
/// \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.
|
2016-02-05 19:23:59 +08:00
|
|
|
std::string get(StringRef LocalName, StringRef Default) const;
|
|
|
|
|
|
|
|
/// \brief Read a named option from the \c Context.
|
|
|
|
///
|
|
|
|
/// Reads the option with the check-local name \p LocalName from local or
|
|
|
|
/// global \c CheckOptions. Gets local option first. If local is not
|
|
|
|
/// present, falls back to get global option. If global option is not present
|
|
|
|
/// either, returns Default.
|
|
|
|
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const;
|
2014-09-12 16:53:36 +08:00
|
|
|
|
|
|
|
/// \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;
|
|
|
|
};
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
/// \brief Base class for all clang-tidy checks.
|
|
|
|
///
|
2015-02-25 21:05:33 +08:00
|
|
|
/// To implement a \c ClangTidyCheck, write a subclass and override some of the
|
2013-07-29 16:19:24 +08:00
|
|
|
/// base class's methods. E.g. to implement a check that validates namespace
|
2015-02-25 21:05:33 +08:00
|
|
|
/// declarations, override \c registerMatchers:
|
2013-07-29 16:19:24 +08:00
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// registerMatchers(ast_matchers::MatchFinder *Finder) {
|
|
|
|
/// Finder->addMatcher(namespaceDecl().bind("namespace"), this);
|
|
|
|
/// }
|
|
|
|
/// \endcode
|
|
|
|
///
|
2015-02-25 21:05:33 +08:00
|
|
|
/// and then override \c check(const MatchResult &Result) to do the actual
|
2013-07-29 16:19:24 +08:00
|
|
|
/// check for each match.
|
|
|
|
///
|
|
|
|
/// A new \c ClangTidyCheck instance is created per translation unit.
|
|
|
|
///
|
|
|
|
/// FIXME: Figure out whether carrying information from one TU to another is
|
|
|
|
/// useful/necessary.
|
|
|
|
class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
|
|
|
|
public:
|
2014-09-12 16:53:36 +08:00
|
|
|
/// \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());
|
|
|
|
}
|
|
|
|
|
2015-02-25 21:05:33 +08:00
|
|
|
/// \brief Override this to register \c PPCallbacks with \c Compiler.
|
2013-07-29 16:19:24 +08:00
|
|
|
///
|
|
|
|
/// This should be used for clang-tidy checks that analyze preprocessor-
|
|
|
|
/// dependent properties, e.g. the order of include directives.
|
|
|
|
virtual void registerPPCallbacks(CompilerInstance &Compiler) {}
|
|
|
|
|
2015-02-25 21:05:33 +08:00
|
|
|
/// \brief Override this to register ASTMatchers with \p Finder.
|
2013-07-29 16:19:24 +08:00
|
|
|
///
|
|
|
|
/// This should be used by clang-tidy checks that analyze code properties that
|
|
|
|
/// dependent on AST knowledge.
|
|
|
|
///
|
|
|
|
/// You can register as many matchers as necessary with \p Finder. Usually,
|
|
|
|
/// "this" will be used as callback, but you can also specify other callback
|
|
|
|
/// classes. Thereby, different matchers can trigger different callbacks.
|
|
|
|
///
|
|
|
|
/// If you need to merge information between the different matchers, you can
|
|
|
|
/// store these as members of the derived class. However, note that all
|
|
|
|
/// matches occur in the order of the AST traversal.
|
|
|
|
virtual void registerMatchers(ast_matchers::MatchFinder *Finder) {}
|
|
|
|
|
|
|
|
/// \brief \c ClangTidyChecks that register ASTMatchers should do the actual
|
|
|
|
/// work in here.
|
|
|
|
virtual void check(const ast_matchers::MatchFinder::MatchResult &Result) {}
|
|
|
|
|
2014-01-13 18:50:51 +08:00
|
|
|
/// \brief Add a diagnostic with the check's name.
|
2014-03-03 07:34:48 +08:00
|
|
|
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description,
|
|
|
|
DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
|
2014-01-13 18:50:51 +08:00
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
/// \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) {}
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
private:
|
2014-03-02 18:20:11 +08:00
|
|
|
void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
|
2014-10-24 01:23:20 +08:00
|
|
|
StringRef getID() const override { return CheckName; }
|
2014-01-13 18:50:51 +08:00
|
|
|
std::string CheckName;
|
2014-09-12 16:53:36 +08:00
|
|
|
ClangTidyContext *Context;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
OptionsView Options;
|
2015-05-21 22:08:56 +08:00
|
|
|
/// \brief Returns the main file name of the current translation unit.
|
|
|
|
StringRef getCurrentMainFile() const { return Context->getCurrentFile(); }
|
2015-08-28 21:20:46 +08:00
|
|
|
/// \brief Returns the language options from the context.
|
|
|
|
LangOptions getLangOpts() const { return Context->getLangOpts(); }
|
2013-07-29 16:19:24 +08:00
|
|
|
};
|
|
|
|
|
2014-01-03 17:31:57 +08:00
|
|
|
class ClangTidyCheckFactories;
|
|
|
|
|
|
|
|
class ClangTidyASTConsumerFactory {
|
|
|
|
public:
|
2014-06-05 21:31:45 +08:00
|
|
|
ClangTidyASTConsumerFactory(ClangTidyContext &Context);
|
2014-01-03 17:31:57 +08:00
|
|
|
|
|
|
|
/// \brief Returns an ASTConsumer that runs the specified clang-tidy checks.
|
2014-08-11 03:56:59 +08:00
|
|
|
std::unique_ptr<clang::ASTConsumer>
|
|
|
|
CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File);
|
2014-01-03 17:31:57 +08:00
|
|
|
|
|
|
|
/// \brief Get the list of enabled checks.
|
2014-09-12 16:53:36 +08:00
|
|
|
std::vector<std::string> getCheckNames();
|
|
|
|
|
|
|
|
/// \brief Get the union of options from all checks.
|
|
|
|
ClangTidyOptions::OptionMap getCheckOptions();
|
2014-01-03 17:31:57 +08:00
|
|
|
|
|
|
|
private:
|
2014-09-12 16:53:36 +08:00
|
|
|
typedef std::vector<std::pair<std::string, bool>> CheckersList;
|
2014-08-06 19:49:10 +08:00
|
|
|
CheckersList getCheckersControlList(GlobList &Filter);
|
2014-01-03 17:31:57 +08:00
|
|
|
|
|
|
|
ClangTidyContext &Context;
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<ClangTidyCheckFactories> CheckFactories;
|
2014-01-03 17:31:57 +08:00
|
|
|
};
|
|
|
|
|
Clang-tidy: added --disable-checks, --list-checks options.
Summary:
Allow disabling checks by regex. By default, disable alpha.* checks,
that are not particularly good tested (e.g. IdempotentOperationChecker, see
http://llvm-reviews.chandlerc.com/D2427).
Fixed a bug, that would disable all analyzer checks, when using a regex more
strict, than 'clang-analyzer-', for example --checks='clang-analyzer-deadcode-'.
Added --list-checks to list all enabled checks. This is useful to test specific
values in --checks/--disable-checks.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2444
llvm-svn: 197717
2013-12-20 03:57:05 +08:00
|
|
|
/// \brief Fills the list of check names that are enabled when the provided
|
|
|
|
/// filters are applied.
|
2014-04-29 23:20:10 +08:00
|
|
|
std::vector<std::string> getCheckNames(const ClangTidyOptions &Options);
|
Clang-tidy: added --disable-checks, --list-checks options.
Summary:
Allow disabling checks by regex. By default, disable alpha.* checks,
that are not particularly good tested (e.g. IdempotentOperationChecker, see
http://llvm-reviews.chandlerc.com/D2427).
Fixed a bug, that would disable all analyzer checks, when using a regex more
strict, than 'clang-analyzer-', for example --checks='clang-analyzer-deadcode-'.
Added --list-checks to list all enabled checks. This is useful to test specific
values in --checks/--disable-checks.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2444
llvm-svn: 197717
2013-12-20 03:57:05 +08:00
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
/// \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);
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
/// \brief Run a set of clang-tidy checks on a set of files.
|
2014-10-24 01:23:20 +08:00
|
|
|
///
|
|
|
|
/// \param Profile if provided, it enables check profile collection in
|
|
|
|
/// MatchFinder, and will contain the result of the profile.
|
2014-06-05 21:31:45 +08:00
|
|
|
ClangTidyStats
|
2014-09-04 22:23:36 +08:00
|
|
|
runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
|
2014-06-05 21:31:45 +08:00
|
|
|
const tooling::CompilationDatabase &Compilations,
|
|
|
|
ArrayRef<std::string> InputFiles,
|
2014-10-24 01:23:20 +08:00
|
|
|
std::vector<ClangTidyError> *Errors,
|
|
|
|
ProfileData *Profile = nullptr);
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
// FIXME: This interface will need to be significantly extended to be useful.
|
|
|
|
// FIXME: Implement confidence levels for displaying/fixing errors.
|
|
|
|
//
|
|
|
|
/// \brief Displays the found \p Errors to the users. If \p Fix is true, \p
|
|
|
|
/// Errors containing fixes are automatically applied.
|
2016-01-14 01:36:41 +08:00
|
|
|
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix,
|
|
|
|
unsigned &WarningsAsErrorsCount);
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-09-04 18:31:23 +08:00
|
|
|
/// \brief Serializes replacements into YAML and writes them to the specified
|
|
|
|
/// output stream.
|
|
|
|
void exportReplacements(const std::vector<ClangTidyError> &Errors,
|
|
|
|
raw_ostream &OS);
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
} // end namespace tidy
|
|
|
|
} // end namespace clang
|
|
|
|
|
2015-03-10 00:52:33 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
|