Skip to content

Commit

Permalink
fix: Add support for c++23 elifdef and elifndef
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi committed Sep 20, 2023
1 parent bec4386 commit 94c0ec0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A compiler front end for the C++ language

This repository contains a _work in progress_ compiler front end for C++ 20.
This repository contains a _work in progress_ compiler front end for C++ 23.

## Install

Expand All @@ -25,7 +25,7 @@ https://robertoraggi.github.io/cplusplus/
On Linux, macOS and Windows:

```sh
# install the python packages required to run the unit tests
# install the python packages required to run the unit tests (optional)
pip install -r tests/unit_tests/requirements.txt

# configure the source code
Expand Down
25 changes: 25 additions & 0 deletions src/parser/cxx/preprocessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,30 @@ void Preprocessor::Private::expand(
setState(std::tuple(true, evaluating));
}
}
} else if (matchId(ts, "elifdef")) {
if (!evaluating) {
setState(std::tuple(true, false));
} else {
const Macro *macro = nullptr;
const auto value = lookupMacro(ts->head, macro);
if (value) {
setState(std::tuple(!evaluating, false));
} else {
setState(std::tuple(true, evaluating));
}
}
} else if (matchId(ts, "elifndef")) {
if (!evaluating) {
setState(std::tuple(true, false));
} else {
const Macro *macro = nullptr;
const auto value = lookupMacro(ts->head, macro);
if (!value) {
setState(std::tuple(!evaluating, false));
} else {
setState(std::tuple(true, evaluating));
}
}
} else if (matchId(ts, "else")) {
setState(std::tuple(!evaluating, false));
} else if (matchId(ts, "endif")) {
Expand Down Expand Up @@ -1231,6 +1255,7 @@ auto Preprocessor::Private::constantExpression(const TokList *ts) -> long {
}

auto Preprocessor::Private::conditionalExpression(const TokList *&ts) -> long {
if (!ts) return 0;
const auto value = binaryExpression(ts);
if (!match(ts, TokenKind::T_QUESTION)) return value;
const auto iftrue = conditionalExpression(ts);
Expand Down
41 changes: 41 additions & 0 deletions tests/unit_tests/preprocessor/elifdef_001.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// RUN: %cxx -E %s -o - | %filecheck %s

#define B

#ifdef A
int a_not_reachable;
#elifdef B
int b_reachable;
#elifdef C
int c_not_reachable;
#else
int else_not_reachable;
#endif

// CHECK: b_reachable;

#ifdef AA
int aa_not_reachable;
#elifndef BB
int bb_reachable;
#elifndef CC
int cc_not_reachable;
#elifdef DD
int dd_not_reachable;
#else
int else_not_reachable;
#endif

// CHECK: bb_reachable;

#ifdef AAA
int aaa_not_reachable;
#elifdef BBB
int bbb_not_reachable;
#elifdef CCC
int ccc_not_reachable;
#else
int else_reachable;
#endif

// CHECK: else_reachable;

0 comments on commit 94c0ec0

Please sign in to comment.