Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / src / stdio / scanf_core / core_structs.h
blob246f770e0cabed796b82e8ba708b17fd82d4d9b8
1 //===-- Core Structures for scanf ------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_CORE_STRUCTS_H
10 #define LLVM_LIBC_SRC_STDIO_SCANF_CORE_CORE_STRUCTS_H
12 #include "src/__support/CPP/bitset.h"
13 #include "src/__support/CPP/string_view.h"
14 #include "src/__support/FPUtil/FPBits.h"
16 #include <inttypes.h>
17 #include <stddef.h>
19 namespace LIBC_NAMESPACE {
20 namespace scanf_core {
22 // These length modifiers match the length modifiers in the format string, which
23 // is why they are formatted differently from the rest of the file.
24 enum class LengthModifier { hh, h, l, ll, j, z, t, L, NONE };
26 enum FormatFlags : uint8_t {
27 NONE = 0x00,
28 NO_WRITE = 0x01, // *
29 ALLOCATE = 0x02, // m
32 struct FormatSection {
33 bool has_conv;
35 cpp::string_view raw_string;
37 // Format Specifier Values
38 FormatFlags flags = FormatFlags::NONE;
39 LengthModifier length_modifier = LengthModifier::NONE;
40 int max_width = -1;
42 // output_ptr is nullptr if and only if the NO_WRITE flag is set.
43 void *output_ptr = nullptr;
45 char conv_name;
47 cpp::bitset<256> scan_set;
49 bool operator==(const FormatSection &other) {
50 if (has_conv != other.has_conv)
51 return false;
53 if (raw_string != other.raw_string)
54 return false;
56 if (has_conv) {
57 if (!((static_cast<uint8_t>(flags) ==
58 static_cast<uint8_t>(other.flags)) &&
59 (max_width == other.max_width) &&
60 (length_modifier == other.length_modifier) &&
61 (conv_name == other.conv_name)))
62 return false;
64 // If the pointers are used, then they should be equal. If the NO_WRITE
65 // flag is set or the conversion is %, then the pointers are not used.
66 // If the pointers are used and they are not equal, return false.
68 if (!(((flags & FormatFlags::NO_WRITE) != 0) || (conv_name == '%') ||
69 (output_ptr == other.output_ptr)))
70 return false;
72 if (conv_name == '[')
73 return scan_set == other.scan_set;
75 return true;
79 enum ErrorCodes : int {
80 // This is the value to be returned by conversions when no error has occurred.
81 READ_OK = 0,
82 // These are the scanf return values for when an error has occurred. They are
83 // all negative, and should be distinct.
84 FILE_READ_ERROR = -1,
85 FILE_STATUS_ERROR = -2,
86 MATCHING_FAILURE = -3,
87 ALLOCATION_FAILURE = -4,
89 } // namespace scanf_core
90 } // namespace LIBC_NAMESPACE
92 #endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_CORE_STRUCTS_H