Fpsonar conditional compilation

← Back to fpsonar

Four rules about {$ifdef} / {$ifndef} blocks: branches that hold nothing, branches that can never be reached, and guards naming a symbol that does not exist.

All are off by default.

Two of them — ConditionalBranchNeverCompiled and UnknownConditionalSymbol — depend on knowing which symbols are defined. Read the warning under each before enabling it.

Empty branches

edit

EmptyConditionalBranch

edit

minor · off by default

A branch of a closed conditional that contains no source text. Comments do not count as content, so a branch holding only a note is still empty.

Usually the leftover of a change that removed the code but not the guard. Occasionally it is deliberate — a placeholder for a port that has not been written — in which case a // NOSONAR comment says so more clearly than an empty block does.

The rule looks only at conditionals that name a symbol and carry no {$elseif}; anything more complicated is left alone.

fpsonar complains about this

{$ifdef USE_CACHE}
{$endif}

{$ifdef WINDOWS}
  InitConsole;
{$else}
  { nothing to do here }
{$endif}

fpsonar accepts this

{$ifdef USE_CACHE}
  InitCache;
{$endif}

{$ifdef WINDOWS}
  InitConsole;
{$endif}

NegatedConditionalWithEmptyElse

edit

minor · off by default

An {$ifndef} whose {$else} branch is empty while the first branch has content. The {$else} adds nothing and reads as though a case were handled.

Only one of the two empty-branch rules fires on any one conditional: when the first branch is also empty, EmptyConditionalBranch reports it instead.

fpsonar complains about this

{$ifndef EMBEDDED}
  LoadPlugins;
{$else}
{$endif}

fpsonar accepts this

{$ifndef EMBEDDED}
  LoadPlugins;
{$endif}

Symbols and reachability

edit

The two rules below answer questions about which symbols are defined, so they are only as good as the define set fpsonar was given. Both deliberately report when they are unsure, which is the opposite of how most fpsonar rules behave — elsewhere, an unknown fact means silence.

Give fpsonar the defines your build uses:

fpsonar analyze src --define USE_CACHE --define WINDOWS

ConditionalBranchNeverCompiled

edit

minor · off by default

A branch whose guard is false under the configured defines, so nothing inside it is ever compiled. Dead code that the compiler cannot warn about, because it never sees it.

Read this before enabling. fpsonar analyses one define set at a time, while real code is usually built for several targets. A branch guarded by {$ifdef WINDOWS} is not dead — it is dead in this analysis. Run the rule once per target configuration, or restrict it to project-specific feature symbols where a single answer is correct.

fpsonar complains about this

// analysed without OLD_PARSER defined
{$ifdef OLD_PARSER}
  Result := LegacyParse(aText);     // never compiled
{$endif}

fpsonar accepts this

// the branch is gone, or OLD_PARSER is genuinely part of the build
Result := Parse(aText);

UnknownConditionalSymbol

edit

major · off by default

A guard naming a symbol that is neither defined for this analysis nor recognised as a standard FPC, target or dialect symbol. The usual cause is a typo, and a typo in an {$ifdef} fails silently — the branch simply never compiles, and nothing complains.

The recognised set is a list of names and globs, tunable with the knownSymbols parameter. It ships covering the compiler families (FPC, FPC_*, VER?, CPU*, ENDIAN_*, …), the operating systems (UNIX, LINUX, WINDOWS, DARWIN, AMIGA, …) and the dialect symbols (PAS2JS, UNICODE, UNICODERTL).

Read this before enabling. A symbol your project defines but never tells fpsonar about will be reported. Add your own to the list rather than switching the rule off:

{
  "rules": {
    "UnknownConditionalSymbol": {
      "enabled": true,
      "params": { "knownSymbols": "FPC,FPC_*,CPU*,UNIX,LINUX,WINDOWS,MYAPP_*" }
    }
  }
}

fpsonar complains about this

{$ifdef WINDWOS}          // typo — this branch is never compiled anywhere
  InitConsole;
{$endif}

fpsonar accepts this

{$ifdef WINDOWS}
  InitConsole;
{$endif}
edit

Rules about portability across targets — path separators, line endings, record layout and byte counts — are in portability. They ship in the same unit as these, because both families came from the same review of conditional-compilation practice.

See also

edit
  • fpsonar — what fpSonar is, and how to run it

← Back to fpsonar