-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc]: Clean up unnecessary function pointers in scanf #121215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
885d874
12f2ae0
6236e59
d12a8f5
f3b8d7f
bfe6b86
90ce1b2
1f9e27c
1bd2181
0abb5c3
13f418d
d3c3329
fe6f638
cbe1ab0
09194c2
a459d11
6144b00
97ae074
76f6a31
12696d4
b02a039
82ccee6
6f14edc
5c45508
4539ae8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,71 @@ | |
|
|
||
| #include "src/stdio/scanf_core/reader.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| #include "src/__support/File/file.h" | ||
| #include "hdr/types/FILE.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
| namespace scanf_core { | ||
|
|
||
| namespace internal { | ||
|
|
||
| #if defined(LIBC_TARGET_ARCH_IS_GPU) | ||
| // The GPU build provides FILE access through the host operating system's | ||
| // library. So here we simply use the public entrypoints like in the SYSTEM_FILE | ||
| // interface. Entrypoints should normally not call others, this is an exception. | ||
| // FIXME: We do not acquire any locks here, so this is not thread safe. | ||
| LIBC_INLINE int getc(void *f) { | ||
| return LIBC_NAMESPACE::getc(reinterpret_cast<::FILE *>(f)); | ||
| } | ||
|
|
||
| LIBC_INLINE void ungetc(int c, void *f) { | ||
| LIBC_NAMESPACE::ungetc(c, reinterpret_cast<::FILE *>(f)); | ||
| } | ||
|
|
||
| #elif !defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) | ||
|
|
||
| LIBC_INLINE int getc(void *f) { | ||
| unsigned char c; | ||
| auto result = | ||
| reinterpret_cast<LIBC_NAMESPACE::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<LIBC_NAMESPACE::File *>(f)->ungetc_unlocked(c); | ||
| } | ||
|
|
||
| #else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE) | ||
|
|
||
| // Since ungetc_unlocked isn't always available, we don't acquire the lock for | ||
| // system files. | ||
| 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)); | ||
| } | ||
| #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE | ||
|
|
||
| } // namespace internal | ||
|
|
||
| char Reader::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>(internal::getc(input_stream)); | ||
| } | ||
|
||
|
|
||
| void Reader::ungetc(char c) { | ||
| --cur_chars_read; | ||
| if (rb != nullptr && rb->buff_cur > 0) { | ||
|
|
@@ -23,7 +83,8 @@ void Reader::ungetc(char c) { | |
| --(rb->buff_cur); | ||
| return; | ||
| } | ||
| stream_ungetc(static_cast<int>(c), input_stream); | ||
| internal::ungetc(static_cast<int>(c), input_stream); | ||
| } | ||
|
|
||
| } // namespace scanf_core | ||
| } // namespace LIBC_NAMESPACE_DECL | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is missing the dependency on
libc.src.__support.File.file, as well as the flag controlling if it's using the system file. You'll also need to set up proper handling for the GPU like whatvfscanf_internalhas.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I seem to be getting an error like (after adding the above change):
is that expected on a M1 mac?
using the build commands from:#115394 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, that's because we only support overlay mode on mac, and overlay mode doesn't have
FILE. You'll need to add logic to only addlibc.src.__support.File.fileto the list ofDEPENDSifLLVM_LIBC_FULL_BUILDis true.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I think there's a bug / typo in the CMake call there.
Instead of:
it should be:
I think is what caused my build to have the bugs with macOS' system stdio.h macros
Link to code:
llvm-project/libc/src/stdio/scanf_core/CMakeLists.txt
Lines 119 to 121 in 106c483
I can add the
COMPILE_OPTIONSin this PR if that's oK?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
COMPILE_OPTIONSshouldn't be necessary, becauseuse_system_filealready contains it, see: https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/CMakeLists.txt#L30