Not getting c4267 warning for few lines in code - c++

I had a task of removing all warnings in one of the project.
At one place in code I had following line.
dims[0] = children.size();
Where dims is 'int' array and children is a 'vector', so size_t is assigned to int. I am compiling this code on VS2015.
Ideally the above assignment should generate a warning
"C4267: '=': conversion from 'size_t' to 'int', possible loss of data"
But I don't get any such warning. So there are zero warnings in the code.
Now I convert warning 4267 into an error using '/we4267' and then start getting error for above lines.
I am confuse why compile didn't showed any warning in first place.
Thanking you all for your help.

I found that when /WX compiler option is added then some warnings, in my case C4267, are suppressed. Either adding compiler option /WX /Wall or removing them all together shows error/warning C4267. In my case I was getting error in other modules as there was no warning related compiler option add but the module where I didn't got C4267 warning, had compiler option /WX.
Its just that there is no documentation regarding this(or may be I was not able to find one).

Related

Clang RecursiveASTVisitor warning message?

When running my RecursiveASTVisitor program on Objective C codes, I keep getting this warning warning: unknown warning option '-Wno-receiver-is-weak'; did you mean '-Wno-receiver-expr'? [-Wunknown-warning-option]
But I didn't compile it with warnings enabled or have any warning options within my code.
Where does the error warning come from and how can I disable it?

How to disable warning C4927 in VS2013 Community

I'm receiving the following warning in my project:
warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while calling the constructor 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(const _Elem *)'
with
[
_Elem=char
]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xstring(778) : see declaration of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string'
I understand why it's happening, I'm just unable to suppress it. I've tried adding it to the Disable Specific Warnings list in my project settings, and I've also set my warning level to Turn Off All Warnings (/W0), yet the warning persists. Does anyone have any recommendations on how to hide the message?
You can control Visual Studio warnings directly in code using #pragma warning (https://msdn.microsoft.com/en-us/library/2c8f766e.aspx). If you'd like to silence the warning for a single line, you can put the following immediately before the line:
#pragma warning (suppress: 4927)
line-that-causes-warning-4927
You can also disable it from any point past the #pragma, by using 'disable' instead of 'suppress'. However, as the comments suggest, it's better to actually fix the warning, because it could be causing issues in your program.

What to do when I cant compile my project after making it x64 compatible in msvc2010?

What to do when I cant compile my project after making it x64 compatible in msvc2010?
I recently had a hard time adding a x64 platform to msvc2010 and now when I want to compile, I get these error messages:
1>src\CarClass.cpp(130): error C2220: warning treated as error - no 'object' file generated
1>src\CarClass.cpp(130): warning C4267: 'return' : conversion from 'size_t' to 'int', possible loss of data
P.S. I will look deeper into the code and try to google the best out of what I have right now.
The issue is that your warnings are treated as errors. If you want to change this, do this:
Project -> Properties -> C/C++ -> General
and make sure that Treat Warnings as Errors is set to No.
If you want to go further and address the warning, you should post the line of code that produces the warning and then further help can be given.

Is it possible to suppress invalid conversion from 'const char*' to 'char* errors with a compiler flag when porting over to newer RHEL versions?

I encounter the invalid conversion from 'const char*' to 'char* while compiling some code on a newer RHEL host.
The error I encounter comes from an external library that I'm including that ideally I'd not like to change and I'm aware that ideally the correct solution is to fix the error in the .h file in the external project which throws this error.
However, I was wondering if there's a compiler flag that I can pass in to ignore this error or convert it to a warning for a period of time until this error is fixed by the owner of the library?
Thank you
You could consider using -isystem /path/to/your/libs/headers to suppress all warnings from all include files in the given directory (unless you then use -Wsystem-headers to show them anyways).
And then you would use :
#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-fpermissive"
#include <yourlibrary>
#pragma GCC diagnostic pop
To turn that error into a warning, however this is a bit of a hack and may hide or introduce bugs.
The clean solution would be to actually fix the warning of course, but that doesn't seem to be possible.

Make VS compiler catch signed/unsigned assignments?

The Visual Studio compiler does not seem to warn on signed/unsigned assignments, only on comparisons. For example the code below will generate a warning on the if statement but not the initial assignments.
Is there anyway to make it catch these? I'm already at W4 but thought (hoped) there may be another setting somewhere.
Thanks,
int foo(void)
{
unsigned int fooUnsigned = 0xffffffff;
int fooSigned = fooUnsigned; // no warning
if (fooSigned < fooUnsigned) // warning
{
return 0;
}
return fooSigned;
}
Update:
Quamrana is right, this is controlled by warning 4365 which appears to be off by default, even at W4. However you can explicitly enable it for a given warning level like so;
#pragma warning (4 : 4365)
Which results in;
warning C4365: 'initializing' : conversion from 'unsigned int' to 'int', signed/unsigned mismatch
You need to enable warning 4365 to catch the assignment.
That might be tricky - you need to enable ALL warnings - use /Wall which enables lots of warnings, so you may have some trouble seeing the warning occur, but it does.
You can change the level of any specific warning by using /W[level][code]. So in this case /W34365 will make warning 4365 into a level 3 warning. If you do this a lot you might find it useful to put these options in a text file and use the #[file] option to simplify the command line.
#quamrana:
There must be something beyond the /Wall option to enable warning 4365:
C:\Temp>cl /Wall /c foo.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
foo.c
foo.c(6) : warning C4018: '<' : signed/unsigned mismatch
I see that Andrew got it to work, but does anyone have an idea why it's not working here?
The Visual Studio docs indicate that it should, but I can't even get the example program in the docs to give the C4365 warning (though it does give the related C4245 warning - but that occurs with just a /W4 option anyway).