Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the actions class which performs semantic analysis and |
| 11 | // builds an AST out of a parse stream. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
| 16 | #include "clang/Sema/DelayedDiagnostic.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 17 | #include "TargetAttributesSema.h" |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
John McCall | 680523a | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/APFloat.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CXXFieldCollector.h" |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 22 | #include "clang/Sema/TemplateDeduction.h" |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 23 | #include "clang/Sema/ExternalSemaSource.h" |
Sebastian Redl | 8c84571 | 2010-09-28 20:23:00 +0000 | [diff] [blame] | 24 | #include "clang/Sema/ObjCMethodList.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 25 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 27 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 28 | #include "clang/Sema/SemaConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 30 | #include "clang/AST/ASTDiagnostic.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 31 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 32 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 33 | #include "clang/AST/Expr.h" |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 34 | #include "clang/AST/StmtCXX.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 36 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 37 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 39 | using namespace sema; |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 40 | |
| 41 | FunctionScopeInfo::~FunctionScopeInfo() { } |
| 42 | |
Argyrios Kyrtzidis | 8fc32d2 | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 43 | void FunctionScopeInfo::Clear() { |
John McCall | b60a77e | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 44 | HasBranchProtectedScope = false; |
| 45 | HasBranchIntoScope = false; |
| 46 | HasIndirectGoto = false; |
| 47 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 48 | SwitchStack.clear(); |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 49 | Returns.clear(); |
Argyrios Kyrtzidis | 8fc32d2 | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 50 | ErrorTrap.reset(); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 51 | PossiblyUnreachableDiags.clear(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | BlockScopeInfo::~BlockScopeInfo() { } |
| 55 | |
Douglas Gregor | c1a3e5e | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 56 | void Sema::ActOnTranslationUnitScope(Scope *S) { |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 57 | TUScope = S; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 58 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
John McCall | c7e04da | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 60 | VAListTagName = PP.getIdentifierInfo("__va_list_tag"); |
| 61 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 62 | if (!Context.isInt128Installed() && // May be set by ASTReader. |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 63 | PP.getTargetInfo().getPointerWidth(0) >= 64) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 64 | TypeSourceInfo *TInfo; |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 66 | // Install [u]int128_t for 64-bit targets. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 67 | TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 68 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 69 | SourceLocation(), |
| 70 | &Context.Idents.get("__int128_t"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 71 | TInfo), TUScope); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 72 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 73 | TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 74 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 75 | SourceLocation(), |
| 76 | &Context.Idents.get("__uint128_t"), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 77 | TInfo), TUScope); |
Argyrios Kyrtzidis | 0061138 | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 78 | Context.setInt128Installed(); |
Chris Lattner | 4d150c8 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 79 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
| 81 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 82 | if (!PP.getLangOptions().ObjC1) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 84 | // Built-in ObjC types may already be set by ASTReader (hence isNull checks). |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 85 | if (Context.getObjCSelType().isNull()) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 86 | // Create the built-in typedef for 'SEL'. |
Fariborz Jahanian | 04765ac | 2009-11-23 18:04:25 +0000 | [diff] [blame] | 87 | QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 88 | TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 89 | TypedefDecl *SelTypedef |
| 90 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 91 | &Context.Idents.get("SEL"), SelInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 92 | PushOnScopeChains(SelTypedef, TUScope); |
| 93 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 94 | Context.ObjCSelRedefinitionType = Context.getObjCSelType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 95 | } |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 97 | // Synthesize "@class Protocol; |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 98 | if (Context.getObjCProtoType().isNull()) { |
| 99 | ObjCInterfaceDecl *ProtocolDecl = |
| 100 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | &Context.Idents.get("Protocol"), |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 102 | SourceLocation(), true); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 103 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
Fariborz Jahanian | 10324db | 2009-11-18 23:15:37 +0000 | [diff] [blame] | 104 | PushOnScopeChains(ProtocolDecl, TUScope, false); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 105 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 106 | // Create the built-in typedef for 'id'. |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 107 | if (Context.getObjCIdType().isNull()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 108 | QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0); |
| 109 | T = Context.getObjCObjectPointerType(T); |
| 110 | TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 111 | TypedefDecl *IdTypedef |
| 112 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 113 | &Context.Idents.get("id"), IdInfo); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 114 | PushOnScopeChains(IdTypedef, TUScope); |
| 115 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 116 | Context.ObjCIdRedefinitionType = Context.getObjCIdType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 117 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 118 | // Create the built-in typedef for 'Class'. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 119 | if (Context.getObjCClassType().isNull()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 120 | QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0); |
| 121 | T = Context.getObjCObjectPointerType(T); |
| 122 | TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 123 | TypedefDecl *ClassTypedef |
| 124 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 125 | &Context.Idents.get("Class"), ClassInfo); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 126 | PushOnScopeChains(ClassTypedef, TUScope); |
| 127 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 128 | Context.ObjCClassRedefinitionType = Context.getObjCClassType(); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 129 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 132 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 133 | bool CompleteTranslationUnit, |
| 134 | CodeCompleteConsumer *CodeCompleter) |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 135 | : TheTargetAttributesSema(0), FPFeatures(pp.getLangOptions()), |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 136 | LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 138 | ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0), |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 139 | PackContext(0), VisContext(0), |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 140 | IdResolver(pp.getLangOptions()), CXXTypeInfoDecl(0), MSVCGuidDecl(0), |
Bill Wendling | b7566d8 | 2010-09-08 21:30:16 +0000 | [diff] [blame] | 141 | GlobalNewDeleteDeclared(false), |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 142 | CompleteTranslationUnit(CompleteTranslationUnit), |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 143 | NumSFINAEErrors(0), SuppressAccessChecking(false), |
| 144 | AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false), |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 145 | NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1), |
| 146 | CurrentInstantiationScope(0), TyposCorrected(0), |
Bill Wendling | b7566d8 | 2010-09-08 21:30:16 +0000 | [diff] [blame] | 147 | AnalysisWarnings(*this) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 148 | { |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 149 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 150 | if (getLangOptions().CPlusPlus) |
| 151 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 153 | // Tell diagnostics how to render things from the AST library. |
Douglas Gregor | 79a9a34 | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 154 | PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 155 | &Context); |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 156 | |
| 157 | ExprEvalContexts.push_back( |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 158 | ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0)); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 159 | |
Argyrios Kyrtzidis | 8fc32d2 | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 160 | FunctionScopes.push_back(new FunctionScopeInfo(Diags)); |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void Sema::Initialize() { |
| 164 | // Tell the AST consumer about this Sema object. |
| 165 | Consumer.Initialize(Context); |
| 166 | |
| 167 | // FIXME: Isn't this redundant with the initialization above? |
| 168 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) |
| 169 | SC->InitializeSema(*this); |
| 170 | |
| 171 | // Tell the external Sema source about this Sema object. |
| 172 | if (ExternalSemaSource *ExternalSema |
| 173 | = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) |
| 174 | ExternalSema->InitializeSema(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 177 | Sema::~Sema() { |
| 178 | if (PackContext) FreePackedContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 179 | if (VisContext) FreeVisContext(); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 180 | delete TheTargetAttributesSema; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 181 | |
| 182 | // Kill all the active scopes. |
| 183 | for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I) |
| 184 | delete FunctionScopes[I]; |
| 185 | if (FunctionScopes.size() == 1) |
| 186 | delete FunctionScopes[0]; |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 187 | |
| 188 | // Tell the SemaConsumer to forget about us; we're going out of scope. |
| 189 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) |
| 190 | SC->ForgetSema(); |
| 191 | |
| 192 | // Detach from the external Sema source. |
| 193 | if (ExternalSemaSource *ExternalSema |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 194 | = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) |
Douglas Gregor | 46ea32a | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 195 | ExternalSema->ForgetSema(); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 199 | /// If there is already an implicit cast, merge into the existing one. |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 200 | /// The result is of the given category. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 202 | CastKind Kind, ExprValueKind VK, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 203 | const CXXCastPath *BasePath) { |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 204 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 205 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Mon P Wang | 3a2c744 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 207 | if (ExprTy == TypeTy) |
| 208 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 210 | // If this is a derived-to-base cast to a through a virtual base, we |
| 211 | // need a vtable. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 212 | if (Kind == CK_DerivedToBase && |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 213 | BasePathInvolvesVirtualBase(*BasePath)) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 214 | QualType T = Expr->getType(); |
| 215 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 216 | T = Pointer->getPointeeType(); |
| 217 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
| 218 | MarkVTableUsed(Expr->getLocStart(), |
| 219 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 220 | } |
| 221 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 222 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 223 | if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) { |
Anders Carlsson | 4c5fad3 | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 224 | ImpCast->setType(Ty); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 225 | ImpCast->setValueKind(VK); |
Anders Carlsson | 4c5fad3 | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | } |
| 229 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 230 | Expr = ImplicitCastExpr::Create(Context, Ty, Kind, Expr, BasePath, VK); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 231 | } |
| 232 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 233 | ExprValueKind Sema::CastCategory(Expr *E) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 234 | Expr::Classification Classification = E->Classify(Context); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 235 | return Classification.isRValue() ? VK_RValue : |
| 236 | (Classification.isLValue() ? VK_LValue : VK_XValue); |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 239 | /// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector. |
| 240 | static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { |
| 241 | if (D->isUsed()) |
| 242 | return true; |
| 243 | |
| 244 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 245 | // UnusedFileScopedDecls stores the first declaration. |
| 246 | // The declaration may have become definition so check again. |
| 247 | const FunctionDecl *DeclToCheck; |
| 248 | if (FD->hasBody(DeclToCheck)) |
| 249 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 250 | |
| 251 | // Later redecls may add new information resulting in not having to warn, |
| 252 | // so check again. |
| 253 | DeclToCheck = FD->getMostRecentDeclaration(); |
| 254 | if (DeclToCheck != FD) |
| 255 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 256 | } |
| 257 | |
| 258 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 259 | // UnusedFileScopedDecls stores the first declaration. |
| 260 | // The declaration may have become definition so check again. |
| 261 | const VarDecl *DeclToCheck = VD->getDefinition(); |
| 262 | if (DeclToCheck) |
| 263 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 264 | |
| 265 | // Later redecls may add new information resulting in not having to warn, |
| 266 | // so check again. |
| 267 | DeclToCheck = VD->getMostRecentDeclaration(); |
| 268 | if (DeclToCheck != VD) |
| 269 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 270 | } |
| 271 | |
| 272 | return false; |
| 273 | } |
| 274 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 275 | namespace { |
| 276 | struct UndefinedInternal { |
| 277 | NamedDecl *decl; |
| 278 | FullSourceLoc useLoc; |
| 279 | |
| 280 | UndefinedInternal(NamedDecl *decl, FullSourceLoc useLoc) |
| 281 | : decl(decl), useLoc(useLoc) {} |
| 282 | }; |
| 283 | |
| 284 | bool operator<(const UndefinedInternal &l, const UndefinedInternal &r) { |
| 285 | return l.useLoc.isBeforeInTranslationUnitThan(r.useLoc); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /// checkUndefinedInternals - Check for undefined objects with internal linkage. |
| 290 | static void checkUndefinedInternals(Sema &S) { |
| 291 | if (S.UndefinedInternals.empty()) return; |
| 292 | |
| 293 | // Collect all the still-undefined entities with internal linkage. |
| 294 | llvm::SmallVector<UndefinedInternal, 16> undefined; |
| 295 | for (llvm::DenseMap<NamedDecl*,SourceLocation>::iterator |
| 296 | i = S.UndefinedInternals.begin(), e = S.UndefinedInternals.end(); |
| 297 | i != e; ++i) { |
| 298 | NamedDecl *decl = i->first; |
| 299 | |
| 300 | // Ignore attributes that have become invalid. |
| 301 | if (decl->isInvalidDecl()) continue; |
| 302 | |
| 303 | // __attribute__((weakref)) is basically a definition. |
| 304 | if (decl->hasAttr<WeakRefAttr>()) continue; |
| 305 | |
| 306 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
| 307 | if (fn->isPure() || fn->hasBody()) |
| 308 | continue; |
| 309 | } else { |
| 310 | if (cast<VarDecl>(decl)->hasDefinition() != VarDecl::DeclarationOnly) |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | // We build a FullSourceLoc so that we can sort with array_pod_sort. |
| 315 | FullSourceLoc loc(i->second, S.Context.getSourceManager()); |
| 316 | undefined.push_back(UndefinedInternal(decl, loc)); |
| 317 | } |
| 318 | |
| 319 | if (undefined.empty()) return; |
| 320 | |
| 321 | // Sort (in order of use site) so that we're not (as) dependent on |
| 322 | // the iteration order through an llvm::DenseMap. |
| 323 | llvm::array_pod_sort(undefined.begin(), undefined.end()); |
| 324 | |
| 325 | for (llvm::SmallVectorImpl<UndefinedInternal>::iterator |
| 326 | i = undefined.begin(), e = undefined.end(); i != e; ++i) { |
| 327 | NamedDecl *decl = i->decl; |
| 328 | S.Diag(decl->getLocation(), diag::warn_undefined_internal) |
| 329 | << isa<VarDecl>(decl) << decl; |
| 330 | S.Diag(i->useLoc, diag::note_used_here); |
| 331 | } |
| 332 | } |
| 333 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 334 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 335 | /// translation unit when EOF is reached and all but the top-level scope is |
| 336 | /// popped. |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 337 | void Sema::ActOnEndOfTranslationUnit() { |
| 338 | // At PCH writing, implicit instantiations and VTable handling info are |
| 339 | // stored and performed when the PCH is included. |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 340 | if (CompleteTranslationUnit) { |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 341 | // If any dynamic classes have their key function defined within |
| 342 | // this translation unit, then those vtables are considered "used" and must |
| 343 | // be emitted. |
| 344 | for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 345 | assert(!DynamicClasses[I]->isDependentType() && |
| 346 | "Should not see dependent types here!"); |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 347 | if (const CXXMethodDecl *KeyFunction |
| 348 | = Context.getKeyFunction(DynamicClasses[I])) { |
| 349 | const FunctionDecl *Definition = 0; |
| 350 | if (KeyFunction->hasBody(Definition)) |
| 351 | MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true); |
| 352 | } |
| 353 | } |
| 354 | |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 355 | // If DefinedUsedVTables ends up marking any virtual member functions it |
| 356 | // might lead to more pending template instantiations, which we then need |
| 357 | // to instantiate. |
| 358 | DefineUsedVTables(); |
Argyrios Kyrtzidis | 0e03638 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 359 | |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 360 | // C++: Perform implicit template instantiations. |
| 361 | // |
| 362 | // FIXME: When we perform these implicit instantiations, we do not |
| 363 | // carefully keep track of the point of instantiation (C++ [temp.point]). |
| 364 | // This means that name lookup that occurs within the template |
| 365 | // instantiation will always happen at the end of the translation unit, |
| 366 | // so it will find some names that should not be found. Although this is |
| 367 | // common behavior for C++ compilers, it is technically wrong. In the |
| 368 | // future, we either need to be able to filter the results of name lookup |
| 369 | // or we need to perform template instantiations earlier. |
| 370 | PerformPendingInstantiations(); |
| 371 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 372 | |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 373 | // Remove file scoped decls that turned out to be used. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 374 | UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(), |
| 375 | UnusedFileScopedDecls.end(), |
| 376 | std::bind1st(std::ptr_fun(ShouldRemoveFromUnused), |
| 377 | this)), |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 378 | UnusedFileScopedDecls.end()); |
Douglas Gregor | 47268a3 | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 380 | if (!CompleteTranslationUnit) { |
| 381 | TUScope = 0; |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 382 | return; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 383 | } |
Argyrios Kyrtzidis | 72b9057 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 385 | // Check for #pragma weak identifiers that were never declared |
| 386 | // FIXME: This will cause diagnostics to be emitted in a non-determinstic |
| 387 | // order! Iterating over a densemap like this is bad. |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 388 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 389 | I = WeakUndeclaredIdentifiers.begin(), |
| 390 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 391 | if (I->second.getUsed()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 393 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 394 | << I->first; |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 397 | // C99 6.9.2p2: |
| 398 | // A declaration of an identifier for an object that has file |
| 399 | // scope without an initializer, and without a storage-class |
| 400 | // specifier or with the storage-class specifier static, |
| 401 | // constitutes a tentative definition. If a translation unit |
| 402 | // contains one or more tentative definitions for an identifier, |
| 403 | // and the translation unit contains no external definition for |
| 404 | // that identifier, then the behavior is exactly as if the |
| 405 | // translation unit contains a file scope declaration of that |
| 406 | // identifier, with the composite type as of the end of the |
| 407 | // translation unit, with an initializer equal to 0. |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 408 | llvm::SmallSet<VarDecl *, 32> Seen; |
| 409 | for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) { |
| 410 | VarDecl *VD = TentativeDefinitions[i]->getActingDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 412 | // If the tentative definition was completed, getActingDefinition() returns |
| 413 | // null. If we've already seen this variable before, insert()'s second |
| 414 | // return value is false. |
| 415 | if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD)) |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 416 | continue; |
| 417 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 419 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | if (RequireCompleteType(VD->getLocation(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 421 | ArrayT->getElementType(), |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 422 | diag::err_tentative_def_incomplete_type_arr)) { |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 423 | VD->setInvalidDecl(); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 424 | continue; |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 427 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 428 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 429 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 430 | QualType T = Context.getConstantArrayType(ArrayT->getElementType(), |
| 431 | One, ArrayType::Normal, 0); |
Chris Lattner | 63d65f8 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 432 | VD->setType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | b6c8c8b | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 434 | diag::err_tentative_def_incomplete_type)) |
| 435 | VD->setInvalidDecl(); |
| 436 | |
| 437 | // Notify the consumer that we've completed a tentative definition. |
| 438 | if (!VD->isInvalidDecl()) |
| 439 | Consumer.CompleteTentativeDefinition(VD); |
| 440 | |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 441 | } |
Argyrios Kyrtzidis | 43f0a7c | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 442 | |
| 443 | // If there were errors, disable 'unused' warnings since they will mostly be |
| 444 | // noise. |
| 445 | if (!Diags.hasErrorOccurred()) { |
| 446 | // Output warning for unused file scoped decls. |
| 447 | for (llvm::SmallVectorImpl<const DeclaratorDecl*>::iterator |
| 448 | I = UnusedFileScopedDecls.begin(), |
| 449 | E = UnusedFileScopedDecls.end(); I != E; ++I) { |
| 450 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 451 | const FunctionDecl *DiagD; |
| 452 | if (!FD->hasBody(DiagD)) |
| 453 | DiagD = FD; |
Argyrios Kyrtzidis | 48b8959 | 2011-03-03 17:47:42 +0000 | [diff] [blame] | 454 | if (DiagD->isDeleted()) |
| 455 | continue; // Deleted functions are supposed to be unused. |
Argyrios Kyrtzidis | 43f0a7c | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 456 | Diag(DiagD->getLocation(), |
| 457 | isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function |
| 458 | : diag::warn_unused_function) |
| 459 | << DiagD->getDeclName(); |
| 460 | } else { |
| 461 | const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition(); |
| 462 | if (!DiagD) |
| 463 | DiagD = cast<VarDecl>(*I); |
| 464 | Diag(DiagD->getLocation(), diag::warn_unused_variable) |
| 465 | << DiagD->getDeclName(); |
| 466 | } |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 467 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 468 | |
| 469 | checkUndefinedInternals(*this); |
Argyrios Kyrtzidis | 49b96d1 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 470 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 471 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 472 | // Check we've noticed that we're no longer parsing the initializer for every |
| 473 | // variable. If we miss cases, then at best we have a performance issue and |
| 474 | // at worst a rejects-valid bug. |
| 475 | assert(ParsingInitForAutoVars.empty() && |
| 476 | "Didn't unmark var as having its initializer parsed"); |
| 477 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 478 | TUScope = 0; |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 482 | //===----------------------------------------------------------------------===// |
| 483 | // Helper functions. |
| 484 | //===----------------------------------------------------------------------===// |
| 485 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 486 | DeclContext *Sema::getFunctionLevelDeclContext() { |
John McCall | db0ee1d | 2009-12-19 10:53:49 +0000 | [diff] [blame] | 487 | DeclContext *DC = CurContext; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | d900831 | 2010-05-22 16:25:05 +0000 | [diff] [blame] | 489 | while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 490 | DC = DC->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 492 | return DC; |
| 493 | } |
| 494 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 495 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 496 | /// to the function decl for the function being parsed. If we're currently |
| 497 | /// in a 'block', this returns the containing context. |
| 498 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 499 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 500 | return dyn_cast<FunctionDecl>(DC); |
| 501 | } |
| 502 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 503 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 504 | DeclContext *DC = getFunctionLevelDeclContext(); |
Steve Naroff | d7612e1 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 505 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 506 | } |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 507 | |
| 508 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | 8517d9b | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 509 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 510 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 511 | return cast<NamedDecl>(DC); |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 515 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 516 | if (!isActive()) |
| 517 | return; |
| 518 | |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 519 | if (llvm::Optional<TemplateDeductionInfo*> Info = SemaRef.isSFINAEContext()) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 520 | switch (DiagnosticIDs::getDiagnosticSFINAEResponse(getDiagID())) { |
| 521 | case DiagnosticIDs::SFINAE_Report: |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 522 | // Fall through; we'll report the diagnostic below. |
| 523 | break; |
| 524 | |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 525 | case DiagnosticIDs::SFINAE_AccessControl: |
| 526 | // Unless access checking is specifically called out as a SFINAE |
| 527 | // error, report this diagnostic. |
| 528 | if (!SemaRef.AccessCheckingSFINAE) |
| 529 | break; |
| 530 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 531 | case DiagnosticIDs::SFINAE_SubstitutionFailure: |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 532 | // Count this failure so that we know that template argument deduction |
| 533 | // has failed. |
| 534 | ++SemaRef.NumSFINAEErrors; |
| 535 | SemaRef.Diags.setLastDiagnosticIgnored(); |
| 536 | SemaRef.Diags.Clear(); |
| 537 | Clear(); |
| 538 | return; |
| 539 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 540 | case DiagnosticIDs::SFINAE_Suppress: |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 541 | // Make a copy of this suppressed diagnostic and store it with the |
| 542 | // template-deduction information; |
Douglas Gregor | b535041 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 543 | FlushCounts(); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 544 | DiagnosticInfo DiagInfo(&SemaRef.Diags); |
Douglas Gregor | b535041 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 545 | |
Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 546 | if (*Info) |
| 547 | (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(), |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 548 | PartialDiagnostic(DiagInfo, |
| 549 | SemaRef.Context.getDiagAllocator())); |
| 550 | |
| 551 | // Suppress this diagnostic. |
| 552 | SemaRef.Diags.setLastDiagnosticIgnored(); |
| 553 | SemaRef.Diags.Clear(); |
| 554 | Clear(); |
| 555 | return; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // Emit the diagnostic. |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 560 | if (!this->Emit()) |
| 561 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 563 | // If this is not a note, and we're in a template instantiation |
| 564 | // that is different from the last template instantiation where |
| 565 | // we emitted an error, print a template instantiation |
| 566 | // backtrace. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 567 | if (!DiagnosticIDs::isBuiltinNote(DiagID) && |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 568 | !SemaRef.ActiveTemplateInstantiations.empty() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | SemaRef.ActiveTemplateInstantiations.back() |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 570 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 571 | SemaRef.PrintInstantiationStack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | 25a88bb | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 573 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 574 | } |
| 575 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | eab5d1e | 2010-03-25 22:17:48 +0000 | [diff] [blame] | 577 | Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 578 | DiagnosticBuilder DB = Diags.Report(Loc, DiagID); |
Douglas Gregor | eab5d1e | 2010-03-25 22:17:48 +0000 | [diff] [blame] | 579 | return SemaDiagnosticBuilder(DB, *this, DiagID); |
| 580 | } |
| 581 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 582 | Sema::SemaDiagnosticBuilder |
| 583 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 584 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 585 | PD.Emit(Builder); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 587 | return Builder; |
| 588 | } |
| 589 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 590 | /// \brief Determines the active Scope associated with the given declaration |
| 591 | /// context. |
| 592 | /// |
| 593 | /// This routine maps a declaration context to the active Scope object that |
| 594 | /// represents that declaration context in the parser. It is typically used |
| 595 | /// from "scope-less" code (e.g., template instantiation, lazy creation of |
| 596 | /// declarations) that injects a name for name-lookup purposes and, therefore, |
| 597 | /// must update the Scope. |
| 598 | /// |
| 599 | /// \returns The scope corresponding to the given declaraion context, or NULL |
| 600 | /// if no such scope is open. |
| 601 | Scope *Sema::getScopeForContext(DeclContext *Ctx) { |
| 602 | |
| 603 | if (!Ctx) |
| 604 | return 0; |
| 605 | |
| 606 | Ctx = Ctx->getPrimaryContext(); |
| 607 | for (Scope *S = getCurScope(); S; S = S->getParent()) { |
Sebastian Redl | cddc69f | 2010-07-08 23:07:34 +0000 | [diff] [blame] | 608 | // Ignore scopes that cannot have declarations. This is important for |
| 609 | // out-of-line definitions of static class members. |
| 610 | if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) |
| 611 | if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity())) |
| 612 | if (Ctx == Entity->getPrimaryContext()) |
| 613 | return S; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | } |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 618 | |
| 619 | /// \brief Enter a new function scope |
| 620 | void Sema::PushFunctionScope() { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 621 | if (FunctionScopes.size() == 1) { |
| 622 | // Use the "top" function scope rather than having to allocate |
| 623 | // memory for a new scope. |
Argyrios Kyrtzidis | 8fc32d2 | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 624 | FunctionScopes.back()->Clear(); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 625 | FunctionScopes.push_back(FunctionScopes.back()); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 626 | return; |
| 627 | } |
| 628 | |
Argyrios Kyrtzidis | 8fc32d2 | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 629 | FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics())); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { |
Argyrios Kyrtzidis | 8fc32d2 | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 633 | FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(), |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 634 | BlockScope, Block)); |
| 635 | } |
| 636 | |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 637 | void Sema::PopFunctionOrBlockScope(const AnalysisBasedWarnings::Policy *WP, |
| 638 | const Decl *D, const BlockExpr *blkExpr) { |
| 639 | FunctionScopeInfo *Scope = FunctionScopes.pop_back_val(); |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 640 | assert(!FunctionScopes.empty() && "mismatched push/pop!"); |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 641 | |
| 642 | // Issue any analysis-based warnings. |
| 643 | if (WP && D) |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 644 | AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 645 | else { |
| 646 | for (llvm::SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
| 647 | i = Scope->PossiblyUnreachableDiags.begin(), |
| 648 | e = Scope->PossiblyUnreachableDiags.end(); |
| 649 | i != e; ++i) { |
| 650 | const sema::PossiblyUnreachableDiag &D = *i; |
| 651 | Diag(D.Loc, D.PD); |
| 652 | } |
| 653 | } |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 654 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 655 | if (FunctionScopes.back() != Scope) { |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 656 | delete Scope; |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 657 | } |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /// \brief Determine whether any errors occurred within this function/method/ |
| 661 | /// block. |
| 662 | bool Sema::hasAnyErrorsInThisFunction() const { |
Argyrios Kyrtzidis | 8fc32d2 | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 663 | return getCurFunction()->ErrorTrap.hasErrorOccurred(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | BlockScopeInfo *Sema::getCurBlock() { |
| 667 | if (FunctionScopes.empty()) |
| 668 | return 0; |
| 669 | |
| 670 | return dyn_cast<BlockScopeInfo>(FunctionScopes.back()); |
| 671 | } |
John McCall | 76bd1f3 | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 672 | |
| 673 | // Pin this vtable to this file. |
| 674 | ExternalSemaSource::~ExternalSemaSource() {} |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 675 | |
Sebastian Redl | 8c84571 | 2010-09-28 20:23:00 +0000 | [diff] [blame] | 676 | std::pair<ObjCMethodList, ObjCMethodList> |
| 677 | ExternalSemaSource::ReadMethodPool(Selector Sel) { |
| 678 | return std::pair<ObjCMethodList, ObjCMethodList>(); |
| 679 | } |
| 680 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 681 | void PrettyDeclStackTraceEntry::print(llvm::raw_ostream &OS) const { |
| 682 | SourceLocation Loc = this->Loc; |
| 683 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
| 684 | if (Loc.isValid()) { |
| 685 | Loc.print(OS, S.getSourceManager()); |
| 686 | OS << ": "; |
| 687 | } |
| 688 | OS << Message; |
| 689 | |
| 690 | if (TheDecl && isa<NamedDecl>(TheDecl)) { |
| 691 | std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString(); |
| 692 | if (!Name.empty()) |
| 693 | OS << " '" << Name << '\''; |
| 694 | } |
| 695 | |
| 696 | OS << '\n'; |
| 697 | } |