Improve debugability of clang decls (#7945)

Add `Dump` functions for `ClangDecl`/`ClangDeclSignature`, and add
`MakeClangDeclId`/`MakeClangDeclSignatureId` and add those to
`lldbinit.py`.

Also change the label for `ClangDeclId` and `ClangDeclSignatureId` to
not have an "_id" suffix, to make them consistent with other ID types.
This commit is contained in:
Nicholas Bishop authored and GitHub committed 2026-10-09 20:24:23 +00:00
1 parent 9887d23b3f
commit 04443a9a14
7 files changed
+64 -13

No files matched your search

+2
View File
@@ -82,6 +82,8 @@ Example usage:
# The set of "Make" functions in dump.cpp.
id_types = {
"clang_decl": "SemIR::MakeClangDeclId",
"clang_decl_signature": "SemIR::MakeClangDeclSignatureId",
"class": "SemIR::MakeClassId",
"constant": "SemIR::MakeConstantId",
"constraint": "SemIR::MakeNamedConstraintId",
+12
View File
@@ -54,6 +54,18 @@ LLVM_DUMP_METHOD static auto Dump(
return SemIR::Dump(context.sem_ir(), generated_function_id);
}
LLVM_DUMP_METHOD static auto Dump(const Context& context,
SemIR::ClangDeclId clang_decl_id)
-> std::string {
return SemIR::Dump(context.sem_ir(), clang_decl_id);
}
LLVM_DUMP_METHOD static auto Dump(
const Context& context, SemIR::ClangDeclSignatureId clang_decl_signature_id)
-> std::string {
return SemIR::Dump(context.sem_ir(), clang_decl_signature_id);
}
LLVM_DUMP_METHOD static auto Dump(const Context& context,
SemIR::ClassId class_id) -> std::string {
return SemIR::Dump(context.sem_ir(), class_id);
+11 -11
View File
@@ -55,18 +55,18 @@ fn G(x: Cpp.X) {
// CHECK:STDOUT: import_ir_inst5: {ir_id: import_ir(Cpp), clang_source_loc_id: clang_source_loc50000005}
// CHECK:STDOUT: import_ir_inst6: {ir_id: import_ir(Cpp), clang_source_loc_id: clang_source_loc50000006}
// CHECK:STDOUT: clang_decls:
// CHECK:STDOUT: clang_decl_id50000000: {key: {decl: "namespace Carbon {\n}"}, inst_id: inst(Package)}
// CHECK:STDOUT: clang_decl_id50000001: {key: {decl: "<translation unit>"}, inst_id: inst50000011}
// CHECK:STDOUT: clang_decl_id50000002: {key: {decl: "struct X {}"}, inst_id: inst50000013}
// CHECK:STDOUT: clang_decl_id50000003: {key: {decl: "X * _Nonnull p"}, inst_id: inst50000024}
// CHECK:STDOUT: clang_decl_id50000004: {key: {decl: "void f(X x = {})", clang_decl_signature_id: clang_decl_signature_id50000000}, inst_id: inst5000002F}
// CHECK:STDOUT: clang_decl_id50000005: {key: {decl: "inline void f__carbon_thunk()", clang_decl_signature_id: clang_decl_signature_id50000000}, inst_id: inst50000032}
// CHECK:STDOUT: clang_decl_id50000006: {key: {decl: "void f(X x = {})", clang_decl_signature_id: clang_decl_signature_id50000001}, inst_id: inst5000003D}
// CHECK:STDOUT: clang_decl_id50000007: {key: {decl: "inline void f__carbon_thunk(X * _Nonnull x)", clang_decl_signature_id: clang_decl_signature_id50000001}, inst_id: inst50000047}
// CHECK:STDOUT: clang_decl_id50000008: {key: {decl: "X * _Nonnull global"}, inst_id: inst50000052}
// CHECK:STDOUT: clang_decl50000000: {key: {decl: "namespace Carbon {\n}"}, inst_id: inst(Package)}
// CHECK:STDOUT: clang_decl50000001: {key: {decl: "<translation unit>"}, inst_id: inst50000011}
// CHECK:STDOUT: clang_decl50000002: {key: {decl: "struct X {}"}, inst_id: inst50000013}
// CHECK:STDOUT: clang_decl50000003: {key: {decl: "X * _Nonnull p"}, inst_id: inst50000024}
// CHECK:STDOUT: clang_decl50000004: {key: {decl: "void f(X x = {})", clang_decl_signature_id: clang_decl_signature50000000}, inst_id: inst5000002F}
// CHECK:STDOUT: clang_decl50000005: {key: {decl: "inline void f__carbon_thunk()", clang_decl_signature_id: clang_decl_signature50000000}, inst_id: inst50000032}
// CHECK:STDOUT: clang_decl50000006: {key: {decl: "void f(X x = {})", clang_decl_signature_id: clang_decl_signature50000001}, inst_id: inst5000003D}
// CHECK:STDOUT: clang_decl50000007: {key: {decl: "inline void f__carbon_thunk(X * _Nonnull x)", clang_decl_signature_id: clang_decl_signature50000001}, inst_id: inst50000047}
// CHECK:STDOUT: clang_decl50000008: {key: {decl: "X * _Nonnull global"}, inst_id: inst50000052}
// CHECK:STDOUT: clang_decl_signatures:
// CHECK:STDOUT: clang_decl_signature_id50000000: {kind: normal, num_params: 0}
// CHECK:STDOUT: clang_decl_signature_id50000001: {kind: normal, num_params: 1, modes: [value]}
// CHECK:STDOUT: clang_decl_signature50000000: {kind: normal, num_params: 0}
// CHECK:STDOUT: clang_decl_signature50000001: {kind: normal, num_params: 1, modes: [value]}
// CHECK:STDOUT: name_scopes:
// CHECK:STDOUT: name_scope0: {inst: inst(Package), parent_scope: name_scope<none>, has_error: false, extended_scopes: [], names: {name(Cpp): inst50000011, name0: inst5000001E}}
// CHECK:STDOUT: name_scope50000001: {inst: inst50000011, parent_scope: name_scope0, has_error: false, extended_scopes: [], names: {name2: inst50000013, name3: inst5000002C, name4: inst50000054}}
+1
View File
@@ -354,6 +354,7 @@ cc_library(
":typed_insts",
"//common:ostream",
"//common:raw_string_ostream",
"@llvm-project//clang:ast",
],
# Always link dump methods so they are callable from a debugger
# even though they are never called.
+33
View File
@@ -8,6 +8,7 @@
#include <string>
#include "clang/AST/DeclBase.h"
#include "common/raw_string_ostream.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/inst.h"
@@ -213,6 +214,31 @@ LLVM_DUMP_METHOD auto Dump(const File& file,
return out.TakeStr();
}
LLVM_DUMP_METHOD auto Dump(const File& file, ClangDeclId clang_decl_id)
-> std::string {
RawStringOstream out;
out << clang_decl_id;
if (clang_decl_id.has_value()) {
const auto& clang_decl = file.clang_decls().Get(clang_decl_id);
out << ": " << clang_decl << "\ndecl:\n";
clang_decl.decl()->dump(out);
}
return out.TakeStr();
}
LLVM_DUMP_METHOD auto Dump(const File& file,
ClangDeclSignatureId clang_decl_signature_id)
-> std::string {
RawStringOstream out;
out << clang_decl_signature_id;
if (clang_decl_signature_id.has_value()) {
const auto& clang_decl_signature =
file.clang_decl_signatures().Get(clang_decl_signature_id);
out << ": " << clang_decl_signature;
}
return out.TakeStr();
}
LLVM_DUMP_METHOD auto Dump(const File& file, ClassId class_id) -> std::string {
RawStringOstream out;
out << class_id;
@@ -647,6 +673,13 @@ LLVM_DUMP_METHOD static auto MakeGeneratedFunctionId(int id)
-> GeneratedFunctionId {
return GeneratedFunctionId(id);
}
LLVM_DUMP_METHOD static auto MakeClangDeclId(int id) -> ClangDeclId {
return ClangDeclId(id);
}
LLVM_DUMP_METHOD static auto MakeClangDeclSignatureId(int id)
-> ClangDeclSignatureId {
return ClangDeclSignatureId(id);
}
LLVM_DUMP_METHOD static auto MakeClassId(int id) -> ClassId {
return ClassId(id);
}
+3
View File
@@ -33,6 +33,9 @@ auto Dump(const File& file) -> std::string;
auto Dump(const File& file, RawBundleId bundle_id) -> std::string;
auto Dump(const File& file, GeneratedFunctionId generated_function_id)
-> std::string;
auto Dump(const File& file, ClangDeclId clang_decl_id) -> std::string;
auto Dump(const File& file, ClangDeclSignatureId clang_decl_signature_id)
-> std::string;
auto Dump(const File& file, ClassId class_id) -> std::string;
auto Dump(const File& file, ConstantId const_id) -> std::string;
auto Dump(const File& file, CppOverloadSetId overload_set_id) -> std::string;
+2 -2
View File
@@ -500,14 +500,14 @@ inline constexpr ImportIRId ImportIRId::Cpp = ImportIRId(ApiForImpl.index + 1);
// embedding pointers directly into the storage of SemIR as part of an
// instruction.
struct ClangDeclId : public IdBase<ClangDeclId> {
static constexpr llvm::StringLiteral Label = "clang_decl_id";
static constexpr llvm::StringLiteral Label = "clang_decl";
using IdBase::IdBase;
};
// The ID of a `ClangDeclSignature`.
struct ClangDeclSignatureId : public IdBase<ClangDeclSignatureId> {
static constexpr llvm::StringLiteral Label = "clang_decl_signature_id";
static constexpr llvm::StringLiteral Label = "clang_decl_signature";
using IdBase::IdBase;
};