12

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
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183

1 Answers1

22

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

valiano
  • 16,433
  • 7
  • 64
  • 79
  • I tried 'clang-tidy -p . ptr.cpp -extra-arg="--std=c++17"' and tried 'clang-tidy -p . ptr.cpp -extra-arg="--std=c++1z"' and tried 'clang-tidy -p . ptr.cpp -extra-arg=c++17' and tried 'clang-tidy -p . ptr.cpp -extra-arg=c++1z', but I still get a lot of errors. For example, I get: "/usr/lib/gcc/x86_64-linux-gnu/7.1.0/../../../../include/c++/7.1.0/type_traits:177:5: error: 'inline' can only appear on functions [clang-diagnostic-error]". – Erel Segal-Halevi Jan 24 '18 at 03:05
  • Note: to build the program, I run "clang++-5.0 --std=c++17 ptr.cpp". It runs without errors. Maybe clang-tidy does not find clang++-5.0? – Erel Segal-Halevi Jan 24 '18 at 03:07
  • @ErelSegal-Halevi If you're using clang++-5.0, it's probably good to use clang-tidy-5.0 - I would suggest to run `clang-tidy-5.0` explicitly (the default clang-tidy for Ubuntu 16.04 is `3.8`, so it may be the one that's picked). Generally clang and clang-tidy are independent of each other (although they share some libraries). – valiano Jan 24 '18 at 07:21
  • @ErelSegal-Halevi You could pick clang-tidy-5.0 from here: https://www.ubuntuupdates.org/package/xorg-edgers/xenial/main/base/clang-tidy-5.0 – valiano Jan 24 '18 at 07:22
  • 1
    This solved the problem! I use: '''clang-tidy-5.0 -extra-arg="--std=c++17" ptr.cpp''' – Erel Segal-Halevi Jan 26 '18 at 12:24
  • I think the extra-arg should be "--std=c++17" and not "c++1z" – Erel Segal-Halevi Jan 27 '18 at 21:02
  • `--extra-arg="-std=c++17"` worked for me – david Aug 26 '22 at 12:56