Skip to content

Commit 8a0abea

Browse files
author
Steve Naroff
committed
Type::isArithmeticType(): disallow incomplete enum decls. Bug submitted by Eli. llvm-svn: 46102
1 parent 506e507 commit 8a0abea

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

clang/AST/Type.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,11 @@ bool Type::isArithmeticType() const {
444444
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
445445
return BT->getKind() != BuiltinType::Void;
446446
if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
447-
if (TT->getDecl()->getKind() == Decl::Enum)
448-
return true;
447+
if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
448+
// GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
449+
// If a body isn't seen by the time we get here, we exclude it from
450+
// being allowed in arithmetic expressions.
451+
return ED->isDefinition();
449452
return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
450453
}
451454

clang/include/clang/AST/Decl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ class EnumDecl : public TagDecl {
663663
EnumDecl(SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl)
664664
: TagDecl(Enum, L, Id, PrevDecl) {
665665
ElementList = 0;
666+
IntegerType = QualType();
666667
}
667668

668669
/// defineElements - When created, EnumDecl correspond to a forward declared

clang/test/Sema/enum.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ int test() {
2222
return sizeof(enum e) ;
2323
}
2424

25+
enum gccForwardEnumExtension ve; // expected-warning {{ISO C forbids forward references to 'enum' types}}
26+
27+
int test2(int i)
28+
{
29+
ve + i; // expected-error{{invalid operands to binary expression ('enum gccForwardEnumExtension' and 'int')}}
30+
}

0 commit comments

Comments
 (0)