Skip to content

Commit 76720cf

Browse files
committed
[libc][annex_k] Add libc_constraint_handler macros.
1 parent b66fcb5 commit 76720cf

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

libc/src/__support/annex_k/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ add_header_library(
1111
libc.src.stdlib.abort
1212
)
1313

14+
add_header_library(
15+
constraint_macros
16+
HDRS
17+
constraint_macros.h
18+
DEPENDS
19+
.libc_constraint_handler
20+
libc.src.__support.libc_errno
21+
)
22+
1423
add_header_library(
1524
libc_constraint_handler
1625
HDRS
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- Helper macros header for constraint violations ----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_ANNEX_K_MACROS_H
10+
#define LLVM_LIBC_SRC___SUPPORT_ANNEX_K_MACROS_H
11+
12+
#include "libc_constraint_handler.h"
13+
#include "src/__support/libc_errno.h"
14+
15+
#define _CONSTRAINT_VIOLATION(msg, error_code, ret_code) \
16+
{ \
17+
libc_errno = error_code; \
18+
libc_constraint_handler(msg, nullptr, error_code); \
19+
return ret_code; \
20+
}
21+
22+
#define _CONSTRAINT_VIOLATION_IF(expr, error_code, return_code) \
23+
{ \
24+
auto expr_val = expr; \
25+
if (expr_val) { \
26+
libc_errno = error_code; \
27+
libc_constraint_handler(nullptr, nullptr, error_code); \
28+
return return_code; \
29+
} \
30+
}
31+
32+
#define _CONSTRAINT_VIOLATION_CLEANUP_IF(expr, cleanup, error_code, \
33+
return_code) \
34+
{ \
35+
auto expr_val = expr; \
36+
if (expr_val) { \
37+
cleanup; \
38+
libc_errno = error_code; \
39+
libc_constraint_handler(nullptr, nullptr, error_code); \
40+
return return_code; \
41+
} \
42+
}
43+
44+
#endif // LLVM_LIBC_SRC___SUPPORT_ANNEX_K_MACROS_H

0 commit comments

Comments
 (0)