1*0b57cec5SDimitry Andric //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file implements the actions class which performs semantic analysis and 10*0b57cec5SDimitry Andric // builds an AST out of a parse stream. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 15*0b57cec5SDimitry Andric #include "clang/AST/ASTDiagnostic.h" 16*0b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h" 17*0b57cec5SDimitry Andric #include "clang/AST/DeclFriend.h" 18*0b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h" 19*0b57cec5SDimitry Andric #include "clang/AST/Expr.h" 20*0b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 21*0b57cec5SDimitry Andric #include "clang/AST/PrettyDeclStackTrace.h" 22*0b57cec5SDimitry Andric #include "clang/AST/StmtCXX.h" 23*0b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h" 24*0b57cec5SDimitry Andric #include "clang/Basic/PartialDiagnostic.h" 25*0b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h" 26*0b57cec5SDimitry Andric #include "clang/Lex/HeaderSearch.h" 27*0b57cec5SDimitry Andric #include "clang/Lex/Preprocessor.h" 28*0b57cec5SDimitry Andric #include "clang/Sema/CXXFieldCollector.h" 29*0b57cec5SDimitry Andric #include "clang/Sema/DelayedDiagnostic.h" 30*0b57cec5SDimitry Andric #include "clang/Sema/ExternalSemaSource.h" 31*0b57cec5SDimitry Andric #include "clang/Sema/Initialization.h" 32*0b57cec5SDimitry Andric #include "clang/Sema/MultiplexExternalSemaSource.h" 33*0b57cec5SDimitry Andric #include "clang/Sema/ObjCMethodList.h" 34*0b57cec5SDimitry Andric #include "clang/Sema/Scope.h" 35*0b57cec5SDimitry Andric #include "clang/Sema/ScopeInfo.h" 36*0b57cec5SDimitry Andric #include "clang/Sema/SemaConsumer.h" 37*0b57cec5SDimitry Andric #include "clang/Sema/SemaInternal.h" 38*0b57cec5SDimitry Andric #include "clang/Sema/TemplateDeduction.h" 39*0b57cec5SDimitry Andric #include "clang/Sema/TemplateInstCallback.h" 40*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 41*0b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 42*0b57cec5SDimitry Andric #include "llvm/Support/TimeProfiler.h" 43*0b57cec5SDimitry Andric 44*0b57cec5SDimitry Andric using namespace clang; 45*0b57cec5SDimitry Andric using namespace sema; 46*0b57cec5SDimitry Andric 47*0b57cec5SDimitry Andric SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) { 48*0b57cec5SDimitry Andric return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts); 49*0b57cec5SDimitry Andric } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); } 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context, 54*0b57cec5SDimitry Andric const Preprocessor &PP) { 55*0b57cec5SDimitry Andric PrintingPolicy Policy = Context.getPrintingPolicy(); 56*0b57cec5SDimitry Andric // In diagnostics, we print _Bool as bool if the latter is defined as the 57*0b57cec5SDimitry Andric // former. 58*0b57cec5SDimitry Andric Policy.Bool = Context.getLangOpts().Bool; 59*0b57cec5SDimitry Andric if (!Policy.Bool) { 60*0b57cec5SDimitry Andric if (const MacroInfo *BoolMacro = PP.getMacroInfo(Context.getBoolName())) { 61*0b57cec5SDimitry Andric Policy.Bool = BoolMacro->isObjectLike() && 62*0b57cec5SDimitry Andric BoolMacro->getNumTokens() == 1 && 63*0b57cec5SDimitry Andric BoolMacro->getReplacementToken(0).is(tok::kw__Bool); 64*0b57cec5SDimitry Andric } 65*0b57cec5SDimitry Andric } 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric return Policy; 68*0b57cec5SDimitry Andric } 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric void Sema::ActOnTranslationUnitScope(Scope *S) { 71*0b57cec5SDimitry Andric TUScope = S; 72*0b57cec5SDimitry Andric PushDeclContext(S, Context.getTranslationUnitDecl()); 73*0b57cec5SDimitry Andric } 74*0b57cec5SDimitry Andric 75*0b57cec5SDimitry Andric namespace clang { 76*0b57cec5SDimitry Andric namespace sema { 77*0b57cec5SDimitry Andric 78*0b57cec5SDimitry Andric class SemaPPCallbacks : public PPCallbacks { 79*0b57cec5SDimitry Andric Sema *S = nullptr; 80*0b57cec5SDimitry Andric llvm::SmallVector<SourceLocation, 8> IncludeStack; 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric public: 83*0b57cec5SDimitry Andric void set(Sema &S) { this->S = &S; } 84*0b57cec5SDimitry Andric 85*0b57cec5SDimitry Andric void reset() { S = nullptr; } 86*0b57cec5SDimitry Andric 87*0b57cec5SDimitry Andric virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, 88*0b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType, 89*0b57cec5SDimitry Andric FileID PrevFID) override { 90*0b57cec5SDimitry Andric if (!S) 91*0b57cec5SDimitry Andric return; 92*0b57cec5SDimitry Andric switch (Reason) { 93*0b57cec5SDimitry Andric case EnterFile: { 94*0b57cec5SDimitry Andric SourceManager &SM = S->getSourceManager(); 95*0b57cec5SDimitry Andric SourceLocation IncludeLoc = SM.getIncludeLoc(SM.getFileID(Loc)); 96*0b57cec5SDimitry Andric if (IncludeLoc.isValid()) { 97*0b57cec5SDimitry Andric if (llvm::timeTraceProfilerEnabled()) { 98*0b57cec5SDimitry Andric const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc)); 99*0b57cec5SDimitry Andric llvm::timeTraceProfilerBegin( 100*0b57cec5SDimitry Andric "Source", FE != nullptr ? FE->getName() : StringRef("<unknown>")); 101*0b57cec5SDimitry Andric } 102*0b57cec5SDimitry Andric 103*0b57cec5SDimitry Andric IncludeStack.push_back(IncludeLoc); 104*0b57cec5SDimitry Andric S->DiagnoseNonDefaultPragmaPack( 105*0b57cec5SDimitry Andric Sema::PragmaPackDiagnoseKind::NonDefaultStateAtInclude, IncludeLoc); 106*0b57cec5SDimitry Andric } 107*0b57cec5SDimitry Andric break; 108*0b57cec5SDimitry Andric } 109*0b57cec5SDimitry Andric case ExitFile: 110*0b57cec5SDimitry Andric if (!IncludeStack.empty()) { 111*0b57cec5SDimitry Andric if (llvm::timeTraceProfilerEnabled()) 112*0b57cec5SDimitry Andric llvm::timeTraceProfilerEnd(); 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric S->DiagnoseNonDefaultPragmaPack( 115*0b57cec5SDimitry Andric Sema::PragmaPackDiagnoseKind::ChangedStateAtExit, 116*0b57cec5SDimitry Andric IncludeStack.pop_back_val()); 117*0b57cec5SDimitry Andric } 118*0b57cec5SDimitry Andric break; 119*0b57cec5SDimitry Andric default: 120*0b57cec5SDimitry Andric break; 121*0b57cec5SDimitry Andric } 122*0b57cec5SDimitry Andric } 123*0b57cec5SDimitry Andric }; 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric } // end namespace sema 126*0b57cec5SDimitry Andric } // end namespace clang 127*0b57cec5SDimitry Andric 128*0b57cec5SDimitry Andric Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, 129*0b57cec5SDimitry Andric TranslationUnitKind TUKind, CodeCompleteConsumer *CodeCompleter) 130*0b57cec5SDimitry Andric : ExternalSource(nullptr), isMultiplexExternalSource(false), 131*0b57cec5SDimitry Andric FPFeatures(pp.getLangOpts()), LangOpts(pp.getLangOpts()), PP(pp), 132*0b57cec5SDimitry Andric Context(ctxt), Consumer(consumer), Diags(PP.getDiagnostics()), 133*0b57cec5SDimitry Andric SourceMgr(PP.getSourceManager()), CollectStats(false), 134*0b57cec5SDimitry Andric CodeCompleter(CodeCompleter), CurContext(nullptr), 135*0b57cec5SDimitry Andric OriginalLexicalContext(nullptr), MSStructPragmaOn(false), 136*0b57cec5SDimitry Andric MSPointerToMemberRepresentationMethod( 137*0b57cec5SDimitry Andric LangOpts.getMSPointerToMemberRepresentationMethod()), 138*0b57cec5SDimitry Andric VtorDispStack(MSVtorDispAttr::Mode(LangOpts.VtorDispMode)), PackStack(0), 139*0b57cec5SDimitry Andric DataSegStack(nullptr), BSSSegStack(nullptr), ConstSegStack(nullptr), 140*0b57cec5SDimitry Andric CodeSegStack(nullptr), CurInitSeg(nullptr), VisContext(nullptr), 141*0b57cec5SDimitry Andric PragmaAttributeCurrentTargetDecl(nullptr), 142*0b57cec5SDimitry Andric IsBuildingRecoveryCallExpr(false), Cleanup{}, LateTemplateParser(nullptr), 143*0b57cec5SDimitry Andric LateTemplateParserCleanup(nullptr), OpaqueParser(nullptr), IdResolver(pp), 144*0b57cec5SDimitry Andric StdExperimentalNamespaceCache(nullptr), StdInitializerList(nullptr), 145*0b57cec5SDimitry Andric StdCoroutineTraitsCache(nullptr), CXXTypeInfoDecl(nullptr), 146*0b57cec5SDimitry Andric MSVCGuidDecl(nullptr), NSNumberDecl(nullptr), NSValueDecl(nullptr), 147*0b57cec5SDimitry Andric NSStringDecl(nullptr), StringWithUTF8StringMethod(nullptr), 148*0b57cec5SDimitry Andric ValueWithBytesObjCTypeMethod(nullptr), NSArrayDecl(nullptr), 149*0b57cec5SDimitry Andric ArrayWithObjectsMethod(nullptr), NSDictionaryDecl(nullptr), 150*0b57cec5SDimitry Andric DictionaryWithObjectsMethod(nullptr), GlobalNewDeleteDeclared(false), 151*0b57cec5SDimitry Andric TUKind(TUKind), NumSFINAEErrors(0), 152*0b57cec5SDimitry Andric FullyCheckedComparisonCategories( 153*0b57cec5SDimitry Andric static_cast<unsigned>(ComparisonCategoryType::Last) + 1), 154*0b57cec5SDimitry Andric AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false), 155*0b57cec5SDimitry Andric NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1), 156*0b57cec5SDimitry Andric CurrentInstantiationScope(nullptr), DisableTypoCorrection(false), 157*0b57cec5SDimitry Andric TyposCorrected(0), AnalysisWarnings(*this), 158*0b57cec5SDimitry Andric ThreadSafetyDeclCache(nullptr), VarDataSharingAttributesStack(nullptr), 159*0b57cec5SDimitry Andric CurScope(nullptr), Ident_super(nullptr), Ident___float128(nullptr) { 160*0b57cec5SDimitry Andric TUScope = nullptr; 161*0b57cec5SDimitry Andric isConstantEvaluatedOverride = false; 162*0b57cec5SDimitry Andric 163*0b57cec5SDimitry Andric LoadedExternalKnownNamespaces = false; 164*0b57cec5SDimitry Andric for (unsigned I = 0; I != NSAPI::NumNSNumberLiteralMethods; ++I) 165*0b57cec5SDimitry Andric NSNumberLiteralMethods[I] = nullptr; 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric if (getLangOpts().ObjC) 168*0b57cec5SDimitry Andric NSAPIObj.reset(new NSAPI(Context)); 169*0b57cec5SDimitry Andric 170*0b57cec5SDimitry Andric if (getLangOpts().CPlusPlus) 171*0b57cec5SDimitry Andric FieldCollector.reset(new CXXFieldCollector()); 172*0b57cec5SDimitry Andric 173*0b57cec5SDimitry Andric // Tell diagnostics how to render things from the AST library. 174*0b57cec5SDimitry Andric Diags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument, &Context); 175*0b57cec5SDimitry Andric 176*0b57cec5SDimitry Andric ExprEvalContexts.emplace_back( 177*0b57cec5SDimitry Andric ExpressionEvaluationContext::PotentiallyEvaluated, 0, CleanupInfo{}, 178*0b57cec5SDimitry Andric nullptr, ExpressionEvaluationContextRecord::EK_Other); 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric // Initialization of data sharing attributes stack for OpenMP 181*0b57cec5SDimitry Andric InitDataSharingAttributesStack(); 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric std::unique_ptr<sema::SemaPPCallbacks> Callbacks = 184*0b57cec5SDimitry Andric llvm::make_unique<sema::SemaPPCallbacks>(); 185*0b57cec5SDimitry Andric SemaPPCallbackHandler = Callbacks.get(); 186*0b57cec5SDimitry Andric PP.addPPCallbacks(std::move(Callbacks)); 187*0b57cec5SDimitry Andric SemaPPCallbackHandler->set(*this); 188*0b57cec5SDimitry Andric } 189*0b57cec5SDimitry Andric 190*0b57cec5SDimitry Andric void Sema::addImplicitTypedef(StringRef Name, QualType T) { 191*0b57cec5SDimitry Andric DeclarationName DN = &Context.Idents.get(Name); 192*0b57cec5SDimitry Andric if (IdResolver.begin(DN) == IdResolver.end()) 193*0b57cec5SDimitry Andric PushOnScopeChains(Context.buildImplicitTypedef(T, Name), TUScope); 194*0b57cec5SDimitry Andric } 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric void Sema::Initialize() { 197*0b57cec5SDimitry Andric if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) 198*0b57cec5SDimitry Andric SC->InitializeSema(*this); 199*0b57cec5SDimitry Andric 200*0b57cec5SDimitry Andric // Tell the external Sema source about this Sema object. 201*0b57cec5SDimitry Andric if (ExternalSemaSource *ExternalSema 202*0b57cec5SDimitry Andric = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) 203*0b57cec5SDimitry Andric ExternalSema->InitializeSema(*this); 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric // This needs to happen after ExternalSemaSource::InitializeSema(this) or we 206*0b57cec5SDimitry Andric // will not be able to merge any duplicate __va_list_tag decls correctly. 207*0b57cec5SDimitry Andric VAListTagName = PP.getIdentifierInfo("__va_list_tag"); 208*0b57cec5SDimitry Andric 209*0b57cec5SDimitry Andric if (!TUScope) 210*0b57cec5SDimitry Andric return; 211*0b57cec5SDimitry Andric 212*0b57cec5SDimitry Andric // Initialize predefined 128-bit integer types, if needed. 213*0b57cec5SDimitry Andric if (Context.getTargetInfo().hasInt128Type()) { 214*0b57cec5SDimitry Andric // If either of the 128-bit integer types are unavailable to name lookup, 215*0b57cec5SDimitry Andric // define them now. 216*0b57cec5SDimitry Andric DeclarationName Int128 = &Context.Idents.get("__int128_t"); 217*0b57cec5SDimitry Andric if (IdResolver.begin(Int128) == IdResolver.end()) 218*0b57cec5SDimitry Andric PushOnScopeChains(Context.getInt128Decl(), TUScope); 219*0b57cec5SDimitry Andric 220*0b57cec5SDimitry Andric DeclarationName UInt128 = &Context.Idents.get("__uint128_t"); 221*0b57cec5SDimitry Andric if (IdResolver.begin(UInt128) == IdResolver.end()) 222*0b57cec5SDimitry Andric PushOnScopeChains(Context.getUInt128Decl(), TUScope); 223*0b57cec5SDimitry Andric } 224*0b57cec5SDimitry Andric 225*0b57cec5SDimitry Andric 226*0b57cec5SDimitry Andric // Initialize predefined Objective-C types: 227*0b57cec5SDimitry Andric if (getLangOpts().ObjC) { 228*0b57cec5SDimitry Andric // If 'SEL' does not yet refer to any declarations, make it refer to the 229*0b57cec5SDimitry Andric // predefined 'SEL'. 230*0b57cec5SDimitry Andric DeclarationName SEL = &Context.Idents.get("SEL"); 231*0b57cec5SDimitry Andric if (IdResolver.begin(SEL) == IdResolver.end()) 232*0b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCSelDecl(), TUScope); 233*0b57cec5SDimitry Andric 234*0b57cec5SDimitry Andric // If 'id' does not yet refer to any declarations, make it refer to the 235*0b57cec5SDimitry Andric // predefined 'id'. 236*0b57cec5SDimitry Andric DeclarationName Id = &Context.Idents.get("id"); 237*0b57cec5SDimitry Andric if (IdResolver.begin(Id) == IdResolver.end()) 238*0b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCIdDecl(), TUScope); 239*0b57cec5SDimitry Andric 240*0b57cec5SDimitry Andric // Create the built-in typedef for 'Class'. 241*0b57cec5SDimitry Andric DeclarationName Class = &Context.Idents.get("Class"); 242*0b57cec5SDimitry Andric if (IdResolver.begin(Class) == IdResolver.end()) 243*0b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCClassDecl(), TUScope); 244*0b57cec5SDimitry Andric 245*0b57cec5SDimitry Andric // Create the built-in forward declaratino for 'Protocol'. 246*0b57cec5SDimitry Andric DeclarationName Protocol = &Context.Idents.get("Protocol"); 247*0b57cec5SDimitry Andric if (IdResolver.begin(Protocol) == IdResolver.end()) 248*0b57cec5SDimitry Andric PushOnScopeChains(Context.getObjCProtocolDecl(), TUScope); 249*0b57cec5SDimitry Andric } 250*0b57cec5SDimitry Andric 251*0b57cec5SDimitry Andric // Create the internal type for the *StringMakeConstantString builtins. 252*0b57cec5SDimitry Andric DeclarationName ConstantString = &Context.Idents.get("__NSConstantString"); 253*0b57cec5SDimitry Andric if (IdResolver.begin(ConstantString) == IdResolver.end()) 254*0b57cec5SDimitry Andric PushOnScopeChains(Context.getCFConstantStringDecl(), TUScope); 255*0b57cec5SDimitry Andric 256*0b57cec5SDimitry Andric // Initialize Microsoft "predefined C++ types". 257*0b57cec5SDimitry Andric if (getLangOpts().MSVCCompat) { 258*0b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && 259*0b57cec5SDimitry Andric IdResolver.begin(&Context.Idents.get("type_info")) == IdResolver.end()) 260*0b57cec5SDimitry Andric PushOnScopeChains(Context.buildImplicitRecord("type_info", TTK_Class), 261*0b57cec5SDimitry Andric TUScope); 262*0b57cec5SDimitry Andric 263*0b57cec5SDimitry Andric addImplicitTypedef("size_t", Context.getSizeType()); 264*0b57cec5SDimitry Andric } 265*0b57cec5SDimitry Andric 266*0b57cec5SDimitry Andric // Initialize predefined OpenCL types and supported extensions and (optional) 267*0b57cec5SDimitry Andric // core features. 268*0b57cec5SDimitry Andric if (getLangOpts().OpenCL) { 269*0b57cec5SDimitry Andric getOpenCLOptions().addSupport( 270*0b57cec5SDimitry Andric Context.getTargetInfo().getSupportedOpenCLOpts()); 271*0b57cec5SDimitry Andric getOpenCLOptions().enableSupportedCore(getLangOpts()); 272*0b57cec5SDimitry Andric addImplicitTypedef("sampler_t", Context.OCLSamplerTy); 273*0b57cec5SDimitry Andric addImplicitTypedef("event_t", Context.OCLEventTy); 274*0b57cec5SDimitry Andric if (getLangOpts().OpenCLCPlusPlus || getLangOpts().OpenCLVersion >= 200) { 275*0b57cec5SDimitry Andric addImplicitTypedef("clk_event_t", Context.OCLClkEventTy); 276*0b57cec5SDimitry Andric addImplicitTypedef("queue_t", Context.OCLQueueTy); 277*0b57cec5SDimitry Andric addImplicitTypedef("reserve_id_t", Context.OCLReserveIDTy); 278*0b57cec5SDimitry Andric addImplicitTypedef("atomic_int", Context.getAtomicType(Context.IntTy)); 279*0b57cec5SDimitry Andric addImplicitTypedef("atomic_uint", 280*0b57cec5SDimitry Andric Context.getAtomicType(Context.UnsignedIntTy)); 281*0b57cec5SDimitry Andric auto AtomicLongT = Context.getAtomicType(Context.LongTy); 282*0b57cec5SDimitry Andric addImplicitTypedef("atomic_long", AtomicLongT); 283*0b57cec5SDimitry Andric auto AtomicULongT = Context.getAtomicType(Context.UnsignedLongTy); 284*0b57cec5SDimitry Andric addImplicitTypedef("atomic_ulong", AtomicULongT); 285*0b57cec5SDimitry Andric addImplicitTypedef("atomic_float", 286*0b57cec5SDimitry Andric Context.getAtomicType(Context.FloatTy)); 287*0b57cec5SDimitry Andric auto AtomicDoubleT = Context.getAtomicType(Context.DoubleTy); 288*0b57cec5SDimitry Andric addImplicitTypedef("atomic_double", AtomicDoubleT); 289*0b57cec5SDimitry Andric // OpenCLC v2.0, s6.13.11.6 requires that atomic_flag is implemented as 290*0b57cec5SDimitry Andric // 32-bit integer and OpenCLC v2.0, s6.1.1 int is always 32-bit wide. 291*0b57cec5SDimitry Andric addImplicitTypedef("atomic_flag", Context.getAtomicType(Context.IntTy)); 292*0b57cec5SDimitry Andric auto AtomicIntPtrT = Context.getAtomicType(Context.getIntPtrType()); 293*0b57cec5SDimitry Andric addImplicitTypedef("atomic_intptr_t", AtomicIntPtrT); 294*0b57cec5SDimitry Andric auto AtomicUIntPtrT = Context.getAtomicType(Context.getUIntPtrType()); 295*0b57cec5SDimitry Andric addImplicitTypedef("atomic_uintptr_t", AtomicUIntPtrT); 296*0b57cec5SDimitry Andric auto AtomicSizeT = Context.getAtomicType(Context.getSizeType()); 297*0b57cec5SDimitry Andric addImplicitTypedef("atomic_size_t", AtomicSizeT); 298*0b57cec5SDimitry Andric auto AtomicPtrDiffT = Context.getAtomicType(Context.getPointerDiffType()); 299*0b57cec5SDimitry Andric addImplicitTypedef("atomic_ptrdiff_t", AtomicPtrDiffT); 300*0b57cec5SDimitry Andric 301*0b57cec5SDimitry Andric // OpenCL v2.0 s6.13.11.6: 302*0b57cec5SDimitry Andric // - The atomic_long and atomic_ulong types are supported if the 303*0b57cec5SDimitry Andric // cl_khr_int64_base_atomics and cl_khr_int64_extended_atomics 304*0b57cec5SDimitry Andric // extensions are supported. 305*0b57cec5SDimitry Andric // - The atomic_double type is only supported if double precision 306*0b57cec5SDimitry Andric // is supported and the cl_khr_int64_base_atomics and 307*0b57cec5SDimitry Andric // cl_khr_int64_extended_atomics extensions are supported. 308*0b57cec5SDimitry Andric // - If the device address space is 64-bits, the data types 309*0b57cec5SDimitry Andric // atomic_intptr_t, atomic_uintptr_t, atomic_size_t and 310*0b57cec5SDimitry Andric // atomic_ptrdiff_t are supported if the cl_khr_int64_base_atomics and 311*0b57cec5SDimitry Andric // cl_khr_int64_extended_atomics extensions are supported. 312*0b57cec5SDimitry Andric std::vector<QualType> Atomic64BitTypes; 313*0b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicLongT); 314*0b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicULongT); 315*0b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicDoubleT); 316*0b57cec5SDimitry Andric if (Context.getTypeSize(AtomicSizeT) == 64) { 317*0b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicSizeT); 318*0b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicIntPtrT); 319*0b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicUIntPtrT); 320*0b57cec5SDimitry Andric Atomic64BitTypes.push_back(AtomicPtrDiffT); 321*0b57cec5SDimitry Andric } 322*0b57cec5SDimitry Andric for (auto &I : Atomic64BitTypes) 323*0b57cec5SDimitry Andric setOpenCLExtensionForType(I, 324*0b57cec5SDimitry Andric "cl_khr_int64_base_atomics cl_khr_int64_extended_atomics"); 325*0b57cec5SDimitry Andric 326*0b57cec5SDimitry Andric setOpenCLExtensionForType(AtomicDoubleT, "cl_khr_fp64"); 327*0b57cec5SDimitry Andric } 328*0b57cec5SDimitry Andric 329*0b57cec5SDimitry Andric setOpenCLExtensionForType(Context.DoubleTy, "cl_khr_fp64"); 330*0b57cec5SDimitry Andric 331*0b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE_EXT(Type, Id, Ext) \ 332*0b57cec5SDimitry Andric setOpenCLExtensionForType(Context.Id, Ext); 333*0b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def" 334*0b57cec5SDimitry Andric #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 335*0b57cec5SDimitry Andric addImplicitTypedef(#ExtType, Context.Id##Ty); \ 336*0b57cec5SDimitry Andric setOpenCLExtensionForType(Context.Id##Ty, #Ext); 337*0b57cec5SDimitry Andric #include "clang/Basic/OpenCLExtensionTypes.def" 338*0b57cec5SDimitry Andric }; 339*0b57cec5SDimitry Andric 340*0b57cec5SDimitry Andric if (Context.getTargetInfo().hasBuiltinMSVaList()) { 341*0b57cec5SDimitry Andric DeclarationName MSVaList = &Context.Idents.get("__builtin_ms_va_list"); 342*0b57cec5SDimitry Andric if (IdResolver.begin(MSVaList) == IdResolver.end()) 343*0b57cec5SDimitry Andric PushOnScopeChains(Context.getBuiltinMSVaListDecl(), TUScope); 344*0b57cec5SDimitry Andric } 345*0b57cec5SDimitry Andric 346*0b57cec5SDimitry Andric DeclarationName BuiltinVaList = &Context.Idents.get("__builtin_va_list"); 347*0b57cec5SDimitry Andric if (IdResolver.begin(BuiltinVaList) == IdResolver.end()) 348*0b57cec5SDimitry Andric PushOnScopeChains(Context.getBuiltinVaListDecl(), TUScope); 349*0b57cec5SDimitry Andric } 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric Sema::~Sema() { 352*0b57cec5SDimitry Andric if (VisContext) FreeVisContext(); 353*0b57cec5SDimitry Andric 354*0b57cec5SDimitry Andric // Kill all the active scopes. 355*0b57cec5SDimitry Andric for (sema::FunctionScopeInfo *FSI : FunctionScopes) 356*0b57cec5SDimitry Andric delete FSI; 357*0b57cec5SDimitry Andric 358*0b57cec5SDimitry Andric // Tell the SemaConsumer to forget about us; we're going out of scope. 359*0b57cec5SDimitry Andric if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) 360*0b57cec5SDimitry Andric SC->ForgetSema(); 361*0b57cec5SDimitry Andric 362*0b57cec5SDimitry Andric // Detach from the external Sema source. 363*0b57cec5SDimitry Andric if (ExternalSemaSource *ExternalSema 364*0b57cec5SDimitry Andric = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) 365*0b57cec5SDimitry Andric ExternalSema->ForgetSema(); 366*0b57cec5SDimitry Andric 367*0b57cec5SDimitry Andric // If Sema's ExternalSource is the multiplexer - we own it. 368*0b57cec5SDimitry Andric if (isMultiplexExternalSource) 369*0b57cec5SDimitry Andric delete ExternalSource; 370*0b57cec5SDimitry Andric 371*0b57cec5SDimitry Andric threadSafety::threadSafetyCleanup(ThreadSafetyDeclCache); 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric // Destroys data sharing attributes stack for OpenMP 374*0b57cec5SDimitry Andric DestroyDataSharingAttributesStack(); 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric // Detach from the PP callback handler which outlives Sema since it's owned 377*0b57cec5SDimitry Andric // by the preprocessor. 378*0b57cec5SDimitry Andric SemaPPCallbackHandler->reset(); 379*0b57cec5SDimitry Andric 380*0b57cec5SDimitry Andric assert(DelayedTypos.empty() && "Uncorrected typos!"); 381*0b57cec5SDimitry Andric } 382*0b57cec5SDimitry Andric 383*0b57cec5SDimitry Andric /// makeUnavailableInSystemHeader - There is an error in the current 384*0b57cec5SDimitry Andric /// context. If we're still in a system header, and we can plausibly 385*0b57cec5SDimitry Andric /// make the relevant declaration unavailable instead of erroring, do 386*0b57cec5SDimitry Andric /// so and return true. 387*0b57cec5SDimitry Andric bool Sema::makeUnavailableInSystemHeader(SourceLocation loc, 388*0b57cec5SDimitry Andric UnavailableAttr::ImplicitReason reason) { 389*0b57cec5SDimitry Andric // If we're not in a function, it's an error. 390*0b57cec5SDimitry Andric FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext); 391*0b57cec5SDimitry Andric if (!fn) return false; 392*0b57cec5SDimitry Andric 393*0b57cec5SDimitry Andric // If we're in template instantiation, it's an error. 394*0b57cec5SDimitry Andric if (inTemplateInstantiation()) 395*0b57cec5SDimitry Andric return false; 396*0b57cec5SDimitry Andric 397*0b57cec5SDimitry Andric // If that function's not in a system header, it's an error. 398*0b57cec5SDimitry Andric if (!Context.getSourceManager().isInSystemHeader(loc)) 399*0b57cec5SDimitry Andric return false; 400*0b57cec5SDimitry Andric 401*0b57cec5SDimitry Andric // If the function is already unavailable, it's not an error. 402*0b57cec5SDimitry Andric if (fn->hasAttr<UnavailableAttr>()) return true; 403*0b57cec5SDimitry Andric 404*0b57cec5SDimitry Andric fn->addAttr(UnavailableAttr::CreateImplicit(Context, "", reason, loc)); 405*0b57cec5SDimitry Andric return true; 406*0b57cec5SDimitry Andric } 407*0b57cec5SDimitry Andric 408*0b57cec5SDimitry Andric ASTMutationListener *Sema::getASTMutationListener() const { 409*0b57cec5SDimitry Andric return getASTConsumer().GetASTMutationListener(); 410*0b57cec5SDimitry Andric } 411*0b57cec5SDimitry Andric 412*0b57cec5SDimitry Andric ///Registers an external source. If an external source already exists, 413*0b57cec5SDimitry Andric /// creates a multiplex external source and appends to it. 414*0b57cec5SDimitry Andric /// 415*0b57cec5SDimitry Andric ///\param[in] E - A non-null external sema source. 416*0b57cec5SDimitry Andric /// 417*0b57cec5SDimitry Andric void Sema::addExternalSource(ExternalSemaSource *E) { 418*0b57cec5SDimitry Andric assert(E && "Cannot use with NULL ptr"); 419*0b57cec5SDimitry Andric 420*0b57cec5SDimitry Andric if (!ExternalSource) { 421*0b57cec5SDimitry Andric ExternalSource = E; 422*0b57cec5SDimitry Andric return; 423*0b57cec5SDimitry Andric } 424*0b57cec5SDimitry Andric 425*0b57cec5SDimitry Andric if (isMultiplexExternalSource) 426*0b57cec5SDimitry Andric static_cast<MultiplexExternalSemaSource*>(ExternalSource)->addSource(*E); 427*0b57cec5SDimitry Andric else { 428*0b57cec5SDimitry Andric ExternalSource = new MultiplexExternalSemaSource(*ExternalSource, *E); 429*0b57cec5SDimitry Andric isMultiplexExternalSource = true; 430*0b57cec5SDimitry Andric } 431*0b57cec5SDimitry Andric } 432*0b57cec5SDimitry Andric 433*0b57cec5SDimitry Andric /// Print out statistics about the semantic analysis. 434*0b57cec5SDimitry Andric void Sema::PrintStats() const { 435*0b57cec5SDimitry Andric llvm::errs() << "\n*** Semantic Analysis Stats:\n"; 436*0b57cec5SDimitry Andric llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n"; 437*0b57cec5SDimitry Andric 438*0b57cec5SDimitry Andric BumpAlloc.PrintStats(); 439*0b57cec5SDimitry Andric AnalysisWarnings.PrintStats(); 440*0b57cec5SDimitry Andric } 441*0b57cec5SDimitry Andric 442*0b57cec5SDimitry Andric void Sema::diagnoseNullableToNonnullConversion(QualType DstType, 443*0b57cec5SDimitry Andric QualType SrcType, 444*0b57cec5SDimitry Andric SourceLocation Loc) { 445*0b57cec5SDimitry Andric Optional<NullabilityKind> ExprNullability = SrcType->getNullability(Context); 446*0b57cec5SDimitry Andric if (!ExprNullability || *ExprNullability != NullabilityKind::Nullable) 447*0b57cec5SDimitry Andric return; 448*0b57cec5SDimitry Andric 449*0b57cec5SDimitry Andric Optional<NullabilityKind> TypeNullability = DstType->getNullability(Context); 450*0b57cec5SDimitry Andric if (!TypeNullability || *TypeNullability != NullabilityKind::NonNull) 451*0b57cec5SDimitry Andric return; 452*0b57cec5SDimitry Andric 453*0b57cec5SDimitry Andric Diag(Loc, diag::warn_nullability_lost) << SrcType << DstType; 454*0b57cec5SDimitry Andric } 455*0b57cec5SDimitry Andric 456*0b57cec5SDimitry Andric void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) { 457*0b57cec5SDimitry Andric if (Diags.isIgnored(diag::warn_zero_as_null_pointer_constant, 458*0b57cec5SDimitry Andric E->getBeginLoc())) 459*0b57cec5SDimitry Andric return; 460*0b57cec5SDimitry Andric // nullptr only exists from C++11 on, so don't warn on its absence earlier. 461*0b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus11) 462*0b57cec5SDimitry Andric return; 463*0b57cec5SDimitry Andric 464*0b57cec5SDimitry Andric if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer) 465*0b57cec5SDimitry Andric return; 466*0b57cec5SDimitry Andric if (E->IgnoreParenImpCasts()->getType()->isNullPtrType()) 467*0b57cec5SDimitry Andric return; 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric // If it is a macro from system header, and if the macro name is not "NULL", 470*0b57cec5SDimitry Andric // do not warn. 471*0b57cec5SDimitry Andric SourceLocation MaybeMacroLoc = E->getBeginLoc(); 472*0b57cec5SDimitry Andric if (Diags.getSuppressSystemWarnings() && 473*0b57cec5SDimitry Andric SourceMgr.isInSystemMacro(MaybeMacroLoc) && 474*0b57cec5SDimitry Andric !findMacroSpelling(MaybeMacroLoc, "NULL")) 475*0b57cec5SDimitry Andric return; 476*0b57cec5SDimitry Andric 477*0b57cec5SDimitry Andric Diag(E->getBeginLoc(), diag::warn_zero_as_null_pointer_constant) 478*0b57cec5SDimitry Andric << FixItHint::CreateReplacement(E->getSourceRange(), "nullptr"); 479*0b57cec5SDimitry Andric } 480*0b57cec5SDimitry Andric 481*0b57cec5SDimitry Andric /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 482*0b57cec5SDimitry Andric /// If there is already an implicit cast, merge into the existing one. 483*0b57cec5SDimitry Andric /// The result is of the given category. 484*0b57cec5SDimitry Andric ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty, 485*0b57cec5SDimitry Andric CastKind Kind, ExprValueKind VK, 486*0b57cec5SDimitry Andric const CXXCastPath *BasePath, 487*0b57cec5SDimitry Andric CheckedConversionKind CCK) { 488*0b57cec5SDimitry Andric #ifndef NDEBUG 489*0b57cec5SDimitry Andric if (VK == VK_RValue && !E->isRValue()) { 490*0b57cec5SDimitry Andric switch (Kind) { 491*0b57cec5SDimitry Andric default: 492*0b57cec5SDimitry Andric llvm_unreachable("can't implicitly cast lvalue to rvalue with this cast " 493*0b57cec5SDimitry Andric "kind"); 494*0b57cec5SDimitry Andric case CK_Dependent: 495*0b57cec5SDimitry Andric case CK_LValueToRValue: 496*0b57cec5SDimitry Andric case CK_ArrayToPointerDecay: 497*0b57cec5SDimitry Andric case CK_FunctionToPointerDecay: 498*0b57cec5SDimitry Andric case CK_ToVoid: 499*0b57cec5SDimitry Andric case CK_NonAtomicToAtomic: 500*0b57cec5SDimitry Andric break; 501*0b57cec5SDimitry Andric } 502*0b57cec5SDimitry Andric } 503*0b57cec5SDimitry Andric assert((VK == VK_RValue || Kind == CK_Dependent || !E->isRValue()) && 504*0b57cec5SDimitry Andric "can't cast rvalue to lvalue"); 505*0b57cec5SDimitry Andric #endif 506*0b57cec5SDimitry Andric 507*0b57cec5SDimitry Andric diagnoseNullableToNonnullConversion(Ty, E->getType(), E->getBeginLoc()); 508*0b57cec5SDimitry Andric diagnoseZeroToNullptrConversion(Kind, E); 509*0b57cec5SDimitry Andric 510*0b57cec5SDimitry Andric QualType ExprTy = Context.getCanonicalType(E->getType()); 511*0b57cec5SDimitry Andric QualType TypeTy = Context.getCanonicalType(Ty); 512*0b57cec5SDimitry Andric 513*0b57cec5SDimitry Andric if (ExprTy == TypeTy) 514*0b57cec5SDimitry Andric return E; 515*0b57cec5SDimitry Andric 516*0b57cec5SDimitry Andric // C++1z [conv.array]: The temporary materialization conversion is applied. 517*0b57cec5SDimitry Andric // We also use this to fuel C++ DR1213, which applies to C++11 onwards. 518*0b57cec5SDimitry Andric if (Kind == CK_ArrayToPointerDecay && getLangOpts().CPlusPlus && 519*0b57cec5SDimitry Andric E->getValueKind() == VK_RValue) { 520*0b57cec5SDimitry Andric // The temporary is an lvalue in C++98 and an xvalue otherwise. 521*0b57cec5SDimitry Andric ExprResult Materialized = CreateMaterializeTemporaryExpr( 522*0b57cec5SDimitry Andric E->getType(), E, !getLangOpts().CPlusPlus11); 523*0b57cec5SDimitry Andric if (Materialized.isInvalid()) 524*0b57cec5SDimitry Andric return ExprError(); 525*0b57cec5SDimitry Andric E = Materialized.get(); 526*0b57cec5SDimitry Andric } 527*0b57cec5SDimitry Andric 528*0b57cec5SDimitry Andric if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) { 529*0b57cec5SDimitry Andric if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) { 530*0b57cec5SDimitry Andric ImpCast->setType(Ty); 531*0b57cec5SDimitry Andric ImpCast->setValueKind(VK); 532*0b57cec5SDimitry Andric return E; 533*0b57cec5SDimitry Andric } 534*0b57cec5SDimitry Andric } 535*0b57cec5SDimitry Andric 536*0b57cec5SDimitry Andric return ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK); 537*0b57cec5SDimitry Andric } 538*0b57cec5SDimitry Andric 539*0b57cec5SDimitry Andric /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding 540*0b57cec5SDimitry Andric /// to the conversion from scalar type ScalarTy to the Boolean type. 541*0b57cec5SDimitry Andric CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) { 542*0b57cec5SDimitry Andric switch (ScalarTy->getScalarTypeKind()) { 543*0b57cec5SDimitry Andric case Type::STK_Bool: return CK_NoOp; 544*0b57cec5SDimitry Andric case Type::STK_CPointer: return CK_PointerToBoolean; 545*0b57cec5SDimitry Andric case Type::STK_BlockPointer: return CK_PointerToBoolean; 546*0b57cec5SDimitry Andric case Type::STK_ObjCObjectPointer: return CK_PointerToBoolean; 547*0b57cec5SDimitry Andric case Type::STK_MemberPointer: return CK_MemberPointerToBoolean; 548*0b57cec5SDimitry Andric case Type::STK_Integral: return CK_IntegralToBoolean; 549*0b57cec5SDimitry Andric case Type::STK_Floating: return CK_FloatingToBoolean; 550*0b57cec5SDimitry Andric case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean; 551*0b57cec5SDimitry Andric case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean; 552*0b57cec5SDimitry Andric case Type::STK_FixedPoint: return CK_FixedPointToBoolean; 553*0b57cec5SDimitry Andric } 554*0b57cec5SDimitry Andric llvm_unreachable("unknown scalar type kind"); 555*0b57cec5SDimitry Andric } 556*0b57cec5SDimitry Andric 557*0b57cec5SDimitry Andric /// Used to prune the decls of Sema's UnusedFileScopedDecls vector. 558*0b57cec5SDimitry Andric static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { 559*0b57cec5SDimitry Andric if (D->getMostRecentDecl()->isUsed()) 560*0b57cec5SDimitry Andric return true; 561*0b57cec5SDimitry Andric 562*0b57cec5SDimitry Andric if (D->isExternallyVisible()) 563*0b57cec5SDimitry Andric return true; 564*0b57cec5SDimitry Andric 565*0b57cec5SDimitry Andric if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 566*0b57cec5SDimitry Andric // If this is a function template and none of its specializations is used, 567*0b57cec5SDimitry Andric // we should warn. 568*0b57cec5SDimitry Andric if (FunctionTemplateDecl *Template = FD->getDescribedFunctionTemplate()) 569*0b57cec5SDimitry Andric for (const auto *Spec : Template->specializations()) 570*0b57cec5SDimitry Andric if (ShouldRemoveFromUnused(SemaRef, Spec)) 571*0b57cec5SDimitry Andric return true; 572*0b57cec5SDimitry Andric 573*0b57cec5SDimitry Andric // UnusedFileScopedDecls stores the first declaration. 574*0b57cec5SDimitry Andric // The declaration may have become definition so check again. 575*0b57cec5SDimitry Andric const FunctionDecl *DeclToCheck; 576*0b57cec5SDimitry Andric if (FD->hasBody(DeclToCheck)) 577*0b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 578*0b57cec5SDimitry Andric 579*0b57cec5SDimitry Andric // Later redecls may add new information resulting in not having to warn, 580*0b57cec5SDimitry Andric // so check again. 581*0b57cec5SDimitry Andric DeclToCheck = FD->getMostRecentDecl(); 582*0b57cec5SDimitry Andric if (DeclToCheck != FD) 583*0b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 584*0b57cec5SDimitry Andric } 585*0b57cec5SDimitry Andric 586*0b57cec5SDimitry Andric if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 587*0b57cec5SDimitry Andric // If a variable usable in constant expressions is referenced, 588*0b57cec5SDimitry Andric // don't warn if it isn't used: if the value of a variable is required 589*0b57cec5SDimitry Andric // for the computation of a constant expression, it doesn't make sense to 590*0b57cec5SDimitry Andric // warn even if the variable isn't odr-used. (isReferenced doesn't 591*0b57cec5SDimitry Andric // precisely reflect that, but it's a decent approximation.) 592*0b57cec5SDimitry Andric if (VD->isReferenced() && 593*0b57cec5SDimitry Andric VD->mightBeUsableInConstantExpressions(SemaRef->Context)) 594*0b57cec5SDimitry Andric return true; 595*0b57cec5SDimitry Andric 596*0b57cec5SDimitry Andric if (VarTemplateDecl *Template = VD->getDescribedVarTemplate()) 597*0b57cec5SDimitry Andric // If this is a variable template and none of its specializations is used, 598*0b57cec5SDimitry Andric // we should warn. 599*0b57cec5SDimitry Andric for (const auto *Spec : Template->specializations()) 600*0b57cec5SDimitry Andric if (ShouldRemoveFromUnused(SemaRef, Spec)) 601*0b57cec5SDimitry Andric return true; 602*0b57cec5SDimitry Andric 603*0b57cec5SDimitry Andric // UnusedFileScopedDecls stores the first declaration. 604*0b57cec5SDimitry Andric // The declaration may have become definition so check again. 605*0b57cec5SDimitry Andric const VarDecl *DeclToCheck = VD->getDefinition(); 606*0b57cec5SDimitry Andric if (DeclToCheck) 607*0b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 608*0b57cec5SDimitry Andric 609*0b57cec5SDimitry Andric // Later redecls may add new information resulting in not having to warn, 610*0b57cec5SDimitry Andric // so check again. 611*0b57cec5SDimitry Andric DeclToCheck = VD->getMostRecentDecl(); 612*0b57cec5SDimitry Andric if (DeclToCheck != VD) 613*0b57cec5SDimitry Andric return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 614*0b57cec5SDimitry Andric } 615*0b57cec5SDimitry Andric 616*0b57cec5SDimitry Andric return false; 617*0b57cec5SDimitry Andric } 618*0b57cec5SDimitry Andric 619*0b57cec5SDimitry Andric static bool isFunctionOrVarDeclExternC(NamedDecl *ND) { 620*0b57cec5SDimitry Andric if (auto *FD = dyn_cast<FunctionDecl>(ND)) 621*0b57cec5SDimitry Andric return FD->isExternC(); 622*0b57cec5SDimitry Andric return cast<VarDecl>(ND)->isExternC(); 623*0b57cec5SDimitry Andric } 624*0b57cec5SDimitry Andric 625*0b57cec5SDimitry Andric /// Determine whether ND is an external-linkage function or variable whose 626*0b57cec5SDimitry Andric /// type has no linkage. 627*0b57cec5SDimitry Andric bool Sema::isExternalWithNoLinkageType(ValueDecl *VD) { 628*0b57cec5SDimitry Andric // Note: it's not quite enough to check whether VD has UniqueExternalLinkage, 629*0b57cec5SDimitry Andric // because we also want to catch the case where its type has VisibleNoLinkage, 630*0b57cec5SDimitry Andric // which does not affect the linkage of VD. 631*0b57cec5SDimitry Andric return getLangOpts().CPlusPlus && VD->hasExternalFormalLinkage() && 632*0b57cec5SDimitry Andric !isExternalFormalLinkage(VD->getType()->getLinkage()) && 633*0b57cec5SDimitry Andric !isFunctionOrVarDeclExternC(VD); 634*0b57cec5SDimitry Andric } 635*0b57cec5SDimitry Andric 636*0b57cec5SDimitry Andric /// Obtains a sorted list of functions and variables that are undefined but 637*0b57cec5SDimitry Andric /// ODR-used. 638*0b57cec5SDimitry Andric void Sema::getUndefinedButUsed( 639*0b57cec5SDimitry Andric SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> > &Undefined) { 640*0b57cec5SDimitry Andric for (const auto &UndefinedUse : UndefinedButUsed) { 641*0b57cec5SDimitry Andric NamedDecl *ND = UndefinedUse.first; 642*0b57cec5SDimitry Andric 643*0b57cec5SDimitry Andric // Ignore attributes that have become invalid. 644*0b57cec5SDimitry Andric if (ND->isInvalidDecl()) continue; 645*0b57cec5SDimitry Andric 646*0b57cec5SDimitry Andric // __attribute__((weakref)) is basically a definition. 647*0b57cec5SDimitry Andric if (ND->hasAttr<WeakRefAttr>()) continue; 648*0b57cec5SDimitry Andric 649*0b57cec5SDimitry Andric if (isa<CXXDeductionGuideDecl>(ND)) 650*0b57cec5SDimitry Andric continue; 651*0b57cec5SDimitry Andric 652*0b57cec5SDimitry Andric if (ND->hasAttr<DLLImportAttr>() || ND->hasAttr<DLLExportAttr>()) { 653*0b57cec5SDimitry Andric // An exported function will always be emitted when defined, so even if 654*0b57cec5SDimitry Andric // the function is inline, it doesn't have to be emitted in this TU. An 655*0b57cec5SDimitry Andric // imported function implies that it has been exported somewhere else. 656*0b57cec5SDimitry Andric continue; 657*0b57cec5SDimitry Andric } 658*0b57cec5SDimitry Andric 659*0b57cec5SDimitry Andric if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 660*0b57cec5SDimitry Andric if (FD->isDefined()) 661*0b57cec5SDimitry Andric continue; 662*0b57cec5SDimitry Andric if (FD->isExternallyVisible() && 663*0b57cec5SDimitry Andric !isExternalWithNoLinkageType(FD) && 664*0b57cec5SDimitry Andric !FD->getMostRecentDecl()->isInlined() && 665*0b57cec5SDimitry Andric !FD->hasAttr<ExcludeFromExplicitInstantiationAttr>()) 666*0b57cec5SDimitry Andric continue; 667*0b57cec5SDimitry Andric if (FD->getBuiltinID()) 668*0b57cec5SDimitry Andric continue; 669*0b57cec5SDimitry Andric } else { 670*0b57cec5SDimitry Andric auto *VD = cast<VarDecl>(ND); 671*0b57cec5SDimitry Andric if (VD->hasDefinition() != VarDecl::DeclarationOnly) 672*0b57cec5SDimitry Andric continue; 673*0b57cec5SDimitry Andric if (VD->isExternallyVisible() && 674*0b57cec5SDimitry Andric !isExternalWithNoLinkageType(VD) && 675*0b57cec5SDimitry Andric !VD->getMostRecentDecl()->isInline() && 676*0b57cec5SDimitry Andric !VD->hasAttr<ExcludeFromExplicitInstantiationAttr>()) 677*0b57cec5SDimitry Andric continue; 678*0b57cec5SDimitry Andric 679*0b57cec5SDimitry Andric // Skip VarDecls that lack formal definitions but which we know are in 680*0b57cec5SDimitry Andric // fact defined somewhere. 681*0b57cec5SDimitry Andric if (VD->isKnownToBeDefined()) 682*0b57cec5SDimitry Andric continue; 683*0b57cec5SDimitry Andric } 684*0b57cec5SDimitry Andric 685*0b57cec5SDimitry Andric Undefined.push_back(std::make_pair(ND, UndefinedUse.second)); 686*0b57cec5SDimitry Andric } 687*0b57cec5SDimitry Andric } 688*0b57cec5SDimitry Andric 689*0b57cec5SDimitry Andric /// checkUndefinedButUsed - Check for undefined objects with internal linkage 690*0b57cec5SDimitry Andric /// or that are inline. 691*0b57cec5SDimitry Andric static void checkUndefinedButUsed(Sema &S) { 692*0b57cec5SDimitry Andric if (S.UndefinedButUsed.empty()) return; 693*0b57cec5SDimitry Andric 694*0b57cec5SDimitry Andric // Collect all the still-undefined entities with internal linkage. 695*0b57cec5SDimitry Andric SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined; 696*0b57cec5SDimitry Andric S.getUndefinedButUsed(Undefined); 697*0b57cec5SDimitry Andric if (Undefined.empty()) return; 698*0b57cec5SDimitry Andric 699*0b57cec5SDimitry Andric for (auto Undef : Undefined) { 700*0b57cec5SDimitry Andric ValueDecl *VD = cast<ValueDecl>(Undef.first); 701*0b57cec5SDimitry Andric SourceLocation UseLoc = Undef.second; 702*0b57cec5SDimitry Andric 703*0b57cec5SDimitry Andric if (S.isExternalWithNoLinkageType(VD)) { 704*0b57cec5SDimitry Andric // C++ [basic.link]p8: 705*0b57cec5SDimitry Andric // A type without linkage shall not be used as the type of a variable 706*0b57cec5SDimitry Andric // or function with external linkage unless 707*0b57cec5SDimitry Andric // -- the entity has C language linkage 708*0b57cec5SDimitry Andric // -- the entity is not odr-used or is defined in the same TU 709*0b57cec5SDimitry Andric // 710*0b57cec5SDimitry Andric // As an extension, accept this in cases where the type is externally 711*0b57cec5SDimitry Andric // visible, since the function or variable actually can be defined in 712*0b57cec5SDimitry Andric // another translation unit in that case. 713*0b57cec5SDimitry Andric S.Diag(VD->getLocation(), isExternallyVisible(VD->getType()->getLinkage()) 714*0b57cec5SDimitry Andric ? diag::ext_undefined_internal_type 715*0b57cec5SDimitry Andric : diag::err_undefined_internal_type) 716*0b57cec5SDimitry Andric << isa<VarDecl>(VD) << VD; 717*0b57cec5SDimitry Andric } else if (!VD->isExternallyVisible()) { 718*0b57cec5SDimitry Andric // FIXME: We can promote this to an error. The function or variable can't 719*0b57cec5SDimitry Andric // be defined anywhere else, so the program must necessarily violate the 720*0b57cec5SDimitry Andric // one definition rule. 721*0b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::warn_undefined_internal) 722*0b57cec5SDimitry Andric << isa<VarDecl>(VD) << VD; 723*0b57cec5SDimitry Andric } else if (auto *FD = dyn_cast<FunctionDecl>(VD)) { 724*0b57cec5SDimitry Andric (void)FD; 725*0b57cec5SDimitry Andric assert(FD->getMostRecentDecl()->isInlined() && 726*0b57cec5SDimitry Andric "used object requires definition but isn't inline or internal?"); 727*0b57cec5SDimitry Andric // FIXME: This is ill-formed; we should reject. 728*0b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::warn_undefined_inline) << VD; 729*0b57cec5SDimitry Andric } else { 730*0b57cec5SDimitry Andric assert(cast<VarDecl>(VD)->getMostRecentDecl()->isInline() && 731*0b57cec5SDimitry Andric "used var requires definition but isn't inline or internal?"); 732*0b57cec5SDimitry Andric S.Diag(VD->getLocation(), diag::err_undefined_inline_var) << VD; 733*0b57cec5SDimitry Andric } 734*0b57cec5SDimitry Andric if (UseLoc.isValid()) 735*0b57cec5SDimitry Andric S.Diag(UseLoc, diag::note_used_here); 736*0b57cec5SDimitry Andric } 737*0b57cec5SDimitry Andric 738*0b57cec5SDimitry Andric S.UndefinedButUsed.clear(); 739*0b57cec5SDimitry Andric } 740*0b57cec5SDimitry Andric 741*0b57cec5SDimitry Andric void Sema::LoadExternalWeakUndeclaredIdentifiers() { 742*0b57cec5SDimitry Andric if (!ExternalSource) 743*0b57cec5SDimitry Andric return; 744*0b57cec5SDimitry Andric 745*0b57cec5SDimitry Andric SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs; 746*0b57cec5SDimitry Andric ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs); 747*0b57cec5SDimitry Andric for (auto &WeakID : WeakIDs) 748*0b57cec5SDimitry Andric WeakUndeclaredIdentifiers.insert(WeakID); 749*0b57cec5SDimitry Andric } 750*0b57cec5SDimitry Andric 751*0b57cec5SDimitry Andric 752*0b57cec5SDimitry Andric typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap; 753*0b57cec5SDimitry Andric 754*0b57cec5SDimitry Andric /// Returns true, if all methods and nested classes of the given 755*0b57cec5SDimitry Andric /// CXXRecordDecl are defined in this translation unit. 756*0b57cec5SDimitry Andric /// 757*0b57cec5SDimitry Andric /// Should only be called from ActOnEndOfTranslationUnit so that all 758*0b57cec5SDimitry Andric /// definitions are actually read. 759*0b57cec5SDimitry Andric static bool MethodsAndNestedClassesComplete(const CXXRecordDecl *RD, 760*0b57cec5SDimitry Andric RecordCompleteMap &MNCComplete) { 761*0b57cec5SDimitry Andric RecordCompleteMap::iterator Cache = MNCComplete.find(RD); 762*0b57cec5SDimitry Andric if (Cache != MNCComplete.end()) 763*0b57cec5SDimitry Andric return Cache->second; 764*0b57cec5SDimitry Andric if (!RD->isCompleteDefinition()) 765*0b57cec5SDimitry Andric return false; 766*0b57cec5SDimitry Andric bool Complete = true; 767*0b57cec5SDimitry Andric for (DeclContext::decl_iterator I = RD->decls_begin(), 768*0b57cec5SDimitry Andric E = RD->decls_end(); 769*0b57cec5SDimitry Andric I != E && Complete; ++I) { 770*0b57cec5SDimitry Andric if (const CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(*I)) 771*0b57cec5SDimitry Andric Complete = M->isDefined() || M->isDefaulted() || 772*0b57cec5SDimitry Andric (M->isPure() && !isa<CXXDestructorDecl>(M)); 773*0b57cec5SDimitry Andric else if (const FunctionTemplateDecl *F = dyn_cast<FunctionTemplateDecl>(*I)) 774*0b57cec5SDimitry Andric // If the template function is marked as late template parsed at this 775*0b57cec5SDimitry Andric // point, it has not been instantiated and therefore we have not 776*0b57cec5SDimitry Andric // performed semantic analysis on it yet, so we cannot know if the type 777*0b57cec5SDimitry Andric // can be considered complete. 778*0b57cec5SDimitry Andric Complete = !F->getTemplatedDecl()->isLateTemplateParsed() && 779*0b57cec5SDimitry Andric F->getTemplatedDecl()->isDefined(); 780*0b57cec5SDimitry Andric else if (const CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(*I)) { 781*0b57cec5SDimitry Andric if (R->isInjectedClassName()) 782*0b57cec5SDimitry Andric continue; 783*0b57cec5SDimitry Andric if (R->hasDefinition()) 784*0b57cec5SDimitry Andric Complete = MethodsAndNestedClassesComplete(R->getDefinition(), 785*0b57cec5SDimitry Andric MNCComplete); 786*0b57cec5SDimitry Andric else 787*0b57cec5SDimitry Andric Complete = false; 788*0b57cec5SDimitry Andric } 789*0b57cec5SDimitry Andric } 790*0b57cec5SDimitry Andric MNCComplete[RD] = Complete; 791*0b57cec5SDimitry Andric return Complete; 792*0b57cec5SDimitry Andric } 793*0b57cec5SDimitry Andric 794*0b57cec5SDimitry Andric /// Returns true, if the given CXXRecordDecl is fully defined in this 795*0b57cec5SDimitry Andric /// translation unit, i.e. all methods are defined or pure virtual and all 796*0b57cec5SDimitry Andric /// friends, friend functions and nested classes are fully defined in this 797*0b57cec5SDimitry Andric /// translation unit. 798*0b57cec5SDimitry Andric /// 799*0b57cec5SDimitry Andric /// Should only be called from ActOnEndOfTranslationUnit so that all 800*0b57cec5SDimitry Andric /// definitions are actually read. 801*0b57cec5SDimitry Andric static bool IsRecordFullyDefined(const CXXRecordDecl *RD, 802*0b57cec5SDimitry Andric RecordCompleteMap &RecordsComplete, 803*0b57cec5SDimitry Andric RecordCompleteMap &MNCComplete) { 804*0b57cec5SDimitry Andric RecordCompleteMap::iterator Cache = RecordsComplete.find(RD); 805*0b57cec5SDimitry Andric if (Cache != RecordsComplete.end()) 806*0b57cec5SDimitry Andric return Cache->second; 807*0b57cec5SDimitry Andric bool Complete = MethodsAndNestedClassesComplete(RD, MNCComplete); 808*0b57cec5SDimitry Andric for (CXXRecordDecl::friend_iterator I = RD->friend_begin(), 809*0b57cec5SDimitry Andric E = RD->friend_end(); 810*0b57cec5SDimitry Andric I != E && Complete; ++I) { 811*0b57cec5SDimitry Andric // Check if friend classes and methods are complete. 812*0b57cec5SDimitry Andric if (TypeSourceInfo *TSI = (*I)->getFriendType()) { 813*0b57cec5SDimitry Andric // Friend classes are available as the TypeSourceInfo of the FriendDecl. 814*0b57cec5SDimitry Andric if (CXXRecordDecl *FriendD = TSI->getType()->getAsCXXRecordDecl()) 815*0b57cec5SDimitry Andric Complete = MethodsAndNestedClassesComplete(FriendD, MNCComplete); 816*0b57cec5SDimitry Andric else 817*0b57cec5SDimitry Andric Complete = false; 818*0b57cec5SDimitry Andric } else { 819*0b57cec5SDimitry Andric // Friend functions are available through the NamedDecl of FriendDecl. 820*0b57cec5SDimitry Andric if (const FunctionDecl *FD = 821*0b57cec5SDimitry Andric dyn_cast<FunctionDecl>((*I)->getFriendDecl())) 822*0b57cec5SDimitry Andric Complete = FD->isDefined(); 823*0b57cec5SDimitry Andric else 824*0b57cec5SDimitry Andric // This is a template friend, give up. 825*0b57cec5SDimitry Andric Complete = false; 826*0b57cec5SDimitry Andric } 827*0b57cec5SDimitry Andric } 828*0b57cec5SDimitry Andric RecordsComplete[RD] = Complete; 829*0b57cec5SDimitry Andric return Complete; 830*0b57cec5SDimitry Andric } 831*0b57cec5SDimitry Andric 832*0b57cec5SDimitry Andric void Sema::emitAndClearUnusedLocalTypedefWarnings() { 833*0b57cec5SDimitry Andric if (ExternalSource) 834*0b57cec5SDimitry Andric ExternalSource->ReadUnusedLocalTypedefNameCandidates( 835*0b57cec5SDimitry Andric UnusedLocalTypedefNameCandidates); 836*0b57cec5SDimitry Andric for (const TypedefNameDecl *TD : UnusedLocalTypedefNameCandidates) { 837*0b57cec5SDimitry Andric if (TD->isReferenced()) 838*0b57cec5SDimitry Andric continue; 839*0b57cec5SDimitry Andric Diag(TD->getLocation(), diag::warn_unused_local_typedef) 840*0b57cec5SDimitry Andric << isa<TypeAliasDecl>(TD) << TD->getDeclName(); 841*0b57cec5SDimitry Andric } 842*0b57cec5SDimitry Andric UnusedLocalTypedefNameCandidates.clear(); 843*0b57cec5SDimitry Andric } 844*0b57cec5SDimitry Andric 845*0b57cec5SDimitry Andric /// This is called before the very first declaration in the translation unit 846*0b57cec5SDimitry Andric /// is parsed. Note that the ASTContext may have already injected some 847*0b57cec5SDimitry Andric /// declarations. 848*0b57cec5SDimitry Andric void Sema::ActOnStartOfTranslationUnit() { 849*0b57cec5SDimitry Andric if (getLangOpts().ModulesTS && 850*0b57cec5SDimitry Andric (getLangOpts().getCompilingModule() == LangOptions::CMK_ModuleInterface || 851*0b57cec5SDimitry Andric getLangOpts().getCompilingModule() == LangOptions::CMK_None)) { 852*0b57cec5SDimitry Andric // We start in an implied global module fragment. 853*0b57cec5SDimitry Andric SourceLocation StartOfTU = 854*0b57cec5SDimitry Andric SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 855*0b57cec5SDimitry Andric ActOnGlobalModuleFragmentDecl(StartOfTU); 856*0b57cec5SDimitry Andric ModuleScopes.back().ImplicitGlobalModuleFragment = true; 857*0b57cec5SDimitry Andric } 858*0b57cec5SDimitry Andric } 859*0b57cec5SDimitry Andric 860*0b57cec5SDimitry Andric void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) { 861*0b57cec5SDimitry Andric // No explicit actions are required at the end of the global module fragment. 862*0b57cec5SDimitry Andric if (Kind == TUFragmentKind::Global) 863*0b57cec5SDimitry Andric return; 864*0b57cec5SDimitry Andric 865*0b57cec5SDimitry Andric // Transfer late parsed template instantiations over to the pending template 866*0b57cec5SDimitry Andric // instantiation list. During normal compilation, the late template parser 867*0b57cec5SDimitry Andric // will be installed and instantiating these templates will succeed. 868*0b57cec5SDimitry Andric // 869*0b57cec5SDimitry Andric // If we are building a TU prefix for serialization, it is also safe to 870*0b57cec5SDimitry Andric // transfer these over, even though they are not parsed. The end of the TU 871*0b57cec5SDimitry Andric // should be outside of any eager template instantiation scope, so when this 872*0b57cec5SDimitry Andric // AST is deserialized, these templates will not be parsed until the end of 873*0b57cec5SDimitry Andric // the combined TU. 874*0b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.end(), 875*0b57cec5SDimitry Andric LateParsedInstantiations.begin(), 876*0b57cec5SDimitry Andric LateParsedInstantiations.end()); 877*0b57cec5SDimitry Andric LateParsedInstantiations.clear(); 878*0b57cec5SDimitry Andric 879*0b57cec5SDimitry Andric // If DefinedUsedVTables ends up marking any virtual member functions it 880*0b57cec5SDimitry Andric // might lead to more pending template instantiations, which we then need 881*0b57cec5SDimitry Andric // to instantiate. 882*0b57cec5SDimitry Andric DefineUsedVTables(); 883*0b57cec5SDimitry Andric 884*0b57cec5SDimitry Andric // C++: Perform implicit template instantiations. 885*0b57cec5SDimitry Andric // 886*0b57cec5SDimitry Andric // FIXME: When we perform these implicit instantiations, we do not 887*0b57cec5SDimitry Andric // carefully keep track of the point of instantiation (C++ [temp.point]). 888*0b57cec5SDimitry Andric // This means that name lookup that occurs within the template 889*0b57cec5SDimitry Andric // instantiation will always happen at the end of the translation unit, 890*0b57cec5SDimitry Andric // so it will find some names that are not required to be found. This is 891*0b57cec5SDimitry Andric // valid, but we could do better by diagnosing if an instantiation uses a 892*0b57cec5SDimitry Andric // name that was not visible at its first point of instantiation. 893*0b57cec5SDimitry Andric if (ExternalSource) { 894*0b57cec5SDimitry Andric // Load pending instantiations from the external source. 895*0b57cec5SDimitry Andric SmallVector<PendingImplicitInstantiation, 4> Pending; 896*0b57cec5SDimitry Andric ExternalSource->ReadPendingInstantiations(Pending); 897*0b57cec5SDimitry Andric for (auto PII : Pending) 898*0b57cec5SDimitry Andric if (auto Func = dyn_cast<FunctionDecl>(PII.first)) 899*0b57cec5SDimitry Andric Func->setInstantiationIsPending(true); 900*0b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.begin(), 901*0b57cec5SDimitry Andric Pending.begin(), Pending.end()); 902*0b57cec5SDimitry Andric } 903*0b57cec5SDimitry Andric 904*0b57cec5SDimitry Andric { 905*0b57cec5SDimitry Andric llvm::TimeTraceScope TimeScope("PerformPendingInstantiations", 906*0b57cec5SDimitry Andric StringRef("")); 907*0b57cec5SDimitry Andric PerformPendingInstantiations(); 908*0b57cec5SDimitry Andric } 909*0b57cec5SDimitry Andric 910*0b57cec5SDimitry Andric assert(LateParsedInstantiations.empty() && 911*0b57cec5SDimitry Andric "end of TU template instantiation should not create more " 912*0b57cec5SDimitry Andric "late-parsed templates"); 913*0b57cec5SDimitry Andric } 914*0b57cec5SDimitry Andric 915*0b57cec5SDimitry Andric /// ActOnEndOfTranslationUnit - This is called at the very end of the 916*0b57cec5SDimitry Andric /// translation unit when EOF is reached and all but the top-level scope is 917*0b57cec5SDimitry Andric /// popped. 918*0b57cec5SDimitry Andric void Sema::ActOnEndOfTranslationUnit() { 919*0b57cec5SDimitry Andric assert(DelayedDiagnostics.getCurrentPool() == nullptr 920*0b57cec5SDimitry Andric && "reached end of translation unit with a pool attached?"); 921*0b57cec5SDimitry Andric 922*0b57cec5SDimitry Andric // If code completion is enabled, don't perform any end-of-translation-unit 923*0b57cec5SDimitry Andric // work. 924*0b57cec5SDimitry Andric if (PP.isCodeCompletionEnabled()) 925*0b57cec5SDimitry Andric return; 926*0b57cec5SDimitry Andric 927*0b57cec5SDimitry Andric // Complete translation units and modules define vtables and perform implicit 928*0b57cec5SDimitry Andric // instantiations. PCH files do not. 929*0b57cec5SDimitry Andric if (TUKind != TU_Prefix) { 930*0b57cec5SDimitry Andric DiagnoseUseOfUnimplementedSelectors(); 931*0b57cec5SDimitry Andric 932*0b57cec5SDimitry Andric ActOnEndOfTranslationUnitFragment( 933*0b57cec5SDimitry Andric !ModuleScopes.empty() && ModuleScopes.back().Module->Kind == 934*0b57cec5SDimitry Andric Module::PrivateModuleFragment 935*0b57cec5SDimitry Andric ? TUFragmentKind::Private 936*0b57cec5SDimitry Andric : TUFragmentKind::Normal); 937*0b57cec5SDimitry Andric 938*0b57cec5SDimitry Andric if (LateTemplateParserCleanup) 939*0b57cec5SDimitry Andric LateTemplateParserCleanup(OpaqueParser); 940*0b57cec5SDimitry Andric 941*0b57cec5SDimitry Andric CheckDelayedMemberExceptionSpecs(); 942*0b57cec5SDimitry Andric } else { 943*0b57cec5SDimitry Andric // If we are building a TU prefix for serialization, it is safe to transfer 944*0b57cec5SDimitry Andric // these over, even though they are not parsed. The end of the TU should be 945*0b57cec5SDimitry Andric // outside of any eager template instantiation scope, so when this AST is 946*0b57cec5SDimitry Andric // deserialized, these templates will not be parsed until the end of the 947*0b57cec5SDimitry Andric // combined TU. 948*0b57cec5SDimitry Andric PendingInstantiations.insert(PendingInstantiations.end(), 949*0b57cec5SDimitry Andric LateParsedInstantiations.begin(), 950*0b57cec5SDimitry Andric LateParsedInstantiations.end()); 951*0b57cec5SDimitry Andric LateParsedInstantiations.clear(); 952*0b57cec5SDimitry Andric } 953*0b57cec5SDimitry Andric 954*0b57cec5SDimitry Andric DiagnoseUnterminatedPragmaPack(); 955*0b57cec5SDimitry Andric DiagnoseUnterminatedPragmaAttribute(); 956*0b57cec5SDimitry Andric 957*0b57cec5SDimitry Andric // All delayed member exception specs should be checked or we end up accepting 958*0b57cec5SDimitry Andric // incompatible declarations. 959*0b57cec5SDimitry Andric assert(DelayedOverridingExceptionSpecChecks.empty()); 960*0b57cec5SDimitry Andric assert(DelayedEquivalentExceptionSpecChecks.empty()); 961*0b57cec5SDimitry Andric 962*0b57cec5SDimitry Andric // All dllexport classes should have been processed already. 963*0b57cec5SDimitry Andric assert(DelayedDllExportClasses.empty()); 964*0b57cec5SDimitry Andric assert(DelayedDllExportMemberFunctions.empty()); 965*0b57cec5SDimitry Andric 966*0b57cec5SDimitry Andric // Remove file scoped decls that turned out to be used. 967*0b57cec5SDimitry Andric UnusedFileScopedDecls.erase( 968*0b57cec5SDimitry Andric std::remove_if(UnusedFileScopedDecls.begin(nullptr, true), 969*0b57cec5SDimitry Andric UnusedFileScopedDecls.end(), 970*0b57cec5SDimitry Andric [this](const DeclaratorDecl *DD) { 971*0b57cec5SDimitry Andric return ShouldRemoveFromUnused(this, DD); 972*0b57cec5SDimitry Andric }), 973*0b57cec5SDimitry Andric UnusedFileScopedDecls.end()); 974*0b57cec5SDimitry Andric 975*0b57cec5SDimitry Andric if (TUKind == TU_Prefix) { 976*0b57cec5SDimitry Andric // Translation unit prefixes don't need any of the checking below. 977*0b57cec5SDimitry Andric if (!PP.isIncrementalProcessingEnabled()) 978*0b57cec5SDimitry Andric TUScope = nullptr; 979*0b57cec5SDimitry Andric return; 980*0b57cec5SDimitry Andric } 981*0b57cec5SDimitry Andric 982*0b57cec5SDimitry Andric // Check for #pragma weak identifiers that were never declared 983*0b57cec5SDimitry Andric LoadExternalWeakUndeclaredIdentifiers(); 984*0b57cec5SDimitry Andric for (auto WeakID : WeakUndeclaredIdentifiers) { 985*0b57cec5SDimitry Andric if (WeakID.second.getUsed()) 986*0b57cec5SDimitry Andric continue; 987*0b57cec5SDimitry Andric 988*0b57cec5SDimitry Andric Decl *PrevDecl = LookupSingleName(TUScope, WeakID.first, SourceLocation(), 989*0b57cec5SDimitry Andric LookupOrdinaryName); 990*0b57cec5SDimitry Andric if (PrevDecl != nullptr && 991*0b57cec5SDimitry Andric !(isa<FunctionDecl>(PrevDecl) || isa<VarDecl>(PrevDecl))) 992*0b57cec5SDimitry Andric Diag(WeakID.second.getLocation(), diag::warn_attribute_wrong_decl_type) 993*0b57cec5SDimitry Andric << "'weak'" << ExpectedVariableOrFunction; 994*0b57cec5SDimitry Andric else 995*0b57cec5SDimitry Andric Diag(WeakID.second.getLocation(), diag::warn_weak_identifier_undeclared) 996*0b57cec5SDimitry Andric << WeakID.first; 997*0b57cec5SDimitry Andric } 998*0b57cec5SDimitry Andric 999*0b57cec5SDimitry Andric if (LangOpts.CPlusPlus11 && 1000*0b57cec5SDimitry Andric !Diags.isIgnored(diag::warn_delegating_ctor_cycle, SourceLocation())) 1001*0b57cec5SDimitry Andric CheckDelegatingCtorCycles(); 1002*0b57cec5SDimitry Andric 1003*0b57cec5SDimitry Andric if (!Diags.hasErrorOccurred()) { 1004*0b57cec5SDimitry Andric if (ExternalSource) 1005*0b57cec5SDimitry Andric ExternalSource->ReadUndefinedButUsed(UndefinedButUsed); 1006*0b57cec5SDimitry Andric checkUndefinedButUsed(*this); 1007*0b57cec5SDimitry Andric } 1008*0b57cec5SDimitry Andric 1009*0b57cec5SDimitry Andric // A global-module-fragment is only permitted within a module unit. 1010*0b57cec5SDimitry Andric bool DiagnosedMissingModuleDeclaration = false; 1011*0b57cec5SDimitry Andric if (!ModuleScopes.empty() && 1012*0b57cec5SDimitry Andric ModuleScopes.back().Module->Kind == Module::GlobalModuleFragment && 1013*0b57cec5SDimitry Andric !ModuleScopes.back().ImplicitGlobalModuleFragment) { 1014*0b57cec5SDimitry Andric Diag(ModuleScopes.back().BeginLoc, 1015*0b57cec5SDimitry Andric diag::err_module_declaration_missing_after_global_module_introducer); 1016*0b57cec5SDimitry Andric DiagnosedMissingModuleDeclaration = true; 1017*0b57cec5SDimitry Andric } 1018*0b57cec5SDimitry Andric 1019*0b57cec5SDimitry Andric if (TUKind == TU_Module) { 1020*0b57cec5SDimitry Andric // If we are building a module interface unit, we need to have seen the 1021*0b57cec5SDimitry Andric // module declaration by now. 1022*0b57cec5SDimitry Andric if (getLangOpts().getCompilingModule() == 1023*0b57cec5SDimitry Andric LangOptions::CMK_ModuleInterface && 1024*0b57cec5SDimitry Andric (ModuleScopes.empty() || 1025*0b57cec5SDimitry Andric !ModuleScopes.back().Module->isModulePurview()) && 1026*0b57cec5SDimitry Andric !DiagnosedMissingModuleDeclaration) { 1027*0b57cec5SDimitry Andric // FIXME: Make a better guess as to where to put the module declaration. 1028*0b57cec5SDimitry Andric Diag(getSourceManager().getLocForStartOfFile( 1029*0b57cec5SDimitry Andric getSourceManager().getMainFileID()), 1030*0b57cec5SDimitry Andric diag::err_module_declaration_missing); 1031*0b57cec5SDimitry Andric } 1032*0b57cec5SDimitry Andric 1033*0b57cec5SDimitry Andric // If we are building a module, resolve all of the exported declarations 1034*0b57cec5SDimitry Andric // now. 1035*0b57cec5SDimitry Andric if (Module *CurrentModule = PP.getCurrentModule()) { 1036*0b57cec5SDimitry Andric ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 1037*0b57cec5SDimitry Andric 1038*0b57cec5SDimitry Andric SmallVector<Module *, 2> Stack; 1039*0b57cec5SDimitry Andric Stack.push_back(CurrentModule); 1040*0b57cec5SDimitry Andric while (!Stack.empty()) { 1041*0b57cec5SDimitry Andric Module *Mod = Stack.pop_back_val(); 1042*0b57cec5SDimitry Andric 1043*0b57cec5SDimitry Andric // Resolve the exported declarations and conflicts. 1044*0b57cec5SDimitry Andric // FIXME: Actually complain, once we figure out how to teach the 1045*0b57cec5SDimitry Andric // diagnostic client to deal with complaints in the module map at this 1046*0b57cec5SDimitry Andric // point. 1047*0b57cec5SDimitry Andric ModMap.resolveExports(Mod, /*Complain=*/false); 1048*0b57cec5SDimitry Andric ModMap.resolveUses(Mod, /*Complain=*/false); 1049*0b57cec5SDimitry Andric ModMap.resolveConflicts(Mod, /*Complain=*/false); 1050*0b57cec5SDimitry Andric 1051*0b57cec5SDimitry Andric // Queue the submodules, so their exports will also be resolved. 1052*0b57cec5SDimitry Andric Stack.append(Mod->submodule_begin(), Mod->submodule_end()); 1053*0b57cec5SDimitry Andric } 1054*0b57cec5SDimitry Andric } 1055*0b57cec5SDimitry Andric 1056*0b57cec5SDimitry Andric // Warnings emitted in ActOnEndOfTranslationUnit() should be emitted for 1057*0b57cec5SDimitry Andric // modules when they are built, not every time they are used. 1058*0b57cec5SDimitry Andric emitAndClearUnusedLocalTypedefWarnings(); 1059*0b57cec5SDimitry Andric } 1060*0b57cec5SDimitry Andric 1061*0b57cec5SDimitry Andric // C99 6.9.2p2: 1062*0b57cec5SDimitry Andric // A declaration of an identifier for an object that has file 1063*0b57cec5SDimitry Andric // scope without an initializer, and without a storage-class 1064*0b57cec5SDimitry Andric // specifier or with the storage-class specifier static, 1065*0b57cec5SDimitry Andric // constitutes a tentative definition. If a translation unit 1066*0b57cec5SDimitry Andric // contains one or more tentative definitions for an identifier, 1067*0b57cec5SDimitry Andric // and the translation unit contains no external definition for 1068*0b57cec5SDimitry Andric // that identifier, then the behavior is exactly as if the 1069*0b57cec5SDimitry Andric // translation unit contains a file scope declaration of that 1070*0b57cec5SDimitry Andric // identifier, with the composite type as of the end of the 1071*0b57cec5SDimitry Andric // translation unit, with an initializer equal to 0. 1072*0b57cec5SDimitry Andric llvm::SmallSet<VarDecl *, 32> Seen; 1073*0b57cec5SDimitry Andric for (TentativeDefinitionsType::iterator 1074*0b57cec5SDimitry Andric T = TentativeDefinitions.begin(ExternalSource), 1075*0b57cec5SDimitry Andric TEnd = TentativeDefinitions.end(); 1076*0b57cec5SDimitry Andric T != TEnd; ++T) { 1077*0b57cec5SDimitry Andric VarDecl *VD = (*T)->getActingDefinition(); 1078*0b57cec5SDimitry Andric 1079*0b57cec5SDimitry Andric // If the tentative definition was completed, getActingDefinition() returns 1080*0b57cec5SDimitry Andric // null. If we've already seen this variable before, insert()'s second 1081*0b57cec5SDimitry Andric // return value is false. 1082*0b57cec5SDimitry Andric if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second) 1083*0b57cec5SDimitry Andric continue; 1084*0b57cec5SDimitry Andric 1085*0b57cec5SDimitry Andric if (const IncompleteArrayType *ArrayT 1086*0b57cec5SDimitry Andric = Context.getAsIncompleteArrayType(VD->getType())) { 1087*0b57cec5SDimitry Andric // Set the length of the array to 1 (C99 6.9.2p5). 1088*0b57cec5SDimitry Andric Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); 1089*0b57cec5SDimitry Andric llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); 1090*0b57cec5SDimitry Andric QualType T = Context.getConstantArrayType(ArrayT->getElementType(), 1091*0b57cec5SDimitry Andric One, ArrayType::Normal, 0); 1092*0b57cec5SDimitry Andric VD->setType(T); 1093*0b57cec5SDimitry Andric } else if (RequireCompleteType(VD->getLocation(), VD->getType(), 1094*0b57cec5SDimitry Andric diag::err_tentative_def_incomplete_type)) 1095*0b57cec5SDimitry Andric VD->setInvalidDecl(); 1096*0b57cec5SDimitry Andric 1097*0b57cec5SDimitry Andric // No initialization is performed for a tentative definition. 1098*0b57cec5SDimitry Andric CheckCompleteVariableDeclaration(VD); 1099*0b57cec5SDimitry Andric 1100*0b57cec5SDimitry Andric // Notify the consumer that we've completed a tentative definition. 1101*0b57cec5SDimitry Andric if (!VD->isInvalidDecl()) 1102*0b57cec5SDimitry Andric Consumer.CompleteTentativeDefinition(VD); 1103*0b57cec5SDimitry Andric } 1104*0b57cec5SDimitry Andric 1105*0b57cec5SDimitry Andric // If there were errors, disable 'unused' warnings since they will mostly be 1106*0b57cec5SDimitry Andric // noise. Don't warn for a use from a module: either we should warn on all 1107*0b57cec5SDimitry Andric // file-scope declarations in modules or not at all, but whether the 1108*0b57cec5SDimitry Andric // declaration is used is immaterial. 1109*0b57cec5SDimitry Andric if (!Diags.hasErrorOccurred() && TUKind != TU_Module) { 1110*0b57cec5SDimitry Andric // Output warning for unused file scoped decls. 1111*0b57cec5SDimitry Andric for (UnusedFileScopedDeclsType::iterator 1112*0b57cec5SDimitry Andric I = UnusedFileScopedDecls.begin(ExternalSource), 1113*0b57cec5SDimitry Andric E = UnusedFileScopedDecls.end(); I != E; ++I) { 1114*0b57cec5SDimitry Andric if (ShouldRemoveFromUnused(this, *I)) 1115*0b57cec5SDimitry Andric continue; 1116*0b57cec5SDimitry Andric 1117*0b57cec5SDimitry Andric if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { 1118*0b57cec5SDimitry Andric const FunctionDecl *DiagD; 1119*0b57cec5SDimitry Andric if (!FD->hasBody(DiagD)) 1120*0b57cec5SDimitry Andric DiagD = FD; 1121*0b57cec5SDimitry Andric if (DiagD->isDeleted()) 1122*0b57cec5SDimitry Andric continue; // Deleted functions are supposed to be unused. 1123*0b57cec5SDimitry Andric if (DiagD->isReferenced()) { 1124*0b57cec5SDimitry Andric if (isa<CXXMethodDecl>(DiagD)) 1125*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_member_function) 1126*0b57cec5SDimitry Andric << DiagD->getDeclName(); 1127*0b57cec5SDimitry Andric else { 1128*0b57cec5SDimitry Andric if (FD->getStorageClass() == SC_Static && 1129*0b57cec5SDimitry Andric !FD->isInlineSpecified() && 1130*0b57cec5SDimitry Andric !SourceMgr.isInMainFile( 1131*0b57cec5SDimitry Andric SourceMgr.getExpansionLoc(FD->getLocation()))) 1132*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), 1133*0b57cec5SDimitry Andric diag::warn_unneeded_static_internal_decl) 1134*0b57cec5SDimitry Andric << DiagD->getDeclName(); 1135*0b57cec5SDimitry Andric else 1136*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) 1137*0b57cec5SDimitry Andric << /*function*/0 << DiagD->getDeclName(); 1138*0b57cec5SDimitry Andric } 1139*0b57cec5SDimitry Andric } else { 1140*0b57cec5SDimitry Andric if (FD->getDescribedFunctionTemplate()) 1141*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_template) 1142*0b57cec5SDimitry Andric << /*function*/0 << DiagD->getDeclName(); 1143*0b57cec5SDimitry Andric else 1144*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), 1145*0b57cec5SDimitry Andric isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function 1146*0b57cec5SDimitry Andric : diag::warn_unused_function) 1147*0b57cec5SDimitry Andric << DiagD->getDeclName(); 1148*0b57cec5SDimitry Andric } 1149*0b57cec5SDimitry Andric } else { 1150*0b57cec5SDimitry Andric const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition(); 1151*0b57cec5SDimitry Andric if (!DiagD) 1152*0b57cec5SDimitry Andric DiagD = cast<VarDecl>(*I); 1153*0b57cec5SDimitry Andric if (DiagD->isReferenced()) { 1154*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) 1155*0b57cec5SDimitry Andric << /*variable*/1 << DiagD->getDeclName(); 1156*0b57cec5SDimitry Andric } else if (DiagD->getType().isConstQualified()) { 1157*0b57cec5SDimitry Andric const SourceManager &SM = SourceMgr; 1158*0b57cec5SDimitry Andric if (SM.getMainFileID() != SM.getFileID(DiagD->getLocation()) || 1159*0b57cec5SDimitry Andric !PP.getLangOpts().IsHeaderFile) 1160*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_const_variable) 1161*0b57cec5SDimitry Andric << DiagD->getDeclName(); 1162*0b57cec5SDimitry Andric } else { 1163*0b57cec5SDimitry Andric if (DiagD->getDescribedVarTemplate()) 1164*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_template) 1165*0b57cec5SDimitry Andric << /*variable*/1 << DiagD->getDeclName(); 1166*0b57cec5SDimitry Andric else 1167*0b57cec5SDimitry Andric Diag(DiagD->getLocation(), diag::warn_unused_variable) 1168*0b57cec5SDimitry Andric << DiagD->getDeclName(); 1169*0b57cec5SDimitry Andric } 1170*0b57cec5SDimitry Andric } 1171*0b57cec5SDimitry Andric } 1172*0b57cec5SDimitry Andric 1173*0b57cec5SDimitry Andric emitAndClearUnusedLocalTypedefWarnings(); 1174*0b57cec5SDimitry Andric } 1175*0b57cec5SDimitry Andric 1176*0b57cec5SDimitry Andric if (!Diags.isIgnored(diag::warn_unused_private_field, SourceLocation())) { 1177*0b57cec5SDimitry Andric // FIXME: Load additional unused private field candidates from the external 1178*0b57cec5SDimitry Andric // source. 1179*0b57cec5SDimitry Andric RecordCompleteMap RecordsComplete; 1180*0b57cec5SDimitry Andric RecordCompleteMap MNCComplete; 1181*0b57cec5SDimitry Andric for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(), 1182*0b57cec5SDimitry Andric E = UnusedPrivateFields.end(); I != E; ++I) { 1183*0b57cec5SDimitry Andric const NamedDecl *D = *I; 1184*0b57cec5SDimitry Andric const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); 1185*0b57cec5SDimitry Andric if (RD && !RD->isUnion() && 1186*0b57cec5SDimitry Andric IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) { 1187*0b57cec5SDimitry Andric Diag(D->getLocation(), diag::warn_unused_private_field) 1188*0b57cec5SDimitry Andric << D->getDeclName(); 1189*0b57cec5SDimitry Andric } 1190*0b57cec5SDimitry Andric } 1191*0b57cec5SDimitry Andric } 1192*0b57cec5SDimitry Andric 1193*0b57cec5SDimitry Andric if (!Diags.isIgnored(diag::warn_mismatched_delete_new, SourceLocation())) { 1194*0b57cec5SDimitry Andric if (ExternalSource) 1195*0b57cec5SDimitry Andric ExternalSource->ReadMismatchingDeleteExpressions(DeleteExprs); 1196*0b57cec5SDimitry Andric for (const auto &DeletedFieldInfo : DeleteExprs) { 1197*0b57cec5SDimitry Andric for (const auto &DeleteExprLoc : DeletedFieldInfo.second) { 1198*0b57cec5SDimitry Andric AnalyzeDeleteExprMismatch(DeletedFieldInfo.first, DeleteExprLoc.first, 1199*0b57cec5SDimitry Andric DeleteExprLoc.second); 1200*0b57cec5SDimitry Andric } 1201*0b57cec5SDimitry Andric } 1202*0b57cec5SDimitry Andric } 1203*0b57cec5SDimitry Andric 1204*0b57cec5SDimitry Andric // Check we've noticed that we're no longer parsing the initializer for every 1205*0b57cec5SDimitry Andric // variable. If we miss cases, then at best we have a performance issue and 1206*0b57cec5SDimitry Andric // at worst a rejects-valid bug. 1207*0b57cec5SDimitry Andric assert(ParsingInitForAutoVars.empty() && 1208*0b57cec5SDimitry Andric "Didn't unmark var as having its initializer parsed"); 1209*0b57cec5SDimitry Andric 1210*0b57cec5SDimitry Andric if (!PP.isIncrementalProcessingEnabled()) 1211*0b57cec5SDimitry Andric TUScope = nullptr; 1212*0b57cec5SDimitry Andric } 1213*0b57cec5SDimitry Andric 1214*0b57cec5SDimitry Andric 1215*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1216*0b57cec5SDimitry Andric // Helper functions. 1217*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1218*0b57cec5SDimitry Andric 1219*0b57cec5SDimitry Andric DeclContext *Sema::getFunctionLevelDeclContext() { 1220*0b57cec5SDimitry Andric DeclContext *DC = CurContext; 1221*0b57cec5SDimitry Andric 1222*0b57cec5SDimitry Andric while (true) { 1223*0b57cec5SDimitry Andric if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC) || isa<CapturedDecl>(DC)) { 1224*0b57cec5SDimitry Andric DC = DC->getParent(); 1225*0b57cec5SDimitry Andric } else if (isa<CXXMethodDecl>(DC) && 1226*0b57cec5SDimitry Andric cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && 1227*0b57cec5SDimitry Andric cast<CXXRecordDecl>(DC->getParent())->isLambda()) { 1228*0b57cec5SDimitry Andric DC = DC->getParent()->getParent(); 1229*0b57cec5SDimitry Andric } 1230*0b57cec5SDimitry Andric else break; 1231*0b57cec5SDimitry Andric } 1232*0b57cec5SDimitry Andric 1233*0b57cec5SDimitry Andric return DC; 1234*0b57cec5SDimitry Andric } 1235*0b57cec5SDimitry Andric 1236*0b57cec5SDimitry Andric /// getCurFunctionDecl - If inside of a function body, this returns a pointer 1237*0b57cec5SDimitry Andric /// to the function decl for the function being parsed. If we're currently 1238*0b57cec5SDimitry Andric /// in a 'block', this returns the containing context. 1239*0b57cec5SDimitry Andric FunctionDecl *Sema::getCurFunctionDecl() { 1240*0b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 1241*0b57cec5SDimitry Andric return dyn_cast<FunctionDecl>(DC); 1242*0b57cec5SDimitry Andric } 1243*0b57cec5SDimitry Andric 1244*0b57cec5SDimitry Andric ObjCMethodDecl *Sema::getCurMethodDecl() { 1245*0b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 1246*0b57cec5SDimitry Andric while (isa<RecordDecl>(DC)) 1247*0b57cec5SDimitry Andric DC = DC->getParent(); 1248*0b57cec5SDimitry Andric return dyn_cast<ObjCMethodDecl>(DC); 1249*0b57cec5SDimitry Andric } 1250*0b57cec5SDimitry Andric 1251*0b57cec5SDimitry Andric NamedDecl *Sema::getCurFunctionOrMethodDecl() { 1252*0b57cec5SDimitry Andric DeclContext *DC = getFunctionLevelDeclContext(); 1253*0b57cec5SDimitry Andric if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) 1254*0b57cec5SDimitry Andric return cast<NamedDecl>(DC); 1255*0b57cec5SDimitry Andric return nullptr; 1256*0b57cec5SDimitry Andric } 1257*0b57cec5SDimitry Andric 1258*0b57cec5SDimitry Andric void Sema::EmitCurrentDiagnostic(unsigned DiagID) { 1259*0b57cec5SDimitry Andric // FIXME: It doesn't make sense to me that DiagID is an incoming argument here 1260*0b57cec5SDimitry Andric // and yet we also use the current diag ID on the DiagnosticsEngine. This has 1261*0b57cec5SDimitry Andric // been made more painfully obvious by the refactor that introduced this 1262*0b57cec5SDimitry Andric // function, but it is possible that the incoming argument can be 1263*0b57cec5SDimitry Andric // eliminated. If it truly cannot be (for example, there is some reentrancy 1264*0b57cec5SDimitry Andric // issue I am not seeing yet), then there should at least be a clarifying 1265*0b57cec5SDimitry Andric // comment somewhere. 1266*0b57cec5SDimitry Andric if (Optional<TemplateDeductionInfo*> Info = isSFINAEContext()) { 1267*0b57cec5SDimitry Andric switch (DiagnosticIDs::getDiagnosticSFINAEResponse( 1268*0b57cec5SDimitry Andric Diags.getCurrentDiagID())) { 1269*0b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_Report: 1270*0b57cec5SDimitry Andric // We'll report the diagnostic below. 1271*0b57cec5SDimitry Andric break; 1272*0b57cec5SDimitry Andric 1273*0b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_SubstitutionFailure: 1274*0b57cec5SDimitry Andric // Count this failure so that we know that template argument deduction 1275*0b57cec5SDimitry Andric // has failed. 1276*0b57cec5SDimitry Andric ++NumSFINAEErrors; 1277*0b57cec5SDimitry Andric 1278*0b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 1279*0b57cec5SDimitry Andric // template-deduction information. 1280*0b57cec5SDimitry Andric if (*Info && !(*Info)->hasSFINAEDiagnostic()) { 1281*0b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 1282*0b57cec5SDimitry Andric (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(), 1283*0b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 1284*0b57cec5SDimitry Andric } 1285*0b57cec5SDimitry Andric 1286*0b57cec5SDimitry Andric Diags.setLastDiagnosticIgnored(); 1287*0b57cec5SDimitry Andric Diags.Clear(); 1288*0b57cec5SDimitry Andric return; 1289*0b57cec5SDimitry Andric 1290*0b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_AccessControl: { 1291*0b57cec5SDimitry Andric // Per C++ Core Issue 1170, access control is part of SFINAE. 1292*0b57cec5SDimitry Andric // Additionally, the AccessCheckingSFINAE flag can be used to temporarily 1293*0b57cec5SDimitry Andric // make access control a part of SFINAE for the purposes of checking 1294*0b57cec5SDimitry Andric // type traits. 1295*0b57cec5SDimitry Andric if (!AccessCheckingSFINAE && !getLangOpts().CPlusPlus11) 1296*0b57cec5SDimitry Andric break; 1297*0b57cec5SDimitry Andric 1298*0b57cec5SDimitry Andric SourceLocation Loc = Diags.getCurrentDiagLoc(); 1299*0b57cec5SDimitry Andric 1300*0b57cec5SDimitry Andric // Suppress this diagnostic. 1301*0b57cec5SDimitry Andric ++NumSFINAEErrors; 1302*0b57cec5SDimitry Andric 1303*0b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 1304*0b57cec5SDimitry Andric // template-deduction information. 1305*0b57cec5SDimitry Andric if (*Info && !(*Info)->hasSFINAEDiagnostic()) { 1306*0b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 1307*0b57cec5SDimitry Andric (*Info)->addSFINAEDiagnostic(DiagInfo.getLocation(), 1308*0b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 1309*0b57cec5SDimitry Andric } 1310*0b57cec5SDimitry Andric 1311*0b57cec5SDimitry Andric Diags.setLastDiagnosticIgnored(); 1312*0b57cec5SDimitry Andric Diags.Clear(); 1313*0b57cec5SDimitry Andric 1314*0b57cec5SDimitry Andric // Now the diagnostic state is clear, produce a C++98 compatibility 1315*0b57cec5SDimitry Andric // warning. 1316*0b57cec5SDimitry Andric Diag(Loc, diag::warn_cxx98_compat_sfinae_access_control); 1317*0b57cec5SDimitry Andric 1318*0b57cec5SDimitry Andric // The last diagnostic which Sema produced was ignored. Suppress any 1319*0b57cec5SDimitry Andric // notes attached to it. 1320*0b57cec5SDimitry Andric Diags.setLastDiagnosticIgnored(); 1321*0b57cec5SDimitry Andric return; 1322*0b57cec5SDimitry Andric } 1323*0b57cec5SDimitry Andric 1324*0b57cec5SDimitry Andric case DiagnosticIDs::SFINAE_Suppress: 1325*0b57cec5SDimitry Andric // Make a copy of this suppressed diagnostic and store it with the 1326*0b57cec5SDimitry Andric // template-deduction information; 1327*0b57cec5SDimitry Andric if (*Info) { 1328*0b57cec5SDimitry Andric Diagnostic DiagInfo(&Diags); 1329*0b57cec5SDimitry Andric (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(), 1330*0b57cec5SDimitry Andric PartialDiagnostic(DiagInfo, Context.getDiagAllocator())); 1331*0b57cec5SDimitry Andric } 1332*0b57cec5SDimitry Andric 1333*0b57cec5SDimitry Andric // Suppress this diagnostic. 1334*0b57cec5SDimitry Andric Diags.setLastDiagnosticIgnored(); 1335*0b57cec5SDimitry Andric Diags.Clear(); 1336*0b57cec5SDimitry Andric return; 1337*0b57cec5SDimitry Andric } 1338*0b57cec5SDimitry Andric } 1339*0b57cec5SDimitry Andric 1340*0b57cec5SDimitry Andric // Copy the diagnostic printing policy over the ASTContext printing policy. 1341*0b57cec5SDimitry Andric // TODO: Stop doing that. See: https://reviews.llvm.org/D45093#1090292 1342*0b57cec5SDimitry Andric Context.setPrintingPolicy(getPrintingPolicy()); 1343*0b57cec5SDimitry Andric 1344*0b57cec5SDimitry Andric // Emit the diagnostic. 1345*0b57cec5SDimitry Andric if (!Diags.EmitCurrentDiagnostic()) 1346*0b57cec5SDimitry Andric return; 1347*0b57cec5SDimitry Andric 1348*0b57cec5SDimitry Andric // If this is not a note, and we're in a template instantiation 1349*0b57cec5SDimitry Andric // that is different from the last template instantiation where 1350*0b57cec5SDimitry Andric // we emitted an error, print a template instantiation 1351*0b57cec5SDimitry Andric // backtrace. 1352*0b57cec5SDimitry Andric if (!DiagnosticIDs::isBuiltinNote(DiagID)) 1353*0b57cec5SDimitry Andric PrintContextStack(); 1354*0b57cec5SDimitry Andric } 1355*0b57cec5SDimitry Andric 1356*0b57cec5SDimitry Andric Sema::SemaDiagnosticBuilder 1357*0b57cec5SDimitry Andric Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { 1358*0b57cec5SDimitry Andric SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); 1359*0b57cec5SDimitry Andric PD.Emit(Builder); 1360*0b57cec5SDimitry Andric 1361*0b57cec5SDimitry Andric return Builder; 1362*0b57cec5SDimitry Andric } 1363*0b57cec5SDimitry Andric 1364*0b57cec5SDimitry Andric // Print notes showing how we can reach FD starting from an a priori 1365*0b57cec5SDimitry Andric // known-callable function. 1366*0b57cec5SDimitry Andric static void emitCallStackNotes(Sema &S, FunctionDecl *FD) { 1367*0b57cec5SDimitry Andric auto FnIt = S.DeviceKnownEmittedFns.find(FD); 1368*0b57cec5SDimitry Andric while (FnIt != S.DeviceKnownEmittedFns.end()) { 1369*0b57cec5SDimitry Andric DiagnosticBuilder Builder( 1370*0b57cec5SDimitry Andric S.Diags.Report(FnIt->second.Loc, diag::note_called_by)); 1371*0b57cec5SDimitry Andric Builder << FnIt->second.FD; 1372*0b57cec5SDimitry Andric Builder.setForceEmit(); 1373*0b57cec5SDimitry Andric 1374*0b57cec5SDimitry Andric FnIt = S.DeviceKnownEmittedFns.find(FnIt->second.FD); 1375*0b57cec5SDimitry Andric } 1376*0b57cec5SDimitry Andric } 1377*0b57cec5SDimitry Andric 1378*0b57cec5SDimitry Andric // Emit any deferred diagnostics for FD and erase them from the map in which 1379*0b57cec5SDimitry Andric // they're stored. 1380*0b57cec5SDimitry Andric static void emitDeferredDiags(Sema &S, FunctionDecl *FD) { 1381*0b57cec5SDimitry Andric auto It = S.DeviceDeferredDiags.find(FD); 1382*0b57cec5SDimitry Andric if (It == S.DeviceDeferredDiags.end()) 1383*0b57cec5SDimitry Andric return; 1384*0b57cec5SDimitry Andric bool HasWarningOrError = false; 1385*0b57cec5SDimitry Andric for (PartialDiagnosticAt &PDAt : It->second) { 1386*0b57cec5SDimitry Andric const SourceLocation &Loc = PDAt.first; 1387*0b57cec5SDimitry Andric const PartialDiagnostic &PD = PDAt.second; 1388*0b57cec5SDimitry Andric HasWarningOrError |= S.getDiagnostics().getDiagnosticLevel( 1389*0b57cec5SDimitry Andric PD.getDiagID(), Loc) >= DiagnosticsEngine::Warning; 1390*0b57cec5SDimitry Andric DiagnosticBuilder Builder(S.Diags.Report(Loc, PD.getDiagID())); 1391*0b57cec5SDimitry Andric Builder.setForceEmit(); 1392*0b57cec5SDimitry Andric PD.Emit(Builder); 1393*0b57cec5SDimitry Andric } 1394*0b57cec5SDimitry Andric S.DeviceDeferredDiags.erase(It); 1395*0b57cec5SDimitry Andric 1396*0b57cec5SDimitry Andric // FIXME: Should this be called after every warning/error emitted in the loop 1397*0b57cec5SDimitry Andric // above, instead of just once per function? That would be consistent with 1398*0b57cec5SDimitry Andric // how we handle immediate errors, but it also seems like a bit much. 1399*0b57cec5SDimitry Andric if (HasWarningOrError) 1400*0b57cec5SDimitry Andric emitCallStackNotes(S, FD); 1401*0b57cec5SDimitry Andric } 1402*0b57cec5SDimitry Andric 1403*0b57cec5SDimitry Andric // In CUDA, there are some constructs which may appear in semantically-valid 1404*0b57cec5SDimitry Andric // code, but trigger errors if we ever generate code for the function in which 1405*0b57cec5SDimitry Andric // they appear. Essentially every construct you're not allowed to use on the 1406*0b57cec5SDimitry Andric // device falls into this category, because you are allowed to use these 1407*0b57cec5SDimitry Andric // constructs in a __host__ __device__ function, but only if that function is 1408*0b57cec5SDimitry Andric // never codegen'ed on the device. 1409*0b57cec5SDimitry Andric // 1410*0b57cec5SDimitry Andric // To handle semantic checking for these constructs, we keep track of the set of 1411*0b57cec5SDimitry Andric // functions we know will be emitted, either because we could tell a priori that 1412*0b57cec5SDimitry Andric // they would be emitted, or because they were transitively called by a 1413*0b57cec5SDimitry Andric // known-emitted function. 1414*0b57cec5SDimitry Andric // 1415*0b57cec5SDimitry Andric // We also keep a partial call graph of which not-known-emitted functions call 1416*0b57cec5SDimitry Andric // which other not-known-emitted functions. 1417*0b57cec5SDimitry Andric // 1418*0b57cec5SDimitry Andric // When we see something which is illegal if the current function is emitted 1419*0b57cec5SDimitry Andric // (usually by way of CUDADiagIfDeviceCode, CUDADiagIfHostCode, or 1420*0b57cec5SDimitry Andric // CheckCUDACall), we first check if the current function is known-emitted. If 1421*0b57cec5SDimitry Andric // so, we immediately output the diagnostic. 1422*0b57cec5SDimitry Andric // 1423*0b57cec5SDimitry Andric // Otherwise, we "defer" the diagnostic. It sits in Sema::DeviceDeferredDiags 1424*0b57cec5SDimitry Andric // until we discover that the function is known-emitted, at which point we take 1425*0b57cec5SDimitry Andric // it out of this map and emit the diagnostic. 1426*0b57cec5SDimitry Andric 1427*0b57cec5SDimitry Andric Sema::DeviceDiagBuilder::DeviceDiagBuilder(Kind K, SourceLocation Loc, 1428*0b57cec5SDimitry Andric unsigned DiagID, FunctionDecl *Fn, 1429*0b57cec5SDimitry Andric Sema &S) 1430*0b57cec5SDimitry Andric : S(S), Loc(Loc), DiagID(DiagID), Fn(Fn), 1431*0b57cec5SDimitry Andric ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) { 1432*0b57cec5SDimitry Andric switch (K) { 1433*0b57cec5SDimitry Andric case K_Nop: 1434*0b57cec5SDimitry Andric break; 1435*0b57cec5SDimitry Andric case K_Immediate: 1436*0b57cec5SDimitry Andric case K_ImmediateWithCallStack: 1437*0b57cec5SDimitry Andric ImmediateDiag.emplace(S.Diag(Loc, DiagID)); 1438*0b57cec5SDimitry Andric break; 1439*0b57cec5SDimitry Andric case K_Deferred: 1440*0b57cec5SDimitry Andric assert(Fn && "Must have a function to attach the deferred diag to."); 1441*0b57cec5SDimitry Andric auto &Diags = S.DeviceDeferredDiags[Fn]; 1442*0b57cec5SDimitry Andric PartialDiagId.emplace(Diags.size()); 1443*0b57cec5SDimitry Andric Diags.emplace_back(Loc, S.PDiag(DiagID)); 1444*0b57cec5SDimitry Andric break; 1445*0b57cec5SDimitry Andric } 1446*0b57cec5SDimitry Andric } 1447*0b57cec5SDimitry Andric 1448*0b57cec5SDimitry Andric Sema::DeviceDiagBuilder::DeviceDiagBuilder(DeviceDiagBuilder &&D) 1449*0b57cec5SDimitry Andric : S(D.S), Loc(D.Loc), DiagID(D.DiagID), Fn(D.Fn), 1450*0b57cec5SDimitry Andric ShowCallStack(D.ShowCallStack), ImmediateDiag(D.ImmediateDiag), 1451*0b57cec5SDimitry Andric PartialDiagId(D.PartialDiagId) { 1452*0b57cec5SDimitry Andric // Clean the previous diagnostics. 1453*0b57cec5SDimitry Andric D.ShowCallStack = false; 1454*0b57cec5SDimitry Andric D.ImmediateDiag.reset(); 1455*0b57cec5SDimitry Andric D.PartialDiagId.reset(); 1456*0b57cec5SDimitry Andric } 1457*0b57cec5SDimitry Andric 1458*0b57cec5SDimitry Andric Sema::DeviceDiagBuilder::~DeviceDiagBuilder() { 1459*0b57cec5SDimitry Andric if (ImmediateDiag) { 1460*0b57cec5SDimitry Andric // Emit our diagnostic and, if it was a warning or error, output a callstack 1461*0b57cec5SDimitry Andric // if Fn isn't a priori known-emitted. 1462*0b57cec5SDimitry Andric bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel( 1463*0b57cec5SDimitry Andric DiagID, Loc) >= DiagnosticsEngine::Warning; 1464*0b57cec5SDimitry Andric ImmediateDiag.reset(); // Emit the immediate diag. 1465*0b57cec5SDimitry Andric if (IsWarningOrError && ShowCallStack) 1466*0b57cec5SDimitry Andric emitCallStackNotes(S, Fn); 1467*0b57cec5SDimitry Andric } else { 1468*0b57cec5SDimitry Andric assert((!PartialDiagId || ShowCallStack) && 1469*0b57cec5SDimitry Andric "Must always show call stack for deferred diags."); 1470*0b57cec5SDimitry Andric } 1471*0b57cec5SDimitry Andric } 1472*0b57cec5SDimitry Andric 1473*0b57cec5SDimitry Andric // Indicate that this function (and thus everything it transtively calls) will 1474*0b57cec5SDimitry Andric // be codegen'ed, and emit any deferred diagnostics on this function and its 1475*0b57cec5SDimitry Andric // (transitive) callees. 1476*0b57cec5SDimitry Andric void Sema::markKnownEmitted( 1477*0b57cec5SDimitry Andric Sema &S, FunctionDecl *OrigCaller, FunctionDecl *OrigCallee, 1478*0b57cec5SDimitry Andric SourceLocation OrigLoc, 1479*0b57cec5SDimitry Andric const llvm::function_ref<bool(Sema &, FunctionDecl *)> IsKnownEmitted) { 1480*0b57cec5SDimitry Andric // Nothing to do if we already know that FD is emitted. 1481*0b57cec5SDimitry Andric if (IsKnownEmitted(S, OrigCallee)) { 1482*0b57cec5SDimitry Andric assert(!S.DeviceCallGraph.count(OrigCallee)); 1483*0b57cec5SDimitry Andric return; 1484*0b57cec5SDimitry Andric } 1485*0b57cec5SDimitry Andric 1486*0b57cec5SDimitry Andric // We've just discovered that OrigCallee is known-emitted. Walk our call 1487*0b57cec5SDimitry Andric // graph to see what else we can now discover also must be emitted. 1488*0b57cec5SDimitry Andric 1489*0b57cec5SDimitry Andric struct CallInfo { 1490*0b57cec5SDimitry Andric FunctionDecl *Caller; 1491*0b57cec5SDimitry Andric FunctionDecl *Callee; 1492*0b57cec5SDimitry Andric SourceLocation Loc; 1493*0b57cec5SDimitry Andric }; 1494*0b57cec5SDimitry Andric llvm::SmallVector<CallInfo, 4> Worklist = {{OrigCaller, OrigCallee, OrigLoc}}; 1495*0b57cec5SDimitry Andric llvm::SmallSet<CanonicalDeclPtr<FunctionDecl>, 4> Seen; 1496*0b57cec5SDimitry Andric Seen.insert(OrigCallee); 1497*0b57cec5SDimitry Andric while (!Worklist.empty()) { 1498*0b57cec5SDimitry Andric CallInfo C = Worklist.pop_back_val(); 1499*0b57cec5SDimitry Andric assert(!IsKnownEmitted(S, C.Callee) && 1500*0b57cec5SDimitry Andric "Worklist should not contain known-emitted functions."); 1501*0b57cec5SDimitry Andric S.DeviceKnownEmittedFns[C.Callee] = {C.Caller, C.Loc}; 1502*0b57cec5SDimitry Andric emitDeferredDiags(S, C.Callee); 1503*0b57cec5SDimitry Andric 1504*0b57cec5SDimitry Andric // If this is a template instantiation, explore its callgraph as well: 1505*0b57cec5SDimitry Andric // Non-dependent calls are part of the template's callgraph, while dependent 1506*0b57cec5SDimitry Andric // calls are part of to the instantiation's call graph. 1507*0b57cec5SDimitry Andric if (auto *Templ = C.Callee->getPrimaryTemplate()) { 1508*0b57cec5SDimitry Andric FunctionDecl *TemplFD = Templ->getAsFunction(); 1509*0b57cec5SDimitry Andric if (!Seen.count(TemplFD) && !S.DeviceKnownEmittedFns.count(TemplFD)) { 1510*0b57cec5SDimitry Andric Seen.insert(TemplFD); 1511*0b57cec5SDimitry Andric Worklist.push_back( 1512*0b57cec5SDimitry Andric {/* Caller = */ C.Caller, /* Callee = */ TemplFD, C.Loc}); 1513*0b57cec5SDimitry Andric } 1514*0b57cec5SDimitry Andric } 1515*0b57cec5SDimitry Andric 1516*0b57cec5SDimitry Andric // Add all functions called by Callee to our worklist. 1517*0b57cec5SDimitry Andric auto CGIt = S.DeviceCallGraph.find(C.Callee); 1518*0b57cec5SDimitry Andric if (CGIt == S.DeviceCallGraph.end()) 1519*0b57cec5SDimitry Andric continue; 1520*0b57cec5SDimitry Andric 1521*0b57cec5SDimitry Andric for (std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation> FDLoc : 1522*0b57cec5SDimitry Andric CGIt->second) { 1523*0b57cec5SDimitry Andric FunctionDecl *NewCallee = FDLoc.first; 1524*0b57cec5SDimitry Andric SourceLocation CallLoc = FDLoc.second; 1525*0b57cec5SDimitry Andric if (Seen.count(NewCallee) || IsKnownEmitted(S, NewCallee)) 1526*0b57cec5SDimitry Andric continue; 1527*0b57cec5SDimitry Andric Seen.insert(NewCallee); 1528*0b57cec5SDimitry Andric Worklist.push_back( 1529*0b57cec5SDimitry Andric {/* Caller = */ C.Callee, /* Callee = */ NewCallee, CallLoc}); 1530*0b57cec5SDimitry Andric } 1531*0b57cec5SDimitry Andric 1532*0b57cec5SDimitry Andric // C.Callee is now known-emitted, so we no longer need to maintain its list 1533*0b57cec5SDimitry Andric // of callees in DeviceCallGraph. 1534*0b57cec5SDimitry Andric S.DeviceCallGraph.erase(CGIt); 1535*0b57cec5SDimitry Andric } 1536*0b57cec5SDimitry Andric } 1537*0b57cec5SDimitry Andric 1538*0b57cec5SDimitry Andric Sema::DeviceDiagBuilder Sema::targetDiag(SourceLocation Loc, unsigned DiagID) { 1539*0b57cec5SDimitry Andric if (LangOpts.OpenMP && LangOpts.OpenMPIsDevice) 1540*0b57cec5SDimitry Andric return diagIfOpenMPDeviceCode(Loc, DiagID); 1541*0b57cec5SDimitry Andric if (getLangOpts().CUDA) 1542*0b57cec5SDimitry Andric return getLangOpts().CUDAIsDevice ? CUDADiagIfDeviceCode(Loc, DiagID) 1543*0b57cec5SDimitry Andric : CUDADiagIfHostCode(Loc, DiagID); 1544*0b57cec5SDimitry Andric return DeviceDiagBuilder(DeviceDiagBuilder::K_Immediate, Loc, DiagID, 1545*0b57cec5SDimitry Andric getCurFunctionDecl(), *this); 1546*0b57cec5SDimitry Andric } 1547*0b57cec5SDimitry Andric 1548*0b57cec5SDimitry Andric /// Looks through the macro-expansion chain for the given 1549*0b57cec5SDimitry Andric /// location, looking for a macro expansion with the given name. 1550*0b57cec5SDimitry Andric /// If one is found, returns true and sets the location to that 1551*0b57cec5SDimitry Andric /// expansion loc. 1552*0b57cec5SDimitry Andric bool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) { 1553*0b57cec5SDimitry Andric SourceLocation loc = locref; 1554*0b57cec5SDimitry Andric if (!loc.isMacroID()) return false; 1555*0b57cec5SDimitry Andric 1556*0b57cec5SDimitry Andric // There's no good way right now to look at the intermediate 1557*0b57cec5SDimitry Andric // expansions, so just jump to the expansion location. 1558*0b57cec5SDimitry Andric loc = getSourceManager().getExpansionLoc(loc); 1559*0b57cec5SDimitry Andric 1560*0b57cec5SDimitry Andric // If that's written with the name, stop here. 1561*0b57cec5SDimitry Andric SmallVector<char, 16> buffer; 1562*0b57cec5SDimitry Andric if (getPreprocessor().getSpelling(loc, buffer) == name) { 1563*0b57cec5SDimitry Andric locref = loc; 1564*0b57cec5SDimitry Andric return true; 1565*0b57cec5SDimitry Andric } 1566*0b57cec5SDimitry Andric return false; 1567*0b57cec5SDimitry Andric } 1568*0b57cec5SDimitry Andric 1569*0b57cec5SDimitry Andric /// Determines the active Scope associated with the given declaration 1570*0b57cec5SDimitry Andric /// context. 1571*0b57cec5SDimitry Andric /// 1572*0b57cec5SDimitry Andric /// This routine maps a declaration context to the active Scope object that 1573*0b57cec5SDimitry Andric /// represents that declaration context in the parser. It is typically used 1574*0b57cec5SDimitry Andric /// from "scope-less" code (e.g., template instantiation, lazy creation of 1575*0b57cec5SDimitry Andric /// declarations) that injects a name for name-lookup purposes and, therefore, 1576*0b57cec5SDimitry Andric /// must update the Scope. 1577*0b57cec5SDimitry Andric /// 1578*0b57cec5SDimitry Andric /// \returns The scope corresponding to the given declaraion context, or NULL 1579*0b57cec5SDimitry Andric /// if no such scope is open. 1580*0b57cec5SDimitry Andric Scope *Sema::getScopeForContext(DeclContext *Ctx) { 1581*0b57cec5SDimitry Andric 1582*0b57cec5SDimitry Andric if (!Ctx) 1583*0b57cec5SDimitry Andric return nullptr; 1584*0b57cec5SDimitry Andric 1585*0b57cec5SDimitry Andric Ctx = Ctx->getPrimaryContext(); 1586*0b57cec5SDimitry Andric for (Scope *S = getCurScope(); S; S = S->getParent()) { 1587*0b57cec5SDimitry Andric // Ignore scopes that cannot have declarations. This is important for 1588*0b57cec5SDimitry Andric // out-of-line definitions of static class members. 1589*0b57cec5SDimitry Andric if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) 1590*0b57cec5SDimitry Andric if (DeclContext *Entity = S->getEntity()) 1591*0b57cec5SDimitry Andric if (Ctx == Entity->getPrimaryContext()) 1592*0b57cec5SDimitry Andric return S; 1593*0b57cec5SDimitry Andric } 1594*0b57cec5SDimitry Andric 1595*0b57cec5SDimitry Andric return nullptr; 1596*0b57cec5SDimitry Andric } 1597*0b57cec5SDimitry Andric 1598*0b57cec5SDimitry Andric /// Enter a new function scope 1599*0b57cec5SDimitry Andric void Sema::PushFunctionScope() { 1600*0b57cec5SDimitry Andric if (FunctionScopes.empty() && CachedFunctionScope) { 1601*0b57cec5SDimitry Andric // Use CachedFunctionScope to avoid allocating memory when possible. 1602*0b57cec5SDimitry Andric CachedFunctionScope->Clear(); 1603*0b57cec5SDimitry Andric FunctionScopes.push_back(CachedFunctionScope.release()); 1604*0b57cec5SDimitry Andric } else { 1605*0b57cec5SDimitry Andric FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics())); 1606*0b57cec5SDimitry Andric } 1607*0b57cec5SDimitry Andric if (LangOpts.OpenMP) 1608*0b57cec5SDimitry Andric pushOpenMPFunctionRegion(); 1609*0b57cec5SDimitry Andric } 1610*0b57cec5SDimitry Andric 1611*0b57cec5SDimitry Andric void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { 1612*0b57cec5SDimitry Andric FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(), 1613*0b57cec5SDimitry Andric BlockScope, Block)); 1614*0b57cec5SDimitry Andric } 1615*0b57cec5SDimitry Andric 1616*0b57cec5SDimitry Andric LambdaScopeInfo *Sema::PushLambdaScope() { 1617*0b57cec5SDimitry Andric LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics()); 1618*0b57cec5SDimitry Andric FunctionScopes.push_back(LSI); 1619*0b57cec5SDimitry Andric return LSI; 1620*0b57cec5SDimitry Andric } 1621*0b57cec5SDimitry Andric 1622*0b57cec5SDimitry Andric void Sema::RecordParsingTemplateParameterDepth(unsigned Depth) { 1623*0b57cec5SDimitry Andric if (LambdaScopeInfo *const LSI = getCurLambda()) { 1624*0b57cec5SDimitry Andric LSI->AutoTemplateParameterDepth = Depth; 1625*0b57cec5SDimitry Andric return; 1626*0b57cec5SDimitry Andric } 1627*0b57cec5SDimitry Andric llvm_unreachable( 1628*0b57cec5SDimitry Andric "Remove assertion if intentionally called in a non-lambda context."); 1629*0b57cec5SDimitry Andric } 1630*0b57cec5SDimitry Andric 1631*0b57cec5SDimitry Andric // Check that the type of the VarDecl has an accessible copy constructor and 1632*0b57cec5SDimitry Andric // resolve its destructor's exception specification. 1633*0b57cec5SDimitry Andric static void checkEscapingByref(VarDecl *VD, Sema &S) { 1634*0b57cec5SDimitry Andric QualType T = VD->getType(); 1635*0b57cec5SDimitry Andric EnterExpressionEvaluationContext scope( 1636*0b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); 1637*0b57cec5SDimitry Andric SourceLocation Loc = VD->getLocation(); 1638*0b57cec5SDimitry Andric Expr *VarRef = 1639*0b57cec5SDimitry Andric new (S.Context) DeclRefExpr(S.Context, VD, false, T, VK_LValue, Loc); 1640*0b57cec5SDimitry Andric ExprResult Result = S.PerformMoveOrCopyInitialization( 1641*0b57cec5SDimitry Andric InitializedEntity::InitializeBlock(Loc, T, false), VD, VD->getType(), 1642*0b57cec5SDimitry Andric VarRef, /*AllowNRVO=*/true); 1643*0b57cec5SDimitry Andric if (!Result.isInvalid()) { 1644*0b57cec5SDimitry Andric Result = S.MaybeCreateExprWithCleanups(Result); 1645*0b57cec5SDimitry Andric Expr *Init = Result.getAs<Expr>(); 1646*0b57cec5SDimitry Andric S.Context.setBlockVarCopyInit(VD, Init, S.canThrow(Init)); 1647*0b57cec5SDimitry Andric } 1648*0b57cec5SDimitry Andric 1649*0b57cec5SDimitry Andric // The destructor's exception specification is needed when IRGen generates 1650*0b57cec5SDimitry Andric // block copy/destroy functions. Resolve it here. 1651*0b57cec5SDimitry Andric if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) 1652*0b57cec5SDimitry Andric if (CXXDestructorDecl *DD = RD->getDestructor()) { 1653*0b57cec5SDimitry Andric auto *FPT = DD->getType()->getAs<FunctionProtoType>(); 1654*0b57cec5SDimitry Andric S.ResolveExceptionSpec(Loc, FPT); 1655*0b57cec5SDimitry Andric } 1656*0b57cec5SDimitry Andric } 1657*0b57cec5SDimitry Andric 1658*0b57cec5SDimitry Andric static void markEscapingByrefs(const FunctionScopeInfo &FSI, Sema &S) { 1659*0b57cec5SDimitry Andric // Set the EscapingByref flag of __block variables captured by 1660*0b57cec5SDimitry Andric // escaping blocks. 1661*0b57cec5SDimitry Andric for (const BlockDecl *BD : FSI.Blocks) { 1662*0b57cec5SDimitry Andric for (const BlockDecl::Capture &BC : BD->captures()) { 1663*0b57cec5SDimitry Andric VarDecl *VD = BC.getVariable(); 1664*0b57cec5SDimitry Andric if (VD->hasAttr<BlocksAttr>()) { 1665*0b57cec5SDimitry Andric // Nothing to do if this is a __block variable captured by a 1666*0b57cec5SDimitry Andric // non-escaping block. 1667*0b57cec5SDimitry Andric if (BD->doesNotEscape()) 1668*0b57cec5SDimitry Andric continue; 1669*0b57cec5SDimitry Andric VD->setEscapingByref(); 1670*0b57cec5SDimitry Andric } 1671*0b57cec5SDimitry Andric // Check whether the captured variable is or contains an object of 1672*0b57cec5SDimitry Andric // non-trivial C union type. 1673*0b57cec5SDimitry Andric QualType CapType = BC.getVariable()->getType(); 1674*0b57cec5SDimitry Andric if (CapType.hasNonTrivialToPrimitiveDestructCUnion() || 1675*0b57cec5SDimitry Andric CapType.hasNonTrivialToPrimitiveCopyCUnion()) 1676*0b57cec5SDimitry Andric S.checkNonTrivialCUnion(BC.getVariable()->getType(), 1677*0b57cec5SDimitry Andric BD->getCaretLocation(), 1678*0b57cec5SDimitry Andric Sema::NTCUC_BlockCapture, 1679*0b57cec5SDimitry Andric Sema::NTCUK_Destruct|Sema::NTCUK_Copy); 1680*0b57cec5SDimitry Andric } 1681*0b57cec5SDimitry Andric } 1682*0b57cec5SDimitry Andric 1683*0b57cec5SDimitry Andric for (VarDecl *VD : FSI.ByrefBlockVars) { 1684*0b57cec5SDimitry Andric // __block variables might require us to capture a copy-initializer. 1685*0b57cec5SDimitry Andric if (!VD->isEscapingByref()) 1686*0b57cec5SDimitry Andric continue; 1687*0b57cec5SDimitry Andric // It's currently invalid to ever have a __block variable with an 1688*0b57cec5SDimitry Andric // array type; should we diagnose that here? 1689*0b57cec5SDimitry Andric // Regardless, we don't want to ignore array nesting when 1690*0b57cec5SDimitry Andric // constructing this copy. 1691*0b57cec5SDimitry Andric if (VD->getType()->isStructureOrClassType()) 1692*0b57cec5SDimitry Andric checkEscapingByref(VD, S); 1693*0b57cec5SDimitry Andric } 1694*0b57cec5SDimitry Andric } 1695*0b57cec5SDimitry Andric 1696*0b57cec5SDimitry Andric /// Pop a function (or block or lambda or captured region) scope from the stack. 1697*0b57cec5SDimitry Andric /// 1698*0b57cec5SDimitry Andric /// \param WP The warning policy to use for CFG-based warnings, or null if such 1699*0b57cec5SDimitry Andric /// warnings should not be produced. 1700*0b57cec5SDimitry Andric /// \param D The declaration corresponding to this function scope, if producing 1701*0b57cec5SDimitry Andric /// CFG-based warnings. 1702*0b57cec5SDimitry Andric /// \param BlockType The type of the block expression, if D is a BlockDecl. 1703*0b57cec5SDimitry Andric Sema::PoppedFunctionScopePtr 1704*0b57cec5SDimitry Andric Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP, 1705*0b57cec5SDimitry Andric const Decl *D, QualType BlockType) { 1706*0b57cec5SDimitry Andric assert(!FunctionScopes.empty() && "mismatched push/pop!"); 1707*0b57cec5SDimitry Andric 1708*0b57cec5SDimitry Andric markEscapingByrefs(*FunctionScopes.back(), *this); 1709*0b57cec5SDimitry Andric 1710*0b57cec5SDimitry Andric PoppedFunctionScopePtr Scope(FunctionScopes.pop_back_val(), 1711*0b57cec5SDimitry Andric PoppedFunctionScopeDeleter(this)); 1712*0b57cec5SDimitry Andric 1713*0b57cec5SDimitry Andric if (LangOpts.OpenMP) 1714*0b57cec5SDimitry Andric popOpenMPFunctionRegion(Scope.get()); 1715*0b57cec5SDimitry Andric 1716*0b57cec5SDimitry Andric // Issue any analysis-based warnings. 1717*0b57cec5SDimitry Andric if (WP && D) 1718*0b57cec5SDimitry Andric AnalysisWarnings.IssueWarnings(*WP, Scope.get(), D, BlockType); 1719*0b57cec5SDimitry Andric else 1720*0b57cec5SDimitry Andric for (const auto &PUD : Scope->PossiblyUnreachableDiags) 1721*0b57cec5SDimitry Andric Diag(PUD.Loc, PUD.PD); 1722*0b57cec5SDimitry Andric 1723*0b57cec5SDimitry Andric return Scope; 1724*0b57cec5SDimitry Andric } 1725*0b57cec5SDimitry Andric 1726*0b57cec5SDimitry Andric void Sema::PoppedFunctionScopeDeleter:: 1727*0b57cec5SDimitry Andric operator()(sema::FunctionScopeInfo *Scope) const { 1728*0b57cec5SDimitry Andric // Stash the function scope for later reuse if it's for a normal function. 1729*0b57cec5SDimitry Andric if (Scope->isPlainFunction() && !Self->CachedFunctionScope) 1730*0b57cec5SDimitry Andric Self->CachedFunctionScope.reset(Scope); 1731*0b57cec5SDimitry Andric else 1732*0b57cec5SDimitry Andric delete Scope; 1733*0b57cec5SDimitry Andric } 1734*0b57cec5SDimitry Andric 1735*0b57cec5SDimitry Andric void Sema::PushCompoundScope(bool IsStmtExpr) { 1736*0b57cec5SDimitry Andric getCurFunction()->CompoundScopes.push_back(CompoundScopeInfo(IsStmtExpr)); 1737*0b57cec5SDimitry Andric } 1738*0b57cec5SDimitry Andric 1739*0b57cec5SDimitry Andric void Sema::PopCompoundScope() { 1740*0b57cec5SDimitry Andric FunctionScopeInfo *CurFunction = getCurFunction(); 1741*0b57cec5SDimitry Andric assert(!CurFunction->CompoundScopes.empty() && "mismatched push/pop"); 1742*0b57cec5SDimitry Andric 1743*0b57cec5SDimitry Andric CurFunction->CompoundScopes.pop_back(); 1744*0b57cec5SDimitry Andric } 1745*0b57cec5SDimitry Andric 1746*0b57cec5SDimitry Andric /// Determine whether any errors occurred within this function/method/ 1747*0b57cec5SDimitry Andric /// block. 1748*0b57cec5SDimitry Andric bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const { 1749*0b57cec5SDimitry Andric return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred(); 1750*0b57cec5SDimitry Andric } 1751*0b57cec5SDimitry Andric 1752*0b57cec5SDimitry Andric void Sema::setFunctionHasBranchIntoScope() { 1753*0b57cec5SDimitry Andric if (!FunctionScopes.empty()) 1754*0b57cec5SDimitry Andric FunctionScopes.back()->setHasBranchIntoScope(); 1755*0b57cec5SDimitry Andric } 1756*0b57cec5SDimitry Andric 1757*0b57cec5SDimitry Andric void Sema::setFunctionHasBranchProtectedScope() { 1758*0b57cec5SDimitry Andric if (!FunctionScopes.empty()) 1759*0b57cec5SDimitry Andric FunctionScopes.back()->setHasBranchProtectedScope(); 1760*0b57cec5SDimitry Andric } 1761*0b57cec5SDimitry Andric 1762*0b57cec5SDimitry Andric void Sema::setFunctionHasIndirectGoto() { 1763*0b57cec5SDimitry Andric if (!FunctionScopes.empty()) 1764*0b57cec5SDimitry Andric FunctionScopes.back()->setHasIndirectGoto(); 1765*0b57cec5SDimitry Andric } 1766*0b57cec5SDimitry Andric 1767*0b57cec5SDimitry Andric BlockScopeInfo *Sema::getCurBlock() { 1768*0b57cec5SDimitry Andric if (FunctionScopes.empty()) 1769*0b57cec5SDimitry Andric return nullptr; 1770*0b57cec5SDimitry Andric 1771*0b57cec5SDimitry Andric auto CurBSI = dyn_cast<BlockScopeInfo>(FunctionScopes.back()); 1772*0b57cec5SDimitry Andric if (CurBSI && CurBSI->TheDecl && 1773*0b57cec5SDimitry Andric !CurBSI->TheDecl->Encloses(CurContext)) { 1774*0b57cec5SDimitry Andric // We have switched contexts due to template instantiation. 1775*0b57cec5SDimitry Andric assert(!CodeSynthesisContexts.empty()); 1776*0b57cec5SDimitry Andric return nullptr; 1777*0b57cec5SDimitry Andric } 1778*0b57cec5SDimitry Andric 1779*0b57cec5SDimitry Andric return CurBSI; 1780*0b57cec5SDimitry Andric } 1781*0b57cec5SDimitry Andric 1782*0b57cec5SDimitry Andric FunctionScopeInfo *Sema::getEnclosingFunction() const { 1783*0b57cec5SDimitry Andric if (FunctionScopes.empty()) 1784*0b57cec5SDimitry Andric return nullptr; 1785*0b57cec5SDimitry Andric 1786*0b57cec5SDimitry Andric for (int e = FunctionScopes.size() - 1; e >= 0; --e) { 1787*0b57cec5SDimitry Andric if (isa<sema::BlockScopeInfo>(FunctionScopes[e])) 1788*0b57cec5SDimitry Andric continue; 1789*0b57cec5SDimitry Andric return FunctionScopes[e]; 1790*0b57cec5SDimitry Andric } 1791*0b57cec5SDimitry Andric return nullptr; 1792*0b57cec5SDimitry Andric } 1793*0b57cec5SDimitry Andric 1794*0b57cec5SDimitry Andric LambdaScopeInfo *Sema::getCurLambda(bool IgnoreNonLambdaCapturingScope) { 1795*0b57cec5SDimitry Andric if (FunctionScopes.empty()) 1796*0b57cec5SDimitry Andric return nullptr; 1797*0b57cec5SDimitry Andric 1798*0b57cec5SDimitry Andric auto I = FunctionScopes.rbegin(); 1799*0b57cec5SDimitry Andric if (IgnoreNonLambdaCapturingScope) { 1800*0b57cec5SDimitry Andric auto E = FunctionScopes.rend(); 1801*0b57cec5SDimitry Andric while (I != E && isa<CapturingScopeInfo>(*I) && !isa<LambdaScopeInfo>(*I)) 1802*0b57cec5SDimitry Andric ++I; 1803*0b57cec5SDimitry Andric if (I == E) 1804*0b57cec5SDimitry Andric return nullptr; 1805*0b57cec5SDimitry Andric } 1806*0b57cec5SDimitry Andric auto *CurLSI = dyn_cast<LambdaScopeInfo>(*I); 1807*0b57cec5SDimitry Andric if (CurLSI && CurLSI->Lambda && 1808*0b57cec5SDimitry Andric !CurLSI->Lambda->Encloses(CurContext)) { 1809*0b57cec5SDimitry Andric // We have switched contexts due to template instantiation. 1810*0b57cec5SDimitry Andric assert(!CodeSynthesisContexts.empty()); 1811*0b57cec5SDimitry Andric return nullptr; 1812*0b57cec5SDimitry Andric } 1813*0b57cec5SDimitry Andric 1814*0b57cec5SDimitry Andric return CurLSI; 1815*0b57cec5SDimitry Andric } 1816*0b57cec5SDimitry Andric // We have a generic lambda if we parsed auto parameters, or we have 1817*0b57cec5SDimitry Andric // an associated template parameter list. 1818*0b57cec5SDimitry Andric LambdaScopeInfo *Sema::getCurGenericLambda() { 1819*0b57cec5SDimitry Andric if (LambdaScopeInfo *LSI = getCurLambda()) { 1820*0b57cec5SDimitry Andric return (LSI->TemplateParams.size() || 1821*0b57cec5SDimitry Andric LSI->GLTemplateParameterList) ? LSI : nullptr; 1822*0b57cec5SDimitry Andric } 1823*0b57cec5SDimitry Andric return nullptr; 1824*0b57cec5SDimitry Andric } 1825*0b57cec5SDimitry Andric 1826*0b57cec5SDimitry Andric 1827*0b57cec5SDimitry Andric void Sema::ActOnComment(SourceRange Comment) { 1828*0b57cec5SDimitry Andric if (!LangOpts.RetainCommentsFromSystemHeaders && 1829*0b57cec5SDimitry Andric SourceMgr.isInSystemHeader(Comment.getBegin())) 1830*0b57cec5SDimitry Andric return; 1831*0b57cec5SDimitry Andric RawComment RC(SourceMgr, Comment, LangOpts.CommentOpts, false); 1832*0b57cec5SDimitry Andric if (RC.isAlmostTrailingComment()) { 1833*0b57cec5SDimitry Andric SourceRange MagicMarkerRange(Comment.getBegin(), 1834*0b57cec5SDimitry Andric Comment.getBegin().getLocWithOffset(3)); 1835*0b57cec5SDimitry Andric StringRef MagicMarkerText; 1836*0b57cec5SDimitry Andric switch (RC.getKind()) { 1837*0b57cec5SDimitry Andric case RawComment::RCK_OrdinaryBCPL: 1838*0b57cec5SDimitry Andric MagicMarkerText = "///<"; 1839*0b57cec5SDimitry Andric break; 1840*0b57cec5SDimitry Andric case RawComment::RCK_OrdinaryC: 1841*0b57cec5SDimitry Andric MagicMarkerText = "/**<"; 1842*0b57cec5SDimitry Andric break; 1843*0b57cec5SDimitry Andric default: 1844*0b57cec5SDimitry Andric llvm_unreachable("if this is an almost Doxygen comment, " 1845*0b57cec5SDimitry Andric "it should be ordinary"); 1846*0b57cec5SDimitry Andric } 1847*0b57cec5SDimitry Andric Diag(Comment.getBegin(), diag::warn_not_a_doxygen_trailing_member_comment) << 1848*0b57cec5SDimitry Andric FixItHint::CreateReplacement(MagicMarkerRange, MagicMarkerText); 1849*0b57cec5SDimitry Andric } 1850*0b57cec5SDimitry Andric Context.addComment(RC); 1851*0b57cec5SDimitry Andric } 1852*0b57cec5SDimitry Andric 1853*0b57cec5SDimitry Andric // Pin this vtable to this file. 1854*0b57cec5SDimitry Andric ExternalSemaSource::~ExternalSemaSource() {} 1855*0b57cec5SDimitry Andric 1856*0b57cec5SDimitry Andric void ExternalSemaSource::ReadMethodPool(Selector Sel) { } 1857*0b57cec5SDimitry Andric void ExternalSemaSource::updateOutOfDateSelector(Selector Sel) { } 1858*0b57cec5SDimitry Andric 1859*0b57cec5SDimitry Andric void ExternalSemaSource::ReadKnownNamespaces( 1860*0b57cec5SDimitry Andric SmallVectorImpl<NamespaceDecl *> &Namespaces) { 1861*0b57cec5SDimitry Andric } 1862*0b57cec5SDimitry Andric 1863*0b57cec5SDimitry Andric void ExternalSemaSource::ReadUndefinedButUsed( 1864*0b57cec5SDimitry Andric llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {} 1865*0b57cec5SDimitry Andric 1866*0b57cec5SDimitry Andric void ExternalSemaSource::ReadMismatchingDeleteExpressions(llvm::MapVector< 1867*0b57cec5SDimitry Andric FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &) {} 1868*0b57cec5SDimitry Andric 1869*0b57cec5SDimitry Andric /// Figure out if an expression could be turned into a call. 1870*0b57cec5SDimitry Andric /// 1871*0b57cec5SDimitry Andric /// Use this when trying to recover from an error where the programmer may have 1872*0b57cec5SDimitry Andric /// written just the name of a function instead of actually calling it. 1873*0b57cec5SDimitry Andric /// 1874*0b57cec5SDimitry Andric /// \param E - The expression to examine. 1875*0b57cec5SDimitry Andric /// \param ZeroArgCallReturnTy - If the expression can be turned into a call 1876*0b57cec5SDimitry Andric /// with no arguments, this parameter is set to the type returned by such a 1877*0b57cec5SDimitry Andric /// call; otherwise, it is set to an empty QualType. 1878*0b57cec5SDimitry Andric /// \param OverloadSet - If the expression is an overloaded function 1879*0b57cec5SDimitry Andric /// name, this parameter is populated with the decls of the various overloads. 1880*0b57cec5SDimitry Andric bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy, 1881*0b57cec5SDimitry Andric UnresolvedSetImpl &OverloadSet) { 1882*0b57cec5SDimitry Andric ZeroArgCallReturnTy = QualType(); 1883*0b57cec5SDimitry Andric OverloadSet.clear(); 1884*0b57cec5SDimitry Andric 1885*0b57cec5SDimitry Andric const OverloadExpr *Overloads = nullptr; 1886*0b57cec5SDimitry Andric bool IsMemExpr = false; 1887*0b57cec5SDimitry Andric if (E.getType() == Context.OverloadTy) { 1888*0b57cec5SDimitry Andric OverloadExpr::FindResult FR = OverloadExpr::find(const_cast<Expr*>(&E)); 1889*0b57cec5SDimitry Andric 1890*0b57cec5SDimitry Andric // Ignore overloads that are pointer-to-member constants. 1891*0b57cec5SDimitry Andric if (FR.HasFormOfMemberPointer) 1892*0b57cec5SDimitry Andric return false; 1893*0b57cec5SDimitry Andric 1894*0b57cec5SDimitry Andric Overloads = FR.Expression; 1895*0b57cec5SDimitry Andric } else if (E.getType() == Context.BoundMemberTy) { 1896*0b57cec5SDimitry Andric Overloads = dyn_cast<UnresolvedMemberExpr>(E.IgnoreParens()); 1897*0b57cec5SDimitry Andric IsMemExpr = true; 1898*0b57cec5SDimitry Andric } 1899*0b57cec5SDimitry Andric 1900*0b57cec5SDimitry Andric bool Ambiguous = false; 1901*0b57cec5SDimitry Andric bool IsMV = false; 1902*0b57cec5SDimitry Andric 1903*0b57cec5SDimitry Andric if (Overloads) { 1904*0b57cec5SDimitry Andric for (OverloadExpr::decls_iterator it = Overloads->decls_begin(), 1905*0b57cec5SDimitry Andric DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) { 1906*0b57cec5SDimitry Andric OverloadSet.addDecl(*it); 1907*0b57cec5SDimitry Andric 1908*0b57cec5SDimitry Andric // Check whether the function is a non-template, non-member which takes no 1909*0b57cec5SDimitry Andric // arguments. 1910*0b57cec5SDimitry Andric if (IsMemExpr) 1911*0b57cec5SDimitry Andric continue; 1912*0b57cec5SDimitry Andric if (const FunctionDecl *OverloadDecl 1913*0b57cec5SDimitry Andric = dyn_cast<FunctionDecl>((*it)->getUnderlyingDecl())) { 1914*0b57cec5SDimitry Andric if (OverloadDecl->getMinRequiredArguments() == 0) { 1915*0b57cec5SDimitry Andric if (!ZeroArgCallReturnTy.isNull() && !Ambiguous && 1916*0b57cec5SDimitry Andric (!IsMV || !(OverloadDecl->isCPUDispatchMultiVersion() || 1917*0b57cec5SDimitry Andric OverloadDecl->isCPUSpecificMultiVersion()))) { 1918*0b57cec5SDimitry Andric ZeroArgCallReturnTy = QualType(); 1919*0b57cec5SDimitry Andric Ambiguous = true; 1920*0b57cec5SDimitry Andric } else { 1921*0b57cec5SDimitry Andric ZeroArgCallReturnTy = OverloadDecl->getReturnType(); 1922*0b57cec5SDimitry Andric IsMV = OverloadDecl->isCPUDispatchMultiVersion() || 1923*0b57cec5SDimitry Andric OverloadDecl->isCPUSpecificMultiVersion(); 1924*0b57cec5SDimitry Andric } 1925*0b57cec5SDimitry Andric } 1926*0b57cec5SDimitry Andric } 1927*0b57cec5SDimitry Andric } 1928*0b57cec5SDimitry Andric 1929*0b57cec5SDimitry Andric // If it's not a member, use better machinery to try to resolve the call 1930*0b57cec5SDimitry Andric if (!IsMemExpr) 1931*0b57cec5SDimitry Andric return !ZeroArgCallReturnTy.isNull(); 1932*0b57cec5SDimitry Andric } 1933*0b57cec5SDimitry Andric 1934*0b57cec5SDimitry Andric // Attempt to call the member with no arguments - this will correctly handle 1935*0b57cec5SDimitry Andric // member templates with defaults/deduction of template arguments, overloads 1936*0b57cec5SDimitry Andric // with default arguments, etc. 1937*0b57cec5SDimitry Andric if (IsMemExpr && !E.isTypeDependent()) { 1938*0b57cec5SDimitry Andric bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 1939*0b57cec5SDimitry Andric getDiagnostics().setSuppressAllDiagnostics(true); 1940*0b57cec5SDimitry Andric ExprResult R = BuildCallToMemberFunction(nullptr, &E, SourceLocation(), 1941*0b57cec5SDimitry Andric None, SourceLocation()); 1942*0b57cec5SDimitry Andric getDiagnostics().setSuppressAllDiagnostics(Suppress); 1943*0b57cec5SDimitry Andric if (R.isUsable()) { 1944*0b57cec5SDimitry Andric ZeroArgCallReturnTy = R.get()->getType(); 1945*0b57cec5SDimitry Andric return true; 1946*0b57cec5SDimitry Andric } 1947*0b57cec5SDimitry Andric return false; 1948*0b57cec5SDimitry Andric } 1949*0b57cec5SDimitry Andric 1950*0b57cec5SDimitry Andric if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) { 1951*0b57cec5SDimitry Andric if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { 1952*0b57cec5SDimitry Andric if (Fun->getMinRequiredArguments() == 0) 1953*0b57cec5SDimitry Andric ZeroArgCallReturnTy = Fun->getReturnType(); 1954*0b57cec5SDimitry Andric return true; 1955*0b57cec5SDimitry Andric } 1956*0b57cec5SDimitry Andric } 1957*0b57cec5SDimitry Andric 1958*0b57cec5SDimitry Andric // We don't have an expression that's convenient to get a FunctionDecl from, 1959*0b57cec5SDimitry Andric // but we can at least check if the type is "function of 0 arguments". 1960*0b57cec5SDimitry Andric QualType ExprTy = E.getType(); 1961*0b57cec5SDimitry Andric const FunctionType *FunTy = nullptr; 1962*0b57cec5SDimitry Andric QualType PointeeTy = ExprTy->getPointeeType(); 1963*0b57cec5SDimitry Andric if (!PointeeTy.isNull()) 1964*0b57cec5SDimitry Andric FunTy = PointeeTy->getAs<FunctionType>(); 1965*0b57cec5SDimitry Andric if (!FunTy) 1966*0b57cec5SDimitry Andric FunTy = ExprTy->getAs<FunctionType>(); 1967*0b57cec5SDimitry Andric 1968*0b57cec5SDimitry Andric if (const FunctionProtoType *FPT = 1969*0b57cec5SDimitry Andric dyn_cast_or_null<FunctionProtoType>(FunTy)) { 1970*0b57cec5SDimitry Andric if (FPT->getNumParams() == 0) 1971*0b57cec5SDimitry Andric ZeroArgCallReturnTy = FunTy->getReturnType(); 1972*0b57cec5SDimitry Andric return true; 1973*0b57cec5SDimitry Andric } 1974*0b57cec5SDimitry Andric return false; 1975*0b57cec5SDimitry Andric } 1976*0b57cec5SDimitry Andric 1977*0b57cec5SDimitry Andric /// Give notes for a set of overloads. 1978*0b57cec5SDimitry Andric /// 1979*0b57cec5SDimitry Andric /// A companion to tryExprAsCall. In cases when the name that the programmer 1980*0b57cec5SDimitry Andric /// wrote was an overloaded function, we may be able to make some guesses about 1981*0b57cec5SDimitry Andric /// plausible overloads based on their return types; such guesses can be handed 1982*0b57cec5SDimitry Andric /// off to this method to be emitted as notes. 1983*0b57cec5SDimitry Andric /// 1984*0b57cec5SDimitry Andric /// \param Overloads - The overloads to note. 1985*0b57cec5SDimitry Andric /// \param FinalNoteLoc - If we've suppressed printing some overloads due to 1986*0b57cec5SDimitry Andric /// -fshow-overloads=best, this is the location to attach to the note about too 1987*0b57cec5SDimitry Andric /// many candidates. Typically this will be the location of the original 1988*0b57cec5SDimitry Andric /// ill-formed expression. 1989*0b57cec5SDimitry Andric static void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads, 1990*0b57cec5SDimitry Andric const SourceLocation FinalNoteLoc) { 1991*0b57cec5SDimitry Andric int ShownOverloads = 0; 1992*0b57cec5SDimitry Andric int SuppressedOverloads = 0; 1993*0b57cec5SDimitry Andric for (UnresolvedSetImpl::iterator It = Overloads.begin(), 1994*0b57cec5SDimitry Andric DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { 1995*0b57cec5SDimitry Andric // FIXME: Magic number for max shown overloads stolen from 1996*0b57cec5SDimitry Andric // OverloadCandidateSet::NoteCandidates. 1997*0b57cec5SDimitry Andric if (ShownOverloads >= 4 && S.Diags.getShowOverloads() == Ovl_Best) { 1998*0b57cec5SDimitry Andric ++SuppressedOverloads; 1999*0b57cec5SDimitry Andric continue; 2000*0b57cec5SDimitry Andric } 2001*0b57cec5SDimitry Andric 2002*0b57cec5SDimitry Andric NamedDecl *Fn = (*It)->getUnderlyingDecl(); 2003*0b57cec5SDimitry Andric // Don't print overloads for non-default multiversioned functions. 2004*0b57cec5SDimitry Andric if (const auto *FD = Fn->getAsFunction()) { 2005*0b57cec5SDimitry Andric if (FD->isMultiVersion() && FD->hasAttr<TargetAttr>() && 2006*0b57cec5SDimitry Andric !FD->getAttr<TargetAttr>()->isDefaultVersion()) 2007*0b57cec5SDimitry Andric continue; 2008*0b57cec5SDimitry Andric } 2009*0b57cec5SDimitry Andric S.Diag(Fn->getLocation(), diag::note_possible_target_of_call); 2010*0b57cec5SDimitry Andric ++ShownOverloads; 2011*0b57cec5SDimitry Andric } 2012*0b57cec5SDimitry Andric 2013*0b57cec5SDimitry Andric if (SuppressedOverloads) 2014*0b57cec5SDimitry Andric S.Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates) 2015*0b57cec5SDimitry Andric << SuppressedOverloads; 2016*0b57cec5SDimitry Andric } 2017*0b57cec5SDimitry Andric 2018*0b57cec5SDimitry Andric static void notePlausibleOverloads(Sema &S, SourceLocation Loc, 2019*0b57cec5SDimitry Andric const UnresolvedSetImpl &Overloads, 2020*0b57cec5SDimitry Andric bool (*IsPlausibleResult)(QualType)) { 2021*0b57cec5SDimitry Andric if (!IsPlausibleResult) 2022*0b57cec5SDimitry Andric return noteOverloads(S, Overloads, Loc); 2023*0b57cec5SDimitry Andric 2024*0b57cec5SDimitry Andric UnresolvedSet<2> PlausibleOverloads; 2025*0b57cec5SDimitry Andric for (OverloadExpr::decls_iterator It = Overloads.begin(), 2026*0b57cec5SDimitry Andric DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { 2027*0b57cec5SDimitry Andric const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It); 2028*0b57cec5SDimitry Andric QualType OverloadResultTy = OverloadDecl->getReturnType(); 2029*0b57cec5SDimitry Andric if (IsPlausibleResult(OverloadResultTy)) 2030*0b57cec5SDimitry Andric PlausibleOverloads.addDecl(It.getDecl()); 2031*0b57cec5SDimitry Andric } 2032*0b57cec5SDimitry Andric noteOverloads(S, PlausibleOverloads, Loc); 2033*0b57cec5SDimitry Andric } 2034*0b57cec5SDimitry Andric 2035*0b57cec5SDimitry Andric /// Determine whether the given expression can be called by just 2036*0b57cec5SDimitry Andric /// putting parentheses after it. Notably, expressions with unary 2037*0b57cec5SDimitry Andric /// operators can't be because the unary operator will start parsing 2038*0b57cec5SDimitry Andric /// outside the call. 2039*0b57cec5SDimitry Andric static bool IsCallableWithAppend(Expr *E) { 2040*0b57cec5SDimitry Andric E = E->IgnoreImplicit(); 2041*0b57cec5SDimitry Andric return (!isa<CStyleCastExpr>(E) && 2042*0b57cec5SDimitry Andric !isa<UnaryOperator>(E) && 2043*0b57cec5SDimitry Andric !isa<BinaryOperator>(E) && 2044*0b57cec5SDimitry Andric !isa<CXXOperatorCallExpr>(E)); 2045*0b57cec5SDimitry Andric } 2046*0b57cec5SDimitry Andric 2047*0b57cec5SDimitry Andric static bool IsCPUDispatchCPUSpecificMultiVersion(const Expr *E) { 2048*0b57cec5SDimitry Andric if (const auto *UO = dyn_cast<UnaryOperator>(E)) 2049*0b57cec5SDimitry Andric E = UO->getSubExpr(); 2050*0b57cec5SDimitry Andric 2051*0b57cec5SDimitry Andric if (const auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { 2052*0b57cec5SDimitry Andric if (ULE->getNumDecls() == 0) 2053*0b57cec5SDimitry Andric return false; 2054*0b57cec5SDimitry Andric 2055*0b57cec5SDimitry Andric const NamedDecl *ND = *ULE->decls_begin(); 2056*0b57cec5SDimitry Andric if (const auto *FD = dyn_cast<FunctionDecl>(ND)) 2057*0b57cec5SDimitry Andric return FD->isCPUDispatchMultiVersion() || FD->isCPUSpecificMultiVersion(); 2058*0b57cec5SDimitry Andric } 2059*0b57cec5SDimitry Andric return false; 2060*0b57cec5SDimitry Andric } 2061*0b57cec5SDimitry Andric 2062*0b57cec5SDimitry Andric bool Sema::tryToRecoverWithCall(ExprResult &E, const PartialDiagnostic &PD, 2063*0b57cec5SDimitry Andric bool ForceComplain, 2064*0b57cec5SDimitry Andric bool (*IsPlausibleResult)(QualType)) { 2065*0b57cec5SDimitry Andric SourceLocation Loc = E.get()->getExprLoc(); 2066*0b57cec5SDimitry Andric SourceRange Range = E.get()->getSourceRange(); 2067*0b57cec5SDimitry Andric 2068*0b57cec5SDimitry Andric QualType ZeroArgCallTy; 2069*0b57cec5SDimitry Andric UnresolvedSet<4> Overloads; 2070*0b57cec5SDimitry Andric if (tryExprAsCall(*E.get(), ZeroArgCallTy, Overloads) && 2071*0b57cec5SDimitry Andric !ZeroArgCallTy.isNull() && 2072*0b57cec5SDimitry Andric (!IsPlausibleResult || IsPlausibleResult(ZeroArgCallTy))) { 2073*0b57cec5SDimitry Andric // At this point, we know E is potentially callable with 0 2074*0b57cec5SDimitry Andric // arguments and that it returns something of a reasonable type, 2075*0b57cec5SDimitry Andric // so we can emit a fixit and carry on pretending that E was 2076*0b57cec5SDimitry Andric // actually a CallExpr. 2077*0b57cec5SDimitry Andric SourceLocation ParenInsertionLoc = getLocForEndOfToken(Range.getEnd()); 2078*0b57cec5SDimitry Andric bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get()); 2079*0b57cec5SDimitry Andric Diag(Loc, PD) << /*zero-arg*/ 1 << IsMV << Range 2080*0b57cec5SDimitry Andric << (IsCallableWithAppend(E.get()) 2081*0b57cec5SDimitry Andric ? FixItHint::CreateInsertion(ParenInsertionLoc, "()") 2082*0b57cec5SDimitry Andric : FixItHint()); 2083*0b57cec5SDimitry Andric if (!IsMV) 2084*0b57cec5SDimitry Andric notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult); 2085*0b57cec5SDimitry Andric 2086*0b57cec5SDimitry Andric // FIXME: Try this before emitting the fixit, and suppress diagnostics 2087*0b57cec5SDimitry Andric // while doing so. 2088*0b57cec5SDimitry Andric E = BuildCallExpr(nullptr, E.get(), Range.getEnd(), None, 2089*0b57cec5SDimitry Andric Range.getEnd().getLocWithOffset(1)); 2090*0b57cec5SDimitry Andric return true; 2091*0b57cec5SDimitry Andric } 2092*0b57cec5SDimitry Andric 2093*0b57cec5SDimitry Andric if (!ForceComplain) return false; 2094*0b57cec5SDimitry Andric 2095*0b57cec5SDimitry Andric bool IsMV = IsCPUDispatchCPUSpecificMultiVersion(E.get()); 2096*0b57cec5SDimitry Andric Diag(Loc, PD) << /*not zero-arg*/ 0 << IsMV << Range; 2097*0b57cec5SDimitry Andric if (!IsMV) 2098*0b57cec5SDimitry Andric notePlausibleOverloads(*this, Loc, Overloads, IsPlausibleResult); 2099*0b57cec5SDimitry Andric E = ExprError(); 2100*0b57cec5SDimitry Andric return true; 2101*0b57cec5SDimitry Andric } 2102*0b57cec5SDimitry Andric 2103*0b57cec5SDimitry Andric IdentifierInfo *Sema::getSuperIdentifier() const { 2104*0b57cec5SDimitry Andric if (!Ident_super) 2105*0b57cec5SDimitry Andric Ident_super = &Context.Idents.get("super"); 2106*0b57cec5SDimitry Andric return Ident_super; 2107*0b57cec5SDimitry Andric } 2108*0b57cec5SDimitry Andric 2109*0b57cec5SDimitry Andric IdentifierInfo *Sema::getFloat128Identifier() const { 2110*0b57cec5SDimitry Andric if (!Ident___float128) 2111*0b57cec5SDimitry Andric Ident___float128 = &Context.Idents.get("__float128"); 2112*0b57cec5SDimitry Andric return Ident___float128; 2113*0b57cec5SDimitry Andric } 2114*0b57cec5SDimitry Andric 2115*0b57cec5SDimitry Andric void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD, 2116*0b57cec5SDimitry Andric CapturedRegionKind K) { 2117*0b57cec5SDimitry Andric CapturingScopeInfo *CSI = new CapturedRegionScopeInfo( 2118*0b57cec5SDimitry Andric getDiagnostics(), S, CD, RD, CD->getContextParam(), K, 2119*0b57cec5SDimitry Andric (getLangOpts().OpenMP && K == CR_OpenMP) ? getOpenMPNestingLevel() : 0); 2120*0b57cec5SDimitry Andric CSI->ReturnType = Context.VoidTy; 2121*0b57cec5SDimitry Andric FunctionScopes.push_back(CSI); 2122*0b57cec5SDimitry Andric } 2123*0b57cec5SDimitry Andric 2124*0b57cec5SDimitry Andric CapturedRegionScopeInfo *Sema::getCurCapturedRegion() { 2125*0b57cec5SDimitry Andric if (FunctionScopes.empty()) 2126*0b57cec5SDimitry Andric return nullptr; 2127*0b57cec5SDimitry Andric 2128*0b57cec5SDimitry Andric return dyn_cast<CapturedRegionScopeInfo>(FunctionScopes.back()); 2129*0b57cec5SDimitry Andric } 2130*0b57cec5SDimitry Andric 2131*0b57cec5SDimitry Andric const llvm::MapVector<FieldDecl *, Sema::DeleteLocs> & 2132*0b57cec5SDimitry Andric Sema::getMismatchingDeleteExpressions() const { 2133*0b57cec5SDimitry Andric return DeleteExprs; 2134*0b57cec5SDimitry Andric } 2135*0b57cec5SDimitry Andric 2136*0b57cec5SDimitry Andric void Sema::setOpenCLExtensionForType(QualType T, llvm::StringRef ExtStr) { 2137*0b57cec5SDimitry Andric if (ExtStr.empty()) 2138*0b57cec5SDimitry Andric return; 2139*0b57cec5SDimitry Andric llvm::SmallVector<StringRef, 1> Exts; 2140*0b57cec5SDimitry Andric ExtStr.split(Exts, " ", /* limit */ -1, /* keep empty */ false); 2141*0b57cec5SDimitry Andric auto CanT = T.getCanonicalType().getTypePtr(); 2142*0b57cec5SDimitry Andric for (auto &I : Exts) 2143*0b57cec5SDimitry Andric OpenCLTypeExtMap[CanT].insert(I.str()); 2144*0b57cec5SDimitry Andric } 2145*0b57cec5SDimitry Andric 2146*0b57cec5SDimitry Andric void Sema::setOpenCLExtensionForDecl(Decl *FD, StringRef ExtStr) { 2147*0b57cec5SDimitry Andric llvm::SmallVector<StringRef, 1> Exts; 2148*0b57cec5SDimitry Andric ExtStr.split(Exts, " ", /* limit */ -1, /* keep empty */ false); 2149*0b57cec5SDimitry Andric if (Exts.empty()) 2150*0b57cec5SDimitry Andric return; 2151*0b57cec5SDimitry Andric for (auto &I : Exts) 2152*0b57cec5SDimitry Andric OpenCLDeclExtMap[FD].insert(I.str()); 2153*0b57cec5SDimitry Andric } 2154*0b57cec5SDimitry Andric 2155*0b57cec5SDimitry Andric void Sema::setCurrentOpenCLExtensionForType(QualType T) { 2156*0b57cec5SDimitry Andric if (CurrOpenCLExtension.empty()) 2157*0b57cec5SDimitry Andric return; 2158*0b57cec5SDimitry Andric setOpenCLExtensionForType(T, CurrOpenCLExtension); 2159*0b57cec5SDimitry Andric } 2160*0b57cec5SDimitry Andric 2161*0b57cec5SDimitry Andric void Sema::setCurrentOpenCLExtensionForDecl(Decl *D) { 2162*0b57cec5SDimitry Andric if (CurrOpenCLExtension.empty()) 2163*0b57cec5SDimitry Andric return; 2164*0b57cec5SDimitry Andric setOpenCLExtensionForDecl(D, CurrOpenCLExtension); 2165*0b57cec5SDimitry Andric } 2166*0b57cec5SDimitry Andric 2167*0b57cec5SDimitry Andric std::string Sema::getOpenCLExtensionsFromDeclExtMap(FunctionDecl *FD) { 2168*0b57cec5SDimitry Andric if (!OpenCLDeclExtMap.empty()) 2169*0b57cec5SDimitry Andric return getOpenCLExtensionsFromExtMap(FD, OpenCLDeclExtMap); 2170*0b57cec5SDimitry Andric 2171*0b57cec5SDimitry Andric return ""; 2172*0b57cec5SDimitry Andric } 2173*0b57cec5SDimitry Andric 2174*0b57cec5SDimitry Andric std::string Sema::getOpenCLExtensionsFromTypeExtMap(FunctionType *FT) { 2175*0b57cec5SDimitry Andric if (!OpenCLTypeExtMap.empty()) 2176*0b57cec5SDimitry Andric return getOpenCLExtensionsFromExtMap(FT, OpenCLTypeExtMap); 2177*0b57cec5SDimitry Andric 2178*0b57cec5SDimitry Andric return ""; 2179*0b57cec5SDimitry Andric } 2180*0b57cec5SDimitry Andric 2181*0b57cec5SDimitry Andric template <typename T, typename MapT> 2182*0b57cec5SDimitry Andric std::string Sema::getOpenCLExtensionsFromExtMap(T *FDT, MapT &Map) { 2183*0b57cec5SDimitry Andric std::string ExtensionNames = ""; 2184*0b57cec5SDimitry Andric auto Loc = Map.find(FDT); 2185*0b57cec5SDimitry Andric 2186*0b57cec5SDimitry Andric for (auto const& I : Loc->second) { 2187*0b57cec5SDimitry Andric ExtensionNames += I; 2188*0b57cec5SDimitry Andric ExtensionNames += " "; 2189*0b57cec5SDimitry Andric } 2190*0b57cec5SDimitry Andric ExtensionNames.pop_back(); 2191*0b57cec5SDimitry Andric 2192*0b57cec5SDimitry Andric return ExtensionNames; 2193*0b57cec5SDimitry Andric } 2194*0b57cec5SDimitry Andric 2195*0b57cec5SDimitry Andric bool Sema::isOpenCLDisabledDecl(Decl *FD) { 2196*0b57cec5SDimitry Andric auto Loc = OpenCLDeclExtMap.find(FD); 2197*0b57cec5SDimitry Andric if (Loc == OpenCLDeclExtMap.end()) 2198*0b57cec5SDimitry Andric return false; 2199*0b57cec5SDimitry Andric for (auto &I : Loc->second) { 2200*0b57cec5SDimitry Andric if (!getOpenCLOptions().isEnabled(I)) 2201*0b57cec5SDimitry Andric return true; 2202*0b57cec5SDimitry Andric } 2203*0b57cec5SDimitry Andric return false; 2204*0b57cec5SDimitry Andric } 2205*0b57cec5SDimitry Andric 2206*0b57cec5SDimitry Andric template <typename T, typename DiagLocT, typename DiagInfoT, typename MapT> 2207*0b57cec5SDimitry Andric bool Sema::checkOpenCLDisabledTypeOrDecl(T D, DiagLocT DiagLoc, 2208*0b57cec5SDimitry Andric DiagInfoT DiagInfo, MapT &Map, 2209*0b57cec5SDimitry Andric unsigned Selector, 2210*0b57cec5SDimitry Andric SourceRange SrcRange) { 2211*0b57cec5SDimitry Andric auto Loc = Map.find(D); 2212*0b57cec5SDimitry Andric if (Loc == Map.end()) 2213*0b57cec5SDimitry Andric return false; 2214*0b57cec5SDimitry Andric bool Disabled = false; 2215*0b57cec5SDimitry Andric for (auto &I : Loc->second) { 2216*0b57cec5SDimitry Andric if (I != CurrOpenCLExtension && !getOpenCLOptions().isEnabled(I)) { 2217*0b57cec5SDimitry Andric Diag(DiagLoc, diag::err_opencl_requires_extension) << Selector << DiagInfo 2218*0b57cec5SDimitry Andric << I << SrcRange; 2219*0b57cec5SDimitry Andric Disabled = true; 2220*0b57cec5SDimitry Andric } 2221*0b57cec5SDimitry Andric } 2222*0b57cec5SDimitry Andric return Disabled; 2223*0b57cec5SDimitry Andric } 2224*0b57cec5SDimitry Andric 2225*0b57cec5SDimitry Andric bool Sema::checkOpenCLDisabledTypeDeclSpec(const DeclSpec &DS, QualType QT) { 2226*0b57cec5SDimitry Andric // Check extensions for declared types. 2227*0b57cec5SDimitry Andric Decl *Decl = nullptr; 2228*0b57cec5SDimitry Andric if (auto TypedefT = dyn_cast<TypedefType>(QT.getTypePtr())) 2229*0b57cec5SDimitry Andric Decl = TypedefT->getDecl(); 2230*0b57cec5SDimitry Andric if (auto TagT = dyn_cast<TagType>(QT.getCanonicalType().getTypePtr())) 2231*0b57cec5SDimitry Andric Decl = TagT->getDecl(); 2232*0b57cec5SDimitry Andric auto Loc = DS.getTypeSpecTypeLoc(); 2233*0b57cec5SDimitry Andric 2234*0b57cec5SDimitry Andric // Check extensions for vector types. 2235*0b57cec5SDimitry Andric // e.g. double4 is not allowed when cl_khr_fp64 is absent. 2236*0b57cec5SDimitry Andric if (QT->isExtVectorType()) { 2237*0b57cec5SDimitry Andric auto TypePtr = QT->castAs<ExtVectorType>()->getElementType().getTypePtr(); 2238*0b57cec5SDimitry Andric return checkOpenCLDisabledTypeOrDecl(TypePtr, Loc, QT, OpenCLTypeExtMap); 2239*0b57cec5SDimitry Andric } 2240*0b57cec5SDimitry Andric 2241*0b57cec5SDimitry Andric if (checkOpenCLDisabledTypeOrDecl(Decl, Loc, QT, OpenCLDeclExtMap)) 2242*0b57cec5SDimitry Andric return true; 2243*0b57cec5SDimitry Andric 2244*0b57cec5SDimitry Andric // Check extensions for builtin types. 2245*0b57cec5SDimitry Andric return checkOpenCLDisabledTypeOrDecl(QT.getCanonicalType().getTypePtr(), Loc, 2246*0b57cec5SDimitry Andric QT, OpenCLTypeExtMap); 2247*0b57cec5SDimitry Andric } 2248*0b57cec5SDimitry Andric 2249*0b57cec5SDimitry Andric bool Sema::checkOpenCLDisabledDecl(const NamedDecl &D, const Expr &E) { 2250*0b57cec5SDimitry Andric IdentifierInfo *FnName = D.getIdentifier(); 2251*0b57cec5SDimitry Andric return checkOpenCLDisabledTypeOrDecl(&D, E.getBeginLoc(), FnName, 2252*0b57cec5SDimitry Andric OpenCLDeclExtMap, 1, D.getSourceRange()); 2253*0b57cec5SDimitry Andric } 2254