blob: d7265176d12e0f7a5b8bbd18da329de613e80859 [file] [log] [blame]
Dmitri Gribenko525394e2012-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) {
35 PrintingPolicy Policy = Context->getPrintingPolicy();
36 S->printPretty(Out, /*Helper*/ 0, Policy);
37}
38
39class PrintMatch : public MatchFinder::MatchCallback {
40 SmallString<1024> Printed;
41 unsigned NumFoundStmts;
42
43public:
44 PrintMatch() : NumFoundStmts(0) {}
45
46 virtual void run(const MatchFinder::MatchResult &Result) {
47 const Stmt *S = Result.Nodes.getStmtAs<Stmt>("id");
48 if (!S)
49 return;
50 NumFoundStmts++;
51 if (NumFoundStmts > 1)
52 return;
53
54 llvm::raw_svector_ostream Out(Printed);
55 PrintStmt(Out, Result.Context, S);
56 }
57
58 StringRef getPrinted() const {
59 return Printed;
60 }
61
62 unsigned getNumFoundStmts() const {
63 return NumFoundStmts;
64 }
65};
66
Stephen Hines651f13c2014-04-23 16:59:28 -070067template <typename T>
68::testing::AssertionResult
69PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args,
70 const T &NodeMatch, StringRef ExpectedPrinted) {
Dmitri Gribenko525394e2012-09-23 20:29:07 +000071
72 PrintMatch Printer;
73 MatchFinder Finder;
74 Finder.addMatcher(NodeMatch, &Printer);
Stephen Hines651f13c2014-04-23 16:59:28 -070075 std::unique_ptr<FrontendActionFactory> Factory(
76 newFrontendActionFactory(&Finder));
Dmitri Gribenko525394e2012-09-23 20:29:07 +000077
78 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args))
Stephen Hines651f13c2014-04-23 16:59:28 -070079 return testing::AssertionFailure()
80 << "Parsing error in \"" << Code.str() << "\"";
Dmitri Gribenko525394e2012-09-23 20:29:07 +000081
82 if (Printer.getNumFoundStmts() == 0)
83 return testing::AssertionFailure()
84 << "Matcher didn't find any statements";
85
86 if (Printer.getNumFoundStmts() > 1)
87 return testing::AssertionFailure()
88 << "Matcher should match only one statement "
89 "(found " << Printer.getNumFoundStmts() << ")";
90
91 if (Printer.getPrinted() != ExpectedPrinted)
92 return ::testing::AssertionFailure()
Stephen Hines651f13c2014-04-23 16:59:28 -070093 << "Expected \"" << ExpectedPrinted.str() << "\", "
94 "got \"" << Printer.getPrinted().str() << "\"";
Dmitri Gribenko525394e2012-09-23 20:29:07 +000095
96 return ::testing::AssertionSuccess();
97}
98
Stephen Hines651f13c2014-04-23 16:59:28 -070099::testing::AssertionResult
100PrintedStmtCXX98Matches(StringRef Code, const StatementMatcher &NodeMatch,
101 StringRef ExpectedPrinted) {
102 std::vector<std::string> Args;
103 Args.push_back("-std=c++98");
104 Args.push_back("-Wno-unused-value");
105 return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted);
106}
107
Dmitri Gribenko525394e2012-09-23 20:29:07 +0000108::testing::AssertionResult PrintedStmtCXX98Matches(
109 StringRef Code,
110 StringRef ContainingFunction,
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,
116 Args,
117 functionDecl(hasName(ContainingFunction),
118 has(compoundStmt(has(stmt().bind("id"))))),
119 ExpectedPrinted);
120}
121
Stephen Hines651f13c2014-04-23 16:59:28 -0700122::testing::AssertionResult
123PrintedStmtCXX11Matches(StringRef Code, const StatementMatcher &NodeMatch,
124 StringRef ExpectedPrinted) {
125 std::vector<std::string> Args;
126 Args.push_back("-std=c++11");
127 Args.push_back("-Wno-unused-value");
128 return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted);
129}
130
Dmitri Gribenko525394e2012-09-23 20:29:07 +0000131::testing::AssertionResult PrintedStmtMSMatches(
132 StringRef Code,
133 StringRef ContainingFunction,
134 StringRef ExpectedPrinted) {
135 std::vector<std::string> Args;
136 Args.push_back("-std=c++98");
137 Args.push_back("-fms-extensions");
138 Args.push_back("-Wno-unused-value");
139 return PrintedStmtMatches(Code,
140 Args,
141 functionDecl(hasName(ContainingFunction),
142 has(compoundStmt(has(stmt().bind("id"))))),
143 ExpectedPrinted);
144}
145
146} // unnamed namespace
147
148TEST(StmtPrinter, TestIntegerLiteral) {
149 ASSERT_TRUE(PrintedStmtCXX98Matches(
150 "void A() {"
151 " 1, -1, 1U, 1u,"
152 " 1L, 1l, -1L, 1UL, 1ul,"
153 " 1LL, -1LL, 1ULL;"
154 "}",
155 "A",
156 "1 , -1 , 1U , 1U , "
157 "1L , 1L , -1L , 1UL , 1UL , "
158 "1LL , -1LL , 1ULL"));
159 // Should be: with semicolon
160}
161
162TEST(StmtPrinter, TestMSIntegerLiteral) {
163 ASSERT_TRUE(PrintedStmtMSMatches(
164 "void A() {"
165 " 1i8, -1i8, 1ui8, "
166 " 1i16, -1i16, 1ui16, "
167 " 1i32, -1i32, 1ui32, "
NAKAMURA Takumi96794f12012-11-29 10:22:40 +0000168 " 1i64, -1i64, 1ui64;"
Dmitri Gribenko525394e2012-09-23 20:29:07 +0000169 "}",
170 "A",
171 "1 , -1 , 1U , "
172 "1 , -1 , 1U , "
173 "1L , -1L , 1UL , "
NAKAMURA Takumi96794f12012-11-29 10:22:40 +0000174 "1LL , -1LL , 1ULL"));
Dmitri Gribenko525394e2012-09-23 20:29:07 +0000175 // Should be: with semicolon
Dmitri Gribenko525394e2012-09-23 20:29:07 +0000176}
177
178TEST(StmtPrinter, TestFloatingPointLiteral) {
179 ASSERT_TRUE(PrintedStmtCXX98Matches(
180 "void A() { 1.0f, -1.0f, 1.0, -1.0, 1.0l, -1.0l; }",
181 "A",
182 "1.F , -1.F , 1. , -1. , 1.L , -1.L"));
183 // Should be: with semicolon
184}
Stephen Hines651f13c2014-04-23 16:59:28 -0700185
186TEST(StmtPrinter, TestCXXConversionDeclImplicit) {
187 ASSERT_TRUE(PrintedStmtCXX98Matches(
188 "struct A {"
189 "operator void *();"
190 "A operator&(A);"
191 "};"
192 "void bar(void *);"
193 "void foo(A a, A b) {"
194 " bar(a & b);"
195 "}",
196 memberCallExpr(anything()).bind("id"),
197 "a & b"));
198}
199
200TEST(StmtPrinter, TestCXXConversionDeclExplicit) {
201 ASSERT_TRUE(PrintedStmtCXX11Matches(
202 "struct A {"
203 "operator void *();"
204 "A operator&(A);"
205 "};"
206 "void bar(void *);"
207 "void foo(A a, A b) {"
208 " auto x = (a & b).operator void *();"
209 "}",
210 memberCallExpr(anything()).bind("id"),
211 "(a & b)"));
212 // WRONG; Should be: (a & b).operator void *()
213}