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