From 6bd0406df9e99df055b12758e4e7fb21d634af41 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 12 Jul 2025 05:01:06 -0400 Subject: [PATCH] Update .clang-format for my fickle tastes --- .clang-format | 14 ++++- src/include/keywords.hh | 20 +++--- src/include/panic.hh | 11 ++-- src/include/rust.hh | 131 ++++++++++++++++++++-------------------- src/main.cc | 18 +++--- src/runtime.cc | 4 +- 6 files changed, 107 insertions(+), 91 deletions(-) diff --git a/.clang-format b/.clang-format index 0a5ddaf..7e5c539 100644 --- a/.clang-format +++ b/.clang-format @@ -16,6 +16,9 @@ ColumnLimit: 0 IndentWidth: 4 ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 +IndentGotoLabels: true +IndentPPDirectives: BeforeHash +IndentCaseLabels: false # Alignment checks AlignAfterOpenBracket: false @@ -70,6 +73,9 @@ BraceWrapping: # Don't break before ?:, it looks ugly BreakBeforeTernaryOperators: false +BreakConstructorInitializers: AfterColon +BreakInheritanceList: AfterColon +BreakAfterAttributes: Leave # Trim empty lines when there are more than 1 MaxEmptyLinesToKeep: 2 @@ -89,7 +95,12 @@ SpaceAfterTemplateKeyword: true # Put spaces before \.?\= operators, initializer {lists}, inline (parentheses), // comments. SpaceBeforeAssignmentOperators: true SpaceBeforeCpp11BracedList: true -SpaceBeforeParens: Always +SpaceBeforeParens: Custom +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterFunctionDeclarationName: true + AfterFunctionDefinitionName: true + AfterOverloadedOperator: true SpacesBeforeTrailingComments: 1 SpacesInLineCommentPrefix: Minimum: 1 @@ -97,6 +108,7 @@ SpacesInLineCommentPrefix: # for (auto& loops : range), conditional ( statements ), ( parentheses ), [ brackets ] SpaceBeforeCaseColon: false SpaceBeforeInheritanceColon: false +SpaceBeforeCtorInitializerColon: false SpaceBeforeRangeBasedForLoopColon: false SpacesInConditionalStatement: false SpacesInParentheses: false diff --git a/src/include/keywords.hh b/src/include/keywords.hh index d147b5a..fae52e1 100644 --- a/src/include/keywords.hh +++ b/src/include/keywords.hh @@ -26,20 +26,20 @@ #define RUSTPP_UNIQ(name) RUSTPP_UNIQ_(name, __LINE__) // clang-format on -#define rustpp_for(value, iter) \ - if (auto RUSTPP_UNIQ (iterator) = iter.into_iter (); auto RUSTPP_UNIQ (opt) = RUSTPP_UNIQ (iterator).next ()) \ - for (auto value = RUSTPP_UNIQ (opt).unwrap_unchecked (); \ - RUSTPP_UNIQ (opt).is_some (); \ - RUSTPP_UNIQ (opt) = RUSTPP_UNIQ (iterator).next (), value = RUSTPP_UNIQ (opt).unwrap_unchecked ()) +#define rustpp_for(value, iter) \ + if (auto RUSTPP_UNIQ(iterator) = iter.into_iter(); auto RUSTPP_UNIQ(opt) = RUSTPP_UNIQ(iterator).next()) \ + for (auto value = RUSTPP_UNIQ(opt).unwrap_unchecked(); \ + RUSTPP_UNIQ(opt).is_some(); \ + RUSTPP_UNIQ(opt) = RUSTPP_UNIQ(iterator).next(), value = RUSTPP_UNIQ(opt).unwrap_unchecked()) #ifndef I_AM_RUSTPP_RUNTIME -#define main rustpp__main -#define new rustpp__new + #define main rustpp__main + #define new rustpp__new -#ifndef NO_RUSTPP_FOR -#define for(...) rustpp_for (__VA_ARGS__) -#endif /* NO_RUSTPP_FOR */ + #ifndef NO_RUSTPP_FOR + #define for(...) rustpp_for(__VA_ARGS__) + #endif /* NO_RUSTPP_FOR */ #endif /* I_AM_RUSTPP_RUNTIME */ diff --git a/src/include/panic.hh b/src/include/panic.hh index b056635..c3b41e8 100644 --- a/src/include/panic.hh +++ b/src/include/panic.hh @@ -11,18 +11,19 @@ namespace panic { public: // std::stacktrace stack; std::string message; - Panic (std::string message) : message (message) { + Panic (std::string message): + message(message) { // stack = std::stacktrace::current(); } constexpr const char *what () { - return message.c_str (); + return message.c_str(); } }; } // namespace panic -#define panic(...) \ - do { \ - throw panic::Panic { std::format (__VA_ARGS__) }; \ +#define panic(...) \ + do { \ + throw panic::Panic { std::format(__VA_ARGS__) }; \ } while (false) diff --git a/src/include/rust.hh b/src/include/rust.hh index 5a745cb..e5547ea 100644 --- a/src/include/rust.hh +++ b/src/include/rust.hh @@ -53,7 +53,7 @@ mod option { } operator bool () { - return this->is_some (); + return this->is_some(); } operator i32 () { @@ -65,17 +65,17 @@ mod option { } fn unwrap () -> T { - match (variant) { - arm (variant::Some, return value.Some); - arm (variant::None, panic ("Called Option::unwrap() on a None value")); + match(variant) { + arm(variant::Some, return value.Some); + arm(variant::None, panic("Called Option::unwrap() on a None value")); } } template - fn map (U (f) (T)) -> Option { - match (variant) { - arm (variant::Some, return Some (f (value.Some))); - arm (variant::None, return None ()); + fn map (U(f)(T)) -> Option { + match(variant) { + arm(variant::Some, return Some(f(value.Some))); + arm(variant::None, return None()); } } }; @@ -84,14 +84,14 @@ mod option { /// /// Takes an optional (never-dereferenced) pointer argument, for type inference. template - fn None (T *_type = (T *) (0)) -> Option { - return Option::None (); + fn None(T *_type = (T *) (0)) -> Option { + return Option::None(); } /// Constructs an option with `variant` Some template - fn Some (T value) -> Option { - return Option::Some (value); + fn Some(T value) -> Option { + return Option::Some(value); } } @@ -139,44 +139,44 @@ mod result { } fn ok () -> option::Option { - match (variant) { - arm (variant::Ok, return option::Some (value.Ok)); - arm (variant::Err, return option::None ()); + match(variant) { + arm(variant::Ok, return option::Some(value.Ok)); + arm(variant::Err, return option::None()); } } fn err () -> option::Option { - match (variant) { - arm (variant::Err, return option::Some (value.Err)); - arm (variant::Ok, return option::None ()); + match(variant) { + arm(variant::Err, return option::Some(value.Err)); + arm(variant::Ok, return option::None()); } } fn unwrap () -> T { - match (variant) { - arm (variant::Ok, return value.Ok); - arm (variant::Err, panic ("Called Result::unwrap() on Err value")); + match(variant) { + arm(variant::Ok, return value.Ok); + arm(variant::Err, panic("Called Result::unwrap() on Err value")); } } fn unwrap_err () -> E { - match (variant) { - arm (variant::Ok, panic ("Called Result::unwrap_err() on Ok value")); - arm (variant::Err, return value.Err); + match(variant) { + arm(variant::Ok, panic("Called Result::unwrap_err() on Ok value")); + arm(variant::Err, return value.Err); } } }; /// Constructs a Result with variant Err and value `Err(value)` template - fn Err (E error) -> Result { - return Result::Err (error); + fn Err(E error) -> Result { + return Result::Err(error); } /// Constructs a Result with variant Ok and value `Ok(value)` template - fn Ok (T value) -> Result { - return Result::Ok (value); + fn Ok(T value) -> Result { + return Result::Ok(value); } } @@ -188,9 +188,9 @@ mod ptr { static fn rustpp__new (T *value) -> option::Option { if (value) { - return option::Some ((Self) { value }); + return option::Some((Self) { value }); } else { - return option::None (); + return option::None(); } } @@ -200,17 +200,17 @@ mod ptr { }; template - fn nonnull (T * value) -> option::Option> { + fn nonnull(T * value) -> option::Option> { switch (value) { - case nullptr: - return option::Some (value); - default: - return option::None (&value); + case nullptr: + return option::Some(value); + default: + return option::None(&value); } } template - fn nonnull_unchecked (T * value) -> Nonnull { + fn nonnull_unchecked(T * value) -> Nonnull { return { value }; } } @@ -230,8 +230,9 @@ mod slice { T *head; T *tail; - Iter (T *head, T *tail) : head (head), - tail (tail) {} + Iter (T *head, T *tail): + head(head), + tail(tail) {} public: makeSelf (Iter); @@ -242,9 +243,9 @@ mod slice { fn next () -> option::Option> override { if (head < tail) - return ptr::Nonnull::rustpp__new (head++); + return ptr::Nonnull::rustpp__new(head++); else - return option::None> (); + return option::None>(); } fn into_iter () -> Self & { @@ -259,7 +260,7 @@ mod slice { makeSelf (Slice); fn into_iter () -> Iter { - return Iter::rustpp__new (self.ptr, self.len); + return Iter::rustpp__new(self.ptr, self.len); } }; } @@ -273,26 +274,28 @@ mod vec { T *tail; public: - Drain () : allocation (nullptr), - head (nullptr), - tail (nullptr) {} - Drain (T *allocation, usize len) : allocation (allocation), - head (allocation), - tail (allocation + len) {} + Drain (): + allocation(nullptr), + head(nullptr), + tail(nullptr) {} + Drain (T *allocation, usize len): + allocation(allocation), + head(allocation), + tail(allocation + len) {} ~Drain () { - free (allocation); + free(allocation); } fn next () -> option::Option override { if (head < tail) { - return option::Some (*head++); + return option::Some(*head++); } if (allocation) { - free (allocation); + free(allocation); allocation = nullptr; } - return option::None (); + return option::None(); } }; @@ -310,9 +313,9 @@ mod vec { } fn resize (usize cap) -> usize { - ptr = (T *) realloc ((void *) ptr, cap); + ptr = (T *) realloc((void *) ptr, cap); if (!ptr) { - panic ("Failed to resize Vec from {} to {}.", self.cap, cap); + panic("Failed to resize Vec from {} to {}.", self.cap, cap); } self.cap = cap; return self.cap; @@ -320,31 +323,31 @@ mod vec { fn _grow () -> usize { switch (len) { - case 0: - return self.resize (8); - default: - return self.resize ((cap << 1) - cap); + case 0: + return self.resize(8); + default: + return self.resize((cap << 1) - cap); } } fn drop () { - free (ptr); + free(ptr); self = {}; } fn push (T value) { - self._grow (); + self._grow(); ptr[len++] = value; } fn pop () -> option::Option { use option::Some, option::None; if (len == 0) { - return None (ptr); + return None(ptr); } let value = ptr[--len]; - return Some (value); + return Some(value); } fn as_slice () -> slice::Slice { @@ -352,11 +355,11 @@ mod vec { } fn iter () -> slice::Iter { - return self.as_slice ().into_iter (); + return self.as_slice().into_iter(); } fn into_iter () -> Drain { - let drain = Drain (self.ptr, self.len); + let drain = Drain(self.ptr, self.len); self = {}; return drain; } @@ -365,7 +368,7 @@ mod vec { mod env { /// Gets the environment variables - fn args () -> slice::Slice; + fn args() -> slice::Slice; } diff --git a/src/main.cc b/src/main.cc index 77c2a2d..c44578f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,20 +6,20 @@ fn main () -> i32 { let v = vec::Vec::new (); - v.push (10); - v.push (20); - v.push (30); + v.push(10); + v.push(20); + v.push(30); - std::println ("Printing contents of v by reference"); - for (x, v.iter ()) { - std::println ("{}", *x); + std::println("Printing contents of v by reference"); + for (x, v.iter()) { + std::println("{}", *x); } // This is okay, because the previous for loop borrowed v - std::println ("Printing contents of v by value"); + std::println("Printing contents of v by value"); for (x, v) { - std::println ("{}", x); + std::println("{}", x); } - return Err ("Oops!"); + return Err("Oops!"); } diff --git a/src/runtime.cc b/src/runtime.cc index 4a256ba..7b7ba74 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -14,9 +14,9 @@ fn main (int argc, char **argv) -> int { int exitcode = 0; program_args = { (Cstr *) argv, (usize) argc }; try { - exitcode = rustpp__main (); + exitcode = rustpp__main(); } catch (panic::Panic p) { - std::println ("Thread exited with explicit panic: {}", p.what ()); + std::println("Thread exited with explicit panic: {}", p.what()); exitcode = -1; } return exitcode;