This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add misc-sizeof-container check to find sizeof() uses on stl containers.
ClosedPublic

Authored by alexfh on Sep 10 2015, 7:11 AM.

Diff Detail

Event Timeline

alexfh updated this revision to Diff 34437.Sep 10 2015, 7:11 AM
alexfh retitled this revision from to [clang-tidy] Add misc-sizeof-container check to find sizeof() uses on stl containers..
alexfh updated this object.
alexfh added a reviewer: djasper.
alexfh added a subscriber: cfe-commits.
alexfh updated this revision to Diff 34441.Sep 10 2015, 7:17 AM

Ignore template instantiations.

Great idea for a checker! Some comments below.

clang-tidy/misc/SizeofContainerCheck.cpp
37

Do you need the expr() matcher?

39

I think that this should be checking for more than just basic_string and vector. Why not other containers? For instance, anything that exposes a member function with a size() function seems like it would be reasonable.

49

I think the period should be replaced with a semicolon (it's not a sentence, so the period is also wrong). e.g. (with some wording tweaks),

"sizeof() does not return the size of the container; did you mean to call size()?"

Also, is this actually emitting the diagnostic? If so, a comment would be good explaining why the early return for macros is located where it is, so no one does a drive-by "fix."

alexfh updated this revision to Diff 34446.Sep 10 2015, 7:54 AM

Match a broader set of containers. Updated diagnostic message. Added tests.

alexfh marked 3 inline comments as done.Sep 10 2015, 7:54 AM
alexfh added inline comments.
clang-tidy/misc/SizeofContainerCheck.cpp
38

Yes, I do. sizeOfExpr().bind("...") doesn't compile for some reason.

tools/clang/tools/extra/clang-tidy/misc/SizeofContainerCheck.cpp:24:35: error: no member named 'bind' in 'clang::ast_matchers::internal::Matcher<clang::Stmt>'
                  .bind("expr"))).bind("sizeof"),
                  ~~~~~~~~~~~~~~~ ^
40

I'd like to limit this to the STL containers first. So "any recordDecl named std::.* with a .size() method" might work.

50
  1. Done.
  2. It's actually emitting the diagnostic. And I thought that the comment on line 52 below explains what happens in enough detail (// Don't generate fixes for macros.). If something is still unclear, can you explain what exactly?

I noticed a few more things, but mostly nitpicky at this point.

clang-tidy/misc/SizeofContainerCheck.cpp
23

Should we also ignore parens for when people do crazy things, like sizeof((((some_string+other_string))))?

26

I missed this earlier, but what about other 2-arg overloaded operators, like placement operator new & operator delete, or operator delete with size (and array forms)?

39

Why is "|::string" needed?

alexfh updated this revision to Diff 34451.Sep 10 2015, 8:54 AM
alexfh marked 3 inline comments as done.

Don't complain on the ARRAYSIZE(<array of containers>) pattern (where
sizeof(container) is used as a denominator).

alexfh marked 5 inline comments as done.Sep 10 2015, 9:04 AM
alexfh added inline comments.
clang-tidy/misc/SizeofContainerCheck.cpp
23

I don't think we need to remove anything beyond the most external pair of parentheses.

26

Do you have an example of an expression that will break when a .size() is appended to it? Note, that it should be an expression of a class type.

39

Needed for code bases that use a std::string-like string class defined in the global namespace. Maybe we need a configuration option for custom container regexps. But this will likely be a follow up.

This revision is now accepted and ready to land.Sep 10 2015, 9:32 AM
alexfh closed this revision.Sep 10 2015, 9:39 AM
alexfh marked 3 inline comments as done.
alexfh added a subscriber: alexfh.Sep 11 2015, 3:09 PM

Indeed. But this has been fixed before I could get to it.