Skip to content

[Clang][Sema] Fix type of enumerators in incomplete enumerations #84068

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

Merged
merged 1 commit into from
Mar 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,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();
if (isa<EnumType>(getType()))
return getType();
if (const auto *ECD = getEnumConstantDecl()) {
const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
if (ED->isCompleteDefinition())
return Ctx.getTypeDeclType(ED);
}
return getType();
}

SourceLocation Expr::getExprLoc() const {
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Sema/enum-constant-type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify %s -Wenum-compare
// expected-no-diagnostics

enum E1 {
E11 = 0
};

enum E2 {
E21 = 0,
E22 = E11,
E23 = E21 + E22
};
11 changes: 10 additions & 1 deletion clang/test/Sema/warn-compare-enum-types-mismatch.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
// 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

// In C enumerators (i.e enumeration constants) have type int (until C23). In
// order to support diagnostics such as -Wenum-compare we pretend they have the
// type of their enumeration.

typedef enum EnumA {
A
} EnumA;

enum EnumB {
B
B,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to capture some of what I wrote in the review summary as a comment here so it's clear why this is correct in both C and C++.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what I was asking, was do we have an equivalent C++ test that verifies in a .cpp file that we also do not obtain a diagnostic for this.

B1 = 1,
// In C++ this comparison doesnt warn as enumerators dont have the type of
// their enumeration before the closing brace. We mantain the same behavior
// in C.
B2 = A == B1
};

enum {
Expand Down