blob: a0644401a76aba07f8677d4ddb2716c2b2012c86 [file] [log] [blame]
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +00001//===- unittests/AST/StmtPrinterTest.cpp --- Statement printer tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains tests for Stmt::printPretty() and related methods.
11//
12// Search this file for WRONG to see test cases that are producing something
13// completely wrong, invalid C++ or just misleading.
14//
15// These tests have a coding convention:
16// * statements to be printed should be contained within a function named 'A'
17// unless it should have some special name (e.g., 'operator+');
18// * additional helper declarations are 'Z', 'Y', 'X' and so on.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/AST/ASTContext.h"
23#include "clang/ASTMatchers/ASTMatchFinder.h"
24#include "clang/Tooling/Tooling.h"
25#include "llvm/ADT/SmallString.h"
26#include "gtest/gtest.h"
27
28using namespace clang;
29using namespace ast_matchers;
30using namespace tooling;
31
32namespace {
33
Alex Lorenzf7579612017-10-26 00:56:54 +000034using PolicyAdjusterType =
35 Optional<llvm::function_ref<void(PrintingPolicy &Policy)>>;
36
37void PrintStmt(raw_ostream &Out, const ASTContext *Context, const Stmt *S,
38 PolicyAdjusterType PolicyAdjuster) {
Richard Trieuddd01ce2014-06-09 22:53:25 +000039 assert(S != nullptr && "Expected non-null Stmt");
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000040 PrintingPolicy Policy = Context->getPrintingPolicy();
Alex Lorenzf7579612017-10-26 00:56:54 +000041 if (PolicyAdjuster)
42 (*PolicyAdjuster)(Policy);
Craig Topper416fa342014-06-08 08:38:12 +000043 S->printPretty(Out, /*Helper*/ nullptr, Policy);
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000044}
45
46class PrintMatch : public MatchFinder::MatchCallback {
47 SmallString<1024> Printed;
48 unsigned NumFoundStmts;
Alex Lorenzf7579612017-10-26 00:56:54 +000049 PolicyAdjusterType PolicyAdjuster;
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000050
51public:
Alex Lorenzf7579612017-10-26 00:56:54 +000052 PrintMatch(PolicyAdjusterType PolicyAdjuster)
53 : NumFoundStmts(0), PolicyAdjuster(PolicyAdjuster) {}
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000054
Alexander Kornienko34eb2072015-04-11 02:00:23 +000055 void run(const MatchFinder::MatchResult &Result) override {
Alexander Kornienko7cdc7052016-12-13 16:19:34 +000056 const Stmt *S = Result.Nodes.getNodeAs<Stmt>("id");
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000057 if (!S)
58 return;
59 NumFoundStmts++;
60 if (NumFoundStmts > 1)
61 return;
62
63 llvm::raw_svector_ostream Out(Printed);
Alex Lorenzf7579612017-10-26 00:56:54 +000064 PrintStmt(Out, Result.Context, S, PolicyAdjuster);
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000065 }
66
67 StringRef getPrinted() const {
68 return Printed;
69 }
70
71 unsigned getNumFoundStmts() const {
72 return NumFoundStmts;
73 }
74};
75
Benjamin Kramer594802f2014-02-26 10:23:43 +000076template <typename T>
77::testing::AssertionResult
78PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args,
Alex Lorenzf7579612017-10-26 00:56:54 +000079 const T &NodeMatch, StringRef ExpectedPrinted,
80 PolicyAdjusterType PolicyAdjuster = None) {
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000081
Alex Lorenzf7579612017-10-26 00:56:54 +000082 PrintMatch Printer(PolicyAdjuster);
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000083 MatchFinder Finder;
84 Finder.addMatcher(NodeMatch, &Printer);
Ahmed Charlesb8984322014-03-07 20:03:18 +000085 std::unique_ptr<FrontendActionFactory> Factory(
86 newFrontendActionFactory(&Finder));
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000087
88 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args))
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +000089 return testing::AssertionFailure()
90 << "Parsing error in \"" << Code.str() << "\"";
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000091
92 if (Printer.getNumFoundStmts() == 0)
93 return testing::AssertionFailure()
94 << "Matcher didn't find any statements";
95
96 if (Printer.getNumFoundStmts() > 1)
97 return testing::AssertionFailure()
98 << "Matcher should match only one statement "
99 "(found " << Printer.getNumFoundStmts() << ")";
100
101 if (Printer.getPrinted() != ExpectedPrinted)
102 return ::testing::AssertionFailure()
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +0000103 << "Expected \"" << ExpectedPrinted.str() << "\", "
104 "got \"" << Printer.getPrinted().str() << "\"";
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000105
106 return ::testing::AssertionSuccess();
107}
108
Benjamin Kramer594802f2014-02-26 10:23:43 +0000109::testing::AssertionResult
110PrintedStmtCXX98Matches(StringRef Code, const StatementMatcher &NodeMatch,
111 StringRef ExpectedPrinted) {
112 std::vector<std::string> Args;
113 Args.push_back("-std=c++98");
114 Args.push_back("-Wno-unused-value");
115 return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted);
116}
117
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000118::testing::AssertionResult PrintedStmtCXX98Matches(
119 StringRef Code,
120 StringRef ContainingFunction,
121 StringRef ExpectedPrinted) {
122 std::vector<std::string> Args;
123 Args.push_back("-std=c++98");
124 Args.push_back("-Wno-unused-value");
125 return PrintedStmtMatches(Code,
126 Args,
127 functionDecl(hasName(ContainingFunction),
128 has(compoundStmt(has(stmt().bind("id"))))),
129 ExpectedPrinted);
130}
131
Benjamin Kramer594802f2014-02-26 10:23:43 +0000132::testing::AssertionResult
133PrintedStmtCXX11Matches(StringRef Code, const StatementMatcher &NodeMatch,
Alex Lorenzf7579612017-10-26 00:56:54 +0000134 StringRef ExpectedPrinted,
135 PolicyAdjusterType PolicyAdjuster = None) {
Benjamin Kramer594802f2014-02-26 10:23:43 +0000136 std::vector<std::string> Args;
137 Args.push_back("-std=c++11");
138 Args.push_back("-Wno-unused-value");
Alex Lorenzf7579612017-10-26 00:56:54 +0000139 return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted,
140 PolicyAdjuster);
Benjamin Kramer594802f2014-02-26 10:23:43 +0000141}
142
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000143::testing::AssertionResult PrintedStmtMSMatches(
144 StringRef Code,
145 StringRef ContainingFunction,
146 StringRef ExpectedPrinted) {
147 std::vector<std::string> Args;
David Majnemer65a407c2014-06-21 18:46:07 +0000148 Args.push_back("-target");
149 Args.push_back("i686-pc-win32");
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000150 Args.push_back("-std=c++98");
151 Args.push_back("-fms-extensions");
152 Args.push_back("-Wno-unused-value");
153 return PrintedStmtMatches(Code,
154 Args,
155 functionDecl(hasName(ContainingFunction),
156 has(compoundStmt(has(stmt().bind("id"))))),
157 ExpectedPrinted);
158}
159
Alex Lorenzf7579612017-10-26 00:56:54 +0000160::testing::AssertionResult
161PrintedStmtObjCMatches(StringRef Code, const StatementMatcher &NodeMatch,
162 StringRef ExpectedPrinted,
163 PolicyAdjusterType PolicyAdjuster = None) {
164 std::vector<std::string> Args;
165 Args.push_back("-ObjC");
166 Args.push_back("-fobjc-runtime=macosx-10.12.0");
167 return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted,
168 PolicyAdjuster);
169}
170
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000171} // unnamed namespace
172
173TEST(StmtPrinter, TestIntegerLiteral) {
174 ASSERT_TRUE(PrintedStmtCXX98Matches(
175 "void A() {"
176 " 1, -1, 1U, 1u,"
177 " 1L, 1l, -1L, 1UL, 1ul,"
178 " 1LL, -1LL, 1ULL;"
179 "}",
180 "A",
181 "1 , -1 , 1U , 1U , "
182 "1L , 1L , -1L , 1UL , 1UL , "
183 "1LL , -1LL , 1ULL"));
184 // Should be: with semicolon
185}
186
187TEST(StmtPrinter, TestMSIntegerLiteral) {
188 ASSERT_TRUE(PrintedStmtMSMatches(
189 "void A() {"
190 " 1i8, -1i8, 1ui8, "
191 " 1i16, -1i16, 1ui16, "
192 " 1i32, -1i32, 1ui32, "
NAKAMURA Takumic2b2b752012-11-29 10:22:40 +0000193 " 1i64, -1i64, 1ui64;"
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000194 "}",
195 "A",
David Majnemer65a407c2014-06-21 18:46:07 +0000196 "1i8 , -1i8 , 1Ui8 , "
197 "1i16 , -1i16 , 1Ui16 , "
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000198 "1 , -1 , 1U , "
NAKAMURA Takumic2b2b752012-11-29 10:22:40 +0000199 "1LL , -1LL , 1ULL"));
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000200 // Should be: with semicolon
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000201}
202
203TEST(StmtPrinter, TestFloatingPointLiteral) {
204 ASSERT_TRUE(PrintedStmtCXX98Matches(
205 "void A() { 1.0f, -1.0f, 1.0, -1.0, 1.0l, -1.0l; }",
206 "A",
207 "1.F , -1.F , 1. , -1. , 1.L , -1.L"));
208 // Should be: with semicolon
209}
Benjamin Kramer594802f2014-02-26 10:23:43 +0000210
211TEST(StmtPrinter, TestCXXConversionDeclImplicit) {
212 ASSERT_TRUE(PrintedStmtCXX98Matches(
213 "struct A {"
214 "operator void *();"
215 "A operator&(A);"
216 "};"
217 "void bar(void *);"
218 "void foo(A a, A b) {"
219 " bar(a & b);"
220 "}",
Aaron Ballman512fb642015-09-17 13:30:52 +0000221 cxxMemberCallExpr(anything()).bind("id"),
Benjamin Kramer594802f2014-02-26 10:23:43 +0000222 "a & b"));
223}
224
225TEST(StmtPrinter, TestCXXConversionDeclExplicit) {
226 ASSERT_TRUE(PrintedStmtCXX11Matches(
227 "struct A {"
228 "operator void *();"
229 "A operator&(A);"
230 "};"
231 "void bar(void *);"
232 "void foo(A a, A b) {"
233 " auto x = (a & b).operator void *();"
234 "}",
Aaron Ballman512fb642015-09-17 13:30:52 +0000235 cxxMemberCallExpr(anything()).bind("id"),
Benjamin Kramer594802f2014-02-26 10:23:43 +0000236 "(a & b)"));
237 // WRONG; Should be: (a & b).operator void *()
238}
Alex Lorenzf7579612017-10-26 00:56:54 +0000239
240TEST(StmtPrinter, TestNoImplicitBases) {
241 const char *CPPSource = R"(
242class A {
243 int field;
244 int member() { return field; }
245};
246)";
247 // No implicit 'this'.
248 ASSERT_TRUE(PrintedStmtCXX11Matches(
249 CPPSource, memberExpr(anything()).bind("id"), "field",
250 PolicyAdjusterType(
251 [](PrintingPolicy &PP) { PP.SuppressImplicitBase = true; })));
252 // Print implicit 'this'.
253 ASSERT_TRUE(PrintedStmtCXX11Matches(
254 CPPSource, memberExpr(anything()).bind("id"), "this->field"));
255
256 const char *ObjCSource = R"(
257@interface I {
258 int ivar;
259}
260@end
261@implementation I
262- (int) method {
263 return ivar;
264}
265@end
266 )";
267 // No implicit 'self'.
268 ASSERT_TRUE(PrintedStmtObjCMatches(ObjCSource, returnStmt().bind("id"),
269 "return ivar;\n",
270 PolicyAdjusterType([](PrintingPolicy &PP) {
271 PP.SuppressImplicitBase = true;
272 })));
273 // Print implicit 'self'.
274 ASSERT_TRUE(PrintedStmtObjCMatches(ObjCSource, returnStmt().bind("id"),
275 "return self->ivar;\n"));
276}