vabridgers wrote:

Here's some documentation for the work done thus far. This was human written 
from an AI generated summary of the work (hope that's ok :) ). 

Please suggest how best to move forward. Thanks

# Scope Reduction Clang-tidy Checker

This checker identifies variables that can be declared in smaller scopes to 
improve code locality and readability. Claude Sonnet 4.5 was used to co-pilot 
the algorithm development and evolution of the LIT tests through iterative 
systems testing. The AI tool suggested LIT tests, and they were verified one by 
one. Human analysis was done for completeness, and human intervention was 
required to add LIT annotations. The algorithm was improved iteratively, and 
AST matchers were added as an optimization step after the cases were proven to 
be working as expected.

The detection capabilities include:
- **Basic scope reduction**: Variables declared in broad scopes but only used 
in more narrow scopes.
- **For-loop initialization opportunities**: Variables used in for-loop 
expressions.
- **Nested scope optimization**: Variables that can be moved to deeply nested 
blocks.

The checker detects patterns to reduce false positive reports for:
- **Unary operator modification**: Detects variable initialization and 
modification patterns
- **Switch body conditional initialization**: Prevents moving initialize 
variables into switch bodies
- **Simple assignment overwrites**: Detects variables overwritten patterns
- **Accumulator patterns**: Recognizes variable accumulator and modification 
patterns in loops
- **Multi-case switches**: Detects variables used across multiple switch cases
- **Try-catch contexts**: Excludes exception handling variables
- **Dependency relationships**: Prevents moving variables whose addresses are 
taken
- **Member function calls**: Excludes variables initialized with method calls
- **Nested function calls**: Enhanced AST matching for wrapped function calls
- **Complex initializations**: Handles builder patterns and chained calls

The analysis is implemented as follows:
- **AST Matchers**: Matches AST patterns for the algorithms used
- **AST-based detection**: Uses Clang's AST traversal
- **Parent-child scope verification**: Validates scope nesting relationships
- **Helper function utilities**: Reusable parent traversal logic

Diagnostics output has the following features:
- **Clear messages**: Specific variable and scope information
- **Usage notes**: Shows up to 3 usage locations with overflow indication
- **Scope suggestions**: Points to suggested declaration locations
- **Alternative suggestions**: For-loop initialization when applicable

Configuration is done automatically; analysis focuses on function-scope 
variables within function boundaries.

Testing and improvement were completed iteratively through the following 
process:
- Running the checker on a complete LLVM/Clang build of 6254 files, identifying 
true and false results.
- For specific improvements identified along each step, supporting LIT positive 
and negative LIT tests were added and the process continued.
- In summary, from a starting point of 50,750 results, the checker now detects 
2,175 results.
- A 95% reduction in false and non-useful results.

A table of the steps and the measure improvements follow. There were never 
crashes observed in any iterative run.

| Improvement | Results | Reduction |
|---|---|---|
|Baseline|50,750|N/A|
|for-range detection|31,352|19,398|
|loop improvements|17,097|14,255|
|accumulator pattern|16,535|562|
|accumulator pattern improvements|6,009|10,526|
|Misc false positive reduction|5,420|589|
|Class method usage detection|2,820|2,600|
|for-loop variable modification detection|2,241|579|
|Misc false positive reduction|2,175|66|

https://github.com/llvm/llvm-project/pull/175429
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to