Skip to content

Commit

Permalink
Merge pull request #1176 from 64/clang-format
Browse files Browse the repository at this point in the history
meta: add clang-format and reformat managarm sysdeps
  • Loading branch information
64 authored Nov 30, 2024
2 parents 7299994 + c2e79bc commit 4b21d4f
Show file tree
Hide file tree
Showing 38 changed files with 3,340 additions and 3,100 deletions.
36 changes: 36 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
BasedOnStyle: LLVM
Language: Cpp

ColumnLimit: 100
IndentWidth: 4
TabWidth: 4
UseTab: ForIndentation

ContinuationIndentWidth: 4
ConstructorInitializerIndentWidth: 0
IndentCaseLabels: true
# Do not indent "public", "private", etc.
AccessModifierOffset: -4

AlignAfterOpenBracket: BlockIndent

# Put template<> on its own line.
BreakTemplateDeclarations: Yes
# Put long return types on their own line.
BreakAfterReturnType: Automatic
# Put binary operators at the start of the line.
BreakBeforeBinaryOperators: NonAssignment

# Put all initializers onto their own lines.
PackConstructorInitializers: CurrentLine

# Put all function arguments on their own lines.
BinPackArguments: false
BinPackParameters: false

# Format braces as {1, 2, 3}, not as { 1, 2, 3 }.
Cpp11BracedListStyle: true

# Force \n at EOF.
InsertNewlineAtEOF: true
21 changes: 21 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: pre-commit

on: [merge_group, pull_request]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# Check .clang-format is up to date
- uses: actions/checkout@v4
with:
repository: 'managarm/common-config'
path: common-config
- run: 'cmp .clang-format common-config/.clang-format'
name: 'Check .clang-format is up to date'

# Check formatting
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fail_fast: false
repos:
- repo: https://github.com/pocc/pre-commit-hooks
rev: 336fdd7c3cab698ead0b1c95157b9e74d3906b62
hooks:
- id: clang-format
additional_dependencies: [clang-format==19.1.3]
args: ["-i"]
exclude: >
(?x)^(
options/ansi/musl-generic-math/.*|
options/ansi/generic/complex/.*|
options/posix/musl-generic-regex/.*
)
# Only enable on Managarm sysdeps for now.
files:
sysdeps/managarm/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ The type of library to be built (static, shared, or both) is controlled by meson

We also support building with `-Db_sanitize=undefined` to use UBSan inside mlibc. Note that this does not enable UBSan for external applications which link against `libc.so`, but it can be useful during development to detect internal bugs (e.g when adding new sysdeps).

## Running pre-commit hooks

To format your code before submitting a PR, you should install [`pre-commit`](https://pre-commit.com/). Then do `pre-commit install` to install the git hook which runs each time you commit. Alternatively, you can do `pre-commit run -a` to manually format all files.

## Running Tests

The `mlibc` test suite can be run under a Linux host. To do this, first install a set of kernel headers (as described [here](https://docs.kernel.org/kbuild/headers_install.html)), then run from the project root:
Expand Down
48 changes: 24 additions & 24 deletions options/ansi/generic/stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ static void store_int(void *dest, unsigned int size, unsigned long long i) {

template<typename H>
static int do_scanf(H &handler, const char *fmt, __builtin_va_list args) {
#define NOMATCH_CHECK(cond) if(cond) return match_count // if cond is true, matching error
#define EOF_CHECK(cond) if(cond) return match_count ? match_count : EOF // if cond is true, no more data to read
#define NOMATCH_CHECK(cond) ({ if(cond) return match_count; }) // if cond is true, matching error
#define EOF_CHECK(cond) ({ if(cond) return match_count ? match_count : EOF; }) // if cond is true, no more data to read
int match_count = 0;
for (; *fmt; fmt++) {
if (isspace(*fmt)) {
Expand Down Expand Up @@ -891,20 +891,20 @@ int vsscanf(const char *__restrict buffer, const char *__restrict format, __buil
return result;
}

int fwprintf(FILE *__restrict, const wchar_t *__restrict, ...) MLIBC_STUB_BODY
int fwscanf(FILE *__restrict, const wchar_t *__restrict, ...) MLIBC_STUB_BODY
int vfwprintf(FILE *__restrict, const wchar_t *__restrict, __builtin_va_list) MLIBC_STUB_BODY
int vfwscanf(FILE *__restrict, const wchar_t *__restrict, __builtin_va_list) MLIBC_STUB_BODY
int fwprintf(FILE *__restrict, const wchar_t *__restrict, ...) { MLIBC_STUB_BODY; }
int fwscanf(FILE *__restrict, const wchar_t *__restrict, ...) { MLIBC_STUB_BODY; }
int vfwprintf(FILE *__restrict, const wchar_t *__restrict, __builtin_va_list) { MLIBC_STUB_BODY; }
int vfwscanf(FILE *__restrict, const wchar_t *__restrict, __builtin_va_list) { MLIBC_STUB_BODY; }

int swprintf(wchar_t *__restrict, size_t, const wchar_t *__restrict, ...) MLIBC_STUB_BODY
int swscanf(wchar_t *__restrict, const wchar_t *__restrict, ...) MLIBC_STUB_BODY
int vswprintf(wchar_t *__restrict, size_t, const wchar_t *__restrict, __builtin_va_list) MLIBC_STUB_BODY
int vswscanf(wchar_t *__restrict, const wchar_t *__restrict, __builtin_va_list) MLIBC_STUB_BODY
int swprintf(wchar_t *__restrict, size_t, const wchar_t *__restrict, ...) { MLIBC_STUB_BODY; }
int swscanf(wchar_t *__restrict, const wchar_t *__restrict, ...) { MLIBC_STUB_BODY; }
int vswprintf(wchar_t *__restrict, size_t, const wchar_t *__restrict, __builtin_va_list) { MLIBC_STUB_BODY; }
int vswscanf(wchar_t *__restrict, const wchar_t *__restrict, __builtin_va_list) { MLIBC_STUB_BODY; }

int wprintf(const wchar_t *__restrict, ...) MLIBC_STUB_BODY
int wscanf(const wchar_t *__restrict, ...) MLIBC_STUB_BODY
int vwprintf(const wchar_t *__restrict, __builtin_va_list) MLIBC_STUB_BODY
int vwscanf(const wchar_t *__restrict, __builtin_va_list) MLIBC_STUB_BODY
int wprintf(const wchar_t *__restrict, ...) { MLIBC_STUB_BODY; }
int wscanf(const wchar_t *__restrict, ...) { MLIBC_STUB_BODY; }
int vwprintf(const wchar_t *__restrict, __builtin_va_list) { MLIBC_STUB_BODY; }
int vwscanf(const wchar_t *__restrict, __builtin_va_list) { MLIBC_STUB_BODY; }

int fgetc(FILE *stream) {
char c;
Expand Down Expand Up @@ -1014,16 +1014,16 @@ int puts(const char *string) {
return 1;
}

wint_t fgetwc(FILE *) MLIBC_STUB_BODY
wchar_t *fgetws(wchar_t *__restrict, int, FILE *__restrict) MLIBC_STUB_BODY
wint_t fputwc(wchar_t, FILE *) MLIBC_STUB_BODY
int fputws(const wchar_t *__restrict, FILE *__restrict) MLIBC_STUB_BODY
int fwide(FILE *, int) MLIBC_STUB_BODY
wint_t getwc(FILE *) MLIBC_STUB_BODY
wint_t getwchar(void) MLIBC_STUB_BODY
wint_t putwc(wchar_t, FILE *) MLIBC_STUB_BODY
wint_t putwchar(wchar_t) MLIBC_STUB_BODY
wint_t ungetwc(wint_t, FILE *) MLIBC_STUB_BODY
wint_t fgetwc(FILE *) { MLIBC_STUB_BODY; }
wchar_t *fgetws(wchar_t *__restrict, int, FILE *__restrict) { MLIBC_STUB_BODY; }
wint_t fputwc(wchar_t, FILE *) { MLIBC_STUB_BODY; }
int fputws(const wchar_t *__restrict, FILE *__restrict) { MLIBC_STUB_BODY; }
int fwide(FILE *, int) { MLIBC_STUB_BODY; }
wint_t getwc(FILE *) { MLIBC_STUB_BODY; }
wint_t getwchar(void) { MLIBC_STUB_BODY; }
wint_t putwc(wchar_t, FILE *) { MLIBC_STUB_BODY; }
wint_t putwchar(wchar_t) { MLIBC_STUB_BODY; }
wint_t ungetwc(wint_t, FILE *) { MLIBC_STUB_BODY; }

size_t fread(void *buffer, size_t size, size_t count, FILE *file_base) {
auto file = static_cast<mlibc::abstract_file *>(file_base);
Expand Down
24 changes: 12 additions & 12 deletions options/ansi/generic/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ char *strchrnul(const char *s, int c) {
return const_cast<char *>(s + i);
}

double wcstod(const wchar_t *__restrict, wchar_t **__restrict) MLIBC_STUB_BODY
float wcstof(const wchar_t *__restrict, wchar_t **__restrict) MLIBC_STUB_BODY
long double wcstold(const wchar_t *__restrict, wchar_t **__restrict) MLIBC_STUB_BODY
double wcstod(const wchar_t *__restrict, wchar_t **__restrict) { MLIBC_STUB_BODY; }
float wcstof(const wchar_t *__restrict, wchar_t **__restrict) { MLIBC_STUB_BODY; }
long double wcstold(const wchar_t *__restrict, wchar_t **__restrict) { MLIBC_STUB_BODY; }

long wcstol(const wchar_t *__restrict nptr, wchar_t **__restrict endptr, int base) {
return mlibc::stringToInteger<long, wchar_t>(nptr, endptr, base);
Expand Down Expand Up @@ -277,16 +277,16 @@ wchar_t *wcscat(wchar_t *__restrict dest, const wchar_t *__restrict src) {
return dest;
}

wchar_t *wcsncat(wchar_t *__restrict, const wchar_t *__restrict, size_t) MLIBC_STUB_BODY
wchar_t *wcsncat(wchar_t *__restrict, const wchar_t *__restrict, size_t) { MLIBC_STUB_BODY; }

int wcscmp(const wchar_t *l, const wchar_t *r) {
for(; *l == *r && *l && *r; l++, r++);
return *l - *r;
}

int wcscoll(const wchar_t *, const wchar_t *) MLIBC_STUB_BODY
int wcsncmp(const wchar_t *, const wchar_t *, size_t) MLIBC_STUB_BODY
int wcsxfrm(wchar_t *__restrict, const wchar_t *__restrict, size_t) MLIBC_STUB_BODY
int wcscoll(const wchar_t *, const wchar_t *) { MLIBC_STUB_BODY; }
int wcsncmp(const wchar_t *, const wchar_t *, size_t) { MLIBC_STUB_BODY; }
int wcsxfrm(wchar_t *__restrict, const wchar_t *__restrict, size_t) { MLIBC_STUB_BODY; }

int wmemcmp(const wchar_t *a, const wchar_t *b, size_t size) {
for(size_t i = 0; i < size; i++) {
Expand All @@ -307,18 +307,18 @@ wchar_t *wcschr(const wchar_t *s, wchar_t c) {
return *s ? (wchar_t *)s : 0;
}

size_t wcscspn(const wchar_t *, const wchar_t *) MLIBC_STUB_BODY
wchar_t *wcspbrk(const wchar_t *, const wchar_t *) MLIBC_STUB_BODY
size_t wcscspn(const wchar_t *, const wchar_t *) { MLIBC_STUB_BODY; }
wchar_t *wcspbrk(const wchar_t *, const wchar_t *) { MLIBC_STUB_BODY; }

wchar_t *wcsrchr(const wchar_t *s, wchar_t c) {
const wchar_t *p;
for(p = s + wcslen(s); p >= s && *p != c; p--);
return p >= s ? (wchar_t *)p : 0;
}

size_t wcsspn(const wchar_t *, const wchar_t *) MLIBC_STUB_BODY
wchar_t *wcsstr(const wchar_t *, const wchar_t *) MLIBC_STUB_BODY
wchar_t *wcstok(wchar_t *__restrict, const wchar_t *__restrict, wchar_t **__restrict) MLIBC_STUB_BODY
size_t wcsspn(const wchar_t *, const wchar_t *) { MLIBC_STUB_BODY; }
wchar_t *wcsstr(const wchar_t *, const wchar_t *) { MLIBC_STUB_BODY; }
wchar_t *wcstok(wchar_t *__restrict, const wchar_t *__restrict, wchar_t **__restrict) { MLIBC_STUB_BODY; }

wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t size) {
auto s_bytes = s;
Expand Down
2 changes: 1 addition & 1 deletion options/ansi/generic/uchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <uchar.h>
#include <wchar.h>

size_t c32rtomb(char *, char32_t, mbstate_t *) MLIBC_STUB_BODY
size_t c32rtomb(char *, char32_t, mbstate_t *) { MLIBC_STUB_BODY; }

size_t mbrtoc32(char32_t *__restrict pc32, const char *__restrict pmb, size_t max, mbstate_t *__restrict ps) {
static mbstate_t internal_state;
Expand Down
4 changes: 2 additions & 2 deletions options/ansi/generic/wctype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#include <mlibc/debug.hpp>
#include <frg/string.hpp>

wctrans_t wctrans(const char *) MLIBC_STUB_BODY
wint_t towctrans(wint_t, wctrans_t) MLIBC_STUB_BODY
wctrans_t wctrans(const char *) { MLIBC_STUB_BODY; }
wint_t towctrans(wint_t, wctrans_t) { MLIBC_STUB_BODY; }

2 changes: 1 addition & 1 deletion options/internal/include/bits/ensure.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void __ensure_warn(const char *assertion, const char *file, unsigned int line,
sysdep; \
})

#define MLIBC_STUB_BODY { MLIBC_UNIMPLEMENTED(); __builtin_unreachable(); }
#define MLIBC_STUB_BODY ({ MLIBC_UNIMPLEMENTED(); __builtin_unreachable(); })

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion options/rtld/include/mlibc/rtld-abi.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MLIBC_RTLD_ABI
#define MLIBC_RTLD_ABI

#include <stdint.h>
#include <stddef.h>

#if defined(__x86_64__) || defined(__aarch64__) || defined(__i386__) || defined(__riscv) || defined (__m68k__)

Expand Down
2 changes: 2 additions & 0 deletions sysdeps/aero/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/astral/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/dripos/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/ironclad/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/keyronex/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/lemon/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
2 changes: 2 additions & 0 deletions sysdeps/linux/include/sys/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* (linux-headers, or an autogenerated stub while building), and an
* autogenerated file containing SYS_ defines.
*/
/* clang-format off */
#include <linux/unistd.h>
#include <bits/syscall_aliases.h>
/* clang-format on */

#endif /* _SYS_SYSCALL_H */
2 changes: 2 additions & 0 deletions sysdeps/lyre/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
DisableFormat: true
39 changes: 24 additions & 15 deletions sysdeps/managarm/aarch64/thread.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include <mlibc/thread-entry.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/tcb.hpp>
#include <bits/ensure.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdint.h>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/tcb.hpp>
#include <mlibc/thread-entry.hpp>
#include <stddef.h>
#include <stdint.h>
#include <sys/mman.h>

extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb) {
// Wait until our parent sets up the TID.
while(!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED))
while (!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED))
mlibc::sys_futex_wait(&tcb->tid, 0, nullptr);

if(mlibc::sys_tcb_set(tcb))
if (mlibc::sys_tcb_set(tcb))
__ensure(!"sys_tcb_set() failed");

tcb->invokeThreadFunc(entry, user_arg);
Expand All @@ -29,28 +29,37 @@ namespace mlibc {

static constexpr size_t default_stacksize = 0x200000;

int sys_prepare_stack(void **stack, void *entry, void *user_arg, void *tcb, size_t *stack_size, size_t *guard_size, void **stack_base) {
int sys_prepare_stack(
void **stack,
void *entry,
void *user_arg,
void *tcb,
size_t *stack_size,
size_t *guard_size,
void **stack_base
) {
if (!*stack_size)
*stack_size = default_stacksize;
*guard_size = 0;

if (*stack) {
*stack_base = *stack;
} else {
*stack_base = mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(*stack_base == MAP_FAILED) {
*stack_base =
mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (*stack_base == MAP_FAILED) {
return errno;
}
}

uintptr_t *sp = reinterpret_cast<uintptr_t *>(reinterpret_cast<uintptr_t>(*stack_base) + *stack_size);

uintptr_t *sp =
reinterpret_cast<uintptr_t *>(reinterpret_cast<uintptr_t>(*stack_base) + *stack_size);

*--sp = reinterpret_cast<uintptr_t>(tcb);
*--sp = reinterpret_cast<uintptr_t>(user_arg);
*--sp = reinterpret_cast<uintptr_t>(entry);
*stack = reinterpret_cast<void*>(sp);
*stack = reinterpret_cast<void *>(sp);
return 0;
}

} //namespace mlibc
} // namespace mlibc
Loading

0 comments on commit 4b21d4f

Please sign in to comment.