clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
620 stars 101 forks source link

clangd does not support source_location #527

Closed InspAlgo closed 1 year ago

InspAlgo commented 1 year ago

Please describe the problem.

For the following code, I have configured C++20 version, which also supports the format syntax. However, clangd does not support the syntax for source_location. I'm using MSVC for compilation, specifying C++20, and as you can see from the screenshot below, the compilation and execution are successful.

#include <format>
#include <iostream>
#include <source_location>

int main() {
    auto loc = std::source_location::current();
    std::cout << std::format("line={} col={} file={} func={}",
                             loc.line(), loc.column(), loc.file_name(), loc.function_name())
              << std::endl;
}

Snipaste_2023-09-10_20-19-52 Snipaste_2023-09-10_20-36-23

As this is not a complete project, but rather a single-file compilation, I haven't configured a compile_command.json file. Single-file compilation is more suitable for my current learning of new C++ syntax.

settings.json

    "clangd.path": "C:\\Program Files\\LLVM\\bin\\clangd.exe",
    "clangd.arguments": [
        "--all-scopes-completion", 
        "--header-insertion=iwyu",
        "--background-index",
        "--query-driver=C:\\Program Files\\LLVM\\bin\\clang++.exe",
        "--clang-tidy", 
        "--clang-tidy-checks=cppcoreguidelines-*,performance-*,bugprone-*,portability-*,modernize-*,google-*",
        "--completion-parse=auto", 
        "--completion-style=detailed", 
        "--fallback-style=Webkit", 
        "--pretty", 
        "--ranking-model=decision_forest", 
        "--log=verbose", 
        "-j=12"
    ],
    "clangd.fallbackFlags": [
        "-std=c++20",
        "-pedantic",
        "-Wall",
        "-Wextra",
        "-Wcast-align",
        "-Wdouble-promotion",
        "-Wformat=2",
        "-Wimplicit-fallthrough",
        "-Wmisleading-indentation",
        "-Wnon-virtual-dtor",
        "-Wnull-dereference",
        "-Wold-style-cast",
        "-Woverloaded-virtual",
        "-Wpedantic",
        "-Wshadow",
        "-Wunused",
        "-pthread",
        "-fuse-ld=lld",
        "-fsanitize=address",
        "-fsanitize=undefined",
        "-I${workspaceFolder}"
    ]

tasks.json The version of MSVC is 14.36.32532.

{
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "cmd.exe",
                "args": [
                    "/C",
                    "\"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat\"",
                    "&&"
                ]
            }
        }
    },
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/utf-8",
                "/Zc:__cplusplus",
                "/Zi",
                "/EHsc",
                "/std:c++20",
                "/Fe:",
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: cl.exe build and debug active file",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "console": "internalConsole",
            "preLaunchTask": "C/C++: cl.exe build active file"
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-clang-x64"
        }
    ],
    "version": 4
}

Logs clangd.log

System information Clangd version (from the log, or clangd --version):

clangd version 16.0.6
Features: windows
Platform: x86_64-pc-windows-msvc

clangd extension version: v0.1.24 Operating system: Windows 11 23H2

InspAlgo commented 1 year ago

.clangd/config.yaml

CompileFlags:
  Add: [-std=c++20]
  Compiler: clang++
Diagnostics:
  ClangTidy:
    Add: ["*"]
    Remove:
      [
        abseil*,
        fuchsia*,
        llvmlib*,
        zircon*,
        altera*,
        google-readability-todo,
        readability-braces-around-statements,
        hicpp-braces-around-statements,
        modernize-use-trailing-return-type,
      ]
Index:
  Background: Build
HighCommander4 commented 1 year ago

Can you ctrl+click on <source_location> and take a screenshot of what the header's contents (just the first page seen in the editor) looks like?

InspAlgo commented 1 year ago

Can you ctrl+click on <source_location> and take a screenshot of what the header's contents (just the first page seen in the editor) looks like?

Snipaste_2023-09-11_21-41-10

InspAlgo commented 1 year ago

Can you ctrl+click on <source_location> and take a screenshot of what the header's contents (just the first page seen in the editor) looks like?

It seems I've found a solution. I updated my MSVC version, which is now 14.37.32822. It was previously 14.36.32532, and I noticed there have been changes in the macro definitions. Snipaste_2023-09-11_22-09-25 Snipaste_2023-09-11_22-10-49

However, I wonder why, even though I've included -std=c++20 in the clangd.fallbackFlags parameter, the macro __cpp_consteval is not available. My current clangd version is 16.0.6, which should support it, right?

HighCommander4 commented 1 year ago

However, I wonder why, even though I've included -std=c++20 in the clangd.fallbackFlags parameter, the macro __cpp_consteval is not available. My current clangd version is 16.0.6, which should support it, right?

https://clang.llvm.org/cxx_status.html shows "clang 17" in the entry for "Immediate functions (consteval)"