From 2564aaa12c92e188f748ae59b7aae58c0c1dc4bd Mon Sep 17 00:00:00 2001 From: acqn Date: Wed, 10 Jan 2024 04:48:27 +0800 Subject: [PATCH 1/2] Refix for diagnosis on expected expressions. --- src/cc65/expr.c | 2 +- test/ref/bug1889-missing-identifier.c | 2 ++ test/ref/bug1889-missing-identifier.cref | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 0f3a6e110..3ef141300 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -1408,7 +1408,7 @@ static void Primary (ExprDesc* E) } else { /* Let's see if this is a C99-style declaration */ DeclSpec Spec; - ParseDeclSpec (&Spec, TS_DEFAULT_TYPE_INT, SC_AUTO); + ParseDeclSpec (&Spec, TS_DEFAULT_TYPE_NONE, SC_AUTO); if ((Spec.Flags & DS_TYPE_MASK) != DS_NONE) { Error ("Mixed declarations and code are not supported in cc65"); diff --git a/test/ref/bug1889-missing-identifier.c b/test/ref/bug1889-missing-identifier.c index d9cf4aa52..a8140565f 100644 --- a/test/ref/bug1889-missing-identifier.c +++ b/test/ref/bug1889-missing-identifier.c @@ -3,6 +3,8 @@ int enum { a } x; inline enum { b }; +_Static_assert(); + int main(void) { return 0; diff --git a/test/ref/bug1889-missing-identifier.cref b/test/ref/bug1889-missing-identifier.cref index 7381d2032..6317657d1 100644 --- a/test/ref/bug1889-missing-identifier.cref +++ b/test/ref/bug1889-missing-identifier.cref @@ -1,3 +1,4 @@ bug1889-missing-identifier.c:3: Error: Identifier or ';' expected after declaration specifiers bug1889-missing-identifier.c:3: Warning: Implicit 'int' is an obsolete feature bug1889-missing-identifier.c:4: Error: Declaration specifier or identifier expected +bug1889-missing-identifier.c:6: Error: Expression expected From 94dfc08c0e439b46b0f1a0e625bca9609b02ff94 Mon Sep 17 00:00:00 2001 From: acqn Date: Wed, 10 Jan 2024 04:43:50 +0800 Subject: [PATCH 2/2] Fixed false "Non constant initializers" error messages on wrong places, which could be resulted from failed array declarations etc. --- src/cc65/expr.c | 12 +++++++++++- test/ref/custom-reference-error.c | 5 ++++- test/ref/custom-reference-error.cref | 3 ++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 3ef141300..9cf05d2dc 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -4305,8 +4305,13 @@ ExprDesc NoCodeConstExpr (void (*Func) (ExprDesc*)) if (!ED_IsConst (&Expr) || !ED_CodeRangeIsEmpty (&Expr)) { Error ("Constant expression expected"); /* To avoid any compiler errors, make the expression a valid const */ - Expr.Flags &= E_MASK_RTYPE | E_MASK_KEEP_RESULT; + Expr.Flags &= E_MASK_RTYPE | E_MASK_KEEP_MAKE; Expr.Flags |= E_LOC_NONE; + + /* Remove any non-constant code generated */ + if (!ED_CodeRangeIsEmpty (&Expr)) { + RemoveCodeRange (&Expr.Start, &Expr.End); + } } /* Return by value */ @@ -4331,6 +4336,11 @@ ExprDesc NoCodeConstAbsIntExpr (void (*Func) (ExprDesc*)) Error ("Constant integer expression expected"); /* To avoid any compiler errors, make the expression a valid const */ ED_MakeConstAbsInt (&Expr, 1); + + /* Remove any non-constant code generated */ + if (!ED_CodeRangeIsEmpty (&Expr)) { + RemoveCodeRange (&Expr.Start, &Expr.End); + } } /* Return by value */ diff --git a/test/ref/custom-reference-error.c b/test/ref/custom-reference-error.c index a7c1b6c56..e98fb024d 100644 --- a/test/ref/custom-reference-error.c +++ b/test/ref/custom-reference-error.c @@ -14,7 +14,7 @@ */ typedef short return_t; -#error /* produce an error */ +#error This is an/* produce an error */error return_t main(int argc, char* argv[]) { @@ -22,3 +22,6 @@ return_t main(int argc, char* argv[]) n = 0; /* produce an error */ /* produce a warning */ } + +int arr[main(0, 0)]; /* produce an error */ +int b = 0; diff --git a/test/ref/custom-reference-error.cref b/test/ref/custom-reference-error.cref index b21c72dce..9ffa581cd 100644 --- a/test/ref/custom-reference-error.cref +++ b/test/ref/custom-reference-error.cref @@ -1,6 +1,7 @@ -custom-reference-error.c:17: Error: #error +custom-reference-error.c:17: Error: #error: This is an error custom-reference-error.c:21: Error: Call to undeclared function 'printf' custom-reference-error.c:22: Error: Undeclared identifier 'n' custom-reference-error.c:24: Warning: Control reaches end of non-void function [-Wreturn-type] custom-reference-error.c:24: Warning: Parameter 'argc' is never used custom-reference-error.c:24: Warning: Parameter 'argv' is never used +custom-reference-error.c:26: Error: Constant integer expression expected