Difference between revisions of "CHelper"

From havefunsoft wiki
Jump to: navigation, search
m (Code=)
m (__attributes__)
 
(12 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
==Code==
 
==Code==
 
Public Repository: https://svn.code.sf.net/p/lazarus-ccr/svn/components/chelper/
 
Public Repository: https://svn.code.sf.net/p/lazarus-ccr/svn/components/chelper/
 +
==CConvert==
 +
cconvert is a command-line utility that's used for parsing headers. It's designed to be a stand-alone parser. The converted should produce read-for-use pascal headers.
 +
 +
-defines - allows to add additional C-preprocess definitions. This is used to add additional defines that are defined outside of the converted the converted header. It's allowed to define multiple definition files. If two different definitions are sharing the same name, the later definition takes precedence.
 +
 +
It's quite common for a C library to have one base headers that's used to store definitions. The base header is (in)directly included by other headers of the library. Definitions declared in the base headers would be used by other headers of the library.
 +
 +
-configuration file represents pascal code generation settings. The format of the file is .ini file. Following sections and values are expected to be in the file.
 +
 +
Section [Main]
 +
;RecordsArePacked (0 / 1 default): if set, the pascal code would explicitly set "packed" record for each C-struct defined
 +
;FuncsAreExternal (0 / 1 default): if set, any function declaration is considered as external function (otherwise "extern" modified is expected). You want to set this flag to 0, if you're intent to use static object files, rather than dynamically loaded library
 +
;EnumAsConst (0 / 1 default): the flag indicates, if enumerations should be declare in pascal as (integer) constants. Having enumeration declare as constants, rather than enumerations has it's own benefits, such as extensibility (an integer would accept any value) and well-defined file size (by default, pascal enumerations byte size, depends on the number of elements. C enumerations by default as 4-bytes, matching "int").
 +
;TypeNamePrefix (string): the prefix that should be added to each non-pointer type. It's a common convention to name types in object pascal starting with "T". However, when creating an interface to C, it's usually omitted.
 +
;RefTypeNamePrefix (string, default "P"): prefix that should be added to each reference type (pointer). I'ts a common convention to name reference types in object pascal starting with "P". C syntax allows NOT to declare a special type name for reference type, for Pascal the ref-type declaration is required. Since there's usually no corresponding C-type it's fine to put "P" prefix for these auxilary types.
 +
;FuncConv (string, default "cdecl"): function calling convention that should be used. When creating an interface for Win.DLL, it's likely that convention should be set to "stdcall"
 +
;FuncDeclPostfix (string): additional pascal verbiage that should be added after the pascal function declaration (after function convention)
 +
;ParamPrefix (string, default "par"): name of parameter, that's used for "nameless" function declarations (common for C++ dialect or C)
 +
:ExtLibName (string): the library name that should be added for "external" modified in pascal declarations.
 +
Section (Types)
 +
The section consists of pair - CType=Pascal Type name.
 +
bool=LongBool
 +
double=Double
 +
float'=Single
 +
float*=PSingle
 +
It suggests the pascal name equivalent for a C-declared type. Useful to avoid reserved named conflicts.
 +
==Compiler Features==
 +
Different C/C++ compiler might have the compiler specific syntax modifiers. It's assume that CConvert should handle them. However, if it's not, it should be possible to overcome the limitation by using preprocessor by defining a macros.
 +
===__attributes__===
 +
The directive was introduced in [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html#Attribute-Syntax GCC] to give a hint to the compiler about the code
 +
 +
Here's an example from svn_error.h (1.6.9)
 +
/** Create an error structure with the given @a apr_err and @a child,
 +
  * with a printf-style error message produced by passing @a fmt, using
 +
  * apr_psprintf().
 +
  */
 +
svn_error_t *
 +
svn_error_createf(apr_status_t apr_err,
 +
                  svn_error_t *child,
 +
                  const char *fmt,
 +
                  ...)
 +
  __attribute__ ((format(printf, 3, 4)));
 +
 +
The cconvert is unable to parse the compiler directive. Instead of parsing it you could define it as a custom definition
 +
custom_defines.h:
 +
 +
  #define __attribute__ (x, ...)
 +
 +
That will cause the cconvert it ignore the directive.
 +
 +
cconvert would stop with an error on __attribute__. But if the custom define is used...
 +
  cconvert -defines  custom_defines.h svn_error.h
 +
...it would just ignore it and generate:
 +
function svn_error_createf(apr_err: apr_status_t; child: Psvn_error_t;
 +
  fmt: PChar; par3: array of const): Psvn_error_t; cdecl; external;

Latest revision as of 09:36, 3 March 2015

chelper is C/C++/ObjC headers converting to pascal utility.

Read more about it on freepascal wiki

Code

Public Repository: https://svn.code.sf.net/p/lazarus-ccr/svn/components/chelper/

CConvert

cconvert is a command-line utility that's used for parsing headers. It's designed to be a stand-alone parser. The converted should produce read-for-use pascal headers.

-defines - allows to add additional C-preprocess definitions. This is used to add additional defines that are defined outside of the converted the converted header. It's allowed to define multiple definition files. If two different definitions are sharing the same name, the later definition takes precedence.

It's quite common for a C library to have one base headers that's used to store definitions. The base header is (in)directly included by other headers of the library. Definitions declared in the base headers would be used by other headers of the library.

-configuration file represents pascal code generation settings. The format of the file is .ini file. Following sections and values are expected to be in the file.

Section [Main]

RecordsArePacked (0 / 1 default)
if set, the pascal code would explicitly set "packed" record for each C-struct defined
FuncsAreExternal (0 / 1 default)
if set, any function declaration is considered as external function (otherwise "extern" modified is expected). You want to set this flag to 0, if you're intent to use static object files, rather than dynamically loaded library
EnumAsConst (0 / 1 default)
the flag indicates, if enumerations should be declare in pascal as (integer) constants. Having enumeration declare as constants, rather than enumerations has it's own benefits, such as extensibility (an integer would accept any value) and well-defined file size (by default, pascal enumerations byte size, depends on the number of elements. C enumerations by default as 4-bytes, matching "int").
TypeNamePrefix (string)
the prefix that should be added to each non-pointer type. It's a common convention to name types in object pascal starting with "T". However, when creating an interface to C, it's usually omitted.
RefTypeNamePrefix (string, default "P")
prefix that should be added to each reference type (pointer). I'ts a common convention to name reference types in object pascal starting with "P". C syntax allows NOT to declare a special type name for reference type, for Pascal the ref-type declaration is required. Since there's usually no corresponding C-type it's fine to put "P" prefix for these auxilary types.
FuncConv (string, default "cdecl")
function calling convention that should be used. When creating an interface for Win.DLL, it's likely that convention should be set to "stdcall"
FuncDeclPostfix (string)
additional pascal verbiage that should be added after the pascal function declaration (after function convention)
ParamPrefix (string, default "par")
name of parameter, that's used for "nameless" function declarations (common for C++ dialect or C)
ExtLibName (string): the library name that should be added for "external" modified in pascal declarations.

Section (Types) The section consists of pair - CType=Pascal Type name.

bool=LongBool
double=Double
float'=Single
float*=PSingle

It suggests the pascal name equivalent for a C-declared type. Useful to avoid reserved named conflicts.

Compiler Features

Different C/C++ compiler might have the compiler specific syntax modifiers. It's assume that CConvert should handle them. However, if it's not, it should be possible to overcome the limitation by using preprocessor by defining a macros.

__attributes__

The directive was introduced in GCC to give a hint to the compiler about the code

Here's an example from svn_error.h (1.6.9)

/** Create an error structure with the given @a apr_err and @a child,
 * with a printf-style error message produced by passing @a fmt, using
 * apr_psprintf().
 */
svn_error_t *
svn_error_createf(apr_status_t apr_err,
                  svn_error_t *child,
                  const char *fmt,
                  ...)
  __attribute__ ((format(printf, 3, 4)));

The cconvert is unable to parse the compiler directive. Instead of parsing it you could define it as a custom definition

custom_defines.h:

 #define __attribute__ (x, ...)

That will cause the cconvert it ignore the directive.

cconvert would stop with an error on __attribute__. But if the custom define is used...

 cconvert -defines  custom_defines.h svn_error.h

...it would just ignore it and generate:

function svn_error_createf(apr_err: apr_status_t; child: Psvn_error_t; 
 fmt: PChar; par3: array of const): Psvn_error_t; cdecl; external;