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" 170b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h" 180b57cec5SDimitry Andric #include "clang/AST/DeclFriend.h" 190b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h" 200b57cec5SDimitry Andric #include "clang/AST/Expr.h" 210b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 220b57cec5SDimitry Andric #include "clang/AST/PrettyDeclStackTrace.h" 230b57cec5SDimitry Andric #include "clang/AST/StmtCXX.h" 240b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h" 250b57cec5SDimitry Andric #include "clang/Basic/PartialDiagnostic.h" 265ffd83dbSDimitry Andric #include "clang/Basic/SourceManager.h" 27a7dea167SDimitry Andric #include "clang/Basic/Stack.h" 280b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h" 290b57cec5SDimitry Andric #include "clang/Lex/HeaderSearch.h" 300b57cec5SDimitry Andric #include "clang/Lex/Preprocessor.h" 310b57cec5SDimitry Andric #include "clang/Sema/CXXFieldCollector.h" 320b57cec5SDimitry Andric #include "clang/Sema/DelayedDiagnostic.h" 330b57cec5SDimitry Andric #include "clang/Sema/ExternalSemaSource.h" 340b57cec5SDimitry Andric #include "clang/Sema/Initialization.h" 350b57cec5SDimitry Andric #include "clang/Sema/MultiplexExternalSemaSource.h" 360b57cec5SDimitry Andric #include "clang/Sema/ObjCMethodList.h" 370b57cec5SDimitry Andric #include "clang/Sema/Scope.h" 380b57cec5SDimitry Andric #include "clang/Sema/ScopeInfo.h" 390b57cec5SDimitry Andric #include "clang/Sema/SemaConsumer.h" 400b57cec5SDimitry Andric #include "clang/Sema/SemaInternal.h" 410b57cec5SDimitry Andric #include "clang/Sema/TemplateDeduction.h" 420b57cec5SDimitry Andric #include "clang/Sema/TemplateInstCallback.h" 43a7dea167SDimitry Andric #include "clang/Sema/TypoCorrection.h" 440b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 45*e8d8bef9SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 460b57cec5SDimitry Andric #include "llvm/Support/TimeProfiler.h" 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric using namespace clang; 490b57cec5SDimitry Andric using namespace sema; 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) { 520b57cec5SDimitry Andric return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts); 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); } 560b57cec5SDimitry Andric 5755e4f9d5SDimitry Andric IdentifierInfo * 5855e4f9d5SDimitry Andric Sema::InventAbbreviatedTemplateParameterTypeName(IdentifierInfo *ParamName, 5955e4f9d5SDimitry Andric unsigned int Index) { 6055e4f9d5SDimitry Andric std::string InventedName; 6155e4f9d5SDimitry Andric llvm::raw_string_ostream OS(InventedName); 6255e4f9d5SDimitry Andric 6355e4f9d5SDimitry Andric if (!ParamName) 6455e4f9d5SDimitry Andric OS << "auto:" << Index + 1; 6555e4f9d5SDimitry Andric else 6655e4f9d5SDimitry Andric OS << ParamName->getName() << ":auto"; 6755e4f9d5SDimitry Andric 6855e4f9d5SDimitry Andric OS.flush(); 6955e4f9d5SDimitry Andric return &Context.Idents.get(OS.str()); 7055e4f9d5SDimitry Andric } 7155e4f9d5SDimitry Andric 720b57cec5SDimitry Andric PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context, 730b57cec5SDimitry Andric const Preprocessor &PP) { 740b57cec5SDimitry Andric PrintingPolicy Policy = Context.getPrintingPolicy(); 750b57cec5SDimitry Andric // In diagnostics, we print _Bool as bool if the latter is defined as the 760b57cec5SDimitry Andric // former. 770b57cec5SDimitry Andric Policy.Bool = Context.getLangOpts().Bool; 780b57cec5SDimitry Andric if (!Policy.Bool) { 790b57cec5SDimitry Andric if (const MacroInfo *BoolMacro = PP.getMacroInfo(Context.getBoolName())) { 800b57cec5SDimitry Andric Policy.Bool = BoolMacro->isObjectLike() && 810b57cec5SDimitry Andric BoolMacro->getNumTokens() == 1 && 820b57cec5SDimitry Andric BoolMacro->getReplacementToken(0).is(tok::kw__Bool); 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric return Policy; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric void Sema::ActOnTranslationUnitScope(Scope *S) { 900b57cec5SDimitry Andric TUScope = S; 910b57cec5SDimitry Andric PushDeclContext(S, Context.getTranslationUnitDecl()); 920b57cec5SDimitry Andric } 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric namespace clang { 950b57cec5SDimitry Andric namespace sema { 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric class SemaPPCallbacks : public PPCallbacks { 980b57cec5SDimitry Andric Sema *S = nullptr; 990b57cec5SDimitry Andric llvm::SmallVector<SourceLocation, 8> IncludeStack; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric public: 1020b57cec5SDimitry Andric void set(Sema &S) { this->S = &S; } 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric void reset() { S = nullptr; } 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 1070b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType, 1080b57cec5SDimitry Andric FileID PrevFID) override { 1090b57cec5SDimitry Andric if (!S) 1100b57cec5SDimitry Andric return; 1110b57cec5SDimitry Andric switch (Reason) { 1120b57cec5SDimitry Andric case EnterFile: { 1130b57cec5SDimitry Andric SourceManager &SM = S->getSourceManager(); 1140b57cec5SDimitry Andric SourceLocation IncludeLoc = SM.getIncludeLoc(SM.getFileID(Loc)); 1150b57cec5SDimitry Andric if (IncludeLoc.isValid()) { 1160b57cec5SDimitry Andric if (llvm::timeTraceProfilerEnabled()) { 1170b57cec5SDimitry Andric const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc)); 1180b57cec5SDimitry Andric llvm::timeTraceProfilerBegin( 1190b57cec5SDimitry Andric "Source", FE != nullptr ? FE->getName() : StringRef("<unknown>")); 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric IncludeStack.push_back(IncludeLoc); 123*e8d8bef9SDimitry Andric S->DiagnoseNonDefaultPragmaAlignPack( 124*e8d8bef9SDimitry Andric Sema::PragmaAlignPackDiagnoseKind::NonDefaultStateAtInclude, 125*e8d8bef9SDimitry Andric IncludeLoc); 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric break; 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric case ExitFile: 1300b57cec5SDimitry Andric if (!IncludeStack.empty()) { 1310b57cec5SDimitry Andric if (llvm::timeTraceProfilerEnabled()) 1320b57cec5SDimitry Andric llvm::timeTraceProfilerEnd(); 1330b57cec5SDimitry Andric 134*e8d8bef9SDimitry Andric S->DiagnoseNonDefaultPragmaAlignPack( 135*e8d8bef9SDimitry Andric Sema::PragmaAlignPackDiagnoseKind::ChangedStateAtExit, 1360b57cec5SDimitry Andric IncludeStack.pop_back_val()); 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric break; 1390b57cec5SDimitry Andric default: 1400b57cec5SDimitry Andric break; 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric }; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric } // end namespace sema 1460b57cec5SDimitry Andric } // end namespace clang 1470b57cec5SDimitry Andric 1485ffd83dbSDimitry Andric const unsigned Sema::MaxAlignmentExponent; 1495ffd83dbSDimitry Andric const unsigned Sema::MaximumAlignment; 1505ffd83dbSDimitry Andric 1510b57cec5SDimitry Andric Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, 1520b57cec5SDimitry Andric TranslationUnitKind TUKind, CodeCompleteConsumer *CodeCompleter) 1530b57cec5SDimitry Andric : ExternalSource(nullptr), isMultiplexExternalSource(false), 1545ffd83dbSDimitry Andric CurFPFeatures(pp.getLangOpts()), LangOpts(pp.getLangOpts()), PP(pp), 1550b57cec5SDimitry Andric Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()), 1560b57cec5SDimitry Andric SourceMgr(PP.getSourceManager()), CollectStats(false), 1570b57cec5SDimitry Andric CodeCompleter(CodeCompleter), CurContext(nullptr), 1580b57cec5SDimitry Andric OriginalLexicalContext(nullptr), MSStructPragmaOn(false), 1590b57cec5SDimitry Andric MSPointerToMemberRepresentationMethod( 1600b57cec5SDimitry Andric LangOpts.getMSPointerToMemberRepresentationMethod()), 161*e8d8bef9SDimitry Andric VtorDispStack(LangOpts.getVtorDispMode()), 162*e8d8bef9SDimitry Andric AlignPackStack(AlignPackInfo(getLangOpts().XLPragmaPack)), 1630b57cec5SDimitry Andric DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr), 164*e8d8bef9SDimitry Andric CodeSegStack(nullptr), FpPragmaStack(FPOptionsOverride()), 165*e8d8bef9SDimitry Andric CurInitSeg(nullptr), VisContext(nullptr), 166*e8d8bef9SDimitry Andric PragmaAttributeCurrentTargetDecl(nullptr), 1670b57cec5SDimitry Andric IsBuildingRecoveryCallExpr(false), Cleanup{}, LateTemplateParser(nullptr), 1680b57cec5SDimitry Andric LateTemplateParserCleanup(nullptr), OpaqueParser(nullptr), IdResolver(pp), 1690b57cec5SDimitry Andric StdExperimentalNamespaceCache(nullptr), StdInitializerList(nullptr), 1700b57cec5SDimitry Andric StdCoroutineTraitsCache(nullptr), CXXTypeInfoDecl(nullptr), 1710b57cec5SDimitry Andric MSVCGuidDecl(nullptr), NSNumberDecl(nullptr), NSValueDecl(nullptr), 1720b57cec5SDimitry Andric NSStringDecl(nullptr), StringWithUTF8StringMethod(nullptr), 1730b57cec5SDimitry Andric ValueWithBytesObjCTypeMethod(nullptr), NSArrayDecl(nullptr), 1740b57cec5SDimitry Andric ArrayWithObjectsMethod(nullptr), NSDictionaryDecl(nullptr), 1750b57cec5SDimitry Andric DictionaryWithObjectsMethod(nullptr), GlobalNewDeleteDeclared(false), 1760b57cec5SDimitry Andric TUKind(TUKind), NumSFINAEErrors(0), 1770b57cec5SDimitry Andric FullyCheckedComparisonCategories( 1780b57cec5SDimitry Andric static_cast<unsigned>(ComparisonCategoryType::Last) + 1), 17955e4f9d5SDimitry Andric SatisfactionCache(Context), AccessCheckingSFINAE(false), 18055e4f9d5SDimitry Andric InNonInstantiationSFINAEContext(false), NonInstantiationEntries(0), 18155e4f9d5SDimitry Andric ArgumentPackSubstitutionIndex(-1), CurrentInstantiationScope(nullptr), 18255e4f9d5SDimitry Andric DisableTypoCorrection(false), TyposCorrected(0), AnalysisWarnings(*this), 1830b57cec5SDimitry Andric ThreadSafetyDeclCache(nullptr), VarDataSharingAttributesStack(nullptr), 1840b57cec5SDimitry Andric CurScope(nullptr), Ident_super(nullptr), Ident___float128(nullptr) { 1850b57cec5SDimitry Andric TUScope = nullptr; 1860b57cec5SDimitry Andric isConstantEvaluatedOverride = false; 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric LoadedExternalKnownNamespaces = false; 1890b57cec5SDimitry Andric for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I) 1900b57cec5SDimitry Andric NSNumberLiteralMethods[I] = nullptr; 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric if (getLangOpts().ObjC) 1930b57cec5SDimitry Andric NSAPIObj.reset(new NSAPI(Context)); 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric if (getLangOpts().CPlusPlus) 1960b57cec5SDimitry Andric FieldCollector.reset(new CXXFieldCollector()); 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric // Tell diagnostics how to render things from the AST library. 1990b57cec5SDimitry Andric Diags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument, &Context); 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric ExprEvalContexts.emplace_back( 2020b57cec5SDimitry Andric ExpressionEvaluationContext::PotentiallyEvaluated, 0, CleanupInfo{}, 2030b57cec5SDimitry Andric nullptr, ExpressionEvaluationContextRecord::EK_Other); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // Initialization of data sharing attributes stack for OpenMP 2060b57cec5SDimitry Andric InitDataSharingAttributesStack(); 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric std::unique_ptr<sema::SemaPPCallbacks> Callbacks = 209a7dea167SDimitry Andric std::make_unique<sema::SemaPPCallbacks>(); 2100b57cec5SDimitry Andric SemaPPCallbackHandler = Callbacks.get(); 2110b57cec5SDimitry Andric PP.addPPCallbacks(std::move(Callbacks)); 2120b57cec5SDimitry Andric SemaPPCallbackHandler->set(*this); 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric 215480093f4SDimitry Andric // Anchor Sema's type info to this TU. 216480093f4SDimitry Andric void Sema::anchor() {} 217480093f4SDimitry Andric 2180b57cec5SDimitry Andric void Sema::addImplicitTypedef(StringRef Name, QualType T) { 2190b57cec5SDimitry Andric DeclarationName DN = &Context.Idents.get(Name); 2200b57cec5SDimitry Andric if (IdResolver.begin(DN) == IdResolver.end()) 2210b57cec5SDimitry Andric PushOnScopeChains(Context.buildImplicitTypedef(T, Name), TUScope); 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric void Sema::Initialize() { 2250b57cec5SDimitry Andric if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) 2260b57cec5SDimitry Andric SC->InitializeSema(*this); 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric // Tell the external Sema source about this Sema object. 2290b57cec5SDimitry Andric if (ExternalSemaSource *ExternalSema 2300b57cec5SDimitry Andric = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) 2310b57cec5SDimitry Andric ExternalSema->InitializeSema(*this); 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric // This needs to happen after ExternalSemaSource::InitializeSema(this) or we 2340b57cec5SDimitry Andric // will not be able to merge any duplicate __va_list_tag decls correctly. 2350b57cec5SDimitry Andric VAListTagName = PP.getIdentifierInfo("__va_list_tag"); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric if (!TUScope) 2380b57cec5SDimitry Andric return; 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric // Initialize predefined 128-bit integer types, if needed. 241*e8d8bef9SDimitry Andric if (Context.getTargetInfo().hasInt128Type() || 242*e8d8bef9SDimitry Andric (Context.getAuxTargetInfo() && 243*e8d8bef9SDimitry Andric Context.getAuxTargetInfo()->hasInt128Type())) { 2440b57cec5SDimitry Andric // If either of the 128-bit integer types are unavailable to name lookup, 2450b57cec5SDimitry Andric // define them now. 2460b57cec5SDimitry Andric DeclarationName Int128 = &Context.Idents.get("__int128_t"); 2470b57cec5SDimitry Andric if (IdResolver.begin(Int128) == IdResolver.end()) 2480b57cec5SDimitry Andric PushOnScopeChains(Context.getInt128Decl(), TUScope); 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric DeclarationName UInt128 = &Context.Idents.get("__uint128_t"); 2510b57cec5SDimitry Andric if (IdResolver.begin(UInt128) == IdResolver.end()) 2520b57cec5SDimitry Andric PushOnScopeChains(Context.getUInt128Decl(), TUScope); 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric // Initialize predefined Objective-C types: 2570b57cec5SDimitry Andric if (getLangOpts().ObjC) { 2580b57cec5SDimitry Andric // If 'SEL' does not yet refer to any declarations, make it refer to the 2590b57cec5SDimitry Andric // predefined 'SEL'. 2600b57cec5SDimitry Andric DeclarationName SEL = &Context.Idents.get("SEL"); 2610b57cec5SDimitry Andric if (IdResolver.begin(SEL) == IdResolver.end()) 2620b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCSelDecl(), TUScope); 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric // If 'id' does not yet refer to any declarations, make it refer to the 2650b57cec5SDimitry Andric // predefined 'id'. 2660b57cec5SDimitry Andric DeclarationName Id = &Context.Idents.get("id"); 2670b57cec5SDimitry Andric if (IdResolver.begin(Id) == IdResolver.end()) 2680b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCIdDecl(), TUScope); 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric // Create the built-in typedef for 'Class'. 2710b57cec5SDimitry Andric DeclarationName Class = &Context.Idents.get("Class"); 2720b57cec5SDimitry Andric if (IdResolver.begin(Class) == IdResolver.end()) 2730b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCClassDecl(), TUScope); 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // Create the built-in forward declaratino for 'Protocol'. 2760b57cec5SDimitry Andric DeclarationName Protocol = &Context.Idents.get("Protocol"); 2770b57cec5SDimitry Andric if (IdResolver.begin(Protocol) == IdResolver.end()) 2780b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope); 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric // Create the internal type for the *StringMakeConstantString builtins. 2820b57cec5SDimitry Andric DeclarationName ConstantString = &Context.Idents.get("__NSConstantString"); 2830b57cec5SDimitry Andric if (IdResolver.begin(ConstantString) == IdResolver.end()) 2840b57cec5SDimitry Andric PushOnScopeChains(Context.getCFConstantStringDecl(), TUScope); 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric // Initialize Microsoft "predefined C++ types". 2870b57cec5SDimitry Andric if (getLangOpts().MSVCCompat) { 2880b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && 2890b57cec5SDimitry Andric IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end()) 2900b57cec5SDimitry Andric PushOnScopeChains(Context.buildImplicitRecord("type_info", TTK_Class), 2910b57cec5SDimitry Andric TUScope); 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric addImplicitTypedef("size_t", Context.getSizeType()); 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // Initialize predefined OpenCL types and supported extensions and (optional) 2970b57cec5SDimitry Andric // core features. 2980b57cec5SDimitry Andric if (getLangOpts().OpenCL) { 2990b57cec5SDimitry Andric getOpenCLOptions().addSupport( 300*e8d8bef9SDimitry Andric Context.getTargetInfo().getSupportedOpenCLOpts(), getLangOpts()); 3010b57cec5SDimitry Andric getOpenCLOptions().enableSupportedCore(getLangOpts()); 3020b57cec5SDimitry Andric addImplicitTypedef("sampler_t", Context.OCLSamplerTy); 3030b57cec5SDimitry Andric addImplicitTypedef("event_t", Context.OCLEventTy); 3040b57cec5SDimitry Andric if (getLangOpts().OpenCLCPlusPlus || getLangOpts().OpenCLVersion >= 200) { 3050b57cec5SDimitry Andric addImplicitTypedef("clk_event_t", Context.OCLClkEventTy); 3060b57cec5SDimitry Andric addImplicitTypedef("queue_t", Context.OCLQueueTy); 3070b57cec5SDimitry Andric addImplicitTypedef("reserve_id_t", Context.OCLReserveIDTy); 3080b57cec5SDimitry Andric addImplicitTypedef("atomic_int", Context.getAtomicType(Context.IntTy)); 3090b57cec5SDimitry Andric addImplicitTypedef("atomic_uint", 3100b57cec5SDimitry Andric Context.getAtomicType(Context.UnsignedIntTy)); 3110b57cec5SDimitry Andric auto AtomicLongT = Context.getAtomicType(Context.LongTy); 3120b57cec5SDimitry Andric addImplicitTypedef("atomic_long", AtomicLongT); 3130b57cec5SDimitry Andric auto AtomicULongT = Context.getAtomicType(Context.UnsignedLongTy); 3140b57cec5SDimitry Andric addImplicitTypedef("atomic_ulong", AtomicULongT); 3150b57cec5SDimitry Andric addImplicitTypedef("atomic_float", 3160b57cec5SDimitry Andric Context.getAtomicType(Context.FloatTy)); 3170b57cec5SDimitry Andric auto AtomicDoubleT = Context.getAtomicType(Context.DoubleTy); 3180b57cec5SDimitry Andric addImplicitTypedef("atomic_double", AtomicDoubleT); 3190b57cec5SDimitry Andric // OpenCLC v2.0, s6.13.11.6 requires that atomic_flag is implemented as 3200b57cec5SDimitry Andric // 32-bit integer and OpenCLC v2.0, s6.1.1 int is always 32-bit wide. 3210b57cec5SDimitry Andric addImplicitTypedef("atomic_flag", Context.getAtomicType(Context.IntTy)); 3220b57cec5SDimitry Andric auto AtomicIntPtrT = Context.getAtomicType(Context.getIntPtrType()); 3230b57cec5SDimitry Andric addImplicitTypedef("atomic_intptr_t", AtomicIntPtrT); 3240b57cec5SDimitry Andric auto AtomicUIntPtrT = Context.getAtomicType(Context.getUIntPtrType()); 3250b57cec5SDimitry Andric addImplicitTypedef("atomic_uintptr_t", AtomicUIntPtrT); 3260b57cec5SDimitry Andric auto AtomicSizeT = Context.getAtomicType(Context.getSizeType()); 3270b57cec5SDimitry Andric addImplicitTypedef("atomic_size_t", AtomicSizeT); 3280b57cec5SDimitry Andric auto AtomicPtrDiffT = Context.getAtomicType(Context.getPointerDiffType()); 3290b57cec5SDimitry Andric addImplicitTypedef("atomic_ptrdiff_t", AtomicPtrDiffT); 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric // OpenCL v2.0 s6.13.11.6: 3320b57cec5SDimitry Andric // - The atomic_long and atomic_ulong types are supported if the 3330b57cec5SDimitry Andric // cl_khr_int64_base_atomics and cl_khr_int64_extended_atomics 3340b57cec5SDimitry Andric // extensions are supported. 3350b57cec5SDimitry Andric // - The atomic_double type is only supported if double precision 3360b57cec5SDimitry Andric // is supported and the cl_khr_int64_base_atomics and 3370b57cec5SDimitry Andric // cl_khr_int64_extended_atomics extensions are supported. 3380b57cec5SDimitry Andric // - If the device address space is 64-bits, the data types 3390b57cec5SDimitry Andric // atomic_intptr_t, atomic_uintptr_t, atomic_size_t and 3400b57cec5SDimitry Andric // atomic_ptrdiff_t are supported if the cl_khr_int64_base_atomics and 3410b57cec5SDimitry Andric // cl_khr_int64_extended_atomics extensions are supported. 3420b57cec5SDimitry Andric std::vector<QualType> Atomic64BitTypes; 3430b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicLongT); 3440b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicULongT); 3450b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicDoubleT); 3460b57cec5SDimitry Andric if (Context.getTypeSize(AtomicSizeT) == 64) { 3470b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicSizeT); 3480b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicIntPtrT); 3490b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicUIntPtrT); 3500b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicPtrDiffT); 3510b57cec5SDimitry Andric } 3520b57cec5SDimitry Andric for (auto &I : Atomic64BitTypes) 3530b57cec5SDimitry Andric setOpenCLExtensionForType(I, 3540b57cec5SDimitry Andric "cl_khr_int64_base_atomics cl_khr_int64_extended_atomics"); 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric setOpenCLExtensionForType(AtomicDoubleT, "cl_khr_fp64"); 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric setOpenCLExtensionForType(Context.DoubleTy, "cl_khr_fp64"); 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE_EXT(Type, Id, Ext) \ 3620b57cec5SDimitry Andric setOpenCLExtensionForType(Context.Id, Ext); 3630b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def" 3640b57cec5SDimitry Andric #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 3650b57cec5SDimitry Andric addImplicitTypedef(#ExtType, Context.Id##Ty); \ 3660b57cec5SDimitry Andric setOpenCLExtensionForType(Context.Id##Ty, #Ext); 3670b57cec5SDimitry Andric #include "clang/Basic/OpenCLExtensionTypes.def" 368a7dea167SDimitry Andric } 369a7dea167SDimitry Andric 370a7dea167SDimitry Andric if (Context.getTargetInfo().hasAArch64SVETypes()) { 371a7dea167SDimitry Andric #define SVE_TYPE(Name, Id, SingletonId) \ 372a7dea167SDimitry Andric addImplicitTypedef(Name, Context.SingletonId); 373a7dea167SDimitry Andric #include "clang/Basic/AArch64SVEACLETypes.def" 374a7dea167SDimitry Andric } 3750b57cec5SDimitry Andric 376*e8d8bef9SDimitry Andric if (Context.getTargetInfo().getTriple().isPPC64() && 377*e8d8bef9SDimitry Andric Context.getTargetInfo().hasFeature("paired-vector-memops")) { 378*e8d8bef9SDimitry Andric if (Context.getTargetInfo().hasFeature("mma")) { 379*e8d8bef9SDimitry Andric #define PPC_VECTOR_MMA_TYPE(Name, Id, Size) \ 380*e8d8bef9SDimitry Andric addImplicitTypedef(#Name, Context.Id##Ty); 381*e8d8bef9SDimitry Andric #include "clang/Basic/PPCTypes.def" 382*e8d8bef9SDimitry Andric } 383*e8d8bef9SDimitry Andric #define PPC_VECTOR_VSX_TYPE(Name, Id, Size) \ 384*e8d8bef9SDimitry Andric addImplicitTypedef(#Name, Context.Id##Ty); 385*e8d8bef9SDimitry Andric #include "clang/Basic/PPCTypes.def" 386*e8d8bef9SDimitry Andric } 387*e8d8bef9SDimitry Andric 3880b57cec5SDimitry Andric if (Context.getTargetInfo().hasBuiltinMSVaList()) { 3890b57cec5SDimitry Andric DeclarationName MSVaList = &Context.Idents.get("__builtin_ms_va_list"); 3900b57cec5SDimitry Andric if (IdResolver.begin(MSVaList) == IdResolver.end()) 3910b57cec5SDimitry Andric PushOnScopeChains(Context.getBuiltinMSVaListDecl(), TUScope); 3920b57cec5SDimitry Andric } 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list"); 3950b57cec5SDimitry Andric if (IdResolver.begin(BuiltinVaList) == IdResolver.end()) 3960b57cec5SDimitry Andric PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope); 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric Sema::~Sema() { 400*e8d8bef9SDimitry Andric assert(InstantiatingSpecializations.empty() && 401*e8d8bef9SDimitry Andric "failed to clean up an InstantiatingTemplate?"); 402*e8d8bef9SDimitry Andric 4030b57cec5SDimitry Andric if (VisContext) FreeVisContext(); 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric // Kill all the active scopes. 4060b57cec5SDimitry Andric for (sema::FunctionScopeInfo *FSI : FunctionScopes) 4070b57cec5SDimitry Andric delete FSI; 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric // Tell the SemaConsumer to forget about us; we're going out of scope. 4100b57cec5SDimitry Andric if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) 4110b57cec5SDimitry Andric SC->ForgetSema(); 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric // Detach from the external Sema source. 4140b57cec5SDimitry Andric if (ExternalSemaSource *ExternalSema 4150b57cec5SDimitry Andric = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) 4160b57cec5SDimitry Andric ExternalSema->ForgetSema(); 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric // If Sema's ExternalSource is the multiplexer - we own it. 4190b57cec5SDimitry Andric if (isMultiplexExternalSource) 4200b57cec5SDimitry Andric delete ExternalSource; 4210b57cec5SDimitry Andric 42255e4f9d5SDimitry Andric // Delete cached satisfactions. 42355e4f9d5SDimitry Andric std::vector<ConstraintSatisfaction *> Satisfactions; 42455e4f9d5SDimitry Andric Satisfactions.reserve(Satisfactions.size()); 42555e4f9d5SDimitry Andric for (auto &Node : SatisfactionCache) 42655e4f9d5SDimitry Andric Satisfactions.push_back(&Node); 42755e4f9d5SDimitry Andric for (auto *Node : Satisfactions) 42855e4f9d5SDimitry Andric delete Node; 42955e4f9d5SDimitry Andric 4300b57cec5SDimitry Andric threadSafety::threadSafetyCleanup(ThreadSafetyDeclCache); 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric // Destroys data sharing attributes stack for OpenMP 4330b57cec5SDimitry Andric DestroyDataSharingAttributesStack(); 4340b57cec5SDimitry Andric 4350b57cec5SDimitry Andric // Detach from the PP callback handler which outlives Sema since it's owned 4360b57cec5SDimitry Andric // by the preprocessor. 4370b57cec5SDimitry Andric SemaPPCallbackHandler->reset(); 438a7dea167SDimitry Andric } 4390b57cec5SDimitry Andric 440a7dea167SDimitry Andric void Sema::warnStackExhausted(SourceLocation Loc) { 441a7dea167SDimitry Andric // Only warn about this once. 442a7dea167SDimitry Andric if (!WarnedStackExhausted) { 443a7dea167SDimitry Andric Diag(Loc, diag::warn_stack_exhausted); 444a7dea167SDimitry Andric WarnedStackExhausted = true; 445a7dea167SDimitry Andric } 446a7dea167SDimitry Andric } 447a7dea167SDimitry Andric 448a7dea167SDimitry Andric void Sema::runWithSufficientStackSpace(SourceLocation Loc, 449a7dea167SDimitry Andric llvm::function_ref<void()> Fn) { 450a7dea167SDimitry Andric clang::runWithSufficientStackSpace([&] { warnStackExhausted(Loc); }, Fn); 4510b57cec5SDimitry Andric } 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric /// makeUnavailableInSystemHeader - There is an error in the current 4540b57cec5SDimitry Andric /// context. If we're still in a system header, and we can plausibly 4550b57cec5SDimitry Andric /// make the relevant declaration unavailable instead of erroring, do 4560b57cec5SDimitry Andric /// so and return true. 4570b57cec5SDimitry Andric bool Sema::makeUnavailableInSystemHeader(SourceLocation loc, 4580b57cec5SDimitry Andric UnavailableAttr::ImplicitReason reason) { 4590b57cec5SDimitry Andric // If we're not in a function, it's an error. 4600b57cec5SDimitry Andric FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext); 4610b57cec5SDimitry Andric if (!fn) return false; 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric // If we're in template instantiation, it's an error. 4640b57cec5SDimitry Andric if (inTemplateInstantiation()) 4650b57cec5SDimitry Andric return false; 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric // If that function's not in a system header, it's an error. 4680b57cec5SDimitry Andric if (!Context.getSourceManager().isInSystemHeader(loc)) 4690b57cec5SDimitry Andric return false; 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // If the function is already unavailable, it's not an error. 4720b57cec5SDimitry Andric if (fn->hasAttr<UnavailableAttr>()) return true; 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric fn->addAttr(UnavailableAttr::CreateImplicit(Context, "", reason, loc)); 4750b57cec5SDimitry Andric return true; 4760b57cec5SDimitry Andric } 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric ASTMutationListener *Sema::getASTMutationListener() const { 4790b57cec5SDimitry Andric return getASTConsumer().GetASTMutationListener(); 4800b57cec5SDimitry Andric } 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric ///Registers an external source. If an external source already exists, 4830b57cec5SDimitry Andric /// creates a multiplex external source and appends to it. 4840b57cec5SDimitry Andric /// 4850b57cec5SDimitry Andric ///\param[in] E - A non-null external sema source. 4860b57cec5SDimitry Andric /// 4870b57cec5SDimitry Andric void Sema::addExternalSource(ExternalSemaSource *E) { 4880b57cec5SDimitry Andric assert(E && "Cannot use with NULL ptr"); 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric if (!ExternalSource) { 4910b57cec5SDimitry Andric ExternalSource = E; 4920b57cec5SDimitry Andric return; 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric if (isMultiplexExternalSource) 4960b57cec5SDimitry Andric static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E); 4970b57cec5SDimitry Andric else { 4980b57cec5SDimitry Andric ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E); 4990b57cec5SDimitry Andric isMultiplexExternalSource = true; 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric /// Print out statistics about the semantic analysis. 5040b57cec5SDimitry Andric void Sema::PrintStats() const { 5050b57cec5SDimitry Andric llvm::errs() << "\n*** Semantic Analysis Stats:\n"; 5060b57cec5SDimitry Andric llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n"; 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric BumpAlloc.PrintStats(); 5090b57cec5SDimitry Andric AnalysisWarnings.PrintStats(); 5100b57cec5SDimitry Andric } 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric void Sema::diagnoseNullableToNonnullConversion(QualType DstType, 5130b57cec5SDimitry Andric QualType SrcType, 5140b57cec5SDimitry Andric SourceLocation Loc) { 5150b57cec5SDimitry Andric Optional<NullabilityKind> ExprNullability = SrcType->getNullability(Context); 516*e8d8bef9SDimitry Andric if (!ExprNullability || (*ExprNullability != NullabilityKind::Nullable && 517*e8d8bef9SDimitry Andric *ExprNullability != NullabilityKind::NullableResult)) 5180b57cec5SDimitry Andric return; 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric Optional<NullabilityKind> TypeNullability = DstType->getNullability(Context); 5210b57cec5SDimitry Andric if (!TypeNullability || *TypeNullability != NullabilityKind::NonNull) 5220b57cec5SDimitry Andric return; 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric Diag(Loc, diag::warn_nullability_lost) << SrcType << DstType; 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) { 5280b57cec5SDimitry Andric if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant, 5290b57cec5SDimitry Andric E->getBeginLoc())) 5300b57cec5SDimitry Andric return; 5310b57cec5SDimitry Andric // nullptr only exists from C++11 on, so don't warn on its absence earlier. 5320b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus11) 5330b57cec5SDimitry Andric return; 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer) 5360b57cec5SDimitry Andric return; 5370b57cec5SDimitry Andric if (E->IgnoreParenImpCasts()->getType()->isNullPtrType()) 5380b57cec5SDimitry Andric return; 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric // If it is a macro from system header, and if the macro name is not "NULL", 5410b57cec5SDimitry Andric // do not warn. 5420b57cec5SDimitry Andric SourceLocation MaybeMacroLoc = E->getBeginLoc(); 5430b57cec5SDimitry Andric if (Diags.getSuppressSystemWarnings() && 5440b57cec5SDimitry Andric SourceMgr.isInSystemMacro(MaybeMacroLoc) && 5450b57cec5SDimitry Andric !findMacroSpelling(MaybeMacroLoc, "NULL")) 5460b57cec5SDimitry Andric return; 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric Diag(E->getBeginLoc(), diag::warn_zero_as_null_pointer_constant) 5490b57cec5SDimitry Andric << FixItHint::CreateReplacement(E->getSourceRange(), "nullptr"); 5500b57cec5SDimitry Andric } 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 5530b57cec5SDimitry Andric /// If there is already an implicit cast, merge into the existing one. 5540b57cec5SDimitry Andric /// The result is of the given category. 5550b57cec5SDimitry Andric ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty, 5560b57cec5SDimitry Andric CastKind Kind, ExprValueKind VK, 5570b57cec5SDimitry Andric const CXXCastPath *BasePath, 5580b57cec5SDimitry Andric CheckedConversionKind CCK) { 5590b57cec5SDimitry Andric #ifndef NDEBUG 5600b57cec5SDimitry Andric if (VK == VK_RValue && !E->isRValue()) { 5610b57cec5SDimitry Andric switch (Kind) { 5620b57cec5SDimitry Andric default: 563*e8d8bef9SDimitry Andric llvm_unreachable(("can't implicitly cast lvalue to rvalue with this cast " 564*e8d8bef9SDimitry Andric "kind: " + 565*e8d8bef9SDimitry Andric std::string(CastExpr::getCastKindName(Kind))) 566*e8d8bef9SDimitry Andric .c_str()); 5670b57cec5SDimitry Andric case CK_Dependent: 5680b57cec5SDimitry Andric case CK_LValueToRValue: 5690b57cec5SDimitry Andric case CK_ArrayToPointerDecay: 5700b57cec5SDimitry Andric case CK_FunctionToPointerDecay: 5710b57cec5SDimitry Andric case CK_ToVoid: 5720b57cec5SDimitry Andric case CK_NonAtomicToAtomic: 5730b57cec5SDimitry Andric break; 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric } 5760b57cec5SDimitry Andric assert((VK == VK_RValue || Kind == CK_Dependent || !E->isRValue()) && 5770b57cec5SDimitry Andric "can't cast rvalue to lvalue"); 5780b57cec5SDimitry Andric #endif 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andric diagnoseNullableToNonnullConversion(Ty, E->getType(), E->getBeginLoc()); 5810b57cec5SDimitry Andric diagnoseZeroToNullptrConversion(Kind, E); 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric QualType ExprTy = Context.getCanonicalType(E->getType()); 5840b57cec5SDimitry Andric QualType TypeTy = Context.getCanonicalType(Ty); 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric if (ExprTy == TypeTy) 5870b57cec5SDimitry Andric return E; 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric // C++1z [conv.array]: The temporary materialization conversion is applied. 5900b57cec5SDimitry Andric // We also use this to fuel C++ DR1213, which applies to C++11 onwards. 5910b57cec5SDimitry Andric if (Kind == CK_ArrayToPointerDecay && getLangOpts().CPlusPlus && 5920b57cec5SDimitry Andric E->getValueKind() == VK_RValue) { 5930b57cec5SDimitry Andric // The temporary is an lvalue in C++98 and an xvalue otherwise. 5940b57cec5SDimitry Andric ExprResult Materialized = CreateMaterializeTemporaryExpr( 5950b57cec5SDimitry Andric E->getType(), E, !getLangOpts().CPlusPlus11); 5960b57cec5SDimitry Andric if (Materialized.isInvalid()) 5970b57cec5SDimitry Andric return ExprError(); 5980b57cec5SDimitry Andric E = Materialized.get(); 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) { 6020b57cec5SDimitry Andric if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) { 6030b57cec5SDimitry Andric ImpCast->setType(Ty); 6040b57cec5SDimitry Andric ImpCast->setValueKind(VK); 6050b57cec5SDimitry Andric return E; 6060b57cec5SDimitry Andric } 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric 609*e8d8bef9SDimitry Andric return ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK, 610*e8d8bef9SDimitry Andric CurFPFeatureOverrides()); 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding 6140b57cec5SDimitry Andric /// to the conversion from scalar type ScalarTy to the Boolean type. 6150b57cec5SDimitry Andric CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) { 6160b57cec5SDimitry Andric switch (ScalarTy->getScalarTypeKind()) { 6170b57cec5SDimitry Andric case Type::STK_Bool: return CK_NoOp; 6180b57cec5SDimitry Andric case Type::STK_CPointer: return CK_PointerToBoolean; 6190b57cec5SDimitry Andric case Type::STK_BlockPointer: return CK_PointerToBoolean; 6200b57cec5SDimitry Andric case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean; 6210b57cec5SDimitry Andric case Type::STK_MemberPointer: return CK_MemberPointerToBoolean; 6220b57cec5SDimitry Andric case Type::STK_Integral: return CK_IntegralToBoolean; 6230b57cec5SDimitry Andric case Type::STK_Floating: return CK_FloatingToBoolean; 6240b57cec5SDimitry Andric case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean; 6250b57cec5SDimitry Andric case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean; 6260b57cec5SDimitry Andric case Type::STK_FixedPoint: return CK_FixedPointToBoolean; 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric llvm_unreachable("unknown scalar type kind"); 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric /// Used to prune the decls of Sema's UnusedFileScopedDecls vector. 6320b57cec5SDimitry Andric static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { 6330b57cec5SDimitry Andric if (D->getMostRecentDecl()->isUsed()) 6340b57cec5SDimitry Andric return true; 6350b57cec5SDimitry Andric 6360b57cec5SDimitry Andric if (D->isExternallyVisible()) 6370b57cec5SDimitry Andric return true; 6380b57cec5SDimitry Andric 6390b57cec5SDimitry Andric if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 6400b57cec5SDimitry Andric // If this is a function template and none of its specializations is used, 6410b57cec5SDimitry Andric // we should warn. 6420b57cec5SDimitry Andric if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate()) 6430b57cec5SDimitry Andric for (const auto *Spec : Template->specializations()) 6440b57cec5SDimitry Andric if (ShouldRemoveFromUnused(SemaRef, Spec)) 6450b57cec5SDimitry Andric return true; 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric // UnusedFileScopedDecls stores the first declaration. 6480b57cec5SDimitry Andric // The declaration may have become definition so check again. 6490b57cec5SDimitry Andric const FunctionDecl *DeclToCheck; 6500b57cec5SDimitry Andric if (FD->hasBody(DeclToCheck)) 6510b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric // Later redecls may add new information resulting in not having to warn, 6540b57cec5SDimitry Andric // so check again. 6550b57cec5SDimitry Andric DeclToCheck = FD->getMostRecentDecl(); 6560b57cec5SDimitry Andric if (DeclToCheck != FD) 6570b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 6610b57cec5SDimitry Andric // If a variable usable in constant expressions is referenced, 6620b57cec5SDimitry Andric // don't warn if it isn't used: if the value of a variable is required 6630b57cec5SDimitry Andric // for the computation of a constant expression, it doesn't make sense to 6640b57cec5SDimitry Andric // warn even if the variable isn't odr-used. (isReferenced doesn't 6650b57cec5SDimitry Andric // precisely reflect that, but it's a decent approximation.) 6660b57cec5SDimitry Andric if (VD->isReferenced() && 6670b57cec5SDimitry Andric VD->mightBeUsableInConstantExpressions(SemaRef->Context)) 6680b57cec5SDimitry Andric return true; 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric if (VarTemplateDecl *Template = VD->getDescribedVarTemplate()) 6710b57cec5SDimitry Andric // If this is a variable template and none of its specializations is used, 6720b57cec5SDimitry Andric // we should warn. 6730b57cec5SDimitry Andric for (const auto *Spec : Template->specializations()) 6740b57cec5SDimitry Andric if (ShouldRemoveFromUnused(SemaRef, Spec)) 6750b57cec5SDimitry Andric return true; 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric // UnusedFileScopedDecls stores the first declaration. 6780b57cec5SDimitry Andric // The declaration may have become definition so check again. 6790b57cec5SDimitry Andric const VarDecl *DeclToCheck = VD->getDefinition(); 6800b57cec5SDimitry Andric if (DeclToCheck) 6810b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric // Later redecls may add new information resulting in not having to warn, 6840b57cec5SDimitry Andric // so check again. 6850b57cec5SDimitry Andric DeclToCheck = VD->getMostRecentDecl(); 6860b57cec5SDimitry Andric if (DeclToCheck != VD) 6870b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 6880b57cec5SDimitry Andric } 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric return false; 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric static bool isFunctionOrVarDeclExternC(NamedDecl *ND) { 6940b57cec5SDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(ND)) 6950b57cec5SDimitry Andric return FD->isExternC(); 6960b57cec5SDimitry Andric return cast<VarDecl>(ND)->isExternC(); 6970b57cec5SDimitry Andric } 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric /// Determine whether ND is an external-linkage function or variable whose 7000b57cec5SDimitry Andric /// type has no linkage. 7010b57cec5SDimitry Andric bool Sema::isExternalWithNoLinkageType(ValueDecl *VD) { 7020b57cec5SDimitry Andric // Note: it's not quite enough to check whether VD has UniqueExternalLinkage, 7030b57cec5SDimitry Andric // because we also want to catch the case where its type has VisibleNoLinkage, 7040b57cec5SDimitry Andric // which does not affect the linkage of VD. 7050b57cec5SDimitry Andric return getLangOpts().CPlusPlus && VD->hasExternalFormalLinkage() && 7060b57cec5SDimitry Andric !isExternalFormalLinkage(VD->getType()->getLinkage()) && 7070b57cec5SDimitry Andric !isFunctionOrVarDeclExternC(VD); 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric /// Obtains a sorted list of functions and variables that are undefined but 7110b57cec5SDimitry Andric /// ODR-used. 7120b57cec5SDimitry Andric void Sema::getUndefinedButUsed( 7130b57cec5SDimitry Andric SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) { 7140b57cec5SDimitry Andric for (const auto &UndefinedUse : UndefinedButUsed) { 7150b57cec5SDimitry Andric NamedDecl *ND = UndefinedUse.first; 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric // Ignore attributes that have become invalid. 7180b57cec5SDimitry Andric if (ND->isInvalidDecl()) continue; 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric // __attribute__((weakref)) is basically a definition. 7210b57cec5SDimitry Andric if (ND->hasAttr<WeakRefAttr>()) continue; 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric if (isa<CXXDeductionGuideDecl>(ND)) 7240b57cec5SDimitry Andric continue; 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric if (ND->hasAttr<DLLImportAttr>() || ND->hasAttr<DLLExportAttr>()) { 7270b57cec5SDimitry Andric // An exported function will always be emitted when defined, so even if 7280b57cec5SDimitry Andric // the function is inline, it doesn't have to be emitted in this TU. An 7290b57cec5SDimitry Andric // imported function implies that it has been exported somewhere else. 7300b57cec5SDimitry Andric continue; 7310b57cec5SDimitry Andric } 7320b57cec5SDimitry Andric 7330b57cec5SDimitry Andric if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 7340b57cec5SDimitry Andric if (FD->isDefined()) 7350b57cec5SDimitry Andric continue; 7360b57cec5SDimitry Andric if (FD->isExternallyVisible() && 7370b57cec5SDimitry Andric !isExternalWithNoLinkageType(FD) && 7380b57cec5SDimitry Andric !FD->getMostRecentDecl()->isInlined() && 7390b57cec5SDimitry Andric !FD->hasAttr<ExcludeFromExplicitInstantiationAttr>()) 7400b57cec5SDimitry Andric continue; 7410b57cec5SDimitry Andric if (FD->getBuiltinID()) 7420b57cec5SDimitry Andric continue; 7430b57cec5SDimitry Andric } else { 7440b57cec5SDimitry Andric auto *VD = cast<VarDecl>(ND); 7450b57cec5SDimitry Andric if (VD->hasDefinition() != VarDecl::DeclarationOnly) 7460b57cec5SDimitry Andric continue; 7470b57cec5SDimitry Andric if (VD->isExternallyVisible() && 7480b57cec5SDimitry Andric !isExternalWithNoLinkageType(VD) && 7490b57cec5SDimitry Andric !VD->getMostRecentDecl()->isInline() && 7500b57cec5SDimitry Andric !VD->hasAttr<ExcludeFromExplicitInstantiationAttr>()) 7510b57cec5SDimitry Andric continue; 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric // Skip VarDecls that lack formal definitions but which we know are in 7540b57cec5SDimitry Andric // fact defined somewhere. 7550b57cec5SDimitry Andric if (VD->isKnownToBeDefined()) 7560b57cec5SDimitry Andric continue; 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric Undefined.push_back(std::make_pair(ND, UndefinedUse.second)); 7600b57cec5SDimitry Andric } 7610b57cec5SDimitry Andric } 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric /// checkUndefinedButUsed - Check for undefined objects with internal linkage 7640b57cec5SDimitry Andric /// or that are inline. 7650b57cec5SDimitry Andric static void checkUndefinedButUsed(Sema &S) { 7660b57cec5SDimitry Andric if (S.UndefinedButUsed.empty()) return; 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric // Collect all the still-undefined entities with internal linkage. 7690b57cec5SDimitry Andric SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined; 7700b57cec5SDimitry Andric S.getUndefinedButUsed(Undefined); 7710b57cec5SDimitry Andric if (Undefined.empty()) return; 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric for (auto Undef : Undefined) { 7740b57cec5SDimitry Andric ValueDecl *VD = cast<ValueDecl>(Undef.first); 7750b57cec5SDimitry Andric SourceLocation UseLoc = Undef.second; 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric if (S.isExternalWithNoLinkageType(VD)) { 7780b57cec5SDimitry Andric // C++ [basic.link]p8: 7790b57cec5SDimitry Andric // A type without linkage shall not be used as the type of a variable 7800b57cec5SDimitry Andric // or function with external linkage unless 7810b57cec5SDimitry Andric // -- the entity has C language linkage 7820b57cec5SDimitry Andric // -- the entity is not odr-used or is defined in the same TU 7830b57cec5SDimitry Andric // 7840b57cec5SDimitry Andric // As an extension, accept this in cases where the type is externally 7850b57cec5SDimitry Andric // visible, since the function or variable actually can be defined in 7860b57cec5SDimitry Andric // another translation unit in that case. 7870b57cec5SDimitry Andric S.Diag(VD->getLocation(), isExternallyVisible(VD->getType()->getLinkage()) 7880b57cec5SDimitry Andric ? diag::ext_undefined_internal_type 7890b57cec5SDimitry Andric : diag::err_undefined_internal_type) 7900b57cec5SDimitry Andric << isa<VarDecl>(VD) << VD; 7910b57cec5SDimitry Andric } else if (!VD->isExternallyVisible()) { 7920b57cec5SDimitry Andric // FIXME: We can promote this to an error. The function or variable can't 7930b57cec5SDimitry Andric // be defined anywhere else, so the program must necessarily violate the 7940b57cec5SDimitry Andric // one definition rule. 7950b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::warn_undefined_internal) 7960b57cec5SDimitry Andric << isa<VarDecl>(VD) << VD; 7970b57cec5SDimitry Andric } else if (auto *FD = dyn_cast<FunctionDecl>(VD)) { 7980b57cec5SDimitry Andric (void)FD; 7990b57cec5SDimitry Andric assert(FD->getMostRecentDecl()->isInlined() && 8000b57cec5SDimitry Andric "used object requires definition but isn't inline or internal?"); 8010b57cec5SDimitry Andric // FIXME: This is ill-formed; we should reject. 8020b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::warn_undefined_inline) << VD; 8030b57cec5SDimitry Andric } else { 8040b57cec5SDimitry Andric assert(cast<VarDecl>(VD)->getMostRecentDecl()->isInline() && 8050b57cec5SDimitry Andric "used var requires definition but isn't inline or internal?"); 8060b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::err_undefined_inline_var) << VD; 8070b57cec5SDimitry Andric } 8080b57cec5SDimitry Andric if (UseLoc.isValid()) 8090b57cec5SDimitry Andric S.Diag(UseLoc, diag::note_used_here); 8100b57cec5SDimitry Andric } 8110b57cec5SDimitry Andric 8120b57cec5SDimitry Andric S.UndefinedButUsed.clear(); 8130b57cec5SDimitry Andric } 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric void Sema::LoadExternalWeakUndeclaredIdentifiers() { 8160b57cec5SDimitry Andric if (!ExternalSource) 8170b57cec5SDimitry Andric return; 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs; 8200b57cec5SDimitry Andric ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs); 8210b57cec5SDimitry Andric for (auto &WeakID : WeakIDs) 8220b57cec5SDimitry Andric WeakUndeclaredIdentifiers.insert(WeakID); 8230b57cec5SDimitry Andric } 8240b57cec5SDimitry Andric 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap; 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric /// Returns true, if all methods and nested classes of the given 8290b57cec5SDimitry Andric /// CXXRecordDecl are defined in this translation unit. 8300b57cec5SDimitry Andric /// 8310b57cec5SDimitry Andric /// Should only be called from ActOnEndOfTranslationUnit so that all 8320b57cec5SDimitry Andric /// definitions are actually read. 8330b57cec5SDimitry Andric static bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD, 8340b57cec5SDimitry Andric RecordCompleteMap &MNCComplete) { 8350b57cec5SDimitry Andric RecordCompleteMap::iterator Cache = MNCComplete.find(RD); 8360b57cec5SDimitry Andric if (Cache != MNCComplete.end()) 8370b57cec5SDimitry Andric return Cache->second; 8380b57cec5SDimitry Andric if (!RD->isCompleteDefinition()) 8390b57cec5SDimitry Andric return false; 8400b57cec5SDimitry Andric bool Complete = true; 8410b57cec5SDimitry Andric for (DeclContext::decl_iterator I = RD->decls_begin(), 8420b57cec5SDimitry Andric E = RD->decls_end(); 8430b57cec5SDimitry Andric I != E && Complete; ++I) { 8440b57cec5SDimitry Andric if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I)) 8450b57cec5SDimitry Andric Complete = M->isDefined() || M->isDefaulted() || 8460b57cec5SDimitry Andric (M->isPure() && !isa<CXXDestructorDecl>(M)); 8470b57cec5SDimitry Andric else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I)) 8480b57cec5SDimitry Andric // If the template function is marked as late template parsed at this 8490b57cec5SDimitry Andric // point, it has not been instantiated and therefore we have not 8500b57cec5SDimitry Andric // performed semantic analysis on it yet, so we cannot know if the type 8510b57cec5SDimitry Andric // can be considered complete. 8520b57cec5SDimitry Andric Complete = !F->getTemplatedDecl()->isLateTemplateParsed() && 8530b57cec5SDimitry Andric F->getTemplatedDecl()->isDefined(); 8540b57cec5SDimitry Andric else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) { 8550b57cec5SDimitry Andric if (R->isInjectedClassName()) 8560b57cec5SDimitry Andric continue; 8570b57cec5SDimitry Andric if (R->hasDefinition()) 8580b57cec5SDimitry Andric Complete = MethodsAndNestedClassesComplete(R->getDefinition(), 8590b57cec5SDimitry Andric MNCComplete); 8600b57cec5SDimitry Andric else 8610b57cec5SDimitry Andric Complete = false; 8620b57cec5SDimitry Andric } 8630b57cec5SDimitry Andric } 8640b57cec5SDimitry Andric MNCComplete[RD] = Complete; 8650b57cec5SDimitry Andric return Complete; 8660b57cec5SDimitry Andric } 8670b57cec5SDimitry Andric 8680b57cec5SDimitry Andric /// Returns true, if the given CXXRecordDecl is fully defined in this 8690b57cec5SDimitry Andric /// translation unit, i.e. all methods are defined or pure virtual and all 8700b57cec5SDimitry Andric /// friends, friend functions and nested classes are fully defined in this 8710b57cec5SDimitry Andric /// translation unit. 8720b57cec5SDimitry Andric /// 8730b57cec5SDimitry Andric /// Should only be called from ActOnEndOfTranslationUnit so that all 8740b57cec5SDimitry Andric /// definitions are actually read. 8750b57cec5SDimitry Andric static bool IsRecordFullyDefined(const CXXRecordDecl *RD, 8760b57cec5SDimitry Andric RecordCompleteMap &RecordsComplete, 8770b57cec5SDimitry Andric RecordCompleteMap &MNCComplete) { 8780b57cec5SDimitry Andric RecordCompleteMap::iterator Cache = RecordsComplete.find(RD); 8790b57cec5SDimitry Andric if (Cache != RecordsComplete.end()) 8800b57cec5SDimitry Andric return Cache->second; 8810b57cec5SDimitry Andric bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete); 8820b57cec5SDimitry Andric for (CXXRecordDecl::friend_iterator I = RD->friend_begin(), 8830b57cec5SDimitry Andric E = RD->friend_end(); 8840b57cec5SDimitry Andric I != E && Complete; ++I) { 8850b57cec5SDimitry Andric // Check if friend classes and methods are complete. 8860b57cec5SDimitry Andric if (TypeSourceInfo *TSI = (*I)->getFriendType()) { 8870b57cec5SDimitry Andric // Friend classes are available as the TypeSourceInfo of the FriendDecl. 8880b57cec5SDimitry Andric if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl()) 8890b57cec5SDimitry Andric Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete); 8900b57cec5SDimitry Andric else 8910b57cec5SDimitry Andric Complete = false; 8920b57cec5SDimitry Andric } else { 8930b57cec5SDimitry Andric // Friend functions are available through the NamedDecl of FriendDecl. 8940b57cec5SDimitry Andric if (const FunctionDecl *FD = 8950b57cec5SDimitry Andric dyn_cast<FunctionDecl>((*I)->getFriendDecl())) 8960b57cec5SDimitry Andric Complete = FD->isDefined(); 8970b57cec5SDimitry Andric else 8980b57cec5SDimitry Andric // This is a template friend, give up. 8990b57cec5SDimitry Andric Complete = false; 9000b57cec5SDimitry Andric } 9010b57cec5SDimitry Andric } 9020b57cec5SDimitry Andric RecordsComplete[RD] = Complete; 9030b57cec5SDimitry Andric return Complete; 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric 9060b57cec5SDimitry Andric void Sema::emitAndClearUnusedLocalTypedefWarnings() { 9070b57cec5SDimitry Andric if (ExternalSource) 9080b57cec5SDimitry Andric ExternalSource->ReadUnusedLocalTypedefNameCandidates( 9090b57cec5SDimitry Andric UnusedLocalTypedefNameCandidates); 9100b57cec5SDimitry Andric for (const TypedefNameDecl *TD : UnusedLocalTypedefNameCandidates) { 9110b57cec5SDimitry Andric if (TD->isReferenced()) 9120b57cec5SDimitry Andric continue; 9130b57cec5SDimitry Andric Diag(TD->getLocation(), diag::warn_unused_local_typedef) 9140b57cec5SDimitry Andric << isa<TypeAliasDecl>(TD) << TD->getDeclName(); 9150b57cec5SDimitry Andric } 9160b57cec5SDimitry Andric UnusedLocalTypedefNameCandidates.clear(); 9170b57cec5SDimitry Andric } 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric /// This is called before the very first declaration in the translation unit 9200b57cec5SDimitry Andric /// is parsed. Note that the ASTContext may have already injected some 9210b57cec5SDimitry Andric /// declarations. 9220b57cec5SDimitry Andric void Sema::ActOnStartOfTranslationUnit() { 9230b57cec5SDimitry Andric if (getLangOpts().ModulesTS && 9240b57cec5SDimitry Andric (getLangOpts().getCompilingModule() == LangOptions::CMK_ModuleInterface || 9250b57cec5SDimitry Andric getLangOpts().getCompilingModule() == LangOptions::CMK_None)) { 9260b57cec5SDimitry Andric // We start in an implied global module fragment. 9270b57cec5SDimitry Andric SourceLocation StartOfTU = 9280b57cec5SDimitry Andric SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 9290b57cec5SDimitry Andric ActOnGlobalModuleFragmentDecl(StartOfTU); 9300b57cec5SDimitry Andric ModuleScopes.back().ImplicitGlobalModuleFragment = true; 9310b57cec5SDimitry Andric } 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) { 9350b57cec5SDimitry Andric // No explicit actions are required at the end of the global module fragment. 9360b57cec5SDimitry Andric if (Kind == TUFragmentKind::Global) 9370b57cec5SDimitry Andric return; 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric // Transfer late parsed template instantiations over to the pending template 9400b57cec5SDimitry Andric // instantiation list. During normal compilation, the late template parser 9410b57cec5SDimitry Andric // will be installed and instantiating these templates will succeed. 9420b57cec5SDimitry Andric // 9430b57cec5SDimitry Andric // If we are building a TU prefix for serialization, it is also safe to 9440b57cec5SDimitry Andric // transfer these over, even though they are not parsed. The end of the TU 9450b57cec5SDimitry Andric // should be outside of any eager template instantiation scope, so when this 9460b57cec5SDimitry Andric // AST is deserialized, these templates will not be parsed until the end of 9470b57cec5SDimitry Andric // the combined TU. 9480b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.end(), 9490b57cec5SDimitry Andric LateParsedInstantiations.begin(), 9500b57cec5SDimitry Andric LateParsedInstantiations.end()); 9510b57cec5SDimitry Andric LateParsedInstantiations.clear(); 9520b57cec5SDimitry Andric 9530b57cec5SDimitry Andric // If DefinedUsedVTables ends up marking any virtual member functions it 9540b57cec5SDimitry Andric // might lead to more pending template instantiations, which we then need 9550b57cec5SDimitry Andric // to instantiate. 9560b57cec5SDimitry Andric DefineUsedVTables(); 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric // C++: Perform implicit template instantiations. 9590b57cec5SDimitry Andric // 9600b57cec5SDimitry Andric // FIXME: When we perform these implicit instantiations, we do not 9610b57cec5SDimitry Andric // carefully keep track of the point of instantiation (C++ [temp.point]). 9620b57cec5SDimitry Andric // This means that name lookup that occurs within the template 9630b57cec5SDimitry Andric // instantiation will always happen at the end of the translation unit, 9640b57cec5SDimitry Andric // so it will find some names that are not required to be found. This is 9650b57cec5SDimitry Andric // valid, but we could do better by diagnosing if an instantiation uses a 9660b57cec5SDimitry Andric // name that was not visible at its first point of instantiation. 9670b57cec5SDimitry Andric if (ExternalSource) { 9680b57cec5SDimitry Andric // Load pending instantiations from the external source. 9690b57cec5SDimitry Andric SmallVector<PendingImplicitInstantiation, 4> Pending; 9700b57cec5SDimitry Andric ExternalSource->ReadPendingInstantiations(Pending); 9710b57cec5SDimitry Andric for (auto PII : Pending) 9720b57cec5SDimitry Andric if (auto Func = dyn_cast<FunctionDecl>(PII.first)) 9730b57cec5SDimitry Andric Func->setInstantiationIsPending(true); 9740b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.begin(), 9750b57cec5SDimitry Andric Pending.begin(), Pending.end()); 9760b57cec5SDimitry Andric } 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric { 979480093f4SDimitry Andric llvm::TimeTraceScope TimeScope("PerformPendingInstantiations"); 9800b57cec5SDimitry Andric PerformPendingInstantiations(); 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric 9835ffd83dbSDimitry Andric emitDeferredDiags(); 984a7dea167SDimitry Andric 9850b57cec5SDimitry Andric assert(LateParsedInstantiations.empty() && 9860b57cec5SDimitry Andric "end of TU template instantiation should not create more " 9870b57cec5SDimitry Andric "late-parsed templates"); 988a7dea167SDimitry Andric 989a7dea167SDimitry Andric // Report diagnostics for uncorrected delayed typos. Ideally all of them 990a7dea167SDimitry Andric // should have been corrected by that time, but it is very hard to cover all 991a7dea167SDimitry Andric // cases in practice. 992a7dea167SDimitry Andric for (const auto &Typo : DelayedTypos) { 993a7dea167SDimitry Andric // We pass an empty TypoCorrection to indicate no correction was performed. 994a7dea167SDimitry Andric Typo.second.DiagHandler(TypoCorrection()); 995a7dea167SDimitry Andric } 996a7dea167SDimitry Andric DelayedTypos.clear(); 9970b57cec5SDimitry Andric } 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric /// ActOnEndOfTranslationUnit - This is called at the very end of the 10000b57cec5SDimitry Andric /// translation unit when EOF is reached and all but the top-level scope is 10010b57cec5SDimitry Andric /// popped. 10020b57cec5SDimitry Andric void Sema::ActOnEndOfTranslationUnit() { 10030b57cec5SDimitry Andric assert(DelayedDiagnostics.getCurrentPool() == nullptr 10040b57cec5SDimitry Andric && "reached end of translation unit with a pool attached?"); 10050b57cec5SDimitry Andric 10060b57cec5SDimitry Andric // If code completion is enabled, don't perform any end-of-translation-unit 10070b57cec5SDimitry Andric // work. 10080b57cec5SDimitry Andric if (PP.isCodeCompletionEnabled()) 10090b57cec5SDimitry Andric return; 10100b57cec5SDimitry Andric 10110b57cec5SDimitry Andric // Complete translation units and modules define vtables and perform implicit 10120b57cec5SDimitry Andric // instantiations. PCH files do not. 10130b57cec5SDimitry Andric if (TUKind != TU_Prefix) { 10140b57cec5SDimitry Andric DiagnoseUseOfUnimplementedSelectors(); 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric ActOnEndOfTranslationUnitFragment( 10170b57cec5SDimitry Andric !ModuleScopes.empty() && ModuleScopes.back().Module->Kind == 10180b57cec5SDimitry Andric Module::PrivateModuleFragment 10190b57cec5SDimitry Andric ? TUFragmentKind::Private 10200b57cec5SDimitry Andric : TUFragmentKind::Normal); 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric if (LateTemplateParserCleanup) 10230b57cec5SDimitry Andric LateTemplateParserCleanup(OpaqueParser); 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric CheckDelayedMemberExceptionSpecs(); 10260b57cec5SDimitry Andric } else { 10270b57cec5SDimitry Andric // If we are building a TU prefix for serialization, it is safe to transfer 10280b57cec5SDimitry Andric // these over, even though they are not parsed. The end of the TU should be 10290b57cec5SDimitry Andric // outside of any eager template instantiation scope, so when this AST is 10300b57cec5SDimitry Andric // deserialized, these templates will not be parsed until the end of the 10310b57cec5SDimitry Andric // combined TU. 10320b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.end(), 10330b57cec5SDimitry Andric LateParsedInstantiations.begin(), 10340b57cec5SDimitry Andric LateParsedInstantiations.end()); 10350b57cec5SDimitry Andric LateParsedInstantiations.clear(); 10365ffd83dbSDimitry Andric 10375ffd83dbSDimitry Andric if (LangOpts.PCHInstantiateTemplates) { 10385ffd83dbSDimitry Andric llvm::TimeTraceScope TimeScope("PerformPendingInstantiations"); 10395ffd83dbSDimitry Andric PerformPendingInstantiations(); 10405ffd83dbSDimitry Andric } 10410b57cec5SDimitry Andric } 10420b57cec5SDimitry Andric 1043*e8d8bef9SDimitry Andric DiagnoseUnterminatedPragmaAlignPack(); 10440b57cec5SDimitry Andric DiagnoseUnterminatedPragmaAttribute(); 10450b57cec5SDimitry Andric 10460b57cec5SDimitry Andric // All delayed member exception specs should be checked or we end up accepting 10470b57cec5SDimitry Andric // incompatible declarations. 10480b57cec5SDimitry Andric assert(DelayedOverridingExceptionSpecChecks.empty()); 10490b57cec5SDimitry Andric assert(DelayedEquivalentExceptionSpecChecks.empty()); 10500b57cec5SDimitry Andric 10510b57cec5SDimitry Andric // All dllexport classes should have been processed already. 10520b57cec5SDimitry Andric assert(DelayedDllExportClasses.empty()); 10530b57cec5SDimitry Andric assert(DelayedDllExportMemberFunctions.empty()); 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric // Remove file scoped decls that turned out to be used. 10560b57cec5SDimitry Andric UnusedFileScopedDecls.erase( 10570b57cec5SDimitry Andric std::remove_if(UnusedFileScopedDecls.begin(nullptr, true), 10580b57cec5SDimitry Andric UnusedFileScopedDecls.end(), 10590b57cec5SDimitry Andric [this](const DeclaratorDecl *DD) { 10600b57cec5SDimitry Andric return ShouldRemoveFromUnused(this, DD); 10610b57cec5SDimitry Andric }), 10620b57cec5SDimitry Andric UnusedFileScopedDecls.end()); 10630b57cec5SDimitry Andric 10640b57cec5SDimitry Andric if (TUKind == TU_Prefix) { 10650b57cec5SDimitry Andric // Translation unit prefixes don't need any of the checking below. 10660b57cec5SDimitry Andric if (!PP.isIncrementalProcessingEnabled()) 10670b57cec5SDimitry Andric TUScope = nullptr; 10680b57cec5SDimitry Andric return; 10690b57cec5SDimitry Andric } 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric // Check for #pragma weak identifiers that were never declared 10720b57cec5SDimitry Andric LoadExternalWeakUndeclaredIdentifiers(); 10730b57cec5SDimitry Andric for (auto WeakID : WeakUndeclaredIdentifiers) { 10740b57cec5SDimitry Andric if (WeakID.second.getUsed()) 10750b57cec5SDimitry Andric continue; 10760b57cec5SDimitry Andric 10770b57cec5SDimitry Andric Decl *PrevDecl = LookupSingleName(TUScope, WeakID.first, SourceLocation(), 10780b57cec5SDimitry Andric LookupOrdinaryName); 10790b57cec5SDimitry Andric if (PrevDecl != nullptr && 10800b57cec5SDimitry Andric !(isa<FunctionDecl>(PrevDecl) || isa<VarDecl>(PrevDecl))) 10810b57cec5SDimitry Andric Diag(WeakID.second.getLocation(), diag::warn_attribute_wrong_decl_type) 10820b57cec5SDimitry Andric << "'weak'" << ExpectedVariableOrFunction; 10830b57cec5SDimitry Andric else 10840b57cec5SDimitry Andric Diag(WeakID.second.getLocation(), diag::warn_weak_identifier_undeclared) 10850b57cec5SDimitry Andric << WeakID.first; 10860b57cec5SDimitry Andric } 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric if (LangOpts.CPlusPlus11 && 10890b57cec5SDimitry Andric !Diags.isIgnored(diag::warn_delegating_ctor_cycle, SourceLocation())) 10900b57cec5SDimitry Andric CheckDelegatingCtorCycles(); 10910b57cec5SDimitry Andric 10920b57cec5SDimitry Andric if (!Diags.hasErrorOccurred()) { 10930b57cec5SDimitry Andric if (ExternalSource) 10940b57cec5SDimitry Andric ExternalSource->ReadUndefinedButUsed(UndefinedButUsed); 10950b57cec5SDimitry Andric checkUndefinedButUsed(*this); 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric 10980b57cec5SDimitry Andric // A global-module-fragment is only permitted within a module unit. 10990b57cec5SDimitry Andric bool DiagnosedMissingModuleDeclaration = false; 11000b57cec5SDimitry Andric if (!ModuleScopes.empty() && 11010b57cec5SDimitry Andric ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment && 11020b57cec5SDimitry Andric !ModuleScopes.back().ImplicitGlobalModuleFragment) { 11030b57cec5SDimitry Andric Diag(ModuleScopes.back().BeginLoc, 11040b57cec5SDimitry Andric diag::err_module_declaration_missing_after_global_module_introducer); 11050b57cec5SDimitry Andric DiagnosedMissingModuleDeclaration = true; 11060b57cec5SDimitry Andric } 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric if (TUKind == TU_Module) { 11090b57cec5SDimitry Andric // If we are building a module interface unit, we need to have seen the 11100b57cec5SDimitry Andric // module declaration by now. 11110b57cec5SDimitry Andric if (getLangOpts().getCompilingModule() == 11120b57cec5SDimitry Andric LangOptions::CMK_ModuleInterface && 11130b57cec5SDimitry Andric (ModuleScopes.empty() || 11140b57cec5SDimitry Andric !ModuleScopes.back().Module->isModulePurview()) && 11150b57cec5SDimitry Andric !DiagnosedMissingModuleDeclaration) { 11160b57cec5SDimitry Andric // FIXME: Make a better guess as to where to put the module declaration. 11170b57cec5SDimitry Andric Diag(getSourceManager().getLocForStartOfFile( 11180b57cec5SDimitry Andric getSourceManager().getMainFileID()), 11190b57cec5SDimitry Andric diag::err_module_declaration_missing); 11200b57cec5SDimitry Andric } 11210b57cec5SDimitry Andric 11220b57cec5SDimitry Andric // If we are building a module, resolve all of the exported declarations 11230b57cec5SDimitry Andric // now. 11240b57cec5SDimitry Andric if (Module *CurrentModule = PP.getCurrentModule()) { 11250b57cec5SDimitry Andric ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric SmallVector<Module *, 2> Stack; 11280b57cec5SDimitry Andric Stack.push_back(CurrentModule); 11290b57cec5SDimitry Andric while (!Stack.empty()) { 11300b57cec5SDimitry Andric Module *Mod = Stack.pop_back_val(); 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric // Resolve the exported declarations and conflicts. 11330b57cec5SDimitry Andric // FIXME: Actually complain, once we figure out how to teach the 11340b57cec5SDimitry Andric // diagnostic client to deal with complaints in the module map at this 11350b57cec5SDimitry Andric // point. 11360b57cec5SDimitry Andric ModMap.resolveExports(Mod, /*Complain=*/false); 11370b57cec5SDimitry Andric ModMap.resolveUses(Mod, /*Complain=*/false); 11380b57cec5SDimitry Andric ModMap.resolveConflicts(Mod, /*Complain=*/false); 11390b57cec5SDimitry Andric 11400b57cec5SDimitry Andric // Queue the submodules, so their exports will also be resolved. 11410b57cec5SDimitry Andric Stack.append(Mod->submodule_begin(), Mod->submodule_end()); 11420b57cec5SDimitry Andric } 11430b57cec5SDimitry Andric } 11440b57cec5SDimitry Andric 11450b57cec5SDimitry Andric // Warnings emitted in ActOnEndOfTranslationUnit() should be emitted for 11460b57cec5SDimitry Andric // modules when they are built, not every time they are used. 11470b57cec5SDimitry Andric emitAndClearUnusedLocalTypedefWarnings(); 11480b57cec5SDimitry Andric } 11490b57cec5SDimitry Andric 11500b57cec5SDimitry Andric // C99 6.9.2p2: 11510b57cec5SDimitry Andric // A declaration of an identifier for an object that has file 11520b57cec5SDimitry Andric // scope without an initializer, and without a storage-class 11530b57cec5SDimitry Andric // specifier or with the storage-class specifier static, 11540b57cec5SDimitry Andric // constitutes a tentative definition. If a translation unit 11550b57cec5SDimitry Andric // contains one or more tentative definitions for an identifier, 11560b57cec5SDimitry Andric // and the translation unit contains no external definition for 11570b57cec5SDimitry Andric // that identifier, then the behavior is exactly as if the 11580b57cec5SDimitry Andric // translation unit contains a file scope declaration of that 11590b57cec5SDimitry Andric // identifier, with the composite type as of the end of the 11600b57cec5SDimitry Andric // translation unit, with an initializer equal to 0. 11610b57cec5SDimitry Andric llvm::SmallSet<VarDecl *, 32> Seen; 11620b57cec5SDimitry Andric for (TentativeDefinitionsType::iterator 11630b57cec5SDimitry Andric T = TentativeDefinitions.begin(ExternalSource), 11640b57cec5SDimitry Andric TEnd = TentativeDefinitions.end(); 11650b57cec5SDimitry Andric T != TEnd; ++T) { 11660b57cec5SDimitry Andric VarDecl *VD = (*T)->getActingDefinition(); 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric // If the tentative definition was completed, getActingDefinition() returns 11690b57cec5SDimitry Andric // null. If we've already seen this variable before, insert()'s second 11700b57cec5SDimitry Andric // return value is false. 11710b57cec5SDimitry Andric if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second) 11720b57cec5SDimitry Andric continue; 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric if (const IncompleteArrayType *ArrayT 11750b57cec5SDimitry Andric = Context.getAsIncompleteArrayType(VD->getType())) { 11760b57cec5SDimitry Andric // Set the length of the array to 1 (C99 6.9.2p5). 11770b57cec5SDimitry Andric Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); 11780b57cec5SDimitry Andric llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); 1179a7dea167SDimitry Andric QualType T = Context.getConstantArrayType(ArrayT->getElementType(), One, 1180a7dea167SDimitry Andric nullptr, ArrayType::Normal, 0); 11810b57cec5SDimitry Andric VD->setType(T); 11820b57cec5SDimitry Andric } else if (RequireCompleteType(VD->getLocation(), VD->getType(), 11830b57cec5SDimitry Andric diag::err_tentative_def_incomplete_type)) 11840b57cec5SDimitry Andric VD->setInvalidDecl(); 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric // No initialization is performed for a tentative definition. 11870b57cec5SDimitry Andric CheckCompleteVariableDeclaration(VD); 11880b57cec5SDimitry Andric 11890b57cec5SDimitry Andric // Notify the consumer that we've completed a tentative definition. 11900b57cec5SDimitry Andric if (!VD->isInvalidDecl()) 11910b57cec5SDimitry Andric Consumer.CompleteTentativeDefinition(VD); 11920b57cec5SDimitry Andric } 11930b57cec5SDimitry Andric 1194480093f4SDimitry Andric for (auto D : ExternalDeclarations) { 1195480093f4SDimitry Andric if (!D || D->isInvalidDecl() || D->getPreviousDecl() || !D->isUsed()) 1196480093f4SDimitry Andric continue; 1197480093f4SDimitry Andric 1198480093f4SDimitry Andric Consumer.CompleteExternalDeclaration(D); 1199480093f4SDimitry Andric } 1200480093f4SDimitry Andric 12010b57cec5SDimitry Andric // If there were errors, disable 'unused' warnings since they will mostly be 12020b57cec5SDimitry Andric // noise. Don't warn for a use from a module: either we should warn on all 12030b57cec5SDimitry Andric // file-scope declarations in modules or not at all, but whether the 12040b57cec5SDimitry Andric // declaration is used is immaterial. 12050b57cec5SDimitry Andric if (!Diags.hasErrorOccurred() && TUKind != TU_Module) { 12060b57cec5SDimitry Andric // Output warning for unused file scoped decls. 12070b57cec5SDimitry Andric for (UnusedFileScopedDeclsType::iterator 12080b57cec5SDimitry Andric I = UnusedFileScopedDecls.begin(ExternalSource), 12090b57cec5SDimitry Andric E = UnusedFileScopedDecls.end(); I != E; ++I) { 12100b57cec5SDimitry Andric if (ShouldRemoveFromUnused(this, *I)) 12110b57cec5SDimitry Andric continue; 12120b57cec5SDimitry Andric 12130b57cec5SDimitry Andric if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { 12140b57cec5SDimitry Andric const FunctionDecl *DiagD; 12150b57cec5SDimitry Andric if (!FD->hasBody(DiagD)) 12160b57cec5SDimitry Andric DiagD = FD; 12170b57cec5SDimitry Andric if (DiagD->isDeleted()) 12180b57cec5SDimitry Andric continue; // Deleted functions are supposed to be unused. 12190b57cec5SDimitry Andric if (DiagD->isReferenced()) { 12200b57cec5SDimitry Andric if (isa<CXXMethodDecl>(DiagD)) 12210b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_member_function) 1222*e8d8bef9SDimitry Andric << DiagD; 12230b57cec5SDimitry Andric else { 12240b57cec5SDimitry Andric if (FD->getStorageClass() == SC_Static && 12250b57cec5SDimitry Andric !FD->isInlineSpecified() && 12260b57cec5SDimitry Andric !SourceMgr.isInMainFile( 12270b57cec5SDimitry Andric SourceMgr.getExpansionLoc(FD->getLocation()))) 12280b57cec5SDimitry Andric Diag(DiagD->getLocation(), 12290b57cec5SDimitry Andric diag::warn_unneeded_static_internal_decl) 1230*e8d8bef9SDimitry Andric << DiagD; 12310b57cec5SDimitry Andric else 12320b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) 1233*e8d8bef9SDimitry Andric << /*function*/ 0 << DiagD; 12340b57cec5SDimitry Andric } 12350b57cec5SDimitry Andric } else { 12360b57cec5SDimitry Andric if (FD->getDescribedFunctionTemplate()) 12370b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_template) 1238*e8d8bef9SDimitry Andric << /*function*/ 0 << DiagD; 12390b57cec5SDimitry Andric else 1240*e8d8bef9SDimitry Andric Diag(DiagD->getLocation(), isa<CXXMethodDecl>(DiagD) 1241*e8d8bef9SDimitry Andric ? diag::warn_unused_member_function 12420b57cec5SDimitry Andric : diag::warn_unused_function) 1243*e8d8bef9SDimitry Andric << DiagD; 12440b57cec5SDimitry Andric } 12450b57cec5SDimitry Andric } else { 12460b57cec5SDimitry Andric const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition(); 12470b57cec5SDimitry Andric if (!DiagD) 12480b57cec5SDimitry Andric DiagD = cast<VarDecl>(*I); 12490b57cec5SDimitry Andric if (DiagD->isReferenced()) { 12500b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) 1251*e8d8bef9SDimitry Andric << /*variable*/ 1 << DiagD; 12520b57cec5SDimitry Andric } else if (DiagD->getType().isConstQualified()) { 12530b57cec5SDimitry Andric const SourceManager &SM = SourceMgr; 12540b57cec5SDimitry Andric if (SM.getMainFileID() != SM.getFileID(DiagD->getLocation()) || 12550b57cec5SDimitry Andric !PP.getLangOpts().IsHeaderFile) 12560b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_const_variable) 1257*e8d8bef9SDimitry Andric << DiagD; 12580b57cec5SDimitry Andric } else { 12590b57cec5SDimitry Andric if (DiagD->getDescribedVarTemplate()) 12600b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_template) 1261*e8d8bef9SDimitry Andric << /*variable*/ 1 << DiagD; 12620b57cec5SDimitry Andric else 1263*e8d8bef9SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_variable) << DiagD; 12640b57cec5SDimitry Andric } 12650b57cec5SDimitry Andric } 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric emitAndClearUnusedLocalTypedefWarnings(); 12690b57cec5SDimitry Andric } 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric if (!Diags.isIgnored(diag::warn_unused_private_field, SourceLocation())) { 12720b57cec5SDimitry Andric // FIXME: Load additional unused private field candidates from the external 12730b57cec5SDimitry Andric // source. 12740b57cec5SDimitry Andric RecordCompleteMap RecordsComplete; 12750b57cec5SDimitry Andric RecordCompleteMap MNCComplete; 12760b57cec5SDimitry Andric for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(), 12770b57cec5SDimitry Andric E = UnusedPrivateFields.end(); I != E; ++I) { 12780b57cec5SDimitry Andric const NamedDecl *D = *I; 12790b57cec5SDimitry Andric const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); 12800b57cec5SDimitry Andric if (RD && !RD->isUnion() && 12810b57cec5SDimitry Andric IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) { 12820b57cec5SDimitry Andric Diag(D->getLocation(), diag::warn_unused_private_field) 12830b57cec5SDimitry Andric << D->getDeclName(); 12840b57cec5SDimitry Andric } 12850b57cec5SDimitry Andric } 12860b57cec5SDimitry Andric } 12870b57cec5SDimitry Andric 12880b57cec5SDimitry Andric if (!Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) { 12890b57cec5SDimitry Andric if (ExternalSource) 12900b57cec5SDimitry Andric ExternalSource->ReadMismatchingDeleteExpressions(DeleteExprs); 12910b57cec5SDimitry Andric for (const auto &DeletedFieldInfo : DeleteExprs) { 12920b57cec5SDimitry Andric for (const auto &DeleteExprLoc : DeletedFieldInfo.second) { 12930b57cec5SDimitry Andric AnalyzeDeleteExprMismatch(DeletedFieldInfo.first, DeleteExprLoc.first, 12940b57cec5SDimitry Andric DeleteExprLoc.second); 12950b57cec5SDimitry Andric } 12960b57cec5SDimitry Andric } 12970b57cec5SDimitry Andric } 12980b57cec5SDimitry Andric 12990b57cec5SDimitry Andric // Check we've noticed that we're no longer parsing the initializer for every 13000b57cec5SDimitry Andric // variable. If we miss cases, then at best we have a performance issue and 13010b57cec5SDimitry Andric // at worst a rejects-valid bug. 13020b57cec5SDimitry Andric assert(ParsingInitForAutoVars.empty() && 13030b57cec5SDimitry Andric "Didn't unmark var as having its initializer parsed"); 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric if (!PP.isIncrementalProcessingEnabled()) 13060b57cec5SDimitry Andric TUScope = nullptr; 13070b57cec5SDimitry Andric } 13080b57cec5SDimitry Andric 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13110b57cec5SDimitry Andric // Helper functions. 13120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13130b57cec5SDimitry Andric 13140b57cec5SDimitry Andric DeclContext *Sema::getFunctionLevelDeclContext() { 13150b57cec5SDimitry Andric DeclContext *DC = CurContext; 13160b57cec5SDimitry Andric 13170b57cec5SDimitry Andric while (true) { 131855e4f9d5SDimitry Andric if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC) || 131955e4f9d5SDimitry Andric isa<RequiresExprBodyDecl>(DC)) { 13200b57cec5SDimitry Andric DC = DC->getParent(); 13210b57cec5SDimitry Andric } else if (isa<CXXMethodDecl>(DC) && 13220b57cec5SDimitry Andric cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && 13230b57cec5SDimitry Andric cast<CXXRecordDecl>(DC->getParent())->isLambda()) { 13240b57cec5SDimitry Andric DC = DC->getParent()->getParent(); 13250b57cec5SDimitry Andric } 13260b57cec5SDimitry Andric else break; 13270b57cec5SDimitry Andric } 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric return DC; 13300b57cec5SDimitry Andric } 13310b57cec5SDimitry Andric 13320b57cec5SDimitry Andric /// getCurFunctionDecl - If inside of a function body, this returns a pointer 13330b57cec5SDimitry Andric /// to the function decl for the function being parsed. If we're currently 13340b57cec5SDimitry Andric /// in a 'block', this returns the containing context. 13350b57cec5SDimitry Andric FunctionDecl *Sema::getCurFunctionDecl() { 13360b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 13370b57cec5SDimitry Andric return dyn_cast<FunctionDecl>(DC); 13380b57cec5SDimitry Andric } 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric ObjCMethodDecl *Sema::getCurMethodDecl() { 13410b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 13420b57cec5SDimitry Andric while (isa<RecordDecl>(DC)) 13430b57cec5SDimitry Andric DC = DC->getParent(); 13440b57cec5SDimitry Andric return dyn_cast<ObjCMethodDecl>(DC); 13450b57cec5SDimitry Andric } 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric NamedDecl *Sema::getCurFunctionOrMethodDecl() { 13480b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 13490b57cec5SDimitry Andric if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) 13500b57cec5SDimitry Andric return cast<NamedDecl>(DC); 13510b57cec5SDimitry Andric return nullptr; 13520b57cec5SDimitry Andric } 13530b57cec5SDimitry Andric 1354480093f4SDimitry Andric LangAS Sema::getDefaultCXXMethodAddrSpace() const { 1355480093f4SDimitry Andric if (getLangOpts().OpenCL) 1356480093f4SDimitry Andric return LangAS::opencl_generic; 1357480093f4SDimitry Andric return LangAS::Default; 1358480093f4SDimitry Andric } 1359480093f4SDimitry Andric 13600b57cec5SDimitry Andric void Sema::EmitCurrentDiagnostic(unsigned DiagID) { 13610b57cec5SDimitry Andric // FIXME: It doesn't make sense to me that DiagID is an incoming argument here 13620b57cec5SDimitry Andric // and yet we also use the current diag ID on the DiagnosticsEngine. This has 13630b57cec5SDimitry Andric // been made more painfully obvious by the refactor that introduced this 13640b57cec5SDimitry Andric // function, but it is possible that the incoming argument can be 13650b57cec5SDimitry Andric // eliminated. If it truly cannot be (for example, there is some reentrancy 13660b57cec5SDimitry Andric // issue I am not seeing yet), then there should at least be a clarifying 13670b57cec5SDimitry Andric // comment somewhere. 13680b57cec5SDimitry Andric if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) { 13690b57cec5SDimitry Andric switch (DiagnosticIDs::getDiagnosticSFINAEResponse( 13700b57cec5SDimitry Andric Diags.getCurrentDiagID())) { 13710b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_Report: 13720b57cec5SDimitry Andric // We'll report the diagnostic below. 13730b57cec5SDimitry Andric break; 13740b57cec5SDimitry Andric 13750b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_SubstitutionFailure: 13760b57cec5SDimitry Andric // Count this failure so that we know that template argument deduction 13770b57cec5SDimitry Andric // has failed. 13780b57cec5SDimitry Andric ++NumSFINAEErrors; 13790b57cec5SDimitry Andric 13800b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 13810b57cec5SDimitry Andric // template-deduction information. 13820b57cec5SDimitry Andric if (*Info && !(*Info)->hasSFINAEDiagnostic()) { 13830b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 13840b57cec5SDimitry Andric (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(), 13850b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 13860b57cec5SDimitry Andric } 13870b57cec5SDimitry Andric 1388a7dea167SDimitry Andric Diags.setLastDiagnosticIgnored(true); 13890b57cec5SDimitry Andric Diags.Clear(); 13900b57cec5SDimitry Andric return; 13910b57cec5SDimitry Andric 13920b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_AccessControl: { 13930b57cec5SDimitry Andric // Per C++ Core Issue 1170, access control is part of SFINAE. 13940b57cec5SDimitry Andric // Additionally, the AccessCheckingSFINAE flag can be used to temporarily 13950b57cec5SDimitry Andric // make access control a part of SFINAE for the purposes of checking 13960b57cec5SDimitry Andric // type traits. 13970b57cec5SDimitry Andric if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11) 13980b57cec5SDimitry Andric break; 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric SourceLocation Loc = Diags.getCurrentDiagLoc(); 14010b57cec5SDimitry Andric 14020b57cec5SDimitry Andric // Suppress this diagnostic. 14030b57cec5SDimitry Andric ++NumSFINAEErrors; 14040b57cec5SDimitry Andric 14050b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 14060b57cec5SDimitry Andric // template-deduction information. 14070b57cec5SDimitry Andric if (*Info && !(*Info)->hasSFINAEDiagnostic()) { 14080b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 14090b57cec5SDimitry Andric (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(), 14100b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 14110b57cec5SDimitry Andric } 14120b57cec5SDimitry Andric 1413a7dea167SDimitry Andric Diags.setLastDiagnosticIgnored(true); 14140b57cec5SDimitry Andric Diags.Clear(); 14150b57cec5SDimitry Andric 14160b57cec5SDimitry Andric // Now the diagnostic state is clear, produce a C++98 compatibility 14170b57cec5SDimitry Andric // warning. 14180b57cec5SDimitry Andric Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control); 14190b57cec5SDimitry Andric 14200b57cec5SDimitry Andric // The last diagnostic which Sema produced was ignored. Suppress any 14210b57cec5SDimitry Andric // notes attached to it. 1422a7dea167SDimitry Andric Diags.setLastDiagnosticIgnored(true); 14230b57cec5SDimitry Andric return; 14240b57cec5SDimitry Andric } 14250b57cec5SDimitry Andric 14260b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_Suppress: 14270b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 14280b57cec5SDimitry Andric // template-deduction information; 14290b57cec5SDimitry Andric if (*Info) { 14300b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 14310b57cec5SDimitry Andric (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(), 14320b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 14330b57cec5SDimitry Andric } 14340b57cec5SDimitry Andric 14350b57cec5SDimitry Andric // Suppress this diagnostic. 1436a7dea167SDimitry Andric Diags.setLastDiagnosticIgnored(true); 14370b57cec5SDimitry Andric Diags.Clear(); 14380b57cec5SDimitry Andric return; 14390b57cec5SDimitry Andric } 14400b57cec5SDimitry Andric } 14410b57cec5SDimitry Andric 14420b57cec5SDimitry Andric // Copy the diagnostic printing policy over the ASTContext printing policy. 14430b57cec5SDimitry Andric // TODO: Stop doing that. See: https://reviews.llvm.org/D45093#1090292 14440b57cec5SDimitry Andric Context.setPrintingPolicy(getPrintingPolicy()); 14450b57cec5SDimitry Andric 14460b57cec5SDimitry Andric // Emit the diagnostic. 14470b57cec5SDimitry Andric if (!Diags.EmitCurrentDiagnostic()) 14480b57cec5SDimitry Andric return; 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andric // If this is not a note, and we're in a template instantiation 14510b57cec5SDimitry Andric // that is different from the last template instantiation where 14520b57cec5SDimitry Andric // we emitted an error, print a template instantiation 14530b57cec5SDimitry Andric // backtrace. 14540b57cec5SDimitry Andric if (!DiagnosticIDs::isBuiltinNote(DiagID)) 14550b57cec5SDimitry Andric PrintContextStack(); 14560b57cec5SDimitry Andric } 14570b57cec5SDimitry Andric 14580b57cec5SDimitry Andric Sema::SemaDiagnosticBuilder 1459*e8d8bef9SDimitry Andric Sema::Diag(SourceLocation Loc, const PartialDiagnostic &PD, bool DeferHint) { 1460*e8d8bef9SDimitry Andric return Diag(Loc, PD.getDiagID(), DeferHint) << PD; 1461*e8d8bef9SDimitry Andric } 14620b57cec5SDimitry Andric 1463*e8d8bef9SDimitry Andric bool Sema::hasUncompilableErrorOccurred() const { 1464*e8d8bef9SDimitry Andric if (getDiagnostics().hasUncompilableErrorOccurred()) 1465*e8d8bef9SDimitry Andric return true; 1466*e8d8bef9SDimitry Andric auto *FD = dyn_cast<FunctionDecl>(CurContext); 1467*e8d8bef9SDimitry Andric if (!FD) 1468*e8d8bef9SDimitry Andric return false; 1469*e8d8bef9SDimitry Andric auto Loc = DeviceDeferredDiags.find(FD); 1470*e8d8bef9SDimitry Andric if (Loc == DeviceDeferredDiags.end()) 1471*e8d8bef9SDimitry Andric return false; 1472*e8d8bef9SDimitry Andric for (auto PDAt : Loc->second) { 1473*e8d8bef9SDimitry Andric if (DiagnosticIDs::isDefaultMappingAsError(PDAt.second.getDiagID())) 1474*e8d8bef9SDimitry Andric return true; 1475*e8d8bef9SDimitry Andric } 1476*e8d8bef9SDimitry Andric return false; 14770b57cec5SDimitry Andric } 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric // Print notes showing how we can reach FD starting from an a priori 14800b57cec5SDimitry Andric // known-callable function. 14810b57cec5SDimitry Andric static void emitCallStackNotes(Sema &S, FunctionDecl *FD) { 14820b57cec5SDimitry Andric auto FnIt = S.DeviceKnownEmittedFns.find(FD); 14830b57cec5SDimitry Andric while (FnIt != S.DeviceKnownEmittedFns.end()) { 14845ffd83dbSDimitry Andric // Respect error limit. 14855ffd83dbSDimitry Andric if (S.Diags.hasFatalErrorOccurred()) 14865ffd83dbSDimitry Andric return; 14870b57cec5SDimitry Andric DiagnosticBuilder Builder( 14880b57cec5SDimitry Andric S.Diags.Report(FnIt->second.Loc, diag::note_called_by)); 14890b57cec5SDimitry Andric Builder << FnIt->second.FD; 14900b57cec5SDimitry Andric FnIt = S.DeviceKnownEmittedFns.find(FnIt->second.FD); 14910b57cec5SDimitry Andric } 14920b57cec5SDimitry Andric } 14930b57cec5SDimitry Andric 14945ffd83dbSDimitry Andric namespace { 14955ffd83dbSDimitry Andric 14965ffd83dbSDimitry Andric /// Helper class that emits deferred diagnostic messages if an entity directly 14975ffd83dbSDimitry Andric /// or indirectly using the function that causes the deferred diagnostic 14985ffd83dbSDimitry Andric /// messages is known to be emitted. 14995ffd83dbSDimitry Andric /// 15005ffd83dbSDimitry Andric /// During parsing of AST, certain diagnostic messages are recorded as deferred 15015ffd83dbSDimitry Andric /// diagnostics since it is unknown whether the functions containing such 15025ffd83dbSDimitry Andric /// diagnostics will be emitted. A list of potentially emitted functions and 15035ffd83dbSDimitry Andric /// variables that may potentially trigger emission of functions are also 15045ffd83dbSDimitry Andric /// recorded. DeferredDiagnosticsEmitter recursively visits used functions 15055ffd83dbSDimitry Andric /// by each function to emit deferred diagnostics. 15065ffd83dbSDimitry Andric /// 15075ffd83dbSDimitry Andric /// During the visit, certain OpenMP directives or initializer of variables 15085ffd83dbSDimitry Andric /// with certain OpenMP attributes will cause subsequent visiting of any 15095ffd83dbSDimitry Andric /// functions enter a state which is called OpenMP device context in this 15105ffd83dbSDimitry Andric /// implementation. The state is exited when the directive or initializer is 15115ffd83dbSDimitry Andric /// exited. This state can change the emission states of subsequent uses 15125ffd83dbSDimitry Andric /// of functions. 15135ffd83dbSDimitry Andric /// 15145ffd83dbSDimitry Andric /// Conceptually the functions or variables to be visited form a use graph 15155ffd83dbSDimitry Andric /// where the parent node uses the child node. At any point of the visit, 15165ffd83dbSDimitry Andric /// the tree nodes traversed from the tree root to the current node form a use 15175ffd83dbSDimitry Andric /// stack. The emission state of the current node depends on two factors: 15185ffd83dbSDimitry Andric /// 1. the emission state of the root node 15195ffd83dbSDimitry Andric /// 2. whether the current node is in OpenMP device context 15205ffd83dbSDimitry Andric /// If the function is decided to be emitted, its contained deferred diagnostics 15215ffd83dbSDimitry Andric /// are emitted, together with the information about the use stack. 15225ffd83dbSDimitry Andric /// 15235ffd83dbSDimitry Andric class DeferredDiagnosticsEmitter 15245ffd83dbSDimitry Andric : public UsedDeclVisitor<DeferredDiagnosticsEmitter> { 15255ffd83dbSDimitry Andric public: 15265ffd83dbSDimitry Andric typedef UsedDeclVisitor<DeferredDiagnosticsEmitter> Inherited; 15275ffd83dbSDimitry Andric 15285ffd83dbSDimitry Andric // Whether the function is already in the current use-path. 1529*e8d8bef9SDimitry Andric llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> InUsePath; 15305ffd83dbSDimitry Andric 15315ffd83dbSDimitry Andric // The current use-path. 15325ffd83dbSDimitry Andric llvm::SmallVector<CanonicalDeclPtr<FunctionDecl>, 4> UsePath; 15335ffd83dbSDimitry Andric 15345ffd83dbSDimitry Andric // Whether the visiting of the function has been done. Done[0] is for the 15355ffd83dbSDimitry Andric // case not in OpenMP device context. Done[1] is for the case in OpenMP 15365ffd83dbSDimitry Andric // device context. We need two sets because diagnostics emission may be 15375ffd83dbSDimitry Andric // different depending on whether it is in OpenMP device context. 1538*e8d8bef9SDimitry Andric llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> DoneMap[2]; 15395ffd83dbSDimitry Andric 15405ffd83dbSDimitry Andric // Emission state of the root node of the current use graph. 15415ffd83dbSDimitry Andric bool ShouldEmitRootNode; 15425ffd83dbSDimitry Andric 15435ffd83dbSDimitry Andric // Current OpenMP device context level. It is initialized to 0 and each 15445ffd83dbSDimitry Andric // entering of device context increases it by 1 and each exit decreases 15455ffd83dbSDimitry Andric // it by 1. Non-zero value indicates it is currently in device context. 15465ffd83dbSDimitry Andric unsigned InOMPDeviceContext; 15475ffd83dbSDimitry Andric 15485ffd83dbSDimitry Andric DeferredDiagnosticsEmitter(Sema &S) 15495ffd83dbSDimitry Andric : Inherited(S), ShouldEmitRootNode(false), InOMPDeviceContext(0) {} 15505ffd83dbSDimitry Andric 15515ffd83dbSDimitry Andric void VisitOMPTargetDirective(OMPTargetDirective *Node) { 15525ffd83dbSDimitry Andric ++InOMPDeviceContext; 15535ffd83dbSDimitry Andric Inherited::VisitOMPTargetDirective(Node); 15545ffd83dbSDimitry Andric --InOMPDeviceContext; 15555ffd83dbSDimitry Andric } 15565ffd83dbSDimitry Andric 15575ffd83dbSDimitry Andric void visitUsedDecl(SourceLocation Loc, Decl *D) { 15585ffd83dbSDimitry Andric if (isa<VarDecl>(D)) 15595ffd83dbSDimitry Andric return; 15605ffd83dbSDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(D)) 15615ffd83dbSDimitry Andric checkFunc(Loc, FD); 15625ffd83dbSDimitry Andric else 15635ffd83dbSDimitry Andric Inherited::visitUsedDecl(Loc, D); 15645ffd83dbSDimitry Andric } 15655ffd83dbSDimitry Andric 15665ffd83dbSDimitry Andric void checkVar(VarDecl *VD) { 15675ffd83dbSDimitry Andric assert(VD->isFileVarDecl() && 15685ffd83dbSDimitry Andric "Should only check file-scope variables"); 15695ffd83dbSDimitry Andric if (auto *Init = VD->getInit()) { 15705ffd83dbSDimitry Andric auto DevTy = OMPDeclareTargetDeclAttr::getDeviceType(VD); 15715ffd83dbSDimitry Andric bool IsDev = DevTy && (*DevTy == OMPDeclareTargetDeclAttr::DT_NoHost || 15725ffd83dbSDimitry Andric *DevTy == OMPDeclareTargetDeclAttr::DT_Any); 15735ffd83dbSDimitry Andric if (IsDev) 15745ffd83dbSDimitry Andric ++InOMPDeviceContext; 15755ffd83dbSDimitry Andric this->Visit(Init); 15765ffd83dbSDimitry Andric if (IsDev) 15775ffd83dbSDimitry Andric --InOMPDeviceContext; 15785ffd83dbSDimitry Andric } 15795ffd83dbSDimitry Andric } 15805ffd83dbSDimitry Andric 15815ffd83dbSDimitry Andric void checkFunc(SourceLocation Loc, FunctionDecl *FD) { 15825ffd83dbSDimitry Andric auto &Done = DoneMap[InOMPDeviceContext > 0 ? 1 : 0]; 15835ffd83dbSDimitry Andric FunctionDecl *Caller = UsePath.empty() ? nullptr : UsePath.back(); 15845ffd83dbSDimitry Andric if ((!ShouldEmitRootNode && !S.getLangOpts().OpenMP && !Caller) || 15855ffd83dbSDimitry Andric S.shouldIgnoreInHostDeviceCheck(FD) || InUsePath.count(FD)) 15865ffd83dbSDimitry Andric return; 15875ffd83dbSDimitry Andric // Finalize analysis of OpenMP-specific constructs. 1588*e8d8bef9SDimitry Andric if (Caller && S.LangOpts.OpenMP && UsePath.size() == 1 && 1589*e8d8bef9SDimitry Andric (ShouldEmitRootNode || InOMPDeviceContext)) 15905ffd83dbSDimitry Andric S.finalizeOpenMPDelayedAnalysis(Caller, FD, Loc); 15915ffd83dbSDimitry Andric if (Caller) 15925ffd83dbSDimitry Andric S.DeviceKnownEmittedFns[FD] = {Caller, Loc}; 15935ffd83dbSDimitry Andric // Always emit deferred diagnostics for the direct users. This does not 15945ffd83dbSDimitry Andric // lead to explosion of diagnostics since each user is visited at most 15955ffd83dbSDimitry Andric // twice. 15965ffd83dbSDimitry Andric if (ShouldEmitRootNode || InOMPDeviceContext) 15975ffd83dbSDimitry Andric emitDeferredDiags(FD, Caller); 15985ffd83dbSDimitry Andric // Do not revisit a function if the function body has been completely 15995ffd83dbSDimitry Andric // visited before. 16005ffd83dbSDimitry Andric if (!Done.insert(FD).second) 16015ffd83dbSDimitry Andric return; 16025ffd83dbSDimitry Andric InUsePath.insert(FD); 16035ffd83dbSDimitry Andric UsePath.push_back(FD); 16045ffd83dbSDimitry Andric if (auto *S = FD->getBody()) { 16055ffd83dbSDimitry Andric this->Visit(S); 16065ffd83dbSDimitry Andric } 16075ffd83dbSDimitry Andric UsePath.pop_back(); 16085ffd83dbSDimitry Andric InUsePath.erase(FD); 16095ffd83dbSDimitry Andric } 16105ffd83dbSDimitry Andric 16115ffd83dbSDimitry Andric void checkRecordedDecl(Decl *D) { 16125ffd83dbSDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(D)) { 16135ffd83dbSDimitry Andric ShouldEmitRootNode = S.getEmissionStatus(FD, /*Final=*/true) == 16145ffd83dbSDimitry Andric Sema::FunctionEmissionStatus::Emitted; 16155ffd83dbSDimitry Andric checkFunc(SourceLocation(), FD); 16165ffd83dbSDimitry Andric } else 16175ffd83dbSDimitry Andric checkVar(cast<VarDecl>(D)); 16185ffd83dbSDimitry Andric } 16195ffd83dbSDimitry Andric 16205ffd83dbSDimitry Andric // Emit any deferred diagnostics for FD 16215ffd83dbSDimitry Andric void emitDeferredDiags(FunctionDecl *FD, bool ShowCallStack) { 16220b57cec5SDimitry Andric auto It = S.DeviceDeferredDiags.find(FD); 16230b57cec5SDimitry Andric if (It == S.DeviceDeferredDiags.end()) 16240b57cec5SDimitry Andric return; 16250b57cec5SDimitry Andric bool HasWarningOrError = false; 16265ffd83dbSDimitry Andric bool FirstDiag = true; 16270b57cec5SDimitry Andric for (PartialDiagnosticAt &PDAt : It->second) { 16285ffd83dbSDimitry Andric // Respect error limit. 16295ffd83dbSDimitry Andric if (S.Diags.hasFatalErrorOccurred()) 16305ffd83dbSDimitry Andric return; 16310b57cec5SDimitry Andric const SourceLocation &Loc = PDAt.first; 16320b57cec5SDimitry Andric const PartialDiagnostic &PD = PDAt.second; 16335ffd83dbSDimitry Andric HasWarningOrError |= 16345ffd83dbSDimitry Andric S.getDiagnostics().getDiagnosticLevel(PD.getDiagID(), Loc) >= 16355ffd83dbSDimitry Andric DiagnosticsEngine::Warning; 16365ffd83dbSDimitry Andric { 16370b57cec5SDimitry Andric DiagnosticBuilder Builder(S.Diags.Report(Loc, PD.getDiagID())); 16380b57cec5SDimitry Andric PD.Emit(Builder); 16390b57cec5SDimitry Andric } 16405ffd83dbSDimitry Andric // Emit the note on the first diagnostic in case too many diagnostics 16415ffd83dbSDimitry Andric // cause the note not emitted. 16425ffd83dbSDimitry Andric if (FirstDiag && HasWarningOrError && ShowCallStack) { 16430b57cec5SDimitry Andric emitCallStackNotes(S, FD); 16445ffd83dbSDimitry Andric FirstDiag = false; 16455ffd83dbSDimitry Andric } 16465ffd83dbSDimitry Andric } 16475ffd83dbSDimitry Andric } 16485ffd83dbSDimitry Andric }; 16495ffd83dbSDimitry Andric } // namespace 16505ffd83dbSDimitry Andric 16515ffd83dbSDimitry Andric void Sema::emitDeferredDiags() { 16525ffd83dbSDimitry Andric if (ExternalSource) 16535ffd83dbSDimitry Andric ExternalSource->ReadDeclsToCheckForDeferredDiags( 16545ffd83dbSDimitry Andric DeclsToCheckForDeferredDiags); 16555ffd83dbSDimitry Andric 16565ffd83dbSDimitry Andric if ((DeviceDeferredDiags.empty() && !LangOpts.OpenMP) || 16575ffd83dbSDimitry Andric DeclsToCheckForDeferredDiags.empty()) 16585ffd83dbSDimitry Andric return; 16595ffd83dbSDimitry Andric 16605ffd83dbSDimitry Andric DeferredDiagnosticsEmitter DDE(*this); 16615ffd83dbSDimitry Andric for (auto D : DeclsToCheckForDeferredDiags) 16625ffd83dbSDimitry Andric DDE.checkRecordedDecl(D); 16630b57cec5SDimitry Andric } 16640b57cec5SDimitry Andric 16650b57cec5SDimitry Andric // In CUDA, there are some constructs which may appear in semantically-valid 16660b57cec5SDimitry Andric // code, but trigger errors if we ever generate code for the function in which 16670b57cec5SDimitry Andric // they appear. Essentially every construct you're not allowed to use on the 16680b57cec5SDimitry Andric // device falls into this category, because you are allowed to use these 16690b57cec5SDimitry Andric // constructs in a __host__ __device__ function, but only if that function is 16700b57cec5SDimitry Andric // never codegen'ed on the device. 16710b57cec5SDimitry Andric // 16720b57cec5SDimitry Andric // To handle semantic checking for these constructs, we keep track of the set of 16730b57cec5SDimitry Andric // functions we know will be emitted, either because we could tell a priori that 16740b57cec5SDimitry Andric // they would be emitted, or because they were transitively called by a 16750b57cec5SDimitry Andric // known-emitted function. 16760b57cec5SDimitry Andric // 16770b57cec5SDimitry Andric // We also keep a partial call graph of which not-known-emitted functions call 16780b57cec5SDimitry Andric // which other not-known-emitted functions. 16790b57cec5SDimitry Andric // 16800b57cec5SDimitry Andric // When we see something which is illegal if the current function is emitted 16810b57cec5SDimitry Andric // (usually by way of CUDADiagIfDeviceCode, CUDADiagIfHostCode, or 16820b57cec5SDimitry Andric // CheckCUDACall), we first check if the current function is known-emitted. If 16830b57cec5SDimitry Andric // so, we immediately output the diagnostic. 16840b57cec5SDimitry Andric // 16850b57cec5SDimitry Andric // Otherwise, we "defer" the diagnostic. It sits in Sema::DeviceDeferredDiags 16860b57cec5SDimitry Andric // until we discover that the function is known-emitted, at which point we take 16870b57cec5SDimitry Andric // it out of this map and emit the diagnostic. 16880b57cec5SDimitry Andric 1689*e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder::SemaDiagnosticBuilder(Kind K, SourceLocation Loc, 1690*e8d8bef9SDimitry Andric unsigned DiagID, 1691*e8d8bef9SDimitry Andric FunctionDecl *Fn, Sema &S) 16920b57cec5SDimitry Andric : S(S), Loc(Loc), DiagID(DiagID), Fn(Fn), 16930b57cec5SDimitry Andric ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) { 16940b57cec5SDimitry Andric switch (K) { 16950b57cec5SDimitry Andric case K_Nop: 16960b57cec5SDimitry Andric break; 16970b57cec5SDimitry Andric case K_Immediate: 16980b57cec5SDimitry Andric case K_ImmediateWithCallStack: 1699*e8d8bef9SDimitry Andric ImmediateDiag.emplace( 1700*e8d8bef9SDimitry Andric ImmediateDiagBuilder(S.Diags.Report(Loc, DiagID), S, DiagID)); 17010b57cec5SDimitry Andric break; 17020b57cec5SDimitry Andric case K_Deferred: 17030b57cec5SDimitry Andric assert(Fn && "Must have a function to attach the deferred diag to."); 17040b57cec5SDimitry Andric auto &Diags = S.DeviceDeferredDiags[Fn]; 17050b57cec5SDimitry Andric PartialDiagId.emplace(Diags.size()); 17060b57cec5SDimitry Andric Diags.emplace_back(Loc, S.PDiag(DiagID)); 17070b57cec5SDimitry Andric break; 17080b57cec5SDimitry Andric } 17090b57cec5SDimitry Andric } 17100b57cec5SDimitry Andric 1711*e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder::SemaDiagnosticBuilder(SemaDiagnosticBuilder &&D) 17120b57cec5SDimitry Andric : S(D.S), Loc(D.Loc), DiagID(D.DiagID), Fn(D.Fn), 17130b57cec5SDimitry Andric ShowCallStack(D.ShowCallStack), ImmediateDiag(D.ImmediateDiag), 17140b57cec5SDimitry Andric PartialDiagId(D.PartialDiagId) { 17150b57cec5SDimitry Andric // Clean the previous diagnostics. 17160b57cec5SDimitry Andric D.ShowCallStack = false; 17170b57cec5SDimitry Andric D.ImmediateDiag.reset(); 17180b57cec5SDimitry Andric D.PartialDiagId.reset(); 17190b57cec5SDimitry Andric } 17200b57cec5SDimitry Andric 1721*e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { 17220b57cec5SDimitry Andric if (ImmediateDiag) { 17230b57cec5SDimitry Andric // Emit our diagnostic and, if it was a warning or error, output a callstack 17240b57cec5SDimitry Andric // if Fn isn't a priori known-emitted. 17250b57cec5SDimitry Andric bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel( 17260b57cec5SDimitry Andric DiagID, Loc) >= DiagnosticsEngine::Warning; 17270b57cec5SDimitry Andric ImmediateDiag.reset(); // Emit the immediate diag. 17280b57cec5SDimitry Andric if (IsWarningOrError && ShowCallStack) 17290b57cec5SDimitry Andric emitCallStackNotes(S, Fn); 17300b57cec5SDimitry Andric } else { 17310b57cec5SDimitry Andric assert((!PartialDiagId || ShowCallStack) && 17320b57cec5SDimitry Andric "Must always show call stack for deferred diags."); 17330b57cec5SDimitry Andric } 17340b57cec5SDimitry Andric } 17350b57cec5SDimitry Andric 1736*e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder Sema::targetDiag(SourceLocation Loc, 1737*e8d8bef9SDimitry Andric unsigned DiagID) { 1738a7dea167SDimitry Andric if (LangOpts.OpenMP) 1739a7dea167SDimitry Andric return LangOpts.OpenMPIsDevice ? diagIfOpenMPDeviceCode(Loc, DiagID) 1740a7dea167SDimitry Andric : diagIfOpenMPHostCode(Loc, DiagID); 17410b57cec5SDimitry Andric if (getLangOpts().CUDA) 17420b57cec5SDimitry Andric return getLangOpts().CUDAIsDevice ? CUDADiagIfDeviceCode(Loc, DiagID) 17430b57cec5SDimitry Andric : CUDADiagIfHostCode(Loc, DiagID); 17445ffd83dbSDimitry Andric 17455ffd83dbSDimitry Andric if (getLangOpts().SYCLIsDevice) 17465ffd83dbSDimitry Andric return SYCLDiagIfDeviceCode(Loc, DiagID); 17475ffd83dbSDimitry Andric 1748*e8d8bef9SDimitry Andric return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate, Loc, DiagID, 17490b57cec5SDimitry Andric getCurFunctionDecl(), *this); 17500b57cec5SDimitry Andric } 17510b57cec5SDimitry Andric 1752*e8d8bef9SDimitry Andric Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID, 1753*e8d8bef9SDimitry Andric bool DeferHint) { 1754*e8d8bef9SDimitry Andric bool IsError = Diags.getDiagnosticIDs()->isDefaultMappingAsError(DiagID); 1755*e8d8bef9SDimitry Andric bool ShouldDefer = getLangOpts().CUDA && LangOpts.GPUDeferDiag && 1756*e8d8bef9SDimitry Andric DiagnosticIDs::isDeferrable(DiagID) && 1757*e8d8bef9SDimitry Andric (DeferHint || !IsError); 1758*e8d8bef9SDimitry Andric auto SetIsLastErrorImmediate = [&](bool Flag) { 1759*e8d8bef9SDimitry Andric if (IsError) 1760*e8d8bef9SDimitry Andric IsLastErrorImmediate = Flag; 1761*e8d8bef9SDimitry Andric }; 1762*e8d8bef9SDimitry Andric if (!ShouldDefer) { 1763*e8d8bef9SDimitry Andric SetIsLastErrorImmediate(true); 1764*e8d8bef9SDimitry Andric return SemaDiagnosticBuilder(SemaDiagnosticBuilder::K_Immediate, Loc, 1765*e8d8bef9SDimitry Andric DiagID, getCurFunctionDecl(), *this); 1766*e8d8bef9SDimitry Andric } 1767*e8d8bef9SDimitry Andric 1768*e8d8bef9SDimitry Andric SemaDiagnosticBuilder DB = 1769*e8d8bef9SDimitry Andric getLangOpts().CUDAIsDevice 1770*e8d8bef9SDimitry Andric ? CUDADiagIfDeviceCode(Loc, DiagID) 1771*e8d8bef9SDimitry Andric : CUDADiagIfHostCode(Loc, DiagID); 1772*e8d8bef9SDimitry Andric SetIsLastErrorImmediate(DB.isImmediate()); 1773*e8d8bef9SDimitry Andric return DB; 1774*e8d8bef9SDimitry Andric } 1775*e8d8bef9SDimitry Andric 17765ffd83dbSDimitry Andric void Sema::checkDeviceDecl(const ValueDecl *D, SourceLocation Loc) { 17775ffd83dbSDimitry Andric if (isUnevaluatedContext()) 17785ffd83dbSDimitry Andric return; 17795ffd83dbSDimitry Andric 17805ffd83dbSDimitry Andric Decl *C = cast<Decl>(getCurLexicalContext()); 17815ffd83dbSDimitry Andric 17825ffd83dbSDimitry Andric // Memcpy operations for structs containing a member with unsupported type 17835ffd83dbSDimitry Andric // are ok, though. 17845ffd83dbSDimitry Andric if (const auto *MD = dyn_cast<CXXMethodDecl>(C)) { 17855ffd83dbSDimitry Andric if ((MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && 17865ffd83dbSDimitry Andric MD->isTrivial()) 17875ffd83dbSDimitry Andric return; 17885ffd83dbSDimitry Andric 17895ffd83dbSDimitry Andric if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(MD)) 17905ffd83dbSDimitry Andric if (Ctor->isCopyOrMoveConstructor() && Ctor->isTrivial()) 17915ffd83dbSDimitry Andric return; 17925ffd83dbSDimitry Andric } 17935ffd83dbSDimitry Andric 17945ffd83dbSDimitry Andric auto CheckType = [&](QualType Ty) { 17955ffd83dbSDimitry Andric if (Ty->isDependentType()) 17965ffd83dbSDimitry Andric return; 17975ffd83dbSDimitry Andric 1798*e8d8bef9SDimitry Andric if (Ty->isExtIntType()) { 1799*e8d8bef9SDimitry Andric if (!Context.getTargetInfo().hasExtIntType()) { 1800*e8d8bef9SDimitry Andric targetDiag(Loc, diag::err_device_unsupported_type) 1801*e8d8bef9SDimitry Andric << D << false /*show bit size*/ << 0 /*bitsize*/ 1802*e8d8bef9SDimitry Andric << Ty << Context.getTargetInfo().getTriple().str(); 1803*e8d8bef9SDimitry Andric } 1804*e8d8bef9SDimitry Andric return; 1805*e8d8bef9SDimitry Andric } 1806*e8d8bef9SDimitry Andric 18075ffd83dbSDimitry Andric if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) || 18085ffd83dbSDimitry Andric ((Ty->isFloat128Type() || 18095ffd83dbSDimitry Andric (Ty->isRealFloatingType() && Context.getTypeSize(Ty) == 128)) && 18105ffd83dbSDimitry Andric !Context.getTargetInfo().hasFloat128Type()) || 18115ffd83dbSDimitry Andric (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 && 18125ffd83dbSDimitry Andric !Context.getTargetInfo().hasInt128Type())) { 18135ffd83dbSDimitry Andric targetDiag(Loc, diag::err_device_unsupported_type) 1814*e8d8bef9SDimitry Andric << D << true /*show bit size*/ 1815*e8d8bef9SDimitry Andric << static_cast<unsigned>(Context.getTypeSize(Ty)) << Ty 18165ffd83dbSDimitry Andric << Context.getTargetInfo().getTriple().str(); 18175ffd83dbSDimitry Andric targetDiag(D->getLocation(), diag::note_defined_here) << D; 18185ffd83dbSDimitry Andric } 18195ffd83dbSDimitry Andric }; 18205ffd83dbSDimitry Andric 18215ffd83dbSDimitry Andric QualType Ty = D->getType(); 18225ffd83dbSDimitry Andric CheckType(Ty); 18235ffd83dbSDimitry Andric 18245ffd83dbSDimitry Andric if (const auto *FPTy = dyn_cast<FunctionProtoType>(Ty)) { 18255ffd83dbSDimitry Andric for (const auto &ParamTy : FPTy->param_types()) 18265ffd83dbSDimitry Andric CheckType(ParamTy); 18275ffd83dbSDimitry Andric CheckType(FPTy->getReturnType()); 18285ffd83dbSDimitry Andric } 18295ffd83dbSDimitry Andric } 18305ffd83dbSDimitry Andric 18310b57cec5SDimitry Andric /// Looks through the macro-expansion chain for the given 18320b57cec5SDimitry Andric /// location, looking for a macro expansion with the given name. 18330b57cec5SDimitry Andric /// If one is found, returns true and sets the location to that 18340b57cec5SDimitry Andric /// expansion loc. 18350b57cec5SDimitry Andric bool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) { 18360b57cec5SDimitry Andric SourceLocation loc = locref; 18370b57cec5SDimitry Andric if (!loc.isMacroID()) return false; 18380b57cec5SDimitry Andric 18390b57cec5SDimitry Andric // There's no good way right now to look at the intermediate 18400b57cec5SDimitry Andric // expansions, so just jump to the expansion location. 18410b57cec5SDimitry Andric loc = getSourceManager().getExpansionLoc(loc); 18420b57cec5SDimitry Andric 18430b57cec5SDimitry Andric // If that's written with the name, stop here. 1844*e8d8bef9SDimitry Andric SmallString<16> buffer; 18450b57cec5SDimitry Andric if (getPreprocessor().getSpelling(loc, buffer) == name) { 18460b57cec5SDimitry Andric locref = loc; 18470b57cec5SDimitry Andric return true; 18480b57cec5SDimitry Andric } 18490b57cec5SDimitry Andric return false; 18500b57cec5SDimitry Andric } 18510b57cec5SDimitry Andric 18520b57cec5SDimitry Andric /// Determines the active Scope associated with the given declaration 18530b57cec5SDimitry Andric /// context. 18540b57cec5SDimitry Andric /// 18550b57cec5SDimitry Andric /// This routine maps a declaration context to the active Scope object that 18560b57cec5SDimitry Andric /// represents that declaration context in the parser. It is typically used 18570b57cec5SDimitry Andric /// from "scope-less" code (e.g., template instantiation, lazy creation of 18580b57cec5SDimitry Andric /// declarations) that injects a name for name-lookup purposes and, therefore, 18590b57cec5SDimitry Andric /// must update the Scope. 18600b57cec5SDimitry Andric /// 18610b57cec5SDimitry Andric /// \returns The scope corresponding to the given declaraion context, or NULL 18620b57cec5SDimitry Andric /// if no such scope is open. 18630b57cec5SDimitry Andric Scope *Sema::getScopeForContext(DeclContext *Ctx) { 18640b57cec5SDimitry Andric 18650b57cec5SDimitry Andric if (!Ctx) 18660b57cec5SDimitry Andric return nullptr; 18670b57cec5SDimitry Andric 18680b57cec5SDimitry Andric Ctx = Ctx->getPrimaryContext(); 18690b57cec5SDimitry Andric for (Scope *S = getCurScope(); S; S = S->getParent()) { 18700b57cec5SDimitry Andric // Ignore scopes that cannot have declarations. This is important for 18710b57cec5SDimitry Andric // out-of-line definitions of static class members. 18720b57cec5SDimitry Andric if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) 18730b57cec5SDimitry Andric if (DeclContext *Entity = S->getEntity()) 18740b57cec5SDimitry Andric if (Ctx == Entity->getPrimaryContext()) 18750b57cec5SDimitry Andric return S; 18760b57cec5SDimitry Andric } 18770b57cec5SDimitry Andric 18780b57cec5SDimitry Andric return nullptr; 18790b57cec5SDimitry Andric } 18800b57cec5SDimitry Andric 18810b57cec5SDimitry Andric /// Enter a new function scope 18820b57cec5SDimitry Andric void Sema::PushFunctionScope() { 18830b57cec5SDimitry Andric if (FunctionScopes.empty() && CachedFunctionScope) { 18840b57cec5SDimitry Andric // Use CachedFunctionScope to avoid allocating memory when possible. 18850b57cec5SDimitry Andric CachedFunctionScope->Clear(); 18860b57cec5SDimitry Andric FunctionScopes.push_back(CachedFunctionScope.release()); 18870b57cec5SDimitry Andric } else { 18880b57cec5SDimitry Andric FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics())); 18890b57cec5SDimitry Andric } 18900b57cec5SDimitry Andric if (LangOpts.OpenMP) 18910b57cec5SDimitry Andric pushOpenMPFunctionRegion(); 18920b57cec5SDimitry Andric } 18930b57cec5SDimitry Andric 18940b57cec5SDimitry Andric void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { 18950b57cec5SDimitry Andric FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(), 18960b57cec5SDimitry Andric BlockScope, Block)); 18970b57cec5SDimitry Andric } 18980b57cec5SDimitry Andric 18990b57cec5SDimitry Andric LambdaScopeInfo *Sema::PushLambdaScope() { 19000b57cec5SDimitry Andric LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics()); 19010b57cec5SDimitry Andric FunctionScopes.push_back(LSI); 19020b57cec5SDimitry Andric return LSI; 19030b57cec5SDimitry Andric } 19040b57cec5SDimitry Andric 19050b57cec5SDimitry Andric void Sema::RecordParsingTemplateParameterDepth(unsigned Depth) { 19060b57cec5SDimitry Andric if (LambdaScopeInfo *const LSI = getCurLambda()) { 19070b57cec5SDimitry Andric LSI->AutoTemplateParameterDepth = Depth; 19080b57cec5SDimitry Andric return; 19090b57cec5SDimitry Andric } 19100b57cec5SDimitry Andric llvm_unreachable( 19110b57cec5SDimitry Andric "Remove assertion if intentionally called in a non-lambda context."); 19120b57cec5SDimitry Andric } 19130b57cec5SDimitry Andric 19140b57cec5SDimitry Andric // Check that the type of the VarDecl has an accessible copy constructor and 19150b57cec5SDimitry Andric // resolve its destructor's exception specification. 19160b57cec5SDimitry Andric static void checkEscapingByref(VarDecl *VD, Sema &S) { 19170b57cec5SDimitry Andric QualType T = VD->getType(); 19180b57cec5SDimitry Andric EnterExpressionEvaluationContext scope( 19190b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); 19200b57cec5SDimitry Andric SourceLocation Loc = VD->getLocation(); 19210b57cec5SDimitry Andric Expr *VarRef = 19220b57cec5SDimitry Andric new (S.Context) DeclRefExpr(S.Context, VD, false, T, VK_LValue, Loc); 19230b57cec5SDimitry Andric ExprResult Result = S.PerformMoveOrCopyInitialization( 19240b57cec5SDimitry Andric InitializedEntity::InitializeBlock(Loc, T, false), VD, VD->getType(), 19250b57cec5SDimitry Andric VarRef, /*AllowNRVO=*/true); 19260b57cec5SDimitry Andric if (!Result.isInvalid()) { 19270b57cec5SDimitry Andric Result = S.MaybeCreateExprWithCleanups(Result); 19280b57cec5SDimitry Andric Expr *Init = Result.getAs<Expr>(); 19290b57cec5SDimitry Andric S.Context.setBlockVarCopyInit(VD, Init, S.canThrow(Init)); 19300b57cec5SDimitry Andric } 19310b57cec5SDimitry Andric 19320b57cec5SDimitry Andric // The destructor's exception specification is needed when IRGen generates 19330b57cec5SDimitry Andric // block copy/destroy functions. Resolve it here. 19340b57cec5SDimitry Andric if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) 19350b57cec5SDimitry Andric if (CXXDestructorDecl *DD = RD->getDestructor()) { 19360b57cec5SDimitry Andric auto *FPT = DD->getType()->getAs<FunctionProtoType>(); 19370b57cec5SDimitry Andric S.ResolveExceptionSpec(Loc, FPT); 19380b57cec5SDimitry Andric } 19390b57cec5SDimitry Andric } 19400b57cec5SDimitry Andric 19410b57cec5SDimitry Andric static void markEscapingByrefs(const FunctionScopeInfo &FSI, Sema &S) { 19420b57cec5SDimitry Andric // Set the EscapingByref flag of __block variables captured by 19430b57cec5SDimitry Andric // escaping blocks. 19440b57cec5SDimitry Andric for (const BlockDecl *BD : FSI.Blocks) { 19450b57cec5SDimitry Andric for (const BlockDecl::Capture &BC : BD->captures()) { 19460b57cec5SDimitry Andric VarDecl *VD = BC.getVariable(); 19470b57cec5SDimitry Andric if (VD->hasAttr<BlocksAttr>()) { 19480b57cec5SDimitry Andric // Nothing to do if this is a __block variable captured by a 19490b57cec5SDimitry Andric // non-escaping block. 19500b57cec5SDimitry Andric if (BD->doesNotEscape()) 19510b57cec5SDimitry Andric continue; 19520b57cec5SDimitry Andric VD->setEscapingByref(); 19530b57cec5SDimitry Andric } 19540b57cec5SDimitry Andric // Check whether the captured variable is or contains an object of 19550b57cec5SDimitry Andric // non-trivial C union type. 19560b57cec5SDimitry Andric QualType CapType = BC.getVariable()->getType(); 19570b57cec5SDimitry Andric if (CapType.hasNonTrivialToPrimitiveDestructCUnion() || 19580b57cec5SDimitry Andric CapType.hasNonTrivialToPrimitiveCopyCUnion()) 19590b57cec5SDimitry Andric S.checkNonTrivialCUnion(BC.getVariable()->getType(), 19600b57cec5SDimitry Andric BD->getCaretLocation(), 19610b57cec5SDimitry Andric Sema::NTCUC_BlockCapture, 19620b57cec5SDimitry Andric Sema::NTCUK_Destruct|Sema::NTCUK_Copy); 19630b57cec5SDimitry Andric } 19640b57cec5SDimitry Andric } 19650b57cec5SDimitry Andric 19660b57cec5SDimitry Andric for (VarDecl *VD : FSI.ByrefBlockVars) { 19670b57cec5SDimitry Andric // __block variables might require us to capture a copy-initializer. 19680b57cec5SDimitry Andric if (!VD->isEscapingByref()) 19690b57cec5SDimitry Andric continue; 19700b57cec5SDimitry Andric // It's currently invalid to ever have a __block variable with an 19710b57cec5SDimitry Andric // array type; should we diagnose that here? 19720b57cec5SDimitry Andric // Regardless, we don't want to ignore array nesting when 19730b57cec5SDimitry Andric // constructing this copy. 19740b57cec5SDimitry Andric if (VD->getType()->isStructureOrClassType()) 19750b57cec5SDimitry Andric checkEscapingByref(VD, S); 19760b57cec5SDimitry Andric } 19770b57cec5SDimitry Andric } 19780b57cec5SDimitry Andric 19790b57cec5SDimitry Andric /// Pop a function (or block or lambda or captured region) scope from the stack. 19800b57cec5SDimitry Andric /// 19810b57cec5SDimitry Andric /// \param WP The warning policy to use for CFG-based warnings, or null if such 19820b57cec5SDimitry Andric /// warnings should not be produced. 19830b57cec5SDimitry Andric /// \param D The declaration corresponding to this function scope, if producing 19840b57cec5SDimitry Andric /// CFG-based warnings. 19850b57cec5SDimitry Andric /// \param BlockType The type of the block expression, if D is a BlockDecl. 19860b57cec5SDimitry Andric Sema::PoppedFunctionScopePtr 19870b57cec5SDimitry Andric Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP, 19880b57cec5SDimitry Andric const Decl *D, QualType BlockType) { 19890b57cec5SDimitry Andric assert(!FunctionScopes.empty() && "mismatched push/pop!"); 19900b57cec5SDimitry Andric 19910b57cec5SDimitry Andric markEscapingByrefs(*FunctionScopes.back(), *this); 19920b57cec5SDimitry Andric 19930b57cec5SDimitry Andric PoppedFunctionScopePtr Scope(FunctionScopes.pop_back_val(), 19940b57cec5SDimitry Andric PoppedFunctionScopeDeleter(this)); 19950b57cec5SDimitry Andric 19960b57cec5SDimitry Andric if (LangOpts.OpenMP) 19970b57cec5SDimitry Andric popOpenMPFunctionRegion(Scope.get()); 19980b57cec5SDimitry Andric 19990b57cec5SDimitry Andric // Issue any analysis-based warnings. 20000b57cec5SDimitry Andric if (WP && D) 20010b57cec5SDimitry Andric AnalysisWarnings.IssueWarnings(*WP, Scope.get(), D, BlockType); 20020b57cec5SDimitry Andric else 20030b57cec5SDimitry Andric for (const auto &PUD : Scope->PossiblyUnreachableDiags) 20040b57cec5SDimitry Andric Diag(PUD.Loc, PUD.PD); 20050b57cec5SDimitry Andric 20060b57cec5SDimitry Andric return Scope; 20070b57cec5SDimitry Andric } 20080b57cec5SDimitry Andric 20090b57cec5SDimitry Andric void Sema::PoppedFunctionScopeDeleter:: 20100b57cec5SDimitry Andric operator()(sema::FunctionScopeInfo *Scope) const { 20110b57cec5SDimitry Andric // Stash the function scope for later reuse if it's for a normal function. 20120b57cec5SDimitry Andric if (Scope->isPlainFunction() && !Self->CachedFunctionScope) 20130b57cec5SDimitry Andric Self->CachedFunctionScope.reset(Scope); 20140b57cec5SDimitry Andric else 20150b57cec5SDimitry Andric delete Scope; 20160b57cec5SDimitry Andric } 20170b57cec5SDimitry Andric 20180b57cec5SDimitry Andric void Sema::PushCompoundScope(bool IsStmtExpr) { 20190b57cec5SDimitry Andric getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo(IsStmtExpr)); 20200b57cec5SDimitry Andric } 20210b57cec5SDimitry Andric 20220b57cec5SDimitry Andric void Sema::PopCompoundScope() { 20230b57cec5SDimitry Andric FunctionScopeInfo *CurFunction = getCurFunction(); 20240b57cec5SDimitry Andric assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop"); 20250b57cec5SDimitry Andric 20260b57cec5SDimitry Andric CurFunction->CompoundScopes.pop_back(); 20270b57cec5SDimitry Andric } 20280b57cec5SDimitry Andric 20290b57cec5SDimitry Andric /// Determine whether any errors occurred within this function/method/ 20300b57cec5SDimitry Andric /// block. 20310b57cec5SDimitry Andric bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const { 20325ffd83dbSDimitry Andric return getCurFunction()->hasUnrecoverableErrorOccurred(); 20330b57cec5SDimitry Andric } 20340b57cec5SDimitry Andric 20350b57cec5SDimitry Andric void Sema::setFunctionHasBranchIntoScope() { 20360b57cec5SDimitry Andric if (!FunctionScopes.empty()) 20370b57cec5SDimitry Andric FunctionScopes.back()->setHasBranchIntoScope(); 20380b57cec5SDimitry Andric } 20390b57cec5SDimitry Andric 20400b57cec5SDimitry Andric void Sema::setFunctionHasBranchProtectedScope() { 20410b57cec5SDimitry Andric if (!FunctionScopes.empty()) 20420b57cec5SDimitry Andric FunctionScopes.back()->setHasBranchProtectedScope(); 20430b57cec5SDimitry Andric } 20440b57cec5SDimitry Andric 20450b57cec5SDimitry Andric void Sema::setFunctionHasIndirectGoto() { 20460b57cec5SDimitry Andric if (!FunctionScopes.empty()) 20470b57cec5SDimitry Andric FunctionScopes.back()->setHasIndirectGoto(); 20480b57cec5SDimitry Andric } 20490b57cec5SDimitry Andric 20500b57cec5SDimitry Andric BlockScopeInfo *Sema::getCurBlock() { 20510b57cec5SDimitry Andric if (FunctionScopes.empty()) 20520b57cec5SDimitry Andric return nullptr; 20530b57cec5SDimitry Andric 20540b57cec5SDimitry Andric auto CurBSI = dyn_cast<BlockScopeInfo>(FunctionScopes.back()); 20550b57cec5SDimitry Andric if (CurBSI && CurBSI->TheDecl && 20560b57cec5SDimitry Andric !CurBSI->TheDecl->Encloses(CurContext)) { 20570b57cec5SDimitry Andric // We have switched contexts due to template instantiation. 20580b57cec5SDimitry Andric assert(!CodeSynthesisContexts.empty()); 20590b57cec5SDimitry Andric return nullptr; 20600b57cec5SDimitry Andric } 20610b57cec5SDimitry Andric 20620b57cec5SDimitry Andric return CurBSI; 20630b57cec5SDimitry Andric } 20640b57cec5SDimitry Andric 20650b57cec5SDimitry Andric FunctionScopeInfo *Sema::getEnclosingFunction() const { 20660b57cec5SDimitry Andric if (FunctionScopes.empty()) 20670b57cec5SDimitry Andric return nullptr; 20680b57cec5SDimitry Andric 20690b57cec5SDimitry Andric for (int e = FunctionScopes.size() - 1; e >= 0; --e) { 20700b57cec5SDimitry Andric if (isa<sema::BlockScopeInfo>(FunctionScopes[e])) 20710b57cec5SDimitry Andric continue; 20720b57cec5SDimitry Andric return FunctionScopes[e]; 20730b57cec5SDimitry Andric } 20740b57cec5SDimitry Andric return nullptr; 20750b57cec5SDimitry Andric } 20760b57cec5SDimitry Andric 2077a7dea167SDimitry Andric LambdaScopeInfo *Sema::getEnclosingLambda() const { 2078a7dea167SDimitry Andric for (auto *Scope : llvm::reverse(FunctionScopes)) { 2079a7dea167SDimitry Andric if (auto *LSI = dyn_cast<sema::LambdaScopeInfo>(Scope)) { 2080a7dea167SDimitry Andric if (LSI->Lambda && !LSI->Lambda->Encloses(CurContext)) { 2081a7dea167SDimitry Andric // We have switched contexts due to template instantiation. 2082a7dea167SDimitry Andric // FIXME: We should swap out the FunctionScopes during code synthesis 2083a7dea167SDimitry Andric // so that we don't need to check for this. 2084a7dea167SDimitry Andric assert(!CodeSynthesisContexts.empty()); 2085a7dea167SDimitry Andric return nullptr; 2086a7dea167SDimitry Andric } 2087a7dea167SDimitry Andric return LSI; 2088a7dea167SDimitry Andric } 2089a7dea167SDimitry Andric } 2090a7dea167SDimitry Andric return nullptr; 2091a7dea167SDimitry Andric } 2092a7dea167SDimitry Andric 20930b57cec5SDimitry Andric LambdaScopeInfo *Sema::getCurLambda(bool IgnoreNonLambdaCapturingScope) { 20940b57cec5SDimitry Andric if (FunctionScopes.empty()) 20950b57cec5SDimitry Andric return nullptr; 20960b57cec5SDimitry Andric 20970b57cec5SDimitry Andric auto I = FunctionScopes.rbegin(); 20980b57cec5SDimitry Andric if (IgnoreNonLambdaCapturingScope) { 20990b57cec5SDimitry Andric auto E = FunctionScopes.rend(); 21000b57cec5SDimitry Andric while (I != E && isa<CapturingScopeInfo>(*I) && !isa<LambdaScopeInfo>(*I)) 21010b57cec5SDimitry Andric ++I; 21020b57cec5SDimitry Andric if (I == E) 21030b57cec5SDimitry Andric return nullptr; 21040b57cec5SDimitry Andric } 21050b57cec5SDimitry Andric auto *CurLSI = dyn_cast<LambdaScopeInfo>(*I); 21060b57cec5SDimitry Andric if (CurLSI && CurLSI->Lambda && 21070b57cec5SDimitry Andric !CurLSI->Lambda->Encloses(CurContext)) { 21080b57cec5SDimitry Andric // We have switched contexts due to template instantiation. 21090b57cec5SDimitry Andric assert(!CodeSynthesisContexts.empty()); 21100b57cec5SDimitry Andric return nullptr; 21110b57cec5SDimitry Andric } 21120b57cec5SDimitry Andric 21130b57cec5SDimitry Andric return CurLSI; 21140b57cec5SDimitry Andric } 2115a7dea167SDimitry Andric 21160b57cec5SDimitry Andric // We have a generic lambda if we parsed auto parameters, or we have 21170b57cec5SDimitry Andric // an associated template parameter list. 21180b57cec5SDimitry Andric LambdaScopeInfo *Sema::getCurGenericLambda() { 21190b57cec5SDimitry Andric if (LambdaScopeInfo *LSI = getCurLambda()) { 21200b57cec5SDimitry Andric return (LSI->TemplateParams.size() || 21210b57cec5SDimitry Andric LSI->GLTemplateParameterList) ? LSI : nullptr; 21220b57cec5SDimitry Andric } 21230b57cec5SDimitry Andric return nullptr; 21240b57cec5SDimitry Andric } 21250b57cec5SDimitry Andric 21260b57cec5SDimitry Andric 21270b57cec5SDimitry Andric void Sema::ActOnComment(SourceRange Comment) { 21280b57cec5SDimitry Andric if (!LangOpts.RetainCommentsFromSystemHeaders && 21290b57cec5SDimitry Andric SourceMgr.isInSystemHeader(Comment.getBegin())) 21300b57cec5SDimitry Andric return; 21310b57cec5SDimitry Andric RawComment RC(SourceMgr, Comment, LangOpts.CommentOpts, false); 21320b57cec5SDimitry Andric if (RC.isAlmostTrailingComment()) { 21330b57cec5SDimitry Andric SourceRange MagicMarkerRange(Comment.getBegin(), 21340b57cec5SDimitry Andric Comment.getBegin().getLocWithOffset(3)); 21350b57cec5SDimitry Andric StringRef MagicMarkerText; 21360b57cec5SDimitry Andric switch (RC.getKind()) { 21370b57cec5SDimitry Andric case RawComment::RCK_OrdinaryBCPL: 21380b57cec5SDimitry Andric MagicMarkerText = "///<"; 21390b57cec5SDimitry Andric break; 21400b57cec5SDimitry Andric case RawComment::RCK_OrdinaryC: 21410b57cec5SDimitry Andric MagicMarkerText = "/**<"; 21420b57cec5SDimitry Andric break; 21430b57cec5SDimitry Andric default: 21440b57cec5SDimitry Andric llvm_unreachable("if this is an almost Doxygen comment, " 21450b57cec5SDimitry Andric "it should be ordinary"); 21460b57cec5SDimitry Andric } 21470b57cec5SDimitry Andric Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) << 21480b57cec5SDimitry Andric FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText); 21490b57cec5SDimitry Andric } 21500b57cec5SDimitry Andric Context.addComment(RC); 21510b57cec5SDimitry Andric } 21520b57cec5SDimitry Andric 21530b57cec5SDimitry Andric // Pin this vtable to this file. 21540b57cec5SDimitry Andric ExternalSemaSource::~ExternalSemaSource() {} 2155480093f4SDimitry Andric char ExternalSemaSource::ID; 21560b57cec5SDimitry Andric 21570b57cec5SDimitry Andric void ExternalSemaSource::ReadMethodPool(Selector Sel) { } 21580b57cec5SDimitry Andric void ExternalSemaSource::updateOutOfDateSelector(Selector Sel) { } 21590b57cec5SDimitry Andric 21600b57cec5SDimitry Andric void ExternalSemaSource::ReadKnownNamespaces( 21610b57cec5SDimitry Andric SmallVectorImpl<NamespaceDecl *> &Namespaces) { 21620b57cec5SDimitry Andric } 21630b57cec5SDimitry Andric 21640b57cec5SDimitry Andric void ExternalSemaSource::ReadUndefinedButUsed( 21650b57cec5SDimitry Andric llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {} 21660b57cec5SDimitry Andric 21670b57cec5SDimitry Andric void ExternalSemaSource::ReadMismatchingDeleteExpressions(llvm::MapVector< 21680b57cec5SDimitry Andric FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &) {} 21690b57cec5SDimitry Andric 21700b57cec5SDimitry Andric /// Figure out if an expression could be turned into a call. 21710b57cec5SDimitry Andric /// 21720b57cec5SDimitry Andric /// Use this when trying to recover from an error where the programmer may have 21730b57cec5SDimitry Andric /// written just the name of a function instead of actually calling it. 21740b57cec5SDimitry Andric /// 21750b57cec5SDimitry Andric /// \param E - The expression to examine. 21760b57cec5SDimitry Andric /// \param ZeroArgCallReturnTy - If the expression can be turned into a call 21770b57cec5SDimitry Andric /// with no arguments, this parameter is set to the type returned by such a 21780b57cec5SDimitry Andric /// call; otherwise, it is set to an empty QualType. 21790b57cec5SDimitry Andric /// \param OverloadSet - If the expression is an overloaded function 21800b57cec5SDimitry Andric /// name, this parameter is populated with the decls of the various overloads. 21810b57cec5SDimitry Andric bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy, 21820b57cec5SDimitry Andric UnresolvedSetImpl &OverloadSet) { 21830b57cec5SDimitry Andric ZeroArgCallReturnTy = QualType(); 21840b57cec5SDimitry Andric OverloadSet.clear(); 21850b57cec5SDimitry Andric 21860b57cec5SDimitry Andric const OverloadExpr *Overloads = nullptr; 21870b57cec5SDimitry Andric bool IsMemExpr = false; 21880b57cec5SDimitry Andric if (E.getType() == Context.OverloadTy) { 21890b57cec5SDimitry Andric OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E)); 21900b57cec5SDimitry Andric 21910b57cec5SDimitry Andric // Ignore overloads that are pointer-to-member constants. 21920b57cec5SDimitry Andric if (FR.HasFormOfMemberPointer) 21930b57cec5SDimitry Andric return false; 21940b57cec5SDimitry Andric 21950b57cec5SDimitry Andric Overloads = FR.Expression; 21960b57cec5SDimitry Andric } else if (E.getType() == Context.BoundMemberTy) { 21970b57cec5SDimitry Andric Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens()); 21980b57cec5SDimitry Andric IsMemExpr = true; 21990b57cec5SDimitry Andric } 22000b57cec5SDimitry Andric 22010b57cec5SDimitry Andric bool Ambiguous = false; 22020b57cec5SDimitry Andric bool IsMV = false; 22030b57cec5SDimitry Andric 22040b57cec5SDimitry Andric if (Overloads) { 22050b57cec5SDimitry Andric for (OverloadExpr::decls_iterator it = Overloads->decls_begin(), 22060b57cec5SDimitry Andric DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) { 22070b57cec5SDimitry Andric OverloadSet.addDecl(*it); 22080b57cec5SDimitry Andric 22090b57cec5SDimitry Andric // Check whether the function is a non-template, non-member which takes no 22100b57cec5SDimitry Andric // arguments. 22110b57cec5SDimitry Andric if (IsMemExpr) 22120b57cec5SDimitry Andric continue; 22130b57cec5SDimitry Andric if (const FunctionDecl *OverloadDecl 22140b57cec5SDimitry Andric = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) { 22150b57cec5SDimitry Andric if (OverloadDecl->getMinRequiredArguments() == 0) { 22160b57cec5SDimitry Andric if (!ZeroArgCallReturnTy.isNull() && !Ambiguous && 22170b57cec5SDimitry Andric (!IsMV || !(OverloadDecl->isCPUDispatchMultiVersion() || 22180b57cec5SDimitry Andric OverloadDecl->isCPUSpecificMultiVersion()))) { 22190b57cec5SDimitry Andric ZeroArgCallReturnTy = QualType(); 22200b57cec5SDimitry Andric Ambiguous = true; 22210b57cec5SDimitry Andric } else { 22220b57cec5SDimitry Andric ZeroArgCallReturnTy = OverloadDecl->getReturnType(); 22230b57cec5SDimitry Andric IsMV = OverloadDecl->isCPUDispatchMultiVersion() || 22240b57cec5SDimitry Andric OverloadDecl->isCPUSpecificMultiVersion(); 22250b57cec5SDimitry Andric } 22260b57cec5SDimitry Andric } 22270b57cec5SDimitry Andric } 22280b57cec5SDimitry Andric } 22290b57cec5SDimitry Andric 22300b57cec5SDimitry Andric // If it's not a member, use better machinery to try to resolve the call 22310b57cec5SDimitry Andric if (!IsMemExpr) 22320b57cec5SDimitry Andric return !ZeroArgCallReturnTy.isNull(); 22330b57cec5SDimitry Andric } 22340b57cec5SDimitry Andric 22350b57cec5SDimitry Andric // Attempt to call the member with no arguments - this will correctly handle 22360b57cec5SDimitry Andric // member templates with defaults/deduction of template arguments, overloads 22370b57cec5SDimitry Andric // with default arguments, etc. 22380b57cec5SDimitry Andric if (IsMemExpr && !E.isTypeDependent()) { 2239a7dea167SDimitry Andric Sema::TentativeAnalysisScope Trap(*this); 22400b57cec5SDimitry Andric ExprResult R = BuildCallToMemberFunction(nullptr, &E, SourceLocation(), 22410b57cec5SDimitry Andric None, SourceLocation()); 22420b57cec5SDimitry Andric if (R.isUsable()) { 22430b57cec5SDimitry Andric ZeroArgCallReturnTy = R.get()->getType(); 22440b57cec5SDimitry Andric return true; 22450b57cec5SDimitry Andric } 22460b57cec5SDimitry Andric return false; 22470b57cec5SDimitry Andric } 22480b57cec5SDimitry Andric 22490b57cec5SDimitry Andric if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) { 22500b57cec5SDimitry Andric if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { 22510b57cec5SDimitry Andric if (Fun->getMinRequiredArguments() == 0) 22520b57cec5SDimitry Andric ZeroArgCallReturnTy = Fun->getReturnType(); 22530b57cec5SDimitry Andric return true; 22540b57cec5SDimitry Andric } 22550b57cec5SDimitry Andric } 22560b57cec5SDimitry Andric 22570b57cec5SDimitry Andric // We don't have an expression that's convenient to get a FunctionDecl from, 22580b57cec5SDimitry Andric // but we can at least check if the type is "function of 0 arguments". 22590b57cec5SDimitry Andric QualType ExprTy = E.getType(); 22600b57cec5SDimitry Andric const FunctionType *FunTy = nullptr; 22610b57cec5SDimitry Andric QualType PointeeTy = ExprTy->getPointeeType(); 22620b57cec5SDimitry Andric if (!PointeeTy.isNull()) 22630b57cec5SDimitry Andric FunTy = PointeeTy->getAs<FunctionType>(); 22640b57cec5SDimitry Andric if (!FunTy) 22650b57cec5SDimitry Andric FunTy = ExprTy->getAs<FunctionType>(); 22660b57cec5SDimitry Andric 22670b57cec5SDimitry Andric if (const FunctionProtoType *FPT = 22680b57cec5SDimitry Andric dyn_cast_or_null<FunctionProtoType>(FunTy)) { 22690b57cec5SDimitry Andric if (FPT->getNumParams() == 0) 22700b57cec5SDimitry Andric ZeroArgCallReturnTy = FunTy->getReturnType(); 22710b57cec5SDimitry Andric return true; 22720b57cec5SDimitry Andric } 22730b57cec5SDimitry Andric return false; 22740b57cec5SDimitry Andric } 22750b57cec5SDimitry Andric 22760b57cec5SDimitry Andric /// Give notes for a set of overloads. 22770b57cec5SDimitry Andric /// 22780b57cec5SDimitry Andric /// A companion to tryExprAsCall. In cases when the name that the programmer 22790b57cec5SDimitry Andric /// wrote was an overloaded function, we may be able to make some guesses about 22800b57cec5SDimitry Andric /// plausible overloads based on their return types; such guesses can be handed 22810b57cec5SDimitry Andric /// off to this method to be emitted as notes. 22820b57cec5SDimitry Andric /// 22830b57cec5SDimitry Andric /// \param Overloads - The overloads to note. 22840b57cec5SDimitry Andric /// \param FinalNoteLoc - If we've suppressed printing some overloads due to 22850b57cec5SDimitry Andric /// -fshow-overloads=best, this is the location to attach to the note about too 22860b57cec5SDimitry Andric /// many candidates. Typically this will be the location of the original 22870b57cec5SDimitry Andric /// ill-formed expression. 22880b57cec5SDimitry Andric static void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads, 22890b57cec5SDimitry Andric const SourceLocation FinalNoteLoc) { 22900b57cec5SDimitry Andric int ShownOverloads = 0; 22910b57cec5SDimitry Andric int SuppressedOverloads = 0; 22920b57cec5SDimitry Andric for (UnresolvedSetImpl::iterator It = Overloads.begin(), 22930b57cec5SDimitry Andric DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { 22940b57cec5SDimitry Andric // FIXME: Magic number for max shown overloads stolen from 22950b57cec5SDimitry Andric // OverloadCandidateSet::NoteCandidates. 22960b57cec5SDimitry Andric if (ShownOverloads >= 4 && S.Diags.getShowOverloads() == Ovl_Best) { 22970b57cec5SDimitry Andric ++SuppressedOverloads; 22980b57cec5SDimitry Andric continue; 22990b57cec5SDimitry Andric } 23000b57cec5SDimitry Andric 23010b57cec5SDimitry Andric NamedDecl *Fn = (*It)->getUnderlyingDecl(); 23020b57cec5SDimitry Andric // Don't print overloads for non-default multiversioned functions. 23030b57cec5SDimitry Andric if (const auto *FD = Fn->getAsFunction()) { 23040b57cec5SDimitry Andric if (FD->isMultiVersion() && FD->hasAttr<TargetAttr>() && 23050b57cec5SDimitry Andric !FD->getAttr<TargetAttr>()->isDefaultVersion()) 23060b57cec5SDimitry Andric continue; 23070b57cec5SDimitry Andric } 23080b57cec5SDimitry Andric S.Diag(Fn->getLocation(), diag::note_possible_target_of_call); 23090b57cec5SDimitry Andric ++ShownOverloads; 23100b57cec5SDimitry Andric } 23110b57cec5SDimitry Andric 23120b57cec5SDimitry Andric if (SuppressedOverloads) 23130b57cec5SDimitry Andric S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates) 23140b57cec5SDimitry Andric << SuppressedOverloads; 23150b57cec5SDimitry Andric } 23160b57cec5SDimitry Andric 23170b57cec5SDimitry Andric static void notePlausibleOverloads(Sema &S, SourceLocation Loc, 23180b57cec5SDimitry Andric const UnresolvedSetImpl &Overloads, 23190b57cec5SDimitry Andric bool (*IsPlausibleResult)(QualType)) { 23200b57cec5SDimitry Andric if (!IsPlausibleResult) 23210b57cec5SDimitry Andric return noteOverloads(S, Overloads, Loc); 23220b57cec5SDimitry Andric 23230b57cec5SDimitry Andric UnresolvedSet<2> PlausibleOverloads; 23240b57cec5SDimitry Andric for (OverloadExpr::decls_iterator It = Overloads.begin(), 23250b57cec5SDimitry Andric DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { 23260b57cec5SDimitry Andric const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It); 23270b57cec5SDimitry Andric QualType OverloadResultTy = OverloadDecl->getReturnType(); 23280b57cec5SDimitry Andric if (IsPlausibleResult(OverloadResultTy)) 23290b57cec5SDimitry Andric PlausibleOverloads.addDecl(It.getDecl()); 23300b57cec5SDimitry Andric } 23310b57cec5SDimitry Andric noteOverloads(S, PlausibleOverloads, Loc); 23320b57cec5SDimitry Andric } 23330b57cec5SDimitry Andric 23340b57cec5SDimitry Andric /// Determine whether the given expression can be called by just 23350b57cec5SDimitry Andric /// putting parentheses after it. Notably, expressions with unary 23360b57cec5SDimitry Andric /// operators can't be because the unary operator will start parsing 23370b57cec5SDimitry Andric /// outside the call. 23380b57cec5SDimitry Andric static bool IsCallableWithAppend(Expr *E) { 23390b57cec5SDimitry Andric E = E->IgnoreImplicit(); 23400b57cec5SDimitry Andric return (!isa<CStyleCastExpr>(E) && 23410b57cec5SDimitry Andric !isa<UnaryOperator>(E) && 23420b57cec5SDimitry Andric !isa<BinaryOperator>(E) && 23430b57cec5SDimitry Andric !isa<CXXOperatorCallExpr>(E)); 23440b57cec5SDimitry Andric } 23450b57cec5SDimitry Andric 23460b57cec5SDimitry Andric static bool IsCPUDispatchCPUSpecificMultiVersion(const Expr *E) { 23470b57cec5SDimitry Andric if (const auto *UO = dyn_cast<UnaryOperator>(E)) 23480b57cec5SDimitry Andric E = UO->getSubExpr(); 23490b57cec5SDimitry Andric 23500b57cec5SDimitry Andric if (const auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { 23510b57cec5SDimitry Andric if (ULE->getNumDecls() == 0) 23520b57cec5SDimitry Andric return false; 23530b57cec5SDimitry Andric 23540b57cec5SDimitry Andric const NamedDecl *ND = *ULE->decls_begin(); 23550b57cec5SDimitry Andric if (const auto *FD = dyn_cast<FunctionDecl>(ND)) 23560b57cec5SDimitry Andric return FD->isCPUDispatchMultiVersion() || FD->isCPUSpecificMultiVersion(); 23570b57cec5SDimitry Andric } 23580b57cec5SDimitry Andric return false; 23590b57cec5SDimitry Andric } 23600b57cec5SDimitry Andric 23610b57cec5SDimitry Andric bool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD, 23620b57cec5SDimitry Andric bool ForceComplain, 23630b57cec5SDimitry Andric bool (*IsPlausibleResult)(QualType)) { 23640b57cec5SDimitry Andric SourceLocation Loc = E.get()->getExprLoc(); 23650b57cec5SDimitry Andric SourceRange Range = E.get()->getSourceRange(); 23660b57cec5SDimitry Andric 23670b57cec5SDimitry Andric QualType ZeroArgCallTy; 23680b57cec5SDimitry Andric UnresolvedSet<4> Overloads; 23690b57cec5SDimitry Andric if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) && 23700b57cec5SDimitry Andric !ZeroArgCallTy.isNull() && 23710b57cec5SDimitry Andric (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) { 23720b57cec5SDimitry Andric // At this point, we know E is potentially callable with 0 23730b57cec5SDimitry Andric // arguments and that it returns something of a reasonable type, 23740b57cec5SDimitry Andric // so we can emit a fixit and carry on pretending that E was 23750b57cec5SDimitry Andric // actually a CallExpr. 23760b57cec5SDimitry Andric SourceLocation ParenInsertionLoc = getLocForEndOfToken(Range.getEnd()); 23770b57cec5SDimitry Andric bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get()); 23780b57cec5SDimitry Andric Diag(Loc, PD) << /*zero-arg*/ 1 << IsMV << Range 23790b57cec5SDimitry Andric << (IsCallableWithAppend(E.get()) 23800b57cec5SDimitry Andric ? FixItHint::CreateInsertion(ParenInsertionLoc, "()") 23810b57cec5SDimitry Andric : FixItHint()); 23820b57cec5SDimitry Andric if (!IsMV) 23830b57cec5SDimitry Andric notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult); 23840b57cec5SDimitry Andric 23850b57cec5SDimitry Andric // FIXME: Try this before emitting the fixit, and suppress diagnostics 23860b57cec5SDimitry Andric // while doing so. 23870b57cec5SDimitry Andric E = BuildCallExpr(nullptr, E.get(), Range.getEnd(), None, 23880b57cec5SDimitry Andric Range.getEnd().getLocWithOffset(1)); 23890b57cec5SDimitry Andric return true; 23900b57cec5SDimitry Andric } 23910b57cec5SDimitry Andric 23920b57cec5SDimitry Andric if (!ForceComplain) return false; 23930b57cec5SDimitry Andric 23940b57cec5SDimitry Andric bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get()); 23950b57cec5SDimitry Andric Diag(Loc, PD) << /*not zero-arg*/ 0 << IsMV << Range; 23960b57cec5SDimitry Andric if (!IsMV) 23970b57cec5SDimitry Andric notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult); 23980b57cec5SDimitry Andric E = ExprError(); 23990b57cec5SDimitry Andric return true; 24000b57cec5SDimitry Andric } 24010b57cec5SDimitry Andric 24020b57cec5SDimitry Andric IdentifierInfo *Sema::getSuperIdentifier() const { 24030b57cec5SDimitry Andric if (!Ident_super) 24040b57cec5SDimitry Andric Ident_super = &Context.Idents.get("super"); 24050b57cec5SDimitry Andric return Ident_super; 24060b57cec5SDimitry Andric } 24070b57cec5SDimitry Andric 24080b57cec5SDimitry Andric IdentifierInfo *Sema::getFloat128Identifier() const { 24090b57cec5SDimitry Andric if (!Ident___float128) 24100b57cec5SDimitry Andric Ident___float128 = &Context.Idents.get("__float128"); 24110b57cec5SDimitry Andric return Ident___float128; 24120b57cec5SDimitry Andric } 24130b57cec5SDimitry Andric 24140b57cec5SDimitry Andric void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD, 2415a7dea167SDimitry Andric CapturedRegionKind K, 2416a7dea167SDimitry Andric unsigned OpenMPCaptureLevel) { 2417a7dea167SDimitry Andric auto *CSI = new CapturedRegionScopeInfo( 24180b57cec5SDimitry Andric getDiagnostics(), S, CD, RD, CD->getContextParam(), K, 2419a7dea167SDimitry Andric (getLangOpts().OpenMP && K == CR_OpenMP) ? getOpenMPNestingLevel() : 0, 2420a7dea167SDimitry Andric OpenMPCaptureLevel); 24210b57cec5SDimitry Andric CSI->ReturnType = Context.VoidTy; 24220b57cec5SDimitry Andric FunctionScopes.push_back(CSI); 24230b57cec5SDimitry Andric } 24240b57cec5SDimitry Andric 24250b57cec5SDimitry Andric CapturedRegionScopeInfo *Sema::getCurCapturedRegion() { 24260b57cec5SDimitry Andric if (FunctionScopes.empty()) 24270b57cec5SDimitry Andric return nullptr; 24280b57cec5SDimitry Andric 24290b57cec5SDimitry Andric return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back()); 24300b57cec5SDimitry Andric } 24310b57cec5SDimitry Andric 24320b57cec5SDimitry Andric const llvm::MapVector<FieldDecl *, Sema::DeleteLocs> & 24330b57cec5SDimitry Andric Sema::getMismatchingDeleteExpressions() const { 24340b57cec5SDimitry Andric return DeleteExprs; 24350b57cec5SDimitry Andric } 24360b57cec5SDimitry Andric 24370b57cec5SDimitry Andric void Sema::setOpenCLExtensionForType(QualType T, llvm::StringRef ExtStr) { 24380b57cec5SDimitry Andric if (ExtStr.empty()) 24390b57cec5SDimitry Andric return; 24400b57cec5SDimitry Andric llvm::SmallVector<StringRef, 1> Exts; 24410b57cec5SDimitry Andric ExtStr.split(Exts, " ", /* limit */ -1, /* keep empty */ false); 24420b57cec5SDimitry Andric auto CanT = T.getCanonicalType().getTypePtr(); 24430b57cec5SDimitry Andric for (auto &I : Exts) 24440b57cec5SDimitry Andric OpenCLTypeExtMap[CanT].insert(I.str()); 24450b57cec5SDimitry Andric } 24460b57cec5SDimitry Andric 24470b57cec5SDimitry Andric void Sema::setOpenCLExtensionForDecl(Decl *FD, StringRef ExtStr) { 24480b57cec5SDimitry Andric llvm::SmallVector<StringRef, 1> Exts; 24490b57cec5SDimitry Andric ExtStr.split(Exts, " ", /* limit */ -1, /* keep empty */ false); 24500b57cec5SDimitry Andric if (Exts.empty()) 24510b57cec5SDimitry Andric return; 24520b57cec5SDimitry Andric for (auto &I : Exts) 24530b57cec5SDimitry Andric OpenCLDeclExtMap[FD].insert(I.str()); 24540b57cec5SDimitry Andric } 24550b57cec5SDimitry Andric 24560b57cec5SDimitry Andric void Sema::setCurrentOpenCLExtensionForType(QualType T) { 24570b57cec5SDimitry Andric if (CurrOpenCLExtension.empty()) 24580b57cec5SDimitry Andric return; 24590b57cec5SDimitry Andric setOpenCLExtensionForType(T, CurrOpenCLExtension); 24600b57cec5SDimitry Andric } 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric void Sema::setCurrentOpenCLExtensionForDecl(Decl *D) { 24630b57cec5SDimitry Andric if (CurrOpenCLExtension.empty()) 24640b57cec5SDimitry Andric return; 24650b57cec5SDimitry Andric setOpenCLExtensionForDecl(D, CurrOpenCLExtension); 24660b57cec5SDimitry Andric } 24670b57cec5SDimitry Andric 24680b57cec5SDimitry Andric std::string Sema::getOpenCLExtensionsFromDeclExtMap(FunctionDecl *FD) { 24690b57cec5SDimitry Andric if (!OpenCLDeclExtMap.empty()) 24700b57cec5SDimitry Andric return getOpenCLExtensionsFromExtMap(FD, OpenCLDeclExtMap); 24710b57cec5SDimitry Andric 24720b57cec5SDimitry Andric return ""; 24730b57cec5SDimitry Andric } 24740b57cec5SDimitry Andric 24750b57cec5SDimitry Andric std::string Sema::getOpenCLExtensionsFromTypeExtMap(FunctionType *FT) { 24760b57cec5SDimitry Andric if (!OpenCLTypeExtMap.empty()) 24770b57cec5SDimitry Andric return getOpenCLExtensionsFromExtMap(FT, OpenCLTypeExtMap); 24780b57cec5SDimitry Andric 24790b57cec5SDimitry Andric return ""; 24800b57cec5SDimitry Andric } 24810b57cec5SDimitry Andric 24820b57cec5SDimitry Andric template <typename T, typename MapT> 24830b57cec5SDimitry Andric std::string Sema::getOpenCLExtensionsFromExtMap(T *FDT, MapT &Map) { 24840b57cec5SDimitry Andric auto Loc = Map.find(FDT); 24855ffd83dbSDimitry Andric return llvm::join(Loc->second, " "); 24860b57cec5SDimitry Andric } 24870b57cec5SDimitry Andric 24880b57cec5SDimitry Andric bool Sema::isOpenCLDisabledDecl(Decl *FD) { 24890b57cec5SDimitry Andric auto Loc = OpenCLDeclExtMap.find(FD); 24900b57cec5SDimitry Andric if (Loc == OpenCLDeclExtMap.end()) 24910b57cec5SDimitry Andric return false; 24920b57cec5SDimitry Andric for (auto &I : Loc->second) { 24930b57cec5SDimitry Andric if (!getOpenCLOptions().isEnabled(I)) 24940b57cec5SDimitry Andric return true; 24950b57cec5SDimitry Andric } 24960b57cec5SDimitry Andric return false; 24970b57cec5SDimitry Andric } 24980b57cec5SDimitry Andric 24990b57cec5SDimitry Andric template <typename T, typename DiagLocT, typename DiagInfoT, typename MapT> 25000b57cec5SDimitry Andric bool Sema::checkOpenCLDisabledTypeOrDecl(T D, DiagLocT DiagLoc, 25010b57cec5SDimitry Andric DiagInfoT DiagInfo, MapT &Map, 25020b57cec5SDimitry Andric unsigned Selector, 25030b57cec5SDimitry Andric SourceRange SrcRange) { 25040b57cec5SDimitry Andric auto Loc = Map.find(D); 25050b57cec5SDimitry Andric if (Loc == Map.end()) 25060b57cec5SDimitry Andric return false; 25070b57cec5SDimitry Andric bool Disabled = false; 25080b57cec5SDimitry Andric for (auto &I : Loc->second) { 25090b57cec5SDimitry Andric if (I != CurrOpenCLExtension && !getOpenCLOptions().isEnabled(I)) { 25100b57cec5SDimitry Andric Diag(DiagLoc, diag::err_opencl_requires_extension) << Selector << DiagInfo 25110b57cec5SDimitry Andric << I << SrcRange; 25120b57cec5SDimitry Andric Disabled = true; 25130b57cec5SDimitry Andric } 25140b57cec5SDimitry Andric } 25150b57cec5SDimitry Andric return Disabled; 25160b57cec5SDimitry Andric } 25170b57cec5SDimitry Andric 25180b57cec5SDimitry Andric bool Sema::checkOpenCLDisabledTypeDeclSpec(const DeclSpec &DS, QualType QT) { 25190b57cec5SDimitry Andric // Check extensions for declared types. 25200b57cec5SDimitry Andric Decl *Decl = nullptr; 25210b57cec5SDimitry Andric if (auto TypedefT = dyn_cast<TypedefType>(QT.getTypePtr())) 25220b57cec5SDimitry Andric Decl = TypedefT->getDecl(); 25230b57cec5SDimitry Andric if (auto TagT = dyn_cast<TagType>(QT.getCanonicalType().getTypePtr())) 25240b57cec5SDimitry Andric Decl = TagT->getDecl(); 25250b57cec5SDimitry Andric auto Loc = DS.getTypeSpecTypeLoc(); 25260b57cec5SDimitry Andric 25270b57cec5SDimitry Andric // Check extensions for vector types. 25280b57cec5SDimitry Andric // e.g. double4 is not allowed when cl_khr_fp64 is absent. 25290b57cec5SDimitry Andric if (QT->isExtVectorType()) { 25300b57cec5SDimitry Andric auto TypePtr = QT->castAs<ExtVectorType>()->getElementType().getTypePtr(); 25310b57cec5SDimitry Andric return checkOpenCLDisabledTypeOrDecl(TypePtr, Loc, QT, OpenCLTypeExtMap); 25320b57cec5SDimitry Andric } 25330b57cec5SDimitry Andric 25340b57cec5SDimitry Andric if (checkOpenCLDisabledTypeOrDecl(Decl, Loc, QT, OpenCLDeclExtMap)) 25350b57cec5SDimitry Andric return true; 25360b57cec5SDimitry Andric 25370b57cec5SDimitry Andric // Check extensions for builtin types. 25380b57cec5SDimitry Andric return checkOpenCLDisabledTypeOrDecl(QT.getCanonicalType().getTypePtr(), Loc, 25390b57cec5SDimitry Andric QT, OpenCLTypeExtMap); 25400b57cec5SDimitry Andric } 25410b57cec5SDimitry Andric 25420b57cec5SDimitry Andric bool Sema::checkOpenCLDisabledDecl(const NamedDecl &D, const Expr &E) { 25430b57cec5SDimitry Andric IdentifierInfo *FnName = D.getIdentifier(); 25440b57cec5SDimitry Andric return checkOpenCLDisabledTypeOrDecl(&D, E.getBeginLoc(), FnName, 25450b57cec5SDimitry Andric OpenCLDeclExtMap, 1, D.getSourceRange()); 25460b57cec5SDimitry Andric } 2547