Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / UnitTest / PrintfMatcher.cpp
blobe895da2be120f8cfba96b1f1f18512d6cc9afecc
1 //===-- PrintfMatcher.cpp ---------------------------------------*- 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 #include "PrintfMatcher.h"
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/stdio/printf_core/core_structs.h"
14 #include "test/UnitTest/StringUtils.h"
15 #include "test/UnitTest/Test.h"
17 #include <stdint.h>
19 namespace LIBC_NAMESPACE {
20 namespace testing {
22 using printf_core::FormatFlags;
23 using printf_core::FormatSection;
24 using printf_core::LengthModifier;
26 bool FormatSectionMatcher::match(FormatSection actualValue) {
27 actual = actualValue;
28 return expected == actual;
31 namespace {
33 #define IF_FLAG_SHOW_FLAG(flag_name) \
34 do { \
35 if ((form.flags & FormatFlags::flag_name) == FormatFlags::flag_name) \
36 tlog << "\n\t\t" << #flag_name; \
37 } while (false)
38 #define CASE_LM(lm) \
39 case (LengthModifier::lm): \
40 tlog << #lm; \
41 break
43 static void display(FormatSection form) {
44 tlog << "Raw String (len " << form.raw_string.size() << "): \"";
45 for (size_t i = 0; i < form.raw_string.size(); ++i) {
46 tlog << form.raw_string[i];
48 tlog << "\"";
49 if (form.has_conv) {
50 tlog << "\n\tHas Conv\n\tFlags:";
51 IF_FLAG_SHOW_FLAG(LEFT_JUSTIFIED);
52 IF_FLAG_SHOW_FLAG(FORCE_SIGN);
53 IF_FLAG_SHOW_FLAG(SPACE_PREFIX);
54 IF_FLAG_SHOW_FLAG(ALTERNATE_FORM);
55 IF_FLAG_SHOW_FLAG(LEADING_ZEROES);
56 tlog << "\n";
57 tlog << "\tmin width: " << form.min_width << "\n";
58 tlog << "\tprecision: " << form.precision << "\n";
59 tlog << "\tlength modifier: ";
60 switch (form.length_modifier) {
61 CASE_LM(none);
62 CASE_LM(l);
63 CASE_LM(ll);
64 CASE_LM(h);
65 CASE_LM(hh);
66 CASE_LM(j);
67 CASE_LM(z);
68 CASE_LM(t);
69 CASE_LM(L);
71 tlog << "\n";
72 tlog << "\tconversion name: " << form.conv_name << "\n";
73 if (form.conv_name == 'p' || form.conv_name == 'n' || form.conv_name == 's')
74 tlog << "\tpointer value: "
75 << int_to_hex<uintptr_t>(
76 reinterpret_cast<uintptr_t>(form.conv_val_ptr))
77 << "\n";
78 else if (form.conv_name != '%')
79 tlog << "\tvalue: "
80 << int_to_hex<fputil::FPBits<long double>::UIntType>(
81 form.conv_val_raw)
82 << "\n";
85 } // anonymous namespace
87 void FormatSectionMatcher::explainError() {
88 tlog << "expected format section: ";
89 display(expected);
90 tlog << '\n';
91 tlog << "actual format section : ";
92 display(actual);
93 tlog << '\n';
96 } // namespace testing
97 } // namespace LIBC_NAMESPACE