Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:30

0001 //===- Format.h - Efficient printf-style formatting for streams -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements the format() function, which can be used with other
0010 // LLVM subsystems to provide printf-style formatting.  This gives all the power
0011 // and risk of printf.  This can be used like this (with raw_ostreams as an
0012 // example):
0013 //
0014 //    OS << "mynumber: " << format("%4.5f", 1234.412) << '\n';
0015 //
0016 // Or if you prefer:
0017 //
0018 //  OS << format("mynumber: %4.5f\n", 1234.412);
0019 //
0020 //===----------------------------------------------------------------------===//
0021 
0022 #ifndef LLVM_SUPPORT_FORMAT_H
0023 #define LLVM_SUPPORT_FORMAT_H
0024 
0025 #include "llvm/ADT/ArrayRef.h"
0026 #include "llvm/ADT/STLExtras.h"
0027 #include "llvm/ADT/StringRef.h"
0028 #include "llvm/Support/DataTypes.h"
0029 #include <cassert>
0030 #include <cstdio>
0031 #include <optional>
0032 #include <tuple>
0033 #include <utility>
0034 
0035 namespace llvm {
0036 
0037 /// This is a helper class used for handling formatted output.  It is the
0038 /// abstract base class of a templated derived class.
0039 class format_object_base {
0040 protected:
0041   const char *Fmt;
0042   ~format_object_base() = default; // Disallow polymorphic deletion.
0043   format_object_base(const format_object_base &) = default;
0044   virtual void home(); // Out of line virtual method.
0045 
0046   /// Call snprintf() for this object, on the given buffer and size.
0047   virtual int snprint(char *Buffer, unsigned BufferSize) const = 0;
0048 
0049 public:
0050   format_object_base(const char *fmt) : Fmt(fmt) {}
0051 
0052   /// Format the object into the specified buffer.  On success, this returns
0053   /// the length of the formatted string.  If the buffer is too small, this
0054   /// returns a length to retry with, which will be larger than BufferSize.
0055   unsigned print(char *Buffer, unsigned BufferSize) const {
0056     assert(BufferSize && "Invalid buffer size!");
0057 
0058     // Print the string, leaving room for the terminating null.
0059     int N = snprint(Buffer, BufferSize);
0060 
0061     // VC++ and old GlibC return negative on overflow, just double the size.
0062     if (N < 0)
0063       return BufferSize * 2;
0064 
0065     // Other implementations yield number of bytes needed, not including the
0066     // final '\0'.
0067     if (unsigned(N) >= BufferSize)
0068       return N + 1;
0069 
0070     // Otherwise N is the length of output (not including the final '\0').
0071     return N;
0072   }
0073 };
0074 
0075 /// These are templated helper classes used by the format function that
0076 /// capture the object to be formatted and the format string. When actually
0077 /// printed, this synthesizes the string into a temporary buffer provided and
0078 /// returns whether or not it is big enough.
0079 
0080 // Helper to validate that format() parameters are scalars or pointers.
0081 template <typename... Args> struct validate_format_parameters;
0082 template <typename Arg, typename... Args>
0083 struct validate_format_parameters<Arg, Args...> {
0084   static_assert(std::is_scalar_v<Arg>,
0085                 "format can't be used with non fundamental / non pointer type");
0086   validate_format_parameters() { validate_format_parameters<Args...>(); }
0087 };
0088 template <> struct validate_format_parameters<> {};
0089 
0090 template <typename... Ts>
0091 class format_object final : public format_object_base {
0092   std::tuple<Ts...> Vals;
0093 
0094   template <std::size_t... Is>
0095   int snprint_tuple(char *Buffer, unsigned BufferSize,
0096                     std::index_sequence<Is...>) const {
0097 #ifdef _MSC_VER
0098     return _snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
0099 #else
0100     return snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
0101 #endif
0102   }
0103 
0104 public:
0105   format_object(const char *fmt, const Ts &... vals)
0106       : format_object_base(fmt), Vals(vals...) {
0107     validate_format_parameters<Ts...>();
0108   }
0109 
0110   int snprint(char *Buffer, unsigned BufferSize) const override {
0111     return snprint_tuple(Buffer, BufferSize, std::index_sequence_for<Ts...>());
0112   }
0113 };
0114 
0115 /// These are helper functions used to produce formatted output.  They use
0116 /// template type deduction to construct the appropriate instance of the
0117 /// format_object class to simplify their construction.
0118 ///
0119 /// This is typically used like:
0120 /// \code
0121 ///   OS << format("%0.4f", myfloat) << '\n';
0122 /// \endcode
0123 
0124 template <typename... Ts>
0125 inline format_object<Ts...> format(const char *Fmt, const Ts &... Vals) {
0126   return format_object<Ts...>(Fmt, Vals...);
0127 }
0128 
0129 /// This is a helper class for left_justify, right_justify, and center_justify.
0130 class FormattedString {
0131 public:
0132   enum Justification { JustifyNone, JustifyLeft, JustifyRight, JustifyCenter };
0133   FormattedString(StringRef S, unsigned W, Justification J)
0134       : Str(S), Width(W), Justify(J) {}
0135 
0136 private:
0137   StringRef Str;
0138   unsigned Width;
0139   Justification Justify;
0140   friend class raw_ostream;
0141 };
0142 
0143 /// left_justify - append spaces after string so total output is
0144 /// \p Width characters.  If \p Str is larger that \p Width, full string
0145 /// is written with no padding.
0146 inline FormattedString left_justify(StringRef Str, unsigned Width) {
0147   return FormattedString(Str, Width, FormattedString::JustifyLeft);
0148 }
0149 
0150 /// right_justify - add spaces before string so total output is
0151 /// \p Width characters.  If \p Str is larger that \p Width, full string
0152 /// is written with no padding.
0153 inline FormattedString right_justify(StringRef Str, unsigned Width) {
0154   return FormattedString(Str, Width, FormattedString::JustifyRight);
0155 }
0156 
0157 /// center_justify - add spaces before and after string so total output is
0158 /// \p Width characters.  If \p Str is larger that \p Width, full string
0159 /// is written with no padding.
0160 inline FormattedString center_justify(StringRef Str, unsigned Width) {
0161   return FormattedString(Str, Width, FormattedString::JustifyCenter);
0162 }
0163 
0164 /// This is a helper class used for format_hex() and format_decimal().
0165 class FormattedNumber {
0166   uint64_t HexValue;
0167   int64_t DecValue;
0168   unsigned Width;
0169   bool Hex;
0170   bool Upper;
0171   bool HexPrefix;
0172   friend class raw_ostream;
0173 
0174 public:
0175   FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U,
0176                   bool Prefix)
0177       : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
0178         HexPrefix(Prefix) {}
0179 };
0180 
0181 /// format_hex - Output \p N as a fixed width hexadecimal. If number will not
0182 /// fit in width, full number is still printed.  Examples:
0183 ///   OS << format_hex(255, 4)              => 0xff
0184 ///   OS << format_hex(255, 4, true)        => 0xFF
0185 ///   OS << format_hex(255, 6)              => 0x00ff
0186 ///   OS << format_hex(255, 2)              => 0xff
0187 inline FormattedNumber format_hex(uint64_t N, unsigned Width,
0188                                   bool Upper = false) {
0189   assert(Width <= 18 && "hex width must be <= 18");
0190   return FormattedNumber(N, 0, Width, true, Upper, true);
0191 }
0192 
0193 /// format_hex_no_prefix - Output \p N as a fixed width hexadecimal. Does not
0194 /// prepend '0x' to the outputted string.  If number will not fit in width,
0195 /// full number is still printed.  Examples:
0196 ///   OS << format_hex_no_prefix(255, 2)              => ff
0197 ///   OS << format_hex_no_prefix(255, 2, true)        => FF
0198 ///   OS << format_hex_no_prefix(255, 4)              => 00ff
0199 ///   OS << format_hex_no_prefix(255, 1)              => ff
0200 inline FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width,
0201                                             bool Upper = false) {
0202   assert(Width <= 16 && "hex width must be <= 16");
0203   return FormattedNumber(N, 0, Width, true, Upper, false);
0204 }
0205 
0206 /// format_decimal - Output \p N as a right justified, fixed-width decimal. If
0207 /// number will not fit in width, full number is still printed.  Examples:
0208 ///   OS << format_decimal(0, 5)     => "    0"
0209 ///   OS << format_decimal(255, 5)   => "  255"
0210 ///   OS << format_decimal(-1, 3)    => " -1"
0211 ///   OS << format_decimal(12345, 3) => "12345"
0212 inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
0213   return FormattedNumber(0, N, Width, false, false, false);
0214 }
0215 
0216 class FormattedBytes {
0217   ArrayRef<uint8_t> Bytes;
0218 
0219   // If not std::nullopt, display offsets for each line relative to starting
0220   // value.
0221   std::optional<uint64_t> FirstByteOffset;
0222   uint32_t IndentLevel;  // Number of characters to indent each line.
0223   uint32_t NumPerLine;   // Number of bytes to show per line.
0224   uint8_t ByteGroupSize; // How many hex bytes are grouped without spaces
0225   bool Upper;            // Show offset and hex bytes as upper case.
0226   bool ASCII;            // Show the ASCII bytes for the hex bytes to the right.
0227   friend class raw_ostream;
0228 
0229 public:
0230   FormattedBytes(ArrayRef<uint8_t> B, uint32_t IL, std::optional<uint64_t> O,
0231                  uint32_t NPL, uint8_t BGS, bool U, bool A)
0232       : Bytes(B), FirstByteOffset(O), IndentLevel(IL), NumPerLine(NPL),
0233         ByteGroupSize(BGS), Upper(U), ASCII(A) {
0234 
0235     if (ByteGroupSize > NumPerLine)
0236       ByteGroupSize = NumPerLine;
0237   }
0238 };
0239 
0240 inline FormattedBytes
0241 format_bytes(ArrayRef<uint8_t> Bytes,
0242              std::optional<uint64_t> FirstByteOffset = std::nullopt,
0243              uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4,
0244              uint32_t IndentLevel = 0, bool Upper = false) {
0245   return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine,
0246                         ByteGroupSize, Upper, false);
0247 }
0248 
0249 inline FormattedBytes
0250 format_bytes_with_ascii(ArrayRef<uint8_t> Bytes,
0251                         std::optional<uint64_t> FirstByteOffset = std::nullopt,
0252                         uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4,
0253                         uint32_t IndentLevel = 0, bool Upper = false) {
0254   return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine,
0255                         ByteGroupSize, Upper, true);
0256 }
0257 
0258 } // end namespace llvm
0259 
0260 #endif