Recent

Author Topic: Possible codetools bug  (Read 531 times)

440bx

  • Hero Member
  • *****
  • Posts: 4740
Possible codetools bug
« on: November 06, 2024, 12:03:48 pm »
Hello,

Consider the following code:
Code: Pascal  [Select][+][-]
  1.  
  2. program Codetools;
  3.  
  4. uses
  5.   Windows
  6.   ;
  7.  
  8.  
  9. { --------------------------------------------------------------------------- }
  10. { DOS HEADER related types and constants                                      }
  11.  
  12. const
  13.   IMAGE_DOS_SIGNATURE = $5a4d;                        { 'MZ'                  }
  14.  
  15. {$ifndef MS_FIELD_NAMES}
  16. type
  17.   { _IMAGE_DOS_HEADER                                                         }
  18.  
  19.   PIMAGE_DOS_HEADER   = ^TIMAGE_DOS_HEADER;
  20.   TIMAGE_DOS_HEADER   = packed record
  21.     Signature              : word;
  22.     BytesOnLastPage        : word;
  23.     PageCount              : word;
  24.     RelocationsCount       : word;
  25.     HeaderInParagraphs     : word;
  26.     ExtraParagraphsNeeded  : word;
  27.     MaximumMemory          : word;
  28.     InitialSS              : word;
  29.     InitialSP              : word;
  30.     Checksum               : word;
  31.     InitialIp              : word;
  32.     InitialCs              : word;
  33.     RelocationsTableOffset : word;
  34.     OverlayNumber          : word;
  35.     ReservedWordsA         : packed array[0..3] of word;
  36.     OEMidentifier          : word;
  37.     OEMinformation         : word;
  38.     ReservedWordsB         : packed array[0..9] of word;
  39.     OffsetToNewExecutable  : DWORD;
  40.   end;
  41. {$endif}
  42.  
  43.  
  44. {$ifdef MS_FIELD_NAMES}                               {  winnt.h field names  }
  45. type
  46.   { _IMAGE_DOS_HEADER                                                         }
  47.  
  48.   PIMAGE_DOS_HEADER   = ^TIMAGE_DOS_HEADER;
  49.   TIMAGE_DOS_HEADER   = packed record
  50.     e_magic                : word;
  51.     e_cblp                 : word;
  52.     e_cp                   : word;
  53.     e_crlc                 : word;
  54.     e_cparhdr              : word;
  55.     e_minalloc             : word;
  56.     e_maxalloc             : word;
  57.     e_ss                   : word;
  58.     e_sp                   : word;
  59.     e_csum                 : word;
  60.     e_ip                   : word;
  61.     e_cs                   : word;
  62.     e_lfarlc               : word;
  63.     e_ovno                 : word;
  64.     e_res                  : packed array[0..3] of word;
  65.     e_oemid                : word;
  66.     e_oeminfo              : word;
  67.     e_res2                 : packed array[0..9] of word;
  68.     e_lfanew               : DWORD;
  69.   end;
  70. {$endif}
  71.  
  72.  
  73. var
  74.  ImageDosHeader : PIMAGE_DOS_HEADER = nil;
  75.  ImageNtHeaders : pointer           = nil;
  76.  
  77. begin
  78.   { don't try to run this... it won't                                         }
  79.  
  80.   ImageNtHeaders := ImageDosHeader + ImageDosHeader^
  81. end.
goto line 80, type a dot after ImageDosHeader^ and codetools will offer to complete with a set of fields that is not active, i.e, if you select e_lfanew and try to compile, there will be a compile error.  (see attachment)

if it is completed with "OffsetToNewExecutable" (which is _not_ offered by Codetools) then the compile will be successful.

Comments welcome.

Lazarus v3.99 (not the current trunk)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Possible codetools bug
« Reply #1 on: November 06, 2024, 12:50:27 pm »
Comments welcome.
The autocomplete is (somewhat) correct.
You can check this by setting the cursor on ImageDosHeader^.
Press Alt+Up.
Set the cursor on the type PIMAGE_DOS_HEADER and press Alt+Up.
Set the cursor on the type ^TIMAGE_DOS_HEADER and press Alt+Up.

Then you see you end up in struct.inc.

So you have this line and expect the ^TIMAGE_DOS_HEADER points to YOUR new TIMAGE_DOS_HEADER.
But is doesn't. It points to the TIMAGE_DOS_HEADER in struct.inc according to the autocompleter (which isn't actually correct).

Why are you redefining types and not renaming them?
Now you have two TIMAGE_DOS_HEADER in your project (which gets confusing).
Rename it to something else and you have no problems.

I agree... the autocompleter should look at forward definition before going down the tree... but it gets confused by the already defined definition.

BTW. So it's not only the autocompleter but also the code-browser (Alt+Up etc) which does this incorrectly (but maybe those are the same ;) ).
« Last Edit: November 06, 2024, 12:53:41 pm by rvk »

440bx

  • Hero Member
  • *****
  • Posts: 4740
Re: Possible codetools bug
« Reply #2 on: November 06, 2024, 01:01:35 pm »
Why are you redefining types and not renaming them?
Now you have two TIMAGE_DOS_HEADER in your project (which gets confusing).
Rename it to something else and you have no problems.
I agree.

I normally use my own API definitions which includes (and exceeds) those found in Windows.pas but, I was writing a little test program and in that test program, I used the "standard" Windows but needed _my_ image definitions which is how I ended up with the double definition of IMAGE_DOS_HEADER.

IOW, in my non-test code, there is no "Windows", therefore the problem does not occur in that code.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10553
  • Debugger - SynEdit - and more
    • wiki

440bx

  • Hero Member
  • *****
  • Posts: 4740
Re: Possible codetools bug
« Reply #4 on: November 06, 2024, 01:53:44 pm »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018