visq / language-c

Source repository for https://hackage.haskell.org/package/language-c
http://visq.github.io/language-c/
Other
87 stars 45 forks source link

0-based line number directives generate errors. #74

Closed trofi closed 4 years ago

trofi commented 4 years ago

Initially I noticed the error as a c2hs failure on most projects when using c2hs against gcc-11.0.0 (development trunk).

Complete c2hs example:

$ cat a.chs
module M where
$ /usr/bin/c2hs '--cpp=x86_64-pc-linux-gnu-gcc-10.2.0' '--cppopts=-E'  a.chs
$ /usr/bin/c2hs '--cpp=x86_64-pc-linux-gnu-gcc-11.0.0' '--cppopts=-E'  a.chs
c2hs: C header contains errors:

a.i:1: (column 1) [ERROR]  >>> Lexical error !
  The character '#' does not fit here.

This seems to happen because gcc-11 slightly renumbered line numbers for synthetic entries:

$ diff -U0 <(gcc-10.2.0 -E -x c /dev/null) <(gcc-11.0.0 -E -x c /dev/null)
--- /dev/fd/63  2020-08-08 09:09:37.245505668 +0100
+++ /dev/fd/62  2020-08-08 09:09:37.245505668 +0100
@@ -1,4 +1,3 @@
-# 1 "/dev/null"
-# 1 "<built-in>"
-# 1 "<command-line>"
-# 31 "<command-line>"
+# 0 "/dev/null"
+# 0 "<built-in>"
+# 0 "<command-line>"
@@ -6 +5 @@
-# 32 "<command-line>" 2
+# 0 "<command-line>" 2

Note: # 1 "/dev/null" changed to # 0 "/dev/null".

I think the error is also seen when running language-c directly as:

$ ghci
Prelude> Language.C.parseC (Data.ByteString.Char8.pack "# 1 \"/dev/null\"\n") Language.C.nopos
*** Exception: No match in record selector posOffset

Prelude> Language.C.parseC (Data.ByteString.Char8.pack "# 0 \"/dev/null\"\n") Language.C.nopos
Left <no file>:: [ERROR]  >>> Syntax Error !
  Lexical error !
  The character '#' does not fit here.

Thanks!

trofi commented 4 years ago

Specific change in gcc happened at https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=6bf2ff0d52a90acdc54f18c75d1978f6b4de4609