(Tagged FastMM4 as that’s the first code I saw these warnings to be turned off)
Delphi 7 introduced introduced warnings for unsafe constructs like W1047 and W1048 so you could prepare your code for the first Delphi .NET compilers .
The oldest online documentation on this is in Delphi 2007:
After Delphi 2007, the .NET compiler got shelved, but the errors and warning stayed as they serve a good purpose for native code as well.
Delphi 2007 did not document any of the other directives.
Unlike the D2007 documentation, however, the UNSAFECODE should be written UNSAFE_CODE as with using {$WARN UNSAFECODE ON}, you will get this error:
E1030 Invalid compiler directive: 'UNSAFECODE'
Looking at the library code and example code that ships with Delphi, these are the valid $WARN compiler directives having to do with UNSAFE:
UNSAFE_CAST (since Delphi 7, but only used in Vcl.WinXPanels.pas introduced in Delphi 10.2 Tokyo and up)
UNSAFE_CODE (since Delphi 7, but still documented as UNSAFECODE)
UNSAFE_TYPE (since Delphi 7)
UNSAFE_VOID_POINTER (since Delphi XE3, as precursor to the NEXTGEN compilers)
The ultimate source for these is the file DCCStrs.pas that has shipped since Delphi 2009: [WayBack] warnings – Identifiers for Delphi’s $WARN compiler directive – Stack Overflow.
A problem is that current documentation still lists the wrong name in many places:
This one finally got it right: [WayBack] Warning messages (Delphi) – RAD Studio
Note it also documented UNSAFE_VOID_POINTER:
UNSAFE_VOID_POINTER |
W1070 |
[WayBack] W1070 Use of untype pointer can disrupt instance reference counts (Delphi) – RAD Studio
And these warning messages still do not contain the directives, but do explain the underlying code construct better:
- [WayBack] W1046 Unsafe type ‘%s%s%s’ (Delphi) – RAD Studio
You have used a data type or operation for which static code analysis cannot prove that it does not overwrite memory. For example, you might receive this warning if you declare something as absolute. Such code can be considered a security risk.
- [WayBack] W1047 Unsafe code ‘%s’ (Delphi) – RAD Studio
You have used a data type or operation for which static code analysis cannot prove that it does not overwrite memory. Such code can be considered a security risk.For example, using GetMem can elicit this warning because a block of memory has no associated type.
- [WayBack] W1048 Unsafe typecast of ‘%s’ to ‘%s’ (Delphi) – RAD Studio
You have used a data type or operation for which static code analysis cannot prove that it does not overwrite memory. For example, you might have cased one record to another or one instance to another.
You have to use these directives:
// Get rid of "W1047 Unsafe code 'ASM'", "W1047 Unsafe code '^ operator'", "W1047 Unsafe code '@ operator'" and similar
{$WARN UNSAFE_CODE OFF}
// Get rid of "W1048 Unsafe typecast of 'TFreedObject' to 'PByte'" and similar
{$WARN UNSAFE_CAST OFF}
Back in the days, some people were not amused and disabled the warnings, for instance in [Archive.is] Re: How can I eliminate these warnings in Delphi 7 which did not appear in Delphi 5. – Google Groups:
I have one include file that I usually include in all projects as follows—– WarningsOff.inc —————-
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF CompilerVersion >= 14}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN SYMBOL_DEPRECATED OFF}
{$WARN SYMBOL_LIBRARY OFF}
{$WARN UNIT_DEPRECATED OFF}
{$WARN UNIT_LIBRARY OFF}
{$WARN UNIT_PLATFORM OFF}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_CAST OFF}
{$IFEND}
{$ENDIF}
———————and it gets ride of all unwanted warnings in the IDE or even DCC32.exe when compiling the project
from the command line.
I just add the following line to the project .dpr file and do not worry about the rest.
{$I WarningsOff.inc}
Dennis Passmore
“If you cannot conceive the idea you
will never achieve the desired results”
I disagree with such an approach, as those warnings have their purpose.
Knowing how to selectively disable/enable them however, is important.
–jeroen