Skip to content

[clang][Diagnostics] Fix wrong line number display #65238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 5, 2023

Conversation

hazohelet
Copy link
Member

When the caret location is lower than the lowest source range, clang is printing wrong line numbers. The first line number should consider caret location line even when there are source ranges provided.

Current wrong line example: https://godbolt.org/z/aj4qEjzs4

When the caret location is lower than the lowest source range, clang is printing wrong line numbers.
The first line number should consider caret location line even when there are source ranges provided.

Current wrong line example: https://godbolt.org/z/aj4qEjzs4
@hazohelet hazohelet added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Sep 4, 2023
@hazohelet hazohelet requested a review from a team as a code owner September 4, 2023 01:54
@cor3ntin cor3ntin added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Sep 4, 2023
@cor3ntin
Copy link
Contributor

cor3ntin commented Sep 4, 2023

This looks good. My only question is, can Loc ever be invalid there, and what happens if it is?

@tbaederr
Copy link
Contributor

tbaederr commented Sep 4, 2023

In 8554a55, this was added to fix a range with a lower begin source location than Loc is, so I think this change would break that?

@hazohelet
Copy link
Member Author

When the Loc is higher than the lowest begin source location, DisplayLineNo still points to the lowest begin location line because we have

  DisplayLineNo =
        std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin()));

in L1169, correct?
This does not break the test added in the commit at least locally.

I don't think we need to ignore Loc.getPresumedLoc().getLine() when Ranges is not empty.

@cor3ntin
Copy link
Contributor

cor3ntin commented Sep 4, 2023

Ouch the inability to comment outside of modified code is going to be painful.

The changed code being

  unsigned DisplayLineNo = Loc.getPresumedLoc().getLine();
  for (const auto &I : Ranges) {
  DisplayLineNo = 
            std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin()));
  }

If the range starts before DisplayLineNo, it still works. Am I missing something?

@hazohelet
Copy link
Member Author

Ouch the inability to comment outside of modified code is going to be painful.

The changed code being

  unsigned DisplayLineNo = Loc.getPresumedLoc().getLine();
  for (const auto &I : Ranges) {
  DisplayLineNo = 
            std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin()));
  }

If the range starts before DisplayLineNo, it still works. Am I missing something?

Thanks, that's my point.

When I provided an invalid source location, the diagnostics looked like the following. Filename, line/column number and code snippet are not printed.

source:7:3: error: no matching function for call to 'func'
    7 |   func(3, 4, 5);
      |   ^~~~
note: candidate function not viable: expects an lvalue for 3rd argument
1 error generated.

When Loc is invalid, calling Loc.getPresumedLoc().getLine() should cause validness assertion failure in PresumedLoc::getLine, so invalid Loc seems to be handled elsewhere.

@tbaederr
Copy link
Contributor

tbaederr commented Sep 4, 2023

Okay, makes sense to me. Should probably also be backported to 17.

@cor3ntin
Copy link
Contributor

cor3ntin commented Sep 4, 2023

could you upload a dummy change to trigger the build? Thanks!

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@hazohelet hazohelet merged commit ef5217b into llvm:main Sep 5, 2023
@hazohelet hazohelet deleted the line-number branch September 5, 2023 07:10
avillega pushed a commit to avillega/llvm-project that referenced this pull request Sep 11, 2023
When the caret location is lower than the lowest source range, clang is
printing wrong line numbers. The first line number should consider caret
location line even when there are source ranges provided.

Current wrong line example: https://godbolt.org/z/aj4qEjzs4
tru pushed a commit that referenced this pull request Sep 27, 2023
When the caret location is lower than the lowest source range, clang is
printing wrong line numbers. The first line number should consider caret
location line even when there are source ranges provided.

Current wrong line example: https://godbolt.org/z/aj4qEjzs4

(cherry picked from commit ef5217b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants