File: PrintfMatcher.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (91 lines) | stat: -rw-r--r-- 3,012 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//===-- PrintfMatcher.cpp ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "PrintfMatcher.h"

#include "src/__support/CPP/UInt128.h"
#include "src/stdio/printf_core/core_structs.h"

#include "utils/UnitTest/StringUtils.h"

#include <stdint.h>

namespace __llvm_libc {
namespace printf_core {
namespace testing {

bool FormatSectionMatcher::match(FormatSection actualValue) {
  actual = actualValue;
  return expected == actual;
}

namespace {

#define IF_FLAG_SHOW_FLAG(flag_name)                                           \
  do {                                                                         \
    if ((form.flags & FormatFlags::flag_name) == FormatFlags::flag_name)       \
      stream << "\n\t\t" << #flag_name;                                        \
  } while (false)
#define CASE_LM(lm)                                                            \
  case (LengthModifier::lm):                                                   \
    stream << #lm;                                                             \
    break

void display(testutils::StreamWrapper &stream, FormatSection form) {
  stream << "Raw String (len " << form.raw_len << "): \"";
  for (size_t i = 0; i < form.raw_len; ++i) {
    stream << form.raw_string[i];
  }
  stream << "\"";
  if (form.has_conv) {
    stream << "\n\tHas Conv\n\tFlags:";
    IF_FLAG_SHOW_FLAG(LEFT_JUSTIFIED);
    IF_FLAG_SHOW_FLAG(FORCE_SIGN);
    IF_FLAG_SHOW_FLAG(SPACE_PREFIX);
    IF_FLAG_SHOW_FLAG(ALTERNATE_FORM);
    IF_FLAG_SHOW_FLAG(LEADING_ZEROES);
    stream << "\n";
    stream << "\tmin width: " << form.min_width << "\n";
    stream << "\tprecision: " << form.precision << "\n";
    stream << "\tlength modifier: ";
    switch (form.length_modifier) {
      CASE_LM(none);
      CASE_LM(l);
      CASE_LM(ll);
      CASE_LM(h);
      CASE_LM(hh);
      CASE_LM(j);
      CASE_LM(z);
      CASE_LM(t);
      CASE_LM(L);
    }
    stream << "\n";
    stream << "\tconversion name: " << form.conv_name << "\n";
    if (form.conv_name == 'p' || form.conv_name == 'n' || form.conv_name == 's')
      stream << "\tpointer value: "
             << int_to_hex<uintptr_t>(
                    reinterpret_cast<uintptr_t>(form.conv_val_ptr))
             << "\n";
    else if (form.conv_name != '%')
      stream << "\tvalue: " << int_to_hex<UInt128>(form.conv_val_raw) << "\n";
  }
}
} // anonymous namespace

void FormatSectionMatcher::explainError(testutils::StreamWrapper &stream) {
  stream << "expected format section: ";
  display(stream, expected);
  stream << '\n';
  stream << "actual format section  : ";
  display(stream, actual);
  stream << '\n';
}

} // namespace testing
} // namespace printf_core
} // namespace __llvm_libc