Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ cc_library(
strip_include_prefix = strip_include_prefix_config(),
deps = [
":assert_or_abort",
":compiler_compat",
":concepts",
":fixed_vector",
":preconditions",
Expand All @@ -640,6 +641,7 @@ cc_library(
strip_include_prefix = strip_include_prefix_config(),
deps = [
":algorithm",
":compiler_compat",
":concepts",
":iterator_utils",
":memory",
Expand Down Expand Up @@ -789,6 +791,9 @@ cc_library(
hdrs = ["include/fixed_containers/memory.hpp"],
includes = includes_config(),
strip_include_prefix = strip_include_prefix_config(),
deps = [
":compiler_compat",
],
copts = ["-std=c++20"],
)

Expand Down Expand Up @@ -1071,6 +1076,14 @@ cc_library(
copts = ["-std=c++20"],
)

cc_library(
name = "compiler_compat",
hdrs = ["include/fixed_containers/compiler_compat.hpp"],
includes = includes_config(),
strip_include_prefix = strip_include_prefix_config(),
copts = ["-std=c++20"],
)

cc_library(
name = "enums_test_common",
hdrs = ["test/enums_test_common.hpp"],
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,15 @@ ctest -C Debug

### bazel
#### clang
- clang++-20 and above [support function side effect verification](https://clang.llvm.org/docs/FunctionEffectAnalysis.html)

1) Build separately (optional)
```
CC=clang++-13 bazel build --config=clang ...
```
2) Run tests
```
CC=clang++-13 bazel test --config=clang :all_tests
CC=clang++-13 bazel test --config=clang --build_tests_only --nocache_test_results --runs_per_test=1 :all_tests
```

#### clang with libc++
Expand Down Expand Up @@ -458,7 +460,7 @@ The macro is needed to avoid analysis on some particularly slow places.

## Tested Compilers

- Clang 13
- Clang 13 (and 20)
- GCC 11
- MSVC++ 14.29 / Visual Studio 2019

Expand Down
50 changes: 50 additions & 0 deletions include/fixed_containers/compiler_compat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

/**
* @file compiler_compat.hpp
* @brief Compiler compatibility utilities and attributes.
*/

/**
* @brief FIXED_CONTAINERS_NONALLOCATING expands to [[clang::nonallocating]] when supported.
*
* This attribute helps clang's function effects analysis detect potentially allocating
* functions. It's only applied when:
* 1. Using clang compiler
* 2. Clang version supports the nonallocating attribute (clang 20+)
* - https://releases.llvm.org/20.1.0/tools/clang/docs/ReleaseNotes.html#static-analyzer
* 3. _GLIBCXX_ASSERTIONS is not enabled (to avoid false positives from debug assertions)
*
* @note _GLIBCXX_ASSERTIONS enables debug assertions in the standard library which contain calls
* to __builtin_printf that clang's function effects analysis considers potentially allocating,
* even though the code under test may not allocate in normal operation.
*/
#if defined(__clang__) && __clang_major__ >= 20 && !defined(_GLIBCXX_ASSERTIONS)
#define FIXED_CONTAINERS_NONALLOCATING [[clang::nonallocating]]
#else
#define FIXED_CONTAINERS_NONALLOCATING
#endif

/**
* @brief FIXED_CONTAINERS_SUPPRESS_FUNCTION_EFFECTS disables nonallocating and nonblocking checks.
*
* This macro applies diagnostic pragmas to suppress function effects warnings for code
* that is expected to be non-allocating but may trigger false positives in the compiler's
* conservative analysis.
*
* Usage:
* ```cpp
* FIXED_CONTAINERS_SUPPRESS_FUNCTION_EFFECTS(
* some_function_that_might_trigger_false_positive();
* )
* ```
*/
#if defined(__clang__)
#define FIXED_CONTAINERS_SUPPRESS_FUNCTION_EFFECTS(...) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wunknown-warning-option\"") \
_Pragma("clang diagnostic ignored \"-Wfunction-effects\"") \
__VA_ARGS__ _Pragma("clang diagnostic pop")
#else
#define FIXED_CONTAINERS_SUPPRESS_FUNCTION_EFFECTS(...) __VA_ARGS__
#endif
Loading
Loading