I receive "can't be indexed twice" for headerfiles in a C++ project

Dear community,

I receive the “can’t be indexed twice” error for header files in my C++ project.
This creates a problem for me as in C++ it is valid to have header files that contain the whole implementation of a class/behavior mixed with header files that are part of a .hpp + .cpp pair.

In my particular case I have a folder hierarchy that contains only header files.
If I wish to analyze the .cpp files these folders have to be set as include folders … which is correct.
But in this case I do not seem to be able to also set these folders as source folders (which results in the error message that they can’t be indexed twice).

Is there some way for me to tell sonar, that a given file needs to be handled as something that can be include -ed (to analyze the files where it is included to), and also analyze it as it contains substantial amount of logic/code?
Also, is there some way to let sonar handle this for the whole hierarchy?

Version information:

  • INFO: SonarScanner 4.6.2.2472
  • INFO: Java 11.0.11 AdoptOpenJDK (64-bit)
  • INFO: Linux 3.10.0-1160.11.1.el7.x86_64 amd64

Best Regards,
Kristof

Hello @Kristof_Szabados,

You normally don’t have anything to do to have the files indexed once. What you experience is usually due to conflicting plugins. Are you trying to analyze your code with an external plug-in?

Hello @JolyLoic ,

This should be the c family plug.

So, most of the code in this project is separated into a “include” folder which contains in a folder hierarchy .hpp files and into a “sources” folder which contains the .cpp files.

What I noticed was, that there are no issues reported for any file under the include folder.
There are no build errors and also sonar does not report anything that I would identify as coming from include files not being available.
The include folder is also added to sonar.cxx.includeDirectories … so I believe Sonar finds the include files under it.
Yet, even if I intentionally introduce an issue into a .hpp file under the include folder (that has no .cpp pair as all functions are inlined there), that gets reported in .cpp files … I get no report for it.

So, I assumed, that maybe I also need to add these folders to sonar.sources for the analyzer to treat the files as sources, but that results in the error message that they can not be index twice.
Please note, that I have also created an other topic … as this error message talking about “main and test files” feels a bit generic and does not have enough information to help here (I believe).
I can exclude from the sources the .hpp files under the include folder, and the analysis will complete, but not report any issues for those files.

Do you know what I might be missing here, or how I could proceed further?

Thank you in advance,
Kristof

Hello @Kristof_Szabados,

sonar.cxx is community plugin for C++ analysis. It shouldn’t be used with the official SonarSource C++ analyzer. If your intention is to use the official one, please remove all the options that start with sonar.cxx and try again and let us know if you face any issue.
If your intention was to use sonar.cxx please direct your questions to its maintainers in their support channels.
Official C++ analyzer doc: C/C++/Objective-C | SonarQube Docs

Thanks

Dear @Abbas ,

I have commented out sonar.cxx.includeDirectories from our settings.
But the include directories are not yet show to contain any issues.

I tried adding them to sonar.sources, but that lead to the double indexing issue.
On my redirected output I have also looked for files under the include folder appearing in the debug output in “indexed with language” mentions, but did not find any.

What am I doing wrong?
Thank you in advance,
Kristof

Hi @Kristof_Szabados,

For a header file to be analyzed, it should be:

  • included in one of the source files compiled by the build-wrapper command.
  • residing in one of the subdirectories of sonar.sources. if you are adding the root directory that contains the headers, you shouldn’t add the files explicitly

If these two conditions are met, and you still face the issue, please share your scanner debug log and your build-wrapper JSON file.

If you prefer to share them privately, I can send you a private message.

Thanks,

Hi @Abbas,

It looks like I managed to solve the issue.
The actual source of the problem seemed to be, that when I tried to add the include folder to sonar.sources, it was added 2 times and sonar could not handle this.

So my problem is now solved.
But, this still does not really sound right:

  • Why is it an error that stops the analysis is the same if file has to be categorized 2 or more times?
    As it should get the same category each time, this should not be something that breaks the process.
    It sound more like a warning “what you are doing is not really efficient”.
  • Why is the error message indicating that the file would have different categories and that this has something to do with test files? This has the potential of creating a misunderstanding on the root cause of the issue … resulting in a lot of unnecessary debugging.

Could this be fixed?
Is this the right venue for me to report this issue, or should I raise a ticket somewhere?

Thank you,
Kristof

Hi @Kristof_Szabados,

Glad it worked.

Yes, the community is the right venue. Maybe it is better if it was through a different post(with different tags) since the issue here is more related to sonar-scanner than CFamily analyzer.

If you are indexing the same file twice, you are definitely doing something wrong. You should check what you did wrong and fix it. You are in a special case where you are indexing the same file twice as a source file. That is a rare mistake and it is not clear to me why you were doing it. But you should definitely fix it.

I kind of agree with you that the warning here is misleading. When a file is indexed twice, the common mistake is that it is indexed as a source file and a test file(using sonar.tests) hence the warning. The log can be improved to raise a different error message if it is indexed twice with the same type. Feel free to create a post to suggest that improvement with scanner and SonarQube tags.

Thanks,

Dear @Abbas ,

Thank you for you help.

Technically I have already created the topic here.
But, for some reason it looks like I can not add the suggested tags to this topic.

Is this possible in some menu, or should I create a new topic?

Thank you in advance,
Kristof

Hi @Kristof_Szabados ,
I added the tags.
Thanks

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.