xref: /minix3/external/bsd/llvm/dist/clang/unittests/Tooling/RefactoringCallbacksTest.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- unittest/Tooling/RefactoringCallbacksTest.cpp ----------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "clang/Tooling/RefactoringCallbacks.h"
11f4a2713aSLionel Sambuc #include "RewriterTestContext.h"
12f4a2713aSLionel Sambuc #include "clang/ASTMatchers/ASTMatchFinder.h"
13f4a2713aSLionel Sambuc #include "clang/ASTMatchers/ASTMatchers.h"
14f4a2713aSLionel Sambuc #include "gtest/gtest.h"
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc namespace clang {
17f4a2713aSLionel Sambuc namespace tooling {
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc using namespace ast_matchers;
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc template <typename T>
expectRewritten(const std::string & Code,const std::string & Expected,const T & AMatcher,RefactoringCallback & Callback)22f4a2713aSLionel Sambuc void expectRewritten(const std::string &Code,
23f4a2713aSLionel Sambuc                      const std::string &Expected,
24f4a2713aSLionel Sambuc                      const T &AMatcher,
25f4a2713aSLionel Sambuc                      RefactoringCallback &Callback) {
26f4a2713aSLionel Sambuc   MatchFinder Finder;
27f4a2713aSLionel Sambuc   Finder.addMatcher(AMatcher, &Callback);
28*0a6a1f1dSLionel Sambuc   std::unique_ptr<tooling::FrontendActionFactory> Factory(
29f4a2713aSLionel Sambuc       tooling::newFrontendActionFactory(&Finder));
30f4a2713aSLionel Sambuc   ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), Code))
31f4a2713aSLionel Sambuc       << "Parsing error in \"" << Code << "\"";
32f4a2713aSLionel Sambuc   RewriterTestContext Context;
33f4a2713aSLionel Sambuc   FileID ID = Context.createInMemoryFile("input.cc", Code);
34f4a2713aSLionel Sambuc   EXPECT_TRUE(tooling::applyAllReplacements(Callback.getReplacements(),
35f4a2713aSLionel Sambuc                                             Context.Rewrite));
36f4a2713aSLionel Sambuc   EXPECT_EQ(Expected, Context.getRewrittenText(ID));
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc 
TEST(RefactoringCallbacksTest,ReplacesStmtsWithString)39f4a2713aSLionel Sambuc TEST(RefactoringCallbacksTest, ReplacesStmtsWithString) {
40f4a2713aSLionel Sambuc   std::string Code = "void f() { int i = 1; }";
41f4a2713aSLionel Sambuc   std::string Expected = "void f() { ; }";
42f4a2713aSLionel Sambuc   ReplaceStmtWithText Callback("id", ";");
43f4a2713aSLionel Sambuc   expectRewritten(Code, Expected, id("id", declStmt()), Callback);
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
TEST(RefactoringCallbacksTest,ReplacesStmtsInCalledMacros)46f4a2713aSLionel Sambuc TEST(RefactoringCallbacksTest, ReplacesStmtsInCalledMacros) {
47f4a2713aSLionel Sambuc   std::string Code = "#define A void f() { int i = 1; }\nA";
48f4a2713aSLionel Sambuc   std::string Expected = "#define A void f() { ; }\nA";
49f4a2713aSLionel Sambuc   ReplaceStmtWithText Callback("id", ";");
50f4a2713aSLionel Sambuc   expectRewritten(Code, Expected, id("id", declStmt()), Callback);
51f4a2713aSLionel Sambuc }
52f4a2713aSLionel Sambuc 
TEST(RefactoringCallbacksTest,IgnoresStmtsInUncalledMacros)53f4a2713aSLionel Sambuc TEST(RefactoringCallbacksTest, IgnoresStmtsInUncalledMacros) {
54f4a2713aSLionel Sambuc   std::string Code = "#define A void f() { int i = 1; }";
55f4a2713aSLionel Sambuc   std::string Expected = "#define A void f() { int i = 1; }";
56f4a2713aSLionel Sambuc   ReplaceStmtWithText Callback("id", ";");
57f4a2713aSLionel Sambuc   expectRewritten(Code, Expected, id("id", declStmt()), Callback);
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc 
TEST(RefactoringCallbacksTest,ReplacesInteger)60f4a2713aSLionel Sambuc TEST(RefactoringCallbacksTest, ReplacesInteger) {
61f4a2713aSLionel Sambuc   std::string Code = "void f() { int i = 1; }";
62f4a2713aSLionel Sambuc   std::string Expected = "void f() { int i = 2; }";
63f4a2713aSLionel Sambuc   ReplaceStmtWithText Callback("id", "2");
64f4a2713aSLionel Sambuc   expectRewritten(Code, Expected, id("id", expr(integerLiteral())),
65f4a2713aSLionel Sambuc                   Callback);
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
TEST(RefactoringCallbacksTest,ReplacesStmtWithStmt)68f4a2713aSLionel Sambuc TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
69f4a2713aSLionel Sambuc   std::string Code = "void f() { int i = false ? 1 : i * 2; }";
70f4a2713aSLionel Sambuc   std::string Expected = "void f() { int i = i * 2; }";
71f4a2713aSLionel Sambuc   ReplaceStmtWithStmt Callback("always-false", "should-be");
72f4a2713aSLionel Sambuc   expectRewritten(Code, Expected,
73f4a2713aSLionel Sambuc       id("always-false", conditionalOperator(
74f4a2713aSLionel Sambuc           hasCondition(boolLiteral(equals(false))),
75f4a2713aSLionel Sambuc           hasFalseExpression(id("should-be", expr())))),
76f4a2713aSLionel Sambuc       Callback);
77f4a2713aSLionel Sambuc }
78f4a2713aSLionel Sambuc 
TEST(RefactoringCallbacksTest,ReplacesIfStmt)79f4a2713aSLionel Sambuc TEST(RefactoringCallbacksTest, ReplacesIfStmt) {
80f4a2713aSLionel Sambuc   std::string Code = "bool a; void f() { if (a) f(); else a = true; }";
81f4a2713aSLionel Sambuc   std::string Expected = "bool a; void f() { f(); }";
82f4a2713aSLionel Sambuc   ReplaceIfStmtWithItsBody Callback("id", true);
83f4a2713aSLionel Sambuc   expectRewritten(Code, Expected,
84f4a2713aSLionel Sambuc       id("id", ifStmt(
85f4a2713aSLionel Sambuc           hasCondition(implicitCastExpr(hasSourceExpression(
86f4a2713aSLionel Sambuc               declRefExpr(to(varDecl(hasName("a"))))))))),
87f4a2713aSLionel Sambuc       Callback);
88f4a2713aSLionel Sambuc }
89f4a2713aSLionel Sambuc 
TEST(RefactoringCallbacksTest,RemovesEntireIfOnEmptyElse)90f4a2713aSLionel Sambuc TEST(RefactoringCallbacksTest, RemovesEntireIfOnEmptyElse) {
91f4a2713aSLionel Sambuc   std::string Code = "void f() { if (false) int i = 0; }";
92f4a2713aSLionel Sambuc   std::string Expected = "void f() {  }";
93f4a2713aSLionel Sambuc   ReplaceIfStmtWithItsBody Callback("id", false);
94f4a2713aSLionel Sambuc   expectRewritten(Code, Expected,
95f4a2713aSLionel Sambuc       id("id", ifStmt(hasCondition(boolLiteral(equals(false))))),
96f4a2713aSLionel Sambuc       Callback);
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc } // end namespace ast_matchers
100f4a2713aSLionel Sambuc } // end namespace clang
101