[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 08:59:08 PST 2018


pfultz2 added a comment.

> Can you point to some real world code that would benefit from this check?

Yes, here:

https://github.com/ROCmSoftwarePlatform/MIOpen/blob/master/src/convolution.cpp#L184

It is erroneously calling `sizeof(yDesc.GetType())`. The `GetType` function returns an enum that represents the float type. In this case it wants to compute the size of the float type not the size of the enum that represents the float type. There's actually many instances of this error in the codebase.

> I don't see this as being a common issue with code and I am concerned about false positives.

I have seen it as a common issue. It only runs for a function that returns an integer type. Another possibility is to not warn for function calls that are dependent on a template parameter.

Of course, there is still the option to disable it.

> For instance, it's reasonable to write sizeof(func_call()) in a context where you don't want to repeat the type name in multiple places.

Generally, a typedef is used to avoid repeating the type.

> I've seen this construct used in LLVM's code base (see Prologue::getLength() in DWARFDebugLine.h for one such instance).

This is the first time I have seen this used legitimately outside of template metaprogramming. Alternatively, you could also write `sizeof(decltype(expr))`.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D44231





More information about the cfe-commits mailing list