DEV Community

Tomoyuki Aota
Tomoyuki Aota

Posted on • Updated on

How to start using C++ Core Guidelines Checker and GSL

(A Japanese translation is available here.)

Note

In this article, I described how to start using C++ Core Guidelines Checker. However, C++ Core Guidelines Checker has some immature points, and clang-tidy seems a better tool. Therefore, please consider using clang-tidy. (@tenmyo pointed out that clang-tidy has checks based on C++ Core Guidelines.)

I'm leaving the information about C++ Core Guidelines Checker just for reference.

Introduction

What is C++ Core Guidelines Checker?

C++ Core Guidelines is created mainly by
Bjarne Stroustrup and Herb Sutter for writing better C++ code. Most of the items in the guidelines are designed to be supported by an analysis tool.

C++ Core Guidelines Checker is a static analysis tool based on C++ Core Guidelines. Microsoft is developing this tool, and Visual Studio 2017 supports it by default. This tool emits warnings for violating code in Error List window when VS builds the code. Visual Studio C++ Core Guidelines Checker Reference | Microsoft Docs describes what warnings to be emitted for what kind of violation.

As of May 2018, C++ Core Guidelines Checker works only on Visual Studio on Windows. Therefore, the content of this article applies only to such environment.

What is GSL?

GSL (Guidelines Support Library) is a library which contains types and functions that are suggested by C++ Core Guidelines. The following article covers what types and functions are available.

There are several implementations of GSL. In this article, I will use Microsoft/GSL.

GSL supports multiple platforms/compilers. I hope C++ Core Guidelines Checker will have multi-platform support as well.

How to begin using them?

Enable C++ Core Guidelines Checker

C++ Core Guidelines Checker has to be enabled in each VS project. The following pages cover the procedure to enable it. Also, several useful things (like disabling specific warnings) are described.

Install GSL code

Copy and paste gsl in your codebase. A package manager like Conan is preferable, but I could not find a well-maintenanced one. Hmm...

A wrapper of GSL

Currently, C++ Core Guidelines Checker emits warnings to third-party libraries, and there are no settings to avoid it. (Microsoft recognizes this problem and working on it.) To be specific, it emits warnings to GSL code when we use GSL. This behavior results in the flood of warnings to the code which are not mine. In order to avoid this, we need a wrapper of GSL, and we use GSL through the wrapper.

The code of the wrapper of GSL is shown below. Here, some ifdefs of _MSC_VER are because 1. CppCoreCheck/warnings.h are created during VS 2017 installation, and 2. #pragma directive for suppressing warnings are specific to MSVC.

#ifdef _MSC_VER
#if _MSC_VER >= 1910
#include <CppCoreCheck/warnings.h>
#pragma warning(disable: ALL_CPPCORECHECK_WARNINGS)
#endif // _MSC_VER >= 1910
#endif // _MSC_VER


#include <gsl/gsl>


#ifdef _MSC_VER
#if _MSC_VER >= 1910
#pragma warning(default: ALL_CPPCORECHECK_WARNINGS)
#endif // _MSC_VER >= 1910
#endif // _MSC_VER
Enter fullscreen mode Exit fullscreen mode

Code example

I created a code example.
https://github.com/TomoyukiAota/CoreCheckerAndGsl
You should get something like this after the steps described above.

Afterword

In this article, I described how to introduce C++ Core Guidelines Checker and GSL. I recognize that there is some awkwardness. C++ Core Guidelines Checker works only on VS on Windows. GSL is installed by copy and paste and a wrapper for suppressing warnings are required. However, I believe the warnings from C++ Core Guidelines Checker allows me to write better C++ code and GSL provides better types/functions, so I hope such awkwardness is cleared someday.

Top comments (0)