Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc warning flags for implicit conversions

I recently had a bug in a similar context to next one:

double getSomeValue()
{
    return 4.0;
}
...
std::string str;
str = getSomeValue();

As you can see here is easy to spot the problem, but in a large code base where getSomeValue() is not in the same file with the calling code it might be difficult to spot this double to std::string silent conversion. GCC compiles this code fine with -Wall -Wextra -Werror (sample output here, I don't know what warning flags were used: http://ideone.com/BTXBFk).

How may I force GCC to emit warnings for these dangerous implicit conversions? I tried -Wconversion, but it is very strict and it causes errors in most included headers for common cases like unsigned - 1. Is there a weaker version of -Wconversion?

like image 689
Mircea Ispas Avatar asked Mar 07 '16 11:03

Mircea Ispas


People also ask

Which flag would you pass to your C++ compiler so it warns you about implicit conversions?

-Werror is a compiler flag that causes all compiler warnings to be treated as errors. Developers who enable -Werror are making a statement: we care about our code base, and we won't accept warnings here.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

How does GCC treat warning errors?

You can use the -Werror compiler flag to turn all or some warnings into errors. Show activity on this post. You can use -fdiagnostics-show-option to see the -W option that applies to a particular warning.

How do I enable warnings in GCC?

GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.

How do I eliminate the warning-wparentheses in GCC for IF statements?

When there is the potential for this confusion, GCC issues a warning when this flag is specified. To eliminate the warning, add explicit braces around the innermost if statement so there is no way the else can belong to the enclosing if. The resulting code looks like this: This warning is enabled by -Wparentheses .

How do I remove a GCC warning when a flag is specified?

When there is the potential for this confusion, GCC issues a warning when this flag is specified. To eliminate the warning, add explicit braces around the innermost if statement so there is no way the else can belong to the enclosing if. The resulting code looks like this:

Why does GCC issue warnings when I use a common variable?

To help detect accidental misuses of such arrays GCC issues warnings unless it can prove that the use is safe. See Common Variable Attributes . Warn for cases where adding an attribute may be beneficial.

How to suppress the warning that X is always initialized in GCC?

If the value of y is always 1, 2 or 3, then x is always initialized, but GCC doesn’t know this. To suppress the warning, you need to provide a default case with assert(0) or similar code. This option also warns when a non-volatile automatic variable might be changed by a call to longjmp. The compiler sees only the calls to setjmp.


1 Answers

You can use the -Wfloat-conversion flag, or the broader -Wconversion.

However, note that with C++11 uniform initialization brace syntax, you get a warning "out of the box", without the -Wconversion flag; e.g.:

#include <string>

double getSomeValue() {
    return 4.0;
}

int main() {   
    std::string str{ getSomeValue() }; // C++11 brace-init
}
C:\Temp\CppTests>g++ -std=c++11 test.cpp
test.cpp: In function 'int main()':
test.cpp:8:35: warning: narrowing conversion of 'getSomeValue()' from 'double' t
o 'char' inside { } [-Wnarrowing]
     std::string str{ getSomeValue() };
                                   ^
like image 137
Mr.C64 Avatar answered Sep 20 '22 02:09

Mr.C64