[PATCH] D44231: [clang-tidy] Check for sizeof that call functions

Paul Fultz II via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 8 07:43:11 PST 2018

pfultz2 added a comment.

> Can you elaborate a bit more about this?

This catches problems when calling `sizeof(f())` when `f` returns an integer(or enum). This is because, most likely, the integer represents the type to be chosen, however, `sizeof` is being computed for an integer and not the type the integer represents. That is, the user has an enum for the `data_type`:

  enum data_type {

At some point the user may call a function to get the data type(perhaps `x.GetType()`) and pass it on to `sizeof`, like `sizeof(x.GetType())`, which is incorrect.

> I think we also need to update the check document (adding proper section of this new behavior, and the new option).

I will update the doc.

Comment at: clang-tidy/misc/SizeofExpressionCheck.cpp:220
+                 Result.Nodes.getNodeAs<Expr>("sizeof-integer-call")) {
+    diag(E->getLocStart(), "suspicious usage of 'sizeof(expr)' to an integer");
   } else if (const auto *E = Result.Nodes.getNodeAs<Expr>("sizeof-this")) {
alexfh wrote:
> I'm not sure I understand the message "suspicious usage of ... to an integer". Specifically, what does the "to an integer" part mean?
That could probably be worded better. It means the `expr` is an integer type. Maybe I should say `suspicious usage of 'sizeof() on an expression that results in an integer`?

  rCTE Clang Tools Extra


More information about the cfe-commits mailing list