forked from OSchip/llvm-project
Warn on "void f(int a[10]) { sizeof(a); }"
llvm-svn: 133036
This commit is contained in:
parent
e8bbc10880
commit
0870debb8b
|
@ -115,6 +115,7 @@ def SignCompare : DiagGroup<"sign-compare">;
|
||||||
def : DiagGroup<"stack-protector">;
|
def : DiagGroup<"stack-protector">;
|
||||||
def : DiagGroup<"switch-default">;
|
def : DiagGroup<"switch-default">;
|
||||||
def : DiagGroup<"synth">;
|
def : DiagGroup<"synth">;
|
||||||
|
def SizeofArrayArgument : DiagGroup<"sizeof-array-argument">;
|
||||||
def TautologicalCompare : DiagGroup<"tautological-compare">;
|
def TautologicalCompare : DiagGroup<"tautological-compare">;
|
||||||
def HeaderHygiene : DiagGroup<"header-hygiene">;
|
def HeaderHygiene : DiagGroup<"header-hygiene">;
|
||||||
|
|
||||||
|
@ -248,6 +249,7 @@ def Most : DiagGroup<"most", [
|
||||||
ReturnType,
|
ReturnType,
|
||||||
SelfAssignment,
|
SelfAssignment,
|
||||||
Switch,
|
Switch,
|
||||||
|
SizeofArrayArgument,
|
||||||
Trigraphs,
|
Trigraphs,
|
||||||
Uninitialized,
|
Uninitialized,
|
||||||
UnknownPragmas,
|
UnknownPragmas,
|
||||||
|
|
|
@ -2602,6 +2602,10 @@ def warn_self_assignment : Warning<
|
||||||
"explicitly assigning a variable of type %0 to itself">,
|
"explicitly assigning a variable of type %0 to itself">,
|
||||||
InGroup<SelfAssignment>, DefaultIgnore;
|
InGroup<SelfAssignment>, DefaultIgnore;
|
||||||
|
|
||||||
|
def warn_sizeof_array_param : Warning<
|
||||||
|
"sizeof on array function parameter will return size of %0 instead of %1">,
|
||||||
|
InGroup<SizeofArrayArgument>;
|
||||||
|
|
||||||
def err_sizeof_nonfragile_interface : Error<
|
def err_sizeof_nonfragile_interface : Error<
|
||||||
"invalid application of '%select{alignof|sizeof}1' to interface %0 in "
|
"invalid application of '%select{alignof|sizeof}1' to interface %0 in "
|
||||||
"non-fragile ABI">;
|
"non-fragile ABI">;
|
||||||
|
|
|
@ -3160,6 +3160,20 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op,
|
||||||
Op->getSourceRange(), ExprKind))
|
Op->getSourceRange(), ExprKind))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
if (ExprKind == UETT_SizeOf) {
|
||||||
|
if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) {
|
||||||
|
if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) {
|
||||||
|
QualType OType = PVD->getOriginalType();
|
||||||
|
QualType Type = PVD->getType();
|
||||||
|
if (Type->isPointerType() && OType->isArrayType()) {
|
||||||
|
Diag(Op->getExprLoc(), diag::warn_sizeof_array_param)
|
||||||
|
<< Type << OType;
|
||||||
|
Diag(PVD->getLocation(), diag::note_declared_at);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
||||||
|
|
||||||
|
typedef int Arr[10];
|
||||||
|
|
||||||
|
typedef int trungl_int;
|
||||||
|
|
||||||
|
void f(int a[10], Arr arr) { // \
|
||||||
|
// expected-note {{declared here}} \
|
||||||
|
// expected-note {{declared here}} \
|
||||||
|
// expected-note {{declared here}} \
|
||||||
|
// expected-note {{declared here}}
|
||||||
|
|
||||||
|
/* Should warn. */
|
||||||
|
(void)sizeof(a); // \
|
||||||
|
// expected-warning{{sizeof on array function parameter will return size of 'int *' instead of 'int [10]'}}
|
||||||
|
(void)sizeof((((a)))); // \
|
||||||
|
// expected-warning{{sizeof on array function parameter will return size of 'int *' instead of 'int [10]'}}
|
||||||
|
(void)sizeof a; // \
|
||||||
|
// expected-warning{{sizeof on array function parameter will return size of 'int *' instead of 'int [10]'}}
|
||||||
|
(void)sizeof arr; // \
|
||||||
|
// expected-warning{{sizeof on array function parameter will return size of 'int *' instead of 'Arr' (aka 'int [10]')}}
|
||||||
|
|
||||||
|
/* Shouldn't warn. */
|
||||||
|
int b[10];
|
||||||
|
(void)sizeof b;
|
||||||
|
Arr brr;
|
||||||
|
(void)sizeof brr;
|
||||||
|
(void)sizeof(Arr);
|
||||||
|
(void)sizeof(int);
|
||||||
|
}
|
Loading…
Reference in New Issue