StmtPrinterTest.cpp revision 96794f1f38119691879e99a3f4298cd442130cf2
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
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))
79    return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
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()
92      << "Expected \"" << ExpectedPrinted << "\", "
93         "got \"" << Printer.getPrinted() << "\"";
94
95  return ::testing::AssertionSuccess();
96}
97
98::testing::AssertionResult PrintedStmtCXX98Matches(
99                                              StringRef Code,
100                                              StringRef ContainingFunction,
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,
106                            Args,
107                            functionDecl(hasName(ContainingFunction),
108                                         has(compoundStmt(has(stmt().bind("id"))))),
109                            ExpectedPrinted);
110}
111
112::testing::AssertionResult PrintedStmtMSMatches(
113                                              StringRef Code,
114                                              StringRef ContainingFunction,
115                                              StringRef ExpectedPrinted) {
116  std::vector<std::string> Args;
117  Args.push_back("-std=c++98");
118  Args.push_back("-fms-extensions");
119  Args.push_back("-Wno-unused-value");
120  return PrintedStmtMatches(Code,
121                            Args,
122                            functionDecl(hasName(ContainingFunction),
123                                         has(compoundStmt(has(stmt().bind("id"))))),
124                            ExpectedPrinted);
125}
126
127} // unnamed namespace
128
129TEST(StmtPrinter, TestIntegerLiteral) {
130  ASSERT_TRUE(PrintedStmtCXX98Matches(
131    "void A() {"
132    "  1, -1, 1U, 1u,"
133    "  1L, 1l, -1L, 1UL, 1ul,"
134    "  1LL, -1LL, 1ULL;"
135    "}",
136    "A",
137    "1 , -1 , 1U , 1U , "
138    "1L , 1L , -1L , 1UL , 1UL , "
139    "1LL , -1LL , 1ULL"));
140    // Should be: with semicolon
141}
142
143TEST(StmtPrinter, TestMSIntegerLiteral) {
144  ASSERT_TRUE(PrintedStmtMSMatches(
145    "void A() {"
146    "  1i8, -1i8, 1ui8, "
147    "  1i16, -1i16, 1ui16, "
148    "  1i32, -1i32, 1ui32, "
149    "  1i64, -1i64, 1ui64;"
150    "}",
151    "A",
152    "1 , -1 , 1U , "
153    "1 , -1 , 1U , "
154    "1L , -1L , 1UL , "
155    "1LL , -1LL , 1ULL"));
156    // Should be: with semicolon
157    // WRONG; all 128-bit literals should be printed as 128-bit.
158    // (This is because currently we do semantic analysis incorrectly.)
159}
160
161TEST(StmtPrinter, TestFloatingPointLiteral) {
162  ASSERT_TRUE(PrintedStmtCXX98Matches(
163    "void A() { 1.0f, -1.0f, 1.0, -1.0, 1.0l, -1.0l; }",
164    "A",
165    "1.F , -1.F , 1. , -1. , 1.L , -1.L"));
166    // Should be: with semicolon
167}
168
169