Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Sema/SemaOpenMP.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
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
/// \file
9
/// This file implements semantic analysis for OpenMP directives and
10
/// clauses.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "TreeTransform.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/ASTMutationListener.h"
17
#include "clang/AST/CXXInheritance.h"
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/DeclCXX.h"
20
#include "clang/AST/DeclOpenMP.h"
21
#include "clang/AST/StmtCXX.h"
22
#include "clang/AST/StmtOpenMP.h"
23
#include "clang/AST/StmtVisitor.h"
24
#include "clang/AST/TypeOrdering.h"
25
#include "clang/Basic/OpenMPKinds.h"
26
#include "clang/Sema/Initialization.h"
27
#include "clang/Sema/Lookup.h"
28
#include "clang/Sema/Scope.h"
29
#include "clang/Sema/ScopeInfo.h"
30
#include "clang/Sema/SemaInternal.h"
31
#include "llvm/ADT/PointerEmbeddedInt.h"
32
using namespace clang;
33
34
//===----------------------------------------------------------------------===//
35
// Stack of data-sharing attributes for variables
36
//===----------------------------------------------------------------------===//
37
38
static const Expr *checkMapClauseExpressionBase(
39
    Sema &SemaRef, Expr *E,
40
    OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
41
    OpenMPClauseKind CKind, bool NoDiagnose);
42
43
namespace {
44
/// Default data sharing attributes, which can be applied to directive.
45
enum DefaultDataSharingAttributes {
46
  DSA_unspecified = 0, /// Data sharing attribute not specified.
47
  DSA_none = 1 << 0,   /// Default data sharing attribute 'none'.
48
  DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
49
};
50
51
/// Attributes of the defaultmap clause.
52
enum DefaultMapAttributes {
53
  DMA_unspecified,   /// Default mapping is not specified.
54
  DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
55
};
56
57
/// Stack for tracking declarations used in OpenMP directives and
58
/// clauses and their data-sharing attributes.
59
class DSAStackTy {
60
public:
61
  struct DSAVarData {
62
    OpenMPDirectiveKind DKind = OMPD_unknown;
63
    OpenMPClauseKind CKind = OMPC_unknown;
64
    const Expr *RefExpr = nullptr;
65
    DeclRefExpr *PrivateCopy = nullptr;
66
    SourceLocation ImplicitDSALoc;
67
4.00M
    DSAVarData() = default;
68
    DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69
               const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70
               SourceLocation ImplicitDSALoc)
71
        : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72
572
          PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
73
  };
74
  using OperatorOffsetTy =
75
      llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
76
  using DoacrossDependMapTy =
77
      llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
78
79
private:
80
  struct DSAInfo {
81
    OpenMPClauseKind Attributes = OMPC_unknown;
82
    /// Pointer to a reference expression and a flag which shows that the
83
    /// variable is marked as lastprivate(true) or not (false).
84
    llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
85
    DeclRefExpr *PrivateCopy = nullptr;
86
  };
87
  using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88
  using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89
  using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90
  using LoopControlVariablesMapTy =
91
      llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
92
  /// Struct that associates a component with the clause kind where they are
93
  /// found.
94
  struct MappedExprComponentTy {
95
    OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96
    OpenMPClauseKind Kind = OMPC_unknown;
97
  };
98
  using MappedExprComponentsTy =
99
      llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100
  using CriticalsWithHintsTy =
101
      llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
102
  struct ReductionData {
103
    using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
104
    SourceRange ReductionRange;
105
    llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
106
1.09k
    ReductionData() = default;
107
1.08k
    void set(BinaryOperatorKind BO, SourceRange RR) {
108
1.08k
      ReductionRange = RR;
109
1.08k
      ReductionOp = BO;
110
1.08k
    }
111
18
    void set(const Expr *RefExpr, SourceRange RR) {
112
18
      ReductionRange = RR;
113
18
      ReductionOp = RefExpr;
114
18
    }
115
  };
116
  using DeclReductionMapTy =
117
      llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
118
119
  struct SharingMapTy {
120
    DeclSAMapTy SharingMap;
121
    DeclReductionMapTy ReductionMap;
122
    AlignedMapTy AlignedMap;
123
    MappedExprComponentsTy MappedExprComponents;
124
    LoopControlVariablesMapTy LCVMap;
125
    DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
126
    SourceLocation DefaultAttrLoc;
127
    DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128
    SourceLocation DefaultMapAttrLoc;
129
    OpenMPDirectiveKind Directive = OMPD_unknown;
130
    DeclarationNameInfo DirectiveName;
131
    Scope *CurScope = nullptr;
132
    SourceLocation ConstructLoc;
133
    /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134
    /// get the data (loop counters etc.) about enclosing loop-based construct.
135
    /// This data is required during codegen.
136
    DoacrossDependMapTy DoacrossDepends;
137
    /// First argument (Expr *) contains optional argument of the
138
    /// 'ordered' clause, the second one is true if the regions has 'ordered'
139
    /// clause, false otherwise.
140
    llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
141
    unsigned AssociatedLoops = 1;
142
    bool HasMutipleLoops = false;
143
    const Decl *PossiblyLoopCounter = nullptr;
144
    bool NowaitRegion = false;
145
    bool CancelRegion = false;
146
    bool LoopStart = false;
147
    bool BodyComplete = false;
148
    SourceLocation InnerTeamsRegionLoc;
149
    /// Reference to the taskgroup task_reduction reference expression.
150
    Expr *TaskgroupReductionRef = nullptr;
151
    llvm::DenseSet<QualType> MappedClassesQualTypes;
152
    /// List of globals marked as declare target link in this target region
153
    /// (isOpenMPTargetExecutionDirective(Directive) == true).
154
    llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
155
    SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
156
                 Scope *CurScope, SourceLocation Loc)
157
        : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
158
171k
          ConstructLoc(Loc) {}
159
    SharingMapTy() = default;
160
  };
161
162
  using StackTy = SmallVector<SharingMapTy, 4>;
163
164
  /// Stack of used declaration and their data-sharing attributes.
165
  DeclSAMapTy Threadprivates;
166
  const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
167
  SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
168
  /// true, if check for DSA must be from parent directive, false, if
169
  /// from current directive.
170
  OpenMPClauseKind ClauseKindMode = OMPC_unknown;
171
  Sema &SemaRef;
172
  bool ForceCapturing = false;
173
  /// true if all the vaiables in the target executable directives must be
174
  /// captured by reference.
175
  bool ForceCaptureByReferenceInTargetExecutable = false;
176
  CriticalsWithHintsTy Criticals;
177
  unsigned IgnoredStackElements = 0;
178
179
  /// Iterators over the stack iterate in order from innermost to outermost
180
  /// directive.
181
  using const_iterator = StackTy::const_reverse_iterator;
182
530k
  const_iterator begin() const {
183
530k
    return Stack.empty() ? 
const_iterator()4.34k
184
530k
                         : 
Stack.back().first.rbegin() + IgnoredStackElements525k
;
185
530k
  }
186
3.96M
  const_iterator end() const {
187
3.96M
    return Stack.empty() ? 
const_iterator()4.34k
:
Stack.back().first.rend()3.96M
;
188
3.96M
  }
189
  using iterator = StackTy::reverse_iterator;
190
1.40M
  iterator begin() {
191
1.40M
    return Stack.empty() ? 
iterator()0
192
1.40M
                         : Stack.back().first.rbegin() + IgnoredStackElements;
193
1.40M
  }
194
1.41M
  iterator end() {
195
1.41M
    return Stack.empty() ? 
iterator()0
: Stack.back().first.rend();
196
1.41M
  }
197
198
  // Convenience operations to get at the elements of the stack.
199
200
18.9M
  bool isStackEmpty() const {
201
18.9M
    return Stack.empty() ||
202
18.9M
           
Stack.back().second != CurrentNonCapturingFunctionScope18.8M
||
203
18.9M
           
Stack.back().first.size() <= IgnoredStackElements18.8M
;
204
18.9M
  }
205
16.6M
  size_t getStackSize() const {
206
16.6M
    return isStackEmpty() ? 
0173k
207
16.6M
                          : 
Stack.back().first.size() - IgnoredStackElements16.5M
;
208
16.6M
  }
209
210
11.6M
  SharingMapTy *getTopOfStackOrNull() {
211
11.6M
    size_t Size = getStackSize();
212
11.6M
    if (Size == 0)
213
167k
      return nullptr;
214
11.5M
    return &Stack.back().first[Size - 1];
215
11.5M
  }
216
7.80M
  const SharingMapTy *getTopOfStackOrNull() const {
217
7.80M
    return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull();
218
7.80M
  }
219
3.89M
  SharingMapTy &getTopOfStack() {
220
3.89M
    assert(!isStackEmpty() && "no current directive");
221
3.89M
    return *getTopOfStackOrNull();
222
3.89M
  }
223
3.08M
  const SharingMapTy &getTopOfStack() const {
224
3.08M
    return const_cast<DSAStackTy&>(*this).getTopOfStack();
225
3.08M
  }
226
227
291k
  SharingMapTy *getSecondOnStackOrNull() {
228
291k
    size_t Size = getStackSize();
229
291k
    if (Size <= 1)
230
83.0k
      return nullptr;
231
208k
    return &Stack.back().first[Size - 2];
232
208k
  }
233
268k
  const SharingMapTy *getSecondOnStackOrNull() const {
234
268k
    return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull();
235
268k
  }
236
237
  /// Get the stack element at a certain level (previously returned by
238
  /// \c getNestingLevel).
239
  ///
240
  /// Note that nesting levels count from outermost to innermost, and this is
241
  /// the reverse of our iteration order where new inner levels are pushed at
242
  /// the front of the stack.
243
4.58M
  SharingMapTy &getStackElemAtLevel(unsigned Level) {
244
4.58M
    assert(Level < getStackSize() && "no such stack element");
245
4.58M
    return Stack.back().first[Level];
246
4.58M
  }
247
4.58M
  const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
248
4.58M
    return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level);
249
4.58M
  }
250
251
  DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
252
253
  /// Checks if the variable is a local for OpenMP region.
254
  bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
255
256
  /// Vector of previously declared requires directives
257
  SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
258
  /// omp_allocator_handle_t type.
259
  QualType OMPAllocatorHandleT;
260
  /// Expression for the predefined allocators.
261
  Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
262
      nullptr};
263
  /// Vector of previously encountered target directives
264
  SmallVector<SourceLocation, 2> TargetLocations;
265
266
public:
267
41.5k
  explicit DSAStackTy(Sema &S) : SemaRef(S) {}
268
269
  /// Sets omp_allocator_handle_t type.
270
251
  void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
271
  /// Gets omp_allocator_handle_t type.
272
1.28k
  QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
273
  /// Sets the given default allocator.
274
  void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
275
2.00k
                    Expr *Allocator) {
276
2.00k
    OMPPredefinedAllocators[AllocatorKind] = Allocator;
277
2.00k
  }
278
  /// Returns the specified default allocator.
279
3.42k
  Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
280
3.42k
    return OMPPredefinedAllocators[AllocatorKind];
281
3.42k
  }
282
283
1.99M
  bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
284
153k
  OpenMPClauseKind getClauseParsingMode() const {
285
153k
    assert(isClauseParsingMode() && "Must be in clause parsing mode.");
286
153k
    return ClauseKindMode;
287
153k
  }
288
228k
  void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
289
290
164k
  bool isBodyComplete() const {
291
164k
    const SharingMapTy *Top = getTopOfStackOrNull();
292
164k
    return Top && 
Top->BodyComplete130k
;
293
164k
  }
294
161k
  void setBodyComplete() {
295
161k
    getTopOfStack().BodyComplete = true;
296
161k
  }
297
298
198k
  bool isForceVarCapturing() const { return ForceCapturing; }
299
58.7k
  void setForceVarCapturing(bool V) { ForceCapturing = V; }
300
301
41.2k
  void setForceCaptureByReferenceInTargetExecutable(bool V) {
302
41.2k
    ForceCaptureByReferenceInTargetExecutable = V;
303
41.2k
  }
304
535k
  bool isForceCaptureByReferenceInTargetExecutable() const {
305
535k
    return ForceCaptureByReferenceInTargetExecutable;
306
535k
  }
307
308
  void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
309
171k
            Scope *CurScope, SourceLocation Loc) {
310
171k
    assert(!IgnoredStackElements &&
311
171k
           "cannot change stack while ignoring elements");
312
171k
    if (Stack.empty() ||
313
171k
        
Stack.back().second != CurrentNonCapturingFunctionScope152k
)
314
19.0k
      Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
315
171k
    Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
316
171k
    Stack.back().first.back().DefaultAttrLoc = Loc;
317
171k
  }
318
319
171k
  void pop() {
320
171k
    assert(!IgnoredStackElements &&
321
171k
           "cannot change stack while ignoring elements");
322
171k
    assert(!Stack.back().first.empty() &&
323
171k
           "Data-sharing attributes stack is empty!");
324
171k
    Stack.back().first.pop_back();
325
171k
  }
326
327
  /// RAII object to temporarily leave the scope of a directive when we want to
328
  /// logically operate in its parent.
329
  class ParentDirectiveScope {
330
    DSAStackTy &Self;
331
    bool Active;
332
  public:
333
    ParentDirectiveScope(DSAStackTy &Self, bool Activate)
334
983k
        : Self(Self), Active(false) {
335
983k
      if (Activate)
336
25.3k
        enable();
337
983k
    }
338
983k
    ~ParentDirectiveScope() { disable(); }
339
983k
    void disable() {
340
983k
      if (Active) {
341
25.3k
        --Self.IgnoredStackElements;
342
25.3k
        Active = false;
343
25.3k
      }
344
983k
    }
345
25.3k
    void enable() {
346
25.3k
      if (!Active) {
347
25.3k
        ++Self.IgnoredStackElements;
348
25.3k
        Active = true;
349
25.3k
      }
350
25.3k
    }
351
  };
352
353
  /// Marks that we're started loop parsing.
354
86.4k
  void loopInit() {
355
86.4k
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
356
86.4k
           "Expected loop-based directive.");
357
86.4k
    getTopOfStack().LoopStart = true;
358
86.4k
  }
359
  /// Start capturing of the variables in the loop context.
360
107k
  void loopStart() {
361
107k
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
362
107k
           "Expected loop-based directive.");
363
107k
    getTopOfStack().LoopStart = false;
364
107k
  }
365
  /// true, if variables are captured, false otherwise.
366
338k
  bool isLoopStarted() const {
367
338k
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
368
338k
           "Expected loop-based directive.");
369
338k
    return !getTopOfStack().LoopStart;
370
338k
  }
371
  /// Marks (or clears) declaration as possibly loop counter.
372
93.8k
  void resetPossibleLoopCounter(const Decl *D = nullptr) {
373
93.8k
    getTopOfStack().PossiblyLoopCounter =
374
93.8k
        D ? 
D->getCanonicalDecl()22.5k
:
D71.2k
;
375
93.8k
  }
376
  /// Gets the possible loop counter decl.
377
811k
  const Decl *getPossiblyLoopCunter() const {
378
811k
    return getTopOfStack().PossiblyLoopCounter;
379
811k
  }
380
  /// Start new OpenMP region stack in new non-capturing function.
381
51.3k
  void pushFunction() {
382
51.3k
    assert(!IgnoredStackElements &&
383
51.3k
           "cannot change stack while ignoring elements");
384
51.3k
    const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
385
51.3k
    assert(!isa<CapturingScopeInfo>(CurFnScope));
386
51.3k
    CurrentNonCapturingFunctionScope = CurFnScope;
387
51.3k
  }
388
  /// Pop region stack for non-capturing function.
389
347k
  void popFunction(const FunctionScopeInfo *OldFSI) {
390
347k
    assert(!IgnoredStackElements &&
391
347k
           "cannot change stack while ignoring elements");
392
347k
    if (!Stack.empty() && 
Stack.back().second == OldFSI317k
) {
393
19.0k
      assert(Stack.back().first.empty());
394
19.0k
      Stack.pop_back();
395
19.0k
    }
396
347k
    CurrentNonCapturingFunctionScope = nullptr;
397
643k
    for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
398
643k
      if (!isa<CapturingScopeInfo>(FSI)) {
399
300k
        CurrentNonCapturingFunctionScope = FSI;
400
300k
        break;
401
300k
      }
402
643k
    }
403
347k
  }
404
405
38
  void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
406
38
    Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
407
38
  }
408
  const std::pair<const OMPCriticalDirective *, llvm::APSInt>
409
653
  getCriticalWithHint(const DeclarationNameInfo &Name) const {
410
653
    auto I = Criticals.find(Name.getAsString());
411
653
    if (I != Criticals.end())
412
100
      return I->second;
413
553
    return std::make_pair(nullptr, llvm::APSInt());
414
553
  }
415
  /// If 'aligned' declaration for given variable \a D was not seen yet,
416
  /// add it and return NULL; otherwise return previous occurrence's expression
417
  /// for diagnostics.
418
  const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
419
420
  /// Register specified variable as loop control variable.
421
  void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
422
  /// Check if the specified variable is a loop control variable for
423
  /// current region.
424
  /// \return The index of the loop control variable in the list of associated
425
  /// for-loops (from outer to inner).
426
  const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
427
  /// Check if the specified variable is a loop control variable for
428
  /// parent region.
429
  /// \return The index of the loop control variable in the list of associated
430
  /// for-loops (from outer to inner).
431
  const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
432
  /// Get the loop control variable for the I-th loop (or nullptr) in
433
  /// parent directive.
434
  const ValueDecl *getParentLoopControlVariable(unsigned I) const;
435
436
  /// Adds explicit data sharing attribute to the specified declaration.
437
  void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
438
              DeclRefExpr *PrivateCopy = nullptr);
439
440
  /// Adds additional information for the reduction items with the reduction id
441
  /// represented as an operator.
442
  void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
443
                                 BinaryOperatorKind BOK);
444
  /// Adds additional information for the reduction items with the reduction id
445
  /// represented as reduction identifier.
446
  void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
447
                                 const Expr *ReductionRef);
448
  /// Returns the location and reduction operation from the innermost parent
449
  /// region for the given \p D.
450
  const DSAVarData
451
  getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
452
                                   BinaryOperatorKind &BOK,
453
                                   Expr *&TaskgroupDescriptor) const;
454
  /// Returns the location and reduction operation from the innermost parent
455
  /// region for the given \p D.
456
  const DSAVarData
457
  getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
458
                                   const Expr *&ReductionRef,
459
                                   Expr *&TaskgroupDescriptor) const;
460
  /// Return reduction reference expression for the current taskgroup.
461
1.77k
  Expr *getTaskgroupReductionRef() const {
462
1.77k
    assert(getTopOfStack().Directive == OMPD_taskgroup &&
463
1.77k
           "taskgroup reference expression requested for non taskgroup "
464
1.77k
           "directive.");
465
1.77k
    return getTopOfStack().TaskgroupReductionRef;
466
1.77k
  }
467
  /// Checks if the given \p VD declaration is actually a taskgroup reduction
468
  /// descriptor variable at the \p Level of OpenMP regions.
469
5.38k
  bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
470
5.38k
    return getStackElemAtLevel(Level).TaskgroupReductionRef &&
471
5.38k
           cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
472
4.72k
                   ->getDecl() == VD;
473
5.38k
  }
474
475
  /// Returns data sharing attributes from top of the stack for the
476
  /// specified declaration.
477
  const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
478
  /// Returns data-sharing attributes for the specified declaration.
479
  const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
480
  /// Checks if the specified variables has data-sharing attributes which
481
  /// match specified \a CPred predicate in any directive which matches \a DPred
482
  /// predicate.
483
  const DSAVarData
484
  hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
485
         const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
486
         bool FromParent) const;
487
  /// Checks if the specified variables has data-sharing attributes which
488
  /// match specified \a CPred predicate in any innermost directive which
489
  /// matches \a DPred predicate.
490
  const DSAVarData
491
  hasInnermostDSA(ValueDecl *D,
492
                  const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
493
                  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
494
                  bool FromParent) const;
495
  /// Checks if the specified variables has explicit data-sharing
496
  /// attributes which match specified \a CPred predicate at the specified
497
  /// OpenMP region.
498
  bool hasExplicitDSA(const ValueDecl *D,
499
                      const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
500
                      unsigned Level, bool NotLastprivate = false) const;
501
502
  /// Returns true if the directive at level \Level matches in the
503
  /// specified \a DPred predicate.
504
  bool hasExplicitDirective(
505
      const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
506
      unsigned Level) const;
507
508
  /// Finds a directive which matches specified \a DPred predicate.
509
  bool hasDirective(
510
      const llvm::function_ref<bool(
511
          OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
512
          DPred,
513
      bool FromParent) const;
514
515
  /// Returns currently analyzed directive.
516
5.01M
  OpenMPDirectiveKind getCurrentDirective() const {
517
5.01M
    const SharingMapTy *Top = getTopOfStackOrNull();
518
5.01M
    return Top ? 
Top->Directive4.88M
:
OMPD_unknown132k
;
519
5.01M
  }
520
  /// Returns directive kind at specified level.
521
15.9k
  OpenMPDirectiveKind getDirective(unsigned Level) const {
522
15.9k
    assert(!isStackEmpty() && "No directive at specified level.");
523
15.9k
    return getStackElemAtLevel(Level).Directive;
524
15.9k
  }
525
  /// Returns parent directive.
526
264k
  OpenMPDirectiveKind getParentDirective() const {
527
264k
    const SharingMapTy *Parent = getSecondOnStackOrNull();
528
264k
    return Parent ? 
Parent->Directive181k
:
OMPD_unknown82.8k
;
529
264k
  }
530
531
  /// Add requires decl to internal vector
532
58
  void addRequiresDecl(OMPRequiresDecl *RD) {
533
58
    RequiresDecls.push_back(RD);
534
58
  }
535
536
  /// Checks if the defined 'requires' directive has specified type of clause.
537
  template <typename ClauseType>
538
193k
  bool hasRequiresDeclWithClause() {
539
193k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
540
13
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
13
        return isa<ClauseType>(C);
542
13
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>()::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
540
4
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
4
        return isa<ClauseType>(C);
542
4
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>()::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
540
9
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
9
        return isa<ClauseType>(C);
542
9
      });
Unexecuted instantiation: SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>()::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Unexecuted instantiation: SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>()::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
543
13
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>()::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
539
4
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
540
4
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
4
        return isa<ClauseType>(C);
542
4
      });
543
4
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>()::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
539
9
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
540
9
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
9
        return isa<ClauseType>(C);
542
9
      });
543
9
    });
Unexecuted instantiation: SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>()::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Unexecuted instantiation: SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>()::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
544
193k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>()
Line
Count
Source
538
47.4k
  bool hasRequiresDeclWithClause() {
539
47.4k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
540
47.4k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
47.4k
        return isa<ClauseType>(C);
542
47.4k
      });
543
47.4k
    });
544
47.4k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>()
Line
Count
Source
538
50.8k
  bool hasRequiresDeclWithClause() {
539
50.8k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
540
50.8k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
50.8k
        return isa<ClauseType>(C);
542
50.8k
      });
543
50.8k
    });
544
50.8k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>()
Line
Count
Source
538
47.4k
  bool hasRequiresDeclWithClause() {
539
47.4k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
540
47.4k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
47.4k
        return isa<ClauseType>(C);
542
47.4k
      });
543
47.4k
    });
544
47.4k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>()
Line
Count
Source
538
47.4k
  bool hasRequiresDeclWithClause() {
539
47.4k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
540
47.4k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
541
47.4k
        return isa<ClauseType>(C);
542
47.4k
      });
543
47.4k
    });
544
47.4k
  }
545
546
  /// Checks for a duplicate clause amongst previously declared requires
547
  /// directives
548
71
  bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
549
71
    bool IsDuplicate = false;
550
75
    for (OMPClause *CNew : ClauseList) {
551
133
      for (const OMPRequiresDecl *D : RequiresDecls) {
552
133
        for (const OMPClause *CPrev : D->clauselists()) {
553
133
          if (CNew->getClauseKind() == CPrev->getClauseKind()) {
554
17
            SemaRef.Diag(CNew->getBeginLoc(),
555
17
                         diag::err_omp_requires_clause_redeclaration)
556
17
                << getOpenMPClauseName(CNew->getClauseKind());
557
17
            SemaRef.Diag(CPrev->getBeginLoc(),
558
17
                         diag::note_omp_requires_previous_clause)
559
17
                << getOpenMPClauseName(CPrev->getClauseKind());
560
17
            IsDuplicate = true;
561
17
          }
562
133
        }
563
133
      }
564
75
    }
565
71
    return IsDuplicate;
566
71
  }
567
568
  /// Add location of previously encountered target to internal vector
569
47.4k
  void addTargetDirLocation(SourceLocation LocStart) {
570
47.4k
    TargetLocations.push_back(LocStart);
571
47.4k
  }
572
573
  // Return previously encountered target region locations.
574
71
  ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
575
71
    return TargetLocations;
576
71
  }
577
578
  /// Set default data sharing attribute to none.
579
571
  void setDefaultDSANone(SourceLocation Loc) {
580
571
    getTopOfStack().DefaultAttr = DSA_none;
581
571
    getTopOfStack().DefaultAttrLoc = Loc;
582
571
  }
583
  /// Set default data sharing attribute to shared.
584
302
  void setDefaultDSAShared(SourceLocation Loc) {
585
302
    getTopOfStack().DefaultAttr = DSA_shared;
586
302
    getTopOfStack().DefaultAttrLoc = Loc;
587
302
  }
588
  /// Set default data mapping attribute to 'tofrom:scalar'.
589
88
  void setDefaultDMAToFromScalar(SourceLocation Loc) {
590
88
    getTopOfStack().DefaultMapAttr = DMA_tofrom_scalar;
591
88
    getTopOfStack().DefaultMapAttrLoc = Loc;
592
88
  }
593
594
410k
  DefaultDataSharingAttributes getDefaultDSA() const {
595
410k
    return isStackEmpty() ? 
DSA_unspecified0
596
410k
                          : getTopOfStack().DefaultAttr;
597
410k
  }
598
195
  SourceLocation getDefaultDSALocation() const {
599
195
    return isStackEmpty() ? 
SourceLocation()0
600
195
                          : getTopOfStack().DefaultAttrLoc;
601
195
  }
602
31.4k
  DefaultMapAttributes getDefaultDMA() const {
603
31.4k
    return isStackEmpty() ? 
DMA_unspecified0
604
31.4k
                          : getTopOfStack().DefaultMapAttr;
605
31.4k
  }
606
450k
  DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
607
450k
    return getStackElemAtLevel(Level).DefaultMapAttr;
608
450k
  }
609
0
  SourceLocation getDefaultDMALocation() const {
610
0
    return isStackEmpty() ? SourceLocation()
611
0
                          : getTopOfStack().DefaultMapAttrLoc;
612
0
  }
613
614
  /// Checks if the specified variable is a threadprivate.
615
811k
  bool isThreadPrivate(VarDecl *D) {
616
811k
    const DSAVarData DVar = getTopDSA(D, false);
617
811k
    return isOpenMPThreadPrivate(DVar.CKind);
618
811k
  }
619
620
  /// Marks current region as ordered (it has an 'ordered' clause).
621
  void setOrderedRegion(bool IsOrdered, const Expr *Param,
622
988
                        OMPOrderedClause *Clause) {
623
988
    if (IsOrdered)
624
988
      getTopOfStack().OrderedRegion.emplace(Param, Clause);
625
0
    else
626
0
      getTopOfStack().OrderedRegion.reset();
627
988
  }
628
  /// Returns true, if region is ordered (has associated 'ordered' clause),
629
  /// false - otherwise.
630
55.1k
  bool isOrderedRegion() const {
631
55.1k
    if (const SharingMapTy *Top = getTopOfStackOrNull())
632
55.1k
      return Top->OrderedRegion.hasValue();
633
0
    return false;
634
0
  }
635
  /// Returns optional parameter for the ordered region.
636
1.82k
  std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
637
1.82k
    if (const SharingMapTy *Top = getTopOfStackOrNull())
638
1.82k
      if (Top->OrderedRegion.hasValue())
639
1.82k
        return Top->OrderedRegion.getValue();
640
0
    return std::make_pair(nullptr, nullptr);
641
0
  }
642
  /// Returns true, if parent region is ordered (has associated
643
  /// 'ordered' clause), false - otherwise.
644
1.36k
  bool isParentOrderedRegion() const {
645
1.36k
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
646
1.34k
      return Parent->OrderedRegion.hasValue();
647
12
    return false;
648
12
  }
649
  /// Returns optional parameter for the ordered region.
650
  std::pair<const Expr *, OMPOrderedClause *>
651
1.96k
  getParentOrderedRegionParam() const {
652
1.96k
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
653
1.75k
      if (Parent->OrderedRegion.hasValue())
654
1.74k
        return Parent->OrderedRegion.getValue();
655
220
    return std::make_pair(nullptr, nullptr);
656
220
  }
657
  /// Marks current region as nowait (it has a 'nowait' clause).
658
974
  void setNowaitRegion(bool IsNowait = true) {
659
974
    getTopOfStack().NowaitRegion = IsNowait;
660
974
  }
661
  /// Returns true, if parent region is nowait (has associated
662
  /// 'nowait' clause), false - otherwise.
663
365
  bool isParentNowaitRegion() const {
664
365
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
665
365
      return Parent->NowaitRegion;
666
0
    return false;
667
0
  }
668
  /// Marks parent region as cancel region.
669
641
  void setParentCancelRegion(bool Cancel = true) {
670
641
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
671
641
      Parent->CancelRegion |= Cancel;
672
641
  }
673
  /// Return true if current region has inner cancel construct.
674
37.6k
  bool isCancelRegion() const {
675
37.6k
    const SharingMapTy *Top = getTopOfStackOrNull();
676
37.6k
    return Top ? Top->CancelRegion : 
false0
;
677
37.6k
  }
678
679
  /// Set collapse value for the region.
680
88.8k
  void setAssociatedLoops(unsigned Val) {
681
88.8k
    getTopOfStack().AssociatedLoops = Val;
682
88.8k
    if (Val > 1)
683
2.84k
      getTopOfStack().HasMutipleLoops = true;
684
88.8k
  }
685
  /// Return collapse value for region.
686
840k
  unsigned getAssociatedLoops() const {
687
840k
    const SharingMapTy *Top = getTopOfStackOrNull();
688
840k
    return Top ? 
Top->AssociatedLoops839k
:
0210
;
689
840k
  }
690
  /// Returns true if the construct is associated with multiple loops.
691
50.2k
  bool hasMutipleLoops() const {
692
50.2k
    const SharingMapTy *Top = getTopOfStackOrNull();
693
50.2k
    return Top ? Top->HasMutipleLoops : 
false0
;
694
50.2k
  }
695
696
  /// Marks current target region as one with closely nested teams
697
  /// region.
698
22.2k
  void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
699
22.2k
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
700
22.2k
      Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
701
22.2k
  }
702
  /// Returns true, if current region has closely nested teams region.
703
27.8k
  bool hasInnerTeamsRegion() const {
704
27.8k
    return getInnerTeamsRegionLoc().isValid();
705
27.8k
  }
706
  /// Returns location of the nested teams region (if any).
707
27.8k
  SourceLocation getInnerTeamsRegionLoc() const {
708
27.8k
    const SharingMapTy *Top = getTopOfStackOrNull();
709
27.8k
    return Top ? Top->InnerTeamsRegionLoc : 
SourceLocation()0
;
710
27.8k
  }
711
712
1.28M
  Scope *getCurScope() const {
713
1.28M
    const SharingMapTy *Top = getTopOfStackOrNull();
714
1.28M
    return Top ? Top->CurScope : 
nullptr0
;
715
1.28M
  }
716
326k
  SourceLocation getConstructLoc() const {
717
326k
    const SharingMapTy *Top = getTopOfStackOrNull();
718
326k
    return Top ? Top->ConstructLoc : 
SourceLocation()0
;
719
326k
  }
720
721
  /// Do the check specified in \a Check to all component lists and return true
722
  /// if any issue is found.
723
  bool checkMappableExprComponentListsForDecl(
724
      const ValueDecl *VD, bool CurrentRegionOnly,
725
      const llvm::function_ref<
726
          bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
727
               OpenMPClauseKind)>
728
157k
          Check) const {
729
157k
    if (isStackEmpty())
730
0
      return false;
731
157k
    auto SI = begin();
732
157k
    auto SE = end();
733
157k
734
157k
    if (SI == SE)
735
0
      return false;
736
157k
737
157k
    if (CurrentRegionOnly)
738
139k
      SE = std::next(SI);
739
17.6k
    else
740
17.6k
      std::advance(SI, 1);
741
157k
742
295k
    for (; SI != SE; 
++SI138k
) {
743
142k
      auto MI = SI->MappedExprComponents.find(VD);
744
142k
      if (MI != SI->MappedExprComponents.end())
745
4.58k
        for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
746
4.58k
             MI->second.Components)
747
4.60k
          if (Check(L, MI->second.Kind))
748
3.58k
            return true;
749
142k
    }
750
157k
    
return false153k
;
751
157k
  }
752
753
  /// Do the check specified in \a Check to all component lists at a given level
754
  /// and return true if any issue is found.
755
  bool checkMappableExprComponentListsForDeclAtLevel(
756
      const ValueDecl *VD, unsigned Level,
757
      const llvm::function_ref<
758
          bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
759
               OpenMPClauseKind)>
760
691k
          Check) const {
761
691k
    if (getStackSize() <= Level)
762
0
      return false;
763
691k
764
691k
    const SharingMapTy &StackElem = getStackElemAtLevel(Level);
765
691k
    auto MI = StackElem.MappedExprComponents.find(VD);
766
691k
    if (MI != StackElem.MappedExprComponents.end())
767
15.2k
      for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
768
15.2k
           MI->second.Components)
769
15.2k
        if (Check(L, MI->second.Kind))
770
7.74k
          return true;
771
691k
    
return false683k
;
772
691k
  }
773
774
  /// Create a new mappable expression component list associated with a given
775
  /// declaration and initialize it with the provided list of components.
776
  void addMappableExpressionComponents(
777
      const ValueDecl *VD,
778
      OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
779
20.1k
      OpenMPClauseKind WhereFoundClauseKind) {
780
20.1k
    MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
781
20.1k
    // Create new entry and append the new components there.
782
20.1k
    MEC.Components.resize(MEC.Components.size() + 1);
783
20.1k
    MEC.Components.back().append(Components.begin(), Components.end());
784
20.1k
    MEC.Kind = WhereFoundClauseKind;
785
20.1k
  }
786
787
506k
  unsigned getNestingLevel() const {
788
506k
    assert(!isStackEmpty());
789
506k
    return getStackSize() - 1;
790
506k
  }
791
  void addDoacrossDependClause(OMPDependClause *C,
792
433
                               const OperatorOffsetTy &OpsOffs) {
793
433
    SharingMapTy *Parent = getSecondOnStackOrNull();
794
433
    assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
795
433
    Parent->DoacrossDepends.try_emplace(C, OpsOffs);
796
433
  }
797
  llvm::iterator_range<DoacrossDependMapTy::const_iterator>
798
688
  getDoacrossDependClauses() const {
799
688
    const SharingMapTy &StackElem = getTopOfStack();
800
688
    if (isOpenMPWorksharingDirective(StackElem.Directive)) {
801
688
      const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
802
688
      return llvm::make_range(Ref.begin(), Ref.end());
803
688
    }
804
0
    return llvm::make_range(StackElem.DoacrossDepends.end(),
805
0
                            StackElem.DoacrossDepends.end());
806
0
  }
807
808
  // Store types of classes which have been explicitly mapped
809
50
  void addMappedClassesQualTypes(QualType QT) {
810
50
    SharingMapTy &StackElem = getTopOfStack();
811
50
    StackElem.MappedClassesQualTypes.insert(QT);
812
50
  }
813
814
  // Return set of mapped classes types
815
675
  bool isClassPreviouslyMapped(QualType QT) const {
816
675
    const SharingMapTy &StackElem = getTopOfStack();
817
675
    return StackElem.MappedClassesQualTypes.count(QT) != 0;
818
675
  }
819
820
  /// Adds global declare target to the parent target region.
821
8
  void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
822
8
    assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
823
8
               E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
824
8
           "Expected declare target link global.");
825
16
    for (auto &Elem : *this) {
826
16
      if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
827
8
        Elem.DeclareTargetLinkVarDecls.push_back(E);
828
8
        return;
829
8
      }
830
16
    }
831
8
  }
832
833
  /// Returns the list of globals with declare target link if current directive
834
  /// is target.
835
49.3k
  ArrayRef<DeclRefExpr *> getLinkGlobals() const {
836
49.3k
    assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
837
49.3k
           "Expected target executable directive.");
838
49.3k
    return getTopOfStack().DeclareTargetLinkVarDecls;
839
49.3k
  }
840
};
841
842
2.94M
bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
843
2.94M
  return isOpenMPParallelDirective(DKind) || 
isOpenMPTeamsDirective(DKind)2.40M
;
844
2.94M
}
845
846
2.44M
bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
847
2.44M
  return isImplicitTaskingRegion(DKind) || 
isOpenMPTaskingDirective(DKind)1.61M
||
848
2.44M
         
DKind == OMPD_unknown1.07M
;
849
2.44M
}
850
851
} // namespace
852
853
369k
static const Expr *getExprAsWritten(const Expr *E) {
854
369k
  if (const auto *FE = dyn_cast<FullExpr>(E))
855
76
    E = FE->getSubExpr();
856
369k
857
369k
  if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
858
0
    E = MTE->GetTemporaryExpr();
859
369k
860
369k
  while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
861
12
    E = Binder->getSubExpr();
862
369k
863
369k
  if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
864
106k
    E = ICE->getSubExprAsWritten();
865
369k
  return E->IgnoreParens();
866
369k
}
867
868
151k
static Expr *getExprAsWritten(Expr *E) {
869
151k
  return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
870
151k
}
871
872
8.58M
static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
873
8.58M
  if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
874
64.8k
    if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
875
17.0k
      D = ME->getMemberDecl();
876
8.58M
  const auto *VD = dyn_cast<VarDecl>(D);
877
8.58M
  const auto *FD = dyn_cast<FieldDecl>(D);
878
8.58M
  if (VD != nullptr) {
879
8.51M
    VD = VD->getCanonicalDecl();
880
8.51M
    D = VD;
881
8.51M
  } else {
882
68.5k
    assert(FD);
883
68.5k
    FD = FD->getCanonicalDecl();
884
68.5k
    D = FD;
885
68.5k
  }
886
8.58M
  return D;
887
8.58M
}
888
889
4.80M
static ValueDecl *getCanonicalDecl(ValueDecl *D) {
890
4.80M
  return const_cast<ValueDecl *>(
891
4.80M
      getCanonicalDecl(const_cast<const ValueDecl *>(D)));
892
4.80M
}
893
894
DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
895
1.80M
                                          ValueDecl *D) const {
896
1.80M
  D = getCanonicalDecl(D);
897
1.80M
  auto *VD = dyn_cast<VarDecl>(D);
898
1.80M
  const auto *FD = dyn_cast<FieldDecl>(D);
899
1.80M
  DSAVarData DVar;
900
1.80M
  if (Iter == end()) {
901
685k
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
902
685k
    // in a region but not in construct]
903
685k
    //  File-scope or namespace-scope variables referenced in called routines
904
685k
    //  in the region are shared unless they appear in a threadprivate
905
685k
    //  directive.
906
685k
    if (VD && 
!VD->isFunctionOrMethodVarDecl()683k
&&
!isa<ParmVarDecl>(VD)581k
)
907
514k
      DVar.CKind = OMPC_shared;
908
685k
909
685k
    // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
910
685k
    // in a region but not in construct]
911
685k
    //  Variables with static storage duration that are declared in called
912
685k
    //  routines in the region are shared.
913
685k
    if (VD && 
VD->hasGlobalStorage()683k
)
914
514k
      DVar.CKind = OMPC_shared;
915
685k
916
685k
    // Non-static data members are shared by default.
917
685k
    if (FD)
918
2.01k
      DVar.CKind = OMPC_shared;
919
685k
920
685k
    return DVar;
921
685k
  }
922
1.11M
923
1.11M
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
924
1.11M
  // in a Construct, C/C++, predetermined, p.1]
925
1.11M
  // Variables with automatic storage duration that are declared in a scope
926
1.11M
  // inside the construct are private.
927
1.11M
  if (VD && 
isOpenMPLocal(VD, Iter)1.11M
&&
VD->isLocalVarDecl()738
&&
928
1.11M
      
(738
VD->getStorageClass() == SC_Auto738
||
VD->getStorageClass() == SC_None738
)) {
929
738
    DVar.CKind = OMPC_private;
930
738
    return DVar;
931
738
  }
932
1.11M
933
1.11M
  DVar.DKind = Iter->Directive;
934
1.11M
  // Explicitly specified attributes and local variables with predetermined
935
1.11M
  // attributes.
936
1.11M
  if (Iter->SharingMap.count(D)) {
937
6.67k
    const DSAInfo &Data = Iter->SharingMap.lookup(D);
938
6.67k
    DVar.RefExpr = Data.RefExpr.getPointer();
939
6.67k
    DVar.PrivateCopy = Data.PrivateCopy;
940
6.67k
    DVar.CKind = Data.Attributes;
941
6.67k
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
942
6.67k
    return DVar;
943
6.67k
  }
944
1.11M
945
1.11M
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
946
1.11M
  // in a Construct, C/C++, implicitly determined, p.1]
947
1.11M
  //  In a parallel or task construct, the data-sharing attributes of these
948
1.11M
  //  variables are determined by the default clause, if present.
949
1.11M
  switch (Iter->DefaultAttr) {
950
1.11M
  case DSA_shared:
951
203
    DVar.CKind = OMPC_shared;
952
203
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
953
203
    return DVar;
954
1.11M
  case DSA_none:
955
154
    return DVar;
956
1.11M
  case DSA_unspecified:
957
1.11M
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
958
1.11M
    // in a Construct, implicitly determined, p.2]
959
1.11M
    //  In a parallel construct, if no default clause is present, these
960
1.11M
    //  variables are shared.
961
1.11M
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
962
1.11M
    if (isOpenMPParallelDirective(DVar.DKind) ||
963
1.11M
        
isOpenMPTeamsDirective(DVar.DKind)1.01M
) {
964
215k
      DVar.CKind = OMPC_shared;
965
215k
      return DVar;
966
215k
    }
967
896k
968
896k
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
969
896k
    // in a Construct, implicitly determined, p.4]
970
896k
    //  In a task construct, if no default clause is present, a variable that in
971
896k
    //  the enclosing context is determined to be shared by all implicit tasks
972
896k
    //  bound to the current team is shared.
973
896k
    if (isOpenMPTaskingDirective(DVar.DKind)) {
974
514k
      DSAVarData DVarTemp;
975
514k
      const_iterator I = Iter, E = end();
976
1.01M
      do {
977
1.01M
        ++I;
978
1.01M
        // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
979
1.01M
        // Referenced in a Construct, implicitly determined, p.6]
980
1.01M
        //  In a task construct, if no default clause is present, a variable
981
1.01M
        //  whose data-sharing attribute is not determined by the rules above is
982
1.01M
        //  firstprivate.
983
1.01M
        DVarTemp = getDSA(I, D);
984
1.01M
        if (DVarTemp.CKind != OMPC_shared) {
985
8.41k
          DVar.RefExpr = nullptr;
986
8.41k
          DVar.CKind = OMPC_firstprivate;
987
8.41k
          return DVar;
988
8.41k
        }
989
1.00M
      } while (I != E && 
!isImplicitTaskingRegion(I->Directive)503k
);
990
514k
      DVar.CKind =
991
505k
          (DVarTemp.CKind == OMPC_unknown) ? 
OMPC_firstprivate0
: OMPC_shared;
992
505k
      return DVar;
993
382k
    }
994
382k
  }
995
382k
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
996
382k
  // in a Construct, implicitly determined, p.3]
997
382k
  //  For constructs other than task, if no default clause is present, these
998
382k
  //  variables inherit their data-sharing attributes from the enclosing
999
382k
  //  context.
1000
382k
  return getDSA(++Iter, D);
1001
382k
}
1002
1003
const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1004
1.23k
                                         const Expr *NewDE) {
1005
1.23k
  assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1006
1.23k
  D = getCanonicalDecl(D);
1007
1.23k
  SharingMapTy &StackElem = getTopOfStack();
1008
1.23k
  auto It = StackElem.AlignedMap.find(D);
1009
1.23k
  if (It == StackElem.AlignedMap.end()) {
1010
1.21k
    assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1011
1.21k
    StackElem.AlignedMap[D] = NewDE;
1012
1.21k
    return nullptr;
1013
1.21k
  }
1014
20
  assert(It->second && "Unexpected nullptr expr in the aligned map");
1015
20
  return It->second;
1016
20
}
1017
1018
84.9k
void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1019
84.9k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1020
84.9k
  D = getCanonicalDecl(D);
1021
84.9k
  SharingMapTy &StackElem = getTopOfStack();
1022
84.9k
  StackElem.LCVMap.try_emplace(
1023
84.9k
      D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1024
84.9k
}
1025
1026
const DSAStackTy::LCDeclInfo
1027
1.44M
DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1028
1.44M
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1029
1.44M
  D = getCanonicalDecl(D);
1030
1.44M
  const SharingMapTy &StackElem = getTopOfStack();
1031
1.44M
  auto It = StackElem.LCVMap.find(D);
1032
1.44M
  if (It != StackElem.LCVMap.end())
1033
1.74k
    return It->second;
1034
1.44M
  return {0, nullptr};
1035
1.44M
}
1036
1037
const DSAStackTy::LCDeclInfo
1038
254
DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1039
254
  const SharingMapTy *Parent = getSecondOnStackOrNull();
1040
254
  assert(Parent && "Data-sharing attributes stack is empty");
1041
254
  D = getCanonicalDecl(D);
1042
254
  auto It = Parent->LCVMap.find(D);
1043
254
  if (It != Parent->LCVMap.end())
1044
248
    return It->second;
1045
6
  return {0, nullptr};
1046
6
}
1047
1048
84
const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1049
84
  const SharingMapTy *Parent = getSecondOnStackOrNull();
1050
84
  assert(Parent && "Data-sharing attributes stack is empty");
1051
84
  if (Parent->LCVMap.size() < I)
1052
12
    return nullptr;
1053
72
  for (const auto &Pair : Parent->LCVMap)
1054
120
    if (Pair.second.first == I)
1055
72
      return Pair.first;
1056
72
  
return nullptr0
;
1057
72
}
1058
1059
void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1060
152k
                        DeclRefExpr *PrivateCopy) {
1061
152k
  D = getCanonicalDecl(D);
1062
152k
  if (A == OMPC_threadprivate) {
1063
1.65k
    DSAInfo &Data = Threadprivates[D];
1064
1.65k
    Data.Attributes = A;
1065
1.65k
    Data.RefExpr.setPointer(E);
1066
1.65k
    Data.PrivateCopy = nullptr;
1067
150k
  } else {
1068
150k
    DSAInfo &Data = getTopOfStack().SharingMap[D];
1069
150k
    assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1070
150k
           (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1071
150k
           (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1072
150k
           (isLoopControlVariable(D).first && A == OMPC_private));
1073
150k
    if (A == OMPC_lastprivate && 
Data.Attributes == OMPC_firstprivate5.84k
) {
1074
90
      Data.RefExpr.setInt(/*IntVal=*/true);
1075
90
      return;
1076
90
    }
1077
150k
    const bool IsLastprivate =
1078
150k
        A == OMPC_lastprivate || 
Data.Attributes == OMPC_lastprivate144k
;
1079
150k
    Data.Attributes = A;
1080
150k
    Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1081
150k
    Data.PrivateCopy = PrivateCopy;
1082
150k
    if (PrivateCopy) {
1083
1.86k
      DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1084
1.86k
      Data.Attributes = A;
1085
1.86k
      Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1086
1.86k
      Data.PrivateCopy = nullptr;
1087
1.86k
    }
1088
150k
  }
1089
152k
}
1090
1091
/// Build a variable declaration for OpenMP loop iteration variable.
1092
static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1093
                             StringRef Name, const AttrVec *Attrs = nullptr,
1094
476k
                             DeclRefExpr *OrigRef = nullptr) {
1095
476k
  DeclContext *DC = SemaRef.CurContext;
1096
476k
  IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1097
476k
  TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1098
476k
  auto *Decl =
1099
476k
      VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1100
476k
  if (Attrs) {
1101
1.18k
    for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1102
1.67k
         I != E; 
++I484
)
1103
484
      Decl->addAttr(*I);
1104
1.18k
  }
1105
476k
  Decl->setImplicit();
1106
476k
  if (OrigRef) {
1107
119k
    Decl->addAttr(
1108
119k
        OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1109
119k
  }
1110
476k
  return Decl;
1111
476k
}
1112
1113
static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1114
                                     SourceLocation Loc,
1115
940k
                                     bool RefersToCapture = false) {
1116
940k
  D->setReferenced();
1117
940k
  D->markUsed(S.Context);
1118
940k
  return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1119
940k
                             SourceLocation(), D, RefersToCapture, Loc, Ty,
1120
940k
                             VK_LValue);
1121
940k
}
1122
1123
void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1124
1.08k
                                           BinaryOperatorKind BOK) {
1125
1.08k
  D = getCanonicalDecl(D);
1126
1.08k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1127
1.08k
  assert(
1128
1.08k
      getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1129
1.08k
      "Additional reduction info may be specified only for reduction items.");
1130
1.08k
  ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1131
1.08k
  assert(ReductionData.ReductionRange.isInvalid() &&
1132
1.08k
         getTopOfStack().Directive == OMPD_taskgroup &&
1133
1.08k
         "Additional reduction info may be specified only once for reduction "
1134
1.08k
         "items.");
1135
1.08k
  ReductionData.set(BOK, SR);
1136
1.08k
  Expr *&TaskgroupReductionRef =
1137
1.08k
      getTopOfStack().TaskgroupReductionRef;
1138
1.08k
  if (!TaskgroupReductionRef) {
1139
990
    VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1140
990
                               SemaRef.Context.VoidPtrTy, ".task_red.");
1141
990
    TaskgroupReductionRef =
1142
990
        buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1143
990
  }
1144
1.08k
}
1145
1146
void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1147
18
                                           const Expr *ReductionRef) {
1148
18
  D = getCanonicalDecl(D);
1149
18
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1150
18
  assert(
1151
18
      getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1152
18
      "Additional reduction info may be specified only for reduction items.");
1153
18
  ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1154
18
  assert(ReductionData.ReductionRange.isInvalid() &&
1155
18
         getTopOfStack().Directive == OMPD_taskgroup &&
1156
18
         "Additional reduction info may be specified only once for reduction "
1157
18
         "items.");
1158
18
  ReductionData.set(ReductionRef, SR);
1159
18
  Expr *&TaskgroupReductionRef =
1160
18
      getTopOfStack().TaskgroupReductionRef;
1161
18
  if (!TaskgroupReductionRef) {
1162
18
    VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1163
18
                               SemaRef.Context.VoidPtrTy, ".task_red.");
1164
18
    TaskgroupReductionRef =
1165
18
        buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1166
18
  }
1167
18
}
1168
1169
const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1170
    const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1171
662
    Expr *&TaskgroupDescriptor) const {
1172
662
  D = getCanonicalDecl(D);
1173
662
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1174
756
  for (const_iterator I = begin() + 1, E = end(); I != E; 
++I94
) {
1175
666
    const DSAInfo &Data = I->SharingMap.lookup(D);
1176
666
    if (Data.Attributes != OMPC_reduction || 
I->Directive != OMPD_taskgroup576
)
1177
94
      continue;
1178
572
    const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1179
572
    if (!ReductionData.ReductionOp ||
1180
572
        ReductionData.ReductionOp.is<const Expr *>())
1181
18
      return DSAVarData();
1182
554
    SR = ReductionData.ReductionRange;
1183
554
    BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1184
554
    assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1185
554
                                       "expression for the descriptor is not "
1186
554
                                       "set.");
1187
554
    TaskgroupDescriptor = I->TaskgroupReductionRef;
1188
554
    return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1189
554
                      Data.PrivateCopy, I->DefaultAttrLoc);
1190
554
  }
1191
662
  
return DSAVarData()90
;
1192
662
}
1193
1194
const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1195
    const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1196
662
    Expr *&TaskgroupDescriptor) const {
1197
662
  D = getCanonicalDecl(D);
1198
662
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1199
756
  for (const_iterator I = begin() + 1, E = end(); I != E; 
++I94
) {
1200
666
    const DSAInfo &Data = I->SharingMap.lookup(D);
1201
666
    if (Data.Attributes != OMPC_reduction || 
I->Directive != OMPD_taskgroup576
)
1202
94
      continue;
1203
572
    const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1204
572
    if (!ReductionData.ReductionOp ||
1205
572
        !ReductionData.ReductionOp.is<const Expr *>())
1206
554
      return DSAVarData();
1207
18
    SR = ReductionData.ReductionRange;
1208
18
    ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1209
18
    assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1210
18
                                       "expression for the descriptor is not "
1211
18
                                       "set.");
1212
18
    TaskgroupDescriptor = I->TaskgroupReductionRef;
1213
18
    return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1214
18
                      Data.PrivateCopy, I->DefaultAttrLoc);
1215
18
  }
1216
662
  
return DSAVarData()90
;
1217
662
}
1218
1219
1.11M
bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1220
1.11M
  D = D->getCanonicalDecl();
1221
1.91M
  for (const_iterator E = end(); I != E; 
++I798k
) {
1222
1.69M
    if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1223
1.69M
        
isOpenMPTargetExecutionDirective(I->Directive)900k
) {
1224
899k
      Scope *TopScope = I->CurScope ? 
I->CurScope->getParent()799k
:
nullptr99.8k
;
1225
899k
      Scope *CurScope = getCurScope();
1226
7.76M
      while (CurScope && 
CurScope != TopScope7.66M
&&
!CurScope->isDeclScope(D)6.86M
)
1227
6.86M
        CurScope = CurScope->getParent();
1228
899k
      return CurScope != TopScope;
1229
899k
    }
1230
1.69M
  }
1231
1.11M
  
return false217k
;
1232
1.11M
}
1233
1234
static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1235
                                  bool AcceptIfMutable = true,
1236
544k
                                  bool *IsClassType = nullptr) {
1237
544k
  ASTContext &Context = SemaRef.getASTContext();
1238
544k
  Type = Type.getNonReferenceType().getCanonicalType();
1239
544k
  bool IsConstant = Type.isConstant(Context);
1240
544k
  Type = Context.getBaseElementType(Type);
1241
544k
  const CXXRecordDecl *RD = AcceptIfMutable && 
SemaRef.getLangOpts().CPlusPlus521k
1242
544k
                                ? 
Type->getAsCXXRecordDecl()490k
1243
544k
                                : 
nullptr54.2k
;
1244
544k
  if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1245
5.73k
    if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1246
5.73k
      RD = CTD->getTemplatedDecl();
1247
544k
  if (IsClassType)
1248
38.8k
    *IsClassType = RD;
1249
544k
  return IsConstant && 
!(17.8k
SemaRef.getLangOpts().CPlusPlus17.8k
&&
RD17.8k
&&
1250
17.8k
                         
RD->hasDefinition()4.47k
&&
RD->hasMutableFields()4.47k
);
1251
544k
}
1252
1253
static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1254
                                      QualType Type, OpenMPClauseKind CKind,
1255
                                      SourceLocation ELoc,
1256
                                      bool AcceptIfMutable = true,
1257
38.8k
                                      bool ListItemNotVar = false) {
1258
38.8k
  ASTContext &Context = SemaRef.getASTContext();
1259
38.8k
  bool IsClassType;
1260
38.8k
  if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1261
4.65k
    unsigned Diag = ListItemNotVar
1262
4.65k
                        ? 
diag::err_omp_const_list_item0
1263
4.65k
                        : IsClassType ? 
diag::err_omp_const_not_mutable_variable152
1264
4.65k
                                      : 
diag::err_omp_const_variable4.49k
;
1265
4.65k
    SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1266
4.65k
    if (!ListItemNotVar && D) {
1267
4.65k
      const VarDecl *VD = dyn_cast<VarDecl>(D);
1268
4.65k
      bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1269
4.65k
                               VarDecl::DeclarationOnly;
1270
4.65k
      SemaRef.Diag(D->getLocation(),
1271
4.65k
                   IsDecl ? 
diag::note_previous_decl1.27k
:
diag::note_defined_here3.37k
)
1272
4.65k
          << D;
1273
4.65k
    }
1274
4.65k
    return true;
1275
4.65k
  }
1276
34.2k
  return false;
1277
34.2k
}
1278
1279
const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1280
1.41M
                                                   bool FromParent) {
1281
1.41M
  D = getCanonicalDecl(D);
1282
1.41M
  DSAVarData DVar;
1283
1.41M
1284
1.41M
  auto *VD = dyn_cast<VarDecl>(D);
1285
1.41M
  auto TI = Threadprivates.find(D);
1286
1.41M
  if (TI != Threadprivates.end()) {
1287
7.10k
    DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1288
7.10k
    DVar.CKind = OMPC_threadprivate;
1289
7.10k
    return DVar;
1290
7.10k
  }
1291
1.41M
  if (VD && 
VD->hasAttr<OMPThreadPrivateDeclAttr>()1.39M
) {
1292
6
    DVar.RefExpr = buildDeclRefExpr(
1293
6
        SemaRef, VD, D->getType().getNonReferenceType(),
1294
6
        VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1295
6
    DVar.CKind = OMPC_threadprivate;
1296
6
    addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1297
6
    return DVar;
1298
6
  }
1299
1.41M
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1300
1.41M
  // in a Construct, C/C++, predetermined, p.1]
1301
1.41M
  //  Variables appearing in threadprivate directives are threadprivate.
1302
1.41M
  if ((VD && 
VD->getTLSKind() != VarDecl::TLS_None1.39M
&&
1303
1.41M
       
!(7
VD->hasAttr<OMPThreadPrivateDeclAttr>()7
&&
1304
7
         
SemaRef.getLangOpts().OpenMPUseTLS0
&&
1305
7
         
SemaRef.getASTContext().getTargetInfo().isTLSSupported()0
)) ||
1306
1.41M
      
(1.41M
VD1.41M
&&
VD->getStorageClass() == SC_Register1.39M
&&
1307
1.41M
       
VD->hasAttr<AsmLabelAttr>()44
&&
!VD->isLocalVarDecl()16
)) {
1308
23
    DVar.RefExpr = buildDeclRefExpr(
1309
23
        SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1310
23
    DVar.CKind = OMPC_threadprivate;
1311
23
    addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1312
23
    return DVar;
1313
23
  }
1314
1.41M
  if (SemaRef.getLangOpts().OpenMPCUDAMode && 
VD4.43k
&&
1315
1.41M
      
VD->isLocalVarDeclOrParm()4.43k
&&
!isStackEmpty()4.42k
&&
1316
1.41M
      
!isLoopControlVariable(D).first4.42k
) {
1317
4.27k
    const_iterator IterTarget =
1318
4.39k
        std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1319
4.39k
          return isOpenMPTargetExecutionDirective(Data.Directive);
1320
4.39k
        });
1321
4.27k
    if (IterTarget != end()) {
1322
4.27k
      const_iterator ParentIterTarget = IterTarget + 1;
1323
4.27k
      for (const_iterator Iter = begin();
1324
8.65k
           Iter != ParentIterTarget; 
++Iter4.37k
) {
1325
4.38k
        if (isOpenMPLocal(VD, Iter)) {
1326
10
          DVar.RefExpr =
1327
10
              buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1328
10
                               D->getLocation());
1329
10
          DVar.CKind = OMPC_threadprivate;
1330
10
          return DVar;
1331
10
        }
1332
4.38k
      }
1333
4.27k
      
if (4.26k
!isClauseParsingMode()4.26k
||
IterTarget != begin()384
) {
1334
3.92k
        auto DSAIter = IterTarget->SharingMap.find(D);
1335
3.92k
        if (DSAIter != IterTarget->SharingMap.end() &&
1336
3.92k
            
isOpenMPPrivate(DSAIter->getSecond().Attributes)759
) {
1337
759
          DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1338
759
          DVar.CKind = OMPC_threadprivate;
1339
759
          return DVar;
1340
759
        }
1341
3.16k
        const_iterator End = end();
1342
3.16k
        if (!SemaRef.isOpenMPCapturedByRef(
1343
3.16k
                D, std::distance(ParentIterTarget, End))) {
1344
912
          DVar.RefExpr =
1345
912
              buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1346
912
                               IterTarget->ConstructLoc);
1347
912
          DVar.CKind = OMPC_threadprivate;
1348
912
          return DVar;
1349
912
        }
1350
1.41M
      }
1351
4.26k
    }
1352
4.27k
  }
1353
1.41M
1354
1.41M
  if (isStackEmpty())
1355
461
    // Not in OpenMP execution region and top scope was already checked.
1356
461
    return DVar;
1357
1.41M
1358
1.41M
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1359
1.41M
  // in a Construct, C/C++, predetermined, p.4]
1360
1.41M
  //  Static data members are shared.
1361
1.41M
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1362
1.41M
  // in a Construct, C/C++, predetermined, p.7]
1363
1.41M
  //  Variables with static storage duration that are declared in a scope
1364
1.41M
  //  inside the construct are shared.
1365
1.41M
  if (VD && 
VD->isStaticDataMember()1.39M
) {
1366
4.34k
    // Check for explicitly specified attributes.
1367
4.34k
    const_iterator I = begin();
1368
4.34k
    const_iterator EndI = end();
1369
4.34k
    if (FromParent && 
I != EndI410
)
1370
410
      ++I;
1371
4.34k
    auto It = I->SharingMap.find(D);
1372
4.34k
    if (It != I->SharingMap.end()) {
1373
652
      const DSAInfo &Data = It->getSecond();
1374
652
      DVar.RefExpr = Data.RefExpr.getPointer();
1375
652
      DVar.PrivateCopy = Data.PrivateCopy;
1376
652
      DVar.CKind = Data.Attributes;
1377
652
      DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1378
652
      DVar.DKind = I->Directive;
1379
652
      return DVar;
1380
652
    }
1381
3.69k
1382
3.69k
    DVar.CKind = OMPC_shared;
1383
3.69k
    return DVar;
1384
3.69k
  }
1385
1.40M
1386
1.40M
  auto &&MatchesAlways = [](OpenMPDirectiveKind) 
{ return true; }10.2k
;
1387
1.40M
  // The predetermined shared attribute for const-qualified types having no
1388
1.40M
  // mutable members was removed after OpenMP 3.1.
1389
1.40M
  if (SemaRef.LangOpts.OpenMP <= 31) {
1390
505k
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1391
505k
    // in a Construct, C/C++, predetermined, p.6]
1392
505k
    //  Variables with const qualified type having no mutable member are
1393
505k
    //  shared.
1394
505k
    if (isConstNotMutableType(SemaRef, D->getType())) {
1395
10.2k
      // Variables with const-qualified type having no mutable member may be
1396
10.2k
      // listed in a firstprivate clause, even if they are static data members.
1397
10.2k
      DSAVarData DVarTemp = hasInnermostDSA(
1398
10.2k
          D,
1399
10.2k
          [](OpenMPClauseKind C) {
1400
6.88k
            return C == OMPC_firstprivate || 
C == OMPC_shared6.36k
;
1401
6.88k
          },
1402
10.2k
          MatchesAlways, FromParent);
1403
10.2k
      if (DVarTemp.CKind != OMPC_unknown && 
DVarTemp.RefExpr6.86k
)
1404
356
        return DVarTemp;
1405
9.90k
1406
9.90k
      DVar.CKind = OMPC_shared;
1407
9.90k
      return DVar;
1408
9.90k
    }
1409
505k
  }
1410
1.39M
1411
1.39M
  // Explicitly specified attributes and local variables with predetermined
1412
1.39M
  // attributes.
1413
1.39M
  const_iterator I = begin();
1414
1.39M
  const_iterator EndI = end();
1415
1.39M
  if (FromParent && 
I != EndI42.9k
)
1416
42.9k
    ++I;
1417
1.39M
  auto It = I->SharingMap.find(D);
1418
1.39M
  if (It != I->SharingMap.end()) {
1419
163k
    const DSAInfo &Data = It->getSecond();
1420
163k
    DVar.RefExpr = Data.RefExpr.getPointer();
1421
163k
    DVar.PrivateCopy = Data.PrivateCopy;
1422
163k
    DVar.CKind = Data.Attributes;
1423
163k
    DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1424
163k
    DVar.DKind = I->Directive;
1425
163k
  }
1426
1.39M
1427
1.39M
  return DVar;
1428
1.39M
}
1429
1430
const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1431
48.2k
                                                        bool FromParent) const {
1432
48.2k
  if (isStackEmpty()) {
1433
0
    const_iterator I;
1434
0
    return getDSA(I, D);
1435
0
  }
1436
48.2k
  D = getCanonicalDecl(D);
1437
48.2k
  const_iterator StartI = begin();
1438
48.2k
  const_iterator EndI = end();
1439
48.2k
  if (FromParent && 
StartI != EndI3.05k
)
1440
3.05k
    ++StartI;
1441
48.2k
  return getDSA(StartI, D);
1442
48.2k
}
1443
1444
const DSAStackTy::DSAVarData
1445
DSAStackTy::hasDSA(ValueDecl *D,
1446
                   const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1447
                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1448
179k
                   bool FromParent) const {
1449
179k
  if (isStackEmpty())
1450
0
    return {};
1451
179k
  D = getCanonicalDecl(D);
1452
179k
  const_iterator I = begin();
1453
179k
  const_iterator EndI = end();
1454
179k
  if (FromParent && 
I != EndI47.2k
)
1455
47.2k
    ++I;
1456
513k
  for (; I != EndI; 
++I333k
) {
1457
337k
    if (!DPred(I->Directive) &&
1458
337k
        
!isImplicitOrExplicitTaskingRegion(I->Directive)0
)
1459
0
      continue;
1460
337k
    const_iterator NewI = I;
1461
337k
    DSAVarData DVar = getDSA(NewI, D);
1462
337k
    if (I == NewI && 
CPred(DVar.CKind)108k
)
1463
4.04k
      return DVar;
1464
337k
  }
1465
179k
  
return {}175k
;
1466
179k
}
1467
1468
const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1469
    ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1470
    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1471
55.8k
    bool FromParent) const {
1472
55.8k
  if (isStackEmpty())
1473
0
    return {};
1474
55.8k
  D = getCanonicalDecl(D);
1475
55.8k
  const_iterator StartI = begin();
1476
55.8k
  const_iterator EndI = end();
1477
55.8k
  if (FromParent && 
StartI != EndI47.6k
)
1478
47.6k
    ++StartI;
1479
55.8k
  if (StartI == EndI || 
!DPred(StartI->Directive)37.7k
)
1480
35.5k
    return {};
1481
20.3k
  const_iterator NewI = StartI;
1482
20.3k
  DSAVarData DVar = getDSA(NewI, D);
1483
20.3k
  return (NewI == StartI && 
CPred(DVar.CKind)16.7k
) ?
DVar6.92k
:
DSAVarData()13.4k
;
1484
20.3k
}
1485
1486
bool DSAStackTy::hasExplicitDSA(
1487
    const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1488
1.68M
    unsigned Level, bool NotLastprivate) const {
1489
1.68M
  if (getStackSize() <= Level)
1490
0
    return false;
1491
1.68M
  D = getCanonicalDecl(D);
1492
1.68M
  const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1493
1.68M
  auto I = StackElem.SharingMap.find(D);
1494
1.68M
  if (I != StackElem.SharingMap.end() &&
1495
1.68M
      
I->getSecond().RefExpr.getPointer()189k
&&
1496
1.68M
      
CPred(I->getSecond().Attributes)189k
&&
1497
1.68M
      
(82.7k
!NotLastprivate82.7k
||
!I->getSecond().RefExpr.getInt()5.39k
))
1498
82.6k
    return true;
1499
1.60M
  // Check predetermined rules for the loop control variables.
1500
1.60M
  auto LI = StackElem.LCVMap.find(D);
1501
1.60M
  if (LI != StackElem.LCVMap.end())
1502
10.5k
    return CPred(OMPC_private);
1503
1.59M
  return false;
1504
1.59M
}
1505
1506
bool DSAStackTy::hasExplicitDirective(
1507
    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1508
1.73M
    unsigned Level) const {
1509
1.73M
  if (getStackSize() <= Level)
1510
0
    return false;
1511
1.73M
  const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1512
1.73M
  return DPred(StackElem.Directive);
1513
1.73M
}
1514
1515
bool DSAStackTy::hasDirective(
1516
    const llvm::function_ref<bool(OpenMPDirectiveKind,
1517
                                  const DeclarationNameInfo &, SourceLocation)>
1518
        DPred,
1519
87.9k
    bool FromParent) const {
1520
87.9k
  // We look only in the enclosing region.
1521
87.9k
  size_t Skip = FromParent ? 
20
: 1;
1522
87.9k
  for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1523
130k
       I != E; 
++I42.8k
) {
1524
96.9k
    if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1525
54.0k
      return true;
1526
96.9k
  }
1527
87.9k
  
return false33.8k
;
1528
87.9k
}
1529
1530
41.5k
void Sema::InitDataSharingAttributesStack() {
1531
41.5k
  VarDataSharingAttributesStack = new DSAStackTy(*this);
1532
41.5k
}
1533
1534
22.1M
#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1535
1536
51.3k
void Sema::pushOpenMPFunctionRegion() {
1537
51.3k
  DSAStack->pushFunction();
1538
51.3k
}
1539
1540
347k
void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1541
347k
  DSAStack->popFunction(OldFSI);
1542
347k
}
1543
1544
4.51k
static bool isOpenMPDeviceDelayedContext(Sema &S) {
1545
4.51k
  assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1546
4.51k
         "Expected OpenMP device compilation.");
1547
4.51k
  return !S.isInOpenMPTargetExecutionDirective() &&
1548
4.51k
         
!S.isInOpenMPDeclareTargetContext()4.42k
;
1549
4.51k
}
1550
1551
/// Do we know that we will eventually codegen the given function?
1552
3.90k
static bool isKnownEmitted(Sema &S, FunctionDecl *FD) {
1553
3.90k
  assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1554
3.90k
         "Expected OpenMP device compilation.");
1555
3.90k
  // Templates are emitted when they're instantiated.
1556
3.90k
  if (FD->isDependentContext())
1557
1
    return false;
1558
3.90k
1559
3.90k
  if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1560
3.90k
          FD->getCanonicalDecl()))
1561
18
    return true;
1562
3.89k
1563
3.89k
  // Otherwise, the function is known-emitted if it's in our set of
1564
3.89k
  // known-emitted functions.
1565
3.89k
  return S.DeviceKnownEmittedFns.count(FD) > 0;
1566
3.89k
}
1567
1568
Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1569
80
                                                     unsigned DiagID) {
1570
80
  assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1571
80
         "Expected OpenMP device compilation.");
1572
80
  return DeviceDiagBuilder((isOpenMPDeviceDelayedContext(*this) &&
1573
80
                            
!isKnownEmitted(*this, getCurFunctionDecl())43
)
1574
80
                               ? 
DeviceDiagBuilder::K_Deferred40
1575
80
                               : 
DeviceDiagBuilder::K_Immediate40
,
1576
80
                           Loc, DiagID, getCurFunctionDecl(), *this);
1577
80
}
1578
1579
4.43k
void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) {
1580
4.43k
  assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1581
4.43k
         "Expected OpenMP device compilation.");
1582
4.43k
  assert(Callee && "Callee may not be null.");
1583
4.43k
  FunctionDecl *Caller = getCurFunctionDecl();
1584
4.43k
1585
4.43k
  // If the caller is known-emitted, mark the callee as known-emitted.
1586
4.43k
  // Otherwise, mark the call in our call graph so we can traverse it later.
1587
4.43k
  if (!isOpenMPDeviceDelayedContext(*this) ||
1588
4.43k
      
(4.32k
Caller4.32k
&&
isKnownEmitted(*this, Caller)3.71k
))
1589
114
    markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted);
1590
4.32k
  else if (Caller)
1591
3.70k
    DeviceCallGraph[Caller].insert({Callee, Loc});
1592
4.43k
}
1593
1594
56.3k
void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1595
56.3k
  assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1596
56.3k
         "OpenMP device compilation mode is expected.");
1597
56.3k
  QualType Ty = E->getType();
1598
56.3k
  if ((Ty->isFloat16Type() && 
!Context.getTargetInfo().hasFloat16Type()0
) ||
1599
56.3k
      ((Ty->isFloat128Type() ||
1600
56.3k
        
(56.3k
Ty->isRealFloatingType()56.3k
&&
Context.getTypeSize(Ty) == 128715
)) &&
1601
56.3k
       
!Context.getTargetInfo().hasFloat128Type()4
) ||
1602
56.3k
      
(56.3k
Ty->isIntegerType()56.3k
&&
Context.getTypeSize(Ty) == 12855.6k
&&
1603
56.3k
       
!Context.getTargetInfo().hasInt128Type()4
))
1604
4
    targetDiag(E->getExprLoc(), diag::err_omp_unsupported_type)
1605
4
        << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty
1606
4
        << Context.getTargetInfo().getTriple().str() << E->getSourceRange();
1607
56.3k
}
1608
1609
781k
bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
1610
781k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1611
781k
1612
781k
  ASTContext &Ctx = getASTContext();
1613
781k
  bool IsByRef = true;
1614
781k
1615
781k
  // Find the directive that is associated with the provided scope.
1616
781k
  D = cast<ValueDecl>(D->getCanonicalDecl());
1617
781k
  QualType Ty = D->getType();
1618
781k
1619
781k
  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1620
525k
    // This table summarizes how a given variable should be passed to the device
1621
525k
    // given its type and the clauses where it appears. This table is based on
1622
525k
    // the description in OpenMP 4.5 [2.10.4, target Construct] and
1623
525k
    // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1624
525k
    //
1625
525k
    // =========================================================================
1626
525k
    // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
1627
525k
    // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
1628
525k
    // =========================================================================
1629
525k
    // | scl  |               |     |       |       -       |          | bycopy|
1630
525k
    // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
1631
525k
    // | scl  |               |  x  |   -   |       -       |     -    | null  |
1632
525k
    // | scl  |       x       |     |       |       -       |          | byref |
1633
525k
    // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
1634
525k
    // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
1635
525k
    // | scl  |               |  -  |   -   |       -       |     x    | byref |
1636
525k
    // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
1637
525k
    //
1638
525k
    // | agg  |      n.a.     |     |       |       -       |          | byref |
1639
525k
    // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
1640
525k
    // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1641
525k
    // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1642
525k
    // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
1643
525k
    //
1644
525k
    // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
1645
525k
    // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
1646
525k
    // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1647
525k
    // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1648
525k
    // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
1649
525k
    // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
1650
525k
    // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
1651
525k
    // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
1652
525k
    // =========================================================================
1653
525k
    // Legend:
1654
525k
    //  scl - scalar
1655
525k
    //  ptr - pointer
1656
525k
    //  agg - aggregate
1657
525k
    //  x - applies
1658
525k
    //  - - invalid in this combination
1659
525k
    //  [] - mapped with an array section
1660
525k
    //  byref - should be mapped by reference
1661
525k
    //  byval - should be mapped by value
1662
525k
    //  null - initialize a local variable to null on the device
1663
525k
    //
1664
525k
    // Observations:
1665
525k
    //  - All scalar declarations that show up in a map clause have to be passed
1666
525k
    //    by reference, because they may have been mapped in the enclosing data
1667
525k
    //    environment.
1668
525k
    //  - If the scalar value does not fit the size of uintptr, it has to be
1669
525k
    //    passed by reference, regardless the result in the table above.
1670
525k
    //  - For pointers mapped by value that have either an implicit map or an
1671
525k
    //    array section, the runtime library may pass the NULL value to the
1672
525k
    //    device instead of the value passed to it by the compiler.
1673
525k
1674
525k
    if (Ty->isReferenceType())
1675
19.2k
      Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1676
525k
1677
525k
    // Locate map clauses and see if the variable being captured is referred to
1678
525k
    // in any of those clauses. Here we only care about variables, not fields,
1679
525k
    // because fields are part of aggregates.
1680
525k
    bool IsVariableUsedInMapClause = false;
1681
525k
    bool IsVariableAssociatedWithSection = false;
1682
525k
1683
525k
    DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1684
525k
        D, Level,
1685
525k
        [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1686
525k
            OMPClauseMappableExprCommon::MappableExprComponentListRef
1687
525k
                MapExprComponents,
1688
525k
            OpenMPClauseKind WhereFoundClauseKind) {
1689
11.4k
          // Only the map clause information influences how a variable is
1690
11.4k
          // captured. E.g. is_device_ptr does not require changing the default
1691
11.4k
          // behavior.
1692
11.4k
          if (WhereFoundClauseKind != OMPC_map)
1693
972
            return false;
1694
10.4k
1695
10.4k
          auto EI = MapExprComponents.rbegin();
1696
10.4k
          auto EE = MapExprComponents.rend();
1697
10.4k
1698
10.4k
          assert(EI != EE && "Invalid map expression!");
1699
10.4k
1700
10.4k
          if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1701
10.4k
            IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1702
10.4k
1703
10.4k
          ++EI;
1704
10.4k
          if (EI == EE)
1705
6.53k
            return false;
1706
3.94k
1707
3.94k
          if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1708
3.94k
              
isa<OMPArraySectionExpr>(EI->getAssociatedExpression())3.22k
||
1709
3.94k
              
isa<MemberExpr>(EI->getAssociatedExpression())840
) {
1710
3.94k
            IsVariableAssociatedWithSection = true;
1711
3.94k
            // There is nothing more we need to know about this variable.
1712
3.94k
            return true;
1713
3.94k
          }
1714
0
1715
0
          // Keep looking for more map info.
1716
0
          return false;
1717
0
        });
1718
525k
1719
525k
    if (IsVariableUsedInMapClause) {
1720
10.4k
      // If variable is identified in a map clause it is always captured by
1721
10.4k
      // reference except if it is a pointer that is dereferenced somehow.
1722
10.4k
      IsByRef = !(Ty->isPointerType() && 
IsVariableAssociatedWithSection1.96k
);
1723
515k
    } else {
1724
515k
      // By default, all the data that has a scalar type is mapped by copy
1725
515k
      // (except for reduction variables).
1726
515k
      IsByRef =
1727
515k
          (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1728
515k
           
!Ty->isAnyPointerType()92
) ||
1729
515k
          
!Ty->isScalarType()515k
||
1730
515k
          
DSAStack363k
->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar363k
||
1731
515k
          
DSAStack363k
->hasExplicitDSA(
1732
363k
              D, [](OpenMPClauseKind K) 
{ return K == OMPC_reduction; }22.7k
, Level);
1733
515k
    }
1734
525k
  }
1735
781k
1736
781k
  if (IsByRef && 
Ty.getNonReferenceType()->isScalarType()422k
) {
1737
190k
    IsByRef =
1738
190k
        !DSAStack->hasExplicitDSA(
1739
190k
            D,
1740
190k
            [](OpenMPClauseKind K) -> bool 
{ return K == OMPC_firstprivate; }27.4k
,
1741
190k
            Level, /*NotLastprivate=*/true) &&
1742
190k
        // If the variable is artificial and must be captured by value - try to
1743
190k
        // capture by value.
1744
190k
        
!(184k
isa<OMPCapturedExprDecl>(D)184k
&&
!D->hasAttr<OMPCaptureNoInitAttr>()1.90k
&&
1745
184k
          
!cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()1.80k
);
1746
190k
  }
1747
781k
1748
781k
  // When passing data by copy, we need to make sure it fits the uintptr size
1749
781k
  // and alignment, because the runtime library only deals with uintptr types.
1750
781k
  // If it does not fit the uintptr size, we need to pass the data by reference
1751
781k
  // instead.
1752
781k
  if (!IsByRef &&
1753
781k
      
(365k
Ctx.getTypeSizeInChars(Ty) >
1754
365k
           Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1755
365k
       
Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())364k
)) {
1756
1.75k
    IsByRef = true;
1757
1.75k
  }
1758
781k
1759
781k
  return IsByRef;
1760
781k
}
1761
1762
295k
unsigned Sema::getOpenMPNestingLevel() const {
1763
295k
  assert(getLangOpts().OpenMP);
1764
295k
  return DSAStack->getNestingLevel();
1765
295k
}
1766
1767
141k
bool Sema::isInOpenMPTargetExecutionDirective() const {
1768
141k
  return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1769
141k
          
!56.8k
DSAStack56.8k
->isClauseParsingMode()) ||
1770
141k
         
DSAStack85.1k
->hasDirective(
1771
85.1k
             [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1772
92.2k
                SourceLocation) -> bool {
1773
92.2k
               return isOpenMPTargetExecutionDirective(K);
1774
92.2k
             },
1775
85.1k
             false);
1776
141k
}
1777
1778
VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
1779
983k
                                    unsigned StopAt) {
1780
983k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1781
983k
  D = getCanonicalDecl(D);
1782
983k
1783
983k
  // If we want to determine whether the variable should be captured from the
1784
983k
  // perspective of the current capturing scope, and we've already left all the
1785
983k
  // capturing scopes of the top directive on the stack, check from the
1786
983k
  // perspective of its parent directive (if any) instead.
1787
983k
  DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
1788
983k
      *DSAStack, CheckScopeInfo && 
DSAStack164k
->isBodyComplete()164k
);
1789
983k
1790
983k
  // If we are attempting to capture a global variable in a directive with
1791
983k
  // 'target' we return true so that this global is also mapped to the device.
1792
983k
  //
1793
983k
  auto *VD = dyn_cast<VarDecl>(D);
1794
983k
  if (VD && 
!VD->hasLocalStorage()968k
&&
1795
983k
      
(214k
getCurCapturedRegion()214k
||
getCurBlock()82.3k
||
getCurLambda()81.6k
)) {
1796
136k
    if (isInOpenMPDeclareTargetContext()) {
1797
30
      // Try to mark variable as declare target if it is used in capturing
1798
30
      // regions.
1799
30
      if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1800
10
        checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
1801
30
      return nullptr;
1802
136k
    } else if (isInOpenMPTargetExecutionDirective()) {
1803
109k
      // If the declaration is enclosed in a 'declare target' directive,
1804
109k
      // then it should not be captured.
1805
109k
      //
1806
109k
      if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
1807
138
        return nullptr;
1808
109k
      return VD;
1809
109k
    }
1810
136k
  }
1811
873k
1812
873k
  if (CheckScopeInfo) {
1813
101k
    bool OpenMPFound = false;
1814
104k
    for (unsigned I = StopAt + 1; I > 0; 
--I2.70k
) {
1815
103k
      FunctionScopeInfo *FSI = FunctionScopes[I - 1];
1816
103k
      if(!isa<CapturingScopeInfo>(FSI))
1817
79.0k
        return nullptr;
1818
24.7k
      if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
1819
22.0k
        if (RSI->CapRegionKind == CR_OpenMP) {
1820
22.0k
          OpenMPFound = true;
1821
22.0k
          break;
1822
22.0k
        }
1823
24.7k
    }
1824
101k
    
if (22.3k
!OpenMPFound22.3k
)
1825
350
      return nullptr;
1826
794k
  }
1827
794k
1828
794k
  if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1829
794k
      
(785k
!785k
DSAStack785k
->isClauseParsingMode() ||
1830
785k
       
DSAStack150k
->getParentDirective() != OMPD_unknown150k
)) {
1831
776k
    auto &&Info = DSAStack->isLoopControlVariable(D);
1832
776k
    if (Info.first ||
1833
776k
        
(775k
VD775k
&&
VD->hasLocalStorage()767k
&&
1834
775k
         
isImplicitOrExplicitTaskingRegion(743k
DSAStack743k
->getCurrentDirective())) ||
1835
776k
        
(202k
VD202k
&&
DSAStack194k
->isForceVarCapturing()194k
))
1836
574k
      return VD ? 
VD573k
:
Info.second412
;
1837
201k
    DSAStackTy::DSAVarData DVarPrivate =
1838
201k
        DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1839
201k
    if (DVarPrivate.CKind != OMPC_unknown && 
isOpenMPPrivate(DVarPrivate.CKind)28.6k
)
1840
24.0k
      return VD ? 
VD20.3k
:
cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl())3.74k
;
1841
177k
    // Threadprivate variables must not be captured.
1842
177k
    if (isOpenMPThreadPrivate(DVarPrivate.CKind))
1843
1.42k
      return nullptr;
1844
176k
    // The variable is not private or it is the variable in the directive with
1845
176k
    // default(none) clause and not used in any clause.
1846
176k
    DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1847
334k
                                   [](OpenMPDirectiveKind) { return true; },
1848
176k
                                   DSAStack->isClauseParsingMode());
1849
176k
    if (DVarPrivate.CKind != OMPC_unknown ||
1850
176k
        
(173k
VD173k
&&
DSAStack170k
->getDefaultDSA() == DSA_none170k
))
1851
2.84k
      return VD ? 
VD1.92k
:
cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl())912
;
1852
191k
  }
1853
191k
  return nullptr;
1854
191k
}
1855
1856
void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1857
15.9k
                                        unsigned Level) const {
1858
15.9k
  SmallVector<OpenMPDirectiveKind, 4> Regions;
1859
15.9k
  getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1860
15.9k
  FunctionScopesIndex -= Regions.size();
1861
15.9k
}
1862
1863
87.1k
void Sema::startOpenMPLoop() {
1864
87.1k
  assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1865
87.1k
  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1866
87.1k
    
DSAStack86.4k
->loopInit()86.4k
;
1867
87.1k
}
1868
1869
864k
bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
1870
864k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1871
864k
  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1872
749k
    if (DSAStack->getAssociatedLoops() > 0 &&
1873
749k
        
!338k
DSAStack338k
->isLoopStarted()) {
1874
22.5k
      DSAStack->resetPossibleLoopCounter(D);
1875
22.5k
      DSAStack->loopStart();
1876
22.5k
      return true;
1877
22.5k
    }
1878
726k
    if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1879
726k
         
DSAStack597k
->isLoopControlVariable(D).first597k
) &&
1880
726k
        
!129k
DSAStack129k
->hasExplicitDSA(
1881
129k
            D, [](OpenMPClauseKind K) 
{ return K != OMPC_private; }24.8k
, Level) &&
1882
726k
        
!isOpenMPSimdDirective(126k
DSAStack126k
->getCurrentDirective()))
1883
49.1k
      return true;
1884
792k
  }
1885
792k
  if (const auto *VD = dyn_cast<VarDecl>(D)) {
1886
792k
    if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
1887
792k
        
DSAStack3.89k
->isForceVarCapturing()3.89k
&&
1888
792k
        
!127
DSAStack127
->hasExplicitDSA(
1889
127
            D, [](OpenMPClauseKind K) 
{ return K == OMPC_copyin; }121
, Level))
1890
6
      return true;
1891
792k
  }
1892
792k
  return DSAStack->hasExplicitDSA(
1893
792k
             D, [](OpenMPClauseKind K) 
{ return K == OMPC_private; }78.1k
, Level) ||
1894
792k
         
(770k
DSAStack770k
->isClauseParsingMode()770k
&&
1895
770k
          
DSAStack153k
->getClauseParsingMode() == OMPC_private153k
) ||
1896
792k
         // Consider taskgroup reduction descriptor variable a private to avoid
1897
792k
         // possible capture in the region.
1898
792k
         
(759k
DSAStack759k
->hasExplicitDirective(
1899
759k
              [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1900
759k
              Level) &&
1901
759k
          
DSAStack5.38k
->isTaskgroupReductionRef(D, Level)5.38k
);
1902
792k
}
1903
1904
void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1905
211k
                                unsigned Level) {
1906
211k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1907
211k
  D = getCanonicalDecl(D);
1908
211k
  OpenMPClauseKind OMPC = OMPC_unknown;
1909
263k
  for (unsigned I = 
DSAStack211k
->getNestingLevel() + 1; I > Level;
--I51.5k
) {
1910
211k
    const unsigned NewLevel = I - 1;
1911
211k
    if (DSAStack->hasExplicitDSA(D,
1912
211k
                                 [&OMPC](const OpenMPClauseKind K) {
1913
46.8k
                                   if (isOpenMPPrivate(K)) {
1914
46.1k
                                     OMPC = K;
1915
46.1k
                                     return true;
1916
46.1k
                                   }
1917
665
                                   return false;
1918
665
                                 },
1919
211k
                                 NewLevel))
1920
46.1k
      break;
1921
165k
    if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1922
165k
            D, NewLevel,
1923
165k
            [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1924
165k
               OpenMPClauseKind) 
{ return true; }3.80k
)) {
1925
3.80k
      OMPC = OMPC_map;
1926
3.80k
      break;
1927
3.80k
    }
1928
161k
    if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1929
161k
                                       NewLevel)) {
1930
110k
      OMPC = OMPC_map;
1931
110k
      if (D->getType()->isScalarType() &&
1932
110k
          
DSAStack86.5k
->getDefaultDMAAtLevel(NewLevel) !=
1933
86.5k
              DefaultMapAttributes::DMA_tofrom_scalar)
1934
86.4k
        OMPC = OMPC_firstprivate;
1935
110k
      break;
1936
110k
    }
1937
161k
  }
1938
211k
  if (OMPC != OMPC_unknown)
1939
159k
    FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1940
211k
}
1941
1942
bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1943
757k
                                      unsigned Level) const {
1944
757k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1945
757k
  // Return true if the current level is no longer enclosed in a target region.
1946
757k
1947
757k
  const auto *VD = dyn_cast<VarDecl>(D);
1948
757k
  return VD && !VD->hasLocalStorage() &&
1949
757k
         
DSAStack27.8k
->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1950
27.8k
                                        Level);
1951
757k
}
1952
1953
32.9k
void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1954
1955
void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1956
                               const DeclarationNameInfo &DirName,
1957
171k
                               Scope *CurScope, SourceLocation Loc) {
1958
171k
  DSAStack->push(DKind, DirName, CurScope, Loc);
1959
171k
  PushExpressionEvaluationContext(
1960
171k
      ExpressionEvaluationContext::PotentiallyEvaluated);
1961
171k
}
1962
1963
114k
void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1964
114k
  DSAStack->setClauseParsingMode(K);
1965
114k
}
1966
1967
114k
void Sema::EndOpenMPClause() {
1968
114k
  DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1969
114k
}
1970
1971
static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
1972
                                 ArrayRef<OMPClause *> Clauses);
1973
1974
171k
void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1975
171k
  // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1976
171k
  //  A variable of class type (or array thereof) that appears in a lastprivate
1977
171k
  //  clause requires an accessible, unambiguous default constructor for the
1978
171k
  //  class type, unless the list item is also specified in a firstprivate
1979
171k
  //  clause.
1980
171k
  if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1981
147k
    for (OMPClause *C : D->clauses()) {
1982
103k
      if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1983
2.75k
        SmallVector<Expr *, 8> PrivateCopies;
1984
5.97k
        for (Expr *DE : Clause->varlists()) {
1985
5.97k
          if (DE->isValueDependent() || 
DE->isTypeDependent()4.38k
) {
1986
1.58k
            PrivateCopies.push_back(nullptr);
1987
1.58k
            continue;
1988
1.58k
          }
1989
4.38k
          auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1990
4.38k
          auto *VD = cast<VarDecl>(DRE->getDecl());
1991
4.38k
          QualType Type = VD->getType().getNonReferenceType();
1992
4.38k
          const DSAStackTy::DSAVarData DVar =
1993
4.38k
              DSAStack->getTopDSA(VD, /*FromParent=*/false);
1994
4.38k
          if (DVar.CKind == OMPC_lastprivate) {
1995
4.05k
            // Generate helper private variable and initialize it with the
1996
4.05k
            // default value. The address of the original variable is replaced
1997
4.05k
            // by the address of the new private variable in CodeGen. This new
1998
4.05k
            // variable is not added to IdResolver, so the code in the OpenMP
1999
4.05k
            // region uses original variable for proper diagnostics.
2000
4.05k
            VarDecl *VDPrivate = buildVarDecl(
2001
4.05k
                *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2002
4.05k
                VD->getName(), VD->hasAttrs() ? 
&VD->getAttrs()42
:
nullptr4.00k
, DRE);
2003
4.05k
            ActOnUninitializedDecl(VDPrivate);
2004
4.05k
            if (VDPrivate->isInvalidDecl()) {
2005
0
              PrivateCopies.push_back(nullptr);
2006
0
              continue;
2007
0
            }
2008
4.05k
            PrivateCopies.push_back(buildDeclRefExpr(
2009
4.05k
                *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2010
4.05k
          } else {
2011
338
            // The variable is also a firstprivate, so initialization sequence
2012
338
            // for private copy is generated already.
2013
338
            PrivateCopies.push_back(nullptr);
2014
338
          }
2015
4.38k
        }
2016
2.75k
        Clause->setPrivateCopies(PrivateCopies);
2017
2.75k
      }
2018
103k
    }
2019
147k
    // Check allocate clauses.
2020
147k
    if (!CurContext->isDependentContext())
2021
107k
      checkAllocateClauses(*this, DSAStack, D->clauses());
2022
147k
  }
2023
171k
2024
171k
  DSAStack->pop();
2025
171k
  DiscardCleanupsInEvaluationContext();
2026
171k
  PopExpressionEvaluationContext();
2027
171k
}
2028
2029
static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2030
                                     Expr *NumIterations, Sema &SemaRef,
2031
                                     Scope *S, DSAStackTy *Stack);
2032
2033
namespace {
2034
2035
class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2036
private:
2037
  Sema &SemaRef;
2038
2039
public:
2040
40
  explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2041
16
  bool ValidateCandidate(const TypoCorrection &Candidate) override {
2042
16
    NamedDecl *ND = Candidate.getCorrectionDecl();
2043
16
    if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2044
16
      return VD->hasGlobalStorage() &&
2045
16
             SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2046
16
                                   SemaRef.getCurScope());
2047
16
    }
2048
0
    return false;
2049
0
  }
2050
2051
40
  std::unique_ptr<CorrectionCandidateCallback> clone() override {
2052
40
    return llvm::make_unique<VarDeclFilterCCC>(*this);
2053
40
  }
2054
2055
};
2056
2057
class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2058
private:
2059
  Sema &SemaRef;
2060
2061
public:
2062
4
  explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2063
4
  bool ValidateCandidate(const TypoCorrection &Candidate) override {
2064
4
    NamedDecl *ND = Candidate.getCorrectionDecl();
2065
4
    if (ND && 
(0
(0
isa<VarDecl>(ND)0
&&
ND->getKind() == Decl::Var0
) ||
2066
0
               isa<FunctionDecl>(ND))) {
2067
0
      return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2068
0
                                   SemaRef.getCurScope());
2069
0
    }
2070
4
    return false;
2071
4
  }
2072
2073
4
  std::unique_ptr<CorrectionCandidateCallback> clone() override {
2074
4
    return llvm::make_unique<VarOrFuncDeclFilterCCC>(*this);
2075
4
  }
2076
};
2077
2078
} // namespace
2079
2080
ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2081
                                         CXXScopeSpec &ScopeSpec,
2082
                                         const DeclarationNameInfo &Id,
2083
2.22k
                                         OpenMPDirectiveKind Kind) {
2084
2.22k
  LookupResult Lookup(*this, Id, LookupOrdinaryName);
2085
2.22k
  LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2086
2.22k
2087
2.22k
  if (Lookup.isAmbiguous())
2088
8
    return ExprError();
2089
2.21k
2090
2.21k
  VarDecl *VD;
2091
2.21k
  if (!Lookup.isSingleResult()) {
2092
40
    VarDeclFilterCCC CCC(*this);
2093
40
    if (TypoCorrection Corrected =
2094
8
            CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2095
8
                        CTK_ErrorRecovery)) {
2096
8
      diagnoseTypo(Corrected,
2097
8
                   PDiag(Lookup.empty()
2098
8
                             ? diag::err_undeclared_var_use_suggest
2099
8
                             : 
diag::err_omp_expected_var_arg_suggest0
)
2100
8
                       << Id.getName());
2101
8
      VD = Corrected.getCorrectionDeclAs<VarDecl>();
2102
32
    } else {
2103
32
      Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2104
32
                                       : 
diag::err_omp_expected_var_arg0
)
2105
32
          << Id.getName();
2106
32
      return ExprError();
2107
32
    }
2108
2.17k
  } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2109
16
    Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2110
16
    Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2111
16
    return ExprError();
2112
16
  }
2113
2.16k
  Lookup.suppressDiagnostics();
2114
2.16k
2115
2.16k
  // OpenMP [2.9.2, Syntax, C/C++]
2116
2.16k
  //   Variables must be file-scope, namespace-scope, or static block-scope.
2117
2.16k
  if (Kind == OMPD_threadprivate && 
!VD->hasGlobalStorage()1.64k
) {
2118
8
    Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2119
8
        << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2120
8
    bool IsDecl =
2121
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2122
8
    Diag(VD->getLocation(),
2123
8
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2124
8
        << VD;
2125
8
    return ExprError();
2126
8
  }
2127
2.16k
2128
2.16k
  VarDecl *CanonicalVD = VD->getCanonicalDecl();
2129
2.16k
  NamedDecl *ND = CanonicalVD;
2130
2.16k
  // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2131
2.16k
  //   A threadprivate directive for file-scope variables must appear outside
2132
2.16k
  //   any definition or declaration.
2133
2.16k
  if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2134
2.16k
      
!getCurLexicalContext()->isTranslationUnit()1.15k
) {
2135
16
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2136
16
        << getOpenMPDirectiveName(Kind) << VD;
2137
16
    bool IsDecl =
2138
16
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2139
16
    Diag(VD->getLocation(),
2140
16
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2141
16
        << VD;
2142
16
    return ExprError();
2143
16
  }
2144
2.14k
  // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2145
2.14k
  //   A threadprivate directive for static class member variables must appear
2146
2.14k
  //   in the class definition, in the same scope in which the member
2147
2.14k
  //   variables are declared.
2148
2.14k
  if (CanonicalVD->isStaticDataMember() &&
2149
2.14k
      
!CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())148
) {
2150
8
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2151
8
        << getOpenMPDirectiveName(Kind) << VD;
2152
8
    bool IsDecl =
2153
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2154
8
    Diag(VD->getLocation(),
2155
8
         IsDecl ? diag::note_previous_decl : 
diag::note_defined_here0
)
2156
8
        << VD;
2157
8
    return ExprError();
2158
8
  }
2159
2.13k
  // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2160
2.13k
  //   A threadprivate directive for namespace-scope variables must appear
2161
2.13k
  //   outside any definition or declaration other than the namespace
2162
2.13k
  //   definition itself.
2163
2.13k
  if (CanonicalVD->getDeclContext()->isNamespace() &&
2164
2.13k
      
(487
!getCurLexicalContext()->isFileContext()487
||
2165
487
       !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2166
0
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2167
0
        << getOpenMPDirectiveName(Kind) << VD;
2168
0
    bool IsDecl =
2169
0
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2170
0
    Diag(VD->getLocation(),
2171
0
         IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2172
0
        << VD;
2173
0
    return ExprError();
2174
0
  }
2175
2.13k
  // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2176
2.13k
  //   A threadprivate directive for static block-scope variables must appear
2177
2.13k
  //   in the scope of the variable and not in a nested scope.
2178
2.13k
  if (CanonicalVD->isLocalVarDecl() && 
CurScope354
&&
2179
2.13k
      
!isDeclInScope(ND, getCurLexicalContext(), CurScope)354
) {
2180
8
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2181
8
        << getOpenMPDirectiveName(Kind) << VD;
2182
8
    bool IsDecl =
2183
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2184
8
    Diag(VD->getLocation(),
2185
8
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2186
8
        << VD;
2187
8
    return ExprError();
2188
8
  }
2189
2.12k
2190
2.12k
  // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2191
2.12k
  //   A threadprivate directive must lexically precede all references to any
2192
2.12k
  //   of the variables in its list.
2193
2.12k
  if (Kind == OMPD_threadprivate && 
VD->isUsed()1.62k
&&
2194
2.12k
      
!80
DSAStack80
->isThreadPrivate(VD)) {
2195
4
    Diag(Id.getLoc(), diag::err_omp_var_used)
2196
4
        << getOpenMPDirectiveName(Kind) << VD;
2197
4
    return ExprError();
2198
4
  }
2199
2.12k
2200
2.12k
  QualType ExprType = VD->getType().getNonReferenceType();
2201
2.12k
  return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2202
2.12k
                             SourceLocation(), VD,
2203
2.12k
                             /*RefersToEnclosingVariableOrCapture=*/false,
2204
2.12k
                             Id.getLoc(), ExprType, VK_LValue);
2205
2.12k
}
2206
2207
Sema::DeclGroupPtrTy
2208
Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2209
1.50k
                                        ArrayRef<Expr *> VarList) {
2210
1.50k
  if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2211
1.43k
    CurContext->addDecl(D);
2212
1.43k
    return DeclGroupPtrTy::make(DeclGroupRef(D));
2213
1.43k
  }
2214
68
  return nullptr;
2215
68
}
2216
2217
namespace {
2218
class LocalVarRefChecker final
2219
    : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2220
  Sema &SemaRef;
2221
2222
public:
2223
18
  bool VisitDeclRefExpr(const DeclRefExpr *E) {
2224
18
    if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2225
12
      if (VD->hasLocalStorage()) {
2226
4
        SemaRef.Diag(E->getBeginLoc(),
2227
4
                     diag::err_omp_local_var_in_threadprivate_init)
2228
4
            << E->getSourceRange();
2229
4
        SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2230
4
            << VD << VD->getSourceRange();
2231
4
        return true;
2232
4
      }
2233
14
    }
2234
14
    return false;
2235
14
  }
2236
1.50k
  bool VisitStmt(const Stmt *S) {
2237
1.50k
    for (const Stmt *Child : S->children()) {
2238
774
      if (Child && Visit(Child))
2239
8
        return true;
2240
774
    }
2241
1.50k
    
return false1.49k
;
2242
1.50k
  }
2243
750
  explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2244
};
2245
} // namespace
2246
2247
OMPThreadPrivateDecl *
2248
1.65k
Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2249
1.65k
  SmallVector<Expr *, 8> Vars;
2250
1.78k
  for (Expr *RefExpr : VarList) {
2251
1.78k
    auto *DE = cast<DeclRefExpr>(RefExpr);
2252
1.78k
    auto *VD = cast<VarDecl>(DE->getDecl());
2253
1.78k
    SourceLocation ILoc = DE->getExprLoc();
2254
1.78k
2255
1.78k
    // Mark variable as used.
2256
1.78k
    VD->setReferenced();
2257
1.78k
    VD->markUsed(Context);
2258
1.78k
2259
1.78k
    QualType QType = VD->getType();
2260
1.78k
    if (QType->isDependentType() || 
QType->isInstantiationDependentType()1.64k
) {
2261
144
      // It will be analyzed later.
2262
144
      Vars.push_back(DE);
2263
144
      continue;
2264
144
    }
2265
1.64k
2266
1.64k
    // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2267
1.64k
    //   A threadprivate variable must not have an incomplete type.
2268
1.64k
    if (RequireCompleteType(ILoc, VD->getType(),
2269
1.64k
                            diag::err_omp_threadprivate_incomplete_type)) {
2270
4
      continue;
2271
4
    }
2272
1.63k
2273
1.63k
    // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2274
1.63k
    //   A threadprivate variable must not have a reference type.
2275
1.63k
    if (VD->getType()->isReferenceType()) {
2276
4
      Diag(ILoc, diag::err_omp_ref_type_arg)
2277
4
          << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2278
4
      bool IsDecl =
2279
4
          VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2280
4
      Diag(VD->getLocation(),
2281
4
           IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2282
4
          << VD;
2283
4
      continue;
2284
4
    }
2285
1.63k
2286
1.63k
    // Check if this is a TLS variable. If TLS is not being supported, produce
2287
1.63k
    // the corresponding diagnostic.
2288
1.63k
    if ((VD->getTLSKind() != VarDecl::TLS_None &&
2289
1.63k
         
!(24
VD->hasAttr<OMPThreadPrivateDeclAttr>()24
&&
2290
24
           
getLangOpts().OpenMPUseTLS20
&&
2291
24
           
getASTContext().getTargetInfo().isTLSSupported()20
)) ||
2292
1.63k
        
(1.62k
VD->getStorageClass() == SC_Register1.62k
&&
VD->hasAttr<AsmLabelAttr>()4
&&
2293
1.62k
         
!VD->isLocalVarDecl()4
)) {
2294
8
      Diag(ILoc, diag::err_omp_var_thread_local)
2295
8
          << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 
04
:
14
);
2296
8
      bool IsDecl =
2297
8
          VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2298
8
      Diag(VD->getLocation(),
2299
8
           IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2300
8
          << VD;
2301
8
      continue;
2302
8
    }
2303
1.62k
2304
1.62k
    // Check if initial value of threadprivate variable reference variable with
2305
1.62k
    // local storage (it is not supported by runtime).
2306
1.62k
    if (const Expr *Init = VD->getAnyInitializer()) {
2307
750
      LocalVarRefChecker Checker(*this);
2308
750
      if (Checker.Visit(Init))
2309
4
        continue;
2310
1.62k
    }
2311
1.62k
2312
1.62k
    Vars.push_back(RefExpr);
2313
1.62k
    DSAStack->addDSA(VD, DE, OMPC_threadprivate);
2314
1.62k
    VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2315
1.62k
        Context, SourceRange(Loc, Loc)));
2316
1.62k
    if (ASTMutationListener *ML = Context.getASTMutationListener())
2317
165
      ML->DeclarationMarkedOpenMPThreadPrivate(VD);
2318
1.62k
  }
2319
1.65k
  OMPThreadPrivateDecl *D = nullptr;
2320
1.65k
  if (!Vars.empty()) {
2321
1.58k
    D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2322
1.58k
                                     Vars);
2323
1.58k
    D->setAccess(AS_public);
2324
1.58k
  }
2325
1.65k
  return D;
2326
1.65k
}
2327
2328
static OMPAllocateDeclAttr::AllocatorTypeTy
2329
1.24k
getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
2330
1.24k
  if (!Allocator)
2331
673
    return OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2332
568
  if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
2333
568
      Allocator->isInstantiationDependent() ||
2334
568
      Allocator->containsUnexpandedParameterPack())
2335
0
    return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2336
568
  auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
2337
568
  const Expr *AE = Allocator->IgnoreParenImpCasts();
2338
568
  for (int I = OMPAllocateDeclAttr::OMPDefaultMemAlloc;
2339
3.43k
       I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 
++I2.86k
) {
2340
3.42k
    auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
2341
3.42k
    const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
2342
3.42k
    llvm::FoldingSetNodeID AEId, DAEId;
2343
3.42k
    AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
2344
3.42k
    DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
2345
3.42k
    if (AEId == DAEId) {
2346
564
      AllocatorKindRes = AllocatorKind;
2347
564
      break;
2348
564
    }
2349
3.42k
  }
2350
568
  return AllocatorKindRes;
2351
568
}
2352
2353
static bool checkPreviousOMPAllocateAttribute(
2354
    Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
2355
1.10k
    OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
2356
1.10k
  if (!VD->hasAttr<OMPAllocateDeclAttr>())
2357
954
    return false;
2358
146
  const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
2359
146
  Expr *PrevAllocator = A->getAllocator();
2360
146
  OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
2361
146
      getAllocatorKind(S, Stack, PrevAllocator);
2362
146
  bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
2363
146
  if (AllocatorsMatch &&
2364
146
      
AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc140
&&
2365
146
      
Allocator0
&&
PrevAllocator0
) {
2366
0
    const Expr *AE = Allocator->IgnoreParenImpCasts();
2367
0
    const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
2368
0
    llvm::FoldingSetNodeID AEId, PAEId;
2369
0
    AE->Profile(AEId, S.Context, /*Canonical=*/true);
2370
0
    PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
2371
0
    AllocatorsMatch = AEId == PAEId;
2372
0
  }
2373
146
  if (!AllocatorsMatch) {
2374
6
    SmallString<256> AllocatorBuffer;
2375
6
    llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
2376
6
    if (Allocator)
2377
4
      Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
2378
6
    SmallString<256> PrevAllocatorBuffer;
2379
6
    llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
2380
6
    if (PrevAllocator)
2381
4
      PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
2382
4
                                 S.getPrintingPolicy());
2383
6
2384
6
    SourceLocation AllocatorLoc =
2385
6
        Allocator ? 
Allocator->getExprLoc()4
:
RefExpr->getExprLoc()2
;
2386
6
    SourceRange AllocatorRange =
2387
6
        Allocator ? 
Allocator->getSourceRange()4
:
RefExpr->getSourceRange()2
;
2388
6
    SourceLocation PrevAllocatorLoc =
2389
6
        PrevAllocator ? 
PrevAllocator->getExprLoc()4
:
A->getLocation()2
;
2390
6
    SourceRange PrevAllocatorRange =
2391
6
        PrevAllocator ? 
PrevAllocator->getSourceRange()4
:
A->getRange()2
;
2392
6
    S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
2393
6
        << (Allocator ? 
14
:
02
) << AllocatorStream.str()
2394
6
        << (PrevAllocator ? 
14
:
02
) << PrevAllocatorStream.str()
2395
6
        << AllocatorRange;
2396
6
    S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
2397
6
        << PrevAllocatorRange;
2398
6
    return true;
2399
6
  }
2400
140
  return false;
2401
140
}
2402
2403
static void
2404
applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
2405
                          OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
2406
1.09k
                          Expr *Allocator, SourceRange SR) {
2407
1.09k
  if (VD->hasAttr<OMPAllocateDeclAttr>())
2408
140
    return;
2409
950
  if (Allocator &&
2410
950
      
(501
Allocator->isTypeDependent()501
||
Allocator->isValueDependent()501
||
2411
501
       Allocator->isInstantiationDependent() ||
2412
501
       Allocator->containsUnexpandedParameterPack()))
2413
0
    return;
2414
950
  auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
2415
950
                                                Allocator, SR);
2416
950
  VD->addAttr(A);
2417
950
  if (ASTMutationListener *ML = S.Context.getASTMutationListener())
2418
161
    ML->DeclarationMarkedOpenMPAllocate(VD, A);
2419
950
}
2420
2421
Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
2422
    SourceLocation Loc, ArrayRef<Expr *> VarList,
2423
544
    ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
2424
544
  assert(Clauses.size() <= 1 && "Expected at most one clause.");
2425
544
  Expr *Allocator = nullptr;
2426
544
  if (Clauses.empty()) {
2427
302
    // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
2428
302
    // allocate directives that appear in a target region must specify an
2429
302
    // allocator clause unless a requires directive with the dynamic_allocators
2430
302
    // clause is present in the same compilation unit.
2431
302
    if (LangOpts.OpenMPIsDevice &&
2432
302
        
!4
DSAStack4
->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
2433
2
      targetDiag(Loc, diag::err_expected_allocator_clause);
2434
302
  } else {
2435
242
    Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
2436
242
  }
2437
544
  OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
2438
544
      getAllocatorKind(*this, DSAStack, Allocator);
2439
544
  SmallVector<Expr *, 8> Vars;
2440
561
  for (Expr *RefExpr : VarList) {
2441
561
    auto *DE = cast<DeclRefExpr>(RefExpr);
2442
561
    auto *VD = cast<VarDecl>(DE->getDecl());
2443
561
2444
561
    // Check if this is a TLS variable or global register.
2445
561
    if (VD->getTLSKind() != VarDecl::TLS_None ||
2446
561
        
VD->hasAttr<OMPThreadPrivateDeclAttr>()557
||
2447
561
        
(557
VD->getStorageClass() == SC_Register557
&&
VD->hasAttr<AsmLabelAttr>()4
&&
2448
557
         
!VD->isLocalVarDecl()4
))
2449
8
      continue;
2450
553
2451
553
    // If the used several times in the allocate directive, the same allocator
2452
553
    // must be used.
2453
553
    if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
2454
553
                                          AllocatorKind, Allocator))
2455
6
      continue;
2456
547
2457
547
    // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
2458
547
    // If a list item has a static storage type, the allocator expression in the
2459
547
    // allocator clause must be a constant expression that evaluates to one of
2460
547
    // the predefined memory allocator values.
2461
547
    if (Allocator && 
VD->hasGlobalStorage()267
) {
2462
203
      if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
2463
4
        Diag(Allocator->getExprLoc(),
2464
4
             diag::err_omp_expected_predefined_allocator)
2465
4
            << Allocator->getSourceRange();
2466
4
        bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2467
4
                      VarDecl::DeclarationOnly;
2468
4
        Diag(VD->getLocation(),
2469
4
             IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2470
4
            << VD;
2471
4
        continue;
2472
4
      }
2473
543
    }
2474
543
2475
543
    Vars.push_back(RefExpr);
2476
543
    applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
2477
543
                              DE->getSourceRange());
2478
543
  }
2479
544
  if (Vars.empty())
2480
58
    return nullptr;
2481
486
  if (!Owner)
2482
428
    Owner = getCurLexicalContext();
2483
486
  auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
2484
486
  D->setAccess(AS_public);
2485
486
  Owner->addDecl(D);
2486
486
  return DeclGroupPtrTy::make(DeclGroupRef(D));
2487
486
}
2488
2489
Sema::DeclGroupPtrTy
2490
Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2491
72
                                   ArrayRef<OMPClause *> ClauseList) {
2492
72
  OMPRequiresDecl *D = nullptr;
2493
72
  if (!CurContext->isFileContext()) {
2494
1
    Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2495
71
  } else {
2496
71
    D = CheckOMPRequiresDecl(Loc, ClauseList);
2497
71
    if (D) {
2498
58
      CurContext->addDecl(D);
2499
58
      DSAStack->addRequiresDecl(D);
2500
58
    }
2501
71
  }
2502
72
  return DeclGroupPtrTy::make(DeclGroupRef(D));
2503
72
}
2504
2505
OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2506
71
                                            ArrayRef<OMPClause *> ClauseList) {
2507
71
  /// For target specific clauses, the requires directive cannot be
2508
71
  /// specified after the handling of any of the target regions in the
2509
71
  /// current compilation unit.
2510
71
  ArrayRef<SourceLocation> TargetLocations =
2511
71
      DSAStack->getEncounteredTargetLocs();
2512
71
  if (!TargetLocations.empty()) {
2513
5
    for (const OMPClause *CNew : ClauseList) {
2514
5
      // Check if any of the requires clauses affect target regions.
2515
5
      if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
2516
5
          
isa<OMPUnifiedAddressClause>(CNew)4
||
2517
5
          
isa<OMPReverseOffloadClause>(CNew)3
||
2518
5
          
isa<OMPDynamicAllocatorsClause>(CNew)2
) {
2519
4
        Diag(Loc, diag::err_omp_target_before_requires)
2520
4
            << getOpenMPClauseName(CNew->getClauseKind());
2521
4
        for (SourceLocation TargetLoc : TargetLocations) {
2522
4
          Diag(TargetLoc, diag::note_omp_requires_encountered_target);
2523
4
        }
2524
4
      }
2525
5
    }
2526
5
  }
2527
71
2528
71
  if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2529
58
    return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2530
58
                                   ClauseList);
2531
13
  return nullptr;
2532
13
}
2533
2534
static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2535
                              const ValueDecl *D,
2536
                              const DSAStackTy::DSAVarData &DVar,
2537
2.86k
                              bool IsLoopIterVar = false) {
2538
2.86k
  if (DVar.RefExpr) {
2539
2.28k
    SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2540
2.28k
        << getOpenMPClauseName(DVar.CKind);
2541
2.28k
    return;
2542
2.28k
  }
2543
578
  enum {
2544
578
    PDSA_StaticMemberShared,
2545
578
    PDSA_StaticLocalVarShared,
2546
578
    PDSA_LoopIterVarPrivate,
2547
578
    PDSA_LoopIterVarLinear,
2548
578
    PDSA_LoopIterVarLastprivate,
2549
578
    PDSA_ConstVarShared,
2550
578
    PDSA_GlobalVarShared,
2551
578
    PDSA_TaskVarFirstprivate,
2552
578
    PDSA_LocalVarPrivate,
2553
578
    PDSA_Implicit
2554
578
  } Reason = PDSA_Implicit;
2555
578
  bool ReportHint = false;
2556
578
  auto ReportLoc = D->getLocation();
2557
578
  auto *VD = dyn_cast<VarDecl>(D);
2558
578
  if (IsLoopIterVar) {
2559
0
    if (DVar.CKind == OMPC_private)
2560
0
      Reason = PDSA_LoopIterVarPrivate;
2561
0
    else if (DVar.CKind == OMPC_lastprivate)
2562
0
      Reason = PDSA_LoopIterVarLastprivate;
2563
0
    else
2564
0
      Reason = PDSA_LoopIterVarLinear;
2565
578
  } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2566
578
             
DVar.CKind == OMPC_firstprivate4
) {
2567
4
    Reason = PDSA_TaskVarFirstprivate;
2568
4
    ReportLoc = DVar.ImplicitDSALoc;
2569
574
  } else if (VD && VD->isStaticLocal())
2570
0
    Reason = PDSA_StaticLocalVarShared;
2571
574
  else if (VD && VD->isStaticDataMember())
2572
464
    Reason = PDSA_StaticMemberShared;
2573
110
  else if (VD && VD->isFileVarDecl())
2574
0
    Reason = PDSA_GlobalVarShared;
2575
110
  else if (D->getType().isConstant(SemaRef.getASTContext()))
2576
0
    Reason = PDSA_ConstVarShared;
2577
110
  else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
2578
38
    ReportHint = true;
2579
38
    Reason = PDSA_LocalVarPrivate;
2580
38
  }
2581
578
  if (Reason != PDSA_Implicit) {
2582
506
    SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
2583
506
        << Reason << ReportHint
2584
506
        << getOpenMPDirectiveName(Stack->getCurrentDirective());
2585
506
  } else 
if (72
DVar.ImplicitDSALoc.isValid()72
) {
2586
12
    SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2587
12
        << getOpenMPClauseName(DVar.CKind);
2588
12
  }
2589
578
}
2590
2591
namespace {
2592
class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
2593
  DSAStackTy *Stack;
2594
  Sema &SemaRef;
2595
  bool ErrorFound = false;
2596
  CapturedStmt *CS = nullptr;
2597
  llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2598
  llvm::SmallVector<Expr *, 4> ImplicitMap;
2599
  Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2600
  llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
2601
2602
34.9k
  void VisitSubCaptures(OMPExecutableDirective *S) {
2603
34.9k
    // Check implicitly captured variables.
2604
34.9k
    if (!S->hasAssociatedStmt() || 
!S->getAssociatedStmt()34.3k
)
2605
793
      return;
2606
34.1k
    visitSubCaptures(S->getInnermostCapturedStmt());
2607
34.1k
  }
2608
2609
public:
2610
450k
  void VisitDeclRefExpr(DeclRefExpr *E) {
2611
450k
    if (E->isTypeDependent() || 
E->isValueDependent()450k
||
2612
450k
        
E->containsUnexpandedParameterPack()450k
||
E->isInstantiationDependent()450k
)
2613
4
      return;
2614
450k
    if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2615
413k
      // Check the datasharing rules for the expressions in the clauses.
2616
413k
      if (!CS) {
2617
68
        if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
2618
32
          if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
2619
32
            Visit(CED->getInit());
2620
32
            return;
2621
32
          }
2622
413k
      } else if (VD->isImplicit() || 
isa<OMPCapturedExprDecl>(VD)406k
)
2623
6.49k
        // Do not analyze internal variables and do not enclose them into
2624
6.49k
        // implicit clauses.
2625
6.49k
        return;
2626
406k
      VD = VD->getCanonicalDecl();
2627
406k
      // Skip internally declared variables.
2628
406k
      if (VD->hasLocalStorage() && 
CS382k
&&
!CS->capturesVariable(VD)382k
)
2629
152k
        return;
2630
253k
2631
253k
      DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
2632
253k
      // Check if the variable has explicit DSA set and stop analysis if it so.
2633
253k
      if (DVar.RefExpr || 
!ImplicitDeclarations.insert(VD).second193k
)
2634
165k
        return;
2635
88.2k
2636
88.2k
      // Skip internally declared static variables.
2637
88.2k
      llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2638
88.2k
          OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
2639
88.2k
      if (VD->hasGlobalStorage() && 
CS7.78k
&&
!CS->capturesVariable(VD)7.78k
&&
2640
88.2k
          
(3.36k
Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>()3.36k
||
2641
3.36k
           
!Res3.36k
||
*Res != OMPDeclareTargetDeclAttr::MT_Link48
))
2642
3.34k
        return;
2643
84.8k
2644
84.8k
      SourceLocation ELoc = E->getExprLoc();
2645
84.8k
      OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2646
84.8k
      // The default(none) clause requires that each variable that is referenced
2647
84.8k
      // in the construct, and does not have a predetermined data-sharing
2648
84.8k
      // attribute, must have its data-sharing attribute explicitly determined
2649
84.8k
      // by being listed in a data-sharing attribute clause.
2650
84.8k
      if (DVar.CKind == OMPC_unknown && 
Stack->getDefaultDSA() == DSA_none83.8k
&&
2651
84.8k
          
isImplicitOrExplicitTaskingRegion(DKind)201
&&
2652
84.8k
          
VarsWithInheritedDSA.count(VD) == 0201
) {
2653
201
        VarsWithInheritedDSA[VD] = E;
2654
201
        return;
2655
201
      }
2656
84.6k
2657
84.6k
      if (isOpenMPTargetExecutionDirective(DKind) &&
2658
84.6k
          
!Stack->isLoopControlVariable(VD).first41.1k
) {
2659
41.1k
        if (!Stack->checkMappableExprComponentListsForDecl(
2660
41.1k
                VD, /*CurrentRegionOnly=*/true,
2661
41.1k
                [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2662
41.1k
                       StackComponents,
2663
41.1k
                   OpenMPClauseKind) {
2664
1.21k
                  // Variable is used if it has been marked as an array, array
2665
1.21k
                  // section or the variable iself.
2666
1.21k
                  return StackComponents.size() == 1 ||
2667
1.21k
                         std::all_of(
2668
532
                             std::next(StackComponents.rbegin()),
2669
532
                             StackComponents.rend(),
2670
532
                             [](const OMPClauseMappableExprCommon::
2671
756
                                    MappableComponent &MC) {
2672
756
                               return MC.getAssociatedDeclaration() ==
2673
756
                                          nullptr &&
2674
756
                                      
(646
isa<OMPArraySectionExpr>(
2675
646
                                           MC.getAssociatedExpression()) ||
2676
646
                                       isa<ArraySubscriptExpr>(
2677
184
                                           MC.getAssociatedExpression()));
2678
756
                             });
2679
40.0k
                })) {
2680
40.0k
          bool IsFirstprivate = false;
2681
40.0k
          // By default lambdas are captured as firstprivates.
2682
40.0k
          if (const auto *RD =
2683
2.99k
                  VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
2684
2.99k
            IsFirstprivate = RD->isLambda();
2685
40.0k
          IsFirstprivate =
2686
40.0k
              IsFirstprivate ||
2687
40.0k
              
(39.9k
VD->getType().getNonReferenceType()->isScalarType()39.9k
&&
2688
39.9k
               
Stack->getDefaultDMA() != DMA_tofrom_scalar31.4k
&&
!Res31.3k
);
2689
40.0k
          if (IsFirstprivate)
2690
31.3k
            ImplicitFirstprivate.emplace_back(E);
2691
8.61k
          else
2692
8.61k
            ImplicitMap.emplace_back(E);
2693
40.0k
          return;
2694
40.0k
        }
2695
44.6k
      }
2696
44.6k
2697
44.6k
      // OpenMP [2.9.3.6, Restrictions, p.2]
2698
44.6k
      //  A list item that appears in a reduction clause of the innermost
2699
44.6k
      //  enclosing worksharing or parallel construct may not be accessed in an
2700
44.6k
      //  explicit task.
2701
44.6k
      DVar = Stack->hasInnermostDSA(
2702
44.6k
          VD, [](OpenMPClauseKind C) 
{ return C == OMPC_reduction; }9.57k
,
2703
44.6k
          [](OpenMPDirectiveKind K) {
2704
26.8k
            return isOpenMPParallelDirective(K) ||
2705
26.8k
                   
isOpenMPWorksharingDirective(K)23.2k
||
isOpenMPTeamsDirective(K)22.9k
;
2706
26.8k
          },
2707
44.6k
          /*FromParent=*/true);
2708
44.6k
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind == OMPC_reduction3.02k
) {
2709
28
        ErrorFound = true;
2710
28
        SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2711
28
        reportOriginalDsa(SemaRef, Stack, VD, DVar);
2712
28
        return;
2713
28
      }
2714
44.6k
2715
44.6k
      // Define implicit data-sharing attributes for task.
2716
44.6k
      DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
2717
44.6k
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind != OMPC_shared2.99k
&&
2718
44.6k
          
!Stack->isLoopControlVariable(VD).first1.75k
) {
2719
1.75k
        ImplicitFirstprivate.push_back(E);
2720
1.75k
        return;
2721
1.75k
      }
2722
42.8k
2723
42.8k
      // Store implicitly used globals with declare target link for parent
2724
42.8k
      // target.
2725
42.8k
      if (!isOpenMPTargetExecutionDirective(DKind) && 
Res41.7k
&&
2726
42.8k
          
*Res == OMPDeclareTargetDeclAttr::MT_Link8
) {
2727
8
        Stack->addToParentTargetRegionLinkGlobals(E);
2728
8
        return;
2729
8
      }
2730
42.8k
    }
2731
450k
  }
2732
3.94k
  void VisitMemberExpr(MemberExpr *E) {
2733
3.94k
    if (E->isTypeDependent() || E->isValueDependent() ||
2734
3.94k
        E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2735
0
      return;
2736
3.94k
    auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2737
3.94k
    OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2738
3.94k
    if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
2739
1.49k
      if (!FD)
2740
22
        return;
2741
1.47k
      DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
2742
1.47k
      // Check if the variable has explicit DSA set and stop analysis if it
2743
1.47k
      // so.
2744
1.47k
      if (DVar.RefExpr || 
!ImplicitDeclarations.insert(FD).second1.34k
)
2745
399
        return;
2746
1.07k
2747
1.07k
      if (isOpenMPTargetExecutionDirective(DKind) &&
2748
1.07k
          
!Stack->isLoopControlVariable(FD).first749
&&
2749
1.07k
          !Stack->checkMappableExprComponentListsForDecl(
2750
749
              FD, /*CurrentRegionOnly=*/true,
2751
749
              [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2752
749
                     StackComponents,
2753
749
                 OpenMPClauseKind) {
2754
72
                return isa<CXXThisExpr>(
2755
72
                    cast<MemberExpr>(
2756
72
                        StackComponents.back().getAssociatedExpression())
2757
72
                        ->getBase()
2758
72
                        ->IgnoreParens());
2759
677
              })) {
2760
677
        // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2761
677
        //  A bit-field cannot appear in a map clause.
2762
677
        //
2763
677
        if (FD->isBitField())
2764
2
          return;
2765
675
2766
675
        // Check to see if the member expression is referencing a class that
2767
675
        // has already been explicitly mapped
2768
675
        if (Stack->isClassPreviouslyMapped(TE->getType()))
2769
20
          return;
2770
655
2771
655
        ImplicitMap.emplace_back(E);
2772
655
        return;
2773
655
      }
2774
396
2775
396
      SourceLocation ELoc = E->getExprLoc();
2776
396
      // OpenMP [2.9.3.6, Restrictions, p.2]
2777
396
      //  A list item that appears in a reduction clause of the innermost
2778
396
      //  enclosing worksharing or parallel construct may not be accessed in
2779
396
      //  an  explicit task.
2780
396
      DVar = Stack->hasInnermostDSA(
2781
396
          FD, [](OpenMPClauseKind C) 
{ return C == OMPC_reduction; }4
,
2782
396
          [](OpenMPDirectiveKind K) {
2783
262
            return isOpenMPParallelDirective(K) ||
2784
262
                   
isOpenMPWorksharingDirective(K)258
||
isOpenMPTeamsDirective(K)258
;
2785
262
          },
2786
396
          /*FromParent=*/true);
2787
396
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind == OMPC_reduction16
) {
2788
0
        ErrorFound = true;
2789
0
        SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2790
0
        reportOriginalDsa(SemaRef, Stack, FD, DVar);
2791
0
        return;
2792
0
      }
2793
396
2794
396
      // Define implicit data-sharing attributes for task.
2795
396
      DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
2796
396
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind != OMPC_shared16
&&
2797
396
          
!Stack->isLoopControlVariable(FD).first0
) {
2798
0
        // Check if there is a captured expression for the current field in the
2799
0
        // region. Do not mark it as firstprivate unless there is no captured
2800
0
        // expression.
2801
0
        // TODO: try to make it firstprivate.
2802
0
        if (DVar.CKind != OMPC_unknown)
2803
0
          ImplicitFirstprivate.push_back(E);
2804
0
      }
2805
396
      return;
2806
396
    }
2807
2.44k
    if (isOpenMPTargetExecutionDirective(DKind)) {
2808
1.13k
      OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
2809
1.13k
      if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
2810
1.13k
                                        /*NoDiagnose=*/true))
2811
0
        return;
2812
1.13k
      const auto *VD = cast<ValueDecl>(
2813
1.13k
          CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2814
1.13k
      if (!Stack->checkMappableExprComponentListsForDecl(
2815
1.13k
              VD, /*CurrentRegionOnly=*/true,
2816
1.13k
              [&CurComponents](
2817
1.13k
                  OMPClauseMappableExprCommon::MappableExprComponentListRef
2818
1.13k
                      StackComponents,
2819
1.13k
                  OpenMPClauseKind) {
2820
292
                auto CCI = CurComponents.rbegin();
2821
292
                auto CCE = CurComponents.rend();
2822
488
                for (const auto &SC : llvm::reverse(StackComponents)) {
2823
488
                  // Do both expressions have the same kind?
2824
488
                  if (CCI->getAssociatedExpression()->getStmtClass() !=
2825
488
                      SC.getAssociatedExpression()->getStmtClass())
2826
36
                    if (!(isa<OMPArraySectionExpr>(
2827
36
                              SC.getAssociatedExpression()) &&
2828
36
                          isa<ArraySubscriptExpr>(
2829
36
                              CCI->getAssociatedExpression())))
2830
0
                      return false;
2831
488
2832
488
                  const Decl *CCD = CCI->getAssociatedDeclaration();
2833
488
                  const Decl *SCD = SC.getAssociatedDeclaration();
2834
488
                  CCD = CCD ? 
CCD->getCanonicalDecl()452
:
nullptr36
;
2835
488
                  SCD = SCD ? 
SCD->getCanonicalDecl()452
:
nullptr36
;
2836
488
                  if (SCD != CCD)
2837
110
                    return false;
2838
378
                  std::advance(CCI, 1);
2839
378
                  if (CCI == CCE)
2840
42
                    break;
2841
378
                }
2842
292
                
return true182
;
2843
950
              })) {
2844
950
        Visit(E->getBase());
2845
950
      }
2846
1.31k
    } else {
2847
1.31k
      Visit(E->getBase());
2848
1.31k
    }
2849
2.44k
  }
2850
34.9k
  void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
2851
34.9k
    for (OMPClause *C : S->clauses()) {
2852
16.8k
      // Skip analysis of arguments of implicitly defined firstprivate clause
2853
16.8k
      // for task|target directives.
2854
16.8k
      // Skip analysis of arguments of implicitly defined map clause for target
2855
16.8k
      // directives.
2856
16.8k
      if (C && !((isa<OMPFirstprivateClause>(C) || 
isa<OMPMapClause>(C)13.7k
) &&
2857
16.8k
                 
C->isImplicit()3.91k
)) {
2858
18.6k
        for (Stmt *CC : C->children()) {
2859
18.6k
          if (CC)
2860
17.7k
            Visit(CC);
2861
18.6k
        }
2862
15.1k
      }
2863
16.8k
    }
2864
34.9k
    // Check implicitly captured variables.
2865
34.9k
    VisitSubCaptures(S);
2866
34.9k
  }
2867
902k
  void VisitStmt(Stmt *S) {
2868
1.17M
    for (Stmt *C : S->children()) {
2869
1.17M
      if (C) {
2870
1.11M
        // Check implicitly captured variables in the task-based directives to
2871
1.11M
        // check if they must be firstprivatized.
2872
1.11M
        Visit(C);
2873
1.11M
      }
2874
1.17M
    }
2875
902k
  }
2876
2877
135k
  void visitSubCaptures(CapturedStmt *S) {
2878
150k
    for (const CapturedStmt::Capture &Cap : S->captures()) {
2879
150k
      if (!Cap.capturesVariable() && 
!Cap.capturesVariableByCopy()50.6k
)
2880
5.82k
        continue;
2881
145k
      VarDecl *VD = Cap.getCapturedVar();
2882
145k
      // Do not try to map the variable if it or its sub-component was mapped
2883
145k
      // already.
2884
145k
      if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2885
145k
          Stack->checkMappableExprComponentListsForDecl(
2886
70.8k
              VD, /*CurrentRegionOnly=*/true,
2887
70.8k
              [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2888
70.8k
                 OpenMPClauseKind) 
{ return true; }1.66k
))
2889
1.66k
        continue;
2890
143k
      DeclRefExpr *DRE = buildDeclRefExpr(
2891
143k
          SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2892
143k
          Cap.getLocation(), /*RefersToCapture=*/true);
2893
143k
      Visit(DRE);
2894
143k
    }
2895
135k
  }
2896
111k
  bool isErrorFound() const { return ErrorFound; }
2897
222k
  ArrayRef<Expr *> getImplicitFirstprivate() const {
2898
222k
    return ImplicitFirstprivate;
2899
222k
  }
2900
222k
  ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
2901
111k
  const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
2902
111k
    return VarsWithInheritedDSA;
2903
111k
  }
2904
2905
  DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2906
111k
      : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
2907
111k
    // Process declare target link variables for the target directives.
2908
111k
    if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
2909
49.3k
      for (DeclRefExpr *E : Stack->getLinkGlobals())
2910
8
        Visit(E);
2911
49.3k
    }
2912
111k
  }
2913
};
2914
} // namespace
2915
2916
168k
void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
2917
168k
  switch (DKind) {
2918
168k
  case OMPD_parallel:
2919
41.6k
  case OMPD_parallel_for:
2920
41.6k
  case OMPD_parallel_for_simd:
2921
41.6k
  case OMPD_parallel_sections:
2922
41.6k
  case OMPD_teams:
2923
41.6k
  case OMPD_teams_distribute:
2924
41.6k
  case OMPD_teams_distribute_simd: {
2925
41.6k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2926
41.6k
    QualType KmpInt32PtrTy =
2927
41.6k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2928
41.6k
    Sema::CapturedParamNameType Params[] = {
2929
41.6k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
2930
41.6k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
2931
41.6k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
2932
41.6k
    };
2933
41.6k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2934
41.6k
                             Params);
2935
41.6k
    break;
2936
41.6k
  }
2937
41.6k
  case OMPD_target_teams:
2938
26.5k
  case OMPD_target_parallel:
2939
26.5k
  case OMPD_target_parallel_for:
2940
26.5k
  case OMPD_target_parallel_for_simd:
2941
26.5k
  case OMPD_target_teams_distribute:
2942
26.5k
  case OMPD_target_teams_distribute_simd: {
2943
26.5k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2944
26.5k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2945
26.5k
    QualType KmpInt32PtrTy =
2946
26.5k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2947
26.5k
    QualType Args[] = {VoidPtrTy};
2948
26.5k
    FunctionProtoType::ExtProtoInfo EPI;
2949
26.5k
    EPI.Variadic = true;
2950
26.5k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2951
26.5k
    Sema::CapturedParamNameType Params[] = {
2952
26.5k
        std::make_pair(".global_tid.", KmpInt32Ty),
2953
26.5k
        std::make_pair(".part_id.", KmpInt32PtrTy),
2954
26.5k
        std::make_pair(".privates.", VoidPtrTy),
2955
26.5k
        std::make_pair(
2956
26.5k
            ".copy_fn.",
2957
26.5k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
2958
26.5k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2959
26.5k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
2960
26.5k
    };
2961
26.5k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2962
26.5k
                             Params);
2963
26.5k
    // Mark this captured region as inlined, because we don't use outlined
2964
26.5k
    // function directly.
2965
26.5k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
2966
26.5k
        AlwaysInlineAttr::CreateImplicit(
2967
26.5k
            Context, AlwaysInlineAttr::Keyword_forceinline));
2968
26.5k
    Sema::CapturedParamNameType ParamsTarget[] = {
2969
26.5k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
2970
26.5k
    };
2971
26.5k
    // Start a captured region for 'target' with no implicit parameters.
2972
26.5k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2973
26.5k
                             ParamsTarget);
2974
26.5k
    Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
2975
26.5k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
2976
26.5k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
2977
26.5k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
2978
26.5k
    };
2979
26.5k
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
2980
26.5k
    // the same implicit parameters.
2981
26.5k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2982
26.5k
                             ParamsTeamsOrParallel);
2983
26.5k
    break;
2984
26.5k
  }
2985
35.9k
  case OMPD_target:
2986
35.9k
  case OMPD_target_simd: {
2987
35.9k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2988
35.9k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2989
35.9k
    QualType KmpInt32PtrTy =
2990
35.9k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2991
35.9k
    QualType Args[] = {VoidPtrTy};
2992
35.9k
    FunctionProtoType::ExtProtoInfo EPI;
2993
35.9k
    EPI.Variadic = true;
2994
35.9k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2995
35.9k
    Sema::CapturedParamNameType Params[] = {
2996
35.9k
        std::make_pair(".global_tid.", KmpInt32Ty),
2997
35.9k
        std::make_pair(".part_id.", KmpInt32PtrTy),
2998
35.9k
        std::make_pair(".privates.", VoidPtrTy),
2999
35.9k
        std::make_pair(
3000
35.9k
            ".copy_fn.",
3001
35.9k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3002
35.9k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3003
35.9k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3004
35.9k
    };
3005
35.9k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3006
35.9k
                             Params);
3007
35.9k
    // Mark this captured region as inlined, because we don't use outlined
3008
35.9k
    // function directly.
3009
35.9k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3010
35.9k
        AlwaysInlineAttr::CreateImplicit(
3011
35.9k
            Context, AlwaysInlineAttr::Keyword_forceinline));
3012
35.9k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3013
35.9k
                             std::make_pair(StringRef(), QualType()));
3014
35.9k
    break;
3015
35.9k
  }
3016
35.9k
  case OMPD_simd:
3017
27.1k
  case OMPD_for:
3018
27.1k
  case OMPD_for_simd:
3019
27.1k
  case OMPD_sections:
3020
27.1k
  case OMPD_section:
3021
27.1k
  case OMPD_single:
3022
27.1k
  case OMPD_master:
3023
27.1k
  case OMPD_critical:
3024
27.1k
  case OMPD_taskgroup:
3025
27.1k
  case OMPD_distribute:
3026
27.1k
  case OMPD_distribute_simd:
3027
27.1k
  case OMPD_ordered:
3028
27.1k
  case OMPD_atomic:
3029
27.1k
  case OMPD_target_data: {
3030
27.1k
    Sema::CapturedParamNameType Params[] = {
3031
27.1k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3032
27.1k
    };
3033
27.1k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3034
27.1k
                             Params);
3035
27.1k
    break;
3036
27.1k
  }
3037
27.1k
  case OMPD_task: {
3038
2.14k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3039
2.14k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3040
2.14k
    QualType KmpInt32PtrTy =
3041
2.14k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3042
2.14k
    QualType Args[] = {VoidPtrTy};
3043
2.14k
    FunctionProtoType::ExtProtoInfo EPI;
3044
2.14k
    EPI.Variadic = true;
3045
2.14k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3046
2.14k
    Sema::CapturedParamNameType Params[] = {
3047
2.14k
        std::make_pair(".global_tid.", KmpInt32Ty),
3048
2.14k
        std::make_pair(".part_id.", KmpInt32PtrTy),
3049
2.14k
        std::make_pair(".privates.", VoidPtrTy),
3050
2.14k
        std::make_pair(
3051
2.14k
            ".copy_fn.",
3052
2.14k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3053
2.14k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3054
2.14k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3055
2.14k
    };
3056
2.14k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3057
2.14k
                             Params);
3058
2.14k
    // Mark this captured region as inlined, because we don't use outlined
3059
2.14k
    // function directly.
3060
2.14k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3061
2.14k
        AlwaysInlineAttr::CreateImplicit(
3062
2.14k
            Context, AlwaysInlineAttr::Keyword_forceinline));
3063
2.14k
    break;
3064
27.1k
  }
3065
27.1k
  case OMPD_taskloop:
3066
7.14k
  case OMPD_taskloop_simd: {
3067
7.14k
    QualType KmpInt32Ty =
3068
7.14k
        Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
3069
7.14k
            .withConst();
3070
7.14k
    QualType KmpUInt64Ty =
3071
7.14k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
3072
7.14k
            .withConst();
3073
7.14k
    QualType KmpInt64Ty =
3074
7.14k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
3075
7.14k
            .withConst();
3076
7.14k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3077
7.14k
    QualType KmpInt32PtrTy =
3078
7.14k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3079
7.14k
    QualType Args[] = {VoidPtrTy};
3080
7.14k
    FunctionProtoType::ExtProtoInfo EPI;
3081
7.14k
    EPI.Variadic = true;
3082
7.14k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3083
7.14k
    Sema::CapturedParamNameType Params[] = {
3084
7.14k
        std::make_pair(".global_tid.", KmpInt32Ty),
3085
7.14k
        std::make_pair(".part_id.", KmpInt32PtrTy),
3086
7.14k
        std::make_pair(".privates.", VoidPtrTy),
3087
7.14k
        std::make_pair(
3088
7.14k
            ".copy_fn.",
3089
7.14k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3090
7.14k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3091
7.14k
        std::make_pair(".lb.", KmpUInt64Ty),
3092
7.14k
        std::make_pair(".ub.", KmpUInt64Ty),
3093
7.14k
        std::make_pair(".st.", KmpInt64Ty),
3094
7.14k
        std::make_pair(".liter.", KmpInt32Ty),
3095
7.14k
        std::make_pair(".reductions.", VoidPtrTy),
3096
7.14k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3097
7.14k
    };
3098
7.14k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3099
7.14k
                             Params);
3100
7.14k
    // Mark this captured region as inlined, because we don't use outlined
3101
7.14k
    // function directly.
3102
7.14k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3103
7.14k
        AlwaysInlineAttr::CreateImplicit(
3104
7.14k
            Context, AlwaysInlineAttr::Keyword_forceinline));
3105
7.14k
    break;
3106
7.14k
  }
3107
7.56k
  case OMPD_distribute_parallel_for_simd:
3108
7.56k
  case OMPD_distribute_parallel_for: {
3109
7.56k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3110
7.56k
    QualType KmpInt32PtrTy =
3111
7.56k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3112
7.56k
    Sema::CapturedParamNameType Params[] = {
3113
7.56k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3114
7.56k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3115
7.56k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3116
7.56k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3117
7.56k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3118
7.56k
    };
3119
7.56k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3120
7.56k
                             Params);
3121
7.56k
    break;
3122
7.56k
  }
3123
10.1k
  case OMPD_target_teams_distribute_parallel_for:
3124
10.1k
  case OMPD_target_teams_distribute_parallel_for_simd: {
3125
10.1k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3126
10.1k
    QualType KmpInt32PtrTy =
3127
10.1k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3128
10.1k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3129
10.1k
3130
10.1k
    QualType Args[] = {VoidPtrTy};
3131
10.1k
    FunctionProtoType::ExtProtoInfo EPI;
3132
10.1k
    EPI.Variadic = true;
3133
10.1k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3134
10.1k
    Sema::CapturedParamNameType Params[] = {
3135
10.1k
        std::make_pair(".global_tid.", KmpInt32Ty),
3136
10.1k
        std::make_pair(".part_id.", KmpInt32PtrTy),
3137
10.1k
        std::make_pair(".privates.", VoidPtrTy),
3138
10.1k
        std::make_pair(
3139
10.1k
            ".copy_fn.",
3140
10.1k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3141
10.1k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3142
10.1k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3143
10.1k
    };
3144
10.1k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3145
10.1k
                             Params);
3146
10.1k
    // Mark this captured region as inlined, because we don't use outlined
3147
10.1k
    // function directly.
3148
10.1k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3149
10.1k
        AlwaysInlineAttr::CreateImplicit(
3150
10.1k
            Context, AlwaysInlineAttr::Keyword_forceinline));
3151
10.1k
    Sema::CapturedParamNameType ParamsTarget[] = {
3152
10.1k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3153
10.1k
    };
3154
10.1k
    // Start a captured region for 'target' with no implicit parameters.
3155
10.1k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3156
10.1k
                             ParamsTarget);
3157
10.1k
3158
10.1k
    Sema::CapturedParamNameType ParamsTeams[] = {
3159
10.1k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3160
10.1k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3161
10.1k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3162
10.1k
    };
3163
10.1k
    // Start a captured region for 'target' with no implicit parameters.
3164
10.1k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3165
10.1k
                             ParamsTeams);
3166
10.1k
3167
10.1k
    Sema::CapturedParamNameType ParamsParallel[] = {
3168
10.1k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3169
10.1k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3170
10.1k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3171
10.1k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3172
10.1k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3173
10.1k
    };
3174
10.1k
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
3175
10.1k
    // the same implicit parameters.
3176
10.1k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3177
10.1k
                             ParamsParallel);
3178
10.1k
    break;
3179
10.1k
  }
3180
10.1k
3181
10.1k
  case OMPD_teams_distribute_parallel_for:
3182
7.06k
  case OMPD_teams_distribute_parallel_for_simd: {
3183
7.06k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3184
7.06k
    QualType KmpInt32PtrTy =
3185
7.06k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3186
7.06k
3187
7.06k
    Sema::CapturedParamNameType ParamsTeams[] = {
3188
7.06k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3189
7.06k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3190
7.06k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3191
7.06k
    };
3192
7.06k
    // Start a captured region for 'target' with no implicit parameters.
3193
7.06k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3194
7.06k
                             ParamsTeams);
3195
7.06k
3196
7.06k
    Sema::CapturedParamNameType ParamsParallel[] = {
3197
7.06k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3198
7.06k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3199
7.06k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
3200
7.06k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
3201
7.06k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3202
7.06k
    };
3203
7.06k
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
3204
7.06k
    // the same implicit parameters.
3205
7.06k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3206
7.06k
                             ParamsParallel);
3207
7.06k
    break;
3208
7.06k
  }
3209
7.06k
  case OMPD_target_update:
3210
3.34k
  case OMPD_target_enter_data:
3211
3.34k
  case OMPD_target_exit_data: {
3212
3.34k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3213
3.34k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3214
3.34k
    QualType KmpInt32PtrTy =
3215
3.34k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3216
3.34k
    QualType Args[] = {VoidPtrTy};
3217
3.34k
    FunctionProtoType::ExtProtoInfo EPI;
3218
3.34k
    EPI.Variadic = true;
3219
3.34k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3220
3.34k
    Sema::CapturedParamNameType Params[] = {
3221
3.34k
        std::make_pair(".global_tid.", KmpInt32Ty),
3222
3.34k
        std::make_pair(".part_id.", KmpInt32PtrTy),
3223
3.34k
        std::make_pair(".privates.", VoidPtrTy),
3224
3.34k
        std::make_pair(
3225
3.34k
            ".copy_fn.",
3226
3.34k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3227
3.34k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3228
3.34k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3229
3.34k
    };
3230
3.34k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3231
3.34k
                             Params);
3232
3.34k
    // Mark this captured region as inlined, because we don't use outlined
3233
3.34k
    // function directly.
3234
3.34k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3235
3.34k
        AlwaysInlineAttr::CreateImplicit(
3236
3.34k
            Context, AlwaysInlineAttr::Keyword_forceinline));
3237
3.34k
    break;
3238
3.34k
  }
3239
3.34k
  case OMPD_threadprivate:
3240
0
  case OMPD_allocate:
3241
0
  case OMPD_taskyield:
3242
0
  case OMPD_barrier:
3243
0
  case OMPD_taskwait:
3244
0
  case OMPD_cancellation_point:
3245
0
  case OMPD_cancel:
3246
0
  case OMPD_flush:
3247
0
  case OMPD_declare_reduction:
3248
0
  case OMPD_declare_mapper:
3249
0
  case OMPD_declare_simd:
3250
0
  case OMPD_declare_target:
3251
0
  case OMPD_end_declare_target:
3252
0
  case OMPD_requires:
3253
0
    llvm_unreachable("OpenMP Directive is not allowed");
3254
0
  case OMPD_unknown:
3255
0
    llvm_unreachable("Unknown OpenMP directive");
3256
168k
  }
3257
168k
}
3258
3259
208k
int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
3260
208k
  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3261
208k
  getOpenMPCaptureRegions(CaptureRegions, DKind);
3262
208k
  return CaptureRegions.size();
3263
208k
}
3264
3265
static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
3266
                                             Expr *CaptureExpr, bool WithInit,
3267
36.2k
                                             bool AsExpression) {
3268
36.2k
  assert(CaptureExpr);
3269
36.2k
  ASTContext &C = S.getASTContext();
3270
36.2k
  Expr *Init = AsExpression ? 
CaptureExpr34.4k
:
CaptureExpr->IgnoreImpCasts()1.79k
;
3271
36.2k
  QualType Ty = Init->getType();
3272
36.2k
  if (CaptureExpr->getObjectKind() == OK_Ordinary && 
CaptureExpr->isGLValue()36.1k
) {
3273
2.34k
    if (S.getLangOpts().CPlusPlus) {
3274
2.34k
      Ty = C.getLValueReferenceType(Ty);
3275
2.34k
    } else {
3276
0
      Ty = C.getPointerType(Ty);
3277
0
      ExprResult Res =
3278
0
          S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
3279
0
      if (!Res.isUsable())
3280
0
        return nullptr;
3281
0
      Init = Res.get();
3282
0
    }
3283
2.34k
    WithInit = true;
3284
2.34k
  }
3285
36.2k
  auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
3286
36.2k
                                          CaptureExpr->getBeginLoc());
3287
36.2k
  if (!WithInit)
3288
70
    CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
3289
36.2k
  S.CurContext->addHiddenDecl(CED);
3290
36.2k
  S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
3291
36.2k
  return CED;
3292
36.2k
}
3293
3294
static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3295
1.97k
                                 bool WithInit) {
3296
1.97k
  OMPCapturedExprDecl *CD;
3297
1.97k
  if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
3298
178
    CD = cast<OMPCapturedExprDecl>(VD);
3299
1.79k
  else
3300
1.79k
    CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
3301
1.79k
                          /*AsExpression=*/false);
3302
1.97k
  return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3303
1.97k
                          CaptureExpr->getExprLoc());
3304
1.97k
}
3305
3306
53.5k
static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
3307
53.5k
  CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
3308
53.5k
  if (!Ref) {
3309
34.4k
    OMPCapturedExprDecl *CD = buildCaptureDecl(
3310
34.4k
        S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
3311
34.4k
        /*WithInit=*/true, /*AsExpression=*/true);
3312
34.4k
    Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3313
34.4k
                           CaptureExpr->getExprLoc());
3314
34.4k
  }
3315
53.5k
  ExprResult Res = Ref;
3316
53.5k
  if (!S.getLangOpts().CPlusPlus &&
3317
53.5k
      
CaptureExpr->getObjectKind() == OK_Ordinary745
&&
CaptureExpr->isGLValue()745
&&
3318
53.5k
      
Ref->getType()->isPointerType()0
) {
3319
0
    Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
3320
0
    if (!Res.isUsable())
3321
0
      return ExprError();
3322
53.5k
  }
3323
53.5k
  return S.DefaultLvalueConversion(Res.get());
3324
53.5k
}
3325
3326
namespace {
3327
// OpenMP directives parsed in this section are represented as a
3328
// CapturedStatement with an associated statement.  If a syntax error
3329
// is detected during the parsing of the associated statement, the
3330
// compiler must abort processing and close the CapturedStatement.
3331
//
3332
// Combined directives such as 'target parallel' have more than one
3333
// nested CapturedStatements.  This RAII ensures that we unwind out
3334
// of all the nested CapturedStatements when an error is found.
3335
class CaptureRegionUnwinderRAII {
3336
private:
3337
  Sema &S;
3338
  bool &ErrorFound;
3339
  OpenMPDirectiveKind DKind = OMPD_unknown;
3340
3341
public:
3342
  CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
3343
                            OpenMPDirectiveKind DKind)
3344
168k
      : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
3345
168k
  ~CaptureRegionUnwinderRAII() {
3346
168k
    if (ErrorFound) {
3347
6.94k
      int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
3348
17.3k
      while (--ThisCaptureLevel >= 0)
3349
10.4k
        S.ActOnCapturedRegionError();
3350
6.94k
    }
3351
168k
  }
3352
};
3353
} // namespace
3354
3355
668k
void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
3356
668k
  // Capture variables captured by reference in lambdas for target-based
3357
668k
  // directives.
3358
668k
  if (!CurContext->isDependentContext() &&
3359
668k
      (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
3360
668k
       isOpenMPTargetDataManagementDirective(
3361
374k
           DSAStack->getCurrentDirective()))) {
3362
303k
    QualType Type = V->getType();
3363
303k
    if (const auto *RD = Type.getCanonicalType()
3364
20.6k
                             .getNonReferenceType()
3365
20.6k
                             ->getAsCXXRecordDecl()) {
3366
20.6k
      bool SavedForceCaptureByReferenceInTargetExecutable =
3367
20.6k
          DSAStack->isForceCaptureByReferenceInTargetExecutable();
3368
20.6k
      DSAStack->setForceCaptureByReferenceInTargetExecutable(
3369
20.6k
          /*V=*/true);
3370
20.6k
      if (RD->isLambda()) {
3371
116
        llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
3372
116
        FieldDecl *ThisCapture;
3373
116
        RD->getCaptureFields(Captures, ThisCapture);
3374
268
        for (const LambdaCapture &LC : RD->captures()) {
3375
268
          if (LC.getCaptureKind() == LCK_ByRef) {
3376
140
            VarDecl *VD = LC.getCapturedVar();
3377
140
            DeclContext *VDC = VD->getDeclContext();
3378
140
            if (!VDC->Encloses(CurContext))
3379
0
              continue;
3380
140
            MarkVariableReferenced(LC.getLocation(), VD);
3381
140
          } else 
if (128
LC.getCaptureKind() == LCK_This128
) {
3382
48
            QualType ThisTy = getCurrentThisType();
3383
48
            if (!ThisTy.isNull() &&
3384
48
                
Context.typesAreCompatible(ThisTy, ThisCapture->getType())28
)
3385
28
              CheckCXXThisCapture(LC.getLocation());
3386
48
          }
3387
268
        }
3388
116
      }
3389
20.6k
      DSAStack->setForceCaptureByReferenceInTargetExecutable(
3390
20.6k
          SavedForceCaptureByReferenceInTargetExecutable);
3391
20.6k
    }
3392
303k
  }
3393
668k
}
3394
3395
StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
3396
168k
                                      ArrayRef<OMPClause *> Clauses) {
3397
168k
  bool ErrorFound = false;
3398
168k
  CaptureRegionUnwinderRAII CaptureRegionUnwinder(
3399
168k
      *this, ErrorFound, DSAStack->getCurrentDirective());
3400
168k
  if (!S.isUsable()) {
3401
6.86k
    ErrorFound = true;
3402
6.86k
    return StmtError();
3403
6.86k
  }
3404
161k
3405
161k
  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3406
161k
  getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
3407
161k
  OMPOrderedClause *OC = nullptr;
3408
161k
  OMPScheduleClause *SC = nullptr;
3409
161k
  SmallVector<const OMPLinearClause *, 4> LCs;
3410
161k
  SmallVector<const OMPClauseWithPreInit *, 4> PICs;
3411
161k
  // This is required for proper codegen.
3412
161k
  for (OMPClause *Clause : Clauses) {
3413
77.0k
    if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
3414
77.0k
        
Clause->getClauseKind() == OMPC_in_reduction4.79k
) {
3415
834
      // Capture taskgroup task_reduction descriptors inside the tasking regions
3416
834
      // with the corresponding in_reduction items.
3417
834
      auto *IRC = cast<OMPInReductionClause>(Clause);
3418
834
      for (Expr *E : IRC->taskgroup_descriptors())
3419
912
        if (E)
3420
506
          MarkDeclarationsReferencedInExpr(E);
3421
834
    }
3422
77.0k
    if (isOpenMPPrivate(Clause->getClauseKind()) ||
3423
77.0k
        
Clause->getClauseKind() == OMPC_copyprivate47.9k
||
3424
77.0k
        
(47.7k
getLangOpts().OpenMPUseTLS47.7k
&&
3425
47.7k
         
getASTContext().getTargetInfo().isTLSSupported()24.6k
&&
3426
47.7k
         
Clause->getClauseKind() == OMPC_copyin23.2k
)) {
3427
29.3k
      DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
3428
29.3k
      // Mark all variables in private list clauses as used in inner region.
3429
42.6k
      for (Stmt *VarRef : Clause->children()) {
3430
42.6k
        if (auto *E = cast_or_null<Expr>(VarRef)) {
3431
42.6k
          MarkDeclarationsReferencedInExpr(E);
3432
42.6k
        }
3433
42.6k
      }
3434
29.3k
      DSAStack->setForceVarCapturing(/*V=*/false);
3435
47.6k
    } else if (CaptureRegions.size() > 1 ||
3436
47.6k
               
CaptureRegions.back() != OMPD_unknown23.2k
) {
3437
40.0k
      if (auto *C = OMPClauseWithPreInit::get(Clause))
3438
13.6k
        PICs.push_back(C);
3439
40.0k
      if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
3440
0
        if (Expr *E = C->getPostUpdateExpr())
3441
0
          MarkDeclarationsReferencedInExpr(E);
3442
0
      }
3443
40.0k
    }
3444
77.0k
    if (Clause->getClauseKind() == OMPC_schedule)
3445
2.82k
      SC = cast<OMPScheduleClause>(Clause);
3446
74.1k
    else if (Clause->getClauseKind() == OMPC_ordered)
3447
924
      OC = cast<OMPOrderedClause>(Clause);
3448
73.2k
    else if (Clause->getClauseKind() == OMPC_linear)
3449
1.86k
      LCs.push_back(cast<OMPLinearClause>(Clause));
3450
77.0k
  }
3451
161k
  // OpenMP, 2.7.1 Loop Construct, Restrictions
3452
161k
  // The nonmonotonic modifier cannot be specified if an ordered clause is
3453
161k
  // specified.
3454
161k
  if (SC &&
3455
161k
      
(2.82k
SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic2.82k
||
3456
2.82k
       SC->getSecondScheduleModifier() ==
3457
2.80k
           OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
3458
161k
      
OC24
) {
3459
4
    Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
3460
4
             ? SC->getFirstScheduleModifierLoc()
3461
4
             : 
SC->getSecondScheduleModifierLoc()0
,
3462
4
         diag::err_omp_schedule_nonmonotonic_ordered)
3463
4
        << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3464
4
    ErrorFound = true;
3465
4
  }
3466
161k
  if (!LCs.empty() && 
OC1.83k
&&
OC->getNumForLoops()24
) {
3467
4
    for (const OMPLinearClause *C : LCs) {
3468
4
      Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
3469
4
          << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
3470
4
    }
3471
4
    ErrorFound = true;
3472
4
  }
3473
161k
  if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
3474
161k
      
isOpenMPSimdDirective(55.3k
DSAStack55.3k
->getCurrentDirective()) &&
OC26.4k
&&
3475
161k
      
OC->getNumForLoops()170
) {
3476
72
    Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
3477
72
        << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3478
72
    ErrorFound = true;
3479
72
  }
3480
161k
  if (ErrorFound) {
3481
80
    return StmtError();
3482
80
  }
3483
161k
  StmtResult SR = S;
3484
161k
  unsigned CompletedRegions = 0;
3485
284k
  for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
3486
284k
    // Mark all variables in private list clauses as used in inner region.
3487
284k
    // Required for proper codegen of combined directives.
3488
284k
    // TODO: add processing for other clauses.
3489
284k
    if (ThisCaptureRegion != OMPD_unknown) {
3490
257k
      for (const clang::OMPClauseWithPreInit *C : PICs) {
3491
31.0k
        OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3492
31.0k
        // Find the particular capture region for the clause if the
3493
31.0k
        // directive is a combined one with multiple capture regions.
3494
31.0k
        // If the directive is not a combined one, the capture region
3495
31.0k
        // associated with the clause is OMPD_unknown and is generated
3496
31.0k
        // only once.
3497
31.0k
        if (CaptureRegion == ThisCaptureRegion ||
3498
31.0k
            
CaptureRegion == OMPD_unknown26.7k
) {
3499
23.8k
          if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
3500
4.28k
            for (Decl *D : DS->decls())
3501
4.28k
              MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3502
4.28k
          }
3503
23.8k
        }
3504
31.0k
      }
3505
257k
    }
3506
284k
    if (++CompletedRegions == CaptureRegions.size())
3507
284k
      
DSAStack161k
->setBodyComplete()161k
;
3508
284k
    SR = ActOnCapturedRegionEnd(SR.get());
3509
284k
  }
3510
161k
  return SR;
3511
161k
}
3512
3513
static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3514
                              OpenMPDirectiveKind CancelRegion,
3515
159k
                              SourceLocation StartLoc) {
3516
159k
  // CancelRegion is only needed for cancel and cancellation_point.
3517
159k
  if (CurrentRegion != OMPD_cancel && 
CurrentRegion != OMPD_cancellation_point159k
)
3518
158k
    return false;
3519
481
3520
481
  if (CancelRegion == OMPD_parallel || 
CancelRegion == OMPD_for323
||
3521
481
      
CancelRegion == OMPD_sections160
||
CancelRegion == OMPD_taskgroup64
)
3522
465
    return false;
3523
16
3524
16
  SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3525
16
      << getOpenMPDirectiveName(CancelRegion);
3526
16
  return true;
3527
16
}
3528
3529
static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
3530
                                  OpenMPDirectiveKind CurrentRegion,
3531
                                  const DeclarationNameInfo &CurrentName,
3532
                                  OpenMPDirectiveKind CancelRegion,
3533
159k
                                  SourceLocation StartLoc) {
3534
159k
  if (Stack->getCurScope()) {
3535
113k
    OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3536
113k
    OpenMPDirectiveKind OffendingRegion = ParentRegion;
3537
113k
    bool NestingProhibited = false;
3538
113k
    bool CloseNesting = true;
3539
113k
    bool OrphanSeen = false;
3540
113k
    enum {
3541
113k
      NoRecommend,
3542
113k
      ShouldBeInParallelRegion,
3543
113k
      ShouldBeInOrderedRegion,
3544
113k
      ShouldBeInTargetRegion,
3545
113k
      ShouldBeInTeamsRegion
3546
113k
    } Recommend = NoRecommend;
3547
113k
    if (isOpenMPSimdDirective(ParentRegion) && 
CurrentRegion != OMPD_ordered1.67k
) {
3548
1.50k
      // OpenMP [2.16, Nesting of Regions]
3549
1.50k
      // OpenMP constructs may not be nested inside a simd region.
3550
1.50k
      // OpenMP [2.8.1,simd Construct, Restrictions]
3551
1.50k
      // An ordered construct with the simd clause is the only OpenMP
3552
1.50k
      // construct that can appear in the simd region.
3553
1.50k
      // Allowing a SIMD construct nested in another SIMD construct is an
3554
1.50k
      // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3555
1.50k
      // message.
3556
1.50k
      SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3557
1.50k
                                 ? 
diag::err_omp_prohibited_region_simd1.46k
3558
1.50k
                                 : 
diag::warn_omp_nesting_simd38
);
3559
1.50k
      return CurrentRegion != OMPD_simd;
3560
1.50k
    }
3561
111k
    if (ParentRegion == OMPD_atomic) {
3562
166
      // OpenMP [2.16, Nesting of Regions]
3563
166
      // OpenMP constructs may not be nested inside an atomic region.
3564
166
      SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3565
166
      return true;
3566
166
    }
3567
111k
    if (CurrentRegion == OMPD_section) {
3568
371
      // OpenMP [2.7.2, sections Construct, Restrictions]
3569
371
      // Orphaned section directives are prohibited. That is, the section
3570
371
      // directives must appear within the sections construct and must not be
3571
371
      // encountered elsewhere in the sections region.
3572
371
      if (ParentRegion != OMPD_sections &&
3573
371
          
ParentRegion != OMPD_parallel_sections120
) {
3574
74
        SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3575
74
            << (ParentRegion != OMPD_unknown)
3576
74
            << getOpenMPDirectiveName(ParentRegion);
3577
74
        return true;
3578
74
      }
3579
297
      return false;
3580
297
    }
3581
111k
    // Allow some constructs (except teams and cancellation constructs) to be
3582
111k
    // orphaned (they could be used in functions, called from OpenMP regions
3583
111k
    // with the required preconditions).
3584
111k
    if (ParentRegion == OMPD_unknown &&
3585
111k
        
!isOpenMPNestingTeamsDirective(CurrentRegion)73.4k
&&
3586
111k
        
CurrentRegion != OMPD_cancellation_point73.4k
&&
3587
111k
        
CurrentRegion != OMPD_cancel73.3k
)
3588
73.3k
      return false;
3589
38.0k
    if (CurrentRegion == OMPD_cancellation_point ||
3590
38.0k
        
CurrentRegion == OMPD_cancel37.9k
) {
3591
425
      // OpenMP [2.16, Nesting of Regions]
3592
425
      // A cancellation point construct for which construct-type-clause is
3593
425
      // taskgroup must be nested inside a task construct. A cancellation
3594
425
      // point construct for which construct-type-clause is not taskgroup must
3595
425
      // be closely nested inside an OpenMP construct that matches the type
3596
425
      // specified in construct-type-clause.
3597
425
      // A cancel construct for which construct-type-clause is taskgroup must be
3598
425
      // nested inside a task construct. A cancel construct for which
3599
425
      // construct-type-clause is not taskgroup must be closely nested inside an
3600
425
      // OpenMP construct that matches the type specified in
3601
425
      // construct-type-clause.
3602
425
      NestingProhibited =
3603
425
          !((CancelRegion == OMPD_parallel &&
3604
425
             
(130
ParentRegion == OMPD_parallel130
||
3605
130
              
ParentRegion == OMPD_target_parallel28
)) ||
3606
425
            
(323
CancelRegion == OMPD_for323
&&
3607
323
             
(151
ParentRegion == OMPD_for151
||
ParentRegion == OMPD_parallel_for129
||
3608
151
              
ParentRegion == OMPD_target_parallel_for117
||
3609
151
              
ParentRegion == OMPD_distribute_parallel_for77
||
3610
151
              
ParentRegion == OMPD_teams_distribute_parallel_for59
||
3611
151
              
ParentRegion == OMPD_target_teams_distribute_parallel_for49
)) ||
3612
425
            
(196
CancelRegion == OMPD_taskgroup196
&&
ParentRegion == OMPD_task48
) ||
3613
425
            
(172
CancelRegion == OMPD_sections172
&&
3614
172
             
(96
ParentRegion == OMPD_section96
||
ParentRegion == OMPD_sections76
||
3615
96
              
ParentRegion == OMPD_parallel_sections48
)));
3616
425
      OrphanSeen = ParentRegion == OMPD_unknown;
3617
37.6k
    } else if (CurrentRegion == OMPD_master) {
3618
130
      // OpenMP [2.16, Nesting of Regions]
3619
130
      // A master region may not be closely nested inside a worksharing,
3620
130
      // atomic, or explicit task region.
3621
130
      NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3622
130
                          
isOpenMPTaskingDirective(ParentRegion)82
;
3623
37.5k
    } else if (CurrentRegion == OMPD_critical && 
CurrentName.getName()177
) {
3624
36
      // OpenMP [2.16, Nesting of Regions]
3625
36
      // A critical region may not be nested (closely or otherwise) inside a
3626
36
      // critical region with the same name. Note that this restriction is not
3627
36
      // sufficient to prevent deadlock.
3628
36
      SourceLocation PreviousCriticalLoc;
3629
36
      bool DeadLock = Stack->hasDirective(
3630
36
          [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3631
36
                                              const DeclarationNameInfo &DNI,
3632
104
                                              SourceLocation Loc) {
3633
104
            if (K == OMPD_critical && 
DNI.getName() == CurrentName.getName()78
) {
3634
10
              PreviousCriticalLoc = Loc;
3635
10
              return true;
3636
10
            }
3637
94
            return false;
3638
94
          },
3639
36
          false /* skip top directive */);
3640
36
      if (DeadLock) {
3641
10
        SemaRef.Diag(StartLoc,
3642
10
                     diag::err_omp_prohibited_region_critical_same_name)
3643
10
            << CurrentName.getName();
3644
10
        if (PreviousCriticalLoc.isValid())
3645
10
          SemaRef.Diag(PreviousCriticalLoc,
3646
10
                       diag::note_omp_previous_critical_region);
3647
10
        return true;
3648
10
      }
3649
37.5k
    } else if (CurrentRegion == OMPD_barrier) {
3650
101
      // OpenMP [2.16, Nesting of Regions]
3651
101
      // A barrier region may not be closely nested inside a worksharing,
3652
101
      // explicit task, critical, ordered, atomic, or master region.
3653
101
      NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3654
101
                          
isOpenMPTaskingDirective(ParentRegion)61
||
3655
101
                          
ParentRegion == OMPD_master53
||
3656
101
                          
ParentRegion == OMPD_critical49
||
3657
101
                          
ParentRegion == OMPD_ordered45
;
3658
37.4k
    } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
3659
37.4k
               
!isOpenMPParallelDirective(CurrentRegion)15.0k
&&
3660
37.4k
               
!isOpenMPTeamsDirective(CurrentRegion)4.06k
) {
3661
4.06k
      // OpenMP [2.16, Nesting of Regions]
3662
4.06k
      // A worksharing region may not be closely nested inside a worksharing,
3663
4.06k
      // explicit task, critical, ordered, atomic, or master region.
3664
4.06k
      NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3665
4.06k
                          
isOpenMPTaskingDirective(ParentRegion)3.90k
||
3666
4.06k
                          
ParentRegion == OMPD_master3.86k
||
3667
4.06k
                          
ParentRegion == OMPD_critical3.85k
||
3668
4.06k
                          
ParentRegion == OMPD_ordered3.83k
;
3669
4.06k
      Recommend = ShouldBeInParallelRegion;
3670
33.3k
    } else if (CurrentRegion == OMPD_ordered) {
3671
720
      // OpenMP [2.16, Nesting of Regions]
3672
720
      // An ordered region may not be closely nested inside a critical,
3673
720
      // atomic, or explicit task region.
3674
720
      // An ordered region must be closely nested inside a loop region (or
3675
720
      // parallel loop region) with an ordered clause.
3676
720
      // OpenMP [2.8.1,simd Construct, Restrictions]
3677
720
      // An ordered construct with the simd clause is the only OpenMP construct
3678
720
      // that can appear in the simd region.
3679
720
      NestingProhibited = ParentRegion == OMPD_critical ||
3680
720
                          
isOpenMPTaskingDirective(ParentRegion)716
||
3681
720
                          
!(708
isOpenMPSimdDirective(ParentRegion)708
||
3682
708
                            
Stack->isParentOrderedRegion()538
);
3683
720
      Recommend = ShouldBeInOrderedRegion;
3684
32.6k
    } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
3685
16.7k
      // OpenMP [2.16, Nesting of Regions]
3686
16.7k
      // If specified, a teams construct must be contained within a target
3687
16.7k
      // construct.
3688
16.7k
      NestingProhibited = ParentRegion != OMPD_target;
3689
16.7k
      OrphanSeen = ParentRegion == OMPD_unknown;
3690
16.7k
      Recommend = ShouldBeInTargetRegion;
3691
16.7k
    }
3692
38.0k
    
if (38.0k
!NestingProhibited38.0k
&&
3693
38.0k
        
!isOpenMPTargetExecutionDirective(CurrentRegion)37.0k
&&
3694
38.0k
        
!isOpenMPTargetDataManagementDirective(CurrentRegion)34.9k
&&
3695
38.0k
        
(34.3k
ParentRegion == OMPD_teams34.3k
||
ParentRegion == OMPD_target_teams27.1k
)) {
3696
7.55k
      // OpenMP [2.16, Nesting of Regions]
3697
7.55k
      // distribute, parallel, parallel sections, parallel workshare, and the
3698
7.55k
      // parallel loop and parallel loop SIMD constructs are the only OpenMP
3699
7.55k
      // constructs that can be closely nested in the teams region.
3700
7.55k
      NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3701
7.55k
                          
!isOpenMPDistributeDirective(CurrentRegion)2.58k
;
3702
7.55k
      Recommend = ShouldBeInParallelRegion;
3703
7.55k
    }
3704
38.0k
    if (!NestingProhibited &&
3705
38.0k
        
isOpenMPNestingDistributeDirective(CurrentRegion)36.9k
) {
3706
7.45k
      // OpenMP 4.5 [2.17 Nesting of Regions]
3707
7.45k
      // The region associated with the distribute construct must be strictly
3708
7.45k
      // nested inside a teams region
3709
7.45k
      NestingProhibited =
3710
7.45k
          (ParentRegion != OMPD_teams && 
ParentRegion != OMPD_target_teams493
);
3711
7.45k
      Recommend = ShouldBeInTeamsRegion;
3712
7.45k
    }
3713
38.0k
    if (!NestingProhibited &&
3714
38.0k
        
(36.6k
isOpenMPTargetExecutionDirective(CurrentRegion)36.6k
||
3715
36.6k
         
isOpenMPTargetDataManagementDirective(CurrentRegion)34.4k
)) {
3716
2.70k
      // OpenMP 4.5 [2.17 Nesting of Regions]
3717
2.70k
      // If a target, target update, target data, target enter data, or
3718
2.70k
      // target exit data construct is encountered during execution of a
3719
2.70k
      // target region, the behavior is unspecified.
3720
2.70k
      NestingProhibited = Stack->hasDirective(
3721
2.70k
          [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3722
4.60k
                             SourceLocation) {
3723
4.60k
            if (isOpenMPTargetExecutionDirective(K)) {
3724
548
              OffendingRegion = K;
3725
548
              return true;
3726
548
            }
3727
4.05k
            return false;
3728
4.05k
          },
3729
2.70k
          false /* don't skip top directive */);
3730
2.70k
      CloseNesting = false;
3731
2.70k
    }
3732
38.0k
    if (NestingProhibited) {
3733
1.96k
      if (OrphanSeen) {
3734
100
        SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3735
100
            << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3736
1.86k
      } else {
3737
1.86k
        SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3738
1.86k
            << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3739
1.86k
            << Recommend << getOpenMPDirectiveName(CurrentRegion);
3740
1.86k
      }
3741
1.96k
      return true;
3742
1.96k
    }
3743
82.0k
  }
3744
82.0k
  return false;
3745
82.0k
}
3746
3747
static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3748
                           ArrayRef<OMPClause *> Clauses,
3749
113k
                           ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3750
113k
  bool ErrorFound = false;
3751
113k
  unsigned NamedModifiersNumber = 0;
3752
113k
  SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3753
113k
      OMPD_unknown + 1);
3754
113k
  SmallVector<SourceLocation, 4> NameModifierLoc;
3755
113k
  for (const OMPClause *C : Clauses) {
3756
59.6k
    if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3757
6.02k
      // At most one if clause without a directive-name-modifier can appear on
3758
6.02k
      // the directive.
3759
6.02k
      OpenMPDirectiveKind CurNM = IC->getNameModifier();
3760
6.02k
      if (FoundNameModifiers[CurNM]) {
3761
200
        S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
3762
200
            << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3763
200
            << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3764
200
        ErrorFound = true;
3765
5.82k
      } else if (CurNM != OMPD_unknown) {
3766
2.96k
        NameModifierLoc.push_back(IC->getNameModifierLoc());
3767
2.96k
        ++NamedModifiersNumber;
3768
2.96k
      }
3769
6.02k
      FoundNameModifiers[CurNM] = IC;
3770
6.02k
      if (CurNM == OMPD_unknown)
3771
2.94k
        continue;
3772
3.07k
      // Check if the specified name modifier is allowed for the current
3773
3.07k
      // directive.
3774
3.07k
      // At most one if clause with the particular directive-name-modifier can
3775
3.07k
      // appear on the directive.
3776
3.07k
      bool MatchFound = false;
3777
3.51k
      for (auto NM : AllowedNameModifiers) {
3778
3.51k
        if (CurNM == NM) {
3779
2.93k
          MatchFound = true;
3780
2.93k
          break;
3781
2.93k
        }
3782
3.51k
      }
3783
3.07k
      if (!MatchFound) {
3784
142
        S.Diag(IC->getNameModifierLoc(),
3785
142
               diag::err_omp_wrong_if_directive_name_modifier)
3786
142
            << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3787
142
        ErrorFound = true;
3788
142
      }
3789
3.07k
    }
3790
59.6k
  }
3791
113k
  // If any if clause on the directive includes a directive-name-modifier then
3792
113k
  // all if clauses on the directive must include a directive-name-modifier.
3793
113k
  if (FoundNameModifiers[OMPD_unknown] && 
NamedModifiersNumber > 02.85k
) {
3794
120
    if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3795
90
      S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
3796
90
             diag::err_omp_no_more_if_clause);
3797
90
    } else {
3798
30
      std::string Values;
3799
30
      std::string Sep(", ");
3800
30
      unsigned AllowedCnt = 0;
3801
30
      unsigned TotalAllowedNum =
3802
30
          AllowedNameModifiers.size() - NamedModifiersNumber;
3803
88
      for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3804
58
           ++Cnt) {
3805
58
        OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3806
58
        if (!FoundNameModifiers[NM]) {
3807
28
          Values += "'";
3808
28
          Values += getOpenMPDirectiveName(NM);
3809
28
          Values += "'";
3810
28
          if (AllowedCnt + 2 == TotalAllowedNum)
3811
0
            Values += " or ";
3812
28
          else if (AllowedCnt + 1 != TotalAllowedNum)
3813
0
            Values += Sep;
3814
28
          ++AllowedCnt;
3815
28
        }
3816
58
      }
3817
30
      S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
3818
30
             diag::err_omp_unnamed_if_clause)
3819
30
          << (TotalAllowedNum > 1) << Values;
3820
30
    }
3821
134
    for (SourceLocation Loc : NameModifierLoc) {
3822
134
      S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3823
134
    }
3824
120
    ErrorFound = true;
3825
120
  }
3826
113k
  return ErrorFound;
3827
113k
}
3828
3829
static std::pair<ValueDecl *, bool>
3830
getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
3831
163k
               SourceRange &ERange, bool AllowArraySection = false) {
3832
163k
  if (RefExpr->isTypeDependent() || 
RefExpr->isValueDependent()151k
||
3833
163k
      
RefExpr->containsUnexpandedParameterPack()151k
)
3834
12.5k
    return std::make_pair(nullptr, true);
3835
151k
3836
151k
  // OpenMP [3.1, C/C++]
3837
151k
  //  A list item is a variable name.
3838
151k
  // OpenMP  [2.9.3.3, Restrictions, p.1]
3839
151k
  //  A variable that is part of another variable (as an array or
3840
151k
  //  structure element) cannot appear in a private clause.
3841
151k
  RefExpr = RefExpr->IgnoreParens();
3842
151k
  enum {
3843
151k
    NoArrayExpr = -1,
3844
151k
    ArraySubscript = 0,
3845
151k
    OMPArraySection = 1
3846
151k
  } IsArrayExpr = NoArrayExpr;
3847
151k
  if (AllowArraySection) {
3848
84.0k
    if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
3849
246
      Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
3850
246
      while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
3851
0
        Base = TempASE->getBase()->IgnoreParenImpCasts();
3852
246
      RefExpr = Base;
3853
246
      IsArrayExpr = ArraySubscript;
3854
83.8k
    } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
3855
618
      Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
3856
858
      while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
3857
240
        Base = TempOASE->getBase()->IgnoreParenImpCasts();
3858
658
      while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
3859
40
        Base = TempASE->getBase()->IgnoreParenImpCasts();
3860
618
      RefExpr = Base;
3861
618
      IsArrayExpr = OMPArraySection;
3862
618
    }
3863
84.0k
  }
3864
151k
  ELoc = RefExpr->getExprLoc();
3865
151k
  ERange = RefExpr->getSourceRange();
3866
151k
  RefExpr = RefExpr->IgnoreParenImpCasts();
3867
151k
  auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3868
151k
  auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
3869
151k
  if ((!DE || 
!isa<VarDecl>(DE->getDecl())146k
) &&
3870
151k
      
(4.55k
S.getCurrentThisType().isNull()4.55k
||
!ME2.47k
||
3871
4.55k
       
!isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts())2.47k
||
3872
4.55k
       
!isa<FieldDecl>(ME->getMemberDecl())2.32k
)) {
3873
2.23k
    if (IsArrayExpr != NoArrayExpr) {
3874
12
      S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
3875
12
                                                         << ERange;
3876
2.21k
    } else {
3877
2.21k
      S.Diag(ELoc,
3878
2.21k
             AllowArraySection
3879
2.21k
                 ? 
diag::err_omp_expected_var_name_member_expr_or_array_item956
3880
2.21k
                 : 
diag::err_omp_expected_var_name_member_expr1.26k
)
3881
2.21k
          << (S.getCurrentThisType().isNull() ? 
02.07k
:
1144
) << ERange;
3882
2.21k
    }
3883
2.23k
    return std::make_pair(nullptr, false);
3884
2.23k
  }
3885
148k
  return std::make_pair(
3886
148k
      getCanonicalDecl(DE ? 
DE->getDecl()146k
:
ME->getMemberDecl()2.32k
), false);
3887
148k
}
3888
3889
static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
3890
107k
                                 ArrayRef<OMPClause *> Clauses) {
3891
107k
  assert(!S.CurContext->isDependentContext() &&
3892
107k
         "Expected non-dependent context.");
3893
107k
  auto AllocateRange =
3894
107k
      llvm::make_filter_range(Clauses, OMPAllocateClause::classof);
3895
107k
  llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>>
3896
107k
      DeclToCopy;
3897
107k
  auto PrivateRange = llvm::make_filter_range(Clauses, [](const OMPClause *C) {
3898
81.5k
    return isOpenMPPrivate(C->getClauseKind());
3899
81.5k
  });
3900
107k
  for (OMPClause *Cl : PrivateRange) {
3901
43.6k
    MutableArrayRef<Expr *>::iterator I, It, Et;
3902
43.6k
    if (Cl->getClauseKind() == OMPC_private) {
3903
4.73k
      auto *PC = cast<OMPPrivateClause>(Cl);
3904
4.73k
      I = PC->private_copies().begin();
3905
4.73k
      It = PC->varlist_begin();
3906
4.73k
      Et = PC->varlist_end();
3907
38.8k
    } else if (Cl->getClauseKind() == OMPC_firstprivate) {
3908
28.4k
      auto *PC = cast<OMPFirstprivateClause>(Cl);
3909
28.4k
      I = PC->private_copies().begin();
3910
28.4k
      It = PC->varlist_begin();
3911
28.4k
      Et = PC->varlist_end();
3912
28.4k
    } else 
if (10.3k
Cl->getClauseKind() == OMPC_lastprivate10.3k
) {
3913
2.03k
      auto *PC = cast<OMPLastprivateClause>(Cl);
3914
2.03k
      I = PC->private_copies().begin();
3915
2.03k
      It = PC->varlist_begin();
3916
2.03k
      Et = PC->varlist_end();
3917
8.34k
    } else if (Cl->getClauseKind() == OMPC_linear) {
3918
1.16k
      auto *PC = cast<OMPLinearClause>(Cl);
3919
1.16k
      I = PC->privates().begin();
3920
1.16k
      It = PC->varlist_begin();
3921
1.16k
      Et = PC->varlist_end();
3922
7.18k
    } else if (Cl->getClauseKind() == OMPC_reduction) {
3923
5.80k
      auto *PC = cast<OMPReductionClause>(Cl);
3924
5.80k
      I = PC->privates().begin();
3925
5.80k
      It = PC->varlist_begin();
3926
5.80k
      Et = PC->varlist_end();
3927
5.80k
    } else 
if (1.38k
Cl->getClauseKind() == OMPC_task_reduction1.38k
) {
3928
924
      auto *PC = cast<OMPTaskReductionClause>(Cl);
3929
924
      I = PC->privates().begin();
3930
924
      It = PC->varlist_begin();
3931
924
      Et = PC->varlist_end();
3932
924
    } else 
if (464
Cl->getClauseKind() == OMPC_in_reduction464
) {
3933
464
      auto *PC = cast<OMPInReductionClause>(Cl);
3934
464
      I = PC->privates().begin();
3935
464
      It = PC->varlist_begin();
3936
464
      Et = PC->varlist_end();
3937
464
    } else {
3938
0
      llvm_unreachable("Expected private clause.");
3939
0
    }
3940
59.4k
    
for (Expr *E : llvm::make_range(It, Et))43.6k
{
3941
59.4k
      if (!*I) {
3942
338
        ++I;
3943
338
        continue;
3944
338
      }
3945
59.0k
      SourceLocation ELoc;
3946
59.0k
      SourceRange ERange;
3947
59.0k
      Expr *SimpleRefExpr = E;
3948
59.0k
      auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
3949
59.0k
                                /*AllowArraySection=*/true);
3950
59.0k
      DeclToCopy.try_emplace(Res.first,
3951
59.0k
                             cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()));
3952
59.0k
      ++I;
3953
59.0k
    }
3954
43.6k
  }
3955
107k
  for (OMPClause *C : AllocateRange) {
3956
551
    auto *AC = cast<OMPAllocateClause>(C);
3957
551
    OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3958
551
        getAllocatorKind(S, Stack, AC->getAllocator());
3959
551
    // OpenMP, 2.11.4 allocate Clause, Restrictions.
3960
551
    // For task, taskloop or target directives, allocation requests to memory
3961
551
    // allocators with the trait access set to thread result in unspecified
3962
551
    // behavior.
3963
551
    if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
3964
551
        
(236
isOpenMPTaskingDirective(Stack->getCurrentDirective())236
||
3965
236
         
isOpenMPTargetExecutionDirective(Stack->getCurrentDirective())164
)) {
3966
228
      S.Diag(AC->getAllocator()->getExprLoc(),
3967
228
             diag::warn_omp_allocate_thread_on_task_target_directive)
3968
228
          << getOpenMPDirectiveName(Stack->getCurrentDirective());
3969
228
    }
3970
551
    for (Expr *E : AC->varlists()) {
3971
551
      SourceLocation ELoc;
3972
551
      SourceRange ERange;
3973
551
      Expr *SimpleRefExpr = E;
3974
551
      auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange);
3975
551
      ValueDecl *VD = Res.first;
3976
551
      DSAStackTy::DSAVarData Data = Stack->getTopDSA(VD, /*FromParent=*/false);
3977
551
      if (!isOpenMPPrivate(Data.CKind)) {
3978
4
        S.Diag(E->getExprLoc(),
3979
4
               diag::err_omp_expected_private_copy_for_allocate);
3980
4
        continue;
3981
4
      }
3982
547
      VarDecl *PrivateVD = DeclToCopy[VD];
3983
547
      if (checkPreviousOMPAllocateAttribute(S, Stack, E, PrivateVD,
3984
547
                                            AllocatorKind, AC->getAllocator()))
3985
0
        continue;
3986
547
      applyOMPAllocateAttribute(S, PrivateVD, AllocatorKind, AC->getAllocator(),
3987
547
                                E->getSourceRange());
3988
547
    }
3989
551
  }
3990
107k
}
3991
3992
StmtResult Sema::ActOnOpenMPExecutableDirective(
3993
    OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3994
    OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3995
159k
    Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3996
159k
  StmtResult Res = StmtError();
3997
159k
  // First check CancelRegion which is then used in checkNestingOfRegions.
3998
159k
  if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3999
159k
      
checkNestingOfRegions(*this, 159k
DSAStack159k
, Kind, DirName, CancelRegion,
4000
159k
                            StartLoc))
4001
3.70k
    return StmtError();
4002
155k
4003
155k
  llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
4004
155k
  VarsWithInheritedDSAType VarsWithInheritedDSA;
4005
155k
  bool ErrorFound = false;
4006
155k
  ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
4007
155k
  if (AStmt && 
!CurContext->isDependentContext()151k
) {
4008
111k
    assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4009
111k
4010
111k
    // Check default data sharing attributes for referenced variables.
4011
111k
    DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
4012
111k
    int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
4013
111k
    Stmt *S = AStmt;
4014
308k
    while (--ThisCaptureLevel >= 0)
4015
197k
      S = cast<CapturedStmt>(S)->getCapturedStmt();
4016
111k
    DSAChecker.Visit(S);
4017
111k
    if (!isOpenMPTargetDataManagementDirective(Kind) &&
4018
111k
        
!isOpenMPTaskingDirective(Kind)107k
) {
4019
101k
      // Visit subcaptures to generate implicit clauses for captured vars.
4020
101k
      auto *CS = cast<CapturedStmt>(AStmt);
4021
101k
      SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4022
101k
      getOpenMPCaptureRegions(CaptureRegions, Kind);
4023
101k
      // Ignore outer tasking regions for target directives.
4024
101k
      if (CaptureRegions.size() > 1 && 
CaptureRegions.front() == OMPD_task53.6k
)
4025
49.1k
        CS = cast<CapturedStmt>(CS->getCapturedStmt());
4026
101k
      DSAChecker.visitSubCaptures(CS);
4027
101k
    }
4028
111k
    if (DSAChecker.isErrorFound())
4029
28
      return StmtError();
4030
111k
    // Generate list of implicitly defined firstprivate variables.
4031
111k
    VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
4032
111k
4033
111k
    SmallVector<Expr *, 4> ImplicitFirstprivates(
4034
111k
        DSAChecker.getImplicitFirstprivate().begin(),
4035
111k
        DSAChecker.getImplicitFirstprivate().end());
4036
111k
    SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
4037
111k
                                        DSAChecker.getImplicitMap().end());
4038
111k
    // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
4039
111k
    for (OMPClause *C : Clauses) {
4040
53.4k
      if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
4041
464
        for (Expr *E : IRC->taskgroup_descriptors())
4042
470
          if (E)
4043
470
            ImplicitFirstprivates.emplace_back(E);
4044
464
      }
4045
53.4k
    }
4046
111k
    if (!ImplicitFirstprivates.empty()) {
4047
25.9k
      if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
4048
25.9k
              ImplicitFirstprivates, SourceLocation(), SourceLocation(),
4049
25.9k
              SourceLocation())) {
4050
25.9k
        ClausesWithImplicit.push_back(Implicit);
4051
25.9k
        ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
4052
25.9k
                     ImplicitFirstprivates.size();
4053
25.9k
      } else {
4054
4
        ErrorFound = true;
4055
4
      }
4056
25.9k
    }
4057
111k
    if (!ImplicitMaps.empty()) {
4058
5.57k
      CXXScopeSpec MapperIdScopeSpec;
4059
5.57k
      DeclarationNameInfo MapperId;
4060
5.57k
      if (OMPClause *Implicit = ActOnOpenMPMapClause(
4061
5.57k
              llvm::None, llvm::None, MapperIdScopeSpec, MapperId,
4062
5.57k
              OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(),
4063
5.57k
              SourceLocation(), ImplicitMaps, OMPVarListLocTy())) {
4064
5.57k
        ClausesWithImplicit.emplace_back(Implicit);
4065
5.57k
        ErrorFound |=
4066
5.57k
            cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
4067
5.57k
      } else {
4068
0
        ErrorFound = true;
4069
0
      }
4070
5.57k
    }
4071
111k
  }
4072
155k
4073
155k
  llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
4074
155k
  switch (Kind) {
4075
155k
  case OMPD_parallel:
4076
11.7k
    Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
4077
11.7k
                                       EndLoc);
4078
11.7k
    AllowedNameModifiers.push_back(OMPD_parallel);
4079
11.7k
    break;
4080
155k
  case OMPD_simd:
4081
3.36k
    Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4082
3.36k
                                   VarsWithInheritedDSA);
4083
3.36k
    break;
4084
155k
  case OMPD_for:
4085
3.75k
    Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
4086
3.75k
                                  VarsWithInheritedDSA);
4087
3.75k
    break;
4088
155k
  case OMPD_for_simd:
4089
3.23k
    Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4090
3.23k
                                      EndLoc, VarsWithInheritedDSA);
4091
3.23k
    break;
4092
155k
  case OMPD_sections:
4093
1.76k
    Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
4094
1.76k
                                       EndLoc);
4095
1.76k
    break;
4096
155k
  case OMPD_section:
4097
393
    assert(ClausesWithImplicit.empty() &&
4098
393
           "No clauses are allowed for 'omp section' directive");
4099
393
    Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
4100
393
    break;
4101
155k
  case OMPD_single:
4102
941
    Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
4103
941
                                     EndLoc);
4104
941
    break;
4105
155k
  case OMPD_master:
4106
369
    assert(ClausesWithImplicit.empty() &&
4107
369
           "No clauses are allowed for 'omp master' directive");
4108
369
    Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
4109
369
    break;
4110
155k
  case OMPD_critical:
4111
661
    Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
4112
661
                                       StartLoc, EndLoc);
4113
661
    break;
4114
155k
  case OMPD_parallel_for:
4115
3.14k
    Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
4116
3.14k
                                          EndLoc, VarsWithInheritedDSA);
4117
3.14k
    AllowedNameModifiers.push_back(OMPD_parallel);
4118
3.14k
    break;
4119
155k
  case OMPD_parallel_for_simd:
4120
3.57k
    Res = ActOnOpenMPParallelForSimdDirective(
4121
3.57k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4122
3.57k
    AllowedNameModifiers.push_back(OMPD_parallel);
4123
3.57k
    break;
4124
155k
  case OMPD_parallel_sections:
4125
1.83k
    Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
4126
1.83k
                                               StartLoc, EndLoc);
4127
1.83k
    AllowedNameModifiers.push_back(OMPD_parallel);
4128
1.83k
    break;
4129
155k
  case OMPD_task:
4130
1.94k
    Res =
4131
1.94k
        ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4132
1.94k
    AllowedNameModifiers.push_back(OMPD_task);
4133
1.94k
    break;
4134
155k
  case OMPD_taskyield:
4135
290
    assert(ClausesWithImplicit.empty() &&
4136
290
           "No clauses are allowed for 'omp taskyield' directive");
4137
290
    assert(AStmt == nullptr &&
4138
290
           "No associated statement allowed for 'omp taskyield' directive");
4139
290
    Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
4140
290
    break;
4141
155k
  case OMPD_barrier:
4142
254
    assert(ClausesWithImplicit.empty() &&
4143
254
           "No clauses are allowed for 'omp barrier' directive");
4144
254
    assert(AStmt == nullptr &&
4145
254
           "No associated statement allowed for 'omp barrier' directive");
4146
254
    Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
4147
254
    break;
4148
155k
  case OMPD_taskwait:
4149
276
    assert(ClausesWithImplicit.empty() &&
4150
276
           "No clauses are allowed for 'omp taskwait' directive");
4151
276
    assert(AStmt == nullptr &&
4152
276
           "No associated statement allowed for 'omp taskwait' directive");
4153
276
    Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
4154
276
    break;
4155
155k
  case OMPD_taskgroup:
4156
1.77k
    Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
4157
1.77k
                                        EndLoc);
4158
1.77k
    break;
4159
155k
  case OMPD_flush:
4160
344
    assert(AStmt == nullptr &&
4161
344
           "No associated statement allowed for 'omp flush' directive");
4162
344
    Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
4163
344
    break;
4164
155k
  case OMPD_ordered:
4165
978
    Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
4166
978
                                      EndLoc);
4167
978
    break;
4168
155k
  case OMPD_atomic:
4169
2.19k
    Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
4170
2.19k
                                     EndLoc);
4171
2.19k
    break;
4172
155k
  case OMPD_teams:
4173
11.8k
    Res =
4174
11.8k
        ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
4175
11.8k
    break;
4176
155k
  case OMPD_target:
4177
28.9k
    Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
4178
28.9k
                                     EndLoc);
4179
28.9k
    AllowedNameModifiers.push_back(OMPD_target);
4180
28.9k
    break;
4181
155k
  case OMPD_target_parallel:
4182
3.80k
    Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
4183
3.80k
                                             StartLoc, EndLoc);
4184
3.80k
    AllowedNameModifiers.push_back(OMPD_target);
4185
3.80k
    AllowedNameModifiers.push_back(OMPD_parallel);
4186
3.80k
    break;
4187
155k
  case OMPD_target_parallel_for:
4188
4.39k
    Res = ActOnOpenMPTargetParallelForDirective(
4189
4.39k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4190
4.39k
    AllowedNameModifiers.push_back(OMPD_target);
4191
4.39k
    AllowedNameModifiers.push_back(OMPD_parallel);
4192
4.39k
    break;
4193
155k
  case OMPD_cancellation_point:
4194
83
    assert(ClausesWithImplicit.empty() &&
4195
83
           "No clauses are allowed for 'omp cancellation point' directive");
4196
83
    assert(AStmt == nullptr && "No associated statement allowed for 'omp "
4197
83
                               "cancellation point' directive");
4198
83
    Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
4199
83
    break;
4200
155k
  case OMPD_cancel:
4201
282
    assert(AStmt == nullptr &&
4202
282
           "No associated statement allowed for 'omp cancel' directive");
4203
282
    Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
4204
282
                                     CancelRegion);
4205
282
    AllowedNameModifiers.push_back(OMPD_cancel);
4206
282
    break;
4207
155k
  case OMPD_target_data:
4208
1.31k
    Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
4209
1.31k
                                         EndLoc);
4210
1.31k
    AllowedNameModifiers.push_back(OMPD_target_data);
4211
1.31k
    break;
4212
155k
  case OMPD_target_enter_data:
4213
866
    Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
4214
866
                                              EndLoc, AStmt);
4215
866
    AllowedNameModifiers.push_back(OMPD_target_enter_data);
4216
866
    break;
4217
155k
  case OMPD_target_exit_data:
4218
878
    Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
4219
878
                                             EndLoc, AStmt);
4220
878
    AllowedNameModifiers.push_back(OMPD_target_exit_data);
4221
878
    break;
4222
155k
  case OMPD_taskloop:
4223
2.99k
    Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
4224
2.99k
                                       EndLoc, VarsWithInheritedDSA);
4225
2.99k
    AllowedNameModifiers.push_back(OMPD_taskloop);
4226
2.99k
    break;
4227
155k
  case OMPD_taskloop_simd:
4228
3.37k
    Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4229
3.37k
                                           EndLoc, VarsWithInheritedDSA);
4230
3.37k
    AllowedNameModifiers.push_back(OMPD_taskloop);
4231
3.37k
    break;
4232
155k
  case OMPD_distribute:
4233
1.15k
    Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
4234
1.15k
                                         EndLoc, VarsWithInheritedDSA);
4235
1.15k
    break;
4236
155k
  case OMPD_target_update:
4237
1.27k
    Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
4238
1.27k
                                           EndLoc, AStmt);
4239
1.27k
    AllowedNameModifiers.push_back(OMPD_target_update);
4240
1.27k
    break;
4241
155k
  case OMPD_distribute_parallel_for:
4242
2.97k
    Res = ActOnOpenMPDistributeParallelForDirective(
4243
2.97k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4244
2.97k
    AllowedNameModifiers.push_back(OMPD_parallel);
4245
2.97k
    break;
4246
155k
  case OMPD_distribute_parallel_for_simd:
4247
3.87k
    Res = ActOnOpenMPDistributeParallelForSimdDirective(
4248
3.87k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4249
3.87k
    AllowedNameModifiers.push_back(OMPD_parallel);
4250
3.87k
    break;
4251
155k
  case OMPD_distribute_simd:
4252
2.84k
    Res = ActOnOpenMPDistributeSimdDirective(
4253
2.84k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4254
2.84k
    break;
4255
155k
  case OMPD_target_parallel_for_simd:
4256
4.68k
    Res = ActOnOpenMPTargetParallelForSimdDirective(
4257
4.68k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4258
4.68k
    AllowedNameModifiers.push_back(OMPD_target);
4259
4.68k
    AllowedNameModifiers.push_back(OMPD_parallel);
4260
4.68k
    break;
4261
155k
  case OMPD_target_simd:
4262
4.28k
    Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
4263
4.28k
                                         EndLoc, VarsWithInheritedDSA);
4264
4.28k
    AllowedNameModifiers.push_back(OMPD_target);
4265
4.28k
    break;
4266
155k
  case OMPD_teams_distribute:
4267
2.39k
    Res = ActOnOpenMPTeamsDistributeDirective(
4268
2.39k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4269
2.39k
    break;
4270
155k
  case OMPD_teams_distribute_simd:
4271
2.93k
    Res = ActOnOpenMPTeamsDistributeSimdDirective(
4272
2.93k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4273
2.93k
    break;
4274
155k
  case OMPD_teams_distribute_parallel_for_simd:
4275
3.45k
    Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
4276
3.45k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4277
3.45k
    AllowedNameModifiers.push_back(OMPD_parallel);
4278
3.45k
    break;
4279
155k
  case OMPD_teams_distribute_parallel_for:
4280
2.87k
    Res = ActOnOpenMPTeamsDistributeParallelForDirective(
4281
2.87k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4282
2.87k
    AllowedNameModifiers.push_back(OMPD_parallel);
4283
2.87k
    break;
4284
155k
  case OMPD_target_teams:
4285
3.55k
    Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
4286
3.55k
                                          EndLoc);
4287
3.55k
    AllowedNameModifiers.push_back(OMPD_target);
4288
3.55k
    break;
4289
155k
  case OMPD_target_teams_distribute:
4290
3.58k
    Res = ActOnOpenMPTargetTeamsDistributeDirective(
4291
3.58k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4292
3.58k
    AllowedNameModifiers.push_back(OMPD_target);
4293
3.58k
    break;
4294
155k
  case OMPD_target_teams_distribute_parallel_for:
4295
4.20k
    Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
4296
4.20k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4297
4.20k
    AllowedNameModifiers.push_back(OMPD_target);
4298
4.20k
    AllowedNameModifiers.push_back(OMPD_parallel);
4299
4.20k
    break;
4300
155k
  case OMPD_target_teams_distribute_parallel_for_simd:
4301
5.26k
    Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
4302
5.26k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4303
5.26k
    AllowedNameModifiers.push_back(OMPD_target);
4304
5.26k
    AllowedNameModifiers.push_back(OMPD_parallel);
4305
5.26k
    break;
4306
155k
  case OMPD_target_teams_distribute_simd:
4307
4.68k
    Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
4308
4.68k
        ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
4309
4.68k
    AllowedNameModifiers.push_back(OMPD_target);
4310
4.68k
    break;
4311
155k
  case OMPD_declare_target:
4312
0
  case OMPD_end_declare_target:
4313
0
  case OMPD_threadprivate:
4314
0
  case OMPD_allocate:
4315
0
  case OMPD_declare_reduction:
4316
0
  case OMPD_declare_mapper:
4317
0
  case OMPD_declare_simd:
4318
0
  case OMPD_requires:
4319
0
    llvm_unreachable("OpenMP Directive is not allowed");
4320
0
  case OMPD_unknown:
4321
0
    llvm_unreachable("Unknown OpenMP directive");
4322
155k
  }
4323
155k
4324
155k
  ErrorFound = Res.isInvalid() || 
ErrorFound148k
;
4325
155k
4326
155k
  // Check variables in the clauses if default(none) was specified.
4327
155k
  if (DSAStack->getDefaultDSA() == DSA_none) {
4328
571
    DSAAttrChecker DSAChecker(DSAStack, *this, nullptr);
4329
2.82k
    for (OMPClause *C : Clauses) {
4330
2.82k
      switch (C->getClauseKind()) {
4331
2.82k
      case OMPC_num_threads:
4332
108
      case OMPC_dist_schedule:
4333
108
        // Do not analyse if no parent teams directive.
4334
108
        if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()))
4335
0
          break;
4336
108
        continue;
4337
108
      case OMPC_if:
4338
100
        if (isOpenMPTeamsDirective(DSAStack->getCurrentDirective()) &&
4339
100
            
cast<OMPIfClause>(C)->getNameModifier() != OMPD_target2
)
4340
2
          break;
4341
98
        continue;
4342
98
      case OMPC_schedule:
4343
96
        break;
4344
2.51k
      case OMPC_ordered:
4345
2.51k
      case OMPC_device:
4346
2.51k
      case OMPC_num_teams:
4347
2.51k
      case OMPC_thread_limit:
4348
2.51k
      case OMPC_priority:
4349
2.51k
      case OMPC_grainsize:
4350
2.51k
      case OMPC_num_tasks:
4351
2.51k
      case OMPC_hint:
4352
2.51k
      case OMPC_collapse:
4353
2.51k
      case OMPC_safelen:
4354
2.51k
      case OMPC_simdlen:
4355
2.51k
      case OMPC_final:
4356
2.51k
      case OMPC_default:
4357
2.51k
      case OMPC_proc_bind:
4358
2.51k
      case OMPC_private:
4359
2.51k
      case OMPC_firstprivate:
4360
2.51k
      case OMPC_lastprivate:
4361
2.51k
      case OMPC_shared:
4362
2.51k
      case OMPC_reduction:
4363
2.51k
      case OMPC_task_reduction:
4364
2.51k
      case OMPC_in_reduction:
4365
2.51k
      case OMPC_linear:
4366
2.51k
      case OMPC_aligned:
4367
2.51k
      case OMPC_copyin:
4368
2.51k
      case OMPC_copyprivate:
4369
2.51k
      case OMPC_nowait:
4370
2.51k
      case OMPC_untied:
4371
2.51k
      case OMPC_mergeable:
4372
2.51k
      case OMPC_allocate:
4373
2.51k
      case OMPC_read:
4374
2.51k
      case OMPC_write:
4375
2.51k
      case OMPC_update:
4376
2.51k
      case OMPC_capture:
4377
2.51k
      case OMPC_seq_cst:
4378
2.51k
      case OMPC_depend:
4379
2.51k
      case OMPC_threads:
4380
2.51k
      case OMPC_simd:
4381
2.51k
      case OMPC_map:
4382
2.51k
      case OMPC_nogroup:
4383
2.51k
      case OMPC_defaultmap:
4384
2.51k
      case OMPC_to:
4385
2.51k
      case OMPC_from:
4386
2.51k
      case OMPC_use_device_ptr:
4387
2.51k
      case OMPC_is_device_ptr:
4388
2.51k
        continue;
4389
2.51k
      case OMPC_allocator:
4390
0
      case OMPC_flush:
4391
0
      case OMPC_threadprivate:
4392
0
      case OMPC_uniform:
4393
0
      case OMPC_unknown:
4394
0
      case OMPC_unified_address:
4395
0
      case OMPC_unified_shared_memory:
4396
0
      case OMPC_reverse_offload:
4397
0
      case OMPC_dynamic_allocators:
4398
0
      case OMPC_atomic_default_mem_order:
4399
0
        llvm_unreachable("Unexpected clause");
4400
98
      }
4401
98
      for (Stmt *CC : C->children()) {
4402
98
        if (CC)
4403
42
          DSAChecker.Visit(CC);
4404
98
      }
4405
98
    }
4406
571
    for (auto &P : DSAChecker.getVarsWithInheritedDSA())
4407
12
      VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
4408
571
  }
4409
155k
  for (const auto &P : VarsWithInheritedDSA) {
4410
195
    if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(P.getFirst()))
4411
0
      continue;
4412
195
    ErrorFound = true;
4413
195
    Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
4414
195
        << P.first << P.second->getSourceRange();
4415
195
    Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);
4416
195
  }
4417
155k
4418
155k
  if (!AllowedNameModifiers.empty())
4419
113k
    ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
4420
113k
                 
ErrorFound113k
;
4421
155k
4422
155k
  if (ErrorFound)
4423
8.37k
    return StmtError();
4424
147k
4425
147k
  if (!(Res.getAs<OMPExecutableDirective>()->isStandaloneDirective())) {
4426
142k
    Res.getAs<OMPExecutableDirective>()
4427
142k
        ->getStructuredBlock()
4428
142k
        ->setIsOMPStructuredBlock(true);
4429
142k
  }
4430
147k
4431
147k
  if (!CurContext->isDependentContext() &&
4432
147k
      
isOpenMPTargetExecutionDirective(Kind)107k
&&
4433
147k
      
!(47.4k
DSAStack47.4k
->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>()47.4k
||
4434
47.4k
        
DSAStack47.4k
->hasRequiresDeclWithClause<OMPUnifiedAddressClause>()47.4k
||
4435
47.4k
        
DSAStack47.4k
->hasRequiresDeclWithClause<OMPReverseOffloadClause>()47.4k
||
4436
47.4k
        
DSAStack47.4k
->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>()47.4k
)) {
4437
47.4k
    // Register target to DSA Stack.
4438
47.4k
    DSAStack->addTargetDirLocation(StartLoc);
4439
47.4k
  }
4440
147k
4441
147k
  return Res;
4442
147k
}
4443
4444
Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
4445
    DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
4446
    ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
4447
    ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
4448
416
    ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
4449
416
  assert(Aligneds.size() == Alignments.size());
4450
416
  assert(Linears.size() == LinModifiers.size());
4451
416
  assert(Linears.size() == Steps.size());
4452
416
  if (!DG || DG.get().isNull())
4453
0
    return DeclGroupPtrTy();
4454
416
4455
416
  if (!DG.get().isSingleDecl()) {
4456
2
    Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
4457
2
    return DG;
4458
2
  }
4459
414
  Decl *ADecl = DG.get().getSingleDecl();
4460
414
  if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
4461
18
    ADecl = FTD->getTemplatedDecl();
4462
414
4463
414
  auto *FD = dyn_cast<FunctionDecl>(ADecl);
4464
414
  if (!FD) {
4465
4
    Diag(ADecl->getLocation(), diag::err_omp_function_expected);
4466
4
    return DeclGroupPtrTy();
4467
4
  }
4468
410
4469
410
  // OpenMP [2.8.2, declare simd construct, Description]
4470
410
  // The parameter of the simdlen clause must be a constant positive integer
4471
410
  // expression.
4472
410
  ExprResult SL;
4473
410
  if (Simdlen)
4474
104
    SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
4475
410
  // OpenMP [2.8.2, declare simd construct, Description]
4476
410
  // The special this pointer can be used as if was one of the arguments to the
4477
410
  // function in any of the linear, aligned, or uniform clauses.
4478
410
  // The uniform clause declares one or more arguments to have an invariant
4479
410
  // value for all concurrent invocations of the function in the execution of a
4480
410
  // single SIMD loop.
4481
410
  llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
4482
410
  const Expr *UniformedLinearThis = nullptr;
4483
410
  for (const Expr *E : Uniforms) {
4484
94
    E = E->IgnoreParenImpCasts();
4485
94
    if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4486
68
      if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
4487
64
        if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4488
64
            FD->getParamDecl(PVD->getFunctionScopeIndex())
4489
64
                    ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
4490
64
          UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
4491
64
          continue;
4492
64
        }
4493
30
    if (isa<CXXThisExpr>(E)) {
4494
26
      UniformedLinearThis = E;
4495
26
      continue;
4496
26
    }
4497
4
    Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4498
4
        << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 
10
: 0);
4499
4
  }
4500
410
  // OpenMP [2.8.2, declare simd construct, Description]
4501
410
  // The aligned clause declares that the object to which each list item points
4502
410
  // is aligned to the number of bytes expressed in the optional parameter of
4503
410
  // the aligned clause.
4504
410
  // The special this pointer can be used as if was one of the arguments to the
4505
410
  // function in any of the linear, aligned, or uniform clauses.
4506
410
  // The type of list items appearing in the aligned clause must be array,
4507
410
  // pointer, reference to array, or reference to pointer.
4508
410
  llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
4509
410
  const Expr *AlignedThis = nullptr;
4510
410
  for (const Expr *E : Aligneds) {
4511
190
    E = E->IgnoreParenImpCasts();
4512
190
    if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4513
186
      if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4514
182
        const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4515
182
        if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4516
182
            FD->getParamDecl(PVD->getFunctionScopeIndex())
4517
182
                    ->getCanonicalDecl() == CanonPVD) {
4518
182
          // OpenMP  [2.8.1, simd construct, Restrictions]
4519
182
          // A list-item cannot appear in more than one aligned clause.
4520
182
          if (AlignedArgs.count(CanonPVD) > 0) {
4521
4
            Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4522
4
                << 1 << E->getSourceRange();
4523
4
            Diag(AlignedArgs[CanonPVD]->getExprLoc(),
4524
4
                 diag::note_omp_explicit_dsa)
4525
4
                << getOpenMPClauseName(OMPC_aligned);
4526
4
            continue;
4527
4
          }
4528
178
          AlignedArgs[CanonPVD] = E;
4529
178
          QualType QTy = PVD->getType()
4530
178
                             .getNonReferenceType()
4531
178
                             .getUnqualifiedType()
4532
178
                             .getCanonicalType();
4533
178
          const Type *Ty = QTy.getTypePtrOrNull();
4534
178
          if (!Ty || (!Ty->isArrayType() && 
!Ty->isPointerType()154
)) {
4535
0
            Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
4536
0
                << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
4537
0
            Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
4538
0
          }
4539
178
          continue;
4540
178
        }
4541
182
      }
4542
8
    if (isa<CXXThisExpr>(E)) {
4543
4
      if (AlignedThis) {
4544
2
        Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4545
2
            << 2 << E->getSourceRange();
4546
2
        Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
4547
2
            << getOpenMPClauseName(OMPC_aligned);
4548
2
      }
4549
4
      AlignedThis = E;
4550
4
      continue;
4551
4
    }
4552
4
    Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4553
4
        << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 
10
: 0);
4554
4
  }
4555
410
  // The optional parameter of the aligned clause, alignment, must be a constant
4556
410
  // positive integer expression. If no optional parameter is specified,
4557
410
  // implementation-defined default alignments for SIMD instructions on the
4558
410
  // target platforms are assumed.
4559
410
  SmallVector<const Expr *, 4> NewAligns;
4560
410
  for (Expr *E : Alignments) {
4561
190
    ExprResult Align;
4562
190
    if (E)
4563
72
      Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
4564
190
    NewAligns.push_back(Align.get());
4565
190
  }
4566
410
  // OpenMP [2.8.2, declare simd construct, Description]
4567
410
  // The linear clause declares one or more list items to be private to a SIMD
4568
410
  // lane and to have a linear relationship with respect to the iteration space
4569
410
  // of a loop.
4570
410
  // The special this pointer can be used as if was one of the arguments to the
4571
410
  // function in any of the linear, aligned, or uniform clauses.
4572
410
  // When a linear-step expression is specified in a linear clause it must be
4573
410
  // either a constant integer expression or an integer-typed parameter that is
4574
410
  // specified in a uniform clause on the directive.
4575
410
  llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
4576
410
  const bool IsUniformedThis = UniformedLinearThis != nullptr;
4577
410
  auto MI = LinModifiers.begin();
4578
410
  for (const Expr *E : Linears) {
4579
152
    auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
4580
152
    ++MI;
4581
152
    E = E->IgnoreParenImpCasts();
4582
152
    if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
4583
142
      if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4584
142
        const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4585
142
        if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4586
142
            FD->getParamDecl(PVD->getFunctionScopeIndex())
4587
142
                    ->getCanonicalDecl() == CanonPVD) {
4588
142
          // OpenMP  [2.15.3.7, linear Clause, Restrictions]
4589
142
          // A list-item cannot appear in more than one linear clause.
4590
142
          if (LinearArgs.count(CanonPVD) > 0) {
4591
4
            Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4592
4
                << getOpenMPClauseName(OMPC_linear)
4593
4
                << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
4594
4
            Diag(LinearArgs[CanonPVD]->getExprLoc(),
4595
4
                 diag::note_omp_explicit_dsa)
4596
4
                << getOpenMPClauseName(OMPC_linear);
4597
4
            continue;
4598
4
          }
4599
138
          // Each argument can appear in at most one uniform or linear clause.
4600
138
          if (UniformedArgs.count(CanonPVD) > 0) {
4601
4
            Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4602
4
                << getOpenMPClauseName(OMPC_linear)
4603
4
                << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
4604
4
            Diag(UniformedArgs[CanonPVD]->getExprLoc(),
4605
4
                 diag::note_omp_explicit_dsa)
4606
4
                << getOpenMPClauseName(OMPC_uniform);
4607
4
            continue;
4608
4
          }
4609
134
          LinearArgs[CanonPVD] = E;
4610
134
          if (E->isValueDependent() || 
E->isTypeDependent()126
||
4611
134
              
E->isInstantiationDependent()126
||
4612
134
              
E->containsUnexpandedParameterPack()126
)
4613
8
            continue;
4614
126
          (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
4615
126
                                      PVD->getOriginalType());
4616
126
          continue;
4617
126
        }
4618
142
      }
4619
10
    if (isa<CXXThisExpr>(E)) {
4620
10
      if (UniformedLinearThis) {
4621
2
        Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4622
2
            << getOpenMPClauseName(OMPC_linear)
4623
2
            << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : 
OMPC_linear0
)
4624
2
            << E->getSourceRange();
4625
2
        Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
4626
2
            << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
4627
2
                                                   : 
OMPC_linear0
);
4628
2
        continue;
4629
2
      }
4630
8
      UniformedLinearThis = E;
4631
8
      if (E->isValueDependent() || E->isTypeDependent() ||
4632
8
          E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4633
0
        continue;
4634
8
      (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
4635
8
                                  E->getType());
4636
8
      continue;
4637
8
    }
4638
0
    Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4639
0
        << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4640
0
  }
4641
410
  Expr *Step = nullptr;
4642
410
  Expr *NewStep = nullptr;
4643
410
  SmallVector<Expr *, 4> NewSteps;
4644
410
  for (Expr *E : Steps) {
4645
152
    // Skip the same step expression, it was checked already.
4646
152
    if (Step == E || 
!E126
) {
4647
42
      NewSteps.push_back(E ? 
NewStep4
:
nullptr38
);
4648
42
      continue;
4649
42
    }
4650
110
    Step = E;
4651
110
    if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
4652
38
      if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4653
22
        const VarDecl *CanonPVD = PVD->getCanonicalDecl();
4654
22
        if (UniformedArgs.count(CanonPVD) == 0) {
4655
4
          Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4656
4
              << Step->getSourceRange();
4657
18
        } else if (E->isValueDependent() || E->isTypeDependent() ||
4658
18
                   E->isInstantiationDependent() ||
4659
18
                   E->containsUnexpandedParameterPack() ||
4660
18
                   CanonPVD->getType()->hasIntegerRepresentation()) {
4661
18
          NewSteps.push_back(Step);
4662
18
        } else {
4663
0
          Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4664
0
              << Step->getSourceRange();
4665
0
        }
4666
22
        continue;
4667
22
      }
4668
88
    NewStep = Step;
4669
88
    if (Step && !Step->isValueDependent() && 
!Step->isTypeDependent()72
&&
4670
88
        
!Step->isInstantiationDependent()72
&&
4671
88
        
!Step->containsUnexpandedParameterPack()72
) {
4672
72
      NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4673
72
                    .get();
4674
72
      if (NewStep)
4675
72
        NewStep = VerifyIntegerConstantExpression(NewStep).get();
4676
72
    }
4677
88
    NewSteps.push_back(NewStep);
4678
88
  }
4679
410
  auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4680
410
      Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
4681
410
      Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
4682
410
      const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4683
410
      const_cast<Expr **>(Linears.data()), Linears.size(),
4684
410
      const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4685
410
      NewSteps.data(), NewSteps.size(), SR);
4686
410
  ADecl->addAttr(NewAttr);
4687
410
  return ConvertDeclToDeclGroup(ADecl);
4688
410
}
4689
4690
StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
4691
                                              Stmt *AStmt,
4692
                                              SourceLocation StartLoc,
4693
11.7k
                                              SourceLocation EndLoc) {
4694
11.7k
  if (!AStmt)
4695
576
    return StmtError();
4696
11.1k
4697
11.1k
  auto *CS = cast<CapturedStmt>(AStmt);
4698
11.1k
  // 1.2.2 OpenMP Language Terminology
4699
11.1k
  // Structured block - An executable statement with a single entry at the
4700
11.1k
  // top and a single exit at the bottom.
4701
11.1k
  // The point of exit cannot be a branch out of the structured block.
4702
11.1k
  // longjmp() and throw() must not violate the entry/exit criteria.
4703
11.1k
  CS->getCapturedDecl()->setNothrow();
4704
11.1k
4705
11.1k
  setFunctionHasBranchProtectedScope();
4706
11.1k
4707
11.1k
  return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4708
11.1k
                                      DSAStack->isCancelRegion());
4709
11.1k
}
4710
4711
namespace {
4712
/// Helper class for checking canonical form of the OpenMP loops and
4713
/// extracting iteration space of each loop in the loop nest, that will be used
4714
/// for IR generation.
4715
class OpenMPIterationSpaceChecker {
4716
  /// Reference to Sema.
4717
  Sema &SemaRef;
4718
  /// Data-sharing stack.
4719
  DSAStackTy &Stack;
4720
  /// A location for diagnostics (when there is no some better location).
4721
  SourceLocation DefaultLoc;
4722
  /// A location for diagnostics (when increment is not compatible).
4723
  SourceLocation ConditionLoc;
4724
  /// A source location for referring to loop init later.
4725
  SourceRange InitSrcRange;
4726
  /// A source location for referring to condition later.
4727
  SourceRange ConditionSrcRange;
4728
  /// A source location for referring to increment later.
4729
  SourceRange IncrementSrcRange;
4730
  /// Loop variable.
4731
  ValueDecl *LCDecl = nullptr;
4732
  /// Reference to loop variable.
4733
  Expr *LCRef = nullptr;
4734
  /// Lower bound (initializer for the var).
4735
  Expr *LB = nullptr;
4736
  /// Upper bound.
4737
  Expr *UB = nullptr;
4738
  /// Loop step (increment).
4739
  Expr *Step = nullptr;
4740
  /// This flag is true when condition is one of:
4741
  ///   Var <  UB
4742
  ///   Var <= UB
4743
  ///   UB  >  Var
4744
  ///   UB  >= Var
4745
  /// This will have no value when the condition is !=
4746
  llvm::Optional<bool> TestIsLessOp;
4747
  /// This flag is true when condition is strict ( < or > ).
4748
  bool TestIsStrictOp = false;
4749
  /// This flag is true when step is subtracted on each iteration.
4750
  bool SubtractStep = false;
4751
  /// The outer loop counter this loop depends on (if any).
4752
  const ValueDecl *DepDecl = nullptr;
4753
  /// Contains number of loop (starts from 1) on which loop counter init
4754
  /// expression of this loop depends on.
4755
  Optional<unsigned> InitDependOnLC;
4756
  /// Contains number of loop (starts from 1) on which loop counter condition
4757
  /// expression of this loop depends on.
4758
  Optional<unsigned> CondDependOnLC;
4759
  /// Checks if the provide statement depends on the loop counter.
4760
  Optional<unsigned> doesDependOnLoopCounter(const Stmt *S, bool IsInitializer);
4761
4762
public:
4763
  OpenMPIterationSpaceChecker(Sema &SemaRef, DSAStackTy &Stack,
4764
                              SourceLocation DefaultLoc)
4765
      : SemaRef(SemaRef), Stack(Stack), DefaultLoc(DefaultLoc),
4766
161k
        ConditionLoc(DefaultLoc) {}
4767
  /// Check init-expr for canonical loop form and save loop counter
4768
  /// variable - #Var and its initialization value - #LB.
4769
  bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
4770
  /// Check test-expr for canonical form, save upper-bound (#UB), flags
4771
  /// for less/greater and for strict/non-strict comparison.
4772
  bool checkAndSetCond(Expr *S);
4773
  /// Check incr-expr for canonical loop form and return true if it
4774
  /// does not conform, otherwise save loop step (#Step).
4775
  bool checkAndSetInc(Expr *S);
4776
  /// Return the loop counter variable.
4777
161k
  ValueDecl *getLoopDecl() const { return LCDecl; }
4778
  /// Return the reference expression to loop counter variable.
4779
85.0k
  Expr *getLoopDeclRefExpr() const { return LCRef; }
4780
  /// Source range of the loop init.
4781
55.2k
  SourceRange getInitSrcRange() const { return InitSrcRange; }
4782
  /// Source range of the loop condition.
4783
55.2k
  SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
4784
  /// Source range of the loop increment.
4785
55.2k
  SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
4786
  /// True if the step should be subtracted.
4787
55.2k
  bool shouldSubtractStep() const { return SubtractStep; }
4788
  /// True, if the compare operator is strict (<, > or !=).
4789
55.2k
  bool isStrictTestOp() const { return TestIsStrictOp; }
4790
  /// Build the expression to calculate the number of iterations.
4791
  Expr *buildNumIterations(
4792
      Scope *S, const bool LimitedType,
4793
      llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
4794
  /// Build the precondition expression for the loops.
4795
  Expr *
4796
  buildPreCond(Scope *S, Expr *Cond,
4797
               llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
4798
  /// Build reference expression to the counter be used for codegen.
4799
  DeclRefExpr *
4800
  buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4801
                  DSAStackTy &DSA) const;
4802
  /// Build reference expression to the private counter be used for
4803
  /// codegen.
4804
  Expr *buildPrivateCounterVar() const;
4805
  /// Build initialization of the counter be used for codegen.
4806
  Expr *buildCounterInit() const;
4807
  /// Build step of the counter be used for codegen.
4808
  Expr *buildCounterStep() const;
4809
  /// Build loop data with counter value for depend clauses in ordered
4810
  /// directives.
4811
  Expr *
4812
  buildOrderedLoopData(Scope *S, Expr *Counter,
4813
                       llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4814
                       SourceLocation Loc, Expr *Inc = nullptr,
4815
                       OverloadedOperatorKind OOK = OO_Amp);
4816
  /// Return true if any expression is dependent.
4817
  bool dependent() const;
4818
4819
private:
4820
  /// Check the right-hand side of an assignment in the increment
4821
  /// expression.
4822
  bool checkAndSetIncRHS(Expr *RHS);
4823
  /// Helper to set loop counter variable and its initializer.
4824
  bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
4825
                      bool EmitDiags);
4826
  /// Helper to set upper bound.
4827
  bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
4828
             SourceRange SR, SourceLocation SL);
4829
  /// Helper to set loop increment.
4830
  bool setStep(Expr *NewStep, bool Subtract);
4831
};
4832
4833
77.3k
bool OpenMPIterationSpaceChecker::dependent() const {
4834
77.3k
  if (!LCDecl) {
4835
594
    assert(!LB && !UB && !Step);
4836
594
    return false;
4837
594
  }
4838
76.7k
  return LCDecl->getType()->isDependentType() ||
4839
76.7k
         
(74.2k
LB74.2k
&&
LB->isValueDependent()74.2k
) ||
(71.3k
UB71.3k
&&
UB->isValueDependent()70.8k
) ||
4840
76.7k
         
(68.7k
Step68.7k
&&
Step->isValueDependent()66.8k
);
4841
76.7k
}
4842
4843
bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
4844
                                                 Expr *NewLCRefExpr,
4845
161k
                                                 Expr *NewLB, bool EmitDiags) {
4846
161k
  // State consistency checking to ensure correct usage.
4847
161k
  assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
4848
161k
         UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4849
161k
  if (!NewLCDecl || !NewLB)
4850
0
    return true;
4851
161k
  LCDecl = getCanonicalDecl(NewLCDecl);
4852
161k
  LCRef = NewLCRefExpr;
4853
161k
  if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4854
2.42k
    if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4855
2.42k
      if ((Ctor->isCopyOrMoveConstructor() ||
4856
2.42k
           
Ctor->isConvertingConstructor(/*AllowExplicit=*/false)444
) &&
4857
2.42k
          
CE->getNumArgs() > 02.08k
&&
CE->getArg(0) != nullptr2.08k
)
4858
2.08k
        NewLB = CE->getArg(0)->IgnoreParenImpCasts();
4859
161k
  LB = NewLB;
4860
161k
  if (EmitDiags)