use cpp_code_analysis::format::{ExtraFunction, FormatConfig, FormatKind, PieceKind};
use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
fn a_session(source: &str) -> (Session<MemoryFiles>, std::path::PathBuf) {
let files = MemoryFiles::new().with_file("/p/a.cpp", source);
let providers = SessionFiles::new(OpenDocuments::new(), files);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.load("/p/a.cpp");
(session, std::path::PathBuf::from("/p/a.cpp"))
}
fn formats(source: &str, config: &FormatConfig) -> Vec<String> {
let (session, path) = a_session(source);
let view = session.view_of_the_file(&path).expect("a view of the file");
session
.format_literals(&view, config)
.into_iter()
.map(|literal| {
let text = &source[literal.range.start_offset..literal.range.end_offset()];
let pieces: Vec<String> = literal
.pieces
.iter()
.map(|piece| {
format!(
"{}:{}",
match piece.kind {
PieceKind::Specifier => "specifier",
PieceKind::Argument => "argument",
PieceKind::Conversion => "conversion",
PieceKind::Escape => "escape",
PieceKind::DynamicWidth => "dynamic",
},
&text[piece.start..piece.end]
)
})
.collect();
format!(
"{} {text} [{}]",
match literal.kind {
FormatKind::Std => "std",
FormatKind::Printf => "printf",
FormatKind::Scanf => "scanf",
},
pieces.join(" ")
)
})
.collect()
}
fn everything() -> FormatConfig {
FormatConfig::default()
}
#[test]
fn a_qualified_format_call_is_read_out_of_an_initializer() {
let source = r#"
void f() {
std::string message = std::format("Hello, {}!", "world");
}
"#;
assert_eq!(
formats(source, &everything()),
vec![r#"std "Hello, {}!" [specifier:{}]"#]
);
}
#[test]
fn a_statement_that_begins_with_a_qualified_call_is_still_read() {
let source = r#"
void f() {
std::println("值 = {:.3f},宽度 {:{}}", 3.14159, 9);
}
"#;
assert_eq!(
formats(source, &everything()),
vec![
"std \"值 = {:.3f},宽度 {:{}}\" [specifier:{:.3 conversion:f specifier:} specifier:{: dynamic:{} specifier:}]"
]
);
}
#[test]
fn a_c_call_is_read_and_its_argument_position_is_the_library_s_own() {
let source = r#"
void f(int y, int w, char* s, double d, FILE* out) {
printf("%-8.2f|%*d|%1$s|%%", d, w, s);
fprintf(out, "%d", y);
snprintf(s, 64, "%.2f", d);
scanf("%3d %[a-z] %n", &y, s, &w);
sscanf(s, "%d", &y);
}
"#;
assert_eq!(
formats(source, &everything()),
vec![
r#"printf "%-8.2f|%*d|%1$s|%%" [specifier:%-8.2 conversion:f specifier:% argument:* conversion:d specifier:% argument:1$ conversion:s escape:%%]"#,
r#"printf "%d" [specifier:% conversion:d]"#,
r#"printf "%.2f" [specifier:%.2 conversion:f]"#,
r#"scanf "%3d %[a-z] %n" [specifier:%3 conversion:d specifier:% conversion:[a-z] specifier:% conversion:n]"#,
r#"scanf "%d" [specifier:% conversion:d]"#,
]
);
}
#[test]
fn an_argument_that_happens_to_be_a_string_is_not_a_format_string() {
let source = r#"
void f() {
printf("%s", "text");
fprintf(stderr, "%s", "text");
}
"#;
assert_eq!(
formats(source, &everything()),
vec![
r#"printf "%s" [specifier:% conversion:s]"#,
r#"printf "%s" [specifier:% conversion:s]"#,
]
);
}
#[test]
fn a_print_reads_its_format_string_with_or_without_a_stream() {
let source = r#"
void f(FILE* stream) {
std::print("a {} b", 1);
std::print(stream, "c {} d", 1);
std::print("e {}", "text");
}
"#;
assert_eq!(
formats(source, &everything()),
vec![
r#"std "a {} b" [specifier:{}]"#,
r#"std "c {} d" [specifier:{}]"#,
r#"std "e {}" [specifier:{}]"#,
]
);
}
#[test]
fn only_a_literal_that_is_a_whole_argument_is_read() {
let source = r#"
void f(char* a, int x) {
printf(a + "%d", x);
printf("%" PRIu64 "\n", x);
const char* bare = "%d";
}
"#;
assert_eq!(
formats(source, &everything()),
Vec::<String>::new(),
"none of the three is a format string of a call"
);
}
#[test]
fn a_name_that_is_not_one_of_the_library_s_is_left_alone() {
let source = r#"
namespace app {
std::string format(const char* text);
void logf(const char* text);
}
void f() {
app::format("{}");
app::logf("%d");
record("{}", 1);
std::setw("%d");
}
"#;
assert_eq!(formats(source, &everything()), Vec::<String>::new());
}
#[test]
fn an_unqualified_name_is_resolved_before_it_is_read() {
let source = r#"
namespace std {
template <class... Args>
std::string format(const char* text, Args... args);
}
using namespace std;
void f() {
format("{}", 1);
}
"#;
assert_eq!(
formats(source, &everything()),
vec![r#"std "{}" [specifier:{}]"#],
"the analysis resolved `format` into `std`"
);
}
#[test]
fn a_bare_format_outside_std_is_not_read() {
let source = r#"
namespace app {
std::string format(const char* text);
}
using namespace app;
void f() {
format("{}", 1);
}
"#;
assert_eq!(formats(source, &everything()), Vec::<String>::new());
}
#[test]
fn a_member_call_is_read_by_its_method_name() {
let source = r#"
struct Logger {
void printf(const char* text, ...);
void format(const char* text);
};
void f(Logger& log) {
log.printf("%d", 1);
log.format("{}");
}
"#;
assert_eq!(
formats(source, &everything()),
vec![r#"printf "%d" [specifier:% conversion:d]"#],
"the method is the library's name; `format` on an object is not `std::format`"
);
}
#[test]
fn the_fmt_library_is_read_by_its_own_names() {
let source = r#"
void f() {
fmt::print("{}\n", 1);
fmt::format("{:>8}", 1);
fmt::v11::format("{:d}", 1);
}
"#;
assert_eq!(
formats(source, &everything()),
vec![
r#"std "{}\n" [specifier:{}]"#,
r#"std "{:>8}" [specifier:{:>8}]"#,
r#"std "{:d}" [specifier:{: conversion:d specifier:}]"#,
]
);
}
#[test]
fn a_project_s_own_wrapper_is_read_only_when_it_is_listed() {
let source = r#"
namespace mylib {
void logf(const char* text, ...);
}
void f() {
mylib::logf("a=%d", 1);
}
"#;
assert_eq!(
formats(source, &everything()),
Vec::<String>::new(),
"nothing says `logf` is a format function"
);
let mut config = everything();
config.functions = vec![ExtraFunction {
name: "mylib::logf".to_string(),
syntax: FormatKind::Printf,
argument: 0,
}];
assert_eq!(
formats(source, &config),
vec![r#"printf "a=%d" [specifier:% conversion:d]"#],
"the project listed it"
);
}
#[test]
fn a_family_can_be_turned_off() {
let source = r#"
void f(int x) {
std::format("{}", x);
printf("%d", x);
scanf("%d", &x);
}
"#;
let mut config = everything();
config.printf = false;
assert_eq!(
formats(source, &config),
vec![
r#"std "{}" [specifier:{}]"#,
r#"scanf "%d" [specifier:% conversion:d]"#,
]
);
let off = FormatConfig {
std_format: false,
printf: false,
scanf: false,
functions: Vec::new(),
};
assert_eq!(formats(source, &off), Vec::<String>::new());
}
#[test]
fn a_file_with_no_format_string_answers_nothing() {
let source = "int f(int a, int b) { return a + b; }\n";
assert_eq!(formats(source, &everything()), Vec::<String>::new());
}
#[test]
fn every_range_is_inside_its_literal_and_its_file() {
let source = r#"
void f(int y, double d) {
printf("%-8.2f|%1$s|%%", d, "");
std::println("值 = {:.3f} {:{}}", d, 1);
}
"#;
let (session, path) = a_session(source);
let view = session.view_of_the_file(&path).expect("a view");
let found = session.format_literals(&view, &everything());
assert_eq!(found.len(), 2);
for literal in &found {
assert!(literal.range.end_offset() <= source.len());
let text = &source[literal.range.start_offset..literal.range.end_offset()];
assert!(text.starts_with('"') || text.starts_with('R'), "{text}");
let mut previous = 0usize;
for piece in &literal.pieces {
assert!(piece.start >= previous, "out of order in {text}");
assert!(piece.end <= text.len(), "past the literal in {text}");
assert!(piece.start < piece.end, "empty piece in {text}");
previous = piece.end;
}
}
}
#[test]
#[ignore = "a measurement: needs CPPLS_FILE"]
fn what_the_walk_costs() {
let Some(path) = std::env::var_os("CPPLS_FILE").map(std::path::PathBuf::from) else {
eprintln!("skipped: set CPPLS_FILE");
return;
};
let source = std::fs::read_to_string(&path).expect("the file is readable");
let (session, path) = a_session(&source);
let started = std::time::Instant::now();
let view = session.view_of_the_file(&path).expect("a view");
let view_time = started.elapsed();
let tokens = view.tree.get_tokens().len();
let literals = view
.tree
.get_tokens()
.iter()
.filter(|token| token.kind == cpp_parser::CppTokenKind::StringLiteral)
.count();
let started = std::time::Instant::now();
let rounds = 20;
let mut kept = 0usize;
for _ in 0..rounds {
kept = session
.format_literals(&view, &FormatConfig::default())
.len();
}
let each = started.elapsed() / rounds;
let started = std::time::Instant::now();
let names = session.classified_names(&view);
let classify_time = started.elapsed();
println!(
"PROBE {}: {} bytes, {tokens} token(s), {literals} string literal(s), {kept} format string(s) kept | \
{each:?} per format walk, {} name(s) in {classify_time:?}, view {view_time:?}",
path.display(),
source.len(),
names.len(),
);
}
#[test]
fn the_fmt_library_s_two_syntaxes_are_both_read() {
let source = r#"
void f(int n, char* buf) {
fmt::printf("%d\n", n);
fmt::fprintf(stderr, "%d\n", n);
fmt::sprintf("%d", n);
std::sprintf(buf, "%d", n);
fmt::format("{}", n);
fmt::print("{:>8}", n);
fmt::format_to(out, "{}", n);
}
"#;
assert_eq!(
formats(source, &everything()),
vec![
r#"printf "%d\n" [specifier:% conversion:d]"#,
r#"printf "%d\n" [specifier:% conversion:d]"#,
r#"printf "%d" [specifier:% conversion:d]"#,
r#"printf "%d" [specifier:% conversion:d]"#,
r#"std "{}" [specifier:{}]"#,
r#"std "{:>8}" [specifier:{:>8}]"#,
r#"std "{}" [specifier:{}]"#,
]
);
}