>> Hey, everyone. My name is Julia,
and today I'm going to show you how to use
clang-tidy in Visual Studio code.
Clang-tidy is a C++ lender tool.
It provides an extensible framework for diagnosing and fixing
typical programming errors, things like style violations,
interface misuse, or bugs that can be deduced via static analysis.
There are a bunch of different C++ lenders out there,
and one reason that people use clang-tidy
is because it can be run on any platform,
Windows, Mac or Linux.
While both Visual Studio and VS Code have clang-tidy integration,
clang-tidy can also be run from the command line.
This is great because this means that if you and
your coworkers are using different editors or IDEs,
you can all still integrate
clang-tidy into your C++ development workflow,
achieving consistent lending across each of your environments.
Let me show you how to use clang-tidy in VS Code.
In case some of you watching aren't
familiar with how clang-tidy works,
I'll start by using the command line syntax in the terminal.
To run clang-tidy on your file,
use the clang-tidy command followed by
the name of the source file that you want to run clang-tidy on.
Then the dash checks flag to specify
which clang-tidy lender checks you want to run on this file.
I'm going to use
the cppcoreguidelines checks and I'm going to add dash star,
which indicates that I want to run all of
the cppcoreguidelines clang-tidy checks on this file,
and then I'll hit "Enter".
I see that I have a warning from clang-tidy and it's
telling me that 100 is a magic number,
consider replacing it with a named constant.
Then it tells me which
cppcoreguidelines check this warning is coming from.
It's the avoid-magic-numbers check.
In my code,
I have 100,
you will see on line 20,
I'm setting the count equal to 100.
Now, let's say I do want to check for magic numbers in my code.
Like, I don't want to disable that check I want to keep it.
But I want to allow
the number 100 because I know that I want to use
it and I don't want to deal with
seeing warnings every single time I use it in my program.
You can customize the clang-tidy checks that you run by
creating a dot clang-tidy configuration file in your workspace.
Let's go ahead and do that.
It's just called .clang-tidy and then here you can
specify which checks you want to run along
with check options to customize these checks.
A .clang-tidy configuration file is really
helpful if you want to run multiple checks,
but you don't want to have to type out all of them on
the command line each time you
want to run the lender on your code,
or if you want to customize your checks,
which is what we're doing by setting
the magic number 100 as an allowed number in our program.
If I save this file and then let's run clang-tidy again.
But this time I'm not going to use that checks flag,
clang-tidy will detect the .clang-tidy
configuration file in my workspace
and it will automatically use that.
Let's see if we still get that warning for the number 100.
Now I have a new warning,
which is that one is also a magic number,
but it's not flagging 100.
It's letting that go through.
That means that clang-tidy
is in fact reading from our configuration file and
allowing the number 100 to not be flagged as a warning.
Now, I'm going to show you how to run
clang-tidy in Visual Studio code,
not through the command line syntax.
There are two ways to run code analysis in VS Code.
The first is to manually invoke
code analysis by opening the command palette,
which you can do by pressing control shift P,
and then select "Run Code Analysis on
Active File" and this will run clang-tidy.
VS Code detected that .clang-tidy
configuration file in my workspace,
so it knows which checks and check options
to use when running clang-tidy on my active file,
which is why I see a squiggle under the number
one and I get the same error message that I saw on the terminal.
That one is a magic number,
consider replacing it with a named constant.
I can also see which check is causing this warning,
and it's a hyperlink.
If you click on the name of that check,
you'll be taken to the official documentation to learn more
about what that check does and how you can customize it.
I'm not seeing squiggles for the number
100 because VS Code is reading
from our check options in the .clang-tidy configuration file.
Let's say you didn't have
a clang-tidy configuration file in your workspace.
You can use VS Codes UI to enable and disable clang-tidy checks.
If we open our VS Code settings for your workspace,
and then type code analysis,
and then it's like C++.
This is where you'll see all of
the settings related to clang-tidy.
If we scroll down a bit,
we see these two settings one for
checks disabled and one for checks enabled.
As I mentioned, you don't need to have
a clang-tidy configuration file
in your workspace in order to use clang-tidy with VS Code.
You can just enable and disable checks in this UI.
But if you do have a clang-tidy configuration file,
any checks that are enabled or disabled in this settings
UI will be augmented to the clang-tidy configuration file.
Let's say I disable the magic numbers check in this UI,
cppcoreguidelines avoid magic numbers.
Another setting that I want to change is run automatically.
Previously, when I invoked
clang-tidy I opened the command palette and
manually run code analysis on active file.
If you enable the run automatically setting,
then code analysis will automatically be run
any time you open or save a file.
Let's test that out,
so now I'm going to save this file.
VS Code should automatically be running
clang-tidy on my file right now,
and this squiggle should not appear because I disabled that check.
Now, let's add some bad code to this program.
I'm going to add
a new variable X and I'm not going to initialize it.
Now, when I save my file,
code analysis should automatically run because
I enabled the run automatically code analysis
setting for the workspace and
I should see a squiggle under int X because
it's not initialized and I should not see
a squiggle under the number one,
because I disabled the magic numbers check.
There is that squiggle,
and if I hover over it,
I can see what the warning is.
You can also see the results of
clang-tidy checks in the problems pane.
Something I did before this demo that
you probably would want to do if
your project has a lot of headers that
you don't want to run code analysis on,
is I updated the code analysis
exclude setting in my settings.json file.
The code analysis exclude setting can be
found in your workspace settings UI and it basically
allows you to specify a directory or a file name or
a list of files that you don't want to run
clang-tidy on when you are running clang-tidy on your workspace.
If we look at my settings.json file,
right here I specified
the out directory as a directory that I don't want to include in
my clang-tidy code analysis and that's because I have a bunch of
headers in that directory that are third party library headers.
They're not things that I wrote,
so I don't want to run my lender checks on all of those files.
If I hover over this variable,
there's this item here that says Quick Fix.
Outside of VS Code,
clang-tidy allows you to automatically apply
suggested fixes from the checks that you ran on your code.
That is not currently supported in VS Code,
but it is something we are
actively working on and it's coming soon.
Right now if you click on "Quick Fix",
it'll give you the option to clear
the code analysis squiggles if you just don't want to see them.
Keep an eye out for upcoming releases where we'll allow you to
automatically apply suggested fixes
from clang-tidy checks in the editor.
Thanks for watching.
I hope you learned about how to use
clang-tidy both from the command line and in VS Code.
Please reach out on Twitter if you have any questions,
and I hope you enjoy the rest of the Pure Virtual C++ Conference.