aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-07-15 02:48:00 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-07-15 02:48:00 +0200
commit0b4d4b0e4171ba170c0468fdcc014bcd16232802 (patch)
tree160de4d3bf985e6058c1c18ab9febca85a1a3460
parentwork (diff)
downloadcalp-0b4d4b0e4171ba170c0468fdcc014bcd16232802.tar.gz
calp-0b4d4b0e4171ba170c0468fdcc014bcd16232802.tar.xz
Fix nested #if
-rw-r--r--module/c/preprocessor2.scm3
-rw-r--r--tests/test/cpp/preprocessor2.scm28
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"