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/dir_access.cpp | 38 -------------------------------------- 1 file changed, 38 deletions(-) (limited to 'core/os/dir_access.cpp') diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index 53b959a580..520968c9a9 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -36,9 +36,7 @@ #include "core/project_settings.h" String DirAccess::_get_root_path() const { - switch (_access_type) { - case ACCESS_RESOURCES: return ProjectSettings::get_singleton()->get_resource_path(); case ACCESS_USERDATA: @@ -48,9 +46,7 @@ String DirAccess::_get_root_path() const { } } String DirAccess::_get_root_string() const { - switch (_access_type) { - case ACCESS_RESOURCES: return "res://"; case ACCESS_USERDATA: @@ -61,7 +57,6 @@ String DirAccess::_get_root_string() const { } int DirAccess::get_current_drive() { - String path = get_current_dir().to_lower(); for (int i = 0; i < get_drive_count(); i++) { String d = get_drive(i).to_lower(); @@ -73,21 +68,17 @@ int DirAccess::get_current_drive() { } bool DirAccess::drives_are_shortcuts() { - return false; } static Error _erase_recursive(DirAccess *da) { - List dirs; List files; da->list_dir_begin(); String n = da->get_next(); while (n != String()) { - if (n != "." && n != "..") { - if (da->current_is_dir()) dirs.push_back(n); else @@ -100,10 +91,8 @@ static Error _erase_recursive(DirAccess *da) { da->list_dir_end(); for (List::Element *E = dirs.front(); E; E = E->next()) { - Error err = da->change_dir(E->get()); if (err == OK) { - err = _erase_recursive(da); if (err) { da->change_dir(".."); @@ -123,7 +112,6 @@ static Error _erase_recursive(DirAccess *da) { } for (List::Element *E = files.front(); E; E = E->next()) { - Error err = da->remove(da->get_current_dir().plus_file(E->get())); if (err) { return err; @@ -134,12 +122,10 @@ static Error _erase_recursive(DirAccess *da) { } Error DirAccess::erase_contents_recursive() { - return _erase_recursive(this); } Error DirAccess::make_dir_recursive(String p_dir) { - if (p_dir.length() < 1) { return OK; }; @@ -178,11 +164,9 @@ Error DirAccess::make_dir_recursive(String p_dir) { String curpath = base; for (int i = 0; i < subdirs.size(); i++) { - curpath = curpath.plus_file(subdirs[i]); Error err = make_dir(curpath); if (err != OK && err != ERR_ALREADY_EXISTS) { - ERR_FAIL_V(err); } } @@ -191,17 +175,12 @@ Error DirAccess::make_dir_recursive(String p_dir) { } String DirAccess::fix_path(String p_path) const { - switch (_access_type) { - case ACCESS_RESOURCES: { - if (ProjectSettings::get_singleton()) { if (p_path.begins_with("res://")) { - String resource_path = ProjectSettings::get_singleton()->get_resource_path(); if (resource_path != "") { - return p_path.replace_first("res:/", resource_path); }; return p_path.replace_first("res://", ""); @@ -210,12 +189,9 @@ String DirAccess::fix_path(String p_path) const { } break; case ACCESS_USERDATA: { - if (p_path.begins_with("user://")) { - String data_dir = OS::get_singleton()->get_user_data_dir(); if (data_dir != "") { - return p_path.replace_first("user:/", data_dir); }; return p_path.replace_first("user://", ""); @@ -223,7 +199,6 @@ String DirAccess::fix_path(String p_path) const { } break; case ACCESS_FILESYSTEM: { - return p_path; } break; case ACCESS_MAX: @@ -236,16 +211,12 @@ String DirAccess::fix_path(String p_path) const { DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX] = { nullptr, nullptr, nullptr }; DirAccess *DirAccess::create_for_path(const String &p_path) { - DirAccess *da = nullptr; if (p_path.begins_with("res://")) { - da = create(ACCESS_RESOURCES); } else if (p_path.begins_with("user://")) { - da = create(ACCESS_USERDATA); } else { - da = create(ACCESS_FILESYSTEM); } @@ -253,7 +224,6 @@ DirAccess *DirAccess::create_for_path(const String &p_path) { } DirAccess *DirAccess::open(const String &p_path, Error *r_error) { - DirAccess *da = create_for_path(p_path); ERR_FAIL_COND_V_MSG(!da, nullptr, "Cannot create DirAccess for path '" + p_path + "'."); @@ -269,7 +239,6 @@ DirAccess *DirAccess::open(const String &p_path, Error *r_error) { } DirAccess *DirAccess::create(AccessType p_access) { - DirAccess *da = create_func[p_access] ? create_func[p_access]() : nullptr; if (da) { da->_access_type = p_access; @@ -279,7 +248,6 @@ DirAccess *DirAccess::create(AccessType p_access) { }; String DirAccess::get_full_path(const String &p_path, AccessType p_access) { - DirAccess *d = DirAccess::create(p_access); if (!d) return p_path; @@ -291,7 +259,6 @@ String DirAccess::get_full_path(const String &p_path, AccessType p_access) { } Error DirAccess::copy(String p_from, String p_to, int p_chmod_flags) { - //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data()); Error err; FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ, &err); @@ -303,7 +270,6 @@ Error DirAccess::copy(String p_from, String p_to, int p_chmod_flags) { FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE, &err); if (err) { - fsrc->close(); memdelete(fsrc); ERR_PRINT("Failed to open " + p_to); @@ -315,7 +281,6 @@ Error DirAccess::copy(String p_from, String p_to, int p_chmod_flags) { fsrc->seek(0); err = OK; while (size--) { - if (fsrc->get_error() != OK) { err = fsrc->get_error(); break; @@ -367,9 +332,7 @@ Error DirAccess::_copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flag list_dir_begin(); String n = get_next(); while (n != String()) { - if (n != "." && n != "..") { - if (current_is_dir()) dirs.push_back(n); else { @@ -440,7 +403,6 @@ Error DirAccess::copy_dir(String p_from, String p_to, int p_chmod_flags) { } bool DirAccess::exists(String p_dir) { - DirAccess *da = DirAccess::create_for_path(p_dir); bool valid = da->change_dir(p_dir) == OK; memdelete(da); -- 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/dir_access.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'core/os/dir_access.cpp') diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index 520968c9a9..5763c74862 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -45,6 +45,7 @@ String DirAccess::_get_root_path() const { return ""; } } + String DirAccess::_get_root_string() const { switch (_access_type) { case ACCESS_RESOURCES: -- 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/dir_access.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'core/os/dir_access.cpp') diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index 5763c74862..9f2672e038 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -61,8 +61,9 @@ int DirAccess::get_current_drive() { String path = get_current_dir().to_lower(); for (int i = 0; i < get_drive_count(); i++) { String d = get_drive(i).to_lower(); - if (path.begins_with(d)) + if (path.begins_with(d)) { return i; + } } return 0; @@ -80,10 +81,11 @@ static Error _erase_recursive(DirAccess *da) { String n = da->get_next(); while (n != String()) { if (n != "." && n != "..") { - if (da->current_is_dir()) + if (da->current_is_dir()) { dirs.push_back(n); - else + } else { files.push_back(n); + } } n = da->get_next(); @@ -147,13 +149,13 @@ Error DirAccess::make_dir_recursive(String p_dir) { String base; - if (full_dir.begins_with("res://")) + if (full_dir.begins_with("res://")) { base = "res://"; - else if (full_dir.begins_with("user://")) + } else if (full_dir.begins_with("user://")) { base = "user://"; - else if (full_dir.begins_with("/")) + } else if (full_dir.begins_with("/")) { base = "/"; - else if (full_dir.find(":/") != -1) { + } else if (full_dir.find(":/") != -1) { base = full_dir.substr(0, full_dir.find(":/") + 2); } else { ERR_FAIL_V(ERR_INVALID_PARAMETER); @@ -229,8 +231,9 @@ DirAccess *DirAccess::open(const String &p_path, Error *r_error) { ERR_FAIL_COND_V_MSG(!da, nullptr, "Cannot create DirAccess for path '" + p_path + "'."); Error err = da->change_dir(p_path); - if (r_error) + if (r_error) { *r_error = err; + } if (err != OK) { memdelete(da); return nullptr; @@ -250,8 +253,9 @@ DirAccess *DirAccess::create(AccessType p_access) { String DirAccess::get_full_path(const String &p_path, AccessType p_access) { DirAccess *d = DirAccess::create(p_access); - if (!d) + if (!d) { return p_path; + } d->change_dir(p_path); String full = d->get_current_dir(); @@ -298,8 +302,9 @@ Error DirAccess::copy(String p_from, String p_to, int p_chmod_flags) { fdst->close(); err = FileAccess::set_unix_permissions(p_to, p_chmod_flags); // If running on a platform with no chmod support (i.e., Windows), don't fail - if (err == ERR_UNAVAILABLE) + if (err == ERR_UNAVAILABLE) { err = OK; + } } memdelete(fsrc); @@ -334,9 +339,9 @@ Error DirAccess::_copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flag String n = get_next(); while (n != String()) { if (n != "." && n != "..") { - if (current_is_dir()) + if (current_is_dir()) { dirs.push_back(n); - else { + } else { const String &rel_path = n; if (!n.is_rel_path()) { list_dir_end(); -- cgit v1.2.3