Commit 8a593e29 authored by Haojian Wu's avatar Haojian Wu
Browse files

[AST] Correct the CXXOperatorCallExpr source range.

Summary:
Previously, the range for "->" CXXOperatorCallExpr is the range of the
class object (not including the operator!), e.g. "[[vector_ptr]]->size()".

This patch includes the range of the operator, which fixes the issue
where clangd doesn't go to the overloaded operator "->" definition.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: ilya-biryukov, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D76128
parent 18c97662
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -649,9 +649,14 @@ StringRef LoopConvertCheck::getContainerString(ASTContext *Context,
                                               const ForStmt *Loop,
                                               const Expr *ContainerExpr) {
  StringRef ContainerString;
  if (isa<CXXThisExpr>(ContainerExpr->IgnoreParenImpCasts())) {
  ContainerExpr = ContainerExpr->IgnoreParenImpCasts();
  if (isa<CXXThisExpr>(ContainerExpr)) {
    ContainerString = "this";
  } else {
    // For CXXOperatorCallExpr (e.g. vector_ptr->size()), its first argument is
    // the class object (vector_ptr) we are targeting.
    if (const auto* E = dyn_cast<CXXOperatorCallExpr>(ContainerExpr))
      ContainerExpr = E->getArg(0);
    ContainerString =
        getStringFromRange(Context->getSourceManager(), Context->getLangOpts(),
                           ContainerExpr->getSourceRange());
+8 −0
Original line number Diff line number Diff line
@@ -380,6 +380,14 @@ TEST(SelectionTest, CommonAncestor) {
      {"struct foo { [[^~foo()]]; };", "CXXDestructorDecl"},
      // FIXME: The following to should be class itself instead.
      {"struct foo { [[fo^o(){}]] };", "CXXConstructorDecl"},

      {R"cpp(
        struct S1 { void f(); };
        struct S2 { S1 * operator->(); };
        void test(S2 s2) {
          s2[[-^>]]f();
        }
      )cpp", "DeclRefExpr"} // DeclRefExpr to the "operator->" method.
  };
  for (const Case &C : Cases) {
    Annotations Test(C.Code);
+8 −0
Original line number Diff line number Diff line
@@ -452,6 +452,14 @@ TEST(LocateSymbol, All) {
        }
      )cpp",

      R"cpp(
        struct S1 { void f(); };
        struct S2 { S1 * $decl[[operator]]->(); };
        void test(S2 s2) {
          s2-^>f();
        }
      )cpp",

      R"cpp(// Declaration of explicit template specialization
        template <typename T>
        struct $decl[[Foo]] {};
+1 −1
Original line number Diff line number Diff line
@@ -637,7 +637,7 @@ SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
      // Postfix operator
      return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
  } else if (Kind == OO_Arrow) {
    return getArg(0)->getSourceRange();
    return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
  } else if (Kind == OO_Call) {
    return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
  } else if (Kind == OO_Subscript) {