Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#if A vs #ifdef A vs. #if defined A vs. #if defined(A) #154

Open
2 tasks
pmbittner opened this issue Jan 6, 2025 · 0 comments
Open
2 tasks

#if A vs #ifdef A vs. #if defined A vs. #if defined(A) #154

pmbittner opened this issue Jan 6, 2025 · 0 comments

Comments

@pmbittner
Copy link
Member

pmbittner commented Jan 6, 2025

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:

  1. #if A -> Formula: A, AnnotationType: if
  2. #ifdef A -> Formula: A, AnnotationType: if
  3. #if defined A -> Formula: DEFINED_A, AnnotationType: if
  4. #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:

  • Implement proposed fix or a reasonable alternative
  • Document design decisions in our JavaDoc at fitting places.
@pmbittner pmbittner changed the title #ifdef A vs. #if defined(A) #if A vs #ifdef A vs. #if defined A vs. #if defined(A) Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant