From c8680f46970a5a53d07e05edd93657e64764da3c Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Wed, 28 Sep 2011 02:45:33 +0000 Subject: [PATCH] Introduce Decl::getParentFunctionOrMethod which if the decl is defined inside a function/method/block it returns the corresponding DeclContext, otherwise it returns null. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140672 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclBase.h | 8 +++++++- lib/AST/DeclBase.cpp | 10 +++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 306a4031d5..2a2f810044 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -567,7 +567,13 @@ public: /// scoped decl is defined outside the current function or method. This is /// roughly global variables and functions, but also handles enums (which /// could be defined inside or outside a function etc). - bool isDefinedOutsideFunctionOrMethod() const; + bool isDefinedOutsideFunctionOrMethod() const { + return getParentFunctionOrMethod() == 0; + } + + /// \brief If this decl is defined inside a function/method/block it returns + /// the corresponding DeclContext, otherwise it returns null. + const DeclContext *getParentFunctionOrMethod() const; /// \brief Retrieves the "canonical" declaration of the given declaration. virtual Decl *getCanonicalDecl() { return this; } diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index 60fb7025ab..d0afcbc45b 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -137,14 +137,14 @@ bool Decl::isTemplateDecl() const { return isa(this); } -bool Decl::isDefinedOutsideFunctionOrMethod() const { - for (const DeclContext *DC = getDeclContext(); - DC && !DC->isTranslationUnit(); +const DeclContext *Decl::getParentFunctionOrMethod() const { + for (const DeclContext *DC = getDeclContext(); + DC && !DC->isTranslationUnit() && !DC->isNamespace(); DC = DC->getParent()) if (DC->isFunctionOrMethod()) - return false; + return DC; - return true; + return 0; } -- 2.40.0