Skip to content

Applied -Wconversion and Fixed the Warnings#1147

Merged
isuruf merged 19 commits intosymengine:masterfrom
ShikharJ:Issue228
Apr 16, 2017
Merged

Applied -Wconversion and Fixed the Warnings#1147
isuruf merged 19 commits intosymengine:masterfrom
ShikharJ:Issue228

Conversation

@ShikharJ
Copy link
Copy Markdown
Member

@ShikharJ ShikharJ commented Dec 9, 2016

@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

@certik
Copy link
Copy Markdown
Contributor

certik commented Dec 9, 2016

Thanks for fixing the warnings!

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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you keep the std::ceil?

@@ -510,7 +510,7 @@ class GaloisFieldDict

unsigned int size() const
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to size_t.

if (dict_.empty())
return 0;
return dict_.size() - 1;
return static_cast<unsigned>(dict_.size() - 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@@ -307,7 +307,7 @@ class ODictWrapper

unsigned int size() const
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

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())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change really necessary?

Copy link
Copy Markdown
Member Author

@ShikharJ ShikharJ Dec 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isuruf -Wconversion catches the implicit type conversion happening here (since the above expression returns an unsigned int). Hence the change.

@ShikharJ
Copy link
Copy Markdown
Member Author

@isuruf @certik I have tried my best to fix the maximum number of issued warnings. Still, about 3 warnings are issued in Teuchos library, one in symengine_casts.h and another at symengine_rcp.h, (a total of 5), for which I am unable to find a working fix. -Wconversion has been re-applied though.

@isuruf
Copy link
Copy Markdown
Member

isuruf commented Dec 10, 2016

Can you post the 5 warnings?

@ShikharJ
Copy link
Copy Markdown
Member Author

ShikharJ commented Dec 10, 2016

@isuruf

/home/shikhar/symengine/symengine/utilities/teuchos/Teuchos_RCPNode.cpp:309:32: warning: conversion to ‘int’ from ‘std::multimap<const void*, {anonymous}::RCPNodeInfo>::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
   return rcp_node_list()->size();
                                ^
/home/shikhar/symengine/symengine/utilities/teuchos/Teuchos_TestForException.cpp:87:15: warning: conversion to ‘int’ from ‘std::__cxx11::basic_string<char>::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
   break_on_me = errorMsg.length(); // Use errMsg to avoid compiler warning.
               ^
/home/shikhar/symengine/symengine/utilities/teuchos/Teuchos_stacktrace.cpp:383:41: warning: conversion to ‘int’ from ‘std::vector<long long unsigned int>::size_type {aka long unsigned int}’ may alter its value [-Wconversion]
     return this->stacktrace_buffer.size();
                                         ^
/home/shikhar/symengine/symengine/symengine_rcp.h:380:19: warning: conversion to ‘unsigned int’ from ‘long int’ may alter its value [-Wconversion]
     RCP<T> p = rcp(new T(std::forward<Args>(args)...));
                   ^
/home/shikhar/symengine/symengine/symengine_casts.h:36:12: warning: conversion to ‘unsigned int’ from ‘double’ may alter its value [-Wfloat-conversion]
     return f;
            ^

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.
It also looks like more warnings are cropping up in Travis. I'll continue looking into them.

Copy link
Copy Markdown
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

inline To implicit_cast(const From &f)
{
return f;
return static_cast<To>(f);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument of StacktraceAddresses should accept size_t instead.

const vec_basic &x)
{
unsigned nnz = x.size();
unsigned nnz = static_cast<unsigned>(x.size());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use size_t instead of unsigned?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually okay since the row indices stored in CSRMatrix are unsigned.

@ShikharJ
Copy link
Copy Markdown
Member Author

@certik Indeed, static_cast is not the way to go. The point you mentioned (of silent failures) is one of the reasons why many C++ development authors explicitly suggest people to minimise casting. However, the reason I resorted to casting was firstly to signify places that needed improvements. Secondly, I had my doubts about whether a particular "up-gradation" (say changing the return type from int to long) or "down-gradation" (changing from size_t to unsigned) should be applied at a place or not. Instead of clarifying them one by one, I thought it was better to have them reviewed all at once, so that the changes can be applied where they are utmost necessary.

@isuruf
Copy link
Copy Markdown
Member

isuruf commented Dec 12, 2016

See my changes

@certik
Copy link
Copy Markdown
Contributor

certik commented Dec 12, 2016

It looks like primesieve has these conversion issues as well.

@isuruf
Copy link
Copy Markdown
Member

isuruf commented Mar 29, 2017

@ShikharJ, ping

while (i <= s.degree())
if (not s.get_coeff(i++).is_zero())
return i - 1;
if (not((s.get_coeff(i++)).is_zero()))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is .get_coeff expecting a long?

Copy link
Copy Markdown
Member Author

@ShikharJ ShikharJ Mar 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice that it wasn't. I'll update this. BTW, can you also enable the auto-cancellation of Travis builds?

powr = div(one, genpow);
if (is_a<const Integer>(*powr)) {
int i = down_cast<const Integer &>(*powr).as_int();
int i = static_cast<int>(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use static_cast when you don't know whether it will fit or not

}

unsigned int size() const
int size() const
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be negative?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the purpose of having two functions degree and size, but if it works, okay. cc @srajangarg

Copy link
Copy Markdown
Contributor

@srajangarg srajangarg Mar 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size() :
    if p == 0 :
        return 0
    else :
        return degree() + 1

You yourself have made the change in the parent function it seems.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srajangarg, yes I know, but what is the use of having the two functions?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flint has both the functions, so I thought it was standard to provide both.

@ShikharJ ShikharJ changed the title [WIP] Applied -Wconversion and Reduced the Warnings Applied -Wconversion and Fixed the Warnings Mar 30, 2017
@ShikharJ
Copy link
Copy Markdown
Member Author

@isuruf This is ready for a review.

@isuruf
Copy link
Copy Markdown
Member

isuruf commented Mar 30, 2017

Looks good to me, @certik, can you review?

@ShikharJ
Copy link
Copy Markdown
Member Author

ShikharJ commented Apr 8, 2017

Ping @certik.

@ShikharJ
Copy link
Copy Markdown
Member Author

Ping @certik.

@certik
Copy link
Copy Markdown
Contributor

certik commented Apr 14, 2017

The changes look good. Can we enable the -Wconversion on Travis and make it an error?

@certik
Copy link
Copy Markdown
Contributor

certik commented Apr 14, 2017

Are benchmarks affected?

{
SYMENGINE_ASSERT(f <= std::numeric_limits<To>::max());
SYMENGINE_ASSERT(f >= std::numeric_limits<To>::min());
return static_cast<To>(f);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning is enabled by default and in travis-ci all warnings are converted to errors

@isuruf isuruf merged commit e0bf1f7 into symengine:master Apr 16, 2017
@isuruf
Copy link
Copy Markdown
Member

isuruf commented Apr 16, 2017

@ShikharJ, thanks for the PR

@ShikharJ ShikharJ deleted the Issue228 branch April 17, 2017 06:20
isuruf added a commit to isuruf/symengine that referenced this pull request Aug 4, 2018
Applied -Wconversion and Fixed the Warnings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants