From 97575a239b8f4d87a8f7ad5bb324c8e71c02d4d9 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 26 Mar 2021 10:26:10 +0200 Subject: clang-tidy fixes --- .clang-tidy | 2 +- libk/makefile | 5 +++-- libk/stdlib.h | 2 +- libk/stdlib/console.cc | 4 ++-- libk/stdlib/memory.cc | 42 ------------------------------------------ libk/stdlib/quicksort.h | 31 ++++++++++++++++++++----------- libk/stdlib/virtual.cc | 6 ++++++ libk/string.h | 8 ++++---- libk/string/memory.cc | 37 +++++++++++++++++++++++++++++++++++++ libk/string/string.cc | 26 -------------------------- libk/test/quicksort.cc | 21 +++++++++++++++++---- makefile | 5 ++++- src/cpu/cpu.h | 14 +++++++------- src/kernel.cc | 4 ++-- src/makefile | 2 +- 15 files changed, 105 insertions(+), 104 deletions(-) delete mode 100644 libk/stdlib/memory.cc create mode 100644 libk/stdlib/virtual.cc create mode 100644 libk/string/memory.cc delete mode 100644 libk/string/string.cc diff --git a/.clang-tidy b/.clang-tidy index bf7aa57..db91f1c 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,5 @@ --- -Checks: '-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*,performance-*,portability-*,readability-*' +Checks: '-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*,performance-*,portability-*,readability-*,-readability-braces-around-statements,-cppcoreguidelines-avoid-c-arrays' WarningsAsErrors: '' HeaderFilterRegex: '.*' AnalyzeTemporaryDtors: false diff --git a/libk/makefile b/libk/makefile index 9063f72..f3b9aeb 100644 --- a/libk/makefile +++ b/libk/makefile @@ -1,5 +1,5 @@ -CXX_OBJ = string/string.o string/integerview.o \ - stdlib/abort.o stdlib/console.o stdlib/memory.o +CXX_OBJ = string/integerview.o string/memory.o \ + stdlib/abort.o stdlib/console.o stdlib/virtual.o CXX_OBJ := $(addprefix $(OBJ_DIR)/, $(CXX_OBJ)) CXX_DEP = $(CXX_OBJ:%.o=%.d) CXX_JSON = $(CXX_OBJ:.o=.json) @@ -30,6 +30,7 @@ $(TEST_CXX_OBJ): $(OBJ_DIR)/%.o : %.cc @$(TEST_CXX) -target $(TARGET) $(TEST_CXX_FLAGS) $(CXX_INCLUDE) -MMD -c $< -o $@ compile_commands.json: $(CXX_JSON) + @echo " GEN $@" @echo [ > $@ @cat $(CXX_JSON) >> $@ @echo ] >> $@ diff --git a/libk/stdlib.h b/libk/stdlib.h index fe8736d..6fab4c5 100644 --- a/libk/stdlib.h +++ b/libk/stdlib.h @@ -62,7 +62,7 @@ void printk(const T& a, const Args&... x) { } } -extern "C" __attribute__((__noreturn__)) void abort(); +extern "C" [[noreturn]] void abort(); #define runtime_assert(x, msg) \ if (!x) { \ diff --git a/libk/stdlib/console.cc b/libk/stdlib/console.cc index c076297..3054120 100644 --- a/libk/stdlib/console.cc +++ b/libk/stdlib/console.cc @@ -1,8 +1,8 @@ #include "stdlib.h" constexpr size_t max_consoles = 4; -static Console* global_console[max_consoles] = {nullptr}; -static size_t last_console = 0; +Console* global_console[max_consoles] = {nullptr}; +size_t last_console = 0; void Console::set(Console* ptr) { if (ptr != nullptr && last_console < max_consoles) { diff --git a/libk/stdlib/memory.cc b/libk/stdlib/memory.cc deleted file mode 100644 index b5c8f6d..0000000 --- a/libk/stdlib/memory.cc +++ /dev/null @@ -1,42 +0,0 @@ -#include - -extern "C" void __cxa_pure_virtual() { - printk("__cxa_pure_virtual\n"); - abort(); -} - -extern "C" void* memcpy(void* dstptr, const void* srcptr, const size_t n) { - auto* dst = static_cast(dstptr); - const auto* src = static_cast(srcptr); - for (size_t i = 0; i < n; ++i) dst[i] = src[i]; - return dstptr; -} - -extern "C" void* memset(void* bufptr, const int value, const size_t n) { - auto* buf = static_cast(bufptr); - for (size_t i = 0; i < n; ++i) buf[i] = static_cast(value); - return bufptr; -} - -extern "C" void* memmove(void* dstptr, const void* srcptr, const size_t n) { - auto* dst = static_cast(dstptr); - const auto* src = static_cast(srcptr); - if (dst < src) { - for (size_t i = 0; i < n; ++i) dst[i] = src[i]; - } else { - for (size_t i = n; i != 0; i--) dst[i - 1] = src[i - 1]; - } - return dstptr; -} - -extern "C" int memcmp(const void* aptr, const void* bptr, const size_t n) { - const auto* a = static_cast(aptr); - const auto* b = static_cast(bptr); - for (size_t i = 0; i < n; ++i) { - if (a[i] < b[i]) - return -1; - else if (b[i] < a[i]) - return 1; - } - return 0; -} diff --git a/libk/stdlib/quicksort.h b/libk/stdlib/quicksort.h index 632a962..77b437b 100644 --- a/libk/stdlib/quicksort.h +++ b/libk/stdlib/quicksort.h @@ -2,6 +2,8 @@ #include +enum struct SortOrder { Ascending, Descending }; + template constexpr void swap(T& l, T& r) { const T t = r; @@ -24,7 +26,7 @@ constexpr void qsort_pivot(T a[], const size_t begin, const size_t end) { * Quicksort Lomuend partition scheme * The pivot is assumed end be the last position */ -template +template constexpr auto qsort_partition(T a[], const size_t begin, const size_t end) { struct { size_t left; @@ -39,10 +41,17 @@ constexpr auto qsort_partition(T a[], const size_t begin, const size_t end) { // - place any value less than the pivot on the left // - place any value equal end the pivot on the right for (auto j = begin; j < p; ++j) { - if (a[j] < pivot) - swap(a[j], a[i++]); - else if (a[j] == pivot) - swap(a[j], a[--p]); + if constexpr (order == SortOrder::Ascending) { + if (a[j] < pivot) + swap(a[j], a[i++]); + else if (a[j] == pivot) + swap(a[j], a[--p]); + } else if constexpr (order == SortOrder::Descending) { + if (a[j] > pivot) + swap(a[j], a[i++]); + else if (a[j] == pivot) + swap(a[j], a[--p]); + } } // We now have all the values less than the pivot left of i, and all the pivots right of p @@ -57,7 +66,7 @@ constexpr auto qsort_partition(T a[], const size_t begin, const size_t end) { /** * Sort an array in place */ -template +template constexpr void qsort(T a[], const size_t begin, const size_t end) { if (begin >= end) return; @@ -67,15 +76,15 @@ constexpr void qsort(T a[], const size_t begin, const size_t end) { // Reorder the array so that all elements less than the pivot are left of it, and all elements greater than the // pivot are end the right. Equal elements can go either way. After partitioning, the pivot is in its final // position. - const auto limit = qsort_partition(a, begin, end); + const auto limit = qsort_partition(a, begin, end); // Recursively sort the left and right sub-arrays // Sort the shorter subarray first if ((limit.left - begin) < (end - limit.right)) { - qsort(a, begin, limit.left); - qsort(a, limit.right, end); + qsort(a, begin, limit.left); + qsort(a, limit.right, end); } else { - qsort(a, limit.right, end); - qsort(a, begin, limit.left); + qsort(a, limit.right, end); + qsort(a, begin, limit.left); } } diff --git a/libk/stdlib/virtual.cc b/libk/stdlib/virtual.cc new file mode 100644 index 0000000..880b22e --- /dev/null +++ b/libk/stdlib/virtual.cc @@ -0,0 +1,6 @@ +#include + +extern "C" void __cxa_pure_virtual() { // NOLINT + printk("__cxa_pure_virtual called, aborting...\n"); + abort(); +} diff --git a/libk/string.h b/libk/string.h index d0a0b32..7c0afb9 100644 --- a/libk/string.h +++ b/libk/string.h @@ -2,10 +2,10 @@ #include extern "C" { -void* memcpy(void* dstptr, const void* srcptr, const size_t n); -void* memset(void* bufptr, const int value, const size_t n); -void* memmove(void* dstptr, const void* srcptr, const size_t n); -int memcmp(const void* aptr, const void* bptr, const size_t n); +void* memcpy(void* dstptr, const void* srcptr, size_t n); +void* memset(void* bufptr, int value, size_t n); +void* memmove(void* dstptr, const void* srcptr, size_t n); +int memcmp(const void* aptr, const void* bptr, size_t n); } /** diff --git a/libk/string/memory.cc b/libk/string/memory.cc new file mode 100644 index 0000000..5b8a1f8 --- /dev/null +++ b/libk/string/memory.cc @@ -0,0 +1,37 @@ +#include + +extern "C" void* memcpy(void* dstptr, const void* srcptr, const size_t n) { + auto* dst = static_cast(dstptr); + const auto* src = static_cast(srcptr); + for (size_t i = 0; i < n; ++i) dst[i] = src[i]; + return dstptr; +} + +extern "C" void* memset(void* bufptr, const int value, const size_t n) { + auto* buf = static_cast(bufptr); + for (size_t i = 0; i < n; ++i) buf[i] = static_cast(value); + return bufptr; +} + +extern "C" void* memmove(void* dstptr, const void* srcptr, const size_t n) { + auto* dst = static_cast(dstptr); + const auto* src = static_cast(srcptr); + if (dst < src) { + for (size_t i = 0; i < n; ++i) dst[i] = src[i]; + } else { + for (size_t i = n; i != 0; i--) dst[i - 1] = src[i - 1]; + } + return dstptr; +} + +extern "C" int memcmp(const void* aptr, const void* bptr, const size_t n) { + const auto* a = static_cast(aptr); + const auto* b = static_cast(bptr); + for (size_t i = 0; i < n; ++i) { + if (a[i] < b[i]) + return -1; + else if (b[i] < a[i]) + return 1; + } + return 0; +} diff --git a/libk/string/string.cc b/libk/string/string.cc deleted file mode 100644 index c75feb1..0000000 --- a/libk/string/string.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include "string.h" - -// TODO - -// int strcpy(char *dst, const char *stc); -// void strcat(void *dst, const void *src); -// char* strncpy(char *dest, const char *src, int length); -// int strncmp(const char *s1, const char *s2, int c); - -/* strcmp - compare two C-strings */ -constexpr int strcmp(const char* s1, const char* s2) { - const auto s1_len = strlen(s1); - for (size_t i = 0; i < s1_len; ++i) { - if (s1[i] == s2[i]) - continue; - if (s1[i] > s2[i]) - return 1; - if (s1[i] < s2[i]) - return -1; - } - return 0; -} - -static_assert(strcmp("one", "one") == 0); -static_assert(strcmp("one", "two") < 0); -static_assert(strcmp("foo", "bar") > 0); diff --git a/libk/test/quicksort.cc b/libk/test/quicksort.cc index 582e49e..7a25c08 100644 --- a/libk/test/quicksort.cc +++ b/libk/test/quicksort.cc @@ -1,9 +1,14 @@ #include -constexpr bool is_sorted(int a[], size_t from, size_t to) { +constexpr bool is_sorted_asc(int a[], size_t from, size_t to) { for (; from < to - 1; ++from) - if (a[from] > a[from + 1]) - return false; + if (a[from] > a[from + 1]) return false; + return true; +} + +constexpr bool is_sorted_desc(int a[], size_t from, size_t to) { + for (; from < to - 1; ++from) + if (a[from] < a[from + 1]) return false; return true; } @@ -12,5 +17,13 @@ static_assert([]() { const size_t a_len = sizeof(a) / sizeof(int); qsort(a, 0, a_len - 1); - return is_sorted(a, 0, a_len - 1); + return is_sorted_asc(a, 0, a_len - 1); +}()); + +static_assert([]() { + int a[] = {12, 82, 347, 92, 74, 123, 0, 56}; + const size_t a_len = sizeof(a) / sizeof(int); + + qsort(a, 0, a_len - 1); + return is_sorted_desc(a, 0, a_len - 1); }()); diff --git a/makefile b/makefile index 742db3d..87f229f 100644 --- a/makefile +++ b/makefile @@ -2,8 +2,9 @@ include .config OBJ_DIR != echo $(CONFIG_OBJ_DIR) include toolchain.makefile -.PHONY: all clean test tidy todo run menuconfig +.PHONY: all libk clean test tidy todo run menuconfig all: $(OBJ_DIR)/src/glitch.elf +libk: $(OBJ_DIR)/libk.a LIBS := $(OBJ_DIR)/libk.a $(OBJ_DIR)/libd.a LD_FLAGS := $(LD_FLAGS) --library-path $(CURDIR)/$(OBJ_DIR) @@ -15,8 +16,10 @@ export AR AR_FLAGS AS AS_FLAGS NM SED \ $(OBJ_DIR)/src/glitch.elf: $(LIBS) @make -C src OBJ_DIR=$(CURDIR)/$(OBJ_DIR)/src $(CURDIR)/$(OBJ_DIR)/src/glitch.elf + $(OBJ_DIR)/libk.a: @make -C libk OBJ_DIR=$(CURDIR)/$(OBJ_DIR)/libk $(CURDIR)/$(OBJ_DIR)/libk.a + $(OBJ_DIR)/libd.a: @make -C drivers OBJ_DIR=$(CURDIR)/$(OBJ_DIR)/drivers $(CURDIR)/$(OBJ_DIR)/libd.a diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h index 2dc9cbe..5b4763f 100644 --- a/src/cpu/cpu.h +++ b/src/cpu/cpu.h @@ -75,14 +75,14 @@ public: CPU(); ~CPU() = default; - const char* manufacturer() const { return m_manufacturer; } - uint8_t family() const { return m_info.family == 15 ? m_info.family + m_info.family_ex : m_info.family; } - uint8_t model() const { + [[nodiscard]] const char* manufacturer() const { return m_manufacturer; } + [[nodiscard]] uint8_t family() const { return m_info.family == 15 ? m_info.family + m_info.family_ex : m_info.family; } + [[nodiscard]] uint8_t model() const { return (m_info.model == 6 || m_info.model == 15) ? (static_cast(m_info.model_ex << 4) + m_info.model) : m_info.model; } - uint8_t stepping() const { return m_info.stepping; } - auto features() const { return m_features; } + [[nodiscard]] uint8_t stepping() const { return m_info.stepping; } + [[nodiscard]] auto features() const { return m_features; } private: char m_manufacturer[13] = {'\0'}; @@ -91,10 +91,10 @@ private: uint8_t model : 4 = 0; uint8_t family : 4 = 0; uint8_t type : 2 = 0; - uint8_t reserved_1 : 2 = 0; + [[maybe_unused]] uint8_t unused_1 : 2 = 0; uint8_t model_ex : 4 = 0; uint8_t family_ex : 8 = 0; - uint8_t reserved_2 : 4 = 0; + [[maybe_unused]] uint8_t unused_2 : 4 = 0; } __attribute__((packed)) m_info; features_t m_features; diff --git a/src/kernel.cc b/src/kernel.cc index 53500bc..578344c 100644 --- a/src/kernel.cc +++ b/src/kernel.cc @@ -32,7 +32,7 @@ extern "C" { void dump_multiboot(uint32_t mb_magic, uint32_t mb_addr); void dump_gdt(); -void kernel_main(uint32_t mb_magic, const uint32_t mb_addr) { +[[noreturn]] void kernel_main(uint32_t mb_magic, const uint32_t mb_addr) { #ifdef HAS_SERIAL0 if constexpr (serial0_console) if (serial0.self_check()) Console::set(&serial0); @@ -53,7 +53,7 @@ void kernel_main(uint32_t mb_magic, const uint32_t mb_addr) { runtime_assert(core0.features().cx8, "CPU lacks cx8"); dump_address(); - // dump_gdt(); + dump_gdt(); { printk("Multiboot2 header at ", uhex{mb_addr}, '\n'); diff --git a/src/makefile b/src/makefile index 96507ce..1d85f4b 100644 --- a/src/makefile +++ b/src/makefile @@ -41,7 +41,7 @@ $(CXX_OBJ) : $(OBJ_DIR)/%.o : %.cc @$(CXX) -target $(TARGET) $(CXX_FLAGS) $(CXX_INCLUDE) -MMD -MJ$(subst .o,.json,$@) -c $< -o $@ $(OBJ_DIR)/glitch.elf: $(autogen) $(AS_OBJ) $(CXX_OBJ) linker.ld - @echo " LD $@" + @echo " LD $(@F)" @$(LD) $(LD_FLAGS) -o $@ $(AS_OBJ) $(CXX_OBJ) -lk -ld # constexpr tests -- cgit v1.2.1