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

How to skip non-top level pseudo declarations, #314

Closed
phadej opened this issue Nov 28, 2024 · 2 comments
Closed

How to skip non-top level pseudo declarations, #314

phadej opened this issue Nov 28, 2024 · 2 comments

Comments

@phadej
Copy link
Collaborator

phadej commented Nov 28, 2024

E.g.

typedef struct S5 {
    char a;
    int b;
} S5;

gives

% cabal run clang-ast-dump ex.h 
## `ex.h`

* "S5"
    * cursor kind: simpleEnum CXCursor_StructDecl
    * cursor type: "struct S5"
* "a"
    * parent: "S5"
    * cursor kind: simpleEnum CXCursor_FieldDecl
    * cursor type: "char"
    * field offset: 0
* "b"
    * parent: "S5"
    * cursor kind: simpleEnum CXCursor_FieldDecl
    * cursor type: "int"
    * field offset: 32
* "S5"
    * cursor kind: simpleEnum CXCursor_TypedefDecl
    * cursor type: "S5"
    * typedef name: "S5"
* "S5"
    * cursor kind: simpleEnum CXCursor_StructDecl
    * cursor type: "struct S5"
* "a"
    * parent: "S5"
    * cursor kind: simpleEnum CXCursor_FieldDecl
    * cursor type: "char"
    * field offset: 0
* "b"
    * parent: "S5"
    * cursor kind: simpleEnum CXCursor_FieldDecl
    * cursor type: "int"
    * field offset: 32

also clang agrees with this:

% clang -Xclang -ast-dump -fsyntax-only ex.h
TranslationUnitDecl 0x1bc00458 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x1bc00c80 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x1bc00a20 '__int128'
|-TypedefDecl 0x1bc00cf0 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x1bc00a40 'unsigned __int128'
|-TypedefDecl 0x1bc00ff8 <<invalid sloc>> <invalid sloc> implicit __NSConstantString 'struct __NSConstantString_tag'
| `-RecordType 0x1bc00dd0 'struct __NSConstantString_tag'
|   `-Record 0x1bc00d48 '__NSConstantString_tag'
|-TypedefDecl 0x1bc01090 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x1bc01050 'char *'
|   `-BuiltinType 0x1bc00500 'char'
|-TypedefDecl 0x1bc01388 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list 'struct __va_list_tag[1]'
| `-ConstantArrayType 0x1bc01330 'struct __va_list_tag[1]' 1 
|   `-RecordType 0x1bc01170 'struct __va_list_tag'
|     `-Record 0x1bc010e8 '__va_list_tag'
|-RecordDecl 0x1bc56fb0 <ex.h:1:9, line:4:1> line:1:16 struct S5 definition
| |-FieldDecl 0x1bc57060 <line:2:5, col:10> col:10 a 'char'
| `-FieldDecl 0x1bc570c8 <line:3:5, col:9> col:9 b 'int'
`-TypedefDecl 0x1bc57178 <line:1:1, line:4:3> col:3 S5 'struct S5':'struct S5'
  `-ElaboratedType 0x1bc57120 'struct S5' sugar
    `-RecordType 0x1bc57030 'struct S5'
      `-Record 0x1bc56fb0 'S5'

if someone knows already, please tell.

@phadej
Copy link
Collaborator Author

phadej commented Nov 28, 2024

To clarify, it seems that libclang always presents

typedef struct <name> { ... } bar;

declaration as two, and it doesn't matter whether the struct has an own name (typedef struct foo { ... } bar), or not (typedef struct { ...} bar).

@phadej
Copy link
Collaborator Author

phadej commented Nov 28, 2024

It feels that this is simply how libclang works, the original structure is not preserved in AST.

This means that we cannot really distinguish between

typedef struct foo { ... } bar;

and

struct foo { ... };
typedef struct foo bar;

(We could look at file positions, but we really shouldn't).


This is slightly unfortunate as

typedef struct { ... } foo;

is split into

struct (unnamed at ...) { .. }
typedef struct (unnamed at ...) foo;

This is what I would done in C IR translation #253, but libclang already does this for us. The unfortunate part is that it's hard to figure that out. We'd prefer to know about typedef already when processing the struct, but it doesn't seem to be possible.

@phadej phadej closed this as completed Nov 28, 2024
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

No branches or pull requests

1 participant