Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 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*/ 0, 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 | |
Benjamin Kramer | 594802f | 2014-02-26 10:23:43 +0000 | [diff] [blame^] | 67 | template <typename T> |
| 68 | ::testing::AssertionResult |
| 69 | PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args, |
| 70 | const T &NodeMatch, StringRef ExpectedPrinted) { |
Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 71 | |
| 72 | PrintMatch Printer; |
| 73 | MatchFinder Finder; |
| 74 | Finder.addMatcher(NodeMatch, &Printer); |
| 75 | OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); |
| 76 | |
| 77 | if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) |
Saleem Abdulrasool | 8a8454b | 2014-01-25 20:04:44 +0000 | [diff] [blame] | 78 | return testing::AssertionFailure() |
| 79 | << "Parsing error in \"" << Code.str() << "\""; |
Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 80 | |
| 81 | if (Printer.getNumFoundStmts() == 0) |
| 82 | return testing::AssertionFailure() |
| 83 | << "Matcher didn't find any statements"; |
| 84 | |
| 85 | if (Printer.getNumFoundStmts() > 1) |
| 86 | return testing::AssertionFailure() |
| 87 | << "Matcher should match only one statement " |
| 88 | "(found " << Printer.getNumFoundStmts() << ")"; |
| 89 | |
| 90 | if (Printer.getPrinted() != ExpectedPrinted) |
| 91 | return ::testing::AssertionFailure() |
Saleem Abdulrasool | 8a8454b | 2014-01-25 20:04:44 +0000 | [diff] [blame] | 92 | << "Expected \"" << ExpectedPrinted.str() << "\", " |
| 93 | "got \"" << Printer.getPrinted().str() << "\""; |
Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 94 | |
| 95 | return ::testing::AssertionSuccess(); |
| 96 | } |
| 97 | |
Benjamin Kramer | 594802f | 2014-02-26 10:23:43 +0000 | [diff] [blame^] | 98 | ::testing::AssertionResult |
| 99 | PrintedStmtCXX98Matches(StringRef Code, const StatementMatcher &NodeMatch, |
| 100 | StringRef ExpectedPrinted) { |
| 101 | std::vector<std::string> Args; |
| 102 | Args.push_back("-std=c++98"); |
| 103 | Args.push_back("-Wno-unused-value"); |
| 104 | return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted); |
| 105 | } |
| 106 | |
Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 107 | ::testing::AssertionResult PrintedStmtCXX98Matches( |
| 108 | StringRef Code, |
| 109 | StringRef ContainingFunction, |
| 110 | StringRef ExpectedPrinted) { |
| 111 | std::vector<std::string> Args; |
| 112 | Args.push_back("-std=c++98"); |
| 113 | Args.push_back("-Wno-unused-value"); |
| 114 | return PrintedStmtMatches(Code, |
| 115 | Args, |
| 116 | functionDecl(hasName(ContainingFunction), |
| 117 | has(compoundStmt(has(stmt().bind("id"))))), |
| 118 | ExpectedPrinted); |
| 119 | } |
| 120 | |
Benjamin Kramer | 594802f | 2014-02-26 10:23:43 +0000 | [diff] [blame^] | 121 | ::testing::AssertionResult |
| 122 | PrintedStmtCXX11Matches(StringRef Code, const StatementMatcher &NodeMatch, |
| 123 | StringRef ExpectedPrinted) { |
| 124 | std::vector<std::string> Args; |
| 125 | Args.push_back("-std=c++11"); |
| 126 | Args.push_back("-Wno-unused-value"); |
| 127 | return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted); |
| 128 | } |
| 129 | |
Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 130 | ::testing::AssertionResult PrintedStmtMSMatches( |
| 131 | StringRef Code, |
| 132 | StringRef ContainingFunction, |
| 133 | StringRef ExpectedPrinted) { |
| 134 | std::vector<std::string> Args; |
| 135 | Args.push_back("-std=c++98"); |
| 136 | Args.push_back("-fms-extensions"); |
| 137 | Args.push_back("-Wno-unused-value"); |
| 138 | return PrintedStmtMatches(Code, |
| 139 | Args, |
| 140 | functionDecl(hasName(ContainingFunction), |
| 141 | has(compoundStmt(has(stmt().bind("id"))))), |
| 142 | ExpectedPrinted); |
| 143 | } |
| 144 | |
| 145 | } // unnamed namespace |
| 146 | |
| 147 | TEST(StmtPrinter, TestIntegerLiteral) { |
| 148 | ASSERT_TRUE(PrintedStmtCXX98Matches( |
| 149 | "void A() {" |
| 150 | " 1, -1, 1U, 1u," |
| 151 | " 1L, 1l, -1L, 1UL, 1ul," |
| 152 | " 1LL, -1LL, 1ULL;" |
| 153 | "}", |
| 154 | "A", |
| 155 | "1 , -1 , 1U , 1U , " |
| 156 | "1L , 1L , -1L , 1UL , 1UL , " |
| 157 | "1LL , -1LL , 1ULL")); |
| 158 | // Should be: with semicolon |
| 159 | } |
| 160 | |
| 161 | TEST(StmtPrinter, TestMSIntegerLiteral) { |
| 162 | ASSERT_TRUE(PrintedStmtMSMatches( |
| 163 | "void A() {" |
| 164 | " 1i8, -1i8, 1ui8, " |
| 165 | " 1i16, -1i16, 1ui16, " |
| 166 | " 1i32, -1i32, 1ui32, " |
NAKAMURA Takumi | c2b2b75 | 2012-11-29 10:22:40 +0000 | [diff] [blame] | 167 | " 1i64, -1i64, 1ui64;" |
Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 168 | "}", |
| 169 | "A", |
| 170 | "1 , -1 , 1U , " |
| 171 | "1 , -1 , 1U , " |
| 172 | "1L , -1L , 1UL , " |
NAKAMURA Takumi | c2b2b75 | 2012-11-29 10:22:40 +0000 | [diff] [blame] | 173 | "1LL , -1LL , 1ULL")); |
Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 174 | // Should be: with semicolon |
Dmitri Gribenko | 24bef9a | 2012-09-23 20:29:07 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | TEST(StmtPrinter, TestFloatingPointLiteral) { |
| 178 | ASSERT_TRUE(PrintedStmtCXX98Matches( |
| 179 | "void A() { 1.0f, -1.0f, 1.0, -1.0, 1.0l, -1.0l; }", |
| 180 | "A", |
| 181 | "1.F , -1.F , 1. , -1. , 1.L , -1.L")); |
| 182 | // Should be: with semicolon |
| 183 | } |
Benjamin Kramer | 594802f | 2014-02-26 10:23:43 +0000 | [diff] [blame^] | 184 | |
| 185 | TEST(StmtPrinter, TestCXXConversionDeclImplicit) { |
| 186 | ASSERT_TRUE(PrintedStmtCXX98Matches( |
| 187 | "struct A {" |
| 188 | "operator void *();" |
| 189 | "A operator&(A);" |
| 190 | "};" |
| 191 | "void bar(void *);" |
| 192 | "void foo(A a, A b) {" |
| 193 | " bar(a & b);" |
| 194 | "}", |
| 195 | memberCallExpr(anything()).bind("id"), |
| 196 | "a & b")); |
| 197 | } |
| 198 | |
| 199 | TEST(StmtPrinter, TestCXXConversionDeclExplicit) { |
| 200 | ASSERT_TRUE(PrintedStmtCXX11Matches( |
| 201 | "struct A {" |
| 202 | "operator void *();" |
| 203 | "A operator&(A);" |
| 204 | "};" |
| 205 | "void bar(void *);" |
| 206 | "void foo(A a, A b) {" |
| 207 | " auto x = (a & b).operator void *();" |
| 208 | "}", |
| 209 | memberCallExpr(anything()).bind("id"), |
| 210 | "(a & b)")); |
| 211 | // WRONG; Should be: (a & b).operator void *() |
| 212 | } |