SonarLint does not detect issues when using C++ Standard 20 with CMake

  • Operating system: Windows 11 pro
  • SonarLint plugin version: v4.2.2
  • Programming language you’re coding in: CPP
  • VSCode version: 1.85.1
  • cpp dev environment: clangd + CodeLLDB + cmakeTools
  • clang version:
    clang version 17.0.6
    Target: x86_64-w64-windows-gnu
    Thread model: posix
  • Is connected mode used:no

As per the documentation, I utilize CMake to generate the ‘compile_commands.json’ file. It is correct when I set the C++ standard to 17 .But when I use CPP20, SonarLint is unable to detect issues.

CPP code:

#include <iostream>

int x;
int t() {
  int a = 0;  
  std::cout << "Hello, World!" << std::endl;

  return 0;
}

cmakelists:

cmake_minimum_required(VERSION 3.28)
project(MyProject)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
file(GLOB SOURCE_FILES "src/*.cpp")
add_executable(MyProject ${SOURCE_FILES})

compile_commands.json:

[
{
  "directory": "F:/person/cppLearn/build",
  "command": "C:\\msys64\\clang64\\bin\\c++.exe -std=gnu++17 -o CMakeFiles\\MyProject.dir\\src\\main.cpp.obj -c F:\\person\\cppLearn\\src\\main.cpp",
  "file": "F:\\person\\cppLearn\\src\\main.cpp",
  "output": "CMakeFiles\\MyProject.dir\\src\\main.cpp.obj"
}
]

sonarLint output:
[Info - 21:26:33.336] Analyzing file “file:///f:/person/cppLearn/src/main.cpp”…
[Info - 21:26:33.737] Found 2 issues

when set cpp standard 20 and regen json

compile_commands.json:

[
{
  "directory": "F:/person/cppLearn/build",
  "command": "C:\\msys64\\clang64\\bin\\c++.exe -std=gnu++20 @CMakeFiles\\MyProject.dir\\src\\main.cpp.obj.modmap -o CMakeFiles\\MyProject.dir\\src\\main.cpp.obj -c F:\\person\\cppLearn\\src\\main.cpp",
  "file": "F:\\person\\cppLearn\\src\\main.cpp",
  "output": "CMakeFiles\\MyProject.dir\\src\\main.cpp.obj"
}
]

sonarlint output:
[Info - 21:29:41.776] Analyzing file “file:///f:/person/cppLearn/src/main.cpp”…
[Info - 21:29:41.814] Found 0 issues

Hi @lunchbox,

I notice in your second example, you have a response file: @CMakeFiles\\MyProject.dir\\src\\main.cpp.obj.modmap

We don’t support unexpanded files in the compilation database yet. see [CPP-3572] - Jira

You can fix this issue by setting these two CMAKE variables and cleanly regenerating the compilation database:

set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES 0)
set(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES 0)

Also, I suggest enabling analyzer logs if you want to get more detailed SonarLint output: Running an analysis - VS Code

Thanks,

2 Likes

thks! :smiling_face_with_three_hearts:

1 Like

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