简体   繁体   中英

Clarification sought on validity and reason for “empty” C struct definition in Python CFFI definition file

I am reading some code,and came across this rather odd C struct definition:

Can anyone explain (with references if possible):

  1. If this is a valid struct definition

  2. What would be the purpose of such a definition (where no fields/members are defined)?

    typedef struct dataObject {...; } DATA_OBJECT;

[[ Edit ]]

The code above is available here

If this is a valid struct definition

No.

What would be the purpose of such a definition (where no fields/members are defined)?

The file purpose is to provide the python CFFI parser with type and function declarations to use.

The purpose of this file is to preprocessed by python CFFI ffibuilder.cdef() . From letting C compiler fill the gaps :

Moreover, you can use “...” (literally, dot-dot-dot) in the cdef() at various places, in order to ask the C compiler to fill in the details. These places are:

  • structure declarations: any struct { } or union { } that ends with “...;” as the last “field” is partial: it may be missing fields, have them declared out of order, use non-standard alignment, etc. Precisely, the field offsets, total struct size, and total struct alignment deduced by looking at the cdef are not relied upon and will instead be corrected by the compiler. (But note that you can only access fields that you declared, not others.) Any struct declaration which doesn't use “...” is assumed to be exact, but this is checked: you get an error if it is not correct.
  • [...]
  • unknown types: [....] In some cases you need to say that foo_t is not opaque, but just a struct where you don't know any field; then you would use typedef struct {...; } foo_t; typedef struct {...; } foo_t; .

I suspect it means to the CFFI that struct dataObject and DATA_OBJECT are opaque types meant to be used only as pointers and the CFFI parser does not support struct declarations.

The file is used here in clips_build.py as I understand to build clipspy python interface to C.

  1. If this is a valid struct definition

No it isn't. To grab part of the C17 6.7.2.1 formal grammar:

struct-declaration:
specifier-qualifier-list struct-declarator-list opt ;
static_assert-declaration

So to begin with, the struct needs to contain a "specifier-qualifier list" which in plain English is the const int etc stuff before the variable name. Since this isn't present, gcc for example whines about a syntax error:

error: expected specifier-qualifier-list before '...' token


2.What would be the purpose of such a definition (where no fields/members are defined)?

I'm guessing it's either pseudo code or a dev "TODO" where they committed code that doesn't compile, since it has yet to be written.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM