[all-commits] [llvm/llvm-project] 166209: Fix false match on nullPointerConstant for array i...
Kay Hicketts via All-commits
all-commits at lists.llvm.org
Tue Jun 30 10:57:29 PDT 2026
Branch: refs/heads/main
Home: https://github.com/llvm/llvm-project
Commit: 166209c3b05c5e3c3eb930ae0e613b18c43797a9
https://github.com/llvm/llvm-project/commit/166209c3b05c5e3c3eb930ae0e613b18c43797a9
Author: Kay Hicketts <kay.hicketts at gmail.com>
Date: 2026-06-30 (Tue, 30 Jun 2026)
Changed paths:
M clang/docs/ReleaseNotes.md
M clang/include/clang/ASTMatchers/ASTMatchers.h
M clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Log Message:
-----------
Fix false match on nullPointerConstant for array indices (#201893)
# Problem Detail
In the code
```
int *p[1];
(void) p[0];
```
The AST tree for ```p[0]``` looks like
```
|-CStyleCastExpr 0xc0ae6f1d8 <line:3:3, col:13> 'void' <ToVoid>
| `-ArraySubscriptExpr 0xc0ae6f1a8 <col:10, col:13> 'int *' lvalue
| |-ImplicitCastExpr 0xc0ae6f190 <col:10> 'int **' <ArrayToPointerDecay>
| | `-DeclRefExpr 0xc0ae6f118 <col:10> 'int *[1]' lvalue Var 0xc0ae6f098 'p' 'int *[1]'
| `-IntegerLiteral 0xc0ae6f138 <col:12> 'int' 0
```
So the previous matcher was finding the integer literal and then seeing
that the parent was a pointer type and thus we were getting a positive
match for something that should be replaced by a null pointer, even
though the index is just an integer.
# Solution
In a real null pointer scenario in cpp
```int *sausage = 0;```
The AST is like
```
| `-VarDecl 0xc0ae6f218 <col:3, col:18> col:8 sausage 'int *' cinit
| `-ImplicitCastExpr 0xc0ae6f2a0 <col:18> 'int *' <NullToPointer>
| `-IntegerLiteral 0xc0ae6f280 <col:18> 'int' 0
```
In C it's the same but with the exception of explicit casts
e.g.
```char *beans = (char*)0;```
which are represented as
```
| `-DeclStmt 0xacee134e8 <line:11:3, col:25>
| `-VarDecl 0xacee13420 <col:3, col:24> col:9 beans 'char *' cinit
| `-CStyleCastExpr 0xacee134c0 <col:17, col:24> 'char *' <NullToPointer>
| `-IntegerLiteral 0xacee13488 <col:24> 'int' 0
```
So the new matcher looks for an IntegerLiteral with value 0 that has a
parent that does any kind of cast to a null pointer
I used AI tools to help me build and understand the code.
# Original Issue
clang-tidy tools replace 0 with nullptr in index references due to
misinterpreting index numbers as pointers
Fixes: https://github.com/llvm/llvm-project/issues/87008
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list