www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22103] New: importC: Parser accepts wrong syntax for array

https://issues.dlang.org/show_bug.cgi?id=22103

          Issue ID: 22103
           Summary: importC: Parser accepts wrong syntax for array
                    declarators
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ibuclaw gdcproject.org

Syntax in 6.7.6 is:
---
direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
direct-declarator [ static type-qualifier-list(opt) assignment-expression ]
direct-declarator [ type-qualifier-list static assignment-expression ]
direct-declarator [ type-qualifier-list(opt) * ]
---

But CParser instead parses C code as if it is instead:
---
direct-declarator [ assignment-expression(opt) ] type-qualifier-list(opt)
direct-declarator [ static assignment-expression(opt) ]
type-qualifier-list(opt)
direct-declarator [ * assignment-expression(opt) ] type-qualifier-list(opt)
---

E.g:  This code is wrong, but dmd compiles it as valid.
---
void test(int arr[4] const, int val);

int main()
{
  int array[4] const;
  test(array, 4);
  return 0;
}
---

Conversely, this code is correct, but dmd rejects it.
---
void test(int arr[restrict 4], int val);

int main()
{
  int array[4];
  test(array, 4);
  return 0;
}
---

Implementation detail (C11 6.7.6.2-1): The optional type-qualifiers and the
keyword `static` shall appear only in a declaration of a function parameter
with an array type, and then only in the outermost array type derivation.

Meaning , this code should be rejected:
---
void badparam1(int arr[4][restrict]);
void badparam2(int arr[4][static 2]);
int var[static 4];
typedef int carray[const];
---
etc...

--
Jul 05