Menu

[4a4584]: / Source / EmmaMetaData.pas  Maximize  Restore  History

Download this file

537 lines (458 with data), 15.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
(***********************************************************************)
(* Delphi Code Coverage *)
(* *)
(* A quick hack of a Code Coverage Tool for Delphi *)
(* by Christer Fahlgren and Nick Ring *)
(* *)
(* This Source Code Form is subject to the terms of the Mozilla Public *)
(* License, v. 2.0. If a copy of the MPL was not distributed with this *)
(* file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
unit EmmaMetaData;
interface
uses
System.Types,
System.Classes,
System.Generics.Collections,
EmmaMergable,
EmmaFileHelper;
const
EmmaVersion20 = $20;
EmmaVersion21 = $21;
type
TIntegerDynArrays = array of TIntegerDynArray;
TMethodDescriptor = class
strict private
FName: string;
FDescriptor: string;
FStatus: Integer;
FBlockMap: TIntegerDynArrays;
FBlockSizes: TIntegerDynArray;
FFirstLine: Integer;
function GetEntryLength: Int64;
function FindFirstLine: Integer;
public
property Name: string read FName write FName;
property Descriptor: string read FDescriptor write FDescriptor;
property Status: Integer read FStatus write FStatus;
property BlockMap: TIntegerDynArrays read FBlockMap write FBlockMap;
property BlockSizes: TIntegerDynArray read FBlockSizes write FBlockSizes;
property FirstLine: Integer read FFirstLine write FFirstLine;
property EntryLength: Int64 read GetEntryLength;
destructor Destroy; override;
function ToString: string; override;
procedure LoadFromFile(const DataInput: IEmmaDataInput);
procedure WriteToFile(DataOutput: IEmmaDataOutput);
procedure SetBlockSizesLength(const ALength: Integer);
procedure SetBlockMapLength(const ALength: Integer);
end;
TClassDescriptor = class
strict private
FClassVMName: string;
FPackageVMName: string;
FName: string;
FStamp: Int64;
FSrcFileNameFlag: Byte;
FSrcFileName: string;
FMethods: TList<TMethodDescriptor>;
function GetEntryLength: Int64;
public
property ClassVMName: string read FClassVMName;
property PackageVMName: string read FPackageVMName;
property Name: string read FName;
property Stamp: Int64 read FStamp;
property SrcFileNameFlag: Byte read FSrcFileNameFlag;
property SrcFileName: string read FSrcFileName;
property EntryLength: Int64 read GetEntryLength;
constructor Create(
const AName: string;
const ASrcFileNameFlag: Integer;
const ASrcFileName: string;
const AClassVMName: string;
const APackageVMName: string);
constructor CreateFromFile(const DataInput: IEmmaDataInput);
destructor Destroy; override;
function ToString: string; override;
procedure LoadFromFile(const DataInput: IEmmaDataInput);
procedure WriteToFile(DataOutput: IEmmaDataOutput);
procedure Add(const AMethodDescriptor: TMethodDescriptor);
end;
TCoverageOptions = class
strict private
// v2.1
FExcludeEmptyClasses: Boolean;
// v2.0
FExcludeSyntheticMethods: Boolean;
FExcludeBridgeMethods: Boolean;
FDoSUIDCompensation: Boolean;
FVersion: Int64;
function GetEntryLength: Int64;
public
property ExcludeEmptyClasses: Boolean read FExcludeEmptyClasses write FExcludeEmptyClasses;
property ExcludeSyntheticMethods: Boolean read FExcludeSyntheticMethods write FExcludeSyntheticMethods;
property ExcludeBridgeMethods: Boolean read FExcludeBridgeMethods write FExcludeBridgeMethods;
property DoSUIDCompensation: Boolean read FDoSUIDCompensation write FDoSUIDCompensation;
property EntryLength: Int64 read GetEntryLength;
constructor Create(const AVersion: Int64);
procedure LoadFromFile(const DataInput: IEmmaDataInput);
procedure WriteToFile(DataOutput: IEmmaDataOutput);
end;
TEmmaMetaData = class(TMergable)
strict private
FCoverageOptions: TCoverageOptions;
FHasSourceFileInfo: Boolean;
FHasLineNumberInfo: Boolean;
FClassList: TList<TClassDescriptor>;
protected
function GetEntryLength: Int64; override;
function GetEntryType: Byte; override;
public
property CoverageOptions: TCoverageOptions read FCoverageOptions;
property HasSourceFileInfo: Boolean read FHasSourceFileInfo write FHasSourceFileInfo;
property HasLineNumberInfo: Boolean read FHasLineNumberInfo write FHasLineNumberInfo;
property ClassList: TList<TClassDescriptor> read FClassList;
constructor Create(const AVersion: Int64);
destructor Destroy; override;
function ToString: string; override;
procedure LoadFromFile(const DataInput: IEmmaDataInput); override;
procedure WriteToFile(DataOutput: IEmmaDataOutput); override;
procedure Add(const AClassDescriptor: TClassDescriptor);
end;
const
METHOD_NO_LINE_NUMBER_TABLE = $01;
METHOD_ABSTRACT_OR_NATIVE = $02;
METHOD_EXCLUDED = $04;
METHOD_ADDED = $08;
METHOD_NO_BLOCK_DATA = (
METHOD_ABSTRACT_OR_NATIVE
or METHOD_EXCLUDED
or METHOD_ADDED
);
METHOD_NO_LINE_DATA = (
METHOD_NO_LINE_NUMBER_TABLE
or METHOD_NO_BLOCK_DATA
);
implementation
uses
Math,
SysUtils,
StrUtils;
{$region 'TMethodDescriptor'}
destructor TMethodDescriptor.Destroy;
var
i: Integer;
begin
for i := 0 to Length(FBlockMap) - 1 do
SetLength(FBlockMap[i], 0);
SetLength(FBlockMap, 0);
SetLength(FBlockSizes, 0);
inherited Destroy;
end;
function TMethodDescriptor.GetEntryLength: Int64;
var
i: Integer;
begin
Result := 0;
Result := Result + EmmaFileHelper.GetUtf8Length(FName);
Result := Result + EmmaFileHelper.GetUtf8Length(FDescriptor);
Result := Result + SizeOf(FStatus);
if (FStatus and METHOD_NO_BLOCK_DATA) = 0 then
begin
Result := Result + EmmaFileHelper.GetEntryLength(FBlockSizes);
if (FStatus and METHOD_NO_LINE_DATA) = 0 then
begin
Result := Result + SizeOf(Integer);
for i := 0 to High(FBlockSizes) do
Result := Result + EmmaFileHelper.GetEntryLength(FBlockMap[i]);
Result := Result + SizeOf(FFirstLine);
end;
end;
end;
function TMethodDescriptor.ToString: string;
var
i: Integer;
j: Integer;
begin
Result := ' MD[';
Result := Result + ' name=' + FName;
Result := Result + ' status=' + IntToStr(FStatus);
Result := Result + ' firstline=' + IntToStr(FFirstLine);
for i := 0 to Length(FBlockMap) - 1 do
begin
Result := Result + ' Block[ no=' + IntToStr(i);
Result := Result + ' Lines[';
for j := 0 to Length(FBlockMap[i]) - 1 do
begin
Result := Result + IntToStr(FBlockMap[i][j]);
if j <> Length(FBlockMap[i]) - 1 then
Result := Result + ',';
end;
Result := Result + ']';
end;
Result := Result + ']';
end;
procedure TMethodDescriptor.LoadFromFile(const DataInput: IEmmaDataInput);
var
i: Integer;
BlockMapLength: Integer;
begin
FName := DataInput.ReadUTF;
FDescriptor := DataInput.ReadUTF;
FStatus := DataInput.ReadInteger;
FFirstLine := 0;
if (FStatus and METHOD_NO_BLOCK_DATA) = 0 then
begin
DataInput.ReadIntArray(FBlockSizes);
if (FStatus and METHOD_NO_LINE_DATA) = 0 then
begin
BlockMapLength := DataInput.ReadInteger;
SetLength(FBlockMap, BlockMapLength);
for i := 0 to BlockMapLength - 1 do
DataInput.ReadIntArray(FBlockMap[i]);
FFirstLine := DataInput.ReadInteger;
end;
end;
end;
procedure TMethodDescriptor.SetBlockMapLength(const ALength: Integer);
begin
SetLength(FBlockMap, ALength);
end;
procedure TMethodDescriptor.SetBlockSizesLength(const ALength: Integer);
begin
SetLength(FBlockSizes, ALength);
end;
function TMethodDescriptor.FindFirstLine: Integer;
var
MinValues: TIntegerDynArray;
I: Integer;
begin
SetLength(MinValues, Length(FBlockMap));
for I := Low(FBlockMap) to High(FBlockMap) do
MinValues[i] := MinIntValue(FBlockMap[i]);
Result := MinIntValue(MinValues);
end;
procedure TMethodDescriptor.WriteToFile(DataOutput: IEmmaDataOutput);
var
i: Integer;
begin
DataOutput.WriteUTF(FName);
DataOutput.WriteUTF(FDescriptor);
DataOutput.WriteInteger(FStatus);
if (FStatus and METHOD_NO_BLOCK_DATA) = 0 then
begin
DataOutput.WriteIntArray(FBlockSizes);
if (FStatus and METHOD_NO_LINE_DATA) = 0 then
begin
DataOutput.WriteInteger(Length(FBlockMap));
for i := 0 to High(FBlockMap) do
DataOutput.WriteIntArray(FBlockMap[i]);
FFirstLine := FindFirstLine;
DataOutput.WriteInteger(FFirstLine);
end;
end;
end;
{$endregion 'TMethodDescriptor'}
{$region 'TClassDescriptor'}
constructor TClassDescriptor.Create(
const AName: string;
const ASrcFileNameFlag: Integer;
const ASrcFileName: string;
const AClassVMName: string;
const APackageVMName: string);
begin
inherited Create;
FMethods := TList<TMethodDescriptor>.Create;
FName := AName;
FSrcFileNameFlag := ASrcFileNameFlag;
if FSrcFileNameFlag <> 0 then
FSrcFileName := ASrcFileName
else
FSrcFileName := '';
FClassVMName := AClassVMName;
FPackageVMName := APackageVMName;
end;
constructor TClassDescriptor.CreateFromFile(const DataInput: IEmmaDataInput);
begin
inherited Create;
FMethods := TList<TMethodDescriptor>.Create;
LoadFromFile(DataInput);
end;
destructor TClassDescriptor.Destroy;
var
CurrentMethod: TMethodDescriptor;
begin
for CurrentMethod in FMethods do
CurrentMethod.Free;
FMethods.Free;
inherited Destroy;
end;
procedure TClassDescriptor.LoadFromFile(const DataInput: IEmmaDataInput);
var
MethodIndex: Integer;
MethodDescriptor: TMethodDescriptor;
MethodCount: Integer;
begin
FClassVMName := DataInput.ReadUTF;
FPackageVMName := DataInput.ReadUTF;
FName := DataInput.ReadUTF;
FStamp := DataInput.ReadInt64;
FSrcFileNameFlag := DataInput.ReadByte;
if FSrcFileNameFlag <> 0 then
FSrcFileName := DataInput.ReadUTF
else
FSrcFileName := '';
MethodCount := DataInput.ReadInteger;
for MethodIndex := 0 to MethodCount - 1 do
begin
MethodDescriptor := TMethodDescriptor.Create;
MethodDescriptor.LoadFromFile(DataInput);
FMethods.Add(MethodDescriptor);
end;
end;
procedure TClassDescriptor.WriteToFile(DataOutput: IEmmaDataOutput);
var
MethodDescriptor: TMethodDescriptor;
begin
DataOutput.WriteUTF(FClassVMName);
DataOutput.WriteUTF(FPackageVMName);
DataOutput.WriteUTF(FName);
DataOutput.WriteInt64(FStamp);
DataOutput.WriteByte(FSrcFileNameFlag);
if FSrcFileNameFlag <> 0 then
DataOutput.WriteUTF(FSrcFileName);
DataOutput.WriteInteger(FMethods.Count);
for MethodDescriptor in FMethods do
MethodDescriptor.WriteToFile(DataOutput);
end;
function TClassDescriptor.ToString: string;
var
MethodDescriptor: TMethodDescriptor;
begin
Result := 'CD [ name=' + FName;
Result := Result + ' stamp=' + IntToStr(FStamp);
for MethodDescriptor in FMethods do
Result := Result + MethodDescriptor.ToString;
Result := Result + ']';
end;
function TClassDescriptor.GetEntryLength: Int64;
var
MethodDescriptor: TMethodDescriptor;
begin
Result := 0;
Result := Result + EmmaFileHelper.GetUtf8Length(FClassVMName);
Result := Result + EmmaFileHelper.GetUtf8Length(FPackageVMName);
Result := Result + EmmaFileHelper.GetUtf8Length(FName);
Result := Result + SizeOf(Int64);
Result := Result + SizeOf(Byte);
if (FSrcFileNameFlag <> 0) then
Result := Result + EmmaFileHelper.GetUtf8Length(FSrcFileName);
Result := Result + SizeOf(Integer);
for MethodDescriptor in FMethods do
Result := Result + MethodDescriptor.EntryLength;
end;
procedure TClassDescriptor.Add(const AMethodDescriptor: TMethodDescriptor);
begin
FMethods.Add(AMethodDescriptor);
end;
{$endregion 'TClassDescriptor'}
{$region 'TCoverageOptions'}
function TCoverageOptions.GetEntryLength: Int64;
begin
if FVersion = EmmaVersion21 then
Result := SizeOf(Boolean) * 4
else
Result := SizeOf(Boolean) * 3;
end;
constructor TCoverageOptions.Create(const AVersion: Int64);
begin
FVersion := AVersion;
FExcludeEmptyClasses := False;
end;
procedure TCoverageOptions.LoadFromFile(const DataInput: IEmmaDataInput);
begin
if FVersion = EmmaVersion21 then
FExcludeEmptyClasses := DataInput.ReadBoolean;
FExcludeSyntheticMethods := DataInput.ReadBoolean;
FExcludeBridgeMethods := DataInput.ReadBoolean;
FDoSUIDCompensation := DataInput.ReadBoolean;
end;
procedure TCoverageOptions.WriteToFile(DataOutput: IEmmaDataOutput);
begin
if FVersion = EmmaVersion21 then
DataOutput.WriteBoolean(FExcludeEmptyClasses);
DataOutput.WriteBoolean(FExcludeSyntheticMethods);
DataOutput.WriteBoolean(FExcludeBridgeMethods);
DataOutput.WriteBoolean(FDoSUIDCompensation);
end;
{$endregion 'TCoverageOptions'}
{$region 'TEmmaMetaData'}
constructor TEmmaMetaData.Create(const AVersion: Int64);
begin
inherited Create;
FClassList := TList<TClassDescriptor>.Create;
FCoverageOptions := TCoverageOptions.Create(AVersion);
end;
destructor TEmmaMetaData.Destroy;
var
CurrentClass: TClassDescriptor;
begin
for CurrentClass in FClassList do
CurrentClass.Free;
FClassList.Destroy;
FCoverageOptions.Free;
inherited Destroy;
end;
procedure TEmmaMetaData.LoadFromFile(const DataInput: IEmmaDataInput);
var
ClassDescriptorIndex: Integer;
ClassDescriptorCount: Integer;
begin
FCoverageOptions.LoadFromFile(DataInput);
FHasSourceFileInfo := DataInput.ReadBoolean;
FHasLineNumberInfo := DataInput.ReadBoolean;
ClassDescriptorCount := DataInput.ReadInteger;
for ClassDescriptorIndex := 0 to ClassDescriptorCount - 1 do
FClassList.Add(TClassDescriptor.CreateFromFile(DataInput));
end;
function TEmmaMetaData.ToString: string;
var
ClassDescriptor: TClassDescriptor;
begin
for ClassDescriptor in FClassList do
begin
Result := Result + ' Class:' + ClassDescriptor.Name;
Result := Result + ClassDescriptor.ToString;
end;
end;
function TEmmaMetaData.GetEntryLength: Int64;
var
ClassDescriptor: TClassDescriptor;
begin
Result := 0;
Result := Result + FCoverageOptions.EntryLength;
Result := Result + SizeOf(Boolean);
Result := Result + SizeOf(Boolean);
Result := Result + SizeOf(Integer);
for ClassDescriptor in FClassList do
Result := Result + ClassDescriptor.EntryLength;
end;
function TEmmaMetaData.GetEntryType: Byte;
begin
Result := 0;
end;
procedure TEmmaMetaData.WriteToFile(DataOutput: IEmmaDataOutput);
var
ClassDescriptor: TClassDescriptor;
begin
FCoverageOptions.WriteToFile(DataOutput);
DataOutput.WriteBoolean(FHasSourceFileInfo);
DataOutput.WriteBoolean(FHasLineNumberInfo);
DataOutput.WriteInteger(FClassList.Count);
for ClassDescriptor in FClassList do
ClassDescriptor.WriteToFile(DataOutput);
end;
procedure TEmmaMetaData.Add(const AClassDescriptor: TClassDescriptor);
begin
FClassList.Add(AClassDescriptor);
end;
{$endregion 'TEmmaMetaData'}
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.