This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix findNextTokenSkippingComments & rangeContainsExpansionsOrDirectives
ClosedPublic

Authored by PiotrZSL on Mar 25 2023, 11:42 AM.

Details

Summary

findNextTokenSkippingComments is actually a endless loop,
implementing it correctly.
rangeContainsExpansionsOrDirectives were skiping every second
token, if there were no whitespaces bettwen tokens.

Diff Detail

Event Timeline

PiotrZSL created this revision.Mar 25 2023, 11:42 AM
Herald added a project: Restricted Project. · View Herald Transcript
PiotrZSL requested review of this revision.Mar 25 2023, 11:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 25 2023, 11:42 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

I'm surprised we don't have unit tests that catch this?

clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
84–85

I'm not sure this logic is correct - if (CurrentToken is there to ensure that CurrentToken is not a nullopt before dereferencing via CurrentToken->is. It should be:

if (CurrentToken && !CurrentToken->is(tok::comment))
  return CurrentToken;
99

This is a bit unintuitive if we compare it with e.g. STL iterators - end() is always "one-past" the end. What's the convention for SourceRange, does end contain a valid end range, or one-past-the-end?

PiotrZSL added inline comments.Apr 1 2023, 3:21 AM
clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
84–85

Current code is correct, it's inversion of what we had in while, simply if no token, or token is not comment, then leave.

99

In SourceRange end is pointing to last token, not to last token + 1.
This is inclusive range.

We don't have unit tests for most of code... It's usually tested indirectly by checks... But this code is used I think only in 1-2 checks.

carlosgalvezp accepted this revision.Apr 1 2023, 7:25 AM

It's usually tested indirectly by checks... But this code is used I think only in 1-2 checks.

I see. I'm still trying to understand why we haven't seen this before. If the function is an endless loop, shouldn't the checks that use this function end up in an endless loop too? Would be interesting to try and reproduce an endless loop in the tests of one of the checks that use this. Anyway the patch is fine as is, thanks for fixing!

clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
84–85

You are right, nevermind!

This revision is now accepted and ready to land.Apr 1 2023, 7:25 AM
ccotter added a subscriber: ccotter.Apr 1 2023, 2:34 PM
PiotrZSL marked 2 inline comments as done.Apr 2 2023, 8:44 AM
PiotrZSL marked an inline comment as done.Apr 2 2023, 8:49 AM
This revision was automatically updated to reflect the committed changes.