Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of gcc's -Wshadow in visual C++

Tags:

-Wshadow will "Warn whenever a local variable shadows another local variable.". Is there an equivalent in Visual C++ (2008)? I tried /W4 but it didn't pick up on it. I also tried Cppcheck but that didn't see it either.

e.g. if I inadvertently do:

class A
{
        private:
                int memberVar;
        public:
                void fn()
                {
                        int memberVar = 27;
                }
};

I would really like to know about it!

like image 725
danio Avatar asked Jun 03 '11 09:06

danio


People also ask

Does Visual Studio have GCC?

Visual Studio's C++ Android development natively supports building your projects with the GCC that ships with the Android NDK, just like it does for Clang. You can also target Linux – either remotely or locally with the Windows Subsystem for Linux – with GCC. Compiler options for GCC.

Does Visual Studio Code have a compiler for C?

VS Code is first and foremost an editor, and relies on command-line tools to do much of the development workflow. The C/C++ extension does not include a C++ compiler or debugger. You will need to install these tools or use those already installed on your computer.

Does Visual Studio have a compiler?

Visual Studio C/C++ IDE and Compiler for Windows.

How do I know my GCC version in Visual Studio?

Type “gcc –version” in command prompt to check whether C compiler is installed in your machine. Type “g++ –version” in command prompt to check whether C++ compiler is installed in your machine.


2 Answers

Check out warnings C6244 and C6246

But you will need to enable automatic code analysis to get them, see How to: Enable and Disable Automatic Code Analysis for C/C++

If you cannot do it in your VS edition (Analyzing Managed Code Quality by Using Code Analysis), try to add the /analyze flag to the compilation command line. You will get some warnings that the '/analyze-' flag, which has been added by your IDE, is replaced with the manually added '/analyze' flag, but the analysis will work ;-)

like image 163
dwn Avatar answered Oct 18 '22 11:10

dwn


I am afraid no.

You could perhaps try compiling your code with Clang:

  • it has this warning (and a lot of others)
  • it has a compatibility mode for MSVC headers (and can build most of MFC)

We use gcc at work, to build our code, but compile with Clang regularly to test the code conformance to the Standard and benefit from its warnings.

like image 42
Matthieu M. Avatar answered Oct 18 '22 10:10

Matthieu M.