blob: eac36d65ee47e8aba68e656387331e85f1145eaa [file] [log] [blame]
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +00001//===- 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
28using namespace clang;
29using namespace ast_matchers;
30using namespace tooling;
31
32namespace {
33
34void PrintStmt(raw_ostream &Out, const ASTContext *Context, const Stmt *S) {
35 PrintingPolicy Policy = Context->getPrintingPolicy();
36 S->printPretty(Out, /*Helper*/ 0, Policy);
37}
38
39class PrintMatch : public MatchFinder::MatchCallback {
40 SmallString<1024> Printed;
41 unsigned NumFoundStmts;
42
43public:
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::testing::AssertionResult PrintedStmtMatches(
68 StringRef Code,
69 const std::vector<std::string> &Args,
70 const DeclarationMatcher &NodeMatch,
71 StringRef ExpectedPrinted) {
72
73 PrintMatch Printer;
74 MatchFinder Finder;
75 Finder.addMatcher(NodeMatch, &Printer);
76 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
77
78 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args))
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +000079 return testing::AssertionFailure()
80 << "Parsing error in \"" << Code.str() << "\"";
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000081
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()
Saleem Abdulrasool8a8454b2014-01-25 20:04:44 +000093 << "Expected \"" << ExpectedPrinted.str() << "\", "
94 "got \"" << Printer.getPrinted().str() << "\"";
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +000095
96 return ::testing::AssertionSuccess();
97}
98
99::testing::AssertionResult PrintedStmtCXX98Matches(
100 StringRef Code,
101 StringRef ContainingFunction,
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,
107 Args,
108 functionDecl(hasName(ContainingFunction),
109 has(compoundStmt(has(stmt().bind("id"))))),
110 ExpectedPrinted);
111}
112
113::testing::AssertionResult PrintedStmtMSMatches(
114 StringRef Code,
115 StringRef ContainingFunction,
116 StringRef ExpectedPrinted) {
117 std::vector<std::string> Args;
118 Args.push_back("-std=c++98");
119 Args.push_back("-fms-extensions");
120 Args.push_back("-Wno-unused-value");
121 return PrintedStmtMatches(Code,
122 Args,
123 functionDecl(hasName(ContainingFunction),
124 has(compoundStmt(has(stmt().bind("id"))))),
125 ExpectedPrinted);
126}
127
128} // unnamed namespace
129
130TEST(StmtPrinter, TestIntegerLiteral) {
131 ASSERT_TRUE(PrintedStmtCXX98Matches(
132 "void A() {"
133 " 1, -1, 1U, 1u,"
134 " 1L, 1l, -1L, 1UL, 1ul,"
135 " 1LL, -1LL, 1ULL;"
136 "}",
137 "A",
138 "1 , -1 , 1U , 1U , "
139 "1L , 1L , -1L , 1UL , 1UL , "
140 "1LL , -1LL , 1ULL"));
141 // Should be: with semicolon
142}
143
144TEST(StmtPrinter, TestMSIntegerLiteral) {
145 ASSERT_TRUE(PrintedStmtMSMatches(
146 "void A() {"
147 " 1i8, -1i8, 1ui8, "
148 " 1i16, -1i16, 1ui16, "
149 " 1i32, -1i32, 1ui32, "
NAKAMURA Takumic2b2b752012-11-29 10:22:40 +0000150 " 1i64, -1i64, 1ui64;"
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000151 "}",
152 "A",
153 "1 , -1 , 1U , "
154 "1 , -1 , 1U , "
155 "1L , -1L , 1UL , "
NAKAMURA Takumic2b2b752012-11-29 10:22:40 +0000156 "1LL , -1LL , 1ULL"));
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000157 // Should be: with semicolon
Dmitri Gribenko24bef9a2012-09-23 20:29:07 +0000158}
159
160TEST(StmtPrinter, TestFloatingPointLiteral) {
161 ASSERT_TRUE(PrintedStmtCXX98Matches(
162 "void A() { 1.0f, -1.0f, 1.0, -1.0, 1.0l, -1.0l; }",
163 "A",
164 "1.F , -1.F , 1. , -1. , 1.L , -1.L"));
165 // Should be: with semicolon
166}