13 stack usage analyzer detect parameters that can be const pointerreference const correctness#15
Merged
SizzleUnrlsd merged 11 commits intomainfrom Jan 29, 2026
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Const-correctness diagnostic for pointer/reference parameters
Summary
We added a static diagnostic that flags function parameters (pointers and references) that are never used to modify the pointed/referred object and can therefore be made
const. The diagnostic is emitted asConstParameterNotModifiedwith the standard human-readable severity prefix and sub-labels:[!][!!][!!!]The rule focuses on API const-correctness and is conservative to avoid false positives.
What was added
ConstParameterNotModifiedininclude/StackUsageAnalyzer.hpp.src/StackUsageAnalyzer.cppto detect read-only pointee/referent usage and emit a suggestion.test/pointer_reference-const_correctness/(includes rvalue reference case)..cppfiles (run_test.py).Detection logic (current behavior)
Scope
T**,T*&),void*, and function pointers (conservative).constat the pointee/referent level.What counts as modifying
A parameter is considered modifiable (no suggestion) if any of these are detected:
*p = ...,p[i] = ...).Interprocedural handling (conservative)
When the parameter is passed to a call:
const T*/const T&-> read-only.readnone/readonlyare honored.memcpy/memmove/memsettreat destination as write.O0 / debug-friendly pointer flow
Handles the common pattern where an argument is spilled to an
allocaand then reloaded:*p = 0).Type reconstruction
DILocalVariable/DISubroutineType).const,volatile,restrict) are preserved.constto the pointee/referent (not just the pointer variable).T * const paramproduces a message explaining pointer-const doesn’t protect the pointee and suggestsconst T *.Diagnostic format (human readable)
Example:
When the parameter is pointer-const only:
Rvalue reference example:
Files changed
include/StackUsageAnalyzer.hppConstParameterNotModifiedtoDescriptiveErrorCode.src/StackUsageAnalyzer.cpprun_test.py.cppfiles for test expectations.test/pointer_reference-const_correctness/readonly-pointer.creadonly-reference.cppconst-readonly.cconst-mixed.cadvanced-cases.cadvanced-cases.cppNotes / Known conservative behavior
volatilepointers currently still get a const suggestion (const volatile T*) if read-only.Possible follow-ups (optional)
volatilepointees (if you want to preserve MMIO patterns).printf,puts, etc.) to reduce false negatives.ConstParameterNotModified.Pointer,ConstParameterNotModified.Reference,ConstParameterNotModified.PointerConstOnly,ConstParameterNotModified.ReferenceRvaluePreferValue.