This is an archive of the discontinued LLVM Phabricator instance.

[clang][dataflow] Generalise match switch utility to other AST types and add a `CFGMatchSwitch` which currently handles `CFGStmt` and `CFGInitializer`.
ClosedPublic

Authored by wyt on Aug 10 2022, 1:09 PM.

Details

Summary

MatchSwitch currently takes in matchers and functions for the Stmt class.

This patch generalises the match switch utility (renamed to ASTMatchSwitch) to work for different AST node types by introducing a template argument which is the base type for the AST nodes that the match switch will handle.

A CFGMatchSwitch is introduced as a wrapper around multiple ASTMatchSwitchs for different base types. It works by unwrapping CFGElements into their contained AST nodes and passing the nodes to the relevant ASTMatchSwitch. The CFGMatchSwitch currently only handles CFGStmt and CFGInitializer.

Depends On D131614

Diff Detail

Event Timeline

wyt created this revision.Aug 10 2022, 1:09 PM
Herald added a project: Restricted Project. · View Herald Transcript
wyt requested review of this revision.Aug 10 2022, 1:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 10 2022, 1:09 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
wyt edited the summary of this revision. (Show Details)Aug 10 2022, 1:16 PM
wyt added reviewers: hlopko, gribozavr2, sgatev, ymandel, samestep.
wyt edited the summary of this revision. (Show Details)Aug 11 2022, 1:03 AM

LGTM so far, please finish the patch and tests.

wyt updated this revision to Diff 452958.Aug 16 2022, 4:59 AM

Move implementation to CFGMatchSwitch.h, add tests.

wyt updated this revision to Diff 452965.Aug 16 2022, 5:23 AM

Propagate change from parent patch.

wyt edited the summary of this revision. (Show Details)Aug 16 2022, 5:25 AM
wyt added a reviewer: xazax.hun.
wyt updated this revision to Diff 452966.Aug 16 2022, 5:28 AM

Fix incorrect change.

gribozavr2 accepted this revision.Aug 16 2022, 8:45 AM
gribozavr2 added inline comments.
clang/include/clang/Analysis/FlowSensitive/CFGMatchSwitch.h
54–55
68–69
clang/include/clang/Analysis/FlowSensitive/MatchSwitch.h
97–98

Can we keep doing this?

109

Ditto.

clang/unittests/Analysis/FlowSensitive/CFGMatchSwitchTest.cpp
35–36
This revision is now accepted and ready to land.Aug 16 2022, 8:45 AM
wyt updated this revision to Diff 453704.Aug 18 2022, 10:36 AM
wyt marked 5 inline comments as done.

Address comments.

gribozavr2 accepted this revision.Aug 18 2022, 11:35 AM
wyt updated this revision to Diff 454534.Aug 22 2022, 9:15 AM

Rebase.

gribozavr2 accepted this revision.Aug 22 2022, 9:46 AM
sgatev added inline comments.Aug 23 2022, 2:04 AM
clang/include/clang/Analysis/FlowSensitive/CFGMatchSwitch.h
25–26

These seem unnecessary.

29

This seems unnecessary.

31

This seems unnecessary.

33

This seems unnecessary.

56

Is the outer move necessary? Also, aren't we supposed to assign the result back to StmtBuilder? Same comment for InitBuilder below.

clang/include/clang/Analysis/FlowSensitive/MatchSwitch.h
1

Let's add a FIXME to rename the file to ASTMatchSwitch.h and update the comment below.

51–64

What do you think about calling these MatchSwitchMatcher and MatchSwitchAction?

69
95–100

Let's add a static assert.

95–100
clang/unittests/Analysis/FlowSensitive/CFGMatchSwitchTest.cpp
13

This seems unnecessary.

15–16

These seem unnecessary.

21–25

These seem unnecessary.

wyt updated this revision to Diff 454868.Aug 23 2022, 9:08 AM
wyt marked 13 inline comments as done.

Address comments.

clang/include/clang/Analysis/FlowSensitive/CFGMatchSwitch.h
56

Is the outer move necessary? Also, aren't we supposed to assign the result back to StmtBuilder? Same comment for InitBuilder below.

Yeap the outer move was unnecessary.
The self-assignment wasn't necessary since the CaseOf modifies the object it is called on, in fact the self-assignment led to the destroyal of contents inside the builder.

wyt updated this revision to Diff 454869.Aug 23 2022, 9:16 AM

Fix comments for consistency.

sgatev accepted this revision.Aug 26 2022, 4:09 AM
gribozavr2 accepted this revision.Aug 26 2022, 4:46 AM
wyt reopened this revision.Aug 31 2022, 11:53 AM
This revision is now accepted and ready to land.Aug 31 2022, 11:53 AM
wyt updated this revision to Diff 457052.Aug 31 2022, 12:10 PM

Use u suffix to declare constants as unsigned in CFGMatchSwitchTest.cpp.

gribozavr2 accepted this revision.Aug 31 2022, 1:25 PM