Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@theStack
Copy link
Contributor

@theStack theStack commented Nov 13, 2019

Since commit 7cee858 ("Add compile time verification of assumptions we're currently making implicitly/tacitly", PR #15391), this check is done in compat/assumptions.h:

// Assumption: We assume that the macro NDEBUG is not defined.
// Example(s): We use assert(...) extensively with the assumption of it never
// being a noop at runtime.
#if defined(NDEBUG)
# error "Bitcoin cannot be compiled without assertions."
#endif

This header is included by util/system.h, which is in turn included by most .cpp files, including the ones this commit modifies (validation.cpp and net_processing.cpp).

Since commit 7cee858 ("Add compile time
verification of assumptions we're currently making implicitly/tacitly"), this
check is done in compat/assumptions.h which is included by util/system.h,
which is in turn included by most .cpp files, including the ones this commit
modifies.
@theStack theStack force-pushed the 20191112-remove-redundant-ndebug-checks branch from f9f952b to 7f3397b Compare November 13, 2019 00:14
@practicalswift
Copy link
Contributor

@theStack

I'm all for removing redundancies in the general case, but in this specific case the redundancy is intentional:

Note that the inclusion of assumptions.h could be removed accidentally from net_processing.cpp and validation.cpp without breaking the build.

Thus we cannot assume that assumptions.h will always be included :)

I think of it this way:

  • Enforcement: The NDEBUG checks in net_processing.cpp and validation.cpp make sure that we enforce this project wide (since at least one of those files likely will be part of all relevant future builds of the project where this should be enforced).
  • Documentation: The NDEBUG check in assumptions.h serves as a documentation of an important assumption we make.

Does that make sense? :)

@theStack
Copy link
Contributor Author

@practicalswift: Thanks for clarifying!
My strong assumption was that in any possible build, one of the dozens .cpp files which currently include the compat/assumptions.h header (which after all says "compile-time verification", implying more than merely documentation) would also do so in the future, triggering the error in case NDEBUG is set.
Also it seemed quite fishy to me that the check was even present in two .cpp files, not just one. Looking deeper into it, that seems to have historic reasons: introduced originally by gmaxwell with commit 9b59e3b, the NDEBUG check was only present in main.cpp. Then later when main.cpp was split up into validation.cpp and net_processing.cpp it got copied over into both files (commits e736772, 76faa3c by TheBlueMatt; part of PR #9260 with a very entertaining title/description 😆 ).

Still feels odd to have the exact same preprocessor construct and error string scattered aorund in three places in the same project, but I get your point.

@laanwj
Copy link
Member

laanwj commented Nov 15, 2019

Tend toward NACK.
This code can go when the run-time critical assumptions stop using assertions. Then it is fully safe to define NDEBUG. Until then, belts and suspenders.
(@MarcoFalke is working on this)

@maflcko
Copy link
Member

maflcko commented Nov 15, 2019

Yeah, if the goal is to make sure the assert can never be disabled by accident, but also avoid these preprocessor checks, you could cherry-pick something like fa15643, replace the std::abort(); with assert(false), and add the preprocessor check to ./src/util/check.h (a single place).

@laanwj
Copy link
Member

laanwj commented Nov 18, 2019

Though, I'd say it's not much of a goal in itself. Wouldn't be surprised if the preprocessor is the least CPU intensive part of the C++ compilation process.

@fanquake
Copy link
Member

fanquake commented Jan 4, 2020

Closing as something that we can potentially revisit in the future.

@fanquake fanquake closed this Jan 4, 2020
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants