llvm / llvm/llvm-project

Clang should not choke on `#pragma GCC diagnostics ignored` it doesn't understand.

Open
#219,294 4 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Minimal example (cut down from a much larger program where usage actually makes sense):

```c++
int main() {
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
return 0;
}
```

Compiling with Clang 23.1

```
clang++ test.cc -Werror
```

Results in

```
test.cc:2:32: error: unknown warning group '-Wmaybe-uninitialized', ignored [-Werror,-Wunknown-warning-option]
2 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
| ^
1 error generated.
```

(GCC 16.2 can compile the program just fine, and obeys the directive in situations where it makes sense.)

Clang should not throw an error on this. While it's nice that Clang has support for `#pragma GCC diagnostic` functionality, those lines are intrinsically a directive to GCC. There are going to be warning options which GCC supports which Clang does not. Having Clang look at directives specifically intended for GCC and assuming it _always_ applies to Clang too is rather frustrating when you're trying to simultaneously support multiple compilers. (Which I assume is the goal of reading the `#pragma GCC` directives in the first place.)

Note I'm only speaking of the `#pragma GCC diagnostic ignored` directive. I'm not attempting to say anything about command line options proper. If you're explicitly calling clang++ (or otherwise explicitly referring to Clang), it's reasonable to expect you're only ever going to pass options Clang understands. That's substantially different than situations where GCC-targeted directives (with no Clang references) are "helpfully" being picked up by Clang.

P.S. I would also file a feature request with GCC that they should really support `#pragma GCC_AND_BY_GCC_I_MEAN_GCC_ONLY_AND_NOT_CLANG_TOO`, but I don't think they'll go for it.

Contributor guide

Open the contributing guide

Research direction

Use the minimal test.cc reproducer and run clang++ test.cc -Werror. Trace Clang's handling of #pragma GCC diagnostic ignored for warning groups it does not recognize, then verify the reported example no longer errors under -Werror and add or update a regression test for that behavior.

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
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.