clangd
clangd copied to clipboard
False positive `-Wempty-translation-unit` warning
clangd reports an -Wempty-translation-unit warning in this C code,
int main() {}
…under the following configuration.
CompileFlags:
Add: -Wempty-translation-unit
I think this is a bug in clangd because clang -Wempty-translation-unit on the same code doesn't print any warning.
Logs
I[19:38:40.000] clangd version 21.0.0git (https://github.com/llvm/llvm-project a2ca2f3f10002da61e9860d0ce11e0272482baba)
I[19:38:40.002] Features: mac+grpc+xpc
I[19:38:40.002] PID: 485
I[19:38:40.002] Working directory: /Users/ud2/repro
I[19:38:40.002] argv[0]: /Users/ud2/Downloads/clangd_snapshot_20250401/bin/clangd
I[19:38:40.002] argv[1]: --log=verbose
V[19:38:40.002] User config file is /Users/ud2/Library/Preferences/clangd/config.yaml
I[19:38:40.002] Starting LSP over stdin/stdout
V[19:38:40.003] <<< {"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},"inactiveRegionsCapabilities":{"inactiveRegions":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","version":"1.99.0"},"initializationOptions":{"clangdFileStatus":true,"fallbackFlags":[]},"locale":"en","processId":449,"rootPath":"/Users/ud2/repro","rootUri":"file:///Users/ud2/repro","trace":"off","workspaceFolders":[{"name":"repro","uri":"file:///Users/ud2/repro"}]}}
I[19:38:40.003] <-- initialize(0)
V[19:38:40.003] Invoking /usr/bin/xcrun to find clang installation
V[19:38:40.021] Invoking /usr/bin/xcrun to find clang installation
I[19:38:40.032] --> reply:initialize(0) 29 ms
V[19:38:40.032] >>> {"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.applyRename","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 21.0.0git (https://github.com/llvm/llvm-project a2ca2f3f10002da61e9860d0ce11e0272482baba) mac+grpc+xpc arm64-apple-darwin24.3.0; target=x86_64-apple-darwin24.3.0"}}}
V[19:38:40.033] <<< {"jsonrpc":"2.0","method":"initialized","params":{}}
I[19:38:40.033] <-- initialized
V[19:38:41.934] <<< {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"languageId":"c","text":"int main() {}\n","uri":"file:///Users/ud2/repro/main.c","version":1}}}
I[19:38:41.934] <-- textDocument/didOpen
V[19:38:41.934] config note at /Users/ud2/repro/.clangd:1:0: Parsing config fragment
V[19:38:41.934] config note at /Users/ud2/repro/.clangd:1:0: Parsed 1 fragments from file
V[19:38:41.934] Config fragment: compiling /Users/ud2/repro/.clangd:1 -> 0x0000000121004FB8 (trusted=false)
I[19:38:41.935] --> textDocument/publishDiagnostics
V[19:38:41.935] >>> {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[],"uri":"file:///Users/ud2/repro/.clangd"}}
I[19:38:41.935] Failed to find compilation database for /Users/ud2/repro/main.c
I[19:38:41.935] ASTWorker building file /Users/ud2/repro/main.c version 1 with command clangd fallback
[/Users/ud2/repro]
/Library/Developer/CommandLineTools/usr/bin/clang -Wempty-translation-unit -resource-dir=/Users/ud2/Downloads/clangd_snapshot_20250401/lib/clang/21 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -- /Users/ud2/repro/main.c
V[19:38:41.937] Driver produced command: cc1 -cc1 -triple x86_64-apple-macosx15.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -fsyntax-only -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mframe-pointer=all -ffp-contract=on -fno-rounding-math -funwind-tables=2 -target-sdk-version=15.4 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -fdefine-target-os-macros -fno-modulemap-allow-subdirectory-search -target-cpu penryn -tune-cpu generic -debugger-tuning=lldb -fdebug-compilation-dir=/Users/ud2/repro -target-linker-version 1022.1 -fcoverage-compilation-dir=/Users/ud2/repro -resource-dir /Users/ud2/Downloads/clangd_snapshot_20250401/lib/clang/21 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Users/ud2/Downloads/clangd_snapshot_20250401/lib/clang/21/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -Wempty-translation-unit -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fmax-type-align=16 -no-round-trip-args -D__GCC_HAVE_DWARF2_CFI_ASM=1 -x c /Users/ud2/repro/main.c
I[19:38:41.937] --> textDocument/clangd.fileStatus
V[19:38:41.937] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"parsing includes, running Update","uri":"file:///Users/ud2/repro/main.c"}}
V[19:38:41.937] Building first preamble for /Users/ud2/repro/main.c version 1
I[19:38:41.944] Built preamble of size 247864 for file /Users/ud2/repro/main.c version 1 in 0.01 seconds
I[19:38:41.945] --> workspace/semanticTokens/refresh(0)
V[19:38:41.945] >>> {"id":0,"jsonrpc":"2.0","method":"workspace/semanticTokens/refresh","params":null}
V[19:38:41.945] indexed preamble AST for /Users/ud2/repro/main.c version 1:
symbol slab: 0 symbols, 120 bytes
ref slab: 0 symbols, 0 refs, 128 bytes
relations slab: 0 relations, 24 bytes
V[19:38:41.948] <<< {"id":1,"jsonrpc":"2.0","method":"textDocument/documentSymbol","params":{"textDocument":{"uri":"file:///Users/ud2/repro/main.c"}}}
I[19:38:41.948] <-- textDocument/documentSymbol(1)
V[19:38:41.948] <<< {"id":2,"jsonrpc":"2.0","method":"textDocument/semanticTokens/full","params":{"textDocument":{"uri":"file:///Users/ud2/repro/main.c"}}}
I[19:38:41.948] <-- textDocument/semanticTokens/full(2)
I[19:38:41.948] Indexing c17 standard library in the context of /Users/ud2/repro/main.c
V[19:38:41.948] <<< {"id":0,"jsonrpc":"2.0","result":null}
I[19:38:41.948] <-- reply(0)
V[19:38:41.959] indexed file AST for /Users/ud2/repro/main.c version 1:
symbol slab: 1 symbols, 4448 bytes
ref slab: 1 symbols, 1 refs, 4248 bytes
relations slab: 0 relations, 24 bytes
V[19:38:41.959] Build dynamic index for main-file symbols with estimated memory usage of 11520 bytes
I[19:38:41.959] --> textDocument/publishDiagnostics
V[19:38:41.959] >>> {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[{"code":"-Wempty-translation-unit","message":"ISO C requires a translation unit to contain at least one declaration","range":{"end":{"character":0,"line":0},"start":{"character":0,"line":0}},"relatedInformation":[],"severity":2,"source":"clang"}],"uri":"file:///Users/ud2/repro/main.c","version":1}}
I[19:38:41.959] --> textDocument/inactiveRegions
V[19:38:41.959] >>> {"jsonrpc":"2.0","method":"textDocument/inactiveRegions","params":{"regions":[],"textDocument":{"uri":"file:///Users/ud2/repro/main.c"}}}
V[19:38:41.959] ASTWorker running DocumentSymbols on version 1 of /Users/ud2/repro/main.c
I[19:38:41.959] --> reply:textDocument/documentSymbol(1) 10 ms
V[19:38:41.959] >>> {"id":1,"jsonrpc":"2.0","result":[{"detail":"int ()","kind":12,"name":"main","range":{"end":{"character":13,"line":0},"start":{"character":0,"line":0}},"selectionRange":{"end":{"character":8,"line":0},"start":{"character":4,"line":0}}}]}
V[19:38:41.959] ASTWorker running SemanticHighlights on version 1 of /Users/ud2/repro/main.c
I[19:38:41.959] --> reply:textDocument/semanticTokens/full(2) 10 ms
V[19:38:41.959] >>> {"id":2,"jsonrpc":"2.0","result":{"data":[0,4,4,3,131075],"resultId":"1"}}
I[19:38:41.959] --> textDocument/clangd.fileStatus
V[19:38:41.959] >>> {"jsonrpc":"2.0","method":"textDocument/clangd.fileStatus","params":{"state":"idle","uri":"file:///Users/ud2/repro/main.c"}}
System information
Output of clangd --version:
clangd version 21.0.0git (https://github.com/llvm/llvm-project a2ca2f3f10002da61e9860d0ce11e0272482baba)
Features: mac+grpc+xpc
Platform: arm64-apple-darwin24.3.0; target=x86_64-apple-darwin24.3.0
Editor/LSP plugin: vscode-clangd 0.1.33
Operating system: macOS 15.3.2
The diagnostic is coming from the preamble --> this is an artifact of clangd's preamble optimization.
Clangd is basically processing the file in two steps that are roughly equivalent to:
clang -cc1 preamble.h -emit-pch -o preamble.h.pch -Wempty-translation-unit
clang -cc1 -include-pch preamble.h.pch main.c -Wempty-translation-unit
where preamble.h is an invented file containing "all of the includes at the top of main.c". In this case, preamble.h is empty.
Can confirm this. Starting the file with this worked for me as a workaround:
#ifdef __clang__
#pragma clang diagnostic ignored "-Wempty-translation-unit" // bug?
#endif
/* your normal code here */
(my target only has gcc as a possible compiler, thats why i can make the destinction like this)