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-10-07 23:49:36 +08:00
|
|
|
class TestClangTidyAction : public ASTFrontendAction {
|
2014-02-27 22:28:02 +08:00
|
|
|
public:
|
2014-10-07 23:49:36 +08:00
|
|
|
TestClangTidyAction(ClangTidyCheck &Check, ast_matchers::MatchFinder &Finder,
|
|
|
|
ClangTidyContext &Context)
|
|
|
|
: Check(Check), Finder(Finder), Context(Context) {}
|
2013-07-29 16:19:24 +08:00
|
|
|
|
|
|
|
private:
|
2014-10-07 23:49:36 +08:00
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
|
|
|
|
StringRef File) override {
|
|
|
|
Context.setSourceManager(&Compiler.getSourceManager());
|
2014-02-27 22:28:02 +08:00
|
|
|
Check.registerPPCallbacks(Compiler);
|
2014-10-07 23:49:36 +08:00
|
|
|
return Finder.newASTConsumer();
|
2014-02-27 22:28:02 +08:00
|
|
|
}
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
ClangTidyCheck &Check;
|
2014-10-07 23:49:36 +08:00
|
|
|
ast_matchers::MatchFinder &Finder;
|
|
|
|
ClangTidyContext &Context;
|
2014-02-27 22:28:02 +08:00
|
|
|
};
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-04-08 20:27:49 +08:00
|
|
|
template <typename T>
|
|
|
|
std::string runCheckOnCode(StringRef Code,
|
2014-08-12 20:12:54 +08:00
|
|
|
std::vector<ClangTidyError> *Errors = nullptr,
|
|
|
|
const Twine &Filename = "input.cc",
|
|
|
|
ArrayRef<std::string> ExtraArgs = None) {
|
2014-09-04 22:23:36 +08:00
|
|
|
ClangTidyOptions Options;
|
|
|
|
Options.Checks = "*";
|
|
|
|
ClangTidyContext Context(llvm::make_unique<DefaultOptionsProvider>(
|
|
|
|
ClangTidyGlobalOptions(), Options));
|
2014-02-27 22:28:02 +08:00
|
|
|
ClangTidyDiagnosticConsumer DiagConsumer(Context);
|
2014-09-12 16:53:36 +08:00
|
|
|
T Check("test-check", &Context);
|
2014-02-27 22:28:02 +08:00
|
|
|
ast_matchers::MatchFinder Finder;
|
|
|
|
Check.registerMatchers(&Finder);
|
2014-10-07 23:49:36 +08:00
|
|
|
|
|
|
|
std::vector<std::string> ArgCXX11(1, "clang-tidy");
|
|
|
|
ArgCXX11.push_back("-fsyntax-only");
|
|
|
|
ArgCXX11.push_back("-std=c++11");
|
|
|
|
ArgCXX11.insert(ArgCXX11.end(), ExtraArgs.begin(), ExtraArgs.end());
|
|
|
|
ArgCXX11.push_back(Filename.str());
|
2014-10-09 21:22:46 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<FileManager> Files(
|
2014-10-07 23:49:36 +08:00
|
|
|
new FileManager(FileSystemOptions()));
|
|
|
|
tooling::ToolInvocation Invocation(
|
2014-10-09 21:22:46 +08:00
|
|
|
ArgCXX11, new TestClangTidyAction(Check, Finder, Context), Files.get());
|
|
|
|
Invocation.mapVirtualFile(Filename.str(), Code);
|
2014-10-07 23:49:36 +08:00
|
|
|
Invocation.setDiagnosticConsumer(&DiagConsumer);
|
|
|
|
if (!Invocation.run())
|
2014-02-27 22:28:02 +08:00
|
|
|
return "";
|
2014-10-07 23:49:36 +08:00
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
DiagConsumer.finish();
|
|
|
|
tooling::Replacements Fixes;
|
2014-05-09 20:24:09 +08:00
|
|
|
for (const ClangTidyError &Error : Context.getErrors())
|
2014-04-29 23:20:10 +08:00
|
|
|
Fixes.insert(Error.Fix.begin(), Error.Fix.end());
|
2014-05-09 20:24:09 +08:00
|
|
|
if (Errors)
|
|
|
|
*Errors = Context.getErrors();
|
2014-02-27 22:28:02 +08:00
|
|
|
return tooling::applyAllReplacements(Code, Fixes);
|
|
|
|
}
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2015-03-02 19:55:04 +08:00
|
|
|
#define EXPECT_NO_CHANGES(Check, Code) \
|
|
|
|
EXPECT_EQ(Code, runCheckOnCode<Check>(Code))
|
|
|
|
|
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
|