Skip to content
Snippets Groups Projects
Commit 2e30fd71 authored by Andre Maroneze's avatar Andre Maroneze
Browse files

[Kernel] JCDB: improve parsing of quoted arguments

parent 6dd638de
No related branches found
No related tags found
No related merge requests found
...@@ -139,7 +139,9 @@ let split_command_args s = ...@@ -139,7 +139,9 @@ let split_command_args s =
never need quotes. *) never need quotes. *)
let quote_define_argument arg = Format.sprintf "%S" arg let quote_define_argument arg = Format.sprintf "%S" arg
(* Filters and normalize useful flags: -I, -D, -U, ... *) (* Filters and normalize useful flags: -I, -D, -U, ...
This includes removing extraneous double quotes
(when the first and last characters are both '"') *)
let filter_useful_flags ~requote option_list = let filter_useful_flags ~requote option_list =
let convert_define arg = let convert_define arg =
if requote then quote_define_argument arg else arg if requote then quote_define_argument arg else arg
...@@ -150,11 +152,20 @@ let filter_useful_flags ~requote option_list = ...@@ -150,11 +152,20 @@ let filter_useful_flags ~requote option_list =
| Define s -> s ^ convert_define suffix | Define s -> s ^ convert_define suffix
| Undefine s -> s ^ suffix | Undefine s -> s ^ suffix
in in
let remove_extraneous_quotes arg =
let len = String.length arg in
if len = 0 then arg
else
if String.get arg 0 = '"' && String.get arg (len-1) = '"' then
String.sub arg 1 (len-2)
else arg
in
(* we must process the arguments in-order, since several -D and -U may (* we must process the arguments in-order, since several -D and -U may
exist on the command line *) exist on the command line *)
(* prev is the prefix of the previous argument (if any) *) (* prev is the prefix of the previous argument (if any) *)
let _, res = let _, res =
List.fold_left (fun (prev, acc_res) arg -> List.fold_left (fun (prev, acc_res) arg ->
let arg = remove_extraneous_quotes arg in
match prev with match prev with
| None -> begin | None -> begin
match has_whitelisted_prefix arg with match has_whitelisted_prefix arg with
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
"file": "jcdb.c" "file": "jcdb.c"
}, },
{ "directory": ".", { "directory": ".",
"command": "/usr/bin/clang -D'MSG=\"a \\\" \\\"b\"' -D'SINGLE_DOUBLE(a)=\"a \\\"with spaces and tab \"' -DSOMEDEF=\"With spaces, quotes and \\-es.\" -D\"DOUBLE_SINGLE(a)=a \\\"macro with spaces and non-escaped \\\\'\\\"\" -DEMPTY='' -DEMPTY2= -DTEST=42 -D'MACRO_FOR_INCR(s)=s+1' -DTOUNDEF -UTOUNDEF", "command": "/usr/bin/clang -D'MSG=\"a \\\" \\\"b\"' -D'SINGLE_DOUBLE(a)=\"a \\\"with spaces and tab \"' -DSOMEDEF=\"With spaces, quotes and \\-es.\" -D\"DOUBLE_SINGLE(a)=a \\\"macro with spaces and non-escaped \\\\'\\\"\" -DEMPTY='' -DEMPTY2= -DTEST=42 \"-DTEST2=43\" -D'MACRO_FOR_INCR(s)=s+1' -DTOUNDEF -UTOUNDEF",
"file": "jcdb.c" "file": "jcdb.c"
} }
] ]
...@@ -22,4 +22,4 @@ int main () { ...@@ -22,4 +22,4 @@ int main () {
#ifndef __FRAMAC__ #ifndef __FRAMAC__
printf("%s\n", s); // for GCC debugging printf("%s\n", s); // for GCC debugging
#endif #endif
return MACRO_FOR_INCR(TEST); } return MACRO_FOR_INCR(TEST) - TEST2; }
...@@ -13,7 +13,7 @@ int main(void) ...@@ -13,7 +13,7 @@ int main(void)
{ {
int __retres; int __retres;
char *s = (char *)"a macro with spaces and non-escaped \'"; char *s = (char *)"a macro with spaces and non-escaped \'";
__retres = 42 + 1; __retres = (42 + 1) - 43;
return __retres; return __retres;
} }
......
...@@ -9,7 +9,7 @@ int main(void) ...@@ -9,7 +9,7 @@ int main(void)
{ {
int __retres; int __retres;
char *s = (char *)"a macro with spaces and non-escaped \'"; char *s = (char *)"a macro with spaces and non-escaped \'";
__retres = 42 + 1; __retres = (42 + 1) - 43;
return __retres; return __retres;
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
"-DEMPTY=", "-DEMPTY=",
"-DEMPTY2=", "-DEMPTY2=",
"-DTEST=42", "-DTEST=42",
"\"-DTEST2=43\"",
"-DMACRO_FOR_INCR(s)=s+1", "-DMACRO_FOR_INCR(s)=s+1",
"-DSINGLE_DOUBLE(a)=\"a \\\"with spaces and\ttab \"", "-DSINGLE_DOUBLE(a)=\"a \\\"with spaces and\ttab \"",
"-DMSG=\"a \\\" \\\"b\"", "-DMSG=\"a \\\" \\\"b\"",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment