aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-07-23 20:49:48 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-08-18 16:38:37 +0200
commitc6480bfdd5cef49aa30b9277620b194b4df7fe1e (patch)
tree435b67dc2e3bc2021dd0ecc0c644be169530d18f
parentMerge cpp-environment sub-modules into main module. (diff)
downloadcalp-c6480bfdd5cef49aa30b9277620b194b4df7fe1e.tar.gz
calp-c6480bfdd5cef49aa30b9277620b194b4df7fe1e.tar.xz
Add propper error messaging when pre-processor fails.
-rw-r--r--module/c/preprocessor2.scm74
1 files changed, 47 insertions, 27 deletions
diff --git a/module/c/preprocessor2.scm b/module/c/preprocessor2.scm
index 229b1ae9..f4451e84 100644
--- a/module/c/preprocessor2.scm
+++ b/module/c/preprocessor2.scm
@@ -3,6 +3,7 @@
:use-module (srfi srfi-71)
:use-module (srfi srfi-88)
+ :use-module (ice-9 format)
:use-module (c cpp-environment)
:use-module ((c eval2) :select (c-boolean->boolean))
:use-module ((c eval-basic) :select (eval-basic-c))
@@ -757,33 +758,52 @@
args)
#f))
- (cond ((null? tokens) (values environment '()))
- ((newline-token? (car tokens))
- (let ((environment (bump-line environment))
- (tokens* (drop-whitespace (cdr tokens))))
- (cond ((null? tokens*) (values environment '()))
- ((equal? "#" (punctuator-token? (car tokens*)))
- (let* ((line-tokens remaining-tokens (tokens-until-eol (cdr tokens*)))
- ;; drop whitespace after newline check to not "eat" the next newline token
- (line-tokens (drop-whitespace line-tokens)))
- (catch 'propagate
- (lambda () (handle-preprocessing-directive environment line-tokens remaining-tokens loop))
- (lambda (_ . args) (apply err args)))))
-
- ;; Line is not a pre-processing directive
- (else (let* ((preceding-tokens remaining-tokens (tokens-until-cpp-directive (cdr tokens)))
- (env* resolved-tokens (if (in-conditional/inactive? environment)
- (values environment '())
- (resolve-token-stream environment preceding-tokens))))
- (on-snd (append resolved-tokens
- ;; The initial newline is presreved here, for better output,
- ;; and to keep at least one whitespace token when there was one previously.
- ;; possibly also keep a newline for line-directives.
- (unless (null? remaining-tokens) (lex "\n"))
- (abort* (loop env* remaining-tokens)))))))))
-
- (else (err "Unexpected middle of line, (near ~s)"
- (unlex tokens))))))
+ (catch 'cpp-error
+ (lambda ()
+ (cond ((null? tokens) (values environment '()))
+ ((newline-token? (car tokens))
+ (let ((environment (bump-line environment))
+ (tokens* (drop-whitespace (cdr tokens))))
+ (cond ((null? tokens*) (values environment '()))
+ ((equal? "#" (punctuator-token? (car tokens*)))
+ (let* ((line-tokens remaining-tokens (tokens-until-eol (cdr tokens*)))
+ ;; drop whitespace after newline check to not "eat" the next newline token
+ (line-tokens (drop-whitespace line-tokens)))
+ (catch 'propagate
+ (lambda () (handle-preprocessing-directive environment line-tokens remaining-tokens loop))
+ (lambda (_ . args) (apply err args)))))
+
+ ;; Line is not a pre-processing directive
+ (else (let* ((preceding-tokens remaining-tokens (tokens-until-cpp-directive (cdr tokens)))
+ (env* resolved-tokens (if (in-conditional/inactive? environment)
+ (values environment '())
+ (resolve-token-stream environment preceding-tokens))))
+ (on-snd (append resolved-tokens
+ ;; The initial newline is presreved here, for better output,
+ ;; and to keep at least one whitespace token when there was one previously.
+ ;; possibly also keep a newline for line-directives.
+ (unless (null? remaining-tokens) (lex "\n"))
+ (abort* (loop env* remaining-tokens)))))))))
+
+ (else (err "Unexpected middle of line, (near ~s)"
+ (unlex tokens)))))
+ (lambda (_ proc fmt args rest)
+ (with-output-to-port (current-error-port)
+ (lambda ()
+ (format #t "cpp error in ~a~%" proc)
+ (format #t "~a:~a: error: ~?~%"
+ (current-file environment)
+ (current-line environment)
+ fmt args)
+ (let ((trace (drop-right ((@@ (c cpp-environment) cpp-file-stack) environment) 1)))
+ (unless (null? trace)
+ (for-each (lambda (file)
+ (format #t "Included from ~a:~a~%"
+ (car file) (cdr file)))
+ (cdr trace))))
+ ;; re-throw
+ (scm-error 'cpp-error proc fmt args rest)
+ ))))))