Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to C++17 #64152

Merged
merged 8 commits into from
Mar 16, 2023
Merged

Switch to C++17 #64152

merged 8 commits into from
Mar 16, 2023

Conversation

jbytheway
Copy link
Contributor

@jbytheway jbytheway commented Mar 11, 2023

Summary

Infrastructure "Convert build to C++17 (from C++14)"

Purpose of change

Following up on #63970 and #64011.

We want to move to C++17 to get new features and generally keep the codebase up to date.

Describe the solution

Update each of the build files to use C++17 (Makefile, CMakeLists.txt, Visual Studio projects).

Fix the various compiler errors and clang-tidy warnings that arise.

Describe alternatives you've considered

Waiting until #64011 was already merged. It is merged now.

Testing

Unit tests, clang-tidy.

Mostly this needs to be tested on CI, so I have to open it as a non-draft, but marking it WIP because there's bound to be issues on other compilers.

Additional context

@jbytheway jbytheway requested a review from KorGgenT as a code owner March 11, 2023 15:17
@jbytheway jbytheway changed the title [WIP] Switch to cpp17 [WIP] Switch to C++17 Mar 11, 2023
@irwiss
Copy link
Contributor

irwiss commented Mar 11, 2023

Considering not all cpp17 features can be used; can structured binding be used? Asking both from compiler support and code style perspectives? E.g.

nutrients this_min;
nutrients this_max;
std::tie( this_min, this_max ) = compute_nutrient_range( result_it, rec, extra_flags );

into
auto [this_min, this_max] = compute_nutrient_range(...
(as you can see type information isn't visible)
Decomposing pairs is by far the most annoying part of using them

@github-actions github-actions bot added [C++] Changes (can be) made in C++. Previously named `Code` Character / World Generation Issues and enhancements concerning stages of creating a character or a world Code: Build Issues regarding different builds and build environments Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Code: Tests Measurement, self-control, statistics, balancing. Code: Tooling Tooling that is not part of the main game but is part of the repo. Crafting / Construction / Recipes Includes: Uncrafting / Disassembling Info / User Interface Game - player communication, menus, etc. Items: Containers Things that hold other things Limbs Limbs, mutable limbs, and code related to them. Map / Mapgen Overmap, Mapgen, Map extras, Map display Mechanics: Enchantments / Spells Enchantments and spells Melee Melee weapons, tactics, techniques, reach attack Missions Quests and missions Mutations / Traits / Professions/ Hobbies Mutations / Traits / Professions/ Hobbies NPC / Factions NPCs, AI, Speech, Factions, Ownership Vehicles Vehicles, parts, mechanics & interactions json-styled JSON lint passed, label assigned by github actions astyled astyled PR, label is assigned by github actions labels Mar 11, 2023
@jbytheway
Copy link
Contributor Author

From a compiler support perspective, yes, structured bindings are fine.

From a code style perspective, I think we will probably allow them in certain situations. But since we do have the "almost always avoid auto" rule, we'll probably want to be cautious about how we adopt them. Only time (and @kevingranade) will tell.

At the least I hope to use them for iterating over containers of pairs, which is a super common pattern in our codebase.

@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Mar 11, 2023
@jbytheway
Copy link
Contributor Author

For reference, the C++17 features which we should avoid are documented here,

@BrettDong
Copy link
Member

Maybe revert back to ghc file system and see if permission errors go away?

@jbytheway
Copy link
Contributor Author

Maybe revert back to ghc file system and see if permission errors go away?

@akrieger is hoping to test locally and see what can be done.

@kevingranade
Copy link
Member

I saw an instance of this in an unrelated PR yesterday and it resolved with a rerun, so at a minimum I don't think this PR is the cause.

@jbytheway
Copy link
Contributor Author

Good to know, thanks!

@RAldrich
Copy link
Contributor

I would appreciate it if we could resolve #64210 before merging this. (Not going to fix / dropping Debian support is totally a possible resolution - I just think it would be best to make a decision before merging additional compiler changes).

@jbytheway
Copy link
Contributor Author

I think the issue in #64210 should be mostly orthogonal to this, but thanks for the heads up.

@akrieger
Copy link
Member

Same failure again, seems suspect. I scrolled through other recent failures and didn't see the same.

I tested yesterday and didn't repro, but realized just now I tested with 2022, so I'm going to revert back to 2019 and try again today.

@BrettDong
Copy link
Member

Successfully reproduced the error. Built with VC2019 build tools.

image

@BrettDong
Copy link
Member

The error is caused by the trailing / in the path requested. I made it working in VC2019 by removing trailing slash:

image

diff --git a/src/filesystem.cpp b/src/filesystem.cpp
index e498da3736..d437f50470 100644
--- a/src/filesystem.cpp
+++ b/src/filesystem.cpp
@@ -21,7 +21,11 @@

 bool assure_dir_exist( const std::string &path )
 {
-    return assure_dir_exist( fs::u8path( path ) );
+    std::string p = path;
+    if (!p.empty() && p.back() == '/') {
+        p.pop_back();
+    }
+    return assure_dir_exist( fs::u8path( p ));
 }

 bool assure_dir_exist( const cata_path &path )

@akrieger
Copy link
Member

akrieger commented Mar 13, 2023

[edit: apparently not this bug in the VS patch notes] I saw something related to that in the some 2019 patch notes. I believe there are many fixes listed in https://learn.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements-2019?view=msvc-170 which are backports of recent standards committee meetings, including stuff about trailing slashes like mentioned in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87846

@akrieger
Copy link
Member

Specifically https://cplusplus.github.io/LWG/issue3096 is probably the relevant change.

@jbytheway
Copy link
Contributor Author

Thanks for the diagnosis. I've pushed a tentative fix.

@kevingranade
Copy link
Member

Regarding the item density test, I thought it was something like that too, but it turns out it's much more prosaic, the clang++-12 workflow is the first one that runs with magiclysm loaded, and you were hitting a magiclysm-specific data error that was fixed in #64258
Other than that, it looks like this will be good to go with a rebase.

Convert the Makefile, CMakeLists, and Visual Studio projects to use
C++17.
- std::uncaught_exception is deprecated.
- std::is_literal is deprecated.
- Some small things in the filesystem library have changed.
- [[nodiscard]] is now available, so suppress the clang-tidy check
  suggesting it.
A few small changes in C++17 mean that clang-tidy is now able to apply a
few more fixes.  Do so.
To help me debug CI issues.
@jbytheway
Copy link
Contributor Author

OK, I've removed the item density fix and rebased on master.

Based on previous experience on this PR, the CI is going to take a long time to run (presumably because all the cached data is useless here).

Copy link
Member

@akrieger akrieger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of this lint shouldve been caught pre-17, no?

tests/list_test.cpp Outdated Show resolved Hide resolved
src/tgz_archiver.h Outdated Show resolved Hide resolved
src/harvest.cpp Show resolved Hide resolved
@jbytheway jbytheway changed the title [WIP] Switch to C++17 Switch to C++17 Mar 15, 2023
@jbytheway
Copy link
Contributor Author

I think this is good to go, so I've removed the WIP label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions [C++] Changes (can be) made in C++. Previously named `Code` Character / World Generation Issues and enhancements concerning stages of creating a character or a world Code: Build Issues regarding different builds and build environments Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Code: Tests Measurement, self-control, statistics, balancing. Code: Tooling Tooling that is not part of the main game but is part of the repo. Crafting / Construction / Recipes Includes: Uncrafting / Disassembling Info / User Interface Game - player communication, menus, etc. Items: Containers Things that hold other things json-styled JSON lint passed, label assigned by github actions Limbs Limbs, mutable limbs, and code related to them. Map / Mapgen Overmap, Mapgen, Map extras, Map display Mechanics: Enchantments / Spells Enchantments and spells Melee Melee weapons, tactics, techniques, reach attack Missions Quests and missions Mutations / Traits / Professions/ Hobbies Mutations / Traits / Professions/ Hobbies NPC / Factions NPCs, AI, Speech, Factions, Ownership Vehicles Vehicles, parts, mechanics & interactions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants