Skip to content

Commit ef23e1f

Browse files
committed
[debug-utils] Add a new utility getDebugVarName to translate a SILValue to a potential debug name.
If we don't find a name, we just return unknown so code that uses this still gets valid output.
1 parent 775c632 commit ef23e1f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

include/swift/SIL/DebugUtils.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ struct DebugVarCarryingInst {
327327
}
328328
};
329329

330+
/// Attempt to discover a StringRef varName for the value \p value. If we fail,
331+
/// we return the name "unknown".
332+
inline StringRef getDebugVarName(SILValue value) {
333+
if (auto *asi = dyn_cast<AllocStackInst>(value)) {
334+
DebugVarCarryingInst debugVar(asi);
335+
if (auto varInfo = debugVar.getVarInfo()) {
336+
return varInfo->Name;
337+
} else {
338+
if (auto *decl = debugVar.getDecl()) {
339+
return decl->getBaseName().userFacingName();
340+
}
341+
}
342+
}
343+
344+
StringRef varName = "unknown";
345+
if (auto *use = getSingleDebugUse(value)) {
346+
DebugVarCarryingInst debugVar(use->getUser());
347+
if (auto varInfo = debugVar.getVarInfo()) {
348+
varName = varInfo->Name;
349+
} else {
350+
if (auto *decl = debugVar.getDecl()) {
351+
varName = decl->getBaseName().userFacingName();
352+
}
353+
}
354+
}
355+
return varName;
356+
}
357+
330358
} // end namespace swift
331359

332360
#endif // SWIFT_SIL_DEBUGUTILS_H

0 commit comments

Comments
 (0)