summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/unittests/clang-tidy/NamespaceAliaserTest.cpp
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2016-10-12 07:59:54 +0000
committerHaojian Wu <hokein@google.com>2016-10-12 07:59:54 +0000
commit6c24d9345d134990cb134d14fdd770cc47ca7c04 (patch)
tree54363f7a96b743f4a524d2c5a557fbf4e85dcc63 /clang-tools-extra/unittests/clang-tidy/NamespaceAliaserTest.cpp
parenta9e659619f592c49b352af16253ec0b79c207956 (diff)
downloadbcm5719-llvm-6c24d9345d134990cb134d14fdd770cc47ca7c04.tar.gz
bcm5719-llvm-6c24d9345d134990cb134d14fdd770cc47ca7c04.zip
[ClangTidy] Add UsingInserter and NamespaceAliaser
Summary: This adds helper classes to add using declaractions and namespace aliases to function bodies. These help making function calls to deeply nested functions concise (e.g. when calling helpers in a refactoring) Patch by Julian Bangert! Reviewers: alexfh, hokein Subscribers: cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D24997 llvm-svn: 283981
Diffstat (limited to 'clang-tools-extra/unittests/clang-tidy/NamespaceAliaserTest.cpp')
-rw-r--r--clang-tools-extra/unittests/clang-tidy/NamespaceAliaserTest.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/clang-tools-extra/unittests/clang-tidy/NamespaceAliaserTest.cpp b/clang-tools-extra/unittests/clang-tidy/NamespaceAliaserTest.cpp
new file mode 100644
index 00000000000..9102298a9b3
--- /dev/null
+++ b/clang-tools-extra/unittests/clang-tidy/NamespaceAliaserTest.cpp
@@ -0,0 +1,124 @@
+//===---- 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:
+ using ClangTidyCheck::ClangTidyCheck;
+ 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 CallExpr *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, int 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
OpenPOWER on IntegriCloud