diff --git a/scripts/lldbinit.py b/scripts/lldbinit.py index ff1732ab9157..719a615d14fe 100644 --- a/scripts/lldbinit.py +++ b/scripts/lldbinit.py @@ -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", diff --git a/toolchain/check/dump.cpp b/toolchain/check/dump.cpp index 3aae38bee6fc..b79e9a23fbba 100644 --- a/toolchain/check/dump.cpp +++ b/toolchain/check/dump.cpp @@ -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); diff --git a/toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon b/toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon index 9252cf4d6732..732238a34a4d 100644 --- a/toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon +++ b/toolchain/check/testdata/basics/raw_sem_ir/cpp_interop.carbon @@ -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: ""}, 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: ""}, 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, 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}} diff --git a/toolchain/sem_ir/BUILD b/toolchain/sem_ir/BUILD index 56665a8cbdf7..7fa4ad192ec1 100644 --- a/toolchain/sem_ir/BUILD +++ b/toolchain/sem_ir/BUILD @@ -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. diff --git a/toolchain/sem_ir/dump.cpp b/toolchain/sem_ir/dump.cpp index bcbc346431fa..3fd33db915a3 100644 --- a/toolchain/sem_ir/dump.cpp +++ b/toolchain/sem_ir/dump.cpp @@ -8,6 +8,7 @@ #include +#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); } diff --git a/toolchain/sem_ir/dump.h b/toolchain/sem_ir/dump.h index 77f7f3a69dbc..a97015cb6040 100644 --- a/toolchain/sem_ir/dump.h +++ b/toolchain/sem_ir/dump.h @@ -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; diff --git a/toolchain/sem_ir/ids.h b/toolchain/sem_ir/ids.h index 9d319f5e1dcc..a64b3c72e506 100644 --- a/toolchain/sem_ir/ids.h +++ b/toolchain/sem_ir/ids.h @@ -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 { - 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 { - static constexpr llvm::StringLiteral Label = "clang_decl_signature_id"; + static constexpr llvm::StringLiteral Label = "clang_decl_signature"; using IdBase::IdBase; };