Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
PVS-Studio for JetBrains CLion: ad astr…

PVS-Studio for JetBrains CLion: ad astra per aspera

Aug 06 2021

The PVS-Studio analyzer already has plugins for such IDEs from JetBrains as Rider, IntelliJ IDEA and Android Studio. Somehow we missed another IDE - CLion. The time has come to make amends! But why would you need PVS-Studio if CLion already has the code analyzer? What problems appeared during development? Keep reading to find answers to these questions.

0849_CLionPluginNew/image1.png

There will be no tech-hardcore in this article. This is more of a story about moments that we stumbled upon during the development process. Chill out and enjoy your reading. ;)

Note. By the way, the JetBrains website provides the results of various surveys for 2021. The figures cover most common language standards, IDEs, tools, etc. I highly recommend to read, it's worthy. CLion is one of the three most used IDEs / editors. What IDEs for C / C++ do you use?

Why would I need PVS-Studio if CLion already has a static analyzer?

Glad you asked! Let me answer you with a small story.

JetBrains maintains a separate Twitter account for CLion where they share various news, tell about new IDE features, etc. One of the tweets was an example of how the global DFA works.

0849_CLionPluginNew/image2.png

The example demonstrates how the static analyzer managed to detect the dereference of buffer null pointer obtained from the foo function. Andrey, our DevRel, decided to modify the example and see how CLion would handle it.

0849_CLionPluginNew/image3.png

Result: the same warning from CLion. PVS-Studio, however, issues two warnings:

  • V522 There might be dereferencing of a potential null pointer 'buffer'.
  • V611 The memory was allocated using 'new' operator but was released using the 'free' function. Consider inspecting operation logics behind the 'buffer' variable.

Here's an answer to the question that we are discussing - PVS-Studio and CLion collaboration will let us detect more errors at the writing code stage. CLion highlights the errors on the fly, but at the same time is limited in the analysis capabilities. PVS-Studio doesn't highlight errors immediately, but it can perform deeper analysis. Note that PVS-Studio has an incremental analysis – the mode that checks only the changed files.

As a result, the CLion analysis and PVS-Studio analysis complete each other. Imagine what would happen if we also added the compiler's warnings... :)

Second birth

Truth be told, we had a plugin prototype for CLion... several years ago. Yes, some time ago we started developing it. But it was not destined to finish it for a number of reasons. The prototype went on the shelf.

Now, since the users became more and more interested, we decided to finish what we've started. More precisely, we had a couple of possible ways. Either we modify the existing prototype, or start all over again.

Spoiler: both were a bumpy ride.

0849_CLionPluginNew/image4.png

After we found the sources in the repository, we decided to check the level of its readiness. What if there's not much to finish? Surprisingly, the code compiled immediately and that cheered us up.

Note. Hello from the C# department! About a year ago we ported the C# analyzer on Linux and macOS. And what's interesting, we managed to run the PVS-Studio on Linux on the very first day of working on the task! Yet the PVS-Studio analyzer for C# for Linux/macOS release was shipped only six months later. Talking about how many nuances get in the way...

Good news: code compiled and the prototype already had some features. Bad news: we still needed to integrate it in IDE. At the same time, the plugin-to-be must have the main features from PVS-Studio plugins for IDEs from JetBrains: Rider, IntelliJ IDEA, Android Studio. It is clear that there will be some specifics. But in general, the user who tries PVS-Studio together with various IDEs, should follow the same scenario for working with the analyzer, interact with the same settings and UI.

As a result, we tried to take the plugin for Rider as a ready-made solution. However, we found out that we couldn't immediately reuse those developments for CLion. Too much specifics.

Here comes a natural question: why don't you start everything from scratch? Just take a ready-made 'template' and add the existing functional from the prototype on it! Well, it's worth a shot.

We have found a template from the JetBrains official website. But after we downloaded it, it turned out that the sample... wasn't compiling.

0849_CLionPluginNew/image5.png

After a short investigation, we found out that it was a sample for older CLion versions. The API has changed in the new ones - hence the problems.

Interestingly, there is an instruction in the JetBrains blog that explains how to fix the sample to make it work. The edits helped launch the sample. However, the approach itself raised a question - why not just make a separate sample?

As a result, after we combined the prototype with the template, we found out that... nothing works. One might want to give up the prototype and write everything from scratch. It seems that to do this we only need a description of API and IDE interaction. But not so fast.

The results of API documentation search looked like this:

0849_CLionPluginNew/image7.png

Jokes aside, we didn't find anything on the JetBrains website. At all. A dubious idea - to search through all the available classes in hope to find the right one. Our developers who made the PVS-Studio plugin for Rider, confirmed our concerns - there is no documentation.

But if you know where to find such docs - please, leave a comment. It will be useful to us and it will be useful to others.

Perhaps, it's time to stop rushing to start everything from 0 - we need to complete what we have. It may be painful, but it's easier than to start the development again. The decision was correct - after some time spent on debugging and editing, it turned out that the prototype, in general, was already doing what was needed. As a result, we quickly managed to teach it to get a list of source code files from the project.

We gradually migrated the code, the plugin acquired functionality. Eventually it started operating as we needed. But this applied only to Windows, since the main development was carried out on this OS. Initially we were writing code for cross-platform compatibility, but after testing under Linux and macOS we had to add some improvements. In some cases, we had to do more than just editing plugin code. We also had to delve into the core of the C++ analyzer. Indeed, it was a multilingual task - knowledge of both Java and C++ came in handy.

There were two major improvements:

  • the C++ analyzer now takes into account environment variables when analyzing via JSON Compilation DB;
  • also, the analyzer learnt to give out the detected problems one by one and not altogether after it analyzed everything. This allowed us to add more interactivity—now you don't need to wait for the end of the analysis to start analyzing errors. We can do it during the analysis.

How to try it?

There wouldn't be an article if we didn't have the result. No intrigue here, alas. :)

The PVS-Studio plugin for CLion is available for use.

0849_CLionPluginNew/image8.png

To try it, you need:

  • request a trial license;
  • install the analyzer itself and the plugin.

You can request a license and download the analyzer here. Follow the link to get an extended trial period for 30 days. ;)

The plugin installation is also simple. You can find the details in the relevant documentations section: how to download, enter the license and what to do next.

Try it, use it, write us if something doesn't work. We also welcome all suggestions on improving the integration.

And we can't help but ask a question: which IDE / editor do you want PVS-Studio to integrate into? Perhaps, Visual Studio Code? ;)



Comments (0)

Next comments next comments
close comment form
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam