C++ compiler /analyze switch question
Moving question back here. This specific to the C++ compiler.
On this test, I expected it to report a use of an uninitialized variable. Is this correct expectation of the /analyze switch? I'm using VS 2005 B2.
void
foo(int& x )
{
printf("%d", x );
}
int
main()
{
int uninitialized;
foo( uninitialized );
return 0;
}
Hi Brian,
/analyze and the static analysis is not owned by the compiler team. The switch just tells the cl.exe driver to pass the source to c1xxast.dll which is owned by another team.
Any ways, I am not sure why no warnings are issued. If I change the code to be:
#include <stdio.h>
void foo( int x )
{
printf( "%d", x );
}
int main()
{
int uninitialized;
foo( uninitialized );
return 0;
}
I get the following warnings:
warning C6001: Using uninitialized memory 'uninitialized'.:defect path: 10, 11 issued by c1xxast.dll
I am not sure if your posted code should or should not issue a warning. If you believe it should then it could be a bug. I haven't looked deep enough though.
Note: even the compiler back-end (c2.dll) is not wanring on the sample you provided.
BTW, you can use the compiler switch /Bt to see that the code is passed to c1xxast.dll during compilation.
Thanks,
Ayman Shoukry
VC++ Team
Thanks, Ayman. If I pass x by-value, I also get the prefest diagnostic within foo. It does seem like a bug: a by-ref argument does not imply def before use. I'll open a bug (for your next release, obviously).
Hi Brian,
This is not a bug. Code analysis is done per function, so in main there is no info how x will be used in foo. Assumptions are done in favor of noise reduction, so assumption here is that foo will initialize x.
To get more warnings functions can be annotated (see
http://msdn2.microsoft.com/en-us/library/ms182032):
#include
<stdio.h>
#include <CodeAnalysis\SourceAnnotations.h>
using namespace vc_attributes;
void foo( [Pre (Deref = 1, Valid = SA_Yes)] int& x )
{
printf( "%d", x );
}
int main()
{
int uninitialized;
foo( uninitialized );
return 0;
}