Skip to content

Commit 1eac8e8

Browse files
Ignore unknown pragmas during lexing (#597)
Currently, the parser accepts `#pragma` directives that occur in declaration position. Elab ignores (with a warning) pragmas that are not global declarations. C2C ignores (with a warning) all remaining pragmas except the `section`, `use_section` and `reserve_register` pragmas handled in cfrontend/CPragmas.ml. However, `#pragma` directives can occur anywhere in a C source file, incl. in the middle of a statement or an expression. Some system header files includes such weird pragmas. This PR recognizes `#pragma` directives that are not handled in cfrontend/CPragmas.ml during lexical analysis and ignores them (with a warning). No `PRAGMA` token is generated for unhandled pragmas. This way, unhandled pragmas can occur anywhere in the source file and will not cause parsing errors. Co-authored-by: Bernhard Schommer <bernhardschommer@gmail.com>
1 parent 74cbdbf commit 1eac8e8

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎cfrontend/CPragmas.ml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,14 @@ let process_pragma name =
8787
| _ ->
8888
false
8989

90+
let supported_pragma name =
91+
match Tokenize.string name with
92+
| ("section" | "use_section" | "reserve_register") :: _ -> true
93+
| _ -> false
94+
9095
let reset () =
9196
reserved_registers := []
9297

9398
let initialize () =
99+
Lexer.supported_pragma := supported_pragma;
94100
C2C.process_pragma_hook := process_pragma

‎cparser/Lexer.mll‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ let () =
9999
(* We can ignore the __extension__ GCC keyword. *)
100100
ignored_keywords := SSet.add "__extension__" !ignored_keywords
101101

102+
let supported_pragma : (string -> bool) ref = ref (fun _ -> false)
103+
102104
let init_ctx = SSet.of_list (List.map fst CBuiltins.builtins.C.builtin_typedefs)
103105

104106
let types_context : SSet.t ref = ref init_ctx
@@ -528,7 +530,14 @@ and hash = parse
528530
"pragma"
529531
whitespace_char_no_newline +
530532
([^ '\n']* as s) '\n'
531-
{ new_line lexbuf; PRAGMA (s, currentLoc lexbuf) }
533+
{ if !supported_pragma s then begin
534+
new_line lexbuf;
535+
PRAGMA (s, currentLoc lexbuf)
536+
end else begin
537+
warning lexbuf Diagnostics.Unknown_pragmas "unknown pragma ignored";
538+
new_line lexbuf;
539+
initial_linebegin lexbuf
540+
end }
532541
| [^ '\n']* '\n'
533542
{ warning lexbuf Diagnostics.Unnamed "unrecognized '#' line";
534543
new_line lexbuf; initial_linebegin lexbuf }

0 commit comments

Comments
 (0)