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?
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?
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
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,
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?
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.