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/os/file_access.cpp | 72 ------------------------------------------------- 1 file changed, 72 deletions(-) (limited to 'core/os/file_access.cpp') diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index cb8705f706..f31842bcae 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -43,7 +43,6 @@ FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = nullptr; bool FileAccess::backup_save = false; FileAccess *FileAccess::create(AccessType p_access) { - ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, nullptr); FileAccess *ret = create_func[p_access](); @@ -52,7 +51,6 @@ FileAccess *FileAccess::create(AccessType p_access) { } bool FileAccess::exists(const String &p_name) { - if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name)) return true; @@ -64,22 +62,17 @@ bool FileAccess::exists(const String &p_name) { } void FileAccess::_set_access_type(AccessType p_access) { - _access_type = p_access; }; FileAccess *FileAccess::create_for_path(const String &p_path) { - FileAccess *ret = nullptr; if (p_path.begins_with("res://")) { - ret = create(ACCESS_RESOURCES); } else if (p_path.begins_with("user://")) { - ret = create(ACCESS_USERDATA); } else { - ret = create(ACCESS_FILESYSTEM); } @@ -87,12 +80,10 @@ FileAccess *FileAccess::create_for_path(const String &p_path) { } Error FileAccess::reopen(const String &p_path, int p_mode_flags) { - return _open(p_path, p_mode_flags); }; FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) { - //try packed data first FileAccess *ret = nullptr; @@ -111,7 +102,6 @@ FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_er if (r_error) *r_error = err; if (err != OK) { - memdelete(ret); ret = nullptr; } @@ -120,7 +110,6 @@ FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_er } FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) { - return create_func[p_access]; }; @@ -130,15 +119,11 @@ String FileAccess::fix_path(const String &p_path) const { String r_path = p_path.replace("\\", "/"); switch (_access_type) { - case ACCESS_RESOURCES: { - if (ProjectSettings::get_singleton()) { if (r_path.begins_with("res://")) { - String resource_path = ProjectSettings::get_singleton()->get_resource_path(); if (resource_path != "") { - return r_path.replace("res:/", resource_path); }; return r_path.replace("res://", ""); @@ -147,12 +132,9 @@ String FileAccess::fix_path(const String &p_path) const { } break; case ACCESS_USERDATA: { - if (r_path.begins_with("user://")) { - String data_dir = OS::get_singleton()->get_user_data_dir(); if (data_dir != "") { - return r_path.replace("user:/", data_dir); }; return r_path.replace("user://", ""); @@ -160,7 +142,6 @@ String FileAccess::fix_path(const String &p_path) const { } break; case ACCESS_FILESYSTEM: { - return r_path; } break; case ACCESS_MAX: @@ -173,7 +154,6 @@ String FileAccess::fix_path(const String &p_path) const { /* these are all implemented for ease of porting, then can later be optimized */ uint16_t FileAccess::get_16() const { - uint16_t res; uint8_t a, b; @@ -181,7 +161,6 @@ uint16_t FileAccess::get_16() const { b = get_8(); if (endian_swap) { - SWAP(a, b); } @@ -192,7 +171,6 @@ uint16_t FileAccess::get_16() const { return res; } uint32_t FileAccess::get_32() const { - uint32_t res; uint16_t a, b; @@ -200,7 +178,6 @@ uint32_t FileAccess::get_32() const { b = get_16(); if (endian_swap) { - SWAP(a, b); } @@ -211,7 +188,6 @@ uint32_t FileAccess::get_32() const { return res; } uint64_t FileAccess::get_64() const { - uint64_t res; uint32_t a, b; @@ -219,7 +195,6 @@ uint64_t FileAccess::get_64() const { b = get_32(); if (endian_swap) { - SWAP(a, b); } @@ -231,14 +206,12 @@ uint64_t FileAccess::get_64() const { } float FileAccess::get_float() const { - MarshallFloat m; m.i = get_32(); return m.f; }; real_t FileAccess::get_real() const { - if (real_is_double) return get_double(); else @@ -246,20 +219,17 @@ real_t FileAccess::get_real() const { } double FileAccess::get_double() const { - MarshallDouble m; m.l = get_64(); return m.d; }; String FileAccess::get_token() const { - CharString token; CharType c = get_8(); while (!eof_reached()) { - if (c <= ' ') { if (token.length()) break; @@ -281,16 +251,13 @@ class CharBuffer { int written = 0; bool grow() { - if (vector.resize(next_power_of_2(1 + written)) != OK) { - return false; } if (buffer == stack_buffer) { // first chunk? for (int i = 0; i < written; i++) { - vector.write[i] = stack_buffer[i]; } } @@ -309,9 +276,7 @@ public: } _FORCE_INLINE_ void push_back(char c) { - if (written >= capacity) { - ERR_FAIL_COND(!grow()); } @@ -319,19 +284,16 @@ public: } _FORCE_INLINE_ const char *get_data() const { - return buffer; } }; String FileAccess::get_line() const { - CharBuffer line; CharType c = get_8(); while (!eof_reached()) { - if (c == '\n' || c == '\0') { line.push_back(0); return String::utf8(line.get_data()); @@ -345,7 +307,6 @@ String FileAccess::get_line() const { } Vector FileAccess::get_csv_line(const String &p_delim) const { - ERR_FAIL_COND_V(p_delim.length() != 1, Vector()); String l; @@ -357,7 +318,6 @@ Vector FileAccess::get_csv_line(const String &p_delim) const { l += get_line() + "\n"; qc = 0; for (int i = 0; i < l.length(); i++) { - if (l[i] == '"') qc++; } @@ -371,7 +331,6 @@ Vector FileAccess::get_csv_line(const String &p_delim) const { bool in_quote = false; String current; for (int i = 0; i < l.length(); i++) { - CharType c = l[i]; CharType s[2] = { 0, 0 }; @@ -384,7 +343,6 @@ Vector FileAccess::get_csv_line(const String &p_delim) const { current += s; i++; } else { - in_quote = !in_quote; } } else { @@ -399,7 +357,6 @@ Vector FileAccess::get_csv_line(const String &p_delim) const { } int FileAccess::get_buffer(uint8_t *p_dst, int p_length) const { - int i = 0; for (i = 0; i < p_length && !eof_reached(); i++) p_dst[i] = get_8(); @@ -425,14 +382,12 @@ String FileAccess::get_as_utf8_string() const { } void FileAccess::store_16(uint16_t p_dest) { - uint8_t a, b; a = p_dest & 0xFF; b = p_dest >> 8; if (endian_swap) { - SWAP(a, b); } @@ -440,14 +395,12 @@ void FileAccess::store_16(uint16_t p_dest) { store_8(b); } void FileAccess::store_32(uint32_t p_dest) { - uint16_t a, b; a = p_dest & 0xFFFF; b = p_dest >> 16; if (endian_swap) { - SWAP(a, b); } @@ -455,14 +408,12 @@ void FileAccess::store_32(uint32_t p_dest) { store_16(b); } void FileAccess::store_64(uint64_t p_dest) { - uint32_t a, b; a = p_dest & 0xFFFFFFFF; b = p_dest >> 32; if (endian_swap) { - SWAP(a, b); } @@ -471,7 +422,6 @@ void FileAccess::store_64(uint64_t p_dest) { } void FileAccess::store_real(real_t p_real) { - if (sizeof(real_t) == 4) store_float(p_real); else @@ -479,21 +429,18 @@ void FileAccess::store_real(real_t p_real) { } void FileAccess::store_float(float p_dest) { - MarshallFloat m; m.f = p_dest; store_32(m.i); }; void FileAccess::store_double(double p_dest) { - MarshallDouble m; m.d = p_dest; store_64(m.l); }; uint64_t FileAccess::get_modified_time(const String &p_file) { - if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) return 0; @@ -506,7 +453,6 @@ uint64_t FileAccess::get_modified_time(const String &p_file) { } uint32_t FileAccess::get_unix_permissions(const String &p_file) { - if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) return 0; @@ -519,7 +465,6 @@ uint32_t FileAccess::get_unix_permissions(const String &p_file) { } Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissions) { - FileAccess *fa = create_for_path(p_file); ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create FileAccess for path '" + p_file + "'."); @@ -529,7 +474,6 @@ Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissi } void FileAccess::store_string(const String &p_string) { - if (p_string.length() == 0) return; @@ -538,14 +482,12 @@ void FileAccess::store_string(const String &p_string) { } void FileAccess::store_pascal_string(const String &p_string) { - CharString cs = p_string.utf8(); store_32(cs.length()); store_buffer((uint8_t *)&cs[0], cs.length()); }; String FileAccess::get_pascal_string() { - uint32_t sl = get_32(); CharString cs; cs.resize(sl + 1); @@ -559,13 +501,11 @@ String FileAccess::get_pascal_string() { }; void FileAccess::store_line(const String &p_line) { - store_string(p_line); store_8('\n'); } void FileAccess::store_csv_line(const Vector &p_values, const String &p_delim) { - ERR_FAIL_COND(p_delim.length() != 1); String line = ""; @@ -587,13 +527,11 @@ void FileAccess::store_csv_line(const Vector &p_values, const String &p_ } void FileAccess::store_buffer(const uint8_t *p_src, int p_length) { - for (int i = 0; i < p_length; i++) store_8(p_src[i]); } Vector FileAccess::get_file_as_array(const String &p_path, Error *r_error) { - FileAccess *f = FileAccess::open(p_path, READ, r_error); if (!f) { if (r_error) { // if error requested, do not throw error @@ -609,7 +547,6 @@ Vector FileAccess::get_file_as_array(const String &p_path, Error *r_err } String FileAccess::get_file_as_string(const String &p_path, Error *r_error) { - Error err; Vector array = get_file_as_array(p_path, &err); if (r_error) { @@ -628,7 +565,6 @@ String FileAccess::get_file_as_string(const String &p_path, Error *r_error) { } String FileAccess::get_md5(const String &p_file) { - FileAccess *f = FileAccess::open(p_file, READ); if (!f) return String(); @@ -639,10 +575,8 @@ String FileAccess::get_md5(const String &p_file) { unsigned char step[32768]; while (true) { - int br = f->get_buffer(step, 32768); if (br > 0) { - ctx.update(step, br); } if (br < 4096) @@ -658,7 +592,6 @@ String FileAccess::get_md5(const String &p_file) { } String FileAccess::get_multiple_md5(const Vector &p_file) { - CryptoCore::MD5Context ctx; ctx.start(); @@ -669,10 +602,8 @@ String FileAccess::get_multiple_md5(const Vector &p_file) { unsigned char step[32768]; while (true) { - int br = f->get_buffer(step, 32768); if (br > 0) { - ctx.update(step, br); } if (br < 4096) @@ -688,7 +619,6 @@ String FileAccess::get_multiple_md5(const Vector &p_file) { } String FileAccess::get_sha256(const String &p_file) { - FileAccess *f = FileAccess::open(p_file, READ); if (!f) return String(); @@ -699,10 +629,8 @@ String FileAccess::get_sha256(const String &p_file) { unsigned char step[32768]; while (true) { - int br = f->get_buffer(step, 32768); if (br > 0) { - ctx.update(step, br); } if (br < 4096) -- 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/os/file_access.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'core/os/file_access.cpp') diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index f31842bcae..8b13e53812 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -170,6 +170,7 @@ uint16_t FileAccess::get_16() const { return res; } + uint32_t FileAccess::get_32() const { uint32_t res; uint16_t a, b; @@ -187,6 +188,7 @@ uint32_t FileAccess::get_32() const { return res; } + uint64_t FileAccess::get_64() const { uint64_t res; uint32_t a, b; @@ -394,6 +396,7 @@ void FileAccess::store_16(uint16_t p_dest) { store_8(a); store_8(b); } + void FileAccess::store_32(uint32_t p_dest) { uint16_t a, b; @@ -407,6 +410,7 @@ void FileAccess::store_32(uint32_t p_dest) { store_16(a); store_16(b); } + void FileAccess::store_64(uint64_t p_dest) { uint32_t a, b; -- 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/os/file_access.cpp | 64 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 22 deletions(-) (limited to 'core/os/file_access.cpp') diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index 8b13e53812..f9ba8ff2d2 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -51,12 +51,14 @@ FileAccess *FileAccess::create(AccessType p_access) { } bool FileAccess::exists(const String &p_name) { - if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name)) + if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name)) { return true; + } FileAccess *f = open(p_name, READ); - if (!f) + if (!f) { return false; + } memdelete(f); return true; } @@ -90,8 +92,9 @@ FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_er if (!(p_mode_flags & WRITE) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) { ret = PackedData::get_singleton()->try_open_path(p_path); if (ret) { - if (r_error) + if (r_error) { *r_error = OK; + } return ret; } } @@ -99,8 +102,9 @@ FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_er ret = create_for_path(p_path); Error err = ret->_open(p_path, p_mode_flags); - if (r_error) + if (r_error) { *r_error = err; + } if (err != OK) { memdelete(ret); ret = nullptr; @@ -214,10 +218,11 @@ float FileAccess::get_float() const { }; real_t FileAccess::get_real() const { - if (real_is_double) + if (real_is_double) { return get_double(); - else + } else { return get_float(); + } } double FileAccess::get_double() const { @@ -233,8 +238,9 @@ String FileAccess::get_token() const { while (!eof_reached()) { if (c <= ' ') { - if (token.length()) + if (token.length()) { break; + } } else { token += c; } @@ -299,8 +305,9 @@ String FileAccess::get_line() const { if (c == '\n' || c == '\0') { line.push_back(0); return String::utf8(line.get_data()); - } else if (c != '\r') + } else if (c != '\r') { line.push_back(c); + } c = get_8(); } @@ -314,14 +321,16 @@ Vector FileAccess::get_csv_line(const String &p_delim) const { String l; int qc = 0; do { - if (eof_reached()) + if (eof_reached()) { break; + } l += get_line() + "\n"; qc = 0; for (int i = 0; i < l.length(); i++) { - if (l[i] == '"') + if (l[i] == '"') { qc++; + } } } while (qc % 2); @@ -360,8 +369,9 @@ Vector FileAccess::get_csv_line(const String &p_delim) const { int FileAccess::get_buffer(uint8_t *p_dst, int p_length) const { int i = 0; - for (i = 0; i < p_length && !eof_reached(); i++) + for (i = 0; i < p_length && !eof_reached(); i++) { p_dst[i] = get_8(); + } return i; } @@ -426,10 +436,11 @@ void FileAccess::store_64(uint64_t p_dest) { } void FileAccess::store_real(real_t p_real) { - if (sizeof(real_t) == 4) + if (sizeof(real_t) == 4) { store_float(p_real); - else + } else { store_double(p_real); + } } void FileAccess::store_float(float p_dest) { @@ -445,8 +456,9 @@ void FileAccess::store_double(double p_dest) { }; uint64_t FileAccess::get_modified_time(const String &p_file) { - if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) { return 0; + } FileAccess *fa = create_for_path(p_file); ERR_FAIL_COND_V_MSG(!fa, 0, "Cannot create FileAccess for path '" + p_file + "'."); @@ -457,8 +469,9 @@ uint64_t FileAccess::get_modified_time(const String &p_file) { } uint32_t FileAccess::get_unix_permissions(const String &p_file) { - if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) { return 0; + } FileAccess *fa = create_for_path(p_file); ERR_FAIL_COND_V_MSG(!fa, 0, "Cannot create FileAccess for path '" + p_file + "'."); @@ -478,8 +491,9 @@ Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissi } void FileAccess::store_string(const String &p_string) { - if (p_string.length() == 0) + if (p_string.length() == 0) { return; + } CharString cs = p_string.utf8(); store_buffer((uint8_t *)&cs[0], cs.length()); @@ -531,8 +545,9 @@ void FileAccess::store_csv_line(const Vector &p_values, const String &p_ } void FileAccess::store_buffer(const uint8_t *p_src, int p_length) { - for (int i = 0; i < p_length; i++) + for (int i = 0; i < p_length; i++) { store_8(p_src[i]); + } } Vector FileAccess::get_file_as_array(const String &p_path, Error *r_error) { @@ -570,8 +585,9 @@ String FileAccess::get_file_as_string(const String &p_path, Error *r_error) { String FileAccess::get_md5(const String &p_file) { FileAccess *f = FileAccess::open(p_file, READ); - if (!f) + if (!f) { return String(); + } CryptoCore::MD5Context ctx; ctx.start(); @@ -583,8 +599,9 @@ String FileAccess::get_md5(const String &p_file) { if (br > 0) { ctx.update(step, br); } - if (br < 4096) + if (br < 4096) { break; + } } unsigned char hash[16]; @@ -610,8 +627,9 @@ String FileAccess::get_multiple_md5(const Vector &p_file) { if (br > 0) { ctx.update(step, br); } - if (br < 4096) + if (br < 4096) { break; + } } memdelete(f); } @@ -624,8 +642,9 @@ String FileAccess::get_multiple_md5(const Vector &p_file) { String FileAccess::get_sha256(const String &p_file) { FileAccess *f = FileAccess::open(p_file, READ); - if (!f) + if (!f) { return String(); + } CryptoCore::SHA256Context ctx; ctx.start(); @@ -637,8 +656,9 @@ String FileAccess::get_sha256(const String &p_file) { if (br > 0) { ctx.update(step, br); } - if (br < 4096) + if (br < 4096) { break; + } } unsigned char hash[32]; -- cgit v1.2.3