Skip to content

Commit 25ce041

Browse files
authored
build: re-enable warnings as errors (#5)
And address some of the warnings
1 parent 30618c3 commit 25ce041

File tree

11 files changed

+84
-26
lines changed

11 files changed

+84
-26
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ if (${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR})
4949
endif()
5050

5151
project_options(
52-
# WARNINGS_AS_ERRORS
52+
WARNINGS_AS_ERRORS
5353
# ENABLE_COVERAGE
5454
# ENABLE_CPPCHECK
5555
# ENABLE_CLANG_TIDY
@@ -74,7 +74,7 @@ project_options(
7474

7575
# TODO: Address all the warning below. This should be only temporary...
7676
target_compile_options(project_warnings
77-
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-Wno-sign-conversion;-Wno-shorten-64-to-32;-Wno-shadow;-Wno-implicit-fallthrough;-Wno-implicit-int-conversion;-Wno-old-style-cast;-Wno-gnu-zero-variadic-macro-arguments;-Wno-implicit-int-float-conversion;-Wno-deprecated-copy;-Wno-missing-field-initializers>
77+
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-Wno-sign-conversion;-Wno-shadow;-Wno-implicit-fallthrough;-Wno-old-style-cast;-Wno-deprecated-copy;-Wno-missing-field-initializers;-Wno-null-dereference;-Wno-maybe-uninitialized>
7878
)
7979

8080
check_cxx_source_compiles(

src/executable/mlir/Target/PythonBytecode/LinearScanRegisterAllocation.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,21 +466,21 @@ class LinearScanRegisterAllocation
466466
auto current_value = std::get<mlir::Value>(cur.value);
467467

468468
// Resolve block arguments to their defining operations
469-
if (current_value.isa<mlir::BlockArgument>()) {
469+
if (mlir::isa<mlir::BlockArgument>(current_value)) {
470470
if (auto it = live_interval_analysis.block_input_mappings.find(cur.value);
471471
it != live_interval_analysis.block_input_mappings.end()) {
472472
for (auto mapped_value : it->second) {
473473
ASSERT(!std::holds_alternative<ForwardedOutput>(mapped_value));
474474
if (clobbers_r0(std::get<mlir::Value>(mapped_value))) {
475-
ASSERT(current_value.isa<mlir::BlockArgument>());
475+
ASSERT(mlir::isa<mlir::BlockArgument>(current_value));
476476
current_value = std::get<mlir::Value>(mapped_value);
477477
break;
478478
}
479479
}
480480
}
481481
}
482482

483-
ASSERT(!current_value.isa<mlir::BlockArgument>());
483+
ASSERT(!mlir::isa<mlir::BlockArgument>(current_value));
484484
auto loc = current_value.getLoc();
485485

486486
// Insert: push r{cur_reg}, move r{scratch}, r{cur_reg}, pop r{cur_reg}

src/runtime/PyArgParser.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "TypeError.hpp"
99
#include "vm/VM.hpp"
1010

11+
#include <limits>
12+
1113
namespace py {
1214

1315
template<typename... ArgTypes> struct PyArgsParser
@@ -56,7 +58,15 @@ template<typename... ArgTypes> struct PyArgsParser
5658
return Err(type_error("'{}' object cannot be interpreted as an integer",
5759
int_obj.unwrap()->type()->name()));
5860
} else {
59-
std::get<Idx>(result) = as<PyInteger>(int_obj.unwrap())->as_i64();
61+
auto value = as<PyInteger>(int_obj.unwrap())->as_i64();
62+
static_assert(sizeof(ExpectedType) <= 8);
63+
if (!fits_in<ExpectedType>(value)) {
64+
return Err(type_error("{} not within range ({}, {})",
65+
value,
66+
std::numeric_limits<ExpectedType>::min(),
67+
std::numeric_limits<ExpectedType>::max()));
68+
}
69+
std::get<Idx>(result) = static_cast<ExpectedType>(value);
6070
}
6171
} else {
6272
[]<bool flag = false>() {

src/runtime/PyList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ PyResult<PyObject *> PyList::__mul__(size_t count) const
505505
if (count <= 0) { return PyList::create(); }
506506
std::vector<Value> values;
507507
values.reserve(count * m_elements.size());
508-
for (auto _ : std::views::iota(size_t{ 0 }, count)) {
508+
for ([[maybe_unused]] auto _ : std::views::iota(size_t{ 0 }, count)) {
509509
values.insert(values.end(), m_elements.begin(), m_elements.end());
510510
}
511511

src/runtime/PyString.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,11 @@ PyResult<PyString *> PyString::create(const Bytes &bytes, const std::string &enc
150150
if (byte < std::byte{ 128 }) {
151151
result.push_back(static_cast<char>(byte));
152152
} else {
153-
result.push_back(0xc2 + (static_cast<unsigned char>(byte) > 0xbf));
154-
result.push_back((static_cast<char>(byte) & 0x3f) + 0x80);
153+
result.push_back(
154+
static_cast<unsigned char>(0xc2) + static_cast<unsigned char>(byte) > 0xbf);
155+
result.push_back(static_cast<char>(
156+
(static_cast<unsigned char>(byte) & static_cast<unsigned char>(0x3f))
157+
+ static_cast<unsigned char>(0x80)));
155158
}
156159
}
157160
return PyString::create(result);
@@ -1864,9 +1867,14 @@ PyResult<PyObject *> PyString::translate(PyObject *table) const
18641867
if (mapped_cp > 0x110000 || mapped_cp < 0) {
18651868
return Err(value_error("character mapping must be in range(0x110000)"));
18661869
}
1867-
auto el = utf8::utf8chr(mapped_cp.get_ui());
1870+
auto mapped_cp_ui = mapped_cp.get_ui();
1871+
if (!fits_in<uint32_t>(mapped_cp_ui)) {
1872+
return Err(value_error(
1873+
"character mapping value {} does not fit in uint32_t", mapped_cp_ui));
1874+
}
1875+
auto el = utf8::utf8chr(static_cast<uint32_t>(mapped_cp_ui));
18681876
if (!el.has_value()) {}
1869-
cache[mapped_cp.get_ui()] = *el;
1877+
cache[static_cast<uint32_t>(mapped_cp_ui)] = *el;
18701878
result.append(*el);
18711879
} else if (mapped_value.unwrap()->type()->issubclass(types::str())) {
18721880
auto mapped_str = static_cast<const PyString &>(*mapped_value.unwrap()).value();

src/runtime/Value.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ bool Bytes::operator==(const PyObject *other) const
455455
constexpr unsigned char to_digit_value(char value)
456456
{
457457
if (value >= 0 && value <= 9) { return value; }
458-
if (value >= 'a' && value <= 'z') { return (value - 'a') + 10; }
459-
if (value >= 'A' && value <= 'Z') { return (value - 'A') + 10; }
458+
if (value >= 'a' && value <= 'z') { return static_cast<unsigned char>((value - 'a') + 10); }
459+
if (value >= 'A' && value <= 'Z') { return static_cast<unsigned char>((value - 'A') + 10); }
460460
return 37;
461461
}
462462

@@ -611,12 +611,6 @@ bool NameConstant::operator==(const NameConstant &other) const
611611
[](const auto &rhs, const auto &lhs) { return rhs == lhs; }, value, other.value);
612612
}
613613

614-
std::ostream &py::operator<<(std::ostream &os, const Tuple &tuple)
615-
{
616-
os << tuple.to_string();
617-
return os;
618-
}
619-
620614
std::string Tuple::to_string() const
621615
{
622616
std::ostringstream os;
@@ -642,6 +636,12 @@ std::string Tuple::to_string() const
642636

643637
namespace py {
644638

639+
std::ostream &operator<<(std::ostream &os, const Tuple &tuple)
640+
{
641+
os << tuple.to_string();
642+
return os;
643+
}
644+
645645
PyResult<Value> add(const Value &lhs, const Value &rhs, Interpreter &)
646646
{
647647
return std::visit(

src/runtime/modules/signal/module.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <csignal>
1313
#include <cstdint>
14+
#include <limits>
1415
#include <variant>
1516

1617
namespace py {
@@ -83,7 +84,10 @@ PyResult<PyObject *> signal(PyTuple *args, PyDict *kwargs)
8384
previous_handler = std::get<PyObject *>(it->second);
8485
}
8586

86-
if (std::signal(signalnum, sighandler) == SIG_ERR) {
87+
if (signalnum >= NSIG) { return Err(value_error("signal number out of range")); }
88+
89+
static_assert(NSIG < std::numeric_limits<int>::max());
90+
if (std::signal(static_cast<int>(signalnum), sighandler) == SIG_ERR) {
8791
return Err(value_error("error setting signal handler"));
8892
}
8993

src/runtime/modules/sre/Pattern.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include "runtime/PyObject.hpp"
99
#include "runtime/PyTuple.hpp"
1010
#include "runtime/TypeError.hpp"
11+
#include "runtime/ValueError.hpp"
1112
#include "runtime/types/api.hpp"
1213
#include "runtime/types/builtin.hpp"
1314
#include "utilities.hpp"
1415
#include "vm/VM.hpp"
1516
#include <cstdint>
17+
#include <limits>
1618

1719
using namespace py;
1820
using namespace py::sre;
@@ -49,7 +51,14 @@ PyResult<Pattern *> Pattern::create(PyObject *pattern,
4951
auto el_ = PyObject::from(el);
5052
if (el_.is_err()) { return Err(el_.unwrap_err()); }
5153
if (!el_.unwrap()->type()->issubclass(types::integer())) { TODO(); }
52-
code_vec.push_back(static_cast<const PyInteger &>(*el_.unwrap()).as_size_t());
54+
auto value = static_cast<const PyInteger &>(*el_.unwrap()).as_size_t();
55+
if (!fits_in<uint32_t>(value)) {
56+
return Err(value_error("code value {} does not fit in [{}, {}]",
57+
value,
58+
std::numeric_limits<uint32_t>::min(),
59+
std::numeric_limits<uint32_t>::max()));
60+
}
61+
code_vec.push_back(static_cast<uint32_t>(value));
5362
}
5463

5564
std::optional<bool> isbytes;

src/runtime/modules/sre/module.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ PyResult<PyObject *> ascii_iscased(PyTuple *args, PyDict *kwargs)
9595

9696
auto ch = character->as_big_int().get_si();
9797
if (ch >= 128) { return Ok(py_false()); }
98-
return Ok(std::isalpha(ch) ? py_true() : py_false());
98+
return Ok(std::isalpha(static_cast<int>(ch)) ? py_true() : py_false());
9999
}
100100

101101
PyResult<PyObject *> ascii_tolower(PyTuple *args, PyDict *kwargs)
@@ -111,7 +111,7 @@ PyResult<PyObject *> ascii_tolower(PyTuple *args, PyDict *kwargs)
111111

112112
auto ch = character->as_big_int().get_si();
113113
if (ch >= 128) { return Ok(character); }
114-
return PyInteger::create(std::tolower(ch));
114+
return PyInteger::create(std::tolower(static_cast<int>(ch)));
115115
}
116116

117117

src/runtime/modules/time/module.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ PyModule *time_module()
5656

5757
module->add_symbol(PyString::create("monotonic").unwrap(),
5858
PyNativeFunction::create("monotonic", [](PyTuple *, PyDict *) {
59-
const double ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
59+
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
6060
std::chrono::steady_clock::now().time_since_epoch())
61-
.count();
62-
return PyFloat::create(ns / std::nano::den);
61+
.count();
62+
return PyFloat::create(static_cast<double>(ns) / std::nano::den);
6363
}).unwrap());
6464

6565
return module;

0 commit comments

Comments
 (0)