<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJzVWU1z2zgS_TXyBUUVRX1ZBx9iO67xljNJJZns0QWSoIQxSHAB0LLm189rAJRoWY6T2pNTtkWRBNDofv36NZLrcndx29hWGlGyfMdG88vRInUbadk3x4uHz4_CVEpv2f86YZ3UDZ6O5tej7HzjXGtH0w-j7AY_ll7W8eVxoWvc64dYXC_T6SpdLaa43G64S6RNeKK0bpOtdJuEN4ksk1K0oilF45Ics225KZPc8KbYjLLVeJRej9IP8W82hXmitcxpZkRrdNkVIjybjNkV1pSlMMxtBKu0gkmyWbNCl4IMDlNgH_6nGGWX9OPvPmpZYoTG_mTj2NMou2J0sYMFbLSMbzH8e2Kj6TXuTwf3thupBMNYenh1YtBoeR1X31_0ZoSv2Zixr13DyC7Fm3XiZLlLJlOWJMVGFA9w-DVXThj-U38xPi7adj9r-Hs7ypY1M13TkDN0w_7Ku8Z1LEvH6Ww8ZXffvx28c8q4-P7d3Y9PDJG2CC2bTMfpeDIcxNjn1sla_kOA6qQqx_39a1HxTjnmuFkLh7XY0_nifjFL2iJRsumeknXT9S__oa1jV1_-otc23G6FUieNOgDi41MrCodVdecAwAiH7xthBOP4bTSDi2jzlsIqngrROlbzXU4xyzzmCZEMn43ABvGKKDrMiKcv4PdVcPV8pSPDPhqjTYSEMzvyOcCqNC8ZBxTrVipO2cFK7njO7QGaV7pTJcx1jHdOI8oO-zo5BFA1rAqgy0LMsyxM8qd-bUTXlMA0RtxsyPbsZidarNNYhsl4s2MtvAXMl-CEwmmzCxNW8kmUyWDOZGA4G-5WA5W0XT9iuD0Go2wHeHqT8f7REn9bzPo7K_zn2-c_f2eBrxH8xDmIHasUX9sY2kmPDrYWDRIMce-D_sJTuAzenn6Y4neS0dpxOF326chiOoISguGEL2IFQOz2-pC_rOwEoeORG8lzvGdEBdg2hb-LvAURLRGbkuDKCt4hkC24Vpua00ulWBtehkiDwX-BIYjDf423Xs23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>
        <th>Summary</th>
        <td>
            `altera-id-dependent-backward-branch` mass false positive: all variables assigned/initialized with a variable are considered id-dependent
        </td>
    </tr>

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

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

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

<pre>
    Inspired by [`this StackOverflow question`](https://stackoverflow.com/questions/70390963/what-is-a-loop-with-an-id-dependent-backward-branch).

# Steps to reproduce
1. Consider the following code:
```c++
void foo(int x, int y) {
    x = y;
    while (x < y) {
    }
}
```
2.  Run `clang-tidy-13 --checks=altera-id-dependent-backward-branch a.cpp`

I'm running on Ubuntu 20.04.3 LTS:

```
Ubuntu LLVM version 13.0.1

  Optimized build.
  Default target: x86_64-pc-linux-gnu
  Host CPU: haswell
```

# Expected outcome
There are no warnings, except maybe "this loop is never executed".

# Real outcome
```
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "a.cpp"
No compilation database found in /home/yeputons or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 warning generated.
/home/yeputons/a.cpp:3:12: warning: backward branch (while loop) is ID-dependent due to variable reference to 'x' and may cause performance degradation [altera-id-dependent-backward-branch]
    while (x < y) {
```

# Comments
This look completely unrelated to the [`altera-id-dependent-backward-branch` check's docs](https://clang.llvm.org/extra/clang-tidy/checks/altera-id-dependent-backward-branch.html): there is no `get_local_id` or anything like this in sight.

If I remove the `x = y` assignment, the warning goes away.

There is also no note on why exactly `x` is considered id-dependent, while [there should be one](https://github.com/llvm/llvm-project/blob/1ba99eaf70951a4aa062688577047d8a23bfe54c/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp#L246-L251). Maybe I need some extra flags or it's a separate issue?

# Probable cause
I'm not familiar with LLVM's source code, but it seems to me that the bug is in [`saveIdDepVarFromReference`](https://github.com/llvm/llvm-project/blob/1ba99eaf70951a4aa062688577047d8a23bfe54c/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp#L132-L156) at the very least: it saves _all_ potentially id-dependent variables in `IdDepVarsMap` regardless of whether the right-hand side of the assignment/initialization is id-dependent itself. Looks like some early returns are missing. Similar bug probably happens in `saveIdDepFieldFromReference` as well.

More high-level picture is as follows:

1. The warning is issued [here](https://github.com/llvm/llvm-project/blob/1ba99eaf70951a4aa062688577047d8a23bfe54c/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp#L248-L251) iff `IdDepVar` is found in the `IdDepVarsMap` map.
2. Variable `x` is marked as id-dependent by one of `IdDependentBackwardBranchCheck::saveIdDep*` methods from [here](https://github.com/llvm/llvm-project/blob/1ba99eaf70951a4aa062688577047d8a23bfe54c/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp#L115) (they're the only place modifying `IdDepVarsMap`).
3. They are called from [this bunch of `if`s](https://github.com/llvm/llvm-project/blob/1ba99eaf70951a4aa062688577047d8a23bfe54c/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp#L211-L224). In particular, if there is a `PotentialVar` (`pot_tid_var` match) and `RefExpr` (`assign_ref_var` match), `saveIdDepVarFromReference` is called.
4. `pot_tid_var` matches either [a variable initialized from a variable or a field](https://github.com/llvm/llvm-project/blob/1ba99eaf70951a4aa062688577047d8a23bfe54c/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp#L49) (because the latter may be id-dependent) or [a similar assignment](https://github.com/llvm/llvm-project/blob/1ba99eaf70951a4aa062688577047d8a23bfe54c/clang-tools-extra/clang-tidy/altera/IdDependentBackwardBranchCheck.cpp#L56-L59). In both cases, either `assign_ref_var` or `assign_ref_field` is matches as well via `RefVarOrField` depending on whether the initializer is a variable or a field.
5. Hence, `saveIdDepVarFromReference` is called on the `x = y` assignment as well with `assign_ref_var = y` and `pot_tid_var = x`. 
6. While `saveIdDepVarFromReference` checks that the right hand side is in `IdDepFieldsMap`, it does not do anything to with knowledge and assumes that `PotentialVar` is always id-dependent.

Btw, [here is a code review](https://reviews.llvm.org/D70094/new/)

# Suggested fix
1. Early return from `saveIdDepVarFromReference` if `RefVar`/`RefField` (there should be only one, an assertion would be nice to have) is not found in `IdDepVarsMap`.
2. Rename both `saveIdDepVarFromReference` and `saveIdDepFieldFromReference` to something like `potentiallySaveIdDepVarFromReference` to highlight that it performs some checks itself. It's easy to assume that the variable is already established to be id-dependent while reviewing `saveIdDepVarFromReference`.
3. Rename `PotentialVar` and `pot_tid_var` to something like `PossiblyIdDependentVar`/`possibly_iddep_var` to highlight that it's not necessary id-dependent even more. `PotentialVar` is more open to interpretation and the erroneous `saveIdDepVarFromReference(RefExpr, MemExpr, PotentialVar);` line does not immediately stand out as "save only a possible id dependent variable as really id-dependent". However, I'm not sure if that would really help, as the line is already inside some if-statement which looks somewhat reasonable.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVWU1z2zgS_TXyBUUWRX0ffIjtqOItZ5JKMtmjCyRBCWOQ4AKgZc2v39cAKdGyHCdHp2yLIgmg0f369Wsk08X-8ra2jTSiYNmejWZXo3nittKy747nD18ehSmV3rH_tcI6qWs8Hc1uRuly61xjR5MPo3SNH0sv6-7lONcV7vVDLK4XyWSVrOYTXO623EXSRjxSWjfRTrptxOtIFlEhGlEXonZRhtl23BRRZnidb0fpKh4lN6PkQ_c3ncA80VjmNDOiMbpocxGejWN2jTVlIQxzW8FKrWCSrDcs14Ugg8MU2If_yUfpFf34u49aFhihsT9ZO_Y0Sq8ZXexhARsturcY_j2x0eQG9yeDe7utVIJhLD28PjNotLjpVj9c9GaEr2nM2Le2ZmSX4vUmcrLYR-MJi6J8K_IHOPyGKycM_6W_GI_zpjnMGv7ejtJFxUxb1-QMXbO_s7Z2LUuTOJnGE3b34_vRO-eM696_u_v5mSHSFqFl40mcxOPhIMa-NE5W8l8CVCtVEff3b0TJW-WY42YjHNZiT8v5_XwaNXmkZ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