diff options
Diffstat (limited to 'clang-tools-extra/unittests/clang-tidy')
5 files changed, 160 insertions, 0 deletions
diff --git a/clang-tools-extra/unittests/clang-tidy/CMakeLists.txt b/clang-tools-extra/unittests/clang-tidy/CMakeLists.txt new file mode 100644 index 00000000000..d0500428a20 --- /dev/null +++ b/clang-tools-extra/unittests/clang-tidy/CMakeLists.txt @@ -0,0 +1,20 @@ +set(LLVM_LINK_COMPONENTS + support + ) + +get_filename_component(CLANG_LINT_SOURCE_DIR + ${CMAKE_CURRENT_SOURCE_DIR}/../../clang-tidy REALPATH) +include_directories(${CLANG_LINT_SOURCE_DIR}) + +add_extra_unittest(ClangTidyTests + LLVMModuleTest.cpp + GoogleModuleTest.cpp) + +target_link_libraries(ClangTidyTests + gtest + gtest_main + clangTidy + clangTooling + clangBasic + clangASTMatchers + ) diff --git a/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h b/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h new file mode 100644 index 00000000000..bd230285c43 --- /dev/null +++ b/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h @@ -0,0 +1,75 @@ +//===--- 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" +#include "gtest/gtest.h" + +namespace clang { +namespace tidy { + +template <typename T> class ClangTidyTest : public ::testing::Test { +protected: + ClangTidyTest() : Check(new T), Context(&Errors) {} + + std::string runCheckOn(StringRef Code) { + ClangTidyDiagnosticConsumer DiagConsumer(Context); + Check->setContext(&Context); + EXPECT_TRUE( + tooling::runToolOnCode(new TestPPAction(*Check, &Context), Code)); + ast_matchers::MatchFinder Finder; + Check->registerMatchers(&Finder); + OwningPtr<tooling::FrontendActionFactory> Factory( + tooling::newFrontendActionFactory(&Finder)); + EXPECT_TRUE(tooling::runToolOnCode(Factory->create(), Code)); + 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()); + return tooling::applyAllReplacements(Code, Fixes); + } + + void expectNoChanges(StringRef Code) { EXPECT_EQ(Code, runCheckOn(Code)); } + +private: + class TestPPAction : public PreprocessOnlyAction { + public: + TestPPAction(ClangTidyCheck &Check, ClangTidyContext *Context) + : Check(Check), Context(Context) {} + + private: + virtual bool BeginSourceFileAction(CompilerInstance &Compiler, + llvm::StringRef file_name) { + Context->setSourceManager(&Compiler.getSourceManager()); + Check.registerPPCallbacks(Compiler); + return true; + } + + ClangTidyCheck &Check; + ClangTidyContext *Context; + }; + + OwningPtr<ClangTidyCheck> Check; + SmallVector<ClangTidyError, 16> Errors; + ClangTidyContext Context; +}; + +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANG_TIDY_TEST_H diff --git a/clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp b/clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp new file mode 100644 index 00000000000..26b85b929d6 --- /dev/null +++ b/clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp @@ -0,0 +1,26 @@ +#include "ClangTidyTest.h" + +#include "google/GoogleTidyModule.h" + +namespace clang { +namespace tidy { + +typedef ClangTidyTest<ExplicitConstructorCheck> ExplicitConstructorCheckTest; + +TEST_F(ExplicitConstructorCheckTest, SingleArgumentConstructorsOnly) { + expectNoChanges("class C { C(); };"); + expectNoChanges("class C { C(int i, int j); };"); +} + +TEST_F(ExplicitConstructorCheckTest, Basic) { + EXPECT_EQ("class C { explicit C(int i); };", + runCheckOn("class C { C(int i); };")); +} + +TEST_F(ExplicitConstructorCheckTest, DefaultParameters) { + EXPECT_EQ("class C { explicit C(int i, int j = 0); };", + runCheckOn("class C { C(int i, int j = 0); };")); +} + +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp b/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp new file mode 100644 index 00000000000..da1d9e0db91 --- /dev/null +++ b/clang-tools-extra/unittests/clang-tidy/LLVMModuleTest.cpp @@ -0,0 +1,15 @@ +#include "ClangTidyTest.h" + +#include "llvm/LLVMTidyModule.h" + +namespace clang { +namespace tidy { + +typedef ClangTidyTest<NamespaceCommentCheck> NamespaceCommentCheckTest; + +TEST_F(NamespaceCommentCheckTest, Basic) { + EXPECT_EQ("namespace i {\n} // namespace i", runCheckOn("namespace i {\n}")); +} + +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/unittests/clang-tidy/Makefile b/clang-tools-extra/unittests/clang-tidy/Makefile new file mode 100644 index 00000000000..2c617ef3f51 --- /dev/null +++ b/clang-tools-extra/unittests/clang-tidy/Makefile @@ -0,0 +1,24 @@ +##===- unittests/clang-tidy/Makefile -----------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +CLANG_LEVEL = ../../../.. +include $(CLANG_LEVEL)/../../Makefile.config + +TESTNAME = ClangTidy +LINK_COMPONENTS := asmparser bitreader support MC MCParser option +USEDLIBS = clangTidy.a clangTidyLLVMModule.a clangTidyGoogleModule.a \ + clangFormat.a clangTooling.a clangFrontend.a clangSerialization.a \ + clangDriver.a clangRewriteFrontend.a clangRewriteCore.a \ + clangParse.a clangSema.a clangAnalysis.a clangAST.a \ + clangASTMatchers.a clangEdit.a clangLex.a clangBasic.a + +include $(CLANG_LEVEL)/Makefile +MAKEFILE_UNITTEST_NO_INCLUDE_COMMON := 1 +CPP.Flags += -I$(PROJ_SRC_DIR)/../../clang-tidy +include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest |