This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Ignore implicit code in bugprone-branch-clone
ClosedPublic

Authored by PiotrZSL on May 22 2023, 11:58 AM.

Details

Summary

Implicit code like, template instances, compiler generated
code are not excluded in this check by using
TK_IgnoreUnlessSpelledInSource.

Fixes #62693

Diff Detail

Event Timeline

PiotrZSL created this revision.May 22 2023, 11:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2023, 11:58 AM
Herald added a subscriber: xazax.hun. · View Herald Transcript
PiotrZSL requested review of this revision.May 22 2023, 11:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2023, 11:58 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
donat.nagy added a comment.EditedMay 23 2023, 5:07 AM

For me it's an unfortunate surprise that Clang Tidy is only seeing the result of the template instantiation process where the type variables are substituted (I would've guessed that it's working on the original template body), but if that's the observed behavior, then this commit is a clean and straightforward fix.

I'd suggest adding another testcase that looks like

template <typename T>
int branch_clone_in_template(T t) {
  if (t) {
    return 42;
  } else {
    return 42;
  }
}
int use_template(int x) {
  return branch_clone_in_template<int>(x);
}

to clarify the behavior on cases where the checker encounters a "real" branch clone that happens to be in a template. I think we can/should merge your change even if it suppresses these true positives (as it's a self-contained change that replaces a serious issue with a minor limitation), but in that case we should mark this testcase with a FIXME comment to highlight this low-priority followup work. (Of course it's even better if you happen to have a quick idea which ensures that the checker can report "real" branch clones even if they're in templates.)

PiotrZSL updated this revision to Diff 524764.May 23 2023, 9:29 AM

Add positive test

donat.nagy accepted this revision.May 23 2023, 10:44 AM

If this freshly added testcase passes, then there is no reason to delay merging this commit. Thanks for the contribution :) !

This revision is now accepted and ready to land.May 23 2023, 10:44 AM