codehelion-backend-clang 0.7.0

Out-of-process C and C++ compiler helper for codehelion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
use clang::{Entity, EntityKind, EntityVisitResult, Type};
use codehelion_helper::ir::{
    Anchor, DirectPropagation, FallibleKind, SemanticConstruct, SemanticConstructKind,
};

use crate::types::category;

use super::Reading;

impl Reading<'_> {
    /// Read the parts both closed range-for rules need before they can say
    /// what the loop does: the anchored `std::vector` the loop walks, the
    /// element binding it writes, and the one statement its body is made of.
    ///
    /// Nothing comes back for a loop outside that shape — a range that is not
    /// one direct standard vector binding, a missing body, or a body holding
    /// anything other than a single statement — which leaves the loop outside
    /// both vocabularies before either has to look at the statement.
    fn plain_range_parts<'clang>(
        &mut self,
        entity: Entity<'clang>,
    ) -> Option<(Anchor, String, Entity<'clang>)> {
        if entity.get_kind() != EntityKind::ForRangeStmt {
            return None;
        }
        let (source, binding) = direct_range_bindings(entity)?;
        let source_anchor = direct_standard_vector_reference(entity, &source)
            .and_then(|source| self.anchor(source))?;
        let body = entity
            .get_children()
            .into_iter()
            .find(|child| child.get_kind() == EntityKind::CompoundStmt)?;
        let statements = body.get_children();
        let [statement] = statements.as_slice() else {
            return None;
        };
        Some((source_anchor, binding, *statement))
    }

    /// Record one closed range-for reading as the standard vector it reads and
    /// the single operation the loop performs on each element.
    fn record_plain_range(
        &mut self,
        source_anchor: Anchor,
        operation_anchor: Anchor,
        kind: SemanticConstructKind,
    ) {
        self.semantic_constructs.extend([
            SemanticConstruct {
                anchor: source_anchor,
                kind: SemanticConstructKind::Source,
                fallible_kind: None,
                direct_propagation: None,
                resource_kind: None,
            },
            SemanticConstruct {
                anchor: operation_anchor,
                kind,
                fallible_kind: None,
                direct_propagation: None,
                resource_kind: None,
            },
        ]);
    }

    /// Record the deliberately small C++ counterpart of a plain Rust
    /// `for value in input { output.push(value) }` collection loop.
    ///
    /// A range-for loop is accepted only when its written range is one direct
    /// `std::vector` binding and its body is exactly one direct
    /// `std::vector::push_back(binding)` call.  The compiler resolves both
    /// vectors and the selected method; the token check only proves that the
    /// sole argument is the range binding rather than a transformed expression.
    pub(super) fn remember_plain_range_collection(&mut self, entity: Entity<'_>) {
        let Some((source_anchor, binding, call)) = self.plain_range_parts(entity) else {
            return;
        };
        if call.get_kind() != EntityKind::CallExpr
            || !call.get_reference().is_some_and(is_standard_vector_push)
            || !direct_call_argument_is(call, &binding)
        {
            return;
        }
        let Some(collect_anchor) = self.anchor(call) else {
            return;
        };
        self.record_plain_range(
            source_anchor,
            collect_anchor,
            SemanticConstructKind::Collect,
        );
    }

    /// Record a direct numeric range-for accumulation as SOURCE/REDUCE.
    ///
    /// This is the exact C++ analogue of the Rust helper's closed loop rule:
    /// one standard vector binding, one statement, numeric accumulator, and
    /// the written loop binding as the untransformed right-hand side. It does
    /// not infer initialization, reduction identities, guards, or calls.
    pub(super) fn remember_plain_range_reduce(&mut self, entity: Entity<'_>) {
        let Some((source_anchor, binding, accumulation)) = self.plain_range_parts(entity) else {
            return;
        };
        if accumulation.get_kind() != EntityKind::CompoundAssignOperator
            || !direct_numeric_accumulation(accumulation, &binding)
        {
            return;
        }
        let Some(reduce_anchor) = self.anchor(accumulation) else {
            return;
        };
        self.record_plain_range(source_anchor, reduce_anchor, SemanticConstructKind::Reduce);
    }

    /// Keep an `if` that directly asks a standard fallible value whether it
    /// holds a value as one closed validation operation.
    ///
    /// The callee's declaration, rather than a method spelling, decides this.
    /// A project type with a similar method or conversion is not an `optional`,
    /// and a compound condition adds semantics this compact vocabulary does not
    /// claim to understand.
    pub(super) fn remember_fallible_validation(&mut self, entity: Entity<'_>) {
        if entity.get_kind() != EntityKind::IfStmt {
            return;
        }
        let children = entity.get_children();
        let Some(condition) = children.first().copied() else {
            return;
        };
        let fallible_kind = is_direct_standard_fallible_presence_check(condition)
            .or_else(|| direct_standard_fallible_early_return(&children, condition));
        let Some(fallible_kind) = fallible_kind else {
            return;
        };
        // The condition is the semantic operation. Anchoring the enclosing
        // `if` would miss a macro invocation inside it and report a spelling
        // range that does not identify the operation Clang resolved.
        let Some(anchor) = self.anchor(condition) else {
            return;
        };
        self.semantic_constructs.push(SemanticConstruct {
            anchor,
            kind: SemanticConstructKind::Validate,
            fallible_kind: Some(fallible_kind),
            direct_propagation: None,
            resource_kind: None,
        });
    }

    /// Keep the exact C++ `expected` identity adapter as closed error
    /// propagation evidence.
    ///
    /// The accepted form is intentionally narrower than an error-handling
    /// idiom: a project-written function must accept and return exactly the
    /// same standard `expected` type, have one parameter, and consist only of
    /// `return parameter;`. It forwards both the value and error alternative
    /// unchanged, which is the C++ counterpart of the registered Rust
    /// `Ok(value?)` adapter. Any inspection, conversion, construction, or
    /// additional statement is left outside this rule.
    pub(super) fn remember_expected_identity_propagation(&mut self, entity: Entity<'_>) {
        if entity.get_kind() != EntityKind::FunctionDecl {
            return;
        }
        let Some(result_type) = entity.get_result_type() else {
            return;
        };
        if !is_standard_expected_type(result_type) {
            return;
        }
        let Some(arguments) = entity.get_arguments() else {
            return;
        };
        let [argument] = arguments.as_slice() else {
            return;
        };
        let Some(argument_type) = argument.get_type() else {
            return;
        };
        if !is_standard_expected_type(argument_type)
            || result_type.get_canonical_type().get_display_name()
                != argument_type.get_canonical_type().get_display_name()
        {
            return;
        }
        let Some(argument_name) = argument.get_name() else {
            return;
        };
        let Some(body) = entity
            .get_children()
            .into_iter()
            .find(|child| child.get_kind() == EntityKind::CompoundStmt)
        else {
            return;
        };
        let statements = body.get_children();
        let [returned] = statements.as_slice() else {
            return;
        };
        if returned.get_kind() != EntityKind::ReturnStmt
            || direct_returned_name(*returned).as_deref() != Some(argument_name.as_str())
        {
            return;
        }
        let Some(anchor) = self.anchor(*returned) else {
            return;
        };
        self.semantic_constructs.push(SemanticConstruct {
            anchor,
            kind: SemanticConstructKind::PropagateError,
            // The SOG category is intentionally language-neutral: standard
            // `expected<T, E>` and Rust `Result<T, E>` both carry one success
            // and one error alternative. The rule still requires the direct
            // adapter form, so this coarse correspondence cannot generalize
            // arbitrary expected-using functions into Result matches.
            fallible_kind: Some(FallibleKind::Result),
            direct_propagation: Some(DirectPropagation::ResultAdapter),
            resource_kind: None,
        });
    }

    /// Record the smallest C++ RAII lifetime Clang can establish without
    /// control-flow reconstruction: one direct standard `lock_guard` or
    /// `unique_lock` binding
    /// in a function body and that body's closing scope. Nested scopes,
    /// multiple guards and project-defined lookalikes remain outside the
    /// restricted vocabulary.
    pub(super) fn remember_direct_lock_lifetime(&mut self, entity: Entity<'_>) {
        if entity.get_kind() != EntityKind::FunctionDecl {
            return;
        }
        let Some(body) = entity
            .get_children()
            .into_iter()
            .find(|child| child.get_kind() == EntityKind::CompoundStmt)
        else {
            return;
        };
        let acquisitions: Vec<_> = body
            .get_children()
            .into_iter()
            .filter(|statement| statement.get_kind() == EntityKind::DeclStmt)
            .flat_map(|statement| statement.get_children())
            .filter(|declaration| declaration.get_kind() == EntityKind::VarDecl)
            .filter(|declaration| declaration.get_type().is_some_and(is_standard_lock_type))
            .filter_map(|declaration| self.anchor(declaration))
            .collect();
        let [acquire] = acquisitions.as_slice() else {
            return;
        };
        let Some(release) = self.scope_end_anchor(body) else {
            return;
        };
        self.semantic_constructs.extend([
            SemanticConstruct {
                anchor: acquire.clone(),
                kind: SemanticConstructKind::AcquireResource,
                fallible_kind: None,
                direct_propagation: None,
                resource_kind: Some("lock".to_owned()),
            },
            SemanticConstruct {
                anchor: release,
                kind: SemanticConstructKind::ReleaseResource,
                fallible_kind: None,
                direct_propagation: None,
                resource_kind: Some("lock".to_owned()),
            },
        ]);
    }
}

/// A deliberately small semantic API vocabulary named only after Clang has
/// resolved the target into the standard library.
pub(super) fn standard_api_name(entity: Entity<'_>) -> Option<String> {
    if !crate::types::in_standard_namespace(entity) {
        return None;
    }
    let name = entity.get_name()?;
    matches!(
        name.as_str(),
        "begin"
            | "filter"
            | "copy_if"
            | "map"
            | "transform"
            | "fold"
            | "accumulate"
            | "collect"
            | "push_back"
            | "to_string"
            | "stoull"
    )
    .then(|| format!("std::{name}"))
}

/// Return the family of a resolved standard fallible presence check.
///
/// Both `has_value()` and the explicit `operator bool` conversion carry the
/// same presence fact. The declaration must still belong to the standard
/// optional or expected family, so a project conversion operator cannot enter the closed
/// semantic vocabulary by sharing either spelling.
fn standard_fallible_presence_method(method: Entity<'_>) -> Option<FallibleKind> {
    method
        .get_name()
        .filter(|name| matches!(name.as_str(), "has_value" | "operator bool"))?;
    method.get_semantic_parent().and_then(|parent| {
        // libc++ exposes this inherited method on `__optional_storage_base`,
        // whereas other standard libraries can expose it on `optional`
        // itself. In both cases the declaration is inside `std` and its
        // compiler-owned type name identifies it as the optional family.
        crate::types::in_standard_namespace(parent).then_some(())?;
        let name = parent.get_name()?.to_ascii_lowercase();
        if name.contains("optional") {
            Some(FallibleKind::Option)
        } else if name.contains("expected") {
            Some(FallibleKind::Result)
        } else {
            None
        }
    })
}

/// Return the family when the condition is exactly one resolved standard
/// optional/expected presence call.
fn is_direct_standard_fallible_presence_check(condition: Entity<'_>) -> Option<FallibleKind> {
    let mut current = condition;
    let call = loop {
        match current.get_kind() {
            EntityKind::CallExpr => break Some(current),
            // A macro that contributes only parentheses around a direct
            // standard call appears as one or more of these wrappers. They
            // carry no operation of their own, unlike a unary or binary
            // expression, so following exactly one child preserves the same
            // closed condition accepted for source-written code.
            EntityKind::UnexposedExpr | EntityKind::ParenExpr => {
                let mut children = current.get_children().into_iter();
                let Some(child) = children.next() else {
                    break None;
                };
                if children.next().is_some() {
                    break None;
                }
                current = child;
            }
            _ => break None,
        }
    };
    call.and_then(|call| call.get_reference())
        .map(|reference| reference.get_canonical_entity())
        .and_then(standard_fallible_presence_method)
}

/// Return the family for an exact C++ early return guard.
///
/// Only `if (!value.has_value()) return;` and its single-statement braced
/// spelling enter the vocabulary. The `!` expression must contain exactly one
/// direct standard presence check, the then branch must contain exactly a bare
/// return, and the `if` must have no else branch. This keeps the accepted form
/// equivalent to the Rust unit-return guard without inferring the meaning of
/// arbitrary control flow.
fn direct_standard_fallible_early_return(
    if_children: &[Entity<'_>],
    condition: Entity<'_>,
) -> Option<FallibleKind> {
    let [_, then_branch] = if_children else {
        return None;
    };
    if condition.get_kind() != EntityKind::UnaryOperator
        || condition
            .get_range()
            .map(written_tokens)
            .is_none_or(|tokens| {
                tokens
                    .first()
                    .is_none_or(|token| token.get_spelling() != "!")
            })
    {
        return None;
    }
    let mut condition_children = condition.get_children().into_iter();
    let operand = condition_children.next()?;
    if condition_children.next().is_some() || !then_branch_is_unit_return(*then_branch) {
        return None;
    }
    is_direct_standard_fallible_presence_check(operand)
}

/// Whether a C++ branch is precisely one bare `return;` statement.
fn then_branch_is_unit_return(branch: Entity<'_>) -> bool {
    let returned = match branch.get_kind() {
        EntityKind::ReturnStmt => branch,
        EntityKind::CompoundStmt => {
            let statements = branch.get_children();
            let [returned] = statements.as_slice() else {
                return false;
            };
            *returned
        }
        _ => return false,
    };
    returned.get_kind() == EntityKind::ReturnStmt && returned.get_children().is_empty()
}

/// Whether `ty` resolves to the standard `expected` family.
fn is_standard_expected_type(ty: Type<'_>) -> bool {
    ty.get_canonical_type()
        .get_declaration()
        .is_some_and(|declaration| {
            crate::types::in_standard_namespace(declaration)
                && declaration.get_name().as_deref() == Some("expected")
        })
}

/// Whether `ty` is one of the standard lexical RAII lock types this helper
/// supports.
fn is_standard_lock_type(ty: Type<'_>) -> bool {
    ty.get_canonical_type()
        .get_declaration()
        .is_some_and(|declaration| {
            crate::types::in_standard_namespace(declaration)
                && matches!(
                    declaration.get_name().as_deref(),
                    Some("lock_guard" | "unique_lock")
                )
        })
}

/// Return the sole declaration-reference name under an identity return.
///
/// A copied `expected` value normally has compiler-inserted construct and cast
/// cursors between the return statement and the written reference. Following
/// only single-child wrappers admits that representation while rejecting calls,
/// operators, and any expression with additional semantic operands.
fn direct_returned_name(returned: Entity<'_>) -> Option<String> {
    let mut current = returned.get_children().into_iter().next()?;
    loop {
        if current.get_kind() == EntityKind::DeclRefExpr {
            return current.get_name();
        }
        let mut children = current.get_children().into_iter();
        current = children.next()?;
        if children.next().is_some() {
            return None;
        }
    }
}

/// The tokens written across `range`, or none when there are none to read.
///
/// Every caller here reads a fixed shape out of the tokens of one construct,
/// so a range that is not a stretch of one file has nothing to say to them: it
/// reaches into no file at all, its two ends are in different files, or it
/// covers nothing. Tokenizing those would answer a question nobody asked, and
/// the empty answer they produce is the one libclang is least careful with.
fn written_tokens(range: clang::source::SourceRange<'_>) -> Vec<clang::token::Token<'_>> {
    let start = range.get_start().get_file_location();
    let end = range.get_end().get_file_location();
    let (Some(from), Some(to)) = (start.file, end.file) else {
        return Vec::new();
    };
    if from != to || end.offset <= start.offset {
        return Vec::new();
    }
    range.tokenize()
}

/// The direct source and element bindings written in one C++ range-for loop.
///
/// The cursor includes compiler-generated `__range`/`__begin` variables, so
/// user spelling is read from the loop tokens and the element binding is the
/// unique non-generated `VarDecl` in the loop's desugaring.
fn direct_range_bindings(loop_: Entity<'_>) -> Option<(String, String)> {
    let tokens = written_tokens(loop_.get_range()?);
    let tokens: Vec<_> = tokens
        .iter()
        .map(clang::token::Token::get_spelling)
        .collect();
    let colon = tokens.iter().position(|token| token == ":")?;
    let source = tokens.get(colon.checked_add(1)?)?.clone();
    if !is_plain_identifier(&source) || tokens.get(colon.checked_add(2)?) != Some(&")".to_owned()) {
        return None;
    }
    let bindings: Vec<_> = loop_
        .get_children()
        .into_iter()
        .filter(|child| child.get_kind() == EntityKind::VarDecl)
        .filter_map(|child| child.get_name())
        .filter(|name| !name.starts_with("__"))
        .collect();
    let [binding] = bindings.as_slice() else {
        return None;
    };
    (binding != &source).then(|| (source, binding.clone()))
}

/// Find the written range expression only if Clang resolved it as a standard
/// `std::vector` binding.  Range customization points and project containers
/// deliberately remain outside the first closed loop vocabulary.
fn direct_standard_vector_reference<'clang>(
    loop_: Entity<'clang>,
    source: &str,
) -> Option<Entity<'clang>> {
    let mut references = Vec::new();
    loop_.visit_children(|entity, _| {
        if entity.get_kind() == EntityKind::DeclRefExpr
            && entity.get_name().as_deref() == Some(source)
            && entity.get_type().is_some_and(is_standard_vector_type)
        {
            references.push(entity);
        }
        EntityVisitResult::Recurse
    });
    (references.len() == 1).then(|| references[0])
}

/// Whether a resolved expression has exactly the standard vector family.
fn is_standard_vector_type(ty: Type<'_>) -> bool {
    ty.get_canonical_type()
        .get_declaration()
        .is_some_and(|declaration| {
            crate::types::in_standard_namespace(declaration)
                && declaration.get_name().as_deref() == Some("vector")
        })
}

/// Whether a resolved call selected `std::vector::push_back`.
fn is_standard_vector_push(method: Entity<'_>) -> bool {
    method.get_name().as_deref() == Some("push_back")
        && method.get_semantic_parent().is_some_and(|parent| {
            crate::types::in_standard_namespace(parent)
                && parent.get_name().as_deref() == Some("vector")
        })
}

/// Whether a call's only written argument is the loop element unchanged.
fn direct_call_argument_is(call: Entity<'_>, binding: &str) -> bool {
    let Some(range) = call.get_range() else {
        return false;
    };
    let tokens = written_tokens(range);
    let tokens: Vec<_> = tokens
        .iter()
        .map(clang::token::Token::get_spelling)
        .collect();
    let Some(opening) = tokens.iter().rposition(|token| token == "(") else {
        return false;
    };
    let Some(argument) = opening.checked_add(1) else {
        return false;
    };
    let Some(closing) = opening.checked_add(2) else {
        return false;
    };
    let Some(after) = opening.checked_add(3) else {
        return false;
    };
    tokens.get(argument).is_some_and(|token| token == binding)
        && tokens.get(closing).is_some_and(|token| token == ")")
        && tokens.get(after).is_none()
}

/// Whether a statement is exactly `numeric_binding += loop_binding` or `*=`.
fn direct_numeric_accumulation(statement: Entity<'_>, binding: &str) -> bool {
    let Some(range) = statement.get_range() else {
        return false;
    };
    let tokens = written_tokens(range);
    let tokens: Vec<_> = tokens
        .iter()
        .map(clang::token::Token::get_spelling)
        .collect();
    let [accumulator, operator, value] = tokens.as_slice() else {
        return false;
    };
    if !is_plain_identifier(accumulator)
        || accumulator == binding
        || !matches!(operator.as_str(), "+=" | "*=")
        || value != binding
    {
        return false;
    }
    let mut references = Vec::new();
    statement.visit_children(|entity, _| {
        if entity.get_kind() == EntityKind::DeclRefExpr
            && entity.get_name().as_deref() == Some(accumulator)
            && entity.get_type().is_some_and(is_numeric_type)
        {
            references.push(entity);
        }
        EntityVisitResult::Recurse
    });
    references.len() == 1
}

/// Whether Clang resolved an expression as a number that a direct reduction
/// can accumulate without inventing conversion semantics.
fn is_numeric_type(ty: Type<'_>) -> bool {
    matches!(
        category(ty),
        codehelion_helper::ir::TypeCategory::Integer | codehelion_helper::ir::TypeCategory::Float
    )
}

/// A conservative source identifier, never a member expression, call, or
/// qualified name that would need a broader normalizer to interpret.
fn is_plain_identifier(name: &str) -> bool {
    let mut characters = name.chars();
    characters
        .next()
        .is_some_and(|character| character == '_' || character.is_ascii_alphabetic())
        && characters.all(|character| character == '_' || character.is_ascii_alphanumeric())
}