blob: 7df66684e29965eca7c83a560c77f9ad8edd0d20 [file] [log] [blame]
Haojian Wuada28622016-10-17 08:33:59 +00001//===---- UsingInserterTest.cpp - clang-tidy ----------------------------===//
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#include "../clang-tidy/utils/UsingInserter.h"
11
12#include "ClangTidyTest.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/ASTMatchers/ASTMatchers.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace tidy {
19namespace utils {
20
21// Replace all function calls with calls to foo::func. Inserts using
22// declarations as necessary. This checker is for testing only. It
23// can only run on one test case (e.g. wih one SourceManager).
24class InsertUsingCheck : public clang::tidy::ClangTidyCheck {
25public:
26 using clang::tidy::ClangTidyCheck::ClangTidyCheck;
27 void registerMatchers(clang::ast_matchers::MatchFinder *Finder) override {
28 Finder->addMatcher(clang::ast_matchers::callExpr().bind("foo"), this);
29 }
30 void
31 check(const clang::ast_matchers::MatchFinder::MatchResult &Result) override {
32 if (!Inserter)
33 Inserter.reset(new UsingInserter(*Result.SourceManager));
34
35 const clang::CallExpr *Call =
36 Result.Nodes.getNodeAs<clang::CallExpr>("foo");
37 assert(Call != nullptr && "Did not find node \"foo\"");
38 auto Hint =
39 Inserter->createUsingDeclaration(*Result.Context, *Call, "::foo::func");
40
41 if (Hint.hasValue())
42 diag(Call->getLocStart(), "Fix for testing") << Hint.getValue();
43
44 diag(Call->getLocStart(), "insert call")
45 << clang::FixItHint::CreateReplacement(
46 Call->getCallee()->getSourceRange(),
47 Inserter->getShortName(*Result.Context, *Call, "::foo::func"));
48 }
49
50private:
51 std::unique_ptr<UsingInserter> Inserter;
52};
53
54template <typename Check>
55std::string runChecker(StringRef Code, int ExpectedWarningCount) {
56 std::map<StringRef, StringRef> AdditionalFileContents = {{"foo.h",
57 "namespace foo {\n"
58 "namespace bar {\n"
59 "}\n"
60 "void func() { }\n"
61 "}"}};
62 std::vector<ClangTidyError> errors;
63
64 std::string result =
65 test::runCheckOnCode<Check>(Code, &errors, "foo.cc", None,
66 ClangTidyOptions(), AdditionalFileContents);
67
68 EXPECT_EQ(ExpectedWarningCount, errors.size());
69 return result;
70}
71
72TEST(UsingInserterTest, ReusesExisting) {
73 EXPECT_EQ("#include \"foo.h\"\n"
74 "namespace {"
75 "using ::foo::func;\n"
76 "void f() { func(); }"
77 "}",
78 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
79 "namespace {"
80 "using ::foo::func;\n"
81 "void f() { f(); }"
82 "}",
83 1));
84}
85
86TEST(UsingInserterTest, ReusesExistingGlobal) {
87 EXPECT_EQ("#include \"foo.h\"\n"
88 "using ::foo::func;\n"
89 "namespace {"
90 "void f() { func(); }"
91 "}",
92 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
93 "using ::foo::func;\n"
94 "namespace {"
95 "void f() { f(); }"
96 "}",
97 1));
98}
99
100TEST(UsingInserterTest, AvoidsConflict) {
101 EXPECT_EQ("#include \"foo.h\"\n"
102 "namespace {"
103 "void f() { int func; ::foo::func(); }"
104 "}",
105 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
106 "namespace {"
107 "void f() { int func; f(); }"
108 "}",
109 1));
110}
111
112} // namespace utils
113} // namespace tidy
114} // namespace clang