-
-
Notifications
You must be signed in to change notification settings - Fork 542
contracts: add violation handler infrastructure and annotated optional #7129
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
Open
Sanchit2662
wants to merge
10
commits into
TheHPXProject:master
Choose a base branch
from
Sanchit2662:poc/contracts-infrastructure
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9e9e28c
contracts: add violation handler infrastructure
Sanchit2662 943199f
contracts: add ENFORCE/OBSERVE/IGNORE mode support
Sanchit2662 f790fd2
contracts: port test cases from PoC into module tests
Sanchit2662 7dfefd1
contracts: fix cmake-format issues
Sanchit2662 dd15484
contracts: address review feedback
Sanchit2662 2f9cfba
contracts: fix CI build failures
Sanchit2662 4f8ca41
futures: annotate future::get() and shared_future::get() with precond…
Sanchit2662 5e294d9
datastructures: annotate small_vector element access with preconditions
Sanchit2662 3fc42f0
datastructures: fix clang-format / cmake-format
Sanchit2662 93e4cfe
algorithms: annotate sort and stable_sort with preconditions
Sanchit2662 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
libs/core/contracts/include/hpx/contracts/violation_handler.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright (c) 2026 The STE||AR-Group | ||
| // | ||
| // SPDX-License-Identifier: BSL-1.0 | ||
| // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <hpx/config.hpp> | ||
| #include <hpx/assertion/source_location.hpp> | ||
|
|
||
| namespace hpx::contracts { | ||
|
|
||
| HPX_CXX_CORE_EXPORT enum class contract_kind { pre, post, assertion }; | ||
|
|
||
| HPX_CXX_CORE_EXPORT struct violation_info | ||
| { | ||
| contract_kind kind; | ||
| char const* condition; | ||
| hpx::source_location location; | ||
| }; | ||
|
|
||
| HPX_CXX_CORE_EXPORT using violation_handler_t = | ||
| void (*)(violation_info const&); | ||
|
|
||
| HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT violation_handler_t | ||
| set_violation_handler(violation_handler_t handler) noexcept; | ||
| HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT violation_handler_t | ||
| get_violation_handler() noexcept; | ||
|
|
||
| HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT void default_violation_handler( | ||
| violation_info const& info); | ||
| HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT void invoke_violation_handler( | ||
| violation_info const& info); | ||
|
|
||
| } // namespace hpx::contracts | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright (c) 2026 The STE||AR-Group | ||
| // | ||
| // SPDX-License-Identifier: BSL-1.0 | ||
| // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
|
||
| #include <hpx/config.hpp> | ||
| #include <hpx/contracts/config/defines.hpp> | ||
|
hkaiser marked this conversation as resolved.
|
||
| #include <hpx/contracts/violation_handler.hpp> | ||
|
|
||
| #include <cstdlib> | ||
| #include <iostream> | ||
|
|
||
| namespace hpx::contracts { | ||
|
|
||
| namespace detail { | ||
|
|
||
| [[nodiscard]] violation_handler_t& get_handler() noexcept | ||
| { | ||
| static violation_handler_t handler = nullptr; | ||
| return handler; | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| violation_handler_t set_violation_handler( | ||
| violation_handler_t handler) noexcept | ||
| { | ||
| violation_handler_t old = detail::get_handler(); | ||
| detail::get_handler() = handler; | ||
| return old; | ||
| } | ||
|
|
||
| violation_handler_t get_violation_handler() noexcept | ||
| { | ||
| return detail::get_handler(); | ||
| } | ||
|
|
||
| void default_violation_handler(violation_info const& info) | ||
| { | ||
| char const* kind_str = nullptr; | ||
| switch (info.kind) | ||
| { | ||
| case contract_kind::pre: | ||
| kind_str = "precondition"; | ||
| break; | ||
| case contract_kind::post: | ||
| kind_str = "postcondition"; | ||
| break; | ||
| case contract_kind::assertion: | ||
| kind_str = "assertion"; | ||
| break; | ||
| } | ||
|
|
||
| std::cerr << info.location << ": Contract " << kind_str << " '" | ||
| << info.condition << "' violated\n"; | ||
|
|
||
| #if !defined(HPX_HAVE_CONTRACTS_MODE) || HPX_HAVE_CONTRACTS_MODE != 2 | ||
| // abort in ENFORCE (and by default); continue in OBSERVE | ||
| std::abort(); | ||
| #endif | ||
| } | ||
|
|
||
| void invoke_violation_handler(violation_info const& info) | ||
| { | ||
| violation_handler_t handler = detail::get_handler(); | ||
| if (handler == nullptr) | ||
| default_violation_handler(info); | ||
| else | ||
| handler(info); | ||
| } | ||
|
|
||
| } // namespace hpx::contracts | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Copyright (c) 2026 The STE||AR-Group | ||
| // | ||
| // SPDX-License-Identifier: BSL-1.0 | ||
| // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
|
||
| #include <hpx/contracts.hpp> | ||
|
|
||
| int main() | ||
| { | ||
| HPX_CONTRACT_ASSERT(false); // default handler should abort here | ||
| return 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better if this was accepting any callable of the given signature:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to confirm , when you said
hpx::unique_function, did you meanhpx::move_only_function? That's what I found in the codebase. Also switching from a raw function pointer to a move-only wrapper would change the internal storage in violation_handler.cpp ; since we can't hold it in a plain static pointer anymore.Makes sense to me if that's the direction, just wanted to confirm before changing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant
move_only_function, sorry for the confusion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sanchit2662 Are you still interested in moving this PR forward?