Holgerwa
Holgerwa

Reputation: 3460

Compiler Hint: "Inline function '...' has not been expanded..."

In a unit I use the function DeleteFile and the compiler outputs a hint:

"H2443 Inline function 'DeleteFile' has not been expanded because unit 'Windows' is not specified in USES list"

In Uses there is SysUtils, which defines DeleteFile (although internally calling Windows.DeleteFile).

What does this hint mean? If I put Windows into the Uses clause, it's gone, but I would like to understand what it is that bothers the compiler.

Upvotes: 16

Views: 14174

Answers (4)

Ian Boyd
Ian Boyd

Reputation: 256701

I was also confused by this hint. Then i realized what the issue is. Your code:

uses
   SysUtils;

procedure TForm1.DoStuff;
begin
   SysUtils.DeleteFile('foo');
end;

is literally being replaced with:

uses
   SysUtils;

procedure TForm1.DoStuff;
var
  Flags, LastError: Cardinal;
begin
  Result := Winapi.Windows.DeleteFile(PChar(FileName));

  if not Result then
  begin
    LastError := GetLastError;
    Flags := GetFileAttributes(PChar(FileName));

    if (Flags <> INVALID_FILE_ATTRIBUTES) and (faSymLink and Flags <> 0) and
      (faDirectory and Flags <> 0) then
    begin
      Result := RemoveDirectory(PChar(FileName));
      Exit;
    end;

    SetLastError(LastError);
  end;
end;

If you'll notice, your "new" code depends on WinApi.Windows unit:

Result := Winapi.Windows.DeleteFile(PChar(FileName));

which you didn't include in your uses clause.

If you manually had inlined the code (copied and pasted), the code would simply not compile until you added Windows to your uses.

Instead, the compiler will not do the inline because:

Inline function 'DeleteFile' has not been expanded because unit 'Windows' is not specified in USES list"

Upvotes: 11

Toon Krijthe
Toon Krijthe

Reputation: 53366

Inline functions can be expanded inline. For example:

function AddPlus(const A,B: Integer): Integer; inline;
begin
  Result := A + B + 1;
end;

var
  x,y,z : Integer;
begin
  y := 22;
  z := 11;
  x := AddPlus(y, z);
end.

Is rewritten to:

var
  x,y,z : Integer;
begin
  y := 22;
  z := 11;
  x := y+z+1;
end.

This removes the overhead of a function call.

But in order to replace the call with the function body, the compiler needs more information, hence the complaint about the unit.

Beware that not all inline functions are converted. Some are treated just like normal functions (its up to the compiler). Besides, inline is only needed at really tight performance bottlenecks.

Upvotes: 12

Davy Landman
Davy Landman

Reputation: 15439

It's an inlining restriction.

See Hallvard Vassbotn's article about Inlined Routines.

Extracted from that site:

The rest of the inlining restrictions are common for both platforms and the most important ones are

  • no inlining across package boundaries
  • the inlined routine cannot access implementation section identifiers
  • the call site must have access to all identifiers used in the inlined routine

Note The last point means that unless the call site unit uses the units required by the routine, the routine cannot be inlined. When this happens, the compiler emits a hint like this

 [Pascal Hint] InlinedRoutinesU.pas(14): H2443 Inline function 
   'InlineMe' has not been expanded because unit 'RequiredUnit' 
    is not specified in USES list 

To resolve the issue, add the missing unit name to the call site's uses clause.

Upvotes: 27

RobS
RobS

Reputation: 3857

Inline functions are expanded in place by the compiler, avoiding the overhead of a function call. E.g. for squaring, sqr(x) is compiled in as x*x rather than calling a function that multiplies x and returns the result.

Upvotes: 1

Related Questions