From 67c49e892b50a9ccb64f7296e00c23c61a8b7186 Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Wed, 16 Jan 2008 23:54:22 +0000 Subject: [PATCH] Type::isArithmeticType(): disallow incomplete enum decls. Bug submitted by Eli. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46102 91177308-0d34-0410-b5e6-96231b3b80d8 --- AST/Type.cpp | 7 +++++-- include/clang/AST/Decl.h | 1 + test/Sema/enum.c | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/AST/Type.cpp b/AST/Type.cpp index 78e0dbbb27..e59569d2d6 100644 --- a/AST/Type.cpp +++ b/AST/Type.cpp @@ -444,8 +444,11 @@ bool Type::isArithmeticType() const { if (const BuiltinType *BT = dyn_cast(CanonicalType)) return BT->getKind() != BuiltinType::Void; if (const TagType *TT = dyn_cast(CanonicalType)) - if (TT->getDecl()->getKind() == Decl::Enum) - return true; + if (const EnumDecl *ED = dyn_cast(TT->getDecl())) + // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2). + // If a body isn't seen by the time we get here, we exclude it from + // being allowed in arithmetic expressions. + return ED->isDefinition(); return isa(CanonicalType) || isa(CanonicalType); } diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index 40af7bada0..0bd250d99b 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -663,6 +663,7 @@ public: EnumDecl(SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) : TagDecl(Enum, L, Id, PrevDecl) { ElementList = 0; + IntegerType = QualType(); } /// defineElements - When created, EnumDecl correspond to a forward declared diff --git a/test/Sema/enum.c b/test/Sema/enum.c index 79a92c8f2d..169c394c50 100644 --- a/test/Sema/enum.c +++ b/test/Sema/enum.c @@ -22,3 +22,9 @@ int test() { return sizeof(enum e) ; } +enum gccForwardEnumExtension ve; // expected-warning {{ISO C forbids forward references to 'enum' types}} + +int test2(int i) +{ + ve + i; // expected-error{{invalid operands to binary expression ('enum gccForwardEnumExtension' and 'int')}} +} -- 2.50.1