Error during sonarscanner process

Hello,

I’m trying to use SonarCloud integrated with AzureDevOps. During the analysis process of C++ code, both on my PC (windows) or on the Azure CI, this error comes up several times:

ERROR: templates can only be declared in namespace or class scope

But in analysis result there is no trace of this error, and the pipeline completes successfully. Which is the root cause for this error and how can I fix it ?

Thank you in adavance,

BR

Welcome to the community @daniele.ferro!

The error you are listed looks like a compilation error, not any error that would be a producer by sonar-scanner analysis. In such case, it should be produced also when you are only building the project, without running a sonar-scanner analysis. And the correct course of action is to fix the code.

The error itself is not produced by our tool, but by the compiler itself. From my experience, it looks like being caused by a template method inside a nested class:

void func() {
  struct NestedClass {
    template<typename T>
    void method() const; // this is template inside a function, that is not allowed
  };
}

In such a case, the class should be declared outside the function in the namespace scope.
However, I do not recognize this particular message format (the compiler that you are using).

Thank you @Tomasz_Kaminski !

When I link the project the error never comes up - the compiler is clang 12 - so I suppose that the scan process relies on a different compiler (the one provided by visual studio). Is there a way to instruct sonar-scanner to use the clang 12 compiler ?

Could you please share the logs produced by the invocation of the sonar-scanner where the ERROR is produced?

As a separate question, I would like to ask to clarify what you mean by “analysis process” on CI. Usually, the analysis is compromised by two steps:

  1. producing the build-wrapper output, via running the build-wrapper on the project compilation.
  2. analyzing the project by invoking sonar-scanner with build-wrapper.

I was expecting the error to be produced by the first step (the compilation), and by the compiler that is used in CI.

If you agree, I would prefer to send you log file in a private message, because it is a proprietary source code and I can not share details.
Regarding your question, I meant that the error is present only during the sonar-scanner execution, not during the compilation. I followed the instruction you listed, but the error is present (several times) both on my PC and on the CI server (where an Azure agent is running).
How can I send you the requested log files ?

Send you a PM.

Thank you for the report and help with the reproduction @daniele.ferro.

The error message mentioned in the thread was indeed produced by the invocation of the sonar-scanner:
ERROR: templates can only be declared in namespace or class scope.
This is a parsing error, that should not be displayed by default, and require action to be displayed. These parsing errors, should not stop the analysis, and this is why is reported as successful by design. However, I fully agree that the current output of the analysis is confusing, and I have created the following ticket CPP-3898. You can use it to track the error.

The parsing error itself is the result of the fact, that we do not set our analysis in the MSVC compatibility mode, which supports non-standard constructs accepted only by this compiler, while clang looks to be doing it by default on windows. I have created a CPP-3899 ticket ticket to track the status.

Unfortunately the error is also displayed by Azure, as shown in the picture below:

In this case the error is listed more than 80 times, even though the pipeline completes successfully.
I will follow your tickets.

Thank you once again for your support Tomasz !