[clang] 2ae1822 - [Diagnostics] Fixed -Wsizeof-array-div false positive when divisor is sizeof reference type (PR47495)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 20 10:58:15 PDT 2020


On Sun, 20 Sep 2020 at 08:43, Dávid Bolvanský via cfe-commits <
cfe-commits at lists.llvm.org> wrote:

> Author: Dávid Bolvanský
> Date: 2020-09-20T17:43:06+02:00
> New Revision: 2ae182258c49724e4daaae196de829ea65c116e8
>
> URL:
> https://github.com/llvm/llvm-project/commit/2ae182258c49724e4daaae196de829ea65c116e8
> DIFF:
> https://github.com/llvm/llvm-project/commit/2ae182258c49724e4daaae196de829ea65c116e8.diff
>
> LOG: [Diagnostics] Fixed -Wsizeof-array-div false positive when divisor is
> sizeof reference type (PR47495)
>
> Added:
>
>
> Modified:
>     clang/lib/Sema/SemaExpr.cpp
>     clang/test/Sema/div-sizeof-array.cpp
>
> Removed:
>
>
>
>
> ################################################################################
> diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
> index 501e1aed1509..736a6c166eb3 100644
> --- a/clang/lib/Sema/SemaExpr.cpp
> +++ b/clang/lib/Sema/SemaExpr.cpp
> @@ -10038,7 +10038,7 @@ static void
> DiagnoseDivisionSizeofPointerOrArray(Sema &S, Expr *LHS, Expr *RHS,
>      QualType ArrayElemTy = ArrayTy->getElementType();
>      if (ArrayElemTy != S.Context.getBaseElementType(ArrayTy) ||
>          ArrayElemTy->isDependentType() || RHSTy->isDependentType() ||
> -        ArrayElemTy->isCharType() ||
> +        RHSTy->isReferenceType() || ArrayElemTy->isCharType() ||
>

Instead of skipping the check for a reference type, could we instead
replace RHSTy with its referenced type for the purpose of the check when
RHSTy is a reference type? I think we do want to diagnose (for example)
sizeof(array) / sizeof(decltype(*array[0])) for an array of pointers.


>          S.Context.getTypeSize(ArrayElemTy) ==
> S.Context.getTypeSize(RHSTy))
>        return;
>      S.Diag(Loc, diag::warn_division_sizeof_array)
>
> diff  --git a/clang/test/Sema/div-sizeof-array.cpp
> b/clang/test/Sema/div-sizeof-array.cpp
> index e295a9dec6d8..898ff42a7bd4 100644
> --- a/clang/test/Sema/div-sizeof-array.cpp
> +++ b/clang/test/Sema/div-sizeof-array.cpp
> @@ -42,4 +42,8 @@ void test(void) {
>
>    float m[4][4];
>    int d1 = sizeof(m) / sizeof(**m);
> +
> +  int array[10];
> +  int narray = sizeof(array) / sizeof(int &);
> +  int narray2 = sizeof(array) / sizeof(decltype(array[0]));
>  }
>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200920/a2a23f6e/attachment.html>


More information about the cfe-commits mailing list