Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Checks: >-
bugprone-use-after-move,
bugprone-virtual-near-miss,
modernize-deprecated-headers,
modernize-use-equals-default,
modernize-use-equals-delete,
modernize-use-nullptr,
modernize-use-override,
performance-avoid-endl,
Expand All @@ -42,7 +44,7 @@ CheckOptions:
bugprone-unused-return-value.AllowCastToVoid: true
# Shallow handles stay passed by value: a copy duplicates a few pointers or
# ids, with no reference count or allocation. See docs/code-style.md.
performance-unnecessary-value-param.AllowedTypes: '^Gecode::(Home|Group|PropagatorGroup|BrancherGroup|Pos|Reify)$;^Gecode::(Int|Bool|Set|Float)Var$;^Gecode::(Int|Bool|Set|Float)VarArray$;^Gecode::(VarArray|ViewArray)(<.*>)?$;^Gecode::(OpenIntVarList|OpenBoolVarList|OpenVarList|OpenViewList)(<.*>)?$;^Gecode::(VarImpView|DerivedView|ConstView)(<.*>)?$;^Gecode::(Int|Set|Float)::[A-Za-z]*View(<.*>)?$;^Gecode::.*::VarType$'
performance-unnecessary-value-param.AllowedTypes: '^Gecode::Float::ScaleView$'
readability-identifier-naming.FunctionCase: lower_case
readability-identifier-naming.MethodCase: lower_case
readability-identifier-naming.VariableCase: lower_case
Expand Down
83 changes: 83 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,89 @@ Date: unreleased
This release contains breaking changes. See the entries below for the
changes downstream code needs.

[ENTRY]
Module: kernel
What: change
Rank: major
[DESCRIPTION]
Make Home, the groups, Pos, Reify, LocalHandle, the variables, the variable
arrays, ViewArray, the open variable and view lists, and AFC with its
subclasses trivially copyable. Their copy constructors and copy assignments
are now implicit or defaulted, and the AFC classes and LocalHandle no longer
declare a destructor. Copies behave as before, but the ABI changes, and the
compiler may now pass these types in registers.
[MORE]
VarArray::operator= and ViewArray::operator= now return a non-const
reference.

[ENTRY]
Module: kernel
What: removed
Rank: major
[DESCRIPTION]
Delete the copy assignment of Home and of Space. Assigning one Home to
another copy-assigned the whole Space that the home refers to. A Home never
changes its space; construct a new Home instead. To copy a Space, clone it.

[ENTRY]
Module: kernel
What: change
Rank: minor
[DESCRIPTION]
Default special members that only construct, copy, or destroy members, and
delete with = delete the members that must not be called. Across the
library, default constructors, copy constructors, copy assignments, and
destructors with empty or member-wise bodies are now defaulted on their first
declaration; among them are those of the variables, most views, FloatVal,
IntSet, TupleSet, DFA, Action, CHB, the argument arrays, and the range and
value iterators. Behaviour is unchanged, but the ABI changes: several
members that the libraries exported, such as the copy constructors of
TupleSet, Action, and CHB, are now inline.
[MORE]
Copy members that were private and undefined, such as those of Search::Base,
Support::BitSetBase, Set::GLBndSet, and FlatZinc::Printer, are now public
and deleted, so copying one is reported as a call to a deleted function.

A defaulted default constructor is no longer user-provided. A class with
such a constructor and a field without an initializer can no longer be
default-initialized as a const object: `const Gecode::IntSetRanges r;` no
longer compiles. Initialize the object, or drop the const. This affects
most range and value iterators: IntSetRanges, IntSetValues, IntVarValues,
SetVarGlbRanges, SetVarLubRanges, SetVarUnknownRanges, the iterators in
Iter::Ranges and Iter::Values, and the range and value iterators of views
such as Int::ViewRanges and Set::UnknownRanges. It also affects SetRel,
FreeList, Float::OffsetView, and Search::TraceRecorder::ID.

[ENTRY]
Module: kernel
What: change
Rank: minor
[DESCRIPTION]
Remove operator!= where it only negated operator==, and replace the
operator< of Int::LDSB::Literal with a defaulted operator<=>. C++20 rewrites
`a != b` as `!(a == b)` and `a < b` through operator<=>, so these expressions
keep their results. A call that names the operator, such as
`operator!=(a, b)` or `a.operator<(b)`, no longer compiles; write the
expression instead.
[MORE]
operator!= is removed from PropagatorGroup, BrancherGroup, IntSet, DFA,
Support::BitSetData, space_allocator, region_allocator,
Int::BinPacking::Item, and the integer, set, and float views. The kernel's
ConstView, VarImpView, and DerivedView templates keep theirs, and so does
TupleSet: without it, `true != t` would be ambiguous between its int
constructor and its bool conversion.
Support::BitSetData::operator== is now defaulted and takes its argument by
const reference. Int::LDSB::Literal now also has ==, !=, <=, >, and >=.

[ENTRY]
Module: int
What: bug
Rank: minor
[DESCRIPTION]
Fix operator== of Int::CachedView. It used a member that the view does not
have, so comparing two cached integer views did not compile. It now compares
the base views, like Set::CachedView.

[ENTRY]
Module: support
What: removed
Expand Down
91 changes: 81 additions & 10 deletions docs/code-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,87 @@ and the type has a real move constructor, such as `std::function`,
`std::string`, `std::vector`, or a branching configuration, take it by value
and `std::move` it into place instead.

The check's `AllowedTypes` keeps Gecode's shallow handles passed by value:
`Home`, the groups, `Pos`, `Reify`, the variables (`IntVar` and so on), the
variable arrays and `ViewArray`, the open variable and view lists, the views,
and `View::VarType`. Copying one copies a few pointers or ids, with no
reference count or allocation, and Gecode code relies on their value
semantics. Owned and reference-counted types are not exempt: `SharedHandle`
types such as `IntSet`, `TupleSet`, `DFA`, `Rnd`, `Action`, and `CHB`,
`SharedArray`, the `*Args` arrays, `REG`, strings, and callbacks. Their copies
touch a reference count or allocate, so they follow the rule above. `FloatVal`
is not exempt either: it is two doubles, not a handle.
Gecode's shallow handles are trivially copyable, so the check never reports
them: `Home`, the groups, `Pos`, `Reify`, the variables (`IntVar` and so
on), the variable arrays and `ViewArray`, the open variable and view lists,
`LocalHandle`, `AFC`, and the views. Copying one copies a few pointers or ids,
with no reference count or allocation, and Gecode code relies on their value
semantics. Keep them so: define no copy member, move member, or destructor
for a handle that only copies its members, or default it on its first
declaration. `test/array.cpp` asserts the property. The check's
`AllowedTypes` lists only `Float::ScaleView`, a view that is not trivially
copyable because its `FloatVal` member is not. Owned and reference-counted
types are not exempt: `SharedHandle` types such as `IntSet`, `TupleSet`,
`DFA`, `Rnd`, `Action`, and `CHB`, `SharedArray`, the `*Args` arrays, `REG`,
strings, and callbacks. Their copies touch a reference count or allocate, so
they follow the rule above. `FloatVal` is not exempt either: it is two
doubles, not a handle.

Special members follow the rule of zero: a class whose members copy, move,
and destroy themselves declares no copy member, move member, or destructor.
Where a special member must be declared, for example a default constructor
beside other constructors or a virtual destructor, write `= default` instead
of an empty body or a member-wise copy, on the member's first declaration in
the class. A member defaulted out of line is user-provided and keeps the
class non-trivial. Virtual destructors may stay out of line: their class is
never trivial, and one defined in a .cpp file decides where the vtable is
emitted. A member that must not be called is a public `= delete`, not a
private member without a definition. `modernize-use-equals-default` and
`modernize-use-equals-delete` enforce these rules.

Some hand-written members stay, each after a
`// NOLINTNEXTLINE(modernize-use-equals-default)` line. Cloning constructors
such as `Space(Space&)` are not copy members: they take their source by
non-const reference because cloning writes forwarding pointers into it, and
they update their variables with `update()`. An empty default constructor
stays where defaulting it would change what value-initialization does:
`T()`, `T{}`, and the `alloc<T>(n)` functions of spaces, regions, and the
heap zero a class whose default constructor is not user-provided, while the
empty constructor leaves fields without an initializer untouched. A
constructor whose body is empty only in some configurations stays as well.

Comparisons use the C++20 forms. A class whose equality compares every field
declares `bool operator==(const T&) const = default;`. A class ordered
lexicographically by its fields in declaration order declares
`auto operator<=>(const T&) const = default;` instead, which also provides
`==`; its header includes `<compare>`. Declare no `operator!=` and no `<`,
`<=`, `>`, or `>=` beside them: the compiler rewrites `a != b` as
`!(a == b)` and the relational operators through `operator<=>`, in either
argument order. A hand-written `operator==` stays where equality is not
field-wise, for example a deep comparison of a shared object or a comparison
of the address an allocator refers to, and it gets no `operator!=` either. A
hand-written `operator<` stays where the order is not the field order, such
as a sort key. Operators that build modelling expressions, such as
`IntVar < IntVar`, keep all their operators, and so do the possibly/certainly
interval comparisons of `FloatVal`. The kernel's heterogeneous `ConstView`,
`VarImpView`, and `DerivedView` templates keep `operator!=`: without it, their
reversed `operator==` candidates would join every view comparison. A class
with both an implicit converting constructor and an implicit conversion
operator keeps `operator!=` as well. `TupleSet` is one: without its
`operator!=`, `true != t` is ambiguous between its `int` constructor and its
`bool` conversion. Rewriting
covers only operator expressions, so write `a != b`, not `operator!=(a, b)` or
`a.operator<(b)`. No clang-tidy check enforces this rule.

A const/non-const overload pair stays a pair unless one member with an
explicit object parameter, `template <class Self> auto& f(this Self&& self)`,
removes a duplicated body of more than one statement, not counting `assert`,
or a `const_cast` forwarding idiom. A pair whose bodies are one statement each
stays: the merged template is not shorter. Merge only when the merged member
returns the same type and accepts the same receivers as the pair, including
rvalues and calls through derived classes. A member reached through `self`
is looked up in the receiver's class, so a derived class can hide it, and a
class that inherits privately and re-exports the member with `using` makes
the base inaccessible, so the call no longer compiles. Storage reached
through a pointer does not inherit const from `self`; return it through
`std::forward_like<Self&>`. An explicit-object member cannot be virtual or
static, takes no trailing cv or ref qualifier, and its address is a plain
function pointer rather than a member pointer. The merged member is a
template over `Self`, so its definition must be visible to callers: a pair
defined in a .cpp file moves to a header. `Matrix::operator()` keeps its pair
for the derived-class reason: its two bodies duplicate a range check, but a
merged member would reject a privately derived matrix. No clang-tidy check
enforces this rule.

Other performance checks need focused review: `performance-enum-size` changes
layouts and ABI, and `performance-no-int-to-ptr` conflicts with deliberate
Expand Down
2 changes: 2 additions & 0 deletions examples/job-shop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ class JobShopProbe : public JobShopBase {
assign(*this, makespan, INT_ASSIGN_MIN());
}
/// Constructor for cloning \a s
// NOLINTNEXTLINE(modernize-use-equals-default)
JobShopProbe(JobShopProbe& s) : JobShopBase(s) {}
/// Copy during cloning
Space*
Expand Down Expand Up @@ -641,6 +642,7 @@ class JobShopSolve : public JobShopBase {
assign(*this, makespan, INT_ASSIGN_MIN());
}
/// Constructor for cloning \a s
// NOLINTNEXTLINE(modernize-use-equals-default)
JobShopSolve(JobShopSolve& s)
: JobShopBase(s), sorder(s.sorder), fst(s.fst), snd(s.snd), iafc(s.iafc), iaction(s.iaction),
baction(s.baction), ichb(s.ichb), bchb(s.bchb), rnd(s.rnd) {}
Expand Down
2 changes: 2 additions & 0 deletions examples/knights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class KnightsReified : public Knights {
}
}
/// Constructor for cloning \a s
// NOLINTNEXTLINE(modernize-use-equals-default)
KnightsReified(KnightsReified& s) : Knights(s) {}
/// Copy during cloning
Space*
Expand Down Expand Up @@ -313,6 +314,7 @@ class KnightsCircuit : public Knights {
dom(*this, succ[f], neighbors(f));
}
/// Constructor for cloning \a s
// NOLINTNEXTLINE(modernize-use-equals-default)
KnightsCircuit(KnightsCircuit& s) : Knights(s) {}
/// Copy during cloning
Space*
Expand Down
1 change: 1 addition & 0 deletions examples/open-shop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class OpenShop : public IntMinimizeScript {
int m; //< Machine
int p; //< Processing time
/// Default constructor
// NOLINTNEXTLINE(modernize-use-equals-default)
Task(void) {}
/// Constructor
Task(int j0, int m0, int p0) : j(j0), m(m0), p(p0) {}
Expand Down
1 change: 1 addition & 0 deletions examples/sudoku-advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Sudoku : public Script {
}

/// Constructor for cloning \a s
// NOLINTNEXTLINE(modernize-use-equals-default)
Sudoku(Sudoku& s) : Script(s), nn(s.nn), bw(s.bw), bh(s.bh), givens(s.givens) {}
};

Expand Down
5 changes: 1 addition & 4 deletions gecode/driver/script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ namespace Gecode { namespace Driver {
class ScriptGetInfo : public CPProfilerSearchTracer::GetInfo {
public:
/// Initialize
ScriptGetInfo(void);
ScriptGetInfo(void) = default;
/// Return info for a space (which must be a script)
std::string get_info(const Space& home) const override;
};
Expand Down Expand Up @@ -267,9 +267,6 @@ namespace Gecode { namespace Driver {

#ifdef GECODE_HAS_CPPROFILER

template <class BaseSpace>
ScriptGetInfo<BaseSpace>::ScriptGetInfo(void) {}

template <class BaseSpace>
std::string
ScriptGetInfo<BaseSpace>::get_info(const Space& home) const {
Expand Down
12 changes: 6 additions & 6 deletions gecode/flatzinc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ namespace Gecode { namespace FlatZinc {
#endif
);

private:
Printer(const Printer&);
Printer& operator=(const Printer&);
public:
Printer(const Printer&) = delete;
Printer& operator=(const Printer&) = delete;
};

/**
Expand Down Expand Up @@ -463,7 +463,7 @@ namespace Gecode { namespace FlatZinc {
/// Constructor
BranchInformation(void);
/// Copy constructor
BranchInformation(const BranchInformation& bi);
BranchInformation(const BranchInformation& bi) = default;
/// Initialise for use
void init(void);
/// Add new brancher information
Expand Down Expand Up @@ -587,12 +587,12 @@ namespace Gecode { namespace FlatZinc {

public:
OnRestartHandle() : SharedHandle() {}
OnRestartHandle(const OnRestartHandle& handle) : SharedHandle(handle) {}
OnRestartHandle(const OnRestartHandle& handle) = default;
OnRestartHandle&
operator=(const OnRestartHandle& handle) {
return static_cast<OnRestartHandle&>(SharedHandle::operator=(handle));
}
virtual ~OnRestartHandle() {};
virtual ~OnRestartHandle() = default;

void
init() {
Expand Down
3 changes: 2 additions & 1 deletion gecode/flatzinc/ast.hh
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ namespace Gecode { namespace FlatZinc { namespace AST {
int min;
int max;
std::vector<int> s;
// NOLINTNEXTLINE(modernize-use-equals-default)
SetLit(void) {}
SetLit(int min0, int max0) : interval(true), min(min0), max(max0) {}
SetLit(const std::vector<int>& s0) : interval(false), s(s0) {}
Expand Down Expand Up @@ -331,7 +332,7 @@ namespace Gecode { namespace FlatZinc { namespace AST {
}
};

inline Node::~Node(void) {}
inline Node::~Node(void) = default;

inline void
Node::append(Node* new_node) {
Expand Down
2 changes: 1 addition & 1 deletion gecode/flatzinc/blackbox-backend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace Gecode { namespace FlatZinc {
/// Backend for a deterministic FlatZinc blackbox function.
class BlackBoxBackend : public SharedHandle::Object {
public:
~BlackBoxBackend(void) override {}
~BlackBoxBackend(void) override = default;
virtual void run(BlackBoxCall& call) = 0;
void
run(const std::vector<int64_t>& int_input, const std::vector<double>& float_input,
Expand Down
3 changes: 2 additions & 1 deletion gecode/flatzinc/blackbox-process.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Gecode { namespace FlatZinc {
#ifdef GECODE_HAS_THREADS
std::thread::id owner;
#endif
// NOLINTNEXTLINE(modernize-use-equals-default)
BlackBoxProcessSession(void)
#ifdef GECODE_HAS_THREADS
: owner(std::this_thread::get_id())
Expand All @@ -34,7 +35,7 @@ namespace Gecode { namespace FlatZinc {
}

public:
virtual ~BlackBoxProcessSession(void) {}
virtual ~BlackBoxProcessSession(void) = default;
bool
owned_by_current_thread(void) const {
#ifdef GECODE_HAS_THREADS
Expand Down
2 changes: 1 addition & 1 deletion gecode/flatzinc/blackbox-propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace Gecode { namespace FlatZinc {
object(backend);
}
explicit BlackBoxHandle(const SharedHandle& handle) : SharedHandle(handle) {}
BlackBoxHandle(const BlackBoxHandle& handle) : SharedHandle(handle) {}
BlackBoxHandle(const BlackBoxHandle& handle) = default;
BlackBoxHandle&
operator=(const BlackBoxHandle& handle) {
return static_cast<BlackBoxHandle&>(SharedHandle::operator=(handle));
Expand Down
2 changes: 1 addition & 1 deletion gecode/flatzinc/blackbox.hh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Gecode { namespace FlatZinc {
class BlackBoxContextHandle : public SharedHandle {
public:
BlackBoxContextHandle(void) : SharedHandle() {}
BlackBoxContextHandle(const BlackBoxContextHandle& handle) : SharedHandle(handle) {}
BlackBoxContextHandle(const BlackBoxContextHandle& handle) = default;
BlackBoxContextHandle&
operator=(const BlackBoxContextHandle& handle) {
return static_cast<BlackBoxContextHandle&>(SharedHandle::operator=(handle));
Expand Down
Loading
Loading