How to run Code analysis on project files only - c++

I have recently upgraded from Visual Studio 2012 to 2017 and I'm experimenting with the Code Analysis features (which seem to be far more capable than they were in 2012).
The problem I have is that I'll get many results for code dependencies, whereas I am really only interested in my own local project files. This is particularly true when I have Enable C++ Core Check (Released) enabled.
For example, as shown above, I receive multitudes of analysis results for the boost libraries. There are tons of results for xerces-c, and other libraries which I'm using as well.
Is there a way to restrict the analysis to only those files which I have written myself (local to the project)?

There isn't a flag or setting that I'm aware of to directly accomplish this, but you can get something close by using #pragma warning to change the warning level (or disable specific warnings) before you include those library headers, then restore the warning level before including your own.
It isn't perfect, and could result in suppressing warnings you'd want to see, but if you only disable them during Code Analysis that wouldn't be an issue.

Related

Add /Wall when running code analysis only?

During my regular builds, I have /W4 turned on, with a few additional warnings as well, and some that are simply disabled due to too many false positives with Visual Studio. This mostly works okay, but I'd still like, from time to time, to check on all warnings which /Wall would give.
My preference would be to do this during Code Analysis, which I run about once a week anyway. I already have a custom ruleset file for CA, but I can't figure out how to enable additional non-CA warnings in it. I've also tried looking in the .vcxproj files to see if there's anything in there which is specific to CA, but I couldn't find anything.
Additional info: I use CMake to create the projects, so it would be preferred if it's something I can do in there, but I can probably modify my CMake myself to work with potential solutions. Also, all projects are C++ (not CLI) except one which is C#.

Setting up files to compile on any computer in Visual Studio

Question:
Once my code is working how should I prepare my files so that a stranger on a different computer can compile it without difficulty?
Additional Details:
I am sending a code sample to a company as part of an application so obviously an elegant solution would be better (i.e. minimise number of files required etc) and no work should be necessary by the stranger at the other end.
Although I am only using one simple library, even so I need to set include directories, include lib files, images, dll files etc so that it all compiles correctly.
If it matters, I am using Visual Studio 2015 and the simple library is SDL.
Sorry if this is a duplicate, I was sure that this question would have been asked before but if it exists I just don't know the correct terminology to find it amongst the noise.
Apologies if this is overly simplistic, but you might want to bound the scope of your project by deciding which computers you want to support, and build your code yourself on those platforms, in advance, just to be sure.
List the supported platforms in your release notes, including any platform-specific instructions or information (which VC++ versions, which C++ versions, which OS versions, which DLLs, directory structure, etc.).
You may have to stick some "#ifdef"s and such in your code, but only by building on a particular platform/configuration will you really know for sure.
You can use properties/props files in your VS solution which sets the paths to includes and precompiled libs, then reference the build variables in your project files.
To compile on another machine, you just need to change the values in the properties files.

Is bscmake needed using Visual Studio 2010?

I'm wondering if bscmake is still needed for Visual Studio 2010 C++ Projects using unmamanged/managed code.
My understanding of bscmake is, that it is generating browse-files for each cpp-file in order to be able to use things in visual studio like the "class view" or "go to definition",...
But after asking a question about bscmake myself and reading other similar questions I was unsure what bscmake is for and why you should use it.
References:
How to get rid off “BSCMAKE error BK1500: Internal error” compile errors
LINK : fatal error LNK1000: Internal error during BuildLibrary
So my question is:
Is there any need to activate bscmake?
Advantages/Disadvantages activating bscmake?
bscmake seems no longer be supported for VS2010, see this bug reports:
Per-project Browsing in VS2010 and BSC files cannot be used.
So you can speed up your compile times by turning off the creation of SBR/BSC files,
as they are useless anyway.
BSC files had the big advantage that they could be used to browse external libraries which
were not part of your solution (MFC for example). This seems not be possible with
intellisense. I personally use VisualAssist as a replacement (which works quite well for me).
The intellisense does not need the browse information, it is used for object explorer which I have never used or needed to : http://msdn.microsoft.com/en-us/library/kbs280h1%28v=vs.80%29.aspx
The main disadvantages are longer builds, build errors due to being unable to build the bsc files.
The main advantages are you can view more information of your classes and objects and apparently they have added some feature called 'live browsing' since VS2005 http://msdn.microsoft.com/en-us/magazine/cc163658.aspx.
However I have to say that the intellisense has worked fine for me, plus I use VisualAssist and I have never needed the object browser and find that the additional compilation time and build errors is so frustrating for large solutions that I never have this switch on.

Can I set visual studio 2005 command line switches in my code (ie through pragma/define)?

I know that you can provide a command line or add switches in your project properties but can you do that from source file? I need to set certain switches for certain source files without going into project properties and manually doing it everytime. So maybe you can do it with preprocessors from C++ source file?
There're some #pragma's that can, for example, alter code generation/optimization for a block of code, but I think that's pretty limited thing.
First: Compilation/linking do not execute your C++ code. So you can't write a C++ piece'o code and make decision (to change some project settings of THIS project) based on some C++ conditions (like if/else/...).
#pragma and #defines are pre-processor directives as you already know. Though compiler understands #pragmas, programming capability is limited and usually compiler specific.
A common use of these is to make the code platform independent.
To answer your original question "how do I modify the project properties progrmatically (not manuall)?": You can write "code" using #pragmas & #defines (even conditional ones) in your code to control various settings. Usually support is compiler-vendor specific, so your code will most probably not be portable (across compilers) unless you're careful.
On my GCC, I always found c++config.h a good example of various cpp directive. On my windows PC it's installed in X:\MinGW\lib\gcc\mingw32\4.5.2\include\c++\mingw32\bits\c++config.h
To find out supported cpp directives, RTM of your compiler.
The other option obviously is to write shell/perl/... script to make modifications to the project settings/Makefiles. I recommend that compared to getting yourself tangled in cpp directives. :)
Of course this option would change the settings before compilation and not during. But if that's not a prob for you, go with this rather than cpp directives.
As has been mentioned, #pragma statements are very vendor specific. For Visual Studio 2005, take a look here for the supported #pragma statements: Pragma Directives
I would also with thekashyap regarding writing a script to modify the project settings for each file for the following reasons:
In the event that you have to add or change a setting, there is a *single* place to modify.
Because there is only one place to look, future maintainers know where to find special settings for a given file. They won't be surprised by something set manually that is hidden deep within the project settings dialogs.

Dead code identification (C++)

I have a large legacy C++ project compiled under Visual Studio 2008. I know there is a reasonably amount of 'dead' code that is not accessed anywhere -- methods that are not called, whole classes that are not used.
I'm looking for a tool that will identify this by static analysis.
This question: Dead code detection in legacy C/C++ project suggests using code coverage tools. This isn't an option as the test coverage just isn't high enough.
It also mentions a -Wunreachable-code. option to gcc. I'd like something similar for Visual Studio. We already use the linker's /OPT:REF option to remove redundant code, but this doesn't report the dead code at a useful level (when used with /VERBOSE there are over 100,000 lines, including a lot from libraries).
Are there any better options that work well with a Visual Studio project?
I know that Gimpel's Lint products (PC-Lint and Flexelint) will identify unreachable code and unused / unreferenced modules.
They both fall in the category of static analysis tools.
I have no affiliation w/ Gimpel, just a satisfied long-term customer.
You'll want something along the lines of QA-C++ (http://www.programmingresearch.com/QACPP_MAIN.html), also see http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis for similar products.
You're looking for a static code analysis tool that detects unreachable code; many coding guidelines (such as MISRA-C++, if I'm not mistaken) require that no unreachable code exists. An analysis tool geared specifically to enforce such a guideline would be your best bet.
And you'll like be able to find other uses for the tool as well.
I dont know Visual C, and had also recommended the -Wunreachable-code specific coverage tools. As solution for your situation I would try the following:
Make with ctags (or similar programm) a list of all your symbols in your source
Enable in your compiler the dead code elimination (I would assume it defaults to on)
Enable your whole-program/link time optimizations (so he knows that not used functions in your moduls are not required by other externals and get discarded)
Take the symbols from your binary and compare them with the symbols from 1.
Another approach could be some call graph generating tool (e.g. doxygen).
I suggest you use a couple approaches:
1. GCC has some useful compilation flags:
-Wunused-function
-Wunused-label
-Wunused-value
-Wunused-variable
-Wunused-parameter
-Wunused-but-set-parameter
2. Cppcheck has some useful features like:
--enable=unusedFunction
3. Use static analyzer as was suggest before.
One approach that works for me - with Delphi - is to enable debugging, and run your program under the debugger.
When a Delphi program is run under the debugger, the IDE shows in the margin which lines of code can be set as breakpoints. Code which is truly dead - i.e., has been stripped out by the linker/compiler is obvious as breakpoints can't be set there.
Some additional notes, as commenters seem to misunderstand this:
a: You don't need to try setting a breakpoint on each line. Just open up the source file in the IDE, and quickly scroll through it. Dead code is easily spotted.
b: This is NOT a 'code coverage' check. You don't need to run the application to see if it reaches the lines.
c: I'm not familiar enough VS2008 so can't say if this suggestion will work.
Either
1) MSVC's under-used in built static analysis tool.
2) The MSVC marketplace has lots of tools including support for most free tools, including CppCheck
You will need the latest version of Visual Studio for market place applications, but the free "Community Edition" has very lenient licencing.
Write a script that randomly deletes a function (from the source code) and recompiles everything from scratch. If it still compiles - that function was dead code.