Skip to content

Commit 7171fdf

Browse files
committed
修复C++标准库选项误报并核对Clang标准别名
1 parent 0f75346 commit 7171fdf

3 files changed

Lines changed: 61 additions & 3 deletions

File tree

‎crates/codeguard-adapters/src/c_family_compilation_entry.rs‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,28 @@ impl CFamilyCompilationEntry {
4141
}
4242
let standards: Vec<_> = args
4343
.iter()
44-
.filter(|a| a.starts_with("-std") || a.starts_with("--std"))
44+
.filter(|a| {
45+
matches!(a.as_str(), "-std" | "--std")
46+
|| a.starts_with("-std=")
47+
|| a.starts_with("--std=")
48+
})
4549
.collect();
4650
if standards.len() > 1 {
4751
return Some("compilation_database_standard_ambiguous");
4852
}
4953
match standards.first().map(|a| a.as_str()) {
5054
None => Some("compilation_database_standard_missing"),
51-
Some("-std=c11" | "-std=c++17") => None,
55+
Some("-std=c11" | "-std=c++17" | "--std=c11" | "--std=c++17") => None,
56+
Some("--std") => {
57+
let index = args
58+
.iter()
59+
.position(|a| a == "--std")
60+
.expect("已匹配原参数");
61+
match args.get(index + 1).map(String::as_str) {
62+
Some("c11" | "c++17") => None,
63+
_ => Some("compilation_database_standard_unsupported"),
64+
}
65+
}
5266
Some(_) => Some("compilation_database_standard_unsupported"),
5367
}
5468
}
@@ -144,6 +158,22 @@ mod tests {
144158
assert_eq!(entries[0].execution_context_blocker(), None);
145159
}
146160

161+
#[test]
162+
fn standard_library_selection_is_not_a_language_standard_conflict() {
163+
for arguments in [
164+
vec!["clang++", "-std=c++17", "-stdlib=libc++", "file.cpp"],
165+
vec!["clang++", "--std=c++17", "file.cpp"],
166+
vec!["clang++", "--std", "c++17", "file.cpp"],
167+
vec!["clang++", "-stdlib=libstdc++", "-std=c++17", "file.cpp"],
168+
] {
169+
let input =
170+
serde_json::json!([{"directory":"/build","file":"file.cpp","arguments":arguments}]);
171+
let entries =
172+
parse_c_family_compilation_database(&serde_json::to_vec(&input).unwrap()).unwrap();
173+
assert_eq!(entries[0].execution_context_blocker(), None);
174+
}
175+
}
176+
147177
#[test]
148178
fn malformed_or_command_only_entries_never_become_clean_scope() {
149179
for bytes in [

‎crates/codeguard-cli/tests/init_command_contract.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,3 +1742,29 @@ fn init_exposes_compilation_argument_blockers_without_running_compiler() {
17421742
assert!(!project.0.join(".codeguard").exists());
17431743
}
17441744
}
1745+
1746+
#[test]
1747+
fn init_does_not_confuse_standard_library_or_native_standard_aliases() {
1748+
let project = Project::new();
1749+
for arguments in [
1750+
vec!["clang++", "-std=c++17", "-stdlib=libc++", "main.cpp"],
1751+
vec!["clang++", "--std=c++17", "main.cpp"],
1752+
vec!["clang++", "--std", "c++17", "main.cpp"],
1753+
] {
1754+
fs::write(project.0.join("compile_commands.json"), serde_json::to_vec(&serde_json::json!([{"directory":"/build","file":"main.cpp","arguments":arguments}])).unwrap()).unwrap();
1755+
let (exit, report) = project.run(&["--format=json"]);
1756+
assert_eq!(exit, 0);
1757+
let checker = report["profile_summary"]["checkers"]
1758+
.as_array()
1759+
.unwrap()
1760+
.iter()
1761+
.find(|c| c["checker_id"] == "c_family.compilation_database")
1762+
.unwrap();
1763+
assert_eq!(
1764+
checker["reason"],
1765+
"compilation_database_arguments_observed_execution_context_unverified"
1766+
);
1767+
assert_eq!(checker["configuration"], "unknown");
1768+
assert_eq!(checker["execution"], "not_run");
1769+
}
1770+
}

‎tests/acceptance/c-family-compilation-database-init.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
公开init只读模式不创建工作区,配置状态unknown、execution为not_run;apply退出3保持partial,把稳定数据库SHA256写入project.json,输入文件不改动,不运行command字符串。command-only数据库阻塞观察完整性。数据库宏变化与删除更新画像,保留用户任务文字;不把参数观察视为标准确认、头文件解析或完整项目覆盖。
88

9-
当前测试源码SHA256:`d336dde9bd839e755b0c25dfdfe8eda1a97f99fd31aa1de38b80c511238150c1`。
9+
当前测试源码SHA256:`03679b171d8ef19322d4be8f45c040381a9ae93dc95cb1135117c0524c57c6fb`。
1010

1111
本轮使用CLI临时项目,没有运行原生编译器、clang-tidy或真实安装宿主,不授予生产资格。当前46 partial、26 wasm_candidate_only、292 not_integrated为公开计划中的364条路径;旧52 partial记录为纠正前历史检查点。
1212

1313
新增上下文诊断:响应文件、前端透传、插件、重复标准、缺标准、不支持标准6类参数反例通过。适配器3项测试通过;当前公开CLI定向4项默认/WASM各通过(WASM 2.00秒),严格Clippy通过。配置可读不等于执行上下文可用;未命中已知阻塞也不授权运行。
14+
15+
低误报修复:合法-stdlib选择曾被-std前缀匹配误识别为重复标准,新增测试先复现失败后修正。Apple Clang21实际stdin核验-std=c++17、--std=c++17、--std c++17均接受,-std c++17拒绝;仅接纳核对过的选项形式。适配器4项通过,默认初始化47项通过(2.47秒),WASM新增合法反例1项通过(1.82秒)。不扩大语言版本或项目执行资格。

0 commit comments

Comments
 (0)