Recent

Author Topic: [SOLVED] $STOP directive with value taken from SizeOf  (Read 5269 times)

furious programming

  • Hero Member
  • *****
  • Posts: 836
[SOLVED] $STOP directive with value taken from SizeOf
« on: May 08, 2021, 02:18:06 am »
There are several data types in my project, the size of which must be strictly defined. If the size of a given type is different than the required size, compilation is to be aborted and an appropriate message displayed in the Messages window. Currently, I do it this way:

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyType = OtherUnit.OtherType;
  3.  
  4.   {$IF SIZEOF(TMyType) <> 1}
  5.     {$STOP the size of the TMyType is not 1 byte}
  6.   {$ENDIF}

And it works great — in the event of a problem, compilation is interrupted and the fixed message is displayed in the message window.

However, this allows me to know that the size of a given data type is incorrect, but I don't know what it is. Is there any way to use SIZEOF and use the received value to format the message body? I mean something like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyType = OtherUnit.OtherType;
  3.  
  4.   {$IF SIZEOF(TMyType) <> 1}
  5.     {$STOP the size of the TMyType is SIZEOF(TMyType) byte(s), but must be 1 byte}
  6.   {$ENDIF}

to receive this message:

Code: Pascal  [Select][+][-]
  1. 'the size of the TMyType is 4 byte(s), but must be 1 byte'

Is such a thing possible?
« Last Edit: May 11, 2021, 03:52:01 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

speter

  • Sr. Member
  • ****
  • Posts: 338
Re: $STOP directive with value taken from SizeOf
« Reply #1 on: May 08, 2021, 02:32:32 am »
Could you move the size check to the unit's initialisation section?

Something like (untested code):
Code: Pascal  [Select][+][-]
  1. unit xxx;
  2. interface
  3. uses ...
  4. type
  5.   TMyType = ...
  6.  ...
  7. implementation
  8. var
  9.   zzz : integer;
  10.  ...
  11.  
  12. begin
  13.   zzz := sizeof(TMyType);
  14.   if zzz <> 1 then
  15.     showmessage('TMyType is '+zzz.tostring+' bytes');
  16. end.
  17.  

Of course, this won't happen during compilation, so probably isn't that useful. ;(

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: $STOP directive with value taken from SizeOf
« Reply #2 on: May 08, 2021, 02:37:12 am »
Unfortunately I don't have an answer for you but, I have a question instead.  Do you happen to have a link to a page that documents all the functions that can be called at compile time ?

I ask for two reasons, I didn't know that sizeof could  be used in compiler directives.  I eventually found a documentation page where it was stated that "sizeof" could be used in compile time expressions but, I was not able to find a list of compile time functions.

Maybe (big maybe), "length" is another function that can be used in compiler directives (I haven't tried it but, it would be very nice if it were available.)

A page that documents all the functions that can be used in compiler directive expressions would be very nice.  If you happen to know of one, please do share.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

furious programming

  • Hero Member
  • *****
  • Posts: 836
Re: $STOP directive with value taken from SizeOf
« Reply #3 on: May 08, 2021, 02:46:03 am »
@speter: nope, I would like to use compiler directives to abort compilation, not to abort the application startup. 8)


Unfortunately I don't have an answer for you but, I have a question instead.  Do you happen to have a link to a page that documents all the functions that can be called at compile time ?

@440bx: hmm… no. Unfortunately, (AFAIK) the issue of directives is not fully documented. For example, the + operator that can be used with DEFINED is not described, but can be used to sum results of the DEFINED and then, test the result against the fixed number. Example:

Code: Pascal  [Select][+][-]
  1. {$IF DEFINED(FOO) + DEFINED(BAR) + DEFINED(QUX) <> 3}
  2.   {$STOP not all mandatory symbols declared}
  3. {$ENDIF}

Quote
I ask for two reasons, I didn't know that sizeof could  be used in compiler directives.

SIZEOF can be used in combination with $IF, but I don't know if it can be used to format the error message text. That's why I'm asking, if it is possible to somehow format the message text with some integers.
« Last Edit: May 08, 2021, 02:49:03 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: $STOP directive with value taken from SizeOf
« Reply #4 on: May 08, 2021, 02:56:04 am »
@furious programming

Thank you for sharing what you know about it.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: $STOP directive with value taken from SizeOf
« Reply #5 on: May 08, 2021, 03:49:38 pm »
A page that documents all the functions that can be used in compiler directive expressions would be very nice.  If you happen to know of one, please do share.

How about this one? ;) (Though the arithmetic operators aren't documented (essentially the whole set is supported), so for those a bug report should be filed)

furious programming

  • Hero Member
  • *****
  • Posts: 836
Re: $STOP directive with value taken from SizeOf
« Reply #6 on: May 08, 2021, 06:41:12 pm »
@PascalDragon: useful link, thanks. But can you answer my question? Is it possible to format the content of the error message?
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6646
Re: $STOP directive with value taken from SizeOf
« Reply #7 on: May 08, 2021, 06:48:40 pm »
@PascalDragon: useful link, thanks. But can you answer my question? Is it possible to format the content of the error message?

I don't think that will do what you want. I'm similarly paranoid about such things, but put them into assertions in the startup code where one /can/ get (and display) an unambiguous actual size.

Otherwise I suggest writing a cut-down test program which is compiled and run as part of the build sequence, i.e. very much like the traditional ./configure scripts would do on a unix platform.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: $STOP directive with value taken from SizeOf
« Reply #8 on: May 08, 2021, 09:43:55 pm »
Is there any way to use SIZEOF and use the received value to format the message body?

No, because {$STOP} does not allow formatted messages.  You would have to check each size individually and {$STOP} a different message for each one, eg:

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyType = OtherUnit.OtherType;
  3.  
  4.   {$IF SIZEOF(TMyType) <> 1}
  5.     {$IF SIZEOF(TMyType) = 2}
  6.       {$STOP the size of the TMyType is 2 bytes, but must be 1 byte}
  7.     {$ELSEIF SIZEOF(TMyType) = 4}
  8.       {$STOP the size of the TMyType is 4 bytes, but must be 1 byte}
  9.     ...
  10.     {$ELSE}
  11.       {$STOP the size of the TMyType is > 1 byte, but must be 1 byte}
  12.     {$ENDIF}
  13.   {$ENDIF}

Needless to say, that is very verbose coding.  Probably not worth the effort.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: $STOP directive with value taken from SizeOf
« Reply #9 on: May 08, 2021, 10:12:05 pm »
How about this one? ;) (Though the arithmetic operators aren't documented (essentially the whole set is supported), so for those a bug report should be filed)
That is really nice, thank you.  I have to say, not an easy page to find. 

Also, I have not been able to find an "equivalent" in the FPC (pdf) documentation.

Thank you again, that is really useful.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: $STOP directive with value taken from SizeOf
« Reply #10 on: May 09, 2021, 01:26:23 am »
Also, I have not been able to find an "equivalent" in the FPC (pdf) documentation.

It's the Programmer's Guide, so prog.pdf. Chapter 2, section 2.4 for FPC 3.2.0
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: $STOP directive with value taken from SizeOf
« Reply #11 on: May 09, 2021, 01:43:21 am »
It's the Programmer's Guide, so prog.pdf. Chapter 2, section 2.4 for FPC 3.2.0
You're right!. It's right there.  Thank you for pointing that out. 

I've read all the manuals from top to bottom but, obviously not all the information got recorded in my neurons.  I gotta upgrade my memory to DDR4.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: $STOP directive with value taken from SizeOf
« Reply #12 on: May 09, 2021, 02:16:40 am »
If it's any consolation, I had to unzip the pdf docs and look into it because I didn't remember whether it was really there despite having just read that manual (again!) :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: $STOP directive with value taken from SizeOf
« Reply #13 on: May 09, 2021, 02:28:21 am »
How about this one? ;) (Though the arithmetic operators aren't documented (essentially the whole set is supported), so for those a bug report should be filed)

Bug report filed: https://bugs.freepascal.org/view.php?id=38866  O:-)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: $STOP directive with value taken from SizeOf
« Reply #14 on: May 09, 2021, 06:05:27 pm »
@PascalDragon: useful link, thanks. But can you answer my question? Is it possible to format the content of the error message?

No, it's not.

Also, I have not been able to find an "equivalent" in the FPC (pdf) documentation.

The preprocessor related parts are documented in the programmer's guide, cause it's strictly not part of the language.

 

TinyPortal © 2005-2018