Skip to content

Commit

Permalink
Merge pull request #266 from chaoticgd/warn_unused
Browse files Browse the repository at this point in the history
Add the [[gnu::warn_unused]] attribute to the Result template
  • Loading branch information
chaoticgd authored Nov 28, 2024
2 parents 62c9964 + 167f806 commit 773c2b2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
17 changes: 10 additions & 7 deletions docs/ErrorHandling.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Error Handling

CCC uses a custom type template for error handling called `Result` which
packages together a return type and a pointer to error information. This allows
packages together a return value and a pointer to error information. This allows
errors to be treated as values, and enables CCC to be compiled in environments
where C++ exceptions are disabled.

It is defined like so:

```
class [[nodiscard]] Result {
class [[nodiscard]] CCC_WARN_UNUSED Result {
...
protected:
Value m_value;
Expand All @@ -17,11 +17,14 @@ protected:
};
```

Note the `nodiscard` attribute. This means if you ignore the return value of a
function returning a `Result` object, the compiler will warn you. Additionally,
if you assign the return value to an object, but do not use the object, your
compiler is likely to give you a warning about an unused variable. These two
warnings in combination make these `Result` types much easier to use.
Note the `nodiscard` attribute. This makes it so if you ignore the return value
of a function returning a `Result` object, the compiler will generate a warning.

Additionally, the `CCC_WARN_UNUSED` macro makes it so that if you assign a
returned `Result` object to a local variable but do not use the local variable,
supported compilers will generate a warning in that case too.

These two warnings in combination make these `Result` types much easier to use.

## Basic Usage

Expand Down
10 changes: 8 additions & 2 deletions src/ccc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ void set_custom_error_callback(CustomErrorCallback callback);
#define CCC_ASSERT(condition) \
CCC_ABORT_IF_FALSE(condition, #condition)

#if defined(__GNUC__) || defined(__clang__)
#define CCC_WARN_UNUSED [[gnu::warn_unused]]
#else
#define CCC_WARN_UNUSED
#endif

// The main error handling construct in CCC. This class is used to bundle
// together a return value and a pointer to error information, so that errors
// can be propagated up the stack.
template <typename Value>
class [[nodiscard]] Result {
class [[nodiscard]] CCC_WARN_UNUSED Result {
template <typename OtherValue>
friend class Result;
protected:
Expand Down Expand Up @@ -149,7 +155,7 @@ class [[nodiscard]] Result {
};

template <>
class [[nodiscard]] Result<void> : public Result<int> {
class [[nodiscard]] CCC_WARN_UNUSED Result<void> : public Result<int> {
public:
Result() : Result<int>(0) {}

Expand Down
17 changes: 17 additions & 0 deletions test/ccc/symbol_database_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,25 @@ TEST(CCCSymbolDatabase, HandlesFromName)

// Create the symbols.
Result<DataType*> a = database.data_types.create_symbol("A", (*source)->handle());
CCC_GTEST_FAIL_IF_ERROR(a);

Result<DataType*> b_1 = database.data_types.create_symbol("B", (*source)->handle());
CCC_GTEST_FAIL_IF_ERROR(b_1);

Result<DataType*> b_2 = database.data_types.create_symbol("B", (*source)->handle());
CCC_GTEST_FAIL_IF_ERROR(b_2);

Result<DataType*> c_1 = database.data_types.create_symbol("C", (*source)->handle());
CCC_GTEST_FAIL_IF_ERROR(c_1);

Result<DataType*> c_2 = database.data_types.create_symbol("C", (*source)->handle());
CCC_GTEST_FAIL_IF_ERROR(c_2);

Result<DataType*> c_3 = database.data_types.create_symbol("C", (*source)->handle());
CCC_GTEST_FAIL_IF_ERROR(c_3);

Result<DataType*> d = database.data_types.create_symbol("D", (*source)->handle());
CCC_GTEST_FAIL_IF_ERROR(d);

// Destroy D.
database.data_types.mark_symbol_for_destruction((*d)->handle(), &database);
Expand Down Expand Up @@ -415,6 +428,7 @@ TEST(CCCSymbolDatabase, DeduplicateWobblyTypedefs)
std::unique_ptr<ast::BuiltIn> underlying_type = std::make_unique<ast::BuiltIn>();
Result<DataType*> underlying_symbol = database.create_data_type_if_unique(
std::move(underlying_type), StabsTypeNumber{1,1}, "Underlying", **file, group);
CCC_GTEST_FAIL_IF_ERROR(underlying_symbol);

// Create a typedef for that builtin.
std::unique_ptr<ast::TypeName> typedef_type = std::make_unique<ast::TypeName>();
Expand All @@ -426,6 +440,7 @@ TEST(CCCSymbolDatabase, DeduplicateWobblyTypedefs)
typedef_type->unresolved_stabs->stabs_type_number.type = 1;
Result<DataType*> typedef_symbol = database.create_data_type_if_unique(
std::move(typedef_type), StabsTypeNumber{1,2}, "Typedef", **file, group);
CCC_GTEST_FAIL_IF_ERROR(typedef_symbol);

// Create a struct referencing the builtin type directly.
std::unique_ptr<ast::StructOrUnion> struct_underlying_type = std::make_unique<ast::StructOrUnion>();
Expand All @@ -438,6 +453,7 @@ TEST(CCCSymbolDatabase, DeduplicateWobblyTypedefs)
struct_underlying_type->fields.emplace_back(std::move(member_underlying_type));
Result<DataType*> struct_underlying_symbol = database.create_data_type_if_unique(
std::move(struct_underlying_type), StabsTypeNumber{1,3}, "WobblyStruct", **file, group);
CCC_GTEST_FAIL_IF_ERROR(struct_underlying_symbol);

// Create a struct referencing the builtin through the typedef.
std::unique_ptr<ast::StructOrUnion> struct_typedef_type = std::make_unique<ast::StructOrUnion>();
Expand All @@ -450,6 +466,7 @@ TEST(CCCSymbolDatabase, DeduplicateWobblyTypedefs)
struct_typedef_type->fields.emplace_back(std::move(member_typedef_type));
Result<DataType*> struct_typedef_symbol = database.create_data_type_if_unique(
std::move(struct_typedef_type), StabsTypeNumber{1,4}, "WobblyStruct", **file, group);
CCC_GTEST_FAIL_IF_ERROR(struct_typedef_symbol);

// Validate that the two structs were deduplicated despite not being equal.
auto handles = database.data_types.handles_from_name("WobblyStruct");
Expand Down

0 comments on commit 773c2b2

Please sign in to comment.