How does SonarQube handle the preprocessor includes in C?

Hi all,

Sorry to ask another question please.

I am curios how does the SonarQube/Sonar Scanner handle the preprocessor include instructions in C language?

Does the scanner run on the final source file, AFTER the include file, or on each source + header file?

My concern is the following:

  • if I want to exclude a header file, do I risk that also the files where that particular header file is included, to be automatically excluded from the scan?

Thank you kindly.

Hello @AlexandruAn,

  • The analysis runs on the entire translation unit (all the included header files are part of the translation unit)
  • When a header file is exluded the issues on it will no be shown. That will not affect the results of the other files.
  • When a source file is excluded, the headers that are only included in that file will no be analyzed. That is due to the fact that we don’t analyze headers by themselves.

Let me know if you have further questions!

Hi @Abbas,

Thank you very much for your quick reply.

So please just allow me to check if I understood correctly the second bullet point you mentioned (the rest are clear):

Suppose I have the following scenario:

In source_file_1.c:

#include “header_file_1.h”
#include “header_file_2.h”
#include “header_file_3.h”
[…]

Consider that all the header files are ONLY included in the source_file_1.c.

If, in SonarQube, we exclude the header_file_1.h from analysis, would that mean the following parts (of the translation unit) would be scanned?

  • header_file_2.h
  • header_file_3.h
  • the rest of the translation unit, as determined by the source_file_1.c

Can you please confirm if I understood this correctly?

Thank you again.

@AlexandruAn, you understood it correctly :+1:

Thank you very much for your help and patience @Abbas . It is clear now. :slight_smile: