From 0be6d925dc3c6413bce7a3ccb49631b8e4a6e67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Thu, 14 May 2020 13:23:58 +0200 Subject: Style: clang-format: Disable KeepEmptyLinesAtTheStartOfBlocks Which means that reduz' beloved style which we all became used to will now be changed automatically to remove the first empty line. This makes us lean closer to 1TBS (the one true brace style) instead of hybridating it with some Allman-inspired spacing. There's still the case of braces around single-statement blocks that needs to be addressed (but clang-format can't help with that, but clang-tidy may if we agree about it). Part of #33027. --- core/array.cpp | 44 -------------------------------------------- 1 file changed, 44 deletions(-) (limited to 'core/array.cpp') diff --git a/core/array.cpp b/core/array.cpp index 7c0129ffde..75efe8f8ff 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -46,7 +46,6 @@ public: }; void Array::_ref(const Array &p_from) const { - ArrayPrivate *_fp = p_from._p; ERR_FAIL_COND(!_fp); // should NOT happen. @@ -64,7 +63,6 @@ void Array::_ref(const Array &p_from) const { } void Array::_unref() const { - if (!_p) return; @@ -75,46 +73,37 @@ void Array::_unref() const { } Variant &Array::operator[](int p_idx) { - return _p->array.write[p_idx]; } const Variant &Array::operator[](int p_idx) const { - return _p->array[p_idx]; } int Array::size() const { - return _p->array.size(); } bool Array::empty() const { - return _p->array.empty(); } void Array::clear() { - _p->array.clear(); } bool Array::operator==(const Array &p_array) const { - return _p == p_array._p; } uint32_t Array::hash() const { - uint32_t h = hash_djb2_one_32(0); for (int i = 0; i < _p->array.size(); i++) { - h = hash_djb2_one_32(_p->array[i].hash(), h); } return h; } void Array::_assign(const Array &p_array) { - if (_p->typed.type != Variant::OBJECT && _p->typed.type == p_array._p->typed.type) { //same type or untyped, just reference, shuold be fine _ref(p_array); @@ -163,24 +152,20 @@ void Array::operator=(const Array &p_array) { _assign(p_array); } void Array::push_back(const Variant &p_value) { - ERR_FAIL_COND(!_p->typed.validate(p_value, "push_back")); _p->array.push_back(p_value); } Error Array::resize(int p_new_size) { - return _p->array.resize(p_new_size); } void Array::insert(int p_pos, const Variant &p_value) { - ERR_FAIL_COND(!_p->typed.validate(p_value, "insert")); _p->array.insert(p_pos, p_value); } void Array::erase(const Variant &p_value) { - ERR_FAIL_COND(!_p->typed.validate(p_value, "erase")); _p->array.erase(p_value); } @@ -196,13 +181,11 @@ Variant Array::back() const { } int Array::find(const Variant &p_value, int p_from) const { - ERR_FAIL_COND_V(!_p->typed.validate(p_value, "find"), -1); return _p->array.find(p_value, p_from); } int Array::rfind(const Variant &p_value, int p_from) const { - if (_p->array.size() == 0) return -1; ERR_FAIL_COND_V(!_p->typed.validate(p_value, "rfind"), -1); @@ -217,7 +200,6 @@ int Array::rfind(const Variant &p_value, int p_from) const { } for (int i = p_from; i >= 0; i--) { - if (_p->array[i] == p_value) { return i; } @@ -227,20 +209,17 @@ int Array::rfind(const Variant &p_value, int p_from) const { } int Array::find_last(const Variant &p_value) const { - ERR_FAIL_COND_V(!_p->typed.validate(p_value, "find_last"), -1); return rfind(p_value); } int Array::count(const Variant &p_value) const { - ERR_FAIL_COND_V(!_p->typed.validate(p_value, "count"), 0); if (_p->array.size() == 0) return 0; int amount = 0; for (int i = 0; i < _p->array.size(); i++) { - if (_p->array[i] == p_value) { amount++; } @@ -256,24 +235,20 @@ bool Array::has(const Variant &p_value) const { } void Array::remove(int p_pos) { - _p->array.remove(p_pos); } void Array::set(int p_idx, const Variant &p_value) { - ERR_FAIL_COND(!_p->typed.validate(p_value, "set")); operator[](p_idx) = p_value; } const Variant &Array::get(int p_idx) const { - return operator[](p_idx); } Array Array::duplicate(bool p_deep) const { - Array new_arr; int element_count = size(); new_arr.resize(element_count); @@ -286,7 +261,6 @@ Array Array::duplicate(bool p_deep) const { } int Array::_clamp_slice_index(int p_index) const { - int arr_size = size(); int fixed_index = CLAMP(p_index, -arr_size, arr_size - 1); if (fixed_index < 0) { @@ -335,7 +309,6 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // l } struct _ArrayVariantSort { - _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const { bool valid = false; Variant res; @@ -347,18 +320,15 @@ struct _ArrayVariantSort { }; Array &Array::sort() { - _p->array.sort_custom<_ArrayVariantSort>(); return *this; } struct _ArrayVariantSortCustom { - Object *obj; StringName func; _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const { - const Variant *args[2] = { &p_l, &p_r }; Callable::CallError err; bool res = obj->call(func, args, 2, err); @@ -368,7 +338,6 @@ struct _ArrayVariantSortCustom { } }; Array &Array::sort_custom(Object *p_obj, const StringName &p_function) { - ERR_FAIL_NULL_V(p_obj, *this); SortArray avs; @@ -379,7 +348,6 @@ Array &Array::sort_custom(Object *p_obj, const StringName &p_function) { } void Array::shuffle() { - const int n = _p->array.size(); if (n < 2) return; @@ -394,7 +362,6 @@ void Array::shuffle() { template _FORCE_INLINE_ int bisect(const Vector &p_array, const Variant &p_value, bool p_before, const Less &p_less) { - int lo = 0; int hi = p_array.size(); if (p_before) { @@ -420,13 +387,11 @@ _FORCE_INLINE_ int bisect(const Vector &p_array, const Variant &p_value } int Array::bsearch(const Variant &p_value, bool p_before) { - ERR_FAIL_COND_V(!_p->typed.validate(p_value, "binary search"), -1); return bisect(_p->array, p_value, p_before, _ArrayVariantSort()); } int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) { - ERR_FAIL_COND_V(!_p->typed.validate(p_value, "custom binary search"), -1); ERR_FAIL_NULL_V(p_obj, 0); @@ -438,19 +403,16 @@ int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringNam } Array &Array::invert() { - _p->array.invert(); return *this; } void Array::push_front(const Variant &p_value) { - ERR_FAIL_COND(!_p->typed.validate(p_value, "push_front")); _p->array.insert(0, p_value); } Variant Array::pop_back() { - if (!_p->array.empty()) { int n = _p->array.size() - 1; Variant ret = _p->array.get(n); @@ -461,7 +423,6 @@ Variant Array::pop_back() { } Variant Array::pop_front() { - if (!_p->array.empty()) { Variant ret = _p->array.get(0); _p->array.remove(0); @@ -471,7 +432,6 @@ Variant Array::pop_front() { } Variant Array::min() const { - Variant minval; for (int i = 0; i < size(); i++) { if (i == 0) { @@ -494,7 +454,6 @@ Variant Array::min() const { } Variant Array::max() const { - Variant maxval; for (int i = 0; i < size(); i++) { if (i == 0) { @@ -542,17 +501,14 @@ void Array::set_typed(uint32_t p_type, const StringName &p_class_name, const Var } Array::Array(const Array &p_from) { - _p = nullptr; _ref(p_from); } Array::Array() { - _p = memnew(ArrayPrivate); _p->refcount.init(); } Array::~Array() { - _unref(); } -- cgit v1.2.3 From 07bc4e2f96f8f47991339654ff4ab16acc19d44f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Thu, 14 May 2020 14:29:06 +0200 Subject: Style: Enforce separation line between function definitions I couldn't find a tool that enforces it, so I went the manual route: ``` find -name "thirdparty" -prune \ -o -name "*.cpp" -o -name "*.h" -o -name "*.m" -o -name "*.mm" \ -o -name "*.glsl" > files perl -0777 -pi -e 's/\n}\n([^#])/\n}\n\n\1/g' $(cat files) misc/scripts/fix_style.sh -c ``` This adds a newline after all `}` on the first column, unless they are followed by `#` (typically `#endif`). This leads to having lots of places with two lines between function/class definitions, but clang-format then fixes it as we enforce max one line of separation. This doesn't fix potential occurrences of function definitions which are indented (e.g. for a helper class defined in a .cpp), but it's better than nothing. Also can't be made to run easily on CI/hooks so we'll have to be careful with new code. Part of #33027. --- core/array.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'core/array.cpp') diff --git a/core/array.cpp b/core/array.cpp index 75efe8f8ff..1a8a833404 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -83,9 +83,11 @@ const Variant &Array::operator[](int p_idx) const { int Array::size() const { return _p->array.size(); } + bool Array::empty() const { return _p->array.empty(); } + void Array::clear() { _p->array.clear(); } @@ -151,6 +153,7 @@ void Array::_assign(const Array &p_array) { void Array::operator=(const Array &p_array) { _assign(p_array); } + void Array::push_back(const Variant &p_value) { ERR_FAIL_COND(!_p->typed.validate(p_value, "push_back")); _p->array.push_back(p_value); @@ -509,6 +512,7 @@ Array::Array() { _p = memnew(ArrayPrivate); _p->refcount.init(); } + Array::~Array() { _unref(); } -- cgit v1.2.3 From 0ee0fa42e6639b6fa474b7cf6afc6b1a78142185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Thu, 14 May 2020 16:41:43 +0200 Subject: Style: Enforce braces around if blocks and loops Using clang-tidy's `readability-braces-around-statements`. https://clang.llvm.org/extra/clang-tidy/checks/readability-braces-around-statements.html --- core/array.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'core/array.cpp') diff --git a/core/array.cpp b/core/array.cpp index 1a8a833404..d1c0688e63 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -50,8 +50,9 @@ void Array::_ref(const Array &p_from) const { ERR_FAIL_COND(!_fp); // should NOT happen. - if (_fp == _p) + if (_fp == _p) { return; // whatever it is, nothing to do here move along + } bool success = _fp->refcount.ref(); @@ -63,8 +64,9 @@ void Array::_ref(const Array &p_from) const { } void Array::_unref() const { - if (!_p) + if (!_p) { return; + } if (_p->refcount.unref()) { memdelete(_p); @@ -189,8 +191,9 @@ int Array::find(const Variant &p_value, int p_from) const { } int Array::rfind(const Variant &p_value, int p_from) const { - if (_p->array.size() == 0) + if (_p->array.size() == 0) { return -1; + } ERR_FAIL_COND_V(!_p->typed.validate(p_value, "rfind"), -1); if (p_from < 0) { @@ -218,8 +221,9 @@ int Array::find_last(const Variant &p_value) const { int Array::count(const Variant &p_value) const { ERR_FAIL_COND_V(!_p->typed.validate(p_value, "count"), 0); - if (_p->array.size() == 0) + if (_p->array.size() == 0) { return 0; + } int amount = 0; for (int i = 0; i < _p->array.size(); i++) { @@ -278,14 +282,17 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // l ERR_FAIL_COND_V_MSG(p_step == 0, new_arr, "Array slice step size cannot be zero."); - if (empty()) // Don't try to slice empty arrays. + if (empty()) { // Don't try to slice empty arrays. return new_arr; + } if (p_step > 0) { - if (p_begin >= size() || p_end < -size()) + if (p_begin >= size() || p_end < -size()) { return new_arr; + } } else { // p_step < 0 - if (p_begin < -size() || p_end >= size()) + if (p_begin < -size() || p_end >= size()) { return new_arr; + } } int begin = _clamp_slice_index(p_begin); @@ -316,8 +323,9 @@ struct _ArrayVariantSort { bool valid = false; Variant res; Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid); - if (!valid) + if (!valid) { res = false; + } return res; } }; @@ -335,8 +343,9 @@ struct _ArrayVariantSortCustom { const Variant *args[2] = { &p_l, &p_r }; Callable::CallError err; bool res = obj->call(func, args, 2, err); - if (err.error != Callable::CallError::CALL_OK) + if (err.error != Callable::CallError::CALL_OK) { res = false; + } return res; } }; @@ -352,8 +361,9 @@ Array &Array::sort_custom(Object *p_obj, const StringName &p_function) { void Array::shuffle() { const int n = _p->array.size(); - if (n < 2) + if (n < 2) { return; + } Variant *data = _p->array.ptrw(); for (int i = n - 1; i >= 1; i--) { const int j = Math::rand() % (i + 1); -- cgit v1.2.3