[libc-commits] [libc] [libc] Refactor getopt to use standard reset and internal test hook (PR #197644)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Thu May 14 11:58:29 PDT 2026


================
@@ -16,8 +16,15 @@
 namespace LIBC_NAMESPACE_DECL {
 
 namespace impl {
-void set_getopt_state(char **, int *, int *, unsigned *, int *, FILE *);
+extern "C" {
+extern char *optarg;
+extern int optind;
+extern int optopt;
+extern int opterr;
 }
+} // namespace impl
+
+void __llvm_libc_getopt_set_errorstream(FILE *);
----------------
vonosmas wrote:

I'm confused a bit on why you need a dedicated entrypoint for this? If you declare this function in a private llvm-libc implementation header, and you include this header from unit tests, then you call simply call this function as a "regular" C++ function `LIBC_NAMESPACE::__llvm_libc_getopt_set_errorstream(errstream);` (which you already do) in the unit tests.

Why is making it an entrypoint and wrapping it in LLVM_LIBC_FUNCTION exporting macro really needed?

https://github.com/llvm/llvm-project/pull/197644


More information about the libc-commits mailing list