Coverage Report

Created: 2019-07-24 05:18

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