SourceCodeTest.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===- unittest/Tooling/SourceCodeTest.cpp --------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "TestVisitor.h"
  9. #include "clang/Basic/Diagnostic.h"
  10. #include "clang/Tooling/Refactoring/SourceCode.h"
  11. using namespace clang;
  12. using tooling::getText;
  13. using tooling::getExtendedText;
  14. namespace {
  15. struct CallsVisitor : TestVisitor<CallsVisitor> {
  16. bool VisitCallExpr(CallExpr *Expr) {
  17. OnCall(Expr, Context);
  18. return true;
  19. }
  20. std::function<void(CallExpr *, ASTContext *Context)> OnCall;
  21. };
  22. TEST(SourceCodeTest, getText) {
  23. CallsVisitor Visitor;
  24. Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
  25. EXPECT_EQ("foo(x, y)", getText(*CE, *Context));
  26. };
  27. Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
  28. Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
  29. EXPECT_EQ("APPLY(foo, x, y)", getText(*CE, *Context));
  30. };
  31. Visitor.runOver("#define APPLY(f, x, y) f(x, y)\n"
  32. "void foo(int x, int y) { APPLY(foo, x, y); }");
  33. }
  34. TEST(SourceCodeTest, getTextWithMacro) {
  35. CallsVisitor Visitor;
  36. Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
  37. EXPECT_EQ("F OO", getText(*CE, *Context));
  38. Expr *P0 = CE->getArg(0);
  39. Expr *P1 = CE->getArg(1);
  40. EXPECT_EQ("", getText(*P0, *Context));
  41. EXPECT_EQ("", getText(*P1, *Context));
  42. };
  43. Visitor.runOver("#define F foo(\n"
  44. "#define OO x, y)\n"
  45. "void foo(int x, int y) { F OO ; }");
  46. Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
  47. EXPECT_EQ("", getText(*CE, *Context));
  48. Expr *P0 = CE->getArg(0);
  49. Expr *P1 = CE->getArg(1);
  50. EXPECT_EQ("x", getText(*P0, *Context));
  51. EXPECT_EQ("y", getText(*P1, *Context));
  52. };
  53. Visitor.runOver("#define FOO(x, y) (void)x; (void)y; foo(x, y);\n"
  54. "void foo(int x, int y) { FOO(x,y) }");
  55. }
  56. TEST(SourceCodeTest, getExtendedText) {
  57. CallsVisitor Visitor;
  58. Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
  59. EXPECT_EQ("foo(x, y);",
  60. getExtendedText(*CE, tok::TokenKind::semi, *Context));
  61. Expr *P0 = CE->getArg(0);
  62. Expr *P1 = CE->getArg(1);
  63. EXPECT_EQ("x", getExtendedText(*P0, tok::TokenKind::semi, *Context));
  64. EXPECT_EQ("x,", getExtendedText(*P0, tok::TokenKind::comma, *Context));
  65. EXPECT_EQ("y", getExtendedText(*P1, tok::TokenKind::semi, *Context));
  66. };
  67. Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
  68. Visitor.runOver("void foo(int x, int y) { if (true) foo(x, y); }");
  69. Visitor.runOver("int foo(int x, int y) { if (true) return 3 + foo(x, y); }");
  70. Visitor.runOver("void foo(int x, int y) { for (foo(x, y);;) ++x; }");
  71. Visitor.runOver(
  72. "bool foo(int x, int y) { for (;foo(x, y);) x = 1; return true; }");
  73. Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
  74. EXPECT_EQ("foo()", getExtendedText(*CE, tok::TokenKind::semi, *Context));
  75. };
  76. Visitor.runOver("bool foo() { if (foo()) return true; return false; }");
  77. Visitor.runOver("void foo() { int x; for (;; foo()) ++x; }");
  78. Visitor.runOver("int foo() { return foo() + 3; }");
  79. }
  80. } // end anonymous namespace