10b57cec5SDimitry Andric //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the actions class which performs semantic analysis and 100b57cec5SDimitry Andric // builds an AST out of a parse stream. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 145ffd83dbSDimitry Andric #include "UsedDeclVisitor.h" 150b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 160b57cec5SDimitry Andric #include "clang/AST/ASTDiagnostic.h" 17d409305fSDimitry Andric #include "clang/AST/Decl.h" 180b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h" 190b57cec5SDimitry Andric #include "clang/AST/DeclFriend.h" 200b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h" 210b57cec5SDimitry Andric #include "clang/AST/Expr.h" 220b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 230b57cec5SDimitry Andric #include "clang/AST/PrettyDeclStackTrace.h" 240b57cec5SDimitry Andric #include "clang/AST/StmtCXX.h" 25fe6060f1SDimitry Andric #include "clang/Basic/DarwinSDKInfo.h" 260b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h" 270b57cec5SDimitry Andric #include "clang/Basic/PartialDiagnostic.h" 285ffd83dbSDimitry Andric #include "clang/Basic/SourceManager.h" 29a7dea167SDimitry Andric #include "clang/Basic/Stack.h" 300b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h" 310b57cec5SDimitry Andric #include "clang/Lex/HeaderSearch.h" 32fe6060f1SDimitry Andric #include "clang/Lex/HeaderSearchOptions.h" 330b57cec5SDimitry Andric #include "clang/Lex/Preprocessor.h" 340b57cec5SDimitry Andric #include "clang/Sema/CXXFieldCollector.h" 350b57cec5SDimitry Andric #include "clang/Sema/DelayedDiagnostic.h" 360b57cec5SDimitry Andric #include "clang/Sema/ExternalSemaSource.h" 370b57cec5SDimitry Andric #include "clang/Sema/Initialization.h" 380b57cec5SDimitry Andric #include "clang/Sema/MultiplexExternalSemaSource.h" 390b57cec5SDimitry Andric #include "clang/Sema/ObjCMethodList.h" 400b57cec5SDimitry Andric #include "clang/Sema/Scope.h" 410b57cec5SDimitry Andric #include "clang/Sema/ScopeInfo.h" 420b57cec5SDimitry Andric #include "clang/Sema/SemaConsumer.h" 430b57cec5SDimitry Andric #include "clang/Sema/SemaInternal.h" 440b57cec5SDimitry Andric #include "clang/Sema/TemplateDeduction.h" 450b57cec5SDimitry Andric #include "clang/Sema/TemplateInstCallback.h" 46a7dea167SDimitry Andric #include "clang/Sema/TypoCorrection.h" 470b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 48e8d8bef9SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 490b57cec5SDimitry Andric #include "llvm/Support/TimeProfiler.h" 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric using namespace clang; 520b57cec5SDimitry Andric using namespace sema; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) { 550b57cec5SDimitry Andric return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts); 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); } 590b57cec5SDimitry Andric 60fe6060f1SDimitry Andric DarwinSDKInfo * 61fe6060f1SDimitry Andric Sema::getDarwinSDKInfoForAvailabilityChecking(SourceLocation Loc, 62fe6060f1SDimitry Andric StringRef Platform) { 63fe6060f1SDimitry Andric if (CachedDarwinSDKInfo) 64fe6060f1SDimitry Andric return CachedDarwinSDKInfo->get(); 65fe6060f1SDimitry Andric auto SDKInfo = parseDarwinSDKInfo( 66fe6060f1SDimitry Andric PP.getFileManager().getVirtualFileSystem(), 67fe6060f1SDimitry Andric PP.getHeaderSearchInfo().getHeaderSearchOpts().Sysroot); 68fe6060f1SDimitry Andric if (SDKInfo && *SDKInfo) { 69fe6060f1SDimitry Andric CachedDarwinSDKInfo = std::make_unique<DarwinSDKInfo>(std::move(**SDKInfo)); 70fe6060f1SDimitry Andric return CachedDarwinSDKInfo->get(); 71fe6060f1SDimitry Andric } 72fe6060f1SDimitry Andric if (!SDKInfo) 73fe6060f1SDimitry Andric llvm::consumeError(SDKInfo.takeError()); 74fe6060f1SDimitry Andric Diag(Loc, diag::warn_missing_sdksettings_for_availability_checking) 75fe6060f1SDimitry Andric << Platform; 76fe6060f1SDimitry Andric CachedDarwinSDKInfo = std::unique_ptr<DarwinSDKInfo>(); 77fe6060f1SDimitry Andric return nullptr; 78fe6060f1SDimitry Andric } 79fe6060f1SDimitry Andric 8055e4f9d5SDimitry Andric IdentifierInfo * 8155e4f9d5SDimitry Andric Sema::InventAbbreviatedTemplateParameterTypeName(IdentifierInfo *ParamName, 8255e4f9d5SDimitry Andric unsigned int Index) { 8355e4f9d5SDimitry Andric std::string InventedName; 8455e4f9d5SDimitry Andric llvm::raw_string_ostream OS(InventedName); 8555e4f9d5SDimitry Andric 8655e4f9d5SDimitry Andric if (!ParamName) 8755e4f9d5SDimitry Andric OS << "auto:" << Index + 1; 8855e4f9d5SDimitry Andric else 8955e4f9d5SDimitry Andric OS << ParamName->getName() << ":auto"; 9055e4f9d5SDimitry Andric 9155e4f9d5SDimitry Andric OS.flush(); 9255e4f9d5SDimitry Andric return &Context.Idents.get(OS.str()); 9355e4f9d5SDimitry Andric } 9455e4f9d5SDimitry Andric 950b57cec5SDimitry Andric PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context, 960b57cec5SDimitry Andric const Preprocessor &PP) { 970b57cec5SDimitry Andric PrintingPolicy Policy = Context.getPrintingPolicy(); 980b57cec5SDimitry Andric // In diagnostics, we print _Bool as bool if the latter is defined as the 990b57cec5SDimitry Andric // former. 1000b57cec5SDimitry Andric Policy.Bool = Context.getLangOpts().Bool; 1010b57cec5SDimitry Andric if (!Policy.Bool) { 1020b57cec5SDimitry Andric if (const MacroInfo *BoolMacro = PP.getMacroInfo(Context.getBoolName())) { 1030b57cec5SDimitry Andric Policy.Bool = BoolMacro->isObjectLike() && 1040b57cec5SDimitry Andric BoolMacro->getNumTokens() == 1 && 1050b57cec5SDimitry Andric BoolMacro->getReplacementToken(0).is(tok::kw__Bool); 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric return Policy; 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric void Sema::ActOnTranslationUnitScope(Scope *S) { 1130b57cec5SDimitry Andric TUScope = S; 1140b57cec5SDimitry Andric PushDeclContext(S, Context.getTranslationUnitDecl()); 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric namespace clang { 1180b57cec5SDimitry Andric namespace sema { 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric class SemaPPCallbacks : public PPCallbacks { 1210b57cec5SDimitry Andric Sema *S = nullptr; 1220b57cec5SDimitry Andric llvm::SmallVector<SourceLocation, 8> IncludeStack; 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric public: 1250b57cec5SDimitry Andric void set(Sema &S) { this->S = &S; } 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric void reset() { S = nullptr; } 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 1300b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType, 1310b57cec5SDimitry Andric FileID PrevFID) override { 1320b57cec5SDimitry Andric if (!S) 1330b57cec5SDimitry Andric return; 1340b57cec5SDimitry Andric switch (Reason) { 1350b57cec5SDimitry Andric case EnterFile: { 1360b57cec5SDimitry Andric SourceManager &SM = S->getSourceManager(); 1370b57cec5SDimitry Andric SourceLocation IncludeLoc = SM.getIncludeLoc(SM.getFileID(Loc)); 1380b57cec5SDimitry Andric if (IncludeLoc.isValid()) { 1390b57cec5SDimitry Andric if (llvm::timeTraceProfilerEnabled()) { 1400b57cec5SDimitry Andric const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc)); 1410b57cec5SDimitry Andric llvm::timeTraceProfilerBegin( 1420b57cec5SDimitry Andric "Source", FE != nullptr ? FE->getName() : StringRef("<unknown>")); 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric IncludeStack.push_back(IncludeLoc); 146e8d8bef9SDimitry Andric S->DiagnoseNonDefaultPragmaAlignPack( 147e8d8bef9SDimitry Andric Sema::PragmaAlignPackDiagnoseKind::NonDefaultStateAtInclude, 148e8d8bef9SDimitry Andric IncludeLoc); 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric break; 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric case ExitFile: 1530b57cec5SDimitry Andric if (!IncludeStack.empty()) { 1540b57cec5SDimitry Andric if (llvm::timeTraceProfilerEnabled()) 1550b57cec5SDimitry Andric llvm::timeTraceProfilerEnd(); 1560b57cec5SDimitry Andric 157e8d8bef9SDimitry Andric S->DiagnoseNonDefaultPragmaAlignPack( 158e8d8bef9SDimitry Andric Sema::PragmaAlignPackDiagnoseKind::ChangedStateAtExit, 1590b57cec5SDimitry Andric IncludeStack.pop_back_val()); 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric break; 1620b57cec5SDimitry Andric default: 1630b57cec5SDimitry Andric break; 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric }; 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric } // end namespace sema 1690b57cec5SDimitry Andric } // end namespace clang 1700b57cec5SDimitry Andric 1715ffd83dbSDimitry Andric const unsigned Sema::MaxAlignmentExponent; 1725ffd83dbSDimitry Andric const unsigned Sema::MaximumAlignment; 1735ffd83dbSDimitry Andric 1740b57cec5SDimitry Andric Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, 1750b57cec5SDimitry Andric TranslationUnitKind TUKind, CodeCompleteConsumer *CodeCompleter) 1760b57cec5SDimitry Andric : ExternalSource(nullptr), isMultiplexExternalSource(false), 1775ffd83dbSDimitry Andric CurFPFeatures(pp.getLangOpts()), LangOpts(pp.getLangOpts()), PP(pp), 1780b57cec5SDimitry Andric Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()), 1790b57cec5SDimitry Andric SourceMgr(PP.getSourceManager()), CollectStats(false), 1800b57cec5SDimitry Andric CodeCompleter(CodeCompleter), CurContext(nullptr), 1810b57cec5SDimitry Andric OriginalLexicalContext(nullptr), MSStructPragmaOn(false), 1820b57cec5SDimitry Andric MSPointerToMemberRepresentationMethod( 1830b57cec5SDimitry Andric LangOpts.getMSPointerToMemberRepresentationMethod()), 184e8d8bef9SDimitry Andric VtorDispStack(LangOpts.getVtorDispMode()), 185e8d8bef9SDimitry Andric AlignPackStack(AlignPackInfo(getLangOpts().XLPragmaPack)), 1860b57cec5SDimitry Andric DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr), 187e8d8bef9SDimitry Andric CodeSegStack(nullptr), FpPragmaStack(FPOptionsOverride()), 188e8d8bef9SDimitry Andric CurInitSeg(nullptr), VisContext(nullptr), 189e8d8bef9SDimitry Andric PragmaAttributeCurrentTargetDecl(nullptr), 1900b57cec5SDimitry Andric IsBuildingRecoveryCallExpr(false), Cleanup{}, LateTemplateParser(nullptr), 1910b57cec5SDimitry Andric LateTemplateParserCleanup(nullptr), OpaqueParser(nullptr), IdResolver(pp), 1920b57cec5SDimitry Andric StdExperimentalNamespaceCache(nullptr), StdInitializerList(nullptr), 1930b57cec5SDimitry Andric StdCoroutineTraitsCache(nullptr), CXXTypeInfoDecl(nullptr), 1940b57cec5SDimitry Andric MSVCGuidDecl(nullptr), NSNumberDecl(nullptr), NSValueDecl(nullptr), 1950b57cec5SDimitry Andric NSStringDecl(nullptr), StringWithUTF8StringMethod(nullptr), 1960b57cec5SDimitry Andric ValueWithBytesObjCTypeMethod(nullptr), NSArrayDecl(nullptr), 1970b57cec5SDimitry Andric ArrayWithObjectsMethod(nullptr), NSDictionaryDecl(nullptr), 1980b57cec5SDimitry Andric DictionaryWithObjectsMethod(nullptr), GlobalNewDeleteDeclared(false), 1990b57cec5SDimitry Andric TUKind(TUKind), NumSFINAEErrors(0), 2000b57cec5SDimitry Andric FullyCheckedComparisonCategories( 2010b57cec5SDimitry Andric static_cast<unsigned>(ComparisonCategoryType::Last) + 1), 20255e4f9d5SDimitry Andric SatisfactionCache(Context), AccessCheckingSFINAE(false), 20355e4f9d5SDimitry Andric InNonInstantiationSFINAEContext(false), NonInstantiationEntries(0), 20455e4f9d5SDimitry Andric ArgumentPackSubstitutionIndex(-1), CurrentInstantiationScope(nullptr), 20555e4f9d5SDimitry Andric DisableTypoCorrection(false), TyposCorrected(0), AnalysisWarnings(*this), 2060b57cec5SDimitry Andric ThreadSafetyDeclCache(nullptr), VarDataSharingAttributesStack(nullptr), 2070b57cec5SDimitry Andric CurScope(nullptr), Ident_super(nullptr), Ident___float128(nullptr) { 208fe6060f1SDimitry Andric assert(pp.TUKind == TUKind); 2090b57cec5SDimitry Andric TUScope = nullptr; 2100b57cec5SDimitry Andric isConstantEvaluatedOverride = false; 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric LoadedExternalKnownNamespaces = false; 2130b57cec5SDimitry Andric for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I) 2140b57cec5SDimitry Andric NSNumberLiteralMethods[I] = nullptr; 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric if (getLangOpts().ObjC) 2170b57cec5SDimitry Andric NSAPIObj.reset(new NSAPI(Context)); 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric if (getLangOpts().CPlusPlus) 2200b57cec5SDimitry Andric FieldCollector.reset(new CXXFieldCollector()); 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric // Tell diagnostics how to render things from the AST library. 2230b57cec5SDimitry Andric Diags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument, &Context); 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric ExprEvalContexts.emplace_back( 2260b57cec5SDimitry Andric ExpressionEvaluationContext::PotentiallyEvaluated, 0, CleanupInfo{}, 2270b57cec5SDimitry Andric nullptr, ExpressionEvaluationContextRecord::EK_Other); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric // Initialization of data sharing attributes stack for OpenMP 2300b57cec5SDimitry Andric InitDataSharingAttributesStack(); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric std::unique_ptr<sema::SemaPPCallbacks> Callbacks = 233a7dea167SDimitry Andric std::make_unique<sema::SemaPPCallbacks>(); 2340b57cec5SDimitry Andric SemaPPCallbackHandler = Callbacks.get(); 2350b57cec5SDimitry Andric PP.addPPCallbacks(std::move(Callbacks)); 2360b57cec5SDimitry Andric SemaPPCallbackHandler->set(*this); 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 239480093f4SDimitry Andric // Anchor Sema's type info to this TU. 240480093f4SDimitry Andric void Sema::anchor() {} 241480093f4SDimitry Andric 2420b57cec5SDimitry Andric void Sema::addImplicitTypedef(StringRef Name, QualType T) { 2430b57cec5SDimitry Andric DeclarationName DN = &Context.Idents.get(Name); 2440b57cec5SDimitry Andric if (IdResolver.begin(DN) == IdResolver.end()) 2450b57cec5SDimitry Andric PushOnScopeChains(Context.buildImplicitTypedef(T, Name), TUScope); 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric void Sema::Initialize() { 2490b57cec5SDimitry Andric if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) 2500b57cec5SDimitry Andric SC->InitializeSema(*this); 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric // Tell the external Sema source about this Sema object. 2530b57cec5SDimitry Andric if (ExternalSemaSource *ExternalSema 2540b57cec5SDimitry Andric = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) 2550b57cec5SDimitry Andric ExternalSema->InitializeSema(*this); 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric // This needs to happen after ExternalSemaSource::InitializeSema(this) or we 2580b57cec5SDimitry Andric // will not be able to merge any duplicate __va_list_tag decls correctly. 2590b57cec5SDimitry Andric VAListTagName = PP.getIdentifierInfo("__va_list_tag"); 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric if (!TUScope) 2620b57cec5SDimitry Andric return; 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric // Initialize predefined 128-bit integer types, if needed. 265e8d8bef9SDimitry Andric if (Context.getTargetInfo().hasInt128Type() || 266e8d8bef9SDimitry Andric (Context.getAuxTargetInfo() && 267e8d8bef9SDimitry Andric Context.getAuxTargetInfo()->hasInt128Type())) { 2680b57cec5SDimitry Andric // If either of the 128-bit integer types are unavailable to name lookup, 2690b57cec5SDimitry Andric // define them now. 2700b57cec5SDimitry Andric DeclarationName Int128 = &Context.Idents.get("__int128_t"); 2710b57cec5SDimitry Andric if (IdResolver.begin(Int128) == IdResolver.end()) 2720b57cec5SDimitry Andric PushOnScopeChains(Context.getInt128Decl(), TUScope); 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric DeclarationName UInt128 = &Context.Idents.get("__uint128_t"); 2750b57cec5SDimitry Andric if (IdResolver.begin(UInt128) == IdResolver.end()) 2760b57cec5SDimitry Andric PushOnScopeChains(Context.getUInt128Decl(), TUScope); 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric // Initialize predefined Objective-C types: 2810b57cec5SDimitry Andric if (getLangOpts().ObjC) { 2820b57cec5SDimitry Andric // If 'SEL' does not yet refer to any declarations, make it refer to the 2830b57cec5SDimitry Andric // predefined 'SEL'. 2840b57cec5SDimitry Andric DeclarationName SEL = &Context.Idents.get("SEL"); 2850b57cec5SDimitry Andric if (IdResolver.begin(SEL) == IdResolver.end()) 2860b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCSelDecl(), TUScope); 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric // If 'id' does not yet refer to any declarations, make it refer to the 2890b57cec5SDimitry Andric // predefined 'id'. 2900b57cec5SDimitry Andric DeclarationName Id = &Context.Idents.get("id"); 2910b57cec5SDimitry Andric if (IdResolver.begin(Id) == IdResolver.end()) 2920b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCIdDecl(), TUScope); 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric // Create the built-in typedef for 'Class'. 2950b57cec5SDimitry Andric DeclarationName Class = &Context.Idents.get("Class"); 2960b57cec5SDimitry Andric if (IdResolver.begin(Class) == IdResolver.end()) 2970b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCClassDecl(), TUScope); 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric // Create the built-in forward declaratino for 'Protocol'. 3000b57cec5SDimitry Andric DeclarationName Protocol = &Context.Idents.get("Protocol"); 3010b57cec5SDimitry Andric if (IdResolver.begin(Protocol) == IdResolver.end()) 3020b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope); 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric // Create the internal type for the *StringMakeConstantString builtins. 3060b57cec5SDimitry Andric DeclarationName ConstantString = &Context.Idents.get("__NSConstantString"); 3070b57cec5SDimitry Andric if (IdResolver.begin(ConstantString) == IdResolver.end()) 3080b57cec5SDimitry Andric PushOnScopeChains(Context.getCFConstantStringDecl(), TUScope); 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric // Initialize Microsoft "predefined C++ types". 3110b57cec5SDimitry Andric if (getLangOpts().MSVCCompat) { 3120b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && 3130b57cec5SDimitry Andric IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end()) 3140b57cec5SDimitry Andric PushOnScopeChains(Context.buildImplicitRecord("type_info", TTK_Class), 3150b57cec5SDimitry Andric TUScope); 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric addImplicitTypedef("size_t", Context.getSizeType()); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric // Initialize predefined OpenCL types and supported extensions and (optional) 3210b57cec5SDimitry Andric // core features. 3220b57cec5SDimitry Andric if (getLangOpts().OpenCL) { 3230b57cec5SDimitry Andric getOpenCLOptions().addSupport( 324e8d8bef9SDimitry Andric Context.getTargetInfo().getSupportedOpenCLOpts(), getLangOpts()); 3250b57cec5SDimitry Andric addImplicitTypedef("sampler_t", Context.OCLSamplerTy); 3260b57cec5SDimitry Andric addImplicitTypedef("event_t", Context.OCLEventTy); 3270b57cec5SDimitry Andric if (getLangOpts().OpenCLCPlusPlus || getLangOpts().OpenCLVersion >= 200) { 3280b57cec5SDimitry Andric addImplicitTypedef("clk_event_t", Context.OCLClkEventTy); 3290b57cec5SDimitry Andric addImplicitTypedef("queue_t", Context.OCLQueueTy); 330*6e75b2fbSDimitry Andric if (getLangOpts().OpenCLPipes) 3310b57cec5SDimitry Andric addImplicitTypedef("reserve_id_t", Context.OCLReserveIDTy); 3320b57cec5SDimitry Andric addImplicitTypedef("atomic_int", Context.getAtomicType(Context.IntTy)); 3330b57cec5SDimitry Andric addImplicitTypedef("atomic_uint", 3340b57cec5SDimitry Andric Context.getAtomicType(Context.UnsignedIntTy)); 3350b57cec5SDimitry Andric addImplicitTypedef("atomic_float", 3360b57cec5SDimitry Andric Context.getAtomicType(Context.FloatTy)); 3370b57cec5SDimitry Andric // OpenCLC v2.0, s6.13.11.6 requires that atomic_flag is implemented as 3380b57cec5SDimitry Andric // 32-bit integer and OpenCLC v2.0, s6.1.1 int is always 32-bit wide. 3390b57cec5SDimitry Andric addImplicitTypedef("atomic_flag", Context.getAtomicType(Context.IntTy)); 340fe6060f1SDimitry Andric 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric // OpenCL v2.0 s6.13.11.6: 3430b57cec5SDimitry Andric // - The atomic_long and atomic_ulong types are supported if the 3440b57cec5SDimitry Andric // cl_khr_int64_base_atomics and cl_khr_int64_extended_atomics 3450b57cec5SDimitry Andric // extensions are supported. 3460b57cec5SDimitry Andric // - The atomic_double type is only supported if double precision 3470b57cec5SDimitry Andric // is supported and the cl_khr_int64_base_atomics and 3480b57cec5SDimitry Andric // cl_khr_int64_extended_atomics extensions are supported. 3490b57cec5SDimitry Andric // - If the device address space is 64-bits, the data types 3500b57cec5SDimitry Andric // atomic_intptr_t, atomic_uintptr_t, atomic_size_t and 3510b57cec5SDimitry Andric // atomic_ptrdiff_t are supported if the cl_khr_int64_base_atomics and 3520b57cec5SDimitry Andric // cl_khr_int64_extended_atomics extensions are supported. 353fe6060f1SDimitry Andric 354fe6060f1SDimitry Andric auto AddPointerSizeDependentTypes = [&]() { 355fe6060f1SDimitry Andric auto AtomicSizeT = Context.getAtomicType(Context.getSizeType()); 356fe6060f1SDimitry Andric auto AtomicIntPtrT = Context.getAtomicType(Context.getIntPtrType()); 357fe6060f1SDimitry Andric auto AtomicUIntPtrT = Context.getAtomicType(Context.getUIntPtrType()); 358fe6060f1SDimitry Andric auto AtomicPtrDiffT = 359fe6060f1SDimitry Andric Context.getAtomicType(Context.getPointerDiffType()); 360fe6060f1SDimitry Andric addImplicitTypedef("atomic_size_t", AtomicSizeT); 361fe6060f1SDimitry Andric addImplicitTypedef("atomic_intptr_t", AtomicIntPtrT); 362fe6060f1SDimitry Andric addImplicitTypedef("atomic_uintptr_t", AtomicUIntPtrT); 363fe6060f1SDimitry Andric addImplicitTypedef("atomic_ptrdiff_t", AtomicPtrDiffT); 364fe6060f1SDimitry Andric }; 365fe6060f1SDimitry Andric 366fe6060f1SDimitry Andric if (Context.getTypeSize(Context.getSizeType()) == 32) { 367fe6060f1SDimitry Andric AddPointerSizeDependentTypes(); 368fe6060f1SDimitry Andric } 369fe6060f1SDimitry Andric 3700b57cec5SDimitry Andric std::vector<QualType> Atomic64BitTypes; 371fe6060f1SDimitry Andric if (getOpenCLOptions().isSupported("cl_khr_int64_base_atomics", 372fe6060f1SDimitry Andric getLangOpts()) && 373fe6060f1SDimitry Andric getOpenCLOptions().isSupported("cl_khr_int64_extended_atomics", 374fe6060f1SDimitry Andric getLangOpts())) { 375fe6060f1SDimitry Andric if (getOpenCLOptions().isSupported("cl_khr_fp64", getLangOpts())) { 376fe6060f1SDimitry Andric auto AtomicDoubleT = Context.getAtomicType(Context.DoubleTy); 377fe6060f1SDimitry Andric addImplicitTypedef("atomic_double", AtomicDoubleT); 3780b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicDoubleT); 3790b57cec5SDimitry Andric } 380fe6060f1SDimitry Andric auto AtomicLongT = Context.getAtomicType(Context.LongTy); 381fe6060f1SDimitry Andric auto AtomicULongT = Context.getAtomicType(Context.UnsignedLongTy); 382fe6060f1SDimitry Andric addImplicitTypedef("atomic_long", AtomicLongT); 383fe6060f1SDimitry Andric addImplicitTypedef("atomic_ulong", AtomicULongT); 3840b57cec5SDimitry Andric 385fe6060f1SDimitry Andric 386fe6060f1SDimitry Andric if (Context.getTypeSize(Context.getSizeType()) == 64) { 387fe6060f1SDimitry Andric AddPointerSizeDependentTypes(); 388fe6060f1SDimitry Andric } 389fe6060f1SDimitry Andric } 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 394fe6060f1SDimitry Andric if (getOpenCLOptions().isSupported(#Ext, getLangOpts())) { \ 3950b57cec5SDimitry Andric addImplicitTypedef(#ExtType, Context.Id##Ty); \ 396fe6060f1SDimitry Andric } 3970b57cec5SDimitry Andric #include "clang/Basic/OpenCLExtensionTypes.def" 398a7dea167SDimitry Andric } 399a7dea167SDimitry Andric 400a7dea167SDimitry Andric if (Context.getTargetInfo().hasAArch64SVETypes()) { 401a7dea167SDimitry Andric #define SVE_TYPE(Name, Id, SingletonId) \ 402a7dea167SDimitry Andric addImplicitTypedef(Name, Context.SingletonId); 403a7dea167SDimitry Andric #include "clang/Basic/AArch64SVEACLETypes.def" 404a7dea167SDimitry Andric } 4050b57cec5SDimitry Andric 406e8d8bef9SDimitry Andric if (Context.getTargetInfo().getTriple().isPPC64() && 407e8d8bef9SDimitry Andric Context.getTargetInfo().hasFeature("paired-vector-memops")) { 408e8d8bef9SDimitry Andric if (Context.getTargetInfo().hasFeature("mma")) { 409e8d8bef9SDimitry Andric #define PPC_VECTOR_MMA_TYPE(Name, Id, Size) \ 410e8d8bef9SDimitry Andric addImplicitTypedef(#Name, Context.Id##Ty); 411e8d8bef9SDimitry Andric #include "clang/Basic/PPCTypes.def" 412e8d8bef9SDimitry Andric } 413e8d8bef9SDimitry Andric #define PPC_VECTOR_VSX_TYPE(Name, Id, Size) \ 414e8d8bef9SDimitry Andric addImplicitTypedef(#Name, Context.Id##Ty); 415e8d8bef9SDimitry Andric #include "clang/Basic/PPCTypes.def" 416e8d8bef9SDimitry Andric } 417e8d8bef9SDimitry Andric 418fe6060f1SDimitry Andric if (Context.getTargetInfo().hasRISCVVTypes()) { 419fe6060f1SDimitry Andric #define RVV_TYPE(Name, Id, SingletonId) \ 420fe6060f1SDimitry Andric addImplicitTypedef(Name, Context.SingletonId); 421fe6060f1SDimitry Andric #include "clang/Basic/RISCVVTypes.def" 422fe6060f1SDimitry Andric } 423fe6060f1SDimitry Andric 4240b57cec5SDimitry Andric if (Context.getTargetInfo().hasBuiltinMSVaList()) { 4250b57cec5SDimitry Andric DeclarationName MSVaList = &Context.Idents.get("__builtin_ms_va_list"); 4260b57cec5SDimitry Andric if (IdResolver.begin(MSVaList) == IdResolver.end()) 4270b57cec5SDimitry Andric PushOnScopeChains(Context.getBuiltinMSVaListDecl(), TUScope); 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list"); 4310b57cec5SDimitry Andric if (IdResolver.begin(BuiltinVaList) == IdResolver.end()) 4320b57cec5SDimitry Andric PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope); 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric Sema::~Sema() { 436e8d8bef9SDimitry Andric assert(InstantiatingSpecializations.empty() && 437e8d8bef9SDimitry Andric "failed to clean up an InstantiatingTemplate?"); 438e8d8bef9SDimitry Andric 4390b57cec5SDimitry Andric if (VisContext) FreeVisContext(); 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric // Kill all the active scopes. 4420b57cec5SDimitry Andric for (sema::FunctionScopeInfo *FSI : FunctionScopes) 4430b57cec5SDimitry Andric delete FSI; 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric // Tell the SemaConsumer to forget about us; we're going out of scope. 4460b57cec5SDimitry Andric if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) 4470b57cec5SDimitry Andric SC->ForgetSema(); 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric // Detach from the external Sema source. 4500b57cec5SDimitry Andric if (ExternalSemaSource *ExternalSema 4510b57cec5SDimitry Andric = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) 4520b57cec5SDimitry Andric ExternalSema->ForgetSema(); 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric // If Sema's ExternalSource is the multiplexer - we own it. 4550b57cec5SDimitry Andric if (isMultiplexExternalSource) 4560b57cec5SDimitry Andric delete ExternalSource; 4570b57cec5SDimitry Andric 45855e4f9d5SDimitry Andric // Delete cached satisfactions. 45955e4f9d5SDimitry Andric std::vector<ConstraintSatisfaction *> Satisfactions; 46055e4f9d5SDimitry Andric Satisfactions.reserve(Satisfactions.size()); 46155e4f9d5SDimitry Andric for (auto &Node : SatisfactionCache) 46255e4f9d5SDimitry Andric Satisfactions.push_back(&Node); 46355e4f9d5SDimitry Andric for (auto *Node : Satisfactions) 46455e4f9d5SDimitry Andric delete Node; 46555e4f9d5SDimitry Andric 4660b57cec5SDimitry Andric threadSafety::threadSafetyCleanup(ThreadSafetyDeclCache); 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric // Destroys data sharing attributes stack for OpenMP 4690b57cec5SDimitry Andric DestroyDataSharingAttributesStack(); 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // Detach from the PP callback handler which outlives Sema since it's owned 4720b57cec5SDimitry Andric // by the preprocessor. 4730b57cec5SDimitry Andric SemaPPCallbackHandler->reset(); 474a7dea167SDimitry Andric } 4750b57cec5SDimitry Andric 476a7dea167SDimitry Andric void Sema::warnStackExhausted(SourceLocation Loc) { 477a7dea167SDimitry Andric // Only warn about this once. 478a7dea167SDimitry Andric if (!WarnedStackExhausted) { 479a7dea167SDimitry Andric Diag(Loc, diag::warn_stack_exhausted); 480a7dea167SDimitry Andric WarnedStackExhausted = true; 481a7dea167SDimitry Andric } 482a7dea167SDimitry Andric } 483a7dea167SDimitry Andric 484a7dea167SDimitry Andric void Sema::runWithSufficientStackSpace(SourceLocation Loc, 485a7dea167SDimitry Andric llvm::function_ref<void()> Fn) { 486a7dea167SDimitry Andric clang::runWithSufficientStackSpace([&] { warnStackExhausted(Loc); }, Fn); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric /// makeUnavailableInSystemHeader - There is an error in the current 4900b57cec5SDimitry Andric /// context. If we're still in a system header, and we can plausibly 4910b57cec5SDimitry Andric /// make the relevant declaration unavailable instead of erroring, do 4920b57cec5SDimitry Andric /// so and return true. 4930b57cec5SDimitry Andric bool Sema::makeUnavailableInSystemHeader(SourceLocation loc, 4940b57cec5SDimitry Andric UnavailableAttr::ImplicitReason reason) { 4950b57cec5SDimitry Andric // If we're not in a function, it's an error. 4960b57cec5SDimitry Andric FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext); 4970b57cec5SDimitry Andric if (!fn) return false; 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // If we're in template instantiation, it's an error. 5000b57cec5SDimitry Andric if (inTemplateInstantiation()) 5010b57cec5SDimitry Andric return false; 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric // If that function's not in a system header, it's an error. 5040b57cec5SDimitry Andric if (!Context.getSourceManager().isInSystemHeader(loc)) 5050b57cec5SDimitry Andric return false; 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric // If the function is already unavailable, it's not an error. 5080b57cec5SDimitry Andric if (fn->hasAttr<UnavailableAttr>()) return true; 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric fn->addAttr(UnavailableAttr::CreateImplicit(Context, "", reason, loc)); 5110b57cec5SDimitry Andric return true; 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric ASTMutationListener *Sema::getASTMutationListener() const { 5150b57cec5SDimitry Andric return getASTConsumer().GetASTMutationListener(); 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric ///Registers an external source. If an external source already exists, 5190b57cec5SDimitry Andric /// creates a multiplex external source and appends to it. 5200b57cec5SDimitry Andric /// 5210b57cec5SDimitry Andric ///\param[in] E - A non-null external sema source. 5220b57cec5SDimitry Andric /// 5230b57cec5SDimitry Andric void Sema::addExternalSource(ExternalSemaSource *E) { 5240b57cec5SDimitry Andric assert(E && "Cannot use with NULL ptr"); 5250b57cec5SDimitry Andric 5260b57cec5SDimitry Andric if (!ExternalSource) { 5270b57cec5SDimitry Andric ExternalSource = E; 5280b57cec5SDimitry Andric return; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric if (isMultiplexExternalSource) 5320b57cec5SDimitry Andric static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E); 5330b57cec5SDimitry Andric else { 5340b57cec5SDimitry Andric ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E); 5350b57cec5SDimitry Andric isMultiplexExternalSource = true; 5360b57cec5SDimitry Andric } 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric /// Print out statistics about the semantic analysis. 5400b57cec5SDimitry Andric void Sema::PrintStats() const { 5410b57cec5SDimitry Andric llvm::errs() << "\n*** Semantic Analysis Stats:\n"; 5420b57cec5SDimitry Andric llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n"; 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric BumpAlloc.PrintStats(); 5450b57cec5SDimitry Andric AnalysisWarnings.PrintStats(); 5460b57cec5SDimitry Andric } 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric void Sema::diagnoseNullableToNonnullConversion(QualType DstType, 5490b57cec5SDimitry Andric QualType SrcType, 5500b57cec5SDimitry Andric SourceLocation Loc) { 5510b57cec5SDimitry Andric Optional<NullabilityKind> ExprNullability = SrcType->getNullability(Context); 552e8d8bef9SDimitry Andric if (!ExprNullability || (*ExprNullability != NullabilityKind::Nullable && 553e8d8bef9SDimitry Andric *ExprNullability != NullabilityKind::NullableResult)) 5540b57cec5SDimitry Andric return; 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric Optional<NullabilityKind> TypeNullability = DstType->getNullability(Context); 5570b57cec5SDimitry Andric if (!TypeNullability || *TypeNullability != NullabilityKind::NonNull) 5580b57cec5SDimitry Andric return; 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric Diag(Loc, diag::warn_nullability_lost) << SrcType << DstType; 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) { 5640b57cec5SDimitry Andric if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant, 5650b57cec5SDimitry Andric E->getBeginLoc())) 5660b57cec5SDimitry Andric return; 5670b57cec5SDimitry Andric // nullptr only exists from C++11 on, so don't warn on its absence earlier. 5680b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus11) 5690b57cec5SDimitry Andric return; 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer) 5720b57cec5SDimitry Andric return; 5730b57cec5SDimitry Andric if (E->IgnoreParenImpCasts()->getType()->isNullPtrType()) 5740b57cec5SDimitry Andric return; 5750b57cec5SDimitry Andric 576d409305fSDimitry Andric // Don't diagnose the conversion from a 0 literal to a null pointer argument 577d409305fSDimitry Andric // in a synthesized call to operator<=>. 578d409305fSDimitry Andric if (!CodeSynthesisContexts.empty() && 579d409305fSDimitry Andric CodeSynthesisContexts.back().Kind == 580d409305fSDimitry Andric CodeSynthesisContext::RewritingOperatorAsSpaceship) 581d409305fSDimitry Andric return; 582d409305fSDimitry Andric 5830b57cec5SDimitry Andric // If it is a macro from system header, and if the macro name is not "NULL", 5840b57cec5SDimitry Andric // do not warn. 5850b57cec5SDimitry Andric SourceLocation MaybeMacroLoc = E->getBeginLoc(); 5860b57cec5SDimitry Andric if (Diags.getSuppressSystemWarnings() && 5870b57cec5SDimitry Andric SourceMgr.isInSystemMacro(MaybeMacroLoc) && 5880b57cec5SDimitry Andric !findMacroSpelling(MaybeMacroLoc, "NULL")) 5890b57cec5SDimitry Andric return; 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric Diag(E->getBeginLoc(), diag::warn_zero_as_null_pointer_constant) 5920b57cec5SDimitry Andric << FixItHint::CreateReplacement(E->getSourceRange(), "nullptr"); 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 5960b57cec5SDimitry Andric /// If there is already an implicit cast, merge into the existing one. 5970b57cec5SDimitry Andric /// The result is of the given category. 5980b57cec5SDimitry Andric ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty, 5990b57cec5SDimitry Andric CastKind Kind, ExprValueKind VK, 6000b57cec5SDimitry Andric const CXXCastPath *BasePath, 6010b57cec5SDimitry Andric CheckedConversionKind CCK) { 6020b57cec5SDimitry Andric #ifndef NDEBUG 603fe6060f1SDimitry Andric if (VK == VK_PRValue && !E->isPRValue()) { 6040b57cec5SDimitry Andric switch (Kind) { 6050b57cec5SDimitry Andric default: 606fe6060f1SDimitry Andric llvm_unreachable( 607fe6060f1SDimitry Andric ("can't implicitly cast glvalue to prvalue with this cast " 608e8d8bef9SDimitry Andric "kind: " + 609e8d8bef9SDimitry Andric std::string(CastExpr::getCastKindName(Kind))) 610e8d8bef9SDimitry Andric .c_str()); 6110b57cec5SDimitry Andric case CK_Dependent: 6120b57cec5SDimitry Andric case CK_LValueToRValue: 6130b57cec5SDimitry Andric case CK_ArrayToPointerDecay: 6140b57cec5SDimitry Andric case CK_FunctionToPointerDecay: 6150b57cec5SDimitry Andric case CK_ToVoid: 6160b57cec5SDimitry Andric case CK_NonAtomicToAtomic: 6170b57cec5SDimitry Andric break; 6180b57cec5SDimitry Andric } 6190b57cec5SDimitry Andric } 620fe6060f1SDimitry Andric assert((VK == VK_PRValue || Kind == CK_Dependent || !E->isPRValue()) && 621fe6060f1SDimitry Andric "can't cast prvalue to glvalue"); 6220b57cec5SDimitry Andric #endif 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric diagnoseNullableToNonnullConversion(Ty, E->getType(), E->getBeginLoc()); 6250b57cec5SDimitry Andric diagnoseZeroToNullptrConversion(Kind, E); 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric QualType ExprTy = Context.getCanonicalType(E->getType()); 6280b57cec5SDimitry Andric QualType TypeTy = Context.getCanonicalType(Ty); 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric if (ExprTy == TypeTy) 6310b57cec5SDimitry Andric return E; 6320b57cec5SDimitry Andric 633fe6060f1SDimitry Andric if (Kind == CK_ArrayToPointerDecay) { 6340b57cec5SDimitry Andric // C++1z [conv.array]: The temporary materialization conversion is applied. 6350b57cec5SDimitry Andric // We also use this to fuel C++ DR1213, which applies to C++11 onwards. 636fe6060f1SDimitry Andric if (getLangOpts().CPlusPlus && E->isPRValue()) { 6370b57cec5SDimitry Andric // The temporary is an lvalue in C++98 and an xvalue otherwise. 6380b57cec5SDimitry Andric ExprResult Materialized = CreateMaterializeTemporaryExpr( 6390b57cec5SDimitry Andric E->getType(), E, !getLangOpts().CPlusPlus11); 6400b57cec5SDimitry Andric if (Materialized.isInvalid()) 6410b57cec5SDimitry Andric return ExprError(); 6420b57cec5SDimitry Andric E = Materialized.get(); 6430b57cec5SDimitry Andric } 644fe6060f1SDimitry Andric // C17 6.7.1p6 footnote 124: The implementation can treat any register 645fe6060f1SDimitry Andric // declaration simply as an auto declaration. However, whether or not 646fe6060f1SDimitry Andric // addressable storage is actually used, the address of any part of an 647fe6060f1SDimitry Andric // object declared with storage-class specifier register cannot be 648fe6060f1SDimitry Andric // computed, either explicitly(by use of the unary & operator as discussed 649fe6060f1SDimitry Andric // in 6.5.3.2) or implicitly(by converting an array name to a pointer as 650fe6060f1SDimitry Andric // discussed in 6.3.2.1).Thus, the only operator that can be applied to an 651fe6060f1SDimitry Andric // array declared with storage-class specifier register is sizeof. 652fe6060f1SDimitry Andric if (VK == VK_PRValue && !getLangOpts().CPlusPlus && !E->isPRValue()) { 653fe6060f1SDimitry Andric if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { 654fe6060f1SDimitry Andric if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 655fe6060f1SDimitry Andric if (VD->getStorageClass() == SC_Register) { 656fe6060f1SDimitry Andric Diag(E->getExprLoc(), diag::err_typecheck_address_of) 657fe6060f1SDimitry Andric << /*register variable*/ 3 << E->getSourceRange(); 658fe6060f1SDimitry Andric return ExprError(); 659fe6060f1SDimitry Andric } 660fe6060f1SDimitry Andric } 661fe6060f1SDimitry Andric } 662fe6060f1SDimitry Andric } 663fe6060f1SDimitry Andric } 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) { 6660b57cec5SDimitry Andric if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) { 6670b57cec5SDimitry Andric ImpCast->setType(Ty); 6680b57cec5SDimitry Andric ImpCast->setValueKind(VK); 6690b57cec5SDimitry Andric return E; 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric 673e8d8bef9SDimitry Andric return ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK, 674e8d8bef9SDimitry Andric CurFPFeatureOverrides()); 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding 6780b57cec5SDimitry Andric /// to the conversion from scalar type ScalarTy to the Boolean type. 6790b57cec5SDimitry Andric CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) { 6800b57cec5SDimitry Andric switch (ScalarTy->getScalarTypeKind()) { 6810b57cec5SDimitry Andric case Type::STK_Bool: return CK_NoOp; 6820b57cec5SDimitry Andric case Type::STK_CPointer: return CK_PointerToBoolean; 6830b57cec5SDimitry Andric case Type::STK_BlockPointer: return CK_PointerToBoolean; 6840b57cec5SDimitry Andric case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean; 6850b57cec5SDimitry Andric case Type::STK_MemberPointer: return CK_MemberPointerToBoolean; 6860b57cec5SDimitry Andric case Type::STK_Integral: return CK_IntegralToBoolean; 6870b57cec5SDimitry Andric case Type::STK_Floating: return CK_FloatingToBoolean; 6880b57cec5SDimitry Andric case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean; 6890b57cec5SDimitry Andric case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean; 6900b57cec5SDimitry Andric case Type::STK_FixedPoint: return CK_FixedPointToBoolean; 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric llvm_unreachable("unknown scalar type kind"); 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric /// Used to prune the decls of Sema's UnusedFileScopedDecls vector. 6960b57cec5SDimitry Andric static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { 6970b57cec5SDimitry Andric if (D->getMostRecentDecl()->isUsed()) 6980b57cec5SDimitry Andric return true; 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric if (D->isExternallyVisible()) 7010b57cec5SDimitry Andric return true; 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 7040b57cec5SDimitry Andric // If this is a function template and none of its specializations is used, 7050b57cec5SDimitry Andric // we should warn. 7060b57cec5SDimitry Andric if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate()) 7070b57cec5SDimitry Andric for (const auto *Spec : Template->specializations()) 7080b57cec5SDimitry Andric if (ShouldRemoveFromUnused(SemaRef, Spec)) 7090b57cec5SDimitry Andric return true; 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric // UnusedFileScopedDecls stores the first declaration. 7120b57cec5SDimitry Andric // The declaration may have become definition so check again. 7130b57cec5SDimitry Andric const FunctionDecl *DeclToCheck; 7140b57cec5SDimitry Andric if (FD->hasBody(DeclToCheck)) 7150b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric // Later redecls may add new information resulting in not having to warn, 7180b57cec5SDimitry Andric // so check again. 7190b57cec5SDimitry Andric DeclToCheck = FD->getMostRecentDecl(); 7200b57cec5SDimitry Andric if (DeclToCheck != FD) 7210b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 7250b57cec5SDimitry Andric // If a variable usable in constant expressions is referenced, 7260b57cec5SDimitry Andric // don't warn if it isn't used: if the value of a variable is required 7270b57cec5SDimitry Andric // for the computation of a constant expression, it doesn't make sense to 7280b57cec5SDimitry Andric // warn even if the variable isn't odr-used. (isReferenced doesn't 7290b57cec5SDimitry Andric // precisely reflect that, but it's a decent approximation.) 7300b57cec5SDimitry Andric if (VD->isReferenced() && 7310b57cec5SDimitry Andric VD->mightBeUsableInConstantExpressions(SemaRef->Context)) 7320b57cec5SDimitry Andric return true; 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric if (VarTemplateDecl *Template = VD->getDescribedVarTemplate()) 7350b57cec5SDimitry Andric // If this is a variable template and none of its specializations is used, 7360b57cec5SDimitry Andric // we should warn. 7370b57cec5SDimitry Andric for (const auto *Spec : Template->specializations()) 7380b57cec5SDimitry Andric if (ShouldRemoveFromUnused(SemaRef, Spec)) 7390b57cec5SDimitry Andric return true; 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric // UnusedFileScopedDecls stores the first declaration. 7420b57cec5SDimitry Andric // The declaration may have become definition so check again. 7430b57cec5SDimitry Andric const VarDecl *DeclToCheck = VD->getDefinition(); 7440b57cec5SDimitry Andric if (DeclToCheck) 7450b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric // Later redecls may add new information resulting in not having to warn, 7480b57cec5SDimitry Andric // so check again. 7490b57cec5SDimitry Andric DeclToCheck = VD->getMostRecentDecl(); 7500b57cec5SDimitry Andric if (DeclToCheck != VD) 7510b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 7520b57cec5SDimitry Andric } 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric return false; 7550b57cec5SDimitry Andric } 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric static bool isFunctionOrVarDeclExternC(NamedDecl *ND) { 7580b57cec5SDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(ND)) 7590b57cec5SDimitry Andric return FD->isExternC(); 7600b57cec5SDimitry Andric return cast<VarDecl>(ND)->isExternC(); 7610b57cec5SDimitry Andric } 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric /// Determine whether ND is an external-linkage function or variable whose 7640b57cec5SDimitry Andric /// type has no linkage. 7650b57cec5SDimitry Andric bool Sema::isExternalWithNoLinkageType(ValueDecl *VD) { 7660b57cec5SDimitry Andric // Note: it's not quite enough to check whether VD has UniqueExternalLinkage, 7670b57cec5SDimitry Andric // because we also want to catch the case where its type has VisibleNoLinkage, 7680b57cec5SDimitry Andric // which does not affect the linkage of VD. 7690b57cec5SDimitry Andric return getLangOpts().CPlusPlus && VD->hasExternalFormalLinkage() && 7700b57cec5SDimitry Andric !isExternalFormalLinkage(VD->getType()->getLinkage()) && 7710b57cec5SDimitry Andric !isFunctionOrVarDeclExternC(VD); 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric /// Obtains a sorted list of functions and variables that are undefined but 7750b57cec5SDimitry Andric /// ODR-used. 7760b57cec5SDimitry Andric void Sema::getUndefinedButUsed( 7770b57cec5SDimitry Andric SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) { 7780b57cec5SDimitry Andric for (const auto &UndefinedUse : UndefinedButUsed) { 7790b57cec5SDimitry Andric NamedDecl *ND = UndefinedUse.first; 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric // Ignore attributes that have become invalid. 7820b57cec5SDimitry Andric if (ND->isInvalidDecl()) continue; 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric // __attribute__((weakref)) is basically a definition. 7850b57cec5SDimitry Andric if (ND->hasAttr<WeakRefAttr>()) continue; 7860b57cec5SDimitry Andric 7870b57cec5SDimitry Andric if (isa<CXXDeductionGuideDecl>(ND)) 7880b57cec5SDimitry Andric continue; 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric if (ND->hasAttr<DLLImportAttr>() || ND->hasAttr<DLLExportAttr>()) { 7910b57cec5SDimitry Andric // An exported function will always be emitted when defined, so even if 7920b57cec5SDimitry Andric // the function is inline, it doesn't have to be emitted in this TU. An 7930b57cec5SDimitry Andric // imported function implies that it has been exported somewhere else. 7940b57cec5SDimitry Andric continue; 7950b57cec5SDimitry Andric } 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 7980b57cec5SDimitry Andric if (FD->isDefined()) 7990b57cec5SDimitry Andric continue; 8000b57cec5SDimitry Andric if (FD->isExternallyVisible() && 8010b57cec5SDimitry Andric !isExternalWithNoLinkageType(FD) && 8020b57cec5SDimitry Andric !FD->getMostRecentDecl()->isInlined() && 8030b57cec5SDimitry Andric !FD->hasAttr<ExcludeFromExplicitInstantiationAttr>()) 8040b57cec5SDimitry Andric continue; 8050b57cec5SDimitry Andric if (FD->getBuiltinID()) 8060b57cec5SDimitry Andric continue; 8070b57cec5SDimitry Andric } else { 8080b57cec5SDimitry Andric auto *VD = cast<VarDecl>(ND); 8090b57cec5SDimitry Andric if (VD->hasDefinition() != VarDecl::DeclarationOnly) 8100b57cec5SDimitry Andric continue; 8110b57cec5SDimitry Andric if (VD->isExternallyVisible() && 8120b57cec5SDimitry Andric !isExternalWithNoLinkageType(VD) && 8130b57cec5SDimitry Andric !VD->getMostRecentDecl()->isInline() && 8140b57cec5SDimitry Andric !VD->hasAttr<ExcludeFromExplicitInstantiationAttr>()) 8150b57cec5SDimitry Andric continue; 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric // Skip VarDecls that lack formal definitions but which we know are in 8180b57cec5SDimitry Andric // fact defined somewhere. 8190b57cec5SDimitry Andric if (VD->isKnownToBeDefined()) 8200b57cec5SDimitry Andric continue; 8210b57cec5SDimitry Andric } 8220b57cec5SDimitry Andric 8230b57cec5SDimitry Andric Undefined.push_back(std::make_pair(ND, UndefinedUse.second)); 8240b57cec5SDimitry Andric } 8250b57cec5SDimitry Andric } 8260b57cec5SDimitry Andric 8270b57cec5SDimitry Andric /// checkUndefinedButUsed - Check for undefined objects with internal linkage 8280b57cec5SDimitry Andric /// or that are inline. 8290b57cec5SDimitry Andric static void checkUndefinedButUsed(Sema &S) { 8300b57cec5SDimitry Andric if (S.UndefinedButUsed.empty()) return; 8310b57cec5SDimitry Andric 8320b57cec5SDimitry Andric // Collect all the still-undefined entities with internal linkage. 8330b57cec5SDimitry Andric SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined; 8340b57cec5SDimitry Andric S.getUndefinedButUsed(Undefined); 8350b57cec5SDimitry Andric if (Undefined.empty()) return; 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric for (auto Undef : Undefined) { 8380b57cec5SDimitry Andric ValueDecl *VD = cast<ValueDecl>(Undef.first); 8390b57cec5SDimitry Andric SourceLocation UseLoc = Undef.second; 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric if (S.isExternalWithNoLinkageType(VD)) { 8420b57cec5SDimitry Andric // C++ [basic.link]p8: 8430b57cec5SDimitry Andric // A type without linkage shall not be used as the type of a variable 8440b57cec5SDimitry Andric // or function with external linkage unless 8450b57cec5SDimitry Andric // -- the entity has C language linkage 8460b57cec5SDimitry Andric // -- the entity is not odr-used or is defined in the same TU 8470b57cec5SDimitry Andric // 8480b57cec5SDimitry Andric // As an extension, accept this in cases where the type is externally 8490b57cec5SDimitry Andric // visible, since the function or variable actually can be defined in 8500b57cec5SDimitry Andric // another translation unit in that case. 8510b57cec5SDimitry Andric S.Diag(VD->getLocation(), isExternallyVisible(VD->getType()->getLinkage()) 8520b57cec5SDimitry Andric ? diag::ext_undefined_internal_type 8530b57cec5SDimitry Andric : diag::err_undefined_internal_type) 8540b57cec5SDimitry Andric << isa<VarDecl>(VD) << VD; 8550b57cec5SDimitry Andric } else if (!VD->isExternallyVisible()) { 8560b57cec5SDimitry Andric // FIXME: We can promote this to an error. The function or variable can't 8570b57cec5SDimitry Andric // be defined anywhere else, so the program must necessarily violate the 8580b57cec5SDimitry Andric // one definition rule. 859fe6060f1SDimitry Andric bool IsImplicitBase = false; 860fe6060f1SDimitry Andric if (const auto *BaseD = dyn_cast<FunctionDecl>(VD)) { 861fe6060f1SDimitry Andric auto *DVAttr = BaseD->getAttr<OMPDeclareVariantAttr>(); 862fe6060f1SDimitry Andric if (DVAttr && !DVAttr->getTraitInfo().isExtensionActive( 863fe6060f1SDimitry Andric llvm::omp::TraitProperty:: 864fe6060f1SDimitry Andric implementation_extension_disable_implicit_base)) { 865fe6060f1SDimitry Andric const auto *Func = cast<FunctionDecl>( 866fe6060f1SDimitry Andric cast<DeclRefExpr>(DVAttr->getVariantFuncRef())->getDecl()); 867fe6060f1SDimitry Andric IsImplicitBase = BaseD->isImplicit() && 868fe6060f1SDimitry Andric Func->getIdentifier()->isMangledOpenMPVariantName(); 869fe6060f1SDimitry Andric } 870fe6060f1SDimitry Andric } 871fe6060f1SDimitry Andric if (!S.getLangOpts().OpenMP || !IsImplicitBase) 8720b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::warn_undefined_internal) 8730b57cec5SDimitry Andric << isa<VarDecl>(VD) << VD; 8740b57cec5SDimitry Andric } else if (auto *FD = dyn_cast<FunctionDecl>(VD)) { 8750b57cec5SDimitry Andric (void)FD; 8760b57cec5SDimitry Andric assert(FD->getMostRecentDecl()->isInlined() && 8770b57cec5SDimitry Andric "used object requires definition but isn't inline or internal?"); 8780b57cec5SDimitry Andric // FIXME: This is ill-formed; we should reject. 8790b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::warn_undefined_inline) << VD; 8800b57cec5SDimitry Andric } else { 8810b57cec5SDimitry Andric assert(cast<VarDecl>(VD)->getMostRecentDecl()->isInline() && 8820b57cec5SDimitry Andric "used var requires definition but isn't inline or internal?"); 8830b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::err_undefined_inline_var) << VD; 8840b57cec5SDimitry Andric } 8850b57cec5SDimitry Andric if (UseLoc.isValid()) 8860b57cec5SDimitry Andric S.Diag(UseLoc, diag::note_used_here); 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric S.UndefinedButUsed.clear(); 8900b57cec5SDimitry Andric } 8910b57cec5SDimitry Andric 8920b57cec5SDimitry Andric void Sema::LoadExternalWeakUndeclaredIdentifiers() { 8930b57cec5SDimitry Andric if (!ExternalSource) 8940b57cec5SDimitry Andric return; 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs; 8970b57cec5SDimitry Andric ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs); 8980b57cec5SDimitry Andric for (auto &WeakID : WeakIDs) 8990b57cec5SDimitry Andric WeakUndeclaredIdentifiers.insert(WeakID); 9000b57cec5SDimitry Andric } 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap; 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric /// Returns true, if all methods and nested classes of the given 9060b57cec5SDimitry Andric /// CXXRecordDecl are defined in this translation unit. 9070b57cec5SDimitry Andric /// 9080b57cec5SDimitry Andric /// Should only be called from ActOnEndOfTranslationUnit so that all 9090b57cec5SDimitry Andric /// definitions are actually read. 9100b57cec5SDimitry Andric static bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD, 9110b57cec5SDimitry Andric RecordCompleteMap &MNCComplete) { 9120b57cec5SDimitry Andric RecordCompleteMap::iterator Cache = MNCComplete.find(RD); 9130b57cec5SDimitry Andric if (Cache != MNCComplete.end()) 9140b57cec5SDimitry Andric return Cache->second; 9150b57cec5SDimitry Andric if (!RD->isCompleteDefinition()) 9160b57cec5SDimitry Andric return false; 9170b57cec5SDimitry Andric bool Complete = true; 9180b57cec5SDimitry Andric for (DeclContext::decl_iterator I = RD->decls_begin(), 9190b57cec5SDimitry Andric E = RD->decls_end(); 9200b57cec5SDimitry Andric I != E && Complete; ++I) { 9210b57cec5SDimitry Andric if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I)) 9220b57cec5SDimitry Andric Complete = M->isDefined() || M->isDefaulted() || 9230b57cec5SDimitry Andric (M->isPure() && !isa<CXXDestructorDecl>(M)); 9240b57cec5SDimitry Andric else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I)) 9250b57cec5SDimitry Andric // If the template function is marked as late template parsed at this 9260b57cec5SDimitry Andric // point, it has not been instantiated and therefore we have not 9270b57cec5SDimitry Andric // performed semantic analysis on it yet, so we cannot know if the type 9280b57cec5SDimitry Andric // can be considered complete. 9290b57cec5SDimitry Andric Complete = !F->getTemplatedDecl()->isLateTemplateParsed() && 9300b57cec5SDimitry Andric F->getTemplatedDecl()->isDefined(); 9310b57cec5SDimitry Andric else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) { 9320b57cec5SDimitry Andric if (R->isInjectedClassName()) 9330b57cec5SDimitry Andric continue; 9340b57cec5SDimitry Andric if (R->hasDefinition()) 9350b57cec5SDimitry Andric Complete = MethodsAndNestedClassesComplete(R->getDefinition(), 9360b57cec5SDimitry Andric MNCComplete); 9370b57cec5SDimitry Andric else 9380b57cec5SDimitry Andric Complete = false; 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric MNCComplete[RD] = Complete; 9420b57cec5SDimitry Andric return Complete; 9430b57cec5SDimitry Andric } 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric /// Returns true, if the given CXXRecordDecl is fully defined in this 9460b57cec5SDimitry Andric /// translation unit, i.e. all methods are defined or pure virtual and all 9470b57cec5SDimitry Andric /// friends, friend functions and nested classes are fully defined in this 9480b57cec5SDimitry Andric /// translation unit. 9490b57cec5SDimitry Andric /// 9500b57cec5SDimitry Andric /// Should only be called from ActOnEndOfTranslationUnit so that all 9510b57cec5SDimitry Andric /// definitions are actually read. 9520b57cec5SDimitry Andric static bool IsRecordFullyDefined(const CXXRecordDecl *RD, 9530b57cec5SDimitry Andric RecordCompleteMap &RecordsComplete, 9540b57cec5SDimitry Andric RecordCompleteMap &MNCComplete) { 9550b57cec5SDimitry Andric RecordCompleteMap::iterator Cache = RecordsComplete.find(RD); 9560b57cec5SDimitry Andric if (Cache != RecordsComplete.end()) 9570b57cec5SDimitry Andric return Cache->second; 9580b57cec5SDimitry Andric bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete); 9590b57cec5SDimitry Andric for (CXXRecordDecl::friend_iterator I = RD->friend_begin(), 9600b57cec5SDimitry Andric E = RD->friend_end(); 9610b57cec5SDimitry Andric I != E && Complete; ++I) { 9620b57cec5SDimitry Andric // Check if friend classes and methods are complete. 9630b57cec5SDimitry Andric if (TypeSourceInfo *TSI = (*I)->getFriendType()) { 9640b57cec5SDimitry Andric // Friend classes are available as the TypeSourceInfo of the FriendDecl. 9650b57cec5SDimitry Andric if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl()) 9660b57cec5SDimitry Andric Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete); 9670b57cec5SDimitry Andric else 9680b57cec5SDimitry Andric Complete = false; 9690b57cec5SDimitry Andric } else { 9700b57cec5SDimitry Andric // Friend functions are available through the NamedDecl of FriendDecl. 9710b57cec5SDimitry Andric if (const FunctionDecl *FD = 9720b57cec5SDimitry Andric dyn_cast<FunctionDecl>((*I)->getFriendDecl())) 9730b57cec5SDimitry Andric Complete = FD->isDefined(); 9740b57cec5SDimitry Andric else 9750b57cec5SDimitry Andric // This is a template friend, give up. 9760b57cec5SDimitry Andric Complete = false; 9770b57cec5SDimitry Andric } 9780b57cec5SDimitry Andric } 9790b57cec5SDimitry Andric RecordsComplete[RD] = Complete; 9800b57cec5SDimitry Andric return Complete; 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric void Sema::emitAndClearUnusedLocalTypedefWarnings() { 9840b57cec5SDimitry Andric if (ExternalSource) 9850b57cec5SDimitry Andric ExternalSource->ReadUnusedLocalTypedefNameCandidates( 9860b57cec5SDimitry Andric UnusedLocalTypedefNameCandidates); 9870b57cec5SDimitry Andric for (const TypedefNameDecl *TD : UnusedLocalTypedefNameCandidates) { 9880b57cec5SDimitry Andric if (TD->isReferenced()) 9890b57cec5SDimitry Andric continue; 9900b57cec5SDimitry Andric Diag(TD->getLocation(), diag::warn_unused_local_typedef) 9910b57cec5SDimitry Andric << isa<TypeAliasDecl>(TD) << TD->getDeclName(); 9920b57cec5SDimitry Andric } 9930b57cec5SDimitry Andric UnusedLocalTypedefNameCandidates.clear(); 9940b57cec5SDimitry Andric } 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric /// This is called before the very first declaration in the translation unit 9970b57cec5SDimitry Andric /// is parsed. Note that the ASTContext may have already injected some 9980b57cec5SDimitry Andric /// declarations. 9990b57cec5SDimitry Andric void Sema::ActOnStartOfTranslationUnit() { 10000b57cec5SDimitry Andric if (getLangOpts().ModulesTS && 10010b57cec5SDimitry Andric (getLangOpts().getCompilingModule() == LangOptions::CMK_ModuleInterface || 10020b57cec5SDimitry Andric getLangOpts().getCompilingModule() == LangOptions::CMK_None)) { 10030b57cec5SDimitry Andric // We start in an implied global module fragment. 10040b57cec5SDimitry Andric SourceLocation StartOfTU = 10050b57cec5SDimitry Andric SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 10060b57cec5SDimitry Andric ActOnGlobalModuleFragmentDecl(StartOfTU); 10070b57cec5SDimitry Andric ModuleScopes.back().ImplicitGlobalModuleFragment = true; 10080b57cec5SDimitry Andric } 10090b57cec5SDimitry Andric } 10100b57cec5SDimitry Andric 10110b57cec5SDimitry Andric void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) { 10120b57cec5SDimitry Andric // No explicit actions are required at the end of the global module fragment. 10130b57cec5SDimitry Andric if (Kind == TUFragmentKind::Global) 10140b57cec5SDimitry Andric return; 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric // Transfer late parsed template instantiations over to the pending template 10170b57cec5SDimitry Andric // instantiation list. During normal compilation, the late template parser 10180b57cec5SDimitry Andric // will be installed and instantiating these templates will succeed. 10190b57cec5SDimitry Andric // 10200b57cec5SDimitry Andric // If we are building a TU prefix for serialization, it is also safe to 10210b57cec5SDimitry Andric // transfer these over, even though they are not parsed. The end of the TU 10220b57cec5SDimitry Andric // should be outside of any eager template instantiation scope, so when this 10230b57cec5SDimitry Andric // AST is deserialized, these templates will not be parsed until the end of 10240b57cec5SDimitry Andric // the combined TU. 10250b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.end(), 10260b57cec5SDimitry Andric LateParsedInstantiations.begin(), 10270b57cec5SDimitry Andric LateParsedInstantiations.end()); 10280b57cec5SDimitry Andric LateParsedInstantiations.clear(); 10290b57cec5SDimitry Andric 10300b57cec5SDimitry Andric // If DefinedUsedVTables ends up marking any virtual member functions it 10310b57cec5SDimitry Andric // might lead to more pending template instantiations, which we then need 10320b57cec5SDimitry Andric // to instantiate. 10330b57cec5SDimitry Andric DefineUsedVTables(); 10340b57cec5SDimitry Andric 10350b57cec5SDimitry Andric // C++: Perform implicit template instantiations. 10360b57cec5SDimitry Andric // 10370b57cec5SDimitry Andric // FIXME: When we perform these implicit instantiations, we do not 10380b57cec5SDimitry Andric // carefully keep track of the point of instantiation (C++ [temp.point]). 10390b57cec5SDimitry Andric // This means that name lookup that occurs within the template 10400b57cec5SDimitry Andric // instantiation will always happen at the end of the translation unit, 10410b57cec5SDimitry Andric // so it will find some names that are not required to be found. This is 10420b57cec5SDimitry Andric // valid, but we could do better by diagnosing if an instantiation uses a 10430b57cec5SDimitry Andric // name that was not visible at its first point of instantiation. 10440b57cec5SDimitry Andric if (ExternalSource) { 10450b57cec5SDimitry Andric // Load pending instantiations from the external source. 10460b57cec5SDimitry Andric SmallVector<PendingImplicitInstantiation, 4> Pending; 10470b57cec5SDimitry Andric ExternalSource->ReadPendingInstantiations(Pending); 10480b57cec5SDimitry Andric for (auto PII : Pending) 10490b57cec5SDimitry Andric if (auto Func = dyn_cast<FunctionDecl>(PII.first)) 10500b57cec5SDimitry Andric Func->setInstantiationIsPending(true); 10510b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.begin(), 10520b57cec5SDimitry Andric Pending.begin(), Pending.end()); 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric { 1056480093f4SDimitry Andric llvm::TimeTraceScope TimeScope("PerformPendingInstantiations"); 10570b57cec5SDimitry Andric PerformPendingInstantiations(); 10580b57cec5SDimitry Andric } 10590b57cec5SDimitry Andric 10605ffd83dbSDimitry Andric emitDeferredDiags(); 1061a7dea167SDimitry Andric 10620b57cec5SDimitry Andric assert(LateParsedInstantiations.empty() && 10630b57cec5SDimitry Andric "end of TU template instantiation should not create more " 10640b57cec5SDimitry Andric "late-parsed templates"); 1065a7dea167SDimitry Andric 1066a7dea167SDimitry Andric // Report diagnostics for uncorrected delayed typos. Ideally all of them 1067a7dea167SDimitry Andric // should have been corrected by that time, but it is very hard to cover all 1068a7dea167SDimitry Andric // cases in practice. 1069a7dea167SDimitry Andric for (const auto &Typo : DelayedTypos) { 1070a7dea167SDimitry Andric // We pass an empty TypoCorrection to indicate no correction was performed. 1071a7dea167SDimitry Andric Typo.second.DiagHandler(TypoCorrection()); 1072a7dea167SDimitry Andric } 1073a7dea167SDimitry Andric DelayedTypos.clear(); 10740b57cec5SDimitry Andric } 10750b57cec5SDimitry Andric 10760b57cec5SDimitry Andric /// ActOnEndOfTranslationUnit - This is called at the very end of the 10770b57cec5SDimitry Andric /// translation unit when EOF is reached and all but the top-level scope is 10780b57cec5SDimitry Andric /// popped. 10790b57cec5SDimitry Andric void Sema::ActOnEndOfTranslationUnit() { 10800b57cec5SDimitry Andric assert(DelayedDiagnostics.getCurrentPool() == nullptr 10810b57cec5SDimitry Andric && "reached end of translation unit with a pool attached?"); 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric // If code completion is enabled, don't perform any end-of-translation-unit 10840b57cec5SDimitry Andric // work. 10850b57cec5SDimitry Andric if (PP.isCodeCompletionEnabled()) 10860b57cec5SDimitry Andric return; 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric // Complete translation units and modules define vtables and perform implicit 10890b57cec5SDimitry Andric // instantiations. PCH files do not. 10900b57cec5SDimitry Andric if (TUKind != TU_Prefix) { 10910b57cec5SDimitry Andric DiagnoseUseOfUnimplementedSelectors(); 10920b57cec5SDimitry Andric 10930b57cec5SDimitry Andric ActOnEndOfTranslationUnitFragment( 10940b57cec5SDimitry Andric !ModuleScopes.empty() && ModuleScopes.back().Module->Kind == 10950b57cec5SDimitry Andric Module::PrivateModuleFragment 10960b57cec5SDimitry Andric ? TUFragmentKind::Private 10970b57cec5SDimitry Andric : TUFragmentKind::Normal); 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andric if (LateTemplateParserCleanup) 11000b57cec5SDimitry Andric LateTemplateParserCleanup(OpaqueParser); 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andric CheckDelayedMemberExceptionSpecs(); 11030b57cec5SDimitry Andric } else { 11040b57cec5SDimitry Andric // If we are building a TU prefix for serialization, it is safe to transfer 11050b57cec5SDimitry Andric // these over, even though they are not parsed. The end of the TU should be 11060b57cec5SDimitry Andric // outside of any eager template instantiation scope, so when this AST is 11070b57cec5SDimitry Andric // deserialized, these templates will not be parsed until the end of the 11080b57cec5SDimitry Andric // combined TU. 11090b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.end(), 11100b57cec5SDimitry Andric LateParsedInstantiations.begin(), 11110b57cec5SDimitry Andric LateParsedInstantiations.end()); 11120b57cec5SDimitry Andric LateParsedInstantiations.clear(); 11135ffd83dbSDimitry Andric 11145ffd83dbSDimitry Andric if (LangOpts.PCHInstantiateTemplates) { 11155ffd83dbSDimitry Andric llvm::TimeTraceScope TimeScope("PerformPendingInstantiations"); 11165ffd83dbSDimitry Andric PerformPendingInstantiations(); 11175ffd83dbSDimitry Andric } 11180b57cec5SDimitry Andric } 11190b57cec5SDimitry Andric 1120e8d8bef9SDimitry Andric DiagnoseUnterminatedPragmaAlignPack(); 11210b57cec5SDimitry Andric DiagnoseUnterminatedPragmaAttribute(); 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric // All delayed member exception specs should be checked or we end up accepting 11240b57cec5SDimitry Andric // incompatible declarations. 11250b57cec5SDimitry Andric assert(DelayedOverridingExceptionSpecChecks.empty()); 11260b57cec5SDimitry Andric assert(DelayedEquivalentExceptionSpecChecks.empty()); 11270b57cec5SDimitry Andric 11280b57cec5SDimitry Andric // All dllexport classes should have been processed already. 11290b57cec5SDimitry Andric assert(DelayedDllExportClasses.empty()); 11300b57cec5SDimitry Andric assert(DelayedDllExportMemberFunctions.empty()); 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric // Remove file scoped decls that turned out to be used. 11330b57cec5SDimitry Andric UnusedFileScopedDecls.erase( 11340b57cec5SDimitry Andric std::remove_if(UnusedFileScopedDecls.begin(nullptr, true), 11350b57cec5SDimitry Andric UnusedFileScopedDecls.end(), 11360b57cec5SDimitry Andric [this](const DeclaratorDecl *DD) { 11370b57cec5SDimitry Andric return ShouldRemoveFromUnused(this, DD); 11380b57cec5SDimitry Andric }), 11390b57cec5SDimitry Andric UnusedFileScopedDecls.end()); 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric if (TUKind == TU_Prefix) { 11420b57cec5SDimitry Andric // Translation unit prefixes don't need any of the checking below. 11430b57cec5SDimitry Andric if (!PP.isIncrementalProcessingEnabled()) 11440b57cec5SDimitry Andric TUScope = nullptr; 11450b57cec5SDimitry Andric return; 11460b57cec5SDimitry Andric } 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric // Check for #pragma weak identifiers that were never declared 11490b57cec5SDimitry Andric LoadExternalWeakUndeclaredIdentifiers(); 11500b57cec5SDimitry Andric for (auto WeakID : WeakUndeclaredIdentifiers) { 11510b57cec5SDimitry Andric if (WeakID.second.getUsed()) 11520b57cec5SDimitry Andric continue; 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric Decl *PrevDecl = LookupSingleName(TUScope, WeakID.first, SourceLocation(), 11550b57cec5SDimitry Andric LookupOrdinaryName); 11560b57cec5SDimitry Andric if (PrevDecl != nullptr && 11570b57cec5SDimitry Andric !(isa<FunctionDecl>(PrevDecl) || isa<VarDecl>(PrevDecl))) 11580b57cec5SDimitry Andric Diag(WeakID.second.getLocation(), diag::warn_attribute_wrong_decl_type) 11590b57cec5SDimitry Andric << "'weak'" << ExpectedVariableOrFunction; 11600b57cec5SDimitry Andric else 11610b57cec5SDimitry Andric Diag(WeakID.second.getLocation(), diag::warn_weak_identifier_undeclared) 11620b57cec5SDimitry Andric << WeakID.first; 11630b57cec5SDimitry Andric } 11640b57cec5SDimitry Andric 11650b57cec5SDimitry Andric if (LangOpts.CPlusPlus11 && 11660b57cec5SDimitry Andric !Diags.isIgnored(diag::warn_delegating_ctor_cycle, SourceLocation())) 11670b57cec5SDimitry Andric CheckDelegatingCtorCycles(); 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric if (!Diags.hasErrorOccurred()) { 11700b57cec5SDimitry Andric if (ExternalSource) 11710b57cec5SDimitry Andric ExternalSource->ReadUndefinedButUsed(UndefinedButUsed); 11720b57cec5SDimitry Andric checkUndefinedButUsed(*this); 11730b57cec5SDimitry Andric } 11740b57cec5SDimitry Andric 11750b57cec5SDimitry Andric // A global-module-fragment is only permitted within a module unit. 11760b57cec5SDimitry Andric bool DiagnosedMissingModuleDeclaration = false; 11770b57cec5SDimitry Andric if (!ModuleScopes.empty() && 11780b57cec5SDimitry Andric ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment && 11790b57cec5SDimitry Andric !ModuleScopes.back().ImplicitGlobalModuleFragment) { 11800b57cec5SDimitry Andric Diag(ModuleScopes.back().BeginLoc, 11810b57cec5SDimitry Andric diag::err_module_declaration_missing_after_global_module_introducer); 11820b57cec5SDimitry Andric DiagnosedMissingModuleDeclaration = true; 11830b57cec5SDimitry Andric } 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric if (TUKind == TU_Module) { 11860b57cec5SDimitry Andric // If we are building a module interface unit, we need to have seen the 11870b57cec5SDimitry Andric // module declaration by now. 11880b57cec5SDimitry Andric if (getLangOpts().getCompilingModule() == 11890b57cec5SDimitry Andric LangOptions::CMK_ModuleInterface && 11900b57cec5SDimitry Andric (ModuleScopes.empty() || 11910b57cec5SDimitry Andric !ModuleScopes.back().Module->isModulePurview()) && 11920b57cec5SDimitry Andric !DiagnosedMissingModuleDeclaration) { 11930b57cec5SDimitry Andric // FIXME: Make a better guess as to where to put the module declaration. 11940b57cec5SDimitry Andric Diag(getSourceManager().getLocForStartOfFile( 11950b57cec5SDimitry Andric getSourceManager().getMainFileID()), 11960b57cec5SDimitry Andric diag::err_module_declaration_missing); 11970b57cec5SDimitry Andric } 11980b57cec5SDimitry Andric 11990b57cec5SDimitry Andric // If we are building a module, resolve all of the exported declarations 12000b57cec5SDimitry Andric // now. 12010b57cec5SDimitry Andric if (Module *CurrentModule = PP.getCurrentModule()) { 12020b57cec5SDimitry Andric ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 12030b57cec5SDimitry Andric 12040b57cec5SDimitry Andric SmallVector<Module *, 2> Stack; 12050b57cec5SDimitry Andric Stack.push_back(CurrentModule); 12060b57cec5SDimitry Andric while (!Stack.empty()) { 12070b57cec5SDimitry Andric Module *Mod = Stack.pop_back_val(); 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric // Resolve the exported declarations and conflicts. 12100b57cec5SDimitry Andric // FIXME: Actually complain, once we figure out how to teach the 12110b57cec5SDimitry Andric // diagnostic client to deal with complaints in the module map at this 12120b57cec5SDimitry Andric // point. 12130b57cec5SDimitry Andric ModMap.resolveExports(Mod, /*Complain=*/false); 12140b57cec5SDimitry Andric ModMap.resolveUses(Mod, /*Complain=*/false); 12150b57cec5SDimitry Andric ModMap.resolveConflicts(Mod, /*Complain=*/false); 12160b57cec5SDimitry Andric 12170b57cec5SDimitry Andric // Queue the submodules, so their exports will also be resolved. 12180b57cec5SDimitry Andric Stack.append(Mod->submodule_begin(), Mod->submodule_end()); 12190b57cec5SDimitry Andric } 12200b57cec5SDimitry Andric } 12210b57cec5SDimitry Andric 12220b57cec5SDimitry Andric // Warnings emitted in ActOnEndOfTranslationUnit() should be emitted for 12230b57cec5SDimitry Andric // modules when they are built, not every time they are used. 12240b57cec5SDimitry Andric emitAndClearUnusedLocalTypedefWarnings(); 12250b57cec5SDimitry Andric } 12260b57cec5SDimitry Andric 12270b57cec5SDimitry Andric // C99 6.9.2p2: 12280b57cec5SDimitry Andric // A declaration of an identifier for an object that has file 12290b57cec5SDimitry Andric // scope without an initializer, and without a storage-class 12300b57cec5SDimitry Andric // specifier or with the storage-class specifier static, 12310b57cec5SDimitry Andric // constitutes a tentative definition. If a translation unit 12320b57cec5SDimitry Andric // contains one or more tentative definitions for an identifier, 12330b57cec5SDimitry Andric // and the translation unit contains no external definition for 12340b57cec5SDimitry Andric // that identifier, then the behavior is exactly as if the 12350b57cec5SDimitry Andric // translation unit contains a file scope declaration of that 12360b57cec5SDimitry Andric // identifier, with the composite type as of the end of the 12370b57cec5SDimitry Andric // translation unit, with an initializer equal to 0. 12380b57cec5SDimitry Andric llvm::SmallSet<VarDecl *, 32> Seen; 12390b57cec5SDimitry Andric for (TentativeDefinitionsType::iterator 12400b57cec5SDimitry Andric T = TentativeDefinitions.begin(ExternalSource), 12410b57cec5SDimitry Andric TEnd = TentativeDefinitions.end(); 12420b57cec5SDimitry Andric T != TEnd; ++T) { 12430b57cec5SDimitry Andric VarDecl *VD = (*T)->getActingDefinition(); 12440b57cec5SDimitry Andric 12450b57cec5SDimitry Andric // If the tentative definition was completed, getActingDefinition() returns 12460b57cec5SDimitry Andric // null. If we've already seen this variable before, insert()'s second 12470b57cec5SDimitry Andric // return value is false. 12480b57cec5SDimitry Andric if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second) 12490b57cec5SDimitry Andric continue; 12500b57cec5SDimitry Andric 12510b57cec5SDimitry Andric if (const IncompleteArrayType *ArrayT 12520b57cec5SDimitry Andric = Context.getAsIncompleteArrayType(VD->getType())) { 12530b57cec5SDimitry Andric // Set the length of the array to 1 (C99 6.9.2p5). 12540b57cec5SDimitry Andric Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); 12550b57cec5SDimitry Andric llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); 1256a7dea167SDimitry Andric QualType T = Context.getConstantArrayType(ArrayT->getElementType(), One, 1257a7dea167SDimitry Andric nullptr, ArrayType::Normal, 0); 12580b57cec5SDimitry Andric VD->setType(T); 12590b57cec5SDimitry Andric } else if (RequireCompleteType(VD->getLocation(), VD->getType(), 12600b57cec5SDimitry Andric diag::err_tentative_def_incomplete_type)) 12610b57cec5SDimitry Andric VD->setInvalidDecl(); 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric // No initialization is performed for a tentative definition. 12640b57cec5SDimitry Andric CheckCompleteVariableDeclaration(VD); 12650b57cec5SDimitry Andric 12660b57cec5SDimitry Andric // Notify the consumer that we've completed a tentative definition. 12670b57cec5SDimitry Andric if (!VD->isInvalidDecl()) 12680b57cec5SDimitry Andric Consumer.CompleteTentativeDefinition(VD); 12690b57cec5SDimitry Andric } 12700b57cec5SDimitry Andric 1271480093f4SDimitry Andric for (auto D : ExternalDeclarations) { 1272480093f4SDimitry Andric if (!D || D->isInvalidDecl() || D->getPreviousDecl() || !D->isUsed()) 1273480093f4SDimitry Andric continue; 1274480093f4SDimitry Andric 1275480093f4SDimitry Andric Consumer.CompleteExternalDeclaration(D); 1276480093f4SDimitry Andric } 1277480093f4SDimitry Andric 12780b57cec5SDimitry Andric // If there were errors, disable 'unused' warnings since they will mostly be 12790b57cec5SDimitry Andric // noise. Don't warn for a use from a module: either we should warn on all 12800b57cec5SDimitry Andric // file-scope declarations in modules or not at all, but whether the 12810b57cec5SDimitry Andric // declaration is used is immaterial. 12820b57cec5SDimitry Andric if (!Diags.hasErrorOccurred() && TUKind != TU_Module) { 12830b57cec5SDimitry Andric // Output warning for unused file scoped decls. 12840b57cec5SDimitry Andric for (UnusedFileScopedDeclsType::iterator 12850b57cec5SDimitry Andric I = UnusedFileScopedDecls.begin(ExternalSource), 12860b57cec5SDimitry Andric E = UnusedFileScopedDecls.end(); I != E; ++I) { 12870b57cec5SDimitry Andric if (ShouldRemoveFromUnused(this, *I)) 12880b57cec5SDimitry Andric continue; 12890b57cec5SDimitry Andric 12900b57cec5SDimitry Andric if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { 12910b57cec5SDimitry Andric const FunctionDecl *DiagD; 12920b57cec5SDimitry Andric if (!FD->hasBody(DiagD)) 12930b57cec5SDimitry Andric DiagD = FD; 12940b57cec5SDimitry Andric if (DiagD->isDeleted()) 12950b57cec5SDimitry Andric continue; // Deleted functions are supposed to be unused. 12960b57cec5SDimitry Andric if (DiagD->isReferenced()) { 12970b57cec5SDimitry Andric if (isa<CXXMethodDecl>(DiagD)) 12980b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_member_function) 1299e8d8bef9SDimitry Andric << DiagD; 13000b57cec5SDimitry Andric else { 13010b57cec5SDimitry Andric if (FD->getStorageClass() == SC_Static && 13020b57cec5SDimitry Andric !FD->isInlineSpecified() && 13030b57cec5SDimitry Andric !SourceMgr.isInMainFile( 13040b57cec5SDimitry Andric SourceMgr.getExpansionLoc(FD->getLocation()))) 13050b57cec5SDimitry Andric Diag(DiagD->getLocation(), 13060b57cec5SDimitry Andric diag::warn_unneeded_static_internal_decl) 1307e8d8bef9SDimitry Andric << DiagD; 13080b57cec5SDimitry Andric else 13090b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) 1310e8d8bef9SDimitry Andric << /*function*/ 0 << DiagD; 13110b57cec5SDimitry Andric } 13120b57cec5SDimitry Andric } else { 13130b57cec5SDimitry Andric if (FD->getDescribedFunctionTemplate()) 13140b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_template) 1315e8d8bef9SDimitry Andric << /*function*/ 0 << DiagD; 13160b57cec5SDimitry Andric else 1317e8d8bef9SDimitry Andric Diag(DiagD->getLocation(), isa<CXXMethodDecl>(DiagD) 1318e8d8bef9SDimitry Andric ? diag::warn_unused_member_function 13190b57cec5SDimitry Andric : diag::warn_unused_function) 1320e8d8bef9SDimitry Andric << DiagD; 13210b57cec5SDimitry Andric } 13220b57cec5SDimitry Andric } else { 13230b57cec5SDimitry Andric const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition(); 13240b57cec5SDimitry Andric if (!DiagD) 13250b57cec5SDimitry Andric DiagD = cast<VarDecl>(*I); 13260b57cec5SDimitry Andric if (DiagD->isReferenced()) { 13270b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) 1328e8d8bef9SDimitry Andric << /*variable*/ 1 << DiagD; 13290b57cec5SDimitry Andric } else if (DiagD->getType().isConstQualified()) { 13300b57cec5SDimitry Andric const SourceManager &SM = SourceMgr; 13310b57cec5SDimitry Andric if (SM.getMainFileID() != SM.getFileID(DiagD->getLocation()) || 13320b57cec5SDimitry Andric !PP.getLangOpts().IsHeaderFile) 13330b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_const_variable) 1334e8d8bef9SDimitry Andric << DiagD; 13350b57cec5SDimitry Andric } else { 13360b57cec5SDimitry Andric if (DiagD->getDescribedVarTemplate()) 13370b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_template) 1338e8d8bef9SDimitry Andric << /*variable*/ 1 << DiagD; 13390b57cec5SDimitry Andric else 1340e8d8bef9SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_variable) << DiagD; 13410b57cec5SDimitry Andric } 13420b57cec5SDimitry Andric } 13430b57cec5SDimitry Andric } 13440b57cec5SDimitry Andric 13450b57cec5SDimitry Andric emitAndClearUnusedLocalTypedefWarnings(); 13460b57cec5SDimitry Andric } 13470b57cec5SDimitry Andric 13480b57cec5SDimitry Andric if (!Diags.isIgnored(diag::warn_unused_private_field, SourceLocation())) { 13490b57cec5SDimitry Andric // FIXME: Load additional unused private field candidates from the external 13500b57cec5SDimitry Andric // source. 13510b57cec5SDimitry Andric RecordCompleteMap RecordsComplete; 13520b57cec5SDimitry Andric RecordCompleteMap MNCComplete; 13530b57cec5SDimitry Andric for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(), 13540b57cec5SDimitry Andric E = UnusedPrivateFields.end(); I != E; ++I) { 13550b57cec5SDimitry Andric const NamedDecl *D = *I; 13560b57cec5SDimitry Andric const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); 13570b57cec5SDimitry Andric if (RD && !RD->isUnion() && 13580b57cec5SDimitry Andric IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) { 13590b57cec5SDimitry Andric Diag(D->getLocation(), diag::warn_unused_private_field) 13600b57cec5SDimitry Andric << D->getDeclName(); 13610b57cec5SDimitry Andric } 13620b57cec5SDimitry Andric } 13630b57cec5SDimitry Andric } 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric if (!Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) { 13660b57cec5SDimitry Andric if (ExternalSource) 13670b57cec5SDimitry Andric ExternalSource->ReadMismatchingDeleteExpressions(DeleteExprs); 13680b57cec5SDimitry Andric for (const auto &DeletedFieldInfo : DeleteExprs) { 13690b57cec5SDimitry Andric for (const auto &DeleteExprLoc : DeletedFieldInfo.second) { 13700b57cec5SDimitry Andric AnalyzeDeleteExprMismatch(DeletedFieldInfo.first, DeleteExprLoc.first, 13710b57cec5SDimitry Andric DeleteExprLoc.second); 13720b57cec5SDimitry Andric } 13730b57cec5SDimitry Andric } 13740b57cec5SDimitry Andric } 13750b57cec5SDimitry Andric 13760b57cec5SDimitry Andric // Check we've noticed that we're no longer parsing the initializer for every 13770b57cec5SDimitry Andric // variable. If we miss cases, then at best we have a performance issue and 13780b57cec5SDimitry Andric // at worst a rejects-valid bug. 13790b57cec5SDimitry Andric assert(ParsingInitForAutoVars.empty() && 13800b57cec5SDimitry Andric "Didn't unmark var as having its initializer parsed"); 13810b57cec5SDimitry Andric 13820b57cec5SDimitry Andric if (!PP.isIncrementalProcessingEnabled()) 13830b57cec5SDimitry Andric TUScope = nullptr; 13840b57cec5SDimitry Andric } 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric 13870b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13880b57cec5SDimitry Andric // Helper functions. 13890b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13900b57cec5SDimitry Andric 13910b57cec5SDimitry Andric DeclContext *Sema::getFunctionLevelDeclContext() { 13920b57cec5SDimitry Andric DeclContext *DC = CurContext; 13930b57cec5SDimitry Andric 13940b57cec5SDimitry Andric while (true) { 139555e4f9d5SDimitry Andric if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC) || 139655e4f9d5SDimitry Andric isa<RequiresExprBodyDecl>(DC)) { 13970b57cec5SDimitry Andric DC = DC->getParent(); 13980b57cec5SDimitry Andric } else if (isa<CXXMethodDecl>(DC) && 13990b57cec5SDimitry Andric cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && 14000b57cec5SDimitry Andric cast<CXXRecordDecl>(DC->getParent())->isLambda()) { 14010b57cec5SDimitry Andric DC = DC->getParent()->getParent(); 14020b57cec5SDimitry Andric } 14030b57cec5SDimitry Andric else break; 14040b57cec5SDimitry Andric } 14050b57cec5SDimitry Andric 14060b57cec5SDimitry Andric return DC; 14070b57cec5SDimitry Andric } 14080b57cec5SDimitry Andric 14090b57cec5SDimitry Andric /// getCurFunctionDecl - If inside of a function body, this returns a pointer 14100b57cec5SDimitry Andric /// to the function decl for the function being parsed. If we're currently 14110b57cec5SDimitry Andric /// in a 'block', this returns the containing context. 14120b57cec5SDimitry Andric FunctionDecl *Sema::getCurFunctionDecl() { 14130b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 14140b57cec5SDimitry Andric return dyn_cast<FunctionDecl>(DC); 14150b57cec5SDimitry Andric } 14160b57cec5SDimitry Andric 14170b57cec5SDimitry Andric ObjCMethodDecl *Sema::getCurMethodDecl() { 14180b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 14190b57cec5SDimitry Andric while (isa<RecordDecl>(DC)) 14200b57cec5SDimitry Andric DC = DC->getParent(); 14210b57cec5SDimitry Andric return dyn_cast<ObjCMethodDecl>(DC); 14220b57cec5SDimitry Andric } 14230b57cec5SDimitry Andric 14240b57cec5SDimitry Andric NamedDecl *Sema::getCurFunctionOrMethodDecl() { 14250b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 14260b57cec5SDimitry Andric if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) 14270b57cec5SDimitry Andric return cast<NamedDecl>(DC); 14280b57cec5SDimitry Andric return nullptr; 14290b57cec5SDimitry Andric } 14300b57cec5SDimitry Andric 1431480093f4SDimitry Andric LangAS Sema::getDefaultCXXMethodAddrSpace() const { 1432480093f4SDimitry Andric if (getLangOpts().OpenCL) 1433480093f4SDimitry Andric return LangAS::opencl_generic; 1434480093f4SDimitry Andric return LangAS::Default; 1435480093f4SDimitry Andric } 1436480093f4SDimitry Andric 14370b57cec5SDimitry Andric void Sema::EmitCurrentDiagnostic(unsigned DiagID) { 14380b57cec5SDimitry Andric // FIXME: It doesn't make sense to me that DiagID is an incoming argument here 14390b57cec5SDimitry Andric // and yet we also use the current diag ID on the DiagnosticsEngine. This has 14400b57cec5SDimitry Andric // been made more painfully obvious by the refactor that introduced this 14410b57cec5SDimitry Andric // function, but it is possible that the incoming argument can be 14420b57cec5SDimitry Andric // eliminated. If it truly cannot be (for example, there is some reentrancy 14430b57cec5SDimitry Andric // issue I am not seeing yet), then there should at least be a clarifying 14440b57cec5SDimitry Andric // comment somewhere. 14450b57cec5SDimitry Andric if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) { 14460b57cec5SDimitry Andric switch (DiagnosticIDs::getDiagnosticSFINAEResponse( 14470b57cec5SDimitry Andric Diags.getCurrentDiagID())) { 14480b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_Report: 14490b57cec5SDimitry Andric // We'll report the diagnostic below. 14500b57cec5SDimitry Andric break; 14510b57cec5SDimitry Andric 14520b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_SubstitutionFailure: 14530b57cec5SDimitry Andric // Count this failure so that we know that template argument deduction 14540b57cec5SDimitry Andric // has failed. 14550b57cec5SDimitry Andric ++NumSFINAEErrors; 14560b57cec5SDimitry Andric 14570b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 14580b57cec5SDimitry Andric // template-deduction information. 14590b57cec5SDimitry Andric if (*Info && !(*Info)->hasSFINAEDiagnostic()) { 14600b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 14610b57cec5SDimitry Andric (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(), 14620b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 14630b57cec5SDimitry Andric } 14640b57cec5SDimitry Andric 1465a7dea167SDimitry Andric Diags.setLastDiagnosticIgnored(true); 14660b57cec5SDimitry Andric Diags.Clear(); 14670b57cec5SDimitry Andric return; 14680b57cec5SDimitry Andric 14690b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_AccessControl: { 14700b57cec5SDimitry Andric // Per C++ Core Issue 1170, access control is part of SFINAE. 14710b57cec5SDimitry Andric // Additionally, the AccessCheckingSFINAE flag can be used to temporarily 14720b57cec5SDimitry Andric // make access control a part of SFINAE for the purposes of checking 14730b57cec5SDimitry Andric // type traits. 14740b57cec5SDimitry Andric if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11) 14750b57cec5SDimitry Andric break; 14760b57cec5SDimitry Andric 14770b57cec5SDimitry Andric SourceLocation Loc = Diags.getCurrentDiagLoc(); 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric // Suppress this diagnostic. 14800b57cec5SDimitry Andric ++NumSFINAEErrors; 14810b57cec5SDimitry Andric 14820b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 14830b57cec5SDimitry Andric // template-deduction information. 14840b57cec5SDimitry Andric if (*Info && !(*Info)->hasSFINAEDiagnostic()) { 14850b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 14860b57cec5SDimitry Andric (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(), 14870b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 14880b57cec5SDimitry Andric } 14890b57cec5SDimitry Andric 1490a7dea167SDimitry Andric Diags.setLastDiagnosticIgnored(true); 14910b57cec5SDimitry Andric Diags.Clear(); 14920b57cec5SDimitry Andric 14930b57cec5SDimitry Andric // Now the diagnostic state is clear, produce a C++98 compatibility 14940b57cec5SDimitry Andric // warning. 14950b57cec5SDimitry Andric Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control); 14960b57cec5SDimitry Andric 14970b57cec5SDimitry Andric // The last diagnostic which Sema produced was ignored. Suppress any 14980b57cec5SDimitry Andric // notes attached to it. 1499a7dea167SDimitry Andric Diags.setLastDiagnosticIgnored(true); 15000b57cec5SDimitry Andric return; 15010b57cec5SDimitry Andric } 15020b57cec5SDimitry Andric 15030b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_Suppress: 15040b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 15050b57cec5SDimitry Andric // template-deduction information; 15060b57cec5SDimitry Andric if (*Info) { 15070b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 15080b57cec5SDimitry Andric (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(), 15090b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 15100b57cec5SDimitry Andric } 15110b57cec5SDimitry Andric 15120b57cec5SDimitry Andric // Suppress this diagnostic. 1513a7dea167SDimitry Andric Diags.setLastDiagnosticIgnored(true); 15140b57cec5SDimitry Andric Diags.Clear(); 15150b57cec5SDimitry Andric return; 15160b57cec5SDimitry Andric } 15170b57cec5SDimitry Andric } 15180b57cec5SDimitry Andric 15190b57cec5SDimitry Andric // Copy the diagnostic printing policy over the ASTContext printing policy. 15200b57cec5SDimitry Andric // TODO: Stop doing that. See: https://reviews.llvm.org/D45093#1090292 15210b57cec5SDimitry Andric Context.setPrintingPolicy(getPrintingPolicy()); 15220b57cec5SDimitry Andric 15230b57cec5SDimitry Andric // Emit the diagnostic. 15240b57cec5SDimitry Andric if (!Diags.EmitCurrentDiagnostic()) 15250b57cec5SDimitry Andric return; 15260b57cec5SDimitry Andric 15270b57cec5SDimitry Andric // If this is not a note, and we're in a template instantiation 15280b57cec5SDimitry Andric // that is different from the last template instantiation where 15290b57cec5SDimitry Andric // we emitted an error, print a template instantiation 15300b57cec5SDimitry Andric // backtrace. 15310b57cec5SDimitry Andric if (!DiagnosticIDs::isBuiltinNote(DiagID)) 15320b57cec5SDimitry Andric PrintContextStack(); 15330b57cec5SDimitry Andric } 15340b57cec5SDimitry Andric 15350b57cec5SDimitry Andric Sema::SemaDiagnosticBuilder 1536e8d8bef9SDimitry Andric Sema::Diag(SourceLocation Loc, const PartialDiagnostic &PD, bool DeferHint) { 1537e8d8bef9SDimitry Andric return Diag(Loc, PD.getDiagID(), DeferHint) << PD; 1538e8d8bef9SDimitry Andric } 15390b57cec5SDimitry Andric 1540e8d8bef9SDimitry Andric bool Sema::hasUncompilableErrorOccurred() const { 1541e8d8bef9SDimitry Andric if (getDiagnostics().hasUncompilableErrorOccurred()) 1542e8d8bef9SDimitry Andric return true; 1543e8d8bef9SDimitry Andric auto *FD = dyn_cast<FunctionDecl>(CurContext); 1544e8d8bef9SDimitry Andric if (!FD) 1545e8d8bef9SDimitry Andric return false; 1546e8d8bef9SDimitry Andric auto Loc = DeviceDeferredDiags.find(FD); 1547e8d8bef9SDimitry Andric if (Loc == DeviceDeferredDiags.end()) 1548e8d8bef9SDimitry Andric return false; 1549e8d8bef9SDimitry Andric for (auto PDAt : Loc->second) { 1550e8d8bef9SDimitry Andric if (DiagnosticIDs::isDefaultMappingAsError(PDAt.second.getDiagID())) 1551e8d8bef9SDimitry Andric return true; 1552e8d8bef9SDimitry Andric } 1553e8d8bef9SDimitry Andric return false; 15540b57cec5SDimitry Andric } 15550b57cec5SDimitry Andric 15560b57cec5SDimitry Andric // Print notes showing how we can reach FD starting from an a priori 15570b57cec5SDimitry Andric // known-callable function. 15580b57cec5SDimitry Andric static void emitCallStackNotes(Sema &S, FunctionDecl *FD) { 15590b57cec5SDimitry Andric auto FnIt = S.DeviceKnownEmittedFns.find(FD); 15600b57cec5SDimitry Andric while (FnIt != S.DeviceKnownEmittedFns.end()) { 15615ffd83dbSDimitry Andric // Respect error limit. 15625ffd83dbSDimitry Andric if (S.Diags.hasFatalErrorOccurred()) 15635ffd83dbSDimitry Andric return; 15640b57cec5SDimitry Andric DiagnosticBuilder Builder( 15650b57cec5SDimitry Andric S.Diags.Report(FnIt->second.Loc, diag::note_called_by)); 15660b57cec5SDimitry Andric Builder << FnIt->second.FD; 15670b57cec5SDimitry Andric FnIt = S.DeviceKnownEmittedFns.find(FnIt->second.FD); 15680b57cec5SDimitry Andric } 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric 15715ffd83dbSDimitry Andric namespace { 15725ffd83dbSDimitry Andric 15735ffd83dbSDimitry Andric /// Helper class that emits deferred diagnostic messages if an entity directly 15745ffd83dbSDimitry Andric /// or indirectly using the function that causes the deferred diagnostic 15755ffd83dbSDimitry Andric /// messages is known to be emitted. 15765ffd83dbSDimitry Andric /// 15775ffd83dbSDimitry Andric /// During parsing of AST, certain diagnostic messages are recorded as deferred 15785ffd83dbSDimitry Andric /// diagnostics since it is unknown whether the functions containing such 15795ffd83dbSDimitry Andric /// diagnostics will be emitted. A list of potentially emitted functions and 15805ffd83dbSDimitry Andric /// variables that may potentially trigger emission of functions are also 15815ffd83dbSDimitry Andric /// recorded. DeferredDiagnosticsEmitter recursively visits used functions 15825ffd83dbSDimitry Andric /// by each function to emit deferred diagnostics. 15835ffd83dbSDimitry Andric /// 15845ffd83dbSDimitry Andric /// During the visit, certain OpenMP directives or initializer of variables 15855ffd83dbSDimitry Andric /// with certain OpenMP attributes will cause subsequent visiting of any 15865ffd83dbSDimitry Andric /// functions enter a state which is called OpenMP device context in this 15875ffd83dbSDimitry Andric /// implementation. The state is exited when the directive or initializer is 15885ffd83dbSDimitry Andric /// exited. This state can change the emission states of subsequent uses 15895ffd83dbSDimitry Andric /// of functions. 15905ffd83dbSDimitry Andric /// 15915ffd83dbSDimitry Andric /// Conceptually the functions or variables to be visited form a use graph 15925ffd83dbSDimitry Andric /// where the parent node uses the child node. At any point of the visit, 15935ffd83dbSDimitry Andric /// the tree nodes traversed from the tree root to the current node form a use 15945ffd83dbSDimitry Andric /// stack. The emission state of the current node depends on two factors: 15955ffd83dbSDimitry Andric /// 1. the emission state of the root node 15965ffd83dbSDimitry Andric /// 2. whether the current node is in OpenMP device context 15975ffd83dbSDimitry Andric /// If the function is decided to be emitted, its contained deferred diagnostics 15985ffd83dbSDimitry Andric /// are emitted, together with the information about the use stack. 15995ffd83dbSDimitry Andric /// 16005ffd83dbSDimitry Andric class DeferredDiagnosticsEmitter 16015ffd83dbSDimitry Andric : public UsedDeclVisitor<DeferredDiagnosticsEmitter> { 16025ffd83dbSDimitry Andric public: 16035ffd83dbSDimitry Andric typedef UsedDeclVisitor<DeferredDiagnosticsEmitter> Inherited; 16045ffd83dbSDimitry Andric 16055ffd83dbSDimitry Andric // Whether the function is already in the current use-path. 1606e8d8bef9SDimitry Andric llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> InUsePath; 16075ffd83dbSDimitry Andric 16085ffd83dbSDimitry Andric // The current use-path. 16095ffd83dbSDimitry Andric llvm::SmallVector<CanonicalDeclPtr<FunctionDecl>, 4> UsePath; 16105ffd83dbSDimitry Andric 16115ffd83dbSDimitry Andric // Whether the visiting of the function has been done. Done[0] is for the 16125ffd83dbSDimitry Andric // case not in OpenMP device context. Done[1] is for the case in OpenMP 16135ffd83dbSDimitry Andric // device context. We need two sets because diagnostics emission may be 16145ffd83dbSDimitry Andric // different depending on whether it is in OpenMP device context. 1615e8d8bef9SDimitry Andric llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> DoneMap[2]; 16165ffd83dbSDimitry Andric 16175ffd83dbSDimitry Andric // Emission state of the root node of the current use graph. 16185ffd83dbSDimitry Andric bool ShouldEmitRootNode; 16195ffd83dbSDimitry Andric 16205ffd83dbSDimitry Andric // Current OpenMP device context level. It is initialized to 0 and each 16215ffd83dbSDimitry Andric // entering of device context increases it by 1 and each exit decreases 16225ffd83dbSDimitry Andric // it by 1. Non-zero value indicates it is currently in device context. 16235ffd83dbSDimitry Andric unsigned InOMPDeviceContext; 16245ffd83dbSDimitry Andric 16255ffd83dbSDimitry Andric DeferredDiagnosticsEmitter(Sema &S) 16265ffd83dbSDimitry Andric : Inherited(S), ShouldEmitRootNode(false), InOMPDeviceContext(0) {} 16275ffd83dbSDimitry Andric 1628fe6060f1SDimitry Andric bool shouldVisitDiscardedStmt() const { return false; } 1629fe6060f1SDimitry Andric 16305ffd83dbSDimitry Andric void VisitOMPTargetDirective(OMPTargetDirective *Node) { 16315ffd83dbSDimitry Andric ++InOMPDeviceContext; 16325ffd83dbSDimitry Andric Inherited::VisitOMPTargetDirective(Node); 16335ffd83dbSDimitry Andric --InOMPDeviceContext; 16345ffd83dbSDimitry Andric } 16355ffd83dbSDimitry Andric 16365ffd83dbSDimitry Andric void visitUsedDecl(SourceLocation Loc, Decl *D) { 16375ffd83dbSDimitry Andric if (isa<VarDecl>(D)) 16385ffd83dbSDimitry Andric return; 16395ffd83dbSDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(D)) 16405ffd83dbSDimitry Andric checkFunc(Loc, FD); 16415ffd83dbSDimitry Andric else 16425ffd83dbSDimitry Andric Inherited::visitUsedDecl(Loc, D); 16435ffd83dbSDimitry Andric } 16445ffd83dbSDimitry Andric 16455ffd83dbSDimitry Andric void checkVar(VarDecl *VD) { 16465ffd83dbSDimitry Andric assert(VD->isFileVarDecl() && 16475ffd83dbSDimitry Andric "Should only check file-scope variables"); 16485ffd83dbSDimitry Andric if (auto *Init = VD->getInit()) { 16495ffd83dbSDimitry Andric auto DevTy = OMPDeclareTargetDeclAttr::getDeviceType(VD); 16505ffd83dbSDimitry Andric bool IsDev = DevTy && (*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost || 16515ffd83dbSDimitry Andric *DevTy == OMPDeclareTargetDeclAttr::DT_Any); 16525ffd83dbSDimitry Andric if (IsDev) 16535ffd83dbSDimitry Andric ++InOMPDeviceContext; 16545ffd83dbSDimitry Andric this->Visit(Init); 16555ffd83dbSDimitry Andric if (IsDev) 16565ffd83dbSDimitry Andric --InOMPDeviceContext; 16575ffd83dbSDimitry Andric } 16585ffd83dbSDimitry Andric } 16595ffd83dbSDimitry Andric 16605ffd83dbSDimitry Andric void checkFunc(SourceLocation Loc, FunctionDecl *FD) { 16615ffd83dbSDimitry Andric auto &Done = DoneMap[InOMPDeviceContext > 0 ? 1 : 0]; 16625ffd83dbSDimitry Andric FunctionDecl *Caller = UsePath.empty() ? nullptr : UsePath.back(); 16635ffd83dbSDimitry Andric if ((!ShouldEmitRootNode && !S.getLangOpts().OpenMP && !Caller) || 16645ffd83dbSDimitry Andric S.shouldIgnoreInHostDeviceCheck(FD) || InUsePath.count(FD)) 16655ffd83dbSDimitry Andric return; 16665ffd83dbSDimitry Andric // Finalize analysis of OpenMP-specific constructs. 1667e8d8bef9SDimitry Andric if (Caller && S.LangOpts.OpenMP && UsePath.size() == 1 && 1668e8d8bef9SDimitry Andric (ShouldEmitRootNode || InOMPDeviceContext)) 16695ffd83dbSDimitry Andric S.finalizeOpenMPDelayedAnalysis(Caller, FD, Loc); 16705ffd83dbSDimitry Andric if (Caller) 16715ffd83dbSDimitry Andric S.DeviceKnownEmittedFns[FD] = {Caller, Loc}; 16725ffd83dbSDimitry Andric // Always emit deferred diagnostics for the direct users. This does not 16735ffd83dbSDimitry Andric // lead to explosion of diagnostics since each user is visited at most 16745ffd83dbSDimitry Andric // twice. 16755ffd83dbSDimitry Andric if (ShouldEmitRootNode || InOMPDeviceContext) 16765ffd83dbSDimitry Andric emitDeferredDiags(FD, Caller); 16775ffd83dbSDimitry Andric // Do not revisit a function if the function body has been completely 16785ffd83dbSDimitry Andric // visited before. 16795ffd83dbSDimitry Andric if (!Done.insert(FD).second) 16805ffd83dbSDimitry Andric return; 16815ffd83dbSDimitry Andric InUsePath.insert(FD); 16825ffd83dbSDimitry Andric UsePath.push_back(FD); 16835ffd83dbSDimitry Andric if (auto *S = FD->getBody()) { 16845ffd83dbSDimitry Andric this->Visit(S); 16855ffd83dbSDimitry Andric } 16865ffd83dbSDimitry Andric UsePath.pop_back(); 16875ffd83dbSDimitry Andric InUsePath.erase(FD); 16885ffd83dbSDimitry Andric } 16895ffd83dbSDimitry Andric 16905ffd83dbSDimitry Andric void checkRecordedDecl(Decl *D) { 16915ffd83dbSDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(D)) { 16925ffd83dbSDimitry Andric ShouldEmitRootNode = S.getEmissionStatus(FD, /*Final=*/true) == 16935ffd83dbSDimitry Andric Sema::FunctionEmissionStatus::Emitted; 16945ffd83dbSDimitry Andric checkFunc(SourceLocation(), FD); 16955ffd83dbSDimitry Andric } else 16965ffd83dbSDimitry Andric checkVar(cast<VarDecl>(D)); 16975ffd83dbSDimitry Andric } 16985ffd83dbSDimitry Andric 16995ffd83dbSDimitry Andric // Emit any deferred diagnostics for FD 17005ffd83dbSDimitry Andric void emitDeferredDiags(FunctionDecl *FD, bool ShowCallStack) { 17010b57cec5SDimitry Andric auto It = S.DeviceDeferredDiags.find(FD); 17020b57cec5SDimitry Andric if (It == S.DeviceDeferredDiags.end()) 17030b57cec5SDimitry Andric return; 17040b57cec5SDimitry Andric bool HasWarningOrError = false; 17055ffd83dbSDimitry Andric bool FirstDiag = true; 17060b57cec5SDimitry Andric for (PartialDiagnosticAt &PDAt : It->second) { 17075ffd83dbSDimitry Andric // Respect error limit. 17085ffd83dbSDimitry Andric if (S.Diags.hasFatalErrorOccurred()) 17095ffd83dbSDimitry Andric return; 17100b57cec5SDimitry Andric const SourceLocation &Loc = PDAt.first; 17110b57cec5SDimitry Andric const PartialDiagnostic &PD = PDAt.second; 17125ffd83dbSDimitry Andric HasWarningOrError |= 17135ffd83dbSDimitry Andric S.getDiagnostics().getDiagnosticLevel(PD.getDiagID(), Loc) >= 17145ffd83dbSDimitry Andric DiagnosticsEngine::Warning; 17155ffd83dbSDimitry Andric { 17160b57cec5SDimitry Andric DiagnosticBuilder Builder(S.Diags.Report(Loc, PD.getDiagID())); 17170b57cec5SDimitry Andric PD.Emit(Builder); 17180b57cec5SDimitry Andric } 17195ffd83dbSDimitry Andric // Emit the note on the first diagnostic in case too many diagnostics 17205ffd83dbSDimitry Andric // cause the note not emitted. 17215ffd83dbSDimitry Andric if (FirstDiag && HasWarningOrError && ShowCallStack) { 17220b57cec5SDimitry Andric emitCallStackNotes(S, FD); 17235ffd83dbSDimitry Andric FirstDiag = false; 17245ffd83dbSDimitry Andric } 17255ffd83dbSDimitry Andric } 17265ffd83dbSDimitry Andric } 17275ffd83dbSDimitry Andric }; 17285ffd83dbSDimitry Andric } // namespace 17295ffd83dbSDimitry Andric 17305ffd83dbSDimitry Andric void Sema::emitDeferredDiags() { 17315ffd83dbSDimitry Andric if (ExternalSource) 17325ffd83dbSDimitry Andric ExternalSource->ReadDeclsToCheckForDeferredDiags( 17335ffd83dbSDimitry Andric DeclsToCheckForDeferredDiags); 17345ffd83dbSDimitry Andric 17355ffd83dbSDimitry Andric if ((DeviceDeferredDiags.empty() && !LangOpts.OpenMP) || 17365ffd83dbSDimitry Andric DeclsToCheckForDeferredDiags.empty()) 17375ffd83dbSDimitry Andric return; 17385ffd83dbSDimitry Andric 17395ffd83dbSDimitry Andric DeferredDiagnosticsEmitter DDE(*this); 17405ffd83dbSDimitry Andric for (auto D : DeclsToCheckForDeferredDiags) 17415ffd83dbSDimitry Andric DDE.checkRecordedDecl(D); 17420b57cec5SDimitry Andric } 17430b57cec5SDimitry Andric 17440b57cec5SDimitry Andric // In CUDA, there are some constructs which may appear in semantically-valid 17450b57cec5SDimitry Andric // code, but trigger errors if we ever generate code for the function in which 17460b57cec5SDimitry Andric // they appear. Essentially every construct you're not allowed to use on the 17470b57cec5SDimitry Andric // device falls into this category, because you are allowed to use these 17480b57cec5SDimitry Andric // constructs in a __host__ __device__ function, but only if that function is 17490b57cec5SDimitry Andric // never codegen'ed on the device. 17500b57cec5SDimitry Andric // 17510b57cec5SDimitry Andric // To handle semantic checking for these constructs, we keep track of the set of 17520b57cec5SDimitry Andric // functions we know will be emitted, either because we could tell a priori that 17530b57cec5SDimitry Andric // they would be emitted, or because they were transitively called by a 17540b57cec5SDimitry Andric // known-emitted function. 17550b57cec5SDimitry Andric // 17560b57cec5SDimitry Andric // We also keep a partial call graph of which not-known-emitted functions call 17570b57cec5SDimitry Andric // which other not-known-emitted functions. 17580b57cec5SDimitry Andric // 17590b57cec5SDimitry Andric // When we see something which is illegal if the current function is emitted 17600b57cec5SDimitry Andric // (usually by way of CUDADiagIfDeviceCode, CUDADiagIfHostCode, or 17610b57cec5SDimitry Andric // CheckCUDACall), we first check if the current function is known-emitted. If 17620b57cec5SDimitry Andric // so, we immediately output the diagnostic. 17630b57cec5SDimitry Andric // 17640b57cec5SDimitry Andric // Otherwise, we "defer" the diagnostic. It sits in Sema::DeviceDeferredDiags 17650b57cec5SDimitry Andric // until we discover that the function is known-emitted, at which point we take 17660b57cec5SDimitry Andric // it out of this map and emit the diagnostic. 17670b57cec5SDimitry Andric 1768e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder::SemaDiagnosticBuilder(Kind K, SourceLocation Loc, 1769e8d8bef9SDimitry Andric unsigned DiagID, 1770e8d8bef9SDimitry Andric FunctionDecl *Fn, Sema &S) 17710b57cec5SDimitry Andric : S(S), Loc(Loc), DiagID(DiagID), Fn(Fn), 17720b57cec5SDimitry Andric ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) { 17730b57cec5SDimitry Andric switch (K) { 17740b57cec5SDimitry Andric case K_Nop: 17750b57cec5SDimitry Andric break; 17760b57cec5SDimitry Andric case K_Immediate: 17770b57cec5SDimitry Andric case K_ImmediateWithCallStack: 1778e8d8bef9SDimitry Andric ImmediateDiag.emplace( 1779e8d8bef9SDimitry Andric ImmediateDiagBuilder(S.Diags.Report(Loc, DiagID), S, DiagID)); 17800b57cec5SDimitry Andric break; 17810b57cec5SDimitry Andric case K_Deferred: 17820b57cec5SDimitry Andric assert(Fn && "Must have a function to attach the deferred diag to."); 17830b57cec5SDimitry Andric auto &Diags = S.DeviceDeferredDiags[Fn]; 17840b57cec5SDimitry Andric PartialDiagId.emplace(Diags.size()); 17850b57cec5SDimitry Andric Diags.emplace_back(Loc, S.PDiag(DiagID)); 17860b57cec5SDimitry Andric break; 17870b57cec5SDimitry Andric } 17880b57cec5SDimitry Andric } 17890b57cec5SDimitry Andric 1790e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder::SemaDiagnosticBuilder(SemaDiagnosticBuilder &&D) 17910b57cec5SDimitry Andric : S(D.S), Loc(D.Loc), DiagID(D.DiagID), Fn(D.Fn), 17920b57cec5SDimitry Andric ShowCallStack(D.ShowCallStack), ImmediateDiag(D.ImmediateDiag), 17930b57cec5SDimitry Andric PartialDiagId(D.PartialDiagId) { 17940b57cec5SDimitry Andric // Clean the previous diagnostics. 17950b57cec5SDimitry Andric D.ShowCallStack = false; 17960b57cec5SDimitry Andric D.ImmediateDiag.reset(); 17970b57cec5SDimitry Andric D.PartialDiagId.reset(); 17980b57cec5SDimitry Andric } 17990b57cec5SDimitry Andric 1800e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { 18010b57cec5SDimitry Andric if (ImmediateDiag) { 18020b57cec5SDimitry Andric // Emit our diagnostic and, if it was a warning or error, output a callstack 18030b57cec5SDimitry Andric // if Fn isn't a priori known-emitted. 18040b57cec5SDimitry Andric bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel( 18050b57cec5SDimitry Andric DiagID, Loc) >= DiagnosticsEngine::Warning; 18060b57cec5SDimitry Andric ImmediateDiag.reset(); // Emit the immediate diag. 18070b57cec5SDimitry Andric if (IsWarningOrError && ShowCallStack) 18080b57cec5SDimitry Andric emitCallStackNotes(S, Fn); 18090b57cec5SDimitry Andric } else { 18100b57cec5SDimitry Andric assert((!PartialDiagId || ShowCallStack) && 18110b57cec5SDimitry Andric "Must always show call stack for deferred diags."); 18120b57cec5SDimitry Andric } 18130b57cec5SDimitry Andric } 18140b57cec5SDimitry Andric 1815d409305fSDimitry Andric Sema::SemaDiagnosticBuilder 1816d409305fSDimitry Andric Sema::targetDiag(SourceLocation Loc, unsigned DiagID, FunctionDecl *FD) { 1817d409305fSDimitry Andric FD = FD ? FD : getCurFunctionDecl(); 1818a7dea167SDimitry Andric if (LangOpts.OpenMP) 1819d409305fSDimitry Andric return LangOpts.OpenMPIsDevice ? diagIfOpenMPDeviceCode(Loc, DiagID, FD) 1820d409305fSDimitry Andric : diagIfOpenMPHostCode(Loc, DiagID, FD); 18210b57cec5SDimitry Andric if (getLangOpts().CUDA) 18220b57cec5SDimitry Andric return getLangOpts().CUDAIsDevice ? CUDADiagIfDeviceCode(Loc, DiagID) 18230b57cec5SDimitry Andric : CUDADiagIfHostCode(Loc, DiagID); 18245ffd83dbSDimitry Andric 18255ffd83dbSDimitry Andric if (getLangOpts().SYCLIsDevice) 18265ffd83dbSDimitry Andric return SYCLDiagIfDeviceCode(Loc, DiagID); 18275ffd83dbSDimitry Andric 1828e8d8bef9SDimitry Andric return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate, Loc, DiagID, 1829d409305fSDimitry Andric FD, *this); 18300b57cec5SDimitry Andric } 18310b57cec5SDimitry Andric 1832e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID, 1833e8d8bef9SDimitry Andric bool DeferHint) { 1834e8d8bef9SDimitry Andric bool IsError = Diags.getDiagnosticIDs()->isDefaultMappingAsError(DiagID); 1835e8d8bef9SDimitry Andric bool ShouldDefer = getLangOpts().CUDA && LangOpts.GPUDeferDiag && 1836e8d8bef9SDimitry Andric DiagnosticIDs::isDeferrable(DiagID) && 1837fe6060f1SDimitry Andric (DeferHint || DeferDiags || !IsError); 1838e8d8bef9SDimitry Andric auto SetIsLastErrorImmediate = [&](bool Flag) { 1839e8d8bef9SDimitry Andric if (IsError) 1840e8d8bef9SDimitry Andric IsLastErrorImmediate = Flag; 1841e8d8bef9SDimitry Andric }; 1842e8d8bef9SDimitry Andric if (!ShouldDefer) { 1843e8d8bef9SDimitry Andric SetIsLastErrorImmediate(true); 1844e8d8bef9SDimitry Andric return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate, Loc, 1845e8d8bef9SDimitry Andric DiagID, getCurFunctionDecl(), *this); 1846e8d8bef9SDimitry Andric } 1847e8d8bef9SDimitry Andric 1848d409305fSDimitry Andric SemaDiagnosticBuilder DB = getLangOpts().CUDAIsDevice 1849e8d8bef9SDimitry Andric ? CUDADiagIfDeviceCode(Loc, DiagID) 1850e8d8bef9SDimitry Andric : CUDADiagIfHostCode(Loc, DiagID); 1851e8d8bef9SDimitry Andric SetIsLastErrorImmediate(DB.isImmediate()); 1852e8d8bef9SDimitry Andric return DB; 1853e8d8bef9SDimitry Andric } 1854e8d8bef9SDimitry Andric 1855d409305fSDimitry Andric void Sema::checkDeviceDecl(ValueDecl *D, SourceLocation Loc) { 18565ffd83dbSDimitry Andric if (isUnevaluatedContext()) 18575ffd83dbSDimitry Andric return; 18585ffd83dbSDimitry Andric 18595ffd83dbSDimitry Andric Decl *C = cast<Decl>(getCurLexicalContext()); 18605ffd83dbSDimitry Andric 18615ffd83dbSDimitry Andric // Memcpy operations for structs containing a member with unsupported type 18625ffd83dbSDimitry Andric // are ok, though. 18635ffd83dbSDimitry Andric if (const auto *MD = dyn_cast<CXXMethodDecl>(C)) { 18645ffd83dbSDimitry Andric if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && 18655ffd83dbSDimitry Andric MD->isTrivial()) 18665ffd83dbSDimitry Andric return; 18675ffd83dbSDimitry Andric 18685ffd83dbSDimitry Andric if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(MD)) 18695ffd83dbSDimitry Andric if (Ctor->isCopyOrMoveConstructor() && Ctor->isTrivial()) 18705ffd83dbSDimitry Andric return; 18715ffd83dbSDimitry Andric } 18725ffd83dbSDimitry Andric 1873d409305fSDimitry Andric // Try to associate errors with the lexical context, if that is a function, or 1874d409305fSDimitry Andric // the value declaration otherwise. 1875d409305fSDimitry Andric FunctionDecl *FD = 1876d409305fSDimitry Andric isa<FunctionDecl>(C) ? cast<FunctionDecl>(C) : dyn_cast<FunctionDecl>(D); 18775ffd83dbSDimitry Andric auto CheckType = [&](QualType Ty) { 18785ffd83dbSDimitry Andric if (Ty->isDependentType()) 18795ffd83dbSDimitry Andric return; 18805ffd83dbSDimitry Andric 1881e8d8bef9SDimitry Andric if (Ty->isExtIntType()) { 1882e8d8bef9SDimitry Andric if (!Context.getTargetInfo().hasExtIntType()) { 1883d409305fSDimitry Andric targetDiag(Loc, diag::err_device_unsupported_type, FD) 1884e8d8bef9SDimitry Andric << D << false /*show bit size*/ << 0 /*bitsize*/ 1885e8d8bef9SDimitry Andric << Ty << Context.getTargetInfo().getTriple().str(); 1886e8d8bef9SDimitry Andric } 1887e8d8bef9SDimitry Andric return; 1888e8d8bef9SDimitry Andric } 1889e8d8bef9SDimitry Andric 18905ffd83dbSDimitry Andric if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) || 18915ffd83dbSDimitry Andric ((Ty->isFloat128Type() || 18925ffd83dbSDimitry Andric (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) && 18935ffd83dbSDimitry Andric !Context.getTargetInfo().hasFloat128Type()) || 18945ffd83dbSDimitry Andric (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 && 18955ffd83dbSDimitry Andric !Context.getTargetInfo().hasInt128Type())) { 1896d409305fSDimitry Andric if (targetDiag(Loc, diag::err_device_unsupported_type, FD) 1897e8d8bef9SDimitry Andric << D << true /*show bit size*/ 1898e8d8bef9SDimitry Andric << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty 1899d409305fSDimitry Andric << Context.getTargetInfo().getTriple().str()) 1900d409305fSDimitry Andric D->setInvalidDecl(); 1901d409305fSDimitry Andric targetDiag(D->getLocation(), diag::note_defined_here, FD) << D; 19025ffd83dbSDimitry Andric } 19035ffd83dbSDimitry Andric }; 19045ffd83dbSDimitry Andric 19055ffd83dbSDimitry Andric QualType Ty = D->getType(); 19065ffd83dbSDimitry Andric CheckType(Ty); 19075ffd83dbSDimitry Andric 19085ffd83dbSDimitry Andric if (const auto *FPTy = dyn_cast<FunctionProtoType>(Ty)) { 19095ffd83dbSDimitry Andric for (const auto &ParamTy : FPTy->param_types()) 19105ffd83dbSDimitry Andric CheckType(ParamTy); 19115ffd83dbSDimitry Andric CheckType(FPTy->getReturnType()); 19125ffd83dbSDimitry Andric } 1913d409305fSDimitry Andric if (const auto *FNPTy = dyn_cast<FunctionNoProtoType>(Ty)) 1914d409305fSDimitry Andric CheckType(FNPTy->getReturnType()); 19155ffd83dbSDimitry Andric } 19165ffd83dbSDimitry Andric 19170b57cec5SDimitry Andric /// Looks through the macro-expansion chain for the given 19180b57cec5SDimitry Andric /// location, looking for a macro expansion with the given name. 19190b57cec5SDimitry Andric /// If one is found, returns true and sets the location to that 19200b57cec5SDimitry Andric /// expansion loc. 19210b57cec5SDimitry Andric bool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) { 19220b57cec5SDimitry Andric SourceLocation loc = locref; 19230b57cec5SDimitry Andric if (!loc.isMacroID()) return false; 19240b57cec5SDimitry Andric 19250b57cec5SDimitry Andric // There's no good way right now to look at the intermediate 19260b57cec5SDimitry Andric // expansions, so just jump to the expansion location. 19270b57cec5SDimitry Andric loc = getSourceManager().getExpansionLoc(loc); 19280b57cec5SDimitry Andric 19290b57cec5SDimitry Andric // If that's written with the name, stop here. 1930e8d8bef9SDimitry Andric SmallString<16> buffer; 19310b57cec5SDimitry Andric if (getPreprocessor().getSpelling(loc, buffer) == name) { 19320b57cec5SDimitry Andric locref = loc; 19330b57cec5SDimitry Andric return true; 19340b57cec5SDimitry Andric } 19350b57cec5SDimitry Andric return false; 19360b57cec5SDimitry Andric } 19370b57cec5SDimitry Andric 19380b57cec5SDimitry Andric /// Determines the active Scope associated with the given declaration 19390b57cec5SDimitry Andric /// context. 19400b57cec5SDimitry Andric /// 19410b57cec5SDimitry Andric /// This routine maps a declaration context to the active Scope object that 19420b57cec5SDimitry Andric /// represents that declaration context in the parser. It is typically used 19430b57cec5SDimitry Andric /// from "scope-less" code (e.g., template instantiation, lazy creation of 19440b57cec5SDimitry Andric /// declarations) that injects a name for name-lookup purposes and, therefore, 19450b57cec5SDimitry Andric /// must update the Scope. 19460b57cec5SDimitry Andric /// 19470b57cec5SDimitry Andric /// \returns The scope corresponding to the given declaraion context, or NULL 19480b57cec5SDimitry Andric /// if no such scope is open. 19490b57cec5SDimitry Andric Scope *Sema::getScopeForContext(DeclContext *Ctx) { 19500b57cec5SDimitry Andric 19510b57cec5SDimitry Andric if (!Ctx) 19520b57cec5SDimitry Andric return nullptr; 19530b57cec5SDimitry Andric 19540b57cec5SDimitry Andric Ctx = Ctx->getPrimaryContext(); 19550b57cec5SDimitry Andric for (Scope *S = getCurScope(); S; S = S->getParent()) { 19560b57cec5SDimitry Andric // Ignore scopes that cannot have declarations. This is important for 19570b57cec5SDimitry Andric // out-of-line definitions of static class members. 19580b57cec5SDimitry Andric if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) 19590b57cec5SDimitry Andric if (DeclContext *Entity = S->getEntity()) 19600b57cec5SDimitry Andric if (Ctx == Entity->getPrimaryContext()) 19610b57cec5SDimitry Andric return S; 19620b57cec5SDimitry Andric } 19630b57cec5SDimitry Andric 19640b57cec5SDimitry Andric return nullptr; 19650b57cec5SDimitry Andric } 19660b57cec5SDimitry Andric 19670b57cec5SDimitry Andric /// Enter a new function scope 19680b57cec5SDimitry Andric void Sema::PushFunctionScope() { 19690b57cec5SDimitry Andric if (FunctionScopes.empty() && CachedFunctionScope) { 19700b57cec5SDimitry Andric // Use CachedFunctionScope to avoid allocating memory when possible. 19710b57cec5SDimitry Andric CachedFunctionScope->Clear(); 19720b57cec5SDimitry Andric FunctionScopes.push_back(CachedFunctionScope.release()); 19730b57cec5SDimitry Andric } else { 19740b57cec5SDimitry Andric FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics())); 19750b57cec5SDimitry Andric } 19760b57cec5SDimitry Andric if (LangOpts.OpenMP) 19770b57cec5SDimitry Andric pushOpenMPFunctionRegion(); 19780b57cec5SDimitry Andric } 19790b57cec5SDimitry Andric 19800b57cec5SDimitry Andric void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { 19810b57cec5SDimitry Andric FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(), 19820b57cec5SDimitry Andric BlockScope, Block)); 19830b57cec5SDimitry Andric } 19840b57cec5SDimitry Andric 19850b57cec5SDimitry Andric LambdaScopeInfo *Sema::PushLambdaScope() { 19860b57cec5SDimitry Andric LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics()); 19870b57cec5SDimitry Andric FunctionScopes.push_back(LSI); 19880b57cec5SDimitry Andric return LSI; 19890b57cec5SDimitry Andric } 19900b57cec5SDimitry Andric 19910b57cec5SDimitry Andric void Sema::RecordParsingTemplateParameterDepth(unsigned Depth) { 19920b57cec5SDimitry Andric if (LambdaScopeInfo *const LSI = getCurLambda()) { 19930b57cec5SDimitry Andric LSI->AutoTemplateParameterDepth = Depth; 19940b57cec5SDimitry Andric return; 19950b57cec5SDimitry Andric } 19960b57cec5SDimitry Andric llvm_unreachable( 19970b57cec5SDimitry Andric "Remove assertion if intentionally called in a non-lambda context."); 19980b57cec5SDimitry Andric } 19990b57cec5SDimitry Andric 20000b57cec5SDimitry Andric // Check that the type of the VarDecl has an accessible copy constructor and 20010b57cec5SDimitry Andric // resolve its destructor's exception specification. 2002fe6060f1SDimitry Andric // This also performs initialization of block variables when they are moved 2003fe6060f1SDimitry Andric // to the heap. It uses the same rules as applicable for implicit moves 2004fe6060f1SDimitry Andric // according to the C++ standard in effect ([class.copy.elision]p3). 20050b57cec5SDimitry Andric static void checkEscapingByref(VarDecl *VD, Sema &S) { 20060b57cec5SDimitry Andric QualType T = VD->getType(); 20070b57cec5SDimitry Andric EnterExpressionEvaluationContext scope( 20080b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); 20090b57cec5SDimitry Andric SourceLocation Loc = VD->getLocation(); 20100b57cec5SDimitry Andric Expr *VarRef = 20110b57cec5SDimitry Andric new (S.Context) DeclRefExpr(S.Context, VD, false, T, VK_LValue, Loc); 2012fe6060f1SDimitry Andric ExprResult Result; 2013fe6060f1SDimitry Andric auto IE = InitializedEntity::InitializeBlock(Loc, T, false); 2014fe6060f1SDimitry Andric if (S.getLangOpts().CPlusPlus2b) { 2015fe6060f1SDimitry Andric auto *E = ImplicitCastExpr::Create(S.Context, T, CK_NoOp, VarRef, nullptr, 2016fe6060f1SDimitry Andric VK_XValue, FPOptionsOverride()); 2017fe6060f1SDimitry Andric Result = S.PerformCopyInitialization(IE, SourceLocation(), E); 2018fe6060f1SDimitry Andric } else { 2019fe6060f1SDimitry Andric Result = S.PerformMoveOrCopyInitialization( 2020fe6060f1SDimitry Andric IE, Sema::NamedReturnInfo{VD, Sema::NamedReturnInfo::MoveEligible}, 2021fe6060f1SDimitry Andric VarRef); 2022fe6060f1SDimitry Andric } 2023fe6060f1SDimitry Andric 20240b57cec5SDimitry Andric if (!Result.isInvalid()) { 20250b57cec5SDimitry Andric Result = S.MaybeCreateExprWithCleanups(Result); 20260b57cec5SDimitry Andric Expr *Init = Result.getAs<Expr>(); 20270b57cec5SDimitry Andric S.Context.setBlockVarCopyInit(VD, Init, S.canThrow(Init)); 20280b57cec5SDimitry Andric } 20290b57cec5SDimitry Andric 20300b57cec5SDimitry Andric // The destructor's exception specification is needed when IRGen generates 20310b57cec5SDimitry Andric // block copy/destroy functions. Resolve it here. 20320b57cec5SDimitry Andric if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) 20330b57cec5SDimitry Andric if (CXXDestructorDecl *DD = RD->getDestructor()) { 20340b57cec5SDimitry Andric auto *FPT = DD->getType()->getAs<FunctionProtoType>(); 20350b57cec5SDimitry Andric S.ResolveExceptionSpec(Loc, FPT); 20360b57cec5SDimitry Andric } 20370b57cec5SDimitry Andric } 20380b57cec5SDimitry Andric 20390b57cec5SDimitry Andric static void markEscapingByrefs(const FunctionScopeInfo &FSI, Sema &S) { 20400b57cec5SDimitry Andric // Set the EscapingByref flag of __block variables captured by 20410b57cec5SDimitry Andric // escaping blocks. 20420b57cec5SDimitry Andric for (const BlockDecl *BD : FSI.Blocks) { 20430b57cec5SDimitry Andric for (const BlockDecl::Capture &BC : BD->captures()) { 20440b57cec5SDimitry Andric VarDecl *VD = BC.getVariable(); 20450b57cec5SDimitry Andric if (VD->hasAttr<BlocksAttr>()) { 20460b57cec5SDimitry Andric // Nothing to do if this is a __block variable captured by a 20470b57cec5SDimitry Andric // non-escaping block. 20480b57cec5SDimitry Andric if (BD->doesNotEscape()) 20490b57cec5SDimitry Andric continue; 20500b57cec5SDimitry Andric VD->setEscapingByref(); 20510b57cec5SDimitry Andric } 20520b57cec5SDimitry Andric // Check whether the captured variable is or contains an object of 20530b57cec5SDimitry Andric // non-trivial C union type. 20540b57cec5SDimitry Andric QualType CapType = BC.getVariable()->getType(); 20550b57cec5SDimitry Andric if (CapType.hasNonTrivialToPrimitiveDestructCUnion() || 20560b57cec5SDimitry Andric CapType.hasNonTrivialToPrimitiveCopyCUnion()) 20570b57cec5SDimitry Andric S.checkNonTrivialCUnion(BC.getVariable()->getType(), 20580b57cec5SDimitry Andric BD->getCaretLocation(), 20590b57cec5SDimitry Andric Sema::NTCUC_BlockCapture, 20600b57cec5SDimitry Andric Sema::NTCUK_Destruct|Sema::NTCUK_Copy); 20610b57cec5SDimitry Andric } 20620b57cec5SDimitry Andric } 20630b57cec5SDimitry Andric 20640b57cec5SDimitry Andric for (VarDecl *VD : FSI.ByrefBlockVars) { 20650b57cec5SDimitry Andric // __block variables might require us to capture a copy-initializer. 20660b57cec5SDimitry Andric if (!VD->isEscapingByref()) 20670b57cec5SDimitry Andric continue; 20680b57cec5SDimitry Andric // It's currently invalid to ever have a __block variable with an 20690b57cec5SDimitry Andric // array type; should we diagnose that here? 20700b57cec5SDimitry Andric // Regardless, we don't want to ignore array nesting when 20710b57cec5SDimitry Andric // constructing this copy. 20720b57cec5SDimitry Andric if (VD->getType()->isStructureOrClassType()) 20730b57cec5SDimitry Andric checkEscapingByref(VD, S); 20740b57cec5SDimitry Andric } 20750b57cec5SDimitry Andric } 20760b57cec5SDimitry Andric 20770b57cec5SDimitry Andric /// Pop a function (or block or lambda or captured region) scope from the stack. 20780b57cec5SDimitry Andric /// 20790b57cec5SDimitry Andric /// \param WP The warning policy to use for CFG-based warnings, or null if such 20800b57cec5SDimitry Andric /// warnings should not be produced. 20810b57cec5SDimitry Andric /// \param D The declaration corresponding to this function scope, if producing 20820b57cec5SDimitry Andric /// CFG-based warnings. 20830b57cec5SDimitry Andric /// \param BlockType The type of the block expression, if D is a BlockDecl. 20840b57cec5SDimitry Andric Sema::PoppedFunctionScopePtr 20850b57cec5SDimitry Andric Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP, 20860b57cec5SDimitry Andric const Decl *D, QualType BlockType) { 20870b57cec5SDimitry Andric assert(!FunctionScopes.empty() && "mismatched push/pop!"); 20880b57cec5SDimitry Andric 20890b57cec5SDimitry Andric markEscapingByrefs(*FunctionScopes.back(), *this); 20900b57cec5SDimitry Andric 20910b57cec5SDimitry Andric PoppedFunctionScopePtr Scope(FunctionScopes.pop_back_val(), 20920b57cec5SDimitry Andric PoppedFunctionScopeDeleter(this)); 20930b57cec5SDimitry Andric 20940b57cec5SDimitry Andric if (LangOpts.OpenMP) 20950b57cec5SDimitry Andric popOpenMPFunctionRegion(Scope.get()); 20960b57cec5SDimitry Andric 20970b57cec5SDimitry Andric // Issue any analysis-based warnings. 20980b57cec5SDimitry Andric if (WP && D) 20990b57cec5SDimitry Andric AnalysisWarnings.IssueWarnings(*WP, Scope.get(), D, BlockType); 21000b57cec5SDimitry Andric else 21010b57cec5SDimitry Andric for (const auto &PUD : Scope->PossiblyUnreachableDiags) 21020b57cec5SDimitry Andric Diag(PUD.Loc, PUD.PD); 21030b57cec5SDimitry Andric 21040b57cec5SDimitry Andric return Scope; 21050b57cec5SDimitry Andric } 21060b57cec5SDimitry Andric 21070b57cec5SDimitry Andric void Sema::PoppedFunctionScopeDeleter:: 21080b57cec5SDimitry Andric operator()(sema::FunctionScopeInfo *Scope) const { 21090b57cec5SDimitry Andric // Stash the function scope for later reuse if it's for a normal function. 21100b57cec5SDimitry Andric if (Scope->isPlainFunction() && !Self->CachedFunctionScope) 21110b57cec5SDimitry Andric Self->CachedFunctionScope.reset(Scope); 21120b57cec5SDimitry Andric else 21130b57cec5SDimitry Andric delete Scope; 21140b57cec5SDimitry Andric } 21150b57cec5SDimitry Andric 21160b57cec5SDimitry Andric void Sema::PushCompoundScope(bool IsStmtExpr) { 21170b57cec5SDimitry Andric getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo(IsStmtExpr)); 21180b57cec5SDimitry Andric } 21190b57cec5SDimitry Andric 21200b57cec5SDimitry Andric void Sema::PopCompoundScope() { 21210b57cec5SDimitry Andric FunctionScopeInfo *CurFunction = getCurFunction(); 21220b57cec5SDimitry Andric assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop"); 21230b57cec5SDimitry Andric 21240b57cec5SDimitry Andric CurFunction->CompoundScopes.pop_back(); 21250b57cec5SDimitry Andric } 21260b57cec5SDimitry Andric 21270b57cec5SDimitry Andric /// Determine whether any errors occurred within this function/method/ 21280b57cec5SDimitry Andric /// block. 21290b57cec5SDimitry Andric bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const { 21305ffd83dbSDimitry Andric return getCurFunction()->hasUnrecoverableErrorOccurred(); 21310b57cec5SDimitry Andric } 21320b57cec5SDimitry Andric 21330b57cec5SDimitry Andric void Sema::setFunctionHasBranchIntoScope() { 21340b57cec5SDimitry Andric if (!FunctionScopes.empty()) 21350b57cec5SDimitry Andric FunctionScopes.back()->setHasBranchIntoScope(); 21360b57cec5SDimitry Andric } 21370b57cec5SDimitry Andric 21380b57cec5SDimitry Andric void Sema::setFunctionHasBranchProtectedScope() { 21390b57cec5SDimitry Andric if (!FunctionScopes.empty()) 21400b57cec5SDimitry Andric FunctionScopes.back()->setHasBranchProtectedScope(); 21410b57cec5SDimitry Andric } 21420b57cec5SDimitry Andric 21430b57cec5SDimitry Andric void Sema::setFunctionHasIndirectGoto() { 21440b57cec5SDimitry Andric if (!FunctionScopes.empty()) 21450b57cec5SDimitry Andric FunctionScopes.back()->setHasIndirectGoto(); 21460b57cec5SDimitry Andric } 21470b57cec5SDimitry Andric 2148fe6060f1SDimitry Andric void Sema::setFunctionHasMustTail() { 2149fe6060f1SDimitry Andric if (!FunctionScopes.empty()) 2150fe6060f1SDimitry Andric FunctionScopes.back()->setHasMustTail(); 2151fe6060f1SDimitry Andric } 2152fe6060f1SDimitry Andric 21530b57cec5SDimitry Andric BlockScopeInfo *Sema::getCurBlock() { 21540b57cec5SDimitry Andric if (FunctionScopes.empty()) 21550b57cec5SDimitry Andric return nullptr; 21560b57cec5SDimitry Andric 21570b57cec5SDimitry Andric auto CurBSI = dyn_cast<BlockScopeInfo>(FunctionScopes.back()); 21580b57cec5SDimitry Andric if (CurBSI && CurBSI->TheDecl && 21590b57cec5SDimitry Andric !CurBSI->TheDecl->Encloses(CurContext)) { 21600b57cec5SDimitry Andric // We have switched contexts due to template instantiation. 21610b57cec5SDimitry Andric assert(!CodeSynthesisContexts.empty()); 21620b57cec5SDimitry Andric return nullptr; 21630b57cec5SDimitry Andric } 21640b57cec5SDimitry Andric 21650b57cec5SDimitry Andric return CurBSI; 21660b57cec5SDimitry Andric } 21670b57cec5SDimitry Andric 21680b57cec5SDimitry Andric FunctionScopeInfo *Sema::getEnclosingFunction() const { 21690b57cec5SDimitry Andric if (FunctionScopes.empty()) 21700b57cec5SDimitry Andric return nullptr; 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric for (int e = FunctionScopes.size() - 1; e >= 0; --e) { 21730b57cec5SDimitry Andric if (isa<sema::BlockScopeInfo>(FunctionScopes[e])) 21740b57cec5SDimitry Andric continue; 21750b57cec5SDimitry Andric return FunctionScopes[e]; 21760b57cec5SDimitry Andric } 21770b57cec5SDimitry Andric return nullptr; 21780b57cec5SDimitry Andric } 21790b57cec5SDimitry Andric 2180a7dea167SDimitry Andric LambdaScopeInfo *Sema::getEnclosingLambda() const { 2181a7dea167SDimitry Andric for (auto *Scope : llvm::reverse(FunctionScopes)) { 2182a7dea167SDimitry Andric if (auto *LSI = dyn_cast<sema::LambdaScopeInfo>(Scope)) { 2183a7dea167SDimitry Andric if (LSI->Lambda && !LSI->Lambda->Encloses(CurContext)) { 2184a7dea167SDimitry Andric // We have switched contexts due to template instantiation. 2185a7dea167SDimitry Andric // FIXME: We should swap out the FunctionScopes during code synthesis 2186a7dea167SDimitry Andric // so that we don't need to check for this. 2187a7dea167SDimitry Andric assert(!CodeSynthesisContexts.empty()); 2188a7dea167SDimitry Andric return nullptr; 2189a7dea167SDimitry Andric } 2190a7dea167SDimitry Andric return LSI; 2191a7dea167SDimitry Andric } 2192a7dea167SDimitry Andric } 2193a7dea167SDimitry Andric return nullptr; 2194a7dea167SDimitry Andric } 2195a7dea167SDimitry Andric 21960b57cec5SDimitry Andric LambdaScopeInfo *Sema::getCurLambda(bool IgnoreNonLambdaCapturingScope) { 21970b57cec5SDimitry Andric if (FunctionScopes.empty()) 21980b57cec5SDimitry Andric return nullptr; 21990b57cec5SDimitry Andric 22000b57cec5SDimitry Andric auto I = FunctionScopes.rbegin(); 22010b57cec5SDimitry Andric if (IgnoreNonLambdaCapturingScope) { 22020b57cec5SDimitry Andric auto E = FunctionScopes.rend(); 22030b57cec5SDimitry Andric while (I != E && isa<CapturingScopeInfo>(*I) && !isa<LambdaScopeInfo>(*I)) 22040b57cec5SDimitry Andric ++I; 22050b57cec5SDimitry Andric if (I == E) 22060b57cec5SDimitry Andric return nullptr; 22070b57cec5SDimitry Andric } 22080b57cec5SDimitry Andric auto *CurLSI = dyn_cast<LambdaScopeInfo>(*I); 22090b57cec5SDimitry Andric if (CurLSI && CurLSI->Lambda && 22100b57cec5SDimitry Andric !CurLSI->Lambda->Encloses(CurContext)) { 22110b57cec5SDimitry Andric // We have switched contexts due to template instantiation. 22120b57cec5SDimitry Andric assert(!CodeSynthesisContexts.empty()); 22130b57cec5SDimitry Andric return nullptr; 22140b57cec5SDimitry Andric } 22150b57cec5SDimitry Andric 22160b57cec5SDimitry Andric return CurLSI; 22170b57cec5SDimitry Andric } 2218a7dea167SDimitry Andric 22190b57cec5SDimitry Andric // We have a generic lambda if we parsed auto parameters, or we have 22200b57cec5SDimitry Andric // an associated template parameter list. 22210b57cec5SDimitry Andric LambdaScopeInfo *Sema::getCurGenericLambda() { 22220b57cec5SDimitry Andric if (LambdaScopeInfo *LSI = getCurLambda()) { 22230b57cec5SDimitry Andric return (LSI->TemplateParams.size() || 22240b57cec5SDimitry Andric LSI->GLTemplateParameterList) ? LSI : nullptr; 22250b57cec5SDimitry Andric } 22260b57cec5SDimitry Andric return nullptr; 22270b57cec5SDimitry Andric } 22280b57cec5SDimitry Andric 22290b57cec5SDimitry Andric 22300b57cec5SDimitry Andric void Sema::ActOnComment(SourceRange Comment) { 22310b57cec5SDimitry Andric if (!LangOpts.RetainCommentsFromSystemHeaders && 22320b57cec5SDimitry Andric SourceMgr.isInSystemHeader(Comment.getBegin())) 22330b57cec5SDimitry Andric return; 22340b57cec5SDimitry Andric RawComment RC(SourceMgr, Comment, LangOpts.CommentOpts, false); 22350b57cec5SDimitry Andric if (RC.isAlmostTrailingComment()) { 22360b57cec5SDimitry Andric SourceRange MagicMarkerRange(Comment.getBegin(), 22370b57cec5SDimitry Andric Comment.getBegin().getLocWithOffset(3)); 22380b57cec5SDimitry Andric StringRef MagicMarkerText; 22390b57cec5SDimitry Andric switch (RC.getKind()) { 22400b57cec5SDimitry Andric case RawComment::RCK_OrdinaryBCPL: 22410b57cec5SDimitry Andric MagicMarkerText = "///<"; 22420b57cec5SDimitry Andric break; 22430b57cec5SDimitry Andric case RawComment::RCK_OrdinaryC: 22440b57cec5SDimitry Andric MagicMarkerText = "/**<"; 22450b57cec5SDimitry Andric break; 22460b57cec5SDimitry Andric default: 22470b57cec5SDimitry Andric llvm_unreachable("if this is an almost Doxygen comment, " 22480b57cec5SDimitry Andric "it should be ordinary"); 22490b57cec5SDimitry Andric } 22500b57cec5SDimitry Andric Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) << 22510b57cec5SDimitry Andric FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText); 22520b57cec5SDimitry Andric } 22530b57cec5SDimitry Andric Context.addComment(RC); 22540b57cec5SDimitry Andric } 22550b57cec5SDimitry Andric 22560b57cec5SDimitry Andric // Pin this vtable to this file. 22570b57cec5SDimitry Andric ExternalSemaSource::~ExternalSemaSource() {} 2258480093f4SDimitry Andric char ExternalSemaSource::ID; 22590b57cec5SDimitry Andric 22600b57cec5SDimitry Andric void ExternalSemaSource::ReadMethodPool(Selector Sel) { } 22610b57cec5SDimitry Andric void ExternalSemaSource::updateOutOfDateSelector(Selector Sel) { } 22620b57cec5SDimitry Andric 22630b57cec5SDimitry Andric void ExternalSemaSource::ReadKnownNamespaces( 22640b57cec5SDimitry Andric SmallVectorImpl<NamespaceDecl *> &Namespaces) { 22650b57cec5SDimitry Andric } 22660b57cec5SDimitry Andric 22670b57cec5SDimitry Andric void ExternalSemaSource::ReadUndefinedButUsed( 22680b57cec5SDimitry Andric llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {} 22690b57cec5SDimitry Andric 22700b57cec5SDimitry Andric void ExternalSemaSource::ReadMismatchingDeleteExpressions(llvm::MapVector< 22710b57cec5SDimitry Andric FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &) {} 22720b57cec5SDimitry Andric 22730b57cec5SDimitry Andric /// Figure out if an expression could be turned into a call. 22740b57cec5SDimitry Andric /// 22750b57cec5SDimitry Andric /// Use this when trying to recover from an error where the programmer may have 22760b57cec5SDimitry Andric /// written just the name of a function instead of actually calling it. 22770b57cec5SDimitry Andric /// 22780b57cec5SDimitry Andric /// \param E - The expression to examine. 22790b57cec5SDimitry Andric /// \param ZeroArgCallReturnTy - If the expression can be turned into a call 22800b57cec5SDimitry Andric /// with no arguments, this parameter is set to the type returned by such a 22810b57cec5SDimitry Andric /// call; otherwise, it is set to an empty QualType. 22820b57cec5SDimitry Andric /// \param OverloadSet - If the expression is an overloaded function 22830b57cec5SDimitry Andric /// name, this parameter is populated with the decls of the various overloads. 22840b57cec5SDimitry Andric bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy, 22850b57cec5SDimitry Andric UnresolvedSetImpl &OverloadSet) { 22860b57cec5SDimitry Andric ZeroArgCallReturnTy = QualType(); 22870b57cec5SDimitry Andric OverloadSet.clear(); 22880b57cec5SDimitry Andric 22890b57cec5SDimitry Andric const OverloadExpr *Overloads = nullptr; 22900b57cec5SDimitry Andric bool IsMemExpr = false; 22910b57cec5SDimitry Andric if (E.getType() == Context.OverloadTy) { 22920b57cec5SDimitry Andric OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E)); 22930b57cec5SDimitry Andric 22940b57cec5SDimitry Andric // Ignore overloads that are pointer-to-member constants. 22950b57cec5SDimitry Andric if (FR.HasFormOfMemberPointer) 22960b57cec5SDimitry Andric return false; 22970b57cec5SDimitry Andric 22980b57cec5SDimitry Andric Overloads = FR.Expression; 22990b57cec5SDimitry Andric } else if (E.getType() == Context.BoundMemberTy) { 23000b57cec5SDimitry Andric Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens()); 23010b57cec5SDimitry Andric IsMemExpr = true; 23020b57cec5SDimitry Andric } 23030b57cec5SDimitry Andric 23040b57cec5SDimitry Andric bool Ambiguous = false; 23050b57cec5SDimitry Andric bool IsMV = false; 23060b57cec5SDimitry Andric 23070b57cec5SDimitry Andric if (Overloads) { 23080b57cec5SDimitry Andric for (OverloadExpr::decls_iterator it = Overloads->decls_begin(), 23090b57cec5SDimitry Andric DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) { 23100b57cec5SDimitry Andric OverloadSet.addDecl(*it); 23110b57cec5SDimitry Andric 23120b57cec5SDimitry Andric // Check whether the function is a non-template, non-member which takes no 23130b57cec5SDimitry Andric // arguments. 23140b57cec5SDimitry Andric if (IsMemExpr) 23150b57cec5SDimitry Andric continue; 23160b57cec5SDimitry Andric if (const FunctionDecl *OverloadDecl 23170b57cec5SDimitry Andric = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) { 23180b57cec5SDimitry Andric if (OverloadDecl->getMinRequiredArguments() == 0) { 23190b57cec5SDimitry Andric if (!ZeroArgCallReturnTy.isNull() && !Ambiguous && 23200b57cec5SDimitry Andric (!IsMV || !(OverloadDecl->isCPUDispatchMultiVersion() || 23210b57cec5SDimitry Andric OverloadDecl->isCPUSpecificMultiVersion()))) { 23220b57cec5SDimitry Andric ZeroArgCallReturnTy = QualType(); 23230b57cec5SDimitry Andric Ambiguous = true; 23240b57cec5SDimitry Andric } else { 23250b57cec5SDimitry Andric ZeroArgCallReturnTy = OverloadDecl->getReturnType(); 23260b57cec5SDimitry Andric IsMV = OverloadDecl->isCPUDispatchMultiVersion() || 23270b57cec5SDimitry Andric OverloadDecl->isCPUSpecificMultiVersion(); 23280b57cec5SDimitry Andric } 23290b57cec5SDimitry Andric } 23300b57cec5SDimitry Andric } 23310b57cec5SDimitry Andric } 23320b57cec5SDimitry Andric 23330b57cec5SDimitry Andric // If it's not a member, use better machinery to try to resolve the call 23340b57cec5SDimitry Andric if (!IsMemExpr) 23350b57cec5SDimitry Andric return !ZeroArgCallReturnTy.isNull(); 23360b57cec5SDimitry Andric } 23370b57cec5SDimitry Andric 23380b57cec5SDimitry Andric // Attempt to call the member with no arguments - this will correctly handle 23390b57cec5SDimitry Andric // member templates with defaults/deduction of template arguments, overloads 23400b57cec5SDimitry Andric // with default arguments, etc. 23410b57cec5SDimitry Andric if (IsMemExpr && !E.isTypeDependent()) { 2342a7dea167SDimitry Andric Sema::TentativeAnalysisScope Trap(*this); 23430b57cec5SDimitry Andric ExprResult R = BuildCallToMemberFunction(nullptr, &E, SourceLocation(), 23440b57cec5SDimitry Andric None, SourceLocation()); 23450b57cec5SDimitry Andric if (R.isUsable()) { 23460b57cec5SDimitry Andric ZeroArgCallReturnTy = R.get()->getType(); 23470b57cec5SDimitry Andric return true; 23480b57cec5SDimitry Andric } 23490b57cec5SDimitry Andric return false; 23500b57cec5SDimitry Andric } 23510b57cec5SDimitry Andric 23520b57cec5SDimitry Andric if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) { 23530b57cec5SDimitry Andric if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { 23540b57cec5SDimitry Andric if (Fun->getMinRequiredArguments() == 0) 23550b57cec5SDimitry Andric ZeroArgCallReturnTy = Fun->getReturnType(); 23560b57cec5SDimitry Andric return true; 23570b57cec5SDimitry Andric } 23580b57cec5SDimitry Andric } 23590b57cec5SDimitry Andric 23600b57cec5SDimitry Andric // We don't have an expression that's convenient to get a FunctionDecl from, 23610b57cec5SDimitry Andric // but we can at least check if the type is "function of 0 arguments". 23620b57cec5SDimitry Andric QualType ExprTy = E.getType(); 23630b57cec5SDimitry Andric const FunctionType *FunTy = nullptr; 23640b57cec5SDimitry Andric QualType PointeeTy = ExprTy->getPointeeType(); 23650b57cec5SDimitry Andric if (!PointeeTy.isNull()) 23660b57cec5SDimitry Andric FunTy = PointeeTy->getAs<FunctionType>(); 23670b57cec5SDimitry Andric if (!FunTy) 23680b57cec5SDimitry Andric FunTy = ExprTy->getAs<FunctionType>(); 23690b57cec5SDimitry Andric 23700b57cec5SDimitry Andric if (const FunctionProtoType *FPT = 23710b57cec5SDimitry Andric dyn_cast_or_null<FunctionProtoType>(FunTy)) { 23720b57cec5SDimitry Andric if (FPT->getNumParams() == 0) 23730b57cec5SDimitry Andric ZeroArgCallReturnTy = FunTy->getReturnType(); 23740b57cec5SDimitry Andric return true; 23750b57cec5SDimitry Andric } 23760b57cec5SDimitry Andric return false; 23770b57cec5SDimitry Andric } 23780b57cec5SDimitry Andric 23790b57cec5SDimitry Andric /// Give notes for a set of overloads. 23800b57cec5SDimitry Andric /// 23810b57cec5SDimitry Andric /// A companion to tryExprAsCall. In cases when the name that the programmer 23820b57cec5SDimitry Andric /// wrote was an overloaded function, we may be able to make some guesses about 23830b57cec5SDimitry Andric /// plausible overloads based on their return types; such guesses can be handed 23840b57cec5SDimitry Andric /// off to this method to be emitted as notes. 23850b57cec5SDimitry Andric /// 23860b57cec5SDimitry Andric /// \param Overloads - The overloads to note. 23870b57cec5SDimitry Andric /// \param FinalNoteLoc - If we've suppressed printing some overloads due to 23880b57cec5SDimitry Andric /// -fshow-overloads=best, this is the location to attach to the note about too 23890b57cec5SDimitry Andric /// many candidates. Typically this will be the location of the original 23900b57cec5SDimitry Andric /// ill-formed expression. 23910b57cec5SDimitry Andric static void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads, 23920b57cec5SDimitry Andric const SourceLocation FinalNoteLoc) { 2393fe6060f1SDimitry Andric unsigned ShownOverloads = 0; 2394fe6060f1SDimitry Andric unsigned SuppressedOverloads = 0; 23950b57cec5SDimitry Andric for (UnresolvedSetImpl::iterator It = Overloads.begin(), 23960b57cec5SDimitry Andric DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { 2397fe6060f1SDimitry Andric if (ShownOverloads >= S.Diags.getNumOverloadCandidatesToShow()) { 23980b57cec5SDimitry Andric ++SuppressedOverloads; 23990b57cec5SDimitry Andric continue; 24000b57cec5SDimitry Andric } 24010b57cec5SDimitry Andric 24020b57cec5SDimitry Andric NamedDecl *Fn = (*It)->getUnderlyingDecl(); 24030b57cec5SDimitry Andric // Don't print overloads for non-default multiversioned functions. 24040b57cec5SDimitry Andric if (const auto *FD = Fn->getAsFunction()) { 24050b57cec5SDimitry Andric if (FD->isMultiVersion() && FD->hasAttr<TargetAttr>() && 24060b57cec5SDimitry Andric !FD->getAttr<TargetAttr>()->isDefaultVersion()) 24070b57cec5SDimitry Andric continue; 24080b57cec5SDimitry Andric } 24090b57cec5SDimitry Andric S.Diag(Fn->getLocation(), diag::note_possible_target_of_call); 24100b57cec5SDimitry Andric ++ShownOverloads; 24110b57cec5SDimitry Andric } 24120b57cec5SDimitry Andric 2413fe6060f1SDimitry Andric S.Diags.overloadCandidatesShown(ShownOverloads); 2414fe6060f1SDimitry Andric 24150b57cec5SDimitry Andric if (SuppressedOverloads) 24160b57cec5SDimitry Andric S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates) 24170b57cec5SDimitry Andric << SuppressedOverloads; 24180b57cec5SDimitry Andric } 24190b57cec5SDimitry Andric 24200b57cec5SDimitry Andric static void notePlausibleOverloads(Sema &S, SourceLocation Loc, 24210b57cec5SDimitry Andric const UnresolvedSetImpl &Overloads, 24220b57cec5SDimitry Andric bool (*IsPlausibleResult)(QualType)) { 24230b57cec5SDimitry Andric if (!IsPlausibleResult) 24240b57cec5SDimitry Andric return noteOverloads(S, Overloads, Loc); 24250b57cec5SDimitry Andric 24260b57cec5SDimitry Andric UnresolvedSet<2> PlausibleOverloads; 24270b57cec5SDimitry Andric for (OverloadExpr::decls_iterator It = Overloads.begin(), 24280b57cec5SDimitry Andric DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { 24290b57cec5SDimitry Andric const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It); 24300b57cec5SDimitry Andric QualType OverloadResultTy = OverloadDecl->getReturnType(); 24310b57cec5SDimitry Andric if (IsPlausibleResult(OverloadResultTy)) 24320b57cec5SDimitry Andric PlausibleOverloads.addDecl(It.getDecl()); 24330b57cec5SDimitry Andric } 24340b57cec5SDimitry Andric noteOverloads(S, PlausibleOverloads, Loc); 24350b57cec5SDimitry Andric } 24360b57cec5SDimitry Andric 24370b57cec5SDimitry Andric /// Determine whether the given expression can be called by just 24380b57cec5SDimitry Andric /// putting parentheses after it. Notably, expressions with unary 24390b57cec5SDimitry Andric /// operators can't be because the unary operator will start parsing 24400b57cec5SDimitry Andric /// outside the call. 24410b57cec5SDimitry Andric static bool IsCallableWithAppend(Expr *E) { 24420b57cec5SDimitry Andric E = E->IgnoreImplicit(); 24430b57cec5SDimitry Andric return (!isa<CStyleCastExpr>(E) && 24440b57cec5SDimitry Andric !isa<UnaryOperator>(E) && 24450b57cec5SDimitry Andric !isa<BinaryOperator>(E) && 24460b57cec5SDimitry Andric !isa<CXXOperatorCallExpr>(E)); 24470b57cec5SDimitry Andric } 24480b57cec5SDimitry Andric 24490b57cec5SDimitry Andric static bool IsCPUDispatchCPUSpecificMultiVersion(const Expr *E) { 24500b57cec5SDimitry Andric if (const auto *UO = dyn_cast<UnaryOperator>(E)) 24510b57cec5SDimitry Andric E = UO->getSubExpr(); 24520b57cec5SDimitry Andric 24530b57cec5SDimitry Andric if (const auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { 24540b57cec5SDimitry Andric if (ULE->getNumDecls() == 0) 24550b57cec5SDimitry Andric return false; 24560b57cec5SDimitry Andric 24570b57cec5SDimitry Andric const NamedDecl *ND = *ULE->decls_begin(); 24580b57cec5SDimitry Andric if (const auto *FD = dyn_cast<FunctionDecl>(ND)) 24590b57cec5SDimitry Andric return FD->isCPUDispatchMultiVersion() || FD->isCPUSpecificMultiVersion(); 24600b57cec5SDimitry Andric } 24610b57cec5SDimitry Andric return false; 24620b57cec5SDimitry Andric } 24630b57cec5SDimitry Andric 24640b57cec5SDimitry Andric bool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD, 24650b57cec5SDimitry Andric bool ForceComplain, 24660b57cec5SDimitry Andric bool (*IsPlausibleResult)(QualType)) { 24670b57cec5SDimitry Andric SourceLocation Loc = E.get()->getExprLoc(); 24680b57cec5SDimitry Andric SourceRange Range = E.get()->getSourceRange(); 24690b57cec5SDimitry Andric 24700b57cec5SDimitry Andric QualType ZeroArgCallTy; 24710b57cec5SDimitry Andric UnresolvedSet<4> Overloads; 24720b57cec5SDimitry Andric if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) && 24730b57cec5SDimitry Andric !ZeroArgCallTy.isNull() && 24740b57cec5SDimitry Andric (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) { 24750b57cec5SDimitry Andric // At this point, we know E is potentially callable with 0 24760b57cec5SDimitry Andric // arguments and that it returns something of a reasonable type, 24770b57cec5SDimitry Andric // so we can emit a fixit and carry on pretending that E was 24780b57cec5SDimitry Andric // actually a CallExpr. 24790b57cec5SDimitry Andric SourceLocation ParenInsertionLoc = getLocForEndOfToken(Range.getEnd()); 24800b57cec5SDimitry Andric bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get()); 24810b57cec5SDimitry Andric Diag(Loc, PD) << /*zero-arg*/ 1 << IsMV << Range 24820b57cec5SDimitry Andric << (IsCallableWithAppend(E.get()) 24830b57cec5SDimitry Andric ? FixItHint::CreateInsertion(ParenInsertionLoc, "()") 24840b57cec5SDimitry Andric : FixItHint()); 24850b57cec5SDimitry Andric if (!IsMV) 24860b57cec5SDimitry Andric notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult); 24870b57cec5SDimitry Andric 24880b57cec5SDimitry Andric // FIXME: Try this before emitting the fixit, and suppress diagnostics 24890b57cec5SDimitry Andric // while doing so. 24900b57cec5SDimitry Andric E = BuildCallExpr(nullptr, E.get(), Range.getEnd(), None, 24910b57cec5SDimitry Andric Range.getEnd().getLocWithOffset(1)); 24920b57cec5SDimitry Andric return true; 24930b57cec5SDimitry Andric } 24940b57cec5SDimitry Andric 24950b57cec5SDimitry Andric if (!ForceComplain) return false; 24960b57cec5SDimitry Andric 24970b57cec5SDimitry Andric bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get()); 24980b57cec5SDimitry Andric Diag(Loc, PD) << /*not zero-arg*/ 0 << IsMV << Range; 24990b57cec5SDimitry Andric if (!IsMV) 25000b57cec5SDimitry Andric notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult); 25010b57cec5SDimitry Andric E = ExprError(); 25020b57cec5SDimitry Andric return true; 25030b57cec5SDimitry Andric } 25040b57cec5SDimitry Andric 25050b57cec5SDimitry Andric IdentifierInfo *Sema::getSuperIdentifier() const { 25060b57cec5SDimitry Andric if (!Ident_super) 25070b57cec5SDimitry Andric Ident_super = &Context.Idents.get("super"); 25080b57cec5SDimitry Andric return Ident_super; 25090b57cec5SDimitry Andric } 25100b57cec5SDimitry Andric 25110b57cec5SDimitry Andric IdentifierInfo *Sema::getFloat128Identifier() const { 25120b57cec5SDimitry Andric if (!Ident___float128) 25130b57cec5SDimitry Andric Ident___float128 = &Context.Idents.get("__float128"); 25140b57cec5SDimitry Andric return Ident___float128; 25150b57cec5SDimitry Andric } 25160b57cec5SDimitry Andric 25170b57cec5SDimitry Andric void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD, 2518a7dea167SDimitry Andric CapturedRegionKind K, 2519a7dea167SDimitry Andric unsigned OpenMPCaptureLevel) { 2520a7dea167SDimitry Andric auto *CSI = new CapturedRegionScopeInfo( 25210b57cec5SDimitry Andric getDiagnostics(), S, CD, RD, CD->getContextParam(), K, 2522a7dea167SDimitry Andric (getLangOpts().OpenMP && K == CR_OpenMP) ? getOpenMPNestingLevel() : 0, 2523a7dea167SDimitry Andric OpenMPCaptureLevel); 25240b57cec5SDimitry Andric CSI->ReturnType = Context.VoidTy; 25250b57cec5SDimitry Andric FunctionScopes.push_back(CSI); 25260b57cec5SDimitry Andric } 25270b57cec5SDimitry Andric 25280b57cec5SDimitry Andric CapturedRegionScopeInfo *Sema::getCurCapturedRegion() { 25290b57cec5SDimitry Andric if (FunctionScopes.empty()) 25300b57cec5SDimitry Andric return nullptr; 25310b57cec5SDimitry Andric 25320b57cec5SDimitry Andric return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back()); 25330b57cec5SDimitry Andric } 25340b57cec5SDimitry Andric 25350b57cec5SDimitry Andric const llvm::MapVector<FieldDecl *, Sema::DeleteLocs> & 25360b57cec5SDimitry Andric Sema::getMismatchingDeleteExpressions() const { 25370b57cec5SDimitry Andric return DeleteExprs; 25380b57cec5SDimitry Andric } 2539