Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 1 | //===---- UsingInserterTest.cpp - clang-tidy ----------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 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: |
Haojian Wu | bd31488 | 2016-10-17 10:05:25 +0000 | [diff] [blame] | 25 | InsertUsingCheck(StringRef Name, ClangTidyContext *Context) |
| 26 | :ClangTidyCheck(Name, Context) {} |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 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 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 35 | const auto *Call = Result.Nodes.getNodeAs<clang::CallExpr>("foo"); |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 36 | assert(Call != nullptr && "Did not find node \"foo\""); |
| 37 | auto Hint = |
| 38 | Inserter->createUsingDeclaration(*Result.Context, *Call, "::foo::func"); |
| 39 | |
| 40 | if (Hint.hasValue()) |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 41 | diag(Call->getBeginLoc(), "Fix for testing") << Hint.getValue(); |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 42 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 43 | diag(Call->getBeginLoc(), "insert call") |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 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> |
Simon Pilgrim | 6b6291a | 2016-10-18 13:15:31 +0000 | [diff] [blame] | 54 | std::string runChecker(StringRef Code, unsigned ExpectedWarningCount) { |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 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 = |
| 64 | test::runCheckOnCode<Check>(Code, &errors, "foo.cc", None, |
| 65 | ClangTidyOptions(), AdditionalFileContents); |
| 66 | |
| 67 | EXPECT_EQ(ExpectedWarningCount, errors.size()); |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | TEST(UsingInserterTest, ReusesExisting) { |
| 72 | EXPECT_EQ("#include \"foo.h\"\n" |
| 73 | "namespace {" |
| 74 | "using ::foo::func;\n" |
| 75 | "void f() { func(); }" |
| 76 | "}", |
| 77 | runChecker<InsertUsingCheck>("#include \"foo.h\"\n" |
| 78 | "namespace {" |
| 79 | "using ::foo::func;\n" |
| 80 | "void f() { f(); }" |
| 81 | "}", |
| 82 | 1)); |
| 83 | } |
| 84 | |
| 85 | TEST(UsingInserterTest, ReusesExistingGlobal) { |
| 86 | EXPECT_EQ("#include \"foo.h\"\n" |
| 87 | "using ::foo::func;\n" |
| 88 | "namespace {" |
| 89 | "void f() { func(); }" |
| 90 | "}", |
| 91 | runChecker<InsertUsingCheck>("#include \"foo.h\"\n" |
| 92 | "using ::foo::func;\n" |
| 93 | "namespace {" |
| 94 | "void f() { f(); }" |
| 95 | "}", |
| 96 | 1)); |
| 97 | } |
| 98 | |
| 99 | TEST(UsingInserterTest, AvoidsConflict) { |
| 100 | EXPECT_EQ("#include \"foo.h\"\n" |
| 101 | "namespace {" |
| 102 | "void f() { int func; ::foo::func(); }" |
| 103 | "}", |
| 104 | runChecker<InsertUsingCheck>("#include \"foo.h\"\n" |
| 105 | "namespace {" |
| 106 | "void f() { int func; f(); }" |
| 107 | "}", |
| 108 | 1)); |
| 109 | } |
| 110 | |
| 111 | } // namespace utils |
| 112 | } // namespace tidy |
| 113 | } // namespace clang |