Author: hokein
Date: Wed Oct 12 02:59:54 2016
New Revision: 283981
URL: http://llvm.org/viewvc/llvm-project?rev=283981=rev
Log:
[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
Added:
clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.cpp
clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.h
clang-tools-extra/trunk/clang-tidy/utils/NamespaceAliaser.cpp
clang-tools-extra/trunk/clang-tidy/utils/NamespaceAliaser.h
clang-tools-extra/trunk/clang-tidy/utils/UsingInserter.cpp
clang-tools-extra/trunk/clang-tidy/utils/UsingInserter.h
clang-tools-extra/trunk/unittests/clang-tidy/NamespaceAliaserTest.cpp
clang-tools-extra/trunk/unittests/clang-tidy/UsingInserterTest.cpp
Added: clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.cpp
URL:
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.cpp?rev=283981=auto
==
--- clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.cpp Wed Oct 12 02:59:54
2016
@@ -0,0 +1,28 @@
+//===-- ASTUtils.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 "ASTUtils.h"
+
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+using namespace ast_matchers;
+
+const FunctionDecl *getSurroundingFunction(ASTContext ,
+ const Stmt ) {
+ return selectFirst(
+ "function", match(stmt(hasAncestor(functionDecl().bind("function"))),
+Statement, Context));
+}
+} // namespace utils
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.h
URL:
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.h?rev=283981=auto
==
--- clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.h (added)
+++ clang-tools-extra/trunk/clang-tidy/utils/ASTUtils.h Wed Oct 12 02:59:54 2016
@@ -0,0 +1,25 @@
+//===-- ASTUtils.h - clang-tidy ===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ASTUTILS_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ASTUTILS_H
+
+#include "clang/AST/AST.h"
+
+namespace clang {
+namespace tidy {
+namespace utils {
+// Returns the (closest) Function declaration surrounding |Statement| or NULL.
+const FunctionDecl *getSurroundingFunction(ASTContext ,
+ const Stmt );
+} // namespace utils
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ASTUTILS_H
Added: clang-tools-extra/trunk/clang-tidy/utils/NamespaceAliaser.cpp
URL:
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/NamespaceAliaser.cpp?rev=283981=auto
==
--- clang-tools-extra/trunk/clang-tidy/utils/NamespaceAliaser.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/utils/NamespaceAliaser.cpp Wed Oct 12
02:59:54 2016
@@ -0,0 +1,99 @@
+//===-- NamespaceAliaser.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 "NamespaceAliaser.h"
+
+#include "ASTUtils.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Lex/Lexer.h"
+namespace clang {
+namespace tidy {
+namespace utils {
+
+using namespace ast_matchers;
+
+NamespaceAliaser::NamespaceAliaser(const SourceManager )
+: SourceMgr(SourceMgr) {}
+
+AST_MATCHER_P(NamespaceAliasDecl, hasTargetNamespace,
+ ast_matchers::internal::Matcher, innerMatcher) {
+ return innerMatcher.matches(*Node.getNamespace(), Finder,