<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/140118>140118</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `-Wunused-const-variable` warning is not emitted under `-Werror` if another earlier warning causes compilation to abort
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          vsz
      </td>
    </tr>
</table>

<pre>
    # Summary
This is the case for other warnings as well, such as: `-Wunused-template`, `-Wunneeded-member-function`, `-Wunused-local-typedef`, `-Wunused-const-variable`, `-Wunused-function`, `-Wunused-member-function`, `-Wundefined-internal`, `-Wunused-variable`, `-Wundefined-func-template` and possibly more.

When compiling with Clang using -Werror, the -Wunused-const-variable warning is not emitted if another warning (e.g., -Wunused-variable) occurs earlier in the translation unit and is promoted to an error. This results in inconsistent warning behavior depending on whether -Werror is enabled.

# Steps to Reproduce
### test.cpp
```cpp
const int x = 42;  // Line 1

void Fcn() {
  int local = 24;  // Line 4
}
```

## Case A – Expected Behavior:
```
clang++ test.cpp -std=c++17 -Wunused-variable -Wunused-const-variable
```
Output:
```
test.cpp:4:7: warning: unused variable 'local' [-Wunused-variable]
test.cpp:1:11: warning: unused variable 'x' [-Wunused-const-variable]
```
## Case B – -Werror suppresses second warning:
```
clang++ test.cpp -std=c++17 -Wunused-variable -Wunused-const-variable -Werror
```
Output:
```
test.cpp:4:7: error: unused variable 'local' [-Wunused-variable]
```

## Case C – Only const variable exists:
### test.cpp
```cpp
const int x = 42;  // Line 1
```
```
clang++ test.cpp -std=c++17 -Wunused-const-variable -Werror
```
Output:
```
test.cpp:1:11: error: unused variable 'x' [-Wunused-const-variable]
```
## Expected Behavior

All enabled warnings should be emitted consistently, regardless of whether `-Werror` is present. 

## Root cause

Clang defers some diagnostics until the end of the translation unit (`Sema::ActionEndOfTranslationUnit()`). It does not process `unused` warnings if there was an error as is outlined [here](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/Sema.cpp#L1483).



</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0Vltr4zgU_jXKyyHBkePYefCD005gYWBgZpZ5lq3jWIssGR05bfbXL5Jza9N22TILKWkkndt3vnMRRGpvEEuWbVn2OBOj76wrD_T3rLbyWDKewo-x74U7sqT62SkCReA7hEYQQmsdWN-hgyfhjDJ7AkHwhFoz_gA0Nh0IYmkFbJ3Mf41mJJRzj_2ghUe2TsKr05VBlCjnPfY1unk7msYra16-ieLaNkLP_XFAie0b94015OcH4ZSoNb7x4APdH1qX2CqDcq6MR2eEfkP-batnwaD2NnoQRsJgiVStj9BbhwuWVCypfnVooLH9oLQye3hSvoMHLcweRgoH81_onHXBREjFO6GfcxIyZqwH7JX3KEG1IMyLrAHjBS72i6DwPha-Ads0oyNA4bRCB8pEu94JQ1oEpGA0ysd4FMHgbG-DJW9BGIi-LiCSxyGN2lPQoExwV5FH4y-O1NiJg7IOJA5oZDiyBp46jO6e4g420ATf5AmwSFOPAwWT33FwVo4NThfTBzySXzTDEA7XyfSZfkbUQBkPz8DSR1hxlm4BGN8xvoOvyiAsJzMHqyTsGsN4EVBh-ZYlFUTRyMoozld34qsgnj_emr74HXx7CLVUAfvCWZGwTQpfngdsAoLbEx4srV5JN4EPjG8Z315igzl5ydLHZjpf5vfZfI8sr7R_G_0w-nurFxTTasXSKg-1fcpd-HdSDRdjjOcRGMZzYNn2nlvZ40uly_C3_Hetz681voomuwP7BuntDdJnStE4DA6JkICwsUbeOPD_In-p5s9nYJL_NP4fkPLhBqpvRh9hKpaLAXxW5Onk5W-stVuPPg_97wL6ysqPkP4sJ--LPaah0vrc5a7TlTo7agk1Xrr5tYnqY-jfDvfCSY1EYNtL54yTaHJ-nUw9GgmNX8CLnH-31kMjRsLpeBo6Elt0BGR7BKnE3ljyqiEYjVc6TgI0Mlh7cyiEZrlOfmAvAsZpVcXR-sXIb-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