Jump to content
Ian Branch

Issue with UsesCleaner..

Recommended Posts

Hi Team,

I couldn't find where to report this to Uwe so here I am..

D10.4.1, Win 10, 32bit.

I downloaded what I believe is the latest UsesCleaner.zip, built it and tried it on an App.

It appeared to do the right thing producing this..

unit MainFrm;

interface

uses
  System.SysUtils,
  System.StrUtils,
  System.UITypes,
  System.TimeSpan,
  System.ImageList,
  vcl.wwdbgrid,
  vcl.Buttons,
  vcl.Grids,
  vcl.wwdbigrd,
  vcl.DBCtrls,
  vcl.ImgList,
  Windows,
  Messages,
  Variants,
  Classes,
  Graphics,
  Controls,

Unfortunately, when I went to build my project I got this error.."[dcc32 Error] MainFrm.pas(326): E2010 Incompatible types: 'NativeUInt' and 'TSearchRec'"

on this line.."    FindClose(sr);" in this code..

procedure FillFiles(Strings: TStrings; const strDirectory: string);
var
  sr: TSearchRec;
begin
  if FindFirst(strDirectory + '\*.tbl', faAnyFile, sr) = 0 then
  try
    Strings.BeginUpdate;
    try
      Strings.Clear;
      repeat
        Strings.Add(sr.Name);
      until FindNext(sr) <> 0;
    finally
      Strings.EndUpdate;
    end;
  finally
    FindClose(sr);
  end;
end;

After some head scratching and trying various things I eventually changed the order of the uses clause to this...

unit MainFrm;

interface

uses
  Windows,
  System.SysUtils,
  System.StrUtils,
  System.UITypes,
  System.TimeSpan,
  System.ImageList,
  vcl.wwdbgrid,
  vcl.Buttons,
  vcl.Grids,
  vcl.wwdbigrd,
  vcl.DBCtrls,
  vcl.ImgList,
  Messages,
  Variants,
  Classes,
  Graphics,
  Controls,

Note the relocation of the Windows,

The App now builds and runs fine.

Is this a bug with UsesCleaner or Delphi??

 

Regards,

Ian

 

Share this post


Link to post

the USES "order" it's very important, when, more than one unit have a function/procedure/var etc... with same name, but, for other use!

Like:

  • unit System.pas have a function named: FindClose(  ... it wait for a Handle = a integer ) 
    • function FindClose(FindFile: THandle): BOOL; stdcall; external kernel name 'FindClose';
  • unit System.SysUtils.pas have a function named: FindClose(  ... it wait for a "TSearchRec" type )
    • System.SysUtils.FindClose( SR ); // TSearchRec type
  • unit WinAPI.WIndows.pas have a function named: FindClose( ... it wait for a Cardinal type )
       WinApi.Windows.FindClose( NativeUInt):LongBool;

 

your case, the order was the cause of this problem!

For avoid it, if you dont works with others IDE editions, have always use "NameSpaces" when occurrs some like this!

 

  • System.FindClose( THandle)
  • System.SysUtils.FindClose( TSearchRed );
  • WinAPI.Windows.FindClose( NativeUInt );

 

hug

Edited by emailx45
  • Like 2

Share this post


Link to post

Ahhh.  Thank you.

I has sorta deduced that it was a Uses order thing but didn't know why.

I note that most of the units in my App have winapi.windows as the first uses entry.

So.  Given the procedure was expecting a TSearchRec type, and Sysutils is now after Windows, suggests that the units further down the uses list override the previous as it were.  i.e. SysUtils.FindClose overrode Windows.FindClose.  Something I never considered/realised.

 

Regards & Tks.

Ian

Share this post


Link to post

Thanks Uwe,

There wasn't a UsesCleaner.cfg in the .zip, should there have been?

Anyway, I have created one based on your example in the reference you gave.

 

Regards,

Ian

Share this post


Link to post
6 minutes ago, Ian Branch said:

There wasn't a UsesCleaner.cfg in the .zip, should there have been?

No, the zip only contains the source. I have attached another one with a compiled exe and a cfg file.

UsesCleaner.zip

  • Thanks 2

Share this post


Link to post

Hi Uwe,

Cheers & Tks.

Regards,

Ian

 

Update - Excellent!!  Ran it over all my files.  Really like what it does with the Uses.   Thumbs Up. :-)

 

Ian

Edited by Ian Branch

Share this post


Link to post

FWIW - UsesCleaner was about moving from this..

uses
  Windows, Messages, System.SysUtils, System.StrUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGrids, System.UITypes,
  System.TimeSpan, System.ImageList, System.RegularExpressions, System.Variants, DB, ImgList, ComCtrls, StdCtrls, 
  ExtCtrls, DateUtils, ImageHlp, Vcl.DBCtrls, vcl.wwdbgrid, Vcl.Mask, vcl.wwdbedit, vcl.wwdbigrd; 

To this..

uses
  Winapi.Windows, Winapi.Messages, Winapi.ImageHlp,
  System.SysUtils, System.StrUtils, System.UITypes, System.TimeSpan, System.ImageList, System.RegularExpressions, System.Variants,
  System.Math, System.Classes, System.DateUtils,
  Data.DB,
  Vcl.DBCtrls, vcl.wwdbgrid, Vcl.Mask, vcl.wwdbedit, vcl.wwdbigrd, vcl.wwdatsrc, Vcl.Buttons, vcl.wwspeedbutton,
  vcl.wwdbnavigator, vcl.wwclearpanel, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, Vcl.ImgList,
  Vcl.ComCtrls, Vcl.StdCtrls, Vcl.ExtCtrls; 

Where the relevant units are now properly qualified and unexpectedly but nicely organised.

 

This is clearly a personal preference thing but it is now my preference.

 

Ian

Share this post


Link to post
11 minutes ago, Ian Branch said:

This is clearly a personal preference thing but it is now my preference.

It's not really about personal preference, but using the correct unit namespaces, you are asking less of the compiler when it comes to finding units and resolving types etc. This improves compile times and results in less memory usage by the compiler. 

 

Unless you are writing code that must compile on versions earlier than XE2 you should be doing this. 

Share this post


Link to post

Hi Vincent,

10 minutes ago, Vincent Parrett said:

Unless you are writing code that must compile on versions earlier than XE2 you should be doing this. 

You are of course quite correct.  As my Apps were originally developed in D7 and have just grown up through the Delphi editions to now D10.4.1, without the necessary tweaking/refinement, UsesCleaner gave me the tool to do that painlessly.

In regard to my 'preference', I was referring to the formatting of the uses clause.

I use GExperts and the Uses Clause Manager will do the same thing, I just hadn't had the explicit need/urge to use it on every unit.

I don't know if there is a setting somewhere to have GExperts format the Uses Clause the same as UsesCleaner. 

If there is I would appreciate being pointed to it.

If there isn't it would be a nice enhancement.

 

Ian

Share this post


Link to post

I thought ppl. who are having more than one unit in a row are doesn't care about the order as it's impossible to reorder it quickly with the line-up/down expert.

Edited by Attila Kovacs

Share this post


Link to post
8 hours ago, Attila Kovacs said:

I thought ppl. who are having more than one unit in a row are doesn't care about the order as it's impossible to reorder it quickly with the line-up/down expert.

Setting the Compressed option in the config file to 0 places each unit in a separate line.

 

10 hours ago, Ian Branch said:

unexpectedly but nicely organised.

The grouping and order of the groups can be controlled with the GroupNames option in the config file. Leaving that empty will preserve the original order. Inside a group the original order is preserved, too.

Share this post


Link to post
10 hours ago, Vincent Parrett said:

@Uwe Raabe any chance you could put this on github? I think a lot more people would be able to find it then.

Yeah, that seems like a god idea.

  • Like 2

Share this post


Link to post
12 hours ago, Ian Branch said:

I don't know if there is a setting somewhere to have GExperts format the Uses Clause the same as UsesCleaner. 

If there is I would appreciate being pointed to it.

If there isn't it would be a nice enhancement.

No there isn't. But if you file a feature request stating how exactly you want it to be formatted, chances are I might implement it.

Share this post


Link to post

It would be much more logical to have an option in UsesCleaner which makes it follow the settings in GExperts formatter.  I want to define my formatting preferences in the formatter, which seems to me to be the right way to do it.  Why should I have to reformat every unit with my chosen formatter settings (without any option set to match another application, of course) in order to fix the unwanted format changes that the UsersCleaner has made?

 

If I could find the UsesCleaner site and a place for feature requests, I would add one: please add an option to never change unit sequence and never add or delete newlines in the uses section and always retain comments.  This would then cover a preference in any formatter to have one unit per line.

Edited by timfrost
added para2

Share this post


Link to post

Hi Tim,

You are quite correct and I implied that in my feature request.

https://sourceforge.net/p/gexperts/feature-requests/128/

 

Old saying - You can please some of the people all of the time and all of the people some of the time, but not all of the people all of the time. 😉

(Poet John Lydgate as made famous by Abraham Lincoln)

Ian

Edited by Ian Branch

Share this post


Link to post
2 hours ago, timfrost said:

If I could find the UsesCleaner site and a place for feature requests

There simply is none - yet. As I provided the sources feel free to tweak it to your needs.

 

2 hours ago, timfrost said:

This would then cover a preference in any formatter to have one unit per line.

This is already covered by the Compressed = 0 option in the config file. 

Share this post


Link to post

I have only seen a link to a zip with the EXE/CFG files.  If I had known where to find the source I might have worked out what compressed=0 does!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×