1//===---- StmtProfile.cpp - Profile implementation for Stmt ASTs ----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::Profile method, which builds a unique bit
11// representation that identifies a statement/expression.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprCXX.h"
20#include "clang/AST/ExprObjC.h"
21#include "clang/AST/ExprOpenMP.h"
22#include "clang/AST/StmtVisitor.h"
23#include "llvm/ADT/FoldingSet.h"
24using namespace clang;
25
26namespace {
27  class StmtProfiler : public ConstStmtVisitor<StmtProfiler> {
28    llvm::FoldingSetNodeID &ID;
29    const ASTContext &Context;
30    bool Canonical;
31
32  public:
33    StmtProfiler(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
34                 bool Canonical)
35      : ID(ID), Context(Context), Canonical(Canonical) { }
36
37    void VisitStmt(const Stmt *S);
38
39#define STMT(Node, Base) void Visit##Node(const Node *S);
40#include "clang/AST/StmtNodes.inc"
41
42    /// \brief Visit a declaration that is referenced within an expression
43    /// or statement.
44    void VisitDecl(const Decl *D);
45
46    /// \brief Visit a type that is referenced within an expression or
47    /// statement.
48    void VisitType(QualType T);
49
50    /// \brief Visit a name that occurs within an expression or statement.
51    void VisitName(DeclarationName Name);
52
53    /// \brief Visit a nested-name-specifier that occurs within an expression
54    /// or statement.
55    void VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
56
57    /// \brief Visit a template name that occurs within an expression or
58    /// statement.
59    void VisitTemplateName(TemplateName Name);
60
61    /// \brief Visit template arguments that occur within an expression or
62    /// statement.
63    void VisitTemplateArguments(const TemplateArgumentLoc *Args,
64                                unsigned NumArgs);
65
66    /// \brief Visit a single template argument.
67    void VisitTemplateArgument(const TemplateArgument &Arg);
68  };
69}
70
71void StmtProfiler::VisitStmt(const Stmt *S) {
72  assert(S && "Requires non-null Stmt pointer");
73  ID.AddInteger(S->getStmtClass());
74  for (const Stmt *SubStmt : S->children()) {
75    if (SubStmt)
76      Visit(SubStmt);
77    else
78      ID.AddInteger(0);
79  }
80}
81
82void StmtProfiler::VisitDeclStmt(const DeclStmt *S) {
83  VisitStmt(S);
84  for (const auto *D : S->decls())
85    VisitDecl(D);
86}
87
88void StmtProfiler::VisitNullStmt(const NullStmt *S) {
89  VisitStmt(S);
90}
91
92void StmtProfiler::VisitCompoundStmt(const CompoundStmt *S) {
93  VisitStmt(S);
94}
95
96void StmtProfiler::VisitSwitchCase(const SwitchCase *S) {
97  VisitStmt(S);
98}
99
100void StmtProfiler::VisitCaseStmt(const CaseStmt *S) {
101  VisitStmt(S);
102}
103
104void StmtProfiler::VisitDefaultStmt(const DefaultStmt *S) {
105  VisitStmt(S);
106}
107
108void StmtProfiler::VisitLabelStmt(const LabelStmt *S) {
109  VisitStmt(S);
110  VisitDecl(S->getDecl());
111}
112
113void StmtProfiler::VisitAttributedStmt(const AttributedStmt *S) {
114  VisitStmt(S);
115  // TODO: maybe visit attributes?
116}
117
118void StmtProfiler::VisitIfStmt(const IfStmt *S) {
119  VisitStmt(S);
120  VisitDecl(S->getConditionVariable());
121}
122
123void StmtProfiler::VisitSwitchStmt(const SwitchStmt *S) {
124  VisitStmt(S);
125  VisitDecl(S->getConditionVariable());
126}
127
128void StmtProfiler::VisitWhileStmt(const WhileStmt *S) {
129  VisitStmt(S);
130  VisitDecl(S->getConditionVariable());
131}
132
133void StmtProfiler::VisitDoStmt(const DoStmt *S) {
134  VisitStmt(S);
135}
136
137void StmtProfiler::VisitForStmt(const ForStmt *S) {
138  VisitStmt(S);
139}
140
141void StmtProfiler::VisitGotoStmt(const GotoStmt *S) {
142  VisitStmt(S);
143  VisitDecl(S->getLabel());
144}
145
146void StmtProfiler::VisitIndirectGotoStmt(const IndirectGotoStmt *S) {
147  VisitStmt(S);
148}
149
150void StmtProfiler::VisitContinueStmt(const ContinueStmt *S) {
151  VisitStmt(S);
152}
153
154void StmtProfiler::VisitBreakStmt(const BreakStmt *S) {
155  VisitStmt(S);
156}
157
158void StmtProfiler::VisitReturnStmt(const ReturnStmt *S) {
159  VisitStmt(S);
160}
161
162void StmtProfiler::VisitGCCAsmStmt(const GCCAsmStmt *S) {
163  VisitStmt(S);
164  ID.AddBoolean(S->isVolatile());
165  ID.AddBoolean(S->isSimple());
166  VisitStringLiteral(S->getAsmString());
167  ID.AddInteger(S->getNumOutputs());
168  for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
169    ID.AddString(S->getOutputName(I));
170    VisitStringLiteral(S->getOutputConstraintLiteral(I));
171  }
172  ID.AddInteger(S->getNumInputs());
173  for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
174    ID.AddString(S->getInputName(I));
175    VisitStringLiteral(S->getInputConstraintLiteral(I));
176  }
177  ID.AddInteger(S->getNumClobbers());
178  for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
179    VisitStringLiteral(S->getClobberStringLiteral(I));
180}
181
182void StmtProfiler::VisitMSAsmStmt(const MSAsmStmt *S) {
183  // FIXME: Implement MS style inline asm statement profiler.
184  VisitStmt(S);
185}
186
187void StmtProfiler::VisitCXXCatchStmt(const CXXCatchStmt *S) {
188  VisitStmt(S);
189  VisitType(S->getCaughtType());
190}
191
192void StmtProfiler::VisitCXXTryStmt(const CXXTryStmt *S) {
193  VisitStmt(S);
194}
195
196void StmtProfiler::VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
197  VisitStmt(S);
198}
199
200void StmtProfiler::VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) {
201  VisitStmt(S);
202  ID.AddBoolean(S->isIfExists());
203  VisitNestedNameSpecifier(S->getQualifierLoc().getNestedNameSpecifier());
204  VisitName(S->getNameInfo().getName());
205}
206
207void StmtProfiler::VisitSEHTryStmt(const SEHTryStmt *S) {
208  VisitStmt(S);
209}
210
211void StmtProfiler::VisitSEHFinallyStmt(const SEHFinallyStmt *S) {
212  VisitStmt(S);
213}
214
215void StmtProfiler::VisitSEHExceptStmt(const SEHExceptStmt *S) {
216  VisitStmt(S);
217}
218
219void StmtProfiler::VisitSEHLeaveStmt(const SEHLeaveStmt *S) {
220  VisitStmt(S);
221}
222
223void StmtProfiler::VisitCapturedStmt(const CapturedStmt *S) {
224  VisitStmt(S);
225}
226
227void StmtProfiler::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
228  VisitStmt(S);
229}
230
231void StmtProfiler::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *S) {
232  VisitStmt(S);
233  ID.AddBoolean(S->hasEllipsis());
234  if (S->getCatchParamDecl())
235    VisitType(S->getCatchParamDecl()->getType());
236}
237
238void StmtProfiler::VisitObjCAtFinallyStmt(const ObjCAtFinallyStmt *S) {
239  VisitStmt(S);
240}
241
242void StmtProfiler::VisitObjCAtTryStmt(const ObjCAtTryStmt *S) {
243  VisitStmt(S);
244}
245
246void
247StmtProfiler::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S) {
248  VisitStmt(S);
249}
250
251void StmtProfiler::VisitObjCAtThrowStmt(const ObjCAtThrowStmt *S) {
252  VisitStmt(S);
253}
254
255void
256StmtProfiler::VisitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt *S) {
257  VisitStmt(S);
258}
259
260namespace {
261class OMPClauseProfiler : public ConstOMPClauseVisitor<OMPClauseProfiler> {
262  StmtProfiler *Profiler;
263  /// \brief Process clauses with list of variables.
264  template <typename T>
265  void VisitOMPClauseList(T *Node);
266
267public:
268  OMPClauseProfiler(StmtProfiler *P) : Profiler(P) { }
269#define OPENMP_CLAUSE(Name, Class)                                             \
270  void Visit##Class(const Class *C);
271#include "clang/Basic/OpenMPKinds.def"
272  void VistOMPClauseWithPreInit(const OMPClauseWithPreInit *C);
273  void VistOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C);
274};
275
276void OMPClauseProfiler::VistOMPClauseWithPreInit(
277    const OMPClauseWithPreInit *C) {
278  if (auto *S = C->getPreInitStmt())
279    Profiler->VisitStmt(S);
280}
281
282void OMPClauseProfiler::VistOMPClauseWithPostUpdate(
283    const OMPClauseWithPostUpdate *C) {
284  VistOMPClauseWithPreInit(C);
285  if (auto *E = C->getPostUpdateExpr())
286    Profiler->VisitStmt(E);
287}
288
289void OMPClauseProfiler::VisitOMPIfClause(const OMPIfClause *C) {
290  if (C->getCondition())
291    Profiler->VisitStmt(C->getCondition());
292}
293
294void OMPClauseProfiler::VisitOMPFinalClause(const OMPFinalClause *C) {
295  if (C->getCondition())
296    Profiler->VisitStmt(C->getCondition());
297}
298
299void OMPClauseProfiler::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
300  if (C->getNumThreads())
301    Profiler->VisitStmt(C->getNumThreads());
302}
303
304void OMPClauseProfiler::VisitOMPSafelenClause(const OMPSafelenClause *C) {
305  if (C->getSafelen())
306    Profiler->VisitStmt(C->getSafelen());
307}
308
309void OMPClauseProfiler::VisitOMPSimdlenClause(const OMPSimdlenClause *C) {
310  if (C->getSimdlen())
311    Profiler->VisitStmt(C->getSimdlen());
312}
313
314void OMPClauseProfiler::VisitOMPCollapseClause(const OMPCollapseClause *C) {
315  if (C->getNumForLoops())
316    Profiler->VisitStmt(C->getNumForLoops());
317}
318
319void OMPClauseProfiler::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
320
321void OMPClauseProfiler::VisitOMPProcBindClause(const OMPProcBindClause *C) { }
322
323void OMPClauseProfiler::VisitOMPScheduleClause(const OMPScheduleClause *C) {
324  VistOMPClauseWithPreInit(C);
325  if (auto *S = C->getChunkSize())
326    Profiler->VisitStmt(S);
327}
328
329void OMPClauseProfiler::VisitOMPOrderedClause(const OMPOrderedClause *C) {
330  if (auto *Num = C->getNumForLoops())
331    Profiler->VisitStmt(Num);
332}
333
334void OMPClauseProfiler::VisitOMPNowaitClause(const OMPNowaitClause *) {}
335
336void OMPClauseProfiler::VisitOMPUntiedClause(const OMPUntiedClause *) {}
337
338void OMPClauseProfiler::VisitOMPMergeableClause(const OMPMergeableClause *) {}
339
340void OMPClauseProfiler::VisitOMPReadClause(const OMPReadClause *) {}
341
342void OMPClauseProfiler::VisitOMPWriteClause(const OMPWriteClause *) {}
343
344void OMPClauseProfiler::VisitOMPUpdateClause(const OMPUpdateClause *) {}
345
346void OMPClauseProfiler::VisitOMPCaptureClause(const OMPCaptureClause *) {}
347
348void OMPClauseProfiler::VisitOMPSeqCstClause(const OMPSeqCstClause *) {}
349
350void OMPClauseProfiler::VisitOMPThreadsClause(const OMPThreadsClause *) {}
351
352void OMPClauseProfiler::VisitOMPSIMDClause(const OMPSIMDClause *) {}
353
354void OMPClauseProfiler::VisitOMPNogroupClause(const OMPNogroupClause *) {}
355
356template<typename T>
357void OMPClauseProfiler::VisitOMPClauseList(T *Node) {
358  for (auto *E : Node->varlists()) {
359    if (E)
360      Profiler->VisitStmt(E);
361  }
362}
363
364void OMPClauseProfiler::VisitOMPPrivateClause(const OMPPrivateClause *C) {
365  VisitOMPClauseList(C);
366  for (auto *E : C->private_copies()) {
367    if (E)
368      Profiler->VisitStmt(E);
369  }
370}
371void
372OMPClauseProfiler::VisitOMPFirstprivateClause(const OMPFirstprivateClause *C) {
373  VisitOMPClauseList(C);
374  VistOMPClauseWithPreInit(C);
375  for (auto *E : C->private_copies()) {
376    if (E)
377      Profiler->VisitStmt(E);
378  }
379  for (auto *E : C->inits()) {
380    if (E)
381      Profiler->VisitStmt(E);
382  }
383}
384void
385OMPClauseProfiler::VisitOMPLastprivateClause(const OMPLastprivateClause *C) {
386  VisitOMPClauseList(C);
387  VistOMPClauseWithPostUpdate(C);
388  for (auto *E : C->source_exprs()) {
389    if (E)
390      Profiler->VisitStmt(E);
391  }
392  for (auto *E : C->destination_exprs()) {
393    if (E)
394      Profiler->VisitStmt(E);
395  }
396  for (auto *E : C->assignment_ops()) {
397    if (E)
398      Profiler->VisitStmt(E);
399  }
400}
401void OMPClauseProfiler::VisitOMPSharedClause(const OMPSharedClause *C) {
402  VisitOMPClauseList(C);
403}
404void OMPClauseProfiler::VisitOMPReductionClause(
405                                         const OMPReductionClause *C) {
406  Profiler->VisitNestedNameSpecifier(
407      C->getQualifierLoc().getNestedNameSpecifier());
408  Profiler->VisitName(C->getNameInfo().getName());
409  VisitOMPClauseList(C);
410  VistOMPClauseWithPostUpdate(C);
411  for (auto *E : C->privates()) {
412    if (E)
413      Profiler->VisitStmt(E);
414  }
415  for (auto *E : C->lhs_exprs()) {
416    if (E)
417      Profiler->VisitStmt(E);
418  }
419  for (auto *E : C->rhs_exprs()) {
420    if (E)
421      Profiler->VisitStmt(E);
422  }
423  for (auto *E : C->reduction_ops()) {
424    if (E)
425      Profiler->VisitStmt(E);
426  }
427}
428void OMPClauseProfiler::VisitOMPLinearClause(const OMPLinearClause *C) {
429  VisitOMPClauseList(C);
430  VistOMPClauseWithPostUpdate(C);
431  for (auto *E : C->privates()) {
432    if (E)
433      Profiler->VisitStmt(E);
434  }
435  for (auto *E : C->inits()) {
436    if (E)
437      Profiler->VisitStmt(E);
438  }
439  for (auto *E : C->updates()) {
440    if (E)
441      Profiler->VisitStmt(E);
442  }
443  for (auto *E : C->finals()) {
444    if (E)
445      Profiler->VisitStmt(E);
446  }
447  if (C->getStep())
448    Profiler->VisitStmt(C->getStep());
449  if (C->getCalcStep())
450    Profiler->VisitStmt(C->getCalcStep());
451}
452void OMPClauseProfiler::VisitOMPAlignedClause(const OMPAlignedClause *C) {
453  VisitOMPClauseList(C);
454  if (C->getAlignment())
455    Profiler->VisitStmt(C->getAlignment());
456}
457void OMPClauseProfiler::VisitOMPCopyinClause(const OMPCopyinClause *C) {
458  VisitOMPClauseList(C);
459  for (auto *E : C->source_exprs()) {
460    if (E)
461      Profiler->VisitStmt(E);
462  }
463  for (auto *E : C->destination_exprs()) {
464    if (E)
465      Profiler->VisitStmt(E);
466  }
467  for (auto *E : C->assignment_ops()) {
468    if (E)
469      Profiler->VisitStmt(E);
470  }
471}
472void
473OMPClauseProfiler::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) {
474  VisitOMPClauseList(C);
475  for (auto *E : C->source_exprs()) {
476    if (E)
477      Profiler->VisitStmt(E);
478  }
479  for (auto *E : C->destination_exprs()) {
480    if (E)
481      Profiler->VisitStmt(E);
482  }
483  for (auto *E : C->assignment_ops()) {
484    if (E)
485      Profiler->VisitStmt(E);
486  }
487}
488void OMPClauseProfiler::VisitOMPFlushClause(const OMPFlushClause *C) {
489  VisitOMPClauseList(C);
490}
491void OMPClauseProfiler::VisitOMPDependClause(const OMPDependClause *C) {
492  VisitOMPClauseList(C);
493}
494void OMPClauseProfiler::VisitOMPDeviceClause(const OMPDeviceClause *C) {
495  if (C->getDevice())
496    Profiler->VisitStmt(C->getDevice());
497}
498void OMPClauseProfiler::VisitOMPMapClause(const OMPMapClause *C) {
499  VisitOMPClauseList(C);
500}
501void OMPClauseProfiler::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) {
502  if (C->getNumTeams())
503    Profiler->VisitStmt(C->getNumTeams());
504}
505void OMPClauseProfiler::VisitOMPThreadLimitClause(
506    const OMPThreadLimitClause *C) {
507  if (C->getThreadLimit())
508    Profiler->VisitStmt(C->getThreadLimit());
509}
510void OMPClauseProfiler::VisitOMPPriorityClause(const OMPPriorityClause *C) {
511  if (C->getPriority())
512    Profiler->VisitStmt(C->getPriority());
513}
514void OMPClauseProfiler::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) {
515  if (C->getGrainsize())
516    Profiler->VisitStmt(C->getGrainsize());
517}
518void OMPClauseProfiler::VisitOMPNumTasksClause(const OMPNumTasksClause *C) {
519  if (C->getNumTasks())
520    Profiler->VisitStmt(C->getNumTasks());
521}
522void OMPClauseProfiler::VisitOMPHintClause(const OMPHintClause *C) {
523  if (C->getHint())
524    Profiler->VisitStmt(C->getHint());
525}
526void OMPClauseProfiler::VisitOMPToClause(const OMPToClause *C) {
527  VisitOMPClauseList(C);
528}
529void OMPClauseProfiler::VisitOMPFromClause(const OMPFromClause *C) {
530  VisitOMPClauseList(C);
531}
532void OMPClauseProfiler::VisitOMPUseDevicePtrClause(
533    const OMPUseDevicePtrClause *C) {
534  VisitOMPClauseList(C);
535}
536void OMPClauseProfiler::VisitOMPIsDevicePtrClause(
537    const OMPIsDevicePtrClause *C) {
538  VisitOMPClauseList(C);
539}
540}
541
542void
543StmtProfiler::VisitOMPExecutableDirective(const OMPExecutableDirective *S) {
544  VisitStmt(S);
545  OMPClauseProfiler P(this);
546  ArrayRef<OMPClause *> Clauses = S->clauses();
547  for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
548       I != E; ++I)
549    if (*I)
550      P.Visit(*I);
551}
552
553void StmtProfiler::VisitOMPLoopDirective(const OMPLoopDirective *S) {
554  VisitOMPExecutableDirective(S);
555}
556
557void StmtProfiler::VisitOMPParallelDirective(const OMPParallelDirective *S) {
558  VisitOMPExecutableDirective(S);
559}
560
561void StmtProfiler::VisitOMPSimdDirective(const OMPSimdDirective *S) {
562  VisitOMPLoopDirective(S);
563}
564
565void StmtProfiler::VisitOMPForDirective(const OMPForDirective *S) {
566  VisitOMPLoopDirective(S);
567}
568
569void StmtProfiler::VisitOMPForSimdDirective(const OMPForSimdDirective *S) {
570  VisitOMPLoopDirective(S);
571}
572
573void StmtProfiler::VisitOMPSectionsDirective(const OMPSectionsDirective *S) {
574  VisitOMPExecutableDirective(S);
575}
576
577void StmtProfiler::VisitOMPSectionDirective(const OMPSectionDirective *S) {
578  VisitOMPExecutableDirective(S);
579}
580
581void StmtProfiler::VisitOMPSingleDirective(const OMPSingleDirective *S) {
582  VisitOMPExecutableDirective(S);
583}
584
585void StmtProfiler::VisitOMPMasterDirective(const OMPMasterDirective *S) {
586  VisitOMPExecutableDirective(S);
587}
588
589void StmtProfiler::VisitOMPCriticalDirective(const OMPCriticalDirective *S) {
590  VisitOMPExecutableDirective(S);
591  VisitName(S->getDirectiveName().getName());
592}
593
594void
595StmtProfiler::VisitOMPParallelForDirective(const OMPParallelForDirective *S) {
596  VisitOMPLoopDirective(S);
597}
598
599void StmtProfiler::VisitOMPParallelForSimdDirective(
600    const OMPParallelForSimdDirective *S) {
601  VisitOMPLoopDirective(S);
602}
603
604void StmtProfiler::VisitOMPParallelSectionsDirective(
605    const OMPParallelSectionsDirective *S) {
606  VisitOMPExecutableDirective(S);
607}
608
609void StmtProfiler::VisitOMPTaskDirective(const OMPTaskDirective *S) {
610  VisitOMPExecutableDirective(S);
611}
612
613void StmtProfiler::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *S) {
614  VisitOMPExecutableDirective(S);
615}
616
617void StmtProfiler::VisitOMPBarrierDirective(const OMPBarrierDirective *S) {
618  VisitOMPExecutableDirective(S);
619}
620
621void StmtProfiler::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *S) {
622  VisitOMPExecutableDirective(S);
623}
624
625void StmtProfiler::VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *S) {
626  VisitOMPExecutableDirective(S);
627}
628
629void StmtProfiler::VisitOMPFlushDirective(const OMPFlushDirective *S) {
630  VisitOMPExecutableDirective(S);
631}
632
633void StmtProfiler::VisitOMPOrderedDirective(const OMPOrderedDirective *S) {
634  VisitOMPExecutableDirective(S);
635}
636
637void StmtProfiler::VisitOMPAtomicDirective(const OMPAtomicDirective *S) {
638  VisitOMPExecutableDirective(S);
639}
640
641void StmtProfiler::VisitOMPTargetDirective(const OMPTargetDirective *S) {
642  VisitOMPExecutableDirective(S);
643}
644
645void StmtProfiler::VisitOMPTargetDataDirective(const OMPTargetDataDirective *S) {
646  VisitOMPExecutableDirective(S);
647}
648
649void StmtProfiler::VisitOMPTargetEnterDataDirective(
650    const OMPTargetEnterDataDirective *S) {
651  VisitOMPExecutableDirective(S);
652}
653
654void StmtProfiler::VisitOMPTargetExitDataDirective(
655    const OMPTargetExitDataDirective *S) {
656  VisitOMPExecutableDirective(S);
657}
658
659void StmtProfiler::VisitOMPTargetParallelDirective(
660    const OMPTargetParallelDirective *S) {
661  VisitOMPExecutableDirective(S);
662}
663
664void StmtProfiler::VisitOMPTargetParallelForDirective(
665    const OMPTargetParallelForDirective *S) {
666  VisitOMPExecutableDirective(S);
667}
668
669void StmtProfiler::VisitOMPTeamsDirective(const OMPTeamsDirective *S) {
670  VisitOMPExecutableDirective(S);
671}
672
673void StmtProfiler::VisitOMPCancellationPointDirective(
674    const OMPCancellationPointDirective *S) {
675  VisitOMPExecutableDirective(S);
676}
677
678void StmtProfiler::VisitOMPCancelDirective(const OMPCancelDirective *S) {
679  VisitOMPExecutableDirective(S);
680}
681
682void StmtProfiler::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *S) {
683  VisitOMPLoopDirective(S);
684}
685
686void StmtProfiler::VisitOMPTaskLoopSimdDirective(
687    const OMPTaskLoopSimdDirective *S) {
688  VisitOMPLoopDirective(S);
689}
690
691void StmtProfiler::VisitOMPDistributeDirective(
692    const OMPDistributeDirective *S) {
693  VisitOMPLoopDirective(S);
694}
695
696void OMPClauseProfiler::VisitOMPDistScheduleClause(
697    const OMPDistScheduleClause *C) {
698  VistOMPClauseWithPreInit(C);
699  if (auto *S = C->getChunkSize())
700    Profiler->VisitStmt(S);
701}
702
703void OMPClauseProfiler::VisitOMPDefaultmapClause(const OMPDefaultmapClause *) {}
704
705void StmtProfiler::VisitOMPTargetUpdateDirective(
706    const OMPTargetUpdateDirective *S) {
707  VisitOMPExecutableDirective(S);
708}
709
710void StmtProfiler::VisitOMPDistributeParallelForDirective(
711    const OMPDistributeParallelForDirective *S) {
712  VisitOMPLoopDirective(S);
713}
714
715void StmtProfiler::VisitOMPDistributeParallelForSimdDirective(
716    const OMPDistributeParallelForSimdDirective *S) {
717  VisitOMPLoopDirective(S);
718}
719
720void StmtProfiler::VisitOMPDistributeSimdDirective(
721    const OMPDistributeSimdDirective *S) {
722  VisitOMPLoopDirective(S);
723}
724
725void StmtProfiler::VisitOMPTargetParallelForSimdDirective(
726    const OMPTargetParallelForSimdDirective *S) {
727  VisitOMPLoopDirective(S);
728}
729
730void StmtProfiler::VisitExpr(const Expr *S) {
731  VisitStmt(S);
732}
733
734void StmtProfiler::VisitDeclRefExpr(const DeclRefExpr *S) {
735  VisitExpr(S);
736  if (!Canonical)
737    VisitNestedNameSpecifier(S->getQualifier());
738  VisitDecl(S->getDecl());
739  if (!Canonical)
740    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
741}
742
743void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
744  VisitExpr(S);
745  ID.AddInteger(S->getIdentType());
746}
747
748void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {
749  VisitExpr(S);
750  S->getValue().Profile(ID);
751  ID.AddInteger(S->getType()->castAs<BuiltinType>()->getKind());
752}
753
754void StmtProfiler::VisitCharacterLiteral(const CharacterLiteral *S) {
755  VisitExpr(S);
756  ID.AddInteger(S->getKind());
757  ID.AddInteger(S->getValue());
758}
759
760void StmtProfiler::VisitFloatingLiteral(const FloatingLiteral *S) {
761  VisitExpr(S);
762  S->getValue().Profile(ID);
763  ID.AddBoolean(S->isExact());
764  ID.AddInteger(S->getType()->castAs<BuiltinType>()->getKind());
765}
766
767void StmtProfiler::VisitImaginaryLiteral(const ImaginaryLiteral *S) {
768  VisitExpr(S);
769}
770
771void StmtProfiler::VisitStringLiteral(const StringLiteral *S) {
772  VisitExpr(S);
773  ID.AddString(S->getBytes());
774  ID.AddInteger(S->getKind());
775}
776
777void StmtProfiler::VisitParenExpr(const ParenExpr *S) {
778  VisitExpr(S);
779}
780
781void StmtProfiler::VisitParenListExpr(const ParenListExpr *S) {
782  VisitExpr(S);
783}
784
785void StmtProfiler::VisitUnaryOperator(const UnaryOperator *S) {
786  VisitExpr(S);
787  ID.AddInteger(S->getOpcode());
788}
789
790void StmtProfiler::VisitOffsetOfExpr(const OffsetOfExpr *S) {
791  VisitType(S->getTypeSourceInfo()->getType());
792  unsigned n = S->getNumComponents();
793  for (unsigned i = 0; i < n; ++i) {
794    const OffsetOfNode &ON = S->getComponent(i);
795    ID.AddInteger(ON.getKind());
796    switch (ON.getKind()) {
797    case OffsetOfNode::Array:
798      // Expressions handled below.
799      break;
800
801    case OffsetOfNode::Field:
802      VisitDecl(ON.getField());
803      break;
804
805    case OffsetOfNode::Identifier:
806      ID.AddPointer(ON.getFieldName());
807      break;
808
809    case OffsetOfNode::Base:
810      // These nodes are implicit, and therefore don't need profiling.
811      break;
812    }
813  }
814
815  VisitExpr(S);
816}
817
818void
819StmtProfiler::VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *S) {
820  VisitExpr(S);
821  ID.AddInteger(S->getKind());
822  if (S->isArgumentType())
823    VisitType(S->getArgumentType());
824}
825
826void StmtProfiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *S) {
827  VisitExpr(S);
828}
829
830void StmtProfiler::VisitOMPArraySectionExpr(const OMPArraySectionExpr *S) {
831  VisitExpr(S);
832}
833
834void StmtProfiler::VisitCallExpr(const CallExpr *S) {
835  VisitExpr(S);
836}
837
838void StmtProfiler::VisitMemberExpr(const MemberExpr *S) {
839  VisitExpr(S);
840  VisitDecl(S->getMemberDecl());
841  if (!Canonical)
842    VisitNestedNameSpecifier(S->getQualifier());
843  ID.AddBoolean(S->isArrow());
844}
845
846void StmtProfiler::VisitCompoundLiteralExpr(const CompoundLiteralExpr *S) {
847  VisitExpr(S);
848  ID.AddBoolean(S->isFileScope());
849}
850
851void StmtProfiler::VisitCastExpr(const CastExpr *S) {
852  VisitExpr(S);
853}
854
855void StmtProfiler::VisitImplicitCastExpr(const ImplicitCastExpr *S) {
856  VisitCastExpr(S);
857  ID.AddInteger(S->getValueKind());
858}
859
860void StmtProfiler::VisitExplicitCastExpr(const ExplicitCastExpr *S) {
861  VisitCastExpr(S);
862  VisitType(S->getTypeAsWritten());
863}
864
865void StmtProfiler::VisitCStyleCastExpr(const CStyleCastExpr *S) {
866  VisitExplicitCastExpr(S);
867}
868
869void StmtProfiler::VisitBinaryOperator(const BinaryOperator *S) {
870  VisitExpr(S);
871  ID.AddInteger(S->getOpcode());
872}
873
874void
875StmtProfiler::VisitCompoundAssignOperator(const CompoundAssignOperator *S) {
876  VisitBinaryOperator(S);
877}
878
879void StmtProfiler::VisitConditionalOperator(const ConditionalOperator *S) {
880  VisitExpr(S);
881}
882
883void StmtProfiler::VisitBinaryConditionalOperator(
884    const BinaryConditionalOperator *S) {
885  VisitExpr(S);
886}
887
888void StmtProfiler::VisitAddrLabelExpr(const AddrLabelExpr *S) {
889  VisitExpr(S);
890  VisitDecl(S->getLabel());
891}
892
893void StmtProfiler::VisitStmtExpr(const StmtExpr *S) {
894  VisitExpr(S);
895}
896
897void StmtProfiler::VisitShuffleVectorExpr(const ShuffleVectorExpr *S) {
898  VisitExpr(S);
899}
900
901void StmtProfiler::VisitConvertVectorExpr(const ConvertVectorExpr *S) {
902  VisitExpr(S);
903}
904
905void StmtProfiler::VisitChooseExpr(const ChooseExpr *S) {
906  VisitExpr(S);
907}
908
909void StmtProfiler::VisitGNUNullExpr(const GNUNullExpr *S) {
910  VisitExpr(S);
911}
912
913void StmtProfiler::VisitVAArgExpr(const VAArgExpr *S) {
914  VisitExpr(S);
915}
916
917void StmtProfiler::VisitInitListExpr(const InitListExpr *S) {
918  if (S->getSyntacticForm()) {
919    VisitInitListExpr(S->getSyntacticForm());
920    return;
921  }
922
923  VisitExpr(S);
924}
925
926void StmtProfiler::VisitDesignatedInitExpr(const DesignatedInitExpr *S) {
927  VisitExpr(S);
928  ID.AddBoolean(S->usesGNUSyntax());
929  for (const DesignatedInitExpr::Designator &D : S->designators()) {
930    if (D.isFieldDesignator()) {
931      ID.AddInteger(0);
932      VisitName(D.getFieldName());
933      continue;
934    }
935
936    if (D.isArrayDesignator()) {
937      ID.AddInteger(1);
938    } else {
939      assert(D.isArrayRangeDesignator());
940      ID.AddInteger(2);
941    }
942    ID.AddInteger(D.getFirstExprIndex());
943  }
944}
945
946// Seems that if VisitInitListExpr() only works on the syntactic form of an
947// InitListExpr, then a DesignatedInitUpdateExpr is not encountered.
948void StmtProfiler::VisitDesignatedInitUpdateExpr(
949    const DesignatedInitUpdateExpr *S) {
950  llvm_unreachable("Unexpected DesignatedInitUpdateExpr in syntactic form of "
951                   "initializer");
952}
953
954void StmtProfiler::VisitNoInitExpr(const NoInitExpr *S) {
955  llvm_unreachable("Unexpected NoInitExpr in syntactic form of initializer");
956}
957
958void StmtProfiler::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *S) {
959  VisitExpr(S);
960}
961
962void StmtProfiler::VisitExtVectorElementExpr(const ExtVectorElementExpr *S) {
963  VisitExpr(S);
964  VisitName(&S->getAccessor());
965}
966
967void StmtProfiler::VisitBlockExpr(const BlockExpr *S) {
968  VisitExpr(S);
969  VisitDecl(S->getBlockDecl());
970}
971
972void StmtProfiler::VisitGenericSelectionExpr(const GenericSelectionExpr *S) {
973  VisitExpr(S);
974  for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
975    QualType T = S->getAssocType(i);
976    if (T.isNull())
977      ID.AddPointer(nullptr);
978    else
979      VisitType(T);
980    VisitExpr(S->getAssocExpr(i));
981  }
982}
983
984void StmtProfiler::VisitPseudoObjectExpr(const PseudoObjectExpr *S) {
985  VisitExpr(S);
986  for (PseudoObjectExpr::const_semantics_iterator
987         i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i)
988    // Normally, we would not profile the source expressions of OVEs.
989    if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(*i))
990      Visit(OVE->getSourceExpr());
991}
992
993void StmtProfiler::VisitAtomicExpr(const AtomicExpr *S) {
994  VisitExpr(S);
995  ID.AddInteger(S->getOp());
996}
997
998static Stmt::StmtClass DecodeOperatorCall(const CXXOperatorCallExpr *S,
999                                          UnaryOperatorKind &UnaryOp,
1000                                          BinaryOperatorKind &BinaryOp) {
1001  switch (S->getOperator()) {
1002  case OO_None:
1003  case OO_New:
1004  case OO_Delete:
1005  case OO_Array_New:
1006  case OO_Array_Delete:
1007  case OO_Arrow:
1008  case OO_Call:
1009  case OO_Conditional:
1010  case OO_Coawait:
1011  case NUM_OVERLOADED_OPERATORS:
1012    llvm_unreachable("Invalid operator call kind");
1013
1014  case OO_Plus:
1015    if (S->getNumArgs() == 1) {
1016      UnaryOp = UO_Plus;
1017      return Stmt::UnaryOperatorClass;
1018    }
1019
1020    BinaryOp = BO_Add;
1021    return Stmt::BinaryOperatorClass;
1022
1023  case OO_Minus:
1024    if (S->getNumArgs() == 1) {
1025      UnaryOp = UO_Minus;
1026      return Stmt::UnaryOperatorClass;
1027    }
1028
1029    BinaryOp = BO_Sub;
1030    return Stmt::BinaryOperatorClass;
1031
1032  case OO_Star:
1033    if (S->getNumArgs() == 1) {
1034      UnaryOp = UO_Deref;
1035      return Stmt::UnaryOperatorClass;
1036    }
1037
1038    BinaryOp = BO_Mul;
1039    return Stmt::BinaryOperatorClass;
1040
1041  case OO_Slash:
1042    BinaryOp = BO_Div;
1043    return Stmt::BinaryOperatorClass;
1044
1045  case OO_Percent:
1046    BinaryOp = BO_Rem;
1047    return Stmt::BinaryOperatorClass;
1048
1049  case OO_Caret:
1050    BinaryOp = BO_Xor;
1051    return Stmt::BinaryOperatorClass;
1052
1053  case OO_Amp:
1054    if (S->getNumArgs() == 1) {
1055      UnaryOp = UO_AddrOf;
1056      return Stmt::UnaryOperatorClass;
1057    }
1058
1059    BinaryOp = BO_And;
1060    return Stmt::BinaryOperatorClass;
1061
1062  case OO_Pipe:
1063    BinaryOp = BO_Or;
1064    return Stmt::BinaryOperatorClass;
1065
1066  case OO_Tilde:
1067    UnaryOp = UO_Not;
1068    return Stmt::UnaryOperatorClass;
1069
1070  case OO_Exclaim:
1071    UnaryOp = UO_LNot;
1072    return Stmt::UnaryOperatorClass;
1073
1074  case OO_Equal:
1075    BinaryOp = BO_Assign;
1076    return Stmt::BinaryOperatorClass;
1077
1078  case OO_Less:
1079    BinaryOp = BO_LT;
1080    return Stmt::BinaryOperatorClass;
1081
1082  case OO_Greater:
1083    BinaryOp = BO_GT;
1084    return Stmt::BinaryOperatorClass;
1085
1086  case OO_PlusEqual:
1087    BinaryOp = BO_AddAssign;
1088    return Stmt::CompoundAssignOperatorClass;
1089
1090  case OO_MinusEqual:
1091    BinaryOp = BO_SubAssign;
1092    return Stmt::CompoundAssignOperatorClass;
1093
1094  case OO_StarEqual:
1095    BinaryOp = BO_MulAssign;
1096    return Stmt::CompoundAssignOperatorClass;
1097
1098  case OO_SlashEqual:
1099    BinaryOp = BO_DivAssign;
1100    return Stmt::CompoundAssignOperatorClass;
1101
1102  case OO_PercentEqual:
1103    BinaryOp = BO_RemAssign;
1104    return Stmt::CompoundAssignOperatorClass;
1105
1106  case OO_CaretEqual:
1107    BinaryOp = BO_XorAssign;
1108    return Stmt::CompoundAssignOperatorClass;
1109
1110  case OO_AmpEqual:
1111    BinaryOp = BO_AndAssign;
1112    return Stmt::CompoundAssignOperatorClass;
1113
1114  case OO_PipeEqual:
1115    BinaryOp = BO_OrAssign;
1116    return Stmt::CompoundAssignOperatorClass;
1117
1118  case OO_LessLess:
1119    BinaryOp = BO_Shl;
1120    return Stmt::BinaryOperatorClass;
1121
1122  case OO_GreaterGreater:
1123    BinaryOp = BO_Shr;
1124    return Stmt::BinaryOperatorClass;
1125
1126  case OO_LessLessEqual:
1127    BinaryOp = BO_ShlAssign;
1128    return Stmt::CompoundAssignOperatorClass;
1129
1130  case OO_GreaterGreaterEqual:
1131    BinaryOp = BO_ShrAssign;
1132    return Stmt::CompoundAssignOperatorClass;
1133
1134  case OO_EqualEqual:
1135    BinaryOp = BO_EQ;
1136    return Stmt::BinaryOperatorClass;
1137
1138  case OO_ExclaimEqual:
1139    BinaryOp = BO_NE;
1140    return Stmt::BinaryOperatorClass;
1141
1142  case OO_LessEqual:
1143    BinaryOp = BO_LE;
1144    return Stmt::BinaryOperatorClass;
1145
1146  case OO_GreaterEqual:
1147    BinaryOp = BO_GE;
1148    return Stmt::BinaryOperatorClass;
1149
1150  case OO_AmpAmp:
1151    BinaryOp = BO_LAnd;
1152    return Stmt::BinaryOperatorClass;
1153
1154  case OO_PipePipe:
1155    BinaryOp = BO_LOr;
1156    return Stmt::BinaryOperatorClass;
1157
1158  case OO_PlusPlus:
1159    UnaryOp = S->getNumArgs() == 1? UO_PreInc
1160                                  : UO_PostInc;
1161    return Stmt::UnaryOperatorClass;
1162
1163  case OO_MinusMinus:
1164    UnaryOp = S->getNumArgs() == 1? UO_PreDec
1165                                  : UO_PostDec;
1166    return Stmt::UnaryOperatorClass;
1167
1168  case OO_Comma:
1169    BinaryOp = BO_Comma;
1170    return Stmt::BinaryOperatorClass;
1171
1172  case OO_ArrowStar:
1173    BinaryOp = BO_PtrMemI;
1174    return Stmt::BinaryOperatorClass;
1175
1176  case OO_Subscript:
1177    return Stmt::ArraySubscriptExprClass;
1178  }
1179
1180  llvm_unreachable("Invalid overloaded operator expression");
1181}
1182
1183void StmtProfiler::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) {
1184  if (S->isTypeDependent()) {
1185    // Type-dependent operator calls are profiled like their underlying
1186    // syntactic operator.
1187    UnaryOperatorKind UnaryOp = UO_Extension;
1188    BinaryOperatorKind BinaryOp = BO_Comma;
1189    Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp);
1190
1191    ID.AddInteger(SC);
1192    for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
1193      Visit(S->getArg(I));
1194    if (SC == Stmt::UnaryOperatorClass)
1195      ID.AddInteger(UnaryOp);
1196    else if (SC == Stmt::BinaryOperatorClass ||
1197             SC == Stmt::CompoundAssignOperatorClass)
1198      ID.AddInteger(BinaryOp);
1199    else
1200      assert(SC == Stmt::ArraySubscriptExprClass);
1201
1202    return;
1203  }
1204
1205  VisitCallExpr(S);
1206  ID.AddInteger(S->getOperator());
1207}
1208
1209void StmtProfiler::VisitCXXMemberCallExpr(const CXXMemberCallExpr *S) {
1210  VisitCallExpr(S);
1211}
1212
1213void StmtProfiler::VisitCUDAKernelCallExpr(const CUDAKernelCallExpr *S) {
1214  VisitCallExpr(S);
1215}
1216
1217void StmtProfiler::VisitAsTypeExpr(const AsTypeExpr *S) {
1218  VisitExpr(S);
1219}
1220
1221void StmtProfiler::VisitCXXNamedCastExpr(const CXXNamedCastExpr *S) {
1222  VisitExplicitCastExpr(S);
1223}
1224
1225void StmtProfiler::VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) {
1226  VisitCXXNamedCastExpr(S);
1227}
1228
1229void StmtProfiler::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *S) {
1230  VisitCXXNamedCastExpr(S);
1231}
1232
1233void
1234StmtProfiler::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *S) {
1235  VisitCXXNamedCastExpr(S);
1236}
1237
1238void StmtProfiler::VisitCXXConstCastExpr(const CXXConstCastExpr *S) {
1239  VisitCXXNamedCastExpr(S);
1240}
1241
1242void StmtProfiler::VisitUserDefinedLiteral(const UserDefinedLiteral *S) {
1243  VisitCallExpr(S);
1244}
1245
1246void StmtProfiler::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) {
1247  VisitExpr(S);
1248  ID.AddBoolean(S->getValue());
1249}
1250
1251void StmtProfiler::VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) {
1252  VisitExpr(S);
1253}
1254
1255void StmtProfiler::VisitCXXStdInitializerListExpr(
1256    const CXXStdInitializerListExpr *S) {
1257  VisitExpr(S);
1258}
1259
1260void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) {
1261  VisitExpr(S);
1262  if (S->isTypeOperand())
1263    VisitType(S->getTypeOperandSourceInfo()->getType());
1264}
1265
1266void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) {
1267  VisitExpr(S);
1268  if (S->isTypeOperand())
1269    VisitType(S->getTypeOperandSourceInfo()->getType());
1270}
1271
1272void StmtProfiler::VisitMSPropertyRefExpr(const MSPropertyRefExpr *S) {
1273  VisitExpr(S);
1274  VisitDecl(S->getPropertyDecl());
1275}
1276
1277void StmtProfiler::VisitMSPropertySubscriptExpr(
1278    const MSPropertySubscriptExpr *S) {
1279  VisitExpr(S);
1280}
1281
1282void StmtProfiler::VisitCXXThisExpr(const CXXThisExpr *S) {
1283  VisitExpr(S);
1284  ID.AddBoolean(S->isImplicit());
1285}
1286
1287void StmtProfiler::VisitCXXThrowExpr(const CXXThrowExpr *S) {
1288  VisitExpr(S);
1289}
1290
1291void StmtProfiler::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) {
1292  VisitExpr(S);
1293  VisitDecl(S->getParam());
1294}
1295
1296void StmtProfiler::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *S) {
1297  VisitExpr(S);
1298  VisitDecl(S->getField());
1299}
1300
1301void StmtProfiler::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) {
1302  VisitExpr(S);
1303  VisitDecl(
1304         const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor()));
1305}
1306
1307void StmtProfiler::VisitCXXConstructExpr(const CXXConstructExpr *S) {
1308  VisitExpr(S);
1309  VisitDecl(S->getConstructor());
1310  ID.AddBoolean(S->isElidable());
1311}
1312
1313void StmtProfiler::VisitCXXInheritedCtorInitExpr(
1314    const CXXInheritedCtorInitExpr *S) {
1315  VisitExpr(S);
1316  VisitDecl(S->getConstructor());
1317}
1318
1319void StmtProfiler::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
1320  VisitExplicitCastExpr(S);
1321}
1322
1323void
1324StmtProfiler::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) {
1325  VisitCXXConstructExpr(S);
1326}
1327
1328void
1329StmtProfiler::VisitLambdaExpr(const LambdaExpr *S) {
1330  VisitExpr(S);
1331  for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
1332                                 CEnd = S->explicit_capture_end();
1333       C != CEnd; ++C) {
1334    ID.AddInteger(C->getCaptureKind());
1335    switch (C->getCaptureKind()) {
1336    case LCK_StarThis:
1337    case LCK_This:
1338      break;
1339    case LCK_ByRef:
1340    case LCK_ByCopy:
1341      VisitDecl(C->getCapturedVar());
1342      ID.AddBoolean(C->isPackExpansion());
1343      break;
1344    case LCK_VLAType:
1345      llvm_unreachable("VLA type in explicit captures.");
1346    }
1347  }
1348  // Note: If we actually needed to be able to match lambda
1349  // expressions, we would have to consider parameters and return type
1350  // here, among other things.
1351  VisitStmt(S->getBody());
1352}
1353
1354void
1355StmtProfiler::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *S) {
1356  VisitExpr(S);
1357}
1358
1359void StmtProfiler::VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
1360  VisitExpr(S);
1361  ID.AddBoolean(S->isGlobalDelete());
1362  ID.AddBoolean(S->isArrayForm());
1363  VisitDecl(S->getOperatorDelete());
1364}
1365
1366void StmtProfiler::VisitCXXNewExpr(const CXXNewExpr *S) {
1367  VisitExpr(S);
1368  VisitType(S->getAllocatedType());
1369  VisitDecl(S->getOperatorNew());
1370  VisitDecl(S->getOperatorDelete());
1371  ID.AddBoolean(S->isArray());
1372  ID.AddInteger(S->getNumPlacementArgs());
1373  ID.AddBoolean(S->isGlobalNew());
1374  ID.AddBoolean(S->isParenTypeId());
1375  ID.AddInteger(S->getInitializationStyle());
1376}
1377
1378void
1379StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) {
1380  VisitExpr(S);
1381  ID.AddBoolean(S->isArrow());
1382  VisitNestedNameSpecifier(S->getQualifier());
1383  ID.AddBoolean(S->getScopeTypeInfo() != nullptr);
1384  if (S->getScopeTypeInfo())
1385    VisitType(S->getScopeTypeInfo()->getType());
1386  ID.AddBoolean(S->getDestroyedTypeInfo() != nullptr);
1387  if (S->getDestroyedTypeInfo())
1388    VisitType(S->getDestroyedType());
1389  else
1390    ID.AddPointer(S->getDestroyedTypeIdentifier());
1391}
1392
1393void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) {
1394  VisitExpr(S);
1395  VisitNestedNameSpecifier(S->getQualifier());
1396  VisitName(S->getName());
1397  ID.AddBoolean(S->hasExplicitTemplateArgs());
1398  if (S->hasExplicitTemplateArgs())
1399    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
1400}
1401
1402void
1403StmtProfiler::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *S) {
1404  VisitOverloadExpr(S);
1405}
1406
1407void StmtProfiler::VisitTypeTraitExpr(const TypeTraitExpr *S) {
1408  VisitExpr(S);
1409  ID.AddInteger(S->getTrait());
1410  ID.AddInteger(S->getNumArgs());
1411  for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
1412    VisitType(S->getArg(I)->getType());
1413}
1414
1415void StmtProfiler::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *S) {
1416  VisitExpr(S);
1417  ID.AddInteger(S->getTrait());
1418  VisitType(S->getQueriedType());
1419}
1420
1421void StmtProfiler::VisitExpressionTraitExpr(const ExpressionTraitExpr *S) {
1422  VisitExpr(S);
1423  ID.AddInteger(S->getTrait());
1424  VisitExpr(S->getQueriedExpression());
1425}
1426
1427void StmtProfiler::VisitDependentScopeDeclRefExpr(
1428    const DependentScopeDeclRefExpr *S) {
1429  VisitExpr(S);
1430  VisitName(S->getDeclName());
1431  VisitNestedNameSpecifier(S->getQualifier());
1432  ID.AddBoolean(S->hasExplicitTemplateArgs());
1433  if (S->hasExplicitTemplateArgs())
1434    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
1435}
1436
1437void StmtProfiler::VisitExprWithCleanups(const ExprWithCleanups *S) {
1438  VisitExpr(S);
1439}
1440
1441void StmtProfiler::VisitCXXUnresolvedConstructExpr(
1442    const CXXUnresolvedConstructExpr *S) {
1443  VisitExpr(S);
1444  VisitType(S->getTypeAsWritten());
1445}
1446
1447void StmtProfiler::VisitCXXDependentScopeMemberExpr(
1448    const CXXDependentScopeMemberExpr *S) {
1449  ID.AddBoolean(S->isImplicitAccess());
1450  if (!S->isImplicitAccess()) {
1451    VisitExpr(S);
1452    ID.AddBoolean(S->isArrow());
1453  }
1454  VisitNestedNameSpecifier(S->getQualifier());
1455  VisitName(S->getMember());
1456  ID.AddBoolean(S->hasExplicitTemplateArgs());
1457  if (S->hasExplicitTemplateArgs())
1458    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
1459}
1460
1461void StmtProfiler::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *S) {
1462  ID.AddBoolean(S->isImplicitAccess());
1463  if (!S->isImplicitAccess()) {
1464    VisitExpr(S);
1465    ID.AddBoolean(S->isArrow());
1466  }
1467  VisitNestedNameSpecifier(S->getQualifier());
1468  VisitName(S->getMemberName());
1469  ID.AddBoolean(S->hasExplicitTemplateArgs());
1470  if (S->hasExplicitTemplateArgs())
1471    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
1472}
1473
1474void StmtProfiler::VisitCXXNoexceptExpr(const CXXNoexceptExpr *S) {
1475  VisitExpr(S);
1476}
1477
1478void StmtProfiler::VisitPackExpansionExpr(const PackExpansionExpr *S) {
1479  VisitExpr(S);
1480}
1481
1482void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) {
1483  VisitExpr(S);
1484  VisitDecl(S->getPack());
1485  if (S->isPartiallySubstituted()) {
1486    auto Args = S->getPartialArguments();
1487    ID.AddInteger(Args.size());
1488    for (const auto &TA : Args)
1489      VisitTemplateArgument(TA);
1490  } else {
1491    ID.AddInteger(0);
1492  }
1493}
1494
1495void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr(
1496    const SubstNonTypeTemplateParmPackExpr *S) {
1497  VisitExpr(S);
1498  VisitDecl(S->getParameterPack());
1499  VisitTemplateArgument(S->getArgumentPack());
1500}
1501
1502void StmtProfiler::VisitSubstNonTypeTemplateParmExpr(
1503    const SubstNonTypeTemplateParmExpr *E) {
1504  // Profile exactly as the replacement expression.
1505  Visit(E->getReplacement());
1506}
1507
1508void StmtProfiler::VisitFunctionParmPackExpr(const FunctionParmPackExpr *S) {
1509  VisitExpr(S);
1510  VisitDecl(S->getParameterPack());
1511  ID.AddInteger(S->getNumExpansions());
1512  for (FunctionParmPackExpr::iterator I = S->begin(), E = S->end(); I != E; ++I)
1513    VisitDecl(*I);
1514}
1515
1516void StmtProfiler::VisitMaterializeTemporaryExpr(
1517                                           const MaterializeTemporaryExpr *S) {
1518  VisitExpr(S);
1519}
1520
1521void StmtProfiler::VisitCXXFoldExpr(const CXXFoldExpr *S) {
1522  VisitExpr(S);
1523  ID.AddInteger(S->getOperator());
1524}
1525
1526void StmtProfiler::VisitCoroutineBodyStmt(const CoroutineBodyStmt *S) {
1527  VisitStmt(S);
1528}
1529
1530void StmtProfiler::VisitCoreturnStmt(const CoreturnStmt *S) {
1531  VisitStmt(S);
1532}
1533
1534void StmtProfiler::VisitCoawaitExpr(const CoawaitExpr *S) {
1535  VisitExpr(S);
1536}
1537
1538void StmtProfiler::VisitCoyieldExpr(const CoyieldExpr *S) {
1539  VisitExpr(S);
1540}
1541
1542void StmtProfiler::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
1543  VisitExpr(E);
1544}
1545
1546void StmtProfiler::VisitTypoExpr(const TypoExpr *E) {
1547  VisitExpr(E);
1548}
1549
1550void StmtProfiler::VisitObjCStringLiteral(const ObjCStringLiteral *S) {
1551  VisitExpr(S);
1552}
1553
1554void StmtProfiler::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
1555  VisitExpr(E);
1556}
1557
1558void StmtProfiler::VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
1559  VisitExpr(E);
1560}
1561
1562void StmtProfiler::VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
1563  VisitExpr(E);
1564}
1565
1566void StmtProfiler::VisitObjCEncodeExpr(const ObjCEncodeExpr *S) {
1567  VisitExpr(S);
1568  VisitType(S->getEncodedType());
1569}
1570
1571void StmtProfiler::VisitObjCSelectorExpr(const ObjCSelectorExpr *S) {
1572  VisitExpr(S);
1573  VisitName(S->getSelector());
1574}
1575
1576void StmtProfiler::VisitObjCProtocolExpr(const ObjCProtocolExpr *S) {
1577  VisitExpr(S);
1578  VisitDecl(S->getProtocol());
1579}
1580
1581void StmtProfiler::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *S) {
1582  VisitExpr(S);
1583  VisitDecl(S->getDecl());
1584  ID.AddBoolean(S->isArrow());
1585  ID.AddBoolean(S->isFreeIvar());
1586}
1587
1588void StmtProfiler::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *S) {
1589  VisitExpr(S);
1590  if (S->isImplicitProperty()) {
1591    VisitDecl(S->getImplicitPropertyGetter());
1592    VisitDecl(S->getImplicitPropertySetter());
1593  } else {
1594    VisitDecl(S->getExplicitProperty());
1595  }
1596  if (S->isSuperReceiver()) {
1597    ID.AddBoolean(S->isSuperReceiver());
1598    VisitType(S->getSuperReceiverType());
1599  }
1600}
1601
1602void StmtProfiler::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *S) {
1603  VisitExpr(S);
1604  VisitDecl(S->getAtIndexMethodDecl());
1605  VisitDecl(S->setAtIndexMethodDecl());
1606}
1607
1608void StmtProfiler::VisitObjCMessageExpr(const ObjCMessageExpr *S) {
1609  VisitExpr(S);
1610  VisitName(S->getSelector());
1611  VisitDecl(S->getMethodDecl());
1612}
1613
1614void StmtProfiler::VisitObjCIsaExpr(const ObjCIsaExpr *S) {
1615  VisitExpr(S);
1616  ID.AddBoolean(S->isArrow());
1617}
1618
1619void StmtProfiler::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *S) {
1620  VisitExpr(S);
1621  ID.AddBoolean(S->getValue());
1622}
1623
1624void StmtProfiler::VisitObjCIndirectCopyRestoreExpr(
1625    const ObjCIndirectCopyRestoreExpr *S) {
1626  VisitExpr(S);
1627  ID.AddBoolean(S->shouldCopy());
1628}
1629
1630void StmtProfiler::VisitObjCBridgedCastExpr(const ObjCBridgedCastExpr *S) {
1631  VisitExplicitCastExpr(S);
1632  ID.AddBoolean(S->getBridgeKind());
1633}
1634
1635void StmtProfiler::VisitDecl(const Decl *D) {
1636  ID.AddInteger(D? D->getKind() : 0);
1637
1638  if (Canonical && D) {
1639    if (const NonTypeTemplateParmDecl *NTTP =
1640          dyn_cast<NonTypeTemplateParmDecl>(D)) {
1641      ID.AddInteger(NTTP->getDepth());
1642      ID.AddInteger(NTTP->getIndex());
1643      ID.AddBoolean(NTTP->isParameterPack());
1644      VisitType(NTTP->getType());
1645      return;
1646    }
1647
1648    if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
1649      // The Itanium C++ ABI uses the type, scope depth, and scope
1650      // index of a parameter when mangling expressions that involve
1651      // function parameters, so we will use the parameter's type for
1652      // establishing function parameter identity. That way, our
1653      // definition of "equivalent" (per C++ [temp.over.link]) is at
1654      // least as strong as the definition of "equivalent" used for
1655      // name mangling.
1656      VisitType(Parm->getType());
1657      ID.AddInteger(Parm->getFunctionScopeDepth());
1658      ID.AddInteger(Parm->getFunctionScopeIndex());
1659      return;
1660    }
1661
1662    if (const TemplateTypeParmDecl *TTP =
1663          dyn_cast<TemplateTypeParmDecl>(D)) {
1664      ID.AddInteger(TTP->getDepth());
1665      ID.AddInteger(TTP->getIndex());
1666      ID.AddBoolean(TTP->isParameterPack());
1667      return;
1668    }
1669
1670    if (const TemplateTemplateParmDecl *TTP =
1671          dyn_cast<TemplateTemplateParmDecl>(D)) {
1672      ID.AddInteger(TTP->getDepth());
1673      ID.AddInteger(TTP->getIndex());
1674      ID.AddBoolean(TTP->isParameterPack());
1675      return;
1676    }
1677  }
1678
1679  ID.AddPointer(D? D->getCanonicalDecl() : nullptr);
1680}
1681
1682void StmtProfiler::VisitType(QualType T) {
1683  if (Canonical)
1684    T = Context.getCanonicalType(T);
1685
1686  ID.AddPointer(T.getAsOpaquePtr());
1687}
1688
1689void StmtProfiler::VisitName(DeclarationName Name) {
1690  ID.AddPointer(Name.getAsOpaquePtr());
1691}
1692
1693void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) {
1694  if (Canonical)
1695    NNS = Context.getCanonicalNestedNameSpecifier(NNS);
1696  ID.AddPointer(NNS);
1697}
1698
1699void StmtProfiler::VisitTemplateName(TemplateName Name) {
1700  if (Canonical)
1701    Name = Context.getCanonicalTemplateName(Name);
1702
1703  Name.Profile(ID);
1704}
1705
1706void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args,
1707                                          unsigned NumArgs) {
1708  ID.AddInteger(NumArgs);
1709  for (unsigned I = 0; I != NumArgs; ++I)
1710    VisitTemplateArgument(Args[I].getArgument());
1711}
1712
1713void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) {
1714  // Mostly repetitive with TemplateArgument::Profile!
1715  ID.AddInteger(Arg.getKind());
1716  switch (Arg.getKind()) {
1717  case TemplateArgument::Null:
1718    break;
1719
1720  case TemplateArgument::Type:
1721    VisitType(Arg.getAsType());
1722    break;
1723
1724  case TemplateArgument::Template:
1725  case TemplateArgument::TemplateExpansion:
1726    VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
1727    break;
1728
1729  case TemplateArgument::Declaration:
1730    VisitDecl(Arg.getAsDecl());
1731    break;
1732
1733  case TemplateArgument::NullPtr:
1734    VisitType(Arg.getNullPtrType());
1735    break;
1736
1737  case TemplateArgument::Integral:
1738    Arg.getAsIntegral().Profile(ID);
1739    VisitType(Arg.getIntegralType());
1740    break;
1741
1742  case TemplateArgument::Expression:
1743    Visit(Arg.getAsExpr());
1744    break;
1745
1746  case TemplateArgument::Pack:
1747    for (const auto &P : Arg.pack_elements())
1748      VisitTemplateArgument(P);
1749    break;
1750  }
1751}
1752
1753void Stmt::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
1754                   bool Canonical) const {
1755  StmtProfiler Profiler(ID, Context, Canonical);
1756  Profiler.Visit(this);
1757}
1758