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 PrintingPolicy Policy = Context->getPrintingPolicy(); 36 S->printPretty(Out, /*Helper*/ nullptr, Policy); 37 } 38 39 class PrintMatch : public MatchFinder::MatchCallback { 40 SmallString<1024> Printed; 41 unsigned NumFoundStmts; 42 43 public: 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 67 template <typename T> 68 ::testing::AssertionResult 69 PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args, 70 const T &NodeMatch, StringRef ExpectedPrinted) { 71 72 PrintMatch Printer; 73 MatchFinder Finder; 74 Finder.addMatcher(NodeMatch, &Printer); 75 std::unique_ptr<FrontendActionFactory> Factory( 76 newFrontendActionFactory(&Finder)); 77 78 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) 79 return testing::AssertionFailure() 80 << "Parsing error in \"" << Code.str() << "\""; 81 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() 93 << "Expected \"" << ExpectedPrinted.str() << "\", " 94 "got \"" << Printer.getPrinted().str() << "\""; 95 96 return ::testing::AssertionSuccess(); 97 } 98 99 ::testing::AssertionResult 100 PrintedStmtCXX98Matches(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 108 ::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 122 ::testing::AssertionResult 123 PrintedStmtCXX11Matches(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 131 ::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 148 TEST(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 162 TEST(StmtPrinter, TestMSIntegerLiteral) { 163 ASSERT_TRUE(PrintedStmtMSMatches( 164 "void A() {" 165 " 1i8, -1i8, 1ui8, " 166 " 1i16, -1i16, 1ui16, " 167 " 1i32, -1i32, 1ui32, " 168 " 1i64, -1i64, 1ui64;" 169 "}", 170 "A", 171 "1 , -1 , 1U , " 172 "1 , -1 , 1U , " 173 "1L , -1L , 1UL , " 174 "1LL , -1LL , 1ULL")); 175 // Should be: with semicolon 176 } 177 178 TEST(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 } 185 186 TEST(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 200 TEST(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 } 214