Skip to content

Commit

Permalink
fix Issue 22030 - importC: Wrong error with bad declarator
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuclaw authored and dlang-bot committed Jun 17, 2021
1 parent 8ce3da5 commit 52ee0a2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/dmd/cparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -1412,15 +1412,17 @@ final class CParser(AST) : Parser!AST

/* If a declarator does not follow, it is unnamed
*/
if (token.value == TOK.semicolon &&
tspec && tspec.isTypeTag())
if (token.value == TOK.semicolon && tspec)
{
nextToken();
auto tt = tspec.isTypeTag();
if (!tt)
return; // legal but meaningless empty declaration

/* If anonymous struct declaration
* struct { ... members ... };
* C11 6.7.2.1-13
*/
nextToken();
auto tt = tspec.isTypeTag();
if (!tt.id && tt.members)
{
if (level != LVL.member)
Expand Down Expand Up @@ -1472,7 +1474,7 @@ final class CParser(AST) : Parser!AST
dt = AST.Type.tuns32;
}
else
dt = cparseDeclarator(tspec, id);
dt = cparseDeclarator(DTR.xdirect, tspec, id);
if (!dt)
{
panic();
Expand All @@ -1497,16 +1499,6 @@ final class CParser(AST) : Parser!AST
dt = dt.addSTC(STC.const_);
}

if (!id) // no identifier
{
if (dt !is tspec)
{
error("identifier or `(` expected"); // )
panic();
break;
}
}

/* GNU Extensions
* init-declarator:
* declarator simple-asm-expr (opt) gnu-attributes (opt)
Expand All @@ -1533,7 +1525,10 @@ final class CParser(AST) : Parser!AST
error("attributes should be specified before the function definition");
auto t = &token;
if (skipBraces(t))
{
token = *t;
return;
}
}
}
break;
Expand Down Expand Up @@ -2210,6 +2205,7 @@ final class CParser(AST) : Parser!AST
* identifier-list , identifier
*
* Params:
* declarator = declarator kind
* t = base type to start with
* pident = set to Identifier if there is one, null if not
* storageClass = any storage classes seen so far
Expand All @@ -2218,10 +2214,10 @@ final class CParser(AST) : Parser!AST
* symbol table for the parameter-type-list, which will contain any
* declared struct, union or enum tags.
*/
private AST.Type cparseDeclarator(AST.Type t, out Identifier pident,
StorageClass storageClass = 0)
private AST.Type cparseDeclarator(DTR declarator, AST.Type t,
out Identifier pident, StorageClass storageClass = 0)
{
//printf("cparseDeclarator()\n");
//printf("cparseDeclarator(%d)\n", declarator);

AST.Type ts;
while (1)
Expand All @@ -2240,7 +2236,7 @@ final class CParser(AST) : Parser!AST
* T ((*fp))();
*/
nextToken();
ts = cparseDeclarator(t, pident);
ts = cparseDeclarator(declarator, t, pident);
check(TOK.rightParenthesis);
break;

Expand All @@ -2253,6 +2249,11 @@ final class CParser(AST) : Parser!AST
continue;

default:
if (declarator == DTR.xdirect)
{
error("identifier or `(` expected"); // )
panic();
}
ts = t;
break;
}
Expand Down Expand Up @@ -2395,7 +2396,7 @@ final class CParser(AST) : Parser!AST
Specifier specifier;
auto tspec = cparseSpecifierQualifierList(LVL.global, specifier);
Identifier id;
auto dt = cparseDeclarator(tspec, id);
auto dt = cparseDeclarator(DTR.xabstract, tspec, id);
if (id)
error("identifier not allowed in abstract-declarator");
return dt;
Expand Down Expand Up @@ -2458,7 +2459,7 @@ final class CParser(AST) : Parser!AST
auto tspec = cparseDeclarationSpecifiers(LVL.prototype, specifier);

Identifier id;
auto t = cparseDeclarator(tspec, id);
auto t = cparseDeclarator(DTR.xabstract, tspec, id);
if (specifier.mod & MOD.xconst)
t = t.addSTC(STC.const_);
auto param = new AST.Parameter(STC.parameter, t, id, null, null);
Expand Down Expand Up @@ -3591,6 +3592,13 @@ final class CParser(AST) : Parser!AST
member = 0x10, /// struct member
}

/// Types of declarator to parse
enum DTR
{
xdirect = 1, /// C11 6.7.6 direct-declarator
xabstract = 2, /// C11 6.7.7 abstract-declarator
}

/// C11 6.7.1 Storage-class specifiers
enum SCW : uint
{
Expand Down
6 changes: 6 additions & 0 deletions test/fail_compilation/failcstuff1.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ fail_compilation/imports/cstuff1.c(305): Error: storage class not allowed in spe
fail_compilation/imports/cstuff1.c(306): Error: storage class not allowed in specifier-qualified-list
fail_compilation/imports/cstuff1.c(307): Error: storage class not allowed in specifier-qualified-list
fail_compilation/imports/cstuff1.c(308): Error: storage class not allowed in specifier-qualified-list
fail_compilation/imports/cstuff1.c(401): Error: identifier or `(` expected
fail_compilation/imports/cstuff1.c(402): Error: identifier or `(` expected
fail_compilation/imports/cstuff1.c(403): Error: identifier or `(` expected
fail_compilation/imports/cstuff1.c(408): Error: identifier or `(` expected
fail_compilation/imports/cstuff1.c(409): Error: identifier or `(` expected
fail_compilation/imports/cstuff1.c(410): Error: identifier or `(` expected
---
*/
import imports.cstuff1;
18 changes: 18 additions & 0 deletions test/fail_compilation/imports/cstuff1.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ struct S22029
auto int afield;
register int rfield;
};

// https://issues.dlang.org/show_bug.cgi?id=22030
#line 400
int;
int *;
int &;
int , int;

struct S22030
{
int;
int *;
int &;
int, int;
int _;
};

void test22030(struct S22030, struct S22030*, struct S22030[4]);

0 comments on commit 52ee0a2

Please sign in to comment.