blob: c75cbdefbf29bb333d7f0d764e38e8ac156e6c2b [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
34void PrintStmt(raw_ostream &Out, const ASTContext *Context, const Stmt *S) {
Richard Trieuddd01ce2014-06-09 22:53:25 +000035 assert(S != nullptr && "Expected non-null Stmt");
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000036 PrintingPolicy Policy = Context->getPrintingPolicy();
Craig Topper416fa342014-06-08 08:38:12 +000037 S->printPretty(Out, /*Helper*/ nullptr, Policy);
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000038}
39
40class PrintMatch : public MatchFinder::MatchCallback {
41 SmallString<1024> Printed;
42 unsigned NumFoundStmts;
43
44public:
45 PrintMatch() : NumFoundStmts(0) {}
46
47 virtual void run(const MatchFinder::MatchResult &Result) {
48 const Stmt *S = Result.Nodes.getStmtAs<Stmt>("id");
49 if (!S)
50 return;
51 NumFoundStmts++;
52 if (NumFoundStmts > 1)
53 return;
54
55 llvm::raw_svector_ostream Out(Printed);
56 PrintStmt(Out, Result.Context, S);
57 }
58
59 StringRef getPrinted() const {
60 return Printed;
61 }
62
63 unsigned getNumFoundStmts() const {
64 return NumFoundStmts;
65 }
66};
67
Benjamin Kramer594802f2014-02-26 10:23:43 +000068template <typename T>
69::testing::AssertionResult
70PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args,
71 const T &NodeMatch, StringRef ExpectedPrinted) {
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000072
73 PrintMatch Printer;
74 MatchFinder Finder;
75 Finder.addMatcher(NodeMatch, &Printer);
Ahmed Charlesb8984322014-03-07 20:03:18 +000076 std::unique_ptr<FrontendActionFactory> Factory(
77 newFrontendActionFactory(&Finder));
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000078
79 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args))
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +000080 return testing::AssertionFailure()
81 << "Parsing error in \"" << Code.str() << "\"";
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000082
83 if (Printer.getNumFoundStmts() == 0)
84 return testing::AssertionFailure()
85 << "Matcher didn't find any statements";
86
87 if (Printer.getNumFoundStmts() > 1)
88 return testing::AssertionFailure()
89 << "Matcher should match only one statement "
90 "(found " << Printer.getNumFoundStmts() << ")";
91
92 if (Printer.getPrinted() != ExpectedPrinted)
93 return ::testing::AssertionFailure()
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +000094 << "Expected \"" << ExpectedPrinted.str() << "\", "
95 "got \"" << Printer.getPrinted().str() << "\"";
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000096
97 return ::testing::AssertionSuccess();
98}
99
Benjamin Kramer594802f2014-02-26 10:23:43 +0000100::testing::AssertionResult
101PrintedStmtCXX98Matches(StringRef Code, const StatementMatcher &NodeMatch,
102 StringRef ExpectedPrinted) {
103 std::vector<std::string> Args;
104 Args.push_back("-std=c++98");
105 Args.push_back("-Wno-unused-value");
106 return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted);
107}
108
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000109::testing::AssertionResult PrintedStmtCXX98Matches(
110 StringRef Code,
111 StringRef ContainingFunction,
112 StringRef ExpectedPrinted) {
113 std::vector<std::string> Args;
114 Args.push_back("-std=c++98");
115 Args.push_back("-Wno-unused-value");
116 return PrintedStmtMatches(Code,
117 Args,
118 functionDecl(hasName(ContainingFunction),
119 has(compoundStmt(has(stmt().bind("id"))))),
120 ExpectedPrinted);
121}
122
Benjamin Kramer594802f2014-02-26 10:23:43 +0000123::testing::AssertionResult
124PrintedStmtCXX11Matches(StringRef Code, const StatementMatcher &NodeMatch,
125 StringRef ExpectedPrinted) {
126 std::vector<std::string> Args;
127 Args.push_back("-std=c++11");
128 Args.push_back("-Wno-unused-value");
129 return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted);
130}
131
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000132::testing::AssertionResult PrintedStmtMSMatches(
133 StringRef Code,
134 StringRef ContainingFunction,
135 StringRef ExpectedPrinted) {
136 std::vector<std::string> Args;
137 Args.push_back("-std=c++98");
138 Args.push_back("-fms-extensions");
139 Args.push_back("-Wno-unused-value");
140 return PrintedStmtMatches(Code,
141 Args,
142 functionDecl(hasName(ContainingFunction),
143 has(compoundStmt(has(stmt().bind("id"))))),
144 ExpectedPrinted);
145}
146
147} // unnamed namespace
148
149TEST(StmtPrinter, TestIntegerLiteral) {
150 ASSERT_TRUE(PrintedStmtCXX98Matches(
151 "void A() {"
152 " 1, -1, 1U, 1u,"
153 " 1L, 1l, -1L, 1UL, 1ul,"
154 " 1LL, -1LL, 1ULL;"
155 "}",
156 "A",
157 "1 , -1 , 1U , 1U , "
158 "1L , 1L , -1L , 1UL , 1UL , "
159 "1LL , -1LL , 1ULL"));
160 // Should be: with semicolon
161}
162
163TEST(StmtPrinter, TestMSIntegerLiteral) {
164 ASSERT_TRUE(PrintedStmtMSMatches(
165 "void A() {"
166 " 1i8, -1i8, 1ui8, "
167 " 1i16, -1i16, 1ui16, "
168 " 1i32, -1i32, 1ui32, "
NAKAMURA Takumic2b2b752012-11-29 10:22:40 +0000169 " 1i64, -1i64, 1ui64;"
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000170 "}",
171 "A",
172 "1 , -1 , 1U , "
173 "1 , -1 , 1U , "
174 "1L , -1L , 1UL , "
NAKAMURA Takumic2b2b752012-11-29 10:22:40 +0000175 "1LL , -1LL , 1ULL"));
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000176 // Should be: with semicolon
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000177}
178
179TEST(StmtPrinter, TestFloatingPointLiteral) {
180 ASSERT_TRUE(PrintedStmtCXX98Matches(
181 "void A() { 1.0f, -1.0f, 1.0, -1.0, 1.0l, -1.0l; }",
182 "A",
183 "1.F , -1.F , 1. , -1. , 1.L , -1.L"));
184 // Should be: with semicolon
185}
Benjamin Kramer594802f2014-02-26 10:23:43 +0000186
187TEST(StmtPrinter, TestCXXConversionDeclImplicit) {
188 ASSERT_TRUE(PrintedStmtCXX98Matches(
189 "struct A {"
190 "operator void *();"
191 "A operator&(A);"
192 "};"
193 "void bar(void *);"
194 "void foo(A a, A b) {"
195 " bar(a & b);"
196 "}",
197 memberCallExpr(anything()).bind("id"),
198 "a & b"));
199}
200
201TEST(StmtPrinter, TestCXXConversionDeclExplicit) {
202 ASSERT_TRUE(PrintedStmtCXX11Matches(
203 "struct A {"
204 "operator void *();"
205 "A operator&(A);"
206 "};"
207 "void bar(void *);"
208 "void foo(A a, A b) {"
209 " auto x = (a & b).operator void *();"
210 "}",
211 memberCallExpr(anything()).bind("id"),
212 "(a & b)"));
213 // WRONG; Should be: (a & b).operator void *()
214}