JCF and conditional compilation

Conditional compilation ( {$IFDEF} and associated statements) can be hard for the code formatter to deal with.

There are two ways which the code formatter can approach conditional compilation, and your choice of which one to use will depend on how your code uses conditional compilation directives. It can
1) Ignore the directives, and treat them as comments. This is the default option, and it means that all code will be formatted if it can be parsed.
2) It can take a particular path through the directives, making assumptions about which ones are defines, and ignoring code where it is not. This means that all code can be parsed, but not all of it will be formatted.

The code formatter is a parser, and it will parse correct Delphi source. It also ignores some aspects that the Delphi compiler insists on, like matching up variable and function names. To enable this, go the the settings|PreProcessor tab, and check "Enable preprocessor parsing", then enter the symbols that should be defined for the formatter.

When to use which options? The default option of ignoring the compiler directives will not work with source laid out as follows:


{$IFDEF SOMETHING}
function foo: WordBool;
{$ELSE}
function foo: boolean;
{$ENDIF}
begin
  Result := False;
end;

Since without directives, this reads as

function foo: WordBool;
function foo: boolean;
begin
  Result := False;
end;
Which is not legal delphi code. However, if it was written as

{$IFDEF SOMETHING}
function foo: WordBool;
begin
  Result := False;
end;
{$ELSE}
function foo: boolean;
begin
  Result := False;
end;
{$ENDIF}
The code formatter would be able to parse both parts of this. It will also be able to parse single statements with IFDEFs, e.g.:

uses
  { delphi }
  {$IFNDEF FPC}Windows,{$ENDIF} Contnrs,
  { local }
  ParseTreeNode,
Similarly, both paths of code like this cannot be parsed at the same time:
 Result := {$IFDEF FOO} FooFunction(); {$ELSE} BarFunction(); {$ENDIF} 
But it can be parsed if written as follows:
 
{$IFDEF FOO} 
  Result := FooFunction(); 
{$ELSE} 
  Result := BarFunction(); 
{$ENDIF}