Menu

[r1837]: / trunk / Src / UWarnings.pas  Maximize  Restore  History

Download this file

627 lines (577 with data), 20.1 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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
{
* UWarnings.pas
*
* Classes and interfaces that encapsulate Delphi $WARN directives used to
* switch off unwanted compiler warnings.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UWarnings.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2010-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UWarnings;
interface
uses
// Delphi
Generics.Collections,
// Project
IntfCommon, UBaseObjects, USettings;
type
{
TWarning:
Record that encapsulates information needed to generate a $WARN compiler
directive.
}
TWarning = record
strict private
var fSymbol: string; // Value of Symbol property
var fMinCompiler: Single; // Value of MinCompiler property
function GetMinCompiler: Single;
{Read accessor for MinCompiler property.
@return Version number of earliest compiler to support Symbol.
}
procedure SetMinCompiler(const Value: Single);
{Write accessor for MinCompiler property.
@param Value [in] New compiler version to be assigned to property.
}
function GetSymbol: string;
{Read accessor for Symbol property.
@return Symbol used in $WARN directive.
}
public
const MinSupportedCompiler = 14.0;
{Version number of earliest compiler that supports any $WARN directive.
(Delphi 6)}
constructor Create(const ASymbol: string; const AMinCompiler: Single);
overload;
{Constructor that provides values for both Symbol and MinCompiler
properties.
@param ASymbol [in] Warning symbol.
@param AMinCompiler [in] Earliest compiler that supports ASymbol.
}
constructor Create(const ASymbol: string); overload;
{Constructor that provides a value for Symbol property and uses default
value for MinCompiler property.
@param ASymbol [in] Warning symbol.
}
function IsValid: Boolean;
{Checks if properties of this warning are valid.
@return True if warning is valid, False if not.
}
property Symbol: string read GetSymbol;
{Symbol used to specify this warning in $WARN compiler directives}
property MinCompiler: Single read GetMinCompiler write SetMinCompiler;
{Version number of earliest compiler that supports this warning}
end;
{
IWarnings:
Interface to class that encapsulates information about warnings and whether
code can be generated to supress them.
}
IWarnings = interface(IInterface)
['{EBE8C8BD-535D-4B4B-A6D4-1AFC02E1C5B7}']
procedure Add(const AWarning: TWarning);
{Adds a warning to list.
@param AWarning [in] Warning to be added. Must be unique.
}
procedure Clear;
{Clears list of warnings.
}
function Count: Integer;
{Gets number of warnings in list.
@return Number of warnings.
}
function IsEmpty: Boolean;
{Checks whether warnings list is empty.
@return True if warnings list empty, False if not.
}
function Contains(const ASymbol: string): Boolean;
{Checks if a warning with a specified symbol is present in the warnings
list.
@param ASymbol [in] Symbol to be checked.
@return True if warning with symbol is in list, False if not.
}
procedure Delete(const ASymbol: string); overload;
{Deletes warning containing specified symbol from list.
@param ASymbol [in] Symbol of warning to be deleted.
}
function Render: string;
{Generates source code for compiler directives that switch off all
warnings in list for supporting compilers.
@return String containing required source code.
}
function GetItem(const Idx: Integer): TWarning;
{Read accessor for Items[] property. Gets warning at specified index in
warnings list.
@param Idx [in] Index of required warning.
@return Warning at specified index.
}
property Items[const Idx: Integer]: TWarning read GetItem; default;
{Array of warnings}
function GetSwitchOff: Boolean;
{Read accessor for SwitchOff property.
@return Value of SwitchOff property.
}
procedure SetSwitchOff(const Value: Boolean);
{Write accessor for SwitchOff property. Sets property to specified value.
@param Value [in] New property value.
}
property SwitchOff: Boolean read GetSwitchOff write SetSwitchOff;
{Property that indicates whether code should be emitted to switch off
listed warnings}
function GetEnumerator: TEnumerator<TWarning>;
{Creates an enumerator for all the warnings in the warnings list.
@return Instance of enumerator.
}
end;
{
TWarningsPersist:
Static class that can save and load an IWarnings object to and from
persistent storage.
}
TWarningsPersist = class(TNoConstructObject)
strict private
class function WarningCompoundName(const Idx: Integer; const Prop: string):
string;
{Constructs the name of a warning value in storage. Name comprises an
index number and a property name.
@param Idx [in] Index component of name.
@param Prop [in] Property component of name.
}
public
class procedure Load(Storage: ISettingsSection; Warnings: IWarnings);
{Loads data from persistent storage into a warnings object.
@param Storage [in] Reference to storage containing needed data.
@param Warnings [in] Warnings object to be updated with data from
storage.
}
class procedure Save(Storage: ISettingsSection; Warnings: IWarnings);
{Saves data from a warnings object to persistent storage.
@param Storage [in] Reference to storage object to receive data.
@param Warnings [in] Reference to warnings object to be persisted.
}
end;
{
TWarnings:
Class that encapsulates information about warnings and whether code can be
generated to supress them. Implements IWarnings interface.
}
TWarnings = class(TInterfacedObject, IWarnings, IAssignable)
strict private
fItems: TList<TWarning>; // List of warning records
fSwitchOff: Boolean; // Value of SwitchOff property
procedure Delete(const AWarning: TWarning); overload;
{Removes a warning from the list based on its symbol.
@param AWarning [in] Warning to be removed.
}
public
constructor Create;
{Constructor. Sets up object.
}
destructor Destroy; override;
{Destructor. Tears down object.
}
{ IWarnings methods }
procedure Add(const AWarning: TWarning);
{Adds a warning to list.
@param AWarning [in] Warning to be added. Must be unique.
}
procedure Clear;
{Clears list of warnings.
}
function Count: Integer;
{Gets number of warnings in list.
@return Number of warnings.
}
function IsEmpty: Boolean;
{Checks whether warnings list is empty.
@return True if warnings list empty, False if not.
}
function Contains(const ASymbol: string): Boolean;
{Checks if a warning with a specified symbol is present in the warnings
list.
@param ASymbol [in] Symbol to be checked.
@return True if warning with symbol is in list, False if not.
}
procedure Delete(const ASymbol: string); overload;
{Deletes warning containing specified symbol from list.
@param ASymbol [in] Symbol of warning to be deleted.
}
function Render: string;
{Generates source code for compiler directives that switch off all
warnings in list for supporting compilers.
@return String containing required source code.
}
function GetItem(const Idx: Integer): TWarning;
{Read accessor for Items[] property. Gets warning at specified index in
warnings list.
@param Idx [in] Index of required warning.
@return Warning at specified index.
}
property Items[const Idx: Integer]: TWarning read GetItem; default;
{Array of warnings}
function GetSwitchOff: Boolean;
{Read accessor for SwitchOff property.
@return Value of SwitchOff property.
}
procedure SetSwitchOff(const Value: Boolean);
{Write accessor for SwitchOff property. Sets property to specified value.
@param Value [in] New property value.
}
property SwitchOff: Boolean read GetSwitchOff write SetSwitchOff;
{Property that indicates whether code should be emitted to switch off
listed warnings}
function GetEnumerator: TEnumerator<TWarning>;
{Creates an enumerator for all the warnings in the warnings list.
@return Instance of enumerator.
}
{ IAssignable methods }
procedure Assign(const Src: IInterface);
{Assigns properties of another IWarnings instance to this object.
@param Src [in] Reference to object to be assigned. Must support
IWarnings.
}
end;
implementation
uses
// Delphi
SysUtils, Generics.Defaults, Math,
// Project
UConsts, UExceptions, UStrUtils;
{ TWarning }
constructor TWarning.Create(const ASymbol: string; const AMinCompiler: Single);
{Constructor that provides values for both Symbol and MinCompiler properties.
@param ASymbol [in] Warning symbol.
@param AMinCompiler [in] Earliest compiler that supports ASymbol.
}
begin
Assert(ASymbol <> '', 'TWarning.Create: ASymbol is empty string');
fSymbol := ASymbol;
MinCompiler := AMinCompiler;
end;
constructor TWarning.Create(const ASymbol: string);
{Constructor that provides a value for Symbol property and uses default
value for MinCompiler property.
@param ASymbol [in] Warning symbol.
}
begin
// we use earliest compiler to support any $WARN directive as MinCompiler
Create(ASymbol, MinSupportedCompiler);
end;
function TWarning.GetMinCompiler: Single;
{Read accessor for MinCompiler property.
@return Version number of earliest compiler to support Symbol.
}
begin
Assert(fMinCompiler >= MinSupportedCompiler,
'TWarning.GetMinCompiler: fMinCompiler too small');
Result := fMinCompiler;
end;
function TWarning.GetSymbol: string;
{Read accessor for Symbol property.
@return Symbol used in $WARN directive.
}
begin
Assert(fSymbol <> '', 'TWarning.GetSymbol: fSymbol is empty string');
Result := fSymbol;
end;
function TWarning.IsValid: Boolean;
{Checks if properties of this warning are valid.
@return True if warning is valid, False if not.
}
begin
Result := (fMinCompiler >= MinSupportedCompiler) and (fSymbol <> '');
end;
procedure TWarning.SetMinCompiler(const Value: Single);
{Write accessor for MinCompiler property.
@param Value [in] New compiler version to be assigned to property.
}
begin
Assert(Value >= MinSupportedCompiler,
'TWarning.SetMinCompiler: AValue too small');
fMinCompiler := Value;
end;
{ TWarnings }
procedure TWarnings.Add(const AWarning: TWarning);
{Adds a warning to list.
@param AWarning [in] Warning to be added.
@except EBug raised in warning with same symbol is already in list.
}
begin
Assert(AWarning.IsValid, ClassName + '.Add: AWarning not valid');
if fItems.Contains(AWarning) then
raise EBug.CreateFmt(
'%s.Add: AWarning %s already in list', [ClassName, AWarning.Symbol]
);
fItems.Add(AWarning);
end;
procedure TWarnings.Assign(const Src: IInterface);
{Assigns properties of another IWarnings instance to this object.
@param Src [in] Reference to object to be assigned. Must support IWarnings.
}
var
W: TWarning; // references each in warning in Src.
begin
Clear;
for W in (Src as IWarnings) do
Add(W);
fSwitchOff := (Src as IWarnings).SwitchOff;
end;
procedure TWarnings.Clear;
{Clears list of warnings.
}
begin
fItems.Clear;
end;
function TWarnings.Contains(const ASymbol: string): Boolean;
{Checks if a warning with a specified symbol is present in the warnings list.
@param ASymbol [in] Symbol to be checked.
@return True if warning with symbol is in list, False if not.
}
begin
Result := fItems.Contains(TWarning.Create(ASymbol));
end;
function TWarnings.Count: Integer;
{Gets number of warnings in list.
@return Number of warnings.
}
begin
Result := fItems.Count;
end;
constructor TWarnings.Create;
{Constructor. Sets up object.
}
begin
inherited Create;
// use generic list that sorts on warning's symbol to store warnings
fItems := TList<TWarning>.Create(
TDelegatedComparer<TWarning>.Create(
function(const Left, Right: TWarning): Integer
begin
Result := StrCompareText(Left.Symbol, Right.Symbol);
end
)
);
end;
procedure TWarnings.Delete(const AWarning: TWarning);
{Removes a warning from the list based on its symbol.
@param AWarning [in] Warning to be removed.
}
begin
fItems.Remove(AWarning);
end;
procedure TWarnings.Delete(const ASymbol: string);
{Deletes warning containing specified symbol from list.
@param ASymbol [in] Symbol of warning to be deleted.
}
begin
Delete(TWarning.Create(ASymbol));
end;
destructor TWarnings.Destroy;
{Destructor. Tears down object.
}
begin
fItems.Free;
inherited;
end;
function TWarnings.GetEnumerator: TEnumerator<TWarning>;
{Creates an enumerator for all the warnings in the warnings list.
@return Instance of enumerator.
}
begin
Result := fItems.GetEnumerator;
end;
function TWarnings.GetItem(const Idx: Integer): TWarning;
{Read accessor for Items[] property. Gets warning at specified index in
warnings list.
@param Idx [in] Index of required warning.
@return Warning at specified index.
}
begin
Result := fItems[Idx];
end;
function TWarnings.GetSwitchOff: Boolean;
{Read accessor for SwitchOff property.
@return Value of SwitchOff property.
}
begin
Result := fSwitchOff;
end;
function TWarnings.IsEmpty: Boolean;
{Checks whether warnings list is empty.
@return True if warnings list empty, False if not.
}
begin
Result := Count = 0;
end;
function TWarnings.Render: string;
{Generates source code for compiler directives that switch off all warnings in
list for supporting compilers.
@return String containing required source code.
}
var
SB: TStringBuilder; // used to construct source code string
W: TWarning; // each warning in list
SortedList: TList<TWarning>; // list of warnings sorted by compiler version
CurrentVer: Single; // compiler version currently being processed
InsideVer: Boolean; // true if rendering warnings for a compiler ver
begin
if IsEmpty then
Exit('');
// Create a list of warnings sorted by minimum compiler: we do this so we can
// group related warnings together in one conditional statement for each
// minimum compiler version
SortedList := TList<TWarning>.Create(
TDelegatedComparer<TWarning>.Create(
// sort TWarning records by MinCompiler field
function(const Left, Right: TWarning): Integer
begin
Result := Math.CompareValue(Left.MinCompiler, Right.MinCompiler);
end
)
);
try
for W in fItems do
SortedList.Add(W);
SortedList.Sort;
// Generate the source code
CurrentVer := 0.0;
InsideVer := False;
SB := TStringBuilder.Create;
try
SB.AppendLine('{$IFNDEF FPC}');
SB.AppendLine(' {$IFDEF CONDITIONALEXPRESSIONS}');
for W in SortedList do
begin
if not Math.SameValue(W.MinCompiler, CurrentVer) then
begin
// required compiler version has changed
if InsideVer then
begin
// we were writing warnings for previous version: close statement
SB.AppendLine(' {$IFEND}');
InsideVer := False;
end;
// create new condition for new version
SB.AppendFormat(
' {$IF CompilerVersion >= %.2f}' + EOL, [W.MinCompiler]
);
InsideVer := True;
CurrentVer := W.MinCompiler;
end;
// write directive to turn warning off
SB.AppendFormat(' {$WARN %s OFF}' + EOL, [W.Symbol]);
end;
// close off any open conditional statement
if InsideVer then
SB.AppendLine(' {$IFEND}');
// close bounding $IFDEFs
SB.AppendLine(' {$ENDIF}');
SB.AppendLine('{$ENDIF}');
Result := SB.ToString;
finally
SB.Free;
end;
finally
SortedList.Free;
end;
end;
procedure TWarnings.SetSwitchOff(const Value: Boolean);
{Write accessor for SwitchOff property. Sets property to specified value.
@param Value [in] New property value.
}
begin
fSwitchOff := Value;
end;
{ TWarningsPersist }
const
// Names of values stored in persistent storage
cInhibitedName = 'SwitchOffWarnings';
cWarningCountName = 'WarningCount';
cWarningCompoundName = 'Warning%d.%s';
// Names of properties used in compound value names
cWarningSymbolProp = 'Symbol';
cWarningSupportProp = 'MinCompiler';
class procedure TWarningsPersist.Load(Storage: ISettingsSection;
Warnings: IWarnings);
{Loads data from persistent storage into a warnings object.
@param Storage [in] Reference to storage containing needed data.
@param Warnings [in] Warnings object to be updated with data from storage.
}
var
Idx: Integer; // loops thru all warnings in storage
CompilerVer: Double; // min compiler version for a warning read from storage
Symbol: string; // symbol of a warning read from storage
begin
Warnings.Clear;
Warnings.SwitchOff := Boolean(
StrToIntDef(Storage.ItemValues[cInhibitedName], Ord(False))
);
for Idx := 0 to Pred(StrToIntDef(Storage.ItemValues[cWarningCountName], 0)) do
begin
Symbol := Storage.ItemValues[WarningCompoundName(Idx, cWarningSymbolProp)];
if (Symbol = '') or Warnings.Contains(Symbol) then
Continue;
CompilerVer := StrToFloatDef(
Storage.ItemValues[WarningCompoundName(Idx, cWarningSupportProp)],
TWarning.MinSupportedCompiler
);
if CompilerVer < TWarning.MinSupportedCompiler then
CompilerVer := TWarning.MinSupportedCompiler;
Warnings.Add(TWarning.Create(Symbol, CompilerVer));
end;
end;
class procedure TWarningsPersist.Save(Storage: ISettingsSection;
Warnings: IWarnings);
{Saves data from a warnings object to persistent storage.
@param Storage [in] Reference to storage object to receive data.
@param Warnings [in] Reference to warnings object to be persisted.
}
var
Idx: Integer; // loops through all warnings
begin
Storage.ItemValues[cInhibitedName] := IntToStr(Ord(Warnings.SwitchOff));
Storage.ItemValues[cWarningCountName] := IntToStr(Warnings.Count);
for Idx := 0 to Pred(Warnings.Count) do
begin
Storage.ItemValues[WarningCompoundName(Idx, cWarningSymbolProp)] :=
Warnings[Idx].Symbol;
Storage.ItemValues[WarningCompoundName(Idx, cWarningSupportProp)] :=
Format('%.2f', [Warnings[Idx].MinCompiler]);
end;
Storage.Save;
end;
class function TWarningsPersist.WarningCompoundName(const Idx: Integer;
const Prop: string): string;
{Constructs the name of a warning value in storage. Name comprises an index
number and a property name.
@param Idx [in] Index component of name.
@param Prop [in] Property component of name.
}
begin
Result := Format(cWarningCompoundName, [Idx, Prop]);
end;
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.