summaryrefslogtreecommitdiffstats
path: root/clang/lib
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/CodeGen/CGDebugInfo.cpp42
-rw-r--r--clang/lib/CodeGen/CGDebugInfo.h11
-rw-r--r--clang/lib/CodeGen/CGDecl.cpp21
3 files changed, 60 insertions, 14 deletions
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 52bd805b81c..fe497f6b93f 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -831,15 +831,18 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
llvm::DIFile *Unit) {
+ TypedefNameDecl *TD = Ty->getDecl();
// We don't set size information, but do specify where the typedef was
// declared.
- SourceLocation Loc = Ty->getDecl()->getLocation();
+ SourceLocation Loc = TD->getLocation();
+
+ llvm::DIScope *TDContext = getDeclarationLexicalScope(*TD, QualType(Ty, 0));
// Typedefs are derived from some other type.
return DBuilder.createTypedef(
getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
- getDeclContextDescriptor(Ty->getDecl()));
+ TDContext);
}
llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
@@ -1472,6 +1475,23 @@ llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
return T;
}
+void CGDebugInfo::recordDeclarationLexicalScope(const Decl &D) {
+ assert(LexicalBlockMap.find(&D) == LexicalBlockMap.end() &&
+ "D is already mapped to lexical block scope");
+ if (!LexicalBlockStack.empty())
+ LexicalBlockMap[&D] = LexicalBlockStack.back();
+}
+
+llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope(const Decl &D,
+ QualType Ty) {
+ auto I = LexicalBlockMap.find(&D);
+ if (I != LexicalBlockMap.end()) {
+ RetainedTypes.push_back(Ty.getAsOpaquePtr());
+ return I->second;
+ }
+ return getDeclContextDescriptor(cast<Decl>(&D));
+}
+
void CGDebugInfo::completeType(const EnumDecl *ED) {
if (DebugKind <= codegenoptions::DebugLineTablesOnly)
return;
@@ -2068,7 +2088,8 @@ llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
// Cache the enum type so it is available when building the declcontext
// and replace the declcontect with the real thing.
TypeCache[Ty].reset(RetTy);
- TmpContext->replaceAllUsesWith(getDeclContextDescriptor(ED));
+ TmpContext->replaceAllUsesWith(
+ getDeclarationLexicalScope(*ED, QualType(Ty, 0)));
ReplaceMap.emplace_back(
std::piecewise_construct, std::make_tuple(Ty),
@@ -2103,7 +2124,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
unsigned Line = getLineNumber(ED->getLocation());
- llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
+ llvm::DIScope *EnumContext = getDeclarationLexicalScope(*ED, QualType(Ty, 0));
llvm::DIType *ClassTy =
ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr;
return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit,
@@ -2364,7 +2385,7 @@ llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
unsigned Line = getLineNumber(RD->getLocation());
StringRef RDName = getClassName(RD);
- llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
+ llvm::DIScope *RDContext = getDeclarationLexicalScope(*RD, QualType(Ty, 0));
// If we ended up creating the type during the context chain construction,
// just return that.
@@ -2511,8 +2532,15 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
if (DC->isRecord())
DC = CGM.getContext().getTranslationUnitDecl();
- llvm::DIScope *Mod = getParentModuleOrNull(VD);
- VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
+ if (VD->isStaticLocal()) {
+ // Get context for static locals (that are technically globals) the same way
+ // we do for "local" locals -- by using current lexical block.
+ assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
+ VDContext = LexicalBlockStack.back();
+ } else {
+ llvm::DIScope *Mod = getParentModuleOrNull(VD);
+ VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
+ }
}
llvm::DISubprogram *
diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index 989ecad9b46..795dc0ad42c 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -116,6 +116,11 @@ class CGDebugInfo {
/// Keep track of our current nested lexical block.
std::vector<llvm::TypedTrackingMDRef<llvm::DIScope>> LexicalBlockStack;
+
+ /// Map of AST declaration to its lexical block scope.
+ llvm::DenseMap<const Decl *, llvm::TypedTrackingMDRef<llvm::DIScope>>
+ LexicalBlockMap;
+
llvm::DenseMap<const Decl *, llvm::TrackingMDRef> RegionMap;
/// Keep track of LexicalBlockStack counter at the beginning of a
/// function. This is used to pop unbalanced regions at the end of a
@@ -378,6 +383,12 @@ public:
/// Emit an Objective-C interface type standalone debug info.
llvm::DIType *getOrCreateInterfaceType(QualType Ty, SourceLocation Loc);
+ /// Map AST declaration to its lexical block scope if available.
+ void recordDeclarationLexicalScope(const Decl &D);
+
+ /// Get lexical scope of AST declaration.
+ llvm::DIScope *getDeclarationLexicalScope(const Decl &D, QualType Ty);
+
/// Emit standalone debug info for a type.
llvm::DIType *getOrCreateStandaloneType(QualType Ty, SourceLocation Loc);
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 5b88ef1830f..809012b17a8 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -83,11 +83,7 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
case Decl::UsingShadow:
case Decl::ObjCTypeParam:
llvm_unreachable("Declaration should not be in declstmts!");
- case Decl::Function: // void X();
- case Decl::Record: // struct/union/class X;
- case Decl::Enum: // enum X;
- case Decl::EnumConstant: // enum ? { X = ? }
- case Decl::CXXRecord: // struct/union/class X; [C++]
+ case Decl::Function: // void X();
case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
case Decl::Label: // __label__ x;
case Decl::Import:
@@ -97,13 +93,21 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
// None of these decls require codegen support.
return;
+ case Decl::Record: // struct/union/class X;
+ case Decl::Enum: // enum X;
+ case Decl::EnumConstant: // enum ? { X = ? }
+ case Decl::CXXRecord: // struct/union/class X; [C++]
+ if (CGDebugInfo *DI = getDebugInfo())
+ DI->recordDeclarationLexicalScope(D);
+ return;
+
case Decl::NamespaceAlias:
if (CGDebugInfo *DI = getDebugInfo())
- DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
+ DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
return;
case Decl::Using: // using X; [C++]
if (CGDebugInfo *DI = getDebugInfo())
- DI->EmitUsingDecl(cast<UsingDecl>(D));
+ DI->EmitUsingDecl(cast<UsingDecl>(D));
return;
case Decl::UsingDirective: // using namespace X; [C++]
if (CGDebugInfo *DI = getDebugInfo())
@@ -121,6 +125,9 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
QualType Ty = TD.getUnderlyingType();
+ if (CGDebugInfo *DI = getDebugInfo())
+ DI->recordDeclarationLexicalScope(D);
+
if (Ty->isVariablyModifiedType())
EmitVariablyModifiedType(Ty);
}
OpenPOWER on IntegriCloud