ualberta-smr/varclang

Expanded macro tokens don't get assigned the correct presence condition

Opened this issue · 1 comments

This can mess up the presence condition of an entire declaration. For example:

#define INT int

#ifdef A
INT j;
#endif

This gets parsed as:

int j; // In context: True

I think what is happening is that when the INT macro is expanded, the preprocessor or lexer inserts an int token which has presence condition True since the original macro was defined in context True. Then, when parsing the declaration specifiers, the parser see a token with condition True and assigns the decl condition True.

I have re-opened this issue because the fix I applied is just temporary and is not fully correct. Pull request #50 makes it so that macro expansion tokens are only assigned the presence condition of the expansion location. The presence condition of the definition location is not taken into account. I chose to do this as it was simply and solved the immediate issues I was having.

Eventually, this tool should implement the correct handling of conditional macros. See this paper for details.

Some things which will need to be taken into consideration when implementing conditional macros:

  • The presence condition of expansion tokens should be the conjunction of the presence condition of the expansion site and the presence condition of the definition site.
  • The preprocessor should be able to insert multiple expansions into the token stream if multiple macro definitions exist under different contexts.
  • If the disjunction of all the presence conditions of all possible macro definitions is not a tautology, the unexpanded token also needs to be inserted into the token stream with the appropriate presence condition. See the paper.
  • The preprocessor macro table will need to be modified to allow multiple definitions of the same macro name but under different contexts. See the paper.
  • Following the typechef approach, we will need to be able to handle undisciplined annotations in order to fully implement conditional macros. For example:
#ifdef A
#define MY_INT int
#endif

MY_INT i;

The preprocessor should expand MY_INT i; as follows. Presence conditions are in brackets

int(A), MY_INT(~A), i(True), ;(True)

Note that the presence condition of tokens changes in the middle of a declaration. This is currently not supported by our tool.