Skip to content

Commit c5bf901

Browse files
authored
Fix crash with -ast-dump=json (llvm#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 llvm#137320
1 parent 0b5daeb commit c5bf901

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ Non-comprehensive list of changes in this release
220220
- Added `__builtin_elementwise_exp10`.
221221
- For AMDPGU targets, added `__builtin_v_cvt_off_f32_i4` that maps to the `v_cvt_off_f32_i4` instruction.
222222
- Added `__builtin_elementwise_minnum` and `__builtin_elementwise_maxnum`.
223+
- No longer crashing on invalid Objective-C categories and extensions when
224+
dumping the AST as JSON. (#GH137320)
223225

224226
New Compiler Flags
225227
------------------

clang/lib/AST/Mangle.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,11 @@ void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
367367
}
368368
OS << (MD->isInstanceMethod() ? '-' : '+') << '[';
369369
if (const auto *CID = MD->getCategory()) {
370-
OS << CID->getClassInterface()->getName();
371-
if (includeCategoryNamespace) {
372-
OS << '(' << *CID << ')';
370+
if (const auto *CI = CID->getClassInterface()) {
371+
OS << CI->getName();
372+
if (includeCategoryNamespace) {
373+
OS << '(' << *CID << ')';
374+
}
373375
}
374376
} else if (const auto *CD =
375377
dyn_cast<ObjCContainerDecl>(MD->getDeclContext())) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: not %clang_cc1 -ast-dump=json %s 2>&1 | FileCheck %s
2+
3+
// Ensure that dumping this does not crash when emitting the mangled name.
4+
// See GH137320 for details.
5+
// Note, this file does not compile and so we also check the error.
6+
7+
@interface SomeClass (SomeExtension)
8+
+ (void)someMethod;
9+
@end
10+
11+
// CHECK: error: cannot find interface declaration for 'SomeClass'
12+
13+
// CHECK: "name": "someMethod"
14+
// CHECK-NEXT: "mangledName": "+[ someMethod]",

0 commit comments

Comments
 (0)