This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy]: fix false positive of cert-oop54-cpp check.
ClosedPublic

Authored by ztamas on Mar 28 2020, 8:12 AM.

Details

Summary

It seems we need a different matcher for binary operator
in a template context.

Fixes this issue:
https://bugs.llvm.org/show_bug.cgi?id=44499

Diff Detail

Event Timeline

ztamas created this revision.Mar 28 2020, 8:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2020, 8:12 AM
ztamas edited the summary of this revision. (Show Details)Mar 28 2020, 8:14 AM
ztamas added a reviewer: aaron.ballman.
Eugene.Zelenko added a project: Restricted Project.
ztamas updated this revision to Diff 253354.Mar 28 2020, 9:35 AM

Run clang-format on test code.

I'm not entirely sure this is where the fix needs to be for this. The test case code is whacky as hell, but from what I can see clang should always emit a BinaryOperator for dependent type operators. No idea why it is spewing out a CXXOperatorCallExpr instead. Would need someone with more knowledge on Sema to confirm(or deny) this though.

clang-tools-extra/test/clang-tidy/checkers/bugprone-unhandled-self-assignment.cpp
531

clang format artefact? This change should be undone as its unrelated to the patch.

ztamas updated this revision to Diff 253412.Mar 29 2020, 5:25 AM

Rebase, TODO comment, remove unrelated change.

ztamas marked an inline comment as done.Mar 29 2020, 5:26 AM

I'm not entirely sure this is where the fix needs to be for this. The test case code is whacky as hell, but from what I can see clang should always emit a BinaryOperator for dependent type operators. No idea why it is spewing out a CXXOperatorCallExpr instead. Would need someone with more knowledge on Sema to confirm(or deny) this though.

I agree, it seems suspicious that a BinaryOperator matcher does not work in this case. However, I'm working on this level of the code, I'm looking at the matchers like an API, what I'm just using in the clang-tidy code, without changing them. So that's why I added this workaround. I don't know how much time it would take to fix this issue in the matcher code or whether it will be fixed in the near future or not, but until then I think it useful to have this workaround in the caller code, so this clang-tidy check works better. I added a TODO comment, so it's more visible that we have an issue here, so it's more probable somebody will fix that working with the matchers.

I agree, it seems suspicious that a BinaryOperator matcher does not work in this case. However, I'm working on this level of the code, I'm looking at the matchers like an API, what I'm just using in the clang-tidy code, without changing them. So that's why I added this workaround. I don't know how much time it would take to fix this issue in the matcher code or whether it will be fixed in the near future or not, but until then I think it useful to have this workaround in the caller code, so this clang-tidy check works better. I added a TODO comment, so it's more visible that we have an issue here, so it's more probable somebody will fix that working with the matchers.

It's not the matchers fault, that's working as intended. its the AST that clang has generated for that source code that looks suspicious.

ztamas updated this revision to Diff 253438.Mar 29 2020, 10:59 AM

Remove false TODO comment.

aaron.ballman accepted this revision.Apr 3 2020, 9:28 AM

LGTM with a testing request. I agree that the Clang AST is a bit surprising, but not so surprising that I could definitely call it a bug.

clang-tools-extra/test/clang-tidy/checkers/bugprone-unhandled-self-assignment.cpp
301–314

Thank you for this! Can you also add a test case where the diagnostic should trigger? e.g.,

class Foo;
template <int a>
bool operator!=(Foo &, Foo &) {
  class Bar {
    Bar &operator=(const Bar &other) {
      p = other.p;
      return *this;
    }

    int *p;
  };
}
This revision is now accepted and ready to land.Apr 3 2020, 9:28 AM
ztamas updated this revision to Diff 255038.Apr 4 2020, 8:15 AM

Add suggested test case and rebase.

ztamas marked an inline comment as done.Apr 4 2020, 8:15 AM
This revision was automatically updated to reflect the committed changes.