Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aarch64/TargetPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ module Target(System: SYSTEM): TARGET =
| EF_annot(kind,txt, targs) ->
begin match (P.to_int kind) with
| 1 -> let annot = annot_text preg_annot "sp" txt args in
fprintf oc "%s annotation: %S\n" comment annot
fprintf oc "%s annotation: %s\n" comment (quote_string annot)
| 2 -> let lbl = new_label () in
fprintf oc "%a:\n" label lbl;
add_ais_annot lbl preg_annot "sp" txt args
Expand Down
2 changes: 1 addition & 1 deletion arm/TargetPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ struct
| EF_annot(kind,txt, targs) ->
begin match (P.to_int kind) with
| 1 -> let annot = annot_text preg_annot "sp" txt args in
fprintf oc "%s annotation: %S\n" comment annot
fprintf oc "%s annotation: %s\n" comment (quote_string annot)
| 2 -> let lbl = new_label () in
fprintf oc "%a:\n" label lbl;
AisAnnot.add_ais_annot lbl preg_annot "r13" txt args
Expand Down
5 changes: 3 additions & 2 deletions backend/Fileinfo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
(* *********************************************************************)

open Printf
open PrintAsmaux

(* Printing annotations in asm syntax *)

Expand Down Expand Up @@ -47,7 +48,7 @@ let print_file oc file =
Hashtbl.find filename_info file
with Not_found ->
let (filenum, filebuf as res) = enter_filename file in
fprintf oc " .file %d %S\n" filenum file;
fprintf oc " .file %d %s\n" filenum (quote_string file);
res

let print_file_line oc pref file line =
Expand All @@ -70,7 +71,7 @@ let print_file_line_d2 oc pref file line =
with Not_found ->
enter_filename file in
if file <> !last_file then begin
fprintf oc " .d2file %S\n" file;
fprintf oc " .d2file %s\n" (quote_string file);
last_file := file
end;
fprintf oc " .d2line %d\n" line;
Expand Down
2 changes: 1 addition & 1 deletion backend/PrintAsm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ module Printer(Target:TARGET) =
print_addr oc Target.label lbl
| AisAnnot.Symbol symb ->
print_addr oc Target.symbol symb
| AisAnnot.String a -> fprintf oc " .ascii %S\n" a in
| AisAnnot.String a -> fprintf oc " .ascii %s\n" (quote_string a) in
let annot oc str =
List.iter (annot_part oc) str
in
Expand Down
23 changes: 22 additions & 1 deletion backend/PrintAsmaux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,27 @@ let print_inline_asm print_preg oc txt sg args res =
List.iter print_fragment (Str.full_split re_asm_param_1 txt);
fprintf oc "\n"

(** Quote the given string, using C string literal syntax.
The resulting string is enclosed in double quotes.
Double quotes and backslash are backslash-escaped.
Newline and tabs are written as [\n] and [\t].
Other control characters are octal-encoded. *)

let quote_string s =
let b = Buffer.create (String.length s + 10) in
Buffer.add_char b '"';
String.iter
(fun c ->
match c with
| '"' | '\\' -> Buffer.add_char b '\\'; Buffer.add_char b c
| '\n' -> Buffer.add_string b "\\n"
| '\t' -> Buffer.add_string b "\\t"
| '\x00' .. '\x1F' -> Printf.bprintf b "\\%03o" (Char.code c)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about quoting at least newline and tab as "\n" and "\t" instead of octal for readability?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's do that. Commit fcda834.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks!

| _ -> Buffer.add_char b c)
s;
Buffer.add_char b '"';
Buffer.contents b

(** This is [String.exists] in OCaml 4.13 and up. Temporarily included here
to support older OCaml versions. *)

Expand All @@ -308,7 +329,7 @@ let quote_argument s =
if string_exists
(function '\x00'..'\x1F' | ' ' | '"' -> true | _ -> false)
s
then "\"" ^ String.escaped s ^ "\""
then quote_string s
else s

(** Print CompCert version and command-line as asm comment *)
Expand Down
8 changes: 4 additions & 4 deletions debug/DwarfPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ module DwarfPrinter(Target: DWARF_TARGET):

let print_string oc c = function
| Simple_string s ->
fprintf oc " .asciz %S%a\n" s print_comment c
fprintf oc " .asciz %s%a\n" (quote_string s) print_comment c
| Offset_string (o,s) ->
let c = sprintf "%s %s" c s in
print_loc_ref oc c o
let c = sprintf "%s %s" c (quote_string s) in
print_loc_ref oc c o

let print_uleb128 oc c d =
fprintf oc " .uleb128 %d%a\n" d print_comment c
Expand Down Expand Up @@ -693,7 +693,7 @@ module DwarfPrinter(Target: DWARF_TARGET):
let s = List.sort (fun (a,_) (b,_) -> compare a b) entries.string_table in
List.iter (fun (id,s) ->
print_label oc (loc_to_label id);
fprintf oc " .asciz %S\n" s) s)
fprintf oc " .asciz %s\n" (quote_string s)) s)


(* Print the debug info and abbrev section *)
Expand Down
2 changes: 1 addition & 1 deletion powerpc/TargetPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ module Target (System : SYSTEM):TARGET =
| EF_annot(kind,txt, targs) ->
begin match (P.to_int kind) with
| 1 -> let annot = annot_text preg_annot "sp" txt args in
fprintf oc "%s annotation: %S\n" comment annot
fprintf oc "%s annotation: %s\n" comment (quote_string annot)

| 2 -> let lbl = new_label () in
fprintf oc "%a:\n" label lbl;
Expand Down
2 changes: 1 addition & 1 deletion riscV/TargetPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ module Target : TARGET =
| EF_annot(kind,txt, targs) ->
begin match (P.to_int kind) with
| 1 -> let annot = annot_text preg_annot "x2" txt args in
fprintf oc "%s annotation: %S\n" comment annot
fprintf oc "%s annotation: %s\n" comment (quote_string annot)
| 2 -> let lbl = new_label () in
fprintf oc "%a:\n" label lbl;
add_ais_annot lbl preg_annot "x2" txt args
Expand Down
2 changes: 1 addition & 1 deletion x86/TargetPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ module Target(System: SYSTEM):TARGET =
| EF_annot(kind,txt, targs) ->
begin match (P.to_int kind) with
| 1 -> let annot = annot_text preg_annot "esp" txt args in
fprintf oc "%s annotation: %S\n" comment annot
fprintf oc "%s annotation: %s\n" comment (quote_string annot)
| 2 -> let lbl = new_label () in
fprintf oc "%a:\n" label lbl;
let sp = if Archi.ptr64 then "rsp" else "esp" in
Expand Down
Loading