Skip to content
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

[c11] Enables type redefinitions #19

Merged
merged 4 commits into from
May 7, 2021
Merged

[c11] Enables type redefinitions #19

merged 4 commits into from
May 7, 2021

Conversation

ivg
Copy link
Member

@ivg ivg commented May 5, 2021

Fixes #9.

ISO/IEC 9899:1999 doesn't allow redefining a type with typedef.

But C11 standard allows redeclaration of typedef names. 6.7/3 says:

    ... a typedef name may be redefined to denote the same type as it
    currently does, provided that type is not a variably modified
    type; ...

The issue is that duplicated typedefs would generate invalid XML
documents if we will just allow them in the grammar, e.g.,


$ echo 'typedef char uint8_t; typedef char uint8_t; void foo(uint8_t);' | ctoxml
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<file>
    <type id="uint8_t" store="auto">
        <char/>
    </type>
    <type id="uint8_t" store="auto">
        <char/>
    </type>
    <fundec id="foo" store="auto">
        <type>
            <void/>
        </type>
        <param name="" store="auto">
            <type ref="uint8_t"/>
        </param>
    </fundec>
</file>

To prevent this, we added an efficient deduplication procedure. This procedure is useful by itself as even before this change the generated XML could be invalid, for example, when several prototypes of the same function occur in the stream (a quite common situation).

At the end we have the following behavior (snippets are taken from our cram tests)

  $ echo 'typedef char uint8_t; typedef char uint8_t; typedef char uint8_t; void foo(uint8_t x);' |  ../printc/printc_bin.exe
  
  typedef char uint8_t;
  
  typedef char uint8_t;
  
  typedef char uint8_t;
  
  void foo(uint8_t x);


  $ echo 'typedef char uint8_t; typedef char uint8_t; typedef char uint8_t; void foo(uint8_t x);' |  ../ctoxml/ctoxml_bin.exe
  <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
  <file>
  	<type id="uint8_t" store="auto">
  		<char/>
  	</type>
  	<fundec id="foo" store="auto">
  		<type>
  			<void/>
  		</type>
  		<param name="x" store="auto">
  			<type ref="uint8_t"/>
  		</param>
  	</fundec>
  </file>

ivg added 4 commits May 5, 2021 17:25
ISO/IEC 9899:1999 didn't allow redefining a type with typedef.

But C11 standard allows redeclaration of typedef names. 6.7/3 says:
```
    ... a typedef name may be redefined to denote the same type as it
    currently does, provided that type is not a variably modified
    type; ...
```

The issue is that duplicated typedefs would generate invalid XML
documents if we will just allow them in the grammar, e.g.,
```

$ echo 'typedef char uint8_t; typedef char uint8_t; void foo(uint8_t);' | ctoxml
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<file>
    <type id="uint8_t" store="auto">
        <char/>
    </type>
    <type id="uint8_t" store="auto">
        <char/>
    </type>
    <fundec id="foo" store="auto">
        <type>
            <void/>
        </type>
        <param name="" store="auto">
            <type ref="uint8_t"/>
        </param>
    </fundec>
</file>
```

And we can't just ignore a definition as the structure of the parser
doesn't allow us to drop global defintions. The solution is to
generate bogus typedefs for retypedefs that use phantom types, e.g.,

```
echo 'typedef char uint8_t; typedef char uint8_t; typedef char uint8_t; void foo(uint8_t x);' | dune exec ./printc/printc_bin.exe

typedef char uint8_t;

typedef char uint8_t$1;

typedef char uint8_t$2;

void foo(uint8_t x);
```

Those typedefs should be just ignored.
@ivg ivg merged commit c019832 into master May 7, 2021
ivg added a commit that referenced this pull request May 7, 2021
I forgot to add so here it is. Most likely it will fail, not because
of the test but because dune will run the test in a wrong package,
where ctoxml is not yet available. But we will see.
ivg added a commit that referenced this pull request May 11, 2021
* adds a test to PR #19

I forgot to add so here it is. Most likely it will fail, not because
of the test but because dune will run the test in a wrong package,
where ctoxml is not yet available. But we will see.

* switch to dune 2.8 package stanza in cram

* adds applies_to to cram stanzas, removes build predicate for dune

* build and test directly instead of using opam

it looks like the cram packages do not work right with opam --with-tests
@ivg ivg deleted the enables-type-redefinitions branch May 11, 2021 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fails to parse re-typedefs
1 participant