llvm / llvm/llvm-project

clang-cl behaves different on attributes than MSVC

Open
#167,835 2 comments 0 reactions 0 assignees View on GitHub
clang:frontend confirmed diverges-from:msvc extension:microsoft
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

When using the attribute `[[nodiscard]]` after `__declspec(dllexport)`, the compiler complains:
```
(5,25): error: 'nodiscard' attribute cannot be applied to types
```
When swapping the `[[nodiscard]]` and `__declspec(dllexport)`, the compilation succeeds.

This behavior is different from MSVC, as MSVC does not care about the order of the arguments.

Tested with

- clang-cl 20.1.8 on Ubuntu
- clang-cl 18.1.0 [Godbolt](https://godbolt.org/z/4cYYW938r)
- MSVC v19.20 [Godbolt](https://godbolt.org/z/ra8h57Eqa)

Complete example:

```
enum bar {
bar1 = 0
};

__declspec(dllexport) [[nodiscard]] bar foo() {
return bar::bar1;
}

int main() {
foo();
return 0;
}
```

Contributor guide

Open the contributing guide

Research direction

Start by compiling the complete example with clang-cl using both attribute orders, then compare the diagnostics with MSVC and the linked Godbolt examples. Trace clang-cl's handling of __declspec(dllexport) and [[nodiscard]] placement; done means the accepted behavior matches MSVC for either order without misapplying nodiscard to a type.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.