Kaynağa Gözat

Fixed position tracking when C preprocessor is enabled

Taddeus Kroes 12 yıl önce
ebeveyn
işleme
543db4149b
2 değiştirilmiş dosya ile 13 ekleme ve 6 silme
  1. 4 4
      lexer.mll
  2. 9 2
      phases/load.ml

+ 4 - 4
lexer.mll

@@ -28,10 +28,10 @@ rule token = parse
         let scan lnum fn =
         let scan lnum fn =
             let filename = String.sub fn 0 (String.length fn - 1) in
             let filename = String.sub fn 0 (String.length fn - 1) in
             let pos = lexbuf.lex_curr_p in
             let pos = lexbuf.lex_curr_p in
-            lexbuf.lex_curr_p <- {
-                pos with pos_fname = filename;
-                         pos_lnum = lnum
-            }
+            lexbuf.lex_curr_p <- { pos with
+                                   pos_fname = filename;
+                                   pos_lnum = lnum;
+                                   pos_bol = pos.pos_cnum }
         in
         in
         Scanf.sscanf marker "# %d \"%s" scan;
         Scanf.sscanf marker "# %d \"%s" scan;
         token lexbuf
         token lexbuf

+ 9 - 2
phases/load.ml

@@ -1,5 +1,12 @@
 open Ast
 open Ast
 
 
+(* Unix command to call for C preprocessor:
+ * -nostdinc        : don't include from C stdlib
+ * -C               : don't remove comments
+ * -traditional-cpp : don't remove excessive whitespaces, so that error
+ *                    messages preserve correct character locations *)
+let cpp_cmd = "cpp -nostdinc -C -traditional-cpp"
+
 let input_all ic =
 let input_all ic =
     let n = in_channel_length ic in
     let n = in_channel_length ic in
     let buf = String.create n in
     let buf = String.create n in
@@ -33,10 +40,10 @@ let phase ir =
         if args.cpp then
         if args.cpp then
             let cpp_out = match args.infile with
             let cpp_out = match args.infile with
                 | Some filename ->
                 | Some filename ->
-                    Unix.open_process_in ("cpp " ^ filename)
+                    Unix.open_process_in (cpp_cmd ^ " " ^ filename)
                 | None ->
                 | None ->
                     let content = input_buffered stdin bufsize in
                     let content = input_buffered stdin bufsize in
-                    let (cpp_out, cpp_in) = Unix.open_process "cpp" in
+                    let (cpp_out, cpp_in) = Unix.open_process cpp_cmd in
                     output_string cpp_in content;
                     output_string cpp_in content;
                     close_out cpp_in;
                     close_out cpp_in;
                     cpp_out
                     cpp_out