Sema.cpp revision 218893
1193326Sed//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements the actions class which performs semantic analysis and 11193326Sed// builds an AST out of a parse stream. 12193326Sed// 13193326Sed//===----------------------------------------------------------------------===// 14193326Sed 15212904Sdim#include "clang/Sema/SemaInternal.h" 16212904Sdim#include "clang/Sema/DelayedDiagnostic.h" 17202379Srdivacky#include "TargetAttributesSema.h" 18198092Srdivacky#include "llvm/ADT/DenseMap.h" 19203955Srdivacky#include "llvm/ADT/SmallSet.h" 20199482Srdivacky#include "llvm/ADT/APFloat.h" 21212904Sdim#include "clang/Sema/CXXFieldCollector.h" 22218893Sdim#include "clang/Sema/TemplateDeduction.h" 23210299Sed#include "clang/Sema/ExternalSemaSource.h" 24218893Sdim#include "clang/Sema/ObjCMethodList.h" 25212904Sdim#include "clang/Sema/PrettyDeclStackTrace.h" 26212904Sdim#include "clang/Sema/Scope.h" 27212904Sdim#include "clang/Sema/ScopeInfo.h" 28212904Sdim#include "clang/Sema/SemaConsumer.h" 29193326Sed#include "clang/AST/ASTContext.h" 30203955Srdivacky#include "clang/AST/ASTDiagnostic.h" 31212904Sdim#include "clang/AST/DeclCXX.h" 32193326Sed#include "clang/AST/DeclObjC.h" 33193326Sed#include "clang/AST/Expr.h" 34218893Sdim#include "clang/AST/StmtCXX.h" 35193326Sed#include "clang/Lex/Preprocessor.h" 36198092Srdivacky#include "clang/Basic/PartialDiagnostic.h" 37193326Sed#include "clang/Basic/TargetInfo.h" 38193326Sedusing namespace clang; 39212904Sdimusing namespace sema; 40204643Srdivacky 41204643SrdivackyFunctionScopeInfo::~FunctionScopeInfo() { } 42204643Srdivacky 43218893Sdimvoid FunctionScopeInfo::Clear() { 44212904Sdim HasBranchProtectedScope = false; 45212904Sdim HasBranchIntoScope = false; 46212904Sdim HasIndirectGoto = false; 47212904Sdim 48204643Srdivacky SwitchStack.clear(); 49208600Srdivacky Returns.clear(); 50218893Sdim ErrorTrap.reset(); 51204643Srdivacky} 52204643Srdivacky 53204643SrdivackyBlockScopeInfo::~BlockScopeInfo() { } 54204643Srdivacky 55212904Sdimvoid Sema::ActOnTranslationUnitScope(Scope *S) { 56193326Sed TUScope = S; 57193326Sed PushDeclContext(S, Context.getTranslationUnitDecl()); 58198092Srdivacky 59210299Sed VAListTagName = PP.getIdentifierInfo("__va_list_tag"); 60210299Sed 61212904Sdim if (!Context.isInt128Installed() && // May be set by ASTReader. 62210299Sed PP.getTargetInfo().getPointerWidth(0) >= 64) { 63200583Srdivacky TypeSourceInfo *TInfo; 64198893Srdivacky 65193326Sed // Install [u]int128_t for 64-bit targets. 66200583Srdivacky TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty); 67193326Sed PushOnScopeChains(TypedefDecl::Create(Context, CurContext, 68193326Sed SourceLocation(), 69193326Sed &Context.Idents.get("__int128_t"), 70200583Srdivacky TInfo), TUScope); 71198893Srdivacky 72200583Srdivacky TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty); 73193326Sed PushOnScopeChains(TypedefDecl::Create(Context, CurContext, 74193326Sed SourceLocation(), 75193326Sed &Context.Idents.get("__uint128_t"), 76200583Srdivacky TInfo), TUScope); 77210299Sed Context.setInt128Installed(); 78193326Sed } 79198092Srdivacky 80198092Srdivacky 81193326Sed if (!PP.getLangOptions().ObjC1) return; 82198092Srdivacky 83212904Sdim // Built-in ObjC types may already be set by ASTReader (hence isNull checks). 84193326Sed if (Context.getObjCSelType().isNull()) { 85199990Srdivacky // Create the built-in typedef for 'SEL'. 86199990Srdivacky QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy); 87200583Srdivacky TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT); 88198893Srdivacky TypedefDecl *SelTypedef 89198893Srdivacky = TypedefDecl::Create(Context, CurContext, SourceLocation(), 90198893Srdivacky &Context.Idents.get("SEL"), SelInfo); 91193326Sed PushOnScopeChains(SelTypedef, TUScope); 92193326Sed Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); 93199990Srdivacky Context.ObjCSelRedefinitionType = Context.getObjCSelType(); 94193326Sed } 95193326Sed 96193326Sed // Synthesize "@class Protocol; 97193326Sed if (Context.getObjCProtoType().isNull()) { 98193326Sed ObjCInterfaceDecl *ProtocolDecl = 99193326Sed ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), 100198092Srdivacky &Context.Idents.get("Protocol"), 101193326Sed SourceLocation(), true); 102193326Sed Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); 103199512Srdivacky PushOnScopeChains(ProtocolDecl, TUScope, false); 104193326Sed } 105198092Srdivacky // Create the built-in typedef for 'id'. 106193326Sed if (Context.getObjCIdType().isNull()) { 107208600Srdivacky QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0); 108208600Srdivacky T = Context.getObjCObjectPointerType(T); 109208600Srdivacky TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T); 110198893Srdivacky TypedefDecl *IdTypedef 111198893Srdivacky = TypedefDecl::Create(Context, CurContext, SourceLocation(), 112198893Srdivacky &Context.Idents.get("id"), IdInfo); 113193326Sed PushOnScopeChains(IdTypedef, TUScope); 114193326Sed Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); 115198092Srdivacky Context.ObjCIdRedefinitionType = Context.getObjCIdType(); 116193326Sed } 117198092Srdivacky // Create the built-in typedef for 'Class'. 118198092Srdivacky if (Context.getObjCClassType().isNull()) { 119208600Srdivacky QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0); 120208600Srdivacky T = Context.getObjCObjectPointerType(T); 121208600Srdivacky TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T); 122198893Srdivacky TypedefDecl *ClassTypedef 123198893Srdivacky = TypedefDecl::Create(Context, CurContext, SourceLocation(), 124198893Srdivacky &Context.Idents.get("Class"), ClassInfo); 125198092Srdivacky PushOnScopeChains(ClassTypedef, TUScope); 126198092Srdivacky Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); 127198092Srdivacky Context.ObjCClassRedefinitionType = Context.getObjCClassType(); 128212904Sdim } 129193326Sed} 130193326Sed 131193326SedSema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, 132199482Srdivacky bool CompleteTranslationUnit, 133199482Srdivacky CodeCompleteConsumer *CodeCompleter) 134218893Sdim : TheTargetAttributesSema(0), FPFeatures(pp.getLangOptions()), 135202379Srdivacky LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), 136198092Srdivacky Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), 137199482Srdivacky ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0), 138218893Sdim PackContext(0), VisContext(0), 139218893Sdim IdResolver(pp.getLangOptions()), CXXTypeInfoDecl(0), MSVCGuidDecl(0), 140218893Sdim GlobalNewDeleteDeclared(false), 141193326Sed CompleteTranslationUnit(CompleteTranslationUnit), 142218893Sdim NumSFINAEErrors(0), SuppressAccessChecking(false), 143218893Sdim AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false), 144218893Sdim NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1), 145218893Sdim CurrentInstantiationScope(0), TyposCorrected(0), 146206084Srdivacky AnalysisWarnings(*this) 147199482Srdivacky{ 148193326Sed TUScope = 0; 149193326Sed if (getLangOptions().CPlusPlus) 150193326Sed FieldCollector.reset(new CXXFieldCollector()); 151198092Srdivacky 152193326Sed // Tell diagnostics how to render things from the AST library. 153203955Srdivacky PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, 154203955Srdivacky &Context); 155199990Srdivacky 156199990Srdivacky ExprEvalContexts.push_back( 157212904Sdim ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0)); 158212904Sdim 159218893Sdim FunctionScopes.push_back(new FunctionScopeInfo(Diags)); 160193326Sed} 161193326Sed 162212904Sdimvoid Sema::Initialize() { 163212904Sdim // Tell the AST consumer about this Sema object. 164212904Sdim Consumer.Initialize(Context); 165212904Sdim 166212904Sdim // FIXME: Isn't this redundant with the initialization above? 167212904Sdim if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) 168212904Sdim SC->InitializeSema(*this); 169212904Sdim 170212904Sdim // Tell the external Sema source about this Sema object. 171212904Sdim if (ExternalSemaSource *ExternalSema 172212904Sdim = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) 173212904Sdim ExternalSema->InitializeSema(*this); 174212904Sdim} 175212904Sdim 176202379SrdivackySema::~Sema() { 177202379Srdivacky if (PackContext) FreePackedContext(); 178212904Sdim if (VisContext) FreeVisContext(); 179202379Srdivacky delete TheTargetAttributesSema; 180212904Sdim 181212904Sdim // Kill all the active scopes. 182212904Sdim for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I) 183212904Sdim delete FunctionScopes[I]; 184212904Sdim if (FunctionScopes.size() == 1) 185212904Sdim delete FunctionScopes[0]; 186212904Sdim 187212904Sdim // Tell the SemaConsumer to forget about us; we're going out of scope. 188212904Sdim if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) 189212904Sdim SC->ForgetSema(); 190212904Sdim 191212904Sdim // Detach from the external Sema source. 192212904Sdim if (ExternalSemaSource *ExternalSema 193212904Sdim = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) 194212904Sdim ExternalSema->ForgetSema(); 195199482Srdivacky} 196199482Srdivacky 197198092Srdivacky/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. 198193326Sed/// If there is already an implicit cast, merge into the existing one. 199212904Sdim/// The result is of the given category. 200198092Srdivackyvoid Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, 201212904Sdim CastKind Kind, ExprValueKind VK, 202212904Sdim const CXXCastPath *BasePath) { 203193326Sed QualType ExprTy = Context.getCanonicalType(Expr->getType()); 204193326Sed QualType TypeTy = Context.getCanonicalType(Ty); 205198092Srdivacky 206193326Sed if (ExprTy == TypeTy) 207193326Sed return; 208198092Srdivacky 209208600Srdivacky // If this is a derived-to-base cast to a through a virtual base, we 210208600Srdivacky // need a vtable. 211212904Sdim if (Kind == CK_DerivedToBase && 212212904Sdim BasePathInvolvesVirtualBase(*BasePath)) { 213208600Srdivacky QualType T = Expr->getType(); 214208600Srdivacky if (const PointerType *Pointer = T->getAs<PointerType>()) 215208600Srdivacky T = Pointer->getPointeeType(); 216208600Srdivacky if (const RecordType *RecordTy = T->getAs<RecordType>()) 217208600Srdivacky MarkVTableUsed(Expr->getLocStart(), 218208600Srdivacky cast<CXXRecordDecl>(RecordTy->getDecl())); 219208600Srdivacky } 220199482Srdivacky 221193326Sed if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { 222212904Sdim if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) { 223198092Srdivacky ImpCast->setType(Ty); 224212904Sdim ImpCast->setValueKind(VK); 225198092Srdivacky return; 226198092Srdivacky } 227198092Srdivacky } 228198092Srdivacky 229212904Sdim Expr = ImplicitCastExpr::Create(Context, Ty, Kind, Expr, BasePath, VK); 230193326Sed} 231193326Sed 232212904SdimExprValueKind Sema::CastCategory(Expr *E) { 233212904Sdim Expr::Classification Classification = E->Classify(Context); 234212904Sdim return Classification.isRValue() ? VK_RValue : 235212904Sdim (Classification.isLValue() ? VK_LValue : VK_XValue); 236193326Sed} 237212904Sdim 238212904Sdim/// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector. 239212904Sdimstatic bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { 240212904Sdim if (D->isUsed()) 241212904Sdim return true; 242212904Sdim 243212904Sdim if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 244212904Sdim // UnusedFileScopedDecls stores the first declaration. 245212904Sdim // The declaration may have become definition so check again. 246212904Sdim const FunctionDecl *DeclToCheck; 247212904Sdim if (FD->hasBody(DeclToCheck)) 248212904Sdim return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 249212904Sdim 250212904Sdim // Later redecls may add new information resulting in not having to warn, 251212904Sdim // so check again. 252212904Sdim DeclToCheck = FD->getMostRecentDeclaration(); 253212904Sdim if (DeclToCheck != FD) 254212904Sdim return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 255212904Sdim } 256212904Sdim 257212904Sdim if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 258212904Sdim // UnusedFileScopedDecls stores the first declaration. 259212904Sdim // The declaration may have become definition so check again. 260212904Sdim const VarDecl *DeclToCheck = VD->getDefinition(); 261212904Sdim if (DeclToCheck) 262212904Sdim return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 263212904Sdim 264212904Sdim // Later redecls may add new information resulting in not having to warn, 265212904Sdim // so check again. 266212904Sdim DeclToCheck = VD->getMostRecentDeclaration(); 267212904Sdim if (DeclToCheck != VD) 268212904Sdim return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); 269212904Sdim } 270212904Sdim 271212904Sdim return false; 272193326Sed} 273193326Sed 274218893Sdimnamespace { 275218893Sdim struct UndefinedInternal { 276218893Sdim NamedDecl *decl; 277218893Sdim FullSourceLoc useLoc; 278218893Sdim 279218893Sdim UndefinedInternal(NamedDecl *decl, FullSourceLoc useLoc) 280218893Sdim : decl(decl), useLoc(useLoc) {} 281218893Sdim }; 282218893Sdim 283218893Sdim bool operator<(const UndefinedInternal &l, const UndefinedInternal &r) { 284218893Sdim return l.useLoc.isBeforeInTranslationUnitThan(r.useLoc); 285218893Sdim } 286218893Sdim} 287218893Sdim 288218893Sdim/// checkUndefinedInternals - Check for undefined objects with internal linkage. 289218893Sdimstatic void checkUndefinedInternals(Sema &S) { 290218893Sdim if (S.UndefinedInternals.empty()) return; 291218893Sdim 292218893Sdim // Collect all the still-undefined entities with internal linkage. 293218893Sdim llvm::SmallVector<UndefinedInternal, 16> undefined; 294218893Sdim for (llvm::DenseMap<NamedDecl*,SourceLocation>::iterator 295218893Sdim i = S.UndefinedInternals.begin(), e = S.UndefinedInternals.end(); 296218893Sdim i != e; ++i) { 297218893Sdim NamedDecl *decl = i->first; 298218893Sdim 299218893Sdim // Ignore attributes that have become invalid. 300218893Sdim if (decl->isInvalidDecl()) continue; 301218893Sdim 302218893Sdim // __attribute__((weakref)) is basically a definition. 303218893Sdim if (decl->hasAttr<WeakRefAttr>()) continue; 304218893Sdim 305218893Sdim if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { 306218893Sdim if (fn->isPure() || fn->hasBody()) 307218893Sdim continue; 308218893Sdim } else { 309218893Sdim if (cast<VarDecl>(decl)->hasDefinition() != VarDecl::DeclarationOnly) 310218893Sdim continue; 311218893Sdim } 312218893Sdim 313218893Sdim // We build a FullSourceLoc so that we can sort with array_pod_sort. 314218893Sdim FullSourceLoc loc(i->second, S.Context.getSourceManager()); 315218893Sdim undefined.push_back(UndefinedInternal(decl, loc)); 316218893Sdim } 317218893Sdim 318218893Sdim if (undefined.empty()) return; 319218893Sdim 320218893Sdim // Sort (in order of use site) so that we're not (as) dependent on 321218893Sdim // the iteration order through an llvm::DenseMap. 322218893Sdim llvm::array_pod_sort(undefined.begin(), undefined.end()); 323218893Sdim 324218893Sdim for (llvm::SmallVectorImpl<UndefinedInternal>::iterator 325218893Sdim i = undefined.begin(), e = undefined.end(); i != e; ++i) { 326218893Sdim NamedDecl *decl = i->decl; 327218893Sdim S.Diag(decl->getLocation(), diag::warn_undefined_internal) 328218893Sdim << isa<VarDecl>(decl) << decl; 329218893Sdim S.Diag(i->useLoc, diag::note_used_here); 330218893Sdim } 331218893Sdim} 332218893Sdim 333193326Sed/// ActOnEndOfTranslationUnit - This is called at the very end of the 334193326Sed/// translation unit when EOF is reached and all but the top-level scope is 335193326Sed/// popped. 336212904Sdimvoid Sema::ActOnEndOfTranslationUnit() { 337212904Sdim // At PCH writing, implicit instantiations and VTable handling info are 338212904Sdim // stored and performed when the PCH is included. 339218893Sdim if (CompleteTranslationUnit) { 340218893Sdim // If any dynamic classes have their key function defined within 341218893Sdim // this translation unit, then those vtables are considered "used" and must 342218893Sdim // be emitted. 343218893Sdim for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { 344218893Sdim assert(!DynamicClasses[I]->isDependentType() && 345218893Sdim "Should not see dependent types here!"); 346218893Sdim if (const CXXMethodDecl *KeyFunction 347218893Sdim = Context.getKeyFunction(DynamicClasses[I])) { 348218893Sdim const FunctionDecl *Definition = 0; 349218893Sdim if (KeyFunction->hasBody(Definition)) 350218893Sdim MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true); 351218893Sdim } 352218893Sdim } 353212904Sdim 354218893Sdim // If DefinedUsedVTables ends up marking any virtual member functions it 355218893Sdim // might lead to more pending template instantiations, which we then need 356218893Sdim // to instantiate. 357218893Sdim DefineUsedVTables(); 358218893Sdim 359218893Sdim // C++: Perform implicit template instantiations. 360218893Sdim // 361218893Sdim // FIXME: When we perform these implicit instantiations, we do not 362218893Sdim // carefully keep track of the point of instantiation (C++ [temp.point]). 363218893Sdim // This means that name lookup that occurs within the template 364218893Sdim // instantiation will always happen at the end of the translation unit, 365218893Sdim // so it will find some names that should not be found. Although this is 366218893Sdim // common behavior for C++ compilers, it is technically wrong. In the 367218893Sdim // future, we either need to be able to filter the results of name lookup 368218893Sdim // or we need to perform template instantiations earlier. 369218893Sdim PerformPendingInstantiations(); 370218893Sdim } 371200583Srdivacky 372212904Sdim // Remove file scoped decls that turned out to be used. 373212904Sdim UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(), 374212904Sdim UnusedFileScopedDecls.end(), 375212904Sdim std::bind1st(std::ptr_fun(ShouldRemoveFromUnused), 376212904Sdim this)), 377212904Sdim UnusedFileScopedDecls.end()); 378207619Srdivacky 379212904Sdim if (!CompleteTranslationUnit) { 380212904Sdim TUScope = 0; 381212904Sdim return; 382212904Sdim } 383212904Sdim 384198092Srdivacky // Check for #pragma weak identifiers that were never declared 385198092Srdivacky // FIXME: This will cause diagnostics to be emitted in a non-determinstic 386198092Srdivacky // order! Iterating over a densemap like this is bad. 387198092Srdivacky for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator 388198092Srdivacky I = WeakUndeclaredIdentifiers.begin(), 389198092Srdivacky E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { 390198092Srdivacky if (I->second.getUsed()) continue; 391198092Srdivacky 392198092Srdivacky Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) 393198092Srdivacky << I->first; 394198092Srdivacky } 395198092Srdivacky 396193326Sed // C99 6.9.2p2: 397193326Sed // A declaration of an identifier for an object that has file 398193326Sed // scope without an initializer, and without a storage-class 399193326Sed // specifier or with the storage-class specifier static, 400193326Sed // constitutes a tentative definition. If a translation unit 401193326Sed // contains one or more tentative definitions for an identifier, 402193326Sed // and the translation unit contains no external definition for 403193326Sed // that identifier, then the behavior is exactly as if the 404193326Sed // translation unit contains a file scope declaration of that 405193326Sed // identifier, with the composite type as of the end of the 406193326Sed // translation unit, with an initializer equal to 0. 407203955Srdivacky llvm::SmallSet<VarDecl *, 32> Seen; 408203955Srdivacky for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) { 409203955Srdivacky VarDecl *VD = TentativeDefinitions[i]->getActingDefinition(); 410193326Sed 411203955Srdivacky // If the tentative definition was completed, getActingDefinition() returns 412203955Srdivacky // null. If we've already seen this variable before, insert()'s second 413203955Srdivacky // return value is false. 414203955Srdivacky if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD)) 415193326Sed continue; 416193326Sed 417198092Srdivacky if (const IncompleteArrayType *ArrayT 418193326Sed = Context.getAsIncompleteArrayType(VD->getType())) { 419198092Srdivacky if (RequireCompleteType(VD->getLocation(), 420193326Sed ArrayT->getElementType(), 421198092Srdivacky diag::err_tentative_def_incomplete_type_arr)) { 422193326Sed VD->setInvalidDecl(); 423198092Srdivacky continue; 424193326Sed } 425198092Srdivacky 426198092Srdivacky // Set the length of the array to 1 (C99 6.9.2p5). 427198092Srdivacky Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); 428198092Srdivacky llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); 429198398Srdivacky QualType T = Context.getConstantArrayType(ArrayT->getElementType(), 430198398Srdivacky One, ArrayType::Normal, 0); 431198092Srdivacky VD->setType(T); 432198092Srdivacky } else if (RequireCompleteType(VD->getLocation(), VD->getType(), 433193326Sed diag::err_tentative_def_incomplete_type)) 434193326Sed VD->setInvalidDecl(); 435193326Sed 436193326Sed // Notify the consumer that we've completed a tentative definition. 437193326Sed if (!VD->isInvalidDecl()) 438193326Sed Consumer.CompleteTentativeDefinition(VD); 439193326Sed 440193326Sed } 441218893Sdim 442218893Sdim // If there were errors, disable 'unused' warnings since they will mostly be 443218893Sdim // noise. 444218893Sdim if (!Diags.hasErrorOccurred()) { 445218893Sdim // Output warning for unused file scoped decls. 446218893Sdim for (llvm::SmallVectorImpl<const DeclaratorDecl*>::iterator 447218893Sdim I = UnusedFileScopedDecls.begin(), 448218893Sdim E = UnusedFileScopedDecls.end(); I != E; ++I) { 449218893Sdim if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { 450218893Sdim const FunctionDecl *DiagD; 451218893Sdim if (!FD->hasBody(DiagD)) 452218893Sdim DiagD = FD; 453218893Sdim Diag(DiagD->getLocation(), 454218893Sdim isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function 455218893Sdim : diag::warn_unused_function) 456218893Sdim << DiagD->getDeclName(); 457218893Sdim } else { 458218893Sdim const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition(); 459218893Sdim if (!DiagD) 460218893Sdim DiagD = cast<VarDecl>(*I); 461218893Sdim Diag(DiagD->getLocation(), diag::warn_unused_variable) 462218893Sdim << DiagD->getDeclName(); 463218893Sdim } 464212904Sdim } 465218893Sdim 466218893Sdim checkUndefinedInternals(*this); 467212904Sdim } 468212904Sdim 469212904Sdim TUScope = 0; 470193326Sed} 471193326Sed 472193326Sed 473193326Sed//===----------------------------------------------------------------------===// 474193326Sed// Helper functions. 475193326Sed//===----------------------------------------------------------------------===// 476193326Sed 477198092SrdivackyDeclContext *Sema::getFunctionLevelDeclContext() { 478201361Srdivacky DeclContext *DC = CurContext; 479198092Srdivacky 480208600Srdivacky while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) 481198092Srdivacky DC = DC->getParent(); 482198092Srdivacky 483198092Srdivacky return DC; 484198092Srdivacky} 485198092Srdivacky 486193326Sed/// getCurFunctionDecl - If inside of a function body, this returns a pointer 487193326Sed/// to the function decl for the function being parsed. If we're currently 488193326Sed/// in a 'block', this returns the containing context. 489193326SedFunctionDecl *Sema::getCurFunctionDecl() { 490198092Srdivacky DeclContext *DC = getFunctionLevelDeclContext(); 491193326Sed return dyn_cast<FunctionDecl>(DC); 492193326Sed} 493193326Sed 494193326SedObjCMethodDecl *Sema::getCurMethodDecl() { 495198092Srdivacky DeclContext *DC = getFunctionLevelDeclContext(); 496193326Sed return dyn_cast<ObjCMethodDecl>(DC); 497193326Sed} 498193326Sed 499193326SedNamedDecl *Sema::getCurFunctionOrMethodDecl() { 500198092Srdivacky DeclContext *DC = getFunctionLevelDeclContext(); 501193326Sed if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) 502193326Sed return cast<NamedDecl>(DC); 503193326Sed return 0; 504193326Sed} 505193326Sed 506193326SedSema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { 507218893Sdim if (!isActive()) 508218893Sdim return; 509218893Sdim 510218893Sdim if (llvm::Optional<TemplateDeductionInfo*> Info = SemaRef.isSFINAEContext()) { 511218893Sdim switch (DiagnosticIDs::getDiagnosticSFINAEResponse(getDiagID())) { 512218893Sdim case DiagnosticIDs::SFINAE_Report: 513218893Sdim // Fall through; we'll report the diagnostic below. 514218893Sdim break; 515218893Sdim 516218893Sdim case DiagnosticIDs::SFINAE_AccessControl: 517218893Sdim // Unless access checking is specifically called out as a SFINAE 518218893Sdim // error, report this diagnostic. 519218893Sdim if (!SemaRef.AccessCheckingSFINAE) 520218893Sdim break; 521218893Sdim 522218893Sdim case DiagnosticIDs::SFINAE_SubstitutionFailure: 523218893Sdim // Count this failure so that we know that template argument deduction 524218893Sdim // has failed. 525218893Sdim ++SemaRef.NumSFINAEErrors; 526218893Sdim SemaRef.Diags.setLastDiagnosticIgnored(); 527218893Sdim SemaRef.Diags.Clear(); 528218893Sdim Clear(); 529218893Sdim return; 530218893Sdim 531218893Sdim case DiagnosticIDs::SFINAE_Suppress: 532218893Sdim // Make a copy of this suppressed diagnostic and store it with the 533218893Sdim // template-deduction information; 534218893Sdim FlushCounts(); 535218893Sdim DiagnosticInfo DiagInfo(&SemaRef.Diags); 536218893Sdim 537218893Sdim if (*Info) 538218893Sdim (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(), 539218893Sdim PartialDiagnostic(DiagInfo, 540218893Sdim SemaRef.Context.getDiagAllocator())); 541218893Sdim 542218893Sdim // Suppress this diagnostic. 543218893Sdim SemaRef.Diags.setLastDiagnosticIgnored(); 544218893Sdim SemaRef.Diags.Clear(); 545218893Sdim Clear(); 546218893Sdim return; 547218893Sdim } 548218893Sdim } 549218893Sdim 550218893Sdim // Emit the diagnostic. 551194179Sed if (!this->Emit()) 552194179Sed return; 553198092Srdivacky 554193326Sed // If this is not a note, and we're in a template instantiation 555193326Sed // that is different from the last template instantiation where 556193326Sed // we emitted an error, print a template instantiation 557193326Sed // backtrace. 558218893Sdim if (!DiagnosticIDs::isBuiltinNote(DiagID) && 559193326Sed !SemaRef.ActiveTemplateInstantiations.empty() && 560198092Srdivacky SemaRef.ActiveTemplateInstantiations.back() 561193326Sed != SemaRef.LastTemplateInstantiationErrorContext) { 562193326Sed SemaRef.PrintInstantiationStack(); 563198092Srdivacky SemaRef.LastTemplateInstantiationErrorContext 564193326Sed = SemaRef.ActiveTemplateInstantiations.back(); 565193326Sed } 566193326Sed} 567195341Sed 568206084SrdivackySema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) { 569218893Sdim DiagnosticBuilder DB = Diags.Report(Loc, DiagID); 570206084Srdivacky return SemaDiagnosticBuilder(DB, *this, DiagID); 571206084Srdivacky} 572206084Srdivacky 573198092SrdivackySema::SemaDiagnosticBuilder 574198092SrdivackySema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { 575198092Srdivacky SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); 576198092Srdivacky PD.Emit(Builder); 577198092Srdivacky 578198092Srdivacky return Builder; 579198092Srdivacky} 580198092Srdivacky 581210299Sed/// \brief Determines the active Scope associated with the given declaration 582210299Sed/// context. 583210299Sed/// 584210299Sed/// This routine maps a declaration context to the active Scope object that 585210299Sed/// represents that declaration context in the parser. It is typically used 586210299Sed/// from "scope-less" code (e.g., template instantiation, lazy creation of 587210299Sed/// declarations) that injects a name for name-lookup purposes and, therefore, 588210299Sed/// must update the Scope. 589210299Sed/// 590210299Sed/// \returns The scope corresponding to the given declaraion context, or NULL 591210299Sed/// if no such scope is open. 592210299SedScope *Sema::getScopeForContext(DeclContext *Ctx) { 593210299Sed 594210299Sed if (!Ctx) 595210299Sed return 0; 596210299Sed 597210299Sed Ctx = Ctx->getPrimaryContext(); 598210299Sed for (Scope *S = getCurScope(); S; S = S->getParent()) { 599210299Sed // Ignore scopes that cannot have declarations. This is important for 600210299Sed // out-of-line definitions of static class members. 601210299Sed if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) 602210299Sed if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity())) 603210299Sed if (Ctx == Entity->getPrimaryContext()) 604210299Sed return S; 605210299Sed } 606210299Sed 607210299Sed return 0; 608210299Sed} 609204643Srdivacky 610204643Srdivacky/// \brief Enter a new function scope 611204643Srdivackyvoid Sema::PushFunctionScope() { 612212904Sdim if (FunctionScopes.size() == 1) { 613212904Sdim // Use the "top" function scope rather than having to allocate 614212904Sdim // memory for a new scope. 615218893Sdim FunctionScopes.back()->Clear(); 616212904Sdim FunctionScopes.push_back(FunctionScopes.back()); 617204643Srdivacky return; 618204643Srdivacky } 619204643Srdivacky 620218893Sdim FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics())); 621204643Srdivacky} 622204643Srdivacky 623204643Srdivackyvoid Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { 624218893Sdim FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(), 625204643Srdivacky BlockScope, Block)); 626204643Srdivacky} 627204643Srdivacky 628204643Srdivackyvoid Sema::PopFunctionOrBlockScope() { 629212904Sdim FunctionScopeInfo *Scope = FunctionScopes.pop_back_val(); 630212904Sdim assert(!FunctionScopes.empty() && "mismatched push/pop!"); 631212904Sdim if (FunctionScopes.back() != Scope) 632212904Sdim delete Scope; 633204643Srdivacky} 634204643Srdivacky 635204643Srdivacky/// \brief Determine whether any errors occurred within this function/method/ 636204643Srdivacky/// block. 637204643Srdivackybool Sema::hasAnyErrorsInThisFunction() const { 638218893Sdim return getCurFunction()->ErrorTrap.hasErrorOccurred(); 639204643Srdivacky} 640204643Srdivacky 641204643SrdivackyBlockScopeInfo *Sema::getCurBlock() { 642204643Srdivacky if (FunctionScopes.empty()) 643204643Srdivacky return 0; 644204643Srdivacky 645204643Srdivacky return dyn_cast<BlockScopeInfo>(FunctionScopes.back()); 646204643Srdivacky} 647210299Sed 648210299Sed// Pin this vtable to this file. 649210299SedExternalSemaSource::~ExternalSemaSource() {} 650212904Sdim 651218893Sdimstd::pair<ObjCMethodList, ObjCMethodList> 652218893SdimExternalSemaSource::ReadMethodPool(Selector Sel) { 653218893Sdim return std::pair<ObjCMethodList, ObjCMethodList>(); 654218893Sdim} 655218893Sdim 656212904Sdimvoid PrettyDeclStackTraceEntry::print(llvm::raw_ostream &OS) const { 657212904Sdim SourceLocation Loc = this->Loc; 658212904Sdim if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); 659212904Sdim if (Loc.isValid()) { 660212904Sdim Loc.print(OS, S.getSourceManager()); 661212904Sdim OS << ": "; 662212904Sdim } 663212904Sdim OS << Message; 664212904Sdim 665212904Sdim if (TheDecl && isa<NamedDecl>(TheDecl)) { 666212904Sdim std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString(); 667212904Sdim if (!Name.empty()) 668212904Sdim OS << " '" << Name << '\''; 669212904Sdim } 670212904Sdim 671212904Sdim OS << '\n'; 672212904Sdim} 673