[clang][Diagnostics] Fix distant source ranges in bad-conversion notes
authorTakuya Shimizu <shimizu2486@gmail.com>
Fri, 14 Jul 2023 13:59:41 +0000 (22:59 +0900)
committerTakuya Shimizu <shimizu2486@gmail.com>
Fri, 14 Jul 2023 13:59:41 +0000 (22:59 +0900)
commit176981ac58d3e4beb02b9d110524e72be509375d
treefbbc60d4340ae9155fcfb579ef4562ba6f2dfe44
parent6e55370b81aff8943f4de183699f18d9d7972174
[clang][Diagnostics] Fix distant source ranges in bad-conversion notes

Now that clang supports printing of multiple lines of code snippet in diagnostics, source ranges in diagnostics that are located in different lines from the diagnosed source location get to be printed if the gap happened to be less than the maximum number of lines clang is allowed to print in.
Many of the bad-conversion notes in overload resolution failures have their source location in the function declaration and source range in the argument of function call. This can cause an unnecessarily many lines of snippet printing.
This patch fixes it by changing the source range from function callsite to the problematic parameter in function declaration.

e.g.

```
void func(int aa, int bb);

void test() { func(1, "two"); }
```
BEFORE this patch:

```
source:4:15: error: no matching function for call to 'func'
    4 | void test() { func(1, "two"); }
      |               ^~~~
source:1:6: note: candidate function not viable: no known conversion from 'const char[4]' to 'int' for 2nd argument
    1 | void func(int aa, int bb);
      |      ^
    2 |
    3 |
    4 | void test() { func(1, "two"); }
      |                       ~~~~~
1 error generated.
```
AFTER this patch:

```
source:4:15: error: no matching function for call to 'func'
    4 | void test() { func(1, "two"); }
      |               ^~~~
source:1:6: note: candidate function not viable: no known conversion from 'const char[4]' to 'int' for 2nd argument
    1 | void func(int aa, int bb);
      |      ^            ~~~~~~
```

Reviewed By: cjdb
Differential Revision: https://reviews.llvm.org/D153359
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaOverload.cpp
clang/test/Misc/diag-overload-cand-ranges.cpp [new file with mode: 0644]
clang/test/Misc/diag-overload-cand-ranges.mm [new file with mode: 0644]