catchorg / catchorg/Catch2

Compacted Structs and Assertion Macro Expressions

Open
#1,977 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

**Description**
Catch2 assertion macros with compacted structs seem to expand the contents instead of just evaluating the truthiness of the overall statement. The statement needs to be wrapped with parentheses in order for the assertion macro to properly align the attributes of a compacted struct. This issue only occurs with the Xtensa compiler(clang++ and g++ compiled catch executable passed).

Example:
```cpp
#include "catch.hpp"
TEST_CASE("Compacted Struct Example")
{
#pragma pack(push, 1)
struct
{
uint8_t data1;
uint16_t data2;
uint32_t data3;
uint16_t data4;
uint8_t data5;
} buffer;
#pragma pack(pop)

buffer.data1 = 0x01;
buffer.data2 = 0x2345;
buffer.data3 = 0x6789ABCD;
buffer.data4 = 0xEF01;
buffer.data5 = 0x23;

REQUIRE((buffer.data1 == 0x01)); // pass
REQUIRE((buffer.data2 == 0x2345)); // pass
REQUIRE((buffer.data3 == 0x6789ABCD)); // pass
REQUIRE((buffer.data4 == 0xEF01)); // pass
REQUIRE((buffer.data5 == 0x23)); // pass

REQUIRE(buffer.data1 == 0x01); // pass
REQUIRE(buffer.data2 == 0x2345); // fail 17665 == 9029 (0x2345)
REQUIRE(buffer.data3 == 0x6789ABCD); // fail 3441640705 (0xcd234501) == 1737075661 (0x6789abcd)
REQUIRE(buffer.data4 == 0xEF01); // fail 359 == 61185 (0xef01)
REQUIRE(buffer.data5 == 0x23); // pass
}
```

**Expected behavior**
In the example above, I would expect this line to pass, but instead, it's misaligned.
```cpp
REQUIRE(buffer.data2 == 0x2345);
```

**Platform information:**

- Compiler+version: **Xtensa 14.02 Lx7 Instruction Set Simulator(clang++ based)**
- Catch version: **v2.9.2**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.