Debian Codegraph @

clang-tools-extra/unittests/clang-tidy/UsingInserterTest.cpp

raw
1 //===---- UsingInserterTest.cpp - clang-tidy ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "../clang-tidy/utils/UsingInserter.h"
10
11 #include "ClangTidyTest.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "gtest/gtest.h"
15
16 namespace clang {
17 namespace tidy {
18 namespace utils {
19
20 // Replace all function calls with calls to foo::func. Inserts using
21 // declarations as necessary. This checker is for testing only. It
22 // can only run on one test case (e.g. wih one SourceManager).
23 class InsertUsingCheck : public clang::tidy::ClangTidyCheck {
24 public:
25 InsertUsingCheck(StringRef Name, ClangTidyContext *Context)
26 :ClangTidyCheck(Name, Context) {}
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 auto *Call = Result.Nodes.getNodeAs<clang::CallExpr>("foo");
36 assert(Call != nullptr && "Did not find node \"foo\"");
37 auto Hint =
38 Inserter->createUsingDeclaration(*Result.Context, *Call, "::foo::func");
39
40 if (Hint)
41 diag(Call->getBeginLoc(), "Fix for testing") << *Hint;
42
43 diag(Call->getBeginLoc(), "insert call")
44 << clang::FixItHint::CreateReplacement(
45 Call->getCallee()->getSourceRange(),
46 Inserter->getShortName(*Result.Context, *Call, "::foo::func"));
47 }
48
49 private:
50 std::unique_ptr<UsingInserter> Inserter;
51 };
52
53 template <typename Check>
54 std::string runChecker(StringRef Code, unsigned ExpectedWarningCount) {
55 std::map<StringRef, StringRef> AdditionalFileContents = {{"foo.h",
56 "namespace foo {\n"
57 "namespace bar {\n"
58 "}\n"
59 "void func() { }\n"
60 "}"}};
61 std::vector<ClangTidyError> errors;
62
63 std::string result = test::runCheckOnCode<Check>(
64 Code, &errors, "foo.cc", {}, ClangTidyOptions(), AdditionalFileContents);
65
66 EXPECT_EQ(ExpectedWarningCount, errors.size());
67 return result;
68 }
69
70 TEST(UsingInserterTest, ReusesExisting) {
71 EXPECT_EQ("#include \"foo.h\"\n"
72 "namespace {"
73 "using ::foo::func;\n"
74 "void f() { func(); }"
75 "}",
76 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
77 "namespace {"
78 "using ::foo::func;\n"
79 "void f() { f(); }"
80 "}",
81 1));
82 }
83
84 TEST(UsingInserterTest, ReusesExistingGlobal) {
85 EXPECT_EQ("#include \"foo.h\"\n"
86 "using ::foo::func;\n"
87 "namespace {"
88 "void f() { func(); }"
89 "}",
90 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
91 "using ::foo::func;\n"
92 "namespace {"
93 "void f() { f(); }"
94 "}",
95 1));
96 }
97
98 TEST(UsingInserterTest, AvoidsConflict) {
99 EXPECT_EQ("#include \"foo.h\"\n"
100 "namespace {"
101 "void f() { int func; ::foo::func(); }"
102 "}",
103 runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
104 "namespace {"
105 "void f() { int func; f(); }"
106 "}",
107 1));
108 }
109
110 } // namespace utils
111 } // namespace tidy
112 } // namespace clang