1 //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the actions class which performs semantic analysis and
10 // builds an AST out of a parse stream.
12 //===----------------------------------------------------------------------===//
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"
55 using namespace clang
;
58 SourceLocation
Sema::getLocForEndOfToken(SourceLocation Loc
, unsigned Offset
) {
59 return Lexer::getLocForEndOfToken(Loc
, Offset
, SourceMgr
, LangOpts
);
62 ModuleLoader
&Sema::getModuleLoader() const { return PP
.getModuleLoader(); }
65 Sema::getDarwinSDKInfoForAvailabilityChecking(SourceLocation Loc
,
67 auto *SDKInfo
= getDarwinSDKInfoForAvailabilityChecking();
68 if (!SDKInfo
&& !WarnedDarwinSDKInfoMissing
) {
69 Diag(Loc
, diag::warn_missing_sdksettings_for_availability_checking
)
71 WarnedDarwinSDKInfoMissing
= true;
76 DarwinSDKInfo
*Sema::getDarwinSDKInfoForAvailabilityChecking() {
77 if (CachedDarwinSDKInfo
)
78 return CachedDarwinSDKInfo
->get();
79 auto SDKInfo
= parseDarwinSDKInfo(
80 PP
.getFileManager().getVirtualFileSystem(),
81 PP
.getHeaderSearchInfo().getHeaderSearchOpts().Sysroot
);
82 if (SDKInfo
&& *SDKInfo
) {
83 CachedDarwinSDKInfo
= std::make_unique
<DarwinSDKInfo
>(std::move(**SDKInfo
));
84 return CachedDarwinSDKInfo
->get();
87 llvm::consumeError(SDKInfo
.takeError());
88 CachedDarwinSDKInfo
= std::unique_ptr
<DarwinSDKInfo
>();
93 Sema::InventAbbreviatedTemplateParameterTypeName(IdentifierInfo
*ParamName
,
95 std::string InventedName
;
96 llvm::raw_string_ostream
OS(InventedName
);
99 OS
<< "auto:" << Index
+ 1;
101 OS
<< ParamName
->getName() << ":auto";
104 return &Context
.Idents
.get(OS
.str());
107 PrintingPolicy
Sema::getPrintingPolicy(const ASTContext
&Context
,
108 const Preprocessor
&PP
) {
109 PrintingPolicy Policy
= Context
.getPrintingPolicy();
110 // In diagnostics, we print _Bool as bool if the latter is defined as the
112 Policy
.Bool
= Context
.getLangOpts().Bool
;
114 if (const MacroInfo
*BoolMacro
= PP
.getMacroInfo(Context
.getBoolName())) {
115 Policy
.Bool
= BoolMacro
->isObjectLike() &&
116 BoolMacro
->getNumTokens() == 1 &&
117 BoolMacro
->getReplacementToken(0).is(tok::kw__Bool
);
121 // Shorten the data output if needed
122 Policy
.EntireContentsOfLargeArray
= false;
127 void Sema::ActOnTranslationUnitScope(Scope
*S
) {
129 PushDeclContext(S
, Context
.getTranslationUnitDecl());
135 class SemaPPCallbacks
: public PPCallbacks
{
137 llvm::SmallVector
<SourceLocation
, 8> IncludeStack
;
140 void set(Sema
&S
) { this->S
= &S
; }
142 void reset() { S
= nullptr; }
144 void FileChanged(SourceLocation Loc
, FileChangeReason Reason
,
145 SrcMgr::CharacteristicKind FileType
,
146 FileID PrevFID
) override
{
151 SourceManager
&SM
= S
->getSourceManager();
152 SourceLocation IncludeLoc
= SM
.getIncludeLoc(SM
.getFileID(Loc
));
153 if (IncludeLoc
.isValid()) {
154 if (llvm::timeTraceProfilerEnabled()) {
155 OptionalFileEntryRef FE
= SM
.getFileEntryRefForID(SM
.getFileID(Loc
));
156 llvm::timeTraceProfilerBegin("Source", FE
? FE
->getName()
157 : StringRef("<unknown>"));
160 IncludeStack
.push_back(IncludeLoc
);
161 S
->DiagnoseNonDefaultPragmaAlignPack(
162 Sema::PragmaAlignPackDiagnoseKind::NonDefaultStateAtInclude
,
168 if (!IncludeStack
.empty()) {
169 if (llvm::timeTraceProfilerEnabled())
170 llvm::timeTraceProfilerEnd();
172 S
->DiagnoseNonDefaultPragmaAlignPack(
173 Sema::PragmaAlignPackDiagnoseKind::ChangedStateAtExit
,
174 IncludeStack
.pop_back_val());
183 } // end namespace sema
184 } // end namespace clang
186 const unsigned Sema::MaxAlignmentExponent
;
187 const uint64_t Sema::MaximumAlignment
;
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 CurScope(nullptr), Ident_super(nullptr) {
223 assert(pp
.TUKind
== TUKind
);
226 LoadedExternalKnownNamespaces
= false;
227 for (unsigned I
= 0; I
!= NSAPI::NumNSNumberLiteralMethods
; ++I
)
228 NSNumberLiteralMethods
[I
] = nullptr;
230 if (getLangOpts().ObjC
)
231 NSAPIObj
.reset(new NSAPI(Context
));
233 if (getLangOpts().CPlusPlus
)
234 FieldCollector
.reset(new CXXFieldCollector());
236 // Tell diagnostics how to render things from the AST library.
237 Diags
.SetArgToStringFn(&FormatASTNodeDiagnosticArgument
, &Context
);
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
242 ExprEvalContexts
.emplace_back(
243 ExpressionEvaluationContext::PotentiallyEvaluated
, 0, CleanupInfo
{},
244 nullptr, ExpressionEvaluationContextRecord::EK_Other
);
246 // Initialization of data sharing attributes stack for OpenMP
247 InitDataSharingAttributesStack();
249 std::unique_ptr
<sema::SemaPPCallbacks
> Callbacks
=
250 std::make_unique
<sema::SemaPPCallbacks
>();
251 SemaPPCallbackHandler
= Callbacks
.get();
252 PP
.addPPCallbacks(std::move(Callbacks
));
253 SemaPPCallbackHandler
->set(*this);
255 CurFPFeatures
.setFPEvalMethod(PP
.getCurrentFPEvalMethod());
258 // Anchor Sema's type info to this TU.
259 void Sema::anchor() {}
261 void Sema::addImplicitTypedef(StringRef Name
, QualType T
) {
262 DeclarationName DN
= &Context
.Idents
.get(Name
);
263 if (IdResolver
.begin(DN
) == IdResolver
.end())
264 PushOnScopeChains(Context
.buildImplicitTypedef(T
, Name
), TUScope
);
267 void Sema::Initialize() {
268 if (SemaConsumer
*SC
= dyn_cast
<SemaConsumer
>(&Consumer
))
269 SC
->InitializeSema(*this);
271 // Tell the external Sema source about this Sema object.
272 if (ExternalSemaSource
*ExternalSema
273 = dyn_cast_or_null
<ExternalSemaSource
>(Context
.getExternalSource()))
274 ExternalSema
->InitializeSema(*this);
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 VAListTagName
= PP
.getIdentifierInfo("__va_list_tag");
283 // Initialize predefined 128-bit integer types, if needed.
284 if (Context
.getTargetInfo().hasInt128Type() ||
285 (Context
.getAuxTargetInfo() &&
286 Context
.getAuxTargetInfo()->hasInt128Type())) {
287 // If either of the 128-bit integer types are unavailable to name lookup,
289 DeclarationName Int128
= &Context
.Idents
.get("__int128_t");
290 if (IdResolver
.begin(Int128
) == IdResolver
.end())
291 PushOnScopeChains(Context
.getInt128Decl(), TUScope
);
293 DeclarationName UInt128
= &Context
.Idents
.get("__uint128_t");
294 if (IdResolver
.begin(UInt128
) == IdResolver
.end())
295 PushOnScopeChains(Context
.getUInt128Decl(), TUScope
);
299 // Initialize predefined Objective-C types:
300 if (getLangOpts().ObjC
) {
301 // If 'SEL' does not yet refer to any declarations, make it refer to the
303 DeclarationName SEL
= &Context
.Idents
.get("SEL");
304 if (IdResolver
.begin(SEL
) == IdResolver
.end())
305 PushOnScopeChains(Context
.getObjCSelDecl(), TUScope
);
307 // If 'id' does not yet refer to any declarations, make it refer to the
309 DeclarationName Id
= &Context
.Idents
.get("id");
310 if (IdResolver
.begin(Id
) == IdResolver
.end())
311 PushOnScopeChains(Context
.getObjCIdDecl(), TUScope
);
313 // Create the built-in typedef for 'Class'.
314 DeclarationName Class
= &Context
.Idents
.get("Class");
315 if (IdResolver
.begin(Class
) == IdResolver
.end())
316 PushOnScopeChains(Context
.getObjCClassDecl(), TUScope
);
318 // Create the built-in forward declaratino for 'Protocol'.
319 DeclarationName Protocol
= &Context
.Idents
.get("Protocol");
320 if (IdResolver
.begin(Protocol
) == IdResolver
.end())
321 PushOnScopeChains(Context
.getObjCProtocolDecl(), TUScope
);
324 // Create the internal type for the *StringMakeConstantString builtins.
325 DeclarationName ConstantString
= &Context
.Idents
.get("__NSConstantString");
326 if (IdResolver
.begin(ConstantString
) == IdResolver
.end())
327 PushOnScopeChains(Context
.getCFConstantStringDecl(), TUScope
);
329 // Initialize Microsoft "predefined C++ types".
330 if (getLangOpts().MSVCCompat
) {
331 if (getLangOpts().CPlusPlus
&&
332 IdResolver
.begin(&Context
.Idents
.get("type_info")) == IdResolver
.end())
334 Context
.buildImplicitRecord("type_info", TagTypeKind::Class
),
337 addImplicitTypedef("size_t", Context
.getSizeType());
340 // Initialize predefined OpenCL types and supported extensions and (optional)
342 if (getLangOpts().OpenCL
) {
343 getOpenCLOptions().addSupport(
344 Context
.getTargetInfo().getSupportedOpenCLOpts(), getLangOpts());
345 addImplicitTypedef("sampler_t", Context
.OCLSamplerTy
);
346 addImplicitTypedef("event_t", Context
.OCLEventTy
);
347 auto OCLCompatibleVersion
= getLangOpts().getOpenCLCompatibleVersion();
348 if (OCLCompatibleVersion
>= 200) {
349 if (getLangOpts().OpenCLCPlusPlus
|| getLangOpts().Blocks
) {
350 addImplicitTypedef("clk_event_t", Context
.OCLClkEventTy
);
351 addImplicitTypedef("queue_t", Context
.OCLQueueTy
);
353 if (getLangOpts().OpenCLPipes
)
354 addImplicitTypedef("reserve_id_t", Context
.OCLReserveIDTy
);
355 addImplicitTypedef("atomic_int", Context
.getAtomicType(Context
.IntTy
));
356 addImplicitTypedef("atomic_uint",
357 Context
.getAtomicType(Context
.UnsignedIntTy
));
358 addImplicitTypedef("atomic_float",
359 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 addImplicitTypedef("atomic_flag", Context
.getAtomicType(Context
.IntTy
));
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.
377 auto AddPointerSizeDependentTypes
= [&]() {
378 auto AtomicSizeT
= Context
.getAtomicType(Context
.getSizeType());
379 auto AtomicIntPtrT
= Context
.getAtomicType(Context
.getIntPtrType());
380 auto AtomicUIntPtrT
= Context
.getAtomicType(Context
.getUIntPtrType());
381 auto AtomicPtrDiffT
=
382 Context
.getAtomicType(Context
.getPointerDiffType());
383 addImplicitTypedef("atomic_size_t", AtomicSizeT
);
384 addImplicitTypedef("atomic_intptr_t", AtomicIntPtrT
);
385 addImplicitTypedef("atomic_uintptr_t", AtomicUIntPtrT
);
386 addImplicitTypedef("atomic_ptrdiff_t", AtomicPtrDiffT
);
389 if (Context
.getTypeSize(Context
.getSizeType()) == 32) {
390 AddPointerSizeDependentTypes();
393 if (getOpenCLOptions().isSupported("cl_khr_fp16", getLangOpts())) {
394 auto AtomicHalfT
= Context
.getAtomicType(Context
.HalfTy
);
395 addImplicitTypedef("atomic_half", AtomicHalfT
);
398 std::vector
<QualType
> Atomic64BitTypes
;
399 if (getOpenCLOptions().isSupported("cl_khr_int64_base_atomics",
401 getOpenCLOptions().isSupported("cl_khr_int64_extended_atomics",
403 if (getOpenCLOptions().isSupported("cl_khr_fp64", getLangOpts())) {
404 auto AtomicDoubleT
= Context
.getAtomicType(Context
.DoubleTy
);
405 addImplicitTypedef("atomic_double", AtomicDoubleT
);
406 Atomic64BitTypes
.push_back(AtomicDoubleT
);
408 auto AtomicLongT
= Context
.getAtomicType(Context
.LongTy
);
409 auto AtomicULongT
= Context
.getAtomicType(Context
.UnsignedLongTy
);
410 addImplicitTypedef("atomic_long", AtomicLongT
);
411 addImplicitTypedef("atomic_ulong", AtomicULongT
);
414 if (Context
.getTypeSize(Context
.getSizeType()) == 64) {
415 AddPointerSizeDependentTypes();
420 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
421 if (getOpenCLOptions().isSupported(#Ext, getLangOpts())) { \
422 addImplicitTypedef(#ExtType, Context.Id##Ty); \
424 #include "clang/Basic/OpenCLExtensionTypes.def"
427 if (Context
.getTargetInfo().hasAArch64SVETypes()) {
428 #define SVE_TYPE(Name, Id, SingletonId) \
429 addImplicitTypedef(Name, Context.SingletonId);
430 #include "clang/Basic/AArch64SVEACLETypes.def"
433 if (Context
.getTargetInfo().getTriple().isPPC64()) {
434 #define PPC_VECTOR_MMA_TYPE(Name, Id, Size) \
435 addImplicitTypedef(#Name, Context.Id##Ty);
436 #include "clang/Basic/PPCTypes.def"
437 #define PPC_VECTOR_VSX_TYPE(Name, Id, Size) \
438 addImplicitTypedef(#Name, Context.Id##Ty);
439 #include "clang/Basic/PPCTypes.def"
442 if (Context
.getTargetInfo().hasRISCVVTypes()) {
443 #define RVV_TYPE(Name, Id, SingletonId) \
444 addImplicitTypedef(Name, Context.SingletonId);
445 #include "clang/Basic/RISCVVTypes.def"
448 if (Context
.getTargetInfo().getTriple().isWasm() &&
449 Context
.getTargetInfo().hasFeature("reference-types")) {
450 #define WASM_TYPE(Name, Id, SingletonId) \
451 addImplicitTypedef(Name, Context.SingletonId);
452 #include "clang/Basic/WebAssemblyReferenceTypes.def"
455 if (Context
.getTargetInfo().hasBuiltinMSVaList()) {
456 DeclarationName MSVaList
= &Context
.Idents
.get("__builtin_ms_va_list");
457 if (IdResolver
.begin(MSVaList
) == IdResolver
.end())
458 PushOnScopeChains(Context
.getBuiltinMSVaListDecl(), TUScope
);
461 DeclarationName BuiltinVaList
= &Context
.Idents
.get("__builtin_va_list");
462 if (IdResolver
.begin(BuiltinVaList
) == IdResolver
.end())
463 PushOnScopeChains(Context
.getBuiltinVaListDecl(), TUScope
);
467 assert(InstantiatingSpecializations
.empty() &&
468 "failed to clean up an InstantiatingTemplate?");
470 if (VisContext
) FreeVisContext();
472 // Kill all the active scopes.
473 for (sema::FunctionScopeInfo
*FSI
: FunctionScopes
)
476 // Tell the SemaConsumer to forget about us; we're going out of scope.
477 if (SemaConsumer
*SC
= dyn_cast
<SemaConsumer
>(&Consumer
))
480 // Detach from the external Sema source.
481 if (ExternalSemaSource
*ExternalSema
482 = dyn_cast_or_null
<ExternalSemaSource
>(Context
.getExternalSource()))
483 ExternalSema
->ForgetSema();
485 // Delete cached satisfactions.
486 std::vector
<ConstraintSatisfaction
*> Satisfactions
;
487 Satisfactions
.reserve(SatisfactionCache
.size());
488 for (auto &Node
: SatisfactionCache
)
489 Satisfactions
.push_back(&Node
);
490 for (auto *Node
: Satisfactions
)
493 threadSafety::threadSafetyCleanup(ThreadSafetyDeclCache
);
495 // Destroys data sharing attributes stack for OpenMP
496 DestroyDataSharingAttributesStack();
498 // Detach from the PP callback handler which outlives Sema since it's owned
499 // by the preprocessor.
500 SemaPPCallbackHandler
->reset();
503 void Sema::warnStackExhausted(SourceLocation Loc
) {
504 // Only warn about this once.
505 if (!WarnedStackExhausted
) {
506 Diag(Loc
, diag::warn_stack_exhausted
);
507 WarnedStackExhausted
= true;
511 void Sema::runWithSufficientStackSpace(SourceLocation Loc
,
512 llvm::function_ref
<void()> Fn
) {
513 clang::runWithSufficientStackSpace([&] { warnStackExhausted(Loc
); }, Fn
);
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 UnavailableAttr::ImplicitReason reason
) {
522 // If we're not in a function, it's an error.
523 FunctionDecl
*fn
= dyn_cast
<FunctionDecl
>(CurContext
);
524 if (!fn
) return false;
526 // If we're in template instantiation, it's an error.
527 if (inTemplateInstantiation())
530 // If that function's not in a system header, it's an error.
531 if (!Context
.getSourceManager().isInSystemHeader(loc
))
534 // If the function is already unavailable, it's not an error.
535 if (fn
->hasAttr
<UnavailableAttr
>()) return true;
537 fn
->addAttr(UnavailableAttr::CreateImplicit(Context
, "", reason
, loc
));
541 ASTMutationListener
*Sema::getASTMutationListener() const {
542 return getASTConsumer().GetASTMutationListener();
545 ///Registers an external source. If an external source already exists,
546 /// creates a multiplex external source and appends to it.
548 ///\param[in] E - A non-null external sema source.
550 void Sema::addExternalSource(ExternalSemaSource
*E
) {
551 assert(E
&& "Cannot use with NULL ptr");
553 if (!ExternalSource
) {
558 if (auto *Ex
= dyn_cast
<MultiplexExternalSemaSource
>(ExternalSource
))
561 ExternalSource
= new MultiplexExternalSemaSource(ExternalSource
.get(), E
);
564 /// Print out statistics about the semantic analysis.
565 void Sema::PrintStats() const {
566 llvm::errs() << "\n*** Semantic Analysis Stats:\n";
567 llvm::errs() << NumSFINAEErrors
<< " SFINAE diagnostics trapped.\n";
569 BumpAlloc
.PrintStats();
570 AnalysisWarnings
.PrintStats();
573 void Sema::diagnoseNullableToNonnullConversion(QualType DstType
,
575 SourceLocation Loc
) {
576 std::optional
<NullabilityKind
> ExprNullability
= SrcType
->getNullability();
577 if (!ExprNullability
|| (*ExprNullability
!= NullabilityKind::Nullable
&&
578 *ExprNullability
!= NullabilityKind::NullableResult
))
581 std::optional
<NullabilityKind
> TypeNullability
= DstType
->getNullability();
582 if (!TypeNullability
|| *TypeNullability
!= NullabilityKind::NonNull
)
585 Diag(Loc
, diag::warn_nullability_lost
) << SrcType
<< DstType
;
588 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 if (!getLangOpts().CPlusPlus11
)
593 if (Kind
!= CK_NullToPointer
&& Kind
!= CK_NullToMemberPointer
)
596 const Expr
*EStripped
= E
->IgnoreParenImpCasts();
597 if (EStripped
->getType()->isNullPtrType())
599 if (isa
<GNUNullExpr
>(EStripped
))
602 if (Diags
.isIgnored(diag::warn_zero_as_null_pointer_constant
,
606 // Don't diagnose the conversion from a 0 literal to a null pointer argument
607 // in a synthesized call to operator<=>.
608 if (!CodeSynthesisContexts
.empty() &&
609 CodeSynthesisContexts
.back().Kind
==
610 CodeSynthesisContext::RewritingOperatorAsSpaceship
)
613 // Ignore null pointers in defaulted comparison operators.
614 FunctionDecl
*FD
= getCurFunctionDecl();
615 if (FD
&& FD
->isDefaulted()) {
619 // If it is a macro from system header, and if the macro name is not "NULL",
621 // Note that uses of "NULL" will be ignored above on systems that define it
623 SourceLocation MaybeMacroLoc
= E
->getBeginLoc();
624 if (Diags
.getSuppressSystemWarnings() &&
625 SourceMgr
.isInSystemMacro(MaybeMacroLoc
) &&
626 !findMacroSpelling(MaybeMacroLoc
, "NULL"))
629 Diag(E
->getBeginLoc(), diag::warn_zero_as_null_pointer_constant
)
630 << FixItHint::CreateReplacement(E
->getSourceRange(), "nullptr");
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 CheckedConversionKind CCK
) {
641 if (VK
== VK_PRValue
&& !E
->isPRValue()) {
645 ("can't implicitly cast glvalue to prvalue with this cast "
647 std::string(CastExpr::getCastKindName(Kind
)))
650 case CK_LValueToRValue
:
651 case CK_ArrayToPointerDecay
:
652 case CK_FunctionToPointerDecay
:
654 case CK_NonAtomicToAtomic
:
658 assert((VK
== VK_PRValue
|| Kind
== CK_Dependent
|| !E
->isPRValue()) &&
659 "can't cast prvalue to glvalue");
662 diagnoseNullableToNonnullConversion(Ty
, E
->getType(), E
->getBeginLoc());
663 diagnoseZeroToNullptrConversion(Kind
, E
);
665 QualType ExprTy
= Context
.getCanonicalType(E
->getType());
666 QualType TypeTy
= Context
.getCanonicalType(Ty
);
668 if (ExprTy
== TypeTy
)
671 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 if (getLangOpts().CPlusPlus
&& E
->isPRValue()) {
675 // The temporary is an lvalue in C++98 and an xvalue otherwise.
676 ExprResult Materialized
= CreateMaterializeTemporaryExpr(
677 E
->getType(), E
, !getLangOpts().CPlusPlus11
);
678 if (Materialized
.isInvalid())
680 E
= Materialized
.get();
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 if (VK
== VK_PRValue
&& !getLangOpts().CPlusPlus
&& !E
->isPRValue()) {
691 if (const auto *DRE
= dyn_cast
<DeclRefExpr
>(E
)) {
692 if (const auto *VD
= dyn_cast
<VarDecl
>(DRE
->getDecl())) {
693 if (VD
->getStorageClass() == SC_Register
) {
694 Diag(E
->getExprLoc(), diag::err_typecheck_address_of
)
695 << /*register variable*/ 3 << E
->getSourceRange();
703 if (ImplicitCastExpr
*ImpCast
= dyn_cast
<ImplicitCastExpr
>(E
)) {
704 if (ImpCast
->getCastKind() == Kind
&& (!BasePath
|| BasePath
->empty())) {
705 ImpCast
->setType(Ty
);
706 ImpCast
->setValueKind(VK
);
711 return ImplicitCastExpr::Create(Context
, Ty
, Kind
, E
, BasePath
, VK
,
712 CurFPFeatureOverrides());
715 /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding
716 /// to the conversion from scalar type ScalarTy to the Boolean type.
717 CastKind
Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy
) {
718 switch (ScalarTy
->getScalarTypeKind()) {
719 case Type::STK_Bool
: return CK_NoOp
;
720 case Type::STK_CPointer
: return CK_PointerToBoolean
;
721 case Type::STK_BlockPointer
: return CK_PointerToBoolean
;
722 case Type::STK_ObjCObjectPointer
: return CK_PointerToBoolean
;
723 case Type::STK_MemberPointer
: return CK_MemberPointerToBoolean
;
724 case Type::STK_Integral
: return CK_IntegralToBoolean
;
725 case Type::STK_Floating
: return CK_FloatingToBoolean
;
726 case Type::STK_IntegralComplex
: return CK_IntegralComplexToBoolean
;
727 case Type::STK_FloatingComplex
: return CK_FloatingComplexToBoolean
;
728 case Type::STK_FixedPoint
: return CK_FixedPointToBoolean
;
730 llvm_unreachable("unknown scalar type kind");
733 /// Used to prune the decls of Sema's UnusedFileScopedDecls vector.
734 static bool ShouldRemoveFromUnused(Sema
*SemaRef
, const DeclaratorDecl
*D
) {
735 if (D
->getMostRecentDecl()->isUsed())
738 if (D
->isExternallyVisible())
741 if (const FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(D
)) {
742 // If this is a function template and none of its specializations is used,
744 if (FunctionTemplateDecl
*Template
= FD
->getDescribedFunctionTemplate())
745 for (const auto *Spec
: Template
->specializations())
746 if (ShouldRemoveFromUnused(SemaRef
, Spec
))
749 // UnusedFileScopedDecls stores the first declaration.
750 // The declaration may have become definition so check again.
751 const FunctionDecl
*DeclToCheck
;
752 if (FD
->hasBody(DeclToCheck
))
753 return !SemaRef
->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck
);
755 // Later redecls may add new information resulting in not having to warn,
757 DeclToCheck
= FD
->getMostRecentDecl();
758 if (DeclToCheck
!= FD
)
759 return !SemaRef
->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck
);
762 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 if (VD
->isReferenced() &&
769 VD
->mightBeUsableInConstantExpressions(SemaRef
->Context
))
772 if (VarTemplateDecl
*Template
= VD
->getDescribedVarTemplate())
773 // If this is a variable template and none of its specializations is used,
775 for (const auto *Spec
: Template
->specializations())
776 if (ShouldRemoveFromUnused(SemaRef
, Spec
))
779 // UnusedFileScopedDecls stores the first declaration.
780 // The declaration may have become definition so check again.
781 const VarDecl
*DeclToCheck
= VD
->getDefinition();
783 return !SemaRef
->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck
);
785 // Later redecls may add new information resulting in not having to warn,
787 DeclToCheck
= VD
->getMostRecentDecl();
788 if (DeclToCheck
!= VD
)
789 return !SemaRef
->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck
);
795 static bool isFunctionOrVarDeclExternC(const NamedDecl
*ND
) {
796 if (const auto *FD
= dyn_cast
<FunctionDecl
>(ND
))
797 return FD
->isExternC();
798 return cast
<VarDecl
>(ND
)->isExternC();
801 /// Determine whether ND is an external-linkage function or variable whose
802 /// type has no linkage.
803 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 return getLangOpts().CPlusPlus
&& VD
->hasExternalFormalLinkage() &&
808 !isExternalFormalLinkage(VD
->getType()->getLinkage()) &&
809 !isFunctionOrVarDeclExternC(VD
);
812 /// Obtains a sorted list of functions and variables that are undefined but
814 void Sema::getUndefinedButUsed(
815 SmallVectorImpl
<std::pair
<NamedDecl
*, SourceLocation
> > &Undefined
) {
816 for (const auto &UndefinedUse
: UndefinedButUsed
) {
817 NamedDecl
*ND
= UndefinedUse
.first
;
819 // Ignore attributes that have become invalid.
820 if (ND
->isInvalidDecl()) continue;
822 // __attribute__((weakref)) is basically a definition.
823 if (ND
->hasAttr
<WeakRefAttr
>()) continue;
825 if (isa
<CXXDeductionGuideDecl
>(ND
))
828 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.
835 if (const auto *FD
= dyn_cast
<FunctionDecl
>(ND
)) {
838 if (FD
->isExternallyVisible() &&
839 !isExternalWithNoLinkageType(FD
) &&
840 !FD
->getMostRecentDecl()->isInlined() &&
841 !FD
->hasAttr
<ExcludeFromExplicitInstantiationAttr
>())
843 if (FD
->getBuiltinID())
846 const auto *VD
= cast
<VarDecl
>(ND
);
847 if (VD
->hasDefinition() != VarDecl::DeclarationOnly
)
849 if (VD
->isExternallyVisible() &&
850 !isExternalWithNoLinkageType(VD
) &&
851 !VD
->getMostRecentDecl()->isInline() &&
852 !VD
->hasAttr
<ExcludeFromExplicitInstantiationAttr
>())
855 // Skip VarDecls that lack formal definitions but which we know are in
856 // fact defined somewhere.
857 if (VD
->isKnownToBeDefined())
861 Undefined
.push_back(std::make_pair(ND
, UndefinedUse
.second
));
865 /// checkUndefinedButUsed - Check for undefined objects with internal linkage
866 /// or that are inline.
867 static void checkUndefinedButUsed(Sema
&S
) {
868 if (S
.UndefinedButUsed
.empty()) return;
870 // Collect all the still-undefined entities with internal linkage.
871 SmallVector
<std::pair
<NamedDecl
*, SourceLocation
>, 16> Undefined
;
872 S
.getUndefinedButUsed(Undefined
);
873 S
.UndefinedButUsed
.clear();
874 if (Undefined
.empty()) return;
876 for (const auto &Undef
: Undefined
) {
877 ValueDecl
*VD
= cast
<ValueDecl
>(Undef
.first
);
878 SourceLocation UseLoc
= Undef
.second
;
880 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
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 S
.Diag(VD
->getLocation(), isExternallyVisible(VD
->getType()->getLinkage())
891 ? diag::ext_undefined_internal_type
892 : diag::err_undefined_internal_type
)
893 << isa
<VarDecl
>(VD
) << VD
;
894 } 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 bool IsImplicitBase
= false;
899 if (const auto *BaseD
= dyn_cast
<FunctionDecl
>(VD
)) {
900 auto *DVAttr
= BaseD
->getAttr
<OMPDeclareVariantAttr
>();
901 if (DVAttr
&& !DVAttr
->getTraitInfo().isExtensionActive(
902 llvm::omp::TraitProperty::
903 implementation_extension_disable_implicit_base
)) {
904 const auto *Func
= cast
<FunctionDecl
>(
905 cast
<DeclRefExpr
>(DVAttr
->getVariantFuncRef())->getDecl());
906 IsImplicitBase
= BaseD
->isImplicit() &&
907 Func
->getIdentifier()->isMangledOpenMPVariantName();
910 if (!S
.getLangOpts().OpenMP
|| !IsImplicitBase
)
911 S
.Diag(VD
->getLocation(), diag::warn_undefined_internal
)
912 << isa
<VarDecl
>(VD
) << VD
;
913 } else if (auto *FD
= dyn_cast
<FunctionDecl
>(VD
)) {
915 assert(FD
->getMostRecentDecl()->isInlined() &&
916 "used object requires definition but isn't inline or internal?");
917 // FIXME: This is ill-formed; we should reject.
918 S
.Diag(VD
->getLocation(), diag::warn_undefined_inline
) << VD
;
920 assert(cast
<VarDecl
>(VD
)->getMostRecentDecl()->isInline() &&
921 "used var requires definition but isn't inline or internal?");
922 S
.Diag(VD
->getLocation(), diag::err_undefined_inline_var
) << VD
;
924 if (UseLoc
.isValid())
925 S
.Diag(UseLoc
, diag::note_used_here
);
929 void Sema::LoadExternalWeakUndeclaredIdentifiers() {
933 SmallVector
<std::pair
<IdentifierInfo
*, WeakInfo
>, 4> WeakIDs
;
934 ExternalSource
->ReadWeakUndeclaredIdentifiers(WeakIDs
);
935 for (auto &WeakID
: WeakIDs
)
936 (void)WeakUndeclaredIdentifiers
[WeakID
.first
].insert(WeakID
.second
);
940 typedef llvm::DenseMap
<const CXXRecordDecl
*, bool> RecordCompleteMap
;
942 /// Returns true, if all methods and nested classes of the given
943 /// CXXRecordDecl are defined in this translation unit.
945 /// Should only be called from ActOnEndOfTranslationUnit so that all
946 /// definitions are actually read.
947 static bool MethodsAndNestedClassesComplete(const CXXRecordDecl
*RD
,
948 RecordCompleteMap
&MNCComplete
) {
949 RecordCompleteMap::iterator Cache
= MNCComplete
.find(RD
);
950 if (Cache
!= MNCComplete
.end())
951 return Cache
->second
;
952 if (!RD
->isCompleteDefinition())
954 bool Complete
= true;
955 for (DeclContext::decl_iterator I
= RD
->decls_begin(),
957 I
!= E
&& Complete
; ++I
) {
958 if (const CXXMethodDecl
*M
= dyn_cast
<CXXMethodDecl
>(*I
))
959 Complete
= M
->isDefined() || M
->isDefaulted() ||
960 (M
->isPure() && !isa
<CXXDestructorDecl
>(M
));
961 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 Complete
= !F
->getTemplatedDecl()->isLateTemplateParsed() &&
967 F
->getTemplatedDecl()->isDefined();
968 else if (const CXXRecordDecl
*R
= dyn_cast
<CXXRecordDecl
>(*I
)) {
969 if (R
->isInjectedClassName())
971 if (R
->hasDefinition())
972 Complete
= MethodsAndNestedClassesComplete(R
->getDefinition(),
978 MNCComplete
[RD
] = Complete
;
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.
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 RecordCompleteMap
&MNCComplete
) {
992 RecordCompleteMap::iterator Cache
= RecordsComplete
.find(RD
);
993 if (Cache
!= RecordsComplete
.end())
994 return Cache
->second
;
995 bool Complete
= MethodsAndNestedClassesComplete(RD
, MNCComplete
);
996 for (CXXRecordDecl::friend_iterator I
= RD
->friend_begin(),
997 E
= RD
->friend_end();
998 I
!= E
&& Complete
; ++I
) {
999 // Check if friend classes and methods are complete.
1000 if (TypeSourceInfo
*TSI
= (*I
)->getFriendType()) {
1001 // Friend classes are available as the TypeSourceInfo of the FriendDecl.
1002 if (CXXRecordDecl
*FriendD
= TSI
->getType()->getAsCXXRecordDecl())
1003 Complete
= MethodsAndNestedClassesComplete(FriendD
, MNCComplete
);
1007 // Friend functions are available through the NamedDecl of FriendDecl.
1008 if (const FunctionDecl
*FD
=
1009 dyn_cast
<FunctionDecl
>((*I
)->getFriendDecl()))
1010 Complete
= FD
->isDefined();
1012 // This is a template friend, give up.
1016 RecordsComplete
[RD
] = Complete
;
1020 void Sema::emitAndClearUnusedLocalTypedefWarnings() {
1022 ExternalSource
->ReadUnusedLocalTypedefNameCandidates(
1023 UnusedLocalTypedefNameCandidates
);
1024 for (const TypedefNameDecl
*TD
: UnusedLocalTypedefNameCandidates
) {
1025 if (TD
->isReferenced())
1027 Diag(TD
->getLocation(), diag::warn_unused_local_typedef
)
1028 << isa
<TypeAliasDecl
>(TD
) << TD
->getDeclName();
1030 UnusedLocalTypedefNameCandidates
.clear();
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
1036 void Sema::ActOnStartOfTranslationUnit() {
1037 if (getLangOpts().CPlusPlusModules
&&
1038 getLangOpts().getCompilingModule() == LangOptions::CMK_HeaderUnit
)
1039 HandleStartOfHeaderUnit();
1042 void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind
) {
1043 // No explicit actions are required at the end of the global module fragment.
1044 if (Kind
== TUFragmentKind::Global
)
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.
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
1056 PendingInstantiations
.insert(PendingInstantiations
.end(),
1057 LateParsedInstantiations
.begin(),
1058 LateParsedInstantiations
.end());
1059 LateParsedInstantiations
.clear();
1061 // If DefinedUsedVTables ends up marking any virtual member functions it
1062 // might lead to more pending template instantiations, which we then need
1064 DefineUsedVTables();
1066 // C++: Perform implicit template instantiations.
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 if (ExternalSource
) {
1076 // Load pending instantiations from the external source.
1077 SmallVector
<PendingImplicitInstantiation
, 4> Pending
;
1078 ExternalSource
->ReadPendingInstantiations(Pending
);
1079 for (auto PII
: Pending
)
1080 if (auto Func
= dyn_cast
<FunctionDecl
>(PII
.first
))
1081 Func
->setInstantiationIsPending(true);
1082 PendingInstantiations
.insert(PendingInstantiations
.begin(),
1083 Pending
.begin(), Pending
.end());
1087 llvm::TimeTraceScope
TimeScope("PerformPendingInstantiations");
1088 PerformPendingInstantiations();
1091 emitDeferredDiags();
1093 assert(LateParsedInstantiations
.empty() &&
1094 "end of TU template instantiation should not create more "
1095 "late-parsed templates");
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 for (const auto &Typo
: DelayedTypos
) {
1101 // We pass an empty TypoCorrection to indicate no correction was performed.
1102 Typo
.second
.DiagHandler(TypoCorrection());
1104 DelayedTypos
.clear();
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
1110 void Sema::ActOnEndOfTranslationUnit() {
1111 assert(DelayedDiagnostics
.getCurrentPool() == nullptr
1112 && "reached end of translation unit with a pool attached?");
1114 // If code completion is enabled, don't perform any end-of-translation-unit
1116 if (PP
.isCodeCompletionEnabled())
1119 // Complete translation units and modules define vtables and perform implicit
1120 // instantiations. PCH files do not.
1121 if (TUKind
!= TU_Prefix
) {
1122 DiagnoseUseOfUnimplementedSelectors();
1124 ActOnEndOfTranslationUnitFragment(
1125 !ModuleScopes
.empty() && ModuleScopes
.back().Module
->Kind
==
1126 Module::PrivateModuleFragment
1127 ? TUFragmentKind::Private
1128 : TUFragmentKind::Normal
);
1130 if (LateTemplateParserCleanup
)
1131 LateTemplateParserCleanup(OpaqueParser
);
1133 CheckDelayedMemberExceptionSpecs();
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
1140 PendingInstantiations
.insert(PendingInstantiations
.end(),
1141 LateParsedInstantiations
.begin(),
1142 LateParsedInstantiations
.end());
1143 LateParsedInstantiations
.clear();
1145 if (LangOpts
.PCHInstantiateTemplates
) {
1146 llvm::TimeTraceScope
TimeScope("PerformPendingInstantiations");
1147 PerformPendingInstantiations();
1151 DiagnoseUnterminatedPragmaAlignPack();
1152 DiagnoseUnterminatedPragmaAttribute();
1153 DiagnoseUnterminatedOpenMPDeclareTarget();
1155 // All delayed member exception specs should be checked or we end up accepting
1156 // incompatible declarations.
1157 assert(DelayedOverridingExceptionSpecChecks
.empty());
1158 assert(DelayedEquivalentExceptionSpecChecks
.empty());
1160 // All dllexport classes should have been processed already.
1161 assert(DelayedDllExportClasses
.empty());
1162 assert(DelayedDllExportMemberFunctions
.empty());
1164 // Remove file scoped decls that turned out to be used.
1165 UnusedFileScopedDecls
.erase(
1166 std::remove_if(UnusedFileScopedDecls
.begin(nullptr, true),
1167 UnusedFileScopedDecls
.end(),
1168 [this](const DeclaratorDecl
*DD
) {
1169 return ShouldRemoveFromUnused(this, DD
);
1171 UnusedFileScopedDecls
.end());
1173 if (TUKind
== TU_Prefix
) {
1174 // Translation unit prefixes don't need any of the checking below.
1175 if (!PP
.isIncrementalProcessingEnabled())
1180 // Check for #pragma weak identifiers that were never declared
1181 LoadExternalWeakUndeclaredIdentifiers();
1182 for (const auto &WeakIDs
: WeakUndeclaredIdentifiers
) {
1183 if (WeakIDs
.second
.empty())
1186 Decl
*PrevDecl
= LookupSingleName(TUScope
, WeakIDs
.first
, SourceLocation(),
1187 LookupOrdinaryName
);
1188 if (PrevDecl
!= nullptr &&
1189 !(isa
<FunctionDecl
>(PrevDecl
) || isa
<VarDecl
>(PrevDecl
)))
1190 for (const auto &WI
: WeakIDs
.second
)
1191 Diag(WI
.getLocation(), diag::warn_attribute_wrong_decl_type
)
1192 << "'weak'" << /*isRegularKeyword=*/0 << ExpectedVariableOrFunction
;
1194 for (const auto &WI
: WeakIDs
.second
)
1195 Diag(WI
.getLocation(), diag::warn_weak_identifier_undeclared
)
1199 if (LangOpts
.CPlusPlus11
&&
1200 !Diags
.isIgnored(diag::warn_delegating_ctor_cycle
, SourceLocation()))
1201 CheckDelegatingCtorCycles();
1203 if (!Diags
.hasErrorOccurred()) {
1205 ExternalSource
->ReadUndefinedButUsed(UndefinedButUsed
);
1206 checkUndefinedButUsed(*this);
1209 // A global-module-fragment is only permitted within a module unit.
1210 bool DiagnosedMissingModuleDeclaration
= false;
1211 if (!ModuleScopes
.empty() && ModuleScopes
.back().Module
->Kind
==
1212 Module::ExplicitGlobalModuleFragment
) {
1213 Diag(ModuleScopes
.back().BeginLoc
,
1214 diag::err_module_declaration_missing_after_global_module_introducer
);
1215 DiagnosedMissingModuleDeclaration
= true;
1218 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 if (getLangOpts().getCompilingModule() ==
1222 LangOptions::CMK_ModuleInterface
&&
1223 !isCurrentModulePurview() && !DiagnosedMissingModuleDeclaration
) {
1224 // FIXME: Make a better guess as to where to put the module declaration.
1225 Diag(getSourceManager().getLocForStartOfFile(
1226 getSourceManager().getMainFileID()),
1227 diag::err_module_declaration_missing
);
1230 // If we are building a module, resolve all of the exported declarations
1232 if (Module
*CurrentModule
= PP
.getCurrentModule()) {
1233 ModuleMap
&ModMap
= PP
.getHeaderSearchInfo().getModuleMap();
1235 SmallVector
<Module
*, 2> Stack
;
1236 Stack
.push_back(CurrentModule
);
1237 while (!Stack
.empty()) {
1238 Module
*Mod
= Stack
.pop_back_val();
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
1244 ModMap
.resolveExports(Mod
, /*Complain=*/false);
1245 ModMap
.resolveUses(Mod
, /*Complain=*/false);
1246 ModMap
.resolveConflicts(Mod
, /*Complain=*/false);
1248 // Queue the submodules, so their exports will also be resolved.
1249 auto SubmodulesRange
= Mod
->submodules();
1250 Stack
.append(SubmodulesRange
.begin(), SubmodulesRange
.end());
1254 // Now we can decide whether the modules we're building need an initializer.
1255 if (Module
*CurrentModule
= getCurrentModule();
1256 CurrentModule
&& CurrentModule
->isInterfaceOrPartition()) {
1257 auto DoesModNeedInit
= [this](Module
*M
) {
1258 if (!getASTContext().getModuleInitializers(M
).empty())
1260 for (auto [Exported
, _
] : M
->Exports
)
1261 if (Exported
->isNamedModuleInterfaceHasInit())
1263 for (Module
*I
: M
->Imports
)
1264 if (I
->isNamedModuleInterfaceHasInit())
1270 CurrentModule
->NamedModuleHasInit
=
1271 DoesModNeedInit(CurrentModule
) ||
1272 llvm::any_of(CurrentModule
->submodules(),
1273 [&](auto *SubM
) { return DoesModNeedInit(SubM
); });
1276 // Warnings emitted in ActOnEndOfTranslationUnit() should be emitted for
1277 // modules when they are built, not every time they are used.
1278 emitAndClearUnusedLocalTypedefWarnings();
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 if (!PendingInlineFuncDecls
.empty()) {
1285 for (auto *D
: PendingInlineFuncDecls
) {
1286 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
1287 bool DefInPMF
= false;
1288 if (auto *FDD
= FD
->getDefinition()) {
1289 DefInPMF
= FDD
->getOwningModule()->isPrivateModule();
1293 Diag(FD
->getLocation(), diag::err_export_inline_not_defined
)
1295 // If we have a PMF it should be at the end of the ModuleScopes.
1297 ModuleScopes
.back().Module
->Kind
== Module::PrivateModuleFragment
) {
1298 Diag(ModuleScopes
.back().BeginLoc
,
1299 diag::note_private_module_fragment
);
1303 PendingInlineFuncDecls
.clear();
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 llvm::SmallSet
<VarDecl
*, 32> Seen
;
1318 for (TentativeDefinitionsType::iterator
1319 T
= TentativeDefinitions
.begin(ExternalSource
.get()),
1320 TEnd
= TentativeDefinitions
.end();
1322 VarDecl
*VD
= (*T
)->getActingDefinition();
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 if (!VD
|| VD
->isInvalidDecl() || !Seen
.insert(VD
).second
)
1330 if (const IncompleteArrayType
*ArrayT
1331 = Context
.getAsIncompleteArrayType(VD
->getType())) {
1332 // Set the length of the array to 1 (C99 6.9.2p5).
1333 Diag(VD
->getLocation(), diag::warn_tentative_incomplete_array
);
1334 llvm::APInt
One(Context
.getTypeSize(Context
.getSizeType()), true);
1335 QualType T
= Context
.getConstantArrayType(
1336 ArrayT
->getElementType(), One
, nullptr, ArraySizeModifier::Normal
, 0);
1338 } else if (RequireCompleteType(VD
->getLocation(), VD
->getType(),
1339 diag::err_tentative_def_incomplete_type
))
1340 VD
->setInvalidDecl();
1342 // No initialization is performed for a tentative definition.
1343 CheckCompleteVariableDeclaration(VD
);
1345 // Notify the consumer that we've completed a tentative definition.
1346 if (!VD
->isInvalidDecl())
1347 Consumer
.CompleteTentativeDefinition(VD
);
1350 for (auto *D
: ExternalDeclarations
) {
1351 if (!D
|| D
->isInvalidDecl() || D
->getPreviousDecl() || !D
->isUsed())
1354 Consumer
.CompleteExternalDeclaration(D
);
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 if (!Diags
.hasErrorOccurred() && TUKind
!= TU_Module
) {
1362 // Output warning for unused file scoped decls.
1363 for (UnusedFileScopedDeclsType::iterator
1364 I
= UnusedFileScopedDecls
.begin(ExternalSource
.get()),
1365 E
= UnusedFileScopedDecls
.end();
1367 if (ShouldRemoveFromUnused(this, *I
))
1370 if (const FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(*I
)) {
1371 const FunctionDecl
*DiagD
;
1372 if (!FD
->hasBody(DiagD
))
1374 if (DiagD
->isDeleted())
1375 continue; // Deleted functions are supposed to be unused.
1376 SourceRange DiagRange
= DiagD
->getLocation();
1377 if (const ASTTemplateArgumentListInfo
*ASTTAL
=
1378 DiagD
->getTemplateSpecializationArgsAsWritten())
1379 DiagRange
.setEnd(ASTTAL
->RAngleLoc
);
1380 if (DiagD
->isReferenced()) {
1381 if (isa
<CXXMethodDecl
>(DiagD
))
1382 Diag(DiagD
->getLocation(), diag::warn_unneeded_member_function
)
1383 << DiagD
<< DiagRange
;
1385 if (FD
->getStorageClass() == SC_Static
&&
1386 !FD
->isInlineSpecified() &&
1387 !SourceMgr
.isInMainFile(
1388 SourceMgr
.getExpansionLoc(FD
->getLocation())))
1389 Diag(DiagD
->getLocation(),
1390 diag::warn_unneeded_static_internal_decl
)
1391 << DiagD
<< DiagRange
;
1393 Diag(DiagD
->getLocation(), diag::warn_unneeded_internal_decl
)
1394 << /*function=*/0 << DiagD
<< DiagRange
;
1397 if (FD
->getDescribedFunctionTemplate())
1398 Diag(DiagD
->getLocation(), diag::warn_unused_template
)
1399 << /*function=*/0 << DiagD
<< DiagRange
;
1401 Diag(DiagD
->getLocation(), isa
<CXXMethodDecl
>(DiagD
)
1402 ? diag::warn_unused_member_function
1403 : diag::warn_unused_function
)
1404 << DiagD
<< DiagRange
;
1407 const VarDecl
*DiagD
= cast
<VarDecl
>(*I
)->getDefinition();
1409 DiagD
= cast
<VarDecl
>(*I
);
1410 SourceRange DiagRange
= DiagD
->getLocation();
1411 if (const auto *VTSD
= dyn_cast
<VarTemplateSpecializationDecl
>(DiagD
)) {
1412 if (const ASTTemplateArgumentListInfo
*ASTTAL
=
1413 VTSD
->getTemplateArgsInfo())
1414 DiagRange
.setEnd(ASTTAL
->RAngleLoc
);
1416 if (DiagD
->isReferenced()) {
1417 Diag(DiagD
->getLocation(), diag::warn_unneeded_internal_decl
)
1418 << /*variable=*/1 << DiagD
<< DiagRange
;
1419 } else if (DiagD
->getDescribedVarTemplate()) {
1420 Diag(DiagD
->getLocation(), diag::warn_unused_template
)
1421 << /*variable=*/1 << DiagD
<< DiagRange
;
1422 } else if (DiagD
->getType().isConstQualified()) {
1423 const SourceManager
&SM
= SourceMgr
;
1424 if (SM
.getMainFileID() != SM
.getFileID(DiagD
->getLocation()) ||
1425 !PP
.getLangOpts().IsHeaderFile
)
1426 Diag(DiagD
->getLocation(), diag::warn_unused_const_variable
)
1427 << DiagD
<< DiagRange
;
1429 Diag(DiagD
->getLocation(), diag::warn_unused_variable
)
1430 << DiagD
<< DiagRange
;
1435 emitAndClearUnusedLocalTypedefWarnings();
1438 if (!Diags
.isIgnored(diag::warn_unused_private_field
, SourceLocation())) {
1439 // FIXME: Load additional unused private field candidates from the external
1441 RecordCompleteMap RecordsComplete
;
1442 RecordCompleteMap MNCComplete
;
1443 for (const NamedDecl
*D
: UnusedPrivateFields
) {
1444 const CXXRecordDecl
*RD
= dyn_cast
<CXXRecordDecl
>(D
->getDeclContext());
1445 if (RD
&& !RD
->isUnion() &&
1446 IsRecordFullyDefined(RD
, RecordsComplete
, MNCComplete
)) {
1447 Diag(D
->getLocation(), diag::warn_unused_private_field
)
1448 << D
->getDeclName();
1453 if (!Diags
.isIgnored(diag::warn_mismatched_delete_new
, SourceLocation())) {
1455 ExternalSource
->ReadMismatchingDeleteExpressions(DeleteExprs
);
1456 for (const auto &DeletedFieldInfo
: DeleteExprs
) {
1457 for (const auto &DeleteExprLoc
: DeletedFieldInfo
.second
) {
1458 AnalyzeDeleteExprMismatch(DeletedFieldInfo
.first
, DeleteExprLoc
.first
,
1459 DeleteExprLoc
.second
);
1464 AnalysisWarnings
.IssueWarnings(Context
.getTranslationUnitDecl());
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 assert(ParsingInitForAutoVars
.empty() &&
1470 "Didn't unmark var as having its initializer parsed");
1472 if (!PP
.isIncrementalProcessingEnabled())
1477 //===----------------------------------------------------------------------===//
1478 // Helper functions.
1479 //===----------------------------------------------------------------------===//
1481 DeclContext
*Sema::getFunctionLevelDeclContext(bool AllowLambda
) const {
1482 DeclContext
*DC
= CurContext
;
1485 if (isa
<BlockDecl
>(DC
) || isa
<EnumDecl
>(DC
) || isa
<CapturedDecl
>(DC
) ||
1486 isa
<RequiresExprBodyDecl
>(DC
)) {
1487 DC
= DC
->getParent();
1488 } else if (!AllowLambda
&& isa
<CXXMethodDecl
>(DC
) &&
1489 cast
<CXXMethodDecl
>(DC
)->getOverloadedOperator() == OO_Call
&&
1490 cast
<CXXRecordDecl
>(DC
->getParent())->isLambda()) {
1491 DC
= DC
->getParent()->getParent();
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 FunctionDecl
*Sema::getCurFunctionDecl(bool AllowLambda
) const {
1502 DeclContext
*DC
= getFunctionLevelDeclContext(AllowLambda
);
1503 return dyn_cast
<FunctionDecl
>(DC
);
1506 ObjCMethodDecl
*Sema::getCurMethodDecl() {
1507 DeclContext
*DC
= getFunctionLevelDeclContext();
1508 while (isa
<RecordDecl
>(DC
))
1509 DC
= DC
->getParent();
1510 return dyn_cast
<ObjCMethodDecl
>(DC
);
1513 NamedDecl
*Sema::getCurFunctionOrMethodDecl() const {
1514 DeclContext
*DC
= getFunctionLevelDeclContext();
1515 if (isa
<ObjCMethodDecl
>(DC
) || isa
<FunctionDecl
>(DC
))
1516 return cast
<NamedDecl
>(DC
);
1520 LangAS
Sema::getDefaultCXXMethodAddrSpace() const {
1521 if (getLangOpts().OpenCL
)
1522 return getASTContext().getDefaultOpenCLPointeeAddrSpace();
1523 return LangAS::Default
;
1526 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 if (std::optional
<TemplateDeductionInfo
*> Info
= isSFINAEContext()) {
1535 switch (DiagnosticIDs::getDiagnosticSFINAEResponse(
1536 Diags
.getCurrentDiagID())) {
1537 case DiagnosticIDs::SFINAE_Report
:
1538 // We'll report the diagnostic below.
1541 case DiagnosticIDs::SFINAE_SubstitutionFailure
:
1542 // Count this failure so that we know that template argument deduction
1546 // Make a copy of this suppressed diagnostic and store it with the
1547 // template-deduction information.
1548 if (*Info
&& !(*Info
)->hasSFINAEDiagnostic()) {
1549 Diagnostic
DiagInfo(&Diags
);
1550 (*Info
)->addSFINAEDiagnostic(DiagInfo
.getLocation(),
1551 PartialDiagnostic(DiagInfo
, Context
.getDiagAllocator()));
1554 Diags
.setLastDiagnosticIgnored(true);
1558 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
1563 if (!AccessCheckingSFINAE
&& !getLangOpts().CPlusPlus11
)
1566 SourceLocation Loc
= Diags
.getCurrentDiagLoc();
1568 // Suppress this diagnostic.
1571 // Make a copy of this suppressed diagnostic and store it with the
1572 // template-deduction information.
1573 if (*Info
&& !(*Info
)->hasSFINAEDiagnostic()) {
1574 Diagnostic
DiagInfo(&Diags
);
1575 (*Info
)->addSFINAEDiagnostic(DiagInfo
.getLocation(),
1576 PartialDiagnostic(DiagInfo
, Context
.getDiagAllocator()));
1579 Diags
.setLastDiagnosticIgnored(true);
1582 // Now the diagnostic state is clear, produce a C++98 compatibility
1584 Diag(Loc
, diag::warn_cxx98_compat_sfinae_access_control
);
1586 // The last diagnostic which Sema produced was ignored. Suppress any
1587 // notes attached to it.
1588 Diags
.setLastDiagnosticIgnored(true);
1592 case DiagnosticIDs::SFINAE_Suppress
:
1593 // Make a copy of this suppressed diagnostic and store it with the
1594 // template-deduction information;
1596 Diagnostic
DiagInfo(&Diags
);
1597 (*Info
)->addSuppressedDiagnostic(DiagInfo
.getLocation(),
1598 PartialDiagnostic(DiagInfo
, Context
.getDiagAllocator()));
1601 // Suppress this diagnostic.
1602 Diags
.setLastDiagnosticIgnored(true);
1608 // Copy the diagnostic printing policy over the ASTContext printing policy.
1609 // TODO: Stop doing that. See: https://reviews.llvm.org/D45093#1090292
1610 Context
.setPrintingPolicy(getPrintingPolicy());
1612 // Emit the diagnostic.
1613 if (!Diags
.EmitCurrentDiagnostic())
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
1620 if (!DiagnosticIDs::isBuiltinNote(DiagID
))
1621 PrintContextStack();
1624 Sema::SemaDiagnosticBuilder
1625 Sema::Diag(SourceLocation Loc
, const PartialDiagnostic
&PD
, bool DeferHint
) {
1626 return Diag(Loc
, PD
.getDiagID(), DeferHint
) << PD
;
1629 bool Sema::hasUncompilableErrorOccurred() const {
1630 if (getDiagnostics().hasUncompilableErrorOccurred())
1632 auto *FD
= dyn_cast
<FunctionDecl
>(CurContext
);
1635 auto Loc
= DeviceDeferredDiags
.find(FD
);
1636 if (Loc
== DeviceDeferredDiags
.end())
1638 for (auto PDAt
: Loc
->second
) {
1639 if (DiagnosticIDs::isDefaultMappingAsError(PDAt
.second
.getDiagID()))
1645 // Print notes showing how we can reach FD starting from an a priori
1646 // known-callable function.
1647 static void emitCallStackNotes(Sema
&S
, const FunctionDecl
*FD
) {
1648 auto FnIt
= S
.DeviceKnownEmittedFns
.find(FD
);
1649 while (FnIt
!= S
.DeviceKnownEmittedFns
.end()) {
1650 // Respect error limit.
1651 if (S
.Diags
.hasFatalErrorOccurred())
1653 DiagnosticBuilder
Builder(
1654 S
.Diags
.Report(FnIt
->second
.Loc
, diag::note_called_by
));
1655 Builder
<< FnIt
->second
.FD
;
1656 FnIt
= S
.DeviceKnownEmittedFns
.find(FnIt
->second
.FD
);
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.
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.
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
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.
1689 class DeferredDiagnosticsEmitter
1690 : public UsedDeclVisitor
<DeferredDiagnosticsEmitter
> {
1692 typedef UsedDeclVisitor
<DeferredDiagnosticsEmitter
> Inherited
;
1694 // Whether the function is already in the current use-path.
1695 llvm::SmallPtrSet
<CanonicalDeclPtr
<Decl
>, 4> InUsePath
;
1697 // The current use-path.
1698 llvm::SmallVector
<CanonicalDeclPtr
<FunctionDecl
>, 4> UsePath
;
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];
1706 // Emission state of the root node of the current use graph.
1707 bool ShouldEmitRootNode
;
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
;
1714 DeferredDiagnosticsEmitter(Sema
&S
)
1715 : Inherited(S
), ShouldEmitRootNode(false), InOMPDeviceContext(0) {}
1717 bool shouldVisitDiscardedStmt() const { return false; }
1719 void VisitOMPTargetDirective(OMPTargetDirective
*Node
) {
1720 ++InOMPDeviceContext
;
1721 Inherited::VisitOMPTargetDirective(Node
);
1722 --InOMPDeviceContext
;
1725 void visitUsedDecl(SourceLocation Loc
, Decl
*D
) {
1726 if (isa
<VarDecl
>(D
))
1728 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
))
1731 Inherited::visitUsedDecl(Loc
, D
);
1734 void checkVar(VarDecl
*VD
) {
1735 assert(VD
->isFileVarDecl() &&
1736 "Should only check file-scope variables");
1737 if (auto *Init
= VD
->getInit()) {
1738 auto DevTy
= OMPDeclareTargetDeclAttr::getDeviceType(VD
);
1739 bool IsDev
= DevTy
&& (*DevTy
== OMPDeclareTargetDeclAttr::DT_NoHost
||
1740 *DevTy
== OMPDeclareTargetDeclAttr::DT_Any
);
1742 ++InOMPDeviceContext
;
1745 --InOMPDeviceContext
;
1749 void checkFunc(SourceLocation Loc
, FunctionDecl
*FD
) {
1750 auto &Done
= DoneMap
[InOMPDeviceContext
> 0 ? 1 : 0];
1751 FunctionDecl
*Caller
= UsePath
.empty() ? nullptr : UsePath
.back();
1752 if ((!ShouldEmitRootNode
&& !S
.getLangOpts().OpenMP
&& !Caller
) ||
1753 S
.shouldIgnoreInHostDeviceCheck(FD
) || InUsePath
.count(FD
))
1755 // Finalize analysis of OpenMP-specific constructs.
1756 if (Caller
&& S
.LangOpts
.OpenMP
&& UsePath
.size() == 1 &&
1757 (ShouldEmitRootNode
|| InOMPDeviceContext
))
1758 S
.finalizeOpenMPDelayedAnalysis(Caller
, FD
, Loc
);
1760 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
1764 if (ShouldEmitRootNode
|| InOMPDeviceContext
)
1765 emitDeferredDiags(FD
, Caller
);
1766 // Do not revisit a function if the function body has been completely
1768 if (!Done
.insert(FD
).second
)
1770 InUsePath
.insert(FD
);
1771 UsePath
.push_back(FD
);
1772 if (auto *S
= FD
->getBody()) {
1776 InUsePath
.erase(FD
);
1779 void checkRecordedDecl(Decl
*D
) {
1780 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
1781 ShouldEmitRootNode
= S
.getEmissionStatus(FD
, /*Final=*/true) ==
1782 Sema::FunctionEmissionStatus::Emitted
;
1783 checkFunc(SourceLocation(), FD
);
1785 checkVar(cast
<VarDecl
>(D
));
1788 // Emit any deferred diagnostics for FD
1789 void emitDeferredDiags(FunctionDecl
*FD
, bool ShowCallStack
) {
1790 auto It
= S
.DeviceDeferredDiags
.find(FD
);
1791 if (It
== S
.DeviceDeferredDiags
.end())
1793 bool HasWarningOrError
= false;
1794 bool FirstDiag
= true;
1795 for (PartialDiagnosticAt
&PDAt
: It
->second
) {
1796 // Respect error limit.
1797 if (S
.Diags
.hasFatalErrorOccurred())
1799 const SourceLocation
&Loc
= PDAt
.first
;
1800 const PartialDiagnostic
&PD
= PDAt
.second
;
1801 HasWarningOrError
|=
1802 S
.getDiagnostics().getDiagnosticLevel(PD
.getDiagID(), Loc
) >=
1803 DiagnosticsEngine::Warning
;
1805 DiagnosticBuilder
Builder(S
.Diags
.Report(Loc
, PD
.getDiagID()));
1808 // Emit the note on the first diagnostic in case too many diagnostics
1809 // cause the note not emitted.
1810 if (FirstDiag
&& HasWarningOrError
&& ShowCallStack
) {
1811 emitCallStackNotes(S
, FD
);
1819 void Sema::emitDeferredDiags() {
1821 ExternalSource
->ReadDeclsToCheckForDeferredDiags(
1822 DeclsToCheckForDeferredDiags
);
1824 if ((DeviceDeferredDiags
.empty() && !LangOpts
.OpenMP
) ||
1825 DeclsToCheckForDeferredDiags
.empty())
1828 DeferredDiagnosticsEmitter
DDE(*this);
1829 for (auto *D
: DeclsToCheckForDeferredDiags
)
1830 DDE
.checkRecordedDecl(D
);
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.
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.
1845 // We also keep a partial call graph of which not-known-emitted functions call
1846 // which other not-known-emitted functions.
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.
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.
1857 Sema::SemaDiagnosticBuilder::SemaDiagnosticBuilder(Kind K
, SourceLocation Loc
,
1859 const FunctionDecl
*Fn
,
1861 : S(S
), Loc(Loc
), DiagID(DiagID
), Fn(Fn
),
1862 ShowCallStack(K
== K_ImmediateWithCallStack
|| K
== K_Deferred
) {
1867 case K_ImmediateWithCallStack
:
1868 ImmediateDiag
.emplace(
1869 ImmediateDiagBuilder(S
.Diags
.Report(Loc
, DiagID
), S
, DiagID
));
1872 assert(Fn
&& "Must have a function to attach the deferred diag to.");
1873 auto &Diags
= S
.DeviceDeferredDiags
[Fn
];
1874 PartialDiagId
.emplace(Diags
.size());
1875 Diags
.emplace_back(Loc
, S
.PDiag(DiagID
));
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 PartialDiagId(D
.PartialDiagId
) {
1884 // Clean the previous diagnostics.
1885 D
.ShowCallStack
= false;
1886 D
.ImmediateDiag
.reset();
1887 D
.PartialDiagId
.reset();
1890 Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
1891 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 bool IsWarningOrError
= S
.getDiagnostics().getDiagnosticLevel(
1895 DiagID
, Loc
) >= DiagnosticsEngine::Warning
;
1896 ImmediateDiag
.reset(); // Emit the immediate diag.
1897 if (IsWarningOrError
&& ShowCallStack
)
1898 emitCallStackNotes(S
, Fn
);
1900 assert((!PartialDiagId
|| ShowCallStack
) &&
1901 "Must always show call stack for deferred diags.");
1905 Sema::SemaDiagnosticBuilder
1906 Sema::targetDiag(SourceLocation Loc
, unsigned DiagID
, const FunctionDecl
*FD
) {
1907 FD
= FD
? FD
: getCurFunctionDecl();
1908 if (LangOpts
.OpenMP
)
1909 return LangOpts
.OpenMPIsTargetDevice
1910 ? diagIfOpenMPDeviceCode(Loc
, DiagID
, FD
)
1911 : diagIfOpenMPHostCode(Loc
, DiagID
, FD
);
1912 if (getLangOpts().CUDA
)
1913 return getLangOpts().CUDAIsDevice
? CUDADiagIfDeviceCode(Loc
, DiagID
)
1914 : CUDADiagIfHostCode(Loc
, DiagID
);
1916 if (getLangOpts().SYCLIsDevice
)
1917 return SYCLDiagIfDeviceCode(Loc
, DiagID
);
1919 return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate
, Loc
, DiagID
,
1923 Sema::SemaDiagnosticBuilder
Sema::Diag(SourceLocation Loc
, unsigned DiagID
,
1925 bool IsError
= Diags
.getDiagnosticIDs()->isDefaultMappingAsError(DiagID
);
1926 bool ShouldDefer
= getLangOpts().CUDA
&& LangOpts
.GPUDeferDiag
&&
1927 DiagnosticIDs::isDeferrable(DiagID
) &&
1928 (DeferHint
|| DeferDiags
|| !IsError
);
1929 auto SetIsLastErrorImmediate
= [&](bool Flag
) {
1931 IsLastErrorImmediate
= Flag
;
1934 SetIsLastErrorImmediate(true);
1935 return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate
, Loc
,
1936 DiagID
, getCurFunctionDecl(), *this);
1939 SemaDiagnosticBuilder DB
= getLangOpts().CUDAIsDevice
1940 ? CUDADiagIfDeviceCode(Loc
, DiagID
)
1941 : CUDADiagIfHostCode(Loc
, DiagID
);
1942 SetIsLastErrorImmediate(DB
.isImmediate());
1946 void Sema::checkTypeSupport(QualType Ty
, SourceLocation Loc
, ValueDecl
*D
) {
1947 if (isUnevaluatedContext() || Ty
.isNull())
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 if (D
&& LangOpts
.SYCLIsDevice
) {
1955 llvm::DenseSet
<QualType
> Visited
;
1956 deepTypeCheckForSYCLDevice(Loc
, Visited
, D
);
1959 Decl
*C
= cast
<Decl
>(getCurLexicalContext());
1961 // Memcpy operations for structs containing a member with unsupported type
1963 if (const auto *MD
= dyn_cast
<CXXMethodDecl
>(C
)) {
1964 if ((MD
->isCopyAssignmentOperator() || MD
->isMoveAssignmentOperator()) &&
1968 if (const auto *Ctor
= dyn_cast
<CXXConstructorDecl
>(MD
))
1969 if (Ctor
->isCopyOrMoveConstructor() && Ctor
->isTrivial())
1973 // Try to associate errors with the lexical context, if that is a function, or
1974 // the value declaration otherwise.
1975 const FunctionDecl
*FD
= isa
<FunctionDecl
>(C
)
1976 ? cast
<FunctionDecl
>(C
)
1977 : dyn_cast_or_null
<FunctionDecl
>(D
);
1979 auto CheckDeviceType
= [&](QualType Ty
) {
1980 if (Ty
->isDependentType())
1983 if (Ty
->isBitIntType()) {
1984 if (!Context
.getTargetInfo().hasBitIntType()) {
1985 PartialDiagnostic PD
= PDiag(diag::err_target_unsupported_type
);
1990 targetDiag(Loc
, PD
, FD
)
1991 << false /*show bit size*/ << 0 /*bitsize*/ << false /*return*/
1992 << Ty
<< Context
.getTargetInfo().getTriple().str();
1997 // Check if we are dealing with two 'long double' but with different
1999 bool LongDoubleMismatched
= false;
2000 if (Ty
->isRealFloatingType() && Context
.getTypeSize(Ty
) == 128) {
2001 const llvm::fltSemantics
&Sem
= Context
.getFloatTypeSemantics(Ty
);
2002 if ((&Sem
!= &llvm::APFloat::PPCDoubleDouble() &&
2003 !Context
.getTargetInfo().hasFloat128Type()) ||
2004 (&Sem
== &llvm::APFloat::PPCDoubleDouble() &&
2005 !Context
.getTargetInfo().hasIbm128Type()))
2006 LongDoubleMismatched
= true;
2009 if ((Ty
->isFloat16Type() && !Context
.getTargetInfo().hasFloat16Type()) ||
2010 (Ty
->isFloat128Type() && !Context
.getTargetInfo().hasFloat128Type()) ||
2011 (Ty
->isIbm128Type() && !Context
.getTargetInfo().hasIbm128Type()) ||
2012 (Ty
->isIntegerType() && Context
.getTypeSize(Ty
) == 128 &&
2013 !Context
.getTargetInfo().hasInt128Type()) ||
2014 (Ty
->isBFloat16Type() && !Context
.getTargetInfo().hasBFloat16Type() &&
2015 !LangOpts
.CUDAIsDevice
) ||
2016 LongDoubleMismatched
) {
2017 PartialDiagnostic PD
= PDiag(diag::err_target_unsupported_type
);
2023 if (targetDiag(Loc
, PD
, FD
)
2024 << true /*show bit size*/
2025 << static_cast<unsigned>(Context
.getTypeSize(Ty
)) << Ty
2026 << false /*return*/ << Context
.getTargetInfo().getTriple().str()) {
2028 D
->setInvalidDecl();
2031 targetDiag(D
->getLocation(), diag::note_defined_here
, FD
) << D
;
2035 auto CheckType
= [&](QualType Ty
, bool IsRetTy
= false) {
2036 if (LangOpts
.SYCLIsDevice
||
2037 (LangOpts
.OpenMP
&& LangOpts
.OpenMPIsTargetDevice
) ||
2038 LangOpts
.CUDAIsDevice
)
2039 CheckDeviceType(Ty
);
2041 QualType UnqualTy
= Ty
.getCanonicalType().getUnqualifiedType();
2042 const TargetInfo
&TI
= Context
.getTargetInfo();
2043 if (!TI
.hasLongDoubleType() && UnqualTy
== Context
.LongDoubleTy
) {
2044 PartialDiagnostic PD
= PDiag(diag::err_target_unsupported_type
);
2050 if (Diag(Loc
, PD
, FD
)
2051 << false /*show bit size*/ << 0 << Ty
<< false /*return*/
2052 << TI
.getTriple().str()) {
2054 D
->setInvalidDecl();
2057 targetDiag(D
->getLocation(), diag::note_defined_here
, FD
) << D
;
2060 bool IsDouble
= UnqualTy
== Context
.DoubleTy
;
2061 bool IsFloat
= UnqualTy
== Context
.FloatTy
;
2062 if (IsRetTy
&& !TI
.hasFPReturn() && (IsDouble
|| IsFloat
)) {
2063 PartialDiagnostic PD
= PDiag(diag::err_target_unsupported_type
);
2069 if (Diag(Loc
, PD
, FD
)
2070 << false /*show bit size*/ << 0 << Ty
<< true /*return*/
2071 << TI
.getTriple().str()) {
2073 D
->setInvalidDecl();
2076 targetDiag(D
->getLocation(), diag::note_defined_here
, FD
) << D
;
2079 if (TI
.hasRISCVVTypes() && Ty
->isRVVSizelessBuiltinType())
2080 checkRVVTypeSupport(Ty
, Loc
, D
);
2082 // Don't allow SVE types in functions without a SVE target.
2083 if (Ty
->isSVESizelessBuiltinType() && FD
&& FD
->hasBody()) {
2084 llvm::StringMap
<bool> CallerFeatureMap
;
2085 Context
.getFunctionFeatureMap(CallerFeatureMap
, FD
);
2086 if (!Builtin::evaluateRequiredTargetFeatures("sve", CallerFeatureMap
) &&
2087 !Builtin::evaluateRequiredTargetFeatures("sme", CallerFeatureMap
))
2088 Diag(D
->getLocation(), diag::err_sve_vector_in_non_sve_target
) << Ty
;
2093 if (const auto *FPTy
= dyn_cast
<FunctionProtoType
>(Ty
)) {
2094 for (const auto &ParamTy
: FPTy
->param_types())
2096 CheckType(FPTy
->getReturnType(), /*IsRetTy=*/true);
2098 if (const auto *FNPTy
= dyn_cast
<FunctionNoProtoType
>(Ty
))
2099 CheckType(FNPTy
->getReturnType(), /*IsRetTy=*/true);
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
2106 bool Sema::findMacroSpelling(SourceLocation
&locref
, StringRef name
) {
2107 SourceLocation loc
= locref
;
2108 if (!loc
.isMacroID()) return false;
2110 // There's no good way right now to look at the intermediate
2111 // expansions, so just jump to the expansion location.
2112 loc
= getSourceManager().getExpansionLoc(loc
);
2114 // If that's written with the name, stop here.
2115 SmallString
<16> buffer
;
2116 if (getPreprocessor().getSpelling(loc
, buffer
) == name
) {
2123 /// Determines the active Scope associated with the given declaration
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.
2132 /// \returns The scope corresponding to the given declaraion context, or NULL
2133 /// if no such scope is open.
2134 Scope
*Sema::getScopeForContext(DeclContext
*Ctx
) {
2139 Ctx
= Ctx
->getPrimaryContext();
2140 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 if (S
->getFlags() & (Scope::DeclScope
| Scope::TemplateParamScope
))
2144 if (DeclContext
*Entity
= S
->getEntity())
2145 if (Ctx
== Entity
->getPrimaryContext())
2152 /// Enter a new function scope
2153 void Sema::PushFunctionScope() {
2154 if (FunctionScopes
.empty() && CachedFunctionScope
) {
2155 // Use CachedFunctionScope to avoid allocating memory when possible.
2156 CachedFunctionScope
->Clear();
2157 FunctionScopes
.push_back(CachedFunctionScope
.release());
2159 FunctionScopes
.push_back(new FunctionScopeInfo(getDiagnostics()));
2161 if (LangOpts
.OpenMP
)
2162 pushOpenMPFunctionRegion();
2165 void Sema::PushBlockScope(Scope
*BlockScope
, BlockDecl
*Block
) {
2166 FunctionScopes
.push_back(new BlockScopeInfo(getDiagnostics(),
2167 BlockScope
, Block
));
2168 CapturingFunctionScopes
++;
2171 LambdaScopeInfo
*Sema::PushLambdaScope() {
2172 LambdaScopeInfo
*const LSI
= new LambdaScopeInfo(getDiagnostics());
2173 FunctionScopes
.push_back(LSI
);
2174 CapturingFunctionScopes
++;
2178 void Sema::RecordParsingTemplateParameterDepth(unsigned Depth
) {
2179 if (LambdaScopeInfo
*const LSI
= getCurLambda()) {
2180 LSI
->AutoTemplateParameterDepth
= Depth
;
2184 "Remove assertion if intentionally called in a non-lambda context.");
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 static void checkEscapingByref(VarDecl
*VD
, Sema
&S
) {
2193 QualType T
= VD
->getType();
2194 EnterExpressionEvaluationContext
scope(
2195 S
, Sema::ExpressionEvaluationContext::PotentiallyEvaluated
);
2196 SourceLocation Loc
= VD
->getLocation();
2198 new (S
.Context
) DeclRefExpr(S
.Context
, VD
, false, T
, VK_LValue
, Loc
);
2200 auto IE
= InitializedEntity::InitializeBlock(Loc
, T
);
2201 if (S
.getLangOpts().CPlusPlus23
) {
2202 auto *E
= ImplicitCastExpr::Create(S
.Context
, T
, CK_NoOp
, VarRef
, nullptr,
2203 VK_XValue
, FPOptionsOverride());
2204 Result
= S
.PerformCopyInitialization(IE
, SourceLocation(), E
);
2206 Result
= S
.PerformMoveOrCopyInitialization(
2207 IE
, Sema::NamedReturnInfo
{VD
, Sema::NamedReturnInfo::MoveEligible
},
2211 if (!Result
.isInvalid()) {
2212 Result
= S
.MaybeCreateExprWithCleanups(Result
);
2213 Expr
*Init
= Result
.getAs
<Expr
>();
2214 S
.Context
.setBlockVarCopyInit(VD
, Init
, S
.canThrow(Init
));
2217 // The destructor's exception specification is needed when IRGen generates
2218 // block copy/destroy functions. Resolve it here.
2219 if (const CXXRecordDecl
*RD
= T
->getAsCXXRecordDecl())
2220 if (CXXDestructorDecl
*DD
= RD
->getDestructor()) {
2221 auto *FPT
= DD
->getType()->getAs
<FunctionProtoType
>();
2222 S
.ResolveExceptionSpec(Loc
, FPT
);
2226 static void markEscapingByrefs(const FunctionScopeInfo
&FSI
, Sema
&S
) {
2227 // Set the EscapingByref flag of __block variables captured by
2229 for (const BlockDecl
*BD
: FSI
.Blocks
) {
2230 for (const BlockDecl::Capture
&BC
: BD
->captures()) {
2231 VarDecl
*VD
= BC
.getVariable();
2232 if (VD
->hasAttr
<BlocksAttr
>()) {
2233 // Nothing to do if this is a __block variable captured by a
2234 // non-escaping block.
2235 if (BD
->doesNotEscape())
2237 VD
->setEscapingByref();
2239 // Check whether the captured variable is or contains an object of
2240 // non-trivial C union type.
2241 QualType CapType
= BC
.getVariable()->getType();
2242 if (CapType
.hasNonTrivialToPrimitiveDestructCUnion() ||
2243 CapType
.hasNonTrivialToPrimitiveCopyCUnion())
2244 S
.checkNonTrivialCUnion(BC
.getVariable()->getType(),
2245 BD
->getCaretLocation(),
2246 Sema::NTCUC_BlockCapture
,
2247 Sema::NTCUK_Destruct
|Sema::NTCUK_Copy
);
2251 for (VarDecl
*VD
: FSI
.ByrefBlockVars
) {
2252 // __block variables might require us to capture a copy-initializer.
2253 if (!VD
->isEscapingByref())
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 if (VD
->getType()->isStructureOrClassType())
2260 checkEscapingByref(VD
, S
);
2264 /// Pop a function (or block or lambda or captured region) scope from the stack.
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 const Decl
*D
, QualType BlockType
) {
2274 assert(!FunctionScopes
.empty() && "mismatched push/pop!");
2276 markEscapingByrefs(*FunctionScopes
.back(), *this);
2278 PoppedFunctionScopePtr
Scope(FunctionScopes
.pop_back_val(),
2279 PoppedFunctionScopeDeleter(this));
2281 if (LangOpts
.OpenMP
)
2282 popOpenMPFunctionRegion(Scope
.get());
2284 // Issue any analysis-based warnings.
2286 AnalysisWarnings
.IssueWarnings(*WP
, Scope
.get(), D
, BlockType
);
2288 for (const auto &PUD
: Scope
->PossiblyUnreachableDiags
)
2289 Diag(PUD
.Loc
, PUD
.PD
);
2294 void Sema::PoppedFunctionScopeDeleter::
2295 operator()(sema::FunctionScopeInfo
*Scope
) const {
2296 if (!Scope
->isPlainFunction())
2297 Self
->CapturingFunctionScopes
--;
2298 // Stash the function scope for later reuse if it's for a normal function.
2299 if (Scope
->isPlainFunction() && !Self
->CachedFunctionScope
)
2300 Self
->CachedFunctionScope
.reset(Scope
);
2305 void Sema::PushCompoundScope(bool IsStmtExpr
) {
2306 getCurFunction()->CompoundScopes
.push_back(
2307 CompoundScopeInfo(IsStmtExpr
, getCurFPFeatures()));
2310 void Sema::PopCompoundScope() {
2311 FunctionScopeInfo
*CurFunction
= getCurFunction();
2312 assert(!CurFunction
->CompoundScopes
.empty() && "mismatched push/pop");
2314 CurFunction
->CompoundScopes
.pop_back();
2317 /// Determine whether any errors occurred within this function/method/
2319 bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const {
2320 return getCurFunction()->hasUnrecoverableErrorOccurred();
2323 void Sema::setFunctionHasBranchIntoScope() {
2324 if (!FunctionScopes
.empty())
2325 FunctionScopes
.back()->setHasBranchIntoScope();
2328 void Sema::setFunctionHasBranchProtectedScope() {
2329 if (!FunctionScopes
.empty())
2330 FunctionScopes
.back()->setHasBranchProtectedScope();
2333 void Sema::setFunctionHasIndirectGoto() {
2334 if (!FunctionScopes
.empty())
2335 FunctionScopes
.back()->setHasIndirectGoto();
2338 void Sema::setFunctionHasMustTail() {
2339 if (!FunctionScopes
.empty())
2340 FunctionScopes
.back()->setHasMustTail();
2343 BlockScopeInfo
*Sema::getCurBlock() {
2344 if (FunctionScopes
.empty())
2347 auto CurBSI
= dyn_cast
<BlockScopeInfo
>(FunctionScopes
.back());
2348 if (CurBSI
&& CurBSI
->TheDecl
&&
2349 !CurBSI
->TheDecl
->Encloses(CurContext
)) {
2350 // We have switched contexts due to template instantiation.
2351 assert(!CodeSynthesisContexts
.empty());
2358 FunctionScopeInfo
*Sema::getEnclosingFunction() const {
2359 if (FunctionScopes
.empty())
2362 for (int e
= FunctionScopes
.size() - 1; e
>= 0; --e
) {
2363 if (isa
<sema::BlockScopeInfo
>(FunctionScopes
[e
]))
2365 return FunctionScopes
[e
];
2370 LambdaScopeInfo
*Sema::getEnclosingLambda() const {
2371 for (auto *Scope
: llvm::reverse(FunctionScopes
)) {
2372 if (auto *LSI
= dyn_cast
<sema::LambdaScopeInfo
>(Scope
)) {
2373 if (LSI
->Lambda
&& !LSI
->Lambda
->Encloses(CurContext
) &&
2374 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 assert(!CodeSynthesisContexts
.empty());
2387 LambdaScopeInfo
*Sema::getCurLambda(bool IgnoreNonLambdaCapturingScope
) {
2388 if (FunctionScopes
.empty())
2391 auto I
= FunctionScopes
.rbegin();
2392 if (IgnoreNonLambdaCapturingScope
) {
2393 auto E
= FunctionScopes
.rend();
2394 while (I
!= E
&& isa
<CapturingScopeInfo
>(*I
) && !isa
<LambdaScopeInfo
>(*I
))
2399 auto *CurLSI
= dyn_cast
<LambdaScopeInfo
>(*I
);
2400 if (CurLSI
&& CurLSI
->Lambda
&& CurLSI
->CallOperator
&&
2401 !CurLSI
->Lambda
->Encloses(CurContext
) && CurLSI
->AfterParameterList
) {
2402 // We have switched contexts due to template instantiation.
2403 assert(!CodeSynthesisContexts
.empty());
2410 // We have a generic lambda if we parsed auto parameters, or we have
2411 // an associated template parameter list.
2412 LambdaScopeInfo
*Sema::getCurGenericLambda() {
2413 if (LambdaScopeInfo
*LSI
= getCurLambda()) {
2414 return (LSI
->TemplateParams
.size() ||
2415 LSI
->GLTemplateParameterList
) ? LSI
: nullptr;
2421 void Sema::ActOnComment(SourceRange Comment
) {
2422 if (!LangOpts
.RetainCommentsFromSystemHeaders
&&
2423 SourceMgr
.isInSystemHeader(Comment
.getBegin()))
2425 RawComment
RC(SourceMgr
, Comment
, LangOpts
.CommentOpts
, false);
2426 if (RC
.isAlmostTrailingComment() || RC
.hasUnsupportedSplice(SourceMgr
)) {
2427 SourceRange
MagicMarkerRange(Comment
.getBegin(),
2428 Comment
.getBegin().getLocWithOffset(3));
2429 StringRef MagicMarkerText
;
2430 switch (RC
.getKind()) {
2431 case RawComment::RCK_OrdinaryBCPL
:
2432 MagicMarkerText
= "///<";
2434 case RawComment::RCK_OrdinaryC
:
2435 MagicMarkerText
= "/**<";
2437 case RawComment::RCK_Invalid
:
2438 // FIXME: are there other scenarios that could produce an invalid
2439 // raw comment here?
2440 Diag(Comment
.getBegin(), diag::warn_splice_in_doxygen_comment
);
2443 llvm_unreachable("if this is an almost Doxygen comment, "
2444 "it should be ordinary");
2446 Diag(Comment
.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment
) <<
2447 FixItHint::CreateReplacement(MagicMarkerRange
, MagicMarkerText
);
2449 Context
.addComment(RC
);
2452 // Pin this vtable to this file.
2453 ExternalSemaSource::~ExternalSemaSource() {}
2454 char ExternalSemaSource::ID
;
2456 void ExternalSemaSource::ReadMethodPool(Selector Sel
) { }
2457 void ExternalSemaSource::updateOutOfDateSelector(Selector Sel
) { }
2459 void ExternalSemaSource::ReadKnownNamespaces(
2460 SmallVectorImpl
<NamespaceDecl
*> &Namespaces
) {
2463 void ExternalSemaSource::ReadUndefinedButUsed(
2464 llvm::MapVector
<NamedDecl
*, SourceLocation
> &Undefined
) {}
2466 void ExternalSemaSource::ReadMismatchingDeleteExpressions(llvm::MapVector
<
2467 FieldDecl
*, llvm::SmallVector
<std::pair
<SourceLocation
, bool>, 4>> &) {}
2469 /// Figure out if an expression could be turned into a call.
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.
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 UnresolvedSetImpl
&OverloadSet
) {
2482 ZeroArgCallReturnTy
= QualType();
2483 OverloadSet
.clear();
2485 const OverloadExpr
*Overloads
= nullptr;
2486 bool IsMemExpr
= false;
2487 if (E
.getType() == Context
.OverloadTy
) {
2488 OverloadExpr::FindResult FR
= OverloadExpr::find(const_cast<Expr
*>(&E
));
2490 // Ignore overloads that are pointer-to-member constants.
2491 if (FR
.HasFormOfMemberPointer
)
2494 Overloads
= FR
.Expression
;
2495 } else if (E
.getType() == Context
.BoundMemberTy
) {
2496 Overloads
= dyn_cast
<UnresolvedMemberExpr
>(E
.IgnoreParens());
2500 bool Ambiguous
= false;
2504 for (OverloadExpr::decls_iterator it
= Overloads
->decls_begin(),
2505 DeclsEnd
= Overloads
->decls_end(); it
!= DeclsEnd
; ++it
) {
2506 OverloadSet
.addDecl(*it
);
2508 // Check whether the function is a non-template, non-member which takes no
2512 if (const FunctionDecl
*OverloadDecl
2513 = dyn_cast
<FunctionDecl
>((*it
)->getUnderlyingDecl())) {
2514 if (OverloadDecl
->getMinRequiredArguments() == 0) {
2515 if (!ZeroArgCallReturnTy
.isNull() && !Ambiguous
&&
2516 (!IsMV
|| !(OverloadDecl
->isCPUDispatchMultiVersion() ||
2517 OverloadDecl
->isCPUSpecificMultiVersion()))) {
2518 ZeroArgCallReturnTy
= QualType();
2521 ZeroArgCallReturnTy
= OverloadDecl
->getReturnType();
2522 IsMV
= OverloadDecl
->isCPUDispatchMultiVersion() ||
2523 OverloadDecl
->isCPUSpecificMultiVersion();
2529 // If it's not a member, use better machinery to try to resolve the call
2531 return !ZeroArgCallReturnTy
.isNull();
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 if (IsMemExpr
&& !E
.isTypeDependent()) {
2538 Sema::TentativeAnalysisScope
Trap(*this);
2539 ExprResult R
= BuildCallToMemberFunction(nullptr, &E
, SourceLocation(),
2540 std::nullopt
, SourceLocation());
2542 ZeroArgCallReturnTy
= R
.get()->getType();
2548 if (const auto *DeclRef
= dyn_cast
<DeclRefExpr
>(E
.IgnoreParens())) {
2549 if (const auto *Fun
= dyn_cast
<FunctionDecl
>(DeclRef
->getDecl())) {
2550 if (Fun
->getMinRequiredArguments() == 0)
2551 ZeroArgCallReturnTy
= Fun
->getReturnType();
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 QualType ExprTy
= E
.getType();
2559 const FunctionType
*FunTy
= nullptr;
2560 QualType PointeeTy
= ExprTy
->getPointeeType();
2561 if (!PointeeTy
.isNull())
2562 FunTy
= PointeeTy
->getAs
<FunctionType
>();
2564 FunTy
= ExprTy
->getAs
<FunctionType
>();
2566 if (const auto *FPT
= dyn_cast_if_present
<FunctionProtoType
>(FunTy
)) {
2567 if (FPT
->getNumParams() == 0)
2568 ZeroArgCallReturnTy
= FunTy
->getReturnType();
2574 /// Give notes for a set of overloads.
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.
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 const SourceLocation FinalNoteLoc
) {
2588 unsigned ShownOverloads
= 0;
2589 unsigned SuppressedOverloads
= 0;
2590 for (UnresolvedSetImpl::iterator It
= Overloads
.begin(),
2591 DeclsEnd
= Overloads
.end(); It
!= DeclsEnd
; ++It
) {
2592 if (ShownOverloads
>= S
.Diags
.getNumOverloadCandidatesToShow()) {
2593 ++SuppressedOverloads
;
2597 const NamedDecl
*Fn
= (*It
)->getUnderlyingDecl();
2598 // Don't print overloads for non-default multiversioned functions.
2599 if (const auto *FD
= Fn
->getAsFunction()) {
2600 if (FD
->isMultiVersion() && FD
->hasAttr
<TargetAttr
>() &&
2601 !FD
->getAttr
<TargetAttr
>()->isDefaultVersion())
2603 if (FD
->isMultiVersion() && FD
->hasAttr
<TargetVersionAttr
>() &&
2604 !FD
->getAttr
<TargetVersionAttr
>()->isDefaultVersion())
2607 S
.Diag(Fn
->getLocation(), diag::note_possible_target_of_call
);
2611 S
.Diags
.overloadCandidatesShown(ShownOverloads
);
2613 if (SuppressedOverloads
)
2614 S
.Diag(FinalNoteLoc
, diag::note_ovl_too_many_candidates
)
2615 << SuppressedOverloads
;
2618 static void notePlausibleOverloads(Sema
&S
, SourceLocation Loc
,
2619 const UnresolvedSetImpl
&Overloads
,
2620 bool (*IsPlausibleResult
)(QualType
)) {
2621 if (!IsPlausibleResult
)
2622 return noteOverloads(S
, Overloads
, Loc
);
2624 UnresolvedSet
<2> PlausibleOverloads
;
2625 for (OverloadExpr::decls_iterator It
= Overloads
.begin(),
2626 DeclsEnd
= Overloads
.end(); It
!= DeclsEnd
; ++It
) {
2627 const auto *OverloadDecl
= cast
<FunctionDecl
>(*It
);
2628 QualType OverloadResultTy
= OverloadDecl
->getReturnType();
2629 if (IsPlausibleResult(OverloadResultTy
))
2630 PlausibleOverloads
.addDecl(It
.getDecl());
2632 noteOverloads(S
, PlausibleOverloads
, Loc
);
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 static bool IsCallableWithAppend(const Expr
*E
) {
2640 E
= E
->IgnoreImplicit();
2641 return (!isa
<CStyleCastExpr
>(E
) &&
2642 !isa
<UnaryOperator
>(E
) &&
2643 !isa
<BinaryOperator
>(E
) &&
2644 !isa
<CXXOperatorCallExpr
>(E
));
2647 static bool IsCPUDispatchCPUSpecificMultiVersion(const Expr
*E
) {
2648 if (const auto *UO
= dyn_cast
<UnaryOperator
>(E
))
2649 E
= UO
->getSubExpr();
2651 if (const auto *ULE
= dyn_cast
<UnresolvedLookupExpr
>(E
)) {
2652 if (ULE
->getNumDecls() == 0)
2655 const NamedDecl
*ND
= *ULE
->decls_begin();
2656 if (const auto *FD
= dyn_cast
<FunctionDecl
>(ND
))
2657 return FD
->isCPUDispatchMultiVersion() || FD
->isCPUSpecificMultiVersion();
2662 bool Sema::tryToRecoverWithCall(ExprResult
&E
, const PartialDiagnostic
&PD
,
2664 bool (*IsPlausibleResult
)(QualType
)) {
2665 SourceLocation Loc
= E
.get()->getExprLoc();
2666 SourceRange Range
= E
.get()->getSourceRange();
2667 UnresolvedSet
<4> Overloads
;
2669 // If this is a SFINAE context, don't try anything that might trigger ADL
2671 if (!isSFINAEContext()) {
2672 QualType ZeroArgCallTy
;
2673 if (tryExprAsCall(*E
.get(), ZeroArgCallTy
, Overloads
) &&
2674 !ZeroArgCallTy
.isNull() &&
2675 (!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 SourceLocation ParenInsertionLoc
= getLocForEndOfToken(Range
.getEnd());
2681 bool IsMV
= IsCPUDispatchCPUSpecificMultiVersion(E
.get());
2682 Diag(Loc
, PD
) << /*zero-arg*/ 1 << IsMV
<< Range
2683 << (IsCallableWithAppend(E
.get())
2684 ? FixItHint::CreateInsertion(ParenInsertionLoc
,
2688 notePlausibleOverloads(*this, Loc
, Overloads
, IsPlausibleResult
);
2690 // FIXME: Try this before emitting the fixit, and suppress diagnostics
2692 E
= BuildCallExpr(nullptr, E
.get(), Range
.getEnd(), std::nullopt
,
2693 Range
.getEnd().getLocWithOffset(1));
2697 if (!ForceComplain
) return false;
2699 bool IsMV
= IsCPUDispatchCPUSpecificMultiVersion(E
.get());
2700 Diag(Loc
, PD
) << /*not zero-arg*/ 0 << IsMV
<< Range
;
2702 notePlausibleOverloads(*this, Loc
, Overloads
, IsPlausibleResult
);
2707 IdentifierInfo
*Sema::getSuperIdentifier() const {
2709 Ident_super
= &Context
.Idents
.get("super");
2713 void Sema::PushCapturedRegionScope(Scope
*S
, CapturedDecl
*CD
, RecordDecl
*RD
,
2714 CapturedRegionKind K
,
2715 unsigned OpenMPCaptureLevel
) {
2716 auto *CSI
= new CapturedRegionScopeInfo(
2717 getDiagnostics(), S
, CD
, RD
, CD
->getContextParam(), K
,
2718 (getLangOpts().OpenMP
&& K
== CR_OpenMP
) ? getOpenMPNestingLevel() : 0,
2719 OpenMPCaptureLevel
);
2720 CSI
->ReturnType
= Context
.VoidTy
;
2721 FunctionScopes
.push_back(CSI
);
2722 CapturingFunctionScopes
++;
2725 CapturedRegionScopeInfo
*Sema::getCurCapturedRegion() {
2726 if (FunctionScopes
.empty())
2729 return dyn_cast
<CapturedRegionScopeInfo
>(FunctionScopes
.back());
2732 const llvm::MapVector
<FieldDecl
*, Sema::DeleteLocs
> &
2733 Sema::getMismatchingDeleteExpressions() const {
2737 Sema::FPFeaturesStateRAII::FPFeaturesStateRAII(Sema
&S
)
2738 : S(S
), OldFPFeaturesState(S
.CurFPFeatures
),
2739 OldOverrides(S
.FpPragmaStack
.CurrentValue
),
2740 OldEvalMethod(S
.PP
.getCurrentFPEvalMethod()),
2741 OldFPPragmaLocation(S
.PP
.getLastFPEvalPragmaLocation()) {}
2743 Sema::FPFeaturesStateRAII::~FPFeaturesStateRAII() {
2744 S
.CurFPFeatures
= OldFPFeaturesState
;
2745 S
.FpPragmaStack
.CurrentValue
= OldOverrides
;
2746 S
.PP
.setCurrentFPEvalMethod(OldFPPragmaLocation
, OldEvalMethod
);
2749 bool Sema::isDeclaratorFunctionLike(Declarator
&D
) {
2750 assert(D
.getCXXScopeSpec().isSet() &&
2751 "can only be called for qualified names");
2753 auto LR
= LookupResult(*this, D
.getIdentifier(), D
.getBeginLoc(),
2754 LookupOrdinaryName
, forRedeclarationInCurContext());
2755 DeclContext
*DC
= computeDeclContext(D
.getCXXScopeSpec(),
2756 !D
.getDeclSpec().isFriendSpecified());
2760 LookupQualifiedName(LR
, DC
);
2761 bool Result
= std::all_of(LR
.begin(), LR
.end(), [](Decl
*Dcl
) {
2762 if (NamedDecl
*ND
= dyn_cast
<NamedDecl
>(Dcl
)) {
2763 ND
= ND
->getUnderlyingDecl();
2764 return isa
<FunctionDecl
>(ND
) || isa
<FunctionTemplateDecl
>(ND
) ||