Add ClangTidyOptions to encapsulate all clang-tidy options.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D3544

git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@207532 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alexander Kornienko 2014-04-29 15:20:10 +00:00
Родитель e8edbb569d
Коммит 6c8a52b813
7 изменённых файлов: 52 добавлений и 27 удалений

Просмотреть файл

@ -285,24 +285,21 @@ void ClangTidyCheck::setName(StringRef Name) {
CheckName = Name.str();
}
std::vector<std::string> getCheckNames(StringRef EnableChecksRegex,
StringRef DisableChecksRegex) {
std::vector<std::string> getCheckNames(const ClangTidyOptions &Options) {
SmallVector<ClangTidyError, 8> Errors;
clang::tidy::ClangTidyContext Context(&Errors, EnableChecksRegex,
DisableChecksRegex);
clang::tidy::ClangTidyContext Context(&Errors, Options);
ClangTidyASTConsumerFactory Factory(Context);
return Factory.getCheckNames();
}
void runClangTidy(StringRef EnableChecksRegex, StringRef DisableChecksRegex,
void runClangTidy(const ClangTidyOptions &Options,
const tooling::CompilationDatabase &Compilations,
ArrayRef<std::string> Ranges,
SmallVectorImpl<ClangTidyError> *Errors) {
// FIXME: Ranges are currently full files. Support selecting specific
// (line-)ranges.
ClangTool Tool(Compilations, Ranges);
clang::tidy::ClangTidyContext Context(Errors, EnableChecksRegex,
DisableChecksRegex);
clang::tidy::ClangTidyContext Context(Errors, Options);
ClangTidyDiagnosticConsumer DiagConsumer(Context);
Tool.setDiagnosticConsumer(&DiagConsumer);

Просмотреть файл

@ -11,6 +11,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_H
#include "ClangTidyDiagnosticConsumer.h"
#include "ClangTidyOptions.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceManager.h"
@ -115,11 +116,10 @@ private:
/// \brief Fills the list of check names that are enabled when the provided
/// filters are applied.
std::vector<std::string> getCheckNames(StringRef EnableChecksRegex,
StringRef DisableChecksRegex);
std::vector<std::string> getCheckNames(const ClangTidyOptions &Options);
/// \brief Run a set of clang-tidy checks on a set of files.
void runClangTidy(StringRef EnableChecksRegex, StringRef DisableChecksRegex,
void runClangTidy(const ClangTidyOptions &Options,
const tooling::CompilationDatabase &Compilations,
ArrayRef<std::string> Ranges,
SmallVectorImpl<ClangTidyError> *Errors);

Просмотреть файл

@ -18,6 +18,7 @@
#include "ClangTidyDiagnosticConsumer.h"
#include "ClangTidyOptions.h"
#include "clang/Frontend/DiagnosticRenderer.h"
#include "llvm/ADT/SmallString.h"
@ -113,19 +114,17 @@ ClangTidyMessage::ClangTidyMessage(StringRef Message,
ClangTidyError::ClangTidyError(StringRef CheckName)
: CheckName(CheckName) {}
ChecksFilter::ChecksFilter(StringRef EnableChecksRegex,
StringRef DisableChecksRegex)
: EnableChecks(EnableChecksRegex), DisableChecks(DisableChecksRegex) {}
ChecksFilter::ChecksFilter(const ClangTidyOptions &Options)
: EnableChecks(Options.EnableChecksRegex),
DisableChecks(Options.DisableChecksRegex) {}
bool ChecksFilter::isCheckEnabled(StringRef Name) {
return EnableChecks.match(Name) && !DisableChecks.match(Name);
}
ClangTidyContext::ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors,
StringRef EnableChecksRegex,
StringRef DisableChecksRegex)
: Errors(Errors), DiagEngine(nullptr),
Filter(EnableChecksRegex, DisableChecksRegex) {}
const ClangTidyOptions &Options)
: Errors(Errors), DiagEngine(nullptr), Filter(Options) {}
DiagnosticBuilder ClangTidyContext::diag(
StringRef CheckName, SourceLocation Loc, StringRef Description,

Просмотреть файл

@ -28,6 +28,8 @@ class CompilationDatabase;
namespace tidy {
struct ClangTidyOptions;
/// \brief A message from a clang-tidy check.
///
/// Note that this is independent of a \c SourceManager.
@ -59,7 +61,7 @@ struct ClangTidyError {
/// \brief Filters checks by name.
class ChecksFilter {
public:
ChecksFilter(StringRef EnableChecksRegex, StringRef DisableChecksRegex);
ChecksFilter(const ClangTidyOptions& Options);
bool isCheckEnabled(StringRef Name);
private:
@ -79,7 +81,7 @@ private:
class ClangTidyContext {
public:
ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors,
StringRef EnableChecksRegex, StringRef DisableChecksRegex);
const ClangTidyOptions &Options);
/// \brief Report any errors detected using this method.
///

Просмотреть файл

@ -0,0 +1,26 @@
//===--- ClangTidyOptions.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.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H
namespace clang {
namespace tidy {
/// \brief Contains options for clang-tidy.
struct ClangTidyOptions {
ClangTidyOptions() : EnableChecksRegex(".*") {}
std::string EnableChecksRegex;
std::string DisableChecksRegex;
};
} // end namespace tidy
} // end namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H

Просмотреть файл

@ -49,18 +49,21 @@ static cl::opt<bool> ListChecks("list-checks",
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
clang::tidy::ClangTidyOptions Options;
Options.EnableChecksRegex = Checks;
Options.DisableChecksRegex = DisableChecks;
// FIXME: Allow using --list-checks without positional arguments.
if (ListChecks) {
llvm::outs() << "Enabled checks:";
for (auto CheckName : clang::tidy::getCheckNames(Checks, DisableChecks))
for (auto CheckName : clang::tidy::getCheckNames(Options))
llvm::outs() << "\n " << CheckName;
llvm::outs() << "\n\n";
return 0;
}
SmallVector<clang::tidy::ClangTidyError, 16> Errors;
clang::tidy::runClangTidy(Checks, DisableChecks,
OptionsParser.getCompilations(),
clang::tidy::runClangTidy(Options, OptionsParser.getCompilations(),
OptionsParser.getSourcePathList(), &Errors);
clang::tidy::handleErrors(Errors, Fix);

Просмотреть файл

@ -43,7 +43,7 @@ template <typename T>
std::string runCheckOnCode(StringRef Code,
SmallVectorImpl<ClangTidyError> &Errors) {
T Check;
ClangTidyContext Context(&Errors, ".*", "");
ClangTidyContext Context(&Errors, ClangTidyOptions());
ClangTidyDiagnosticConsumer DiagConsumer(Context);
Check.setContext(&Context);
std::vector<std::string> ArgCXX11(1, "-std=c++11");
@ -59,10 +59,8 @@ std::string runCheckOnCode(StringRef Code,
return "";
DiagConsumer.finish();
tooling::Replacements Fixes;
for (SmallVector<ClangTidyError, 16>::const_iterator I = Errors.begin(),
E = Errors.end();
I != E; ++I)
Fixes.insert(I->Fix.begin(), I->Fix.end());
for (const ClangTidyError &Error : Errors)
Fixes.insert(Error.Fix.begin(), Error.Fix.end());
return tooling::applyAllReplacements(Code, Fixes);
}