blob: 16d251924467f7fdc65ccce87d7ebaebe1ebe003 [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
Piotr Padlewski08124b12016-12-14 15:29:23 +000036 const auto *Call = Result.Nodes.getNodeAs<clang::CallExpr>("foo");
Haojian Wuada28622016-10-17 08:33:59 +000037 assert(Call != nullptr && "Did not find node \"foo\"");
38 auto Hint =
39 Inserter->createUsingDeclaration(*Result.Context, *Call, "::foo::func");
40
41 if (Hint.hasValue())
Stephen Kelly43465bf2018-08-09 22:42:26 +000042 diag(Call->getBeginLoc(), "Fix for testing") << Hint.getValue();
Haojian Wuada28622016-10-17 08:33:59 +000043
Stephen Kelly43465bf2018-08-09 22:42:26 +000044 diag(Call->getBeginLoc(), "insert call")
Haojian Wuada28622016-10-17 08:33:59 +000045 << 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>
Simon Pilgrim6b6291a2016-10-18 13:15:31 +000055std::string runChecker(StringRef Code, unsigned ExpectedWarningCount) {
Haojian Wuada28622016-10-17 08:33:59 +000056 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