Browse Source

Compiling with clang works, clangd settings, use tagged noexcept new

Kajetan Johannes Hammerle 2 weeks ago
parent
commit
ffa6239baa

+ 3 - 0
.clangd

@@ -0,0 +1,3 @@
+CompileFlags:
+  Add: [-ferror-limit=0, -std=c++20, -DERROR_SIMULATOR=true]
+  CompilationDatabase: ./build_debug/

+ 3 - 2
.gitignore

@@ -1,5 +1,6 @@
-.vscode
 build_debug
 build_release
 install
-profile
+profile
+compiler
+.cache

+ 49 - 41
CMakeLists.txt

@@ -69,16 +69,59 @@ set(SRC_PERFORMANCE
 
 if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
     set(COMPILE_OPTIONS -flto)
-    set(DEBUG_LINK "")
+    set(LINK_OPTIONS -flto)
     set(LOG_LEVEL 2)
     set(ERROR_SIMULATOR "")
 else()
-    set(COMPILE_OPTIONS --coverage)
-    set(DEBUG_LINK gcov)
+    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
+        set(COMPILE_OPTIONS --coverage)
+    endif()
+    set(LINK_OPTIONS gcov)
     set(LOG_LEVEL 4)
     set(ERROR_SIMULATOR "ERROR_SIMULATOR")
 endif()
 
+if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
+    set(MORE_WARNINGS
+        -Waligned-new=all
+        -Walloc-zero
+        -Wanalyzer-too-complex
+        -Warith-conversion
+        -Warray-bounds=2
+        -Wattribute-alias=2
+        -Wbidi-chars=any
+        -Wcast-align=strict
+        -Wcatch-value=3
+        -Wcomma-subscript
+        -Wconditionally-supported
+        -Wduplicated-branches
+        -Wduplicated-cond
+        -Wformat-overflow=2
+        -Wformat-signedness
+        -Wformat-truncation=2
+        -Wimplicit-fallthrough=5
+        -Winvalid-imported-macros
+        -Wlogical-op
+        -Wmultiple-inheritance
+        -Wnoexcept
+        -Wnormalized=nfkc
+        -Wplacement-new=2
+        -Wredundant-tags
+        -Wshift-overflow=2
+        -Wstack-usage=8388608
+        -Wstrict-null-sentinel
+        -Wstringop-overflow=4
+        -Wsuggest-final-methods
+        -Wsuggest-final-types
+        -Wtrampolines
+        -Wtrivial-auto-var-init
+        -Wunused-const-variable=2
+        -Wuse-after-free=3
+        -Wvirtual-inheritance
+        -Wvolatile
+    )
+endif()
+
 add_library(core STATIC ${SRC})
 target_compile_options(core PUBLIC
     ${COMPILE_OPTIONS}
@@ -89,21 +132,10 @@ target_compile_options(core PUBLIC
     -nostdinc++
     -pedantic
     -pedantic-errors
-    -Waligned-new=all
     -Wall
     -Walloca
-    -Walloc-zero
-    -Wanalyzer-too-complex
-    -Warith-conversion
-    -Warray-bounds=2
     -Warray-parameter
-    -Wattribute-alias=2
-    -Wbidi-chars=any
-    -Wcast-align=strict
     -Wcast-qual
-    -Wcatch-value=3
-    -Wcomma-subscript
-    -Wconditionally-supported
     -Wconversion
     -Wctad-maybe-unsupported
     -Wctor-dtor-privacy
@@ -113,8 +145,6 @@ target_compile_options(core PUBLIC
     -Wdeprecated-enum-float-conversion
     -Wdisabled-optimization
     -Wdouble-promotion
-    -Wduplicated-branches
-    -Wduplicated-cond
     -Weffc++
     -Wenum-compare
     -Wenum-conversion
@@ -123,60 +153,38 @@ target_compile_options(core PUBLIC
     -Wextra-semi
     -Wfloat-equal
     -Wformat=2
-    -Wformat-overflow=2
-    -Wformat-signedness
-    -Wformat-truncation=2
     -Wframe-larger-than=8388608
-    -Wimplicit-fallthrough=5
     -Winfinite-recursion
     -Winit-self
-    -Winvalid-imported-macros
     -Winvalid-pch
     -Wlarger-than=1073741824
-    -Wlogical-op
     -Wmismatched-tags
     -Wmissing-braces
     -Wmissing-declarations
     -Wmissing-include-dirs
     -Wmultichar
-    -Wmultiple-inheritance
-    -Wnoexcept
     -Wnon-virtual-dtor
-    -Wnormalized=nfkc
     -Wnull-dereference
     -Wold-style-cast
     -Woverlength-strings
     -Woverloaded-virtual
-    -Wplacement-new=2
     -Wredundant-decls
-    -Wredundant-tags
     -Wregister
     -Wshadow
-    -Wshift-overflow=2
     -Wsign-conversion
     -Wsign-promo
     -Wstack-protector
-    -Wstack-usage=8388608
-    -Wstrict-null-sentinel
     -Wstrict-overflow=5
-    -Wstringop-overflow=4
-    -Wsuggest-final-methods
-    -Wsuggest-final-types
     -Wsuggest-override
     -Wswitch-enum
     -Wsynth
-    -Wtrampolines
-    -Wtrivial-auto-var-init
     -Wundef
     -Wunreachable-code
-    -Wunused-const-variable=2
     -Wunused-macros
-    -Wuse-after-free=3
-    -Wvirtual-inheritance
     -Wvla
-    -Wvolatile
     -Wwrite-strings
     -Wzero-as-null-pointer-constant
+    ${MORE_WARNINGS}
 )
 target_compile_definitions(core 
     PUBLIC CORE_LOG_LEVEL=${LOG_LEVEL}
@@ -184,7 +192,7 @@ target_compile_definitions(core
 )
 target_link_libraries(core 
     PUBLIC -nodefaultlibs c m
-    PRIVATE ${DEBUG_LINK}
+    PRIVATE ${LINK_OPTIONS}
 )
 target_sources(core PUBLIC 
     FILE_SET HEADERS
@@ -236,4 +244,4 @@ target_link_libraries(test PRIVATE core)
 target_compile_definitions(test PRIVATE ${ERROR_SIMULATOR})
 
 add_executable(performance ${SRC_PERFORMANCE})
-target_link_libraries(performance PRIVATE core)
+target_link_libraries(performance PRIVATE core)

+ 2 - 2
include/core/data/Array.hpp

@@ -46,7 +46,7 @@ namespace Core {
             return data + N;
         }
 
-        static consteval i64 getLength() {
+        static constexpr i64 getLength() {
             return N;
         }
 
@@ -57,4 +57,4 @@ namespace Core {
     };
 }
 
-#endif
+#endif

+ 3 - 2
include/core/data/LinkedList.hpp

@@ -2,6 +2,7 @@
 #define CORE_LINKED_LIST_HPP
 
 #include "core/utils/ArrayString.hpp"
+#include "core/utils/New.hpp"
 
 namespace Core {
     template<typename T>
@@ -81,7 +82,7 @@ namespace Core {
 
         template<typename... Args>
         check_return Error put(Node*& n, Args&&... args) {
-            n = new Node(Core::forward<Args>(args)...);
+            n = new(noThrow) Node(Core::forward<Args>(args)...);
             if(n == nullptr) {
                 return Error::OUT_OF_MEMORY;
             }
@@ -186,4 +187,4 @@ namespace Core {
 
 }
 
-#endif
+#endif

+ 3 - 3
include/core/data/List.hpp

@@ -3,7 +3,6 @@
 
 #include "core/utils/AlignedData.hpp"
 #include "core/utils/ArrayString.hpp"
-#include "core/utils/New.hpp"
 
 namespace Core {
     template<typename T>
@@ -189,7 +188,8 @@ namespace Core {
             if(n <= 0) {
                 return Error::NONE;
             }
-            t = reinterpret_cast<T*>(new AlignedType<T>[static_cast<u64>(n)]);
+            t = reinterpret_cast<T*>(new(noThrow)
+                                         AlignedType<T>[static_cast<u64>(n)]);
             return t == nullptr ? Error::OUT_OF_MEMORY : Error::NONE;
         }
 
@@ -218,4 +218,4 @@ namespace Core {
     };
 }
 
-#endif
+#endif

+ 4 - 2
include/core/data/ProbingHashMap.hpp

@@ -6,6 +6,7 @@
 #include "core/utils/ArrayString.hpp"
 #include "core/utils/HashCode.hpp"
 #include "core/utils/Logger.hpp"
+#include "core/utils/New.hpp"
 
 namespace Core {
     template<typename K, typename V>
@@ -170,7 +171,8 @@ namespace Core {
             }
             i64 l = 1l << shifts;
             CORE_RETURN_ERROR(map.keys.resize(l, emptyValue<K>()));
-            map.values = reinterpret_cast<V*>(new AlignedType<V>[l]);
+            map.values = reinterpret_cast<V*>(
+                new(noThrow) AlignedType<V>[static_cast<u64>(l)]);
             if(map.values == nullptr) {
                 return Error::OUT_OF_MEMORY;
             }
@@ -318,4 +320,4 @@ namespace Core {
     };
 }
 
-#endif
+#endif

+ 1 - 1
include/core/math/Vector.hpp

@@ -211,4 +211,4 @@ Core::Vector<N, T> operator*(T factor, const Core::Vector<N, T>& v) {
     return v * factor;
 }
 
-#endif
+#endif

+ 3 - 2
include/core/utils/AlignedData.hpp

@@ -3,7 +3,8 @@
 
 #define CORE_ASSERT_ALIGNED_DATA(var, type)                                    \
     static_assert(sizeof(var) == sizeof(type), "aligned data size missmatch"); \
-    static_assert(var.getSize() >= var.getAlignment(), "size >= alignment");   \
+    static_assert(decltype(var)::getSize() >= decltype(var)::getAlignment(),   \
+                  "size >= alignment");                                        \
     static_assert(alignof(decltype(var)) == alignof(type),                     \
                   "aligned data alignment missmatch");
 
@@ -37,4 +38,4 @@ namespace Core {
     using AlignedType = AlignedData<sizeof(T), alignof(T)>;
 }
 
-#endif
+#endif

+ 8 - 3
include/core/utils/New.hpp

@@ -3,12 +3,17 @@
 
 using size_t = decltype(sizeof(0));
 
-void* operator new(size_t bytes) noexcept;
-void* operator new[](size_t bytes) noexcept;
+struct NoThrow {
+    explicit NoThrow() = default;
+};
+[[maybe_unused]] static constexpr NoThrow noThrow;
+
+void* operator new(size_t bytes, const NoThrow&) noexcept;
+void* operator new[](size_t bytes, const NoThrow&) noexcept;
 void operator delete(void* p) noexcept;
 void operator delete[](void* p) noexcept;
 void operator delete(void* p, size_t bytes) noexcept;
 void operator delete[](void* p, size_t bytes) noexcept;
 void* operator new(size_t bytes, void* p) noexcept;
 
-#endif
+#endif

+ 1 - 1
include/core/utils/Utility.hpp

@@ -46,4 +46,4 @@ namespace Core {
     void free(void* p);
 }
 
-#endif
+#endif

+ 2 - 2
src/BitArray.cpp

@@ -139,7 +139,7 @@ check_return Core::Error Core::BitArray::resize(u64 newLength, u64 newBits) {
         return Error::INVALID_ARGUMENT;
     }
     u64 arrayLength = getArrayLength(newLength, newBits);
-    u64* newData = new u64[arrayLength];
+    u64* newData = new(noThrow) u64[arrayLength];
     if(newData == nullptr) {
         return Error::OUT_OF_MEMORY;
     }
@@ -161,4 +161,4 @@ check_return Core::Error Core::BitArray::resize(u64 newLength, u64 newBits) {
 void Core::BitArray::swap(BitArray& other) {
     Core::swap(lengthBits, other.lengthBits);
     Core::swap(data, other.data);
-}
+}

+ 1 - 1
src/Mutex.cpp

@@ -42,4 +42,4 @@ check_return Core::Error Core::Mutex::lock() {
 check_return Core::Error Core::Mutex::unlock() {
     return mtx_unlock(mutex.as<mtx_t>()) != thrd_success ? Error::MUTEX_ERROR
                                                          : Error::NONE;
-}
+}

+ 2 - 2
src/New.cpp

@@ -4,7 +4,7 @@
 
 #include "ErrorSimulator.hpp"
 
-void* operator new(size_t bytes) noexcept {
+void* operator new(size_t bytes, const NoThrow&) noexcept {
 #ifdef ERROR_SIMULATOR
     if(Core::Fail::leftAllocations > 0) {
         Core::Fail::leftAllocations--;
@@ -15,7 +15,7 @@ void* operator new(size_t bytes) noexcept {
     return malloc(bytes);
 }
 
-void* operator new[](size_t bytes) noexcept {
+void* operator new[](size_t bytes, const NoThrow&) noexcept {
 #ifdef ERROR_SIMULATOR
     if(Core::Fail::leftAllocations > 0) {
         Core::Fail::leftAllocations--;

+ 1 - 1
src/View.cpp

@@ -46,4 +46,4 @@ Core::Vector3 Core::View::getFront() const {
 
 Core::Vector3 Core::View::getBack() const {
     return back;
-}
+}

+ 10 - 2
tasks

@@ -3,6 +3,12 @@ set -e
 clear
 cd $(dirname $0)
 
+compiler="g++"
+if [ -e compiler ]; then
+    compiler=$(cat compiler)
+    echo "compiling with $compiler"
+fi
+
 printHelpExit() {
     echo "$0 clean           | remove build results"
     echo "$0 build <type>    | build everything"
@@ -36,6 +42,8 @@ time=false
 install=false
 coverage=false
 
+export CMAKE_EXPORT_COMPILE_COMMANDS=true
+
 # parsing
 if [ "$task" = "clean" ]; then
     rm -rf build_debug build_release install
@@ -124,7 +132,7 @@ buildProfile() {
     folder=$1
     shift 1
     if [ ! -e "$folder" ]; then 
-        cmake -B "$folder" -S . -G Ninja -DCMAKE_INSTALL_PREFIX=./install $@
+        cmake -B "$folder" -S . -G Ninja -DCMAKE_CXX_COMPILER=${compiler} -DCMAKE_INSTALL_PREFIX=./install $@
     fi
     ninja -C "$folder"
 }
@@ -178,4 +186,4 @@ fi
 if $coverage; then
     gcovr -r . build_debug -e test -e performance \
         --exclude-lines-by-pattern ".*CoverageIgnore.*"
-fi
+fi

+ 2 - 2
test/modules/HashMapTests.cpp

@@ -2,7 +2,7 @@
 #include "../Tests.hpp"
 #include "core/data/HashMap.hpp"
 
-template class Core::HashMap<int, int>;
+template struct Core::HashMap<int, int>;
 using IntMap = Core::HashMap<int, int>;
 
 static void testAdd() {
@@ -354,4 +354,4 @@ void Core::testHashMap(bool light, bool outOfMemoryTest) {
     if(outOfMemoryTest) {
         testOutOfMemory();
     }
-}
+}

+ 2 - 2
test/modules/LinkedListTests.cpp

@@ -2,7 +2,7 @@
 #include "../Tests.hpp"
 #include "core/data/LinkedList.hpp"
 
-template class Core::LinkedList<int>;
+template struct Core::LinkedList<int>;
 using IntList = Core::LinkedList<int>;
 
 struct LinkedListTester final {
@@ -311,4 +311,4 @@ void Core::testLinkedList(bool light, bool outOfMemoryTest) {
     if(outOfMemoryTest) {
         testOutOfMemory();
     }
-}
+}

+ 5 - 4
test/modules/NewTests.cpp

@@ -1,21 +1,22 @@
 #include "../Tests.hpp"
+#include "core/utils/New.hpp"
 
 struct NewTestClass {
     int i;
 };
 
 static void testNewArray() {
-    NewTestClass* n = new NewTestClass[5];
+    NewTestClass* n = new(noThrow) NewTestClass[5];
     delete[] n;
 }
 
 static void testNewFlat() {
-    NewTestClass* n = new NewTestClass();
+    NewTestClass* n = new(noThrow) NewTestClass();
     delete n;
 }
 
 static void testNewArrayWithSize() {
-    NewTestClass* n = new NewTestClass[5];
+    NewTestClass* n = new(noThrow) NewTestClass[5];
     operator delete[](n, 5);
 }
 
@@ -23,4 +24,4 @@ void Core::testNew() {
     testNewArray();
     testNewFlat();
     testNewArrayWithSize();
-}
+}

+ 2 - 2
test/modules/ProbingHashMapTests.cpp

@@ -2,7 +2,7 @@
 #include "../Tests.hpp"
 #include "core/data/ProbingHashMap.hpp"
 
-template class Core::ProbingHashMap<int, int>;
+template struct Core::ProbingHashMap<int, int>;
 using IntMap = Core::ProbingHashMap<int, int>;
 
 static void testAdd() {
@@ -372,4 +372,4 @@ void Core::testProbingHashMap(bool light, bool outOfMemoryTest) {
     }
     testInsertInvalid();
     testAddCollisions();
-}
+}

+ 7 - 6
test/modules/UniquePointerTests.cpp

@@ -1,4 +1,5 @@
 #include "../Tests.hpp"
+#include "core/utils/New.hpp"
 #include "core/utils/UniquePointer.hpp"
 
 struct B final {
@@ -26,14 +27,14 @@ using UniqueB = Core::UniquePointer<B>;
 
 static void testDestroy() {
     {
-        UniqueB p(new B());
+        UniqueB p(new(noThrow) B());
         CORE_TEST_EQUAL(1, B::instances);
     }
     CORE_TEST_EQUAL(0, B::instances);
 }
 
 static void testMoveConstructDestroys() {
-    UniqueB p1(new B());
+    UniqueB p1(new(noThrow) B());
     CORE_TEST_EQUAL(1, B::instances);
     UniqueB p2(Core::move(p1));
     CORE_TEST_EQUAL(1, B::instances);
@@ -43,8 +44,8 @@ static void testMoveConstructDestroys() {
 
 static void testMoveDestroys() {
     {
-        UniqueB p1(new B());
-        UniqueB p2(new B());
+        UniqueB p1(new(noThrow) B());
+        UniqueB p2(new(noThrow) B());
         CORE_TEST_EQUAL(2, B::instances);
         p1 = Core::move(p2);
         CORE_TEST_EQUAL(1, B::instances);
@@ -59,7 +60,7 @@ static void testEmpty() {
 }
 
 static void testCall() {
-    UniqueB p(new B());
+    UniqueB p(new(noThrow) B());
     CORE_TEST_FALSE(p->b);
     p->test();
     CORE_TEST_TRUE(p->b);
@@ -72,4 +73,4 @@ void Core::testUniquePointer() {
     testMoveDestroys();
     testEmpty();
     testCall();
-}
+}