This is an archive of the discontinued LLVM Phabricator instance.

[clang][Diagnostics] Fix distant source ranges in bad-conversion notes
ClosedPublic

Authored by hazohelet on Jun 20 2023, 8:14 AM.

Details

Summary

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);
      |      ^            ~~~~~~

Diff Detail

Event Timeline

hazohelet created this revision.Jun 20 2023, 8:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 8:14 AM
hazohelet requested review of this revision.Jun 20 2023, 8:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 8:14 AM

Can you post a before/after comparison of the exact output for that example?

clang/lib/Sema/SemaOverload.cpp
10752

Can this be const?

10752

Actually, ToPVD is only used to access its source range later, isn't it? If so, can we just declare the a ToPVDRange or something here and use that later for diagnostics?

Address comments from @tbaeder

  • Bind parameter source range rather than its declaration to variable.
hazohelet marked 2 inline comments as done.Jun 20 2023, 11:29 PM

Consider the following code. (I added another parameter to the original code so that the covered range becomes clearer)

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"); }
      |                       ~~~~~

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);
      |      ^            ~~~~~~
clang/lib/Sema/SemaOverload.cpp
10752

Right. I fixed it.

cjdb added a comment.Jun 21 2023, 11:17 AM

I like this patch, thanks for working on it 😄

Consider the following code. (I added another parameter to the original code so that the covered range becomes clearer)

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"); }
      |                       ~~~~~

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);
      |      ^            ~~~~~~

Having this in the commit message would be great, thanks!

clang/lib/Sema/SemaOverload.cpp
10823

Please don't commit until this is resolved (either tests added or it's removed).

hazohelet marked an inline comment as done.Jun 24 2023, 12:26 AM
hazohelet added inline comments.
clang/lib/Sema/SemaOverload.cpp
10823

I opened another diff to remove this block: D153690

hazohelet updated this revision to Diff 535433.Jun 28 2023, 8:52 AM
hazohelet marked an inline comment as done.
hazohelet edited the summary of this revision. (Show Details)

Removed FIXME comment that is already addressed in D153690

cjdb added a comment.Jun 28 2023, 9:52 AM

Per this Discourse post, I think it'd be best for the example I asked to be in your commit message to actually be in your release notes. Sorry for the churn!

hazohelet updated this revision to Diff 535801.Jun 29 2023, 7:50 AM
hazohelet edited the summary of this revision. (Show Details)

Address comment from cjdb

  • Added example code & before/after diagnostic display to release note

Thanks for reminding!

@cjdb I know this is kind of silly after @hazohelet has already added it to the release notes... but it seems like showing the difference is useless since the difference was only introduced during the clang 17 development.(?) For a 16 -> 17 transition, it probably causes almost no change.

cjdb added a comment.Jun 30 2023, 10:28 AM

@cjdb I know this is kind of silly after @hazohelet has already added it to the release notes... but it seems like showing the difference is useless since the difference was only introduced during the clang 17 development.(?) For a 16 -> 17 transition, it probably causes almost no change.

Ah, fair enough then.

hazohelet updated this revision to Diff 536743.Jul 3 2023, 6:22 AM
hazohelet edited the summary of this revision. (Show Details)

Edited release note so that it only mentions the clang 16 -> 17 changes, and not development internal ones

cjdb accepted this revision.Jul 7 2023, 5:41 PM

Yep! Thanks for working on this :-)

This revision is now accepted and ready to land.Jul 7 2023, 5:41 PM
This revision was landed with ongoing or failed builds.Jul 14 2023, 7:00 AM
This revision was automatically updated to reflect the committed changes.