VSF Documented
esp_compiler.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18/*
19 * Clean-room re-implementation of ESP-IDF public API "esp_compiler.h".
20 *
21 * Provides compiler-hint macros (likely/unlikely, diagnostic push/pop,
22 * designated-initialiser workarounds). Compiler identity uses the same
23 * __IS_COMPILER_* convention as VSF's utilities/compiler layer.
24 *
25 * Baseline: ESP-IDF v5.1 public API.
26 */
27
28#ifndef __VSF_ESPIDF_ESP_COMPILER_H__
29#define __VSF_ESPIDF_ESP_COMPILER_H__
30
31/* ------------------------------------------------------------------ */
32/* Compiler identity (mirrors VSF utilities/compiler detect headers) */
33/* ------------------------------------------------------------------ */
34#ifndef __IS_COMPILER_LLVM__
35# if defined(__clang__)
36# define __IS_COMPILER_LLVM__ 1
37# endif
38#endif
39#ifndef __IS_COMPILER_GCC__
40# if defined(__GNUC__) && !defined(__clang__)
41# define __IS_COMPILER_GCC__ 1
42# endif
43#endif
44
45/* ------------------------------------------------------------------ */
46/* likely / unlikely — branch-prediction hints */
47/* ------------------------------------------------------------------ */
48#if __IS_COMPILER_GCC__ || __IS_COMPILER_LLVM__
49# ifndef likely
50# define likely(x) __builtin_expect(!!(x), 1)
51# endif
52# ifndef unlikely
53# define unlikely(x) __builtin_expect(!!(x), 0)
54# endif
55#else
56# ifndef likely
57# define likely(x) (x)
58# endif
59# ifndef unlikely
60# define unlikely(x) (x)
61# endif
62#endif
63
64/* ------------------------------------------------------------------ */
65/* Designated-initialiser workarounds (C99 / C++17 / C++20) */
66/* ------------------------------------------------------------------ */
67#if defined(__cplusplus) && (__cplusplus >= 202002L)
68# define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(member, value) .member = value,
69# define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(member) .member = { },
70#elif defined(__cplusplus) && (__cplusplus < 202002L)
71# define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(member, value) { .member = value },
72# define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(member) .member = { },
73#else
74# define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(member, value) .member = value,
75# define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(member)
76#endif
77
78/* ------------------------------------------------------------------ */
79/* Diagnostic push / pop (GCC / Clang / IAR) */
80/* ------------------------------------------------------------------ */
81#define __ESP_COMPILER_PRAGMA__(string) _Pragma(#string)
82#define _ESP_COMPILER_PRAGMA_(string) __ESP_COMPILER_PRAGMA__(string)
83
84#if __IS_COMPILER_LLVM__
85# define ESP_COMPILER_DIAGNOSTIC_PUSH_IGNORE(warning) \
86 __ESP_COMPILER_PRAGMA__(clang diagnostic push) \
87 __ESP_COMPILER_PRAGMA__(clang diagnostic ignored "-Wunknown-warning-option") \
88 __ESP_COMPILER_PRAGMA__(clang diagnostic ignored warning)
89# define ESP_COMPILER_DIAGNOSTIC_POP(warning) \
90 __ESP_COMPILER_PRAGMA__(clang diagnostic pop)
91#elif __IS_COMPILER_GCC__
92# define ESP_COMPILER_DIAGNOSTIC_PUSH_IGNORE(warning) \
93 __ESP_COMPILER_PRAGMA__(GCC diagnostic push) \
94 __ESP_COMPILER_PRAGMA__(GCC diagnostic ignored "-Wpragmas") \
95 __ESP_COMPILER_PRAGMA__(GCC diagnostic ignored warning)
96# define ESP_COMPILER_DIAGNOSTIC_POP(warning) \
97 __ESP_COMPILER_PRAGMA__(GCC diagnostic pop)
98#else
99# define ESP_COMPILER_DIAGNOSTIC_PUSH_IGNORE(warning)
100# define ESP_COMPILER_DIAGNOSTIC_POP(warning)
101#endif
102
103/* ------------------------------------------------------------------ */
104/* Static analyzer hint (clang-analyzer only) */
105/* ------------------------------------------------------------------ */
106#if defined(__clang_analyzer__)
107# define ESP_STATIC_ANALYZER_CHECK(_expr_, _ret_) \
108 do { if ((_expr_)) { return (_ret_); } } while(0)
109#else
110# define ESP_STATIC_ANALYZER_CHECK(_expr_, _ret_)
111#endif
112
113#endif // __VSF_ESPIDF_ESP_COMPILER_H__
Generated from commit: vsfteam/vsf@c3767bf