[clang] Emit debug info with original source location for tokens from macros … (PR #163190)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 21 08:56:15 PDT 2025
https://github.com/SergejSalnikov updated https://github.com/llvm/llvm-project/pull/163190
>From aabe21c59d9feca85cc08e22720452970b0700ab Mon Sep 17 00:00:00 2001
From: skill <skill at google.com>
Date: Mon, 13 Oct 2025 14:12:09 +0200
Subject: [PATCH 01/14] 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<Decl>(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<llvm::DIScope>(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 <skill at google.com>
Date: Tue, 14 Oct 2025 12:03:12 +0200
Subject: [PATCH 02/14] 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<SourceManager> {
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<SourceManager> {
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<Decl>(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 <skill at google.com>
Date: Tue, 14 Oct 2025 14:55:29 +0200
Subject: [PATCH 03/14] 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<llvm::DICompositeType>(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<PreferredTypeAttr>())
Ty = BitFieldDecl->getAttr<PreferredTypeAttr>()->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<InjectedClassNameType>(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<llvm::DICompositeType>(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<llvm::DIType *, llvm::DIType *>
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<TypeDecl>(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<CXXMethodDecl>(FD->getCanonicalDecl())) {
- return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
+ SourceLocation Loc = getRefinedSpellingLocation(MD->getLocation());
+ return CreateCXXMemberFunction(MD, getOrCreateFile(Loc),
cast<llvm::DICompositeType>(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<uint64_t> &Expr) const {
std::optional<unsigned> 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<BlocksAttr>())
@@ -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<uint64_t, 13> Expr;
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
@@ -5259,7 +5290,8 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
if (isa<DeclRefExpr>(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<llvm::DIScope>(LexicalBlockStack.back());
// Create the descriptor for the variable.
@@ -5377,11 +5409,12 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
return;
auto *Scope = cast<llvm::DIScope>(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<BlocksAttr>();
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<Decl>(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<Decl>(ID.getDeclContext())),
getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
@@ -6286,7 +6321,7 @@ CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
if (VH)
return cast<llvm::DIImportedEntity>(VH);
llvm::DIImportedEntity *R;
- auto Loc = NA.getLocation();
+ auto Loc = getRefinedSpellingLocation(NA.getLocation());
if (const auto *Underlying =
dyn_cast<NamespaceAliasDecl>(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 <skill at google.com>
Date: Tue, 14 Oct 2025 15:23:33 +0200
Subject: [PATCH 04/14] 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 <skill at google.com>
Date: Tue, 14 Oct 2025 15:54:29 +0200
Subject: [PATCH 05/14] 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 <skill at google.com>
Date: Tue, 14 Oct 2025 16:45:47 +0200
Subject: [PATCH 06/14] 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 <skill at google.com>
Date: Tue, 14 Oct 2025 16:56:13 +0200
Subject: [PATCH 07/14] 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 at users.noreply.github.com>
Date: Tue, 14 Oct 2025 21:38:17 +0200
Subject: [PATCH 08/14] 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 at users.noreply.github.com>
Date: Tue, 14 Oct 2025 21:49:42 +0200
Subject: [PATCH 09/14] 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 <skill at google.com>
Date: Wed, 15 Oct 2025 12:58:22 +0200
Subject: [PATCH 10/14] 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<SourceManager> {
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);
}
>From fc6d2c90233524efb7db171fd090f88cf4b6e324 Mon Sep 17 00:00:00 2001
From: skill <skill at google.com>
Date: Fri, 17 Oct 2025 18:17:17 +0200
Subject: [PATCH 11/14] Reuse SourceManager.getFileLoc method
---
clang/include/clang/Basic/SourceManager.h | 16 ---
clang/lib/Basic/SourceManager.cpp | 13 +--
clang/lib/CodeGen/CGDebugInfo.cpp | 117 +++++++++++-----------
clang/lib/CodeGen/CGDebugInfo.h | 12 +--
4 files changed, 62 insertions(+), 96 deletions(-)
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 48ddfd9d7c609..7bf87f60e11f0 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -1250,22 +1250,6 @@ class SourceManager : public RefCountedBase<SourceManager> {
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.
///
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 6f7a9c4614091..9a59bc62d565f 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -915,8 +915,7 @@ SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
return Loc;
}
-SourceLocation
-SourceManager::getRefinedSpellingLocSlowCase(SourceLocation Loc) const {
+SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
do {
const SLocEntry &Entry = getSLocEntry(getFileID(Loc));
const ExpansionInfo &ExpInfo = Entry.getExpansion();
@@ -930,16 +929,6 @@ SourceManager::getRefinedSpellingLocSlowCase(SourceLocation Loc) const {
return Loc;
}
-SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
- do {
- if (isMacroArgExpansion(Loc))
- Loc = getImmediateSpellingLoc(Loc);
- else
- Loc = getImmediateExpansionRange(Loc).getBegin();
- } while (!Loc.isFileID());
- return Loc;
-}
-
FileIDAndOffset SourceManager::getDecomposedExpansionLocSlowCase(
const SrcMgr::SLocEntry *E) const {
// If this is an expansion record, walk through all the expansion points.
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 913fb7da3bcaa..80f34b18fc32e 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -319,7 +319,7 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
return;
SourceManager &SM = CGM.getContext().getSourceManager();
- CurLoc = SM.getRefinedSpellingLoc(Loc);
+ CurLoc = SM.getFileLoc(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
@@ -1332,7 +1332,7 @@ CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
const RecordDecl *RD = Ty->getOriginalDecl()->getDefinitionOrSelf();
if (llvm::DIType *T = getTypeOrNull(QualType(Ty, 0)))
return cast<llvm::DICompositeType>(T);
- SourceLocation Loc = getRefinedSpellingLocation(RD->getLocation());
+ SourceLocation Loc = getFileLocation(RD->getLocation());
llvm::DIFile *DefUnit = getOrCreateFile(Loc);
const unsigned Line = getLineNumber(Loc.isValid() ? Loc : CurLoc);
StringRef RDName = getClassName(RD);
@@ -1561,7 +1561,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
auto PP = getPrintingPolicy();
Ty->getTemplateName().print(OS, PP, TemplateName::Qualified::None);
- SourceLocation Loc = getRefinedSpellingLocation(AliasDecl->getLocation());
+ SourceLocation Loc = getFileLocation(AliasDecl->getLocation());
if (CGM.getCodeGenOpts().DebugTemplateAlias) {
auto ArgVector = ::GetTemplateArgs(TD, Ty);
@@ -1630,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 = getRefinedSpellingLocation(Ty->getDecl()->getLocation());
+ SourceLocation Loc = getFileLocation(Ty->getDecl()->getLocation());
uint32_t Align = getDeclAlignIfRequired(Ty->getDecl(), CGM.getContext());
// Typedefs are derived from some other type.
@@ -1764,7 +1764,7 @@ CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
QualType Ty = BitFieldDecl->getType();
if (BitFieldDecl->hasAttr<PreferredTypeAttr>())
Ty = BitFieldDecl->getAttr<PreferredTypeAttr>()->getType();
- SourceLocation Loc = getRefinedSpellingLocation(BitFieldDecl->getLocation());
+ SourceLocation Loc = getFileLocation(BitFieldDecl->getLocation());
llvm::DIFile *VUnit = getOrCreateFile(Loc);
llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
@@ -1842,8 +1842,7 @@ llvm::DIDerivedType *CGDebugInfo::createBitFieldSeparatorIfNeeded(
return nullptr;
QualType Ty = PreviousBitfield->getType();
- SourceLocation Loc =
- getRefinedSpellingLocation(PreviousBitfield->getLocation());
+ SourceLocation Loc = getFileLocation(PreviousBitfield->getLocation());
llvm::DIFile *VUnit = getOrCreateFile(Loc);
llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
llvm::DIScope *RecordTy = BitFieldDI->getScope();
@@ -1962,7 +1961,7 @@ void CGDebugInfo::CollectRecordLambdaFields(
continue;
}
- Loc = getRefinedSpellingLocation(Loc);
+ Loc = getFileLocation(Loc);
llvm::DIFile *VUnit = getOrCreateFile(Loc);
elements.push_back(createFieldType(
@@ -1977,7 +1976,7 @@ CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
// Create the descriptor for the static variable, with or without
// constant initializers.
Var = Var->getCanonicalDecl();
- SourceLocation Loc = getRefinedSpellingLocation(Var->getLocation());
+ SourceLocation Loc = getFileLocation(Var->getLocation());
llvm::DIFile *VUnit = getOrCreateFile(Loc);
llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
@@ -2030,9 +2029,8 @@ void CGDebugInfo::CollectRecordNormalField(
auto Align = getDeclAlignIfRequired(field, CGM.getContext());
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(field);
FieldType = createFieldType(
- name, type, getRefinedSpellingLocation(field->getLocation()),
- field->getAccess(), OffsetInBits, Align, tunit, RecordTy, RD,
- Annotations);
+ name, type, getFileLocation(field->getLocation()), field->getAccess(),
+ OffsetInBits, Align, tunit, RecordTy, RD, Annotations);
}
elements.push_back(FieldType);
@@ -2046,7 +2044,7 @@ void CGDebugInfo::CollectRecordNestedType(
// instead?
if (isa<InjectedClassNameType>(Ty))
return;
- SourceLocation Loc = getRefinedSpellingLocation(TD->getLocation());
+ SourceLocation Loc = getFileLocation(TD->getLocation());
if (llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc)))
elements.push_back(nestedType);
}
@@ -2250,7 +2248,7 @@ llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
llvm::DIFile *MethodDefUnit = nullptr;
unsigned MethodLine = 0;
if (!Method->isImplicit()) {
- SourceLocation Loc = getRefinedSpellingLocation(Method->getLocation());
+ SourceLocation Loc = getFileLocation(Method->getLocation());
MethodDefUnit = getOrCreateFile(Loc);
MethodLine = getLineNumber(Loc);
}
@@ -2852,7 +2850,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);
+ Loc = getFileLocation(Loc);
llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
return T;
}
@@ -2866,7 +2864,7 @@ llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
SourceLocation Loc) {
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
assert(!D.isNull() && "null type");
- Loc = getRefinedSpellingLocation(Loc);
+ Loc = getFileLocation(Loc);
llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
assert(T && "could not create debug info for type");
@@ -2884,8 +2882,7 @@ void CGDebugInfo::addHeapAllocSiteMetadata(llvm::CallBase *CI,
if (AllocatedTy->isVoidType())
node = llvm::MDNode::get(CGM.getLLVMContext(), {});
else
- node = getOrCreateType(AllocatedTy,
- getOrCreateFile(getRefinedSpellingLocation(Loc)));
+ node = getOrCreateType(AllocatedTy, getOrCreateFile(getFileLocation(Loc)));
CI->setMetadata("heapallocsite", node);
}
@@ -3118,7 +3115,7 @@ std::pair<llvm::DIType *, llvm::DIType *>
CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
RecordDecl *RD = Ty->getOriginalDecl()->getDefinitionOrSelf();
- SourceLocation Loc = getRefinedSpellingLocation(RD->getLocation());
+ SourceLocation Loc = getFileLocation(RD->getLocation());
// Get overall information about the record type for the debug info.
llvm::DIFile *DefUnit = getOrCreateFile(Loc);
@@ -3188,7 +3185,7 @@ llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
llvm::DIFile *Unit) {
// Ignore protocols.
- SourceLocation Loc = getRefinedSpellingLocation(Ty->getDecl()->getLocation());
+ SourceLocation Loc = getFileLocation(Ty->getDecl()->getLocation());
// Use Typedefs to represent ObjCTypeParamType.
return DBuilder.createTypedef(
@@ -3239,7 +3236,7 @@ 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());
+ SourceLocation Loc = getFileLocation(ID->getLocation());
// Get overall information about the record type for the debug info.
llvm::DIFile *DefUnit = getOrCreateFile(Loc);
unsigned Line = getLineNumber(Loc);
@@ -3363,7 +3360,7 @@ llvm::DIModule *CGDebugInfo::getOrCreateModuleRef(ASTSourceDescriptor Mod,
llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
llvm::DIFile *Unit) {
ObjCInterfaceDecl *ID = Ty->getDecl();
- SourceLocation Loc = getRefinedSpellingLocation(ID->getLocation());
+ SourceLocation Loc = getFileLocation(ID->getLocation());
llvm::DIFile *DefUnit = getOrCreateFile(Loc);
unsigned Line = getLineNumber(Loc);
@@ -3460,7 +3457,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
if (FieldName.empty())
continue;
- SourceLocation Loc = getRefinedSpellingLocation(Field->getLocation());
+ SourceLocation Loc = getFileLocation(Field->getLocation());
// Get the location for the field.
llvm::DIFile *FieldDefUnit = getOrCreateFile(Loc);
unsigned FieldLine = getLineNumber(Loc);
@@ -3508,7 +3505,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
if (ObjCPropertyImplDecl *PImpD =
ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
- SourceLocation Loc = getRefinedSpellingLocation(PD->getLocation());
+ SourceLocation Loc = getFileLocation(PD->getLocation());
llvm::DIFile *PUnit = getOrCreateFile(Loc);
unsigned PLine = getLineNumber(Loc);
ObjCMethodDecl *Getter = PImpD->getGetterMethodDecl();
@@ -3790,7 +3787,7 @@ llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
// FwdDecl with the second and then replace the second with
// complete type.
llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
- SourceLocation Loc = getRefinedSpellingLocation(ED->getLocation());
+ SourceLocation Loc = getFileLocation(ED->getLocation());
llvm::DIFile *DefUnit = getOrCreateFile(Loc);
llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
@@ -3828,7 +3825,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
// Return a CompositeType for the enum itself.
llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
- SourceLocation Loc = getRefinedSpellingLocation(ED->getLocation());
+ SourceLocation Loc = getFileLocation(ED->getLocation());
llvm::DIFile *DefUnit = getOrCreateFile(Loc);
unsigned Line = getLineNumber(Loc);
llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
@@ -3848,10 +3845,9 @@ llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
SourceLocation LineLoc,
SourceLocation FileLoc) {
- llvm::DIFile *FName = getOrCreateFile(getRefinedSpellingLocation(FileLoc));
- unsigned Line = LineLoc.isInvalid()
- ? 0
- : getLineNumber(getRefinedSpellingLocation(LineLoc));
+ llvm::DIFile *FName = getOrCreateFile(getFileLocation(FileLoc));
+ unsigned Line =
+ LineLoc.isInvalid() ? 0 : getLineNumber(getFileLocation(LineLoc));
return DBuilder.createTempMacroFile(Parent, Line, FName);
}
@@ -4174,7 +4170,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 = getRefinedSpellingLocation(RD->getLocation());
+ const SourceLocation Loc = getFileLocation(RD->getLocation());
llvm::DIFile *DefUnit = nullptr;
unsigned Line = 0;
if (Loc.isValid()) {
@@ -4289,7 +4285,7 @@ void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
break;
}
CanQualType T = CGM.getContext().getCanonicalTagType(PBase);
- SourceLocation Loc = getRefinedSpellingLocation(RD->getLocation());
+ SourceLocation Loc = getFileLocation(RD->getLocation());
ContainingType = getOrCreateType(T, getOrCreateFile(Loc));
} else if (RD->isDynamicClass())
ContainingType = RealDecl;
@@ -4361,7 +4357,7 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
StringRef &Name, StringRef &LinkageName,
llvm::MDTuple *&TemplateParameters,
llvm::DIScope *&VDContext) {
- SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation());
+ SourceLocation Loc = getFileLocation(VD->getLocation());
Unit = getOrCreateFile(Loc);
LineNo = getLineNumber(Loc);
@@ -4418,7 +4414,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 = getRefinedSpellingLocation(GD.getDecl()->getLocation());
+ SourceLocation Loc = getFileLocation(GD.getDecl()->getLocation());
llvm::DIFile *Unit = getOrCreateFile(Loc);
llvm::DIScope *DContext = Unit;
unsigned Line = getLineNumber(Loc);
@@ -4473,7 +4469,7 @@ llvm::DIGlobalVariable *
CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
QualType T;
StringRef Name, LinkageName;
- SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation());
+ SourceLocation Loc = getFileLocation(VD->getLocation());
llvm::DIFile *Unit = getOrCreateFile(Loc);
llvm::DIScope *DContext = Unit;
unsigned Line = getLineNumber(Loc);
@@ -4499,7 +4495,7 @@ llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
// in unlimited debug info)
if (const auto *TD = dyn_cast<TypeDecl>(D)) {
QualType Ty = CGM.getContext().getTypeDeclType(TD);
- SourceLocation Loc = getRefinedSpellingLocation(TD->getLocation());
+ SourceLocation Loc = getFileLocation(TD->getLocation());
return getOrCreateType(Ty, getOrCreateFile(Loc));
}
auto I = DeclCache.find(D->getCanonicalDecl());
@@ -4546,7 +4542,7 @@ llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
auto MI = SPCache.find(FD->getCanonicalDecl());
if (MI == SPCache.end()) {
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
- SourceLocation Loc = getRefinedSpellingLocation(MD->getLocation());
+ SourceLocation Loc = getFileLocation(MD->getLocation());
return CreateCXXMemberFunction(MD, getOrCreateFile(Loc),
cast<llvm::DICompositeType>(S));
}
@@ -4709,7 +4705,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
const Decl *D = GD.getDecl();
bool HasDecl = (D != nullptr);
- Loc = getRefinedSpellingLocation(Loc);
+ Loc = getFileLocation(Loc);
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
@@ -4778,7 +4774,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
SPFlags | llvm::DISubprogram::SPFlagDefinition;
const unsigned LineNo = getLineNumber(Loc.isValid() ? Loc : CurLoc);
- unsigned ScopeLine = getLineNumber(getRefinedSpellingLocation(ScopeLoc));
+ unsigned ScopeLine = getLineNumber(getFileLocation(ScopeLoc));
llvm::DISubroutineType *DIFnType = getOrCreateFunctionType(D, FnType, Unit);
llvm::DISubprogram *Decl = nullptr;
llvm::DINodeArray Annotations = nullptr;
@@ -4826,7 +4822,8 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
return GetName(D, true);
});
- Loc = getRefinedSpellingLocation(Loc);
+ Loc = getFileLocation(Loc);
+
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
llvm::DIFile *Unit = getOrCreateFile(Loc);
bool IsDeclForCallSite = Fn ? true : false;
@@ -4936,7 +4933,7 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
// Update our current location
setLocation(Loc);
- if (CurLoc.isInvalid() || CurLoc.isMacroID() || LexicalBlockStack.empty())
+ if (CurLoc.isInvalid() || LexicalBlockStack.empty())
return;
llvm::MDNode *Scope = LexicalBlockStack.back();
@@ -4954,9 +4951,8 @@ void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
getColumnNumber(CurLoc)));
}
-SourceLocation
-CGDebugInfo::getRefinedSpellingLocation(SourceLocation Loc) const {
- return CGM.getContext().getSourceManager().getRefinedSpellingLoc(Loc);
+SourceLocation CGDebugInfo::getFileLocation(SourceLocation Loc) const {
+ return CGM.getContext().getSourceManager().getFileLoc(Loc);
}
void CGDebugInfo::AppendAddressSpaceXDeref(
@@ -4975,7 +4971,7 @@ void CGDebugInfo::AppendAddressSpaceXDeref(
void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
SourceLocation Loc) {
// Set our current location.
- Loc = getRefinedSpellingLocation(Loc);
+ Loc = getFileLocation(Loc);
setLocation(Loc);
// Emit a line table change for the current location inside the new scope.
@@ -5028,7 +5024,7 @@ CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
uint64_t FieldSize, FieldOffset;
uint32_t FieldAlign;
- SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation());
+ SourceLocation Loc = getFileLocation(VD->getLocation());
llvm::DIFile *Unit = getOrCreateFile(Loc);
QualType Type = VD->getType();
@@ -5105,7 +5101,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
const bool VarIsArtificial = IsArtificial(VD);
llvm::DIFile *Unit = nullptr;
- SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation());
+ SourceLocation Loc = getFileLocation(VD->getLocation());
if (!VarIsArtificial)
Unit = getOrCreateFile(Loc);
llvm::DIType *Ty;
@@ -5291,7 +5287,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
if (isa<DeclRefExpr>(BD->getBinding()))
return nullptr;
- SourceLocation Loc = getRefinedSpellingLocation(BD->getLocation());
+ SourceLocation Loc = getFileLocation(BD->getLocation());
llvm::DIFile *Unit = getOrCreateFile(Loc);
llvm::DIType *Ty = getOrCreateType(BD->getType(), Unit);
@@ -5410,7 +5406,7 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
return;
auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
- SourceLocation Loc = getRefinedSpellingLocation(D->getLocation());
+ SourceLocation Loc = getFileLocation(D->getLocation());
llvm::DIFile *Unit = getOrCreateFile(Loc);
// Get location information.
@@ -5454,7 +5450,7 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
bool isByRef = VD->hasAttr<BlocksAttr>();
uint64_t XOffset = 0;
- SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation());
+ SourceLocation Loc = getFileLocation(VD->getLocation());
llvm::DIFile *Unit = getOrCreateFile(Loc);
llvm::DIType *Ty;
if (isByRef)
@@ -5578,8 +5574,7 @@ 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 = getFileLocation(blockDecl->getCaretLocation());
llvm::DIFile *tunit = getOrCreateFile(loc);
unsigned line = getLineNumber(loc);
unsigned column = getColumnNumber(loc);
@@ -6051,7 +6046,7 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
});
auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
- SourceLocation Loc = getRefinedSpellingLocation(VD->getLocation());
+ SourceLocation Loc = getFileLocation(VD->getLocation());
// Create the descriptor for the variable.
llvm::DIFile *Unit = getOrCreateFile(Loc);
StringRef Name = VD->getName();
@@ -6123,7 +6118,7 @@ void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
return;
auto Align = getDeclAlignIfRequired(D, CGM.getContext());
- SourceLocation Loc = getRefinedSpellingLocation(D->getLocation());
+ SourceLocation Loc = getFileLocation(D->getLocation());
llvm::DIFile *Unit = getOrCreateFile(Loc);
StringRef Name = D->getName();
llvm::DIType *Ty = getOrCreateType(D->getType(), Unit);
@@ -6209,7 +6204,7 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
return;
llvm::DIScope *DContext = getDeclContextDescriptor(D);
- SourceLocation Loc = getRefinedSpellingLocation(D->getLocation());
+ SourceLocation Loc = getFileLocation(D->getLocation());
llvm::DIImportedEntity *ImportDI = DBuilder.createImportedDeclaration(
DContext, DI, getOrCreateFile(Loc), getLineNumber(Loc), D->getName());
@@ -6220,7 +6215,7 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
const StringLiteral *S) {
- SourceLocation Loc = getRefinedSpellingLocation(S->getStrTokenLoc(0));
+ SourceLocation Loc = getFileLocation(S->getStrTokenLoc(0));
PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
if (!PLoc.isValid())
return;
@@ -6246,7 +6241,7 @@ void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
if (!NSDecl->isAnonymousNamespace() ||
CGM.getCodeGenOpts().DebugExplicitImport) {
- SourceLocation Loc = getRefinedSpellingLocation(UD.getLocation());
+ SourceLocation Loc = getFileLocation(UD.getLocation());
if (!Loc.isValid())
Loc = CurLoc;
DBuilder.createImportedModule(
@@ -6258,7 +6253,7 @@ void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
void CGDebugInfo::EmitUsingShadowDecl(const UsingShadowDecl &USD) {
if (llvm::DINode *Target =
getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
- SourceLocation Loc = getRefinedSpellingLocation(USD.getLocation());
+ SourceLocation Loc = getFileLocation(USD.getLocation());
DBuilder.createImportedDeclaration(
getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
getOrCreateFile(Loc), getLineNumber(Loc));
@@ -6306,7 +6301,7 @@ void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) {
return;
if (Module *M = ID.getImportedModule()) {
auto Info = ASTSourceDescriptor(*M);
- auto Loc = getRefinedSpellingLocation(ID.getLocation());
+ auto Loc = getFileLocation(ID.getLocation());
DBuilder.createImportedDeclaration(
getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
@@ -6322,7 +6317,7 @@ CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
if (VH)
return cast<llvm::DIImportedEntity>(VH);
llvm::DIImportedEntity *R;
- auto Loc = getRefinedSpellingLocation(NA.getLocation());
+ auto Loc = getFileLocation(NA.getLocation());
if (const auto *Underlying =
dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
// This could cache & dedup here rather than relying on metadata deduping.
@@ -6454,7 +6449,7 @@ llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
if (LexicalBlockStack.empty())
return llvm::DebugLoc();
- Loc = getRefinedSpellingLocation(Loc);
+ Loc = getFileLocation(Loc);
llvm::MDNode *Scope = LexicalBlockStack.back();
return llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(Loc),
getColumnNumber(Loc), Scope);
diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index 206f72e6417b1..07e1fbc485292 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -408,17 +408,15 @@ 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
+ /// All locations handled by CGDebugInfo are file 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
+ /// File locations do not point into macro definitions. If a source
+ /// location is part of a macro argument expansion, its file 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.
- ///
+ /// macro body, its file 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;
+ SourceLocation getFileLocation(SourceLocation Loc) const;
/// If target-specific LLVM \p AddressSpace directly maps to target-specific
/// DWARF address space, appends extended dereferencing mechanism to complex
>From 4d99c5f27414586ddd79f3851afb9ed6473caa8e Mon Sep 17 00:00:00 2001
From: SKill <41159490+SergejSalnikov at users.noreply.github.com>
Date: Fri, 17 Oct 2025 18:20:00 +0200
Subject: [PATCH 12/14] Remove non-existing method from SourceManager.h
---
clang/include/clang/Basic/SourceManager.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 7bf87f60e11f0..ed967fd47dc83 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -1977,7 +1977,6 @@ class SourceManager : public RefCountedBase<SourceManager> {
SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
- SourceLocation getRefinedSpellingLocSlowCase(SourceLocation Loc) const;
SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
FileIDAndOffset
>From 9cadca027782974ce53bf3ff52f488a8a2da5bc9 Mon Sep 17 00:00:00 2001
From: skill <skill at google.com>
Date: Tue, 21 Oct 2025 12:03:02 +0200
Subject: [PATCH 13/14] Use PresumedLoc instead of SourceLocation in
CGDebugInfo.
---
clang/lib/CodeGen/CGDebugInfo.cpp | 391 +++++++++++++++---------------
clang/lib/CodeGen/CGDebugInfo.h | 45 ++--
2 files changed, 220 insertions(+), 216 deletions(-)
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 80f34b18fc32e..718a93b54aefb 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -219,17 +219,18 @@ ApplyAtomGroup::~ApplyAtomGroup() {
ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
SourceLocation TemporaryLocation)
: CGF(&CGF) {
- init(TemporaryLocation);
+ SourceManager &SM = CGF.getContext().getSourceManager();
+ init(SM.getPresumedLoc(SM.getFileLoc(TemporaryLocation)));
}
ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
bool DefaultToEmpty,
- SourceLocation TemporaryLocation)
+ const PresumedLoc &TemporaryPLocation)
: CGF(&CGF) {
- init(TemporaryLocation, DefaultToEmpty);
+ init(TemporaryPLocation, DefaultToEmpty);
}
-void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
+void ApplyDebugLocation::init(const PresumedLoc &TemporaryPLocation,
bool DefaultToEmpty) {
auto *DI = CGF->getDebugInfo();
if (!DI) {
@@ -242,8 +243,8 @@ void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
if (OriginalLocation && !DI->CGM.getExpressionLocationsEnabled())
return;
- if (TemporaryLocation.isValid()) {
- DI->EmitLocation(CGF->Builder, TemporaryLocation);
+ if (TemporaryPLocation.isValid()) {
+ DI->EmitLocation(CGF->Builder, TemporaryPLocation);
return;
}
@@ -261,7 +262,8 @@ void ApplyDebugLocation::init(SourceLocation TemporaryLocation,
ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
: CGF(&CGF) {
- init(E->getExprLoc());
+ SourceManager &SM = CGF.getContext().getSourceManager();
+ init(SM.getPresumedLoc(SM.getFileLoc(E->getExprLoc())));
}
ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc)
@@ -313,13 +315,12 @@ ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
DI.EmitLocation(CGF->Builder, SavedLocation);
}
-void CGDebugInfo::setLocation(SourceLocation Loc) {
+void CGDebugInfo::setLocation(const PresumedLoc &PLoc) {
// If the new location isn't valid return.
- if (Loc.isInvalid())
+ if (PLoc.isInvalid())
return;
- SourceManager &SM = CGM.getContext().getSourceManager();
- CurLoc = SM.getFileLoc(Loc);
+ CurPLoc = PLoc;
// 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
@@ -328,19 +329,18 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
return;
auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
- PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
- if (PCLoc.isInvalid() || Scope->getFile() == getOrCreateFile(CurLoc))
+ if (CurPLoc.isInvalid() || Scope->getFile() == getOrCreateFile(CurPLoc))
return;
if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
LexicalBlockStack.pop_back();
LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
- LBF->getScope(), getOrCreateFile(CurLoc)));
+ LBF->getScope(), getOrCreateFile(CurPLoc)));
} else if (isa<llvm::DILexicalBlock>(Scope) ||
isa<llvm::DISubprogram>(Scope)) {
LexicalBlockStack.pop_back();
LexicalBlockStack.emplace_back(
- DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
+ DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurPLoc)));
}
}
@@ -532,21 +532,18 @@ std::optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
return Source;
}
-llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
- SourceManager &SM = CGM.getContext().getSourceManager();
+llvm::DIFile *CGDebugInfo::getOrCreateFile(const PresumedLoc &PLoc) {
StringRef FileName;
FileID FID;
std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo;
- if (Loc.isInvalid()) {
+ if (PLoc.isInvalid()) {
// The DIFile used by the CU is distinct from the main source file. Call
// createFile() below for canonicalization if the source file was specified
// with an absolute path.
FileName = TheCU->getFile()->getFilename();
CSInfo = TheCU->getFile()->getChecksum();
} else {
- assert(Loc.isFileID());
- PresumedLoc PLoc = SM.getPresumedLoc(Loc);
FileName = PLoc.getFilename();
if (FileName.empty()) {
@@ -573,7 +570,8 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
if (CSKind)
CSInfo.emplace(*CSKind, Checksum);
}
- return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc)));
+ SourceManager &SM = CGM.getContext().getSourceManager();
+ return createFile(FileName, CSInfo, getSource(SM, PLoc.getFileID()));
}
llvm::DIFile *CGDebugInfo::createFile(
@@ -624,25 +622,20 @@ std::string CGDebugInfo::remapDIPath(StringRef Path) const {
return P.str().str();
}
-unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
- if (Loc.isInvalid())
+unsigned CGDebugInfo::getLineNumber(const PresumedLoc &PLoc) const {
+ if (PLoc.isInvalid())
return 0;
- SourceManager &SM = CGM.getContext().getSourceManager();
- assert(Loc.isFileID());
- return SM.getPresumedLoc(Loc).getLine();
+ return PLoc.getLine();
}
-unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
+unsigned CGDebugInfo::getColumnNumber(const PresumedLoc & PLoc, bool Force) const {
// We may not want column information at all.
if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
return 0;
// If the location is invalid then use the current column.
- if (Loc.isInvalid() && CurLoc.isInvalid())
+ if (PLoc.isInvalid() && CurPLoc.isInvalid())
return 0;
- SourceManager &SM = CGM.getContext().getSourceManager();
- assert(Loc.isFileID());
- PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
return PLoc.isValid() ? PLoc.getColumn() : 0;
}
@@ -1332,9 +1325,9 @@ CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
const RecordDecl *RD = Ty->getOriginalDecl()->getDefinitionOrSelf();
if (llvm::DIType *T = getTypeOrNull(QualType(Ty, 0)))
return cast<llvm::DICompositeType>(T);
- SourceLocation Loc = getFileLocation(RD->getLocation());
- llvm::DIFile *DefUnit = getOrCreateFile(Loc);
- const unsigned Line = getLineNumber(Loc.isValid() ? Loc : CurLoc);
+ PresumedLoc PLoc = getFileLocation(RD->getLocation());
+ llvm::DIFile *DefUnit = getOrCreateFile(PLoc);
+ const unsigned Line = getLineNumber(PLoc.isValid() ? PLoc : CurPLoc);
StringRef RDName = getClassName(RD);
uint64_t Size = 0;
@@ -1561,7 +1554,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
auto PP = getPrintingPolicy();
Ty->getTemplateName().print(OS, PP, TemplateName::Qualified::None);
- SourceLocation Loc = getFileLocation(AliasDecl->getLocation());
+ PresumedLoc PLoc = getFileLocation(AliasDecl->getLocation());
if (CGM.getCodeGenOpts().DebugTemplateAlias) {
auto ArgVector = ::GetTemplateArgs(TD, Ty);
@@ -1581,15 +1574,15 @@ llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
printTemplateArgumentList(OS, Args.Args, PP);
llvm::DIDerivedType *AliasTy = DBuilder.createTemplateAlias(
- Src, Name, getOrCreateFile(Loc), getLineNumber(Loc),
+ Src, Name, getOrCreateFile(PLoc), getLineNumber(PLoc),
getDeclContextDescriptor(AliasDecl), CollectTemplateParams(Args, Unit));
return AliasTy;
}
printTemplateArgumentList(OS, Ty->template_arguments(), PP,
TD->getTemplateParameters());
- return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
- getLineNumber(Loc),
+ return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(PLoc),
+ getLineNumber(PLoc),
getDeclContextDescriptor(AliasDecl));
}
@@ -1630,7 +1623,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
// We don't set size information, but do specify where the typedef was
// declared.
- SourceLocation Loc = getFileLocation(Ty->getDecl()->getLocation());
+ PresumedLoc PLoc = getFileLocation(Ty->getDecl()->getLocation());
uint32_t Align = getDeclAlignIfRequired(Ty->getDecl(), CGM.getContext());
// Typedefs are derived from some other type.
@@ -1642,7 +1635,7 @@ llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
Flags = getAccessFlag(Ty->getDecl()->getAccess(), cast<RecordDecl>(DC));
return DBuilder.createTypedef(Underlying, Ty->getDecl()->getName(),
- getOrCreateFile(Loc), getLineNumber(Loc),
+ getOrCreateFile(PLoc), getLineNumber(PLoc),
getDeclContextDescriptor(Ty->getDecl()), Align,
Flags, Annotations);
}
@@ -1764,13 +1757,13 @@ CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl,
QualType Ty = BitFieldDecl->getType();
if (BitFieldDecl->hasAttr<PreferredTypeAttr>())
Ty = BitFieldDecl->getAttr<PreferredTypeAttr>()->getType();
- SourceLocation Loc = getFileLocation(BitFieldDecl->getLocation());
- llvm::DIFile *VUnit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(BitFieldDecl->getLocation());
+ llvm::DIFile *VUnit = getOrCreateFile(PLoc);
llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
// Get the location for the field.
- llvm::DIFile *File = getOrCreateFile(Loc);
- unsigned Line = getLineNumber(Loc);
+ llvm::DIFile *File = getOrCreateFile(PLoc);
+ unsigned Line = getLineNumber(PLoc);
const CGBitFieldInfo &BitFieldInfo =
CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl);
@@ -1842,13 +1835,13 @@ llvm::DIDerivedType *CGDebugInfo::createBitFieldSeparatorIfNeeded(
return nullptr;
QualType Ty = PreviousBitfield->getType();
- SourceLocation Loc = getFileLocation(PreviousBitfield->getLocation());
- llvm::DIFile *VUnit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(PreviousBitfield->getLocation());
+ llvm::DIFile *VUnit = getOrCreateFile(PLoc);
llvm::DIType *DebugType = getOrCreateType(Ty, VUnit);
llvm::DIScope *RecordTy = BitFieldDI->getScope();
- llvm::DIFile *File = getOrCreateFile(Loc);
- unsigned Line = getLineNumber(Loc);
+ llvm::DIFile *File = getOrCreateFile(PLoc);
+ unsigned Line = getLineNumber(PLoc);
uint64_t StorageOffsetInBits =
cast<llvm::ConstantInt>(BitFieldDI->getStorageOffsetInBits())
@@ -1864,14 +1857,14 @@ llvm::DIDerivedType *CGDebugInfo::createBitFieldSeparatorIfNeeded(
}
llvm::DIType *CGDebugInfo::createFieldType(
- StringRef name, QualType type, SourceLocation loc, AccessSpecifier AS,
+ StringRef name, QualType type, const PresumedLoc &PLoc, AccessSpecifier AS,
uint64_t offsetInBits, uint32_t AlignInBits, llvm::DIFile *tunit,
llvm::DIScope *scope, const RecordDecl *RD, llvm::DINodeArray Annotations) {
llvm::DIType *debugType = getOrCreateType(type, tunit);
// Get the location for the field.
- llvm::DIFile *file = getOrCreateFile(loc);
- const unsigned line = getLineNumber(loc.isValid() ? loc : CurLoc);
+ llvm::DIFile *file = getOrCreateFile(PLoc);
+ const unsigned line = getLineNumber(PLoc.isValid() ? PLoc : CurPLoc);
uint64_t SizeInBits = 0;
auto Align = AlignInBits;
@@ -1961,11 +1954,11 @@ void CGDebugInfo::CollectRecordLambdaFields(
continue;
}
- Loc = getFileLocation(Loc);
- llvm::DIFile *VUnit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(Loc);
+ llvm::DIFile *VUnit = getOrCreateFile(PLoc);
elements.push_back(createFieldType(
- GetLambdaCaptureName(Capture), Field->getType(), Loc,
+ GetLambdaCaptureName(Capture), Field->getType(), PLoc,
Field->getAccess(), FieldOffset, Align, VUnit, RecordTy, CXXDecl));
}
}
@@ -1976,11 +1969,11 @@ CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy,
// Create the descriptor for the static variable, with or without
// constant initializers.
Var = Var->getCanonicalDecl();
- SourceLocation Loc = getFileLocation(Var->getLocation());
- llvm::DIFile *VUnit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(Var->getLocation());
+ llvm::DIFile *VUnit = getOrCreateFile(PLoc);
llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
- unsigned LineNumber = getLineNumber(Loc);
+ unsigned LineNumber = getLineNumber(PLoc);
StringRef VName = Var->getName();
// FIXME: to avoid complications with type merging we should
@@ -2044,8 +2037,8 @@ void CGDebugInfo::CollectRecordNestedType(
// instead?
if (isa<InjectedClassNameType>(Ty))
return;
- SourceLocation Loc = getFileLocation(TD->getLocation());
- if (llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc)))
+ PresumedLoc PLoc = getFileLocation(TD->getLocation());
+ if (llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(PLoc)))
elements.push_back(nestedType);
}
@@ -2248,9 +2241,9 @@ llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
llvm::DIFile *MethodDefUnit = nullptr;
unsigned MethodLine = 0;
if (!Method->isImplicit()) {
- SourceLocation Loc = getFileLocation(Method->getLocation());
- MethodDefUnit = getOrCreateFile(Loc);
- MethodLine = getLineNumber(Loc);
+ PresumedLoc PLoc = getFileLocation(Method->getLocation());
+ MethodDefUnit = getOrCreateFile(PLoc);
+ MethodLine = getLineNumber(PLoc);
}
// Collect virtual method info.
@@ -2701,7 +2694,6 @@ void CGDebugInfo::emitVTableSymbol(llvm::GlobalVariable *VTable,
ASTContext &Context = CGM.getContext();
StringRef SymbolName = "_vtable$";
- SourceLocation InvalidLoc;
QualType VoidPtr = Context.getPointerType(Context.VoidTy);
// We deal with two different contexts:
@@ -2713,7 +2705,8 @@ void CGDebugInfo::emitVTableSymbol(llvm::GlobalVariable *VTable,
// placed inside the scope of the C++ class/structure.
llvm::DIScope *DContext = getContextDescriptor(RD, TheCU);
auto *Ctxt = cast<llvm::DICompositeType>(DContext);
- llvm::DIFile *Unit = getOrCreateFile(InvalidLoc);
+ PresumedLoc InvalidPLoc = PresumedLoc();
+ llvm::DIFile *Unit = getOrCreateFile(InvalidPLoc);
llvm::DIType *VTy = getOrCreateType(VoidPtr, Unit);
llvm::DINode::DIFlags Flags = getAccessFlag(AccessSpecifier::AS_private, RD) |
llvm::DINode::FlagArtificial;
@@ -2850,8 +2843,8 @@ void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit,
llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
SourceLocation Loc) {
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
- Loc = getFileLocation(Loc);
- llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
+ PresumedLoc PLoc = getFileLocation(Loc);
+ llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(PLoc));
return T;
}
@@ -2864,8 +2857,8 @@ llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
SourceLocation Loc) {
assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
assert(!D.isNull() && "null type");
- Loc = getFileLocation(Loc);
- llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
+ PresumedLoc PLoc = getFileLocation(Loc);
+ llvm::DIType *T = getOrCreateType(D, getOrCreateFile(PLoc));
assert(T && "could not create debug info for type");
RetainedTypes.push_back(D.getAsOpaquePtr());
@@ -3115,9 +3108,9 @@ std::pair<llvm::DIType *, llvm::DIType *>
CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) {
RecordDecl *RD = Ty->getOriginalDecl()->getDefinitionOrSelf();
- SourceLocation Loc = getFileLocation(RD->getLocation());
+ PresumedLoc PLoc = getFileLocation(RD->getLocation());
// Get overall information about the record type for the debug info.
- llvm::DIFile *DefUnit = getOrCreateFile(Loc);
+ llvm::DIFile *DefUnit = getOrCreateFile(PLoc);
// 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.
@@ -3185,12 +3178,12 @@ llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty,
llvm::DIFile *Unit) {
// Ignore protocols.
- SourceLocation Loc = getFileLocation(Ty->getDecl()->getLocation());
+ PresumedLoc PLoc = getFileLocation(Ty->getDecl()->getLocation());
// Use Typedefs to represent ObjCTypeParamType.
return DBuilder.createTypedef(
getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
- Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
+ Ty->getDecl()->getName(), getOrCreateFile(PLoc), getLineNumber(PLoc),
getDeclContextDescriptor(Ty->getDecl()));
}
@@ -3236,10 +3229,10 @@ llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
llvm::dwarf::DW_TAG_structure_type, ID->getName(),
getDeclContextDescriptor(ID), Unit, 0, RuntimeLang);
- SourceLocation Loc = getFileLocation(ID->getLocation());
+ PresumedLoc PLoc = getFileLocation(ID->getLocation());
// Get overall information about the record type for the debug info.
- llvm::DIFile *DefUnit = getOrCreateFile(Loc);
- unsigned Line = getLineNumber(Loc);
+ llvm::DIFile *DefUnit = getOrCreateFile(PLoc);
+ unsigned Line = getLineNumber(PLoc);
// 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.
@@ -3360,9 +3353,9 @@ llvm::DIModule *CGDebugInfo::getOrCreateModuleRef(ASTSourceDescriptor Mod,
llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
llvm::DIFile *Unit) {
ObjCInterfaceDecl *ID = Ty->getDecl();
- SourceLocation Loc = getFileLocation(ID->getLocation());
- llvm::DIFile *DefUnit = getOrCreateFile(Loc);
- unsigned Line = getLineNumber(Loc);
+ PresumedLoc PLoc = getFileLocation(ID->getLocation());
+ llvm::DIFile *DefUnit = getOrCreateFile(PLoc);
+ unsigned Line = getLineNumber(PLoc);
unsigned RuntimeLang = TheCU->getSourceLanguage().getUnversionedName();
@@ -3403,9 +3396,9 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
// Create entries for all of the properties.
auto AddProperty = [&](const ObjCPropertyDecl *PD) {
- SourceLocation Loc = PD->getLocation();
- llvm::DIFile *PUnit = getOrCreateFile(Loc);
- unsigned PLine = getLineNumber(Loc);
+ PresumedLoc PLoc = getFileLocation(PD->getLocation());
+ llvm::DIFile *PUnit = getOrCreateFile(PLoc);
+ unsigned PLine = getLineNumber(PLoc);
ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
llvm::MDNode *PropertyNode = DBuilder.createObjCProperty(
@@ -3457,10 +3450,10 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
if (FieldName.empty())
continue;
- SourceLocation Loc = getFileLocation(Field->getLocation());
+ PresumedLoc PLoc = getFileLocation(Field->getLocation());
// Get the location for the field.
- llvm::DIFile *FieldDefUnit = getOrCreateFile(Loc);
- unsigned FieldLine = getLineNumber(Loc);
+ llvm::DIFile *FieldDefUnit = getOrCreateFile(PLoc);
+ unsigned FieldLine = getLineNumber(PLoc);
QualType FType = Field->getType();
uint64_t FieldSize = 0;
uint32_t FieldAlign = 0;
@@ -3505,9 +3498,9 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
if (ObjCPropertyImplDecl *PImpD =
ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
- SourceLocation Loc = getFileLocation(PD->getLocation());
- llvm::DIFile *PUnit = getOrCreateFile(Loc);
- unsigned PLine = getLineNumber(Loc);
+ PresumedLoc PLoc = getFileLocation(PD->getLocation());
+ llvm::DIFile *PUnit = getOrCreateFile(PLoc);
+ unsigned PLine = getLineNumber(PLoc);
ObjCMethodDecl *Getter = PImpD->getGetterMethodDecl();
ObjCMethodDecl *Setter = PImpD->getSetterMethodDecl();
PropertyNode = DBuilder.createObjCProperty(
@@ -3787,12 +3780,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);
- SourceLocation Loc = getFileLocation(ED->getLocation());
- llvm::DIFile *DefUnit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(ED->getLocation());
+ llvm::DIFile *DefUnit = getOrCreateFile(PLoc);
llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
- unsigned Line = getLineNumber(Loc);
+ unsigned Line = getLineNumber(PLoc);
StringRef EDName = ED->getName();
llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
@@ -3825,9 +3818,9 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
// Return a CompositeType for the enum itself.
llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators);
- SourceLocation Loc = getFileLocation(ED->getLocation());
- llvm::DIFile *DefUnit = getOrCreateFile(Loc);
- unsigned Line = getLineNumber(Loc);
+ PresumedLoc PLoc = getFileLocation(ED->getLocation());
+ llvm::DIFile *DefUnit = getOrCreateFile(PLoc);
+ unsigned Line = getLineNumber(PLoc);
llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);
return DBuilder.createEnumerationType(
@@ -3838,7 +3831,8 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
unsigned MType, SourceLocation LineLoc,
StringRef Name, StringRef Value) {
- unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
+ unsigned Line =
+ LineLoc.isInvalid() ? 0 : getLineNumber(getFileLocation(LineLoc));
return DBuilder.createMacro(Parent, Line, MType, Name, Value);
}
@@ -4170,12 +4164,12 @@ 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 = getFileLocation(RD->getLocation());
+ PresumedLoc PLoc = getFileLocation(RD->getLocation());
llvm::DIFile *DefUnit = nullptr;
unsigned Line = 0;
- if (Loc.isValid()) {
- DefUnit = getOrCreateFile(Loc);
- Line = getLineNumber(Loc);
+ if (PLoc.isValid()) {
+ DefUnit = getOrCreateFile(PLoc);
+ Line = getLineNumber(PLoc);
}
llvm::DIScope *RDContext = getDeclContextDescriptor(RD);
@@ -4285,8 +4279,8 @@ void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD,
break;
}
CanQualType T = CGM.getContext().getCanonicalTagType(PBase);
- SourceLocation Loc = getFileLocation(RD->getLocation());
- ContainingType = getOrCreateType(T, getOrCreateFile(Loc));
+ PresumedLoc PLoc = getFileLocation(RD->getLocation());
+ ContainingType = getOrCreateType(T, getOrCreateFile(PLoc));
} else if (RD->isDynamicClass())
ContainingType = RealDecl;
@@ -4357,11 +4351,11 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
StringRef &Name, StringRef &LinkageName,
llvm::MDTuple *&TemplateParameters,
llvm::DIScope *&VDContext) {
- SourceLocation Loc = getFileLocation(VD->getLocation());
- Unit = getOrCreateFile(Loc);
- LineNo = getLineNumber(Loc);
+ PresumedLoc PLoc = getFileLocation(VD->getLocation());
+ Unit = getOrCreateFile(PLoc);
+ LineNo = getLineNumber(PLoc);
- setLocation(Loc);
+ setLocation(PLoc);
T = VD->getType();
if (T->isIncompleteArrayType()) {
@@ -4414,10 +4408,10 @@ 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 = getFileLocation(GD.getDecl()->getLocation());
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(GD.getDecl()->getLocation());
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
llvm::DIScope *DContext = Unit;
- unsigned Line = getLineNumber(Loc);
+ unsigned Line = getLineNumber(PLoc);
collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext, TParamsArray,
Flags);
auto *FD = cast<FunctionDecl>(GD.getDecl());
@@ -4469,10 +4463,10 @@ llvm::DIGlobalVariable *
CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
QualType T;
StringRef Name, LinkageName;
- SourceLocation Loc = getFileLocation(VD->getLocation());
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(VD->getLocation());
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
llvm::DIScope *DContext = Unit;
- unsigned Line = getLineNumber(Loc);
+ unsigned Line = getLineNumber(PLoc);
llvm::MDTuple *TemplateParameters = nullptr;
collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, TemplateParameters,
@@ -4495,8 +4489,8 @@ llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) {
// in unlimited debug info)
if (const auto *TD = dyn_cast<TypeDecl>(D)) {
QualType Ty = CGM.getContext().getTypeDeclType(TD);
- SourceLocation Loc = getFileLocation(TD->getLocation());
- return getOrCreateType(Ty, getOrCreateFile(Loc));
+ PresumedLoc PLoc = getFileLocation(TD->getLocation());
+ return getOrCreateType(Ty, getOrCreateFile(PLoc));
}
auto I = DeclCache.find(D->getCanonicalDecl());
@@ -4542,8 +4536,8 @@ llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
auto MI = SPCache.find(FD->getCanonicalDecl());
if (MI == SPCache.end()) {
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
- SourceLocation Loc = getFileLocation(MD->getLocation());
- return CreateCXXMemberFunction(MD, getOrCreateFile(Loc),
+ PresumedLoc PLoc = getFileLocation(MD->getLocation());
+ return CreateCXXMemberFunction(MD, getOrCreateFile(PLoc),
cast<llvm::DICompositeType>(S));
}
}
@@ -4705,11 +4699,11 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
const Decl *D = GD.getDecl();
bool HasDecl = (D != nullptr);
- Loc = getFileLocation(Loc);
+ PresumedLoc PLoc = getFileLocation(Loc);
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
llvm::DIScope *FDContext = Unit;
llvm::DINodeArray TParamsArray;
bool KeyInstructions = CGM.getCodeGenOpts().DebugKeyInstructions;
@@ -4758,7 +4752,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
isa<VarDecl>(D) || isa<CapturedDecl>(D)) {
Flags |= llvm::DINode::FlagArtificial;
// Artificial functions should not silently reuse CurLoc.
- CurLoc = SourceLocation();
+ CurPLoc = PresumedLoc();
}
if (CurFuncIsThunk)
@@ -4773,7 +4767,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
llvm::DISubprogram::DISPFlags SPFlagsForDef =
SPFlags | llvm::DISubprogram::SPFlagDefinition;
- const unsigned LineNo = getLineNumber(Loc.isValid() ? Loc : CurLoc);
+ const unsigned LineNo = getLineNumber(PLoc.isValid() ? PLoc : CurPLoc);
unsigned ScopeLine = getLineNumber(getFileLocation(ScopeLoc));
llvm::DISubroutineType *DIFnType = getOrCreateFunctionType(D, FnType, Unit);
llvm::DISubprogram *Decl = nullptr;
@@ -4822,10 +4816,10 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
return GetName(D, true);
});
- Loc = getFileLocation(Loc);
+ PresumedLoc PLoc = getFileLocation(Loc);
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
bool IsDeclForCallSite = Fn ? true : false;
llvm::DIScope *FDContext =
IsDeclForCallSite ? Unit : getDeclContextDescriptor(D);
@@ -4845,10 +4839,10 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
if (D->isImplicit()) {
Flags |= llvm::DINode::FlagArtificial;
// Artificial functions without a location should not silently reuse CurLoc.
- if (Loc.isInvalid())
- CurLoc = SourceLocation();
+ if (PLoc.isInvalid())
+ CurPLoc = PLoc;
}
- unsigned LineNo = getLineNumber(Loc);
+ unsigned LineNo = getLineNumber(PLoc);
unsigned ScopeLine = 0;
llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero;
if (CGM.getCodeGenOpts().OptimizationLevel != 0)
@@ -4930,29 +4924,34 @@ void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
}
void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
+ EmitLocation(Builder, getFileLocation(Loc));
+}
+
+void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, const PresumedLoc &PLoc) {
// Update our current location
- setLocation(Loc);
+ setLocation(PLoc);
- if (CurLoc.isInvalid() || LexicalBlockStack.empty())
+ if (CurPLoc.isInvalid() || LexicalBlockStack.empty())
return;
llvm::MDNode *Scope = LexicalBlockStack.back();
Builder.SetCurrentDebugLocation(
- llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(CurLoc),
- getColumnNumber(CurLoc), Scope, CurInlinedAt));
+ llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(CurPLoc),
+ getColumnNumber(CurPLoc), Scope, CurInlinedAt));
}
-void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
+void CGDebugInfo::CreateLexicalBlock(const PresumedLoc &PLoc) {
llvm::MDNode *Back = nullptr;
if (!LexicalBlockStack.empty())
Back = LexicalBlockStack.back().get();
LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
- cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
- getColumnNumber(CurLoc)));
+ cast<llvm::DIScope>(Back), getOrCreateFile(CurPLoc), getLineNumber(CurPLoc),
+ getColumnNumber(CurPLoc)));
}
-SourceLocation CGDebugInfo::getFileLocation(SourceLocation Loc) const {
- return CGM.getContext().getSourceManager().getFileLoc(Loc);
+PresumedLoc CGDebugInfo::getFileLocation(SourceLocation Loc) const {
+ const SourceManager &SM = CGM.getContext().getSourceManager();
+ return SM.getPresumedLoc(SM.getFileLoc(Loc));
}
void CGDebugInfo::AppendAddressSpaceXDeref(
@@ -4971,19 +4970,19 @@ void CGDebugInfo::AppendAddressSpaceXDeref(
void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
SourceLocation Loc) {
// Set our current location.
- Loc = getFileLocation(Loc);
- setLocation(Loc);
+ PresumedLoc PLoc = getFileLocation(Loc);
+ setLocation(PLoc);
// Emit a line table change for the current location inside the new scope.
Builder.SetCurrentDebugLocation(llvm::DILocation::get(
- CGM.getLLVMContext(), getLineNumber(Loc), getColumnNumber(Loc),
+ CGM.getLLVMContext(), getLineNumber(PLoc), getColumnNumber(PLoc),
LexicalBlockStack.back(), CurInlinedAt));
if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
return;
// Create a new lexical block and push it on the stack.
- CreateLexicalBlock(Loc);
+ CreateLexicalBlock(PLoc);
}
void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder,
@@ -5007,7 +5006,7 @@ void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
// Pop all regions for this function.
while (LexicalBlockStack.size() != RCount) {
// Provide an entry in the line table for the end of the block.
- EmitLocation(Builder, CurLoc);
+ EmitLocation(Builder, CurPLoc);
LexicalBlockStack.pop_back();
}
FnBeginRegionCount.pop_back();
@@ -5024,8 +5023,8 @@ CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD,
uint64_t FieldSize, FieldOffset;
uint32_t FieldAlign;
- SourceLocation Loc = getFileLocation(VD->getLocation());
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(VD->getLocation());
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
QualType Type = VD->getType();
FieldOffset = 0;
@@ -5101,9 +5100,9 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
const bool VarIsArtificial = IsArtificial(VD);
llvm::DIFile *Unit = nullptr;
- SourceLocation Loc = getFileLocation(VD->getLocation());
+ PresumedLoc PLoc = getFileLocation(VD->getLocation());
if (!VarIsArtificial)
- Unit = getOrCreateFile(Loc);
+ Unit = getOrCreateFile(PLoc);
llvm::DIType *Ty;
uint64_t XOffset = 0;
if (VD->hasAttr<BlocksAttr>())
@@ -5120,8 +5119,8 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
unsigned Line = 0;
unsigned Column = 0;
if (!VarIsArtificial) {
- Line = getLineNumber(Loc);
- Column = getColumnNumber(Loc);
+ Line = getLineNumber(PLoc);
+ Column = getColumnNumber(PLoc);
}
SmallVector<uint64_t, 13> Expr;
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
@@ -5287,8 +5286,8 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
if (isa<DeclRefExpr>(BD->getBinding()))
return nullptr;
- SourceLocation Loc = getFileLocation(BD->getLocation());
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(BD->getLocation());
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
llvm::DIType *Ty = getOrCreateType(BD->getType(), Unit);
// If there is no debug info for this type then do not emit debug info
@@ -5311,8 +5310,8 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
Expr.push_back(llvm::dwarf::DW_OP_deref);
}
- unsigned Line = getLineNumber(Loc);
- unsigned Column = getColumnNumber(Loc);
+ unsigned Line = getLineNumber(PLoc);
+ unsigned Column = getColumnNumber(PLoc);
StringRef Name = BD->getName();
auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
// Create the descriptor for the variable.
@@ -5406,12 +5405,12 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
return;
auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
- SourceLocation Loc = getFileLocation(D->getLocation());
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(D->getLocation());
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
// Get location information.
- unsigned Line = getLineNumber(Loc);
- unsigned Column = getColumnNumber(Loc);
+ unsigned Line = getLineNumber(PLoc);
+ unsigned Column = getColumnNumber(PLoc);
StringRef Name = D->getName();
@@ -5450,8 +5449,8 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
bool isByRef = VD->hasAttr<BlocksAttr>();
uint64_t XOffset = 0;
- SourceLocation Loc = getFileLocation(VD->getLocation());
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(VD->getLocation());
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
llvm::DIType *Ty;
if (isByRef)
Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
@@ -5465,8 +5464,8 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
Ty = CreateSelfType(VD->getType(), Ty);
// Get location information.
- const unsigned Line = getLineNumber(Loc.isValid() ? Loc : CurLoc);
- unsigned Column = getColumnNumber(Loc);
+ const unsigned Line = getLineNumber(PLoc.isValid() ? PLoc : CurPLoc);
+ unsigned Column = getColumnNumber(PLoc);
const llvm::DataLayout &target = CGM.getDataLayout();
@@ -5527,32 +5526,33 @@ bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
} // namespace
void CGDebugInfo::collectDefaultFieldsForBlockLiteralDeclare(
- const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc,
- const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit,
+ const CGBlockInfo &Block, const ASTContext &Context,
+ const PresumedLoc &PLoc, const llvm::StructLayout &BlockLayout,
+ llvm::DIFile *Unit,
SmallVectorImpl<llvm::Metadata *> &Fields) {
// Blocks in OpenCL have unique constraints which make the standard fields
// redundant while requiring size and align fields for enqueue_kernel. See
// initializeForBlockHeader in CGBlocks.cpp
if (CGM.getLangOpts().OpenCL) {
- Fields.push_back(createFieldType("__size", Context.IntTy, Loc, AS_public,
+ Fields.push_back(createFieldType("__size", Context.IntTy, PLoc, AS_public,
BlockLayout.getElementOffsetInBits(0),
Unit, Unit));
- Fields.push_back(createFieldType("__align", Context.IntTy, Loc, AS_public,
+ Fields.push_back(createFieldType("__align", Context.IntTy, PLoc, AS_public,
BlockLayout.getElementOffsetInBits(1),
Unit, Unit));
} else {
- Fields.push_back(createFieldType("__isa", Context.VoidPtrTy, Loc, AS_public,
+ Fields.push_back(createFieldType("__isa", Context.VoidPtrTy, PLoc, AS_public,
BlockLayout.getElementOffsetInBits(0),
Unit, Unit));
- Fields.push_back(createFieldType("__flags", Context.IntTy, Loc, AS_public,
+ Fields.push_back(createFieldType("__flags", Context.IntTy, PLoc, AS_public,
BlockLayout.getElementOffsetInBits(1),
Unit, Unit));
Fields.push_back(
- createFieldType("__reserved", Context.IntTy, Loc, AS_public,
+ createFieldType("__reserved", Context.IntTy, PLoc, AS_public,
BlockLayout.getElementOffsetInBits(2), Unit, Unit));
auto *FnTy = Block.getBlockExpr()->getFunctionType();
auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
- Fields.push_back(createFieldType("__FuncPtr", FnPtrType, Loc, AS_public,
+ Fields.push_back(createFieldType("__FuncPtr", FnPtrType, PLoc, AS_public,
BlockLayout.getElementOffsetInBits(3),
Unit, Unit));
Fields.push_back(createFieldType(
@@ -5560,7 +5560,7 @@ void CGDebugInfo::collectDefaultFieldsForBlockLiteralDeclare(
Context.getPointerType(Block.NeedsCopyDispose
? Context.getBlockDescriptorExtendedType()
: Context.getBlockDescriptorType()),
- Loc, AS_public, BlockLayout.getElementOffsetInBits(4), Unit, Unit));
+ PLoc, AS_public, BlockLayout.getElementOffsetInBits(4), Unit, Unit));
}
}
@@ -5574,10 +5574,10 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
const BlockDecl *blockDecl = block.getBlockDecl();
// Collect some general information about the block's location.
- SourceLocation loc = getFileLocation(blockDecl->getCaretLocation());
- llvm::DIFile *tunit = getOrCreateFile(loc);
- unsigned line = getLineNumber(loc);
- unsigned column = getColumnNumber(loc);
+ PresumedLoc PLoc = getFileLocation(blockDecl->getCaretLocation());
+ llvm::DIFile *tunit = getOrCreateFile(PLoc);
+ unsigned line = getLineNumber(PLoc);
+ unsigned column = getColumnNumber(PLoc);
// Build the debug-info type for the block literal.
getDeclContextDescriptor(blockDecl);
@@ -5586,7 +5586,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
CGM.getDataLayout().getStructLayout(block.StructureType);
SmallVector<llvm::Metadata *, 16> fields;
- collectDefaultFieldsForBlockLiteralDeclare(block, C, loc, *blockLayout, tunit,
+ collectDefaultFieldsForBlockLiteralDeclare(block, C, PLoc, *blockLayout, tunit,
fields);
// We want to sort the captures by offset, not because DWARF
@@ -5636,7 +5636,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
else
llvm_unreachable("unexpected block declcontext");
- fields.push_back(createFieldType("this", type, loc, AS_public,
+ fields.push_back(createFieldType("this", type, PLoc, AS_public,
offsetInBits, tunit, tunit));
continue;
}
@@ -5658,7 +5658,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
llvm::DINode::FlagZero, fieldType);
} else {
auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
- fieldType = createFieldType(name, variable->getType(), loc, AS_public,
+ fieldType = createFieldType(name, variable->getType(), PLoc, AS_public,
offsetInBits, Align, tunit, tunit);
}
fields.push_back(fieldType);
@@ -6046,9 +6046,9 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
});
auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
- SourceLocation Loc = getFileLocation(VD->getLocation());
+ PresumedLoc PLoc = getFileLocation(VD->getLocation());
// Create the descriptor for the variable.
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
StringRef Name = VD->getName();
llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
@@ -6106,7 +6106,7 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
}
GV.reset(DBuilder.createGlobalVariableExpression(
- DContext, Name, StringRef(), Unit, getLineNumber(Loc), Ty, true, true,
+ DContext, Name, StringRef(), Unit, getLineNumber(PLoc), Ty, true, true,
InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
TemplateParameters, Align));
}
@@ -6118,15 +6118,15 @@ void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
return;
auto Align = getDeclAlignIfRequired(D, CGM.getContext());
- SourceLocation Loc = getFileLocation(D->getLocation());
- llvm::DIFile *Unit = getOrCreateFile(Loc);
+ PresumedLoc PLoc = getFileLocation(D->getLocation());
+ llvm::DIFile *Unit = getOrCreateFile(PLoc);
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(Loc), Ty, false,
+ DContext, Name, StringRef(), Unit, getLineNumber(PLoc), Ty, false,
false, nullptr, nullptr, nullptr, Align);
Var->addDebugInfo(GVE);
}
@@ -6204,10 +6204,10 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
return;
llvm::DIScope *DContext = getDeclContextDescriptor(D);
- SourceLocation Loc = getFileLocation(D->getLocation());
+ PresumedLoc PLoc = getFileLocation(D->getLocation());
llvm::DIImportedEntity *ImportDI = DBuilder.createImportedDeclaration(
- DContext, DI, getOrCreateFile(Loc), getLineNumber(Loc), D->getName());
+ DContext, DI, getOrCreateFile(PLoc), getLineNumber(PLoc), D->getName());
// Record this DIE in the cache for nested declaration reference.
ImportedDeclCache[GD.getCanonicalDecl().getDecl()].reset(ImportDI);
@@ -6215,15 +6215,14 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
const StringLiteral *S) {
- SourceLocation Loc = getFileLocation(S->getStrTokenLoc(0));
- PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
+ PresumedLoc PLoc = getFileLocation(S->getStrTokenLoc(0));
if (!PLoc.isValid())
return;
- llvm::DIFile *File = getOrCreateFile(Loc);
+ llvm::DIFile *File = getOrCreateFile(PLoc);
llvm::DIGlobalVariableExpression *Debug =
DBuilder.createGlobalVariableExpression(
- nullptr, StringRef(), StringRef(), File, getLineNumber(Loc),
+ nullptr, StringRef(), StringRef(), File, getLineNumber(PLoc),
getOrCreateType(S->getType(), File), true);
GV->addDebugInfo(Debug);
}
@@ -6241,22 +6240,22 @@ void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) {
const NamespaceDecl *NSDecl = UD.getNominatedNamespace();
if (!NSDecl->isAnonymousNamespace() ||
CGM.getCodeGenOpts().DebugExplicitImport) {
- SourceLocation Loc = getFileLocation(UD.getLocation());
- if (!Loc.isValid())
- Loc = CurLoc;
+ PresumedLoc PLoc = getFileLocation(UD.getLocation());
+ if (!PLoc.isValid())
+ PLoc = CurPLoc;
DBuilder.createImportedModule(
getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
- getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
+ getOrCreateNamespace(NSDecl), getOrCreateFile(PLoc), getLineNumber(PLoc));
}
}
void CGDebugInfo::EmitUsingShadowDecl(const UsingShadowDecl &USD) {
if (llvm::DINode *Target =
getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
- SourceLocation Loc = getFileLocation(USD.getLocation());
+ PresumedLoc PLoc = getFileLocation(USD.getLocation());
DBuilder.createImportedDeclaration(
getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
- getOrCreateFile(Loc), getLineNumber(Loc));
+ getOrCreateFile(PLoc), getLineNumber(PLoc));
}
}
@@ -6317,19 +6316,19 @@ CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) {
if (VH)
return cast<llvm::DIImportedEntity>(VH);
llvm::DIImportedEntity *R;
- auto Loc = getFileLocation(NA.getLocation());
+ auto PLoc = getFileLocation(NA.getLocation());
if (const auto *Underlying =
dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
// This could cache & dedup here rather than relying on metadata deduping.
R = DBuilder.createImportedDeclaration(
getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
- EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
- getLineNumber(Loc), NA.getName());
+ EmitNamespaceAlias(*Underlying), getOrCreateFile(PLoc),
+ getLineNumber(PLoc), NA.getName());
else
R = DBuilder.createImportedDeclaration(
getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
- getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
+ getOrCreateFile(PLoc), getLineNumber(PLoc), NA.getName());
VH.reset(R);
return R;
}
@@ -6449,10 +6448,10 @@ llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
if (LexicalBlockStack.empty())
return llvm::DebugLoc();
- Loc = getFileLocation(Loc);
+ PresumedLoc PLoc = getFileLocation(Loc);
llvm::MDNode *Scope = LexicalBlockStack.back();
- return llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(Loc),
- getColumnNumber(Loc), Scope);
+ return llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(PLoc),
+ getColumnNumber(PLoc), Scope);
}
llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const {
diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index 07e1fbc485292..71c6db91e5f0a 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -68,7 +68,7 @@ class CGDebugInfo {
llvm::DICompileUnit *TheCU = nullptr;
ModuleMap *ClangModuleMap = nullptr;
ASTSourceDescriptor PCHDescriptor;
- SourceLocation CurLoc;
+ PresumedLoc CurPLoc;
llvm::MDNode *CurInlinedAt = nullptr;
llvm::DIType *VTablePtrType = nullptr;
llvm::DIType *ClassTy = nullptr;
@@ -346,18 +346,18 @@ class CGDebugInfo {
llvm::DINodeArray CollectBTFDeclTagAnnotations(const Decl *D);
llvm::DIType *createFieldType(StringRef name, QualType type,
- SourceLocation loc, AccessSpecifier AS,
+ const PresumedLoc &PLoc, AccessSpecifier AS,
uint64_t offsetInBits, uint32_t AlignInBits,
llvm::DIFile *tunit, llvm::DIScope *scope,
const RecordDecl *RD = nullptr,
llvm::DINodeArray Annotations = nullptr);
llvm::DIType *createFieldType(StringRef name, QualType type,
- SourceLocation loc, AccessSpecifier AS,
+ const PresumedLoc &PLoc, AccessSpecifier AS,
uint64_t offsetInBits, llvm::DIFile *tunit,
llvm::DIScope *scope,
const RecordDecl *RD = nullptr) {
- return createFieldType(name, type, loc, AS, offsetInBits, 0, tunit, scope,
+ return createFieldType(name, type, PLoc, AS, offsetInBits, 0, tunit, scope,
RD);
}
@@ -406,7 +406,7 @@ class CGDebugInfo {
/// @}
/// Create a new lexical block node and push it on the stack.
- void CreateLexicalBlock(SourceLocation Loc);
+ void CreateLexicalBlock(const PresumedLoc &PLoc);
/// All locations handled by CGDebugInfo are file locations because this
/// is more suitable for debugging than pure spelling or expansion locations.
@@ -416,7 +416,7 @@ class CGDebugInfo {
/// macro body, its file 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 getFileLocation(SourceLocation Loc) const;
+ PresumedLoc getFileLocation(SourceLocation Loc) const;
/// If target-specific LLVM \p AddressSpace directly maps to target-specific
/// DWARF address space, appends extended dereferencing mechanism to complex
@@ -439,9 +439,9 @@ class CGDebugInfo {
/// A helper function to collect debug info for the default fields of a
/// block.
void collectDefaultFieldsForBlockLiteralDeclare(
- const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc,
- const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit,
- SmallVectorImpl<llvm::Metadata *> &Fields);
+ const CGBlockInfo &Block, const ASTContext &Context,
+ const PresumedLoc &PLoc, const llvm::StructLayout &BlockLayout,
+ llvm::DIFile *Unit, SmallVectorImpl<llvm::Metadata *> &Fields);
public:
CGDebugInfo(CodeGenModule &CGM);
@@ -475,11 +475,11 @@ class CGDebugInfo {
/// Update the current source location. If \arg loc is invalid it is
/// ignored.
- void setLocation(SourceLocation Loc);
+ void setLocation(const PresumedLoc &PLoc);
/// Return the current source location. This does not necessarily correspond
/// to the IRBuilder's current DebugLoc.
- SourceLocation getLocation() const { return CurLoc; }
+ const PresumedLoc &getLocation() const { return CurPLoc; }
/// Update the current inline scope. All subsequent calls to \p EmitLocation
/// will create a location with this inlinedAt field.
@@ -496,6 +496,11 @@ class CGDebugInfo {
/// location will be reused.
void EmitLocation(CGBuilderTy &Builder, SourceLocation Loc);
+ /// Emit metadata to indicate a change in line/column information in
+ /// the source file. If the location is invalid, the previous
+ /// location will be reused.
+ void EmitLocation(CGBuilderTy &Builder, const PresumedLoc &Loc);
+
QualType getFunctionType(const FunctionDecl *FD, QualType RetTy,
const SmallVectorImpl<const VarDecl *> &Args);
@@ -753,7 +758,7 @@ class CGDebugInfo {
/// Convenience function to get the file debug info descriptor for the input
/// location.
- llvm::DIFile *getOrCreateFile(SourceLocation Loc);
+ llvm::DIFile *getOrCreateFile(const PresumedLoc &PLoc);
/// Create a file debug info descriptor for a source file.
llvm::DIFile *
@@ -872,12 +877,12 @@ class CGDebugInfo {
/// Get line number for the location. If location is invalid
/// then use current location.
- unsigned getLineNumber(SourceLocation Loc);
+ unsigned getLineNumber(const PresumedLoc &PLoc) const;
/// Get column number for the location. If location is
/// invalid then use current location.
/// \param Force Assume DebugColumnInfo option is true.
- unsigned getColumnNumber(SourceLocation Loc, bool Force = false);
+ unsigned getColumnNumber(const PresumedLoc &PLoc, bool Force = false) const;
/// Collect various properties of a FunctionDecl.
/// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl.
@@ -920,9 +925,9 @@ class CGDebugInfo {
/// location or preferred location of the specified Expr.
class ApplyDebugLocation {
private:
- void init(SourceLocation TemporaryLocation, bool DefaultToEmpty = false);
+ void init(const PresumedLoc &TemporaryLocation, bool DefaultToEmpty = false);
ApplyDebugLocation(CodeGenFunction &CGF, bool DefaultToEmpty,
- SourceLocation TemporaryLocation);
+ const PresumedLoc &TemporaryLocation);
llvm::DebugLoc OriginalLocation;
CodeGenFunction *CGF;
@@ -959,7 +964,7 @@ class ApplyDebugLocation {
/// SourceLocation to CGDebugInfo::setLocation() will result in the
/// last valid location being reused.
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF) {
- return ApplyDebugLocation(CGF, false, SourceLocation());
+ return ApplyDebugLocation(CGF, false, PresumedLoc());
}
/// Apply TemporaryLocation if it is valid. Otherwise switch
/// to an artificial debug location that has a valid scope, but no
@@ -967,7 +972,7 @@ class ApplyDebugLocation {
static ApplyDebugLocation
CreateDefaultArtificial(CodeGenFunction &CGF,
SourceLocation TemporaryLocation) {
- return ApplyDebugLocation(CGF, false, TemporaryLocation);
+ return ApplyDebugLocation(CGF, TemporaryLocation);
}
/// Set the IRBuilder to not attach debug locations. Note that
@@ -976,13 +981,13 @@ class ApplyDebugLocation {
/// all instructions that do not have a location at the beginning of
/// a function are counted towards to function prologue.
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF) {
- return ApplyDebugLocation(CGF, true, SourceLocation());
+ return ApplyDebugLocation(CGF, true, PresumedLoc());
}
};
/// A scoped helper to set the current debug location to an inlined location.
class ApplyInlineDebugLocation {
- SourceLocation SavedLocation;
+ PresumedLoc SavedLocation;
CodeGenFunction *CGF;
public:
>From 05214eca7daf14bc94d4e1c1440d7659d58dee50 Mon Sep 17 00:00:00 2001
From: skill <skill at google.com>
Date: Tue, 21 Oct 2025 17:55:35 +0200
Subject: [PATCH 14/14] Fix bug added during refactoring.
---
clang/lib/CodeGen/CGDebugInfo.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 718a93b54aefb..a5a5593ab5372 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -636,7 +636,7 @@ unsigned CGDebugInfo::getColumnNumber(const PresumedLoc & PLoc, bool Force) cons
// If the location is invalid then use the current column.
if (PLoc.isInvalid() && CurPLoc.isInvalid())
return 0;
- return PLoc.isValid() ? PLoc.getColumn() : 0;
+ return PLoc.isValid() ? PLoc.getColumn() : CurPLoc.getColumn();
}
StringRef CGDebugInfo::getCurrentDirname() {
More information about the cfe-commits
mailing list