This is an archive of the discontinued LLVM Phabricator instance.

[-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize the code
ClosedPublic

Authored by ziqingluo-90 on Jul 27 2023, 12:02 PM.

Details

Summary

Slightly refactor and optimize the code in preparation for implementing grouping parameters for a single fix-it.

Diff Detail

Event Timeline

ziqingluo-90 created this revision.Jul 27 2023, 12:02 PM
Herald added a project: Restricted Project. · View Herald Transcript
ziqingluo-90 requested review of this revision.Jul 27 2023, 12:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 12:02 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ziqingluo-90 added inline comments.Jul 27 2023, 12:25 PM
clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
26

Use the VariableGroupsManager class as an interface to clients. It hides the details about how groups are implemented (which may involve several containers).

clang/lib/Analysis/UnsafeBufferUsage.cpp
1182

To make the order of variables in every group deterministic.

2313

Instead of calling fixVariable to generate fix-its for group mates, directly copying those fix-its from the FixItsForVariable container.

At this point, FixItsForVariable is complete. That is, it maps every variable to its own fix-its (excluding fix-its of group mates).

2332

An implementation of VariableGroupsManager: keep a unique copy of each group in Groups and use their indexes for access.

Note that getGroupOfVar assumes that Var must in the map. This is guaranteed by the changed made in D155524---only "Fixables" associated to variables in the final graph(es) will stay and be fixed. The key set of the VarGrpMap is the set of variables in the final graph(es). Therefore, after the graph(es) are computed and "Fixables" are pruned, VarGrpMap is the variable domain that we will deal with.

2470

Use SetVector to make order deterministic.

2530

I removed this guard so that VarGrpMap is complete---it has an entry for every variable that we need to deal with later.

2555

Mapping variables to group indexes instead of groups themselves to avoid copies.

clang/lib/Sema/AnalysisBasedWarnings.cpp
2180

Factor this procedure out to a method for easy re-use later.

t-rasmud added inline comments.Aug 1 2023, 2:14 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
2313

Do you mean "including fix-its of group mates"? If not, I might have misunderstood the changes and will take a look again.

2555

I think this is a really clever optimization and believe it would save a bunch of time and space in code having significant variable grouping.

NoQ added inline comments.Aug 1 2023, 3:13 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
1182

Would it make sense to immediately clean up the regexps in tests then?

2479–2480

These objects don't really define any move semantics, so std::move is probably redundant.

Maybe structured binding instead?

t-rasmud added inline comments.Aug 1 2023, 3:17 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
1182

@NoQ I had an offline discussion with @ziqingluo-90 about this and we agreed (IIRC) that I would clean up the tests since I added those regexps. I plan to create a patch as soon as this lands. Does that make sense?

NoQ added inline comments.Aug 1 2023, 3:28 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
1182

Yes sure SGTM!

ziqingluo-90 added inline comments.Aug 2 2023, 2:02 PM
clang/lib/Analysis/UnsafeBufferUsage.cpp
2313

No.
FixItsForVariable is a map from variables to their own fix-its, excluding fix-its of group mates.
FinalFixItsForVariable is a map from variables to the set of fix-its that fix the whole group.

The construction of FinalFixItsForVariable reads FixItsForVariable.

Maybe I should have better names for these two variables.

t-rasmud accepted this revision.Aug 2 2023, 2:12 PM

LGTM!

clang/lib/Analysis/UnsafeBufferUsage.cpp
2313

Ah! That makes sense, I misread the FixItsForVariable as FinalFixItsForVariable in you comment. Thank you for explaining.

This revision is now accepted and ready to land.Aug 2 2023, 2:12 PM
NoQ accepted this revision.Aug 10 2023, 1:29 PM

LGTM!

This revision was landed with ongoing or failed builds.Aug 17 2023, 4:25 PM
This revision was automatically updated to reflect the committed changes.