#if A
vs #ifdef A
vs. #if defined A
vs. #if defined(A)
#154
Labels
#if A
vs #ifdef A
vs. #if defined A
vs. #if defined(A)
#154
Our heuristic for C preprocessor macros uses some (boolean) abstraction to enable fast and reliable parsing with some sacrifices in accuracy. One such sacrifice is that our current parser does distinguish
#ifdef A
from#if defined(A)
syntactically and semantically. To improve our parser's accuracy, these two cases could be considered equivalent. As documented in the CPPParserTest:#if A
-> Formula:A
, AnnotationType:if
#ifdef A
-> Formula:A
, AnnotationType:if
#if defined A
-> Formula:DEFINED_A
, AnnotationType:if
#if defined(A)
-> Formula:DEFINED___LB__A__RB__
, AnnotationType:if
On the one hand, these all are syntactically different so it also makes sense to have syntactically different abstractions.
This is violated because our parser abstracts both 1 and 2 to
if
. We do not want to distinguish#if
from#ifdef
because both are conditional annotations. It could make sense to reflect this difference in the formula then? The other cases are fine because they have syntactically distinct abstract representations.On the other hand, semantically equal annotations should remain semantically equal. This is violated by all of the above. 2, 3, 4 are semantically equal but 1 is its own group. Yet 1 and 2 are parsed to a semantically equal representation and 3 and 4 are completely different because they have completely different formulas. Sacrificing semantic equivalence because of unsufficient syntactic analysis is exactly the trade-off of our abstract. That is fine. It should be consistent and documented though. So either - 2, 3, 4 should parse to a semantically equivalent abstract representation, which is different from the representation of 1, or all of the above should parse to different abstract representations for consistency.
Proposed fix: Change the parsing for case 2 in the above list to:
#ifdef A
-> Formula:A
, AnnotationType:if
. This would fix both of the problems mentioned above.This is open to discussion.
Tasks:
The text was updated successfully, but these errors were encountered: