File: ClangTidyTest.h

package info (click to toggle)
llvm-toolchain-3.4 1%3A3.4.2-13
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 253,236 kB
  • ctags: 276,203
  • sloc: cpp: 1,665,580; ansic: 298,647; asm: 206,157; objc: 84,350; python: 73,119; sh: 23,466; perl: 5,679; makefile: 5,542; ml: 5,250; pascal: 2,467; lisp: 1,420; xml: 679; cs: 236; csh: 117
file content (76 lines) | stat: -rw-r--r-- 2,598 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//===--- 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));
    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());
    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