Commit 20272ec5 authored by Kenton Varda's avatar Kenton Varda

PRECOND -> REQUIRE

parent 5b65179c
......@@ -181,7 +181,7 @@ struct UseScratch {
word* words;
ScratchSpace() {
PRECOND(scratchCounter < 6, "Too many scratch spaces needed at once.");
REQUIRE(scratchCounter < 6, "Too many scratch spaces needed at once.");
words = scratchSpace + scratchCounter++ * SCRATCH_SIZE;
}
~ScratchSpace() {
......
......@@ -76,7 +76,7 @@ public:
inline const char operator[](uint index) const { return bytes[index]; }
inline Reader slice(uint start, uint end) const {
KJ_INLINE_DPRECOND(start <= end && end <= size_, "Out-of-bounds slice.");
KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds slice.");
return Reader(bytes + start, end - start);
}
......@@ -121,13 +121,13 @@ public:
inline Reader(const char* text): Data::Reader(text, strlen(text)) {}
inline Reader(char* text): Data::Reader(text, strlen(text)) {}
inline Reader(const char* text, uint size): Data::Reader(text, size) {
KJ_INLINE_DPRECOND(text[size] == '\0', "Text must be NUL-terminated.");
KJ_IREQUIRE(text[size] == '\0', "Text must be NUL-terminated.");
}
template <typename T>
inline Reader(const T& other): Data::Reader(other.c_str(), other.size()) {
// Primarily intended for converting from std::string.
KJ_INLINE_DPRECOND(data()[size()] == '\0', "Text must be NUL-terminated.");
KJ_IREQUIRE(data()[size()] == '\0', "Text must be NUL-terminated.");
}
inline const char* c_str() const { return data(); }
......@@ -164,7 +164,7 @@ public:
inline char& operator[](uint index) const { return bytes[index]; }
inline Builder slice(uint start, uint end) const {
KJ_INLINE_DPRECOND(start <= end && end <= size_, "Out-of-bounds slice.");
KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds slice.");
return Builder(bytes + start, end - start);
}
......@@ -183,7 +183,7 @@ public:
template <typename T>
inline void copyFrom(const T& other) const {
KJ_INLINE_DPRECOND(size() == other.size(), "Sizes must match to copy.");
KJ_IREQUIRE(size() == other.size(), "Sizes must match to copy.");
memcpy(bytes, other.data(), other.size());
}
inline void copyFrom(const void* other) const {
......
......@@ -217,7 +217,7 @@ Text::Reader getUnqualifiedName(Schema schema) {
return nested.getName();
}
}
FAIL_PRECOND("A schema Node's supposed scope did not contain the node as a NestedNode.");
FAIL_REQUIRE("A schema Node's supposed scope did not contain the node as a NestedNode.");
return "(?)";
}
......@@ -365,22 +365,22 @@ TextBlob genValue(schema::Type::Reader type, schema::Value::Reader value, Schema
case schema::Value::Body::TEXT_VALUE: return text(DynamicValue::Reader(body.getTextValue()));
case schema::Value::Body::DATA_VALUE: return text(DynamicValue::Reader(body.getDataValue()));
case schema::Value::Body::LIST_VALUE: {
PRECOND(type.getBody().which() == schema::Type::Body::LIST_TYPE, "type/value mismatch");
REQUIRE(type.getBody().which() == schema::Type::Body::LIST_TYPE, "type/value mismatch");
auto value = body.getListValue<DynamicList>(
ListSchema::of(type.getBody().getListType(), scope));
return text(value);
}
case schema::Value::Body::ENUM_VALUE: {
PRECOND(type.getBody().which() == schema::Type::Body::ENUM_TYPE, "type/value mismatch");
REQUIRE(type.getBody().which() == schema::Type::Body::ENUM_TYPE, "type/value mismatch");
auto enumNode = scope.getDependency(type.getBody().getEnumType()).asEnum().getProto();
auto enumType = enumNode.getBody().getEnumNode();
auto enumerants = enumType.getEnumerants();
PRECOND(body.getEnumValue() < enumerants.size(),
REQUIRE(body.getEnumValue() < enumerants.size(),
"Enum value out-of-range.", body.getEnumValue(), enumNode.getDisplayName());
return text(enumerants[body.getEnumValue()].getName());
}
case schema::Value::Body::STRUCT_VALUE: {
PRECOND(type.getBody().which() == schema::Type::Body::STRUCT_TYPE, "type/value mismatch");
REQUIRE(type.getBody().which() == schema::Type::Body::STRUCT_TYPE, "type/value mismatch");
auto value = body.getStructValue<DynamicStruct>(
scope.getDependency(type.getBody().getStructType()).asStruct());
return text(value);
......@@ -400,7 +400,7 @@ TextBlob genAnnotation(schema::Annotation::Reader annotation,
const char* prefix = " ", const char* suffix = "") {
auto decl = schemaLoader.get(annotation.getId());
auto body = decl.getProto().getBody();
PRECOND(body.which() == schema::Node::Body::ANNOTATION_NODE);
REQUIRE(body.which() == schema::Node::Body::ANNOTATION_NODE);
auto annDecl = body.getAnnotationNode();
return text(prefix, "$", nodeName(decl, scope), "(",
......@@ -468,12 +468,12 @@ TextBlob genDecl(Schema schema, Text::Reader name, uint64_t scopeId, Indent inde
auto proto = schema.getProto();
if (proto.getScopeId() != scopeId) {
// This appears to be an alias for something declared elsewhere.
FAIL_PRECOND("Aliases not implemented.");
FAIL_REQUIRE("Aliases not implemented.");
}
switch (proto.getBody().which()) {
case schema::Node::Body::FILE_NODE:
FAIL_PRECOND("Encountered nested file node.");
FAIL_REQUIRE("Encountered nested file node.");
break;
case schema::Node::Body::STRUCT_NODE: {
auto body = proto.getBody().getStructNode();
......@@ -578,7 +578,7 @@ TextBlob genNestedDecls(Schema schema, Indent indent) {
TextBlob genFile(Schema file) {
auto proto = file.getProto();
auto body = proto.getBody();
PRECOND(body.which() == schema::Node::Body::FILE_NODE, "Expected a file node.",
REQUIRE(body.which() == schema::Node::Body::FILE_NODE, "Expected a file node.",
(uint)body.which());
return text(
......
This diff is collapsed.
......@@ -102,12 +102,12 @@ struct WirePointer {
}
KJ_ALWAYS_INLINE(WordCount farPositionInSegment() const) {
DPRECOND(kind() == FAR,
DREQUIRE(kind() == FAR,
"positionInSegment() should only be called on FAR pointers.");
return (offsetAndKind.get() >> 3) * WORDS;
}
KJ_ALWAYS_INLINE(bool isDoubleFar() const) {
DPRECOND(kind() == FAR,
DREQUIRE(kind() == FAR,
"isDoubleFar() should only be called on FAR pointers.");
return (offsetAndKind.get() >> 2) & 1;
}
......@@ -155,12 +155,12 @@ struct WirePointer {
}
KJ_ALWAYS_INLINE(void set(FieldSize es, ElementCount ec)) {
DPRECOND(ec < (1 << 29) * ELEMENTS, "Lists are limited to 2**29 elements.");
DREQUIRE(ec < (1 << 29) * ELEMENTS, "Lists are limited to 2**29 elements.");
elementSizeAndCount.set(((ec / ELEMENTS) << 3) | static_cast<int>(es));
}
KJ_ALWAYS_INLINE(void setInlineComposite(WordCount wc)) {
DPRECOND(wc < (1 << 29) * WORDS, "Inline composite lists are limited to 2**29 words.");
DREQUIRE(wc < (1 << 29) * WORDS, "Inline composite lists are limited to 2**29 words.");
elementSizeAndCount.set(((wc / WORDS) << 3) |
static_cast<int>(FieldSize::INLINE_COMPOSITE));
}
......@@ -656,7 +656,7 @@ struct WireHelpers {
break;
}
default:
FAIL_PRECOND("Copy source message contained unexpected kind.");
FAIL_REQUIRE("Copy source message contained unexpected kind.");
break;
}
......@@ -796,7 +796,7 @@ struct WireHelpers {
static KJ_ALWAYS_INLINE(ListBuilder initListPointer(
WirePointer* ref, SegmentBuilder* segment, ElementCount elementCount,
FieldSize elementSize)) {
DPRECOND(elementSize != FieldSize::INLINE_COMPOSITE,
DREQUIRE(elementSize != FieldSize::INLINE_COMPOSITE,
"Should have called initStructListPointer() instead.");
BitCount dataSize = dataBitsPerElement(elementSize) * ELEMENTS;
......@@ -848,7 +848,7 @@ struct WireHelpers {
static KJ_ALWAYS_INLINE(ListBuilder getWritableListPointer(
WirePointer* origRef, SegmentBuilder* origSegment, FieldSize elementSize,
const word* defaultValue)) {
DPRECOND(elementSize != FieldSize::INLINE_COMPOSITE,
DREQUIRE(elementSize != FieldSize::INLINE_COMPOSITE,
"Use getStructList{Element,Field}() for structs.");
if (origRef->isNull()) {
......@@ -886,7 +886,7 @@ struct WireHelpers {
// Read the tag to get the actual element count.
WirePointer* tag = reinterpret_cast<WirePointer*>(ptr);
PRECOND(tag->kind() == WirePointer::STRUCT,
REQUIRE(tag->kind() == WirePointer::STRUCT,
"INLINE_COMPOSITE list with non-STRUCT elements not supported.");
ptr += POINTER_SIZE_IN_WORDS;
......@@ -1226,9 +1226,9 @@ struct WireHelpers {
} else {
word* ptr = followFars(ref, segment);
PRECOND(ref->kind() == WirePointer::LIST,
REQUIRE(ref->kind() == WirePointer::LIST,
"Called getText{Field,Element}() but existing pointer is not a list.");
PRECOND(ref->listRef.elementSize() == FieldSize::BYTE,
REQUIRE(ref->listRef.elementSize() == FieldSize::BYTE,
"Called getText{Field,Element}() but existing list pointer is not byte-sized.");
// Subtract 1 from the size for the NUL terminator.
......@@ -1263,9 +1263,9 @@ struct WireHelpers {
} else {
word* ptr = followFars(ref, segment);
PRECOND(ref->kind() == WirePointer::LIST,
REQUIRE(ref->kind() == WirePointer::LIST,
"Called getData{Field,Element}() but existing pointer is not a list.");
PRECOND(ref->listRef.elementSize() == FieldSize::BYTE,
REQUIRE(ref->listRef.elementSize() == FieldSize::BYTE,
"Called getData{Field,Element}() but existing list pointer is not byte-sized.");
return Data::Builder(reinterpret_cast<char*>(ptr), ref->listRef.elementCount() / ELEMENTS);
......@@ -1291,7 +1291,7 @@ struct WireHelpers {
if (ref->listRef.elementSize() == FieldSize::INLINE_COMPOSITE) {
// Read the tag to get the actual element count.
WirePointer* tag = reinterpret_cast<WirePointer*>(ptr);
PRECOND(tag->kind() == WirePointer::STRUCT,
REQUIRE(tag->kind() == WirePointer::STRUCT,
"INLINE_COMPOSITE list with non-STRUCT elements not supported.");
// First list element is at tag + 1 pointer.
......@@ -1951,7 +1951,7 @@ ObjectReader StructReader::getObjectField(
}
const word* StructReader::getUncheckedPointer(WirePointerCount ptrIndex) const {
PRECOND(segment == nullptr, "getUncheckedPointer() only allowed on unchecked messages.");
REQUIRE(segment == nullptr, "getUncheckedPointer() only allowed on unchecked messages.");
return reinterpret_cast<const word*>(pointers + ptrIndex);
}
......
......@@ -146,10 +146,10 @@ MallocMessageBuilder::MallocMessageBuilder(
kj::ArrayPtr<word> firstSegment, AllocationStrategy allocationStrategy)
: nextSize(firstSegment.size()), allocationStrategy(allocationStrategy),
ownFirstSegment(false), returnedFirstSegment(false), firstSegment(firstSegment.begin()) {
PRECOND(firstSegment.size() > 0, "First segment size must be non-zero.");
REQUIRE(firstSegment.size() > 0, "First segment size must be non-zero.");
// Checking just the first word should catch most cases of failing to zero the segment.
PRECOND(*reinterpret_cast<uint64_t*>(firstSegment.begin()) == 0,
REQUIRE(*reinterpret_cast<uint64_t*>(firstSegment.begin()) == 0,
"First segment must be zeroed.");
}
......@@ -218,12 +218,12 @@ FlatMessageBuilder::FlatMessageBuilder(kj::ArrayPtr<word> array): array(array),
FlatMessageBuilder::~FlatMessageBuilder() {}
void FlatMessageBuilder::requireFilled() {
PRECOND(getSegmentsForOutput()[0].end() == array.end(),
REQUIRE(getSegmentsForOutput()[0].end() == array.end(),
"FlatMessageBuilder's buffer was too large.");
}
kj::ArrayPtr<word> FlatMessageBuilder::allocateSegment(uint minimumSize) {
PRECOND(!allocated, "FlatMessageBuilder's buffer was not large enough.");
REQUIRE(!allocated, "FlatMessageBuilder's buffer was not large enough.");
allocated = true;
return array;
}
......
......@@ -447,7 +447,7 @@ public:
CONTEXT("checking compatibility with previously-loaded node of the same id",
existingNode.getDisplayName());
DPRECOND(existingNode.getId() == replacement.getId());
DREQUIRE(existingNode.getId() == replacement.getId());
nodeName = existingNode.getDisplayName();
compatibility = EQUIVALENT;
......@@ -1026,7 +1026,7 @@ internal::RawSchema* SchemaLoader::Impl::loadNative(const internal::RawSchema* n
if (slot == nullptr) {
slot = allocate<internal::RawSchema>();
} else if (slot->canCastTo != nullptr) {
PRECOND(slot->canCastTo == nativeSchema,
REQUIRE(slot->canCastTo == nativeSchema,
"two different compiled-in type have the same type ID",
reader.getId(), reader.getDisplayName(),
readMessageUnchecked<schema::Node>(slot->canCastTo->encodedNode).getDisplayName());
......@@ -1079,7 +1079,7 @@ internal::RawSchema* SchemaLoader::Impl::loadEmpty(
case schema::Node::Body::FILE_NODE:
case schema::Node::Body::CONST_NODE:
case schema::Node::Body::ANNOTATION_NODE:
FAIL_PRECOND("Not a type.");
FAIL_REQUIRE("Not a type.");
break;
}
......@@ -1111,7 +1111,7 @@ SchemaLoader::~SchemaLoader() {}
Schema SchemaLoader::get(uint64_t id) const {
internal::RawSchema* raw = impl->tryGet(id);
PRECOND(raw != nullptr, "no schema node loaded for id", id);
REQUIRE(raw != nullptr, "no schema node loaded for id", id);
return Schema(raw);
}
......
......@@ -51,33 +51,33 @@ Schema Schema::getDependency(uint64_t id) const {
}
}
FAIL_PRECOND("Requested ID not found in dependency table.", id);
FAIL_REQUIRE("Requested ID not found in dependency table.", id);
return Schema();
}
StructSchema Schema::asStruct() const {
PRECOND(getProto().getBody().which() == schema::Node::Body::STRUCT_NODE,
REQUIRE(getProto().getBody().which() == schema::Node::Body::STRUCT_NODE,
"Tried to use non-struct schema as a struct.",
getProto().getDisplayName());
return StructSchema(raw);
}
EnumSchema Schema::asEnum() const {
PRECOND(getProto().getBody().which() == schema::Node::Body::ENUM_NODE,
REQUIRE(getProto().getBody().which() == schema::Node::Body::ENUM_NODE,
"Tried to use non-enum schema as an enum.",
getProto().getDisplayName());
return EnumSchema(raw);
}
InterfaceSchema Schema::asInterface() const {
PRECOND(getProto().getBody().which() == schema::Node::Body::INTERFACE_NODE,
REQUIRE(getProto().getBody().which() == schema::Node::Body::INTERFACE_NODE,
"Tried to use non-interface schema as an interface.",
getProto().getDisplayName());
return InterfaceSchema(raw);
}
void Schema::requireUsableAs(const internal::RawSchema* expected) {
PRECOND(raw == expected ||
REQUIRE(raw == expected ||
(raw != nullptr && expected != nullptr && raw->canCastTo == expected),
"This schema is not compatible with the requested native type.");
}
......@@ -132,7 +132,7 @@ StructSchema::Member StructSchema::getMemberByName(Text::Reader name) const {
KJ_IF_MAYBE(member, findMemberByName(name)) {
return *member;
} else {
FAIL_PRECOND("struct has no such member", name);
FAIL_REQUIRE("struct has no such member", name);
}
}
......@@ -142,7 +142,7 @@ kj::Maybe<StructSchema::Union> StructSchema::Member::getContainingUnion() const
}
StructSchema::Union StructSchema::Member::asUnion() const {
PRECOND(proto.getBody().which() == schema::StructNode::Member::Body::UNION_MEMBER,
REQUIRE(proto.getBody().which() == schema::StructNode::Member::Body::UNION_MEMBER,
"Tried to use non-union struct member as a union.",
parent.getProto().getDisplayName(), proto.getName());
return Union(*this);
......@@ -160,7 +160,7 @@ StructSchema::Member StructSchema::Union::getMemberByName(Text::Reader name) con
KJ_IF_MAYBE(member, findMemberByName(name)) {
return *member;
} else {
FAIL_PRECOND("union has no such member", name);
FAIL_REQUIRE("union has no such member", name);
}
}
......@@ -178,7 +178,7 @@ EnumSchema::Enumerant EnumSchema::getEnumerantByName(Text::Reader name) const {
KJ_IF_MAYBE(enumerant, findEnumerantByName(name)) {
return *enumerant;
} else {
FAIL_PRECOND("enum has no such enumerant", name);
FAIL_REQUIRE("enum has no such enumerant", name);
}
}
......@@ -196,7 +196,7 @@ InterfaceSchema::Method InterfaceSchema::getMethodByName(Text::Reader name) cons
KJ_IF_MAYBE(method, findMethodByName(name)) {
return *method;
} else {
FAIL_PRECOND("interface has no such method", name);
FAIL_REQUIRE("interface has no such method", name);
}
}
......@@ -224,11 +224,11 @@ ListSchema ListSchema::of(schema::Type::Body::Which primitiveType) {
case schema::Type::Body::ENUM_TYPE:
case schema::Type::Body::INTERFACE_TYPE:
case schema::Type::Body::LIST_TYPE:
FAIL_PRECOND("Must use one of the other ListSchema::of() overloads for complex types.");
FAIL_REQUIRE("Must use one of the other ListSchema::of() overloads for complex types.");
break;
case schema::Type::Body::OBJECT_TYPE:
FAIL_PRECOND("List(Object) not supported.");
FAIL_REQUIRE("List(Object) not supported.");
break;
}
......@@ -267,7 +267,7 @@ ListSchema ListSchema::of(schema::Type::Reader elementType, Schema context) {
return of(of(body.getListType(), context));
case schema::Type::Body::OBJECT_TYPE:
FAIL_PRECOND("List(Object) not supported.");
FAIL_REQUIRE("List(Object) not supported.");
return ListSchema();
}
......@@ -276,31 +276,31 @@ ListSchema ListSchema::of(schema::Type::Reader elementType, Schema context) {
}
StructSchema ListSchema::getStructElementType() const {
PRECOND(nestingDepth == 0 && elementType == schema::Type::Body::STRUCT_TYPE,
REQUIRE(nestingDepth == 0 && elementType == schema::Type::Body::STRUCT_TYPE,
"ListSchema::getStructElementType(): The elements are not structs.");
return elementSchema.asStruct();
}
EnumSchema ListSchema::getEnumElementType() const {
PRECOND(nestingDepth == 0 && elementType == schema::Type::Body::ENUM_TYPE,
REQUIRE(nestingDepth == 0 && elementType == schema::Type::Body::ENUM_TYPE,
"ListSchema::getEnumElementType(): The elements are not enums.");
return elementSchema.asEnum();
}
InterfaceSchema ListSchema::getInterfaceElementType() const {
PRECOND(nestingDepth == 0 && elementType == schema::Type::Body::INTERFACE_TYPE,
REQUIRE(nestingDepth == 0 && elementType == schema::Type::Body::INTERFACE_TYPE,
"ListSchema::getInterfaceElementType(): The elements are not interfaces.");
return elementSchema.asInterface();
}
ListSchema ListSchema::getListElementType() const {
PRECOND(nestingDepth > 0,
REQUIRE(nestingDepth > 0,
"ListSchema::getListElementType(): The elements are not lists.");
return ListSchema(elementType, nestingDepth - 1, elementSchema);
}
void ListSchema::requireUsableAs(ListSchema expected) {
PRECOND(elementType == expected.elementType && nestingDepth == expected.nestingDepth,
REQUIRE(elementType == expected.elementType && nestingDepth == expected.nestingDepth,
"This schema is not compatible with the requested native type.");
elementSchema.requireUsableAs(expected.elementSchema.raw);
}
......
......@@ -39,8 +39,8 @@ size_t PackedInputStream::read(void* dst, size_t minBytes, size_t maxBytes) {
return 0;
}
DPRECOND(minBytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned.");
DPRECOND(maxBytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned.");
DREQUIRE(minBytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned.");
DREQUIRE(maxBytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned.");
uint8_t* __restrict__ out = reinterpret_cast<uint8_t*>(dst);
uint8_t* const outEnd = reinterpret_cast<uint8_t*>(dst) + maxBytes;
......@@ -190,7 +190,7 @@ void PackedInputStream::skip(size_t bytes) {
return;
}
DPRECOND(bytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned.");
DREQUIRE(bytes % sizeof(word) == 0, "PackedInputStream reads must be word-aligned.");
kj::ArrayPtr<const byte> buffer = inner.getReadBuffer();
const uint8_t* __restrict__ in = reinterpret_cast<const uint8_t*>(buffer.begin());
......
......@@ -88,7 +88,7 @@ kj::ArrayPtr<const word> FlatArrayMessageReader::getSegment(uint id) {
}
kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
PRECOND(segments.size() > 0, "Tried to serialize uninitialized message.");
REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message.");
size_t totalSize = segments.size() / 2 + 1;
......@@ -237,7 +237,7 @@ kj::ArrayPtr<const word> InputStreamMessageReader::getSegment(uint id) {
// -------------------------------------------------------------------
void writeMessage(kj::OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments) {
PRECOND(segments.size() > 0, "Tried to serialize uninitialized message.");
REQUIRE(segments.size() > 0, "Tried to serialize uninitialized message.");
internal::WireValue<uint32_t> table[(segments.size() + 2) & ~size_t(1)];
......
......@@ -94,7 +94,7 @@ public:
inline size_t size() const { return size_; }
inline T& operator[](size_t index) const {
KJ_INLINE_DPRECOND(index < size_, "Out-of-bounds Array access.");
KJ_IREQUIRE(index < size_, "Out-of-bounds Array access.");
return ptr[index];
}
......@@ -108,11 +108,11 @@ public:
inline T& back() { return *(ptr + size_ - 1); }
inline ArrayPtr<T> slice(size_t start, size_t end) {
KJ_INLINE_DPRECOND(start <= end && end <= size_, "Out-of-bounds Array::slice().");
KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice().");
return ArrayPtr<T>(ptr + start, end - start);
}
inline ArrayPtr<const T> slice(size_t start, size_t end) const {
KJ_INLINE_DPRECOND(start <= end && end <= size_, "Out-of-bounds Array::slice().");
KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice().");
return ArrayPtr<const T>(ptr + start, end - start);
}
......@@ -235,7 +235,7 @@ public:
inline size_t size() const { return pos - ptr; }
inline size_t capacity() const { return endPtr - ptr; }
inline T& operator[](size_t index) const {
KJ_INLINE_DPRECOND(index < pos - ptr, "Out-of-bounds Array access.");
KJ_IREQUIRE(index < pos - ptr, "Out-of-bounds Array access.");
return ptr[index];
}
......@@ -266,7 +266,7 @@ public:
template <typename... Params>
void add(Params&&... params) {
KJ_INLINE_DPRECOND(pos < endPtr, "Added too many elements to ArrayBuilder.");
KJ_IREQUIRE(pos < endPtr, "Added too many elements to ArrayBuilder.");
ctor(*pos, kj::fwd<Params>(params)...);
++pos;
}
......@@ -286,7 +286,7 @@ public:
// arbitrary disposers with ArrayBuilder in the future, and anyway this check might catch bugs.
// Probably we should just create a new Vector-like data structure if we want to allow building
// of arrays without knowing the final size in advance.
KJ_INLINE_DPRECOND(pos == endPtr, "ArrayBuilder::finish() called prematurely.");
KJ_IREQUIRE(pos == endPtr, "ArrayBuilder::finish() called prematurely.");
Array<T> result(reinterpret_cast<T*>(ptr), pos - ptr, internal::HeapArrayDisposer::instance);
ptr = nullptr;
pos = nullptr;
......
......@@ -27,8 +27,8 @@
namespace kj {
namespace internal {
void inlinePreconditionFailure(const char* file, int line, const char* expectation,
const char* macroArgs, const char* message) {
void inlineRequireFailure(const char* file, int line, const char* expectation,
const char* macroArgs, const char* message) {
if (message == nullptr) {
Log::fatalFault(file, line, Exception::Nature::PRECONDITION, expectation, macroArgs);
} else {
......
......@@ -113,24 +113,22 @@ typedef unsigned char byte;
namespace internal {
void inlinePreconditionFailure(
void inlineRequireFailure(
const char* file, int line, const char* expectation, const char* macroArgs,
const char* message = nullptr) KJ_NORETURN;
} // namespace internal
#define KJ_INLINE_PRECOND(condition, ...) \
if (KJ_EXPECT_TRUE(condition)); else ::kj::internal::inlinePreconditionFailure( \
#ifdef NDEBUG
#define KJ_IREQUIRE(condition, ...)
#else
#define KJ_IREQUIRE(condition, ...) \
if (KJ_EXPECT_TRUE(condition)); else ::kj::internal::inlineRequireFailure( \
__FILE__, __LINE__, #condition, #__VA_ARGS__, ##__VA_ARGS__)
// Version of PRECOND() which is safe to use in headers that are #included by users. Used to check
// Version of REQUIRE() which is safe to use in headers that are #included by users. Used to check
// preconditions inside inline methods. KJ_INLINE_DPRECOND is particularly useful in that
// it will be enabled depending on whether the application is compiled in debug mode rather than
// whether libkj is.
#ifdef NDEBUG
#define KJ_INLINE_DPRECOND(...)
#else
#define KJ_INLINE_DPRECOND KJ_INLINE_PRECOND
#endif
// #define KJ_STACK_ARRAY(type, name, size, minStack, maxStack)
......@@ -501,7 +499,7 @@ public:
inline size_t size() const { return size_; }
inline T& operator[](size_t index) const {
KJ_INLINE_DPRECOND(index < size_, "Out-of-bounds ArrayPtr access.");
KJ_IREQUIRE(index < size_, "Out-of-bounds ArrayPtr access.");
return ptr[index];
}
......@@ -511,7 +509,7 @@ public:
inline T& back() const { return *(ptr + size_ - 1); }
inline ArrayPtr slice(size_t start, size_t end) const {
KJ_INLINE_DPRECOND(start <= end && end <= size_, "Out-of-bounds ArrayPtr::slice().");
KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds ArrayPtr::slice().");
return ArrayPtr(ptr + start, end - start);
}
......@@ -578,9 +576,8 @@ To downcast(From* from) {
}
#if !KJ_NO_RTTI
KJ_INLINE_DPRECOND(
from == nullptr || dynamic_cast<To>(from) != nullptr,
"Value cannot be downcast() to requested type.");
KJ_IREQUIRE(from == nullptr || dynamic_cast<To>(from) != nullptr,
"Value cannot be downcast() to requested type.");
#endif
return static_cast<To>(from);
......
......@@ -174,7 +174,7 @@ void ExceptionCallback::logMessage(StringPtr text) {
}
void ExceptionCallback::useProcessWide() {
RECOVERABLE_PRECOND(globalCallback == nullptr,
RECOVERABLE_REQUIRE(globalCallback == nullptr,
"Can't register multiple global ExceptionCallbacks at once.") {
return;
}
......
......@@ -217,7 +217,7 @@ void ArrayOutputStream::write(const void* src, size_t size) {
// Oh goody, the caller wrote directly into our buffer.
fillPos += size;
} else {
PRECOND(size <= (size_t)(array.end() - fillPos),
REQUIRE(size <= (size_t)(array.end() - fillPos),
"ArrayOutputStream's backing array was not large enough for the data written.");
memcpy(fillPos, src, size);
fillPos += size;
......
......@@ -123,7 +123,7 @@ TEST(Logging, Log) {
"1 == 2; i = 123; hi; str = foo\n", mockCallback.text);
mockCallback.text.clear();
EXPECT_THROW(PRECOND(1 == 2, i, "hi", str), MockException); line = __LINE__;
EXPECT_THROW(REQUIRE(1 == 2, i, "hi", str), MockException); line = __LINE__;
EXPECT_EQ("fatal exception: " + fileLine(__FILE__, line) + ": precondition not met: expected "
"1 == 2; i = 123; hi; str = foo\n", mockCallback.text);
mockCallback.text.clear();
......
......@@ -43,7 +43,7 @@
// are disabled. This macro should be used to check for bugs in the surrounding code and its
// dependencies, but NOT to check for invalid input.
//
// * `PRECOND(condition, ...)`: Like `CHECK` but used to check preconditions -- i.e. to validate
// * `REQUIRE(condition, ...)`: Like `CHECK` but used to check preconditions -- e.g. to validate
// parameters passed from a caller. A failure indicates that the caller is buggy.
//
// * `RECOVERABLE_CHECK(condition, ...) { ... }`: Like `CHECK` except that if exceptions are
......@@ -51,7 +51,7 @@
// do whatever it can to fill in dummy values so that the code can continue executing, even if
// this means the eventual output will be garbage.
//
// * `RECOVERABLE_PRECOND(condition, ...) { ... }`: Like `RECOVERABLE_CHECK` and `PRECOND`.
// * `RECOVERABLE_REQUIRE(condition, ...) { ... }`: Like `RECOVERABLE_CHECK` and `REQUIRE`.
//
// * `VALIDATE_INPUT(condition, ...) { ... }`: Like `RECOVERABLE_PRECOND` but used to validate
// input that may have come from the user or some other untrusted source. Recoverability is
......@@ -228,14 +228,14 @@ ArrayPtr<const char> KJ_STRINGIFY(Log::Severity severity);
#define CHECK(...) FAULT(LOCAL_BUG, __VA_ARGS__)
#define RECOVERABLE_CHECK(...) RECOVERABLE_FAULT(LOCAL_BUG, __VA_ARGS__)
#define PRECOND(...) FAULT(PRECONDITION, __VA_ARGS__)
#define RECOVERABLE_PRECOND(...) RECOVERABLE_FAULT(PRECONDITION, __VA_ARGS__)
#define REQUIRE(...) FAULT(PRECONDITION, __VA_ARGS__)
#define RECOVERABLE_REQUIRE(...) RECOVERABLE_FAULT(PRECONDITION, __VA_ARGS__)
#define VALIDATE_INPUT(...) RECOVERABLE_FAULT(INPUT, __VA_ARGS__)
#define FAIL_CHECK(...) CHECK(false, ##__VA_ARGS__)
#define FAIL_RECOVERABLE_CHECK(...) RECOVERABLE_CHECK(false, ##__VA_ARGS__)
#define FAIL_PRECOND(...) PRECOND(false, ##__VA_ARGS__)
#define FAIL_RECOVERABLE_PRECOND(...) RECOVERABLE_PRECOND(false, ##__VA_ARGS__)
#define FAIL_REQUIRE(...) REQUIRE(false, ##__VA_ARGS__)
#define FAIL_RECOVERABLE_REQUIRE(...) RECOVERABLE_REQUIRE(false, ##__VA_ARGS__)
#define FAIL_VALIDATE_INPUT(...) VALIDATE_INPUT(false, ##__VA_ARGS__)
#define SYSCALL(call, ...) \
......@@ -274,14 +274,14 @@ ArrayPtr<const char> KJ_STRINGIFY(Log::Severity severity);
#define DLOG(...) do {} while (false)
#define DCHECK(...) do {} while (false)
#define RECOVERABLE_DCHECK(...) do {} while (false)
#define DPRECOND(...) do {} while (false)
#define RECOVERABLE_DPRECOND(...) do {} while (false)
#define DREQUIRE(...) do {} while (false)
#define RECOVERABLE_DREQUIRE(...) do {} while (false)
#else
#define DLOG LOG
#define DCHECK CHECK
#define RECOVERABLE_DCHECK RECOVERABLE_CHECK
#define DPRECOND PRECOND
#define RECOVERABLE_DPRECOND RECOVERABLE_PRECOND
#define DREQUIRE REQUIRE
#define RECOVERABLE_DREQUIRE RECOVERABLE_REQUIRE
#endif
template <typename... Params>
......
......@@ -365,7 +365,7 @@ inline const char* String::end() const { return content == nullptr ? nullptr : c
inline String::String(char* value, size_t size, const ArrayDisposer& disposer)
: content(value, size + 1, disposer) {
KJ_INLINE_DPRECOND(value[size] == '\0', "String must be NUL-terminated.");
KJ_IREQUIRE(value[size] == '\0', "String must be NUL-terminated.");
}
inline String heapString(const char* value) {
......
......@@ -309,8 +309,8 @@ inline {{unionFullName}}::Which {{unionFullName}}::Builder::which() {
{{#fieldIsPrimitive}}
inline {{fieldType}} {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
{{#fieldUnion}}
KJ_INLINE_DPRECOND(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return _reader.getDataField<{{fieldType}}>(
{{fieldOffset}} * ::capnproto::ELEMENTS{{fieldDefaultMask}});
......@@ -318,8 +318,8 @@ inline {{fieldType}} {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
inline {{fieldType}} {{typeFullName}}::Builder::get{{fieldTitleCase}}() {
{{#fieldUnion}}
KJ_INLINE_DPRECOND(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return _builder.getDataField<{{fieldType}}>(
{{fieldOffset}} * ::capnproto::ELEMENTS{{fieldDefaultMask}});
......@@ -346,8 +346,8 @@ inline bool {{typeFullName}}::Builder::has{{fieldTitleCase}}() {
{{^fieldIsGenericObject}}
inline {{fieldType}}::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
{{#fieldUnion}}
KJ_INLINE_DPRECOND(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<{{fieldType}}>::get(
_reader, {{fieldOffset}} * ::capnproto::POINTERS{{#fieldDefaultBytes}},
......@@ -356,8 +356,8 @@ inline {{fieldType}}::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
inline {{fieldType}}::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}() {
{{#fieldUnion}}
KJ_INLINE_DPRECOND(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<{{fieldType}}>::get(
_builder, {{fieldOffset}} * ::capnproto::POINTERS{{#fieldDefaultBytes}},
......@@ -413,8 +413,8 @@ inline {{fieldType}}::Builder {{typeFullName}}::Builder::init{{fieldTitleCase}}(
template <typename T>
inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
{{#fieldUnion}}
KJ_INLINE_DPRECOND(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::get(
_reader, {{fieldOffset}} * ::capnproto::POINTERS);
......@@ -423,8 +423,8 @@ inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}() {
template <typename T>
inline typename T::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}() {
{{#fieldUnion}}
KJ_INLINE_DPRECOND(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::get(
_builder, {{fieldOffset}} * ::capnproto::POINTERS);
......@@ -433,8 +433,8 @@ inline typename T::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}() {
template <typename T, typename Param>
inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}(Param&& param) {
{{#fieldUnion}}
KJ_INLINE_DPRECOND(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::getDynamic(
_reader, {{fieldOffset}} * ::capnproto::POINTERS, ::kj::fwd<Param>(param));
......@@ -443,8 +443,8 @@ inline typename T::Reader {{typeFullName}}::Reader::get{{fieldTitleCase}}(Param&
template <typename T, typename Param>
inline typename T::Builder {{typeFullName}}::Builder::get{{fieldTitleCase}}(Param&& param) {
{{#fieldUnion}}
KJ_INLINE_DPRECOND(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
KJ_IREQUIRE(which() == {{unionTitleCase}}::{{fieldUpperCase}},
"Must check which() before get()ing a union member.");
{{/fieldUnion}}
return ::capnproto::internal::PointerHelpers<T>::getDynamic(
_builder, {{fieldOffset}} * ::capnproto::POINTERS, ::kj::fwd<Param>(param));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment