To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

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

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

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

Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
How we fixed one bug in CMake

How we fixed one bug in CMake

Mar 23 2020
Author:

In August 2019, CMake introduced the long-awaited support for precompiled headers. Before that, one had to use different plugins, for example, Cotire. Right after the release of CMake with new functionality, there were several more improvements. But in the fall, we decided that we could already start using this feature, and rewrote our scripts. Later, we found a bug that generated incorrect parameters of the Clang compiler and prevented the launch of the PVS-Studio analyzer. The bug had to be fixed by ourselves.

0719_CMakePchFix/image1.png

What was wrong?

The problem was that the source file, which used the precompiled header, was not correctly preprocessed if the Clang compiler was used. There was empty space instead of the header file. But in GCC and MSVC, everything worked fine.

What does CMake do?

It is worth talking about how CMake forms a precompiled header. For each target (starting from CMake 3.17 and for each type of the build - Debug, Release, ...), 2 files are created: cmake_pch.hxx and cmake_pch.cxx. An hxx file is a header file from which a precompiled header will be generated. cxx - is an empty file with the following contents:

/* Generated by CMake */

It is needed to generate a precompiled header - it is used as a source when the compiler starts.

During the project build the header file is included by passing the compiler flags. For Clang, these are:

-Xclang -include-pch -Xclang <PCH_FILE>

In other words, the already created precompiled header is passed in the compilation command. As a result, launch commands are saved in the file compile_commands.json, used by the analyzer for checking the project.

Problem

However, at this point, the name of the original header file gets lost. When the preprocessor starts, Clang tries to get the name of the header file from the generated precompiled header and eventually uses the empty cmake_pch.cxx file. Why? If you look into the Clang documentation, you can see their way of creating precompiled headers:

To generate PCH files using clang -cc1, use the option -emit-pch:
$ clang -cc1 test.h -emit-pch -o test.h.pch

The test.h header file is used as the source file. Whereas CMake operates in the following way:

-Xclang -emit-pch -Xclang -include -Xclang <PCH_HEADER>

It includes it through a special flag, and then passes cmake_pch.cxx as the source. Information about included files passed through the command line, unfortunately, gets lost, but the information about the source file remains. You can find this out by looking into the Clang documentation:

Original file name
    The full path of the header that was used to generate the AST file.

But how did Cotire work in this case?

Cotire is similar in structure to CMake, but the cxx file looked something like this:

#ifdef __cplusplus
#include "/path/to/header.hxx"
#endif

In the source, it had an include directive, so the preprocessor worked fine. This is the first thing we wanted to offer as a fix.

However, with this approach, problems appeared - the encoding of the files was different (the compiler can convert the source file to its own encoding). Also the work of other compilers could break, and in CMake 3.17 the cmake_pch.cxx file was created only for each type of a build, while the hxx files could differ.

Solution

As a result, the decision was to pass the original source file to the compilation line as well:

-Xclang -include-pch -Xclang <PCH_FILE> -Xclang -include -Xclang <PCH_HEADER>

Thanks to the -Xclang option, the next flag is passed directly to the preprocessor, bypassing the driver, which searches for the corresponding .pch/.gch file header. Link to the patch: PCH: Clang: Update PCH usage flags to include original header.

Conclusion

Our small patch has become a part of CMake 3.17, fixing the problem. I hope it was interesting, and you learned a little more about the work of CMake.

We can recommend using this feature in real projects.

Popular related articles
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept