[clang] [clang] Fix RecursiveASTVisitor to traverse the exception parameter in ObjCAtCatchStmt. (PR #216125)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 13 21:23:33 PDT 2026
https://github.com/dmaclach updated https://github.com/llvm/llvm-project/pull/216125
>From f1f108131eb094a95baa42893797e11b94da4277 Mon Sep 17 00:00:00 2001
From: Dave MacLachlan <dmaclach at gmail.com>
Date: Thu, 13 Aug 2026 10:38:55 -0700
Subject: [PATCH 1/4] [clang] Fix RecursiveASTVisitor to traverse the exception
parameter in ObjCAtCatchStmt.
Ensures that the catch parameter declaration (the exception variable) in an Objective-C @catch block is visited during AST traversal. Previously, this declaration was skipped. A unit test has been added to verify the fix.
---
clang/include/clang/AST/RecursiveASTVisitor.h | 6 +++++-
.../Tooling/RecursiveASTVisitorTestDeclVisitor.cpp | 9 +++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 617990b82edca..12204dcfb5938 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -2575,6 +2575,11 @@ DEF_TRAVERSE_STMT(CXXCatchStmt, {
// children() iterates over the handler block.
})
+DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {
+ TRY_TO(TraverseDecl(S->getCatchParamDecl()));
+ // children() iterates over the handler block.
+})
+
DEF_TRAVERSE_STMT(DeclStmt, {
for (auto *I : S->decls()) {
TRY_TO(TraverseDecl(I));
@@ -2604,7 +2609,6 @@ DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
DEF_TRAVERSE_STMT(LabelStmt, {})
DEF_TRAVERSE_STMT(AttributedStmt, {})
DEF_TRAVERSE_STMT(NullStmt, {})
-DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})
DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {})
DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {})
diff --git a/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
index eed016e9ee7c2..2af0ecb40b225 100644
--- a/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
+++ b/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
@@ -134,4 +134,13 @@ TEST(RecursiveASTVisitor, NoRecursionInSelfFriend) {
"vector_iterator<int> it_int;\n"));
}
+TEST(RecursiveASTVisitor, VisitsObjCAtCatchStmtExceptionVariable) {
+ VarDeclVisitor Visitor;
+ Visitor.ExpectMatch("e", 2, 28);
+ EXPECT_TRUE(Visitor.runOver(
+ "@interface NSException; @end\n"
+ "void f() { @try {} @catch (NSException *e) {} }",
+ VarDeclVisitor::Lang_OBJC));
+}
+
} // end anonymous namespace
>From a554739f5e4ea5ff450b7955105bba09c15917a0 Mon Sep 17 00:00:00 2001
From: Dave MacLachlan <dmaclach at gmail.com>
Date: Thu, 13 Aug 2026 10:47:30 -0700
Subject: [PATCH 2/4] Fixed up formatting.
---
.../Tooling/RecursiveASTVisitorTestDeclVisitor.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
index 2af0ecb40b225..fe842431d89bc 100644
--- a/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
+++ b/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
@@ -137,10 +137,9 @@ TEST(RecursiveASTVisitor, NoRecursionInSelfFriend) {
TEST(RecursiveASTVisitor, VisitsObjCAtCatchStmtExceptionVariable) {
VarDeclVisitor Visitor;
Visitor.ExpectMatch("e", 2, 28);
- EXPECT_TRUE(Visitor.runOver(
- "@interface NSException; @end\n"
- "void f() { @try {} @catch (NSException *e) {} }",
- VarDeclVisitor::Lang_OBJC));
+ EXPECT_TRUE(Visitor.runOver("@interface NSException; @end\n"
+ "void f() { @try {} @catch (NSException *e) {} }",
+ VarDeclVisitor::Lang_OBJC));
}
} // end anonymous namespace
>From 10f19a5413b143a66e1db059330389bea2263e56 Mon Sep 17 00:00:00 2001
From: Dave MacLachlan <dmaclach at gmail.com>
Date: Thu, 13 Aug 2026 21:06:06 -0700
Subject: [PATCH 3/4] Responded to comments: - Added test to demonstrate
unguarded-availability issues that could be flagged with this change. -
Added release note to highlight change.
---
clang/docs/ReleaseNotes.md | 5 +++++
clang/test/SemaObjC/unguarded-availability.m | 13 ++++++++++---
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e4a6f72f8fec5..333f78d0faf40 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -177,6 +177,11 @@ features cannot lower the translation-unit ABI level;
### Objective-C Language Changes
+- Fixed an issue where AST consumers based on `RecursiveASTVisitor`
+ (such as `clangd`, `include-cleaner`, and static analyzers) would bypass the
+ exception parameter declaration (and its type) inside Objective-C `@catch`
+ blocks.
+
### Non-comprehensive list of changes in this release
- Clang now allows GNU computed `goto` extension in `constexpr` functions, matching the relaxed
diff --git a/clang/test/SemaObjC/unguarded-availability.m b/clang/test/SemaObjC/unguarded-availability.m
index ecd91990174ae..8c7b81295463d 100644
--- a/clang/test/SemaObjC/unguarded-availability.m
+++ b/clang/test/SemaObjC/unguarded-availability.m
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -Wunguarded-availability -fblocks -fsyntax-only -verify %s
-// RUN: %clang_cc1 -xobjective-c++ -std=c++11 -DOBJCPP -triple x86_64-apple-macosx10.9 -Wunguarded-availability -fblocks -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -Wunguarded-availability -fblocks -fobjc-exceptions -fsyntax-only -verify %s
+// RUN: %clang_cc1 -xobjective-c++ -std=c++11 -DOBJCPP -triple x86_64-apple-macosx10.9 -Wunguarded-availability -fblocks -fobjc-exceptions -fsyntax-only -verify %s
#define AVAILABLE_10_0 __attribute__((availability(macos, introduced = 10.0)))
#define AVAILABLE_10_11 __attribute__((availability(macos, introduced = 10.11)))
@@ -72,7 +72,8 @@ void use_typedef(void) {
}
__attribute__((objc_root_class))
-AVAILABLE_10_11 @interface Class_10_11 { // expected-note{{annotate 'Class_10_11' with an availability attribute to silence}}
+AVAILABLE_10_11 @interface Class_10_11 { // expected-note{{annotate 'Class_10_11' with an availability attribute to silence}} \
+ // expected-note {{'Class_10_11' has been marked as being introduced in macOS 10.11 here, but the deployment target is macOS 10.9}}
int_10_11 foo;
int_10_12 bar; // expected-warning {{'int_10_12' is only available on macOS 10.12 or newer}}
}
@@ -407,3 +408,9 @@ void is_constructor(void) {
void is_destructor(void) {
func_10_11(); // expected-warning{{'func_10_11' is only available on macOS 10.11 or newer}} expected-note{{enclose 'func_10_11' in an @available check to silence this warning}}
}
+
+void test_catch(void) {
+ @try {
+ } @catch (Class_10_11 *e) { // expected-warning {{'Class_10_11' is only available on macOS 10.11 or newer}} expected-note {{enclose 'Class_10_11' in an @available check to silence this warning}}
+ }
+}
>From bbeb9d460dfa6a040a131b5d915dcf5d1b76bc6e Mon Sep 17 00:00:00 2001
From: Dave MacLachlan <dmaclach at gmail.com>
Date: Thu, 13 Aug 2026 21:22:56 -0700
Subject: [PATCH 4/4] Moved release note to `Objective-C Specific Potentially
Breaking Changes`
---
clang/docs/ReleaseNotes.md | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 333f78d0faf40..b64b32aa09e33 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -54,6 +54,13 @@ in a future version of Clang.
### C++ Specific Potentially Breaking Changes
+### Objective-C Specific Potentially Breaking Changes
+
+- Fixed an issue where AST consumers based on `RecursiveASTVisitor` would bypass
+ the exception parameter declaration inside Objective-C `@catch` blocks. This
+ could cause tooling that previously ignored the parameter declaration to now
+ find valid issues. (#GH212564)
+
### ABI Changes in This Version
- Except on PlayStation, Clang now derives the x86-64 System V AVX ABI level
@@ -177,11 +184,6 @@ features cannot lower the translation-unit ABI level;
### Objective-C Language Changes
-- Fixed an issue where AST consumers based on `RecursiveASTVisitor`
- (such as `clangd`, `include-cleaner`, and static analyzers) would bypass the
- exception parameter declaration (and its type) inside Objective-C `@catch`
- blocks.
-
### Non-comprehensive list of changes in this release
- Clang now allows GNU computed `goto` extension in `constexpr` functions, matching the relaxed
More information about the cfe-commits
mailing list