Skip to content

Fix crash with -ast-dump=json #137324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ Non-comprehensive list of changes in this release
- Added `__builtin_elementwise_exp10`.
- For AMDPGU targets, added `__builtin_v_cvt_off_f32_i4` that maps to the `v_cvt_off_f32_i4` instruction.
- Added `__builtin_elementwise_minnum` and `__builtin_elementwise_maxnum`.
- No longer crashing on invalid Objective-C categories and extensions when
dumping the AST as JSON. (#GH137320)

New Compiler Flags
------------------
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/AST/Mangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,11 @@ void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
}
OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
if (const auto *CID = MD->getCategory()) {
OS << CID->getClassInterface()->getName();
if (includeCategoryNamespace) {
OS << '(' << *CID << ')';
if (const auto *CI = CID->getClassInterface()) {
OS << CI->getName();
if (includeCategoryNamespace) {
OS << '(' << *CID << ')';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still try to emit the category name (*CID) when the class interface (CI) isn't found?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea; what does Objective-C expect? CC @rjmccall ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought +[(SomeExtension) someMethod] might be slightly better than +[ someMethod] because at least it shows the category name. But both of them look wrong without the interface name.

It probably doesn't matter too much.

}
}
} else if (const auto *CD =
dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
Expand Down
14 changes: 14 additions & 0 deletions clang/test/AST/ast-crash-dump-mangled-name-json.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: not %clang_cc1 -ast-dump=json %s 2>&1 | FileCheck %s

// Ensure that dumping this does not crash when emitting the mangled name.
// See GH137320 for details.
// Note, this file does not compile and so we also check the error.

@interface SomeClass (SomeExtension)
+ (void)someMethod;
@end

// CHECK: error: cannot find interface declaration for 'SomeClass'

// CHECK: "name": "someMethod"
// CHECK-NEXT: "mangledName": "+[ someMethod]",