Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 1 | //===---- NamespaceAliaserTest.cpp - clang-tidy |
| 2 | //----------------------------===// |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "../clang-tidy/utils/NamespaceAliaser.h" |
| 12 | |
| 13 | #include "ClangTidyTest.h" |
| 14 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 15 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace tidy { |
| 20 | namespace utils { |
| 21 | // This checker is for testing only. It can only run on one test case |
| 22 | // (e.g. with one SourceManager). |
| 23 | class InsertAliasCheck : public ClangTidyCheck { |
| 24 | public: |
Haojian Wu | bd31488 | 2016-10-17 10:05:25 +0000 | [diff] [blame] | 25 | InsertAliasCheck(StringRef Name, ClangTidyContext *Context) |
| 26 | :ClangTidyCheck(Name, Context) {} |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 27 | void registerMatchers(ast_matchers::MatchFinder *Finder) override { |
| 28 | Finder->addMatcher(ast_matchers::callExpr().bind("foo"), this); |
| 29 | } |
Haojian Wu | bd31488 | 2016-10-17 10:05:25 +0000 | [diff] [blame] | 30 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override { |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 31 | if (!Aliaser) |
| 32 | Aliaser.reset(new NamespaceAliaser(*Result.SourceManager)); |
| 33 | |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 34 | const auto *Call = Result.Nodes.getNodeAs<CallExpr>("foo"); |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 35 | assert(Call != nullptr && "Did not find node \"foo\""); |
| 36 | auto Hint = Aliaser->createAlias(*Result.Context, *Call, "::foo::bar", |
| 37 | {"b", "some_alias"}); |
| 38 | if (Hint.hasValue()) |
| 39 | diag(Call->getLocStart(), "Fix for testing") << Hint.getValue(); |
| 40 | |
| 41 | diag(Call->getLocStart(), "insert call") |
| 42 | << FixItHint::CreateInsertion( |
| 43 | Call->getLocStart(), |
| 44 | Aliaser->getNamespaceName(*Result.Context, *Call, "::foo::bar") + |
| 45 | "::"); |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | std::unique_ptr<NamespaceAliaser> Aliaser; |
| 50 | }; |
| 51 | |
| 52 | template <typename Check> |
Simon Pilgrim | 6b6291a | 2016-10-18 13:15:31 +0000 | [diff] [blame] | 53 | std::string runChecker(StringRef Code, unsigned ExpectedWarningCount) { |
Haojian Wu | ada2862 | 2016-10-17 08:33:59 +0000 | [diff] [blame] | 54 | std::map<StringRef, StringRef> AdditionalFileContents = {{"foo.h", |
| 55 | "namespace foo {\n" |
| 56 | "namespace bar {\n" |
| 57 | "}\n" |
| 58 | "void func() { }\n" |
| 59 | "}"}}; |
| 60 | std::vector<ClangTidyError> errors; |
| 61 | |
| 62 | std::string result = |
| 63 | test::runCheckOnCode<Check>(Code, &errors, "foo.cc", None, |
| 64 | ClangTidyOptions(), AdditionalFileContents); |
| 65 | |
| 66 | EXPECT_EQ(ExpectedWarningCount, errors.size()); |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | TEST(NamespaceAliaserTest, AddNewAlias) { |
| 71 | EXPECT_EQ("#include \"foo.h\"\n" |
| 72 | "void f() {\n" |
| 73 | "namespace b = ::foo::bar;" |
| 74 | " b::f(); }", |
| 75 | runChecker<InsertAliasCheck>("#include \"foo.h\"\n" |
| 76 | "void f() { f(); }", |
| 77 | 2)); |
| 78 | } |
| 79 | |
| 80 | TEST(NamespaceAliaserTest, ReuseAlias) { |
| 81 | EXPECT_EQ( |
| 82 | "#include \"foo.h\"\n" |
| 83 | "void f() { namespace x = foo::bar; x::f(); }", |
| 84 | runChecker<InsertAliasCheck>("#include \"foo.h\"\n" |
| 85 | "void f() { namespace x = foo::bar; f(); }", |
| 86 | 1)); |
| 87 | } |
| 88 | |
| 89 | TEST(NamespaceAliaserTest, AddsOnlyOneAlias) { |
| 90 | EXPECT_EQ("#include \"foo.h\"\n" |
| 91 | "void f() {\n" |
| 92 | "namespace b = ::foo::bar;" |
| 93 | " b::f(); b::f(); }", |
| 94 | runChecker<InsertAliasCheck>("#include \"foo.h\"\n" |
| 95 | "void f() { f(); f(); }", |
| 96 | 3)); |
| 97 | } |
| 98 | |
| 99 | TEST(NamespaceAliaserTest, LocalConflict) { |
| 100 | EXPECT_EQ("#include \"foo.h\"\n" |
| 101 | "void f() {\n" |
| 102 | "namespace some_alias = ::foo::bar;" |
| 103 | " namespace b = foo; some_alias::f(); }", |
| 104 | runChecker<InsertAliasCheck>("#include \"foo.h\"\n" |
| 105 | "void f() { namespace b = foo; f(); }", |
| 106 | 2)); |
| 107 | } |
| 108 | |
| 109 | TEST(NamespaceAliaserTest, GlobalConflict) { |
| 110 | EXPECT_EQ("#include \"foo.h\"\n" |
| 111 | "namespace b = foo;\n" |
| 112 | "void f() {\n" |
| 113 | "namespace some_alias = ::foo::bar;" |
| 114 | " some_alias::f(); }", |
| 115 | runChecker<InsertAliasCheck>("#include \"foo.h\"\n" |
| 116 | "namespace b = foo;\n" |
| 117 | "void f() { f(); }", |
| 118 | 2)); |
| 119 | } |
| 120 | |
| 121 | } // namespace utils |
| 122 | } // namespace tidy |
| 123 | } // namespace clang |