0

Im trying to get something like 'custom-made' compiler warnings when the user uses variable types like int8_t which could result in unwanted behaviour (Are int8_t and uint8_t intended to be char types?).

Therefore I'd like to (optionally) replace these variable types and append a warning when they are used. My idea so far is something like this:

header.h

#pragma once

#pragma warning ("int8_t can be interpreted as char - replaced with int16_t")
#define int8_t int16_t

But this will not print the custom error msg, neither will it print it for each replacement but instead always print once, whether int8_t was replaced or not.

If you have any suggestions how to tackle this problem I would gladly appreciate any help!

cdol
  • 28
  • 6
  • 4
    `#define int8_t int16_t` No. Don't. – eerorika Jul 23 '20 at 12:07
  • I suppose the easy way is to have a script (run in CMake or a job in your CI system) that will just grep through modified files and print that "warning" and perhaps do something else (stop compilation?). But don't do a macro substitution. Let developer consider the risks on their own and either ignore the warning or change the type to `char` or `int16_t` themselves. – Yksisarvinen Jul 23 '20 at 12:09
  • This is going to depend on the features of your specific compiler, so you'll need to say what compiler you are using. For gcc, the `deprecated` type attribute may be useful. – Nate Eldredge Jul 23 '20 at 12:14

0 Answers0