Index: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td @@ -3406,6 +3406,10 @@ def warn_division_sizeof_ptr : Warning< "'%0' will return the size of the pointer, not the array itself">, InGroup>; +def warn_division_sizeof_array : Warning< + "expression does not compute the number of elements in this array; element " + "type is %0, not %1">, + InGroup>; def note_function_warning_silence : Note< "prefix with the address-of operator to silence this warning">; Index: cfe/trunk/lib/Sema/SemaExpr.cpp =================================================================== --- cfe/trunk/lib/Sema/SemaExpr.cpp +++ cfe/trunk/lib/Sema/SemaExpr.cpp @@ -9158,17 +9158,28 @@ else RHSTy = RUE->getArgumentExpr()->IgnoreParens()->getType(); - if (!LHSTy->isPointerType() || RHSTy->isPointerType()) - return; - if (LHSTy->getPointeeType().getCanonicalType().getUnqualifiedType() != - RHSTy.getCanonicalType().getUnqualifiedType()) - return; + if (LHSTy->isPointerType() && !RHSTy->isPointerType()) { + if (!S.Context.hasSameUnqualifiedType(LHSTy->getPointeeType(), RHSTy)) + return; - S.Diag(Loc, diag::warn_division_sizeof_ptr) << LHS << LHS->getSourceRange(); - if (const auto *DRE = dyn_cast(LHSArg)) { - if (const ValueDecl *LHSArgDecl = DRE->getDecl()) - S.Diag(LHSArgDecl->getLocation(), diag::note_pointer_declared_here) - << LHSArgDecl; + S.Diag(Loc, diag::warn_division_sizeof_ptr) << LHS << LHS->getSourceRange(); + if (const auto *DRE = dyn_cast(LHSArg)) { + if (const ValueDecl *LHSArgDecl = DRE->getDecl()) + S.Diag(LHSArgDecl->getLocation(), diag::note_pointer_declared_here) + << LHSArgDecl; + } + } else if (const auto *ArrayTy = S.Context.getAsArrayType(LHSTy)) { + QualType ArrayElemTy = ArrayTy->getElementType(); + if (ArrayElemTy->isDependentType() || RHSTy->isDependentType() || + S.Context.getTypeSize(ArrayElemTy) == S.Context.getTypeSize(RHSTy)) + return; + S.Diag(Loc, diag::warn_division_sizeof_array) + << LHSArg->getSourceRange() << ArrayElemTy << RHSTy; + if (const auto *DRE = dyn_cast(LHSArg)) { + if (const ValueDecl *LHSArgDecl = DRE->getDecl()) + S.Diag(LHSArgDecl->getLocation(), diag::note_array_declared_here) + << LHSArgDecl; + } } } Index: cfe/trunk/test/Sema/div-sizeof-array.cpp =================================================================== --- cfe/trunk/test/Sema/div-sizeof-array.cpp +++ cfe/trunk/test/Sema/div-sizeof-array.cpp @@ -0,0 +1,28 @@ +// RUN: %clang_cc1 %s -verify -Wsizeof-array-div -fsyntax-only + +template +int f(Ty (&Array)[N]) { + return sizeof(Array) / sizeof(Ty); // Should not warn +} + +typedef int int32; + +void test(void) { + int arr[12]; // expected-note 2 {{array 'arr' declared here}} + unsigned long long arr2[4]; + int *p = &arr[0]; + int a1 = sizeof(arr) / sizeof(*arr); + int a2 = sizeof arr / sizeof p; // expected-warning {{expression does not compute the number of elements in this array; element type is 'int', not 'int *'}} + int a4 = sizeof arr2 / sizeof p; + int a5 = sizeof(arr) / sizeof(short); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int', not 'short'}} + int a6 = sizeof(arr) / sizeof(int32); + int a7 = sizeof(arr) / sizeof(int); + int a9 = sizeof(arr) / sizeof(unsigned int); + const char arr3[2] = "A"; + int a10 = sizeof(arr3) / sizeof(char); + + int arr4[10][12]; // expected-note 3 {{array 'arr4' declared here}} + int b1 = sizeof(arr4) / sizeof(arr2[12]); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int [12]', not 'unsigned long long'}} + int b2 = sizeof(arr4) / sizeof(int *); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int [12]', not 'int *'}} + int b3 = sizeof(arr4) / sizeof(short *); // expected-warning {{expression does not compute the number of elements in this array; element type is 'int [12]', not 'short *'}} +}