xref: /aosp_15_r20/external/llvm-libc/test/UnitTest/PrintfMatcher.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 //===----------------------------------------------------------------------===//
8 
9 #include "PrintfMatcher.h"
10 
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/printf_core/core_structs.h"
14 
15 #include "test/UnitTest/StringUtils.h"
16 #include "test/UnitTest/Test.h"
17 
18 #include <stdint.h>
19 
20 namespace LIBC_NAMESPACE_DECL {
21 namespace testing {
22 
23 using printf_core::FormatFlags;
24 using printf_core::FormatSection;
25 using printf_core::LengthModifier;
26 
match(FormatSection actualValue)27 bool FormatSectionMatcher::match(FormatSection actualValue) {
28   actual = actualValue;
29   return expected == actual;
30 }
31 
32 namespace {
33 
34 #define IF_FLAG_SHOW_FLAG(flag_name)                                           \
35   do {                                                                         \
36     if ((form.flags & FormatFlags::flag_name) == FormatFlags::flag_name)       \
37       tlog << "\n\t\t" << #flag_name;                                          \
38   } while (false)
39 #define CASE_LM(lm)                                                            \
40   case (LengthModifier::lm):                                                   \
41     tlog << #lm;                                                               \
42     break
43 #define CASE_LM_BIT_WIDTH(lm, bw)                                              \
44   case (LengthModifier::lm):                                                   \
45     tlog << #lm << "\n\tbit width: :" << bw;                                   \
46     break
47 
display(FormatSection form)48 static void display(FormatSection form) {
49   tlog << "Raw String (len " << form.raw_string.size() << "): \"";
50   for (size_t i = 0; i < form.raw_string.size(); ++i) {
51     tlog << form.raw_string[i];
52   }
53   tlog << "\"";
54   if (form.has_conv) {
55     tlog << "\n\tHas Conv\n\tFlags:";
56     IF_FLAG_SHOW_FLAG(LEFT_JUSTIFIED);
57     IF_FLAG_SHOW_FLAG(FORCE_SIGN);
58     IF_FLAG_SHOW_FLAG(SPACE_PREFIX);
59     IF_FLAG_SHOW_FLAG(ALTERNATE_FORM);
60     IF_FLAG_SHOW_FLAG(LEADING_ZEROES);
61     tlog << "\n";
62     tlog << "\tmin width: " << form.min_width << "\n";
63     tlog << "\tprecision: " << form.precision << "\n";
64     tlog << "\tlength modifier: ";
65     switch (form.length_modifier) {
66       CASE_LM(none);
67       CASE_LM(l);
68       CASE_LM(ll);
69       CASE_LM(h);
70       CASE_LM(hh);
71       CASE_LM(j);
72       CASE_LM(z);
73       CASE_LM(t);
74       CASE_LM(L);
75       CASE_LM_BIT_WIDTH(w, form.bit_width);
76       CASE_LM_BIT_WIDTH(wf, form.bit_width);
77     }
78     tlog << "\n";
79     tlog << "\tconversion name: " << form.conv_name << "\n";
80     if (form.conv_name == 'p' || form.conv_name == 'n' || form.conv_name == 's')
81       tlog << "\tpointer value: "
82            << int_to_hex<uintptr_t>(
83                   reinterpret_cast<uintptr_t>(form.conv_val_ptr))
84            << "\n";
85     else if (form.conv_name != '%')
86       tlog << "\tvalue: "
87            << int_to_hex<fputil::FPBits<long double>::StorageType>(
88                   form.conv_val_raw)
89            << "\n";
90   }
91 }
92 } // anonymous namespace
93 
explainError()94 void FormatSectionMatcher::explainError() {
95   tlog << "expected format section: ";
96   display(expected);
97   tlog << '\n';
98   tlog << "actual format section  : ";
99   display(actual);
100   tlog << '\n';
101 }
102 
103 } // namespace testing
104 } // namespace LIBC_NAMESPACE_DECL
105