Skip to content

[C23] Fix typeof handling in enum declarations #146394

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ C23 Feature Support
which clarified how Clang is handling underspecified object declarations.
- Clang now accepts single variadic parameter in type-name. It's a part of
`WG14 N2975 <https://open-std.org/JTC1/SC22/WG14/www/docs/n2975.pdf>`_
- Fixed a bug with handling the type operand form of ``typeof`` when it is used
to specify a fixed underlying type for an enumeration. #GH146351

C11 Feature Support
^^^^^^^^^^^^^^^^^^^
Expand Down
60 changes: 42 additions & 18 deletions clang/include/clang/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ enum class ObjCTypeQual {
NumQuals
};

/// TypeCastState - State whether an expression is or may be a type cast.
enum class TypeCastState { NotTypeCast = 0, MaybeTypeCast, IsTypeCast };
/// If a typo should be encountered, should typo correction suggest type names,
/// non type names, or both?
enum class TypoCorrectionTypeBehavior {
AllowNonTypes,
AllowTypes,
AllowBoth,
};

/// Control what ParseCastExpression will parse.
enum class CastParseKind { AnyCastExpr = 0, UnaryExprOnly, PrimaryExprOnly };
Expand All @@ -116,6 +121,15 @@ enum class ParenParseOption {
CastExpr // Also allow '(' type-name ')' <anything>
};

/// In a call to ParseParenExpression, are the initial parentheses part of an
/// operator that requires the parens be there (like typeof(int)) or could they
/// be something else, such as part of a compound literal or a sizeof
/// expression, etc.
enum class ParenExprKind {
PartOfOperator, // typeof(int)
Unknown, // sizeof(int) or sizeof (int)1.0f, or compound literal, etc
};

/// Describes the behavior that should be taken for an __if_exists
/// block.
enum class IfExistsBehavior {
Expand Down Expand Up @@ -3709,11 +3723,12 @@ class Parser : public CodeCompletionHandler {
/// assignment-expression ...[opt]
/// expression ',' assignment-expression ...[opt]
/// \endverbatim
ExprResult
ParseExpression(TypeCastState isTypeCast = TypeCastState::NotTypeCast);
ExprResult ParseExpression(TypoCorrectionTypeBehavior CorrectionBehavior =
TypoCorrectionTypeBehavior::AllowNonTypes);

ExprResult ParseConstantExpressionInExprEvalContext(
TypeCastState isTypeCast = TypeCastState::NotTypeCast);
TypoCorrectionTypeBehavior CorrectionBehavior =
TypoCorrectionTypeBehavior::AllowNonTypes);
ExprResult ParseConstantExpression();
ExprResult ParseArrayBoundExpression();
ExprResult ParseCaseExpression(SourceLocation CaseLoc);
Expand Down Expand Up @@ -3750,8 +3765,9 @@ class Parser : public CodeCompletionHandler {
ExprResult ParseConstraintLogicalOrExpression(bool IsTrailingRequiresClause);

/// Parse an expr that doesn't include (top-level) commas.
ExprResult ParseAssignmentExpression(
TypeCastState isTypeCast = TypeCastState::NotTypeCast);
ExprResult
ParseAssignmentExpression(TypoCorrectionTypeBehavior CorrectionBehavior =
TypoCorrectionTypeBehavior::AllowNonTypes);

ExprResult ParseConditionalExpression();

Expand Down Expand Up @@ -4017,14 +4033,15 @@ class Parser : public CodeCompletionHandler {
///
ExprResult ParseCastExpression(CastParseKind ParseKind,
bool isAddressOfOperand, bool &NotCastExpr,
TypeCastState isTypeCast,
TypoCorrectionTypeBehavior CorrectionBehavior,
bool isVectorLiteral = false,
bool *NotPrimaryExpression = nullptr);
ExprResult ParseCastExpression(CastParseKind ParseKind,
bool isAddressOfOperand = false,
TypoCorrectionTypeBehavior CorrectionBehavior =
TypoCorrectionTypeBehavior::AllowNonTypes,
bool isVectorLiteral = false,
bool *NotPrimaryExpression = nullptr);
ExprResult
ParseCastExpression(CastParseKind ParseKind, bool isAddressOfOperand = false,
TypeCastState isTypeCast = TypeCastState::NotTypeCast,
bool isVectorLiteral = false,
bool *NotPrimaryExpression = nullptr);

/// Returns true if the next token cannot start an expression.
bool isNotExpressionStart();
Expand Down Expand Up @@ -4181,10 +4198,15 @@ class Parser : public CodeCompletionHandler {
/// \endverbatim
bool ParseSimpleExpressionList(SmallVectorImpl<Expr *> &Exprs);

/// ParseParenExpression - This parses the unit that starts with a '(' token,
/// based on what is allowed by ExprType. The actual thing parsed is returned
/// in ExprType. If stopIfCastExpr is true, it will only return the parsed
/// type, not the parsed cast-expression.
/// This parses the unit that starts with a '(' token, based on what is
/// allowed by ExprType. The actual thing parsed is returned in ExprType. If
/// StopIfCastExpr is true, it will only return the parsed type, not the
/// parsed cast-expression. If ParenBehavior is ParenExprKind::PartOfOperator,
/// the initial open paren and its matching close paren are known to be part
/// of another grammar production and not part of the operand. e.g., the
/// typeof and typeof_unqual operators in C. Otherwise, the function has to
/// parse the parens to determine whether they're part of a cast or compound
/// literal expression rather than a parenthesized type.
///
/// \verbatim
/// primary-expression: [C99 6.5.1]
Expand All @@ -4209,7 +4231,9 @@ class Parser : public CodeCompletionHandler {
/// '(' '[' expression ']' { '[' expression ']' } cast-expression
/// \endverbatim
ExprResult ParseParenExpression(ParenParseOption &ExprType,
bool stopIfCastExpr, bool isTypeCast,
bool StopIfCastExpr,
ParenExprKind ParenBehavior,
TypoCorrectionTypeBehavior CorrectionBehavior,
ParsedType &CastTy,
SourceLocation &RParenLoc);

Expand Down
Loading