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

    <tr>
        <th>Summary</th>
        <td>
            Static analyzer HTML output has incorrect links to "note" diagnostics
        </td>
    </tr>

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

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

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

<pre>
    When the clang static analyzer outputs HTML reports, there is a "Bug Summary" section at the top, which has in-page anchor links to the diagnostics in the source file below. When the checker generates "notes" with `BugReport.addNote`, this section has links to every note in the report.

However, these links are off by one. The first link doesn't work, the second link points at the first note, the third link points at the second note, etc.

To reproduce:
```cpp
struct A {
  int *iptr;
  int a;
  int b;
  int *c;
  int *d;

 A (int *iptr) : iptr(iptr) {}
};

void f() {
  A a(0);
}
```
```
clang --analyze --analyzer-output html -Xanalyzer -analyzer-checker=optin.cplusplus.UninitializedObject test-notes.cpp
```

The output HTML has links to `#EndPath`, `#Note0`, `#Note1`, `#Note2`, `#Note3` (generated by `HTMLDiagnostics::FinalizeHTML`), but the notes themselves have `id="Note1"`, `id="Note2"`, `id="Note3"`, `id="Note4"` (generated by `RewriteFile` and `HandlePiece`).

It looks like `FinalizeHTML` generates the links in incremental order from 0, but `RewriteFile` is iterating over the notes in *reverse* order and starts at n (not n-1).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VcmO4zYQ_Rr6UrAgUV4POrjjGBMgy2Cmg-RKUSWp0jQpkCUbPV8fUJKXtnsAL1JtfI-sx1IhUGMRC7F8Ecv9TPXcOl9Y0s6okK1npavei39atMAtgjbKNhBYMWlQVpn3H-jB9dz1HODL6x-_g8fOeQ5C_hIzPAIFUCCkfOkb-N4fj8q_CykhoGZyFhQPldl1MeXckm6hVQHIzjvVICirW-fBkH0LwG4Irkg11gUmHeMGU3C91wg1GY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