This is an archive of the discontinued LLVM Phabricator instance.

[analyzer][NFC] Move away from using raw-for loops inside StaticAnalyzer
ClosedPublic

Authored by steakhal on Jul 3 2023, 12:42 AM.

Details

Summary

I'm involved with the Static Analyzer for the most part.
I think we should embrace newer language standard features and gradually move forward.

Diff Detail

Event Timeline

steakhal created this revision.Jul 3 2023, 12:42 AM
Herald added a project: Restricted Project. · View Herald Transcript
steakhal requested review of this revision.Jul 3 2023, 12:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 3 2023, 12:42 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

There remained a few more cases using raw-for loops, but those were slightly more tricky to uplift as they either use weird iterators or employ some fancy look-ahead/behind or iterate in parallel over different sequences etc.
I'll also make a measurement to confirm it doesn't change any reports.

Szelethus accepted this revision.Jul 3 2023, 12:58 AM

But this renders much of my C++98 knowledge obsolete!!!!!11

This revision is now accepted and ready to land.Jul 3 2023, 12:58 AM

The transformations were done by hand. So I'd encourage you all to have a look and find places where it breaks. Also, it might have personal biases about the style I settled with. Feel free to challenge.

xazax.hun accepted this revision.Jul 4 2023, 3:18 AM
xazax.hun added inline comments.
clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h
501

Is this just a dummy to make sure this satisfies some concepts? I think I comment might be helpful in that case, people might find a noop dereference operator confusing. Same for some other places.

clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
752–754

Do we need these with the addition of referenced_vars?

clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
634–637

Should we remove these?

clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
194–196
clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp
54

Hm, I wonder whether we actually want to use unique_ptrs to make the ownership explicit. Feel free to leave as is in this PR.

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
172–176

Might be an issue with Phab, but indent looks strange to me here.

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
1195–1196

Ha about using capture_inits?

I don't have concrete remarks, but I would like to note that I'm happy to see this cleanup :)

steakhal planned changes to this revision.Jul 4 2023, 5:26 AM
steakhal marked an inline comment as done.
steakhal added inline comments.
clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h
501

Yes. I'll add a comment.
This falls into the "weird" iterators category.

clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
752–754

I no longer remember where, but yes. But for sure I went through these to see if I could remove them.

clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
634–637

I'm pretty sure we need these somewhere.
One prominent place is where we dump these into JSONs. We do some fancy iteration there. I'm not sure that is the reason for this particular case.
I'll give it another look to confirm.

clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
194–196

Makes sense.

clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp
54

The ownership model of these consumers is so messed up.
It's not that simple to fix. I was bitten by dangling consumers/graphs so many times now only counting last year.
So, yea. Maybe one day.

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
172–176

Thanks for catching this. Indeed. I'll have a look. Probably I messed something up.

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
1195–1196

I would count this as a "fancy" iteration as we increment/advance more logical sequences together.

steakhal updated this revision to Diff 537106.Jul 4 2023, 8:15 AM
steakhal marked 7 inline comments as done.
This revision is now accepted and ready to land.Jul 4 2023, 8:15 AM

Fixed remarks, cleaned up residual includes, rebased.
Should be read to land.
Final thoughts?

clang/include/clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h
501

Added comments to both places.

clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h
752–754

Now I remember.
The referenced_vars is implemented as llvm::make_range(referenced_vars_begin(), referenced_vars_end()), thus I need those functions as they do quite a bit of work if you check.
So, I kept them.

clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
634–637

This was just an oversight. Removed. Thanks!

clang/lib/StaticAnalyzer/Checkers/TrustNonnullChecker.cpp
72

Oh, I didn't know this is a word :D

clang/lib/StaticAnalyzer/Core/Environment.cpp
29

Removed this extra include.

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
172–176

Clang-format went crazy, and I'm using clang-format on save, thus messed this up.
Fixed up manually. Should be good now.

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
1195–1196

I had a better idea and llvm::zip all these together.
Check it out, and tell me if you like it more ;)
I certainly do.

xazax.hun accepted this revision.Jul 4 2023, 8:18 AM

I love it, I think we can land this as is. If there are further comments, we can address those in follow-up PRs.

clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
1195–1196

Nice!

This revision was landed with ongoing or failed builds.Jul 4 2023, 11:56 PM
This revision was automatically updated to reflect the committed changes.
steakhal added inline comments.Jul 5 2023, 12:34 AM
clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp
54