xref: /minix3/external/bsd/llvm/dist/clang/unittests/AST/NamedDeclPrinterTest.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===- unittests/AST/NamedDeclPrinterTest.cpp --- NamedDecl printer tests -===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This file contains tests for NamedDecl::printQualifiedName().
11*0a6a1f1dSLionel Sambuc //
12*0a6a1f1dSLionel Sambuc // These tests have a coding convention:
13*0a6a1f1dSLionel Sambuc // * declaration to be printed is named 'A' unless it should have some special
14*0a6a1f1dSLionel Sambuc // name (e.g., 'operator+');
15*0a6a1f1dSLionel Sambuc // * additional helper declarations are 'Z', 'Y', 'X' and so on.
16*0a6a1f1dSLionel Sambuc //
17*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTContext.h"
20*0a6a1f1dSLionel Sambuc #include "clang/ASTMatchers/ASTMatchFinder.h"
21*0a6a1f1dSLionel Sambuc #include "clang/Tooling/Tooling.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/ADT/SmallString.h"
23*0a6a1f1dSLionel Sambuc #include "gtest/gtest.h"
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc using namespace clang;
26*0a6a1f1dSLionel Sambuc using namespace ast_matchers;
27*0a6a1f1dSLionel Sambuc using namespace tooling;
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc namespace {
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc class PrintMatch : public MatchFinder::MatchCallback {
32*0a6a1f1dSLionel Sambuc   SmallString<1024> Printed;
33*0a6a1f1dSLionel Sambuc   unsigned NumFoundDecls;
34*0a6a1f1dSLionel Sambuc   bool SuppressUnwrittenScope;
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc public:
PrintMatch(bool suppressUnwrittenScope)37*0a6a1f1dSLionel Sambuc   explicit PrintMatch(bool suppressUnwrittenScope)
38*0a6a1f1dSLionel Sambuc     : NumFoundDecls(0), SuppressUnwrittenScope(suppressUnwrittenScope) {}
39*0a6a1f1dSLionel Sambuc 
run(const MatchFinder::MatchResult & Result)40*0a6a1f1dSLionel Sambuc   virtual void run(const MatchFinder::MatchResult &Result) {
41*0a6a1f1dSLionel Sambuc     const NamedDecl *ND = Result.Nodes.getNodeAs<NamedDecl>("id");
42*0a6a1f1dSLionel Sambuc     if (!ND)
43*0a6a1f1dSLionel Sambuc       return;
44*0a6a1f1dSLionel Sambuc     NumFoundDecls++;
45*0a6a1f1dSLionel Sambuc     if (NumFoundDecls > 1)
46*0a6a1f1dSLionel Sambuc       return;
47*0a6a1f1dSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc     llvm::raw_svector_ostream Out(Printed);
49*0a6a1f1dSLionel Sambuc     PrintingPolicy Policy = Result.Context->getPrintingPolicy();
50*0a6a1f1dSLionel Sambuc     Policy.SuppressUnwrittenScope = SuppressUnwrittenScope;
51*0a6a1f1dSLionel Sambuc     ND->printQualifiedName(Out, Policy);
52*0a6a1f1dSLionel Sambuc   }
53*0a6a1f1dSLionel Sambuc 
getPrinted() const54*0a6a1f1dSLionel Sambuc   StringRef getPrinted() const {
55*0a6a1f1dSLionel Sambuc     return Printed;
56*0a6a1f1dSLionel Sambuc   }
57*0a6a1f1dSLionel Sambuc 
getNumFoundDecls() const58*0a6a1f1dSLionel Sambuc   unsigned getNumFoundDecls() const {
59*0a6a1f1dSLionel Sambuc     return NumFoundDecls;
60*0a6a1f1dSLionel Sambuc   }
61*0a6a1f1dSLionel Sambuc };
62*0a6a1f1dSLionel Sambuc 
63*0a6a1f1dSLionel Sambuc ::testing::AssertionResult
PrintedNamedDeclMatches(StringRef Code,const std::vector<std::string> & Args,bool SuppressUnwrittenScope,const DeclarationMatcher & NodeMatch,StringRef ExpectedPrinted,StringRef FileName)64*0a6a1f1dSLionel Sambuc PrintedNamedDeclMatches(StringRef Code, const std::vector<std::string> &Args,
65*0a6a1f1dSLionel Sambuc                         bool SuppressUnwrittenScope,
66*0a6a1f1dSLionel Sambuc                         const DeclarationMatcher &NodeMatch,
67*0a6a1f1dSLionel Sambuc                         StringRef ExpectedPrinted, StringRef FileName) {
68*0a6a1f1dSLionel Sambuc   PrintMatch Printer(SuppressUnwrittenScope);
69*0a6a1f1dSLionel Sambuc   MatchFinder Finder;
70*0a6a1f1dSLionel Sambuc   Finder.addMatcher(NodeMatch, &Printer);
71*0a6a1f1dSLionel Sambuc   std::unique_ptr<FrontendActionFactory> Factory =
72*0a6a1f1dSLionel Sambuc       newFrontendActionFactory(&Finder);
73*0a6a1f1dSLionel Sambuc 
74*0a6a1f1dSLionel Sambuc   if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
75*0a6a1f1dSLionel Sambuc     return testing::AssertionFailure()
76*0a6a1f1dSLionel Sambuc         << "Parsing error in \"" << Code.str() << "\"";
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc   if (Printer.getNumFoundDecls() == 0)
79*0a6a1f1dSLionel Sambuc     return testing::AssertionFailure()
80*0a6a1f1dSLionel Sambuc         << "Matcher didn't find any named declarations";
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc   if (Printer.getNumFoundDecls() > 1)
83*0a6a1f1dSLionel Sambuc     return testing::AssertionFailure()
84*0a6a1f1dSLionel Sambuc         << "Matcher should match only one named declaration "
85*0a6a1f1dSLionel Sambuc            "(found " << Printer.getNumFoundDecls() << ")";
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc   if (Printer.getPrinted() != ExpectedPrinted)
88*0a6a1f1dSLionel Sambuc     return ::testing::AssertionFailure()
89*0a6a1f1dSLionel Sambuc         << "Expected \"" << ExpectedPrinted.str() << "\", "
90*0a6a1f1dSLionel Sambuc            "got \"" << Printer.getPrinted().str() << "\"";
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc   return ::testing::AssertionSuccess();
93*0a6a1f1dSLionel Sambuc }
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc ::testing::AssertionResult
PrintedNamedDeclCXX98Matches(StringRef Code,StringRef DeclName,StringRef ExpectedPrinted)96*0a6a1f1dSLionel Sambuc PrintedNamedDeclCXX98Matches(StringRef Code, StringRef DeclName,
97*0a6a1f1dSLionel Sambuc                              StringRef ExpectedPrinted) {
98*0a6a1f1dSLionel Sambuc   std::vector<std::string> Args(1, "-std=c++98");
99*0a6a1f1dSLionel Sambuc   return PrintedNamedDeclMatches(Code,
100*0a6a1f1dSLionel Sambuc                                  Args,
101*0a6a1f1dSLionel Sambuc                                  /*SuppressUnwrittenScope*/ false,
102*0a6a1f1dSLionel Sambuc                                  namedDecl(hasName(DeclName)).bind("id"),
103*0a6a1f1dSLionel Sambuc                                  ExpectedPrinted,
104*0a6a1f1dSLionel Sambuc                                  "input.cc");
105*0a6a1f1dSLionel Sambuc }
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc ::testing::AssertionResult
PrintedWrittenNamedDeclCXX11Matches(StringRef Code,StringRef DeclName,StringRef ExpectedPrinted)108*0a6a1f1dSLionel Sambuc PrintedWrittenNamedDeclCXX11Matches(StringRef Code, StringRef DeclName,
109*0a6a1f1dSLionel Sambuc                                     StringRef ExpectedPrinted) {
110*0a6a1f1dSLionel Sambuc   std::vector<std::string> Args(1, "-std=c++11");
111*0a6a1f1dSLionel Sambuc   return PrintedNamedDeclMatches(Code,
112*0a6a1f1dSLionel Sambuc                                  Args,
113*0a6a1f1dSLionel Sambuc                                  /*SuppressUnwrittenScope*/ true,
114*0a6a1f1dSLionel Sambuc                                  namedDecl(hasName(DeclName)).bind("id"),
115*0a6a1f1dSLionel Sambuc                                  ExpectedPrinted,
116*0a6a1f1dSLionel Sambuc                                  "input.cc");
117*0a6a1f1dSLionel Sambuc }
118*0a6a1f1dSLionel Sambuc 
119*0a6a1f1dSLionel Sambuc } // unnamed namespace
120*0a6a1f1dSLionel Sambuc 
TEST(NamedDeclPrinter,TestNamespace1)121*0a6a1f1dSLionel Sambuc TEST(NamedDeclPrinter, TestNamespace1) {
122*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(PrintedNamedDeclCXX98Matches(
123*0a6a1f1dSLionel Sambuc     "namespace { int A; }",
124*0a6a1f1dSLionel Sambuc     "A",
125*0a6a1f1dSLionel Sambuc     "(anonymous namespace)::A"));
126*0a6a1f1dSLionel Sambuc }
127*0a6a1f1dSLionel Sambuc 
TEST(NamedDeclPrinter,TestNamespace2)128*0a6a1f1dSLionel Sambuc TEST(NamedDeclPrinter, TestNamespace2) {
129*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(PrintedWrittenNamedDeclCXX11Matches(
130*0a6a1f1dSLionel Sambuc     "inline namespace Z { namespace { int A; } }",
131*0a6a1f1dSLionel Sambuc     "A",
132*0a6a1f1dSLionel Sambuc     "A"));
133*0a6a1f1dSLionel Sambuc }
134