Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
omicronns opened this issue Jul 21, 2018 · 8 comments
Labels
bug Feature: Compile Commands fixed Check the Milestone for the release in which the fix is or will be available. Language Service quick fix

Comments

@omicronns
Copy link

omicronns commented Jul 21, 2018

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.

@bobbrow
Copy link
Member

bobbrow commented Jul 21, 2018

If you could share the compile commands entry that uses this define, that would be the fastest way for us to diagnose the problem. Otherwise, we'll take a look at generating one next week when we get back in the office.

@omicronns
Copy link
Author

Sure.
compile_commands.json

[
{
  "directory": "vscode-intelisense/build",
  "command": "/usr/bin/c++  -DABC=\\\"asd\\\" -D__weak=\"__attribute__((weak))\"    -std=gnu++1z -o CMakeFiles/vscode.dir/lib.cxx.o -c vscode-intelisense/lib.cxx",
  "file": "vscode-intelisense/lib.cxx"
}
]

@sean-mcmanus
Copy link
Contributor

sean-mcmanus commented Jul 23, 2018

You're telling the compiler to define __weak to be "__attribute__((weak))", i.e. a value assignable to a std::string. Can you use add_definitions(-D__weak=__attribute__((weak))) instead? That works for me.

@omicronns
Copy link
Author

omicronns commented Jul 23, 2018

This add_definitions(-D__weak=__attribute__((weak)) -DABC=\"asd\") gives me a compile error.

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `/usr/bin/c++  -D__weak=__attribute__  ( ( weak ) ) -std=gnu++1z -o CMakeFiles/vscode-intelisense.dir/lib.cxx.o -c /home/kad/Projects/test/lib.cxx'

Escaping parentheses add_definitions(-D__weak=__attribute__\(\(weak\)\) -DABC=\"asd\") gives me same behaviour as in original issue.

Also I think parenthesising whole "-D..." expression is not the same as parenthesising -D...="...".

@bobbrow
Copy link
Member

bobbrow commented Jul 23, 2018

@sean-mcmanus, this looks like it could be a bug. Note that -DABC is using \\\" to inject a double quote into the value. I'll take a look.

@omicronns
Copy link
Author

I noticed earlier, that on windows i got different compile_commands.json. When i run cmake on windows at least with mingw generator, it gets generated like -D__weak=__attribute__((weak)) without any escaped quotes like \".

@bobbrow
Copy link
Member

bobbrow commented Jul 23, 2018

I just looked at the tests and we don't have any for this case. That's probably why we missed it.

@sean-mcmanus sean-mcmanus added the fixed Check the Milestone for the release in which the fix is or will be available. label Aug 16, 2018
@sean-mcmanus
Copy link
Contributor

The parsing issues with compile_commands.json definitions should be fixed with 0.19.0 -- I think part of the fix made it into 0.18.0, but there were additional fixes afterwards.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Feature: Compile Commands fixed Check the Milestone for the release in which the fix is or will be available. Language Service quick fix
Projects
None yet
Development

No branches or pull requests

5 participants