xref: /minix3/external/bsd/llvm/dist/clang/unittests/AST/StmtPrinterTest.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- unittests/AST/StmtPrinterTest.cpp --- Statement printer tests ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file contains tests for Stmt::printPretty() and related methods.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc // Search this file for WRONG to see test cases that are producing something
13f4a2713aSLionel Sambuc // completely wrong, invalid C++ or just misleading.
14f4a2713aSLionel Sambuc //
15f4a2713aSLionel Sambuc // These tests have a coding convention:
16f4a2713aSLionel Sambuc // * statements to be printed should be contained within a function named 'A'
17f4a2713aSLionel Sambuc //   unless it should have some special name (e.g., 'operator+');
18f4a2713aSLionel Sambuc // * additional helper declarations are 'Z', 'Y', 'X' and so on.
19f4a2713aSLionel Sambuc //
20f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
23f4a2713aSLionel Sambuc #include "clang/ASTMatchers/ASTMatchFinder.h"
24f4a2713aSLionel Sambuc #include "clang/Tooling/Tooling.h"
25f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
26f4a2713aSLionel Sambuc #include "gtest/gtest.h"
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc using namespace clang;
29f4a2713aSLionel Sambuc using namespace ast_matchers;
30f4a2713aSLionel Sambuc using namespace tooling;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc namespace {
33f4a2713aSLionel Sambuc 
PrintStmt(raw_ostream & Out,const ASTContext * Context,const Stmt * S)34f4a2713aSLionel Sambuc void PrintStmt(raw_ostream &Out, const ASTContext *Context, const Stmt *S) {
35*0a6a1f1dSLionel Sambuc   assert(S != nullptr && "Expected non-null Stmt");
36f4a2713aSLionel Sambuc   PrintingPolicy Policy = Context->getPrintingPolicy();
37*0a6a1f1dSLionel Sambuc   S->printPretty(Out, /*Helper*/ nullptr, Policy);
38f4a2713aSLionel Sambuc }
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc class PrintMatch : public MatchFinder::MatchCallback {
41f4a2713aSLionel Sambuc   SmallString<1024> Printed;
42f4a2713aSLionel Sambuc   unsigned NumFoundStmts;
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc public:
PrintMatch()45f4a2713aSLionel Sambuc   PrintMatch() : NumFoundStmts(0) {}
46f4a2713aSLionel Sambuc 
run(const MatchFinder::MatchResult & Result)47f4a2713aSLionel Sambuc   virtual void run(const MatchFinder::MatchResult &Result) {
48f4a2713aSLionel Sambuc     const Stmt *S = Result.Nodes.getStmtAs<Stmt>("id");
49f4a2713aSLionel Sambuc     if (!S)
50f4a2713aSLionel Sambuc       return;
51f4a2713aSLionel Sambuc     NumFoundStmts++;
52f4a2713aSLionel Sambuc     if (NumFoundStmts > 1)
53f4a2713aSLionel Sambuc       return;
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc     llvm::raw_svector_ostream Out(Printed);
56f4a2713aSLionel Sambuc     PrintStmt(Out, Result.Context, S);
57f4a2713aSLionel Sambuc   }
58f4a2713aSLionel Sambuc 
getPrinted() const59f4a2713aSLionel Sambuc   StringRef getPrinted() const {
60f4a2713aSLionel Sambuc     return Printed;
61f4a2713aSLionel Sambuc   }
62f4a2713aSLionel Sambuc 
getNumFoundStmts() const63f4a2713aSLionel Sambuc   unsigned getNumFoundStmts() const {
64f4a2713aSLionel Sambuc     return NumFoundStmts;
65f4a2713aSLionel Sambuc   }
66f4a2713aSLionel Sambuc };
67f4a2713aSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc template <typename T>
69*0a6a1f1dSLionel Sambuc ::testing::AssertionResult
PrintedStmtMatches(StringRef Code,const std::vector<std::string> & Args,const T & NodeMatch,StringRef ExpectedPrinted)70*0a6a1f1dSLionel Sambuc PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args,
71*0a6a1f1dSLionel Sambuc                    const T &NodeMatch, StringRef ExpectedPrinted) {
72f4a2713aSLionel Sambuc 
73f4a2713aSLionel Sambuc   PrintMatch Printer;
74f4a2713aSLionel Sambuc   MatchFinder Finder;
75f4a2713aSLionel Sambuc   Finder.addMatcher(NodeMatch, &Printer);
76*0a6a1f1dSLionel Sambuc   std::unique_ptr<FrontendActionFactory> Factory(
77*0a6a1f1dSLionel Sambuc       newFrontendActionFactory(&Finder));
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc   if (!runToolOnCodeWithArgs(Factory->create(), Code, Args))
80*0a6a1f1dSLionel Sambuc     return testing::AssertionFailure()
81*0a6a1f1dSLionel Sambuc       << "Parsing error in \"" << Code.str() << "\"";
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   if (Printer.getNumFoundStmts() == 0)
84f4a2713aSLionel Sambuc     return testing::AssertionFailure()
85f4a2713aSLionel Sambuc         << "Matcher didn't find any statements";
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc   if (Printer.getNumFoundStmts() > 1)
88f4a2713aSLionel Sambuc     return testing::AssertionFailure()
89f4a2713aSLionel Sambuc         << "Matcher should match only one statement "
90f4a2713aSLionel Sambuc            "(found " << Printer.getNumFoundStmts() << ")";
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc   if (Printer.getPrinted() != ExpectedPrinted)
93f4a2713aSLionel Sambuc     return ::testing::AssertionFailure()
94*0a6a1f1dSLionel Sambuc       << "Expected \"" << ExpectedPrinted.str() << "\", "
95*0a6a1f1dSLionel Sambuc          "got \"" << Printer.getPrinted().str() << "\"";
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   return ::testing::AssertionSuccess();
98f4a2713aSLionel Sambuc }
99f4a2713aSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc ::testing::AssertionResult
PrintedStmtCXX98Matches(StringRef Code,const StatementMatcher & NodeMatch,StringRef ExpectedPrinted)101*0a6a1f1dSLionel Sambuc PrintedStmtCXX98Matches(StringRef Code, const StatementMatcher &NodeMatch,
102*0a6a1f1dSLionel Sambuc                         StringRef ExpectedPrinted) {
103*0a6a1f1dSLionel Sambuc   std::vector<std::string> Args;
104*0a6a1f1dSLionel Sambuc   Args.push_back("-std=c++98");
105*0a6a1f1dSLionel Sambuc   Args.push_back("-Wno-unused-value");
106*0a6a1f1dSLionel Sambuc   return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted);
107*0a6a1f1dSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc 
PrintedStmtCXX98Matches(StringRef Code,StringRef ContainingFunction,StringRef ExpectedPrinted)109f4a2713aSLionel Sambuc ::testing::AssertionResult PrintedStmtCXX98Matches(
110f4a2713aSLionel Sambuc                                               StringRef Code,
111f4a2713aSLionel Sambuc                                               StringRef ContainingFunction,
112f4a2713aSLionel Sambuc                                               StringRef ExpectedPrinted) {
113f4a2713aSLionel Sambuc   std::vector<std::string> Args;
114f4a2713aSLionel Sambuc   Args.push_back("-std=c++98");
115f4a2713aSLionel Sambuc   Args.push_back("-Wno-unused-value");
116f4a2713aSLionel Sambuc   return PrintedStmtMatches(Code,
117f4a2713aSLionel Sambuc                             Args,
118f4a2713aSLionel Sambuc                             functionDecl(hasName(ContainingFunction),
119f4a2713aSLionel Sambuc                                          has(compoundStmt(has(stmt().bind("id"))))),
120f4a2713aSLionel Sambuc                             ExpectedPrinted);
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc 
123*0a6a1f1dSLionel Sambuc ::testing::AssertionResult
PrintedStmtCXX11Matches(StringRef Code,const StatementMatcher & NodeMatch,StringRef ExpectedPrinted)124*0a6a1f1dSLionel Sambuc PrintedStmtCXX11Matches(StringRef Code, const StatementMatcher &NodeMatch,
125*0a6a1f1dSLionel Sambuc                         StringRef ExpectedPrinted) {
126*0a6a1f1dSLionel Sambuc   std::vector<std::string> Args;
127*0a6a1f1dSLionel Sambuc   Args.push_back("-std=c++11");
128*0a6a1f1dSLionel Sambuc   Args.push_back("-Wno-unused-value");
129*0a6a1f1dSLionel Sambuc   return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted);
130*0a6a1f1dSLionel Sambuc }
131*0a6a1f1dSLionel Sambuc 
PrintedStmtMSMatches(StringRef Code,StringRef ContainingFunction,StringRef ExpectedPrinted)132f4a2713aSLionel Sambuc ::testing::AssertionResult PrintedStmtMSMatches(
133f4a2713aSLionel Sambuc                                               StringRef Code,
134f4a2713aSLionel Sambuc                                               StringRef ContainingFunction,
135f4a2713aSLionel Sambuc                                               StringRef ExpectedPrinted) {
136f4a2713aSLionel Sambuc   std::vector<std::string> Args;
137*0a6a1f1dSLionel Sambuc   Args.push_back("-target");
138*0a6a1f1dSLionel Sambuc   Args.push_back("i686-pc-win32");
139f4a2713aSLionel Sambuc   Args.push_back("-std=c++98");
140f4a2713aSLionel Sambuc   Args.push_back("-fms-extensions");
141f4a2713aSLionel Sambuc   Args.push_back("-Wno-unused-value");
142f4a2713aSLionel Sambuc   return PrintedStmtMatches(Code,
143f4a2713aSLionel Sambuc                             Args,
144f4a2713aSLionel Sambuc                             functionDecl(hasName(ContainingFunction),
145f4a2713aSLionel Sambuc                                          has(compoundStmt(has(stmt().bind("id"))))),
146f4a2713aSLionel Sambuc                             ExpectedPrinted);
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc 
149f4a2713aSLionel Sambuc } // unnamed namespace
150f4a2713aSLionel Sambuc 
TEST(StmtPrinter,TestIntegerLiteral)151f4a2713aSLionel Sambuc TEST(StmtPrinter, TestIntegerLiteral) {
152f4a2713aSLionel Sambuc   ASSERT_TRUE(PrintedStmtCXX98Matches(
153f4a2713aSLionel Sambuc     "void A() {"
154f4a2713aSLionel Sambuc     "  1, -1, 1U, 1u,"
155f4a2713aSLionel Sambuc     "  1L, 1l, -1L, 1UL, 1ul,"
156f4a2713aSLionel Sambuc     "  1LL, -1LL, 1ULL;"
157f4a2713aSLionel Sambuc     "}",
158f4a2713aSLionel Sambuc     "A",
159f4a2713aSLionel Sambuc     "1 , -1 , 1U , 1U , "
160f4a2713aSLionel Sambuc     "1L , 1L , -1L , 1UL , 1UL , "
161f4a2713aSLionel Sambuc     "1LL , -1LL , 1ULL"));
162f4a2713aSLionel Sambuc     // Should be: with semicolon
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc 
TEST(StmtPrinter,TestMSIntegerLiteral)165f4a2713aSLionel Sambuc TEST(StmtPrinter, TestMSIntegerLiteral) {
166f4a2713aSLionel Sambuc   ASSERT_TRUE(PrintedStmtMSMatches(
167f4a2713aSLionel Sambuc     "void A() {"
168f4a2713aSLionel Sambuc     "  1i8, -1i8, 1ui8, "
169f4a2713aSLionel Sambuc     "  1i16, -1i16, 1ui16, "
170f4a2713aSLionel Sambuc     "  1i32, -1i32, 1ui32, "
171f4a2713aSLionel Sambuc     "  1i64, -1i64, 1ui64;"
172f4a2713aSLionel Sambuc     "}",
173f4a2713aSLionel Sambuc     "A",
174*0a6a1f1dSLionel Sambuc     "1i8 , -1i8 , 1Ui8 , "
175*0a6a1f1dSLionel Sambuc     "1i16 , -1i16 , 1Ui16 , "
176f4a2713aSLionel Sambuc     "1 , -1 , 1U , "
177f4a2713aSLionel Sambuc     "1LL , -1LL , 1ULL"));
178f4a2713aSLionel Sambuc     // Should be: with semicolon
179f4a2713aSLionel Sambuc }
180f4a2713aSLionel Sambuc 
TEST(StmtPrinter,TestFloatingPointLiteral)181f4a2713aSLionel Sambuc TEST(StmtPrinter, TestFloatingPointLiteral) {
182f4a2713aSLionel Sambuc   ASSERT_TRUE(PrintedStmtCXX98Matches(
183f4a2713aSLionel Sambuc     "void A() { 1.0f, -1.0f, 1.0, -1.0, 1.0l, -1.0l; }",
184f4a2713aSLionel Sambuc     "A",
185f4a2713aSLionel Sambuc     "1.F , -1.F , 1. , -1. , 1.L , -1.L"));
186f4a2713aSLionel Sambuc     // Should be: with semicolon
187f4a2713aSLionel Sambuc }
188*0a6a1f1dSLionel Sambuc 
TEST(StmtPrinter,TestCXXConversionDeclImplicit)189*0a6a1f1dSLionel Sambuc TEST(StmtPrinter, TestCXXConversionDeclImplicit) {
190*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(PrintedStmtCXX98Matches(
191*0a6a1f1dSLionel Sambuc     "struct A {"
192*0a6a1f1dSLionel Sambuc       "operator void *();"
193*0a6a1f1dSLionel Sambuc       "A operator&(A);"
194*0a6a1f1dSLionel Sambuc     "};"
195*0a6a1f1dSLionel Sambuc     "void bar(void *);"
196*0a6a1f1dSLionel Sambuc     "void foo(A a, A b) {"
197*0a6a1f1dSLionel Sambuc     "  bar(a & b);"
198*0a6a1f1dSLionel Sambuc     "}",
199*0a6a1f1dSLionel Sambuc     memberCallExpr(anything()).bind("id"),
200*0a6a1f1dSLionel Sambuc     "a & b"));
201*0a6a1f1dSLionel Sambuc }
202*0a6a1f1dSLionel Sambuc 
TEST(StmtPrinter,TestCXXConversionDeclExplicit)203*0a6a1f1dSLionel Sambuc TEST(StmtPrinter, TestCXXConversionDeclExplicit) {
204*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(PrintedStmtCXX11Matches(
205*0a6a1f1dSLionel Sambuc     "struct A {"
206*0a6a1f1dSLionel Sambuc       "operator void *();"
207*0a6a1f1dSLionel Sambuc       "A operator&(A);"
208*0a6a1f1dSLionel Sambuc     "};"
209*0a6a1f1dSLionel Sambuc     "void bar(void *);"
210*0a6a1f1dSLionel Sambuc     "void foo(A a, A b) {"
211*0a6a1f1dSLionel Sambuc     "  auto x = (a & b).operator void *();"
212*0a6a1f1dSLionel Sambuc     "}",
213*0a6a1f1dSLionel Sambuc     memberCallExpr(anything()).bind("id"),
214*0a6a1f1dSLionel Sambuc     "(a & b)"));
215*0a6a1f1dSLionel Sambuc     // WRONG; Should be: (a & b).operator void *()
216*0a6a1f1dSLionel Sambuc }
217