added c header file example with lots of typical c constructs

git-svn-id: trunk@14141 -
This commit is contained in:
mattias 2008-02-15 17:41:20 +00:00
parent a1f054d578
commit 9ee445c286
4 changed files with 314 additions and 0 deletions

3
.gitattributes vendored
View File

@ -103,6 +103,8 @@ components/codetools/examples/fixh2pasdirectives.lpi svneol=native#text/plain
components/codetools/examples/fixh2pasdirectives.lpr svneol=native#text/plain
components/codetools/examples/getcontext.lpi svneol=native#text/plain
components/codetools/examples/getcontext.lpr svneol=native#text/plain
components/codetools/examples/h2pastest.lpi svneol=native#text/plain
components/codetools/examples/h2pastest.lpr svneol=native#text/plain
components/codetools/examples/identifiercompletion.lpi svneol=native#text/plain
components/codetools/examples/identifiercompletion.lpr svneol=native#text/plain
components/codetools/examples/listinterfaceclasses.lpi svneol=native#text/plain
@ -127,6 +129,7 @@ components/codetools/examples/scanexamples/modemacpas.pas svneol=native#text/pla
components/codetools/examples/scanexamples/overloadedfunction.pas svneol=native#text/plain
components/codetools/examples/scanexamples/resourcetest1.pas svneol=native#text/plain
components/codetools/examples/scanexamples/simpleunit1.pas svneol=native#text/plain
components/codetools/examples/scanexamples/test.h svneol=native#text/plain
components/codetools/examples/scanexamples/tgeneric2.pas svneol=native#text/plain
components/codetools/examples/scanexamples/uglyifdefs.pas svneol=native#text/plain
components/codetools/examples/scanexamples/wrongforwarddefinitions.pas svneol=native#text/plain

View File

@ -0,0 +1,56 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<PathDelim Value="/"/>
<Version Value="6"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<IconPath Value="./"/>
<TargetFileExt Value=""/>
<Title Value="fixh2pasdirectives"/>
</General>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
<ExcludeFileFilter Value="*.(bak|ppu|ppw|o|so);*~;backup"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
<LaunchingApplication PathPlusParams="/usr/X11R6/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
</local>
</RunParams>
<RequiredPackages Count="2">
<Item1>
<PackageName Value="LCL"/>
</Item1>
<Item2>
<PackageName Value="CodeTools"/>
</Item2>
</RequiredPackages>
<Units Count="1">
<Unit0>
<Filename Value="h2pastest.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="H2PasTest"/>
</Unit0>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="5"/>
<SearchPaths>
<OtherUnitFiles Value="scanexamples/"/>
</SearchPaths>
<CodeGeneration>
<Generate Value="Faster"/>
</CodeGeneration>
<Other>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
</CONFIG>

View File

@ -0,0 +1,111 @@
(*
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
Demonstration of how to convert c header files to pascal interfaces.
Usage:
h2pastest [filename.h]
*)
program H2PasTest;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, CodeCache, CodeToolManager, FileProcs,
CodeTree, NonPascalCodeTools;
var
Filename: string;
Code: TCodeBuffer;
Tree: TCompilerDirectivesTree;
Changed: Boolean;
Pass: Integer;
i: Integer;
p: String;
Undefines: TStringList;
Defines: TStringList;
begin
Undefines:=nil;
Defines:=nil;
Filename:=SetDirSeparators('scanexamples/missingh2pasdirectives.pas');
// parse parameters
for i:=1 to ParamCount do begin
p:=ParamStr(i);
if p='' then continue;
if p[1]='-' then begin
if Undefines=nil then Undefines:=TStringList.Create;
Undefines.Add(copy(p,2,length(p)));
end
else if p[1]='+' then begin
if Defines=nil then Defines:=TStringList.Create;
Defines.Add(copy(p,2,length(p)));
end else
Filename:=p;
end;
if Undefines<>nil then begin
writeln('Undefines: ');
writeln(Undefines.Text);
end;
if Defines<>nil then begin
writeln('Defines: ');
writeln(Defines.Text);
end;
// load the file
Filename:=ExpandFileName(Filename);
Code:=CodeToolBoss.LoadFile(Filename,false,false);
if Code=nil then
raise Exception.Create('loading failed '+Filename);
// parse the directives
Tree:=TCompilerDirectivesTree.Create;
Tree.Parse(Code,CodeToolBoss.GetNestedCommentsFlagForFile(Code.Filename));
writeln('-----------------------------------');
writeln('h2pas created these directives:');
Tree.WriteDebugReport;
// add missing directives
Changed:=false;
Tree.FixMissingH2PasDirectives(Changed);
writeln('-----------------------------------');
writeln('after adding the missing directives:');
Tree.WriteDebugReport;
// reduce directives
Pass:=0;
repeat
inc(Pass);
Changed:=false;
Tree.ReduceCompilerDirectives(Undefines,Defines,Changed);
if not Changed then break;
writeln('-----------------------------------');
writeln('after reduce number ',Pass,':');
Tree.WriteDebugReport;
until false;
writeln('-----------------------------------');
writeln('Source:');
writeln(Code.Source);
end.

View File

@ -0,0 +1,144 @@
/*
Comment
*/
#ifndef __TEST_H
#define __TEST_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#ifndef SOME_FLAG1
#define SOME_FLAG1 31
#define SOME_FLAG2 SOME_FLAG1
#endif
#define constant 1
#define macro1 1
/* An anonymous enum */
enum {
TEST_ENUM1 = 1, /* Enum starts at 1 */
TEST_ENUM2,
TEST_ENUM3
};
/* Byte order conversions */
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define htobs(d) (d)
#define htobl(d) (d)
#define btohs(d) (d)
#define btohl(d) (d)
#elif __BYTE_ORDER == __BIG_ENDIAN
#define htobs(d) bswap_16(d)
#define htobl(d) bswap_32(d)
#define btohs(d) bswap_16(d)
#define btohl(d) bswap_32(d)
#else
#error "Unknown byte order"
#endif
/* complex macro function */
#define test_get_unaligned(ptr) \
({ \
struct __attribute__((packed)) { \
typeof(*(ptr)) __v; \
} *__p = (void *) (ptr); \
__p->__v; \
})
/* named struct with macro and implicit type */
typedef struct {
uint8_t b[6]; // implicit type
} __attribute__((packed)) bdaddr_t;
/* Copy, swap, convert BD Address */
static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
{
return memcmp(ba1, ba2, sizeof(bdaddr_t));
}
void baswap(bdaddr_t *dst, const bdaddr_t *src);
bdaddr_t *strtoba(const char *str);
int baprintf(const char *format, ...);
int bafprintf(FILE *stream, const char *format, ...);
int basprintf(char *str, const char *format, ...);
int basnprintf(char *str, size_t size, const char *format, ...);
void *bt_malloc(size_t size);
void bt_free(void *ptr);
#define HIDPCONNADD _IOW('H', 200, int)
struct hidp_connadd_req {
int ctrl_sock; /* Connected control socket */
int intr_sock; /* Connected interrupt socket */
uint16_t parser; /* Parser version */
uint16_t rd_size; /* Report descriptor size */
uint8_t *rd_data; /* Report descriptor data */
uint8_t country;
uint8_t subclass;
uint16_t vendor;
uint16_t product;
uint16_t version;
uint32_t flags;
uint32_t idle_to;
char name[128]; /* Device name */
};
struct hidp_connlist_req {
uint32_t cnum;
struct hidp_conninfo *ci;
};
#define SDP_UNIX_PATH "/var/run/sdp"
#define SDP_PSM 0x0001
#define SDP_PRIMARY_LANG_BASE 0x0100
#define SDP_ATTR_SVCNAME_PRIMARY 0x0000 + SDP_PRIMARY_LANG_BASE
typedef struct {
uint8_t type;
union {
uint16_t uuid16;
uint32_t uuid32;
uint128_t uuid128;
} value;
} uuid_t;
#define SDP_IS_UUID(x) ((x) == SDP_UUID16 || (x) == SDP_UUID32 || (x) ==SDP_UUID128)
typedef struct _sdp_list sdp_list_t;
struct _sdp_list {
sdp_list_t *next;
void *data;
};
complex operator+(complex, complex);
int y = 7;
float f(int){};
bool b1 = a==b;
char c = 'a';
short signed int ssi_octal = 0123;
long unsigned int lui = sizeof(char);
enum e1{dark, light};
enum e2{a=3, b=9};
int *pi; // pointer to int
char ** ppc; // pointer to pointer to char
int* ap[15]; // array of 15 pointers to ints
int (*fp)(char*); // pointer to function taking a char* argument; returns an int
int * f(char*); // fucntion taking a char* argument; returns a pointer to int
#define MACRO_CONCATENATION(a,b) a##b
#ifdef __cplusplus
}
#endif
#endif