pragma.scm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. ;;; Guile-GCC --- Guile extension to GCC. -*- coding: utf-8 -*-
  2. ;;; Copyright (C) 2012 Ludovic Courtès
  3. ;;;
  4. ;;; This file is part of Guile-GCC.
  5. ;;;
  6. ;;; Guile-GCC is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; Guile-GCC is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Guile-GCC. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-pragma)
  19. #:use-module (gcc)
  20. #:use-module (gcc cpp)
  21. #:use-module (srfi srfi-11))
  22. ;; Test whether pragmas can be registered, and whether they can successfully
  23. ;; use the libcpp and c-family APIs.
  24. (define (register-pragmas)
  25. (register-c-pragma "guile" "lex"
  26. (lambda (cpp)
  27. (let-values (((tok1 type1 loc1)
  28. (c-lex-with-flags #:join-strings? #f))
  29. ((tok2 type2 loc2)
  30. (c-lex-with-flags #:join-strings? #f))
  31. ((type3)
  32. ;; XXX: Using `c-lex-with-flags' here
  33. ;; would confuse the C parser, because
  34. ;; it would miss a token.
  35. (token-type (peek-token cpp 0))))
  36. (if (and (= type1 CPP_STRING) (= type2 CPP_STRING)
  37. (= type3 CPP_PRAGMA_EOL)
  38. (= (tree-code tok1) STRING_CST)
  39. (= (tree-code tok2) STRING_CST))
  40. (begin
  41. (inform loc1 (string-constant-value tok1))
  42. (inform loc2 (string-constant-value tok2)))))))
  43. (register-c-pragma "guile" "goodbye"
  44. (lambda (cpp)
  45. (let ((fn (current-function-decl))
  46. (loc (token-source-location (peek-token cpp 0))))
  47. (inform loc "hello, world from ~a"
  48. (identifier-string (decl-name fn)))
  49. (let ((exit (built-in-decl BUILT_IN_EXIT)))
  50. (add-statement (tree loc (exit 0))))))))
  51. (register-callback "guile" PLUGIN_PRAGMAS register-pragmas)