aboutsummaryrefslogtreecommitdiff
path: root/unittests/clang-tidy/NamespaceAliaserTest.cpp
blob: 71286e2976173a93b0c32965788bf1cbfa853322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//===---- NamespaceAliaserTest.cpp - clang-tidy
//----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "../clang-tidy/utils/NamespaceAliaser.h"

#include "ClangTidyTest.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "gtest/gtest.h"

namespace clang {
namespace tidy {
namespace utils {
// This checker is for testing only. It can only run on one test case
// (e.g. with one SourceManager).
class InsertAliasCheck : public ClangTidyCheck {
public:
  InsertAliasCheck(StringRef Name, ClangTidyContext *Context)
      :ClangTidyCheck(Name, Context) {}
  void registerMatchers(ast_matchers::MatchFinder *Finder) override {
    Finder->addMatcher(ast_matchers::callExpr().bind("foo"), this);
  }
  void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
    if (!Aliaser)
      Aliaser.reset(new NamespaceAliaser(*Result.SourceManager));

    const auto *Call = Result.Nodes.getNodeAs<CallExpr>("foo");
    assert(Call != nullptr && "Did not find node \"foo\"");
    auto Hint = Aliaser->createAlias(*Result.Context, *Call, "::foo::bar",
                                     {"b", "some_alias"});
    if (Hint.hasValue())
      diag(Call->getLocStart(), "Fix for testing") << Hint.getValue();

    diag(Call->getLocStart(), "insert call")
        << FixItHint::CreateInsertion(
               Call->getLocStart(),
               Aliaser->getNamespaceName(*Result.Context, *Call, "::foo::bar") +
                   "::");
  }

private:
  std::unique_ptr<NamespaceAliaser> Aliaser;
};

template <typename Check>
std::string runChecker(StringRef Code, unsigned ExpectedWarningCount) {
  std::map<StringRef, StringRef> AdditionalFileContents = {{"foo.h",
                                                            "namespace foo {\n"
                                                            "namespace bar {\n"
                                                            "}\n"
                                                            "void func() { }\n"
                                                            "}"}};
  std::vector<ClangTidyError> errors;

  std::string result =
      test::runCheckOnCode<Check>(Code, &errors, "foo.cc", None,
                                  ClangTidyOptions(), AdditionalFileContents);

  EXPECT_EQ(ExpectedWarningCount, errors.size());
  return result;
}

TEST(NamespaceAliaserTest, AddNewAlias) {
  EXPECT_EQ("#include \"foo.h\"\n"
            "void f() {\n"
            "namespace b = ::foo::bar;"
            " b::f(); }",
            runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
                                         "void f() { f(); }",
                                         2));
}

TEST(NamespaceAliaserTest, ReuseAlias) {
  EXPECT_EQ(
      "#include \"foo.h\"\n"
      "void f() { namespace x = foo::bar; x::f(); }",
      runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
                                   "void f() { namespace x = foo::bar; f(); }",
                                   1));
}

TEST(NamespaceAliaserTest, AddsOnlyOneAlias) {
  EXPECT_EQ("#include \"foo.h\"\n"
            "void f() {\n"
            "namespace b = ::foo::bar;"
            " b::f(); b::f(); }",
            runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
                                         "void f() { f(); f(); }",
                                         3));
}

TEST(NamespaceAliaserTest, LocalConflict) {
  EXPECT_EQ("#include \"foo.h\"\n"
            "void f() {\n"
            "namespace some_alias = ::foo::bar;"
            " namespace b = foo; some_alias::f(); }",
            runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
                                         "void f() { namespace b = foo; f(); }",
                                         2));
}

TEST(NamespaceAliaserTest, GlobalConflict) {
  EXPECT_EQ("#include \"foo.h\"\n"
            "namespace b = foo;\n"
            "void f() {\n"
            "namespace some_alias = ::foo::bar;"
            " some_alias::f(); }",
            runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
                                         "namespace b = foo;\n"
                                         "void f() { f(); }",
                                         2));
}

} // namespace utils
} // namespace tidy
} // namespace clang