rustc_codegen_llvm/back/command_line_args.rs
1#[cfg(test)]
2mod tests;
3
4/// Joins command-line arguments into a single space-separated string, quoting
5/// and escaping individual arguments as necessary.
6///
7/// The result is intended to be informational, for embedding in debug metadata,
8/// and might not be properly quoted/escaped for actual command-line use.
9pub(crate) fn quote_command_line_args(args: &[String]) -> String {
10 // Start with a decent-sized buffer, since rustc invocations tend to be long.
11 let mut buf = String::with_capacity(128);
12
13 for arg in args {
14 if !buf.is_empty() {
15 buf.push(' ');
16 }
17
18 print_arg_quoted(&mut buf, arg);
19 }
20
21 buf
22}
23
24/// Equivalent to LLVM's `sys::printArg` with quoting always enabled
25/// (see llvm/lib/Support/Program.cpp).
26fn print_arg_quoted(buf: &mut String, arg: &str) {
27 buf.reserve(arg.len() + 2);
28
29 buf.push('"');
30 for ch in arg.chars() {
31 if matches!(ch, '"' | '\\' | '$') {
32 buf.push('\\');
33 }
34 buf.push(ch);
35 }
36 buf.push('"');
37}