1 //===- unittests/AST/StmtPrinterTest.cpp --- Statement printer tests ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains tests for Stmt::printPretty() and related methods.
12 // Search this file for WRONG to see test cases that are producing something
13 // completely wrong, invalid C++ or just misleading.
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.
20 //===----------------------------------------------------------------------===//
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"
28 using namespace clang;
29 using namespace ast_matchers;
30 using namespace tooling;
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);
40 class PrintMatch : public MatchFinder::MatchCallback {
41 SmallString<1024> Printed;
42 unsigned NumFoundStmts;
45 PrintMatch() : NumFoundStmts(0) {}
47 void run(const MatchFinder::MatchResult &Result) override {
48 const Stmt *S = Result.Nodes.getNodeAs<Stmt>("id");
52 if (NumFoundStmts > 1)
55 llvm::raw_svector_ostream Out(Printed);
56 PrintStmt(Out, Result.Context, S);
59 StringRef getPrinted() const {
63 unsigned getNumFoundStmts() const {
69 ::testing::AssertionResult
70 PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args,
71 const T &NodeMatch, StringRef ExpectedPrinted) {
75 Finder.addMatcher(NodeMatch, &Printer);
76 std::unique_ptr<FrontendActionFactory> Factory(
77 newFrontendActionFactory(&Finder));
79 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args))
80 return testing::AssertionFailure()
81 << "Parsing error in \"" << Code.str() << "\"";
83 if (Printer.getNumFoundStmts() == 0)
84 return testing::AssertionFailure()
85 << "Matcher didn't find any statements";
87 if (Printer.getNumFoundStmts() > 1)
88 return testing::AssertionFailure()
89 << "Matcher should match only one statement "
90 "(found " << Printer.getNumFoundStmts() << ")";
92 if (Printer.getPrinted() != ExpectedPrinted)
93 return ::testing::AssertionFailure()
94 << "Expected \"" << ExpectedPrinted.str() << "\", "
95 "got \"" << Printer.getPrinted().str() << "\"";
97 return ::testing::AssertionSuccess();
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);
109 ::testing::AssertionResult PrintedStmtCXX98Matches(
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,
118 functionDecl(hasName(ContainingFunction),
119 has(compoundStmt(has(stmt().bind("id"))))),
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);
132 ::testing::AssertionResult PrintedStmtMSMatches(
134 StringRef ContainingFunction,
135 StringRef ExpectedPrinted) {
136 std::vector<std::string> Args;
137 Args.push_back("-target");
138 Args.push_back("i686-pc-win32");
139 Args.push_back("-std=c++98");
140 Args.push_back("-fms-extensions");
141 Args.push_back("-Wno-unused-value");
142 return PrintedStmtMatches(Code,
144 functionDecl(hasName(ContainingFunction),
145 has(compoundStmt(has(stmt().bind("id"))))),
149 } // unnamed namespace
151 TEST(StmtPrinter, TestIntegerLiteral) {
152 ASSERT_TRUE(PrintedStmtCXX98Matches(
155 " 1L, 1l, -1L, 1UL, 1ul,"
159 "1 , -1 , 1U , 1U , "
160 "1L , 1L , -1L , 1UL , 1UL , "
161 "1LL , -1LL , 1ULL"));
162 // Should be: with semicolon
165 TEST(StmtPrinter, TestMSIntegerLiteral) {
166 ASSERT_TRUE(PrintedStmtMSMatches(
169 " 1i16, -1i16, 1ui16, "
170 " 1i32, -1i32, 1ui32, "
171 " 1i64, -1i64, 1ui64;"
174 "1i8 , -1i8 , 1Ui8 , "
175 "1i16 , -1i16 , 1Ui16 , "
177 "1LL , -1LL , 1ULL"));
178 // Should be: with semicolon
181 TEST(StmtPrinter, TestFloatingPointLiteral) {
182 ASSERT_TRUE(PrintedStmtCXX98Matches(
183 "void A() { 1.0f, -1.0f, 1.0, -1.0, 1.0l, -1.0l; }",
185 "1.F , -1.F , 1. , -1. , 1.L , -1.L"));
186 // Should be: with semicolon
189 TEST(StmtPrinter, TestCXXConversionDeclImplicit) {
190 ASSERT_TRUE(PrintedStmtCXX98Matches(
196 "void foo(A a, A b) {"
199 cxxMemberCallExpr(anything()).bind("id"),
203 TEST(StmtPrinter, TestCXXConversionDeclExplicit) {
204 ASSERT_TRUE(PrintedStmtCXX11Matches(
210 "void foo(A a, A b) {"
211 " auto x = (a & b).operator void *();"
213 cxxMemberCallExpr(anything()).bind("id"),
215 // WRONG; Should be: (a & b).operator void *()