https://github.com/AaronBallman created 
https://github.com/llvm/llvm-project/pull/137324

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

>From 4a159b5245b023fe04e9f46549dee009aa155f42 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aa...@aaronballman.com>
Date: Fri, 25 Apr 2025 08:49:35 -0400
Subject: [PATCH] Fix crash with -ast-dump=json

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
---
 clang/docs/ReleaseNotes.rst                       |  2 ++
 clang/lib/AST/Mangle.cpp                          |  8 +++++---
 clang/test/AST/ast-crash-dump-mangled-name-json.m | 14 ++++++++++++++
 3 files changed, 21 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/AST/ast-crash-dump-mangled-name-json.m

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]",

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to