[libc] Provide sys/queue.h by petrhosek · Pull Request #78081 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-libc

Author: Petr Hosek (petrhosek)

Changes

This header first appeared in 4.4BSD and is provided by a number of C libraries including Newlib. Several of our embedded projects use this header and so to make LLVM libc a drop-in replacement, we need to provide it as well.

For the initial commit, we only implement singly linked variants (SLIST and STAILQ). The doubly linked variants (LIST, TAILQ and CIRCLEQ) can be implemented in the future as needed.


Full diff: https://github.com/llvm/llvm-project/pull/78081.diff

8 Files Affected:

diff --git a/libc/config/baremetal/arm/headers.txt b/libc/config/baremetal/arm/headers.txt index e7be1fd80e8754..6ff51f9786772b 100644 --- a/libc/config/baremetal/arm/headers.txt +++ b/libc/config/baremetal/arm/headers.txt @@ -8,4 +8,5 @@ set(TARGET_PUBLIC_HEADERS libc.include.stdlib libc.include.string libc.include.strings

) diff --git a/libc/config/baremetal/riscv/headers.txt b/libc/config/baremetal/riscv/headers.txt index e7be1fd80e8754..6ff51f9786772b 100644 --- a/libc/config/baremetal/riscv/headers.txt +++ b/libc/config/baremetal/riscv/headers.txt @@ -8,4 +8,5 @@ set(TARGET_PUBLIC_HEADERS libc.include.stdlib libc.include.string libc.include.strings

) diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt index 2c2d1b9b0fd155..895996453eb5ca 100644 --- a/libc/include/CMakeLists.txt +++ b/libc/include/CMakeLists.txt @@ -345,6 +345,12 @@ add_gen_header( .llvm_libc_common_h )

+add_header(

+) + add_gen_header( sys_random DEF_FILE sys/random.h.def diff --git a/libc/include/sys/queue.h b/libc/include/sys/queue.h new file mode 100644 index 00000000000000..50019c95d994a3 --- /dev/null +++ b/libc/include/sys/queue.h @@ -0,0 +1,161 @@ +//===-- Macros defined in sys/queue.h header file -------------------------===// +// +// 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_MACROS_SYS_QUEUE_MACROS_H +#define __LLVM_LIBC_MACROS_SYS_QUEUE_MACROS_H + +#include <stddef.h> + +// Singly-linked list definitions. + +#define SLIST_HEAD(name, type) \

+#define SLIST_FIRST(head) ((head)->first) +#define SLIST_NEXT(elem, field) ((elem)->field.next) + +#define SLIST_FOREACH(var, head, field) \

+#define STAILQ_FIRST(head) ((head)->first) +#define STAILQ_NEXT(elem, field) ((elem)->field.next) + +#define STAILQ_FOREACH(var, head, field) \

diff --git a/libc/test/CMakeLists.txt b/libc/test/CMakeLists.txt index 5e8231ef27f461..f22f2b183aca92 100644 --- a/libc/test/CMakeLists.txt +++ b/libc/test/CMakeLists.txt @@ -14,6 +14,7 @@ if(LIBC_TARGET_ARCHITECTURE_IS_GPU AND return() endif()

+add_subdirectory(include) add_subdirectory(src) add_subdirectory(utils)

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt new file mode 100644 index 00000000000000..6083535e71eabc --- /dev/null +++ b/libc/test/include/CMakeLists.txt @@ -0,0 +1,3 @@ +add_custom_target(libc_include_tests) + +add_subdirectory(sys) diff --git a/libc/test/include/sys/CMakeLists.txt b/libc/test/include/sys/CMakeLists.txt new file mode 100644 index 00000000000000..40477aa65ab493 --- /dev/null +++ b/libc/test/include/sys/CMakeLists.txt @@ -0,0 +1,11 @@ +add_libc_test(

+) diff --git a/libc/test/include/sys/queue_test.cpp b/libc/test/include/sys/queue_test.cpp new file mode 100644 index 00000000000000..4d0daf6190f12d --- /dev/null +++ b/libc/test/include/sys/queue_test.cpp @@ -0,0 +1,123 @@ +//===-- Unittests for queue -----------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/__support/CPP/string.h" +#include "src/__support/char_vector.h" +#include "test/UnitTest/Test.h" + +#include <sys/queue.h> + +using LIBC_NAMESPACE::CharVector; +using LIBC_NAMESPACE::cpp::string; + +namespace LIBC_NAMESPACE { + +TEST(LlvmLibcQueueTest, SList) {