mirror of
https://github.com/intel/llvm.git
synced 2026-01-27 06:06:34 +08:00
Check for casts to an incomplete type in C. Improves diagnostics for cast to
incomplete union (PR5692) and incomplete enum, and fixes obscure accepts-invalid on cast to incomplete struct. llvm-svn: 108630
This commit is contained in:
@@ -2650,6 +2650,8 @@ def err_typecheck_cond_expect_scalar : Error<
|
||||
"used type %0 where arithmetic or pointer type is required">;
|
||||
def ext_typecheck_cond_one_void : Extension<
|
||||
"C99 forbids conditional expressions with only one void side">;
|
||||
def err_typecheck_cast_to_incomplete : Error<
|
||||
"cast to incomplete type %0">;
|
||||
def ext_typecheck_cast_nonscalar : Extension<
|
||||
"C99 forbids casting nonscalar type %0 to the same type">;
|
||||
def ext_typecheck_cast_to_union : Extension<"C99 forbids casts to union type">;
|
||||
|
||||
@@ -3891,6 +3891,10 @@ bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (RequireCompleteType(TyR.getBegin(), castType,
|
||||
diag::err_typecheck_cast_to_incomplete))
|
||||
return true;
|
||||
|
||||
if (!castType->isScalarType() && !castType->isVectorType()) {
|
||||
if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) &&
|
||||
(castType->isStructureType() || castType->isUnionType())) {
|
||||
|
||||
14
clang/test/Sema/cast-incomplete.c
Normal file
14
clang/test/Sema/cast-incomplete.c
Normal file
@@ -0,0 +1,14 @@
|
||||
// RUN: %clang_cc1 -fsyntax-only %s -verify
|
||||
// PR5692
|
||||
|
||||
enum x; // expected-note {{forward declaration}}
|
||||
extern struct y a; // expected-note {{forward declaration}}
|
||||
extern union z b; // expected-note 2 {{forward declaration}}
|
||||
|
||||
void foo() {
|
||||
(enum x)1; // expected-error {{cast to incomplete type}}
|
||||
(struct y)a; // expected-error {{cast to incomplete type}}
|
||||
(union z)b; // expected-error {{cast to incomplete type}}
|
||||
(union z)1; // expected-error {{cast to incomplete type}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user