Add a way to get the context of any particular scope.
authorEric Christopher <echristo@gmail.com>
Fri, 26 Jul 2013 17:02:36 +0000 (17:02 +0000)
committerEric Christopher <echristo@gmail.com>
Fri, 26 Jul 2013 17:02:36 +0000 (17:02 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187212 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/DebugInfo.h
lib/IR/DebugInfo.cpp

index ae7bf049444df8f3ef3accb7e55842e7b05cf510..9fb12f8feb09a5b6fd4c78fe48cbeb483e3653ec 100644 (file)
@@ -187,6 +187,9 @@ namespace llvm {
   public:
     explicit DIScope(const MDNode *N = 0) : DIDescriptor (N) {}
 
+    /// Gets the parent scope for this scope node or returns a
+    /// default constructed scope.
+    DIScope getContext() const;
     StringRef getFilename() const;
     StringRef getDirectory() const;
   };
index 750231658f1edb4165cbe5f8446fd9e83dab4695..369895ce568035ca324d4e2c7bba7ec0229a4a44 100644 (file)
@@ -692,6 +692,31 @@ Value *DITemplateValueParameter::getValue() const {
   return getField(DbgNode, 4);
 }
 
+// If the current node has a parent scope then return that,
+// else return an empty scope.
+DIScope DIScope::getContext() const {
+
+  if (isType())
+    return DIType(DbgNode).getContext();
+
+  if (isSubprogram())
+    return DISubprogram(DbgNode).getContext();
+
+  if (isLexicalBlock())
+    return DILexicalBlock(DbgNode).getContext();
+
+  if (isLexicalBlockFile())
+    return DILexicalBlockFile(DbgNode).getContext();
+
+  if (isNameSpace())
+    return DINameSpace(DbgNode).getContext();
+
+  if (isFile() || isCompileUnit())
+    return DIScope();
+
+  return DIScope();
+}
+
 StringRef DIScope::getFilename() const {
   if (!DbgNode)
     return StringRef();