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
|
//===- unittest/Tooling/FixitTest.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 "TestVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Tooling/FixIt.h"
using namespace clang;
using tooling::fixit::getText;
using tooling::fixit::createRemoval;
using tooling::fixit::createReplacement;
namespace {
struct CallsVisitor : TestVisitor<CallsVisitor> {
bool VisitCallExpr(CallExpr *Expr) {
OnCall(Expr, Context);
return true;
}
std::function<void(CallExpr *, ASTContext *Context)> OnCall;
};
std::string LocationToString(SourceLocation Loc, ASTContext *Context) {
return Loc.printToString(Context->getSourceManager());
}
TEST(FixItTest, getText) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("foo(x, y)", getText(*CE, *Context));
EXPECT_EQ("foo(x, y)", getText(CE->getSourceRange(), *Context));
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
EXPECT_EQ("x", getText(*P0, *Context));
EXPECT_EQ("y", getText(*P1, *Context));
};
Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("APPLY(foo, x, y)", getText(*CE, *Context));
};
Visitor.runOver("#define APPLY(f, x, y) f(x, y)\n"
"void foo(int x, int y) { APPLY(foo, x, y); }");
}
TEST(FixItTest, getTextWithMacro) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("F OO", getText(*CE, *Context));
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
EXPECT_EQ("", getText(*P0, *Context));
EXPECT_EQ("", getText(*P1, *Context));
};
Visitor.runOver("#define F foo(\n"
"#define OO x, y)\n"
"void foo(int x, int y) { F OO ; }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
EXPECT_EQ("", getText(*CE, *Context));
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
EXPECT_EQ("x", getText(*P0, *Context));
EXPECT_EQ("y", getText(*P1, *Context));
};
Visitor.runOver("#define FOO(x, y) (void)x; (void)y; foo(x, y);\n"
"void foo(int x, int y) { FOO(x,y) }");
}
TEST(FixItTest, createRemoval) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
FixItHint Hint = createRemoval(*CE);
EXPECT_EQ("foo(x, y)", getText(Hint.RemoveRange.getAsRange(), *Context));
EXPECT_TRUE(Hint.InsertFromRange.isInvalid());
EXPECT_TRUE(Hint.CodeToInsert.empty());
Expr *P0 = CE->getArg(0);
FixItHint Hint0 = createRemoval(*P0);
EXPECT_EQ("x", getText(Hint0.RemoveRange.getAsRange(), *Context));
EXPECT_TRUE(Hint0.InsertFromRange.isInvalid());
EXPECT_TRUE(Hint0.CodeToInsert.empty());
Expr *P1 = CE->getArg(1);
FixItHint Hint1 = createRemoval(*P1);
EXPECT_EQ("y", getText(Hint1.RemoveRange.getAsRange(), *Context));
EXPECT_TRUE(Hint1.InsertFromRange.isInvalid());
EXPECT_TRUE(Hint1.CodeToInsert.empty());
};
Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
Expr *P0 = CE->getArg(0);
FixItHint Hint0 = createRemoval(*P0);
EXPECT_EQ("x + y", getText(Hint0.RemoveRange.getAsRange(), *Context));
Expr *P1 = CE->getArg(1);
FixItHint Hint1 = createRemoval(*P1);
EXPECT_EQ("y + x", getText(Hint1.RemoveRange.getAsRange(), *Context));
};
Visitor.runOver("void foo(int x, int y) { foo(x + y, y + x); }");
}
TEST(FixItTest, createRemovalWithMacro) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
FixItHint Hint = createRemoval(*CE);
EXPECT_EQ("FOO", getText(Hint.RemoveRange.getAsRange(), *Context));
EXPECT_TRUE(Hint.InsertFromRange.isInvalid());
EXPECT_TRUE(Hint.CodeToInsert.empty());
Expr *P0 = CE->getArg(0);
FixItHint Hint0 = createRemoval(*P0);
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:1:17>",
LocationToString(Hint0.RemoveRange.getBegin(), Context));
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:1:17>",
LocationToString(Hint0.RemoveRange.getEnd(), Context));
EXPECT_TRUE(Hint0.InsertFromRange.isInvalid());
EXPECT_TRUE(Hint0.CodeToInsert.empty());
Expr *P1 = CE->getArg(1);
FixItHint Hint1 = createRemoval(*P1);
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:1:20>",
LocationToString(Hint1.RemoveRange.getBegin(), Context));
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:1:20>",
LocationToString(Hint1.RemoveRange.getEnd(), Context));
EXPECT_TRUE(Hint1.InsertFromRange.isInvalid());
EXPECT_TRUE(Hint1.CodeToInsert.empty());
};
Visitor.runOver("#define FOO foo(1, 1)\n"
"void foo(int x, int y) { FOO; }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
FixItHint Hint = createRemoval(*CE);
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:1:37>",
LocationToString(Hint.RemoveRange.getBegin(), Context));
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:1:45>",
LocationToString(Hint.RemoveRange.getEnd(), Context));
EXPECT_TRUE(Hint.InsertFromRange.isInvalid());
EXPECT_TRUE(Hint.CodeToInsert.empty());
};
Visitor.runOver("#define FOO(x, y) (void)x; (void)y; foo(x, y);\n"
"void foo(int x, int y) { FOO(x,y) }");
}
TEST(FixItTest, createReplacement) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
FixItHint Hint0 = createReplacement(*P0, *P1, *Context);
FixItHint Hint1 = createReplacement(*P1, *P0, *Context);
// Validate Hint0 fields.
EXPECT_EQ("x", getText(Hint0.RemoveRange.getAsRange(), *Context));
EXPECT_TRUE(Hint0.InsertFromRange.isInvalid());
EXPECT_EQ(Hint0.CodeToInsert, "y");
// Validate Hint1 fields.
EXPECT_EQ("y", getText(Hint1.RemoveRange.getAsRange(), *Context));
EXPECT_TRUE(Hint1.InsertFromRange.isInvalid());
EXPECT_EQ(Hint1.CodeToInsert, "x");
};
Visitor.runOver("void foo(int x, int y) { foo(x, y); }");
Visitor.runOver("#define APPLY(f, x, y) f(x, y)\n"
"void foo(int x, int y) { APPLY(foo, x, y); }");
Visitor.runOver("#define APPLY(f, P) f(P)\n"
"#define PAIR(x, y) x, y\n"
"void foo(int x, int y) { APPLY(foo, PAIR(x, y)); }\n");
}
TEST(FixItTest, createReplacementWithMacro) {
CallsVisitor Visitor;
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
FixItHint Hint = createReplacement(*P0, *P1, *Context);
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:1:17>",
LocationToString(Hint.RemoveRange.getBegin(), Context));
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:1:17>",
LocationToString(Hint.RemoveRange.getEnd(), Context));
EXPECT_TRUE(Hint.InsertFromRange.isInvalid());
EXPECT_TRUE(Hint.CodeToInsert.empty());
};
Visitor.runOver("#define FOO foo(1, 1)\n"
"void foo(int x, int y) { FOO; }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
FixItHint Hint = createReplacement(*P0, *P1, *Context);
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:2:30>",
LocationToString(Hint.RemoveRange.getBegin(), Context));
EXPECT_EQ("input.cc:2:26 <Spelling=input.cc:2:30>",
LocationToString(Hint.RemoveRange.getEnd(), Context));
EXPECT_TRUE(Hint.InsertFromRange.isInvalid());
EXPECT_EQ("y", Hint.CodeToInsert);
};
Visitor.runOver("#define FOO(x, y) (void)x; (void)y; foo(x, y);\n"
"void foo(int x, int y) { FOO(x,y) }");
Visitor.OnCall = [](CallExpr *CE, ASTContext *Context) {
Expr *P0 = CE->getArg(0);
Expr *P1 = CE->getArg(1);
FixItHint Hint = createReplacement(*P0, *P1, *Context);
EXPECT_EQ("x + y", getText(Hint.RemoveRange.getAsRange(), *Context));
EXPECT_TRUE(Hint.InsertFromRange.isInvalid());
EXPECT_EQ("y + x", Hint.CodeToInsert);
};
Visitor.runOver("void foo(int x, int y) { foo(x + y, y + x); }");
}
} // end anonymous namespace
|