This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix confusable identifiers interaction with DeclContext
ClosedPublic

Authored by serge-sans-paille on Jun 28 2022, 1:37 AM.

Details

Summary

Properly checks enclosing DeclContext, and add the related test case.

Fix #56221

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2022, 1:37 AM
serge-sans-paille requested review of this revision.Jun 28 2022, 1:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 28 2022, 1:37 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman accepted this revision.Jun 28 2022, 8:53 AM

LGTM, but I have some additional testing requests which might spawn follow-up patches.

clang-tools-extra/test/clang-tidy/checkers/misc/confusable-identifiers.cpp
42

It looks like there's quite a bit of missing test coverage for all the various situations in C++. Here are some additional test cases to add:

template <typename i1>
struct S {
  template <typename il> // Should warn on this one, right?
  void func2() {}
};

template <typename i1>
void func(int il) { // Should warn on this one, right?
}

template <typename O0>
void other() {
  int OO = 0; // Should warn on this one, right?
}
int OO = 0; // But not this one

namespace foo {
  int i1;
}

namespace bar {
  int il;  // Should not warn about this
}

namespace foo {
  int il; // But should warn about this
}

struct Base {
  virtual void O0();

private:
  void II();
};

struct Derived : Base {
  void OO(); // We should warn about this
  void I1(); // But not this
};

(I'm sure there are more that I've missed, but you get the idea.) If any of these point out other issues, feel free to address them in a follow-up patch if you'd prefer.

This revision is now accepted and ready to land.Jun 28 2022, 8:53 AM
clang-tools-extra/test/clang-tidy/checkers/misc/confusable-identifiers.cpp
42

Another case to consider:

template <typename O0, typename OO>
void f();

@aaron.ballman : I agree with most of your suggestion except the one below that I annotated accordingly

struct Base {
  virtual void O0();

private:
  void II();
};

struct Derived : Base {
  void OO(); // We should warn about this
  void I1();   // I think we should warn about this too, because implementation of, say, `O0` could refer to `I1` wich would be confusable with `Il`
};

void I1(); // we should probably warn here too, becaus ein the implementation of, says, `O0`, if we refer to `Il`, it's confusable with this `I1` too

@aaron.ballman : I agree with most of your suggestion except the one below that I annotated accordingly

struct Base {
  virtual void O0();

private:
  void II();
};

struct Derived : Base {
  void OO(); // We should warn about this
  void I1();   // I think we should warn about this too, because implementation of, say, `O0` could refer to `I1` wich would be confusable with `Il`
};

void I1(); // we should probably warn here too, becaus ein the implementation of, says, `O0`, if we refer to `Il`, it's confusable with this `I1` too

I don't think we should diagnose Derived::I1() because Base::II() is private, so I1 can never be validly confused with II (either from within the class context or externally).

The free function case is actually interesting but not because of confusable identifiers. If you call I1() within the context of Derived, are you trying to call the member function (you will) or the free function (you won't)? But I don't think it should be warned as a confusable identifier in terms of Unicode confusables because calling the member function outside of the class context requires an object while calling the free function does not, while calling it within the class is already confused because of name hiding.

Does that change your opinion at all?

Update test case to take into accounts reviewers suggestions.

As a consequence use a more complex approach to detect whether two NamedDecl may interfere.

aaron.ballman accepted this revision.Jul 1 2022, 5:22 AM

LGTM aside from some minor style nits.

clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp
130–132
135–137
142–145
This revision was landed with ongoing or failed builds.Jul 4 2022, 11:56 PM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Jul 5 2022, 12:06 AM

Looks like this breaks tests on windows: http://45.33.8.238/win/61559/step_8.txt

Looks like this breaks tests on windows: http://45.33.8.238/win/61559/step_8.txt

I'm on it.