[libc-commits] [libc] [libc] Provide fnmatch.h header and fnmatch() function stub. (PR #219321)
via libc-commits
libc-commits at lists.llvm.org
Thu Aug 27 15:53:10 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Alexey Samsonov (vonosmas)
<details>
<summary>Changes</summary>
* Provide a POSIX header`<fnmatch.h>` which `fnmatch()` pattern-matching function (tailored for use on filenames)
and associated macro.
* Define this as an "experimental" entrypoint on Linux targets
* Implementation is missing at this point, but it might rely (at least in part) on TBD pattern-matching functionality that is
being built for `<regex.h>` functions.
---
Full diff: https://github.com/llvm/llvm-project/pull/219321.diff
19 Files Affected:
- (modified) libc/config/linux/aarch64/entrypoints.txt (+3)
- (modified) libc/config/linux/aarch64/headers.txt (+1)
- (modified) libc/config/linux/riscv/entrypoints.txt (+3)
- (modified) libc/config/linux/riscv/headers.txt (+1)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+3)
- (modified) libc/config/linux/x86_64/headers.txt (+1)
- (modified) libc/hdr/CMakeLists.txt (+8)
- (added) libc/hdr/fnmatch_macros.h (+27)
- (modified) libc/include/CMakeLists.txt (+9)
- (added) libc/include/fnmatch.yaml (+28)
- (modified) libc/include/llvm-libc-macros/CMakeLists.txt (+6)
- (added) libc/include/llvm-libc-macros/fnmatch-macros.h (+25)
- (modified) libc/src/CMakeLists.txt (+1)
- (added) libc/src/fnmatch/CMakeLists.txt (+11)
- (added) libc/src/fnmatch/fnmatch.cpp (+29)
- (added) libc/src/fnmatch/fnmatch.h (+25)
- (modified) libc/test/src/CMakeLists.txt (+1)
- (added) libc/test/src/fnmatch/CMakeLists.txt (+12)
- (added) libc/test/src/fnmatch/fnmatch_test.cpp (+23)
``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 68ce4dd591806..f18c2ab3b737b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1377,6 +1377,9 @@ endif()
if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # fnmatch.h entrypoints
+ libc.src.fnmatch.fnmatch
+
# net/if.h entrypoints
libc.src.net.if_freenameindex
libc.src.net.if_nameindex
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index b80ed913c5007..a298333d22b4d 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -15,6 +15,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fcntl
libc.include.features
libc.include.fenv
+ libc.include.fnmatch
libc.include.float
libc.include.grp
libc.include.inttypes
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 40fef13470322..75edcc2882d02 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1595,6 +1595,9 @@ endif()
if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # fnmatch.h entrypoints
+ libc.src.fnmatch.fnmatch
+
# net/if.h entrypoints
libc.src.net.if_freenameindex
libc.src.net.if_nameindex
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index a8e00b657b2e9..42f628d0345f9 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -15,6 +15,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fcntl
libc.include.features
libc.include.fenv
+ libc.include.fnmatch
libc.include.float
libc.include.grp
libc.include.inttypes
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 99020a03ff3f7..da7721976451a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1608,6 +1608,9 @@ endif()
if(LLVM_LIBC_ENABLE_EXPERIMENTAL_ENTRYPOINTS)
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # fnmatch.h entrypoints
+ libc.src.fnmatch.fnmatch
+
# net/if.h entrypoints
libc.src.net.if_freenameindex
libc.src.net.if_nameindex
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 06ee91983ebd5..5b23939d165a5 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -15,6 +15,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fcntl
libc.include.features
libc.include.fenv
+ libc.include.fnmatch
libc.include.float
libc.include.grp
libc.include.inttypes
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index dbc0a52d6414d..1fcb6148c7707 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -72,6 +72,14 @@ add_proxy_header_library(
libc.include.llvm-libc-macros.fenv_macros
)
+add_proxy_header_library(
+ fnmatch_macros
+ HDRS
+ fnmatch_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-macros.fnmatch_macros
+)
+
add_proxy_header_library(
inet_address_macros
HDRS
diff --git a/libc/hdr/fnmatch_macros.h b/libc/hdr/fnmatch_macros.h
new file mode 100644
index 0000000000000..f9d3f35f572eb
--- /dev/null
+++ b/libc/hdr/fnmatch_macros.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy header for fnmatch macros.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_FNMATCH_MACROS_H
+#define LLVM_LIBC_HDR_FNMATCH_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/fnmatch-macros.h"
+
+#else // Overlay mode
+
+#include <fnmatch.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_FNMATCH_MACROS_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index d44febe667cb1..cd840844e2ef6 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -126,6 +126,15 @@ add_header_macro(
.llvm-libc-types.fexcept_t
)
+add_header_macro(
+ fnmatch
+ ../libc/include/fnmatch.yaml
+ fnmatch.h
+ DEPENDS
+ .llvm_libc_common_h
+ .llvm-libc-macros.fnmatch_macros
+)
+
add_header_macro(
stdint
../libc/include/stdint.yaml
diff --git a/libc/include/fnmatch.yaml b/libc/include/fnmatch.yaml
new file mode 100644
index 0000000000000..4b4ff363c26e4
--- /dev/null
+++ b/libc/include/fnmatch.yaml
@@ -0,0 +1,28 @@
+header: fnmatch.h
+standards:
+ - posix
+macros:
+ - macro_name: FNM_NOMATCH
+ macro_header: fnmatch-macros.h
+ - macro_name: FNM_PATHNAME
+ macro_header: fnmatch-macros.h
+ - macro_name: FNM_PERIOD
+ macro_header: fnmatch-macros.h
+ - macro_name: FNM_NOESCAPE
+ macro_header: fnmatch-macros.h
+ - macro_name: FNM_CASEFOLD
+ macro_header: fnmatch-macros.h
+ - macro_name: FNM_IGNORECASE
+ macro_header: fnmatch-macros.h
+types: []
+enums: []
+objects: []
+functions:
+ - name: fnmatch
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: const char *
+ - type: const char *
+ - type: int
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index b23b9626557b8..9e31b44f67413 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -117,6 +117,12 @@ add_macro_header(
fenv-macros.h
)
+add_macro_header(
+ fnmatch_macros
+ HDR
+ fnmatch-macros.h
+)
+
add_macro_header(
file_seek_macros
HDR
diff --git a/libc/include/llvm-libc-macros/fnmatch-macros.h b/libc/include/llvm-libc-macros/fnmatch-macros.h
new file mode 100644
index 0000000000000..b0046377b0e78
--- /dev/null
+++ b/libc/include/llvm-libc-macros/fnmatch-macros.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Macros for POSIX fnmatch.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_FNMATCH_MACROS_H
+#define LLVM_LIBC_MACROS_FNMATCH_MACROS_H
+
+#define FNM_NOMATCH 1
+
+#define FNM_PATHNAME (1 << 0)
+#define FNM_NOESCAPE (1 << 1)
+#define FNM_PERIOD (1 << 2)
+#define FNM_CASEFOLD (1 << 4)
+#define FNM_IGNORECASE FNM_CASEFOLD
+
+#endif // LLVM_LIBC_MACROS_FNMATCH_MACROS_H
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 4e7aaa1112ed7..cfd3f1fd9730e 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -6,6 +6,7 @@ add_subdirectory(ctype)
add_subdirectory(dlfcn)
add_subdirectory(errno)
add_subdirectory(fenv)
+add_subdirectory(fnmatch)
add_subdirectory(inttypes)
add_subdirectory(libgen)
add_subdirectory(link)
diff --git a/libc/src/fnmatch/CMakeLists.txt b/libc/src/fnmatch/CMakeLists.txt
new file mode 100644
index 0000000000000..58bc52436fdbb
--- /dev/null
+++ b/libc/src/fnmatch/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_entrypoint_object(
+ fnmatch
+ SRCS
+ fnmatch.cpp
+ HDRS
+ fnmatch.h
+ DEPENDS
+ libc.hdr.fnmatch_macros
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
diff --git a/libc/src/fnmatch/fnmatch.cpp b/libc/src/fnmatch/fnmatch.cpp
new file mode 100644
index 0000000000000..d706a54161e0b
--- /dev/null
+++ b/libc/src/fnmatch/fnmatch.cpp
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the implementation of the fnmatch function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/fnmatch/fnmatch.h"
+#include "hdr/fnmatch_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, fnmatch,
+ ([[maybe_unused]] const char *pattern,
+ [[maybe_unused]] const char *string,
+ [[maybe_unused]] int flags)) {
+ // Always return FNM_NOMATCH for now.
+ return FNM_NOMATCH;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/fnmatch/fnmatch.h b/libc/src/fnmatch/fnmatch.h
new file mode 100644
index 0000000000000..fe9ca2ecd4bde
--- /dev/null
+++ b/libc/src/fnmatch/fnmatch.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for fnmatch.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_FNMATCH_FNMATCH_H
+#define LLVM_LIBC_SRC_FNMATCH_FNMATCH_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int fnmatch(const char *pattern, const char *string, int flags);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_FNMATCH_FNMATCH_H
diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index cdd262aa36473..2f838aa5bd40c 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -73,6 +73,7 @@ add_subdirectory(complex)
add_subdirectory(ctype)
add_subdirectory(errno)
add_subdirectory(fenv)
+add_subdirectory(fnmatch)
add_subdirectory(libgen)
add_subdirectory(link)
add_subdirectory(math)
diff --git a/libc/test/src/fnmatch/CMakeLists.txt b/libc/test/src/fnmatch/CMakeLists.txt
new file mode 100644
index 0000000000000..a244ad4c7fa15
--- /dev/null
+++ b/libc/test/src/fnmatch/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_custom_target(libc-fnmatch-tests)
+
+add_libc_test(
+ fnmatch_test
+ SUITE
+ libc-fnmatch-tests
+ SRCS
+ fnmatch_test.cpp
+ DEPENDS
+ libc.hdr.fnmatch_macros
+ libc.src.fnmatch.fnmatch
+)
diff --git a/libc/test/src/fnmatch/fnmatch_test.cpp b/libc/test/src/fnmatch/fnmatch_test.cpp
new file mode 100644
index 0000000000000..e33fd3653ef67
--- /dev/null
+++ b/libc/test/src/fnmatch/fnmatch_test.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for fnmatch.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fnmatch_macros.h"
+#include "src/fnmatch/fnmatch.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcFnmatchTest, StubAlwaysReturnsNoMatch) {
+ EXPECT_EQ(LIBC_NAMESPACE::fnmatch("test", "test", 0), FNM_NOMATCH);
+ EXPECT_EQ(LIBC_NAMESPACE::fnmatch("*", "anything", 0), FNM_NOMATCH);
+ EXPECT_EQ(LIBC_NAMESPACE::fnmatch("a", "a", FNM_PATHNAME | FNM_PERIOD),
+ FNM_NOMATCH);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/219321
More information about the libc-commits
mailing list