blob: f10f708055ad7eab9ee4bc595d8fb15b8c0d180a [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:
Haojian Wubd314882016-10-17 10:05:25 +000026 InsertUsingCheck(StringRef Name, ClangTidyContext *Context)
27 :ClangTidyCheck(Name, Context) {}
Haojian Wuada28622016-10-17 08:33:59 +000028 void registerMatchers(clang::ast_matchers::MatchFinder *Finder) override {
29 Finder->addMatcher(clang::ast_matchers::callExpr().bind("foo"), this);
30 }
31 void
32 check(const clang::ast_matchers::MatchFinder::MatchResult &Result) override {
33 if (!Inserter)
34 Inserter.reset(new UsingInserter(*Result.SourceManager));
35
36 const clang::CallExpr *Call =
37 Result.Nodes.getNodeAs<clang::CallExpr>("foo");
38 assert(Call != nullptr && "Did not find node \"foo\"");
39 auto Hint =
40 Inserter->createUsingDeclaration(*Result.Context, *Call, "::foo::func");
41
42 if (Hint.hasValue())
43 diag(Call->getLocStart(), "Fix for testing") << Hint.getValue();
44
45 diag(Call->getLocStart(), "insert call")
46 << clang::FixItHint::CreateReplacement(
47 Call->getCallee()->getSourceRange(),
48 Inserter->getShortName(*Result.Context, *Call, "::foo::func"));
49 }
50
51private:
52 std::unique_ptr<UsingInserter> Inserter;
53};
54
55template <typename Check>
56std::string runChecker(StringRef Code, int ExpectedWarningCount) {
57 std::map<StringRef, StringRef> AdditionalFileContents = {{"foo.h",
58 "namespace foo {\n"
59 "namespace bar {\n"
60 "}\n"
61 "void func() { }\n"
62 "}"}};
63 std::vector<ClangTidyError> errors;
64
65 std::string result =
66 test::runCheckOnCode<Check>(Code, &errors, "foo.cc", None,
67 ClangTidyOptions(), AdditionalFileContents);
68
69 EXPECT_EQ(ExpectedWarningCount, errors.size());
70 return result;
71}
72
73TEST(UsingInserterTest, ReusesExisting) {
74 EXPECT_EQ("#include \"foo.h\"\n"
75 "namespace {"
76 "using ::foo::func;\n"
77 "void f() { func(); }"
78 "}",
79 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
80 "namespace {"
81 "using ::foo::func;\n"
82 "void f() { f(); }"
83 "}",
84 1));
85}
86
87TEST(UsingInserterTest, ReusesExistingGlobal) {
88 EXPECT_EQ("#include \"foo.h\"\n"
89 "using ::foo::func;\n"
90 "namespace {"
91 "void f() { func(); }"
92 "}",
93 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
94 "using ::foo::func;\n"
95 "namespace {"
96 "void f() { f(); }"
97 "}",
98 1));
99}
100
101TEST(UsingInserterTest, AvoidsConflict) {
102 EXPECT_EQ("#include \"foo.h\"\n"
103 "namespace {"
104 "void f() { int func; ::foo::func(); }"
105 "}",
106 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
107 "namespace {"
108 "void f() { int func; f(); }"
109 "}",
110 1));
111}
112
113} // namespace utils
114} // namespace tidy
115} // namespace clang