Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang][Sema] Fix missing warning when comparing mismatched enums in … #81418

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ Improvements to Clang's time-trace

Bug Fixes in This Version
-------------------------
- Fixed missing warnings when comparing mismatched enumeration constants
in C (`#29217 <https://github.com/llvm/llvm-project/issues/29217>`).

- Clang now accepts elaborated-type-specifiers that explicitly specialize
a member class template for an implicit instantiation of a class template.

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ class Expr : public ValueStmt {
TR = t;
}

/// If this expression is an enumeration constant, return the
/// enumeration type under which said constant was declared.
/// Otherwise return the expression's type.
/// Note this effectively circumvents the weak typing of C's enum constants
QualType getEnumCoercedType(const ASTContext &Ctx) const;

ExprDependence getDependence() const {
return static_cast<ExprDependence>(ExprBits.Dependent);
}
Expand Down Expand Up @@ -471,6 +477,13 @@ class Expr : public ValueStmt {
/// bit-fields, but it will return null for a conditional bit-field.
FieldDecl *getSourceBitField();

/// If this expression refers to an enum constant, retrieve its declaration
EnumConstantDecl *getEnumConstantDecl();

const EnumConstantDecl *getEnumConstantDecl() const {
return const_cast<Expr *>(this)->getEnumConstantDecl();
}

const FieldDecl *getSourceBitField() const {
return const_cast<Expr*>(this)->getSourceBitField();
}
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ namespace {
}
}

QualType Expr::getEnumCoercedType(const ASTContext &Ctx) const {
if (isa<EnumType>(this->getType()))
return this->getType();
else if (const auto *ECD = this->getEnumConstantDecl())
return Ctx.getTypeDeclType(cast<EnumDecl>(ECD->getDeclContext()));
return this->getType();
}

SourceLocation Expr::getExprLoc() const {
switch (getStmtClass()) {
case Stmt::NoStmtClass: llvm_unreachable("statement without class");
Expand Down Expand Up @@ -4097,6 +4105,13 @@ FieldDecl *Expr::getSourceBitField() {
return nullptr;
}

EnumConstantDecl *Expr::getEnumConstantDecl() {
Expr *E = this->IgnoreParenImpCasts();
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
return dyn_cast<EnumConstantDecl>(DRE->getDecl());
return nullptr;
}

bool Expr::refersToVectorElement() const {
// FIXME: Why do we not just look at the ObjectKind here?
const Expr *E = this->IgnoreParens();
Expand Down
11 changes: 2 additions & 9 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16050,15 +16050,8 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
// Diagnose conversions between different enumeration types.
// In C, we pretend that the type of an EnumConstantDecl is its enumeration
// type, to give us better diagnostics.
QualType SourceType = E->getType();
if (!S.getLangOpts().CPlusPlus) {
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
SourceType = S.Context.getTypeDeclType(Enum);
Source = S.Context.getCanonicalType(SourceType).getTypePtr();
}
}
QualType SourceType = E->getEnumCoercedType(S.Context);
Source = S.Context.getCanonicalType(SourceType).getTypePtr();

if (const EnumType *SourceEnum = Source->getAs<EnumType>())
if (const EnumType *TargetEnum = Target->getAs<EnumType>())
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,8 @@ static void checkEnumArithmeticConversions(Sema &S, Expr *LHS, Expr *RHS,
//
// Warn on this in all language modes. Produce a deprecation warning in C++20.
// Eventually we will presumably reject these cases (in C++23 onwards?).
QualType L = LHS->getType(), R = RHS->getType();
QualType L = LHS->getEnumCoercedType(S.Context),
R = RHS->getEnumCoercedType(S.Context);
bool LEnum = L->isUnscopedEnumerationType(),
REnum = R->isUnscopedEnumerationType();
bool IsCompAssign = ACK == Sema::ACK_CompAssign;
Expand Down
4 changes: 4 additions & 0 deletions clang/test/Sema/builtins-elementwise-math.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void test_builtin_elementwise_add_sat(int i, short s, double d, float4 v, int3 i

enum f { three };
enum f x = __builtin_elementwise_add_sat(one, three);
// expected-warning@-1 {{comparison of different enumeration types ('enum e' and 'enum f')}}

_BitInt(32) ext; // expected-warning {{'_BitInt' in C17 and earlier is a Clang extension}}
ext = __builtin_elementwise_add_sat(ext, ext);
Expand Down Expand Up @@ -134,6 +135,7 @@ void test_builtin_elementwise_sub_sat(int i, short s, double d, float4 v, int3 i

enum f { three };
enum f x = __builtin_elementwise_sub_sat(one, three);
// expected-warning@-1 {{comparison of different enumeration types ('enum e' and 'enum f')}}

_BitInt(32) ext; // expected-warning {{'_BitInt' in C17 and earlier is a Clang extension}}
ext = __builtin_elementwise_sub_sat(ext, ext);
Expand Down Expand Up @@ -189,6 +191,7 @@ void test_builtin_elementwise_max(int i, short s, double d, float4 v, int3 iv, u

enum f { three };
enum f x = __builtin_elementwise_max(one, three);
// expected-warning@-1 {{comparison of different enumeration types ('enum e' and 'enum f')}}

_BitInt(32) ext; // expected-warning {{'_BitInt' in C17 and earlier is a Clang extension}}
ext = __builtin_elementwise_max(ext, ext);
Expand Down Expand Up @@ -244,6 +247,7 @@ void test_builtin_elementwise_min(int i, short s, double d, float4 v, int3 iv, u

enum f { three };
enum f x = __builtin_elementwise_min(one, three);
// expected-warning@-1 {{comparison of different enumeration types ('enum e' and 'enum f')}}

_BitInt(32) ext; // expected-warning {{'_BitInt' in C17 and earlier is a Clang extension}}
ext = __builtin_elementwise_min(ext, ext);
Expand Down
42 changes: 42 additions & 0 deletions clang/test/Sema/warn-compare-enum-types-mismatch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: %clang_cc1 -x c -fsyntax-only -verify -Wenum-compare -Wno-unused-comparison %s
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wenum-compare -Wno-unused-comparison %s

typedef enum EnumA {
A
} EnumA;

enum EnumB {
B
};

enum {
C
};

void foo(void) {
enum EnumA a = A;
enum EnumB b = B;
A == B;
// expected-warning@-1 {{comparison of different enumeration types}}
a == (B);
// expected-warning@-1 {{comparison of different enumeration types}}
a == b;
// expected-warning@-1 {{comparison of different enumeration types}}
A > B;
// expected-warning@-1 {{comparison of different enumeration types}}
A >= b;
// expected-warning@-1 {{comparison of different enumeration types}}
a > b;
// expected-warning@-1 {{comparison of different enumeration types}}
(A) <= ((B));
// expected-warning@-1 {{comparison of different enumeration types}}
a < B;
// expected-warning@-1 {{comparison of different enumeration types}}
a < b;
// expected-warning@-1 {{comparison of different enumeration types}}

// In the following cases we purposefully differ from GCC and dont warn
a == C;
A < C;
b >= C;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int get_flag(int cond) {
#ifdef __cplusplus
// expected-warning@-2 {{conditional expression between different enumeration types ('ro' and 'rw')}}
#else
// expected-no-diagnostics
// expected-warning@-4 {{conditional expression between different enumeration types ('enum ro' and 'enum rw')}}
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions clang/test/Sema/warn-overlap.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare %s
// RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused -Wno-loop-analysis %s
// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare -Wno-enum-compare %s
// RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused -Wno-loop-analysis -Wno-enum-compare %s

#define mydefine 2

Expand Down