15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements semantic analysis for statements. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 142d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h" 155f1e0942a32657b625702aa52f82430d0120f424John McCall#include "clang/Sema/Scope.h" 16781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/Sema/ScopeInfo.h" 17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h" 18ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith#include "clang/Sema/Lookup.h" 19f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner#include "clang/AST/ASTContext.h" 201cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall#include "clang/AST/CharUnits.h" 21c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 22694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu#include "clang/AST/EvaluatedExprVisitor.h" 2384fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/AST/ExprCXX.h" 24419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner#include "clang/AST/ExprObjC.h" 2516f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h" 2616f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtCXX.h" 27209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall#include "clang/AST/TypeLoc.h" 2884fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor#include "clang/Lex/Preprocessor.h" 296fa9086043b0338d895a4cdb0ec8542530af90d7Anders Carlsson#include "clang/Basic/TargetInfo.h" 30ca57b4b7658a031b74cda5ac504311998be8e343Chris Lattner#include "llvm/ADT/ArrayRef.h" 31c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/STLExtras.h" 32694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu#include "llvm/ADT/SmallPtrSet.h" 3350de5e3247a20e0e548dc47022a011250e6e4e8fDouglas Gregor#include "llvm/ADT/SmallString.h" 34c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl#include "llvm/ADT/SmallVector.h" 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 36781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema; 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Sema::ActOnExprStmt(FullExprArg expr) { 399ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *E = expr.get(); 40bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor if (!E) // FIXME: FullExprArg has no error state? 41bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor return StmtError(); 42bebbe0d9b7568ce43a464286bee49429489ef483Douglas Gregor 43834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // C99 6.8.3p2: The expression in an expression statement is evaluated as a 44834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // void expression for its side effects. Conversion to void allows any 45834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // operand, even incomplete types. 46a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 47834a72ac74cf4ff07ba6215545dba3db578f8a07Chris Lattner // Same thing in for stmt first clause (when expr) and third clause. 48a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl return Owned(static_cast<Stmt*>(E)); 495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 52b7d98d35ea723624345f06e5895ddce2e0388ef0Argyrios KyrtzidisStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc, 53e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis bool HasLeadingEmptyMacro) { 54e2ca828119b8bff4a5c25c6db8ee4fec558451e7Argyrios Kyrtzidis return Owned(new (Context) NullStmt(SemiLoc, HasLeadingEmptyMacro)); 555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 57337e550218128e7d922c09bb354fbc71de90c568Chris LattnerStmtResult Sema::ActOnDeclStmt(DeclGroupPtrTy dg, SourceLocation StartLoc, 58337e550218128e7d922c09bb354fbc71de90c568Chris Lattner SourceLocation EndLoc) { 5920401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 61682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner // If we have an invalid decl, just return an error. 6220401698e3bd93a24bb5d9e18e435895cefe5fd1Chris Lattner if (DG.isNull()) return StmtError(); 631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc)); 655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 67a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanianvoid Sema::ActOnForEachDeclStmt(DeclGroupPtrTy dg) { 68a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian DeclGroupRef DG = dg.getAsVal<DeclGroupRef>(); 69dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 70a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian // If we have an invalid decl, just return. 71a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian if (DG.isNull() || !DG.isSingleDecl()) return; 72f85e193739c953358c865005855253af4f68a497John McCall VarDecl *var = cast<VarDecl>(DG.getSingleDecl()); 73f85e193739c953358c865005855253af4f68a497John McCall 74a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian // suppress any potential 'unused variable' warning. 75f85e193739c953358c865005855253af4f68a497John McCall var->setUsed(); 76f85e193739c953358c865005855253af4f68a497John McCall 777acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // foreach variables are never actually initialized in the way that 787acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // the parser came up with. 797acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setInit(0); 807acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 817acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // In ARC, we don't need to retain the iteration variable of a fast 827acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // enumeration loop. Rather than actually trying to catch that 837acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // during declaration processing, we remove the consequences here. 844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 857acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall QualType type = var->getType(); 867acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 877acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // Only do this if we inferred the lifetime. Inferred lifetime 887acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // will show up as a local qualifier because explicit lifetime 897acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // should have shown up as an AttributedType instead. 907acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (type.getLocalQualifiers().getObjCLifetime() == Qualifiers::OCL_Strong) { 917acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall // Add 'const' and mark the variable as pseudo-strong. 927acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setType(type.withConst()); 937acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall var->setARCPseudoStrong(true); 94f85e193739c953358c865005855253af4f68a497John McCall } 95f85e193739c953358c865005855253af4f68a497John McCall } 96a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian} 97a7cf23a72b0846fc5aacf3f38bb8c8f9e76784cfFariborz Jahanian 98ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth/// \brief Diagnose unused '==' and '!=' as likely typos for '=' or '|='. 999d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// 1009d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// Adding a cast to void (or other expression wrappers) will prevent the 1019d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth/// warning from firing. 102ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruthstatic bool DiagnoseUnusedComparison(Sema &S, const Expr *E) { 1039d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth SourceLocation Loc; 10450bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth bool IsNotEqual, CanAssign; 1059d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1069d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { 1079d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Op->getOpcode() != BO_EQ && Op->getOpcode() != BO_NE) 108ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1099d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1109d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Loc = Op->getOperatorLoc(); 11150bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth IsNotEqual = Op->getOpcode() == BO_NE; 11250bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth CanAssign = Op->getLHS()->IgnoreParenImpCasts()->isLValue(); 1139d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } else if (const CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { 1149d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Op->getOperator() != OO_EqualEqual && 1159d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Op->getOperator() != OO_ExclaimEqual) 116ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1179d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1189d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth Loc = Op->getOperatorLoc(); 11950bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth IsNotEqual = Op->getOperator() == OO_ExclaimEqual; 12050bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth CanAssign = Op->getArg(0)->IgnoreParenImpCasts()->isLValue(); 1219d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } else { 1229d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // Not a typo-prone comparison. 123ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1249d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth } 1259d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 1269d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // Suppress warnings when the operator, suspicious as it may be, comes from 1279d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth // a macro expansion. 1289d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth if (Loc.isMacroID()) 129ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return false; 1309d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 131ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth S.Diag(Loc, diag::warn_unused_comparison) 1329d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth << (unsigned)IsNotEqual << E->getSourceRange(); 1339d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 13450bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth // If the LHS is a plausible entity to assign to, provide a fixit hint to 13550bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth // correct common typos. 13650bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth if (CanAssign) { 13750bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth if (IsNotEqual) 13850bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth S.Diag(Loc, diag::note_inequality_comparison_to_or_assign) 13950bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth << FixItHint::CreateReplacement(Loc, "|="); 14050bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth else 14150bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth S.Diag(Loc, diag::note_equality_comparison_to_assign) 14250bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth << FixItHint::CreateReplacement(Loc, "="); 14350bf68fc9698742e36c311fc37e6e4b7de235c4bChandler Carruth } 144ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth 145ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return true; 1469d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth} 1479d8eb3b2a892697aed332f6c318a8554fc2623ceChandler Carruth 148636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlssonvoid Sema::DiagnoseUnusedExprResult(const Stmt *S) { 149d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) 150d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis return DiagnoseUnusedExprResult(Label->getSubStmt()); 151d2827af6f96d441d72315dbe6d8505c3be0f2aa6Argyrios Kyrtzidis 152754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson const Expr *E = dyn_cast_or_null<Expr>(S); 153636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson if (!E) 154636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson return; 155636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson 156a6115068cde719142eb394db88612c185cabd05bEli Friedman const Expr *WarnExpr; 157636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson SourceLocation Loc; 158636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson SourceRange R1, R2; 159d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (SourceMgr.isInSystemMacro(E->getExprLoc()) || 160a6115068cde719142eb394db88612c185cabd05bEli Friedman !E->isUnusedResultAWarning(WarnExpr, Loc, R1, R2, Context)) 161636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson return; 1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16306b3a06007e02aebe497f34651a4e50b00adb051Chris Lattner // If this is a GNU statement expression expanded from a macro, it is probably 16406b3a06007e02aebe497f34651a4e50b00adb051Chris Lattner // unused because it is a function-like macro that can be used as either an 16506b3a06007e02aebe497f34651a4e50b00adb051Chris Lattner // expression or statement. Don't warn, because it is almost certainly a 16606b3a06007e02aebe497f34651a4e50b00adb051Chris Lattner // false positive. 16706b3a06007e02aebe497f34651a4e50b00adb051Chris Lattner if (isa<StmtExpr>(E) && Loc.isMacroID()) 16806b3a06007e02aebe497f34651a4e50b00adb051Chris Lattner return; 16906b3a06007e02aebe497f34651a4e50b00adb051Chris Lattner 170419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // Okay, we have an unused result. Depending on what the base expression is, 171419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // we might want to make a more specific diagnostic. Check for one of these 172419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner // cases now. 173419cfb318cd69b6c717019288d5a3822be18d8f9Chris Lattner unsigned DiagID = diag::warn_unused_expr; 1744765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall if (const ExprWithCleanups *Temps = dyn_cast<ExprWithCleanups>(E)) 1754dffad64c5c7106dc5ac506be94944299c8f7bc3Douglas Gregor E = Temps->getSubExpr(); 17634d49471e0b6386aefdc0f6bd15e4a4876ce5db1Chandler Carruth if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E)) 17734d49471e0b6386aefdc0f6bd15e4a4876ce5db1Chandler Carruth E = TempExpr->getSubExpr(); 17812f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall 179ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth if (DiagnoseUnusedComparison(*this, E)) 180ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth return; 181ec8058f64bbcd79bd47748f4cf8628123dd3bae6Chandler Carruth 182a6115068cde719142eb394db88612c185cabd05bEli Friedman E = WarnExpr; 183bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 1840faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall if (E->getType()->isVoidType()) 1850faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall return; 1860faede6f31b07bcec7b776f2b420c3ea9bb3e58cJohn McCall 187bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner // If the callee has attribute pure, const, or warn_unused_result, warn with 188bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner // a more specific message to make it clear what is happening. 189d20254f2875d0004c57ee766f258dbcee29f4841Nuno Lopes if (const Decl *FD = CE->getCalleeDecl()) { 190bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<WarnUnusedResultAttr>()) { 19142d7b2d25b6f90dc397886d05502b10ab5a8b51bMatt Beaumont-Gay Diag(Loc, diag::warn_unused_result) << R1 << R2; 192bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 193bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 194bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<PureAttr>()) { 195bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; 196bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 197bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 198bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner if (FD->getAttr<ConstAttr>()) { 199bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; 200bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner return; 201bc8d42c6f1565c0b2f93ad524edebfd7a4e6cac6Chris Lattner } 202dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 20312f78a6741a4cb3d904340f8d3d2714568b50e7aJohn McCall } else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { 2044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) { 205f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::err_arc_unused_init_message) << R1; 206f85e193739c953358c865005855253af4f68a497John McCall return; 207f85e193739c953358c865005855253af4f68a497John McCall } 208f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian const ObjCMethodDecl *MD = ME->getMethodDecl(); 209f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian if (MD && MD->getAttr<WarnUnusedResultAttr>()) { 21042d7b2d25b6f90dc397886d05502b10ab5a8b51bMatt Beaumont-Gay Diag(Loc, diag::warn_unused_result) << R1 << R2; 211f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian return; 212f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian } 213ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { 214ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Source = POE->getSyntacticForm(); 215ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (isa<ObjCSubscriptRefExpr>(Source)) 216ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek DiagID = diag::warn_unused_container_subscript_expr; 217ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek else 218ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek DiagID = diag::warn_unused_property_expr; 219d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor } else if (const CXXFunctionalCastExpr *FC 220d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor = dyn_cast<CXXFunctionalCastExpr>(E)) { 221d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor if (isa<CXXConstructExpr>(FC->getSubExpr()) || 222d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor isa<CXXTemporaryObjectExpr>(FC->getSubExpr())) 223d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor return; 224f031774aa2638b4d3f487e7e44180c1f89b867efFariborz Jahanian } 225209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall // Diagnose "(void*) blah" as a typo for "(void) blah". 226209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { 227209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); 228209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall QualType T = TI->getType(); 229209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 230209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall // We really do want to use the non-canonical type here. 231209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall if (T == Context.VoidPtrTy) { 232209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall PointerTypeLoc TL = cast<PointerTypeLoc>(TI->getTypeLoc()); 233209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 234209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall Diag(Loc, diag::warn_unused_voidptr) 235209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall << FixItHint::CreateRemoval(TL.getStarLoc()); 236209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall return; 237209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall } 238209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall } 239209acbd6d0c1b4444eb8c1682717753e1cbe38deJohn McCall 240a6115068cde719142eb394db88612c185cabd05bEli Friedman if (E->isGLValue() && E->getType().isVolatileQualified()) { 241a6115068cde719142eb394db88612c185cabd05bEli Friedman Diag(Loc, diag::warn_unused_volatile) << R1 << R2; 242a6115068cde719142eb394db88612c185cabd05bEli Friedman return; 243a6115068cde719142eb394db88612c185cabd05bEli Friedman } 244a6115068cde719142eb394db88612c185cabd05bEli Friedman 245351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek DiagRuntimeBehavior(Loc, 0, PDiag(DiagID) << R1 << R2); 246636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson} 247636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson 248625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::ActOnStartOfCompoundStmt() { 249625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko PushCompoundScope(); 250625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 251625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 252625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::ActOnFinishOfCompoundStmt() { 253625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko PopCompoundScope(); 254625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 255625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 256625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkosema::CompoundScopeInfo &Sema::getCurCompoundScope() const { 257625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return getCurFunction()->CompoundScopes.back(); 258625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 259625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 26060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2611b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, 262a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl MultiStmtArg elts, bool isStmtExpr) { 263a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl unsigned NumElts = elts.size(); 2645354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer Stmt **Elts = elts.data(); 265c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // If we're in C89 mode, check that we don't have any decls after stmts. If 266c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // so, emit an extension diagnostic. 2674e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().C99 && !getLangOpts().CPlusPlus) { 268c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Note that __extension__ can be around a decl. 269c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner unsigned i = 0; 270c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // Skip over all declarations. 271c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i) 272c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 273c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner 274c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner // We found the end of the list or a statement. Scan for another declstmt. 275c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i) 276c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner /*empty*/; 2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 278c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner if (i != NumElts) { 2794afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor Decl *D = *cast<DeclStmt>(Elts[i])->decl_begin(); 280c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner Diag(D->getLocation(), diag::ext_mixed_decls_code); 281c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 282c30ebfbf23d6a471146e3c68c2cf7f170b7e55dcChris Lattner } 28398414c1b7d1944a57156d52e29bd41c005de09acChris Lattner // Warn about unused expressions in statements. 28498414c1b7d1944a57156d52e29bd41c005de09acChris Lattner for (unsigned i = 0; i != NumElts; ++i) { 285636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson // Ignore statements that are last in a statement expression. 286636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson if (isStmtExpr && i == NumElts - 1) 28798414c1b7d1944a57156d52e29bd41c005de09acChris Lattner continue; 2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 289636463e4c43be15e2f0fd0b8a08667f3066d8af7Anders Carlsson DiagnoseUnusedExprResult(Elts[i]); 29098414c1b7d1944a57156d52e29bd41c005de09acChris Lattner } 291a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl 292625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Check for suspicious empty body (null statement) in `for' and `while' 293625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // statements. Don't do anything for template instantiations, this just adds 294625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // noise. 295625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (NumElts != 0 && !CurrentInstantiationScope && 296625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko getCurCompoundScope().HasEmptyLoopBodies) { 297625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko for (unsigned i = 0; i != NumElts - 1; ++i) 298625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnoseEmptyLoopBody(Elts[i], Elts[i + 1]); 299625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko } 300625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 3018189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R)); 3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 30460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 3059ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCaseStmt(SourceLocation CaseLoc, Expr *LHSVal, 3069ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation DotDotDotLoc, Expr *RHSVal, 30724e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner SourceLocation ColonLoc) { 3089ae2f076ca5ab1feb3ba95629099ec2319833701John McCall assert((LHSVal != 0) && "missing expression in case statement"); 309117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 3108ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith if (getCurFunction()->SwitchStack.empty()) { 3118ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Diag(CaseLoc, diag::err_case_not_in_switch); 31224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner return StmtError(); 3138ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CPlusPlus0x) { 3168ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // C99 6.8.4.2p3: The expression shall be an integer constant. 3178ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // However, GCC allows any evaluatable integer expression. 318282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith if (!LHSVal->isTypeDependent() && !LHSVal->isValueDependent()) { 319282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith LHSVal = VerifyIntegerConstantExpression(LHSVal).take(); 320282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith if (!LHSVal) 321282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith return StmtError(); 322282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith } 323117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 3248ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // GCC extension: The expression shall be an integer constant. 325117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 326282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith if (RHSVal && !RHSVal->isTypeDependent() && !RHSVal->isValueDependent()) { 327282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith RHSVal = VerifyIntegerConstantExpression(RHSVal).take(); 328282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith // Recover from an error by just forgetting about it. 3298ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 3308a87e57beb96212ee61dc08a5f691cd7f7710703Chris Lattner } 3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 332dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc, 333dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor ColonLoc); 334781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.back()->addSwitchCase(CS); 335117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(CS); 3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 33824e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner/// ActOnCaseStmtBody - This installs a statement as the body of a case. 3399ae2f076ca5ab1feb3ba95629099ec2319833701John McCallvoid Sema::ActOnCaseStmtBody(Stmt *caseStmt, Stmt *SubStmt) { 3405440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth DiagnoseUnusedExprResult(SubStmt); 3415440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth 34224e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CaseStmt *CS = static_cast<CaseStmt*>(caseStmt); 34324e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner CS->setSubStmt(SubStmt); 34424e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner} 34524e1e707b4c362f18e371e2bbf054a8345b57bfaChris Lattner 34660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 3471eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, 3489ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *SubStmt, Scope *CurScope) { 3495440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth DiagnoseUnusedExprResult(SubStmt); 3505440bfaff6aec058b60bc6da75bb4f13b7a76491Chandler Carruth 351781472fe99a120098c631b0cbe33c89f8cef5e70John McCall if (getCurFunction()->SwitchStack.empty()) { 3520fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner Diag(DefaultLoc, diag::err_default_not_in_switch); 353117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(SubStmt); 3540fa152e72bb71c4aa184d0edd91caa9cbebbf70eChris Lattner } 355117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl 356dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor DefaultStmt *DS = new (Context) DefaultStmt(DefaultLoc, ColonLoc, SubStmt); 357781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.back()->addSwitchCase(DS); 358117054a99f4994e4ec8a1fc904b554e1f2dc9b29Sebastian Redl return Owned(DS); 3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 36160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 36257ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerSema::ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, 36357ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner SourceLocation ColonLoc, Stmt *SubStmt) { 364ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner // If the label was multiply defined, reject it now. 365ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner if (TheDecl->getStmt()) { 366ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Diag(IdentLoc, diag::err_redefinition_of_label) << TheDecl->getDeclName(); 367ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner Diag(TheDecl->getLocation(), diag::note_previous_definition); 368de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SubStmt); 3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 370de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 371ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner // Otherwise, things are good. Fill in the declaration and return it. 372ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner LabelStmt *LS = new (Context) LabelStmt(IdentLoc, TheDecl, SubStmt); 373ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner TheDecl->setStmt(LS); 374203548ba4b72e7e59320d352afc1eb0b5ab131deAbramo Bagnara if (!TheDecl->isGnuLocal()) 375203548ba4b72e7e59320d352afc1eb0b5ab131deAbramo Bagnara TheDecl->setLocation(IdentLoc); 376ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner return Owned(LS); 3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 379534986f2b21e6050bf00163cd6423fd92155a6edRichard SmithStmtResult Sema::ActOnAttributedStmt(SourceLocation AttrLoc, 3804990890fc9428f98bef90ba349203a648c592778Alexander Kornienko ArrayRef<const Attr*> Attrs, 381534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith Stmt *SubStmt) { 3824990890fc9428f98bef90ba349203a648c592778Alexander Kornienko // Fill in the declaration and return it. 3834990890fc9428f98bef90ba349203a648c592778Alexander Kornienko AttributedStmt *LS = AttributedStmt::Create(Context, AttrLoc, Attrs, SubStmt); 384534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith return Owned(LS); 385534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith} 386534986f2b21e6050bf00163cd6423fd92155a6edRichard Smith 38760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 388d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, Decl *CondVar, 38944aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis Stmt *thenStmt, SourceLocation ElseLoc, 39044aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis Stmt *elseStmt) { 39160d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult(CondVal.release()); 3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3938cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor VarDecl *ConditionVar = 0; 394d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 395d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 396586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor CondResult = CheckConditionVariable(ConditionVar, IfLoc, true); 39799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (CondResult.isInvalid()) 39899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 3998cfe5a784133d90bf329fd20801824a6f71bb8caDouglas Gregor } 40099e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Expr *ConditionExpr = CondResult.takeAs<Expr>(); 40199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!ConditionExpr) 40299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 403dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 404754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(thenStmt); 4055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4069ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!elseStmt) { 407625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnoseEmptyStmtBody(ConditionExpr->getLocEnd(), thenStmt, 408625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko diag::warn_empty_if_body); 4092d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson } 4102d85f8ba62fd6fdcf0ae303d77112b413d412caeAnders Carlsson 411754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(elseStmt); 4121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 413dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi return Owned(new (Context) IfStmt(Context, IfLoc, ConditionVar, ConditionExpr, 41444aa1f397855f130e88e62ffc1029f7f83bb5d2eArgyrios Kyrtzidis thenStmt, ElseLoc, elseStmt)); 4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 417f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have 418f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified width and sign. If an overflow occurs, detect it and emit 419f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner/// the specified diagnostic. 420f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattnervoid Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val, 421f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned NewWidth, bool NewSign, 4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SourceLocation Loc, 423f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner unsigned DiagID) { 424f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Perform a conversion to the promoted condition type if needed. 425f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (NewWidth > Val.getBitWidth()) { 426f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is an extension, just do it. 4279f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.extend(NewWidth); 428f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 429f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor 430f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // If the input was signed and negative and the output is 431f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // unsigned, don't bother to warn: this is implementation-defined 432f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // behavior. 433f9f627dbbc62fbf51b906c24c783b4249dc7e9bbDouglas Gregor // FIXME: Introduce a second, default-ignored warning for this case? 434f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewWidth < Val.getBitWidth()) { 435f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // If this is a truncation, check for overflow. 436f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt ConvVal(Val); 4379f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ConvVal = ConvVal.trunc(NewWidth); 438b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(NewSign); 4399f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ConvVal = ConvVal.extend(Val.getBitWidth()); 440b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner ConvVal.setIsSigned(Val.isSigned()); 441f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (ConvVal != Val) 442d3a94e24ddf3fb90de76b17bd176d9ed61e66f2cChris Lattner Diag(Loc, DiagID) << Val.toString(10) << ConvVal.toString(10); 4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 444f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Regardless of whether a diagnostic was emitted, really do the 445f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // truncation. 4469f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.trunc(NewWidth); 447b2137ae3f1bd0aadb0552189af2824a324ffaa69Chris Lattner Val.setIsSigned(NewSign); 448f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else if (NewSign != Val.isSigned()) { 449f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Convert the sign to match the sign of the condition. This can cause 450f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // overflow as well: unsigned(INTMIN) 451dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // We don't diagnose this overflow, because it is implementation-defined 4522853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor // behavior. 4532853eac24e2e70a74d7da817653b0528b976039fDouglas Gregor // FIXME: Introduce a second, default-ignored warning for this case? 454f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner llvm::APSInt OldVal(Val); 455f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Val.setIsSigned(NewSign); 456f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 457f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner} 458f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner 4590471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattnernamespace { 4600471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner struct CaseCompareFunctor { 4610471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 4620471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const llvm::APSInt &RHS) { 4630471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS.first < RHS; 4640471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 4650e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS, 4660e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 4670e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner return LHS.first < RHS.first; 4680e85a2761ace912c66663d779dd230f88cf77fe0Chris Lattner } 4690471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner bool operator()(const llvm::APSInt &LHS, 4700471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner const std::pair<llvm::APSInt, CaseStmt*> &RHS) { 4710471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner return LHS < RHS.first; 4720471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 4730471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner }; 4740471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner} 4750471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner 476764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// CmpCaseVals - Comparison predicate for sorting case values. 477764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner/// 478764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattnerstatic bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs, 479764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner const std::pair<llvm::APSInt, CaseStmt*>& rhs) { 480764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first < rhs.first) 481764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 482764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 483764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner if (lhs.first == rhs.first && 484764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner lhs.second->getCaseLoc().getRawEncoding() 485764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner < rhs.second->getCaseLoc().getRawEncoding()) 486764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return true; 487764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner return false; 488764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner} 489764a7ce5217f9569e100a3445f47496ee82daf86Chris Lattner 490ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// CmpEnumVals - Comparison predicate for sorting enumeration values. 491ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// 492ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool CmpEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, 493ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) 494ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{ 495ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor return lhs.first < rhs.first; 496ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor} 497ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 498ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// EqEnumVals - Comparison preficate for uniqing enumeration values. 499ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor/// 500ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregorstatic bool EqEnumVals(const std::pair<llvm::APSInt, EnumConstantDecl*>& lhs, 501ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const std::pair<llvm::APSInt, EnumConstantDecl*>& rhs) 502ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor{ 503ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor return lhs.first == rhs.first; 504ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor} 505ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 5065f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// GetTypeBeforeIntegralPromotion - Returns the pre-promotion type of 5075f04881eb025f61396d0555d8173730fe2759e0aChris Lattner/// potentially integral-promoted expression @p expr. 508a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCallstatic QualType GetTypeBeforeIntegralPromotion(Expr *&expr) { 509a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(expr)) 510a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall expr = cleanups->getSubExpr(); 511a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall while (ImplicitCastExpr *impcast = dyn_cast<ImplicitCastExpr>(expr)) { 512a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (impcast->getCastKind() != CK_IntegralCast) break; 513a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall expr = impcast->getSubExpr(); 5145f04881eb025f61396d0555d8173730fe2759e0aChris Lattner } 5155f04881eb025f61396d0555d8173730fe2759e0aChris Lattner return expr->getType(); 5165f04881eb025f61396d0555d8173730fe2759e0aChris Lattner} 5175f04881eb025f61396d0555d8173730fe2759e0aChris Lattner 51860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 519dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, Expr *Cond, 520d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall Decl *CondVar) { 52160d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult; 5229ae2f076ca5ab1feb3ba95629099ec2319833701John McCall 523586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor VarDecl *ConditionVar = 0; 524d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 525d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 5269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CondResult = CheckConditionVariable(ConditionVar, SourceLocation(), false); 5279ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 528586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 529dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 5309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.release(); 531586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor } 532dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 5339ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!Cond) 534586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 535dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 536ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor class SwitchConvertDiagnoser : public ICEConvertDiagnoser { 537ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor Expr *Cond; 5388e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 539ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor public: 540ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor SwitchConvertDiagnoser(Expr *Cond) 541ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor : ICEConvertDiagnoser(false, true), Cond(Cond) { } 5428e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 543ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 544ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T) { 545ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Loc, diag::err_typecheck_statement_requires_integer) << T; 546ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 5478e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 548ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 549ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T) { 550ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Loc, diag::err_switch_incomplete_class_type) 551ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor << T << Cond->getSourceRange(); 552ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 5538e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 554ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 555ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T, 556ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType ConvTy) { 557ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Loc, diag::err_switch_explicit_conversion) << T << ConvTy; 558ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 5598e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 560ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 561ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType ConvTy) { 562ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Conv->getLocation(), diag::note_switch_conversion) 563ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor << ConvTy->isEnumeralType() << ConvTy; 564ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 5658e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 566ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 567ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T) { 568ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Loc, diag::err_switch_multiple_conversions) << T; 569ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 5708e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 571ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 572ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType ConvTy) { 573ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return S.Diag(Conv->getLocation(), diag::note_switch_conversion) 574ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor << ConvTy->isEnumeralType() << ConvTy; 575ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 5768e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 577ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor virtual DiagnosticBuilder diagnoseConversion(Sema &S, SourceLocation Loc, 578ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType T, 579ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor QualType ConvTy) { 580ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor return DiagnosticBuilder::getEmpty(); 581ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } 582ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor } SwitchDiagnoser(Cond); 583ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor 5849ae2f076ca5ab1feb3ba95629099ec2319833701John McCall CondResult 585ab41fe914f63bb470dfa7e400876ada72f57a931Douglas Gregor = ConvertToIntegralOrEnumerationType(SwitchLoc, Cond, SwitchDiagnoser, 586f39aec17b89f8f0dd78e78c50ad2fa08f12272e3Richard Smith /*AllowScopedEnumerations*/ true); 5879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) return StmtError(); 5889ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 589dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 590a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall // C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr. 591a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall CondResult = UsualUnaryConversions(Cond); 592a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (CondResult.isInvalid()) return StmtError(); 593a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall Cond = CondResult.take(); 594a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall 595d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (!CondVar) { 596b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(Cond, SwitchLoc); 5974765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall CondResult = MaybeCreateExprWithCleanups(Cond); 5989ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 599586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 6009ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 601586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor } 602b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 603781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchIntoScope(); 604dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 6059ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SwitchStmt *SS = new (Context) SwitchStmt(Context, ConditionVar, Cond); 606781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.push_back(SS); 607586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return Owned(SS); 6087e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner} 6097e52de4b45286d057b367bb1f9283a1e32d79252Chris Lattner 61028164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greifstatic void AdjustAPSInt(llvm::APSInt &Val, unsigned BitWidth, bool IsSigned) { 61128164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif if (Val.getBitWidth() < BitWidth) 6129f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.extend(BitWidth); 61328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif else if (Val.getBitWidth() > BitWidth) 6149f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Val = Val.trunc(BitWidth); 61528164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Val.setIsSigned(IsSigned); 61628164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif} 61728164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif 61860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 6199ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, 6209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *BodyStmt) { 6219ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SwitchStmt *SS = cast<SwitchStmt>(Switch); 622781472fe99a120098c631b0cbe33c89f8cef5e70John McCall assert(SS == getCurFunction()->SwitchStack.back() && 623781472fe99a120098c631b0cbe33c89f8cef5e70John McCall "switch stack missing push/pop!"); 624de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 6259dcbfa450d751bd68fc4af8b75da381d4f6984b9Steve Naroff SS->setBody(BodyStmt, SwitchLoc); 626781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->SwitchStack.pop_back(); 627c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson 628f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Expr *CondExpr = SS->getCond(); 629a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall if (!CondExpr) return StmtError(); 630de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 631a0d3ca1ea5578bc736bb71bcec50ab41fefc87b9Douglas Gregor QualType CondType = CondExpr->getType(); 632a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall 633a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall Expr *CondExprBeforePromotion = CondExpr; 634a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall QualType CondTypeBeforePromotion = 635a8e0cd8cdecc7e0ba1792e46773b884c6eed4829John McCall GetTypeBeforeIntegralPromotion(CondExprBeforePromotion); 63684fb9c0be621c9e4ca4e56f67dae2a0bb6e44821Douglas Gregor 6375f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // C++ 6.4.2.p2: 6385f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // Integral promotions are performed (on the switch condition). 6395f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // 6405f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // A case value unrepresentable by the original switch condition 6415f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // type (before the promotion) doesn't make sense, even when it can 6425f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // be represented by the promoted type. Therefore we need to find 6435f04881eb025f61396d0555d8173730fe2759e0aChris Lattner // the pre-promotion type of the switch condition. 64412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan if (!CondExpr->isTypeDependent()) { 645acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor // We have already converted the expression to an integral or enumeration 646dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // type, when we started the switch statement. If we don't have an 647acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor // appropriate type now, just return an error. 648acb0bd85d30ecacbe872ca9d9cfac5d7b6038a43Douglas Gregor if (!CondType->isIntegralOrEnumerationType()) 64912356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan return StmtError(); 65012356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan 6512b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner if (CondExpr->isKnownToHaveBooleanValue()) { 65212356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // switch(bool_expr) {...} is often a programmer error, e.g. 65312356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // switch(n && mask) { ... } // Doh - should be "n & mask". 65412356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan // One can always use an if statement instead of switch(bool_expr). 65512356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan Diag(SwitchLoc, diag::warn_bool_switch_condition) 65612356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan << CondExpr->getSourceRange(); 65712356b119032edd64e9c32f9f01920d12c2acc57Edward O'Callaghan } 658c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 659de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 660f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Get the bitwidth of the switched-on value before promotions. We must 661f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // convert the integer case values to this width before comparison. 6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump bool HasDependentValue 663dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor = CondExpr->isTypeDependent() || CondExpr->isValueDependent(); 6641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned CondWidth 6651d6ab7af99a1fc059a6aa5da083640c1d94b07f7Chris Lattner = HasDependentValue ? 0 : Context.getIntWidth(CondTypeBeforePromotion); 6661093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier bool CondIsSigned 667575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor = CondTypeBeforePromotion->isSignedIntegerOrEnumerationType(); 6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 669f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Accumulate all of the case values in a vector so that we can sort them 670f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // and detect duplicates. This vector contains the APInt for the case after 671f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // it has been converted to the condition type. 6725f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner typedef SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy; 6730471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner CaseValsTy CaseVals; 6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 675f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // Keep track of any GNU case ranges we see. The APSInt is the low value. 676ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor typedef std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRangesTy; 677ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseRangesTy CaseRanges; 6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 679f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner DefaultStmt *TheDefaultStmt = 0; 6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 681b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner bool CaseListIsErroneous = false; 6821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 683dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue; 684c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson SC = SC->getNextSwitchCase()) { 6851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 686c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { 687f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner if (TheDefaultStmt) { 688f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined); 6895f4a6829dc58cab2f76e2b98492859aa3b91e3f2Chris Lattner Diag(TheDefaultStmt->getDefaultLoc(), diag::note_duplicate_case_prev); 690de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 691f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner // FIXME: Remove the default statement from the switch block so that 692390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // we'll return a valid AST. This requires recursing down the AST and 693390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // finding it, not something we are set up to do right now. For now, 694390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // just lop the entire switch stmt out of the AST. 695b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseListIsErroneous = true; 696c1fcb7762673be706b0a40477d5e93411e918f93Anders Carlsson } 697f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner TheDefaultStmt = DS; 6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 699f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } else { 700f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseStmt *CS = cast<CaseStmt>(SC); 7011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7021e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner Expr *Lo = CS->getLHS(); 703dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 704dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (Lo->isTypeDependent() || Lo->isValueDependent()) { 705dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HasDependentValue = true; 706dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor break; 707dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 7081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7098ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith llvm::APSInt LoVal; 7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus0x) { 7128ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // C++11 [stmt.switch]p2: the constant-expression shall be a converted 7138ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // constant expression of the promoted type of the switch condition. 7148ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith ExprResult ConvLo = 7158ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CheckConvertedConstantExpression(Lo, CondType, LoVal, CCEK_CaseValue); 7168ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith if (ConvLo.isInvalid()) { 7178ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CaseListIsErroneous = true; 7188ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith continue; 7198ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 7208ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Lo = ConvLo.take(); 7218ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } else { 7228ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // We already verified that the expression has a i-c-e value (C99 7238ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // 6.8.4.2p3) - get that value now. 7248ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith LoVal = Lo->EvaluateKnownConstInt(Context); 7258ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 7268ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // If the LHS is not the same type as the condition, insert an implicit 7278ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // cast. 7288ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Lo = DefaultLvalueConversion(Lo).take(); 7298ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Lo = ImpCastExprToType(Lo, CondType, CK_IntegralCast).take(); 7308ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 7318ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 7328ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // Convert the value to the same width/sign as the condition had prior to 7338ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // integral promotions. 7348ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // 7358ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // FIXME: This causes us to reject valid code: 7368ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // switch ((char)c) { case 256: case 0: return 0; } 7378ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // Here we claim there is a duplicated condition value, but there is not. 738f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned, 73928164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Lo->getLocStart(), 740f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner diag::warn_case_value_overflow); 7416c36be5b383875b490684bcf439d6d427298c1afChris Lattner 7421e0a39012467b4f409142c32148036a9ee05e1d7Chris Lattner CS->setLHS(Lo); 7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 744b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner // If this is a case range, remember it in CaseRanges, otherwise CaseVals. 745dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (CS->getRHS()) { 7461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (CS->getRHS()->isTypeDependent() || 747dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CS->getRHS()->isValueDependent()) { 748dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HasDependentValue = true; 749dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor break; 750dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 751f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner CaseRanges.push_back(std::make_pair(LoVal, CS)); 7521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else 753b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner CaseVals.push_back(std::make_pair(LoVal, CS)); 754f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 755f4021e7d5228a2be5a380269dffa0331a6c78b95Chris Lattner } 756b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner 757dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!HasDependentValue) { 7580fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // If we don't have a default statement, check whether the 7590fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // condition is constant. 7600fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall llvm::APSInt ConstantCondValue; 7610fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall bool HasConstantCond = false; 7620fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (!HasDependentValue && !TheDefaultStmt) { 76351f4708c00110940ca3f337961915f2ca1668375Richard Smith HasConstantCond 76480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith = CondExprBeforePromotion->EvaluateAsInt(ConstantCondValue, Context, 76580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::SE_AllowSideEffects); 76680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith assert(!HasConstantCond || 76780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith (ConstantCondValue.getBitWidth() == CondWidth && 76880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith ConstantCondValue.isSigned() == CondIsSigned)); 7690fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 77080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith bool ShouldCheckConstantCond = HasConstantCond; 7710fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 772dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Sort all the scalar case values so we can easily detect duplicates. 773dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); 774dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 775dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!CaseVals.empty()) { 7760fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { 7770fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (ShouldCheckConstantCond && 7780fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall CaseVals[i].first == ConstantCondValue) 7790fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = false; 7800fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 7810fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (i != 0 && CaseVals[i].first == CaseVals[i-1].first) { 782dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If we have a duplicate, report it. 7833940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor // First, determine if either case value has a name 7843940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor StringRef PrevString, CurrString; 7853940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor Expr *PrevCase = CaseVals[i-1].second->getLHS()->IgnoreParenCasts(); 7863940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor Expr *CurrCase = CaseVals[i].second->getLHS()->IgnoreParenCasts(); 7873940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(PrevCase)) { 7883940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor PrevString = DeclRef->getDecl()->getName(); 7893940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor } 7903940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(CurrCase)) { 7913940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor CurrString = DeclRef->getDecl()->getName(); 7923940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor } 79350de5e3247a20e0e548dc47022a011250e6e4e8fDouglas Gregor llvm::SmallString<16> CaseValStr; 79450de5e3247a20e0e548dc47022a011250e6e4e8fDouglas Gregor CaseVals[i-1].first.toString(CaseValStr); 7953940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor 7963940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor if (PrevString == CurrString) 7973940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor Diag(CaseVals[i].second->getLHS()->getLocStart(), 7983940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor diag::err_duplicate_case) << 79950de5e3247a20e0e548dc47022a011250e6e4e8fDouglas Gregor (PrevString.empty() ? CaseValStr.str() : PrevString); 8003940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor else 8013940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor Diag(CaseVals[i].second->getLHS()->getLocStart(), 8023940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor diag::err_duplicate_case_differing_expr) << 80350de5e3247a20e0e548dc47022a011250e6e4e8fDouglas Gregor (PrevString.empty() ? CaseValStr.str() : PrevString) << 80450de5e3247a20e0e548dc47022a011250e6e4e8fDouglas Gregor (CurrString.empty() ? CaseValStr.str() : CurrString) << 8053940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor CaseValStr; 8063940ce83438e2332ba541f4e65e700fbefe0c7a8Douglas Gregor 8070fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CaseVals[i-1].second->getLHS()->getLocStart(), 808dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::note_duplicate_case_prev); 809390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: We really want to remove the bogus case stmt from the 810390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // substmt, but we have no way to do this right now. 811dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseListIsErroneous = true; 812dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 8136efc4d3659632ddcea4a58cb62e9ee54ca4a373eChris Lattner } 814b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 8151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 816dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Detect duplicate case ranges, which usually don't exist at all in 817dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // the first place. 818dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (!CaseRanges.empty()) { 819dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Sort all the case ranges by their low value so we can easily detect 820dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // overlaps between ranges. 821dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::stable_sort(CaseRanges.begin(), CaseRanges.end()); 8221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 823dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Scan the ranges, computing the high values and removing empty ranges. 824dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor std::vector<llvm::APSInt> HiVals; 825dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 8260fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall llvm::APSInt &LoVal = CaseRanges[i].first; 827dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CR = CaseRanges[i].second; 828dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Expr *Hi = CR->getRHS(); 8298ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith llvm::APSInt HiVal; 8308ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 8314e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus0x) { 8328ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // C++11 [stmt.switch]p2: the constant-expression shall be a converted 8338ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // constant expression of the promoted type of the switch condition. 8348ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith ExprResult ConvHi = 8358ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CheckConvertedConstantExpression(Hi, CondType, HiVal, 8368ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CCEK_CaseValue); 8378ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith if (ConvHi.isInvalid()) { 8388ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith CaseListIsErroneous = true; 8398ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith continue; 8408ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 8418ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Hi = ConvHi.take(); 8428ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } else { 8438ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith HiVal = Hi->EvaluateKnownConstInt(Context); 8448ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 8458ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // If the RHS is not the same type as the condition, insert an 8468ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith // implicit cast. 8478ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Hi = DefaultLvalueConversion(Hi).take(); 8488ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith Hi = ImpCastExprToType(Hi, CondType, CK_IntegralCast).take(); 8498ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith } 8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 851dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Convert the value to the same width/sign as the condition. 852dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned, 85328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Hi->getLocStart(), 854dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::warn_case_value_overflow); 8551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 856dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CR->setRHS(Hi); 8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 858dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If the low value is bigger than the high value, the case is empty. 8590fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (LoVal > HiVal) { 860dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range) 861dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor << SourceRange(CR->getLHS()->getLocStart(), 86228164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif Hi->getLocEnd()); 863dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseRanges.erase(CaseRanges.begin()+i); 864dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor --i, --e; 865dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor continue; 866dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 8670fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 8680fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (ShouldCheckConstantCond && 8690fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall LoVal <= ConstantCondValue && 8700fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ConstantCondValue <= HiVal) 8710fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall ShouldCheckConstantCond = false; 8720fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 873dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor HiVals.push_back(HiVal); 8740471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 876dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Rescan the ranges, looking for overlap with singleton values and other 877dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // ranges. Since the range list is sorted, we only need to compare case 878dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // ranges with their neighbors. 879dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) { 880dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt &CRLo = CaseRanges[i].first; 881dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt &CRHi = HiVals[i]; 882dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *CR = CaseRanges[i].second; 8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 884dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Check to see whether the case range overlaps with any 885dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // singleton cases. 886dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseStmt *OverlapStmt = 0; 887dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor llvm::APSInt OverlapVal(32); 8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 889dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Find the smallest value >= the lower bound. If I is in the 890dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // case range, then we have overlap. 891dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(), 892dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseVals.end(), CRLo, 893dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseCompareFunctor()); 894dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (I != CaseVals.end() && I->first < CRHi) { 895dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = I->first; // Found overlap with scalar. 896dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = I->second; 897dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 899dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Find the smallest value bigger than the upper bound. 900dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor()); 901dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (I != CaseVals.begin() && (I-1)->first >= CRLo) { 902dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = (I-1)->first; // Found overlap with scalar. 903dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = (I-1)->second; 904dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 9051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 906dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // Check to see if this case stmt overlaps with the subsequent 907dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // case range. 908dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (i && CRLo <= HiVals[i-1]) { 909dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapVal = HiVals[i-1]; // Found overlap with range. 910dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor OverlapStmt = CaseRanges[i-1].second; 911dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 9121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 913dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor if (OverlapStmt) { 914dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor // If we have a duplicate, report it. 915dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor Diag(CR->getLHS()->getLocStart(), diag::err_duplicate_case) 916dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor << OverlapVal.toString(10); 9171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(OverlapStmt->getLHS()->getLocStart(), 918dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor diag::note_duplicate_case_prev); 919390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: We really want to remove the bogus case stmt from the 920390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // substmt, but we have no way to do this right now. 921dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor CaseListIsErroneous = true; 922dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor } 9230471f5bc8191e39cdb61fabcaf1870e2af2d42e8Chris Lattner } 924b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 925ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 9260fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Complain if we have a constant condition and we didn't find a match. 9270fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall if (!CaseListIsErroneous && ShouldCheckConstantCond) { 9280fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // TODO: it would be nice if we printed enums as enums, chars as 9290fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // chars, etc. 9300fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall Diag(CondExpr->getExprLoc(), diag::warn_missing_case_for_condition) 9310fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << ConstantCondValue.toString(10) 9320fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall << CondExpr->getSourceRange(); 9330fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall } 9340fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall 9350fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Check to see if switch is over an Enum and handles all of its 936559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // values. We only issue a warning if there is not 'default:', but 937559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // we still do the analysis to preserve this information in the AST 938559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // (which can be used by flow-based analyes). 9390fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // 940ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner const EnumType *ET = CondTypeBeforePromotion->getAs<EnumType>(); 941559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 942ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor // If switch has default case, then ignore it. 943559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!CaseListIsErroneous && !HasConstantCond && ET) { 944ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor const EnumDecl *ED = ET->getDecl(); 9455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> 94658f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet EnumValsTy; 947ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor EnumValsTy EnumVals; 948ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 9490fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // Gather all enum values, set their type and sort them, 9500fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall // allowing easier comparison with CaseVals. 9510fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin(); 95228164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif EDI != ED->enumerator_end(); ++EDI) { 95328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif llvm::APSInt Val = EDI->getInitVal(); 95428164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Val, CondWidth, CondIsSigned); 955581deb3da481053c4993c7600f97acf7768caac5David Blaikie EnumVals.push_back(std::make_pair(Val, *EDI)); 956ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 957ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); 9580fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall EnumValsTy::iterator EIend = 9590fb97083cc0f8a82e404e22991ae80d2216e71d5John McCall std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); 960559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 961559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // See which case values aren't in enum. 9629366750a5a97c8aeae1df4898ea849b087865195David Blaikie EnumValsTy::const_iterator EI = EnumVals.begin(); 9639366750a5a97c8aeae1df4898ea849b087865195David Blaikie for (CaseValsTy::const_iterator CI = CaseVals.begin(); 9649366750a5a97c8aeae1df4898ea849b087865195David Blaikie CI != CaseVals.end(); CI++) { 9659366750a5a97c8aeae1df4898ea849b087865195David Blaikie while (EI != EIend && EI->first < CI->first) 9669366750a5a97c8aeae1df4898ea849b087865195David Blaikie EI++; 9679366750a5a97c8aeae1df4898ea849b087865195David Blaikie if (EI == EIend || EI->first > CI->first) 9689366750a5a97c8aeae1df4898ea849b087865195David Blaikie Diag(CI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) 96954faba4f7f3f0e8f1376da1c459312596ad5486dFariborz Jahanian << CondTypeBeforePromotion; 9709366750a5a97c8aeae1df4898ea849b087865195David Blaikie } 9719366750a5a97c8aeae1df4898ea849b087865195David Blaikie // See which of case ranges aren't in enum 9729366750a5a97c8aeae1df4898ea849b087865195David Blaikie EI = EnumVals.begin(); 9739366750a5a97c8aeae1df4898ea849b087865195David Blaikie for (CaseRangesTy::const_iterator RI = CaseRanges.begin(); 9749366750a5a97c8aeae1df4898ea849b087865195David Blaikie RI != CaseRanges.end() && EI != EIend; RI++) { 9759366750a5a97c8aeae1df4898ea849b087865195David Blaikie while (EI != EIend && EI->first < RI->first) 9769366750a5a97c8aeae1df4898ea849b087865195David Blaikie EI++; 9779366750a5a97c8aeae1df4898ea849b087865195David Blaikie 9789366750a5a97c8aeae1df4898ea849b087865195David Blaikie if (EI == EIend || EI->first != RI->first) { 9799366750a5a97c8aeae1df4898ea849b087865195David Blaikie Diag(RI->second->getLHS()->getExprLoc(), diag::warn_not_in_enum) 98054faba4f7f3f0e8f1376da1c459312596ad5486dFariborz Jahanian << CondTypeBeforePromotion; 98147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 982e0ba9d1beeba01a96808c2fc61f9ca89acec313bTed Kremenek 9831093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier llvm::APSInt Hi = 9849366750a5a97c8aeae1df4898ea849b087865195David Blaikie RI->second->getRHS()->EvaluateKnownConstInt(Context); 9859366750a5a97c8aeae1df4898ea849b087865195David Blaikie AdjustAPSInt(Hi, CondWidth, CondIsSigned); 9869366750a5a97c8aeae1df4898ea849b087865195David Blaikie while (EI != EIend && EI->first < Hi) 9879366750a5a97c8aeae1df4898ea849b087865195David Blaikie EI++; 9889366750a5a97c8aeae1df4898ea849b087865195David Blaikie if (EI == EIend || EI->first != Hi) 9899366750a5a97c8aeae1df4898ea849b087865195David Blaikie Diag(RI->second->getRHS()->getExprLoc(), diag::warn_not_in_enum) 99054faba4f7f3f0e8f1376da1c459312596ad5486dFariborz Jahanian << CondTypeBeforePromotion; 991ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 992dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 993559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // Check which enum vals aren't in switch 994ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseValsTy::const_iterator CI = CaseVals.begin(); 995ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CaseRangesTy::const_iterator RI = CaseRanges.begin(); 996559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek bool hasCasesNotInSwitch = false; 997559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 9985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<DeclarationName,8> UnhandledNames; 999dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 10009366750a5a97c8aeae1df4898ea849b087865195David Blaikie for (EI = EnumVals.begin(); EI != EIend; EI++){ 1001ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner // Drop unneeded case values 1002ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor llvm::APSInt CIVal; 1003ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor while (CI != CaseVals.end() && CI->first < EI->first) 1004ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor CI++; 1005dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1006ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor if (CI != CaseVals.end() && CI->first == EI->first) 1007ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor continue; 1008ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 1009559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek // Drop unneeded case ranges 1010ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor for (; RI != CaseRanges.end(); RI++) { 1011a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt Hi = 1012a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith RI->second->getRHS()->EvaluateKnownConstInt(Context); 101328164ab2ac1deea68cdb989f941728bf1860ce41Gabor Greif AdjustAPSInt(Hi, CondWidth, CondIsSigned); 1014ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor if (EI->first <= Hi) 1015ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor break; 1016ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 1017ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor 1018559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (RI == CaseRanges.end() || EI->first < RI->first) { 101947bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek hasCasesNotInSwitch = true; 102031ceb61172bca7ebc3fb90e9125864c7a29c55c0David Blaikie UnhandledNames.push_back(EI->second->getDeclName()); 102147bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek } 1022ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 1023dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 1024585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie if (TheDefaultStmt && UnhandledNames.empty()) 1025585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie Diag(TheDefaultStmt->getDefaultLoc(), diag::warn_unreachable_default); 102631ceb61172bca7ebc3fb90e9125864c7a29c55c0David Blaikie 1027ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner // Produce a nice diagnostic if multiple values aren't handled. 1028ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner switch (UnhandledNames.size()) { 1029ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 0: break; 1030ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 1: 10311093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier Diag(CondExpr->getExprLoc(), TheDefaultStmt 1032585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie ? diag::warn_def_missing_case1 : diag::warn_missing_case1) 1033ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0]; 1034ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 1035ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 2: 10361093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier Diag(CondExpr->getExprLoc(), TheDefaultStmt 1037585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie ? diag::warn_def_missing_case2 : diag::warn_missing_case2) 1038ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1]; 1039ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 1040ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner case 3: 1041585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie Diag(CondExpr->getExprLoc(), TheDefaultStmt 1042585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie ? diag::warn_def_missing_case3 : diag::warn_missing_case3) 1043ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; 1044ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 1045ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner default: 1046585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie Diag(CondExpr->getExprLoc(), TheDefaultStmt 1047585d7795c2dddaa510b3bb1b3b18633bfcfdf643David Blaikie ? diag::warn_def_missing_cases : diag::warn_missing_cases) 1048ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << (unsigned)UnhandledNames.size() 1049ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner << UnhandledNames[0] << UnhandledNames[1] << UnhandledNames[2]; 1050ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner break; 1051ce78461303f45fecb3460d1c49c9b71f27ad19c3Chris Lattner } 1052559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek 1053559fb554602bedb57dbbf3cc14ac8a38264b4547Ted Kremenek if (!hasCasesNotInSwitch) 105447bb27f16882e4f5ababdd0cf6642bb904a9aaf8Ted Kremenek SS->setAllEnumCasesCovered(); 1055ba915af51ced751c46e7c2b9c6f3b59d2e668825Douglas Gregor } 1056b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner } 1057dbb26db1d426fb6caaaf1b4fa47b46d1947c12c9Douglas Gregor 1058625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), BodyStmt, 1059625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko diag::warn_empty_switch_body); 1060625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 1061390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // FIXME: If the case list was broken is some way, we don't have a good system 1062390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump // to patch it up. Instead, just return the whole substmt as broken. 1063b2ec9d6fede9cccc170a202de7bf7f523dea8be4Chris Lattner if (CaseListIsErroneous) 1064de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return StmtError(); 1065de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl 1066de307473448fb3cebcb4c10090728300b53bca03Sebastian Redl return Owned(SS); 10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1069379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanianvoid 1070379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz JahanianSema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType, 1071379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian Expr *SrcExpr) { 1072379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian unsigned DIAG = diag::warn_not_in_enum_assignement; 10731093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier if (Diags.getDiagnosticLevel(DIAG, SrcExpr->getExprLoc()) 1074379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian == DiagnosticsEngine::Ignored) 1075379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian return; 10761093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 1077379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian if (const EnumType *ET = DstType->getAs<EnumType>()) 1078379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian if (!Context.hasSameType(SrcType, DstType) && 1079379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian SrcType->isIntegerType()) { 1080379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian if (!SrcExpr->isTypeDependent() && !SrcExpr->isValueDependent() && 1081379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian SrcExpr->isIntegerConstantExpr(Context)) { 1082379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian // Get the bitwidth of the enum value before promotions. 1083379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian unsigned DstWith = Context.getIntWidth(DstType); 1084379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian bool DstIsSigned = DstType->isSignedIntegerOrEnumerationType(); 1085379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian 1086379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian llvm::APSInt RhsVal = SrcExpr->EvaluateKnownConstInt(Context); 1087379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian const EnumDecl *ED = ET->getDecl(); 1088379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64> 1089379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian EnumValsTy; 1090379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian EnumValsTy EnumVals; 10911093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 1092379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian // Gather all enum values, set their type and sort them, 1093379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian // allowing easier comparison with rhs constant. 1094379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin(); 1095379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian EDI != ED->enumerator_end(); ++EDI) { 1096379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian llvm::APSInt Val = EDI->getInitVal(); 1097379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian AdjustAPSInt(Val, DstWith, DstIsSigned); 1098379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian EnumVals.push_back(std::make_pair(Val, *EDI)); 1099379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian } 1100379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian if (EnumVals.empty()) 1101379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian return; 1102379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); 1103379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian EnumValsTy::iterator EIend = 1104379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); 11051093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 1106379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian // See which case values aren't in enum. 1107379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian EnumValsTy::const_iterator EI = EnumVals.begin(); 1108379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian while (EI != EIend && EI->first < RhsVal) 1109379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian EI++; 1110379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian if (EI == EIend || EI->first != RhsVal) { 1111379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignement) 1112379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian << DstType; 1113379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian } 1114379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian } 1115379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian } 1116379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian} 1117379b28183a7dcb715c3f3eb2da4b0157d6d8ffbeFariborz Jahanian 111860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1119dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, 11209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Decl *CondVar, Stmt *Body) { 112160d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult CondResult(Cond.release()); 1122dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 11235656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor VarDecl *ConditionVar = 0; 1124d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (CondVar) { 1125d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(CondVar); 1126586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor CondResult = CheckConditionVariable(ConditionVar, WhileLoc, true); 112799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (CondResult.isInvalid()) 112899e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 11295656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor } 11309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *ConditionExpr = CondResult.take(); 113199e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (!ConditionExpr) 113299e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 1133dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 11349ae2f076ca5ab1feb3ba95629099ec2319833701John McCall DiagnoseUnusedExprResult(Body); 11351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1136625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (isa<NullStmt>(Body)) 1137625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko getCurCompoundScope().setHasEmptyLoopBodies(); 1138625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 113943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor return Owned(new (Context) WhileStmt(Context, ConditionVar, ConditionExpr, 11409ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Body, WhileLoc)); 11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 114360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 11449ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, 1145989135901c750af61ef012b6b0a0368be415bc46Chris Lattner SourceLocation WhileLoc, SourceLocation CondLParen, 11469ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *Cond, SourceLocation CondRParen) { 11479ae2f076ca5ab1feb3ba95629099ec2319833701John McCall assert(Cond && "ActOnDoStmt(): missing expression"); 1148f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 1149429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult CondResult = CheckBooleanCondition(Cond, DoLoc); 1150429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (CondResult.isInvalid() || CondResult.isInvalid()) 11515a881bb09928b7ade891efc680088aaad276f8d6John McCall return StmtError(); 1152429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley Cond = CondResult.take(); 11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1154b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(Cond, DoLoc); 1155429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CondResult = MaybeCreateExprWithCleanups(Cond); 11569ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (CondResult.isInvalid()) 1157586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor return StmtError(); 11589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Cond = CondResult.take(); 1159dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 11609ae2f076ca5ab1feb3ba95629099ec2319833701John McCall DiagnoseUnusedExprResult(Body); 1161754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson 11629ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) DoStmt(Body, Cond, DoLoc, WhileLoc, CondRParen)); 11635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1165694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieunamespace { 1166694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // This visitor will traverse a conditional statement and store all 1167694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // the evaluated decls into a vector. Simple is set to true if none 1168694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // of the excluded constructs are used. 1169694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu class DeclExtractor : public EvaluatedExprVisitor<DeclExtractor> { 1170694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallPtrSet<VarDecl*, 8> &Decls; 1171694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallVector<SourceRange, 10> &Ranges; 1172694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu bool Simple; 1173694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieupublic: 1174694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu typedef EvaluatedExprVisitor<DeclExtractor> Inherited; 1175694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1176694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclExtractor(Sema &S, llvm::SmallPtrSet<VarDecl*, 8> &Decls, 1177facde171ae4b8926622a1bffa833732a06f1875bBenjamin Kramer llvm::SmallVector<SourceRange, 10> &Ranges) : 1178694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Inherited(S.Context), 1179694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Decls(Decls), 1180694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Ranges(Ranges), 1181facde171ae4b8926622a1bffa833732a06f1875bBenjamin Kramer Simple(true) {} 1182694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1183694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu bool isSimple() { return Simple; } 1184694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1185694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Replaces the method in EvaluatedExprVisitor. 1186694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitMemberExpr(MemberExpr* E) { 1187694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Simple = false; 1188694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1189694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1190694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Any Stmt not whitelisted will cause the condition to be marked complex. 1191694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitStmt(Stmt *S) { 1192694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Simple = false; 1193694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1194694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1195694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitBinaryOperator(BinaryOperator *E) { 1196694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getLHS()); 1197694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getRHS()); 1198694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1199694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1200694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitCastExpr(CastExpr *E) { 1201694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getSubExpr()); 1202694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1203694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1204694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitUnaryOperator(UnaryOperator *E) { 1205694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Skip checking conditionals with derefernces. 1206694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (E->getOpcode() == UO_Deref) 1207694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Simple = false; 1208694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu else 1209694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getSubExpr()); 1210694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1211694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1212694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitConditionalOperator(ConditionalOperator *E) { 1213694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getCond()); 1214694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getTrueExpr()); 1215694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getFalseExpr()); 1216694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1217694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1218694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitParenExpr(ParenExpr *E) { 1219694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getSubExpr()); 1220694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1221694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1222694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitBinaryConditionalOperator(BinaryConditionalOperator *E) { 1223694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getOpaqueValue()->getSourceExpr()); 1224694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getFalseExpr()); 1225694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1226694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1227694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitIntegerLiteral(IntegerLiteral *E) { } 1228694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitFloatingLiteral(FloatingLiteral *E) { } 1229694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { } 1230694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitCharacterLiteral(CharacterLiteral *E) { } 1231694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitGNUNullExpr(GNUNullExpr *E) { } 1232694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitImaginaryLiteral(ImaginaryLiteral *E) { } 1233694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1234694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitDeclRefExpr(DeclRefExpr *E) { 1235694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); 1236694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (!VD) return; 1237694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1238694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Ranges.push_back(E->getSourceRange()); 1239694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1240694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Decls.insert(VD); 1241694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1242694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1243694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu }; // end class DeclExtractor 1244694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1245694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // DeclMatcher checks to see if the decls are used in a non-evauluated 12461093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier // context. 1247694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu class DeclMatcher : public EvaluatedExprVisitor<DeclMatcher> { 1248694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallPtrSet<VarDecl*, 8> &Decls; 1249694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu bool FoundDecl; 1250694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1251694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieupublic: 1252694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu typedef EvaluatedExprVisitor<DeclMatcher> Inherited; 1253694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1254694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclMatcher(Sema &S, llvm::SmallPtrSet<VarDecl*, 8> &Decls, Stmt *Statement) : 1255694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Inherited(S.Context), Decls(Decls), FoundDecl(false) { 1256694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (!Statement) return; 1257694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1258694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(Statement); 1259694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1260694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1261694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitReturnStmt(ReturnStmt *S) { 1262694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu FoundDecl = true; 1263694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1264694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1265694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitBreakStmt(BreakStmt *S) { 1266694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu FoundDecl = true; 1267694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1268694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1269694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitGotoStmt(GotoStmt *S) { 1270694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu FoundDecl = true; 1271694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1272694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1273694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitCastExpr(CastExpr *E) { 1274694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (E->getCastKind() == CK_LValueToRValue) 1275694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(E->getSubExpr()); 1276694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu else 1277694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E->getSubExpr()); 1278694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1279694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1280694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void CheckLValueToRValueCast(Expr *E) { 1281694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu E = E->IgnoreParenImpCasts(); 1282694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1283694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (isa<DeclRefExpr>(E)) { 1284694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1285694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1286694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1287694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 1288694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(CO->getCond()); 1289694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(CO->getTrueExpr()); 1290694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(CO->getFalseExpr()); 1291694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1292694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1293694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1294694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (BinaryConditionalOperator *BCO = 1295694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu dyn_cast<BinaryConditionalOperator>(E)) { 1296694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(BCO->getOpaqueValue()->getSourceExpr()); 1297694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckLValueToRValueCast(BCO->getFalseExpr()); 1298694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1299694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1300694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1301694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Visit(E); 1302694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1303694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1304694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void VisitDeclRefExpr(DeclRefExpr *E) { 1305694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) 1306694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (Decls.count(VD)) 1307694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu FoundDecl = true; 1308694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1309694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1310694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu bool FoundDeclInUse() { return FoundDecl; } 1311694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1312694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu }; // end class DeclMatcher 1313694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1314694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu void CheckForLoopConditionalStatement(Sema &S, Expr *Second, 1315694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Expr *Third, Stmt *Body) { 1316694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Condition is empty 1317694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (!Second) return; 1318694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1319694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (S.Diags.getDiagnosticLevel(diag::warn_variables_not_in_loop_body, 1320694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu Second->getLocStart()) 1321694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu == DiagnosticsEngine::Ignored) 1322694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1323694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1324694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PartialDiagnostic PDiag = S.PDiag(diag::warn_variables_not_in_loop_body); 1325694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallPtrSet<VarDecl*, 8> Decls; 1326694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu llvm::SmallVector<SourceRange, 10> Ranges; 1327facde171ae4b8926622a1bffa833732a06f1875bBenjamin Kramer DeclExtractor DE(S, Decls, Ranges); 1328694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DE.Visit(Second); 1329694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1330694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Don't analyze complex conditionals. 1331694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (!DE.isSimple()) return; 1332694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1333694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // No decls found. 1334694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (Decls.size() == 0) return; 1335694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 13369087599e5ee64ecd30194b3d89f8748ac95c62f7Richard Trieu // Don't warn on volatile, static, or global variables. 1337694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu for (llvm::SmallPtrSet<VarDecl*, 8>::iterator I = Decls.begin(), 1338694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu E = Decls.end(); 1339694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu I != E; ++I) 13409087599e5ee64ecd30194b3d89f8748ac95c62f7Richard Trieu if ((*I)->getType().isVolatileQualified() || 13419087599e5ee64ecd30194b3d89f8748ac95c62f7Richard Trieu (*I)->hasGlobalStorage()) return; 1342694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1343694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (DeclMatcher(S, Decls, Second).FoundDeclInUse() || 1344694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclMatcher(S, Decls, Third).FoundDeclInUse() || 1345694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu DeclMatcher(S, Decls, Body).FoundDeclInUse()) 1346694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu return; 1347694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1348694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Load decl names into diagnostic. 1349694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (Decls.size() > 4) 1350694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << 0; 1351694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu else { 1352694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << Decls.size(); 1353694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu for (llvm::SmallPtrSet<VarDecl*, 8>::iterator I = Decls.begin(), 1354694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu E = Decls.end(); 1355694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu I != E; ++I) 1356694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << (*I)->getDeclName(); 1357694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1358694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1359694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Load SourceRanges into diagnostic if there is room. 1360694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu // Otherwise, load the SourceRange of the conditional expression. 1361694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu if (Ranges.size() <= PartialDiagnostic::MaxArguments) 1362694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu for (llvm::SmallVector<SourceRange, 10>::iterator I = Ranges.begin(), 1363694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu E = Ranges.end(); 1364694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu I != E; ++I) 1365694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << *I; 1366694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu else 1367694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu PDiag << Second->getSourceRange(); 1368694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1369694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu S.Diag(Ranges.begin()->getBegin(), PDiag); 1370694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu } 1371694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 1372694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu} // end namespace 1373694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 137460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1375f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 13769ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *First, FullExprArg second, Decl *secondVar, 137799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor FullExprArg third, 13789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall SourceLocation RParenLoc, Stmt *Body) { 13794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CPlusPlus) { 13805921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) { 1381f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 1382f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 1383f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 13845921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end(); 13855921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis DI!=DE; ++DI) { 13865921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VarDecl *VD = dyn_cast<VarDecl>(*DI); 1387b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage()) 13885921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis VD = 0; 13895921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis if (VD == 0) 13905921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for); 13915921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis // FIXME: mark decl erroneous! 13925921093cf1c2e9a8bd1a22b6f612e551bae7476bArgyrios Kyrtzidis } 1393ae3b701f59e78e058b83344be17206af3bf5d277Chris Lattner } 13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 139599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor 1396694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu CheckForLoopConditionalStatement(*this, second.get(), third.get(), Body); 1397694e796f462748ab4dc7ecdf4be5da44dd2c8c94Richard Trieu 139860d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult SecondResult(second.release()); 139999e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor VarDecl *ConditionVar = 0; 1400d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall if (secondVar) { 1401d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall ConditionVar = cast<VarDecl>(secondVar); 1402586596fd7f7a336a2847b300c80614dcf39ab6d5Douglas Gregor SecondResult = CheckConditionVariable(ConditionVar, ForLoc, true); 140399e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor if (SecondResult.isInvalid()) 140499e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor return StmtError(); 140599e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor } 1406dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 140799e9b4d172f6877e6ba5ebe75bb8238721f5e01cDouglas Gregor Expr *Third = third.release().takeAs<Expr>(); 1408dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 14093af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson DiagnoseUnusedExprResult(First); 14103af708ff19e4ae2bf9e40550548361b00e5916bfAnders Carlsson DiagnoseUnusedExprResult(Third); 1411754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson DiagnoseUnusedExprResult(Body); 1412754431107b61a0523df5271c2876a73dd5a051e9Anders Carlsson 1413625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (isa<NullStmt>(Body)) 1414625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko getCurCompoundScope().setHasEmptyLoopBodies(); 1415625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 1416dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi return Owned(new (Context) ForStmt(Context, First, 1417dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi SecondResult.take(), ConditionVar, 1418dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi Third, Body, ForLoc, LParenLoc, 141943dec6bbde2d0a16c35978983761c8b7030c8e18Douglas Gregor RParenLoc)); 14205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1422f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// In an Objective C collection iteration statement: 1423f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// for (x in y) 1424f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// x can be an arbitrary l-value expression. Bind it up as a 1425f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall/// full-expression. 1426f6a1648197562e0b133440d612d9af297d0a86ccJohn McCallStmtResult Sema::ActOnForEachLValueExpr(Expr *E) { 142729bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall // Reduce placeholder expressions here. Note that this rejects the 142829bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall // use of pseudo-object l-values in this position. 142929bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall ExprResult result = CheckPlaceholderExpr(E); 143029bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall if (result.isInvalid()) return StmtError(); 143129bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall E = result.take(); 143229bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall 1433f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall CheckImplicitConversions(E); 143429bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall 143529bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall result = MaybeCreateExprWithCleanups(E); 143629bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall if (result.isInvalid()) return StmtError(); 143729bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall 143829bbd1a33edfd3c81c35d5076530c2867a05bddcJohn McCall return Owned(static_cast<Stmt*>(result.take())); 1439f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall} 1440f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall 1441990567cb60e8530ba01b41d4e056e32b44b95ec0John McCallExprResult 1442a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz JahanianSema::CheckObjCForCollectionOperand(SourceLocation forLoc, Expr *collection) { 1443a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian if (!collection) 1444a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian return ExprError(); 14451093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 1446990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Bail out early if we've got a type-dependent expression. 1447990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (collection->isTypeDependent()) return Owned(collection); 1448990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1449990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Perform normal l-value conversion. 1450990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ExprResult result = DefaultFunctionArrayLvalueConversion(collection); 1451990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (result.isInvalid()) 1452990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return ExprError(); 1453990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall collection = result.take(); 1454990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1455990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // The operand needs to have object-pointer type. 1456990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // TODO: should we do a contextual conversion? 1457990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall const ObjCObjectPointerType *pointerType = 1458990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall collection->getType()->getAs<ObjCObjectPointerType>(); 1459990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!pointerType) 1460990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return Diag(forLoc, diag::err_collection_expr_type) 1461990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall << collection->getType() << collection->getSourceRange(); 1462990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1463990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Check that the operand provides 1464990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // - countByEnumeratingWithState:objects:count: 1465990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall const ObjCObjectType *objectType = pointerType->getObjectType(); 1466990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ObjCInterfaceDecl *iface = objectType->getInterface(); 1467990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1468990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If we have a forward-declared type, we can't do this check. 1469b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor // Under ARC, it is an error not to have a forward-declared class. 14701093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier if (iface && 1471b3029960632ca8a3248e74770eda64d6c16f7246Douglas Gregor RequireCompleteType(forLoc, QualType(objectType, 0), 14724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie getLangOpts().ObjCAutoRefCount 1473d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor ? diag::err_arc_collection_forward 1474d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor : 0, 1475d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor collection)) { 1476990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Otherwise, if we have any useful type information, check that 1477990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // the type declares the appropriate method. 1478990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } else if (iface || !objectType->qual_empty()) { 1479990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall IdentifierInfo *selectorIdents[] = { 1480990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("countByEnumeratingWithState"), 1481990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("objects"), 1482990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall &Context.Idents.get("count") 1483990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall }; 1484990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Selector selector = Context.Selectors.getSelector(3, &selectorIdents[0]); 1485990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1486990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall ObjCMethodDecl *method = 0; 1487990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1488990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If there's an interface, look in both the public and private APIs. 1489990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (iface) { 1490990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall method = iface->lookupInstanceMethod(selector); 1491e61354b274ec5aa6acf3d15271896ce7596bb123Anna Zaks if (!method) method = iface->lookupPrivateMethod(selector); 1492990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1493990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1494990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Also check protocol qualifiers. 1495990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!method) 1496990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall method = LookupMethodInQualifiedType(selector, pointerType, 1497990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall /*instance*/ true); 1498990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1499990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // If we didn't find it anywhere, give up. 1500990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall if (!method) { 1501990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Diag(forLoc, diag::warn_collection_expr_type) 1502990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall << collection->getType() << selector << collection->getSourceRange(); 1503990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1504990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1505990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // TODO: check for an incompatible signature? 1506990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 1507990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1508990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Wrap up any cleanups in the expression. 1509990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall return Owned(MaybeCreateExprWithCleanups(collection)); 1510990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall} 1511990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 151260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 1513f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian RedlSema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, 1514a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian Stmt *First, Expr *collection, 1515a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian SourceLocation RParenLoc) { 15161093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 15171093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier ExprResult CollectionExprResult = 1518a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian CheckObjCForCollectionOperand(ForLoc, collection); 15191093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 152020552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (First) { 152120552d2842245692b649e0d25380670922f954a2Fariborz Jahanian QualType FirstType; 152220552d2842245692b649e0d25380670922f954a2Fariborz Jahanian if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) { 15237e24e82a70a2c681f4291a3397bcd1e1005f251aChris Lattner if (!DS->isSingleDecl()) 1524f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag((*DS->decl_begin())->getLocation(), 1525f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_toomany_element_decls)); 1526f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl 1527f85e193739c953358c865005855253af4f68a497John McCall VarDecl *D = cast<VarDecl>(DS->getSingleDecl()); 1528f85e193739c953358c865005855253af4f68a497John McCall FirstType = D->getType(); 1529f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // C99 6.8.5p3: The declaration part of a 'for' statement shall only 1530f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // declare identifiers for objects having storage class 'auto' or 1531f3a41af4d5c98a72a1d6720bbbfd658e57ef2541Chris Lattner // 'register'. 1532f85e193739c953358c865005855253af4f68a497John McCall if (!D->hasLocalStorage()) 1533f85e193739c953358c865005855253af4f68a497John McCall return StmtError(Diag(D->getLocation(), 1534f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_non_variable_decl_in_for)); 15351fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } else { 1536c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor Expr *FirstE = cast<Expr>(First); 15377eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall if (!FirstE->isTypeDependent() && !FirstE->isLValue()) 1538f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl return StmtError(Diag(First->getLocStart(), 1539f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl diag::err_selector_element_not_lvalue) 1540f05b1520d6f175acbfc3913489f4dfa842875ec4Sebastian Redl << First->getSourceRange()); 15411fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson 15421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FirstType = static_cast<Expr*>(First)->getType(); 15431fe379f0fa6fbc0a6057e8966253aea2957ca953Anders Carlsson } 1544c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor if (!FirstType->isDependentType() && 1545c3203e7ee1464a096f341c2e2a83a10be2da000aDouglas Gregor !FirstType->isObjCObjectPointerType() && 1546a5e42a82ce055f29f3733f3a1f10da6cb9877deeFariborz Jahanian !FirstType->isBlockPointerType()) 1547a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian return StmtError(Diag(ForLoc, diag::err_selector_element_type) 1548a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian << FirstType << First->getSourceRange()); 15493ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian } 15501093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 1551a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian if (CollectionExprResult.isInvalid()) 1552a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian return StmtError(); 15531093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 15541093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier return Owned(new (Context) ObjCForCollectionStmt(First, 15551093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier CollectionExprResult.take(), 0, 15568189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek ForLoc, RParenLoc)); 15573ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian} 15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1559ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Finish building a variable declaration for a for-range statement. 1560ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// \return true if an error occurs. 1561ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithstatic bool FinishForRangeVarDecl(Sema &SemaRef, VarDecl *Decl, Expr *Init, 1562ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc, int diag) { 1563ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Deduce the type for the iterator variable now rather than leaving it to 1564ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // AddInitializerToDecl, so we can produce a more suitable diagnostic. 1565ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith TypeSourceInfo *InitTSI = 0; 156662b7cfb73e202051e7ab0dad42ba213acd0dec7eSebastian Redl if ((!isa<InitListExpr>(Init) && Init->getType()->isVoidType()) || 1567b832f6dea893f25b40500a04781286236281cb20Sebastian Redl SemaRef.DeduceAutoType(Decl->getTypeSourceInfo(), Init, InitTSI) == 1568b832f6dea893f25b40500a04781286236281cb20Sebastian Redl Sema::DAR_Failed) 1569ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Loc, diag) << Init->getType(); 1570ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!InitTSI) { 1571ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setInvalidDecl(); 1572ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return true; 1573ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1574ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setTypeSourceInfo(InitTSI); 1575ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl->setType(InitTSI->getType()); 1576ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1577f85e193739c953358c865005855253af4f68a497John McCall // In ARC, infer lifetime. 1578f85e193739c953358c865005855253af4f68a497John McCall // FIXME: ARC may want to turn this into 'const __unsafe_unretained' if 1579f85e193739c953358c865005855253af4f68a497John McCall // we're doing the equivalent of fast iteration. 15801093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier if (SemaRef.getLangOpts().ObjCAutoRefCount && 1581f85e193739c953358c865005855253af4f68a497John McCall SemaRef.inferObjCARCLifetime(Decl)) 1582f85e193739c953358c865005855253af4f68a497John McCall Decl->setInvalidDecl(); 1583f85e193739c953358c865005855253af4f68a497John McCall 1584ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.AddInitializerToDecl(Decl, Init, /*DirectInit=*/false, 1585ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TypeMayContainAuto=*/false); 1586ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.FinalizeDeclaration(Decl); 1587b403d6d746239095a2c7bac958c924d92434e2b4Richard Smith SemaRef.CurContext->addHiddenDecl(Decl); 1588ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return false; 1589ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1590ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1591e1715b66a878bcab315513351e5df68bfc010d2eSam Panzernamespace { 1592e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1593ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// Produce a note indicating which begin/end function was implicitly called 1594e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// by a C++11 for-range statement. This is often not obvious from the code, 1595ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// nor from the diagnostics produced when analysing the implicit expressions 1596ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// required in a for-range statement. 1597ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smithvoid NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E, 1598e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Sema::BeginEndFunction BEF) { 1599ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith CallExpr *CE = dyn_cast<CallExpr>(E); 1600ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!CE) 1601ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return; 1602ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FunctionDecl *D = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); 1603ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!D) 1604ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return; 1605ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation Loc = D->getLocation(); 1606ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1607ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith std::string Description; 1608ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith bool IsTemplate = false; 1609ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FunctionTemplateDecl *FunTmpl = D->getPrimaryTemplate()) { 1610ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Description = SemaRef.getTemplateArgumentBindingsText( 1611ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith FunTmpl->getTemplateParameters(), *D->getTemplateSpecializationArgs()); 1612ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IsTemplate = true; 1613ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1614ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1615ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SemaRef.Diag(Loc, diag::note_for_range_begin_end) 1616ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << BEF << IsTemplate << Description << E->getType(); 1617ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1618ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1619e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// Build a variable declaration for a for-range statement. 1620e1715b66a878bcab315513351e5df68bfc010d2eSam PanzerVarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, 1621e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer QualType Type, const char *Name) { 1622e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer DeclContext *DC = SemaRef.CurContext; 1623e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); 1624e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); 1625e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, 1626e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer TInfo, SC_Auto, SC_None); 1627e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Decl->setImplicit(); 1628e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return Decl; 1629ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1630ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1631ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1632ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 16334d3db4eb6caa49a7cdbfe1798728ce4b23cd0b53Fariborz Jahanianstatic bool ObjCEnumerationCollection(Expr *Collection) { 16344d3db4eb6caa49a7cdbfe1798728ce4b23cd0b53Fariborz Jahanian return !Collection->isTypeDependent() 16354d3db4eb6caa49a7cdbfe1798728ce4b23cd0b53Fariborz Jahanian && Collection->getType()->getAs<ObjCObjectPointerType>() != 0; 16364d3db4eb6caa49a7cdbfe1798728ce4b23cd0b53Fariborz Jahanian} 16374d3db4eb6caa49a7cdbfe1798728ce4b23cd0b53Fariborz Jahanian 1638bc20bbb0bf90446a469848c658ca376832f43dc8Sam Panzer/// ActOnCXXForRangeStmt - Check and build a C++11 for-range statement. 1639ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1640bc20bbb0bf90446a469848c658ca376832f43dc8Sam Panzer/// C++11 [stmt.ranged]: 1641ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// A range-based for statement is equivalent to 1642ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1643ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// { 1644ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// auto && __range = range-init; 1645ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// for ( auto __begin = begin-expr, 1646ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// __end = end-expr; 1647ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// __begin != __end; 1648ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// ++__begin ) { 1649ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// for-range-declaration = *__begin; 1650ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// statement 1651ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// } 1652ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// } 1653ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// 1654ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// The body of the loop is not available yet, since it cannot be analysed until 1655ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// we have determined the type of the for-range-declaration. 1656ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult 1657bc20bbb0bf90446a469848c658ca376832f43dc8Sam PanzerSema::ActOnCXXForRangeStmt(SourceLocation ForLoc, 1658ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Stmt *First, SourceLocation ColonLoc, Expr *Range, 1659e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SourceLocation RParenLoc, bool ShouldTryDeref) { 1660ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!First || !Range) 1661ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 16621093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 16634d3db4eb6caa49a7cdbfe1798728ce4b23cd0b53Fariborz Jahanian if (ObjCEnumerationCollection(Range)) 1664bc20bbb0bf90446a469848c658ca376832f43dc8Sam Panzer return ActOnObjCForCollectionStmt(ForLoc, First, Range, RParenLoc); 1665ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1666ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *DS = dyn_cast<DeclStmt>(First); 1667ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith assert(DS && "first part of for range not a decl stmt"); 1668ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1669ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!DS->isSingleDecl()) { 1670ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(DS->getStartLoc(), diag::err_type_defined_in_for_range); 1671ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1672ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1673ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DS->getSingleDecl()->isInvalidDecl()) 1674ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1675ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1676ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DiagnoseUnexpandedParameterPack(Range, UPPC_Expression)) 1677ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1678ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1679ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build auto && __range = range-init 1680ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RangeLoc = Range->getLocStart(); 1681ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc, 1682ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Context.getAutoRRefDeductType(), 1683ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__range"); 1684ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc, 1685ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_deduction_failure)) 1686ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1687ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1688ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Claim the type doesn't contain auto: we've already done the checking. 1689ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclGroupPtrTy RangeGroup = 1690ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BuildDeclaratorGroup((Decl**)&RangeVar, 1, /*TypeMayContainAuto=*/false); 1691ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith StmtResult RangeDecl = ActOnDeclStmt(RangeGroup, RangeLoc, RangeLoc); 1692ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RangeDecl.isInvalid()) 1693ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1694ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1695ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return BuildCXXForRangeStmt(ForLoc, ColonLoc, RangeDecl.get(), 1696ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*BeginEndDecl=*/0, /*Cond=*/0, /*Inc=*/0, DS, 1697e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer RParenLoc, ShouldTryDeref); 1698e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer} 1699e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1700e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// \brief Create the initialization, compare, and increment steps for 1701e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// the range-based for loop expression. 1702e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// This function does not handle array-based for loops, 1703e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// which are created in Sema::BuildCXXForRangeStmt. 1704e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// 1705e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// \returns a ForRangeStatus indicating success or what kind of error occurred. 1706e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// BeginExpr and EndExpr are set and FRS_Success is returned on success; 1707e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// CandidateSet and BEF are set and some non-success value is returned on 1708e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// failure. 1709e1715b66a878bcab315513351e5df68bfc010d2eSam Panzerstatic Sema::ForRangeStatus BuildNonArrayForRange(Sema &SemaRef, Scope *S, 1710e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Expr *BeginRange, Expr *EndRange, 1711e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer QualType RangeType, 1712e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer VarDecl *BeginVar, 1713e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer VarDecl *EndVar, 1714e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SourceLocation ColonLoc, 1715e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer OverloadCandidateSet *CandidateSet, 1716e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer ExprResult *BeginExpr, 1717e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer ExprResult *EndExpr, 1718e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Sema::BeginEndFunction *BEF) { 1719e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer DeclarationNameInfo BeginNameInfo( 1720e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer &SemaRef.PP.getIdentifierTable().get("begin"), ColonLoc); 1721e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer DeclarationNameInfo EndNameInfo(&SemaRef.PP.getIdentifierTable().get("end"), 1722e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer ColonLoc); 1723e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1724e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer LookupResult BeginMemberLookup(SemaRef, BeginNameInfo, 1725e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Sema::LookupMemberName); 1726e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer LookupResult EndMemberLookup(SemaRef, EndNameInfo, Sema::LookupMemberName); 1727e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1728e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (CXXRecordDecl *D = RangeType->getAsCXXRecordDecl()) { 1729e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // - if _RangeT is a class type, the unqualified-ids begin and end are 1730e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // looked up in the scope of class _RangeT as if by class member access 1731e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // lookup (3.4.5), and if either (or both) finds at least one 1732e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // declaration, begin-expr and end-expr are __range.begin() and 1733e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // __range.end(), respectively; 1734e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SemaRef.LookupQualifiedName(BeginMemberLookup, D); 1735e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SemaRef.LookupQualifiedName(EndMemberLookup, D); 1736e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1737e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (BeginMemberLookup.empty() != EndMemberLookup.empty()) { 1738e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SourceLocation RangeLoc = BeginVar->getLocation(); 1739e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer *BEF = BeginMemberLookup.empty() ? Sema::BEF_end : Sema::BEF_begin; 1740e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1741e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SemaRef.Diag(RangeLoc, diag::err_for_range_member_begin_end_mismatch) 1742e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer << RangeLoc << BeginRange->getType() << *BEF; 1743e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return Sema::FRS_DiagnosticIssued; 1744e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer } 1745e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer } else { 1746e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // - otherwise, begin-expr and end-expr are begin(__range) and 1747e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // end(__range), respectively, where begin and end are looked up with 1748e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // argument-dependent lookup (3.4.2). For the purposes of this name 1749e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // lookup, namespace std is an associated namespace. 1750e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1751e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer } 1752e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1753e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer *BEF = Sema::BEF_begin; 1754e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Sema::ForRangeStatus RangeStatus = 1755e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SemaRef.BuildForRangeBeginEndCall(S, ColonLoc, ColonLoc, BeginVar, 1756e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Sema::BEF_begin, BeginNameInfo, 1757e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer BeginMemberLookup, CandidateSet, 1758e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer BeginRange, BeginExpr); 1759e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1760e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (RangeStatus != Sema::FRS_Success) 1761e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return RangeStatus; 1762e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (FinishForRangeVarDecl(SemaRef, BeginVar, BeginExpr->get(), ColonLoc, 1763e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer diag::err_for_range_iter_deduction_failure)) { 1764e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer NoteForRangeBeginEndFunction(SemaRef, BeginExpr->get(), *BEF); 1765e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return Sema::FRS_DiagnosticIssued; 1766e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer } 1767e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1768e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer *BEF = Sema::BEF_end; 1769e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer RangeStatus = 1770e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SemaRef.BuildForRangeBeginEndCall(S, ColonLoc, ColonLoc, EndVar, 1771e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Sema::BEF_end, EndNameInfo, 1772e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer EndMemberLookup, CandidateSet, 1773e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer EndRange, EndExpr); 1774e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (RangeStatus != Sema::FRS_Success) 1775e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return RangeStatus; 1776e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (FinishForRangeVarDecl(SemaRef, EndVar, EndExpr->get(), ColonLoc, 1777e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer diag::err_for_range_iter_deduction_failure)) { 1778e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer NoteForRangeBeginEndFunction(SemaRef, EndExpr->get(), *BEF); 1779e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return Sema::FRS_DiagnosticIssued; 1780e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer } 1781e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return Sema::FRS_Success; 1782e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer} 1783e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1784e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// Speculatively attempt to dereference an invalid range expression. 1785e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// This function will not emit diagnostics, but returns StmtError if 1786e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer/// an error occurs. 1787e1715b66a878bcab315513351e5df68bfc010d2eSam Panzerstatic StmtResult RebuildForRangeWithDereference(Sema &SemaRef, Scope *S, 1788e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SourceLocation ForLoc, 1789e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Stmt *LoopVarDecl, 1790e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SourceLocation ColonLoc, 1791e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Expr *Range, 1792e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SourceLocation RangeLoc, 1793e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SourceLocation RParenLoc) { 1794e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Sema::SFINAETrap Trap(SemaRef); 1795e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer ExprResult AdjustedRange = SemaRef.BuildUnaryOp(S, RangeLoc, UO_Deref, Range); 1796e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer StmtResult SR = 1797e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SemaRef.ActOnCXXForRangeStmt(ForLoc, LoopVarDecl, ColonLoc, 1798e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer AdjustedRange.get(), RParenLoc, false); 1799e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (Trap.hasErrorOccurred()) 1800e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return StmtError(); 1801e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return SR; 1802ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 1803ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1804ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// BuildCXXForRangeStmt - Build or instantiate a C++0x for-range statement. 1805ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult 1806ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithSema::BuildCXXForRangeStmt(SourceLocation ForLoc, SourceLocation ColonLoc, 1807ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Stmt *RangeDecl, Stmt *BeginEnd, Expr *Cond, 1808ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Inc, Stmt *LoopVarDecl, 1809e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer SourceLocation RParenLoc, bool ShouldTryDeref) { 1810ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Scope *S = getCurScope(); 1811ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1812ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *RangeDS = cast<DeclStmt>(RangeDecl); 1813ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *RangeVar = cast<VarDecl>(RangeDS->getSingleDecl()); 1814ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType RangeVarType = RangeVar->getType(); 1815ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1816ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclStmt *LoopVarDS = cast<DeclStmt>(LoopVarDecl); 1817ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *LoopVar = cast<VarDecl>(LoopVarDS->getSingleDecl()); 1818ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1819ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith StmtResult BeginEndDecl = BeginEnd; 1820ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult NotEqExpr = Cond, IncrExpr = Inc; 1821ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1822ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!BeginEndDecl.get() && !RangeVarType->isDependentType()) { 1823ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith SourceLocation RangeLoc = RangeVar->getLocation(); 1824ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1825e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek const QualType RangeVarNonRefType = RangeVarType.getNonReferenceType(); 1826e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1827e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult BeginRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, 1828e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1829e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRangeRef.isInvalid()) 1830e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1831e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1832e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult EndRangeRef = BuildDeclRefExpr(RangeVar, RangeVarNonRefType, 1833e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1834e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (EndRangeRef.isInvalid()) 1835ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1836ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1837ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType AutoType = Context.getAutoDeductType(); 1838ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Expr *Range = RangeVar->getInit(); 1839ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Range) 1840ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1841ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType RangeType = Range->getType(); 1842ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1843ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (RequireCompleteType(RangeLoc, RangeType, 1844d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor diag::err_for_range_incomplete_type)) 1845ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1846ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1847ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build auto __begin = begin-expr, __end = end-expr. 1848ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, 1849ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__begin"); 1850ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, 1851ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith "__end"); 1852ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1853ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build begin-expr and end-expr and attach to __begin and __end variables. 1854ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BeginExpr, EndExpr; 1855ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (const ArrayType *UnqAT = RangeType->getAsArrayTypeUnsafe()) { 1856ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // - if _RangeT is an array type, begin-expr and end-expr are __range and 1857ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // __range + __bound, respectively, where __bound is the array bound. If 1858ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // _RangeT is an array of unknown size or an array of incomplete type, 1859ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // the program is ill-formed; 1860ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1861ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // begin-expr is __range. 1862e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginExpr = BeginRangeRef; 1863e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (FinishForRangeVarDecl(*this, BeginVar, BeginRangeRef.get(), ColonLoc, 1864ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1865ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1866ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1867ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1868ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1869ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Find the array bound. 1870ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult BoundExpr; 1871ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(UnqAT)) 1872ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr = Owned(IntegerLiteral::Create(Context, CAT->getSize(), 18731dd986dff9ddfbec687975700770bb377988e9edRichard Trieu Context.getPointerDiffType(), 18741dd986dff9ddfbec687975700770bb377988e9edRichard Trieu RangeLoc)); 1875ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith else if (const VariableArrayType *VAT = 1876ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith dyn_cast<VariableArrayType>(UnqAT)) 1877ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr = VAT->getSizeExpr(); 1878ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith else { 1879ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Can't be a DependentSizedArrayType or an IncompleteArrayType since 1880ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // UnqAT is not incomplete and Range is not type-dependent. 1881b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Unexpected array type in for-range"); 1882ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1883ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1884ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // end-expr is __range + __bound. 1885e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek EndExpr = ActOnBinOp(S, ColonLoc, tok::plus, EndRangeRef.get(), 1886ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BoundExpr.get()); 1887ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (EndExpr.isInvalid()) 1888ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1889ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (FinishForRangeVarDecl(*this, EndVar, EndExpr.get(), ColonLoc, 1890ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith diag::err_for_range_iter_deduction_failure)) { 1891ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1892ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1893ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1894ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } else { 1895e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer OverloadCandidateSet CandidateSet(RangeLoc); 1896e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Sema::BeginEndFunction BEFFailure; 1897e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer ForRangeStatus RangeStatus = 1898e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer BuildNonArrayForRange(*this, S, BeginRangeRef.get(), 1899e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer EndRangeRef.get(), RangeType, 1900e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer BeginVar, EndVar, ColonLoc, &CandidateSet, 1901e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer &BeginExpr, &EndExpr, &BEFFailure); 1902e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1903e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // If building the range failed, try dereferencing the range expression 1904e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // unless a diagnostic was issued or the end function is problematic. 1905e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (ShouldTryDeref && RangeStatus == FRS_NoViableFunction && 1906e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer BEFFailure == BEF_begin) { 1907e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer StmtResult SR = RebuildForRangeWithDereference(*this, S, ForLoc, 1908e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer LoopVarDecl, ColonLoc, 1909e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Range, RangeLoc, 1910e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer RParenLoc); 1911e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (!SR.isInvalid()) { 1912e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // The attempt to dereference would succeed; return the result of 1913e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // recovery. 1914e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Diag(RangeLoc, diag::err_for_range_dereference) 1915e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer << RangeLoc << RangeType 1916e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer << FixItHint::CreateInsertion(RangeLoc, "*"); 1917e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer return SR; 1918ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1919ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1920ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1921e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // Otherwise, emit diagnostics if we haven't already. 1922e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (RangeStatus == FRS_NoViableFunction) { 1923e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Expr *Range = BEFFailure ? EndRangeRef.get() : BeginRangeRef.get(); 1924e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer Diag(Range->getLocStart(), diag::err_for_range_invalid) 1925e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer << RangeLoc << Range->getType() << BEFFailure; 1926e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer CandidateSet.NoteCandidates(*this, OCD_AllCandidates, 1927e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer llvm::makeArrayRef(&Range, /*NumArgs=*/1)); 1928e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer } 1929e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // Return an error if no fix was discovered. 1930e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer if (RangeStatus != FRS_Success) 1931ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1932ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1933ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1934e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer assert(!BeginExpr.isInvalid() && !EndExpr.isInvalid() && 1935e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer "invalid range expression in for loop"); 1936e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer 1937e1715b66a878bcab315513351e5df68bfc010d2eSam Panzer // C++11 [dcl.spec.auto]p7: BeginType and EndType must be the same. 1938ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith QualType BeginType = BeginVar->getType(), EndType = EndVar->getType(); 1939ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Context.hasSameType(BeginType, EndType)) { 1940ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Diag(RangeLoc, diag::err_for_range_begin_end_types_differ) 1941ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith << BeginType << EndType; 1942ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1943ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1944ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1945ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1946ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith Decl *BeginEndDecls[] = { BeginVar, EndVar }; 1947ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Claim the type doesn't contain auto: we've already done the checking. 1948ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith DeclGroupPtrTy BeginEndGroup = 1949ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BuildDeclaratorGroup(BeginEndDecls, 2, /*TypeMayContainAuto=*/false); 1950ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginEndDecl = ActOnDeclStmt(BeginEndGroup, ColonLoc, ColonLoc); 1951ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1952e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek const QualType BeginRefNonRefType = BeginType.getNonReferenceType(); 1953e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek ExprResult BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1954ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1955e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1956e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1957e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1958ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult EndRef = BuildDeclRefExpr(EndVar, EndType.getNonReferenceType(), 1959ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith VK_LValue, ColonLoc); 1960e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (EndRef.isInvalid()) 1961e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1962ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1963ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check __begin != __end expression. 1964ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnBinOp(S, ColonLoc, tok::exclaimequal, 1965ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith BeginRef.get(), EndRef.get()); 1966ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnBooleanCondition(S, ColonLoc, NotEqExpr.get()); 1967ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr = ActOnFinishFullExpr(NotEqExpr.get()); 1968ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (NotEqExpr.isInvalid()) { 19698123b6e7838216a53ba050f07e36fee28e1bbdafSam Panzer Diag(RangeLoc, diag::note_for_range_invalid_iterator) 19708123b6e7838216a53ba050f07e36fee28e1bbdafSam Panzer << RangeLoc << 0 << BeginRangeRef.get()->getType(); 1971ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1972ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!Context.hasSameType(BeginType, EndType)) 1973ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, EndExpr.get(), BEF_end); 1974ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1975ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1976ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1977ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check ++__begin expression. 1978e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1979e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1980e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1981e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1982e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1983ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IncrExpr = ActOnUnaryOp(S, ColonLoc, tok::plusplus, BeginRef.get()); 1984ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith IncrExpr = ActOnFinishFullExpr(IncrExpr.get()); 1985ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (IncrExpr.isInvalid()) { 19868123b6e7838216a53ba050f07e36fee28e1bbdafSam Panzer Diag(RangeLoc, diag::note_for_range_invalid_iterator) 19878123b6e7838216a53ba050f07e36fee28e1bbdafSam Panzer << RangeLoc << 2 << BeginRangeRef.get()->getType() ; 1988ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 1989ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 1990ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 1991ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 1992ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Build and check *__begin expression. 1993e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek BeginRef = BuildDeclRefExpr(BeginVar, BeginRefNonRefType, 1994e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek VK_LValue, ColonLoc); 1995e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek if (BeginRef.isInvalid()) 1996e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek return StmtError(); 1997e50b01531afcb4afc40d27720afa09613ddcdfa2Ted Kremenek 1998ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ExprResult DerefExpr = ActOnUnaryOp(S, ColonLoc, tok::star, BeginRef.get()); 1999ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (DerefExpr.isInvalid()) { 20008123b6e7838216a53ba050f07e36fee28e1bbdafSam Panzer Diag(RangeLoc, diag::note_for_range_invalid_iterator) 20018123b6e7838216a53ba050f07e36fee28e1bbdafSam Panzer << RangeLoc << 1 << BeginRangeRef.get()->getType(); 2002ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 2003ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 2004ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 2005ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 2006ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith // Attach *__begin as initializer for VD. 2007ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!LoopVar->isInvalidDecl()) { 2008ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith AddInitializerToDecl(LoopVar, DerefExpr.get(), /*DirectInit=*/false, 2009ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith /*TypeMayContainAuto=*/true); 2010ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (LoopVar->isInvalidDecl()) 2011ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NoteForRangeBeginEndFunction(*this, BeginExpr.get(), BEF_begin); 2012ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 2013cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith } else { 2014cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith // The range is implicitly used as a placeholder when it is dependent. 2015cd6f36693ed40cef8c8639c04438d865187c1f73Richard Smith RangeVar->setUsed(); 2016ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith } 2017ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 2018ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return Owned(new (Context) CXXForRangeStmt(RangeDS, 2019ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith cast_or_null<DeclStmt>(BeginEndDecl.get()), 2020ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith NotEqExpr.take(), IncrExpr.take(), 2021ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith LoopVarDS, /*Body=*/0, ForLoc, 2022ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith ColonLoc, RParenLoc)); 2023ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 2024ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 20251093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier/// FinishObjCForCollectionStmt - Attach the body to a objective-C foreach 2026a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian/// statement. 2027a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz JahanianStmtResult Sema::FinishObjCForCollectionStmt(Stmt *S, Stmt *B) { 2028a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian if (!S || !B) 2029a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian return StmtError(); 2030a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian ObjCForCollectionStmt * ForStmt = cast<ObjCForCollectionStmt>(S); 20311093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 2032a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian ForStmt->setBody(B); 2033a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian return S; 2034a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian} 2035a1eec4bd198b96ef40a7c15cd0e131ca94511ad8Fariborz Jahanian 2036ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// FinishCXXForRangeStmt - Attach the body to a C++0x for-range statement. 2037ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// This is a separate step from ActOnCXXForRangeStmt because analysis of the 2038ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// body cannot be performed until after the type of the range variable is 2039ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// determined. 2040ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithStmtResult Sema::FinishCXXForRangeStmt(Stmt *S, Stmt *B) { 2041ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith if (!S || !B) 2042ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return StmtError(); 2043ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 20444d3db4eb6caa49a7cdbfe1798728ce4b23cd0b53Fariborz Jahanian if (isa<ObjCForCollectionStmt>(S)) 20454d3db4eb6caa49a7cdbfe1798728ce4b23cd0b53Fariborz Jahanian return FinishObjCForCollectionStmt(S, B); 20461093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 2047625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko CXXForRangeStmt *ForStmt = cast<CXXForRangeStmt>(S); 2048625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko ForStmt->setBody(B); 2049625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 2050625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnoseEmptyStmtBody(ForStmt->getRParenLoc(), B, 2051625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko diag::warn_empty_range_based_for_body); 2052625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 2053ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith return S; 2054ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith} 2055ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith 205657ad37823e198f977cac605dbfbaefb4daf325e9Chris LattnerStmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc, 205757ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner SourceLocation LabelLoc, 205857ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner LabelDecl *TheDecl) { 205957ad37823e198f977cac605dbfbaefb4daf325e9Chris Lattner getCurFunction()->setHasBranchIntoScope(); 2060ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner TheDecl->setUsed(); 2061ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner return Owned(new (Context) GotoStmt(TheDecl, GotoLoc, LabelLoc)); 20625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 206460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2065ad56d684259f706b7c0ae5ad9c23adb4f2926817Chris LattnerSema::ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, 20669ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Expr *E) { 2067bbf462314b1dc8e422b7c4dd4cac47e566aedf6dEli Friedman // Convert operand to void* 20685f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor if (!E->isTypeDependent()) { 20695f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor QualType ETy = E->getType(); 20702877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth QualType DestTy = Context.getPointerType(Context.VoidTy.withConst()); 2071429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult ExprRes = Owned(E); 20725f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor AssignConvertType ConvTy = 2073429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley CheckSingleAssignmentConstraints(DestTy, ExprRes); 2074429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (ExprRes.isInvalid()) 2075429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 2076429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley E = ExprRes.take(); 20772877998bd8db2fac1c56430a4edcfa0ce138aff9Chandler Carruth if (DiagnoseAssignmentResult(ConvTy, StarLoc, DestTy, ETy, E, AA_Passing)) 20785f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor return StmtError(); 2079d29975fd08713eb9d1777e60536addaa62df8995Eli Friedman E = MaybeCreateExprWithCleanups(E); 20805f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor } 2081b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 2082781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasIndirectGoto(); 2083b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 20845f1b9e689fa5c101512aef99225f2afea1673449Douglas Gregor return Owned(new (Context) IndirectGotoStmt(GotoLoc, StarLoc, E)); 20855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 208760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 20881b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { 20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getContinueParent(); 20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 20915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.2p1: A break shall appear only in or as a loop body. 20924cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(ContinueLoc, diag::err_continue_not_in_loop)); 20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 20944cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 20958189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) ContinueStmt(ContinueLoc)); 20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 209860d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 20991b273c403734d343d720acb28f04011807c8aa56Steve NaroffSema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { 21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Scope *S = CurScope->getBreakParent(); 21015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!S) { 21025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. 21034cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl return StmtError(Diag(BreakLoc, diag::err_break_not_in_loop_or_switch)); 21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 21054cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 21068189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) BreakStmt(BreakLoc)); 21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 21085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2109dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Determine whether the given expression is a candidate for 2110f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// copy elision in either a return statement or a throw expression. 21115077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 2112f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param ReturnType If we're determining the copy elision candidate for 2113f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// a return statement, this is the return type of the function. If we're 2114f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// determining the copy elision candidate for a throw expression, this will 2115f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// be a NULL type. 21165077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 2117f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// \param E The expression being returned from the function or block, or 2118f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor/// being thrown. 21195077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 21204926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// \param AllowFunctionParameter Whether we allow function parameters to 21214926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// be considered NRVO candidates. C++ prohibits this for NRVO itself, but 21224926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// we re-use this logic to determine whether we should try to move as part of 21234926d832aa2f0af9d7c00633727d49e7967eb978Douglas Gregor/// a return or throw (which does allow function parameters). 21245077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// 21255077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// \returns The NRVO candidate variable, if the return statement may use the 21265077c3876beeaed32280af88244e8050078619a8Douglas Gregor/// NRVO, or NULL if there is no such candidate. 2127f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregorconst VarDecl *Sema::getCopyElisionCandidate(QualType ReturnType, 2128f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor Expr *E, 2129f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor bool AllowFunctionParameter) { 2130f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor QualType ExprType = E->getType(); 21313c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor // - in a return statement in a function with ... 21323c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor // ... a class return type ... 2133f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!ReturnType.isNull()) { 2134f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!ReturnType->isRecordType()) 2135f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor return 0; 2136f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor // ... the same cv-unqualified type as the function return type ... 2137f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor if (!Context.hasSameUnqualifiedType(ReturnType, ExprType)) 2138f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor return 0; 2139f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor } 2140dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2141dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // ... the expression is the name of a non-volatile automatic object 2142f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor // (other than a function or catch-clause parameter)) ... 2143f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParens()); 21448951067a2bc35fb2a535bc18432cb2d02a762b73Nico Weber if (!DR || DR->refersToEnclosingLocal()) 21455077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 21463c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); 21473c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor if (!VD) 21485077c3876beeaed32280af88244e8050078619a8Douglas Gregor return 0; 2149dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 21501cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...object (other than a function or catch-clause parameter)... 21511cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getKind() != Decl::Var && 21521cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall !(AllowFunctionParameter && VD->getKind() == Decl::ParmVar)) 21531cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return 0; 21541cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->isExceptionVariable()) return 0; 21551cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 21561cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...automatic... 21571cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (!VD->hasLocalStorage()) return 0; 21581cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 21591cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // ...non-volatile... 21601cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getType().isVolatileQualified()) return 0; 21611cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->getType()->isReferenceType()) return 0; 21621cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 21631cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // __block variables can't be allocated in a way that permits NRVO. 21641cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->hasAttr<BlocksAttr>()) return 0; 21651cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall 21661cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // Variables with higher required alignment than their type's ABI 21671cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall // alignment cannot use NRVO. 21681cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall if (VD->hasAttr<AlignedAttr>() && 21691cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall Context.getDeclAlign(VD) > Context.getTypeAlignInChars(VD->getType())) 21701cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return 0; 2171dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 21721cd76e8ca8f890a4defadcae3372c025ebe7607cJohn McCall return VD; 21733c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor} 21743c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor 217507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// \brief Perform the initialization of a potentially-movable value, which 217607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor/// is the result of return value. 2177cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// 2178cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// This routine implements C++0x [class.copy]p33, which attempts to treat 2179cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// returned lvalues as rvalues in certain cases (to prefer move construction), 2180cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor/// then falls back to treating them as lvalues if that failed. 2181dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiExprResult 218207f402cff25354c5f06f307f19b0c57c09d964bdDouglas GregorSema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity, 218307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor const VarDecl *NRVOCandidate, 218407f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor QualType ResultType, 2185bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor Expr *Value, 2186bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor bool AllowNRVO) { 2187cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // C++0x [class.copy]p33: 2188dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // When the criteria for elision of a copy operation are met or would 2189dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // be met save for the fact that the source object is a function 2190dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // parameter, and the object to be copied is designated by an lvalue, 2191cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // overload resolution to select the constructor for the copy is first 2192cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // performed as if the object were designated by an rvalue. 2193cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor ExprResult Res = ExprError(); 2194bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor if (AllowNRVO && 2195bca01b46850f867b2f4137f25c882022b58f8471Douglas Gregor (NRVOCandidate || getCopyElisionCandidate(ResultType, Value, true))) { 2196dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ImplicitCastExpr AsRvalue(ImplicitCastExpr::OnStack, 2197dbbecccb8431bb4545fc01c6401abc4253667360Richard Smith Value->getType(), CK_NoOp, Value, VK_XValue); 2198dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2199cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor Expr *InitExpr = &AsRvalue; 2200dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializationKind Kind 220107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor = InitializationKind::CreateCopy(Value->getLocStart(), 220207f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value->getLocStart()); 220307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor InitializationSequence Seq(*this, Entity, Kind, &InitExpr, 1); 2204dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2205dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // [...] If overload resolution fails, or if the type of the first 2206cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // parameter of the selected constructor is not an rvalue reference 22070099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi // to the object's type (possibly cv-qualified), overload resolution 2208cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // is performed again, considering the object as an lvalue. 2209383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl if (Seq) { 2210cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor for (InitializationSequence::step_iterator Step = Seq.step_begin(), 2211cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor StepEnd = Seq.step_end(); 2212cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor Step != StepEnd; ++Step) { 2213383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl if (Step->Kind != InitializationSequence::SK_ConstructorInitialization) 2214cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor continue; 2215dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2216dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi CXXConstructorDecl *Constructor 2217cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor = cast<CXXConstructorDecl>(Step->Function.Function); 2218dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2219cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor const RValueReferenceType *RRefType 222007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor = Constructor->getParamDecl(0)->getType() 222107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor ->getAs<RValueReferenceType>(); 2222dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2223cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // If we don't meet the criteria, break out now. 2224dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (!RRefType || 222507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor !Context.hasSameUnqualifiedType(RRefType->getPointeeType(), 222607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Context.getTypeDeclType(Constructor->getParent()))) 2227cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor break; 2228dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2229cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Promote "AsRvalue" to the heap, since we now need this 2230cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // expression node to persist. 223107f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Value = ImplicitCastExpr::Create(Context, Value->getType(), 2232dbbecccb8431bb4545fc01c6401abc4253667360Richard Smith CK_NoOp, Value, 0, VK_XValue); 2233dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2234cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Complete type-checking the initialization of the return type 2235cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // using the constructor we found. 223607f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Res = Seq.Perform(*this, Entity, Kind, MultiExprArg(&Value, 1)); 2237cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 2238cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 2239cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor } 2240dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2241cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // Either we didn't meet the criteria for treating an lvalue as an rvalue, 2242dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // above, or overload resolution failed. Either way, we need to try 2243cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor // (again) now with the return value expression as written. 2244cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor if (Res.isInvalid()) 224507f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor Res = PerformCopyInitialization(Entity, SourceLocation(), Value); 2246dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2247cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor return Res; 2248cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor} 2249cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor 225084b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman/// ActOnCapScopeReturnStmt - Utility routine to type-check return statements 225184b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman/// for capturing scopes. 22524eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff/// 225360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 225484b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli FriedmanSema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 225584b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman // If this is the first return we've seen, infer the return type. 225684b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman // [expr.prim.lambda]p4 in C++11; block literals follow a superset of those 225784b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman // rules which allows multiple return statements. 225884b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman CapturingScopeInfo *CurCap = cast<CapturingScopeInfo>(getCurFunction()); 22597dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose QualType FnRetType = CurCap->ReturnType; 22607dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 22617dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // For blocks/lambdas with implicit return types, we check each return 22627dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // statement individually, and deduce the common return type when the block 22637dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // or lambda is completed. 226484b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman if (CurCap->HasImplicitReturnType) { 2265a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor if (RetValExp && !isa<InitListExpr>(RetValExp)) { 2266429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp); 2267429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2268429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 2269429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley RetValExp = Result.take(); 22706a576ab708d3aa7d40e5d867ab1de5d3cb507553Douglas Gregor 22717dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose if (!RetValExp->isTypeDependent()) 22727dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose FnRetType = RetValExp->getType(); 22737dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose else 22747dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose FnRetType = CurCap->ReturnType = Context.DependentTy; 22751093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier } else { 2276a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor if (RetValExp) { 2277a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor // C++11 [expr.lambda.prim]p4 bans inferring the result from an 2278a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor // initializer list, because it is not an expression (even 2279a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor // though we represent it as one). We still deduce 'void'. 2280a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor Diag(ReturnLoc, diag::err_lambda_return_init_list) 2281a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor << RetValExp->getSourceRange(); 2282a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor } 2283a0c2b21e0d84ad289781e08e14148da6b8b8b76dDouglas Gregor 22847dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose FnRetType = Context.VoidTy; 2285649657e7d6c150136cae5ab22e39b9794cff80ccFariborz Jahanian } 22867dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 22877dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // Although we'll properly infer the type of the block once it's completed, 22887dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // make sure we provide a return type now for better error recovery. 22897dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose if (CurCap->ReturnType.isNull()) 22907dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose CurCap->ReturnType = FnRetType; 22914eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 229284b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman assert(!FnRetType.isNull()); 22934cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 2294793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor if (BlockScopeInfo *CurBlock = dyn_cast<BlockScopeInfo>(CurCap)) { 229584b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman if (CurBlock->FunctionType->getAs<FunctionType>()->getNoReturnAttr()) { 229684b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman Diag(ReturnLoc, diag::err_noreturn_block_has_return_expr); 229784b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman return StmtError(); 229884b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman } 2299793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor } else { 2300793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CurCap); 2301793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor if (LSI->CallOperator->getType()->getAs<FunctionType>()->getNoReturnAttr()){ 2302793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor Diag(ReturnLoc, diag::err_noreturn_lambda_has_return_expr); 2303793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor return StmtError(); 2304793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor } 2305793cd1c4cdfaafc52e2c2ad9dae959befe4bb166Douglas Gregor } 23066c92fa75e62937f9738696840efcb258560f4568Mike Stump 23074eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // Otherwise, verify that this result type matches the previous one. We are 23084eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // pickier with blocks than for normal functions because we don't have GCC 23094eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff // compatibility to worry about here. 2310d963c37eb912342c8325048749e449861cf0a6e3John McCall const VarDecl *NRVOCandidate = 0; 23110a7efe1142d241678c91bf93ee6adb51289863a4John McCall if (FnRetType->isDependentType()) { 23120a7efe1142d241678c91bf93ee6adb51289863a4John McCall // Delay processing for now. TODO: there are lots of dependent 23130a7efe1142d241678c91bf93ee6adb51289863a4John McCall // types we can conclusively prove aren't void. 23140a7efe1142d241678c91bf93ee6adb51289863a4John McCall } else if (FnRetType->isVoidType()) { 23155b38a0f98e4420dae1bd3e13959bc207c97a9e98Sebastian Redl if (RetValExp && !isa<InitListExpr>(RetValExp) && 23164e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !(getLangOpts().CPlusPlus && 23170a7efe1142d241678c91bf93ee6adb51289863a4John McCall (RetValExp->isTypeDependent() || 23180a7efe1142d241678c91bf93ee6adb51289863a4John McCall RetValExp->getType()->isVoidType()))) { 23194e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian if (!getLangOpts().CPlusPlus && 23204e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian RetValExp->getType()->isVoidType()) 23219354f6aaa70e1543d122644fee0c3f834324d2fcFariborz Jahanian Diag(ReturnLoc, diag::ext_return_has_void_expr) << "literal" << 2; 23224e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian else { 23234e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian Diag(ReturnLoc, diag::err_return_block_has_expr); 23244e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian RetValExp = 0; 23254e648e4770d85febaf15ad8b7bad458bd7338ae2Fariborz Jahanian } 23264eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff } 23275077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else if (!RetValExp) { 23280a7efe1142d241678c91bf93ee6adb51289863a4John McCall return StmtError(Diag(ReturnLoc, diag::err_block_return_missing_expr)); 23290a7efe1142d241678c91bf93ee6adb51289863a4John McCall } else if (!RetValExp->isTypeDependent()) { 23300a7efe1142d241678c91bf93ee6adb51289863a4John McCall // we have a non-void block with an expression, continue checking 23310a7efe1142d241678c91bf93ee6adb51289863a4John McCall 23320a7efe1142d241678c91bf93ee6adb51289863a4John McCall // C99 6.8.6.4p3(136): The return statement is not an assignment. The 23330a7efe1142d241678c91bf93ee6adb51289863a4John McCall // overlap restriction of subclause 6.5.16.1 does not apply to the case of 23340a7efe1142d241678c91bf93ee6adb51289863a4John McCall // function return. 23350a7efe1142d241678c91bf93ee6adb51289863a4John McCall 23360a7efe1142d241678c91bf93ee6adb51289863a4John McCall // In C++ the return statement is handled via a copy initialization. 23370a7efe1142d241678c91bf93ee6adb51289863a4John McCall // the C version of which boils down to CheckSingleAssignmentConstraints. 23380a7efe1142d241678c91bf93ee6adb51289863a4John McCall NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); 23390a7efe1142d241678c91bf93ee6adb51289863a4John McCall InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, 23400a7efe1142d241678c91bf93ee6adb51289863a4John McCall FnRetType, 23410586520acb2f368c874943353a222be7f00c3068Fariborz Jahanian NRVOCandidate != 0); 23420a7efe1142d241678c91bf93ee6adb51289863a4John McCall ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, 23430a7efe1142d241678c91bf93ee6adb51289863a4John McCall FnRetType, RetValExp); 23440a7efe1142d241678c91bf93ee6adb51289863a4John McCall if (Res.isInvalid()) { 23450a7efe1142d241678c91bf93ee6adb51289863a4John McCall // FIXME: Cleanup temporaries here, anyway? 23460a7efe1142d241678c91bf93ee6adb51289863a4John McCall return StmtError(); 2347c6acbc58a7aef0a3382775424c80e9534b54b2edAnders Carlsson } 23480a7efe1142d241678c91bf93ee6adb51289863a4John McCall RetValExp = Res.take(); 23490a7efe1142d241678c91bf93ee6adb51289863a4John McCall CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 2350d963c37eb912342c8325048749e449861cf0a6e3John McCall } 2351dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2352d963c37eb912342c8325048749e449861cf0a6e3John McCall if (RetValExp) { 2353d963c37eb912342c8325048749e449861cf0a6e3John McCall CheckImplicitConversions(RetValExp, ReturnLoc); 2354d963c37eb912342c8325048749e449861cf0a6e3John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 235598eb8a7a702b95183ed015706b1f1c66f5cb27a4Mike Stump } 23560a7efe1142d241678c91bf93ee6adb51289863a4John McCall ReturnStmt *Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 23570a7efe1142d241678c91bf93ee6adb51289863a4John McCall NRVOCandidate); 23584cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 23597dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // If we need to check for the named return value optimization, 23607dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // or if we need to infer the return type, 23617dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // save the return statement in our scope for later processing. 23627dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose if (CurCap->HasImplicitReturnType || 23637dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose (getLangOpts().CPlusPlus && FnRetType->isRecordType() && 23647dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose !CurContext->isDependentContext())) 23655077c3876beeaed32280af88244e8050078619a8Douglas Gregor FunctionScopes.back()->Returns.push_back(Result); 2366dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 23675077c3876beeaed32280af88244e8050078619a8Douglas Gregor return Owned(Result); 23684eb206bebcdab28ababe8df55c6185cec2cdc071Steve Naroff} 23695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 237060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 23719ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { 2372fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor // Check for unexpanded parameter packs. 2373fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor if (RetValExp && DiagnoseUnexpandedParameterPack(RetValExp)) 2374fc92137eee708b632c00a9b0934ff87aeae234a5Douglas Gregor return StmtError(); 23751093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier 237684b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman if (isa<CapturingScopeInfo>(getCurFunction())) 237784b007fae6c0cd30fa07074d34fbe2bf61fa44f9Eli Friedman return ActOnCapScopeReturnStmt(ReturnLoc, RetValExp); 23784cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 2379371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner QualType FnRetType; 238038ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman QualType RelatedRetType; 2381f7c41dab1a8de29b0991e853b8822bb0d1ddc01cMike Stump if (const FunctionDecl *FD = getCurFunctionDecl()) { 2382371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner FnRetType = FD->getResultType(); 238304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall if (FD->hasAttr<NoReturnAttr>() || 238404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall FD->getType()->getAs<FunctionType>()->getNoReturnAttr()) 23858662587fa75d3fb04f873e265841c9314c7f5523Chris Lattner Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr) 238679430e9983f5e67a378fc1f50cd6278f2cea8259Eli Friedman << FD->getDeclName(); 2387926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else if (ObjCMethodDecl *MD = getCurMethodDecl()) { 238838ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman FnRetType = MD->getResultType(); 2389926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (MD->hasRelatedResultType() && MD->getClassInterface()) { 2390926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // In the implementation of a method with a related return type, the 23911093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier // type used to type-check the validity of return statements within the 2392926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // method body is a pointer to the type of the class being implemented. 239338ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman RelatedRetType = Context.getObjCInterfaceType(MD->getClassInterface()); 239438ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman RelatedRetType = Context.getObjCObjectPointerType(RelatedRetType); 2395926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } 2396926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else // If we don't have a function/method context, bail. 2397c97fb9a394ce2cc5e664fcb472e93553528378adSteve Naroff return StmtError(); 23981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23995077c3876beeaed32280af88244e8050078619a8Douglas Gregor ReturnStmt *Result = 0; 24005cf216b7fa64b933b60743b0b26053e8e7aa87beChris Lattner if (FnRetType->isVoidType()) { 24018d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (RetValExp) { 240233deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl if (isa<InitListExpr>(RetValExp)) { 240333deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl // We simply never allow init lists as the return value of void 240433deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl // functions. This is compatible because this was never allowed before, 240533deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl // so there's no legacy code to deal with. 240633deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); 240733deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl int FunctionKind = 0; 240833deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl if (isa<ObjCMethodDecl>(CurDecl)) 240933deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl FunctionKind = 1; 241033deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl else if (isa<CXXConstructorDecl>(CurDecl)) 241133deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl FunctionKind = 2; 241233deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl else if (isa<CXXDestructorDecl>(CurDecl)) 241333deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl FunctionKind = 3; 241433deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl 241533deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl Diag(ReturnLoc, diag::err_return_init_list) 241633deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl << CurDecl->getDeclName() << FunctionKind 241733deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl << RetValExp->getSourceRange(); 241833deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl 241933deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl // Drop the expression. 242033deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl RetValExp = 0; 242133deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl } else if (!RetValExp->isTypeDependent()) { 24228d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky // C99 6.8.6.4p1 (ext_ since GCC warns) 24238d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky unsigned D = diag::ext_return_has_expr; 24248d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (RetValExp->getType()->isVoidType()) 24258d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky D = diag::ext_return_has_void_expr; 24268d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky else { 24278d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky ExprResult Result = Owned(RetValExp); 24288d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Result = IgnoredValueConversions(Result.take()); 24298d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (Result.isInvalid()) 24308d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky return StmtError(); 24318d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky RetValExp = Result.take(); 24328d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky RetValExp = ImpCastExprToType(RetValExp, 24338d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Context.VoidTy, CK_ToVoid).take(); 24348d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky } 24354cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 24368d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky // return (some void expression); is legal in C++. 24378d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky if (D != diag::ext_return_has_void_expr || 24384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !getLangOpts().CPlusPlus) { 24398d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky NamedDecl *CurDecl = getCurFunctionOrMethodDecl(); 2440ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth 2441ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth int FunctionKind = 0; 2442ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth if (isa<ObjCMethodDecl>(CurDecl)) 2443ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 1; 2444ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth else if (isa<CXXConstructorDecl>(CurDecl)) 2445ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 2; 2446ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth else if (isa<CXXDestructorDecl>(CurDecl)) 2447ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth FunctionKind = 3; 2448ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth 24498d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky Diag(ReturnLoc, D) 2450ca0d0d4a0d6ecd256d4bf8c1a0dc183a83119833Chandler Carruth << CurDecl->getDeclName() << FunctionKind 24518d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky << RetValExp->getSourceRange(); 24528d7946151cd15c0e7c34250c122d59b2f5027999Nick Lewycky } 2453e878eb035b343d7d819c092102364ec9849716aeChris Lattner } 24541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 245533deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl if (RetValExp) { 245633deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl CheckImplicitConversions(RetValExp, ReturnLoc); 245733deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl RetValExp = MaybeCreateExprWithCleanups(RetValExp); 245833deb35535aebe81bed0eaf5c14f3032276a086eSebastian Redl } 24595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 2460dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 24615077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, 0); 24625077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else if (!RetValExp && !FnRetType->isDependentType()) { 24633c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner unsigned DiagID = diag::warn_return_missing_expr; // C90 6.6.6.4p4 24643c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner // C99 6.8.6.4p1 (ext_ since GCC warns) 24654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().C99) DiagID = diag::ext_return_missing_expr; 24663c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner 24673c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner if (FunctionDecl *FD = getCurFunctionDecl()) 246808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << FD->getIdentifier() << 0/*fn*/; 24693c73c41cefcfe76f36b7bed72c9f1ec195490951Chris Lattner else 247008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner Diag(ReturnLoc, DiagID) << getCurMethodDecl()->getDeclName() << 1/*meth*/; 24715077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc); 24725077c3876beeaed32280af88244e8050078619a8Douglas Gregor } else { 24735077c3876beeaed32280af88244e8050078619a8Douglas Gregor const VarDecl *NRVOCandidate = 0; 24745077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (!FnRetType->isDependentType() && !RetValExp->isTypeDependent()) { 24755077c3876beeaed32280af88244e8050078619a8Douglas Gregor // we have a non-void function with an expression, continue checking 24765077c3876beeaed32280af88244e8050078619a8Douglas Gregor 247738ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman if (!RelatedRetType.isNull()) { 247838ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman // If we have a related result type, perform an extra conversion here. 247938ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman // FIXME: The diagnostics here don't really describe what is happening. 248038ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman InitializedEntity Entity = 248138ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman InitializedEntity::InitializeTemporary(RelatedRetType); 24828e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 248338ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman ExprResult Res = PerformCopyInitialization(Entity, SourceLocation(), 248438ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman RetValExp); 248538ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman if (Res.isInvalid()) { 248638ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman // FIXME: Cleanup temporaries here, anyway? 248738ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman return StmtError(); 248838ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman } 248938ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman RetValExp = Res.takeAs<Expr>(); 249038ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman } 249138ac243e300798e8cd9fe05888cd97beabfb94e6Eli Friedman 24925077c3876beeaed32280af88244e8050078619a8Douglas Gregor // C99 6.8.6.4p3(136): The return statement is not an assignment. The 24935077c3876beeaed32280af88244e8050078619a8Douglas Gregor // overlap restriction of subclause 6.5.16.1 does not apply to the case of 24945077c3876beeaed32280af88244e8050078619a8Douglas Gregor // function return. 24955077c3876beeaed32280af88244e8050078619a8Douglas Gregor 2496856d3798af7c2f7251e4a295f3da7a09ce4c62abJohn McCall // In C++ the return statement is handled via a copy initialization, 24975077c3876beeaed32280af88244e8050078619a8Douglas Gregor // the C version of which boils down to CheckSingleAssignmentConstraints. 2498f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor NRVOCandidate = getCopyElisionCandidate(FnRetType, RetValExp, false); 2499dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi InitializedEntity Entity = InitializedEntity::InitializeResult(ReturnLoc, 250007f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, 250158f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet NRVOCandidate != 0); 2502dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi ExprResult Res = PerformMoveOrCopyInitialization(Entity, NRVOCandidate, 250307f402cff25354c5f06f307f19b0c57c09d964bdDouglas Gregor FnRetType, RetValExp); 25045077c3876beeaed32280af88244e8050078619a8Douglas Gregor if (Res.isInvalid()) { 25055077c3876beeaed32280af88244e8050078619a8Douglas Gregor // FIXME: Cleanup temporaries here, anyway? 25065077c3876beeaed32280af88244e8050078619a8Douglas Gregor return StmtError(); 25075077c3876beeaed32280af88244e8050078619a8Douglas Gregor } 25084cffe2fd5c23168bc08f0453c684cbd3f79471d3Sebastian Redl 25095077c3876beeaed32280af88244e8050078619a8Douglas Gregor RetValExp = Res.takeAs<Expr>(); 2510dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi if (RetValExp) 25115077c3876beeaed32280af88244e8050078619a8Douglas Gregor CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc); 251266724ea67d7d598b937d86fa66f03f09a1c758f3Douglas Gregor } 2513dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2514b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall if (RetValExp) { 2515b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversions(RetValExp, ReturnLoc); 25164765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall RetValExp = MaybeCreateExprWithCleanups(RetValExp); 2517b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall } 25185077c3876beeaed32280af88244e8050078619a8Douglas Gregor Result = new (Context) ReturnStmt(ReturnLoc, RetValExp, NRVOCandidate); 2519898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor } 2520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 2521dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi // If we need to check for the named return value optimization, save the 25225077c3876beeaed32280af88244e8050078619a8Douglas Gregor // return statement in our scope for later processing. 25234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().CPlusPlus && FnRetType->isRecordType() && 25245077c3876beeaed32280af88244e8050078619a8Douglas Gregor !CurContext->isDependentContext()) 25255077c3876beeaed32280af88244e8050078619a8Douglas Gregor FunctionScopes.back()->Returns.push_back(Result); 25268e1e0543cc3b63e0bc116bae0d2f1e8fc530b436Chad Rosier 25275077c3876beeaed32280af88244e8050078619a8Douglas Gregor return Owned(Result); 25285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 25295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 253060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2531431e90e887c21c0d0d56fc12a2d359df8d53ea66Sebastian RedlSema::ActOnObjCAtCatchStmt(SourceLocation AtLoc, 2532d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall SourceLocation RParen, Decl *Parm, 25339ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *Body) { 2534d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall VarDecl *Var = cast_or_null<VarDecl>(Parm); 2535160b5630aa781ac348303e1ae088d27016637778Douglas Gregor if (Var && Var->isInvalidDecl()) 2536160b5630aa781ac348303e1ae088d27016637778Douglas Gregor return StmtError(); 2537dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 25389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtCatchStmt(AtLoc, RParen, Var, Body)); 25393b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian} 25403b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian 254160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 25429ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, Stmt *Body) { 25439ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtFinallyStmt(AtLoc, Body)); 2544161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian} 2545bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 254660d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2547dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtTryStmt(SourceLocation AtLoc, Stmt *Try, 25489ae2f076ca5ab1feb3ba95629099ec2319833701John McCall MultiStmtArg CatchStmts, Stmt *Finally) { 25494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().ObjCExceptions) 2550da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@try"; 2551da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson 2552781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 25538f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor unsigned NumCatchStmts = CatchStmts.size(); 25549ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(ObjCAtTryStmt::Create(Context, AtLoc, Try, 25555354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer CatchStmts.data(), 25568f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor NumCatchStmts, 25579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Finally)); 2558bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian} 2559bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 2560d1376ee0945a4eba0590ae33d14bade860b08a7dJohn McCallStmtResult Sema::BuildObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw) { 2561d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (Throw) { 2562429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Result = DefaultLvalueConversion(Throw); 2563429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Result.isInvalid()) 2564429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return StmtError(); 25655e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall 2566d1376ee0945a4eba0590ae33d14bade860b08a7dJohn McCall Throw = MaybeCreateExprWithCleanups(Result.take()); 2567d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor QualType ThrowType = Throw->getType(); 2568d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor // Make sure the expression type is an ObjC pointer or "void *". 2569d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (!ThrowType->isDependentType() && 2570d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor !ThrowType->isObjCObjectPointerType()) { 2571d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor const PointerType *PT = ThrowType->getAs<PointerType>(); 2572d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor if (!PT || !PT->getPointeeType()->isVoidType()) 2573d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor return StmtError(Diag(AtLoc, diag::error_objc_throw_expects_object) 2574d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor << Throw->getType() << Throw->getSourceRange()); 2575d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor } 2576d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor } 2577dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi 25789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtThrowStmt(AtLoc, Throw)); 2579d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor} 2580d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor 258160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2582dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiSema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, Expr *Throw, 2583d1377b25a36adfe6604f78cbd3a23a07cf0f29e6Douglas Gregor Scope *CurScope) { 25844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().ObjCExceptions) 2585da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson Diag(AtLoc, diag::err_objc_exceptions_disabled) << "@throw"; 2586da4b7cf09ebfd4e4098b516081fa9dae2f5c99e0Anders Carlsson 25879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall if (!Throw) { 2588e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // @throw without an expression designates a rethrow (which much occur 2589e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff // in the context of an @catch clause). 2590e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff Scope *AtCatchParent = CurScope; 2591e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff while (AtCatchParent && !AtCatchParent->isAtCatchScope()) 2592e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff AtCatchParent = AtCatchParent->getParent(); 2593e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff if (!AtCatchParent) 25944ab2414f297fab1b290e77bfc3b049ccf45eda81Steve Naroff return StmtError(Diag(AtLoc, diag::error_rethrow_used_outside_catch)); 2595dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi } 25969ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return BuildObjCAtThrowStmt(AtLoc, Throw); 259739f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian} 2598bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian 259907524039dce5c820f111a1b3f772b4261f004b4aJohn McCallExprResult 260007524039dce5c820f111a1b3f772b4261f004b4aJohn McCallSema::ActOnObjCAtSynchronizedOperand(SourceLocation atLoc, Expr *operand) { 260107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall ExprResult result = DefaultLvalueConversion(operand); 260207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (result.isInvalid()) 260307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return ExprError(); 260407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall operand = result.take(); 26055e3c67b4bd894a926282d24b4d0cbc0e123c9f4aJohn McCall 2606a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner // Make sure the expression type is an ObjC pointer or "void *". 260707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall QualType type = operand->getType(); 260807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (!type->isDependentType() && 260907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall !type->isObjCObjectPointerType()) { 261007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall const PointerType *pointerType = type->getAs<PointerType>(); 261107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall if (!pointerType || !pointerType->getPointeeType()->isVoidType()) 261207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return Diag(atLoc, diag::error_objc_synchronized_expects_object) 261307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall << type << operand->getSourceRange(); 2614a868a203a18571d091e5d226f5f100d4440f3d94Chris Lattner } 26151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 261607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall // The operand to @synchronized is a full-expression. 261707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall return MaybeCreateExprWithCleanups(operand); 261807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall} 261907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall 262007524039dce5c820f111a1b3f772b4261f004b4aJohn McCallStmtResult 262107524039dce5c820f111a1b3f772b4261f004b4aJohn McCallSema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, Expr *SyncExpr, 262207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall Stmt *SyncBody) { 262307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall // We can't jump into or indirect-jump out of a @synchronized block. 262407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall getCurFunction()->setHasBranchProtectedScope(); 26259ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(new (Context) ObjCAtSynchronizedStmt(AtLoc, SyncExpr, SyncBody)); 2626fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian} 26274b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl 26284b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// ActOnCXXCatchBlock - Takes an exception declaration and a handler block 26294b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl/// and creates a proper catch handler from them. 263060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 2631d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallSema::ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, 26329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall Stmt *HandlerBlock) { 26334b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl // There's nothing to test that ActOnExceptionDecl didn't already test. 26348189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek return Owned(new (Context) CXXCatchStmt(CatchLoc, 2635d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall cast_or_null<VarDecl>(ExDecl), 26369ae2f076ca5ab1feb3ba95629099ec2319833701John McCall HandlerBlock)); 26374b07b2968f87f3cd5a3d8c76145f1cbfd718d42dSebastian Redl} 26388351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 2639f85e193739c953358c865005855253af4f68a497John McCallStmtResult 2640f85e193739c953358c865005855253af4f68a497John McCallSema::ActOnObjCAutoreleasePoolStmt(SourceLocation AtLoc, Stmt *Body) { 2641f85e193739c953358c865005855253af4f68a497John McCall getCurFunction()->setHasBranchProtectedScope(); 2642f85e193739c953358c865005855253af4f68a497John McCall return Owned(new (Context) ObjCAutoreleasePoolStmt(AtLoc, Body)); 2643f85e193739c953358c865005855253af4f68a497John McCall} 2644f85e193739c953358c865005855253af4f68a497John McCall 26453c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace { 26463c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 2647c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlclass TypeWithHandler { 2648c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl QualType t; 2649c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl CXXCatchStmt *stmt; 2650c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redlpublic: 2651c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler(const QualType &type, CXXCatchStmt *statement) 2652c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl : t(type), stmt(statement) {} 2653c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 26540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall // An arbitrary order is fine as long as it places identical 26550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall // types next to each other. 2656c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl bool operator<(const TypeWithHandler &y) const { 26570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (t.getAsOpaquePtr() < y.t.getAsOpaquePtr()) 2658c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return true; 26590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (t.getAsOpaquePtr() > y.t.getAsOpaquePtr()) 2660c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return false; 2661c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl else 2662c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return getTypeSpecStartLoc() < y.getTypeSpecStartLoc(); 2663c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 26641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2665c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl bool operator==(const TypeWithHandler& other) const { 26660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall return t == other.t; 2667c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 26681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2669c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl CXXCatchStmt *getCatchStmt() const { return stmt; } 2670c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl SourceLocation getTypeSpecStartLoc() const { 2671c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return stmt->getExceptionDecl()->getTypeSpecStartLoc(); 2672c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 2673c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl}; 2674c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 26753c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman} 26763c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 26778351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// ActOnCXXTryBlock - Takes a try compound-statement and a number of 26788351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl/// handlers and creates a try statement from them. 267960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult 26809ae2f076ca5ab1feb3ba95629099ec2319833701John McCallSema::ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock, 26818351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl MultiStmtArg RawHandlers) { 2682729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson // Don't report an error if 'try' is used in system headers. 26834e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!getLangOpts().CXXExceptions && 2684729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson !getSourceManager().isInSystemHeader(TryLoc)) 2685729b853f4bfa83e53c638a06a9dccf83b4e1f720Anders Carlsson Diag(TryLoc, diag::err_exceptions_disabled) << "try"; 26867f11d9cf5df1f8ce82af46eabc4ec5cec7d580b0Anders Carlsson 26878351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl unsigned NumHandlers = RawHandlers.size(); 26888351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl assert(NumHandlers > 0 && 26898351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl "The parser shouldn't call this if there are no handlers."); 26905354e77e60e82828c7c2361f5c688c2667ab59ccBenjamin Kramer Stmt **Handlers = RawHandlers.data(); 26918351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 26925f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<TypeWithHandler, 8> TypesWithHandlers; 26931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (unsigned i = 0; i < NumHandlers; ++i) { 26955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner CXXCatchStmt *Handler = cast<CXXCatchStmt>(Handlers[i]); 2696c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (!Handler->getExceptionDecl()) { 2697c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (i < NumHandlers - 1) 2698c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl return StmtError(Diag(Handler->getLocStart(), 2699c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::err_early_catch_all)); 27001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2701c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl continue; 2702c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 27031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2704c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl const QualType CaughtType = Handler->getCaughtType(); 2705c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl const QualType CanonicalCaughtType = Context.getCanonicalType(CaughtType); 2706c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypesWithHandlers.push_back(TypeWithHandler(CanonicalCaughtType, Handler)); 2707c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 2708c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl 2709c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl // Detect handlers for the same type as an earlier one. 2710c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (NumHandlers > 1) { 2711c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl llvm::array_pod_sort(TypesWithHandlers.begin(), TypesWithHandlers.end()); 27121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2713c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler prev = TypesWithHandlers[0]; 2714c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl for (unsigned i = 1; i < TypesWithHandlers.size(); ++i) { 2715c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl TypeWithHandler curr = TypesWithHandlers[i]; 27161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2717c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl if (curr == prev) { 2718c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl Diag(curr.getTypeSpecStartLoc(), 2719c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::warn_exception_caught_by_earlier_handler) 2720c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl << curr.getCatchStmt()->getCaughtType().getAsString(); 2721c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl Diag(prev.getTypeSpecStartLoc(), 2722c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl diag::note_previous_exception_handler) 2723c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl << prev.getCatchStmt()->getCaughtType().getAsString(); 2724c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 27251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2726c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl prev = curr; 2727c447aba04527a71d254b151f79f444d1cbe83ce9Sebastian Redl } 27288351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl } 27291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2730781472fe99a120098c631b0cbe33c89f8cef5e70John McCall getCurFunction()->setHasBranchProtectedScope(); 2731b60a77e453d32db0ab1914d28e175c2defc0eb65John McCall 27328351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // FIXME: We should detect handlers that cannot catch anything because an 27338351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // earlier handler catches a superclass. Need to find a method that is not 27348351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // quadratic for this. 27358351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // Neither of these are explicitly forbidden, but every compiler detects them 27368351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl // and warns. 27378351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl 27389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall return Owned(CXXTryStmt::Create(Context, TryLoc, TryBlock, 2739a1a396df16c02b22983b5c9592022fd9237d4866Sam Weinig Handlers, NumHandlers)); 27408351da06ca3082dfd49dd8e3c1785a986920f57cSebastian Redl} 274128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 274228bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 274328bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHTryBlock(bool IsCXXTry, 274428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley SourceLocation TryLoc, 274528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *TryBlock, 274628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Handler) { 274728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(TryBlock && Handler); 274828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 274928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley getCurFunction()->setHasBranchProtectedScope(); 275028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 275128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHTryStmt::Create(Context,IsCXXTry,TryLoc,TryBlock,Handler)); 275228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 275328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 275428bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 275528bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHExceptBlock(SourceLocation Loc, 275628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Expr *FilterExpr, 275728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Block) { 275828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(FilterExpr && Block); 275928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 276028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley if(!FilterExpr->getType()->isIntegerType()) { 276158f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet return StmtError(Diag(FilterExpr->getExprLoc(), 276258f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet diag::err_filter_expression_integral) 276358f14c012e5d739b09532bb12645dc161f88cfcfFrancois Pichet << FilterExpr->getType()); 276428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley } 276528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 276628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHExceptStmt::Create(Context,Loc,FilterExpr,Block)); 276728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 276828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley 276928bbe4b8acc338476fe0825769b41fb32b423c72John WiegleyStmtResult 277028bbe4b8acc338476fe0825769b41fb32b423c72John WiegleySema::ActOnSEHFinallyBlock(SourceLocation Loc, 277128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley Stmt *Block) { 277228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley assert(Block); 277328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley return Owned(SEHFinallyStmt::Create(Context,Loc,Block)); 277428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley} 2775ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 2776ba0513de93d2fab6db5ab30b6927209fcc883078Douglas GregorStmtResult Sema::BuildMSDependentExistsStmt(SourceLocation KeywordLoc, 2777ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor bool IsIfExists, 2778ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor NestedNameSpecifierLoc QualifierLoc, 2779ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor DeclarationNameInfo NameInfo, 2780ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Stmt *Nested) 2781ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor{ 2782ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor return new (Context) MSDependentExistsStmt(KeywordLoc, IsIfExists, 27831093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier QualifierLoc, NameInfo, 2784ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor cast<CompoundStmt>(Nested)); 2785ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor} 2786ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 2787ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor 27881093f4928a4263d08b44d96e468a42515d8a28f3Chad RosierStmtResult Sema::ActOnMSDependentExistsStmt(SourceLocation KeywordLoc, 2789ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor bool IsIfExists, 27901093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier CXXScopeSpec &SS, 2791ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor UnqualifiedId &Name, 2792ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Stmt *Nested) { 27931093f4928a4263d08b44d96e468a42515d8a28f3Chad Rosier return BuildMSDependentExistsStmt(KeywordLoc, IsIfExists, 2794ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor SS.getWithLocInContext(Context), 2795ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor GetNameFromUnqualifiedId(Name), 2796ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor Nested); 2797ba0513de93d2fab6db5ab30b6927209fcc883078Douglas Gregor} 2798