This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Avoid C arrays check
ClosedPublic

Authored by lebedev.ri on Oct 26 2018, 10:29 AM.

Details

Summary

PR39224
As discussed, we can't always do the transform automatically due to that array-to-pointer decay of C array.
In order to detect whether we can do said transform, we'd need to be able to see all usages of said array,
which is, i would say, rather impossible if e.g. it is in the header.
Thus right now no fixit exists.

Exceptions: extern "C" code.

References:

Diff Detail

Repository
rL LLVM

Event Timeline

lebedev.ri created this revision.Oct 26 2018, 10:29 AM
lebedev.ri edited the summary of this revision. (Show Details)Oct 26 2018, 10:29 AM

modernize would be a fitting module, wouldn't it?

JonasToth added inline comments.Oct 26 2018, 10:53 AM
docs/clang-tidy/checks/list.rst
13 ↗(On Diff #171315)

spurious change

154 ↗(On Diff #171315)

here as well

docs/clang-tidy/checks/misc-avoid-c-arrays.rst
51 ↗(On Diff #171315)

The last two lines do not add value, i think you can safely remove them

test/clang-tidy/misc-avoid-c-arrays.cpp
14 ↗(On Diff #171315)

Please add a case with templates, where T itself is the array type, maybe there are other fancy template tricks that could create an array implictly?

May be this check belongs to modernize?

docs/clang-tidy/checks/misc-avoid-c-arrays.rst
6 ↗(On Diff #171315)

Finds.

9 ↗(On Diff #171315)

However

30 ↗(On Diff #171315)

Please remove ellipsis. and newline.

lebedev.ri added inline comments.Oct 26 2018, 12:15 PM
test/clang-tidy/misc-avoid-c-arrays.cpp
14 ↗(On Diff #171315)

Oh i see, array<int[4], 2> is not diagnosed, that is where it gets interesting :S
So i need to match type(arrayType()), and then find the location to complain about
(the type does not seem to have it).

JonasToth added inline comments.Oct 26 2018, 1:31 PM
test/clang-tidy/misc-avoid-c-arrays.cpp
14 ↗(On Diff #171315)

you could leave it as known limitation as well.
If the type T is then diagnosed as array creation through template-instantiation the warning is still valid.

lebedev.ri marked 9 inline comments as done.
lebedev.ri edited the summary of this revision. (Show Details)
  • Moved into modernize
  • Detect based on TypeLoc, which means we now catch *everything*, including using-declarations. Note the unique_ptr example.

Will be good idea to add aliases at least for existing modules.

Will be good idea to add aliases at least for existing modules.

Added CPPCG, HICPP aliases.

Eugene.Zelenko added inline comments.Oct 28 2018, 6:24 AM
docs/clang-tidy/checks/cppcoreguidelines-avoid-c-arrays.rst
6 ↗(On Diff #171423)

Please make same length as header.

docs/clang-tidy/checks/hicpp-avoid-c-arrays.rst
8 ↗(On Diff #171423)

Please fill as much as possible to 80 character. Probably same for other alias.

docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
6 ↗(On Diff #171423)

Please fill as much as possible to 80 character. Same for other alias.

lebedev.ri marked 3 inline comments as done.

Docs fixes.

docs/clang-tidy/checks/cppcoreguidelines-avoid-c-arrays.rst
6 ↗(On Diff #171423)

Whoops, i noticed that in the main doc, but not here, sorry.

JonasToth added inline comments.Oct 29 2018, 3:09 AM
clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
84 ↗(On Diff #171425)

please conserve the alphabetical order here

clang-tidy/hicpp/HICPPTidyModule.cpp
54 ↗(On Diff #171425)

i would this one should be before avoid-goto.

clang-tidy/modernize/AvoidCArraysCheck.cpp
44 ↗(On Diff #171425)

What about struct-decls that are externC?

docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
15 ↗(On Diff #171425)

Not sure if we want the rational of not providing fixits here in this detail. Maybe it's better to formulate it as fix-it are potentially dangerous in header files and are therefor not emitted right now? Some places would be save to transform and we might want to provide these in the future.

test/clang-tidy/modernize-avoid-c-arrays.cpp
48 ↗(On Diff #171425)

Please add a test for extern c structs, maybe even in the form extern "C" struct Foo { ... };.

Additionally to that, maybe the opposite case should be tested too: extern "C++" in C-Code.

lebedev.ri marked 4 inline comments as done.

Address review notes.

clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
84 ↗(On Diff #171425)

Sorted all the CheckFactories.registerCheck<>(); lines.

clang-tidy/modernize/AvoidCArraysCheck.cpp
44 ↗(On Diff #171425)

Hm, what is a struct-decl?

JonasToth added inline comments.Nov 4 2018, 4:23 AM
clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
84 ↗(On Diff #171425)

the avoid-* checks seem to be displaced now.

clang-tidy/modernize/AvoidCArraysCheck.cpp
44 ↗(On Diff #171425)

we chatted in irc, but for reference

extern "C" {
struct Foo {

};
}
44 ↗(On Diff #171425)

you can mark all comments as done here, as they are implemented.

test/clang-tidy/modernize-avoid-c-arrays.cpp
3 ↗(On Diff #171747)

what about variable-length arrays? I think they should be diagnosed as well, but plain replacement with std::array would be incorrect, more a std::vector would fit.

lebedev.ri marked 4 inline comments as done.Nov 4 2018, 5:04 AM

(only comments, patch to follow)

clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
84 ↗(On Diff #171425)

How it should be sorted? By the new check name? Or the internal class name?
If it is not the latter, then sorting is really problematic as it can't be automatized.

test/clang-tidy/modernize-avoid-c-arrays.cpp
10–11 ↗(On Diff #171747)

This is VLA.
Note that VLA are C99, they (thankfully!) don't exist in C++ standard at all.
Looks like variableArrayType() should single-out this, so let's see..

lebedev.ri updated this revision to Diff 172520.Nov 4 2018, 6:29 AM
lebedev.ri marked 6 inline comments as done.

Better diag for VLA arrays.

clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
84 ↗(On Diff #171425)

Maybe now is better?

JonasToth added inline comments.Nov 4 2018, 7:25 AM
clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
84 ↗(On Diff #171425)

yes, thats the correct position :)

JonasToth added inline comments.Nov 10 2018, 4:48 AM
clang-tidy/modernize/AvoidCArraysCheck.cpp
26 ↗(On Diff #172520)

Nit: these parens are superflous

64 ↗(On Diff #172520)

Why isa<> and not isVariableArrayType() (does it exist?)
isa<> would not resolve typedefs, but I think they should be considered.

lebedev.ri marked 2 inline comments as done.

Use isVariableArrayType()

clang-tidy/modernize/AvoidCArraysCheck.cpp
64 ↗(On Diff #172520)

Hmm, interesting point.
I'm not sure ATM how to expose it in tests, but it counts as a cleanup anyway.

from my side mostly ok. I think the typedef points should be clarified, but I dont see more issues.

test/clang-tidy/modernize-avoid-c-arrays.cpp
32 ↗(On Diff #173514)

What happens for a variable of type k? Does the check warn, or only in the typedef?

lebedev.ri added inline comments.Nov 10 2018, 7:34 AM
test/clang-tidy/modernize-avoid-c-arrays.cpp
32 ↗(On Diff #173514)

Only here, on the typeloc, like the Semi-FIXME i just added there ^ states.

JonasToth accepted this revision.Nov 10 2018, 7:40 AM

LGTM.
Please give other reviewers a chance to take a look at it too.

This revision is now accepted and ready to land.Nov 10 2018, 7:40 AM

*Maybe* we should be actually diagnosing the each actual declaration, not just the typeloc.
Else if you use one typedef, and // NOLINT it, you will silence it for all the decls that use it.

LGTM.
Please give other reviewers a chance to take a look at it too.

Thank you for the review!
I will keep this open for a few more days.

*Maybe* we should be actually diagnosing the each actual declaration, not just the typeloc.
Else if you use one typedef, and // NOLINT it, you will silence it for all the decls that use it.

As my BB is getting closer to being useful, we can gather some statistics first and decide afterwards :)

LGTM.
Please give other reviewers a chance to take a look at it too.

Thank you for the review!
I will keep this open for a few more days.

np, i think until monday is enough.

This revision was automatically updated to reflect the committed changes.