Repository navigation
C++: suggest specifiers for simple integer format mismatches - #23108
SkyCipherr wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The production-query change lacks the repository’s broader DCA evaluation to check for unexpected diagnostic regressions beyond the focused tests and MinGW database.
1 open issue
What changed in this PR
Adds format-specifier suggestions to the C++ cpp/wrong-type-format-argument query, partially addressing #15028 without changing mismatch detection.
Changes:
- Suggests matching specifiers for plain decimal integer conversions using promoted argument types.
- Adds Linux and Microsoft target-model tests covering suggestions and excluded cases.
- Updates existing expected diagnostics and adds a release note.
| File | Description |
|---|---|
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft/WrongTypeFormatArguments.expected |
Updates Microsoft diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft_no_wchar/WrongTypeFormatArguments.expected |
Updates no-wchar diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_unsigned_chars/WrongTypeFormatArguments.expected |
Updates unsigned-char diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/WrongTypeFormatArguments.expected |
Updates signed-char diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Buildless/WrongTypeFormatArguments.expected |
Updates buildless diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.qlref |
Connects Microsoft tests to the query. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.expected |
Records Microsoft suggestion expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/options |
Configures the Microsoft target model. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/integer-hints.c |
Adds Microsoft suggestion and exclusion cases. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.qlref |
Connects Linux tests to the query. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.expected |
Records Linux suggestion expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/options |
Configures the Linux test extractor. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/integer-hints.c |
Adds Linux suggestion and exclusion cases. |
cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql |
Appends suggestions for supported integer mismatches. |
cpp/ql/src/change-notes/2026-11-12-integer-format-suggestions.md |
Documents the diagnostic enhancement. |
Review effort: Balanced (auto)
| select arg, | ||
| "This format specifier for type '" + expected.getName() + "' does not match the argument type '" + | ||
| actual.getUnspecifiedType().getName() + "'." | ||
| actual.getUnspecifiedType().getName() + "'." + integerFormatSuggestion(ffc, n, arg, actual) |
|
This feels very hacky. I think a more principled approach would likely be to update |
|
Thanks @jketema. Following your suggestion, I've reworked this in 0dc2c48.
The API is deliberately limited to the existing scope: plain decimal Validation with CodeQL CLI 2.27.4:
These are local validation results. I have not run the full repository suite or refreshed upstream DCA for this commit; it still needs upstream CI/DCA. |
|
You misunderstood me: this will need to be properly integrated with what is in |
Use the existing length and signedness model for both conversion type inference and reverse specifier suggestions. Preserve narrow display types, target-specific extensions, and the existing suggestion scope. Add Linux and Microsoft regression coverage recorded against the prior model. All 26 printf library and format query tests pass; real-GCC messages and locations remain unchanged.
|
Thanks for clarifying. I misunderstood your earlier suggestion: the previous revision still maintained a separate type-to-specifier mapping, even though it was placed in I've reworked this in 30acf5c9 so that type inference and suggestions now share the existing model:
The query’s detection predicates and the supported suggestion scope remain unchanged. Validation with CodeQL CLI 2.27.4:
These are local results; I have not run the full repository suite or upstream DCA. |
|
We're moving in a direction where the code complexity is increasing and this is starting to become unmaintainable. Also |

Summary
Related to #15028.
Add matching format-specifier suggestions to existing
cpp/wrong-type-format-argumentalerts for simple decimal integer conversions. For example,%dwith anunsigned longargument on LP64 suggests%lu.The implementation shares the existing
Printf.qlltype model in both directions:getIntegralTypeForLengthcontains the existing length-to-type-family rules, including target-specific selection and extension lengths.getIntegralConversiondelegates to it;getIntegralDisplayTypealso shares it while retaining its distincth/hhdisplay types.getIntegerConversionTypecombines that relation with the existing signed/unsigned conversion-character rules.getConversionTypeuses it for integer conversions.getSuggestedIntegerConversionSpecifierreverses the same relation for candidate lengths and characters, matching the supplied type after default argument promotions through its canonical arithmetic type. There is no separate type-to-specifier table. Canonicalization handles explicitly signed types without equating distinct types that happen to have the same width.Suggestions remain limited to plain decimal
d,i, anduconversions with optionall/ll, and signed/unsignedint,long, andlong long. Signedistyle is preserved. Flags, width, precision, positional parameters, other conversions and unsupported types retain their existing messages. This does not close the full issue or change the query's existing handling of equal-width types.Validation
Local validation with CodeQL CLI 2.27.4, based on main at
fbf9fca5ce7c3b64c2f5d719ea0444577850cb4a:0dc2c48). After refactoring, all 108 conversion positions on each of the Linux and Microsoft target models produce identical conversion-family, display-type, argument-type and store-pointer-type results. Coverage includes all modeled integer lengths, signed/unsigned decimal and non-decimal conversions,%n, decorators, positional formats and non-integer controls.-std=c11 -Wformat=2 -Werror=format -c; both source files pass. Deliberately mismatched calls were not executed.git diff --checkpass.The full repository suite and upstream DCA were not run. These are local results; upstream CI/DCA and maintainer review remain outstanding.