aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/uninit-pr105937.C
blob: 744817b1ba8558fbbe65f785213b42a214e20a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// { dg-do compile }
// { dg-require-effective-target c++17 }
// { dg-options "-O2 -Wall" }
// { dg-skip-if "requires hosted libstdc++ for cwchar" { ! hostedlib } }

#include <stdint.h>
#include <optional>
#include <string_view>

using utf8 = char;

enum
{
    FONT_SIZE_TINY = 2,
    FONT_SIZE_SMALL = 0,
    FONT_SIZE_MEDIUM = 1,
    FONT_SIZE_COUNT = 3
};

constexpr const uint16_t FONT_SPRITE_GLYPH_COUNT = 224;

enum class FontSpriteBase : int16_t
{
    MEDIUM_EXTRA_DARK = -2,
    MEDIUM_DARK = -1,

    TINY = FONT_SIZE_TINY * FONT_SPRITE_GLYPH_COUNT,
    SMALL = FONT_SIZE_SMALL * FONT_SPRITE_GLYPH_COUNT,
    MEDIUM = FONT_SIZE_MEDIUM * FONT_SPRITE_GLYPH_COUNT,
};

struct TTFSurface;

class CodepointView
{
private:
    std::string_view _str;

public:
    class iterator
    {
    private:
        std::string_view _str;
        size_t _index;

    public:
        iterator(std::string_view str, size_t index)
            : _str(str)
            , _index(index)
        {
        }

        bool operator==(const iterator& rhs) const
        {
            return _index == rhs._index;
        }
        bool operator!=(const iterator& rhs) const
        {
            return _index != rhs._index;
        }
        char32_t operator*() const
        {
            return GetNextCodepoint(&_str[_index], nullptr);
        }
        iterator& operator++()
        {
            return *this;
        }
        iterator operator++(int)
        {
            auto result = *this;
            if (_index < _str.size())
            {
                const utf8* nextch;
                GetNextCodepoint(&_str[_index], &nextch);
                _index = nextch - _str.data();
            }
            return result;
        }

        size_t GetIndex() const
        {
            return _index;
        }

        static char32_t GetNextCodepoint(const char* ch, const char** next);
    };

    CodepointView(std::string_view str)
        : _str(str)
    {
    }

    iterator begin() const
    {
        return iterator(_str, 0);
    }

    iterator end() const
    {
        return iterator(_str, _str.size());
    }
};

struct InternalTTFFont;
using TTF_Font = InternalTTFFont;
struct TTFFontDescriptor
{
    const utf8* filename;
    const utf8* font_name;
    int32_t ptSize;
    int32_t offset_x;
    int32_t offset_y;
    int32_t line_height;
    int32_t hinting_threshold;
    TTF_Font* font;
};
using codepoint_t = uint32_t;

#define abstract = 0

struct ITTF
{
    virtual ~ITTF() = default;
    virtual TTFFontDescriptor* ttf_get_font_from_sprite_base(FontSpriteBase spriteBase) abstract;
    virtual TTFSurface* ttf_surface_cache_get_or_add(TTF_Font* font, std::string_view text) abstract;
};

namespace OpenRCT2 {
    struct IContext
    {
        virtual ~IContext() = default;

        virtual ITTF* GetTTF() abstract;
    };
}

static void ttf_draw_string_raw_ttf(OpenRCT2::IContext* context, std::string_view text)
{
    TTFFontDescriptor* fontDesc = context->GetTTF()->ttf_get_font_from_sprite_base(FontSpriteBase::MEDIUM_EXTRA_DARK);
    if (fontDesc->font == nullptr)
    {
        return;
    }

    TTFSurface* surface = context->GetTTF()->ttf_surface_cache_get_or_add(fontDesc->font, text);
    if (surface == nullptr)
        return;
}

namespace UnicodeChar
{
    // Punctuation
    constexpr char32_t leftguillemet = 0xAB;
    constexpr char32_t rightguillemet = 0xBB;
    constexpr char32_t german_quote_open = 0x201E;
    constexpr char32_t quote_open = 0x201C;
    constexpr char32_t quote_close = 0x201D;

    // Dingbats
    constexpr char32_t up = 0x25B2;
    constexpr char32_t small_up = 0x25B4;
    constexpr char32_t right = 0x25B6;
    constexpr char32_t down = 0x25BC;
    constexpr char32_t small_down = 0x25BE;
    constexpr char32_t left = 0x25C0;
    constexpr char32_t tick = 0x2713;
    constexpr char32_t plus = 0x2795;
    constexpr char32_t minus = 0x2796;

    // Emoji
    constexpr char32_t cross = 0x274C;
    constexpr char32_t variation_selector = 0xFE0F;
    constexpr char32_t eye = 0x1F441;
    constexpr char32_t road = 0x1F6E3;
    constexpr char32_t railway = 0x1F6E4;
}; // namespace UnicodeChar


static bool ShouldUseSpriteForCodepoint(char32_t codepoint)
{
    switch (codepoint)
    {
        case UnicodeChar::up:
        case UnicodeChar::down:
        case UnicodeChar::leftguillemet:
        case UnicodeChar::tick:
        case UnicodeChar::cross:
        case UnicodeChar::right:
        case UnicodeChar::rightguillemet:
        case UnicodeChar::small_up:
        case UnicodeChar::small_down:
        case UnicodeChar::left:
        case UnicodeChar::quote_open:
        case UnicodeChar::quote_close:
        case UnicodeChar::german_quote_open:
        case UnicodeChar::plus:
        case UnicodeChar::minus:
        case UnicodeChar::variation_selector:
        case UnicodeChar::eye:
        case UnicodeChar::road:
        case UnicodeChar::railway:
            return true;
        default:
            return false;
    }
}

void ttf_process_string_literal(OpenRCT2::IContext* context, std::string_view text)
{
    CodepointView codepoints(text);
    std::optional<size_t> ttfRunIndex;
    for (auto it = codepoints.begin(); it != codepoints.end(); it++)
    {
        auto codepoint = *it;
        if (ShouldUseSpriteForCodepoint(codepoint))
        {
            if (ttfRunIndex.has_value())
            {
                // Draw the TTF run
                auto len = it.GetIndex() - ttfRunIndex.value();  // { dg-bogus "may be used uninitialized" }
                ttf_draw_string_raw_ttf(context, text.substr(ttfRunIndex.value(), len));
                ttfRunIndex = std::nullopt;
            }

            // Draw the sprite font glyph
        }
        else
        {
            if (!ttfRunIndex.has_value())
            {
                ttfRunIndex = it.GetIndex();
            }
        }
    }
}