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 | |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
| 16 | #include "clang/Sema/DelayedDiagnostic.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 17 | #include "TargetAttributesSema.h" |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
John McCall | fceb64b | 2009-11-07 03:30:10 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/APFloat.h" |
John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 21 | #include "clang/Sema/CXXFieldCollector.h" |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 22 | #include "clang/Sema/TemplateDeduction.h" |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 23 | #include "clang/Sema/ExternalSemaSource.h" |
Sebastian Redl | c1ca90a | 2010-09-28 20:23:00 +0000 | [diff] [blame] | 24 | #include "clang/Sema/ObjCMethodList.h" |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 25 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Scope.h" |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 27 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 28 | #include "clang/Sema/SemaConsumer.h" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 29 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 639cccc | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 30 | #include "clang/AST/ASTDiagnostic.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 31 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 32 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 33 | #include "clang/AST/Expr.h" |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 34 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 35 | #include "clang/AST/StmtCXX.h" |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 36 | #include "clang/Lex/Preprocessor.h" |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 37 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 38 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 39 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | using namespace sema; |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 41 | |
| 42 | FunctionScopeInfo::~FunctionScopeInfo() { } |
| 43 | |
Argyrios Kyrtzidis | f51ec1d | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 44 | void FunctionScopeInfo::Clear() { |
John McCall | a95172b | 2010-08-01 00:26:45 +0000 | [diff] [blame] | 45 | HasBranchProtectedScope = false; |
| 46 | HasBranchIntoScope = false; |
| 47 | HasIndirectGoto = false; |
| 48 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 49 | SwitchStack.clear(); |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 50 | Returns.clear(); |
Argyrios Kyrtzidis | f51ec1d | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 51 | ErrorTrap.reset(); |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 52 | PossiblyUnreachableDiags.clear(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | BlockScopeInfo::~BlockScopeInfo() { } |
| 56 | |
Douglas Gregor | f11096c | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 57 | void Sema::ActOnTranslationUnitScope(Scope *S) { |
Steve Naroff | c62adb6 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 58 | TUScope = S; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 59 | PushDeclContext(S, Context.getTranslationUnitDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
John McCall | f24d7bb | 2010-05-28 18:45:08 +0000 | [diff] [blame] | 61 | VAListTagName = PP.getIdentifierInfo("__va_list_tag"); |
| 62 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame^] | 63 | if (PP.getLangOptions().ObjC1) { |
| 64 | // Synthesize "@class Protocol; |
| 65 | if (Context.getObjCProtoType().isNull()) { |
| 66 | ObjCInterfaceDecl *ProtocolDecl = |
| 67 | ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(), |
| 68 | &Context.Idents.get("Protocol"), |
| 69 | SourceLocation(), true); |
| 70 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 71 | PushOnScopeChains(ProtocolDecl, TUScope, false); |
| 72 | } |
Chris Lattner | 7d4f5c4 | 2009-04-30 06:18:40 +0000 | [diff] [blame] | 73 | } |
Steve Naroff | 7f549f1 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Douglas Gregor | 54feb84 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 76 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer, |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 77 | bool CompleteTranslationUnit, |
| 78 | CodeCompleteConsumer *CodeCompleter) |
Peter Collingbourne | 564c0fa | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 79 | : TheTargetAttributesSema(0), FPFeatures(pp.getLangOptions()), |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 80 | LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | Diags(PP.getDiagnostics()), SourceMgr(PP.getSourceManager()), |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 82 | CollectStats(false), ExternalSource(0), CodeCompleter(CodeCompleter), |
| 83 | CurContext(0), PackContext(0), MSStructPragmaOn(false), VisContext(0), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 84 | ExprNeedsCleanups(0), LateTemplateParser(0), OpaqueParser(0), |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 85 | IdResolver(pp.getLangOptions()), CXXTypeInfoDecl(0), MSVCGuidDecl(0), |
Bill Wendling | 73f59c4 | 2010-09-08 21:30:16 +0000 | [diff] [blame] | 86 | GlobalNewDeleteDeclared(false), |
Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 87 | CompleteTranslationUnit(CompleteTranslationUnit), |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 88 | NumSFINAEErrors(0), SuppressAccessChecking(false), |
| 89 | AccessCheckingSFINAE(false), InNonInstantiationSFINAEContext(false), |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 90 | NonInstantiationEntries(0), ArgumentPackSubstitutionIndex(-1), |
| 91 | CurrentInstantiationScope(0), TyposCorrected(0), |
Bill Wendling | 73f59c4 | 2010-09-08 21:30:16 +0000 | [diff] [blame] | 92 | AnalysisWarnings(*this) |
Douglas Gregor | 84d49a2 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 93 | { |
Steve Naroff | 7f549f1 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 94 | TUScope = 0; |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 95 | LoadedExternalKnownNamespaces = false; |
| 96 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 97 | if (getLangOptions().CPlusPlus) |
| 98 | FieldCollector.reset(new CXXFieldCollector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 100 | // Tell diagnostics how to render things from the AST library. |
Douglas Gregor | 639cccc | 2010-02-09 22:26:47 +0000 | [diff] [blame] | 101 | PP.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 102 | &Context); |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 103 | |
| 104 | ExprEvalContexts.push_back( |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 105 | ExpressionEvaluationContextRecord(PotentiallyEvaluated, 0, false)); |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 106 | |
Argyrios Kyrtzidis | f51ec1d | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 107 | FunctionScopes.push_back(new FunctionScopeInfo(Diags)); |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void Sema::Initialize() { |
| 111 | // Tell the AST consumer about this Sema object. |
| 112 | Consumer.Initialize(Context); |
| 113 | |
| 114 | // FIXME: Isn't this redundant with the initialization above? |
| 115 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) |
| 116 | SC->InitializeSema(*this); |
| 117 | |
| 118 | // Tell the external Sema source about this Sema object. |
| 119 | if (ExternalSemaSource *ExternalSema |
| 120 | = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) |
| 121 | ExternalSema->InitializeSema(*this); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 122 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame^] | 123 | // Initialize predefined 128-bit integer types, if needed. |
| 124 | if (PP.getTargetInfo().getPointerWidth(0) >= 64) { |
| 125 | // If either of the 128-bit integer types are unavailable to name lookup, |
| 126 | // define them now. |
| 127 | DeclarationName Int128 = &Context.Idents.get("__int128_t"); |
| 128 | if (IdentifierResolver::begin(Int128) == IdentifierResolver::end()) |
| 129 | PushOnScopeChains(Context.getInt128Decl(), TUScope); |
| 130 | |
| 131 | DeclarationName UInt128 = &Context.Idents.get("__uint128_t"); |
| 132 | if (IdentifierResolver::begin(UInt128) == IdentifierResolver::end()) |
| 133 | PushOnScopeChains(Context.getUInt128Decl(), TUScope); |
| 134 | } |
| 135 | |
| 136 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 137 | // Initialize predefined Objective-C types: |
| 138 | if (PP.getLangOptions().ObjC1) { |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 139 | // If 'SEL' does not yet refer to any declarations, make it refer to the |
| 140 | // predefined 'SEL'. |
| 141 | DeclarationName SEL = &Context.Idents.get("SEL"); |
| 142 | if (IdentifierResolver::begin(SEL) == IdentifierResolver::end()) |
| 143 | PushOnScopeChains(Context.getObjCSelDecl(), TUScope); |
| 144 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 145 | // If 'id' does not yet refer to any declarations, make it refer to the |
| 146 | // predefined 'id'. |
| 147 | DeclarationName Id = &Context.Idents.get("id"); |
| 148 | if (IdentifierResolver::begin(Id) == IdentifierResolver::end()) |
| 149 | PushOnScopeChains(Context.getObjCIdDecl(), TUScope); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 150 | |
| 151 | // Create the built-in typedef for 'Class'. |
| 152 | DeclarationName Class = &Context.Idents.get("Class"); |
| 153 | if (IdentifierResolver::begin(Class) == IdentifierResolver::end()) |
| 154 | PushOnScopeChains(Context.getObjCClassDecl(), TUScope); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 155 | } |
Steve Naroff | 38d31b4 | 2007-02-28 01:22:02 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 157 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 158 | Sema::~Sema() { |
| 159 | if (PackContext) FreePackedContext(); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 160 | if (VisContext) FreeVisContext(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 161 | delete TheTargetAttributesSema; |
Fariborz Jahanian | 743dda4 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 162 | MSStructPragmaOn = false; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 163 | // Kill all the active scopes. |
| 164 | for (unsigned I = 1, E = FunctionScopes.size(); I != E; ++I) |
| 165 | delete FunctionScopes[I]; |
| 166 | if (FunctionScopes.size() == 1) |
| 167 | delete FunctionScopes[0]; |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 168 | |
| 169 | // Tell the SemaConsumer to forget about us; we're going out of scope. |
| 170 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(&Consumer)) |
| 171 | SC->ForgetSema(); |
| 172 | |
| 173 | // Detach from the external Sema source. |
| 174 | if (ExternalSemaSource *ExternalSema |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 175 | = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource())) |
Douglas Gregor | 5c6f10b | 2010-08-12 22:51:45 +0000 | [diff] [blame] | 176 | ExternalSema->ForgetSema(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 177 | } |
| 178 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 179 | |
| 180 | /// makeUnavailableInSystemHeader - There is an error in the current |
| 181 | /// context. If we're still in a system header, and we can plausibly |
| 182 | /// make the relevant declaration unavailable instead of erroring, do |
| 183 | /// so and return true. |
| 184 | bool Sema::makeUnavailableInSystemHeader(SourceLocation loc, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 185 | StringRef msg) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 186 | // If we're not in a function, it's an error. |
| 187 | FunctionDecl *fn = dyn_cast<FunctionDecl>(CurContext); |
| 188 | if (!fn) return false; |
| 189 | |
| 190 | // If we're in template instantiation, it's an error. |
| 191 | if (!ActiveTemplateInstantiations.empty()) |
| 192 | return false; |
| 193 | |
| 194 | // If that function's not in a system header, it's an error. |
| 195 | if (!Context.getSourceManager().isInSystemHeader(loc)) |
| 196 | return false; |
| 197 | |
| 198 | // If the function is already unavailable, it's not an error. |
| 199 | if (fn->hasAttr<UnavailableAttr>()) return true; |
| 200 | |
| 201 | fn->addAttr(new (Context) UnavailableAttr(loc, Context, msg)); |
| 202 | return true; |
| 203 | } |
| 204 | |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 205 | ASTMutationListener *Sema::getASTMutationListener() const { |
| 206 | return getASTConsumer().GetASTMutationListener(); |
| 207 | } |
| 208 | |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 209 | /// \brief Print out statistics about the semantic analysis. |
| 210 | void Sema::PrintStats() const { |
| 211 | llvm::errs() << "\n*** Semantic Analysis Stats:\n"; |
| 212 | llvm::errs() << NumSFINAEErrors << " SFINAE diagnostics trapped.\n"; |
| 213 | |
| 214 | BumpAlloc.PrintStats(); |
| 215 | AnalysisWarnings.PrintStats(); |
| 216 | } |
| 217 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 219 | /// If there is already an implicit cast, merge into the existing one. |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 220 | /// The result is of the given category. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 221 | ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty, |
| 222 | CastKind Kind, ExprValueKind VK, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 223 | const CXXCastPath *BasePath, |
| 224 | CheckedConversionKind CCK) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 225 | QualType ExprTy = Context.getCanonicalType(E->getType()); |
Mon P Wang | 74b3207 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 226 | QualType TypeTy = Context.getCanonicalType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Mon P Wang | 74b3207 | 2008-09-04 08:38:01 +0000 | [diff] [blame] | 228 | if (ExprTy == TypeTy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 229 | return Owned(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 231 | if (getLangOptions().ObjCAutoRefCount) |
| 232 | CheckObjCARCConversion(SourceRange(), Ty, E, CCK); |
| 233 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 234 | // If this is a derived-to-base cast to a through a virtual base, we |
| 235 | // need a vtable. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 236 | if (Kind == CK_DerivedToBase && |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 237 | BasePathInvolvesVirtualBase(*BasePath)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 238 | QualType T = E->getType(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 239 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 240 | T = Pointer->getPointeeType(); |
| 241 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 242 | MarkVTableUsed(E->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 243 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 244 | } |
| 245 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 246 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 247 | if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) { |
Anders Carlsson | 4e66cda | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 248 | ImpCast->setType(Ty); |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 249 | ImpCast->setValueKind(VK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 250 | return Owned(E); |
Anders Carlsson | 4e66cda | 2009-09-15 05:28:24 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 254 | return Owned(ImplicitCastExpr::Create(Context, Ty, Kind, E, BasePath, VK)); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 257 | /// ScalarTypeToBooleanCastKind - Returns the cast kind corresponding |
| 258 | /// to the conversion from scalar type ScalarTy to the Boolean type. |
| 259 | CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) { |
| 260 | switch (ScalarTy->getScalarTypeKind()) { |
| 261 | case Type::STK_Bool: return CK_NoOp; |
| 262 | case Type::STK_Pointer: return CK_PointerToBoolean; |
| 263 | case Type::STK_MemberPointer: return CK_MemberPointerToBoolean; |
| 264 | case Type::STK_Integral: return CK_IntegralToBoolean; |
| 265 | case Type::STK_Floating: return CK_FloatingToBoolean; |
| 266 | case Type::STK_IntegralComplex: return CK_IntegralComplexToBoolean; |
| 267 | case Type::STK_FloatingComplex: return CK_FloatingComplexToBoolean; |
| 268 | } |
| 269 | return CK_Invalid; |
| 270 | } |
| 271 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 272 | ExprValueKind Sema::CastCategory(Expr *E) { |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 273 | Expr::Classification Classification = E->Classify(Context); |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 274 | return Classification.isRValue() ? VK_RValue : |
| 275 | (Classification.isLValue() ? VK_LValue : VK_XValue); |
Chris Lattner | a65e1f3 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 278 | /// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector. |
| 279 | static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) { |
| 280 | if (D->isUsed()) |
| 281 | return true; |
| 282 | |
| 283 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 284 | // UnusedFileScopedDecls stores the first declaration. |
| 285 | // The declaration may have become definition so check again. |
| 286 | const FunctionDecl *DeclToCheck; |
| 287 | if (FD->hasBody(DeclToCheck)) |
| 288 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 289 | |
| 290 | // Later redecls may add new information resulting in not having to warn, |
| 291 | // so check again. |
| 292 | DeclToCheck = FD->getMostRecentDeclaration(); |
| 293 | if (DeclToCheck != FD) |
| 294 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 295 | } |
| 296 | |
| 297 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 298 | // UnusedFileScopedDecls stores the first declaration. |
| 299 | // The declaration may have become definition so check again. |
| 300 | const VarDecl *DeclToCheck = VD->getDefinition(); |
| 301 | if (DeclToCheck) |
| 302 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 303 | |
| 304 | // Later redecls may add new information resulting in not having to warn, |
| 305 | // so check again. |
| 306 | DeclToCheck = VD->getMostRecentDeclaration(); |
| 307 | if (DeclToCheck != VD) |
| 308 | return !SemaRef->ShouldWarnIfUnusedFileScopedDecl(DeclToCheck); |
| 309 | } |
| 310 | |
| 311 | return false; |
| 312 | } |
| 313 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 314 | namespace { |
| 315 | struct UndefinedInternal { |
| 316 | NamedDecl *decl; |
| 317 | FullSourceLoc useLoc; |
| 318 | |
| 319 | UndefinedInternal(NamedDecl *decl, FullSourceLoc useLoc) |
| 320 | : decl(decl), useLoc(useLoc) {} |
| 321 | }; |
| 322 | |
| 323 | bool operator<(const UndefinedInternal &l, const UndefinedInternal &r) { |
| 324 | return l.useLoc.isBeforeInTranslationUnitThan(r.useLoc); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /// checkUndefinedInternals - Check for undefined objects with internal linkage. |
| 329 | static void checkUndefinedInternals(Sema &S) { |
| 330 | if (S.UndefinedInternals.empty()) return; |
| 331 | |
| 332 | // Collect all the still-undefined entities with internal linkage. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 333 | SmallVector<UndefinedInternal, 16> undefined; |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 334 | for (llvm::DenseMap<NamedDecl*,SourceLocation>::iterator |
| 335 | i = S.UndefinedInternals.begin(), e = S.UndefinedInternals.end(); |
| 336 | i != e; ++i) { |
| 337 | NamedDecl *decl = i->first; |
| 338 | |
| 339 | // Ignore attributes that have become invalid. |
| 340 | if (decl->isInvalidDecl()) continue; |
| 341 | |
| 342 | // __attribute__((weakref)) is basically a definition. |
| 343 | if (decl->hasAttr<WeakRefAttr>()) continue; |
| 344 | |
| 345 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
| 346 | if (fn->isPure() || fn->hasBody()) |
| 347 | continue; |
| 348 | } else { |
| 349 | if (cast<VarDecl>(decl)->hasDefinition() != VarDecl::DeclarationOnly) |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | // We build a FullSourceLoc so that we can sort with array_pod_sort. |
| 354 | FullSourceLoc loc(i->second, S.Context.getSourceManager()); |
| 355 | undefined.push_back(UndefinedInternal(decl, loc)); |
| 356 | } |
| 357 | |
| 358 | if (undefined.empty()) return; |
| 359 | |
| 360 | // Sort (in order of use site) so that we're not (as) dependent on |
| 361 | // the iteration order through an llvm::DenseMap. |
| 362 | llvm::array_pod_sort(undefined.begin(), undefined.end()); |
| 363 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 364 | for (SmallVectorImpl<UndefinedInternal>::iterator |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 365 | i = undefined.begin(), e = undefined.end(); i != e; ++i) { |
| 366 | NamedDecl *decl = i->decl; |
| 367 | S.Diag(decl->getLocation(), diag::warn_undefined_internal) |
| 368 | << isa<VarDecl>(decl) << decl; |
| 369 | S.Diag(i->useLoc, diag::note_used_here); |
| 370 | } |
| 371 | } |
| 372 | |
Douglas Gregor | 1c4bfe5 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 373 | void Sema::LoadExternalWeakUndeclaredIdentifiers() { |
| 374 | if (!ExternalSource) |
| 375 | return; |
| 376 | |
| 377 | SmallVector<std::pair<IdentifierInfo *, WeakInfo>, 4> WeakIDs; |
| 378 | ExternalSource->ReadWeakUndeclaredIdentifiers(WeakIDs); |
| 379 | for (unsigned I = 0, N = WeakIDs.size(); I != N; ++I) { |
| 380 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator Pos |
| 381 | = WeakUndeclaredIdentifiers.find(WeakIDs[I].first); |
| 382 | if (Pos != WeakUndeclaredIdentifiers.end()) |
| 383 | continue; |
| 384 | |
| 385 | WeakUndeclaredIdentifiers.insert(WeakIDs[I]); |
| 386 | } |
| 387 | } |
| 388 | |
Chris Lattner | f440440 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 389 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 390 | /// translation unit when EOF is reached and all but the top-level scope is |
| 391 | /// popped. |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 392 | void Sema::ActOnEndOfTranslationUnit() { |
| 393 | // At PCH writing, implicit instantiations and VTable handling info are |
| 394 | // stored and performed when the PCH is included. |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 395 | if (CompleteTranslationUnit) { |
Chandler Carruth | 88bfa5e | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 396 | // If any dynamic classes have their key function defined within |
| 397 | // this translation unit, then those vtables are considered "used" and must |
| 398 | // be emitted. |
Douglas Gregor | 3200219 | 2011-07-28 00:53:40 +0000 | [diff] [blame] | 399 | for (DynamicClassesType::iterator I = DynamicClasses.begin(ExternalSource), |
| 400 | E = DynamicClasses.end(); |
| 401 | I != E; ++I) { |
| 402 | assert(!(*I)->isDependentType() && |
Anders Carlsson | e771e76 | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 403 | "Should not see dependent types here!"); |
Douglas Gregor | 3200219 | 2011-07-28 00:53:40 +0000 | [diff] [blame] | 404 | if (const CXXMethodDecl *KeyFunction = Context.getKeyFunction(*I)) { |
Chandler Carruth | 88bfa5e | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 405 | const FunctionDecl *Definition = 0; |
| 406 | if (KeyFunction->hasBody(Definition)) |
Douglas Gregor | 3200219 | 2011-07-28 00:53:40 +0000 | [diff] [blame] | 407 | MarkVTableUsed(Definition->getLocation(), *I, true); |
Chandler Carruth | 88bfa5e | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 411 | // If DefinedUsedVTables ends up marking any virtual member functions it |
| 412 | // might lead to more pending template instantiations, which we then need |
| 413 | // to instantiate. |
| 414 | DefineUsedVTables(); |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 415 | |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 416 | // C++: Perform implicit template instantiations. |
| 417 | // |
| 418 | // FIXME: When we perform these implicit instantiations, we do not |
| 419 | // carefully keep track of the point of instantiation (C++ [temp.point]). |
| 420 | // This means that name lookup that occurs within the template |
| 421 | // instantiation will always happen at the end of the translation unit, |
| 422 | // so it will find some names that should not be found. Although this is |
| 423 | // common behavior for C++ compilers, it is technically wrong. In the |
| 424 | // future, we either need to be able to filter the results of name lookup |
| 425 | // or we need to perform template instantiations earlier. |
| 426 | PerformPendingInstantiations(); |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 427 | } |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 428 | |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 429 | // Remove file scoped decls that turned out to be used. |
Douglas Gregor | a94a154 | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 430 | UnusedFileScopedDecls.erase(std::remove_if(UnusedFileScopedDecls.begin(0, |
| 431 | true), |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 432 | UnusedFileScopedDecls.end(), |
| 433 | std::bind1st(std::ptr_fun(ShouldRemoveFromUnused), |
| 434 | this)), |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 435 | UnusedFileScopedDecls.end()); |
Douglas Gregor | fb8b27d | 2010-04-09 17:41:13 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 437 | if (!CompleteTranslationUnit) { |
| 438 | TUScope = 0; |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 439 | return; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 440 | } |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 442 | // Check for #pragma weak identifiers that were never declared |
| 443 | // FIXME: This will cause diagnostics to be emitted in a non-determinstic |
| 444 | // order! Iterating over a densemap like this is bad. |
Douglas Gregor | 1c4bfe5 | 2011-07-28 18:09:57 +0000 | [diff] [blame] | 445 | LoadExternalWeakUndeclaredIdentifiers(); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 446 | for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 447 | I = WeakUndeclaredIdentifiers.begin(), |
| 448 | E = WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 449 | if (I->second.getUsed()) continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 451 | Diag(I->second.getLocation(), diag::warn_weak_identifier_undeclared) |
| 452 | << I->first; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 455 | // C99 6.9.2p2: |
| 456 | // A declaration of an identifier for an object that has file |
| 457 | // scope without an initializer, and without a storage-class |
| 458 | // specifier or with the storage-class specifier static, |
| 459 | // constitutes a tentative definition. If a translation unit |
| 460 | // contains one or more tentative definitions for an identifier, |
| 461 | // and the translation unit contains no external definition for |
| 462 | // that identifier, then the behavior is exactly as if the |
| 463 | // translation unit contains a file scope declaration of that |
| 464 | // identifier, with the composite type as of the end of the |
| 465 | // translation unit, with an initializer equal to 0. |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 466 | llvm::SmallSet<VarDecl *, 32> Seen; |
Douglas Gregor | eb08bd4 | 2011-07-27 20:58:46 +0000 | [diff] [blame] | 467 | for (TentativeDefinitionsType::iterator |
| 468 | T = TentativeDefinitions.begin(ExternalSource), |
| 469 | TEnd = TentativeDefinitions.end(); |
| 470 | T != TEnd; ++T) |
| 471 | { |
| 472 | VarDecl *VD = (*T)->getActingDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 474 | // If the tentative definition was completed, getActingDefinition() returns |
| 475 | // null. If we've already seen this variable before, insert()'s second |
| 476 | // return value is false. |
| 477 | if (VD == 0 || VD->isInvalidDecl() || !Seen.insert(VD)) |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 478 | continue; |
| 479 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | if (const IncompleteArrayType *ArrayT |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 481 | = Context.getAsIncompleteArrayType(VD->getType())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | if (RequireCompleteType(VD->getLocation(), |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 483 | ArrayT->getElementType(), |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 484 | diag::err_tentative_def_incomplete_type_arr)) { |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 485 | VD->setInvalidDecl(); |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 486 | continue; |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 487 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 489 | // Set the length of the array to 1 (C99 6.9.2p5). |
| 490 | Diag(VD->getLocation(), diag::warn_tentative_incomplete_array); |
| 491 | llvm::APInt One(Context.getTypeSize(Context.getSizeType()), true); |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 492 | QualType T = Context.getConstantArrayType(ArrayT->getElementType(), |
| 493 | One, ArrayType::Normal, 0); |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 494 | VD->setType(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | } else if (RequireCompleteType(VD->getLocation(), VD->getType(), |
Douglas Gregor | beecd58 | 2009-04-21 17:11:58 +0000 | [diff] [blame] | 496 | diag::err_tentative_def_incomplete_type)) |
| 497 | VD->setInvalidDecl(); |
| 498 | |
| 499 | // Notify the consumer that we've completed a tentative definition. |
| 500 | if (!VD->isInvalidDecl()) |
| 501 | Consumer.CompleteTentativeDefinition(VD); |
| 502 | |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 503 | } |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 504 | |
Alexis Hunt | e262299 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 505 | if (LangOpts.CPlusPlus0x && |
| 506 | Diags.getDiagnosticLevel(diag::warn_delegating_ctor_cycle, |
| 507 | SourceLocation()) |
| 508 | != Diagnostic::Ignored) |
Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 509 | CheckDelegatingCtorCycles(); |
| 510 | |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 511 | // If there were errors, disable 'unused' warnings since they will mostly be |
| 512 | // noise. |
| 513 | if (!Diags.hasErrorOccurred()) { |
| 514 | // Output warning for unused file scoped decls. |
Douglas Gregor | a94a154 | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 515 | for (UnusedFileScopedDeclsType::iterator |
| 516 | I = UnusedFileScopedDecls.begin(ExternalSource), |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 517 | E = UnusedFileScopedDecls.end(); I != E; ++I) { |
Douglas Gregor | a94a154 | 2011-07-27 21:45:57 +0000 | [diff] [blame] | 518 | if (ShouldRemoveFromUnused(this, *I)) |
| 519 | continue; |
| 520 | |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 521 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 522 | const FunctionDecl *DiagD; |
| 523 | if (!FD->hasBody(DiagD)) |
| 524 | DiagD = FD; |
Argyrios Kyrtzidis | e028314 | 2011-03-03 17:47:42 +0000 | [diff] [blame] | 525 | if (DiagD->isDeleted()) |
| 526 | continue; // Deleted functions are supposed to be unused. |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 527 | if (DiagD->isReferenced()) { |
| 528 | if (isa<CXXMethodDecl>(DiagD)) |
| 529 | Diag(DiagD->getLocation(), diag::warn_unneeded_member_function) |
| 530 | << DiagD->getDeclName(); |
| 531 | else |
| 532 | Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) |
| 533 | << /*function*/0 << DiagD->getDeclName(); |
| 534 | } else { |
| 535 | Diag(DiagD->getLocation(), |
| 536 | isa<CXXMethodDecl>(DiagD) ? diag::warn_unused_member_function |
| 537 | : diag::warn_unused_function) |
| 538 | << DiagD->getDeclName(); |
| 539 | } |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 540 | } else { |
| 541 | const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition(); |
| 542 | if (!DiagD) |
| 543 | DiagD = cast<VarDecl>(*I); |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 544 | if (DiagD->isReferenced()) { |
| 545 | Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl) |
| 546 | << /*variable*/1 << DiagD->getDeclName(); |
| 547 | } else { |
| 548 | Diag(DiagD->getLocation(), diag::warn_unused_variable) |
| 549 | << DiagD->getDeclName(); |
| 550 | } |
Argyrios Kyrtzidis | 77fd99f | 2011-01-31 07:04:37 +0000 | [diff] [blame] | 551 | } |
Argyrios Kyrtzidis | 1b30d9c | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 552 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 553 | |
| 554 | checkUndefinedInternals(*this); |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 555 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 556 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 557 | // Check we've noticed that we're no longer parsing the initializer for every |
| 558 | // variable. If we miss cases, then at best we have a performance issue and |
| 559 | // at worst a rejects-valid bug. |
| 560 | assert(ParsingInitForAutoVars.empty() && |
| 561 | "Didn't unmark var as having its initializer parsed"); |
| 562 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 563 | TUScope = 0; |
Chris Lattner | f440440 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 567 | //===----------------------------------------------------------------------===// |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 568 | // Helper functions. |
| 569 | //===----------------------------------------------------------------------===// |
| 570 | |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 571 | DeclContext *Sema::getFunctionLevelDeclContext() { |
John McCall | b878801 | 2009-12-19 10:53:49 +0000 | [diff] [blame] | 572 | DeclContext *DC = CurContext; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | 0c6f539 | 2010-05-22 16:25:05 +0000 | [diff] [blame] | 574 | while (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 575 | DC = DC->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 577 | return DC; |
| 578 | } |
| 579 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 580 | /// getCurFunctionDecl - If inside of a function body, this returns a pointer |
| 581 | /// to the function decl for the function being parsed. If we're currently |
| 582 | /// in a 'block', this returns the containing context. |
| 583 | FunctionDecl *Sema::getCurFunctionDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 584 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 585 | return dyn_cast<FunctionDecl>(DC); |
| 586 | } |
| 587 | |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 588 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 589 | DeclContext *DC = getFunctionLevelDeclContext(); |
Steve Naroff | ecf2bb8 | 2008-11-17 16:28:52 +0000 | [diff] [blame] | 590 | return dyn_cast<ObjCMethodDecl>(DC); |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 591 | } |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 592 | |
| 593 | NamedDecl *Sema::getCurFunctionOrMethodDecl() { |
Anders Carlsson | b26ab81 | 2009-08-08 17:45:02 +0000 | [diff] [blame] | 594 | DeclContext *DC = getFunctionLevelDeclContext(); |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 595 | if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC)) |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 596 | return cast<NamedDecl>(DC); |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 600 | Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() { |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 601 | if (!isActive()) |
| 602 | return; |
| 603 | |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 604 | if (llvm::Optional<TemplateDeductionInfo*> Info = SemaRef.isSFINAEContext()) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 605 | switch (DiagnosticIDs::getDiagnosticSFINAEResponse(getDiagID())) { |
| 606 | case DiagnosticIDs::SFINAE_Report: |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 607 | // Fall through; we'll report the diagnostic below. |
| 608 | break; |
| 609 | |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 610 | case DiagnosticIDs::SFINAE_AccessControl: |
Douglas Gregor | 347d626 | 2011-05-11 23:45:11 +0000 | [diff] [blame] | 611 | // Per C++ Core Issue 1170, access control is part of SFINAE. |
| 612 | // Additionally, the AccessCheckingSFINAE flag can be used to temporary |
| 613 | // make access control a part of SFINAE for the purposes of checking |
| 614 | // type traits. |
| 615 | if (!SemaRef.AccessCheckingSFINAE && |
| 616 | !SemaRef.getLangOptions().CPlusPlus0x) |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 617 | break; |
| 618 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 619 | case DiagnosticIDs::SFINAE_SubstitutionFailure: |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 620 | // Count this failure so that we know that template argument deduction |
| 621 | // has failed. |
| 622 | ++SemaRef.NumSFINAEErrors; |
| 623 | SemaRef.Diags.setLastDiagnosticIgnored(); |
| 624 | SemaRef.Diags.Clear(); |
| 625 | Clear(); |
| 626 | return; |
| 627 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 628 | case DiagnosticIDs::SFINAE_Suppress: |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 629 | // Make a copy of this suppressed diagnostic and store it with the |
| 630 | // template-deduction information; |
Douglas Gregor | b392159 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 631 | FlushCounts(); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 632 | DiagnosticInfo DiagInfo(&SemaRef.Diags); |
Douglas Gregor | b392159 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 633 | |
Douglas Gregor | edb7685 | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 634 | if (*Info) |
| 635 | (*Info)->addSuppressedDiagnostic(DiagInfo.getLocation(), |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 636 | PartialDiagnostic(DiagInfo, |
| 637 | SemaRef.Context.getDiagAllocator())); |
| 638 | |
| 639 | // Suppress this diagnostic. |
| 640 | SemaRef.Diags.setLastDiagnosticIgnored(); |
| 641 | SemaRef.Diags.Clear(); |
| 642 | Clear(); |
| 643 | return; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // Emit the diagnostic. |
Douglas Gregor | 3383451 | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 648 | if (!this->Emit()) |
| 649 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 651 | // If this is not a note, and we're in a template instantiation |
| 652 | // that is different from the last template instantiation where |
| 653 | // we emitted an error, print a template instantiation |
| 654 | // backtrace. |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 655 | if (!DiagnosticIDs::isBuiltinNote(DiagID) && |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 656 | !SemaRef.ActiveTemplateInstantiations.empty() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | SemaRef.ActiveTemplateInstantiations.back() |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 658 | != SemaRef.LastTemplateInstantiationErrorContext) { |
| 659 | SemaRef.PrintInstantiationStack(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | da17bd3 | 2009-03-20 22:48:49 +0000 | [diff] [blame] | 661 | = SemaRef.ActiveTemplateInstantiations.back(); |
| 662 | } |
| 663 | } |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 664 | |
Douglas Gregor | 210b590 | 2010-03-25 22:17:48 +0000 | [diff] [blame] | 665 | Sema::SemaDiagnosticBuilder Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 666 | DiagnosticBuilder DB = Diags.Report(Loc, DiagID); |
Douglas Gregor | 210b590 | 2010-03-25 22:17:48 +0000 | [diff] [blame] | 667 | return SemaDiagnosticBuilder(DB, *this, DiagID); |
| 668 | } |
| 669 | |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 670 | Sema::SemaDiagnosticBuilder |
| 671 | Sema::Diag(SourceLocation Loc, const PartialDiagnostic& PD) { |
| 672 | SemaDiagnosticBuilder Builder(Diag(Loc, PD.getDiagID())); |
| 673 | PD.Emit(Builder); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | |
Anders Carlsson | f68079e | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 675 | return Builder; |
| 676 | } |
| 677 | |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 678 | /// \brief Looks through the macro-expansion chain for the given |
| 679 | /// location, looking for a macro expansion with the given name. |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 680 | /// If one is found, returns true and sets the location to that |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 681 | /// expansion loc. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 682 | bool Sema::findMacroSpelling(SourceLocation &locref, StringRef name) { |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 683 | SourceLocation loc = locref; |
| 684 | if (!loc.isMacroID()) return false; |
| 685 | |
| 686 | // There's no good way right now to look at the intermediate |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 687 | // expansions, so just jump to the expansion location. |
Chandler Carruth | 35f5320 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 688 | loc = getSourceManager().getExpansionLoc(loc); |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 689 | |
| 690 | // If that's written with the name, stop here. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 691 | SmallVector<char, 16> buffer; |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 692 | if (getPreprocessor().getSpelling(loc, buffer) == name) { |
| 693 | locref = loc; |
| 694 | return true; |
| 695 | } |
| 696 | return false; |
| 697 | } |
| 698 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 699 | /// \brief Determines the active Scope associated with the given declaration |
| 700 | /// context. |
| 701 | /// |
| 702 | /// This routine maps a declaration context to the active Scope object that |
| 703 | /// represents that declaration context in the parser. It is typically used |
| 704 | /// from "scope-less" code (e.g., template instantiation, lazy creation of |
| 705 | /// declarations) that injects a name for name-lookup purposes and, therefore, |
| 706 | /// must update the Scope. |
| 707 | /// |
| 708 | /// \returns The scope corresponding to the given declaraion context, or NULL |
| 709 | /// if no such scope is open. |
| 710 | Scope *Sema::getScopeForContext(DeclContext *Ctx) { |
| 711 | |
| 712 | if (!Ctx) |
| 713 | return 0; |
| 714 | |
| 715 | Ctx = Ctx->getPrimaryContext(); |
| 716 | for (Scope *S = getCurScope(); S; S = S->getParent()) { |
Sebastian Redl | caef9ab | 2010-07-08 23:07:34 +0000 | [diff] [blame] | 717 | // Ignore scopes that cannot have declarations. This is important for |
| 718 | // out-of-line definitions of static class members. |
| 719 | if (S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) |
| 720 | if (DeclContext *Entity = static_cast<DeclContext *> (S->getEntity())) |
| 721 | if (Ctx == Entity->getPrimaryContext()) |
| 722 | return S; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | return 0; |
| 726 | } |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 727 | |
| 728 | /// \brief Enter a new function scope |
| 729 | void Sema::PushFunctionScope() { |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 730 | if (FunctionScopes.size() == 1) { |
| 731 | // Use the "top" function scope rather than having to allocate |
| 732 | // memory for a new scope. |
Argyrios Kyrtzidis | f51ec1d | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 733 | FunctionScopes.back()->Clear(); |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 734 | FunctionScopes.push_back(FunctionScopes.back()); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 735 | return; |
| 736 | } |
| 737 | |
Argyrios Kyrtzidis | f51ec1d | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 738 | FunctionScopes.push_back(new FunctionScopeInfo(getDiagnostics())); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) { |
Argyrios Kyrtzidis | f51ec1d | 2010-11-19 00:19:15 +0000 | [diff] [blame] | 742 | FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(), |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 743 | BlockScope, Block)); |
| 744 | } |
| 745 | |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 746 | void Sema::PopFunctionOrBlockScope(const AnalysisBasedWarnings::Policy *WP, |
| 747 | const Decl *D, const BlockExpr *blkExpr) { |
| 748 | FunctionScopeInfo *Scope = FunctionScopes.pop_back_val(); |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 749 | assert(!FunctionScopes.empty() && "mismatched push/pop!"); |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 750 | |
| 751 | // Issue any analysis-based warnings. |
| 752 | if (WP && D) |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 753 | AnalysisWarnings.IssueWarnings(*WP, Scope, D, blkExpr); |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 754 | else { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 755 | for (SmallVectorImpl<sema::PossiblyUnreachableDiag>::iterator |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 756 | i = Scope->PossiblyUnreachableDiags.begin(), |
| 757 | e = Scope->PossiblyUnreachableDiags.end(); |
| 758 | i != e; ++i) { |
| 759 | const sema::PossiblyUnreachableDiag &D = *i; |
| 760 | Diag(D.Loc, D.PD); |
| 761 | } |
| 762 | } |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 763 | |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 764 | if (FunctionScopes.back() != Scope) { |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 765 | delete Scope; |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 766 | } |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /// \brief Determine whether any errors occurred within this function/method/ |
| 770 | /// block. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 771 | bool Sema::hasAnyUnrecoverableErrorsInThisFunction() const { |
| 772 | return getCurFunction()->ErrorTrap.hasUnrecoverableErrorOccurred(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | BlockScopeInfo *Sema::getCurBlock() { |
| 776 | if (FunctionScopes.empty()) |
| 777 | return 0; |
| 778 | |
| 779 | return dyn_cast<BlockScopeInfo>(FunctionScopes.back()); |
| 780 | } |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 781 | |
| 782 | // Pin this vtable to this file. |
| 783 | ExternalSemaSource::~ExternalSemaSource() {} |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 784 | |
Sebastian Redl | c1ca90a | 2010-09-28 20:23:00 +0000 | [diff] [blame] | 785 | std::pair<ObjCMethodList, ObjCMethodList> |
| 786 | ExternalSemaSource::ReadMethodPool(Selector Sel) { |
| 787 | return std::pair<ObjCMethodList, ObjCMethodList>(); |
| 788 | } |
| 789 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 790 | void ExternalSemaSource::ReadKnownNamespaces( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 791 | SmallVectorImpl<NamespaceDecl *> &Namespaces) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 794 | void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 795 | SourceLocation Loc = this->Loc; |
| 796 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
| 797 | if (Loc.isValid()) { |
| 798 | Loc.print(OS, S.getSourceManager()); |
| 799 | OS << ": "; |
| 800 | } |
| 801 | OS << Message; |
| 802 | |
| 803 | if (TheDecl && isa<NamedDecl>(TheDecl)) { |
| 804 | std::string Name = cast<NamedDecl>(TheDecl)->getNameAsString(); |
| 805 | if (!Name.empty()) |
| 806 | OS << " '" << Name << '\''; |
| 807 | } |
| 808 | |
| 809 | OS << '\n'; |
| 810 | } |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 811 | |
| 812 | /// \brief Figure out if an expression could be turned into a call. |
| 813 | /// |
| 814 | /// Use this when trying to recover from an error where the programmer may have |
| 815 | /// written just the name of a function instead of actually calling it. |
| 816 | /// |
| 817 | /// \param E - The expression to examine. |
| 818 | /// \param ZeroArgCallReturnTy - If the expression can be turned into a call |
| 819 | /// with no arguments, this parameter is set to the type returned by such a |
| 820 | /// call; otherwise, it is set to an empty QualType. |
| 821 | /// \param NonTemplateOverloads - If the expression is an overloaded function |
| 822 | /// name, this parameter is populated with the decls of the various overloads. |
| 823 | bool Sema::isExprCallable(const Expr &E, QualType &ZeroArgCallReturnTy, |
| 824 | UnresolvedSetImpl &NonTemplateOverloads) { |
| 825 | ZeroArgCallReturnTy = QualType(); |
| 826 | NonTemplateOverloads.clear(); |
| 827 | if (const OverloadExpr *Overloads = dyn_cast<OverloadExpr>(&E)) { |
| 828 | for (OverloadExpr::decls_iterator it = Overloads->decls_begin(), |
| 829 | DeclsEnd = Overloads->decls_end(); it != DeclsEnd; ++it) { |
| 830 | // Our overload set may include TemplateDecls, which we'll ignore for our |
| 831 | // present purpose. |
| 832 | if (const FunctionDecl *OverloadDecl = dyn_cast<FunctionDecl>(*it)) { |
| 833 | NonTemplateOverloads.addDecl(*it); |
| 834 | if (OverloadDecl->getMinRequiredArguments() == 0) |
| 835 | ZeroArgCallReturnTy = OverloadDecl->getResultType(); |
| 836 | } |
| 837 | } |
| 838 | return true; |
| 839 | } |
| 840 | |
| 841 | if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(&E)) { |
| 842 | if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { |
| 843 | if (Fun->getMinRequiredArguments() == 0) |
| 844 | ZeroArgCallReturnTy = Fun->getResultType(); |
| 845 | return true; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | // We don't have an expression that's convenient to get a FunctionDecl from, |
| 850 | // but we can at least check if the type is "function of 0 arguments". |
| 851 | QualType ExprTy = E.getType(); |
| 852 | const FunctionType *FunTy = NULL; |
Matt Beaumont-Gay | 330a5b4 | 2011-05-05 00:59:35 +0000 | [diff] [blame] | 853 | QualType PointeeTy = ExprTy->getPointeeType(); |
| 854 | if (!PointeeTy.isNull()) |
| 855 | FunTy = PointeeTy->getAs<FunctionType>(); |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 856 | if (!FunTy) |
| 857 | FunTy = ExprTy->getAs<FunctionType>(); |
| 858 | if (!FunTy && ExprTy == Context.BoundMemberTy) { |
| 859 | // Look for the bound-member type. If it's still overloaded, give up, |
| 860 | // although we probably should have fallen into the OverloadExpr case above |
| 861 | // if we actually have an overloaded bound member. |
| 862 | QualType BoundMemberTy = Expr::findBoundMemberType(&E); |
| 863 | if (!BoundMemberTy.isNull()) |
| 864 | FunTy = BoundMemberTy->castAs<FunctionType>(); |
| 865 | } |
| 866 | |
| 867 | if (const FunctionProtoType *FPT = |
| 868 | dyn_cast_or_null<FunctionProtoType>(FunTy)) { |
| 869 | if (FPT->getNumArgs() == 0) |
| 870 | ZeroArgCallReturnTy = FunTy->getResultType(); |
| 871 | return true; |
| 872 | } |
| 873 | return false; |
| 874 | } |
| 875 | |
| 876 | /// \brief Give notes for a set of overloads. |
| 877 | /// |
| 878 | /// A companion to isExprCallable. In cases when the name that the programmer |
| 879 | /// wrote was an overloaded function, we may be able to make some guesses about |
| 880 | /// plausible overloads based on their return types; such guesses can be handed |
| 881 | /// off to this method to be emitted as notes. |
| 882 | /// |
| 883 | /// \param Overloads - The overloads to note. |
| 884 | /// \param FinalNoteLoc - If we've suppressed printing some overloads due to |
| 885 | /// -fshow-overloads=best, this is the location to attach to the note about too |
| 886 | /// many candidates. Typically this will be the location of the original |
| 887 | /// ill-formed expression. |
| 888 | void Sema::NoteOverloads(const UnresolvedSetImpl &Overloads, |
| 889 | const SourceLocation FinalNoteLoc) { |
| 890 | int ShownOverloads = 0; |
| 891 | int SuppressedOverloads = 0; |
| 892 | for (UnresolvedSetImpl::iterator It = Overloads.begin(), |
| 893 | DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { |
| 894 | // FIXME: Magic number for max shown overloads stolen from |
| 895 | // OverloadCandidateSet::NoteCandidates. |
| 896 | if (ShownOverloads >= 4 && |
| 897 | Diags.getShowOverloads() == Diagnostic::Ovl_Best) { |
| 898 | ++SuppressedOverloads; |
| 899 | continue; |
| 900 | } |
| 901 | Diag(cast<FunctionDecl>(*It)->getSourceRange().getBegin(), |
| 902 | diag::note_member_ref_possible_intended_overload); |
| 903 | ++ShownOverloads; |
| 904 | } |
| 905 | if (SuppressedOverloads) |
| 906 | Diag(FinalNoteLoc, diag::note_ovl_too_many_candidates) |
| 907 | << SuppressedOverloads; |
| 908 | } |