1//===-- ScanfMatcher.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 "ScanfMatcher.h"
10
11#include "src/__support/FPUtil/FPBits.h"
12#include "src/__support/macros/config.h"
13#include "src/stdio/scanf_core/core_structs.h"
14
15#include "test/UnitTest/StringUtils.h"
16#include "test/UnitTest/Test.h"
17
18#include <stdint.h>
19
20namespace LIBC_NAMESPACE_DECL {
21namespace testing {
22
23using scanf_core::FormatFlags;
24using scanf_core::FormatSection;
25using scanf_core::LengthModifier;
26
27bool FormatSectionMatcher::match(FormatSection actualValue) {
28 actual = actualValue;
29 return expected == actual;
30}
31
32namespace {
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
44void display(FormatSection form) {
45 tlog << "Raw String (len " << form.raw_string.size() << "): \"";
46 for (size_t i = 0; i < form.raw_string.size(); ++i) {
47 tlog << form.raw_string[i];
48 }
49 tlog << "\"";
50 if (form.has_conv) {
51 tlog << "\n\tHas Conv\n\tFlags:";
52 IF_FLAG_SHOW_FLAG(NO_WRITE);
53 IF_FLAG_SHOW_FLAG(ALLOCATE);
54 tlog << "\n";
55 tlog << "\tmax width: " << form.max_width << "\n";
56 tlog << "\tlength modifier: ";
57 switch (form.length_modifier) {
58 CASE_LM(NONE);
59 CASE_LM(l);
60 CASE_LM(ll);
61 CASE_LM(h);
62 CASE_LM(hh);
63 CASE_LM(j);
64 CASE_LM(z);
65 CASE_LM(t);
66 CASE_LM(L);
67 }
68 tlog << "\n";
69 // If the pointer is used (NO_WRITE is not set and the conversion isn't %).
70 if (((form.flags & FormatFlags::NO_WRITE) == 0) &&
71 (form.conv_name != '%')) {
72 tlog << "\tpointer value: "
73 << int_to_hex<uintptr_t>(
74 reinterpret_cast<uintptr_t>(form.output_ptr))
75 << "\n";
76 }
77
78 tlog << "\tconversion name: " << form.conv_name << "\n";
79
80 if (form.conv_name == '[') {
81 tlog << "\t\t";
82 for (size_t i = 0; i < 256 /* char max */; ++i) {
83 if (form.scan_set.test(i)) {
84 tlog << static_cast<char>(i);
85 }
86 }
87 tlog << "\n\t]\n";
88 }
89 }
90}
91} // anonymous namespace
92
93void 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_DECL
104

source code of libc/test/UnitTest/ScanfMatcher.cpp