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.
This commit is contained in:
ReinUsesLisp 2019-11-27 05:25:27 -03:00
parent 71b53b855a
commit 22cc6f6c1b
9 changed files with 40 additions and 29 deletions

View file

@ -54,12 +54,8 @@ if (MSVC)
/EHsc /EHsc
/Zc:throwingNew # Assumes new never returns null /Zc:throwingNew # Assumes new never returns null
/Zc:inline # Omits inline functions from object-file output /Zc:inline # Omits inline functions from object-file output
/DNOMINMAX) /DNOMINMAX
if (SIRIT_WARNINGS_AS_ERRORS)
list(APPEND SIRIT_CXX_FLAGS
/WX) /WX)
endif()
if (CMAKE_VS_PLATFORM_TOOLSET MATCHES "LLVM-vs[0-9]+") if (CMAKE_VS_PLATFORM_TOOLSET MATCHES "LLVM-vs[0-9]+")
list(APPEND SIRIT_CXX_FLAGS list(APPEND SIRIT_CXX_FLAGS
@ -74,13 +70,11 @@ else()
-pedantic -pedantic
-pedantic-errors -pedantic-errors
-Wfatal-errors -Wfatal-errors
-Wno-missing-braces) -Wno-missing-braces
-Wconversion
if (SIRIT_WARNINGS_AS_ERRORS) -Wsign-conversion
list(APPEND SIRIT_CXX_FLAGS
-Werror) -Werror)
endif() endif()
endif()
# Enable unit-testing. # Enable unit-testing.
enable_testing(true) enable_testing(true)

View file

@ -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; return is_32 ? 1 : 2;
} }

View file

@ -6,8 +6,10 @@
#pragma once #pragma once
#include <cstddef>
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include "operand.h" #include "operand.h"
#include "stream.h" #include "stream.h"
@ -20,7 +22,7 @@ public:
void Fetch(Stream& stream) const override; 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; bool operator==(const Operand& other) const noexcept override;

View file

@ -19,8 +19,8 @@ void LiteralString::Fetch(Stream& stream) const {
stream.Write(string); stream.Write(string);
} }
u16 LiteralString::GetWordCount() const noexcept { std::size_t LiteralString::GetWordCount() const noexcept {
return static_cast<u16>(string.size() / 4 + 1); return string.size() / 4 + 1;
} }
bool LiteralString::operator==(const Operand& other) const noexcept { bool LiteralString::operator==(const Operand& other) const noexcept {

View file

@ -19,7 +19,7 @@ public:
void Fetch(Stream& stream) const override; 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; bool operator==(const Operand& other) const noexcept override;

View file

@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <limits>
#include "common_types.h" #include "common_types.h"
#include "literal_number.h" #include "literal_number.h"
@ -25,7 +26,7 @@ void Op::Fetch(Stream& stream) const {
stream.Write(id.value()); stream.Write(id.value());
} }
u16 Op::GetWordCount() const noexcept { std::size_t Op::GetWordCount() const noexcept {
return 1; return 1;
} }
@ -47,7 +48,7 @@ bool Op::operator==(const Operand& other) const noexcept {
} }
void Op::Write(Stream& stream) const { void Op::Write(Stream& stream) const {
stream.Write(static_cast<u16>(opcode), WordCount()); stream.Write(static_cast<u16>(opcode), CalculateTotalWords());
if (result_type) { if (result_type) {
result_type->Fetch(stream); result_type->Fetch(stream);
@ -99,7 +100,11 @@ void Op::Add(const Operand* operand) {
} }
void Op::Add(u32 integer) { void Op::Add(u32 integer) {
Sink(LiteralNumber::Create<u32>(integer)); Sink(LiteralNumber::Create(integer));
}
void Op::Add(s32 integer) {
Sink(LiteralNumber::Create(integer));
} }
void Op::Add(std::string string) { void Op::Add(std::string string) {
@ -111,18 +116,19 @@ void Op::Add(const std::vector<Id>& ids) {
operands.insert(operands.end(), ids.begin(), ids.end()); operands.insert(operands.end(), ids.begin(), ids.end());
} }
u16 Op::WordCount() const { u16 Op::CalculateTotalWords() const noexcept {
u16 count = 1; std::size_t count = 1;
if (result_type) { if (result_type) {
count++; ++count;
} }
if (id.has_value()) { if (id.has_value()) {
count++; ++count;
} }
for (const Operand* operand : operands) { for (const Operand* operand : operands) {
count += operand->GetWordCount(); count += operand->GetWordCount();
} }
return count; assert(count < std::numeric_limits<u16>::max());
return static_cast<u16>(count);
} }
} // namespace Sirit } // namespace Sirit

View file

@ -6,7 +6,10 @@
#pragma once #pragma once
#include <cstddef>
#include <optional> #include <optional>
#include <vector>
#include "common_types.h" #include "common_types.h"
#include "operand.h" #include "operand.h"
#include "sirit/sirit.h" #include "sirit/sirit.h"
@ -21,7 +24,7 @@ public:
void Fetch(Stream& stream) const override; 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; bool operator==(const Operand& other) const noexcept override;
@ -37,12 +40,14 @@ public:
void Add(u32 integer); void Add(u32 integer);
void Add(s32 integer);
void Add(std::string string); void Add(std::string string);
void Add(const std::vector<Id>& ids); void Add(const std::vector<Id>& ids);
private: private:
u16 WordCount() const; u16 CalculateTotalWords() const noexcept;
spv::Op opcode; spv::Op opcode;

View file

@ -6,6 +6,8 @@
#pragma once #pragma once
#include <cstddef>
#include "stream.h" #include "stream.h"
namespace Sirit { namespace Sirit {
@ -19,7 +21,7 @@ public:
virtual void Fetch(Stream& stream) const = 0; 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; virtual bool operator==(const Operand& other) const noexcept = 0;

View file

@ -15,7 +15,9 @@ Stream::~Stream() = default;
void Stream::Write(std::string_view string) { void Stream::Write(std::string_view string) {
constexpr std::size_t word_size = 4; constexpr std::size_t word_size = 4;
const auto size = string.size(); 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<u8>(string[offset]) : u8(0);
};
words.reserve(words.size() + size / word_size + 1); words.reserve(words.size() + size / word_size + 1);
for (std::size_t i = 0; i < size; i += word_size) { for (std::size_t i = 0; i < size; i += word_size) {