summaryrefslogtreecommitdiff
path: root/libc/src/stdio/printf_core
AgeCommit message (Collapse)Author
2025-11-10[libc] fwrite_unlocked: only return errno if an actual error occurred. (#167350)Sterling-Augustine
fwrite and friends don't modify errno if no error occurred. Therefore frite_unlocked's return value shouldn't be constructed from errno without checking if an error actually occurred. This fixes an error introduced by https://github.com/llvm/llvm-project/commit/9e2f73fe9052a4fbf382a06e30b2441c6d99fb7e
2025-11-05[libc] Add printf error handling (with fixes #2) (#166517)Marcell Leleszi
https://github.com/llvm/llvm-project/issues/159474 Another try of trying to land https://github.com/llvm/llvm-project/pull/166382 - Fix some leftover tests checking for specific errnos - Guard errno checking tests to not run on the GPU @michaelrj-google
2025-11-05[libc] Migrate ctype_utils to use char instead of int where applicable. ↵Alexey Samsonov
(#166225) Functions like isalpha / tolower can operate on chars internally. This allows us to get rid of unnecessary casts and open a way to creating wchar_t overloads with the same names (e.g. for isalpha), that would simplify templated code for conversion functions (see 315dfe5865962d8a3d60e21d1fffce5214fe54ef). Add the int->char converstion to public entrypoints implementation instead. We also need to introduce bounds check on the input argument values - these functions' behavior is unspecified if the argument is neither EOF nor fits in "unsigned char" range, but the tests we've had verified that they always return false for small negative values. To preserve this behavior, cover it explicitly.
2025-11-04Revert commit d8e5698 and 15b19c7 (#166498)Kewen Meng
2025-11-04[libc] Add printf error handling (with fixes) (#166382)Marcell Leleszi
https://github.com/llvm/llvm-project/issues/159474 Resubmitting https://github.com/llvm/llvm-project/pull/162876 with fixes as it broke some buildbots: - Fix comparisons of integer expressions of different signedness - Not check for specific errnos in tests, as they might not be available on all platforms
2025-11-03Revert "[libc] Add printf error handling" (#166232)Kewen Meng
2025-11-03[libc] Add printf error handling (#162876)Marcell Leleszi
[#159474](https://github.com/llvm/llvm-project/issues/159474) - All printf variants set errno and consistently return -1 on error, instead of returning various predefined error codes - Return value overflow handling is added
2025-09-18[libc][bazel] Add (v)asprintf targets and tests (#159476)Michael Jones
2025-09-12[libc] Change __builtin_memcpy to inline_memcpy. (#158345)lntue
2025-07-24[libc] Add missing libc.include.inttypes for targets including <inttypes.h>. ↵lntue
(#150345)
2025-06-11[libc] Move libc_errno.h to libc/src/__support and make ↵lntue
LIBC_ERRNO_MODE_SYSTEM to be header-only. (#143187) This is the first step in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2025-05-01[libc] Fix typo in uint128 type for PPC printf handling (#138157)Joseph Huber
2025-04-23[libc] Special case PPC double double for print (#136614)Joseph Huber
Summary: We use the storage class for `long double` in the printing implementations. We don't fully support the PPC double double type, which that maps to, but we can stub out just the support needed for the print interface to works. This required using the internal interface for storage type, but it should be good enough. Fixes: https://github.com/llvm/llvm-project/issues/136596
2025-04-17[libc] Fix incorrect unsigned comparison (#135595)Wu Yingcong
There is a problem with such unsigned comparison pattern: ``` if(unsigned_a - unsigned_b > 0) { /* only NOT go here when unsigned_a==unsigned_b */ } ``` When `unsigned_a` < `unsigned_b`, the result will still be `>0` due to underflow. This patch fixes two of the occurrences I found. Also remove two redundant `if` where its condition is guaranteed by outer `if`.
2025-03-13[libc] Fix non-templated uses of `printf_core::Writer` (#131149)Simon Tatham
Commit 598e882ee88a1e3 turned `Writer` into a template, and updated most of the call sites that use it. But not all. The alternative FP printf system in `float_dec_converter_limited.h` wasn't updated, and neither was `baremetal/printf.cpp` or `baremetal/vprintf.cpp`. This patch updates `float_dec_converter_limited.h` in the same way that the previous commit updated `float_dec_converter.h`: propagate the templatedness through everything in the header, so that anything using a `Writer` at all has a `write_mode` template parameter to pass on to it. `printf.cpp` and `vprintf.cpp` are updated in the same way that the previous commit updated `printf_core/vfprintf_internal.h`: the `WriteBuffer` has parameter `WriteMode::FLUSH_TO_STREAM`, and `Writer` picks it up implicitly from that.
2025-03-12[libc] Template the writing mode for the writer class (#111559)Joseph Huber
Summary: Currently we dispatch the writing mode off of a runtime enum passed in by the constructor. This causes very unfortunate codegen for the GPU targets where we get worst-case codegen because of the unused function pointer for `sprintf`. Instead, this patch moves all of this to a template so it can be masked out. This results in no dynamic stack and uses 60 VGPRs instead of 117. It also compiles about 5x as fast.
2025-03-10[libc] Add `-Wno-sign-conversion` & re-attempt `-Wconversion` (#129811)Vinay Deshmukh
Relates to https://github.com/llvm/llvm-project/issues/119281#issuecomment-2699470459
2025-03-05Revert "[libc] Enable -Wconversion for tests. (#127523)"Augie Fackler
This reverts commit 1e6e845d49a336e9da7ca6c576ec45c0b419b5f6 because it changed the 1st parameter of adjust() to be unsigned, but libc itself calls adjust() with a negative argument in align_backward() in op_generic.h.
2025-03-04[libc] Enable -Wconversion for tests. (#127523)Vinay Deshmukh
Relates to: #119281
2025-02-06[libc] Fix recently introduced integer-type warnings (#125864)Simon Tatham
These warnings all come up in code modified by one of my two recent commits c06d0ff806b72b1 and b53da77c505a2d3, and all relate to implicit integer type conversion. In a build of ours with strict compile options two of them became errors. Even without that problem, it's worth fixing them to reduce noise that might hide a more serious warning.
2025-02-04[libc][float_dec_converter_limited] Add missing LIBC_INLINE (#125655)Simon Tatham
This caused a build failure in check-libc introduced by commit b53da77c505a2d3.
2025-02-04[libc] Alternative algorithm for decimal FP printf (#123643)Simon Tatham
The existing options for bin→dec float conversion are all based on the Ryū algorithm, which generates 9 output digits at a time using a table lookup. For users who can't afford the space cost of the table, the table-lookup subroutine is replaced with one that computes the needed table entry on demand, but the algorithm is otherwise unmodified. The performance problem with computing table entries on demand is that now you need to calculate a power of 10 for each 9 digits you output. But if you're calculating a custom power of 10 anyway, it's easier to just compute one, and multiply the _whole_ mantissa by it. This patch adds a header file alongside `float_dec_converter.h`, which replaces the whole Ryū system instead of just the table-lookup routine, implementing this alternative simpler algorithm. The result is accurate enough to satisfy (minimally) the accuracy demands of IEEE 754-2019 even in 128-bit long double. The new float128 test cases demonstrate this by testing the cases closest to the 39-digit rounding boundary. In my tests of generating 39 output digits (the maximum number supported by this algorithm) this code is also both faster and smaller than the USE_DYADIC_FLOAT version of the existing Ryū code.
2024-12-03[libc] Change ctype to be encoding independent (#110574)Michael Jones
The previous implementation of the ctype functions assumed ASCII. This patch changes to a switch/case implementation that looks odd, but actually is easier for the compiler to understand and optimize.
2024-11-03[libc] Add proxy headers to handle memory allocation associated with the ↵Job Henandez Lara
header `#include <stdlib.h> (#114690) This finishes the work from https://github.com/llvm/llvm-project/pull/114453 by adding proxy headers for `malloc`, `realloc`, `free` and `aligned_alloc`.
2024-10-10[libc] Clean up 'vasprintf' implementation (#111761)Joseph Huber
Summary: This had some leftover references to the old namespace and didn't put restrict on it.
2024-10-02[libc] Enable dyadic float for float printf (#110765)Michael Jones
Dyadic floats were an existing option for float to string conversion, but it had become stale. This patch fixes it up as well as adding proper config options and test support. Due to the test changes this is a followup to #110759
2024-10-01[libc][stdio] Use proxy headers of stdio.h in src and test folders. (#110067)lntue
https://github.com/llvm/llvm-project/issues/60481
2024-09-25[libc] Fix %m on CPUs with float128 but no int128 (#110053)Michael Jones
This bug is caused by the BigInt implementation failing to initialize from errno. Explanation below, but the fix is a static cast to int. The bug only shows up on risc-v 32 because of a chain of type-oddities: 1) Errno is provided by a struct with an implicit cast to int. 2) The printf parser uses an int128 to store the value of a conversion on systems with long double greater than double. 3) On systems without native int128 support we use our own BigInt instead. These combine such that if both long double and int128 exist (e.g. on x86) there's no issue, errno is implicitly cast to int, which is extended to int128. If long double is double (e.g. on arm32) then int64 is used in the printf parser, the implicit cast works, and there's no issue. The only way this would come up is if the target has a proper long double type, but not int128, which is the case for at least the current risc-v 32 bot.
2024-09-19[libc] Add printf strerror conversion (%m) (#105891)Michael Jones
This patch adds the %m conversion to printf, which prints the strerror(errno). Explanation of why is below, this patch also updates the docs, tests, and build system to accomodate this. The standard for syslog in posix specifies it uses the same format as printf, but adds %m which prints the error message string for the current value of errno. For ease of implementation, it's standard practice for libc implementers to just add %m to printf instead of creating a separate parser for syslog.
2024-08-09[libc] Clean up remaining use of *_WIDTH macros in printf (#102679)Roland McGrath
The previous change missed the second spot doing the same thing. Bug: https://issues.fuchsia.dev/358196552
2024-08-09[libc] Fix use of cpp::numeric_limits<...>::digits (#102674)Roland McGrath
The previous change replaced INT_WIDTH with cpp::numberic_limits<int>::digits, but these don't have the same value. While INT_WIDTH == UINT_WIDTH, not so for ::digits, so use cpp::numberic_limits<unsigned int>::digits et al instead for the intended effects. Bug: https://issues.fuchsia.dev/358196552
2024-08-09[libc] Use cpp::numeric_limits in preference to C23 <limits.h> macros (#102665)Roland McGrath
This updates some code to consistently use cpp::numeric_limits, the src/__support polyfill for std::numeric_limits, rather than the C <limits.h> macros. This is in keeping with the general C++-oriented style in libc code, and also sidesteps issues about the new C23 *_WIDTH macros that the compiler-provided header does not define outside C23 mode. Bug: https://issues.fuchsia.dev/358196552
2024-08-02[libc] Fix printf handling of INT_MIN width (#101729)Michael Jones
Prevously, if INT_MIN was passed as a wildcard width to a printf conversion the parser would attempt to negate it to get the positive width (and set the left justify flag), but it would underflow and the width would be treated as 0. This patch corrects the issue by instead treating a width of INT_MIN as identical to -INT_MAX. Also includes docs changes to explain this behavior and adding b to the list of int conversions.
2024-08-01[libc] Fix 'vasprintf' not working in non-fullbuild modeJoseph Huber
2024-08-01[libc] Fix erroneous warning on GCC (#101520)Joseph Huber
2024-08-01[libc] Implement vasprintf and asprintf (#98824)Tsz Chan
[libc] Implement vasprintf and asprintf --------- Co-authored-by: Izaak Schroeder <izaak.schroeder@gmail.com>
2024-07-12[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)Petr Hosek
This is a part of #97655.
2024-07-12Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace ↵Mehdi Amini
declaration" (#98593) Reverts llvm/llvm-project#98075 bots are broken
2024-07-11[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)Petr Hosek
This is a part of #97655.
2024-07-11Reland: [libc] Move off_t and stdio macros to proxy hdrs (#98384)Michael Jones
reland of https://github.com/llvm/llvm-project/pull/98215 Additionally adds proxy headers for FILE and the fopencookie types The arm32 build has been failing due to redefinitions of the off_t type. This patch fixes this by moving off_t to a proper proxy header. To do this, it also moves stdio macros to a proxy header to hopefully avoid including this proxy header alongside this public stdio.h.
2024-06-18[libc] Remove unnecessary check in printf floats (#95841)Michael Jones
Fixes https://github.com/llvm/llvm-project/issues/95638 The check was `if(unsigned_num >= 0)` which will always be true. The intent was to check for zero, but the `for` loop inside the `if` was already doing that.
2024-06-13[llvm-project] Fix typo "seperate" (#95373)Jay Foad
2024-05-15Fix typo "indicies" (#92232)Jay Foad
2024-04-06[libc][NFC] Rename `UInt.h` to `big_int.h` and `UInt128.h` to `uint128.h` ↵Guillaume Chatelet
for consistency (#87808)
2024-04-02[libc] Move include so it covers the other filesJoseph Huber
Summary: This is more hacky, but I want to get the bot green before we work on a better solution.
2024-04-02[libc] Include 'config.h' from the printf structs for builtinsJoseph Huber
2024-04-01[libc] Include algorithm.h to parser.h (#87125)Caslyn Tonelli
This includes algorithm.h directly to provide the definition for `cpp:max` in parser.h. This will define `max(...)` in the libc namespace for build systems that pull in parser.h explicitly.
2024-03-29Add bit width length modifier to printf (#82461)Om Prakaash
Resolves #81685. This adds support for wN and wfN length modifiers in fprintf.
2024-03-20[libc][NFC] Move `Sign` type to separate header (#85930)Guillaume Chatelet
2024-03-19[libc] Make 'printf' converter output "(null)" instead of "null" (#85845)Joseph Huber
Summary: Currently we print `null` for the null pointer in a `%s` expression. Although it's not defined by the standard, other implementations choose to use `(null)` to indicate this. We also currently print `(nullptr)` so I think it's more consistent to use parens in both cases.