This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix `llvmlibc-inline-function-decl` false positives for templated function definitions
ClosedPublic

Authored by AMS21 on Jun 17 2023, 11:24 PM.

Details

Summary

For a declaration the FunctionDecl begin location does not include the
template parameter lists, but for some reason if you have a separate
definitions to the declaration the begin location does include them.
With this patch we now correctly handle that case.

This fixes llvm#62746

Diff Detail

Event Timeline

AMS21 created this revision.Jun 17 2023, 11:24 PM
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: xazax.hun. · View Herald Transcript
AMS21 requested review of this revision.Jun 17 2023, 11:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2023, 11:24 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
PiotrZSL requested changes to this revision.Jun 18 2023, 12:54 AM

I got one concern, if you write this:

template <typename... Ts>LIBC_INLINE void VariadicTemplate<Ts...>::goodVariadicTemplate() {}
template <typename... Ts>inline void VariadicTemplate<Ts...>::badVariadicTemplate() {}

Aka, no space after >, warning will be emitted for both.
That's because of TemplateParams->getRAngleLoc().getLocWithOffset(1);
So we find > character, then we skip it, now we pointing into inline or whitespace location, and when we skip to a next token.
If you remove this getLocWithOffset(1) then findNextTokenSkippingComments should work correctly.

Additionally you missing release notes.
There is also other way to deal with this issue, simply use FunctionDecl::getReturnTypeSourceRange() and work with tidy::utils::lexer::getPreviousToken.
But in theory both solutions should work fine. Question is also how it will work with attributes, like [[nodiscard]] that could be put before inline.

Requesting change mainly due to false-positive with space.

This revision now requires changes to proceed.Jun 18 2023, 12:54 AM
AMS21 updated this revision to Diff 532455.Jun 18 2023, 2:10 AM

Implemented suggested fix and added a test case
Added entry to ReleaseNotes

AMS21 added a comment.Jun 18 2023, 2:13 AM

Regarding the problems with attributes like nodiscard. I'm honestly not quite sure, but from the way the check is implemented and the warning is worded, it sounds to me like the attributes should come after LIBC_INLINE. Although looking at the Styleguide for libc, I didn't find anything the placement of the macro there.

I thinks its best for now to leave the behavior as it was.

This revision is now accepted and ready to land.Jun 18 2023, 3:11 AM
AMS21 added a comment.Jun 18 2023, 3:43 AM

If there are no more problems, I would kindly as for someone to push this on my behalf :)