Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 239
Erel Segal-Halevi Avatar asked Jan 23 '18 14:01

Erel Segal-Halevi


People also ask

Does clang tidy work with C?

Clang-tidy is a standalone linter tool for checking C and C++ source code files. It provides an additional set of compiler warnings—called checks—that go above and beyond what is typically included in a C or C++ compiler.

How do I run clang tidy on the whole project?

Run clang-tidy on you entire codebaseIn the build folder, run run-clang-tidy . It might be a different command ( run-clang-tidy.py or run-clang-tidy-VERSIONNUMBER ) depending on your distro's packaging preference.


1 Answers

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

like image 80
valiano Avatar answered Sep 16 '22 17:09

valiano