From c929f7ad423e9566dc9a06815d0f3f7f3d809db8 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Tue, 6 Feb 2018 22:17:45 +0000 Subject: Fix a crash when emitting DIEs for variable-length arrays VLAs may refer to a previous DIE to express the DW_AT_count of their type. Clang generates an artificial "vla_expr" variable for this. If this DIE hasn't been created yet LLVM asserts. This patch fixes this by sorting the local variables so that dependencies come before they are needed. It also replaces the linear scan in DWARFFile with a std::map, which can be faster. Differential Revision: https://reviews.llvm.org/D42940 llvm-svn: 324412 --- llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp | 30 +++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp') diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp index 3cde5ad38ce..85a63248a21 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -568,13 +568,41 @@ DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, return Var; } +/// Determine whether a variable appears in a count: expression. +static bool dependsOn(DbgVariable *A, DbgVariable *B) { + auto *Array = dyn_cast(A->getType()); + if (!Array || Array->getTag() != dwarf::DW_TAG_array_type) + return false; + return llvm::any_of(Array->getElements(), [&](DINode *El) { + if (auto *Subrange = dyn_cast(El)) { + auto Count = Subrange->getCount(); + if (auto *Var = Count.dyn_cast()) + return Var == B->getVariable(); + } + return false; + }); +} + +/// Sort local variables so that variables appearing inside of helper +/// expressions come first. +static bool sortLocalVars(DbgVariable *A, DbgVariable *B) { + return dependsOn(B, A); +} + DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, SmallVectorImpl &Children, bool *HasNonScopeChildren) { assert(Children.empty()); DIE *ObjectPointer = nullptr; - for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope)) + // Emit function arguments (order is significant). + auto Vars = DU->getScopeVariables().lookup(Scope); + for (auto &DV : Vars.Args) + Children.push_back(constructVariableDIE(*DV.second, *Scope, ObjectPointer)); + + // Emit local variables. + std::stable_sort(Vars.Locals.begin(), Vars.Locals.end(), sortLocalVars); + for (DbgVariable *DV : Vars.Locals) Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); // Skip imported directives in gmlt-like data. -- cgit v1.2.3