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

Conversation

AaronBallman
Copy link
Collaborator

When given an invalid Objective-C extension, Clang would crash when trying to emit the mangled name of the method to the JSON dump output.

Fixes #137320

When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
@AaronBallman AaronBallman added clang:frontend Language frontend issues, e.g. anything involving "Sema" crash-on-invalid objective-c labels Apr 25, 2025
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Apr 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 25, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

When given an invalid Objective-C extension, Clang would crash when trying to emit the mangled name of the method to the JSON dump output.

Fixes #137320


Full diff: https://github.com/llvm/llvm-project/pull/137324.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/AST/Mangle.cpp (+5-3)
  • (added) clang/test/AST/ast-crash-dump-mangled-name-json.m (+14)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6ecb97825ab8d..9c406be0efa97 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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 extensions when dumping the AST as
+  JSON. (#GH137320)
 
 New Compiler Flags
 ------------------
diff --git a/clang/lib/AST/Mangle.cpp b/clang/lib/AST/Mangle.cpp
index 141957c1cdce0..741c031a40385 100644
--- a/clang/lib/AST/Mangle.cpp
+++ b/clang/lib/AST/Mangle.cpp
@@ -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 << ')';
+      }
     }
   } else if (const auto *CD =
                  dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
diff --git a/clang/test/AST/ast-crash-dump-mangled-name-json.m b/clang/test/AST/ast-crash-dump-mangled-name-json.m
new file mode 100644
index 0000000000000..6def64761bd2f
--- /dev/null
+++ b/clang/test/AST/ast-crash-dump-mangled-name-json.m
@@ -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]",

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.

el-ev pushed a commit that referenced this pull request Apr 29, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes #137320
@AaronBallman AaronBallman merged commit 5e4ec04 into llvm:main Apr 29, 2025
12 checks passed
@AaronBallman AaronBallman deleted the aballman-gh137320 branch April 29, 2025 11:57
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
tonykuttai pushed a commit to tonykuttai/llvm-project that referenced this pull request May 29, 2025
When given an invalid Objective-C extension, Clang would crash when
trying to emit the mangled name of the method to the JSON dump output.

Fixes llvm#137320
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category crash-on-invalid objective-c
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash during clang ast-dump, but only in JSON output mode
3 participants