How to make clangd work with single-header library that uses an implementation macro

210 views Asked by At

I have a CMake project that generates a compile_commands.json database, which is used by clangd to provide completion and errors for me. This worked well until I brought in stb_image.h (a single-header image loading library) to my project. stb_image uses the standard macro-based approach to define its symbols, which looks like this:

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

In a source file. Now I've done something also quite common and standard in creating a separate source file stb_impl.cc just for these two lines hoping the resulting compilation unit would be picked up by CMake and put in compile_commands.json to avoid any problems with clangd not seeing the right info. But even so I'm stuck with this error:

enter image description here

Which only seems to occur with the macro definition, not with the include statement by itself. This is VS Code btw.

I have heard of the workaround of adding a post-process tool like compdb to explicitly add header files to compile_commands.json, but compdb isn't actively developed anymore and I'd rather not add any potential abandonware to my project. And I wonder if that would even fix my problem in the first place. Is there a better solution to be had here?

If anyone could help me to better understand what's going on here I would be grateful.

Edit: This is the clangd log when opening the file (lol_engine.cc in my project):

I[16:36:22.231] clangd version 17.0.4
I[16:36:22.231] Features: windows
I[16:36:22.231] PID: 4008
I[16:36:22.231] Working directory: c:\Users\reddd\source\oceanlight
I[16:36:22.231] argv[0]: C:\Program Files\LLVM\bin\clangd.exe
I[16:36:22.231] argv[1]: -header-insertion=never
I[16:36:22.231] argv[2]: --log=verbose
V[16:36:22.237] User config file is C:\Users\reddd\AppData\Local\clangd\config.yaml
I[16:36:22.237] Starting LSP over stdin/stdout
V[16:36:22.238] <<< {"id":0,"jsonrpc":"2.0","method":"initialize","params":{"capabilities":{"general":{"markdown":{"parser":"marked","version":"1.1.0"},"positionEncodings":["utf-16"],"regularExpressions":{"engine":"ECMAScript","version":"ES2020"},"staleRequestSupport":{"cancel":true,"retryOnContentModified":["textDocument/semanticTokens/full","textDocument/semanticTokens/range","textDocument/semanticTokens/full/delta"]}},"notebookDocument":{"synchronization":{"dynamicRegistration":true,"executionSummarySupport":true}},"textDocument":{"callHierarchy":{"dynamicRegistration":true},"codeAction":{"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}},"dataSupport":true,"disabledSupport":true,"dynamicRegistration":true,"honorsChangeAnnotations":false,"isPreferredSupport":true,"resolveSupport":{"properties":["edit"]}},"codeLens":{"dynamicRegistration":true},"colorProvider":{"dynamicRegistration":true},"completion":{"completionItem":{"commitCharactersSupport":true,"deprecatedSupport":true,"documentationFormat":["markdown","plaintext"],"insertReplaceSupport":true,"insertTextModeSupport":{"valueSet":[1,2]},"labelDetailsSupport":true,"preselectSupport":true,"resolveSupport":{"properties":["documentation","detail","additionalTextEdits"]},"snippetSupport":true,"tagSupport":{"valueSet":[1]}},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]},"completionList":{"itemDefaults":["commitCharacters","editRange","insertTextFormat","insertTextMode"]},"contextSupport":true,"dynamicRegistration":true,"editsNearCursor":true,"insertTextMode":2},"declaration":{"dynamicRegistration":true,"linkSupport":true},"definition":{"dynamicRegistration":true,"linkSupport":true},"diagnostic":{"dynamicRegistration":true,"relatedDocumentSupport":false},"documentHighlight":{"dynamicRegistration":true},"documentLink":{"dynamicRegistration":true,"tooltipSupport":true},"documentSymbol":{"dynamicRegistration":true,"hierarchicalDocumentSymbolSupport":true,"labelSupport":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"tagSupport":{"valueSet":[1]}},"foldingRange":{"dynamicRegistration":true,"foldingRange":{"collapsedText":false},"foldingRangeKind":{"valueSet":["comment","imports","region"]},"lineFoldingOnly":true,"rangeLimit":5000},"formatting":{"dynamicRegistration":true},"hover":{"contentFormat":["markdown","plaintext"],"dynamicRegistration":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"inlayHint":{"dynamicRegistration":true,"resolveSupport":{"properties":["tooltip","textEdits","label.tooltip","label.location","label.command"]}},"inlineValue":{"dynamicRegistration":true},"linkedEditingRange":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"publishDiagnostics":{"codeDescriptionSupport":true,"dataSupport":true,"relatedInformation":true,"tagSupport":{"valueSet":[1,2]},"versionSupport":false},"rangeFormatting":{"dynamicRegistration":true},"references":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"honorsChangeAnnotations":true,"prepareSupport":true,"prepareSupportDefaultBehavior":1},"selectionRange":{"dynamicRegistration":true},"semanticTokens":{"augmentsSyntaxTokens":true,"dynamicRegistration":true,"formats":["relative"],"multilineTokenSupport":false,"overlappingTokenSupport":false,"requests":{"full":{"delta":true},"range":true},"serverCancelSupport":true,"tokenModifiers":["declaration","definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary"],"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator","decorator"]},"signatureHelp":{"contextSupport":true,"dynamicRegistration":true,"signatureInformation":{"activeParameterSupport":true,"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true}}},"synchronization":{"didSave":true,"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"typeHierarchy":{"dynamicRegistration":true}},"window":{"showDocument":{"support":true},"showMessage":{"messageActionItem":{"additionalPropertiesSupport":true}},"workDoneProgress":true},"workspace":{"applyEdit":true,"codeLens":{"refreshSupport":true},"configuration":true,"diagnostics":{"refreshSupport":true},"didChangeConfiguration":{"dynamicRegistration":true},"didChangeWatchedFiles":{"dynamicRegistration":true,"relativePatternSupport":true},"executeCommand":{"dynamicRegistration":true},"fileOperations":{"didCreate":true,"didDelete":true,"didRename":true,"dynamicRegistration":true,"willCreate":true,"willDelete":true,"willRename":true},"inlayHint":{"refreshSupport":true},"inlineValue":{"refreshSupport":true},"semanticTokens":{"refreshSupport":true},"symbol":{"dynamicRegistration":true,"resolveSupport":{"properties":["location.range"]},"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"tagSupport":{"valueSet":[1]}},"workspaceEdit":{"changeAnnotationSupport":{"groupsOnLabel":true},"documentChanges":true,"failureHandling":"textOnlyTransactional","normalizesLineEndings":true,"resourceOperations":["create","rename","delete"]},"workspaceFolders":true}},"clientInfo":{"name":"Visual Studio Code - Insiders","version":"1.85.0-insider"},"initializationOptions":{"clangdFileStatus":true,"fallbackFlags":[]},"locale":"en-us","processId":1012,"rootPath":"c:\\Users\\reddd\\source\\oceanlight","rootUri":"file:///c%3A/Users/reddd/source/oceanlight","trace":"off","workspaceFolders":[{"name":"oceanlight","uri":"file:///c%3A/Users/reddd/source/oceanlight"}]}}

I[16:36:22.238] <-- initialize(0)
I[16:36:22.240] --> reply:initialize(0) 1 ms
V[16:36:22.240] >>> {"id":0,"jsonrpc":"2.0","result":{"capabilities":{"astProvider":true,"callHierarchyProvider":true,"clangdInlayHintsProvider":true,"codeActionProvider":{"codeActionKinds":["quickfix","refactor","info"]},"compilationDatabase":{"automaticReload":true},"completionProvider":{"resolveProvider":false,"triggerCharacters":[".","<",">",":","\"","/","*"]},"declarationProvider":true,"definitionProvider":true,"documentFormattingProvider":true,"documentHighlightProvider":true,"documentLinkProvider":{"resolveProvider":false},"documentOnTypeFormattingProvider":{"firstTriggerCharacter":"\n","moreTriggerCharacter":[]},"documentRangeFormattingProvider":true,"documentSymbolProvider":true,"executeCommandProvider":{"commands":["clangd.applyFix","clangd.applyTweak"]},"foldingRangeProvider":true,"hoverProvider":true,"implementationProvider":true,"inactiveRegionsProvider":true,"inlayHintProvider":true,"memoryUsageProvider":true,"referencesProvider":true,"renameProvider":{"prepareProvider":true},"selectionRangeProvider":true,"semanticTokensProvider":{"full":{"delta":true},"legend":{"tokenModifiers":["declaration","definition","deprecated","deduced","readonly","static","abstract","virtual","dependentName","defaultLibrary","usedAsMutableReference","usedAsMutablePointer","constructorOrDestructor","userDefined","functionScope","classScope","fileScope","globalScope"],"tokenTypes":["variable","variable","parameter","function","method","function","property","variable","class","interface","enum","enumMember","type","type","unknown","namespace","typeParameter","concept","type","macro","modifier","operator","bracket","label","comment"]},"range":false},"signatureHelpProvider":{"triggerCharacters":["(",")","{","}","<",">",","]},"standardTypeHierarchyProvider":true,"textDocumentSync":{"change":2,"openClose":true,"save":true},"typeDefinitionProvider":true,"typeHierarchyProvider":true,"workspaceSymbolProvider":true},"serverInfo":{"name":"clangd","version":"clangd version 17.0.4 windows x86_64-pc-windows-msvc"}}}

V[16:36:22.242] <<< {"jsonrpc":"2.0","method":"initialized","params":{}}

I[16:36:22.242] <-- initialized
I[16:36:22.247] <-- textDocument/didOpen
I[16:36:22.249] Loaded compilation database from c:\Users\reddd\source\oceanlight\build/compile_commands.json
I[16:36:22.253] Loaded compilation database from C:\Users\reddd\source\oceanlight\build\compile_commands.json
I[16:36:22.255] --> window/workDoneProgress/create(0)
V[16:36:22.255] >>> {"id":0,"jsonrpc":"2.0","method":"window/workDoneProgress/create","params":{"token":"backgroundIndexProgress"}}

I[16:36:22.255] Enqueueing 12 commands for indexing
V[16:36:22.256] <<< {"id":0,"jsonrpc":"2.0","result":null}

I[16:36:22.256] <-- reply(0)
I[16:36:22.256] --> $/progress
V[16:36:22.256] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"begin","percentage":0,"title":"indexing"}}}

I[16:36:22.256] --> $/progress
V[16:36:22.256] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"0/1","percentage":0}}}

I[16:36:22.259] --> textDocument/clangd.fileStatus
V[16:36:22.259] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"parsing includes, running Update","uri":"file:///c:/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}

V[16:36:22.259] Building first preamble for c:\Users\reddd\source\oceanlight\liboceanlight\src\lol_engine.cc version 1
V[16:36:22.273] <<< {"jsonrpc":"2.0","method":"$/setTrace","params":{"value":"off"}}

I[16:36:22.273] <-- $/setTrace
I[16:36:22.273] unhandled notification $/setTrace
V[16:36:22.455] BackgroundIndex: building version 1 after loading index from disk
V[16:36:22.649] BackgroundIndex: serving version 1 (42361812 bytes)
I[16:36:22.704] --> $/progress
V[16:36:22.704] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"0/2","percentage":0}}}

I[16:36:22.704] --> $/progress
V[16:36:22.704] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"0/2","percentage":0}}}

I[16:36:22.705] --> $/progress
V[16:36:22.705] >>> {"jsonrpc":"2.0","method":"$/progress","params":{"token":"backgroundIndexProgress","value":{"kind":"report","message":"1/2","percentage":50}}}

V[16:36:22.705] Indexing C:\Users\reddd\source\oceanlight\liboceanlight\src\lol_engine.cc (digest:=87447B9F074A07B8)
V[16:36:22.893] <<< {"id":1,"jsonrpc":"2.0","method":"textDocument/documentLink","params":{"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:22.893] <-- textDocument/documentLink(1)
V[16:36:23.108] <<< {"id":2,"jsonrpc":"2.0","method":"textDocument/codeAction","params":{"context":{"diagnostics":[],"triggerKind":2},"range":{"end":{"character":17,"line":2},"start":{"character":17,"line":2}},"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:23.108] <-- textDocument/codeAction(2)
V[16:36:23.164] <<< {"jsonrpc":"2.0","method":"$/setTrace","params":{"value":"off"}}

I[16:36:23.164] <-- $/setTrace
I[16:36:23.164] unhandled notification $/setTrace
V[16:36:23.219] <<< {"jsonrpc":"2.0","method":"$/setTrace","params":{"value":"off"}}

I[16:36:23.219] <-- $/setTrace
I[16:36:23.219] unhandled notification $/setTrace
V[16:36:23.237] <<< {"jsonrpc":"2.0","method":"$/cancelRequest","params":{"id":2}}

I[16:36:23.237] <-- $/cancelRequest
V[16:36:23.264] <<< {"id":3,"jsonrpc":"2.0","method":"textDocument/codeAction","params":{"context":{"diagnostics":[],"triggerKind":2},"range":{"end":{"character":17,"line":2},"start":{"character":17,"line":2}},"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:23.264] <-- textDocument/codeAction(3)
V[16:36:23.266] <<< {"jsonrpc":"2.0","method":"$/cancelRequest","params":{"id":3}}

I[16:36:23.266] <-- $/cancelRequest
V[16:36:23.281] <<< {"id":4,"jsonrpc":"2.0","method":"textDocument/codeAction","params":{"context":{"diagnostics":[],"triggerKind":2},"range":{"end":{"character":17,"line":2},"start":{"character":17,"line":2}},"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:23.281] <-- textDocument/codeAction(4)
V[16:36:23.375] <<< {"id":5,"jsonrpc":"2.0","method":"textDocument/foldingRange","params":{"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:23.375] <-- textDocument/foldingRange(5)
I[16:36:23.376] --> reply:textDocument/foldingRange(5) 1 ms

V[16:36:23.381] <<< {"jsonrpc":"2.0","method":"$/setTrace","params":{"value":"off"}}

I[16:36:23.381] <-- $/setTrace
I[16:36:23.381] unhandled notification $/setTrace
V[16:36:23.392] <<< {"id":6,"jsonrpc":"2.0","method":"textDocument/semanticTokens/full","params":{"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:23.392] <-- textDocument/semanticTokens/full(6)
V[16:36:23.395] <<< {"jsonrpc":"2.0","method":"$/setTrace","params":{"value":"off"}}

I[16:36:23.395] <-- $/setTrace
I[16:36:23.395] unhandled notification $/setTrace
V[16:36:23.409] <<< {"jsonrpc":"2.0","method":"$/cancelRequest","params":{"id":1}}

I[16:36:23.409] <-- $/cancelRequest
V[16:36:23.411] <<< {"id":7,"jsonrpc":"2.0","method":"textDocument/documentLink","params":{"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:23.411] <-- textDocument/documentLink(7)
V[16:36:23.950] <<< {"jsonrpc":"2.0","method":"$/cancelRequest","params":{"id":4}}

I[16:36:23.950] <-- $/cancelRequest
V[16:36:23.962] <<< {"id":8,"jsonrpc":"2.0","method":"textDocument/codeAction","params":{"context":{"diagnostics":[],"triggerKind":2},"range":{"end":{"character":17,"line":2},"start":{"character":17,"line":2}},"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:23.962] <-- textDocument/codeAction(8)
V[16:36:23.963] <<< {"id":9,"jsonrpc":"2.0","method":"textDocument/documentSymbol","params":{"textDocument":{"uri":"file:///c%3A/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}}

I[16:36:23.963] <-- textDocument/documentSymbol(9)
I[16:36:24.108] Built preamble of size 24013120 for file c:\Users\reddd\source\oceanlight\liboceanlight\src\lol_engine.cc version 1 in 1.85 seconds
I[16:36:24.109] --> workspace/semanticTokens/refresh(1)
V[16:36:24.109] >>> {"id":1,"jsonrpc":"2.0","method":"workspace/semanticTokens/refresh","params":null}

I[16:36:24.109] --> textDocument/clangd.fileStatus
I[16:36:24.109] Indexing c++20 standard library in the context of c:\Users\reddd\source\oceanlight\liboceanlight\src\lol_engine.cc
V[16:36:24.109] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"parsing includes, running Build AST","uri":"file:///c:/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc"}}

V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__cpuid3'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__sse2_available'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__refill_buffer'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__start_mem'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__start_callbacks'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__start_file'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__rewind'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__jpeg_test'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__jpeg_load'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__jpeg_info'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__png_test'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__png_load'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__png_info'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__png_is16'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__bmp_test'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__bmp_load'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__bmp_info'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__tga_test'
V[16:36:24.220] Dropped diagnostic: C:\Users\reddd\source\oceanlight\build\liboceanlight\external\stb\src\stb_image.h: unused function 'stbi__tga_load'
V[16:36:24.231] indexed file AST for c:\Users\reddd\source\oceanlight\liboceanlight\src\lol_engine.cc version 1:
  symbol slab: 9 symbols, 6312 bytes
  ref slab: 167 symbols, 274 refs, 17488 bytes
  relations slab: 0 relations, 24 bytes
V[16:36:24.231] Build dynamic index for main-file symbols with estimated memory usage of 41824 bytes
I[16:36:24.231] --> textDocument/publishDiagnostics
V[16:36:24.231] >>> {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[{"code":"fatal_too_many_errors","message":"Too many errors emitted, stopping now","range":{"end":{"character":0,"line":0},"start":{"character":0,"line":0}},"relatedInformation":[],"severity":1,"source":"clang"}],"uri":"file:///c:/Users/reddd/source/oceanlight/liboceanlight/src/lol_engine.cc","version":1}}

I[16:36:24.232] --> reply:textDocument/documentLink(1) 1339 ms, error: Task was cancelled.
V[16:36:24.232] >>> {"error":{"code":-32800,"message":"Request cancelled"},"id":1,"jsonrpc":"2.0"}

I[16:36:24.232] --> reply:textDocument/codeAction(2) 1123 ms, error: Task was cancelled.
V[16:36:24.232] >>> {"error":{"code":-32800,"message":"Request cancelled"},"id":2,"jsonrpc":"2.0"}

I[16:36:24.232] --> reply:textDocument/codeAction(3) 968 ms, error: Task was cancelled.
V[16:36:24.232] >>> {"error":{"code":-32800,"message":"Request cancelled"},"id":3,"jsonrpc":"2.0"}

I[16:36:24.232] --> reply:textDocument/codeAction(4) 951 ms, error: Task was cancelled.
V[16:36:24.232] >>> {"error":{"code":-32800,"message":"Request cancelled"},"id":4,"jsonrpc":"2.0"}

V[16:36:24.232] ASTWorker running SemanticHighlights on version 1 of c:\Users\reddd\source\oceanlight\liboceanlight\src\lol_engine.cc
I[16:36:24.233] --> reply:textDocument/semanticTokens/full(6) 840 ms

V[16:36:26.418] Ignored diagnostic. C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\experimental/filesystem:28:2:The <experimental/filesystem> header providing std::experimental::filesystem is deprecated by Microsoft and will be REMOVED. It is superseded by the C++17 <filesystem> header providing std::filesystem. You can define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING to suppress this error.
I[16:36:26.735] Indexed c++20 standard library (incomplete due to errors): 15048 symbols, 7655 filtered
V[16:36:26.859] Build dynamic index for header symbols with estimated memory usage of 26303316 bytes

My project structure looks like this:

tree /f
C:.
│   .clang-format
│   .clang-tidy
│   .gitignore
│   CMakeLists.txt
│   CONTRIBUTING.md
│   LICENSE
│   README.md
│
├───.vscode
│       settings.json
│
├───build
├───liboceanlight
│   │   CMakeLists.txt
│   │   config.h.in
│   │
│   ├───include
│   │   └───liboceanlight
│   │           lol_debug_messenger.hpp
│   │           lol_engine.hpp
│   │           lol_engine_init.hpp
│   │           lol_engine_shutdown.hpp
│   │           lol_glfw_callbacks.hpp
│   │           lol_utility.hpp
│   │           lol_version.hpp
│   │           lol_window.hpp
│   │
│   ├───shaders
│   │       fragment_shader.frag
│   │       vertex_shader.vert
│   │
│   ├───src
│   │       lol_debug_messenger.cc
│   │       lol_engine.cc
│   │       lol_engine_init.cc
│   │       lol_engine_shutdown.cc
│   │       lol_glfw_callbacks.cc
│   │       lol_utility.cc
│   │       lol_version.cc
│   │       lol_window.cc
│   │       stb_impl.cc
│   │
│   └───test
│           CMakeLists.txt
│           lol_utility_test.cc
│
└───oceanlight
    │   CMakeLists.txt
    │   config.h.in
    │
    ├───include
    │       args.hpp
    │
    ├───src
    │       args.cc
    │       oceanlight.cc
    │
    └───test

With CMake configured to a generate compile_commands.json for clangd to parse.

I have also tried defining STB_IMAGE_IMPLEMENTATION in a local .clangd file, but I still get the same diagnostic wherever stb_image.h is included.

I should also mention I do not get this problem when using neovim's LSP under WSL. Nor do I get it on VS Code under WSL.

2

There are 2 answers

0
Red On BEST ANSWER

Okay so as it turns out, making a .clangd file with the following contents does indeed fix this for me:

CompileFlags:
  Add: -ferror-limit=0

I figured out that I was setting -ferror-limit to very large values instead of zero and I guess those values just weren't large enough.

I still don't know why this works, or how adding more errors makes the errors disappear, or where the errors go when they die, but this worked for me.

0
Marcus Müller On

I didn't know compdb, but I mean, compile_commands.json is really just pretty boring json, just add an entry for a compile unit stb_image.h which copies the configuration from an appropriate existing entry, adjusts "file" to be the path to stb_image.h, adjusts "output" to be something like "stb_image_temp.o", copies over the "command", but appends a -DSTB_IMAGE_IMPLEMENTATION to that.

You do that operation in a add_custom_command. It should be possible to piece that together in a single invocation of jq.