2013-07-29 16:19:24 +08:00
|
|
|
//===--- ClangTidyTest.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_UNITTESTS_CLANG_TIDY_CLANG_TIDY_TEST_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANG_TIDY_TEST_H
|
|
|
|
|
|
|
|
#include "ClangTidy.h"
|
|
|
|
#include "ClangTidyDiagnosticConsumer.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Tooling/Refactoring.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2014-02-27 22:28:02 +08:00
|
|
|
namespace test {
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
class TestPPAction : public PreprocessOnlyAction {
|
|
|
|
public:
|
|
|
|
TestPPAction(ClangTidyCheck &Check, ClangTidyContext *Context)
|
|
|
|
: Check(Check), Context(Context) {}
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
private:
|
2014-02-27 22:28:02 +08:00
|
|
|
bool BeginSourceFileAction(CompilerInstance &Compiler,
|
2014-03-02 18:20:11 +08:00
|
|
|
llvm::StringRef file_name) override {
|
2014-02-27 22:28:02 +08:00
|
|
|
Context->setSourceManager(&Compiler.getSourceManager());
|
|
|
|
Check.registerPPCallbacks(Compiler);
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
ClangTidyCheck &Check;
|
|
|
|
ClangTidyContext *Context;
|
|
|
|
};
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-04-08 20:27:49 +08:00
|
|
|
template <typename T>
|
|
|
|
std::string runCheckOnCode(StringRef Code,
|
|
|
|
SmallVectorImpl<ClangTidyError> &Errors) {
|
2014-02-27 22:28:02 +08:00
|
|
|
T Check;
|
2014-04-29 23:20:10 +08:00
|
|
|
ClangTidyContext Context(&Errors, ClangTidyOptions());
|
2014-02-27 22:28:02 +08:00
|
|
|
ClangTidyDiagnosticConsumer DiagConsumer(Context);
|
|
|
|
Check.setContext(&Context);
|
2014-03-20 18:52:51 +08:00
|
|
|
std::vector<std::string> ArgCXX11(1, "-std=c++11");
|
2014-02-27 22:28:02 +08:00
|
|
|
|
2014-03-19 20:48:22 +08:00
|
|
|
if (!tooling::runToolOnCodeWithArgs(new TestPPAction(Check, &Context), Code,
|
2014-03-20 18:52:51 +08:00
|
|
|
ArgCXX11))
|
2014-02-27 22:28:02 +08:00
|
|
|
return "";
|
|
|
|
ast_matchers::MatchFinder Finder;
|
|
|
|
Check.registerMatchers(&Finder);
|
2014-03-09 17:24:40 +08:00
|
|
|
std::unique_ptr<tooling::FrontendActionFactory> Factory(
|
2014-02-27 22:28:02 +08:00
|
|
|
tooling::newFrontendActionFactory(&Finder));
|
2014-03-20 18:52:51 +08:00
|
|
|
if (!tooling::runToolOnCodeWithArgs(Factory->create(), Code, ArgCXX11))
|
2014-02-27 22:28:02 +08:00
|
|
|
return "";
|
|
|
|
DiagConsumer.finish();
|
|
|
|
tooling::Replacements Fixes;
|
2014-04-29 23:20:10 +08:00
|
|
|
for (const ClangTidyError &Error : Errors)
|
|
|
|
Fixes.insert(Error.Fix.begin(), Error.Fix.end());
|
2014-02-27 22:28:02 +08:00
|
|
|
return tooling::applyAllReplacements(Code, Fixes);
|
|
|
|
}
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-04-08 20:27:49 +08:00
|
|
|
template <typename T> std::string runCheckOnCode(StringRef Code) {
|
|
|
|
SmallVector<ClangTidyError, 16> Errors;
|
|
|
|
return runCheckOnCode<T>(Code, Errors);
|
|
|
|
}
|
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
} // namespace test
|
2013-07-29 16:19:24 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANG_TIDY_TEST_H
|