Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- ClangTidyTest.h - clang-tidy ---------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANG_TIDY_TEST_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANG_TIDY_TEST_H |
| 12 | |
| 13 | #include "ClangTidy.h" |
| 14 | #include "ClangTidyDiagnosticConsumer.h" |
| 15 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/Frontend/FrontendActions.h" |
| 18 | #include "clang/Tooling/Refactoring.h" |
| 19 | #include "clang/Tooling/Tooling.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
| 22 | namespace tidy { |
Alexander Kornienko | 0988716 | 2014-02-27 14:28:02 +0000 | [diff] [blame^] | 23 | namespace test { |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 24 | |
Alexander Kornienko | 0988716 | 2014-02-27 14:28:02 +0000 | [diff] [blame^] | 25 | class TestPPAction : public PreprocessOnlyAction { |
| 26 | public: |
| 27 | TestPPAction(ClangTidyCheck &Check, ClangTidyContext *Context) |
| 28 | : Check(Check), Context(Context) {} |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 29 | |
| 30 | private: |
Alexander Kornienko | 0988716 | 2014-02-27 14:28:02 +0000 | [diff] [blame^] | 31 | bool BeginSourceFileAction(CompilerInstance &Compiler, |
| 32 | llvm::StringRef file_name) LLVM_OVERRIDE { |
| 33 | Context->setSourceManager(&Compiler.getSourceManager()); |
| 34 | Check.registerPPCallbacks(Compiler); |
| 35 | return true; |
| 36 | } |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 37 | |
Alexander Kornienko | 0988716 | 2014-02-27 14:28:02 +0000 | [diff] [blame^] | 38 | ClangTidyCheck &Check; |
| 39 | ClangTidyContext *Context; |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Alexander Kornienko | 0988716 | 2014-02-27 14:28:02 +0000 | [diff] [blame^] | 42 | template <typename T> std::string runCheckOnCode(StringRef Code) { |
| 43 | T Check; |
| 44 | SmallVector<ClangTidyError, 16> Errors; |
| 45 | ClangTidyContext Context(&Errors); |
| 46 | ClangTidyDiagnosticConsumer DiagConsumer(Context); |
| 47 | Check.setContext(&Context); |
| 48 | |
| 49 | if (!tooling::runToolOnCode(new TestPPAction(Check, &Context), Code)) |
| 50 | return ""; |
| 51 | ast_matchers::MatchFinder Finder; |
| 52 | Check.registerMatchers(&Finder); |
| 53 | OwningPtr<tooling::FrontendActionFactory> Factory( |
| 54 | tooling::newFrontendActionFactory(&Finder)); |
| 55 | if (!tooling::runToolOnCode(Factory->create(), Code)) |
| 56 | return ""; |
| 57 | DiagConsumer.finish(); |
| 58 | tooling::Replacements Fixes; |
| 59 | for (SmallVector<ClangTidyError, 16>::const_iterator I = Errors.begin(), |
| 60 | E = Errors.end(); |
| 61 | I != E; ++I) |
| 62 | Fixes.insert(I->Fix.begin(), I->Fix.end()); |
| 63 | return tooling::applyAllReplacements(Code, Fixes); |
| 64 | } |
| 65 | |
| 66 | } // namespace test |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 67 | } // namespace tidy |
| 68 | } // namespace clang |
| 69 | |
| 70 | #endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANG_TIDY_TEST_H |