Skip to content

Commit 849cabc

Browse files
committed
add a require utility function
In contrast to `assert` and similar functions, `require` works in no-assert builds the same way as in debug builds. It can be used to check invariants also in release builds.
1 parent 5d55621 commit 849cabc

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

include/swift/Basic/Require.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- Require.h ----------------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines swift_unreachable, which provides the
14+
// functionality of llvm_unreachable without necessarily depending on
15+
// the LLVM support libraries.
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_BASIC_REQUIRE_H
20+
#define SWIFT_BASIC_REQUIRE_H
21+
22+
#include "llvm/Support/raw_ostream.h"
23+
24+
/// Checks the `condition` and if it's false abort with `message`.
25+
/// In contrast to `assert` and similar functions, `require` works in
26+
/// no-assert builds the same way as in debug builds.
27+
inline void require(bool condition, const char *message) {
28+
if (!condition) {
29+
llvm::errs() << message << '\n';
30+
abort();
31+
}
32+
}
33+
34+
#endif

0 commit comments

Comments
 (0)