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 9 #include "TestVisitor.h" 10 #include "clang/Basic/Diagnostic.h" 11 #include "clang/Tooling/Refactoring/SourceCode.h" 12 13 using namespace clang; 14 15 using tooling::getText; 16 using tooling::getExtendedText; 17 18 namespace { 19 20 struct CallsVisitor : TestVisitor<CallsVisitor> { 21 bool VisitCallExpr(CallExpr *Expr) { 22 OnCall(Expr, Context); 23 return true; 24 } 25 26 std::function<void(CallExpr *, ASTContext *Context)> OnCall; 27 }; 28 29 TEST(SourceCodeTest, getText) { 30 CallsVisitor Visitor; 31 32 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) { 33 EXPECT_EQ("foo(x, y)", getText(*CE, *Context)); 34 }; 35 Visitor.runOver("void foo(int x, int y) { foo(x, y); }"); 36 37 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) { 38 EXPECT_EQ("APPLY(foo, x, y)", getText(*CE, *Context)); 39 }; 40 Visitor.runOver("#define APPLY(f, x, y) f(x, y)\n" 41 "void foo(int x, int y) { APPLY(foo, x, y); }"); 42 } 43 44 TEST(SourceCodeTest, getTextWithMacro) { 45 CallsVisitor Visitor; 46 47 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) { 48 EXPECT_EQ("F OO", getText(*CE, *Context)); 49 Expr *P0 = CE->getArg(0); 50 Expr *P1 = CE->getArg(1); 51 EXPECT_EQ("", getText(*P0, *Context)); 52 EXPECT_EQ("", getText(*P1, *Context)); 53 }; 54 Visitor.runOver("#define F foo(\n" 55 "#define OO x, y)\n" 56 "void foo(int x, int y) { F OO ; }"); 57 58 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) { 59 EXPECT_EQ("", getText(*CE, *Context)); 60 Expr *P0 = CE->getArg(0); 61 Expr *P1 = CE->getArg(1); 62 EXPECT_EQ("x", getText(*P0, *Context)); 63 EXPECT_EQ("y", getText(*P1, *Context)); 64 }; 65 Visitor.runOver("#define FOO(x, y) (void)x; (void)y; foo(x, y);\n" 66 "void foo(int x, int y) { FOO(x,y) }"); 67 } 68 69 TEST(SourceCodeTest, getExtendedText) { 70 CallsVisitor Visitor; 71 72 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) { 73 EXPECT_EQ("foo(x, y);", 74 getExtendedText(*CE, tok::TokenKind::semi, *Context)); 75 76 Expr *P0 = CE->getArg(0); 77 Expr *P1 = CE->getArg(1); 78 EXPECT_EQ("x", getExtendedText(*P0, tok::TokenKind::semi, *Context)); 79 EXPECT_EQ("x,", getExtendedText(*P0, tok::TokenKind::comma, *Context)); 80 EXPECT_EQ("y", getExtendedText(*P1, tok::TokenKind::semi, *Context)); 81 }; 82 Visitor.runOver("void foo(int x, int y) { foo(x, y); }"); 83 Visitor.runOver("void foo(int x, int y) { if (true) foo(x, y); }"); 84 Visitor.runOver("int foo(int x, int y) { if (true) return 3 + foo(x, y); }"); 85 Visitor.runOver("void foo(int x, int y) { for (foo(x, y);;) ++x; }"); 86 Visitor.runOver( 87 "bool foo(int x, int y) { for (;foo(x, y);) x = 1; return true; }"); 88 89 Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) { 90 EXPECT_EQ("foo()", getExtendedText(*CE, tok::TokenKind::semi, *Context)); 91 }; 92 Visitor.runOver("bool foo() { if (foo()) return true; return false; }"); 93 Visitor.runOver("void foo() { int x; for (;; foo()) ++x; }"); 94 Visitor.runOver("int foo() { return foo() + 3; }"); 95 } 96 97 } // end anonymous namespace 98