SemaChecking.cpp revision 860a319e62b0e256817a458396d191aa91c0787a
159907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner//===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// 259907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner// 359907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner// The LLVM Compiler Infrastructure 459907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 759907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner// 859907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner//===----------------------------------------------------------------------===// 959907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner// 101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// This file implements extra semantic analysis beyond what is enforced 1159907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner// by the C type system. 1259907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner// 1359907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner//===----------------------------------------------------------------------===// 1459907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner 155f8d604246976a93a73549b07bbc8ee0b2061b50John McCall#include "clang/Sema/Initialization.h" 16e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h" 172d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h" 18276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman#include "clang/Sema/Initialization.h" 19781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/Sema/ScopeInfo.h" 20826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek#include "clang/Analysis/Analyses/FormatString.h" 2159907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner#include "clang/AST/ASTContext.h" 22199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck#include "clang/AST/CharUnits.h" 23384aff8b94bb0d1ad6c5667b90621e5699815bb2John McCall#include "clang/AST/DeclCXX.h" 24c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 25be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie#include "clang/AST/Expr.h" 262324512285caac0332bbbc6e4cab6245d2a370a1Ted Kremenek#include "clang/AST/ExprCXX.h" 277ff22b259d4d4729f701679e3a7f0e242365e07fTed Kremenek#include "clang/AST/ExprObjC.h" 28f85e193739c953358c865005855253af4f68a497John McCall#include "clang/AST/EvaluatedExprVisitor.h" 29f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump#include "clang/AST/DeclObjC.h" 30f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump#include "clang/AST/StmtCXX.h" 31f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump#include "clang/AST/StmtObjC.h" 3259907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner#include "clang/Lex/Preprocessor.h" 33f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump#include "llvm/ADT/BitVector.h" 348fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer#include "llvm/ADT/SmallString.h" 35f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump#include "llvm/ADT/STLExtras.h" 363bfc5f49e0e37e235bb0d33bcbcb36af9d1f84abTom Care#include "llvm/Support/raw_ostream.h" 37691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher#include "clang/Basic/TargetBuiltins.h" 3826a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman#include "clang/Basic/TargetInfo.h" 397da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian#include "clang/Basic/ConvertUTF.h" 40a1f3dba77b7418575c1ff539ffa74ebaa068280cZhongxing Xu#include <limits> 4159907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattnerusing namespace clang; 42781472fe99a120098c631b0cbe33c89f8cef5e70John McCallusing namespace sema; 4359907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner 4460800081361b0ffc114877b8abbc81cb57b4edf6Chris LattnerSourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, 4560800081361b0ffc114877b8abbc81cb57b4edf6Chris Lattner unsigned ByteNo) const { 4608f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), 474e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie PP.getLangOpts(), PP.getTargetInfo()); 4860800081361b0ffc114877b8abbc81cb57b4edf6Chris Lattner} 4960800081361b0ffc114877b8abbc81cb57b4edf6Chris Lattner 508e10f3b9cc1db43645bbc2999eb163af8997d468John McCall/// Checks that a call expression's argument count is the desired number. 518e10f3b9cc1db43645bbc2999eb163af8997d468John McCall/// This is useful when doing custom type-checking. Returns true on error. 528e10f3b9cc1db43645bbc2999eb163af8997d468John McCallstatic bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { 538e10f3b9cc1db43645bbc2999eb163af8997d468John McCall unsigned argCount = call->getNumArgs(); 548e10f3b9cc1db43645bbc2999eb163af8997d468John McCall if (argCount == desiredArgCount) return false; 558e10f3b9cc1db43645bbc2999eb163af8997d468John McCall 568e10f3b9cc1db43645bbc2999eb163af8997d468John McCall if (argCount < desiredArgCount) 578e10f3b9cc1db43645bbc2999eb163af8997d468John McCall return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) 588e10f3b9cc1db43645bbc2999eb163af8997d468John McCall << 0 /*function call*/ << desiredArgCount << argCount 598e10f3b9cc1db43645bbc2999eb163af8997d468John McCall << call->getSourceRange(); 608e10f3b9cc1db43645bbc2999eb163af8997d468John McCall 618e10f3b9cc1db43645bbc2999eb163af8997d468John McCall // Highlight all the excess arguments. 628e10f3b9cc1db43645bbc2999eb163af8997d468John McCall SourceRange range(call->getArg(desiredArgCount)->getLocStart(), 638e10f3b9cc1db43645bbc2999eb163af8997d468John McCall call->getArg(argCount - 1)->getLocEnd()); 648e10f3b9cc1db43645bbc2999eb163af8997d468John McCall 658e10f3b9cc1db43645bbc2999eb163af8997d468John McCall return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) 668e10f3b9cc1db43645bbc2999eb163af8997d468John McCall << 0 /*function call*/ << desiredArgCount << argCount 678e10f3b9cc1db43645bbc2999eb163af8997d468John McCall << call->getArg(1)->getSourceRange(); 688e10f3b9cc1db43645bbc2999eb163af8997d468John McCall} 698e10f3b9cc1db43645bbc2999eb163af8997d468John McCall 70e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge/// Check that the first argument to __builtin_annotation is an integer 71e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge/// and the second argument is a non-wide string literal. 72e59392176c2369df2bdd11b7786cb38abac00198Julien Lerougestatic bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { 73e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge if (checkArgCount(S, TheCall, 2)) 74e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge return true; 75e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge 76e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge // First argument should be an integer. 77e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge Expr *ValArg = TheCall->getArg(0); 78e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge QualType Ty = ValArg->getType(); 79e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge if (!Ty->isIntegerType()) { 80e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) 81e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge << ValArg->getSourceRange(); 82e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge return true; 83e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge } 84e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge 85e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge // Second argument should be a constant string. 86e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); 87e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); 8877f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge if (!Literal || !Literal->isAscii()) { 89e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) 90e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge << StrArg->getSourceRange(); 9177f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge return true; 9277f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge } 93e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge 94e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge TheCall->setType(Ty); 9577f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge return false; 9677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge} 9777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge 9860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 99d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders CarlssonSema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 10060d7b3a319d84d688752be3870615ac0f111fb16John McCall ExprResult TheCallResult(Owned(TheCall)); 1012def48394f6d48bde0dec2b514193c2b533265b5Douglas Gregor 102946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner // Find out if any arguments are required to be integer constant expressions. 103946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner unsigned ICEArguments = 0; 104946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner ASTContext::GetBuiltinTypeError Error; 105946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); 106946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner if (Error != ASTContext::GE_None) 107946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner ICEArguments = 0; // Don't diagnose previously diagnosed errors. 108946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner 109946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner // If any arguments are required to be ICE's, check and diagnose. 110946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { 111946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner // Skip arguments not required to be ICE's. 112946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner if ((ICEArguments & (1 << ArgNo)) == 0) continue; 113946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner 114946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner llvm::APSInt Result; 115946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) 116946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner return true; 117946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner ICEArguments &= ~(1 << ArgNo); 118946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner } 119946928f48c96bddf3cfa126862f5ab69c2d904a0Chris Lattner 120d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson switch (BuiltinID) { 12130ce344307f8a8b00054021307015571f83c7364Chris Lattner case Builtin::BI__builtin___CFStringMakeConstantString: 122925e60d3fa706f31886027c876989af79eb0e0d2Chris Lattner assert(TheCall->getNumArgs() == 1 && 1231b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner "Wrong # arguments to builtin CFStringMakeConstantString"); 124690398188ea5b428f06aa13c7d4ce6eb741ad4f9Chris Lattner if (CheckObjCString(TheCall->getArg(0))) 1250eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl return ExprError(); 126d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson break; 12749ff7a1c8c67d56e62d3b4627463b705c0d5008cTed Kremenek case Builtin::BI__builtin_stdarg_start: 12830ce344307f8a8b00054021307015571f83c7364Chris Lattner case Builtin::BI__builtin_va_start: 1290eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl if (SemaBuiltinVAStart(TheCall)) 1300eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl return ExprError(); 131d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson break; 1321b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner case Builtin::BI__builtin_isgreater: 1331b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner case Builtin::BI__builtin_isgreaterequal: 1341b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner case Builtin::BI__builtin_isless: 1351b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner case Builtin::BI__builtin_islessequal: 1361b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner case Builtin::BI__builtin_islessgreater: 1371b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner case Builtin::BI__builtin_isunordered: 1380eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl if (SemaBuiltinUnorderedCompare(TheCall)) 1390eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl return ExprError(); 140d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson break; 141e771a7ac11fb27f0e734e5de4d858f2c268895e5Benjamin Kramer case Builtin::BI__builtin_fpclassify: 142e771a7ac11fb27f0e734e5de4d858f2c268895e5Benjamin Kramer if (SemaBuiltinFPClassification(TheCall, 6)) 143e771a7ac11fb27f0e734e5de4d858f2c268895e5Benjamin Kramer return ExprError(); 144e771a7ac11fb27f0e734e5de4d858f2c268895e5Benjamin Kramer break; 1459ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman case Builtin::BI__builtin_isfinite: 1469ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman case Builtin::BI__builtin_isinf: 1479ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman case Builtin::BI__builtin_isinf_sign: 1489ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman case Builtin::BI__builtin_isnan: 1499ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman case Builtin::BI__builtin_isnormal: 1503b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramer if (SemaBuiltinFPClassification(TheCall, 1)) 1519ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman return ExprError(); 1529ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman break; 153d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman case Builtin::BI__builtin_shufflevector: 1540eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl return SemaBuiltinShuffleVector(TheCall); 1550eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl // TheCall will be freed by the smart pointer here, but that's fine, since 1560eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl // SemaBuiltinShuffleVector guts it, but then doesn't release it. 1574493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar case Builtin::BI__builtin_prefetch: 1580eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl if (SemaBuiltinPrefetch(TheCall)) 1590eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl return ExprError(); 160d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson break; 161d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar case Builtin::BI__builtin_object_size: 1620eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl if (SemaBuiltinObjectSize(TheCall)) 1630eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl return ExprError(); 164d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson break; 165d875fed28578835de89cd407e9db4be788596d7cEli Friedman case Builtin::BI__builtin_longjmp: 166d875fed28578835de89cd407e9db4be788596d7cEli Friedman if (SemaBuiltinLongjmp(TheCall)) 167d875fed28578835de89cd407e9db4be788596d7cEli Friedman return ExprError(); 168d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson break; 1698e10f3b9cc1db43645bbc2999eb163af8997d468John McCall 1708e10f3b9cc1db43645bbc2999eb163af8997d468John McCall case Builtin::BI__builtin_classify_type: 1718e10f3b9cc1db43645bbc2999eb163af8997d468John McCall if (checkArgCount(*this, TheCall, 1)) return true; 1728e10f3b9cc1db43645bbc2999eb163af8997d468John McCall TheCall->setType(Context.IntTy); 1738e10f3b9cc1db43645bbc2999eb163af8997d468John McCall break; 17475c29a012793292ff4578015a9113bf086156d7fChris Lattner case Builtin::BI__builtin_constant_p: 1758e10f3b9cc1db43645bbc2999eb163af8997d468John McCall if (checkArgCount(*this, TheCall, 1)) return true; 1768e10f3b9cc1db43645bbc2999eb163af8997d468John McCall TheCall->setType(Context.IntTy); 17775c29a012793292ff4578015a9113bf086156d7fChris Lattner break; 1785caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_add: 179a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_1: 180a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_2: 181a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_4: 182a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_8: 183a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_16: 1845caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_sub: 185a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_1: 186a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_2: 187a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_4: 188a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_8: 189a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_16: 1905caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_or: 191a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_1: 192a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_2: 193a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_4: 194a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_8: 195a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_16: 1965caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_and: 197a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_1: 198a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_2: 199a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_4: 200a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_8: 201a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_16: 2025caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_fetch_and_xor: 203a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_1: 204a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_2: 205a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_4: 206a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_8: 207a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_16: 2085caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_add_and_fetch: 209a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_1: 210a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_2: 211a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_4: 212a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_8: 213a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_16: 2145caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_sub_and_fetch: 215a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_1: 216a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_2: 217a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_4: 218a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_8: 219a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_16: 2205caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_and_and_fetch: 221a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_1: 222a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_2: 223a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_4: 224a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_8: 225a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_16: 2265caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_or_and_fetch: 227a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_1: 228a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_2: 229a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_4: 230a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_8: 231a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_16: 2325caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_xor_and_fetch: 233a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_1: 234a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_2: 235a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_4: 236a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_8: 237a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_16: 2385caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap: 239a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_1: 240a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_2: 241a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_4: 242a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_8: 243a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_16: 2445caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap: 245a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_1: 246a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_2: 247a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_4: 248a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_8: 249a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_16: 2505caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_test_and_set: 251a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_1: 252a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_2: 253a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_4: 254a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_8: 255a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_16: 2565caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release: 257a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_1: 258a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_2: 259a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_4: 260a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_8: 261a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_16: 26223aa9c8ca0bc441aab2a38a4c9fc7a1c9e9ac16aChris Lattner case Builtin::BI__sync_swap: 263a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_1: 264a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_2: 265a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_4: 266a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_8: 267a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_16: 268d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth return SemaBuiltinAtomicOverloaded(move(TheCallResult)); 269ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith#define BUILTIN(ID, TYPE, ATTRS) 270ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 271ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case Builtin::BI##ID: \ 272ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::AO##ID); 273ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith#include "clang/Basic/Builtins.def" 27477f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge case Builtin::BI__builtin_annotation: 275e59392176c2369df2bdd11b7786cb38abac00198Julien Lerouge if (SemaBuiltinAnnotation(*this, TheCall)) 27677f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge return ExprError(); 27777f68bb90af93b95045fb994e7cd68137adcc132Julien Lerouge break; 27826a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman } 27926a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman 28026a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman // Since the target specific builtins for each arch overlap, only check those 28126a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman // of the arch we are compiling for. 28226a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman if (BuiltinID >= Builtin::FirstTSBuiltin) { 283bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor switch (Context.getTargetInfo().getTriple().getArch()) { 28426a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman case llvm::Triple::arm: 28526a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman case llvm::Triple::thumb: 28626a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) 28726a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman return ExprError(); 28826a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman break; 28926a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman default: 29026a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman break; 29126a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman } 29226a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman } 29326a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman 29426a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman return move(TheCallResult); 29526a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman} 29626a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman 29761eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman// Get the valid immediate range for the specified NEON type code. 29861eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begemanstatic unsigned RFT(unsigned t, bool shift = false) { 299da95f73b59f9af964e33725c515139d34c90c863Bob Wilson NeonTypeFlags Type(t); 300da95f73b59f9af964e33725c515139d34c90c863Bob Wilson int IsQuad = Type.isQuad(); 301da95f73b59f9af964e33725c515139d34c90c863Bob Wilson switch (Type.getEltType()) { 302da95f73b59f9af964e33725c515139d34c90c863Bob Wilson case NeonTypeFlags::Int8: 303da95f73b59f9af964e33725c515139d34c90c863Bob Wilson case NeonTypeFlags::Poly8: 304da95f73b59f9af964e33725c515139d34c90c863Bob Wilson return shift ? 7 : (8 << IsQuad) - 1; 305da95f73b59f9af964e33725c515139d34c90c863Bob Wilson case NeonTypeFlags::Int16: 306da95f73b59f9af964e33725c515139d34c90c863Bob Wilson case NeonTypeFlags::Poly16: 307da95f73b59f9af964e33725c515139d34c90c863Bob Wilson return shift ? 15 : (4 << IsQuad) - 1; 308da95f73b59f9af964e33725c515139d34c90c863Bob Wilson case NeonTypeFlags::Int32: 309da95f73b59f9af964e33725c515139d34c90c863Bob Wilson return shift ? 31 : (2 << IsQuad) - 1; 310da95f73b59f9af964e33725c515139d34c90c863Bob Wilson case NeonTypeFlags::Int64: 311da95f73b59f9af964e33725c515139d34c90c863Bob Wilson return shift ? 63 : (1 << IsQuad) - 1; 312da95f73b59f9af964e33725c515139d34c90c863Bob Wilson case NeonTypeFlags::Float16: 313da95f73b59f9af964e33725c515139d34c90c863Bob Wilson assert(!shift && "cannot shift float types!"); 314da95f73b59f9af964e33725c515139d34c90c863Bob Wilson return (4 << IsQuad) - 1; 315da95f73b59f9af964e33725c515139d34c90c863Bob Wilson case NeonTypeFlags::Float32: 316da95f73b59f9af964e33725c515139d34c90c863Bob Wilson assert(!shift && "cannot shift float types!"); 317da95f73b59f9af964e33725c515139d34c90c863Bob Wilson return (2 << IsQuad) - 1; 31861eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman } 3197530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie llvm_unreachable("Invalid NeonTypeFlag!"); 32061eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman} 32161eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman 3226f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson/// getNeonEltType - Return the QualType corresponding to the elements of 3236f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson/// the vector type specified by the NeonTypeFlags. This is used to check 3246f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson/// the pointer arguments for Neon load/store intrinsics. 3256f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilsonstatic QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) { 3266f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson switch (Flags.getEltType()) { 3276f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson case NeonTypeFlags::Int8: 3286f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; 3296f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson case NeonTypeFlags::Int16: 3306f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; 3316f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson case NeonTypeFlags::Int32: 3326f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; 3336f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson case NeonTypeFlags::Int64: 3346f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy; 3356f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson case NeonTypeFlags::Poly8: 3366f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson return Context.SignedCharTy; 3376f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson case NeonTypeFlags::Poly16: 3386f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson return Context.ShortTy; 3396f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson case NeonTypeFlags::Float16: 3406f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson return Context.UnsignedShortTy; 3416f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson case NeonTypeFlags::Float32: 3426f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson return Context.FloatTy; 3436f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson } 3447530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie llvm_unreachable("Invalid NeonTypeFlag!"); 3456f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson} 3466f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson 34726a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begemanbool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 3481c2a88cfaeb11227d3a6bf7204207e0c8cf6de6fNate Begeman llvm::APSInt Result; 3491c2a88cfaeb11227d3a6bf7204207e0c8cf6de6fNate Begeman 3500d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman unsigned mask = 0; 35161eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman unsigned TV = 0; 3524648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson int PtrArgNum = -1; 3536f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson bool HasConstPtr = false; 3541c2a88cfaeb11227d3a6bf7204207e0c8cf6de6fNate Begeman switch (BuiltinID) { 355a23326b5c244a03bf61ebb86db60a777ea26f926Nate Begeman#define GET_NEON_OVERLOAD_CHECK 356a23326b5c244a03bf61ebb86db60a777ea26f926Nate Begeman#include "clang/Basic/arm_neon.inc" 357a23326b5c244a03bf61ebb86db60a777ea26f926Nate Begeman#undef GET_NEON_OVERLOAD_CHECK 3581c2a88cfaeb11227d3a6bf7204207e0c8cf6de6fNate Begeman } 3591c2a88cfaeb11227d3a6bf7204207e0c8cf6de6fNate Begeman 3600d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman // For NEON intrinsics which are overloaded on vector element type, validate 3610d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman // the immediate which specifies which variant to emit. 3626f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson unsigned ImmArg = TheCall->getNumArgs()-1; 3630d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman if (mask) { 3646f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) 3650d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman return true; 3660d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman 367da95f73b59f9af964e33725c515139d34c90c863Bob Wilson TV = Result.getLimitedValue(64); 368da95f73b59f9af964e33725c515139d34c90c863Bob Wilson if ((TV > 63) || (mask & (1 << TV)) == 0) 3690d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) 3706f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson << TheCall->getArg(ImmArg)->getSourceRange(); 3716f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson } 3726f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson 3734648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson if (PtrArgNum >= 0) { 3746f9f03ef370ad48ece0c5d10edda28f39ccadb5dBob Wilson // Check that pointer arguments have the specified type. 3754648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson Expr *Arg = TheCall->getArg(PtrArgNum); 3764648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) 3774648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson Arg = ICE->getSubExpr(); 3784648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); 3794648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson QualType RHSTy = RHS.get()->getType(); 3804648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context); 3814648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson if (HasConstPtr) 3824648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson EltTy = EltTy.withConst(); 3834648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson QualType LHSTy = Context.getPointerType(EltTy); 3844648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson AssignConvertType ConvTy; 3854648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); 3864648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson if (RHS.isInvalid()) 3874648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson return true; 3884648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, 3894648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson RHS.get(), AA_Assigning)) 3904648255cebd2fc7c18293c1f93afd6e9add9ccccBob Wilson return true; 3910d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman } 3921c2a88cfaeb11227d3a6bf7204207e0c8cf6de6fNate Begeman 3930d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman // For NEON intrinsics which take an immediate value as part of the 3940d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman // instruction, range check them here. 39561eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman unsigned i = 0, l = 0, u = 0; 3960d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman switch (BuiltinID) { 3970d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman default: return false; 398bb37f50c2d0d72e1fa5b349d5f6a11fdc5acb86eNate Begeman case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; 399bb37f50c2d0d72e1fa5b349d5f6a11fdc5acb86eNate Begeman case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; 40099c40bb13b523d58d7aeb6446e4f486d6918ca58Nate Begeman case ARM::BI__builtin_arm_vcvtr_f: 40199c40bb13b523d58d7aeb6446e4f486d6918ca58Nate Begeman case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; 402a23326b5c244a03bf61ebb86db60a777ea26f926Nate Begeman#define GET_NEON_IMMEDIATE_CHECK 403a23326b5c244a03bf61ebb86db60a777ea26f926Nate Begeman#include "clang/Basic/arm_neon.inc" 404a23326b5c244a03bf61ebb86db60a777ea26f926Nate Begeman#undef GET_NEON_IMMEDIATE_CHECK 4050d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman }; 4060d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman 40761eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman // Check that the immediate argument is actually a constant. 4080d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman if (SemaBuiltinConstantArg(TheCall, i, Result)) 4090d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman return true; 4100d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman 41161eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman // Range check against the upper/lower values for this isntruction. 4120d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman unsigned Val = Result.getZExtValue(); 41361eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman if (Val < l || Val > (u + l)) 4140d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) 415476d8b863cb65b2b5833235d97315cdb46e6f5aaBenjamin Kramer << l << u+l << TheCall->getArg(i)->getSourceRange(); 4160d15c5321a11a5fee53b17ca8e9e0d72d6192b23Nate Begeman 41799c40bb13b523d58d7aeb6446e4f486d6918ca58Nate Begeman // FIXME: VFP Intrinsics should error if VFP not present. 41826a31428f130b66f61834d1b4d1cf72f590f70b9Nate Begeman return false; 419d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson} 420d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson 421d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson/// CheckFunctionCall - Check a direct function call for various correctness 422d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson/// and safety properties not strictly enforced by the C type system. 423d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlssonbool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { 424d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson // Get the IdentifierInfo* for the called function. 425d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson IdentifierInfo *FnInfo = FDecl->getIdentifier(); 426de45428f923b38d80407dbb9ede0df504256f9f6Daniel Dunbar 427d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson // None of the checks below are needed for functions that don't have 428d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson // simple names (e.g., C++ conversion functions). 429d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson if (!FnInfo) 430d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson return false; 4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 432de45428f923b38d80407dbb9ede0df504256f9f6Daniel Dunbar // FIXME: This mechanism should be abstracted to be less fragile and 433de45428f923b38d80407dbb9ede0df504256f9f6Daniel Dunbar // more efficient. For example, just map function ids to custom 434de45428f923b38d80407dbb9ede0df504256f9f6Daniel Dunbar // handlers. 435de45428f923b38d80407dbb9ede0df504256f9f6Daniel Dunbar 436c82faca0be59b072b38bed375e21bd24f1f72647Ted Kremenek // Printf and scanf checking. 437c82faca0be59b072b38bed375e21bd24f1f72647Ted Kremenek for (specific_attr_iterator<FormatAttr> 438c82faca0be59b072b38bed375e21bd24f1f72647Ted Kremenek i = FDecl->specific_attr_begin<FormatAttr>(), 439c82faca0be59b072b38bed375e21bd24f1f72647Ted Kremenek e = FDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) { 44029c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas CheckFormatArguments(*i, TheCall); 44159907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner } 4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 443c82faca0be59b072b38bed375e21bd24f1f72647Ted Kremenek for (specific_attr_iterator<NonNullAttr> 444c82faca0be59b072b38bed375e21bd24f1f72647Ted Kremenek i = FDecl->specific_attr_begin<NonNullAttr>(), 445c82faca0be59b072b38bed375e21bd24f1f72647Ted Kremenek e = FDecl->specific_attr_end<NonNullAttr>(); i != e; ++i) { 446909a70d8a185f701c20bacc3c76f8f5979cbbc7cNick Lewycky CheckNonNullArguments(*i, TheCall->getArgs(), 447909a70d8a185f701c20bacc3c76f8f5979cbbc7cNick Lewycky TheCall->getCallee()->getLocStart()); 448c82faca0be59b072b38bed375e21bd24f1f72647Ted Kremenek } 4490eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl 4500a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks unsigned CMId = FDecl->getMemoryFunctionKind(); 4510a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks if (CMId == 0) 452d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks return false; 453bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 454d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks // Handle memory setting and copying functions. 4550a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) 456bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek CheckStrlcpycatArguments(TheCall, FnInfo); 457c36bedc90c687caa71748480c60707ea4608b092Anna Zaks else if (CMId == Builtin::BIstrncat) 458c36bedc90c687caa71748480c60707ea4608b092Anna Zaks CheckStrncatArguments(TheCall, FnInfo); 459d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks else 4600a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks CheckMemaccessArguments(TheCall, CMId, FnInfo); 4617ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth 462d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson return false; 46371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson} 46471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 46529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupasbool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, 46629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Expr **Args, unsigned NumArgs) { 46729c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas for (specific_attr_iterator<FormatAttr> 46829c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas i = Method->specific_attr_begin<FormatAttr>(), 46929c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas e = Method->specific_attr_end<FormatAttr>(); i != e ; ++i) { 47029c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas 47129c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas CheckFormatArguments(*i, Args, NumArgs, false, lbrac, 47229c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Method->getSourceRange()); 47329c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas } 47429c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas 47529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas // diagnose nonnull arguments. 47629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas for (specific_attr_iterator<NonNullAttr> 47729c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas i = Method->specific_attr_begin<NonNullAttr>(), 47829c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) { 47929c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas CheckNonNullArguments(*i, Args, lbrac); 48029c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas } 48129c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas 48229c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas return false; 48329c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas} 48429c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas 485d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlssonbool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { 486725165f2846bd37d3aaf863747fa30126992085eFariborz Jahanian const VarDecl *V = dyn_cast<VarDecl>(NDecl); 487725165f2846bd37d3aaf863747fa30126992085eFariborz Jahanian if (!V) 488d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson return false; 4891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 490725165f2846bd37d3aaf863747fa30126992085eFariborz Jahanian QualType Ty = V->getType(); 491725165f2846bd37d3aaf863747fa30126992085eFariborz Jahanian if (!Ty->isBlockPointerType()) 492d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson return false; 4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 49443d1251a471d19ce83aa8ce91c0104addada5addJean-Daniel Dupas // format string checking. 49543d1251a471d19ce83aa8ce91c0104addada5addJean-Daniel Dupas for (specific_attr_iterator<FormatAttr> 49643d1251a471d19ce83aa8ce91c0104addada5addJean-Daniel Dupas i = NDecl->specific_attr_begin<FormatAttr>(), 49743d1251a471d19ce83aa8ce91c0104addada5addJean-Daniel Dupas e = NDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) { 49843d1251a471d19ce83aa8ce91c0104addada5addJean-Daniel Dupas CheckFormatArguments(*i, TheCall); 49943d1251a471d19ce83aa8ce91c0104addada5addJean-Daniel Dupas } 500d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson 501d406bf0e8c17012110a8476d03c6f9a97b56ecf7Anders Carlsson return false; 502725165f2846bd37d3aaf863747fa30126992085eFariborz Jahanian} 503725165f2846bd37d3aaf863747fa30126992085eFariborz Jahanian 504ff34d401ff385ef7173ca612432b4ea717fff690Richard SmithExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, 505ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith AtomicExpr::AtomicOp Op) { 506276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); 507276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 508276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 509ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // All these operations take one of the following forms: 510ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith enum { 511ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // C __c11_atomic_init(A *, C) 512ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Init, 513ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // C __c11_atomic_load(A *, int) 514ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Load, 515ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // void __atomic_load(A *, CP, int) 516ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Copy, 517ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // C __c11_atomic_add(A *, M, int) 518ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Arithmetic, 519ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // C __atomic_exchange_n(A *, CP, int) 520ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Xchg, 521ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // void __atomic_exchange(A *, C *, CP, int) 522ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith GNUXchg, 523ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) 524ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith C11CmpXchg, 525ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) 526ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith GNUCmpXchg 527ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith } Form = Init; 528ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; 529ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; 530ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // where: 531ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // C is an appropriate type, 532ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, 533ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, 534ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // M is C if C is an integer, and ptrdiff_t if C is a pointer, and 535ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // the int parameters are for orderings. 536ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 537ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith assert(AtomicExpr::AO__c11_atomic_init == 0 && 538ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load 539ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith && "need to update code for modified C11 atomics"); 540ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && 541ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Op <= AtomicExpr::AO__c11_atomic_fetch_xor; 542ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith bool IsN = Op == AtomicExpr::AO__atomic_load_n || 543ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Op == AtomicExpr::AO__atomic_store_n || 544ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Op == AtomicExpr::AO__atomic_exchange_n || 545ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Op == AtomicExpr::AO__atomic_compare_exchange_n; 546ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith bool IsAddSub = false; 547ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 548ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith switch (Op) { 549ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_init: 550ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Form = Init; 551ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 552ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 553ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_load: 554ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_load_n: 555ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Form = Load; 556ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 557ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 558ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_store: 559ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_load: 560ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_store: 561ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_store_n: 562ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Form = Copy; 563ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 564ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 565ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_fetch_add: 566ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_fetch_sub: 567ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_fetch_add: 568ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_fetch_sub: 569ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_add_fetch: 570ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_sub_fetch: 571ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith IsAddSub = true; 572ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // Fall through. 573ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_fetch_and: 574ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_fetch_or: 575ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_fetch_xor: 576ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_fetch_and: 577ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_fetch_or: 578ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_fetch_xor: 57951b92401c9f95023a2ef27064fd5a60fd99175f5Richard Smith case AtomicExpr::AO__atomic_fetch_nand: 580ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_and_fetch: 581ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_or_fetch: 582ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_xor_fetch: 58351b92401c9f95023a2ef27064fd5a60fd99175f5Richard Smith case AtomicExpr::AO__atomic_nand_fetch: 584ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Form = Arithmetic; 585ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 586ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 587ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_exchange: 588ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_exchange_n: 589ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Form = Xchg; 590ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 591ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 592ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_exchange: 593ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Form = GNUXchg; 594ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 595ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 596ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 597ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 598ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Form = C11CmpXchg; 599ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 600ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 601ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_compare_exchange: 602ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case AtomicExpr::AO__atomic_compare_exchange_n: 603ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Form = GNUCmpXchg; 604ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 605276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 606276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 607ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // Check we have the right number of arguments. 608ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (TheCall->getNumArgs() < NumArgs[Form]) { 609276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) 610ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith << 0 << NumArgs[Form] << TheCall->getNumArgs() 611276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman << TheCall->getCallee()->getSourceRange(); 612276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman return ExprError(); 613ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith } else if (TheCall->getNumArgs() > NumArgs[Form]) { 614ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), 615276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman diag::err_typecheck_call_too_many_args) 616ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith << 0 << NumArgs[Form] << TheCall->getNumArgs() 617276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman << TheCall->getCallee()->getSourceRange(); 618276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman return ExprError(); 619276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 620276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 621ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // Inspect the first argument of the atomic operation. 622dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman Expr *Ptr = TheCall->getArg(0); 623276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); 624276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); 625276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman if (!pointerType) { 626ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) 627276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman << Ptr->getType() << Ptr->getSourceRange(); 628276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman return ExprError(); 629276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 630276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 631ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // For a __c11 builtin, this should be a pointer to an _Atomic type. 632ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith QualType AtomTy = pointerType->getPointeeType(); // 'A' 633ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith QualType ValType = AtomTy; // 'C' 634ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (IsC11) { 635ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (!AtomTy->isAtomicType()) { 636ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) 637ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith << Ptr->getType() << Ptr->getSourceRange(); 638ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith return ExprError(); 639ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith } 640ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith ValType = AtomTy->getAs<AtomicType>()->getValueType(); 641276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 642276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 643ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // For an arithmetic operation, the implied arithmetic must be well-formed. 644ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (Form == Arithmetic) { 645ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // gcc does not enforce these rules for GNU atomics, but we do so for sanity. 646ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { 647ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) 648ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 649ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith return ExprError(); 650ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith } 651ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (!IsAddSub && !ValType->isIntegerType()) { 652ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) 653ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 654ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith return ExprError(); 655ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith } 656ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { 657ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // For __atomic_*_n operations, the value type must be a scalar integral or 658ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // pointer type which is 1, 2, 4, 8 or 16 bytes in length. 659276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) 660ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 661276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman return ExprError(); 662276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 663276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 664ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) { 665ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // For GNU atomics, require a trivially-copyable type. This is not part of 666ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // the GNU atomics specification, but we enforce it for sanity. 667ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) 668276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman << Ptr->getType() << Ptr->getSourceRange(); 669276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman return ExprError(); 670276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 671276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 672ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // FIXME: For any builtin other than a load, the ValType must not be 673ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // const-qualified. 674ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 675276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman switch (ValType.getObjCLifetime()) { 676276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Qualifiers::OCL_None: 677276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Qualifiers::OCL_ExplicitNone: 678276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman // okay 679276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman break; 680276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 681276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Qualifiers::OCL_Weak: 682276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Qualifiers::OCL_Strong: 683276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Qualifiers::OCL_Autoreleasing: 684ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // FIXME: Can this happen? By this point, ValType should be known 685ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // to be trivially copyable. 686276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) 687276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman << ValType << Ptr->getSourceRange(); 688276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman return ExprError(); 689276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 690276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 691276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman QualType ResultType = ValType; 692ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (Form == Copy || Form == GNUXchg || Form == Init) 693276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman ResultType = Context.VoidTy; 694ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith else if (Form == C11CmpXchg || Form == GNUCmpXchg) 695276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman ResultType = Context.BoolTy; 696276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 697ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // The type of a parameter passed 'by value'. In the GNU atomics, such 698ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // arguments are actually passed as pointers. 699ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith QualType ByValType = ValType; // 'CP' 700ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (!IsC11 && !IsN) 701ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith ByValType = Ptr->getType(); 702ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 703276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman // The first argument --- the pointer --- has a fixed type; we 704276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman // deduce the types of the rest of the arguments accordingly. Walk 705276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman // the remaining arguments, converting them to the deduced value type. 706ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith for (unsigned i = 1; i != NumArgs[Form]; ++i) { 707276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman QualType Ty; 708ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (i < NumVals[Form] + 1) { 709ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith switch (i) { 710ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case 1: 711ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // The second argument is the non-atomic operand. For arithmetic, this 712ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // is always passed by value, and for a compare_exchange it is always 713ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // passed by address. For the rest, GNU uses by-address and C11 uses 714ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // by-value. 715ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith assert(Form != Load); 716ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) 717ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Ty = ValType; 718ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith else if (Form == Copy || Form == Xchg) 719ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Ty = ByValType; 720ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith else if (Form == Arithmetic) 721ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Ty = Context.getPointerDiffType(); 722ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith else 723ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Ty = Context.getPointerType(ValType.getUnqualifiedType()); 724ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 725ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case 2: 726ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // The third argument to compare_exchange / GNU exchange is a 727ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // (pointer to a) desired value. 728ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Ty = ByValType; 729ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 730ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case 3: 731ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // The fourth argument to GNU compare_exchange is a 'weak' flag. 732ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith Ty = Context.BoolTy; 733ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 734ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith } 735276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } else { 736276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman // The order(s) are always converted to int. 737276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman Ty = Context.IntTy; 738276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 739ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith 740276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman InitializedEntity Entity = 741276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman InitializedEntity::InitializeParameter(Context, Ty, false); 742ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith ExprResult Arg = TheCall->getArg(i); 743276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 744276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman if (Arg.isInvalid()) 745276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman return true; 746276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman TheCall->setArg(i, Arg.get()); 747276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 748276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 749ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // Permute the arguments into a 'consistent' order. 750dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman SmallVector<Expr*, 5> SubExprs; 751dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman SubExprs.push_back(Ptr); 752ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith switch (Form) { 753ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case Init: 754ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // Note, AtomicExpr::getVal1() has a special case for this atomic. 7557a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall SubExprs.push_back(TheCall->getArg(1)); // Val1 756ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 757ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case Load: 758ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(1)); // Order 759ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 760ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case Copy: 761ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case Arithmetic: 762ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case Xchg: 763dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman SubExprs.push_back(TheCall->getArg(2)); // Order 764dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman SubExprs.push_back(TheCall->getArg(1)); // Val1 765ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 766ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case GNUXchg: 767ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith // Note, AtomicExpr::getVal2() has a special case for this atomic. 768ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(3)); // Order 769ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(1)); // Val1 770ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(2)); // Val2 771ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 772ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case C11CmpXchg: 773dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman SubExprs.push_back(TheCall->getArg(3)); // Order 774dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman SubExprs.push_back(TheCall->getArg(1)); // Val1 775dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman SubExprs.push_back(TheCall->getArg(4)); // OrderFail 7762ebb98a78471f2039af4321bae657e7daacc2a62David Chisnall SubExprs.push_back(TheCall->getArg(2)); // Val2 777ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 778ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith case GNUCmpXchg: 779ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(4)); // Order 780ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(1)); // Val1 781ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(5)); // OrderFail 782ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(2)); // Val2 783ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith SubExprs.push_back(TheCall->getArg(3)); // Weak 784ff34d401ff385ef7173ca612432b4ea717fff690Richard Smith break; 785276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman } 786dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman 787dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), 788dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman SubExprs.data(), SubExprs.size(), 789dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman ResultType, Op, 790dfa64ba45922e1c28e36341bdf34785fea74659bEli Friedman TheCall->getRParenLoc())); 791276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman} 792276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 793276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman 7945f8d604246976a93a73549b07bbc8ee0b2061b50John McCall/// checkBuiltinArgument - Given a call to a builtin function, perform 7955f8d604246976a93a73549b07bbc8ee0b2061b50John McCall/// normal type-checking on the given argument, updating the call in 7965f8d604246976a93a73549b07bbc8ee0b2061b50John McCall/// place. This is useful when a builtin function requires custom 7975f8d604246976a93a73549b07bbc8ee0b2061b50John McCall/// type-checking for some of its arguments but not necessarily all of 7985f8d604246976a93a73549b07bbc8ee0b2061b50John McCall/// them. 7995f8d604246976a93a73549b07bbc8ee0b2061b50John McCall/// 8005f8d604246976a93a73549b07bbc8ee0b2061b50John McCall/// Returns true on error. 8015f8d604246976a93a73549b07bbc8ee0b2061b50John McCallstatic bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { 8025f8d604246976a93a73549b07bbc8ee0b2061b50John McCall FunctionDecl *Fn = E->getDirectCallee(); 8035f8d604246976a93a73549b07bbc8ee0b2061b50John McCall assert(Fn && "builtin call without direct callee!"); 8045f8d604246976a93a73549b07bbc8ee0b2061b50John McCall 8055f8d604246976a93a73549b07bbc8ee0b2061b50John McCall ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); 8065f8d604246976a93a73549b07bbc8ee0b2061b50John McCall InitializedEntity Entity = 8075f8d604246976a93a73549b07bbc8ee0b2061b50John McCall InitializedEntity::InitializeParameter(S.Context, Param); 8085f8d604246976a93a73549b07bbc8ee0b2061b50John McCall 8095f8d604246976a93a73549b07bbc8ee0b2061b50John McCall ExprResult Arg = E->getArg(0); 8105f8d604246976a93a73549b07bbc8ee0b2061b50John McCall Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); 8115f8d604246976a93a73549b07bbc8ee0b2061b50John McCall if (Arg.isInvalid()) 8125f8d604246976a93a73549b07bbc8ee0b2061b50John McCall return true; 8135f8d604246976a93a73549b07bbc8ee0b2061b50John McCall 8145f8d604246976a93a73549b07bbc8ee0b2061b50John McCall E->setArg(ArgIndex, Arg.take()); 8155f8d604246976a93a73549b07bbc8ee0b2061b50John McCall return false; 8165f8d604246976a93a73549b07bbc8ee0b2061b50John McCall} 8175f8d604246976a93a73549b07bbc8ee0b2061b50John McCall 8185caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner/// SemaBuiltinAtomicOverloaded - We have a call to a function like 8195caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner/// __sync_fetch_and_add, which is an overloaded function based on the pointer 8205caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner/// type of its first argument. The main ActOnCallExpr routines have already 8215caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner/// promoted the types of arguments because all of these calls are prototyped as 8225caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner/// void(...). 8235caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner/// 8245caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner/// This function goes through and does final semantic checking for these 8255caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner/// builtins, 82660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult 82760d7b3a319d84d688752be3870615ac0f111fb16John McCallSema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { 828d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth CallExpr *TheCall = (CallExpr *)TheCallResult.get(); 8295caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 8305caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 8315caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner 8325caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Ensure that we have at least one argument to do type inference from. 833d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth if (TheCall->getNumArgs() < 1) { 834d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) 835d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth << 0 << 1 << TheCall->getNumArgs() 836d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth << TheCall->getCallee()->getSourceRange(); 837d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth return ExprError(); 838d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth } 8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8405caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Inspect the first argument of the atomic builtin. This should always be 8415caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // a pointer type, whose element is an integral scalar or pointer type. 8425caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Because it is a pointer type, we don't have to worry about any implicit 8435caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // casts here. 844d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth // FIXME: We don't allow floating point scalars as input. 8455caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner Expr *FirstArg = TheCall->getArg(0); 8468c382060c9e6668a94f1485dd16f012cda526c5fEli Friedman ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); 8478c382060c9e6668a94f1485dd16f012cda526c5fEli Friedman if (FirstArgResult.isInvalid()) 8488c382060c9e6668a94f1485dd16f012cda526c5fEli Friedman return ExprError(); 8498c382060c9e6668a94f1485dd16f012cda526c5fEli Friedman FirstArg = FirstArgResult.take(); 8508c382060c9e6668a94f1485dd16f012cda526c5fEli Friedman TheCall->setArg(0, FirstArg); 8518c382060c9e6668a94f1485dd16f012cda526c5fEli Friedman 852f85e193739c953358c865005855253af4f68a497John McCall const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); 853f85e193739c953358c865005855253af4f68a497John McCall if (!pointerType) { 854d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) 855d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth << FirstArg->getType() << FirstArg->getSourceRange(); 856d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth return ExprError(); 857d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth } 8581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 859f85e193739c953358c865005855253af4f68a497John McCall QualType ValType = pointerType->getPointeeType(); 860dd5fa7a21ca9db9d620001ca977681a1b724e3feChris Lattner if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 861d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth !ValType->isBlockPointerType()) { 862d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) 863d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth << FirstArg->getType() << FirstArg->getSourceRange(); 864d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth return ExprError(); 865d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth } 8665caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner 867f85e193739c953358c865005855253af4f68a497John McCall switch (ValType.getObjCLifetime()) { 868f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_None: 869f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_ExplicitNone: 870f85e193739c953358c865005855253af4f68a497John McCall // okay 871f85e193739c953358c865005855253af4f68a497John McCall break; 872f85e193739c953358c865005855253af4f68a497John McCall 873f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Weak: 874f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Strong: 875f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Autoreleasing: 876b8b0313e84700b5c6d597b3be4de41c97b7550f1Argyrios Kyrtzidis Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) 877f85e193739c953358c865005855253af4f68a497John McCall << ValType << FirstArg->getSourceRange(); 878f85e193739c953358c865005855253af4f68a497John McCall return ExprError(); 879f85e193739c953358c865005855253af4f68a497John McCall } 880f85e193739c953358c865005855253af4f68a497John McCall 881b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall // Strip any qualifiers off ValType. 882b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall ValType = ValType.getUnqualifiedType(); 883b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall 8848d13d221cf7c1657404c611efaadf3ac19d899b3Chandler Carruth // The majority of builtins return a value, but a few have special return 8858d13d221cf7c1657404c611efaadf3ac19d899b3Chandler Carruth // types, so allow them to override appropriately below. 8868d13d221cf7c1657404c611efaadf3ac19d899b3Chandler Carruth QualType ResultType = ValType; 8878d13d221cf7c1657404c611efaadf3ac19d899b3Chandler Carruth 8885caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // We need to figure out which concrete builtin this maps onto. For example, 8895caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // __sync_fetch_and_add with a 2 byte object turns into 8905caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // __sync_fetch_and_add_2. 8915caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner#define BUILTIN_ROW(x) \ 8925caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ 8935caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner Builtin::BI##x##_8, Builtin::BI##x##_16 } 8941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8955caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner static const unsigned BuiltinIndices[][5] = { 8965caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_fetch_and_add), 8975caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_fetch_and_sub), 8985caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_fetch_and_or), 8995caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_fetch_and_and), 9005caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_fetch_and_xor), 9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9025caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_add_and_fetch), 9035caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_sub_and_fetch), 9045caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_and_and_fetch), 9055caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_or_and_fetch), 9065caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_xor_and_fetch), 9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9085caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_val_compare_and_swap), 9095caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_bool_compare_and_swap), 9105caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner BUILTIN_ROW(__sync_lock_test_and_set), 91123aa9c8ca0bc441aab2a38a4c9fc7a1c9e9ac16aChris Lattner BUILTIN_ROW(__sync_lock_release), 91223aa9c8ca0bc441aab2a38a4c9fc7a1c9e9ac16aChris Lattner BUILTIN_ROW(__sync_swap) 9135caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner }; 9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#undef BUILTIN_ROW 9151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9165caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Determine the index of the size. 9175caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner unsigned SizeIndex; 918199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck switch (Context.getTypeSizeInChars(ValType).getQuantity()) { 9195caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case 1: SizeIndex = 0; break; 9205caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case 2: SizeIndex = 1; break; 9215caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case 4: SizeIndex = 2; break; 9225caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case 8: SizeIndex = 3; break; 9235caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case 16: SizeIndex = 4; break; 9245caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner default: 925d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) 926d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth << FirstArg->getType() << FirstArg->getSourceRange(); 927d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth return ExprError(); 9285caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner } 9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9305caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Each of these builtins has one pointer argument, followed by some number of 9315caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // values (0, 1 or 2) followed by a potentially empty varags list of stuff 9325caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // that we ignore. Find out which row of BuiltinIndices to read from as well 9335caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // as the number of fixed args. 9347814e6d6645d587891293d59ecf6576defcfac92Douglas Gregor unsigned BuiltinID = FDecl->getBuiltinID(); 9355caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner unsigned BuiltinIndex, NumFixed = 1; 9365caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner switch (BuiltinID) { 937b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie default: llvm_unreachable("Unknown overloaded atomic builtin!"); 938a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add: 939a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_1: 940a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_2: 941a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_4: 942a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_8: 943a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_add_16: 944a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 0; 945a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 946a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 947a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub: 948a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_1: 949a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_2: 950a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_4: 951a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_8: 952a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_sub_16: 953a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 1; 954a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 955a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 956a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or: 957a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_1: 958a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_2: 959a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_4: 960a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_8: 961a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_or_16: 962a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 2; 963a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 964a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 965a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and: 966a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_1: 967a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_2: 968a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_4: 969a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_8: 970a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_and_16: 971a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 3; 972a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 973a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 974a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor: 975a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_1: 976a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_2: 977a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_4: 978a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_8: 979a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_fetch_and_xor_16: 980a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 4; 981a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 982a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 983a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch: 984a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_1: 985a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_2: 986a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_4: 987a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_8: 988a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_add_and_fetch_16: 989a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 5; 990a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 991a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 992a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch: 993a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_1: 994a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_2: 995a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_4: 996a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_8: 997a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_sub_and_fetch_16: 998a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 6; 999a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 1000a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 1001a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch: 1002a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_1: 1003a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_2: 1004a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_4: 1005a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_8: 1006a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_and_and_fetch_16: 1007a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 7; 1008a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 1009a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 1010a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch: 1011a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_1: 1012a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_2: 1013a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_4: 1014a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_8: 1015a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_or_and_fetch_16: 1016a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 8; 1017a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 1018a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 1019a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch: 1020a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_1: 1021a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_2: 1022a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_4: 1023a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_8: 1024a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_xor_and_fetch_16: 1025a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 9; 1026a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 10271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10285caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_val_compare_and_swap: 1029a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_1: 1030a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_2: 1031a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_4: 1032a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_8: 1033a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_val_compare_and_swap_16: 10347eff7c4153faf45b19b5919ef409ee5f6794eb25Daniel Dunbar BuiltinIndex = 10; 10355caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner NumFixed = 2; 10365caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner break; 1037a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 10385caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_bool_compare_and_swap: 1039a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_1: 1040a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_2: 1041a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_4: 1042a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_8: 1043a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_bool_compare_and_swap_16: 10447eff7c4153faf45b19b5919ef409ee5f6794eb25Daniel Dunbar BuiltinIndex = 11; 10455caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner NumFixed = 2; 10468d13d221cf7c1657404c611efaadf3ac19d899b3Chandler Carruth ResultType = Context.BoolTy; 10475caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner break; 1048a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 1049a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set: 1050a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_1: 1051a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_2: 1052a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_4: 1053a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_8: 1054a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_test_and_set_16: 1055a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 12; 1056a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 1057a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 10585caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner case Builtin::BI__sync_lock_release: 1059a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_1: 1060a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_2: 1061a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_4: 1062a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_8: 1063a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_lock_release_16: 10647eff7c4153faf45b19b5919ef409ee5f6794eb25Daniel Dunbar BuiltinIndex = 13; 10655caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner NumFixed = 0; 10668d13d221cf7c1657404c611efaadf3ac19d899b3Chandler Carruth ResultType = Context.VoidTy; 10675caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner break; 1068a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor 1069a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap: 1070a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_1: 1071a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_2: 1072a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_4: 1073a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_8: 1074a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor case Builtin::BI__sync_swap_16: 1075a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor BuiltinIndex = 14; 1076a9766411aaaa1446a5da96dc5042de8da05d89bdDouglas Gregor break; 10775caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner } 10781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10795caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Now that we know how many fixed arguments we expect, first check that we 10805caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // have at least that many. 1081d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth if (TheCall->getNumArgs() < 1+NumFixed) { 1082d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) 1083d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth << 0 << 1+NumFixed << TheCall->getNumArgs() 1084d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth << TheCall->getCallee()->getSourceRange(); 1085d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth return ExprError(); 1086d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth } 10871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1088e7ac0a94f1dee3fae9292eb8372962b6d70b3e0dChris Lattner // Get the decl for the concrete builtin from this, we can tell what the 1089e7ac0a94f1dee3fae9292eb8372962b6d70b3e0dChris Lattner // concrete integer type we should convert to is. 1090e7ac0a94f1dee3fae9292eb8372962b6d70b3e0dChris Lattner unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; 1091e7ac0a94f1dee3fae9292eb8372962b6d70b3e0dChris Lattner const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); 1092e7ac0a94f1dee3fae9292eb8372962b6d70b3e0dChris Lattner IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); 10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FunctionDecl *NewBuiltinDecl = 1094e7ac0a94f1dee3fae9292eb8372962b6d70b3e0dChris Lattner cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, 1095e7ac0a94f1dee3fae9292eb8372962b6d70b3e0dChris Lattner TUScope, false, DRE->getLocStart())); 1096d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth 1097f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall // The first argument --- the pointer --- has a fixed type; we 1098f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall // deduce the types of the rest of the arguments accordingly. Walk 1099f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall // the remaining arguments, converting them to the deduced value type. 11005caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner for (unsigned i = 0; i != NumFixed; ++i) { 1101429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult Arg = TheCall->getArg(i+1); 11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11035caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // GCC does an implicit conversion to the pointer or integer ValType. This 11045caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // can fail in some cases (1i -> int**), check for this error case now. 1105b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall // Initialize the argument. 1106b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 1107b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall ValType, /*consume*/ false); 1108b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 1109429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (Arg.isInvalid()) 1110d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth return ExprError(); 11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11125caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Okay, we have something that *can* be converted to the right type. Check 11135caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // to see if there is a potentially weird extension going on here. This can 11145caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // happen when you do an atomic operation on something like an char* and 11155caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // pass in 42. The 42 gets converted to char. This is even more strange 11165caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // for things like 45.123 -> char, etc. 11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // FIXME: Do this check. 1118b45ae256cfd5ef3ab22b4d715159f978d8120d45John McCall TheCall->setArg(i+1, Arg.take()); 11195caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner } 11201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1121bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor ASTContext& Context = this->getASTContext(); 1122bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor 1123bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor // Create a new DeclRefExpr to refer to the new decl. 1124bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor DeclRefExpr* NewDRE = DeclRefExpr::Create( 1125bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor Context, 1126bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor DRE->getQualifierLoc(), 1127e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara SourceLocation(), 1128bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor NewBuiltinDecl, 1129f4b88a45902af1802a1cb42ba48b1c474474f228John McCall /*enclosing*/ false, 1130bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor DRE->getLocation(), 1131bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor NewBuiltinDecl->getType(), 1132bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor DRE->getValueKind()); 11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11345caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // Set the callee in the CallExpr. 11355caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner // FIXME: This leaks the original parens and implicit casts. 1136bbcb7ea8a062a8f1d5cb504e4518a5d4fbab873aDouglas Gregor ExprResult PromotedCall = UsualUnaryConversions(NewDRE); 1137429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (PromotedCall.isInvalid()) 1138429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return ExprError(); 1139429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley TheCall->setCallee(PromotedCall.take()); 11401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1141db4325b098eff5e9e660db19f0148423fb21f27fChandler Carruth // Change the result type of the call to match the original value type. This 1142db4325b098eff5e9e660db19f0148423fb21f27fChandler Carruth // is arbitrary, but the codegen for these builtins ins design to handle it 1143db4325b098eff5e9e660db19f0148423fb21f27fChandler Carruth // gracefully. 11448d13d221cf7c1657404c611efaadf3ac19d899b3Chandler Carruth TheCall->setType(ResultType); 1145d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth 1146d201457cc781f1b13d0f4b1268ff934e6004cbffChandler Carruth return move(TheCallResult); 11475caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner} 11485caa370ea6f70bd3e7e4a9cc3b69ac1a849c8534Chris Lattner 1149690398188ea5b428f06aa13c7d4ce6eb741ad4f9Chris Lattner/// CheckObjCString - Checks that the argument to the builtin 115071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson/// CFString constructor is correct 1151fd942628abfe30e30427875db953222ae99b4325Steve Naroff/// Note: It might also make sense to do the UTF-16 conversion here (would 1152fd942628abfe30e30427875db953222ae99b4325Steve Naroff/// simplify the backend). 1153690398188ea5b428f06aa13c7d4ce6eb741ad4f9Chris Lattnerbool Sema::CheckObjCString(Expr *Arg) { 115456f349400c5932a196509c0480ff6f99a9a0b48fChris Lattner Arg = Arg->IgnoreParenCasts(); 115571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); 115671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 11575cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor if (!Literal || !Literal->isAscii()) { 1158fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) 1159fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner << Arg->getSourceRange(); 11609cdc4d3834f203dcde3ff274b8928e4620a914d5Anders Carlsson return true; 116171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson } 11621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11637da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian if (Literal->containsNonAsciiOrNull()) { 11645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef String = Literal->getString(); 11657da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian unsigned NumBytes = String.size(); 11665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<UTF16, 128> ToBuf(NumBytes); 11677da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian const UTF8 *FromPtr = (UTF8 *)String.data(); 11687da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian UTF16 *ToPtr = &ToBuf[0]; 11697da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian 11707da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 11717da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian &ToPtr, ToPtr + NumBytes, 11727da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian strictConversion); 11737da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian // Check for conversion failure. 11747da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian if (Result != conversionOK) 11757da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian Diag(Arg->getLocStart(), 11767da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian diag::warn_cfstring_truncated) << Arg->getSourceRange(); 11777da71020b97c69b95831bd03d35e8e8404bfdea0Fariborz Jahanian } 11789cdc4d3834f203dcde3ff274b8928e4620a914d5Anders Carlsson return false; 117959907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner} 118059907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner 1181c27c665c88b49dfb212aedc7bab8b9bf67658b9eChris Lattner/// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. 1182c27c665c88b49dfb212aedc7bab8b9bf67658b9eChris Lattner/// Emit an error and return true on failure, return false on success. 1183925e60d3fa706f31886027c876989af79eb0e0d2Chris Lattnerbool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { 1184925e60d3fa706f31886027c876989af79eb0e0d2Chris Lattner Expr *Fn = TheCall->getCallee(); 1185925e60d3fa706f31886027c876989af79eb0e0d2Chris Lattner if (TheCall->getNumArgs() > 2) { 11862c21a073525cdfa68e4439b7af551385dc2796abChris Lattner Diag(TheCall->getArg(2)->getLocStart(), 1187fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner diag::err_typecheck_call_too_many_args) 1188ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher << 0 /*function call*/ << 2 << TheCall->getNumArgs() 1189ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher << Fn->getSourceRange() 11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump << SourceRange(TheCall->getArg(2)->getLocStart(), 1191fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner (*(TheCall->arg_end()-1))->getLocEnd()); 119230ce344307f8a8b00054021307015571f83c7364Chris Lattner return true; 119330ce344307f8a8b00054021307015571f83c7364Chris Lattner } 119456f20ae1010aa71defd7572f660b41288c56cdd1Eli Friedman 119556f20ae1010aa71defd7572f660b41288c56cdd1Eli Friedman if (TheCall->getNumArgs() < 2) { 1196d77b9a29651d748f0e30a8dad8969635fc04f725Eric Christopher return Diag(TheCall->getLocEnd(), 1197d77b9a29651d748f0e30a8dad8969635fc04f725Eric Christopher diag::err_typecheck_call_too_few_args_at_least) 1198d77b9a29651d748f0e30a8dad8969635fc04f725Eric Christopher << 0 /*function call*/ << 2 << TheCall->getNumArgs(); 119956f20ae1010aa71defd7572f660b41288c56cdd1Eli Friedman } 120056f20ae1010aa71defd7572f660b41288c56cdd1Eli Friedman 12015f8d604246976a93a73549b07bbc8ee0b2061b50John McCall // Type-check the first argument normally. 12025f8d604246976a93a73549b07bbc8ee0b2061b50John McCall if (checkBuiltinArgument(*this, TheCall, 0)) 12035f8d604246976a93a73549b07bbc8ee0b2061b50John McCall return true; 12045f8d604246976a93a73549b07bbc8ee0b2061b50John McCall 1205c27c665c88b49dfb212aedc7bab8b9bf67658b9eChris Lattner // Determine whether the current function is variadic or not. 12069ea9bdbc14374f7bacdb50d3e52c664ff12150ffDouglas Gregor BlockScopeInfo *CurBlock = getCurBlock(); 1207c27c665c88b49dfb212aedc7bab8b9bf67658b9eChris Lattner bool isVariadic; 1208cd9c51433c06705645d1ee5a13da3c9a72d7d025Steve Naroff if (CurBlock) 1209c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall isVariadic = CurBlock->TheDecl->isVariadic(); 12109498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek else if (FunctionDecl *FD = getCurFunctionDecl()) 12119498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek isVariadic = FD->isVariadic(); 12129498d388810d284d3970aef0d69fa4d069fd6cafTed Kremenek else 121353d0ea5f5bfa647ec23418bf3a3b7c183b51e4bdArgyrios Kyrtzidis isVariadic = getCurMethodDecl()->isVariadic(); 12141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1215c27c665c88b49dfb212aedc7bab8b9bf67658b9eChris Lattner if (!isVariadic) { 121630ce344307f8a8b00054021307015571f83c7364Chris Lattner Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); 121730ce344307f8a8b00054021307015571f83c7364Chris Lattner return true; 121830ce344307f8a8b00054021307015571f83c7364Chris Lattner } 12191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 122030ce344307f8a8b00054021307015571f83c7364Chris Lattner // Verify that the second argument to the builtin is the last argument of the 122130ce344307f8a8b00054021307015571f83c7364Chris Lattner // current function or method. 122230ce344307f8a8b00054021307015571f83c7364Chris Lattner bool SecondArgIsLastNamedArgument = false; 1223e2c14103dec39cbd24dac9d7b3e91277b109c14fAnders Carlsson const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); 12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 122588cf226caee50956ef47edd4d44cf7b80703a26cAnders Carlsson if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { 122688cf226caee50956ef47edd4d44cf7b80703a26cAnders Carlsson if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { 122730ce344307f8a8b00054021307015571f83c7364Chris Lattner // FIXME: This isn't correct for methods (results in bogus warning). 122830ce344307f8a8b00054021307015571f83c7364Chris Lattner // Get the last formal in the current function. 122988cf226caee50956ef47edd4d44cf7b80703a26cAnders Carlsson const ParmVarDecl *LastArg; 1230cd9c51433c06705645d1ee5a13da3c9a72d7d025Steve Naroff if (CurBlock) 1231cd9c51433c06705645d1ee5a13da3c9a72d7d025Steve Naroff LastArg = *(CurBlock->TheDecl->param_end()-1); 1232cd9c51433c06705645d1ee5a13da3c9a72d7d025Steve Naroff else if (FunctionDecl *FD = getCurFunctionDecl()) 1233371f258e61e1365b951b17931a3c5ac1530fd1a0Chris Lattner LastArg = *(FD->param_end()-1); 123430ce344307f8a8b00054021307015571f83c7364Chris Lattner else 123553d0ea5f5bfa647ec23418bf3a3b7c183b51e4bdArgyrios Kyrtzidis LastArg = *(getCurMethodDecl()->param_end()-1); 123630ce344307f8a8b00054021307015571f83c7364Chris Lattner SecondArgIsLastNamedArgument = PV == LastArg; 123730ce344307f8a8b00054021307015571f83c7364Chris Lattner } 123830ce344307f8a8b00054021307015571f83c7364Chris Lattner } 12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 124030ce344307f8a8b00054021307015571f83c7364Chris Lattner if (!SecondArgIsLastNamedArgument) 12411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Diag(TheCall->getArg(1)->getLocStart(), 124230ce344307f8a8b00054021307015571f83c7364Chris Lattner diag::warn_second_parameter_of_va_start_not_last_named_argument); 124330ce344307f8a8b00054021307015571f83c7364Chris Lattner return false; 12446cfda23b3768f93a6eb0b2a9135c8334a20125bbEli Friedman} 124530ce344307f8a8b00054021307015571f83c7364Chris Lattner 12461b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and 12471b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner/// friends. This is declared to take (...), so we have to check everything. 1248925e60d3fa706f31886027c876989af79eb0e0d2Chris Lattnerbool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { 1249925e60d3fa706f31886027c876989af79eb0e0d2Chris Lattner if (TheCall->getNumArgs() < 2) 12502c21a073525cdfa68e4439b7af551385dc2796abChris Lattner return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) 1251d77b9a29651d748f0e30a8dad8969635fc04f725Eric Christopher << 0 << 2 << TheCall->getNumArgs()/*function call*/; 1252925e60d3fa706f31886027c876989af79eb0e0d2Chris Lattner if (TheCall->getNumArgs() > 2) 12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Diag(TheCall->getArg(2)->getLocStart(), 1254fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner diag::err_typecheck_call_too_many_args) 1255ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher << 0 /*function call*/ << 2 << TheCall->getNumArgs() 1256fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner << SourceRange(TheCall->getArg(2)->getLocStart(), 1257fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner (*(TheCall->arg_end()-1))->getLocEnd()); 12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1259429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult OrigArg0 = TheCall->getArg(0); 1260429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley ExprResult OrigArg1 = TheCall->getArg(1); 1261cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor 12621b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner // Do standard promotions between the two arguments, returning their common 12631b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner // type. 1264925e60d3fa706f31886027c876989af79eb0e0d2Chris Lattner QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); 1265429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) 1266429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return true; 1267403bc2bd9edffe51d73776643fa87696c9417678Daniel Dunbar 1268403bc2bd9edffe51d73776643fa87696c9417678Daniel Dunbar // Make sure any conversions are pushed back into the call; this is 1269403bc2bd9edffe51d73776643fa87696c9417678Daniel Dunbar // type safe since unordered compare builtins are declared as "_Bool 1270403bc2bd9edffe51d73776643fa87696c9417678Daniel Dunbar // foo(...)". 1271429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley TheCall->setArg(0, OrigArg0.get()); 1272429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley TheCall->setArg(1, OrigArg1.get()); 12731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1274429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) 1275cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor return false; 1276cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor 12771b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner // If the common type isn't a real floating type, then the arguments were 12781b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner // invalid for this operation. 1279860a319e62b0e256817a458396d191aa91c0787aEli Friedman if (Res.isNull() || !Res->isRealFloatingType()) 1280429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley return Diag(OrigArg0.get()->getLocStart(), 1281fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner diag::err_typecheck_call_invalid_ordered_compare) 1282429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley << OrigArg0.get()->getType() << OrigArg1.get()->getType() 1283429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); 12841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12851b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner return false; 12861b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner} 12871b9a0793955070738cac6f04b5abe9496be9b317Chris Lattner 1288e771a7ac11fb27f0e734e5de4d858f2c268895e5Benjamin Kramer/// SemaBuiltinSemaBuiltinFPClassification - Handle functions like 1289e771a7ac11fb27f0e734e5de4d858f2c268895e5Benjamin Kramer/// __builtin_isnan and friends. This is declared to take (...), so we have 12903b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramer/// to check everything. We expect the last argument to be a floating point 12913b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramer/// value. 12923b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramerbool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { 12933b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramer if (TheCall->getNumArgs() < NumArgs) 12949ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) 1295d77b9a29651d748f0e30a8dad8969635fc04f725Eric Christopher << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; 12963b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramer if (TheCall->getNumArgs() > NumArgs) 12973b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramer return Diag(TheCall->getArg(NumArgs)->getLocStart(), 12989ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman diag::err_typecheck_call_too_many_args) 1299ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() 13003b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramer << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), 13019ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman (*(TheCall->arg_end()-1))->getLocEnd()); 13029ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman 13033b1e26b708e8d00d4ba10ff857201bae2aff6baeBenjamin Kramer Expr *OrigArg = TheCall->getArg(NumArgs-1); 13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13059ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman if (OrigArg->isTypeDependent()) 13069ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman return false; 13079ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman 130881368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner // This operation requires a non-_Complex floating-point number. 13099ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman if (!OrigArg->getType()->isRealFloatingType()) 13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Diag(OrigArg->getLocStart(), 13119ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman diag::err_typecheck_call_invalid_unary_fp) 13129ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman << OrigArg->getType() << OrigArg->getSourceRange(); 13131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 131481368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner // If this is an implicit conversion from float -> double, remove it. 131581368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { 131681368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner Expr *CastArg = Cast->getSubExpr(); 131781368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { 131881368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && 131981368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner "promotion from float to double is the only expected cast here"); 132081368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner Cast->setSubExpr(0); 132181368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner TheCall->setArg(NumArgs-1, CastArg); 132281368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner } 132381368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner } 132481368fbfd6d57150f66c993dc9041d62a7a32c4fChris Lattner 13259ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman return false; 13269ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman} 13279ac6f62a77be5b281a7ddc24a16669b457ac47c2Eli Friedman 1328d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. 1329d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman// This is declared to take (...), so we have to check everything. 133060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { 133137b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman if (TheCall->getNumArgs() < 2) 13320eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl return ExprError(Diag(TheCall->getLocEnd(), 1333d77b9a29651d748f0e30a8dad8969635fc04f725Eric Christopher diag::err_typecheck_call_too_few_args_at_least) 133437b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman << 0 /*function call*/ << 2 << TheCall->getNumArgs() 1335d77b9a29651d748f0e30a8dad8969635fc04f725Eric Christopher << TheCall->getSourceRange()); 1336d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman 133737b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman // Determine which of the following types of shufflevector we're checking: 133837b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman // 1) unary, vector mask: (lhs, mask) 133937b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman // 2) binary, vector mask: (lhs, rhs, mask) 134037b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman // 3) binary, scalar mask: (lhs, rhs, index, ..., index) 134137b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman QualType resType = TheCall->getArg(0)->getType(); 134237b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman unsigned numElements = 0; 134337b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman 1344cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor if (!TheCall->getArg(0)->isTypeDependent() && 1345cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor !TheCall->getArg(1)->isTypeDependent()) { 134637b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman QualType LHSType = TheCall->getArg(0)->getType(); 134737b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman QualType RHSType = TheCall->getArg(1)->getType(); 134837b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman 134937b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman if (!LHSType->isVectorType() || !RHSType->isVectorType()) { 1350cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) 13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump << SourceRange(TheCall->getArg(0)->getLocStart(), 1352cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor TheCall->getArg(1)->getLocEnd()); 1353cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor return ExprError(); 1354cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor } 135537b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman 135637b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman numElements = LHSType->getAs<VectorType>()->getNumElements(); 135737b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman unsigned numResElements = TheCall->getNumArgs() - 2; 135837b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman 135937b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman // Check to see if we have a call with 2 vector arguments, the unary shuffle 136037b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman // with mask. If so, verify that RHS is an integer vector type with the 136137b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman // same number of elts as lhs. 136237b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman if (TheCall->getNumArgs() == 2) { 1363f60946222721d9ba3c059563935c17b84703187aDouglas Gregor if (!RHSType->hasIntegerRepresentation() || 136437b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman RHSType->getAs<VectorType>()->getNumElements() != numElements) 136537b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) 136637b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman << SourceRange(TheCall->getArg(1)->getLocStart(), 136737b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman TheCall->getArg(1)->getLocEnd()); 136837b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman numResElements = numElements; 136937b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman } 137037b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { 1371cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) 13721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump << SourceRange(TheCall->getArg(0)->getLocStart(), 1373cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor TheCall->getArg(1)->getLocEnd()); 1374cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor return ExprError(); 137537b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman } else if (numElements != numResElements) { 137637b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman QualType eltType = LHSType->getAs<VectorType>()->getElementType(); 1377788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner resType = Context.getVectorType(eltType, numResElements, 1378e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson VectorType::GenericVector); 1379cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor } 1380d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman } 1381d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman 1382d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { 1383cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor if (TheCall->getArg(i)->isTypeDependent() || 1384cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor TheCall->getArg(i)->isValueDependent()) 1385cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor continue; 1386cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor 138737b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman llvm::APSInt Result(32); 138837b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) 138937b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman return ExprError(Diag(TheCall->getLocStart(), 139037b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman diag::err_shufflevector_nonconstant_argument) 139137b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman << TheCall->getArg(i)->getSourceRange()); 13920eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl 1393d1a0b6d3dfb208f638d3d750b588d9c0daa49289Chris Lattner if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) 13940eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl return ExprError(Diag(TheCall->getLocStart(), 1395fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner diag::err_shufflevector_argument_too_large) 13960eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl << TheCall->getArg(i)->getSourceRange()); 1397d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman } 1398d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman 13995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<Expr*, 32> exprs; 1400d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman 1401d1a0b6d3dfb208f638d3d750b588d9c0daa49289Chris Lattner for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { 1402d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman exprs.push_back(TheCall->getArg(i)); 1403d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman TheCall->setArg(i, 0); 1404d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman } 1405d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman 1406a88dc3079bedf70a5cfc39791727e43a10383006Nate Begeman return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), 140737b6a5731a47f811d754f0d48aa93edf30e30513Nate Begeman exprs.size(), resType, 14088189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek TheCall->getCallee()->getLocStart(), 14098189cde56b4f6f938cd65f53c932fe1860d0204cTed Kremenek TheCall->getRParenLoc())); 1410d38617c8a50f9729c254ab76cd359af797c6739bEli Friedman} 141130ce344307f8a8b00054021307015571f83c7364Chris Lattner 14124493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar/// SemaBuiltinPrefetch - Handle __builtin_prefetch. 14134493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar// This is declared to take (const void*, ...) and can take two 14144493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar// optional constant int args. 14154493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbarbool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { 1416fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner unsigned NumArgs = TheCall->getNumArgs(); 14174493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar 1418fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner if (NumArgs > 3) 1419ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher return Diag(TheCall->getLocEnd(), 1420ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher diag::err_typecheck_call_too_many_args_at_most) 1421ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher << 0 /*function call*/ << 3 << NumArgs 1422ccfa9639f8d09733bcf1c2572c5bd3daba5bd632Eric Christopher << TheCall->getSourceRange(); 14234493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar 14244493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar // Argument 0 is checked for us and the remaining arguments must be 14254493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar // constant integers. 1426fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner for (unsigned i = 1; i != NumArgs; ++i) { 14274493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar Expr *Arg = TheCall->getArg(i); 1428691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher 14299aef7263d84d9224575e113878cfbb06b8343cbfEli Friedman llvm::APSInt Result; 1430691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher if (SemaBuiltinConstantArg(TheCall, i, Result)) 1431691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher return true; 14321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14334493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar // FIXME: gcc issues a warning and rewrites these to 0. These 14344493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar // seems especially odd for the third argument since the default 14354493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar // is 3. 1436fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner if (i == 1) { 14379aef7263d84d9224575e113878cfbb06b8343cbfEli Friedman if (Result.getLimitedValue() > 1) 1438fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) 143921fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner << "0" << "1" << Arg->getSourceRange(); 14404493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar } else { 14419aef7263d84d9224575e113878cfbb06b8343cbfEli Friedman if (Result.getLimitedValue() > 3) 1442fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) 144321fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner << "0" << "3" << Arg->getSourceRange(); 14444493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar } 14454493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar } 14464493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar 1447fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner return false; 14484493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar} 14494493f79fce48cd9cbd9f55fa9d452cde736747a0Daniel Dunbar 1450691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher/// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr 1451691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher/// TheCall is a constant expression. 1452691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopherbool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, 1453691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher llvm::APSInt &Result) { 1454691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher Expr *Arg = TheCall->getArg(ArgNum); 1455691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 1456691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 1457691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher 1458691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; 1459691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher 1460691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher if (!Arg->isIntegerConstantExpr(Result, Context)) 1461691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) 14625e8965525282a48fd34af05183b8c3705a5b00d5Eric Christopher << FDecl->getDeclName() << Arg->getSourceRange(); 1463691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher 146421fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner return false; 146521fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner} 146621fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner 1467d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar/// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, 1468d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar/// int type). This simply type checks that type is one of the defined 1469d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar/// constants (0-3). 1470fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner// For compatibility check 0-3, llvm only handles 0 and 2. 1471d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbarbool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { 1472691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher llvm::APSInt Result; 1473691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher 1474691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher // Check constant-ness first. 1475691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher if (SemaBuiltinConstantArg(TheCall, 1, Result)) 1476691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher return true; 1477d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar 1478691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher Expr *Arg = TheCall->getArg(1); 1479d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { 1480fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) 1481fa25bbb351f4fdd977f51254119cdfc2b525ce90Chris Lattner << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); 1482d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar } 1483d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar 1484d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar return false; 1485d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar} 1486d5f8a4fd4d6dfb0415b93bb7ab721bba5cab1332Daniel Dunbar 1487586d6a81428da2d1ce70bcb98df29d749361cbf3Eli Friedman/// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). 1488d875fed28578835de89cd407e9db4be788596d7cEli Friedman/// This checks that val is a constant 1. 1489d875fed28578835de89cd407e9db4be788596d7cEli Friedmanbool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { 1490d875fed28578835de89cd407e9db4be788596d7cEli Friedman Expr *Arg = TheCall->getArg(1); 1491691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher llvm::APSInt Result; 1492cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor 1493691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher // TODO: This is less than ideal. Overload this to take a value. 1494691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher if (SemaBuiltinConstantArg(TheCall, 1, Result)) 1495691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher return true; 1496691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher 1497691ebc3f3225542e5abd85e107ebdbec907cf510Eric Christopher if (Result != 1) 1498d875fed28578835de89cd407e9db4be788596d7cEli Friedman return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) 1499d875fed28578835de89cd407e9db4be788596d7cEli Friedman << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); 1500d875fed28578835de89cd407e9db4be788596d7cEli Friedman 1501d875fed28578835de89cd407e9db4be788596d7cEli Friedman return false; 1502d875fed28578835de89cd407e9db4be788596d7cEli Friedman} 1503d875fed28578835de89cd407e9db4be788596d7cEli Friedman 1504b43e8ad893706a2499c4901b8f5cb289553d66b1Ted Kremenek// Handle i > 1 ? "x" : "y", recursively. 150529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupasbool Sema::SemaCheckStringLiteral(const Expr *E, Expr **Args, 150629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas unsigned NumArgs, bool HasVAListArg, 1507826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned format_idx, unsigned firstDataArg, 150834269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas FormatStringType Type, bool inFunctionCall) { 15094fe6441a558e471f2ad3c6bddf07c77332539f6bTed Kremenek tryAgain: 1510cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor if (E->isTypeDependent() || E->isValueDependent()) 1511cde01739dffe574c53a6ba1def1a57a2cc7b4a8bDouglas Gregor return false; 1512d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek 151334269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas E = E->IgnoreParenCasts(); 1514f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne 1515a73cdcbc1dc1ed98c54556c6adcd2b12301759ccDavid Blaikie if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) 1516a73cdcbc1dc1ed98c54556c6adcd2b12301759ccDavid Blaikie // Technically -Wformat-nonliteral does not warn about this case. 1517a73cdcbc1dc1ed98c54556c6adcd2b12301759ccDavid Blaikie // The behavior of printf and friends in this case is implementation 1518a73cdcbc1dc1ed98c54556c6adcd2b12301759ccDavid Blaikie // dependent. Ideally if the format string cannot be null then 1519a73cdcbc1dc1ed98c54556c6adcd2b12301759ccDavid Blaikie // it should have a 'nonnull' attribute in the function prototype. 1520a73cdcbc1dc1ed98c54556c6adcd2b12301759ccDavid Blaikie return true; 1521a73cdcbc1dc1ed98c54556c6adcd2b12301759ccDavid Blaikie 1522d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek switch (E->getStmtClass()) { 152356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Stmt::BinaryConditionalOperatorClass: 1524d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek case Stmt::ConditionalOperatorClass: { 152556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const AbstractConditionalOperator *C = cast<AbstractConditionalOperator>(E); 152629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas return SemaCheckStringLiteral(C->getTrueExpr(), Args, NumArgs, HasVAListArg, 152734269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas format_idx, firstDataArg, Type, 152855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu inFunctionCall) 152934269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas && SemaCheckStringLiteral(C->getFalseExpr(), Args, NumArgs, HasVAListArg, 153034269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas format_idx, firstDataArg, Type, 153134269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas inFunctionCall); 1532d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek } 1533d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek 1534d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek case Stmt::ImplicitCastExprClass: { 15354fe6441a558e471f2ad3c6bddf07c77332539f6bTed Kremenek E = cast<ImplicitCastExpr>(E)->getSubExpr(); 15364fe6441a558e471f2ad3c6bddf07c77332539f6bTed Kremenek goto tryAgain; 1537d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek } 1538d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek 153956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Stmt::OpaqueValueExprClass: 154056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { 154156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall E = src; 154256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall goto tryAgain; 154356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 154456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return false; 154556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 1546b43e8ad893706a2499c4901b8f5cb289553d66b1Ted Kremenek case Stmt::PredefinedExprClass: 1547b43e8ad893706a2499c4901b8f5cb289553d66b1Ted Kremenek // While __func__, etc., are technically not string literals, they 1548b43e8ad893706a2499c4901b8f5cb289553d66b1Ted Kremenek // cannot contain format specifiers and thus are not a security 1549b43e8ad893706a2499c4901b8f5cb289553d66b1Ted Kremenek // liability. 1550b43e8ad893706a2499c4901b8f5cb289553d66b1Ted Kremenek return true; 1551b43e8ad893706a2499c4901b8f5cb289553d66b1Ted Kremenek 1552082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek case Stmt::DeclRefExprClass: { 1553082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek const DeclRefExpr *DR = cast<DeclRefExpr>(E); 15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1555082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek // As an exception, do not flag errors for variables binding to 1556082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek // const string literals. 1557082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { 1558082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek bool isConstant = false; 1559082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek QualType T = DR->getType(); 1560082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek 1561082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek if (const ArrayType *AT = Context.getAsArrayType(T)) { 1562082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek isConstant = AT->getElementType().isConstant(Context); 1563ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump } else if (const PointerType *PT = T->getAs<PointerType>()) { 15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump isConstant = T.isConstant(Context) && 1565082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek PT->getPointeeType().isConstant(Context); 1566e98e5b54543f8a9e35626fb44dc0c649e71917d9Jean-Daniel Dupas } else if (T->isObjCObjectPointerType()) { 1567e98e5b54543f8a9e35626fb44dc0c649e71917d9Jean-Daniel Dupas // In ObjC, there is usually no "const ObjectPointer" type, 1568e98e5b54543f8a9e35626fb44dc0c649e71917d9Jean-Daniel Dupas // so don't check if the pointee type is constant. 1569e98e5b54543f8a9e35626fb44dc0c649e71917d9Jean-Daniel Dupas isConstant = T.isConstant(Context); 1570082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek } 15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1572082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek if (isConstant) { 1573e2c60667d1bc29b7e148f9c9828dcbdbfc5b82b0Matt Beaumont-Gay if (const Expr *Init = VD->getAnyInitializer()) { 1574e2c60667d1bc29b7e148f9c9828dcbdbfc5b82b0Matt Beaumont-Gay // Look through initializers like const char c[] = { "foo" } 1575e2c60667d1bc29b7e148f9c9828dcbdbfc5b82b0Matt Beaumont-Gay if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { 1576e2c60667d1bc29b7e148f9c9828dcbdbfc5b82b0Matt Beaumont-Gay if (InitList->isStringLiteralInit()) 1577e2c60667d1bc29b7e148f9c9828dcbdbfc5b82b0Matt Beaumont-Gay Init = InitList->getInit(0)->IgnoreParenImpCasts(); 1578e2c60667d1bc29b7e148f9c9828dcbdbfc5b82b0Matt Beaumont-Gay } 157929c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas return SemaCheckStringLiteral(Init, Args, NumArgs, 1580826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek HasVAListArg, format_idx, firstDataArg, 158134269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas Type, /*inFunctionCall*/false); 1582e2c60667d1bc29b7e148f9c9828dcbdbfc5b82b0Matt Beaumont-Gay } 1583082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek } 15841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1585d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // For vprintf* functions (i.e., HasVAListArg==true), we add a 1586d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // special check to see if the format string is a function parameter 1587d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // of the function calling the printf function. If the function 1588d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // has an attribute indicating it is a printf-like function, then we 1589d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // should suppress warnings concerning non-literals being used in a call 1590d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // to a vprintf function. For example: 1591d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // 1592d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // void 1593d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ 1594d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // va_list ap; 1595d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // va_start(ap, fmt); 1596d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". 1597d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // ... 1598d966a55bccae13f34d18958877c5e71dd643a125Anders Carlsson // 1599f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas if (HasVAListArg) { 1600f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { 1601f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { 1602f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas int PVIndex = PV->getFunctionScopeIndex() + 1; 1603f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas for (specific_attr_iterator<FormatAttr> 1604f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas i = ND->specific_attr_begin<FormatAttr>(), 1605f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) { 1606f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas FormatAttr *PVFormat = *i; 1607f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas // adjust for implicit parameter 1608f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) 1609f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas if (MD->isInstance()) 1610f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas ++PVIndex; 1611f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas // We also check if the formats are compatible. 1612f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas // We can't pass a 'scanf' string to a 'printf' function. 1613f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas if (PVIndex == PVFormat->getFormatIdx() && 1614f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas Type == GetFormatStringType(PVFormat)) 1615f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas return true; 1616f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas } 1617f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas } 1618f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas } 1619f57c413e444b441fa75ae8911d183c19b53bdd56Jean-Daniel Dupas } 1620082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek } 16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1622082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek return false; 1623082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek } 1624d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek 162552aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas case Stmt::CallExprClass: 162652aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas case Stmt::CXXMemberCallExprClass: { 16278f031b3b14e726b093fb8693c2be4ab6d779e331Anders Carlsson const CallExpr *CE = cast<CallExpr>(E); 162852aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { 162952aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { 163052aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas unsigned ArgIndex = FA->getFormatIdx(); 163152aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) 163252aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas if (MD->isInstance()) 163352aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas --ArgIndex; 163452aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas const Expr *Arg = CE->getArg(ArgIndex - 1); 163552aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas 163652aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg, 163752aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas format_idx, firstDataArg, Type, 163852aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas inFunctionCall); 163950687314f87b67073c202fe46d84d12fb55e25cfJordan Rose } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 164050687314f87b67073c202fe46d84d12fb55e25cfJordan Rose unsigned BuiltinID = FD->getBuiltinID(); 164150687314f87b67073c202fe46d84d12fb55e25cfJordan Rose if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || 164250687314f87b67073c202fe46d84d12fb55e25cfJordan Rose BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { 164350687314f87b67073c202fe46d84d12fb55e25cfJordan Rose const Expr *Arg = CE->getArg(0); 164450687314f87b67073c202fe46d84d12fb55e25cfJordan Rose return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg, 164550687314f87b67073c202fe46d84d12fb55e25cfJordan Rose format_idx, firstDataArg, Type, 164650687314f87b67073c202fe46d84d12fb55e25cfJordan Rose inFunctionCall); 164750687314f87b67073c202fe46d84d12fb55e25cfJordan Rose } 16488f031b3b14e726b093fb8693c2be4ab6d779e331Anders Carlsson } 16498f031b3b14e726b093fb8693c2be4ab6d779e331Anders Carlsson } 16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16518f031b3b14e726b093fb8693c2be4ab6d779e331Anders Carlsson return false; 16528f031b3b14e726b093fb8693c2be4ab6d779e331Anders Carlsson } 1653082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek case Stmt::ObjCStringLiteralClass: 1654082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek case Stmt::StringLiteralClass: { 1655082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek const StringLiteral *StrE = NULL; 16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1657082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) 1658d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek StrE = ObjCFExpr->getString(); 1659d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek else 1660082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek StrE = cast<StringLiteral>(E); 16611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1662d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek if (StrE) { 166329c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas CheckFormatString(StrE, E, Args, NumArgs, HasVAListArg, format_idx, 166434269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas firstDataArg, Type, inFunctionCall); 1665d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek return true; 1666d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek } 16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1668d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek return false; 1669d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek } 16701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1671082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek default: 1672082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek return false; 1673d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek } 1674d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek} 1675d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek 1676e898f8a94947c6074d76ff83943b47d5bbdf210dFariborz Jahanianvoid 16771eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::CheckNonNullArguments(const NonNullAttr *NonNull, 1678909a70d8a185f701c20bacc3c76f8f5979cbbc7cNick Lewycky const Expr * const *ExprArgs, 1679909a70d8a185f701c20bacc3c76f8f5979cbbc7cNick Lewycky SourceLocation CallSiteLoc) { 1680cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt for (NonNullAttr::args_iterator i = NonNull->args_begin(), 1681cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt e = NonNull->args_end(); 1682e898f8a94947c6074d76ff83943b47d5bbdf210dFariborz Jahanian i != e; ++i) { 1683909a70d8a185f701c20bacc3c76f8f5979cbbc7cNick Lewycky const Expr *ArgExpr = ExprArgs[*i]; 16844e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek if (ArgExpr->isNullPointerConstant(Context, 1685ce94049b69f75b44c18584fe79cd238978b6b0d5Douglas Gregor Expr::NPC_ValueDependentIsNotNull)) 1686909a70d8a185f701c20bacc3c76f8f5979cbbc7cNick Lewycky Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); 1687e898f8a94947c6074d76ff83943b47d5bbdf210dFariborz Jahanian } 1688e898f8a94947c6074d76ff83943b47d5bbdf210dFariborz Jahanian} 1689d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek 169034269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel DupasSema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { 169134269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas return llvm::StringSwitch<FormatStringType>(Format->getType()) 169234269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas .Case("scanf", FST_Scanf) 169334269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas .Cases("printf", "printf0", FST_Printf) 169434269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas .Cases("NSString", "CFString", FST_NSString) 169534269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas .Case("strftime", FST_Strftime) 169634269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas .Case("strfmon", FST_Strfmon) 169734269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) 169834269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas .Default(FST_Unknown); 169934269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas} 170034269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas 1701826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek/// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar 1702826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek/// functions) for correct use of format strings. 170329c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupasvoid Sema::CheckFormatArguments(const FormatAttr *Format, CallExpr *TheCall) { 170429c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas bool IsCXXMember = false; 17054a2614e94672c47395abcde60518776fbebec589Sebastian Redl // The way the format attribute works in GCC, the implicit this argument 17064a2614e94672c47395abcde60518776fbebec589Sebastian Redl // of member functions is counted. However, it doesn't appear in our own 17074a2614e94672c47395abcde60518776fbebec589Sebastian Redl // lists, so decrement format_idx in that case. 170852aabafeee9e8634eceb46b1a3fdbd3cd22b1cf7Jean-Daniel Dupas IsCXXMember = isa<CXXMemberCallExpr>(TheCall); 170929c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas CheckFormatArguments(Format, TheCall->getArgs(), TheCall->getNumArgs(), 171029c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas IsCXXMember, TheCall->getRParenLoc(), 171129c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas TheCall->getCallee()->getSourceRange()); 171229c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas} 171329c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas 171429c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupasvoid Sema::CheckFormatArguments(const FormatAttr *Format, Expr **Args, 171529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas unsigned NumArgs, bool IsCXXMember, 171629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas SourceLocation Loc, SourceRange Range) { 171734269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas bool HasVAListArg = Format->getFirstArg() == 0; 171834269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas unsigned format_idx = Format->getFormatIdx() - 1; 171934269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas unsigned firstDataArg = HasVAListArg ? 0 : Format->getFirstArg() - 1; 172034269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas if (IsCXXMember) { 172134269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas if (format_idx == 0) 172234269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas return; 172334269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas --format_idx; 172434269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas if(firstDataArg != 0) 172534269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas --firstDataArg; 172634269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas } 172734269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas CheckFormatArguments(Args, NumArgs, HasVAListArg, format_idx, 172834269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas firstDataArg, GetFormatStringType(Format), Loc, Range); 172929c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas} 17304a2614e94672c47395abcde60518776fbebec589Sebastian Redl 173134269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupasvoid Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs, 173234269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas bool HasVAListArg, unsigned format_idx, 173334269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas unsigned firstDataArg, FormatStringType Type, 173434269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas SourceLocation Loc, SourceRange Range) { 1735826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // CHECK: printf/scanf-like function is called with no format string. 173629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas if (format_idx >= NumArgs) { 173729c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Diag(Loc, diag::warn_missing_format_string) << Range; 173871895b9aa3ad71957359497e136b50fcb6136bdfTed Kremenek return; 173971895b9aa3ad71957359497e136b50fcb6136bdfTed Kremenek } 17401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 174129c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); 17421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 174359907c4d8f6fc8aacfdaa0273bd7a9c140fbb45fChris Lattner // CHECK: format string is not a string literal. 17441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // 174571895b9aa3ad71957359497e136b50fcb6136bdfTed Kremenek // Dynamically generated format strings are difficult to 174671895b9aa3ad71957359497e136b50fcb6136bdfTed Kremenek // automatically vet at compile time. Requiring that format strings 174771895b9aa3ad71957359497e136b50fcb6136bdfTed Kremenek // are string literals: (1) permits the checking of format strings by 174871895b9aa3ad71957359497e136b50fcb6136bdfTed Kremenek // the compiler and thereby (2) can practically remove the source of 174971895b9aa3ad71957359497e136b50fcb6136bdfTed Kremenek // many format string exploits. 17507ff22b259d4d4729f701679e3a7f0e242365e07fTed Kremenek 17511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Format string can be either ObjC string (e.g. @"%d") or 17527ff22b259d4d4729f701679e3a7f0e242365e07fTed Kremenek // C string (e.g. "%d") 17531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // ObjC string uses the same format specifiers as C string, so we can use 17547ff22b259d4d4729f701679e3a7f0e242365e07fTed Kremenek // the same format string checking logic for both ObjC and C strings. 175529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas if (SemaCheckStringLiteral(OrigFormatExpr, Args, NumArgs, HasVAListArg, 175634269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas format_idx, firstDataArg, Type)) 17571cd3e1f72c3a1c256fb6a5c3d4512bca1f1b751dChris Lattner return; // Literal format string found, check done! 17581cd3e1f72c3a1c256fb6a5c3d4512bca1f1b751dChris Lattner 17592837a2f02195e624b14b419b2d3e6682a6bc5a0fJean-Daniel Dupas // Strftime is particular as it always uses a single 'time' argument, 17602837a2f02195e624b14b419b2d3e6682a6bc5a0fJean-Daniel Dupas // so it is safe to pass a non-literal string. 17612837a2f02195e624b14b419b2d3e6682a6bc5a0fJean-Daniel Dupas if (Type == FST_Strftime) 17622837a2f02195e624b14b419b2d3e6682a6bc5a0fJean-Daniel Dupas return; 17632837a2f02195e624b14b419b2d3e6682a6bc5a0fJean-Daniel Dupas 1764ce3aa395240207fd1cfa11342fd4b9fd46d6b025Jean-Daniel Dupas // Do not emit diag when the string param is a macro expansion and the 1765ce3aa395240207fd1cfa11342fd4b9fd46d6b025Jean-Daniel Dupas // format is either NSString or CFString. This is a hack to prevent 1766ce3aa395240207fd1cfa11342fd4b9fd46d6b025Jean-Daniel Dupas // diag when using the NSLocalizedString and CFCopyLocalizedString macros 1767ce3aa395240207fd1cfa11342fd4b9fd46d6b025Jean-Daniel Dupas // which are usually used in place of NS and CF string literals. 1768dc1702001964c3314f7090e6a4af889b5771d884Jean-Daniel Dupas if (Type == FST_NSString && 1769dc1702001964c3314f7090e6a4af889b5771d884Jean-Daniel Dupas SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) 1770ce3aa395240207fd1cfa11342fd4b9fd46d6b025Jean-Daniel Dupas return; 1771ce3aa395240207fd1cfa11342fd4b9fd46d6b025Jean-Daniel Dupas 1772655f141f4d4c92eeebcc880211313e84c0a8b2f2Chris Lattner // If there are no arguments specified, warn with -Wformat-security, otherwise 1773655f141f4d4c92eeebcc880211313e84c0a8b2f2Chris Lattner // warn only with -Wformat-nonliteral. 177429c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas if (NumArgs == format_idx+1) 177529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Diag(Args[format_idx]->getLocStart(), 1776826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek diag::warn_format_nonliteral_noargs) 1777655f141f4d4c92eeebcc880211313e84c0a8b2f2Chris Lattner << OrigFormatExpr->getSourceRange(); 1778655f141f4d4c92eeebcc880211313e84c0a8b2f2Chris Lattner else 177929c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Diag(Args[format_idx]->getLocStart(), 1780826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek diag::warn_format_nonliteral) 1781655f141f4d4c92eeebcc880211313e84c0a8b2f2Chris Lattner << OrigFormatExpr->getSourceRange(); 1782d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek} 1783d30ef87f34015d18bde20b9632032d0063d761aaTed Kremenek 1784e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremeneknamespace { 1785826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekclass CheckFormatHandler : public analyze_format_string::FormatStringHandler { 1786826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekprotected: 1787e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek Sema &S; 1788e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek const StringLiteral *FExpr; 1789e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek const Expr *OrigFormatExpr; 17906ee765348b2855c702fa593fb030ef6abe0d01f6Ted Kremenek const unsigned FirstDataArg; 1791e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek const unsigned NumDataArgs; 1792e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek const char *Beg; // Start of format string. 17930d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek const bool HasVAListArg; 179429c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas const Expr * const *Args; 179529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas const unsigned NumArgs; 17960d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek unsigned FormatIdx; 17977f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek llvm::BitVector CoveredArgs; 1798efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek bool usesPositionalArgs; 1799efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek bool atFirstArg; 180055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu bool inFunctionCall; 18014e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenekpublic: 1802826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek CheckFormatHandler(Sema &s, const StringLiteral *fexpr, 18036ee765348b2855c702fa593fb030ef6abe0d01f6Ted Kremenek const Expr *origFormatExpr, unsigned firstDataArg, 180450687314f87b67073c202fe46d84d12fb55e25cfJordan Rose unsigned numDataArgs, const char *beg, bool hasVAListArg, 180529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Expr **args, unsigned numArgs, 180629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas unsigned formatIdx, bool inFunctionCall) 1807e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), 180850687314f87b67073c202fe46d84d12fb55e25cfJordan Rose FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), 180950687314f87b67073c202fe46d84d12fb55e25cfJordan Rose Beg(beg), HasVAListArg(hasVAListArg), 181029c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Args(args), NumArgs(numArgs), FormatIdx(formatIdx), 181155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu usesPositionalArgs(false), atFirstArg(true), 181255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu inFunctionCall(inFunctionCall) { 18137f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek CoveredArgs.resize(numDataArgs); 18147f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek CoveredArgs.reset(); 18157f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek } 18164e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 181707d161f38e708a91486bf1c031d525faebbb249dTed Kremenek void DoneProcessing(); 18184e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 1819826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek void HandleIncompleteSpecifier(const char *startSpecifier, 1820826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen); 182176517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg 182276517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg void HandleNonStandardLengthModifier( 182376517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const analyze_format_string::LengthModifier &LM, 182476517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const char *startSpecifier, unsigned specifierLen); 182576517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg 182676517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg void HandleNonStandardConversionSpecifier( 182776517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const analyze_format_string::ConversionSpecifier &CS, 182876517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const char *startSpecifier, unsigned specifierLen); 182976517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg 183076517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg void HandleNonStandardConversionSpecification( 183176517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const analyze_format_string::LengthModifier &LM, 183276517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const analyze_format_string::ConversionSpecifier &CS, 183376517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const char *startSpecifier, unsigned specifierLen); 183476517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg 1835f85626453123f9691bcef13cff963f556e209c27Hans Wennborg virtual void HandlePosition(const char *startPos, unsigned posLen); 1836f85626453123f9691bcef13cff963f556e209c27Hans Wennborg 1837efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek virtual void HandleInvalidPosition(const char *startSpecifier, 1838efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek unsigned specifierLen, 1839826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek analyze_format_string::PositionContext p); 1840efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek 1841efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek virtual void HandleZeroPosition(const char *startPos, unsigned posLen); 1842efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek 1843e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek void HandleNullChar(const char *nullCharacter); 18444e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 184555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu template <typename Range> 184655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, 184755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu const Expr *ArgumentExpr, 184855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu PartialDiagnostic PDiag, 184955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu SourceLocation StringLoc, 185055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu bool IsStringLocation, Range StringRange, 185155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint Fixit = FixItHint()); 185255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu 1853826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekprotected: 1854c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, 1855c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek const char *startSpec, 1856c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek unsigned specifierLen, 1857c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek const char *csStart, unsigned csLen); 185855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu 185955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu void HandlePositionalNonpositionalArgs(SourceLocation Loc, 186055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu const char *startSpec, 186155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu unsigned specifierLen); 1862c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 1863f88c8e02da48afea6a04091f675af5c4facd99f1Ted Kremenek SourceRange getFormatStringRange(); 1864826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek CharSourceRange getSpecifierRange(const char *startSpecifier, 1865826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen); 1866e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek SourceLocation getLocationOfByte(const char *x); 18674e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 18680d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek const Expr *getDataArg(unsigned i) const; 1869666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek 1870666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, 1871666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek const analyze_format_string::ConversionSpecifier &CS, 1872666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek const char *startSpecifier, unsigned specifierLen, 1873666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek unsigned argIndex); 187455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu 187555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu template <typename Range> 187655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, 187755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu bool IsStringLocation, Range StringRange, 187855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint Fixit = FixItHint()); 187955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu 188055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu void CheckPositionalAndNonpositionalArgs( 188155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu const analyze_format_string::FormatSpecifier *FS); 1882e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek}; 1883e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek} 1884e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek 1885826a3457f737f1fc45a22954fd1bfde38160c165Ted KremenekSourceRange CheckFormatHandler::getFormatStringRange() { 1886e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek return OrigFormatExpr->getSourceRange(); 1887e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek} 1888e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek 1889826a3457f737f1fc45a22954fd1bfde38160c165Ted KremenekCharSourceRange CheckFormatHandler:: 1890826a3457f737f1fc45a22954fd1bfde38160c165Ted KremenekgetSpecifierRange(const char *startSpecifier, unsigned specifierLen) { 189145f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care SourceLocation Start = getLocationOfByte(startSpecifier); 189245f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); 189345f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care 189445f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care // Advance the end SourceLocation by one due to half-open ranges. 1895a64ccefdf0ea4e03ec88805d71b0af74950c7472Argyrios Kyrtzidis End = End.getLocWithOffset(1); 189645f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care 189745f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care return CharSourceRange::getCharRange(Start, End); 1898f88c8e02da48afea6a04091f675af5c4facd99f1Ted Kremenek} 1899f88c8e02da48afea6a04091f675af5c4facd99f1Ted Kremenek 1900826a3457f737f1fc45a22954fd1bfde38160c165Ted KremenekSourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { 19014e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek return S.getLocationOfStringLiteralByte(FExpr, x - Beg); 1902e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek} 1903e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek 1904826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekvoid CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, 1905826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen){ 190655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), 190755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(startSpecifier), 190855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 190955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen)); 1910808015a18bd97781ce437831a95a92fdfc8d5136Ted Kremenek} 1911808015a18bd97781ce437831a95a92fdfc8d5136Ted Kremenek 191276517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborgvoid CheckFormatHandler::HandleNonStandardLengthModifier( 191376517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const analyze_format_string::LengthModifier &LM, 191476517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const char *startSpecifier, unsigned specifierLen) { 191576517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << LM.toString() 1916f85626453123f9691bcef13cff963f556e209c27Hans Wennborg << 0, 191776517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg getLocationOfByte(LM.getStart()), 191876517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg /*IsStringLocation*/true, 191976517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg getSpecifierRange(startSpecifier, specifierLen)); 192076517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg} 192176517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg 192276517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborgvoid CheckFormatHandler::HandleNonStandardConversionSpecifier( 192376517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const analyze_format_string::ConversionSpecifier &CS, 192476517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const char *startSpecifier, unsigned specifierLen) { 192576517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << CS.toString() 1926f85626453123f9691bcef13cff963f556e209c27Hans Wennborg << 1, 192776517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg getLocationOfByte(CS.getStart()), 192876517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg /*IsStringLocation*/true, 192976517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg getSpecifierRange(startSpecifier, specifierLen)); 193076517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg} 193176517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg 193276517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborgvoid CheckFormatHandler::HandleNonStandardConversionSpecification( 193376517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const analyze_format_string::LengthModifier &LM, 193476517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const analyze_format_string::ConversionSpecifier &CS, 193576517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg const char *startSpecifier, unsigned specifierLen) { 193676517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_conversion_spec) 193776517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg << LM.toString() << CS.toString(), 193876517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg getLocationOfByte(LM.getStart()), 193976517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg /*IsStringLocation*/true, 194076517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg getSpecifierRange(startSpecifier, specifierLen)); 194176517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg} 194276517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg 1943f85626453123f9691bcef13cff963f556e209c27Hans Wennborgvoid CheckFormatHandler::HandlePosition(const char *startPos, 1944f85626453123f9691bcef13cff963f556e209c27Hans Wennborg unsigned posLen) { 1945f85626453123f9691bcef13cff963f556e209c27Hans Wennborg EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), 1946f85626453123f9691bcef13cff963f556e209c27Hans Wennborg getLocationOfByte(startPos), 1947f85626453123f9691bcef13cff963f556e209c27Hans Wennborg /*IsStringLocation*/true, 1948f85626453123f9691bcef13cff963f556e209c27Hans Wennborg getSpecifierRange(startPos, posLen)); 1949f85626453123f9691bcef13cff963f556e209c27Hans Wennborg} 1950f85626453123f9691bcef13cff963f556e209c27Hans Wennborg 1951efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenekvoid 1952826a3457f737f1fc45a22954fd1bfde38160c165Ted KremenekCheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, 1953826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek analyze_format_string::PositionContext p) { 195455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) 195555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << (unsigned) p, 195655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(startPos), /*IsStringLocation*/true, 195755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startPos, posLen)); 1958efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek} 1959efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek 1960826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekvoid CheckFormatHandler::HandleZeroPosition(const char *startPos, 1961efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek unsigned posLen) { 196255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), 196355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(startPos), 196455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 196555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startPos, posLen)); 1966efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek} 1967efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek 1968826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekvoid CheckFormatHandler::HandleNullChar(const char *nullCharacter) { 196950687314f87b67073c202fe46d84d12fb55e25cfJordan Rose if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { 19700c06944f5ef31cc6356e30da2971fa5439c4c8efTed Kremenek // The presence of a null character is likely an error. 197155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic( 197255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu S.PDiag(diag::warn_printf_format_string_contains_null_char), 197355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(nullCharacter), /*IsStringLocation*/true, 197455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getFormatStringRange()); 19750c06944f5ef31cc6356e30da2971fa5439c4c8efTed Kremenek } 1976826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek} 1977826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 1978826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekconst Expr *CheckFormatHandler::getDataArg(unsigned i) const { 197929c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas return Args[FirstDataArg + i]; 1980826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek} 19814e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 1982826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekvoid CheckFormatHandler::DoneProcessing() { 1983826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // Does the number of data arguments exceed the number of 1984826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // format conversions in the format string? 1985826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek if (!HasVAListArg) { 1986826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // Find any arguments that weren't covered. 1987826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek CoveredArgs.flip(); 1988826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek signed notCoveredArg = CoveredArgs.find_first(); 1989826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek if (notCoveredArg >= 0) { 1990826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek assert((unsigned)notCoveredArg < NumDataArgs); 1991c03f2df2a2b097bf2686bf32411d5022323988b2Bob Wilson SourceLocation Loc = getDataArg((unsigned) notCoveredArg)->getLocStart(); 1992c03f2df2a2b097bf2686bf32411d5022323988b2Bob Wilson if (!S.getSourceManager().isInSystemMacro(Loc)) { 1993c03f2df2a2b097bf2686bf32411d5022323988b2Bob Wilson EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), 1994c03f2df2a2b097bf2686bf32411d5022323988b2Bob Wilson Loc, 1995c03f2df2a2b097bf2686bf32411d5022323988b2Bob Wilson /*IsStringLocation*/false, getFormatStringRange()); 1996c03f2df2a2b097bf2686bf32411d5022323988b2Bob Wilson } 1997826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek } 1998826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek } 1999826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek} 2000826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2001c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenekbool 2002c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted KremenekCheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, 2003c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek SourceLocation Loc, 2004c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek const char *startSpec, 2005c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek unsigned specifierLen, 2006c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek const char *csStart, 2007c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek unsigned csLen) { 2008c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 2009c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek bool keepGoing = true; 2010c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek if (argIndex < NumDataArgs) { 2011c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek // Consider the argument coverered, even though the specifier doesn't 2012c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek // make sense. 2013c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek CoveredArgs.set(argIndex); 2014c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek } 2015c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek else { 2016c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek // If argIndex exceeds the number of data arguments we 2017c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek // don't issue a warning because that is just a cascade of warnings (and 2018c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek // they may have intended '%%' anyway). We don't want to continue processing 2019c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek // the format string after this point, however, as we will like just get 2020c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek // gibberish when trying to match arguments. 2021c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek keepGoing = false; 2022c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek } 2023c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 202455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) 202555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << StringRef(csStart, csLen), 202655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu Loc, /*IsStringLocation*/true, 202755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpec, specifierLen)); 2028c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 2029c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek return keepGoing; 2030c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek} 2031c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 203255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieuvoid 203355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard TrieuCheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, 203455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu const char *startSpec, 203555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu unsigned specifierLen) { 203655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic( 203755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu S.PDiag(diag::warn_format_mix_positional_nonpositional_args), 203855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); 203955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu} 204055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu 2041666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenekbool 2042666a197deb75d95c78ddd39274af1a54240828d8Ted KremenekCheckFormatHandler::CheckNumArgs( 2043666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek const analyze_format_string::FormatSpecifier &FS, 2044666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek const analyze_format_string::ConversionSpecifier &CS, 2045666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { 2046666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek 2047666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek if (argIndex >= NumDataArgs) { 204855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu PartialDiagnostic PDiag = FS.usesPositionalArg() 204955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) 205055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << (argIndex+1) << NumDataArgs) 205155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu : S.PDiag(diag::warn_printf_insufficient_data_args); 205255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic( 205355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, 205455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen)); 2055666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek return false; 2056666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek } 2057666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek return true; 2058666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek} 2059666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek 206055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieutemplate<typename Range> 206155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieuvoid CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, 206255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu SourceLocation Loc, 206355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu bool IsStringLocation, 206455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu Range StringRange, 206555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint FixIt) { 206629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, 206755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu Loc, IsStringLocation, StringRange, FixIt); 206855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu} 206955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu 207055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// \brief If the format string is not within the funcion call, emit a note 207155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// so that the function call and string are in diagnostic messages. 207255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// 207355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// \param inFunctionCall if true, the format string is within the function 207455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// call and only one diagnostic message will be produced. Otherwise, an 207555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// extra note will be emitted pointing to location of the format string. 207655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// 207755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// \param ArgumentExpr the expression that is passed as the format string 207855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// argument in the function call. Used for getting locations when two 207955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// diagnostics are emitted. 208055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// 208155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// \param PDiag the callee should already have provided any strings for the 208255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// diagnostic message. This function only adds locations and fixits 208355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// to diagnostics. 208455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// 208555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// \param Loc primary location for diagnostic. If two diagnostics are 208655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// required, one will be at Loc and a new SourceLocation will be created for 208755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// the other one. 208855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// 208955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// \param IsStringLocation if true, Loc points to the format string should be 209055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// used for the note. Otherwise, Loc points to the argument list and will 209155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// be used with PDiag. 209255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// 209355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// \param StringRange some or all of the string to highlight. This is 209455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// templated so it can accept either a CharSourceRange or a SourceRange. 209555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// 209655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu/// \param Fixit optional fix it hint for the format string. 209755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieutemplate<typename Range> 209855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieuvoid CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, 209955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu const Expr *ArgumentExpr, 210055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu PartialDiagnostic PDiag, 210155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu SourceLocation Loc, 210255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu bool IsStringLocation, 210355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu Range StringRange, 210455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint FixIt) { 210555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu if (InFunctionCall) 210655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu S.Diag(Loc, PDiag) << StringRange << FixIt; 210755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu else { 210855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) 210955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << ArgumentExpr->getSourceRange(); 211055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), 211155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu diag::note_format_string_defined) 211255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << StringRange << FixIt; 211355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu } 211455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu} 211555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu 2116826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek//===--- CHECK: Printf format string checking ------------------------------===// 2117826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2118826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremeneknamespace { 2119826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekclass CheckPrintfHandler : public CheckFormatHandler { 212050687314f87b67073c202fe46d84d12fb55e25cfJordan Rose bool ObjCContext; 2121826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekpublic: 2122826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, 2123826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const Expr *origFormatExpr, unsigned firstDataArg, 212450687314f87b67073c202fe46d84d12fb55e25cfJordan Rose unsigned numDataArgs, bool isObjC, 2125826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *beg, bool hasVAListArg, 212629c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Expr **Args, unsigned NumArgs, 212729c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas unsigned formatIdx, bool inFunctionCall) 2128826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, 212950687314f87b67073c202fe46d84d12fb55e25cfJordan Rose numDataArgs, beg, hasVAListArg, Args, NumArgs, 213050687314f87b67073c202fe46d84d12fb55e25cfJordan Rose formatIdx, inFunctionCall), ObjCContext(isObjC) {} 2131826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2132826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2133826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek bool HandleInvalidPrintfConversionSpecifier( 2134826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_printf::PrintfSpecifier &FS, 2135826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, 2136826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen); 2137826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2138826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 2139826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, 2140826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen); 2141826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2142826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, 2143826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, unsigned specifierLen); 2144826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, 2145826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_printf::OptionalAmount &Amt, 2146826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned type, 2147826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, unsigned specifierLen); 2148826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek void HandleFlag(const analyze_printf::PrintfSpecifier &FS, 2149826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_printf::OptionalFlag &flag, 2150826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, unsigned specifierLen); 2151826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, 2152826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_printf::OptionalFlag &ignoredFlag, 2153826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_printf::OptionalFlag &flag, 2154826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, unsigned specifierLen); 2155826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek}; 2156826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek} 2157826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2158826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekbool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( 2159826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_printf::PrintfSpecifier &FS, 2160826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, 2161826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen) { 21626ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek const analyze_printf::PrintfConversionSpecifier &CS = 2163c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek FS.getConversionSpecifier(); 2164826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2165c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek return HandleInvalidConversionSpecifier(FS.getArgIndex(), 2166c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek getLocationOfByte(CS.getStart()), 2167c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek startSpecifier, specifierLen, 2168c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek CS.getStart(), CS.getLength()); 216926ac2e07b46bfb4d4f00752c96481c0a98c79c69Ted Kremenek} 217026ac2e07b46bfb4d4f00752c96481c0a98c79c69Ted Kremenek 2171826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekbool CheckPrintfHandler::HandleAmount( 2172826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_format_string::OptionalAmount &Amt, 2173826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned k, const char *startSpecifier, 2174826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen) { 21750d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek 21760d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek if (Amt.hasDataArgument()) { 21770d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek if (!HasVAListArg) { 21787f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek unsigned argIndex = Amt.getArgIndex(); 21797f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek if (argIndex >= NumDataArgs) { 218055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) 218155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << k, 218255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(Amt.getStart()), 218355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 218455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen)); 21850d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek // Don't do any more checking. We will just emit 21860d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek // spurious errors. 21870d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek return false; 21880d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek } 21894e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 21900d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek // Type check the data argument. It should be an 'int'. 219131f8e32788adb299acad455363eb7fd244642c82Ted Kremenek // Although not in conformance with C99, we also allow the argument to be 219231f8e32788adb299acad455363eb7fd244642c82Ted Kremenek // an 'unsigned int' as that is a reasonably safe case. GCC also 219331f8e32788adb299acad455363eb7fd244642c82Ted Kremenek // doesn't emit a warning for that case. 21947f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek CoveredArgs.set(argIndex); 21957f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek const Expr *Arg = getDataArg(argIndex); 21960d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek QualType T = Arg->getType(); 21974e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 21984e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context); 21994e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek assert(ATR.isValid()); 22004e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 22014e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek if (!ATR.matchesType(S.Context, T)) { 220255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) 2203a792aff1c7de253b89c473fdb7eef4a5bba83aecHans Wennborg << k << ATR.getRepresentativeTypeName(S.Context) 220455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << T << Arg->getSourceRange(), 220555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(Amt.getStart()), 220655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 220755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen)); 22080d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek // Don't do any more checking. We will just emit 22090d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek // spurious errors. 22100d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek return false; 22110d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek } 22120d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek } 22130d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek } 22140d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek return true; 22150d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek} 22160d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek 2217e4ee9663168dfb2b4122c768091e30217328c9faTom Carevoid CheckPrintfHandler::HandleInvalidAmount( 2218826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_printf::PrintfSpecifier &FS, 2219e4ee9663168dfb2b4122c768091e30217328c9faTom Care const analyze_printf::OptionalAmount &Amt, 2220e4ee9663168dfb2b4122c768091e30217328c9faTom Care unsigned type, 2221e4ee9663168dfb2b4122c768091e30217328c9faTom Care const char *startSpecifier, 2222e4ee9663168dfb2b4122c768091e30217328c9faTom Care unsigned specifierLen) { 22236ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek const analyze_printf::PrintfConversionSpecifier &CS = 22246ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek FS.getConversionSpecifier(); 2225e4ee9663168dfb2b4122c768091e30217328c9faTom Care 222655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint fixit = 222755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant 222855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), 222955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu Amt.getConstantLength())) 223055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu : FixItHint(); 223155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu 223255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) 223355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << type << CS.toString(), 223455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(Amt.getStart()), 223555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 223655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen), 223755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu fixit); 2238e4ee9663168dfb2b4122c768091e30217328c9faTom Care} 2239e4ee9663168dfb2b4122c768091e30217328c9faTom Care 2240826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekvoid CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, 2241e4ee9663168dfb2b4122c768091e30217328c9faTom Care const analyze_printf::OptionalFlag &flag, 2242e4ee9663168dfb2b4122c768091e30217328c9faTom Care const char *startSpecifier, 2243e4ee9663168dfb2b4122c768091e30217328c9faTom Care unsigned specifierLen) { 2244e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Warn about pointless flag with a fixit removal. 22456ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek const analyze_printf::PrintfConversionSpecifier &CS = 22466ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek FS.getConversionSpecifier(); 224755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) 224855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << flag.toString() << CS.toString(), 224955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(flag.getPosition()), 225055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 225155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen), 225255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint::CreateRemoval( 225355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(flag.getPosition(), 1))); 2254e4ee9663168dfb2b4122c768091e30217328c9faTom Care} 2255e4ee9663168dfb2b4122c768091e30217328c9faTom Care 2256e4ee9663168dfb2b4122c768091e30217328c9faTom Carevoid CheckPrintfHandler::HandleIgnoredFlag( 2257826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_printf::PrintfSpecifier &FS, 2258e4ee9663168dfb2b4122c768091e30217328c9faTom Care const analyze_printf::OptionalFlag &ignoredFlag, 2259e4ee9663168dfb2b4122c768091e30217328c9faTom Care const analyze_printf::OptionalFlag &flag, 2260e4ee9663168dfb2b4122c768091e30217328c9faTom Care const char *startSpecifier, 2261e4ee9663168dfb2b4122c768091e30217328c9faTom Care unsigned specifierLen) { 2262e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Warn about ignored flag with a fixit removal. 226355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) 226455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << ignoredFlag.toString() << flag.toString(), 226555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(ignoredFlag.getPosition()), 226655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 226755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen), 226855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint::CreateRemoval( 226955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(ignoredFlag.getPosition(), 1))); 2270e4ee9663168dfb2b4122c768091e30217328c9faTom Care} 2271e4ee9663168dfb2b4122c768091e30217328c9faTom Care 2272e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenekbool 2273826a3457f737f1fc45a22954fd1bfde38160c165Ted KremenekCheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier 22745c41ee8c49995fb4fd76d686b239c15cbab261eaTed Kremenek &FS, 2275e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek const char *startSpecifier, 2276e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek unsigned specifierLen) { 2277e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek 22786ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek using namespace analyze_format_string; 2279efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek using namespace analyze_printf; 22806ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); 2281e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek 2282baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek if (FS.consumesDataArgument()) { 2283baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek if (atFirstArg) { 2284baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek atFirstArg = false; 2285baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek usesPositionalArgs = FS.usesPositionalArg(); 2286baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek } 2287baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek else if (usesPositionalArgs != FS.usesPositionalArg()) { 228855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 228955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu startSpecifier, specifierLen); 2290baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek return false; 2291baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek } 2292efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek } 2293efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek 22940d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek // First check if the field width, precision, and conversion specifier 22950d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek // have matching data arguments. 2296efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, 2297efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek startSpecifier, specifierLen)) { 22980d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek return false; 22990d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek } 23004e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 2301efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek if (!HandleAmount(FS.getPrecision(), /* precision */ 1, 2302efaff195ba1fa55b6fe0b0b2435b81451387d241Ted Kremenek startSpecifier, specifierLen)) { 23030d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek return false; 23040d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek } 23050d27735c51f5bd392e673cf39a675e14e9442387Ted Kremenek 2306f88c8e02da48afea6a04091f675af5c4facd99f1Ted Kremenek if (!CS.consumesDataArgument()) { 2307f88c8e02da48afea6a04091f675af5c4facd99f1Ted Kremenek // FIXME: Technically specifying a precision or field width here 2308f88c8e02da48afea6a04091f675af5c4facd99f1Ted Kremenek // makes no sense. Worth issuing a warning at some point. 23090e5675de2b67eeca754754ae8d3dacba425a38e8Ted Kremenek return true; 2310f88c8e02da48afea6a04091f675af5c4facd99f1Ted Kremenek } 23114e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 23127f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek // Consume the argument. 23137f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek unsigned argIndex = FS.getArgIndex(); 2314e3fc54790250076e33ad25b14e5be293514fe5eaTed Kremenek if (argIndex < NumDataArgs) { 2315e3fc54790250076e33ad25b14e5be293514fe5eaTed Kremenek // The check to see if the argIndex is valid will come later. 2316e3fc54790250076e33ad25b14e5be293514fe5eaTed Kremenek // We set the bit here because we may exit early from this 2317e3fc54790250076e33ad25b14e5be293514fe5eaTed Kremenek // function if we encounter some other error. 2318e3fc54790250076e33ad25b14e5be293514fe5eaTed Kremenek CoveredArgs.set(argIndex); 2319e3fc54790250076e33ad25b14e5be293514fe5eaTed Kremenek } 23207f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek 23217f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek // Check for using an Objective-C specific conversion specifier 23227f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek // in a non-ObjC literal. 232350687314f87b67073c202fe46d84d12fb55e25cfJordan Rose if (!ObjCContext && CS.isObjCArg()) { 2324826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 2325826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek specifierLen); 23267f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek } 23274e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 2328e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Check for invalid use of field width 2329e4ee9663168dfb2b4122c768091e30217328c9faTom Care if (!FS.hasValidFieldWidth()) { 233045f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, 2331e4ee9663168dfb2b4122c768091e30217328c9faTom Care startSpecifier, specifierLen); 2332e4ee9663168dfb2b4122c768091e30217328c9faTom Care } 2333e4ee9663168dfb2b4122c768091e30217328c9faTom Care 2334e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Check for invalid use of precision 2335e4ee9663168dfb2b4122c768091e30217328c9faTom Care if (!FS.hasValidPrecision()) { 2336e4ee9663168dfb2b4122c768091e30217328c9faTom Care HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, 2337e4ee9663168dfb2b4122c768091e30217328c9faTom Care startSpecifier, specifierLen); 2338e4ee9663168dfb2b4122c768091e30217328c9faTom Care } 2339e4ee9663168dfb2b4122c768091e30217328c9faTom Care 2340e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Check each flag does not conflict with any other component. 234165197b4b0c55bb74af0450230d61ee9461223721Ted Kremenek if (!FS.hasValidThousandsGroupingPrefix()) 234265197b4b0c55bb74af0450230d61ee9461223721Ted Kremenek HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); 2343e4ee9663168dfb2b4122c768091e30217328c9faTom Care if (!FS.hasValidLeadingZeros()) 2344e4ee9663168dfb2b4122c768091e30217328c9faTom Care HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); 2345e4ee9663168dfb2b4122c768091e30217328c9faTom Care if (!FS.hasValidPlusPrefix()) 2346e4ee9663168dfb2b4122c768091e30217328c9faTom Care HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); 234745f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care if (!FS.hasValidSpacePrefix()) 234845f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); 2349e4ee9663168dfb2b4122c768091e30217328c9faTom Care if (!FS.hasValidAlternativeForm()) 2350e4ee9663168dfb2b4122c768091e30217328c9faTom Care HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); 2351e4ee9663168dfb2b4122c768091e30217328c9faTom Care if (!FS.hasValidLeftJustified()) 2352e4ee9663168dfb2b4122c768091e30217328c9faTom Care HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); 2353e4ee9663168dfb2b4122c768091e30217328c9faTom Care 2354e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Check that flags are not ignored by another flag 235545f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' 235645f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), 235745f9b7e8f23072d662ee1cc758f4ecb0da5e3322Tom Care startSpecifier, specifierLen); 2358e4ee9663168dfb2b4122c768091e30217328c9faTom Care if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' 2359e4ee9663168dfb2b4122c768091e30217328c9faTom Care HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), 2360e4ee9663168dfb2b4122c768091e30217328c9faTom Care startSpecifier, specifierLen); 2361e4ee9663168dfb2b4122c768091e30217328c9faTom Care 2362e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Check the length modifier is valid with the given conversion specifier. 2363e4ee9663168dfb2b4122c768091e30217328c9faTom Care const LengthModifier &LM = FS.getLengthModifier(); 2364e4ee9663168dfb2b4122c768091e30217328c9faTom Care if (!FS.hasValidLengthModifier()) 236555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) 236655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << LM.toString() << CS.toString(), 236755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(LM.getStart()), 236855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 236955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen), 237055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint::CreateRemoval( 237155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(LM.getStart(), 237255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu LM.getLength()))); 237376517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg if (!FS.hasStandardLengthModifier()) 237476517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen); 23754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 237676517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 237776517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg if (!FS.hasStandardLengthConversionCombination()) 237876517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg HandleNonStandardConversionSpecification(LM, CS, startSpecifier, 237976517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg specifierLen); 2380e4ee9663168dfb2b4122c768091e30217328c9faTom Care 2381e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Are we using '%n'? 238235d353b47bce29200b910371dd9b8ba7f3058ab8Ted Kremenek if (CS.getKind() == ConversionSpecifier::nArg) { 2383e4ee9663168dfb2b4122c768091e30217328c9faTom Care // Issue a warning about this being a possible security issue. 238455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_printf_write_back), 238555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(CS.getStart()), 238655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, 238755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen)); 2388e82d804ee761006250543d6fe6e98ee7896cd756Ted Kremenek // Continue checking the other format specifiers. 2389e82d804ee761006250543d6fe6e98ee7896cd756Ted Kremenek return true; 2390e82d804ee761006250543d6fe6e98ee7896cd756Ted Kremenek } 23915c41ee8c49995fb4fd76d686b239c15cbab261eaTed Kremenek 2392da51f0d136df131a0137c0ec1069fb586d8a296aTed Kremenek // The remaining checks depend on the data arguments. 2393da51f0d136df131a0137c0ec1069fb586d8a296aTed Kremenek if (HasVAListArg) 2394da51f0d136df131a0137c0ec1069fb586d8a296aTed Kremenek return true; 23954e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 2396666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 2397da51f0d136df131a0137c0ec1069fb586d8a296aTed Kremenek return false; 23984e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 239996827eb52405a71c65c200949f3e644368e86454Michael J. Spencer // Now type check the data expression that matches the 240096827eb52405a71c65c200949f3e644368e86454Michael J. Spencer // format specifier. 240196827eb52405a71c65c200949f3e644368e86454Michael J. Spencer const Expr *Ex = getDataArg(argIndex); 2402339b9072e26a2a0fe796dc69c4d28d964d0ec86dNico Weber const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context, 240350687314f87b67073c202fe46d84d12fb55e25cfJordan Rose ObjCContext); 240496827eb52405a71c65c200949f3e644368e86454Michael J. Spencer if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { 2405ee0259d308e72141982a85b40863e760a8447edfJordan Rose // Look through argument promotions for our error message's reported type. 2406ee0259d308e72141982a85b40863e760a8447edfJordan Rose // This includes the integral and floating promotions, but excludes array 2407ee0259d308e72141982a85b40863e760a8447edfJordan Rose // and function pointer decay; seeing that an argument intended to be a 2408ee0259d308e72141982a85b40863e760a8447edfJordan Rose // string has type 'char [6]' is probably more confusing than 'char *'. 2409ee0259d308e72141982a85b40863e760a8447edfJordan Rose if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex)) { 2410ee0259d308e72141982a85b40863e760a8447edfJordan Rose if (ICE->getCastKind() == CK_IntegralCast || 2411ee0259d308e72141982a85b40863e760a8447edfJordan Rose ICE->getCastKind() == CK_FloatingCast) { 24124d8ae4d57ed18d60da4d3786fb740607aa7c3688Ted Kremenek Ex = ICE->getSubExpr(); 2413ee0259d308e72141982a85b40863e760a8447edfJordan Rose 2414ee0259d308e72141982a85b40863e760a8447edfJordan Rose // Check if we didn't match because of an implicit cast from a 'char' 2415ee0259d308e72141982a85b40863e760a8447edfJordan Rose // or 'short' to an 'int'. This is done because printf is a varargs 2416ee0259d308e72141982a85b40863e760a8447edfJordan Rose // function. 2417ee0259d308e72141982a85b40863e760a8447edfJordan Rose if (ICE->getType() == S.Context.IntTy || 2418ee0259d308e72141982a85b40863e760a8447edfJordan Rose ICE->getType() == S.Context.UnsignedIntTy) { 2419ee0259d308e72141982a85b40863e760a8447edfJordan Rose // All further checking is done on the subexpression. 2420ee0259d308e72141982a85b40863e760a8447edfJordan Rose if (ATR.matchesType(S.Context, Ex->getType())) 2421ee0259d308e72141982a85b40863e760a8447edfJordan Rose return true; 2422ee0259d308e72141982a85b40863e760a8447edfJordan Rose } 24234d8ae4d57ed18d60da4d3786fb740607aa7c3688Ted Kremenek } 2424ee0259d308e72141982a85b40863e760a8447edfJordan Rose } 242596827eb52405a71c65c200949f3e644368e86454Michael J. Spencer 242696827eb52405a71c65c200949f3e644368e86454Michael J. Spencer // We may be able to offer a FixItHint if it is a supported type. 242796827eb52405a71c65c200949f3e644368e86454Michael J. Spencer PrintfSpecifier fixedFS = FS; 24284e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(), 242950687314f87b67073c202fe46d84d12fb55e25cfJordan Rose S.Context, ObjCContext); 243096827eb52405a71c65c200949f3e644368e86454Michael J. Spencer 243196827eb52405a71c65c200949f3e644368e86454Michael J. Spencer if (success) { 243296827eb52405a71c65c200949f3e644368e86454Michael J. Spencer // Get the fix string from the fixed format specifier 2433ee0259d308e72141982a85b40863e760a8447edfJordan Rose SmallString<16> buf; 243496827eb52405a71c65c200949f3e644368e86454Michael J. Spencer llvm::raw_svector_ostream os(buf); 243596827eb52405a71c65c200949f3e644368e86454Michael J. Spencer fixedFS.toString(os); 243696827eb52405a71c65c200949f3e644368e86454Michael J. Spencer 243755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic( 243855733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) 2439a792aff1c7de253b89c473fdb7eef4a5bba83aecHans Wennborg << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() 244055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu << Ex->getSourceRange(), 2441abf145ab57fc56bfb9c48333fcc65b49a564dc60Matt Beaumont-Gay Ex->getLocStart(), 2442abf145ab57fc56bfb9c48333fcc65b49a564dc60Matt Beaumont-Gay /*IsStringLocation*/false, 244355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen), 244455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint::CreateReplacement( 244555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(startSpecifier, specifierLen), 244655733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu os.str())); 244796827eb52405a71c65c200949f3e644368e86454Michael J. Spencer } 244896827eb52405a71c65c200949f3e644368e86454Michael J. Spencer else { 2449220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas EmitFormatDiagnostic( 2450220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) 2451220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() 2452220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas << getSpecifierRange(startSpecifier, specifierLen) 2453220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas << Ex->getSourceRange(), 2454abf145ab57fc56bfb9c48333fcc65b49a564dc60Matt Beaumont-Gay Ex->getLocStart(), 2455abf145ab57fc56bfb9c48333fcc65b49a564dc60Matt Beaumont-Gay /*IsStringLocation*/false, 2456220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas getSpecifierRange(startSpecifier, specifierLen)); 245796827eb52405a71c65c200949f3e644368e86454Michael J. Spencer } 245896827eb52405a71c65c200949f3e644368e86454Michael J. Spencer } 245996827eb52405a71c65c200949f3e644368e86454Michael J. Spencer 2460e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek return true; 2461e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek} 2462e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek 2463826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek//===--- CHECK: Scanf format string checking ------------------------------===// 2464826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2465826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremeneknamespace { 2466826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekclass CheckScanfHandler : public CheckFormatHandler { 2467826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekpublic: 2468826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek CheckScanfHandler(Sema &s, const StringLiteral *fexpr, 2469826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const Expr *origFormatExpr, unsigned firstDataArg, 247050687314f87b67073c202fe46d84d12fb55e25cfJordan Rose unsigned numDataArgs, const char *beg, bool hasVAListArg, 247129c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Expr **Args, unsigned NumArgs, 247229c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas unsigned formatIdx, bool inFunctionCall) 2473826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, 247450687314f87b67073c202fe46d84d12fb55e25cfJordan Rose numDataArgs, beg, hasVAListArg, 247529c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Args, NumArgs, formatIdx, inFunctionCall) {} 2476826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2477826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 2478826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, 2479826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen); 2480c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 2481c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek bool HandleInvalidScanfConversionSpecifier( 2482c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek const analyze_scanf::ScanfSpecifier &FS, 2483c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek const char *startSpecifier, 2484c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek unsigned specifierLen); 2485b7c21018ec1049580cf6df88db09e606550a7baaTed Kremenek 2486b7c21018ec1049580cf6df88db09e606550a7baaTed Kremenek void HandleIncompleteScanList(const char *start, const char *end); 2487826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek}; 2488826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek} 2489826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2490b7c21018ec1049580cf6df88db09e606550a7baaTed Kremenekvoid CheckScanfHandler::HandleIncompleteScanList(const char *start, 2491b7c21018ec1049580cf6df88db09e606550a7baaTed Kremenek const char *end) { 249255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), 249355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(end), /*IsStringLocation*/true, 249455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getSpecifierRange(start, end - start)); 2495b7c21018ec1049580cf6df88db09e606550a7baaTed Kremenek} 2496b7c21018ec1049580cf6df88db09e606550a7baaTed Kremenek 2497c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenekbool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( 2498c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek const analyze_scanf::ScanfSpecifier &FS, 2499c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek const char *startSpecifier, 2500c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek unsigned specifierLen) { 2501c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 25026ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek const analyze_scanf::ScanfConversionSpecifier &CS = 2503c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek FS.getConversionSpecifier(); 2504c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 2505c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek return HandleInvalidConversionSpecifier(FS.getArgIndex(), 2506c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek getLocationOfByte(CS.getStart()), 2507c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek startSpecifier, specifierLen, 2508c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek CS.getStart(), CS.getLength()); 2509c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek} 2510c09b6a59e02ae265fce51b8c11e2a045bcdaa888Ted Kremenek 2511826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekbool CheckScanfHandler::HandleScanfSpecifier( 2512826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const analyze_scanf::ScanfSpecifier &FS, 2513826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const char *startSpecifier, 2514826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned specifierLen) { 2515826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2516826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek using namespace analyze_scanf; 2517826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek using namespace analyze_format_string; 2518826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 25196ecb950c65329f8d6ce9ad0514632df35a5ab61fTed Kremenek const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); 2520826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2521baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek // Handle case where '%' and '*' don't consume an argument. These shouldn't 2522baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek // be used to decide if we are using positional arguments consistently. 2523baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek if (FS.consumesDataArgument()) { 2524baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek if (atFirstArg) { 2525baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek atFirstArg = false; 2526baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek usesPositionalArgs = FS.usesPositionalArg(); 2527baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek } 2528baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek else if (usesPositionalArgs != FS.usesPositionalArg()) { 252955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 253055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu startSpecifier, specifierLen); 2531baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek return false; 2532baa400654bd6f8396f9a07188445ae7955b060a3Ted Kremenek } 2533826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek } 2534826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2535826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // Check if the field with is non-zero. 2536826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const OptionalAmount &Amt = FS.getFieldWidth(); 2537826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek if (Amt.getHowSpecified() == OptionalAmount::Constant) { 2538826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek if (Amt.getConstantAmount() == 0) { 2539826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek const CharSourceRange &R = getSpecifierRange(Amt.getStart(), 2540826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek Amt.getConstantLength()); 254155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), 254255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu getLocationOfByte(Amt.getStart()), 254355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, R, 254455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu FixItHint::CreateRemoval(R)); 25457f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek } 25467f70dc85d5055c19c8003f43a59135de211ad1b9Ted Kremenek } 2547826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2548826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek if (!FS.consumesDataArgument()) { 2549826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // FIXME: Technically specifying a precision or field width here 2550826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // makes no sense. Worth issuing a warning at some point. 2551826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek return true; 2552826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek } 2553826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2554826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // Consume the argument. 2555826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek unsigned argIndex = FS.getArgIndex(); 2556826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek if (argIndex < NumDataArgs) { 2557826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // The check to see if the argIndex is valid will come later. 2558826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // We set the bit here because we may exit early from this 2559826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // function if we encounter some other error. 2560826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek CoveredArgs.set(argIndex); 2561826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek } 2562826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 25631e51c200b17cb6b0238c10a8760ae04765688f16Ted Kremenek // Check the length modifier is valid with the given conversion specifier. 25641e51c200b17cb6b0238c10a8760ae04765688f16Ted Kremenek const LengthModifier &LM = FS.getLengthModifier(); 25651e51c200b17cb6b0238c10a8760ae04765688f16Ted Kremenek if (!FS.hasValidLengthModifier()) { 2566220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas const CharSourceRange &R = getSpecifierRange(LM.getStart(), LM.getLength()); 2567220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length) 2568220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas << LM.toString() << CS.toString() 2569220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas << getSpecifierRange(startSpecifier, specifierLen), 2570220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas getLocationOfByte(LM.getStart()), 2571220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas /*IsStringLocation*/true, R, 2572220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas FixItHint::CreateRemoval(R)); 25731e51c200b17cb6b0238c10a8760ae04765688f16Ted Kremenek } 25741e51c200b17cb6b0238c10a8760ae04765688f16Ted Kremenek 257576517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg if (!FS.hasStandardLengthModifier()) 257676517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen); 25774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 257876517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 257976517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg if (!FS.hasStandardLengthConversionCombination()) 258076517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg HandleNonStandardConversionSpecification(LM, CS, startSpecifier, 258176517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg specifierLen); 258276517426dc8bf7734c07eefc35171a6bfdba1a2bHans Wennborg 2583826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek // The remaining checks depend on the data arguments. 2584826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek if (HasVAListArg) 2585826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek return true; 2586826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2587666a197deb75d95c78ddd39274af1a54240828d8Ted Kremenek if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 2588826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek return false; 2589826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 25906fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg // Check that the argument type matches the format specifier. 25916fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg const Expr *Ex = getDataArg(argIndex); 25926fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg const analyze_scanf::ScanfArgTypeResult &ATR = FS.getArgType(S.Context); 25936fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { 25946fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg ScanfSpecifier fixedFS = FS; 25954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(), 2596be6126a2a784e1446460b8d15c2b26f880c871fcHans Wennborg S.Context); 25976fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg 25986fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg if (success) { 25996fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg // Get the fix string from the fixed format specifier. 2600f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith SmallString<128> buf; 26016fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg llvm::raw_svector_ostream os(buf); 26026fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg fixedFS.toString(os); 26036fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg 26046fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg EmitFormatDiagnostic( 26056fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) 26066fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() 26076fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg << Ex->getSourceRange(), 2608abf145ab57fc56bfb9c48333fcc65b49a564dc60Matt Beaumont-Gay Ex->getLocStart(), 2609abf145ab57fc56bfb9c48333fcc65b49a564dc60Matt Beaumont-Gay /*IsStringLocation*/false, 26106fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg getSpecifierRange(startSpecifier, specifierLen), 26116fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg FixItHint::CreateReplacement( 26126fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg getSpecifierRange(startSpecifier, specifierLen), 26136fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg os.str())); 26146fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg } else { 2615220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas EmitFormatDiagnostic( 2616220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) 26176fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() 2618220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas << Ex->getSourceRange(), 2619abf145ab57fc56bfb9c48333fcc65b49a564dc60Matt Beaumont-Gay Ex->getLocStart(), 2620abf145ab57fc56bfb9c48333fcc65b49a564dc60Matt Beaumont-Gay /*IsStringLocation*/false, 2621220947bd5a66b0f25f5e74d6029769dfb34f07b4Jean-Daniel Dupas getSpecifierRange(startSpecifier, specifierLen)); 26226fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg } 26236fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg } 26246fcd932dfd6835f70cc00d6f7c6789793f6d7b66Hans Wennborg 2625826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek return true; 262607d161f38e708a91486bf1c031d525faebbb249dTed Kremenek} 2627e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek 2628826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenekvoid Sema::CheckFormatString(const StringLiteral *FExpr, 26290e5675de2b67eeca754754ae8d3dacba425a38e8Ted Kremenek const Expr *OrigFormatExpr, 263029c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Expr **Args, unsigned NumArgs, 263129c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas bool HasVAListArg, unsigned format_idx, 263234269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas unsigned firstDataArg, FormatStringType Type, 263329c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas bool inFunctionCall) { 2634826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2635e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek // CHECK: is the format string a wide literal? 2636df9ef1bc8c3780307ab2ed81bf5e31c23310b936Richard Smith if (!FExpr->isAscii() && !FExpr->isUTF8()) { 263755733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu CheckFormatHandler::EmitFormatDiagnostic( 263829c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas *this, inFunctionCall, Args[format_idx], 263955733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), 264055733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); 2641e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek return; 2642e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek } 2643826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2644e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek // Str - The format string. NOTE: this is NOT null-terminated! 26455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef StrRef = FExpr->getString(); 26462f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer const char *Str = StrRef.data(); 26472f4eaef37476ae6891ede8ba215d0f6fd093629bBenjamin Kramer unsigned StrLen = StrRef.size(); 264829c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas const unsigned numDataArgs = NumArgs - firstDataArg; 2649826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2650e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek // CHECK: empty format string? 26514cd5791f4aa6deb572979bb38c1deedbc155efe0Ted Kremenek if (StrLen == 0 && numDataArgs > 0) { 265255733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu CheckFormatHandler::EmitFormatDiagnostic( 265329c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas *this, inFunctionCall, Args[format_idx], 265455733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), 265555733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); 2656e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek return; 2657e0e5313c25f3870d0d18fc67e1b3a3a0e1ef8e07Ted Kremenek } 2658826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 265934269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas if (Type == FST_Printf || Type == FST_NSString) { 2660826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, 266150687314f87b67073c202fe46d84d12fb55e25cfJordan Rose numDataArgs, (Type == FST_NSString), 266229c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Str, HasVAListArg, Args, NumArgs, format_idx, 266355733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu inFunctionCall); 2664826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2665d02deebce5f1b283101e035a7f5d5bab0d2068ecHans Wennborg if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, 26664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie getLangOpts())) 2667826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek H.DoneProcessing(); 266834269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas } else if (Type == FST_Scanf) { 266950687314f87b67073c202fe46d84d12fb55e25cfJordan Rose CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, 267029c3f814b64808c6dac4597b61a50ceecdf141fcJean-Daniel Dupas Str, HasVAListArg, Args, NumArgs, format_idx, 267155733de16f492dadbf5f4d2809d6c610ba6d86aeRichard Trieu inFunctionCall); 2672826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek 2673d02deebce5f1b283101e035a7f5d5bab0d2068ecHans Wennborg if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, 26744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie getLangOpts())) 2675826a3457f737f1fc45a22954fd1bfde38160c165Ted Kremenek H.DoneProcessing(); 267634269df5db40b7c4b4f52aed579d9b3108ff79e4Jean-Daniel Dupas } // TODO: handle other formats 2677ce7024e8a3793b05861a4904ecdb1272924ada14Ted Kremenek} 2678ce7024e8a3793b05861a4904ecdb1272924ada14Ted Kremenek 26797ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth//===--- CHECK: Standard memory functions ---------------------------------===// 26807ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth 26812a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor/// \brief Determine whether the given type is a dynamic class type (e.g., 26822a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor/// whether it has a vtable). 26832a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregorstatic bool isDynamicClassType(QualType T) { 26842a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) 26852a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor if (CXXRecordDecl *Definition = Record->getDefinition()) 26862a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor if (Definition->isDynamicClass()) 26872a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor return true; 26882a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor 26892a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor return false; 26902a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor} 26912a053a33707eba9e642adedb557b1a6aace103b5Douglas Gregor 2692a72a12f3baed07f2663e48c5fc3b670c68dffe0cChandler Carruth/// \brief If E is a sizeof expression, returns its argument expression, 2693000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth/// otherwise returns NULL. 2694000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruthstatic const Expr *getSizeOfExprArg(const Expr* E) { 2695e4a1c64700304459ac436fe29cb498f2da3b6194Nico Weber if (const UnaryExprOrTypeTraitExpr *SizeOf = 2696000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth dyn_cast<UnaryExprOrTypeTraitExpr>(E)) 2697000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) 2698000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); 2699e4a1c64700304459ac436fe29cb498f2da3b6194Nico Weber 2700000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth return 0; 2701000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth} 2702000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth 2703a72a12f3baed07f2663e48c5fc3b670c68dffe0cChandler Carruth/// \brief If E is a sizeof expression, returns its argument type. 2704000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruthstatic QualType getSizeOfArgType(const Expr* E) { 2705000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (const UnaryExprOrTypeTraitExpr *SizeOf = 2706000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth dyn_cast<UnaryExprOrTypeTraitExpr>(E)) 2707000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (SizeOf->getKind() == clang::UETT_SizeOf) 2708000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth return SizeOf->getTypeOfArgument(); 2709000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth 2710000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth return QualType(); 2711e4a1c64700304459ac436fe29cb498f2da3b6194Nico Weber} 2712e4a1c64700304459ac436fe29cb498f2da3b6194Nico Weber 27137ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth/// \brief Check for dangerous or invalid arguments to memset(). 27147ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth/// 2715929f0135ab60166202f52e0dd19835c0490d792aChandler Carruth/// This issues warnings on known problematic, dangerous or unspecified 2716cc2f30c4ed7770b6005bd55b529a55c1fcc250fcMatt Beaumont-Gay/// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' 2717cc2f30c4ed7770b6005bd55b529a55c1fcc250fcMatt Beaumont-Gay/// function calls. 27187ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth/// 27197ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth/// \param Call The call expression to diagnose. 2720cc2f30c4ed7770b6005bd55b529a55c1fcc250fcMatt Beaumont-Gayvoid Sema::CheckMemaccessArguments(const CallExpr *Call, 27210a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks unsigned BId, 2722cc2f30c4ed7770b6005bd55b529a55c1fcc250fcMatt Beaumont-Gay IdentifierInfo *FnName) { 27230a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks assert(BId != 0); 27240a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks 27251d59f7f8fb39fa44ddbb6e2abd321af863b1f55bTed Kremenek // It is possible to have a non-standard definition of memset. Validate 2726707a23e8b87410332b55bb4534fa1c799edef38aDouglas Gregor // we have enough arguments, and if not, abort further checking. 27270a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); 2728cda57822327499aeb6fe606f9cf5903cffca3444Nico Weber if (Call->getNumArgs() < ExpectedNumArgs) 27291d59f7f8fb39fa44ddbb6e2abd321af863b1f55bTed Kremenek return; 27301d59f7f8fb39fa44ddbb6e2abd321af863b1f55bTed Kremenek 27310a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks unsigned LastArg = (BId == Builtin::BImemset || 27320a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks BId == Builtin::BIstrndup ? 1 : 2); 27330a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); 2734cda57822327499aeb6fe606f9cf5903cffca3444Nico Weber const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); 2735000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth 2736000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // We have special checking when the length is a sizeof expression. 2737000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth QualType SizeOfArgTy = getSizeOfArgType(LenExpr); 2738000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); 2739000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth llvm::FoldingSetNodeID SizeOfArgID; 2740000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth 274106bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { 274206bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); 2743e4a1c64700304459ac436fe29cb498f2da3b6194Nico Weber SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); 274406bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor 274506bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor QualType DestTy = Dest->getType(); 274606bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { 274706bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor QualType PointeeTy = DestPtrTy->getPointeeType(); 2748f85e193739c953358c865005855253af4f68a497John McCall 2749000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // Never warn about void type pointers. This can be used to suppress 2750000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // false positives. 2751000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (PointeeTy->isVoidType()) 275206bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor continue; 275306bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor 2754000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by 2755000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // actually comparing the expressions for equality. Because computing the 2756000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // expression IDs can be expensive, we only do this if the diagnostic is 2757000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // enabled. 2758000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (SizeOfArg && 2759000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess, 2760000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth SizeOfArg->getExprLoc())) { 2761000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // We only compute IDs for expressions if the warning is enabled, and 2762000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // cache the sizeof arg's ID. 2763000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (SizeOfArgID == llvm::FoldingSetNodeID()) 2764000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth SizeOfArg->Profile(SizeOfArgID, Context, true); 2765000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth llvm::FoldingSetNodeID DestID; 2766000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth Dest->Profile(DestID, Context, true); 2767000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (DestID == SizeOfArgID) { 2768cda57822327499aeb6fe606f9cf5903cffca3444Nico Weber // TODO: For strncpy() and friends, this could suggest sizeof(dst) 2769cda57822327499aeb6fe606f9cf5903cffca3444Nico Weber // over sizeof(src) as well. 2770000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth unsigned ActionIdx = 0; // Default is to suggest dereferencing. 27716fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks StringRef ReadableName = FnName->getName(); 27726fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks 2773000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) 277490c78328e70cb376754edf87708505a84c044271Anna Zaks if (UnaryOp->getOpcode() == UO_AddrOf) 2775000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth ActionIdx = 1; // If its an address-of operator, just remove it. 2776000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (Context.getTypeSize(PointeeTy) == Context.getCharWidth()) 2777000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth ActionIdx = 2; // If the pointee's size is sizeof(char), 2778000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // suggest an explicit length. 27796fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks 27806fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks // If the function is defined as a builtin macro, do not show macro 27816fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks // expansion. 27826fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks SourceLocation SL = SizeOfArg->getExprLoc(); 27836fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks SourceRange DSR = Dest->getSourceRange(); 27846fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks SourceRange SSR = SizeOfArg->getSourceRange(); 27856fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks SourceManager &SM = PP.getSourceManager(); 27866fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks 27876fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks if (SM.isMacroArgExpansion(SL)) { 27886fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); 27896fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks SL = SM.getSpellingLoc(SL); 27906fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), 27916fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks SM.getSpellingLoc(DSR.getEnd())); 27926fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), 27936fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks SM.getSpellingLoc(SSR.getEnd())); 27946fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks } 27956fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks 279690c78328e70cb376754edf87708505a84c044271Anna Zaks DiagRuntimeBehavior(SL, SizeOfArg, 2797000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth PDiag(diag::warn_sizeof_pointer_expr_memaccess) 27986fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks << ReadableName 279990c78328e70cb376754edf87708505a84c044271Anna Zaks << PointeeTy 280090c78328e70cb376754edf87708505a84c044271Anna Zaks << DestTy 28016fcb3727e31280ba816dc86d024586b8c5933c13Anna Zaks << DSR 280290c78328e70cb376754edf87708505a84c044271Anna Zaks << SSR); 280390c78328e70cb376754edf87708505a84c044271Anna Zaks DiagRuntimeBehavior(SL, SizeOfArg, 280490c78328e70cb376754edf87708505a84c044271Anna Zaks PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) 280590c78328e70cb376754edf87708505a84c044271Anna Zaks << ActionIdx 280690c78328e70cb376754edf87708505a84c044271Anna Zaks << SSR); 280790c78328e70cb376754edf87708505a84c044271Anna Zaks 2808000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth break; 2809000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth } 2810000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth } 2811000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth 2812000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // Also check for cases where the sizeof argument is the exact same 2813000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // type as the memory argument, and where it points to a user-defined 2814000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth // record type. 2815000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (SizeOfArgTy != QualType()) { 2816000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth if (PointeeTy->isRecordType() && 2817000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth Context.typesAreCompatible(SizeOfArgTy, DestTy)) { 2818000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, 2819000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth PDiag(diag::warn_sizeof_pointer_type_memaccess) 2820000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth << FnName << SizeOfArgTy << ArgIdx 2821000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth << PointeeTy << Dest->getSourceRange() 2822000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth << LenExpr->getSourceRange()); 2823000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth break; 2824000d428347f352979e0f6dffcf0a64e73af0a2b5Chandler Carruth } 2825e4a1c64700304459ac436fe29cb498f2da3b6194Nico Weber } 2826e4a1c64700304459ac436fe29cb498f2da3b6194Nico Weber 282706bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor // Always complain about dynamic classes. 28280a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks if (isDynamicClassType(PointeeTy)) { 28290a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks 28300a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks unsigned OperationType = 0; 28310a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks // "overwritten" if we're warning about the destination for any call 28320a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks // but memcmp; otherwise a verb appropriate to the call. 28330a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks if (ArgIdx != 0 || BId == Builtin::BImemcmp) { 28340a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks if (BId == Builtin::BImemcpy) 28350a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks OperationType = 1; 28360a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks else if(BId == Builtin::BImemmove) 28370a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks OperationType = 2; 28380a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks else if (BId == Builtin::BImemcmp) 28390a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks OperationType = 3; 28400a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks } 28410a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks 28425c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay DiagRuntimeBehavior( 28435c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay Dest->getExprLoc(), Dest, 28445c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay PDiag(diag::warn_dyn_class_memaccess) 28450a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) 2846d9b859a74ecaede23a78d37f364498102ef418c9Anna Zaks << FnName << PointeeTy 28470a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks << OperationType 28485c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay << Call->getCallee()->getSourceRange()); 28490a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks } else if (PointeeTy.hasNonTrivialObjCLifetime() && 28500a151a137a68bb656acbcce7ff2407613bb80cfcAnna Zaks BId != Builtin::BImemset) 28515c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay DiagRuntimeBehavior( 28525c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay Dest->getExprLoc(), Dest, 28535c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay PDiag(diag::warn_arc_object_memaccess) 28545c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay << ArgIdx << FnName << PointeeTy 28555c5218e188b39b2e93b541156a69ef061e11aab8Matt Beaumont-Gay << Call->getCallee()->getSourceRange()); 2856f85e193739c953358c865005855253af4f68a497John McCall else 285706bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor continue; 2858f85e193739c953358c865005855253af4f68a497John McCall 2859f85e193739c953358c865005855253af4f68a497John McCall DiagRuntimeBehavior( 2860f85e193739c953358c865005855253af4f68a497John McCall Dest->getExprLoc(), Dest, 2861929f0135ab60166202f52e0dd19835c0490d792aChandler Carruth PDiag(diag::note_bad_memaccess_silence) 286206bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); 286306bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor break; 286406bc9eb9908e42696775b395b290827bde468c8bDouglas Gregor } 28657ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth } 28667ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth} 28677ccc95bceebb2d1c8fbe277c9e33bde7dc1ccbb1Chandler Carruth 2868bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek// A little helper routine: ignore addition and subtraction of integer literals. 2869bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek// This intentionally does not ignore all integer constant expressions because 2870bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek// we don't want to remove sizeof(). 2871bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenekstatic const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { 2872bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek Ex = Ex->IgnoreParenCasts(); 2873bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2874bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek for (;;) { 2875bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); 2876bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek if (!BO || !BO->isAdditiveOp()) 2877bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek break; 2878bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2879bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); 2880bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); 2881bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2882bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek if (isa<IntegerLiteral>(RHS)) 2883bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek Ex = LHS; 2884bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek else if (isa<IntegerLiteral>(LHS)) 2885bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek Ex = RHS; 2886bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek else 2887bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek break; 2888bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek } 2889bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2890bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek return Ex; 2891bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek} 2892bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2893bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek// Warn if the user has made the 'size' argument to strlcpy or strlcat 2894bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek// be the size of the source, instead of the destination. 2895bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenekvoid Sema::CheckStrlcpycatArguments(const CallExpr *Call, 2896bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek IdentifierInfo *FnName) { 2897bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2898bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // Don't crash if the user has the wrong number of arguments 2899bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek if (Call->getNumArgs() != 3) 2900bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek return; 2901bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2902bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); 2903bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); 2904bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const Expr *CompareWithSrc = NULL; 2905bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2906bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // Look for 'strlcpy(dst, x, sizeof(x))' 2907bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek if (const Expr *Ex = getSizeOfExprArg(SizeArg)) 2908bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek CompareWithSrc = Ex; 2909bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek else { 2910bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // Look for 'strlcpy(dst, x, strlen(x))' 2911bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { 2912180f47959a066795cc0f409433023af448bb0328Richard Smith if (SizeCall->isBuiltinCall() == Builtin::BIstrlen 2913bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek && SizeCall->getNumArgs() == 1) 2914bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); 2915bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek } 2916bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek } 2917bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2918bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek if (!CompareWithSrc) 2919bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek return; 2920bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2921bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // Determine if the argument to sizeof/strlen is equal to the source 2922bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // argument. In principle there's all kinds of things you could do 2923bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // here, for instance creating an == expression and evaluating it with 2924bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // EvaluateAsBooleanCondition, but this uses a more direct technique: 2925bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); 2926bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek if (!SrcArgDRE) 2927bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek return; 2928bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2929bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); 2930bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek if (!CompareWithSrcDRE || 2931bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) 2932bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek return; 2933bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2934bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const Expr *OriginalSizeArg = Call->getArg(2); 2935bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) 2936bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek << OriginalSizeArg->getSourceRange() << FnName; 2937bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2938bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // Output a FIXIT hint if the destination is an array (rather than a 2939bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // pointer to an array). This could be enhanced to handle some 2940bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // pointers if we know the actual size, like if DstArg is 'array+2' 2941bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek // we could say 'sizeof(array)-2'. 2942bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); 29438f7462237ff542a4556775f98709ed300aa1731aTed Kremenek QualType DstArgTy = DstArg->getType(); 2944bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 29458f7462237ff542a4556775f98709ed300aa1731aTed Kremenek // Only handle constant-sized or VLAs, but not flexible members. 29468f7462237ff542a4556775f98709ed300aa1731aTed Kremenek if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) { 29478f7462237ff542a4556775f98709ed300aa1731aTed Kremenek // Only issue the FIXIT for arrays of size > 1. 29488f7462237ff542a4556775f98709ed300aa1731aTed Kremenek if (CAT->getSize().getSExtValue() <= 1) 29498f7462237ff542a4556775f98709ed300aa1731aTed Kremenek return; 29508f7462237ff542a4556775f98709ed300aa1731aTed Kremenek } else if (!DstArgTy->isVariableArrayType()) { 29518f7462237ff542a4556775f98709ed300aa1731aTed Kremenek return; 2952bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek } 29538f7462237ff542a4556775f98709ed300aa1731aTed Kremenek 2954f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith SmallString<128> sizeString; 29558f7462237ff542a4556775f98709ed300aa1731aTed Kremenek llvm::raw_svector_ostream OS(sizeString); 29568f7462237ff542a4556775f98709ed300aa1731aTed Kremenek OS << "sizeof("; 29578987b2385d9ba63ada66e1344ace79b04d5cb5c3Douglas Gregor DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); 29588f7462237ff542a4556775f98709ed300aa1731aTed Kremenek OS << ")"; 29598f7462237ff542a4556775f98709ed300aa1731aTed Kremenek 29608f7462237ff542a4556775f98709ed300aa1731aTed Kremenek Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) 29618f7462237ff542a4556775f98709ed300aa1731aTed Kremenek << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), 29628f7462237ff542a4556775f98709ed300aa1731aTed Kremenek OS.str()); 2963bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek} 2964bd5da9d1a711e90b3bc265b4798954085c0b48d9Ted Kremenek 2965c36bedc90c687caa71748480c60707ea4608b092Anna Zaks/// Check if two expressions refer to the same declaration. 2966c36bedc90c687caa71748480c60707ea4608b092Anna Zaksstatic bool referToTheSameDecl(const Expr *E1, const Expr *E2) { 2967c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) 2968c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) 2969c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return D1->getDecl() == D2->getDecl(); 2970c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return false; 2971c36bedc90c687caa71748480c60707ea4608b092Anna Zaks} 2972c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 2973c36bedc90c687caa71748480c60707ea4608b092Anna Zaksstatic const Expr *getStrlenExprArg(const Expr *E) { 2974c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 2975c36bedc90c687caa71748480c60707ea4608b092Anna Zaks const FunctionDecl *FD = CE->getDirectCallee(); 2976c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) 2977c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return 0; 2978c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return CE->getArg(0)->IgnoreParenCasts(); 2979c36bedc90c687caa71748480c60707ea4608b092Anna Zaks } 2980c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return 0; 2981c36bedc90c687caa71748480c60707ea4608b092Anna Zaks} 2982c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 2983c36bedc90c687caa71748480c60707ea4608b092Anna Zaks// Warn on anti-patterns as the 'size' argument to strncat. 2984c36bedc90c687caa71748480c60707ea4608b092Anna Zaks// The correct size argument should look like following: 2985c36bedc90c687caa71748480c60707ea4608b092Anna Zaks// strncat(dst, src, sizeof(dst) - strlen(dest) - 1); 2986c36bedc90c687caa71748480c60707ea4608b092Anna Zaksvoid Sema::CheckStrncatArguments(const CallExpr *CE, 2987c36bedc90c687caa71748480c60707ea4608b092Anna Zaks IdentifierInfo *FnName) { 2988c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // Don't crash if the user has the wrong number of arguments. 2989c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (CE->getNumArgs() < 3) 2990c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return; 2991c36bedc90c687caa71748480c60707ea4608b092Anna Zaks const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); 2992c36bedc90c687caa71748480c60707ea4608b092Anna Zaks const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); 2993c36bedc90c687caa71748480c60707ea4608b092Anna Zaks const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); 2994c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 2995c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // Identify common expressions, which are wrongly used as the size argument 2996c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // to strncat and may lead to buffer overflows. 2997c36bedc90c687caa71748480c60707ea4608b092Anna Zaks unsigned PatternType = 0; 2998c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { 2999c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // - sizeof(dst) 3000c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (referToTheSameDecl(SizeOfArg, DstArg)) 3001c36bedc90c687caa71748480c60707ea4608b092Anna Zaks PatternType = 1; 3002c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // - sizeof(src) 3003c36bedc90c687caa71748480c60707ea4608b092Anna Zaks else if (referToTheSameDecl(SizeOfArg, SrcArg)) 3004c36bedc90c687caa71748480c60707ea4608b092Anna Zaks PatternType = 2; 3005c36bedc90c687caa71748480c60707ea4608b092Anna Zaks } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { 3006c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (BE->getOpcode() == BO_Sub) { 3007c36bedc90c687caa71748480c60707ea4608b092Anna Zaks const Expr *L = BE->getLHS()->IgnoreParenCasts(); 3008c36bedc90c687caa71748480c60707ea4608b092Anna Zaks const Expr *R = BE->getRHS()->IgnoreParenCasts(); 3009c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // - sizeof(dst) - strlen(dst) 3010c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && 3011c36bedc90c687caa71748480c60707ea4608b092Anna Zaks referToTheSameDecl(DstArg, getStrlenExprArg(R))) 3012c36bedc90c687caa71748480c60707ea4608b092Anna Zaks PatternType = 1; 3013c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // - sizeof(src) - (anything) 3014c36bedc90c687caa71748480c60707ea4608b092Anna Zaks else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) 3015c36bedc90c687caa71748480c60707ea4608b092Anna Zaks PatternType = 2; 3016c36bedc90c687caa71748480c60707ea4608b092Anna Zaks } 3017c36bedc90c687caa71748480c60707ea4608b092Anna Zaks } 3018c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 3019c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (PatternType == 0) 3020c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return; 3021c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 3022afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks // Generate the diagnostic. 3023afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks SourceLocation SL = LenArg->getLocStart(); 3024afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks SourceRange SR = LenArg->getSourceRange(); 3025afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks SourceManager &SM = PP.getSourceManager(); 3026afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks 3027afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks // If the function is defined as a builtin macro, do not show macro expansion. 3028afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks if (SM.isMacroArgExpansion(SL)) { 3029afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks SL = SM.getSpellingLoc(SL); 3030afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), 3031afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks SM.getSpellingLoc(SR.getEnd())); 3032afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks } 3033afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks 3034c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (PatternType == 1) 3035afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks Diag(SL, diag::warn_strncat_large_size) << SR; 3036c36bedc90c687caa71748480c60707ea4608b092Anna Zaks else 3037afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks Diag(SL, diag::warn_strncat_src_size) << SR; 3038c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 3039c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // Output a FIXIT hint if the destination is an array (rather than a 3040c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // pointer to an array). This could be enhanced to handle some 3041c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // pointers if we know the actual size, like if DstArg is 'array+2' 3042c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // we could say 'sizeof(array)-2'. 3043c36bedc90c687caa71748480c60707ea4608b092Anna Zaks QualType DstArgTy = DstArg->getType(); 3044c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 3045c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // Only handle constant-sized or VLAs, but not flexible members. 3046c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) { 3047c36bedc90c687caa71748480c60707ea4608b092Anna Zaks // Only issue the FIXIT for arrays of size > 1. 3048c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (CAT->getSize().getSExtValue() <= 1) 3049c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return; 3050c36bedc90c687caa71748480c60707ea4608b092Anna Zaks } else if (!DstArgTy->isVariableArrayType()) { 3051c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return; 3052c36bedc90c687caa71748480c60707ea4608b092Anna Zaks } 3053c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 3054f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith SmallString<128> sizeString; 3055c36bedc90c687caa71748480c60707ea4608b092Anna Zaks llvm::raw_svector_ostream OS(sizeString); 3056c36bedc90c687caa71748480c60707ea4608b092Anna Zaks OS << "sizeof("; 3057c36bedc90c687caa71748480c60707ea4608b092Anna Zaks DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); 3058c36bedc90c687caa71748480c60707ea4608b092Anna Zaks OS << ") - "; 3059c36bedc90c687caa71748480c60707ea4608b092Anna Zaks OS << "strlen("; 3060c36bedc90c687caa71748480c60707ea4608b092Anna Zaks DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); 3061c36bedc90c687caa71748480c60707ea4608b092Anna Zaks OS << ") - 1"; 3062c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 3063afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks Diag(SL, diag::note_strncat_wrong_size) 3064afdb041308bca3e875b23e7a22d879e9039bff03Anna Zaks << FixItHint::CreateReplacement(SR, OS.str()); 3065c36bedc90c687caa71748480c60707ea4608b092Anna Zaks} 3066c36bedc90c687caa71748480c60707ea4608b092Anna Zaks 306706de276fff91264437fa75111ed76de43097e089Ted Kremenek//===--- CHECK: Return Address of Stack Variable --------------------------===// 306806de276fff91264437fa75111ed76de43097e089Ted Kremenek 3069e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidisstatic Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, 3070e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis Decl *ParentDecl); 3071e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidisstatic Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, 3072e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis Decl *ParentDecl); 307306de276fff91264437fa75111ed76de43097e089Ted Kremenek 307406de276fff91264437fa75111ed76de43097e089Ted Kremenek/// CheckReturnStackAddr - Check if a return statement returns the address 307506de276fff91264437fa75111ed76de43097e089Ted Kremenek/// of a stack variable. 307606de276fff91264437fa75111ed76de43097e089Ted Kremenekvoid 307706de276fff91264437fa75111ed76de43097e089Ted KremenekSema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, 307806de276fff91264437fa75111ed76de43097e089Ted Kremenek SourceLocation ReturnLoc) { 30791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 308026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis Expr *stackE = 0; 30815f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<DeclRefExpr *, 8> refVars; 30824e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 308326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // Perform checking for returned stack addresses, local blocks, 308426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // label addresses or references to temporaries. 3085f85e193739c953358c865005855253af4f68a497John McCall if (lhsType->isPointerType() || 30864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { 3087e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/0); 3088ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump } else if (lhsType->isReferenceType()) { 3089e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/0); 309026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } 309126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 309226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (stackE == 0) 309326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis return; // Nothing suspicious was found. 309426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 309526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis SourceLocation diagLoc; 309626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis SourceRange diagRange; 309726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (refVars.empty()) { 309826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis diagLoc = stackE->getLocStart(); 309926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis diagRange = stackE->getSourceRange(); 310026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } else { 310126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // We followed through a reference variable. 'stackE' contains the 310226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // problematic expression but we will warn at the return statement pointing 310326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // at the reference variable. We will later display the "trail" of 310426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // reference variables using notes. 310526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis diagLoc = refVars[0]->getLocStart(); 310626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis diagRange = refVars[0]->getSourceRange(); 310726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } 310826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 310926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. 311026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref 311126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis : diag::warn_ret_stack_addr) 311226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis << DR->getDecl()->getDeclName() << diagRange; 311326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } else if (isa<BlockExpr>(stackE)) { // local block. 311426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis Diag(diagLoc, diag::err_ret_local_block) << diagRange; 311526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } else if (isa<AddrLabelExpr>(stackE)) { // address of label. 311626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; 311726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } else { // local temporary. 311826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref 311926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis : diag::warn_ret_local_temp_addr) 312026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis << diagRange; 312126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } 312226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 312326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // Display the "trail" of reference variables that we followed until we 312426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // found the problematic expression using notes. 312526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis for (unsigned i = 0, e = refVars.size(); i != e; ++i) { 312626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); 312726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // If this var binds to another reference var, show the range of the next 312826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // var, otherwise the var binds to the problematic expression, in which case 312926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // show the range of the expression. 313026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() 313126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis : stackE->getSourceRange(); 313226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis Diag(VD->getLocation(), diag::note_ref_var_local_bind) 313326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis << VD->getDeclName() << range; 313406de276fff91264437fa75111ed76de43097e089Ted Kremenek } 313506de276fff91264437fa75111ed76de43097e089Ted Kremenek} 313606de276fff91264437fa75111ed76de43097e089Ted Kremenek 313706de276fff91264437fa75111ed76de43097e089Ted Kremenek/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that 313806de276fff91264437fa75111ed76de43097e089Ted Kremenek/// check if the expression in a return statement evaluates to an address 313926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// to a location on the stack, a local block, an address of a label, or a 314026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// reference to local temporary. The recursion is used to traverse the 314106de276fff91264437fa75111ed76de43097e089Ted Kremenek/// AST of the return expression, with recursion backtracking when we 314226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// encounter a subexpression that (1) clearly does not lead to one of the 314326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// above problematic expressions (2) is something we cannot determine leads to 314426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// a problematic expression based on such local checking. 314526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// 314626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// Both EvalAddr and EvalVal follow through reference variables to evaluate 314726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// the expression that they point to. Such variables are added to the 314826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// 'refVars' vector so that we know what the reference variable "trail" was. 314906de276fff91264437fa75111ed76de43097e089Ted Kremenek/// 3150e8c600f9fedf2cfd69cdd2cb4bde4a9b39ce2873Ted Kremenek/// EvalAddr processes expressions that are pointers that are used as 3151e8c600f9fedf2cfd69cdd2cb4bde4a9b39ce2873Ted Kremenek/// references (and not L-values). EvalVal handles all other values. 315226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// At the base case of the recursion is a check for the above problematic 315326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis/// expressions. 315406de276fff91264437fa75111ed76de43097e089Ted Kremenek/// 315506de276fff91264437fa75111ed76de43097e089Ted Kremenek/// This implementation handles: 315606de276fff91264437fa75111ed76de43097e089Ted Kremenek/// 315706de276fff91264437fa75111ed76de43097e089Ted Kremenek/// * pointer-to-pointer casts 315806de276fff91264437fa75111ed76de43097e089Ted Kremenek/// * implicit conversions from array references to pointers 315906de276fff91264437fa75111ed76de43097e089Ted Kremenek/// * taking the address of fields 316006de276fff91264437fa75111ed76de43097e089Ted Kremenek/// * arbitrary interplay between "&" and "*" operators 316106de276fff91264437fa75111ed76de43097e089Ted Kremenek/// * pointer arithmetic from an address of a stack variable 316206de276fff91264437fa75111ed76de43097e089Ted Kremenek/// * taking the address of an array element where the array is on the stack 3163e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidisstatic Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, 3164e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis Decl *ParentDecl) { 316526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (E->isTypeDependent()) 316626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis return NULL; 316726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 316806de276fff91264437fa75111ed76de43097e089Ted Kremenek // We should only be called for evaluating pointer expressions. 31690f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall assert((E->getType()->isAnyPointerType() || 3170dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff E->getType()->isBlockPointerType() || 3171a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek E->getType()->isObjCQualifiedIdType()) && 3172fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner "EvalAddr only works on pointers"); 31731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3174f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne E = E->IgnoreParens(); 3175f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne 317606de276fff91264437fa75111ed76de43097e089Ted Kremenek // Our "symbolic interpreter" is just a dispatch off the currently 317706de276fff91264437fa75111ed76de43097e089Ted Kremenek // viewed AST node. We then recursively traverse the AST by calling 317806de276fff91264437fa75111ed76de43097e089Ted Kremenek // EvalAddr and EvalVal appropriately. 317906de276fff91264437fa75111ed76de43097e089Ted Kremenek switch (E->getStmtClass()) { 318026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis case Stmt::DeclRefExprClass: { 318126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis DeclRefExpr *DR = cast<DeclRefExpr>(E); 318226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 318326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) 318426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // If this is a reference variable, follow through to the expression that 318526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // it points to. 318626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (V->hasLocalStorage() && 318726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis V->getType()->isReferenceType() && V->hasInit()) { 318826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // Add the reference variable to the "trail". 318926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis refVars.push_back(DR); 3190e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalAddr(V->getInit(), refVars, ParentDecl); 319126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } 319226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 319326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis return NULL; 319426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } 319506de276fff91264437fa75111ed76de43097e089Ted Kremenek 3196fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner case Stmt::UnaryOperatorClass: { 3197fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // The only unary operator that make sense to handle here 3198fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // is AddrOf. All others don't make sense as pointers. 3199fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner UnaryOperator *U = cast<UnaryOperator>(E); 32001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 32012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (U->getOpcode() == UO_AddrOf) 3202e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalVal(U->getSubExpr(), refVars, ParentDecl); 3203fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner else 3204fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner return NULL; 3205fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner } 32061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3207fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner case Stmt::BinaryOperatorClass: { 3208fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // Handle pointer arithmetic. All other binary operators are not valid 3209fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // in this context. 3210fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner BinaryOperator *B = cast<BinaryOperator>(E); 32112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall BinaryOperatorKind op = B->getOpcode(); 32121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 32132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (op != BO_Add && op != BO_Sub) 3214fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner return NULL; 32151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3216fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner Expr *Base = B->getLHS(); 32173907323dd6665c0c4e383435cb145233f4533406Anders Carlsson 3218fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // Determine which argument is the real pointer base. It could be 3219fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // the RHS argument instead of the LHS. 3220fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner if (!Base->getType()->isPointerType()) Base = B->getRHS(); 32211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3222fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner assert (Base->getType()->isPointerType()); 3223e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalAddr(Base, refVars, ParentDecl); 3224fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner } 322561f40a2b67fc2046768e14f66b617e564cbcc3d8Steve Naroff 3226fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // For conditional operators we need to see if either the LHS or RHS are 3227fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // valid DeclRefExpr*s. If one of them is valid, we return it. 3228fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner case Stmt::ConditionalOperatorClass: { 3229fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner ConditionalOperator *C = cast<ConditionalOperator>(E); 32301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3231fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // Handle the GNU extension for missing LHS. 32329ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor if (Expr *lhsExpr = C->getLHS()) { 32339ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor // In C++, we can have a throw-expression, which has 'void' type. 32349ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor if (!lhsExpr->getType()->isVoidType()) 3235e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis if (Expr* LHS = EvalAddr(lhsExpr, refVars, ParentDecl)) 32369ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor return LHS; 32379ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor } 32389ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor 32399ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor // In C++, we can have a throw-expression, which has 'void' type. 32409ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor if (C->getRHS()->getType()->isVoidType()) 32419ee5ee84f0ad8246bff105786a128c7a909fdf12Douglas Gregor return NULL; 324206de276fff91264437fa75111ed76de43097e089Ted Kremenek 3243e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalAddr(C->getRHS(), refVars, ParentDecl); 3244fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner } 324526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 324626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis case Stmt::BlockExprClass: 3247469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) 324826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis return E; // local block. 324926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis return NULL; 325026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 325126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis case Stmt::AddrLabelExprClass: 325226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis return E; // address of label. 32531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 325480ee6e878a169e6255d4686a91bb696151ff229fJohn McCall case Stmt::ExprWithCleanupsClass: 3255e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, 3256e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis ParentDecl); 325780ee6e878a169e6255d4686a91bb696151ff229fJohn McCall 325854b5274f2c190331438375ad114dad12ae098b57Ted Kremenek // For casts, we need to handle conversions from arrays to 325954b5274f2c190331438375ad114dad12ae098b57Ted Kremenek // pointer values, and pointer-to-pointer conversions. 326049badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor case Stmt::ImplicitCastExprClass: 32616eec8e883de118b431e3ead5b1e604a6ac68ff6bDouglas Gregor case Stmt::CStyleCastExprClass: 3262f85e193739c953358c865005855253af4f68a497John McCall case Stmt::CXXFunctionalCastExprClass: 32638b9414e868853fc052251455674bf7512261e14aEli Friedman case Stmt::ObjCBridgedCastExprClass: 32641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Stmt::CXXStaticCastExprClass: 32651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Stmt::CXXDynamicCastExprClass: 326649badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor case Stmt::CXXConstCastExprClass: 326749badde06e066d058d6c7fcf4e628a72999b65a9Douglas Gregor case Stmt::CXXReinterpretCastExprClass: { 32688b9414e868853fc052251455674bf7512261e14aEli Friedman Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); 32698b9414e868853fc052251455674bf7512261e14aEli Friedman switch (cast<CastExpr>(E)->getCastKind()) { 32708b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_BitCast: 32718b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_LValueToRValue: 32728b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_NoOp: 32738b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_BaseToDerived: 32748b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_DerivedToBase: 32758b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_UncheckedDerivedToBase: 32768b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_Dynamic: 32778b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_CPointerToObjCPointerCast: 32788b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_BlockPointerToObjCPointerCast: 32798b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_AnyPointerToBlockPointerCast: 3280e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalAddr(SubExpr, refVars, ParentDecl); 32818b9414e868853fc052251455674bf7512261e14aEli Friedman 32828b9414e868853fc052251455674bf7512261e14aEli Friedman case CK_ArrayToPointerDecay: 3283e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalVal(SubExpr, refVars, ParentDecl); 32848b9414e868853fc052251455674bf7512261e14aEli Friedman 32858b9414e868853fc052251455674bf7512261e14aEli Friedman default: 32868b9414e868853fc052251455674bf7512261e14aEli Friedman return 0; 32878b9414e868853fc052251455674bf7512261e14aEli Friedman } 3288fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner } 32891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 329003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor case Stmt::MaterializeTemporaryExprClass: 329103e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor if (Expr *Result = EvalAddr( 329203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), 3293e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis refVars, ParentDecl)) 329403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor return Result; 329503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 329603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor return E; 329703e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 3298fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner // Everything else: we simply don't reason about them. 3299fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner default: 3300fae3f1f6565c74d3238747b58357a6e56fbb0e9cChris Lattner return NULL; 330106de276fff91264437fa75111ed76de43097e089Ted Kremenek } 330206de276fff91264437fa75111ed76de43097e089Ted Kremenek} 33031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 330406de276fff91264437fa75111ed76de43097e089Ted Kremenek 330506de276fff91264437fa75111ed76de43097e089Ted Kremenek/// EvalVal - This function is complements EvalAddr in the mutual recursion. 330606de276fff91264437fa75111ed76de43097e089Ted Kremenek/// See the comments for EvalAddr for more details. 3307e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidisstatic Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, 3308e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis Decl *ParentDecl) { 330968957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenekdo { 3310e8c600f9fedf2cfd69cdd2cb4bde4a9b39ce2873Ted Kremenek // We should only be called for evaluating non-pointer expressions, or 3311e8c600f9fedf2cfd69cdd2cb4bde4a9b39ce2873Ted Kremenek // expressions with a pointer type that are not used as references but instead 3312e8c600f9fedf2cfd69cdd2cb4bde4a9b39ce2873Ted Kremenek // are l-values (e.g., DeclRefExpr with a pointer type). 33131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 331406de276fff91264437fa75111ed76de43097e089Ted Kremenek // Our "symbolic interpreter" is just a dispatch off the currently 331506de276fff91264437fa75111ed76de43097e089Ted Kremenek // viewed AST node. We then recursively traverse the AST by calling 331606de276fff91264437fa75111ed76de43097e089Ted Kremenek // EvalAddr and EvalVal appropriately. 3317f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne 3318f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne E = E->IgnoreParens(); 331906de276fff91264437fa75111ed76de43097e089Ted Kremenek switch (E->getStmtClass()) { 332068957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek case Stmt::ImplicitCastExprClass: { 332168957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); 33225baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall if (IE->getValueKind() == VK_LValue) { 332368957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek E = IE->getSubExpr(); 332468957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek continue; 332568957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek } 332668957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek return NULL; 332768957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek } 332868957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek 332980ee6e878a169e6255d4686a91bb696151ff229fJohn McCall case Stmt::ExprWithCleanupsClass: 3330e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); 333180ee6e878a169e6255d4686a91bb696151ff229fJohn McCall 3332a2813cec2605ce7878d1b13471d685f689b251afDouglas Gregor case Stmt::DeclRefExprClass: { 333326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // When we hit a DeclRefExpr we are looking at code that refers to a 333426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // variable's name. If it's not a reference variable we check if it has 333526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // local storage within the function, and if so, return the expression. 333606de276fff91264437fa75111ed76de43097e089Ted Kremenek DeclRefExpr *DR = cast<DeclRefExpr>(E); 33371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3338e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { 3339e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis // Check if it refers to itself, e.g. "int& i = i;". 3340e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis if (V == ParentDecl) 3341e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return DR; 3342e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis 334326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (V->hasLocalStorage()) { 334426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (!V->getType()->isReferenceType()) 334526e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis return DR; 334626e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 334726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // Reference variable, follow through to the expression that 334826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // it points to. 334926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (V->hasInit()) { 335026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // Add the reference variable to the "trail". 335126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis refVars.push_back(DR); 3352e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalVal(V->getInit(), refVars, V); 335326e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } 335426e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis } 3355e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis } 33561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 335706de276fff91264437fa75111ed76de43097e089Ted Kremenek return NULL; 335806de276fff91264437fa75111ed76de43097e089Ted Kremenek } 33591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 336006de276fff91264437fa75111ed76de43097e089Ted Kremenek case Stmt::UnaryOperatorClass: { 336106de276fff91264437fa75111ed76de43097e089Ted Kremenek // The only unary operator that make sense to handle here 336206de276fff91264437fa75111ed76de43097e089Ted Kremenek // is Deref. All others don't resolve to a "name." This includes 336306de276fff91264437fa75111ed76de43097e089Ted Kremenek // handling all sorts of rvalues passed to a unary operator. 336406de276fff91264437fa75111ed76de43097e089Ted Kremenek UnaryOperator *U = cast<UnaryOperator>(E); 33651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 33662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (U->getOpcode() == UO_Deref) 3367e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalAddr(U->getSubExpr(), refVars, ParentDecl); 336806de276fff91264437fa75111ed76de43097e089Ted Kremenek 336906de276fff91264437fa75111ed76de43097e089Ted Kremenek return NULL; 337006de276fff91264437fa75111ed76de43097e089Ted Kremenek } 33711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 337206de276fff91264437fa75111ed76de43097e089Ted Kremenek case Stmt::ArraySubscriptExprClass: { 337306de276fff91264437fa75111ed76de43097e089Ted Kremenek // Array subscripts are potential references to data on the stack. We 337406de276fff91264437fa75111ed76de43097e089Ted Kremenek // retrieve the DeclRefExpr* for the array variable if it indeed 337506de276fff91264437fa75111ed76de43097e089Ted Kremenek // has local storage. 3376e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); 337706de276fff91264437fa75111ed76de43097e089Ted Kremenek } 33781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 337906de276fff91264437fa75111ed76de43097e089Ted Kremenek case Stmt::ConditionalOperatorClass: { 338006de276fff91264437fa75111ed76de43097e089Ted Kremenek // For conditional operators we need to see if either the LHS or RHS are 338126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // non-NULL Expr's. If one is non-NULL, we return it. 338206de276fff91264437fa75111ed76de43097e089Ted Kremenek ConditionalOperator *C = cast<ConditionalOperator>(E); 338306de276fff91264437fa75111ed76de43097e089Ted Kremenek 33843907323dd6665c0c4e383435cb145233f4533406Anders Carlsson // Handle the GNU extension for missing LHS. 33853907323dd6665c0c4e383435cb145233f4533406Anders Carlsson if (Expr *lhsExpr = C->getLHS()) 3386e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis if (Expr *LHS = EvalVal(lhsExpr, refVars, ParentDecl)) 33873907323dd6665c0c4e383435cb145233f4533406Anders Carlsson return LHS; 33883907323dd6665c0c4e383435cb145233f4533406Anders Carlsson 3389e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalVal(C->getRHS(), refVars, ParentDecl); 339006de276fff91264437fa75111ed76de43097e089Ted Kremenek } 33911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 339206de276fff91264437fa75111ed76de43097e089Ted Kremenek // Accesses to members are potential references to data on the stack. 339383f6faf37d9bf58986bedc9bc0ea897a56b4dbadDouglas Gregor case Stmt::MemberExprClass: { 339406de276fff91264437fa75111ed76de43097e089Ted Kremenek MemberExpr *M = cast<MemberExpr>(E); 33951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 339606de276fff91264437fa75111ed76de43097e089Ted Kremenek // Check for indirect access. We only want direct field accesses. 3397a423e81a4d04b7c1882d2787d1189cbc14540c16Ted Kremenek if (M->isArrow()) 3398a423e81a4d04b7c1882d2787d1189cbc14540c16Ted Kremenek return NULL; 3399a423e81a4d04b7c1882d2787d1189cbc14540c16Ted Kremenek 3400a423e81a4d04b7c1882d2787d1189cbc14540c16Ted Kremenek // Check whether the member type is itself a reference, in which case 3401a423e81a4d04b7c1882d2787d1189cbc14540c16Ted Kremenek // we're not going to refer to the member, but to what the member refers to. 3402a423e81a4d04b7c1882d2787d1189cbc14540c16Ted Kremenek if (M->getMemberDecl()->getType()->isReferenceType()) 340306de276fff91264437fa75111ed76de43097e089Ted Kremenek return NULL; 3404a423e81a4d04b7c1882d2787d1189cbc14540c16Ted Kremenek 3405e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis return EvalVal(M->getBase(), refVars, ParentDecl); 340606de276fff91264437fa75111ed76de43097e089Ted Kremenek } 34071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 340803e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor case Stmt::MaterializeTemporaryExprClass: 340903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor if (Expr *Result = EvalVal( 341003e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), 3411e720ce7a3b1c1bfa5f7482183caa6e31fca9a3fbArgyrios Kyrtzidis refVars, ParentDecl)) 341203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor return Result; 341303e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 341403e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor return E; 341503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor 341606de276fff91264437fa75111ed76de43097e089Ted Kremenek default: 341726e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // Check that we don't return or take the address of a reference to a 341826e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // temporary. This is only useful in C++. 341926e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis if (!E->isTypeDependent() && E->isRValue()) 342026e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis return E; 342126e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis 342226e10bea3d2e9d2979194890e51b98ecea165a96Argyrios Kyrtzidis // Everything else: we simply don't reason about them. 342306de276fff91264437fa75111ed76de43097e089Ted Kremenek return NULL; 342406de276fff91264437fa75111ed76de43097e089Ted Kremenek } 342568957a919084ab8bbd1f01d534db1d6f31d0f459Ted Kremenek} while (true); 342606de276fff91264437fa75111ed76de43097e089Ted Kremenek} 3427588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek 3428588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// 3429588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek 3430588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek/// Check for comparisons of floating point operands using != and ==. 3431588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek/// Issue a warning if these are no self-comparisons, as they are not likely 3432588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek/// to do what the programmer intended. 3433dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieuvoid Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { 3434588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek bool EmitWarning = true; 34351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3436dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); 3437dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); 3438588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek 3439588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek // Special case: check for x == x (which is OK). 3440588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek // Do not emit warnings for such cases. 3441588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) 3442588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) 3443588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek if (DRL->getDecl() == DRR->getDecl()) 3444588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek EmitWarning = false; 34451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 34461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 34471b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek // Special case: check for comparisons against literals that can be exactly 34481b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek // represented by APFloat. In such cases, do not emit a warning. This 34491b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek // is a heuristic: often comparison against such literals are used to 34501b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek // detect if a value in a variable has not changed. This clearly can 34511b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek // lead to false negatives. 34521b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek if (EmitWarning) { 34531b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { 34541b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek if (FLL->isExact()) 34551b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek EmitWarning = false; 3456ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump } else 34571b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ 34581b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek if (FLR->isExact()) 34591b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek EmitWarning = false; 34601b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek } 34611b500bb0d40aa3ebf1ace47340bb5f401a9ae99cTed Kremenek } 34621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3463588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek // Check for comparisons with builtin types. 34640eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl if (EmitWarning) 3465588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) 3466180f47959a066795cc0f409433023af448bb0328Richard Smith if (CL->isBuiltinCall()) 3467588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek EmitWarning = false; 34681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 34690eb23307222bda7ad95d968eac4e1ab30864b213Sebastian Redl if (EmitWarning) 3470588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) 3471180f47959a066795cc0f409433023af448bb0328Richard Smith if (CR->isBuiltinCall()) 3472588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek EmitWarning = false; 34731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3474588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek // Emit the diagnostic. 3475588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek if (EmitWarning) 3476dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu Diag(Loc, diag::warn_floatingpoint_eq) 3477dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu << LHS->getSourceRange() << RHS->getSourceRange(); 3478588e5ebee2db045c3611e0c8f601bc4495ebd0f3Ted Kremenek} 3479ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 3480f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall//===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// 3481f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall//===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// 3482ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 3483f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCallnamespace { 3484ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 3485f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// Structure recording the 'active' range of an integer-valued 3486f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// expression. 3487f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCallstruct IntRange { 3488f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall /// The number of bits active in the int. 3489f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall unsigned Width; 3490ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 3491f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall /// True if the int is known not to have negative values. 3492f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall bool NonNegative; 349351313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3494f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IntRange(unsigned Width, bool NonNegative) 3495f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall : Width(Width), NonNegative(NonNegative) 3496f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall {} 349751313c39c84407dd6a323be99a8c322bf8d052a9John McCall 34981844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// Returns the range of the bool type. 3499f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall static IntRange forBoolType() { 3500f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange(1, true); 3501f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall } 350251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 35031844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// Returns the range of an opaque value of the given integral type. 35041844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall static IntRange forValueOfType(ASTContext &C, QualType T) { 35051844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return forValueOfCanonicalType(C, 35061844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall T->getCanonicalTypeInternal().getTypePtr()); 350751313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 350851313c39c84407dd6a323be99a8c322bf8d052a9John McCall 35091844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// Returns the range of an opaque value of a canonical integral type. 35101844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { 3511f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall assert(T->isCanonicalUnqualified()); 351251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3513f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (const VectorType *VT = dyn_cast<VectorType>(T)) 3514f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall T = VT->getElementType().getTypePtr(); 3515f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 3516f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall T = CT->getElementType().getTypePtr(); 3517323ed74658bc8375278eabf074b4777458376540John McCall 3518091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall // For enum types, use the known bit width of the enumerators. 3519323ed74658bc8375278eabf074b4777458376540John McCall if (const EnumType *ET = dyn_cast<EnumType>(T)) { 3520323ed74658bc8375278eabf074b4777458376540John McCall EnumDecl *Enum = ET->getDecl(); 35215e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall if (!Enum->isCompleteDefinition()) 3522091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return IntRange(C.getIntWidth(QualType(T, 0)), false); 3523091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 3524323ed74658bc8375278eabf074b4777458376540John McCall unsigned NumPositive = Enum->getNumPositiveBits(); 3525323ed74658bc8375278eabf074b4777458376540John McCall unsigned NumNegative = Enum->getNumNegativeBits(); 3526323ed74658bc8375278eabf074b4777458376540John McCall 3527323ed74658bc8375278eabf074b4777458376540John McCall return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); 3528323ed74658bc8375278eabf074b4777458376540John McCall } 352951313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3530f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall const BuiltinType *BT = cast<BuiltinType>(T); 3531f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall assert(BT->isInteger()); 353251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3533f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 353451313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 353551313c39c84407dd6a323be99a8c322bf8d052a9John McCall 35361844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// Returns the "target" range of a canonical integral type, i.e. 35371844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// the range of values expressible in the type. 35381844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// 35391844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// This matches forValueOfCanonicalType except that enums have the 35401844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// full range of their type, not the range of their enumerators. 35411844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { 35421844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall assert(T->isCanonicalUnqualified()); 35431844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall 35441844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall if (const VectorType *VT = dyn_cast<VectorType>(T)) 35451844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall T = VT->getElementType().getTypePtr(); 35461844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 35471844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall T = CT->getElementType().getTypePtr(); 35481844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall if (const EnumType *ET = dyn_cast<EnumType>(T)) 354969ff26bd1e7a2faffec0c3b9740cfb0c6bb354c5Douglas Gregor T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); 35501844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall 35511844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall const BuiltinType *BT = cast<BuiltinType>(T); 35521844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall assert(BT->isInteger()); 35531844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall 35541844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 35551844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall } 35561844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall 35571844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// Returns the supremum of two ranges: i.e. their conservative merge. 3558c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall static IntRange join(IntRange L, IntRange R) { 3559f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange(std::max(L.Width, R.Width), 356060fad45739b764886f707bd204eae9ecce6db1f2John McCall L.NonNegative && R.NonNegative); 356160fad45739b764886f707bd204eae9ecce6db1f2John McCall } 356260fad45739b764886f707bd204eae9ecce6db1f2John McCall 35631844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall /// Returns the infinum of two ranges: i.e. their aggressive merge. 3564c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall static IntRange meet(IntRange L, IntRange R) { 356560fad45739b764886f707bd204eae9ecce6db1f2John McCall return IntRange(std::min(L.Width, R.Width), 356660fad45739b764886f707bd204eae9ecce6db1f2John McCall L.NonNegative || R.NonNegative); 356751313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 3568f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall}; 356951313c39c84407dd6a323be99a8c322bf8d052a9John McCall 35700692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, 35710692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenek unsigned MaxWidth) { 3572f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (value.isSigned() && value.isNegative()) 3573f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange(value.getMinSignedBits(), false); 357451313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3575f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (value.getBitWidth() > MaxWidth) 35769f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad value = value.trunc(MaxWidth); 357751313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3578f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // isNonNegative() just checks the sign bit without considering 3579f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // signedness. 3580f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange(value.getActiveBits(), true); 358151313c39c84407dd6a323be99a8c322bf8d052a9John McCall} 358251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 35830692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, 35840692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenek unsigned MaxWidth) { 3585f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (result.isInt()) 3586f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return GetValueRange(C, result.getInt(), MaxWidth); 358751313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3588f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (result.isVector()) { 35890acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); 35900acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { 35910acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); 35920acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall R = IntRange::join(R, El); 35930acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall } 3594f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return R; 359551313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 359651313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3597f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (result.isComplexInt()) { 3598f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); 3599f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); 3600f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange::join(R, I); 3601f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall } 3602f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3603f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // This can happen with lossless casts to intptr_t of "based" lvalues. 3604f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // Assume it might use arbitrary bits. 36050acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall // FIXME: The only reason we need to pass the type in here is to get 36060acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall // the sign right on this one case. It would be nice if APValue 36070acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall // preserved this. 360865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman assert(result.isLValue() || result.isAddrLabelDiff()); 36095e9ebb3c0fb554d9285aa99c470abdf283272bd9Douglas Gregor return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); 361051313c39c84407dd6a323be99a8c322bf8d052a9John McCall} 361151313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3612f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// Pseudo-evaluate the given integer expression, estimating the 3613f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// range of values it might take. 3614f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// 3615f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// \param MaxWidth - the width to which the value will be truncated 36160692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { 361751313c39c84407dd6a323be99a8c322bf8d052a9John McCall E = E->IgnoreParens(); 361851313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3619f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // Try a full evaluation first. 3620f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall Expr::EvalResult result; 362151f4708c00110940ca3f337961915f2ca1668375Richard Smith if (E->EvaluateAsRValue(result, C)) 36220acc311bf73c85fd34ce6f89a4e786b7ecd214aaJohn McCall return GetValueRange(C, result.Val, E->getType(), MaxWidth); 362351313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3624f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // I think we only want to look through implicit casts here; if the 3625f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // user has an explicit widening cast, we should treat the value as 3626f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // being of the new, wider type. 3627f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { 3628b17ee5bff0d7986c14cdddb279e0131bd7f9678dEli Friedman if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) 3629f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return GetExprRange(C, CE->getSubExpr(), MaxWidth); 363051313c39c84407dd6a323be99a8c322bf8d052a9John McCall 36311844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); 363251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 36332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); 363460fad45739b764886f707bd204eae9ecce6db1f2John McCall 3635f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // Assume that non-integer casts can span the full range of the type. 363660fad45739b764886f707bd204eae9ecce6db1f2John McCall if (!isIntegerCast) 3637f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return OutputTypeRange; 363851313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3639f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IntRange SubRange 3640f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall = GetExprRange(C, CE->getSubExpr(), 3641f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall std::min(MaxWidth, OutputTypeRange.Width)); 364251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3643f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // Bail out if the subexpr's range is as wide as the cast type. 3644f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (SubRange.Width >= OutputTypeRange.Width) 3645f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return OutputTypeRange; 364651313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3647f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // Otherwise, we take the smaller width, and we're non-negative if 3648f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // either the output type or the subexpr is. 3649f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange(SubRange.Width, 3650f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall SubRange.NonNegative || OutputTypeRange.NonNegative); 3651f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall } 3652f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3653f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 3654f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // If we can fold the condition, just take that operand. 3655f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall bool CondResult; 3656f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) 3657f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return GetExprRange(C, CondResult ? CO->getTrueExpr() 3658f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall : CO->getFalseExpr(), 3659f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall MaxWidth); 3660f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3661f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // Otherwise, conservatively merge. 3662f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); 3663f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); 3664f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange::join(L, R); 366551313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 366651313c39c84407dd6a323be99a8c322bf8d052a9John McCall 366751313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 366851313c39c84407dd6a323be99a8c322bf8d052a9John McCall switch (BO->getOpcode()) { 366951313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3670f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall // Boolean-valued operations are single-bit and positive. 36712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LAnd: 36722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LOr: 36732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LT: 36742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GT: 36752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LE: 36762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GE: 36772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_EQ: 36782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_NE: 3679f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange::forBoolType(); 368051313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3681862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall // The type of the assignments is the type of the LHS, so the RHS 3682862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall // is not necessarily the same type. 36832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_MulAssign: 36842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_DivAssign: 36852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_RemAssign: 36862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_AddAssign: 36872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_SubAssign: 3688862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall case BO_XorAssign: 3689862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall case BO_OrAssign: 3690862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall // TODO: bitfields? 36911844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return IntRange::forValueOfType(C, E->getType()); 3692c0cd21d2a3301a7a88e0052aebdd09c2441f826dJohn McCall 3693862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall // Simple assignments just pass through the RHS, which will have 3694862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall // been coerced to the LHS type. 3695862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall case BO_Assign: 3696862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall // TODO: bitfields? 3697862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall return GetExprRange(C, BO->getRHS(), MaxWidth); 3698862ff87c0d306af8dfdbe7ac59f181a5815546e5John McCall 369951313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Operations with opaque sources are black-listed. 37002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_PtrMemD: 37012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_PtrMemI: 37021844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return IntRange::forValueOfType(C, E->getType()); 370351313c39c84407dd6a323be99a8c322bf8d052a9John McCall 370460fad45739b764886f707bd204eae9ecce6db1f2John McCall // Bitwise-and uses the *infinum* of the two source ranges. 37052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_And: 37062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_AndAssign: 370760fad45739b764886f707bd204eae9ecce6db1f2John McCall return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), 370860fad45739b764886f707bd204eae9ecce6db1f2John McCall GetExprRange(C, BO->getRHS(), MaxWidth)); 370960fad45739b764886f707bd204eae9ecce6db1f2John McCall 371051313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Left shift gets black-listed based on a judgement call. 37112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shl: 37123aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall // ...except that we want to treat '1 << (blah)' as logically 37133aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall // positive. It's an important idiom. 37143aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall if (IntegerLiteral *I 37153aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { 37163aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall if (I->getValue() == 1) { 37171844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall IntRange R = IntRange::forValueOfType(C, E->getType()); 37183aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall return IntRange(R.Width, /*NonNegative*/ true); 37193aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall } 37203aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall } 37213aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall // fallthrough 37223aae6093dd8f3aecb66d7ff1f4b44e6a86765db4John McCall 37232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_ShlAssign: 37241844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return IntRange::forValueOfType(C, E->getType()); 372551313c39c84407dd6a323be99a8c322bf8d052a9John McCall 372660fad45739b764886f707bd204eae9ecce6db1f2John McCall // Right shift by a constant can narrow its left argument. 37272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shr: 37282de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_ShrAssign: { 372960fad45739b764886f707bd204eae9ecce6db1f2John McCall IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); 373060fad45739b764886f707bd204eae9ecce6db1f2John McCall 373160fad45739b764886f707bd204eae9ecce6db1f2John McCall // If the shift amount is a positive constant, drop the width by 373260fad45739b764886f707bd204eae9ecce6db1f2John McCall // that much. 373360fad45739b764886f707bd204eae9ecce6db1f2John McCall llvm::APSInt shift; 373460fad45739b764886f707bd204eae9ecce6db1f2John McCall if (BO->getRHS()->isIntegerConstantExpr(shift, C) && 373560fad45739b764886f707bd204eae9ecce6db1f2John McCall shift.isNonNegative()) { 373660fad45739b764886f707bd204eae9ecce6db1f2John McCall unsigned zext = shift.getZExtValue(); 373760fad45739b764886f707bd204eae9ecce6db1f2John McCall if (zext >= L.Width) 373860fad45739b764886f707bd204eae9ecce6db1f2John McCall L.Width = (L.NonNegative ? 0 : 1); 373960fad45739b764886f707bd204eae9ecce6db1f2John McCall else 374060fad45739b764886f707bd204eae9ecce6db1f2John McCall L.Width -= zext; 374160fad45739b764886f707bd204eae9ecce6db1f2John McCall } 374260fad45739b764886f707bd204eae9ecce6db1f2John McCall 374360fad45739b764886f707bd204eae9ecce6db1f2John McCall return L; 374460fad45739b764886f707bd204eae9ecce6db1f2John McCall } 374560fad45739b764886f707bd204eae9ecce6db1f2John McCall 374660fad45739b764886f707bd204eae9ecce6db1f2John McCall // Comma acts as its right operand. 37472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Comma: 3748f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return GetExprRange(C, BO->getRHS(), MaxWidth); 3749f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 375060fad45739b764886f707bd204eae9ecce6db1f2John McCall // Black-list pointer subtractions. 37512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 375251313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (BO->getLHS()->getType()->isPointerType()) 37531844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return IntRange::forValueOfType(C, E->getType()); 375400fe761849278180b0d73f276e468cf4c54ff2adJohn McCall break; 375500fe761849278180b0d73f276e468cf4c54ff2adJohn McCall 375600fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // The width of a division result is mostly determined by the size 375700fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // of the LHS. 375800fe761849278180b0d73f276e468cf4c54ff2adJohn McCall case BO_Div: { 375900fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // Don't 'pre-truncate' the operands. 376000fe761849278180b0d73f276e468cf4c54ff2adJohn McCall unsigned opWidth = C.getIntWidth(E->getType()); 376100fe761849278180b0d73f276e468cf4c54ff2adJohn McCall IntRange L = GetExprRange(C, BO->getLHS(), opWidth); 376200fe761849278180b0d73f276e468cf4c54ff2adJohn McCall 376300fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // If the divisor is constant, use that. 376400fe761849278180b0d73f276e468cf4c54ff2adJohn McCall llvm::APSInt divisor; 376500fe761849278180b0d73f276e468cf4c54ff2adJohn McCall if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { 376600fe761849278180b0d73f276e468cf4c54ff2adJohn McCall unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) 376700fe761849278180b0d73f276e468cf4c54ff2adJohn McCall if (log2 >= L.Width) 376800fe761849278180b0d73f276e468cf4c54ff2adJohn McCall L.Width = (L.NonNegative ? 0 : 1); 376900fe761849278180b0d73f276e468cf4c54ff2adJohn McCall else 377000fe761849278180b0d73f276e468cf4c54ff2adJohn McCall L.Width = std::min(L.Width - log2, MaxWidth); 377100fe761849278180b0d73f276e468cf4c54ff2adJohn McCall return L; 377200fe761849278180b0d73f276e468cf4c54ff2adJohn McCall } 377300fe761849278180b0d73f276e468cf4c54ff2adJohn McCall 377400fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // Otherwise, just use the LHS's width. 377500fe761849278180b0d73f276e468cf4c54ff2adJohn McCall IntRange R = GetExprRange(C, BO->getRHS(), opWidth); 377600fe761849278180b0d73f276e468cf4c54ff2adJohn McCall return IntRange(L.Width, L.NonNegative && R.NonNegative); 377700fe761849278180b0d73f276e468cf4c54ff2adJohn McCall } 377800fe761849278180b0d73f276e468cf4c54ff2adJohn McCall 377900fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // The result of a remainder can't be larger than the result of 378000fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // either side. 378100fe761849278180b0d73f276e468cf4c54ff2adJohn McCall case BO_Rem: { 378200fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // Don't 'pre-truncate' the operands. 378300fe761849278180b0d73f276e468cf4c54ff2adJohn McCall unsigned opWidth = C.getIntWidth(E->getType()); 378400fe761849278180b0d73f276e468cf4c54ff2adJohn McCall IntRange L = GetExprRange(C, BO->getLHS(), opWidth); 378500fe761849278180b0d73f276e468cf4c54ff2adJohn McCall IntRange R = GetExprRange(C, BO->getRHS(), opWidth); 378600fe761849278180b0d73f276e468cf4c54ff2adJohn McCall 378700fe761849278180b0d73f276e468cf4c54ff2adJohn McCall IntRange meet = IntRange::meet(L, R); 378800fe761849278180b0d73f276e468cf4c54ff2adJohn McCall meet.Width = std::min(meet.Width, MaxWidth); 378900fe761849278180b0d73f276e468cf4c54ff2adJohn McCall return meet; 379000fe761849278180b0d73f276e468cf4c54ff2adJohn McCall } 37914e4b30ec62d78b24e6556fea2624855c193d0e3eTed Kremenek 379200fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // The default behavior is okay for these. 379300fe761849278180b0d73f276e468cf4c54ff2adJohn McCall case BO_Mul: 379400fe761849278180b0d73f276e468cf4c54ff2adJohn McCall case BO_Add: 379500fe761849278180b0d73f276e468cf4c54ff2adJohn McCall case BO_Xor: 379600fe761849278180b0d73f276e468cf4c54ff2adJohn McCall case BO_Or: 3797f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall break; 379851313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 3799f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 380000fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // The default case is to treat the operation as if it were closed 380100fe761849278180b0d73f276e468cf4c54ff2adJohn McCall // on the narrowest type that encompasses both operands. 3802f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); 3803f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); 3804f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange::join(L, R); 380551313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 380651313c39c84407dd6a323be99a8c322bf8d052a9John McCall 380751313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 380851313c39c84407dd6a323be99a8c322bf8d052a9John McCall switch (UO->getOpcode()) { 380951313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Boolean-valued operations are white-listed. 38102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_LNot: 3811f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IntRange::forBoolType(); 381251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 381351313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Operations with opaque sources are black-listed. 38142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Deref: 38152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_AddrOf: // should be impossible 38161844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return IntRange::forValueOfType(C, E->getType()); 381751313c39c84407dd6a323be99a8c322bf8d052a9John McCall 381851313c39c84407dd6a323be99a8c322bf8d052a9John McCall default: 3819f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return GetExprRange(C, UO->getSubExpr(), MaxWidth); 382051313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 382151313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 38228ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor 38238ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (dyn_cast<OffsetOfExpr>(E)) { 38241844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall IntRange::forValueOfType(C, E->getType()); 38258ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 382651313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3827a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith if (FieldDecl *BitField = E->getBitField()) 3828a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith return IntRange(BitField->getBitWidthValue(C), 38295e9ebb3c0fb554d9285aa99c470abdf283272bd9Douglas Gregor BitField->getType()->isUnsignedIntegerOrEnumerationType()); 3830f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 38311844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall return IntRange::forValueOfType(C, E->getType()); 3832f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall} 3833f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 38340692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic IntRange GetExprRange(ASTContext &C, Expr *E) { 3835323ed74658bc8375278eabf074b4777458376540John McCall return GetExprRange(C, E, C.getIntWidth(E->getType())); 3836323ed74658bc8375278eabf074b4777458376540John McCall} 3837323ed74658bc8375278eabf074b4777458376540John McCall 3838f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// Checks whether the given value, which currently has the given 3839f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// source semantics, has the same value when coerced through the 3840f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// target semantics. 38410692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic bool IsSameFloatAfterCast(const llvm::APFloat &value, 38420692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenek const llvm::fltSemantics &Src, 38430692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenek const llvm::fltSemantics &Tgt) { 3844f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall llvm::APFloat truncated = value; 3845f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3846f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall bool ignored; 3847f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); 3848f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); 3849f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3850f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return truncated.bitwiseIsEqual(value); 385151313c39c84407dd6a323be99a8c322bf8d052a9John McCall} 385251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 3853f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// Checks whether the given value, which currently has the given 3854f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// source semantics, has the same value when coerced through the 3855f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// target semantics. 3856f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// 3857f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall/// The value might be a vector of floats (or a complex number). 38580692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic bool IsSameFloatAfterCast(const APValue &value, 38590692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenek const llvm::fltSemantics &Src, 38600692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenek const llvm::fltSemantics &Tgt) { 3861f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (value.isFloat()) 3862f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); 3863f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3864f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (value.isVector()) { 3865f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) 3866f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) 3867f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return false; 3868f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return true; 3869f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall } 3870f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3871f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall assert(value.isComplexFloat()); 3872f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && 3873f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); 3874f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall} 3875f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 38760692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); 3877323ed74658bc8375278eabf074b4777458376540John McCall 3878e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenekstatic bool IsZero(Sema &S, Expr *E) { 3879e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek // Suppress cases where we are comparing against an enum constant. 3880e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek if (const DeclRefExpr *DR = 3881e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) 3882e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek if (isa<EnumConstantDecl>(DR->getDecl())) 3883e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek return false; 3884e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek 3885e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek // Suppress cases where the '0' value is expanded from a macro. 3886e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek if (E->getLocStart().isMacroID()) 3887e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek return false; 3888e3b159c0d4a93db8586861ff011b5a10de4ae6efTed Kremenek 3889323ed74658bc8375278eabf074b4777458376540John McCall llvm::APSInt Value; 3890323ed74658bc8375278eabf074b4777458376540John McCall return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; 3891323ed74658bc8375278eabf074b4777458376540John McCall} 3892323ed74658bc8375278eabf074b4777458376540John McCall 3893372e103dab4239ec52b65b9eda69fd43c0b348d4John McCallstatic bool HasEnumType(Expr *E) { 3894372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall // Strip off implicit integral promotions. 3895372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 389663b57aeeebb5bb758ecebf9b41b5131781f32bf3Argyrios Kyrtzidis if (ICE->getCastKind() != CK_IntegralCast && 389763b57aeeebb5bb758ecebf9b41b5131781f32bf3Argyrios Kyrtzidis ICE->getCastKind() != CK_NoOp) 3898372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall break; 389963b57aeeebb5bb758ecebf9b41b5131781f32bf3Argyrios Kyrtzidis E = ICE->getSubExpr(); 3900372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall } 3901372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall 3902372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall return E->getType()->isEnumeralType(); 3903372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall} 3904372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall 39050692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { 39062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall BinaryOperatorKind op = E->getOpcode(); 390714af91a38c8cf89f0a5e1835d7d8e0fa220e17a9Douglas Gregor if (E->isValueDependent()) 390814af91a38c8cf89f0a5e1835d7d8e0fa220e17a9Douglas Gregor return; 390914af91a38c8cf89f0a5e1835d7d8e0fa220e17a9Douglas Gregor 39102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (op == BO_LT && IsZero(S, E->getRHS())) { 3911323ed74658bc8375278eabf074b4777458376540John McCall S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) 3912372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall << "< 0" << "false" << HasEnumType(E->getLHS()) 3913323ed74658bc8375278eabf074b4777458376540John McCall << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 39142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall } else if (op == BO_GE && IsZero(S, E->getRHS())) { 3915323ed74658bc8375278eabf074b4777458376540John McCall S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) 3916372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall << ">= 0" << "true" << HasEnumType(E->getLHS()) 3917323ed74658bc8375278eabf074b4777458376540John McCall << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 39182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall } else if (op == BO_GT && IsZero(S, E->getLHS())) { 3919323ed74658bc8375278eabf074b4777458376540John McCall S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) 3920372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall << "0 >" << "false" << HasEnumType(E->getRHS()) 3921323ed74658bc8375278eabf074b4777458376540John McCall << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 39222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall } else if (op == BO_LE && IsZero(S, E->getLHS())) { 3923323ed74658bc8375278eabf074b4777458376540John McCall S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) 3924372e103dab4239ec52b65b9eda69fd43c0b348d4John McCall << "0 <=" << "true" << HasEnumType(E->getRHS()) 3925323ed74658bc8375278eabf074b4777458376540John McCall << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 3926323ed74658bc8375278eabf074b4777458376540John McCall } 3927323ed74658bc8375278eabf074b4777458376540John McCall} 3928323ed74658bc8375278eabf074b4777458376540John McCall 3929323ed74658bc8375278eabf074b4777458376540John McCall/// Analyze the operands of the given comparison. Implements the 3930323ed74658bc8375278eabf074b4777458376540John McCall/// fallback case from AnalyzeComparison. 39310692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { 3932b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 3933b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 3934323ed74658bc8375278eabf074b4777458376540John McCall} 3935f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3936ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall/// \brief Implements -Wsign-compare. 3937ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall/// 3938dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu/// \param E the binary operator to check for warnings 39390692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic void AnalyzeComparison(Sema &S, BinaryOperator *E) { 3940323ed74658bc8375278eabf074b4777458376540John McCall // The type the comparison is being performed in. 3941323ed74658bc8375278eabf074b4777458376540John McCall QualType T = E->getLHS()->getType(); 3942323ed74658bc8375278eabf074b4777458376540John McCall assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) 3943323ed74658bc8375278eabf074b4777458376540John McCall && "comparison with mismatched types"); 3944323ed74658bc8375278eabf074b4777458376540John McCall 3945323ed74658bc8375278eabf074b4777458376540John McCall // We don't do anything special if this isn't an unsigned integral 3946323ed74658bc8375278eabf074b4777458376540John McCall // comparison: we're only interested in integral comparisons, and 3947323ed74658bc8375278eabf074b4777458376540John McCall // signed comparisons only happen in cases we don't care to warn about. 39483e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor // 39493e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor // We also don't care about value-dependent expressions or expressions 39503e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor // whose result is a constant. 39513e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor if (!T->hasUnsignedIntegerRepresentation() 39523e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor || E->isValueDependent() || E->isIntegerConstantExpr(S.Context)) 3953323ed74658bc8375278eabf074b4777458376540John McCall return AnalyzeImpConvsInComparison(S, E); 3954323ed74658bc8375278eabf074b4777458376540John McCall 3955dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); 3956dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); 3957323ed74658bc8375278eabf074b4777458376540John McCall 3958323ed74658bc8375278eabf074b4777458376540John McCall // Check to see if one of the (unmodified) operands is of different 3959323ed74658bc8375278eabf074b4777458376540John McCall // signedness. 3960323ed74658bc8375278eabf074b4777458376540John McCall Expr *signedOperand, *unsignedOperand; 3961dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu if (LHS->getType()->hasSignedIntegerRepresentation()) { 3962dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu assert(!RHS->getType()->hasSignedIntegerRepresentation() && 3963323ed74658bc8375278eabf074b4777458376540John McCall "unsigned comparison between two signed integer expressions?"); 3964dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu signedOperand = LHS; 3965dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu unsignedOperand = RHS; 3966dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu } else if (RHS->getType()->hasSignedIntegerRepresentation()) { 3967dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu signedOperand = RHS; 3968dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu unsignedOperand = LHS; 3969ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall } else { 3970323ed74658bc8375278eabf074b4777458376540John McCall CheckTrivialUnsignedComparison(S, E); 3971323ed74658bc8375278eabf074b4777458376540John McCall return AnalyzeImpConvsInComparison(S, E); 3972ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall } 3973ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 3974323ed74658bc8375278eabf074b4777458376540John McCall // Otherwise, calculate the effective range of the signed operand. 3975323ed74658bc8375278eabf074b4777458376540John McCall IntRange signedRange = GetExprRange(S.Context, signedOperand); 3976f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall 3977323ed74658bc8375278eabf074b4777458376540John McCall // Go ahead and analyze implicit conversions in the operands. Note 3978323ed74658bc8375278eabf074b4777458376540John McCall // that we skip the implicit conversions on both sides. 3979dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); 3980dd22509c82eb7681a0f46b41d61cb2e25a4d8fa1Richard Trieu AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); 3981ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 3982323ed74658bc8375278eabf074b4777458376540John McCall // If the signed range is non-negative, -Wsign-compare won't fire, 3983323ed74658bc8375278eabf074b4777458376540John McCall // but we should still check for comparisons which are always true 3984323ed74658bc8375278eabf074b4777458376540John McCall // or false. 3985323ed74658bc8375278eabf074b4777458376540John McCall if (signedRange.NonNegative) 3986323ed74658bc8375278eabf074b4777458376540John McCall return CheckTrivialUnsignedComparison(S, E); 3987ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 3988ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall // For (in)equality comparisons, if the unsigned operand is a 3989ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall // constant which cannot collide with a overflowed signed operand, 3990ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall // then reinterpreting the signed operand as unsigned will not 3991ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall // change the result of the comparison. 3992323ed74658bc8375278eabf074b4777458376540John McCall if (E->isEqualityOp()) { 3993323ed74658bc8375278eabf074b4777458376540John McCall unsigned comparisonWidth = S.Context.getIntWidth(T); 3994323ed74658bc8375278eabf074b4777458376540John McCall IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); 3995323ed74658bc8375278eabf074b4777458376540John McCall 3996323ed74658bc8375278eabf074b4777458376540John McCall // We should never be unable to prove that the unsigned operand is 3997323ed74658bc8375278eabf074b4777458376540John McCall // non-negative. 3998323ed74658bc8375278eabf074b4777458376540John McCall assert(unsignedRange.NonNegative && "unsigned range includes negative?"); 3999ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 4000323ed74658bc8375278eabf074b4777458376540John McCall if (unsignedRange.Width < comparisonWidth) 4001323ed74658bc8375278eabf074b4777458376540John McCall return; 4002323ed74658bc8375278eabf074b4777458376540John McCall } 4003323ed74658bc8375278eabf074b4777458376540John McCall 40046d3b93d631640125f912339df19b0d2a15af5c9bDouglas Gregor S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 40056d3b93d631640125f912339df19b0d2a15af5c9bDouglas Gregor S.PDiag(diag::warn_mixed_sign_comparison) 40066d3b93d631640125f912339df19b0d2a15af5c9bDouglas Gregor << LHS->getType() << RHS->getType() 40076d3b93d631640125f912339df19b0d2a15af5c9bDouglas Gregor << LHS->getSourceRange() << RHS->getSourceRange()); 4008ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall} 4009ba26e58c64b4f6233dfc4bcd3ef6ce83aab47ffcJohn McCall 401015d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall/// Analyzes an attempt to assign the given value to a bitfield. 401115d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall/// 401215d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall/// Returns true if there was something fishy about the attempt. 40130692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, 40140692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenek SourceLocation InitLoc) { 401515d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall assert(Bitfield->isBitField()); 401615d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall if (Bitfield->isInvalidDecl()) 401715d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall return false; 401815d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 401991b6014876e9186f968f74a8975ab8fc08ef1b68John McCall // White-list bool bitfields. 402091b6014876e9186f968f74a8975ab8fc08ef1b68John McCall if (Bitfield->getType()->isBooleanType()) 402191b6014876e9186f968f74a8975ab8fc08ef1b68John McCall return false; 402291b6014876e9186f968f74a8975ab8fc08ef1b68John McCall 402346ff3034d14aaa92b530e96480741f3d5d458cb8Douglas Gregor // Ignore value- or type-dependent expressions. 402446ff3034d14aaa92b530e96480741f3d5d458cb8Douglas Gregor if (Bitfield->getBitWidth()->isValueDependent() || 402546ff3034d14aaa92b530e96480741f3d5d458cb8Douglas Gregor Bitfield->getBitWidth()->isTypeDependent() || 402646ff3034d14aaa92b530e96480741f3d5d458cb8Douglas Gregor Init->isValueDependent() || 402746ff3034d14aaa92b530e96480741f3d5d458cb8Douglas Gregor Init->isTypeDependent()) 402846ff3034d14aaa92b530e96480741f3d5d458cb8Douglas Gregor return false; 402946ff3034d14aaa92b530e96480741f3d5d458cb8Douglas Gregor 403015d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall Expr *OriginalInit = Init->IgnoreParenImpCasts(); 403115d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 403280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith llvm::APSInt Value; 403380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) 403415d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall return false; 403515d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 403615d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall unsigned OriginalWidth = Value.getBitWidth(); 4037a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); 403815d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 403915d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall if (OriginalWidth <= FieldWidth) 404015d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall return false; 404115d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 40423a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman // Compute the value which the bitfield will contain. 40439f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); 40443a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); 404515d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 40463a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman // Check whether the stored value is equal to the original value. 40473a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman TruncatedValue = TruncatedValue.extend(OriginalWidth); 404815d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall if (Value == TruncatedValue) 404915d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall return false; 405015d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 40513a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman // Special-case bitfields of width 1: booleans are naturally 0/1, and 405234ff062936ac78142a2c0dc8d1ae6a40df456819Eli Friedman // therefore don't strictly fit into a signed bitfield of width 1. 405334ff062936ac78142a2c0dc8d1ae6a40df456819Eli Friedman if (FieldWidth == 1 && Value == 1) 40543a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman return false; 40553a643afa402d51b5fc4faccb4329e9c4392d4551Eli Friedman 405615d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall std::string PrettyValue = Value.toString(10); 405715d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall std::string PrettyTrunc = TruncatedValue.toString(10); 405815d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 405915d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) 406015d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall << PrettyValue << PrettyTrunc << OriginalInit->getType() 406115d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall << Init->getSourceRange(); 406215d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 406315d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall return true; 406415d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall} 406515d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 4066beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall/// Analyze the given simple or compound assignment for warning-worthy 4067beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall/// operations. 40680692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic void AnalyzeAssignment(Sema &S, BinaryOperator *E) { 4069beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall // Just recurse on the LHS. 4070beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 4071beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall 4072beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall // We want to recurse on the RHS as normal unless we're assigning to 4073beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall // a bitfield. 4074beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { 407515d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), 407615d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall E->getOperatorLoc())) { 407715d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall // Recurse, ignoring any implicit conversions on the RHS. 407815d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), 407915d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall E->getOperatorLoc()); 4080beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall } 4081beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall } 4082beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall 4083beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 4084beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall} 4085beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall 408651313c39c84407dd6a323be99a8c322bf8d052a9John McCall/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 40870692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, 4088c36bedc90c687caa71748480c60707ea4608b092Anna Zaks SourceLocation CContext, unsigned diag, 4089c36bedc90c687caa71748480c60707ea4608b092Anna Zaks bool pruneControlFlow = false) { 4090c36bedc90c687caa71748480c60707ea4608b092Anna Zaks if (pruneControlFlow) { 4091c36bedc90c687caa71748480c60707ea4608b092Anna Zaks S.DiagRuntimeBehavior(E->getExprLoc(), E, 4092c36bedc90c687caa71748480c60707ea4608b092Anna Zaks S.PDiag(diag) 4093c36bedc90c687caa71748480c60707ea4608b092Anna Zaks << SourceType << T << E->getSourceRange() 4094c36bedc90c687caa71748480c60707ea4608b092Anna Zaks << SourceRange(CContext)); 4095c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return; 4096c36bedc90c687caa71748480c60707ea4608b092Anna Zaks } 40975a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor S.Diag(E->getExprLoc(), diag) 40985a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor << SourceType << T << E->getSourceRange() << SourceRange(CContext); 40995a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor} 41005a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor 4101e1b02e00d851389415a5b7a1398de26c382887c6Chandler Carruth/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 41020692a1991bdb8b3112887fce90d75f2103a0080bTed Kremenekstatic void DiagnoseImpCast(Sema &S, Expr *E, QualType T, 4103c36bedc90c687caa71748480c60707ea4608b092Anna Zaks SourceLocation CContext, unsigned diag, 4104c36bedc90c687caa71748480c60707ea4608b092Anna Zaks bool pruneControlFlow = false) { 4105c36bedc90c687caa71748480c60707ea4608b092Anna Zaks DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); 4106e1b02e00d851389415a5b7a1398de26c382887c6Chandler Carruth} 4107e1b02e00d851389415a5b7a1398de26c382887c6Chandler Carruth 41089ce6377714a8eb8f577c87028300421e72b00dc9Matt Beaumont-Gay/// Diagnose an implicit cast from a literal expression. Does not warn when the 41099ce6377714a8eb8f577c87028300421e72b00dc9Matt Beaumont-Gay/// cast wouldn't lose information. 4110f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruthvoid DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, 4111f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth SourceLocation CContext) { 41129ce6377714a8eb8f577c87028300421e72b00dc9Matt Beaumont-Gay // Try to convert the literal exactly to an integer. If we can, don't warn. 4113f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth bool isExact = false; 41149ce6377714a8eb8f577c87028300421e72b00dc9Matt Beaumont-Gay const llvm::APFloat &Value = FL->getValue(); 41153e1ef7849845a9e7bf79156bbb8a2c26d77a1d2eJeffrey Yasskin llvm::APSInt IntegerValue(S.Context.getIntWidth(T), 41163e1ef7849845a9e7bf79156bbb8a2c26d77a1d2eJeffrey Yasskin T->hasUnsignedIntegerRepresentation()); 41173e1ef7849845a9e7bf79156bbb8a2c26d77a1d2eJeffrey Yasskin if (Value.convertToInteger(IntegerValue, 4118f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth llvm::APFloat::rmTowardZero, &isExact) 41199ce6377714a8eb8f577c87028300421e72b00dc9Matt Beaumont-Gay == llvm::APFloat::opOK && isExact) 4120f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth return; 4121f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth 4122be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie SmallString<16> PrettySourceValue; 4123be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie Value.toString(PrettySourceValue); 4124de7e7b857c8c6f9dc7fecb18fcb947b003748d16David Blaikie SmallString<16> PrettyTargetValue; 4125be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie if (T->isSpecificBuiltinType(BuiltinType::Bool)) 4126be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie PrettyTargetValue = IntegerValue == 0 ? "false" : "true"; 4127be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie else 4128de7e7b857c8c6f9dc7fecb18fcb947b003748d16David Blaikie IntegerValue.toString(PrettyTargetValue); 4129be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie 41309ce6377714a8eb8f577c87028300421e72b00dc9Matt Beaumont-Gay S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) 4131be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie << FL->getType() << T.getUnqualifiedType() << PrettySourceValue 4132be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); 4133f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth} 4134f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth 4135091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCallstd::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { 4136091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall if (!Range.Width) return "0"; 4137091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 4138091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall llvm::APSInt ValueInRange = Value; 4139091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall ValueInRange.setIsSigned(!Range.NonNegative); 41409f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad ValueInRange = ValueInRange.trunc(Range.Width); 4141091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return ValueInRange.toString(10); 4142091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall} 4143091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 4144323ed74658bc8375278eabf074b4777458376540John McCallvoid CheckImplicitConversion(Sema &S, Expr *E, QualType T, 4145b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall SourceLocation CC, bool *ICContext = 0) { 4146323ed74658bc8375278eabf074b4777458376540John McCall if (E->isTypeDependent() || E->isValueDependent()) return; 414751313c39c84407dd6a323be99a8c322bf8d052a9John McCall 4148323ed74658bc8375278eabf074b4777458376540John McCall const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); 4149323ed74658bc8375278eabf074b4777458376540John McCall const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); 4150323ed74658bc8375278eabf074b4777458376540John McCall if (Source == Target) return; 4151323ed74658bc8375278eabf074b4777458376540John McCall if (Target->isDependentType()) return; 415251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 4153108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth // If the conversion context location is invalid don't complain. We also 4154108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth // don't want to emit a warning if the issue occurs from the expansion of 4155108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth // a system macro. The problem is that 'getSpellingLoc()' is slow, so we 4156108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth // delay this check as long as possible. Once we detect we are in that 4157108f756bebd991eaa980cfb9994353612a2e5ff6Chandler Carruth // scenario, we just return. 4158ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek if (CC.isInvalid()) 4159b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall return; 4160b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall 4161f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu // Diagnose implicit casts to bool. 4162f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { 4163f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu if (isa<StringLiteral>(E)) 4164f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu // Warn on string literal to bool. Checks for string literals in logical 4165f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu // expressions, for instances, assert(0 && "error here"), is prevented 4166f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu // by a check in AnalyzeImplicitConversions(). 4167f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu return DiagnoseImpCast(S, E, T, CC, 4168f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu diag::warn_impcast_string_literal_to_bool); 4169e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames if (Source->isFunctionType()) { 4170e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames // Warn on function to bool. Checks free functions and static member 4171e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames // functions. Weakly imported functions are excluded from the check, 4172e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames // since it's common to test their value to check whether the linker 4173e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames // found a definition for them. 4174e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames ValueDecl *D = 0; 4175e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) { 4176e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames D = R->getDecl(); 4177e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { 4178e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames D = M->getMemberDecl(); 4179e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames } 4180e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames 4181e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames if (D && !D->isWeak()) { 418226b45d86085a125af036dbcf85dad3087b664ab2Richard Trieu if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) { 418326b45d86085a125af036dbcf85dad3087b664ab2Richard Trieu S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) 418426b45d86085a125af036dbcf85dad3087b664ab2Richard Trieu << F << E->getSourceRange() << SourceRange(CC); 41852def77399ab3088106a2d61372344f5b7104e1a2David Blaikie S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence) 41862def77399ab3088106a2d61372344f5b7104e1a2David Blaikie << FixItHint::CreateInsertion(E->getExprLoc(), "&"); 41872def77399ab3088106a2d61372344f5b7104e1a2David Blaikie QualType ReturnType; 41882def77399ab3088106a2d61372344f5b7104e1a2David Blaikie UnresolvedSet<4> NonTemplateOverloads; 41892def77399ab3088106a2d61372344f5b7104e1a2David Blaikie S.isExprCallable(*E, ReturnType, NonTemplateOverloads); 41902def77399ab3088106a2d61372344f5b7104e1a2David Blaikie if (!ReturnType.isNull() 41912def77399ab3088106a2d61372344f5b7104e1a2David Blaikie && ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) 41922def77399ab3088106a2d61372344f5b7104e1a2David Blaikie S.Diag(E->getExprLoc(), diag::note_function_to_bool_call) 41932def77399ab3088106a2d61372344f5b7104e1a2David Blaikie << FixItHint::CreateInsertion( 41942def77399ab3088106a2d61372344f5b7104e1a2David Blaikie S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()"); 419526b45d86085a125af036dbcf85dad3087b664ab2Richard Trieu return; 419626b45d86085a125af036dbcf85dad3087b664ab2Richard Trieu } 4197e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames } 4198e14ca9f509dc22d8fcc5d21d70d28a5848983b42Lang Hames } 4199f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu } 420051313c39c84407dd6a323be99a8c322bf8d052a9John McCall 420151313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Strip vector types. 420251313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (isa<VectorType>(Source)) { 4203ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek if (!isa<VectorType>(Target)) { 4204d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (S.SourceMgr.isInSystemMacro(CC)) 4205ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek return; 4206b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); 4207ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek } 4208b792b30ce3d31e3eb3cfee3321238cf3cc2165b0Chris Lattner 4209b792b30ce3d31e3eb3cfee3321238cf3cc2165b0Chris Lattner // If the vector cast is cast between two vectors of the same size, it is 4210b792b30ce3d31e3eb3cfee3321238cf3cc2165b0Chris Lattner // a bitcast, not a conversion. 4211b792b30ce3d31e3eb3cfee3321238cf3cc2165b0Chris Lattner if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) 4212b792b30ce3d31e3eb3cfee3321238cf3cc2165b0Chris Lattner return; 421351313c39c84407dd6a323be99a8c322bf8d052a9John McCall 421451313c39c84407dd6a323be99a8c322bf8d052a9John McCall Source = cast<VectorType>(Source)->getElementType().getTypePtr(); 421551313c39c84407dd6a323be99a8c322bf8d052a9John McCall Target = cast<VectorType>(Target)->getElementType().getTypePtr(); 421651313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 421751313c39c84407dd6a323be99a8c322bf8d052a9John McCall 421851313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Strip complex types. 421951313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (isa<ComplexType>(Source)) { 4220ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek if (!isa<ComplexType>(Target)) { 4221d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (S.SourceMgr.isInSystemMacro(CC)) 4222ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek return; 4223ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek 4224b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); 4225ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek } 422651313c39c84407dd6a323be99a8c322bf8d052a9John McCall 422751313c39c84407dd6a323be99a8c322bf8d052a9John McCall Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); 422851313c39c84407dd6a323be99a8c322bf8d052a9John McCall Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); 422951313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 423051313c39c84407dd6a323be99a8c322bf8d052a9John McCall 423151313c39c84407dd6a323be99a8c322bf8d052a9John McCall const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); 423251313c39c84407dd6a323be99a8c322bf8d052a9John McCall const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); 423351313c39c84407dd6a323be99a8c322bf8d052a9John McCall 423451313c39c84407dd6a323be99a8c322bf8d052a9John McCall // If the source is floating point... 423551313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (SourceBT && SourceBT->isFloatingPoint()) { 423651313c39c84407dd6a323be99a8c322bf8d052a9John McCall // ...and the target is floating point... 423751313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (TargetBT && TargetBT->isFloatingPoint()) { 423851313c39c84407dd6a323be99a8c322bf8d052a9John McCall // ...then warn if we're dropping FP rank. 423951313c39c84407dd6a323be99a8c322bf8d052a9John McCall 424051313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Builtin FP kinds are ordered by increasing FP rank. 424151313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (SourceBT->getKind() > TargetBT->getKind()) { 424251313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Don't warn about float constants that are precisely 424351313c39c84407dd6a323be99a8c322bf8d052a9John McCall // representable in the target type. 424451313c39c84407dd6a323be99a8c322bf8d052a9John McCall Expr::EvalResult result; 424551f4708c00110940ca3f337961915f2ca1668375Richard Smith if (E->EvaluateAsRValue(result, S.Context)) { 424651313c39c84407dd6a323be99a8c322bf8d052a9John McCall // Value might be a float, a float vector, or a float complex. 424751313c39c84407dd6a323be99a8c322bf8d052a9John McCall if (IsSameFloatAfterCast(result.Val, 4248323ed74658bc8375278eabf074b4777458376540John McCall S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), 4249323ed74658bc8375278eabf074b4777458376540John McCall S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) 425051313c39c84407dd6a323be99a8c322bf8d052a9John McCall return; 425151313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 425251313c39c84407dd6a323be99a8c322bf8d052a9John McCall 4253d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (S.SourceMgr.isInSystemMacro(CC)) 4254ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek return; 4255ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek 4256b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); 425751313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 425851313c39c84407dd6a323be99a8c322bf8d052a9John McCall return; 425951313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 426051313c39c84407dd6a323be99a8c322bf8d052a9John McCall 4261ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek // If the target is integral, always warn. 4262be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie if (TargetBT && TargetBT->isInteger()) { 4263d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (S.SourceMgr.isInSystemMacro(CC)) 4264ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek return; 4265ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek 4266a5b9332418f25338f118358e27303cd510d54107Chandler Carruth Expr *InnerE = E->IgnoreParenImpCasts(); 4267634c8af0805ba33b1530470e03aa314141036aa2Matt Beaumont-Gay // We also want to warn on, e.g., "int i = -1.234" 4268634c8af0805ba33b1530470e03aa314141036aa2Matt Beaumont-Gay if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) 4269634c8af0805ba33b1530470e03aa314141036aa2Matt Beaumont-Gay if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) 4270634c8af0805ba33b1530470e03aa314141036aa2Matt Beaumont-Gay InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); 4271634c8af0805ba33b1530470e03aa314141036aa2Matt Beaumont-Gay 4272f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { 4273f65076ebf5e8bc5ae7b77fe3c3a65dc5db8d763fChandler Carruth DiagnoseFloatingLiteralImpCast(S, FL, T, CC); 4274a5b9332418f25338f118358e27303cd510d54107Chandler Carruth } else { 4275a5b9332418f25338f118358e27303cd510d54107Chandler Carruth DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); 4276a5b9332418f25338f118358e27303cd510d54107Chandler Carruth } 4277a5b9332418f25338f118358e27303cd510d54107Chandler Carruth } 427851313c39c84407dd6a323be99a8c322bf8d052a9John McCall 427951313c39c84407dd6a323be99a8c322bf8d052a9John McCall return; 428051313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 428151313c39c84407dd6a323be99a8c322bf8d052a9John McCall 4282f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (!Source->isIntegerType() || !Target->isIntegerType()) 428351313c39c84407dd6a323be99a8c322bf8d052a9John McCall return; 428451313c39c84407dd6a323be99a8c322bf8d052a9John McCall 42851838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) 42861838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu == Expr::NPCK_GNUNull) && Target->isIntegerType()) { 4287b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie SourceLocation Loc = E->getSourceRange().getBegin(); 4288b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie if (Loc.isMacroID()) 4289b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; 42909fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie if (!Loc.isMacroID() || CC.isMacroID()) 42919fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) 42929fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie << T << clang::SourceRange(CC) 42939fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T)); 42941838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu return; 42951838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu } 42961838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu 4297be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie // TODO: remove this early return once the false positives for constant->bool 4298be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie // in templates, macros, etc, are reduced or removed. 4299be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie if (Target->isSpecificBuiltinType(BuiltinType::Bool)) 4300be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie return; 4301be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie 4302323ed74658bc8375278eabf074b4777458376540John McCall IntRange SourceRange = GetExprRange(S.Context, E); 43031844a6e1074a5c1fd8eadac7fc7317c3e29d4653John McCall IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); 430451313c39c84407dd6a323be99a8c322bf8d052a9John McCall 4305f2370c9b4aade940e2253b5b33262ba507d1d71fJohn McCall if (SourceRange.Width > TargetRange.Width) { 4306091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall // If the source is a constant, use a default-on diagnostic. 4307091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall // TODO: this should happen for bitfield stores, too. 4308091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall llvm::APSInt Value(32); 4309091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall if (E->isIntegerConstantExpr(Value, S.Context)) { 4310d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (S.SourceMgr.isInSystemMacro(CC)) 4311ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek return; 4312ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek 4313091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall std::string PrettySourceValue = Value.toString(10); 4314091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 4315091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 43165e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek S.DiagRuntimeBehavior(E->getExprLoc(), E, 43175e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek S.PDiag(diag::warn_impcast_integer_precision_constant) 43185e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek << PrettySourceValue << PrettyTargetValue 43195e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek << E->getType() << T << E->getSourceRange() 43205e745da03df27ac620b0a5cc280d0d1757cfbac1Ted Kremenek << clang::SourceRange(CC)); 4321091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall return; 4322091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall } 4323091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 4324b792b30ce3d31e3eb3cfee3321238cf3cc2165b0Chris Lattner // People want to build with -Wshorten-64-to-32 and not -Wconversion. 4325d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (S.SourceMgr.isInSystemMacro(CC)) 4326ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek return; 4327ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek 432837050841d7b852497b85d5275d9ea92c07ddf059David Blaikie if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) 4329c36bedc90c687caa71748480c60707ea4608b092Anna Zaks return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, 4330c36bedc90c687caa71748480c60707ea4608b092Anna Zaks /* pruneControlFlow */ true); 4331b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); 4332323ed74658bc8375278eabf074b4777458376540John McCall } 4333323ed74658bc8375278eabf074b4777458376540John McCall 4334323ed74658bc8375278eabf074b4777458376540John McCall if ((TargetRange.NonNegative && !SourceRange.NonNegative) || 4335323ed74658bc8375278eabf074b4777458376540John McCall (!TargetRange.NonNegative && SourceRange.NonNegative && 4336323ed74658bc8375278eabf074b4777458376540John McCall SourceRange.Width == TargetRange.Width)) { 4337ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek 4338d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (S.SourceMgr.isInSystemMacro(CC)) 4339ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek return; 4340ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek 4341323ed74658bc8375278eabf074b4777458376540John McCall unsigned DiagID = diag::warn_impcast_integer_sign; 4342323ed74658bc8375278eabf074b4777458376540John McCall 4343323ed74658bc8375278eabf074b4777458376540John McCall // Traditionally, gcc has warned about this under -Wsign-compare. 4344323ed74658bc8375278eabf074b4777458376540John McCall // We also want to warn about it in -Wconversion. 4345323ed74658bc8375278eabf074b4777458376540John McCall // So if -Wconversion is off, use a completely identical diagnostic 4346323ed74658bc8375278eabf074b4777458376540John McCall // in the sign-compare group. 4347323ed74658bc8375278eabf074b4777458376540John McCall // The conditional-checking code will 4348323ed74658bc8375278eabf074b4777458376540John McCall if (ICContext) { 4349323ed74658bc8375278eabf074b4777458376540John McCall DiagID = diag::warn_impcast_integer_sign_conditional; 4350323ed74658bc8375278eabf074b4777458376540John McCall *ICContext = true; 4351323ed74658bc8375278eabf074b4777458376540John McCall } 4352323ed74658bc8375278eabf074b4777458376540John McCall 4353b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall return DiagnoseImpCast(S, E, T, CC, DiagID); 435451313c39c84407dd6a323be99a8c322bf8d052a9John McCall } 435551313c39c84407dd6a323be99a8c322bf8d052a9John McCall 4356284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor // Diagnose conversions between different enumeration types. 43575a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor // In C, we pretend that the type of an EnumConstantDecl is its enumeration 43585a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor // type, to give us better diagnostics. 43595a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor QualType SourceType = E->getType(); 43604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!S.getLangOpts().CPlusPlus) { 43615a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 43625a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { 43635a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); 43645a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor SourceType = S.Context.getTypeDeclType(Enum); 43655a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor Source = S.Context.getCanonicalType(SourceType).getTypePtr(); 43665a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor } 43675a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor } 43685a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor 4369284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor if (const EnumType *SourceEnum = Source->getAs<EnumType>()) 4370284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor if (const EnumType *TargetEnum = Target->getAs<EnumType>()) 4371284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor if ((SourceEnum->getDecl()->getIdentifier() || 4372162e1c1b487352434552147967c3dd296ebee2f7Richard Smith SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && 4373284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor (TargetEnum->getDecl()->getIdentifier() || 4374162e1c1b487352434552147967c3dd296ebee2f7Richard Smith TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && 4375ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek SourceEnum != TargetEnum) { 4376d87a0cd2b3e1c9e9f01212875f4cbe5b7bb7ab57Matt Beaumont-Gay if (S.SourceMgr.isInSystemMacro(CC)) 4377ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek return; 4378ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek 43795a5b38f4afaf4f203b96a11ba79890c7cd4cc4b8Douglas Gregor return DiagnoseImpCast(S, E, SourceType, T, CC, 4380284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor diag::warn_impcast_different_enum_types); 4381ef9ff88f54ff26ac9782d98a34b74aa424149be1Ted Kremenek } 4382284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758Douglas Gregor 438351313c39c84407dd6a323be99a8c322bf8d052a9John McCall return; 438451313c39c84407dd6a323be99a8c322bf8d052a9John McCall} 438551313c39c84407dd6a323be99a8c322bf8d052a9John McCall 43869fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikievoid CheckConditionalOperator(Sema &S, ConditionalOperator *E, 43879fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie SourceLocation CC, QualType T); 4388323ed74658bc8375278eabf074b4777458376540John McCall 4389323ed74658bc8375278eabf074b4777458376540John McCallvoid CheckConditionalOperand(Sema &S, Expr *E, QualType T, 4390b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall SourceLocation CC, bool &ICContext) { 4391323ed74658bc8375278eabf074b4777458376540John McCall E = E->IgnoreParenImpCasts(); 4392323ed74658bc8375278eabf074b4777458376540John McCall 4393323ed74658bc8375278eabf074b4777458376540John McCall if (isa<ConditionalOperator>(E)) 43949fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); 4395323ed74658bc8375278eabf074b4777458376540John McCall 4396b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall AnalyzeImplicitConversions(S, E, CC); 4397323ed74658bc8375278eabf074b4777458376540John McCall if (E->getType() != T) 4398b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall return CheckImplicitConversion(S, E, T, CC, &ICContext); 4399323ed74658bc8375278eabf074b4777458376540John McCall return; 4400323ed74658bc8375278eabf074b4777458376540John McCall} 4401323ed74658bc8375278eabf074b4777458376540John McCall 44029fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikievoid CheckConditionalOperator(Sema &S, ConditionalOperator *E, 44039fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie SourceLocation CC, QualType T) { 4404b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall AnalyzeImplicitConversions(S, E->getCond(), CC); 4405323ed74658bc8375278eabf074b4777458376540John McCall 4406323ed74658bc8375278eabf074b4777458376540John McCall bool Suspicious = false; 4407b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); 4408b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); 4409323ed74658bc8375278eabf074b4777458376540John McCall 4410323ed74658bc8375278eabf074b4777458376540John McCall // If -Wconversion would have warned about either of the candidates 4411323ed74658bc8375278eabf074b4777458376540John McCall // for a signedness conversion to the context type... 4412323ed74658bc8375278eabf074b4777458376540John McCall if (!Suspicious) return; 4413323ed74658bc8375278eabf074b4777458376540John McCall 4414323ed74658bc8375278eabf074b4777458376540John McCall // ...but it's currently ignored... 44150827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, 44160827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis CC)) 4417323ed74658bc8375278eabf074b4777458376540John McCall return; 4418323ed74658bc8375278eabf074b4777458376540John McCall 4419323ed74658bc8375278eabf074b4777458376540John McCall // ...then check whether it would have warned about either of the 4420323ed74658bc8375278eabf074b4777458376540John McCall // candidates for a signedness conversion to the condition type. 44215254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu if (E->getType() == T) return; 44225254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 44235254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu Suspicious = false; 44245254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), 44255254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu E->getType(), CC, &Suspicious); 44265254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu if (!Suspicious) 44275254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), 4428b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall E->getType(), CC, &Suspicious); 4429323ed74658bc8375278eabf074b4777458376540John McCall} 4430323ed74658bc8375278eabf074b4777458376540John McCall 4431323ed74658bc8375278eabf074b4777458376540John McCall/// AnalyzeImplicitConversions - Find and report any interesting 4432323ed74658bc8375278eabf074b4777458376540John McCall/// implicit conversions in the given expression. There are a couple 4433323ed74658bc8375278eabf074b4777458376540John McCall/// of competing diagnostics here, -Wconversion and -Wsign-compare. 4434b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCallvoid AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { 4435323ed74658bc8375278eabf074b4777458376540John McCall QualType T = OrigE->getType(); 4436323ed74658bc8375278eabf074b4777458376540John McCall Expr *E = OrigE->IgnoreParenImpCasts(); 4437323ed74658bc8375278eabf074b4777458376540John McCall 4438f8b6e1591b5c5085cf4dec8df657e32a68053b12Douglas Gregor if (E->isTypeDependent() || E->isValueDependent()) 4439f8b6e1591b5c5085cf4dec8df657e32a68053b12Douglas Gregor return; 4440f8b6e1591b5c5085cf4dec8df657e32a68053b12Douglas Gregor 4441323ed74658bc8375278eabf074b4777458376540John McCall // For conditional operators, we analyze the arguments as if they 4442323ed74658bc8375278eabf074b4777458376540John McCall // were being fed directly into the output. 4443323ed74658bc8375278eabf074b4777458376540John McCall if (isa<ConditionalOperator>(E)) { 4444323ed74658bc8375278eabf074b4777458376540John McCall ConditionalOperator *CO = cast<ConditionalOperator>(E); 44459fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie CheckConditionalOperator(S, CO, CC, T); 4446323ed74658bc8375278eabf074b4777458376540John McCall return; 4447323ed74658bc8375278eabf074b4777458376540John McCall } 4448323ed74658bc8375278eabf074b4777458376540John McCall 4449323ed74658bc8375278eabf074b4777458376540John McCall // Go ahead and check any implicit conversions we might have skipped. 4450323ed74658bc8375278eabf074b4777458376540John McCall // The non-canonical typecheck is just an optimization; 4451323ed74658bc8375278eabf074b4777458376540John McCall // CheckImplicitConversion will filter out dead implicit conversions. 4452323ed74658bc8375278eabf074b4777458376540John McCall if (E->getType() != T) 4453b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CheckImplicitConversion(S, E, T, CC); 4454323ed74658bc8375278eabf074b4777458376540John McCall 4455323ed74658bc8375278eabf074b4777458376540John McCall // Now continue drilling into this expression. 4456323ed74658bc8375278eabf074b4777458376540John McCall 4457323ed74658bc8375278eabf074b4777458376540John McCall // Skip past explicit casts. 4458323ed74658bc8375278eabf074b4777458376540John McCall if (isa<ExplicitCastExpr>(E)) { 4459323ed74658bc8375278eabf074b4777458376540John McCall E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); 4460b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall return AnalyzeImplicitConversions(S, E, CC); 4461323ed74658bc8375278eabf074b4777458376540John McCall } 4462323ed74658bc8375278eabf074b4777458376540John McCall 4463beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 4464beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall // Do a somewhat different check with comparison operators. 4465beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall if (BO->isComparisonOp()) 4466beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall return AnalyzeComparison(S, BO); 4467beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall 44680fa063816818ac53a0015a5d2b0328812005b40eEli Friedman // And with simple assignments. 44690fa063816818ac53a0015a5d2b0328812005b40eEli Friedman if (BO->getOpcode() == BO_Assign) 4470beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall return AnalyzeAssignment(S, BO); 4471beb22aaa22e255241d6a81e8b0a9239f5fa584f3John McCall } 4472323ed74658bc8375278eabf074b4777458376540John McCall 4473323ed74658bc8375278eabf074b4777458376540John McCall // These break the otherwise-useful invariant below. Fortunately, 4474323ed74658bc8375278eabf074b4777458376540John McCall // we don't really need to recurse into them, because any internal 4475323ed74658bc8375278eabf074b4777458376540John McCall // expressions should have been analyzed already when they were 4476323ed74658bc8375278eabf074b4777458376540John McCall // built into statements. 4477323ed74658bc8375278eabf074b4777458376540John McCall if (isa<StmtExpr>(E)) return; 4478323ed74658bc8375278eabf074b4777458376540John McCall 4479323ed74658bc8375278eabf074b4777458376540John McCall // Don't descend into unevaluated contexts. 4480f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (isa<UnaryExprOrTypeTraitExpr>(E)) return; 4481323ed74658bc8375278eabf074b4777458376540John McCall 4482323ed74658bc8375278eabf074b4777458376540John McCall // Now just recurse over the expression's children. 4483b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall CC = E->getExprLoc(); 4484f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu BinaryOperator *BO = dyn_cast<BinaryOperator>(E); 4485f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu bool IsLogicalOperator = BO && BO->isLogicalOp(); 4486f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu for (Stmt::child_range I = E->children(); I; ++I) { 448754042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); 4488503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor if (!ChildExpr) 4489503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor continue; 4490503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor 4491f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu if (IsLogicalOperator && 4492f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) 4493f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu // Ignore checking string literals that are in logical operators. 4494f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu continue; 4495f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu AnalyzeImplicitConversions(S, ChildExpr, CC); 4496f1f8b1a404d9ce6f0eb78e97b598a220d8ca9090Richard Trieu } 4497323ed74658bc8375278eabf074b4777458376540John McCall} 4498323ed74658bc8375278eabf074b4777458376540John McCall 4499323ed74658bc8375278eabf074b4777458376540John McCall} // end anonymous namespace 4500323ed74658bc8375278eabf074b4777458376540John McCall 4501323ed74658bc8375278eabf074b4777458376540John McCall/// Diagnoses "dangerous" implicit conversions within the given 4502323ed74658bc8375278eabf074b4777458376540John McCall/// expression (which is a full expression). Implements -Wconversion 4503323ed74658bc8375278eabf074b4777458376540John McCall/// and -Wsign-compare. 4504b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall/// 4505b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall/// \param CC the "context" location of the implicit conversion, i.e. 4506b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall/// the most location of the syntactic entity requiring the implicit 4507b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall/// conversion 4508b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCallvoid Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { 4509323ed74658bc8375278eabf074b4777458376540John McCall // Don't diagnose in unevaluated contexts. 4510323ed74658bc8375278eabf074b4777458376540John McCall if (ExprEvalContexts.back().Context == Sema::Unevaluated) 4511323ed74658bc8375278eabf074b4777458376540John McCall return; 4512323ed74658bc8375278eabf074b4777458376540John McCall 4513323ed74658bc8375278eabf074b4777458376540John McCall // Don't diagnose for value- or type-dependent expressions. 4514323ed74658bc8375278eabf074b4777458376540John McCall if (E->isTypeDependent() || E->isValueDependent()) 4515323ed74658bc8375278eabf074b4777458376540John McCall return; 4516323ed74658bc8375278eabf074b4777458376540John McCall 4517d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // Check for array bounds violations in cases where the check isn't triggered 4518d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // elsewhere for other Expr types (like BinaryOperators), e.g. when an 4519d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // ArraySubscriptExpr is on the RHS of a variable initialization. 4520d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain CheckArrayAccess(E); 4521d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain 4522b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall // This is not the right CC for (e.g.) a variable initialization. 4523b4eb64d8426c0eaa58d398961e0e74ff85063d7cJohn McCall AnalyzeImplicitConversions(*this, E, CC); 4524323ed74658bc8375278eabf074b4777458376540John McCall} 4525323ed74658bc8375278eabf074b4777458376540John McCall 452615d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCallvoid Sema::CheckBitFieldInitialization(SourceLocation InitLoc, 452715d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall FieldDecl *BitField, 452815d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall Expr *Init) { 452915d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); 453015d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall} 453115d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall 4532f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump/// CheckParmsForFunctionDef - Check that the parameters of the given 4533f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump/// function are appropriate for the definition of a function. This 4534f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump/// takes care of any checks that cannot be performed on the 4535f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump/// declaration itself, e.g., that the types of each of the function 4536f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump/// parameters are complete. 453782aa713bcda99f388836c2a30bb868d9c9974817Douglas Gregorbool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, 453882aa713bcda99f388836c2a30bb868d9c9974817Douglas Gregor bool CheckParameterNames) { 4539f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump bool HasInvalidParm = false; 454082aa713bcda99f388836c2a30bb868d9c9974817Douglas Gregor for (; P != PEnd; ++P) { 454182aa713bcda99f388836c2a30bb868d9c9974817Douglas Gregor ParmVarDecl *Param = *P; 454282aa713bcda99f388836c2a30bb868d9c9974817Douglas Gregor 4543f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump // C99 6.7.5.3p4: the parameters in a parameter type list in a 4544f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump // function declarator that is part of a function definition of 4545f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump // that function shall not have incomplete type. 4546f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump // 4547f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump // This is also C++ [dcl.fct]p6. 4548f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump if (!Param->isInvalidDecl() && 4549f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump RequireCompleteType(Param->getLocation(), Param->getType(), 4550d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor diag::err_typecheck_decl_incomplete_type)) { 4551f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump Param->setInvalidDecl(); 4552f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump HasInvalidParm = true; 4553f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump } 4554f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump 4555f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump // C99 6.9.1p5: If the declarator includes a parameter type list, the 4556f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump // declaration of each parameter shall include an identifier. 455782aa713bcda99f388836c2a30bb868d9c9974817Douglas Gregor if (CheckParameterNames && 455882aa713bcda99f388836c2a30bb868d9c9974817Douglas Gregor Param->getIdentifier() == 0 && 4559f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump !Param->isImplicit() && 45604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !getLangOpts().CPlusPlus) 4561f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump Diag(Param->getLocation(), diag::err_parameter_name_omitted); 4562d17e340e2d516139931768697bf080f60920ba9dSam Weinig 4563d17e340e2d516139931768697bf080f60920ba9dSam Weinig // C99 6.7.5.3p12: 4564d17e340e2d516139931768697bf080f60920ba9dSam Weinig // If the function declarator is not part of a definition of that 4565d17e340e2d516139931768697bf080f60920ba9dSam Weinig // function, parameters may have incomplete type and may use the [*] 4566d17e340e2d516139931768697bf080f60920ba9dSam Weinig // notation in their sequences of declarator specifiers to specify 4567d17e340e2d516139931768697bf080f60920ba9dSam Weinig // variable length array types. 4568d17e340e2d516139931768697bf080f60920ba9dSam Weinig QualType PType = Param->getOriginalType(); 4569d17e340e2d516139931768697bf080f60920ba9dSam Weinig if (const ArrayType *AT = Context.getAsArrayType(PType)) { 4570d17e340e2d516139931768697bf080f60920ba9dSam Weinig if (AT->getSizeModifier() == ArrayType::Star) { 4571d17e340e2d516139931768697bf080f60920ba9dSam Weinig // FIXME: This diagnosic should point the the '[*]' if source-location 4572d17e340e2d516139931768697bf080f60920ba9dSam Weinig // information is added for it. 4573d17e340e2d516139931768697bf080f60920ba9dSam Weinig Diag(Param->getLocation(), diag::err_array_star_in_function_definition); 4574d17e340e2d516139931768697bf080f60920ba9dSam Weinig } 4575d17e340e2d516139931768697bf080f60920ba9dSam Weinig } 4576f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump } 4577f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump 4578f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump return HasInvalidParm; 4579f8c4921c73d73123e2b79221ad4f1775ce984cfdMike Stump} 4580b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4581b7f4ffe073fa419613946461a2583ba2fcb72280John McCall/// CheckCastAlign - Implements -Wcast-align, which warns when a 4582b7f4ffe073fa419613946461a2583ba2fcb72280John McCall/// pointer cast increases the alignment requirements. 4583b7f4ffe073fa419613946461a2583ba2fcb72280John McCallvoid Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { 4584b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // This is actually a lot of work to potentially be doing on every 4585b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // cast; don't do it if we're ignoring -Wcast_align (as is the default). 45860827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, 45870827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis TRange.getBegin()) 4588d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie == DiagnosticsEngine::Ignored) 4589b7f4ffe073fa419613946461a2583ba2fcb72280John McCall return; 4590b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4591b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // Ignore dependent types. 4592b7f4ffe073fa419613946461a2583ba2fcb72280John McCall if (T->isDependentType() || Op->getType()->isDependentType()) 4593b7f4ffe073fa419613946461a2583ba2fcb72280John McCall return; 4594b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4595b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // Require that the destination be a pointer type. 4596b7f4ffe073fa419613946461a2583ba2fcb72280John McCall const PointerType *DestPtr = T->getAs<PointerType>(); 4597b7f4ffe073fa419613946461a2583ba2fcb72280John McCall if (!DestPtr) return; 4598b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4599b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // If the destination has alignment 1, we're done. 4600b7f4ffe073fa419613946461a2583ba2fcb72280John McCall QualType DestPointee = DestPtr->getPointeeType(); 4601b7f4ffe073fa419613946461a2583ba2fcb72280John McCall if (DestPointee->isIncompleteType()) return; 4602b7f4ffe073fa419613946461a2583ba2fcb72280John McCall CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); 4603b7f4ffe073fa419613946461a2583ba2fcb72280John McCall if (DestAlign.isOne()) return; 4604b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4605b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // Require that the source be a pointer type. 4606b7f4ffe073fa419613946461a2583ba2fcb72280John McCall const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); 4607b7f4ffe073fa419613946461a2583ba2fcb72280John McCall if (!SrcPtr) return; 4608b7f4ffe073fa419613946461a2583ba2fcb72280John McCall QualType SrcPointee = SrcPtr->getPointeeType(); 4609b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4610b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // Whitelist casts from cv void*. We already implicitly 4611b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // whitelisted casts to cv void*, since they have alignment 1. 4612b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // Also whitelist casts involving incomplete types, which implicitly 4613b7f4ffe073fa419613946461a2583ba2fcb72280John McCall // includes 'void'. 4614b7f4ffe073fa419613946461a2583ba2fcb72280John McCall if (SrcPointee->isIncompleteType()) return; 4615b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4616b7f4ffe073fa419613946461a2583ba2fcb72280John McCall CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); 4617b7f4ffe073fa419613946461a2583ba2fcb72280John McCall if (SrcAlign >= DestAlign) return; 4618b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4619b7f4ffe073fa419613946461a2583ba2fcb72280John McCall Diag(TRange.getBegin(), diag::warn_cast_align) 4620b7f4ffe073fa419613946461a2583ba2fcb72280John McCall << Op->getType() << T 4621b7f4ffe073fa419613946461a2583ba2fcb72280John McCall << static_cast<unsigned>(SrcAlign.getQuantity()) 4622b7f4ffe073fa419613946461a2583ba2fcb72280John McCall << static_cast<unsigned>(DestAlign.getQuantity()) 4623b7f4ffe073fa419613946461a2583ba2fcb72280John McCall << TRange << Op->getSourceRange(); 4624b7f4ffe073fa419613946461a2583ba2fcb72280John McCall} 4625b7f4ffe073fa419613946461a2583ba2fcb72280John McCall 4626d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrainstatic const Type* getElementType(const Expr *BaseExpr) { 4627d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain const Type* EltType = BaseExpr->getType().getTypePtr(); 4628d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain if (EltType->isAnyPointerType()) 4629d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain return EltType->getPointeeType().getTypePtr(); 4630d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain else if (EltType->isArrayType()) 4631d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain return EltType->getBaseElementTypeUnsafe(); 4632d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain return EltType; 4633d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain} 4634d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain 4635c268434b332bbe57950d977633369bf5822d9554Chandler Carruth/// \brief Check whether this array fits the idiom of a size-one tail padded 4636c268434b332bbe57950d977633369bf5822d9554Chandler Carruth/// array member of a struct. 4637c268434b332bbe57950d977633369bf5822d9554Chandler Carruth/// 4638c268434b332bbe57950d977633369bf5822d9554Chandler Carruth/// We avoid emitting out-of-bounds access warnings for such arrays as they are 4639c268434b332bbe57950d977633369bf5822d9554Chandler Carruth/// commonly used to emulate flexible arrays in C89 code. 4640c268434b332bbe57950d977633369bf5822d9554Chandler Carruthstatic bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, 4641c268434b332bbe57950d977633369bf5822d9554Chandler Carruth const NamedDecl *ND) { 4642c268434b332bbe57950d977633369bf5822d9554Chandler Carruth if (Size != 1 || !ND) return false; 4643c268434b332bbe57950d977633369bf5822d9554Chandler Carruth 4644c268434b332bbe57950d977633369bf5822d9554Chandler Carruth const FieldDecl *FD = dyn_cast<FieldDecl>(ND); 4645c268434b332bbe57950d977633369bf5822d9554Chandler Carruth if (!FD) return false; 4646c268434b332bbe57950d977633369bf5822d9554Chandler Carruth 4647c268434b332bbe57950d977633369bf5822d9554Chandler Carruth // Don't consider sizes resulting from macro expansions or template argument 4648c268434b332bbe57950d977633369bf5822d9554Chandler Carruth // substitution to form C89 tail-padded arrays. 4649d2cf348f0df76bf1745f131db2ceeb59f23a7305Sean Callanan 4650d2cf348f0df76bf1745f131db2ceeb59f23a7305Sean Callanan TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); 465100e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek while (TInfo) { 465200e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek TypeLoc TL = TInfo->getTypeLoc(); 465300e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek // Look through typedefs. 465400e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek const TypedefTypeLoc *TTL = dyn_cast<TypedefTypeLoc>(&TL); 465500e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek if (TTL) { 465600e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek const TypedefNameDecl *TDL = TTL->getTypedefNameDecl(); 465700e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek TInfo = TDL->getTypeSourceInfo(); 465800e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek continue; 465900e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek } 466000e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek ConstantArrayTypeLoc CTL = cast<ConstantArrayTypeLoc>(TL); 466100e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); 4662d2cf348f0df76bf1745f131db2ceeb59f23a7305Sean Callanan if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) 4663d2cf348f0df76bf1745f131db2ceeb59f23a7305Sean Callanan return false; 466400e1f6f5fed4f33f67181578be0232accd342282Ted Kremenek break; 4665d2cf348f0df76bf1745f131db2ceeb59f23a7305Sean Callanan } 4666c268434b332bbe57950d977633369bf5822d9554Chandler Carruth 4667c268434b332bbe57950d977633369bf5822d9554Chandler Carruth const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); 4668381711c6e7911d762f81a65e9ef4339a6a3d6524Matt Beaumont-Gay if (!RD) return false; 4669381711c6e7911d762f81a65e9ef4339a6a3d6524Matt Beaumont-Gay if (RD->isUnion()) return false; 4670381711c6e7911d762f81a65e9ef4339a6a3d6524Matt Beaumont-Gay if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 4671381711c6e7911d762f81a65e9ef4339a6a3d6524Matt Beaumont-Gay if (!CRD->isStandardLayout()) return false; 4672381711c6e7911d762f81a65e9ef4339a6a3d6524Matt Beaumont-Gay } 4673c268434b332bbe57950d977633369bf5822d9554Chandler Carruth 467422d4fed17df02f5fa07165a69228b0b0f1a73a28Benjamin Kramer // See if this is the last field decl in the record. 467522d4fed17df02f5fa07165a69228b0b0f1a73a28Benjamin Kramer const Decl *D = FD; 467622d4fed17df02f5fa07165a69228b0b0f1a73a28Benjamin Kramer while ((D = D->getNextDeclInContext())) 467722d4fed17df02f5fa07165a69228b0b0f1a73a28Benjamin Kramer if (isa<FieldDecl>(D)) 467822d4fed17df02f5fa07165a69228b0b0f1a73a28Benjamin Kramer return false; 467922d4fed17df02f5fa07165a69228b0b0f1a73a28Benjamin Kramer return true; 4680c268434b332bbe57950d977633369bf5822d9554Chandler Carruth} 4681c268434b332bbe57950d977633369bf5822d9554Chandler Carruth 4682d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrainvoid Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, 468380fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay const ArraySubscriptExpr *ASE, 468425b009a9d2a79929112d3c28c7dd1730bf5246c8Richard Smith bool AllowOnePastEnd, bool IndexNegated) { 468592b670e1ee0b11d7e558ec659795ddf452e583edEli Friedman IndexExpr = IndexExpr->IgnoreParenImpCasts(); 468680fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay if (IndexExpr->isValueDependent()) 468780fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay return; 4688d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain 468980fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay const Type *EffectiveType = getElementType(BaseExpr); 469080fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay BaseExpr = BaseExpr->IgnoreParenCasts(); 46913406458512c3cdfe2076c46b9aedb4621297efd5Chandler Carruth const ConstantArrayType *ArrayTy = 4692d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain Context.getAsConstantArrayType(BaseExpr->getType()); 46933406458512c3cdfe2076c46b9aedb4621297efd5Chandler Carruth if (!ArrayTy) 4694a0125d8520f65aca581378c235384e7affefa1fcTed Kremenek return; 469535001ca261f895817916b468379b696d6d45959dChandler Carruth 46963406458512c3cdfe2076c46b9aedb4621297efd5Chandler Carruth llvm::APSInt index; 469780fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay if (!IndexExpr->EvaluateAsInt(index, Context)) 4698a0125d8520f65aca581378c235384e7affefa1fcTed Kremenek return; 469925b009a9d2a79929112d3c28c7dd1730bf5246c8Richard Smith if (IndexNegated) 470025b009a9d2a79929112d3c28c7dd1730bf5246c8Richard Smith index = -index; 47018fd0a5dfb025a51f48b7931b95efbf35d3c5dfc3Ted Kremenek 4702ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth const NamedDecl *ND = NULL; 4703ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 4704ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth ND = dyn_cast<NamedDecl>(DRE->getDecl()); 4705c268434b332bbe57950d977633369bf5822d9554Chandler Carruth if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) 4706ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); 4707ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth 47089e060ca641a1f845cecb3371b3a3018d306a5198Ted Kremenek if (index.isUnsigned() || !index.isNegative()) { 470925b3b849e3ad340b04442707038c86980035765bTed Kremenek llvm::APInt size = ArrayTy->getSize(); 471035001ca261f895817916b468379b696d6d45959dChandler Carruth if (!size.isStrictlyPositive()) 471135001ca261f895817916b468379b696d6d45959dChandler Carruth return; 4712d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain 4713d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain const Type* BaseType = getElementType(BaseExpr); 4714de5998f1d7dcecb777257ec932142d65de636eb8Nico Weber if (BaseType != EffectiveType) { 4715d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // Make sure we're comparing apples to apples when comparing index to size 4716d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); 4717d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain uint64_t array_typesize = Context.getTypeSize(BaseType); 4718d10f4bc5a05ce0cdc1646dbb0f706bb9d3dfc0c5Kaelyn Uhrain // Handle ptrarith_typesize being zero, such as when casting to void* 471918f169770d9d3f826bad8f1359c8c5770c5ab16fKaelyn Uhrain if (!ptrarith_typesize) ptrarith_typesize = 1; 4720d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain if (ptrarith_typesize != array_typesize) { 4721d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // There's a cast to a different size type involved 4722d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain uint64_t ratio = array_typesize / ptrarith_typesize; 4723d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // TODO: Be smarter about handling cases where array_typesize is not a 4724d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // multiple of ptrarith_typesize 4725d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain if (ptrarith_typesize * ratio == array_typesize) 4726d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain size *= llvm::APInt(size.getBitWidth(), ratio); 4727d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain } 4728d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain } 4729d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain 47303406458512c3cdfe2076c46b9aedb4621297efd5Chandler Carruth if (size.getBitWidth() > index.getBitWidth()) 473192b670e1ee0b11d7e558ec659795ddf452e583edEli Friedman index = index.zext(size.getBitWidth()); 473225b3b849e3ad340b04442707038c86980035765bTed Kremenek else if (size.getBitWidth() < index.getBitWidth()) 473392b670e1ee0b11d7e558ec659795ddf452e583edEli Friedman size = size.zext(index.getBitWidth()); 473425b3b849e3ad340b04442707038c86980035765bTed Kremenek 4735d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // For array subscripting the index must be less than size, but for pointer 4736d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // arithmetic also allow the index (offset) to be equal to size since 4737d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // computing the next address after the end of the array is legal and 4738d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // commonly done e.g. in C++ iterators and range-based for loops. 473992b670e1ee0b11d7e558ec659795ddf452e583edEli Friedman if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) 4740ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth return; 4741ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth 4742ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth // Also don't warn for arrays of size 1 which are members of some 4743ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth // structure. These are often used to approximate flexible arrays in C89 4744ba44712b0e578a851751e199bb75dbbff5836fedChandler Carruth // code. 4745d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain if (IsTailPaddedMemberArray(*this, size, ND)) 47468fd0a5dfb025a51f48b7931b95efbf35d3c5dfc3Ted Kremenek return; 47473406458512c3cdfe2076c46b9aedb4621297efd5Chandler Carruth 474880fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay // Suppress the warning if the subscript expression (as identified by the 474980fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay // ']' location) and the index expression are both from macro expansions 475080fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay // within a system header. 475180fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay if (ASE) { 475280fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( 475380fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay ASE->getRBracketLoc()); 475480fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay if (SourceMgr.isInSystemHeader(RBracketLoc)) { 475580fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay SourceLocation IndexLoc = SourceMgr.getSpellingLoc( 475680fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay IndexExpr->getLocStart()); 475780fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc)) 475880fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay return; 475980fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay } 476080fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay } 476180fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay 4762d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; 476380fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay if (ASE) 4764d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain DiagID = diag::warn_array_index_exceeds_bounds; 4765d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain 4766d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, 4767d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain PDiag(DiagID) << index.toString(10, true) 4768d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain << size.toString(10, true) 4769d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain << (unsigned)size.getLimitedValue(~0U) 4770d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain << IndexExpr->getSourceRange()); 47713406458512c3cdfe2076c46b9aedb4621297efd5Chandler Carruth } else { 4772d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain unsigned DiagID = diag::warn_array_index_precedes_bounds; 477380fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay if (!ASE) { 4774d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain DiagID = diag::warn_ptr_arith_precedes_bounds; 4775d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain if (index.isNegative()) index = -index; 4776d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain } 4777d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain 4778d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, 4779d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain PDiag(DiagID) << index.toString(10, true) 4780d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain << IndexExpr->getSourceRange()); 4781a0125d8520f65aca581378c235384e7affefa1fcTed Kremenek } 478235001ca261f895817916b468379b696d6d45959dChandler Carruth 4783cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay if (!ND) { 4784cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay // Try harder to find a NamedDecl to point at in the note. 4785cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay while (const ArraySubscriptExpr *ASE = 4786cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay dyn_cast<ArraySubscriptExpr>(BaseExpr)) 4787cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay BaseExpr = ASE->getBase()->IgnoreParenCasts(); 4788cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 4789cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay ND = dyn_cast<NamedDecl>(DRE->getDecl()); 4790cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) 4791cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); 4792cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay } 4793cfbc5b5660f49fae63acc1f5ef05df87070a2a3cMatt Beaumont-Gay 479435001ca261f895817916b468379b696d6d45959dChandler Carruth if (ND) 4795d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, 4796d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain PDiag(diag::note_array_index_out_of_bounds) 4797d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain << ND->getDeclName()); 47983aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek} 47993aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek 48003aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenekvoid Sema::CheckArrayAccess(const Expr *expr) { 4801d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain int AllowOnePastEnd = 0; 4802d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain while (expr) { 4803d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain expr = expr->IgnoreParenImpCasts(); 48043aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek switch (expr->getStmtClass()) { 4805d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain case Stmt::ArraySubscriptExprClass: { 4806d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); 480780fb7dd8e0c56632dc68f5864f70abb2fde0e1dfMatt Beaumont-Gay CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, 4808d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain AllowOnePastEnd > 0); 48093aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek return; 4810d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain } 4811d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain case Stmt::UnaryOperatorClass: { 4812d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain // Only unwrap the * and & unary operators 4813d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain const UnaryOperator *UO = cast<UnaryOperator>(expr); 4814d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain expr = UO->getSubExpr(); 4815d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain switch (UO->getOpcode()) { 4816d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain case UO_AddrOf: 4817d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain AllowOnePastEnd++; 4818d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain break; 4819d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain case UO_Deref: 4820d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain AllowOnePastEnd--; 4821d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain break; 4822d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain default: 4823d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain return; 4824d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain } 4825d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain break; 4826d6c8865e6f306556b7eef972ceec2cc8ba026a0dKaelyn Uhrain } 48273aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek case Stmt::ConditionalOperatorClass: { 48283aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek const ConditionalOperator *cond = cast<ConditionalOperator>(expr); 48293aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek if (const Expr *lhs = cond->getLHS()) 48303aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek CheckArrayAccess(lhs); 48313aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek if (const Expr *rhs = cond->getRHS()) 48323aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek CheckArrayAccess(rhs); 48333aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek return; 48343aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek } 48353aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek default: 48363aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek return; 48373aea4dac830cb03d883a13ab30c28a3a53beca58Ted Kremenek } 4838f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne } 4839a0125d8520f65aca581378c235384e7affefa1fcTed Kremenek} 4840f85e193739c953358c865005855253af4f68a497John McCall 4841f85e193739c953358c865005855253af4f68a497John McCall//===--- CHECK: Objective-C retain cycles ----------------------------------// 4842f85e193739c953358c865005855253af4f68a497John McCall 4843f85e193739c953358c865005855253af4f68a497John McCallnamespace { 4844f85e193739c953358c865005855253af4f68a497John McCall struct RetainCycleOwner { 4845f85e193739c953358c865005855253af4f68a497John McCall RetainCycleOwner() : Variable(0), Indirect(false) {} 4846f85e193739c953358c865005855253af4f68a497John McCall VarDecl *Variable; 4847f85e193739c953358c865005855253af4f68a497John McCall SourceRange Range; 4848f85e193739c953358c865005855253af4f68a497John McCall SourceLocation Loc; 4849f85e193739c953358c865005855253af4f68a497John McCall bool Indirect; 4850f85e193739c953358c865005855253af4f68a497John McCall 4851f85e193739c953358c865005855253af4f68a497John McCall void setLocsFrom(Expr *e) { 4852f85e193739c953358c865005855253af4f68a497John McCall Loc = e->getExprLoc(); 4853f85e193739c953358c865005855253af4f68a497John McCall Range = e->getSourceRange(); 4854f85e193739c953358c865005855253af4f68a497John McCall } 4855f85e193739c953358c865005855253af4f68a497John McCall }; 4856f85e193739c953358c865005855253af4f68a497John McCall} 4857f85e193739c953358c865005855253af4f68a497John McCall 4858f85e193739c953358c865005855253af4f68a497John McCall/// Consider whether capturing the given variable can possibly lead to 4859f85e193739c953358c865005855253af4f68a497John McCall/// a retain cycle. 4860f85e193739c953358c865005855253af4f68a497John McCallstatic bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { 4861f85e193739c953358c865005855253af4f68a497John McCall // In ARC, it's captured strongly iff the variable has __strong 4862f85e193739c953358c865005855253af4f68a497John McCall // lifetime. In MRR, it's captured strongly if the variable is 4863f85e193739c953358c865005855253af4f68a497John McCall // __block and has an appropriate type. 4864f85e193739c953358c865005855253af4f68a497John McCall if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 4865f85e193739c953358c865005855253af4f68a497John McCall return false; 4866f85e193739c953358c865005855253af4f68a497John McCall 4867f85e193739c953358c865005855253af4f68a497John McCall owner.Variable = var; 4868f85e193739c953358c865005855253af4f68a497John McCall owner.setLocsFrom(ref); 4869f85e193739c953358c865005855253af4f68a497John McCall return true; 4870f85e193739c953358c865005855253af4f68a497John McCall} 4871f85e193739c953358c865005855253af4f68a497John McCall 48726e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanianstatic bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { 4873f85e193739c953358c865005855253af4f68a497John McCall while (true) { 4874f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 4875f85e193739c953358c865005855253af4f68a497John McCall if (CastExpr *cast = dyn_cast<CastExpr>(e)) { 4876f85e193739c953358c865005855253af4f68a497John McCall switch (cast->getCastKind()) { 4877f85e193739c953358c865005855253af4f68a497John McCall case CK_BitCast: 4878f85e193739c953358c865005855253af4f68a497John McCall case CK_LValueBitCast: 4879f85e193739c953358c865005855253af4f68a497John McCall case CK_LValueToRValue: 488033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 4881f85e193739c953358c865005855253af4f68a497John McCall e = cast->getSubExpr(); 4882f85e193739c953358c865005855253af4f68a497John McCall continue; 4883f85e193739c953358c865005855253af4f68a497John McCall 4884f85e193739c953358c865005855253af4f68a497John McCall default: 4885f85e193739c953358c865005855253af4f68a497John McCall return false; 4886f85e193739c953358c865005855253af4f68a497John McCall } 4887f85e193739c953358c865005855253af4f68a497John McCall } 4888f85e193739c953358c865005855253af4f68a497John McCall 4889f85e193739c953358c865005855253af4f68a497John McCall if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { 4890f85e193739c953358c865005855253af4f68a497John McCall ObjCIvarDecl *ivar = ref->getDecl(); 4891f85e193739c953358c865005855253af4f68a497John McCall if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 4892f85e193739c953358c865005855253af4f68a497John McCall return false; 4893f85e193739c953358c865005855253af4f68a497John McCall 4894f85e193739c953358c865005855253af4f68a497John McCall // Try to find a retain cycle in the base. 48956e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian if (!findRetainCycleOwner(S, ref->getBase(), owner)) 4896f85e193739c953358c865005855253af4f68a497John McCall return false; 4897f85e193739c953358c865005855253af4f68a497John McCall 4898f85e193739c953358c865005855253af4f68a497John McCall if (ref->isFreeIvar()) owner.setLocsFrom(ref); 4899f85e193739c953358c865005855253af4f68a497John McCall owner.Indirect = true; 4900f85e193739c953358c865005855253af4f68a497John McCall return true; 4901f85e193739c953358c865005855253af4f68a497John McCall } 4902f85e193739c953358c865005855253af4f68a497John McCall 4903f85e193739c953358c865005855253af4f68a497John McCall if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { 4904f85e193739c953358c865005855253af4f68a497John McCall VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); 4905f85e193739c953358c865005855253af4f68a497John McCall if (!var) return false; 4906f85e193739c953358c865005855253af4f68a497John McCall return considerVariable(var, ref, owner); 4907f85e193739c953358c865005855253af4f68a497John McCall } 4908f85e193739c953358c865005855253af4f68a497John McCall 4909f85e193739c953358c865005855253af4f68a497John McCall if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { 4910f85e193739c953358c865005855253af4f68a497John McCall if (member->isArrow()) return false; 4911f85e193739c953358c865005855253af4f68a497John McCall 4912f85e193739c953358c865005855253af4f68a497John McCall // Don't count this as an indirect ownership. 4913f85e193739c953358c865005855253af4f68a497John McCall e = member->getBase(); 4914f85e193739c953358c865005855253af4f68a497John McCall continue; 4915f85e193739c953358c865005855253af4f68a497John McCall } 4916f85e193739c953358c865005855253af4f68a497John McCall 49174b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 49184b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Only pay attention to pseudo-objects on property references. 49194b9c2d235fb9449e249d74f48ecfec601650de93John McCall ObjCPropertyRefExpr *pre 49204b9c2d235fb9449e249d74f48ecfec601650de93John McCall = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() 49214b9c2d235fb9449e249d74f48ecfec601650de93John McCall ->IgnoreParens()); 49224b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (!pre) return false; 49234b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (pre->isImplicitProperty()) return false; 49244b9c2d235fb9449e249d74f48ecfec601650de93John McCall ObjCPropertyDecl *property = pre->getExplicitProperty(); 49254b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (!property->isRetaining() && 49264b9c2d235fb9449e249d74f48ecfec601650de93John McCall !(property->getPropertyIvarDecl() && 49274b9c2d235fb9449e249d74f48ecfec601650de93John McCall property->getPropertyIvarDecl()->getType() 49284b9c2d235fb9449e249d74f48ecfec601650de93John McCall .getObjCLifetime() == Qualifiers::OCL_Strong)) 49294b9c2d235fb9449e249d74f48ecfec601650de93John McCall return false; 49304b9c2d235fb9449e249d74f48ecfec601650de93John McCall 49314b9c2d235fb9449e249d74f48ecfec601650de93John McCall owner.Indirect = true; 49326e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian if (pre->isSuperReceiver()) { 49336e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian owner.Variable = S.getCurMethodDecl()->getSelfDecl(); 49346e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian if (!owner.Variable) 49356e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian return false; 49366e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian owner.Loc = pre->getLocation(); 49376e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian owner.Range = pre->getSourceRange(); 49386e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian return true; 49396e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian } 49404b9c2d235fb9449e249d74f48ecfec601650de93John McCall e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) 49414b9c2d235fb9449e249d74f48ecfec601650de93John McCall ->getSourceExpr()); 49424b9c2d235fb9449e249d74f48ecfec601650de93John McCall continue; 49434b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 49444b9c2d235fb9449e249d74f48ecfec601650de93John McCall 4945f85e193739c953358c865005855253af4f68a497John McCall // Array ivars? 4946f85e193739c953358c865005855253af4f68a497John McCall 4947f85e193739c953358c865005855253af4f68a497John McCall return false; 4948f85e193739c953358c865005855253af4f68a497John McCall } 4949f85e193739c953358c865005855253af4f68a497John McCall} 4950f85e193739c953358c865005855253af4f68a497John McCall 4951f85e193739c953358c865005855253af4f68a497John McCallnamespace { 4952f85e193739c953358c865005855253af4f68a497John McCall struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { 4953f85e193739c953358c865005855253af4f68a497John McCall FindCaptureVisitor(ASTContext &Context, VarDecl *variable) 4954f85e193739c953358c865005855253af4f68a497John McCall : EvaluatedExprVisitor<FindCaptureVisitor>(Context), 4955f85e193739c953358c865005855253af4f68a497John McCall Variable(variable), Capturer(0) {} 4956f85e193739c953358c865005855253af4f68a497John McCall 4957f85e193739c953358c865005855253af4f68a497John McCall VarDecl *Variable; 4958f85e193739c953358c865005855253af4f68a497John McCall Expr *Capturer; 4959f85e193739c953358c865005855253af4f68a497John McCall 4960f85e193739c953358c865005855253af4f68a497John McCall void VisitDeclRefExpr(DeclRefExpr *ref) { 4961f85e193739c953358c865005855253af4f68a497John McCall if (ref->getDecl() == Variable && !Capturer) 4962f85e193739c953358c865005855253af4f68a497John McCall Capturer = ref; 4963f85e193739c953358c865005855253af4f68a497John McCall } 4964f85e193739c953358c865005855253af4f68a497John McCall 4965f85e193739c953358c865005855253af4f68a497John McCall void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { 4966f85e193739c953358c865005855253af4f68a497John McCall if (Capturer) return; 4967f85e193739c953358c865005855253af4f68a497John McCall Visit(ref->getBase()); 4968f85e193739c953358c865005855253af4f68a497John McCall if (Capturer && ref->isFreeIvar()) 4969f85e193739c953358c865005855253af4f68a497John McCall Capturer = ref; 4970f85e193739c953358c865005855253af4f68a497John McCall } 4971f85e193739c953358c865005855253af4f68a497John McCall 4972f85e193739c953358c865005855253af4f68a497John McCall void VisitBlockExpr(BlockExpr *block) { 4973f85e193739c953358c865005855253af4f68a497John McCall // Look inside nested blocks 4974f85e193739c953358c865005855253af4f68a497John McCall if (block->getBlockDecl()->capturesVariable(Variable)) 4975f85e193739c953358c865005855253af4f68a497John McCall Visit(block->getBlockDecl()->getBody()); 4976f85e193739c953358c865005855253af4f68a497John McCall } 4977f85e193739c953358c865005855253af4f68a497John McCall }; 4978f85e193739c953358c865005855253af4f68a497John McCall} 4979f85e193739c953358c865005855253af4f68a497John McCall 4980f85e193739c953358c865005855253af4f68a497John McCall/// Check whether the given argument is a block which captures a 4981f85e193739c953358c865005855253af4f68a497John McCall/// variable. 4982f85e193739c953358c865005855253af4f68a497John McCallstatic Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { 4983f85e193739c953358c865005855253af4f68a497John McCall assert(owner.Variable && owner.Loc.isValid()); 4984f85e193739c953358c865005855253af4f68a497John McCall 4985f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParenCasts(); 4986f85e193739c953358c865005855253af4f68a497John McCall BlockExpr *block = dyn_cast<BlockExpr>(e); 4987f85e193739c953358c865005855253af4f68a497John McCall if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) 4988f85e193739c953358c865005855253af4f68a497John McCall return 0; 4989f85e193739c953358c865005855253af4f68a497John McCall 4990f85e193739c953358c865005855253af4f68a497John McCall FindCaptureVisitor visitor(S.Context, owner.Variable); 4991f85e193739c953358c865005855253af4f68a497John McCall visitor.Visit(block->getBlockDecl()->getBody()); 4992f85e193739c953358c865005855253af4f68a497John McCall return visitor.Capturer; 4993f85e193739c953358c865005855253af4f68a497John McCall} 4994f85e193739c953358c865005855253af4f68a497John McCall 4995f85e193739c953358c865005855253af4f68a497John McCallstatic void diagnoseRetainCycle(Sema &S, Expr *capturer, 4996f85e193739c953358c865005855253af4f68a497John McCall RetainCycleOwner &owner) { 4997f85e193739c953358c865005855253af4f68a497John McCall assert(capturer); 4998f85e193739c953358c865005855253af4f68a497John McCall assert(owner.Variable && owner.Loc.isValid()); 4999f85e193739c953358c865005855253af4f68a497John McCall 5000f85e193739c953358c865005855253af4f68a497John McCall S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) 5001f85e193739c953358c865005855253af4f68a497John McCall << owner.Variable << capturer->getSourceRange(); 5002f85e193739c953358c865005855253af4f68a497John McCall S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) 5003f85e193739c953358c865005855253af4f68a497John McCall << owner.Indirect << owner.Range; 5004f85e193739c953358c865005855253af4f68a497John McCall} 5005f85e193739c953358c865005855253af4f68a497John McCall 5006f85e193739c953358c865005855253af4f68a497John McCall/// Check for a keyword selector that starts with the word 'add' or 5007f85e193739c953358c865005855253af4f68a497John McCall/// 'set'. 5008f85e193739c953358c865005855253af4f68a497John McCallstatic bool isSetterLikeSelector(Selector sel) { 5009f85e193739c953358c865005855253af4f68a497John McCall if (sel.isUnarySelector()) return false; 5010f85e193739c953358c865005855253af4f68a497John McCall 50115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef str = sel.getNameForSlot(0); 5012f85e193739c953358c865005855253af4f68a497John McCall while (!str.empty() && str.front() == '_') str = str.substr(1); 5013968a0ee9d98549308e3e70e787e4fd669d2a829dTed Kremenek if (str.startswith("set")) 5014f85e193739c953358c865005855253af4f68a497John McCall str = str.substr(3); 5015968a0ee9d98549308e3e70e787e4fd669d2a829dTed Kremenek else if (str.startswith("add")) { 5016968a0ee9d98549308e3e70e787e4fd669d2a829dTed Kremenek // Specially whitelist 'addOperationWithBlock:'. 5017968a0ee9d98549308e3e70e787e4fd669d2a829dTed Kremenek if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) 5018968a0ee9d98549308e3e70e787e4fd669d2a829dTed Kremenek return false; 5019968a0ee9d98549308e3e70e787e4fd669d2a829dTed Kremenek str = str.substr(3); 5020968a0ee9d98549308e3e70e787e4fd669d2a829dTed Kremenek } 5021f85e193739c953358c865005855253af4f68a497John McCall else 5022f85e193739c953358c865005855253af4f68a497John McCall return false; 5023f85e193739c953358c865005855253af4f68a497John McCall 5024f85e193739c953358c865005855253af4f68a497John McCall if (str.empty()) return true; 5025f85e193739c953358c865005855253af4f68a497John McCall return !islower(str.front()); 5026f85e193739c953358c865005855253af4f68a497John McCall} 5027f85e193739c953358c865005855253af4f68a497John McCall 5028f85e193739c953358c865005855253af4f68a497John McCall/// Check a message send to see if it's likely to cause a retain cycle. 5029f85e193739c953358c865005855253af4f68a497John McCallvoid Sema::checkRetainCycles(ObjCMessageExpr *msg) { 5030f85e193739c953358c865005855253af4f68a497John McCall // Only check instance methods whose selector looks like a setter. 5031f85e193739c953358c865005855253af4f68a497John McCall if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) 5032f85e193739c953358c865005855253af4f68a497John McCall return; 5033f85e193739c953358c865005855253af4f68a497John McCall 5034f85e193739c953358c865005855253af4f68a497John McCall // Try to find a variable that the receiver is strongly owned by. 5035f85e193739c953358c865005855253af4f68a497John McCall RetainCycleOwner owner; 5036f85e193739c953358c865005855253af4f68a497John McCall if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { 50376e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) 5038f85e193739c953358c865005855253af4f68a497John McCall return; 5039f85e193739c953358c865005855253af4f68a497John McCall } else { 5040f85e193739c953358c865005855253af4f68a497John McCall assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); 5041f85e193739c953358c865005855253af4f68a497John McCall owner.Variable = getCurMethodDecl()->getSelfDecl(); 5042f85e193739c953358c865005855253af4f68a497John McCall owner.Loc = msg->getSuperLoc(); 5043f85e193739c953358c865005855253af4f68a497John McCall owner.Range = msg->getSuperLoc(); 5044f85e193739c953358c865005855253af4f68a497John McCall } 5045f85e193739c953358c865005855253af4f68a497John McCall 5046f85e193739c953358c865005855253af4f68a497John McCall // Check whether the receiver is captured by any of the arguments. 5047f85e193739c953358c865005855253af4f68a497John McCall for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) 5048f85e193739c953358c865005855253af4f68a497John McCall if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) 5049f85e193739c953358c865005855253af4f68a497John McCall return diagnoseRetainCycle(*this, capturer, owner); 5050f85e193739c953358c865005855253af4f68a497John McCall} 5051f85e193739c953358c865005855253af4f68a497John McCall 5052f85e193739c953358c865005855253af4f68a497John McCall/// Check a property assign to see if it's likely to cause a retain cycle. 5053f85e193739c953358c865005855253af4f68a497John McCallvoid Sema::checkRetainCycles(Expr *receiver, Expr *argument) { 5054f85e193739c953358c865005855253af4f68a497John McCall RetainCycleOwner owner; 50556e6f93a1f16d9804db79390382e9d1f6322cdbfdFariborz Jahanian if (!findRetainCycleOwner(*this, receiver, owner)) 5056f85e193739c953358c865005855253af4f68a497John McCall return; 5057f85e193739c953358c865005855253af4f68a497John McCall 5058f85e193739c953358c865005855253af4f68a497John McCall if (Expr *capturer = findCapturingExpr(*this, argument, owner)) 5059f85e193739c953358c865005855253af4f68a497John McCall diagnoseRetainCycle(*this, capturer, owner); 5060f85e193739c953358c865005855253af4f68a497John McCall} 5061f85e193739c953358c865005855253af4f68a497John McCall 5062921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanianbool Sema::checkUnsafeAssigns(SourceLocation Loc, 5063f85e193739c953358c865005855253af4f68a497John McCall QualType LHS, Expr *RHS) { 5064f85e193739c953358c865005855253af4f68a497John McCall Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); 5065f85e193739c953358c865005855253af4f68a497John McCall if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) 5066921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian return false; 5067921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian // strip off any implicit cast added to get to the one arc-specific 5068921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 506933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall if (cast->getCastKind() == CK_ARCConsumeObject) { 5070f85e193739c953358c865005855253af4f68a497John McCall Diag(Loc, diag::warn_arc_retained_assign) 5071f85e193739c953358c865005855253af4f68a497John McCall << (LT == Qualifiers::OCL_ExplicitNone) 5072f85e193739c953358c865005855253af4f68a497John McCall << RHS->getSourceRange(); 5073921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian return true; 5074921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian } 5075921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian RHS = cast->getSubExpr(); 5076921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian } 5077921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian return false; 5078f85e193739c953358c865005855253af4f68a497John McCall} 5079f85e193739c953358c865005855253af4f68a497John McCall 5080921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanianvoid Sema::checkUnsafeExprAssigns(SourceLocation Loc, 5081921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian Expr *LHS, Expr *RHS) { 508287eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian QualType LHSType; 508387eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian // PropertyRef on LHS type need be directly obtained from 508487eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian // its declaration as it has a PsuedoType. 508587eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian ObjCPropertyRefExpr *PRE 508687eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); 508787eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian if (PRE && !PRE->isImplicitProperty()) { 508887eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 508987eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian if (PD) 509087eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian LHSType = PD->getType(); 509187eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian } 509287eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian 509387eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian if (LHSType.isNull()) 509487eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian LHSType = LHS->getType(); 5095921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian if (checkUnsafeAssigns(Loc, LHSType, RHS)) 5096921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian return; 5097921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); 5098921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian // FIXME. Check for other life times. 5099921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian if (LT != Qualifiers::OCL_None) 5100921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian return; 5101921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian 510287eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian if (PRE) { 5103921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian if (PRE->isImplicitProperty()) 5104921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian return; 5105921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 5106921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian if (!PD) 5107921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian return; 5108921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian 5109921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian unsigned Attributes = PD->getPropertyAttributes(); 511087eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { 511187eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian // when 'assign' attribute was not explicitly specified 511287eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian // by user, ignore it and rely on property type itself 511387eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian // for lifetime info. 511487eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); 511587eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && 511687eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian LHSType->isObjCRetainableType()) 511787eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian return; 511887eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian 5119921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 512033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall if (cast->getCastKind() == CK_ARCConsumeObject) { 5121921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian Diag(Loc, diag::warn_arc_retained_property_assign) 5122921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian << RHS->getSourceRange(); 5123921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian return; 5124921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian } 5125921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian RHS = cast->getSubExpr(); 5126921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian } 512787eaf726528124a1ad49f38402e9b619685fe30bFariborz Jahanian } 5128921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian } 5129921c14322977253a60dcb171f45e468c95fe41f8Fariborz Jahanian} 5130625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5131625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko//===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// 5132625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5133625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkonamespace { 5134625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkobool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, 5135625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko SourceLocation StmtLoc, 5136625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko const NullStmt *Body) { 5137625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Do not warn if the body is a macro that expands to nothing, e.g: 5138625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // 5139625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // #define CALL(x) 5140625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // if (condition) 5141625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // CALL(0); 5142625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // 5143625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (Body->hasLeadingEmptyMacro()) 5144625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return false; 5145625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5146625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Get line numbers of statement and body. 5147625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko bool StmtLineInvalid; 5148625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc, 5149625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko &StmtLineInvalid); 5150625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (StmtLineInvalid) 5151625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return false; 5152625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5153625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko bool BodyLineInvalid; 5154625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), 5155625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko &BodyLineInvalid); 5156625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (BodyLineInvalid) 5157625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return false; 5158625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5159625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Warn if null statement and body are on the same line. 5160625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (StmtLine != BodyLine) 5161625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return false; 5162625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5163625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return true; 5164625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 5165625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} // Unnamed namespace 5166625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5167625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, 5168625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko const Stmt *Body, 5169625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko unsigned DiagID) { 5170625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Since this is a syntactic check, don't emit diagnostic for template 5171625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // instantiations, this just adds noise. 5172625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (CurrentInstantiationScope) 5173625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; 5174625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5175625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // The body should be a null statement. 5176625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko const NullStmt *NBody = dyn_cast<NullStmt>(Body); 5177625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (!NBody) 5178625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; 5179625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5180625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Do the usual checks. 5181625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 5182625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; 5183625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5184625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko Diag(NBody->getSemiLoc(), DiagID); 5185625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 5186625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 5187625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5188625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkovoid Sema::DiagnoseEmptyLoopBody(const Stmt *S, 5189625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko const Stmt *PossibleBody) { 5190625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko assert(!CurrentInstantiationScope); // Ensured by caller 5191625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5192625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko SourceLocation StmtLoc; 5193625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko const Stmt *Body; 5194625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko unsigned DiagID; 5195625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { 5196625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko StmtLoc = FS->getRParenLoc(); 5197625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko Body = FS->getBody(); 5198625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagID = diag::warn_empty_for_body; 5199625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { 5200625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko StmtLoc = WS->getCond()->getSourceRange().getEnd(); 5201625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko Body = WS->getBody(); 5202625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagID = diag::warn_empty_while_body; 5203625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko } else 5204625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; // Neither `for' nor `while'. 5205625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5206625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // The body should be a null statement. 5207625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko const NullStmt *NBody = dyn_cast<NullStmt>(Body); 5208625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (!NBody) 5209625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; 5210625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5211625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Skip expensive checks if diagnostic is disabled. 5212625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) == 5213625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko DiagnosticsEngine::Ignored) 5214625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; 5215625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5216625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // Do the usual checks. 5217625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 5218625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; 5219625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5220625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // `for(...);' and `while(...);' are popular idioms, so in order to keep 5221625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // noise level low, emit diagnostics only if for/while is followed by a 5222625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // CompoundStmt, e.g.: 5223625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // for (int i = 0; i < n; i++); 5224625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // { 5225625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // a(i); 5226625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // } 5227625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // or if for/while is followed by a statement with more indentation 5228625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // than for/while itself: 5229625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // for (int i = 0; i < n; i++); 5230625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko // a(i); 5231625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko bool ProbableTypo = isa<CompoundStmt>(PossibleBody); 5232625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (!ProbableTypo) { 5233625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko bool BodyColInvalid; 5234625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko unsigned BodyCol = SourceMgr.getPresumedColumnNumber( 5235625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko PossibleBody->getLocStart(), 5236625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko &BodyColInvalid); 5237625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (BodyColInvalid) 5238625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; 5239625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5240625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko bool StmtColInvalid; 5241625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko unsigned StmtCol = SourceMgr.getPresumedColumnNumber( 5242625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko S->getLocStart(), 5243625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko &StmtColInvalid); 5244625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (StmtColInvalid) 5245625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko return; 5246625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5247625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (BodyCol > StmtCol) 5248625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko ProbableTypo = true; 5249625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko } 5250625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko 5251625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko if (ProbableTypo) { 5252625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko Diag(NBody->getSemiLoc(), DiagID); 5253625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 5254625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko } 5255625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko} 5256