]> granicus.if.org Git - clang/commitdiff
Introduce Decl::getParentFunctionOrMethod which if the decl is defined inside
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 28 Sep 2011 02:45:33 +0000 (02:45 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 28 Sep 2011 02:45:33 +0000 (02:45 +0000)
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
lib/AST/DeclBase.cpp

index 306a4031d5dc8afed82cdbe7f4a42ab03bf5f49b..2a2f8100442527b9221552bdf5e6fd7f8e37c11b 100644 (file)
@@ -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; }
index 60fb7025abeda1508403f526a7b5020ae78c60a7..d0afcbc45b56da26041b2b386c0ae237db782846 100644 (file)
@@ -137,14 +137,14 @@ bool Decl::isTemplateDecl() const {
   return isa<TemplateDecl>(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;
 }