[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / __support / arg_list.h
blob43cf8352f4b4600c781217d029d28c692b81eea2
1 //===-- Holder Class for manipulating va_lists ------------------*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_ARG_LIST_H
10 #define LLVM_LIBC_SRC_SUPPORT_ARG_LIST_H
12 #include "src/__support/common.h"
14 #include <stdarg.h>
15 #include <stddef.h>
17 namespace __llvm_libc {
18 namespace internal {
20 class ArgList {
21 va_list vlist;
23 public:
24 LIBC_INLINE ArgList(va_list vlist) { va_copy(this->vlist, vlist); }
25 LIBC_INLINE ArgList(ArgList &other) { va_copy(this->vlist, other.vlist); }
26 LIBC_INLINE ~ArgList() { va_end(this->vlist); }
28 LIBC_INLINE ArgList &operator=(ArgList &rhs) {
29 va_copy(vlist, rhs.vlist);
30 return *this;
33 template <class T> LIBC_INLINE T next_var() { return va_arg(vlist, T); }
36 // Used for testing things that use an ArgList when it's impossible to know what
37 // the arguments should be ahead of time. An example of this would be fuzzing,
38 // since a function passed a random input could request unpredictable arguments.
39 class MockArgList {
40 size_t arg_counter = 0;
42 public:
43 LIBC_INLINE MockArgList() = default;
44 LIBC_INLINE MockArgList(va_list) { ; }
45 LIBC_INLINE MockArgList(MockArgList &other) {
46 arg_counter = other.arg_counter;
48 LIBC_INLINE ~MockArgList() = default;
50 LIBC_INLINE MockArgList &operator=(MockArgList &rhs) {
51 arg_counter = rhs.arg_counter;
52 return *this;
55 template <class T> LIBC_INLINE T next_var() {
56 ++arg_counter;
57 return T(arg_counter);
60 size_t read_count() const { return arg_counter; }
63 } // namespace internal
64 } // namespace __llvm_libc
66 #endif // LLVM_LIBC_SRC_SUPPORT_ARG_LIST_H