diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 8d57c87e97e39..7ad3088f0ab75 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -21,6 +21,7 @@ #include "TargetInfo.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Attr.h" +#include "clang/AST/DeclCXX.h" #include "clang/AST/DeclFriend.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" @@ -79,6 +80,35 @@ static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) { return D->hasAttr() ? D->getMaxAlignment() : 0; } +/// Returns true if \ref VD is a a holding variable (aka a +/// VarDecl retrieved using \ref BindingDecl::getHoldingVar). +static bool IsDecomposedVarDecl(VarDecl const *VD) { + auto const *Init = VD->getInit(); + if (!Init) + return false; + + auto const *RefExpr = + llvm::dyn_cast_or_null(Init->IgnoreUnlessSpelledInSource()); + if (!RefExpr) + return false; + + return llvm::dyn_cast_or_null(RefExpr->getDecl()); +} + +/// Returns true if \ref VD is a compiler-generated variable +/// and should be treated as artificial for the purposes +/// of debug-info generation. +static bool IsArtificial(VarDecl const *VD) { + // Tuple-like bindings are marked as implicit despite + // being spelled out in source. Don't treat them as artificial + // variables. + if (IsDecomposedVarDecl(VD)) + return false; + + return VD->isImplicit() || (isa(VD->getDeclContext()) && + cast(VD->getDeclContext())->isImplicit()); +} + CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()), DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs), @@ -4766,11 +4796,10 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, if (VD->hasAttr()) return nullptr; - bool Unwritten = - VD->isImplicit() || (isa(VD->getDeclContext()) && - cast(VD->getDeclContext())->isImplicit()); + const bool VarIsArtificial = IsArtificial(VD); + llvm::DIFile *Unit = nullptr; - if (!Unwritten) + if (!VarIsArtificial) Unit = getOrCreateFile(VD->getLocation()); llvm::DIType *Ty; uint64_t XOffset = 0; @@ -4787,13 +4816,13 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, // Get location information. unsigned Line = 0; unsigned Column = 0; - if (!Unwritten) { + if (!VarIsArtificial) { Line = getLineNumber(VD->getLocation()); Column = getColumnNumber(VD->getLocation()); } SmallVector Expr; llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; - if (VD->isImplicit()) + if (VarIsArtificial) Flags |= llvm::DINode::FlagArtificial; auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); diff --git a/clang/test/CodeGenCXX/debug-info-structured-binding.cpp b/clang/test/CodeGenCXX/debug-info-structured-binding.cpp index c88a5cdaa20e7..5fbd54c16382c 100644 --- a/clang/test/CodeGenCXX/debug-info-structured-binding.cpp +++ b/clang/test/CodeGenCXX/debug-info-structured-binding.cpp @@ -5,20 +5,46 @@ // CHECK: #dbg_declare(ptr %{{[0-9]+}}, ![[VAR_2:[0-9]+]], !DIExpression(DW_OP_plus_uconst, 4), // CHECK: #dbg_declare(ptr %{{[0-9]+}}, ![[VAR_3:[0-9]+]], !DIExpression(DW_OP_deref), // CHECK: #dbg_declare(ptr %{{[0-9]+}}, ![[VAR_4:[0-9]+]], !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 4), +// CHECK: #dbg_declare(ptr %z1, ![[VAR_5:[0-9]+]], !DIExpression() +// CHECK: #dbg_declare(ptr %z2, ![[VAR_6:[0-9]+]], !DIExpression() // CHECK: ![[VAR_0]] = !DILocalVariable(name: "a" -// CHECK: ![[VAR_1]] = !DILocalVariable(name: "x1" -// CHECK: ![[VAR_2]] = !DILocalVariable(name: "y1" -// CHECK: ![[VAR_3]] = !DILocalVariable(name: "x2" -// CHECK: ![[VAR_4]] = !DILocalVariable(name: "y2" +// CHECK: ![[VAR_1]] = !DILocalVariable(name: "x1", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}) +// CHECK: ![[VAR_2]] = !DILocalVariable(name: "y1", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}) +// CHECK: ![[VAR_3]] = !DILocalVariable(name: "x2", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}) +// CHECK: ![[VAR_4]] = !DILocalVariable(name: "y2", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}) +// CHECK: ![[VAR_5]] = !DILocalVariable(name: "z1", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}) +// CHECK: ![[VAR_6]] = !DILocalVariable(name: "z2", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}) struct A { int x; int y; }; +struct B { + int w; + int z; + template int get(); + template<> int get<0>() { return w; } + template<> int get<1>() { return z; } +}; + +// Note: the following declarations are necessary for decomposition of tuple-like +// structured bindings +namespace std { +template struct tuple_size { +}; +template<> +struct tuple_size { + static constexpr unsigned value = 2; +}; + +template struct tuple_element { using type = int; }; +} // namespace std + int f() { A a{10, 20}; auto [x1, y1] = a; auto &[x2, y2] = a; - return x1 + y1 + x2 + y2; + auto [z1, z2] = B{1, 2}; + return x1 + y1 + x2 + y2 + z1 + z2; }