summaryrefslogtreecommitdiff
path: root/clang-tools-extra/change-namespace
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2016-11-29 14:15:14 +0000
committerEric Liu <ioeric@google.com>2016-11-29 14:15:14 +0000
commit91bf3d097b3ebafecacf7c71b59805b658c3d96c (patch)
tree97b0055067bedd85db29e22cdb42b6e312445a75 /clang-tools-extra/change-namespace
parentb54300ac09ee174621b5c402318588bd57999aca (diff)
[change-namespace] fix non-calling function references.
Reviewers: hokein Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D27208
Diffstat (limited to 'clang-tools-extra/change-namespace')
-rw-r--r--clang-tools-extra/change-namespace/ChangeNamespace.cpp47
-rw-r--r--clang-tools-extra/change-namespace/ChangeNamespace.h4
2 files changed, 36 insertions, 15 deletions
diff --git a/clang-tools-extra/change-namespace/ChangeNamespace.cpp b/clang-tools-extra/change-namespace/ChangeNamespace.cpp
index fbbde4397cc..44eec1a4dba 100644
--- a/clang-tools-extra/change-namespace/ChangeNamespace.cpp
+++ b/clang-tools-extra/change-namespace/ChangeNamespace.cpp
@@ -369,11 +369,13 @@ void ChangeNamespaceTool::registerMatchers(ast_matchers::MatchFinder *Finder) {
hasAncestor(namespaceDecl(isAnonymous())),
hasAncestor(cxxRecordDecl()))),
hasParent(namespaceDecl()));
- Finder->addMatcher(
- decl(forEachDescendant(callExpr(callee(FuncMatcher)).bind("call")),
- IsInMovedNs, unless(isImplicit()))
- .bind("dc"),
- this);
+ Finder->addMatcher(decl(forEachDescendant(expr(anyOf(
+ callExpr(callee(FuncMatcher)).bind("call"),
+ declRefExpr(to(FuncMatcher.bind("func_decl")))
+ .bind("func_ref")))),
+ IsInMovedNs, unless(isImplicit()))
+ .bind("dc"),
+ this);
auto GlobalVarMatcher = varDecl(
hasGlobalStorage(), hasParent(namespaceDecl()),
@@ -421,26 +423,32 @@ void ChangeNamespaceTool::run(
assert(Var);
if (Var->getCanonicalDecl()->isStaticDataMember())
return;
- const clang::Decl *Context = Result.Nodes.getNodeAs<clang::Decl>("dc");
+ const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
assert(Context && "Empty decl context.");
- clang::SourceRange VarRefRange = VarRef->getSourceRange();
- replaceQualifiedSymbolInDeclContext(
- Result, Context->getDeclContext(), VarRefRange.getBegin(),
- VarRefRange.getEnd(), llvm::cast<NamedDecl>(Var));
+ fixDeclRefExpr(Result, Context->getDeclContext(),
+ llvm::cast<NamedDecl>(Var), VarRef);
+ } else if (const auto *FuncRef =
+ Result.Nodes.getNodeAs<DeclRefExpr>("func_ref")) {
+ const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");
+ assert(Func);
+ const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
+ assert(Context && "Empty decl context.");
+ fixDeclRefExpr(Result, Context->getDeclContext(),
+ llvm::cast<NamedDecl>(Func), FuncRef);
} else {
- const auto *Call = Result.Nodes.getNodeAs<clang::CallExpr>("call");
+ const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
assert(Call != nullptr && "Expecting callback for CallExpr.");
- const clang::FunctionDecl *Func = Call->getDirectCallee();
+ const FunctionDecl *Func = Call->getDirectCallee();
assert(Func != nullptr);
// Ignore out-of-line static methods since they will be handled by nested
// name specifiers.
if (Func->getCanonicalDecl()->getStorageClass() ==
- clang::StorageClass::SC_Static &&
+ StorageClass::SC_Static &&
Func->isOutOfLine())
return;
- const clang::Decl *Context = Result.Nodes.getNodeAs<clang::Decl>("dc");
+ const auto *Context = Result.Nodes.getNodeAs<Decl>("dc");
assert(Context && "Empty decl context.");
- clang::SourceRange CalleeRange = Call->getCallee()->getSourceRange();
+ SourceRange CalleeRange = Call->getCallee()->getSourceRange();
replaceQualifiedSymbolInDeclContext(
Result, Context->getDeclContext(), CalleeRange.getBegin(),
CalleeRange.getEnd(), llvm::cast<NamedDecl>(Func));
@@ -698,6 +706,15 @@ void ChangeNamespaceTool::fixUsingShadowDecl(
llvm_unreachable(llvm::toString(std::move(Err)).c_str());
}
+void ChangeNamespaceTool::fixDeclRefExpr(
+ const ast_matchers::MatchFinder::MatchResult &Result,
+ const DeclContext *UseContext, const NamedDecl *From,
+ const DeclRefExpr *Ref) {
+ SourceRange RefRange = Ref->getSourceRange();
+ replaceQualifiedSymbolInDeclContext(Result, UseContext, RefRange.getBegin(),
+ RefRange.getEnd(), From);
+}
+
void ChangeNamespaceTool::onEndOfTranslationUnit() {
// Move namespace blocks and insert forward declaration to old namespace.
for (const auto &FileAndNsMoves : MoveNamespaces) {
diff --git a/clang-tools-extra/change-namespace/ChangeNamespace.h b/clang-tools-extra/change-namespace/ChangeNamespace.h
index 4073f6fe39c..e7521cb8760 100644
--- a/clang-tools-extra/change-namespace/ChangeNamespace.h
+++ b/clang-tools-extra/change-namespace/ChangeNamespace.h
@@ -76,6 +76,10 @@ private:
void fixUsingShadowDecl(const ast_matchers::MatchFinder::MatchResult &Result,
const UsingDecl *UsingDeclaration);
+ void fixDeclRefExpr(const ast_matchers::MatchFinder::MatchResult &Result,
+ const DeclContext *UseContext, const NamedDecl *From,
+ const DeclRefExpr *Ref);
+
// Information about moving an old namespace.
struct MoveNamespace {
// The start offset of the namespace block being moved in the original