[fpc-pascal] Checking the validity of Format and friends at compile-time

Michalis Kamburelis michalis.kambi at gmail.com
Wed Oct 5 04:32:49 CEST 2016


I'm wondering if there is a tool that checks the correctness of calls
to Format and friends (like Exception.CreateFmt) in your code. Or does
someone plan to write one, as an external tool (like a "lint") or
inside the FPC compiler:)

For example, the call

  Format('%s', [123])

is guaranteed to result in an EConvertError at runtime. But nothing at
compile-time detects or warns about it.

Sure, it's not possible to check all cases of Format usage, as you can
construct the Format pattern (or list of arguments) at runtime. You
can also replace resourcestrings at runtime. Still, such tool would be
useful since (at least in my code) the majority or Format calls have
constant strings and could be checked at parse time.

The tool would have to know which routines have Format-like parameters
(and at which positions), to capture Format, and Exception.CreateFmt
(and I have a couple of more in my own units). They could be marked in
the source code, by some special directive or a special comment, or
the checking tool should just know their names.

Such tool seems useful for me. It would at least catch *some* blatant
errors, that otherwise sleep undetected until runtime. E.g. today I
have a bugreport that user observed an EConvertError in my program
(unfortunately, he cannot write more exactly what is the message, or
send a screenshot...). I suspect the bug is in one of my Format or
EXxx.CreateFmt calls, but I have a lot of them. Most of them have
constant pattern (and a list of arguments with size and types known at
compile-time), so it should be possible to run some kind of "lint" on
my code... except I don't have any:)

Does anyone know of such a tool?

If you don't have a good answer, I may implement it myself:) I'd like
to use fcl-passrc for it, but so far it fails to parse my real-life
code in Castle Game Engine:) I submitted a couple of bugreports about
it today.


More information about the fpc-pascal mailing list