Difference between revisions of "Common problems when converting C header files/fr"

From Free Pascal wiki
Jump to navigationJump to search
Line 128: Line 128:
 
==Empty type/var/const sections==
 
==Empty type/var/const sections==
  
After the above tools removed some variables, types, constants some setions become empty, which is not allowed in pascal.
+
Après que les outills du dessus aient enlevé quelques variables, types, constantes, des sections deviennent vides, ce qui n'est pas permis en Pascal.
  
Fix: Add '''Remove empty type/var/const sections''' or the '''Post H2Pas''' tool to the '''After h2pas''' tools.
+
Correction: Ajouter '''Remove empty type/var/const sections''' ou l'outil '''Post H2Pas''' aux outils '''After h2pas'''.
  
 
==h2pas forgets to add IFDEFs for function bodies==
 
==h2pas forgets to add IFDEFs for function bodies==

Revision as of 08:49, 25 June 2022

English (en) français (fr)

h2pas rapporte une erreur

Voilà une liste des structures C courantes qui ne sont pas reconnues par h2pas et comment les corriger: (NdT: les titres ne sont pas traduits pour correspondre au message de l'outil)

Problème h2pas : extern "C"

Certains fichiers d'entête contiennent le bloc espace de nom de C++:

#if defined(__cplusplus)
extern "C" {
#endif
...
#if defined(__cplusplus)
}
#endif

Correction: Ajouter les lignes Supprimer les lignes 'extern "C" ' de C++ dans les outils de la page Avant h2pas.

Problème h2pas : Empty macro

Certains fichiers d'en-tête contiennent des macros vides utilisées pour d'autres extensions:

#define MPI_FILE_DEFINED

Correction: Ajouter la ligne Remove empty C macros dans les outils de la page Avant h2pas.

Problème h2pas : Implicit array types

C autorise des tableaux implicites dans les types de paramètre. Par exemple:

int MPI_Group_range_incl(MPI_Group, int, int [][3], MPI_Group *);

int [][3] est un type implicit, qui n'est pas permis dans Pascal. h2pas supporte l'ajout de types de pointer. Ainsi, il suffit de remplacer tous les [] avec *.

Correction: Ajouter la ligne Remplacer [] par * dans les outils de la page Avant h2pas.

Problème h2pas : Macros for 0 pointers

Certains fichiers d'entête contiennent de pointeurs typés 0:

#define MPI_BOTTOM      (void *)0

Correction: Ajouter la ligne Replace macro values 0 pointer like (char *)0 with NULL dans les outils de la page Avant h2pas.

Problème h2pas : function types are silently skipped

C permet de définir des types de fonctions, alros que Pascal ne permet que des pointeurs vers des fonctions (NdT: point à vérifier, Free Pascal supporte les types procéduraux). Par exemple:

typedef int (MPI_Comm_copy_attr_function)(MPI_Comm, int, void *, void *,
                                        void *, int *);

Le fichier d'entête n'utilise jamais cette définition directement, au lieu de cela, il utilise toujours des pointeurs de tels types MPI_Comm_copy_attr_function*, par exemple :

#define MPI_COMM_NULL_COPY_FN ((MPI_Comm_copy_attr_function*)0)

La traduction peut fonctionner en procédant ainsi: faire du type de fonction un pointeur. Ce qui signifie d'ajouter un * devant la définition et de faire précéder par un P le nom et de retirer le * après chaque référence. Par exemple:

typedef int (*PMPI_Comm_copy_attr_function)(MPI_Comm, int, void *, void *,
                                        void *, int *);
#define MPI_COMM_NULL_COPY_FN ((PMPI_Comm_copy_attr_function)0)

Solution: Ajouter l'outil Convertir les types de fonction en pointeurs dans les outils de la page Avant h2pas.

Compilation de test et ajouter des outils pour embellir la sortie

Lorsque h2pas s'exécute sans erreur, il a créé un fichier Pascal. Le commutateur -i définit s'il s'agit d'une unité ou d'un fichier d'inclusion. La prochaine étape est le test de compilation. Préparez un projet de test qui utilise le nouveau source Pascal. Ensuite utilisez le bouton Exécuter H2Pas et compiler dans l'assistant h2pas.

Exemple: MPICH2

Créer un nouveau projet avec Projet -> Nouveau Projet -> Programme. Enregistrez-le dans mpihelloworld. Modifiez le code:

program MPIHelloWorld;

{$mode objfpc}{$H+}
{$linklib mpich}
{$linklib rt}

uses
  {$IFDEF UNIX}pthreads{$ENDIF}, MPI;

begin
  MPI_Init(@argc, @argv);
  writeln('Hello, world.');
  MPI_Finalize;
end.

Ajoutez le h2p/mpi.pas au projet. Projet -> Ajouter le fichier de l'éditeur au projet

Erreurs courantes du compilateur dans la sortie h2pas

Parfois, h2pas ne produit pas du code Pascal valide. Voici les problèmes courants et comment les résoudre.

Unit name contains the file path

Parfois h2pas ajoute le chemin complet au nom d'unité.

Résolution: Ajouter Remplacement de "unit filename;" par "unit name;" dans les outils de la page Avant h2pas.

Missing include files

h2pas convertit les directives #include en directives pascal {$i }. Si vous créez une unité pour chaque fichier d'entête C alors vopus pouvez retirer toutes les directives avec l'outil Suppression des directives incluses.

Forward type not resolved

Par exemple mpi.pas(26,16) Error: Forward type not resolved "PMPI_Aint" La ligne d'erreur est souvent sur un type pointeur vers un enregistrement:

PMPI_Aint = ^MPI_Aint;

h2pas ajoute PMPI_Aint pour donner un nom au pointeurs C anonymes comme *MPI_Aint. Il ajoute les pointeurs au début du fichier. Mais Pascal exige que la définition soit une section de même type.

Parfois h2pas ajoute un type pointer alors qu'il existe déjà.

Résolution: Ajouter Supprimer les types de pointeurs redéfinis dans les outils Après h2pas.

Removing system types

h2pas ajoute quelques types système, comme PLongint, qui font désormais partie de l'unité System.

Résolution: Ajouter l'outil Supprimer les redéfinitions de type comme "PLongint" aux outils Après h2pas.

Empty type/var/const sections

Après que les outills du dessus aient enlevé quelques variables, types, constantes, des sections deviennent vides, ce qui n'est pas permis en Pascal.

Correction: Ajouter Remove empty type/var/const sections ou l'outil Post H2Pas aux outils After h2pas.

h2pas forgets to add IFDEFs for function bodies

h2pas converts macros to functions. But if the macro was in an IFDEF block, h2pas does not add IFDEFs around the function body.

Fix: Add the tool Add missing h2pas IFDEFs for function bodies or the Post H2Pas. Add this right in front of the tool Reduce compiler directives in pascal file.

Implicit Types

C allows implicits types in parameters. For example: int [][3]. Pascal does not allow this, so you must give it a name and declare a type. But h2pas does not do this automatically and adds instead the implicit type in a quasi pascal syntax. For example:

int some_func(int [][3]);

becomes

function some_func(_para1:array[0..2] of Plongint):longint;cdecl;external;

Fix: Luckily there is now a tool to remove these implicit types and add explicit types, it's called Replace implicit types. It is also part of the Post H2Pas tool. Add it to the after tool list.

Array of nothing

Sometimes h2pas converts the C ellipsis parameter type '...' wrongly to 'array of )'. It should be 'array of const)'.

Fix: Add the tool Fix open arrays or the Post H2Pas to the after tools.

Identifier not found

There are three cases:

The identifier is defined in the unit, but after it is used

In pascal forward defined types are only allowed in a type section. For example:

type
  TMyRecord = record
    Next: PMyRecord; // using the forward definition
  end;
  PMyRecord = ^TMyRecord;

The below is not allowed, because the forward definition is in another type section:

type
  TMyRecord = record
    Next: PMyRecord; // using the forward definition
  end;
type
  PMyRecord = ^TMyRecord;

Solution: The code must be reordered. Add the tool fix forward definitions-

The identifier is defined in another unit (another .h file)

Solution 1: Add the unit to the uses section

If the other unit is already using this unit, then you have a circle. A circle dependency is allowed between .h files, but not between pascal units. In this case you must move code between both units or use IFDEFs like the gtk2 bindings or use the below merge function of the h2pas wizard.

Solution 2: Merge the two include files

The h2pas wizard can merge include files into one. For example first.h includes via the c include directive the file second.h. Select the second.h in the wizard and check the 'Merge file' feature. Then the source of second.h' will be appended to first.h before sending the output to h2pas. As always: This does not change the header files.


Exemple: MPICH2

The type MPI_Request is defined in mpi.h and used in mpio.h. Both c header files heavily use each other, so you can not put them into two separated units. Select mpio.h and enable the 'Merge file' function.

The identifier is a pointer to an existing type, but h2pas forgot to add it

Solution: Add the tool Add missing pointer types like PPPChar or the Post H2Pas tool to the after H2Pas tools.

The identifier is not defined anywhere

Probably you are missing a header file or you are using one with the wrong version or it is private identifier.

Solution: Search the header file and convert it too. If you can not find it or you don't want to translate this file, you can comment the identifier or replace it.

Exemple: MPICH2

Note: this is just a demonstration. The real solution for the MPICH2 headers is to setup the Defines correct, so that the below example vanishes automatically.

The mpi.h file defines MPI_File as pointer of ADIOI_FileD, but ADIOI_FileD is not defined public:

typedef struct ADIOI_FileD *MPI_File;

The h2pas tool translated this to:

MPI_File = ^ADIOI_FileD;

Because ADIOI_FileD is not defined public, it is a private structure, so you can simply replace it with a Pointer: Add to the 'After h2pas' tools a new tool of type 'Search and replace' with the following properties:

Propriété Valeur
Caption Replace ADIOI_FileD with Pointer
Name ReplaceADIOI_FileDwithPointer
SearchFor ADIOI_FileD
ReplaceWith Pointer
Options [trtMatchCase,trtWholeWord]

Illegal expression

Exemple: MPICH2

Compiler gives Illegal expression on the following statement

const
   MPI_LONG_LONG = MPI_LONG_LONG_INT;

The reason is, that h2pas translated the MPI_LONG_LONG_INT constant to a function.

Solution: Fix h2pas or use a trick:

Replace the

#define MPI_LONG_LONG   MPI_LONG_LONG_INT

with

#define MPI_LONG_LONG      ((MPI_Datatype)0x4c000809)

by adding a Search and replace tool before h2pas.

Autres problèmes courants

The pascal file contains a lot of unneccessary ifdef

h2pas translates even the C #ifdef directives. Many of them are C specific and not needed under FPC and can even make it impossible many tools on this side to explore the code. That's why there is a tool to clean up and disable or remove many unneeded IFDEFs: Reduce compiler directives in pascal file. Do not forget to insert the tool Add missing h2pas IFDEFs for function bodies in front of it.

Hints about unneeded directives ($IFDEFs, $DEFINEs, ...)

You can see the directives in the Code Explorer.

The tool will only remove the $IFDEFs and $DEFINEs, that are unneeded on all platforms. That means for example a {$IFDEF CPU386} will not be removed - even if you are currently working on an intel 32 bit compatible machine it will not be removed. This allows to create platform independent bindings. Of course C headers often contain a lot of IFDEFs not needed under FPC (Delphi, Kylix, gpc, ...).

For example: Many C headers are enclosed in

{$IFNDEF FOO}
{$DEFINE FOO}
...
{$ENDIF}

This trick allows to include a C header file multiple times - like the units under pascal. So, pascal does not need this. Therefore the tool Reduce compiler directives in pascal file and the Post H2Pas tool have two properties: Defines and Undefines. Click on the ... button of the property to edit them. Each line can contain a macroname, that should be (un)defined. Add the line FOO to the Undefines. This way the $IFNDEF FOO is always true and the tool will remove it. The $DEFINE FOO is now unneeded too and will be removed too.

Exemple: MPICH2

Add the following to the Undefines property:

 MPI_INCLUDED
 MPIO_INCLUDE
 NEEDS_MPI_FINT
 MPI_BUILD_PROFILING
 MPICH_SUPPRESS_PROTOTYPES
 MPI_Wtime
 HAVE_MPICH2G2
 FOO
 HAVE_PRAGMA_HP_SEC_DEF


Add the following to the Defines property:

 MPICH2
 MPICH_NEW_MPIIO

The C header files contain redefinitions

This is ok for C compilers, because a type is the same if its definition is the same. But for Pascal each type is a different type.

Solution: Add the tool Remove redefinitions in pascal unit or the Post H2Pas to the after h2pas tools. As the name implies, it only works on units, not on include files. And it expect at least a valid pascal syntax. That's why it is important to add this tool after the tools that fix the pascal syntax like Replace "unit filename;" with "unit name;", Remove empty type/var/const sections, Remove includes, Replace implicit types and Fix open arrays.

Exemple: MPICH2

The mpi.h contains several redefinitions like MPIIMPL_HAVE_MPI_TYPE_CREATE_DARRAY. They can all be fixed with this tool.

Alias macros are always translated by h2pas to constants

h2pas converts macros like

#define A B

to

const A = B;

which is almost always correct. But some c header contain aliases for types, variables and functions too.

Example: MPICH2

The mpi.h contains the macro

#define MPI_LONG_LONG      MPI_LONG_LONG_INT

h2pas converts this simply to

const
   MPI_LONG_LONG = MPI_LONG_LONG_INT;

but MPI_LONG_LONG_INT is a function.


Solution: Add the tool Fixes section type of alias definitions in pascal unit or the Pre H2Pas tool to the Before H2Pas tools.

Note: This tool can also fix alias to external functions. For example:

function ExternalFunc1(_para1:longint; _para2:pointer):longint;cdecl;external name 'ExternalFunc1';
const
  ExternalFuncAlias1 = ExternalFunc1;// should be replaced with full declaration

h2pas creates functions for constants

Par exemple:

function MPI_2COMPLEX : MPI_Datatype;
begin
  MPI_2COMPLEX:=MPI_Datatype(MPI_DATATYPE_NULL);
end;

All used identifiers are types and constants, so this function can be replaced with a simple constant. This is checked and done by the tool Replace simple functions with constants or the Post H2Pas.

h2pas creates functions for type casts

Par exemple:

function PMPI_Win_f2c(win : longint) : MPI_Win;
begin
   PMPI_Win_f2c:=MPI_Win(win);
end;

This function can be replaced with a simple type, resulting in a type cast. This is checked and done by the tool Replace simple functions with type casts or the Post H2Pas tool.

Some identifiers are used before they are defined

C allows much more forward definitions than pascal, so the definitions must be topologically sorted. For example:

type
  PMPI_Datatype  = ^MPI_Datatype;
  MPI_Datatype = longint;

The pointer PMPI_Datatype must be moved to the same type section as MPI_Datatype.

Solution: Add the Fix forward definitions by reordering or the Pre H2Pas tool to the Before H2Pas tools. The tool should be put after the tools fixing redefinitions and types. At the moment the tool only moves pointer types. ToDo: reorder some more definitions like constants.

English (en) français (fr)

The C header files do not contain parameter names

For example there is an ugly function without parameter names:

int MPI_Intercomm_create(MPI_Comm, int, MPI_Comm, int, int, MPI_Comm * );

The parameter names are defined in the corresponding .c file:

int MPI_Intercomm_create(MPI_Comm local_comm, int local_leader, 
                         MPI_Comm peer_comm, int remote_leader, int tag, 
                         MPI_Comm *newintercomm)

Solution: There is no solution yet. Except manual editing or searching for other .h files. Often such .h files were auto generated and there are some nicer header files somewhere on the internet.

Proposal: Write a clever tool, that searches functions with missing parameter names and the corresponding functions in the .c files with parameter names and improve the .h file.

Voir aussi