aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2011-02-17 08:37:06 +0000
committerChandler Carruth <chandlerc@gmail.com>2011-02-17 08:37:06 +0000
commit543cb655b174087f6c2d22009934c9fed6c32114 (patch)
tree61c99e83fd8f8c1bf5683f7ba470c7a92fa74f16 /lib
parent8755836379a3f8e9848b5e770aa60d00e1fbb990 (diff)
Implement -Wenum-compare, which warns when comparing two enums of
different types. We omit the warning when the enum types are anonymous. Unlike GCC, this warning does not distinguish between C++ and C/ObjC for controling whether it is on by default, it is always on by default. Original patch contributed by Richard Trieu (@ Google), I fixed some style issues, and cleaned it up for submission. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125739 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Sema/SemaExpr.cpp20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 94dd27d0c2..052dd4b956 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -6637,6 +6637,24 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc,
QualType lType = lex->getType();
QualType rType = rex->getType();
+ Expr *LHSStripped = lex->IgnoreParenImpCasts();
+ Expr *RHSStripped = rex->IgnoreParenImpCasts();
+ QualType LHSStrippedType = LHSStripped->getType();
+ QualType RHSStrippedType = RHSStripped->getType();
+
+ // Two different enums will raise a warning when compared.
+ if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) {
+ if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) {
+ if (LHSEnumType->getDecl()->getIdentifier() &&
+ RHSEnumType->getDecl()->getIdentifier() &&
+ !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) {
+ Diag(Loc, diag::warn_comparison_of_mixed_enum_types)
+ << LHSStrippedType << RHSStrippedType
+ << lex->getSourceRange() << rex->getSourceRange();
+ }
+ }
+ }
+
if (!lType->hasFloatingRepresentation() &&
!(lType->isBlockPointerType() && isRelational) &&
!lex->getLocStart().isMacroID() &&
@@ -6651,8 +6669,6 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc,
// obvious cases in the definition of the template anyways. The idea is to
// warn when the typed comparison operator will always evaluate to the same
// result.
- Expr *LHSStripped = lex->IgnoreParenImpCasts();
- Expr *RHSStripped = rex->IgnoreParenImpCasts();
if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) {
if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) {
if (DRL->getDecl() == DRR->getDecl() &&