Coverage Report

Created: 2020-02-15 09:57

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