Skip to content

Commit f42975f

Browse files
committed
Swift: add assertion and expectation macros
1 parent ba49386 commit f42975f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <cstdlib>
4+
5+
#include "swift/extractor/infra/log/SwiftLogging.h"
6+
7+
// assert CONDITION, which is always evaluated (once) regardless of the build type. If
8+
// CONDITION is not satisfied, emit a critical log optionally using provided format and arguments,
9+
// abort the program
10+
#define CODEQL_ASSERT(CONDITION, ...) \
11+
CODEQL_ASSERT_IMPL(CRITICAL, std::abort(), CONDITION, __VA_ARGS__)
12+
13+
// If CONDITION is not satisfied, emit an error log optionally using provided format and arguments,
14+
// but continue execution
15+
#define CODEQL_EXPECT(CONDITION, ...) CODEQL_EXPECT_OR(void(), CONDITION, __VA_ARGS__)
16+
17+
// If CONDITION is not satisfied, emit an error log optionally using provided format and arguments,
18+
// and execute ACTION (for example return)
19+
#define CODEQL_EXPECT_OR(ACTION, CONDITION, ...) \
20+
CODEQL_ASSERT_IMPL(ERROR, ACTION, CONDITION, __VA_ARGS__)
21+
22+
#define CODEQL_ASSERT_IMPL(LEVEL, ACTION, CONDITION, ...) \
23+
do { \
24+
if (!(CONDITION)) { \
25+
[[unlikely]] LOG_##LEVEL("assertion failed on " #CONDITION ". " __VA_ARGS__); \
26+
codeql::Log::flush(); \
27+
ACTION; \
28+
} \
29+
} while (false)

0 commit comments

Comments
 (0)