Skip to content

Conversation

@gilles-peskine-arm
Copy link
Contributor

Check that macros are defined before conditionals on these macros. This is useful as a sanity check on our headers. Note that some false positives are expected.

Uses pcpp.


When a project macro (MBEDTLS_xxx, PSA_xxx, TF_PSA_CRYPTO_xxx) is used in
a preprocessor conditional, there should normally be a conditional #define
before the #if directive that uses this macro. This script prints macros
used in conditionals for which there is no previous #define.

#line 42 "crypto_config.h"
//#define MBEDTLS_OPTION    // the script works as with config.py realfull
#line 1 "some_header.h"
#if MBEDTLS_OPTION          // <-- ok (commented-out define in config.h)
#define MBEDTLS_INTERNAL
#endif
#if MBEDTLS_INTERNAL        // <-- ok (conditional define above)
#endif
#if MBEDTLS_VALUE           // <-- flagged
#endif
int x = MBEDTLS_VALUE;      // <-- ignored (only pp directives are checked)
#define MBEDTLS_VALUE

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.already_included = set() #type: Set[str]
self.define_seen = set() #type: Set[str]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does define_seen/already_included need to be reset when assessing multiple files to prevent macros detected in one file not being detected in future files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, good point. I think the easiest fix would be to use a separate Checker instance for each command line argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: In Development

Development

Successfully merging this pull request may close these issues.

2 participants