xref: /llvm-project/clang-tools-extra/unittests/clang-tidy/UsingInserterTest.cpp (revision 43465bf3fd6cca715187ee7286c881cb210fc3c4)
1 //===---- 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 
17 namespace clang {
18 namespace tidy {
19 namespace 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).
24 class InsertUsingCheck : public clang::tidy::ClangTidyCheck {
25 public:
26   InsertUsingCheck(StringRef Name, ClangTidyContext *Context)
27       :ClangTidyCheck(Name, Context) {}
28   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 auto *Call = 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->getBeginLoc(), "Fix for testing") << Hint.getValue();
43 
44     diag(Call->getBeginLoc(), "insert call")
45         << clang::FixItHint::CreateReplacement(
46                Call->getCallee()->getSourceRange(),
47                Inserter->getShortName(*Result.Context, *Call, "::foo::func"));
48   }
49 
50 private:
51   std::unique_ptr<UsingInserter> Inserter;
52 };
53 
54 template <typename Check>
55 std::string runChecker(StringRef Code, unsigned 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 
72 TEST(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 
86 TEST(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 
100 TEST(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
115