This is an archive of the discontinued LLVM Phabricator instance.

[clang][dataflow] Refine matching of optional types to anchor at top level.
ClosedPublic

Authored by ymandel on Apr 14 2023, 7:47 AM.

Details

Summary

This patch refines the matching of the relevant optional types to anchor on the
global namespace. Previously, we could match anything with the right name
(e.g. base::Optional) even if nested within other namespaces. This over
matching resulted in an assertion violation when _different_ base::Optional
was encountered nested inside another namespace.

Fixes issue #57036.

Diff Detail

Event Timeline

ymandel created this revision.Apr 14 2023, 7:47 AM
Herald added a project: Restricted Project. · View Herald Transcript
ymandel requested review of this revision.Apr 14 2023, 7:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2023, 7:47 AM
xazax.hun added inline comments.Apr 14 2023, 10:09 AM
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
261

Why do we need two places to define what is an optional type? Would it be possible to somehow reuse the Declaration matcher on the RecordDecel here? Or would that be bad for performance?

ymandel added inline comments.Apr 14 2023, 10:19 AM
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
261

Good point. I dismissed reusing the matcher here because of performance concerns -- setting up the match is expensive (in the constant-time sense but still a lot of overhead). But, we're already using matching quite a lot so I suspect it doesn't matter. Still, what about the reverse -- use this predicate in the matcher, which is faster while still ensuring consistent results?

That said, what I really want is for the matcher logic to be made available outside of the matcher framework -- I think NamedDecl should have the core logic (parallel to getQualifiedNameAsString) and that could be used elsewhere, including the matcher.

ymandel added inline comments.Apr 14 2023, 10:21 AM
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
261

regardless, the matcher is wrong (if we keep it): it should be hasAnyName rather than anyOf and lots of hasName. not sure how I never caught that earlier...

ymandel updated this revision to Diff 513727.Apr 14 2023, 1:28 PM

Improves the matcher and adds (missing) std qualifier to one of the names.

ymandel marked an inline comment as done.Apr 14 2023, 1:29 PM
ymandel added inline comments.
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
261

I've updated the matcher but otherwise left the code alone. I will send a followup patch that shares the code now that I understand how to avoid the mention of the libc++ internal names.

xazax.hun accepted this revision.Apr 14 2023, 1:51 PM

LG! Thanks!

This revision is now accepted and ready to land.Apr 14 2023, 1:51 PM
gribozavr2 accepted this revision.Apr 17 2023, 10:04 AM
This revision was landed with ongoing or failed builds.Apr 17 2023, 11:03 AM
This revision was automatically updated to reflect the committed changes.