From aabe21c59d9feca85cc08e22720452970b0700ab Mon Sep 17 00:00:00 2001 From: skill Date: Mon, 13 Oct 2025 14:12:09 +0200 Subject: [PATCH 01/10] Emit debug info with original source location for tokens from macros arguments. --- clang/lib/CodeGen/CGDebugInfo.cpp | 42 ++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 9fe9a13610296..4b1c299eb99d5 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -110,6 +110,34 @@ static bool IsArtificial(VarDecl const *VD) { cast(VD->getDeclContext())->isImplicit()); } +/// If the given source location resides in a macro argument, this function +/// recursively resolves through macro argument expansions to return a source +/// location that points to where the argument was spelled in the macro +/// invocation. This is necessary for generating correct debug information for +/// code inside macro arguments, as we want to point to user-written code. +static SourceLocation resolveSpellingLocation(SourceManager &SM, + SourceLocation Loc) { + SourceLocation ParentLoc = Loc; + + while (SM.isMacroArgExpansion(ParentLoc)) { + ParentLoc = SM.getImmediateMacroCallerLoc(ParentLoc); + } + + if (ParentLoc.isMacroID()) { + return ParentLoc; + } + return SM.getSpellingLoc(Loc); +} + +/// Returns the presumed location for a given source location, resolving +/// through macro arguments first. This ensures that file, line, and column +/// information points to where a macro argument was spelled, rather than where +/// it was expanded. +static PresumedLoc getPresumedSpellingLoc(SourceManager &SM, + SourceLocation Loc) { + return SM.getPresumedLoc(resolveSpellingLocation(SM, Loc)); +} + CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()), DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs), @@ -318,7 +346,8 @@ void CGDebugInfo::setLocation(SourceLocation Loc) { if (Loc.isInvalid()) return; - CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); + SourceManager &SM = CGM.getContext().getSourceManager(); + CurLoc = SM.getExpansionLoc(resolveSpellingLocation(SM, Loc)); // If we've changed files in the middle of a lexical scope go ahead // and create a new lexical scope with file node if it's different @@ -326,7 +355,6 @@ void CGDebugInfo::setLocation(SourceLocation Loc) { if (LexicalBlockStack.empty()) return; - SourceManager &SM = CGM.getContext().getSourceManager(); auto *Scope = cast(LexicalBlockStack.back()); PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); if (PCLoc.isInvalid() || Scope->getFile() == getOrCreateFile(CurLoc)) @@ -545,7 +573,7 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) { FileName = TheCU->getFile()->getFilename(); CSInfo = TheCU->getFile()->getChecksum(); } else { - PresumedLoc PLoc = SM.getPresumedLoc(Loc); + PresumedLoc PLoc = getPresumedSpellingLoc(SM, Loc); FileName = PLoc.getFilename(); if (FileName.empty()) { @@ -627,7 +655,7 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { if (Loc.isInvalid()) return 0; SourceManager &SM = CGM.getContext().getSourceManager(); - return SM.getPresumedLoc(Loc).getLine(); + return getPresumedSpellingLoc(SM, Loc).getLine(); } unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { @@ -639,7 +667,8 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { if (Loc.isInvalid() && CurLoc.isInvalid()) return 0; SourceManager &SM = CGM.getContext().getSourceManager(); - PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); + PresumedLoc PLoc = Loc.isValid() ? getPresumedSpellingLoc(SM, Loc) + : SM.getPresumedLoc(CurLoc); return PLoc.isValid() ? PLoc.getColumn() : 0; } @@ -6183,7 +6212,8 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV, void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, const StringLiteral *S) { SourceLocation Loc = S->getStrTokenLoc(0); - PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc); + PresumedLoc PLoc = + getPresumedSpellingLoc(CGM.getContext().getSourceManager(), Loc); if (!PLoc.isValid()) return; From cf8a150a49970895a6efae45e7b65a16360ddf2f Mon Sep 17 00:00:00 2001 From: skill Date: Tue, 14 Oct 2025 12:03:12 +0200 Subject: [PATCH 02/10] Add getRefinedSpellingLoc to SourceManager. The method preserves source locations for macro arguments. --- clang/include/clang/Basic/SourceManager.h | 16 +++++++++ clang/lib/Basic/SourceManager.cpp | 14 ++++++++ clang/lib/CodeGen/CGDebugInfo.cpp | 42 ++++------------------- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index ed967fd47dc83..5e8ca172a89c0 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -1250,6 +1250,21 @@ class SourceManager : public RefCountedBase { return getSpellingLocSlowCase(Loc); } + /// Given a SourceLocation object, return the refined spelling + /// location referenced by the ID. + /// + /// The key difference to \ref getSpellingLoc is that the source location + /// for macro body is resolved to the expansion site. + /// + /// This is the place where the characters that make up the lexed token + /// can be found. + SourceLocation getRefinedSpellingLoc(SourceLocation Loc) const { + // Handle the non-mapped case inline, defer to out of line code to handle + // expansions. + if (Loc.isFileID()) return Loc; + return getRefinedSpellingLocSlowCase(Loc); + } + /// Given a SourceLocation object, return the spelling location /// referenced by the ID. /// @@ -1977,6 +1992,7 @@ class SourceManager : public RefCountedBase { SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; + SourceLocation getRefinedSpellingLocSlowCase(SourceLocation Loc) const; SourceLocation getFileLocSlowCase(SourceLocation Loc) const; FileIDAndOffset diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index d8ec837f0f7b9..d71580763af69 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -915,6 +915,20 @@ SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { return Loc; } +SourceLocation +SourceManager::getRefinedSpellingLocSlowCase(SourceLocation Loc) const { + do { + FileIDAndOffset LocInfo = getDecomposedLoc(Loc); + const ExpansionInfo &Expansion = getSLocEntry(LocInfo.first).getExpansion(); + if (Expansion.isMacroArgExpansion()) { + Loc = Expansion.getSpellingLoc().getLocWithOffset(LocInfo.second); + } else { + Loc = Expansion.getExpansionLocStart(); + } + } while (!Loc.isFileID()); + return Loc; +} + SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const { do { if (isMacroArgExpansion(Loc)) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 4b1c299eb99d5..618aca7fa9304 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -110,34 +110,6 @@ static bool IsArtificial(VarDecl const *VD) { cast(VD->getDeclContext())->isImplicit()); } -/// If the given source location resides in a macro argument, this function -/// recursively resolves through macro argument expansions to return a source -/// location that points to where the argument was spelled in the macro -/// invocation. This is necessary for generating correct debug information for -/// code inside macro arguments, as we want to point to user-written code. -static SourceLocation resolveSpellingLocation(SourceManager &SM, - SourceLocation Loc) { - SourceLocation ParentLoc = Loc; - - while (SM.isMacroArgExpansion(ParentLoc)) { - ParentLoc = SM.getImmediateMacroCallerLoc(ParentLoc); - } - - if (ParentLoc.isMacroID()) { - return ParentLoc; - } - return SM.getSpellingLoc(Loc); -} - -/// Returns the presumed location for a given source location, resolving -/// through macro arguments first. This ensures that file, line, and column -/// information points to where a macro argument was spelled, rather than where -/// it was expanded. -static PresumedLoc getPresumedSpellingLoc(SourceManager &SM, - SourceLocation Loc) { - return SM.getPresumedLoc(resolveSpellingLocation(SM, Loc)); -} - CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()), DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs), @@ -347,7 +319,7 @@ void CGDebugInfo::setLocation(SourceLocation Loc) { return; SourceManager &SM = CGM.getContext().getSourceManager(); - CurLoc = SM.getExpansionLoc(resolveSpellingLocation(SM, Loc)); + CurLoc = SM.getRefinedSpellingLoc(Loc); // If we've changed files in the middle of a lexical scope go ahead // and create a new lexical scope with file node if it's different @@ -573,7 +545,7 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) { FileName = TheCU->getFile()->getFilename(); CSInfo = TheCU->getFile()->getChecksum(); } else { - PresumedLoc PLoc = getPresumedSpellingLoc(SM, Loc); + PresumedLoc PLoc = SM.getPresumedLoc(SM.getRefinedSpellingLoc(Loc)); FileName = PLoc.getFilename(); if (FileName.empty()) { @@ -655,7 +627,7 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { if (Loc.isInvalid()) return 0; SourceManager &SM = CGM.getContext().getSourceManager(); - return getPresumedSpellingLoc(SM, Loc).getLine(); + return SM.getPresumedLoc(SM.getRefinedSpellingLoc(Loc)).getLine(); } unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { @@ -667,8 +639,8 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { if (Loc.isInvalid() && CurLoc.isInvalid()) return 0; SourceManager &SM = CGM.getContext().getSourceManager(); - PresumedLoc PLoc = Loc.isValid() ? getPresumedSpellingLoc(SM, Loc) - : SM.getPresumedLoc(CurLoc); + PresumedLoc PLoc = + SM.getPresumedLoc(Loc.isValid() ? SM.getRefinedSpellingLoc(Loc) : CurLoc); return PLoc.isValid() ? PLoc.getColumn() : 0; } @@ -6212,8 +6184,8 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV, void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, const StringLiteral *S) { SourceLocation Loc = S->getStrTokenLoc(0); - PresumedLoc PLoc = - getPresumedSpellingLoc(CGM.getContext().getSourceManager(), Loc); + SourceManager &SM = CGM.getContext().getSourceManager(); + PresumedLoc PLoc = SM.getPresumedLoc(SM.getRefinedSpellingLoc(Loc)); if (!PLoc.isValid()) return; From 3afd854b9cb3cd00a8f262add70d71f824f68f90 Mon Sep 17 00:00:00 2001 From: skill Date: Tue, 14 Oct 2025 14:55:29 +0200 Subject: [PATCH 03/10] Minimize the amount of location expansions inside CGDebugInfo. * Add unit test. --- clang/lib/CodeGen/CGDebugInfo.cpp | 184 +++++++++++++--------- clang/test/DebugInfo/Generic/macro-info.c | 21 +++ 2 files changed, 131 insertions(+), 74 deletions(-) create mode 100644 clang/test/DebugInfo/Generic/macro-info.c diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 618aca7fa9304..80812e6415e95 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -545,7 +545,8 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) { FileName = TheCU->getFile()->getFilename(); CSInfo = TheCU->getFile()->getChecksum(); } else { - PresumedLoc PLoc = SM.getPresumedLoc(SM.getRefinedSpellingLoc(Loc)); + assert(Loc.isFileID()); + PresumedLoc PLoc = SM.getPresumedLoc(Loc); FileName = PLoc.getFilename(); if (FileName.empty()) { @@ -627,7 +628,8 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { if (Loc.isInvalid()) return 0; SourceManager &SM = CGM.getContext().getSourceManager(); - return SM.getPresumedLoc(SM.getRefinedSpellingLoc(Loc)).getLine(); + assert(Loc.isFileID()); + return SM.getPresumedLoc(Loc).getLine(); } unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { @@ -639,8 +641,8 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { if (Loc.isInvalid() && CurLoc.isInvalid()) return 0; SourceManager &SM = CGM.getContext().getSourceManager(); - PresumedLoc PLoc = - SM.getPresumedLoc(Loc.isValid() ? SM.getRefinedSpellingLoc(Loc) : CurLoc); + assert(Loc.isFileID()); + PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); return PLoc.isValid() ? PLoc.getColumn() : 0; } @@ -1330,9 +1332,9 @@ CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty, const RecordDecl *RD = Ty->getOriginalDecl()->getDefinitionOrSelf(); if (llvm::DIType *T = getTypeOrNull(QualType(Ty, 0))) return cast(T); - llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); - const unsigned Line = - getLineNumber(RD->getLocation().isValid() ? RD->getLocation() : CurLoc); + SourceLocation Loc = getRefinedSpellingLocation(RD->getLocation()); + llvm::DIFile *DefUnit = getOrCreateFile(Loc); + const unsigned Line = getLineNumber(Loc.isValid() ? Loc : CurLoc); StringRef RDName = getClassName(RD); uint64_t Size = 0; @@ -1559,7 +1561,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty, auto PP = getPrintingPolicy(); Ty->getTemplateName().print(OS, PP, TemplateName::Qualified::None); - SourceLocation Loc = AliasDecl->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(AliasDecl->getLocation()); if (CGM.getCodeGenOpts().DebugTemplateAlias) { auto ArgVector = ::GetTemplateArgs(TD, Ty); @@ -1628,7 +1630,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty, // We don't set size information, but do specify where the typedef was // declared. - SourceLocation Loc = Ty->getDecl()->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(Ty->getDecl()->getLocation()); uint32_t Align = getDeclAlignIfRequired(Ty->getDecl(), CGM.getContext()); // Typedefs are derived from some other type. @@ -1762,7 +1764,7 @@ CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl, QualType Ty = BitFieldDecl->getType(); if (BitFieldDecl->hasAttr()) Ty = BitFieldDecl->getAttr()->getType(); - SourceLocation Loc = BitFieldDecl->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(BitFieldDecl->getLocation()); llvm::DIFile *VUnit = getOrCreateFile(Loc); llvm::DIType *DebugType = getOrCreateType(Ty, VUnit); @@ -1840,7 +1842,8 @@ llvm::DIDerivedType *CGDebugInfo::createBitFieldSeparatorIfNeeded( return nullptr; QualType Ty = PreviousBitfield->getType(); - SourceLocation Loc = PreviousBitfield->getLocation(); + SourceLocation Loc = + getRefinedSpellingLocation(PreviousBitfield->getLocation()); llvm::DIFile *VUnit = getOrCreateFile(Loc); llvm::DIType *DebugType = getOrCreateType(Ty, VUnit); llvm::DIScope *RecordTy = BitFieldDI->getScope(); @@ -1959,6 +1962,7 @@ void CGDebugInfo::CollectRecordLambdaFields( continue; } + Loc = getRefinedSpellingLocation(Loc); llvm::DIFile *VUnit = getOrCreateFile(Loc); elements.push_back(createFieldType( @@ -1973,10 +1977,11 @@ CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy, // Create the descriptor for the static variable, with or without // constant initializers. Var = Var->getCanonicalDecl(); - llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(Var->getLocation()); + llvm::DIFile *VUnit = getOrCreateFile(Loc); llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit); - unsigned LineNumber = getLineNumber(Var->getLocation()); + unsigned LineNumber = getLineNumber(Loc); StringRef VName = Var->getName(); // FIXME: to avoid complications with type merging we should @@ -2024,9 +2029,10 @@ void CGDebugInfo::CollectRecordNormalField( } else { auto Align = getDeclAlignIfRequired(field, CGM.getContext()); llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(field); - FieldType = - createFieldType(name, type, field->getLocation(), field->getAccess(), - OffsetInBits, Align, tunit, RecordTy, RD, Annotations); + FieldType = createFieldType( + name, type, getRefinedSpellingLocation(field->getLocation()), + field->getAccess(), OffsetInBits, Align, tunit, RecordTy, RD, + Annotations); } elements.push_back(FieldType); @@ -2040,7 +2046,7 @@ void CGDebugInfo::CollectRecordNestedType( // instead? if (isa(Ty)) return; - SourceLocation Loc = TD->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(TD->getLocation()); if (llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc))) elements.push_back(nestedType); } @@ -2244,8 +2250,9 @@ llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction( llvm::DIFile *MethodDefUnit = nullptr; unsigned MethodLine = 0; if (!Method->isImplicit()) { - MethodDefUnit = getOrCreateFile(Method->getLocation()); - MethodLine = getLineNumber(Method->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(Method->getLocation()); + MethodDefUnit = getOrCreateFile(Loc); + MethodLine = getLineNumber(Loc); } // Collect virtual method info. @@ -2696,7 +2703,7 @@ void CGDebugInfo::emitVTableSymbol(llvm::GlobalVariable *VTable, ASTContext &Context = CGM.getContext(); StringRef SymbolName = "_vtable$"; - SourceLocation Loc; + SourceLocation InvalidLoc; QualType VoidPtr = Context.getPointerType(Context.VoidTy); // We deal with two different contexts: @@ -2708,7 +2715,7 @@ void CGDebugInfo::emitVTableSymbol(llvm::GlobalVariable *VTable, // placed inside the scope of the C++ class/structure. llvm::DIScope *DContext = getContextDescriptor(RD, TheCU); auto *Ctxt = cast(DContext); - llvm::DIFile *Unit = getOrCreateFile(Loc); + llvm::DIFile *Unit = getOrCreateFile(InvalidLoc); llvm::DIType *VTy = getOrCreateType(VoidPtr, Unit); llvm::DINode::DIFlags Flags = getAccessFlag(AccessSpecifier::AS_private, RD) | llvm::DINode::FlagArtificial; @@ -2845,6 +2852,7 @@ void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit, llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy, SourceLocation Loc) { assert(CGM.getCodeGenOpts().hasReducedDebugInfo()); + Loc = getRefinedSpellingLocation(Loc); llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc)); return T; } @@ -2858,6 +2866,7 @@ llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D, SourceLocation Loc) { assert(CGM.getCodeGenOpts().hasReducedDebugInfo()); assert(!D.isNull() && "null type"); + Loc = getRefinedSpellingLocation(Loc); llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc)); assert(T && "could not create debug info for type"); @@ -2875,7 +2884,8 @@ void CGDebugInfo::addHeapAllocSiteMetadata(llvm::CallBase *CI, if (AllocatedTy->isVoidType()) node = llvm::MDNode::get(CGM.getLLVMContext(), {}); else - node = getOrCreateType(AllocatedTy, getOrCreateFile(Loc)); + node = getOrCreateType(AllocatedTy, + getOrCreateFile(getRefinedSpellingLocation(Loc))); CI->setMetadata("heapallocsite", node); } @@ -3108,8 +3118,9 @@ std::pair CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) { RecordDecl *RD = Ty->getOriginalDecl()->getDefinitionOrSelf(); + SourceLocation Loc = getRefinedSpellingLocation(RD->getLocation()); // Get overall information about the record type for the debug info. - llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); + llvm::DIFile *DefUnit = getOrCreateFile(Loc); // Records and classes and unions can all be recursive. To handle them, we // first generate a debug descriptor for the struct as a forward declaration. @@ -3177,7 +3188,7 @@ llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty, llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty, llvm::DIFile *Unit) { // Ignore protocols. - SourceLocation Loc = Ty->getDecl()->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(Ty->getDecl()->getLocation()); // Use Typedefs to represent ObjCTypeParamType. return DBuilder.createTypedef( @@ -3228,9 +3239,10 @@ llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, llvm::dwarf::DW_TAG_structure_type, ID->getName(), getDeclContextDescriptor(ID), Unit, 0, RuntimeLang); + SourceLocation Loc = getRefinedSpellingLocation(ID->getLocation()); // Get overall information about the record type for the debug info. - llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); - unsigned Line = getLineNumber(ID->getLocation()); + llvm::DIFile *DefUnit = getOrCreateFile(Loc); + unsigned Line = getLineNumber(Loc); // If this is just a forward declaration return a special forward-declaration // debug type since we won't be able to lay out the entire type. @@ -3351,8 +3363,9 @@ llvm::DIModule *CGDebugInfo::getOrCreateModuleRef(ASTSourceDescriptor Mod, llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty, llvm::DIFile *Unit) { ObjCInterfaceDecl *ID = Ty->getDecl(); - llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); - unsigned Line = getLineNumber(ID->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(ID->getLocation()); + llvm::DIFile *DefUnit = getOrCreateFile(Loc); + unsigned Line = getLineNumber(Loc); unsigned RuntimeLang = TheCU->getSourceLanguage().getUnversionedName(); @@ -3447,9 +3460,10 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty, if (FieldName.empty()) continue; + SourceLocation Loc = getRefinedSpellingLocation(Field->getLocation()); // Get the location for the field. - llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation()); - unsigned FieldLine = getLineNumber(Field->getLocation()); + llvm::DIFile *FieldDefUnit = getOrCreateFile(Loc); + unsigned FieldLine = getLineNumber(Loc); QualType FType = Field->getType(); uint64_t FieldSize = 0; uint32_t FieldAlign = 0; @@ -3494,7 +3508,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty, if (ObjCPropertyImplDecl *PImpD = ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) { if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) { - SourceLocation Loc = PD->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(PD->getLocation()); llvm::DIFile *PUnit = getOrCreateFile(Loc); unsigned PLine = getLineNumber(Loc); ObjCMethodDecl *Getter = PImpD->getGetterMethodDecl(); @@ -3776,11 +3790,12 @@ llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) { // FwdDecl with the second and then replace the second with // complete type. llvm::DIScope *EDContext = getDeclContextDescriptor(ED); - llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(ED->getLocation()); + llvm::DIFile *DefUnit = getOrCreateFile(Loc); llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType( llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0)); - unsigned Line = getLineNumber(ED->getLocation()); + unsigned Line = getLineNumber(Loc); StringRef EDName = ED->getName(); llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType( llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line, @@ -3813,8 +3828,9 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) { // Return a CompositeType for the enum itself. llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators); - llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); - unsigned Line = getLineNumber(ED->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(ED->getLocation()); + llvm::DIFile *DefUnit = getOrCreateFile(Loc); + unsigned Line = getLineNumber(Loc); llvm::DIScope *EnumContext = getDeclContextDescriptor(ED); llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit); return DBuilder.createEnumerationType( @@ -3832,8 +3848,10 @@ llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent, llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent, SourceLocation LineLoc, SourceLocation FileLoc) { - llvm::DIFile *FName = getOrCreateFile(FileLoc); - unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); + llvm::DIFile *FName = getOrCreateFile(getRefinedSpellingLocation(FileLoc)); + unsigned Line = LineLoc.isInvalid() + ? 0 + : getLineNumber(getRefinedSpellingLocation(LineLoc)); return DBuilder.createTempMacroFile(Parent, Line, FName); } @@ -4156,7 +4174,7 @@ llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) { // Get overall information about the record type for the debug info. StringRef RDName = getClassName(RD); - const SourceLocation Loc = RD->getLocation(); + const SourceLocation Loc = getRefinedSpellingLocation(RD->getLocation()); llvm::DIFile *DefUnit = nullptr; unsigned Line = 0; if (Loc.isValid()) { @@ -4271,7 +4289,8 @@ void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD, break; } CanQualType T = CGM.getContext().getCanonicalTagType(PBase); - ContainingType = getOrCreateType(T, getOrCreateFile(RD->getLocation())); + SourceLocation Loc = getRefinedSpellingLocation(RD->getLocation()); + ContainingType = getOrCreateType(T, getOrCreateFile(Loc)); } else if (RD->isDynamicClass()) ContainingType = RealDecl; @@ -4342,10 +4361,11 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, StringRef &Name, StringRef &LinkageName, llvm::MDTuple *&TemplateParameters, llvm::DIScope *&VDContext) { - Unit = getOrCreateFile(VD->getLocation()); - LineNo = getLineNumber(VD->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation()); + Unit = getOrCreateFile(Loc); + LineNo = getLineNumber(Loc); - setLocation(VD->getLocation()); + setLocation(Loc); T = VD->getType(); if (T->isIncompleteArrayType()) { @@ -4398,7 +4418,7 @@ llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD, StringRef Name, LinkageName; llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero; - SourceLocation Loc = GD.getDecl()->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(GD.getDecl()->getLocation()); llvm::DIFile *Unit = getOrCreateFile(Loc); llvm::DIScope *DContext = Unit; unsigned Line = getLineNumber(Loc); @@ -4453,7 +4473,7 @@ llvm::DIGlobalVariable * CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) { QualType T; StringRef Name, LinkageName; - SourceLocation Loc = VD->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation()); llvm::DIFile *Unit = getOrCreateFile(Loc); llvm::DIScope *DContext = Unit; unsigned Line = getLineNumber(Loc); @@ -4479,7 +4499,8 @@ llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) { // in unlimited debug info) if (const auto *TD = dyn_cast(D)) { QualType Ty = CGM.getContext().getTypeDeclType(TD); - return getOrCreateType(Ty, getOrCreateFile(TD->getLocation())); + SourceLocation Loc = getRefinedSpellingLocation(TD->getLocation()); + return getOrCreateType(Ty, getOrCreateFile(Loc)); } auto I = DeclCache.find(D->getCanonicalDecl()); @@ -4525,7 +4546,8 @@ llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) { auto MI = SPCache.find(FD->getCanonicalDecl()); if (MI == SPCache.end()) { if (const auto *MD = dyn_cast(FD->getCanonicalDecl())) { - return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), + SourceLocation Loc = getRefinedSpellingLocation(MD->getLocation()); + return CreateCXXMemberFunction(MD, getOrCreateFile(Loc), cast(S)); } } @@ -4687,6 +4709,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc, const Decl *D = GD.getDecl(); bool HasDecl = (D != nullptr); + Loc = getRefinedSpellingLocation(Loc); llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero; @@ -4755,7 +4778,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc, SPFlags | llvm::DISubprogram::SPFlagDefinition; const unsigned LineNo = getLineNumber(Loc.isValid() ? Loc : CurLoc); - unsigned ScopeLine = getLineNumber(ScopeLoc); + unsigned ScopeLine = getLineNumber(getRefinedSpellingLocation(ScopeLoc)); llvm::DISubroutineType *DIFnType = getOrCreateFunctionType(D, FnType, Unit); llvm::DISubprogram *Decl = nullptr; llvm::DINodeArray Annotations = nullptr; @@ -4803,6 +4826,7 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, return GetName(D, true); }); + Loc = getRefinedSpellingLocation(Loc); llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; llvm::DIFile *Unit = getOrCreateFile(Loc); bool IsDeclForCallSite = Fn ? true : false; @@ -4930,6 +4954,10 @@ void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { getColumnNumber(CurLoc))); } +SourceLocation CGDebugInfo::getRefinedSpellingLocation(SourceLocation Loc) const { + return CGM.getContext().getSourceManager().getRefinedSpellingLoc(Loc); +} + void CGDebugInfo::AppendAddressSpaceXDeref( unsigned AddressSpace, SmallVectorImpl &Expr) const { std::optional DWARFAddressSpace = @@ -4946,6 +4974,7 @@ void CGDebugInfo::AppendAddressSpaceXDeref( void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) { // Set our current location. + Loc = getRefinedSpellingLocation(Loc); setLocation(Loc); // Emit a line table change for the current location inside the new scope. @@ -4998,7 +5027,8 @@ CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD, uint64_t FieldSize, FieldOffset; uint32_t FieldAlign; - llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation()); + llvm::DIFile *Unit = getOrCreateFile(Loc); QualType Type = VD->getType(); FieldOffset = 0; @@ -5074,8 +5104,9 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, const bool VarIsArtificial = IsArtificial(VD); llvm::DIFile *Unit = nullptr; + SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation()); if (!VarIsArtificial) - Unit = getOrCreateFile(VD->getLocation()); + Unit = getOrCreateFile(Loc); llvm::DIType *Ty; uint64_t XOffset = 0; if (VD->hasAttr()) @@ -5092,8 +5123,8 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Line = 0; unsigned Column = 0; if (!VarIsArtificial) { - Line = getLineNumber(VD->getLocation()); - Column = getColumnNumber(VD->getLocation()); + Line = getLineNumber(Loc); + Column = getColumnNumber(Loc); } SmallVector Expr; llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; @@ -5259,7 +5290,8 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD, if (isa(BD->getBinding())) return nullptr; - llvm::DIFile *Unit = getOrCreateFile(BD->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(BD->getLocation()); + llvm::DIFile *Unit = getOrCreateFile(Loc); llvm::DIType *Ty = getOrCreateType(BD->getType(), Unit); // If there is no debug info for this type then do not emit debug info @@ -5282,8 +5314,8 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD, Expr.push_back(llvm::dwarf::DW_OP_deref); } - unsigned Line = getLineNumber(BD->getLocation()); - unsigned Column = getColumnNumber(BD->getLocation()); + unsigned Line = getLineNumber(Loc); + unsigned Column = getColumnNumber(Loc); StringRef Name = BD->getName(); auto *Scope = cast(LexicalBlockStack.back()); // Create the descriptor for the variable. @@ -5377,11 +5409,12 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) { return; auto *Scope = cast(LexicalBlockStack.back()); - llvm::DIFile *Unit = getOrCreateFile(D->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(D->getLocation()); + llvm::DIFile *Unit = getOrCreateFile(Loc); // Get location information. - unsigned Line = getLineNumber(D->getLocation()); - unsigned Column = getColumnNumber(D->getLocation()); + unsigned Line = getLineNumber(Loc); + unsigned Column = getColumnNumber(Loc); StringRef Name = D->getName(); @@ -5420,7 +5453,8 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( bool isByRef = VD->hasAttr(); uint64_t XOffset = 0; - llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation()); + llvm::DIFile *Unit = getOrCreateFile(Loc); llvm::DIType *Ty; if (isByRef) Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType; @@ -5435,8 +5469,8 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( // Get location information. const unsigned Line = - getLineNumber(VD->getLocation().isValid() ? VD->getLocation() : CurLoc); - unsigned Column = getColumnNumber(VD->getLocation()); + getLineNumber(Loc.isValid() ? Loc : CurLoc); + unsigned Column = getColumnNumber(Loc); const llvm::DataLayout &target = CGM.getDataLayout(); @@ -5544,7 +5578,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, const BlockDecl *blockDecl = block.getBlockDecl(); // Collect some general information about the block's location. - SourceLocation loc = blockDecl->getCaretLocation(); + SourceLocation loc = getRefinedSpellingLocation(blockDecl->getCaretLocation()); llvm::DIFile *tunit = getOrCreateFile(loc); unsigned line = getLineNumber(loc); unsigned column = getColumnNumber(loc); @@ -6016,8 +6050,9 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { }); auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); + SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation()); // Create the descriptor for the variable. - llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); + llvm::DIFile *Unit = getOrCreateFile(Loc); StringRef Name = VD->getName(); llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit); @@ -6075,7 +6110,7 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { } GV.reset(DBuilder.createGlobalVariableExpression( - DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty, + DContext, Name, StringRef(), Unit, getLineNumber(Loc), Ty, true, true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD), TemplateParameters, Align)); } @@ -6087,14 +6122,15 @@ void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var, return; auto Align = getDeclAlignIfRequired(D, CGM.getContext()); - llvm::DIFile *Unit = getOrCreateFile(D->getLocation()); + SourceLocation Loc = getRefinedSpellingLocation(D->getLocation()); + llvm::DIFile *Unit = getOrCreateFile(Loc); StringRef Name = D->getName(); llvm::DIType *Ty = getOrCreateType(D->getType(), Unit); llvm::DIScope *DContext = getDeclContextDescriptor(D); llvm::DIGlobalVariableExpression *GVE = DBuilder.createGlobalVariableExpression( - DContext, Name, StringRef(), Unit, getLineNumber(D->getLocation()), + DContext, Name, StringRef(), Unit, getLineNumber(Loc), Ty, false, false, nullptr, nullptr, nullptr, Align); Var->addDebugInfo(GVE); } @@ -6172,7 +6208,7 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV, return; llvm::DIScope *DContext = getDeclContextDescriptor(D); - auto Loc = D->getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(D->getLocation()); llvm::DIImportedEntity *ImportDI = DBuilder.createImportedDeclaration( DContext, DI, getOrCreateFile(Loc), getLineNumber(Loc), D->getName()); @@ -6183,16 +6219,15 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV, void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, const StringLiteral *S) { - SourceLocation Loc = S->getStrTokenLoc(0); - SourceManager &SM = CGM.getContext().getSourceManager(); - PresumedLoc PLoc = SM.getPresumedLoc(SM.getRefinedSpellingLoc(Loc)); + SourceLocation Loc = getRefinedSpellingLocation(S->getStrTokenLoc(0)); + PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc); if (!PLoc.isValid()) return; llvm::DIFile *File = getOrCreateFile(Loc); llvm::DIGlobalVariableExpression *Debug = DBuilder.createGlobalVariableExpression( - nullptr, StringRef(), StringRef(), getOrCreateFile(Loc), + nullptr, StringRef(), StringRef(), File, getLineNumber(Loc), getOrCreateType(S->getType(), File), true); GV->addDebugInfo(Debug); } @@ -6210,7 +6245,7 @@ void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) { const NamespaceDecl *NSDecl = UD.getNominatedNamespace(); if (!NSDecl->isAnonymousNamespace() || CGM.getCodeGenOpts().DebugExplicitImport) { - auto Loc = UD.getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(UD.getLocation()); if (!Loc.isValid()) Loc = CurLoc; DBuilder.createImportedModule( @@ -6222,7 +6257,7 @@ void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) { void CGDebugInfo::EmitUsingShadowDecl(const UsingShadowDecl &USD) { if (llvm::DINode *Target = getDeclarationOrDefinition(USD.getUnderlyingDecl())) { - auto Loc = USD.getLocation(); + SourceLocation Loc = getRefinedSpellingLocation(USD.getLocation()); DBuilder.createImportedDeclaration( getCurrentContextDescriptor(cast(USD.getDeclContext())), Target, getOrCreateFile(Loc), getLineNumber(Loc)); @@ -6270,7 +6305,7 @@ void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) { return; if (Module *M = ID.getImportedModule()) { auto Info = ASTSourceDescriptor(*M); - auto Loc = ID.getLocation(); + auto Loc = getRefinedSpellingLocation(ID.getLocation()); DBuilder.createImportedDeclaration( getCurrentContextDescriptor(cast(ID.getDeclContext())), getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc), @@ -6286,7 +6321,7 @@ CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) { if (VH) return cast(VH); llvm::DIImportedEntity *R; - auto Loc = NA.getLocation(); + auto Loc = getRefinedSpellingLocation(NA.getLocation()); if (const auto *Underlying = dyn_cast(NA.getAliasedNamespace())) // This could cache & dedup here rather than relying on metadata deduping. @@ -6418,6 +6453,7 @@ llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) { if (LexicalBlockStack.empty()) return llvm::DebugLoc(); + Loc = getRefinedSpellingLocation(Loc); llvm::MDNode *Scope = LexicalBlockStack.back(); return llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(Loc), getColumnNumber(Loc), Scope); diff --git a/clang/test/DebugInfo/Generic/macro-info.c b/clang/test/DebugInfo/Generic/macro-info.c new file mode 100644 index 0000000000000..5cbd97b8d206e --- /dev/null +++ b/clang/test/DebugInfo/Generic/macro-info.c @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -o - | FileCheck %s +#define ID(x) x + +// CHECK: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE+3]] +// CHECK: DIGlobalVariable({{.*}}line: [[@LINE+5]],{{.*}} type: [[TYPEID:![0-9]+]] +ID( + int global = 42; + + const char* s() { + return "1234567890"; + } +) + +#define SWAP(x,y) y; x + +// CHECK: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE+4]] +// CHECK: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE+2]] +SWAP( + int global2 = 43, + int global3 = 44 +); \ No newline at end of file From 731f671dd61853a0aec85e9e10eaec4c82975e52 Mon Sep 17 00:00:00 2001 From: skill Date: Tue, 14 Oct 2025 15:23:33 +0200 Subject: [PATCH 04/10] Add more cases to macro-info test. --- clang/test/DebugInfo/Generic/macro-info.c | 35 +++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/clang/test/DebugInfo/Generic/macro-info.c b/clang/test/DebugInfo/Generic/macro-info.c index 5cbd97b8d206e..5029c110ee170 100644 --- a/clang/test/DebugInfo/Generic/macro-info.c +++ b/clang/test/DebugInfo/Generic/macro-info.c @@ -1,21 +1,32 @@ // RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -o - | FileCheck %s -#define ID(x) x -// CHECK: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE+3]] -// CHECK: DIGlobalVariable({{.*}}line: [[@LINE+5]],{{.*}} type: [[TYPEID:![0-9]+]] -ID( - int global = 42; +#define GLOBAL(num) global##num +#define DECL_GLOBAL(x) int x +#define SAME_ORDER(x, y) x; y +#define SWAP_ORDER(x,y) y; x +// CHECK: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE+4]] +// CHECK: DIGlobalVariable({{.*}}line: [[@LINE+6]],{{.*}} type: [[TYPEID:![0-9]+]] +SAME_ORDER( + int + GLOBAL // <- global + () = 42, const char* s() { return "1234567890"; } ) +// CHECK: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE+6]] +// CHECK: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE+2]] +SWAP_ORDER( + int GLOBAL( // <- global2 + 2) = 43, + DECL_GLOBAL( + GLOBAL( // <- global3 + 3)) = 44 +); -#define SWAP(x,y) y; x -// CHECK: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE+4]] -// CHECK: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE+2]] -SWAP( - int global2 = 43, - int global3 = 44 -); \ No newline at end of file +// CHECK: DIGlobalVariable(name: "global4",{{.*}} line: [[@LINE+2]] +DECL_GLOBAL( + GLOBAL( // <- global4 + 4)); From 252e53771cea04db2ccc1f60cafd55b7d3469eda Mon Sep 17 00:00:00 2001 From: skill Date: Tue, 14 Oct 2025 15:54:29 +0200 Subject: [PATCH 05/10] Add comment for getRefinedSpellingLocation. --- clang/lib/CodeGen/CGDebugInfo.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 80812e6415e95..690cb2cd3c788 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -4954,7 +4954,16 @@ void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { getColumnNumber(CurLoc))); } -SourceLocation CGDebugInfo::getRefinedSpellingLocation(SourceLocation Loc) const { +// All locations handled by CGDebugInfo are refined locations because this +// is more suitable for debugging than pure spelling or expansion locations. +// Refined locations do not point into macro definitions. If a source +// location is part of a macro argument expansion, its refined location is +// the spelling location of the argument. If a source location is part of a +// macro body, its refined location is the expansion location. +// This allows debuggers to show the macro invocation site or the argument +// site, but not the macro definition body. +SourceLocation +CGDebugInfo::getRefinedSpellingLocation(SourceLocation Loc) const { return CGM.getContext().getSourceManager().getRefinedSpellingLoc(Loc); } From 2855939920782eb422ee56e70979d226b0f89aac Mon Sep 17 00:00:00 2001 From: skill Date: Tue, 14 Oct 2025 16:45:47 +0200 Subject: [PATCH 06/10] Optimize implementation of getRefinedSpellingLocSlowCase. --- clang/lib/Basic/SourceManager.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index d71580763af69..6ce555b2a3d41 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -918,12 +918,13 @@ SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { SourceLocation SourceManager::getRefinedSpellingLocSlowCase(SourceLocation Loc) const { do { - FileIDAndOffset LocInfo = getDecomposedLoc(Loc); - const ExpansionInfo &Expansion = getSLocEntry(LocInfo.first).getExpansion(); - if (Expansion.isMacroArgExpansion()) { - Loc = Expansion.getSpellingLoc().getLocWithOffset(LocInfo.second); + const SLocEntry &E = getSLocEntry(getFileID(Loc)); + const ExpansionInfo &Exp = E.getExpansion(); + if (Exp.isMacroArgExpansion()) { + Loc = Exp.getSpellingLoc().getLocWithOffset(Loc.getOffset() - + E.getOffset()); } else { - Loc = Expansion.getExpansionLocStart(); + Loc = Exp.getExpansionLocStart(); } } while (!Loc.isFileID()); return Loc; From 37bae8cc8aa1dda88ca147f1ae04a4bdb19dbc9f Mon Sep 17 00:00:00 2001 From: skill Date: Tue, 14 Oct 2025 16:56:13 +0200 Subject: [PATCH 07/10] Rename some variables. --- clang/lib/Basic/SourceManager.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 6ce555b2a3d41..6f7a9c4614091 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -918,13 +918,13 @@ SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { SourceLocation SourceManager::getRefinedSpellingLocSlowCase(SourceLocation Loc) const { do { - const SLocEntry &E = getSLocEntry(getFileID(Loc)); - const ExpansionInfo &Exp = E.getExpansion(); - if (Exp.isMacroArgExpansion()) { - Loc = Exp.getSpellingLoc().getLocWithOffset(Loc.getOffset() - - E.getOffset()); + const SLocEntry &Entry = getSLocEntry(getFileID(Loc)); + const ExpansionInfo &ExpInfo = Entry.getExpansion(); + if (ExpInfo.isMacroArgExpansion()) { + Loc = ExpInfo.getSpellingLoc().getLocWithOffset(Loc.getOffset() - + Entry.getOffset()); } else { - Loc = Exp.getExpansionLocStart(); + Loc = ExpInfo.getExpansionLocStart(); } } while (!Loc.isFileID()); return Loc; From b7f92365c8c0cfa05c90b44dbe861e953ddd5ed2 Mon Sep 17 00:00:00 2001 From: SKill <41159490+SergejSalnikov@users.noreply.github.com> Date: Tue, 14 Oct 2025 21:38:17 +0200 Subject: [PATCH 08/10] Update CGDebugInfo.h --- clang/lib/CodeGen/CGDebugInfo.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h index 78c3eb9c5792e..a4162d4140bc3 100644 --- a/clang/lib/CodeGen/CGDebugInfo.h +++ b/clang/lib/CodeGen/CGDebugInfo.h @@ -408,6 +408,8 @@ class CGDebugInfo { /// Create a new lexical block node and push it on the stack. void CreateLexicalBlock(SourceLocation Loc); + SourceLocation getRefinedSpellingLocation(SourceLocation Loc) const; + /// If target-specific LLVM \p AddressSpace directly maps to target-specific /// DWARF address space, appends extended dereferencing mechanism to complex /// expression \p Expr. Otherwise, does nothing. From e065eff74faf34b09576354c2a178d63099ef4b8 Mon Sep 17 00:00:00 2001 From: SKill <41159490+SergejSalnikov@users.noreply.github.com> Date: Tue, 14 Oct 2025 21:49:42 +0200 Subject: [PATCH 09/10] Move method comment to header file. --- clang/lib/CodeGen/CGDebugInfo.cpp | 8 -------- clang/lib/CodeGen/CGDebugInfo.h | 10 ++++++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 690cb2cd3c788..835a9538fa403 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -4954,14 +4954,6 @@ void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { getColumnNumber(CurLoc))); } -// All locations handled by CGDebugInfo are refined locations because this -// is more suitable for debugging than pure spelling or expansion locations. -// Refined locations do not point into macro definitions. If a source -// location is part of a macro argument expansion, its refined location is -// the spelling location of the argument. If a source location is part of a -// macro body, its refined location is the expansion location. -// This allows debuggers to show the macro invocation site or the argument -// site, but not the macro definition body. SourceLocation CGDebugInfo::getRefinedSpellingLocation(SourceLocation Loc) const { return CGM.getContext().getSourceManager().getRefinedSpellingLoc(Loc); diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h index a4162d4140bc3..206f72e6417b1 100644 --- a/clang/lib/CodeGen/CGDebugInfo.h +++ b/clang/lib/CodeGen/CGDebugInfo.h @@ -408,6 +408,16 @@ class CGDebugInfo { /// Create a new lexical block node and push it on the stack. void CreateLexicalBlock(SourceLocation Loc); + /// All locations handled by CGDebugInfo are refined locations because this + /// is more suitable for debugging than pure spelling or expansion locations. + /// + /// Refined locations do not point into macro definitions. If a source + /// location is part of a macro argument expansion, its refined location is + /// the spelling location of the argument. If a source location is part of a + /// macro body, its refined location is the expansion location. + /// + /// This allows debuggers to show the macro invocation site or the argument + /// site, but not the macro definition body. SourceLocation getRefinedSpellingLocation(SourceLocation Loc) const; /// If target-specific LLVM \p AddressSpace directly maps to target-specific From 456ab3e6f86c8088b5010e25aa7c8c55af91b990 Mon Sep 17 00:00:00 2001 From: skill Date: Wed, 15 Oct 2025 12:58:22 +0200 Subject: [PATCH 10/10] Fix code formatting. --- clang/include/clang/Basic/SourceManager.h | 3 ++- clang/lib/CodeGen/CGDebugInfo.cpp | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index 5e8ca172a89c0..48ddfd9d7c609 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -1261,7 +1261,8 @@ class SourceManager : public RefCountedBase { SourceLocation getRefinedSpellingLoc(SourceLocation Loc) const { // Handle the non-mapped case inline, defer to out of line code to handle // expansions. - if (Loc.isFileID()) return Loc; + if (Loc.isFileID()) + return Loc; return getRefinedSpellingLocSlowCase(Loc); } diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 835a9538fa403..913fb7da3bcaa 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -5469,8 +5469,7 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( Ty = CreateSelfType(VD->getType(), Ty); // Get location information. - const unsigned Line = - getLineNumber(Loc.isValid() ? Loc : CurLoc); + const unsigned Line = getLineNumber(Loc.isValid() ? Loc : CurLoc); unsigned Column = getColumnNumber(Loc); const llvm::DataLayout &target = CGM.getDataLayout(); @@ -5579,7 +5578,8 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, const BlockDecl *blockDecl = block.getBlockDecl(); // Collect some general information about the block's location. - SourceLocation loc = getRefinedSpellingLocation(blockDecl->getCaretLocation()); + SourceLocation loc = + getRefinedSpellingLocation(blockDecl->getCaretLocation()); llvm::DIFile *tunit = getOrCreateFile(loc); unsigned line = getLineNumber(loc); unsigned column = getColumnNumber(loc); @@ -6111,8 +6111,8 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { } GV.reset(DBuilder.createGlobalVariableExpression( - DContext, Name, StringRef(), Unit, getLineNumber(Loc), Ty, - true, true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD), + DContext, Name, StringRef(), Unit, getLineNumber(Loc), Ty, true, true, + InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD), TemplateParameters, Align)); } @@ -6131,8 +6131,8 @@ void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var, llvm::DIScope *DContext = getDeclContextDescriptor(D); llvm::DIGlobalVariableExpression *GVE = DBuilder.createGlobalVariableExpression( - DContext, Name, StringRef(), Unit, getLineNumber(Loc), - Ty, false, false, nullptr, nullptr, nullptr, Align); + DContext, Name, StringRef(), Unit, getLineNumber(Loc), Ty, false, + false, nullptr, nullptr, nullptr, Align); Var->addDebugInfo(GVE); } @@ -6228,8 +6228,8 @@ void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, llvm::DIFile *File = getOrCreateFile(Loc); llvm::DIGlobalVariableExpression *Debug = DBuilder.createGlobalVariableExpression( - nullptr, StringRef(), StringRef(), File, - getLineNumber(Loc), getOrCreateType(S->getType(), File), true); + nullptr, StringRef(), StringRef(), File, getLineNumber(Loc), + getOrCreateType(S->getType(), File), true); GV->addDebugInfo(Debug); }