blob: 71286e2976173a93b0c32965788bf1cbfa853322 [file] [log] [blame]
Haojian Wuada28622016-10-17 08:33:59 +00001//===---- 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
18namespace clang {
19namespace tidy {
20namespace utils {
21// This checker is for testing only. It can only run on one test case
22// (e.g. with one SourceManager).
23class InsertAliasCheck : public ClangTidyCheck {
24public:
Haojian Wubd314882016-10-17 10:05:25 +000025 InsertAliasCheck(StringRef Name, ClangTidyContext *Context)
26 :ClangTidyCheck(Name, Context) {}
Haojian Wuada28622016-10-17 08:33:59 +000027 void registerMatchers(ast_matchers::MatchFinder *Finder) override {
28 Finder->addMatcher(ast_matchers::callExpr().bind("foo"), this);
29 }
Haojian Wubd314882016-10-17 10:05:25 +000030 void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
Haojian Wuada28622016-10-17 08:33:59 +000031 if (!Aliaser)
32 Aliaser.reset(new NamespaceAliaser(*Result.SourceManager));
33
Piotr Padlewski08124b12016-12-14 15:29:23 +000034 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("foo");
Haojian Wuada28622016-10-17 08:33:59 +000035 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
48private:
49 std::unique_ptr<NamespaceAliaser> Aliaser;
50};
51
52template <typename Check>
Simon Pilgrim6b6291a2016-10-18 13:15:31 +000053std::string runChecker(StringRef Code, unsigned ExpectedWarningCount) {
Haojian Wuada28622016-10-17 08:33:59 +000054 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
70TEST(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
80TEST(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
89TEST(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
99TEST(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
109TEST(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