RangeSelectorTest.cpp 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
//===- unittest/Tooling/RangeSelectorTest.cpp -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/Tooling/Transformer/RangeSelector.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/Transformer/SourceCode.h"
#include "llvm/Support/Error.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace clang;
using namespace transformer;
using namespace ast_matchers;

namespace {
using ::llvm::Expected;
using ::llvm::Failed;
using ::llvm::HasValue;
using ::llvm::StringError;
using ::testing::AllOf;
using ::testing::HasSubstr;
using ::testing::Property;

using MatchResult = MatchFinder::MatchResult;

struct TestMatch {
  // The AST unit from which `result` is built. We bundle it because it backs
  // the result. Users are not expected to access it.
  std::unique_ptr<clang::ASTUnit> ASTUnit;
  // The result to use in the test. References `ast_unit`.
  MatchResult Result;
};

template <typename M> TestMatch matchCode(StringRef Code, M Matcher) {
  auto ASTUnit = tooling::buildASTFromCode(Code);
  assert(ASTUnit != nullptr && "AST construction failed");

  ASTContext &Context = ASTUnit->getASTContext();
  assert(!Context.getDiagnostics().hasErrorOccurred() && "Compilation error");

  auto Matches = ast_matchers::match(Matcher, Context);
  // We expect a single, exact match.
  assert(Matches.size() != 0 && "no matches found");
  assert(Matches.size() == 1 && "too many matches");

  return TestMatch{std::move(ASTUnit), MatchResult(Matches[0], &Context)};
}

// Applies \p Selector to \p Match and, on success, returns the selected source.
Expected<StringRef> select(RangeSelector Selector, const TestMatch &Match) {
  Expected<CharSourceRange> Range = Selector(Match.Result);
  if (!Range)
    return Range.takeError();
  return tooling::getText(*Range, *Match.Result.Context);
}

// Applies \p Selector to a trivial match with only a single bound node with id
// "bound_node_id".  For use in testing unbound-node errors.
Expected<CharSourceRange> selectFromTrivial(const RangeSelector &Selector) {
  // We need to bind the result to something, or the match will fail. Use a
  // binding that is not used in the unbound node tests.
  TestMatch Match =
      matchCode("static int x = 0;", varDecl().bind("bound_node_id"));
  return Selector(Match.Result);
}

// Matches the message expected for unbound-node failures.
testing::Matcher<StringError> withUnboundNodeMessage() {
  return testing::Property(
      &StringError::getMessage,
      AllOf(HasSubstr("unbound_id"), HasSubstr("not bound")));
}

// Applies \p Selector to code containing assorted node types, where the match
// binds each one: a statement ("stmt"), a (non-member) ctor-initializer
// ("init"), an expression ("expr") and a (nameless) declaration ("decl").  Used
// to test failures caused by applying selectors to nodes of the wrong type.
Expected<CharSourceRange> selectFromAssorted(RangeSelector Selector) {
  StringRef Code = R"cc(
      struct A {};
      class F : public A {
       public:
        F(int) {}
      };
      void g() { F f(1); }
    )cc";

  auto Matcher =
      compoundStmt(
          hasDescendant(
              cxxConstructExpr(
                  hasDeclaration(
                      decl(hasDescendant(cxxCtorInitializer(isBaseInitializer())
                                             .bind("init")))
                          .bind("decl")))
                  .bind("expr")))
          .bind("stmt");

  return Selector(matchCode(Code, Matcher).Result);
}

// Matches the message expected for type-error failures.
testing::Matcher<StringError> withTypeErrorMessage(StringRef NodeID) {
  return testing::Property(
      &StringError::getMessage,
      AllOf(HasSubstr(NodeID), HasSubstr("mismatched type")));
}

TEST(RangeSelectorTest, UnboundNode) {
  EXPECT_THAT_EXPECTED(selectFromTrivial(node("unbound_id")),
                       Failed<StringError>(withUnboundNodeMessage()));
}

MATCHER_P(EqualsCharSourceRange, Range, "") {
  return Range.getAsRange() == arg.getAsRange() &&
         Range.isTokenRange() == arg.isTokenRange();
}

// FIXME: here and elsewhere: use llvm::Annotations library to explicitly mark
// points and ranges of interest, enabling more readable tests.
TEST(RangeSelectorTest, BeforeOp) {
  StringRef Code = R"cc(
    int f(int x, int y, int z) { return 3; }
    int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
  )cc";
  StringRef Call = "call";
  TestMatch Match = matchCode(Code, callExpr().bind(Call));
  const auto* E = Match.Result.Nodes.getNodeAs<Expr>(Call);
  assert(E != nullptr);
  auto ExprBegin = E->getSourceRange().getBegin();
  EXPECT_THAT_EXPECTED(
      before(node(Call))(Match.Result),
      HasValue(EqualsCharSourceRange(
          CharSourceRange::getCharRange(ExprBegin, ExprBegin))));
}

TEST(RangeSelectorTest, AfterOp) {
  StringRef Code = R"cc(
    int f(int x, int y, int z) { return 3; }
    int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
  )cc";
  StringRef Call = "call";
  TestMatch Match = matchCode(Code, callExpr().bind(Call));
  const auto* E = Match.Result.Nodes.getNodeAs<Expr>(Call);
  assert(E != nullptr);
  const SourceRange Range = E->getSourceRange();
  // The end token, a right paren, is one character wide, so advance by one,
  // bringing us to the semicolon.
  const SourceLocation SemiLoc = Range.getEnd().getLocWithOffset(1);
  const auto ExpectedAfter = CharSourceRange::getCharRange(SemiLoc, SemiLoc);

  // Test with a char range.
  auto CharRange = CharSourceRange::getCharRange(Range.getBegin(), SemiLoc);
  EXPECT_THAT_EXPECTED(after(charRange(CharRange))(Match.Result),
                       HasValue(EqualsCharSourceRange(ExpectedAfter)));

  // Test with a token range.
  auto TokenRange = CharSourceRange::getTokenRange(Range);
  EXPECT_THAT_EXPECTED(after(charRange(TokenRange))(Match.Result),
                       HasValue(EqualsCharSourceRange(ExpectedAfter)));
}

TEST(RangeSelectorTest, RangeOp) {
  StringRef Code = R"cc(
    int f(int x, int y, int z) { return 3; }
    int g() { return f(/* comment */ 3, 7 /* comment */, 9); }
  )cc";
  StringRef Arg0 = "a0";
  StringRef Arg1 = "a1";
  StringRef Call = "call";
  auto Matcher = callExpr(hasArgument(0, expr().bind(Arg0)),
                          hasArgument(1, expr().bind(Arg1)))
                     .bind(Call);
  TestMatch Match = matchCode(Code, Matcher);

  // Node-id specific version:
  EXPECT_THAT_EXPECTED(select(range(Arg0, Arg1), Match), HasValue("3, 7"));
  // General version:
  EXPECT_THAT_EXPECTED(select(range(node(Arg0), node(Arg1)), Match),
                       HasValue("3, 7"));
}

TEST(RangeSelectorTest, NodeOpStatement) {
  StringRef Code = "int f() { return 3; }";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, returnStmt().bind(ID));
  EXPECT_THAT_EXPECTED(select(node(ID), Match), HasValue("return 3;"));
}

TEST(RangeSelectorTest, NodeOpExpression) {
  StringRef Code = "int f() { return 3; }";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, expr().bind(ID));
  EXPECT_THAT_EXPECTED(select(node(ID), Match), HasValue("3"));
}

TEST(RangeSelectorTest, StatementOp) {
  StringRef Code = "int f() { return 3; }";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, expr().bind(ID));
  EXPECT_THAT_EXPECTED(select(statement(ID), Match), HasValue("3;"));
}

TEST(RangeSelectorTest, MemberOp) {
  StringRef Code = R"cc(
    struct S {
      int member;
    };
    int g() {
      S s;
      return s.member;
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, memberExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(member(ID), Match), HasValue("member"));
}

// Tests that member does not select any qualifiers on the member name.
TEST(RangeSelectorTest, MemberOpQualified) {
  StringRef Code = R"cc(
    struct S {
      int member;
    };
    struct T : public S {
      int field;
    };
    int g() {
      T t;
      return t.S::member;
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, memberExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(member(ID), Match), HasValue("member"));
}

TEST(RangeSelectorTest, MemberOpTemplate) {
  StringRef Code = R"cc(
    struct S {
      template <typename T> T foo(T t);
    };
    int f(int x) {
      S s;
      return s.template foo<int>(3);
    }
  )cc";

  StringRef ID = "id";
  TestMatch Match = matchCode(Code, memberExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(member(ID), Match), HasValue("foo"));
}

TEST(RangeSelectorTest, MemberOpOperator) {
  StringRef Code = R"cc(
    struct S {
      int operator*();
    };
    int f(int x) {
      S s;
      return s.operator *();
    }
  )cc";

  StringRef ID = "id";
  TestMatch Match = matchCode(Code, memberExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(member(ID), Match), HasValue("operator *"));
}

TEST(RangeSelectorTest, NameOpNamedDecl) {
  StringRef Code = R"cc(
    int myfun() {
      return 3;
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, functionDecl().bind(ID));
  EXPECT_THAT_EXPECTED(select(name(ID), Match), HasValue("myfun"));
}

TEST(RangeSelectorTest, NameOpDeclRef) {
  StringRef Code = R"cc(
    int foo(int x) {
      return x;
    }
    int g(int x) { return foo(x) * x; }
  )cc";
  StringRef Ref = "ref";
  TestMatch Match = matchCode(Code, declRefExpr(to(functionDecl())).bind(Ref));
  EXPECT_THAT_EXPECTED(select(name(Ref), Match), HasValue("foo"));
}

TEST(RangeSelectorTest, NameOpCtorInitializer) {
  StringRef Code = R"cc(
    class C {
     public:
      C() : field(3) {}
      int field;
    };
  )cc";
  StringRef Init = "init";
  TestMatch Match = matchCode(Code, cxxCtorInitializer().bind(Init));
  EXPECT_THAT_EXPECTED(select(name(Init), Match), HasValue("field"));
}

TEST(RangeSelectorTest, NameOpErrors) {
  EXPECT_THAT_EXPECTED(selectFromTrivial(name("unbound_id")),
                       Failed<StringError>(withUnboundNodeMessage()));
  EXPECT_THAT_EXPECTED(selectFromAssorted(name("stmt")),
                       Failed<StringError>(withTypeErrorMessage("stmt")));
}

TEST(RangeSelectorTest, NameOpDeclRefError) {
  StringRef Code = R"cc(
    struct S {
      int operator*();
    };
    int f(int x) {
      S s;
      return *s + x;
    }
  )cc";
  StringRef Ref = "ref";
  TestMatch Match = matchCode(Code, declRefExpr(to(functionDecl())).bind(Ref));
  EXPECT_THAT_EXPECTED(
      name(Ref)(Match.Result),
      Failed<StringError>(testing::Property(
          &StringError::getMessage,
          AllOf(HasSubstr(Ref), HasSubstr("requires property 'identifier'")))));
}

TEST(RangeSelectorTest, CallArgsOp) {
  const StringRef Code = R"cc(
    struct C {
      int bar(int, int);
    };
    int f() {
      C x;
      return x.bar(3, 4);
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, callExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue("3, 4"));
}

TEST(RangeSelectorTest, CallArgsOpNoArgs) {
  const StringRef Code = R"cc(
    struct C {
      int bar();
    };
    int f() {
      C x;
      return x.bar();
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, callExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue(""));
}

TEST(RangeSelectorTest, CallArgsOpNoArgsWithComments) {
  const StringRef Code = R"cc(
    struct C {
      int bar();
    };
    int f() {
      C x;
      return x.bar(/*empty*/);
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, callExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue("/*empty*/"));
}

// Tests that arguments are extracted correctly when a temporary (with parens)
// is used.
TEST(RangeSelectorTest, CallArgsOpWithParens) {
  const StringRef Code = R"cc(
    struct C {
      int bar(int, int) { return 3; }
    };
    int f() {
      C x;
      return C().bar(3, 4);
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match =
      matchCode(Code, callExpr(callee(functionDecl(hasName("bar")))).bind(ID));
  EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue("3, 4"));
}

TEST(RangeSelectorTest, CallArgsOpLeadingComments) {
  const StringRef Code = R"cc(
    struct C {
      int bar(int, int) { return 3; }
    };
    int f() {
      C x;
      return x.bar(/*leading*/ 3, 4);
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, callExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(callArgs(ID), Match),
                       HasValue("/*leading*/ 3, 4"));
}

TEST(RangeSelectorTest, CallArgsOpTrailingComments) {
  const StringRef Code = R"cc(
    struct C {
      int bar(int, int) { return 3; }
    };
    int f() {
      C x;
      return x.bar(3 /*trailing*/, 4);
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, callExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(callArgs(ID), Match),
                       HasValue("3 /*trailing*/, 4"));
}

TEST(RangeSelectorTest, CallArgsOpEolComments) {
  const StringRef Code = R"cc(
    struct C {
      int bar(int, int) { return 3; }
    };
    int f() {
      C x;
      return x.bar(  // Header
          1,           // foo
          2            // bar
      );
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, callExpr().bind(ID));
  std::string ExpectedString = R"(  // Header
          1,           // foo
          2            // bar
      )";
  EXPECT_THAT_EXPECTED(select(callArgs(ID), Match), HasValue(ExpectedString));
}

TEST(RangeSelectorTest, CallArgsErrors) {
  EXPECT_THAT_EXPECTED(selectFromTrivial(callArgs("unbound_id")),
                       Failed<StringError>(withUnboundNodeMessage()));
  EXPECT_THAT_EXPECTED(selectFromAssorted(callArgs("stmt")),
                       Failed<StringError>(withTypeErrorMessage("stmt")));
}

TEST(RangeSelectorTest, StatementsOp) {
  StringRef Code = R"cc(
    void g();
    void f() { /* comment */ g(); /* comment */ g(); /* comment */ }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, compoundStmt().bind(ID));
  EXPECT_THAT_EXPECTED(
      select(statements(ID), Match),
      HasValue(" /* comment */ g(); /* comment */ g(); /* comment */ "));
}

TEST(RangeSelectorTest, StatementsOpEmptyList) {
  StringRef Code = "void f() {}";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, compoundStmt().bind(ID));
  EXPECT_THAT_EXPECTED(select(statements(ID), Match), HasValue(""));
}

TEST(RangeSelectorTest, StatementsOpErrors) {
  EXPECT_THAT_EXPECTED(selectFromTrivial(statements("unbound_id")),
                       Failed<StringError>(withUnboundNodeMessage()));
  EXPECT_THAT_EXPECTED(selectFromAssorted(statements("decl")),
                       Failed<StringError>(withTypeErrorMessage("decl")));
}

TEST(RangeSelectorTest, ElementsOp) {
  StringRef Code = R"cc(
    void f() {
      int v[] = {/* comment */ 3, /* comment*/ 4 /* comment */};
      (void)v;
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, initListExpr().bind(ID));
  EXPECT_THAT_EXPECTED(
      select(initListElements(ID), Match),
      HasValue("/* comment */ 3, /* comment*/ 4 /* comment */"));
}

TEST(RangeSelectorTest, ElementsOpEmptyList) {
  StringRef Code = R"cc(
    void f() {
      int v[] = {};
      (void)v;
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, initListExpr().bind(ID));
  EXPECT_THAT_EXPECTED(select(initListElements(ID), Match), HasValue(""));
}

TEST(RangeSelectorTest, ElementsOpErrors) {
  EXPECT_THAT_EXPECTED(selectFromTrivial(initListElements("unbound_id")),
                       Failed<StringError>(withUnboundNodeMessage()));
  EXPECT_THAT_EXPECTED(selectFromAssorted(initListElements("stmt")),
                       Failed<StringError>(withTypeErrorMessage("stmt")));
}

TEST(RangeSelectorTest, ElseBranchOpSingleStatement) {
  StringRef Code = R"cc(
    int f() {
      int x = 0;
      if (true) x = 3;
      else x = 4;
      return x + 5;
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, ifStmt().bind(ID));
  EXPECT_THAT_EXPECTED(select(elseBranch(ID), Match), HasValue("else x = 4;"));
}

TEST(RangeSelectorTest, ElseBranchOpCompoundStatement) {
  StringRef Code = R"cc(
    int f() {
      int x = 0;
      if (true) x = 3;
      else { x = 4; }
      return x + 5;
    }
  )cc";
  StringRef ID = "id";
  TestMatch Match = matchCode(Code, ifStmt().bind(ID));
  EXPECT_THAT_EXPECTED(select(elseBranch(ID), Match),
                       HasValue("else { x = 4; }"));
}

// Tests case where the matched node is the complete expanded text.
TEST(RangeSelectorTest, ExpansionOp) {
  StringRef Code = R"cc(
#define BADDECL(E) int bad(int x) { return E; }
    BADDECL(x * x)
  )cc";

  StringRef Fun = "Fun";
  TestMatch Match = matchCode(Code, functionDecl(hasName("bad")).bind(Fun));
  EXPECT_THAT_EXPECTED(select(expansion(node(Fun)), Match),
                       HasValue("BADDECL(x * x)"));
}

// Tests case where the matched node is (only) part of the expanded text.
TEST(RangeSelectorTest, ExpansionOpPartial) {
  StringRef Code = R"cc(
#define BADDECL(E) int bad(int x) { return E; }
    BADDECL(x * x)
  )cc";

  StringRef Ret = "Ret";
  TestMatch Match = matchCode(Code, returnStmt().bind(Ret));
  EXPECT_THAT_EXPECTED(select(expansion(node(Ret)), Match),
                       HasValue("BADDECL(x * x)"));
}

TEST(RangeSelectorTest, IfBoundOpBound) {
  StringRef Code = R"cc(
    int f() {
      return 3 + 5;
    }
  )cc";
  StringRef ID = "id", Op = "op";
  TestMatch Match =
      matchCode(Code, binaryOperator(hasLHS(expr().bind(ID))).bind(Op));
  EXPECT_THAT_EXPECTED(select(ifBound(ID, node(ID), node(Op)), Match),
                       HasValue("3"));
}

TEST(RangeSelectorTest, IfBoundOpUnbound) {
  StringRef Code = R"cc(
    int f() {
      return 3 + 5;
    }
  )cc";
  StringRef ID = "id", Op = "op";
  TestMatch Match = matchCode(Code, binaryOperator().bind(Op));
  EXPECT_THAT_EXPECTED(select(ifBound(ID, node(ID), node(Op)), Match),
                       HasValue("3 + 5"));
}

} // namespace