Skip to content

cpp2 compiles and run without exceptions and rtti #187

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

Closed
wants to merge 1 commit into from

Conversation

prenaux
Copy link
Contributor

@prenaux prenaux commented Dec 30, 2022

Some of my code disables exceptions & rtti. This PR makes minor changes in cpp2util.h to allow cpp2 compiled code to compile & run with both exceptions and RTTI disabled.

#if defined CPP2_THROW
#error "Do not redefine CPP2_THROW when using cpp2 with exceptions enabled."
#endif
#define CPP2_THROW(DESC,EXCEPTION) throw EXCEPTION
Copy link
Contributor Author

Choose a reason for hiding this comment

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

DESC & EXCEPTION are separated because #EXCEPTION only work for literals.

@hsutter
Copy link
Owner

hsutter commented Jan 4, 2023

Thanks for your pull request! It looks like this may be your first contribution to cppfront. Before I can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). I think I sent it a few days ago, but it appears to have not been deliverable so please send me email to coordinate... thanks.

@prenaux prenaux force-pushed the pierre-cpp2-without-exc-rtti branch from e1988a9 to f3eb30c Compare January 6, 2023 08:16
@prenaux
Copy link
Contributor Author

prenaux commented Jan 6, 2023

Rebased on main.

@hsutter
Copy link
Owner

hsutter commented Jan 9, 2023

Thanks, I appreciate this input and suggestion.

How will the dynamic_cast based as implementation work without RTTI?

I'm generally sympathetic to the costs of EH and that a lot of projects disable it, and in Cpp2 I do aim to eventually implement value-based exceptions (P0709). And I'm supportive of efforts to make RTTI costs better, especially on Windows.

But I'm not sure whether I can yet take the dependency on running in environments that don't support EH and RTTI. For example, IIUC the .DS_Store works only on macOS... what will I do on Linux? Windows? I don't want to create and support a parallel mechanism for reporting errors like std::filesystem chose to do, I think that bifurcation is a symptom of the problem (as I explain in P0709) rather than a solution.

I do appreciate this and I do understand the prevalence of environments that disable EH and RTTI. I'm just not sure I'm yet in a state where I can support compatibility with those environments...? Perhaps a better solution than .DS_Store would be to just brute-force disable the parts of cpp2util.h that depend on EH and RTTI in environments that can't handle EH and RTTI... but then that would appear to mean disabling large parts of is and as which are pretty fundamental?

I'll think about this some more. Thanks for the input and the food for thought.

@prenaux prenaux force-pushed the pierre-cpp2-without-exc-rtti branch from f3eb30c to ec90d5b Compare January 9, 2023 05:37
@prenaux
Copy link
Contributor Author

prenaux commented Jan 9, 2023

.DS_Store is noise from the macOS Finder app which it adds as soon as you look at a folder, similar to Thumbs.db & desktop.ini with Windows Explorer. Its not related to this PR so I removed it.

How will the dynamic_cast based as implementation work without RTTI?

It wont, the change I made is just to make the code compile. That code doesn't use as so it does compile and run. If a no-RTTI mode is supported it should disable features that depends on it. Cpp1 doesn't allow you to use dynamic_cast, typeid or throw with rtti/exceptions disabled either.

RTTI and exceptions are disabled in our case on wasm targets for the web (compiled with emscripten) because it adds about ~10-20% to the wasm output and it runs a bit slower even though the code doesn't use it. A chunk of our code and nice to have features of our libraries are also disabled on wasm to keep the final wasm output as small as possible since download size is a very significant factor when we release the application.

At the end of the day if the language is so much better with those features that cost might be acceptable, but in the case of Cpp1 its just not.

This could change with Cpp2, but atm trying out bits of cpp2 in various places its simpler if it supports a no-rtti/no-exceptions mode in our case.

@hsutter hsutter closed this in 5575ec4 Jan 9, 2023
@hsutter
Copy link
Owner

hsutter commented Jan 9, 2023

OK, after thinking on this more I've merged this via another commit doing mostly what you're suggesting, and surface the option as a compiler flag:

  • add a "Support for constrained target environment" compiler flags section with -fno-exceptions and -fno-rtti, which if set get passed through to cpp2util.h via a #define (similar to the way -add-source-info causes CPP2_USE_SOURCE_LOCATION to be set, and -p causes CPP2_USE_MODULES to be set)
  • wrap Throw, Uncaught_exceptions, Dynamic_cast, and Typeid, which test those settings in their bodies, and try to give nice error messages if users try to exercise the (few) features each switch disables (much like your PR does, plus final_action will still run if an exception actually does happen)
  • minor other housekeeping, like also don't try to include <exception> and/or <type_info> if disabled

@prenaux Would you please give this a try in your WASM environment, and let me know if it addresses your needs?

@prenaux prenaux deleted the pierre-cpp2-without-exc-rtti branch January 11, 2023 06:47
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this pull request Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants