use crate::query::node_text;
use tree_sitter::{Node, Tree};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CStandard {
C99,
C11,
C23,
}
pub fn detect_min_c_standard(tree: &Tree, source: &[u8]) -> Option<CStandard> {
let mut best: Option<CStandard> = None;
crate::query::walk_preorder(tree.root_node(), |node| {
if let Some(found) = marker_standard(node, source) {
if best.is_none_or(|b| found > b) {
best = Some(found);
}
if best == Some(CStandard::C23) {
return crate::query::Walk::Stop;
}
}
crate::query::Walk::Continue
});
best
}
fn marker_standard(node: Node, source: &[u8]) -> Option<CStandard> {
match node.kind() {
"generic_expression" => Some(CStandard::C11),
"alignas_qualifier" => Some(CStandard::C11),
"alignof_expression" => {
let text = node_text(node, source);
(text.starts_with("_Alignof") || text.starts_with("alignof")).then_some(CStandard::C11)
}
"type_qualifier" => match node_text(node, source) {
"restrict" => Some(CStandard::C99),
"_Atomic" | "_Noreturn" | "noreturn" => Some(CStandard::C11),
"constexpr" => Some(CStandard::C23),
_ => None,
},
"initializer_pair" => {
let mut cursor = node.walk();
let has_designator = node
.children_by_field_name("designator", &mut cursor)
.any(|d| matches!(d.kind(), "field_designator" | "subscript_designator"));
has_designator.then_some(CStandard::C99)
}
"array_declarator" | "abstract_array_declarator" => {
let size = node.child_by_field_name("size")?;
(!size.is_named() && node_text(size, source) == "*").then_some(CStandard::C99)
}
"null" => (node_text(node, source) == "nullptr").then_some(CStandard::C23),
"attribute_declaration" => Some(CStandard::C23),
"preproc_call" => {
let directive = node.child_by_field_name("directive")?;
(node_text(directive, source) == "#embed").then_some(CStandard::C23)
}
"identifier" => matches!(node_text(node, source), "_Static_assert" | "static_assert")
.then_some(CStandard::C11),
_ => None,
}
}
#[cfg(test)]
#[cfg(feature = "lang-c")]
mod tests {
use super::*;
fn detect(source: &str) -> Option<CStandard> {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&tree_sitter_c::LANGUAGE.into())
.unwrap();
let tree = parser.parse(source, None).unwrap();
detect_min_c_standard(&tree, source.as_bytes())
}
#[test]
fn plain_c89_compatible_file_returns_none() {
let source = "int add(int a, int b) { int c; c = a + b; return c; }";
assert_eq!(detect(source), None);
}
#[test]
fn gnu_pre_standard_spellings_do_not_leak_into_the_signal() {
let source = r#"
__inline int f(__restrict__ int *p) { return *p; }
int g(void) { int x = (int) { 1 }; return __typeof__(x) { 0 }.dummy; }
"#;
assert_eq!(detect(source), None);
}
#[test]
fn stdbool_style_usage_alone_returns_none() {
let source = "bool flag(void) { bool b = true; return b || false; }";
assert_eq!(detect(source), None);
}
#[test]
fn bare_restrict_is_c99() {
let source = "void f(int *restrict p) { *p = 1; }";
assert_eq!(detect(source), Some(CStandard::C99));
}
#[test]
fn field_designated_initializer_is_c99() {
let source = "struct s { int a; int b; }; struct s x = { .a = 1, .b = 2 };";
assert_eq!(detect(source), Some(CStandard::C99));
}
#[test]
fn subscript_designated_initializer_is_c99() {
let source = "int a[3] = { [0] = 1, [2] = 3 };";
assert_eq!(detect(source), Some(CStandard::C99));
}
#[test]
fn unspecified_vla_size_in_prototype_is_c99() {
let source = "void f(int n, int a[*]);";
assert_eq!(detect(source), Some(CStandard::C99));
}
#[test]
fn generic_expression_is_c11() {
let source = "int f(int x) { return _Generic(x, int: 1, default: 0); }";
assert_eq!(detect(source), Some(CStandard::C11));
}
#[test]
fn alignas_qualifier_is_c11() {
let source = "_Alignas(16) int x;";
assert_eq!(detect(source), Some(CStandard::C11));
}
#[test]
fn alignof_expression_is_c11() {
let source = "int x = _Alignof(int);";
assert_eq!(detect(source), Some(CStandard::C11));
}
#[test]
fn gnu_alignof_spellings_do_not_count_as_c11() {
let source = "int x = __alignof__(int); int y = _alignof(int);";
assert_eq!(detect(source), None);
}
#[test]
fn atomic_qualifier_is_c11() {
let source = "_Atomic int counter;";
assert_eq!(detect(source), Some(CStandard::C11));
}
#[test]
fn noreturn_qualifier_is_c11() {
let source = "_Noreturn void die(void);";
assert_eq!(detect(source), Some(CStandard::C11));
}
#[test]
fn static_assert_identifier_is_c11() {
let source = "_Static_assert(sizeof(int) == 4, \"bad int size\");";
assert_eq!(detect(source), Some(CStandard::C11));
}
#[test]
fn nullptr_is_c23() {
let source = "int *p = nullptr;";
assert_eq!(detect(source), Some(CStandard::C23));
}
#[test]
fn constexpr_qualifier_is_c23() {
let source = "constexpr int limit = 100;";
assert_eq!(detect(source), Some(CStandard::C23));
}
#[test]
fn standard_attribute_is_c23() {
let source = "[[nodiscard]] int f(void);";
assert_eq!(detect(source), Some(CStandard::C23));
}
#[test]
fn embed_directive_is_c23() {
let source = "#embed \"data.bin\"\n";
assert_eq!(detect(source), Some(CStandard::C23));
}
#[test]
fn embed_directive_inside_initializer_braces_is_a_known_false_negative() {
let source = "unsigned char data[] = {\n#embed \"data.bin\"\n};";
assert_eq!(detect(source), None);
}
#[test]
fn known_limitation_macro_guarded_marker_is_still_counted() {
let source = r#"
#if __STDC_VERSION__ >= 201112L
int f(int x) { return _Generic(x, int: 1, default: 0); }
#endif
"#;
assert_eq!(detect(source), Some(CStandard::C11));
}
#[test]
fn combining_markers_never_reports_a_lower_standard_than_either_alone() {
let c99_only = "void f(int *restrict p) { *p = 1; }";
let c11_only = "_Atomic int counter;";
let combined = format!("{c99_only}\n{c11_only}");
assert_eq!(detect(c99_only), Some(CStandard::C99));
assert_eq!(detect(c11_only), Some(CStandard::C11));
assert_eq!(detect(&combined), Some(CStandard::C11));
}
}