1 //===- 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 
28 using namespace clang;
29 using namespace ast_matchers;
30 using namespace tooling;
31 
32 namespace {
33 
34 void PrintStmt(raw_ostream &Out, const ASTContext *Context, const Stmt *S) {
35   assert(S != nullptr && "Expected non-null Stmt");
36   PrintingPolicy Policy = Context->getPrintingPolicy();
37   S->printPretty(Out, /*Helper*/ nullptr, Policy);
38 }
39 
40 class PrintMatch : public MatchFinder::MatchCallback {
41   SmallString<1024> Printed;
42   unsigned NumFoundStmts;
43 
44 public:
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 
68 template <typename T>
69 ::testing::AssertionResult
70 PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args,
71                    const T &NodeMatch, StringRef ExpectedPrinted) {
72 
73   PrintMatch Printer;
74   MatchFinder Finder;
75   Finder.addMatcher(NodeMatch, &Printer);
76   std::unique_ptr<FrontendActionFactory> Factory(
77       newFrontendActionFactory(&Finder));
78 
79   if (!runToolOnCodeWithArgs(Factory->create(), Code, Args))
80     return testing::AssertionFailure()
81       << "Parsing error in \"" << Code.str() << "\"";
82 
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()
94       << "Expected \"" << ExpectedPrinted.str() << "\", "
95          "got \"" << Printer.getPrinted().str() << "\"";
96 
97   return ::testing::AssertionSuccess();
98 }
99 
100 ::testing::AssertionResult
101 PrintedStmtCXX98Matches(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 
109 ::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 
123 ::testing::AssertionResult
124 PrintedStmtCXX11Matches(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 
132 ::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 
149 TEST(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 
163 TEST(StmtPrinter, TestMSIntegerLiteral) {
164   ASSERT_TRUE(PrintedStmtMSMatches(
165     "void A() {"
166     "  1i8, -1i8, 1ui8, "
167     "  1i16, -1i16, 1ui16, "
168     "  1i32, -1i32, 1ui32, "
169     "  1i64, -1i64, 1ui64;"
170     "}",
171     "A",
172     "1 , -1 , 1U , "
173     "1 , -1 , 1U , "
174     "1L , -1L , 1UL , "
175     "1LL , -1LL , 1ULL"));
176     // Should be: with semicolon
177 }
178 
179 TEST(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 }
186 
187 TEST(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 
201 TEST(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 }
215