Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang-tidy check to combine locals into points #41238

Merged
merged 19 commits into from
Jun 13, 2020

Conversation

jbytheway
Copy link
Contributor

@jbytheway jbytheway commented Jun 12, 2020

Summary

SUMMARY: Infrastructure "Enforce use of points as local variables"

Purpose of change

For better type safety and code clarity.

Working towards #32017.

Describe the solution

Add a new clang-tidy check that looks for pairs (or triples) of local variables that should be merged into points (or tripoints), and automate that conversion.

On average, I think these changes are a substantial improvement; lots of cases where points were being unnecessarily decomposed and recomposed are now performed as point arithmetic directly.

Describe alternatives you've considered

Doing some subset of the changes by hand.

Testing

Most of the changes are automated, so should be low-risk.

Ran the unit tests.

Additional context

I'm expecting some compiler errors from the old clang we support (3.8), so don't merge until that looks good (it's the first Travis CI job).

@jbytheway jbytheway force-pushed the combine_locals_into_points branch from 98ea6e7 to 328fdda Compare June 12, 2020 12:02
jbytheway added 17 commits June 12, 2020 08:27
This is an initial implementation of the idea.
Add a test for this case, which happens to occur in the CDDA code.
When naming the new point variable, remove leading or trailing
underscores from the root name, so that e.g. win_x, win_y become win.x
and win.y (rather than win_.x and win_.y).
When naming the new point variable, try to avoid existing names.

This check won't avoid all existing names, but it's a start.
The name derivation rules could lead to naming a variable e.g. '1',
which is illegal.
The new check is quite likely to conflict with itself if making multiple
edits to a single function, so prevent it from doing so.
When merging some ints into a point, if the ints were static or
constexpr then the point must be also.
Doing this one separately because it required a little manual
intervention.
Doing headers separately to avoid edit collisions.
@jbytheway jbytheway force-pushed the combine_locals_into_points branch 3 times, most recently from 3d0b65b to 6d1d88f Compare June 12, 2020 12:58
@jbytheway jbytheway force-pushed the combine_locals_into_points branch from 6d1d88f to 9fc62e1 Compare June 12, 2020 13:34
@jbytheway
Copy link
Contributor Author

OK, looks like the clang-3.8 issues are now resolved.

@anothersimulacrum anothersimulacrum added [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Code: Tests Measurement, self-control, statistics, balancing. labels Jun 12, 2020
@kevingranade kevingranade merged commit c2b5b38 into CleverRaven:master Jun 13, 2020
@FeepingCreature
Copy link
Contributor

FeepingCreature commented Jun 13, 2020

Is the src/mapbuffer.cpp change and changes like it valid? I thought the evaluation order of side effects in function calls was arbitrary.

I think you need to write it like tripoint loc{ jsin.get_int(), jsin.get_int(), jsin.get_int() }; to get left-to-right evaluation?

(I get map corruption around that line...)

edit: The aforementioned change makes the load corruption go away.

edit: nm fixed

@jbytheway jbytheway deleted the combine_locals_into_points branch June 14, 2020 01:22
@jbytheway
Copy link
Contributor Author

Thanks for catching that. Indeed it was a bad change. Did you see other similar changes?

@FeepingCreature
Copy link
Contributor

Didn't notice any on a cursory grepping. I'll keep my eyes open for further crashes. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Code: Tests Measurement, self-control, statistics, balancing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants