Search code examples
c++c++17clang-tidy

Using clang-tidy to check c++17 code


I installed clang-tidy on Ubuntu using:

sudo apt install clang-tidy

I ran it on a simple C++ 17 file, and got a warning and errors:

/home/erelsgl/Dropbox/ariel/CPLUSPLUS/intro/01-single-file/ptr.cpp:17:3: warning: 'auto' type specifier is a C++11 extension [clang-diagnostic-c++11-extensions]
                auto i = make_unique<int>();
                ^
/home/erelsgl/Dropbox/ariel/CPLUSPLUS/intro/01-single-file/ptr.cpp:17:12: error: use of undeclared identifier 'make_unique' [clang-diagnostic-error]
                auto i = make_unique<int>();

How can I tell clang-tidy to check this file according to c++17 standards?

NOTE: To build the program, I run:

clang++-5.0 --std=c++17 ptr.cpp

Solution

  • Depending on your compiler / clang-tidy version, the default C++ standard version used to compile source files may vary. clang's default std version is gnu++-98 (or gnu++-14 starting with clang 6.0), and typically clang-tidy has the same defaults as clang.

    I'm guessing that -std=c++17 (or -std=c++1z) isn't specified in the C++ compiler flags, used for compiling ptr.cpp, so clang-tidy falls back to the default -std=gnu++98, and therefore gives warnings for C++11 code.

    For asking clang-tidy to handle C++17, you should specify the -std flag as suggested by @n.m., as parameter to the -extra-arg option, for example:

    clang-tidy -p . ptr.cpp -extra-arg=-std=c++17

    Edit:

    Since clang++-5.0 is used for compiling ptr.cpp, it may be a good idea to use the matching clang-tidy version, 5.0 (on Ubuntu 16.04, the default clang-tidy version installed through apt is 3.8), that is:

    clang-tidy-5.0 -p . ptr.cpp -extra-arg=-std=c++17

    If not already installed, you could grab it from:
    https://www.ubuntuupdates.org/package/xorg-edgers/xenial/main/base/clang-tidy-5.0