Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Sema/Sema.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the actions class which performs semantic analysis and
10
// builds an AST out of a parse stream.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "UsedDeclVisitor.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/ASTDiagnostic.h"
17
#include "clang/AST/Decl.h"
18
#include "clang/AST/DeclCXX.h"
19
#include "clang/AST/DeclFriend.h"
20
#include "clang/AST/DeclObjC.h"
21
#include "clang/AST/Expr.h"
22
#include "clang/AST/ExprCXX.h"
23
#include "clang/AST/PrettyDeclStackTrace.h"
24
#include "clang/AST/StmtCXX.h"
25
#include "clang/Basic/DarwinSDKInfo.h"
26
#include "clang/Basic/DiagnosticOptions.h"
27
#include "clang/Basic/PartialDiagnostic.h"
28
#include "clang/Basic/SourceManager.h"
29
#include "clang/Basic/Stack.h"
30
#include "clang/Basic/TargetInfo.h"
31
#include "clang/Lex/HeaderSearch.h"
32
#include "clang/Lex/HeaderSearchOptions.h"
33
#include "clang/Lex/Preprocessor.h"
34
#include "clang/Sema/CXXFieldCollector.h"
35
#include "clang/Sema/DelayedDiagnostic.h"
36
#include "clang/Sema/EnterExpressionEvaluationContext.h"
37
#include "clang/Sema/ExternalSemaSource.h"
38
#include "clang/Sema/Initialization.h"
39
#include "clang/Sema/MultiplexExternalSemaSource.h"
40
#include "clang/Sema/ObjCMethodList.h"
41
#include "clang/Sema/RISCVIntrinsicManager.h"
42
#include "clang/Sema/Scope.h"
43
#include "clang/Sema/ScopeInfo.h"
44
#include "clang/Sema/SemaConsumer.h"
45
#include "clang/Sema/SemaInternal.h"
46
#include "clang/Sema/TemplateDeduction.h"
47
#include "clang/Sema/TemplateInstCallback.h"
48
#include "clang/Sema/TypoCorrection.h"
49
#include "llvm/ADT/DenseMap.h"
50
#include "llvm/ADT/STLExtras.h"
51
#include "llvm/ADT/SmallPtrSet.h"
52
#include "llvm/Support/TimeProfiler.h"
53
#include <optional>
54
55
using namespace clang;
56
using namespace sema;
57
58
1
SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) {
59
1
  return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
60
1
}
61
62
0
ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); }
63
64
DarwinSDKInfo *
65
Sema::getDarwinSDKInfoForAvailabilityChecking(SourceLocation Loc,
66
0
                                              StringRef Platform) {
67
0
  auto *SDKInfo = getDarwinSDKInfoForAvailabilityChecking();
68
0
  if (!SDKInfo && !WarnedDarwinSDKInfoMissing) {
69
0
    Diag(Loc, diag::warn_missing_sdksettings_for_availability_checking)
70
0
        << Platform;
71
0
    WarnedDarwinSDKInfoMissing = true;
72
0
  }
73
0
  return SDKInfo;
74
0
}
75
76
0
DarwinSDKInfo *Sema::getDarwinSDKInfoForAvailabilityChecking() {
77
0
  if (CachedDarwinSDKInfo)
78
0
    return CachedDarwinSDKInfo->get();
79
0
  auto SDKInfo = parseDarwinSDKInfo(
80
0
      PP.getFileManager().getVirtualFileSystem(),
81
0
      PP.getHeaderSearchInfo().getHeaderSearchOpts().Sysroot);
82
0
  if (SDKInfo && *SDKInfo) {
83
0
    CachedDarwinSDKInfo = std::make_unique<DarwinSDKInfo>(std::move(**SDKInfo));
84
0
    return CachedDarwinSDKInfo->get();
85
0
  }
86
0
  if (!SDKInfo)
87
0
    llvm::consumeError(SDKInfo.takeError());
88
0
  CachedDarwinSDKInfo = std::unique_ptr<DarwinSDKInfo>();
89
0
  return nullptr;
90
0
}
91
92
IdentifierInfo *
93
Sema::InventAbbreviatedTemplateParameterTypeName(IdentifierInfo *ParamName,
94
0
                                                 unsigned int Index) {
95
0
  std::string InventedName;
96
0
  llvm::raw_string_ostream OS(InventedName);
97
98
0
  if (!ParamName)
99
0
    OS << "auto:" << Index + 1;
100
0
  else
101
0
    OS << ParamName->getName() << ":auto";
102
103
0
  OS.flush();
104
0
  return &Context.Idents.get(OS.str());
105
0
}
106
107
PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context,
108
36.4k
                                       const Preprocessor &PP) {
109
36.4k
  PrintingPolicy Policy = Context.getPrintingPolicy();
110
  // In diagnostics, we print _Bool as bool if the latter is defined as the
111
  // former.
112
36.4k
  Policy.Bool = Context.getLangOpts().Bool;
113
36.4k
  if (!Policy.Bool) {
114
18.3k
    if (const MacroInfo *BoolMacro = PP.getMacroInfo(Context.getBoolName())) {
115
0
      Policy.Bool = BoolMacro->isObjectLike() &&
116
0
                    BoolMacro->getNumTokens() == 1 &&
117
0
                    BoolMacro->getReplacementToken(0).is(tok::kw__Bool);
118
0
    }
119
18.3k
  }
120
121
  // Shorten the data output if needed
122
36.4k
  Policy.EntireContentsOfLargeArray = false;
123
124
36.4k
  return Policy;
125
36.4k
}
126
127
46
void Sema::ActOnTranslationUnitScope(Scope *S) {
128
46
  TUScope = S;
129
46
  PushDeclContext(S, Context.getTranslationUnitDecl());
130
46
}
131
132
namespace clang {
133
namespace sema {
134
135
class SemaPPCallbacks : public PPCallbacks {
136
  Sema *S = nullptr;
137
  llvm::SmallVector<SourceLocation, 8> IncludeStack;
138
139
public:
140
46
  void set(Sema &S) { this->S = &S; }
141
142
46
  void reset() { S = nullptr; }
143
144
  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
145
                   SrcMgr::CharacteristicKind FileType,
146
278
                   FileID PrevFID) override {
147
278
    if (!S)
148
0
      return;
149
278
    switch (Reason) {
150
138
    case EnterFile: {
151
138
      SourceManager &SM = S->getSourceManager();
152
138
      SourceLocation IncludeLoc = SM.getIncludeLoc(SM.getFileID(Loc));
153
138
      if (IncludeLoc.isValid()) {
154
0
        if (llvm::timeTraceProfilerEnabled()) {
155
0
          OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getFileID(Loc));
156
0
          llvm::timeTraceProfilerBegin("Source", FE ? FE->getName()
157
0
                                                    : StringRef("<unknown>"));
158
0
        }
159
160
0
        IncludeStack.push_back(IncludeLoc);
161
0
        S->DiagnoseNonDefaultPragmaAlignPack(
162
0
            Sema::PragmaAlignPackDiagnoseKind::NonDefaultStateAtInclude,
163
0
            IncludeLoc);
164
0
      }
165
138
      break;
166
0
    }
167
92
    case ExitFile:
168
92
      if (!IncludeStack.empty()) {
169
0
        if (llvm::timeTraceProfilerEnabled())
170
0
          llvm::timeTraceProfilerEnd();
171
172
0
        S->DiagnoseNonDefaultPragmaAlignPack(
173
0
            Sema::PragmaAlignPackDiagnoseKind::ChangedStateAtExit,
174
0
            IncludeStack.pop_back_val());
175
0
      }
176
92
      break;
177
48
    default:
178
48
      break;
179
278
    }
180
278
  }
181
};
182
183
} // end namespace sema
184
} // end namespace clang
185
186
const unsigned Sema::MaxAlignmentExponent;
187
const uint64_t Sema::MaximumAlignment;
188
189
Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
190
           TranslationUnitKind TUKind, CodeCompleteConsumer *CodeCompleter)
191
    : ExternalSource(nullptr), CurFPFeatures(pp.getLangOpts()),
192
      LangOpts(pp.getLangOpts()), PP(pp), Context(ctxt), Consumer(consumer),
193
      Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()),
194
      APINotes(SourceMgr, LangOpts), CollectStats(false),
195
      CodeCompleter(CodeCompleter), CurContext(nullptr),
196
      OriginalLexicalContext(nullptr), MSStructPragmaOn(false),
197
      MSPointerToMemberRepresentationMethod(
198
          LangOpts.getMSPointerToMemberRepresentationMethod()),
199
      VtorDispStack(LangOpts.getVtorDispMode()),
200
      AlignPackStack(AlignPackInfo(getLangOpts().XLPragmaPack)),
201
      DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr),
202
      CodeSegStack(nullptr), StrictGuardStackCheckStack(false),
203
      FpPragmaStack(FPOptionsOverride()), CurInitSeg(nullptr),
204
      VisContext(nullptr), PragmaAttributeCurrentTargetDecl(nullptr),
205
      IsBuildingRecoveryCallExpr(false), LateTemplateParser(nullptr),
206
      LateTemplateParserCleanup(nullptr), OpaqueParser(nullptr), IdResolver(pp),
207
      StdInitializerList(nullptr), StdCoroutineTraitsCache(nullptr),
208
      CXXTypeInfoDecl(nullptr), StdSourceLocationImplDecl(nullptr),
209
      NSNumberDecl(nullptr), NSValueDecl(nullptr), NSStringDecl(nullptr),
210
      StringWithUTF8StringMethod(nullptr),
211
      ValueWithBytesObjCTypeMethod(nullptr), NSArrayDecl(nullptr),
212
      ArrayWithObjectsMethod(nullptr), NSDictionaryDecl(nullptr),
213
      DictionaryWithObjectsMethod(nullptr), GlobalNewDeleteDeclared(false),
214
      TUKind(TUKind), NumSFINAEErrors(0),
215
      FullyCheckedComparisonCategories(
216
          static_cast<unsigned>(ComparisonCategoryType::Last) + 1),
217
      SatisfactionCache(Context), AccessCheckingSFINAE(false),
218
      InNonInstantiationSFINAEContext(false), NonInstantiationEntries(0),
219
      ArgumentPackSubstitutionIndex(-1), CurrentInstantiationScope(nullptr),
220
      DisableTypoCorrection(false), TyposCorrected(0), AnalysisWarnings(*this),
221
      ThreadSafetyDeclCache(nullptr), VarDataSharingAttributesStack(nullptr),
222
46
      CurScope(nullptr), Ident_super(nullptr) {
223
46
  assert(pp.TUKind == TUKind);
224
0
  TUScope = nullptr;
225
226
46
  LoadedExternalKnownNamespaces = false;
227
736
  for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I)
228
690
    NSNumberLiteralMethods[I] = nullptr;
229
230
46
  if (getLangOpts().ObjC)
231
23
    NSAPIObj.reset(new NSAPI(Context));
232
233
46
  if (getLangOpts().CPlusPlus)
234
23
    FieldCollector.reset(new CXXFieldCollector());
235
236
  // Tell diagnostics how to render things from the AST library.
237
46
  Diags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument, &Context);
238
239
  // This evaluation context exists to ensure that there's always at least one
240
  // valid evaluation context available. It is never removed from the
241
  // evaluation stack.
242
46
  ExprEvalContexts.emplace_back(
243
46
      ExpressionEvaluationContext::PotentiallyEvaluated, 0, CleanupInfo{},
244
46
      nullptr, ExpressionEvaluationContextRecord::EK_Other);
245
246
  // Initialization of data sharing attributes stack for OpenMP
247
46
  InitDataSharingAttributesStack();
248
249
46
  std::unique_ptr<sema::SemaPPCallbacks> Callbacks =
250
46
      std::make_unique<sema::SemaPPCallbacks>();
251
46
  SemaPPCallbackHandler = Callbacks.get();
252
46
  PP.addPPCallbacks(std::move(Callbacks));
253
46
  SemaPPCallbackHandler->set(*this);
254
255
46
  CurFPFeatures.setFPEvalMethod(PP.getCurrentFPEvalMethod());
256
46
}
257
258
// Anchor Sema's type info to this TU.
259
0
void Sema::anchor() {}
260
261
0
void Sema::addImplicitTypedef(StringRef Name, QualType T) {
262
0
  DeclarationName DN = &Context.Idents.get(Name);
263
0
  if (IdResolver.begin(DN) == IdResolver.end())
264
0
    PushOnScopeChains(Context.buildImplicitTypedef(T, Name), TUScope);
265
0
}
266
267
46
void Sema::Initialize() {
268
46
  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
269
0
    SC->InitializeSema(*this);
270
271
  // Tell the external Sema source about this Sema object.
272
46
  if (ExternalSemaSource *ExternalSema
273
46
      = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
274
0
    ExternalSema->InitializeSema(*this);
275
276
  // This needs to happen after ExternalSemaSource::InitializeSema(this) or we
277
  // will not be able to merge any duplicate __va_list_tag decls correctly.
278
46
  VAListTagName = PP.getIdentifierInfo("__va_list_tag");
279
280
46
  if (!TUScope)
281
0
    return;
282
283
  // Initialize predefined 128-bit integer types, if needed.
284
46
  if (Context.getTargetInfo().hasInt128Type() ||
285
46
      (Context.getAuxTargetInfo() &&
286
46
       Context.getAuxTargetInfo()->hasInt128Type())) {
287
    // If either of the 128-bit integer types are unavailable to name lookup,
288
    // define them now.
289
46
    DeclarationName Int128 = &Context.Idents.get("__int128_t");
290
46
    if (IdResolver.begin(Int128) == IdResolver.end())
291
46
      PushOnScopeChains(Context.getInt128Decl(), TUScope);
292
293
46
    DeclarationName UInt128 = &Context.Idents.get("__uint128_t");
294
46
    if (IdResolver.begin(UInt128) == IdResolver.end())
295
46
      PushOnScopeChains(Context.getUInt128Decl(), TUScope);
296
46
  }
297
298
299
  // Initialize predefined Objective-C types:
300
46
  if (getLangOpts().ObjC) {
301
    // If 'SEL' does not yet refer to any declarations, make it refer to the
302
    // predefined 'SEL'.
303
23
    DeclarationName SEL = &Context.Idents.get("SEL");
304
23
    if (IdResolver.begin(SEL) == IdResolver.end())
305
23
      PushOnScopeChains(Context.getObjCSelDecl(), TUScope);
306
307
    // If 'id' does not yet refer to any declarations, make it refer to the
308
    // predefined 'id'.
309
23
    DeclarationName Id = &Context.Idents.get("id");
310
23
    if (IdResolver.begin(Id) == IdResolver.end())
311
23
      PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
312
313
    // Create the built-in typedef for 'Class'.
314
23
    DeclarationName Class = &Context.Idents.get("Class");
315
23
    if (IdResolver.begin(Class) == IdResolver.end())
316
23
      PushOnScopeChains(Context.getObjCClassDecl(), TUScope);
317
318
    // Create the built-in forward declaratino for 'Protocol'.
319
23
    DeclarationName Protocol = &Context.Idents.get("Protocol");
320
23
    if (IdResolver.begin(Protocol) == IdResolver.end())
321
23
      PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope);
322
23
  }
323
324
  // Create the internal type for the *StringMakeConstantString builtins.
325
46
  DeclarationName ConstantString = &Context.Idents.get("__NSConstantString");
326
46
  if (IdResolver.begin(ConstantString) == IdResolver.end())
327
46
    PushOnScopeChains(Context.getCFConstantStringDecl(), TUScope);
328
329
  // Initialize Microsoft "predefined C++ types".
330
46
  if (getLangOpts().MSVCCompat) {
331
0
    if (getLangOpts().CPlusPlus &&
332
0
        IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end())
333
0
      PushOnScopeChains(
334
0
          Context.buildImplicitRecord("type_info", TagTypeKind::Class),
335
0
          TUScope);
336
337
0
    addImplicitTypedef("size_t", Context.getSizeType());
338
0
  }
339
340
  // Initialize predefined OpenCL types and supported extensions and (optional)
341
  // core features.
342
46
  if (getLangOpts().OpenCL) {
343
0
    getOpenCLOptions().addSupport(
344
0
        Context.getTargetInfo().getSupportedOpenCLOpts(), getLangOpts());
345
0
    addImplicitTypedef("sampler_t", Context.OCLSamplerTy);
346
0
    addImplicitTypedef("event_t", Context.OCLEventTy);
347
0
    auto OCLCompatibleVersion = getLangOpts().getOpenCLCompatibleVersion();
348
0
    if (OCLCompatibleVersion >= 200) {
349
0
      if (getLangOpts().OpenCLCPlusPlus || getLangOpts().Blocks) {
350
0
        addImplicitTypedef("clk_event_t", Context.OCLClkEventTy);
351
0
        addImplicitTypedef("queue_t", Context.OCLQueueTy);
352
0
      }
353
0
      if (getLangOpts().OpenCLPipes)
354
0
        addImplicitTypedef("reserve_id_t", Context.OCLReserveIDTy);
355
0
      addImplicitTypedef("atomic_int", Context.getAtomicType(Context.IntTy));
356
0
      addImplicitTypedef("atomic_uint",
357
0
                         Context.getAtomicType(Context.UnsignedIntTy));
358
0
      addImplicitTypedef("atomic_float",
359
0
                         Context.getAtomicType(Context.FloatTy));
360
      // OpenCLC v2.0, s6.13.11.6 requires that atomic_flag is implemented as
361
      // 32-bit integer and OpenCLC v2.0, s6.1.1 int is always 32-bit wide.
362
0
      addImplicitTypedef("atomic_flag", Context.getAtomicType(Context.IntTy));
363
364
365
      // OpenCL v2.0 s6.13.11.6:
366
      // - The atomic_long and atomic_ulong types are supported if the
367
      //   cl_khr_int64_base_atomics and cl_khr_int64_extended_atomics
368
      //   extensions are supported.
369
      // - The atomic_double type is only supported if double precision
370
      //   is supported and the cl_khr_int64_base_atomics and
371
      //   cl_khr_int64_extended_atomics extensions are supported.
372
      // - If the device address space is 64-bits, the data types
373
      //   atomic_intptr_t, atomic_uintptr_t, atomic_size_t and
374
      //   atomic_ptrdiff_t are supported if the cl_khr_int64_base_atomics and
375
      //   cl_khr_int64_extended_atomics extensions are supported.
376
377
0
      auto AddPointerSizeDependentTypes = [&]() {
378
0
        auto AtomicSizeT = Context.getAtomicType(Context.getSizeType());
379
0
        auto AtomicIntPtrT = Context.getAtomicType(Context.getIntPtrType());
380
0
        auto AtomicUIntPtrT = Context.getAtomicType(Context.getUIntPtrType());
381
0
        auto AtomicPtrDiffT =
382
0
            Context.getAtomicType(Context.getPointerDiffType());
383
0
        addImplicitTypedef("atomic_size_t", AtomicSizeT);
384
0
        addImplicitTypedef("atomic_intptr_t", AtomicIntPtrT);
385
0
        addImplicitTypedef("atomic_uintptr_t", AtomicUIntPtrT);
386
0
        addImplicitTypedef("atomic_ptrdiff_t", AtomicPtrDiffT);
387
0
      };
388
389
0
      if (Context.getTypeSize(Context.getSizeType()) == 32) {
390
0
        AddPointerSizeDependentTypes();
391
0
      }
392
393
0
      if (getOpenCLOptions().isSupported("cl_khr_fp16", getLangOpts())) {
394
0
        auto AtomicHalfT = Context.getAtomicType(Context.HalfTy);
395
0
        addImplicitTypedef("atomic_half", AtomicHalfT);
396
0
      }
397
398
0
      std::vector<QualType> Atomic64BitTypes;
399
0
      if (getOpenCLOptions().isSupported("cl_khr_int64_base_atomics",
400
0
                                         getLangOpts()) &&
401
0
          getOpenCLOptions().isSupported("cl_khr_int64_extended_atomics",
402
0
                                         getLangOpts())) {
403
0
        if (getOpenCLOptions().isSupported("cl_khr_fp64", getLangOpts())) {
404
0
          auto AtomicDoubleT = Context.getAtomicType(Context.DoubleTy);
405
0
          addImplicitTypedef("atomic_double", AtomicDoubleT);
406
0
          Atomic64BitTypes.push_back(AtomicDoubleT);
407
0
        }
408
0
        auto AtomicLongT = Context.getAtomicType(Context.LongTy);
409
0
        auto AtomicULongT = Context.getAtomicType(Context.UnsignedLongTy);
410
0
        addImplicitTypedef("atomic_long", AtomicLongT);
411
0
        addImplicitTypedef("atomic_ulong", AtomicULongT);
412
413
414
0
        if (Context.getTypeSize(Context.getSizeType()) == 64) {
415
0
          AddPointerSizeDependentTypes();
416
0
        }
417
0
      }
418
0
    }
419
420
0
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext)                                      \
421
0
  if (getOpenCLOptions().isSupported(#Ext, getLangOpts())) {                   \
422
0
    addImplicitTypedef(#ExtType, Context.Id##Ty);                              \
423
0
  }
424
0
#include "clang/Basic/OpenCLExtensionTypes.def"
425
0
  }
426
427
46
  if (Context.getTargetInfo().hasAArch64SVETypes()) {
428
0
#define SVE_TYPE(Name, Id, SingletonId) \
429
0
    addImplicitTypedef(Name, Context.SingletonId);
430
0
#include "clang/Basic/AArch64SVEACLETypes.def"
431
0
  }
432
433
46
  if (Context.getTargetInfo().getTriple().isPPC64()) {
434
0
#define PPC_VECTOR_MMA_TYPE(Name, Id, Size) \
435
0
      addImplicitTypedef(#Name, Context.Id##Ty);
436
0
#include "clang/Basic/PPCTypes.def"
437
0
#define PPC_VECTOR_VSX_TYPE(Name, Id, Size) \
438
0
    addImplicitTypedef(#Name, Context.Id##Ty);
439
0
#include "clang/Basic/PPCTypes.def"
440
0
  }
441
442
46
  if (Context.getTargetInfo().hasRISCVVTypes()) {
443
0
#define RVV_TYPE(Name, Id, SingletonId)                                        \
444
0
  addImplicitTypedef(Name, Context.SingletonId);
445
0
#include "clang/Basic/RISCVVTypes.def"
446
0
  }
447
448
46
  if (Context.getTargetInfo().getTriple().isWasm() &&
449
46
      Context.getTargetInfo().hasFeature("reference-types")) {
450
0
#define WASM_TYPE(Name, Id, SingletonId)                                       \
451
0
  addImplicitTypedef(Name, Context.SingletonId);
452
0
#include "clang/Basic/WebAssemblyReferenceTypes.def"
453
0
  }
454
455
46
  if (Context.getTargetInfo().hasBuiltinMSVaList()) {
456
46
    DeclarationName MSVaList = &Context.Idents.get("__builtin_ms_va_list");
457
46
    if (IdResolver.begin(MSVaList) == IdResolver.end())
458
46
      PushOnScopeChains(Context.getBuiltinMSVaListDecl(), TUScope);
459
46
  }
460
461
46
  DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list");
462
46
  if (IdResolver.begin(BuiltinVaList) == IdResolver.end())
463
46
    PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope);
464
46
}
465
466
46
Sema::~Sema() {
467
46
  assert(InstantiatingSpecializations.empty() &&
468
46
         "failed to clean up an InstantiatingTemplate?");
469
470
46
  if (VisContext) FreeVisContext();
471
472
  // Kill all the active scopes.
473
46
  for (sema::FunctionScopeInfo *FSI : FunctionScopes)
474
0
    delete FSI;
475
476
  // Tell the SemaConsumer to forget about us; we're going out of scope.
477
46
  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer))
478
0
    SC->ForgetSema();
479
480
  // Detach from the external Sema source.
481
46
  if (ExternalSemaSource *ExternalSema
482
46
        = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
483
0
    ExternalSema->ForgetSema();
484
485
  // Delete cached satisfactions.
486
46
  std::vector<ConstraintSatisfaction *> Satisfactions;
487
46
  Satisfactions.reserve(SatisfactionCache.size());
488
46
  for (auto &Node : SatisfactionCache)
489
0
    Satisfactions.push_back(&Node);
490
46
  for (auto *Node : Satisfactions)
491
0
    delete Node;
492
493
46
  threadSafety::threadSafetyCleanup(ThreadSafetyDeclCache);
494
495
  // Destroys data sharing attributes stack for OpenMP
496
46
  DestroyDataSharingAttributesStack();
497
498
  // Detach from the PP callback handler which outlives Sema since it's owned
499
  // by the preprocessor.
500
46
  SemaPPCallbackHandler->reset();
501
46
}
502
503
0
void Sema::warnStackExhausted(SourceLocation Loc) {
504
  // Only warn about this once.
505
0
  if (!WarnedStackExhausted) {
506
0
    Diag(Loc, diag::warn_stack_exhausted);
507
0
    WarnedStackExhausted = true;
508
0
  }
509
0
}
510
511
void Sema::runWithSufficientStackSpace(SourceLocation Loc,
512
24.7k
                                       llvm::function_ref<void()> Fn) {
513
24.7k
  clang::runWithSufficientStackSpace([&] { warnStackExhausted(Loc); }, Fn);
514
24.7k
}
515
516
/// makeUnavailableInSystemHeader - There is an error in the current
517
/// context.  If we're still in a system header, and we can plausibly
518
/// make the relevant declaration unavailable instead of erroring, do
519
/// so and return true.
520
bool Sema::makeUnavailableInSystemHeader(SourceLocation loc,
521
0
                                      UnavailableAttr::ImplicitReason reason) {
522
  // If we're not in a function, it's an error.
523
0
  FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext);
524
0
  if (!fn) return false;
525
526
  // If we're in template instantiation, it's an error.
527
0
  if (inTemplateInstantiation())
528
0
    return false;
529
530
  // If that function's not in a system header, it's an error.
531
0
  if (!Context.getSourceManager().isInSystemHeader(loc))
532
0
    return false;
533
534
  // If the function is already unavailable, it's not an error.
535
0
  if (fn->hasAttr<UnavailableAttr>()) return true;
536
537
0
  fn->addAttr(UnavailableAttr::CreateImplicit(Context, "", reason, loc));
538
0
  return true;
539
0
}
540
541
0
ASTMutationListener *Sema::getASTMutationListener() const {
542
0
  return getASTConsumer().GetASTMutationListener();
543
0
}
544
545
///Registers an external source. If an external source already exists,
546
/// creates a multiplex external source and appends to it.
547
///
548
///\param[in] E - A non-null external sema source.
549
///
550
0
void Sema::addExternalSource(ExternalSemaSource *E) {
551
0
  assert(E && "Cannot use with NULL ptr");
552
553
0
  if (!ExternalSource) {
554
0
    ExternalSource = E;
555
0
    return;
556
0
  }
557
558
0
  if (auto *Ex = dyn_cast<MultiplexExternalSemaSource>(ExternalSource))
559
0
    Ex->AddSource(E);
560
0
  else
561
0
    ExternalSource = new MultiplexExternalSemaSource(ExternalSource.get(), E);
562
0
}
563
564
/// Print out statistics about the semantic analysis.
565
0
void Sema::PrintStats() const {
566
0
  llvm::errs() << "\n*** Semantic Analysis Stats:\n";
567
0
  llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n";
568
569
0
  BumpAlloc.PrintStats();
570
0
  AnalysisWarnings.PrintStats();
571
0
}
572
573
void Sema::diagnoseNullableToNonnullConversion(QualType DstType,
574
                                               QualType SrcType,
575
1
                                               SourceLocation Loc) {
576
1
  std::optional<NullabilityKind> ExprNullability = SrcType->getNullability();
577
1
  if (!ExprNullability || (*ExprNullability != NullabilityKind::Nullable &&
578
0
                           *ExprNullability != NullabilityKind::NullableResult))
579
1
    return;
580
581
0
  std::optional<NullabilityKind> TypeNullability = DstType->getNullability();
582
0
  if (!TypeNullability || *TypeNullability != NullabilityKind::NonNull)
583
0
    return;
584
585
0
  Diag(Loc, diag::warn_nullability_lost) << SrcType << DstType;
586
0
}
587
588
1
void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) {
589
  // nullptr only exists from C++11 on, so don't warn on its absence earlier.
590
1
  if (!getLangOpts().CPlusPlus11)
591
1
    return;
592
593
0
  if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer)
594
0
    return;
595
596
0
  const Expr *EStripped = E->IgnoreParenImpCasts();
597
0
  if (EStripped->getType()->isNullPtrType())
598
0
    return;
599
0
  if (isa<GNUNullExpr>(EStripped))
600
0
    return;
601
602
0
  if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant,
603
0
                      E->getBeginLoc()))
604
0
    return;
605
606
  // Don't diagnose the conversion from a 0 literal to a null pointer argument
607
  // in a synthesized call to operator<=>.
608
0
  if (!CodeSynthesisContexts.empty() &&
609
0
      CodeSynthesisContexts.back().Kind ==
610
0
          CodeSynthesisContext::RewritingOperatorAsSpaceship)
611
0
    return;
612
613
  // Ignore null pointers in defaulted comparison operators.
614
0
  FunctionDecl *FD = getCurFunctionDecl();
615
0
  if (FD && FD->isDefaulted()) {
616
0
    return;
617
0
  }
618
619
  // If it is a macro from system header, and if the macro name is not "NULL",
620
  // do not warn.
621
  // Note that uses of "NULL" will be ignored above on systems that define it
622
  // as __null.
623
0
  SourceLocation MaybeMacroLoc = E->getBeginLoc();
624
0
  if (Diags.getSuppressSystemWarnings() &&
625
0
      SourceMgr.isInSystemMacro(MaybeMacroLoc) &&
626
0
      !findMacroSpelling(MaybeMacroLoc, "NULL"))
627
0
    return;
628
629
0
  Diag(E->getBeginLoc(), diag::warn_zero_as_null_pointer_constant)
630
0
      << FixItHint::CreateReplacement(E->getSourceRange(), "nullptr");
631
0
}
632
633
/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
634
/// If there is already an implicit cast, merge into the existing one.
635
/// The result is of the given category.
636
ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
637
                                   CastKind Kind, ExprValueKind VK,
638
                                   const CXXCastPath *BasePath,
639
1
                                   CheckedConversionKind CCK) {
640
1
#ifndef NDEBUG
641
1
  if (VK == VK_PRValue && !E->isPRValue()) {
642
0
    switch (Kind) {
643
0
    default:
644
0
      llvm_unreachable(
645
0
          ("can't implicitly cast glvalue to prvalue with this cast "
646
0
           "kind: " +
647
0
           std::string(CastExpr::getCastKindName(Kind)))
648
0
              .c_str());
649
0
    case CK_Dependent:
650
0
    case CK_LValueToRValue:
651
0
    case CK_ArrayToPointerDecay:
652
0
    case CK_FunctionToPointerDecay:
653
0
    case CK_ToVoid:
654
0
    case CK_NonAtomicToAtomic:
655
0
      break;
656
0
    }
657
0
  }
658
1
  assert((VK == VK_PRValue || Kind == CK_Dependent || !E->isPRValue()) &&
659
1
         "can't cast prvalue to glvalue");
660
0
#endif
661
662
0
  diagnoseNullableToNonnullConversion(Ty, E->getType(), E->getBeginLoc());
663
1
  diagnoseZeroToNullptrConversion(Kind, E);
664
665
1
  QualType ExprTy = Context.getCanonicalType(E->getType());
666
1
  QualType TypeTy = Context.getCanonicalType(Ty);
667
668
1
  if (ExprTy == TypeTy)
669
0
    return E;
670
671
1
  if (Kind == CK_ArrayToPointerDecay) {
672
    // C++1z [conv.array]: The temporary materialization conversion is applied.
673
    // We also use this to fuel C++ DR1213, which applies to C++11 onwards.
674
0
    if (getLangOpts().CPlusPlus && E->isPRValue()) {
675
      // The temporary is an lvalue in C++98 and an xvalue otherwise.
676
0
      ExprResult Materialized = CreateMaterializeTemporaryExpr(
677
0
          E->getType(), E, !getLangOpts().CPlusPlus11);
678
0
      if (Materialized.isInvalid())
679
0
        return ExprError();
680
0
      E = Materialized.get();
681
0
    }
682
    // C17 6.7.1p6 footnote 124: The implementation can treat any register
683
    // declaration simply as an auto declaration. However, whether or not
684
    // addressable storage is actually used, the address of any part of an
685
    // object declared with storage-class specifier register cannot be
686
    // computed, either explicitly(by use of the unary & operator as discussed
687
    // in 6.5.3.2) or implicitly(by converting an array name to a pointer as
688
    // discussed in 6.3.2.1).Thus, the only operator that can be applied to an
689
    // array declared with storage-class specifier register is sizeof.
690
0
    if (VK == VK_PRValue && !getLangOpts().CPlusPlus && !E->isPRValue()) {
691
0
      if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
692
0
        if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
693
0
          if (VD->getStorageClass() == SC_Register) {
694
0
            Diag(E->getExprLoc(), diag::err_typecheck_address_of)
695
0
                << /*register variable*/ 3 << E->getSourceRange();
696
0
            return ExprError();
697
0
          }
698
0
        }
699
0
      }
700
0
    }
701
0
  }
702
703
1
  if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
704
1
    if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
705
0
      ImpCast->setType(Ty);
706
0
      ImpCast->setValueKind(VK);
707
0
      return E;
708
0
    }
709
1
  }
710
711
1
  return ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK,
712
1
                                  CurFPFeatureOverrides());
713
1
}
714
715
/// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
716
/// to the conversion from scalar type ScalarTy to the Boolean type.
717
0
CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
718
0
  switch (ScalarTy->getScalarTypeKind()) {
719
0
  case Type::STK_Bool: return CK_NoOp;
720
0
  case Type::STK_CPointer: return CK_PointerToBoolean;
721
0
  case Type::STK_BlockPointer: return CK_PointerToBoolean;
722
0
  case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean;
723
0
  case Type::STK_MemberPointer: return CK_MemberPointerToBoolean;
724
0
  case Type::STK_Integral: return CK_IntegralToBoolean;
725
0
  case Type::STK_Floating: return CK_FloatingToBoolean;
726
0
  case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean;
727
0
  case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean;
728
0
  case Type::STK_FixedPoint: return CK_FixedPointToBoolean;
729
0
  }
730
0
  llvm_unreachable("unknown scalar type kind");
731
0
}
732
733
/// Used to prune the decls of Sema's UnusedFileScopedDecls vector.
734
0
static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
735
0
  if (D->getMostRecentDecl()->isUsed())
736
0
    return true;
737
738
0
  if (D->isExternallyVisible())
739
0
    return true;
740
741
0
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
742
    // If this is a function template and none of its specializations is used,
743
    // we should warn.
744
0
    if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate())
745
0
      for (const auto *Spec : Template->specializations())
746
0
        if (ShouldRemoveFromUnused(SemaRef, Spec))
747
0
          return true;
748
749
    // UnusedFileScopedDecls stores the first declaration.
750
    // The declaration may have become definition so check again.
751
0
    const FunctionDecl *DeclToCheck;
752
0
    if (FD->hasBody(DeclToCheck))
753
0
      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
754
755
    // Later redecls may add new information resulting in not having to warn,
756
    // so check again.
757
0
    DeclToCheck = FD->getMostRecentDecl();
758
0
    if (DeclToCheck != FD)
759
0
      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
760
0
  }
761
762
0
  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
763
    // If a variable usable in constant expressions is referenced,
764
    // don't warn if it isn't used: if the value of a variable is required
765
    // for the computation of a constant expression, it doesn't make sense to
766
    // warn even if the variable isn't odr-used.  (isReferenced doesn't
767
    // precisely reflect that, but it's a decent approximation.)
768
0
    if (VD->isReferenced() &&
769
0
        VD->mightBeUsableInConstantExpressions(SemaRef->Context))
770
0
      return true;
771
772
0
    if (VarTemplateDecl *Template = VD->getDescribedVarTemplate())
773
      // If this is a variable template and none of its specializations is used,
774
      // we should warn.
775
0
      for (const auto *Spec : Template->specializations())
776
0
        if (ShouldRemoveFromUnused(SemaRef, Spec))
777
0
          return true;
778
779
    // UnusedFileScopedDecls stores the first declaration.
780
    // The declaration may have become definition so check again.
781
0
    const VarDecl *DeclToCheck = VD->getDefinition();
782
0
    if (DeclToCheck)
783
0
      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
784
785
    // Later redecls may add new information resulting in not having to warn,
786
    // so check again.
787
0
    DeclToCheck = VD->getMostRecentDecl();
788
0
    if (DeclToCheck != VD)
789
0
      return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck);
790
0
  }
791
792
0
  return false;
793
0
}
794
795
0
static bool isFunctionOrVarDeclExternC(const NamedDecl *ND) {
796
0
  if (const auto *FD = dyn_cast<FunctionDecl>(ND))
797
0
    return FD->isExternC();
798
0
  return cast<VarDecl>(ND)->isExternC();
799
0
}
800
801
/// Determine whether ND is an external-linkage function or variable whose
802
/// type has no linkage.
803
0
bool Sema::isExternalWithNoLinkageType(const ValueDecl *VD) const {
804
  // Note: it's not quite enough to check whether VD has UniqueExternalLinkage,
805
  // because we also want to catch the case where its type has VisibleNoLinkage,
806
  // which does not affect the linkage of VD.
807
0
  return getLangOpts().CPlusPlus && VD->hasExternalFormalLinkage() &&
808
0
         !isExternalFormalLinkage(VD->getType()->getLinkage()) &&
809
0
         !isFunctionOrVarDeclExternC(VD);
810
0
}
811
812
/// Obtains a sorted list of functions and variables that are undefined but
813
/// ODR-used.
814
void Sema::getUndefinedButUsed(
815
0
    SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) {
816
0
  for (const auto &UndefinedUse : UndefinedButUsed) {
817
0
    NamedDecl *ND = UndefinedUse.first;
818
819
    // Ignore attributes that have become invalid.
820
0
    if (ND->isInvalidDecl()) continue;
821
822
    // __attribute__((weakref)) is basically a definition.
823
0
    if (ND->hasAttr<WeakRefAttr>()) continue;
824
825
0
    if (isa<CXXDeductionGuideDecl>(ND))
826
0
      continue;
827
828
0
    if (ND->hasAttr<DLLImportAttr>() || ND->hasAttr<DLLExportAttr>()) {
829
      // An exported function will always be emitted when defined, so even if
830
      // the function is inline, it doesn't have to be emitted in this TU. An
831
      // imported function implies that it has been exported somewhere else.
832
0
      continue;
833
0
    }
834
835
0
    if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
836
0
      if (FD->isDefined())
837
0
        continue;
838
0
      if (FD->isExternallyVisible() &&
839
0
          !isExternalWithNoLinkageType(FD) &&
840
0
          !FD->getMostRecentDecl()->isInlined() &&
841
0
          !FD->hasAttr<ExcludeFromExplicitInstantiationAttr>())
842
0
        continue;
843
0
      if (FD->getBuiltinID())
844
0
        continue;
845
0
    } else {
846
0
      const auto *VD = cast<VarDecl>(ND);
847
0
      if (VD->hasDefinition() != VarDecl::DeclarationOnly)
848
0
        continue;
849
0
      if (VD->isExternallyVisible() &&
850
0
          !isExternalWithNoLinkageType(VD) &&
851
0
          !VD->getMostRecentDecl()->isInline() &&
852
0
          !VD->hasAttr<ExcludeFromExplicitInstantiationAttr>())
853
0
        continue;
854
855
      // Skip VarDecls that lack formal definitions but which we know are in
856
      // fact defined somewhere.
857
0
      if (VD->isKnownToBeDefined())
858
0
        continue;
859
0
    }
860
861
0
    Undefined.push_back(std::make_pair(ND, UndefinedUse.second));
862
0
  }
863
0
}
864
865
/// checkUndefinedButUsed - Check for undefined objects with internal linkage
866
/// or that are inline.
867
0
static void checkUndefinedButUsed(Sema &S) {
868
0
  if (S.UndefinedButUsed.empty()) return;
869
870
  // Collect all the still-undefined entities with internal linkage.
871
0
  SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
872
0
  S.getUndefinedButUsed(Undefined);
873
0
  S.UndefinedButUsed.clear();
874
0
  if (Undefined.empty()) return;
875
876
0
  for (const auto &Undef : Undefined) {
877
0
    ValueDecl *VD = cast<ValueDecl>(Undef.first);
878
0
    SourceLocation UseLoc = Undef.second;
879
880
0
    if (S.isExternalWithNoLinkageType(VD)) {
881
      // C++ [basic.link]p8:
882
      //   A type without linkage shall not be used as the type of a variable
883
      //   or function with external linkage unless
884
      //    -- the entity has C language linkage
885
      //    -- the entity is not odr-used or is defined in the same TU
886
      //
887
      // As an extension, accept this in cases where the type is externally
888
      // visible, since the function or variable actually can be defined in
889
      // another translation unit in that case.
890
0
      S.Diag(VD->getLocation(), isExternallyVisible(VD->getType()->getLinkage())
891
0
                                    ? diag::ext_undefined_internal_type
892
0
                                    : diag::err_undefined_internal_type)
893
0
        << isa<VarDecl>(VD) << VD;
894
0
    } else if (!VD->isExternallyVisible()) {
895
      // FIXME: We can promote this to an error. The function or variable can't
896
      // be defined anywhere else, so the program must necessarily violate the
897
      // one definition rule.
898
0
      bool IsImplicitBase = false;
899
0
      if (const auto *BaseD = dyn_cast<FunctionDecl>(VD)) {
900
0
        auto *DVAttr = BaseD->getAttr<OMPDeclareVariantAttr>();
901
0
        if (DVAttr && !DVAttr->getTraitInfo().isExtensionActive(
902
0
                          llvm::omp::TraitProperty::
903
0
                              implementation_extension_disable_implicit_base)) {
904
0
          const auto *Func = cast<FunctionDecl>(
905
0
              cast<DeclRefExpr>(DVAttr->getVariantFuncRef())->getDecl());
906
0
          IsImplicitBase = BaseD->isImplicit() &&
907
0
                           Func->getIdentifier()->isMangledOpenMPVariantName();
908
0
        }
909
0
      }
910
0
      if (!S.getLangOpts().OpenMP || !IsImplicitBase)
911
0
        S.Diag(VD->getLocation(), diag::warn_undefined_internal)
912
0
            << isa<VarDecl>(VD) << VD;
913
0
    } else if (auto *FD = dyn_cast<FunctionDecl>(VD)) {
914
0
      (void)FD;
915
0
      assert(FD->getMostRecentDecl()->isInlined() &&
916
0
             "used object requires definition but isn't inline or internal?");
917
      // FIXME: This is ill-formed; we should reject.
918
0
      S.Diag(VD->getLocation(), diag::warn_undefined_inline) << VD;
919
0
    } else {
920
0
      assert(cast<VarDecl>(VD)->getMostRecentDecl()->isInline() &&
921
0
             "used var requires definition but isn't inline or internal?");
922
0
      S.Diag(VD->getLocation(), diag::err_undefined_inline_var) << VD;
923
0
    }
924
0
    if (UseLoc.isValid())
925
0
      S.Diag(UseLoc, diag::note_used_here);
926
0
  }
927
0
}
928
929
5.13k
void Sema::LoadExternalWeakUndeclaredIdentifiers() {
930
5.13k
  if (!ExternalSource)
931
5.13k
    return;
932
933
0
  SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs;
934
0
  ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs);
935
0
  for (auto &WeakID : WeakIDs)
936
0
    (void)WeakUndeclaredIdentifiers[WeakID.first].insert(WeakID.second);
937
0
}
938
939
940
typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap;
941
942
/// Returns true, if all methods and nested classes of the given
943
/// CXXRecordDecl are defined in this translation unit.
944
///
945
/// Should only be called from ActOnEndOfTranslationUnit so that all
946
/// definitions are actually read.
947
static bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD,
948
0
                                            RecordCompleteMap &MNCComplete) {
949
0
  RecordCompleteMap::iterator Cache = MNCComplete.find(RD);
950
0
  if (Cache != MNCComplete.end())
951
0
    return Cache->second;
952
0
  if (!RD->isCompleteDefinition())
953
0
    return false;
954
0
  bool Complete = true;
955
0
  for (DeclContext::decl_iterator I = RD->decls_begin(),
956
0
                                  E = RD->decls_end();
957
0
       I != E && Complete; ++I) {
958
0
    if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I))
959
0
      Complete = M->isDefined() || M->isDefaulted() ||
960
0
                 (M->isPure() && !isa<CXXDestructorDecl>(M));
961
0
    else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I))
962
      // If the template function is marked as late template parsed at this
963
      // point, it has not been instantiated and therefore we have not
964
      // performed semantic analysis on it yet, so we cannot know if the type
965
      // can be considered complete.
966
0
      Complete = !F->getTemplatedDecl()->isLateTemplateParsed() &&
967
0
                  F->getTemplatedDecl()->isDefined();
968
0
    else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) {
969
0
      if (R->isInjectedClassName())
970
0
        continue;
971
0
      if (R->hasDefinition())
972
0
        Complete = MethodsAndNestedClassesComplete(R->getDefinition(),
973
0
                                                   MNCComplete);
974
0
      else
975
0
        Complete = false;
976
0
    }
977
0
  }
978
0
  MNCComplete[RD] = Complete;
979
0
  return Complete;
980
0
}
981
982
/// Returns true, if the given CXXRecordDecl is fully defined in this
983
/// translation unit, i.e. all methods are defined or pure virtual and all
984
/// friends, friend functions and nested classes are fully defined in this
985
/// translation unit.
986
///
987
/// Should only be called from ActOnEndOfTranslationUnit so that all
988
/// definitions are actually read.
989
static bool IsRecordFullyDefined(const CXXRecordDecl *RD,
990
                                 RecordCompleteMap &RecordsComplete,
991
0
                                 RecordCompleteMap &MNCComplete) {
992
0
  RecordCompleteMap::iterator Cache = RecordsComplete.find(RD);
993
0
  if (Cache != RecordsComplete.end())
994
0
    return Cache->second;
995
0
  bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete);
996
0
  for (CXXRecordDecl::friend_iterator I = RD->friend_begin(),
997
0
                                      E = RD->friend_end();
998
0
       I != E && Complete; ++I) {
999
    // Check if friend classes and methods are complete.
1000
0
    if (TypeSourceInfo *TSI = (*I)->getFriendType()) {
1001
      // Friend classes are available as the TypeSourceInfo of the FriendDecl.
1002
0
      if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl())
1003
0
        Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete);
1004
0
      else
1005
0
        Complete = false;
1006
0
    } else {
1007
      // Friend functions are available through the NamedDecl of FriendDecl.
1008
0
      if (const FunctionDecl *FD =
1009
0
          dyn_cast<FunctionDecl>((*I)->getFriendDecl()))
1010
0
        Complete = FD->isDefined();
1011
0
      else
1012
        // This is a template friend, give up.
1013
0
        Complete = false;
1014
0
    }
1015
0
  }
1016
0
  RecordsComplete[RD] = Complete;
1017
0
  return Complete;
1018
0
}
1019
1020
0
void Sema::emitAndClearUnusedLocalTypedefWarnings() {
1021
0
  if (ExternalSource)
1022
0
    ExternalSource->ReadUnusedLocalTypedefNameCandidates(
1023
0
        UnusedLocalTypedefNameCandidates);
1024
0
  for (const TypedefNameDecl *TD : UnusedLocalTypedefNameCandidates) {
1025
0
    if (TD->isReferenced())
1026
0
      continue;
1027
0
    Diag(TD->getLocation(), diag::warn_unused_local_typedef)
1028
0
        << isa<TypeAliasDecl>(TD) << TD->getDeclName();
1029
0
  }
1030
0
  UnusedLocalTypedefNameCandidates.clear();
1031
0
}
1032
1033
/// This is called before the very first declaration in the translation unit
1034
/// is parsed. Note that the ASTContext may have already injected some
1035
/// declarations.
1036
46
void Sema::ActOnStartOfTranslationUnit() {
1037
46
  if (getLangOpts().CPlusPlusModules &&
1038
46
      getLangOpts().getCompilingModule() == LangOptions::CMK_HeaderUnit)
1039
0
    HandleStartOfHeaderUnit();
1040
46
}
1041
1042
46
void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) {
1043
  // No explicit actions are required at the end of the global module fragment.
1044
46
  if (Kind == TUFragmentKind::Global)
1045
0
    return;
1046
1047
  // Transfer late parsed template instantiations over to the pending template
1048
  // instantiation list. During normal compilation, the late template parser
1049
  // will be installed and instantiating these templates will succeed.
1050
  //
1051
  // If we are building a TU prefix for serialization, it is also safe to
1052
  // transfer these over, even though they are not parsed. The end of the TU
1053
  // should be outside of any eager template instantiation scope, so when this
1054
  // AST is deserialized, these templates will not be parsed until the end of
1055
  // the combined TU.
1056
46
  PendingInstantiations.insert(PendingInstantiations.end(),
1057
46
                               LateParsedInstantiations.begin(),
1058
46
                               LateParsedInstantiations.end());
1059
46
  LateParsedInstantiations.clear();
1060
1061
  // If DefinedUsedVTables ends up marking any virtual member functions it
1062
  // might lead to more pending template instantiations, which we then need
1063
  // to instantiate.
1064
46
  DefineUsedVTables();
1065
1066
  // C++: Perform implicit template instantiations.
1067
  //
1068
  // FIXME: When we perform these implicit instantiations, we do not
1069
  // carefully keep track of the point of instantiation (C++ [temp.point]).
1070
  // This means that name lookup that occurs within the template
1071
  // instantiation will always happen at the end of the translation unit,
1072
  // so it will find some names that are not required to be found. This is
1073
  // valid, but we could do better by diagnosing if an instantiation uses a
1074
  // name that was not visible at its first point of instantiation.
1075
46
  if (ExternalSource) {
1076
    // Load pending instantiations from the external source.
1077
0
    SmallVector<PendingImplicitInstantiation, 4> Pending;
1078
0
    ExternalSource->ReadPendingInstantiations(Pending);
1079
0
    for (auto PII : Pending)
1080
0
      if (auto Func = dyn_cast<FunctionDecl>(PII.first))
1081
0
        Func->setInstantiationIsPending(true);
1082
0
    PendingInstantiations.insert(PendingInstantiations.begin(),
1083
0
                                 Pending.begin(), Pending.end());
1084
0
  }
1085
1086
46
  {
1087
46
    llvm::TimeTraceScope TimeScope("PerformPendingInstantiations");
1088
46
    PerformPendingInstantiations();
1089
46
  }
1090
1091
46
  emitDeferredDiags();
1092
1093
46
  assert(LateParsedInstantiations.empty() &&
1094
46
         "end of TU template instantiation should not create more "
1095
46
         "late-parsed templates");
1096
1097
  // Report diagnostics for uncorrected delayed typos. Ideally all of them
1098
  // should have been corrected by that time, but it is very hard to cover all
1099
  // cases in practice.
1100
0
  for (const auto &Typo : DelayedTypos) {
1101
    // We pass an empty TypoCorrection to indicate no correction was performed.
1102
0
    Typo.second.DiagHandler(TypoCorrection());
1103
0
  }
1104
46
  DelayedTypos.clear();
1105
46
}
1106
1107
/// ActOnEndOfTranslationUnit - This is called at the very end of the
1108
/// translation unit when EOF is reached and all but the top-level scope is
1109
/// popped.
1110
46
void Sema::ActOnEndOfTranslationUnit() {
1111
46
  assert(DelayedDiagnostics.getCurrentPool() == nullptr
1112
46
         && "reached end of translation unit with a pool attached?");
1113
1114
  // If code completion is enabled, don't perform any end-of-translation-unit
1115
  // work.
1116
46
  if (PP.isCodeCompletionEnabled())
1117
0
    return;
1118
1119
  // Complete translation units and modules define vtables and perform implicit
1120
  // instantiations. PCH files do not.
1121
46
  if (TUKind != TU_Prefix) {
1122
46
    DiagnoseUseOfUnimplementedSelectors();
1123
1124
46
    ActOnEndOfTranslationUnitFragment(
1125
46
        !ModuleScopes.empty() && ModuleScopes.back().Module->Kind ==
1126
0
                                     Module::PrivateModuleFragment
1127
46
            ? TUFragmentKind::Private
1128
46
            : TUFragmentKind::Normal);
1129
1130
46
    if (LateTemplateParserCleanup)
1131
0
      LateTemplateParserCleanup(OpaqueParser);
1132
1133
46
    CheckDelayedMemberExceptionSpecs();
1134
46
  } else {
1135
    // If we are building a TU prefix for serialization, it is safe to transfer
1136
    // these over, even though they are not parsed. The end of the TU should be
1137
    // outside of any eager template instantiation scope, so when this AST is
1138
    // deserialized, these templates will not be parsed until the end of the
1139
    // combined TU.
1140
0
    PendingInstantiations.insert(PendingInstantiations.end(),
1141
0
                                 LateParsedInstantiations.begin(),
1142
0
                                 LateParsedInstantiations.end());
1143
0
    LateParsedInstantiations.clear();
1144
1145
0
    if (LangOpts.PCHInstantiateTemplates) {
1146
0
      llvm::TimeTraceScope TimeScope("PerformPendingInstantiations");
1147
0
      PerformPendingInstantiations();
1148
0
    }
1149
0
  }
1150
1151
46
  DiagnoseUnterminatedPragmaAlignPack();
1152
46
  DiagnoseUnterminatedPragmaAttribute();
1153
46
  DiagnoseUnterminatedOpenMPDeclareTarget();
1154
1155
  // All delayed member exception specs should be checked or we end up accepting
1156
  // incompatible declarations.
1157
46
  assert(DelayedOverridingExceptionSpecChecks.empty());
1158
0
  assert(DelayedEquivalentExceptionSpecChecks.empty());
1159
1160
  // All dllexport classes should have been processed already.
1161
0
  assert(DelayedDllExportClasses.empty());
1162
0
  assert(DelayedDllExportMemberFunctions.empty());
1163
1164
  // Remove file scoped decls that turned out to be used.
1165
0
  UnusedFileScopedDecls.erase(
1166
46
      std::remove_if(UnusedFileScopedDecls.begin(nullptr, true),
1167
46
                     UnusedFileScopedDecls.end(),
1168
46
                     [this](const DeclaratorDecl *DD) {
1169
0
                       return ShouldRemoveFromUnused(this, DD);
1170
0
                     }),
1171
46
      UnusedFileScopedDecls.end());
1172
1173
46
  if (TUKind == TU_Prefix) {
1174
    // Translation unit prefixes don't need any of the checking below.
1175
0
    if (!PP.isIncrementalProcessingEnabled())
1176
0
      TUScope = nullptr;
1177
0
    return;
1178
0
  }
1179
1180
  // Check for #pragma weak identifiers that were never declared
1181
46
  LoadExternalWeakUndeclaredIdentifiers();
1182
46
  for (const auto &WeakIDs : WeakUndeclaredIdentifiers) {
1183
0
    if (WeakIDs.second.empty())
1184
0
      continue;
1185
1186
0
    Decl *PrevDecl = LookupSingleName(TUScope, WeakIDs.first, SourceLocation(),
1187
0
                                      LookupOrdinaryName);
1188
0
    if (PrevDecl != nullptr &&
1189
0
        !(isa<FunctionDecl>(PrevDecl) || isa<VarDecl>(PrevDecl)))
1190
0
      for (const auto &WI : WeakIDs.second)
1191
0
        Diag(WI.getLocation(), diag::warn_attribute_wrong_decl_type)
1192
0
            << "'weak'" << /*isRegularKeyword=*/0 << ExpectedVariableOrFunction;
1193
0
    else
1194
0
      for (const auto &WI : WeakIDs.second)
1195
0
        Diag(WI.getLocation(), diag::warn_weak_identifier_undeclared)
1196
0
            << WeakIDs.first;
1197
0
  }
1198
1199
46
  if (LangOpts.CPlusPlus11 &&
1200
46
      !Diags.isIgnored(diag::warn_delegating_ctor_cycle, SourceLocation()))
1201
23
    CheckDelegatingCtorCycles();
1202
1203
46
  if (!Diags.hasErrorOccurred()) {
1204
0
    if (ExternalSource)
1205
0
      ExternalSource->ReadUndefinedButUsed(UndefinedButUsed);
1206
0
    checkUndefinedButUsed(*this);
1207
0
  }
1208
1209
  // A global-module-fragment is only permitted within a module unit.
1210
46
  bool DiagnosedMissingModuleDeclaration = false;
1211
46
  if (!ModuleScopes.empty() && ModuleScopes.back().Module->Kind ==
1212
0
                                   Module::ExplicitGlobalModuleFragment) {
1213
0
    Diag(ModuleScopes.back().BeginLoc,
1214
0
         diag::err_module_declaration_missing_after_global_module_introducer);
1215
0
    DiagnosedMissingModuleDeclaration = true;
1216
0
  }
1217
1218
46
  if (TUKind == TU_Module) {
1219
    // If we are building a module interface unit, we need to have seen the
1220
    // module declaration by now.
1221
0
    if (getLangOpts().getCompilingModule() ==
1222
0
            LangOptions::CMK_ModuleInterface &&
1223
0
        !isCurrentModulePurview() && !DiagnosedMissingModuleDeclaration) {
1224
      // FIXME: Make a better guess as to where to put the module declaration.
1225
0
      Diag(getSourceManager().getLocForStartOfFile(
1226
0
               getSourceManager().getMainFileID()),
1227
0
           diag::err_module_declaration_missing);
1228
0
    }
1229
1230
    // If we are building a module, resolve all of the exported declarations
1231
    // now.
1232
0
    if (Module *CurrentModule = PP.getCurrentModule()) {
1233
0
      ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
1234
1235
0
      SmallVector<Module *, 2> Stack;
1236
0
      Stack.push_back(CurrentModule);
1237
0
      while (!Stack.empty()) {
1238
0
        Module *Mod = Stack.pop_back_val();
1239
1240
        // Resolve the exported declarations and conflicts.
1241
        // FIXME: Actually complain, once we figure out how to teach the
1242
        // diagnostic client to deal with complaints in the module map at this
1243
        // point.
1244
0
        ModMap.resolveExports(Mod, /*Complain=*/false);
1245
0
        ModMap.resolveUses(Mod, /*Complain=*/false);
1246
0
        ModMap.resolveConflicts(Mod, /*Complain=*/false);
1247
1248
        // Queue the submodules, so their exports will also be resolved.
1249
0
        auto SubmodulesRange = Mod->submodules();
1250
0
        Stack.append(SubmodulesRange.begin(), SubmodulesRange.end());
1251
0
      }
1252
0
    }
1253
1254
    // Now we can decide whether the modules we're building need an initializer.
1255
0
    if (Module *CurrentModule = getCurrentModule();
1256
0
        CurrentModule && CurrentModule->isInterfaceOrPartition()) {
1257
0
      auto DoesModNeedInit = [this](Module *M) {
1258
0
        if (!getASTContext().getModuleInitializers(M).empty())
1259
0
          return true;
1260
0
        for (auto [Exported, _] : M->Exports)
1261
0
          if (Exported->isNamedModuleInterfaceHasInit())
1262
0
            return true;
1263
0
        for (Module *I : M->Imports)
1264
0
          if (I->isNamedModuleInterfaceHasInit())
1265
0
            return true;
1266
1267
0
        return false;
1268
0
      };
1269
1270
0
      CurrentModule->NamedModuleHasInit =
1271
0
          DoesModNeedInit(CurrentModule) ||
1272
0
          llvm::any_of(CurrentModule->submodules(),
1273
0
                       [&](auto *SubM) { return DoesModNeedInit(SubM); });
1274
0
    }
1275
1276
    // Warnings emitted in ActOnEndOfTranslationUnit() should be emitted for
1277
    // modules when they are built, not every time they are used.
1278
0
    emitAndClearUnusedLocalTypedefWarnings();
1279
0
  }
1280
1281
  // C++ standard modules. Diagnose cases where a function is declared inline
1282
  // in the module purview but has no definition before the end of the TU or
1283
  // the start of a Private Module Fragment (if one is present).
1284
46
  if (!PendingInlineFuncDecls.empty()) {
1285
0
    for (auto *D : PendingInlineFuncDecls) {
1286
0
      if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1287
0
        bool DefInPMF = false;
1288
0
        if (auto *FDD = FD->getDefinition()) {
1289
0
          DefInPMF = FDD->getOwningModule()->isPrivateModule();
1290
0
          if (!DefInPMF)
1291
0
            continue;
1292
0
        }
1293
0
        Diag(FD->getLocation(), diag::err_export_inline_not_defined)
1294
0
            << DefInPMF;
1295
        // If we have a PMF it should be at the end of the ModuleScopes.
1296
0
        if (DefInPMF &&
1297
0
            ModuleScopes.back().Module->Kind == Module::PrivateModuleFragment) {
1298
0
          Diag(ModuleScopes.back().BeginLoc,
1299
0
               diag::note_private_module_fragment);
1300
0
        }
1301
0
      }
1302
0
    }
1303
0
    PendingInlineFuncDecls.clear();
1304
0
  }
1305
1306
  // C99 6.9.2p2:
1307
  //   A declaration of an identifier for an object that has file
1308
  //   scope without an initializer, and without a storage-class
1309
  //   specifier or with the storage-class specifier static,
1310
  //   constitutes a tentative definition. If a translation unit
1311
  //   contains one or more tentative definitions for an identifier,
1312
  //   and the translation unit contains no external definition for
1313
  //   that identifier, then the behavior is exactly as if the
1314
  //   translation unit contains a file scope declaration of that
1315
  //   identifier, with the composite type as of the end of the
1316
  //   translation unit, with an initializer equal to 0.
1317
46
  llvm::SmallSet<VarDecl *, 32> Seen;
1318
46
  for (TentativeDefinitionsType::iterator
1319
46
           T = TentativeDefinitions.begin(ExternalSource.get()),
1320
46
           TEnd = TentativeDefinitions.end();
1321
462
       T != TEnd; ++T) {
1322
416
    VarDecl *VD = (*T)->getActingDefinition();
1323
1324
    // If the tentative definition was completed, getActingDefinition() returns
1325
    // null. If we've already seen this variable before, insert()'s second
1326
    // return value is false.
1327
416
    if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second)
1328
49
      continue;
1329
1330
367
    if (const IncompleteArrayType *ArrayT
1331
367
        = Context.getAsIncompleteArrayType(VD->getType())) {
1332
      // Set the length of the array to 1 (C99 6.9.2p5).
1333
0
      Diag(VD->getLocation(), diag::warn_tentative_incomplete_array);
1334
0
      llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true);
1335
0
      QualType T = Context.getConstantArrayType(
1336
0
          ArrayT->getElementType(), One, nullptr, ArraySizeModifier::Normal, 0);
1337
0
      VD->setType(T);
1338
367
    } else if (RequireCompleteType(VD->getLocation(), VD->getType(),
1339
367
                                   diag::err_tentative_def_incomplete_type))
1340
0
      VD->setInvalidDecl();
1341
1342
    // No initialization is performed for a tentative definition.
1343
367
    CheckCompleteVariableDeclaration(VD);
1344
1345
    // Notify the consumer that we've completed a tentative definition.
1346
367
    if (!VD->isInvalidDecl())
1347
367
      Consumer.CompleteTentativeDefinition(VD);
1348
367
  }
1349
1350
46
  for (auto *D : ExternalDeclarations) {
1351
0
    if (!D || D->isInvalidDecl() || D->getPreviousDecl() || !D->isUsed())
1352
0
      continue;
1353
1354
0
    Consumer.CompleteExternalDeclaration(D);
1355
0
  }
1356
1357
  // If there were errors, disable 'unused' warnings since they will mostly be
1358
  // noise. Don't warn for a use from a module: either we should warn on all
1359
  // file-scope declarations in modules or not at all, but whether the
1360
  // declaration is used is immaterial.
1361
46
  if (!Diags.hasErrorOccurred() && TUKind != TU_Module) {
1362
    // Output warning for unused file scoped decls.
1363
0
    for (UnusedFileScopedDeclsType::iterator
1364
0
             I = UnusedFileScopedDecls.begin(ExternalSource.get()),
1365
0
             E = UnusedFileScopedDecls.end();
1366
0
         I != E; ++I) {
1367
0
      if (ShouldRemoveFromUnused(this, *I))
1368
0
        continue;
1369
1370
0
      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
1371
0
        const FunctionDecl *DiagD;
1372
0
        if (!FD->hasBody(DiagD))
1373
0
          DiagD = FD;
1374
0
        if (DiagD->isDeleted())
1375
0
          continue; // Deleted functions are supposed to be unused.
1376
0
        SourceRange DiagRange = DiagD->getLocation();
1377
0
        if (const ASTTemplateArgumentListInfo *ASTTAL =
1378
0
                DiagD->getTemplateSpecializationArgsAsWritten())
1379
0
          DiagRange.setEnd(ASTTAL->RAngleLoc);
1380
0
        if (DiagD->isReferenced()) {
1381
0
          if (isa<CXXMethodDecl>(DiagD))
1382
0
            Diag(DiagD->getLocation(), diag::warn_unneeded_member_function)
1383
0
                << DiagD << DiagRange;
1384
0
          else {
1385
0
            if (FD->getStorageClass() == SC_Static &&
1386
0
                !FD->isInlineSpecified() &&
1387
0
                !SourceMgr.isInMainFile(
1388
0
                   SourceMgr.getExpansionLoc(FD->getLocation())))
1389
0
              Diag(DiagD->getLocation(),
1390
0
                   diag::warn_unneeded_static_internal_decl)
1391
0
                  << DiagD << DiagRange;
1392
0
            else
1393
0
              Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
1394
0
                  << /*function=*/0 << DiagD << DiagRange;
1395
0
          }
1396
0
        } else {
1397
0
          if (FD->getDescribedFunctionTemplate())
1398
0
            Diag(DiagD->getLocation(), diag::warn_unused_template)
1399
0
                << /*function=*/0 << DiagD << DiagRange;
1400
0
          else
1401
0
            Diag(DiagD->getLocation(), isa<CXXMethodDecl>(DiagD)
1402
0
                                           ? diag::warn_unused_member_function
1403
0
                                           : diag::warn_unused_function)
1404
0
                << DiagD << DiagRange;
1405
0
        }
1406
0
      } else {
1407
0
        const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
1408
0
        if (!DiagD)
1409
0
          DiagD = cast<VarDecl>(*I);
1410
0
        SourceRange DiagRange = DiagD->getLocation();
1411
0
        if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(DiagD)) {
1412
0
          if (const ASTTemplateArgumentListInfo *ASTTAL =
1413
0
                  VTSD->getTemplateArgsInfo())
1414
0
            DiagRange.setEnd(ASTTAL->RAngleLoc);
1415
0
        }
1416
0
        if (DiagD->isReferenced()) {
1417
0
          Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
1418
0
              << /*variable=*/1 << DiagD << DiagRange;
1419
0
        } else if (DiagD->getDescribedVarTemplate()) {
1420
0
          Diag(DiagD->getLocation(), diag::warn_unused_template)
1421
0
              << /*variable=*/1 << DiagD << DiagRange;
1422
0
        } else if (DiagD->getType().isConstQualified()) {
1423
0
          const SourceManager &SM = SourceMgr;
1424
0
          if (SM.getMainFileID() != SM.getFileID(DiagD->getLocation()) ||
1425
0
              !PP.getLangOpts().IsHeaderFile)
1426
0
            Diag(DiagD->getLocation(), diag::warn_unused_const_variable)
1427
0
                << DiagD << DiagRange;
1428
0
        } else {
1429
0
          Diag(DiagD->getLocation(), diag::warn_unused_variable)
1430
0
              << DiagD << DiagRange;
1431
0
        }
1432
0
      }
1433
0
    }
1434
1435
0
    emitAndClearUnusedLocalTypedefWarnings();
1436
0
  }
1437
1438
46
  if (!Diags.isIgnored(diag::warn_unused_private_field, SourceLocation())) {
1439
    // FIXME: Load additional unused private field candidates from the external
1440
    // source.
1441
0
    RecordCompleteMap RecordsComplete;
1442
0
    RecordCompleteMap MNCComplete;
1443
0
    for (const NamedDecl *D : UnusedPrivateFields) {
1444
0
      const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
1445
0
      if (RD && !RD->isUnion() &&
1446
0
          IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) {
1447
0
        Diag(D->getLocation(), diag::warn_unused_private_field)
1448
0
              << D->getDeclName();
1449
0
      }
1450
0
    }
1451
0
  }
1452
1453
46
  if (!Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) {
1454
46
    if (ExternalSource)
1455
0
      ExternalSource->ReadMismatchingDeleteExpressions(DeleteExprs);
1456
46
    for (const auto &DeletedFieldInfo : DeleteExprs) {
1457
0
      for (const auto &DeleteExprLoc : DeletedFieldInfo.second) {
1458
0
        AnalyzeDeleteExprMismatch(DeletedFieldInfo.first, DeleteExprLoc.first,
1459
0
                                  DeleteExprLoc.second);
1460
0
      }
1461
0
    }
1462
46
  }
1463
1464
46
  AnalysisWarnings.IssueWarnings(Context.getTranslationUnitDecl());
1465
1466
  // Check we've noticed that we're no longer parsing the initializer for every
1467
  // variable. If we miss cases, then at best we have a performance issue and
1468
  // at worst a rejects-valid bug.
1469
46
  assert(ParsingInitForAutoVars.empty() &&
1470
46
         "Didn't unmark var as having its initializer parsed");
1471
1472
46
  if (!PP.isIncrementalProcessingEnabled())
1473
46
    TUScope = nullptr;
1474
46
}
1475
1476
1477
//===----------------------------------------------------------------------===//
1478
// Helper functions.
1479
//===----------------------------------------------------------------------===//
1480
1481
15.2k
DeclContext *Sema::getFunctionLevelDeclContext(bool AllowLambda) const {
1482
15.2k
  DeclContext *DC = CurContext;
1483
1484
15.2k
  while (true) {
1485
15.2k
    if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC) ||
1486
15.2k
        isa<RequiresExprBodyDecl>(DC)) {
1487
4
      DC = DC->getParent();
1488
15.2k
    } else if (!AllowLambda && isa<CXXMethodDecl>(DC) &&
1489
15.2k
               cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call &&
1490
15.2k
               cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
1491
0
      DC = DC->getParent()->getParent();
1492
15.2k
    } else break;
1493
15.2k
  }
1494
1495
15.2k
  return DC;
1496
15.2k
}
1497
1498
/// getCurFunctionDecl - If inside of a function body, this returns a pointer
1499
/// to the function decl for the function being parsed.  If we're currently
1500
/// in a 'block', this returns the containing context.
1501
12.5k
FunctionDecl *Sema::getCurFunctionDecl(bool AllowLambda) const {
1502
12.5k
  DeclContext *DC = getFunctionLevelDeclContext(AllowLambda);
1503
12.5k
  return dyn_cast<FunctionDecl>(DC);
1504
12.5k
}
1505
1506
2.64k
ObjCMethodDecl *Sema::getCurMethodDecl() {
1507
2.64k
  DeclContext *DC = getFunctionLevelDeclContext();
1508
2.64k
  while (isa<RecordDecl>(DC))
1509
0
    DC = DC->getParent();
1510
2.64k
  return dyn_cast<ObjCMethodDecl>(DC);
1511
2.64k
}
1512
1513
69
NamedDecl *Sema::getCurFunctionOrMethodDecl() const {
1514
69
  DeclContext *DC = getFunctionLevelDeclContext();
1515
69
  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
1516
0
    return cast<NamedDecl>(DC);
1517
69
  return nullptr;
1518
69
}
1519
1520
0
LangAS Sema::getDefaultCXXMethodAddrSpace() const {
1521
0
  if (getLangOpts().OpenCL)
1522
0
    return getASTContext().getDefaultOpenCLPointeeAddrSpace();
1523
0
  return LangAS::Default;
1524
0
}
1525
1526
12.4k
void Sema::EmitCurrentDiagnostic(unsigned DiagID) {
1527
  // FIXME: It doesn't make sense to me that DiagID is an incoming argument here
1528
  // and yet we also use the current diag ID on the DiagnosticsEngine. This has
1529
  // been made more painfully obvious by the refactor that introduced this
1530
  // function, but it is possible that the incoming argument can be
1531
  // eliminated. If it truly cannot be (for example, there is some reentrancy
1532
  // issue I am not seeing yet), then there should at least be a clarifying
1533
  // comment somewhere.
1534
12.4k
  if (std::optional<TemplateDeductionInfo *> Info = isSFINAEContext()) {
1535
0
    switch (DiagnosticIDs::getDiagnosticSFINAEResponse(
1536
0
              Diags.getCurrentDiagID())) {
1537
0
    case DiagnosticIDs::SFINAE_Report:
1538
      // We'll report the diagnostic below.
1539
0
      break;
1540
1541
0
    case DiagnosticIDs::SFINAE_SubstitutionFailure:
1542
      // Count this failure so that we know that template argument deduction
1543
      // has failed.
1544
0
      ++NumSFINAEErrors;
1545
1546
      // Make a copy of this suppressed diagnostic and store it with the
1547
      // template-deduction information.
1548
0
      if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
1549
0
        Diagnostic DiagInfo(&Diags);
1550
0
        (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
1551
0
                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
1552
0
      }
1553
1554
0
      Diags.setLastDiagnosticIgnored(true);
1555
0
      Diags.Clear();
1556
0
      return;
1557
1558
0
    case DiagnosticIDs::SFINAE_AccessControl: {
1559
      // Per C++ Core Issue 1170, access control is part of SFINAE.
1560
      // Additionally, the AccessCheckingSFINAE flag can be used to temporarily
1561
      // make access control a part of SFINAE for the purposes of checking
1562
      // type traits.
1563
0
      if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11)
1564
0
        break;
1565
1566
0
      SourceLocation Loc = Diags.getCurrentDiagLoc();
1567
1568
      // Suppress this diagnostic.
1569
0
      ++NumSFINAEErrors;
1570
1571
      // Make a copy of this suppressed diagnostic and store it with the
1572
      // template-deduction information.
1573
0
      if (*Info && !(*Info)->hasSFINAEDiagnostic()) {
1574
0
        Diagnostic DiagInfo(&Diags);
1575
0
        (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(),
1576
0
                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
1577
0
      }
1578
1579
0
      Diags.setLastDiagnosticIgnored(true);
1580
0
      Diags.Clear();
1581
1582
      // Now the diagnostic state is clear, produce a C++98 compatibility
1583
      // warning.
1584
0
      Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control);
1585
1586
      // The last diagnostic which Sema produced was ignored. Suppress any
1587
      // notes attached to it.
1588
0
      Diags.setLastDiagnosticIgnored(true);
1589
0
      return;
1590
0
    }
1591
1592
0
    case DiagnosticIDs::SFINAE_Suppress:
1593
      // Make a copy of this suppressed diagnostic and store it with the
1594
      // template-deduction information;
1595
0
      if (*Info) {
1596
0
        Diagnostic DiagInfo(&Diags);
1597
0
        (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(),
1598
0
                       PartialDiagnostic(DiagInfo, Context.getDiagAllocator()));
1599
0
      }
1600
1601
      // Suppress this diagnostic.
1602
0
      Diags.setLastDiagnosticIgnored(true);
1603
0
      Diags.Clear();
1604
0
      return;
1605
0
    }
1606
0
  }
1607
1608
  // Copy the diagnostic printing policy over the ASTContext printing policy.
1609
  // TODO: Stop doing that.  See: https://reviews.llvm.org/D45093#1090292
1610
12.4k
  Context.setPrintingPolicy(getPrintingPolicy());
1611
1612
  // Emit the diagnostic.
1613
12.4k
  if (!Diags.EmitCurrentDiagnostic())
1614
109
    return;
1615
1616
  // If this is not a note, and we're in a template instantiation
1617
  // that is different from the last template instantiation where
1618
  // we emitted an error, print a template instantiation
1619
  // backtrace.
1620
12.3k
  if (!DiagnosticIDs::isBuiltinNote(DiagID))
1621
12.3k
    PrintContextStack();
1622
12.3k
}
1623
1624
Sema::SemaDiagnosticBuilder
1625
1
Sema::Diag(SourceLocation Loc, const PartialDiagnostic &PD, bool DeferHint) {
1626
1
  return Diag(Loc, PD.getDiagID(), DeferHint) << PD;
1627
1
}
1628
1629
47
bool Sema::hasUncompilableErrorOccurred() const {
1630
47
  if (getDiagnostics().hasUncompilableErrorOccurred())
1631
47
    return true;
1632
0
  auto *FD = dyn_cast<FunctionDecl>(CurContext);
1633
0
  if (!FD)
1634
0
    return false;
1635
0
  auto Loc = DeviceDeferredDiags.find(FD);
1636
0
  if (Loc == DeviceDeferredDiags.end())
1637
0
    return false;
1638
0
  for (auto PDAt : Loc->second) {
1639
0
    if (DiagnosticIDs::isDefaultMappingAsError(PDAt.second.getDiagID()))
1640
0
      return true;
1641
0
  }
1642
0
  return false;
1643
0
}
1644
1645
// Print notes showing how we can reach FD starting from an a priori
1646
// known-callable function.
1647
0
static void emitCallStackNotes(Sema &S, const FunctionDecl *FD) {
1648
0
  auto FnIt = S.DeviceKnownEmittedFns.find(FD);
1649
0
  while (FnIt != S.DeviceKnownEmittedFns.end()) {
1650
    // Respect error limit.
1651
0
    if (S.Diags.hasFatalErrorOccurred())
1652
0
      return;
1653
0
    DiagnosticBuilder Builder(
1654
0
        S.Diags.Report(FnIt->second.Loc, diag::note_called_by));
1655
0
    Builder << FnIt->second.FD;
1656
0
    FnIt = S.DeviceKnownEmittedFns.find(FnIt->second.FD);
1657
0
  }
1658
0
}
1659
1660
namespace {
1661
1662
/// Helper class that emits deferred diagnostic messages if an entity directly
1663
/// or indirectly using the function that causes the deferred diagnostic
1664
/// messages is known to be emitted.
1665
///
1666
/// During parsing of AST, certain diagnostic messages are recorded as deferred
1667
/// diagnostics since it is unknown whether the functions containing such
1668
/// diagnostics will be emitted. A list of potentially emitted functions and
1669
/// variables that may potentially trigger emission of functions are also
1670
/// recorded. DeferredDiagnosticsEmitter recursively visits used functions
1671
/// by each function to emit deferred diagnostics.
1672
///
1673
/// During the visit, certain OpenMP directives or initializer of variables
1674
/// with certain OpenMP attributes will cause subsequent visiting of any
1675
/// functions enter a state which is called OpenMP device context in this
1676
/// implementation. The state is exited when the directive or initializer is
1677
/// exited. This state can change the emission states of subsequent uses
1678
/// of functions.
1679
///
1680
/// Conceptually the functions or variables to be visited form a use graph
1681
/// where the parent node uses the child node. At any point of the visit,
1682
/// the tree nodes traversed from the tree root to the current node form a use
1683
/// stack. The emission state of the current node depends on two factors:
1684
///    1. the emission state of the root node
1685
///    2. whether the current node is in OpenMP device context
1686
/// If the function is decided to be emitted, its contained deferred diagnostics
1687
/// are emitted, together with the information about the use stack.
1688
///
1689
class DeferredDiagnosticsEmitter
1690
    : public UsedDeclVisitor<DeferredDiagnosticsEmitter> {
1691
public:
1692
  typedef UsedDeclVisitor<DeferredDiagnosticsEmitter> Inherited;
1693
1694
  // Whether the function is already in the current use-path.
1695
  llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> InUsePath;
1696
1697
  // The current use-path.
1698
  llvm::SmallVector<CanonicalDeclPtr<FunctionDecl>, 4> UsePath;
1699
1700
  // Whether the visiting of the function has been done. Done[0] is for the
1701
  // case not in OpenMP device context. Done[1] is for the case in OpenMP
1702
  // device context. We need two sets because diagnostics emission may be
1703
  // different depending on whether it is in OpenMP device context.
1704
  llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> DoneMap[2];
1705
1706
  // Emission state of the root node of the current use graph.
1707
  bool ShouldEmitRootNode;
1708
1709
  // Current OpenMP device context level. It is initialized to 0 and each
1710
  // entering of device context increases it by 1 and each exit decreases
1711
  // it by 1. Non-zero value indicates it is currently in device context.
1712
  unsigned InOMPDeviceContext;
1713
1714
  DeferredDiagnosticsEmitter(Sema &S)
1715
0
      : Inherited(S), ShouldEmitRootNode(false), InOMPDeviceContext(0) {}
1716
1717
0
  bool shouldVisitDiscardedStmt() const { return false; }
1718
1719
0
  void VisitOMPTargetDirective(OMPTargetDirective *Node) {
1720
0
    ++InOMPDeviceContext;
1721
0
    Inherited::VisitOMPTargetDirective(Node);
1722
0
    --InOMPDeviceContext;
1723
0
  }
1724
1725
0
  void visitUsedDecl(SourceLocation Loc, Decl *D) {
1726
0
    if (isa<VarDecl>(D))
1727
0
      return;
1728
0
    if (auto *FD = dyn_cast<FunctionDecl>(D))
1729
0
      checkFunc(Loc, FD);
1730
0
    else
1731
0
      Inherited::visitUsedDecl(Loc, D);
1732
0
  }
1733
1734
0
  void checkVar(VarDecl *VD) {
1735
0
    assert(VD->isFileVarDecl() &&
1736
0
           "Should only check file-scope variables");
1737
0
    if (auto *Init = VD->getInit()) {
1738
0
      auto DevTy = OMPDeclareTargetDeclAttr::getDeviceType(VD);
1739
0
      bool IsDev = DevTy && (*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost ||
1740
0
                             *DevTy == OMPDeclareTargetDeclAttr::DT_Any);
1741
0
      if (IsDev)
1742
0
        ++InOMPDeviceContext;
1743
0
      this->Visit(Init);
1744
0
      if (IsDev)
1745
0
        --InOMPDeviceContext;
1746
0
    }
1747
0
  }
1748
1749
0
  void checkFunc(SourceLocation Loc, FunctionDecl *FD) {
1750
0
    auto &Done = DoneMap[InOMPDeviceContext > 0 ? 1 : 0];
1751
0
    FunctionDecl *Caller = UsePath.empty() ? nullptr : UsePath.back();
1752
0
    if ((!ShouldEmitRootNode && !S.getLangOpts().OpenMP && !Caller) ||
1753
0
        S.shouldIgnoreInHostDeviceCheck(FD) || InUsePath.count(FD))
1754
0
      return;
1755
    // Finalize analysis of OpenMP-specific constructs.
1756
0
    if (Caller && S.LangOpts.OpenMP && UsePath.size() == 1 &&
1757
0
        (ShouldEmitRootNode || InOMPDeviceContext))
1758
0
      S.finalizeOpenMPDelayedAnalysis(Caller, FD, Loc);
1759
0
    if (Caller)
1760
0
      S.DeviceKnownEmittedFns[FD] = {Caller, Loc};
1761
    // Always emit deferred diagnostics for the direct users. This does not
1762
    // lead to explosion of diagnostics since each user is visited at most
1763
    // twice.
1764
0
    if (ShouldEmitRootNode || InOMPDeviceContext)
1765
0
      emitDeferredDiags(FD, Caller);
1766
    // Do not revisit a function if the function body has been completely
1767
    // visited before.
1768
0
    if (!Done.insert(FD).second)
1769
0
      return;
1770
0
    InUsePath.insert(FD);
1771
0
    UsePath.push_back(FD);
1772
0
    if (auto *S = FD->getBody()) {
1773
0
      this->Visit(S);
1774
0
    }
1775
0
    UsePath.pop_back();
1776
0
    InUsePath.erase(FD);
1777
0
  }
1778
1779
0
  void checkRecordedDecl(Decl *D) {
1780
0
    if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1781
0
      ShouldEmitRootNode = S.getEmissionStatus(FD, /*Final=*/true) ==
1782
0
                           Sema::FunctionEmissionStatus::Emitted;
1783
0
      checkFunc(SourceLocation(), FD);
1784
0
    } else
1785
0
      checkVar(cast<VarDecl>(D));
1786
0
  }
1787
1788
  // Emit any deferred diagnostics for FD
1789
0
  void emitDeferredDiags(FunctionDecl *FD, bool ShowCallStack) {
1790
0
    auto It = S.DeviceDeferredDiags.find(FD);
1791
0
    if (It == S.DeviceDeferredDiags.end())
1792
0
      return;
1793
0
    bool HasWarningOrError = false;
1794
0
    bool FirstDiag = true;
1795
0
    for (PartialDiagnosticAt &PDAt : It->second) {
1796
      // Respect error limit.
1797
0
      if (S.Diags.hasFatalErrorOccurred())
1798
0
        return;
1799
0
      const SourceLocation &Loc = PDAt.first;
1800
0
      const PartialDiagnostic &PD = PDAt.second;
1801
0
      HasWarningOrError |=
1802
0
          S.getDiagnostics().getDiagnosticLevel(PD.getDiagID(), Loc) >=
1803
0
          DiagnosticsEngine::Warning;
1804
0
      {
1805
0
        DiagnosticBuilder Builder(S.Diags.Report(Loc, PD.getDiagID()));
1806
0
        PD.Emit(Builder);
1807
0
      }
1808
      // Emit the note on the first diagnostic in case too many diagnostics
1809
      // cause the note not emitted.
1810
0
      if (FirstDiag && HasWarningOrError && ShowCallStack) {
1811
0
        emitCallStackNotes(S, FD);
1812
0
        FirstDiag = false;
1813
0
      }
1814
0
    }
1815
0
  }
1816
};
1817
} // namespace
1818
1819
46
void Sema::emitDeferredDiags() {
1820
46
  if (ExternalSource)
1821
0
    ExternalSource->ReadDeclsToCheckForDeferredDiags(
1822
0
        DeclsToCheckForDeferredDiags);
1823
1824
46
  if ((DeviceDeferredDiags.empty() && !LangOpts.OpenMP) ||
1825
46
      DeclsToCheckForDeferredDiags.empty())
1826
46
    return;
1827
1828
0
  DeferredDiagnosticsEmitter DDE(*this);
1829
0
  for (auto *D : DeclsToCheckForDeferredDiags)
1830
0
    DDE.checkRecordedDecl(D);
1831
0
}
1832
1833
// In CUDA, there are some constructs which may appear in semantically-valid
1834
// code, but trigger errors if we ever generate code for the function in which
1835
// they appear.  Essentially every construct you're not allowed to use on the
1836
// device falls into this category, because you are allowed to use these
1837
// constructs in a __host__ __device__ function, but only if that function is
1838
// never codegen'ed on the device.
1839
//
1840
// To handle semantic checking for these constructs, we keep track of the set of
1841
// functions we know will be emitted, either because we could tell a priori that
1842
// they would be emitted, or because they were transitively called by a
1843
// known-emitted function.
1844
//
1845
// We also keep a partial call graph of which not-known-emitted functions call
1846
// which other not-known-emitted functions.
1847
//
1848
// When we see something which is illegal if the current function is emitted
1849
// (usually by way of CUDADiagIfDeviceCode, CUDADiagIfHostCode, or
1850
// CheckCUDACall), we first check if the current function is known-emitted.  If
1851
// so, we immediately output the diagnostic.
1852
//
1853
// Otherwise, we "defer" the diagnostic.  It sits in Sema::DeviceDeferredDiags
1854
// until we discover that the function is known-emitted, at which point we take
1855
// it out of this map and emit the diagnostic.
1856
1857
Sema::SemaDiagnosticBuilder::SemaDiagnosticBuilder(Kind K, SourceLocation Loc,
1858
                                                   unsigned DiagID,
1859
                                                   const FunctionDecl *Fn,
1860
                                                   Sema &S)
1861
    : S(S), Loc(Loc), DiagID(DiagID), Fn(Fn),
1862
12.4k
      ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) {
1863
12.4k
  switch (K) {
1864
0
  case K_Nop:
1865
0
    break;
1866
12.4k
  case K_Immediate:
1867
12.4k
  case K_ImmediateWithCallStack:
1868
12.4k
    ImmediateDiag.emplace(
1869
12.4k
        ImmediateDiagBuilder(S.Diags.Report(Loc, DiagID), S, DiagID));
1870
12.4k
    break;
1871
0
  case K_Deferred:
1872
0
    assert(Fn && "Must have a function to attach the deferred diag to.");
1873
0
    auto &Diags = S.DeviceDeferredDiags[Fn];
1874
0
    PartialDiagId.emplace(Diags.size());
1875
0
    Diags.emplace_back(Loc, S.PDiag(DiagID));
1876
0
    break;
1877
12.4k
  }
1878
12.4k
}
1879
1880
Sema::SemaDiagnosticBuilder::SemaDiagnosticBuilder(SemaDiagnosticBuilder &&D)
1881
    : S(D.S), Loc(D.Loc), DiagID(D.DiagID), Fn(D.Fn),
1882
      ShowCallStack(D.ShowCallStack), ImmediateDiag(D.ImmediateDiag),
1883
0
      PartialDiagId(D.PartialDiagId) {
1884
  // Clean the previous diagnostics.
1885
0
  D.ShowCallStack = false;
1886
0
  D.ImmediateDiag.reset();
1887
0
  D.PartialDiagId.reset();
1888
0
}
1889
1890
12.4k
Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
1891
12.4k
  if (ImmediateDiag) {
1892
    // Emit our diagnostic and, if it was a warning or error, output a callstack
1893
    // if Fn isn't a priori known-emitted.
1894
12.4k
    bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel(
1895
12.4k
                                DiagID, Loc) >= DiagnosticsEngine::Warning;
1896
12.4k
    ImmediateDiag.reset(); // Emit the immediate diag.
1897
12.4k
    if (IsWarningOrError && ShowCallStack)
1898
0
      emitCallStackNotes(S, Fn);
1899
12.4k
  } else {
1900
0
    assert((!PartialDiagId || ShowCallStack) &&
1901
0
           "Must always show call stack for deferred diags.");
1902
0
  }
1903
12.4k
}
1904
1905
Sema::SemaDiagnosticBuilder
1906
0
Sema::targetDiag(SourceLocation Loc, unsigned DiagID, const FunctionDecl *FD) {
1907
0
  FD = FD ? FD : getCurFunctionDecl();
1908
0
  if (LangOpts.OpenMP)
1909
0
    return LangOpts.OpenMPIsTargetDevice
1910
0
               ? diagIfOpenMPDeviceCode(Loc, DiagID, FD)
1911
0
               : diagIfOpenMPHostCode(Loc, DiagID, FD);
1912
0
  if (getLangOpts().CUDA)
1913
0
    return getLangOpts().CUDAIsDevice ? CUDADiagIfDeviceCode(Loc, DiagID)
1914
0
                                      : CUDADiagIfHostCode(Loc, DiagID);
1915
1916
0
  if (getLangOpts().SYCLIsDevice)
1917
0
    return SYCLDiagIfDeviceCode(Loc, DiagID);
1918
1919
0
  return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate, Loc, DiagID,
1920
0
                               FD, *this);
1921
0
}
1922
1923
Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID,
1924
12.4k
                                       bool DeferHint) {
1925
12.4k
  bool IsError = Diags.getDiagnosticIDs()->isDefaultMappingAsError(DiagID);
1926
12.4k
  bool ShouldDefer = getLangOpts().CUDA && LangOpts.GPUDeferDiag &&
1927
12.4k
                     DiagnosticIDs::isDeferrable(DiagID) &&
1928
12.4k
                     (DeferHint || DeferDiags || !IsError);
1929
12.4k
  auto SetIsLastErrorImmediate = [&](bool Flag) {
1930
12.4k
    if (IsError)
1931
12.3k
      IsLastErrorImmediate = Flag;
1932
12.4k
  };
1933
12.4k
  if (!ShouldDefer) {
1934
12.4k
    SetIsLastErrorImmediate(true);
1935
12.4k
    return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate, Loc,
1936
12.4k
                                 DiagID, getCurFunctionDecl(), *this);
1937
12.4k
  }
1938
1939
0
  SemaDiagnosticBuilder DB = getLangOpts().CUDAIsDevice
1940
0
                                 ? CUDADiagIfDeviceCode(Loc, DiagID)
1941
0
                                 : CUDADiagIfHostCode(Loc, DiagID);
1942
0
  SetIsLastErrorImmediate(DB.isImmediate());
1943
0
  return DB;
1944
12.4k
}
1945
1946
59
void Sema::checkTypeSupport(QualType Ty, SourceLocation Loc, ValueDecl *D) {
1947
59
  if (isUnevaluatedContext() || Ty.isNull())
1948
0
    return;
1949
1950
  // The original idea behind checkTypeSupport function is that unused
1951
  // declarations can be replaced with an array of bytes of the same size during
1952
  // codegen, such replacement doesn't seem to be possible for types without
1953
  // constant byte size like zero length arrays. So, do a deep check for SYCL.
1954
59
  if (D && LangOpts.SYCLIsDevice) {
1955
0
    llvm::DenseSet<QualType> Visited;
1956
0
    deepTypeCheckForSYCLDevice(Loc, Visited, D);
1957
0
  }
1958
1959
59
  Decl *C = cast<Decl>(getCurLexicalContext());
1960
1961
  // Memcpy operations for structs containing a member with unsupported type
1962
  // are ok, though.
1963
59
  if (const auto *MD = dyn_cast<CXXMethodDecl>(C)) {
1964
0
    if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) &&
1965
0
        MD->isTrivial())
1966
0
      return;
1967
1968
0
    if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(MD))
1969
0
      if (Ctor->isCopyOrMoveConstructor() && Ctor->isTrivial())
1970
0
        return;
1971
0
  }
1972
1973
  // Try to associate errors with the lexical context, if that is a function, or
1974
  // the value declaration otherwise.
1975
59
  const FunctionDecl *FD = isa<FunctionDecl>(C)
1976
59
                               ? cast<FunctionDecl>(C)
1977
59
                               : dyn_cast_or_null<FunctionDecl>(D);
1978
1979
59
  auto CheckDeviceType = [&](QualType Ty) {
1980
0
    if (Ty->isDependentType())
1981
0
      return;
1982
1983
0
    if (Ty->isBitIntType()) {
1984
0
      if (!Context.getTargetInfo().hasBitIntType()) {
1985
0
        PartialDiagnostic PD = PDiag(diag::err_target_unsupported_type);
1986
0
        if (D)
1987
0
          PD << D;
1988
0
        else
1989
0
          PD << "expression";
1990
0
        targetDiag(Loc, PD, FD)
1991
0
            << false /*show bit size*/ << 0 /*bitsize*/ << false /*return*/
1992
0
            << Ty << Context.getTargetInfo().getTriple().str();
1993
0
      }
1994
0
      return;
1995
0
    }
1996
1997
    // Check if we are dealing with two 'long double' but with different
1998
    // semantics.
1999
0
    bool LongDoubleMismatched = false;
2000
0
    if (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128) {
2001
0
      const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(Ty);
2002
0
      if ((&Sem != &llvm::APFloat::PPCDoubleDouble() &&
2003
0
           !Context.getTargetInfo().hasFloat128Type()) ||
2004
0
          (&Sem == &llvm::APFloat::PPCDoubleDouble() &&
2005
0
           !Context.getTargetInfo().hasIbm128Type()))
2006
0
        LongDoubleMismatched = true;
2007
0
    }
2008
2009
0
    if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
2010
0
        (Ty->isFloat128Type() && !Context.getTargetInfo().hasFloat128Type()) ||
2011
0
        (Ty->isIbm128Type() && !Context.getTargetInfo().hasIbm128Type()) ||
2012
0
        (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
2013
0
         !Context.getTargetInfo().hasInt128Type()) ||
2014
0
        (Ty->isBFloat16Type() && !Context.getTargetInfo().hasBFloat16Type() &&
2015
0
         !LangOpts.CUDAIsDevice) ||
2016
0
        LongDoubleMismatched) {
2017
0
      PartialDiagnostic PD = PDiag(diag::err_target_unsupported_type);
2018
0
      if (D)
2019
0
        PD << D;
2020
0
      else
2021
0
        PD << "expression";
2022
2023
0
      if (targetDiag(Loc, PD, FD)
2024
0
          << true /*show bit size*/
2025
0
          << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty
2026
0
          << false /*return*/ << Context.getTargetInfo().getTriple().str()) {
2027
0
        if (D)
2028
0
          D->setInvalidDecl();
2029
0
      }
2030
0
      if (D)
2031
0
        targetDiag(D->getLocation(), diag::note_defined_here, FD) << D;
2032
0
    }
2033
0
  };
2034
2035
59
  auto CheckType = [&](QualType Ty, bool IsRetTy = false) {
2036
59
    if (LangOpts.SYCLIsDevice ||
2037
59
        (LangOpts.OpenMP && LangOpts.OpenMPIsTargetDevice) ||
2038
59
        LangOpts.CUDAIsDevice)
2039
0
      CheckDeviceType(Ty);
2040
2041
59
    QualType UnqualTy = Ty.getCanonicalType().getUnqualifiedType();
2042
59
    const TargetInfo &TI = Context.getTargetInfo();
2043
59
    if (!TI.hasLongDoubleType() && UnqualTy == Context.LongDoubleTy) {
2044
0
      PartialDiagnostic PD = PDiag(diag::err_target_unsupported_type);
2045
0
      if (D)
2046
0
        PD << D;
2047
0
      else
2048
0
        PD << "expression";
2049
2050
0
      if (Diag(Loc, PD, FD)
2051
0
          << false /*show bit size*/ << 0 << Ty << false /*return*/
2052
0
          << TI.getTriple().str()) {
2053
0
        if (D)
2054
0
          D->setInvalidDecl();
2055
0
      }
2056
0
      if (D)
2057
0
        targetDiag(D->getLocation(), diag::note_defined_here, FD) << D;
2058
0
    }
2059
2060
59
    bool IsDouble = UnqualTy == Context.DoubleTy;
2061
59
    bool IsFloat = UnqualTy == Context.FloatTy;
2062
59
    if (IsRetTy && !TI.hasFPReturn() && (IsDouble || IsFloat)) {
2063
0
      PartialDiagnostic PD = PDiag(diag::err_target_unsupported_type);
2064
0
      if (D)
2065
0
        PD << D;
2066
0
      else
2067
0
        PD << "expression";
2068
2069
0
      if (Diag(Loc, PD, FD)
2070
0
          << false /*show bit size*/ << 0 << Ty << true /*return*/
2071
0
          << TI.getTriple().str()) {
2072
0
        if (D)
2073
0
          D->setInvalidDecl();
2074
0
      }
2075
0
      if (D)
2076
0
        targetDiag(D->getLocation(), diag::note_defined_here, FD) << D;
2077
0
    }
2078
2079
59
    if (TI.hasRISCVVTypes() && Ty->isRVVSizelessBuiltinType())
2080
0
      checkRVVTypeSupport(Ty, Loc, D);
2081
2082
    // Don't allow SVE types in functions without a SVE target.
2083
59
    if (Ty->isSVESizelessBuiltinType() && FD && FD->hasBody()) {
2084
0
      llvm::StringMap<bool> CallerFeatureMap;
2085
0
      Context.getFunctionFeatureMap(CallerFeatureMap, FD);
2086
0
      if (!Builtin::evaluateRequiredTargetFeatures("sve", CallerFeatureMap) &&
2087
0
          !Builtin::evaluateRequiredTargetFeatures("sme", CallerFeatureMap))
2088
0
        Diag(D->getLocation(), diag::err_sve_vector_in_non_sve_target) << Ty;
2089
0
    }
2090
59
  };
2091
2092
59
  CheckType(Ty);
2093
59
  if (const auto *FPTy = dyn_cast<FunctionProtoType>(Ty)) {
2094
0
    for (const auto &ParamTy : FPTy->param_types())
2095
0
      CheckType(ParamTy);
2096
0
    CheckType(FPTy->getReturnType(), /*IsRetTy=*/true);
2097
0
  }
2098
59
  if (const auto *FNPTy = dyn_cast<FunctionNoProtoType>(Ty))
2099
0
    CheckType(FNPTy->getReturnType(), /*IsRetTy=*/true);
2100
59
}
2101
2102
/// Looks through the macro-expansion chain for the given
2103
/// location, looking for a macro expansion with the given name.
2104
/// If one is found, returns true and sets the location to that
2105
/// expansion loc.
2106
0
bool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) {
2107
0
  SourceLocation loc = locref;
2108
0
  if (!loc.isMacroID()) return false;
2109
2110
  // There's no good way right now to look at the intermediate
2111
  // expansions, so just jump to the expansion location.
2112
0
  loc = getSourceManager().getExpansionLoc(loc);
2113
2114
  // If that's written with the name, stop here.
2115
0
  SmallString<16> buffer;
2116
0
  if (getPreprocessor().getSpelling(loc, buffer) == name) {
2117
0
    locref = loc;
2118
0
    return true;
2119
0
  }
2120
0
  return false;
2121
0
}
2122
2123
/// Determines the active Scope associated with the given declaration
2124
/// context.
2125
///
2126
/// This routine maps a declaration context to the active Scope object that
2127
/// represents that declaration context in the parser. It is typically used
2128
/// from "scope-less" code (e.g., template instantiation, lazy creation of
2129
/// declarations) that injects a name for name-lookup purposes and, therefore,
2130
/// must update the Scope.
2131
///
2132
/// \returns The scope corresponding to the given declaraion context, or NULL
2133
/// if no such scope is open.
2134
0
Scope *Sema::getScopeForContext(DeclContext *Ctx) {
2135
2136
0
  if (!Ctx)
2137
0
    return nullptr;
2138
2139
0
  Ctx = Ctx->getPrimaryContext();
2140
0
  for (Scope *S = getCurScope(); S; S = S->getParent()) {
2141
    // Ignore scopes that cannot have declarations. This is important for
2142
    // out-of-line definitions of static class members.
2143
0
    if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope))
2144
0
      if (DeclContext *Entity = S->getEntity())
2145
0
        if (Ctx == Entity->getPrimaryContext())
2146
0
          return S;
2147
0
  }
2148
2149
0
  return nullptr;
2150
0
}
2151
2152
/// Enter a new function scope
2153
0
void Sema::PushFunctionScope() {
2154
0
  if (FunctionScopes.empty() && CachedFunctionScope) {
2155
    // Use CachedFunctionScope to avoid allocating memory when possible.
2156
0
    CachedFunctionScope->Clear();
2157
0
    FunctionScopes.push_back(CachedFunctionScope.release());
2158
0
  } else {
2159
0
    FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics()));
2160
0
  }
2161
0
  if (LangOpts.OpenMP)
2162
0
    pushOpenMPFunctionRegion();
2163
0
}
2164
2165
5
void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
2166
5
  FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
2167
5
                                              BlockScope, Block));
2168
5
  CapturingFunctionScopes++;
2169
5
}
2170
2171
0
LambdaScopeInfo *Sema::PushLambdaScope() {
2172
0
  LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics());
2173
0
  FunctionScopes.push_back(LSI);
2174
0
  CapturingFunctionScopes++;
2175
0
  return LSI;
2176
0
}
2177
2178
0
void Sema::RecordParsingTemplateParameterDepth(unsigned Depth) {
2179
0
  if (LambdaScopeInfo *const LSI = getCurLambda()) {
2180
0
    LSI->AutoTemplateParameterDepth = Depth;
2181
0
    return;
2182
0
  }
2183
0
  llvm_unreachable(
2184
0
      "Remove assertion if intentionally called in a non-lambda context.");
2185
0
}
2186
2187
// Check that the type of the VarDecl has an accessible copy constructor and
2188
// resolve its destructor's exception specification.
2189
// This also performs initialization of block variables when they are moved
2190
// to the heap. It uses the same rules as applicable for implicit moves
2191
// according to the C++ standard in effect ([class.copy.elision]p3).
2192
0
static void checkEscapingByref(VarDecl *VD, Sema &S) {
2193
0
  QualType T = VD->getType();
2194
0
  EnterExpressionEvaluationContext scope(
2195
0
      S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
2196
0
  SourceLocation Loc = VD->getLocation();
2197
0
  Expr *VarRef =
2198
0
      new (S.Context) DeclRefExpr(S.Context, VD, false, T, VK_LValue, Loc);
2199
0
  ExprResult Result;
2200
0
  auto IE = InitializedEntity::InitializeBlock(Loc, T);
2201
0
  if (S.getLangOpts().CPlusPlus23) {
2202
0
    auto *E = ImplicitCastExpr::Create(S.Context, T, CK_NoOp, VarRef, nullptr,
2203
0
                                       VK_XValue, FPOptionsOverride());
2204
0
    Result = S.PerformCopyInitialization(IE, SourceLocation(), E);
2205
0
  } else {
2206
0
    Result = S.PerformMoveOrCopyInitialization(
2207
0
        IE, Sema::NamedReturnInfo{VD, Sema::NamedReturnInfo::MoveEligible},
2208
0
        VarRef);
2209
0
  }
2210
2211
0
  if (!Result.isInvalid()) {
2212
0
    Result = S.MaybeCreateExprWithCleanups(Result);
2213
0
    Expr *Init = Result.getAs<Expr>();
2214
0
    S.Context.setBlockVarCopyInit(VD, Init, S.canThrow(Init));
2215
0
  }
2216
2217
  // The destructor's exception specification is needed when IRGen generates
2218
  // block copy/destroy functions. Resolve it here.
2219
0
  if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
2220
0
    if (CXXDestructorDecl *DD = RD->getDestructor()) {
2221
0
      auto *FPT = DD->getType()->getAs<FunctionProtoType>();
2222
0
      S.ResolveExceptionSpec(Loc, FPT);
2223
0
    }
2224
0
}
2225
2226
5
static void markEscapingByrefs(const FunctionScopeInfo &FSI, Sema &S) {
2227
  // Set the EscapingByref flag of __block variables captured by
2228
  // escaping blocks.
2229
5
  for (const BlockDecl *BD : FSI.Blocks) {
2230
0
    for (const BlockDecl::Capture &BC : BD->captures()) {
2231
0
      VarDecl *VD = BC.getVariable();
2232
0
      if (VD->hasAttr<BlocksAttr>()) {
2233
        // Nothing to do if this is a __block variable captured by a
2234
        // non-escaping block.
2235
0
        if (BD->doesNotEscape())
2236
0
          continue;
2237
0
        VD->setEscapingByref();
2238
0
      }
2239
      // Check whether the captured variable is or contains an object of
2240
      // non-trivial C union type.
2241
0
      QualType CapType = BC.getVariable()->getType();
2242
0
      if (CapType.hasNonTrivialToPrimitiveDestructCUnion() ||
2243
0
          CapType.hasNonTrivialToPrimitiveCopyCUnion())
2244
0
        S.checkNonTrivialCUnion(BC.getVariable()->getType(),
2245
0
                                BD->getCaretLocation(),
2246
0
                                Sema::NTCUC_BlockCapture,
2247
0
                                Sema::NTCUK_Destruct|Sema::NTCUK_Copy);
2248
0
    }
2249
0
  }
2250
2251
5
  for (VarDecl *VD : FSI.ByrefBlockVars) {
2252
    // __block variables might require us to capture a copy-initializer.
2253
0
    if (!VD->isEscapingByref())
2254
0
      continue;
2255
    // It's currently invalid to ever have a __block variable with an
2256
    // array type; should we diagnose that here?
2257
    // Regardless, we don't want to ignore array nesting when
2258
    // constructing this copy.
2259
0
    if (VD->getType()->isStructureOrClassType())
2260
0
      checkEscapingByref(VD, S);
2261
0
  }
2262
5
}
2263
2264
/// Pop a function (or block or lambda or captured region) scope from the stack.
2265
///
2266
/// \param WP The warning policy to use for CFG-based warnings, or null if such
2267
///        warnings should not be produced.
2268
/// \param D The declaration corresponding to this function scope, if producing
2269
///        CFG-based warnings.
2270
/// \param BlockType The type of the block expression, if D is a BlockDecl.
2271
Sema::PoppedFunctionScopePtr
2272
Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP,
2273
5
                           const Decl *D, QualType BlockType) {
2274
5
  assert(!FunctionScopes.empty() && "mismatched push/pop!");
2275
2276
0
  markEscapingByrefs(*FunctionScopes.back(), *this);
2277
2278
5
  PoppedFunctionScopePtr Scope(FunctionScopes.pop_back_val(),
2279
5
                               PoppedFunctionScopeDeleter(this));
2280
2281
5
  if (LangOpts.OpenMP)
2282
0
    popOpenMPFunctionRegion(Scope.get());
2283
2284
  // Issue any analysis-based warnings.
2285
5
  if (WP && D)
2286
1
    AnalysisWarnings.IssueWarnings(*WP, Scope.get(), D, BlockType);
2287
4
  else
2288
4
    for (const auto &PUD : Scope->PossiblyUnreachableDiags)
2289
0
      Diag(PUD.Loc, PUD.PD);
2290
2291
5
  return Scope;
2292
5
}
2293
2294
void Sema::PoppedFunctionScopeDeleter::
2295
5
operator()(sema::FunctionScopeInfo *Scope) const {
2296
5
  if (!Scope->isPlainFunction())
2297
5
    Self->CapturingFunctionScopes--;
2298
  // Stash the function scope for later reuse if it's for a normal function.
2299
5
  if (Scope->isPlainFunction() && !Self->CachedFunctionScope)
2300
0
    Self->CachedFunctionScope.reset(Scope);
2301
5
  else
2302
5
    delete Scope;
2303
5
}
2304
2305
1
void Sema::PushCompoundScope(bool IsStmtExpr) {
2306
1
  getCurFunction()->CompoundScopes.push_back(
2307
1
      CompoundScopeInfo(IsStmtExpr, getCurFPFeatures()));
2308
1
}
2309
2310
1
void Sema::PopCompoundScope() {
2311
1
  FunctionScopeInfo *CurFunction = getCurFunction();
2312
1
  assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop");
2313
2314
0
  CurFunction->CompoundScopes.pop_back();
2315
1
}
2316
2317
/// Determine whether any errors occurred within this function/method/
2318
/// block.
2319
1
bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
2320
1
  return getCurFunction()->hasUnrecoverableErrorOccurred();
2321
1
}
2322
2323
0
void Sema::setFunctionHasBranchIntoScope() {
2324
0
  if (!FunctionScopes.empty())
2325
0
    FunctionScopes.back()->setHasBranchIntoScope();
2326
0
}
2327
2328
2
void Sema::setFunctionHasBranchProtectedScope() {
2329
2
  if (!FunctionScopes.empty())
2330
0
    FunctionScopes.back()->setHasBranchProtectedScope();
2331
2
}
2332
2333
0
void Sema::setFunctionHasIndirectGoto() {
2334
0
  if (!FunctionScopes.empty())
2335
0
    FunctionScopes.back()->setHasIndirectGoto();
2336
0
}
2337
2338
0
void Sema::setFunctionHasMustTail() {
2339
0
  if (!FunctionScopes.empty())
2340
0
    FunctionScopes.back()->setHasMustTail();
2341
0
}
2342
2343
79
BlockScopeInfo *Sema::getCurBlock() {
2344
79
  if (FunctionScopes.empty())
2345
69
    return nullptr;
2346
2347
10
  auto CurBSI = dyn_cast<BlockScopeInfo>(FunctionScopes.back());
2348
10
  if (CurBSI && CurBSI->TheDecl &&
2349
10
      !CurBSI->TheDecl->Encloses(CurContext)) {
2350
    // We have switched contexts due to template instantiation.
2351
0
    assert(!CodeSynthesisContexts.empty());
2352
0
    return nullptr;
2353
0
  }
2354
2355
10
  return CurBSI;
2356
10
}
2357
2358
0
FunctionScopeInfo *Sema::getEnclosingFunction() const {
2359
0
  if (FunctionScopes.empty())
2360
0
    return nullptr;
2361
2362
0
  for (int e = FunctionScopes.size() - 1; e >= 0; --e) {
2363
0
    if (isa<sema::BlockScopeInfo>(FunctionScopes[e]))
2364
0
      continue;
2365
0
    return FunctionScopes[e];
2366
0
  }
2367
0
  return nullptr;
2368
0
}
2369
2370
0
LambdaScopeInfo *Sema::getEnclosingLambda() const {
2371
0
  for (auto *Scope : llvm::reverse(FunctionScopes)) {
2372
0
    if (auto *LSI = dyn_cast<sema::LambdaScopeInfo>(Scope)) {
2373
0
      if (LSI->Lambda && !LSI->Lambda->Encloses(CurContext) &&
2374
0
          LSI->AfterParameterList) {
2375
        // We have switched contexts due to template instantiation.
2376
        // FIXME: We should swap out the FunctionScopes during code synthesis
2377
        // so that we don't need to check for this.
2378
0
        assert(!CodeSynthesisContexts.empty());
2379
0
        return nullptr;
2380
0
      }
2381
0
      return LSI;
2382
0
    }
2383
0
  }
2384
0
  return nullptr;
2385
0
}
2386
2387
63
LambdaScopeInfo *Sema::getCurLambda(bool IgnoreNonLambdaCapturingScope) {
2388
63
  if (FunctionScopes.empty())
2389
62
    return nullptr;
2390
2391
1
  auto I = FunctionScopes.rbegin();
2392
1
  if (IgnoreNonLambdaCapturingScope) {
2393
0
    auto E = FunctionScopes.rend();
2394
0
    while (I != E && isa<CapturingScopeInfo>(*I) && !isa<LambdaScopeInfo>(*I))
2395
0
      ++I;
2396
0
    if (I == E)
2397
0
      return nullptr;
2398
0
  }
2399
1
  auto *CurLSI = dyn_cast<LambdaScopeInfo>(*I);
2400
1
  if (CurLSI && CurLSI->Lambda && CurLSI->CallOperator &&
2401
1
      !CurLSI->Lambda->Encloses(CurContext) && CurLSI->AfterParameterList) {
2402
    // We have switched contexts due to template instantiation.
2403
0
    assert(!CodeSynthesisContexts.empty());
2404
0
    return nullptr;
2405
0
  }
2406
2407
1
  return CurLSI;
2408
1
}
2409
2410
// We have a generic lambda if we parsed auto parameters, or we have
2411
// an associated template parameter list.
2412
0
LambdaScopeInfo *Sema::getCurGenericLambda() {
2413
0
  if (LambdaScopeInfo *LSI =  getCurLambda()) {
2414
0
    return (LSI->TemplateParams.size() ||
2415
0
                    LSI->GLTemplateParameterList) ? LSI : nullptr;
2416
0
  }
2417
0
  return nullptr;
2418
0
}
2419
2420
2421
943
void Sema::ActOnComment(SourceRange Comment) {
2422
943
  if (!LangOpts.RetainCommentsFromSystemHeaders &&
2423
943
      SourceMgr.isInSystemHeader(Comment.getBegin()))
2424
0
    return;
2425
943
  RawComment RC(SourceMgr, Comment, LangOpts.CommentOpts, false);
2426
943
  if (RC.isAlmostTrailingComment() || RC.hasUnsupportedSplice(SourceMgr)) {
2427
1
    SourceRange MagicMarkerRange(Comment.getBegin(),
2428
1
                                 Comment.getBegin().getLocWithOffset(3));
2429
1
    StringRef MagicMarkerText;
2430
1
    switch (RC.getKind()) {
2431
1
    case RawComment::RCK_OrdinaryBCPL:
2432
1
      MagicMarkerText = "///<";
2433
1
      break;
2434
0
    case RawComment::RCK_OrdinaryC:
2435
0
      MagicMarkerText = "/**<";
2436
0
      break;
2437
0
    case RawComment::RCK_Invalid:
2438
      // FIXME: are there other scenarios that could produce an invalid
2439
      // raw comment here?
2440
0
      Diag(Comment.getBegin(), diag::warn_splice_in_doxygen_comment);
2441
0
      return;
2442
0
    default:
2443
0
      llvm_unreachable("if this is an almost Doxygen comment, "
2444
1
                       "it should be ordinary");
2445
1
    }
2446
1
    Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) <<
2447
1
      FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText);
2448
1
  }
2449
943
  Context.addComment(RC);
2450
943
}
2451
2452
// Pin this vtable to this file.
2453
0
ExternalSemaSource::~ExternalSemaSource() {}
2454
char ExternalSemaSource::ID;
2455
2456
0
void ExternalSemaSource::ReadMethodPool(Selector Sel) { }
2457
0
void ExternalSemaSource::updateOutOfDateSelector(Selector Sel) { }
2458
2459
void ExternalSemaSource::ReadKnownNamespaces(
2460
0
                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
2461
0
}
2462
2463
void ExternalSemaSource::ReadUndefinedButUsed(
2464
0
    llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {}
2465
2466
void ExternalSemaSource::ReadMismatchingDeleteExpressions(llvm::MapVector<
2467
0
    FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &) {}
2468
2469
/// Figure out if an expression could be turned into a call.
2470
///
2471
/// Use this when trying to recover from an error where the programmer may have
2472
/// written just the name of a function instead of actually calling it.
2473
///
2474
/// \param E - The expression to examine.
2475
/// \param ZeroArgCallReturnTy - If the expression can be turned into a call
2476
///  with no arguments, this parameter is set to the type returned by such a
2477
///  call; otherwise, it is set to an empty QualType.
2478
/// \param OverloadSet - If the expression is an overloaded function
2479
///  name, this parameter is populated with the decls of the various overloads.
2480
bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy,
2481
0
                         UnresolvedSetImpl &OverloadSet) {
2482
0
  ZeroArgCallReturnTy = QualType();
2483
0
  OverloadSet.clear();
2484
2485
0
  const OverloadExpr *Overloads = nullptr;
2486
0
  bool IsMemExpr = false;
2487
0
  if (E.getType() == Context.OverloadTy) {
2488
0
    OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E));
2489
2490
    // Ignore overloads that are pointer-to-member constants.
2491
0
    if (FR.HasFormOfMemberPointer)
2492
0
      return false;
2493
2494
0
    Overloads = FR.Expression;
2495
0
  } else if (E.getType() == Context.BoundMemberTy) {
2496
0
    Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens());
2497
0
    IsMemExpr = true;
2498
0
  }
2499
2500
0
  bool Ambiguous = false;
2501
0
  bool IsMV = false;
2502
2503
0
  if (Overloads) {
2504
0
    for (OverloadExpr::decls_iterator it = Overloads->decls_begin(),
2505
0
         DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) {
2506
0
      OverloadSet.addDecl(*it);
2507
2508
      // Check whether the function is a non-template, non-member which takes no
2509
      // arguments.
2510
0
      if (IsMemExpr)
2511
0
        continue;
2512
0
      if (const FunctionDecl *OverloadDecl
2513
0
            = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) {
2514
0
        if (OverloadDecl->getMinRequiredArguments() == 0) {
2515
0
          if (!ZeroArgCallReturnTy.isNull() && !Ambiguous &&
2516
0
              (!IsMV || !(OverloadDecl->isCPUDispatchMultiVersion() ||
2517
0
                          OverloadDecl->isCPUSpecificMultiVersion()))) {
2518
0
            ZeroArgCallReturnTy = QualType();
2519
0
            Ambiguous = true;
2520
0
          } else {
2521
0
            ZeroArgCallReturnTy = OverloadDecl->getReturnType();
2522
0
            IsMV = OverloadDecl->isCPUDispatchMultiVersion() ||
2523
0
                   OverloadDecl->isCPUSpecificMultiVersion();
2524
0
          }
2525
0
        }
2526
0
      }
2527
0
    }
2528
2529
    // If it's not a member, use better machinery to try to resolve the call
2530
0
    if (!IsMemExpr)
2531
0
      return !ZeroArgCallReturnTy.isNull();
2532
0
  }
2533
2534
  // Attempt to call the member with no arguments - this will correctly handle
2535
  // member templates with defaults/deduction of template arguments, overloads
2536
  // with default arguments, etc.
2537
0
  if (IsMemExpr && !E.isTypeDependent()) {
2538
0
    Sema::TentativeAnalysisScope Trap(*this);
2539
0
    ExprResult R = BuildCallToMemberFunction(nullptr, &E, SourceLocation(),
2540
0
                                             std::nullopt, SourceLocation());
2541
0
    if (R.isUsable()) {
2542
0
      ZeroArgCallReturnTy = R.get()->getType();
2543
0
      return true;
2544
0
    }
2545
0
    return false;
2546
0
  }
2547
2548
0
  if (const auto *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
2549
0
    if (const auto *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
2550
0
      if (Fun->getMinRequiredArguments() == 0)
2551
0
        ZeroArgCallReturnTy = Fun->getReturnType();
2552
0
      return true;
2553
0
    }
2554
0
  }
2555
2556
  // We don't have an expression that's convenient to get a FunctionDecl from,
2557
  // but we can at least check if the type is "function of 0 arguments".
2558
0
  QualType ExprTy = E.getType();
2559
0
  const FunctionType *FunTy = nullptr;
2560
0
  QualType PointeeTy = ExprTy->getPointeeType();
2561
0
  if (!PointeeTy.isNull())
2562
0
    FunTy = PointeeTy->getAs<FunctionType>();
2563
0
  if (!FunTy)
2564
0
    FunTy = ExprTy->getAs<FunctionType>();
2565
2566
0
  if (const auto *FPT = dyn_cast_if_present<FunctionProtoType>(FunTy)) {
2567
0
    if (FPT->getNumParams() == 0)
2568
0
      ZeroArgCallReturnTy = FunTy->getReturnType();
2569
0
    return true;
2570
0
  }
2571
0
  return false;
2572
0
}
2573
2574
/// Give notes for a set of overloads.
2575
///
2576
/// A companion to tryExprAsCall. In cases when the name that the programmer
2577
/// wrote was an overloaded function, we may be able to make some guesses about
2578
/// plausible overloads based on their return types; such guesses can be handed
2579
/// off to this method to be emitted as notes.
2580
///
2581
/// \param Overloads - The overloads to note.
2582
/// \param FinalNoteLoc - If we've suppressed printing some overloads due to
2583
///  -fshow-overloads=best, this is the location to attach to the note about too
2584
///  many candidates. Typically this will be the location of the original
2585
///  ill-formed expression.
2586
static void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads,
2587
0
                          const SourceLocation FinalNoteLoc) {
2588
0
  unsigned ShownOverloads = 0;
2589
0
  unsigned SuppressedOverloads = 0;
2590
0
  for (UnresolvedSetImpl::iterator It = Overloads.begin(),
2591
0
       DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
2592
0
    if (ShownOverloads >= S.Diags.getNumOverloadCandidatesToShow()) {
2593
0
      ++SuppressedOverloads;
2594
0
      continue;
2595
0
    }
2596
2597
0
    const NamedDecl *Fn = (*It)->getUnderlyingDecl();
2598
    // Don't print overloads for non-default multiversioned functions.
2599
0
    if (const auto *FD = Fn->getAsFunction()) {
2600
0
      if (FD->isMultiVersion() && FD->hasAttr<TargetAttr>() &&
2601
0
          !FD->getAttr<TargetAttr>()->isDefaultVersion())
2602
0
        continue;
2603
0
      if (FD->isMultiVersion() && FD->hasAttr<TargetVersionAttr>() &&
2604
0
          !FD->getAttr<TargetVersionAttr>()->isDefaultVersion())
2605
0
        continue;
2606
0
    }
2607
0
    S.Diag(Fn->getLocation(), diag::note_possible_target_of_call);
2608
0
    ++ShownOverloads;
2609
0
  }
2610
2611
0
  S.Diags.overloadCandidatesShown(ShownOverloads);
2612
2613
0
  if (SuppressedOverloads)
2614
0
    S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates)
2615
0
      << SuppressedOverloads;
2616
0
}
2617
2618
static void notePlausibleOverloads(Sema &S, SourceLocation Loc,
2619
                                   const UnresolvedSetImpl &Overloads,
2620
0
                                   bool (*IsPlausibleResult)(QualType)) {
2621
0
  if (!IsPlausibleResult)
2622
0
    return noteOverloads(S, Overloads, Loc);
2623
2624
0
  UnresolvedSet<2> PlausibleOverloads;
2625
0
  for (OverloadExpr::decls_iterator It = Overloads.begin(),
2626
0
         DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
2627
0
    const auto *OverloadDecl = cast<FunctionDecl>(*It);
2628
0
    QualType OverloadResultTy = OverloadDecl->getReturnType();
2629
0
    if (IsPlausibleResult(OverloadResultTy))
2630
0
      PlausibleOverloads.addDecl(It.getDecl());
2631
0
  }
2632
0
  noteOverloads(S, PlausibleOverloads, Loc);
2633
0
}
2634
2635
/// Determine whether the given expression can be called by just
2636
/// putting parentheses after it.  Notably, expressions with unary
2637
/// operators can't be because the unary operator will start parsing
2638
/// outside the call.
2639
0
static bool IsCallableWithAppend(const Expr *E) {
2640
0
  E = E->IgnoreImplicit();
2641
0
  return (!isa<CStyleCastExpr>(E) &&
2642
0
          !isa<UnaryOperator>(E) &&
2643
0
          !isa<BinaryOperator>(E) &&
2644
0
          !isa<CXXOperatorCallExpr>(E));
2645
0
}
2646
2647
0
static bool IsCPUDispatchCPUSpecificMultiVersion(const Expr *E) {
2648
0
  if (const auto *UO = dyn_cast<UnaryOperator>(E))
2649
0
    E = UO->getSubExpr();
2650
2651
0
  if (const auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
2652
0
    if (ULE->getNumDecls() == 0)
2653
0
      return false;
2654
2655
0
    const NamedDecl *ND = *ULE->decls_begin();
2656
0
    if (const auto *FD = dyn_cast<FunctionDecl>(ND))
2657
0
      return FD->isCPUDispatchMultiVersion() || FD->isCPUSpecificMultiVersion();
2658
0
  }
2659
0
  return false;
2660
0
}
2661
2662
bool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD,
2663
                                bool ForceComplain,
2664
0
                                bool (*IsPlausibleResult)(QualType)) {
2665
0
  SourceLocation Loc = E.get()->getExprLoc();
2666
0
  SourceRange Range = E.get()->getSourceRange();
2667
0
  UnresolvedSet<4> Overloads;
2668
2669
  // If this is a SFINAE context, don't try anything that might trigger ADL
2670
  // prematurely.
2671
0
  if (!isSFINAEContext()) {
2672
0
    QualType ZeroArgCallTy;
2673
0
    if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) &&
2674
0
        !ZeroArgCallTy.isNull() &&
2675
0
        (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) {
2676
      // At this point, we know E is potentially callable with 0
2677
      // arguments and that it returns something of a reasonable type,
2678
      // so we can emit a fixit and carry on pretending that E was
2679
      // actually a CallExpr.
2680
0
      SourceLocation ParenInsertionLoc = getLocForEndOfToken(Range.getEnd());
2681
0
      bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get());
2682
0
      Diag(Loc, PD) << /*zero-arg*/ 1 << IsMV << Range
2683
0
                    << (IsCallableWithAppend(E.get())
2684
0
                            ? FixItHint::CreateInsertion(ParenInsertionLoc,
2685
0
                                                         "()")
2686
0
                            : FixItHint());
2687
0
      if (!IsMV)
2688
0
        notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
2689
2690
      // FIXME: Try this before emitting the fixit, and suppress diagnostics
2691
      // while doing so.
2692
0
      E = BuildCallExpr(nullptr, E.get(), Range.getEnd(), std::nullopt,
2693
0
                        Range.getEnd().getLocWithOffset(1));
2694
0
      return true;
2695
0
    }
2696
0
  }
2697
0
  if (!ForceComplain) return false;
2698
2699
0
  bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get());
2700
0
  Diag(Loc, PD) << /*not zero-arg*/ 0 << IsMV << Range;
2701
0
  if (!IsMV)
2702
0
    notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult);
2703
0
  E = ExprError();
2704
0
  return true;
2705
0
}
2706
2707
0
IdentifierInfo *Sema::getSuperIdentifier() const {
2708
0
  if (!Ident_super)
2709
0
    Ident_super = &Context.Idents.get("super");
2710
0
  return Ident_super;
2711
0
}
2712
2713
void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD,
2714
                                   CapturedRegionKind K,
2715
0
                                   unsigned OpenMPCaptureLevel) {
2716
0
  auto *CSI = new CapturedRegionScopeInfo(
2717
0
      getDiagnostics(), S, CD, RD, CD->getContextParam(), K,
2718
0
      (getLangOpts().OpenMP && K == CR_OpenMP) ? getOpenMPNestingLevel() : 0,
2719
0
      OpenMPCaptureLevel);
2720
0
  CSI->ReturnType = Context.VoidTy;
2721
0
  FunctionScopes.push_back(CSI);
2722
0
  CapturingFunctionScopes++;
2723
0
}
2724
2725
0
CapturedRegionScopeInfo *Sema::getCurCapturedRegion() {
2726
0
  if (FunctionScopes.empty())
2727
0
    return nullptr;
2728
2729
0
  return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back());
2730
0
}
2731
2732
const llvm::MapVector<FieldDecl *, Sema::DeleteLocs> &
2733
0
Sema::getMismatchingDeleteExpressions() const {
2734
0
  return DeleteExprs;
2735
0
}
2736
2737
Sema::FPFeaturesStateRAII::FPFeaturesStateRAII(Sema &S)
2738
    : S(S), OldFPFeaturesState(S.CurFPFeatures),
2739
      OldOverrides(S.FpPragmaStack.CurrentValue),
2740
      OldEvalMethod(S.PP.getCurrentFPEvalMethod()),
2741
1
      OldFPPragmaLocation(S.PP.getLastFPEvalPragmaLocation()) {}
2742
2743
1
Sema::FPFeaturesStateRAII::~FPFeaturesStateRAII() {
2744
1
  S.CurFPFeatures = OldFPFeaturesState;
2745
1
  S.FpPragmaStack.CurrentValue = OldOverrides;
2746
1
  S.PP.setCurrentFPEvalMethod(OldFPPragmaLocation, OldEvalMethod);
2747
1
}
2748
2749
0
bool Sema::isDeclaratorFunctionLike(Declarator &D) {
2750
0
  assert(D.getCXXScopeSpec().isSet() &&
2751
0
         "can only be called for qualified names");
2752
2753
0
  auto LR = LookupResult(*this, D.getIdentifier(), D.getBeginLoc(),
2754
0
                         LookupOrdinaryName, forRedeclarationInCurContext());
2755
0
  DeclContext *DC = computeDeclContext(D.getCXXScopeSpec(),
2756
0
                                       !D.getDeclSpec().isFriendSpecified());
2757
0
  if (!DC)
2758
0
    return false;
2759
2760
0
  LookupQualifiedName(LR, DC);
2761
0
  bool Result = std::all_of(LR.begin(), LR.end(), [](Decl *Dcl) {
2762
0
    if (NamedDecl *ND = dyn_cast<NamedDecl>(Dcl)) {
2763
0
      ND = ND->getUnderlyingDecl();
2764
0
      return isa<FunctionDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
2765
0
             isa<UsingDecl>(ND);
2766
0
    }
2767
0
    return false;
2768
0
  });
2769
0
  return Result;
2770
0
}