This patch adds the entrypoint for printf. With this, building a
"hello world" program with just LLVM-libc is possible.
Details
- Reviewers
sivachandra lntue - Commits
- rGad233c6047fc: [libc] add printf
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
libc/src/stdio/printf.cpp | ||
---|---|---|
35 | You don't need to cast to FILE * and call ferror. You can directly call the error method. Also, a use case like this makes me feel that the writer functions should return an int value which can be consumed by printf_main to process the error. That way, the file writer can write_unlocked and error_unlocked under a single lock/unlock. |
switch to unlocked writes with the lock aquisition happening before the parsing. Additionally track errors better.
libc/src/stdio/printf.cpp | ||
---|---|---|
33 | Nit: There is an RAII file lock: __llvm_libc::File::FileLock lock(__llvm_libc::stdout); Couple of things more:
| |
libc/src/stdio/printf_core/writer.h | ||
41 ↗ | (On Diff #435960) | If this method returns an int value, which can be negative on error, printf_main can do an early return as soon as it sees an error. If this is OK, you should do the same with the converter functions. Why is that not a preferred design? |
move shared fprintf functionality into internal function, as well as other cleanups
libc/src/stdio/printf.cpp | ||
---|---|---|
33 | For the file lock class, that isn't a public class so I can't access it here. I've moved some error handling to printf_main. I've moved the shared printf and fprintf implementation into vfprintf_internal. | |
libc/src/stdio/printf_core/writer.h | ||
41 ↗ | (On Diff #435960) | It's not a preferred design because write may be called multiple times in a single function. Int conversion uses up to 4 calls. Having an if result < 0 after each of those would be very inefficient, and more importantly not particularly helpful. Usually if a write fails, then all subsequent writes will also fail, so I think it's best to not check every write, instead check the status of the writer every loop. I've added a condition in printf_main to do just that. |
libc/src/stdio/printf.cpp | ||
---|---|---|
33 | No, that is not an overkill - you are keeping all file operations cohesively managed by the FileWriter. +1 for the vfprintf_internal refactoring. | |
libc/src/stdio/printf_core/writer.h | ||
41 ↗ | (On Diff #435960) | Normal design practice would be to return as soon as you see an error. We should follow that until we have sufficient evidence that conditionals like that are the source of a performance problem. Even if we can conclude that conditionals like that are a performance problem, a potential solution should investigate other kind of refactorings which reduce conditionals while stopping as soon as an error is detected. |
I split the file handling rewrite into its own separate patch after addressing the comments (https://reviews.llvm.org/D127773), this is now just adding true printf.
You don't need to cast to FILE * and call ferror. You can directly call the error method.
Also, a use case like this makes me feel that the writer functions should return an int value which can be consumed by printf_main to process the error. That way, the file writer can write_unlocked and error_unlocked under a single lock/unlock.