[llvm] [libc] Refactor scanf reader to match printf (PR #66023)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 22 12:49:38 PDT 2023
https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/66023
>From 87010b983a87c8dca0791f6cc55707ef49cf89dc Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Mon, 11 Sep 2023 15:22:15 -0700
Subject: [PATCH 1/3] [libc] Refactor scanf reader to match printf
In a previous patch, the printf writer was rewritten to use a single
writer class with a buffer and a callback hook. This patch refactors
scanf's reader to match conceptually.
---
libc/config/linux/aarch64/entrypoints.txt | 4 ++
libc/config/linux/riscv64/entrypoints.txt | 7 ++-
libc/config/linux/x86_64/entrypoints.txt | 6 +--
libc/src/stdio/CMakeLists.txt | 26 ++++++++--
libc/src/stdio/scanf.cpp | 8 ++-
libc/src/stdio/scanf_core/CMakeLists.txt | 40 ++++-----------
libc/src/stdio/scanf_core/file_reader.cpp | 27 ----------
libc/src/stdio/scanf_core/file_reader.h | 39 ---------------
libc/src/stdio/scanf_core/reader.cpp | 32 +++---------
libc/src/stdio/scanf_core/reader.h | 49 +++++++++++++------
libc/src/stdio/scanf_core/scanf_main.cpp | 5 --
libc/src/stdio/scanf_core/string_reader.cpp | 24 ---------
libc/src/stdio/scanf_core/string_reader.h | 33 -------------
.../src/stdio/scanf_core/vfscanf_internal.cpp | 29 -----------
libc/src/stdio/scanf_core/vfscanf_internal.h | 46 ++++++++++++++++-
libc/src/stdio/sscanf.cpp | 7 +--
libc/test/src/stdio/CMakeLists.txt | 21 ++++++--
libc/test/src/stdio/fscanf_test.cpp | 38 ++++++++++----
libc/test/src/stdio/scanf_core/CMakeLists.txt | 17 +++----
...string_reader_test.cpp => reader_test.cpp} | 15 +++---
20 files changed, 198 insertions(+), 275 deletions(-)
delete mode 100644 libc/src/stdio/scanf_core/file_reader.cpp
delete mode 100644 libc/src/stdio/scanf_core/file_reader.h
delete mode 100644 libc/src/stdio/scanf_core/string_reader.cpp
delete mode 100644 libc/src/stdio/scanf_core/string_reader.h
delete mode 100644 libc/src/stdio/scanf_core/vfscanf_internal.cpp
rename libc/test/src/stdio/scanf_core/{string_reader_test.cpp => reader_test.cpp} (76%)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e0bf9800ec881c2..fc865dc0cbb6a57 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -126,6 +126,10 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.snprintf
libc.src.stdio.vsprintf
libc.src.stdio.vsnprintf
+ #TODO: Check if scanf can be enabled on aarch64
+ #libc.src.stdio.sscanf
+ #libc.src.stdio.scanf
+ #libc.src.stdio.fscanf
# sys/mman.h entrypoints
libc.src.sys.mman.madvise
diff --git a/libc/config/linux/riscv64/entrypoints.txt b/libc/config/linux/riscv64/entrypoints.txt
index 2b2f2629f78ce67..9574595ed459383 100644
--- a/libc/config/linux/riscv64/entrypoints.txt
+++ b/libc/config/linux/riscv64/entrypoints.txt
@@ -132,6 +132,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.vsnprintf
libc.src.stdio.vfprintf
libc.src.stdio.vprintf
+ libc.src.stdio.sscanf
+ libc.src.stdio.scanf
+ libc.src.stdio.fscanf
# sys/mman.h entrypoints
libc.src.sys.mman.madvise
@@ -439,10 +442,6 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
- libc.src.stdio.printf
- libc.src.stdio.sscanf
- libc.src.stdio.scanf
- libc.src.stdio.fscanf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index dcb8c6231432d35..1c3fcb0c1e7c9e9 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -132,6 +132,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.vsnprintf
libc.src.stdio.vfprintf
libc.src.stdio.vprintf
+ libc.src.stdio.sscanf
+ libc.src.stdio.scanf
+ libc.src.stdio.fscanf
# sys/mman.h entrypoints
libc.src.sys.mman.madvise
@@ -445,9 +448,6 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
- libc.src.stdio.sscanf
- libc.src.stdio.scanf
- libc.src.stdio.fscanf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index f3a75fb965c6e16..da95533308fdf71 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -126,6 +126,21 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)
+list(APPEND scanf_deps
+ libc.src.__support.arg_list
+ libc.src.stdio.scanf_core.vfscanf_internal
+)
+
+if(LLVM_LIBC_FULL_BUILD)
+ list(APPEND scanf_deps
+ libc.src.__support.File.file
+ libc.src.__support.File.platform_file
+ libc.src.__support.File.platform_stdin
+ )
+else()
+ list(APPEND scanf_copts "-DLIBC_COPT_SCANF_USE_SYSTEM_FILE")
+endif()
+
add_entrypoint_object(
sscanf
SRCS
@@ -134,7 +149,6 @@ add_entrypoint_object(
sscanf.h
DEPENDS
libc.src.__support.arg_list
- libc.src.stdio.scanf_core.string_reader
libc.src.stdio.scanf_core.reader
libc.src.stdio.scanf_core.scanf_main
)
@@ -146,8 +160,9 @@ add_entrypoint_object(
HDRS
fscanf.h
DEPENDS
- libc.src.__support.arg_list
- libc.src.stdio.scanf_core.vfscanf_internal
+ ${scanf_deps}
+ COMPILE_OPTIONS
+ ${scanf_copts}
)
add_entrypoint_object(
@@ -157,8 +172,9 @@ add_entrypoint_object(
HDRS
scanf.h
DEPENDS
- libc.src.__support.arg_list
- libc.src.stdio.scanf_core.vfscanf_internal
+ ${scanf_deps}
+ COMPILE_OPTIONS
+ ${scanf_copts}
)
add_entrypoint_object(
diff --git a/libc/src/stdio/scanf.cpp b/libc/src/stdio/scanf.cpp
index 60e77895edcc3c6..d98cc3d58e23251 100644
--- a/libc/src/stdio/scanf.cpp
+++ b/libc/src/stdio/scanf.cpp
@@ -15,6 +15,12 @@
#include <stdarg.h>
#include <stdio.h>
+#ifndef LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#define SCANF_STDIN __llvm_libc::stdin
+#else // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#define SCANF_STDIN ::stdin
+#endif // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
@@ -25,7 +31,7 @@ LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
// destruction automatically.
va_end(vlist);
int ret_val = scanf_core::vfscanf_internal(
- reinterpret_cast<::FILE *>(__llvm_libc::stdin), format, args);
+ reinterpret_cast<::FILE *>(SCANF_STDIN), format, args);
// This is done to avoid including stdio.h in the internals. On most systems
// EOF is -1, so this will be transformed into just "return ret_val".
return (ret_val == -1) ? EOF : ret_val;
diff --git a/libc/src/stdio/scanf_core/CMakeLists.txt b/libc/src/stdio/scanf_core/CMakeLists.txt
index 8cdd33e5c2c0f94..1fab27d3d28b5e0 100644
--- a/libc/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/src/stdio/scanf_core/CMakeLists.txt
@@ -22,12 +22,6 @@ add_header_library(
libc.src.__support.CPP.string_view
)
-if(NOT (TARGET libc.src.__support.File.file))
- # Not all platforms have a file implementation. If file is unvailable,
- # then we must skip all the parts that need file.
- return()
-endif()
-
add_object_library(
scanf_main
SRCS
@@ -42,24 +36,6 @@ add_object_library(
libc.src.__support.arg_list
)
-add_object_library(
- string_reader
- SRCS
- string_reader.cpp
- HDRS
- string_reader.h
-)
-
-add_object_library(
- file_reader
- SRCS
- file_reader.cpp
- HDRS
- file_reader.h
- DEPENDS
- libc.src.__support.File.file
-)
-
add_object_library(
reader
SRCS
@@ -67,8 +43,7 @@ add_object_library(
HDRS
reader.h
DEPENDS
- .string_reader
- .file_reader
+ libc.src.__support.macros.attributes
)
add_object_library(
@@ -99,15 +74,20 @@ add_object_library(
libc.src.__support.str_to_float
)
-add_object_library(
+if(NOT (TARGET libc.src.__support.File.file) AND LLVM_LIBC_FULL_BUILD)
+ # Not all platforms have a file implementation. If file is unvailable, and a
+ # full build is requested, then we must skip all file based printf sections.
+ return()
+endif()
+
+add_header_library(
vfscanf_internal
- SRCS
- vfscanf_internal.cpp
HDRS
vfscanf_internal.h
DEPENDS
.reader
- .file_reader
.scanf_main
+ libc.include.stdio
+ libc.src.__support.File.file
libc.src.__support.arg_list
)
diff --git a/libc/src/stdio/scanf_core/file_reader.cpp b/libc/src/stdio/scanf_core/file_reader.cpp
deleted file mode 100644
index 51e22299e3dd2c5..000000000000000
--- a/libc/src/stdio/scanf_core/file_reader.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-//===-- FILE Reader implementation for scanf --------------------*- 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 "src/stdio/scanf_core/file_reader.h"
-#include "src/__support/File/file.h"
-#include <stddef.h>
-
-namespace __llvm_libc {
-namespace scanf_core {
-
-char FileReader::get_char() {
- char tiny_buff = 0;
- auto result = file->read_unlocked(&tiny_buff, 1);
- if (result.value != 1 || result.has_error())
- return 0;
- return tiny_buff;
-}
-
-void FileReader::unget_char(char c) { file->ungetc_unlocked(c); }
-
-} // namespace scanf_core
-} // namespace __llvm_libc
diff --git a/libc/src/stdio/scanf_core/file_reader.h b/libc/src/stdio/scanf_core/file_reader.h
deleted file mode 100644
index a70e00bc2413963..000000000000000
--- a/libc/src/stdio/scanf_core/file_reader.h
+++ /dev/null
@@ -1,39 +0,0 @@
-//===-- FILE Reader definition for scanf ------------------------*- 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_FILE_READER_H
-#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_FILE_READER_H
-
-#include "src/__support/File/file.h"
-
-#include <stddef.h>
-#include <stdio.h>
-
-namespace __llvm_libc {
-namespace scanf_core {
-
-class FileReader {
- __llvm_libc::File *file;
-
-public:
- FileReader(::FILE *init_file) {
- file = reinterpret_cast<__llvm_libc::File *>(init_file);
- file->lock();
- }
-
- ~FileReader() { file->unlock(); }
-
- char get_char();
- void unget_char(char c);
- bool has_error() { return file->error_unlocked(); }
-};
-
-} // namespace scanf_core
-} // namespace __llvm_libc
-
-#endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_FILE_READER_H
diff --git a/libc/src/stdio/scanf_core/reader.cpp b/libc/src/stdio/scanf_core/reader.cpp
index 4834f4d6a6e7955..45241d8d86b01fe 100644
--- a/libc/src/stdio/scanf_core/reader.cpp
+++ b/libc/src/stdio/scanf_core/reader.cpp
@@ -12,33 +12,17 @@
namespace __llvm_libc {
namespace scanf_core {
-char Reader::getc() {
- ++cur_chars_read;
- if (reader_type == ReaderType::String) {
- return string_reader->get_char();
- } else {
- return file_reader->get_char();
- }
-}
-
void Reader::ungetc(char c) {
--cur_chars_read;
- if (reader_type == ReaderType::String) {
- // The string reader ignores the char c passed to unget since it doesn't
- // need to place anything back into a buffer, and modifying the source
- // string would be dangerous.
- return string_reader->unget_char();
- } else {
- return file_reader->unget_char(c);
+ if (rb != nullptr && rb->buff_cur > 0) {
+ // While technically c should be written back to the buffer, in scanf we
+ // always write the character that was already there. Additionally, the
+ // buffer is most likely to contain a string that isn't part of a file,
+ // which may not be writable.
+ --(rb->buff_cur);
+ return;
}
+ stream_ungetc(static_cast<int>(c), input_stream);
}
-
-bool Reader::has_error() {
- if (reader_type == ReaderType::File) {
- return file_reader->has_error();
- }
- return false;
-}
-
} // namespace scanf_core
} // namespace __llvm_libc
diff --git a/libc/src/stdio/scanf_core/reader.h b/libc/src/stdio/scanf_core/reader.h
index 000ab02ad28f21e..7e9cfc5c8ca2fb1 100644
--- a/libc/src/stdio/scanf_core/reader.h
+++ b/libc/src/stdio/scanf_core/reader.h
@@ -9,44 +9,61 @@
#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_READER_H
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_READER_H
-#include "src/stdio/scanf_core/file_reader.h"
-#include "src/stdio/scanf_core/string_reader.h"
+#include "src/__support/macros/attributes.h" // For LIBC_INLINE
#include <stddef.h>
namespace __llvm_libc {
namespace scanf_core {
-enum class ReaderType { String, File };
+using StreamGetc = int (*)(void *);
+using StreamUngetc = void (*)(int, void *);
-class Reader final {
- union {
- StringReader *string_reader;
- FileReader *file_reader;
- };
+// This is intended to be either a raw string or a buffer syncronized with the
+// file's internal buffer.
+struct ReadBuffer {
+ char *buffer;
+ size_t buff_len;
+ size_t buff_cur = 0;
+};
+
+class Reader {
+ ReadBuffer *rb;
- const ReaderType reader_type;
+ void *input_stream = nullptr;
+
+ StreamGetc stream_getc = nullptr;
+ StreamUngetc stream_ungetc = nullptr;
size_t cur_chars_read = 0;
public:
- Reader(StringReader *init_string_reader)
- : string_reader(init_string_reader), reader_type(ReaderType::String) {}
+ // TODO: Set buff_len with a proper constant
+ Reader(ReadBuffer *string_buffer) : rb(string_buffer) {}
- Reader(FileReader *init_file_reader)
- : file_reader(init_file_reader), reader_type(ReaderType::File) {}
+ Reader(void *stream, StreamGetc stream_getc_in, StreamUngetc stream_ungetc_in,
+ ReadBuffer *stream_buffer = nullptr)
+ : rb(stream_buffer), input_stream(stream), stream_getc(stream_getc_in),
+ stream_ungetc(stream_ungetc_in) {}
// This returns the next character from the input and advances it by one
// character. When it hits the end of the string or file it returns '\0' to
// signal to stop parsing.
- char getc();
+ LIBC_INLINE char getc() {
+ ++cur_chars_read;
+ if (rb != nullptr) {
+ char output = rb->buffer[rb->buff_cur];
+ ++(rb->buff_cur);
+ return output;
+ }
+ // This should reset the buffer if applicable.
+ return static_cast<char>(stream_getc(input_stream));
+ }
// This moves the input back by one character, placing c into the buffer if
// this is a file reader, else c is ignored.
void ungetc(char c);
size_t chars_read() { return cur_chars_read; }
-
- bool has_error();
};
} // namespace scanf_core
diff --git a/libc/src/stdio/scanf_core/scanf_main.cpp b/libc/src/stdio/scanf_core/scanf_main.cpp
index e7e41cbe899d720..ba6a92c41eea3ed 100644
--- a/libc/src/stdio/scanf_core/scanf_main.cpp
+++ b/libc/src/stdio/scanf_core/scanf_main.cpp
@@ -38,11 +38,6 @@ int scanf_main(Reader *reader, const char *__restrict str,
}
}
- if (conversions == 0 && reader->has_error()) {
- // This is intended to be converted to EOF in the client call to avoid
- // including stdio.h in this internal file.
- return -1;
- }
return conversions;
}
diff --git a/libc/src/stdio/scanf_core/string_reader.cpp b/libc/src/stdio/scanf_core/string_reader.cpp
deleted file mode 100644
index 1d728d2b9eb35e6..000000000000000
--- a/libc/src/stdio/scanf_core/string_reader.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-//===-- String Reader implementation for scanf ------------------*- 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 "src/stdio/scanf_core/string_reader.h"
-#include <stddef.h>
-
-namespace __llvm_libc {
-namespace scanf_core {
-
-char StringReader::get_char() {
- char cur_char = string[cur_index];
- ++cur_index;
- return cur_char;
-}
-
-void StringReader::unget_char() { --cur_index; }
-
-} // namespace scanf_core
-} // namespace __llvm_libc
diff --git a/libc/src/stdio/scanf_core/string_reader.h b/libc/src/stdio/scanf_core/string_reader.h
deleted file mode 100644
index 35550b16c32140f..000000000000000
--- a/libc/src/stdio/scanf_core/string_reader.h
+++ /dev/null
@@ -1,33 +0,0 @@
-//===-- String Reader definition for scanf ----------------------*- 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_STRING_READER_H
-#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_STRING_READER_H
-
-#include <stddef.h>
-
-namespace __llvm_libc {
-namespace scanf_core {
-
-class StringReader {
- const char *string;
- size_t cur_index = 0;
-
-public:
- StringReader(const char *init_string) { string = init_string; }
-
- ~StringReader() {}
-
- char get_char();
- void unget_char();
-};
-
-} // namespace scanf_core
-} // namespace __llvm_libc
-
-#endif // LLVM_LIBC_SRC_STDIO_SCANF_CORE_STRING_READER_H
diff --git a/libc/src/stdio/scanf_core/vfscanf_internal.cpp b/libc/src/stdio/scanf_core/vfscanf_internal.cpp
deleted file mode 100644
index af2f6fa01ad475d..000000000000000
--- a/libc/src/stdio/scanf_core/vfscanf_internal.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-//===-- Internal implementation of vfscanf ---------------------*- 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 "src/stdio/scanf_core/vfscanf_internal.h"
-
-#include "src/__support/arg_list.h"
-#include "src/stdio/scanf_core/file_reader.h"
-#include "src/stdio/scanf_core/reader.h"
-#include "src/stdio/scanf_core/scanf_main.h"
-
-#include <stdio.h>
-
-namespace __llvm_libc {
-namespace scanf_core {
-
-int vfscanf_internal(::FILE *__restrict stream, const char *__restrict format,
- internal::ArgList &args) {
- FileReader file_reader(stream);
- scanf_core::Reader reader(&file_reader);
- return scanf_core::scanf_main(&reader, format, args);
-}
-
-} // namespace scanf_core
-} // namespace __llvm_libc
diff --git a/libc/src/stdio/scanf_core/vfscanf_internal.h b/libc/src/stdio/scanf_core/vfscanf_internal.h
index 456d1ff92056bed..693230560bc4f30 100644
--- a/libc/src/stdio/scanf_core/vfscanf_internal.h
+++ b/libc/src/stdio/scanf_core/vfscanf_internal.h
@@ -9,15 +9,57 @@
#ifndef LLVM_LIBC_SRC_STDIO_SCANF_CORE_VFSCANF_INTERNAL_H
#define LLVM_LIBC_SRC_STDIO_SCANF_CORE_VFSCANF_INTERNAL_H
+#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
+#include "src/stdio/scanf_core/reader.h"
+#include "src/stdio/scanf_core/scanf_main.h"
#include <stdio.h>
namespace __llvm_libc {
+
+namespace internal {
+#ifndef LIBC_COPT_SCANF_USE_SYSTEM_FILE
+LIBC_INLINE int getc(void *f) {
+ unsigned char c;
+ auto result = reinterpret_cast<__llvm_libc::File *>(f)->read_unlocked(&c, 1);
+ size_t r = result.value;
+ if (result.has_error() || r != 1) {
+ return '\0';
+ }
+ return c;
+}
+
+LIBC_INLINE void ungetc(int c, void *f) {
+ reinterpret_cast<__llvm_libc::File *>(f)->ungetc(c);
+}
+
+LIBC_INLINE int ferror_unlocked(FILE *f) {
+ return reinterpret_cast<__llvm_libc::File *>(f)->error_unlocked();
+}
+#else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE)
+LIBC_INLINE int getc(void *f) { return ::getc(reinterpret_cast<::FILE *>(f)); }
+
+LIBC_INLINE void ungetc(int c, void *f) {
+ ::ungetc(c, reinterpret_cast<::FILE *>(f));
+}
+
+LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror_unlocked(f); }
+#endif // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+} // namespace internal
+
namespace scanf_core {
-int vfscanf_internal(::FILE *__restrict stream, const char *__restrict format,
- internal::ArgList &args);
+LIBC_INLINE int vfscanf_internal(::FILE *__restrict stream,
+ const char *__restrict format,
+ internal::ArgList &args) {
+ scanf_core::Reader reader(stream, &internal::getc, internal::ungetc);
+ int retval = scanf_core::scanf_main(&reader, format, args);
+ if (retval == 0 && internal::ferror_unlocked(stream)) {
+ return EOF;
+ }
+ return retval;
+}
} // namespace scanf_core
} // namespace __llvm_libc
diff --git a/libc/src/stdio/sscanf.cpp b/libc/src/stdio/sscanf.cpp
index 205b58fb3390e95..1f3e5ba888cd8fa 100644
--- a/libc/src/stdio/sscanf.cpp
+++ b/libc/src/stdio/sscanf.cpp
@@ -8,10 +8,10 @@
#include "src/stdio/sscanf.h"
+#include "src/__support/CPP/limits.h"
#include "src/__support/arg_list.h"
#include "src/stdio/scanf_core/reader.h"
#include "src/stdio/scanf_core/scanf_main.h"
-#include "src/stdio/scanf_core/string_reader.h"
#include <stdarg.h>
#include <stdio.h>
@@ -27,8 +27,9 @@ LLVM_LIBC_FUNCTION(int, sscanf,
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
- scanf_core::StringReader string_reader(buffer);
- scanf_core::Reader reader(&string_reader);
+ scanf_core::ReadBuffer rb{const_cast<char *>(buffer),
+ cpp::numeric_limits<size_t>::max()};
+ scanf_core::Reader reader(&rb);
int ret_val = scanf_core::scanf_main(&reader, format, args);
// This is done to avoid including stdio.h in the internals. On most systems
// EOF is -1, so this will be transformed into just "return ret_val".
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 98fa2deb8b0e258..54391b1ccba68b7 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -232,6 +232,20 @@ add_libc_test(
libc.src.stdio.vprintf
)
+
+if(LLVM_LIBC_FULL_BUILD)
+ # In fullbuild mode, fscanf's tests use the internal FILE for other functions.
+ list(APPEND fscanf_test_deps
+ libc.src.stdio.fclose
+ libc.src.stdio.ferror
+ libc.src.stdio.fopen
+ libc.src.stdio.fwrite
+ )
+else()
+# Else in overlay mode they use the system's FILE.
+ set(fscanf_test_copts "-DLIBC_COPT_SCANF_USE_SYSTEM_FILE")
+endif()
+
add_libc_unittest(
fscanf_test
SUITE
@@ -240,11 +254,10 @@ add_libc_unittest(
fscanf_test.cpp
DEPENDS
libc.src.stdio.fscanf
- libc.src.stdio.fclose
- libc.src.stdio.ferror
- libc.src.stdio.fopen
- libc.src.stdio.fwrite
+ ${fscanf_test_deps}
libc.src.__support.CPP.string_view
+ COMPILE_OPTIONS
+ ${fscanf_test_copts}
)
add_libc_unittest(
diff --git a/libc/test/src/stdio/fscanf_test.cpp b/libc/test/src/stdio/fscanf_test.cpp
index 71402f0e7c75b4e..35dd32dd8bf3fe0 100644
--- a/libc/test/src/stdio/fscanf_test.cpp
+++ b/libc/test/src/stdio/fscanf_test.cpp
@@ -7,10 +7,13 @@
//===----------------------------------------------------------------------===//
#include "src/__support/CPP/string_view.h"
+
+#ifndef LIBC_COPT_SCANF_USE_SYSTEM_FILE
#include "src/stdio/fclose.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fwrite.h"
+#endif // LIBC_COPT_SCANF_USE_SYSTEM_FILE
#include "src/stdio/fscanf.h"
@@ -18,9 +21,24 @@
#include <stdio.h>
+namespace scanf_test {
+#ifndef LIBC_COPT_SCANF_USE_SYSTEM_FILE
+using __llvm_libc::fclose;
+using __llvm_libc::ferror;
+using __llvm_libc::fopen;
+using __llvm_libc::fwrite;
+#else // defined(LIBC_COPT_SCANF_USE_SYSTEM_FILE)
+using ::fclose;
+using ::ferror;
+using ::fopen;
+using ::fwrite;
+#endif // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+} // namespace scanf_test
+
TEST(LlvmLibcFScanfTest, WriteToFile) {
- constexpr char FILENAME[] = "testdata/fscanf_output.test";
- ::FILE *file = __llvm_libc::fopen(FILENAME, "w");
+ const char *FILENAME = "fscanf_output.test";
+ auto FILE_PATH = libc_make_test_file_path(FILENAME);
+ ::FILE *file = scanf_test::fopen(FILE_PATH, "w");
ASSERT_FALSE(file == nullptr);
int read;
@@ -28,26 +46,26 @@ TEST(LlvmLibcFScanfTest, WriteToFile) {
constexpr char simple[] = "A simple string with no conversions.\n";
ASSERT_EQ(sizeof(simple) - 1,
- __llvm_libc::fwrite(simple, 1, sizeof(simple) - 1, file));
+ scanf_test::fwrite(simple, 1, sizeof(simple) - 1, file));
constexpr char numbers[] = "1234567890\n";
ASSERT_EQ(sizeof(numbers) - 1,
- __llvm_libc::fwrite(numbers, 1, sizeof(numbers) - 1, file));
+ scanf_test::fwrite(numbers, 1, sizeof(numbers) - 1, file));
constexpr char numbers_and_more[] = "1234 and more\n";
ASSERT_EQ(sizeof(numbers_and_more) - 1,
- __llvm_libc::fwrite(numbers_and_more, 1,
- sizeof(numbers_and_more) - 1, file));
+ scanf_test::fwrite(numbers_and_more, 1,
+ sizeof(numbers_and_more) - 1, file));
read =
__llvm_libc::fscanf(file, "Reading from a write-only file should fail.");
EXPECT_LT(read, 0);
- ASSERT_EQ(0, __llvm_libc::fclose(file));
+ ASSERT_EQ(0, scanf_test::fclose(file));
- file = __llvm_libc::fopen(FILENAME, "r");
+ file = scanf_test::fopen(FILE_PATH, "r");
ASSERT_FALSE(file == nullptr);
char data[50];
@@ -67,6 +85,6 @@ TEST(LlvmLibcFScanfTest, WriteToFile) {
ASSERT_EQ(__llvm_libc::cpp::string_view(numbers_and_more),
__llvm_libc::cpp::string_view(data, sizeof(numbers_and_more) - 1));
- ASSERT_EQ(__llvm_libc::ferror(file), 0);
- ASSERT_EQ(__llvm_libc::fclose(file), 0);
+ ASSERT_EQ(scanf_test::ferror(file), 0);
+ ASSERT_EQ(scanf_test::fclose(file), 0);
}
diff --git a/libc/test/src/stdio/scanf_core/CMakeLists.txt b/libc/test/src/stdio/scanf_core/CMakeLists.txt
index db20335a5c94301..a6ff3ec66abc326 100644
--- a/libc/test/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/test/src/stdio/scanf_core/CMakeLists.txt
@@ -13,24 +13,23 @@ add_libc_unittest(
libc.src.__support.arg_list
)
-if(NOT (TARGET libc.src.__support.File.file))
- # Not all platforms have a file implementation. If file is unvailable,
- # then we must skip all the parts that need file.
- return()
-endif()
-
add_libc_unittest(
- string_reader_test
+ reader_test
SUITE
libc_stdio_unittests
SRCS
- string_reader_test.cpp
+ reader_test.cpp
DEPENDS
libc.src.stdio.scanf_core.reader
- libc.src.stdio.scanf_core.string_reader
libc.src.__support.CPP.string_view
)
+if(NOT (TARGET libc.src.__support.File.file))
+ # Not all platforms have a file implementation. If file is unvailable,
+ # then we must skip all the parts that need file.
+ return()
+endif()
+
add_libc_unittest(
converter_test
SUITE
diff --git a/libc/test/src/stdio/scanf_core/string_reader_test.cpp b/libc/test/src/stdio/scanf_core/reader_test.cpp
similarity index 76%
rename from libc/test/src/stdio/scanf_core/string_reader_test.cpp
rename to libc/test/src/stdio/scanf_core/reader_test.cpp
index 4b3d14855d5174b..9db916c115a2424 100644
--- a/libc/test/src/stdio/scanf_core/string_reader_test.cpp
+++ b/libc/test/src/stdio/scanf_core/reader_test.cpp
@@ -8,20 +8,21 @@
#include "src/__support/CPP/string_view.h"
#include "src/stdio/scanf_core/reader.h"
-#include "src/stdio/scanf_core/string_reader.h"
#include "test/UnitTest/Test.h"
TEST(LlvmLibcScanfStringReaderTest, Constructor) {
char str[10];
- __llvm_libc::scanf_core::StringReader str_reader(str);
- __llvm_libc::scanf_core::Reader reader(&str_reader);
+ // buff_len justneeds to be a big number. The specific value isn't important
+ // in the real world.
+ __llvm_libc::scanf_core::ReadBuffer rb{const_cast<char *>(str), 1000000};
+ __llvm_libc::scanf_core::Reader reader(&rb);
}
TEST(LlvmLibcScanfStringReaderTest, SimpleRead) {
const char *str = "abc";
- __llvm_libc::scanf_core::StringReader str_reader(str);
- __llvm_libc::scanf_core::Reader reader(&str_reader);
+ __llvm_libc::scanf_core::ReadBuffer rb{const_cast<char *>(str), 1000000};
+ __llvm_libc::scanf_core::Reader reader(&rb);
for (size_t i = 0; i < sizeof("abc"); ++i) {
ASSERT_EQ(str[i], reader.getc());
@@ -30,8 +31,8 @@ TEST(LlvmLibcScanfStringReaderTest, SimpleRead) {
TEST(LlvmLibcScanfStringReaderTest, ReadAndReverse) {
const char *str = "abcDEF123";
- __llvm_libc::scanf_core::StringReader str_reader(str);
- __llvm_libc::scanf_core::Reader reader(&str_reader);
+ __llvm_libc::scanf_core::ReadBuffer rb{const_cast<char *>(str), 1000000};
+ __llvm_libc::scanf_core::Reader reader(&rb);
for (size_t i = 0; i < 5; ++i) {
ASSERT_EQ(str[i], reader.getc());
>From 40b454d599fbec44f12f9de950d3ff5530243dbe Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Fri, 22 Sep 2023 12:41:10 -0700
Subject: [PATCH 2/3] Rename system file flag, clean up formatting.
---
libc/config/linux/aarch64/entrypoints.txt | 1 -
libc/docs/dev/printf_behavior.rst | 2 +-
libc/src/stdio/CMakeLists.txt | 4 ++--
libc/src/stdio/printf.cpp | 6 +++---
libc/src/stdio/printf_core/vfprintf_internal.h | 6 +++---
libc/src/stdio/scanf.cpp | 6 +++---
libc/src/stdio/scanf_core/vfscanf_internal.h | 12 +++++++++---
libc/src/stdio/vprintf.cpp | 6 +++---
libc/test/src/stdio/CMakeLists.txt | 11 ++++-------
libc/test/src/stdio/fprintf_test.cpp | 10 +++++-----
libc/test/src/stdio/fscanf_test.cpp | 10 +++++-----
libc/test/src/stdio/vfprintf_test.cpp | 10 +++++-----
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 2 +-
13 files changed, 44 insertions(+), 42 deletions(-)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index fc865dc0cbb6a57..aabfe2717cc1525 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -126,7 +126,6 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.snprintf
libc.src.stdio.vsprintf
libc.src.stdio.vsnprintf
- #TODO: Check if scanf can be enabled on aarch64
#libc.src.stdio.sscanf
#libc.src.stdio.scanf
#libc.src.stdio.fscanf
diff --git a/libc/docs/dev/printf_behavior.rst b/libc/docs/dev/printf_behavior.rst
index a5f70c10f850004..61d84d2213b91d3 100644
--- a/libc/docs/dev/printf_behavior.rst
+++ b/libc/docs/dev/printf_behavior.rst
@@ -30,7 +30,7 @@ General Flags:
These compile-time flags will change the behavior of LLVM-libc's printf when it
is compiled. Combinations of flags that are incompatible will be marked.
-LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+LIBC_COPT_STDIO_USE_SYSTEM_FILE
--------------------------------
When set, this flag changes fprintf and printf to use the FILE API from the
system's libc, instead of LLVM-libc's internal FILE API. This is set by default
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index da95533308fdf71..9c24c567b6c1d5b 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -138,7 +138,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.__support.File.platform_stdin
)
else()
- list(APPEND scanf_copts "-DLIBC_COPT_SCANF_USE_SYSTEM_FILE")
+ list(APPEND scanf_copts "-DLIBC_COPT_STDIO_USE_SYSTEM_FILE")
endif()
add_entrypoint_object(
@@ -224,7 +224,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.__support.File.platform_stdout
)
else()
- list(APPEND printf_copts "-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE")
+ list(APPEND printf_copts "-DLIBC_COPT_STDIO_USE_SYSTEM_FILE")
endif()
add_entrypoint_object(
diff --git a/libc/src/stdio/printf.cpp b/libc/src/stdio/printf.cpp
index 3aaab6977934977..c59a084353b5c1d 100644
--- a/libc/src/stdio/printf.cpp
+++ b/libc/src/stdio/printf.cpp
@@ -15,11 +15,11 @@
#include <stdarg.h>
#include <stdio.h>
-#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
#define PRINTF_STDOUT __llvm_libc::stdout
-#else // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#else // LIBC_COPT_STDIO_USE_SYSTEM_FILE
#define PRINTF_STDOUT ::stdout
-#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
namespace __llvm_libc {
diff --git a/libc/src/stdio/printf_core/vfprintf_internal.h b/libc/src/stdio/printf_core/vfprintf_internal.h
index e00248ff0ad856e..d66b53bd1bced45 100644
--- a/libc/src/stdio/printf_core/vfprintf_internal.h
+++ b/libc/src/stdio/printf_core/vfprintf_internal.h
@@ -21,7 +21,7 @@
namespace __llvm_libc {
namespace internal {
-#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
LIBC_INLINE int ferror_unlocked(FILE *f) {
return reinterpret_cast<__llvm_libc::File *>(f)->error_unlocked();
}
@@ -39,7 +39,7 @@ LIBC_INLINE size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb,
return reinterpret_cast<__llvm_libc::File *>(f)->write_unlocked(ptr,
size * nmemb);
}
-#else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE)
+#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror_unlocked(f); }
LIBC_INLINE void flockfile(::FILE *f) { ::flockfile(f); }
@@ -50,7 +50,7 @@ LIBC_INLINE size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb,
::FILE *f) {
return ::fwrite_unlocked(ptr, size, nmemb, f);
}
-#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
} // namespace internal
namespace printf_core {
diff --git a/libc/src/stdio/scanf.cpp b/libc/src/stdio/scanf.cpp
index d98cc3d58e23251..67c6e19a6ec5fd2 100644
--- a/libc/src/stdio/scanf.cpp
+++ b/libc/src/stdio/scanf.cpp
@@ -15,11 +15,11 @@
#include <stdarg.h>
#include <stdio.h>
-#ifndef LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
#define SCANF_STDIN __llvm_libc::stdin
-#else // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#else // LIBC_COPT_STDIO_USE_SYSTEM_FILE
#define SCANF_STDIN ::stdin
-#endif // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
namespace __llvm_libc {
diff --git a/libc/src/stdio/scanf_core/vfscanf_internal.h b/libc/src/stdio/scanf_core/vfscanf_internal.h
index 693230560bc4f30..d444e60f7294a59 100644
--- a/libc/src/stdio/scanf_core/vfscanf_internal.h
+++ b/libc/src/stdio/scanf_core/vfscanf_internal.h
@@ -19,7 +19,9 @@
namespace __llvm_libc {
namespace internal {
-#ifndef LIBC_COPT_SCANF_USE_SYSTEM_FILE
+
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
+
LIBC_INLINE int getc(void *f) {
unsigned char c;
auto result = reinterpret_cast<__llvm_libc::File *>(f)->read_unlocked(&c, 1);
@@ -37,7 +39,9 @@ LIBC_INLINE void ungetc(int c, void *f) {
LIBC_INLINE int ferror_unlocked(FILE *f) {
return reinterpret_cast<__llvm_libc::File *>(f)->error_unlocked();
}
-#else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE)
+
+#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
+
LIBC_INLINE int getc(void *f) { return ::getc(reinterpret_cast<::FILE *>(f)); }
LIBC_INLINE void ungetc(int c, void *f) {
@@ -45,7 +49,9 @@ LIBC_INLINE void ungetc(int c, void *f) {
}
LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror_unlocked(f); }
-#endif // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
+
} // namespace internal
namespace scanf_core {
diff --git a/libc/src/stdio/vprintf.cpp b/libc/src/stdio/vprintf.cpp
index d38bf4fecfc07d3..0789ee9cfc3c294 100644
--- a/libc/src/stdio/vprintf.cpp
+++ b/libc/src/stdio/vprintf.cpp
@@ -15,11 +15,11 @@
#include <stdarg.h>
#include <stdio.h>
-#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
#define PRINTF_STDOUT __llvm_libc::stdout
-#else // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#else // LIBC_COPT_STDIO_USE_SYSTEM_FILE
#define PRINTF_STDOUT ::stdout
-#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
namespace __llvm_libc {
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 54391b1ccba68b7..9909948e6ca36d0 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -160,7 +160,7 @@ if(LLVM_LIBC_FULL_BUILD)
set(hermetic_test_only HERMETIC_TEST_ONLY)
else()
# Else in overlay mode they use the system's FILE.
- set(fprintf_test_copts "-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE")
+ set(use_system_file "-DLIBC_COPT_STDIO_USE_SYSTEM_FILE")
endif()
add_libc_unittest(
@@ -173,7 +173,7 @@ add_libc_unittest(
libc.src.stdio.fprintf
${fprintf_test_deps}
COMPILE_OPTIONS
- ${fprintf_test_copts}
+ ${use_system_file}
)
add_libc_test(
@@ -218,7 +218,7 @@ add_libc_unittest(
libc.src.stdio.vfprintf
${fprintf_test_deps}
COMPILE_OPTIONS
- ${fprintf_test_copts}
+ ${use_system_file}
)
add_libc_test(
@@ -241,9 +241,6 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fopen
libc.src.stdio.fwrite
)
-else()
-# Else in overlay mode they use the system's FILE.
- set(fscanf_test_copts "-DLIBC_COPT_SCANF_USE_SYSTEM_FILE")
endif()
add_libc_unittest(
@@ -257,7 +254,7 @@ add_libc_unittest(
${fscanf_test_deps}
libc.src.__support.CPP.string_view
COMPILE_OPTIONS
- ${fscanf_test_copts}
+ ${use_system_file}
)
add_libc_unittest(
diff --git a/libc/test/src/stdio/fprintf_test.cpp b/libc/test/src/stdio/fprintf_test.cpp
index a521bb47eb1362a..9a3c1f2e87206e6 100644
--- a/libc/test/src/stdio/fprintf_test.cpp
+++ b/libc/test/src/stdio/fprintf_test.cpp
@@ -6,12 +6,12 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
#include "src/stdio/fclose.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
-#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
#include "src/stdio/fprintf.h"
@@ -20,17 +20,17 @@
#include <stdio.h>
namespace printf_test {
-#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using __llvm_libc::fclose;
using __llvm_libc::ferror;
using __llvm_libc::fopen;
using __llvm_libc::fread;
-#else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE)
+#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
using ::fclose;
using ::ferror;
using ::fopen;
using ::fread;
-#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
} // namespace printf_test
TEST(LlvmLibcFPrintfTest, WriteToFile) {
diff --git a/libc/test/src/stdio/fscanf_test.cpp b/libc/test/src/stdio/fscanf_test.cpp
index 35dd32dd8bf3fe0..efb8f31ff11b56f 100644
--- a/libc/test/src/stdio/fscanf_test.cpp
+++ b/libc/test/src/stdio/fscanf_test.cpp
@@ -8,12 +8,12 @@
#include "src/__support/CPP/string_view.h"
-#ifndef LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
#include "src/stdio/fclose.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fwrite.h"
-#endif // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
#include "src/stdio/fscanf.h"
@@ -22,17 +22,17 @@
#include <stdio.h>
namespace scanf_test {
-#ifndef LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using __llvm_libc::fclose;
using __llvm_libc::ferror;
using __llvm_libc::fopen;
using __llvm_libc::fwrite;
-#else // defined(LIBC_COPT_SCANF_USE_SYSTEM_FILE)
+#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
using ::fclose;
using ::ferror;
using ::fopen;
using ::fwrite;
-#endif // LIBC_COPT_SCANF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
} // namespace scanf_test
TEST(LlvmLibcFScanfTest, WriteToFile) {
diff --git a/libc/test/src/stdio/vfprintf_test.cpp b/libc/test/src/stdio/vfprintf_test.cpp
index 31facdb6b162126..49c40e0274127a6 100644
--- a/libc/test/src/stdio/vfprintf_test.cpp
+++ b/libc/test/src/stdio/vfprintf_test.cpp
@@ -10,12 +10,12 @@
// because these functions are identical in every way except for how the varargs
// are passed.
-#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
#include "src/stdio/fclose.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fread.h"
-#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
#include "src/stdio/vfprintf.h"
@@ -24,17 +24,17 @@
#include <stdio.h>
namespace printf_test {
-#ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using __llvm_libc::fclose;
using __llvm_libc::ferror;
using __llvm_libc::fopen;
using __llvm_libc::fread;
-#else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE)
+#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
using ::fclose;
using ::ferror;
using ::fopen;
using ::fread;
-#endif // LIBC_COPT_PRINTF_USE_SYSTEM_FILE
+#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
} // namespace printf_test
int call_vfprintf(::FILE *__restrict stream, const char *__restrict format,
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index e2fa5ae34188e6d..a2aa47ce74e1fcc 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -25,7 +25,7 @@ package(
licenses(["notice"])
PRINTF_COPTS = [
- "LIBC_COPT_PRINTF_USE_SYSTEM_FILE",
+ "LIBC_COPT_STDIO_USE_SYSTEM_FILE",
"LIBC_COPT_PRINTF_DISABLE_INDEX_MODE",
"LIBC_COPT_PRINTF_DISABLE_WRITE_INT",
]
>From 3559e7a47bb1375179643894b44a67cc9e0a3105 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Fri, 22 Sep 2023 12:49:01 -0700
Subject: [PATCH 3/3] formatting cleanup prior to landing
---
libc/docs/dev/printf_behavior.rst | 2 +-
libc/src/stdio/scanf_core/vfscanf_internal.h | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/libc/docs/dev/printf_behavior.rst b/libc/docs/dev/printf_behavior.rst
index 61d84d2213b91d3..29b1b17ebaecb0a 100644
--- a/libc/docs/dev/printf_behavior.rst
+++ b/libc/docs/dev/printf_behavior.rst
@@ -31,7 +31,7 @@ These compile-time flags will change the behavior of LLVM-libc's printf when it
is compiled. Combinations of flags that are incompatible will be marked.
LIBC_COPT_STDIO_USE_SYSTEM_FILE
---------------------------------
+-------------------------------
When set, this flag changes fprintf and printf to use the FILE API from the
system's libc, instead of LLVM-libc's internal FILE API. This is set by default
when LLVM-libc is built in overlay mode.
diff --git a/libc/src/stdio/scanf_core/vfscanf_internal.h b/libc/src/stdio/scanf_core/vfscanf_internal.h
index d444e60f7294a59..68d03d02663b970 100644
--- a/libc/src/stdio/scanf_core/vfscanf_internal.h
+++ b/libc/src/stdio/scanf_core/vfscanf_internal.h
@@ -26,9 +26,9 @@ LIBC_INLINE int getc(void *f) {
unsigned char c;
auto result = reinterpret_cast<__llvm_libc::File *>(f)->read_unlocked(&c, 1);
size_t r = result.value;
- if (result.has_error() || r != 1) {
+ if (result.has_error() || r != 1)
return '\0';
- }
+
return c;
}
@@ -61,9 +61,9 @@ LIBC_INLINE int vfscanf_internal(::FILE *__restrict stream,
internal::ArgList &args) {
scanf_core::Reader reader(stream, &internal::getc, internal::ungetc);
int retval = scanf_core::scanf_main(&reader, format, args);
- if (retval == 0 && internal::ferror_unlocked(stream)) {
+ if (retval == 0 && internal::ferror_unlocked(stream))
return EOF;
- }
+
return retval;
}
} // namespace scanf_core
More information about the llvm-commits
mailing list