From 22cc6f6c1bec6ea050069dc618555bc089c66357 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 27 Nov 2019 05:25:27 -0300 Subject: [PATCH] cmake: Always treat warnings as errors Enable cast warnings in gcc and clang and always treat warnings as errors. GetWordCount now returns std::size_t for simplicity and the word count is asserted and casted in WordCount (now called CalculateTotalWords. Silence warnings. --- CMakeLists.txt | 18 ++++++------------ src/literal_number.cpp | 2 +- src/literal_number.h | 4 +++- src/literal_string.cpp | 4 ++-- src/literal_string.h | 2 +- src/op.cpp | 22 ++++++++++++++-------- src/op.h | 9 +++++++-- src/operand.h | 4 +++- src/stream.cpp | 4 +++- 9 files changed, 40 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b77c1b..a7935d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,12 +54,8 @@ if (MSVC) /EHsc /Zc:throwingNew # Assumes new never returns null /Zc:inline # Omits inline functions from object-file output - /DNOMINMAX) - - if (SIRIT_WARNINGS_AS_ERRORS) - list(APPEND SIRIT_CXX_FLAGS - /WX) - endif() + /DNOMINMAX + /WX) if (CMAKE_VS_PLATFORM_TOOLSET MATCHES "LLVM-vs[0-9]+") list(APPEND SIRIT_CXX_FLAGS @@ -74,12 +70,10 @@ else() -pedantic -pedantic-errors -Wfatal-errors - -Wno-missing-braces) - - if (SIRIT_WARNINGS_AS_ERRORS) - list(APPEND SIRIT_CXX_FLAGS - -Werror) - endif() + -Wno-missing-braces + -Wconversion + -Wsign-conversion + -Werror) endif() # Enable unit-testing. diff --git a/src/literal_number.cpp b/src/literal_number.cpp index 4fafb9e..f418be4 100644 --- a/src/literal_number.cpp +++ b/src/literal_number.cpp @@ -22,7 +22,7 @@ void LiteralNumber::Fetch(Stream& stream) const { } } -u16 LiteralNumber::GetWordCount() const noexcept { +std::size_t LiteralNumber::GetWordCount() const noexcept { return is_32 ? 1 : 2; } diff --git a/src/literal_number.h b/src/literal_number.h index fda0226..6fd60d1 100644 --- a/src/literal_number.h +++ b/src/literal_number.h @@ -6,8 +6,10 @@ #pragma once +#include #include #include + #include "operand.h" #include "stream.h" @@ -20,7 +22,7 @@ public: void Fetch(Stream& stream) const override; - u16 GetWordCount() const noexcept override; + std::size_t GetWordCount() const noexcept override; bool operator==(const Operand& other) const noexcept override; diff --git a/src/literal_string.cpp b/src/literal_string.cpp index 9b8f062..bc8e6e1 100644 --- a/src/literal_string.cpp +++ b/src/literal_string.cpp @@ -19,8 +19,8 @@ void LiteralString::Fetch(Stream& stream) const { stream.Write(string); } -u16 LiteralString::GetWordCount() const noexcept { - return static_cast(string.size() / 4 + 1); +std::size_t LiteralString::GetWordCount() const noexcept { + return string.size() / 4 + 1; } bool LiteralString::operator==(const Operand& other) const noexcept { diff --git a/src/literal_string.h b/src/literal_string.h index 4cf5287..7c051d1 100644 --- a/src/literal_string.h +++ b/src/literal_string.h @@ -19,7 +19,7 @@ public: void Fetch(Stream& stream) const override; - u16 GetWordCount() const noexcept override; + std::size_t GetWordCount() const noexcept override; bool operator==(const Operand& other) const noexcept override; diff --git a/src/op.cpp b/src/op.cpp index f5b9b75..6319584 100644 --- a/src/op.cpp +++ b/src/op.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "common_types.h" #include "literal_number.h" @@ -25,7 +26,7 @@ void Op::Fetch(Stream& stream) const { stream.Write(id.value()); } -u16 Op::GetWordCount() const noexcept { +std::size_t Op::GetWordCount() const noexcept { return 1; } @@ -47,7 +48,7 @@ bool Op::operator==(const Operand& other) const noexcept { } void Op::Write(Stream& stream) const { - stream.Write(static_cast(opcode), WordCount()); + stream.Write(static_cast(opcode), CalculateTotalWords()); if (result_type) { result_type->Fetch(stream); @@ -99,7 +100,11 @@ void Op::Add(const Operand* operand) { } void Op::Add(u32 integer) { - Sink(LiteralNumber::Create(integer)); + Sink(LiteralNumber::Create(integer)); +} + +void Op::Add(s32 integer) { + Sink(LiteralNumber::Create(integer)); } void Op::Add(std::string string) { @@ -111,18 +116,19 @@ void Op::Add(const std::vector& ids) { operands.insert(operands.end(), ids.begin(), ids.end()); } -u16 Op::WordCount() const { - u16 count = 1; +u16 Op::CalculateTotalWords() const noexcept { + std::size_t count = 1; if (result_type) { - count++; + ++count; } if (id.has_value()) { - count++; + ++count; } for (const Operand* operand : operands) { count += operand->GetWordCount(); } - return count; + assert(count < std::numeric_limits::max()); + return static_cast(count); } } // namespace Sirit diff --git a/src/op.h b/src/op.h index a7413f8..cc16ac3 100644 --- a/src/op.h +++ b/src/op.h @@ -6,7 +6,10 @@ #pragma once +#include #include +#include + #include "common_types.h" #include "operand.h" #include "sirit/sirit.h" @@ -21,7 +24,7 @@ public: void Fetch(Stream& stream) const override; - u16 GetWordCount() const noexcept override; + std::size_t GetWordCount() const noexcept override; bool operator==(const Operand& other) const noexcept override; @@ -37,12 +40,14 @@ public: void Add(u32 integer); + void Add(s32 integer); + void Add(std::string string); void Add(const std::vector& ids); private: - u16 WordCount() const; + u16 CalculateTotalWords() const noexcept; spv::Op opcode; diff --git a/src/operand.h b/src/operand.h index 64e951b..4e19f80 100644 --- a/src/operand.h +++ b/src/operand.h @@ -6,6 +6,8 @@ #pragma once +#include + #include "stream.h" namespace Sirit { @@ -19,7 +21,7 @@ public: virtual void Fetch(Stream& stream) const = 0; - virtual u16 GetWordCount() const noexcept = 0; + virtual std::size_t GetWordCount() const noexcept = 0; virtual bool operator==(const Operand& other) const noexcept = 0; diff --git a/src/stream.cpp b/src/stream.cpp index 2282364..7a60faf 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -15,7 +15,9 @@ Stream::~Stream() = default; void Stream::Write(std::string_view string) { constexpr std::size_t word_size = 4; const auto size = string.size(); - auto read = [string, size](std::size_t offset) { return offset < size ? string[offset] : 0; }; + const auto read = [string, size](std::size_t offset) { + return offset < size ? static_cast(string[offset]) : u8(0); + }; words.reserve(words.size() + size / word_size + 1); for (std::size_t i = 0; i < size; i += word_size) {