If possible, avoid thinking of it as "LLP64" specifically, and use the datatypes provided (uintptr_t
, size_t
, etc.), might as well have code that should be correct elsewhere. Especially do not assume long
is 64bits.
I think i need at least a parse tree / AST along with a symbol table.
I don't believe a perfect solution exists, there are already static analysis tools available you can search for can use to catch many possible errors, but some things still get past and the sensitivity level needed to detect some of them can give a lot of false positives.
Compilers can detect the basic ones you will easily find with your own parser, for example:
auto x = (SomeTypeDefedThing)*some_pointer;
int y = some_long;
warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
warning: conversion to ‘int’ from ‘long int’ may alter its value [-Wconversion]
Explicit integers conversions are a bigger problem, if you have a x = (int)y
or x = (long)y
, etc., the compiler just assumes you meant that conversion, and determining if y
can be out of range is really tricky.
While you might be able to find this in static analysis, I expect such a warning to be very noisy, as such an explicit conversion normally really did want to do that.
If you are doing any binary file or network IO, that can be a problem. Ideally it will have been written with the types already provided to work correctly between different implementations. Such code can take many forms and be hard to simply search for, but if you know your program does do such file or network things, it should be easy to identify and check manually.
If you really need to look for a specific pattern that is very common in your code, but not detected by pre-existing compilers and static analysis tools, then maybe putting together your own can help.
Clang provides a number of useful libraries and tools to look at. While GCC (and other toolchains like MSVC) probably have intermediate file formats you can parse, I have not seen them used and seems to be far less documentation (if complete at all) on those.
They have an introduction doc, and you can get the ast by compiling with -ast-dump
. https://clang.llvm.org/docs/IntroductionToTheClangAST.html