Issue 234921
Summary clangd shows diagnostics in macro expansions that contain suppression comments
Labels new issue
Assignees
Reporter iam28th
    Greetings!

Consider the following small example:

macro.h:
```cpp
#define DEFINE_VAR(var) \
	/* NOLINTNEXTLINE */ \
	static bool var = false;
```

main.cpp:
```
#include "macro.h"

DEFINE_VAR(var);

int main() {
	return static_cast<int>(var);
}
```

.clang-tidy:
```yaml
Checks: '-*,misc-use-anonymous-namespace'
```

clang-tidy binary yields suppressed warning as expected:
```bash
iam28th@VARN4:~/BlueTide/projects/playground/clangd-nolint-in-macro$ clang-tidy -p . --config-file=.clang-tidy main.cpp
Suppressed 1 warnings (1 NOLINT).
```


however, clangd server publishes diagnostics on didOpen:

<details>
  <summary>details</summary>

```bash
clangd --background-index --input-style=delimited <<'EOF'
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"processId":null,"rootUri":"file:///home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro","capabilities":{}}}
---
{"jsonrpc":"2.0","method":"initialized","params":{}}
---
{"method":"textDocument/didOpen","jsonrpc":"2.0","params":{"textDocument":{"uri":"file:///home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro/main.cpp","version":1,"languageId":"cpp","text":"#include \"macro.h\"\n\nDEFINE_VAR(var);\n\nint main() {\n\treturn static_cast<int>(var);\n}\n"}}}
---
{"jsonrpc":"2.0","id":2,"method":"sync","params":null}
---
{"jsonrpc":"2.0","id":3,"method":"shutdown","params":null}
---
{"jsonrpc":"2.0","method":"exit"}
EOF

########
# OUTPUT:
########
clangd is a language server that provides IDE-like features to editors.

It should be used via an editor plugin rather than invoked directly. For more information, see:
	https://clangd.llvm.org/
	https://microsoft.github.io/language-server-protocol/

clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment variable.

I[14:51:52.526] clangd version 23.1.1
I[14:51:52.526] Features: linux
I[14:51:52.526] PID: 239666
I[14:51:52.526] Working directory: /home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro
I[14:51:52.526] argv[0]: clangd
I[14:51:52.526] argv[1]: --background-index
I[14:51:52.526] argv[2]: --input-style=delimited
I[14:51:52.526] Starting LSP over stdin/stdout
I[14:51:52.527] <-- initialize(1)
I[14:51:52.527] --> reply:initialize(1) 0 ms
Content-Length: 1974

{"id":1,"jsonrpc":"2.0","result":{"capabilities":{"astProvider":true,"callHierarchyProvider":true,"clangdInlayHintsProvider":true,"codeActionProvider":true,"compilationDatabase":{"automaticReload":true},"completionProvider":{"resolveProvider":false,"triggerCharacters":[".","<",">",":","\"","/","*"]},"declarationProvider":true,"definitionProvider":true,"documentFormattingProvider":true,"documentHighlightProvider":true,"documentLinkProvider":{"resolveProvider":false},"documentOnTypeFormattingProvider":{"firstTriggerCharacter":"\n","moreTriggerCharacter":[]},"documentRangeFormattingProvider":{"rangesSupport":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":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 23.1.1 linux x86_64-pc-linux-gnu"}}}I[14:51:52.527] <-- initialized
I[14:51:52.527] <-- textDocument/didOpen
I[14:51:52.527] <-- sync(2)
I[14:51:52.528] Loading config file at /home/iam28th/.config/clangd/config.yaml
I[14:51:52.528] --> textDocument/publishDiagnostics
Content-Length: 144

{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[],"uri":"file:///home/iam28th/.config/clangd/config.yaml"}}I[14:51:52.528] Loaded compilation database from /home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro/compile_commands.json
I[14:51:52.528] ASTWorker building file /home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro/main.cpp version 1 with command
[/home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro]
/usr/bin/g++ --driver-mode=g++ -o main -std=c++20 -resource-dir=/usr/lib/clang/23 -- /home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro/main.cpp
I[14:51:52.528] Enqueueing 1 commands for indexing
I[14:51:52.539] Built preamble of size 252352 for file /home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro/main.cpp version 1 in 0.01 seconds
I[14:51:52.541] Indexing c++20 standard library in the context of /home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro/main.cpp
I[14:51:52.549] --> textDocument/publishDiagnostics
Content-Length: 532

{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[{"code":"misc-use-anonymous-namespace","codeDescription":{"href":"https://clang.llvm.org/extra/clang-tidy/checks/misc/use-anonymous-namespace.html"},"message":"Variable 'var' declared 'static', move to anonymous namespace instead","range":{"end":{"character":14,"line":2},"start":{"character":11,"line":2}},"severity":2,"source":"clang-tidy"}],"uri":"file:///home/iam28th/BlueTide/projects/playground/clangd-nolint-in-macro/main.cpp","version":1}}I[14:51:56.096] Indexed c++20 standard library (incomplete due to errors): 16248 symbols, 11770 filtered
I[14:51:56.147] --> reply:sync(2) 3619 ms
Content-Length: 38

{"id":2,"jsonrpc":"2.0","result":null}I[14:51:56.147] <-- shutdown(3)
I[14:51:56.147] --> reply:shutdown(3) 0 ms
Content-Length: 38

{"id":3,"jsonrpc":"2.0","result":null}I[14:51:56.148] <-- exit
I[14:51:56.148] LSP finished, exiting with status 0
```

</details>


I found an issue related to clangd and nolint in macros in the past, but that one was about system headers specifically:
https://github.com/clangd/clangd/issues/1448

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to