The C++26 contracts design bears little relationship with the abandoned C++2a one.
While we have, where possible, attempted to re-use code, the underlying syntax and
implementation are divergent.
We have thus decided to represent this as a new implementation, after first removing
the old one. Trying to review the patches as changes between two different designs
would be quite confusing.
gcc/c-family/ChangeLog:
* c-cppbuiltin.cc (c_cpp_builtins): Remove C++2a-specific
feature macros.
* c.opt: Remove C++2a-specific flags.
gcc/cp/ChangeLog:
* contracts.cc: Remove all C++2a contracts code.
* contracts.h: Likewise.
* coroutines.cc (coro_build_actor_or_destroy_function): Remove
unused contracts references.
* cp-gimplify.cc (cp_genericize_r): Remove C++2a contracts
implementation.
* cp-objcp-common.cc (cp_handle_option): Likewise.
* cp-tree.h: Remove C++2a contracts code.
* decl.cc: Likewise.
* decl2.cc: Likewise.
* mangle.cc: Likewise.
* module.cc: Likewise.
* parser.cc: Likewise.
* pt.cc: Likewise.
* search.cc: Likewise.
* semantics.cc: Likewise.
* tree.cc: Likewise.
* constexpr.cc: Likewise.
gcc/ChangeLog:
* doc/invoke.texi: Remove documentation of contracts options.
gcc/testsuite/ChangeLog:
* g++.dg/contracts/contracts-access1.C: Removed.
* g++.dg/contracts/contracts-assume1.C: Removed.
* g++.dg/contracts/contracts-assume2.C: Removed.
* g++.dg/contracts/contracts-assume3.C: Removed.
* g++.dg/contracts/contracts-assume4.C: Removed.
* g++.dg/contracts/contracts-assume5.C: Removed.
* g++.dg/contracts/contracts-assume6.C: Removed.
* g++.dg/contracts/contracts-comdat1.C: Removed.
* g++.dg/contracts/contracts-config1.C: Removed.
* g++.dg/contracts/contracts-constexpr1.C: Removed.
* g++.dg/contracts/contracts-constexpr2.C: Removed.
* g++.dg/contracts/contracts-constexpr3.C: Removed.
* g++.dg/contracts/contracts-conversion1.C: Removed.
* g++.dg/contracts/contracts-ctor-dtor1.C: Removed.
* g++.dg/contracts/contracts-ctor-dtor2.C: Removed.
* g++.dg/contracts/contracts-cv1.C: Removed.
* g++.dg/contracts/contracts-deduced1.C: Removed.
* g++.dg/contracts/contracts-deduced2.C: Removed.
* g++.dg/contracts/contracts-err1.C: Removed.
* g++.dg/contracts/contracts-externC.C: Removed.
* g++.dg/contracts/contracts-friend1.C: Removed.
* g++.dg/contracts/contracts-ft1.C: Removed.
* g++.dg/contracts/contracts-ignore1.C: Removed.
* g++.dg/contracts/contracts-ignore2.C: Removed.
* g++.dg/contracts/contracts-large-return.C: Removed.
* g++.dg/contracts/contracts-multiline1.C: Removed.
* g++.dg/contracts/contracts-multiple-inheritance1.C: Removed.
* g++.dg/contracts/contracts-multiple-inheritance2.C: Removed.
* g++.dg/contracts/contracts-nested-class1.C: Removed.
* g++.dg/contracts/contracts-nested-class2.C: Removed.
* g++.dg/contracts/contracts-nocopy1.C: Removed.
* g++.dg/contracts/contracts-override.C: Removed.
* g++.dg/contracts/contracts-post1.C: Removed.
* g++.dg/contracts/contracts-post2.C: Removed.
* g++.dg/contracts/contracts-post3.C: Removed.
* g++.dg/contracts/contracts-post4.C: Removed.
* g++.dg/contracts/contracts-post5.C: Removed.
* g++.dg/contracts/contracts-post6.C: Removed.
* g++.dg/contracts/contracts-post7.C: Removed.
* g++.dg/contracts/contracts-pre1.C: Removed.
* g++.dg/contracts/contracts-pre10.C: Removed.
* g++.dg/contracts/contracts-pre2.C: Removed.
* g++.dg/contracts/contracts-pre2a1.C: Removed.
* g++.dg/contracts/contracts-pre2a2.C: Removed.
* g++.dg/contracts/contracts-pre3.C: Removed.
* g++.dg/contracts/contracts-pre4.C: Removed.
* g++.dg/contracts/contracts-pre5.C: Removed.
* g++.dg/contracts/contracts-pre6.C: Removed.
* g++.dg/contracts/contracts-pre7.C: Removed.
* g++.dg/contracts/contracts-pre9.C: Removed.
* g++.dg/contracts/contracts-redecl1.C: Removed.
* g++.dg/contracts/contracts-redecl2.C: Removed.
* g++.dg/contracts/contracts-redecl3.C: Removed.
* g++.dg/contracts/contracts-redecl4.C: Removed.
* g++.dg/contracts/contracts-redecl5.C: Removed.
* g++.dg/contracts/contracts-redecl6.C: Removed.
* g++.dg/contracts/contracts-redecl7.C: Removed.
* g++.dg/contracts/contracts-redecl8.C: Removed.
* g++.dg/contracts/contracts-tmpl-attr1.C: Removed.
* g++.dg/contracts/contracts-tmpl-spec1.C: Removed.
* g++.dg/contracts/contracts-tmpl-spec2.C: Removed.
* g++.dg/contracts/contracts-tmpl-spec3.C: Removed.
* g++.dg/contracts/contracts1.C: Removed.
* g++.dg/contracts/contracts10.C: Removed.
* g++.dg/contracts/contracts11.C: Removed.
* g++.dg/contracts/contracts12.C: Removed.
* g++.dg/contracts/contracts13.C: Removed.
* g++.dg/contracts/contracts14.C: Removed.
* g++.dg/contracts/contracts15.C: Removed.
* g++.dg/contracts/contracts16.C: Removed.
* g++.dg/contracts/contracts17.C: Removed.
* g++.dg/contracts/contracts18.C: Removed.
* g++.dg/contracts/contracts19.C: Removed.
* g++.dg/contracts/contracts2.C: Removed.
* g++.dg/contracts/contracts20.C: Removed.
* g++.dg/contracts/contracts22.C: Removed.
* g++.dg/contracts/contracts24.C: Removed.
* g++.dg/contracts/contracts25.C: Removed.
* g++.dg/contracts/contracts3.C: Removed.
* g++.dg/contracts/contracts35.C: Removed.
* g++.dg/contracts/contracts4.C: Removed.
* g++.dg/contracts/contracts5.C: Removed.
* g++.dg/contracts/contracts6.C: Removed.
* g++.dg/contracts/contracts7.C: Removed.
* g++.dg/contracts/pr110159.C: Removed.
* g++.dg/contracts/pr113968.C: Removed.
* g++.dg/contracts/pr115434.C: Removed.
* g++.dg/contracts/pr116490.C: Removed.
* g++.dg/contracts/pr116607.C: Removed.
* g++.dg/contracts/pr117579.C: Removed.
* g++.dg/contracts/noexcept_preload_handler/Makefile: Removed.
* g++.dg/contracts/noexcept_preload_handler/README: Removed.
* g++.dg/contracts/noexcept_preload_handler/assert_fail.cpp: Removed.
* g++.dg/contracts/noexcept_preload_handler/handle_contract_violation.cpp: Removed.
* g++.dg/contracts/preload_handler/Makefile: Removed.
* g++.dg/contracts/preload_handler/README: Removed.
* g++.dg/contracts/preload_handler/assert_fail.cpp: Removed.
* g++.dg/contracts/preload_handler/handle_contract_violation.cpp: Removed.
* g++.dg/contracts/preload_nocontinue_handler/Makefile: Removed.
* g++.dg/contracts/preload_nocontinue_handler/README: Removed.
* g++.dg/contracts/preload_nocontinue_handler/assert_fail.cpp: Removed.
* g++.dg/contracts/preload_nocontinue_handler/handle_contract_violation.cpp: Removed.
* g++.dg/contracts/preload_nocontinue_handler/nocontinue.cpp: Removed.
* g++.dg/contracts/backtrace_handler/Makefile: Removed.
* g++.dg/contracts/backtrace_handler/README: Removed.
* g++.dg/contracts/backtrace_handler/assert_fail.cpp: Removed.
* g++.dg/contracts/backtrace_handler/example_out.txt: Removed.
* g++.dg/contracts/backtrace_handler/example_pretty.txt: Removed.
* g++.dg/contracts/backtrace_handler/handle_contract_violation.cpp: Removed.
* g++.dg/contracts/backtrace_handler/prettytrace.sh: Removed.
* g++.dg/coroutines/pr110871.C: Removed.
* g++.dg/coroutines/pr110872.C: Removed.
* g++.dg/modules/contracts-1_a.C: Removed.
* g++.dg/modules/contracts-1_b.C: Removed.
* g++.dg/modules/contracts-2_a.C: Removed.
* g++.dg/modules/contracts-2_b.C: Removed.
* g++.dg/modules/contracts-2_c.C: Removed.
* g++.dg/modules/contracts-3_a.C: Removed.
* g++.dg/modules/contracts-3_b.C: Removed.
* g++.dg/modules/contracts-4_a.C: Removed.
* g++.dg/modules/contracts-4_b.C: Removed.
* g++.dg/modules/contracts-4_c.C: Removed.
* g++.dg/modules/contracts-4_d.C: Removed.
* g++.dg/modules/contracts-5_a.C: Removed.
* g++.dg/modules/contracts-5_b.C: Removed.
* g++.dg/modules/contracts-tpl-friend-1_a.C: Removed.
* g++.dg/modules/contracts-tpl-friend-1_b.C: Removed.
* g++.dg/contracts/contracts8.C: Removed.
* g++.dg/contracts/contracts9.C: Removed.
* g++.dg/contracts/except_preload_handler/Makefile: Removed.
* g++.dg/contracts/except_preload_handler/README: Removed.
* g++.dg/contracts/except_preload_handler/assert_fail.cpp: Removed.
* g++.dg/contracts/except_preload_handler/handle_contract_violation.cpp: Removed.
Co-Authored-by: Nina Ranns <dinka.ranns@gmail.com>
Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
cpp_warn (pfile, "__cpp_concepts");
if (flag_contracts)
{
- cpp_define (pfile, "__cpp_contracts=201906L");
- cpp_define (pfile, "__cpp_contracts_literal_semantics=201906L");
- cpp_define (pfile, "__cpp_contracts_roles=201906L");
}
else if (cxx_dialect >= cxx26)
cpp_warn (pfile, "__cpp_contracts");
C++ ObjC++ Var(flag_contracts) Init(0)
Enable certain features present in drafts of C++ Contracts.
-Enum
-Name(on_off) Type(int) UnknownError(argument %qs must be either %<on%> or %<off%>)
-
-EnumValue
-Enum(on_off) String(off) Value(0)
-
-EnumValue
-Enum(on_off) String(on) Value(1)
-
-fcontract-assumption-mode=
-C++ ObjC++ Joined RejectNegative
--fcontract-assumption-mode=[on|off] Enable or disable treating axiom level contracts as assumptions (default on).
-
-fcontract-build-level=
-C++ ObjC++ Joined RejectNegative
--fcontract-build-level=[off|default|audit] Specify max contract level to generate runtime checks for.
-
-fcontract-strict-declarations=
-C++ ObjC++ Var(flag_contract_strict_declarations) Enum(on_off) Joined Init(0) RejectNegative
--fcontract-strict-declarations=[on|off] Enable or disable warnings on generalized redeclaration of functions with contracts (default off).
-
-fcontract-mode=
-C++ ObjC++ Var(flag_contract_mode) Enum(on_off) Joined Init(1) RejectNegative
--fcontract-mode=[on|off] Enable or disable all contract facilities (default on).
-
-fcontract-continuation-mode=
-C++ ObjC++ Joined RejectNegative
--fcontract-continuation-mode=[on|off] Enable or disable contract continuation mode (default off).
-
-fcontract-role=
-C++ ObjC++ Joined RejectNegative
--fcontract-role=<name>:<semantics> Specify the semantics for all levels in a role (default, review), or a custom contract role with given semantics (ex: opt:assume,assume,assume).
-
-fcontract-semantic=
-C++ ObjC++ Joined RejectNegative
--fcontract-semantic=<level>:<semantic> Specify the concrete semantics for level.
-
fcoroutines
C++ ObjC++ LTO Var(flag_coroutines)
Enable C++ coroutines (experimental).
#include "fold-const.h"
#include "intl.h"
#include "toplev.h"
-#include "contracts.h"
static bool verify_constant (tree, bool, bool *, bool *);
#define VERIFY_CONSTANT(X) \
case ASSERTION_STMT:
case PRECONDITION_STMT:
case POSTCONDITION_STMT:
- {
- if (contract_ignored_p (t))
- break;
-
- if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
- G_("contract predicate is false in "
- "constant expression"),
- EXPR_LOCATION (t), contract_evaluated_p (t),
- non_constant_p, overflow_p))
- *non_constant_p = true;
- r = void_node;
- }
+ gcc_checking_assert (false && "hoo...");
break;
case TEMPLATE_ID_EXPR:
case ASSERTION_STMT:
case PRECONDITION_STMT:
case POSTCONDITION_STMT:
- if (!checked_contract_p (get_contract_semantic (t)))
- return true;
- return RECUR (CONTRACT_CONDITION (t), rval);
+ gcc_checking_assert (false && "hmm...");
case LABEL_EXPR:
t = LABEL_EXPR_LABEL (t);
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
-
-/* Design Notes
-
- A function is called a "guarded" function if it has pre or post contract
- attributes. A contract is considered an "active" contract if runtime code is
- needed for the contract under the current contract configuration.
-
- pre and post contract attributes are parsed and stored in DECL_ATTRIBUTES.
- assert contracts are parsed and wrapped in statements. When genericizing, all
- active and assumed contracts are transformed into an if block. An observed
- contract:
-
- [[ pre: v > 0 ]]
-
- is transformed into:
-
- if (!(v > 0)) {
- handle_contract_violation(__pseudo_contract_violation{
- 5, // line_number,
- "main.cpp", // file_name,
- "fun", // function_name,
- "v > 0", // comment,
- "default", // assertion_level,
- "default", // assertion_role,
- maybe_continue, // continuation_mode
- });
- terminate (); // if never_continue
- }
-
- We use an internal type with the same layout as contract_violation rather
- than try to define the latter internally and somehow deal with its actual
- definition in a TU that includes <contract>.
-
- ??? is it worth factoring out the calls to handle_contract_violation and
- terminate into a local function?
-
- Assumed contracts use the same implementation as C++23 [[assume]].
-
- Parsing of pre and post contract conditions need to be deferred when the
- contracts are attached to a member function. The postcondition identifier
- cannot be used before the deduced return type of an auto function is used,
- except when used in a defining declaration in which case they conditions are
- fully parsed once the body is finished (see cpp2a/contracts-deduced{1,2}.C).
-
- A list of pre and post contracts can either be repeated in their entirety or
- completely absent in subsequent declarations. If contract lists appear on two
- matching declarations, their contracts have to be equivalent. In general this
- means that anything before the colon have to be token equivalent and the
- condition must be cp_tree_equal (primarily to allow for parameter renaming).
-
- Contracts on overrides must match those present on (all of) the overridee(s).
-
- Template specializations may have their own contracts. If no contracts are
- specified on the initial specialization they're assumed to be the same as
- the primary template. Specialization redeclarations must then match either
- the primary template (if they were unspecified originally), or those
- specified on the specialization.
-
-
- For non-cdtors two functions are generated for ease of implementation and to
- avoid some cases where code bloat may occurr. These are the DECL_PRE_FN and
- DECL_POST_FN. Each handles checking either the set of pre or post contracts
- of a guarded function.
-
- int fun(int v)
- [[ pre: v > 0 ]]
- [[ post r: r < 0 ]]
- {
- return -v;
- }
-
- We implement the checking as follows:
-
- For functions with no post-conditions we wrap the original function body as
- follows:
-
- {
- handle_pre_condition_checking ();
- original_function_body ();
- }
-
- This implements the intent that the preconditions are processed after the
- function parameters are initialised but before any other actions.
-
- For functions with post-conditions:
-
- if (preconditions_exist)
- handle_pre_condition_checking ();
- try
- {
- original_function_body ();
- }
- finally
- {
- handle_post_condition_checking ();
- }
- else [only if the function is not marked noexcept(true) ]
- {
- ;
- }
-
- In this, post-conditions [that might apply to the return value etc.] are
- evaluated on every non-exceptional edge out of the function.
-
- FIXME outlining contract checks into separate functions was motivated
- partly by wanting to call the postcondition function at each return
- statement, which we no longer do; at this point outlining doesn't seem to
- have any advantage over emitting the contracts directly in the function
- body.
-
- More helpful for optimization might be to make the contracts a wrapper
- function that could be inlined into the caller, the callee, or both. */
-
-#include "config.h"
-#include "system.h"
-#include "coretypes.h"
-#include "cp-tree.h"
-#include "stringpool.h"
-#include "diagnostic.h"
-#include "options.h"
-#include "contracts.h"
-#include "tree.h"
-#include "tree-inline.h"
-#include "attribs.h"
-#include "tree-iterator.h"
-#include "print-tree.h"
-#include "stor-layout.h"
-#include "intl.h"
-#include "cgraph.h"
-
-const int max_custom_roles = 32;
-static contract_role contract_build_roles[max_custom_roles] = {
-};
-
-bool valid_configs[CCS_MAYBE + 1][CCS_MAYBE + 1] = {
- { 0, 0, 0, 0, 0, },
- { 0, 1, 0, 0, 0, },
- { 0, 1, 1, 1, 1, },
- { 0, 1, 1, 1, 1, },
- { 0, 1, 0, 0, 1, },
-};
-
-static void
-validate_contract_role (contract_role *role)
-{
- gcc_assert (role);
- if (!unchecked_contract_p (role->axiom_semantic))
- error ("axiom contract semantic must be %<assume%> or %<ignore%>");
-
- if (!valid_configs[role->default_semantic][role->audit_semantic] )
- warning (0, "the %<audit%> semantic should be at least as strong as "
- "the %<default%> semantic");
-}
-
-static contract_semantic
-lookup_concrete_semantic (const char *name)
-{
- if (strcmp (name, "ignore") == 0)
- return CCS_IGNORE;
- if (strcmp (name, "assume") == 0)
- return CCS_ASSUME;
- if (strcmp (name, "check_never_continue") == 0
- || strcmp (name, "never") == 0
- || strcmp (name, "abort") == 0)
- return CCS_NEVER;
- if (strcmp (name, "check_maybe_continue") == 0
- || strcmp (name, "maybe") == 0)
- return CCS_MAYBE;
- error ("'%s' is not a valid explicit concrete semantic", name);
- return CCS_INVALID;
-}
-
-/* Compare role and name up to either the NUL terminator or the first
- occurrence of colon. */
-
-static bool
-role_name_equal (const char *role, const char *name)
-{
- size_t role_len = strcspn (role, ":");
- size_t name_len = strcspn (name, ":");
- if (role_len != name_len)
- return false;
- return strncmp (role, name, role_len) == 0;
-}
-
-static bool
-role_name_equal (contract_role *role, const char *name)
-{
- if (role->name == NULL)
- return false;
- return role_name_equal (role->name, name);
-}
-
-static void setup_default_contract_role (bool update = true);
-
-static contract_role *
-get_contract_role (const char *name)
-{
- for (int i = 0; i < max_custom_roles; ++i)
- {
- contract_role *potential = contract_build_roles + i;
- if (role_name_equal (potential, name))
- return potential;
- }
- if (role_name_equal (name, "default") || role_name_equal (name, "review"))
- {
- setup_default_contract_role (false);
- return get_contract_role (name);
- }
- return NULL;
-}
-
-static contract_role *
-add_contract_role (const char *name,
- contract_semantic des,
- contract_semantic aus,
- contract_semantic axs,
- bool update = true)
-{
- for (int i = 0; i < max_custom_roles; ++i)
- {
- contract_role *potential = contract_build_roles + i;
- if (potential->name != NULL
- && !role_name_equal (potential, name))
- continue;
- if (potential->name != NULL && !update)
- return potential;
- potential->name = name;
- potential->default_semantic = des;
- potential->audit_semantic = aus;
- potential->axiom_semantic = axs;
- return potential;
- }
- return NULL;
-}
-
-enum contract_build_level { OFF, DEFAULT, AUDIT };
-static bool flag_contract_continuation_mode = false;
-static bool flag_contract_assumption_mode = true;
-static int flag_contract_build_level = DEFAULT;
-
-static bool contracts_p1332_default = false, contracts_p1332_review = false,
- contracts_std = false, contracts_p1429 = false;
-
-static contract_semantic
-get_concrete_check ()
-{
- return flag_contract_continuation_mode ? CCS_MAYBE : CCS_NEVER;
-}
-
-static contract_semantic
-get_concrete_axiom_semantic ()
-{
- return flag_contract_assumption_mode ? CCS_ASSUME : CCS_IGNORE;
-}
-
-static void
-setup_default_contract_role (bool update)
-{
- contract_semantic check = get_concrete_check ();
- contract_semantic axiom = get_concrete_axiom_semantic ();
- switch (flag_contract_build_level)
- {
- case OFF:
- add_contract_role ("default", CCS_IGNORE, CCS_IGNORE, axiom, update);
- add_contract_role ("review", CCS_IGNORE, CCS_IGNORE, CCS_IGNORE, update);
- break;
- case DEFAULT:
- add_contract_role ("default", check, CCS_IGNORE, axiom, update);
- add_contract_role ("review", check, CCS_IGNORE, CCS_IGNORE, update);
- break;
- case AUDIT:
- add_contract_role ("default", check, check, axiom, update);
- add_contract_role ("review", check, check, CCS_IGNORE, update);
- break;
- }
-}
-
-contract_semantic
-map_contract_semantic (const char *ident)
-{
- if (strcmp (ident, "ignore") == 0)
- return CCS_IGNORE;
- else if (strcmp (ident, "assume") == 0)
- return CCS_ASSUME;
- else if (strcmp (ident, "check_never_continue") == 0)
- return CCS_NEVER;
- else if (strcmp (ident, "check_maybe_continue") == 0)
- return CCS_MAYBE;
- return CCS_INVALID;
-}
-
-contract_level
-map_contract_level (const char *ident)
-{
- if (strcmp (ident, "default") == 0)
- return CONTRACT_DEFAULT;
- else if (strcmp (ident, "audit") == 0)
- return CONTRACT_AUDIT;
- else if (strcmp (ident, "axiom") == 0)
- return CONTRACT_AXIOM;
- return CONTRACT_INVALID;
-}
-
-
-void
-handle_OPT_fcontract_build_level_ (const char *arg)
-{
- if (contracts_p1332_default || contracts_p1332_review || contracts_p1429)
- {
- error ("%<-fcontract-build-level=%> cannot be mixed with p1332/p1429");
- return;
- }
- else
- contracts_std = true;
-
- if (strcmp (arg, "off") == 0)
- flag_contract_build_level = OFF;
- else if (strcmp (arg, "default") == 0)
- flag_contract_build_level = DEFAULT;
- else if (strcmp (arg, "audit") == 0)
- flag_contract_build_level = AUDIT;
- else
- error ("%<-fcontract-build-level=%> must be off|default|audit");
-
- setup_default_contract_role ();
-}
-
-void
-handle_OPT_fcontract_assumption_mode_ (const char *arg)
-{
- if (contracts_p1332_default || contracts_p1332_review || contracts_p1429)
- {
- error ("%<-fcontract-assumption-mode=%> cannot be mixed with p1332/p1429");
- return;
- }
- else
- contracts_std = true;
-
- if (strcmp (arg, "on") == 0)
- flag_contract_assumption_mode = true;
- else if (strcmp (arg, "off") == 0)
- flag_contract_assumption_mode = false;
- else
- error ("%<-fcontract-assumption-mode=%> must be %<on%> or %<off%>");
-
- setup_default_contract_role ();
-}
-
-void
-handle_OPT_fcontract_continuation_mode_ (const char *arg)
-{
- if (contracts_p1332_default || contracts_p1332_review || contracts_p1429)
- {
- error ("%<-fcontract-continuation-mode=%> cannot be mixed with p1332/p1429");
- return;
- }
- else
- contracts_std = true;
-
- if (strcmp (arg, "on") == 0)
- flag_contract_continuation_mode = true;
- else if (strcmp (arg, "off") == 0)
- flag_contract_continuation_mode = false;
- else
- error ("%<-fcontract-continuation-mode=%> must be %<on%> or %<off%>");
-
- setup_default_contract_role ();
-}
-
-void
-handle_OPT_fcontract_role_ (const char *arg)
-{
- const char *name = arg;
- const char *vals = strchr (name, ':');
- if (vals == NULL)
- {
- error ("%<-fcontract-role=%> must be in the form role:semantics");
- return;
- }
-
- contract_semantic dess = CCS_INVALID, auss = CCS_INVALID, axss = CCS_INVALID;
- char *des = NULL, *aus = NULL, *axs = NULL;
- des = xstrdup (vals + 1);
-
- aus = strchr (des, ',');
- if (aus == NULL)
- {
- error ("%<-fcontract-role=%> semantics must include default,audit,axiom values");
- goto validate;
- }
- *aus = '\0'; // null terminate des
- aus = aus + 1; // move past null
-
- axs = strchr (aus, ',');
- if (axs == NULL)
- {
- error ("%<-fcontract-role=%> semantics must include default,audit,axiom values");
- goto validate;
- }
- *axs = '\0'; // null terminate aus
- axs = axs + 1; // move past null
-
- dess = lookup_concrete_semantic (des);
- auss = lookup_concrete_semantic (aus);
- axss = lookup_concrete_semantic (axs);
-validate:
- free (des);
- if (dess == CCS_INVALID || auss == CCS_INVALID || axss == CCS_INVALID)
- return;
-
- bool is_defalult_role = role_name_equal (name, "default");
- bool is_review_role = role_name_equal (name, "review");
- bool is_std_role = is_defalult_role || is_review_role;
- if ((contracts_std && is_std_role) || (contracts_p1429 && is_defalult_role))
- {
- error ("%<-fcontract-role=%> cannot be mixed with std/p1429 contract flags");
- return;
- }
- else if (is_std_role)
- {
- contracts_p1332_default |= is_defalult_role;
- contracts_p1332_review |= is_review_role;
- }
-
- contract_role *role = add_contract_role (name, dess, auss, axss);
-
- if (role == NULL)
- {
- // TODO: not enough space?
- error ("%<-fcontract-level=%> too many custom roles");
- return;
- }
- else
- validate_contract_role (role);
-}
-
-void
-handle_OPT_fcontract_semantic_ (const char *arg)
-{
- if (!strchr (arg, ':'))
- {
- error ("%<-fcontract-semantic=%> must be in the form level:semantic");
- return;
- }
-
- if (contracts_std || contracts_p1332_default)
- {
- error ("%<-fcontract-semantic=%> cannot be mixed with std/p1332 contract flags");
- return;
- }
- contracts_p1429 = true;
-
- contract_role *role = get_contract_role ("default");
- if (!role)
- {
- error ("%<-fcontract-semantic=%> cannot find default role");
- return;
- }
-
- const char *semantic = strchr (arg, ':') + 1;
- contract_semantic sem = lookup_concrete_semantic (semantic);
- if (sem == CCS_INVALID)
- return;
-
- if (strncmp ("default:", arg, 8) == 0)
- role->default_semantic = sem;
- else if (strncmp ("audit:", arg, 6) == 0)
- role->audit_semantic = sem;
- else if (strncmp ("axiom:", arg, 6) == 0)
- role->axiom_semantic = sem;
- else
- error ("%<-fcontract-semantic=%> level must be default, audit, or axiom");
- validate_contract_role (role);
-}
-
-/* Returns the default role. */
-
-static contract_role *
-get_default_contract_role ()
-{
- return get_contract_role ("default");
-}
-
-/* Convert a contract CONFIG into a contract_mode. */
-
-static contract_mode
-contract_config_to_mode (tree config)
-{
- if (config == NULL_TREE)
- return contract_mode (CONTRACT_DEFAULT, get_default_contract_role ());
-
- /* TREE_LIST has TREE_VALUE is a level and TREE_PURPOSE is role. */
- if (TREE_CODE (config) == TREE_LIST)
- {
- contract_role *role = NULL;
- if (TREE_PURPOSE (config))
- role = get_contract_role (IDENTIFIER_POINTER (TREE_PURPOSE (config)));
- if (!role)
- role = get_default_contract_role ();
-
- contract_level level =
- map_contract_level (IDENTIFIER_POINTER (TREE_VALUE (config)));
- return contract_mode (level, role);
- }
-
- /* Literal semantic. */
- gcc_assert (TREE_CODE (config) == IDENTIFIER_NODE);
- contract_semantic semantic =
- map_contract_semantic (IDENTIFIER_POINTER (config));
- return contract_mode (semantic);
-}
-
-/* Convert a contract's config into a concrete semantic using the current
- contract semantic mapping. */
-
-static contract_semantic
-compute_concrete_semantic (tree contract)
-{
- contract_mode mode = contract_config_to_mode (CONTRACT_MODE (contract));
- /* Compute the concrete semantic for the contract. */
- if (!flag_contract_mode)
- /* If contracts are off, treat all contracts as ignore. */
- return CCS_IGNORE;
- else if (mode.kind == contract_mode::cm_invalid)
- return CCS_INVALID;
- else if (mode.kind == contract_mode::cm_explicit)
- return mode.get_semantic ();
- else
- {
- gcc_assert (mode.get_role ());
- gcc_assert (mode.get_level () != CONTRACT_INVALID);
- contract_level level = mode.get_level ();
- contract_role *role = mode.get_role ();
- if (level == CONTRACT_DEFAULT)
- return role->default_semantic;
- else if (level == CONTRACT_AUDIT)
- return role->audit_semantic;
- else if (level == CONTRACT_AXIOM)
- return role->axiom_semantic;
- }
- gcc_assert (false);
-}
-
-/* Return true if any contract in CONTRACT_ATTRs is not yet parsed. */
-
-bool
-contract_any_deferred_p (tree contract_attr)
-{
- for (; contract_attr; contract_attr = CONTRACT_CHAIN (contract_attr))
- if (CONTRACT_CONDITION_DEFERRED_P (CONTRACT_STATEMENT (contract_attr)))
- return true;
- return false;
-}
-
-/* Returns true if all attributes are contracts. */
-
-bool
-all_attributes_are_contracts_p (tree attributes)
-{
- for (; attributes; attributes = TREE_CHAIN (attributes))
- if (!cxx_contract_attribute_p (attributes))
- return false;
- return true;
-}
-
-/* Mark most of a contract as being invalid. */
-
-tree
-invalidate_contract (tree t)
-{
- if (TREE_CODE (t) == POSTCONDITION_STMT && POSTCONDITION_IDENTIFIER (t))
- POSTCONDITION_IDENTIFIER (t) = error_mark_node;
- CONTRACT_CONDITION (t) = error_mark_node;
- CONTRACT_COMMENT (t) = error_mark_node;
- return t;
-}
-
-/* Returns an invented parameter declration of the form 'TYPE ID' for the
- purpose of parsing the postcondition.
-
- We use a PARM_DECL instead of a VAR_DECL so that tsubst forces a lookup
- in local specializations when we instantiate these things later. */
-
-tree
-make_postcondition_variable (cp_expr id, tree type)
-{
- if (id == error_mark_node)
- return id;
-
- tree decl = build_lang_decl (PARM_DECL, id, type);
- DECL_ARTIFICIAL (decl) = true;
- DECL_SOURCE_LOCATION (decl) = id.get_location ();
-
- pushdecl (decl);
- return decl;
-}
-
-/* As above, except that the type is unknown. */
-
-tree
-make_postcondition_variable (cp_expr id)
-{
- return make_postcondition_variable (id, make_auto ());
-}
-
-/* Check that the TYPE is valid for a named postcondition variable. Emit a
- diagnostic if it is not. Returns TRUE if the result is OK and false
- otherwise. */
-
-bool
-check_postcondition_result (tree decl, tree type, location_t loc)
-{
- /* Do not be confused by targetm.cxx.cdtor_return_this ();
- conceptually, cdtors have no return value. */
- if (VOID_TYPE_P (type)
- || DECL_CONSTRUCTOR_P (decl)
- || DECL_DESTRUCTOR_P (decl))
- {
- error_at (loc,
- DECL_CONSTRUCTOR_P (decl)
- ? G_("constructor does not return a value to test")
- : DECL_DESTRUCTOR_P (decl)
- ? G_("destructor does not return a value to test")
- : G_("function does not return a value to test"));
- return false;
- }
-
- return true;
-}
-
-/* Instantiate each postcondition with the return type to finalize the
- attribute. */
-
-void
-rebuild_postconditions (tree decl)
-{
- tree type = TREE_TYPE (TREE_TYPE (decl));
- tree attributes = DECL_CONTRACTS (decl);
-
- for (; attributes ; attributes = TREE_CHAIN (attributes))
- {
- if (!cxx_contract_attribute_p (attributes))
- continue;
- tree contract = TREE_VALUE (TREE_VALUE (attributes));
- if (TREE_CODE (contract) != POSTCONDITION_STMT)
- continue;
- tree condition = CONTRACT_CONDITION (contract);
-
- /* If any conditions are deferred, they're all deferred. Note that
- we don't have to instantiate postconditions in that case because
- the type is available through the declaration. */
- if (TREE_CODE (condition) == DEFERRED_PARSE)
- return;
-
- tree oldvar = POSTCONDITION_IDENTIFIER (contract);
- if (!oldvar)
- continue;
-
- /* Always update the context of the result variable so that it can
- be remapped by remap_contracts. */
- DECL_CONTEXT (oldvar) = decl;
-
- /* If the return type is undeduced, defer until later. */
- if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
- return;
-
- /* Check the postcondition variable. */
- location_t loc = DECL_SOURCE_LOCATION (oldvar);
- if (!check_postcondition_result (decl, type, loc))
- {
- invalidate_contract (contract);
- continue;
- }
-
- /* "Instantiate" the result variable using the known type. Also update
- the context so the inliner will actually remap this the parameter when
- generating contract checks. */
- tree newvar = copy_node (oldvar);
- TREE_TYPE (newvar) = type;
-
- /* Make parameters and result available for substitution. */
- local_specialization_stack stack (lss_copy);
- for (tree t = DECL_ARGUMENTS (decl); t != NULL_TREE; t = TREE_CHAIN (t))
- register_local_identity (t);
- register_local_specialization (newvar, oldvar);
-
- ++processing_contract_condition;
- condition = tsubst_expr (condition, make_tree_vec (0),
- tf_warning_or_error, decl);
- --processing_contract_condition;
-
- /* Update the contract condition and result. */
- POSTCONDITION_IDENTIFIER (contract) = newvar;
- CONTRACT_CONDITION (contract) = finish_contract_condition (condition);
- }
-}
-
-static tree
-build_comment (cp_expr condition)
-{
- /* Try to get the actual source text for the condition; if that fails pretty
- print the resulting tree. */
- char *str = get_source_text_between (global_dc->get_file_cache (),
- condition.get_start (),
- condition.get_finish ());
- if (!str)
- {
- /* FIXME cases where we end up here
- #line macro usage (oof)
- contracts10.C
- contracts11.C */
- const char *str = expr_to_string (condition);
- return build_string_literal (strlen (str) + 1, str);
- }
-
- tree t = build_string_literal (strlen (str) + 1, str);
- free (str);
- return t;
-}
-
-/* Build a contract statement. */
-
-tree
-grok_contract (tree attribute, tree mode, tree result, cp_expr condition,
- location_t loc)
-{
- if (condition == error_mark_node)
- return error_mark_node;
-
- tree_code code;
- if (is_attribute_p ("assert", attribute))
- code = ASSERTION_STMT;
- else if (is_attribute_p ("pre", attribute))
- code = PRECONDITION_STMT;
- else if (is_attribute_p ("post", attribute))
- code = POSTCONDITION_STMT;
- else
- gcc_unreachable ();
-
- /* Build the contract. The condition is added later. In the case that
- the contract is deferred, result an plain identifier, not a result
- variable. */
- tree contract;
- tree type = void_type_node;
- if (code != POSTCONDITION_STMT)
- contract = build3_loc (loc, code, type, mode, NULL_TREE, NULL_TREE);
- else
- contract = build4_loc (loc, code, type, mode, NULL_TREE, NULL_TREE, result);
-
- /* Determine the concrete semantic. */
- set_contract_semantic (contract, compute_concrete_semantic (contract));
-
- /* If the contract is deferred, don't do anything with the condition. */
- if (TREE_CODE (condition) == DEFERRED_PARSE)
- {
- CONTRACT_CONDITION (contract) = condition;
- return contract;
- }
-
- /* Generate the comment from the original condition. */
- CONTRACT_COMMENT (contract) = build_comment (condition);
-
- /* The condition is converted to bool. */
- condition = finish_contract_condition (condition);
-
- if (condition == error_mark_node)
- return error_mark_node;
-
- CONTRACT_CONDITION (contract) = condition;
-
- return contract;
-}
-
-/* Build the contract attribute specifier where IDENTIFIER is one of 'pre',
- 'post' or 'assert' and CONTRACT is the underlying statement. */
-tree
-finish_contract_attribute (tree identifier, tree contract)
-{
- if (contract == error_mark_node)
- return error_mark_node;
-
- tree attribute = build_tree_list (build_tree_list (NULL_TREE, identifier),
- build_tree_list (NULL_TREE, contract));
-
- /* Mark the attribute as dependent if the condition is dependent.
-
- TODO: I'm not sure this is strictly necessary. It's going to be marked as
- such by a subroutine of cplus_decl_attributes. */
- tree condition = CONTRACT_CONDITION (contract);
- if (TREE_CODE (condition) == DEFERRED_PARSE
- || value_dependent_expression_p (condition))
- ATTR_IS_DEPENDENT (attribute) = true;
-
- return attribute;
-}
-
-/* Update condition of a late-parsed contract and postcondition variable,
- if any. */
-
-void
-update_late_contract (tree contract, tree result, tree condition)
-{
- if (TREE_CODE (contract) == POSTCONDITION_STMT)
- POSTCONDITION_IDENTIFIER (contract) = result;
-
- /* Generate the comment from the original condition. */
- CONTRACT_COMMENT (contract) = build_comment (condition);
-
- /* The condition is converted to bool. */
- condition = finish_contract_condition (condition);
- CONTRACT_CONDITION (contract) = condition;
-}
-
-/* Return TRUE iff ATTR has been parsed by the front-end as a c++2a contract
- attribute. */
-
-bool
-cxx_contract_attribute_p (const_tree attr)
-{
- if (attr == NULL_TREE
- || TREE_CODE (attr) != TREE_LIST)
- return false;
-
- if (!TREE_PURPOSE (attr) || TREE_CODE (TREE_PURPOSE (attr)) != TREE_LIST)
- return false;
- if (!TREE_VALUE (attr) || TREE_CODE (TREE_VALUE (attr)) != TREE_LIST)
- return false;
- if (!TREE_VALUE (TREE_VALUE (attr)))
- return false;
-
- return (TREE_CODE (TREE_VALUE (TREE_VALUE (attr))) == PRECONDITION_STMT
- || TREE_CODE (TREE_VALUE (TREE_VALUE (attr))) == POSTCONDITION_STMT
- || TREE_CODE (TREE_VALUE (TREE_VALUE (attr))) == ASSERTION_STMT);
-}
-
-/* True if ATTR is an assertion. */
-
-bool
-cp_contract_assertion_p (const_tree attr)
-{
- /* This is only an assertion if it is a valid cxx contract attribute and the
- statement is an ASSERTION_STMT. */
- return cxx_contract_attribute_p (attr)
- && TREE_CODE (CONTRACT_STATEMENT (attr)) == ASSERTION_STMT;
-}
-
-/* Remove all c++2a style contract attributes from the DECL_ATTRIBUTEs of the
- FUNCTION_DECL FNDECL. */
-
-void
-remove_contract_attributes (tree fndecl)
-{
- if (!flag_contracts)
- return;
-
- tree list = NULL_TREE;
- for (tree p = DECL_ATTRIBUTES (fndecl); p; p = TREE_CHAIN (p))
- if (!cxx_contract_attribute_p (p))
- {
- tree nl = copy_node (p);
- TREE_CHAIN (nl) = list;
- list = nl;
- }
- DECL_ATTRIBUTES (fndecl) = nreverse (list);
-}
-
-static tree find_first_non_contract (tree attributes)
-{
- tree head = attributes;
- tree p = find_contract (attributes);
-
- /* There are no contracts. */
- if (!p)
- return head;
-
- /* There are leading contracts. */
- if (p == head)
- {
- while (cxx_contract_attribute_p (p))
- p = TREE_CHAIN (p);
- head = p;
- }
-
- return head;
-}
-
-/* Remove contracts from ATTRIBUTES. */
-
-tree splice_out_contracts (tree attributes)
-{
- tree head = find_first_non_contract (attributes);
- if (!head)
- return NULL_TREE;
-
- /* Splice out remaining contracts. */
- tree p = TREE_CHAIN (head);
- tree q = head;
- while (p)
- {
- if (cxx_contract_attribute_p (p))
- {
- /* Skip a sequence of contracts and then link q to the next
- non-contract attribute. */
- do
- p = TREE_CHAIN (p);
- while (cxx_contract_attribute_p (p));
- TREE_CHAIN (q) = p;
- }
- else
- p = TREE_CHAIN (p);
- }
-
- return head;
-}
-
-/* Copy contract attributes from NEWDECL onto the attribute list of OLDDECL. */
-
-void copy_contract_attributes (tree olddecl, tree newdecl)
-{
- tree attrs = NULL_TREE;
- for (tree c = DECL_CONTRACTS (newdecl); c; c = TREE_CHAIN (c))
- {
- if (!cxx_contract_attribute_p (c))
- continue;
- attrs = tree_cons (TREE_PURPOSE (c), TREE_VALUE (c), attrs);
- }
- attrs = chainon (DECL_ATTRIBUTES (olddecl), nreverse (attrs));
- DECL_ATTRIBUTES (olddecl) = attrs;
-
- /* And update DECL_CONTEXT of the postcondition result identifier. */
- rebuild_postconditions (olddecl);
-}
-
-/* Returns the parameter corresponding to the return value of a guarded
- function D. Returns NULL_TREE if D has no postconditions or is void. */
-
-static tree
-get_postcondition_result_parameter (tree d)
-{
- if (!d || d == error_mark_node)
- return NULL_TREE;
-
- if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (d))))
- return NULL_TREE;
-
- tree post = DECL_POST_FN (d);
- if (!post || post == error_mark_node)
- return NULL_TREE;
-
- for (tree arg = DECL_ARGUMENTS (post); arg; arg = TREE_CHAIN (arg))
- if (!TREE_CHAIN (arg))
- return arg;
-
- return NULL_TREE;
-}
-
-
-/* For use with the tree inliner. This preserves non-mapped local variables,
- such as postcondition result variables, during remapping. */
-
-static tree
-retain_decl (tree decl, copy_body_data *)
-{
- return decl;
-}
-
-/* Rewrite the condition of contract in place, so that references to SRC's
- parameters are updated to refer to DST's parameters. The postcondition
- result variable is left unchanged.
-
- This, along with remap_contracts, are subroutines of duplicate_decls.
- When declarations are merged, we sometimes need to update contracts to
- refer to new parameters.
-
- If DUPLICATE_P is true, this is called by duplicate_decls to rewrite contacts
- in terms of a new set of parameters. In this case, we can retain local
- variables appearing in the contract because the contract is not being
- prepared for insertion into a new function. Importantly, this preserves the
- references to postcondition results, which are not replaced during merging.
-
- If false, we're preparing to emit the contract condition into the body
- of a new function, so we need to make copies of all local variables
- appearing in the contract (e.g., if it includes a lambda expression). Note
- that in this case, postcondition results are mapped to the last parameter
- of DST.
-
- This is also used to reuse a parent type's contracts on virtual methods. */
-
-static void
-remap_contract (tree src, tree dst, tree contract, bool duplicate_p)
-{
- copy_body_data id;
- hash_map<tree, tree> decl_map;
-
- memset (&id, 0, sizeof (id));
- id.src_fn = src;
- id.dst_fn = dst;
- id.src_cfun = DECL_STRUCT_FUNCTION (src);
- id.decl_map = &decl_map;
-
- /* If we're merging contracts, don't copy local variables. */
- id.copy_decl = duplicate_p ? retain_decl : copy_decl_no_change;
-
- id.transform_call_graph_edges = CB_CGE_DUPLICATE;
- id.transform_new_cfg = false;
- id.transform_return_to_modify = false;
- id.transform_parameter = true;
-
- /* Make sure not to unshare trees behind the front-end's back
- since front-end specific mechanisms may rely on sharing. */
- id.regimplify = false;
- id.do_not_unshare = true;
- id.do_not_fold = true;
-
- /* We're not inside any EH region. */
- id.eh_lp_nr = 0;
-
- bool do_remap = false;
-
- /* Insert parameter remappings. */
- if (TREE_CODE (src) == FUNCTION_DECL)
- src = DECL_ARGUMENTS (src);
- if (TREE_CODE (dst) == FUNCTION_DECL)
- dst = DECL_ARGUMENTS (dst);
-
- for (tree sp = src, dp = dst;
- sp || dp;
- sp = DECL_CHAIN (sp), dp = DECL_CHAIN (dp))
- {
- if (!sp && dp
- && TREE_CODE (contract) == POSTCONDITION_STMT
- && DECL_CHAIN (dp) == NULL_TREE)
- {
- gcc_assert (!duplicate_p);
- if (tree result = POSTCONDITION_IDENTIFIER (contract))
- {
- gcc_assert (DECL_P (result));
- insert_decl_map (&id, result, dp);
- do_remap = true;
- }
- break;
- }
- gcc_assert (sp && dp);
-
- if (sp == dp)
- continue;
-
- insert_decl_map (&id, sp, dp);
- do_remap = true;
- }
- if (!do_remap)
- return;
-
- walk_tree (&CONTRACT_CONDITION (contract), copy_tree_body_r, &id, NULL);
-}
-
-/* Rewrite any references to SRC's PARM_DECLs to the corresponding PARM_DECL in
- DST in all of the contract attributes in CONTRACTS by calling remap_contract
- on each.
-
- This is used for two purposes: to rewrite contract attributes during
- duplicate_decls, and to prepare contracts for emission into a function's
- respective precondition and postcondition functions. DUPLICATE_P is used
- to determine the context in which this function is called. See above for
- the behavior described by this flag. */
-
-void
-remap_contracts (tree src, tree dst, tree contracts, bool duplicate_p)
-{
- for (tree attr = contracts; attr; attr = CONTRACT_CHAIN (attr))
- {
- if (!cxx_contract_attribute_p (attr))
- continue;
- tree contract = CONTRACT_STATEMENT (attr);
- if (TREE_CODE (CONTRACT_CONDITION (contract)) != DEFERRED_PARSE)
- remap_contract (src, dst, contract, duplicate_p);
- }
-}
-
-/* Helper to replace references to dummy this parameters with references to
- the first argument of the FUNCTION_DECL DATA. */
-
-static tree
-remap_dummy_this_1 (tree *tp, int *, void *data)
-{
- if (!is_this_parameter (*tp))
- return NULL_TREE;
- tree fn = (tree)data;
- *tp = DECL_ARGUMENTS (fn);
- return NULL_TREE;
-}
-
-/* Replace all references to dummy this parameters in EXPR with references to
- the first argument of the FUNCTION_DECL FN. */
-
-static void
-remap_dummy_this (tree fn, tree *expr)
-{
- walk_tree (expr, remap_dummy_this_1, fn, NULL);
-}
-
-/* Contract matching. */
-
-/* True if the contract is valid. */
-
-static bool
-contract_valid_p (tree contract)
-{
- return CONTRACT_CONDITION (contract) != error_mark_node;
-}
-
-/* True if the contract attribute is valid. */
-
-static bool
-contract_attribute_valid_p (tree attribute)
-{
- return contract_valid_p (TREE_VALUE (TREE_VALUE (attribute)));
-}
-
-/* Compare the contract conditions of OLD_ATTR and NEW_ATTR. Returns false
- if the conditions are equivalent, and true otherwise. */
-
-static bool
-check_for_mismatched_contracts (tree old_attr, tree new_attr,
- contract_matching_context ctx)
-{
- tree old_contract = CONTRACT_STATEMENT (old_attr);
- tree new_contract = CONTRACT_STATEMENT (new_attr);
-
- /* Different kinds of contracts do not match. */
- if (TREE_CODE (old_contract) != TREE_CODE (new_contract))
- {
- auto_diagnostic_group d;
- error_at (EXPR_LOCATION (new_contract),
- ctx == cmc_declaration
- ? "mismatched contract attribute in declaration"
- : "mismatched contract attribute in override");
- inform (EXPR_LOCATION (old_contract), "previous contract here");
- return true;
- }
-
- /* A deferred contract tentatively matches. */
- if (CONTRACT_CONDITION_DEFERRED_P (new_contract))
- return false;
-
- /* Compare the conditions of the contracts. We fold immediately to avoid
- issues comparing contracts on overrides that use parameters -- see
- contracts-pre3. */
- tree t1 = cp_fully_fold_init (CONTRACT_CONDITION (old_contract));
- tree t2 = cp_fully_fold_init (CONTRACT_CONDITION (new_contract));
-
- /* Compare the contracts. The fold doesn't eliminate conversions to members.
- Set the comparing_override_contracts flag to ensure that references
- through 'this' are equal if they designate the same member, regardless of
- the path those members. */
- bool saved_comparing_contracts = comparing_override_contracts;
- comparing_override_contracts = (ctx == cmc_override);
- bool matching_p = cp_tree_equal (t1, t2);
- comparing_override_contracts = saved_comparing_contracts;
-
- if (!matching_p)
- {
- auto_diagnostic_group d;
- error_at (EXPR_LOCATION (CONTRACT_CONDITION (new_contract)),
- ctx == cmc_declaration
- ? "mismatched contract condition in declaration"
- : "mismatched contract condition in override");
- inform (EXPR_LOCATION (CONTRACT_CONDITION (old_contract)),
- "previous contract here");
- return true;
- }
-
- return false;
-}
-
-/* Compare the contract attributes of OLDDECL and NEWDECL. Returns true
- if the contracts match, and false if they differ. */
-
-bool
-match_contract_conditions (location_t oldloc, tree old_attrs,
- location_t newloc, tree new_attrs,
- contract_matching_context ctx)
-{
- /* Contracts only match if they are both specified. */
- if (!old_attrs || !new_attrs)
- return true;
-
- /* Compare each contract in turn. */
- while (old_attrs && new_attrs)
- {
- /* If either contract is ill-formed, skip the rest of the comparison,
- since we've already diagnosed an error. */
- if (!contract_attribute_valid_p (new_attrs)
- || !contract_attribute_valid_p (old_attrs))
- return false;
-
- if (check_for_mismatched_contracts (old_attrs, new_attrs, ctx))
- return false;
- old_attrs = CONTRACT_CHAIN (old_attrs);
- new_attrs = CONTRACT_CHAIN (new_attrs);
- }
-
- /* If we didn't compare all attributes, the contracts don't match. */
- if (old_attrs || new_attrs)
- {
- auto_diagnostic_group d;
- error_at (newloc,
- ctx == cmc_declaration
- ? "declaration has a different number of contracts than "
- "previously declared"
- : "override has a different number of contracts than "
- "previously declared");
- inform (oldloc,
- new_attrs
- ? "original declaration with fewer contracts here"
- : "original declaration with more contracts here");
- return false;
- }
-
- return true;
-}
-
-/* Deferred contract mapping.
-
- This is used to compare late-parsed contracts on overrides with their
- base class functions.
-
- TODO: It seems like this could be replaced by a simple list that maps from
- overrides to their base functions. It's not clear that we really need
- a map to a function + a list of contracts. */
-
-/* Map from FNDECL to a tree list of contracts that have not been matched or
- diagnosed yet. The TREE_PURPOSE is the basefn we're overriding, and the
- TREE_VALUE is the list of contract attrs for BASEFN. */
-
-static hash_map<tree_decl_hash, tree> pending_guarded_decls;
-
-void
-defer_guarded_contract_match (tree fndecl, tree fn, tree contracts)
-{
- if (!pending_guarded_decls.get (fndecl))
- {
- pending_guarded_decls.put (fndecl, build_tree_list (fn, contracts));
- return;
- }
- for (tree pending = *pending_guarded_decls.get (fndecl);
- pending;
- pending = TREE_CHAIN (pending))
- {
- if (TREE_VALUE (pending) == contracts)
- return;
- if (TREE_CHAIN (pending) == NULL_TREE)
- TREE_CHAIN (pending) = build_tree_list (fn, contracts);
- }
-}
-
-/* If the FUNCTION_DECL DECL has any contracts that had their matching
- deferred earlier, do that checking now. */
-
-void
-match_deferred_contracts (tree decl)
-{
- tree *tp = pending_guarded_decls.get (decl);
- if (!tp)
- return;
-
- gcc_assert(!contract_any_deferred_p (DECL_CONTRACTS (decl)));
-
- processing_template_decl_sentinel ptds;
- processing_template_decl = uses_template_parms (decl);
-
- /* Do late contract matching. */
- for (tree pending = *tp; pending; pending = TREE_CHAIN (pending))
- {
- tree new_contracts = TREE_VALUE (pending);
- location_t new_loc = CONTRACT_SOURCE_LOCATION (new_contracts);
- tree old_contracts = DECL_CONTRACTS (decl);
- location_t old_loc = CONTRACT_SOURCE_LOCATION (old_contracts);
- tree base = TREE_PURPOSE (pending);
- match_contract_conditions (new_loc, new_contracts,
- old_loc, old_contracts,
- base ? cmc_override : cmc_declaration);
- }
-
- /* Clear out deferred match list so we don't check it twice. */
- pending_guarded_decls.remove (decl);
-}
-
-/* Map from FUNCTION_DECL to a FUNCTION_DECL for either the PRE_FN or POST_FN.
- These are used to parse contract conditions and are called inside the body
- of the guarded function. */
-static GTY(()) hash_map<tree, tree> *decl_pre_fn;
-static GTY(()) hash_map<tree, tree> *decl_post_fn;
-
-/* Returns the precondition funtion for D, or null if not set. */
-
-tree
-get_precondition_function (tree d)
-{
- hash_map_maybe_create<hm_ggc> (decl_pre_fn);
- tree *result = decl_pre_fn->get (d);
- return result ? *result : NULL_TREE;
-}
-
-/* Returns the postcondition funtion for D, or null if not set. */
-
-tree
-get_postcondition_function (tree d)
-{
- hash_map_maybe_create<hm_ggc> (decl_post_fn);
- tree *result = decl_post_fn->get (d);
- return result ? *result : NULL_TREE;
-}
-
-/* Makes PRE the precondition function for D. */
-
-void
-set_precondition_function (tree d, tree pre)
-{
- gcc_assert (pre);
- hash_map_maybe_create<hm_ggc> (decl_pre_fn);
- gcc_assert (!decl_pre_fn->get (d));
- decl_pre_fn->put (d, pre);
-}
-
-/* Makes POST the postcondition function for D. */
-
-void
-set_postcondition_function (tree d, tree post)
-{
- gcc_assert (post);
- hash_map_maybe_create<hm_ggc> (decl_post_fn);
- gcc_assert (!decl_post_fn->get (d));
- decl_post_fn->put (d, post);
-}
-
-/* Set the PRE and POST functions for D. Note that PRE and POST can be
- null in this case. If so the functions are not recorded. */
-
-void
-set_contract_functions (tree d, tree pre, tree post)
-{
- if (pre)
- set_precondition_function (d, pre);
- if (post)
- set_postcondition_function (d, post);
-}
-
-/* Return a copy of the FUNCTION_DECL IDECL with its own unshared
- PARM_DECL and DECL_ATTRIBUTEs. */
-
-static tree
-copy_fn_decl (tree idecl)
-{
- tree decl = copy_decl (idecl);
- DECL_ATTRIBUTES (decl) = copy_list (DECL_ATTRIBUTES (idecl));
-
- if (DECL_RESULT (idecl))
- {
- DECL_RESULT (decl) = copy_decl (DECL_RESULT (idecl));
- DECL_CONTEXT (DECL_RESULT (decl)) = decl;
- }
- if (!DECL_ARGUMENTS (idecl) || VOID_TYPE_P (DECL_ARGUMENTS (idecl)))
- return decl;
-
- tree last = DECL_ARGUMENTS (decl) = copy_decl (DECL_ARGUMENTS (decl));
- DECL_CONTEXT (last) = decl;
- for (tree p = TREE_CHAIN (DECL_ARGUMENTS (idecl)); p; p = TREE_CHAIN (p))
- {
- if (VOID_TYPE_P (p))
- {
- TREE_CHAIN (last) = void_list_node;
- break;
- }
- last = TREE_CHAIN (last) = copy_decl (p);
- DECL_CONTEXT (last) = decl;
- }
- return decl;
-}
-
-/* Build a declaration for the pre- or postcondition of a guarded FNDECL. */
-
-static tree
-build_contract_condition_function (tree fndecl, bool pre)
-{
- if (TREE_TYPE (fndecl) == error_mark_node)
- return error_mark_node;
- if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
- && !TYPE_METHOD_BASETYPE (TREE_TYPE (fndecl)))
- return error_mark_node;
-
- /* Create and rename the unchecked function and give an internal name. */
- tree fn = copy_fn_decl (fndecl);
- DECL_RESULT (fn) = NULL_TREE;
- tree value_type = pre ? void_type_node : TREE_TYPE (TREE_TYPE (fn));
-
- /* Don't propagate declaration attributes to the checking function,
- including the original contracts. */
- DECL_ATTRIBUTES (fn) = NULL_TREE;
-
- tree arg_types = NULL_TREE;
- tree *last = &arg_types;
-
- /* FIXME will later optimizations delete unused args to prevent extra arg
- passing? do we care? */
- tree class_type = NULL_TREE;
- for (tree arg_type = TYPE_ARG_TYPES (TREE_TYPE (fn));
- arg_type && arg_type != void_list_node;
- arg_type = TREE_CHAIN (arg_type))
- {
- if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
- && TYPE_ARG_TYPES (TREE_TYPE (fn)) == arg_type)
- {
- class_type = TREE_TYPE (TREE_VALUE (arg_type));
- continue;
- }
- *last = build_tree_list (TREE_PURPOSE (arg_type), TREE_VALUE (arg_type));
- last = &TREE_CHAIN (*last);
- }
-
- if (pre || VOID_TYPE_P (value_type))
- *last = void_list_node;
- else
- {
- tree name = get_identifier ("__r");
- tree parm = build_lang_decl (PARM_DECL, name, value_type);
- DECL_CONTEXT (parm) = fn;
- DECL_ARTIFICIAL (parm) = true;
- DECL_ARGUMENTS (fn) = chainon (DECL_ARGUMENTS (fn), parm);
-
- *last = build_tree_list (NULL_TREE, value_type);
- TREE_CHAIN (*last) = void_list_node;
-
- /* The handler is a void return. */
- value_type = void_type_node;
- }
-
- TREE_TYPE (fn) = build_function_type (value_type, arg_types);
- if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl))
- TREE_TYPE (fn) = build_method_type (class_type, TREE_TYPE (fn));
-
- DECL_NAME (fn) = copy_node (DECL_NAME (fn));
- DECL_INITIAL (fn) = error_mark_node;
- DECL_ABSTRACT_ORIGIN (fn) = fndecl;
-
- IDENTIFIER_VIRTUAL_P (DECL_NAME (fn)) = false;
- DECL_VIRTUAL_P (fn) = false;
-
- /* Make these functions internal if we can, i.e. if the guarded function is
- not vague linkage, or if we can put them in a comdat group with the
- guarded function. */
- if (!DECL_WEAK (fndecl) || HAVE_COMDAT_GROUP)
- {
- TREE_PUBLIC (fn) = false;
- DECL_EXTERNAL (fn) = false;
- DECL_WEAK (fn) = false;
- DECL_COMDAT (fn) = false;
-
- /* We may not have set the comdat group on the guarded function yet.
- If we haven't, we'll add this to the same group in comdat_linkage
- later. Otherwise, add it to the same comdat group now. */
- if (DECL_ONE_ONLY (fndecl))
- {
- symtab_node *n = symtab_node::get (fndecl);
- cgraph_node::get_create (fn)->add_to_same_comdat_group (n);
- }
-
- DECL_INTERFACE_KNOWN (fn) = true;
- }
-
- DECL_ARTIFICIAL (fn) = true;
-
- /* Update various inline related declaration properties. */
- //DECL_DECLARED_INLINE_P (fn) = true;
- DECL_DISREGARD_INLINE_LIMITS (fn) = true;
- TREE_NO_WARNING (fn) = 1;
-
- return fn;
-}
-
-/* Return true if CONTRACT is checked or assumed under the current build
- configuration. */
-
-bool
-contract_active_p (tree contract)
-{
- return get_contract_semantic (contract) != CCS_IGNORE;
-}
-
-static bool
-has_active_contract_condition (tree d, tree_code c)
-{
- for (tree as = DECL_CONTRACTS (d) ; as != NULL_TREE; as = CONTRACT_CHAIN (as))
- {
- tree contract = TREE_VALUE (TREE_VALUE (as));
- if (TREE_CODE (contract) == c && contract_active_p (contract))
- return true;
- }
- return false;
-}
-
-/* True if D has any checked or assumed preconditions. */
-
-static bool
-has_active_preconditions (tree d)
-{
- return has_active_contract_condition (d, PRECONDITION_STMT);
-}
-
-/* True if D has any checked or assumed postconditions. */
-
-static bool
-has_active_postconditions (tree d)
-{
- return has_active_contract_condition (d, POSTCONDITION_STMT);
-}
-
-/* Return true if any contract in the CONTRACT list is checked or assumed
- under the current build configuration. */
-
-bool
-contract_any_active_p (tree contract)
-{
- for (; contract != NULL_TREE; contract = CONTRACT_CHAIN (contract))
- if (contract_active_p (TREE_VALUE (TREE_VALUE (contract))))
- return true;
- return false;
-}
-
-/* Do we need to mess with contracts for DECL1? */
-
-static bool
-handle_contracts_p (tree decl1)
-{
- return (flag_contracts
- && !processing_template_decl
- && DECL_ABSTRACT_ORIGIN (decl1) == NULL_TREE
- && contract_any_active_p (DECL_CONTRACTS (decl1)));
-}
-
-/* Should we break out DECL1's pre/post contracts into separate functions?
- FIXME I'd like this to default to 0, but that will need an overhaul to the
- return identifier handling to just refer to the RESULT_DECL. */
-
-static bool
-outline_contracts_p (tree decl1)
-{
- return (!DECL_CONSTRUCTOR_P (decl1)
- && !DECL_DESTRUCTOR_P (decl1));
-}
-
-/* Build the precondition checking function for D. */
-
-static tree
-build_precondition_function (tree d)
-{
- if (!has_active_preconditions (d))
- return NULL_TREE;
-
- return build_contract_condition_function (d, /*pre=*/true);
-}
-
-/* Build the postcondition checking function for D. If the return
- type is undeduced, don't build the function yet. We do that in
- apply_deduced_return_type. */
-
-static tree
-build_postcondition_function (tree d)
-{
- if (!has_active_postconditions (d))
- return NULL_TREE;
-
- tree type = TREE_TYPE (TREE_TYPE (d));
- if (is_auto (type))
- return NULL_TREE;
-
- return build_contract_condition_function (d, /*pre=*/false);
-}
-
-static void
-build_contract_function_decls (tree d)
-{
- /* Constructors and destructors have their contracts inserted inline. */
- if (!outline_contracts_p (d))
- return;
-
- /* Build the pre/post functions (or not). */
- tree pre = build_precondition_function (d);
- tree post = build_postcondition_function (d);
- set_contract_functions (d, pre, post);
-}
-
-static const char *
-get_contract_level_name (tree contract)
-{
- if (CONTRACT_LITERAL_MODE_P (contract))
- return "";
- if (tree mode = CONTRACT_MODE (contract))
- if (tree level = TREE_VALUE (mode))
- return IDENTIFIER_POINTER (level);
- return "default";
-}
-
-static const char *
-get_contract_role_name (tree contract)
-{
- if (CONTRACT_LITERAL_MODE_P (contract))
- return "";
- if (tree mode = CONTRACT_MODE (contract))
- if (tree role = TREE_PURPOSE (mode))
- return IDENTIFIER_POINTER (role);
- return "default";
-}
-
-/* Build a layout-compatible internal version of std::contract_violation. */
-
-static tree
-get_pseudo_contract_violation_type ()
-{
- if (!pseudo_contract_violation_type)
- {
- /* Must match <contract>:
- class contract_violation {
- const char* _M_file;
- const char* _M_function;
- const char* _M_comment;
- const char* _M_level;
- const char* _M_role;
- uint_least32_t _M_line;
- signed char _M_continue;
- If this changes, also update the initializer in
- build_contract_violation. */
- struct field_info { tree type; const char* name; };
- const field_info info[] = {
- { const_string_type_node, "_M_file" },
- { const_string_type_node, "_M_function" },
- { const_string_type_node, "_M_comment" },
- { const_string_type_node, "_M_level" },
- { const_string_type_node, "_M_role" },
- { uint_least32_type_node, "_M_line" },
- { signed_char_type_node, "_M_continue" }
- };
- tree fields = NULL_TREE;
- for (const field_info& i : info)
- {
- /* finish_builtin_struct wants fieldss chained in reverse. */
- tree next = build_decl (BUILTINS_LOCATION, FIELD_DECL,
- get_identifier (i.name), i.type);
- DECL_CHAIN (next) = fields;
- fields = next;
- }
- iloc_sentinel ils (input_location);
- input_location = BUILTINS_LOCATION;
- pseudo_contract_violation_type = make_class_type (RECORD_TYPE);
- finish_builtin_struct (pseudo_contract_violation_type,
- "__pseudo_contract_violation",
- fields, NULL_TREE);
- CLASSTYPE_AS_BASE (pseudo_contract_violation_type)
- = pseudo_contract_violation_type;
- DECL_CONTEXT (TYPE_NAME (pseudo_contract_violation_type))
- = FROB_CONTEXT (global_namespace);
- TREE_PUBLIC (TYPE_NAME (pseudo_contract_violation_type)) = true;
- CLASSTYPE_LITERAL_P (pseudo_contract_violation_type) = true;
- CLASSTYPE_LAZY_COPY_CTOR (pseudo_contract_violation_type) = true;
- xref_basetypes (pseudo_contract_violation_type, /*bases=*/NULL_TREE);
- pseudo_contract_violation_type
- = cp_build_qualified_type (pseudo_contract_violation_type,
- TYPE_QUAL_CONST);
- }
- return pseudo_contract_violation_type;
-}
-
-/* Return a VAR_DECL to pass to handle_contract_violation. */
-
-static tree
-build_contract_violation (tree contract, contract_continuation cmode)
-{
- expanded_location loc = expand_location (EXPR_LOCATION (contract));
- const char *function = fndecl_name (DECL_ORIGIN (current_function_decl));
- const char *level = get_contract_level_name (contract);
- const char *role = get_contract_role_name (contract);
-
- /* Must match the type layout in get_pseudo_contract_violation_type. */
- tree ctor = build_constructor_va
- (init_list_type_node, 7,
- NULL_TREE, build_string_literal (loc.file),
- NULL_TREE, build_string_literal (function),
- NULL_TREE, CONTRACT_COMMENT (contract),
- NULL_TREE, build_string_literal (level),
- NULL_TREE, build_string_literal (role),
- NULL_TREE, build_int_cst (uint_least32_type_node, loc.line),
- NULL_TREE, build_int_cst (signed_char_type_node, cmode));
-
- ctor = finish_compound_literal (get_pseudo_contract_violation_type (),
- ctor, tf_none);
- protected_set_expr_location (ctor, EXPR_LOCATION (contract));
- return ctor;
-}
-
-/* Return handle_contract_violation(), declaring it if needed. */
-
-static tree
-declare_handle_contract_violation ()
-{
- tree fnname = get_identifier ("handle_contract_violation");
- tree viol_name = get_identifier ("contract_violation");
- tree l = lookup_qualified_name (global_namespace, fnname,
- LOOK_want::HIDDEN_FRIEND);
- for (tree f: lkp_range (l))
- if (TREE_CODE (f) == FUNCTION_DECL)
- {
- tree parms = TYPE_ARG_TYPES (TREE_TYPE (f));
- if (remaining_arguments (parms) != 1)
- continue;
- tree parmtype = non_reference (TREE_VALUE (parms));
- if (CLASS_TYPE_P (parmtype)
- && TYPE_IDENTIFIER (parmtype) == viol_name)
- return f;
- }
-
- tree id_exp = get_identifier ("experimental");
- tree ns_exp = lookup_qualified_name (std_node, id_exp);
-
- tree violation = error_mark_node;
- if (TREE_CODE (ns_exp) == NAMESPACE_DECL)
- violation = lookup_qualified_name (ns_exp, viol_name,
- LOOK_want::TYPE
- |LOOK_want::HIDDEN_FRIEND);
-
- if (TREE_CODE (violation) == TYPE_DECL)
- violation = TREE_TYPE (violation);
- else
- {
- push_nested_namespace (std_node);
- push_namespace (id_exp, /*inline*/false);
- violation = make_class_type (RECORD_TYPE);
- create_implicit_typedef (viol_name, violation);
- DECL_SOURCE_LOCATION (TYPE_NAME (violation)) = BUILTINS_LOCATION;
- DECL_CONTEXT (TYPE_NAME (violation)) = current_namespace;
- TREE_PUBLIC (TYPE_NAME (violation)) = true;
- pushdecl_namespace_level (TYPE_NAME (violation), /*hidden*/true);
- pop_namespace ();
- pop_nested_namespace (std_node);
- }
-
- tree argtype = cp_build_qualified_type (violation, TYPE_QUAL_CONST);
- argtype = cp_build_reference_type (argtype, /*rval*/false);
- tree fntype = build_function_type_list (void_type_node, argtype, NULL_TREE);
-
- push_nested_namespace (global_namespace);
- tree fn = build_cp_library_fn_ptr ("handle_contract_violation", fntype,
- ECF_COLD);
- pushdecl_namespace_level (fn, /*hiding*/true);
- pop_nested_namespace (global_namespace);
-
- return fn;
-}
-
-/* Build the call to handle_contract_violation for CONTRACT. */
-
-static void
-build_contract_handler_call (tree contract,
- contract_continuation cmode)
-{
- /* We may need to declare new types, ensure they are not considered
- attached to a named module. */
- auto module_kind_override = make_temp_override
- (module_kind, module_kind & ~(MK_PURVIEW | MK_ATTACH | MK_EXPORTING));
-
- tree violation = build_contract_violation (contract, cmode);
- tree violation_fn = declare_handle_contract_violation ();
- tree call = build_call_n (violation_fn, 1, build_address (violation));
- finish_expr_stmt (call);
-}
-
-/* Generate the code that checks or assumes a contract, but do not attach
- it to the current context. This is called during genericization. */
-
-tree
-build_contract_check (tree contract)
-{
- contract_semantic semantic = get_contract_semantic (contract);
- if (semantic == CCS_INVALID)
- return NULL_TREE;
-
- /* Ignored contracts are never checked or assumed. */
- if (semantic == CCS_IGNORE)
- return void_node;
-
- remap_dummy_this (current_function_decl, &CONTRACT_CONDITION (contract));
- tree condition = CONTRACT_CONDITION (contract);
- if (condition == error_mark_node)
- return NULL_TREE;
-
- location_t loc = EXPR_LOCATION (contract);
-
- if (semantic == CCS_ASSUME)
- return build_assume_call (loc, condition);
-
- tree if_stmt = begin_if_stmt ();
- tree cond = build_x_unary_op (loc,
- TRUTH_NOT_EXPR,
- condition, NULL_TREE,
- tf_warning_or_error);
- finish_if_stmt_cond (cond, if_stmt);
-
- /* Get the continuation mode. */
- contract_continuation cmode;
- switch (semantic)
- {
- case CCS_NEVER: cmode = NEVER_CONTINUE; break;
- case CCS_MAYBE: cmode = MAYBE_CONTINUE; break;
- default: gcc_unreachable ();
- }
-
- build_contract_handler_call (contract, cmode);
- if (cmode == NEVER_CONTINUE)
- finish_expr_stmt (build_call_a (terminate_fn, 0, nullptr));
-
- finish_then_clause (if_stmt);
- tree scope = IF_SCOPE (if_stmt);
- IF_SCOPE (if_stmt) = NULL;
- return do_poplevel (scope);
-}
-
-/* Add the contract statement CONTRACT to the current block if valid. */
-
-static void
-emit_contract_statement (tree contract)
-{
- /* Only add valid contracts. */
- if (get_contract_semantic (contract) != CCS_INVALID
- && CONTRACT_CONDITION (contract) != error_mark_node)
- add_stmt (contract);
-}
-
-/* Generate the statement for the given contract attribute by adding the
- statement to the current block. Returns the next contract in the chain. */
-
-static tree
-emit_contract_attr (tree attr)
-{
- gcc_assert (TREE_CODE (attr) == TREE_LIST);
-
- emit_contract_statement (CONTRACT_STATEMENT (attr));
-
- return CONTRACT_CHAIN (attr);
-}
-
-/* Add the statements of contract attributes ATTRS to the current block. */
-
-static void
-emit_contract_conditions (tree attrs, tree_code code)
-{
- if (!attrs) return;
- gcc_assert (TREE_CODE (attrs) == TREE_LIST);
- gcc_assert (code == PRECONDITION_STMT || code == POSTCONDITION_STMT);
- while (attrs)
- {
- tree contract = CONTRACT_STATEMENT (attrs);
- if (TREE_CODE (contract) == code)
- attrs = emit_contract_attr (attrs);
- else
- attrs = CONTRACT_CHAIN (attrs);
- }
-}
-
-/* Emit the statement for an assertion attribute. */
-
-void
-emit_assertion (tree attr)
-{
- emit_contract_attr (attr);
-}
-
-/* Emit statements for precondition attributes. */
-
-static void
-emit_preconditions (tree attr)
-{
- return emit_contract_conditions (attr, PRECONDITION_STMT);
-}
-
-/* Emit statements for postcondition attributes. */
-
-static void
-emit_postconditions (tree attr)
-{
- return emit_contract_conditions (attr, POSTCONDITION_STMT);
-}
-
-/* We're compiling the pre/postcondition function CONDFN; remap any FN
- attributes that match CODE and emit them. */
-
-static void
-remap_and_emit_conditions (tree fn, tree condfn, tree_code code)
-{
- gcc_assert (code == PRECONDITION_STMT || code == POSTCONDITION_STMT);
- for (tree attr = DECL_CONTRACTS (fn); attr;
- attr = CONTRACT_CHAIN (attr))
- {
- tree contract = CONTRACT_STATEMENT (attr);
- if (TREE_CODE (contract) == code)
- {
- contract = copy_node (contract);
- remap_contract (fn, condfn, contract, /*duplicate_p=*/false);
- emit_contract_statement (contract);
- }
- }
-}
-
-/* Converts a contract condition to bool and ensures it has a locaiton. */
-
-tree
-finish_contract_condition (cp_expr condition)
-{
- /* Ensure we have the condition location saved in case we later need to
- emit a conversion error during template instantiation and wouldn't
- otherwise have it. */
- if (!CAN_HAVE_LOCATION_P (condition) || EXCEPTIONAL_CLASS_P (condition))
- {
- condition = build1_loc (condition.get_location (), VIEW_CONVERT_EXPR,
- TREE_TYPE (condition), condition);
- EXPR_LOCATION_WRAPPER_P (condition) = 1;
- }
-
- if (condition == error_mark_node || type_dependent_expression_p (condition))
- return condition;
-
- return condition_conversion (condition);
-}
-
-void
-maybe_update_postconditions (tree fco)
-{
- /* Update any postconditions and the postcondition checking function
- as needed. If there are postconditions, we'll use those to rewrite
- return statements to check postconditions. */
- if (has_active_postconditions (fco))
- {
- rebuild_postconditions (fco);
- tree post = build_postcondition_function (fco);
- set_postcondition_function (fco, post);
- }
-}
-
-/* Called on attribute lists that must not contain contracts. If any
- contracts are present, issue an error diagnostic and return true. */
-
-bool
-diagnose_misapplied_contracts (tree attributes)
-{
- if (attributes == NULL_TREE)
- return false;
-
- tree contract_attr = find_contract (attributes);
- if (!contract_attr)
- return false;
-
- error_at (EXPR_LOCATION (CONTRACT_STATEMENT (contract_attr)),
- "contracts must appertain to a function type");
-
- /* Invalidate the contract so we don't treat it as valid later on. */
- invalidate_contract (TREE_VALUE (TREE_VALUE (contract_attr)));
-
- return true;
-}
-
-/* Build and return an argument list containing all the parameters of the
- (presumably guarded) FUNCTION_DECL FN. This can be used to forward all of
- FN's arguments to a function taking the same list of arguments -- namely
- the unchecked form of FN.
-
- We use CALL_FROM_THUNK_P instead of forward_parm for forwarding
- semantics. */
-
-static vec<tree, va_gc> *
-build_arg_list (tree fn)
-{
- vec<tree, va_gc> *args = make_tree_vector ();
- for (tree t = DECL_ARGUMENTS (fn); t; t = DECL_CHAIN (t))
- vec_safe_push (args, t);
- return args;
-}
-
-void
-start_function_contracts (tree decl1)
-{
- if (!handle_contracts_p (decl1))
- return;
-
- /* For cdtors, we evaluate the contracts check inline. */
- if (!outline_contracts_p (decl1))
- return;
-
- /* Contracts may have just been added without a chance to parse them, though
- we still need the PRE_FN available to generate a call to it. */
- if (!DECL_PRE_FN (decl1))
- build_contract_function_decls (decl1);
-
-}
-
-/* If we have a precondition function and it's valid, call it. */
-
-static void
-add_pre_condition_fn_call (tree fndecl)
-{
- /* If we're starting a guarded function with valid contracts, we need to
- insert a call to the pre function. */
- gcc_checking_assert (DECL_PRE_FN (fndecl)
- && DECL_PRE_FN (fndecl) != error_mark_node);
-
- releasing_vec args = build_arg_list (fndecl);
- tree call = build_call_a (DECL_PRE_FN (fndecl), args->length (),
- args->address ());
- CALL_FROM_THUNK_P (call) = true;
- finish_expr_stmt (call);
-}
-
-/* Build and add a call to the post-condition checking function, when that
- is in use. */
-
-static void
-add_post_condition_fn_call (tree fndecl)
-{
- gcc_checking_assert (DECL_POST_FN (fndecl)
- && DECL_POST_FN (fndecl) != error_mark_node);
-
- releasing_vec args = build_arg_list (fndecl);
- if (get_postcondition_result_parameter (fndecl))
- vec_safe_push (args, DECL_RESULT (fndecl));
- tree call = build_call_a (DECL_POST_FN (fndecl), args->length (),
- args->address ());
- CALL_FROM_THUNK_P (call) = true;
- finish_expr_stmt (call);
-}
-
-/* Add a call or a direct evaluation of the pre checks. */
-
-static void
-apply_preconditions (tree fndecl)
-{
- if (outline_contracts_p (fndecl))
- add_pre_condition_fn_call (fndecl);
- else
- emit_preconditions (DECL_CONTRACTS (fndecl));
-}
-
-/* Add a call or a direct evaluation of the post checks. */
-
-static void
-apply_postconditions (tree fndecl)
-{
- if (outline_contracts_p (fndecl))
- add_post_condition_fn_call (fndecl);
- else
- emit_postconditions (DECL_CONTRACTS (fndecl));
-}
-
-/* Add contract handling to the function in FNDECL.
-
- When we have only pre-conditions, this simply prepends a call (or a direct
- evaluation, for cdtors) to the existing function body.
-
- When we have post conditions we build a try-finally block.
- If the function might throw then the handler in the try-finally is an
- EH_ELSE expression, where the post condition check is applied to the
- non-exceptional path, and an empty statement is added to the EH path. If
- the function has a non-throwing eh spec, then the handler is simply the
- post-condition checker. */
-
-void
-maybe_apply_function_contracts (tree fndecl)
-{
- if (!handle_contracts_p (fndecl))
- /* We did nothing and the original function body statement list will be
- popped by our caller. */
- return;
-
- bool do_pre = has_active_preconditions (fndecl);
- bool do_post = has_active_postconditions (fndecl);
- /* We should not have reached here with nothing to do... */
- gcc_checking_assert (do_pre || do_post);
-
- /* This copies the approach used for function try blocks. */
- tree fnbody = pop_stmt_list (DECL_SAVED_TREE (fndecl));
- DECL_SAVED_TREE (fndecl) = push_stmt_list ();
- tree compound_stmt = begin_compound_stmt (0);
- current_binding_level->artificial = 1;
-
- /* Do not add locations for the synthesised code. */
- location_t loc = UNKNOWN_LOCATION;
-
- /* For other cases, we call a function to process the check. */
-
- /* If we have a pre, but not a post, then just emit that and we are done. */
- if (!do_post)
- {
- apply_preconditions (fndecl);
- add_stmt (fnbody);
- finish_compound_stmt (compound_stmt);
- return;
- }
-
- if (do_pre)
- /* Add a precondition call, if we have one. */
- apply_preconditions (fndecl);
- tree try_fin = build_stmt (loc, TRY_FINALLY_EXPR, fnbody, NULL_TREE);
- add_stmt (try_fin);
- TREE_OPERAND (try_fin, 1) = push_stmt_list ();
- /* If we have exceptions, and a function that might throw, then add
- an EH_ELSE clause that allows the exception to propagate upwards
- without encountering the post-condition checks. */
- if (flag_exceptions && !type_noexcept_p (TREE_TYPE (fndecl)))
- {
- tree eh_else = build_stmt (loc, EH_ELSE_EXPR, NULL_TREE, NULL_TREE);
- add_stmt (eh_else);
- TREE_OPERAND (eh_else, 0) = push_stmt_list ();
- apply_postconditions (fndecl);
- TREE_OPERAND (eh_else, 0) = pop_stmt_list (TREE_OPERAND (eh_else, 0));
- TREE_OPERAND (eh_else, 1) = build_empty_stmt (loc);
- }
- else
- apply_postconditions (fndecl);
- TREE_OPERAND (try_fin, 1) = pop_stmt_list (TREE_OPERAND (try_fin, 1));
- finish_compound_stmt (compound_stmt);
- /* The DECL_SAVED_TREE stmt list will be popped by our caller. */
-}
-
-/* Finish up the pre & post function definitions for a guarded FNDECL,
- and compile those functions all the way to assembler language output. */
-
-void
-finish_function_contracts (tree fndecl)
-{
- if (!handle_contracts_p (fndecl)
- || !outline_contracts_p (fndecl))
- return;
-
- for (tree ca = DECL_CONTRACTS (fndecl); ca; ca = CONTRACT_CHAIN (ca))
- {
- tree contract = CONTRACT_STATEMENT (ca);
- if (!CONTRACT_CONDITION (contract)
- || CONTRACT_CONDITION_DEFERRED_P (contract)
- || CONTRACT_CONDITION (contract) == error_mark_node)
- return;
- }
-
- int flags = SF_DEFAULT | SF_PRE_PARSED;
-
- /* If either the pre or post functions are bad, don't bother emitting
- any contracts. The program is already ill-formed. */
- tree pre = DECL_PRE_FN (fndecl);
- tree post = DECL_POST_FN (fndecl);
- if (pre == error_mark_node || post == error_mark_node)
- return;
-
- if (pre && DECL_INITIAL (fndecl) != error_mark_node)
- {
- DECL_PENDING_INLINE_P (pre) = false;
- start_preparsed_function (pre, DECL_ATTRIBUTES (pre), flags);
- remap_and_emit_conditions (fndecl, pre, PRECONDITION_STMT);
- tree finished_pre = finish_function (false);
- expand_or_defer_fn (finished_pre);
- }
-
- if (post && DECL_INITIAL (fndecl) != error_mark_node)
- {
- DECL_PENDING_INLINE_P (post) = false;
- start_preparsed_function (post,
- DECL_ATTRIBUTES (post),
- flags);
- remap_and_emit_conditions (fndecl, post, POSTCONDITION_STMT);
- if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (post))))
- finish_return_stmt (get_postcondition_result_parameter (fndecl));
-
- tree finished_post = finish_function (false);
- expand_or_defer_fn (finished_post);
- }
-}
-
-
-/* A subroutine of duplicate_decls. Diagnose issues in the redeclaration of
- guarded functions. */
-
-void
-duplicate_contracts (tree newdecl, tree olddecl)
-{
- if (TREE_CODE (newdecl) == TEMPLATE_DECL)
- newdecl = DECL_TEMPLATE_RESULT (newdecl);
- if (TREE_CODE (olddecl) == TEMPLATE_DECL)
- olddecl = DECL_TEMPLATE_RESULT (olddecl);
-
- /* Compare contracts to see if they match. */
- tree old_contracts = DECL_CONTRACTS (olddecl);
- tree new_contracts = DECL_CONTRACTS (newdecl);
-
- if (!old_contracts && !new_contracts)
- return;
-
- location_t old_loc = DECL_SOURCE_LOCATION (olddecl);
- location_t new_loc = DECL_SOURCE_LOCATION (newdecl);
-
- /* If both declarations specify contracts, ensure they match.
-
- TODO: This handles a potential error a little oddly. Consider:
-
- struct B {
- virtual void f(int n) [[pre: n == 0]];
- };
- struct D : B {
- void f(int n) override; // inherits contracts
- };
- void D::f(int n) [[pre: n == 0]] // OK
- { }
-
- It's okay because we're explicitly restating the inherited contract.
- Changing the precondition on the definition D::f causes match_contracts
- to complain about the mismatch.
-
- This would previously have been diagnosed as adding contracts to an
- override, but this seems like it should be well-formed. */
- if (old_contracts && new_contracts)
- {
- if (!match_contract_conditions (old_loc, old_contracts,
- new_loc, new_contracts,
- cmc_declaration))
- return;
- if (DECL_UNIQUE_FRIEND_P (newdecl))
- /* Newdecl's contracts are still DEFERRED_PARSE, and we're about to
- collapse it into olddecl, so stash away olddecl's contracts for
- later comparison. */
- defer_guarded_contract_match (olddecl, olddecl, old_contracts);
- }
-
- /* Handle cases where contracts are omitted in one or the other
- declaration. */
- if (old_contracts)
- {
- /* Contracts have been previously specified by are no omitted. The
- new declaration inherits the existing contracts. */
- if (!new_contracts)
- copy_contract_attributes (newdecl, olddecl);
-
- /* In all cases, remove existing contracts from OLDDECL to prevent the
- attribute merging function from adding excess contracts. */
- remove_contract_attributes (olddecl);
- }
- else if (!old_contracts)
- {
- /* We are adding contracts to a declaration. */
- if (new_contracts)
- {
- /* We can't add to a previously defined function. */
- if (DECL_INITIAL (olddecl))
- {
- auto_diagnostic_group d;
- error_at (new_loc, "cannot add contracts after definition");
- inform (DECL_SOURCE_LOCATION (olddecl), "original definition here");
- return;
- }
-
- /* We can't add to an unguarded virtual function declaration. */
- if (DECL_VIRTUAL_P (olddecl) && new_contracts)
- {
- auto_diagnostic_group d;
- error_at (new_loc, "cannot add contracts to a virtual function");
- inform (DECL_SOURCE_LOCATION (olddecl), "original declaration here");
- return;
- }
-
- /* Depending on the "first declaration" rule, we may not be able
- to add contracts to a function after the fact. */
- if (flag_contract_strict_declarations)
- {
- warning_at (new_loc,
- OPT_fcontract_strict_declarations_,
- "declaration adds contracts to %q#D",
- olddecl);
- return;
- }
-
- /* Copy the contracts from NEWDECL to OLDDECL. We shouldn't need to
- remap them because NEWDECL's parameters will replace those of
- OLDDECL. Remove the contracts from NEWDECL so they aren't
- cloned when merging. */
- copy_contract_attributes (olddecl, newdecl);
- remove_contract_attributes (newdecl);
- }
- }
-}
-
-/* Replace the any contract attributes on OVERRIDER with a copy where any
- references to BASEFN's PARM_DECLs have been rewritten to the corresponding
- PARM_DECL in OVERRIDER. */
-
-void
-inherit_base_contracts (tree overrider, tree basefn)
-{
- tree last = NULL_TREE, contract_attrs = NULL_TREE;
- for (tree a = DECL_CONTRACTS (basefn);
- a != NULL_TREE;
- a = CONTRACT_CHAIN (a))
- {
- tree c = copy_node (a);
- TREE_VALUE (c) = build_tree_list (TREE_PURPOSE (TREE_VALUE (c)),
- copy_node (CONTRACT_STATEMENT (c)));
-
- tree src = basefn;
- tree dst = overrider;
- remap_contract (src, dst, CONTRACT_STATEMENT (c), /*duplicate_p=*/true);
-
- CONTRACT_COMMENT (CONTRACT_STATEMENT (c)) =
- copy_node (CONTRACT_COMMENT (CONTRACT_STATEMENT (c)));
-
- chainon (last, c);
- last = c;
- if (!contract_attrs)
- contract_attrs = c;
- }
-
- set_decl_contracts (overrider, contract_attrs);
-}
-
-#include "gt-cp-contracts.h"
#ifndef GCC_CP_CONTRACT_H
#define GCC_CP_CONTRACT_H
-/* Contract levels approximate the complexity of the expression. */
-enum contract_level
-{
- CONTRACT_INVALID,
- CONTRACT_DEFAULT,
- CONTRACT_AUDIT,
- CONTRACT_AXIOM
-};
-
-/* The concrete semantics determine the behavior of a contract. */
-
-enum contract_semantic
-{
- CCS_INVALID,
- CCS_IGNORE,
- CCS_ASSUME,
- CCS_NEVER,
- CCS_MAYBE
-};
-
-/* True if the contract is unchecked. */
-
-inline bool
-unchecked_contract_p (contract_semantic cs)
-{
- return cs == CCS_IGNORE || cs == CCS_ASSUME;
-}
-
-/* True if the contract is checked. */
-
-inline bool
-checked_contract_p (contract_semantic cs)
-{
- return cs >= CCS_NEVER;
-}
-
-/* Must match std::contract_violation_continuation_mode in <contract>. */
-enum contract_continuation
-{
- NEVER_CONTINUE,
- MAYBE_CONTINUE
-};
-
-/* Assertion role info. */
-struct contract_role
-{
- const char *name;
- contract_semantic default_semantic;
- contract_semantic audit_semantic;
- contract_semantic axiom_semantic;
-};
-
-/* Information for configured contract semantics. */
-
-struct contract_configuration
-{
- contract_level level;
- contract_role* role;
-};
-
-/* A contract mode contains information used to derive the checking
- and assumption semantics of a contract. This is either a dynamic
- configuration, meaning it derives from the build mode, or it is
- explicitly specified. */
-
-struct contract_mode
-{
- contract_mode () : kind(cm_invalid) {}
- contract_mode (contract_level level, contract_role *role = NULL)
- : kind(cm_dynamic)
- {
- contract_configuration cc;
- cc.level = level;
- cc.role = role;
- u.config = cc;
- }
- contract_mode (contract_semantic semantic) : kind(cm_explicit)
- {
- u.semantic = semantic;
- }
-
- contract_level get_level () const
- {
- gcc_assert (kind == cm_dynamic);
- return u.config.level;
- }
-
- contract_role *get_role () const
- {
- gcc_assert (kind == cm_dynamic);
- return u.config.role;
- }
-
- contract_semantic get_semantic () const
- {
- gcc_assert (kind == cm_explicit);
- return u.semantic;
- }
-
- enum { cm_invalid, cm_dynamic, cm_explicit } kind;
-
- union
- {
- contract_configuration config;
- contract_semantic semantic;
- } u;
-};
-
-/* Map a source level semantic or level name to its value, or invalid. */
-extern contract_semantic map_contract_semantic (const char *);
-extern contract_level map_contract_level (const char *);
-
-/* Check if an attribute is a cxx contract attribute. */
-extern bool cxx_contract_attribute_p (const_tree);
-extern bool cp_contract_assertion_p (const_tree);
-
-/* Handle various command line arguments related to semantic mapping. */
-extern void handle_OPT_fcontract_build_level_ (const char *);
-extern void handle_OPT_fcontract_assumption_mode_ (const char *);
-extern void handle_OPT_fcontract_continuation_mode_ (const char *);
-extern void handle_OPT_fcontract_role_ (const char *);
-extern void handle_OPT_fcontract_semantic_ (const char *);
-
-enum contract_matching_context
-{
- cmc_declaration,
- cmc_override
-};
-
-/* True if NODE is any kind of contract. */
-#define CONTRACT_P(NODE) \
- (TREE_CODE (NODE) == ASSERTION_STMT \
- || TREE_CODE (NODE) == PRECONDITION_STMT \
- || TREE_CODE (NODE) == POSTCONDITION_STMT)
-
-/* True if NODE is a contract condition. */
-#define CONTRACT_CONDITION_P(NODE) \
- (TREE_CODE (NODE) == PRECONDITION_STMT \
- || TREE_CODE (NODE) == POSTCONDITION_STMT)
-
-/* True if NODE is a precondition. */
-#define PRECONDITION_P(NODE) \
- (TREE_CODE (NODE) == PRECONDITION_STMT)
-
-/* True if NODE is a postcondition. */
-#define POSTCONDITION_P(NODE) \
- (TREE_CODE (NODE) == POSTCONDITION_STMT)
-
-#define CONTRACT_CHECK(NODE) \
- (TREE_CHECK3 (NODE, ASSERTION_STMT, PRECONDITION_STMT, POSTCONDITION_STMT))
-
-/* True iff the FUNCTION_DECL NODE currently has any contracts. */
-#define DECL_HAS_CONTRACTS_P(NODE) \
- (DECL_CONTRACTS (NODE) != NULL_TREE)
-
-/* For a FUNCTION_DECL of a guarded function, this points to a list of the pre
- and post contracts of the first decl of NODE in original order. */
-#define DECL_CONTRACTS(NODE) \
- (find_contract (DECL_ATTRIBUTES (NODE)))
-
-/* The next contract (if any) after this one in an attribute list. */
-#define CONTRACT_CHAIN(NODE) \
- (find_contract (TREE_CHAIN (NODE)))
-
-/* The wrapper of the original source location of a list of contracts. */
-#define CONTRACT_SOURCE_LOCATION_WRAPPER(NODE) \
- (TREE_PURPOSE (TREE_VALUE (NODE)))
-
-/* The original source location of a list of contracts. */
-#define CONTRACT_SOURCE_LOCATION(NODE) \
- (EXPR_LOCATION (CONTRACT_SOURCE_LOCATION_WRAPPER (NODE)))
-
-/* The actual code _STMT for a contract attribute. */
-#define CONTRACT_STATEMENT(NODE) \
- (TREE_VALUE (TREE_VALUE (NODE)))
-
-/* True if the contract semantic was specified literally. If true, the
- contract mode is an identifier containing the semantic. Otherwise,
- it is a TREE_LIST whose TREE_VALUE is the level and whose TREE_PURPOSE
- is the role. */
-#define CONTRACT_LITERAL_MODE_P(NODE) \
- (CONTRACT_MODE (NODE) != NULL_TREE \
- && TREE_CODE (CONTRACT_MODE (NODE)) == IDENTIFIER_NODE)
-
-/* The identifier denoting the literal semantic of the contract. */
-#define CONTRACT_LITERAL_SEMANTIC(NODE) \
- (TREE_OPERAND (NODE, 0))
-
-/* The written "mode" of the contract. Either an IDENTIFIER with the
- literal semantic or a TREE_LIST containing the level and role. */
-#define CONTRACT_MODE(NODE) \
- (TREE_OPERAND (CONTRACT_CHECK (NODE), 0))
-
-/* The identifier denoting the build level of the contract. */
-#define CONTRACT_LEVEL(NODE) \
- (TREE_VALUE (CONTRACT_MODE (NODE)))
-
-/* The identifier denoting the role of the contract */
-#define CONTRACT_ROLE(NODE) \
- (TREE_PURPOSE (CONTRACT_MODE (NODE)))
-
-/* The parsed condition of the contract. */
-#define CONTRACT_CONDITION(NODE) \
- (TREE_OPERAND (CONTRACT_CHECK (NODE), 1))
-
-/* True iff the condition of the contract NODE is not yet parsed. */
-#define CONTRACT_CONDITION_DEFERRED_P(NODE) \
- (TREE_CODE (CONTRACT_CONDITION (NODE)) == DEFERRED_PARSE)
-
-/* The raw comment of the contract. */
-#define CONTRACT_COMMENT(NODE) \
- (TREE_OPERAND (CONTRACT_CHECK (NODE), 2))
-
-/* The VAR_DECL of a postcondition result. For deferred contracts, this
- is an IDENTIFIER. */
-#define POSTCONDITION_IDENTIFIER(NODE) \
- (TREE_OPERAND (POSTCONDITION_STMT_CHECK (NODE), 3))
-
-/* For a FUNCTION_DECL of a guarded function, this holds the function decl
- where pre contract checks are emitted. */
-#define DECL_PRE_FN(NODE) \
- (get_precondition_function ((NODE)))
-
-/* For a FUNCTION_DECL of a guarded function, this holds the function decl
- where post contract checks are emitted. */
-#define DECL_POST_FN(NODE) \
- (get_postcondition_function ((NODE)))
-
-/* True iff the FUNCTION_DECL is the pre function for a guarded function. */
-#define DECL_IS_PRE_FN_P(NODE) \
- (DECL_ABSTRACT_ORIGIN (NODE) && DECL_PRE_FN (DECL_ABSTRACT_ORIGIN (NODE)) == NODE)
-
-/* True iff the FUNCTION_DECL is the post function for a guarded function. */
-#define DECL_IS_POST_FN_P(NODE) \
- (DECL_ABSTRACT_ORIGIN (NODE) && DECL_POST_FN (DECL_ABSTRACT_ORIGIN (NODE)) == NODE)
-
-/* contracts.cc */
-extern void emit_assertion (tree);
-
-extern void remove_contract_attributes (tree);
-extern bool all_attributes_are_contracts_p (tree);
-extern tree finish_contract_attribute (tree, tree);
-extern void copy_contract_attributes (tree, tree);
-extern bool diagnose_misapplied_contracts (tree);
-extern void remap_contracts (tree, tree, tree, bool);
-extern tree splice_out_contracts (tree);
-extern void inherit_base_contracts (tree, tree);
-
-extern tree make_postcondition_variable (cp_expr);
-extern tree make_postcondition_variable (cp_expr, tree);
-extern void maybe_update_postconditions (tree);
-extern void rebuild_postconditions (tree);
-extern bool check_postcondition_result (tree, tree, location_t);
-
-extern tree grok_contract (tree, tree, tree, cp_expr,
- location_t);
-extern tree finish_contract_condition (cp_expr);
-extern void update_late_contract (tree, tree, tree);
-extern tree invalidate_contract (tree);
-extern void duplicate_contracts (tree, tree);
-
-extern void match_deferred_contracts (tree);
-extern void defer_guarded_contract_match (tree, tree, tree);
-
-extern tree get_precondition_function (tree);
-extern tree get_postcondition_function (tree);
-extern void start_function_contracts (tree);
-extern void maybe_apply_function_contracts (tree);
-extern void finish_function_contracts (tree);
-extern void set_contract_functions (tree, tree, tree);
-
-extern tree build_contract_check (tree);
-
-/* Return the first contract in ATTRS, or NULL_TREE if there are none. */
-
-inline tree
-find_contract (tree attrs)
-{
- while (attrs && !cxx_contract_attribute_p (attrs))
- attrs = TREE_CHAIN (attrs);
- return attrs;
-}
-
-inline void
-set_decl_contracts (tree decl, tree contract_attrs)
-{
- remove_contract_attributes (decl);
- DECL_ATTRIBUTES (decl) = chainon (DECL_ATTRIBUTES (decl), contract_attrs);
-}
-
-/* Returns the computed semantic of the node. */
-
-inline contract_semantic
-get_contract_semantic (const_tree t)
-{
- return (contract_semantic) (TREE_LANG_FLAG_3 (CONTRACT_CHECK (t))
- | (TREE_LANG_FLAG_2 (t) << 1)
- | (TREE_LANG_FLAG_0 ((t)) << 2));
-}
-
-/* Sets the computed semantic of the node. */
-
-inline void
-set_contract_semantic (tree t, contract_semantic semantic)
-{
- TREE_LANG_FLAG_3 (CONTRACT_CHECK (t)) = semantic & 0x01;
- TREE_LANG_FLAG_2 (t) = (semantic & 0x02) >> 1;
- TREE_LANG_FLAG_0 (t) = (semantic & 0x04) >> 2;
-}
-
-
-/* Will this contract be ignored. */
-
-inline bool
-contract_ignored_p (const_tree contract)
-{
- return (get_contract_semantic (contract) <= CCS_IGNORE);
-}
-
-/* Will this contract be evaluated? */
-
-inline bool
-contract_evaluated_p (const_tree contract)
-{
- return (get_contract_semantic (contract) >= CCS_NEVER);
-}
#endif /* ! GCC_CP_CONTRACT_H */
#include "gcc-rich-location.h"
#include "hash-map.h"
#include "coroutines.h"
-#include "contracts.h"
/* ================= Debug. ================= */
DECL_USER_ALIGN (fn) = DECL_USER_ALIGN (orig);
/* Apply attributes from the original fn. */
DECL_ATTRIBUTES (fn) = copy_list (DECL_ATTRIBUTES (orig));
- /* but we do not want ones for contracts. */
- remove_contract_attributes (fn);
/* A void return. */
tree resdecl = build_decl (loc, RESULT_DECL, 0, void_type_node);
#include "omp-general.h"
#include "opts.h"
#include "gcc-urlifier.h"
-#include "contracts.h"
/* Keep track of forward references to immediate-escalating functions in
case they become consteval. This vector contains ADDR_EXPRs and
wtd->bind_expr_stack.pop ();
break;
- case ASSERTION_STMT:
- case PRECONDITION_STMT:
- case POSTCONDITION_STMT:
- {
- if (tree check = build_contract_check (stmt))
- {
- *stmt_p = check;
- return cp_genericize_r (stmt_p, walk_subtrees, data);
- }
-
- /* If we didn't build a check, replace it with void_node so we don't
- leak contracts into GENERIC. */
- *stmt_p = void_node;
- *walk_subtrees = 0;
- }
- break;
-
case USING_STMT:
{
tree block = NULL_TREE;
#include "c-family/c-common.h"
#include "dwarf2.h"
#include "stringpool.h"
-#include "contracts.h"
/* Class to determine whether a given C++ language feature is available.
Used to implement __has_{feature,extension}. */
if (handle_module_option (unsigned (scode), arg, value))
return true;
- enum opt_code code = (enum opt_code) scode;
- bool handled_p = true;
-
- switch (code)
- {
- case OPT_fcontract_build_level_:
- handle_OPT_fcontract_build_level_ (arg);
- break;
-
- case OPT_fcontract_assumption_mode_:
- handle_OPT_fcontract_assumption_mode_ (arg);
- break;
-
- case OPT_fcontract_continuation_mode_:
- handle_OPT_fcontract_continuation_mode_ (arg);
- break;
-
- case OPT_fcontract_role_:
- handle_OPT_fcontract_role_ (arg);
- break;
-
- case OPT_fcontract_semantic_:
- handle_OPT_fcontract_semantic_ (arg);
- break;
-
- default:
- handled_p = false;
- break;
- }
- if (handled_p)
- return handled_p;
-
return c_common_handle_option (scode, arg, value, kind, loc, handlers);
}
CPTI_THREAD_ATEXIT,
CPTI_DSO_HANDLE,
CPTI_DCAST,
-
- CPTI_PSEUDO_CONTRACT_VIOLATION,
CPTI_META_INFO_TYPE,
CPTI_MAX
#define current_aggr cp_global_trees[CPTI_AGGR_TAG]
/* std::align_val_t */
#define align_type_node cp_global_trees[CPTI_ALIGN_TYPE]
-#define pseudo_contract_violation_type cp_global_trees[CPTI_PSEUDO_CONTRACT_VIOLATION]
#define meta_info_type_node cp_global_trees[CPTI_META_INFO_TYPE]
/* We cache these tree nodes so as to call get_identifier less frequently.
OVL_DEDUP_P (in OVERLOAD)
INIT_EXPR_NRV_P (in INIT_EXPR)
ATOMIC_CONSTR_MAP_INSTANTIATED_P (in ATOMIC_CONSTR)
- contract_semantic (in ASSERTION_, PRECONDITION_, POSTCONDITION_STMT)
RETURN_EXPR_LOCAL_ADDR_P (in RETURN_EXPR)
PACK_INDEX_PARENTHESIZED_P (in PACK_INDEX_*)
MUST_NOT_THROW_NOEXCEPT_P (in MUST_NOT_THROW_EXPR)
LAMBDA_EXPR_CAPTURE_OPTIMIZED (in LAMBDA_EXPR)
IMPLICIT_CONV_EXPR_BRACED_INIT (in IMPLICIT_CONV_EXPR)
PACK_EXPANSION_AUTO_P (in *_PACK_EXPANSION)
- contract_semantic (in ASSERTION_, PRECONDITION_, POSTCONDITION_STMT)
STATIC_INIT_DECOMP_NONBASE_P (in the TREE_LIST
for {static,tls}_aggregates)
MUST_NOT_THROW_CATCH_P (in MUST_NOT_THROW_EXPR)
LAMBDA_EXPR_STATIC_P (in LAMBDA_EXPR)
TARGET_EXPR_ELIDING_P (in TARGET_EXPR)
IF_STMT_VACUOUS_INIT_P (IF_STMT)
- contract_semantic (in ASSERTION_, PRECONDITION_, POSTCONDITION_STMT)
TYPENAME_IS_RESOLVING_P (in TYPENAME_TYPE)
4: IDENTIFIER_MARKED (IDENTIFIER_NODEs)
TREE_HAS_CONSTRUCTOR (in INDIRECT_REF, SAVE_EXPR, CONSTRUCTOR,
int x_processing_template_decl;
int x_processing_specialization;
int x_processing_constraint;
- int x_processing_contract_condition;
int suppress_location_wrappers;
BOOL_BITFIELD x_processing_explicit_instantiation : 1;
BOOL_BITFIELD need_pop_function_context : 1;
#define processing_explicit_instantiation scope_chain->x_processing_explicit_instantiation
#define processing_omp_trait_property_expr scope_chain->x_processing_omp_trait_property_expr
-/* Nonzero if we are parsing the conditional expression of a contract
- condition. These expressions appear outside the parameter list (like a
- trailing return type), but are potentially evaluated. */
-
-#define processing_contract_condition scope_chain->x_processing_contract_condition
-
#define in_discarded_stmt scope_chain->discarded_stmt
#define in_consteval_if_p scope_chain->consteval_if_p
#define in_expansion_stmt scope_chain->expansion_stmt
FIXME we should always do this except during deduction/ordering. */
extern int comparing_dependent_aliases;
-/* Nonzero if we want to consider different member expressions to compare
- equal if they designate the same entity. This is set when comparing
- contract conditions of overrides. */
-extern bool comparing_override_contracts;
-
/* In parser.cc. */
extern bool cp_preserve_using_decl;
#include "opts.h"
#include "langhooks-def.h" /* For lhd_simulate_record_decl */
#include "coroutines.h"
-#include "contracts.h"
#include "gcc-urlifier.h"
#include "diagnostic-highlight-colors.h"
#include "pretty-print-markup.h"
= DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
- duplicate_contracts (newdecl, olddecl);
-
/* Optionally warn about more than one declaration for the same
name, but don't warn about a function declaration followed by a
definition. */
specializations. */
gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
- /* Make sure the contracts are equivalent. */
- duplicate_contracts (newdecl, olddecl);
-
- /* Remove contracts from old_result so they aren't appended to
- old_result by the merge function. */
- remove_contract_attributes (old_result);
-
DECL_ATTRIBUTES (old_result)
= (*targetm.merge_decl_attributes) (old_result, new_result);
}
if (! types_match || new_defines_function)
{
- /* These are the final DECL_ARGUMENTS that will be used within the
- body; update any references to old DECL_ARGUMENTS in the
- contracts, if present. */
- if (tree contracts = DECL_CONTRACTS (newdecl))
- remap_contracts (olddecl, newdecl, contracts, true);
/* Mark the old PARM_DECLs in case std::meta::parameters_of has
been called on the old declaration and reflections of those
info and other bits, but if not, we won't. */
DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
-
- /* In some cases, duplicate_contracts will remove contracts from
- OLDDECL, to avoid duplications. Sometimes, the contracts end up
- shared. If we removed them, re-add them. */
- if (!DECL_CONTRACTS (olddecl))
- copy_contract_attributes (olddecl, newdecl);
}
/* If redeclaring a builtin function, it stays built in
if newdecl is a gnu_inline definition, or if newdecl is just
/* Don't clear out the arguments if we're just redeclaring a
function. */
if (DECL_ARGUMENTS (olddecl))
- {
- /* If we removed contracts from previous definition, re-attach
- them. Otherwise, rewrite the contracts so they match the
- parameters of the new declaration. */
- if (DECL_INITIAL (olddecl)
- && DECL_CONTRACTS (newdecl)
- && !DECL_CONTRACTS (olddecl))
- copy_contract_attributes (olddecl, newdecl);
- else
- {
- /* Temporarily undo the re-contexting of parameters so we can
- actually remap parameters. The inliner won't replace
- parameters if we don't do this. */
- tree args = DECL_ARGUMENTS (newdecl);
- for (tree p = args; p; p = DECL_CHAIN (p))
- DECL_CONTEXT (p) = newdecl;
-
- /* Save new argument names for use in contracts parsing,
- unless we've already started parsing the body of olddecl
- (particular issues arise when newdecl is from a prior
- friend decl with no argument names, see
- modules/contracts-tpl-friend-1). */
- if (tree contracts = DECL_CONTRACTS (olddecl))
- remap_contracts (newdecl, olddecl, contracts, true);
-
- /* And reverse this operation again. */
- for (tree p = args; p; p = DECL_CHAIN (p))
- DECL_CONTEXT (p) = olddecl;
- }
-
DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
- }
}
}
else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
if (flag_exceptions)
init_exception_processing ();
- if (flag_contracts)
- init_terminate_fn ();
-
if (modules_p ())
init_modules (parse_in);
"an attribute that appertains to a type-specifier is ignored");
}
- /* Diagnose invalid application of contracts, if any. */
- if (find_contract (declspecs->attributes))
- diagnose_misapplied_contracts (declspecs->attributes);
- else
- diagnose_misapplied_contracts (declspecs->std_attributes);
-
return declared_type;
}
/* Aliases are definitions. */
&& !alias)
{
- if (DECL_VIRTUAL_P (decl) || !flag_contracts)
- permerror (declarator->id_loc,
+ permerror (declarator->id_loc,
"declaration of %q#D outside of class is not definition",
decl);
- else if (flag_contract_strict_declarations)
- warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_,
- "declaration of %q#D outside of class is not definition",
- decl);
}
}
*attrlist = NULL_TREE;
}
- if (DECL_HAS_CONTRACTS_P (decl))
- rebuild_postconditions (decl);
-
/* Check main's type after attributes have been applied. */
if (ctype == NULL_TREE && DECL_MAIN_P (decl))
{
}
}
- if (declspecs->std_attributes
- && !diagnose_misapplied_contracts (declspecs->std_attributes))
+ if (declspecs->std_attributes)
{
location_t attr_loc = declspecs->locations[ds_std_attribute];
auto_diagnostic_group d;
"is ignored");
}
- if (attrlist)
- diagnose_misapplied_contracts (*attrlist);
-
/* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */
bool is_xobj_member_function = false;
/* Determine the type of the entity declared by recurring on the
inner_declarator = declarator->declarator;
- /* Check that contracts aren't misapplied. */
- if (tree contract_attr = find_contract (declarator->std_attributes))
- if (declarator->kind != cdk_function
- || innermost_code != cdk_function)
- diagnose_misapplied_contracts (contract_attr);
-
/* We don't want to warn in parameter context because we don't
yet know if the parse will succeed, and this might turn out
to be a constructor call. */
returned_attrs = attr_chainon (returned_attrs, att);
}
- /* Actually apply the contract attributes to the declaration. */
- for (tree *p = &attrs; *p;)
- {
- tree l = *p;
- if (cxx_contract_attribute_p (l))
- {
- *p = TREE_CHAIN (l);
- /* Intentionally reverse order of contracts so they're
- reversed back into their lexical order. */
- TREE_CHAIN (l) = NULL_TREE;
- returned_attrs = chainon (l, returned_attrs);
- }
- else
- p = &TREE_CHAIN (l);
- }
-
if (attrs)
/* [dcl.fct]/2:
warn for standard attributes. */
if (attrlist
&& !funcdef_flag
- && cxx11_attribute_p (*attrlist)
- && !all_attributes_are_contracts_p (*attrlist))
+ && cxx11_attribute_p (*attrlist))
{
*attrlist = NULL_TREE;
auto_diagnostic_group d;
store_parm_decls (current_function_parms);
- start_function_contracts (decl1);
-
if (!processing_template_decl
&& flag_lifetime_dse > 1
&& DECL_CONSTRUCTOR_P (decl1)
if (fndecl == NULL_TREE || fndecl == error_mark_node)
return error_mark_node;
- bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl)
- && !processing_template_decl);
-
if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl))
finish_lambda_scope ();
finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
(TREE_TYPE (fndecl)),
current_eh_spec_block);
-
- /* If outlining succeeded, then add contracts handling if needed. */
- if (coroutine->cp_valid_coroutine () && do_contracts)
- maybe_apply_function_contracts (fndecl);
}
else
/* For a cloned function, we've already got all the code we need;
finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
(TREE_TYPE (current_function_decl)),
current_eh_spec_block);
-
- if (do_contracts)
- maybe_apply_function_contracts (current_function_decl);
-
}
/* If we're saving up tree structure, tie off the function now. */
/* Clean up. */
invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
- /* Build outlined functions for coroutines and contracts. */
+ /* Build outlined functions for coroutines. */
if (coroutine)
{
delete coroutine;
}
- /* If we have used outlined contracts checking functions, build and emit
- them here. */
- finish_function_contracts (fndecl);
-
return fndecl;
}
\f
#include "omp-general.h"
#include "tree-inline.h"
#include "escaped_string.h"
-#include "contracts.h"
#include "gcc-rich-location.h"
#include "tree-pretty-print-markup.h"
tree attr;
for (attr = attributes; attr; attr = TREE_CHAIN (attr))
{
- if (cxx_contract_attribute_p (attr))
- continue;
-
/* Annotation arguments are handled in handle_annotation_attribute. */
if (annotation_p (attr))
continue;
comdat_linkage (tree decl)
{
if (flag_weak)
- {
- make_decl_one_only (decl, cxx_comdat_group (decl));
- if (HAVE_COMDAT_GROUP && flag_contracts && DECL_CONTRACTS (decl))
- {
- symtab_node *n = symtab_node::get (decl);
- if (tree pre = DECL_PRE_FN (decl))
- cgraph_node::get_create (pre)->add_to_same_comdat_group (n);
- if (tree post = DECL_POST_FN (decl))
- cgraph_node::get_create (post)->add_to_same_comdat_group (n);
- }
- }
+ make_decl_one_only (decl, cxx_comdat_group (decl));
else if (TREE_CODE (decl) == FUNCTION_DECL
|| (VAR_P (decl) && DECL_ARTIFICIAL (decl)))
/* We can just emit function and compiler-generated variables
write_encoding (decl);
}
- /* If this is the pre/post function for a guarded function, append
- .pre/post, like something from create_virtual_clone. */
- if (DECL_IS_PRE_FN_P (decl))
- write_string (".pre");
- else if (DECL_IS_POST_FN_P (decl))
- write_string (".post");
-
/* If this is a coroutine helper, then append an appropriate string to
identify which. */
if (tree ramp = DECL_RAMP_FN (decl))
#include "attribs.h"
#include "intl.h"
#include "langhooks.h"
-#include "contracts.h"
/* This TU doesn't need or want to see the networking. */
#define CODY_NETWORKING 0
#include "mapper-client.h"
// deferred noexcept and default parms, or references
// to parms from earlier forward-decls (PR c++/119608).
//
- // Currently we'll end up cloning those bits of tree.
+ // Currently we'll end up cloning those bits of tree.
// It would be nice to reference those specific nodes.
// I think putting those things in the map when we
// reference their template by name.
base_tag - ix, ix, parm, fn);
tree_node_vals (parm);
}
-
- if (!streaming_p ())
- {
- /* We must walk contract attrs so the dependency graph is complete. */
- for (tree contract = DECL_CONTRACTS (fn);
- contract;
- contract = CONTRACT_CHAIN (contract))
- tree_node (contract);
- }
-
- /* Write a reference to contracts pre/post functions, if any, to avoid
- regenerating them in importers. */
- tree_node (DECL_PRE_FN (fn));
- tree_node (DECL_POST_FN (fn));
}
/* Build skeleton parm nodes, read their flags, type & parm indices. */
return 0;
}
- /* Reload references to contract functions, if any. */
- tree pre_fn = tree_node ();
- tree post_fn = tree_node ();
- set_contract_functions (fn, pre_fn, post_fn);
-
return base_tag;
}
Matches decls_match behaviour. */
&& (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
|| !DECL_EXTERN_C_P (m_inner)
- || DECL_EXTERN_C_P (d_inner))
- /* Reject if one is a different member of a
- guarded/pre/post fn set. */
- && (!flag_contracts
- || (DECL_IS_PRE_FN_P (d_inner)
- == DECL_IS_PRE_FN_P (m_inner)))
- && (!flag_contracts
- || (DECL_IS_POST_FN_P (d_inner)
- == DECL_IS_POST_FN_P (m_inner))))
+ || DECL_EXTERN_C_P (d_inner)))
{
tree m_reqs = get_constraints (m_inner);
if (m_reqs)
(cxx_dialect < cxx20 && flag_coroutines
? "/coroutines" : ""),
flag_module_implicit_inline ? "/implicit-inline" : "",
- flag_contracts ? "/contracts" : "",
NULL);
return dialect;
#include "cp-name-hint.h"
#include "memmodel.h"
#include "c-family/known-headers.h"
-#include "contracts.h"
#include "bitmap.h"
#include "builtins.h"
#include "analyzer/analyzer-language.h"
parser->unparsed_queues->last ().nsdmis
#define unparsed_noexcepts \
parser->unparsed_queues->last ().noexcepts
-#define unparsed_contracts \
- parser->unparsed_queues->last ().contracts
static void
push_unparsed_function_queues (cp_parser *parser)
static tree cp_parser_yield_expression
(cp_parser *);
-/* Contracts */
-
-static void cp_parser_late_contract_condition
- (cp_parser *, tree, tree);
-
enum pragma_context {
pragma_external,
pragma_member,
return attrs;
}
-/* True if and only if the name is one of the contract types. */
-
-static bool
-contract_attribute_p (const_tree id)
-{
- return is_attribute_p ("assert", id)
- || is_attribute_p ("pre", id)
- || is_attribute_p ("post", id);
-}
-
/* Handle omp::directive and omp::sequence attributes in *PATTRS
(if any) at the start or after declaration-id of a declaration. */
/* Peek at the next token. */
token = cp_lexer_peek_token (parser->lexer);
- /* If we have contracts, check that they're valid in this context. */
- if (std_attrs != error_mark_node)
- {
- if (tree pre = lookup_attribute ("pre", std_attrs))
- error_at (EXPR_LOCATION (TREE_VALUE (pre)),
- "preconditions cannot be statements");
- else if (tree post = lookup_attribute ("post", std_attrs))
- error_at (EXPR_LOCATION (TREE_VALUE (post)),
- "postconditions cannot be statements");
-
- /* Check that assertions are null statements. */
- if (cp_contract_assertion_p (std_attrs))
- if (token->type != CPP_SEMICOLON)
- error_at (token->location, "assertions must be followed by %<;%>");
- }
-
bool omp_attrs_forbidden_p;
omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
"%<fallthrough%> attribute not followed by %<;%>");
std_attrs = NULL_TREE;
}
-
- /* Handle [[assert: ...]]; */
- if (cp_contract_assertion_p (std_attrs))
- {
- /* Add the assertion as a statement in the current block. */
- if (!statement)
- emit_assertion (std_attrs);
- /* We already checked that the contract assertion is followed by
- a semicolon. */
- std_attrs = NULL_TREE;
- }
}
/* Set the line number for the statement. */
declared. */;
else
{
- if (find_contract (attrs))
- {
- diagnose_misapplied_contracts (attrs);
- attrs = NULL_TREE;
- }
- else if (decl_specs->type
+ if (decl_specs->type
&& decl_specs->type_definition_p
&& OVERLOAD_TYPE_P (decl_specs->type))
{
}
vec_safe_truncate (unparsed_nsdmis, 0);
- /* Now contract attributes. */
- FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
- {
- tree ctx = DECL_CONTEXT (decl);
- switch_to_class (ctx);
-
- temp_override<tree> cfd(current_function_decl, decl);
-
- /* Make sure that any template parameters are in scope. */
- maybe_begin_member_template_processing (decl);
-
- /* Make sure that any member-function parameters are in scope.
- This function doesn't expect ccp to be set. */
- current_class_ptr = current_class_ref = NULL_TREE;
- inject_parm_decls (decl);
-
- /* 'this' is not allowed in static member functions. */
- unsigned char local_variables_forbidden_p
- = parser->local_variables_forbidden_p;
- if (DECL_THIS_STATIC (decl))
- parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
-
- /* Now we can parse contract conditions. */
- for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
- {
- if (cxx_contract_attribute_p (a))
- cp_parser_late_contract_condition (parser, decl, a);
- }
-
- /* Restore the state of local_variables_forbidden_p. */
- parser->local_variables_forbidden_p = local_variables_forbidden_p;
-
- /* Remove any member-function parameters from the symbol table. */
- pop_injected_parms ();
-
- /* Remove any template parameters from the symbol table. */
- maybe_end_member_template_processing ();
-
- /* Perform any deferred contract matching. */
- match_deferred_contracts (decl);
- }
- vec_safe_truncate (unparsed_contracts, 0);
-
current_class_ptr = NULL_TREE;
current_class_ref = NULL_TREE;
switch_to_class (NULL_TREE);
/* Parse the attributes. */
attributes = cp_parser_attributes_opt (parser);
- if (find_contract (attributes))
- diagnose_misapplied_contracts (attributes);
/* If the next token is `::', that is invalid -- but sometimes
people do try to write:
return attributes;
}
-/* Optionally parse a C++20 contract role. A NULL return means that no
- contract role was specified.
-
- contract-role:
- % default
- % identifier
-
- If the identifier does not name a known contract role, it will
- be assumed to be default. Returns the identifier for the role
- token. */
-
-static tree
-cp_parser_contract_role (cp_parser *parser)
-{
- gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
- cp_lexer_consume_token (parser->lexer);
-
- cp_token *token = cp_lexer_peek_token (parser->lexer);
- tree role_id = NULL_TREE;
- if (token->type == CPP_NAME)
- role_id = token->u.value;
- else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
- role_id = get_identifier ("default");
- else
- {
- error_at (token->location, "expected contract-role");
- return error_mark_node;
- }
- cp_lexer_consume_token (parser->lexer);
-
- /* FIXME: Warn about invalid/unknown roles? */
- return role_id;
-}
-
-/* Parse an optional contract mode.
-
- contract-mode:
- contract-semantic
- [contract-level] [contract-role]
-
- contract-semantic:
- check_never_continue
- check_maybe_continue
- check_always_continue
-
- contract-level:
- default
- audit
- axiom
-
- contract-role:
- default
- identifier
-
- This grammar is taken from P1332R0. During parsing, this sets options
- on the MODE object to determine the configuration of the contract.
-
- Returns a tree containing the identifiers used in the configuration.
- This is either an IDENTIFIER with the literal semantic or a TREE_LIST
- whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
- contract-role, if any. NULL_TREE is returned if no information is
- given (i.e., all defaults selected). */
-
-static tree
-cp_parser_contract_mode_opt (cp_parser *parser,
- bool postcondition_p)
-{
- /* The mode is empty; the level and role are default. */
- if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
- return NULL_TREE;
-
- /* There is only a role; the level is default. */
- if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
- {
- tree role_id = cp_parser_contract_role (parser);
- return build_tree_list (role_id, get_identifier ("default"));
- }
-
- /* Otherwise, match semantic or level. */
- cp_token *token = cp_lexer_peek_token (parser->lexer);
- contract_level level = CONTRACT_INVALID;
- contract_semantic semantic = CCS_INVALID;
- tree config_id;
- if (token->type == CPP_NAME)
- {
- config_id = token->u.value;
-
- /* Either a named level, a concrete semantic, or an identifier
- for a postcondition. */
- const char *ident = IDENTIFIER_POINTER (token->u.value);
- level = map_contract_level (ident);
- semantic = map_contract_semantic (ident);
-
- /* The identifier is the return value for a postcondition. */
- if (level == CONTRACT_INVALID && semantic == CCS_INVALID
- && postcondition_p)
- return NULL_TREE;
- }
- else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
- {
- config_id = get_identifier ("default");
- level = CONTRACT_DEFAULT;
- }
- else
- {
- /* We got some other token other than a ':'. */
- error_at (token->location, "expected contract semantic or level");
- return NULL_TREE;
- }
-
- /* Consume the literal semantic or level token. */
- cp_lexer_consume_token (parser->lexer);
-
- if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
- {
- error_at (token->location,
- "expected contract level: "
- "%<default%>, %<audit%>, or %<axiom%>");
- return NULL_TREE;
- }
-
- /* We matched an explicit semantic. */
- if (semantic != CCS_INVALID)
- {
- if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
- {
- error ("invalid use of contract role for explicit semantic");
- cp_lexer_consume_token (parser->lexer);
- cp_lexer_consume_token (parser->lexer);
- }
- return config_id;
- }
-
- /* We matched a level, there may be a role; otherwise this is default. */
- if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
- {
- tree role_id = cp_parser_contract_role (parser);
- return build_tree_list (role_id, config_id);
- }
-
- return build_tree_list (NULL_TREE, config_id);
-}
-
-static tree
-find_error (tree *tp, int *, void *)
-{
- if (*tp == error_mark_node)
- return *tp;
- return NULL_TREE;
-}
-
-static bool
-contains_error_p (tree t)
-{
- return walk_tree (&t, find_error, NULL, NULL);
-}
-
-/* Parse a standard C++20 contract attribute specifier.
-
- contract-attribute-specifier:
- [ [ assert contract-level [opt] : conditional-expression ] ]
- [ [ pre contract-level [opt] : conditional-expression ] ]
- [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
-
- For free functions, we cannot determine the type of the postcondition
- identifier because the we haven't called grokdeclarator yet. In those
- cases we parse the postcondition as if the identifier was declared as
- 'auto <identifier>'. We then instantiate the postcondition once the
- return type is known.
-
- For member functions, contracts are in the complete-class context, so the
- parse is deferred. We also have the return type avaialable (unless it's
- deduced), so we don't need to parse the postcondition in terms of a
- placeholder. */
-
-static tree
-cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
-{
- gcc_assert (contract_attribute_p (attribute));
- cp_token *token = cp_lexer_consume_token (parser->lexer);
- location_t loc = token->location;
-
- bool assertion_p = is_attribute_p ("assert", attribute);
- bool postcondition_p = is_attribute_p ("post", attribute);
-
- /* Parse the optional mode. */
- tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
-
- /* Check for postcondition identifiers. */
- cp_expr identifier;
- if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
- identifier = cp_parser_identifier (parser);
- if (identifier == error_mark_node)
- return error_mark_node;
-
- cp_parser_require (parser, CPP_COLON, RT_COLON);
-
- /* Defer the parsing of pre/post contracts inside class definitions. */
- tree contract;
- if (!assertion_p &&
- current_class_type &&
- TYPE_BEING_DEFINED (current_class_type))
- {
- /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
- that doesn't close the attribute, return an error and let the attribute
- handling code emit an error for missing ']]'. */
- cp_token *first = cp_lexer_peek_token (parser->lexer);
- cp_parser_skip_to_closing_parenthesis_1 (parser,
- /*recovering=*/false,
- CPP_CLOSE_SQUARE,
- /*consume_paren=*/false);
- if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
- || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
- return error_mark_node;
- cp_token *last = cp_lexer_peek_token (parser->lexer);
-
- /* Build a deferred-parse node. */
- tree condition = make_node (DEFERRED_PARSE);
- DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
- DEFPARSE_INSTANTIATIONS (condition) = NULL;
-
- /* And its corresponding contract. */
- contract = grok_contract (attribute, mode, identifier, condition, loc);
- }
- else
- {
- /* Enable location wrappers when parsing contracts. */
- auto suppression = make_temp_override (suppress_location_wrappers, 0);
-
- /* Build a fake variable for the result identifier. */
- tree result = NULL_TREE;
- if (identifier)
- {
- begin_scope (sk_block, NULL_TREE);
- result = make_postcondition_variable (identifier);
- ++processing_template_decl;
- }
-
- /* Parse the condition, ensuring that parameters or the return variable
- aren't flagged for use outside the body of a function. */
- ++processing_contract_condition;
- cp_expr condition = cp_parser_conditional_expression (parser);
- --processing_contract_condition;
-
- /* Try to recover from errors by scanning up to the end of the
- attribute. Sometimes we get partially parsed expressions, so
- we need to search the condition for errors. */
- if (contains_error_p (condition))
- cp_parser_skip_up_to_closing_square_bracket (parser);
-
- /* Build the contract. */
- contract = grok_contract (attribute, mode, result, condition, loc);
-
- /* Leave our temporary scope for the postcondition result. */
- if (result)
- {
- --processing_template_decl;
- pop_bindings_and_leave_scope ();
- }
- }
-
- if (!flag_contracts)
- {
- error_at (loc, "contracts are only available with %<-fcontracts%>");
- return error_mark_node;
- }
-
- return finish_contract_attribute (attribute, contract);
-}
-
-/* Parse a contract condition for a deferred contract. */
-
-void cp_parser_late_contract_condition (cp_parser *parser,
- tree fn,
- tree attribute)
-{
- tree contract = TREE_VALUE (TREE_VALUE (attribute));
-
- /* Make sure we've gotten something that hasn't been parsed yet or that
- we're not parsing an invalid contract. */
- tree condition = CONTRACT_CONDITION (contract);
- if (TREE_CODE (condition) != DEFERRED_PARSE)
- return;
-
- tree identifier = NULL_TREE;
- if (TREE_CODE (contract) == POSTCONDITION_STMT)
- identifier = POSTCONDITION_IDENTIFIER (contract);
-
- /* Build a fake variable for the result identifier. */
- tree result = NULL_TREE;
- if (identifier)
- {
- /* TODO: Can we guarantee that the identifier has a location? */
- location_t loc = cp_expr_location (contract);
- tree type = TREE_TYPE (TREE_TYPE (fn));
- if (!check_postcondition_result (fn, type, loc))
- {
- invalidate_contract (contract);
- return;
- }
-
- begin_scope (sk_block, NULL_TREE);
- result = make_postcondition_variable (identifier, type);
- ++processing_template_decl;
- }
-
- /* 'this' is not allowed in preconditions of constructors or in postconditions
- of destructors. Note that the previous value of this variable is
- established by the calling function, so we need to save it here. */
- tree saved_ccr = current_class_ref;
- tree saved_ccp = current_class_ptr;
- if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
- (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
- {
- current_class_ref = current_class_ptr = NULL_TREE;
- parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
- }
-
- push_unparsed_function_queues (parser);
-
- /* Push the saved tokens onto the parser's lexer stack. */
- cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
- cp_parser_push_lexer_for_tokens (parser, tokens);
-
- /* Parse the condition, ensuring that parameters or the return variable
- aren't flagged for use outside the body of a function. */
- ++processing_contract_condition;
- condition = cp_parser_conditional_expression (parser);
- --processing_contract_condition;
-
- /* Revert to the main lexer. */
- cp_parser_pop_lexer (parser);
-
- /* Restore the queue. */
- pop_unparsed_function_queues (parser);
-
- current_class_ref = saved_ccr;
- current_class_ptr = saved_ccp;
-
- /* Commit to changes. */
- update_late_contract (contract, result, condition);
-
- /* Leave our temporary scope for the postcondition result. */
- if (result)
- {
- --processing_template_decl;
- pop_bindings_and_leave_scope ();
- }
-}
-
/* Parse a C++-26 annotation list.
annotation-list:
attribute-specifier:
[ [ attribute-using-prefix [opt] attribute-list ] ]
[ [ annotation-list ]]
- contract-attribute-specifier
alignment-specifier
attribute-using-prefix:
alignas ( type-id ... [opt] )
alignas ( alignment-expression ... [opt] ).
- Extensions for contracts:
-
- contract-attribute-specifier:
- [ [ assert : contract-mode [opt] : conditional-expression ] ]
- [ [ pre : contract-mode [opt] : conditional-expression ] ]
- [ [ post : contract-mode [opt] identifier [opt] :
- conditional-expression ] ]
-
Return void_list_node if the current token doesn't start an
attribute-specifier to differentiate from NULL_TREE returned e.g.
for [ [ ] ]. */
attr_name = canonicalize_attr_name (attr_name);
}
- /* Handle contract-attribute-specs specially. */
- if (attr_name && contract_attribute_p (attr_name))
- {
- tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
- if (attrs != error_mark_node)
- attributes = attrs;
- goto finish_attrs;
- }
-
if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
{
token = cp_lexer_peek_nth_token (parser->lexer, 2);
attributes = cp_parser_std_attribute_list (parser, attr_ns);
- finish_attrs:
if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
|| !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
cp_parser_skip_to_end_of_statement (parser);
vec_safe_push (unparsed_noexcepts, entry);
}
- /* Contracts are deferred. */
- for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
- if (cxx_contract_attribute_p (attr))
- {
- vec_safe_push (unparsed_contracts, decl);
- break;
- }
}
/* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
#include "builtins.h"
#include "omp-general.h"
#include "pretty-print-markup.h"
-#include "contracts.h"
/* The type of functions taking a tree, and some additional data, and
returning an int. */
}
decl = register_specialization (tmpl, gen_tmpl, targs,
is_friend, 0);
- remove_contract_attributes (result);
return decl;
}
is_friend, 0);
}
- /* If this is a specialization, splice any contracts that may have
- been inherited from the template, removing them. */
- if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl))
- remove_contract_attributes (decl);
-
/* A 'structor should already have clones. */
gcc_assert (decl == error_mark_node
|| variable_template_p (tmpl)
static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
tsubst_flags_t, tree);
-/* Instantiate the contract statement. */
-
-static tree
-tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
- tree in_decl)
-{
- tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
- bool auto_p = type_uses_auto (type);
-
- tree r = copy_node (t);
-
- /* Rebuild the result variable. */
- if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
- {
- tree oldvar = POSTCONDITION_IDENTIFIER (t);
-
- tree newvar = copy_node (oldvar);
- TREE_TYPE (newvar) = type;
- DECL_CONTEXT (newvar) = decl;
- POSTCONDITION_IDENTIFIER (r) = newvar;
-
- /* Make sure the postcondition is valid. */
- location_t loc = DECL_SOURCE_LOCATION (oldvar);
- if (!auto_p)
- if (!check_postcondition_result (decl, type, loc))
- return invalidate_contract (r);
-
- /* Make the variable available for lookup. */
- register_local_specialization (newvar, oldvar);
- }
-
- /* Instantiate the condition. If the return type is undeduced, process
- the expression as if inside a template to avoid spurious type errors. */
- if (auto_p)
- ++processing_template_decl;
- ++processing_contract_condition;
- CONTRACT_CONDITION (r)
- = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl);
- --processing_contract_condition;
- if (auto_p)
- --processing_template_decl;
-
- /* And the comment. */
- CONTRACT_COMMENT (r)
- = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl);
-
- return r;
-}
-
-/* Update T by instantiating its contract attribute. */
-
-static void
-tsubst_contract_attribute (tree decl, tree t, tree args,
- tsubst_flags_t complain, tree in_decl)
-{
- /* For non-specializations, adjust the current declaration to the most general
- version of in_decl. Because we defer the instantiation of contracts as long
- as possible, they are still written in terms of the parameters (and return
- type) of the most general template. */
- tree tmpl = DECL_TI_TEMPLATE (in_decl);
- if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
- in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
- local_specialization_stack specs (lss_copy);
- register_parameter_specializations (in_decl, decl);
-
- /* Get the contract to be instantiated. */
- tree contract = CONTRACT_STATEMENT (t);
-
- /* Use the complete set of template arguments for instantiation. The
- contract may not have been instantiated and still refer to outer levels
- of template parameters. */
- args = DECL_TI_ARGS (decl);
-
- /* For member functions, make this available for semantic analysis. */
- tree save_ccp = current_class_ptr;
- tree save_ccr = current_class_ref;
- if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
- {
- tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
- tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
- inject_this_parameter (this_type, cp_type_quals (this_type));
- }
-
- contract = tsubst_contract (decl, contract, args, complain, in_decl);
-
- current_class_ptr = save_ccp;
- current_class_ref = save_ccr;
-
- /* Rebuild the attribute. */
- TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
-}
-
-/* Rebuild the attribute list for DECL, substituting into contracts
- as needed. */
-
-void
-tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl)
-{
- tree list = copy_list (DECL_ATTRIBUTES (decl));
- for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr))
- {
- if (cxx_contract_attribute_p (attr))
- tsubst_contract_attribute (decl, attr, args, complain, in_decl);
- }
- DECL_ATTRIBUTES (decl) = list;
-}
-
/* Instantiate a single dependent attribute T (a TREE_LIST), and return either
T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
{
gcc_assert (ATTR_IS_DEPENDENT (t));
- /* Note that contract attributes are never substituted from this function.
- Their instantiation is triggered by regenerate_from_template_decl when
- we instantiate the body of the function. */
-
tree val = TREE_VALUE (t);
if (val == NULL_TREE)
/* Nothing to do. */;
finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
break;
- case PRECONDITION_STMT:
- case POSTCONDITION_STMT:
- gcc_unreachable ();
-
- case ASSERTION_STMT:
- {
- r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
- if (r != error_mark_node)
- add_stmt (r);
- RETURN (r);
- }
- break;
-
case DECL_EXPR:
{
tree decl, pattern_decl;
error_at (loc, "call to %<__builtin_operator_new%> "
"does not select replaceable global "
"allocation function");
- else
+ else
error_at (loc, "call to %<__builtin_operator_delete%> "
"does not select replaceable global "
"deallocation function");
complain, adc_variable_type);
}
gcc_assert (cp_unevaluated_operand
- || processing_contract_condition
|| TREE_STATIC (r)
|| decl_constant_var_p (r)
|| seen_error ());
DECL_CONTEXT (t) = decl;
}
- if (DECL_CONTRACTS (decl))
- {
- /* If we're regenerating a specialization, the contracts will have
- been copied from the most general template. Replace those with
- the ones from the actual specialization. */
- tree tmpl = DECL_TI_TEMPLATE (decl);
- if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
- {
- remove_contract_attributes (decl);
- copy_contract_attributes (decl, code_pattern);
- }
-
- tsubst_contract_attributes (decl, args, tf_warning_or_error, code_pattern);
- }
-
/* Merge additional specifiers from the CODE_PATTERN. */
if (DECL_DECLARED_INLINE_P (code_pattern)
&& !DECL_DECLARED_INLINE_P (decl))
return 0;
}
- if (!DECL_HAS_CONTRACTS_P (basefn) && DECL_HAS_CONTRACTS_P (overrider))
- {
- auto_diagnostic_group d;
- error ("function with contracts %q+D overriding contractless function",
- overrider);
- inform (DECL_SOURCE_LOCATION (basefn),
- "overridden function is %qD", basefn);
- return 0;
- }
- else if (DECL_HAS_CONTRACTS_P (basefn) && !DECL_HAS_CONTRACTS_P (overrider))
- {
- /* We're inheriting basefn's contracts; create a copy of them but
- replace references to their parms to our parms. */
- inherit_base_contracts (overrider, basefn);
- }
- else if (DECL_HAS_CONTRACTS_P (basefn) && DECL_HAS_CONTRACTS_P (overrider))
- {
- /* We're in the process of completing the overrider's class, which means
- our conditions definitely are not parsed so simply chain on the
- basefn for later checking.
-
- Note that OVERRIDER's contracts will have been fully parsed at the
- point the deferred match is run. */
- defer_guarded_contract_match (overrider, basefn, DECL_CONTRACTS (basefn));
- }
-
if (DECL_FINAL_P (basefn))
{
auto_diagnostic_group d;
#include "predict.h"
#include "memmodel.h"
#include "gimplify.h"
-#include "contracts.h"
/* There routines provide a modular interface to perform many parsing
operations. They may therefore be used during actual parsing, or
{
if (!t)
return;
- if (TREE_CODE (t) != POSTCONDITION_STMT)
- protected_set_expr_location (t, loc);
+
+ protected_set_expr_location (t, loc);
+
/* Avoid locus differences for C++ cdtor calls depending on whether
cdtor_returns_this: a conversion to void is added to discard the return
value, and this conversion ends up carrying the location, and when it
if (current_function_decl
&& DECL_STATIC_FUNCTION_P (current_function_decl))
error ("invalid use of member %qD in static member function", decl);
- else if (current_function_decl
- && processing_contract_condition
- && DECL_CONSTRUCTOR_P (current_function_decl))
- error ("invalid use of member %qD in constructor %<pre%> contract", decl);
- else if (current_function_decl
- && processing_contract_condition
- && DECL_DESTRUCTOR_P (current_function_decl))
- error ("invalid use of member %qD in destructor %<post%> contract", decl);
else
error ("invalid use of non-static data member %qD", decl);
inform (DECL_SOURCE_LOCATION (decl), "declared here");
}
else if (fn && DECL_STATIC_FUNCTION_P (fn))
error ("%<this%> is unavailable for static member functions");
- else if (fn && processing_contract_condition && DECL_CONSTRUCTOR_P (fn))
- error ("invalid use of %<this%> before it is valid");
- else if (fn && processing_contract_condition && DECL_DESTRUCTOR_P (fn))
- error ("invalid use of %<this%> after it is valid");
else if (fn)
error ("invalid use of %<this%> in non-member function");
else
}
return error_mark_node;
}
- else if (processing_contract_condition && (TREE_CODE (decl) == PARM_DECL))
- /* Use of a parameter in a contract condition is fine. */
- return decl;
else
{
if (complain & tf_error)
&& DECL_CONTEXT (decl) == NULL_TREE
&& !CONSTRAINT_VAR_P (decl)
&& !cp_unevaluated_operand
- && !processing_contract_condition
&& !processing_omp_trait_property_expr)
{
*error_msg = G_("use of parameter outside function body");
TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
- maybe_update_postconditions (fco);
-
/* Apply the type to the result object. */
result = DECL_RESULT (fco);
static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
-static tree handle_contract_attribute (tree *, tree, tree, int, bool *);
static tree handle_no_dangling_attribute (tree *, tree, tree, int, bool *);
static tree handle_annotation_attribute (tree *, tree, tree, int, bool *);
return cp_tree_equal (t1, t2);
}
-bool comparing_override_contracts;
-
-/* In a component reference, return the innermost object of
- the postfix-expression. */
-
-static tree
-get_innermost_component (tree t)
-{
- gcc_assert (TREE_CODE (t) == COMPONENT_REF);
- while (TREE_CODE (t) == COMPONENT_REF)
- t = TREE_OPERAND (t, 0);
- return t;
-}
-
-/* Returns true if T is a possibly converted 'this' or '*this' expression. */
-
-static bool
-is_this_expression (tree t)
-{
- t = get_innermost_component (t);
- /* See through deferences and no-op conversions. */
- if (INDIRECT_REF_P (t))
- t = TREE_OPERAND (t, 0);
- if (TREE_CODE (t) == NOP_EXPR)
- t = TREE_OPERAND (t, 0);
- return is_this_parameter (t);
-}
-
-static bool
-comparing_this_references (tree t1, tree t2)
-{
- return is_this_expression (t1) && is_this_expression (t2);
-}
-
-static bool
-equivalent_member_references (tree t1, tree t2)
-{
- if (!comparing_this_references (t1, t2))
- return false;
- t1 = TREE_OPERAND (t1, 1);
- t2 = TREE_OPERAND (t2, 1);
- return t1 == t2;
-}
-
/* Return truthvalue of whether T1 is the same tree structure as T2.
Return 1 if they are the same. Return 0 if they are different. */
return false;
return true;
- case COMPONENT_REF:
- /* If we're comparing contract conditions of overrides, member references
- compare equal if they designate the same member. */
- if (comparing_override_contracts)
- return equivalent_member_references (t1, t2);
- break;
-
case REFLECT_EXPR:
if (!cp_tree_equal (REFLECT_EXPR_HANDLE (t1), REFLECT_EXPR_HANDLE (t2))
|| REFLECT_EXPR_KIND (t1) != REFLECT_EXPR_KIND (t2))
handle_carries_dependency_attribute, NULL },
{ "indeterminate", 0, 0, true, false, false, false,
handle_indeterminate_attribute, NULL },
- { "pre", 0, -1, false, false, false, false,
- handle_contract_attribute, NULL },
- { "post", 0, -1, false, false, false, false,
- handle_contract_attribute, NULL }
};
const scoped_attribute_specs std_attribute_table =
return NULL_TREE;
}
-/* Perform checking for contract attributes. */
-
-tree
-handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name),
- tree ARG_UNUSED (args), int ARG_UNUSED (flags),
- bool *ARG_UNUSED (no_add_attrs))
-{
- /* TODO: Is there any checking we could do here? */
- return NULL_TREE;
-}
-
/* Handle a "no_dangling" attribute; arguments as in
struct attribute_spec.handler. */
-fconcepts -fconcepts-diagnostics-depth=@var{n}
-fconstexpr-depth=@var{n} -fconstexpr-cache-depth=@var{n}
-fconstexpr-loop-limit=@var{n} -fconstexpr-ops-limit=@var{n}
--fcontracts -fcontract-assumption-mode=@r{[}on@r{|}off@r{]}
--fcontract-build-level=@r{[}off@r{|}default@r{|}audit@r{]}
--fcontract-continuation-mode=@r{[}on@r{|}off@r{]}
--fcontract-mode=@r{[}on@r{|}off@r{]}
--fcontract-role=@var{name}:@var{default},@var{audit},@var{axiom}
--fcontract-semantic=@r{[}default@r{|}audit@r{|}axiom@r{]}:@var{semantic}
--fcontract-strict-declarations=@r{[}on@r{|}off@r{]}
-fcoroutines -fdiagnostics-all-candidates
-fno-elide-constructors
-fno-enforce-eh-specs
evaluation might take too long.
The default is 33554432 (1<<25).
-@opindex fcontracts
-@opindex fno-contracts
-@item -fcontracts
-Enable experimental support for the C++ Contracts feature, as briefly
-added to and then removed from the C++20 working paper (N4820). The
-implementation also includes proposed enhancements from papers P1290,
-P1332, and P1429. This functionality is intended mostly for those
-interested in experimentation towards refining the feature to get it
-into shape for a future C++ standard.
-
-On violation of a checked contract, the violation handler is called.
-Users can replace the violation handler by defining
-@smallexample
-void
-handle_contract_violation (const std::experimental::contract_violation&);
-@end smallexample
-
-There are different sets of additional flags that can be used together
-to specify which contracts will be checked and how, for N4820
-contracts, P1332 contracts, or P1429 contracts; these sets cannot be
-used together.
-
-@table @gcctabopt
-@opindex fcontract-mode
-@item -fcontract-mode=@r{[}on@r{|}off@r{]}
-Control whether any contracts have any semantics at all. Defaults to on.
-
-@opindex fcontract-assumption-mode
-@item -fcontract-assumption-mode=@r{[}on@r{|}off@r{]}
-[N4820] Control whether contracts with level @samp{axiom}
-should have the assume semantic. Defaults to on.
-
-@opindex fcontract-build-level
-@item -fcontract-build-level=@r{[}off@r{|}default@r{|}audit@r{]}
-[N4820] Specify which level of contracts to generate checks
-for. Defaults to @samp{default}.
-
-@opindex fcontract-continuation-mode
-@item -fcontract-continuation-mode=@r{[}on@r{|}off@r{]}
-[N4820] Control whether to allow the program to continue executing
-after a contract violation. That is, do checked contracts have the
-@samp{maybe} semantic described below rather than the @samp{never}
-semantic. Defaults to off.
-
-@opindex fcontract-role
-@item -fcontract-role=@var{name}:@var{default},@var{audit},@var{axiom}
-[P1332] Specify the concrete semantics for each contract level
-of a particular contract role.
-
-@opindex fcontract-semantic
-@item -fcontract-semantic=@r{[}default@r{|}audit@r{|}axiom@r{]}:@var{semantic}
-[P1429] Specify the concrete semantic for a particular
-contract level.
-
-@opindex fcontract-strict-declarations
-@item -fcontract-strict-declarations=@r{[}on@r{|}off@r{]}
-Control whether to reject adding contracts to a function after its
-first declaration. Defaults to off.
-@end table
-
-The possible concrete semantics for that can be specified with
-@samp{-fcontract-role} or @samp{-fcontract-semantic} are:
-
-@table @code
-@item ignore
-This contract has no effect.
-
-@item assume
-This contract is treated like C++23 @code{[[assume]]}.
-
-@item check_never_continue
-@itemx never
-@itemx abort
-This contract is checked. If it fails, the violation handler is
-called. If the handler returns, @code{std::terminate} is called.
-
-@item check_maybe_continue
-@itemx maybe
-This contract is checked. If it fails, the violation handler is
-called. If the handler returns, execution continues normally.
-@end table
-
@opindex fcoroutines
@opindex fno-coroutines
@item -fcoroutines
+++ /dev/null
-CXXFLAGS=--std=c++17 -g
-
-default: assert_fail libhandle_contract_violation.so
-
-run: default
- LD_PRELOAD=./libhandle_contract_violation.so ./assert_fail
-
-./libhandle_contract_violation.so: ./handle_contract_violation.cpp
- ${CXX} ${CXXFLAGS} -shared -fPIC -o $@ $<
-
-clean:
- rm -fr ./libhandle_contract_violation.so ./assert_fail
-
+++ /dev/null
-build and install gcc to $prefix, then to see the raw backtrace info run:
-LD_LIBRARY_PATH=$prefix/lib64 CXX=$prefix/bin/g++ make run
-
-for a filtered view using addr2line, see ./prettytrace.sh:
-LD_LIBRARY_PATH=$prefix/lib64 CXX=$prefix/bin/g++ make run |& ./prettytrace.sh
-
-prettytrace.sh relies on addr2line and c++filt to lookup and demangle names,
-and misc coreutils
-
-example_out.txt has an example of the raw output while example_pretty.txt
-shows the corresponding prettified output
-
+++ /dev/null
-void fun1() {
- int x = 0;
- [[ assert: x < 0 ]];
-}
-namespace tns {
- void fun2() {
- fun1();
- }
-}
-template<typename T>
-void fun3(T a) {
- tns::fun2();
-}
-void fun4() {
- fun3(5);
-}
-int main(int, char**) {
- void (*fp)() = nullptr;
- fp = fun4;
- fp();
- return 0;
-}
-
+++ /dev/null
-LD_PRELOAD=./libhandle_contract_violation.so ./assert_fail
-contract violation: assert_fail.cpp:3: fun1::x < 0 is false [with contract level=default]
-violation occurs here:
-./assert_fail[0x4011ad]
-./assert_fail[0x4011e0]
-./assert_fail[0x401230]
-./assert_fail[0x4011f1]
-./assert_fail[0x401219]
-/usr/lib/libc.so.6(__libc_start_main+0xf3)[0x7f26e4fa9223]
-./assert_fail[0x4010be]
-[0x0]
-end of violation
+++ /dev/null
-LD_PRELOAD=./libhandle_contract_violation.so ./assert_fail
-contract violation: assert_fail.cpp:3: fun1::x < 0 is false [with contract level=default]
-violation occurs here:
- assert_fail.cpp:fun1():4
- assert_fail.cpp:tns::fun2():8
- assert_fail.cpp:void fun3<int>(int):13
- assert_fail.cpp:fun4():16
- assert_fail.cpp:main:21
+++ /dev/null
-#include <iostream>
-#include <contract>
-#include <execinfo.h>
-#include <unistd.h>
-
-static constexpr int MAX_BACKTRACE_DEPTH = 128;
-
-void handle_contract_violation(const std::contract_violation &violation) {
- size_t _backtraceSize{0};
- void *_backtrace[MAX_BACKTRACE_DEPTH]{};
-
- _backtraceSize = backtrace(_backtrace, MAX_BACKTRACE_DEPTH);
- if(_backtraceSize == MAX_BACKTRACE_DEPTH)
- std::cerr << "warning: backtrace may have been truncated" << std::endl;
-
- std::cerr << "contract violation: " << violation.file_name()
- << ":" << violation.line_number()
- << ": " << violation.comment() << " is false"
- << " [with contract level=" << violation.assertion_level() << "]" << std::endl
- << "violation occurs here:" << std::endl;
- // skip the stack frame of handle_contract_violation and
- // on_contract_violation to get to wherever the assert was
- backtrace_symbols_fd(_backtrace + 2, _backtraceSize - 1, STDERR_FILENO);
- std::cerr << "end of violation" << std::endl;
-}
-
+++ /dev/null
-#!/bin/bash
-
-inViolation="false"
-sed 's/^/:/' /dev/stdin | while read -r line; do
- line="$(echo "$line" | sed 's/^://')"
-
- if [[ "${inViolation}" == "false" ]]; then
- echo "$line"
- if [[ -n "$(echo "$line" | grep 'violation occurs here:')" ]]; then
- inViolation="true"
- fi
- continue
- fi
-
- if [[ -n "$(echo "$line" | grep 'end of violation')" ]]; then
- inViolation="false"
- continue
- fi
-
- addr="$(echo "$line" | sed -r 's/.*\[0x([a-f0-9]+)\]$/\1/')"
- bin="$(echo "$line" | sed -r 's/^([^([]*).*/\1/')"
- [[ -n "${bin}" ]] || continue
- t="$(addr2line -e "$bin" "$addr" -f)"
- file="$(echo "$t" | tail -1 | tr ':' '\n' | head -1)"
- file="$(echo "$file" | sed -r "s:^$(pwd)/?::")"
- line="$(echo "$t" | tail -1 | tr ':' '\n' | tail -1 | cut -d' ' -f1)"
- func="$(echo "$t" | head -1 | c++filt)"
- [[ $file != "??" ]] && echo " $file:$func:$line"
-done
-
+++ /dev/null
-// ensure that that preconditions can access public, protected, and private
-// members of the current and base classes
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-struct Base
-{
- int pub{-1};
-
- virtual int b()
- [[ pre: pub > 0 ]]
- [[ pre: pro > 0 ]]
- [[ pre: pri > 0 ]]
- {
- return pub * pro * pri;
- }
-
- protected:
- int pro{-1};
- int pri{-1};
-};
-
-struct Child : Base
-{
- int fun()
- [[ pre: pub > 0 ]]
- [[ pre: pro > 0 ]]
- [[ pre: pri > 0 ]]
- {
- return pub * pro;
- }
-};
-
-struct VChild : Base
-{
- int b()
- [[ pre: pub > 0 ]]
- [[ pre: pro > 0 ]]
- [[ pre: pri > 0 ]]
- {
- return pub * pro;
- }
-};
-
-template<typename B>
-struct TChild : B
-{
- int fun()
- [[ pre: B::pub > 0 ]]
- [[ pre: B::pro > 0 ]]
- [[ pre: B::pri > 0 ]]
- {
- return B::pub * B::pro;
- }
-};
-
-struct PubBase
-{
- int pub{-1};
- int pro{-1};
- int pri{-1};
-};
-
-struct PubChild : PubBase
-{
- int fun()
- [[ pre: pub > 0 ]]
- [[ pre: pro > 0 ]]
- [[ pre: pri > 0 ]]
- {
- return pub * pro;
- }
-};
-
-template<typename B>
-struct TPubChild : B
-{
- int fun()
- [[ pre: B::pub > 0 ]]
- [[ pre: B::pro > 0 ]]
- [[ pre: B::pri > 0 ]]
- {
- return B::pub * B::pro;
- }
-};
-
-int main()
-{
- Base base{};
- base.b();
-
- Child child{};
- child.fun();
-
- VChild vchild{};
- vchild.b();
-
- TChild<Base> tchild{};
- tchild.fun();
-
- PubChild pubchild{};
- pubchild.fun();
-
- TPubChild<PubBase> tpubchild;
- tpubchild.fun();
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function Base::b at .*.C:11: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Base::b at .*.C:12: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Base::b at .*.C:13: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Child::fun at .*.C:26: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Child::fun at .*.C:27: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Child::fun at .*.C:28: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function VChild::b at .*.C:37: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function VChild::b at .*.C:38: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function VChild::b at .*.C:39: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function TChild<Base>::fun at .*.C:49: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function TChild<Base>::fun at .*.C:50: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function TChild<Base>::fun at .*.C:51: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function PubChild::fun at .*.C:67: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function PubChild::fun at .*.C:68: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function PubChild::fun at .*.C:69: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function TPubChild<PubBase>::fun at .*.C:79: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function TPubChild<PubBase>::fun at .*.C:80: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function TPubChild<PubBase>::fun at .*.C:81: .*(\n|\r\n|\r)" }
-
+++ /dev/null
-// test that assumed contracts do instatiate templates
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-template<typename T>
-int f(T t)
-{
- return -t;
-}
-
-int dummy()
-{
- [[ assert assume: f(1.0) > 0 ]];
- return -1;
-}
-
-template<>
-int f(double t) // { dg-error "specialization of.*after instantiation" }
-{
- return -1.0;
-}
-
-int main()
-{
- dummy();
- f(1);
- f(1.0);
- return 0;
-}
-
+++ /dev/null
-// ensure that assert contracts can be turned into compile time assumptions
-// and that they can be used for optimization.
-//
-// Even though x == -1, the assert contract tells the compiler that it is
-// safe to assume the x <= 0 branch is never taken fun can be transformed into
-// just
-// printf("%d: test x>0\n", x);
-// return 0;
-// we ensure this by matching on the output and expecting a 0 return code from
-// main -- unlike contracts-ignore2 which expects a failing return code
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-role=default:never,assume,ignore -O1" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-#include <cstdio>
-
-int fun(int x) {
- [[assert audit: x > 0]];
- if(x <= 0)
- {
- printf("%d: test x<=0 opt out\n", x);
- return -1;
- }
- else
- {
- printf("%d: test x>0\n", x);
- return 0;
- }
-}
-
-int main(int, char**) {
- volatile int x = -1;
- return fun(x);
-}
-
-// { dg-output "-1: test x>0(\n|\r\n|\r)" }
+++ /dev/null
-// test that assumed contracts that reference undefined entities do not cause
-// a link failure
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int f(int t);
-
-int dummy()
-{
- [[ assert assume: f(1) > 0 ]];
- return -1;
-}
-
-int main()
-{
- dummy();
- return 0;
-}
-
+++ /dev/null
-// test that assumed constexpr contracts that reference undefined entities do
-// not cause constexpr eval failure
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-constexpr int f(int t); // { dg-warning "used but never defined" }
-
-constexpr int dummy()
-{
- [[ assert assume: f(1) > 0 ]];
- return -1;
-}
-
-int main()
-{
- constexpr int n = dummy();
- return 0;
-}
-
+++ /dev/null
-// test that assumed constexpr contracts that reference defined entities, or
-// undefined entities in unevaluated context, cause constexpr eval failure when
-// the predicate is constexpr false
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-constexpr int f(int t)
-{
- return -1;
-}
-
-constexpr int dummy()
-{
- [[ assert assume: f(1) > 0 ]];
- return -1;
-}
-
-constexpr int undef(int t);
-
-constexpr int dummy2()
-{
- [[ assert assume: sizeof(decltype(undef(1))) < 0 ]];
- return -1;
-}
-
-int main()
-{
- constexpr int n = dummy(); // { dg-message "in .constexpr. expansion" }
- // { dg-error "contract predicate" "" { target *-*-* } 14 }
- constexpr int m = dummy2(); // { dg-message "in .constexpr. expansion" }
- // { dg-error "contract predicate" "" { target *-*-* } 22 }
- return 0;
-}
-
+++ /dev/null
-// ensure that non-defined entities in assume contracts do not error
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-template<typename T>
-T id2(T n);
-
-int fun(int n)
- [[ pre assume: id2(n) > 0 ]]
- [[ pre: n > 0 ]]
-{
- return -n;
-}
-
-template<typename T>
-T tfun(T n)
- [[ pre assume: id2(n) > 0 ]]
- [[ pre: n > 0 ]]
-{
- return -n;
-}
-
-template<typename T>
-constexpr T id(T n); // { dg-warning "used but never defined" }
-
-template<typename T>
-constexpr T cfun(T n)
- [[ pre assume: id(n) > 0 ]]
- [[ pre: id(n) > 0 ]] // { dg-error "used before its definition" }
-{
- return -n;
-}
-
-template<typename T>
-constexpr T id3(T n)
-{
- return n;
-}
-
-template<typename T>
-constexpr T cfun2(T n)
- [[ pre assume: id3(n) > 0 ]] // { dg-error "contract predicate" }
-{
- return -n;
-}
-
-template<typename T>
-constexpr T cfun3(T n)
- [[ pre: id3(n) > 0 ]] // { dg-error "contract predicate" }
-{
- return -n;
-}
-
-int main() {
- constexpr int n = cfun(-5);
- constexpr int n2 = cfun2(-5);
- constexpr int n3 = cfun3(-5);
- fun(-5);
- tfun(-5);
-}
-
+++ /dev/null
-// Contract condition functions should be local symbols in a comdat group with
-// the guarded function.
-
-// { dg-do compile { target { c++20 && comdat_group } } }
-// { dg-additional-options -fcontracts }
-// { dg-final { scan-assembler-not "_Z1fi.pre,comdat" } }
-// { dg-final { scan-assembler-not {(weak|globl)[^\n]*_Z1fi.pre} } }
-
-inline int f(int i)
- [[ pre: i > 0 ]]
-{
- return i;
-}
-
-int main()
-{
- if (f(42) != 42)
- __builtin_abort ();
-}
+++ /dev/null
-// Small test to ensure that the level and role information printed by various
-// contract configurations is correct.
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-role=default:maybe,maybe,ignore" }
-
-int fun(int n)
- [[ post default r: r > 0 ]]
-{
- return -n;
-}
-
-int main(int, char **)
-{
- [[ assert default: false ]];
- [[ assert: false ]];
- [[ assert audit: false ]];
- [[ assert default %new_role: false ]];
- [[ assert %new_role: false ]];
- [[ assert audit %new_role: false ]];
- [[ assert check_maybe_continue: false ]];
- [[ assert %default: false ]];
- [[ assert audit %default: false ]];
- fun(5);
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output {contract violation in function main at .*:14: false(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:15: false(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:16: false(\n|\r\n|\r)} }
-// { dg-output {\[level:audit, continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:17: false(\n|\r\n|\r)} }
-// { dg-output {\[role:new_role, continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:18: false(\n|\r\n|\r)} }
-// { dg-output {\[role:new_role, continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:19: false(\n|\r\n|\r)} }
-// { dg-output {\[level:audit, role:new_role, continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:20: false(\n|\r\n|\r)} }
-// { dg-output {\[level:, role:, continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:21: false(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:22: false(\n|\r\n|\r)} }
-// { dg-output {\[level:audit, continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function fun at .*:7: r > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
+++ /dev/null
-// ensure that passing pre/post do not affect constexpr functions
-// ensure that failing pre/post generate an error at runtime in constexpr funcs
-// { dg-do run }
-// { dg-options "-std=c++20 -fcontracts -fcontract-continuation-mode=on" }
-
-constexpr int wfun(int a)
- [[ pre: a > 0 ]]
- [[ post r: r > 0 ]]
-{
- return a;
-}
-
-constexpr int ffun(int a)
- [[ pre: a > 0 ]]
- [[ post r: r > 0 ]]
-{
- return a;
-}
-
-template<typename T>
-constexpr int tfun(T a)
- [[ pre: a > 0 ]]
- [[ post r: r > 0 ]]
-{
- return a;
-}
-
-template<typename T>
-constexpr int wtfun(T a)
- [[ pre: a > 0 ]]
- [[ post r: r > 0 ]]
-{
- return a;
-}
-
-template<typename T>
-constexpr int ftfun(T a)
- [[ pre: a > 0 ]]
- [[ post r: r > 0 ]]
-{
- return a;
-}
-
-constexpr int explicitfn(int a)
- [[ pre ignore: a > 0 ]]
- [[ pre check_maybe_continue: a > 0 ]]
- [[ post ignore r: r > 0 ]]
- [[ post check_maybe_continue r: r > 0 ]]
-{
- return a;
-}
-
-int main(int, char **) {
- constexpr int a = wfun(10);
- int b = ffun(-10);
- constexpr int c = wtfun(10);
- int d = ftfun(-10);
-
- int e = explicitfn(-10);
-
- int z = ftfun(-10.0);
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function ffun at .*.C:14: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ffun at .*.C:15: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ftfun<int> at .*.C:38: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ftfun<int> at .*.C:39: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function explicitfn at .*.C:46: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function explicitfn at .*.C:48: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ftfun<double> at .*.C:38: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ftfun<double> at .*.C:39: .*(\n|\r\n|\r)" }
-
+++ /dev/null
-// ensure that failing pre/post can fail at constexpr time
-// { dg-do compile }
-// { dg-options "-std=c++20 -fcontracts -fcontract-continuation-mode=on" }
-
-constexpr int ffun(int a)
- [[ pre: a > 0 ]]
- [[ post r: r > 10 ]]
-{
- return a;
-}
-
-template<typename T>
-constexpr int ftfun(T a)
- [[ pre: a > 0 ]]
- [[ post r: r > 10 ]]
-{
- return a;
-}
-
-constexpr int explicitfn(int a)
- [[ pre ignore: a > 0 ]]
- [[ pre check_maybe_continue: a > 0 ]]
- [[ post ignore r: r > 10 ]]
- [[ post check_maybe_continue r: r > 10 ]]
-{
- return a;
-}
-
-template<typename T>
-constexpr int ftfun2(T a)
- [[ pre: a > 0 ]]
- [[ post r: r > 10 ]]
-{
- return a;
-}
-
-int main(int, char **) {
- constexpr int a = ffun(-10);
- // { dg-error "contract predicate" "" { target *-*-* } 6 }
- constexpr int b = ftfun(-10);
- // { dg-error "contract predicate" "" { target *-*-* } 14 }
- constexpr int c = explicitfn(-10);
- // { dg-error "contract predicate" "" { target *-*-* } 22 }
- constexpr int d = ftfun2(-10.0);
- // { dg-error "contract predicate" "" { target *-*-* } 31 }
-
- constexpr int e = ffun(5);
- // { dg-error "contract predicate" "" { target *-*-* } 7 }
- constexpr int f = ftfun(5);
- // { dg-error "contract predicate" "" { target *-*-* } 15 }
- constexpr int g = explicitfn(5);
- // { dg-error "contract predicate" "" { target *-*-* } 24 }
- constexpr int h = ftfun2(5.5);
- // { dg-error "contract predicate" "" { target *-*-* } 32 }
-
- return 0;
-}
-
+++ /dev/null
-// An assumed contract shouldn't break constant evaluation.
-
-// { dg-do compile { target c++20 } }
-// { dg-additional-options -fcontracts }
-
-bool b;
-
-constexpr int f() [[ pre assume: b ]] { return 42; }
-
-static_assert (f() > 0);
+++ /dev/null
-// Test to ensure that diagnostic location for condition conversion is in the
-// right place.
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-
-template<typename T>
-void fn()
- [[ pre: T{} ]] // { dg-error "no match" }
-{
-}
-
-struct Z { };
-
-int main(int, char**) {
- fn<int>();
- fn<Z>();
- return 0;
-}
+++ /dev/null
-// Tests to ensure that contracts are properly emitted for constructors,
-// destructors, and their intersection with templates.
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-bool pre_{false}, post_{false};
-bool delegate_{false};
-
-struct S
-{
- S() [[ pre: pre_ ]] [[ post: post_ ]];
- ~S() [[ pre: pre_ ]] [[ post: post_ ]];
-};
-
-S::S() { return; }
-S::~S() { return; }
-
-struct SInline
-{
- SInline() [[ pre: pre_ ]] [[ post: post_ ]] { return; }
- ~SInline() [[ pre: pre_ ]] [[ post: post_ ]] { return; }
-};
-
-struct SDelegate0
-{
- SDelegate0(int) [[ pre: pre_ ]] [[ post: post_ ]] { return; }
- SDelegate0() : SDelegate0(0) { return; }
- ~SDelegate0() [[ pre: pre_ ]] [[ post: post_ ]] { return; }
-};
-
-struct SDelegate1
-{
- SDelegate1(int) { return; }
- SDelegate1() [[ pre: pre_ ]] [[ post: post_ ]] : SDelegate1(0) { return; }
- ~SDelegate1() [[ pre: pre_ ]] [[ post: post_ ]] { return; }
-};
-
-struct SDelegate2
-{
- SDelegate2(int) [[ pre: pre_ ]] [[ post: post_ ]] { return; }
- SDelegate2() [[ pre: pre_ && delegate_ ]] [[ post: post_ && delegate_ ]] : SDelegate2(0) { return; }
- ~SDelegate2() [[ pre: pre_ ]] [[ post: post_ ]] { return; }
-};
-
-struct SDelegate3
-{
- SDelegate3(int) [[ pre: pre_ ]] [[ post: post_ ]] { return; }
- SDelegate3() [[ pre: pre_ && delegate_ ]] [[ post: post_ && delegate_ ]] : SDelegate3(0) { return; }
- ~SDelegate3() [[ pre: pre_ ]] [[ post: post_ ]] { return; }
-};
-
-template<typename T>
-struct S1
-{
- S1() [[ pre: pre_ ]] [[ post: post_ ]] { }
-};
-
-struct S2
-{
- template<typename T>
- S2(T) [[ pre: pre_ ]] [[ post: post_ ]] { }
-};
-
-template<typename T>
-struct S3
-{
- template<typename S>
- S3(S) [[ pre: pre_ ]] [[ post: post_ ]] { }
-};
-
-struct G0
-{
- G0() [[ post: x > 0 ]] {}
- ~G0() [[ pre: x > 0 ]] {}
- int x{-1};
-};
-
-struct G1
-{
- G1() [[ post: this->x > 0 ]] {}
- ~G1() [[ pre: this->x > 0 ]] {}
- int x{-1};
-};
-
-int x{-1};
-
-struct G2
-{
- G2() [[ pre: ::x > 0 ]] {}
- ~G2() [[ post: ::x > 0 ]] {}
- int x{1};
-};
-
-void test0()
-{
- S s;
- SInline si;
- SDelegate0 sd0;
- SDelegate1 sd1;
- SDelegate2 sd2;
- SDelegate3 sd3;
- S1<int> s1_i;
- S1<double> s1_d;
- S2 s2_i{1};
- S2 s2_d{.1};
- S3<int> s3_i_i{1};
- S3<int> s3_i_d{.1};
- S3<double> s3_d_i{1};
- S3<double> s3_d_d{.1};
-}
-
-void test1()
-{
- G0 g0;
- G1 g1;
- G2 g2;
-}
-
-int main(int, char**)
-{
- test0();
- test1();
- return 0;
-};
-
-// test0
-// { dg-output "contract violation in function S::S at .*.C:11 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S::S at .*.C:11 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SInline::SInline at .*.C:20 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SInline::SInline at .*.C:20 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate0::SDelegate0 at .*.C:26 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate0::SDelegate0 at .*.C:26 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate1::SDelegate1 at .*.C:34 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate1::SDelegate1 at .*.C:34 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate2::SDelegate2 at .*.C:41 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate2::SDelegate2 at .*.C:40 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate2::SDelegate2 at .*.C:40 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate2::SDelegate2 at .*.C:41 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate3::SDelegate3 at .*.C:48 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate3::SDelegate3 at .*.C:47 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate3::SDelegate3 at .*.C:47 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate3::SDelegate3 at .*.C:48 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S1<int>::S1 at .*.C:55 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S1<int>::S1 at .*.C:55 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S1<double>::S1 at .*.C:55 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S1<double>::S1 at .*.C:55 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S2::S2<int> at .*.C:61 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S2::S2<int> at .*.C:61 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S2::S2<double> at .*.C:61 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S2::S2<double> at .*.C:61 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S3<int>::S3<int> at .*.C:68 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S3<int>::S3<int> at .*.C:68 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S3<int>::S3<double> at .*.C:68 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S3<int>::S3<double> at .*.C:68 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S3<double>::S3<int> at .*.C:68 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S3<double>::S3<int> at .*.C:68 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S3<double>::S3<double> at .*.C:68 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S3<double>::S3<double> at .*.C:68 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate3::~SDelegate3 at .*.C:49 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate3::~SDelegate3 at .*.C:49 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate2::~SDelegate2 at .*.C:42 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate2::~SDelegate2 at .*.C:42 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate1::~SDelegate1 at .*.C:35 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate1::~SDelegate1 at .*.C:35 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate0::~SDelegate0 at .*.C:28 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SDelegate0::~SDelegate0 at .*.C:28 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SInline::~SInline at .*.C:21 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function SInline::~SInline at .*.C:21 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S::~S at .*.C:12 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S::~S at .*.C:12 .*(\n|\r\n|\r)" }
-
-// test1
-// { dg-output "contract violation in function G0::G0 at .*.C:73 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function G1::G1 at .*.C:80 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function G1::~G1 at .*.C:81 .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function G0::~G0 at .*.C:74 .*(\n|\r\n|\r)" }
-
+++ /dev/null
-// Tests to ensure that an invalid this parm cannot be used in pre on ctors or
-// in post on dtors.
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-struct F0
-{
- F0() [[ pre: x > 0 ]]; // { dg-error "invalid use of member" }
- ~F0() [[ post: x > 0 ]]; // { dg-error "invalid use of member" }
- int x{-1};
-};
-
-struct F1
-{
- F1() [[ pre: this->x > 0 ]]; // { dg-error "may not be used" }
- ~F1() [[ post: this->x > 0 ]]; // { dg-error "may not be used" }
- int x{-1};
-};
-
-struct F2
-{
- F2()
- [[ post ret: false ]] // { dg-error "does not return a value" }
- {
- }
- ~F2()
- [[ post r: false ]] // { dg-error "does not return a value" }
- {
- }
- void f()
- [[ post r: false ]] // { dg-error "does not return a value" }
- {
- }
-};
-
+++ /dev/null
-// Tests to ensure that contracts have a properly cv qualified this
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-struct S
-{
- int g() const { return x_; }
- int f() { return x_; }
-
- void mem_c() const
- [[ pre: f() ]] // { dg-error "discards qualifiers" }
- {
- }
- void mem_nc()
- [[ pre: f() ]]
- {
- }
-
- void memc_c() const
- [[ pre: g() ]]
- {
- }
- void memc_nc()
- [[ pre: g() ]]
- {
- }
-
- private:
- int x_{-10};
-};
-
-int main(int, char**)
-{
- S s;
- return 0;
-};
-
+++ /dev/null
-// Tests to ensure that deduced return types work with postconditions using
-// the return value on defining declarations.
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts --param ggc-min-heapsize=0 --param ggc-min-expand=0" }
-
-auto undeduced(int z)
-{
- if (!(sizeof(decltype(undeduced(5))) > 4)) // { dg-error "before deduction" }
- return 5;
- return 4;
-}
-
-// defining declaration, fine
-auto g0(int a) [[ pre: a < 0 ]] [[ post r: r > 0 ]]
-{
- return -a * 1.2;
-}
-
-// non defining post using nondeduced identifier, fine
-int g1(int m) [[ post r: r == m ]];
-
-int g1(int n) [[ post s: s == n ]]
-{
- return -n;
-}
-
-int g2(int z)
- [[ pre: sizeof(decltype(g2(5))) > 4 ]]; // { dg-error "not declared" }
-
-int g3(int z)
- [[ pre: sizeof(decltype(g2(5))) > 4 ]]
-{
- return -z;
-}
-
-// deduced that doesn't use return, good
-auto g4(int m) [[ post: m ]];
-
-auto g4(int m) [[ post: m ]]
-{
- return -m;
-}
-
-auto g5(int m) [[ pre: m ]];
-
-auto g5(int m) [[ pre: m ]]
-{
- return -m;
-}
-
-template<typename T>
-auto g6(T t) [[ post r: r == t ]];
-
-template<typename S>
-auto g6(S s) [[ post q: q == s ]]
-{
- return s;
-}
-
-template<typename T>
-T g7(T t) [[ post r: r == t ]];
-
-template<typename S>
-S g7(S s) [[ post q: q == s ]]
-{
- return s;
-}
-
-template<typename T>
-auto g8(T t) [[ post r: r == t && sizeof(decltype(::g8(t))) > 2 ]]; // { dg-error "not been declared" }
-
-// This failure is related to the fact that we've invalidated the previous
-// contract.
-template<typename S>
-auto g8(S s) [[ post q: q == s && sizeof(decltype(::g8(s))) > 2 ]] // { dg-error "mismatched" }
-{
- return s;
-}
-
-// non defining pre, bad
-auto f0(int z)
- [[ pre: sizeof(decltype(f0(5))) > 4 ]]; // { dg-error "not declared" }
-
-// defining pre, still ill formed
-auto f1(int z)
- [[ pre: sizeof(decltype(f1(5))) > 4 ]] // { dg-error "not declared" }
-{
- return '5';
-}
-
-// undeduced using postcon, OK
-auto f2(int m) [[ post r: r == m ]];
-
-auto f2(int n) [[ post s: s == n ]]
-{
- return n;
-}
-
-template<typename T>
-void f3(T t) [[ post r: false ]] // { dg-error "function does not return a value" }
-{
-}
-
-int main(int, char**)
-{
- return 0;
-}
-
+++ /dev/null
-// check that contracts work around deduced return types
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-auto g0(int a) [[ pre: a < 0 ]] [[ post r: r > 0 ]]
-{
- return -a * 1.2;
-}
-
-int g1(int m) [[ post r: r == m ]];
-
-int g1(int n) [[ post s: s == n ]]
-{
- return -n;
-}
-
-int g2(int z)
-{
- return -z;
-}
-
-int g3(int z)
- [[ pre: sizeof(decltype(g2(5))) > 4 ]]
-{
- return -z;
-}
-
-auto g4(int m) [[ post: m ]];
-
-auto g4(int m) [[ post: m ]]
-{
- return -m;
-}
-
-auto g5(int m) [[ pre: m ]];
-
-auto g5(int m) [[ pre: m ]]
-{
- return -m;
-}
-
-template<typename T>
-auto g6(T t) [[ post r: r == t ]];
-
-template<typename S>
-auto g6(S s) [[ post q: q == s ]]
-{
- return -s;
-}
-
-// template<typename T>
-// T g7(T t) [[ post r: r == t ]];
-
-template<typename S>
-S g7(S s) [[ post q: q == s ]]
-{
- return -s;
-}
-
-int main(int, char**) {
- g0(5);
- g1(6);
- g2(1);
- g3(1);
- g4(0);
- g5(0);
- g6(5);
- g6(5.5);
- g7(5);
- g7(6.6);
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function g0 at .*.C:5: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g0 at .*.C:5: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g1 at .*.C:12: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g3 at .*.C:23: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g4 at .*.C:30: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g5 at .*.C:37: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g6<int> at .*.C:46: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g6<double> at .*.C:46: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g7<int> at .*.C:55: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function g7<double> at .*.C:55: .*(\n|\r\n|\r)" }
-
+++ /dev/null
-// PR c++/108542
-// { dg-additional-options -fcontracts }
-// { dg-do compile { target c++11 } }
-
-template<typename T>
-void f (T n) {}
-void g() [[pre: f]]; // { dg-error "overloaded" }
+++ /dev/null
-// simple check to ensure we don't emit a function with the same name twice,
-// when wrapping functions in pre- and postconditions.
-// { dg-do link }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-volatile int x = 10;
-
-extern "C" void
-f ()
- [[ pre: x < 10 ]]
-{
-}
-
-int
-main ()
- [[ post: x > 10 ]]
-{
- f();
-}
+++ /dev/null
-// ensure contracts on friend declarations are a complete class context
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-struct X {
- friend void fn0(X x) [[ pre: x.a > 0 ]] { }
-
- friend void fn2(X x);
- static void fns0(X x) [[ pre: x.a > 0 ]] { }
- static void fns1(X x) [[ pre: x.a > 0 ]];
- static void fns2(X x);
-
- friend void fn(X &x) { x.a = -5; }
-
-private:
- int a{10};
-};
-
-void fn2(X x) [[ pre: x.a > 0 ]] { }
-void X::fns1(X x) { }
-void X::fns2(X x) [[ pre: x.a > 0 ]] { }
-
-int main(int, char**) {
- X x;
- fn(x); // no contract
-
- fn0(x);
- fn2(x);
-
- X::fns0(x);
- X::fns1(x);
- X::fns2(x);
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function fn0 at .*.C:6: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function fn2 at .*.C:19: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function X::fns0 at .*.C:9: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function X::fns1 at .*.C:10: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function X::fns2 at .*.C:21: .*(\n|\r\n|\r)" }
+++ /dev/null
-// { dg-do compile }
-
-#ifdef __cpp_contracts
-static_assert (false);
-#endif
-
-#ifdef __cpp_contracts_literal_semantics
-static_assert (false);
-#endif
-
-#ifdef __cpp_contracts_roles
-static_assert (false);
-#endif
-
+++ /dev/null
-// test that ignored contracts do instatiate templates
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-template<typename T>
-int f(T t)
-{
- return -t;
-}
-
-int dummy()
-{
- [[ assert ignore: f(1.0) > 0 ]];
- return -1;
-}
-
-template<>
-int f(double t) // { dg-error "specialization of.*after instantiation" }
-{
- return -1.0;
-}
-
-int main()
-{
- dummy();
- f(1);
- f(1.0);
- return 0;
-}
-
+++ /dev/null
-// baseline for testing assert contracts being turned into compile time
-// assumptions; see contracts-assume2 for the assumed case
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-#include <cstdio>
-
-int fun(int x) {
- [[assert audit: x > 0]];
- if(x <= 0)
- {
- printf("%d: test x<=0 opt out\n", x);
- return -1;
- }
- else
- {
- printf("%d: test x>0\n", x);
- return 0;
- }
-}
-
-int main(int, char**) {
- volatile int x = -1;
- return fun(x);
-}
-
-// { dg-shouldfail "" }
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-struct Foo
-{
- int x;
- bool y;
- long z[4];
-};
-
-Foo foo() [[ pre: true ]]
-{
- return {};
-}
-
+++ /dev/null
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-int main(int, char **)
-{
- int x = 5;
- int y = 10;
- [[ assert:
- x
- <
- 10
- &&
- y
- >
- 123
- ]];
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function main at .*.C:8: x < 10 && y > 123.*(\n|\r\n|\r)" }
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-struct BaseA {
- virtual int fun(int n) [[ pre: n > 0 ]] { return -n; }
-};
-
-struct BaseB {
- virtual int fun(int n) [[ pre: n > 0 ]] { return -n; }
-};
-
-struct Child : public BaseA, BaseB {
- int fun(int n) [[ pre: n > 0 ]] { return -n; }
-};
-
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-struct BaseA {
- virtual int fun(int n) [[ pre: n > 0 ]] { return -n; }
-};
-
-struct BaseB {
- virtual int fun(int n) [[ pre: n < 0 ]] { return -n; }
-};
-
-struct Child1 : public BaseA, BaseB {
- int fun(int n) [[ pre: n > 0 ]] { return -n; } // { dg-error "mismatched" }
-};
-
-struct Child2 : public BaseA, BaseB {
- int fun(int n) [[ pre: n < 0 ]] { return -n; } // { dg-error "mismatched" }
-};
-
-struct Child3 : public BaseA, BaseB {
- int fun(int n) { return -n; }
-};
-
-struct Child4 : public BaseA {
- int fun(int n);
-};
-
-int Child4::fun(int n)
- [[ pre: n != 0 ]] // { dg-error "mismatched" }
-{
- return -n;
-}
-
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-void gfn3(int n) [[ pre: n > 0 ]];
-
-struct Outer {
- struct Inner {
- void fn(int n) [[ pre: n > 0 && bob > 1 ]];
- void fn2(int n) [[ pre: n > 0 && bob > 1 ]];
- };
-
- void fn(int m) [[ pre: m > 1 ]];
- friend void Inner::fn(int n) [[ pre: n > 0 && bob > 1 ]]; // { dg-error "not declared" }
-
- friend void gfn(int p) [[ pre: p > 0 ]];
- friend void gfn(int q) [[ pre: q > 1 ]]; // { dg-error "'q' was not declared" }
-
- // This should be okay.
- friend void gfn2(int q);
- friend void gfn2(int p) [[ pre: p > 0 ]] { }
-
- static int bob;
-};
-int Outer::bob{-1};
+++ /dev/null
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-void gfn3(int n) [[ pre: n > 0 ]];
-
-struct Outer {
- struct Inner {
- void fn(int n) [[ pre: n > 0 && bob > 1 ]];
- };
-
- void fn(int m) [[ pre: m > 1 ]];
-
- friend void gfn1(int q);
- friend void gfn1(int p) [[ pre: p > 0 ]] { }
-
- friend void gfn2(int q, Outer *);
- friend void gfn2(int p, Outer *) [[ pre: p > 0 ]] { }
-
- friend void gfn3(int n);
-
- static int bob;
-};
-int Outer::bob{-1};
-
-void Outer::Inner::fn(int x) { }
-void Outer::fn(int y) { }
-
-void gfn3(int n) { }
-void gfn1(int q);
-
-int main(int, char **) {
- Outer::Inner in;
- in.fn(-5);
- Outer out;
- out.fn(-6);
- gfn1(-7);
- gfn2(-8, &out);
- gfn3(-9);
-}
-
+++ /dev/null
-// Contracts shouldn't introduce more copies.
-
-// { dg-do compile { target c++20 } }
-// { dg-additional-options -fcontracts }
-
-struct A
-{
- int i;
- A(int i): i(i) { }
- A(const A&) = delete;
-};
-
-A f(A a)
- [[ pre: a.i > 0 ]]
- [[ post r: r.i > 0 ]]
-{
- return {a.i};
-}
-
-int main()
-{
- if (f({42}).i != 42)
- __builtin_abort ();
-}
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-struct Foo {
- virtual int f0(int n) [[ pre: false ]] { return n; }
- virtual int f1(int n) [[ pre: false ]] { return n; }
- virtual int f2(int n) [[ pre: false ]] { return n; }
- virtual int f3(int n) [[ pre: false ]] { return n; }
- virtual int f4(int n) [[ pre: false ]] { return n; }
- virtual int f5(int n) [[ pre: false ]] { return n; }
- virtual int f6(int n) [[ pre: false ]] { return n; }
- virtual int f7(int n) [[ pre: false ]] { return n; }
- virtual int f8(int n) [[ pre: false ]] { return n; }
- virtual int f9(int n) [[ pre: false ]] { return n; }
- virtual int f10(int n) [[ pre: false ]] { return n; }
- virtual int f11(int n) [[ pre: n > 0 ]] [[ pre: n > 1 ]] { return n; }
- virtual int f12(int n) [[ pre: n > 0 ]] [[ pre: n > 1 ]] { return n; }
-};
-
-struct Bar : Foo {
- [[ pre: n > -1 ]] int f0(int n = 0) override { return -n; } // { dg-error "contracts must appertain" }
- int [[ pre: n > -2 ]] f1(int n = 0) override { return -n; } // { dg-error "contracts must appertain" }
- int f2 [[ pre: n > -3 ]] (int n = 0) override { return -n; } // { dg-error "contracts must appertain" }
- int f4([[ pre: n > -4 ]] int n = 0) override { return -n; } // { dg-error "contracts must appertain" }
- int f5(int [[ pre: n > -5 ]] n = 0) override { return -n; } // { dg-error "contracts must appertain" }
- int f6(int n [[ pre: n > -6 ]] = 0) override { return -n; } // { dg-error "contracts must appertain" }
- int f7(int n = [[ pre: n > -7 ]] 0) override { return -n; }
- // { dg-error "expected identifier" "" { target *-*-* } .-1 }
- // { dg-error "expected .\{. before numeric" "" { target *-*-* } .-2 }
- // { dg-error "invalid user-defined conversion" "" { target *-*-* } .-3 }
- // { dg-error "expected .,." "" { target *-*-* } .-4 }
- int f8(int n = 0 [[ pre: n > -8 ]]) override { return -n; }
- // { dg-error "shall only introduce an attribute" "" { target *-*-* } .-1 }
- int f9(int n = 0) [[ pre: n > -9 ]] override { return -n; } // { dg-error "mismatched contract" }
-
- // The grammar doesn't appear to permit contracts after the virt-specifiers
- // but the parser will happily add these to an attribute list that is not
- // owned by the function declarator.
- int f10(int n = 0) override [[ pre: n > -10 ]] { return -n; } // { dg-error "contracts must appertain" }
- int f11(int n) [[ pre: n > 1 ]] override [[ pre: n > 0 ]] { return -n; } // { dg-error "contracts must appertain" }
- int f12(int n) [[ pre: n > 0 ]] override [[ pre: n > 1 ]] { return -n; } // { dg-error "contracts must appertain" }
-};
-
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-int f1(int n)
- [[pre: n >= 0]]
- [[post r: r >= 0]]
- [[post r: !(r < 0)]]
-{
- return n;
-}
-
-int f2(int n)
- [[post: true]]
-{
- return 0;
-}
-
-int f3(int n)
- [[post r: r >= n]]
-{
- return n + 1;
-}
-
-int f4(int n)
- [[post: x > 0]] // { dg-error "not declared" }
-{
- return 0;
-}
-
-void f5()
- [[post: true]]
-{ }
-
-void f6()
- [[post r: true]] // { dg-error "function does not return a value" }
-{ }
-
-int f7(int n)
- [[post: n > 0]]
-{
- return x; // { dg-error "not declared" }
-}
-
-void f8(int n)
- [[post: n > 0]]
-{
- return;
-}
-
-void f9(int n)
- [[post: n > 0]]
-{
- return n; // { dg-error "return-statement with a value" }
-}
-
-int f10(int n)
- [[post: n > 0]]
-{
- return; // { dg-error "return-statement with no value" }
-}
-
-void f11()
- [[post: true]]
-{
- constexpr int n = 0;
- return n; // { dg-error "return-statement with a value" }
-}
-
-int f12()
- [[post: true]]
-{
- return; // { dg-error "return-statement with no value" }
-}
-
+++ /dev/null
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int f1(int n)
- [[post r: r == n]]
-{
- return n;
-}
-
-int main()
-{
- f1(0);
-}
+++ /dev/null
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-// { dg-shouldfail "assert violation" }
-// { dg-output "contract violation in function f1" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int f1(int n)
- [[post r: r > n]]
-{
- return n;
-}
-
-int main()
-{
- f1(0);
-}
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-struct S
-{
- S() [[post: n == 0]]
- : n(0)
- { }
-
- ~S() [[post: true]]
- { }
-
- int f1()
- [[post r: n == r]]
- {
- return n;
- }
-
- int f2()
- [[post r: r == x]] // { dg-error "not declared" }
- {
- return n;
- }
-
- void f3()
- [[post r: n]] // { dg-error "does not return a value" }
- {
- }
-
- int n = 0;
-};
-
-int main()
-{
- // f1(0);
-}
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-// Tests for function templates
-
-template<typename T>
-T f1(T n)
- [[pre: n >= 0]]
- [[post r: r >= 0]]
- [[post r: !(r < 0)]]
-{
- return n;
-}
-
-
-void driver()
-{
- f1(0);
-}
\ No newline at end of file
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-// Test for class members
-
-template<typename T>
-struct S
-{
- S(T n)
- [[post: true]]
- : n(n)
- { }
-
- T f1(T n)
- [[pre: n >= 0]]
- [[post r: r >= 0]]
- [[post r: !(r < 0)]]
- {
- return n;
- }
-
- T n;
-};
-
-
-void driver()
-{
- S<int> s1(0);
- s1.f1(2);
-}
+++ /dev/null
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-/* { dg-skip-if "requires hosted libstdc++ for cwchar" { ! hostedlib } } */
-
-#include <experimental/contract>
-
-using std::experimental::contract_violation;
-void handle_contract_violation(const contract_violation &violation)
-{
- __builtin_exit (0);
-}
-
-struct A {
- int i;
- A(): i(42) {}
- A(const A&);
-};
-
-A f()
- [[ post r: r.i == 24 ]]
-{
- A a;
- return a;
-}
-
-int main()
-{
- f();
- return -1;
-}
+++ /dev/null
-// generic pre contract parsing checks
-// check omitted, 'default', 'audit', and 'axiom' contract levels parse
-// ensure that an invalid contrcat level 'off' errors
-// ensure that a predicate referencing an undefined variable errors
-// ensure that a missing colon after contract level errors
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-void f1(int x) [[ pre: x >= 0 ]] { }
-void f2(int x) [[ pre default: x >= 0 ]] { }
-void f3(int x) [[ pre audit: x >= 0 ]] { }
-void f4(int x) [[ pre axiom: x >= 0 ]] { }
-
-void finvalid(int x) [[ pre invalid: x >= 0 ]] { } // { dg-error "expected contract level" }
-void fundeclared() [[ pre: x >= 0 ]] { } // { dg-error ".x. was not declared in this scope" }
-void fmissingcolon(int x) [[ pre default x == 0]] { } // { dg-error "expected .:. before .x." }
-
-int Z;
-void (*fp1)(int x) [[ pre: Z > 0 ]]; // { dg-error "contracts must appertain" }
-void (*fp2 [[ pre: Z > 0 ]])(int x); // { dg-error "contracts must appertain" }
-typedef void (*fp3)(int x) [[ pre: Z > 0 ]]; // { dg-error "contracts must appertain" }
-typedef void (*fp4 [[ pre: Z > 0 ]])(int x); // { dg-error "contracts must appertain" }
-fp3 fn5(int a) [[ pre: a > 0 ]]; // { dg-bogus "contracts must appertain" }
-
-int xyz;
-[[ pre: xyz ]] struct Bar; // { dg-error "contracts must appertain" }
-// { dg-warning "attribute ignored" "" { target *-*-* } .-1 }
-struct [[ pre: xyz ]] Bar; // { dg-error "contracts must appertain" }
-struct Bar [[ pre: xyz ]]; // { dg-error "contracts must appertain" }
-struct Zoo {} x [[ pre: xyz ]]; // { dg-error "contracts must appertain" }
-
-void f6(int x) [[ pre: x > 0 ; // { dg-error "expected .]." }
-void f7(int x) [[ pre: x > 0 ]; // { dg-error "expected .]." }
-void f8(int x) [[ pre: x > 0 { }; // { dg-error "expected .]." }
-void f9(int x) [[ pre: x > 0 ] { }; // { dg-error "expected .]." }
-
+++ /dev/null
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-struct M
-{
- template<typename T>
- int f(int a) [[ pre: a > 0 ]];
-
- template<typename T>
- int g(int a) [[ pre: a > 0 ]]
- {
- return -a;
- }
-
- template<typename T>
- int f_arg(T a) [[ pre: a > 0 ]];
-
- template<typename T>
- int g_arg(T a) [[ pre: a > 0 ]]
- {
- return (int)-a;
- }
-
- template<typename T>
- T f_ret(int a) [[ pre: a > 0 ]];
-
- template<typename T>
- T g_ret(int a) [[ pre: a > 0 ]]
- {
- return -a * 1.5;
- }
-};
-
-template<typename T>
-int M::f(int a)
-{
- return -a;
-}
-
-template<typename T>
-int M::f_arg(T a)
-{
- return (int)-a;
-}
-
-template<typename T>
-T M::f_ret(int a)
-{
- return -a * (T)1.5;
-}
-
-template<typename T>
-struct S
-{
- template<typename Q>
- int f(int a) [[ pre: a > 0 ]];
-
- template<typename Q>
- int g(int a) [[ pre: a > 0 ]]
- {
- return -a;
- }
-};
-
-template<typename T>
-template<typename Q>
-int S<T>::f(int a)
-{
- return -a;
-}
-
-#include <cstdio>
-int main(int, char**)
-{
- {
- M m;
- printf ("=================================\n");
- printf ("m.f<int>(-10): %d\n", m.f<int>(-10));
- printf ("m.f<double>(-11.5): %d\n", m.f<double>(-11.5));
- printf ("m.f<int>(10): %d\n", m.f<int>(10));
- printf ("m.f<double>(11.5): %d\n", m.f<double>(11.5));
-
- printf ("=================================\n");
- printf ("m.g<int>(-10): %d\n", m.g<int>(-10));
- printf ("m.g<double>(-11.5): %d\n", m.g<double>(-11.5));
- printf ("m.g<int>(10): %d\n", m.g<int>(10));
- printf ("m.g<double>(11.5): %d\n", m.g<double>(11.5));
-
- printf ("=================================\n");
- printf ("m.f_arg(-10): %d\n", m.f_arg(-10));
- printf ("m.f_arg(-11.5): %d\n", m.f_arg(-11.5));
- printf ("m.f_arg(10): %d\n", m.f_arg(10));
- printf ("m.f_arg(11.5): %d\n", m.f_arg(11.5));
-
- printf ("=================================\n");
- printf ("m.g_arg(-10): %d\n", m.g_arg(-10));
- printf ("m.g_arg(-11.5): %d\n", m.g_arg(-11.5));
- printf ("m.g_arg(10): %d\n", m.g_arg(10));
- printf ("m.g_arg(11.5): %d\n", m.g_arg(11.5));
-
- printf ("=================================\n");
- printf ("m.f_ret<int>(-10): %d\n", m.f_ret<int>(-10));
- printf ("m.f_ret<double>(-11.5): %f\n", m.f_ret<double>(-11.5));
- printf ("m.f_ret<int>(10): %d\n", m.f_ret<int>(10));
- printf ("m.f_ret<double>(11.5): %f\n", m.f_ret<double>(11.5));
-
- printf ("=================================\n");
- printf ("m.g_ret<int>(-10): %d\n", m.g_ret<int>(-10));
- printf ("m.g_ret<double>(-11.5): %f\n", m.g_ret<double>(-11.5));
- printf ("m.g_ret<int>(10): %d\n", m.g_ret<int>(10));
- printf ("m.g_ret<double>(11.5): %f\n", m.g_ret<double>(11.5));
- }
-
- {
- S<int> s;
- printf ("=================================\n");
- s.f<int>(-10);
-
- s.f<double>(-10);
-
- printf ("=================================\n");
- s.g<int>(-10);
-
- s.g<double>(-10);
- }
-
- {
- S<double> s;
- printf ("=================================\n");
- s.f<int>(-10);
-
- s.f<double>(-10);
-
- printf ("=================================\n");
- s.g<int>(-10);
-
- s.g<double>(-10);
- }
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::f<int> at .*:7: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.f<int>\(-10\): 10(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::f<double> at .*:7: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.f<double>\(-11.5\): 11(\n|\r\n|\r)} }
-// { dg-output {m.f<int>\(10\): -10(\n|\r\n|\r)} }
-// { dg-output {m.f<double>\(11.5\): -11(\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::g<int> at .*:10: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.g<int>\(-10\): 10(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::g<double> at .*:10: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.g<double>\(-11.5\): 11(\n|\r\n|\r)} }
-// { dg-output {m.g<int>\(10\): -10(\n|\r\n|\r)} }
-// { dg-output {m.g<double>\(11.5\): -11(\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::f_arg<int> at .*:16: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.f_arg\(-10\): 10(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::f_arg<double> at .*:16: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.f_arg\(-11.5\): 11(\n|\r\n|\r)} }
-// { dg-output {m.f_arg\(10\): -10(\n|\r\n|\r)} }
-// { dg-output {m.f_arg\(11.5\): -11(\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::g_arg<int> at .*:19: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.g_arg\(-10\): 10(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::g_arg<double> at .*:19: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.g_arg\(-11.5\): 11(\n|\r\n|\r)} }
-// { dg-output {m.g_arg\(10\): -10(\n|\r\n|\r)} }
-// { dg-output {m.g_arg\(11.5\): -11(\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::f_ret<int> at .*:25: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.f_ret<int>\(-10\): 10(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::f_ret<double> at .*:25: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.f_ret<double>\(-11.5\): 16.500000(\n|\r\n|\r)} }
-// { dg-output {m.f_ret<int>\(10\): -10(\n|\r\n|\r)} }
-// { dg-output {m.f_ret<double>\(11.5\): -16.500000(\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::g_ret<int> at .*:28: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.g_ret<int>\(-10\): 15(\n|\r\n|\r)} }
-// { dg-output {contract violation in function M::g_ret<double> at .*:28: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {m.g_ret<double>\(-11.5\): 16.500000(\n|\r\n|\r)} }
-// { dg-output {m.g_ret<int>\(10\): -15(\n|\r\n|\r)} }
-// { dg-output {m.g_ret<double>\(11.5\): -16.500000(\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function S<int>::f<int> at .*:56: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function S<int>::f<double> at .*:56: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function S<int>::g<int> at .*:59: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function S<int>::g<double> at .*:59: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function S<double>::f<int> at .*:56: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function S<double>::f<double> at .*:56: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {=================================(\n|\r\n|\r)} }
-// { dg-output {contract violation in function S<double>::g<int> at .*:59: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function S<double>::g<double> at .*:59: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
+++ /dev/null
-// basic test to ensure pre contracts work for free functions
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-namespace nullary
-{
- int x = 10;
- int y = 10;
-
- void fun()
- [[ pre: x < 0 ]]
- {
- printf("fun::x: %d\n", x);
- }
-
- void fun2()
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- {
- printf("fun2::x: %d fun2::y: %d\n", x, y);
- }
-
- void funend()
- [[ pre: x < 0 ]];
-}
-
-namespace nonvoid
-{
- int x = 10;
- double y = 10.5;
-
- struct S
- {
- bool z;
- };
-
- void vfun()
- [[ pre: x < 0 ]]
- {
- printf("vfun::x: %d\n", x);
- }
-
- int fun()
- [[ pre: x < 0 ]]
- {
- printf("fun::x: %d\n", x);
- return x;
- }
-
- double fun2()
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- {
- printf("fun2::x: %d fun2::y: %f\n", x, y);
- return y;
- }
-
- S funend()
- [[ pre: x < 0 ]];
-}
-
-namespace nonnullary
-{
- int x = 10;
- double y = 10.5;
-
- struct S
- {
- bool z;
- };
-
- void vfun(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]]
- {
- printf("vfun::x: %d\n", x);
- }
-
- int fun(int m, double n)
- [[ pre: x < 0 ]]
- {
- printf("fun::x: %d\n", x);
- return x;
- }
-
- double fun2(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]]
- {
- printf("fun2::x: %d fun2::y: %f\n", x, y);
- return y;
- }
-
- S funend(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]];
-}
-
-int main(int, char**) {
- // nullary void
- {
- nullary::fun();
- nullary::fun2();
- nullary::funend();
- }
-
- // nullary non void
- {
- nonvoid::vfun();
-
- int f = 13;
- f = nonvoid::fun();
- printf("main::f: %d\n", f);
- double d = 13.37;
- d = nonvoid::fun2();
- printf("main::d: %f\n", d);
- nonvoid::S s = nonvoid::funend();
- printf("main::s.z: %d\n", s.z ? 1 : 0);
- }
-
- // non-nullary non-void
- {
- int x = 11;
- double y = 11.5;
-
- nonnullary::vfun(x, y);
-
- int f = 13;
- f = nonnullary::fun(x, y);
- printf("main::f: %d\n", f);
- double d = 13.37;
- d = nonnullary::fun2(x, y);
- printf("main::d: %f\n", d);
- nonnullary::S s = nonnullary::funend(x, y);
- printf("main::s.z: %d\n", s.z ? 1 : 0);
- }
- return 0;
-}
-
-namespace nullary
-{
- void funend()
- [[ pre: x < 0 ]]
- {
- printf("funend::x: %d\n", x);
- }
-}
-
-namespace nonvoid
-{
- S funend()
- [[ pre: x < 0 ]]
- {
- printf("funend::x: %d\n", x);
- S s;
- s.z = true;
- return s;
- }
-}
-
-namespace nonnullary
-{
- S funend(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]]
- {
- printf("funend::x: %d\n", x);
- S s;
- s.z = true;
- return s;
- }
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function nullary::fun at .*.C:12: .*(\n|\r\n|\r)" }
-// { dg-output "fun::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nullary::fun2 at .*.C:18: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nullary::fun2 at .*.C:19: .*(\n|\r\n|\r)" }
-// { dg-output "fun2::x: 10 fun2::y: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nullary::funend at .*.C:146: .*(\n|\r\n|\r)" }
-// { dg-output "funend::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonvoid::vfun at .*.C:39: .*(\n|\r\n|\r)" }
-// { dg-output "vfun::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonvoid::fun at .*.C:45: .*(\n|\r\n|\r)" }
-// { dg-output "fun::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::f: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonvoid::fun2 at .*.C:52: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonvoid::fun2 at .*.C:53: .*(\n|\r\n|\r)" }
-// { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
-// { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonvoid::funend at .*.C:155: .*(\n|\r\n|\r)" }
-// { dg-output "funend::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::s.z: 1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::vfun at .*.C:74: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::vfun at .*.C:75: .*(\n|\r\n|\r)" }
-// { dg-output "vfun::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::fun at .*.C:81: .*(\n|\r\n|\r)" }
-// { dg-output "fun::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::f: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::fun2 at .*.C:88: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::fun2 at .*.C:89: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::fun2 at .*.C:90: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::fun2 at .*.C:91: .*(\n|\r\n|\r)" }
-// { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
-// { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::funend at .*.C:167: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nonnullary::funend at .*.C:168: .*(\n|\r\n|\r)" }
-// { dg-output "funend::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::s.z: 1(\n|\r\n|\r)" }
-
+++ /dev/null
-// ensure the feature test macros are defined pre c++20 while we still support
-// -fcontracts independent of std version
-// { dg-do compile { target c++11 } }
-// { dg-additional-options "-fcontracts" }
-
-static_assert (__cpp_contracts >= 201906, "__cpp_contracts");
-static_assert (__cpp_contracts_literal_semantics >= 201906, "__cpp_contracts_literal_semantics");
-static_assert (__cpp_contracts_roles >= 201906, "__cpp_contracts_roles");
-
-int main()
-{
- int x;
-
- [[assert: x >= 0]];
- [[assert default: x < 0]];
- [[assert audit: x == 0]];
- [[assert axiom: x == 1]];
-
- [[assert: x > 0 ? true : false]];
- [[assert: x < 0 ? true : false]];
-
- [[assert ignore: x >= 0]];
- [[assert assume: x >= 0]];
- [[assert check_never_continue: x >= 0]];
- [[assert check_maybe_continue: x >= 0]];
-
- [[assert %default: x >= 0]];
- [[assert default %default: x < 0]];
- [[assert audit %default: x == 0]];
- [[assert axiom %default: x == 1]];
- return 0;
-}
-
+++ /dev/null
-// basic test to ensure contracts work pre-c++2a
-// { dg-do run { target c++11 } }
-// { dg-additional-options "-fcontracts -fcontract-continuation-mode=on" }
-
-int f(int n)
- [[ pre: n > 0 ]]
- [[ post r: r < 0 ]]
-{
- [[ assert: n > 0 ]];
- return -n;
-}
-
-int main()
-{
- f(-5);
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function f at .*\\.C:6: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*\\.C:9: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*\\.C:7: .*(\n|\r\n|\r)" }
-
+++ /dev/null
-// tests to ensure pre contracts work on member functions
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-namespace member
-{
- int x = 10;
- double y = 10.5;
-
- struct S
- {
- bool z;
- };
-
- struct T1
- {
- void vfun(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]];
-
- int fun(int m, double n)
- [[ pre: x < 0 ]];
-
- double fun2(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]];
-
- S funend(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]];
- };
-
- void T1::vfun(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]]
- {
- printf("vfun::x: %d\n", x);
- }
-
-
- int T1::fun(int m, double n)
- [[ pre: x < 0 ]]
- {
- printf("fun::x: %d\n", x);
- return x;
- }
-
- double T1::fun2(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]]
- {
- printf("fun2::x: %d fun2::y: %f\n", x, y);
- return y;
- }
-}
-
-namespace special
-{
- struct T1
- {
- T1(int m, int n)
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]];
-
- int operator+(int m)
- [[ pre: m > 0 ]]
- [[ pre: v > 0 ]];
-
- int operator-(int m)
- [[ pre: m > 0 ]]
- [[ pre: v > 0 ]];
-
- ~T1()
- [[ pre: v > 0 ]];
-
- int v{-10};
- };
-
- T1::T1(int m, int n)
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]]
- : v{-m * n}
- {
- }
-
- int T1::operator+(int m)
- [[ pre: m > 0 ]]
- [[ pre: v > 0 ]]
- {
- return v + m;
- }
-
- int T1::operator-(int m)
- [[ pre: m > 0 ]]
- [[ pre: v > 0 ]]
- {
- return v - m;
- }
-
- T1::~T1()
- [[ pre: v > 0 ]]
- {
- }
-
- struct T2
- {
- T2(int m, int n)
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]]
- : v{-m * n}
- {
- }
-
- int operator+(int m)
- [[ pre: m > 0 ]]
- [[ pre: v > 0 ]]
- {
- return v + m;
- }
-
- int operator-(int m)
- [[ pre: m > 0 ]]
- [[ pre: v > 0 ]]
- {
- return v - m;
- }
-
- ~T2()
- [[ pre: v > 0 ]]
- {
- }
-
- int v{-10};
- };
-
- struct TC : T1
- {
- TC(int m, int n)
- [[ pre: m < -1 ]]
- [[ pre: n < -1 ]]
- : T1{m, n}
- {
- }
-
- ~TC()
- [[ pre: vc < 0 ]]
- {
- }
-
- TC(int a)
- [[ pre: a < 0 ]]
- : TC{a, a}
- {
- }
-
- int vc{10};
- };
-
- void test()
- {
- T1 t1{10, 20};
- int a = t1 - -5;
- int b = t1 + -5;
- printf("==========\n");
-
- T2 t2{10, 20};
- int k = t2 - -5;
- int j = t2 + -5;
- printf("==========\n");
-
- TC tc{10, 20};
- printf("==========\n");
-
- TC tc2{10};
- printf("==========\n");
- }
-}
-
-namespace virt
-{
- struct T1
- {
- virtual int fun(int m, int n)
- [[ pre: m > 0 ]]
- [[ pre: n > 0 ]]
- [[ pre: v > 0 ]];
- int v{-10};
- };
-
- int T1::fun(int m, int n)
- [[ pre: m > 0 ]]
- [[ pre: n > 0 ]]
- [[ pre: v > 0 ]]
- {
- printf("T1::fun::m: %d, T1::fun::n: %d, T1::v: %d\n", m, n, v);
- return m * n * v;
- }
-
- struct T2 : T1
- {
- };
-
- struct T3 : T2
- {
- virtual int fun(int m, int n)
- [[ pre: m > 0 ]]
- [[ pre: n > 0 ]]
- [[ pre: v > 0 ]]
- override
- {
- printf("T3::fun::m: %d, T3::fun::n: %d, T3::v: %d\n", m, n, v);
- return m * n * v;
- }
- };
-
- struct T3b : T2
- {
- virtual int fun(int m, int n)
- [[ pre: m > 0 ]]
- [[ pre: n > 0 ]]
- [[ pre: v > 0 ]]
- override;
-
- int p(int a)
- [[ pre: a > 0 ]]
- [[ pre: v > 0 ]];
-
- int u(int a)
- [[ pre: a > 0 ]]
- [[ pre: z > 0 ]];
-
- int n(int a)
- [[ pre: a > 0 ]];
-
- static int Sn(int a)
- [[ pre: a > 0 ]];
-
- int z{-10};
- };
-
- int T3b::fun(int m, int n)
- [[ pre: m > 0 ]]
- [[ pre: n > 0 ]]
- [[ pre: v > 0 ]]
- {
- printf("T3b::fun::m: %d, T3b::fun::n: %d, T3b::v: %d\n", m, n, v);
- return m * n * v;
- }
-
- int T3b::p(int a)
- [[ pre: a > 0 ]]
- [[ pre: v > 0 ]]
- {
- printf("T3b::p: a: %d, v: %d\n", a, v);
- return a * v;
- }
-
- int T3b::u(int a)
- [[ pre: a > 0 ]]
- [[ pre: z > 0 ]]
- {
- printf("T3b::u: a: %d, z: %d\n", a, z);
- return a * z;
- }
-
- int T3b::n(int a)
- [[ pre: a > 0 ]]
- {
- printf("T3b::n: a: %d\n", a);
- return a;
- }
-
- int T3b::Sn(int a)
- [[ pre: a > 0 ]]
- {
- printf("T3b::Sn: a: %d\n", a);
- return a;
- }
-
- struct T3c : T2
- {
- int fun(int m, int n)
- [[ pre: m > 0 ]]
- [[ pre: n > 0 ]]
- [[ pre: v > 0 ]]
- {
- printf("T3c::fun::m: %d, T3c::fun::n: %d, T3c::v: %d\n", m, n, v);
- return m * n * v;
- }
-
- int p(int a)
- [[ pre: a > 0 ]]
- [[ pre: v > 0 ]]
- {
- printf("T3c::p: a: %d, v: %d\n", a, v);
- return a * v;
- }
-
- int u(int a)
- [[ pre: a > 0 ]]
- [[ pre: z > 0 ]]
- {
- printf("T3c::u: a: %d, z: %d\n", a, z);
- return a * z;
- }
-
- int n(int a)
- [[ pre: a > 0 ]]
- {
- printf("T3c::n: a: %d\n", a);
- return a;
- }
-
- static int Sn(int a)
- [[ pre: a > 0 ]]
- {
- printf("T3c::Sn: a: %d\n", a);
- return a;
- }
-
- int z{-10};
- };
-
- void t(const char *kind, T1 *t)
- {
- printf("=================\n%s:\n", kind);
- t->fun(-1, -2);
- }
-
- void test()
- {
- T1 t1;
- t1.fun(-10, -20);
-
- T2 t2;
- t2.fun(-10, -20);
-
- T3 t3;
- t3.fun(-10, -20);
-
- T3b t3b;
- t3b.fun(-10, -20);
-
- T3c t3c;
- t3c.fun(-10, -20);
-
- t("T1", &t1);
- t("T2", &t2);
- t("T3", &t3);
- t("T3b", &t3b);
- t("T3c", &t3c);
-
- printf("=============\n");
- t3b.p(-3);
- t3b.u(-3);
- t3b.n(-3);
- T3b::Sn(-3);
-
- printf("=============\n");
- t3c.p(-3);
- t3c.u(-3);
- t3c.n(-3);
- T3c::Sn(-3);
- }
-}
-
-int main(int, char**)
-{
- // ordinary member functions
- {
- int x = 11;
- double y = 11.5;
- member::T1 t1;
- t1.vfun(x, y);
-
- int f = 13;
- f = t1.fun(x, y);
- printf("main::f: %d\n", f);
- double d = 13.37;
- d = t1.fun2(x, y);
- printf("main::d: %f\n", d);
- member::S s = t1.funend(x, y);
- printf("main::s.z: %d\n", s.z ? 1 : 0);
- }
-
- special::test();
- virt::test();
- return 0;
-}
-
-member::S member::T1::funend(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]]
-{
- printf("funend::x: %d\n", x);
- S s;
- s.z = true;
- return s;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function member::T1::vfun at .*.C:37: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function member::T1::vfun at .*.C:38: .*(\n|\r\n|\r)" }
-// { dg-output "vfun::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function member::T1::fun at .*.C:45: .*(\n|\r\n|\r)" }
-// { dg-output "fun::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::f: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function member::T1::fun2 at .*.C:52: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function member::T1::fun2 at .*.C:53: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function member::T1::fun2 at .*.C:54: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function member::T1::fun2 at .*.C:55: .*(\n|\r\n|\r)" }
-// { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
-// { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
-// { dg-output "contract violation in function member::T1::funend at .*.C:397: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function member::T1::funend at .*.C:398: .*(\n|\r\n|\r)" }
-// { dg-output "funend::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::s.z: 1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::T1 at .*.C:85: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::T1 at .*.C:86: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::operator- at .*.C:99: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::operator- at .*.C:100: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::operator. at .*.C:92: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::operator. at .*.C:93: .*(\n|\r\n|\r)" }
-// { dg-output "==========(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T2::T2 at .*.C:113: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T2::T2 at .*.C:114: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T2::operator- at .*.C:127: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T2::operator- at .*.C:128: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T2::operator. at .*.C:120: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T2::operator. at .*.C:121: .*(\n|\r\n|\r)" }
-// { dg-output "==========(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::TC::TC at .*.C:144: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::TC::TC at .*.C:145: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::T1 at .*.C:85: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::T1 at .*.C:86: .*(\n|\r\n|\r)" }
-// { dg-output "==========(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::TC::TC at .*.C:156: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::TC::TC at .*.C:144: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::TC::TC at .*.C:145: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::T1 at .*.C:85: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::T1 at .*.C:86: .*(\n|\r\n|\r)" }
-// { dg-output "==========(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::TC::~TC at .*.C:151: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::~T1 at .*.C:106: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::TC::~TC at .*.C:151: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::~T1 at .*.C:106: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T2::~T2 at .*.C:134: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function special::T1::~T1 at .*.C:106: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:196: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:197: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:198: .*(\n|\r\n|\r)" }
-// { dg-output "T1::fun::m: -10, T1::fun::n: -20, T1::v: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:196: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:197: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:198: .*(\n|\r\n|\r)" }
-// { dg-output "T1::fun::m: -10, T1::fun::n: -20, T1::v: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3::fun at .*.C:211: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3::fun at .*.C:212: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3::fun at .*.C:213: .*(\n|\r\n|\r)" }
-// { dg-output "T3::fun::m: -10, T3::fun::n: -20, T3::v: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::fun at .*.C:247: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::fun at .*.C:248: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::fun at .*.C:249: .*(\n|\r\n|\r)" }
-// { dg-output "T3b::fun::m: -10, T3b::fun::n: -20, T3b::v: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::fun at .*.C:288: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::fun at .*.C:289: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::fun at .*.C:290: .*(\n|\r\n|\r)" }
-// { dg-output "T3c::fun::m: -10, T3c::fun::n: -20, T3c::v: -10(\n|\r\n|\r)" }
-// { dg-output "=================(\n|\r\n|\r)" }
-// { dg-output "T1:(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:196: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:197: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:198: .*(\n|\r\n|\r)" }
-// { dg-output "T1::fun::m: -1, T1::fun::n: -2, T1::v: -10(\n|\r\n|\r)" }
-// { dg-output "=================(\n|\r\n|\r)" }
-// { dg-output "T2:(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:196: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:197: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T1::fun at .*.C:198: .*(\n|\r\n|\r)" }
-// { dg-output "T1::fun::m: -1, T1::fun::n: -2, T1::v: -10(\n|\r\n|\r)" }
-// { dg-output "=================(\n|\r\n|\r)" }
-// { dg-output "T3:(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3::fun at .*.C:211: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3::fun at .*.C:212: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3::fun at .*.C:213: .*(\n|\r\n|\r)" }
-// { dg-output "T3::fun::m: -1, T3::fun::n: -2, T3::v: -10(\n|\r\n|\r)" }
-// { dg-output "=================(\n|\r\n|\r)" }
-// { dg-output "T3b:(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::fun at .*.C:247: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::fun at .*.C:248: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::fun at .*.C:249: .*(\n|\r\n|\r)" }
-// { dg-output "T3b::fun::m: -1, T3b::fun::n: -2, T3b::v: -10(\n|\r\n|\r)" }
-// { dg-output "=================(\n|\r\n|\r)" }
-// { dg-output "T3c:(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::fun at .*.C:288: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::fun at .*.C:289: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::fun at .*.C:290: .*(\n|\r\n|\r)" }
-// { dg-output "T3c::fun::m: -1, T3c::fun::n: -2, T3c::v: -10(\n|\r\n|\r)" }
-// { dg-output "=============(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::p at .*.C:256: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::p at .*.C:257: .*(\n|\r\n|\r)" }
-// { dg-output "T3b::p: a: -3, v: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::u at .*.C:264: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::u at .*.C:265: .*(\n|\r\n|\r)" }
-// { dg-output "T3b::u: a: -3, z: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::n at .*.C:272: .*(\n|\r\n|\r)" }
-// { dg-output "T3b::n: a: -3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3b::Sn at .*.C:279: .*(\n|\r\n|\r)" }
-// { dg-output "T3b::Sn: a: -3(\n|\r\n|\r)" }
-// { dg-output "=============(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::p at .*.C:297: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::p at .*.C:298: .*(\n|\r\n|\r)" }
-// { dg-output "T3c::p: a: -3, v: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::u at .*.C:305: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::u at .*.C:306: .*(\n|\r\n|\r)" }
-// { dg-output "T3c::u: a: -3, z: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::n at .*.C:313: .*(\n|\r\n|\r)" }
-// { dg-output "T3c::n: a: -3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function virt::T3c::Sn at .*.C:320: .*(\n|\r\n|\r)" }
-// { dg-output "T3c::Sn: a: -3(\n|\r\n|\r)" }
-
+++ /dev/null
-// test that contracts on overriding functions are found correctly
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-struct Base
-{
- virtual int f(int a) [[ pre: a > 0 ]];
-};
-
-int Base::f(int a)
-{
- return a + 10;
-}
-
-// inherits original
-struct Child0 : Base
-{
-};
-
-// defined out of line, explicit override
-struct Child1 : Base
-{
- virtual int f(int a) override;
-};
-
-int Child1::f(int a)
-{
- return a + 20;
-}
-
-// defined out of line
-struct Child2 : Base
-{
- int f(int a);
-};
-
-int Child2::f(int a)
-{
- return a + 30;
-}
-
-// defined inline, explicitly override
-struct Child3 : Base
-{
- virtual int f(int a) override
- {
- return a + 40;
- }
-};
-
-// defined inline
-struct Child4 : Base
-{
- int f(int a)
- {
- return a + 50;
- }
-};
-
-#include <cstdio>
-int main(int, char**)
-{
- Base b;
- Child0 c0;
- Child1 c1;
- Child2 c2;
- Child3 c3;
- Child4 c4;
-
- printf("Base: %d\n", b.f(-10));
- printf("Child0: %d\n", c0.f(-10));
- printf("Child1: %d\n", c1.f(-10));
- printf("Child2: %d\n", c2.f(-10));
- printf("Child3: %d\n", c3.f(-10));
- printf("Child4: %d\n", c4.f(-10));
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function Base::f at .*.C:7: .*(\n|\r\n|\r)" }
-// { dg-output "Base: 0(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Base::f at .*.C:7: .*(\n|\r\n|\r)" }
-// { dg-output "Child0: 0(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Child1::f at .*.C:7: .*(\n|\r\n|\r)" }
-// { dg-output "Child1: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Child2::f at .*.C:7: .*(\n|\r\n|\r)" }
-// { dg-output "Child2: 20(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Child3::f at .*.C:7: .*(\n|\r\n|\r)" }
-// { dg-output "Child3: 30(\n|\r\n|\r)" }
-// { dg-output "contract violation in function Child4::f at .*.C:7: .*(\n|\r\n|\r)" }
-// { dg-output "Child4: 40(\n|\r\n|\r)" }
-
+++ /dev/null
-// basic test to ensure pre contracts work for free templates
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-template<typename T>
-int body(int a)
- [[ pre: a > 0 ]]
-{
- T t = a * 2.5;
- return t;
-}
-
-template<typename T>
-int none(int a)
- [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<typename T>
-int arg0(T t)
- [[ pre: t > 0 ]]
-{
- return -t - 10;
-}
-
-template<typename T>
-int arg1(int a, T t)
- [[ pre: a > 0 ]]
- [[ pre: t > 0 ]]
-{
- return -t * a;
-}
-
-template<typename T>
-T ret(int a)
- [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-int main(int, char**)
-{
- printf("%d\n", body<int>(-1));
- printf("%d\n", body<double>(-2));
- printf("%d\n", none<int>(-1));
- printf("%d\n", none<double>(-2));
- printf("%d\n", arg0(-1));
- printf("%d\n", arg0(-2.9));
- printf("%d\n", arg1(-3, -1));
- printf("%d\n", arg1(-4, -2.9));
- printf("%d\n", (int)ret<int>(-3));
- printf("%d\n", (int)ret<double>(-4.9));
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function body<int> at .*.C:8: .*(\n|\r\n|\r)" }
-// { dg-output "-2(\n|\r\n|\r)" }
-// { dg-output "contract violation in function body<double> at .*.C:8: .*(\n|\r\n|\r)" }
-// { dg-output "-5(\n|\r\n|\r)" }
-// { dg-output "contract violation in function none<int> at .*.C:16: .*(\n|\r\n|\r)" }
-// { dg-output "1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function none<double> at .*.C:16: .*(\n|\r\n|\r)" }
-// { dg-output "2(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg0<int> at .*.C:23: .*(\n|\r\n|\r)" }
-// { dg-output "-9(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg0<double> at .*.C:23: .*(\n|\r\n|\r)" }
-// { dg-output "-7(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg1<int> at .*.C:30: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg1<int> at .*.C:31: .*(\n|\r\n|\r)" }
-// { dg-output "-3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg1<double> at .*.C:30: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg1<double> at .*.C:31: .*(\n|\r\n|\r)" }
-// { dg-output "-11(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ret<int> at .*.C:38: .*(\n|\r\n|\r)" }
-// { dg-output "3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ret<double> at .*.C:38: .*(\n|\r\n|\r)" }
-// { dg-output "4(\n|\r\n|\r)" }
-
+++ /dev/null
-// ensure no errors are thrown when we have to insert a decl for the internal
-// unchecked function after leaving a (possibly nested) namespace
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-namespace ns0
-{
- int f(int a) [[ pre: a > 0 ]];
-}
-
-int ns0::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-namespace ns0
-{
- namespace ns1
- {
- int f(int a) [[ pre: a > 0 ]];
- }
-}
-
-int ns0::ns1::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-namespace ns0
-{
- namespace ns1
- {
- int f2(int a) [[ pre: a > 0 ]];
- namespace ns2
- {
- int f(int a) [[ pre: a > 0 ]];
- }
- }
- int ns1::f2(int a) [[ pre: a > 0 ]]
- {
- return -a;
- }
-}
-
-int ns0::ns1::ns2::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-namespace ns0
-{
- struct S
- {
- int f(int a) [[ pre: a > 0 ]];
- };
- namespace ns1
- {
- struct S2
- {
- int f(int a) [[ pre: a > 0 ]];
- };
- }
-}
-
-int ns0::S::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-int ns0::ns1::S2::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
+++ /dev/null
-// ensure no errors are thrown when we have to insert a decl for the internal
-// unchecked function after leaving a (possibly nested) namespace
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-namespace ns0
-{
- template<typename T>
- int f(T a) [[ pre: a > 0 ]];
-}
-
-template<typename T>
-int ns0::f(T a) [[ pre: a > 0 ]]
-{
- return (int)-a;
-}
-
-namespace ns0
-{
- namespace ns1
- {
- template<typename T>
- int f(T a) [[ pre: a > 0 ]];
- }
-}
-
-template<typename T>
-int ns0::ns1::f(T a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-namespace ns0
-{
- namespace ns1
- {
- template<typename T>
- int f2(T a) [[ pre: a > 0 ]];
- namespace ns2
- {
- template<typename T>
- int f(T a) [[ pre: a > 0 ]];
- }
- }
- template<typename T>
- int ns1::f2(T a) [[ pre: a > 0 ]]
- {
- return -a;
- }
-}
-
-template<typename T>
-int ns0::ns1::ns2::f(T a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-namespace ns0
-{
- template<typename T>
- struct S
- {
- int f(T a) [[ pre: a > 0 ]];
- };
- namespace ns1
- {
- template<typename T>
- struct S2
- {
- int f(T a) [[ pre: a > 0 ]];
- };
- }
-}
-
-template<typename T>
-int ns0::S<T>::f(T a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<typename T>
-int ns0::ns1::S2<T>::f(T a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-#include <cstdio>
-int main(int, char**)
-{
- printf ("%d\n", ns0::f(-1));
- printf ("%d\n", ns0::ns1::f(-2));
- printf ("%d\n", ns0::ns1::f2(-3));
- printf ("%d\n", ns0::ns1::ns2::f(-4));
- ns0::S<int> ns0_s;
- printf ("%d\n", ns0_s.f(-5));
- ns0::ns1::S2<int> ns0_ns1_s2;
- printf ("%d\n", ns0_ns1_s2.f(-6));
-
- printf ("%d\n", ns0::f(-7.5));
- printf ("%d\n", ns0::ns1::f(-8.5));
- printf ("%d\n", ns0::ns1::f2(-9.5));
- printf ("%d\n", ns0::ns1::ns2::f(-10.5));
- ns0::S<double> ns0_sd;
- printf ("%d\n", ns0_sd.f(-11.5));
- ns0::ns1::S2<double> ns0_ns1_s2d;
- printf ("%d\n", ns0_ns1_s2d.f(-12.5));
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function ns0::f<int> at .*.C:13: .*(\n|\r\n|\r)" }
-// { dg-output "1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::ns1::f<int> at .*.C:28: .*(\n|\r\n|\r)" }
-// { dg-output "2(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::ns1::f2<int> at .*.C:46: .*(\n|\r\n|\r)" }
-// { dg-output "3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::ns1::ns2::f<int> at .*.C:53: .*(\n|\r\n|\r)" }
-// { dg-output "4(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::S<int>::f at .*.C:76: .*(\n|\r\n|\r)" }
-// { dg-output "5(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::ns1::S2<int>::f at .*.C:82: .*(\n|\r\n|\r)" }
-// { dg-output "6(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::f<double> at .*.C:13: .*(\n|\r\n|\r)" }
-// { dg-output "7(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::ns1::f<double> at .*.C:28: .*(\n|\r\n|\r)" }
-// { dg-output "8(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::ns1::f2<double> at .*.C:46: .*(\n|\r\n|\r)" }
-// { dg-output "9(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::ns1::ns2::f<double> at .*.C:53: .*(\n|\r\n|\r)" }
-// { dg-output "10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::S<double>::f at .*.C:76: .*(\n|\r\n|\r)" }
-// { dg-output "11(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ns0::ns1::S2<double>::f at .*.C:82: .*(\n|\r\n|\r)" }
-// { dg-output "12(\n|\r\n|\r)" }
+++ /dev/null
-// ensure no errors are thrown for various combinations of class templates
-// with guarded members
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-template<typename T>
-struct S
-{
- int f(int a) [[ pre: a > 0 ]];
- int g(int a) [[ pre: a > 0 ]];
-};
-
-template<typename T>
-int S<T>::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<typename T>
-int S<T>::g(int a) // Contract is inherited (error from line 10).
-{
- return -a;
-}
-
-template<typename T>
-struct S_arg
-{
- int f(T a) [[ pre: a > 0 ]];
- int g(T a) [[ pre: a > 0 ]];
-};
-
-template<typename T>
-int S_arg<T>::f(T a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<typename T>
-int S_arg<T>::g(T a) // Contract is inherited (error from line 29).
-{
- return -a;
-}
-
-template<typename T>
-struct S_ret
-{
- T f(int a) [[ pre: a > 0 ]];
- T g(int a) [[ pre: a > 0 ]];
-};
-
-template<typename T>
-T S_ret<T>::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<typename T>
-T S_ret<T>::g(int a) // Contract is inherited (error from line 48).
-{
- return -a;
-}
-
-#include <cstdio>
-int main(int, char**)
-{
- {
- S<int> s_int;
- printf ("s_int.f(-10): %d\n", s_int.f(-10));
- printf ("s_int.g(-10): %d\n", s_int.g(-10));
- printf ("s_int.f(10): %d\n", s_int.f(10));
- printf ("s_int.g(10): %d\n", s_int.g(10));
-
- S<double> s_double;
- printf ("s_double.f(-10.5): %d\n", s_double.f(-10.5));
- printf ("s_double.g(-10.5): %d\n", s_double.g(-10.5));
- printf ("s_double.f(10.5): %d\n", s_double.f(10.5));
- printf ("s_double.g(10.5): %d\n", s_double.g(10.5));
- }
-
- {
- S_arg<int> s_arg_int;
- printf ("s_arg_int.f(-10): %d\n", s_arg_int.f(-10));
- printf ("s_arg_int.g(-10): %d\n", s_arg_int.g(-10));
- printf ("s_arg_int.f(10): %d\n", s_arg_int.f(10));
- printf ("s_arg_int.g(10): %d\n", s_arg_int.g(10));
-
- S_arg<double> s_arg_double;
- printf ("s_arg_double.f(-10): %d\n", s_arg_double.f(-10));
- printf ("s_arg_double.g(-10): %d\n", s_arg_double.g(-10));
- printf ("s_arg_double.f(10): %d\n", s_arg_double.f(10));
- printf ("s_arg_double.g(10): %d\n", s_arg_double.g(10));
- }
-
- {
- S_ret<int> s_ret_int;
- printf ("s_ret_int.f(-10): %d\n", s_ret_int.f(-10));
- printf ("s_ret_int.g(-10): %d\n", s_ret_int.g(-10));
- printf ("s_ret_int.f(10): %d\n", s_ret_int.f(10));
- printf ("s_ret_int.g(10): %d\n", s_ret_int.g(10));
-
- S_ret<double> s_ret_double;
- printf ("s_ret_double.f(-10): %f\n", s_ret_double.f(-10));
- printf ("s_ret_double.g(-10): %f\n", s_ret_double.g(-10));
- printf ("s_ret_double.f(10): %f\n", s_ret_double.f(10));
- printf ("s_ret_double.g(10): %f\n", s_ret_double.g(10));
- }
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function S<int>::f at .*.C:14: .*(\n|\r\n|\r)" }
-// { dg-output "s_int.f.-10.: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S<int>::g at .*.C:10: .*(\n|\r\n|\r)" }
-// { dg-output "s_int.g.-10.: 10(\n|\r\n|\r)" }
-// { dg-output "s_int.f.10.: -10(\n|\r\n|\r)" }
-// { dg-output "s_int.g.10.: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S<double>::f at .*.C:14: .*(\n|\r\n|\r)" }
-// { dg-output "s_double.f.-10.5.: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S<double>::g at .*.C:10: .*(\n|\r\n|\r)" }
-// { dg-output "s_double.g.-10.5.: 10(\n|\r\n|\r)" }
-// { dg-output "s_double.f.10.5.: -10(\n|\r\n|\r)" }
-// { dg-output "s_double.g.10.5.: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S_arg<int>::f at .*.C:33: .*(\n|\r\n|\r)" }
-// { dg-output "s_arg_int.f.-10.: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S_arg<int>::g at .*.C:29: .*(\n|\r\n|\r)" }
-// { dg-output "s_arg_int.g.-10.: 10(\n|\r\n|\r)" }
-// { dg-output "s_arg_int.f.10.: -10(\n|\r\n|\r)" }
-// { dg-output "s_arg_int.g.10.: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S_arg<double>::f at .*.C:33: .*(\n|\r\n|\r)" }
-// { dg-output "s_arg_double.f.-10.: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S_arg<double>::g at .*.C:29: .*(\n|\r\n|\r)" }
-// { dg-output "s_arg_double.g.-10.: 10(\n|\r\n|\r)" }
-// { dg-output "s_arg_double.f.10.: -10(\n|\r\n|\r)" }
-// { dg-output "s_arg_double.g.10.: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S_ret<int>::f at .*.C:52: .*(\n|\r\n|\r)" }
-// { dg-output "s_ret_int.f.-10.: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S_ret<int>::g at .*.C:48: .*(\n|\r\n|\r)" }
-// { dg-output "s_ret_int.g.-10.: 10(\n|\r\n|\r)" }
-// { dg-output "s_ret_int.f.10.: -10(\n|\r\n|\r)" }
-// { dg-output "s_ret_int.g.10.: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S_ret<double>::f at .*.C:52: .*(\n|\r\n|\r)" }
-// { dg-output "s_ret_double.f.-10.: 10.000000(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S_ret<double>::g at .*.C:48: .*(\n|\r\n|\r)" }
-// { dg-output "s_ret_double.g.-10.: 10.000000(\n|\r\n|\r)" }
-// { dg-output "s_ret_double.f.10.: -10.000000(\n|\r\n|\r)" }
-// { dg-output "s_ret_double.g.10.: -10.000000(\n|\r\n|\r)" }
+++ /dev/null
-// generic error tests for contract redecls with generalized redecl
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-// OK if equivalent -- even through renames.
-int g0(int a) [[ pre: a > 0 ]];
-int g0(int a) [[ pre: a > 0 ]];
-
-int g0b(int a) [[ pre: a > 0 ]];
-int g0b(int b) [[ pre: b > 0 ]];
-int g0b(int c) [[ pre: c > 0 ]]
-{
- return 0;
-}
-
-// OK if specified before.
-int g1(int a) [[ pre: a > 0 ]];
-int g1(int a);
-
-// OK if specified after.
-int g2(int a);
-int g2(int a) [[ pre: a > 0 ]];
-
-int g2b(int a);
-int g2b(int b) [[ pre: b > 0 ]];
-
-// can add to non-virtual methods
-struct G0
-{
- int f(int a);
-};
-
-// OK to add contracts at the point of definition.
-int G0::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-struct G1
-{
- int f1(int a);
-};
-
-// OK to redeclare functions and add constraints...
-int G1::f1(int a) [[ pre: a > 0 ]];
-
-// ...and leave them off later.
-int G1::f1(int a)
-{
- return -a;
-}
-
-int f0(int a) [[ pre: a > 0 ]];
-int f0(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]]; // { dg-error "different number of contracts" }
-
-int f1(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]];
-int f1(int a) [[ pre: a > 0 ]]; // { dg-error "different number of contracts" }
-
-int f2(int a) [[ pre: a > 0 ]];
-int f2(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
-
-int f3(int a) { return a; }
-int f3(int a) [[ pre: a < 0 ]]; // { dg-error "cannot add contracts" }
-
-struct Base
-{
- virtual int f(int a) [[ pre: a > 0 ]];
-};
-
-struct Child : Base
-{
- int f(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
-};
-
-struct S1
-{
- virtual int f(int a); // contracts are inherited at the point of declarations
-};
-
-int S1::f(int a) [[ pre: a > 0 ]] // { dg-error "cannot add" }
-{
- return -a;
-}
-
-struct S2
-{
- int f() { return 0; }
-};
-
-int S2::f(); // OK?
-
-
-struct S3
-{
- int f() { return 0; }
-};
-
-int S3::f() [[pre: true]]; // { dg-error "cannot add contracts" }
-
-
-// The initial decl of a guarded member must appear inside the class.
-struct S4
-{
- int f(int a);
-};
-
-int S4::g(int a) [[ pre: a > 0 ]]; // { dg-error "no declaration matches" }
-
-
-struct S5
-{
- template<typename T>
- S5(T a);
-};
-
-template<typename T>
-S5::S5(T a) [[ pre: a > 0 ]]
-{
-}
-
-struct S6
-{
- template<typename T>
- S6(T a);
-};
-
-template<typename T>
-S6::S6(T a) [[ pre: a > 0 ]];
-
-template<typename T>
-S6::S6(T a)
-{
-}
-
-int p0(int n)
- [[ post r: r > 0 && r == n ]]
- [[ post r: r > 1 && r == n ]]
- [[ post r: r > 2 && r == n ]]
- [[ post r: r > 3 && r == n ]];
-
-int p0(int z)
- [[ post r: r > 0 && r == z ]]
- [[ post r1: r1 > 1 && r1 == z ]]
- [[ post r2: r2 > 2 && r2 == z ]]
- [[ post r3: r3 > 3 && r3 == z ]]
-{
- return z;
-}
-
+++ /dev/null
-// generic error tests for generalized contract redecls
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-// allowed to repeat contracts or omit them
-int g0(int a) [[ pre: a > 0 ]];
-int g0(int a) [[ pre: a > 0 ]];
-
-int g1(int a) [[ pre: a > 0 ]];
-int g1(int a);
-
-// allowed to add from none if generalized redecl is on (by default)
-int g2(int a);
-int g2(int a) [[ pre: a > 0 ]];
-
-// can add to non-virtual methods
-struct G0
-{
- int f(int a);
-};
-
-int G0::f(int a) [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-struct G1
-{
- int f(int a);
-};
-
-int G1::f(int a) [[ pre: a > 0 ]];
-
-int G1::f(int a)
-{
- return -a;
-}
-
-// allowed to redeclare even without contracts
-struct G2
-{
- int f(int a);
-};
-
-int G2::f(int a);
-
-
-int f0(int a) [[ pre: a > 0 ]];
-int f0(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]]; // { dg-error "different number of contracts" }
-
-int f1(int a) [[ pre: a > 0 ]];
-int f1(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
-
-int f2(int a) { return a; }
-int f2(int a) [[ pre: a < 0 ]]; // { dg-error "cannot add contracts after definition" }
-
-struct Base
-{
- virtual int f(int a) [[ pre: a > 0 ]];
-};
-
-struct Child : Base
-{
- int f(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
-};
-
-// the initial decl of a guarded member must appear inside the class
-struct F2
-{
- int f(int a);
-};
-
-int F2::g(int a) [[ pre: a > 0 ]]; // { dg-error "no declaration matches" }
-// FIXME if we move F2 down then a different error makes F2 undeclared
-
-struct F0
-{
- virtual int f(int a);
-};
-
-int F0::f(int a); // { dg-error "declaration.*is not definition" }
-
-struct F1
-{
- virtual int f(int a);
-};
-
-int F1::f(int a) [[ pre: a > 0 ]] // { dg-error "cannot add" }
-{
- return -a;
-}
-
-// cannot "re"declare members of a forward declared class
-struct F2;
-int F2::test(); // { dg-error "no declaration matches" }
-int F2::test2() [[ pre: true ]]; // { dg-error "no declaration matches" }
-
-// can only redeclare member functions
-struct F3
-{
- int x;
- typedef int my_int;
-
- struct Inner0;
- struct Inner1;
- enum my_enum0; // { dg-error "use of enum.*without previous decl" }
- enum my_enum1 { E1, E2 };
-
- int test0();
- int test1();
- int test2();
-};
-
-int F3::x{-1}; // { dg-error "is not a static data member" }
-typedef double F3::my_int; // { dg-error "typedef name may not be a nested-name-specifier" }
-struct F3::Inner0; // { dg-warning "declaration.*does not declare anything" }
-
-struct F3::Inner1 { };
-
-enum F3::my_enum1 { E0, E1, END }; // { dg-error "multiple definition" }
-
-struct F4
-{
- int test0();
-
- int F3::test0() [[ pre: true ]]; // { dg-error "cannot declare member function" }
- friend int F3::test1();
- friend int F3::test2();
-};
-int F3::test2() [[ pre: true ]] { return -1; }
-
-void dummy0()
-{
- int F4::test0() [[ pre: true ]]; // { dg-error "qualified-id in declaration" }
-}
-
-namespace ns0
-{
- typedef int value;
- struct X
- {
- int test1(value);
- typedef double value;
- int test2(value);
- };
- int X::test1(value); // { dg-error "no declaration matches" }
- int X::test2(value);
-}
-
+++ /dev/null
-// basic test to ensure contracts generalized redecl works
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-namespace defining
-{
- int x = 10;
- double y = 10.5;
-
- struct S
- {
- bool z;
- };
-
- struct T1
- {
- void vfun(int m, double n);
- int fun(int m, double n);
- double fun2(int m, double n);
- S funend(int m, double n);
- };
-
- void T1::vfun(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]]
- {
- printf("vfun::x: %d\n", x);
- }
-
- int T1::fun(int m, double n)
- [[ pre: x < 0 ]]
- {
- printf("fun::x: %d\n", x);
- return x;
- }
-
- double T1::fun2(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]]
- {
- printf("fun2::x: %d fun2::y: %f\n", x, y);
- return y;
- }
-}
-
-namespace nondefining
-{
- int x = 10;
- double y = 10.5;
-
- struct S
- {
- bool z;
- };
-
- struct T1
- {
- void vfun(int m, double n);
- int fun(int m, double n);
- double fun2(int m, double n);
- S funend(int m, double n);
- };
-
- void T1::vfun(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]];
- int T1::fun(int m, double n)
- [[ pre: x < 0 ]];
- double T1::fun2(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]];
-
- void T1::vfun(int m, double n)
- {
- printf("vfun::x: %d\n", x);
- }
-
- int T1::fun(int m, double n)
- {
- printf("fun::x: %d\n", x);
- return x;
- }
-
- double T1::fun2(int m, double n)
- {
- printf("fun2::x: %d fun2::y: %f\n", x, y);
- return y;
- }
-}
-
-int main(int, char**) {
- // defining redecl
- {
- int x = 11;
- double y = 11.5;
-
- defining::T1 t1;
- t1.vfun(x, y);
-
- int f = 13;
- f = t1.fun(x, y);
- printf("main::f: %d\n", f);
- double d = 13.37;
- d = t1.fun2(x, y);
- printf("main::d: %f\n", d);
- defining::S s = t1.funend(x, y);
- printf("main::s.z: %d\n", s.z ? 1 : 0);
- }
-
- // nondefining redecl
- {
- int x = 12;
- double y = 12.5;
-
- nondefining::T1 t1;
- t1.vfun(x, y);
-
- int f = 13;
- f = t1.fun(x, y);
- printf("main::f: %d\n", f);
- double d = 13.37;
- d = t1.fun2(x, y);
- printf("main::d: %f\n", d);
- nondefining::S s = t1.funend(x, y);
- printf("main::s.z: %d\n", s.z ? 1 : 0);
- }
- return 0;
-}
-
-namespace defining
-{
- S T1::funend(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]]
- {
- printf("funend::x: %d\n", x);
- S s;
- s.z = true;
- return s;
- }
-}
-
-namespace nondefining
-{
- S T1::funend(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]];
-
- S T1::funend(int m, double n)
- {
- printf("funend::x: %d\n", x);
- S s;
- s.z = true;
- return s;
- }
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function defining::T1::vfun at .*.C:25: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::vfun at .*.C:26: .*(\n|\r\n|\r)" }
-// { dg-output "vfun::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun at .*.C:32: .*(\n|\r\n|\r)" }
-// { dg-output "fun::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::f: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun2 at .*.C:39: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun2 at .*.C:40: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun2 at .*.C:41: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun2 at .*.C:42: .*(\n|\r\n|\r)" }
-// { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
-// { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::funend at .*.C:138: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::funend at .*.C:139: .*(\n|\r\n|\r)" }
-// { dg-output "funend::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::s.z: 1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::vfun at .*.C:68: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::vfun at .*.C:69: .*(\n|\r\n|\r)" }
-// { dg-output "vfun::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun at .*.C:71: .*(\n|\r\n|\r)" }
-// { dg-output "fun::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::f: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:73: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:74: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:75: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:76: .*(\n|\r\n|\r)" }
-// { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
-// { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::funend at .*.C:151: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::funend at .*.C:152: .*(\n|\r\n|\r)" }
-// { dg-output "funend::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::s.z: 1(\n|\r\n|\r)" }
-
+++ /dev/null
-// test that free functions can be redeclared with contracts without affecting
-// normal default parm handling
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-int f(int a, int, int c = 10);
-int f(int a, int b = 11, int);
-int f(int a, int b, int c)
- [[ pre: a < 0 ]]
- [[ pre: b < 0 ]]
- [[ pre: c < 0 ]];
-
-int f(int, int, int);
-
-int f(int a, int b, int c)
-{
- printf("f: a: %d, b: %d, c: %d\n", a, b, c);
- return a * b - c;
-}
-
-int f(int a = 12, int, int);
-
-int main(int, char **)
-{
- f(1,1,1);
- printf("=====\n");
- f(1,1);
- printf("=====\n");
- f(1);
- printf("=====\n");
- f();
- printf("=====\n");
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function f at .*.C:10: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:11: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:12: .*(\n|\r\n|\r)" }
-// { dg-output "f: a: 1, b: 1, c: 1(\n|\r\n|\r)" }
-// { dg-output "=====(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:10: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:11: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:12: .*(\n|\r\n|\r)" }
-// { dg-output "f: a: 1, b: 1, c: 10(\n|\r\n|\r)" }
-// { dg-output "=====(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:10: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:11: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:12: .*(\n|\r\n|\r)" }
-// { dg-output "f: a: 1, b: 11, c: 10(\n|\r\n|\r)" }
-// { dg-output "=====(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:10: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:11: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function f at .*.C:12: .*(\n|\r\n|\r)" }
-// { dg-output "f: a: 12, b: 11, c: 10(\n|\r\n|\r)" }
-// { dg-output "=====(\n|\r\n|\r)" }
-
+++ /dev/null
-// generic error tests for generalized contract redecls
-// we also test for the warning diagnostic for strict redecl
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts -fcontract-strict-declarations=on" }
-
-// allowed to repeat contracts or omit them
-int g0(int a) [[ pre: a > 0 ]];
-int g0(int a) [[ pre: a > 0 ]];
-
-int g1(int a) [[ pre: a > 0 ]];
-int g1(int a);
-
-// allowed to add from none if generalized redecl is on (by default)
-int g2(int a);
-int g2(int a) [[ pre: a > 0 ]]; // { dg-warning "adds contracts" }
-int g2(int a) [[ pre: a > 0 ]]; // { dg-bogus "adds contracts" }
-
-// can add to non-virtual methods
-struct G0
-{
- int f(int a);
-};
-
-int G0::f(int a) [[ pre: a > 0 ]] // { dg-warning "adds contracts" }
-{
- return -a;
-}
-
-struct G1
-{
- int f(int a);
-};
-
-int G1::f(int a) [[ pre: a > 0 ]]; // { dg-warning "adds contracts" }
-// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 }
-
-int G1::f(int a);
-// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 }
-
-int G1::f(int a) [[ pre: a > 0 ]];
-// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 }
-
-int G1::f(int a)
-{
- return -a;
-}
-
-// allowed to redeclare even without contracts
-struct G2
-{
- int f(int a);
-};
-
-int G2::f(int a); // { dg-warning "outside of class is not definition" }
-
-
-int f0(int a) [[ pre: a > 0 ]];
-int f0(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]]; // { dg-error "different number of contracts" }
-
-int f1(int a) [[ pre: a > 0 ]];
-int f1(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
-
-int f2(int a) { return a; }
-int f2(int a) [[ pre: a < 0 ]]; // { dg-error "cannot add contracts after definition" }
-
-struct Base
-{
- virtual int f(int a) [[ pre: a > 0 ]];
-};
-
-struct Child : Base
-{
- int f(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
-};
-
-// the initial decl of a guarded member must appear inside the class
-struct F2
-{
- int f(int a);
-};
-
-int F2::g(int a) [[ pre: a > 0 ]]; // { dg-error "no declaration matches" }
-// FIXME if we move F2 down then a different error makes F2 undeclared
-
-struct F0
-{
- virtual int f(int a);
-};
-
-int F0::f(int a); // { dg-error "declaration.*is not definition" }
-
-struct F1
-{
- virtual int f(int a);
-};
-
-int F1::f(int a) [[ pre: a > 0 ]] // { dg-error "cannot add" }
-{
- return -a;
-}
-
+++ /dev/null
-// basic test to ensure contracts generalized redecl works
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-namespace defining
-{
- int x = 10;
- double y = 10.5;
-
- struct S
- {
- bool z;
- };
-
- struct T1
- {
- void vfun(int m, double n) const;
- int fun(int m, double n) volatile;
- double fun2(int m, double n) const volatile;
- static S funend(int m, double n);
- };
-
- void T1::vfun(int m, double n) const
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]]
- {
- printf("vfun::x: %d\n", x);
- }
-
- int T1::fun(int m, double n) volatile
- [[ pre: x < 0 ]]
- {
- printf("fun::x: %d\n", x);
- return x;
- }
-
- double T1::fun2(int m, double n) const volatile
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]]
- {
- printf("fun2::x: %d fun2::y: %f\n", x, y);
- return y;
- }
-}
-
-namespace nondefining
-{
- int x = 10;
- double y = 10.5;
-
- struct S
- {
- bool z;
- };
-
- struct T1
- {
- void vfun(int m, double n) const;
- int fun(int m, double n) volatile;
- double fun2(int m, double n) const volatile;
- static S funend(int m, double n);
- };
-
- void T1::vfun(int m, double n) const
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]];
- int T1::fun(int m, double n) volatile
- [[ pre: x < 0 ]];
- double T1::fun2(int m, double n) const volatile
- [[ pre: x < 0 ]]
- [[ pre: y < 0 ]]
- [[ pre: m < 0 ]]
- [[ pre: n < 0 ]];
-
- void T1::vfun(int m, double n) const
- {
- printf("vfun::x: %d\n", x);
- }
-
- int T1::fun(int m, double n) volatile
- {
- printf("fun::x: %d\n", x);
- return x;
- }
-
- double T1::fun2(int m, double n) const volatile
- {
- printf("fun2::x: %d fun2::y: %f\n", x, y);
- return y;
- }
-}
-
-int main(int, char**) {
- // defining redecl
- {
- int x = 11;
- double y = 11.5;
-
- defining::T1 t1;
- t1.vfun(x, y);
-
- int f = 13;
- f = t1.fun(x, y);
- printf("main::f: %d\n", f);
- double d = 13.37;
- d = t1.fun2(x, y);
- printf("main::d: %f\n", d);
- defining::S s = defining::T1::funend(x, y);
- printf("main::s.z: %d\n", s.z ? 1 : 0);
- }
-
- // nondefining redecl
- {
- int x = 12;
- double y = 12.5;
-
- nondefining::T1 t1;
- t1.vfun(x, y);
-
- int f = 13;
- f = t1.fun(x, y);
- printf("main::f: %d\n", f);
- double d = 13.37;
- d = t1.fun2(x, y);
- printf("main::d: %f\n", d);
- nondefining::S s = nondefining::T1::funend(x, y);
- printf("main::s.z: %d\n", s.z ? 1 : 0);
- }
- return 0;
-}
-
-namespace defining
-{
- S T1::funend(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]]
- {
- printf("funend::x: %d\n", x);
- S s;
- s.z = true;
- return s;
- }
-}
-
-namespace nondefining
-{
- S T1::funend(int m, double n)
- [[ pre: x < 0 ]]
- [[ pre: m < 0 ]];
-
- S T1::funend(int m, double n)
- {
- printf("funend::x: %d\n", x);
- S s;
- s.z = true;
- return s;
- }
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function defining::T1::vfun at .*.C:25: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::vfun at .*.C:26: .*(\n|\r\n|\r)" }
-// { dg-output "vfun::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun at .*.C:32: .*(\n|\r\n|\r)" }
-// { dg-output "fun::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::f: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun2 at .*.C:39: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun2 at .*.C:40: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun2 at .*.C:41: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::fun2 at .*.C:42: .*(\n|\r\n|\r)" }
-// { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
-// { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::funend at .*.C:138: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function defining::T1::funend at .*.C:139: .*(\n|\r\n|\r)" }
-// { dg-output "funend::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::s.z: 1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::vfun at .*.C:68: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::vfun at .*.C:69: .*(\n|\r\n|\r)" }
-// { dg-output "vfun::x: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun at .*.C:71: .*(\n|\r\n|\r)" }
-// { dg-output "fun::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::f: 10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:73: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:74: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:75: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:76: .*(\n|\r\n|\r)" }
-// { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
-// { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::funend at .*.C:151: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function nondefining::T1::funend at .*.C:152: .*(\n|\r\n|\r)" }
-// { dg-output "funend::x: 10(\n|\r\n|\r)" }
-// { dg-output "main::s.z: 1(\n|\r\n|\r)" }
-
+++ /dev/null
-// test that contracts can be added during (defining) friend declarations
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-struct T;
-
-struct S
-{
- int now(int a, T *t) [[ pre: a > 0 ]] [[ pre: x < 0 ]];
-
-
- int x{-1};
-};
-
-int now(int a, T *t) [[ pre: a > 0 ]];
-int later(int a, T *t);
-int both(int a, T *t) [[ pre: a > 0 ]];
-
-struct T
-{
- friend int now(int a, T *t);
- friend int later(int a, T *t) [[ pre: a > 0 ]] [[ pre: t->pri > 0 ]]
- {
- printf("later: a: %d, t->pri: %d\n", a, t->pri);
- return -a * t->pri;
- }
- friend int both(int a, T *t) [[ pre: a > 0 ]]
- {
- printf("both: a: %d, t->pri: %d\n", a, t->pri);
- return -a * t->pri;
- }
-
-
- friend int S::now(int a, T *t);
-
- friend int hidden(int a, T *t) [[ pre: a > 0 ]] [[ pre: t->pri > 0 ]]
- {
- printf("hidden: a: %d, t->pri: %d\n", a, t->pri);
- return -a * t->pri;
- }
- friend int hidden2(int a, T *t) [[ pre: a > 0 ]] [[ pre: t->pri > 0 ]]
- {
- printf("hidden2: a: %d, t->pri: %d\n", a, t->pri);
- return -a * t->pri;
- }
-
- int x{1};
- private:
- int pri{-10};
-};
-
-int hidden2(int a, T *t) [[ pre: a > 0 ]] [[ pre: t->pri > 0 ]];
-
-int S::now(int a, T *t)
-{
- printf("S::now: a: %d, t->pri: %d\n", a, t->pri);
- return -a * t->pri;
-}
-
-int now(int a, T *t)
-{
- printf("now: a: %d, t->pri: %d\n", a, t->pri);
- return -a * t->pri;
-}
-
-int main(int, char**)
-{
- T t;
- S s;
- s.now(-10, &t);
-
- now(-20, &t);
- later(-21, &t);
- both(-22, &t);
- hidden(-23, &t);
- hidden2(-24, &t);
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function S::now at .*.C:9: .*(\n|\r\n|\r)" }
-// { dg-output "S::now: a: -10, t->pri: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function now at .*.C:15: .*(\n|\r\n|\r)" }
-// { dg-output "now: a: -20, t->pri: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function later at .*.C:22: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function later at .*.C:22: .*(\n|\r\n|\r)" }
-// { dg-output "later: a: -21, t->pri: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function both at .*.C:27: .*(\n|\r\n|\r)" }
-// { dg-output "both: a: -22, t->pri: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function hidden at .*.C:36: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function hidden at .*.C:36: .*(\n|\r\n|\r)" }
-// { dg-output "hidden: a: -23, t->pri: -10(\n|\r\n|\r)" }
-// { dg-output "contract violation in function hidden2 at .*.C:41: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function hidden2 at .*.C:41: .*(\n|\r\n|\r)" }
-// { dg-output "hidden2: a: -24, t->pri: -10(\n|\r\n|\r)" }
-
+++ /dev/null
-// test that contracts are matched on friend decls when the type is complete
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-struct T;
-
-int both(int x, T *t) [[ pre: x > 0 ]] { return 0; }
-int both2(int x, T *t) [[ pre: x > 0 ]];
-
-template<typename Z>
-int fn(int x, Z *z) [[ pre: x > 0 ]];
-
-template<typename Z>
-int fn2(int x, Z *z);
-
-template<typename Z>
-int fn3(int x, Z *z) [[ pre: x > 0 ]];
-
-template<>
-int fn3<T>(int x, T *z) [[ pre: x > 1 ]];
-
-struct T
-{
- friend int both2(int x, T *t) [[ pre: x > 1 ]] // { dg-error "mismatched" }
- {
- return 0;
- }
-
- friend int hidden(int x, T *t)
- [[ pre: x > 1 ]] [[ pre: t->pri > 0 ]]
- {
- return x;
- }
-
- /* cannot define friend spec, so we never get to matching contracts
- friend int fn<T>(int x, T *t)
- [[ pre: t->pri > 0 ]] { return 0; } // error defining explicit spec friend
- */
-
- // bad, general contracts must match general
- template<typename Z>
- friend int fn(int x, Z *z)
- [[ pre: z->pri > 1 ]] { return 0; } // { dg-error "mismatched" }
-
- // fine, can add contracts
- template<typename Z>
- friend int fn2(int x, Z *z)
- [[ pre: z->pri > 1 ]] { return 0; } // { dg-bogus "mismatched" }
-
- /* cannot declare without definition, so dup friend can't occur:
- friend int dup(int x, T *t)
- [[ pre: t->pri > 0 ]]; // error non-defining friend with contracts
- friend int dup(int x, T *t)
- [[ pre: t->pri > 1 ]]; // error non-defining friend with contracts
- */
-
- int x{1};
- private:
- int pri{-10};
-};
-
-int hidden(int x, T *t)
- [[ pre: x > 0 ]] [[ pre: t->pri > 1 ]]; // { dg-error "mismatched" }
-
+++ /dev/null
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-template<typename T>
-[[z]]
-[[nodiscard]]
-T fun(T n)
- [[ pre: n > 0 ]]
- [[ post r: r > 0 ]] // { dg-warning ".z. attribute.*ignored" }
-{
- return n;
-}
-
-int main(int, char**) {
- fun(-5); // { dg-warning "ignoring return value" }
- fun(-5.3); // { dg-warning "ignoring return value" }
- return 0;
-}
-
+++ /dev/null
-// basic test to ensure pre contracts work for free template specializations
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-template<typename T>
-int body(int a)
- [[ pre: a > 0 ]]
-{
- T t = a * 2.5;
- return t;
-}
-
-template<>
-int body<double>(int a)
- [[ pre: a > 0 ]]
-{
- double t = a * 3.3;
- return t;
-}
-
-template<typename T>
-int none(int a)
- [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<>
-int none<double>(int a)
- [[ pre: a > 0 ]]
-{
- return a - 100;
-}
-
-template<typename T>
-int arg0(T t)
- [[ pre: t > 0 ]]
-{
- return -t - 10;
-}
-
-template<>
-int arg0<double>(double t)
- [[ pre: t > 0 ]]
-{
- return -t + 10;
-}
-
-template<typename T>
-int arg1(int a, T t)
- [[ pre: a > 0 ]]
- [[ pre: t > 0 ]]
-{
- return -t * a;
-}
-
-template<>
-int arg1<double>(int a, double t)
- [[ pre: a > 0 ]]
- [[ pre: t > 0 ]]
-{
- return -t * a + 17;
-}
-
-template<typename T>
-T ret(int a)
- [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<>
-double ret<double>(int a)
- [[ pre: a > 0 ]]
-{
- return -a * 3.3;
-}
-
-
-int main(int, char**)
-{
- printf("%d\n", body<int>(-1));
- printf("%d\n", body<double>(-1));
- printf("%d\n", none<int>(-1));
- printf("%d\n", none<double>(-1));
- printf("%d\n", arg0(-1));
- printf("%d\n", arg0(-1.0));
- printf("%d\n", arg1(-3, -1));
- printf("%d\n", arg1(-3, -1.0));
- printf("%d\n", (int)ret<int>(-1));
- printf("%d\n", (int)ret<double>(-1));
- printf("%f\n", ret<double>(-1));
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function body<int> at .*.C:8: .*(\n|\r\n|\r)" }
-// { dg-output "-2(\n|\r\n|\r)" }
-// { dg-output "contract violation in function body<double> at .*.C:16: .*(\n|\r\n|\r)" }
-// { dg-output "-3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function none<int> at .*.C:24: .*(\n|\r\n|\r)" }
-// { dg-output "1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function none<double> at .*.C:31: .*(\n|\r\n|\r)" }
-// { dg-output "-101(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg0<int> at .*.C:38: .*(\n|\r\n|\r)" }
-// { dg-output "-9(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg0<double> at .*.C:45: .*(\n|\r\n|\r)" }
-// { dg-output "11(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg1<int> at .*.C:52: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg1<int> at .*.C:53: .*(\n|\r\n|\r)" }
-// { dg-output "-3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg1<double> at .*.C:60: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function arg1<double> at .*.C:61: .*(\n|\r\n|\r)" }
-// { dg-output "14(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ret<int> at .*.C:68: .*(\n|\r\n|\r)" }
-// { dg-output "1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ret<double> at .*.C:75: .*(\n|\r\n|\r)" }
-// { dg-output "3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ret<double> at .*.C:75: .*(\n|\r\n|\r)" }
-// { dg-output "3.300000(\n|\r\n|\r)" }
-
+++ /dev/null
-// basic test to ensure contracts work for class and member specializations
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on -fsigned-char" }
-#include <cstdio>
-
-// template specializations can have differing contracts
-template<typename T>
-int body(int a)
- [[ pre: a > 0 ]]
-{
- T t = a * 2.5;
- return t;
-}
-
-template<>
-int body<double>(int a)
- [[ pre: a > 1 ]]
-{
- double t = a * 3.3;
- return t;
-}
-
-template<typename T>
-int none(int a)
- [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<>
-int none<double>(int a)
- [[ pre: a > 1 ]]
-{
- return a - 100;
-}
-
-template<typename T>
-int arg0(T t)
- [[ pre: t > 0 ]]
-{
- return -t - 10;
-}
-
-template<>
-int arg0<double>(double t)
- [[ pre: t > 1 ]]
-{
- return -t + 10;
-}
-
-template<typename T>
-int arg1(int a, T t)
- [[ pre: a > 0 ]]
- [[ pre: t > 0 ]]
-{
- return -t * a;
-}
-
-template<>
-int arg1<double>(int a, double t)
- [[ pre: a > 1 ]]
- [[ pre: t > 1 ]]
-{
- return -t * a + 17;
-}
-
-template<typename T>
-T ret(int a)
- [[ pre: a > 0 ]]
-{
- return -a;
-}
-
-template<>
-double ret<double>(int a)
- [[ pre: a > 1 ]]
-{
- return -a * 3.3;
-}
-
-// template specializations can have no contracts
-template<typename T>
-int g1(T t) [[ pre: t > 0 ]]
-{
- return (int)t;
-}
-
-template<>
-int g1<double>(double t)
-{
- return (int)t;
-}
-
-// template specializations can have no contracts in the first decl but add
-// them later
-template<typename T>
-int g2(T t) [[ pre: t > 0 ]]
-{
- return (int)t;
-}
-
-template<>
-int g2<double>(double t);
-
-template<>
-int g2<double>(double t)
- [[ pre: t < 0 ]]
-{
- return (int)t;
-}
-
-template<>
-int g2<char>(char t)
- [[ pre: t < 'c' ]]
-{
- return (int)t;
-}
-
-// contracts can be different on the general template, partial and full specs
-template<typename T, typename S>
-struct G3
-{
- void f(T t, S s)
- [[ pre: t > 0 ]]
- [[ pre: s > 0 ]]
- {
- printf ("G3 general T S\n");
- }
-};
-
-template<typename S>
-struct G3<int, S>
-{
- void f(int t, S s);
-};
-
-template<typename S>
-void G3<int, S>::f(int t, S s)
- [[ pre: t > 1 ]]
- [[ pre: s > 1 ]]
-{
- printf ("G3 partial int S\n");
-}
-
-template<>
-void G3<int, double>::f(int t, double s)
- [[ pre: t > 2 ]]
- [[ pre: s > 2 ]]
-{
- printf ("G3 full int double\n");
-}
-
-struct C
-{
- bool operator>(int rhs) { return false; }
-};
-
-// deletes contracts
-template<>
-void G3<int, C>::f(int t, C s);
-
-template<>
-void G3<int, C>::f(int t, C s)
-{
- printf ("G3 full int C\n");
-};
-
-// specialized ctors
-template<typename T, typename S>
-struct G4
-{
- G4(T t, S s)
- [[ pre: t > 0 ]]
- [[ pre: s > 0 ]]
- [[ post: x > 0 ]]
- {
- printf ("G4 general T S\n");
- return;
- }
- int x{-1};
-};
-
-template<typename S>
-struct G4<char, S>
-{
- G4(char t, S s)
- [[ pre: t > 'c' ]]
- [[ pre: s > 3 ]]
- [[ post: x2 > 3 ]]
- {
- printf ("G4 partial char S\n");
- return;
- }
- int x2{-1};
-};
-
-template<>
-G4<double, double>::G4(double, double)
-{
- printf ("G4 full double double\n");
- return;
-}
-
-template<>
-G4<double, char>::G4(double a, char b)
- [[ pre: a > 0 ]]
- [[ pre: b > 'b' ]]
- [[ post: x > 1 ]]
-{
- printf ("G4 full double char\n");
- return;
-}
-
-// crossover of template classes and template members ok
-template<typename T, typename S>
-struct G5
-{
- template<typename P>
- void f(T t, S s, P r)
- [[ pre: t > 0 ]]
- [[ pre: s > 0 ]]
- [[ pre: r > 0 ]]
- {
- printf ("G5 gen T S, f gen R\n");
- }
-};
-
-template<typename S>
-struct G5<char, S>
-{
- template<typename R>
- void f(char x, S y, R z)
- [[ pre: x > 'z' ]]
- [[ pre: y > 1 ]]
- [[ pre: z > 1 ]]
- {
- printf ("G5 partial char S, f gen R\n");
- }
-};
-
-template<>
-template<typename Q>
-void G5<double, double>::f(double a, double b, Q c)
- [[ pre: a > 2 ]]
- [[ pre: b > 2 ]]
- [[ pre: c > 2 ]]
-{
- printf ("G5 full double double, f gen R\n");
-}
-
-int main(int, char**)
-{
- printf("%d\n", body<int>(-1));
- printf("%d\n", body<double>(-1));
- printf("%d\n", none<int>(-1));
- printf("%d\n", none<double>(-1));
- printf("%d\n", arg0(-1));
- printf("%d\n", arg0(-1.0));
- printf("%d\n", arg1(-3, -1));
- printf("%d\n", arg1(-3, -1.0));
- printf("%d\n", (int)ret<int>(-1));
- printf("%d\n", (int)ret<double>(-1));
- printf("%f\n", ret<double>(-1));
-
- printf("%d\n", g1(-1));
- printf("%d\n", g1(-1.0));
-
- printf("%d\n", g2(-1));
- printf("%d\n", g2(1.0));
- printf("%d\n", g2('d'));
-
- G3<double, double> g3_gen;
- G3<int, int> g3_partial;
- G3<int, double> g3_full;
- g3_gen.f(-1.0, -1.0); // general
- g3_partial.f(-2, -2); // partial spec
- g3_full.f(-3, -3.0); // full spec
-
- G3<char, char> g3_gen2;
- G3<int, char> g3_partial2;
- g3_gen2.f((char)-1, (char)-1);
- g3_partial2.f(-1, (char)-1);
-
- G3<int, C> g3_full2;
- g3_full2.f(5, C{});
- g3_full2.f(-5, C{});
-
- G4 g4_gen{-1, -1};
- G4 g4_full1{-1.0, -1.0};
- G4 g4_full2{-1.0, (char)'b'};
- G4 g4_partial{(char)'c', -5};
-
- G5<int, int> g5_gen;
- g5_gen.f(-1, -1, -2);
- g5_gen.f(-1, -1, -2.0);
-
- G5<char, int> g5_part;
- g5_part.f('a', -1, -2);
- g5_part.f('a', -1, -2.1);
-
- G5<double, double> g5_full;
- g5_full.f(-1.0, -1.0, -2);
- g5_full.f(-1.0, -1.0, -2.1);
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output {contract violation in function body<int> at .*:9: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {-2(\n|\r\n|\r)} }
-// { dg-output {contract violation in function body<double> at .*:17: a > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {-3(\n|\r\n|\r)} }
-// { dg-output {contract violation in function none<int> at .*:25: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {1(\n|\r\n|\r)} }
-// { dg-output {contract violation in function none<double> at .*:32: a > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {-101(\n|\r\n|\r)} }
-// { dg-output {contract violation in function arg0<int> at .*:39: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {-9(\n|\r\n|\r)} }
-// { dg-output {contract violation in function arg0<double> at .*:46: t > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {11(\n|\r\n|\r)} }
-// { dg-output {contract violation in function arg1<int> at .*:53: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function arg1<int> at .*:54: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {-3(\n|\r\n|\r)} }
-// { dg-output {contract violation in function arg1<double> at .*:61: a > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function arg1<double> at .*:62: t > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {14(\n|\r\n|\r)} }
-// { dg-output {contract violation in function ret<int> at .*:69: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {1(\n|\r\n|\r)} }
-// { dg-output {contract violation in function ret<double> at .*:76: a > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {3(\n|\r\n|\r)} }
-// { dg-output {contract violation in function ret<double> at .*:76: a > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {3.300000(\n|\r\n|\r)} }
-// { dg-output {contract violation in function g1<int> at .*:83: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {-1(\n|\r\n|\r)} }
-// { dg-output {-1(\n|\r\n|\r)} }
-// { dg-output {contract violation in function g2<int> at .*:97: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {-1(\n|\r\n|\r)} }
-// { dg-output {contract violation in function g2<double> at .*:107: t < 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {1(\n|\r\n|\r)} }
-// { dg-output {contract violation in function g2<char> at .*:114: t < 'c'(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {100(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<double, double>::f at .*:124: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<double, double>::f at .*:125: s > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G3 general T S(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<int, int>::f at .*:139: t > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<int, int>::f at .*:140: s > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G3 partial int S(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<int, double>::f at .*:147: t > 2(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<int, double>::f at .*:148: s > 2(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G3 full int double(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<char, char>::f at .*:124: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<char, char>::f at .*:125: s > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G3 general T S(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<int, char>::f at .*:139: t > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G3<int, char>::f at .*:140: s > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G3 partial int S(\n|\r\n|\r)} }
-// { dg-output {G3 full int C(\n|\r\n|\r)} }
-// { dg-output {G3 full int C(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<int, int>::G4 at .*:173: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<int, int>::G4 at .*:174: s > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G4 general T S(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<int, int>::G4 at .*:175: x > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G4 full double double(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<double, char>::G4 at .*:206: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<double, char>::G4 at .*:207: b > 'b'(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G4 full double char(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<double, char>::G4 at .*:208: x > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<char, int>::G4 at .*:187: t > 'c'(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<char, int>::G4 at .*:188: s > 3(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G4 partial char S(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G4<char, int>::G4 at .*:189: x2 > 3(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, int>::f<int> at .*:220: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, int>::f<int> at .*:221: s > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, int>::f<int> at .*:222: r > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G5 gen T S, f gen R(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, int>::f<double> at .*:220: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, int>::f<double> at .*:221: s > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, int>::f<double> at .*:222: r > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G5 gen T S, f gen R(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<char, int>::f<int> at .*:233: x > 'z'(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<char, int>::f<int> at .*:234: y > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<char, int>::f<int> at .*:235: z > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G5 partial char S, f gen R(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<char, int>::f<double> at .*:233: x > 'z'(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<char, int>::f<double> at .*:234: y > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<char, int>::f<double> at .*:235: z > 1(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G5 partial char S, f gen R(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<double, double>::f<int> at .*:244: a > 2(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<double, double>::f<int> at .*:245: b > 2(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<double, double>::f<int> at .*:246: c > 2(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G5 full double double, f gen R(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<double, double>::f<double> at .*:244: a > 2(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<double, double>::f<double> at .*:245: b > 2(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<double, double>::f<double> at .*:246: c > 2(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G5 full double double, f gen R(\n|\r\n|\r)} }
+++ /dev/null
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-template<typename T, typename S>
-struct G5
-{
- template<typename R>
- void f(T t, S s, R r)
- [[ pre: t > 0 ]] [[ pre: s > 0 ]] [[ pre: r > 0 ]]
- {
- printf ("G5 gen T S, f gen R\n");
- }
-};
-
-// specializations can remove contracts
-template<>
-template<typename R>
-void G5<double, double>::f(double a, double b, R c)
-{
- printf ("G5 full double double, f gen R\n");
-}
-
-int main(int, char**) {
- G5<double, double> g5_full;
- g5_full.f(-1.0, -1.0, -2);
- g5_full.f(-1.0, -1.0, -2.1);
-
- G5<int, double> g5_gen;
- g5_gen.f(-1, -1.0, -2);
- g5_gen.f(-1, -1.0, -2.1);
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output {G5 full double double, f gen R(\n|\r\n|\r)} }
-// { dg-output {G5 full double double, f gen R(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, double>::f<int> at .*:10: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, double>::f<int> at .*:10: s > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, double>::f<int> at .*:10: r > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G5 gen T S, f gen R(\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, double>::f<double> at .*:10: t > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, double>::f<double> at .*:10: s > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function G5<int, double>::f<double> at .*:10: r > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {G5 gen T S, f gen R(\n|\r\n|\r)} }
+++ /dev/null
-// generic assert contract parsing checks
-// check omitted, 'default', 'audit', and 'axiom' contract levels parse
-// check that all concrete semantics parse
-// check omitted, '%default' contract roles parse
-// ensure that an invalid contract level 'invalid' errors
-// ensure that a predicate referencing an undefined variable errors
-// ensure that a missing colon after contract level errors
-// ensure that an invalid contract role 'invalid' errors
-// ensure that a missing colon after contract role errors
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-static_assert (__cpp_contracts >= 201906);
-static_assert (__cpp_contracts_literal_semantics >= 201906);
-static_assert (__cpp_contracts_roles >= 201906);
-
-int main()
-{
- int x;
-
- [[assert: x >= 0]];
- [[assert default: x < 0]];
- [[assert audit: x == 0]];
- [[assert axiom: x == 1]];
-
- [[assert: x > 0 ? true : false]];
- [[assert: x < 0 ? true : false]];
-
- [[assert: x = 0]]; // { dg-error "expected .]. before .=. token" }
-
- [[assert ignore: x >= 0]];
- [[assert assume: x >= 0]];
- [[assert check_never_continue: x >= 0]];
- [[assert check_maybe_continue: x >= 0]];
-
- [[assert %default: x >= 0]];
- [[assert default %default: x < 0]];
- [[assert audit %default: x == 0]];
- [[assert axiom %default: x == 1]];
-
- [[assert check_always_continue: x >= 0]]; // { dg-error "expected contract level" }
- [[assert invalid: x == 0]]; // { dg-error "expected contract level" }
- [[assert: y == 0]]; // { dg-error ".y. was not declared in this scope" }
- [[assert default x == 0]]; // { dg-error "expected .:. before .x." }
- [[assert %default x >= 0]]; // { dg-error "expected .:. before .x." }
-
- [[assert %invalid: x >= 0]]; // TODO: optional warning?
- return 0;
-}
+++ /dev/null
-// general checks to ensure that contract violations are generated during
-// runtime when appropriate
-// each check also validates the expected file name, line number, function,
-// predicate, and contract level are included in the violation_info object
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-build-level=audit -fcontract-continuation-mode=on" }
-
-namespace tns
-{
- int fun()
- {
- int x = 1;
- [[ assert: x < 0 ]];
- return 0;
- }
- int fun2();
-
- struct TestType
- {
- static int fun();
- static int fun2()
- {
- int x = 1;
- [[ assert: x < 0 ]];
- return 0;
- }
- };
-}
-
-int tns::fun2()
-{
- int x = 1;
- [[ assert: x < 0 ]];
- return 0;
-}
-
-int tns::TestType::fun()
-{
- int x = 1;
- [[ assert: x < 0 ]];
- return 0;
-}
-
-int main()
-{
- int x = 100;
- [[assert: x < 0]];
- [[assert default: x < 1]];
- [[assert audit: x < 2]];
-// contract_violation.line_number() may eventually come from
-// std::source_location which *is* affected by the #line macro; our current
-// implementation conforms to this so we've included it as a check
-#line 100
- [[assert: x < 3]];
- [[assert axiom: x < 4]];
-
- tns::fun();
- tns::fun2();
-
- tns::TestType::fun();
- tns::TestType::fun2();
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function main at .*.C:47: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function main at .*.C:48: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function main at .*.C:49: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function main at .*.C:100: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function tns::fun at .*.C:13: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function tns::fun2 at .*.C:33: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function tns::TestType::fun at .*.C:40: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function tns::TestType::fun2 at .*.C:24: .*(\n|\r\n|\r)" }
-
+++ /dev/null
-// ensure that assert contract predicates that are not convertible to bool
-// generate an error
-// ensure the same for instatiated template functions
-// ensure the same for non-instatiated template functions when the predicate
-// is not dependent on the template parameters
-// ensure template parameter dependent, potentially non-boolean, contract
-// predicates do not generate an error if never instatiated
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-void fun()
-{
- return;
-}
-
-template<typename T>
-void fun2(T a)
-{
- [[assert: fun()]]; // { dg-error "could not convert|in argument" }
-}
-
-template<typename T>
-void fun3(T a)
-{
- [[assert: fun()]]; // { dg-error "could not convert|in argument" }
-}
-
-template<typename T>
-void fun4(T a)
-{
- [[assert: a.fun()]];
-}
-
-struct test
-{
- void fun() { }
- void fun2() { }
-};
-
-template<typename T>
-void fun5(T a)
-{
- [[assert: a.fun2()]]; // { dg-error "could not convert" }
-}
-
-struct VoidFun
-{
- void fun() { }
-};
-struct BoolFun
-{
- bool fun() { return true; }
-};
-
-template<typename T>
-void fun6(T a)
-{
- [[ assert: a.fun() ]]; // { dg-error "could not convert" }
-}
-
-template void fun6(VoidFun);
-
-template<typename T>
-void fun7(T a)
-{
- [[ assert: a.fun() ]];
-}
-
-template void fun7(BoolFun);
-
-struct ImplicitBool
-{
- operator bool() { return true; }
-};
-struct ExplicitBool
-{
- explicit operator bool() { return true; }
-};
-
-template<typename T>
-void fun8(T a)
-{
- [[ assert: T() ]];
-}
-
-template void fun8(ImplicitBool);
-template void fun8(ExplicitBool);
-
-void fun9()
-{
- [[ assert: ImplicitBool() ]];
- [[ assert: ExplicitBool() ]];
-}
-
-int main()
-{
- [[assert: fun()]]; // { dg-error "could not convert" }
- fun2(1);
-
- test t;
- fun5(t);
- return 0;
-}
+++ /dev/null
-// ensure that constants for contract levels are inserted into the binary when
-// used and omitted when the runtime check is not generated
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts -fcontract-build-level=default" }
-// { dg-final { scan-assembler-not "audit" } }
-// { dg-final { scan-assembler "default" } }
-
-int main()
-{
- int x = 1;
- [[assert: x < 0]];
- [[assert default: x < 0]];
- [[assert audit: x < 0]];
- return 0;
-}
+++ /dev/null
-// ensure that passing asserts do not affect constexpr functions
-// ensure that failing asserts generate an error in a constexpr function
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-constexpr int wfun(int a) {
- [[assert: a > 0]];
- return a;
-}
-
-constexpr int ffun(int a) {
- [[assert: a > 0]];
- return a;
-}
-
-template<typename T>
-constexpr int tfun(T a) {
- [[assert: a > 0]];
- return a;
-}
-
-template<typename T>
-constexpr int wtfun(T a) {
- [[assert: a > 0]];
- return a;
-}
-
-template<typename T>
-constexpr int ftfun(T a) {
- [[assert: a > 0]];
- return a;
-}
-
-constexpr int explicitfn(int a) {
- [[assert ignore: a > 0]];
- [[assert check_never_continue: a > 0]];
- return a;
-}
-
-int main(int, char **) {
- constexpr int a = wfun(10);
- constexpr int b = ffun(-10); // { dg-message "in .constexpr. expansion" }
- // { dg-error "contract predicate" "" { target *-*-* } 12 }
- constexpr int c = wtfun(10);
- constexpr int d = ftfun(-10); // { dg-message "in .constexpr. expansion" }
- // { dg-error "contract predicate" "" { target *-*-* } 30 }
- constexpr int e = explicitfn(-10); // { dg-message "in .constexpr. expansion" }
- // { dg-error "contract predicate" "" { target *-*-* } 36 }
- return 0;
-}
-
+++ /dev/null
-// ensure that exceptions thrown inside a custom contract violation handler
-// are catchable up the call stack
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <iostream>
-#include <experimental/contract>
-
-void handle_contract_violation(const std::experimental::contract_violation &violation) {
- std::cerr << "custom std::handle_contract_violation called:"
- << " " << violation.line_number()
- << " " << violation.file_name()
- << std::endl;
- throw -(int)violation.line_number();
-}
-
-int fun() {
- int x = 0;
- [[ assert: x < 0 ]];
- return 0;
-}
-
-int fun3() {
- fun();
- return 2;
-}
-
-int main(int, char**) {
- try {
- int x = 0;
- [[ assert: x < 0 ]];
- } catch(int &ex) {
- std::cerr << "synth caught direct: " << ex << std::endl;
- }
-
- try {
- fun();
- } catch(int &ex) {
- std::cerr << "synth caught indirect: " << ex << std::endl;
- }
-
- try {
- fun3();
- } catch(int &ex) {
- std::cerr << "synth caught double indirect: " << ex << std::endl;
- }
-
- std::cerr << "end main" << std::endl;
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
-// { dg-output "custom std::handle_contract_violation called: 30 .*/contracts14.C(\n|\r\n|\r)" }
-// { dg-output "synth caught direct: -30(\n|\r\n|\r)" }
-// { dg-output "custom std::handle_contract_violation called: 18 .*/contracts14.C(\n|\r\n|\r)" }
-// { dg-output "synth caught indirect: -18(\n|\r\n|\r)" }
-// { dg-output "custom std::handle_contract_violation called: 18 .*/contracts14.C(\n|\r\n|\r)" }
-// { dg-output "synth caught double indirect: -18(\n|\r\n|\r)" }
-// { dg-output "end main" }
-
+++ /dev/null
-// ensure that exceptions thrown inside a custom contract violation handler
-// are not catchable up the call stack when failing in a noexcept function
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <iostream>
-#include <experimental/contract>
-
-void handle_contract_violation(const std::experimental::contract_violation &violation) {
- std::cerr << "custom std::handle_contract_violation called:"
- << " " << violation.line_number()
- << " " << violation.file_name()
- << std::endl;
- throw -(int)violation.line_number();
-}
-
-int fun() noexcept {
- int x = 0;
- [[ assert: x < 0 ]];
- return 0;
-}
-
-int fun3() {
- fun();
- return 2;
-}
-
-int main(int, char**) {
- try {
- int x = 0;
- [[ assert: x < 0 ]];
- } catch(int &ex) {
- std::cerr << "synth caught direct: " << ex << std::endl;
- }
-
- try {
- fun();
- } catch(int &ex) {
- std::cerr << "synth caught indirect: " << ex << std::endl;
- }
-
- try {
- fun3();
- } catch(int &ex) {
- std::cerr << "synth caught double indirect: " << ex << std::endl;
- }
-
- std::cerr << "end main" << std::endl;
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
-// { dg-output "custom std::handle_contract_violation called: 30 .*/contracts15.C(\n|\r\n|\r)" }
-// { dg-output "synth caught direct: -30(\n|\r\n|\r)" }
-// { dg-output "custom std::handle_contract_violation called: 18 .*/contracts15.C(\n|\r\n|\r)" }
-// { dg-output "terminate called after throwing an instance of .int.(\n|\r\n|\r)" }
-// { dg-shouldfail "throwing in noexcept" }
-
+++ /dev/null
-// ensure that exceptions thrown inside a custom contract violation handler
-// are not catchable up the call stack even when continue mode is off
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-#include <iostream>
-#include <experimental/contract>
-
-void handle_contract_violation(const std::experimental::contract_violation &violation) {
- std::cerr << "custom std::handle_contract_violation called:"
- << " " << violation.line_number()
- << " " << violation.file_name()
- << std::endl;
- throw -(int)violation.line_number();
-}
-
-int fun() {
- int x = 0;
- [[ assert: x < 0 ]];
- return 0;
-}
-
-int main(int, char**) {
- try {
- fun();
- } catch(int &ex) {
- std::cerr << "synth caught indirect: " << ex << std::endl;
- }
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
-// { dg-output "custom std::handle_contract_violation called: 18 .*/contracts16.C(\n|\r\n|\r)" }
-// { dg-output "synth caught indirect: -18(\n|\r\n|\r)" }
-
+++ /dev/null
-// ensure that exceptions thrown inside a custom contract violation handler
-// are not catchable up the call stack when continue mode is off and the
-// assert fails in a noexcept function
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-#include <iostream>
-#include <experimental/contract>
-
-void handle_contract_violation(const std::experimental::contract_violation &violation) {
- std::cerr << "custom std::handle_contract_violation called:"
- << " " << violation.line_number()
- << " " << violation.file_name()
- << std::endl;
- throw -violation.line_number();
-}
-
-int fun() noexcept {
- int x = 0;
- [[ assert: x < 0 ]];
- return 0;
-}
-
-int main(int, char**) {
- try {
- fun();
- } catch(int &ex) {
- std::cerr << "synth caught indirect: " << ex << std::endl;
- }
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
-// { dg-output "custom std::handle_contract_violation called: 19 .*/contracts17.C(\n|\r\n|\r)" }
-// { dg-shouldfail "throwing in noexcept" }
-
+++ /dev/null
-// check that a valid program using assertions compiles and runs
-// ensure an axiom with a failing predicate doesn't prevent a successful run
-// (axiom level contracts are never checked at runtime)
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-role=custom:never,ignore,ignore" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int main()
-{
- int x = 1;
- [[assert axiom: x < 0]];
- [[assert %custom: x > 0]];
- [[assert audit %custom: x < 0]];
- return 0;
-}
-
+++ /dev/null
-// check that a valid program using assertions compiles and runs
-// ensure an axiom with a failing predicate doesn't prevent a successful run
-// (axiom level contracts are never checked at runtime)
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-role=custom:maybe,maybe,ignore" }
-
-int main()
-{
- int x = 10;
- [[assert axiom: x < 0]];
- [[assert %custom: x < 0]];
- [[assert audit %custom: x < 1]];
- [[assert axiom %custom: x < 1]];
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function main at .*.C:11: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function main at .*.C:12: .*(\n|\r\n|\r)" }
-
+++ /dev/null
-// check that a valid program using assertions compiles and runs
-// ensure an axiom with a failing predicate doesn't prevent a successful run
-// (axiom level contracts are never checked at runtime)
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int main()
-{
- int x = 1;
- [[assert axiom: x < 0]];
- [[assert default: x > 0]];
- return 0;
-}
+++ /dev/null
-// test that contract attributes cause errors pre-c++2a
-// { dg-do compile { target c++17_only } }
-
-int fun(int a)
- [[ pre: a > 0 ]] // { dg-error "contracts are only available with .-fcontracts." }
- [[ post r: r < 0 ]] // { dg-error "contracts are only available with .-fcontracts." }
-{
- [[ assert: a != 0 ]]; // { dg-error "contracts are only available with .-fcontracts." }
- return -a;
-}
-
+++ /dev/null
-// ensure a default level assert with a failing predicate does not generate an
-// error during runtime when the contracts mode is off
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-mode=off" }
-// { dg-output "returning from main" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-#include <cstdio>
-
-int constexpr f()
-{
- constexpr int x = 1;
- [[assert default: x < 0]];
- return x;
-}
-
-template<typename T> int k()
-{
- int x = 1;
- [[assert default: x < 0]];
- return x;
-}
-
-
-int main()
-{
- int x = 1;
- [[assert default: x < 0]];
- constexpr int x2 = f();
- int x3 = k<int>();
-
- printf ("returning from main\n");
- return 0;
-}
+++ /dev/null
-// check that a valid program using assertions compiles and runs
-// ensure an axiom with a failing predicate doesn't prevent a successful run
-// (axiom level contracts are never checked at runtime)
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-semantic=default:never -fcontract-semantic=audit:ignore -fcontract-semantic=axiom:ignore" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int main()
-{
- int x = 1;
- [[assert axiom: x < 0]];
- [[assert: x > 0]];
- [[assert audit: x < 0]];
- return 0;
-}
-
+++ /dev/null
-// ensure that passing asserts do not affect constexpr functions
-// ensure that failing asserts generate an error at runtime in constexpr funcs
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-constexpr int wfun(int a) {
- [[assert: a > 0]];
- return a;
-}
-
-constexpr int ffun(int a) {
- [[assert: a > 0]];
- return a;
-}
-
-template<typename T>
-constexpr int tfun(T a) {
- [[assert: a > 0]];
- return a;
-}
-
-template<typename T>
-constexpr int wtfun(T a) {
- [[assert: a > 0]];
- return a;
-}
-
-template<typename T>
-constexpr int ftfun(T a) {
- [[assert: a > 0]];
- return a;
-}
-
-constexpr int explicitfn(int a) {
- [[assert ignore: a > 0]];
- [[assert check_maybe_continue: a > 0]];
- return a;
-}
-
-int main(int, char **) {
- int a = wfun(10);
- int b = ffun(-10);
- int c = wtfun(10);
- int d = ftfun(-10);
-
- int e = explicitfn(-10);
-
- int z = ftfun(-10.0);
-
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function ffun at .*.C:12: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ftfun<int> at .*.C:30: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function explicitfn at .*.C:36: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function ftfun<double> at .*.C:30: .*(\n|\r\n|\r)" }
-
+++ /dev/null
-// ensure a default level assert with a failing predicate generates an error
-// during runtime when the contract build level is default
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-// { dg-shouldfail "assert violation" }
-// { dg-output "contract violation in function main" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int main()
-{
- int x = 1;
- [[assert default: x < 0]];
- return 0;
-}
+++ /dev/null
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-#include <cstdio>
-
-struct S
-{
- template<typename T>
- S(T a) [[ pre: a > 0 ]] [[ pre: a > 10 ]];
-};
-
-template<typename T>
-S::S(T a)
-{
- printf ("S::S(T): %d\n", (int)a);
-}
-
-struct S1
-{
- template<typename T>
- S1(T a) [[ pre: a > 0 ]] [[ pre: a > 10 ]]
- {
- printf ("S1::S1(T): %d\n", (int)a);
- }
-};
-
-int main(int, char **) {
- S s{-1};
- S s2{-2.5};
-
- S1 s1_1{-3};
- S1 s1_2{-4.5};
- return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-// { dg-output "contract violation in function S::S<int> at .*.C:8: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S::S<int> at .*.C:8: .*(\n|\r\n|\r)" }
-// { dg-output "S::S.T.: -1(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S::S<double> at .*.C:8: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S::S<double> at .*.C:8: .*(\n|\r\n|\r)" }
-// { dg-output "S::S.T.: -2(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S1::S1<int> at .*.C:20: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S1::S1<int> at .*.C:20: .*(\n|\r\n|\r)" }
-// { dg-output "S1::S1.T.: -3(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S1::S1<double> at .*.C:20: .*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S1::S1<double> at .*.C:20: .*(\n|\r\n|\r)" }
-// { dg-output "S1::S1.T.: -4(\n|\r\n|\r)" }
-
+++ /dev/null
-// ensure an audit level assert with a failing predicate does not generate an
-// error during runtime when the contract build level is default
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int main()
-{
- int x = 1;
- [[assert audit: x < 0]];
- return 0;
-}
+++ /dev/null
-// ensure an audit level assert with a failing predicate generates an error
-// during runtime when the contract build level is audit
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-build-level=audit" }
-// { dg-shouldfail "assert violation" }
-// { dg-output "contract violation in function main" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int main()
-{
- int x = 1;
- [[assert audit: x < 0]];
- return 0;
-}
+++ /dev/null
-// ensure a default level assert with a failing predicate does not generate an
-// error during runtime when the contract build level is off
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-build-level=off" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int main()
-{
- int x = 1;
- [[assert default: x < 0]];
- return 0;
-}
+++ /dev/null
-// ensure a default level assert with a failing predicate does generates an
-// error during runtime but lets the program continue and complete
-// successfully when the contract build level is default but continuation on
-// contract failure is switched on
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-// { dg-output "contract violation in function main" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-int main()
-{
- int x = 1;
- [[assert default: x < 0]];
- return 0;
-}
+++ /dev/null
-// generic assert contract parsing checks
-// ensure that existing generalized attribute parsing is not intefered with
-// ensure that an assert contract cannot chain into an empty attribute list
-// ensure that an attribute list cannot chain into an assert contract
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts" }
-
-constexpr bool fun(int x) {
- return x < 0;
-}
-
-namespace tns {
- constexpr bool f(int x) {
- return x < 0;
- }
-}
-
-bool assert(int x) {
- return x < 0;
-}
-
-int main()
-{
- constexpr int x = 1;
- [[fun(x)]]; // { dg-warning "attributes at the beginning of statement are ignored" }
- [[fun(x), assert(x)]]; // { dg-warning "attributes at the beginning of statement are ignored" }
-
- [[assert default: fun(x), ]]; // { dg-error "expected ']'" }
- [[assert default: fun(x) ]];
-
- [[fun(x), assert default: fun(x)]]; // { dg-error "expected .]. before .default." }
- // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 }
- [[fun(x), assert: fun(x)]]; // { dg-error "expected .]. before .:. token" }
- // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 }
- [[fun(x), assert fun(x)]]; // { dg-error "expected .]. before .fun." }
- // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 }
- [[ using tns: f(x) ]]; // { dg-warning "attributes at the beginning of statement are ignored" }
- [[ using tns: f(x), assert default: fun(x) ]]; // { dg-error "expected .]. before .default." }
- // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 }
- [[ using tns: f(x), , default: fun(x) ]]; // { dg-error "expected .]. before .:." }
- // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 }
- return 0;
-}
+++ /dev/null
-// ensure that dependent and non-dependent asserts inside templated
-// functions parse without error whether the function is instatiated or not
-// ensure that assert contract checks are generated inside called templates
-// ensure that template functions can be used as assert predicates
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
-
-template<typename T>
-int fun1(int a, T b)
-{
- [[ assert: a > 0 ]];
- [[ assert: (long long)b > 0 ]];
- return a > 0;
-}
-
-template<typename T>
-struct test
-{
- static int fun(int a, T b) {
- [[ assert: a > 0 ]];
- [[ assert: b > 0 ]];
- return a > 0;
- }
-};
-
-int main()
-{
- fun1(1, -1);
- fun1(-1, 1.0);
- fun1(-1, (const char *)0x1234);
-
- [[ assert: fun1(-1, -5) ]];
- [[ assert: test<int>::fun(10, -6) ]];
- [[ assert: test<double>::fun(10.0, -7) ]];
- // return 0;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output {contract violation in function fun1<int> at .*:12: \(long long\)b > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function fun1<double> at .*:11: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function fun1<const char\*> at .*:11: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function fun1<int> at .*:11: a > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function fun1<int> at .*:12: \(long long\)b > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function main at .*:32: fun1\(-1, -5\)(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function test<int>::fun at .*:21: b > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
-// { dg-output {contract violation in function test<double>::fun at .*:21: b > 0(\n|\r\n|\r)} }
-// { dg-output {\[continue:on\](\n|\r\n|\r)} }
+++ /dev/null
-CXXFLAGS=--std=c++17 -fcontract-continuation-mode=on
-
-default: assert_fail libhandle_contract_violation.so
-
-run: default
- LD_PRELOAD=./libhandle_contract_violation.so ./assert_fail
-
-./libhandle_contract_violation.so: ./handle_contract_violation.cpp
- ${CXX} ${CXXFLAGS} -shared -fPIC -o $@ $<
-
-clean:
- rm -fr ./*.o ./libhandle_contract_violation.so ./assert_fail
-
+++ /dev/null
-build and install gcc to $prefix, then run:
-LD_LIBRARY_PATH=$prefix/lib64 CXX=$prefix/bin/g++ make run
-
-this test has a custom handle_contract_violation that throws an exception
-this test is built with -fcontract-continuation-mode=on
-
-since 1) our fun() is not marked noexcept and 2) the continue mode is set to
-on, we expect the exception thrown within the contract violation handler to
-propagate back up into the catch block located in main()
-
-expected output therefore ends in:
- synth caught indirect: -1
-
+++ /dev/null
-#include <iostream>
-#include <contract>
-
-int fun() {
- int x = 0;
- [[ assert: x < 0 ]];
-
- return 0;
-}
-
-int main(int argc, char**) {
- try {
- fun();
- } catch(int &ex) {
- std::cerr << "synth caught indirect: " << ex << std::endl;
- }
-
- return 0;
-}
-
+++ /dev/null
-#include <iostream>
-#include <contract>
-
-void handle_contract_violation(const std::contract_violation &violation) {
- std::cerr << "custom handle_contract_violation: " << std::endl
- << " line_number: " << violation.line_number() << std::endl
- << " file_name: " << violation.file_name() << std::endl
- << " function_name: " << violation.function_name() << std::endl
- << " comment: " << violation.comment() << std::endl
- << " assertion_level: " << violation.assertion_level() << std::endl
- << std::endl;
- throw -1;
-}
-
+++ /dev/null
-CXXFLAGS=--std=c++17 -fcontract-continuation-mode=on
-
-default: assert_fail libhandle_contract_violation.so
-
-run: default
- LD_PRELOAD=./libhandle_contract_violation.so ./assert_fail
-
-./libhandle_contract_violation.so: ./handle_contract_violation.cpp
- ${CXX} ${CXXFLAGS} -shared -fPIC -o $@ $<
-
-clean:
- rm -fr ./*.o ./libhandle_contract_violation.so ./assert_fail
-
+++ /dev/null
-build and install gcc to $prefix, then run:
-LD_LIBRARY_PATH=$prefix/lib64 CXX=$prefix/bin/g++ make run
-
-this test has a custom handle_contract_violation that throws an exception
-this test is built with -fcontract-continuation-mode=on
-
-since 1) our fun() *IS* marked noexcept even though 2) the continue mode is
-set to on, we expect the exception thrown within the contract violation
-handler to quashed and have std::terminate run.
-
-expected output therefore ends in:
- terminate called after throwing an instance of 'int'
-
-despite there being a catch(int &) handler in main
-
+++ /dev/null
-#include <iostream>
-#include <contract>
-
-int fun() noexcept {
- int x = 0;
- [[ assert: x < 0 ]];
-
- return 0;
-}
-
-int main(int argc, char**) {
- try {
- fun();
- } catch(int &ex) {
- std::cerr << "synth caught indirect: " << ex << std::endl;
- }
-
- return 0;
-}
-
+++ /dev/null
-#include <iostream>
-#include <contract>
-
-void handle_contract_violation(const std::contract_violation &violation) {
- std::cerr << "custom handle_contract_violation: " << std::endl
- << " line_number: " << violation.line_number() << std::endl
- << " file_name: " << violation.file_name() << std::endl
- << " function_name: " << violation.function_name() << std::endl
- << " comment: " << violation.comment() << std::endl
- << " assertion_level: " << violation.assertion_level() << std::endl
- << std::endl;
- throw -1;
-}
-
+++ /dev/null
-// check that contracts can be handled even when exceptions are disabled
-// { dg-do run }
-// { dg-options "-std=c++2a -fcontracts -fno-exceptions " }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-// { dg-output "contract violation in function f at .* a<5" }
-
-#include <exception>
-#include <cstdlib>
-
-int terminate_called = 0;
-void my_term()
-{
- std::exit(0);
-}
-
-
-void f(int a)
- [[ pre : a<5 ]]
- {
- }
-
-int
-main ()
-{
- std::set_terminate (my_term);
- f(3);
- f(10);
-}
+++ /dev/null
-// check that an invalid contract condition doesn't cause an ICE
-// { dg-do compile }
-// { dg-options "-std=c++2a -fcontracts " }
-
-struct A
-{
- A (A&);
-};
-struct S
-{
- void f(A a)
- [[ pre : a]] // { dg-error "could not convert" }
- [[ pre : a.b]]// { dg-error "has no member" }
- {
-
- }
-};
-void f(A a)
- [[ pre : a]] // { dg-error "could not convert" }
- [[ pre : a.b]]// { dg-error "has no member" }
- {
- [[ assert : a ]]; // { dg-error "could not convert" }
- [[ assert : a.b ]];// { dg-error "has no member" }
- }
-
-int
-main ()
-{
-}
+++ /dev/null
-// We were failing to apply post conditions to void functions.
-
-// { dg-do run }
-// { dg-options "-std=c++20 -fcontracts -fcontract-continuation-mode=on" }
-// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-
-void foo (const int b)
-[[ post: b == 9 ]] // contract not checked
-{}
-
-int main()
-{
- foo(3);
-}
-
-// { dg-output "contract violation in function foo at .*.C:9: b == 9.*(\n|\r\n|\r)" }
+++ /dev/null
-// ICE in explicit instantiation of a function with contracts
-// { dg-do run }
-// { dg-options "-std=c++20 -fcontracts -fcontract-continuation-mode=on" }
-// { dg-skip-if "required hosted libstdc++ for stdc++exp" { ! hostedlib } }
-
-template<class T>
-void foo(T t)
-[[pre : t == 9 ]] {
-}
-
-template void foo<int>(int i);
-
-
-template<class T>
-struct templateS
-{
- void fooS(T t)
- [[pre : t == 9 ]] {
- }
-};
-
-template struct templateS<int>;
-
-
-struct S {
-
- template<class T>
- void fooS(T t)
- [[pre : t == 9 ]] {
- }
-
- template<class T>
- static void fooStatic(T t)
- [[pre : t == 9 ]] {
- }
-};
-
-template void S::fooS<int>(int i);
-
-template void S::fooStatic<int>(int i);
-
-int main()
-{
- foo(3);
-
- templateS<int> ts;
- ts.fooS(3);
-
- S s;
- s.fooS(3);
- S::fooStatic(3);
-}
-
-// { dg-output "contract violation in function foo<int> at .* t == 9.*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function templateS<int>::fooS at .* t == 9.*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S::fooS<int> at .* t == 9.*(\n|\r\n|\r)" }
-// { dg-output "contract violation in function S::fooStatic<int> at .* t == 9.*(\n|\r\n|\r)" }
+++ /dev/null
-// { dg-options "-std=c++20 -fcontracts " }
-struct a {
- __attribute__((no_sanitize("")))
- int f(int) [[pre:true]];
-};
-int a::f(int) { return 0; }
+++ /dev/null
-// Check that contract assertion on a non-empty statement doesn't cause an
-// ICE.
-// { dg-options "-std=c++2a -fcontracts " }
-
-void f();
-int main ()
-{
- [[assert: true]] f(); // { dg-error "assertions must be followed by" }
-}
+++ /dev/null
-CXXFLAGS=--std=c++17
-
-default: assert_fail libhandle_contract_violation.so
-
-run: default
- LD_PRELOAD=./libhandle_contract_violation.so ./assert_fail
-
-./libhandle_contract_violation.so: ./handle_contract_violation.cpp
- ${CXX} ${CXXFLAGS} -shared -fPIC -o $@ $<
-
-clean:
- rm -fr ./libhandle_contract_violation.so ./assert_fail
-
+++ /dev/null
-build and install gcc to $prefix, then run:
-LD_LIBRARY_PATH=$prefix/lib64 CXX=$prefix/bin/g++ make run
+++ /dev/null
-int main(int, char**) {
- int x = 0;
- [[ assert: x < 0 ]];
-
- return 0;
-}
-
+++ /dev/null
-#include <iostream>
-#include <contract>
-
-void handle_contract_violation(const std::contract_violation &violation) {
- std::cerr << "custom handle_contract_violation: " << std::endl
- << " line_number: " << violation.line_number() << std::endl
- << " file_name: " << violation.file_name() << std::endl
- << " function_name: " << violation.function_name() << std::endl
- << " comment: " << violation.comment() << std::endl
- << " assertion_level: " << violation.assertion_level() << std::endl
- << " assertion_role: " << violation.assertion_role() << std::endl
- << " continuation_mode: " << (int)violation.continuation_mode() << std::endl
- << std::endl;
-}
-
+++ /dev/null
-CXXFLAGS=--std=c++17 -fcontract-continuation-mode=on
-LDFLAGS=-ldl
-
-default: assert_fail libhandle_contract_violation.so libnocontinue.so
-
-run: default
- ./assert_fail
-
-runno: default
- LD_PRELOAD="./libnocontinue.so ./libhandle_contract_violation.so" ./assert_fail
-
-runnostd: default
- LD_PRELOAD=./libnocontinue.so ./assert_fail
-
-./libhandle_contract_violation.so: ./handle_contract_violation.cpp
- ${CXX} ${CXXFLAGS} -shared -fPIC -o $@ $<
-
-./libnocontinue.so: ./nocontinue.cpp
- ${CXX} ${CXXFLAGS} -shared -fPIC -o $@ $<
-
-clean:
- rm -fr ./libhandle_contract_violation.so ./libnocontinue.so ./assert_fail
-
+++ /dev/null
-build and install gcc to $prefix, then run:
-
-LD_LIBRARY_PATH=$prefix/lib64 CXX=$prefix/bin/g++ make run
- Build and run with continuation mode on; will print violation info from the
- standard handler and then continue to print "returning from main"
-
-LD_LIBRARY_PATH=$prefix/lib64 CXX=$prefix/bin/g++ make runnostd
- Build and run with continuation mode on, using the default violation handler
- while preloading the 'nocontinue' hook. This uses LD_PRELOAD to turn all
- continuing contract violations into non-continuing versions.
-
- Will print violation info from the standard handler and then terminate -- it
- will not print "returning from main"
-
-LD_LIBRARY_PATH=$prefix/lib64 CXX=$prefix/bin/g++ make runno
- Build and run with continuation mode on, using a custom violation handler
- while preloading the 'nocontinue' hook. This uses LD_PRELOAD to turn all
- continuing contract violations into non-continuing versions and to install a
- custom violation handler.
-
- Will print violation info from the custom handler and then terminate -- it
- will not print "returning from main"
-
+++ /dev/null
-#include <iostream>
-
-int main(int, char**) {
- int x = 0;
- [[ assert: x < 0 ]];
-
- std::cout << "returning from main" << std::endl;
- return 0;
-}
-
+++ /dev/null
-#include <iostream>
-#include <contract>
-
-void handle_contract_violation(const std::contract_violation &violation) {
- std::cerr << "custom handle_contract_violation: " << std::endl
- << " line_number: " << violation.line_number() << std::endl
- << " file_name: " << violation.file_name() << std::endl
- << " function_name: " << violation.function_name() << std::endl
- << " comment: " << violation.comment() << std::endl
- << " assertion_level: " << violation.assertion_level() << std::endl
- << std::endl;
-}
-
+++ /dev/null
-#include <exception>
-#include <contract>
-#include <dlfcn.h>
-
-using handler = void (*)(const std::contract_violation &);
-constexpr const char *mangledHandlerName = "_Z25handle_contract_violationRKSt18contract_violation";
-void handle_contract_violation(const std::contract_violation &violation) {
- try {
- handler original_handle_contract_violation;
- original_handle_contract_violation =
- (handler)dlsym(RTLD_NEXT, mangledHandlerName);
- (*original_handle_contract_violation)(violation);
- }
- catch(...) {
- ; // squash
- }
- std::terminate();
-}
-
+++ /dev/null
-// { dg-additional-options "-fcontracts -fcontract-continuation-mode=on" }
-// { dg-do run }
-// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
-
-#include <iostream>
-#include <coroutine>
-
-// In order to test the contract violation diagnostic, we have to set
-// -fcontract-continuation-mode=on; this means that the code will emit
-// the message below - but before that the contract should have been checked.
-void process(int from, int to)
-{
- if (from > to)
- std::cout << "would have been a disaster!" << std::endl;
-}
-
-template <typename T>
-struct generator
-{
- struct promise_type
- {
- template <typename... Args>
- promise_type(Args&&... args) {
- std::cout << "promise init" << std::endl;
- process(args...);
- }
-
- std::suspend_always yield_value(T) { return {}; }
-
- std::suspend_always initial_suspend() const noexcept { return {}; }
- std::suspend_never final_suspend() const noexcept { return {}; }
- void unhandled_exception() noexcept {}
-
- generator<T> get_return_object() noexcept { return {}; }
- };
-};
-
-namespace std {
-template <typename T, typename... Args>
-struct coroutine_traits<generator<T>, Args...>
-{
- using promise_type = typename generator<T>::promise_type;
-};
-
-};
-
-generator<int> seq(int from, int to) [[pre: from <= to]]
-
-{
- std::cout << "coro initial" << std::endl;
- for (int i = from; i <= to; ++i) {
- co_yield i;
- std::cout << "coro resumed" << std::endl;
- }
-}
-
-int main() {
- std::cout << "main initial" << std::endl;
- generator<int> s = seq(10, 5);
- (void)s;
- std::cout << "main continues" << std::endl;
-}
-
-// { dg-output "contract violation in function seq at .*.C:47: from \<= to.*(\n|\r\n|\r)" }
+++ /dev/null
-// { dg-additional-options "-fcontracts -fcontract-continuation-mode=on" }
-// { dg-do run }
-// { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
-
-#include <iostream>
-#include <coroutine>
-
-
-template <typename T>
-struct generator
-{
- struct promise_type
- {
- std::suspend_always yield_value(T) { return {}; }
-
- std::suspend_always initial_suspend() const noexcept { return {}; }
- std::suspend_never final_suspend() const noexcept { return {}; }
- void unhandled_exception() noexcept {}
-
- generator<T> get_return_object() noexcept { return {}; }
- };
-
- bool is_valid() { return false; }
-};
-
-namespace std {
-template <typename T, typename... Args>
-struct coroutine_traits<generator<T>, Args...>
-{
- using promise_type = typename generator<T>::promise_type;
-};
-
-};
-
-generator<int> val(int v)
-[[post g: g.is_valid()]]
-{
- std::cout << "coro initial" << std::endl;
- co_yield v;
- std::cout << "coro resumed" << std::endl;
-}
-
-int main() {
- std::cout << "main initial" << std::endl;
- generator<int> s = val(1);
- (void)s;
- std::cout << "main continues" << std::endl;
-}
-
-// { dg-output "contract violation in function val at .*.C:36: g.is_valid().*(\n|\r\n|\r)" }
+++ /dev/null
-// Basic test to ensure that guarded templates correctly serialize and
-// deserialize their contracts through the CMI.
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-continuation-mode=on" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-module;
-#include <cstdio>
-#include <experimental/contract>
-export module foo;
-// { dg-module-cmi foo }
-
-export int violation_count{0};
-export extern "C++" void handle_contract_violation(const std::experimental::contract_violation &violation)
-{
- violation_count++;
- printf("violation_count: %d\n", violation_count);
-}
-
-export int nontemplate(int n)
- [[ pre: n > 0 ]]
- [[ post r: r > 0 ]]
-{
- return -n;
-}
-
-export
-template<typename T>
-T fn(T n)
- [[ pre: n > 0 ]]
- [[ post r: r > 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, n);
- return n;
-}
-
-export
-template<typename T>
-void void_fn(T n)
- [[ pre: n < 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, n);
-}
-
-export void foo_fn()
-{
- fn(5);
-}
-
+++ /dev/null
-// { dg-module-do run }
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-continuation-mode=on" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-
-#include <cstdio>
-import foo;
-
-template<typename T>
-bool bar_fn_pre(T n) { printf("bar fn pre(%d)\n", n); return true; }
-
-template<typename T>
-T bar_fn(T n)
- [[ pre: bar_fn_pre(n) && n > 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, n);
- return n;
-}
-
-int main(int, char**)
-{
- nontemplate(5);
- nontemplate(-5);
- fn(5);
- fn(-5);
- void_fn(5);
- void_fn(-5);
- bar_fn(5);
- bar_fn(-5);
- return violation_count == 6 ? 0 : -1;
-}
-
+++ /dev/null
-// Basic test to ensure that guarded function contracts are correctly
-// serialized and deserialized through the CMI.
-// This also tries to ensure that entities referenced in a function's
-// contracts are correctly marked as a dependency of the function itself and
-// serialized in the correct order.
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:maybe,maybe,ignore" }
-module;
-#include <cstdio>
-#include <experimental/contract>
-export module foo;
-// { dg-module-cmi foo }
-
-export int violation_count{0};
-export int violation_line_sum{0};
-export extern "C++" void handle_contract_violation(const std::experimental::contract_violation &violation)
-{
- violation_count++;
- violation_line_sum += violation.line_number () * violation_count;
- printf("violation: %d %d\n", violation_count, violation_line_sum);
-}
-
-export int fn2(int n);
-export int fn_in2(int n);
-export int pre_print(int n) { printf("pre_print(%d)\n", n); return n; }
-
-export int fn_in1(int n) [[ pre: pre_print(n) > 0 ]]
-{
- printf("%s blah (%d)\n", __FUNCTION__, n);
- return n;
-}
-export int fn_in2(int x) [[ pre: pre_print(x) > 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, x);
- return x;
-}
-
-export int fn_iso(int n);
-
-export int fn1(int n)
- [[ pre: pre_print(n) > 0 ]];
-
-export int fn2(int n)
- [[ pre: pre_print(n) > 0 ]];
-
-export int pre_print2(int n);
-
-export int fn3(int n)
- [[ pre: pre_print2(n) > 0 ]];
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
+++ /dev/null
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:maybe,maybe,ignore" }
-module;
-#include <cstdio>
-module foo;
-
-int fn1(int x)
-{
- printf("%s(%d)\n", __FUNCTION__, x);
- return x;
-}
-
-int fn_iso(int n) [[ pre: pre_print(n) > 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, n);
- return n;
-}
-
-int pre_print2(int n)
-{
- printf("pre_print(%d)\n", n);
- return n;
-}
-
-int fn2(int x)
-{
- printf("%s(%d)\n", __FUNCTION__, x);
- return x;
-}
-
-int fn3(int x)
-{
- printf("%s(%d)\n", __FUNCTION__, x);
- return x;
-}
-
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
+++ /dev/null
-// { dg-module-do run }
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:maybe,maybe,ignore" }
-import foo;
-
-int main(int, char**)
-{
- int x = -1;
-
- fn_iso(x--);
- fn1(x--);
- fn2(x--);
- fn3(x--);
- fn_in1(x--);
- fn_in2(x--);
- return (violation_count == 6 && violation_line_sum == 729) ? 0 : -1;
-}
-
-// TODO does any of this actually get verified?
-// { dg-output "pre_print.-1.(\n|\r\n|\r)*" }
-// { dg-output "violation: 1 12(\n|\r\n|\r)*" }
-// { dg-output "fn_iso.-1.(\n|\r\n|\r)*" }
-
+++ /dev/null
-// Basic test to ensure that guarded templates correctly serialize and
-// deserialize their contracts through the CMI.
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-continuation-mode=on" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-module;
-#include <cstdio>
-#include <experimental/contract>
-export module foo;
-// { dg-module-cmi foo }
-
-export int violation_count{0};
-export extern "C++" void handle_contract_violation(const std::experimental::contract_violation &violation)
-{
- violation_count++;
- printf("violation_count: %d\n", violation_count);
-}
-
-export int nontemplate(int n)
- [[ pre: n > 0 ]]
- [[ post r: r > 0 ]]
-{
- return -n;
-}
-
-export
-template<typename T>
-T fn(T n)
- [[ pre: n > 0 ]]
- [[ post r: r > 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, n);
- return n;
-}
-
-export
-template<typename T>
-void void_fn(T n)
- [[ pre: n < 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, n);
-}
-
+++ /dev/null
-// { dg-module-do run }
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:ignore,ignore,ignore" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-
-#include <cstdio>
-import foo;
-
-template<typename T>
-bool bar_fn_pre(T n) { printf("bar fn pre(%d)\n", n); return true; }
-
-template<typename T>
-T bar_fn(T n)
- [[ pre: bar_fn_pre(n) && n > 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, n);
- return n;
-}
-
-int main(int, char**)
-{
- nontemplate(5);
- nontemplate(-5);
- fn(5);
- fn(-5);
- fn(5.3);
- fn(-5.3);
- void_fn(5);
- void_fn(-5);
- bar_fn(5);
- bar_fn(-5);
- return violation_count == 7 ? 0 : -1;
-}
-
+++ /dev/null
-// Test that template contracts are not reintpreted when the reinterpret
-// contracts flag is not set, regardless of the current TU's contract
-// configuration.
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:maybe,maybe,ignore" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-module;
-#include <cstdio>
-#include <experimental/contract>
-export module foo;
-// { dg-module-cmi foo }
-
-export int violation_count{0};
-export extern "C++" void handle_contract_violation(const std::experimental::contract_violation &violation)
-{
- violation_count++;
- printf("violation_count: %d\n", violation_count);
-}
-
-export template<typename T>
-T fn_t(T t)
- [[ pre: t > 0 ]]
- [[ pre audit %custom: t > 0 ]]
-{
- printf("%s(%d)\n", __FUNCTION__, t);
- return t;
-}
-
-export int fn_int(int n);
-
+++ /dev/null
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:ignore,ignore,ignore" }
-module foo;
-
-int fn_int(int n)
-{
- return fn_t(n);
-}
-
+++ /dev/null
-// { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:ignore,ignore,ignore" }
-export module bar;
-import foo;
-
-export int bar_fn_int(int n)
-{
- return fn_t(n);
-}
-
+++ /dev/null
-// { dg-module-do run }
-// { dg-additional-options "-fmodules-ts -fcontracts" }
-// { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
-
-#include <cstdio>
-import foo;
-import bar;
-
-int main(int, char**)
-{
- int x = -1;
-
- printf("calling fn_int\n");
- fn_int(x--);
- printf("calling bar_fn_int\n");
- bar_fn_int(x--);
-
- return violation_count - 4;
-}
-
-// TODO verify dg-output as well once the testsuite supports it
-
+++ /dev/null
-// PR c++/108205
-// Test that the implicitly declared handle_contract_violation function is
-// properly matched with a later declaration in an importing TU.
-// { dg-additional-options "-fmodules -fcontracts -fcontract-continuation-mode=on" }
-// { dg-module-cmi test }
-
-export module test;
-export inline void foo(int x) noexcept [[ pre: x != 0 ]] {}
+++ /dev/null
-// PR c++/108205
-// { dg-module-do run }
-// { dg-additional-options "-fmodules -fcontracts -fcontract-continuation-mode=on" }
-// { dg-skip-if "required hosted libstdc++ for experimental/contract" { ! hostedlib } }
-
-#include <experimental/contract>
-import test;
-
-bool saw_violation = false;
-void handle_contract_violation(const std::experimental::contract_violation& v) {
- saw_violation = true;
-}
-
-int main() {
- foo(10);
- if (saw_violation)
- __builtin_abort();
- foo(0);
- if (!saw_violation)
- __builtin_abort();
-}
+++ /dev/null
-// { dg-additional-options "-fmodules-ts -fcontracts" }
-
-export module foo;
-// { dg-module-cmi foo }
-
-void foo (int, void *);
-void foo (float, void *);
-
-template <typename T> class TPL
-{
- friend void foo (T, void *); // { dg-warning "non-template function" }
-
- T member;
-};
-
-template class TPL<float>; // instantiate
-
+++ /dev/null
-// { dg-additional-options "-fmodules-ts -fcontracts" }
-
-module foo;
-
-void foo (int x, void *p)
- [[ pre: x > 0 ]]
-{
- auto *obj = reinterpret_cast<TPL<int> *> (p);
-
- obj->member = x;
-}
-
-void foo (float x, void *p)
- [[ pre: x > 0 ]]
-{
- auto *obj = reinterpret_cast<TPL<float> *> (p);
-
- obj->member = x;
-}