Coverage Report

Created: 2021-01-19 06:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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/OpenMPClause.h"
22
#include "clang/AST/StmtCXX.h"
23
#include "clang/AST/StmtOpenMP.h"
24
#include "clang/AST/StmtVisitor.h"
25
#include "clang/AST/TypeOrdering.h"
26
#include "clang/Basic/DiagnosticSema.h"
27
#include "clang/Basic/OpenMPKinds.h"
28
#include "clang/Basic/PartialDiagnostic.h"
29
#include "clang/Basic/TargetInfo.h"
30
#include "clang/Sema/Initialization.h"
31
#include "clang/Sema/Lookup.h"
32
#include "clang/Sema/Scope.h"
33
#include "clang/Sema/ScopeInfo.h"
34
#include "clang/Sema/SemaInternal.h"
35
#include "llvm/ADT/IndexedMap.h"
36
#include "llvm/ADT/PointerEmbeddedInt.h"
37
#include "llvm/ADT/STLExtras.h"
38
#include "llvm/ADT/StringExtras.h"
39
#include "llvm/Frontend/OpenMP/OMPConstants.h"
40
#include <set>
41
42
using namespace clang;
43
using namespace llvm::omp;
44
45
//===----------------------------------------------------------------------===//
46
// Stack of data-sharing attributes for variables
47
//===----------------------------------------------------------------------===//
48
49
static const Expr *checkMapClauseExpressionBase(
50
    Sema &SemaRef, Expr *E,
51
    OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
52
    OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose);
53
54
namespace {
55
/// Default data sharing attributes, which can be applied to directive.
56
enum DefaultDataSharingAttributes {
57
  DSA_unspecified = 0,       /// Data sharing attribute not specified.
58
  DSA_none = 1 << 0,         /// Default data sharing attribute 'none'.
59
  DSA_shared = 1 << 1,       /// Default data sharing attribute 'shared'.
60
  DSA_firstprivate = 1 << 2, /// Default data sharing attribute 'firstprivate'.
61
};
62
63
/// Stack for tracking declarations used in OpenMP directives and
64
/// clauses and their data-sharing attributes.
65
class DSAStackTy {
66
public:
67
  struct DSAVarData {
68
    OpenMPDirectiveKind DKind = OMPD_unknown;
69
    OpenMPClauseKind CKind = OMPC_unknown;
70
    unsigned Modifier = 0;
71
    const Expr *RefExpr = nullptr;
72
    DeclRefExpr *PrivateCopy = nullptr;
73
    SourceLocation ImplicitDSALoc;
74
    bool AppliedToPointee = false;
75
8.98M
    DSAVarData() = default;
76
    DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
77
               const Expr *RefExpr, DeclRefExpr *PrivateCopy,
78
               SourceLocation ImplicitDSALoc, unsigned Modifier,
79
               bool AppliedToPointee)
80
        : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
81
          PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc),
82
1.06k
          AppliedToPointee(AppliedToPointee) {}
83
  };
84
  using OperatorOffsetTy =
85
      llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
86
  using DoacrossDependMapTy =
87
      llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
88
  /// Kind of the declaration used in the uses_allocators clauses.
89
  enum class UsesAllocatorsDeclKind {
90
    /// Predefined allocator
91
    PredefinedAllocator,
92
    /// User-defined allocator
93
    UserDefinedAllocator,
94
    /// The declaration that represent allocator trait
95
    AllocatorTrait,
96
  };
97
98
private:
99
  struct DSAInfo {
100
    OpenMPClauseKind Attributes = OMPC_unknown;
101
    unsigned Modifier = 0;
102
    /// Pointer to a reference expression and a flag which shows that the
103
    /// variable is marked as lastprivate(true) or not (false).
104
    llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
105
    DeclRefExpr *PrivateCopy = nullptr;
106
    /// true if the attribute is applied to the pointee, not the variable
107
    /// itself.
108
    bool AppliedToPointee = false;
109
  };
110
  using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
111
  using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
112
  using LCDeclInfo = std::pair<unsigned, VarDecl *>;
113
  using LoopControlVariablesMapTy =
114
      llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
115
  /// Struct that associates a component with the clause kind where they are
116
  /// found.
117
  struct MappedExprComponentTy {
118
    OMPClauseMappableExprCommon::MappableExprComponentLists Components;
119
    OpenMPClauseKind Kind = OMPC_unknown;
120
  };
121
  using MappedExprComponentsTy =
122
      llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
123
  using CriticalsWithHintsTy =
124
      llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
125
  struct ReductionData {
126
    using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
127
    SourceRange ReductionRange;
128
    llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
129
1.95k
    ReductionData() = default;
130
1.92k
    void set(BinaryOperatorKind BO, SourceRange RR) {
131
1.92k
      ReductionRange = RR;
132
1.92k
      ReductionOp = BO;
133
1.92k
    }
134
30
    void set(const Expr *RefExpr, SourceRange RR) {
135
30
      ReductionRange = RR;
136
30
      ReductionOp = RefExpr;
137
30
    }
138
  };
139
  using DeclReductionMapTy =
140
      llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
141
  struct DefaultmapInfo {
142
    OpenMPDefaultmapClauseModifier ImplicitBehavior =
143
        OMPC_DEFAULTMAP_MODIFIER_unknown;
144
    SourceLocation SLoc;
145
1.00M
    DefaultmapInfo() = default;
146
    DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
147
0
        : ImplicitBehavior(M), SLoc(Loc) {}
148
  };
149
150
  struct SharingMapTy {
151
    DeclSAMapTy SharingMap;
152
    DeclReductionMapTy ReductionMap;
153
    UsedRefMapTy AlignedMap;
154
    UsedRefMapTy NontemporalMap;
155
    MappedExprComponentsTy MappedExprComponents;
156
    LoopControlVariablesMapTy LCVMap;
157
    DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
158
    SourceLocation DefaultAttrLoc;
159
    DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown];
160
    OpenMPDirectiveKind Directive = OMPD_unknown;
161
    DeclarationNameInfo DirectiveName;
162
    Scope *CurScope = nullptr;
163
    DeclContext *Context = nullptr;
164
    SourceLocation ConstructLoc;
165
    /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
166
    /// get the data (loop counters etc.) about enclosing loop-based construct.
167
    /// This data is required during codegen.
168
    DoacrossDependMapTy DoacrossDepends;
169
    /// First argument (Expr *) contains optional argument of the
170
    /// 'ordered' clause, the second one is true if the regions has 'ordered'
171
    /// clause, false otherwise.
172
    llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
173
    unsigned AssociatedLoops = 1;
174
    bool HasMutipleLoops = false;
175
    const Decl *PossiblyLoopCounter = nullptr;
176
    bool NowaitRegion = false;
177
    bool CancelRegion = false;
178
    bool LoopStart = false;
179
    bool BodyComplete = false;
180
    SourceLocation PrevScanLocation;
181
    SourceLocation PrevOrderedLocation;
182
    SourceLocation InnerTeamsRegionLoc;
183
    /// Reference to the taskgroup task_reduction reference expression.
184
    Expr *TaskgroupReductionRef = nullptr;
185
    llvm::DenseSet<QualType> MappedClassesQualTypes;
186
    SmallVector<Expr *, 4> InnerUsedAllocators;
187
    llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates;
188
    /// List of globals marked as declare target link in this target region
189
    /// (isOpenMPTargetExecutionDirective(Directive) == true).
190
    llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
191
    /// List of decls used in inclusive/exclusive clauses of the scan directive.
192
    llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
193
    llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind>
194
        UsesAllocatorsDecls;
195
    Expr *DeclareMapperVar = nullptr;
196
    SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
197
                 Scope *CurScope, SourceLocation Loc)
198
        : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
199
333k
          ConstructLoc(Loc) {}
200
    SharingMapTy() = default;
201
  };
202
203
  using StackTy = SmallVector<SharingMapTy, 4>;
204
205
  /// Stack of used declaration and their data-sharing attributes.
206
  DeclSAMapTy Threadprivates;
207
  const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
208
  SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
209
  /// true, if check for DSA must be from parent directive, false, if
210
  /// from current directive.
211
  OpenMPClauseKind ClauseKindMode = OMPC_unknown;
212
  Sema &SemaRef;
213
  bool ForceCapturing = false;
214
  /// true if all the variables in the target executable directives must be
215
  /// captured by reference.
216
  bool ForceCaptureByReferenceInTargetExecutable = false;
217
  CriticalsWithHintsTy Criticals;
218
  unsigned IgnoredStackElements = 0;
219
220
  /// Iterators over the stack iterate in order from innermost to outermost
221
  /// directive.
222
  using const_iterator = StackTy::const_reverse_iterator;
223
1.10M
  const_iterator begin() const {
224
115
    return Stack.empty() ? const_iterator()
225
1.10M
                         : Stack.back().first.rbegin() + IgnoredStackElements;
226
1.10M
  }
227
8.39M
  const_iterator end() const {
228
8.39M
    return Stack.empty() ? 
const_iterator()115
: Stack.back().first.rend();
229
8.39M
  }
230
  using iterator = StackTy::reverse_iterator;
231
3.71M
  iterator begin() {
232
0
    return Stack.empty() ? iterator()
233
3.71M
                         : Stack.back().first.rbegin() + IgnoredStackElements;
234
3.71M
  }
235
3.73M
  iterator end() {
236
3.73M
    return Stack.empty() ? 
iterator()0
: Stack.back().first.rend();
237
3.73M
  }
238
239
  // Convenience operations to get at the elements of the stack.
240
241
98.9M
  bool isStackEmpty() const {
242
98.9M
    return Stack.empty() ||
243
98.5M
           Stack.back().second != CurrentNonCapturingFunctionScope ||
244
98.5M
           Stack.back().first.size() <= IgnoredStackElements;
245
98.9M
  }
246
71.8M
  size_t getStackSize() const {
247
450k
    return isStackEmpty() ? 0
248
71.3M
                          : Stack.back().first.size() - IgnoredStackElements;
249
71.8M
  }
250
251
35.3M
  SharingMapTy *getTopOfStackOrNull() {
252
35.3M
    size_t Size = getStackSize();
253
35.3M
    if (Size == 0)
254
448k
      return nullptr;
255
34.8M
    return &Stack.back().first[Size - 1];
256
34.8M
  }
257
21.1M
  const SharingMapTy *getTopOfStackOrNull() const {
258
21.1M
    return const_cast<DSAStackTy&>(*this).getTopOfStackOrNull();
259
21.1M
  }
260
14.2M
  SharingMapTy &getTopOfStack() {
261
14.2M
    assert(!isStackEmpty() && "no current directive");
262
14.2M
    return *getTopOfStackOrNull();
263
14.2M
  }
264
12.4M
  const SharingMapTy &getTopOfStack() const {
265
12.4M
    return const_cast<DSAStackTy&>(*this).getTopOfStack();
266
12.4M
  }
267
268
555k
  SharingMapTy *getSecondOnStackOrNull() {
269
555k
    size_t Size = getStackSize();
270
555k
    if (Size <= 1)
271
164k
      return nullptr;
272
391k
    return &Stack.back().first[Size - 2];
273
391k
  }
274
515k
  const SharingMapTy *getSecondOnStackOrNull() const {
275
515k
    return const_cast<DSAStackTy&>(*this).getSecondOnStackOrNull();
276
515k
  }
277
278
  /// Get the stack element at a certain level (previously returned by
279
  /// \c getNestingLevel).
280
  ///
281
  /// Note that nesting levels count from outermost to innermost, and this is
282
  /// the reverse of our iteration order where new inner levels are pushed at
283
  /// the front of the stack.
284
19.2M
  SharingMapTy &getStackElemAtLevel(unsigned Level) {
285
19.2M
    assert(Level < getStackSize() && "no such stack element");
286
19.2M
    return Stack.back().first[Level];
287
19.2M
  }
288
19.1M
  const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
289
19.1M
    return const_cast<DSAStackTy&>(*this).getStackElemAtLevel(Level);
290
19.1M
  }
291
292
  DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
293
294
  /// Checks if the variable is a local for OpenMP region.
295
  bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
296
297
  /// Vector of previously declared requires directives
298
  SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
299
  /// omp_allocator_handle_t type.
300
  QualType OMPAllocatorHandleT;
301
  /// omp_depend_t type.
302
  QualType OMPDependT;
303
  /// omp_event_handle_t type.
304
  QualType OMPEventHandleT;
305
  /// omp_alloctrait_t type.
306
  QualType OMPAlloctraitT;
307
  /// Expression for the predefined allocators.
308
  Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
309
      nullptr};
310
  /// Vector of previously encountered target directives
311
  SmallVector<SourceLocation, 2> TargetLocations;
312
  SourceLocation AtomicLocation;
313
314
public:
315
78.9k
  explicit DSAStackTy(Sema &S) : SemaRef(S) {}
316
317
  /// Sets omp_allocator_handle_t type.
318
538
  void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
319
  /// Gets omp_allocator_handle_t type.
320
2.95k
  QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
321
  /// Sets omp_alloctrait_t type.
322
28
  void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; }
323
  /// Gets omp_alloctrait_t type.
324
78
  QualType getOMPAlloctraitT() const { return OMPAlloctraitT; }
325
  /// Sets the given default allocator.
326
  void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
327
4.84k
                    Expr *Allocator) {
328
4.84k
    OMPPredefinedAllocators[AllocatorKind] = Allocator;
329
4.84k
  }
330
  /// Returns the specified default allocator.
331
7.11k
  Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
332
7.11k
    return OMPPredefinedAllocators[AllocatorKind];
333
7.11k
  }
334
  /// Sets omp_depend_t type.
335
26
  void setOMPDependT(QualType Ty) { OMPDependT = Ty; }
336
  /// Gets omp_depend_t type.
337
4.20k
  QualType getOMPDependT() const { return OMPDependT; }
338
339
  /// Sets omp_event_handle_t type.
340
15
  void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; }
341
  /// Gets omp_event_handle_t type.
342
170
  QualType getOMPEventHandleT() const { return OMPEventHandleT; }
343
344
5.22M
  bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
345
508k
  OpenMPClauseKind getClauseParsingMode() const {
346
508k
    assert(isClauseParsingMode() && "Must be in clause parsing mode.");
347
508k
    return ClauseKindMode;
348
508k
  }
349
429k
  void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
350
351
281k
  bool isBodyComplete() const {
352
281k
    const SharingMapTy *Top = getTopOfStackOrNull();
353
281k
    return Top && 
Top->BodyComplete218k
;
354
281k
  }
355
305k
  void setBodyComplete() {
356
305k
    getTopOfStack().BodyComplete = true;
357
305k
  }
358
359
357k
  bool isForceVarCapturing() const { return ForceCapturing; }
360
108k
  void setForceVarCapturing(bool V) { ForceCapturing = V; }
361
362
78.3k
  void setForceCaptureByReferenceInTargetExecutable(bool V) {
363
78.3k
    ForceCaptureByReferenceInTargetExecutable = V;
364
78.3k
  }
365
889k
  bool isForceCaptureByReferenceInTargetExecutable() const {
366
889k
    return ForceCaptureByReferenceInTargetExecutable;
367
889k
  }
368
369
  void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
370
333k
            Scope *CurScope, SourceLocation Loc) {
371
333k
    assert(!IgnoredStackElements &&
372
333k
           "cannot change stack while ignoring elements");
373
333k
    if (Stack.empty() ||
374
301k
        Stack.back().second != CurrentNonCapturingFunctionScope)
375
31.7k
      Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
376
333k
    Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
377
333k
    Stack.back().first.back().DefaultAttrLoc = Loc;
378
333k
  }
379
380
333k
  void pop() {
381
333k
    assert(!IgnoredStackElements &&
382
333k
           "cannot change stack while ignoring elements");
383
333k
    assert(!Stack.back().first.empty() &&
384
333k
           "Data-sharing attributes stack is empty!");
385
333k
    Stack.back().first.pop_back();
386
333k
  }
387
388
  /// RAII object to temporarily leave the scope of a directive when we want to
389
  /// logically operate in its parent.
390
  class ParentDirectiveScope {
391
    DSAStackTy &Self;
392
    bool Active;
393
  public:
394
    ParentDirectiveScope(DSAStackTy &Self, bool Activate)
395
1.69M
        : Self(Self), Active(false) {
396
1.69M
      if (Activate)
397
42.0k
        enable();
398
1.69M
    }
399
1.69M
    ~ParentDirectiveScope() { disable(); }
400
1.69M
    void disable() {
401
1.69M
      if (Active) {
402
42.0k
        --Self.IgnoredStackElements;
403
42.0k
        Active = false;
404
42.0k
      }
405
1.69M
    }
406
42.0k
    void enable() {
407
42.0k
      if (!Active) {
408
42.0k
        ++Self.IgnoredStackElements;
409
42.0k
        Active = true;
410
42.0k
      }
411
42.0k
    }
412
  };
413
414
  /// Marks that we're started loop parsing.
415
165k
  void loopInit() {
416
165k
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
417
165k
           "Expected loop-based directive.");
418
165k
    getTopOfStack().LoopStart = true;
419
165k
  }
420
  /// Start capturing of the variables in the loop context.
421
207k
  void loopStart() {
422
207k
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
423
207k
           "Expected loop-based directive.");
424
207k
    getTopOfStack().LoopStart = false;
425
207k
  }
426
  /// true, if variables are captured, false otherwise.
427
1.13M
  bool isLoopStarted() const {
428
1.13M
    assert(isOpenMPLoopDirective(getCurrentDirective()) &&
429
1.13M
           "Expected loop-based directive.");
430
1.13M
    return !getTopOfStack().LoopStart;
431
1.13M
  }
432
  /// Marks (or clears) declaration as possibly loop counter.
433
176k
  void resetPossibleLoopCounter(const Decl *D = nullptr) {
434
176k
    getTopOfStack().PossiblyLoopCounter =
435
133k
        D ? 
D->getCanonicalDecl()43.5k
: D;
436
176k
  }
437
  /// Gets the possible loop counter decl.
438
2.47M
  const Decl *getPossiblyLoopCunter() const {
439
2.47M
    return getTopOfStack().PossiblyLoopCounter;
440
2.47M
  }
441
  /// Start new OpenMP region stack in new non-capturing function.
442
96.5k
  void pushFunction() {
443
96.5k
    assert(!IgnoredStackElements &&
444
96.5k
           "cannot change stack while ignoring elements");
445
96.5k
    const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
446
96.5k
    assert(!isa<CapturingScopeInfo>(CurFnScope));
447
96.5k
    CurrentNonCapturingFunctionScope = CurFnScope;
448
96.5k
  }
449
  /// Pop region stack for non-capturing function.
450
653k
  void popFunction(const FunctionScopeInfo *OldFSI) {
451
653k
    assert(!IgnoredStackElements &&
452
653k
           "cannot change stack while ignoring elements");
453
653k
    if (!Stack.empty() && 
Stack.back().second == OldFSI592k
) {
454
31.7k
      assert(Stack.back().first.empty());
455
31.7k
      Stack.pop_back();
456
31.7k
    }
457
653k
    CurrentNonCapturingFunctionScope = nullptr;
458
1.19M
    for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
459
1.19M
      if (!isa<CapturingScopeInfo>(FSI)) {
460
563k
        CurrentNonCapturingFunctionScope = FSI;
461
563k
        break;
462
563k
      }
463
1.19M
    }
464
653k
  }
465
466
62
  void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
467
62
    Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
468
62
  }
469
  const std::pair<const OMPCriticalDirective *, llvm::APSInt>
470
1.55k
  getCriticalWithHint(const DeclarationNameInfo &Name) const {
471
1.55k
    auto I = Criticals.find(Name.getAsString());
472
1.55k
    if (I != Criticals.end())
473
138
      return I->second;
474
1.41k
    return std::make_pair(nullptr, llvm::APSInt());
475
1.41k
  }
476
  /// If 'aligned' declaration for given variable \a D was not seen yet,
477
  /// add it and return NULL; otherwise return previous occurrence's expression
478
  /// for diagnostics.
479
  const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
480
  /// If 'nontemporal' declaration for given variable \a D was not seen yet,
481
  /// add it and return NULL; otherwise return previous occurrence's expression
482
  /// for diagnostics.
483
  const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
484
485
  /// Register specified variable as loop control variable.
486
  void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
487
  /// Check if the specified variable is a loop control variable for
488
  /// current region.
489
  /// \return The index of the loop control variable in the list of associated
490
  /// for-loops (from outer to inner).
491
  const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
492
  /// Check if the specified variable is a loop control variable for
493
  /// parent region.
494
  /// \return The index of the loop control variable in the list of associated
495
  /// for-loops (from outer to inner).
496
  const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
497
  /// Check if the specified variable is a loop control variable for
498
  /// current region.
499
  /// \return The index of the loop control variable in the list of associated
500
  /// for-loops (from outer to inner).
501
  const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
502
                                         unsigned Level) const;
503
  /// Get the loop control variable for the I-th loop (or nullptr) in
504
  /// parent directive.
505
  const ValueDecl *getParentLoopControlVariable(unsigned I) const;
506
507
  /// Marks the specified decl \p D as used in scan directive.
508
160
  void markDeclAsUsedInScanDirective(ValueDecl *D) {
509
160
    if (SharingMapTy *Stack = getSecondOnStackOrNull())
510
160
      Stack->UsedInScanDirective.insert(D);
511
160
  }
512
513
  /// Checks if the specified declaration was used in the inner scan directive.
514
166
  bool isUsedInScanDirective(ValueDecl *D) const {
515
166
    if (const SharingMapTy *Stack = getTopOfStackOrNull())
516
166
      return Stack->UsedInScanDirective.count(D) > 0;
517
0
    return false;
518
0
  }
519
520
  /// Adds explicit data sharing attribute to the specified declaration.
521
  void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
522
              DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0,
523
              bool AppliedToPointee = false);
524
525
  /// Adds additional information for the reduction items with the reduction id
526
  /// represented as an operator.
527
  void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
528
                                 BinaryOperatorKind BOK);
529
  /// Adds additional information for the reduction items with the reduction id
530
  /// represented as reduction identifier.
531
  void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
532
                                 const Expr *ReductionRef);
533
  /// Returns the location and reduction operation from the innermost parent
534
  /// region for the given \p D.
535
  const DSAVarData
536
  getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
537
                                   BinaryOperatorKind &BOK,
538
                                   Expr *&TaskgroupDescriptor) const;
539
  /// Returns the location and reduction operation from the innermost parent
540
  /// region for the given \p D.
541
  const DSAVarData
542
  getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
543
                                   const Expr *&ReductionRef,
544
                                   Expr *&TaskgroupDescriptor) const;
545
  /// Return reduction reference expression for the current taskgroup or
546
  /// parallel/worksharing directives with task reductions.
547
77.8k
  Expr *getTaskgroupReductionRef() const {
548
77.8k
    assert((getTopOfStack().Directive == OMPD_taskgroup ||
549
77.8k
            ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
550
77.8k
              isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
551
77.8k
             !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
552
77.8k
           "taskgroup reference expression requested for non taskgroup or "
553
77.8k
           "parallel/worksharing directive.");
554
77.8k
    return getTopOfStack().TaskgroupReductionRef;
555
77.8k
  }
556
  /// Checks if the given \p VD declaration is actually a taskgroup reduction
557
  /// descriptor variable at the \p Level of OpenMP regions.
558
608k
  bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
559
608k
    return getStackElemAtLevel(Level).TaskgroupReductionRef &&
560
13.5k
           cast<DeclRefExpr>(getStackElemAtLevel(Level).TaskgroupReductionRef)
561
13.5k
                   ->getDecl() == VD;
562
608k
  }
563
564
  /// Returns data sharing attributes from top of the stack for the
565
  /// specified declaration.
566
  const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
567
  /// Returns data-sharing attributes for the specified declaration.
568
  const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
569
  /// Returns data-sharing attributes for the specified declaration.
570
  const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
571
  /// Checks if the specified variables has data-sharing attributes which
572
  /// match specified \a CPred predicate in any directive which matches \a DPred
573
  /// predicate.
574
  const DSAVarData
575
  hasDSA(ValueDecl *D,
576
         const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
577
         const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
578
         bool FromParent) const;
579
  /// Checks if the specified variables has data-sharing attributes which
580
  /// match specified \a CPred predicate in any innermost directive which
581
  /// matches \a DPred predicate.
582
  const DSAVarData
583
  hasInnermostDSA(ValueDecl *D,
584
                  const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
585
                  const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
586
                  bool FromParent) const;
587
  /// Checks if the specified variables has explicit data-sharing
588
  /// attributes which match specified \a CPred predicate at the specified
589
  /// OpenMP region.
590
  bool
591
  hasExplicitDSA(const ValueDecl *D,
592
                 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
593
                 unsigned Level, bool NotLastprivate = false) const;
594
595
  /// Returns true if the directive at level \Level matches in the
596
  /// specified \a DPred predicate.
597
  bool hasExplicitDirective(
598
      const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
599
      unsigned Level) const;
600
601
  /// Finds a directive which matches specified \a DPred predicate.
602
  bool hasDirective(
603
      const llvm::function_ref<bool(
604
          OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
605
          DPred,
606
      bool FromParent) const;
607
608
  /// Returns currently analyzed directive.
609
13.3M
  OpenMPDirectiveKind getCurrentDirective() const {
610
13.3M
    const SharingMapTy *Top = getTopOfStackOrNull();
611
13.0M
    return Top ? Top->Directive : 
OMPD_unknown258k
;
612
13.3M
  }
613
  /// Returns directive kind at specified level.
614
1.78M
  OpenMPDirectiveKind getDirective(unsigned Level) const {
615
1.78M
    assert(!isStackEmpty() && "No directive at specified level.");
616
1.78M
    return getStackElemAtLevel(Level).Directive;
617
1.78M
  }
618
  /// Returns the capture region at the specified level.
619
  OpenMPDirectiveKind getCaptureRegion(unsigned Level,
620
10.0k
                                       unsigned OpenMPCaptureLevel) const {
621
10.0k
    SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
622
10.0k
    getOpenMPCaptureRegions(CaptureRegions, getDirective(Level));
623
10.0k
    return CaptureRegions[OpenMPCaptureLevel];
624
10.0k
  }
625
  /// Returns parent directive.
626
508k
  OpenMPDirectiveKind getParentDirective() const {
627
508k
    const SharingMapTy *Parent = getSecondOnStackOrNull();
628
346k
    return Parent ? Parent->Directive : 
OMPD_unknown162k
;
629
508k
  }
630
631
  /// Add requires decl to internal vector
632
155
  void addRequiresDecl(OMPRequiresDecl *RD) {
633
155
    RequiresDecls.push_back(RD);
634
155
  }
635
636
  /// Checks if the defined 'requires' directive has specified type of clause.
637
  template <typename ClauseType>
638
351k
  bool hasRequiresDeclWithClause() const {
639
19.6k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
19.6k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
19.6k
        return isa<ClauseType>(C);
642
19.6k
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
640
4.95k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
4.95k
        return isa<ClauseType>(C);
642
4.95k
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
640
4.90k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
4.90k
        return isa<ClauseType>(C);
642
4.90k
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
640
4.88k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
4.88k
        return isa<ClauseType>(C);
642
4.88k
      });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const::'lambda'(clang::OMPClause const*)::operator()(clang::OMPClause const*) const
Line
Count
Source
640
4.88k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
4.88k
        return isa<ClauseType>(C);
642
4.88k
      });
643
19.6k
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
639
4.95k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
4.95k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
4.95k
        return isa<ClauseType>(C);
642
4.95k
      });
643
4.95k
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
639
4.90k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
4.90k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
4.90k
        return isa<ClauseType>(C);
642
4.90k
      });
643
4.90k
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
639
4.88k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
4.88k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
4.88k
        return isa<ClauseType>(C);
642
4.88k
      });
643
4.88k
    });
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>() const::'lambda'(clang::OMPRequiresDecl const*)::operator()(clang::OMPRequiresDecl const*) const
Line
Count
Source
639
4.88k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
4.88k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
4.88k
        return isa<ClauseType>(C);
642
4.88k
      });
643
4.88k
    });
644
351k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPDynamicAllocatorsClause>() const
Line
Count
Source
638
87.7k
  bool hasRequiresDeclWithClause() const {
639
87.7k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
87.7k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
87.7k
        return isa<ClauseType>(C);
642
87.7k
      });
643
87.7k
    });
644
87.7k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedSharedMemoryClause>() const
Line
Count
Source
638
89.8k
  bool hasRequiresDeclWithClause() const {
639
89.8k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
89.8k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
89.8k
        return isa<ClauseType>(C);
642
89.8k
      });
643
89.8k
    });
644
89.8k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPUnifiedAddressClause>() const
Line
Count
Source
638
86.9k
  bool hasRequiresDeclWithClause() const {
639
86.9k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
86.9k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
86.9k
        return isa<ClauseType>(C);
642
86.9k
      });
643
86.9k
    });
644
86.9k
  }
SemaOpenMP.cpp:bool (anonymous namespace)::DSAStackTy::hasRequiresDeclWithClause<clang::OMPReverseOffloadClause>() const
Line
Count
Source
638
86.9k
  bool hasRequiresDeclWithClause() const {
639
86.9k
    return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
640
86.9k
      return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
641
86.9k
        return isa<ClauseType>(C);
642
86.9k
      });
643
86.9k
    });
644
86.9k
  }
645
646
  /// Checks for a duplicate clause amongst previously declared requires
647
  /// directives
648
170
  bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
649
170
    bool IsDuplicate = false;
650
174
    for (OMPClause *CNew : ClauseList) {
651
135
      for (const OMPRequiresDecl *D : RequiresDecls) {
652
135
        for (const OMPClause *CPrev : D->clauselists()) {
653
135
          if (CNew->getClauseKind() == CPrev->getClauseKind()) {
654
19
            SemaRef.Diag(CNew->getBeginLoc(),
655
19
                         diag::err_omp_requires_clause_redeclaration)
656
19
                << getOpenMPClauseName(CNew->getClauseKind());
657
19
            SemaRef.Diag(CPrev->getBeginLoc(),
658
19
                         diag::note_omp_requires_previous_clause)
659
19
                << getOpenMPClauseName(CPrev->getClauseKind());
660
19
            IsDuplicate = true;
661
19
          }
662
135
        }
663
135
      }
664
174
    }
665
170
    return IsDuplicate;
666
170
  }
667
668
  /// Add location of previously encountered target to internal vector
669
82.1k
  void addTargetDirLocation(SourceLocation LocStart) {
670
82.1k
    TargetLocations.push_back(LocStart);
671
82.1k
  }
672
673
  /// Add location for the first encountered atomicc directive.
674
4.34k
  void addAtomicDirectiveLoc(SourceLocation Loc) {
675
4.34k
    if (AtomicLocation.isInvalid())
676
67
      AtomicLocation = Loc;
677
4.34k
  }
678
679
  /// Returns the location of the first encountered atomic directive in the
680
  /// module.
681
170
  SourceLocation getAtomicDirectiveLoc() const {
682
170
    return AtomicLocation;
683
170
  }
684
685
  // Return previously encountered target region locations.
686
170
  ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
687
170
    return TargetLocations;
688
170
  }
689
690
  /// Set default data sharing attribute to none.
691
1.03k
  void setDefaultDSANone(SourceLocation Loc) {
692
1.03k
    getTopOfStack().DefaultAttr = DSA_none;
693
1.03k
    getTopOfStack().DefaultAttrLoc = Loc;
694
1.03k
  }
695
  /// Set default data sharing attribute to shared.
696
711
  void setDefaultDSAShared(SourceLocation Loc) {
697
711
    getTopOfStack().DefaultAttr = DSA_shared;
698
711
    getTopOfStack().DefaultAttrLoc = Loc;
699
711
  }
700
  /// Set default data sharing attribute to firstprivate.
701
94
  void setDefaultDSAFirstPrivate(SourceLocation Loc) {
702
94
    getTopOfStack().DefaultAttr = DSA_firstprivate;
703
94
    getTopOfStack().DefaultAttrLoc = Loc;
704
94
  }
705
  /// Set default data mapping attribute to Modifier:Kind
706
  void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
707
                         OpenMPDefaultmapClauseKind Kind,
708
3.23k
                         SourceLocation Loc) {
709
3.23k
    DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
710
3.23k
    DMI.ImplicitBehavior = M;
711
3.23k
    DMI.SLoc = Loc;
712
3.23k
  }
713
  /// Check whether the implicit-behavior has been set in defaultmap
714
1.96k
  bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
715
1.96k
    if (VariableCategory == OMPC_DEFAULTMAP_unknown)
716
600
      return getTopOfStack()
717
600
                     .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
718
600
                     .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
719
600
             getTopOfStack()
720
600
                     .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
721
600
                     .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
722
600
             getTopOfStack()
723
600
                     .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
724
600
                     .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
725
1.36k
    return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
726
1.36k
           OMPC_DEFAULTMAP_MODIFIER_unknown;
727
1.36k
  }
728
729
0
  DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
730
0
    return getStackSize() <= Level ? DSA_unspecified
731
0
                                   : getStackElemAtLevel(Level).DefaultAttr;
732
0
  }
733
1.83M
  DefaultDataSharingAttributes getDefaultDSA() const {
734
0
    return isStackEmpty() ? DSA_unspecified
735
1.83M
                          : getTopOfStack().DefaultAttr;
736
1.83M
  }
737
518
  SourceLocation getDefaultDSALocation() const {
738
0
    return isStackEmpty() ? SourceLocation()
739
518
                          : getTopOfStack().DefaultAttrLoc;
740
518
  }
741
  OpenMPDefaultmapClauseModifier
742
178k
  getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
743
178k
    return isStackEmpty()
744
0
               ? OMPC_DEFAULTMAP_MODIFIER_unknown
745
178k
               : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
746
178k
  }
747
  OpenMPDefaultmapClauseModifier
748
  getDefaultmapModifierAtLevel(unsigned Level,
749
764k
                               OpenMPDefaultmapClauseKind Kind) const {
750
764k
    return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
751
764k
  }
752
  bool isDefaultmapCapturedByRef(unsigned Level,
753
581k
                                 OpenMPDefaultmapClauseKind Kind) const {
754
581k
    OpenMPDefaultmapClauseModifier M =
755
581k
        getDefaultmapModifierAtLevel(Level, Kind);
756
581k
    if (Kind == OMPC_DEFAULTMAP_scalar || 
Kind == OMPC_DEFAULTMAP_pointer109k
) {
757
581k
      return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
758
581k
             (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
759
581k
             (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
760
581k
             (M == OMPC_DEFAULTMAP_MODIFIER_tofrom);
761
581k
    }
762
0
    return true;
763
0
  }
764
  static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
765
248k
                                     OpenMPDefaultmapClauseKind Kind) {
766
248k
    switch (Kind) {
767
165k
    case OMPC_DEFAULTMAP_scalar:
768
192k
    case OMPC_DEFAULTMAP_pointer:
769
192k
      return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
770
1.73k
             (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
771
1.68k
             (M == OMPC_DEFAULTMAP_MODIFIER_default);
772
55.2k
    case OMPC_DEFAULTMAP_aggregate:
773
55.2k
      return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
774
0
    default:
775
0
      break;
776
0
    }
777
0
    llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
778
0
  }
779
  bool mustBeFirstprivateAtLevel(unsigned Level,
780
182k
                                 OpenMPDefaultmapClauseKind Kind) const {
781
182k
    OpenMPDefaultmapClauseModifier M =
782
182k
        getDefaultmapModifierAtLevel(Level, Kind);
783
182k
    return mustBeFirstprivateBase(M, Kind);
784
182k
  }
785
65.2k
  bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
786
65.2k
    OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
787
65.2k
    return mustBeFirstprivateBase(M, Kind);
788
65.2k
  }
789
790
  /// Checks if the specified variable is a threadprivate.
791
2.63M
  bool isThreadPrivate(VarDecl *D) {
792
2.63M
    const DSAVarData DVar = getTopDSA(D, false);
793
2.63M
    return isOpenMPThreadPrivate(DVar.CKind);
794
2.63M
  }
795
796
  /// Marks current region as ordered (it has an 'ordered' clause).
797
  void setOrderedRegion(bool IsOrdered, const Expr *Param,
798
1.14k
                        OMPOrderedClause *Clause) {
799
1.14k
    if (IsOrdered)
800
1.14k
      getTopOfStack().OrderedRegion.emplace(Param, Clause);
801
0
    else
802
0
      getTopOfStack().OrderedRegion.reset();
803
1.14k
  }
804
  /// Returns true, if region is ordered (has associated 'ordered' clause),
805
  /// false - otherwise.
806
104k
  bool isOrderedRegion() const {
807
104k
    if (const SharingMapTy *Top = getTopOfStackOrNull())
808
104k
      return Top->OrderedRegion.hasValue();
809
0
    return false;
810
0
  }
811
  /// Returns optional parameter for the ordered region.
812
1.97k
  std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
813
1.97k
    if (const SharingMapTy *Top = getTopOfStackOrNull())
814
1.97k
      if (Top->OrderedRegion.hasValue())
815
1.97k
        return Top->OrderedRegion.getValue();
816
0
    return std::make_pair(nullptr, nullptr);
817
0
  }
818
  /// Returns true, if parent region is ordered (has associated
819
  /// 'ordered' clause), false - otherwise.
820
2.06k
  bool isParentOrderedRegion() const {
821
2.06k
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
822
2.05k
      return Parent->OrderedRegion.hasValue();
823
12
    return false;
824
12
  }
825
  /// Returns optional parameter for the ordered region.
826
  std::pair<const Expr *, OMPOrderedClause *>
827
2.50k
  getParentOrderedRegionParam() const {
828
2.50k
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
829
1.92k
      if (Parent->OrderedRegion.hasValue())
830
1.91k
        return Parent->OrderedRegion.getValue();
831
583
    return std::make_pair(nullptr, nullptr);
832
583
  }
833
  /// Marks current region as nowait (it has a 'nowait' clause).
834
1.49k
  void setNowaitRegion(bool IsNowait = true) {
835
1.49k
    getTopOfStack().NowaitRegion = IsNowait;
836
1.49k
  }
837
  /// Returns true, if parent region is nowait (has associated
838
  /// 'nowait' clause), false - otherwise.
839
803
  bool isParentNowaitRegion() const {
840
803
    if (const SharingMapTy *Parent = getSecondOnStackOrNull())
841
803
      return Parent->NowaitRegion;
842
0
    return false;
843
0
  }
844
  /// Marks parent region as cancel region.
845
1.47k
  void setParentCancelRegion(bool Cancel = true) {
846
1.47k
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
847
1.47k
      Parent->CancelRegion |= Cancel;
848
1.47k
  }
849
  /// Return true if current region has inner cancel construct.
850
88.5k
  bool isCancelRegion() const {
851
88.5k
    const SharingMapTy *Top = getTopOfStackOrNull();
852
88.5k
    return Top ? Top->CancelRegion : 
false0
;
853
88.5k
  }
854
855
  /// Mark that parent region already has scan directive.
856
98
  void setParentHasScanDirective(SourceLocation Loc) {
857
98
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
858
98
      Parent->PrevScanLocation = Loc;
859
98
  }
860
  /// Return true if current region has inner cancel construct.
861
102
  bool doesParentHasScanDirective() const {
862
102
    const SharingMapTy *Top = getSecondOnStackOrNull();
863
102
    return Top ? Top->PrevScanLocation.isValid() : 
false0
;
864
102
  }
865
  /// Return true if current region has inner cancel construct.
866
4
  SourceLocation getParentScanDirectiveLoc() const {
867
4
    const SharingMapTy *Top = getSecondOnStackOrNull();
868
4
    return Top ? Top->PrevScanLocation : 
SourceLocation()0
;
869
4
  }
870
  /// Mark that parent region already has ordered directive.
871
854
  void setParentHasOrderedDirective(SourceLocation Loc) {
872
854
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
873
295
      Parent->PrevOrderedLocation = Loc;
874
854
  }
875
  /// Return true if current region has inner ordered construct.
876
866
  bool doesParentHasOrderedDirective() const {
877
866
    const SharingMapTy *Top = getSecondOnStackOrNull();
878
559
    return Top ? 
Top->PrevOrderedLocation.isValid()307
: false;
879
866
  }
880
  /// Returns the location of the previously specified ordered directive.
881
12
  SourceLocation getParentOrderedDirectiveLoc() const {
882
12
    const SharingMapTy *Top = getSecondOnStackOrNull();
883
12
    return Top ? Top->PrevOrderedLocation : 
SourceLocation()0
;
884
12
  }
885
886
  /// Set collapse value for the region.
887
168k
  void setAssociatedLoops(unsigned Val) {
888
168k
    getTopOfStack().AssociatedLoops = Val;
889
168k
    if (Val > 1)
890
4.31k
      getTopOfStack().HasMutipleLoops = true;
891
168k
  }
892
  /// Return collapse value for region.
893
2.52M
  unsigned getAssociatedLoops() const {
894
2.52M
    const SharingMapTy *Top = getTopOfStackOrNull();
895
2.52M
    return Top ? Top->AssociatedLoops : 
0302
;
896
2.52M
  }
897
  /// Returns true if the construct is associated with multiple loops.
898
95.2k
  bool hasMutipleLoops() const {
899
95.2k
    const SharingMapTy *Top = getTopOfStackOrNull();
900
95.2k
    return Top ? Top->HasMutipleLoops : 
false0
;
901
95.2k
  }
902
903
  /// Marks current target region as one with closely nested teams
904
  /// region.
905
36.6k
  void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
906
36.6k
    if (SharingMapTy *Parent = getSecondOnStackOrNull())
907
36.4k
      Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
908
36.6k
  }
909
  /// Returns true, if current region has closely nested teams region.
910
49.9k
  bool hasInnerTeamsRegion() const {
911
49.9k
    return getInnerTeamsRegionLoc().isValid();
912
49.9k
  }
913
  /// Returns location of the nested teams region (if any).
914
50.0k
  SourceLocation getInnerTeamsRegionLoc() const {
915
50.0k
    const SharingMapTy *Top = getTopOfStackOrNull();
916
50.0k
    return Top ? Top->InnerTeamsRegionLoc : 
SourceLocation()0
;
917
50.0k
  }
918
919
2.71M
  Scope *getCurScope() const {
920
2.71M
    const SharingMapTy *Top = getTopOfStackOrNull();
921
2.71M
    return Top ? Top->CurScope : 
nullptr0
;
922
2.71M
  }
923
327k
  void setContext(DeclContext *DC) { getTopOfStack().Context = DC; }
924
606k
  SourceLocation getConstructLoc() const {
925
606k
    const SharingMapTy *Top = getTopOfStackOrNull();
926
606k
    return Top ? Top->ConstructLoc : 
SourceLocation()0
;
927
606k
  }
928
929
  /// Do the check specified in \a Check to all component lists and return true
930
  /// if any issue is found.
931
  bool checkMappableExprComponentListsForDecl(
932
      const ValueDecl *VD, bool CurrentRegionOnly,
933
      const llvm::function_ref<
934
          bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
935
               OpenMPClauseKind)>
936
265k
          Check) const {
937
265k
    if (isStackEmpty())
938
0
      return false;
939
265k
    auto SI = begin();
940
265k
    auto SE = end();
941
942
265k
    if (SI == SE)
943
0
      return false;
944
945
265k
    if (CurrentRegionOnly)
946
236k
      SE = std::next(SI);
947
29.3k
    else
948
29.3k
      std::advance(SI, 1);
949
950
504k
    for (; SI != SE; 
++SI238k
) {
951
246k
      auto MI = SI->MappedExprComponents.find(VD);
952
246k
      if (MI != SI->MappedExprComponents.end())
953
10.9k
        for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
954
10.9k
             MI->second.Components)
955
11.0k
          if (Check(L, MI->second.Kind))
956
8.33k
            return true;
957
246k
    }
958
257k
    return false;
959
265k
  }
960
961
  /// Do the check specified in \a Check to all component lists at a given level
962
  /// and return true if any issue is found.
963
  bool checkMappableExprComponentListsForDeclAtLevel(
964
      const ValueDecl *VD, unsigned Level,
965
      const llvm::function_ref<
966
          bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
967
               OpenMPClauseKind)>
968
1.15M
          Check) const {
969
1.15M
    if (getStackSize() <= Level)
970
0
      return false;
971
972
1.15M
    const SharingMapTy &StackElem = getStackElemAtLevel(Level);
973
1.15M
    auto MI = StackElem.MappedExprComponents.find(VD);
974
1.15M
    if (MI != StackElem.MappedExprComponents.end())
975
35.6k
      for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
976
35.6k
           MI->second.Components)
977
35.6k
        if (Check(L, MI->second.Kind))
978
17.4k
          return true;
979
1.13M
    return false;
980
1.15M
  }
981
982
  /// Create a new mappable expression component list associated with a given
983
  /// declaration and initialize it with the provided list of components.
984
  void addMappableExpressionComponents(
985
      const ValueDecl *VD,
986
      OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
987
44.2k
      OpenMPClauseKind WhereFoundClauseKind) {
988
44.2k
    MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
989
    // Create new entry and append the new components there.
990
44.2k
    MEC.Components.resize(MEC.Components.size() + 1);
991
44.2k
    MEC.Components.back().append(Components.begin(), Components.end());
992
44.2k
    MEC.Kind = WhereFoundClauseKind;
993
44.2k
  }
994
995
920k
  unsigned getNestingLevel() const {
996
920k
    assert(!isStackEmpty());
997
920k
    return getStackSize() - 1;
998
920k
  }
999
  void addDoacrossDependClause(OMPDependClause *C,
1000
475
                               const OperatorOffsetTy &OpsOffs) {
1001
475
    SharingMapTy *Parent = getSecondOnStackOrNull();
1002
475
    assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
1003
475
    Parent->DoacrossDepends.try_emplace(C, OpsOffs);
1004
475
  }
1005
  llvm::iterator_range<DoacrossDependMapTy::const_iterator>
1006
809
  getDoacrossDependClauses() const {
1007
809
    const SharingMapTy &StackElem = getTopOfStack();
1008
809
    if (isOpenMPWorksharingDirective(StackElem.Directive)) {
1009
809
      const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
1010
809
      return llvm::make_range(Ref.begin(), Ref.end());
1011
809
    }
1012
0
    return llvm::make_range(StackElem.DoacrossDepends.end(),
1013
0
                            StackElem.DoacrossDepends.end());
1014
0
  }
1015
1016
  // Store types of classes which have been explicitly mapped
1017
97
  void addMappedClassesQualTypes(QualType QT) {
1018
97
    SharingMapTy &StackElem = getTopOfStack();
1019
97
    StackElem.MappedClassesQualTypes.insert(QT);
1020
97
  }
1021
1022
  // Return set of mapped classes types
1023
1.43k
  bool isClassPreviouslyMapped(QualType QT) const {
1024
1.43k
    const SharingMapTy &StackElem = getTopOfStack();
1025
1.43k
    return StackElem.MappedClassesQualTypes.count(QT) != 0;
1026
1.43k
  }
1027
1028
  /// Adds global declare target to the parent target region.
1029
8
  void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
1030
8
    assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1031
8
               E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
1032
8
           "Expected declare target link global.");
1033
16
    for (auto &Elem : *this) {
1034
16
      if (isOpenMPTargetExecutionDirective(Elem.Directive)) {
1035
8
        Elem.DeclareTargetLinkVarDecls.push_back(E);
1036
8
        return;
1037
8
      }
1038
16
    }
1039
8
  }
1040
1041
  /// Returns the list of globals with declare target link if current directive
1042
  /// is target.
1043
90.1k
  ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1044
90.1k
    assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1045
90.1k
           "Expected target executable directive.");
1046
90.1k
    return getTopOfStack().DeclareTargetLinkVarDecls;
1047
90.1k
  }
1048
1049
  /// Adds list of allocators expressions.
1050
959
  void addInnerAllocatorExpr(Expr *E) {
1051
959
    getTopOfStack().InnerUsedAllocators.push_back(E);
1052
959
  }
1053
  /// Return list of used allocators.
1054
305k
  ArrayRef<Expr *> getInnerAllocators() const {
1055
305k
    return getTopOfStack().InnerUsedAllocators;
1056
305k
  }
1057
  /// Marks the declaration as implicitly firstprivate nin the task-based
1058
  /// regions.
1059
115k
  void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1060
115k
    getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(D);
1061
115k
  }
1062
  /// Checks if the decl is implicitly firstprivate in the task-based region.
1063
273k
  bool isImplicitTaskFirstprivate(Decl *D) const {
1064
273k
    return getTopOfStack().ImplicitTaskFirstprivates.count(D) > 0;
1065
273k
  }
1066
1067
  /// Marks decl as used in uses_allocators clause as the allocator.
1068
308
  void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1069
308
    getTopOfStack().UsesAllocatorsDecls.try_emplace(D, Kind);
1070
308
  }
1071
  /// Checks if specified decl is used in uses allocator clause as the
1072
  /// allocator.
1073
  Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(unsigned Level,
1074
1.99M
                                                        const Decl *D) const {
1075
1.99M
    const SharingMapTy &StackElem = getTopOfStack();
1076
1.99M
    auto I = StackElem.UsesAllocatorsDecls.find(D);
1077
1.99M
    if (I == StackElem.UsesAllocatorsDecls.end())
1078
1.99M
      return None;
1079
564
    return I->getSecond();
1080
564
  }
1081
422k
  Optional<UsesAllocatorsDeclKind> isUsesAllocatorsDecl(const Decl *D) const {
1082
422k
    const SharingMapTy &StackElem = getTopOfStack();
1083
422k
    auto I = StackElem.UsesAllocatorsDecls.find(D);
1084
422k
    if (I == StackElem.UsesAllocatorsDecls.end())
1085
422k
      return None;
1086
244
    return I->getSecond();
1087
244
  }
1088
1089
320
  void addDeclareMapperVarRef(Expr *Ref) {
1090
320
    SharingMapTy &StackElem = getTopOfStack();
1091
320
    StackElem.DeclareMapperVar = Ref;
1092
320
  }
1093
1.32M
  const Expr *getDeclareMapperVarRef() const {
1094
1.32M
    const SharingMapTy *Top = getTopOfStackOrNull();
1095
1.19M
    return Top ? Top->DeclareMapperVar : 
nullptr127k
;
1096
1.32M
  }
1097
};
1098
1099
6.51M
bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1100
6.51M
  return isOpenMPParallelDirective(DKind) || 
isOpenMPTeamsDirective(DKind)5.46M
;
1101
6.51M
}
1102
1103
5.97M
bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1104
5.97M
  return isImplicitTaskingRegion(DKind) || 
isOpenMPTaskingDirective(DKind)4.51M
||
1105
3.18M
         DKind == OMPD_unknown;
1106
5.97M
}
1107
1108
} // namespace
1109
1110
717k
static const Expr *getExprAsWritten(const Expr *E) {
1111
717k
  if (const auto *FE = dyn_cast<FullExpr>(E))
1112
194
    E = FE->getSubExpr();
1113
1114
717k
  if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
1115
0
    E = MTE->getSubExpr();
1116
1117
717k
  while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
1118
18
    E = Binder->getSubExpr();
1119
1120
717k
  if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
1121
217k
    E = ICE->getSubExprAsWritten();
1122
717k
  return E->IgnoreParens();
1123
717k
}
1124
1125
289k
static Expr *getExprAsWritten(Expr *E) {
1126
289k
  return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
1127
289k
}
1128
1129
19.3M
static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1130
19.3M
  if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
1131
136k
    if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
1132
29.3k
      D = ME->getMemberDecl();
1133
19.3M
  const auto *VD = dyn_cast<VarDecl>(D);
1134
19.3M
  const auto *FD = dyn_cast<FieldDecl>(D);
1135
19.3M
  if (VD != nullptr) {
1136
19.2M
    VD = VD->getCanonicalDecl();
1137
19.2M
    D = VD;
1138
111k
  } else {
1139
111k
    assert(FD);
1140
111k
    FD = FD->getCanonicalDecl();
1141
111k
    D = FD;
1142
111k
  }
1143
19.3M
  return D;
1144
19.3M
}
1145
1146
10.2M
static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1147
10.2M
  return const_cast<ValueDecl *>(
1148
10.2M
      getCanonicalDecl(const_cast<const ValueDecl *>(D)));
1149
10.2M
}
1150
1151
DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1152
3.58M
                                          ValueDecl *D) const {
1153
3.58M
  D = getCanonicalDecl(D);
1154
3.58M
  auto *VD = dyn_cast<VarDecl>(D);
1155
3.58M
  const auto *FD = dyn_cast<FieldDecl>(D);
1156
3.58M
  DSAVarData DVar;
1157
3.58M
  if (Iter == end()) {
1158
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1159
    // in a region but not in construct]
1160
    //  File-scope or namespace-scope variables referenced in called routines
1161
    //  in the region are shared unless they appear in a threadprivate
1162
    //  directive.
1163
953k
    if (VD && 
!VD->isFunctionOrMethodVarDecl()950k
&&
!isa<ParmVarDecl>(VD)686k
)
1164
506k
      DVar.CKind = OMPC_shared;
1165
1166
    // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1167
    // in a region but not in construct]
1168
    //  Variables with static storage duration that are declared in called
1169
    //  routines in the region are shared.
1170
953k
    if (VD && 
VD->hasGlobalStorage()950k
)
1171
509k
      DVar.CKind = OMPC_shared;
1172
1173
    // Non-static data members are shared by default.
1174
953k
    if (FD)
1175
3.19k
      DVar.CKind = OMPC_shared;
1176
1177
953k
    return DVar;
1178
953k
  }
1179
1180
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1181
  // in a Construct, C/C++, predetermined, p.1]
1182
  // Variables with automatic storage duration that are declared in a scope
1183
  // inside the construct are private.
1184
2.63M
  if (VD && 
isOpenMPLocal(VD, Iter)2.62M
&&
VD->isLocalVarDecl()2.69k
&&
1185
2.64k
      (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
1186
2.64k
    DVar.CKind = OMPC_private;
1187
2.64k
    return DVar;
1188
2.64k
  }
1189
1190
2.63M
  DVar.DKind = Iter->Directive;
1191
  // Explicitly specified attributes and local variables with predetermined
1192
  // attributes.
1193
2.63M
  if (Iter->SharingMap.count(D)) {
1194
30.6k
    const DSAInfo &Data = Iter->SharingMap.lookup(D);
1195
30.6k
    DVar.RefExpr = Data.RefExpr.getPointer();
1196
30.6k
    DVar.PrivateCopy = Data.PrivateCopy;
1197
30.6k
    DVar.CKind = Data.Attributes;
1198
30.6k
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1199
30.6k
    DVar.Modifier = Data.Modifier;
1200
30.6k
    DVar.AppliedToPointee = Data.AppliedToPointee;
1201
30.6k
    return DVar;
1202
30.6k
  }
1203
1204
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1205
  // in a Construct, C/C++, implicitly determined, p.1]
1206
  //  In a parallel or task construct, the data-sharing attributes of these
1207
  //  variables are determined by the default clause, if present.
1208
2.60M
  switch (Iter->DefaultAttr) {
1209
1.68k
  case DSA_shared:
1210
1.68k
    DVar.CKind = OMPC_shared;
1211
1.68k
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1212
1.68k
    return DVar;
1213
1.05k
  case DSA_none:
1214
1.05k
    return DVar;
1215
342
  case DSA_firstprivate:
1216
342
    if (VD->getStorageDuration() == SD_Static &&
1217
328
        VD->getDeclContext()->isFileContext()) {
1218
296
      DVar.CKind = OMPC_unknown;
1219
46
    } else {
1220
46
      DVar.CKind = OMPC_firstprivate;
1221
46
    }
1222
342
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1223
342
    return DVar;
1224
2.59M
  case DSA_unspecified:
1225
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1226
    // in a Construct, implicitly determined, p.2]
1227
    //  In a parallel construct, if no default clause is present, these
1228
    //  variables are shared.
1229
2.59M
    DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1230
2.59M
    if ((isOpenMPParallelDirective(DVar.DKind) &&
1231
259k
         !isOpenMPTaskLoopDirective(DVar.DKind)) ||
1232
2.37M
        isOpenMPTeamsDirective(DVar.DKind)) {
1233
388k
      DVar.CKind = OMPC_shared;
1234
388k
      return DVar;
1235
388k
    }
1236
1237
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1238
    // in a Construct, implicitly determined, p.4]
1239
    //  In a task construct, if no default clause is present, a variable that in
1240
    //  the enclosing context is determined to be shared by all implicit tasks
1241
    //  bound to the current team is shared.
1242
2.20M
    if (isOpenMPTaskingDirective(DVar.DKind)) {
1243
1.24M
      DSAVarData DVarTemp;
1244
1.24M
      const_iterator I = Iter, E = end();
1245
1.73M
      do {
1246
1.73M
        ++I;
1247
        // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1248
        // Referenced in a Construct, implicitly determined, p.6]
1249
        //  In a task construct, if no default clause is present, a variable
1250
        //  whose data-sharing attribute is not determined by the rules above is
1251
        //  firstprivate.
1252
1.73M
        DVarTemp = getDSA(I, D);
1253
1.73M
        if (DVarTemp.CKind != OMPC_shared) {
1254
707k
          DVar.RefExpr = nullptr;
1255
707k
          DVar.CKind = OMPC_firstprivate;
1256
707k
          return DVar;
1257
707k
        }
1258
1.02M
      } while (I != E && 
!isImplicitTaskingRegion(I->Directive)539k
);
1259
542k
      DVar.CKind =
1260
542k
          (DVarTemp.CKind == OMPC_unknown) ? 
OMPC_firstprivate0
: OMPC_shared;
1261
542k
      return DVar;
1262
959k
    }
1263
959k
  }
1264
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1265
  // in a Construct, implicitly determined, p.3]
1266
  //  For constructs other than task, if no default clause is present, these
1267
  //  variables inherit their data-sharing attributes from the enclosing
1268
  //  context.
1269
959k
  return getDSA(++Iter, D);
1270
959k
}
1271
1272
const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1273
1.81k
                                         const Expr *NewDE) {
1274
1.81k
  assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1275
1.81k
  D = getCanonicalDecl(D);
1276
1.81k
  SharingMapTy &StackElem = getTopOfStack();
1277
1.81k
  auto It = StackElem.AlignedMap.find(D);
1278
1.81k
  if (It == StackElem.AlignedMap.end()) {
1279
1.77k
    assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1280
1.77k
    StackElem.AlignedMap[D] = NewDE;
1281
1.77k
    return nullptr;
1282
1.77k
  }
1283
40
  assert(It->second && "Unexpected nullptr expr in the aligned map");
1284
40
  return It->second;
1285
40
}
1286
1287
const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1288
590
                                             const Expr *NewDE) {
1289
590
  assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1290
590
  D = getCanonicalDecl(D);
1291
590
  SharingMapTy &StackElem = getTopOfStack();
1292
590
  auto It = StackElem.NontemporalMap.find(D);
1293
590
  if (It == StackElem.NontemporalMap.end()) {
1294
562
    assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1295
562
    StackElem.NontemporalMap[D] = NewDE;
1296
562
    return nullptr;
1297
562
  }
1298
28
  assert(It->second && "Unexpected nullptr expr in the aligned map");
1299
28
  return It->second;
1300
28
}
1301
1302
162k
void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1303
162k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1304
162k
  D = getCanonicalDecl(D);
1305
162k
  SharingMapTy &StackElem = getTopOfStack();
1306
162k
  StackElem.LCVMap.try_emplace(
1307
162k
      D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1308
162k
}
1309
1310
const DSAStackTy::LCDeclInfo
1311
3.39M
DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1312
3.39M
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1313
3.39M
  D = getCanonicalDecl(D);
1314
3.39M
  const SharingMapTy &StackElem = getTopOfStack();
1315
3.39M
  auto It = StackElem.LCVMap.find(D);
1316
3.39M
  if (It != StackElem.LCVMap.end())
1317
5.12k
    return It->second;
1318
3.39M
  return {0, nullptr};
1319
3.39M
}
1320
1321
const DSAStackTy::LCDeclInfo
1322
132k
DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1323
132k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1324
132k
  D = getCanonicalDecl(D);
1325
853k
  for (unsigned I = Level + 1; I > 0; 
--I720k
) {
1326
724k
    const SharingMapTy &StackElem = getStackElemAtLevel(I - 1);
1327
724k
    auto It = StackElem.LCVMap.find(D);
1328
724k
    if (It != StackElem.LCVMap.end())
1329
3.02k
      return It->second;
1330
724k
  }
1331
129k
  return {0, nullptr};
1332
132k
}
1333
1334
const DSAStackTy::LCDeclInfo
1335
258
DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1336
258
  const SharingMapTy *Parent = getSecondOnStackOrNull();
1337
258
  assert(Parent && "Data-sharing attributes stack is empty");
1338
258
  D = getCanonicalDecl(D);
1339
258
  auto It = Parent->LCVMap.find(D);
1340
258
  if (It != Parent->LCVMap.end())
1341
252
    return It->second;
1342
6
  return {0, nullptr};
1343
6
}
1344
1345
132
const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1346
132
  const SharingMapTy *Parent = getSecondOnStackOrNull();
1347
132
  assert(Parent && "Data-sharing attributes stack is empty");
1348
132
  if (Parent->LCVMap.size() < I)
1349
12
    return nullptr;
1350
120
  for (const auto &Pair : Parent->LCVMap)
1351
180
    if (Pair.second.first == I)
1352
120
      return Pair.first;
1353
0
  return nullptr;
1354
120
}
1355
1356
void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1357
                        DeclRefExpr *PrivateCopy, unsigned Modifier,
1358
221k
                        bool AppliedToPointee) {
1359
221k
  D = getCanonicalDecl(D);
1360
221k
  if (A == OMPC_threadprivate) {
1361
2.63k
    DSAInfo &Data = Threadprivates[D];
1362
2.63k
    Data.Attributes = A;
1363
2.63k
    Data.RefExpr.setPointer(E);
1364
2.63k
    Data.PrivateCopy = nullptr;
1365
2.63k
    Data.Modifier = Modifier;
1366
218k
  } else {
1367
218k
    DSAInfo &Data = getTopOfStack().SharingMap[D];
1368
218k
    assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1369
218k
           (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1370
218k
           (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1371
218k
           (isLoopControlVariable(D).first && A == OMPC_private));
1372
218k
    Data.Modifier = Modifier;
1373
218k
    if (A == OMPC_lastprivate && 
Data.Attributes == OMPC_firstprivate10.6k
) {
1374
216
      Data.RefExpr.setInt(/*IntVal=*/true);
1375
216
      return;
1376
216
    }
1377
218k
    const bool IsLastprivate =
1378
218k
        A == OMPC_lastprivate || 
Data.Attributes == OMPC_lastprivate208k
;
1379
218k
    Data.Attributes = A;
1380
218k
    Data.RefExpr.setPointerAndInt(E, IsLastprivate);
1381
218k
    Data.PrivateCopy = PrivateCopy;
1382
218k
    Data.AppliedToPointee = AppliedToPointee;
1383
218k
    if (PrivateCopy) {
1384
2.72k
      DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1385
2.72k
      Data.Modifier = Modifier;
1386
2.72k
      Data.Attributes = A;
1387
2.72k
      Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
1388
2.72k
      Data.PrivateCopy = nullptr;
1389
2.72k
      Data.AppliedToPointee = AppliedToPointee;
1390
2.72k
    }
1391
218k
  }
1392
221k
}
1393
1394
/// Build a variable declaration for OpenMP loop iteration variable.
1395
static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1396
                             StringRef Name, const AttrVec *Attrs = nullptr,
1397
877k
                             DeclRefExpr *OrigRef = nullptr) {
1398
877k
  DeclContext *DC = SemaRef.CurContext;
1399
877k
  IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1400
877k
  TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
1401
877k
  auto *Decl =
1402
877k
      VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
1403
877k
  if (Attrs) {
1404
1.56k
    for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1405
2.19k
         I != E; 
++I636
)
1406
636
      Decl->addAttr(*I);
1407
1.56k
  }
1408
877k
  Decl->setImplicit();
1409
877k
  if (OrigRef) {
1410
213k
    Decl->addAttr(
1411
213k
        OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
1412
213k
  }
1413
877k
  return Decl;
1414
877k
}
1415
1416
static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1417
                                     SourceLocation Loc,
1418
1.70M
                                     bool RefersToCapture = false) {
1419
1.70M
  D->setReferenced();
1420
1.70M
  D->markUsed(S.Context);
1421
1.70M
  return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
1422
1.70M
                             SourceLocation(), D, RefersToCapture, Loc, Ty,
1423
1.70M
                             VK_LValue);
1424
1.70M
}
1425
1426
void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1427
1.92k
                                           BinaryOperatorKind BOK) {
1428
1.92k
  D = getCanonicalDecl(D);
1429
1.92k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1430
1.92k
  assert(
1431
1.92k
      getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1432
1.92k
      "Additional reduction info may be specified only for reduction items.");
1433
1.92k
  ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1434
1.92k
  assert(ReductionData.ReductionRange.isInvalid() &&
1435
1.92k
         (getTopOfStack().Directive == OMPD_taskgroup ||
1436
1.92k
          ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1437
1.92k
            isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1438
1.92k
           !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1439
1.92k
         "Additional reduction info may be specified only once for reduction "
1440
1.92k
         "items.");
1441
1.92k
  ReductionData.set(BOK, SR);
1442
1.92k
  Expr *&TaskgroupReductionRef =
1443
1.92k
      getTopOfStack().TaskgroupReductionRef;
1444
1.92k
  if (!TaskgroupReductionRef) {
1445
1.76k
    VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1446
1.76k
                               SemaRef.Context.VoidPtrTy, ".task_red.");
1447
1.76k
    TaskgroupReductionRef =
1448
1.76k
        buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1449
1.76k
  }
1450
1.92k
}
1451
1452
void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1453
30
                                           const Expr *ReductionRef) {
1454
30
  D = getCanonicalDecl(D);
1455
30
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1456
30
  assert(
1457
30
      getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1458
30
      "Additional reduction info may be specified only for reduction items.");
1459
30
  ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1460
30
  assert(ReductionData.ReductionRange.isInvalid() &&
1461
30
         (getTopOfStack().Directive == OMPD_taskgroup ||
1462
30
          ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1463
30
            isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1464
30
           !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1465
30
         "Additional reduction info may be specified only once for reduction "
1466
30
         "items.");
1467
30
  ReductionData.set(ReductionRef, SR);
1468
30
  Expr *&TaskgroupReductionRef =
1469
30
      getTopOfStack().TaskgroupReductionRef;
1470
30
  if (!TaskgroupReductionRef) {
1471
30
    VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1472
30
                               SemaRef.Context.VoidPtrTy, ".task_red.");
1473
30
    TaskgroupReductionRef =
1474
30
        buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
1475
30
  }
1476
30
}
1477
1478
const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1479
    const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1480
1.22k
    Expr *&TaskgroupDescriptor) const {
1481
1.22k
  D = getCanonicalDecl(D);
1482
1.22k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1483
1.37k
  for (const_iterator I = begin() + 1, E = end(); I != E; 
++I154
) {
1484
1.22k
    const DSAInfo &Data = I->SharingMap.lookup(D);
1485
1.22k
    if (Data.Attributes != OMPC_reduction ||
1486
1.07k
        Data.Modifier != OMPC_REDUCTION_task)
1487
154
      continue;
1488
1.06k
    const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1489
1.06k
    if (!ReductionData.ReductionOp ||
1490
1.06k
        ReductionData.ReductionOp.is<const Expr *>())
1491
30
      return DSAVarData();
1492
1.03k
    SR = ReductionData.ReductionRange;
1493
1.03k
    BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
1494
1.03k
    assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1495
1.03k
                                       "expression for the descriptor is not "
1496
1.03k
                                       "set.");
1497
1.03k
    TaskgroupDescriptor = I->TaskgroupReductionRef;
1498
1.03k
    return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1499
1.03k
                      Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1500
1.03k
                      /*AppliedToPointee=*/false);
1501
1.03k
  }
1502
158
  return DSAVarData();
1503
1.22k
}
1504
1505
const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1506
    const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1507
1.22k
    Expr *&TaskgroupDescriptor) const {
1508
1.22k
  D = getCanonicalDecl(D);
1509
1.22k
  assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1510
1.37k
  for (const_iterator I = begin() + 1, E = end(); I != E; 
++I154
) {
1511
1.22k
    const DSAInfo &Data = I->SharingMap.lookup(D);
1512
1.22k
    if (Data.Attributes != OMPC_reduction ||
1513
1.07k
        Data.Modifier != OMPC_REDUCTION_task)
1514
154
      continue;
1515
1.06k
    const ReductionData &ReductionData = I->ReductionMap.lookup(D);
1516
1.06k
    if (!ReductionData.ReductionOp ||
1517
1.06k
        !ReductionData.ReductionOp.is<const Expr *>())
1518
1.03k
      return DSAVarData();
1519
30
    SR = ReductionData.ReductionRange;
1520
30
    ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
1521
30
    assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1522
30
                                       "expression for the descriptor is not "
1523
30
                                       "set.");
1524
30
    TaskgroupDescriptor = I->TaskgroupReductionRef;
1525
30
    return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1526
30
                      Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1527
30
                      /*AppliedToPointee=*/false);
1528
30
  }
1529
158
  return DSAVarData();
1530
1.22k
}
1531
1532
2.64M
bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1533
2.64M
  D = D->getCanonicalDecl();
1534
5.36M
  for (const_iterator E = end(); I != E; 
++I2.72M
) {
1535
4.67M
    if (isImplicitOrExplicitTaskingRegion(I->Directive) ||
1536
2.90M
        isOpenMPTargetExecutionDirective(I->Directive)) {
1537
1.95M
      if (I->CurScope) {
1538
1.74M
        Scope *TopScope = I->CurScope->getParent();
1539
1.74M
        Scope *CurScope = getCurScope();
1540
23.0M
        while (CurScope && CurScope != TopScope && 
!CurScope->isDeclScope(D)21.2M
)
1541
21.2M
          CurScope = CurScope->getParent();
1542
1.74M
        return CurScope != TopScope;
1543
1.74M
      }
1544
636k
      
for (DeclContext *DC = D->getDeclContext(); 212k
DC;
DC = DC->getParent()423k
)
1545
425k
        if (I->Context == DC)
1546
1.41k
          return true;
1547
211k
      return false;
1548
212k
    }
1549
4.67M
  }
1550
686k
  return false;
1551
2.64M
}
1552
1553
static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1554
                                  bool AcceptIfMutable = true,
1555
76.4k
                                  bool *IsClassType = nullptr) {
1556
76.4k
  ASTContext &Context = SemaRef.getASTContext();
1557
76.4k
  Type = Type.getNonReferenceType().getCanonicalType();
1558
76.4k
  bool IsConstant = Type.isConstant(Context);
1559
76.4k
  Type = Context.getBaseElementType(Type);
1560
76.4k
  const CXXRecordDecl *RD = AcceptIfMutable && 
SemaRef.getLangOpts().CPlusPlus25.9k
1561
24.1k
                                ? Type->getAsCXXRecordDecl()
1562
52.2k
                                : nullptr;
1563
76.4k
  if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1564
2.19k
    if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1565
2.19k
      RD = CTD->getTemplatedDecl();
1566
76.4k
  if (IsClassType)
1567
76.4k
    *IsClassType = RD;
1568
76.4k
  return IsConstant && 
!(10.9k
SemaRef.getLangOpts().CPlusPlus10.9k
&&
RD10.9k
&&
1569
1.03k
                         RD->hasDefinition() && RD->hasMutableFields());
1570
76.4k
}
1571
1572
static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1573
                                      QualType Type, OpenMPClauseKind CKind,
1574
                                      SourceLocation ELoc,
1575
                                      bool AcceptIfMutable = true,
1576
76.4k
                                      bool ListItemNotVar = false) {
1577
76.4k
  ASTContext &Context = SemaRef.getASTContext();
1578
76.4k
  bool IsClassType;
1579
76.4k
  if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1580
10.2k
    unsigned Diag = ListItemNotVar
1581
0
                        ? diag::err_omp_const_list_item
1582
10.2k
                        : IsClassType ? 
diag::err_omp_const_not_mutable_variable296
1583
9.93k
                                      : diag::err_omp_const_variable;
1584
10.2k
    SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1585
10.2k
    if (!ListItemNotVar && D) {
1586
10.2k
      const VarDecl *VD = dyn_cast<VarDecl>(D);
1587
10.2k
      bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1588
10.2k
                               VarDecl::DeclarationOnly;
1589
10.2k
      SemaRef.Diag(D->getLocation(),
1590
7.40k
                   IsDecl ? 
diag::note_previous_decl2.81k
: diag::note_defined_here)
1591
10.2k
          << D;
1592
10.2k
    }
1593
10.2k
    return true;
1594
10.2k
  }
1595
66.1k
  return false;
1596
66.1k
}
1597
1598
const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1599
3.70M
                                                   bool FromParent) {
1600
3.70M
  D = getCanonicalDecl(D);
1601
3.70M
  DSAVarData DVar;
1602
1603
3.70M
  auto *VD = dyn_cast<VarDecl>(D);
1604
3.70M
  auto TI = Threadprivates.find(D);
1605
3.70M
  if (TI != Threadprivates.end()) {
1606
19.8k
    DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1607
19.8k
    DVar.CKind = OMPC_threadprivate;
1608
19.8k
    DVar.Modifier = TI->getSecond().Modifier;
1609
19.8k
    return DVar;
1610
19.8k
  }
1611
3.68M
  if (VD && 
VD->hasAttr<OMPThreadPrivateDeclAttr>()3.66M
) {
1612
8
    DVar.RefExpr = buildDeclRefExpr(
1613
8
        SemaRef, VD, D->getType().getNonReferenceType(),
1614
8
        VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1615
8
    DVar.CKind = OMPC_threadprivate;
1616
8
    addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1617
8
    return DVar;
1618
8
  }
1619
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1620
  // in a Construct, C/C++, predetermined, p.1]
1621
  //  Variables appearing in threadprivate directives are threadprivate.
1622
3.68M
  if ((VD && 
VD->getTLSKind() != VarDecl::TLS_None3.66M
&&
1623
6
       !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1624
0
         SemaRef.getLangOpts().OpenMPUseTLS &&
1625
0
         SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1626
3.68M
      (VD && 
VD->getStorageClass() == SC_Register3.66M
&&
1627
224
       VD->hasAttr<AsmLabelAttr>() && 
!VD->isLocalVarDecl()0
)) {
1628
6
    DVar.RefExpr = buildDeclRefExpr(
1629
6
        SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1630
6
    DVar.CKind = OMPC_threadprivate;
1631
6
    addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1632
6
    return DVar;
1633
6
  }
1634
3.68M
  if (SemaRef.getLangOpts().OpenMPCUDAMode && 
VD19.6k
&&
1635
19.6k
      VD->isLocalVarDeclOrParm() && 
!isStackEmpty()19.5k
&&
1636
19.5k
      !isLoopControlVariable(D).first) {
1637
19.0k
    const_iterator IterTarget =
1638
19.2k
        std::find_if(begin(), end(), [](const SharingMapTy &Data) {
1639
19.2k
          return isOpenMPTargetExecutionDirective(Data.Directive);
1640
19.2k
        });
1641
19.0k
    if (IterTarget != end()) {
1642
19.0k
      const_iterator ParentIterTarget = IterTarget + 1;
1643
19.0k
      for (const_iterator Iter = begin();
1644
38.3k
           Iter != ParentIterTarget; 
++Iter19.2k
) {
1645
19.2k
        if (isOpenMPLocal(VD, Iter)) {
1646
13
          DVar.RefExpr =
1647
13
              buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1648
13
                               D->getLocation());
1649
13
          DVar.CKind = OMPC_threadprivate;
1650
13
          return DVar;
1651
13
        }
1652
19.2k
      }
1653
19.0k
      if (!isClauseParsingMode() || 
IterTarget != begin()627
) {
1654
18.5k
        auto DSAIter = IterTarget->SharingMap.find(D);
1655
18.5k
        if (DSAIter != IterTarget->SharingMap.end() &&
1656
2.74k
            isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1657
2.74k
          DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1658
2.74k
          DVar.CKind = OMPC_threadprivate;
1659
2.74k
          return DVar;
1660
2.74k
        }
1661
15.7k
        const_iterator End = end();
1662
15.7k
        if (!SemaRef.isOpenMPCapturedByRef(
1663
15.7k
                D, std::distance(ParentIterTarget, End),
1664
4.97k
                /*OpenMPCaptureLevel=*/0)) {
1665
4.97k
          DVar.RefExpr =
1666
4.97k
              buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1667
4.97k
                               IterTarget->ConstructLoc);
1668
4.97k
          DVar.CKind = OMPC_threadprivate;
1669
4.97k
          return DVar;
1670
4.97k
        }
1671
3.67M
      }
1672
19.0k
    }
1673
19.0k
  }
1674
1675
3.67M
  if (isStackEmpty())
1676
    // Not in OpenMP execution region and top scope was already checked.
1677
1.02k
    return DVar;
1678
1679
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1680
  // in a Construct, C/C++, predetermined, p.4]
1681
  //  Static data members are shared.
1682
  // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1683
  // in a Construct, C/C++, predetermined, p.7]
1684
  //  Variables with static storage duration that are declared in a scope
1685
  //  inside the construct are shared.
1686
3.67M
  if (VD && 
VD->isStaticDataMember()3.65M
) {
1687
    // Check for explicitly specified attributes.
1688
13.0k
    const_iterator I = begin();
1689
13.0k
    const_iterator EndI = end();
1690
13.0k
    if (FromParent && 
I != EndI962
)
1691
962
      ++I;
1692
13.0k
    if (I != EndI) {
1693
13.0k
      auto It = I->SharingMap.find(D);
1694
13.0k
      if (It != I->SharingMap.end()) {
1695
1.65k
        const DSAInfo &Data = It->getSecond();
1696
1.65k
        DVar.RefExpr = Data.RefExpr.getPointer();
1697
1.65k
        DVar.PrivateCopy = Data.PrivateCopy;
1698
1.65k
        DVar.CKind = Data.Attributes;
1699
1.65k
        DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1700
1.65k
        DVar.DKind = I->Directive;
1701
1.65k
        DVar.Modifier = Data.Modifier;
1702
1.65k
        DVar.AppliedToPointee = Data.AppliedToPointee;
1703
1.65k
        return DVar;
1704
1.65k
      }
1705
11.3k
    }
1706
1707
11.3k
    DVar.CKind = OMPC_shared;
1708
11.3k
    return DVar;
1709
11.3k
  }
1710
1711
3.66M
  auto &&MatchesAlways = [](OpenMPDirectiveKind) 
{ return true; }16
;
1712
  // The predetermined shared attribute for const-qualified types having no
1713
  // mutable members was removed after OpenMP 3.1.
1714
3.66M
  if (SemaRef.LangOpts.OpenMP <= 31) {
1715
    // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1716
    // in a Construct, C/C++, predetermined, p.6]
1717
    //  Variables with const qualified type having no mutable member are
1718
    //  shared.
1719
64
    if (isConstNotMutableType(SemaRef, D->getType())) {
1720
      // Variables with const-qualified type having no mutable member may be
1721
      // listed in a firstprivate clause, even if they are static data members.
1722
16
      DSAVarData DVarTemp = hasInnermostDSA(
1723
16
          D,
1724
16
          [](OpenMPClauseKind C, bool) {
1725
16
            return C == OMPC_firstprivate || C == OMPC_shared;
1726
16
          },
1727
16
          MatchesAlways, FromParent);
1728
16
      if (DVarTemp.CKind != OMPC_unknown && 
DVarTemp.RefExpr0
)
1729
0
        return DVarTemp;
1730
1731
16
      DVar.CKind = OMPC_shared;
1732
16
      return DVar;
1733
16
    }
1734
64
  }
1735
1736
  // Explicitly specified attributes and local variables with predetermined
1737
  // attributes.
1738
3.66M
  const_iterator I = begin();
1739
3.66M
  const_iterator EndI = end();
1740
3.66M
  if (FromParent && 
I != EndI87.4k
)
1741
87.4k
    ++I;
1742
3.66M
  if (I == EndI)
1743
6
    return DVar;
1744
3.66M
  auto It = I->SharingMap.find(D);
1745
3.66M
  if (It != I->SharingMap.end()) {
1746
393k
    const DSAInfo &Data = It->getSecond();
1747
393k
    DVar.RefExpr = Data.RefExpr.getPointer();
1748
393k
    DVar.PrivateCopy = Data.PrivateCopy;
1749
393k
    DVar.CKind = Data.Attributes;
1750
393k
    DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1751
393k
    DVar.DKind = I->Directive;
1752
393k
    DVar.Modifier = Data.Modifier;
1753
393k
    DVar.AppliedToPointee = Data.AppliedToPointee;
1754
393k
  }
1755
1756
3.66M
  return DVar;
1757
3.66M
}
1758
1759
const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1760
83.6k
                                                        bool FromParent) const {
1761
83.6k
  if (isStackEmpty()) {
1762
0
    const_iterator I;
1763
0
    return getDSA(I, D);
1764
0
  }
1765
83.6k
  D = getCanonicalDecl(D);
1766
83.6k
  const_iterator StartI = begin();
1767
83.6k
  const_iterator EndI = end();
1768
83.6k
  if (FromParent && 
StartI != EndI4.94k
)
1769
4.94k
    ++StartI;
1770
83.6k
  return getDSA(StartI, D);
1771
83.6k
}
1772
1773
const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1774
188k
                                                        unsigned Level) const {
1775
188k
  if (getStackSize() <= Level)
1776
0
    return DSAVarData();
1777
188k
  D = getCanonicalDecl(D);
1778
188k
  const_iterator StartI = std::next(begin(), getStackSize() - 1 - Level);
1779
188k
  return getDSA(StartI, D);
1780
188k
}
1781
1782
const DSAStackTy::DSAVarData
1783
DSAStackTy::hasDSA(ValueDecl *D,
1784
                   const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1785
                   const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1786
307k
                   bool FromParent) const {
1787
307k
  if (isStackEmpty())
1788
0
    return {};
1789
307k
  D = getCanonicalDecl(D);
1790
307k
  const_iterator I = begin();
1791
307k
  const_iterator EndI = end();
1792
307k
  if (FromParent && 
I != EndI92.5k
)
1793
92.5k
    ++I;
1794
903k
  for (; I != EndI; 
++I596k
) {
1795
604k
    if (!DPred(I->Directive) &&
1796
0
        !isImplicitOrExplicitTaskingRegion(I->Directive))
1797
0
      continue;
1798
604k
    const_iterator NewI = I;
1799
604k
    DSAVarData DVar = getDSA(NewI, D);
1800
604k
    if (I == NewI && 
CPred(DVar.CKind, DVar.AppliedToPointee)175k
)
1801
7.84k
      return DVar;
1802
604k
  }
1803
299k
  return {};
1804
307k
}
1805
1806
const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1807
    ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1808
    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1809
80.1k
    bool FromParent) const {
1810
80.1k
  if (isStackEmpty())
1811
0
    return {};
1812
80.1k
  D = getCanonicalDecl(D);
1813
80.1k
  const_iterator StartI = begin();
1814
80.1k
  const_iterator EndI = end();
1815
80.1k
  if (FromParent && 
StartI != EndI80.1k
)
1816
80.1k
    ++StartI;
1817
80.1k
  if (StartI == EndI || 
!DPred(StartI->Directive)46.9k
)
1818
60.4k
    return {};
1819
19.7k
  const_iterator NewI = StartI;
1820
19.7k
  DSAVarData DVar = getDSA(NewI, D);
1821
19.7k
  return (NewI == StartI && 
CPred(DVar.CKind, DVar.AppliedToPointee)19.4k
)
1822
146
             ? DVar
1823
19.5k
             : DSAVarData();
1824
19.7k
}
1825
1826
bool DSAStackTy::hasExplicitDSA(
1827
    const ValueDecl *D,
1828
    const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
1829
4.44M
    unsigned Level, bool NotLastprivate) const {
1830
4.44M
  if (getStackSize() <= Level)
1831
0
    return false;
1832
4.44M
  D = getCanonicalDecl(D);
1833
4.44M
  const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1834
4.44M
  auto I = StackElem.SharingMap.find(D);
1835
4.44M
  if (I != StackElem.SharingMap.end() && 
I->getSecond().RefExpr.getPointer()461k
&&
1836
461k
      CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) &&
1837
156k
      (!NotLastprivate || 
!I->getSecond().RefExpr.getInt()13.2k
))
1838
155k
    return true;
1839
  // Check predetermined rules for the loop control variables.
1840
4.29M
  auto LI = StackElem.LCVMap.find(D);
1841
4.29M
  if (LI != StackElem.LCVMap.end())
1842
27.3k
    return CPred(OMPC_private, /*AppliedToPointee=*/false);
1843
4.26M
  return false;
1844
4.26M
}
1845
1846
bool DSAStackTy::hasExplicitDirective(
1847
    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1848
9.61M
    unsigned Level) const {
1849
9.61M
  if (getStackSize() <= Level)
1850
0
    return false;
1851
9.61M
  const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1852
9.61M
  return DPred(StackElem.Directive);
1853
9.61M
}
1854
1855
bool DSAStackTy::hasDirective(
1856
    const llvm::function_ref<bool(OpenMPDirectiveKind,
1857
                                  const DeclarationNameInfo &, SourceLocation)>
1858
        DPred,
1859
180k
    bool FromParent) const {
1860
  // We look only in the enclosing region.
1861
180k
  size_t Skip = FromParent ? 
20
: 1;
1862
180k
  for (const_iterator I = begin() + std::min(Skip, getStackSize()), E = end();
1863
282k
       I != E; 
++I102k
) {
1864
219k
    if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1865
117k
      return true;
1866
219k
  }
1867
62.9k
  return false;
1868
180k
}
1869
1870
78.9k
void Sema::InitDataSharingAttributesStack() {
1871
78.9k
  VarDataSharingAttributesStack = new DSAStackTy(*this);
1872
78.9k
}
1873
1874
64.8M
#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1875
1876
96.5k
void Sema::pushOpenMPFunctionRegion() {
1877
96.5k
  DSAStack->pushFunction();
1878
96.5k
}
1879
1880
653k
void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1881
653k
  DSAStack->popFunction(OldFSI);
1882
653k
}
1883
1884
147
static bool isOpenMPDeviceDelayedContext(Sema &S) {
1885
147
  assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1886
147
         "Expected OpenMP device compilation.");
1887
147
  return !S.isInOpenMPTargetExecutionDirective() &&
1888
112
         !S.isInOpenMPDeclareTargetContext();
1889
147
}
1890
1891
namespace {
1892
/// Status of the function emission on the host/device.
1893
enum class FunctionEmissionStatus {
1894
  Emitted,
1895
  Discarded,
1896
  Unknown,
1897
};
1898
} // anonymous namespace
1899
1900
Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1901
190
                                                         unsigned DiagID) {
1902
190
  assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1903
190
         "Expected OpenMP device compilation.");
1904
1905
190
  FunctionDecl *FD = getCurFunctionDecl();
1906
190
  SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
1907
190
  if (FD) {
1908
173
    FunctionEmissionStatus FES = getEmissionStatus(FD);
1909
173
    switch (FES) {
1910
23
    case FunctionEmissionStatus::Emitted:
1911
23
      Kind = SemaDiagnosticBuilder::K_Immediate;
1912
23
      break;
1913
147
    case FunctionEmissionStatus::Unknown:
1914
147
      Kind = isOpenMPDeviceDelayedContext(*this)
1915
112
                 ? SemaDiagnosticBuilder::K_Deferred
1916
35
                 : SemaDiagnosticBuilder::K_Immediate;
1917
147
      break;
1918
3
    case FunctionEmissionStatus::TemplateDiscarded:
1919
3
    case FunctionEmissionStatus::OMPDiscarded:
1920
3
      Kind = SemaDiagnosticBuilder::K_Nop;
1921
3
      break;
1922
0
    case FunctionEmissionStatus::CUDADiscarded:
1923
0
      llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
1924
0
      break;
1925
190
    }
1926
190
  }
1927
1928
190
  return SemaDiagnosticBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1929
190
}
1930
1931
Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
1932
0
                                                       unsigned DiagID) {
1933
0
  assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
1934
0
         "Expected OpenMP host compilation.");
1935
0
  FunctionEmissionStatus FES = getEmissionStatus(getCurFunctionDecl());
1936
0
  SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
1937
0
  switch (FES) {
1938
0
  case FunctionEmissionStatus::Emitted:
1939
0
    Kind = SemaDiagnosticBuilder::K_Immediate;
1940
0
    break;
1941
0
  case FunctionEmissionStatus::Unknown:
1942
0
    Kind = SemaDiagnosticBuilder::K_Deferred;
1943
0
    break;
1944
0
  case FunctionEmissionStatus::TemplateDiscarded:
1945
0
  case FunctionEmissionStatus::OMPDiscarded:
1946
0
  case FunctionEmissionStatus::CUDADiscarded:
1947
0
    Kind = SemaDiagnosticBuilder::K_Nop;
1948
0
    break;
1949
0
  }
1950
1951
0
  return SemaDiagnosticBuilder(Kind, Loc, DiagID, getCurFunctionDecl(), *this);
1952
0
}
1953
1954
static OpenMPDefaultmapClauseKind
1955
909k
getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
1956
909k
  if (LO.OpenMP <= 45) {
1957
304k
    if (VD->getType().getNonReferenceType()->isScalarType())
1958
270k
      return OMPC_DEFAULTMAP_scalar;
1959
33.2k
    return OMPC_DEFAULTMAP_aggregate;
1960
33.2k
  }
1961
605k
  if (VD->getType().getNonReferenceType()->isAnyPointerType())
1962
149k
    return OMPC_DEFAULTMAP_pointer;
1963
455k
  if (VD->getType().getNonReferenceType()->isScalarType())
1964
409k
    return OMPC_DEFAULTMAP_scalar;
1965
46.2k
  return OMPC_DEFAULTMAP_aggregate;
1966
46.2k
}
1967
1968
bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
1969
1.30M
                                 unsigned OpenMPCaptureLevel) const {
1970
1.30M
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
1971
1972
1.30M
  ASTContext &Ctx = getASTContext();
1973
1.30M
  bool IsByRef = true;
1974
1975
  // Find the directive that is associated with the provided scope.
1976
1.30M
  D = cast<ValueDecl>(D->getCanonicalDecl());
1977
1.30M
  QualType Ty = D->getType();
1978
1979
1.30M
  bool IsVariableUsedInMapClause = false;
1980
1.30M
  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1981
    // This table summarizes how a given variable should be passed to the device
1982
    // given its type and the clauses where it appears. This table is based on
1983
    // the description in OpenMP 4.5 [2.10.4, target Construct] and
1984
    // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1985
    //
1986
    // =========================================================================
1987
    // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
1988
    // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
1989
    // =========================================================================
1990
    // | scl  |               |     |       |       -       |          | bycopy|
1991
    // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
1992
    // | scl  |               |  x  |   -   |       -       |     -    | null  |
1993
    // | scl  |       x       |     |       |       -       |          | byref |
1994
    // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
1995
    // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
1996
    // | scl  |               |  -  |   -   |       -       |     x    | byref |
1997
    // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
1998
    //
1999
    // | agg  |      n.a.     |     |       |       -       |          | byref |
2000
    // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
2001
    // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
2002
    // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
2003
    // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
2004
    //
2005
    // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
2006
    // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
2007
    // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
2008
    // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
2009
    // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
2010
    // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
2011
    // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
2012
    // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
2013
    // =========================================================================
2014
    // Legend:
2015
    //  scl - scalar
2016
    //  ptr - pointer
2017
    //  agg - aggregate
2018
    //  x - applies
2019
    //  - - invalid in this combination
2020
    //  [] - mapped with an array section
2021
    //  byref - should be mapped by reference
2022
    //  byval - should be mapped by value
2023
    //  null - initialize a local variable to null on the device
2024
    //
2025
    // Observations:
2026
    //  - All scalar declarations that show up in a map clause have to be passed
2027
    //    by reference, because they may have been mapped in the enclosing data
2028
    //    environment.
2029
    //  - If the scalar value does not fit the size of uintptr, it has to be
2030
    //    passed by reference, regardless the result in the table above.
2031
    //  - For pointers mapped by value that have either an implicit map or an
2032
    //    array section, the runtime library may pass the NULL value to the
2033
    //    device instead of the value passed to it by the compiler.
2034
2035
875k
    if (Ty->isReferenceType())
2036
28.0k
      Ty = Ty->castAs<ReferenceType>()->getPointeeType();
2037
2038
    // Locate map clauses and see if the variable being captured is referred to
2039
    // in any of those clauses. Here we only care about variables, not fields,
2040
    // because fields are part of aggregates.
2041
875k
    bool IsVariableAssociatedWithSection = false;
2042
2043
875k
    DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2044
875k
        D, Level,
2045
875k
        [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
2046
875k
            OMPClauseMappableExprCommon::MappableExprComponentListRef
2047
875k
                MapExprComponents,
2048
26.7k
            OpenMPClauseKind WhereFoundClauseKind) {
2049
          // Only the map clause information influences how a variable is
2050
          // captured. E.g. is_device_ptr does not require changing the default
2051
          // behavior.
2052
26.7k
          if (WhereFoundClauseKind != OMPC_map)
2053
1.71k
            return false;
2054
2055
25.0k
          auto EI = MapExprComponents.rbegin();
2056
25.0k
          auto EE = MapExprComponents.rend();
2057
2058
25.0k
          assert(EI != EE && "Invalid map expression!");
2059
2060
25.0k
          if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
2061
25.0k
            IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
2062
2063
25.0k
          ++EI;
2064
25.0k
          if (EI == EE)
2065
16.5k
            return false;
2066
2067
8.54k
          if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
2068
7.10k
              isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
2069
2.32k
              isa<MemberExpr>(EI->getAssociatedExpression()) ||
2070
8.54k
              
isa<OMPArrayShapingExpr>(EI->getAssociatedExpression())96
) {
2071
8.54k
            IsVariableAssociatedWithSection = true;
2072
            // There is nothing more we need to know about this variable.
2073
8.54k
            return true;
2074
8.54k
          }
2075
2076
          // Keep looking for more map info.
2077
0
          return false;
2078
0
        });
2079
2080
875k
    if (IsVariableUsedInMapClause) {
2081
      // If variable is identified in a map clause it is always captured by
2082
      // reference except if it is a pointer that is dereferenced somehow.
2083
24.9k
      IsByRef = !(Ty->isPointerType() && 
IsVariableAssociatedWithSection4.50k
);
2084
850k
    } else {
2085
      // By default, all the data that has a scalar type is mapped by copy
2086
      // (except for reduction variables).
2087
      // Defaultmap scalar is mutual exclusive to defaultmap pointer
2088
850k
      IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2089
96
                 !Ty->isAnyPointerType()) ||
2090
850k
                !Ty->isScalarType() ||
2091
581k
                DSAStack->isDefaultmapCapturedByRef(
2092
581k
                    Level, getVariableCategoryFromDecl(LangOpts, D)) ||
2093
576k
                DSAStack->hasExplicitDSA(
2094
576k
                    D,
2095
33.8k
                    [](OpenMPClauseKind K, bool AppliedToPointee) {
2096
33.8k
                      return K == OMPC_reduction && 
!AppliedToPointee10.4k
;
2097
33.8k
                    },
2098
576k
                    Level);
2099
850k
    }
2100
875k
  }
2101
2102
1.30M
  if (IsByRef && 
Ty.getNonReferenceType()->isScalarType()733k
) {
2103
304k
    IsByRef =
2104
304k
        ((IsVariableUsedInMapClause &&
2105
10.0k
          DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2106
10.0k
              OMPD_target) ||
2107
301k
         !(DSAStack->hasExplicitDSA(
2108
301k
               D,
2109
49.9k
               [](OpenMPClauseKind K, bool AppliedToPointee) -> bool {
2110
49.9k
                 return K == OMPC_firstprivate ||
2111
43.6k
                        (K == OMPC_reduction && 
AppliedToPointee27.2k
);
2112
49.9k
               },
2113
301k
               Level, /*NotLastprivate=*/true) ||
2114
295k
           DSAStack->isUsesAllocatorsDecl(Level, D))) &&
2115
        // If the variable is artificial and must be captured by value - try to
2116
        // capture by value.
2117
298k
        !(isa<OMPCapturedExprDecl>(D) && 
!D->hasAttr<OMPCaptureNoInitAttr>()3.46k
&&
2118
3.32k
          !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue()) &&
2119
        // If the variable is implicitly firstprivate and scalar - capture by
2120
        // copy
2121
296k
        !(DSAStack->getDefaultDSA() == DSA_firstprivate &&
2122
180
          !DSAStack->hasExplicitDSA(
2123
0
              D, [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; },
2124
180
              Level) &&
2125
180
          !DSAStack->isLoopControlVariable(D, Level).first);
2126
304k
  }
2127
2128
  // When passing data by copy, we need to make sure it fits the uintptr size
2129
  // and alignment, because the runtime library only deals with uintptr types.
2130
  // If it does not fit the uintptr size, we need to pass the data by reference
2131
  // instead.
2132
1.30M
  if (!IsByRef &&
2133
578k
      (Ctx.getTypeSizeInChars(Ty) >
2134
578k
           Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
2135
576k
       Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
2136
2.13k
    IsByRef = true;
2137
2.13k
  }
2138
2139
1.30M
  return IsByRef;
2140
1.30M
}
2141
2142
555k
unsigned Sema::getOpenMPNestingLevel() const {
2143
555k
  assert(getLangOpts().OpenMP);
2144
555k
  return DSAStack->getNestingLevel();
2145
555k
}
2146
2147
266k
bool Sema::isInOpenMPTargetExecutionDirective() const {
2148
266k
  return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2149
94.3k
          !DSAStack->isClauseParsingMode()) ||
2150
173k
         DSAStack->hasDirective(
2151
173k
             [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2152
206k
                SourceLocation) -> bool {
2153
206k
               return isOpenMPTargetExecutionDirective(K);
2154
206k
             },
2155
173k
             false);
2156
266k
}
2157
2158
VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2159
1.69M
                                    unsigned StopAt) {
2160
1.69M
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2161
1.69M
  D = getCanonicalDecl(D);
2162
2163
1.69M
  auto *VD = dyn_cast<VarDecl>(D);
2164
  // Do not capture constexpr variables.
2165
1.69M
  if (VD && 
VD->isConstexpr()1.67M
)
2166
40
    return nullptr;
2167
2168
  // If we want to determine whether the variable should be captured from the
2169
  // perspective of the current capturing scope, and we've already left all the
2170
  // capturing scopes of the top directive on the stack, check from the
2171
  // perspective of its parent directive (if any) instead.
2172
1.69M
  DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2173
1.69M
      *DSAStack, CheckScopeInfo && 
DSAStack281k
->isBodyComplete()281k
);
2174
2175
  // If we are attempting to capture a global variable in a directive with
2176
  // 'target' we return true so that this global is also mapped to the device.
2177
  //
2178
1.69M
  if (VD && 
!VD->hasLocalStorage()1.67M
&&
2179
358k
      (getCurCapturedRegion() || 
getCurBlock()156k
||
getCurLambda()154k
)) {
2180
208k
    if (isInOpenMPDeclareTargetContext()) {
2181
      // Try to mark variable as declare target if it is used in capturing
2182
      // regions.
2183
97
      if (LangOpts.OpenMP <= 45 &&
2184
30
          !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2185
10
        checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
2186
97
      return nullptr;
2187
97
    }
2188
208k
    if (isInOpenMPTargetExecutionDirective()) {
2189
      // If the declaration is enclosed in a 'declare target' directive,
2190
      // then it should not be captured.
2191
      //
2192
164k
      if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2193
318
        return nullptr;
2194
163k
      CapturedRegionScopeInfo *CSI = nullptr;
2195
163k
      for (FunctionScopeInfo *FSI : llvm::drop_begin(
2196
163k
               llvm::reverse(FunctionScopes),
2197
166k
               CheckScopeInfo ? 
(FunctionScopes.size() - (StopAt + 1))93.6k
:
070.3k
)) {
2198
166k
        if (!isa<CapturingScopeInfo>(FSI))
2199
56
          return nullptr;
2200
166k
        if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2201
163k
          if (RSI->CapRegionKind == CR_OpenMP) {
2202
163k
            CSI = RSI;
2203
163k
            break;
2204
163k
          }
2205
166k
      }
2206
163k
      assert(CSI && "Failed to find CapturedRegionScopeInfo");
2207
163k
      SmallVector<OpenMPDirectiveKind, 4> Regions;
2208
163k
      getOpenMPCaptureRegions(Regions,
2209
163k
                              DSAStack->getDirective(CSI->OpenMPLevel));
2210
163k
      if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2211
138k
        return VD;
2212
1.55M
    }
2213
208k
  }
2214
2215
1.55M
  if (CheckScopeInfo) {
2216
212k
    bool OpenMPFound = false;
2217
215k
    for (unsigned I = StopAt + 1; I > 0; 
--I3.31k
) {
2218
215k
      FunctionScopeInfo *FSI = FunctionScopes[I - 1];
2219
215k
      if(!isa<CapturingScopeInfo>(FSI))
2220
152k
        return nullptr;
2221
63.3k
      if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(FSI))
2222
59.9k
        if (RSI->CapRegionKind == CR_OpenMP) {
2223
59.9k
          OpenMPFound = true;
2224
59.9k
          break;
2225
59.9k
        }
2226
63.3k
    }
2227
60.5k
    if (!OpenMPFound)
2228
522
      return nullptr;
2229
1.40M
  }
2230
2231
1.40M
  if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2232
1.39M
      (!DSAStack->isClauseParsingMode() ||
2233
1.37M
       
DSAStack283k
->getParentDirective() != OMPD_unknown283k
)) {
2234
1.37M
    auto &&Info = DSAStack->isLoopControlVariable(D);
2235
1.37M
    if (Info.first ||
2236
1.37M
        (VD && 
VD->hasLocalStorage()1.35M
&&
2237
1.29M
         isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2238
351k
        (VD && 
DSAStack340k
->isForceVarCapturing()340k
))
2239
1.02M
      return VD ? 
VD1.02M
:
Info.second716
;
2240
351k
    DSAStackTy::DSAVarData DVarTop =
2241
351k
        DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2242
351k
    if (DVarTop.CKind != OMPC_unknown && 
isOpenMPPrivate(DVarTop.CKind)55.0k
&&
2243
48.1k
        (!VD || 
VD->hasLocalStorage()42.9k
||
!DVarTop.AppliedToPointee26.4k
))
2244
48.0k
      return VD ? 
VD42.8k
:
cast<VarDecl>(DVarTop.PrivateCopy->getDecl())5.21k
;
2245
    // Threadprivate variables must not be captured.
2246
303k
    if (isOpenMPThreadPrivate(DVarTop.CKind))
2247
4.15k
      return nullptr;
2248
    // The variable is not private or it is the variable in the directive with
2249
    // default(none) clause and not used in any clause.
2250
299k
    DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2251
299k
        D,
2252
171k
        [](OpenMPClauseKind C, bool AppliedToPointee) {
2253
171k
          return isOpenMPPrivate(C) && 
!AppliedToPointee4.99k
;
2254
171k
        },
2255
598k
        [](OpenMPDirectiveKind) { return true; },
2256
299k
        DSAStack->isClauseParsingMode());
2257
    // Global shared must not be captured.
2258
299k
    if (VD && 
!VD->hasLocalStorage()293k
&&
DVarPrivate.CKind == OMPC_unknown35.3k
&&
2259
35.0k
        ((DSAStack->getDefaultDSA() != DSA_none &&
2260
34.5k
          DSAStack->getDefaultDSA() != DSA_firstprivate) ||
2261
550
         DVarTop.CKind == OMPC_shared))
2262
34.6k
      return nullptr;
2263
264k
    if (DVarPrivate.CKind != OMPC_unknown ||
2264
260k
        (VD && 
(255k
DSAStack255k
->getDefaultDSA() == DSA_none255k
||
2265
255k
                DSAStack->getDefaultDSA() == DSA_firstprivate)))
2266
5.26k
      return VD ? 
VD3.84k
:
cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl())1.42k
;
2267
292k
  }
2268
292k
  return nullptr;
2269
292k
}
2270
2271
void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2272
25.1k
                                        unsigned Level) const {
2273
25.1k
  FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2274
25.1k
}
2275
2276
167k
void Sema::startOpenMPLoop() {
2277
167k
  assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2278
167k
  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2279
165k
    DSAStack->loopInit();
2280
167k
}
2281
2282
118
void Sema::startOpenMPCXXRangeFor() {
2283
118
  assert(LangOpts.OpenMP && "OpenMP must be enabled.");
2284
118
  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2285
118
    DSAStack->resetPossibleLoopCounter();
2286
118
    DSAStack->loopStart();
2287
118
  }
2288
118
}
2289
2290
OpenMPClauseKind Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2291
2.85M
                                           unsigned CapLevel) const {
2292
2.85M
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2293
2.85M
  if (DSAStack->hasExplicitDirective(
2294
2.85M
          [](OpenMPDirectiveKind K) { return isOpenMPTaskingDirective(K); },
2295
253k
          Level)) {
2296
253k
    bool IsTriviallyCopyable =
2297
253k
        D->getType().getNonReferenceType().isTriviallyCopyableType(Context) &&
2298
207k
        !D->getType()
2299
207k
             .getNonReferenceType()
2300
207k
             .getCanonicalType()
2301
207k
             ->getAsCXXRecordDecl();
2302
253k
    OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2303
253k
    SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2304
253k
    getOpenMPCaptureRegions(CaptureRegions, DKind);
2305
253k
    if (isOpenMPTaskingDirective(CaptureRegions[CapLevel]) &&
2306
220k
        (IsTriviallyCopyable ||
2307
190k
         
!isOpenMPTaskLoopDirective(CaptureRegions[CapLevel])38.6k
)) {
2308
190k
      if (DSAStack->hasExplicitDSA(
2309
190k
              D,
2310
27.8k
              [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; },
2311
190k
              Level, /*NotLastprivate=*/true))
2312
6.42k
        return OMPC_firstprivate;
2313
183k
      DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2314
183k
      if (DVar.CKind != OMPC_shared &&
2315
132k
          !DSAStack->isLoopControlVariable(D, Level).first && 
!DVar.RefExpr129k
) {
2316
115k
        DSAStack->addImplicitTaskFirstprivate(Level, D);
2317
115k
        return OMPC_firstprivate;
2318
115k
      }
2319
2.73M
    }
2320
253k
  }
2321
2.73M
  if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2322
2.35M
    if (DSAStack->getAssociatedLoops() > 0 &&
2323
1.13M
        !DSAStack->isLoopStarted()) {
2324
43.5k
      DSAStack->resetPossibleLoopCounter(D);
2325
43.5k
      DSAStack->loopStart();
2326
43.5k
      return OMPC_private;
2327
43.5k
    }
2328
2.30M
    if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
2329
1.88M
         DSAStack->isLoopControlVariable(D).first) &&
2330
420k
        !DSAStack->hasExplicitDSA(
2331
59.0k
            D, [](OpenMPClauseKind K, bool) { return K != OMPC_private; },
2332
420k
            Level) &&
2333
409k
        !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2334
97.5k
      return OMPC_private;
2335
2.59M
  }
2336
2.59M
  if (const auto *VD = dyn_cast<VarDecl>(D)) {
2337
2.59M
    if (DSAStack->isThreadPrivate(const_cast<VarDecl *>(VD)) &&
2338
17.0k
        DSAStack->isForceVarCapturing() &&
2339
314
        !DSAStack->hasExplicitDSA(
2340
308
            D, [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; },
2341
314
            Level))
2342
6
      return OMPC_private;
2343
2.59M
  }
2344
  // User-defined allocators are private since they must be defined in the
2345
  // context of target region.
2346
2.59M
  if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level) &&
2347
1.69M
      DSAStack->isUsesAllocatorsDecl(Level, D).getValueOr(
2348
1.69M
          DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2349
1.69M
          DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2350
10
    return OMPC_private;
2351
2.59M
  return (DSAStack->hasExplicitDSA(
2352
227k
              D, [](OpenMPClauseKind K, bool) { return K == OMPC_private; },
2353
2.59M
              Level) ||
2354
2.56M
          (DSAStack->isClauseParsingMode() &&
2355
508k
           DSAStack->getClauseParsingMode() == OMPC_private) ||
2356
          // Consider taskgroup reduction descriptor variable a private
2357
          // to avoid possible capture in the region.
2358
2.54M
          (DSAStack->hasExplicitDirective(
2359
2.54M
               [](OpenMPDirectiveKind K) {
2360
2.54M
                 return K == OMPD_taskgroup ||
2361
2.53M
                        ((isOpenMPParallelDirective(K) ||
2362
1.44M
                          isOpenMPWorksharingDirective(K)) &&
2363
1.15M
                         !isOpenMPSimdDirective(K));
2364
2.54M
               },
2365
2.54M
               Level) &&
2366
608k
           DSAStack->isTaskgroupReductionRef(D, Level)))
2367
49.2k
             ? OMPC_private
2368
2.54M
             : OMPC_unknown;
2369
2.59M
}
2370
2371
void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2372
365k
                                unsigned Level) {
2373
365k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2374
365k
  D = getCanonicalDecl(D);
2375
365k
  OpenMPClauseKind OMPC = OMPC_unknown;
2376
450k
  for (unsigned I = 
DSAStack365k
->getNestingLevel() + 1; I > Level;
--I85.5k
) {
2377
365k
    const unsigned NewLevel = I - 1;
2378
365k
    if (DSAStack->hasExplicitDSA(
2379
365k
            D,
2380
90.1k
            [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) {
2381
90.1k
              if (isOpenMPPrivate(K) && 
!AppliedToPointee88.7k
) {
2382
87.8k
                OMPC = K;
2383
87.8k
                return true;
2384
87.8k
              }
2385
2.27k
              return false;
2386
2.27k
            },
2387
365k
            NewLevel))
2388
87.8k
      break;
2389
277k
    if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2390
277k
            D, NewLevel,
2391
277k
            [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2392
8.91k
               OpenMPClauseKind) { return true; })) {
2393
8.91k
      OMPC = OMPC_map;
2394
8.91k
      break;
2395
8.91k
    }
2396
268k
    if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2397
182k
                                       NewLevel)) {
2398
182k
      OMPC = OMPC_map;
2399
182k
      if (DSAStack->mustBeFirstprivateAtLevel(
2400
182k
              NewLevel, getVariableCategoryFromDecl(LangOpts, D)))
2401
141k
        OMPC = OMPC_firstprivate;
2402
182k
      break;
2403
182k
    }
2404
268k
  }
2405
365k
  if (OMPC != OMPC_unknown)
2406
279k
    FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, unsigned(OMPC)));
2407
365k
}
2408
2409
bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2410
1.31M
                                      unsigned CaptureLevel) const {
2411
1.31M
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2412
  // Return true if the current level is no longer enclosed in a target region.
2413
2414
1.31M
  SmallVector<OpenMPDirectiveKind, 4> Regions;
2415
1.31M
  getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
2416
1.31M
  const auto *VD = dyn_cast<VarDecl>(D);
2417
1.31M
  return VD && !VD->hasLocalStorage() &&
2418
47.6k
         DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
2419
47.6k
                                        Level) &&
2420
25.1k
         Regions[CaptureLevel] != OMPD_task;
2421
1.31M
}
2422
2423
bool Sema::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2424
58.0k
                                      unsigned CaptureLevel) const {
2425
58.0k
  assert(LangOpts.OpenMP && "OpenMP is not allowed");
2426
  // Return true if the current level is no longer enclosed in a target region.
2427
2428
58.0k
  if (const auto *VD = dyn_cast<VarDecl>(D)) {
2429
58.0k
    if (!VD->hasLocalStorage()) {
2430
58.0k
      if (isInOpenMPTargetExecutionDirective())
2431
44.4k
        return true;
2432
13.6k
      DSAStackTy::DSAVarData TopDVar =
2433
13.6k
          DSAStack->getTopDSA(D, /*FromParent=*/false);
2434
13.6k
      unsigned NumLevels =
2435
13.6k
          getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2436
13.6k
      if (Level == 0)
2437
10.5k
        return (NumLevels == CaptureLevel + 1) && 
TopDVar.CKind != OMPC_shared8.88k
;
2438
4.09k
      
do 3.05k
{
2439
4.09k
        --Level;
2440
4.09k
        DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2441
4.09k
        if (DVar.CKind != OMPC_shared)
2442
316
          return true;
2443
3.77k
      } while (Level > 0);
2444
3.05k
    }
2445
58.0k
  }
2446
2.73k
  return true;
2447
58.0k
}
2448
2449
74.5k
void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
2450
2451
void Sema::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2452
284
                                          OMPTraitInfo &TI) {
2453
284
  OMPDeclareVariantScopes.push_back(OMPDeclareVariantScope(TI));
2454
284
}
2455
2456
280
void Sema::ActOnOpenMPEndDeclareVariant() {
2457
280
  assert(isInOpenMPDeclareVariantScope() &&
2458
280
         "Not in OpenMP declare variant scope!");
2459
2460
280
  OMPDeclareVariantScopes.pop_back();
2461
280
}
2462
2463
void Sema::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2464
                                         const FunctionDecl *Callee,
2465
68.0k
                                         SourceLocation Loc) {
2466
68.0k
  assert(LangOpts.OpenMP && "Expected OpenMP compilation mode.");
2467
68.0k
  Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2468
68.0k
      OMPDeclareTargetDeclAttr::getDeviceType(Caller->getMostRecentDecl());
2469
  // Ignore host functions during device analyzis.
2470
68.0k
  if (LangOpts.OpenMPIsDevice && 
DevTy1.05k
&&
2471
798
      *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2472
0
    return;
2473
  // Ignore nohost functions during host analyzis.
2474
68.0k
  if (!LangOpts.OpenMPIsDevice && 
DevTy66.9k
&&
2475
617
      *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2476
0
    return;
2477
68.0k
  const FunctionDecl *FD = Callee->getMostRecentDecl();
2478
68.0k
  DevTy = OMPDeclareTargetDeclAttr::getDeviceType(FD);
2479
68.0k
  if (LangOpts.OpenMPIsDevice && 
DevTy1.05k
&&
2480
365
      *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2481
    // Diagnose host function called during device codegen.
2482
3
    StringRef HostDevTy =
2483
3
        getOpenMPSimpleClauseTypeName(OMPC_device_type, OMPC_DEVICE_TYPE_host);
2484
3
    Diag(Loc, diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2485
3
    Diag(*OMPDeclareTargetDeclAttr::getLocation(FD),
2486
3
         diag::note_omp_marked_device_type_here)
2487
3
        << HostDevTy;
2488
3
    return;
2489
3
  }
2490
68.0k
      if (!LangOpts.OpenMPIsDevice && 
DevTy66.9k
&&
2491
697
          *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2492
        // Diagnose nohost function called during host codegen.
2493
25
        StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2494
25
            OMPC_device_type, OMPC_DEVICE_TYPE_nohost);
2495
25
        Diag(Loc, diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2496
25
        Diag(*OMPDeclareTargetDeclAttr::getLocation(FD),
2497
25
             diag::note_omp_marked_device_type_here)
2498
25
            << NoHostDevTy;
2499
25
      }
2500
68.0k
}
2501
2502
void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2503
                               const DeclarationNameInfo &DirName,
2504
333k
                               Scope *CurScope, SourceLocation Loc) {
2505
333k
  DSAStack->push(DKind, DirName, CurScope, Loc);
2506
333k
  PushExpressionEvaluationContext(
2507
333k
      ExpressionEvaluationContext::PotentiallyEvaluated);
2508
333k
}
2509
2510
214k
void Sema::StartOpenMPClause(OpenMPClauseKind K) {
2511
214k
  DSAStack->setClauseParsingMode(K);
2512
214k
}
2513
2514
214k
void Sema::EndOpenMPClause() {
2515
214k
  DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2516
214k
}
2517
2518
static std::pair<ValueDecl *, bool>
2519
getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2520
               SourceRange &ERange, bool AllowArraySection = false);
2521
2522
/// Check consistency of the reduction clauses.
2523
static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2524
283k
                                  ArrayRef<OMPClause *> Clauses) {
2525
283k
  bool InscanFound = false;
2526
283k
  SourceLocation InscanLoc;
2527
  // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2528
  // A reduction clause without the inscan reduction-modifier may not appear on
2529
  // a construct on which a reduction clause with the inscan reduction-modifier
2530
  // appears.
2531
191k
  for (OMPClause *C : Clauses) {
2532
191k
    if (C->getClauseKind() != OMPC_reduction)
2533
169k
      continue;
2534
22.0k
    auto *RC = cast<OMPReductionClause>(C);
2535
22.0k
    if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2536
118
      InscanFound = true;
2537
118
      InscanLoc = RC->getModifierLoc();
2538
118
      continue;
2539
118
    }
2540
21.9k
    if (RC->getModifier() == OMPC_REDUCTION_task) {
2541
      // OpenMP 5.0, 2.19.5.4 reduction Clause.
2542
      // A reduction clause with the task reduction-modifier may only appear on
2543
      // a parallel construct, a worksharing construct or a combined or
2544
      // composite construct for which any of the aforementioned constructs is a
2545
      // constituent construct and simd or loop are not constituent constructs.
2546
334
      OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2547
334
      if (!(isOpenMPParallelDirective(CurDir) ||
2548
138
            isOpenMPWorksharingDirective(CurDir)) ||
2549
238
          isOpenMPSimdDirective(CurDir))
2550
138
        S.Diag(RC->getModifierLoc(),
2551
138
               diag::err_omp_reduction_task_not_parallel_or_worksharing);
2552
334
      continue;
2553
334
    }
2554
21.9k
  }
2555
283k
  if (InscanFound) {
2556
130
    for (OMPClause *C : Clauses) {
2557
130
      if (C->getClauseKind() != OMPC_reduction)
2558
0
        continue;
2559
130
      auto *RC = cast<OMPReductionClause>(C);
2560
130
      if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2561
12
        S.Diag(RC->getModifier() == OMPC_REDUCTION_unknown
2562
6
                   ? RC->getBeginLoc()
2563
6
                   : RC->getModifierLoc(),
2564
12
               diag::err_omp_inscan_reduction_expected);
2565
12
        S.Diag(InscanLoc, diag::note_omp_previous_inscan_reduction);
2566
12
        continue;
2567
12
      }
2568
178
      
for (Expr *Ref : RC->varlists())118
{
2569
178
        assert(Ref && "NULL expr in OpenMP nontemporal clause.");
2570
178
        SourceLocation ELoc;
2571
178
        SourceRange ERange;
2572
178
        Expr *SimpleRefExpr = Ref;
2573
178
        auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
2574
178
                                  /*AllowArraySection=*/true);
2575
178
        ValueDecl *D = Res.first;
2576
178
        if (!D)
2577
12
          continue;
2578
166
        if (!Stack->isUsedInScanDirective(getCanonicalDecl(D))) {
2579
30
          S.Diag(Ref->getExprLoc(),
2580
30
                 diag::err_omp_reduction_not_inclusive_exclusive)
2581
30
              << Ref->getSourceRange();
2582
30
        }
2583
166
      }
2584
118
    }
2585
118
  }
2586
283k
}
2587
2588
static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2589
                                 ArrayRef<OMPClause *> Clauses);
2590
static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2591
                                 bool WithInit);
2592
2593
static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2594
                              const ValueDecl *D,
2595
                              const DSAStackTy::DSAVarData &DVar,
2596
                              bool IsLoopIterVar = false);
2597
2598
333k
void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
2599
  // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2600
  //  A variable of class type (or array thereof) that appears in a lastprivate
2601
  //  clause requires an accessible, unambiguous default constructor for the
2602
  //  class type, unless the list item is also specified in a firstprivate
2603
  //  clause.
2604
333k
  if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
2605
191k
    for (OMPClause *C : D->clauses()) {
2606
191k
      if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
2607
5.89k
        SmallVector<Expr *, 8> PrivateCopies;
2608
10.3k
        for (Expr *DE : Clause->varlists()) {
2609
10.3k
          if (DE->isValueDependent() || 
DE->isTypeDependent()8.22k
) {
2610
2.15k
            PrivateCopies.push_back(nullptr);
2611
2.15k
            continue;
2612
2.15k
          }
2613
8.22k
          auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
2614
8.22k
          auto *VD = cast<VarDecl>(DRE->getDecl());
2615
8.22k
          QualType Type = VD->getType().getNonReferenceType();
2616
8.22k
          const DSAStackTy::DSAVarData DVar =
2617
8.22k
              DSAStack->getTopDSA(VD, /*FromParent=*/false);
2618
8.22k
          if (DVar.CKind == OMPC_lastprivate) {
2619
            // Generate helper private variable and initialize it with the
2620
            // default value. The address of the original variable is replaced
2621
            // by the address of the new private variable in CodeGen. This new
2622
            // variable is not added to IdResolver, so the code in the OpenMP
2623
            // region uses original variable for proper diagnostics.
2624
7.49k
            VarDecl *VDPrivate = buildVarDecl(
2625
7.49k
                *this, DE->getExprLoc(), Type.getUnqualifiedType(),
2626
7.40k
                VD->getName(), VD->hasAttrs() ? 
&VD->getAttrs()92
: nullptr, DRE);
2627
7.49k
            ActOnUninitializedDecl(VDPrivate);
2628
7.49k
            if (VDPrivate->isInvalidDecl()) {
2629
0
              PrivateCopies.push_back(nullptr);
2630
0
              continue;
2631
0
            }
2632
7.49k
            PrivateCopies.push_back(buildDeclRefExpr(
2633
7.49k
                *this, VDPrivate, DE->getType(), DE->getExprLoc()));
2634
728
          } else {
2635
            // The variable is also a firstprivate, so initialization sequence
2636
            // for private copy is generated already.
2637
728
            PrivateCopies.push_back(nullptr);
2638
728
          }
2639
8.22k
        }
2640
5.89k
        Clause->setPrivateCopies(PrivateCopies);
2641
5.89k
        continue;
2642
5.89k
      }
2643
      // Finalize nontemporal clause by handling private copies, if any.
2644
185k
      if (auto *Clause = dyn_cast<OMPNontemporalClause>(C)) {
2645
426
        SmallVector<Expr *, 8> PrivateRefs;
2646
614
        for (Expr *RefExpr : Clause->varlists()) {
2647
614
          assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
2648
614
          SourceLocation ELoc;
2649
614
          SourceRange ERange;
2650
614
          Expr *SimpleRefExpr = RefExpr;
2651
614
          auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
2652
614
          if (Res.second)
2653
            // It will be analyzed later.
2654
52
            PrivateRefs.push_back(RefExpr);
2655
614
          ValueDecl *D = Res.first;
2656
614
          if (!D)
2657
52
            continue;
2658
2659
562
          const DSAStackTy::DSAVarData DVar =
2660
562
              DSAStack->getTopDSA(D, /*FromParent=*/false);
2661
20
          PrivateRefs.push_back(DVar.PrivateCopy ? DVar.PrivateCopy
2662
542
                                                 : SimpleRefExpr);
2663
562
        }
2664
426
        Clause->setPrivateRefs(PrivateRefs);
2665
426
        continue;
2666
426
      }
2667
184k
      if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(C)) {
2668
442
        for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; 
++I240
) {
2669
240
          OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
2670
240
          auto *DRE = dyn_cast<DeclRefExpr>(D.Allocator->IgnoreParenImpCasts());
2671
240
          if (!DRE)
2672
0
            continue;
2673
240
          ValueDecl *VD = DRE->getDecl();
2674
240
          if (!VD || !isa<VarDecl>(VD))
2675
40
            continue;
2676
200
          DSAStackTy::DSAVarData DVar =
2677
200
              DSAStack->getTopDSA(VD, /*FromParent=*/false);
2678
          // OpenMP [2.12.5, target Construct]
2679
          // Memory allocators that appear in a uses_allocators clause cannot
2680
          // appear in other data-sharing attribute clauses or data-mapping
2681
          // attribute clauses in the same construct.
2682
200
          Expr *MapExpr = nullptr;
2683
200
          if (DVar.RefExpr ||
2684
198
              DSAStack->checkMappableExprComponentListsForDecl(
2685
198
                  VD, /*CurrentRegionOnly=*/true,
2686
198
                  [VD, &MapExpr](
2687
198
                      OMPClauseMappableExprCommon::MappableExprComponentListRef
2688
198
                          MapExprComponents,
2689
2
                      OpenMPClauseKind C) {
2690
2
                    auto MI = MapExprComponents.rbegin();
2691
2
                    auto ME = MapExprComponents.rend();
2692
2
                    if (MI != ME &&
2693
2
                        MI->getAssociatedDeclaration()->getCanonicalDecl() ==
2694
2
                            VD->getCanonicalDecl()) {
2695
2
                      MapExpr = MI->getAssociatedExpression();
2696
2
                      return true;
2697
2
                    }
2698
0
                    return false;
2699
4
                  })) {
2700
4
            Diag(D.Allocator->getExprLoc(),
2701
4
                 diag::err_omp_allocator_used_in_clauses)
2702
4
                << D.Allocator->getSourceRange();
2703
4
            if (DVar.RefExpr)
2704
2
              reportOriginalDsa(*this, DSAStack, VD, DVar);
2705
2
            else
2706
2
              Diag(MapExpr->getExprLoc(), diag::note_used_here)
2707
2
                  << MapExpr->getSourceRange();
2708
4
          }
2709
200
        }
2710
202
        continue;
2711
202
      }
2712
184k
    }
2713
    // Check allocate clauses.
2714
283k
    if (!CurContext->isDependentContext())
2715
207k
      checkAllocateClauses(*this, DSAStack, D->clauses());
2716
283k
    checkReductionClauses(*this, DSAStack, D->clauses());
2717
283k
  }
2718
2719
333k
  DSAStack->pop();
2720
333k
  DiscardCleanupsInEvaluationContext();
2721
333k
  PopExpressionEvaluationContext();
2722
333k
}
2723
2724
static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
2725
                                     Expr *NumIterations, Sema &SemaRef,
2726
                                     Scope *S, DSAStackTy *Stack);
2727
2728
namespace {
2729
2730
class VarDeclFilterCCC final : public CorrectionCandidateCallback {
2731
private:
2732
  Sema &SemaRef;
2733
2734
public:
2735
40
  explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
2736
16
  bool ValidateCandidate(const TypoCorrection &Candidate) override {
2737
16
    NamedDecl *ND = Candidate.getCorrectionDecl();
2738
16
    if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
2739
16
      return VD->hasGlobalStorage() &&
2740
16
             SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2741
16
                                   SemaRef.getCurScope());
2742
16
    }
2743
0
    return false;
2744
0
  }
2745
2746
40
  std::unique_ptr<CorrectionCandidateCallback> clone() override {
2747
40
    return std::make_unique<VarDeclFilterCCC>(*this);
2748
40
  }
2749
2750
};
2751
2752
class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
2753
private:
2754
  Sema &SemaRef;
2755
2756
public:
2757
12
  explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
2758
12
  bool ValidateCandidate(const TypoCorrection &Candidate) override {
2759
12
    NamedDecl *ND = Candidate.getCorrectionDecl();
2760
12
    if (ND && 
(0
(0
isa<VarDecl>(ND)0
&&
ND->getKind() == Decl::Var0
) ||
2761
0
               isa<FunctionDecl>(ND))) {
2762
0
      return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
2763
0
                                   SemaRef.getCurScope());
2764
0
    }
2765
12
    return false;
2766
12
  }
2767
2768
12
  std::unique_ptr<CorrectionCandidateCallback> clone() override {
2769
12
    return std::make_unique<VarOrFuncDeclFilterCCC>(*this);
2770
12
  }
2771
};
2772
2773
} // namespace
2774
2775
ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
2776
                                         CXXScopeSpec &ScopeSpec,
2777
                                         const DeclarationNameInfo &Id,
2778
3.18k
                                         OpenMPDirectiveKind Kind) {
2779
3.18k
  LookupResult Lookup(*this, Id, LookupOrdinaryName);
2780
3.18k
  LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
2781
2782
3.18k
  if (Lookup.isAmbiguous())
2783
8
    return ExprError();
2784
2785
3.18k
  VarDecl *VD;
2786
3.18k
  if (!Lookup.isSingleResult()) {
2787
40
    VarDeclFilterCCC CCC(*this);
2788
40
    if (TypoCorrection Corrected =
2789
8
            CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, CCC,
2790
8
                        CTK_ErrorRecovery)) {
2791
8
      diagnoseTypo(Corrected,
2792
8
                   PDiag(Lookup.empty()
2793
8
                             ? diag::err_undeclared_var_use_suggest
2794
0
                             : diag::err_omp_expected_var_arg_suggest)
2795
8
                       << Id.getName());
2796
8
      VD = Corrected.getCorrectionDeclAs<VarDecl>();
2797
32
    } else {
2798
32
      Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
2799
0
                                       : diag::err_omp_expected_var_arg)
2800
32
          << Id.getName();
2801
32
      return ExprError();
2802
32
    }
2803
3.14k
  } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
2804
16
    Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
2805
16
    Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
2806
16
    return ExprError();
2807
16
  }
2808
3.13k
  Lookup.suppressDiagnostics();
2809
2810
  // OpenMP [2.9.2, Syntax, C/C++]
2811
  //   Variables must be file-scope, namespace-scope, or static block-scope.
2812
3.13k
  if (Kind == OMPD_threadprivate && 
!VD->hasGlobalStorage()2.60k
) {
2813
8
    Diag(Id.getLoc(), diag::err_omp_global_var_arg)
2814
8
        << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
2815
8
    bool IsDecl =
2816
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2817
8
    Diag(VD->getLocation(),
2818
8
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2819
8
        << VD;
2820
8
    return ExprError();
2821
8
  }
2822
2823
3.12k
  VarDecl *CanonicalVD = VD->getCanonicalDecl();
2824
3.12k
  NamedDecl *ND = CanonicalVD;
2825
  // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2826
  //   A threadprivate directive for file-scope variables must appear outside
2827
  //   any definition or declaration.
2828
3.12k
  if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2829
1.70k
      !getCurLexicalContext()->isTranslationUnit()) {
2830
16
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2831
16
        << getOpenMPDirectiveName(Kind) << VD;
2832
16
    bool IsDecl =
2833
16
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2834
16
    Diag(VD->getLocation(),
2835
16
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2836
16
        << VD;
2837
16
    return ExprError();
2838
16
  }
2839
  // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2840
  //   A threadprivate directive for static class member variables must appear
2841
  //   in the class definition, in the same scope in which the member
2842
  //   variables are declared.
2843
3.10k
  if (CanonicalVD->isStaticDataMember() &&
2844
174
      !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2845
8
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2846
8
        << getOpenMPDirectiveName(Kind) << VD;
2847
8
    bool IsDecl =
2848
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2849
8
    Diag(VD->getLocation(),
2850
8
         IsDecl ? diag::note_previous_decl : 
diag::note_defined_here0
)
2851
8
        << VD;
2852
8
    return ExprError();
2853
8
  }
2854
  // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2855
  //   A threadprivate directive for namespace-scope variables must appear
2856
  //   outside any definition or declaration other than the namespace
2857
  //   definition itself.
2858
3.10k
  if (CanonicalVD->getDeclContext()->isNamespace() &&
2859
817
      (!getCurLexicalContext()->isFileContext() ||
2860
817
       !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2861
0
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2862
0
        << getOpenMPDirectiveName(Kind) << VD;
2863
0
    bool IsDecl =
2864
0
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2865
0
    Diag(VD->getLocation(),
2866
0
         IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2867
0
        << VD;
2868
0
    return ExprError();
2869
0
  }
2870
  // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2871
  //   A threadprivate directive for static block-scope variables must appear
2872
  //   in the scope of the variable and not in a nested scope.
2873
3.10k
  if (CanonicalVD->isLocalVarDecl() && 
CurScope410
&&
2874
410
      !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
2875
8
    Diag(Id.getLoc(), diag::err_omp_var_scope)
2876
8
        << getOpenMPDirectiveName(Kind) << VD;
2877
8
    bool IsDecl =
2878
8
        VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2879
8
    Diag(VD->getLocation(),
2880
8
         IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2881
8
        << VD;
2882
8
    return ExprError();
2883
8
  }
2884
2885
  // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2886
  //   A threadprivate directive must lexically precede all references to any
2887
  //   of the variables in its list.
2888
3.09k
  if (Kind == OMPD_threadprivate && 
VD->isUsed()2.58k
&&
2889
84
      !DSAStack->isThreadPrivate(VD)) {
2890
4
    Diag(Id.getLoc(), diag::err_omp_var_used)
2891
4
        << getOpenMPDirectiveName(Kind) << VD;
2892
4
    return ExprError();
2893
4
  }
2894
2895
3.08k
  QualType ExprType = VD->getType().getNonReferenceType();
2896
3.08k
  return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2897
3.08k
                             SourceLocation(), VD,
2898
3.08k
                             /*RefersToEnclosingVariableOrCapture=*/false,
2899
3.08k
                             Id.getLoc(), ExprType, VK_LValue);
2900
3.08k
}
2901
2902
Sema::DeclGroupPtrTy
2903
Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2904
2.45k
                                        ArrayRef<Expr *> VarList) {
2905
2.45k
  if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
2906
2.39k
    CurContext->addDecl(D);
2907
2.39k
    return DeclGroupPtrTy::make(DeclGroupRef(D));
2908
2.39k
  }
2909
68
  return nullptr;
2910
68
}
2911
2912
namespace {
2913
class LocalVarRefChecker final
2914
    : public ConstStmtVisitor<LocalVarRefChecker, bool> {
2915
  Sema &SemaRef;
2916
2917
public:
2918
23
  bool VisitDeclRefExpr(const DeclRefExpr *E) {
2919
23
    if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
2920
16
      if (VD->hasLocalStorage()) {
2921
4
        SemaRef.Diag(E->getBeginLoc(),
2922
4
                     diag::err_omp_local_var_in_threadprivate_init)
2923
4
            << E->getSourceRange();
2924
4
        SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2925
4
            << VD << VD->getSourceRange();
2926
4
        return true;
2927
4
      }
2928
19
    }
2929
19
    return false;
2930
19
  }
2931
2.15k
  bool VisitStmt(const Stmt *S) {
2932
980
    for (const Stmt *Child : S->children()) {
2933
980
      if (Child && Visit(Child))
2934
8
        return true;
2935
980
    }
2936
2.14k
    return false;
2937
2.15k
  }
2938
1.19k
  explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
2939
};
2940
} // namespace
2941
2942
OMPThreadPrivateDecl *
2943
2.67k
Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
2944
2.67k
  SmallVector<Expr *, 8> Vars;
2945
2.81k
  for (Expr *RefExpr : VarList) {
2946
2.81k
    auto *DE = cast<DeclRefExpr>(RefExpr);
2947
2.81k
    auto *VD = cast<VarDecl>(DE->getDecl());
2948
2.81k
    SourceLocation ILoc = DE->getExprLoc();
2949
2950
    // Mark variable as used.
2951
2.81k
    VD->setReferenced();
2952
2.81k
    VD->markUsed(Context);
2953
2954
2.81k
    QualType QType = VD->getType();
2955
2.81k
    if (QType->isDependentType() || 
QType->isInstantiationDependentType()2.64k
) {
2956
      // It will be analyzed later.
2957
174
      Vars.push_back(DE);
2958
174
      continue;
2959
174
    }
2960
2961
    // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2962
    //   A threadprivate variable must not have an incomplete type.
2963
2.64k
    if (RequireCompleteType(ILoc, VD->getType(),
2964
4
                            diag::err_omp_threadprivate_incomplete_type)) {
2965
4
      continue;
2966
4
    }
2967
2968
    // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2969
    //   A threadprivate variable must not have a reference type.
2970
2.63k
    if (VD->getType()->isReferenceType()) {
2971
4
      Diag(ILoc, diag::err_omp_ref_type_arg)
2972
4
          << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2973
4
      bool IsDecl =
2974
4
          VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2975
4
      Diag(VD->getLocation(),
2976
4
           IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2977
4
          << VD;
2978
4
      continue;
2979
4
    }
2980
2981
    // Check if this is a TLS variable. If TLS is not being supported, produce
2982
    // the corresponding diagnostic.
2983
2.63k
    if ((VD->getTLSKind() != VarDecl::TLS_None &&
2984
34
         !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2985
30
           getLangOpts().OpenMPUseTLS &&
2986
30
           getASTContext().getTargetInfo().isTLSSupported())) ||
2987
2.62k
        (VD->getStorageClass() == SC_Register && 
VD->hasAttr<AsmLabelAttr>()4
&&
2988
8
         
!VD->isLocalVarDecl()4
)) {
2989
8
      Diag(ILoc, diag::err_omp_var_thread_local)
2990
4
          << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
2991
8
      bool IsDecl =
2992
8
          VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2993
8
      Diag(VD->getLocation(),
2994
8
           IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
2995
8
          << VD;
2996
8
      continue;
2997
8
    }
2998
2999
    // Check if initial value of threadprivate variable reference variable with
3000
    // local storage (it is not supported by runtime).
3001
2.62k
    if (const Expr *Init = VD->getAnyInitializer()) {
3002
1.19k
      LocalVarRefChecker Checker(*this);
3003
1.19k
      if (Checker.Visit(Init))
3004
4
        continue;
3005
2.62k
    }
3006
3007
2.62k
    Vars.push_back(RefExpr);
3008
2.62k
    DSAStack->addDSA(VD, DE, OMPC_threadprivate);
3009
2.62k
    VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
3010
2.62k
        Context, SourceRange(Loc, Loc)));
3011
2.62k
    if (ASTMutationListener *ML = Context.getASTMutationListener())
3012
208
      ML->DeclarationMarkedOpenMPThreadPrivate(VD);
3013
2.62k
  }
3014
2.67k
  OMPThreadPrivateDecl *D = nullptr;
3015
2.67k
  if (!Vars.empty()) {
3016
2.60k
    D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
3017
2.60k
                                     Vars);
3018
2.60k
    D->setAccess(AS_public);
3019
2.60k
  }
3020
2.67k
  return D;
3021
2.67k
}
3022
3023
static OMPAllocateDeclAttr::AllocatorTypeTy
3024
1.78k
getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3025
1.78k
  if (!Allocator)
3026
841
    return OMPAllocateDeclAttr::OMPNullMemAlloc;
3027
941
  if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3028
941
      Allocator->isInstantiationDependent() ||
3029
941
      Allocator->containsUnexpandedParameterPack())
3030
0
    return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3031
941
  auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3032
941
  const Expr *AE = Allocator->IgnoreParenImpCasts();
3033
7.13k
  for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; 
++I6.19k
) {
3034
7.11k
    auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
3035
7.11k
    const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3036
7.11k
    llvm::FoldingSetNodeID AEId, DAEId;
3037
7.11k
    AE->Profile(AEId, S.getASTContext(), /*Canonical=*/true);
3038
7.11k
    DefAllocator->Profile(DAEId, S.getASTContext(), /*Canonical=*/true);
3039
7.11k
    if (AEId == DAEId) {
3040
925
      AllocatorKindRes = AllocatorKind;
3041
925
      break;
3042
925
    }
3043
7.11k
  }
3044
941
  return AllocatorKindRes;
3045
941
}
3046
3047
static bool checkPreviousOMPAllocateAttribute(
3048
    Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
3049
1.64k
    OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
3050
1.64k
  if (!VD->hasAttr<OMPAllocateDeclAttr>())
3051
1.49k
    return false;
3052
146
  const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
3053
146
  Expr *PrevAllocator = A->getAllocator();
3054
146
  OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
3055
146
      getAllocatorKind(S, Stack, PrevAllocator);
3056
146
  bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
3057
146
  if (AllocatorsMatch &&
3058
140
      AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
3059
0
      Allocator && PrevAllocator) {
3060
0
    const Expr *AE = Allocator->IgnoreParenImpCasts();
3061
0
    const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
3062
0
    llvm::FoldingSetNodeID AEId, PAEId;
3063
0
    AE->Profile(AEId, S.Context, /*Canonical=*/true);
3064
0
    PAE->Profile(PAEId, S.Context, /*Canonical=*/true);
3065
0
    AllocatorsMatch = AEId == PAEId;
3066
0
  }
3067
146
  if (!AllocatorsMatch) {
3068
6
    SmallString<256> AllocatorBuffer;
3069
6
    llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
3070
6
    if (Allocator)
3071
4
      Allocator->printPretty(AllocatorStream, nullptr, S.getPrintingPolicy());
3072
6
    SmallString<256> PrevAllocatorBuffer;
3073
6
    llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
3074
6
    if (PrevAllocator)
3075
4
      PrevAllocator->printPretty(PrevAllocatorStream, nullptr,
3076
4
                                 S.getPrintingPolicy());
3077
3078
6
    SourceLocation AllocatorLoc =
3079
4
        Allocator ? Allocator->getExprLoc() : 
RefExpr->getExprLoc()2
;
3080
6
    SourceRange AllocatorRange =
3081
4
        Allocator ? Allocator->getSourceRange() : 
RefExpr->getSourceRange()2
;
3082
6
    SourceLocation PrevAllocatorLoc =
3083
4
        PrevAllocator ? PrevAllocator->getExprLoc() : 
A->getLocation()2
;
3084
6
    SourceRange PrevAllocatorRange =
3085
4
        PrevAllocator ? PrevAllocator->getSourceRange() : 
A->getRange()2
;
3086
6
    S.Diag(AllocatorLoc, diag::warn_omp_used_different_allocator)
3087
4
        << (Allocator ? 1 : 
02
) << AllocatorStream.str()
3088
4
        << (PrevAllocator ? 1 : 
02
) << PrevAllocatorStream.str()
3089
6
        << AllocatorRange;
3090
6
    S.Diag(PrevAllocatorLoc, diag::note_omp_previous_allocator)
3091
6
        << PrevAllocatorRange;
3092
6
    return true;
3093
6
  }
3094
140
  return false;
3095
140
}
3096
3097
static void
3098
applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3099
                          OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3100
1.63k
                          Expr *Allocator, SourceRange SR) {
3101
1.63k
  if (VD->hasAttr<OMPAllocateDeclAttr>())
3102
140
    return;
3103
1.49k
  if (Allocator &&
3104
874
      (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3105
874
       Allocator->isInstantiationDependent() ||
3106
874
       Allocator->containsUnexpandedParameterPack()))
3107
0
    return;
3108
1.49k
  auto *A = OMPAllocateDeclAttr::CreateImplicit(S.Context, AllocatorKind,
3109
1.49k
                                                Allocator, SR);
3110
1.49k
  VD->addAttr(A);
3111
1.49k
  if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3112
183
    ML->DeclarationMarkedOpenMPAllocate(VD, A);
3113
1.49k
}
3114
3115
Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
3116
    SourceLocation Loc, ArrayRef<Expr *> VarList,
3117
546
    ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
3118
546
  assert(Clauses.size() <= 1 && "Expected at most one clause.");
3119
546
  Expr *Allocator = nullptr;
3120
546
  if (Clauses.empty()) {
3121
    // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3122
    // allocate directives that appear in a target region must specify an
3123
    // allocator clause unless a requires directive with the dynamic_allocators
3124
    // clause is present in the same compilation unit.
3125
302
    if (LangOpts.OpenMPIsDevice &&
3126
4
        !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3127
2
      targetDiag(Loc, diag::err_expected_allocator_clause);
3128
244
  } else {
3129
244
    Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
3130
244
  }
3131
546
  OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3132
546
      getAllocatorKind(*this, DSAStack, Allocator);
3133
546
  SmallVector<Expr *, 8> Vars;
3134
563
  for (Expr *RefExpr : VarList) {
3135
563
    auto *DE = cast<DeclRefExpr>(RefExpr);
3136
563
    auto *VD = cast<VarDecl>(DE->getDecl());
3137
3138
    // Check if this is a TLS variable or global register.
3139
563
    if (VD->getTLSKind() != VarDecl::TLS_None ||
3140
559
        VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
3141
559
        (VD->getStorageClass() == SC_Register && 
VD->hasAttr<AsmLabelAttr>()4
&&
3142
4
         !VD->isLocalVarDecl()))
3143
8
      continue;
3144
3145
    // If the used several times in the allocate directive, the same allocator
3146
    // must be used.
3147
555
    if (checkPreviousOMPAllocateAttribute(*this, DSAStack, RefExpr, VD,
3148
555
                                          AllocatorKind, Allocator))
3149
6
      continue;
3150
3151
    // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3152
    // If a list item has a static storage type, the allocator expression in the
3153
    // allocator clause must be a constant expression that evaluates to one of
3154
    // the predefined memory allocator values.
3155
549
    if (Allocator && 
VD->hasGlobalStorage()269
) {
3156
203
      if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3157
4
        Diag(Allocator->getExprLoc(),
3158
4
             diag::err_omp_expected_predefined_allocator)
3159
4
            << Allocator->getSourceRange();
3160
4
        bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3161
4
                      VarDecl::DeclarationOnly;
3162
4
        Diag(VD->getLocation(),
3163
4
             IsDecl ? 
diag::note_previous_decl0
: diag::note_defined_here)
3164
4
            << VD;
3165
4
        continue;
3166
4
      }
3167
545
    }
3168
3169
545
    Vars.push_back(RefExpr);
3170
545
    applyOMPAllocateAttribute(*this, VD, AllocatorKind, Allocator,
3171
545
                              DE->getSourceRange());
3172
545
  }
3173
546
  if (Vars.empty())
3174
58
    return nullptr;
3175
488
  if (!Owner)
3176
430
    Owner = getCurLexicalContext();
3177
488
  auto *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
3178
488
  D->setAccess(AS_public);
3179
488
  Owner->addDecl(D);
3180
488
  return DeclGroupPtrTy::make(DeclGroupRef(D));
3181
488
}
3182
3183
Sema::DeclGroupPtrTy
3184
Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3185
171
                                   ArrayRef<OMPClause *> ClauseList) {
3186
171
  OMPRequiresDecl *D = nullptr;
3187
171
  if (!CurContext->isFileContext()) {
3188
1
    Diag(Loc, diag::err_omp_invalid_scope) << "requires";
3189
170
  } else {
3190
170
    D = CheckOMPRequiresDecl(Loc, ClauseList);
3191
170
    if (D) {
3192
155
      CurContext->addDecl(D);
3193
155
      DSAStack->addRequiresDecl(D);
3194
155
    }
3195
170
  }
3196
171
  return DeclGroupPtrTy::make(DeclGroupRef(D));
3197
171
}
3198
3199
void Sema::ActOnOpenMPAssumesDirective(SourceLocation Loc,
3200
                                       OpenMPDirectiveKind DKind,
3201
                                       ArrayRef<StringRef> Assumptions,
3202
145
                                       bool SkippedClauses) {
3203
145
  if (!SkippedClauses && 
Assumptions.empty()91
)
3204
4
    Diag(Loc, diag::err_omp_no_clause_for_directive)
3205
4
        << llvm::omp::getAllAssumeClauseOptions()
3206
4
        << llvm::omp::getOpenMPDirectiveName(DKind);
3207
3208
145
  auto *AA = AssumptionAttr::Create(Context, llvm::join(Assumptions, ","), Loc);
3209
145
  if (DKind == llvm::omp::Directive::OMPD_begin_assumes) {
3210
86
    OMPAssumeScoped.push_back(AA);
3211
86
    return;
3212
86
  }
3213
3214
  // Global assumes without assumption clauses are ignored.
3215
59
  if (Assumptions.empty())
3216
18
    return;
3217
3218
41
  assert(DKind == llvm::omp::Directive::OMPD_assumes &&
3219
41
         "Unexpected omp assumption directive!");
3220
41
  OMPAssumeGlobal.push_back(AA);
3221
3222
  // The OMPAssumeGlobal scope above will take care of new declarations but
3223
  // we also want to apply the assumption to existing ones, e.g., to
3224
  // declarations in included headers. To this end, we traverse all existing
3225
  // declaration contexts and annotate function declarations here.
3226
41
  SmallVector<DeclContext *, 8> DeclContexts;
3227
41
  auto *Ctx = CurContext;
3228
45
  while (Ctx->getLexicalParent())
3229
4
    Ctx = Ctx->getLexicalParent();
3230
41
  DeclContexts.push_back(Ctx);
3231
5.67k
  while (!DeclContexts.empty()) {
3232
5.63k
    DeclContext *DC = DeclContexts.pop_back_val();
3233
8.96k
    for (auto *SubDC : DC->decls()) {
3234
8.96k
      if (SubDC->isInvalidDecl())
3235
0
        continue;
3236
8.96k
      if (auto *CTD = dyn_cast<ClassTemplateDecl>(SubDC)) {
3237
4
        DeclContexts.push_back(CTD->getTemplatedDecl());
3238
4
        for (auto *S : CTD->specializations())
3239
4
          DeclContexts.push_back(S);
3240
4
        continue;
3241
4
      }
3242
8.96k
      if (auto *DC = dyn_cast<DeclContext>(SubDC))
3243
5.58k
        DeclContexts.push_back(DC);
3244
8.96k
      if (auto *F = dyn_cast<FunctionDecl>(SubDC)) {
3245
5.26k
        F->addAttr(AA);
3246
5.26k
        continue;
3247
5.26k
      }
3248
8.96k
    }
3249
5.63k
  }
3250
41
}
3251
3252
84
void Sema::ActOnOpenMPEndAssumesDirective() {
3253
84
  assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!");
3254
84
  OMPAssumeScoped.pop_back();
3255
84
}
3256
3257
OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
3258
170
                                            ArrayRef<OMPClause *> ClauseList) {
3259
  /// For target specific clauses, the requires directive cannot be
3260
  /// specified after the handling of any of the target regions in the
3261
  /// current compilation unit.
3262
170
  ArrayRef<SourceLocation> TargetLocations =
3263
170
      DSAStack->getEncounteredTargetLocs();
3264
170
  SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3265
170
  if (!TargetLocations.empty() || 
!AtomicLoc.isInvalid()165
) {
3266
8
    for (const OMPClause *CNew : ClauseList) {
3267
      // Check if any of the requires clauses affect target regions.
3268
8
      if (isa<OMPUnifiedSharedMemoryClause>(CNew) ||
3269
7
          isa<OMPUnifiedAddressClause>(CNew) ||
3270
6
          isa<OMPReverseOffloadClause>(CNew) ||
3271
5
          isa<OMPDynamicAllocatorsClause>(CNew)) {
3272
4
        Diag(Loc, diag::err_omp_directive_before_requires)
3273
4
            << "target" << getOpenMPClauseName(CNew->getClauseKind());
3274
4
        for (SourceLocation TargetLoc : TargetLocations) {
3275
4
          Diag(TargetLoc, diag::note_omp_requires_encountered_directive)
3276
4
              << "target";
3277
4
        }
3278
4
      } else if (!AtomicLoc.isInvalid() &&
3279
3
                 isa<OMPAtomicDefaultMemOrderClause>(CNew)) {
3280
3
        Diag(Loc, diag::err_omp_directive_before_requires)
3281
3
            << "atomic" << getOpenMPClauseName(CNew->getClauseKind());
3282
3
        Diag(AtomicLoc, diag::note_omp_requires_encountered_directive)
3283
3
            << "atomic";
3284
3
      }
3285
8
    }
3286
8
  }
3287
3288
170
  if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3289
155
    return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
3290
155
                                   ClauseList);
3291
15
  return nullptr;
3292
15
}
3293
3294
static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3295
                              const ValueDecl *D,
3296
                              const DSAStackTy::DSAVarData &DVar,
3297
5.29k
                              bool IsLoopIterVar) {
3298
5.29k
  if (DVar.RefExpr) {
3299
4.15k
    SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
3300
4.15k
        << getOpenMPClauseName(DVar.CKind);
3301
4.15k
    return;
3302
4.15k
  }
3303
1.14k
  enum {
3304
1.14k
    PDSA_StaticMemberShared,
3305
1.14k
    PDSA_StaticLocalVarShared,
3306
1.14k
    PDSA_LoopIterVarPrivate,
3307
1.14k
    PDSA_LoopIterVarLinear,
3308
1.14k
    PDSA_LoopIterVarLastprivate,
3309
1.14k
    PDSA_ConstVarShared,
3310
1.14k
    PDSA_GlobalVarShared,
3311
1.14k
    PDSA_TaskVarFirstprivate,
3312
1.14k
    PDSA_LocalVarPrivate,
3313
1.14k
    PDSA_Implicit
3314
1.14k
  } Reason = PDSA_Implicit;
3315
1.14k
  bool ReportHint = false;
3316
1.14k
  auto ReportLoc = D->getLocation();
3317
1.14k
  auto *VD = dyn_cast<VarDecl>(D);
3318
1.14k
  if (IsLoopIterVar) {
3319
0
    if (DVar.CKind == OMPC_private)
3320
0
      Reason = PDSA_LoopIterVarPrivate;
3321
0
    else if (DVar.CKind == OMPC_lastprivate)
3322
0
      Reason = PDSA_LoopIterVarLastprivate;
3323
0
    else
3324
0
      Reason = PDSA_LoopIterVarLinear;
3325
1.14k
  } else if (isOpenMPTaskingDirective(DVar.DKind) &&
3326
12
             DVar.CKind == OMPC_firstprivate) {
3327
12
    Reason = PDSA_TaskVarFirstprivate;
3328
12
    ReportLoc = DVar.ImplicitDSALoc;
3329
1.13k
  } else if (VD && VD->isStaticLocal())
3330
0
    Reason = PDSA_StaticLocalVarShared;
3331
1.13k
  else if (VD && VD->isStaticDataMember())
3332
1.00k
    Reason = PDSA_StaticMemberShared;
3333
126
  else if (VD && VD->isFileVarDecl())
3334
0
    Reason = PDSA_GlobalVarShared;
3335
126
  else if (D->getType().isConstant(SemaRef.getASTContext()))
3336
0
    Reason = PDSA_ConstVarShared;
3337
126
  else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3338
54
    ReportHint = true;
3339
54
    Reason = PDSA_LocalVarPrivate;
3340
54
  }
3341
1.14k
  if (Reason != PDSA_Implicit) {
3342
1.07k
    SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
3343
1.07k
        << Reason << ReportHint
3344
1.07k
        << getOpenMPDirectiveName(Stack->getCurrentDirective());
3345
72
  } else if (DVar.ImplicitDSALoc.isValid()) {
3346
12
    SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
3347
12
        << getOpenMPClauseName(DVar.CKind);
3348
12
  }
3349
1.14k
}
3350
3351
static OpenMPMapClauseKind
3352
getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
3353
16.9k
                             bool IsAggregateOrDeclareTarget) {
3354
16.9k
  OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
3355
16.9k
  switch (M) {
3356
40
  case OMPC_DEFAULTMAP_MODIFIER_alloc:
3357
40
    Kind = OMPC_MAP_alloc;
3358
40
    break;
3359
32
  case OMPC_DEFAULTMAP_MODIFIER_to:
3360
32
    Kind = OMPC_MAP_to;
3361
32
    break;
3362
32
  case OMPC_DEFAULTMAP_MODIFIER_from:
3363
32
    Kind = OMPC_MAP_from;
3364
32
    break;
3365
344
  case OMPC_DEFAULTMAP_MODIFIER_tofrom:
3366
344
    Kind = OMPC_MAP_tofrom;
3367
344
    break;
3368
32
  case OMPC_DEFAULTMAP_MODIFIER_present:
3369
    // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description]
3370
    // If implicit-behavior is present, each variable referenced in the
3371
    // construct in the category specified by variable-category is treated as if
3372
    // it had been listed in a map clause with the map-type of alloc and
3373
    // map-type-modifier of present.
3374
32
    Kind = OMPC_MAP_alloc;
3375
32
    break;
3376
0
  case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
3377
0
  case OMPC_DEFAULTMAP_MODIFIER_last:
3378
0
    llvm_unreachable("Unexpected defaultmap implicit behavior");
3379
24
  case OMPC_DEFAULTMAP_MODIFIER_none:
3380
32
  case OMPC_DEFAULTMAP_MODIFIER_default:
3381
16.4k
  case OMPC_DEFAULTMAP_MODIFIER_unknown:
3382
    // IsAggregateOrDeclareTarget could be true if:
3383
    // 1. the implicit behavior for aggregate is tofrom
3384
    // 2. it's a declare target link
3385
16.4k
    if (IsAggregateOrDeclareTarget) {
3386
16.4k
      Kind = OMPC_MAP_tofrom;
3387
16.4k
      break;
3388
16.4k
    }
3389
0
    llvm_unreachable("Unexpected defaultmap implicit behavior");
3390
16.9k
  }
3391
16.9k
  assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
3392
16.9k
  return Kind;
3393
16.9k
}
3394
3395
namespace {
3396
class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
3397
  DSAStackTy *Stack;
3398
  Sema &SemaRef;
3399
  bool ErrorFound = false;
3400
  bool TryCaptureCXXThisMembers = false;
3401
  CapturedStmt *CS = nullptr;
3402
  const static unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_pointer + 1;
3403
  llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
3404
  llvm::SmallVector<Expr *, 4> ImplicitMap[DefaultmapKindNum][OMPC_MAP_delete];
3405
  llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
3406
      ImplicitMapModifier[DefaultmapKindNum];
3407
  Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
3408
  llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
3409
3410
64.9k
  void VisitSubCaptures(OMPExecutableDirective *S) {
3411
    // Check implicitly captured variables.
3412
64.9k
    if (!S->hasAssociatedStmt() || 
!S->getAssociatedStmt()63.1k
)
3413
1.79k
      return;
3414
63.1k
    if (S->getDirectiveKind() == OMPD_atomic ||
3415
62.7k
        S->getDirectiveKind() == OMPD_critical ||
3416
62.4k
        S->getDirectiveKind() == OMPD_section ||
3417
61.8k
        S->getDirectiveKind() == OMPD_master) {
3418
1.37k
      Visit(S->getAssociatedStmt());
3419
1.37k
      return;
3420
1.37k
    }
3421
61.7k
    visitSubCaptures(S->getInnermostCapturedStmt());
3422
    // Try to capture inner this->member references to generate correct mappings
3423
    // and diagnostics.
3424
61.7k
    if (TryCaptureCXXThisMembers ||
3425
61.6k
        (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3426
27.6k
         llvm::any_of(S->getInnermostCapturedStmt()->captures(),
3427
26.8k
                      [](const CapturedStmt::Capture &C) {
3428
26.8k
                        return C.capturesThis();
3429
626
                      }))) {
3430
626
      bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
3431
626
      TryCaptureCXXThisMembers = true;
3432
626
      Visit(S->getInnermostCapturedStmt()->getCapturedStmt());
3433
626
      TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
3434
626
    }
3435
    // In tasks firstprivates are not captured anymore, need to analyze them
3436
    // explicitly.
3437
61.7k
    if (isOpenMPTaskingDirective(S->getDirectiveKind()) &&
3438
7.03k
        !isOpenMPTaskLoopDirective(S->getDirectiveKind())) {
3439
1.31k
      for (OMPClause *C : S->clauses())
3440
1.19k
        if (auto *FC = dyn_cast<OMPFirstprivateClause>(C)) {
3441
668
          for (Expr *Ref : FC->varlists())
3442
1.18k
            Visit(Ref);
3443
668
        }
3444
1.31k
    }
3445
61.7k
  }
3446
3447
public:
3448
775k
  void VisitDeclRefExpr(DeclRefExpr *E) {
3449
775k
    if (TryCaptureCXXThisMembers || 
E->isTypeDependent()773k
||
3450
773k
        E->isValueDependent() || E->containsUnexpandedParameterPack() ||
3451
773k
        E->isInstantiationDependent())
3452
2.12k
      return;
3453
773k
    if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
3454
      // Check the datasharing rules for the expressions in the clauses.
3455
695k
      if (!CS) {
3456
190
        if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3457
90
          if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
3458
90
            Visit(CED->getInit());
3459
90
            return;
3460
90
          }
3461
695k
      } else if (VD->isImplicit() || 
isa<OMPCapturedExprDecl>(VD)685k
)
3462
        // Do not analyze internal variables and do not enclose them into
3463
        // implicit clauses.
3464
10.0k
        return;
3465
685k
      VD = VD->getCanonicalDecl();
3466
      // Skip internally declared variables.
3467
685k
      if (VD->hasLocalStorage() && 
CS650k
&&
!CS->capturesVariable(VD)650k
&&
3468
270k
          !Stack->isImplicitTaskFirstprivate(VD))
3469
263k
        return;
3470
      // Skip allocators in uses_allocators clauses.
3471
422k
      if (Stack->isUsesAllocatorsDecl(VD).hasValue())
3472
138
        return;
3473
3474
422k
      DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
3475
      // Check if the variable has explicit DSA set and stop analysis if it so.
3476
422k
      if (DVar.RefExpr || 
!ImplicitDeclarations.insert(VD).second305k
)
3477
275k
        return;
3478
3479
      // Skip internally declared static variables.
3480
146k
      llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3481
146k
          OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
3482
146k
      if (VD->hasGlobalStorage() && 
CS10.0k
&&
!CS->capturesVariable(VD)10.0k
&&
3483
2.85k
          (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
3484
2.84k
           !Res || 
*Res != OMPDeclareTargetDeclAttr::MT_Link102
) &&
3485
2.79k
          !Stack->isImplicitTaskFirstprivate(VD))
3486
2.77k
        return;
3487
3488
143k
      SourceLocation ELoc = E->getExprLoc();
3489
143k
      OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3490
      // The default(none) clause requires that each variable that is referenced
3491
      // in the construct, and does not have a predetermined data-sharing
3492
      // attribute, must have its data-sharing attribute explicitly determined
3493
      // by being listed in a data-sharing attribute clause.
3494
143k
      if (DVar.CKind == OMPC_unknown &&
3495
143k
          (Stack->getDefaultDSA() == DSA_none ||
3496
142k
           Stack->getDefaultDSA() == DSA_firstprivate) &&
3497
466
          isImplicitOrExplicitTaskingRegion(DKind) &&
3498
466
          VarsWithInheritedDSA.count(VD) == 0) {
3499
466
        bool InheritedDSA = Stack->getDefaultDSA() == DSA_none;
3500
466
        if (!InheritedDSA && 
Stack->getDefaultDSA() == DSA_firstprivate66
) {
3501
66
          DSAStackTy::DSAVarData DVar =
3502
66
              Stack->getImplicitDSA(VD, /*FromParent=*/false);
3503
66
          InheritedDSA = DVar.CKind == OMPC_unknown;
3504
66
        }
3505
466
        if (InheritedDSA)
3506
448
          VarsWithInheritedDSA[VD] = E;
3507
466
        return;
3508
466
      }
3509
3510
      // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
3511
      // If implicit-behavior is none, each variable referenced in the
3512
      // construct that does not have a predetermined data-sharing attribute
3513
      // and does not appear in a to or link clause on a declare target
3514
      // directive must be listed in a data-mapping attribute clause, a
3515
      // data-haring attribute clause (including a data-sharing attribute
3516
      // clause on a combined construct where target. is one of the
3517
      // constituent constructs), or an is_device_ptr clause.
3518
143k
      OpenMPDefaultmapClauseKind ClauseKind =
3519
143k
          getVariableCategoryFromDecl(SemaRef.getLangOpts(), VD);
3520
143k
      if (SemaRef.getLangOpts().OpenMP >= 50) {
3521
92.1k
        bool IsModifierNone = Stack->getDefaultmapModifier(ClauseKind) ==
3522
92.1k
                              OMPC_DEFAULTMAP_MODIFIER_none;
3523
92.1k
        if (DVar.CKind == OMPC_unknown && 
IsModifierNone91.9k
&&
3524
268
            VarsWithInheritedDSA.count(VD) == 0 && !Res) {
3525
          // Only check for data-mapping attribute and is_device_ptr here
3526
          // since we have already make sure that the declaration does not
3527
          // have a data-sharing attribute above
3528
244
          if (!Stack->checkMappableExprComponentListsForDecl(
3529
244
                  VD, /*CurrentRegionOnly=*/true,
3530
244
                  [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
3531
244
                           MapExprComponents,
3532
166
                       OpenMPClauseKind) {
3533
166
                    auto MI = MapExprComponents.rbegin();
3534
166
                    auto ME = MapExprComponents.rend();
3535
166
                    return MI != ME && MI->getAssociatedDeclaration() == VD;
3536
78
                  })) {
3537
78
            VarsWithInheritedDSA[VD] = E;
3538
78
            return;
3539
78
          }
3540
143k
        }
3541
92.1k
      }
3542
143k
      if (SemaRef.getLangOpts().OpenMP > 50) {
3543
3.79k
        bool IsModifierPresent = Stack->getDefaultmapModifier(ClauseKind) ==
3544
3.79k
                                 OMPC_DEFAULTMAP_MODIFIER_present;
3545
3.79k
        if (IsModifierPresent) {
3546
32
          if (llvm::find(ImplicitMapModifier[ClauseKind],
3547
32
                         OMPC_MAP_MODIFIER_present) ==
3548
32
              std::end(ImplicitMapModifier[ClauseKind])) {
3549
32
            ImplicitMapModifier[ClauseKind].push_back(
3550
32
                OMPC_MAP_MODIFIER_present);
3551
32
          }
3552
32
        }
3553
3.79k
      }
3554
3555
143k
      if (isOpenMPTargetExecutionDirective(DKind) &&
3556
67.7k
          !Stack->isLoopControlVariable(VD).first) {
3557
67.7k
        if (!Stack->checkMappableExprComponentListsForDecl(
3558
67.7k
                VD, /*CurrentRegionOnly=*/true,
3559
67.7k
                [](OMPClauseMappableExprCommon::MappableExprComponentListRef
3560
67.7k
                       StackComponents,
3561
2.72k
                   OpenMPClauseKind) {
3562
                  // Variable is used if it has been marked as an array, array
3563
                  // section, array shaping or the variable iself.
3564
2.72k
                  return StackComponents.size() == 1 ||
3565
1.11k
                         std::all_of(
3566
1.11k
                             std::next(StackComponents.rbegin()),
3567
1.11k
                             StackComponents.rend(),
3568
1.11k
                             [](const OMPClauseMappableExprCommon::
3569
1.56k
                                    MappableComponent &MC) {
3570
1.56k
                               return MC.getAssociatedDeclaration() ==
3571
1.56k
                                          nullptr &&
3572
1.27k
                                      (isa<OMPArraySectionExpr>(
3573
1.27k
                                           MC.getAssociatedExpression()) ||
3574
384
                                       isa<OMPArrayShapingExpr>(
3575
384
                                           MC.getAssociatedExpression()) ||
3576
368
                                       isa<ArraySubscriptExpr>(
3577
368
                                           MC.getAssociatedExpression()));
3578
1.56k
                             });
3579
65.2k
                })) {
3580
65.2k
          bool IsFirstprivate = false;
3581
          // By default lambdas are captured as firstprivates.
3582
65.2k
          if (const auto *RD =
3583
5.49k
                  VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
3584
5.49k
            IsFirstprivate = RD->isLambda();
3585
65.2k
          IsFirstprivate =
3586
65.2k
              IsFirstprivate || 
(65.2k
Stack->mustBeFirstprivate(ClauseKind)65.2k
&&
!Res49.7k
);
3587
65.2k
          if (IsFirstprivate) {
3588
49.7k
            ImplicitFirstprivate.emplace_back(E);
3589
15.5k
          } else {
3590
15.5k
            OpenMPDefaultmapClauseModifier M =
3591
15.5k
                Stack->getDefaultmapModifier(ClauseKind);
3592
15.5k
            OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3593
15.5k
                M, ClauseKind == OMPC_DEFAULTMAP_aggregate || 
Res440
);
3594
15.5k
            ImplicitMap[ClauseKind][Kind].emplace_back(E);
3595
15.5k
          }
3596
65.2k
          return;
3597
65.2k
        }
3598
77.8k
      }
3599
3600
      // OpenMP [2.9.3.6, Restrictions, p.2]
3601
      //  A list item that appears in a reduction clause of the innermost
3602
      //  enclosing worksharing or parallel construct may not be accessed in an
3603
      //  explicit task.
3604
77.8k
      DVar = Stack->hasInnermostDSA(
3605
77.8k
          VD,
3606
18.4k
          [](OpenMPClauseKind C, bool AppliedToPointee) {
3607
18.4k
            return C == OMPC_reduction && 
!AppliedToPointee86
;
3608
18.4k
          },
3609
45.4k
          [](OpenMPDirectiveKind K) {
3610
45.4k
            return isOpenMPParallelDirective(K) ||
3611
35.0k
                   isOpenMPWorksharingDirective(K) || 
isOpenMPTeamsDirective(K)34.7k
;
3612
45.4k
          },
3613
77.8k
          /*FromParent=*/true);
3614
77.8k
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind == OMPC_reduction10.7k
) {
3615
84
        ErrorFound = true;
3616
84
        SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3617
84
        reportOriginalDsa(SemaRef, Stack, VD, DVar);
3618
84
        return;
3619
84
      }
3620
3621
      // Define implicit data-sharing attributes for task.
3622
77.7k
      DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
3623
77.7k
      if (((isOpenMPTaskingDirective(DKind) && 
DVar.CKind != OMPC_shared10.6k
) ||
3624
73.5k
           (Stack->getDefaultDSA() == DSA_firstprivate &&
3625
0
            DVar.CKind == OMPC_firstprivate && !DVar.RefExpr)) &&
3626
4.19k
          !Stack->isLoopControlVariable(VD).first) {
3627
4.19k
        ImplicitFirstprivate.push_back(E);
3628
4.19k
        return;
3629
4.19k
      }
3630
3631
      // Store implicitly used globals with declare target link for parent
3632
      // target.
3633
73.5k
      if (!isOpenMPTargetExecutionDirective(DKind) && 
Res71.1k
&&
3634
8
          *Res == OMPDeclareTargetDeclAttr::MT_Link) {
3635
8
        Stack->addToParentTargetRegionLinkGlobals(E);
3636
8
        return;
3637
8
      }
3638
73.5k
    }
3639
773k
  }
3640
7.29k
  void VisitMemberExpr(MemberExpr *E) {
3641
7.29k
    if (E->isTypeDependent() || E->isValueDependent() ||
3642
7.29k
        E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
3643
0
      return;
3644
7.29k
    auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
3645
7.29k
    OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
3646
7.29k
    if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParenCasts())) {
3647
3.05k
      if (!FD)
3648
40
        return;
3649
3.01k
      DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
3650
      // Check if the variable has explicit DSA set and stop analysis if it
3651
      // so.
3652
3.01k
      if (DVar.RefExpr || 
!ImplicitDeclarations.insert(FD).second2.70k
)
3653
887
        return;
3654
3655
2.13k
      if (isOpenMPTargetExecutionDirective(DKind) &&
3656
1.57k
          !Stack->isLoopControlVariable(FD).first &&
3657
1.57k
          !Stack->checkMappableExprComponentListsForDecl(
3658
1.57k
              FD, /*CurrentRegionOnly=*/true,
3659
1.57k
              [](OMPClauseMappableExprCommon::MappableExprComponentListRef
3660
1.57k
                     StackComponents,
3661
128
                 OpenMPClauseKind) {
3662
128
                return isa<CXXThisExpr>(
3663
128
                    cast<MemberExpr>(
3664
128
                        StackComponents.back().getAssociatedExpression())
3665
128
                        ->getBase()
3666
128
                        ->IgnoreParens());
3667
1.44k
              })) {
3668
        // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
3669
        //  A bit-field cannot appear in a map clause.
3670
        //
3671
1.44k
        if (FD->isBitField())
3672
10
          return;
3673
3674
        // Check to see if the member expression is referencing a class that
3675
        // has already been explicitly mapped
3676
1.43k
        if (Stack->isClassPreviouslyMapped(TE->getType()))
3677
30
          return;
3678
3679
1.40k
        OpenMPDefaultmapClauseModifier Modifier =
3680
1.40k
            Stack->getDefaultmapModifier(OMPC_DEFAULTMAP_aggregate);
3681
1.40k
        OpenMPDefaultmapClauseKind ClauseKind =
3682
1.40k
            getVariableCategoryFromDecl(SemaRef.getLangOpts(), FD);
3683
1.40k
        OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
3684
1.40k
            Modifier, /*IsAggregateOrDeclareTarget*/ true);
3685
1.40k
        ImplicitMap[ClauseKind][Kind].emplace_back(E);
3686
1.40k
        return;
3687
1.40k
      }
3688
3689
684
      SourceLocation ELoc = E->getExprLoc();
3690
      // OpenMP [2.9.3.6, Restrictions, p.2]
3691
      //  A list item that appears in a reduction clause of the innermost
3692
      //  enclosing worksharing or parallel construct may not be accessed in
3693
      //  an  explicit task.
3694
684
      DVar = Stack->hasInnermostDSA(
3695
684
          FD,
3696
18
          [](OpenMPClauseKind C, bool AppliedToPointee) {
3697
18
            return C == OMPC_reduction && 
!AppliedToPointee0
;
3698
18
          },
3699
380
          [](OpenMPDirectiveKind K) {
3700
380
            return isOpenMPParallelDirective(K) ||
3701
362
                   isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
3702
380
          },
3703
684
          /*FromParent=*/true);
3704
684
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind == OMPC_reduction78
) {
3705
0
        ErrorFound = true;
3706
0
        SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
3707
0
        reportOriginalDsa(SemaRef, Stack, FD, DVar);
3708
0
        return;
3709
0
      }
3710
3711
      // Define implicit data-sharing attributes for task.
3712
684
      DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
3713
684
      if (isOpenMPTaskingDirective(DKind) && 
DVar.CKind != OMPC_shared78
&&
3714
0
          !Stack->isLoopControlVariable(FD).first) {
3715
        // Check if there is a captured expression for the current field in the
3716
        // region. Do not mark it as firstprivate unless there is no captured
3717
        // expression.
3718
        // TODO: try to make it firstprivate.
3719
0
        if (DVar.CKind != OMPC_unknown)
3720
0
          ImplicitFirstprivate.push_back(E);
3721
0
      }
3722
684
      return;
3723
684
    }
3724
4.23k
    if (isOpenMPTargetExecutionDirective(DKind)) {
3725
2.50k
      OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
3726
2.50k
      if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
3727
2.50k
                                        Stack->getCurrentDirective(),
3728
2.50k
                                        /*NoDiagnose=*/true))
3729
20
        return;
3730
2.48k
      const auto *VD = cast<ValueDecl>(
3731
2.48k
          CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
3732
2.48k
      if (!Stack->checkMappableExprComponentListsForDecl(
3733
2.48k
              VD, /*CurrentRegionOnly=*/true,
3734
2.48k
              [&CurComponents](
3735
2.48k
                  OMPClauseMappableExprCommon::MappableExprComponentListRef
3736
2.48k
                      StackComponents,
3737
720
                  OpenMPClauseKind) {
3738
720
                auto CCI = CurComponents.rbegin();
3739
720
                auto CCE = CurComponents.rend();
3740
1.25k
                for (const auto &SC : llvm::reverse(StackComponents)) {
3741
                  // Do both expressions have the same kind?
3742
1.25k
                  if (CCI->getAssociatedExpression()->getStmtClass() !=
3743
1.25k
                      SC.getAssociatedExpression()->getStmtClass())
3744
80
                    if (!((isa<OMPArraySectionExpr>(
3745
80
                               SC.getAssociatedExpression()) ||
3746
0
                           isa<OMPArrayShapingExpr>(
3747
0
                               SC.getAssociatedExpression())) &&
3748
80
                          isa<ArraySubscriptExpr>(
3749
80
                              CCI->getAssociatedExpression())))
3750
0
                      return false;
3751
3752
1.25k
                  const Decl *CCD = CCI->getAssociatedDeclaration();
3753
1.25k
                  const Decl *SCD = SC.getAssociatedDeclaration();
3754
1.17k
                  CCD = CCD ? CCD->getCanonicalDecl() : 
nullptr80
;
3755
1.17k
                  SCD = SCD ? SCD->getCanonicalDecl() : 
nullptr80
;
3756
1.25k
                  if (SCD != CCD)
3757
302
                    return false;
3758
954
                  std::advance(CCI, 1);
3759
954
                  if (CCI == CCE)
3760
122
                    break;
3761
954
                }
3762
418
                return true;
3763
2.06k
              })) {
3764
2.06k
        Visit(E->getBase());
3765
2.06k
      }
3766
1.72k
    } else if (!TryCaptureCXXThisMembers) {
3767
1.72k
      Visit(E->getBase());
3768
1.72k
    }
3769
4.23k
  }
3770
64.9k
  void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
3771
31.1k
    for (OMPClause *C : S->clauses()) {
3772
      // Skip analysis of arguments of implicitly defined firstprivate clause
3773
      // for task|target directives.
3774
      // Skip analysis of arguments of implicitly defined map clause for target
3775
      // directives.
3776
31.1k
      if (C && !((isa<OMPFirstprivateClause>(C) || 
isa<OMPMapClause>(C)25.7k
) &&
3777
8.07k
                 C->isImplicit() &&
3778
28.4k
                 
!isOpenMPTaskingDirective(Stack->getCurrentDirective())3.19k
)) {
3779
33.3k
        for (Stmt *CC : C->children()) {
3780
33.3k
          if (CC)
3781
32.3k
            Visit(CC);
3782
33.3k
        }
3783
28.4k
      }
3784
31.1k
    }
3785
    // Check implicitly captured variables.
3786
64.9k
    VisitSubCaptures(S);
3787
64.9k
  }
3788
1.53M
  void VisitStmt(Stmt *S) {
3789
2.01M
    for (Stmt *C : S->children()) {
3790
2.01M
      if (C) {
3791
        // Check implicitly captured variables in the task-based directives to
3792
        // check if they must be firstprivatized.
3793
1.90M
        Visit(C);
3794
1.90M
      }
3795
2.01M
    }
3796
1.53M
  }
3797
3798
241k
  void visitSubCaptures(CapturedStmt *S) {
3799
244k
    for (const CapturedStmt::Capture &Cap : S->captures()) {
3800
244k
      if (!Cap.capturesVariable() && 
!Cap.capturesVariableByCopy()81.1k
)
3801
8.42k
        continue;
3802
236k
      VarDecl *VD = Cap.getCapturedVar();
3803
      // Do not try to map the variable if it or its sub-component was mapped
3804
      // already.
3805
236k
      if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
3806
115k
          Stack->checkMappableExprComponentListsForDecl(
3807
115k
              VD, /*CurrentRegionOnly=*/true,
3808
115k
              [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
3809
3.63k
                 OpenMPClauseKind) { return true; }))
3810
3.63k
        continue;
3811
232k
      DeclRefExpr *DRE = buildDeclRefExpr(
3812
232k
          SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
3813
232k
          Cap.getLocation(), /*RefersToCapture=*/true);
3814
232k
      Visit(DRE);
3815
232k
    }
3816
241k
  }
3817
209k
  bool isErrorFound() const { return ErrorFound; }
3818
419k
  ArrayRef<Expr *> getImplicitFirstprivate() const {
3819
419k
    return ImplicitFirstprivate;
3820
419k
  }
3821
  ArrayRef<Expr *> getImplicitMap(OpenMPDefaultmapClauseKind DK,
3822
2.51M
                                  OpenMPMapClauseKind MK) const {
3823
2.51M
    return ImplicitMap[DK][MK];
3824
2.51M
  }
3825
  ArrayRef<OpenMPMapModifierKind>
3826
628k
  getImplicitMapModifier(OpenMPDefaultmapClauseKind Kind) const {
3827
628k
    return ImplicitMapModifier[Kind];
3828
628k
  }
3829
210k
  const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
3830
210k
    return VarsWithInheritedDSA;
3831
210k
  }
3832
3833
  DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
3834
210k
      : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
3835
    // Process declare target link variables for the target directives.
3836
210k
    if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
3837
90.1k
      for (DeclRefExpr *E : Stack->getLinkGlobals())
3838
8
        Visit(E);
3839
90.1k
    }
3840
210k
  }
3841
};
3842
} // namespace
3843
3844
327k
void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
3845
327k
  switch (DKind) {
3846
28.9k
  case OMPD_parallel:
3847
34.0k
  case OMPD_parallel_for:
3848
40.5k
  case OMPD_parallel_for_simd:
3849
44.4k
  case OMPD_parallel_sections:
3850
47.5k
  case OMPD_parallel_master:
3851
68.7k
  case OMPD_teams:
3852
73.6k
  case OMPD_teams_distribute:
3853
79.2k
  case OMPD_teams_distribute_simd: {
3854
79.2k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3855
79.2k
    QualType KmpInt32PtrTy =
3856
79.2k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3857
79.2k
    Sema::CapturedParamNameType Params[] = {
3858
79.2k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3859
79.2k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3860
79.2k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3861
79.2k
    };
3862
79.2k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3863
79.2k
                             Params);
3864
79.2k
    break;
3865
73.6k
  }
3866
7.89k
  case OMPD_target_teams:
3867
15.9k
  case OMPD_target_parallel:
3868
24.3k
  case OMPD_target_parallel_for:
3869
32.8k
  case OMPD_target_parallel_for_simd:
3870
40.2k
  case OMPD_target_teams_distribute:
3871
49.6k
  case OMPD_target_teams_distribute_simd: {
3872
49.6k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3873
49.6k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3874
49.6k
    QualType KmpInt32PtrTy =
3875
49.6k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3876
49.6k
    QualType Args[] = {VoidPtrTy};
3877
49.6k
    FunctionProtoType::ExtProtoInfo EPI;
3878
49.6k
    EPI.Variadic = true;
3879
49.6k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3880
49.6k
    Sema::CapturedParamNameType Params[] = {
3881
49.6k
        std::make_pair(".global_tid.", KmpInt32Ty),
3882
49.6k
        std::make_pair(".part_id.", KmpInt32PtrTy),
3883
49.6k
        std::make_pair(".privates.", VoidPtrTy),
3884
49.6k
        std::make_pair(
3885
49.6k
            ".copy_fn.",
3886
49.6k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3887
49.6k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3888
49.6k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3889
49.6k
    };
3890
49.6k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3891
49.6k
                             Params, /*OpenMPCaptureLevel=*/0);
3892
    // Mark this captured region as inlined, because we don't use outlined
3893
    // function directly.
3894
49.6k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3895
49.6k
        AlwaysInlineAttr::CreateImplicit(
3896
49.6k
            Context, {}, AttributeCommonInfo::AS_Keyword,
3897
49.6k
            AlwaysInlineAttr::Keyword_forceinline));
3898
49.6k
    Sema::CapturedParamNameType ParamsTarget[] = {
3899
49.6k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3900
49.6k
    };
3901
    // Start a captured region for 'target' with no implicit parameters.
3902
49.6k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3903
49.6k
                             ParamsTarget, /*OpenMPCaptureLevel=*/1);
3904
49.6k
    Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
3905
49.6k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
3906
49.6k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
3907
49.6k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3908
49.6k
    };
3909
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
3910
    // the same implicit parameters.
3911
49.6k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3912
49.6k
                             ParamsTeamsOrParallel, /*OpenMPCaptureLevel=*/2);
3913
49.6k
    break;
3914
40.2k
  }
3915
55.6k
  case OMPD_target:
3916
64.2k
  case OMPD_target_simd: {
3917
64.2k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3918
64.2k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3919
64.2k
    QualType KmpInt32PtrTy =
3920
64.2k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3921
64.2k
    QualType Args[] = {VoidPtrTy};
3922
64.2k
    FunctionProtoType::ExtProtoInfo EPI;
3923
64.2k
    EPI.Variadic = true;
3924
64.2k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3925
64.2k
    Sema::CapturedParamNameType Params[] = {
3926
64.2k
        std::make_pair(".global_tid.", KmpInt32Ty),
3927
64.2k
        std::make_pair(".part_id.", KmpInt32PtrTy),
3928
64.2k
        std::make_pair(".privates.", VoidPtrTy),
3929
64.2k
        std::make_pair(
3930
64.2k
            ".copy_fn.",
3931
64.2k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3932
64.2k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3933
64.2k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3934
64.2k
    };
3935
64.2k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3936
64.2k
                             Params, /*OpenMPCaptureLevel=*/0);
3937
    // Mark this captured region as inlined, because we don't use outlined
3938
    // function directly.
3939
64.2k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3940
64.2k
        AlwaysInlineAttr::CreateImplicit(
3941
64.2k
            Context, {}, AttributeCommonInfo::AS_Keyword,
3942
64.2k
            AlwaysInlineAttr::Keyword_forceinline));
3943
64.2k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3944
64.2k
                             std::make_pair(StringRef(), QualType()),
3945
64.2k
                             /*OpenMPCaptureLevel=*/1);
3946
64.2k
    break;
3947
55.6k
  }
3948
4.45k
  case OMPD_atomic:
3949
6.19k
  case OMPD_critical:
3950
7.50k
  case OMPD_section:
3951
8.88k
  case OMPD_master:
3952
8.88k
    break;
3953
6.60k
  case OMPD_simd:
3954
14.3k
  case OMPD_for:
3955
20.7k
  case OMPD_for_simd:
3956
25.0k
  case OMPD_sections:
3957
27.2k
  case OMPD_single:
3958
30.1k
  case OMPD_taskgroup:
3959
32.2k
  case OMPD_distribute:
3960
37.8k
  case OMPD_distribute_simd:
3961
39.2k
  case OMPD_ordered:
3962
43.1k
  case OMPD_target_data: {
3963
43.1k
    Sema::CapturedParamNameType Params[] = {
3964
43.1k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3965
43.1k
    };
3966
43.1k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3967
43.1k
                             Params);
3968
43.1k
    break;
3969
39.2k
  }
3970
4.26k
  case OMPD_task: {
3971
4.26k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
3972
4.26k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
3973
4.26k
    QualType KmpInt32PtrTy =
3974
4.26k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
3975
4.26k
    QualType Args[] = {VoidPtrTy};
3976
4.26k
    FunctionProtoType::ExtProtoInfo EPI;
3977
4.26k
    EPI.Variadic = true;
3978
4.26k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
3979
4.26k
    Sema::CapturedParamNameType Params[] = {
3980
4.26k
        std::make_pair(".global_tid.", KmpInt32Ty),
3981
4.26k
        std::make_pair(".part_id.", KmpInt32PtrTy),
3982
4.26k
        std::make_pair(".privates.", VoidPtrTy),
3983
4.26k
        std::make_pair(
3984
4.26k
            ".copy_fn.",
3985
4.26k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
3986
4.26k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
3987
4.26k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
3988
4.26k
    };
3989
4.26k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
3990
4.26k
                             Params);
3991
    // Mark this captured region as inlined, because we don't use outlined
3992
    // function directly.
3993
4.26k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
3994
4.26k
        AlwaysInlineAttr::CreateImplicit(
3995
4.26k
            Context, {}, AttributeCommonInfo::AS_Keyword,
3996
4.26k
            AlwaysInlineAttr::Keyword_forceinline));
3997
4.26k
    break;
3998
39.2k
  }
3999
5.28k
  case OMPD_taskloop:
4000
10.4k
  case OMPD_taskloop_simd:
4001
14.5k
  case OMPD_master_taskloop:
4002
19.7k
  case OMPD_master_taskloop_simd: {
4003
19.7k
    QualType KmpInt32Ty =
4004
19.7k
        Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
4005
19.7k
            .withConst();
4006
19.7k
    QualType KmpUInt64Ty =
4007
19.7k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
4008
19.7k
            .withConst();
4009
19.7k
    QualType KmpInt64Ty =
4010
19.7k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
4011
19.7k
            .withConst();
4012
19.7k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4013
19.7k
    QualType KmpInt32PtrTy =
4014
19.7k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4015
19.7k
    QualType Args[] = {VoidPtrTy};
4016
19.7k
    FunctionProtoType::ExtProtoInfo EPI;
4017
19.7k
    EPI.Variadic = true;
4018
19.7k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4019
19.7k
    Sema::CapturedParamNameType Params[] = {
4020
19.7k
        std::make_pair(".global_tid.", KmpInt32Ty),
4021
19.7k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4022
19.7k
        std::make_pair(".privates.", VoidPtrTy),
4023
19.7k
        std::make_pair(
4024
19.7k
            ".copy_fn.",
4025
19.7k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4026
19.7k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4027
19.7k
        std::make_pair(".lb.", KmpUInt64Ty),
4028
19.7k
        std::make_pair(".ub.", KmpUInt64Ty),
4029
19.7k
        std::make_pair(".st.", KmpInt64Ty),
4030
19.7k
        std::make_pair(".liter.", KmpInt32Ty),
4031
19.7k
        std::make_pair(".reductions.", VoidPtrTy),
4032
19.7k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4033
19.7k
    };
4034
19.7k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4035
19.7k
                             Params);
4036
    // Mark this captured region as inlined, because we don't use outlined
4037
    // function directly.
4038
19.7k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4039
19.7k
        AlwaysInlineAttr::CreateImplicit(
4040
19.7k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4041
19.7k
            AlwaysInlineAttr::Keyword_forceinline));
4042
19.7k
    break;
4043
14.5k
  }
4044
3.35k
  case OMPD_parallel_master_taskloop:
4045
7.75k
  case OMPD_parallel_master_taskloop_simd: {
4046
7.75k
    QualType KmpInt32Ty =
4047
7.75k
        Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
4048
7.75k
            .withConst();
4049
7.75k
    QualType KmpUInt64Ty =
4050
7.75k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
4051
7.75k
            .withConst();
4052
7.75k
    QualType KmpInt64Ty =
4053
7.75k
        Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
4054
7.75k
            .withConst();
4055
7.75k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4056
7.75k
    QualType KmpInt32PtrTy =
4057
7.75k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4058
7.75k
    Sema::CapturedParamNameType ParamsParallel[] = {
4059
7.75k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4060
7.75k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4061
7.75k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4062
7.75k
    };
4063
    // Start a captured region for 'parallel'.
4064
7.75k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4065
7.75k
                             ParamsParallel, /*OpenMPCaptureLevel=*/0);
4066
7.75k
    QualType Args[] = {VoidPtrTy};
4067
7.75k
    FunctionProtoType::ExtProtoInfo EPI;
4068
7.75k
    EPI.Variadic = true;
4069
7.75k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4070
7.75k
    Sema::CapturedParamNameType Params[] = {
4071
7.75k
        std::make_pair(".global_tid.", KmpInt32Ty),
4072
7.75k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4073
7.75k
        std::make_pair(".privates.", VoidPtrTy),
4074
7.75k
        std::make_pair(
4075
7.75k
            ".copy_fn.",
4076
7.75k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4077
7.75k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4078
7.75k
        std::make_pair(".lb.", KmpUInt64Ty),
4079
7.75k
        std::make_pair(".ub.", KmpUInt64Ty),
4080
7.75k
        std::make_pair(".st.", KmpInt64Ty),
4081
7.75k
        std::make_pair(".liter.", KmpInt32Ty),
4082
7.75k
        std::make_pair(".reductions.", VoidPtrTy),
4083
7.75k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4084
7.75k
    };
4085
7.75k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4086
7.75k
                             Params, /*OpenMPCaptureLevel=*/1);
4087
    // Mark this captured region as inlined, because we don't use outlined
4088
    // function directly.
4089
7.75k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4090
7.75k
        AlwaysInlineAttr::CreateImplicit(
4091
7.75k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4092
7.75k
            AlwaysInlineAttr::Keyword_forceinline));
4093
7.75k
    break;
4094
3.35k
  }
4095
6.90k
  case OMPD_distribute_parallel_for_simd:
4096
12.3k
  case OMPD_distribute_parallel_for: {
4097
12.3k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4098
12.3k
    QualType KmpInt32PtrTy =
4099
12.3k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4100
12.3k
    Sema::CapturedParamNameType Params[] = {
4101
12.3k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4102
12.3k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4103
12.3k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4104
12.3k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4105
12.3k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4106
12.3k
    };
4107
12.3k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4108
12.3k
                             Params);
4109
12.3k
    break;
4110
6.90k
  }
4111
7.93k
  case OMPD_target_teams_distribute_parallel_for:
4112
17.9k
  case OMPD_target_teams_distribute_parallel_for_simd: {
4113
17.9k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4114
17.9k
    QualType KmpInt32PtrTy =
4115
17.9k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4116
17.9k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4117
4118
17.9k
    QualType Args[] = {VoidPtrTy};
4119
17.9k
    FunctionProtoType::ExtProtoInfo EPI;
4120
17.9k
    EPI.Variadic = true;
4121
17.9k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4122
17.9k
    Sema::CapturedParamNameType Params[] = {
4123
17.9k
        std::make_pair(".global_tid.", KmpInt32Ty),
4124
17.9k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4125
17.9k
        std::make_pair(".privates.", VoidPtrTy),
4126
17.9k
        std::make_pair(
4127
17.9k
            ".copy_fn.",
4128
17.9k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4129
17.9k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4130
17.9k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4131
17.9k
    };
4132
17.9k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4133
17.9k
                             Params, /*OpenMPCaptureLevel=*/0);
4134
    // Mark this captured region as inlined, because we don't use outlined
4135
    // function directly.
4136
17.9k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4137
17.9k
        AlwaysInlineAttr::CreateImplicit(
4138
17.9k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4139
17.9k
            AlwaysInlineAttr::Keyword_forceinline));
4140
17.9k
    Sema::CapturedParamNameType ParamsTarget[] = {
4141
17.9k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4142
17.9k
    };
4143
    // Start a captured region for 'target' with no implicit parameters.
4144
17.9k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4145
17.9k
                             ParamsTarget, /*OpenMPCaptureLevel=*/1);
4146
4147
17.9k
    Sema::CapturedParamNameType ParamsTeams[] = {
4148
17.9k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4149
17.9k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4150
17.9k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4151
17.9k
    };
4152
    // Start a captured region for 'target' with no implicit parameters.
4153
17.9k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4154
17.9k
                             ParamsTeams, /*OpenMPCaptureLevel=*/2);
4155
4156
17.9k
    Sema::CapturedParamNameType ParamsParallel[] = {
4157
17.9k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4158
17.9k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4159
17.9k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4160
17.9k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4161
17.9k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4162
17.9k
    };
4163
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
4164
    // the same implicit parameters.
4165
17.9k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4166
17.9k
                             ParamsParallel, /*OpenMPCaptureLevel=*/3);
4167
17.9k
    break;
4168
7.93k
  }
4169
4170
5.47k
  case OMPD_teams_distribute_parallel_for:
4171
11.8k
  case OMPD_teams_distribute_parallel_for_simd: {
4172
11.8k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4173
11.8k
    QualType KmpInt32PtrTy =
4174
11.8k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4175
4176
11.8k
    Sema::CapturedParamNameType ParamsTeams[] = {
4177
11.8k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4178
11.8k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4179
11.8k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4180
11.8k
    };
4181
    // Start a captured region for 'target' with no implicit parameters.
4182
11.8k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4183
11.8k
                             ParamsTeams, /*OpenMPCaptureLevel=*/0);
4184
4185
11.8k
    Sema::CapturedParamNameType ParamsParallel[] = {
4186
11.8k
        std::make_pair(".global_tid.", KmpInt32PtrTy),
4187
11.8k
        std::make_pair(".bound_tid.", KmpInt32PtrTy),
4188
11.8k
        std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
4189
11.8k
        std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
4190
11.8k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4191
11.8k
    };
4192
    // Start a captured region for 'teams' or 'parallel'.  Both regions have
4193
    // the same implicit parameters.
4194
11.8k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4195
11.8k
                             ParamsParallel, /*OpenMPCaptureLevel=*/1);
4196
11.8k
    break;
4197
5.47k
  }
4198
4.48k
  case OMPD_target_update:
4199
6.51k
  case OMPD_target_enter_data:
4200
8.52k
  case OMPD_target_exit_data: {
4201
8.52k
    QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
4202
8.52k
    QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4203
8.52k
    QualType KmpInt32PtrTy =
4204
8.52k
        Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
4205
8.52k
    QualType Args[] = {VoidPtrTy};
4206
8.52k
    FunctionProtoType::ExtProtoInfo EPI;
4207
8.52k
    EPI.Variadic = true;
4208
8.52k
    QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
4209
8.52k
    Sema::CapturedParamNameType Params[] = {
4210
8.52k
        std::make_pair(".global_tid.", KmpInt32Ty),
4211
8.52k
        std::make_pair(".part_id.", KmpInt32PtrTy),
4212
8.52k
        std::make_pair(".privates.", VoidPtrTy),
4213
8.52k
        std::make_pair(
4214
8.52k
            ".copy_fn.",
4215
8.52k
            Context.getPointerType(CopyFnType).withConst().withRestrict()),
4216
8.52k
        std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
4217
8.52k
        std::make_pair(StringRef(), QualType()) // __context with shared vars
4218
8.52k
    };
4219
8.52k
    ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
4220
8.52k
                             Params);
4221
    // Mark this captured region as inlined, because we don't use outlined
4222
    // function directly.
4223
8.52k
    getCurCapturedRegion()->TheCapturedDecl->addAttr(
4224
8.52k
        AlwaysInlineAttr::CreateImplicit(
4225
8.52k
            Context, {}, AttributeCommonInfo::AS_Keyword,
4226
8.52k
            AlwaysInlineAttr::Keyword_forceinline));
4227
8.52k
    break;
4228
6.51k
  }
4229
0
  case OMPD_threadprivate:
4230
0
  case OMPD_allocate:
4231
0
  case OMPD_taskyield:
4232
0
  case OMPD_barrier:
4233
0
  case OMPD_taskwait:
4234
0
  case OMPD_cancellation_point:
4235
0
  case OMPD_cancel:
4236
0
  case OMPD_flush:
4237
0
  case OMPD_depobj:
4238
0
  case OMPD_scan:
4239
0
  case OMPD_declare_reduction:
4240
0
  case OMPD_declare_mapper:
4241
0
  case OMPD_declare_simd:
4242
0
  case OMPD_declare_target:
4243
0
  case OMPD_end_declare_target:
4244
0
  case OMPD_requires:
4245
0
  case OMPD_declare_variant:
4246
0
  case OMPD_begin_declare_variant:
4247
0
  case OMPD_end_declare_variant:
4248
0
    llvm_unreachable("OpenMP Directive is not allowed");
4249
0
  case OMPD_unknown:
4250
0
  default:
4251
0
    llvm_unreachable("Unknown OpenMP directive");
4252
327k
  }
4253
327k
  DSAStack->setContext(CurContext);
4254
327k
}
4255
4256
316
int Sema::getNumberOfConstructScopes(unsigned Level) const {
4257
316
  return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
4258
316
}
4259
4260
431k
int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
4261
431k
  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4262
431k
  getOpenMPCaptureRegions(CaptureRegions, DKind);
4263
431k
  return CaptureRegions.size();
4264
431k
}
4265
4266
static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
4267
                                             Expr *CaptureExpr, bool WithInit,
4268
65.4k
                                             bool AsExpression) {
4269
65.4k
  assert(CaptureExpr);
4270
65.4k
  ASTContext &C = S.getASTContext();
4271
62.7k
  Expr *Init = AsExpression ? CaptureExpr : 
CaptureExpr->IgnoreImpCasts()2.71k
;
4272
65.4k
  QualType Ty = Init->getType();
4273
65.4k
  if (CaptureExpr->getObjectKind() == OK_Ordinary && 
CaptureExpr->isGLValue()65.2k
) {
4274
4.01k
    if (S.getLangOpts().CPlusPlus) {
4275
4.01k
      Ty = C.getLValueReferenceType(Ty);
4276
0
    } else {
4277
0
      Ty = C.getPointerType(Ty);
4278
0
      ExprResult Res =
4279
0
          S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
4280
0
      if (!Res.isUsable())
4281
0
        return nullptr;
4282
0
      Init = Res.get();
4283
0
    }
4284
4.01k
    WithInit = true;
4285
4.01k
  }
4286
65.4k
  auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
4287
65.4k
                                          CaptureExpr->getBeginLoc());
4288
65.4k
  if (!WithInit)
4289
140
    CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
4290
65.4k
  S.CurContext->addHiddenDecl(CED);
4291
65.4k
  Sema::TentativeAnalysisScope Trap(S);
4292
65.4k
  S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
4293
65.4k
  return CED;
4294
65.4k
}
4295
4296
static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
4297
2.93k
                                 bool WithInit) {
4298
2.93k
  OMPCapturedExprDecl *CD;
4299
2.93k
  if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
4300
218
    CD = cast<OMPCapturedExprDecl>(VD);
4301
2.71k
  else
4302
2.71k
    CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
4303
2.71k
                          /*AsExpression=*/false);
4304
2.93k
  return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4305
2.93k
                          CaptureExpr->getExprLoc());
4306
2.93k
}
4307
4308
125k
static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
4309
125k
  CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
4310
125k
  if (!Ref) {
4311
62.7k
    OMPCapturedExprDecl *CD = buildCaptureDecl(
4312
62.7k
        S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
4313
62.7k
        /*WithInit=*/true, /*AsExpression=*/true);
4314
62.7k
    Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
4315
62.7k
                           CaptureExpr->getExprLoc());
4316
62.7k
  }
4317
125k
  ExprResult Res = Ref;
4318
125k
  if (!S.getLangOpts().CPlusPlus &&
4319
1.08k
      CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
4320
0
      Ref->getType()->isPointerType()) {
4321
0
    Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
4322
0
    if (!Res.isUsable())
4323
0
      return ExprError();
4324
125k
  }
4325
125k
  return S.DefaultLvalueConversion(Res.get());
4326
125k
}
4327
4328
namespace {
4329
// OpenMP directives parsed in this section are represented as a
4330
// CapturedStatement with an associated statement.  If a syntax error
4331
// is detected during the parsing of the associated statement, the
4332
// compiler must abort processing and close the CapturedStatement.
4333
//
4334
// Combined directives such as 'target parallel' have more than one
4335
// nested CapturedStatements.  This RAII ensures that we unwind out
4336
// of all the nested CapturedStatements when an error is found.
4337
class CaptureRegionUnwinderRAII {
4338
private:
4339
  Sema &S;
4340
  bool &ErrorFound;
4341
  OpenMPDirectiveKind DKind = OMPD_unknown;
4342
4343
public:
4344
  CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
4345
                            OpenMPDirectiveKind DKind)
4346
318k
      : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
4347
318k
  ~CaptureRegionUnwinderRAII() {
4348
318k
    if (ErrorFound) {
4349
12.9k
      int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
4350
31.4k
      while (--ThisCaptureLevel >= 0)
4351
18.5k
        S.ActOnCapturedRegionError();
4352
12.9k
    }
4353
318k
  }
4354
};
4355
} // namespace
4356
4357
1.18M
void Sema::tryCaptureOpenMPLambdas(ValueDecl *V) {
4358
  // Capture variables captured by reference in lambdas for target-based
4359
  // directives.
4360
1.18M
  if (!CurContext->isDependentContext() &&
4361
1.18M
      (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
4362
685k
       isOpenMPTargetDataManagementDirective(
4363
685k
           DSAStack->getCurrentDirective()))) {
4364
522k
    QualType Type = V->getType();
4365
522k
    if (const auto *RD = Type.getCanonicalType()
4366
39.1k
                             .getNonReferenceType()
4367
39.1k
                             ->getAsCXXRecordDecl()) {
4368
39.1k
      bool SavedForceCaptureByReferenceInTargetExecutable =
4369
39.1k
          DSAStack->isForceCaptureByReferenceInTargetExecutable();
4370
39.1k
      DSAStack->setForceCaptureByReferenceInTargetExecutable(
4371
39.1k
          /*V=*/true);
4372
39.1k
      if (RD->isLambda()) {
4373
122
        llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
4374
122
        FieldDecl *ThisCapture;
4375
122
        RD->getCaptureFields(Captures, ThisCapture);
4376
274
        for (const LambdaCapture &LC : RD->captures()) {
4377
274
          if (LC.getCaptureKind() == LCK_ByRef) {
4378
146
            VarDecl *VD = LC.getCapturedVar();
4379
146
            DeclContext *VDC = VD->getDeclContext();
4380
146
            if (!VDC->Encloses(CurContext))
4381
0
              continue;
4382
146
            MarkVariableReferenced(LC.getLocation(), VD);
4383
128
          } else if (LC.getCaptureKind() == LCK_This) {
4384
48
            QualType ThisTy = getCurrentThisType();
4385
48
            if (!ThisTy.isNull() &&
4386
28
                Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
4387
28
              CheckCXXThisCapture(LC.getLocation());
4388
48
          }
4389
274
        }
4390
122
      }
4391
39.1k
      DSAStack->setForceCaptureByReferenceInTargetExecutable(
4392
39.1k
          SavedForceCaptureByReferenceInTargetExecutable);
4393
39.1k
    }
4394
522k
  }
4395
1.18M
}
4396
4397
static bool checkOrderedOrderSpecified(Sema &S,
4398
305k
                                       const ArrayRef<OMPClause *> Clauses) {
4399
305k
  const OMPOrderedClause *Ordered = nullptr;
4400
305k
  const OMPOrderClause *Order = nullptr;
4401
4402
142k
  for (const OMPClause *Clause : Clauses) {
4403
142k
    if (Clause->getClauseKind() == OMPC_ordered)
4404
1.07k
      Ordered = cast<OMPOrderedClause>(Clause);
4405
141k
    else if (Clause->getClauseKind() == OMPC_order) {
4406
179
      Order = cast<OMPOrderClause>(Clause);
4407
179
      if (Order->getKind() != OMPC_ORDER_concurrent)
4408
0
        Order = nullptr;
4409
179
    }
4410
142k
    if (Ordered && 
Order1.41k
)
4411
2
      break;
4412
142k
  }
4413
4414
305k
  if (Ordered && 
Order1.07k
) {
4415
2
    S.Diag(Order->getKindKwLoc(),
4416
2
           diag::err_omp_simple_clause_incompatible_with_ordered)
4417
2
        << getOpenMPClauseName(OMPC_order)
4418
2
        << getOpenMPSimpleClauseTypeName(OMPC_order, OMPC_ORDER_concurrent)
4419
2
        << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
4420
2
    S.Diag(Ordered->getBeginLoc(), diag::note_omp_ordered_param)
4421
2
        << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
4422
2
    return true;
4423
2
  }
4424
305k
  return false;
4425
305k
}
4426
4427
StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
4428
327k
                                      ArrayRef<OMPClause *> Clauses) {
4429
327k
  if (DSAStack->getCurrentDirective() == OMPD_atomic ||
4430
323k
      DSAStack->getCurrentDirective() == OMPD_critical ||
4431
321k
      DSAStack->getCurrentDirective() == OMPD_section ||
4432
320k
      DSAStack->getCurrentDirective() == OMPD_master)
4433
8.88k
    return S;
4434
4435
318k
  bool ErrorFound = false;
4436
318k
  CaptureRegionUnwinderRAII CaptureRegionUnwinder(
4437
318k
      *this, ErrorFound, DSAStack->getCurrentDirective());
4438
318k
  if (!S.isUsable()) {
4439
12.8k
    ErrorFound = true;
4440
12.8k
    return StmtError();
4441
12.8k
  }
4442
4443
305k
  SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4444
305k
  getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
4445
305k
  OMPOrderedClause *OC = nullptr;
4446
305k
  OMPScheduleClause *SC = nullptr;
4447
305k
  SmallVector<const OMPLinearClause *, 4> LCs;
4448
305k
  SmallVector<const OMPClauseWithPreInit *, 4> PICs;
4449
  // This is required for proper codegen.
4450
142k
  for (OMPClause *Clause : Clauses) {
4451
142k
    if (!LangOpts.OpenMPSimd &&
4452
75.8k
        isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
4453
8.21k
        Clause->getClauseKind() == OMPC_in_reduction) {
4454
      // Capture taskgroup task_reduction descriptors inside the tasking regions
4455
      // with the corresponding in_reduction items.
4456
765
      auto *IRC = cast<OMPInReductionClause>(Clause);
4457
765
      for (Expr *E : IRC->taskgroup_descriptors())
4458
898
        if (E)
4459
476
          MarkDeclarationsReferencedInExpr(E);
4460
765
    }
4461
142k
    if (isOpenMPPrivate(Clause->getClauseKind()) ||
4462
89.0k
        Clause->getClauseKind() == OMPC_copyprivate ||
4463
88.9k
        (getLangOpts().OpenMPUseTLS &&
4464
48.1k
         getASTContext().getTargetInfo().isTLSSupported() &&
4465
54.2k
         
Clause->getClauseKind() == OMPC_copyin46.2k
)) {
4466
54.2k
      DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
4467
      // Mark all variables in private list clauses as used in inner region.
4468
73.4k
      for (Stmt *VarRef : Clause->children()) {
4469
73.4k
        if (auto *E = cast_or_null<Expr>(VarRef)) {
4470
73.4k
          MarkDeclarationsReferencedInExpr(E);
4471
73.4k
        }
4472
73.4k
      }
4473
54.2k
      DSAStack->setForceVarCapturing(/*V=*/false);
4474
88.7k
    } else if (CaptureRegions.size() > 1 ||
4475
77.9k
               
CaptureRegions.back() != OMPD_unknown39.7k
) {
4476
77.9k
      if (auto *C = OMPClauseWithPreInit::get(Clause))
4477
23.5k
        PICs.push_back(C);
4478
77.9k
      if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
4479
0
        if (Expr *E = C->getPostUpdateExpr())
4480
0
          MarkDeclarationsReferencedInExpr(E);
4481
0
      }
4482
77.9k
    }
4483
142k
    if (Clause->getClauseKind() == OMPC_schedule)
4484
3.66k
      SC = cast<OMPScheduleClause>(Clause);
4485
139k
    else if (Clause->getClauseKind() == OMPC_ordered)
4486
1.07k
      OC = cast<OMPOrderedClause>(Clause);
4487
138k
    else if (Clause->getClauseKind() == OMPC_linear)
4488
2.61k
      LCs.push_back(cast<OMPLinearClause>(Clause));
4489
142k
  }
4490
  // Capture allocator expressions if used.
4491
305k
  for (Expr *E : DSAStack->getInnerAllocators())
4492
959
    MarkDeclarationsReferencedInExpr(E);
4493
  // OpenMP, 2.7.1 Loop Construct, Restrictions
4494
  // The nonmonotonic modifier cannot be specified if an ordered clause is
4495
  // specified.
4496
305k
  if (SC &&
4497
3.66k
      (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
4498
3.54k
       SC->getSecondScheduleModifier() ==
4499
3.54k
           OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
4500
127
      OC) {
4501
8
    Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
4502
8
             ? SC->getFirstScheduleModifierLoc()
4503
0
             : SC->getSecondScheduleModifierLoc(),
4504
8
         diag::err_omp_simple_clause_incompatible_with_ordered)
4505
8
        << getOpenMPClauseName(OMPC_schedule)
4506
8
        << getOpenMPSimpleClauseTypeName(OMPC_schedule,
4507
8
                                         OMPC_SCHEDULE_MODIFIER_nonmonotonic)
4508
8
        << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4509
8
    ErrorFound = true;
4510
8
  }
4511
  // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
4512
  // If an order(concurrent) clause is present, an ordered clause may not appear
4513
  // on the same directive.
4514
305k
  if (checkOrderedOrderSpecified(*this, Clauses))
4515
2
    ErrorFound = true;
4516
305k
  if (!LCs.empty() && 
OC2.55k
&&
OC->getNumForLoops()32
) {
4517
4
    for (const OMPLinearClause *C : LCs) {
4518
4
      Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
4519
4
          << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4520
4
    }
4521
4
    ErrorFound = true;
4522
4
  }
4523
305k
  if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
4524
95.1k
      isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && 
OC44.6k
&&
4525
230
      OC->getNumForLoops()) {
4526
82
    Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
4527
82
        << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
4528
82
    ErrorFound = true;
4529
82
  }
4530
305k
  if (ErrorFound) {
4531
96
    return StmtError();
4532
96
  }
4533
305k
  StmtResult SR = S;
4534
305k
  unsigned CompletedRegions = 0;
4535
537k
  for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
4536
    // Mark all variables in private list clauses as used in inner region.
4537
    // Required for proper codegen of combined directives.
4538
    // TODO: add processing for other clauses.
4539
537k
    if (ThisCaptureRegion != OMPD_unknown) {
4540
51.9k
      for (const clang::OMPClauseWithPreInit *C : PICs) {
4541
51.9k
        OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
4542
        // Find the particular capture region for the clause if the
4543
        // directive is a combined one with multiple capture regions.
4544
        // If the directive is not a combined one, the capture region
4545
        // associated with the clause is OMPD_unknown and is generated
4546
        // only once.
4547
51.9k
        if (CaptureRegion == ThisCaptureRegion ||
4548
44.1k
            CaptureRegion == OMPD_unknown) {
4549
39.7k
          if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
4550
6.85k
            for (Decl *D : DS->decls())
4551
6.85k
              MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
4552
6.85k
          }
4553
39.7k
        }
4554
51.9k
      }
4555
494k
    }
4556
537k
    if (ThisCaptureRegion == OMPD_target) {
4557
      // Capture allocator traits in the target region. They are used implicitly
4558
      // and, thus, are not captured by default.
4559
57.5k
      for (OMPClause *C : Clauses) {
4560
57.5k
        if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(C)) {
4561
442
          for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
4562
240
               ++I) {
4563
240
            OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
4564
240
            if (Expr *E = D.AllocatorTraits)
4565
36
              MarkDeclarationsReferencedInExpr(E);
4566
240
          }
4567
202
          continue;
4568
202
        }
4569
57.5k
      }
4570
126k
    }
4571
537k
    if (++CompletedRegions == CaptureRegions.size())
4572
305k
      DSAStack->setBodyComplete();
4573
537k
    SR = ActOnCapturedRegionEnd(SR.get());
4574
537k
  }
4575
305k
  return SR;
4576
305k
}
4577
4578
static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
4579
                              OpenMPDirectiveKind CancelRegion,
4580
312k
                              SourceLocation StartLoc) {
4581
  // CancelRegion is only needed for cancel and cancellation_point.
4582
312k
  if (CurrentRegion != OMPD_cancel && 
CurrentRegion != OMPD_cancellation_point311k
)
4583
311k
    return false;
4584
4585
995
  if (CancelRegion == OMPD_parallel || 
CancelRegion == OMPD_for637
||
4586
404
      CancelRegion == OMPD_sections || 
CancelRegion == OMPD_taskgroup252
)
4587
975
    return false;
4588
4589
20
  SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4590
20
      << getOpenMPDirectiveName(CancelRegion);
4591
20
  return true;
4592
20
}
4593
4594
static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
4595
                                  OpenMPDirectiveKind CurrentRegion,
4596
                                  const DeclarationNameInfo &CurrentName,
4597
                                  OpenMPDirectiveKind CancelRegion,
4598
312k
                                  SourceLocation StartLoc) {
4599
312k
  if (Stack->getCurScope()) {
4600
223k
    OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
4601
223k
    OpenMPDirectiveKind OffendingRegion = ParentRegion;
4602
223k
    bool NestingProhibited = false;
4603
223k
    bool CloseNesting = true;
4604
223k
    bool OrphanSeen = false;
4605
223k
    enum {
4606
223k
      NoRecommend,
4607
223k
      ShouldBeInParallelRegion,
4608
223k
      ShouldBeInOrderedRegion,
4609
223k
      ShouldBeInTargetRegion,
4610
223k
      ShouldBeInTeamsRegion,
4611
223k
      ShouldBeInLoopSimdRegion,
4612
223k
    } Recommend = NoRecommend;
4613
223k
    if (isOpenMPSimdDirective(ParentRegion) &&
4614
5.01k
        ((SemaRef.LangOpts.OpenMP <= 45 && 
CurrentRegion != OMPD_ordered3.19k
) ||
4615
1.93k
         (SemaRef.LangOpts.OpenMP >= 50 && 
CurrentRegion != OMPD_ordered1.82k
&&
4616
1.64k
          CurrentRegion != OMPD_simd && 
CurrentRegion != OMPD_atomic1.60k
&&
4617
4.51k
          
CurrentRegion != OMPD_scan1.56k
))) {
4618
      // OpenMP [2.16, Nesting of Regions]
4619
      // OpenMP constructs may not be nested inside a simd region.
4620
      // OpenMP [2.8.1,simd Construct, Restrictions]
4621
      // An ordered construct with the simd clause is the only OpenMP
4622
      // construct that can appear in the simd region.
4623
      // Allowing a SIMD construct nested in another SIMD construct is an
4624
      // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
4625
      // message.
4626
      // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
4627
      // The only OpenMP constructs that can be encountered during execution of
4628
      // a simd region are the atomic construct, the loop construct, the simd
4629
      // construct and the ordered construct with the simd clause.
4630
4.51k
      SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
4631
4.43k
                                 ? diag::err_omp_prohibited_region_simd
4632
76
                                 : diag::warn_omp_nesting_simd)
4633
3.08k
          << (SemaRef.LangOpts.OpenMP >= 50 ? 
11.43k
: 0);
4634
4.51k
      return CurrentRegion != OMPD_simd;
4635
4.51k
    }
4636
219k
    if (ParentRegion == OMPD_atomic) {
4637
      // OpenMP [2.16, Nesting of Regions]
4638
      // OpenMP constructs may not be nested inside an atomic region.
4639
516
      SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
4640
516
      return true;
4641
516
    }
4642
218k
    if (CurrentRegion == OMPD_section) {
4643
      // OpenMP [2.7.2, sections Construct, Restrictions]
4644
      // Orphaned section directives are prohibited. That is, the section
4645
      // directives must appear within the sections construct and must not be
4646
      // encountered elsewhere in the sections region.
4647
923
      if (ParentRegion != OMPD_sections &&
4648
300
          ParentRegion != OMPD_parallel_sections) {
4649
222
        SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
4650
222
            << (ParentRegion != OMPD_unknown)
4651
222
            << getOpenMPDirectiveName(ParentRegion);
4652
222
        return true;
4653
222
      }
4654
701
      return false;
4655
701
    }
4656
    // Allow some constructs (except teams and cancellation constructs) to be
4657
    // orphaned (they could be used in functions, called from OpenMP regions
4658
    // with the required preconditions).
4659
217k
    if (ParentRegion == OMPD_unknown &&
4660
143k
        !isOpenMPNestingTeamsDirective(CurrentRegion) &&
4661
143k
        CurrentRegion != OMPD_cancellation_point &&
4662
143k
        CurrentRegion != OMPD_cancel && 
CurrentRegion != OMPD_scan143k
)
4663
143k
      return false;
4664
73.9k
    if (CurrentRegion == OMPD_cancellation_point ||
4665
73.6k
        CurrentRegion == OMPD_cancel) {
4666
      // OpenMP [2.16, Nesting of Regions]
4667
      // A cancellation point construct for which construct-type-clause is
4668
      // taskgroup must be nested inside a task construct. A cancellation
4669
      // point construct for which construct-type-clause is not taskgroup must
4670
      // be closely nested inside an OpenMP construct that matches the type
4671
      // specified in construct-type-clause.
4672
      // A cancel construct for which construct-type-clause is taskgroup must be
4673
      // nested inside a task construct. A cancel construct for which
4674
      // construct-type-clause is not taskgroup must be closely nested inside an
4675
      // OpenMP construct that matches the type specified in
4676
      // construct-type-clause.
4677
847
      NestingProhibited =
4678
847
          !((CancelRegion == OMPD_parallel &&
4679
302
             (ParentRegion == OMPD_parallel ||
4680
116
              ParentRegion == OMPD_target_parallel)) ||
4681
609
            (CancelRegion == OMPD_for &&
4682
221
             (ParentRegion == OMPD_for || 
ParentRegion == OMPD_parallel_for185
||
4683
165
              ParentRegion == OMPD_target_parallel_for ||
4684
85
              ParentRegion == OMPD_distribute_parallel_for ||
4685
65
              ParentRegion == OMPD_teams_distribute_parallel_for ||
4686
53
              ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
4687
416
            (CancelRegion == OMPD_taskgroup &&
4688
172
             (ParentRegion == OMPD_task ||
4689
136
              (SemaRef.getLangOpts().OpenMP >= 50 &&
4690
112
               (ParentRegion == OMPD_taskloop ||
4691
84
                ParentRegion == OMPD_master_taskloop ||
4692
56
                ParentRegion == OMPD_parallel_master_taskloop)))) ||
4693
296
            (CancelRegion == OMPD_sections &&
4694
152
             (ParentRegion == OMPD_section || 
ParentRegion == OMPD_sections116
||
4695
68
              ParentRegion == OMPD_parallel_sections)));
4696
847
      OrphanSeen = ParentRegion == OMPD_unknown;
4697
73.0k
    } else if (CurrentRegion == OMPD_master) {
4698
      // OpenMP [2.16, Nesting of Regions]
4699
      // A master region may not be closely nested inside a worksharing,
4700
      // atomic, or explicit task region.
4701
354
      NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4702
226
                          isOpenMPTaskingDirective(ParentRegion);
4703
72.7k
    } else if (CurrentRegion == OMPD_critical && 
CurrentName.getName()428
) {
4704
      // OpenMP [2.16, Nesting of Regions]
4705
      // A critical region may not be nested (closely or otherwise) inside a
4706
      // critical region with the same name. Note that this restriction is not
4707
      // sufficient to prevent deadlock.
4708
76
      SourceLocation PreviousCriticalLoc;
4709
76
      bool DeadLock = Stack->hasDirective(
4710
76
          [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
4711
76
                                              const DeclarationNameInfo &DNI,
4712
176
                                              SourceLocation Loc) {
4713
176
            if (K == OMPD_critical && 
DNI.getName() == CurrentName.getName()130
) {
4714
22
              PreviousCriticalLoc = Loc;
4715
22
              return true;
4716
22
            }
4717
154
            return false;
4718
154
          },
4719
76
          false /* skip top directive */);
4720
76
      if (DeadLock) {
4721
22
        SemaRef.Diag(StartLoc,
4722
22
                     diag::err_omp_prohibited_region_critical_same_name)
4723
22
            << CurrentName.getName();
4724
22
        if (PreviousCriticalLoc.isValid())
4725
22
          SemaRef.Diag(PreviousCriticalLoc,
4726
22
                       diag::note_omp_previous_critical_region);
4727
22
        return true;
4728
22
      }
4729
72.6k
    } else if (CurrentRegion == OMPD_barrier) {
4730
      // OpenMP [2.16, Nesting of Regions]
4731
      // A barrier region may not be closely nested inside a worksharing,
4732
      // explicit task, critical, ordered, atomic, or master region.
4733
302
      NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4734
182
                          isOpenMPTaskingDirective(ParentRegion) ||
4735
158
                          ParentRegion == OMPD_master ||
4736
146
                          ParentRegion == OMPD_parallel_master ||
4737
140
                          ParentRegion == OMPD_critical ||
4738
128
                          ParentRegion == OMPD_ordered;
4739
72.3k
    } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
4740
26.6k
               !isOpenMPParallelDirective(CurrentRegion) &&
4741
7.72k
               !isOpenMPTeamsDirective(CurrentRegion)) {
4742
      // OpenMP [2.16, Nesting of Regions]
4743
      // A worksharing region may not be closely nested inside a worksharing,
4744
      // explicit task, critical, ordered, atomic, or master region.
4745
7.72k
      NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
4746
7.23k
                          isOpenMPTaskingDirective(ParentRegion) ||
4747
7.13k
                          ParentRegion == OMPD_master ||
4748
7.08k
                          ParentRegion == OMPD_parallel_master ||
4749
7.05k
                          ParentRegion == OMPD_critical ||
4750
7.01k
                          ParentRegion == OMPD_ordered;
4751
7.72k
      Recommend = ShouldBeInParallelRegion;
4752
64.6k
    } else if (CurrentRegion == OMPD_ordered) {
4753
      // OpenMP [2.16, Nesting of Regions]
4754
      // An ordered region may not be closely nested inside a critical,
4755
      // atomic, or explicit task region.
4756
      // An ordered region must be closely nested inside a loop region (or
4757
      // parallel loop region) with an ordered clause.
4758
      // OpenMP [2.8.1,simd Construct, Restrictions]
4759
      // An ordered construct with the simd clause is the only OpenMP construct
4760
      // that can appear in the simd region.
4761
1.09k
      NestingProhibited = ParentRegion == OMPD_critical ||
4762
1.08k
                          isOpenMPTaskingDirective(ParentRegion) ||
4763
1.05k
                          !(isOpenMPSimdDirective(ParentRegion) ||
4764
762
                            Stack->isParentOrderedRegion());
4765
1.09k
      Recommend = ShouldBeInOrderedRegion;
4766
63.5k
    } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
4767
      // OpenMP [2.16, Nesting of Regions]
4768
      // If specified, a teams construct must be contained within a target
4769
      // construct.
4770
28.3k
      NestingProhibited =
4771
28.3k
          (SemaRef.LangOpts.OpenMP <= 45 && 
ParentRegion != OMPD_target11.2k
) ||
4772
27.4k
          (SemaRef.LangOpts.OpenMP >= 50 && 
ParentRegion != OMPD_unknown17.0k
&&
4773
16.9k
           ParentRegion != OMPD_target);
4774
28.3k
      OrphanSeen = ParentRegion == OMPD_unknown;
4775
28.3k
      Recommend = ShouldBeInTargetRegion;
4776
35.2k
    } else if (CurrentRegion == OMPD_scan) {
4777
      // OpenMP [2.16, Nesting of Regions]
4778
      // If specified, a teams construct must be contained within a target
4779
      // construct.
4780
488
      NestingProhibited =
4781
488
          SemaRef.LangOpts.OpenMP < 50 ||
4782
300
          (ParentRegion != OMPD_simd && 
ParentRegion != OMPD_for228
&&
4783
204
           ParentRegion != OMPD_for_simd && 
ParentRegion != OMPD_parallel_for186
&&
4784
144
           ParentRegion != OMPD_parallel_for_simd);
4785
488
      OrphanSeen = ParentRegion == OMPD_unknown;
4786
488
      Recommend = ShouldBeInLoopSimdRegion;
4787
488
    }
4788
73.9k
    if (!NestingProhibited &&
4789
70.6k
        !isOpenMPTargetExecutionDirective(CurrentRegion) &&
4790
65.6k
        !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
4791
63.8k
        (ParentRegion == OMPD_teams || 
ParentRegion == OMPD_target_teams52.0k
)) {
4792
      // OpenMP [2.16, Nesting of Regions]
4793
      // distribute, parallel, parallel sections, parallel workshare, and the
4794
      // parallel loop and parallel loop SIMD constructs are the only OpenMP
4795
      // constructs that can be closely nested in the teams region.
4796
12.4k
      NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
4797
4.56k
                          !isOpenMPDistributeDirective(CurrentRegion);
4798
12.4k
      Recommend = ShouldBeInParallelRegion;
4799
12.4k
    }
4800
73.9k
    if (!NestingProhibited &&
4801
70.3k
        isOpenMPNestingDistributeDirective(CurrentRegion)) {
4802
      // OpenMP 4.5 [2.17 Nesting of Regions]
4803
      // The region associated with the distribute construct must be strictly
4804
      // nested inside a teams region
4805
12.5k
      NestingProhibited =
4806
12.5k
          (ParentRegion != OMPD_teams && 
ParentRegion != OMPD_target_teams1.21k
);
4807
12.5k
      Recommend = ShouldBeInTeamsRegion;
4808
12.5k
    }
4809
73.9k
    if (!NestingProhibited &&
4810
69.4k
        (isOpenMPTargetExecutionDirective(CurrentRegion) ||
4811
64.3k
         isOpenMPTargetDataManagementDirective(CurrentRegion))) {
4812
      // OpenMP 4.5 [2.17 Nesting of Regions]
4813
      // If a target, target update, target data, target enter data, or
4814
      // target exit data construct is encountered during execution of a
4815
      // target region, the behavior is unspecified.
4816
6.87k
      NestingProhibited = Stack->hasDirective(
4817
6.87k
          [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
4818
12.5k
                             SourceLocation) {
4819
12.5k
            if (isOpenMPTargetExecutionDirective(K)) {
4820
1.64k
              OffendingRegion = K;
4821
1.64k
              return true;
4822
1.64k
            }
4823
10.9k
            return false;
4824
10.9k
          },
4825
6.87k
          false /* don't skip top directive */);
4826
6.87k
      CloseNesting = false;
4827
6.87k
    }
4828
73.9k
    if (NestingProhibited) {
4829
6.14k
      if (OrphanSeen) {
4830
142
        SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
4831
142
            << getOpenMPDirectiveName(CurrentRegion) << Recommend;
4832
6.00k
      } else {
4833
6.00k
        SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
4834
6.00k
            << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
4835
6.00k
            << Recommend << getOpenMPDirectiveName(CurrentRegion);
4836
6.00k
      }
4837
6.14k
      return true;
4838
6.14k
    }
4839
156k
  }
4840
156k
  return false;
4841
156k
}
4842
4843
struct Kind2Unsigned {
4844
  using argument_type = OpenMPDirectiveKind;
4845
530k
  unsigned operator()(argument_type DK) { return unsigned(DK); }
4846
};
4847
static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
4848
                           ArrayRef<OMPClause *> Clauses,
4849
240k
                           ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
4850
240k
  bool ErrorFound = false;
4851
240k
  unsigned NamedModifiersNumber = 0;
4852
240k
  llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
4853
240k
  FoundNameModifiers.resize(llvm::omp::Directive_enumSize + 1);
4854
240k
  SmallVector<SourceLocation, 4> NameModifierLoc;
4855
126k
  for (const OMPClause *C : Clauses) {
4856
126k
    if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
4857
      // At most one if clause without a directive-name-modifier can appear on
4858
      // the directive.
4859
12.2k
      OpenMPDirectiveKind CurNM = IC->getNameModifier();
4860
12.2k
      if (FoundNameModifiers[CurNM]) {
4861
428
        S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
4862
428
            << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
4863
428
            << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
4864
428
        ErrorFound = true;
4865
11.7k
      } else if (CurNM != OMPD_unknown) {
4866
6.10k
        NameModifierLoc.push_back(IC->getNameModifierLoc());
4867
6.10k
        ++NamedModifiersNumber;
4868
6.10k
      }
4869
12.2k
      FoundNameModifiers[CurNM] = IC;
4870
12.2k
      if (CurNM == OMPD_unknown)
4871
5.86k
        continue;
4872
      // Check if the specified name modifier is allowed for the current
4873
      // directive.
4874
      // At most one if clause with the particular directive-name-modifier can
4875
      // appear on the directive.
4876
6.33k
      bool MatchFound = false;
4877
7.38k
      for (auto NM : AllowedNameModifiers) {
4878
7.38k
        if (CurNM == NM) {
4879
6.05k
          MatchFound = true;
4880
6.05k
          break;
4881
6.05k
        }
4882
7.38k
      }
4883
6.33k
      if (!MatchFound) {
4884
282
        S.Diag(IC->getNameModifierLoc(),
4885
282
               diag::err_omp_wrong_if_directive_name_modifier)
4886
282
            << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
4887
282
        ErrorFound = true;
4888
282
      }
4889
6.33k
    }
4890
126k
  }
4891
  // If any if clause on the directive includes a directive-name-modifier then
4892
  // all if clauses on the directive must include a directive-name-modifier.
4893
240k
  if (FoundNameModifiers[OMPD_unknown] && 
NamedModifiersNumber > 05.67k
) {
4894
254
    if (NamedModifiersNumber == AllowedNameModifiers.size()) {
4895
178
      S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
4896
178
             diag::err_omp_no_more_if_clause);
4897
76
    } else {
4898
76
      std::string Values;
4899
76
      std::string Sep(", ");
4900
76
      unsigned AllowedCnt = 0;
4901
76
      unsigned TotalAllowedNum =
4902
76
          AllowedNameModifiers.size() - NamedModifiersNumber;
4903
240
      for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
4904
164
           ++Cnt) {
4905
164
        OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
4906
164
        if (!FoundNameModifiers[NM]) {
4907
84
          Values += "'";
4908
84
          Values += getOpenMPDirectiveName(NM);
4909
84
          Values += "'";
4910
84
          if (AllowedCnt + 2 == TotalAllowedNum)
4911
12
            Values += " or ";
4912
72
          else if (AllowedCnt + 1 != TotalAllowedNum)
4913
0
            Values += Sep;
4914
84
          ++AllowedCnt;
4915
84
        }
4916
164
      }
4917
76
      S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
4918
76
             diag::err_omp_unnamed_if_clause)
4919
76
          << (TotalAllowedNum > 1) << Values;
4920
76
    }
4921
282
    for (SourceLocation Loc : NameModifierLoc) {
4922
282
      S.Diag(Loc, diag::note_omp_previous_named_if_clause);
4923
282
    }
4924
254
    ErrorFound = true;
4925
254
  }
4926
240k
  return ErrorFound;
4927
240k
}
4928
4929
static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *&RefExpr,
4930
                                                   SourceLocation &ELoc,
4931
                                                   SourceRange &ERange,
4932
282k
                                                   bool AllowArraySection) {
4933
282k
  if (RefExpr->isTypeDependent() || 
RefExpr->isValueDependent()262k
||
4934
262k
      RefExpr->containsUnexpandedParameterPack())
4935
20.5k
    return std::make_pair(nullptr, true);
4936
4937
  // OpenMP [3.1, C/C++]
4938
  //  A list item is a variable name.
4939
  // OpenMP  [2.9.3.3, Restrictions, p.1]
4940
  //  A variable that is part of another variable (as an array or
4941
  //  structure element) cannot appear in a private clause.
4942
262k
  RefExpr = RefExpr->IgnoreParens();
4943
262k
  enum {
4944
262k
    NoArrayExpr = -1,
4945
262k
    ArraySubscript = 0,
4946
262k
    OMPArraySection = 1
4947
262k
  } IsArrayExpr = NoArrayExpr;
4948
262k
  if (AllowArraySection) {
4949
153k
    if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
4950
464
      Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
4951
464
      while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4952
0
        Base = TempASE->getBase()->IgnoreParenImpCasts();
4953
464
      RefExpr = Base;
4954
464
      IsArrayExpr = ArraySubscript;
4955
153k
    } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
4956
1.11k
      Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
4957
1.64k
      while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
4958
528
        Base = TempOASE->getBase()->IgnoreParenImpCasts();
4959
1.15k
      while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
4960
40
        Base = TempASE->getBase()->IgnoreParenImpCasts();
4961
1.11k
      RefExpr = Base;
4962
1.11k
      IsArrayExpr = OMPArraySection;
4963
1.11k
    }
4964
153k
  }
4965
262k
  ELoc = RefExpr->getExprLoc();
4966
262k
  ERange = RefExpr->getSourceRange();
4967
262k
  RefExpr = RefExpr->IgnoreParenImpCasts();
4968
262k
  auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4969
262k
  auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
4970
262k
  if ((!DE || 
!isa<VarDecl>(DE->getDecl())254k
) &&
4971
7.65k
      (S.getCurrentThisType().isNull() || 
!ME3.71k
||
4972
3.71k
       !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
4973
4.12k
       
!isa<FieldDecl>(ME->getMemberDecl())3.53k
)) {