Author: rtrieu Date: Fri Sep 8 17:25:05 2017 New Revision: 312842 URL: http://llvm.org/viewvc/llvm-project?rev=312842&view=rev Log: Catch more cases with -Wenum-compare
Treat typedef enum as named enums instead of anonymous enums. Anonymous enums are ignored by the warning, so previously, typedef enums were ignored as well. Modified: cfe/trunk/lib/Sema/SemaExpr.cpp cfe/trunk/lib/Sema/SemaStmt.cpp cfe/trunk/test/SemaCXX/warn-enum-compare.cpp Modified: cfe/trunk/lib/Sema/SemaExpr.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=312842&r1=312841&r2=312842&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaExpr.cpp (original) +++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Sep 8 17:25:05 2017 @@ -9151,9 +9151,11 @@ static void checkEnumComparison(Sema &S, return; // Ignore anonymous enums. - if (!LHSEnumType->getDecl()->getIdentifier()) + if (!LHSEnumType->getDecl()->getIdentifier() && + !LHSEnumType->getDecl()->getTypedefNameForAnonDecl()) return; - if (!RHSEnumType->getDecl()->getIdentifier()) + if (!RHSEnumType->getDecl()->getIdentifier() && + !RHSEnumType->getDecl()->getTypedefNameForAnonDecl()) return; if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) Modified: cfe/trunk/lib/Sema/SemaStmt.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=312842&r1=312841&r2=312842&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaStmt.cpp (original) +++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Sep 8 17:25:05 2017 @@ -754,9 +754,11 @@ static void checkEnumTypesInSwitchStmt(S return; // Ignore anonymous enums. - if (!CondEnumType->getDecl()->getIdentifier()) + if (!CondEnumType->getDecl()->getIdentifier() && + !CondEnumType->getDecl()->getTypedefNameForAnonDecl()) return; - if (!CaseEnumType->getDecl()->getIdentifier()) + if (!CaseEnumType->getDecl()->getIdentifier() && + !CaseEnumType->getDecl()->getTypedefNameForAnonDecl()) return; if (S.Context.hasSameUnqualifiedType(CondType, CaseType)) Modified: cfe/trunk/test/SemaCXX/warn-enum-compare.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-enum-compare.cpp?rev=312842&r1=312841&r2=312842&view=diff ============================================================================== --- cfe/trunk/test/SemaCXX/warn-enum-compare.cpp (original) +++ cfe/trunk/test/SemaCXX/warn-enum-compare.cpp Fri Sep 8 17:25:05 2017 @@ -4,6 +4,8 @@ enum Foo { FooA, FooB, FooC }; enum Bar { BarD, BarE, BarF }; enum { AnonAA = 42, AnonAB = 43 }; enum { AnonBA = 44, AnonBB = 45 }; +enum { Anon1, Anon2, Anon3 }; +typedef enum { TD1, TD2 } TD; namespace name1 { enum Foo {F1, F2, F3}; @@ -29,6 +31,7 @@ void test () { name1::Foo a; oneFoo b; twoFoo c; + TD td; while (x == FooA); while (y == BarD); @@ -65,6 +68,9 @@ void test () { while ((((((B1))))) == (((name1::B2)))); while (B2 == ((((((name2::B1))))))); + while (td == Anon1); + while (td == AnonAA); // expected-warning {{comparison of constant 'AnonAA' (42) with expression of type 'TD' is always false}} + while (B1 == B2); // expected-warning {{comparison of two values with different enumeration types ('name1::Baz' and 'name2::Baz')}} while (name1::B2 == name2::B3); // expected-warning {{comparison of two values with different enumeration types ('name1::Baz' and 'name2::Baz')}} while (z == name2::B2); // expected-warning {{comparison of two values with different enumeration types ('name1::Baz' and 'name2::Baz')}} @@ -209,6 +215,24 @@ void test () { while (getBar() > x); // expected-warning {{comparison of two values with different enumeration types ('Bar' and 'Foo')}} while (getBar() < x); // expected-warning {{comparison of two values with different enumeration types ('Bar' and 'Foo')}} + while (td == FooA); // expected-warning {{comparison of two values with different enumeration types ('TD' and 'Foo')}} + while (td == BarD); // expected-warning {{comparison of two values with different enumeration types ('TD' and 'Bar')}} + while (name1::F1 == td); // expected-warning {{comparison of two values with different enumeration types ('name1::Foo' and 'TD')}} + while (name2::B1 == td); // expected-warning {{comparison of two values with different enumeration types ('name2::Baz' and 'TD')}} + while (td == a); // expected-warning {{comparison of two values with different enumeration types ('TD' and 'name1::Foo')}} + while (td == b); // expected-warning {{comparison of two values with different enumeration types ('TD' and 'oneFoo' (aka 'name1::Foo'))}} + while (td == c); // expected-warning {{comparison of two values with different enumeration types ('TD' and 'twoFoo' (aka 'name1::Foo'))}} + while (td == x); // expected-warning {{comparison of two values with different enumeration types ('TD' and 'Foo')}} + while (td == y); // expected-warning {{comparison of two values with different enumeration types ('TD' and 'Bar')}} + while (td == z); // expected-warning {{comparison of two values with different enumeration types ('TD' and 'name1::Baz')}} + + while (a == TD1); // expected-warning {{comparison of two values with different enumeration types ('name1::Foo' and 'TD')}} + while (b == TD2); // expected-warning {{comparison of two values with different enumeration types ('oneFoo' (aka 'name1::Foo') and 'TD')}} + while (c == TD1); // expected-warning {{comparison of two values with different enumeration types ('twoFoo' (aka 'name1::Foo') and 'TD')}} + while (x == TD2); // expected-warning {{comparison of two values with different enumeration types ('Foo' and 'TD')}} + while (y == TD1); // expected-warning {{comparison of two values with different enumeration types ('Bar' and 'TD')}} + while (z == TD2); // expected-warning {{comparison of two values with different enumeration types ('name1::Baz' and 'TD')}} + switch (a) { case name1::F1: break; case name1::F3: break; @@ -233,4 +257,23 @@ void test () { case FooB: break; case FooC: break; } + + switch (td) { + case TD1: break; + case FooB: break; // expected-warning {{comparison of two values with different enumeration types in switch statement ('TD' and 'Foo')}} + case BarF: break; // expected-warning {{comparison of two values with different enumeration types in switch statement ('TD' and 'Bar')}} + // expected-warning@-1 {{case value not in enumerated type 'TD'}} + case AnonAA: break; // expected-warning {{case value not in enumerated type 'TD'}} + } + + switch (td) { + case Anon1: break; + case TD2: break; + } + + switch (a) { + case TD1: break; // expected-warning {{comparison of two values with different enumeration types in switch statement ('name1::Foo' and 'TD')}} + case TD2: break; // expected-warning {{comparison of two values with different enumeration types in switch statement ('name1::Foo' and 'TD')}} + case name1::F3: break; + } } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits