In a previous post we talked about the clang-modernize tool to detect where you can use some new C++11 features to modernize your C++ source code. But how can we easily detect where the new C++ features are used in a project?
Facebook and Google use C++11 extensively in their source code. Folly from Facebook as we discovered in a previous post use almost all of the C++11 features and I was curious to know if Microsoft also use the new C++11 standards in their open sourced code.
The Microsoft open source projects can be found in their GitHub repository. Some of them are only forks from other projects that they have adapted to their needs like LLVM, Clang and OpenCV and others are developed by them. Let’s take a look in one of their projects and discover if the C++11 is utilized. The best choice could be their latest open sourced C/C++ project:WinObjc.
Windows Bridge for iOS (also referred to as WinObjC) is a Microsoft open source project that provides an Objective-C development environment for Visual Studio/Windows. In addition, WinObjC provides support for iOS API compatibility.
A first method we can employ solution to check where C++11 was used is to explore each source file which could take hours or maybe days depending on the size of the project. Another solution is to use Clang.
Clang and its powerful diagnostic engine to the rescue.
The clang team aims to provide error messages that are as clear and expressive as possible. They try to make it as user-friendly as they can for a command line compiler. To do this, clang pinpoints exactly what is wrong in the code. This is done through a diagnostics engine which processes the error information into a user-friendly message.
Clang emits many kind of diagnostics and for our needs the more interested ones have a description like this: “XXX incompatible with C++98″
But how can we get the Clang Diagnostics for a Visual Studio solution?
The first alternative is to compile your projects using Clang, however it’s not an easy task. Indeed, you have to modify your build process to use Clang instead of the microsoft compiler.
The second alternative is to analyze your visual studio projects using CppDepend which uses Clang as front end parser, and report all its diagnostics. CppDepend is free for the open source community.
And here’s the result of all clang diagnostics for the WinObjc solution
But in our case we need only the diagnostics of kind “incompatible with C++98″. You can easily filter the previous request to get only these diagnostics:
As we can observe the nullptr and auto features are used in many places in the source code, but unlike Facebook and Google projects the C++11 features are not widely used. Also, when exploring the C++ source files of WinObjC, we can remark that they use the C++/CX extensions more.
Summary:
Sometimes it’s interesting to know if a c++ project uses the new standards and where they are used. Clang is a powerful tool for gathering this info.