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

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] clang-tidy-diff.py reports warnings for files with incorrectly inferred compile commands
        </td>
    </tr>

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

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

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

<pre>
    While the script `run-clang-tidy.py` uses the compilation database to know which files to run checks on,
the script `clang-tidy-diff.py` uses a unified diff to figure out which files and lines to check.

This means that `clang-tidy-diff.py` can invocate `clang-tidy` on files that are not included in the current compilation database.
It will run `clang-tidy` on headerfiles and even on source files used in different build configurations,
e.g. source files only built for certain CMake configurations or for specific platforms.

`clang-tidy` uses the `JSONCompilationDatabasePlugin` to get a known compile commands, but this is nowadays wrapped
by an `InterpolatingCompilationDatabase` class that allows it to infer compile commands for files missing in the compilation database.
This can result in a command that lacks build flags or some needed include paths.


We aim to use `clang-tidy-diff.py` as a gating step in our CI/Gerrit to detect regressions,
but we get a lot of false positives due to compilation errors, like missing include-paths.
We also get warnings when it runs on headerfiles, like unused macros or functions:
```
module/src/Functionality.h:2:9: error: macro is not used [clang-diagnostic-unused-macros]
#define MODULE_SRC_FUNCTIONALITY_H
        ^

other/include/function.h:342:24: warning: unused function 'func' [clang-diagnostic-unused-function]
static inline int func()
                  ^
```

Are we wrongly assuming that the goal of `clang-tidy-diff.py` is to detect regressions in commits as a gate,
or could this behavior be improved?

As alternatives to improve this I can think of:

- Add an option flag to `clang-tidy` to skip reporting on files that is not compilable.
  If a CompileCommand was guessed (seen via [Heuristic](https://github.com/llvm/llvm-project/blob/2e9abc0c714a97b0412de8b09b74735fafa4432b/clang/include/clang/Tooling/CompilationDatabase.h#L63-L66)?) and the diagnostics have `UncompilableErrorOccurred`
  then don't report it.

- Add an option flag to `clang-tidy-diff.py` to filter out files not available in the compilation database.

Any thoughts?

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVtty4jgQ_Rp4UUGBbW4PPGRJssNWZrK1k6mpfUrJsmxrIiSXJUPx93taMhAmydQOBb7I7e7Tp0-3yG1xXH-vlZbM15I50arGs8F80nZmJDQ31cir4jhujlhjnZMu2Am7a5TmXlnDCu55zh0cWPZi7IEdaiVqVsKnozV4YqKW4sUxawbJZjC5HUxurqNdIo0KVZavw3HWGVUqWTB6Qg5LVXWtZLbzV6G4KZhWJgYNAccxVDw-1cqxneSGMuC_iCq4YcrsreBeXlvRUyTcZ0ZOOHAY62EvdFcAozKRn65tpfHv8tSj2gK90jrQ816UWvJCtpfU5F4aWne2a4XsQYChEJPgyxAx75QuENcElkJkdyZdjqvxtQNr9DG841lpWyZk6zn8bT7zF_mTF4bnZOMaKVAQwRokhoWdu-L5TS5n0eDmr6-PXzYXTm57Sv7WXaUMGaN0lQSvQUmm5y_obQcSKBOg9XCHYuILI17wo2OHljeNLCKE_AjGKNzWeNk2loKZ6p24odyau1M1tbYH-PUEQxkw-gZAYCBSt1POwe255B-XOkiPZNVK12lSCxLsPcbImlN7xNqVmleBbGd3UJeUUVdBYKzhvv6J73D8LhlXO8INuj_WNqd-qgIdzHnZEBTIgW22g-T-T9m2MfdCeik84FZA7K4kROwfZF8kDenbkpVcI2hjnfJqD2aKLgyD14zAtW1D-bSCsi7chbRGr9OiVLSLOjjw1sAO9a2hfoBDt7ifuuPstDOhHXZctDaqtTMiNkB6EWf_Dbc7W3RaInXXChzve3uulT-Oa7yV4LfCL-Kni-A9Ss_H9hvM_ohkF4pXxjqvxChCGUUog9ltHz1JC1liRLHPj7ffHu6ev_6zeb7_9mXztH38cvOwffr3-VO0ZP1nMLt7XWULobUA2tOGq1OKAW2aEd4kI5w9c3TZ83IyZYNkQdc4_Qr7yfyM3nmUUqBkNGRx8ix6WQ6S1TXqy-eC_5r4eLzB9ISWDq01FcY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