From 4a9523c55fa11d85a4edbf24abc2c17bb3849fbc Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 17 Nov 2021 18:13:25 -0800 Subject: [PATCH] PR52537: When performing a no-op TreeTransform of a rewritten binary operator, mark any functions it calls as referenced. --- clang/include/clang/Sema/Sema.h | 3 ++- clang/lib/Sema/SemaExpr.cpp | 18 ++++++++++---- clang/lib/Sema/TreeTransform.h | 20 ++++++++++++---- clang/test/SemaCXX/compare-cxx2a.cpp | 36 ++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index a159be2b5fb1..91c9feb40cba 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -5114,7 +5114,8 @@ public: /// type -- entities referenced by the type are now referenced. void MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T); void MarkDeclarationsReferencedInExpr(Expr *E, - bool SkipLocalVariables = false); + bool SkipLocalVariables = false, + ArrayRef StopAt = None); /// Try to recover by turning the given expression into a /// call. Returns true if recovery was attempted or an error was diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 3297c01047ad..97f2062d4485 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -18867,14 +18867,22 @@ class EvaluatedExprMarker : public UsedDeclVisitor { public: typedef UsedDeclVisitor Inherited; bool SkipLocalVariables; + ArrayRef StopAt; - EvaluatedExprMarker(Sema &S, bool SkipLocalVariables) - : Inherited(S), SkipLocalVariables(SkipLocalVariables) {} + EvaluatedExprMarker(Sema &S, bool SkipLocalVariables, + ArrayRef StopAt) + : Inherited(S), SkipLocalVariables(SkipLocalVariables), StopAt(StopAt) {} void visitUsedDecl(SourceLocation Loc, Decl *D) { S.MarkFunctionReferenced(Loc, cast(D)); } + void Visit(Expr *E) { + if (std::find(StopAt.begin(), StopAt.end(), E) != StopAt.end()) + return; + Inherited::Visit(E); + } + void VisitDeclRefExpr(DeclRefExpr *E) { // If we were asked not to visit local variables, don't. if (SkipLocalVariables) { @@ -18901,9 +18909,11 @@ public: /// /// \param SkipLocalVariables If true, don't mark local variables as /// 'referenced'. +/// \param StopAt Subexpressions that we shouldn't recurse into. void Sema::MarkDeclarationsReferencedInExpr(Expr *E, - bool SkipLocalVariables) { - EvaluatedExprMarker(*this, SkipLocalVariables).Visit(E); + bool SkipLocalVariables, + ArrayRef StopAt) { + EvaluatedExprMarker(*this, SkipLocalVariables, StopAt).Visit(E); } /// Emit a diagnostic when statements are reachable. diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 442b4819b808..7f3326c13263 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -11008,14 +11008,10 @@ ExprResult TreeTransform::TransformCXXRewrittenBinaryOperator( if (RHS.isInvalid()) return ExprError(); - if (!getDerived().AlwaysRebuild() && - LHS.get() == Decomp.LHS && - RHS.get() == Decomp.RHS) - return E; - // Extract the already-resolved callee declarations so that we can restrict // ourselves to using them as the unqualified lookup results when rebuilding. UnresolvedSet<2> UnqualLookups; + bool ChangedAnyLookups = false; Expr *PossibleBinOps[] = {E->getSemanticForm(), const_cast(Decomp.InnerBinOp)}; for (Expr *PossibleBinOp : PossibleBinOps) { @@ -11032,9 +11028,23 @@ ExprResult TreeTransform::TransformCXXRewrittenBinaryOperator( E->getOperatorLoc(), Callee->getFoundDecl())); if (!Found) return ExprError(); + if (Found != Callee->getFoundDecl()) + ChangedAnyLookups = true; UnqualLookups.addDecl(Found); } + if (!getDerived().AlwaysRebuild() && !ChangedAnyLookups && + LHS.get() == Decomp.LHS && RHS.get() == Decomp.RHS) { + // Mark all functions used in the rewrite as referenced. Note that when + // a < b is rewritten to (a <=> b) < 0, both the <=> and the < might be + // function calls, and/or there might be a user-defined conversion sequence + // applied to the operands of the <. + // FIXME: this is a bit instantiation-specific. + const Expr *StopAt[] = {Decomp.LHS, Decomp.RHS}; + SemaRef.MarkDeclarationsReferencedInExpr(E, false, StopAt); + return E; + } + return getDerived().RebuildCXXRewrittenBinaryOperator( E->getOperatorLoc(), Decomp.Opcode, UnqualLookups, LHS.get(), RHS.get()); } diff --git a/clang/test/SemaCXX/compare-cxx2a.cpp b/clang/test/SemaCXX/compare-cxx2a.cpp index 61b8558ccc02..0cb48bcfcec2 100644 --- a/clang/test/SemaCXX/compare-cxx2a.cpp +++ b/clang/test/SemaCXX/compare-cxx2a.cpp @@ -425,3 +425,39 @@ namespace PR44992 { friend auto operator<=>(s const &, s const &) = default; }; } + +namespace PR52537 { + template struct X {}; + template bool operator==(const X &, int) { return T::error; } // expected-error 2{{no members}} + template int operator<=>(const X &, int) { return T::error; } // expected-error 2{{no members}} + + const X x1; + template bool f1() { return x1 != 0; } // expected-note {{instantiation of}} + void g1() { f1(); } // expected-note {{instantiation of}} + + const X x2; + template bool f2() { return 0 == x2; } // expected-note {{instantiation of}} + void g2() { f2(); } // expected-note {{instantiation of}} + + const X x3; + template bool f3() { return x3 < 0; } // expected-note {{instantiation of}} + void g3() { f3(); } // expected-note {{instantiation of}} + + const X x4; + template bool f4() { return 0 >= x4; } // expected-note {{instantiation of}} + void g4() { f4(); } // expected-note {{instantiation of}} + + template struct Y {}; + template struct Z { Z(int) { T::error; } using nondeduced = Z; }; // expected-error 2{{no members}} + template Z operator<=>(const Y&, int); + template bool operator<(const Z&, const typename Z::nondeduced&); + template bool operator<(const typename Z::nondeduced&, const Z&); + + const Y y5; + template bool f5() { return y5 < 0; } // expected-note {{instantiation of}} + void g5() { f5(); } // expected-note {{instantiation of}} + + const Y y6; + template bool f6() { return 0 < y6; } // expected-note {{instantiation of}} + void g6() { f6(); } // expected-note {{instantiation of}} +}