Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 10 | // This file implements the actions class which performs semantic analysis and |
| 11 | // builds an AST out of a parse stream. |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 15 | #include "Sema.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 16 | #include "TargetAttributesSema.h" |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
John McCall | fceb64b | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/APFloat.h" |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 639cccc | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTDiagnostic.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 28 | using namespace clang; |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 29 | |
| 30 | FunctionScopeInfo::~FunctionScopeInfo() { } |
| 31 | |
| 32 | void FunctionScopeInfo::Clear(unsigned NumErrors) { |
| 33 | NeedsScopeChecking = false; |
| 34 | LabelMap.clear(); |
| 35 | SwitchStack.clear(); |
| 36 | NumErrorsAtStartOfFunction = NumErrors; |
| 37 | } |
| 38 | |
| 39 | BlockScopeInfo::~BlockScopeInfo() { } |
| 40 | |
Steve Naroff | c62adb6 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 41 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 42 | TUScope = S; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 43 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 45 | if (PP.getTargetInfo().getPointerWidth(0) >= 64) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 46 | TypeSourceInfo *TInfo; |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 48 | // Install [u]int128_t for 64-bit targets. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 49 | TInfo = Context.getTrivialTypeSourceInfo(Context.Int128Ty); |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 50 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 51 | SourceLocation(), |
| 52 | &Context.Idents.get("__int128_t"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 53 | TInfo), TUScope); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 54 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 55 | TInfo = Context.getTrivialTypeSourceInfo(Context.UnsignedInt128Ty); |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 56 | PushOnScopeChains(TypedefDecl::Create(Context, CurContext, |
| 57 | SourceLocation(), |
| 58 | &Context.Idents.get("__uint128_t"), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 59 | TInfo), TUScope); |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 60 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
| 62 | |
Chris Lattner | fe0e0af | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 63 | if (!PP.getLangOptions().ObjC1) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Steve Naroff | 853308d | 2009-06-16 00:20:10 +0000 | [diff] [blame] | 65 | // Built-in ObjC types may already be set by PCHReader (hence isNull checks). |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 66 | if (Context.getObjCSelType().isNull()) { |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 67 | // Create the built-in typedef for 'SEL'. |
Fariborz Jahanian | 0afc555 | 2009-11-23 18:04:25 +0000 | [diff] [blame] | 68 | QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy); |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 69 | TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 70 | TypedefDecl *SelTypedef |
| 71 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 72 | &Context.Idents.get("SEL"), SelInfo); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 73 | PushOnScopeChains(SelTypedef, TUScope); |
| 74 | Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); |
Fariborz Jahanian | 04b258c | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 75 | Context.ObjCSelRedefinitionType = Context.getObjCSelType(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | 5a92bab | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 5a92bab | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 78 | // Synthesize "@class Protocol; |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 79 | if (Context.getObjCProtoType().isNull()) { |
| 80 | ObjCInterfaceDecl *ProtocolDecl = |
| 81 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | &Context.Idents.get("Protocol"), |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 83 | SourceLocation(), true); |
| 84 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
Fariborz Jahanian | 1e3609f | 2009-11-18 23:15:37 +0000 | [diff] [blame] | 85 | PushOnScopeChains(ProtocolDecl, TUScope, false); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 86 | } |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 87 | // Create the built-in typedef for 'id'. |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 88 | if (Context.getObjCIdType().isNull()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 89 | QualType IdT = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy); |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 90 | TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(IdT); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 91 | TypedefDecl *IdTypedef |
| 92 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 93 | &Context.Idents.get("id"), IdInfo); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 94 | PushOnScopeChains(IdTypedef, TUScope); |
| 95 | Context.setObjCIdType(Context.getTypeDeclType(IdTypedef)); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 96 | Context.ObjCIdRedefinitionType = Context.getObjCIdType(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 97 | } |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 98 | // Create the built-in typedef for 'Class'. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 99 | if (Context.getObjCClassType().isNull()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 100 | QualType ClassType |
| 101 | = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy); |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 102 | TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(ClassType); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 103 | TypedefDecl *ClassTypedef |
| 104 | = TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 105 | &Context.Idents.get("Class"), ClassInfo); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 106 | PushOnScopeChains(ClassTypedef, TUScope); |
| 107 | Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef)); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 108 | Context.ObjCClassRedefinitionType = Context.getObjCClassType(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 109 | } |
Steve Naroff | 7f549f1 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Douglas Gregor | 54feb84 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 112 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 113 | bool CompleteTranslationUnit, |
| 114 | CodeCompleteConsumer *CodeCompleter) |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 115 | : TheTargetAttributesSema(0), |
| 116 | LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 118 | ExternalSource(0), CodeCompleter(CodeCompleter), CurContext(0), |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 119 | PackContext(0), TopFunctionScope(0), ParsingDeclDepth(0), |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 120 | IdResolver(pp.getLangOptions()), StdNamespace(0), StdBadAlloc(0), |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 121 | GlobalNewDeleteDeclared(false), |
Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 122 | CompleteTranslationUnit(CompleteTranslationUnit), |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 123 | NumSFINAEErrors(0), NonInstantiationEntries(0), |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 124 | CurrentInstantiationScope(0), TyposCorrected(0), |
| 125 | AnalysisWarnings(*this) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 126 | { |
Steve Naroff | 7f549f1 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 127 | TUScope = 0; |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 128 | if (getLangOptions().CPlusPlus) |
| 129 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 131 | // Tell diagnostics how to render things from the AST library. |
Douglas Gregor | 639cccc | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 132 | PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 133 | &Context); |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 134 | |
| 135 | ExprEvalContexts.push_back( |
| 136 | ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0)); |
Steve Naroff | 38d31b4 | 2007-02-28 01:22:02 +0000 | [diff] [blame] | 137 | } |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 138 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 139 | Sema::~Sema() { |
| 140 | if (PackContext) FreePackedContext(); |
| 141 | delete TheTargetAttributesSema; |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 142 | while (!FunctionScopes.empty()) |
| 143 | PopFunctionOrBlockScope(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 147 | /// If there is already an implicit cast, merge into the existing one. |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 148 | /// If isLvalue, the result of the cast is an lvalue. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, |
Anders Carlsson | 97597938 | 2010-04-23 22:18:37 +0000 | [diff] [blame] | 150 | CastExpr::CastKind Kind, |
Anders Carlsson | 0c509ee | 2010-04-24 16:57:13 +0000 | [diff] [blame] | 151 | bool isLvalue, CXXBaseSpecifierArray BasePath) { |
Mon P Wang | 74b3207 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 152 | QualType ExprTy = Context.getCanonicalType(Expr->getType()); |
| 153 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Mon P Wang | 74b3207 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 155 | if (ExprTy == TypeTy) |
| 156 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
John McCall | fceb64b | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 158 | if (Expr->getType()->isPointerType() && Ty->isPointerType()) { |
| 159 | QualType ExprBaseType = cast<PointerType>(ExprTy)->getPointeeType(); |
| 160 | QualType BaseType = cast<PointerType>(TypeTy)->getPointeeType(); |
Mon P Wang | 74b3207 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 161 | if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) { |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 162 | Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast) |
| 163 | << Expr->getSourceRange(); |
Mon P Wang | 74b3207 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 167 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) { |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 168 | if (ImpCast->getCastKind() == Kind && BasePath.empty()) { |
Anders Carlsson | 4e66cda | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 169 | ImpCast->setType(Ty); |
| 170 | ImpCast->setLvalueCast(isLvalue); |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | |
Anders Carlsson | 7d96cd7 | 2010-04-24 16:34:21 +0000 | [diff] [blame] | 175 | Expr = new (Context) ImplicitCastExpr(Ty, Kind, Expr, BasePath, isLvalue); |
Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chris Lattner | 57c523f | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 178 | void Sema::DeleteExpr(ExprTy *E) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 179 | if (E) static_cast<Expr*>(E)->Destroy(Context); |
Chris Lattner | 57c523f | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 180 | } |
| 181 | void Sema::DeleteStmt(StmtTy *S) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 182 | if (S) static_cast<Stmt*>(S)->Destroy(Context); |
Chris Lattner | 57c523f | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chris Lattner | f440440 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 185 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 186 | /// translation unit when EOF is reached and all but the top-level scope is |
| 187 | /// popped. |
Douglas Gregor | fb8b27d | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 188 | void Sema::ActOnEndOfTranslationUnit() { |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 189 | while (1) { |
| 190 | // C++: Perform implicit template instantiations. |
| 191 | // |
| 192 | // FIXME: When we perform these implicit instantiations, we do not carefully |
| 193 | // keep track of the point of instantiation (C++ [temp.point]). This means |
| 194 | // that name lookup that occurs within the template instantiation will |
| 195 | // always happen at the end of the translation unit, so it will find |
| 196 | // some names that should not be found. Although this is common behavior |
| 197 | // for C++ compilers, it is technically wrong. In the future, we either need |
| 198 | // to be able to filter the results of name lookup or we need to perform |
| 199 | // template instantiations earlier. |
| 200 | PerformPendingImplicitInstantiations(); |
| 201 | |
| 202 | /// If ProcessPendingClassesWithUnmarkedVirtualMembers ends up marking |
| 203 | /// any virtual member functions it might lead to more pending template |
| 204 | /// instantiations, which is why we need to loop here. |
| 205 | if (!ProcessPendingClassesWithUnmarkedVirtualMembers()) |
| 206 | break; |
| 207 | } |
| 208 | |
Douglas Gregor | fb8b27d | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 209 | // Remove functions that turned out to be used. |
| 210 | UnusedStaticFuncs.erase(std::remove_if(UnusedStaticFuncs.begin(), |
| 211 | UnusedStaticFuncs.end(), |
| 212 | std::mem_fun(&FunctionDecl::isUsed)), |
| 213 | UnusedStaticFuncs.end()); |
| 214 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 215 | // Check for #pragma weak identifiers that were never declared |
| 216 | // FIXME: This will cause diagnostics to be emitted in a non-determinstic |
| 217 | // order! Iterating over a densemap like this is bad. |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 218 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 219 | I = WeakUndeclaredIdentifiers.begin(), |
| 220 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 221 | if (I->second.getUsed()) continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 223 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 224 | << I->first; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Douglas Gregor | 54feb84 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 227 | if (!CompleteTranslationUnit) |
| 228 | return; |
| 229 | |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 230 | // C99 6.9.2p2: |
| 231 | // A declaration of an identifier for an object that has file |
| 232 | // scope without an initializer, and without a storage-class |
| 233 | // specifier or with the storage-class specifier static, |
| 234 | // constitutes a tentative definition. If a translation unit |
| 235 | // contains one or more tentative definitions for an identifier, |
| 236 | // and the translation unit contains no external definition for |
| 237 | // that identifier, then the behavior is exactly as if the |
| 238 | // translation unit contains a file scope declaration of that |
| 239 | // identifier, with the composite type as of the end of the |
| 240 | // translation unit, with an initializer equal to 0. |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 241 | llvm::SmallSet<VarDecl *, 32> Seen; |
| 242 | for (unsigned i = 0, e = TentativeDefinitions.size(); i != e; ++i) { |
| 243 | VarDecl *VD = TentativeDefinitions[i]->getActingDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 245 | // If the tentative definition was completed, getActingDefinition() returns |
| 246 | // null. If we've already seen this variable before, insert()'s second |
| 247 | // return value is false. |
| 248 | if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD)) |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 249 | continue; |
| 250 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 252 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | if (RequireCompleteType(VD->getLocation(), |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 254 | ArrayT->getElementType(), |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 255 | diag::err_tentative_def_incomplete_type_arr)) { |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 256 | VD->setInvalidDecl(); |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 257 | continue; |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 258 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 260 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 261 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 262 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 263 | QualType T = Context.getConstantArrayType(ArrayT->getElementType(), |
| 264 | One, ArrayType::Normal, 0); |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 265 | VD->setType(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 267 | diag::err_tentative_def_incomplete_type)) |
| 268 | VD->setInvalidDecl(); |
| 269 | |
| 270 | // Notify the consumer that we've completed a tentative definition. |
| 271 | if (!VD->isInvalidDecl()) |
| 272 | Consumer.CompleteTentativeDefinition(VD); |
| 273 | |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 274 | } |
Tanya Lattner | 9007380 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 275 | |
| 276 | // Output warning for unused functions. |
| 277 | for (std::vector<FunctionDecl*>::iterator |
| 278 | F = UnusedStaticFuncs.begin(), |
| 279 | FEnd = UnusedStaticFuncs.end(); |
| 280 | F != FEnd; |
| 281 | ++F) |
| 282 | Diag((*F)->getLocation(), diag::warn_unused_function) << (*F)->getDeclName(); |
| 283 | |
Chris Lattner | f440440 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 287 | //===----------------------------------------------------------------------===// |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 288 | // Helper functions. |
| 289 | //===----------------------------------------------------------------------===// |
| 290 | |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 291 | DeclContext *Sema::getFunctionLevelDeclContext() { |
John McCall | b878801 | 2009-12-19 10:53:49 +0000 | [diff] [blame] | 292 | DeclContext *DC = CurContext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 294 | while (isa<BlockDecl>(DC)) |
| 295 | DC = DC->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 297 | return DC; |
| 298 | } |
| 299 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 300 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 301 | /// to the function decl for the function being parsed. If we're currently |
| 302 | /// in a 'block', this returns the containing context. |
| 303 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 304 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 305 | return dyn_cast<FunctionDecl>(DC); |
| 306 | } |
| 307 | |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 308 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 309 | DeclContext *DC = getFunctionLevelDeclContext(); |
Steve Naroff | ecf2bb8 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 310 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 312 | |
| 313 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 314 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 315 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 316 | return cast<NamedDecl>(DC); |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 320 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 321 | if (!this->Emit()) |
| 322 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 324 | // If this is not a note, and we're in a template instantiation |
| 325 | // that is different from the last template instantiation where |
| 326 | // we emitted an error, print a template instantiation |
| 327 | // backtrace. |
| 328 | if (!SemaRef.Diags.isBuiltinNote(DiagID) && |
| 329 | !SemaRef.ActiveTemplateInstantiations.empty() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | SemaRef.ActiveTemplateInstantiations.back() |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 331 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 332 | SemaRef.PrintInstantiationStack(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 334 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 335 | } |
| 336 | } |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 337 | |
Douglas Gregor | 210b590 | 2010-03-25 22:17:48 +0000 | [diff] [blame] | 338 | Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
| 339 | if (isSFINAEContext()) { |
| 340 | switch (Diagnostic::getDiagnosticSFINAEResponse(DiagID)) { |
| 341 | case Diagnostic::SFINAE_Report: |
| 342 | // Fall through; we'll report the diagnostic below. |
| 343 | break; |
| 344 | |
| 345 | case Diagnostic::SFINAE_SubstitutionFailure: |
| 346 | // Count this failure so that we know that template argument deduction |
| 347 | // has failed. |
| 348 | ++NumSFINAEErrors; |
| 349 | // Fall through |
| 350 | |
| 351 | case Diagnostic::SFINAE_Suppress: |
| 352 | // Suppress this diagnostic. |
| 353 | Diags.setLastDiagnosticIgnored(); |
| 354 | return SemaDiagnosticBuilder(*this); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); |
| 359 | return SemaDiagnosticBuilder(DB, *this, DiagID); |
| 360 | } |
| 361 | |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 362 | Sema::SemaDiagnosticBuilder |
| 363 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 364 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 365 | PD.Emit(Builder); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 367 | return Builder; |
| 368 | } |
| 369 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 370 | |
| 371 | /// \brief Enter a new function scope |
| 372 | void Sema::PushFunctionScope() { |
| 373 | if (FunctionScopes.empty()) { |
| 374 | // Use the "top" function scope rather than having to allocate memory for |
| 375 | // a new scope. |
| 376 | TopFunctionScope.Clear(getDiagnostics().getNumErrors()); |
| 377 | FunctionScopes.push_back(&TopFunctionScope); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | FunctionScopes.push_back( |
| 382 | new FunctionScopeInfo(getDiagnostics().getNumErrors())); |
| 383 | } |
| 384 | |
| 385 | void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { |
| 386 | FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics().getNumErrors(), |
| 387 | BlockScope, Block)); |
| 388 | } |
| 389 | |
| 390 | void Sema::PopFunctionOrBlockScope() { |
| 391 | if (FunctionScopes.back() != &TopFunctionScope) |
| 392 | delete FunctionScopes.back(); |
| 393 | else |
| 394 | TopFunctionScope.Clear(getDiagnostics().getNumErrors()); |
| 395 | |
| 396 | FunctionScopes.pop_back(); |
| 397 | } |
| 398 | |
| 399 | /// \brief Determine whether any errors occurred within this function/method/ |
| 400 | /// block. |
| 401 | bool Sema::hasAnyErrorsInThisFunction() const { |
| 402 | unsigned NumErrors = TopFunctionScope.NumErrorsAtStartOfFunction; |
| 403 | if (!FunctionScopes.empty()) |
| 404 | NumErrors = FunctionScopes.back()->NumErrorsAtStartOfFunction; |
| 405 | return NumErrors != getDiagnostics().getNumErrors(); |
| 406 | } |
| 407 | |
| 408 | BlockScopeInfo *Sema::getCurBlock() { |
| 409 | if (FunctionScopes.empty()) |
| 410 | return 0; |
| 411 | |
| 412 | return dyn_cast<BlockScopeInfo>(FunctionScopes.back()); |
| 413 | } |