Diagnose invalid sizeof/alignof operands.

llvm-svn: 39291
This commit is contained in:
Chris Lattner 2007-01-23 22:29:49 +00:00
parent 367b019f79
commit 6531c1075c
3 changed files with 50 additions and 2 deletions

View File

@ -336,7 +336,25 @@ ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
SourceLocation RParenLoc) {
// If error parsing type, ignore.
if (Ty == 0) return true;
return new SizeOfAlignOfTypeExpr(isSizeof, TypeRef::getFromOpaquePtr(Ty));
// Verify that this is a valid expression.
TypeRef ArgTy = TypeRef::getFromOpaquePtr(Ty);
if (isa<FunctionType>(ArgTy) && isSizeof) {
// alignof(function) is allowed.
Diag(OpLoc, diag::ext_sizeof_function_type);
return new IntegerConstant(/*1*/);
} else if (ArgTy->isVoidType()) {
Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
} else if (ArgTy->isIncompleteType()) {
std::string TypeName;
ArgTy->getAsString(TypeName);
Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
diag::err_alignof_incomplete_type, TypeName);
return new IntegerConstant(/*0*/);
}
return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy);
}

View File

@ -336,7 +336,25 @@ ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
SourceLocation RParenLoc) {
// If error parsing type, ignore.
if (Ty == 0) return true;
return new SizeOfAlignOfTypeExpr(isSizeof, TypeRef::getFromOpaquePtr(Ty));
// Verify that this is a valid expression.
TypeRef ArgTy = TypeRef::getFromOpaquePtr(Ty);
if (isa<FunctionType>(ArgTy) && isSizeof) {
// alignof(function) is allowed.
Diag(OpLoc, diag::ext_sizeof_function_type);
return new IntegerConstant(/*1*/);
} else if (ArgTy->isVoidType()) {
Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
} else if (ArgTy->isIncompleteType()) {
std::string TypeName;
ArgTy->getAsString(TypeName);
Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
diag::err_alignof_incomplete_type, TypeName);
return new IntegerConstant(/*0*/);
}
return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy);
}

View File

@ -370,6 +370,7 @@ DIAG(ext_unknown_escape, EXTENSION,
DIAG(err_hex_escape_no_digits, ERROR,
"\\x used with no following hex digits")
// Declarations.
DIAG(err_typename_requires_specqual, ERROR,
"type name requires a specifier or qualifier")
DIAG(err_typename_invalid_storageclass, ERROR,
@ -434,6 +435,17 @@ DIAG(warn_implicit_function_decl, WARNING,
DIAG(ext_implicit_function_decl, EXTENSION,
"implicit declaration of function '%s' is invalid in C99")
// Expressions.
DIAG(ext_sizeof_function_type, EXTENSION,
"invalid application of 'sizeof' to a function type")
DIAG(ext_sizeof_void_type, EXTENSION,
"invalid application of '%s' to a void type")
DIAG(err_sizeof_incomplete_type, ERROR,
"invalid application of 'sizeof' to an incomplete type '%s'")
DIAG(err_alignof_incomplete_type, ERROR,
"invalid application of '__alignof' to an incomplete type '%s'")
// Statements.
DIAG(err_continue_not_in_loop, ERROR,
"'continue' statement not in loop statement")
DIAG(err_break_not_in_loop_or_switch, ERROR,