1 //===-- ScanfMatcher.cpp ----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ScanfMatcher.h"
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/__support/macros/config.h"
13 #include "src/stdio/scanf_core/core_structs.h"
15 #include "test/UnitTest/StringUtils.h"
16 #include "test/UnitTest/Test.h"
20 namespace LIBC_NAMESPACE_DECL
{
23 using scanf_core::FormatFlags
;
24 using scanf_core::FormatSection
;
25 using scanf_core::LengthModifier
;
27 bool FormatSectionMatcher::match(FormatSection actualValue
) {
29 return expected
== actual
;
34 #define IF_FLAG_SHOW_FLAG(flag_name) \
36 if ((form.flags & FormatFlags::flag_name) == FormatFlags::flag_name) \
37 tlog << "\n\t\t" << #flag_name; \
40 case (LengthModifier::lm): \
44 void 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
];
51 tlog
<< "\n\tHas Conv\n\tFlags:";
52 IF_FLAG_SHOW_FLAG(NO_WRITE
);
53 IF_FLAG_SHOW_FLAG(ALLOCATE
);
55 tlog
<< "\tmax width: " << form
.max_width
<< "\n";
56 tlog
<< "\tlength modifier: ";
57 switch (form
.length_modifier
) {
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
))
78 tlog
<< "\tconversion name: " << form
.conv_name
<< "\n";
80 if (form
.conv_name
== '[') {
82 for (size_t i
= 0; i
< 256 /* char max */; ++i
) {
83 if (form
.scan_set
.test(i
)) {
84 tlog
<< static_cast<char>(i
);
91 } // anonymous namespace
93 void FormatSectionMatcher::explainError() {
94 tlog
<< "expected format section: ";
97 tlog
<< "actual format section : ";
102 } // namespace testing
103 } // namespace LIBC_NAMESPACE_DECL