Page MenuHomePhabricator

[Sema] Prevent -Wunused-lambda-capture from generating false positive warnings on templated member function
Needs ReviewPublic

Authored by ziangwan on Jul 31 2019, 5:05 PM.

Details

Summary

This is a continuation of https://reviews.llvm.org/D44844 and a potential fix for https://bugs.llvm.org/show_bug.cgi?id=36880.
This fix follows the specification in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0588r1.html 8.1.5.2 paragraph 7.

template <typename A>
class Variant {
public:
  Variant() {
    [this](auto value) { Construct(value); }(5); // false positive warning about "this" capture unused.
  }


  template <typename Arg>
  void Construct(Arg value) {}
};


int main() {
  Variant<int> v;
  return 0;
}

Whenever we see a UnresolvedMemberExpr, we always mark a this capture ODR-used, even if later on it may be resolved to a static member function. The trade-off of this fix is that it will leads to false negative in the following case:

template <typename A>
class OverloadedMixFalseNegative {
public:
  OverloadedMixFalseNegative() {
    [this](auto value) { Construct(value); }(5); // warning disappears but should happen
  }

  static void Construct(int value) {}
  void Construct(float value) {}
};

Diff Detail

Event Timeline

ziangwan created this revision.Jul 31 2019, 5:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 31 2019, 5:05 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ziangwan edited reviewers, added: arphaman; removed: stephenkelly.Jul 31 2019, 5:06 PM
ziangwan edited the summary of this revision. (Show Details)Aug 2 2019, 1:28 PM
ziangwan changed the repository for this revision from rC Clang to rL LLVM.Aug 4 2019, 6:21 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2019, 6:21 PM

Thanks for the patch @ziangwan !

clang/lib/Sema/SemaExpr.cpp
5631

LLVM style is not to use {} for single statement bodies.

ziangwan updated this revision to Diff 214013.Aug 7 2019, 3:06 PM

Fix style issue.

ziangwan marked an inline comment as done.Aug 7 2019, 3:06 PM