Skip to content

fixes #20 #22

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Bin/Chet.exe
Binary file not shown.
859 changes: 4 additions & 855 deletions Chet.dproj

Large diffs are not rendered by default.

Binary file modified Chet.res
Binary file not shown.
74 changes: 65 additions & 9 deletions Classes/Chet.CommentWriter.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface

uses
System.Classes,
System.SysUtils,
System.SysUtils,System.StrUtils,System.Character,
{$IFDEF DEBUG}
System.Generics.Collections,
{$ENDIF}
Expand Down Expand Up @@ -105,6 +105,7 @@ TParsedCommentWriter = class abstract(TCommentWriter)
constructor Create(const ASourceWriter: TSourceWriter);
destructor Destroy; override;
procedure WriteComment(const ACursor: TCursor); override;
class function DropExtraWhitespaces(const AText: string): string; inline;
end;

type
Expand Down Expand Up @@ -253,10 +254,9 @@ procedure TParsedCommentWriter.Append(const AText: String);
var
S: String;
begin
if (AText = '') then
Exit;
S := DropExtraWhitespaces(AText.Trim.Replace('*)', '* )'));
if S.IsEmpty then Exit;

S := AText.Replace('*)', '* )');
if (FLastLineEmpty) then
FBuilder.Append(FPrefix).Append(S.TrimLeft)
else if (FNeedToTrimLeft) then
Expand Down Expand Up @@ -319,6 +319,29 @@ destructor TParsedCommentWriter.Destroy;
inherited;
end;

class function TParsedCommentWriter.DropExtraWhitespaces(const AText: string): string;
var
I,J,J1: Integer;
begin
SetLength(Result, Length(AText));
J := 0;
J1 := 0;
for I := 1 to Length(AText) do
begin
if AText[I].IsWhiteSpace then
Inc(J1)
else
J1 := 0;

if J1 < 2 then
Result[I-J] := AText[I]
else
Inc(J);
end;
if J > 0 then
SetLength(Result,Length(AText) - J);
end;

procedure TParsedCommentWriter.EndSummaryOrRemarks;
begin
case FState of
Expand Down Expand Up @@ -439,6 +462,11 @@ procedure TParsedCommentWriter.WriteNode(const AComment, APrevSibling: TComment;
end;
end;

function SkipTag(const ATagName: string): Boolean;
begin
Result := IndexText(ATagName,['li','ul','table']) > -1;
end;

var
Kind: TCommentKind;
S, Args: String;
Expand Down Expand Up @@ -469,10 +497,34 @@ procedure TParsedCommentWriter.WriteNode(const AComment, APrevSibling: TComment;
end;

TCommentKind.HtmlStartTag:
Assert(False);
begin
S := AComment.HtmlTagName;
{$IFDEF DEBUG}
OutputDebugString(PChar(S));
{$ENDIF}
if not SkipTag(S) then begin
if not AComment.HtmlTagIsSelfClosing then
Append(Format('<%s>',[S]))
else
Append(Format('<%s ',[S]))
end;
// do nothing;
end;

TCommentKind.HtmlEndTag:
Assert(False);
begin
S := AComment.HtmlTagName;
{$IFDEF DEBUG}
OutputDebugString(PChar(S));
{$ENDIF}
if not SkipTag(S) then begin
if not AComment.HtmlTagIsSelfClosing then
Append(Format('</%s> ',[AComment.HtmlTagName]))
else
Append('/> ');
end;
// do nothing;
end;

TCommentKind.Paragraph,
TCommentKind.VerbatimLine:
Expand Down Expand Up @@ -502,7 +554,7 @@ procedure TParsedCommentWriter.WriteNode(const AComment, APrevSibling: TComment;
AppendLine;
end;
end;
if (Kind = TCommentKind.VerbatimLine) then
if (Kind = TCommentKind.VerbatimLine) and (AComment.ChildCount > 0) then
Append(AComment.VerbatimLineText)
else
WriteChildren(AComment);
Expand Down Expand Up @@ -602,8 +654,12 @@ procedure TParsedCommentWriter.WriteNode(const AComment, APrevSibling: TComment;
AppendLine;
end;
else
S := AComment.Text.Replace('//', '', []);
Append(S);
S := AComment.Text.Replace('//', '', []).Replace('* ','',[rfReplaceAll]);
if S = '*' then
AppendLine
else
Append(S);

if (AComment.HasTrailingNewline) then
AppendLine;
end;
Expand Down
90 changes: 69 additions & 21 deletions Classes/Chet.HeaderTranslator.pas
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ constructor THeaderTranslator.Create(const AProject: TProject);
FSymbolPrefix := '_';
{$ENDIF}
FCombinedHeaderFilename := TPath.Combine(TPath.GetTempPath, '_chet_.h');
FIndex := TIndex.Create(False, False);
FIndex := TIndex.Create(False, FProject.ShowParserWarnings);
FTypes := TList<TCursor>.Create;
FDeclaredTypes := TList<TCursor>.Create;
FVisitedTypes := TDictionary<TCursor, Integer>.Create(
Expand Down Expand Up @@ -707,7 +707,7 @@ function THeaderTranslator.MakePointerType(const ATypeName: String;

function THeaderTranslator.ParseCombinedHeaderFile: Boolean;
var
Args: TArray<String>;
Args,WinSdkIncludePaths : TArray<String>;
Options: TTranslationUnitFlags;
DiagOpts: TDiagnosticDisplayOptions;
Diag: IDiagnostic;
Expand Down Expand Up @@ -739,9 +739,18 @@ function THeaderTranslator.ParseCombinedHeaderFile: Boolean;
end;

Args := Args + ['-I' + FProject.HeaderFileDirectory];
WinSdkIncludePaths := FProject.WinSDKIncludePaths;

for I := 0 to High(WinSdkIncludePaths) do
Args := Args + ['-I'+WinSdkIncludePaths[I]];

FTranslationUnit := FIndex.ParseTranslationUnit(
FCombinedHeaderFilename,
Args,
[],
Options
);

FTranslationUnit := FIndex.ParseTranslationUnit(FCombinedHeaderFilename,
Args, [], Options);
if (FTranslationUnit = nil) then
raise EHeaderTranslatorError.Create('Cannot parse header files.');

Expand All @@ -754,7 +763,10 @@ function THeaderTranslator.ParseCombinedHeaderFile: Boolean;
begin
DoMessage(Diag.Format(DiagOpts));
Inc(ErrorCount);
end;
end
else
if FProject.ShowParserWarnings and (Diag.Severity = TDiagnosticSeverity.Warning) then
DoMessage(Diag.Format(DiagOpts));
end;

if (ErrorCount = 0) then
Expand All @@ -771,12 +783,12 @@ function THeaderTranslator.ParseCombinedHeaderFile: Boolean;

function THeaderTranslator.RemoveQualifiers(const ACTypeName: String): String;
var
HasPrefix: Boolean;
HasPrefix,StartsWithUnderscore: Boolean;
begin
Result := ACTypeName;

{$IFDEF EXPERIMENTAL}
var StartsWithUnderscore := False;
StartsWithUnderscore := False;
if (FProject.PrefixSymbolsWithUnderscore) then
begin
StartsWithUnderscore := Result.StartsWith('_');
Expand Down Expand Up @@ -904,7 +916,7 @@ procedure THeaderTranslator.SetupBuiltinTypes;
FBuiltinTypes[TTypeKind.Char32] := 'UCS4Char';
FBuiltinTypes[TTypeKind.UShort] := 'Word';
FBuiltinTypes[TTypeKind.UInt] := 'Cardinal';
FBuiltinTypes[TTypeKind.ULong] := 'Cardinal';
FBuiltinTypes[TTypeKind.ULong] := 'Longword';
FBuiltinTypes[TTypeKind.ULongLong] := 'UInt64';

case FProject.CharConvert of
Expand All @@ -926,7 +938,7 @@ procedure THeaderTranslator.SetupBuiltinTypes;
FBuiltinTypes[TTypeKind.WChar] := 'WideChar';
FBuiltinTypes[TTypeKind.Short] := 'Smallint';
FBuiltinTypes[TTypeKind.Int] := 'Integer';
FBuiltinTypes[TTypeKind.Long] := 'Integer';
FBuiltinTypes[TTypeKind.Long] := 'Longint';
FBuiltinTypes[TTypeKind.LongLong] := 'Int64';
FBuiltinTypes[TTypeKind.Float] := 'Single';
FBuiltinTypes[TTypeKind.Double] := 'Double';
Expand Down Expand Up @@ -1343,12 +1355,10 @@ procedure THeaderTranslator.WriteConstantsRhs(Tokens: TArray<String>; StartIndex
StringConcatPlusInserted : Boolean;
begin
StringConcatPlusInserted := False;

for I := StartIndex to Count - 1 do
begin
S := Tokens[I];
IsString := False;

{ Issue #4 (https://github.com/neslib/Chet/issues/4)
Convert wide character string constant (L"...").
These are the supported prefixes:
Expand Down Expand Up @@ -1779,13 +1789,18 @@ procedure THeaderTranslator.WriteEnumTypeConst(const ACursor: TCursor);
IsUnsigned := True;
end;

TTypeKind.UInt,
TTypeKind.ULong:
TTypeKind.UInt:
begin
FWriter.WriteLn('Cardinal;');
IsUnsigned := True;
end;

TTypeKind.ULong:
begin
FWriter.WriteLn('Longword;');
IsUnsigned := True;
end;

TTypeKind.ULongLong:
begin
FWriter.WriteLn('UInt64;');
Expand All @@ -1799,10 +1814,12 @@ procedure THeaderTranslator.WriteEnumTypeConst(const ACursor: TCursor);
TTypeKind.Short:
FWriter.WriteLn('Smallint;');

TTypeKind.Int,
TTypeKind.Long:
TTypeKind.Int:
FWriter.WriteLn('Integer;');

TTypeKind.Long:
FWriter.WriteLn('Longint;');

TTypeKind.LongLong:
FWriter.WriteLn('Int64;');
else
Expand Down Expand Up @@ -1982,8 +1999,10 @@ procedure THeaderTranslator.WriteForwardTypeDeclarations;

CheckIndirection(['short', 'short int'], 'Smallint');
CheckIndirection(['unsigned short int'], 'Word');
CheckIndirection(['long', 'int', 'long int'], 'Integer');
CheckIndirection(['unsigned int', 'unsigned long int'], 'Cardinal');
CheckIndirection(['int'], 'Integer');
CheckIndirection(['long', 'long int'], 'Longint');
CheckIndirection(['unsigned int'], 'Cardinal');
CheckIndirection(['unsigned long', 'unsigned long int'], 'Longword');
CheckIndirection(['long long int'], 'Int64');
CheckIndirection(['unsigned long long int'], 'UInt64');
CheckIndirection(['float'], 'Single');
Expand Down Expand Up @@ -2115,6 +2134,9 @@ procedure THeaderTranslator.WriteFunctionProto(const ACursor: TCursor;
ResType, ProtoType: TType;
ArgIndex, ArgCount: Integer;
HasResult: Boolean;
{$IFnDEF OldHandleCallConv}
CallConv: string;
{$ENDIF}
begin
{ AType is the function proto type (of kind FunctionProto or FunctionNoProto).
Use its ResultType and ArgTypes properties for parameter type information.
Expand Down Expand Up @@ -2204,15 +2226,41 @@ procedure THeaderTranslator.WriteFunctionProto(const ACursor: TCursor;
else
FWriter.Write(')');

{$IFnDEF OldHandleCallConv}
CallConv := ';';
// https://clang.llvm.org/docs/AttributeReference.html#calling-conventions
// this needs explicitly specify the target platform by specifying the command line options
// --target=i686-pc-win32 --target=i686-pc-windows-msvc
case AType.FunctionCallingConv of

TCallingConv.C: CallConv := CallConv + ' cdecl';
TCallingConv.X86StdCall,
TCallingConv.Win64{X86_64Win64}: CallConv := CallConv + ' stdcall';
TCallingConv.X86FastCall: CallConv := CallConv + ' fastcall';
TCallingConv.X86Pascal: CallConv := CallConv + ' pascal';
TCallingConv.X86RegCall: CallConv := CallConv + ' register';
else
begin
if FProject.CallConv = TCallConv.StdCall then
CallConv := CallConv + ' stdcall'
else
CallConv := CallConv + ' cdecl';
if AType.FunctionCallingConv <> TCallingConv.Default then
CallConv := CallConv + ' { TODO -cFIXME: Calling conversion may be wrong! }';
end;
end;
if (AType.Kind = TTypeKind.FunctionProto) and (AType.IsFunctionVariadic) then
FWriter.Write(' varargs');
CallConv := CallConv + ' varargs';

FWriter.Write(';');

if (FProject.CallConv = TCallConv.StdCall) then
FWriter.Write(CallConv);
{$ELSE}
if FProject.CallConv = TCallConv.StdCall then
FWriter.Write(' stdcall')
else
FWriter.Write(' cdecl');
if (AType.Kind = TTypeKind.FunctionProto) and (AType.IsFunctionVariadic) then
FWriter.Write(' varargs');
{$ENDIF}
end;

procedure THeaderTranslator.WriteFunctions;
Expand Down
Loading