Coverage Report

Created: 2022-05-14 11:35

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/DeclPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the Decl::print method, which pretty prints the
10
// AST back out to C/Objective-C/C++/Objective-C++ code.
11
//
12
//===----------------------------------------------------------------------===//
13
#include "clang/AST/ASTContext.h"
14
#include "clang/AST/Attr.h"
15
#include "clang/AST/Decl.h"
16
#include "clang/AST/DeclCXX.h"
17
#include "clang/AST/DeclObjC.h"
18
#include "clang/AST/DeclTemplate.h"
19
#include "clang/AST/DeclVisitor.h"
20
#include "clang/AST/Expr.h"
21
#include "clang/AST/ExprCXX.h"
22
#include "clang/AST/PrettyPrinter.h"
23
#include "clang/Basic/Module.h"
24
#include "llvm/Support/raw_ostream.h"
25
using namespace clang;
26
27
namespace {
28
  class DeclPrinter : public DeclVisitor<DeclPrinter> {
29
    raw_ostream &Out;
30
    PrintingPolicy Policy;
31
    const ASTContext &Context;
32
    unsigned Indentation;
33
    bool PrintInstantiation;
34
35
18.3k
    raw_ostream& Indent() { return Indent(Indentation); }
36
    raw_ostream& Indent(unsigned Indentation);
37
    void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls);
38
39
    void Print(AccessSpecifier AS);
40
    void PrintConstructorInitializers(CXXConstructorDecl *CDecl,
41
                                      std::string &Proto);
42
43
    /// Print an Objective-C method type in parentheses.
44
    ///
45
    /// \param Quals The Objective-C declaration qualifiers.
46
    /// \param T The type to print.
47
    void PrintObjCMethodType(ASTContext &Ctx, Decl::ObjCDeclQualifier Quals,
48
                             QualType T);
49
50
    void PrintObjCTypeParams(ObjCTypeParamList *Params);
51
52
  public:
53
    DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy,
54
                const ASTContext &Context, unsigned Indentation = 0,
55
                bool PrintInstantiation = false)
56
        : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation),
57
33.5k
          PrintInstantiation(PrintInstantiation) {}
58
59
    void VisitDeclContext(DeclContext *DC, bool Indent = true);
60
61
    void VisitTranslationUnitDecl(TranslationUnitDecl *D);
62
    void VisitTypedefDecl(TypedefDecl *D);
63
    void VisitTypeAliasDecl(TypeAliasDecl *D);
64
    void VisitEnumDecl(EnumDecl *D);
65
    void VisitRecordDecl(RecordDecl *D);
66
    void VisitEnumConstantDecl(EnumConstantDecl *D);
67
    void VisitEmptyDecl(EmptyDecl *D);
68
    void VisitFunctionDecl(FunctionDecl *D);
69
    void VisitFriendDecl(FriendDecl *D);
70
    void VisitFieldDecl(FieldDecl *D);
71
    void VisitVarDecl(VarDecl *D);
72
    void VisitLabelDecl(LabelDecl *D);
73
    void VisitParmVarDecl(ParmVarDecl *D);
74
    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
75
    void VisitImportDecl(ImportDecl *D);
76
    void VisitStaticAssertDecl(StaticAssertDecl *D);
77
    void VisitNamespaceDecl(NamespaceDecl *D);
78
    void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
79
    void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
80
    void VisitCXXRecordDecl(CXXRecordDecl *D);
81
    void VisitLinkageSpecDecl(LinkageSpecDecl *D);
82
    void VisitTemplateDecl(const TemplateDecl *D);
83
    void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
84
    void VisitClassTemplateDecl(ClassTemplateDecl *D);
85
    void VisitClassTemplateSpecializationDecl(
86
                                            ClassTemplateSpecializationDecl *D);
87
    void VisitClassTemplatePartialSpecializationDecl(
88
                                     ClassTemplatePartialSpecializationDecl *D);
89
    void VisitObjCMethodDecl(ObjCMethodDecl *D);
90
    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
91
    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92
    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
93
    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
94
    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
95
    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
96
    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
97
    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
98
    void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
99
    void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
100
    void VisitUsingDecl(UsingDecl *D);
101
    void VisitUsingEnumDecl(UsingEnumDecl *D);
102
    void VisitUsingShadowDecl(UsingShadowDecl *D);
103
    void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
104
    void VisitOMPAllocateDecl(OMPAllocateDecl *D);
105
    void VisitOMPRequiresDecl(OMPRequiresDecl *D);
106
    void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
107
    void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
108
    void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
109
    void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP);
110
    void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *NTTP);
111
112
    void printTemplateParameters(const TemplateParameterList *Params,
113
                                 bool OmitTemplateKW = false);
114
    void printTemplateArguments(llvm::ArrayRef<TemplateArgument> Args,
115
                                const TemplateParameterList *Params);
116
    void printTemplateArguments(llvm::ArrayRef<TemplateArgumentLoc> Args,
117
                                const TemplateParameterList *Params);
118
    void prettyPrintAttributes(Decl *D);
119
    void prettyPrintPragmas(Decl *D);
120
    void printDeclType(QualType T, StringRef DeclName, bool Pack = false);
121
  };
122
}
123
124
void Decl::print(raw_ostream &Out, unsigned Indentation,
125
503
                 bool PrintInstantiation) const {
126
503
  print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation);
127
503
}
128
129
void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy,
130
24.9k
                 unsigned Indentation, bool PrintInstantiation) const {
131
24.9k
  DeclPrinter Printer(Out, Policy, getASTContext(), Indentation,
132
24.9k
                      PrintInstantiation);
133
24.9k
  Printer.Visit(const_cast<Decl*>(this));
134
24.9k
}
135
136
void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
137
1
                                  bool OmitTemplateKW) const {
138
1
  print(Out, Context, Context.getPrintingPolicy(), OmitTemplateKW);
139
1
}
140
141
void TemplateParameterList::print(raw_ostream &Out, const ASTContext &Context,
142
                                  const PrintingPolicy &Policy,
143
1
                                  bool OmitTemplateKW) const {
144
1
  DeclPrinter Printer(Out, Policy, Context);
145
1
  Printer.printTemplateParameters(this, OmitTemplateKW);
146
1
}
147
148
231
static QualType GetBaseType(QualType T) {
149
  // FIXME: This should be on the Type class!
150
231
  QualType BaseType = T;
151
316
  while (!BaseType->isSpecifierType()) {
152
86
    if (const PointerType *PTy = BaseType->getAs<PointerType>())
153
42
      BaseType = PTy->getPointeeType();
154
44
    else if (const ObjCObjectPointerType *OPT =
155
44
                 BaseType->getAs<ObjCObjectPointerType>())
156
0
      BaseType = OPT->getPointeeType();
157
44
    else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
158
0
      BaseType = BPy->getPointeeType();
159
44
    else if (const ArrayType *ATy = dyn_cast<ArrayType>(BaseType))
160
8
      BaseType = ATy->getElementType();
161
36
    else if (const FunctionType *FTy = BaseType->getAs<FunctionType>())
162
33
      BaseType = FTy->getReturnType();
163
3
    else if (const VectorType *VTy = BaseType->getAs<VectorType>())
164
0
      BaseType = VTy->getElementType();
165
3
    else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())
166
0
      BaseType = RTy->getPointeeType();
167
3
    else if (const AutoType *ATy = BaseType->getAs<AutoType>())
168
1
      BaseType = ATy->getDeducedType();
169
2
    else if (const ParenType *PTy = BaseType->getAs<ParenType>())
170
1
      BaseType = PTy->desugar();
171
1
    else
172
      // This must be a syntax error.
173
1
      break;
174
86
  }
175
231
  return BaseType;
176
231
}
177
178
14.3k
static QualType getDeclType(Decl* D) {
179
14.3k
  if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
180
464
    return TDD->getUnderlyingType();
181
13.8k
  if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
182
9.60k
    return VD->getType();
183
4.24k
  return QualType();
184
13.8k
}
185
186
void Decl::printGroup(Decl** Begin, unsigned NumDecls,
187
                      raw_ostream &Out, const PrintingPolicy &Policy,
188
14.8k
                      unsigned Indentation) {
189
14.8k
  if (NumDecls == 1) {
190
13.2k
    (*Begin)->print(Out, Policy, Indentation);
191
13.2k
    return;
192
13.2k
  }
193
194
1.56k
  Decl** End = Begin + NumDecls;
195
1.56k
  TagDecl* TD = dyn_cast<TagDecl>(*Begin);
196
1.56k
  if (TD)
197
252
    ++Begin;
198
199
1.56k
  PrintingPolicy SubPolicy(Policy);
200
201
1.56k
  bool isFirst = true;
202
8.23k
  for ( ; Begin != End; 
++Begin6.66k
) {
203
6.66k
    if (isFirst) {
204
1.56k
      if(TD)
205
252
        SubPolicy.IncludeTagDefinition = true;
206
1.56k
      SubPolicy.SuppressSpecifiers = false;
207
1.56k
      isFirst = false;
208
5.10k
    } else {
209
5.10k
      if (!isFirst) Out << ", ";
210
5.10k
      SubPolicy.IncludeTagDefinition = false;
211
5.10k
      SubPolicy.SuppressSpecifiers = true;
212
5.10k
    }
213
214
6.66k
    (*Begin)->print(Out, SubPolicy, Indentation);
215
6.66k
  }
216
1.56k
}
217
218
0
LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const {
219
  // Get the translation unit
220
0
  const DeclContext *DC = this;
221
0
  while (!DC->isTranslationUnit())
222
0
    DC = DC->getParent();
223
224
0
  ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
225
0
  DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0);
226
0
  Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
227
0
}
228
229
18.3k
raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
230
34.0k
  for (unsigned i = 0; i != Indentation; 
++i15.6k
)
231
15.6k
    Out << "  ";
232
18.3k
  return Out;
233
18.3k
}
234
235
42.7k
void DeclPrinter::prettyPrintAttributes(Decl *D) {
236
42.7k
  if (Policy.PolishForDeclaration)
237
2.13k
    return;
238
239
40.5k
  if (D->hasAttrs()) {
240
1.46k
    AttrVec &Attrs = D->getAttrs();
241
1.83k
    for (auto *A : Attrs) {
242
1.83k
      if (A->isInherited() || 
A->isImplicit()1.66k
)
243
1.36k
        continue;
244
470
      switch (A->getKind()) {
245
0
#define ATTR(X)
246
0
#define PRAGMA_SPELLING_ATTR(X) case attr::X:
247
0
#include "clang/Basic/AttrList.inc"
248
0
        break;
249
470
      default:
250
470
        A->printPretty(Out, Policy);
251
470
        break;
252
470
      }
253
470
    }
254
1.46k
  }
255
40.5k
}
256
257
35.2k
void DeclPrinter::prettyPrintPragmas(Decl *D) {
258
35.2k
  if (Policy.PolishForDeclaration)
259
1.78k
    return;
260
261
33.4k
  if (D->hasAttrs()) {
262
1.27k
    AttrVec &Attrs = D->getAttrs();
263
1.52k
    for (auto *A : Attrs) {
264
1.52k
      switch (A->getKind()) {
265
0
#define ATTR(X)
266
1.08k
#define PRAGMA_SPELLING_ATTR(X) case attr::X:
267
558
#include 
"clang/Basic/AttrList.inc"0
268
558
        A->printPretty(Out, Policy);
269
558
        Indent();
270
558
        break;
271
968
      default:
272
968
        break;
273
1.52k
      }
274
1.52k
    }
275
1.27k
  }
276
33.4k
}
277
278
27.6k
void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) {
279
  // Normally, a PackExpansionType is written as T[3]... (for instance, as a
280
  // template argument), but if it is the type of a declaration, the ellipsis
281
  // is placed before the name being declared.
282
27.6k
  if (auto *PET = T->getAs<PackExpansionType>()) {
283
14
    Pack = true;
284
14
    T = PET->getPattern();
285
14
  }
286
27.6k
  T.print(Out, Policy, (Pack ? 
"..."66
:
""27.6k
) + DeclName, Indentation);
287
27.6k
}
288
289
201
void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) {
290
201
  this->Indent();
291
201
  Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
292
201
  Out << ";\n";
293
201
  Decls.clear();
294
295
201
}
296
297
1.52k
void DeclPrinter::Print(AccessSpecifier AS) {
298
1.52k
  const auto AccessSpelling = getAccessSpelling(AS);
299
1.52k
  if (AccessSpelling.empty())
300
0
    llvm_unreachable("No access specifier!");
301
1.52k
  Out << AccessSpelling;
302
1.52k
}
303
304
void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl,
305
1.88k
                                               std::string &Proto) {
306
1.88k
  bool HasInitializerList = false;
307
2.16k
  for (const auto *BMInitializer : CDecl->inits()) {
308
2.16k
    if (BMInitializer->isInClassMemberInitializer())
309
0
      continue;
310
311
2.16k
    if (!HasInitializerList) {
312
1.66k
      Proto += " : ";
313
1.66k
      Out << Proto;
314
1.66k
      Proto.clear();
315
1.66k
      HasInitializerList = true;
316
1.66k
    } else
317
496
      Out << ", ";
318
319
2.16k
    if (BMInitializer->isAnyMemberInitializer()) {
320
1.54k
      FieldDecl *FD = BMInitializer->getAnyMember();
321
1.54k
      Out << *FD;
322
1.54k
    } else {
323
623
      Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy);
324
623
    }
325
326
2.16k
    Out << "(";
327
2.16k
    if (!BMInitializer->getInit()) {
328
      // Nothing to print
329
2.16k
    } else {
330
2.16k
      Expr *Init = BMInitializer->getInit();
331
2.16k
      if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
332
0
        Init = Tmp->getSubExpr();
333
334
2.16k
      Init = Init->IgnoreParens();
335
336
2.16k
      Expr *SimpleInit = nullptr;
337
2.16k
      Expr **Args = nullptr;
338
2.16k
      unsigned NumArgs = 0;
339
2.16k
      if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
340
349
        Args = ParenList->getExprs();
341
349
        NumArgs = ParenList->getNumExprs();
342
1.81k
      } else if (CXXConstructExpr *Construct =
343
1.81k
                     dyn_cast<CXXConstructExpr>(Init)) {
344
1.04k
        Args = Construct->getArgs();
345
1.04k
        NumArgs = Construct->getNumArgs();
346
1.04k
      } else
347
766
        SimpleInit = Init;
348
349
2.16k
      if (SimpleInit)
350
766
        SimpleInit->printPretty(Out, nullptr, Policy, Indentation, "\n",
351
766
                                &Context);
352
1.39k
      else {
353
2.24k
        for (unsigned I = 0; I != NumArgs; 
++I850
) {
354
850
          assert(Args[I] != nullptr && "Expected non-null Expr");
355
850
          if (isa<CXXDefaultArgExpr>(Args[I]))
356
0
            break;
357
358
850
          if (I)
359
43
            Out << ", ";
360
850
          Args[I]->printPretty(Out, nullptr, Policy, Indentation, "\n",
361
850
                               &Context);
362
850
        }
363
1.39k
      }
364
2.16k
    }
365
2.16k
    Out << ")";
366
2.16k
    if (BMInitializer->isPackExpansion())
367
0
      Out << "...";
368
2.16k
  }
369
1.88k
}
370
371
//----------------------------------------------------------------------------
372
// Common C declarations
373
//----------------------------------------------------------------------------
374
375
3.44k
void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
376
3.44k
  if (Policy.TerseOutput)
377
58
    return;
378
379
3.39k
  if (Indent)
380
2.40k
    Indentation += Policy.Indentation;
381
382
3.39k
  SmallVector<Decl*, 2> Decls;
383
3.39k
  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
384
27.6k
       D != DEnd; 
++D24.3k
) {
385
386
    // Don't print ObjCIvarDecls, as they are printed when visiting the
387
    // containing ObjCInterfaceDecl.
388
24.3k
    if (isa<ObjCIvarDecl>(*D))
389
46
      continue;
390
391
    // Skip over implicit declarations in pretty-printing mode.
392
24.2k
    if (D->isImplicit())
393
9.94k
      continue;
394
395
    // Don't print implicit specializations, as they are printed when visiting
396
    // corresponding templates.
397
14.3k
    if (auto FD = dyn_cast<FunctionDecl>(*D))
398
4.88k
      if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation &&
399
4.88k
          
!isa<ClassTemplateSpecializationDecl>(DC)736
)
400
0
        continue;
401
402
    // The next bits of code handle stuff like "struct {int x;} a,b"; we're
403
    // forced to merge the declarations because there's no other way to
404
    // refer to the struct in question.  When that struct is named instead, we
405
    // also need to merge to avoid splitting off a stand-alone struct
406
    // declaration that produces the warning ext_no_declarators in some
407
    // contexts.
408
    //
409
    // This limited merging is safe without a bunch of other checks because it
410
    // only merges declarations directly referring to the tag, not typedefs.
411
    //
412
    // Check whether the current declaration should be grouped with a previous
413
    // non-free-standing tag declaration.
414
14.3k
    QualType CurDeclType = getDeclType(*D);
415
14.3k
    if (!Decls.empty() && 
!CurDeclType.isNull()299
) {
416
231
      QualType BaseType = GetBaseType(CurDeclType);
417
231
      if (!BaseType.isNull() && isa<ElaboratedType>(BaseType) &&
418
231
          
cast<ElaboratedType>(BaseType)->getOwnedTagDecl() == Decls[0]158
) {
419
150
        Decls.push_back(*D);
420
150
        continue;
421
150
      }
422
231
    }
423
424
    // If we have a merged group waiting to be handled, handle it now.
425
14.1k
    if (!Decls.empty())
426
149
      ProcessDeclGroup(Decls);
427
428
    // If the current declaration is not a free standing declaration, save it
429
    // so we can merge it with the subsequent declaration(s) using it.
430
14.1k
    if (isa<TagDecl>(*D) && 
!cast<TagDecl>(*D)->isFreeStanding()1.19k
) {
431
201
      Decls.push_back(*D);
432
201
      continue;
433
201
    }
434
435
13.9k
    if (isa<AccessSpecDecl>(*D)) {
436
1.04k
      Indentation -= Policy.Indentation;
437
1.04k
      this->Indent();
438
1.04k
      Print(D->getAccess());
439
1.04k
      Out << ":\n";
440
1.04k
      Indentation += Policy.Indentation;
441
1.04k
      continue;
442
1.04k
    }
443
444
12.9k
    this->Indent();
445
12.9k
    Visit(*D);
446
447
    // FIXME: Need to be able to tell the DeclPrinter when
448
12.9k
    const char *Terminator = nullptr;
449
12.9k
    if (isa<OMPThreadPrivateDecl>(*D) || 
isa<OMPDeclareReductionDecl>(*D)12.7k
||
450
12.9k
        
isa<OMPDeclareMapperDecl>(*D)12.6k
||
isa<OMPRequiresDecl>(*D)12.5k
||
451
12.9k
        
isa<OMPAllocateDecl>(*D)12.5k
)
452
508
      Terminator = nullptr;
453
12.4k
    else if (isa<ObjCMethodDecl>(*D) && 
cast<ObjCMethodDecl>(*D)->hasBody()154
)
454
9
      Terminator = nullptr;
455
12.4k
    else if (auto FD = dyn_cast<FunctionDecl>(*D)) {
456
4.88k
      if (FD->isThisDeclarationADefinition())
457
4.03k
        Terminator = nullptr;
458
853
      else
459
853
        Terminator = ";";
460
7.51k
    } else if (auto TD = dyn_cast<FunctionTemplateDecl>(*D)) {
461
657
      if (TD->getTemplatedDecl()->isThisDeclarationADefinition())
462
530
        Terminator = nullptr;
463
127
      else
464
127
        Terminator = ";";
465
6.86k
    } else if (isa<NamespaceDecl>(*D) || 
isa<LinkageSpecDecl>(*D)6.73k
||
466
6.86k
             
isa<ObjCImplementationDecl>(*D)6.70k
||
467
6.86k
             
isa<ObjCInterfaceDecl>(*D)6.69k
||
468
6.86k
             
isa<ObjCProtocolDecl>(*D)6.65k
||
469
6.86k
             
isa<ObjCCategoryImplDecl>(*D)6.64k
||
470
6.86k
             
isa<ObjCCategoryDecl>(*D)6.64k
)
471
220
      Terminator = nullptr;
472
6.64k
    else if (isa<EnumConstantDecl>(*D)) {
473
237
      DeclContext::decl_iterator Next = D;
474
237
      ++Next;
475
237
      if (Next != DEnd)
476
134
        Terminator = ",";
477
237
    } else
478
6.40k
      Terminator = ";";
479
480
12.9k
    if (Terminator)
481
7.51k
      Out << Terminator;
482
12.9k
    if (!Policy.TerseOutput &&
483
12.9k
        ((isa<FunctionDecl>(*D) &&
484
12.9k
          
cast<FunctionDecl>(*D)->doesThisDeclarationHaveABody()4.88k
) ||
485
12.9k
         
(8.88k
isa<FunctionTemplateDecl>(*D)8.88k
&&
486
8.88k
          
cast<FunctionTemplateDecl>(*D)->getTemplatedDecl()->doesThisDeclarationHaveABody()657
)))
487
4.56k
      ; // StmtPrinter already added '\n' after CompoundStmt.
488
8.35k
    else
489
8.35k
      Out << "\n";
490
491
    // Declare target attribute is special one, natural spelling for the pragma
492
    // assumes "ending" construct so print it here.
493
12.9k
    if (D->hasAttr<OMPDeclareTargetDeclAttr>())
494
294
      Out << "#pragma omp end declare target\n";
495
12.9k
  }
496
497
3.39k
  if (!Decls.empty())
498
52
    ProcessDeclGroup(Decls);
499
500
3.39k
  if (Indent)
501
2.40k
    Indentation -= Policy.Indentation;
502
3.39k
}
503
504
901
void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
505
901
  VisitDeclContext(D, false);
506
901
}
507
508
516
void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
509
516
  if (!Policy.SuppressSpecifiers) {
510
516
    Out << "typedef ";
511
512
516
    if (D->isModulePrivate())
513
0
      Out << "__module_private__ ";
514
516
  }
515
516
  QualType Ty = D->getTypeSourceInfo()->getType();
516
516
  Ty.print(Out, Policy, D->getName(), Indentation);
517
516
  prettyPrintAttributes(D);
518
516
}
519
520
58
void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
521
58
  Out << "using " << *D;
522
58
  prettyPrintAttributes(D);
523
58
  Out << " = " << D->getTypeSourceInfo()->getType().getAsString(Policy);
524
58
}
525
526
161
void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
527
161
  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
528
0
    Out << "__module_private__ ";
529
161
  Out << "enum";
530
161
  if (D->isScoped()) {
531
35
    if (D->isScopedUsingClassTag())
532
35
      Out << " class";
533
0
    else
534
0
      Out << " struct";
535
35
  }
536
537
161
  prettyPrintAttributes(D);
538
539
161
  if (D->getDeclName())
540
139
    Out << ' ' << D->getDeclName();
541
542
161
  if (D->isFixed())
543
15
    Out << " : " << D->getIntegerType().stream(Policy);
544
545
161
  if (D->isCompleteDefinition()) {
546
152
    Out << " {\n";
547
152
    VisitDeclContext(D);
548
152
    Indent() << "}";
549
152
  }
550
161
}
551
552
169
void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
553
169
  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
554
0
    Out << "__module_private__ ";
555
169
  Out << D->getKindName();
556
557
169
  prettyPrintAttributes(D);
558
559
169
  if (D->getIdentifier())
560
154
    Out << ' ' << *D;
561
562
169
  if (D->isCompleteDefinition()) {
563
133
    Out << " {\n";
564
133
    VisitDeclContext(D);
565
133
    Indent() << "}";
566
133
  }
567
169
}
568
569
263
void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
570
263
  Out << *D;
571
263
  prettyPrintAttributes(D);
572
263
  if (Expr *Init = D->getInitExpr()) {
573
31
    Out << " = ";
574
31
    Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
575
31
  }
576
263
}
577
578
static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
579
                                   PrintingPolicy &Policy, unsigned Indentation,
580
59
                                   const ASTContext &Context) {
581
59
  std::string Proto = "explicit";
582
59
  llvm::raw_string_ostream EOut(Proto);
583
59
  if (ES.getExpr()) {
584
54
    EOut << "(";
585
54
    ES.getExpr()->printPretty(EOut, nullptr, Policy, Indentation, "\n",
586
54
                              &Context);
587
54
    EOut << ")";
588
54
  }
589
59
  EOut << " ";
590
59
  EOut.flush();
591
59
  Out << Proto;
592
59
}
593
594
8.03k
void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
595
8.03k
  if (!D->getDescribedFunctionTemplate() &&
596
8.03k
      
!D->isFunctionTemplateSpecialization()7.30k
)
597
6.54k
    prettyPrintPragmas(D);
598
599
8.03k
  if (D->isFunctionTemplateSpecialization())
600
753
    Out << "template<> ";
601
7.28k
  else if (!D->getDescribedFunctionTemplate()) {
602
6.54k
    for (unsigned I = 0, NumTemplateParams = D->getNumTemplateParameterLists();
603
6.55k
         I < NumTemplateParams; 
++I6
)
604
6
      printTemplateParameters(D->getTemplateParameterList(I));
605
6.54k
  }
606
607
8.03k
  CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D);
608
8.03k
  CXXConversionDecl *ConversionDecl = dyn_cast<CXXConversionDecl>(D);
609
8.03k
  CXXDeductionGuideDecl *GuideDecl = dyn_cast<CXXDeductionGuideDecl>(D);
610
8.03k
  if (!Policy.SuppressSpecifiers) {
611
8.02k
    switch (D->getStorageClass()) {
612
7.90k
    case SC_None: break;
613
21
    case SC_Extern: Out << "extern "; break;
614
108
    case SC_Static: Out << "static "; break;
615
0
    case SC_PrivateExtern: Out << "__private_extern__ "; break;
616
0
    case SC_Auto: case SC_Register:
617
0
      llvm_unreachable("invalid for functions");
618
8.02k
    }
619
620
8.02k
    if (D->isInlineSpecified())  
Out << "inline "117
;
621
8.02k
    if (D->isVirtualAsWritten()) 
Out << "virtual "11
;
622
8.02k
    if (D->isModulePrivate())    
Out << "__module_private__ "0
;
623
8.02k
    if (D->isConstexprSpecified() && 
!D->isExplicitlyDefaulted()161
)
624
156
      Out << "constexpr ";
625
8.02k
    if (D->isConsteval())        
Out << "consteval "0
;
626
8.02k
    ExplicitSpecifier ExplicitSpec = ExplicitSpecifier::getFromDecl(D);
627
8.02k
    if (ExplicitSpec.isSpecified())
628
59
      printExplicitSpecifier(ExplicitSpec, Out, Policy, Indentation, Context);
629
8.02k
  }
630
631
8.03k
  PrintingPolicy SubPolicy(Policy);
632
8.03k
  SubPolicy.SuppressSpecifiers = false;
633
8.03k
  std::string Proto;
634
635
8.03k
  if (Policy.FullyQualifiedName) {
636
5
    Proto += D->getQualifiedNameAsString();
637
8.03k
  } else {
638
8.03k
    llvm::raw_string_ostream OS(Proto);
639
8.03k
    if (!Policy.SuppressScope) {
640
7.81k
      if (const NestedNameSpecifier *NS = D->getQualifier()) {
641
62
        NS->print(OS, Policy);
642
62
      }
643
7.81k
    }
644
8.03k
    D->getNameInfo().printName(OS, Policy);
645
8.03k
  }
646
647
8.03k
  if (GuideDecl)
648
7
    Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString();
649
8.03k
  if (D->isFunctionTemplateSpecialization()) {
650
753
    llvm::raw_string_ostream POut(Proto);
651
753
    DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation);
652
753
    const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten();
653
753
    if (TArgAsWritten && 
!Policy.PrintCanonicalTypes19
)
654
19
      TArgPrinter.printTemplateArguments(TArgAsWritten->arguments(), nullptr);
655
734
    else if (const TemplateArgumentList *TArgs =
656
734
                 D->getTemplateSpecializationArgs())
657
734
      TArgPrinter.printTemplateArguments(TArgs->asArray(), nullptr);
658
753
  }
659
660
8.03k
  QualType Ty = D->getType();
661
8.03k
  while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
662
0
    Proto = '(' + Proto + ')';
663
0
    Ty = PT->getInnerType();
664
0
  }
665
666
8.03k
  if (const FunctionType *AFT = Ty->getAs<FunctionType>()) {
667
8.03k
    const FunctionProtoType *FT = nullptr;
668
8.03k
    if (D->hasWrittenPrototype())
669
7.88k
      FT = dyn_cast<FunctionProtoType>(AFT);
670
671
8.03k
    Proto += "(";
672
8.03k
    if (FT) {
673
7.88k
      llvm::raw_string_ostream POut(Proto);
674
7.88k
      DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation);
675
13.7k
      for (unsigned i = 0, e = D->getNumParams(); i != e; 
++i5.89k
) {
676
5.89k
        if (i) 
POut << ", "1.64k
;
677
5.89k
        ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
678
5.89k
      }
679
680
7.88k
      if (FT->isVariadic()) {
681
54
        if (D->getNumParams()) 
POut << ", "47
;
682
54
        POut << "...";
683
7.83k
      } else if (!D->getNumParams() && 
!Context.getLangOpts().CPlusPlus3.63k
) {
684
        // The function has a prototype, so it needs to retain the prototype
685
        // in C.
686
138
        POut << "void";
687
138
      }
688
7.88k
    } else 
if (151
D->doesThisDeclarationHaveABody()151
&&
!D->hasPrototype()111
) {
689
117
      for (unsigned i = 0, e = D->getNumParams(); i != e; 
++i6
) {
690
6
        if (i)
691
1
          Proto += ", ";
692
6
        Proto += D->getParamDecl(i)->getNameAsString();
693
6
      }
694
111
    }
695
696
8.03k
    Proto += ")";
697
698
8.03k
    if (FT) {
699
7.88k
      if (FT->isConst())
700
54
        Proto += " const";
701
7.88k
      if (FT->isVolatile())
702
2
        Proto += " volatile";
703
7.88k
      if (FT->isRestrict())
704
0
        Proto += " restrict";
705
706
7.88k
      switch (FT->getRefQualifier()) {
707
7.88k
      case RQ_None:
708
7.88k
        break;
709
1
      case RQ_LValue:
710
1
        Proto += " &";
711
1
        break;
712
1
      case RQ_RValue:
713
1
        Proto += " &&";
714
1
        break;
715
7.88k
      }
716
7.88k
    }
717
718
8.03k
    if (FT && 
FT->hasDynamicExceptionSpec()7.88k
) {
719
3
      Proto += " throw(";
720
3
      if (FT->getExceptionSpecType() == EST_MSAny)
721
0
        Proto += "...";
722
3
      else
723
6
        
for (unsigned I = 0, N = FT->getNumExceptions(); 3
I != N;
++I3
) {
724
3
          if (I)
725
1
            Proto += ", ";
726
727
3
          Proto += FT->getExceptionType(I).getAsString(SubPolicy);
728
3
        }
729
3
      Proto += ")";
730
8.03k
    } else if (FT && 
isNoexceptExceptionSpec(FT->getExceptionSpecType())7.88k
) {
731
57
      Proto += " noexcept";
732
57
      if (isComputedNoexcept(FT->getExceptionSpecType())) {
733
3
        Proto += "(";
734
3
        llvm::raw_string_ostream EOut(Proto);
735
3
        FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
736
3
                                           Indentation, "\n", &Context);
737
3
        EOut.flush();
738
3
        Proto += ")";
739
3
      }
740
57
    }
741
742
8.03k
    if (CDecl) {
743
2.02k
      if (!Policy.TerseOutput)
744
1.88k
        PrintConstructorInitializers(CDecl, Proto);
745
6.01k
    } else if (!ConversionDecl && 
!isa<CXXDestructorDecl>(D)5.84k
) {
746
5.73k
      if (FT && 
FT->hasTrailingReturn()5.58k
) {
747
14
        if (!GuideDecl)
748
7
          Out << "auto ";
749
14
        Out << Proto << " -> ";
750
14
        Proto.clear();
751
14
      }
752
5.73k
      AFT->getReturnType().print(Out, Policy, Proto);
753
5.73k
      Proto.clear();
754
5.73k
    }
755
8.03k
    Out << Proto;
756
757
8.03k
    if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) {
758
2
      Out << " requires ";
759
2
      TrailingRequiresClause->printPretty(Out, nullptr, SubPolicy, Indentation,
760
2
                                          "\n", &Context);
761
2
    }
762
8.03k
  } else {
763
0
    Ty.print(Out, Policy, Proto);
764
0
  }
765
766
8.03k
  prettyPrintAttributes(D);
767
768
8.03k
  if (D->isPure())
769
2
    Out << " = 0";
770
8.03k
  else if (D->isDeletedAsWritten())
771
1
    Out << " = delete";
772
8.03k
  else if (D->isExplicitlyDefaulted())
773
5
    Out << " = default";
774
8.02k
  else if (D->doesThisDeclarationHaveABody()) {
775
6.03k
    if (!Policy.TerseOutput) {
776
5.20k
      if (!D->hasPrototype() && 
D->getNumParams()109
) {
777
        // This is a K&R function definition, so we need to print the
778
        // parameters.
779
5
        Out << '\n';
780
5
        DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation);
781
5
        Indentation += Policy.Indentation;
782
11
        for (unsigned i = 0, e = D->getNumParams(); i != e; 
++i6
) {
783
6
          Indent();
784
6
          ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
785
6
          Out << ";\n";
786
6
        }
787
5
        Indentation -= Policy.Indentation;
788
5
      }
789
790
5.20k
      if (D->getBody())
791
5.20k
        D->getBody()->printPrettyControlled(Out, nullptr, SubPolicy, Indentation, "\n",
792
5.20k
                                  &Context);
793
5.20k
    } else {
794
824
      if (!Policy.TerseOutput && 
isa<CXXConstructorDecl>(*D)0
)
795
0
        Out << " {}";
796
824
    }
797
6.03k
  }
798
8.03k
}
799
800
47
void DeclPrinter::VisitFriendDecl(FriendDecl *D) {
801
47
  if (TypeSourceInfo *TSI = D->getFriendType()) {
802
9
    unsigned NumTPLists = D->getFriendTypeNumTemplateParameterLists();
803
9
    for (unsigned i = 0; i < NumTPLists; 
++i0
)
804
0
      printTemplateParameters(D->getFriendTypeTemplateParameterList(i));
805
9
    Out << "friend ";
806
9
    Out << " " << TSI->getType().getAsString(Policy);
807
9
  }
808
38
  else if (FunctionDecl *FD =
809
38
      dyn_cast<FunctionDecl>(D->getFriendDecl())) {
810
23
    Out << "friend ";
811
23
    VisitFunctionDecl(FD);
812
23
  }
813
15
  else if (FunctionTemplateDecl *FTD =
814
15
           dyn_cast<FunctionTemplateDecl>(D->getFriendDecl())) {
815
9
    Out << "friend ";
816
9
    VisitFunctionTemplateDecl(FTD);
817
9
  }
818
6
  else if (ClassTemplateDecl *CTD =
819
6
           dyn_cast<ClassTemplateDecl>(D->getFriendDecl())) {
820
6
    Out << "friend ";
821
6
    VisitRedeclarableTemplateDecl(CTD);
822
6
  }
823
47
}
824
825
2.49k
void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
826
  // FIXME: add printing of pragma attributes if required.
827
2.49k
  if (!Policy.SuppressSpecifiers && 
D->isMutable()2.48k
)
828
40
    Out << "mutable ";
829
2.49k
  if (!Policy.SuppressSpecifiers && 
D->isModulePrivate()2.48k
)
830
0
    Out << "__module_private__ ";
831
832
2.49k
  Out << D->getASTContext().getUnqualifiedObjCPointerType(D->getType()).
833
2.49k
         stream(Policy, D->getName(), Indentation);
834
835
2.49k
  if (D->isBitField()) {
836
92
    Out << " : ";
837
92
    D->getBitWidth()->printPretty(Out, nullptr, Policy, Indentation, "\n",
838
92
                                  &Context);
839
92
  }
840
841
2.49k
  Expr *Init = D->getInClassInitializer();
842
2.49k
  if (!Policy.SuppressInitializers && Init) {
843
244
    if (D->getInClassInitStyle() == ICIS_ListInit)
844
0
      Out << " ";
845
244
    else
846
244
      Out << " = ";
847
244
    Init->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
848
244
  }
849
2.49k
  prettyPrintAttributes(D);
850
2.49k
}
851
852
0
void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
853
0
  Out << *D << ":";
854
0
}
855
856
27.2k
void DeclPrinter::VisitVarDecl(VarDecl *D) {
857
27.2k
  prettyPrintPragmas(D);
858
859
27.2k
  QualType T = D->getTypeSourceInfo()
860
27.2k
    ? 
D->getTypeSourceInfo()->getType()27.1k
861
27.2k
    : 
D->getASTContext().getUnqualifiedObjCPointerType(D->getType())113
;
862
863
27.2k
  if (!Policy.SuppressSpecifiers) {
864
22.1k
    StorageClass SC = D->getStorageClass();
865
22.1k
    if (SC != SC_None)
866
2.20k
      Out << VarDecl::getStorageClassSpecifierString(SC) << " ";
867
868
22.1k
    switch (D->getTSCSpec()) {
869
22.1k
    case TSCS_unspecified:
870
22.1k
      break;
871
1
    case TSCS___thread:
872
1
      Out << "__thread ";
873
1
      break;
874
1
    case TSCS__Thread_local:
875
1
      Out << "_Thread_local ";
876
1
      break;
877
8
    case TSCS_thread_local:
878
8
      Out << "thread_local ";
879
8
      break;
880
22.1k
    }
881
882
22.1k
    if (D->isModulePrivate())
883
0
      Out << "__module_private__ ";
884
885
22.1k
    if (D->isConstexpr()) {
886
25
      Out << "constexpr ";
887
25
      T.removeLocalConst();
888
25
    }
889
22.1k
  }
890
891
27.2k
  printDeclType(T, (isa<ParmVarDecl>(D) && 
Policy.CleanUglifiedParameters5.91k
&&
892
27.2k
                    
D->getIdentifier()1
)
893
27.2k
                       ? 
D->getIdentifier()->deuglifiedName()1
894
27.2k
                       : 
D->getName()27.2k
);
895
27.2k
  Expr *Init = D->getInit();
896
27.2k
  if (!Policy.SuppressInitializers && 
Init27.1k
) {
897
11.4k
    bool ImplicitInit = false;
898
11.4k
    if (CXXConstructExpr *Construct =
899
11.4k
            dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
900
1.18k
      if (D->getInitStyle() == VarDecl::CallInit &&
901
1.18k
          
!Construct->isListInitialization()962
) {
902
962
        ImplicitInit = Construct->getNumArgs() == 0 ||
903
962
          
Construct->getArg(0)->isDefaultArgument()52
;
904
962
      }
905
1.18k
    }
906
11.4k
    if (!ImplicitInit) {
907
10.4k
      if ((D->getInitStyle() == VarDecl::CallInit) && 
!isa<ParenListExpr>(Init)53
)
908
52
        Out << "(";
909
10.4k
      else if (D->getInitStyle() == VarDecl::CInit) {
910
10.3k
        Out << " = ";
911
10.3k
      }
912
10.4k
      PrintingPolicy SubPolicy(Policy);
913
10.4k
      SubPolicy.SuppressSpecifiers = false;
914
10.4k
      SubPolicy.IncludeTagDefinition = false;
915
10.4k
      Init->printPretty(Out, nullptr, SubPolicy, Indentation, "\n", &Context);
916
10.4k
      if ((D->getInitStyle() == VarDecl::CallInit) && 
!isa<ParenListExpr>(Init)53
)
917
52
        Out << ")";
918
10.4k
    }
919
11.4k
  }
920
27.2k
  prettyPrintAttributes(D);
921
27.2k
}
922
923
5.91k
void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
924
5.91k
  VisitVarDecl(D);
925
5.91k
}
926
927
2
void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
928
2
  Out << "__asm (";
929
2
  D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation, "\n",
930
2
                                 &Context);
931
2
  Out << ")";
932
2
}
933
934
17
void DeclPrinter::VisitImportDecl(ImportDecl *D) {
935
17
  Out << "@import " << D->getImportedModule()->getFullModuleName()
936
17
      << ";\n";
937
17
}
938
939
4
void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
940
4
  Out << "static_assert(";
941
4
  D->getAssertExpr()->printPretty(Out, nullptr, Policy, Indentation, "\n",
942
4
                                  &Context);
943
4
  if (StringLiteral *SL = D->getMessage()) {
944
2
    Out << ", ";
945
2
    SL->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
946
2
  }
947
4
  Out << ")";
948
4
}
949
950
//----------------------------------------------------------------------------
951
// C++ declarations
952
//----------------------------------------------------------------------------
953
143
void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
954
143
  if (D->isInline())
955
1
    Out << "inline ";
956
957
143
  Out << "namespace ";
958
143
  if (D->getDeclName())
959
140
    Out << D->getDeclName() << ' ';
960
143
  Out << "{\n";
961
962
143
  VisitDeclContext(D);
963
143
  Indent() << "}";
964
143
}
965
966
5
void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
967
5
  Out << "using namespace ";
968
5
  if (D->getQualifier())
969
3
    D->getQualifier()->print(Out, Policy);
970
5
  Out << *D->getNominatedNamespaceAsWritten();
971
5
}
972
973
4
void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
974
4
  Out << "namespace " << *D << " = ";
975
4
  if (D->getQualifier())
976
3
    D->getQualifier()->print(Out, Policy);
977
4
  Out << *D->getAliasedNamespace();
978
4
}
979
980
18
void DeclPrinter::VisitEmptyDecl(EmptyDecl *D) {
981
18
  prettyPrintAttributes(D);
982
18
}
983
984
2.21k
void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
985
  // FIXME: add printing of pragma attributes if required.
986
2.21k
  if (!Policy.SuppressSpecifiers && D->isModulePrivate())
987
0
    Out << "__module_private__ ";
988
2.21k
  Out << D->getKindName();
989
990
2.21k
  prettyPrintAttributes(D);
991
992
2.21k
  if (D->getIdentifier()) {
993
2.17k
    Out << ' ' << *D;
994
995
2.17k
    if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
996
521
      ArrayRef<TemplateArgument> Args = S->getTemplateArgs().asArray();
997
521
      if (!Policy.PrintCanonicalTypes)
998
521
        if (const auto* TSI = S->getTypeAsWritten())
999
54
          if (const auto *TST =
1000
54
                  dyn_cast<TemplateSpecializationType>(TSI->getType()))
1001
54
            Args = TST->template_arguments();
1002
521
      printTemplateArguments(
1003
521
          Args, S->getSpecializedTemplate()->getTemplateParameters());
1004
521
    }
1005
2.17k
  }
1006
1007
2.21k
  if (D->isCompleteDefinition()) {
1008
    // Print the base classes
1009
2.09k
    if (D->getNumBases()) {
1010
513
      Out << " : ";
1011
513
      for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
1012
1.03k
             BaseEnd = D->bases_end(); Base != BaseEnd; 
++Base520
) {
1013
520
        if (Base != D->bases_begin())
1014
7
          Out << ", ";
1015
1016
520
        if (Base->isVirtual())
1017
13
          Out << "virtual ";
1018
1019
520
        AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
1020
520
        if (AS != AS_none) {
1021
485
          Print(AS);
1022
485
          Out << " ";
1023
485
        }
1024
520
        Out << Base->getType().getAsString(Policy);
1025
1026
520
        if (Base->isPackExpansion())
1027
1
          Out << "...";
1028
520
      }
1029
513
    }
1030
1031
    // Print the class definition
1032
    // FIXME: Doesn't print access specifiers, e.g., "public:"
1033
2.09k
    if (Policy.TerseOutput) {
1034
103
      Out << " {}";
1035
1.99k
    } else {
1036
1.99k
      Out << " {\n";
1037
1.99k
      VisitDeclContext(D);
1038
1.99k
      Indent() << "}";
1039
1.99k
    }
1040
2.09k
  }
1041
2.21k
}
1042
1043
28
void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1044
28
  const char *l;
1045
28
  if (D->getLanguage() == LinkageSpecDecl::lang_c)
1046
18
    l = "C";
1047
10
  else {
1048
10
    assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
1049
10
           "unknown language in linkage specification");
1050
0
    l = "C++";
1051
10
  }
1052
1053
0
  Out << "extern \"" << l << "\" ";
1054
28
  if (D->hasBraces()) {
1055
22
    Out << "{\n";
1056
22
    VisitDeclContext(D);
1057
22
    Indent() << "}";
1058
22
  } else
1059
6
    Visit(*D->decls_begin());
1060
28
}
1061
1062
void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params,
1063
1.34k
                                          bool OmitTemplateKW) {
1064
1.34k
  assert(Params);
1065
1066
1.34k
  if (!OmitTemplateKW)
1067
1.34k
    Out << "template ";
1068
1.34k
  Out << '<';
1069
1070
1.34k
  bool NeedComma = false;
1071
1.77k
  for (const Decl *Param : *Params) {
1072
1.77k
    if (Param->isImplicit())
1073
9
      continue;
1074
1075
1.76k
    if (NeedComma)
1076
422
      Out << ", ";
1077
1.33k
    else
1078
1.33k
      NeedComma = true;
1079
1080
1.76k
    if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
1081
1.28k
      VisitTemplateTypeParmDecl(TTP);
1082
1.28k
    } else 
if (auto 478
NTTP478
= dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1083
442
      VisitNonTypeTemplateParmDecl(NTTP);
1084
442
    } else 
if (auto 36
TTPD36
= dyn_cast<TemplateTemplateParmDecl>(Param)) {
1085
36
      VisitTemplateDecl(TTPD);
1086
      // FIXME: print the default argument, if present.
1087
36
    }
1088
1.76k
  }
1089
1090
1.34k
  Out << '>';
1091
1.34k
  if (!OmitTemplateKW)
1092
1.34k
    Out << ' ';
1093
1.34k
}
1094
1095
void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args,
1096
1.25k
                                         const TemplateParameterList *Params) {
1097
1.25k
  Out << "<";
1098
3.01k
  for (size_t I = 0, E = Args.size(); I < E; 
++I1.76k
) {
1099
1.76k
    if (I)
1100
509
      Out << ", ";
1101
1.76k
    if (!Params)
1102
1.14k
      Args[I].print(Policy, Out, /*IncludeType*/ true);
1103
619
    else
1104
619
      Args[I].print(Policy, Out,
1105
619
                    TemplateParameterList::shouldIncludeTypeForArgument(
1106
619
                        Policy, Params, I));
1107
1.76k
  }
1108
1.25k
  Out << ">";
1109
1.25k
}
1110
1111
void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
1112
19
                                         const TemplateParameterList *Params) {
1113
19
  Out << "<";
1114
38
  for (size_t I = 0, E = Args.size(); I < E; 
++I19
) {
1115
19
    if (I)
1116
0
      Out << ", ";
1117
19
    if (!Params)
1118
19
      Args[I].getArgument().print(Policy, Out, /*IncludeType*/ true);
1119
0
    else
1120
0
      Args[I].getArgument().print(
1121
0
          Policy, Out,
1122
0
          TemplateParameterList::shouldIncludeTypeForArgument(Policy, Params,
1123
0
                                                              I));
1124
19
  }
1125
19
  Out << ">";
1126
19
}
1127
1128
1.31k
void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
1129
1.31k
  printTemplateParameters(D->getTemplateParameters());
1130
1131
1.31k
  if (const TemplateTemplateParmDecl *TTP =
1132
1.31k
        dyn_cast<TemplateTemplateParmDecl>(D)) {
1133
37
    Out << "class";
1134
1135
37
    if (TTP->isParameterPack())
1136
2
      Out << " ...";
1137
35
    else if (TTP->getDeclName())
1138
34
      Out << ' ';
1139
1140
37
    if (TTP->getDeclName()) {
1141
35
      if (Policy.CleanUglifiedParameters && 
TTP->getIdentifier()2
)
1142
2
        Out << TTP->getIdentifier()->deuglifiedName();
1143
33
      else
1144
33
        Out << TTP->getDeclName();
1145
35
    }
1146
1.28k
  } else if (auto *TD = D->getTemplatedDecl())
1147
1.27k
    Visit(TD);
1148
8
  else if (const auto *Concept = dyn_cast<ConceptDecl>(D)) {
1149
8
    Out << "concept " << Concept->getName() << " = " ;
1150
8
    Concept->getConstraintExpr()->printPretty(Out, nullptr, Policy, Indentation,
1151
8
                                              "\n", &Context);
1152
8
  }
1153
1.31k
}
1154
1155
734
void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1156
734
  prettyPrintPragmas(D->getTemplatedDecl());
1157
  // Print any leading template parameter lists.
1158
734
  if (const FunctionDecl *FD = D->getTemplatedDecl()) {
1159
734
    for (unsigned I = 0, NumTemplateParams = FD->getNumTemplateParameterLists();
1160
735
         I < NumTemplateParams; 
++I1
)
1161
1
      printTemplateParameters(FD->getTemplateParameterList(I));
1162
734
  }
1163
734
  VisitRedeclarableTemplateDecl(D);
1164
  // Declare target attribute is special one, natural spelling for the pragma
1165
  // assumes "ending" construct so print it here.
1166
734
  if (D->getTemplatedDecl()->hasAttr<OMPDeclareTargetDeclAttr>())
1167
8
    Out << "#pragma omp end declare target\n";
1168
1169
  // Never print "instantiations" for deduction guides (they don't really
1170
  // have them).
1171
734
  if (PrintInstantiation &&
1172
734
      
!isa<CXXDeductionGuideDecl>(D->getTemplatedDecl())603
) {
1173
599
    FunctionDecl *PrevDecl = D->getTemplatedDecl();
1174
599
    const FunctionDecl *Def;
1175
599
    if (PrevDecl->isDefined(Def) && 
Def != PrevDecl487
)
1176
7
      return;
1177
592
    for (auto *I : D->specializations())
1178
747
      if (I->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) {
1179
706
        if (!PrevDecl->isThisDeclarationADefinition())
1180
101
          Out << ";\n";
1181
706
        Indent();
1182
706
        prettyPrintPragmas(I);
1183
706
        Visit(I);
1184
706
      }
1185
592
  }
1186
734
}
1187
1188
504
void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1189
504
  VisitRedeclarableTemplateDecl(D);
1190
1191
504
  if (PrintInstantiation) {
1192
385
    for (auto *I : D->specializations())
1193
496
      if (I->getSpecializationKind() == TSK_ImplicitInstantiation) {
1194
467
        if (D->isThisDeclarationADefinition())
1195
463
          Out << ";";
1196
467
        Out << "\n";
1197
467
        Indent();
1198
467
        Visit(I);
1199
467
      }
1200
385
  }
1201
504
}
1202
1203
void DeclPrinter::VisitClassTemplateSpecializationDecl(
1204
501
                                           ClassTemplateSpecializationDecl *D) {
1205
501
  Out << "template<> ";
1206
501
  VisitCXXRecordDecl(D);
1207
501
}
1208
1209
void DeclPrinter::VisitClassTemplatePartialSpecializationDecl(
1210
20
                                    ClassTemplatePartialSpecializationDecl *D) {
1211
20
  printTemplateParameters(D->getTemplateParameters());
1212
20
  VisitCXXRecordDecl(D);
1213
20
}
1214
1215
//----------------------------------------------------------------------------
1216
// Objective-C declarations
1217
//----------------------------------------------------------------------------
1218
1219
void DeclPrinter::PrintObjCMethodType(ASTContext &Ctx,
1220
                                      Decl::ObjCDeclQualifier Quals,
1221
2.31k
                                      QualType T) {
1222
2.31k
  Out << '(';
1223
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_In)
1224
58
    Out << "in ";
1225
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Inout)
1226
0
    Out << "inout ";
1227
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Out)
1228
0
    Out << "out ";
1229
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Bycopy)
1230
0
    Out << "bycopy ";
1231
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Byref)
1232
0
    Out << "byref ";
1233
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_Oneway)
1234
58
    Out << "oneway ";
1235
2.31k
  if (Quals & Decl::ObjCDeclQualifier::OBJC_TQ_CSNullability) {
1236
279
    if (auto nullability = AttributedType::stripOuterNullability(T))
1237
279
      Out << getNullabilitySpelling(*nullability, true) << ' ';
1238
279
  }
1239
1240
2.31k
  Out << Ctx.getUnqualifiedObjCPointerType(T).getAsString(Policy);
1241
2.31k
  Out << ')';
1242
2.31k
}
1243
1244
1
void DeclPrinter::PrintObjCTypeParams(ObjCTypeParamList *Params) {
1245
1
  Out << "<";
1246
1
  unsigned First = true;
1247
2
  for (auto *Param : *Params) {
1248
2
    if (First) {
1249
1
      First = false;
1250
1
    } else {
1251
1
      Out << ", ";
1252
1
    }
1253
1254
2
    switch (Param->getVariance()) {
1255
1
    case ObjCTypeParamVariance::Invariant:
1256
1
      break;
1257
1258
1
    case ObjCTypeParamVariance::Covariant:
1259
1
      Out << "__covariant ";
1260
1
      break;
1261
1262
0
    case ObjCTypeParamVariance::Contravariant:
1263
0
      Out << "__contravariant ";
1264
0
      break;
1265
2
    }
1266
1267
2
    Out << Param->getDeclName();
1268
1269
2
    if (Param->hasExplicitBound()) {
1270
2
      Out << " : " << Param->getUnderlyingType().getAsString(Policy);
1271
2
    }
1272
2
  }
1273
1
  Out << ">";
1274
1
}
1275
1276
1.55k
void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
1277
1.55k
  if (OMD->isInstanceMethod())
1278
1.26k
    Out << "- ";
1279
292
  else
1280
292
    Out << "+ ";
1281
1.55k
  if (!OMD->getReturnType().isNull()) {
1282
1.55k
    PrintObjCMethodType(OMD->getASTContext(), OMD->getObjCDeclQualifier(),
1283
1.55k
                        OMD->getReturnType());
1284
1.55k
  }
1285
1286
1.55k
  std::string name = OMD->getSelector().getAsString();
1287
1.55k
  std::string::size_type pos, lastPos = 0;
1288
1.55k
  for (const auto *PI : OMD->parameters()) {
1289
    // FIXME: selector is missing here!
1290
764
    pos = name.find_first_of(':', lastPos);
1291
764
    if (lastPos != 0)
1292
88
      Out << " ";
1293
764
    Out << name.substr(lastPos, pos - lastPos) << ':';
1294
764
    PrintObjCMethodType(OMD->getASTContext(),
1295
764
                        PI->getObjCDeclQualifier(),
1296
764
                        PI->getType());
1297
764
    Out << *PI;
1298
764
    lastPos = pos + 1;
1299
764
  }
1300
1301
1.55k
  if (OMD->param_begin() == OMD->param_end())
1302
879
    Out << name;
1303
1304
1.55k
  if (OMD->isVariadic())
1305
1
      Out << ", ...";
1306
1307
1.55k
  prettyPrintAttributes(OMD);
1308
1309
1.55k
  if (OMD->getBody() && 
!Policy.TerseOutput20
) {
1310
9
    Out << ' ';
1311
9
    OMD->getBody()->printPretty(Out, nullptr, Policy, Indentation, "\n",
1312
9
                                &Context);
1313
9
  }
1314
1.54k
  else if (Policy.PolishForDeclaration)
1315
73
    Out << ';';
1316
1.55k
}
1317
1318
8
void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
1319
8
  std::string I = OID->getNameAsString();
1320
8
  ObjCInterfaceDecl *SID = OID->getSuperClass();
1321
1322
8
  bool eolnOut = false;
1323
8
  if (SID)
1324
1
    Out << "@implementation " << I << " : " << *SID;
1325
7
  else
1326
7
    Out << "@implementation " << I;
1327
1328
8
  if (OID->ivar_size() > 0) {
1329
2
    Out << "{\n";
1330
2
    eolnOut = true;
1331
2
    Indentation += Policy.Indentation;
1332
5
    for (const auto *I : OID->ivars()) {
1333
5
      Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1334
5
                    getAsString(Policy) << ' ' << *I << ";\n";
1335
5
    }
1336
2
    Indentation -= Policy.Indentation;
1337
2
    Out << "}\n";
1338
2
  }
1339
6
  else if (SID || 
(OID->decls_begin() != OID->decls_end())5
) {
1340
5
    Out << "\n";
1341
5
    eolnOut = true;
1342
5
  }
1343
8
  VisitDeclContext(OID, false);
1344
8
  if (!eolnOut)
1345
1
    Out << "\n";
1346
8
  Out << "@end";
1347
8
}
1348
1349
75
void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
1350
75
  std::string I = OID->getNameAsString();
1351
75
  ObjCInterfaceDecl *SID = OID->getSuperClass();
1352
1353
75
  if (!OID->isThisDeclarationADefinition()) {
1354
3
    Out << "@class " << I;
1355
1356
3
    if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1357
0
      PrintObjCTypeParams(TypeParams);
1358
0
    }
1359
1360
3
    Out << ";";
1361
3
    return;
1362
3
  }
1363
72
  bool eolnOut = false;
1364
72
  Out << "@interface " << I;
1365
1366
72
  if (auto TypeParams = OID->getTypeParamListAsWritten()) {
1367
1
    PrintObjCTypeParams(TypeParams);
1368
1
  }
1369
1370
72
  if (SID)
1371
28
    Out << " : " << QualType(OID->getSuperClassType(), 0).getAsString(Policy);
1372
1373
  // Protocols?
1374
72
  const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
1375
72
  if (!Protocols.empty()) {
1376
6
    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1377
12
         E = Protocols.end(); I != E; 
++I6
)
1378
6
      Out << (I == Protocols.begin() ? '<' : 
','0
) << **I;
1379
6
    Out << "> ";
1380
6
  }
1381
1382
72
  if (OID->ivar_size() > 0) {
1383
21
    Out << "{\n";
1384
21
    eolnOut = true;
1385
21
    Indentation += Policy.Indentation;
1386
45
    for (const auto *I : OID->ivars()) {
1387
45
      Indent() << I->getASTContext()
1388
45
                      .getUnqualifiedObjCPointerType(I->getType())
1389
45
                      .getAsString(Policy) << ' ' << *I << ";\n";
1390
45
    }
1391
21
    Indentation -= Policy.Indentation;
1392
21
    Out << "}\n";
1393
21
  }
1394
51
  else if (SID || 
(OID->decls_begin() != OID->decls_end())35
) {
1395
33
    Out << "\n";
1396
33
    eolnOut = true;
1397
33
  }
1398
1399
72
  VisitDeclContext(OID, false);
1400
72
  if (!eolnOut)
1401
18
    Out << "\n";
1402
72
  Out << "@end";
1403
  // FIXME: implement the rest...
1404
72
}
1405
1406
13
void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
1407
13
  if (!PID->isThisDeclarationADefinition()) {
1408
3
    Out << "@protocol " << *PID << ";\n";
1409
3
    return;
1410
3
  }
1411
  // Protocols?
1412
10
  const ObjCList<ObjCProtocolDecl> &Protocols = PID->getReferencedProtocols();
1413
10
  if (!Protocols.empty()) {
1414
1
    Out << "@protocol " << *PID;
1415
1
    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1416
2
         E = Protocols.end(); I != E; 
++I1
)
1417
1
      Out << (I == Protocols.begin() ? '<' : 
','0
) << **I;
1418
1
    Out << ">\n";
1419
1
  } else
1420
9
    Out << "@protocol " << *PID << '\n';
1421
10
  VisitDeclContext(PID, false);
1422
10
  Out << "@end";
1423
10
}
1424
1425
3
void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
1426
3
  Out << "@implementation ";
1427
3
  if (const auto *CID = PID->getClassInterface())
1428
2
    Out << *CID;
1429
1
  else
1430
1
    Out << "<<error-type>>";
1431
3
  Out << '(' << *PID << ")\n";
1432
1433
3
  VisitDeclContext(PID, false);
1434
3
  Out << "@end";
1435
  // FIXME: implement the rest...
1436
3
}
1437
1438
11
void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
1439
11
  Out << "@interface ";
1440
11
  if (const auto *CID = PID->getClassInterface())
1441
10
    Out << *CID;
1442
1
  else
1443
1
    Out << "<<error-type>>";
1444
11
  if (auto TypeParams = PID->getTypeParamList()) {
1445
0
    PrintObjCTypeParams(TypeParams);
1446
0
  }
1447
11
  Out << "(" << *PID << ")\n";
1448
11
  if (PID->ivar_size() > 0) {
1449
1
    Out << "{\n";
1450
1
    Indentation += Policy.Indentation;
1451
1
    for (const auto *I : PID->ivars())
1452
1
      Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()).
1453
1
                    getAsString(Policy) << ' ' << *I << ";\n";
1454
1
    Indentation -= Policy.Indentation;
1455
1
    Out << "}\n";
1456
1
  }
1457
1458
11
  VisitDeclContext(PID, false);
1459
11
  Out << "@end";
1460
1461
  // FIXME: implement the rest...
1462
11
}
1463
1464
1
void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
1465
1
  Out << "@compatibility_alias " << *AID
1466
1
      << ' ' << *AID->getClassInterface() << ";\n";
1467
1
}
1468
1469
/// PrintObjCPropertyDecl - print a property declaration.
1470
///
1471
/// Print attributes in the following order:
1472
/// - class
1473
/// - nonatomic | atomic
1474
/// - assign | retain | strong | copy | weak | unsafe_unretained
1475
/// - readwrite | readonly
1476
/// - getter & setter
1477
/// - nullability
1478
48
void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
1479
48
  if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
1480
0
    Out << "@required\n";
1481
48
  else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1482
1
    Out << "@optional\n";
1483
1484
48
  QualType T = PDecl->getType();
1485
1486
48
  Out << "@property";
1487
48
  if (PDecl->getPropertyAttributes() != ObjCPropertyAttribute::kind_noattr) {
1488
48
    bool first = true;
1489
48
    Out << "(";
1490
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_class) {
1491
1
      Out << (first ? "" : 
", "0
) << "class";
1492
1
      first = false;
1493
1
    }
1494
1495
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_direct) {
1496
0
      Out << (first ? "" : ", ") << "direct";
1497
0
      first = false;
1498
0
    }
1499
1500
48
    if (PDecl->getPropertyAttributes() &
1501
48
        ObjCPropertyAttribute::kind_nonatomic) {
1502
19
      Out << (first ? "" : 
", "0
) << "nonatomic";
1503
19
      first = false;
1504
19
    }
1505
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic) {
1506
17
      Out << (first ? 
""16
:
", "1
) << "atomic";
1507
17
      first = false;
1508
17
    }
1509
1510
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_assign) {
1511
10
      Out << (first ? 
""3
:
", "7
) << "assign";
1512
10
      first = false;
1513
10
    }
1514
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_retain) {
1515
5
      Out << (first ? 
""3
:
", "2
) << "retain";
1516
5
      first = false;
1517
5
    }
1518
1519
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_strong) {
1520
1
      Out << (first ? 
""0
: ", ") << "strong";
1521
1
      first = false;
1522
1
    }
1523
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_copy) {
1524
21
      Out << (first ? 
""0
: ", ") << "copy";
1525
21
      first = false;
1526
21
    }
1527
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak) {
1528
1
      Out << (first ? 
""0
: ", ") << "weak";
1529
1
      first = false;
1530
1
    }
1531
48
    if (PDecl->getPropertyAttributes() &
1532
48
        ObjCPropertyAttribute::kind_unsafe_unretained) {
1533
7
      Out << (first ? 
""0
: ", ") << "unsafe_unretained";
1534
7
      first = false;
1535
7
    }
1536
1537
48
    if (PDecl->getPropertyAttributes() &
1538
48
        ObjCPropertyAttribute::kind_readwrite) {
1539
38
      Out << (first ? 
""0
: ", ") << "readwrite";
1540
38
      first = false;
1541
38
    }
1542
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) {
1543
10
      Out << (first ? 
""6
:
", "4
) << "readonly";
1544
10
      first = false;
1545
10
    }
1546
1547
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_getter) {
1548
16
      Out << (first ? 
""0
: ", ") << "getter = ";
1549
16
      PDecl->getGetterName().print(Out);
1550
16
      first = false;
1551
16
    }
1552
48
    if (PDecl->getPropertyAttributes() & ObjCPropertyAttribute::kind_setter) {
1553
15
      Out << (first ? 
""0
: ", ") << "setter = ";
1554
15
      PDecl->getSetterName().print(Out);
1555
15
      first = false;
1556
15
    }
1557
1558
48
    if (PDecl->getPropertyAttributes() &
1559
48
        ObjCPropertyAttribute::kind_nullability) {
1560
4
      if (auto nullability = AttributedType::stripOuterNullability(T)) {
1561
4
        if (*nullability == NullabilityKind::Unspecified &&
1562
4
            (PDecl->getPropertyAttributes() &
1563
1
             ObjCPropertyAttribute::kind_null_resettable)) {
1564
1
          Out << (first ? 
""0
: ", ") << "null_resettable";
1565
3
        } else {
1566
3
          Out << (first ? 
""0
: ", ")
1567
3
              << getNullabilitySpelling(*nullability, true);
1568
3
        }
1569
4
        first = false;
1570
4
      }
1571
4
    }
1572
1573
48
    (void) first; // Silence dead store warning due to idiomatic code.
1574
48
    Out << ")";
1575
48
  }
1576
48
  std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
1577
48
      getAsString(Policy);
1578
48
  Out << ' ' << TypeStr;
1579
48
  if (!StringRef(TypeStr).endswith("*"))
1580
28
    Out << ' ';
1581
48
  Out << *PDecl;
1582
48
  if (Policy.PolishForDeclaration)
1583
21
    Out << ';';
1584
48
}
1585
1586
6
void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
1587
6
  if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
1588
5
    Out << "@synthesize ";
1589
1
  else
1590
1
    Out << "@dynamic ";
1591
6
  Out << *PID->getPropertyDecl();
1592
6
  if (PID->getPropertyIvarDecl())
1593
5
    Out << '=' << *PID->getPropertyIvarDecl();
1594
6
}
1595
1596
23
void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
1597
23
  if (!D->isAccessDeclaration())
1598
23
    Out << "using ";
1599
23
  if (D->hasTypename())
1600
0
    Out << "typename ";
1601
23
  D->getQualifier()->print(Out, Policy);
1602
1603
  // Use the correct record name when the using declaration is used for
1604
  // inheriting constructors.
1605
23
  for (const auto *Shadow : D->shadows()) {
1606
23
    if (const auto *ConstructorShadow =
1607
23
            dyn_cast<ConstructorUsingShadowDecl>(Shadow)) {
1608
4
      assert(Shadow->getDeclContext() == ConstructorShadow->getDeclContext());
1609
0
      Out << *ConstructorShadow->getNominatedBaseClass();
1610
4
      return;
1611
4
    }
1612
23
  }
1613
19
  Out << *D;
1614
19
}
1615
1616
8
void DeclPrinter::VisitUsingEnumDecl(UsingEnumDecl *D) {
1617
8
  Out << "using enum " << D->getEnumDecl();
1618
8
}
1619
1620
void
1621
0
DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
1622
0
  Out << "using typename ";
1623
0
  D->getQualifier()->print(Out, Policy);
1624
0
  Out << D->getDeclName();
1625
0
}
1626
1627
5
void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1628
5
  if (!D->isAccessDeclaration())
1629
5
    Out << "using ";
1630
5
  D->getQualifier()->print(Out, Policy);
1631
5
  Out << D->getDeclName();
1632
5
}
1633
1634
0
void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1635
  // ignore
1636
0
}
1637
1638
452
void DeclPrinter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1639
452
  Out << "#pragma omp threadprivate";
1640
452
  if (!D->varlist_empty()) {
1641
452
    for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1642
452
                                                E = D->varlist_end();
1643
912
                                                I != E; 
++I460
) {
1644
460
      Out << (I == D->varlist_begin() ? 
'('452
:
','8
);
1645
460
      NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1646
460
      ND->printQualifiedName(Out);
1647
460
    }
1648
452
    Out << ")";
1649
452
  }
1650
452
}
1651
1652
182
void DeclPrinter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1653
182
  Out << "#pragma omp allocate";
1654
182
  if (!D->varlist_empty()) {
1655
182
    for (OMPAllocateDecl::varlist_iterator I = D->varlist_begin(),
1656
182
                                           E = D->varlist_end();
1657
380
         I != E; 
++I198
) {
1658
198
      Out << (I == D->varlist_begin() ? 
'('182
:
','16
);
1659
198
      NamedDecl *ND = cast<DeclRefExpr>(*I)->getDecl();
1660
198
      ND->printQualifiedName(Out);
1661
198
    }
1662
182
    Out << ")";
1663
182
  }
1664
182
  if (!D->clauselist_empty()) {
1665
94
    OMPClausePrinter Printer(Out, Policy);
1666
98
    for (OMPClause *C : D->clauselists()) {
1667
98
      Out << " ";
1668
98
      Printer.Visit(C);
1669
98
    }
1670
94
  }
1671
182
}
1672
1673
48
void DeclPrinter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1674
48
  Out << "#pragma omp requires ";
1675
48
  if (!D->clauselist_empty()) {
1676
48
    OMPClausePrinter Printer(Out, Policy);
1677
96
    for (auto I = D->clauselist_begin(), E = D->clauselist_end(); I != E; 
++I48
)
1678
48
      Printer.Visit(*I);
1679
48
  }
1680
48
}
1681
1682
92
void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1683
92
  if (!D->isInvalidDecl()) {
1684
92
    Out << "#pragma omp declare reduction (";
1685
92
    if (D->getDeclName().getNameKind() == DeclarationName::CXXOperatorName) {
1686
32
      const char *OpName =
1687
32
          getOperatorSpelling(D->getDeclName().getCXXOverloadedOperator());
1688
32
      assert(OpName && "not an overloaded operator");
1689
0
      Out << OpName;
1690
60
    } else {
1691
60
      assert(D->getDeclName().isIdentifier());
1692
0
      D->printName(Out);
1693
60
    }
1694
0
    Out << " : ";
1695
92
    D->getType().print(Out, Policy);
1696
92
    Out << " : ";
1697
92
    D->getCombiner()->printPretty(Out, nullptr, Policy, 0, "\n", &Context);
1698
92
    Out << ")";
1699
92
    if (auto *Init = D->getInitializer()) {
1700
56
      Out << " initializer(";
1701
56
      switch (D->getInitializerKind()) {
1702
0
      case OMPDeclareReductionDecl::DirectInit:
1703
0
        Out << "omp_priv(";
1704
0
        break;
1705
40
      case OMPDeclareReductionDecl::CopyInit:
1706
40
        Out << "omp_priv = ";
1707
40
        break;
1708
16
      case OMPDeclareReductionDecl::CallInit:
1709
16
        break;
1710
56
      }
1711
56
      Init->printPretty(Out, nullptr, Policy, 0, "\n", &Context);
1712
56
      if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit)
1713
0
        Out << ")";
1714
56
      Out << ")";
1715
56
    }
1716
92
  }
1717
92
}
1718
1719
116
void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1720
116
  if (!D->isInvalidDecl()) {
1721
116
    Out << "#pragma omp declare mapper (";
1722
116
    D->printName(Out);
1723
116
    Out << " : ";
1724
116
    D->getType().print(Out, Policy);
1725
116
    Out << " ";
1726
116
    Out << D->getVarName();
1727
116
    Out << ")";
1728
116
    if (!D->clauselist_empty()) {
1729
116
      OMPClausePrinter Printer(Out, Policy);
1730
128
      for (auto *C : D->clauselists()) {
1731
128
        Out << " ";
1732
128
        Printer.Visit(C);
1733
128
      }
1734
116
    }
1735
116
  }
1736
116
}
1737
1738
0
void DeclPrinter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1739
0
  D->getInit()->printPretty(Out, nullptr, Policy, Indentation, "\n", &Context);
1740
0
}
1741
1742
1.29k
void DeclPrinter::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *TTP) {
1743
1.29k
  if (const TypeConstraint *TC = TTP->getTypeConstraint())
1744
2
    TC->print(Out, Policy);
1745
1.29k
  else if (TTP->wasDeclaredWithTypename())
1746
825
    Out << "typename";
1747
466
  else
1748
466
    Out << "class";
1749
1750
1.29k
  if (TTP->isParameterPack())
1751
33
    Out << " ...";
1752
1.26k
  else if (TTP->getDeclName())
1753
1.23k
    Out << ' ';
1754
1755
1.29k
  if (TTP->getDeclName()) {
1756
1.26k
    if (Policy.CleanUglifiedParameters && 
TTP->getIdentifier()6
)
1757
6
      Out << TTP->getIdentifier()->deuglifiedName();
1758
1.26k
    else
1759
1.26k
      Out << TTP->getDeclName();
1760
1.26k
  }
1761
1762
1.29k
  if (TTP->hasDefaultArgument()) {
1763
7
    Out << " = ";
1764
7
    Out << TTP->getDefaultArgument().getAsString(Policy);
1765
7
  }
1766
1.29k
}
1767
1768
void DeclPrinter::VisitNonTypeTemplateParmDecl(
1769
451
    const NonTypeTemplateParmDecl *NTTP) {
1770
451
  StringRef Name;
1771
451
  if (IdentifierInfo *II = NTTP->getIdentifier())
1772
431
    Name =
1773
431
        Policy.CleanUglifiedParameters ? 
II->deuglifiedName()6
:
II->getName()425
;
1774
451
  printDeclType(NTTP->getType(), Name, NTTP->isParameterPack());
1775
1776
451
  if (NTTP->hasDefaultArgument()) {
1777
9
    Out << " = ";
1778
9
    NTTP->getDefaultArgument()->printPretty(Out, nullptr, Policy, Indentation,
1779
9
                                            "\n", &Context);
1780
9
  }
1781
451
}