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

c_parser: support parenthesized compounds #423

Merged
merged 3 commits into from Jul 7, 2021
Merged

c_parser: support parenthesized compounds #423

merged 3 commits into from Jul 7, 2021

Conversation

JordanYates
Copy link
Contributor

Add support for parenthesized compound statements as per #422.

Below is a source snippet and corresponding generated AST (as generated by ast.show())

void foo()
{
	int a;

	({});
	({ 1; });
	({ 1; 2; });
	int b = ({ 1; });
	int c, d = ({ int x = 1; x + 2; });

	a = ({ int x = 1; 2 * x; });
}
FileAST:
  FuncDef:
    Decl: foo, [], [], []
      FuncDecl:
        TypeDecl: foo, []
          IdentifierType: ['void']
    Compound:
      Decl: a, [], [], []
        TypeDecl: a, []
          IdentifierType: ['int']
      Compound:
      Compound:
        Constant: int, 1
      Compound:
        Constant: int, 1
        Constant: int, 2
      Decl: b, [], [], []
        TypeDecl: b, []
          IdentifierType: ['int']
        Compound:
          Constant: int, 1
      Decl: c, [], [], []
        TypeDecl: c, []
          IdentifierType: ['int']
      Decl: d, [], [], []
        TypeDecl: d, []
          IdentifierType: ['int']
        Compound:
          Decl: x, [], [], []
            TypeDecl: x, []
              IdentifierType: ['int']
            Constant: int, 1
          BinaryOp: +
            ID: x
            Constant: int, 2
      Assignment: =
        ID: a
        Compound:
          Decl: x, [], [], []
            TypeDecl: x, []
              IdentifierType: ['int']
            Constant: int, 1
          BinaryOp: *
            Constant: int, 2
            ID: x

Signed-off-by: Jordan Yates jordan.yates@data61.csiro.au

Jordan Yates added 3 commits July 7, 2021 11:45
Support parenthesized compound statements as described here:
    https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Add support to `expand_init` for additional `c_ast` types. If a type
is not explicitly handled, return the type name instead of `None`.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Add parsing tests for various situations of parenthesized compound
statements. The complete tree generated by the test string is:

```
FileAST:
  FuncDef:
    Decl: foo, [], [], []
      FuncDecl:
        TypeDecl: foo, []
          IdentifierType: ['void']
    Compound:
      Decl: a, [], [], []
        TypeDecl: a, []
          IdentifierType: ['int']
      Compound:
      Compound:
        Constant: int, 1
      Compound:
        Constant: int, 1
        Constant: int, 2
      Decl: b, [], [], []
        TypeDecl: b, []
          IdentifierType: ['int']
        Compound:
          Constant: int, 1
      Decl: c, [], [], []
        TypeDecl: c, []
          IdentifierType: ['int']
      Decl: d, [], [], []
        TypeDecl: d, []
          IdentifierType: ['int']
        Compound:
          Decl: x, [], [], []
            TypeDecl: x, []
              IdentifierType: ['int']
            Constant: int, 1
          BinaryOp: +
            ID: x
            Constant: int, 2
      Assignment: =
        ID: a
        Compound:
          Decl: x, [], [], []
            TypeDecl: x, []
              IdentifierType: ['int']
            Constant: int, 1
          BinaryOp: *
            Constant: int, 2
            ID: x
```

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Copy link
Owner

@eliben eliben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed tests; looks good

@eliben eliben merged commit 56b1467 into eliben:master Jul 7, 2021
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.

None yet

2 participants