Skip to content

Commit 7d324c8

Browse files
committed
feat: Support C23/C++23 style elifdef and elifndef
1 parent 19a8518 commit 7d324c8

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

src/main/java/org/variantsync/diffdetective/feature/cpp/CPPDiffLineFormulaExtractor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
public class CPPDiffLineFormulaExtractor implements DiffLineFormulaExtractor {
2929
// ^[+-]?\s*#\s*(if|ifdef|ifndef|elif)(\s+(.*)|\((.*)\))$
30-
private static final String CPP_ANNOTATION_REGEX = "^[+-]?\\s*#\\s*(if|ifdef|ifndef|elif)([\\s(].*)$";
30+
private static final String CPP_ANNOTATION_REGEX = "^[+-]?\\s*#\\s*(if|ifdef|ifndef|elif|elifdef|elifndef)([\\s(].*)$";
3131
private static final Pattern CPP_ANNOTATION_PATTERN = Pattern.compile(CPP_ANNOTATION_REGEX);
3232

3333
/**
@@ -63,18 +63,18 @@ public Node extractFormula(final String line) throws UnparseableFormulaException
6363
throw new UnparseableFormulaException(e);
6464
}
6565

66-
// treat {@code #ifdef id} and {@code #ifndef id}
66+
// treat {@code #ifdef id}, {@code #ifndef id}, {@code #elifdef id} and {@code #elifndef id}
6767
// like {@code defined(id)} and {@code !defined(id)}
68-
if ("ifdef".equals(annotationType) || "ifndef".equals(annotationType)) {
68+
if (annotationType.endsWith("def")) {
6969
if (parsedFormula instanceof Literal literal) {
7070
literal.var = String.format("defined(%s)", literal.var);
7171

7272
// negate for ifndef
73-
if ("ifndef".equals(annotationType)) {
73+
if (annotationType.endsWith("ndef")) {
7474
literal.positive = false;
7575
}
7676
} else {
77-
throw new UnparseableFormulaException("When using #ifdef or #ifndef, only literals are allowed. Hence, \"" + line + "\" is disallowed.");
77+
throw new UnparseableFormulaException("When using #ifdef, #ifndef, #elifdef or #elifndef, only literals are allowed. Hence, \"" + line + "\" is disallowed.");
7878
}
7979
}
8080

src/test/java/CPPParserTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ private static List<TestCase> testCases() {
2525
new TestCase("#if A", var("A")),
2626
new TestCase("#ifdef A", var("defined(A)")),
2727
new TestCase("#ifndef A", negate(var("defined(A)"))),
28+
new TestCase("#elifdef A", var("defined(A)")),
29+
new TestCase("#elifndef A", negate(var("defined(A)"))),
2830
new TestCase("#elif A", var("A")),
2931

3032
new TestCase("#if !A", negate(var("A"))),

0 commit comments

Comments
 (0)