Skip to content

Definitions passed via compile_commands.json (using CMake), gets parsed wrong. #2305

@omicronns

Description

@omicronns

Type: LanguageService

Definitions from compile_commands.json gets parsed wrong. If I want to pass definition via CMake I add following statement in CMakeLists.txt:

add_definitions("-D__weak=__attribute__((weak))" -DABC=\"asd\")

Now __weak should be replaced with __attribute__((weak)) in code, and ABC should be replaced with "asd" string literal. At least that how compiler behaves. Instead if I hover over __weak i get #define __weak "__attribute__((weak))" which is incorrect. Also if i hover over ABC i get proper information #define ABC "asd". This happens only when definition contains one or more characters that have to be escaped, so if I set __weak definition to __attribute__ it gets passed correctly. It happens because without need to escape characters, part after = in definition does not get wrapped in " by CMake. So instead -D__weak="__attribute__((weak))" on command line appears -D__weak=__attribute__.
This also causes problem to be reported (but it is due to invalid __weak value):

{
	"resource": "vscode-intelisense/lib.cxx",
	"owner": "_generated_diagnostic_collection_name_#0",
	"code": "undefined",
	"severity": 8,
	"message": "expected a declaration",
	"startLineNumber": 1,
	"startColumn": 1,
	"endLineNumber": 1,
	"endColumn": 2
}
  • OS and Version: Linux 4.17.6-1-ARCH
  • VS Code Version: 1.25.1
  • C/C++ Extension Version: 0.17.7
  • Clang Version: 6.0.1

To Reproduce

Example:
c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}

lib.cxx

__weak void foo() {ABC;}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(vscode-intelisense)

set(CMAKE_EXPORT_COMPILE_COMMANDS "1")

set(CMAKE_CXX_STANDARD 17)

add_library(${PROJECT_NAME} STATIC lib.cxx)
add_definitions("-D__weak=__attribute__((weak))" -DABC=\"asd\")

Steps to reproduce the behavior:

  1. Recreate file structure:
vscode-intelisense
  |
  |-lib.cxx
  |-CMakeLists.txt
  .vscode
    |-c_cpp_properties.json
  1. Build with rm -rf build && cmake . -Bbuild && VERBOSE=1 cmake --build build inside vscode-intelisense folder.
  2. Open vscode-intelisense directory in vscode.
  3. Hover over __weak and ABC in lib.cxx.

Expected behavior

__weak definition to be recognized correctly as __attribute__((weak)) instead "__attribute__((weak))".
No problems reported.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions