<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/81685>81685</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Add bit width length modifier to printf
</td>
</tr>
<tr>
<th>Labels</th>
<td>
good first issue,
libc
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
michaelrj-google
</td>
</tr>
</table>
<pre>
Specified in C23 on page 349 of this PDF: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf
C23 adds a new length modifier to printf: w_N_ and wf_N_ which allow the user to specify an exact bit width for an integer conversion. To support this, LLVM-libc needs to add support to the printf parser and converter, as well as the internal data types. Below is a (hopefully complete) list of tasks that will add this support. Feel free to reach out if you need help, you can find me as michaelrj on discord, or you can just leave a comment on this bug.
### Code:
In [core_structs.h](https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/printf_core/core_structs.h):
- [ ] add `w` and `wf` to the `LengthModifier` enum
- [ ] add a member to `FormatSection` to hold the bit width (`size_t` would be an appropriate type)
- [ ] add both of these to the `operator==`.
In [parser.h](https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/printf_core/parser.h)
- [ ] add `w` and `wf` to the `parse_length_modifier` function. This may also involve changing it to return a struct that contains a `LengthModifier` and a number.
- [ ] add the new length modifiers to the switch statement in `get_next_section`
- [ ] add the new length modifiers to the switch statement in `get_type_desc`
In [converter_utils.h](https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/printf_core/converter_utils.h)
- [ ] add `w` and `wf` to `apply_length_modifier` (if you've already defined a struct that is a LengthModifier and a number, this would probably be another good place to use it).
- [ ] remember that the number of bits doesn't have to be a power of two, and can be any number (though you can assume it's small enough to fit in an `intmax_t`)
In [write_int_converter.h](https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/printf_core/write_int_converter.h)
- [ ] add `w` and `wf` to the switch statement in `convert_write_int`
### Testing:
In [sprintf_test.cpp](https://github.com/llvm/llvm-project/blob/main/libc/test/src/stdio/sprintf_test.cpp)
- [ ] add tests for the length modifiers for the format specifiers that support them. This can be a new test, or added to the existing integer type tests.
In [PrintfMatcher.cpp](https://github.com/llvm/llvm-project/blob/main/libc/test/UnitTest/PrintfMatcher.cpp)
- [ ] Add support for printing the new length modifiers (feel free to ask for help with this, it can be fiddly)
In [parser_test.cpp](https://github.com/llvm/llvm-project/blob/main/libc/test/src/stdio/printf_core/parser_test.cpp)
- [ ] add a test case for each of the new length modifiers
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V11v47gO_TXKC1HDUb4f8tBpEeACMxcDTO99DWSLttWRJUOi62Z__YJykqZtutgdzBYokjRmyEPy8IhSMZraIW7F4otY3E9UT40P29aUjUIbHm9q72uLk8Lrw_ZHh6WpDGowDu7kDLyDTtUIs_kGfAXUmAjf73didgsNURfF7FbInZC7YRgy36G7iaQzH2ohd49UToXcxVJKNqin89FOyJ32ZRRy52b5Zpl1uhL5vchvx1cOq7SOoMDhABZdTQ20XjOwAOShC8ZRxRiG_X_3oJyGoeJPQ2PKBpS1fgBqEPo4_iCmtA6gHOCzKgkKQzAYTQ1UPvDXxhHWGKD07glDNN5l8OAh9l3nA6W8hbyDr1___-3GmqIEh6gj-1Zav5j5FHbEB50KHJ_hjW4JAztREQa0lt_ZmkMHpyxoRQro0GHM4AtyDoaLIOS68R1WvbUHKH3bWSQUcgPWREpNUfEnu1KcE_vVemzUEVYGO0QLVUBkhAFV2YDvCUwFB9-nVKBB2zE4_qJUDirjNLTIIM9UYTZoE0sfNJv6cLZ-7COBRfWEoBhji47YOsEo-jq7bLCQs_EP7rxGZtDFw_84EIsvpQ-4jxT6kmLWiMU9F-EV32pDTV9kpW-F3Fn7dHq76YJ_xJKE3BXWF0LuWmUcPzRFyWwM6ZW08ULuxk7tOZyQuzdR5eYNthuGBmJxn0oslvkglnnqL3-u-J8jA8Qy_5p4--1IW36Grm-vOVLQYluMTBXLfOdDq-gHlmS8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