Applied -Wconversion and Fixed the Warnings#1147
Conversation
|
Thanks for fixing the warnings! |
symengine/fields.cpp
Outdated
| GaloisFieldDict f(*this); | ||
| unsigned n = this->degree(); | ||
| auto k = std::ceil(std::sqrt(n / 2)); | ||
| auto k = static_cast<unsigned>(std::sqrt(n / 2) + 1); |
symengine/fields.h
Outdated
| @@ -510,7 +510,7 @@ class GaloisFieldDict | |||
|
|
|||
| unsigned int size() const | |||
There was a problem hiding this comment.
This should be changed to size_t.
symengine/fields.h
Outdated
| if (dict_.empty()) | ||
| return 0; | ||
| return dict_.size() - 1; | ||
| return static_cast<unsigned>(dict_.size() - 1); |
symengine/polys/upolybase.h
Outdated
| @@ -307,7 +307,7 @@ class ODictWrapper | |||
|
|
|||
| unsigned int size() const | |||
benchmarks/lwbench.cpp
Outdated
| return std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count() | ||
| return static_cast<double>( | ||
| std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1) | ||
| .count()) |
There was a problem hiding this comment.
Is this change really necessary?
There was a problem hiding this comment.
@isuruf -Wconversion catches the implicit type conversion happening here (since the above expression returns an unsigned int). Hence the change.
|
Can you post the 5 warnings? |
Edit: @isuruf @certik I think, I have finally found a fix to 4 of the above warnings (except the one raised in symengine_rcp.h). Please review and suggest any changes. |
certik
left a comment
There was a problem hiding this comment.
I think in general, using static_cast to get rid of the warning is the wrong fix. The warning says that the conversion might alter its value. Well, so would static_cast, wouldn't it? I would even argue that using such a fix makes things worse --- before we at least got a warning, now it will silently fail, if there is a problem.
I think the right fix is to keep returning unsigned integer, so that one doesn't have to convert at all. There might be some exceptions, but I left some comments where I think static_cast is not the way to go.
symengine/symengine_casts.h
Outdated
| inline To implicit_cast(const From &f) | ||
| { | ||
| return f; | ||
| return static_cast<To>(f); |
There was a problem hiding this comment.
I don't think that's the correct fix. I think this should be left as it was, to allow the compiler to check types.
| } | ||
| int get_size() const { | ||
| return this->stacktrace_buffer.size(); | ||
| return static_cast<unsigned>(this->stacktrace_buffer.size()); |
There was a problem hiding this comment.
Here I think the get_size should return size_t argument.
| const size_t stacktrace_size = 0; | ||
| #endif | ||
| return rcp(new StacktraceAddresses(stacktrace_array, stacktrace_size, | ||
| return rcp(new StacktraceAddresses(stacktrace_array, static_cast<unsigned>(stacktrace_size), |
There was a problem hiding this comment.
The argument of StacktraceAddresses should accept size_t instead.
symengine/sparse_matrix.cpp
Outdated
| const vec_basic &x) | ||
| { | ||
| unsigned nnz = x.size(); | ||
| unsigned nnz = static_cast<unsigned>(x.size()); |
There was a problem hiding this comment.
Use size_t instead of unsigned?
There was a problem hiding this comment.
This is actually okay since the row indices stored in CSRMatrix are unsigned.
|
@certik Indeed, |
|
See my changes |
|
It looks like primesieve has these conversion issues as well. |
|
@ShikharJ, ping |
This will avoid warnings in inlined functions in dependency headers
mpfr prec should fit in double
symengine/series_flint.cpp
Outdated
| while (i <= s.degree()) | ||
| if (not s.get_coeff(i++).is_zero()) | ||
| return i - 1; | ||
| if (not((s.get_coeff(i++)).is_zero())) |
There was a problem hiding this comment.
@isuruf This line is giving away this error:
error: conversion to ‘unsigned int’ from ‘long int’ may alter its value [-Werror=conversion]
even though is_zero() would return a bool. Any idea why is this cropping up?
There was a problem hiding this comment.
I didn't notice that it wasn't. I'll update this. BTW, can you also enable the auto-cancellation of Travis builds?
symengine/polys/basic_conversions.h
Outdated
| powr = div(one, genpow); | ||
| if (is_a<const Integer>(*powr)) { | ||
| int i = down_cast<const Integer &>(*powr).as_int(); | ||
| int i = static_cast<int>( |
There was a problem hiding this comment.
Don't use static_cast when you don't know whether it will fit or not
| } | ||
|
|
||
| unsigned int size() const | ||
| int size() const |
There was a problem hiding this comment.
@isuruf I don't think it can. Initially, it was giving an error such as this:
symengine/symengine/polys/upolybase.h:377:17: error: overriding ‘int SymEngine::UPolyBase<Container, Poly>::size() const [with Container = SymEngine::fmpq_poly_wrapper; Poly = SymEngine::URatPolyFlint]’
It didn't make much sense to change the parent function. Hence it was changed here. What should be the approach here?
There was a problem hiding this comment.
I don't understand the purpose of having two functions degree and size, but if it works, okay. cc @srajangarg
There was a problem hiding this comment.
size() :
if p == 0 :
return 0
else :
return degree() + 1
You yourself have made the change in the parent function it seems.
There was a problem hiding this comment.
@srajangarg @isuruf
Seems like that. The change was made in this commit in upolybase.h. I've tried reverting this back, but then we'll have to make even more changes (currently, we just have to account for two size() functions, one in uintpoly_flint.h and other in uintpoly_piranha.h).
There was a problem hiding this comment.
@srajangarg, yes I know, but what is the use of having the two functions?
There was a problem hiding this comment.
Flint has both the functions, so I thought it was standard to provide both.
|
@isuruf This is ready for a review. |
|
Looks good to me, @certik, can you review? |
|
Ping @certik. |
|
Ping @certik. |
|
The changes look good. Can we enable the |
|
Are benchmarks affected? |
| { | ||
| SYMENGINE_ASSERT(f <= std::numeric_limits<To>::max()); | ||
| SYMENGINE_ASSERT(f >= std::numeric_limits<To>::min()); | ||
| return static_cast<To>(f); |
There was a problem hiding this comment.
@certik, in Release mode numeric_cast should have the same cost as static_cast (which is zero)
| endif() | ||
|
|
||
| if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") | ||
| set_target_properties(symengine PROPERTIES COMPILE_FLAGS "-Wconversion -Wno-sign-conversion") |
There was a problem hiding this comment.
This warning is enabled by default and in travis-ci all warnings are converted to errors
|
@ShikharJ, thanks for the PR |
Applied -Wconversion and Fixed the Warnings
@certik @isuruf Pardon me for bothering you with such frugal commit messages, but due to the sheer number of the issued warnings, it was difficult to keep track of fixed files. -Wconversion has been removed in the latest commit, just to test the changes. When the work is close to completion, it will be re-applied.
Relevant: #228