From 0b4d4b0e4171ba170c0468fdcc014bcd16232802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 15 Jul 2022 02:48:00 +0200 Subject: Fix nested #if --- module/c/preprocessor2.scm | 3 ++- tests/test/cpp/preprocessor2.scm | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/module/c/preprocessor2.scm b/module/c/preprocessor2.scm index f18ca748..1c73187d 100644 --- a/module/c/preprocessor2.scm +++ b/module/c/preprocessor2.scm @@ -459,7 +459,7 @@ (typecheck environment cpp-environment?) ;; (typecheck tokens (list-of lexeme?)) - (-> (extend-environment environment defined-macro) + (-> (extend-environment environment (list defined-macro)) ;; no newlines in #if line ((unval resolve-token-stream 1) tokens) resolve-constant-expression @@ -630,6 +630,7 @@ ((in-conditional/inactive? environment) (case (string->symbol (identifier-token? (car line-tokens))) + ((ifdef if) (loop (enter-inactive-if environment) remaining-tokens)) ((else) (loop (flip-flop-if environment) remaining-tokens)) ((endif) (loop (leave-if environment) remaining-tokens)) ((elif) (loop (resolve-for-if diff --git a/tests/test/cpp/preprocessor2.scm b/tests/test/cpp/preprocessor2.scm index 71e0c1a0..27332894 100644 --- a/tests/test/cpp/preprocessor2.scm +++ b/tests/test/cpp/preprocessor2.scm @@ -641,8 +641,6 @@ (lex "\"stdio.h\"")))) ;; TODO #include is subject to macro expansion - ;; test with through resolve-header - (test-group "Actually including stuff" (call-with-tmp-header " @@ -1135,8 +1133,30 @@ _Pragma(\"not-called\") #endif")))) - (test-group "Nested conditions" - 'TODO) + (test-equal "Nested conditions" + (lex "a\n\nc") + (run " +#define X +#ifdef X +a +#ifdef Y +b +#endif +c +#endif +")) + + (test-equal + (lex "") + (run " +#ifdef X +a +#ifdef Y +b +#endif /* Y */ +c +#endif /* X */ +")) (test-group "Unexpected if ends" (test-error "#else outside if" -- cgit v1.2.3