This is an archive of the discontinued LLVM Phabricator instance.

[clang][Sema] Provide source range to several Wunused warnings
ClosedPublic

Authored by hazohelet on Jun 12 2023, 6:12 AM.

Details

Summary

When the diagnosed function/variable is a template specialization, the source range covers the specialization arguments.
e.g.

warning: unused function 'func<int>' [-Wunused-function]
template <> int func<int> () {}
                ^~~~~~~~~

This comes in line with the printed text in the warning message. In the above case, func<int>

Diff Detail

Event Timeline

hazohelet created this revision.Jun 12 2023, 6:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 12 2023, 6:12 AM
hazohelet requested review of this revision.Jun 12 2023, 6:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 12 2023, 6:12 AM
tbaeder added inline comments.Jun 12 2023, 7:52 AM
clang/test/Misc/diag-unused-source-ranges.cpp
2

Should be %clang_cc1 I think.

Precommit-CI also found some problems on Windows

tbaeder added inline comments.Jun 12 2023, 8:05 AM
clang/test/Misc/diag-unused-source-ranges.h
1 ↗(On Diff #530481)

Moving this file to Inputs/ would make sense I think.

Precommit-CI also found some problems on Windows

It's a littile surprising to me that clang outputs different diagnostics on windows. I suspect it might be related to the clang driver, so I'll try %clang_cc1 as you suggested, and if it still fails on windows, I'll split the test into separate files to have completely same inputs as already-working tests in test/Sema

hazohelet updated this revision to Diff 530834.Jun 13 2023, 2:28 AM
  • Change %clang to %clang_cc1 -fcxx-exceptions in the test
aaron.ballman added inline comments.Jun 13 2023, 4:46 AM
clang/lib/Sema/Sema.cpp
1350

Does DiagD->getSourceRange() not give you the same results?

1384
hazohelet added inline comments.Jun 13 2023, 5:08 AM
clang/lib/Sema/Sema.cpp
1350

FunctionDecl::getSourceRange also covers the return type and the function body if it exists, so we cannot use it here.

aaron.ballman accepted this revision.Jun 13 2023, 5:14 AM

LG aside from the few suggested changes and moving the header to the Inputs directory.

clang/lib/Sema/Sema.cpp
1350

Ah, okay, that makes sense. Thank you!

<not your problem>
I think we should consider adding SourceRange accessors for various interesting ranges like this so that we don't need to use ad hoc solutions in various places.
</not your problem>

clang/test/Misc/diag-unused-source-ranges.h
1 ↗(On Diff #530481)

+1

This revision is now accepted and ready to land.Jun 13 2023, 5:14 AM
hazohelet updated this revision to Diff 530912.Jun 13 2023, 8:05 AM

Address review comments

  • Move the test header to Inputs folder
  • Remove explicit constructor call of SourceRange
hazohelet marked 3 inline comments as done.Jun 13 2023, 8:07 AM

Thanks for the review.
I'll check the pre-commit CI result before landing this just in case.

hazohelet added inline comments.Jun 14 2023, 4:40 AM
clang/lib/Sema/Sema.cpp
1386

It looks like VarTemplateSpecializationDecl::getTemplateArgsInfo() could be null, so I'll add another check for that to avoid regression.

(This seems weird to me because IIRC there's no template type deduction for variable templates and thus every specialization for variable templates has explicit template arguments provided)

hazohelet updated this revision to Diff 531266.Jun 14 2023, 5:04 AM

Added an additional null check that I mentioned earlier