This is an archive of the discontinued LLVM Phabricator instance.

[ASTMatchers] Add a new matcher for callee declarations of Obj-C message expressions
ClosedPublic

Authored by ziqingluo-90 on Jul 8 2022, 2:15 PM.

Details

Summary

For an Obj-C message expression [o m], the adding matcher will match the declaration of the method m. This matcher is the Obj-C counterpart of the existing callee ASTMatcher which matches function/method declarations for C/C++ calls. So I think this matcher is general enough to be added to the ASTMatcher.h.

Diff Detail

Event Timeline

ziqingluo-90 created this revision.Jul 8 2022, 2:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 8 2022, 2:15 PM
ziqingluo-90 requested review of this revision.Jul 8 2022, 2:15 PM
njames93 accepted this revision.Jul 9 2022, 12:11 AM
njames93 retitled this revision from Adding a new ASTMatcher for callee declarations of Obj-C message expressions to [ASTMatchers] Add a new matcher for callee declarations of Obj-C message expressions.

LGTM

This revision is now accepted and ready to land.Jul 9 2022, 12:11 AM
NoQ added inline comments.Jul 10 2022, 4:12 PM
clang/include/clang/ASTMatchers/ASTMatchers.h
3841

Nitpick carryover: needs capital letter and .

aaron.ballman added inline comments.Jul 11 2022, 5:21 AM
clang/include/clang/ASTMatchers/ASTMatchers.h
3841

Don't forget to regen the docs when you fix this, too. :-)

3853

Is there a reason why we want a separate matcher here instead of overloading callee()?

ziqingluo-90 added inline comments.Jul 11 2022, 11:42 AM
clang/include/clang/ASTMatchers/ASTMatchers.h
3853

This is a good question!

callee has already been overloaded to accept both Matcher<Decl> and Matcher<Stmt> as the parameter. In my case, I will need callee to be polymorphic in returning both Matcher<CallExpr> and Matcher<ObjCMessageExpr> types. So that will end up in 4 definitions of callee, one of which has different return type and parameter type from one of the others.

Is this achievable? I know I can overload parameters or make return types polymorphic, but can I mix them together?

ziqingluo-90 added inline comments.Jul 11 2022, 5:15 PM
clang/include/clang/ASTMatchers/ASTMatchers.h
3853

I figured it out. I will soon update this patch to simply overload callee

Taking @aaron.ballman 's advice to overload callee instead of creating a new matcher. Avoid to bloat ASTMatchers.h.

Sorry to the reviewers that have to review this patch again.

Thank you, this is looking really promising!

Can you also add a test to unittests/ASTMatchers/Dynamic/RegistryTest.cpp to the TEST_F(RegistryTest, OverloadedMatchers) test which tests the behavior of callee() to make sure it works from dynamic matchers? If that test fails, I think you may need to update lib/ASTMatchers/Dynamic/Registry.cpp to add a REGISTER_OVERLOADED_3 that is used by both callee and equals.

clang/include/clang/ASTMatchers/ASTMatchers.h
3871

This way you get an assert if someone adds another type and forgets to update the rest of the body.

Added a test to unittests/ASTMatchers/Dynamic/RegistryTest.cpp and confirms that the overloaded callee still works with dynamic matchers

aaron.ballman accepted this revision.Jul 14 2022, 5:01 AM

LGTM, thank you!

Thanks @aaron.ballman , I plan to commit this patch on Monday (18th July 2022).