1 //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements extra semantic analysis beyond what is enforced 11 // by the C type system. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Sema/SemaInternal.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/CharUnits.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/DeclObjC.h" 20 #include "clang/AST/EvaluatedExprVisitor.h" 21 #include "clang/AST/Expr.h" 22 #include "clang/AST/ExprCXX.h" 23 #include "clang/AST/ExprObjC.h" 24 #include "clang/AST/StmtCXX.h" 25 #include "clang/AST/StmtObjC.h" 26 #include "clang/Analysis/Analyses/FormatString.h" 27 #include "clang/Basic/CharInfo.h" 28 #include "clang/Basic/TargetBuiltins.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering. 31 #include "clang/Sema/Initialization.h" 32 #include "clang/Sema/Lookup.h" 33 #include "clang/Sema/ScopeInfo.h" 34 #include "clang/Sema/Sema.h" 35 #include "llvm/ADT/STLExtras.h" 36 #include "llvm/ADT/SmallBitVector.h" 37 #include "llvm/ADT/SmallString.h" 38 #include "llvm/Support/ConvertUTF.h" 39 #include "llvm/Support/raw_ostream.h" 40 #include <limits> 41 using namespace clang; 42 using namespace sema; 43 44 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, 45 unsigned ByteNo) const { 46 return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts, 47 Context.getTargetInfo()); 48 } 49 50 /// Checks that a call expression's argument count is the desired number. 51 /// This is useful when doing custom type-checking. Returns true on error. 52 static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { 53 unsigned argCount = call->getNumArgs(); 54 if (argCount == desiredArgCount) return false; 55 56 if (argCount < desiredArgCount) 57 return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) 58 << 0 /*function call*/ << desiredArgCount << argCount 59 << call->getSourceRange(); 60 61 // Highlight all the excess arguments. 62 SourceRange range(call->getArg(desiredArgCount)->getLocStart(), 63 call->getArg(argCount - 1)->getLocEnd()); 64 65 return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) 66 << 0 /*function call*/ << desiredArgCount << argCount 67 << call->getArg(1)->getSourceRange(); 68 } 69 70 /// Check that the first argument to __builtin_annotation is an integer 71 /// and the second argument is a non-wide string literal. 72 static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { 73 if (checkArgCount(S, TheCall, 2)) 74 return true; 75 76 // First argument should be an integer. 77 Expr *ValArg = TheCall->getArg(0); 78 QualType Ty = ValArg->getType(); 79 if (!Ty->isIntegerType()) { 80 S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) 81 << ValArg->getSourceRange(); 82 return true; 83 } 84 85 // Second argument should be a constant string. 86 Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); 87 StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); 88 if (!Literal || !Literal->isAscii()) { 89 S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) 90 << StrArg->getSourceRange(); 91 return true; 92 } 93 94 TheCall->setType(Ty); 95 return false; 96 } 97 98 /// Check that the argument to __builtin_addressof is a glvalue, and set the 99 /// result type to the corresponding pointer type. 100 static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) { 101 if (checkArgCount(S, TheCall, 1)) 102 return true; 103 104 ExprResult Arg(TheCall->getArg(0)); 105 QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getLocStart()); 106 if (ResultType.isNull()) 107 return true; 108 109 TheCall->setArg(0, Arg.get()); 110 TheCall->setType(ResultType); 111 return false; 112 } 113 114 static void SemaBuiltinMemChkCall(Sema &S, FunctionDecl *FDecl, 115 CallExpr *TheCall, unsigned SizeIdx, 116 unsigned DstSizeIdx) { 117 if (TheCall->getNumArgs() <= SizeIdx || 118 TheCall->getNumArgs() <= DstSizeIdx) 119 return; 120 121 const Expr *SizeArg = TheCall->getArg(SizeIdx); 122 const Expr *DstSizeArg = TheCall->getArg(DstSizeIdx); 123 124 llvm::APSInt Size, DstSize; 125 126 // find out if both sizes are known at compile time 127 if (!SizeArg->EvaluateAsInt(Size, S.Context) || 128 !DstSizeArg->EvaluateAsInt(DstSize, S.Context)) 129 return; 130 131 if (Size.ule(DstSize)) 132 return; 133 134 // confirmed overflow so generate the diagnostic. 135 IdentifierInfo *FnName = FDecl->getIdentifier(); 136 SourceLocation SL = TheCall->getLocStart(); 137 SourceRange SR = TheCall->getSourceRange(); 138 139 S.Diag(SL, diag::warn_memcpy_chk_overflow) << SR << FnName; 140 } 141 142 static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) { 143 if (checkArgCount(S, BuiltinCall, 2)) 144 return true; 145 146 SourceLocation BuiltinLoc = BuiltinCall->getLocStart(); 147 Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts(); 148 Expr *Call = BuiltinCall->getArg(0); 149 Expr *Chain = BuiltinCall->getArg(1); 150 151 if (Call->getStmtClass() != Stmt::CallExprClass) { 152 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call) 153 << Call->getSourceRange(); 154 return true; 155 } 156 157 auto CE = cast<CallExpr>(Call); 158 if (CE->getCallee()->getType()->isBlockPointerType()) { 159 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call) 160 << Call->getSourceRange(); 161 return true; 162 } 163 164 const Decl *TargetDecl = CE->getCalleeDecl(); 165 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) 166 if (FD->getBuiltinID()) { 167 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call) 168 << Call->getSourceRange(); 169 return true; 170 } 171 172 if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) { 173 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call) 174 << Call->getSourceRange(); 175 return true; 176 } 177 178 ExprResult ChainResult = S.UsualUnaryConversions(Chain); 179 if (ChainResult.isInvalid()) 180 return true; 181 if (!ChainResult.get()->getType()->isPointerType()) { 182 S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer) 183 << Chain->getSourceRange(); 184 return true; 185 } 186 187 QualType ReturnTy = CE->getCallReturnType(); 188 QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() }; 189 QualType BuiltinTy = S.Context.getFunctionType( 190 ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo()); 191 QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy); 192 193 Builtin = 194 S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get(); 195 196 BuiltinCall->setType(CE->getType()); 197 BuiltinCall->setValueKind(CE->getValueKind()); 198 BuiltinCall->setObjectKind(CE->getObjectKind()); 199 BuiltinCall->setCallee(Builtin); 200 BuiltinCall->setArg(1, ChainResult.get()); 201 202 return false; 203 } 204 205 ExprResult 206 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, 207 CallExpr *TheCall) { 208 ExprResult TheCallResult(TheCall); 209 210 // Find out if any arguments are required to be integer constant expressions. 211 unsigned ICEArguments = 0; 212 ASTContext::GetBuiltinTypeError Error; 213 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); 214 if (Error != ASTContext::GE_None) 215 ICEArguments = 0; // Don't diagnose previously diagnosed errors. 216 217 // If any arguments are required to be ICE's, check and diagnose. 218 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { 219 // Skip arguments not required to be ICE's. 220 if ((ICEArguments & (1 << ArgNo)) == 0) continue; 221 222 llvm::APSInt Result; 223 if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) 224 return true; 225 ICEArguments &= ~(1 << ArgNo); 226 } 227 228 switch (BuiltinID) { 229 case Builtin::BI__builtin___CFStringMakeConstantString: 230 assert(TheCall->getNumArgs() == 1 && 231 "Wrong # arguments to builtin CFStringMakeConstantString"); 232 if (CheckObjCString(TheCall->getArg(0))) 233 return ExprError(); 234 break; 235 case Builtin::BI__builtin_stdarg_start: 236 case Builtin::BI__builtin_va_start: 237 if (SemaBuiltinVAStart(TheCall)) 238 return ExprError(); 239 break; 240 case Builtin::BI__va_start: { 241 switch (Context.getTargetInfo().getTriple().getArch()) { 242 case llvm::Triple::arm: 243 case llvm::Triple::thumb: 244 if (SemaBuiltinVAStartARM(TheCall)) 245 return ExprError(); 246 break; 247 default: 248 if (SemaBuiltinVAStart(TheCall)) 249 return ExprError(); 250 break; 251 } 252 break; 253 } 254 case Builtin::BI__builtin_isgreater: 255 case Builtin::BI__builtin_isgreaterequal: 256 case Builtin::BI__builtin_isless: 257 case Builtin::BI__builtin_islessequal: 258 case Builtin::BI__builtin_islessgreater: 259 case Builtin::BI__builtin_isunordered: 260 if (SemaBuiltinUnorderedCompare(TheCall)) 261 return ExprError(); 262 break; 263 case Builtin::BI__builtin_fpclassify: 264 if (SemaBuiltinFPClassification(TheCall, 6)) 265 return ExprError(); 266 break; 267 case Builtin::BI__builtin_isfinite: 268 case Builtin::BI__builtin_isinf: 269 case Builtin::BI__builtin_isinf_sign: 270 case Builtin::BI__builtin_isnan: 271 case Builtin::BI__builtin_isnormal: 272 if (SemaBuiltinFPClassification(TheCall, 1)) 273 return ExprError(); 274 break; 275 case Builtin::BI__builtin_shufflevector: 276 return SemaBuiltinShuffleVector(TheCall); 277 // TheCall will be freed by the smart pointer here, but that's fine, since 278 // SemaBuiltinShuffleVector guts it, but then doesn't release it. 279 case Builtin::BI__builtin_prefetch: 280 if (SemaBuiltinPrefetch(TheCall)) 281 return ExprError(); 282 break; 283 case Builtin::BI__assume: 284 case Builtin::BI__builtin_assume: 285 if (SemaBuiltinAssume(TheCall)) 286 return ExprError(); 287 break; 288 case Builtin::BI__builtin_assume_aligned: 289 if (SemaBuiltinAssumeAligned(TheCall)) 290 return ExprError(); 291 break; 292 case Builtin::BI__builtin_object_size: 293 if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3)) 294 return ExprError(); 295 break; 296 case Builtin::BI__builtin_longjmp: 297 if (SemaBuiltinLongjmp(TheCall)) 298 return ExprError(); 299 break; 300 case Builtin::BI__builtin_setjmp: 301 if (SemaBuiltinSetjmp(TheCall)) 302 return ExprError(); 303 break; 304 305 case Builtin::BI__builtin_classify_type: 306 if (checkArgCount(*this, TheCall, 1)) return true; 307 TheCall->setType(Context.IntTy); 308 break; 309 case Builtin::BI__builtin_constant_p: 310 if (checkArgCount(*this, TheCall, 1)) return true; 311 TheCall->setType(Context.IntTy); 312 break; 313 case Builtin::BI__sync_fetch_and_add: 314 case Builtin::BI__sync_fetch_and_add_1: 315 case Builtin::BI__sync_fetch_and_add_2: 316 case Builtin::BI__sync_fetch_and_add_4: 317 case Builtin::BI__sync_fetch_and_add_8: 318 case Builtin::BI__sync_fetch_and_add_16: 319 case Builtin::BI__sync_fetch_and_sub: 320 case Builtin::BI__sync_fetch_and_sub_1: 321 case Builtin::BI__sync_fetch_and_sub_2: 322 case Builtin::BI__sync_fetch_and_sub_4: 323 case Builtin::BI__sync_fetch_and_sub_8: 324 case Builtin::BI__sync_fetch_and_sub_16: 325 case Builtin::BI__sync_fetch_and_or: 326 case Builtin::BI__sync_fetch_and_or_1: 327 case Builtin::BI__sync_fetch_and_or_2: 328 case Builtin::BI__sync_fetch_and_or_4: 329 case Builtin::BI__sync_fetch_and_or_8: 330 case Builtin::BI__sync_fetch_and_or_16: 331 case Builtin::BI__sync_fetch_and_and: 332 case Builtin::BI__sync_fetch_and_and_1: 333 case Builtin::BI__sync_fetch_and_and_2: 334 case Builtin::BI__sync_fetch_and_and_4: 335 case Builtin::BI__sync_fetch_and_and_8: 336 case Builtin::BI__sync_fetch_and_and_16: 337 case Builtin::BI__sync_fetch_and_xor: 338 case Builtin::BI__sync_fetch_and_xor_1: 339 case Builtin::BI__sync_fetch_and_xor_2: 340 case Builtin::BI__sync_fetch_and_xor_4: 341 case Builtin::BI__sync_fetch_and_xor_8: 342 case Builtin::BI__sync_fetch_and_xor_16: 343 case Builtin::BI__sync_fetch_and_nand: 344 case Builtin::BI__sync_fetch_and_nand_1: 345 case Builtin::BI__sync_fetch_and_nand_2: 346 case Builtin::BI__sync_fetch_and_nand_4: 347 case Builtin::BI__sync_fetch_and_nand_8: 348 case Builtin::BI__sync_fetch_and_nand_16: 349 case Builtin::BI__sync_add_and_fetch: 350 case Builtin::BI__sync_add_and_fetch_1: 351 case Builtin::BI__sync_add_and_fetch_2: 352 case Builtin::BI__sync_add_and_fetch_4: 353 case Builtin::BI__sync_add_and_fetch_8: 354 case Builtin::BI__sync_add_and_fetch_16: 355 case Builtin::BI__sync_sub_and_fetch: 356 case Builtin::BI__sync_sub_and_fetch_1: 357 case Builtin::BI__sync_sub_and_fetch_2: 358 case Builtin::BI__sync_sub_and_fetch_4: 359 case Builtin::BI__sync_sub_and_fetch_8: 360 case Builtin::BI__sync_sub_and_fetch_16: 361 case Builtin::BI__sync_and_and_fetch: 362 case Builtin::BI__sync_and_and_fetch_1: 363 case Builtin::BI__sync_and_and_fetch_2: 364 case Builtin::BI__sync_and_and_fetch_4: 365 case Builtin::BI__sync_and_and_fetch_8: 366 case Builtin::BI__sync_and_and_fetch_16: 367 case Builtin::BI__sync_or_and_fetch: 368 case Builtin::BI__sync_or_and_fetch_1: 369 case Builtin::BI__sync_or_and_fetch_2: 370 case Builtin::BI__sync_or_and_fetch_4: 371 case Builtin::BI__sync_or_and_fetch_8: 372 case Builtin::BI__sync_or_and_fetch_16: 373 case Builtin::BI__sync_xor_and_fetch: 374 case Builtin::BI__sync_xor_and_fetch_1: 375 case Builtin::BI__sync_xor_and_fetch_2: 376 case Builtin::BI__sync_xor_and_fetch_4: 377 case Builtin::BI__sync_xor_and_fetch_8: 378 case Builtin::BI__sync_xor_and_fetch_16: 379 case Builtin::BI__sync_nand_and_fetch: 380 case Builtin::BI__sync_nand_and_fetch_1: 381 case Builtin::BI__sync_nand_and_fetch_2: 382 case Builtin::BI__sync_nand_and_fetch_4: 383 case Builtin::BI__sync_nand_and_fetch_8: 384 case Builtin::BI__sync_nand_and_fetch_16: 385 case Builtin::BI__sync_val_compare_and_swap: 386 case Builtin::BI__sync_val_compare_and_swap_1: 387 case Builtin::BI__sync_val_compare_and_swap_2: 388 case Builtin::BI__sync_val_compare_and_swap_4: 389 case Builtin::BI__sync_val_compare_and_swap_8: 390 case Builtin::BI__sync_val_compare_and_swap_16: 391 case Builtin::BI__sync_bool_compare_and_swap: 392 case Builtin::BI__sync_bool_compare_and_swap_1: 393 case Builtin::BI__sync_bool_compare_and_swap_2: 394 case Builtin::BI__sync_bool_compare_and_swap_4: 395 case Builtin::BI__sync_bool_compare_and_swap_8: 396 case Builtin::BI__sync_bool_compare_and_swap_16: 397 case Builtin::BI__sync_lock_test_and_set: 398 case Builtin::BI__sync_lock_test_and_set_1: 399 case Builtin::BI__sync_lock_test_and_set_2: 400 case Builtin::BI__sync_lock_test_and_set_4: 401 case Builtin::BI__sync_lock_test_and_set_8: 402 case Builtin::BI__sync_lock_test_and_set_16: 403 case Builtin::BI__sync_lock_release: 404 case Builtin::BI__sync_lock_release_1: 405 case Builtin::BI__sync_lock_release_2: 406 case Builtin::BI__sync_lock_release_4: 407 case Builtin::BI__sync_lock_release_8: 408 case Builtin::BI__sync_lock_release_16: 409 case Builtin::BI__sync_swap: 410 case Builtin::BI__sync_swap_1: 411 case Builtin::BI__sync_swap_2: 412 case Builtin::BI__sync_swap_4: 413 case Builtin::BI__sync_swap_8: 414 case Builtin::BI__sync_swap_16: 415 return SemaBuiltinAtomicOverloaded(TheCallResult); 416 #define BUILTIN(ID, TYPE, ATTRS) 417 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ 418 case Builtin::BI##ID: \ 419 return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); 420 #include "clang/Basic/Builtins.def" 421 case Builtin::BI__builtin_annotation: 422 if (SemaBuiltinAnnotation(*this, TheCall)) 423 return ExprError(); 424 break; 425 case Builtin::BI__builtin_addressof: 426 if (SemaBuiltinAddressof(*this, TheCall)) 427 return ExprError(); 428 break; 429 case Builtin::BI__builtin_operator_new: 430 case Builtin::BI__builtin_operator_delete: 431 if (!getLangOpts().CPlusPlus) { 432 Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language) 433 << (BuiltinID == Builtin::BI__builtin_operator_new 434 ? "__builtin_operator_new" 435 : "__builtin_operator_delete") 436 << "C++"; 437 return ExprError(); 438 } 439 // CodeGen assumes it can find the global new and delete to call, 440 // so ensure that they are declared. 441 DeclareGlobalNewDelete(); 442 break; 443 444 // check secure string manipulation functions where overflows 445 // are detectable at compile time 446 case Builtin::BI__builtin___memcpy_chk: 447 case Builtin::BI__builtin___memmove_chk: 448 case Builtin::BI__builtin___memset_chk: 449 case Builtin::BI__builtin___strlcat_chk: 450 case Builtin::BI__builtin___strlcpy_chk: 451 case Builtin::BI__builtin___strncat_chk: 452 case Builtin::BI__builtin___strncpy_chk: 453 case Builtin::BI__builtin___stpncpy_chk: 454 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 2, 3); 455 break; 456 case Builtin::BI__builtin___memccpy_chk: 457 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 3, 4); 458 break; 459 case Builtin::BI__builtin___snprintf_chk: 460 case Builtin::BI__builtin___vsnprintf_chk: 461 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 1, 3); 462 break; 463 464 case Builtin::BI__builtin_call_with_static_chain: 465 if (SemaBuiltinCallWithStaticChain(*this, TheCall)) 466 return ExprError(); 467 break; 468 } 469 470 // Since the target specific builtins for each arch overlap, only check those 471 // of the arch we are compiling for. 472 if (BuiltinID >= Builtin::FirstTSBuiltin) { 473 switch (Context.getTargetInfo().getTriple().getArch()) { 474 case llvm::Triple::arm: 475 case llvm::Triple::armeb: 476 case llvm::Triple::thumb: 477 case llvm::Triple::thumbeb: 478 if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) 479 return ExprError(); 480 break; 481 case llvm::Triple::aarch64: 482 case llvm::Triple::aarch64_be: 483 if (CheckAArch64BuiltinFunctionCall(BuiltinID, TheCall)) 484 return ExprError(); 485 break; 486 case llvm::Triple::mips: 487 case llvm::Triple::mipsel: 488 case llvm::Triple::mips64: 489 case llvm::Triple::mips64el: 490 if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) 491 return ExprError(); 492 break; 493 case llvm::Triple::x86: 494 case llvm::Triple::x86_64: 495 if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall)) 496 return ExprError(); 497 break; 498 default: 499 break; 500 } 501 } 502 503 return TheCallResult; 504 } 505 506 // Get the valid immediate range for the specified NEON type code. 507 static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) { 508 NeonTypeFlags Type(t); 509 int IsQuad = ForceQuad ? true : Type.isQuad(); 510 switch (Type.getEltType()) { 511 case NeonTypeFlags::Int8: 512 case NeonTypeFlags::Poly8: 513 return shift ? 7 : (8 << IsQuad) - 1; 514 case NeonTypeFlags::Int16: 515 case NeonTypeFlags::Poly16: 516 return shift ? 15 : (4 << IsQuad) - 1; 517 case NeonTypeFlags::Int32: 518 return shift ? 31 : (2 << IsQuad) - 1; 519 case NeonTypeFlags::Int64: 520 case NeonTypeFlags::Poly64: 521 return shift ? 63 : (1 << IsQuad) - 1; 522 case NeonTypeFlags::Poly128: 523 return shift ? 127 : (1 << IsQuad) - 1; 524 case NeonTypeFlags::Float16: 525 assert(!shift && "cannot shift float types!"); 526 return (4 << IsQuad) - 1; 527 case NeonTypeFlags::Float32: 528 assert(!shift && "cannot shift float types!"); 529 return (2 << IsQuad) - 1; 530 case NeonTypeFlags::Float64: 531 assert(!shift && "cannot shift float types!"); 532 return (1 << IsQuad) - 1; 533 } 534 llvm_unreachable("Invalid NeonTypeFlag!"); 535 } 536 537 /// getNeonEltType - Return the QualType corresponding to the elements of 538 /// the vector type specified by the NeonTypeFlags. This is used to check 539 /// the pointer arguments for Neon load/store intrinsics. 540 static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context, 541 bool IsPolyUnsigned, bool IsInt64Long) { 542 switch (Flags.getEltType()) { 543 case NeonTypeFlags::Int8: 544 return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; 545 case NeonTypeFlags::Int16: 546 return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; 547 case NeonTypeFlags::Int32: 548 return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; 549 case NeonTypeFlags::Int64: 550 if (IsInt64Long) 551 return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy; 552 else 553 return Flags.isUnsigned() ? Context.UnsignedLongLongTy 554 : Context.LongLongTy; 555 case NeonTypeFlags::Poly8: 556 return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy; 557 case NeonTypeFlags::Poly16: 558 return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy; 559 case NeonTypeFlags::Poly64: 560 return Context.UnsignedLongTy; 561 case NeonTypeFlags::Poly128: 562 break; 563 case NeonTypeFlags::Float16: 564 return Context.HalfTy; 565 case NeonTypeFlags::Float32: 566 return Context.FloatTy; 567 case NeonTypeFlags::Float64: 568 return Context.DoubleTy; 569 } 570 llvm_unreachable("Invalid NeonTypeFlag!"); 571 } 572 573 bool Sema::CheckNeonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 574 llvm::APSInt Result; 575 uint64_t mask = 0; 576 unsigned TV = 0; 577 int PtrArgNum = -1; 578 bool HasConstPtr = false; 579 switch (BuiltinID) { 580 #define GET_NEON_OVERLOAD_CHECK 581 #include "clang/Basic/arm_neon.inc" 582 #undef GET_NEON_OVERLOAD_CHECK 583 } 584 585 // For NEON intrinsics which are overloaded on vector element type, validate 586 // the immediate which specifies which variant to emit. 587 unsigned ImmArg = TheCall->getNumArgs()-1; 588 if (mask) { 589 if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) 590 return true; 591 592 TV = Result.getLimitedValue(64); 593 if ((TV > 63) || (mask & (1ULL << TV)) == 0) 594 return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) 595 << TheCall->getArg(ImmArg)->getSourceRange(); 596 } 597 598 if (PtrArgNum >= 0) { 599 // Check that pointer arguments have the specified type. 600 Expr *Arg = TheCall->getArg(PtrArgNum); 601 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) 602 Arg = ICE->getSubExpr(); 603 ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); 604 QualType RHSTy = RHS.get()->getType(); 605 606 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); 607 bool IsPolyUnsigned = Arch == llvm::Triple::aarch64; 608 bool IsInt64Long = 609 Context.getTargetInfo().getInt64Type() == TargetInfo::SignedLong; 610 QualType EltTy = 611 getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long); 612 if (HasConstPtr) 613 EltTy = EltTy.withConst(); 614 QualType LHSTy = Context.getPointerType(EltTy); 615 AssignConvertType ConvTy; 616 ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); 617 if (RHS.isInvalid()) 618 return true; 619 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, 620 RHS.get(), AA_Assigning)) 621 return true; 622 } 623 624 // For NEON intrinsics which take an immediate value as part of the 625 // instruction, range check them here. 626 unsigned i = 0, l = 0, u = 0; 627 switch (BuiltinID) { 628 default: 629 return false; 630 #define GET_NEON_IMMEDIATE_CHECK 631 #include "clang/Basic/arm_neon.inc" 632 #undef GET_NEON_IMMEDIATE_CHECK 633 } 634 635 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); 636 } 637 638 bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall, 639 unsigned MaxWidth) { 640 assert((BuiltinID == ARM::BI__builtin_arm_ldrex || 641 BuiltinID == ARM::BI__builtin_arm_ldaex || 642 BuiltinID == ARM::BI__builtin_arm_strex || 643 BuiltinID == ARM::BI__builtin_arm_stlex || 644 BuiltinID == AArch64::BI__builtin_arm_ldrex || 645 BuiltinID == AArch64::BI__builtin_arm_ldaex || 646 BuiltinID == AArch64::BI__builtin_arm_strex || 647 BuiltinID == AArch64::BI__builtin_arm_stlex) && 648 "unexpected ARM builtin"); 649 bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex || 650 BuiltinID == ARM::BI__builtin_arm_ldaex || 651 BuiltinID == AArch64::BI__builtin_arm_ldrex || 652 BuiltinID == AArch64::BI__builtin_arm_ldaex; 653 654 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 655 656 // Ensure that we have the proper number of arguments. 657 if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2)) 658 return true; 659 660 // Inspect the pointer argument of the atomic builtin. This should always be 661 // a pointer type, whose element is an integral scalar or pointer type. 662 // Because it is a pointer type, we don't have to worry about any implicit 663 // casts here. 664 Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1); 665 ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg); 666 if (PointerArgRes.isInvalid()) 667 return true; 668 PointerArg = PointerArgRes.get(); 669 670 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>(); 671 if (!pointerType) { 672 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) 673 << PointerArg->getType() << PointerArg->getSourceRange(); 674 return true; 675 } 676 677 // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next 678 // task is to insert the appropriate casts into the AST. First work out just 679 // what the appropriate type is. 680 QualType ValType = pointerType->getPointeeType(); 681 QualType AddrType = ValType.getUnqualifiedType().withVolatile(); 682 if (IsLdrex) 683 AddrType.addConst(); 684 685 // Issue a warning if the cast is dodgy. 686 CastKind CastNeeded = CK_NoOp; 687 if (!AddrType.isAtLeastAsQualifiedAs(ValType)) { 688 CastNeeded = CK_BitCast; 689 Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers) 690 << PointerArg->getType() 691 << Context.getPointerType(AddrType) 692 << AA_Passing << PointerArg->getSourceRange(); 693 } 694 695 // Finally, do the cast and replace the argument with the corrected version. 696 AddrType = Context.getPointerType(AddrType); 697 PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded); 698 if (PointerArgRes.isInvalid()) 699 return true; 700 PointerArg = PointerArgRes.get(); 701 702 TheCall->setArg(IsLdrex ? 0 : 1, PointerArg); 703 704 // In general, we allow ints, floats and pointers to be loaded and stored. 705 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 706 !ValType->isBlockPointerType() && !ValType->isFloatingType()) { 707 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr) 708 << PointerArg->getType() << PointerArg->getSourceRange(); 709 return true; 710 } 711 712 // But ARM doesn't have instructions to deal with 128-bit versions. 713 if (Context.getTypeSize(ValType) > MaxWidth) { 714 assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate"); 715 Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size) 716 << PointerArg->getType() << PointerArg->getSourceRange(); 717 return true; 718 } 719 720 switch (ValType.getObjCLifetime()) { 721 case Qualifiers::OCL_None: 722 case Qualifiers::OCL_ExplicitNone: 723 // okay 724 break; 725 726 case Qualifiers::OCL_Weak: 727 case Qualifiers::OCL_Strong: 728 case Qualifiers::OCL_Autoreleasing: 729 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) 730 << ValType << PointerArg->getSourceRange(); 731 return true; 732 } 733 734 735 if (IsLdrex) { 736 TheCall->setType(ValType); 737 return false; 738 } 739 740 // Initialize the argument to be stored. 741 ExprResult ValArg = TheCall->getArg(0); 742 InitializedEntity Entity = InitializedEntity::InitializeParameter( 743 Context, ValType, /*consume*/ false); 744 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg); 745 if (ValArg.isInvalid()) 746 return true; 747 TheCall->setArg(0, ValArg.get()); 748 749 // __builtin_arm_strex always returns an int. It's marked as such in the .def, 750 // but the custom checker bypasses all default analysis. 751 TheCall->setType(Context.IntTy); 752 return false; 753 } 754 755 bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 756 llvm::APSInt Result; 757 758 if (BuiltinID == ARM::BI__builtin_arm_ldrex || 759 BuiltinID == ARM::BI__builtin_arm_ldaex || 760 BuiltinID == ARM::BI__builtin_arm_strex || 761 BuiltinID == ARM::BI__builtin_arm_stlex) { 762 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64); 763 } 764 765 if (BuiltinID == ARM::BI__builtin_arm_prefetch) { 766 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || 767 SemaBuiltinConstantArgRange(TheCall, 2, 0, 1); 768 } 769 770 if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) 771 return true; 772 773 // For intrinsics which take an immediate value as part of the instruction, 774 // range check them here. 775 unsigned i = 0, l = 0, u = 0; 776 switch (BuiltinID) { 777 default: return false; 778 case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; 779 case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; 780 case ARM::BI__builtin_arm_vcvtr_f: 781 case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; 782 case ARM::BI__builtin_arm_dmb: 783 case ARM::BI__builtin_arm_dsb: 784 case ARM::BI__builtin_arm_isb: 785 case ARM::BI__builtin_arm_dbg: l = 0; u = 15; break; 786 } 787 788 // FIXME: VFP Intrinsics should error if VFP not present. 789 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); 790 } 791 792 bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID, 793 CallExpr *TheCall) { 794 llvm::APSInt Result; 795 796 if (BuiltinID == AArch64::BI__builtin_arm_ldrex || 797 BuiltinID == AArch64::BI__builtin_arm_ldaex || 798 BuiltinID == AArch64::BI__builtin_arm_strex || 799 BuiltinID == AArch64::BI__builtin_arm_stlex) { 800 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128); 801 } 802 803 if (BuiltinID == AArch64::BI__builtin_arm_prefetch) { 804 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) || 805 SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) || 806 SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) || 807 SemaBuiltinConstantArgRange(TheCall, 4, 0, 1); 808 } 809 810 if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall)) 811 return true; 812 813 // For intrinsics which take an immediate value as part of the instruction, 814 // range check them here. 815 unsigned i = 0, l = 0, u = 0; 816 switch (BuiltinID) { 817 default: return false; 818 case AArch64::BI__builtin_arm_dmb: 819 case AArch64::BI__builtin_arm_dsb: 820 case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break; 821 } 822 823 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l); 824 } 825 826 bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 827 unsigned i = 0, l = 0, u = 0; 828 switch (BuiltinID) { 829 default: return false; 830 case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; 831 case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break; 832 case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break; 833 case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break; 834 case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break; 835 case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break; 836 case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break; 837 } 838 839 return SemaBuiltinConstantArgRange(TheCall, i, l, u); 840 } 841 842 bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { 843 unsigned i = 0, l = 0, u = 0; 844 switch (BuiltinID) { 845 default: return false; 846 case X86::BI_mm_prefetch: i = 1; l = 0; u = 3; break; 847 case X86::BI__builtin_ia32_cmpps: 848 case X86::BI__builtin_ia32_cmpss: 849 case X86::BI__builtin_ia32_cmppd: 850 case X86::BI__builtin_ia32_cmpsd: i = 2; l = 0; u = 31; break; 851 } 852 return SemaBuiltinConstantArgRange(TheCall, i, l, u); 853 } 854 855 /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo 856 /// parameter with the FormatAttr's correct format_idx and firstDataArg. 857 /// Returns true when the format fits the function and the FormatStringInfo has 858 /// been populated. 859 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, 860 FormatStringInfo *FSI) { 861 FSI->HasVAListArg = Format->getFirstArg() == 0; 862 FSI->FormatIdx = Format->getFormatIdx() - 1; 863 FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; 864 865 // The way the format attribute works in GCC, the implicit this argument 866 // of member functions is counted. However, it doesn't appear in our own 867 // lists, so decrement format_idx in that case. 868 if (IsCXXMember) { 869 if(FSI->FormatIdx == 0) 870 return false; 871 --FSI->FormatIdx; 872 if (FSI->FirstDataArg != 0) 873 --FSI->FirstDataArg; 874 } 875 return true; 876 } 877 878 /// Checks if a the given expression evaluates to null. 879 /// 880 /// \brief Returns true if the value evaluates to null. 881 static bool CheckNonNullExpr(Sema &S, 882 const Expr *Expr) { 883 // As a special case, transparent unions initialized with zero are 884 // considered null for the purposes of the nonnull attribute. 885 if (const RecordType *UT = Expr->getType()->getAsUnionType()) { 886 if (UT->getDecl()->hasAttr<TransparentUnionAttr>()) 887 if (const CompoundLiteralExpr *CLE = 888 dyn_cast<CompoundLiteralExpr>(Expr)) 889 if (const InitListExpr *ILE = 890 dyn_cast<InitListExpr>(CLE->getInitializer())) 891 Expr = ILE->getInit(0); 892 } 893 894 bool Result; 895 return (!Expr->isValueDependent() && 896 Expr->EvaluateAsBooleanCondition(Result, S.Context) && 897 !Result); 898 } 899 900 static void CheckNonNullArgument(Sema &S, 901 const Expr *ArgExpr, 902 SourceLocation CallSiteLoc) { 903 if (CheckNonNullExpr(S, ArgExpr)) 904 S.Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); 905 } 906 907 bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) { 908 FormatStringInfo FSI; 909 if ((GetFormatStringType(Format) == FST_NSString) && 910 getFormatStringInfo(Format, false, &FSI)) { 911 Idx = FSI.FormatIdx; 912 return true; 913 } 914 return false; 915 } 916 /// \brief Diagnose use of %s directive in an NSString which is being passed 917 /// as formatting string to formatting method. 918 static void 919 DiagnoseCStringFormatDirectiveInCFAPI(Sema &S, 920 const NamedDecl *FDecl, 921 Expr **Args, 922 unsigned NumArgs) { 923 unsigned Idx = 0; 924 bool Format = false; 925 ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily(); 926 if (SFFamily == ObjCStringFormatFamily::SFF_CFString) { 927 Idx = 2; 928 Format = true; 929 } 930 else 931 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { 932 if (S.GetFormatNSStringIdx(I, Idx)) { 933 Format = true; 934 break; 935 } 936 } 937 if (!Format || NumArgs <= Idx) 938 return; 939 const Expr *FormatExpr = Args[Idx]; 940 if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr)) 941 FormatExpr = CSCE->getSubExpr(); 942 const StringLiteral *FormatString; 943 if (const ObjCStringLiteral *OSL = 944 dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) 945 FormatString = OSL->getString(); 946 else 947 FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts()); 948 if (!FormatString) 949 return; 950 if (S.FormatStringHasSArg(FormatString)) { 951 S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string) 952 << "%s" << 1 << 1; 953 S.Diag(FDecl->getLocation(), diag::note_entity_declared_at) 954 << FDecl->getDeclName(); 955 } 956 } 957 958 static void CheckNonNullArguments(Sema &S, 959 const NamedDecl *FDecl, 960 ArrayRef<const Expr *> Args, 961 SourceLocation CallSiteLoc) { 962 // Check the attributes attached to the method/function itself. 963 llvm::SmallBitVector NonNullArgs; 964 for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) { 965 if (!NonNull->args_size()) { 966 // Easy case: all pointer arguments are nonnull. 967 for (const auto *Arg : Args) 968 if (S.isValidPointerAttrType(Arg->getType())) 969 CheckNonNullArgument(S, Arg, CallSiteLoc); 970 return; 971 } 972 973 for (unsigned Val : NonNull->args()) { 974 if (Val >= Args.size()) 975 continue; 976 if (NonNullArgs.empty()) 977 NonNullArgs.resize(Args.size()); 978 NonNullArgs.set(Val); 979 } 980 } 981 982 // Check the attributes on the parameters. 983 ArrayRef<ParmVarDecl*> parms; 984 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl)) 985 parms = FD->parameters(); 986 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(FDecl)) 987 parms = MD->parameters(); 988 989 unsigned ArgIndex = 0; 990 for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end(); 991 I != E; ++I, ++ArgIndex) { 992 const ParmVarDecl *PVD = *I; 993 if (PVD->hasAttr<NonNullAttr>() || 994 (ArgIndex < NonNullArgs.size() && NonNullArgs[ArgIndex])) 995 CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc); 996 } 997 998 // In case this is a variadic call, check any remaining arguments. 999 for (/**/; ArgIndex < NonNullArgs.size(); ++ArgIndex) 1000 if (NonNullArgs[ArgIndex]) 1001 CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc); 1002 } 1003 1004 /// Handles the checks for format strings, non-POD arguments to vararg 1005 /// functions, and NULL arguments passed to non-NULL parameters. 1006 void Sema::checkCall(NamedDecl *FDecl, ArrayRef<const Expr *> Args, 1007 unsigned NumParams, bool IsMemberFunction, 1008 SourceLocation Loc, SourceRange Range, 1009 VariadicCallType CallType) { 1010 // FIXME: We should check as much as we can in the template definition. 1011 if (CurContext->isDependentContext()) 1012 return; 1013 1014 // Printf and scanf checking. 1015 llvm::SmallBitVector CheckedVarArgs; 1016 if (FDecl) { 1017 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) { 1018 // Only create vector if there are format attributes. 1019 CheckedVarArgs.resize(Args.size()); 1020 1021 CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, 1022 CheckedVarArgs); 1023 } 1024 } 1025 1026 // Refuse POD arguments that weren't caught by the format string 1027 // checks above. 1028 if (CallType != VariadicDoesNotApply) { 1029 for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) { 1030 // Args[ArgIdx] can be null in malformed code. 1031 if (const Expr *Arg = Args[ArgIdx]) { 1032 if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx]) 1033 checkVariadicArgument(Arg, CallType); 1034 } 1035 } 1036 } 1037 1038 if (FDecl) { 1039 CheckNonNullArguments(*this, FDecl, Args, Loc); 1040 1041 // Type safety checking. 1042 for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>()) 1043 CheckArgumentWithTypeTag(I, Args.data()); 1044 } 1045 } 1046 1047 /// CheckConstructorCall - Check a constructor call for correctness and safety 1048 /// properties not enforced by the C type system. 1049 void Sema::CheckConstructorCall(FunctionDecl *FDecl, 1050 ArrayRef<const Expr *> Args, 1051 const FunctionProtoType *Proto, 1052 SourceLocation Loc) { 1053 VariadicCallType CallType = 1054 Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; 1055 checkCall(FDecl, Args, Proto->getNumParams(), 1056 /*IsMemberFunction=*/true, Loc, SourceRange(), CallType); 1057 } 1058 1059 /// CheckFunctionCall - Check a direct function call for various correctness 1060 /// and safety properties not strictly enforced by the C type system. 1061 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, 1062 const FunctionProtoType *Proto) { 1063 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && 1064 isa<CXXMethodDecl>(FDecl); 1065 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || 1066 IsMemberOperatorCall; 1067 VariadicCallType CallType = getVariadicCallType(FDecl, Proto, 1068 TheCall->getCallee()); 1069 unsigned NumParams = Proto ? Proto->getNumParams() : 0; 1070 Expr** Args = TheCall->getArgs(); 1071 unsigned NumArgs = TheCall->getNumArgs(); 1072 if (IsMemberOperatorCall) { 1073 // If this is a call to a member operator, hide the first argument 1074 // from checkCall. 1075 // FIXME: Our choice of AST representation here is less than ideal. 1076 ++Args; 1077 --NumArgs; 1078 } 1079 checkCall(FDecl, llvm::makeArrayRef(Args, NumArgs), NumParams, 1080 IsMemberFunction, TheCall->getRParenLoc(), 1081 TheCall->getCallee()->getSourceRange(), CallType); 1082 1083 IdentifierInfo *FnInfo = FDecl->getIdentifier(); 1084 // None of the checks below are needed for functions that don't have 1085 // simple names (e.g., C++ conversion functions). 1086 if (!FnInfo) 1087 return false; 1088 1089 CheckAbsoluteValueFunction(TheCall, FDecl, FnInfo); 1090 if (getLangOpts().ObjC1) 1091 DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs); 1092 1093 unsigned CMId = FDecl->getMemoryFunctionKind(); 1094 if (CMId == 0) 1095 return false; 1096 1097 // Handle memory setting and copying functions. 1098 if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) 1099 CheckStrlcpycatArguments(TheCall, FnInfo); 1100 else if (CMId == Builtin::BIstrncat) 1101 CheckStrncatArguments(TheCall, FnInfo); 1102 else 1103 CheckMemaccessArguments(TheCall, CMId, FnInfo); 1104 1105 return false; 1106 } 1107 1108 bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, 1109 ArrayRef<const Expr *> Args) { 1110 VariadicCallType CallType = 1111 Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; 1112 1113 checkCall(Method, Args, Method->param_size(), 1114 /*IsMemberFunction=*/false, 1115 lbrac, Method->getSourceRange(), CallType); 1116 1117 return false; 1118 } 1119 1120 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall, 1121 const FunctionProtoType *Proto) { 1122 const VarDecl *V = dyn_cast<VarDecl>(NDecl); 1123 if (!V) 1124 return false; 1125 1126 QualType Ty = V->getType(); 1127 if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType()) 1128 return false; 1129 1130 VariadicCallType CallType; 1131 if (!Proto || !Proto->isVariadic()) { 1132 CallType = VariadicDoesNotApply; 1133 } else if (Ty->isBlockPointerType()) { 1134 CallType = VariadicBlock; 1135 } else { // Ty->isFunctionPointerType() 1136 CallType = VariadicFunction; 1137 } 1138 unsigned NumParams = Proto ? Proto->getNumParams() : 0; 1139 1140 checkCall(NDecl, llvm::makeArrayRef(TheCall->getArgs(), 1141 TheCall->getNumArgs()), 1142 NumParams, /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 1143 TheCall->getCallee()->getSourceRange(), CallType); 1144 1145 return false; 1146 } 1147 1148 /// Checks function calls when a FunctionDecl or a NamedDecl is not available, 1149 /// such as function pointers returned from functions. 1150 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) { 1151 VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto, 1152 TheCall->getCallee()); 1153 unsigned NumParams = Proto ? Proto->getNumParams() : 0; 1154 1155 checkCall(/*FDecl=*/nullptr, 1156 llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()), 1157 NumParams, /*IsMemberFunction=*/false, TheCall->getRParenLoc(), 1158 TheCall->getCallee()->getSourceRange(), CallType); 1159 1160 return false; 1161 } 1162 1163 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) { 1164 if (Ordering < AtomicExpr::AO_ABI_memory_order_relaxed || 1165 Ordering > AtomicExpr::AO_ABI_memory_order_seq_cst) 1166 return false; 1167 1168 switch (Op) { 1169 case AtomicExpr::AO__c11_atomic_init: 1170 llvm_unreachable("There is no ordering argument for an init"); 1171 1172 case AtomicExpr::AO__c11_atomic_load: 1173 case AtomicExpr::AO__atomic_load_n: 1174 case AtomicExpr::AO__atomic_load: 1175 return Ordering != AtomicExpr::AO_ABI_memory_order_release && 1176 Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel; 1177 1178 case AtomicExpr::AO__c11_atomic_store: 1179 case AtomicExpr::AO__atomic_store: 1180 case AtomicExpr::AO__atomic_store_n: 1181 return Ordering != AtomicExpr::AO_ABI_memory_order_consume && 1182 Ordering != AtomicExpr::AO_ABI_memory_order_acquire && 1183 Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel; 1184 1185 default: 1186 return true; 1187 } 1188 } 1189 1190 ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, 1191 AtomicExpr::AtomicOp Op) { 1192 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); 1193 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 1194 1195 // All these operations take one of the following forms: 1196 enum { 1197 // C __c11_atomic_init(A *, C) 1198 Init, 1199 // C __c11_atomic_load(A *, int) 1200 Load, 1201 // void __atomic_load(A *, CP, int) 1202 Copy, 1203 // C __c11_atomic_add(A *, M, int) 1204 Arithmetic, 1205 // C __atomic_exchange_n(A *, CP, int) 1206 Xchg, 1207 // void __atomic_exchange(A *, C *, CP, int) 1208 GNUXchg, 1209 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) 1210 C11CmpXchg, 1211 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) 1212 GNUCmpXchg 1213 } Form = Init; 1214 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; 1215 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; 1216 // where: 1217 // C is an appropriate type, 1218 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, 1219 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, 1220 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and 1221 // the int parameters are for orderings. 1222 1223 assert(AtomicExpr::AO__c11_atomic_init == 0 && 1224 AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load 1225 && "need to update code for modified C11 atomics"); 1226 bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && 1227 Op <= AtomicExpr::AO__c11_atomic_fetch_xor; 1228 bool IsN = Op == AtomicExpr::AO__atomic_load_n || 1229 Op == AtomicExpr::AO__atomic_store_n || 1230 Op == AtomicExpr::AO__atomic_exchange_n || 1231 Op == AtomicExpr::AO__atomic_compare_exchange_n; 1232 bool IsAddSub = false; 1233 1234 switch (Op) { 1235 case AtomicExpr::AO__c11_atomic_init: 1236 Form = Init; 1237 break; 1238 1239 case AtomicExpr::AO__c11_atomic_load: 1240 case AtomicExpr::AO__atomic_load_n: 1241 Form = Load; 1242 break; 1243 1244 case AtomicExpr::AO__c11_atomic_store: 1245 case AtomicExpr::AO__atomic_load: 1246 case AtomicExpr::AO__atomic_store: 1247 case AtomicExpr::AO__atomic_store_n: 1248 Form = Copy; 1249 break; 1250 1251 case AtomicExpr::AO__c11_atomic_fetch_add: 1252 case AtomicExpr::AO__c11_atomic_fetch_sub: 1253 case AtomicExpr::AO__atomic_fetch_add: 1254 case AtomicExpr::AO__atomic_fetch_sub: 1255 case AtomicExpr::AO__atomic_add_fetch: 1256 case AtomicExpr::AO__atomic_sub_fetch: 1257 IsAddSub = true; 1258 // Fall through. 1259 case AtomicExpr::AO__c11_atomic_fetch_and: 1260 case AtomicExpr::AO__c11_atomic_fetch_or: 1261 case AtomicExpr::AO__c11_atomic_fetch_xor: 1262 case AtomicExpr::AO__atomic_fetch_and: 1263 case AtomicExpr::AO__atomic_fetch_or: 1264 case AtomicExpr::AO__atomic_fetch_xor: 1265 case AtomicExpr::AO__atomic_fetch_nand: 1266 case AtomicExpr::AO__atomic_and_fetch: 1267 case AtomicExpr::AO__atomic_or_fetch: 1268 case AtomicExpr::AO__atomic_xor_fetch: 1269 case AtomicExpr::AO__atomic_nand_fetch: 1270 Form = Arithmetic; 1271 break; 1272 1273 case AtomicExpr::AO__c11_atomic_exchange: 1274 case AtomicExpr::AO__atomic_exchange_n: 1275 Form = Xchg; 1276 break; 1277 1278 case AtomicExpr::AO__atomic_exchange: 1279 Form = GNUXchg; 1280 break; 1281 1282 case AtomicExpr::AO__c11_atomic_compare_exchange_strong: 1283 case AtomicExpr::AO__c11_atomic_compare_exchange_weak: 1284 Form = C11CmpXchg; 1285 break; 1286 1287 case AtomicExpr::AO__atomic_compare_exchange: 1288 case AtomicExpr::AO__atomic_compare_exchange_n: 1289 Form = GNUCmpXchg; 1290 break; 1291 } 1292 1293 // Check we have the right number of arguments. 1294 if (TheCall->getNumArgs() < NumArgs[Form]) { 1295 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) 1296 << 0 << NumArgs[Form] << TheCall->getNumArgs() 1297 << TheCall->getCallee()->getSourceRange(); 1298 return ExprError(); 1299 } else if (TheCall->getNumArgs() > NumArgs[Form]) { 1300 Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), 1301 diag::err_typecheck_call_too_many_args) 1302 << 0 << NumArgs[Form] << TheCall->getNumArgs() 1303 << TheCall->getCallee()->getSourceRange(); 1304 return ExprError(); 1305 } 1306 1307 // Inspect the first argument of the atomic operation. 1308 Expr *Ptr = TheCall->getArg(0); 1309 Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); 1310 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); 1311 if (!pointerType) { 1312 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) 1313 << Ptr->getType() << Ptr->getSourceRange(); 1314 return ExprError(); 1315 } 1316 1317 // For a __c11 builtin, this should be a pointer to an _Atomic type. 1318 QualType AtomTy = pointerType->getPointeeType(); // 'A' 1319 QualType ValType = AtomTy; // 'C' 1320 if (IsC11) { 1321 if (!AtomTy->isAtomicType()) { 1322 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) 1323 << Ptr->getType() << Ptr->getSourceRange(); 1324 return ExprError(); 1325 } 1326 if (AtomTy.isConstQualified()) { 1327 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic) 1328 << Ptr->getType() << Ptr->getSourceRange(); 1329 return ExprError(); 1330 } 1331 ValType = AtomTy->getAs<AtomicType>()->getValueType(); 1332 } 1333 1334 // For an arithmetic operation, the implied arithmetic must be well-formed. 1335 if (Form == Arithmetic) { 1336 // gcc does not enforce these rules for GNU atomics, but we do so for sanity. 1337 if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { 1338 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) 1339 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 1340 return ExprError(); 1341 } 1342 if (!IsAddSub && !ValType->isIntegerType()) { 1343 Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) 1344 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 1345 return ExprError(); 1346 } 1347 if (IsC11 && ValType->isPointerType() && 1348 RequireCompleteType(Ptr->getLocStart(), ValType->getPointeeType(), 1349 diag::err_incomplete_type)) { 1350 return ExprError(); 1351 } 1352 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { 1353 // For __atomic_*_n operations, the value type must be a scalar integral or 1354 // pointer type which is 1, 2, 4, 8 or 16 bytes in length. 1355 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) 1356 << IsC11 << Ptr->getType() << Ptr->getSourceRange(); 1357 return ExprError(); 1358 } 1359 1360 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) && 1361 !AtomTy->isScalarType()) { 1362 // For GNU atomics, require a trivially-copyable type. This is not part of 1363 // the GNU atomics specification, but we enforce it for sanity. 1364 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) 1365 << Ptr->getType() << Ptr->getSourceRange(); 1366 return ExprError(); 1367 } 1368 1369 // FIXME: For any builtin other than a load, the ValType must not be 1370 // const-qualified. 1371 1372 switch (ValType.getObjCLifetime()) { 1373 case Qualifiers::OCL_None: 1374 case Qualifiers::OCL_ExplicitNone: 1375 // okay 1376 break; 1377 1378 case Qualifiers::OCL_Weak: 1379 case Qualifiers::OCL_Strong: 1380 case Qualifiers::OCL_Autoreleasing: 1381 // FIXME: Can this happen? By this point, ValType should be known 1382 // to be trivially copyable. 1383 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) 1384 << ValType << Ptr->getSourceRange(); 1385 return ExprError(); 1386 } 1387 1388 QualType ResultType = ValType; 1389 if (Form == Copy || Form == GNUXchg || Form == Init) 1390 ResultType = Context.VoidTy; 1391 else if (Form == C11CmpXchg || Form == GNUCmpXchg) 1392 ResultType = Context.BoolTy; 1393 1394 // The type of a parameter passed 'by value'. In the GNU atomics, such 1395 // arguments are actually passed as pointers. 1396 QualType ByValType = ValType; // 'CP' 1397 if (!IsC11 && !IsN) 1398 ByValType = Ptr->getType(); 1399 1400 // The first argument --- the pointer --- has a fixed type; we 1401 // deduce the types of the rest of the arguments accordingly. Walk 1402 // the remaining arguments, converting them to the deduced value type. 1403 for (unsigned i = 1; i != NumArgs[Form]; ++i) { 1404 QualType Ty; 1405 if (i < NumVals[Form] + 1) { 1406 switch (i) { 1407 case 1: 1408 // The second argument is the non-atomic operand. For arithmetic, this 1409 // is always passed by value, and for a compare_exchange it is always 1410 // passed by address. For the rest, GNU uses by-address and C11 uses 1411 // by-value. 1412 assert(Form != Load); 1413 if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) 1414 Ty = ValType; 1415 else if (Form == Copy || Form == Xchg) 1416 Ty = ByValType; 1417 else if (Form == Arithmetic) 1418 Ty = Context.getPointerDiffType(); 1419 else 1420 Ty = Context.getPointerType(ValType.getUnqualifiedType()); 1421 break; 1422 case 2: 1423 // The third argument to compare_exchange / GNU exchange is a 1424 // (pointer to a) desired value. 1425 Ty = ByValType; 1426 break; 1427 case 3: 1428 // The fourth argument to GNU compare_exchange is a 'weak' flag. 1429 Ty = Context.BoolTy; 1430 break; 1431 } 1432 } else { 1433 // The order(s) are always converted to int. 1434 Ty = Context.IntTy; 1435 } 1436 1437 InitializedEntity Entity = 1438 InitializedEntity::InitializeParameter(Context, Ty, false); 1439 ExprResult Arg = TheCall->getArg(i); 1440 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 1441 if (Arg.isInvalid()) 1442 return true; 1443 TheCall->setArg(i, Arg.get()); 1444 } 1445 1446 // Permute the arguments into a 'consistent' order. 1447 SmallVector<Expr*, 5> SubExprs; 1448 SubExprs.push_back(Ptr); 1449 switch (Form) { 1450 case Init: 1451 // Note, AtomicExpr::getVal1() has a special case for this atomic. 1452 SubExprs.push_back(TheCall->getArg(1)); // Val1 1453 break; 1454 case Load: 1455 SubExprs.push_back(TheCall->getArg(1)); // Order 1456 break; 1457 case Copy: 1458 case Arithmetic: 1459 case Xchg: 1460 SubExprs.push_back(TheCall->getArg(2)); // Order 1461 SubExprs.push_back(TheCall->getArg(1)); // Val1 1462 break; 1463 case GNUXchg: 1464 // Note, AtomicExpr::getVal2() has a special case for this atomic. 1465 SubExprs.push_back(TheCall->getArg(3)); // Order 1466 SubExprs.push_back(TheCall->getArg(1)); // Val1 1467 SubExprs.push_back(TheCall->getArg(2)); // Val2 1468 break; 1469 case C11CmpXchg: 1470 SubExprs.push_back(TheCall->getArg(3)); // Order 1471 SubExprs.push_back(TheCall->getArg(1)); // Val1 1472 SubExprs.push_back(TheCall->getArg(4)); // OrderFail 1473 SubExprs.push_back(TheCall->getArg(2)); // Val2 1474 break; 1475 case GNUCmpXchg: 1476 SubExprs.push_back(TheCall->getArg(4)); // Order 1477 SubExprs.push_back(TheCall->getArg(1)); // Val1 1478 SubExprs.push_back(TheCall->getArg(5)); // OrderFail 1479 SubExprs.push_back(TheCall->getArg(2)); // Val2 1480 SubExprs.push_back(TheCall->getArg(3)); // Weak 1481 break; 1482 } 1483 1484 if (SubExprs.size() >= 2 && Form != Init) { 1485 llvm::APSInt Result(32); 1486 if (SubExprs[1]->isIntegerConstantExpr(Result, Context) && 1487 !isValidOrderingForOp(Result.getSExtValue(), Op)) 1488 Diag(SubExprs[1]->getLocStart(), 1489 diag::warn_atomic_op_has_invalid_memory_order) 1490 << SubExprs[1]->getSourceRange(); 1491 } 1492 1493 AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), 1494 SubExprs, ResultType, Op, 1495 TheCall->getRParenLoc()); 1496 1497 if ((Op == AtomicExpr::AO__c11_atomic_load || 1498 (Op == AtomicExpr::AO__c11_atomic_store)) && 1499 Context.AtomicUsesUnsupportedLibcall(AE)) 1500 Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib) << 1501 ((Op == AtomicExpr::AO__c11_atomic_load) ? 0 : 1); 1502 1503 return AE; 1504 } 1505 1506 1507 /// checkBuiltinArgument - Given a call to a builtin function, perform 1508 /// normal type-checking on the given argument, updating the call in 1509 /// place. This is useful when a builtin function requires custom 1510 /// type-checking for some of its arguments but not necessarily all of 1511 /// them. 1512 /// 1513 /// Returns true on error. 1514 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { 1515 FunctionDecl *Fn = E->getDirectCallee(); 1516 assert(Fn && "builtin call without direct callee!"); 1517 1518 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); 1519 InitializedEntity Entity = 1520 InitializedEntity::InitializeParameter(S.Context, Param); 1521 1522 ExprResult Arg = E->getArg(0); 1523 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); 1524 if (Arg.isInvalid()) 1525 return true; 1526 1527 E->setArg(ArgIndex, Arg.get()); 1528 return false; 1529 } 1530 1531 /// SemaBuiltinAtomicOverloaded - We have a call to a function like 1532 /// __sync_fetch_and_add, which is an overloaded function based on the pointer 1533 /// type of its first argument. The main ActOnCallExpr routines have already 1534 /// promoted the types of arguments because all of these calls are prototyped as 1535 /// void(...). 1536 /// 1537 /// This function goes through and does final semantic checking for these 1538 /// builtins, 1539 ExprResult 1540 Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { 1541 CallExpr *TheCall = (CallExpr *)TheCallResult.get(); 1542 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 1543 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 1544 1545 // Ensure that we have at least one argument to do type inference from. 1546 if (TheCall->getNumArgs() < 1) { 1547 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) 1548 << 0 << 1 << TheCall->getNumArgs() 1549 << TheCall->getCallee()->getSourceRange(); 1550 return ExprError(); 1551 } 1552 1553 // Inspect the first argument of the atomic builtin. This should always be 1554 // a pointer type, whose element is an integral scalar or pointer type. 1555 // Because it is a pointer type, we don't have to worry about any implicit 1556 // casts here. 1557 // FIXME: We don't allow floating point scalars as input. 1558 Expr *FirstArg = TheCall->getArg(0); 1559 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); 1560 if (FirstArgResult.isInvalid()) 1561 return ExprError(); 1562 FirstArg = FirstArgResult.get(); 1563 TheCall->setArg(0, FirstArg); 1564 1565 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); 1566 if (!pointerType) { 1567 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) 1568 << FirstArg->getType() << FirstArg->getSourceRange(); 1569 return ExprError(); 1570 } 1571 1572 QualType ValType = pointerType->getPointeeType(); 1573 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && 1574 !ValType->isBlockPointerType()) { 1575 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) 1576 << FirstArg->getType() << FirstArg->getSourceRange(); 1577 return ExprError(); 1578 } 1579 1580 switch (ValType.getObjCLifetime()) { 1581 case Qualifiers::OCL_None: 1582 case Qualifiers::OCL_ExplicitNone: 1583 // okay 1584 break; 1585 1586 case Qualifiers::OCL_Weak: 1587 case Qualifiers::OCL_Strong: 1588 case Qualifiers::OCL_Autoreleasing: 1589 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) 1590 << ValType << FirstArg->getSourceRange(); 1591 return ExprError(); 1592 } 1593 1594 // Strip any qualifiers off ValType. 1595 ValType = ValType.getUnqualifiedType(); 1596 1597 // The majority of builtins return a value, but a few have special return 1598 // types, so allow them to override appropriately below. 1599 QualType ResultType = ValType; 1600 1601 // We need to figure out which concrete builtin this maps onto. For example, 1602 // __sync_fetch_and_add with a 2 byte object turns into 1603 // __sync_fetch_and_add_2. 1604 #define BUILTIN_ROW(x) \ 1605 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ 1606 Builtin::BI##x##_8, Builtin::BI##x##_16 } 1607 1608 static const unsigned BuiltinIndices[][5] = { 1609 BUILTIN_ROW(__sync_fetch_and_add), 1610 BUILTIN_ROW(__sync_fetch_and_sub), 1611 BUILTIN_ROW(__sync_fetch_and_or), 1612 BUILTIN_ROW(__sync_fetch_and_and), 1613 BUILTIN_ROW(__sync_fetch_and_xor), 1614 BUILTIN_ROW(__sync_fetch_and_nand), 1615 1616 BUILTIN_ROW(__sync_add_and_fetch), 1617 BUILTIN_ROW(__sync_sub_and_fetch), 1618 BUILTIN_ROW(__sync_and_and_fetch), 1619 BUILTIN_ROW(__sync_or_and_fetch), 1620 BUILTIN_ROW(__sync_xor_and_fetch), 1621 BUILTIN_ROW(__sync_nand_and_fetch), 1622 1623 BUILTIN_ROW(__sync_val_compare_and_swap), 1624 BUILTIN_ROW(__sync_bool_compare_and_swap), 1625 BUILTIN_ROW(__sync_lock_test_and_set), 1626 BUILTIN_ROW(__sync_lock_release), 1627 BUILTIN_ROW(__sync_swap) 1628 }; 1629 #undef BUILTIN_ROW 1630 1631 // Determine the index of the size. 1632 unsigned SizeIndex; 1633 switch (Context.getTypeSizeInChars(ValType).getQuantity()) { 1634 case 1: SizeIndex = 0; break; 1635 case 2: SizeIndex = 1; break; 1636 case 4: SizeIndex = 2; break; 1637 case 8: SizeIndex = 3; break; 1638 case 16: SizeIndex = 4; break; 1639 default: 1640 Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) 1641 << FirstArg->getType() << FirstArg->getSourceRange(); 1642 return ExprError(); 1643 } 1644 1645 // Each of these builtins has one pointer argument, followed by some number of 1646 // values (0, 1 or 2) followed by a potentially empty varags list of stuff 1647 // that we ignore. Find out which row of BuiltinIndices to read from as well 1648 // as the number of fixed args. 1649 unsigned BuiltinID = FDecl->getBuiltinID(); 1650 unsigned BuiltinIndex, NumFixed = 1; 1651 bool WarnAboutSemanticsChange = false; 1652 switch (BuiltinID) { 1653 default: llvm_unreachable("Unknown overloaded atomic builtin!"); 1654 case Builtin::BI__sync_fetch_and_add: 1655 case Builtin::BI__sync_fetch_and_add_1: 1656 case Builtin::BI__sync_fetch_and_add_2: 1657 case Builtin::BI__sync_fetch_and_add_4: 1658 case Builtin::BI__sync_fetch_and_add_8: 1659 case Builtin::BI__sync_fetch_and_add_16: 1660 BuiltinIndex = 0; 1661 break; 1662 1663 case Builtin::BI__sync_fetch_and_sub: 1664 case Builtin::BI__sync_fetch_and_sub_1: 1665 case Builtin::BI__sync_fetch_and_sub_2: 1666 case Builtin::BI__sync_fetch_and_sub_4: 1667 case Builtin::BI__sync_fetch_and_sub_8: 1668 case Builtin::BI__sync_fetch_and_sub_16: 1669 BuiltinIndex = 1; 1670 break; 1671 1672 case Builtin::BI__sync_fetch_and_or: 1673 case Builtin::BI__sync_fetch_and_or_1: 1674 case Builtin::BI__sync_fetch_and_or_2: 1675 case Builtin::BI__sync_fetch_and_or_4: 1676 case Builtin::BI__sync_fetch_and_or_8: 1677 case Builtin::BI__sync_fetch_and_or_16: 1678 BuiltinIndex = 2; 1679 break; 1680 1681 case Builtin::BI__sync_fetch_and_and: 1682 case Builtin::BI__sync_fetch_and_and_1: 1683 case Builtin::BI__sync_fetch_and_and_2: 1684 case Builtin::BI__sync_fetch_and_and_4: 1685 case Builtin::BI__sync_fetch_and_and_8: 1686 case Builtin::BI__sync_fetch_and_and_16: 1687 BuiltinIndex = 3; 1688 break; 1689 1690 case Builtin::BI__sync_fetch_and_xor: 1691 case Builtin::BI__sync_fetch_and_xor_1: 1692 case Builtin::BI__sync_fetch_and_xor_2: 1693 case Builtin::BI__sync_fetch_and_xor_4: 1694 case Builtin::BI__sync_fetch_and_xor_8: 1695 case Builtin::BI__sync_fetch_and_xor_16: 1696 BuiltinIndex = 4; 1697 break; 1698 1699 case Builtin::BI__sync_fetch_and_nand: 1700 case Builtin::BI__sync_fetch_and_nand_1: 1701 case Builtin::BI__sync_fetch_and_nand_2: 1702 case Builtin::BI__sync_fetch_and_nand_4: 1703 case Builtin::BI__sync_fetch_and_nand_8: 1704 case Builtin::BI__sync_fetch_and_nand_16: 1705 BuiltinIndex = 5; 1706 WarnAboutSemanticsChange = true; 1707 break; 1708 1709 case Builtin::BI__sync_add_and_fetch: 1710 case Builtin::BI__sync_add_and_fetch_1: 1711 case Builtin::BI__sync_add_and_fetch_2: 1712 case Builtin::BI__sync_add_and_fetch_4: 1713 case Builtin::BI__sync_add_and_fetch_8: 1714 case Builtin::BI__sync_add_and_fetch_16: 1715 BuiltinIndex = 6; 1716 break; 1717 1718 case Builtin::BI__sync_sub_and_fetch: 1719 case Builtin::BI__sync_sub_and_fetch_1: 1720 case Builtin::BI__sync_sub_and_fetch_2: 1721 case Builtin::BI__sync_sub_and_fetch_4: 1722 case Builtin::BI__sync_sub_and_fetch_8: 1723 case Builtin::BI__sync_sub_and_fetch_16: 1724 BuiltinIndex = 7; 1725 break; 1726 1727 case Builtin::BI__sync_and_and_fetch: 1728 case Builtin::BI__sync_and_and_fetch_1: 1729 case Builtin::BI__sync_and_and_fetch_2: 1730 case Builtin::BI__sync_and_and_fetch_4: 1731 case Builtin::BI__sync_and_and_fetch_8: 1732 case Builtin::BI__sync_and_and_fetch_16: 1733 BuiltinIndex = 8; 1734 break; 1735 1736 case Builtin::BI__sync_or_and_fetch: 1737 case Builtin::BI__sync_or_and_fetch_1: 1738 case Builtin::BI__sync_or_and_fetch_2: 1739 case Builtin::BI__sync_or_and_fetch_4: 1740 case Builtin::BI__sync_or_and_fetch_8: 1741 case Builtin::BI__sync_or_and_fetch_16: 1742 BuiltinIndex = 9; 1743 break; 1744 1745 case Builtin::BI__sync_xor_and_fetch: 1746 case Builtin::BI__sync_xor_and_fetch_1: 1747 case Builtin::BI__sync_xor_and_fetch_2: 1748 case Builtin::BI__sync_xor_and_fetch_4: 1749 case Builtin::BI__sync_xor_and_fetch_8: 1750 case Builtin::BI__sync_xor_and_fetch_16: 1751 BuiltinIndex = 10; 1752 break; 1753 1754 case Builtin::BI__sync_nand_and_fetch: 1755 case Builtin::BI__sync_nand_and_fetch_1: 1756 case Builtin::BI__sync_nand_and_fetch_2: 1757 case Builtin::BI__sync_nand_and_fetch_4: 1758 case Builtin::BI__sync_nand_and_fetch_8: 1759 case Builtin::BI__sync_nand_and_fetch_16: 1760 BuiltinIndex = 11; 1761 WarnAboutSemanticsChange = true; 1762 break; 1763 1764 case Builtin::BI__sync_val_compare_and_swap: 1765 case Builtin::BI__sync_val_compare_and_swap_1: 1766 case Builtin::BI__sync_val_compare_and_swap_2: 1767 case Builtin::BI__sync_val_compare_and_swap_4: 1768 case Builtin::BI__sync_val_compare_and_swap_8: 1769 case Builtin::BI__sync_val_compare_and_swap_16: 1770 BuiltinIndex = 12; 1771 NumFixed = 2; 1772 break; 1773 1774 case Builtin::BI__sync_bool_compare_and_swap: 1775 case Builtin::BI__sync_bool_compare_and_swap_1: 1776 case Builtin::BI__sync_bool_compare_and_swap_2: 1777 case Builtin::BI__sync_bool_compare_and_swap_4: 1778 case Builtin::BI__sync_bool_compare_and_swap_8: 1779 case Builtin::BI__sync_bool_compare_and_swap_16: 1780 BuiltinIndex = 13; 1781 NumFixed = 2; 1782 ResultType = Context.BoolTy; 1783 break; 1784 1785 case Builtin::BI__sync_lock_test_and_set: 1786 case Builtin::BI__sync_lock_test_and_set_1: 1787 case Builtin::BI__sync_lock_test_and_set_2: 1788 case Builtin::BI__sync_lock_test_and_set_4: 1789 case Builtin::BI__sync_lock_test_and_set_8: 1790 case Builtin::BI__sync_lock_test_and_set_16: 1791 BuiltinIndex = 14; 1792 break; 1793 1794 case Builtin::BI__sync_lock_release: 1795 case Builtin::BI__sync_lock_release_1: 1796 case Builtin::BI__sync_lock_release_2: 1797 case Builtin::BI__sync_lock_release_4: 1798 case Builtin::BI__sync_lock_release_8: 1799 case Builtin::BI__sync_lock_release_16: 1800 BuiltinIndex = 15; 1801 NumFixed = 0; 1802 ResultType = Context.VoidTy; 1803 break; 1804 1805 case Builtin::BI__sync_swap: 1806 case Builtin::BI__sync_swap_1: 1807 case Builtin::BI__sync_swap_2: 1808 case Builtin::BI__sync_swap_4: 1809 case Builtin::BI__sync_swap_8: 1810 case Builtin::BI__sync_swap_16: 1811 BuiltinIndex = 16; 1812 break; 1813 } 1814 1815 // Now that we know how many fixed arguments we expect, first check that we 1816 // have at least that many. 1817 if (TheCall->getNumArgs() < 1+NumFixed) { 1818 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) 1819 << 0 << 1+NumFixed << TheCall->getNumArgs() 1820 << TheCall->getCallee()->getSourceRange(); 1821 return ExprError(); 1822 } 1823 1824 if (WarnAboutSemanticsChange) { 1825 Diag(TheCall->getLocEnd(), diag::warn_sync_fetch_and_nand_semantics_change) 1826 << TheCall->getCallee()->getSourceRange(); 1827 } 1828 1829 // Get the decl for the concrete builtin from this, we can tell what the 1830 // concrete integer type we should convert to is. 1831 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; 1832 const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); 1833 FunctionDecl *NewBuiltinDecl; 1834 if (NewBuiltinID == BuiltinID) 1835 NewBuiltinDecl = FDecl; 1836 else { 1837 // Perform builtin lookup to avoid redeclaring it. 1838 DeclarationName DN(&Context.Idents.get(NewBuiltinName)); 1839 LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName); 1840 LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); 1841 assert(Res.getFoundDecl()); 1842 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); 1843 if (!NewBuiltinDecl) 1844 return ExprError(); 1845 } 1846 1847 // The first argument --- the pointer --- has a fixed type; we 1848 // deduce the types of the rest of the arguments accordingly. Walk 1849 // the remaining arguments, converting them to the deduced value type. 1850 for (unsigned i = 0; i != NumFixed; ++i) { 1851 ExprResult Arg = TheCall->getArg(i+1); 1852 1853 // GCC does an implicit conversion to the pointer or integer ValType. This 1854 // can fail in some cases (1i -> int**), check for this error case now. 1855 // Initialize the argument. 1856 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 1857 ValType, /*consume*/ false); 1858 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 1859 if (Arg.isInvalid()) 1860 return ExprError(); 1861 1862 // Okay, we have something that *can* be converted to the right type. Check 1863 // to see if there is a potentially weird extension going on here. This can 1864 // happen when you do an atomic operation on something like an char* and 1865 // pass in 42. The 42 gets converted to char. This is even more strange 1866 // for things like 45.123 -> char, etc. 1867 // FIXME: Do this check. 1868 TheCall->setArg(i+1, Arg.get()); 1869 } 1870 1871 ASTContext& Context = this->getASTContext(); 1872 1873 // Create a new DeclRefExpr to refer to the new decl. 1874 DeclRefExpr* NewDRE = DeclRefExpr::Create( 1875 Context, 1876 DRE->getQualifierLoc(), 1877 SourceLocation(), 1878 NewBuiltinDecl, 1879 /*enclosing*/ false, 1880 DRE->getLocation(), 1881 Context.BuiltinFnTy, 1882 DRE->getValueKind()); 1883 1884 // Set the callee in the CallExpr. 1885 // FIXME: This loses syntactic information. 1886 QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); 1887 ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, 1888 CK_BuiltinFnToFnPtr); 1889 TheCall->setCallee(PromotedCall.get()); 1890 1891 // Change the result type of the call to match the original value type. This 1892 // is arbitrary, but the codegen for these builtins ins design to handle it 1893 // gracefully. 1894 TheCall->setType(ResultType); 1895 1896 return TheCallResult; 1897 } 1898 1899 /// CheckObjCString - Checks that the argument to the builtin 1900 /// CFString constructor is correct 1901 /// Note: It might also make sense to do the UTF-16 conversion here (would 1902 /// simplify the backend). 1903 bool Sema::CheckObjCString(Expr *Arg) { 1904 Arg = Arg->IgnoreParenCasts(); 1905 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); 1906 1907 if (!Literal || !Literal->isAscii()) { 1908 Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) 1909 << Arg->getSourceRange(); 1910 return true; 1911 } 1912 1913 if (Literal->containsNonAsciiOrNull()) { 1914 StringRef String = Literal->getString(); 1915 unsigned NumBytes = String.size(); 1916 SmallVector<UTF16, 128> ToBuf(NumBytes); 1917 const UTF8 *FromPtr = (const UTF8 *)String.data(); 1918 UTF16 *ToPtr = &ToBuf[0]; 1919 1920 ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 1921 &ToPtr, ToPtr + NumBytes, 1922 strictConversion); 1923 // Check for conversion failure. 1924 if (Result != conversionOK) 1925 Diag(Arg->getLocStart(), 1926 diag::warn_cfstring_truncated) << Arg->getSourceRange(); 1927 } 1928 return false; 1929 } 1930 1931 /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. 1932 /// Emit an error and return true on failure, return false on success. 1933 bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { 1934 Expr *Fn = TheCall->getCallee(); 1935 if (TheCall->getNumArgs() > 2) { 1936 Diag(TheCall->getArg(2)->getLocStart(), 1937 diag::err_typecheck_call_too_many_args) 1938 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 1939 << Fn->getSourceRange() 1940 << SourceRange(TheCall->getArg(2)->getLocStart(), 1941 (*(TheCall->arg_end()-1))->getLocEnd()); 1942 return true; 1943 } 1944 1945 if (TheCall->getNumArgs() < 2) { 1946 return Diag(TheCall->getLocEnd(), 1947 diag::err_typecheck_call_too_few_args_at_least) 1948 << 0 /*function call*/ << 2 << TheCall->getNumArgs(); 1949 } 1950 1951 // Type-check the first argument normally. 1952 if (checkBuiltinArgument(*this, TheCall, 0)) 1953 return true; 1954 1955 // Determine whether the current function is variadic or not. 1956 BlockScopeInfo *CurBlock = getCurBlock(); 1957 bool isVariadic; 1958 if (CurBlock) 1959 isVariadic = CurBlock->TheDecl->isVariadic(); 1960 else if (FunctionDecl *FD = getCurFunctionDecl()) 1961 isVariadic = FD->isVariadic(); 1962 else 1963 isVariadic = getCurMethodDecl()->isVariadic(); 1964 1965 if (!isVariadic) { 1966 Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); 1967 return true; 1968 } 1969 1970 // Verify that the second argument to the builtin is the last argument of the 1971 // current function or method. 1972 bool SecondArgIsLastNamedArgument = false; 1973 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); 1974 1975 // These are valid if SecondArgIsLastNamedArgument is false after the next 1976 // block. 1977 QualType Type; 1978 SourceLocation ParamLoc; 1979 1980 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { 1981 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { 1982 // FIXME: This isn't correct for methods (results in bogus warning). 1983 // Get the last formal in the current function. 1984 const ParmVarDecl *LastArg; 1985 if (CurBlock) 1986 LastArg = *(CurBlock->TheDecl->param_end()-1); 1987 else if (FunctionDecl *FD = getCurFunctionDecl()) 1988 LastArg = *(FD->param_end()-1); 1989 else 1990 LastArg = *(getCurMethodDecl()->param_end()-1); 1991 SecondArgIsLastNamedArgument = PV == LastArg; 1992 1993 Type = PV->getType(); 1994 ParamLoc = PV->getLocation(); 1995 } 1996 } 1997 1998 if (!SecondArgIsLastNamedArgument) 1999 Diag(TheCall->getArg(1)->getLocStart(), 2000 diag::warn_second_parameter_of_va_start_not_last_named_argument); 2001 else if (Type->isReferenceType()) { 2002 Diag(Arg->getLocStart(), 2003 diag::warn_va_start_of_reference_type_is_undefined); 2004 Diag(ParamLoc, diag::note_parameter_type) << Type; 2005 } 2006 2007 TheCall->setType(Context.VoidTy); 2008 return false; 2009 } 2010 2011 bool Sema::SemaBuiltinVAStartARM(CallExpr *Call) { 2012 // void __va_start(va_list *ap, const char *named_addr, size_t slot_size, 2013 // const char *named_addr); 2014 2015 Expr *Func = Call->getCallee(); 2016 2017 if (Call->getNumArgs() < 3) 2018 return Diag(Call->getLocEnd(), 2019 diag::err_typecheck_call_too_few_args_at_least) 2020 << 0 /*function call*/ << 3 << Call->getNumArgs(); 2021 2022 // Determine whether the current function is variadic or not. 2023 bool IsVariadic; 2024 if (BlockScopeInfo *CurBlock = getCurBlock()) 2025 IsVariadic = CurBlock->TheDecl->isVariadic(); 2026 else if (FunctionDecl *FD = getCurFunctionDecl()) 2027 IsVariadic = FD->isVariadic(); 2028 else if (ObjCMethodDecl *MD = getCurMethodDecl()) 2029 IsVariadic = MD->isVariadic(); 2030 else 2031 llvm_unreachable("unexpected statement type"); 2032 2033 if (!IsVariadic) { 2034 Diag(Func->getLocStart(), diag::err_va_start_used_in_non_variadic_function); 2035 return true; 2036 } 2037 2038 // Type-check the first argument normally. 2039 if (checkBuiltinArgument(*this, Call, 0)) 2040 return true; 2041 2042 static const struct { 2043 unsigned ArgNo; 2044 QualType Type; 2045 } ArgumentTypes[] = { 2046 { 1, Context.getPointerType(Context.CharTy.withConst()) }, 2047 { 2, Context.getSizeType() }, 2048 }; 2049 2050 for (const auto &AT : ArgumentTypes) { 2051 const Expr *Arg = Call->getArg(AT.ArgNo)->IgnoreParens(); 2052 if (Arg->getType().getCanonicalType() == AT.Type.getCanonicalType()) 2053 continue; 2054 Diag(Arg->getLocStart(), diag::err_typecheck_convert_incompatible) 2055 << Arg->getType() << AT.Type << 1 /* different class */ 2056 << 0 /* qualifier difference */ << 3 /* parameter mismatch */ 2057 << AT.ArgNo + 1 << Arg->getType() << AT.Type; 2058 } 2059 2060 return false; 2061 } 2062 2063 /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and 2064 /// friends. This is declared to take (...), so we have to check everything. 2065 bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { 2066 if (TheCall->getNumArgs() < 2) 2067 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) 2068 << 0 << 2 << TheCall->getNumArgs()/*function call*/; 2069 if (TheCall->getNumArgs() > 2) 2070 return Diag(TheCall->getArg(2)->getLocStart(), 2071 diag::err_typecheck_call_too_many_args) 2072 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 2073 << SourceRange(TheCall->getArg(2)->getLocStart(), 2074 (*(TheCall->arg_end()-1))->getLocEnd()); 2075 2076 ExprResult OrigArg0 = TheCall->getArg(0); 2077 ExprResult OrigArg1 = TheCall->getArg(1); 2078 2079 // Do standard promotions between the two arguments, returning their common 2080 // type. 2081 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); 2082 if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) 2083 return true; 2084 2085 // Make sure any conversions are pushed back into the call; this is 2086 // type safe since unordered compare builtins are declared as "_Bool 2087 // foo(...)". 2088 TheCall->setArg(0, OrigArg0.get()); 2089 TheCall->setArg(1, OrigArg1.get()); 2090 2091 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) 2092 return false; 2093 2094 // If the common type isn't a real floating type, then the arguments were 2095 // invalid for this operation. 2096 if (Res.isNull() || !Res->isRealFloatingType()) 2097 return Diag(OrigArg0.get()->getLocStart(), 2098 diag::err_typecheck_call_invalid_ordered_compare) 2099 << OrigArg0.get()->getType() << OrigArg1.get()->getType() 2100 << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); 2101 2102 return false; 2103 } 2104 2105 /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like 2106 /// __builtin_isnan and friends. This is declared to take (...), so we have 2107 /// to check everything. We expect the last argument to be a floating point 2108 /// value. 2109 bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { 2110 if (TheCall->getNumArgs() < NumArgs) 2111 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) 2112 << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; 2113 if (TheCall->getNumArgs() > NumArgs) 2114 return Diag(TheCall->getArg(NumArgs)->getLocStart(), 2115 diag::err_typecheck_call_too_many_args) 2116 << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() 2117 << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), 2118 (*(TheCall->arg_end()-1))->getLocEnd()); 2119 2120 Expr *OrigArg = TheCall->getArg(NumArgs-1); 2121 2122 if (OrigArg->isTypeDependent()) 2123 return false; 2124 2125 // This operation requires a non-_Complex floating-point number. 2126 if (!OrigArg->getType()->isRealFloatingType()) 2127 return Diag(OrigArg->getLocStart(), 2128 diag::err_typecheck_call_invalid_unary_fp) 2129 << OrigArg->getType() << OrigArg->getSourceRange(); 2130 2131 // If this is an implicit conversion from float -> double, remove it. 2132 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { 2133 Expr *CastArg = Cast->getSubExpr(); 2134 if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { 2135 assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && 2136 "promotion from float to double is the only expected cast here"); 2137 Cast->setSubExpr(nullptr); 2138 TheCall->setArg(NumArgs-1, CastArg); 2139 } 2140 } 2141 2142 return false; 2143 } 2144 2145 /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. 2146 // This is declared to take (...), so we have to check everything. 2147 ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { 2148 if (TheCall->getNumArgs() < 2) 2149 return ExprError(Diag(TheCall->getLocEnd(), 2150 diag::err_typecheck_call_too_few_args_at_least) 2151 << 0 /*function call*/ << 2 << TheCall->getNumArgs() 2152 << TheCall->getSourceRange()); 2153 2154 // Determine which of the following types of shufflevector we're checking: 2155 // 1) unary, vector mask: (lhs, mask) 2156 // 2) binary, vector mask: (lhs, rhs, mask) 2157 // 3) binary, scalar mask: (lhs, rhs, index, ..., index) 2158 QualType resType = TheCall->getArg(0)->getType(); 2159 unsigned numElements = 0; 2160 2161 if (!TheCall->getArg(0)->isTypeDependent() && 2162 !TheCall->getArg(1)->isTypeDependent()) { 2163 QualType LHSType = TheCall->getArg(0)->getType(); 2164 QualType RHSType = TheCall->getArg(1)->getType(); 2165 2166 if (!LHSType->isVectorType() || !RHSType->isVectorType()) 2167 return ExprError(Diag(TheCall->getLocStart(), 2168 diag::err_shufflevector_non_vector) 2169 << SourceRange(TheCall->getArg(0)->getLocStart(), 2170 TheCall->getArg(1)->getLocEnd())); 2171 2172 numElements = LHSType->getAs<VectorType>()->getNumElements(); 2173 unsigned numResElements = TheCall->getNumArgs() - 2; 2174 2175 // Check to see if we have a call with 2 vector arguments, the unary shuffle 2176 // with mask. If so, verify that RHS is an integer vector type with the 2177 // same number of elts as lhs. 2178 if (TheCall->getNumArgs() == 2) { 2179 if (!RHSType->hasIntegerRepresentation() || 2180 RHSType->getAs<VectorType>()->getNumElements() != numElements) 2181 return ExprError(Diag(TheCall->getLocStart(), 2182 diag::err_shufflevector_incompatible_vector) 2183 << SourceRange(TheCall->getArg(1)->getLocStart(), 2184 TheCall->getArg(1)->getLocEnd())); 2185 } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { 2186 return ExprError(Diag(TheCall->getLocStart(), 2187 diag::err_shufflevector_incompatible_vector) 2188 << SourceRange(TheCall->getArg(0)->getLocStart(), 2189 TheCall->getArg(1)->getLocEnd())); 2190 } else if (numElements != numResElements) { 2191 QualType eltType = LHSType->getAs<VectorType>()->getElementType(); 2192 resType = Context.getVectorType(eltType, numResElements, 2193 VectorType::GenericVector); 2194 } 2195 } 2196 2197 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { 2198 if (TheCall->getArg(i)->isTypeDependent() || 2199 TheCall->getArg(i)->isValueDependent()) 2200 continue; 2201 2202 llvm::APSInt Result(32); 2203 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) 2204 return ExprError(Diag(TheCall->getLocStart(), 2205 diag::err_shufflevector_nonconstant_argument) 2206 << TheCall->getArg(i)->getSourceRange()); 2207 2208 // Allow -1 which will be translated to undef in the IR. 2209 if (Result.isSigned() && Result.isAllOnesValue()) 2210 continue; 2211 2212 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) 2213 return ExprError(Diag(TheCall->getLocStart(), 2214 diag::err_shufflevector_argument_too_large) 2215 << TheCall->getArg(i)->getSourceRange()); 2216 } 2217 2218 SmallVector<Expr*, 32> exprs; 2219 2220 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { 2221 exprs.push_back(TheCall->getArg(i)); 2222 TheCall->setArg(i, nullptr); 2223 } 2224 2225 return new (Context) ShuffleVectorExpr(Context, exprs, resType, 2226 TheCall->getCallee()->getLocStart(), 2227 TheCall->getRParenLoc()); 2228 } 2229 2230 /// SemaConvertVectorExpr - Handle __builtin_convertvector 2231 ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo, 2232 SourceLocation BuiltinLoc, 2233 SourceLocation RParenLoc) { 2234 ExprValueKind VK = VK_RValue; 2235 ExprObjectKind OK = OK_Ordinary; 2236 QualType DstTy = TInfo->getType(); 2237 QualType SrcTy = E->getType(); 2238 2239 if (!SrcTy->isVectorType() && !SrcTy->isDependentType()) 2240 return ExprError(Diag(BuiltinLoc, 2241 diag::err_convertvector_non_vector) 2242 << E->getSourceRange()); 2243 if (!DstTy->isVectorType() && !DstTy->isDependentType()) 2244 return ExprError(Diag(BuiltinLoc, 2245 diag::err_convertvector_non_vector_type)); 2246 2247 if (!SrcTy->isDependentType() && !DstTy->isDependentType()) { 2248 unsigned SrcElts = SrcTy->getAs<VectorType>()->getNumElements(); 2249 unsigned DstElts = DstTy->getAs<VectorType>()->getNumElements(); 2250 if (SrcElts != DstElts) 2251 return ExprError(Diag(BuiltinLoc, 2252 diag::err_convertvector_incompatible_vector) 2253 << E->getSourceRange()); 2254 } 2255 2256 return new (Context) 2257 ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc); 2258 } 2259 2260 /// SemaBuiltinPrefetch - Handle __builtin_prefetch. 2261 // This is declared to take (const void*, ...) and can take two 2262 // optional constant int args. 2263 bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { 2264 unsigned NumArgs = TheCall->getNumArgs(); 2265 2266 if (NumArgs > 3) 2267 return Diag(TheCall->getLocEnd(), 2268 diag::err_typecheck_call_too_many_args_at_most) 2269 << 0 /*function call*/ << 3 << NumArgs 2270 << TheCall->getSourceRange(); 2271 2272 // Argument 0 is checked for us and the remaining arguments must be 2273 // constant integers. 2274 for (unsigned i = 1; i != NumArgs; ++i) 2275 if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3)) 2276 return true; 2277 2278 return false; 2279 } 2280 2281 /// SemaBuiltinAssume - Handle __assume (MS Extension). 2282 // __assume does not evaluate its arguments, and should warn if its argument 2283 // has side effects. 2284 bool Sema::SemaBuiltinAssume(CallExpr *TheCall) { 2285 Expr *Arg = TheCall->getArg(0); 2286 if (Arg->isInstantiationDependent()) return false; 2287 2288 if (Arg->HasSideEffects(Context)) 2289 return Diag(Arg->getLocStart(), diag::warn_assume_side_effects) 2290 << Arg->getSourceRange() 2291 << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier(); 2292 2293 return false; 2294 } 2295 2296 /// Handle __builtin_assume_aligned. This is declared 2297 /// as (const void*, size_t, ...) and can take one optional constant int arg. 2298 bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) { 2299 unsigned NumArgs = TheCall->getNumArgs(); 2300 2301 if (NumArgs > 3) 2302 return Diag(TheCall->getLocEnd(), 2303 diag::err_typecheck_call_too_many_args_at_most) 2304 << 0 /*function call*/ << 3 << NumArgs 2305 << TheCall->getSourceRange(); 2306 2307 // The alignment must be a constant integer. 2308 Expr *Arg = TheCall->getArg(1); 2309 2310 // We can't check the value of a dependent argument. 2311 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { 2312 llvm::APSInt Result; 2313 if (SemaBuiltinConstantArg(TheCall, 1, Result)) 2314 return true; 2315 2316 if (!Result.isPowerOf2()) 2317 return Diag(TheCall->getLocStart(), 2318 diag::err_alignment_not_power_of_two) 2319 << Arg->getSourceRange(); 2320 } 2321 2322 if (NumArgs > 2) { 2323 ExprResult Arg(TheCall->getArg(2)); 2324 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, 2325 Context.getSizeType(), false); 2326 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); 2327 if (Arg.isInvalid()) return true; 2328 TheCall->setArg(2, Arg.get()); 2329 } 2330 2331 return false; 2332 } 2333 2334 /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr 2335 /// TheCall is a constant expression. 2336 bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, 2337 llvm::APSInt &Result) { 2338 Expr *Arg = TheCall->getArg(ArgNum); 2339 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); 2340 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); 2341 2342 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; 2343 2344 if (!Arg->isIntegerConstantExpr(Result, Context)) 2345 return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) 2346 << FDecl->getDeclName() << Arg->getSourceRange(); 2347 2348 return false; 2349 } 2350 2351 /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr 2352 /// TheCall is a constant expression in the range [Low, High]. 2353 bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, 2354 int Low, int High) { 2355 llvm::APSInt Result; 2356 2357 // We can't check the value of a dependent argument. 2358 Expr *Arg = TheCall->getArg(ArgNum); 2359 if (Arg->isTypeDependent() || Arg->isValueDependent()) 2360 return false; 2361 2362 // Check constant-ness first. 2363 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result)) 2364 return true; 2365 2366 if (Result.getSExtValue() < Low || Result.getSExtValue() > High) 2367 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) 2368 << Low << High << Arg->getSourceRange(); 2369 2370 return false; 2371 } 2372 2373 /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). 2374 /// This checks that the target supports __builtin_longjmp and 2375 /// that val is a constant 1. 2376 bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { 2377 if (!Context.getTargetInfo().hasSjLjLowering()) 2378 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_unsupported) 2379 << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd()); 2380 2381 Expr *Arg = TheCall->getArg(1); 2382 llvm::APSInt Result; 2383 2384 // TODO: This is less than ideal. Overload this to take a value. 2385 if (SemaBuiltinConstantArg(TheCall, 1, Result)) 2386 return true; 2387 2388 if (Result != 1) 2389 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) 2390 << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); 2391 2392 return false; 2393 } 2394 2395 2396 /// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]). 2397 /// This checks that the target supports __builtin_setjmp. 2398 bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) { 2399 if (!Context.getTargetInfo().hasSjLjLowering()) 2400 return Diag(TheCall->getLocStart(), diag::err_builtin_setjmp_unsupported) 2401 << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd()); 2402 return false; 2403 } 2404 2405 namespace { 2406 enum StringLiteralCheckType { 2407 SLCT_NotALiteral, 2408 SLCT_UncheckedLiteral, 2409 SLCT_CheckedLiteral 2410 }; 2411 } 2412 2413 // Determine if an expression is a string literal or constant string. 2414 // If this function returns false on the arguments to a function expecting a 2415 // format string, we will usually need to emit a warning. 2416 // True string literals are then checked by CheckFormatString. 2417 static StringLiteralCheckType 2418 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args, 2419 bool HasVAListArg, unsigned format_idx, 2420 unsigned firstDataArg, Sema::FormatStringType Type, 2421 Sema::VariadicCallType CallType, bool InFunctionCall, 2422 llvm::SmallBitVector &CheckedVarArgs) { 2423 tryAgain: 2424 if (E->isTypeDependent() || E->isValueDependent()) 2425 return SLCT_NotALiteral; 2426 2427 E = E->IgnoreParenCasts(); 2428 2429 if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)) 2430 // Technically -Wformat-nonliteral does not warn about this case. 2431 // The behavior of printf and friends in this case is implementation 2432 // dependent. Ideally if the format string cannot be null then 2433 // it should have a 'nonnull' attribute in the function prototype. 2434 return SLCT_UncheckedLiteral; 2435 2436 switch (E->getStmtClass()) { 2437 case Stmt::BinaryConditionalOperatorClass: 2438 case Stmt::ConditionalOperatorClass: { 2439 // The expression is a literal if both sub-expressions were, and it was 2440 // completely checked only if both sub-expressions were checked. 2441 const AbstractConditionalOperator *C = 2442 cast<AbstractConditionalOperator>(E); 2443 StringLiteralCheckType Left = 2444 checkFormatStringExpr(S, C->getTrueExpr(), Args, 2445 HasVAListArg, format_idx, firstDataArg, 2446 Type, CallType, InFunctionCall, CheckedVarArgs); 2447 if (Left == SLCT_NotALiteral) 2448 return SLCT_NotALiteral; 2449 StringLiteralCheckType Right = 2450 checkFormatStringExpr(S, C->getFalseExpr(), Args, 2451 HasVAListArg, format_idx, firstDataArg, 2452 Type, CallType, InFunctionCall, CheckedVarArgs); 2453 return Left < Right ? Left : Right; 2454 } 2455 2456 case Stmt::ImplicitCastExprClass: { 2457 E = cast<ImplicitCastExpr>(E)->getSubExpr(); 2458 goto tryAgain; 2459 } 2460 2461 case Stmt::OpaqueValueExprClass: 2462 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { 2463 E = src; 2464 goto tryAgain; 2465 } 2466 return SLCT_NotALiteral; 2467 2468 case Stmt::PredefinedExprClass: 2469 // While __func__, etc., are technically not string literals, they 2470 // cannot contain format specifiers and thus are not a security 2471 // liability. 2472 return SLCT_UncheckedLiteral; 2473 2474 case Stmt::DeclRefExprClass: { 2475 const DeclRefExpr *DR = cast<DeclRefExpr>(E); 2476 2477 // As an exception, do not flag errors for variables binding to 2478 // const string literals. 2479 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { 2480 bool isConstant = false; 2481 QualType T = DR->getType(); 2482 2483 if (const ArrayType *AT = S.Context.getAsArrayType(T)) { 2484 isConstant = AT->getElementType().isConstant(S.Context); 2485 } else if (const PointerType *PT = T->getAs<PointerType>()) { 2486 isConstant = T.isConstant(S.Context) && 2487 PT->getPointeeType().isConstant(S.Context); 2488 } else if (T->isObjCObjectPointerType()) { 2489 // In ObjC, there is usually no "const ObjectPointer" type, 2490 // so don't check if the pointee type is constant. 2491 isConstant = T.isConstant(S.Context); 2492 } 2493 2494 if (isConstant) { 2495 if (const Expr *Init = VD->getAnyInitializer()) { 2496 // Look through initializers like const char c[] = { "foo" } 2497 if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { 2498 if (InitList->isStringLiteralInit()) 2499 Init = InitList->getInit(0)->IgnoreParenImpCasts(); 2500 } 2501 return checkFormatStringExpr(S, Init, Args, 2502 HasVAListArg, format_idx, 2503 firstDataArg, Type, CallType, 2504 /*InFunctionCall*/false, CheckedVarArgs); 2505 } 2506 } 2507 2508 // For vprintf* functions (i.e., HasVAListArg==true), we add a 2509 // special check to see if the format string is a function parameter 2510 // of the function calling the printf function. If the function 2511 // has an attribute indicating it is a printf-like function, then we 2512 // should suppress warnings concerning non-literals being used in a call 2513 // to a vprintf function. For example: 2514 // 2515 // void 2516 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ 2517 // va_list ap; 2518 // va_start(ap, fmt); 2519 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". 2520 // ... 2521 // } 2522 if (HasVAListArg) { 2523 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { 2524 if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { 2525 int PVIndex = PV->getFunctionScopeIndex() + 1; 2526 for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) { 2527 // adjust for implicit parameter 2528 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) 2529 if (MD->isInstance()) 2530 ++PVIndex; 2531 // We also check if the formats are compatible. 2532 // We can't pass a 'scanf' string to a 'printf' function. 2533 if (PVIndex == PVFormat->getFormatIdx() && 2534 Type == S.GetFormatStringType(PVFormat)) 2535 return SLCT_UncheckedLiteral; 2536 } 2537 } 2538 } 2539 } 2540 } 2541 2542 return SLCT_NotALiteral; 2543 } 2544 2545 case Stmt::CallExprClass: 2546 case Stmt::CXXMemberCallExprClass: { 2547 const CallExpr *CE = cast<CallExpr>(E); 2548 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { 2549 if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { 2550 unsigned ArgIndex = FA->getFormatIdx(); 2551 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) 2552 if (MD->isInstance()) 2553 --ArgIndex; 2554 const Expr *Arg = CE->getArg(ArgIndex - 1); 2555 2556 return checkFormatStringExpr(S, Arg, Args, 2557 HasVAListArg, format_idx, firstDataArg, 2558 Type, CallType, InFunctionCall, 2559 CheckedVarArgs); 2560 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { 2561 unsigned BuiltinID = FD->getBuiltinID(); 2562 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || 2563 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { 2564 const Expr *Arg = CE->getArg(0); 2565 return checkFormatStringExpr(S, Arg, Args, 2566 HasVAListArg, format_idx, 2567 firstDataArg, Type, CallType, 2568 InFunctionCall, CheckedVarArgs); 2569 } 2570 } 2571 } 2572 2573 return SLCT_NotALiteral; 2574 } 2575 case Stmt::ObjCStringLiteralClass: 2576 case Stmt::StringLiteralClass: { 2577 const StringLiteral *StrE = nullptr; 2578 2579 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) 2580 StrE = ObjCFExpr->getString(); 2581 else 2582 StrE = cast<StringLiteral>(E); 2583 2584 if (StrE) { 2585 S.CheckFormatString(StrE, E, Args, HasVAListArg, format_idx, firstDataArg, 2586 Type, InFunctionCall, CallType, CheckedVarArgs); 2587 return SLCT_CheckedLiteral; 2588 } 2589 2590 return SLCT_NotALiteral; 2591 } 2592 2593 default: 2594 return SLCT_NotALiteral; 2595 } 2596 } 2597 2598 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { 2599 return llvm::StringSwitch<FormatStringType>(Format->getType()->getName()) 2600 .Case("scanf", FST_Scanf) 2601 .Cases("printf", "printf0", FST_Printf) 2602 .Cases("NSString", "CFString", FST_NSString) 2603 .Case("strftime", FST_Strftime) 2604 .Case("strfmon", FST_Strfmon) 2605 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) 2606 .Case("freebsd_kprintf", FST_FreeBSDKPrintf) 2607 .Default(FST_Unknown); 2608 } 2609 2610 /// CheckFormatArguments - Check calls to printf and scanf (and similar 2611 /// functions) for correct use of format strings. 2612 /// Returns true if a format string has been fully checked. 2613 bool Sema::CheckFormatArguments(const FormatAttr *Format, 2614 ArrayRef<const Expr *> Args, 2615 bool IsCXXMember, 2616 VariadicCallType CallType, 2617 SourceLocation Loc, SourceRange Range, 2618 llvm::SmallBitVector &CheckedVarArgs) { 2619 FormatStringInfo FSI; 2620 if (getFormatStringInfo(Format, IsCXXMember, &FSI)) 2621 return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx, 2622 FSI.FirstDataArg, GetFormatStringType(Format), 2623 CallType, Loc, Range, CheckedVarArgs); 2624 return false; 2625 } 2626 2627 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, 2628 bool HasVAListArg, unsigned format_idx, 2629 unsigned firstDataArg, FormatStringType Type, 2630 VariadicCallType CallType, 2631 SourceLocation Loc, SourceRange Range, 2632 llvm::SmallBitVector &CheckedVarArgs) { 2633 // CHECK: printf/scanf-like function is called with no format string. 2634 if (format_idx >= Args.size()) { 2635 Diag(Loc, diag::warn_missing_format_string) << Range; 2636 return false; 2637 } 2638 2639 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); 2640 2641 // CHECK: format string is not a string literal. 2642 // 2643 // Dynamically generated format strings are difficult to 2644 // automatically vet at compile time. Requiring that format strings 2645 // are string literals: (1) permits the checking of format strings by 2646 // the compiler and thereby (2) can practically remove the source of 2647 // many format string exploits. 2648 2649 // Format string can be either ObjC string (e.g. @"%d") or 2650 // C string (e.g. "%d") 2651 // ObjC string uses the same format specifiers as C string, so we can use 2652 // the same format string checking logic for both ObjC and C strings. 2653 StringLiteralCheckType CT = 2654 checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg, 2655 format_idx, firstDataArg, Type, CallType, 2656 /*IsFunctionCall*/true, CheckedVarArgs); 2657 if (CT != SLCT_NotALiteral) 2658 // Literal format string found, check done! 2659 return CT == SLCT_CheckedLiteral; 2660 2661 // Strftime is particular as it always uses a single 'time' argument, 2662 // so it is safe to pass a non-literal string. 2663 if (Type == FST_Strftime) 2664 return false; 2665 2666 // Do not emit diag when the string param is a macro expansion and the 2667 // format is either NSString or CFString. This is a hack to prevent 2668 // diag when using the NSLocalizedString and CFCopyLocalizedString macros 2669 // which are usually used in place of NS and CF string literals. 2670 if (Type == FST_NSString && 2671 SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) 2672 return false; 2673 2674 // If there are no arguments specified, warn with -Wformat-security, otherwise 2675 // warn only with -Wformat-nonliteral. 2676 if (Args.size() == firstDataArg) 2677 Diag(Args[format_idx]->getLocStart(), 2678 diag::warn_format_nonliteral_noargs) 2679 << OrigFormatExpr->getSourceRange(); 2680 else 2681 Diag(Args[format_idx]->getLocStart(), 2682 diag::warn_format_nonliteral) 2683 << OrigFormatExpr->getSourceRange(); 2684 return false; 2685 } 2686 2687 namespace { 2688 class CheckFormatHandler : public analyze_format_string::FormatStringHandler { 2689 protected: 2690 Sema &S; 2691 const StringLiteral *FExpr; 2692 const Expr *OrigFormatExpr; 2693 const unsigned FirstDataArg; 2694 const unsigned NumDataArgs; 2695 const char *Beg; // Start of format string. 2696 const bool HasVAListArg; 2697 ArrayRef<const Expr *> Args; 2698 unsigned FormatIdx; 2699 llvm::SmallBitVector CoveredArgs; 2700 bool usesPositionalArgs; 2701 bool atFirstArg; 2702 bool inFunctionCall; 2703 Sema::VariadicCallType CallType; 2704 llvm::SmallBitVector &CheckedVarArgs; 2705 public: 2706 CheckFormatHandler(Sema &s, const StringLiteral *fexpr, 2707 const Expr *origFormatExpr, unsigned firstDataArg, 2708 unsigned numDataArgs, const char *beg, bool hasVAListArg, 2709 ArrayRef<const Expr *> Args, 2710 unsigned formatIdx, bool inFunctionCall, 2711 Sema::VariadicCallType callType, 2712 llvm::SmallBitVector &CheckedVarArgs) 2713 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), 2714 FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), 2715 Beg(beg), HasVAListArg(hasVAListArg), 2716 Args(Args), FormatIdx(formatIdx), 2717 usesPositionalArgs(false), atFirstArg(true), 2718 inFunctionCall(inFunctionCall), CallType(callType), 2719 CheckedVarArgs(CheckedVarArgs) { 2720 CoveredArgs.resize(numDataArgs); 2721 CoveredArgs.reset(); 2722 } 2723 2724 void DoneProcessing(); 2725 2726 void HandleIncompleteSpecifier(const char *startSpecifier, 2727 unsigned specifierLen) override; 2728 2729 void HandleInvalidLengthModifier( 2730 const analyze_format_string::FormatSpecifier &FS, 2731 const analyze_format_string::ConversionSpecifier &CS, 2732 const char *startSpecifier, unsigned specifierLen, 2733 unsigned DiagID); 2734 2735 void HandleNonStandardLengthModifier( 2736 const analyze_format_string::FormatSpecifier &FS, 2737 const char *startSpecifier, unsigned specifierLen); 2738 2739 void HandleNonStandardConversionSpecifier( 2740 const analyze_format_string::ConversionSpecifier &CS, 2741 const char *startSpecifier, unsigned specifierLen); 2742 2743 void HandlePosition(const char *startPos, unsigned posLen) override; 2744 2745 void HandleInvalidPosition(const char *startSpecifier, 2746 unsigned specifierLen, 2747 analyze_format_string::PositionContext p) override; 2748 2749 void HandleZeroPosition(const char *startPos, unsigned posLen) override; 2750 2751 void HandleNullChar(const char *nullCharacter) override; 2752 2753 template <typename Range> 2754 static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, 2755 const Expr *ArgumentExpr, 2756 PartialDiagnostic PDiag, 2757 SourceLocation StringLoc, 2758 bool IsStringLocation, Range StringRange, 2759 ArrayRef<FixItHint> Fixit = None); 2760 2761 protected: 2762 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, 2763 const char *startSpec, 2764 unsigned specifierLen, 2765 const char *csStart, unsigned csLen); 2766 2767 void HandlePositionalNonpositionalArgs(SourceLocation Loc, 2768 const char *startSpec, 2769 unsigned specifierLen); 2770 2771 SourceRange getFormatStringRange(); 2772 CharSourceRange getSpecifierRange(const char *startSpecifier, 2773 unsigned specifierLen); 2774 SourceLocation getLocationOfByte(const char *x); 2775 2776 const Expr *getDataArg(unsigned i) const; 2777 2778 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, 2779 const analyze_format_string::ConversionSpecifier &CS, 2780 const char *startSpecifier, unsigned specifierLen, 2781 unsigned argIndex); 2782 2783 template <typename Range> 2784 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, 2785 bool IsStringLocation, Range StringRange, 2786 ArrayRef<FixItHint> Fixit = None); 2787 }; 2788 } 2789 2790 SourceRange CheckFormatHandler::getFormatStringRange() { 2791 return OrigFormatExpr->getSourceRange(); 2792 } 2793 2794 CharSourceRange CheckFormatHandler:: 2795 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { 2796 SourceLocation Start = getLocationOfByte(startSpecifier); 2797 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); 2798 2799 // Advance the end SourceLocation by one due to half-open ranges. 2800 End = End.getLocWithOffset(1); 2801 2802 return CharSourceRange::getCharRange(Start, End); 2803 } 2804 2805 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { 2806 return S.getLocationOfStringLiteralByte(FExpr, x - Beg); 2807 } 2808 2809 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, 2810 unsigned specifierLen){ 2811 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), 2812 getLocationOfByte(startSpecifier), 2813 /*IsStringLocation*/true, 2814 getSpecifierRange(startSpecifier, specifierLen)); 2815 } 2816 2817 void CheckFormatHandler::HandleInvalidLengthModifier( 2818 const analyze_format_string::FormatSpecifier &FS, 2819 const analyze_format_string::ConversionSpecifier &CS, 2820 const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { 2821 using namespace analyze_format_string; 2822 2823 const LengthModifier &LM = FS.getLengthModifier(); 2824 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 2825 2826 // See if we know how to fix this length modifier. 2827 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 2828 if (FixedLM) { 2829 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 2830 getLocationOfByte(LM.getStart()), 2831 /*IsStringLocation*/true, 2832 getSpecifierRange(startSpecifier, specifierLen)); 2833 2834 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 2835 << FixedLM->toString() 2836 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 2837 2838 } else { 2839 FixItHint Hint; 2840 if (DiagID == diag::warn_format_nonsensical_length) 2841 Hint = FixItHint::CreateRemoval(LMRange); 2842 2843 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), 2844 getLocationOfByte(LM.getStart()), 2845 /*IsStringLocation*/true, 2846 getSpecifierRange(startSpecifier, specifierLen), 2847 Hint); 2848 } 2849 } 2850 2851 void CheckFormatHandler::HandleNonStandardLengthModifier( 2852 const analyze_format_string::FormatSpecifier &FS, 2853 const char *startSpecifier, unsigned specifierLen) { 2854 using namespace analyze_format_string; 2855 2856 const LengthModifier &LM = FS.getLengthModifier(); 2857 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); 2858 2859 // See if we know how to fix this length modifier. 2860 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); 2861 if (FixedLM) { 2862 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 2863 << LM.toString() << 0, 2864 getLocationOfByte(LM.getStart()), 2865 /*IsStringLocation*/true, 2866 getSpecifierRange(startSpecifier, specifierLen)); 2867 2868 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) 2869 << FixedLM->toString() 2870 << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); 2871 2872 } else { 2873 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 2874 << LM.toString() << 0, 2875 getLocationOfByte(LM.getStart()), 2876 /*IsStringLocation*/true, 2877 getSpecifierRange(startSpecifier, specifierLen)); 2878 } 2879 } 2880 2881 void CheckFormatHandler::HandleNonStandardConversionSpecifier( 2882 const analyze_format_string::ConversionSpecifier &CS, 2883 const char *startSpecifier, unsigned specifierLen) { 2884 using namespace analyze_format_string; 2885 2886 // See if we know how to fix this conversion specifier. 2887 Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); 2888 if (FixedCS) { 2889 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 2890 << CS.toString() << /*conversion specifier*/1, 2891 getLocationOfByte(CS.getStart()), 2892 /*IsStringLocation*/true, 2893 getSpecifierRange(startSpecifier, specifierLen)); 2894 2895 CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); 2896 S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) 2897 << FixedCS->toString() 2898 << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); 2899 } else { 2900 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) 2901 << CS.toString() << /*conversion specifier*/1, 2902 getLocationOfByte(CS.getStart()), 2903 /*IsStringLocation*/true, 2904 getSpecifierRange(startSpecifier, specifierLen)); 2905 } 2906 } 2907 2908 void CheckFormatHandler::HandlePosition(const char *startPos, 2909 unsigned posLen) { 2910 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), 2911 getLocationOfByte(startPos), 2912 /*IsStringLocation*/true, 2913 getSpecifierRange(startPos, posLen)); 2914 } 2915 2916 void 2917 CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, 2918 analyze_format_string::PositionContext p) { 2919 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) 2920 << (unsigned) p, 2921 getLocationOfByte(startPos), /*IsStringLocation*/true, 2922 getSpecifierRange(startPos, posLen)); 2923 } 2924 2925 void CheckFormatHandler::HandleZeroPosition(const char *startPos, 2926 unsigned posLen) { 2927 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), 2928 getLocationOfByte(startPos), 2929 /*IsStringLocation*/true, 2930 getSpecifierRange(startPos, posLen)); 2931 } 2932 2933 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { 2934 if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { 2935 // The presence of a null character is likely an error. 2936 EmitFormatDiagnostic( 2937 S.PDiag(diag::warn_printf_format_string_contains_null_char), 2938 getLocationOfByte(nullCharacter), /*IsStringLocation*/true, 2939 getFormatStringRange()); 2940 } 2941 } 2942 2943 // Note that this may return NULL if there was an error parsing or building 2944 // one of the argument expressions. 2945 const Expr *CheckFormatHandler::getDataArg(unsigned i) const { 2946 return Args[FirstDataArg + i]; 2947 } 2948 2949 void CheckFormatHandler::DoneProcessing() { 2950 // Does the number of data arguments exceed the number of 2951 // format conversions in the format string? 2952 if (!HasVAListArg) { 2953 // Find any arguments that weren't covered. 2954 CoveredArgs.flip(); 2955 signed notCoveredArg = CoveredArgs.find_first(); 2956 if (notCoveredArg >= 0) { 2957 assert((unsigned)notCoveredArg < NumDataArgs); 2958 if (const Expr *E = getDataArg((unsigned) notCoveredArg)) { 2959 SourceLocation Loc = E->getLocStart(); 2960 if (!S.getSourceManager().isInSystemMacro(Loc)) { 2961 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), 2962 Loc, /*IsStringLocation*/false, 2963 getFormatStringRange()); 2964 } 2965 } 2966 } 2967 } 2968 } 2969 2970 bool 2971 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, 2972 SourceLocation Loc, 2973 const char *startSpec, 2974 unsigned specifierLen, 2975 const char *csStart, 2976 unsigned csLen) { 2977 2978 bool keepGoing = true; 2979 if (argIndex < NumDataArgs) { 2980 // Consider the argument coverered, even though the specifier doesn't 2981 // make sense. 2982 CoveredArgs.set(argIndex); 2983 } 2984 else { 2985 // If argIndex exceeds the number of data arguments we 2986 // don't issue a warning because that is just a cascade of warnings (and 2987 // they may have intended '%%' anyway). We don't want to continue processing 2988 // the format string after this point, however, as we will like just get 2989 // gibberish when trying to match arguments. 2990 keepGoing = false; 2991 } 2992 2993 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) 2994 << StringRef(csStart, csLen), 2995 Loc, /*IsStringLocation*/true, 2996 getSpecifierRange(startSpec, specifierLen)); 2997 2998 return keepGoing; 2999 } 3000 3001 void 3002 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, 3003 const char *startSpec, 3004 unsigned specifierLen) { 3005 EmitFormatDiagnostic( 3006 S.PDiag(diag::warn_format_mix_positional_nonpositional_args), 3007 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); 3008 } 3009 3010 bool 3011 CheckFormatHandler::CheckNumArgs( 3012 const analyze_format_string::FormatSpecifier &FS, 3013 const analyze_format_string::ConversionSpecifier &CS, 3014 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { 3015 3016 if (argIndex >= NumDataArgs) { 3017 PartialDiagnostic PDiag = FS.usesPositionalArg() 3018 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) 3019 << (argIndex+1) << NumDataArgs) 3020 : S.PDiag(diag::warn_printf_insufficient_data_args); 3021 EmitFormatDiagnostic( 3022 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, 3023 getSpecifierRange(startSpecifier, specifierLen)); 3024 return false; 3025 } 3026 return true; 3027 } 3028 3029 template<typename Range> 3030 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, 3031 SourceLocation Loc, 3032 bool IsStringLocation, 3033 Range StringRange, 3034 ArrayRef<FixItHint> FixIt) { 3035 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, 3036 Loc, IsStringLocation, StringRange, FixIt); 3037 } 3038 3039 /// \brief If the format string is not within the funcion call, emit a note 3040 /// so that the function call and string are in diagnostic messages. 3041 /// 3042 /// \param InFunctionCall if true, the format string is within the function 3043 /// call and only one diagnostic message will be produced. Otherwise, an 3044 /// extra note will be emitted pointing to location of the format string. 3045 /// 3046 /// \param ArgumentExpr the expression that is passed as the format string 3047 /// argument in the function call. Used for getting locations when two 3048 /// diagnostics are emitted. 3049 /// 3050 /// \param PDiag the callee should already have provided any strings for the 3051 /// diagnostic message. This function only adds locations and fixits 3052 /// to diagnostics. 3053 /// 3054 /// \param Loc primary location for diagnostic. If two diagnostics are 3055 /// required, one will be at Loc and a new SourceLocation will be created for 3056 /// the other one. 3057 /// 3058 /// \param IsStringLocation if true, Loc points to the format string should be 3059 /// used for the note. Otherwise, Loc points to the argument list and will 3060 /// be used with PDiag. 3061 /// 3062 /// \param StringRange some or all of the string to highlight. This is 3063 /// templated so it can accept either a CharSourceRange or a SourceRange. 3064 /// 3065 /// \param FixIt optional fix it hint for the format string. 3066 template<typename Range> 3067 void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, 3068 const Expr *ArgumentExpr, 3069 PartialDiagnostic PDiag, 3070 SourceLocation Loc, 3071 bool IsStringLocation, 3072 Range StringRange, 3073 ArrayRef<FixItHint> FixIt) { 3074 if (InFunctionCall) { 3075 const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); 3076 D << StringRange; 3077 for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end(); 3078 I != E; ++I) { 3079 D << *I; 3080 } 3081 } else { 3082 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) 3083 << ArgumentExpr->getSourceRange(); 3084 3085 const Sema::SemaDiagnosticBuilder &Note = 3086 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), 3087 diag::note_format_string_defined); 3088 3089 Note << StringRange; 3090 for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end(); 3091 I != E; ++I) { 3092 Note << *I; 3093 } 3094 } 3095 } 3096 3097 //===--- CHECK: Printf format string checking ------------------------------===// 3098 3099 namespace { 3100 class CheckPrintfHandler : public CheckFormatHandler { 3101 bool ObjCContext; 3102 public: 3103 CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, 3104 const Expr *origFormatExpr, unsigned firstDataArg, 3105 unsigned numDataArgs, bool isObjC, 3106 const char *beg, bool hasVAListArg, 3107 ArrayRef<const Expr *> Args, 3108 unsigned formatIdx, bool inFunctionCall, 3109 Sema::VariadicCallType CallType, 3110 llvm::SmallBitVector &CheckedVarArgs) 3111 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, 3112 numDataArgs, beg, hasVAListArg, Args, 3113 formatIdx, inFunctionCall, CallType, CheckedVarArgs), 3114 ObjCContext(isObjC) 3115 {} 3116 3117 3118 bool HandleInvalidPrintfConversionSpecifier( 3119 const analyze_printf::PrintfSpecifier &FS, 3120 const char *startSpecifier, 3121 unsigned specifierLen) override; 3122 3123 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, 3124 const char *startSpecifier, 3125 unsigned specifierLen) override; 3126 bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 3127 const char *StartSpecifier, 3128 unsigned SpecifierLen, 3129 const Expr *E); 3130 3131 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, 3132 const char *startSpecifier, unsigned specifierLen); 3133 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, 3134 const analyze_printf::OptionalAmount &Amt, 3135 unsigned type, 3136 const char *startSpecifier, unsigned specifierLen); 3137 void HandleFlag(const analyze_printf::PrintfSpecifier &FS, 3138 const analyze_printf::OptionalFlag &flag, 3139 const char *startSpecifier, unsigned specifierLen); 3140 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, 3141 const analyze_printf::OptionalFlag &ignoredFlag, 3142 const analyze_printf::OptionalFlag &flag, 3143 const char *startSpecifier, unsigned specifierLen); 3144 bool checkForCStrMembers(const analyze_printf::ArgType &AT, 3145 const Expr *E); 3146 3147 }; 3148 } 3149 3150 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( 3151 const analyze_printf::PrintfSpecifier &FS, 3152 const char *startSpecifier, 3153 unsigned specifierLen) { 3154 const analyze_printf::PrintfConversionSpecifier &CS = 3155 FS.getConversionSpecifier(); 3156 3157 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 3158 getLocationOfByte(CS.getStart()), 3159 startSpecifier, specifierLen, 3160 CS.getStart(), CS.getLength()); 3161 } 3162 3163 bool CheckPrintfHandler::HandleAmount( 3164 const analyze_format_string::OptionalAmount &Amt, 3165 unsigned k, const char *startSpecifier, 3166 unsigned specifierLen) { 3167 3168 if (Amt.hasDataArgument()) { 3169 if (!HasVAListArg) { 3170 unsigned argIndex = Amt.getArgIndex(); 3171 if (argIndex >= NumDataArgs) { 3172 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) 3173 << k, 3174 getLocationOfByte(Amt.getStart()), 3175 /*IsStringLocation*/true, 3176 getSpecifierRange(startSpecifier, specifierLen)); 3177 // Don't do any more checking. We will just emit 3178 // spurious errors. 3179 return false; 3180 } 3181 3182 // Type check the data argument. It should be an 'int'. 3183 // Although not in conformance with C99, we also allow the argument to be 3184 // an 'unsigned int' as that is a reasonably safe case. GCC also 3185 // doesn't emit a warning for that case. 3186 CoveredArgs.set(argIndex); 3187 const Expr *Arg = getDataArg(argIndex); 3188 if (!Arg) 3189 return false; 3190 3191 QualType T = Arg->getType(); 3192 3193 const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); 3194 assert(AT.isValid()); 3195 3196 if (!AT.matchesType(S.Context, T)) { 3197 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) 3198 << k << AT.getRepresentativeTypeName(S.Context) 3199 << T << Arg->getSourceRange(), 3200 getLocationOfByte(Amt.getStart()), 3201 /*IsStringLocation*/true, 3202 getSpecifierRange(startSpecifier, specifierLen)); 3203 // Don't do any more checking. We will just emit 3204 // spurious errors. 3205 return false; 3206 } 3207 } 3208 } 3209 return true; 3210 } 3211 3212 void CheckPrintfHandler::HandleInvalidAmount( 3213 const analyze_printf::PrintfSpecifier &FS, 3214 const analyze_printf::OptionalAmount &Amt, 3215 unsigned type, 3216 const char *startSpecifier, 3217 unsigned specifierLen) { 3218 const analyze_printf::PrintfConversionSpecifier &CS = 3219 FS.getConversionSpecifier(); 3220 3221 FixItHint fixit = 3222 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant 3223 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), 3224 Amt.getConstantLength())) 3225 : FixItHint(); 3226 3227 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) 3228 << type << CS.toString(), 3229 getLocationOfByte(Amt.getStart()), 3230 /*IsStringLocation*/true, 3231 getSpecifierRange(startSpecifier, specifierLen), 3232 fixit); 3233 } 3234 3235 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, 3236 const analyze_printf::OptionalFlag &flag, 3237 const char *startSpecifier, 3238 unsigned specifierLen) { 3239 // Warn about pointless flag with a fixit removal. 3240 const analyze_printf::PrintfConversionSpecifier &CS = 3241 FS.getConversionSpecifier(); 3242 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) 3243 << flag.toString() << CS.toString(), 3244 getLocationOfByte(flag.getPosition()), 3245 /*IsStringLocation*/true, 3246 getSpecifierRange(startSpecifier, specifierLen), 3247 FixItHint::CreateRemoval( 3248 getSpecifierRange(flag.getPosition(), 1))); 3249 } 3250 3251 void CheckPrintfHandler::HandleIgnoredFlag( 3252 const analyze_printf::PrintfSpecifier &FS, 3253 const analyze_printf::OptionalFlag &ignoredFlag, 3254 const analyze_printf::OptionalFlag &flag, 3255 const char *startSpecifier, 3256 unsigned specifierLen) { 3257 // Warn about ignored flag with a fixit removal. 3258 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) 3259 << ignoredFlag.toString() << flag.toString(), 3260 getLocationOfByte(ignoredFlag.getPosition()), 3261 /*IsStringLocation*/true, 3262 getSpecifierRange(startSpecifier, specifierLen), 3263 FixItHint::CreateRemoval( 3264 getSpecifierRange(ignoredFlag.getPosition(), 1))); 3265 } 3266 3267 // Determines if the specified is a C++ class or struct containing 3268 // a member with the specified name and kind (e.g. a CXXMethodDecl named 3269 // "c_str()"). 3270 template<typename MemberKind> 3271 static llvm::SmallPtrSet<MemberKind*, 1> 3272 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { 3273 const RecordType *RT = Ty->getAs<RecordType>(); 3274 llvm::SmallPtrSet<MemberKind*, 1> Results; 3275 3276 if (!RT) 3277 return Results; 3278 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 3279 if (!RD || !RD->getDefinition()) 3280 return Results; 3281 3282 LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(), 3283 Sema::LookupMemberName); 3284 R.suppressDiagnostics(); 3285 3286 // We just need to include all members of the right kind turned up by the 3287 // filter, at this point. 3288 if (S.LookupQualifiedName(R, RT->getDecl())) 3289 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 3290 NamedDecl *decl = (*I)->getUnderlyingDecl(); 3291 if (MemberKind *FK = dyn_cast<MemberKind>(decl)) 3292 Results.insert(FK); 3293 } 3294 return Results; 3295 } 3296 3297 /// Check if we could call '.c_str()' on an object. 3298 /// 3299 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't 3300 /// allow the call, or if it would be ambiguous). 3301 bool Sema::hasCStrMethod(const Expr *E) { 3302 typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; 3303 MethodSet Results = 3304 CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType()); 3305 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 3306 MI != ME; ++MI) 3307 if ((*MI)->getMinRequiredArguments() == 0) 3308 return true; 3309 return false; 3310 } 3311 3312 // Check if a (w)string was passed when a (w)char* was needed, and offer a 3313 // better diagnostic if so. AT is assumed to be valid. 3314 // Returns true when a c_str() conversion method is found. 3315 bool CheckPrintfHandler::checkForCStrMembers( 3316 const analyze_printf::ArgType &AT, const Expr *E) { 3317 typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; 3318 3319 MethodSet Results = 3320 CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); 3321 3322 for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); 3323 MI != ME; ++MI) { 3324 const CXXMethodDecl *Method = *MI; 3325 if (Method->getMinRequiredArguments() == 0 && 3326 AT.matchesType(S.Context, Method->getReturnType())) { 3327 // FIXME: Suggest parens if the expression needs them. 3328 SourceLocation EndLoc = S.getLocForEndOfToken(E->getLocEnd()); 3329 S.Diag(E->getLocStart(), diag::note_printf_c_str) 3330 << "c_str()" 3331 << FixItHint::CreateInsertion(EndLoc, ".c_str()"); 3332 return true; 3333 } 3334 } 3335 3336 return false; 3337 } 3338 3339 bool 3340 CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier 3341 &FS, 3342 const char *startSpecifier, 3343 unsigned specifierLen) { 3344 3345 using namespace analyze_format_string; 3346 using namespace analyze_printf; 3347 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); 3348 3349 if (FS.consumesDataArgument()) { 3350 if (atFirstArg) { 3351 atFirstArg = false; 3352 usesPositionalArgs = FS.usesPositionalArg(); 3353 } 3354 else if (usesPositionalArgs != FS.usesPositionalArg()) { 3355 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 3356 startSpecifier, specifierLen); 3357 return false; 3358 } 3359 } 3360 3361 // First check if the field width, precision, and conversion specifier 3362 // have matching data arguments. 3363 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, 3364 startSpecifier, specifierLen)) { 3365 return false; 3366 } 3367 3368 if (!HandleAmount(FS.getPrecision(), /* precision */ 1, 3369 startSpecifier, specifierLen)) { 3370 return false; 3371 } 3372 3373 if (!CS.consumesDataArgument()) { 3374 // FIXME: Technically specifying a precision or field width here 3375 // makes no sense. Worth issuing a warning at some point. 3376 return true; 3377 } 3378 3379 // Consume the argument. 3380 unsigned argIndex = FS.getArgIndex(); 3381 if (argIndex < NumDataArgs) { 3382 // The check to see if the argIndex is valid will come later. 3383 // We set the bit here because we may exit early from this 3384 // function if we encounter some other error. 3385 CoveredArgs.set(argIndex); 3386 } 3387 3388 // FreeBSD kernel extensions. 3389 if (CS.getKind() == ConversionSpecifier::FreeBSDbArg || 3390 CS.getKind() == ConversionSpecifier::FreeBSDDArg) { 3391 // We need at least two arguments. 3392 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1)) 3393 return false; 3394 3395 // Claim the second argument. 3396 CoveredArgs.set(argIndex + 1); 3397 3398 // Type check the first argument (int for %b, pointer for %D) 3399 const Expr *Ex = getDataArg(argIndex); 3400 const analyze_printf::ArgType &AT = 3401 (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? 3402 ArgType(S.Context.IntTy) : ArgType::CPointerTy; 3403 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) 3404 EmitFormatDiagnostic( 3405 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 3406 << AT.getRepresentativeTypeName(S.Context) << Ex->getType() 3407 << false << Ex->getSourceRange(), 3408 Ex->getLocStart(), /*IsStringLocation*/false, 3409 getSpecifierRange(startSpecifier, specifierLen)); 3410 3411 // Type check the second argument (char * for both %b and %D) 3412 Ex = getDataArg(argIndex + 1); 3413 const analyze_printf::ArgType &AT2 = ArgType::CStrTy; 3414 if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType())) 3415 EmitFormatDiagnostic( 3416 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 3417 << AT2.getRepresentativeTypeName(S.Context) << Ex->getType() 3418 << false << Ex->getSourceRange(), 3419 Ex->getLocStart(), /*IsStringLocation*/false, 3420 getSpecifierRange(startSpecifier, specifierLen)); 3421 3422 return true; 3423 } 3424 3425 // Check for using an Objective-C specific conversion specifier 3426 // in a non-ObjC literal. 3427 if (!ObjCContext && CS.isObjCArg()) { 3428 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, 3429 specifierLen); 3430 } 3431 3432 // Check for invalid use of field width 3433 if (!FS.hasValidFieldWidth()) { 3434 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, 3435 startSpecifier, specifierLen); 3436 } 3437 3438 // Check for invalid use of precision 3439 if (!FS.hasValidPrecision()) { 3440 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, 3441 startSpecifier, specifierLen); 3442 } 3443 3444 // Check each flag does not conflict with any other component. 3445 if (!FS.hasValidThousandsGroupingPrefix()) 3446 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); 3447 if (!FS.hasValidLeadingZeros()) 3448 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); 3449 if (!FS.hasValidPlusPrefix()) 3450 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); 3451 if (!FS.hasValidSpacePrefix()) 3452 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); 3453 if (!FS.hasValidAlternativeForm()) 3454 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); 3455 if (!FS.hasValidLeftJustified()) 3456 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); 3457 3458 // Check that flags are not ignored by another flag 3459 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' 3460 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), 3461 startSpecifier, specifierLen); 3462 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' 3463 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), 3464 startSpecifier, specifierLen); 3465 3466 // Check the length modifier is valid with the given conversion specifier. 3467 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) 3468 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 3469 diag::warn_format_nonsensical_length); 3470 else if (!FS.hasStandardLengthModifier()) 3471 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 3472 else if (!FS.hasStandardLengthConversionCombination()) 3473 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 3474 diag::warn_format_non_standard_conversion_spec); 3475 3476 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 3477 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 3478 3479 // The remaining checks depend on the data arguments. 3480 if (HasVAListArg) 3481 return true; 3482 3483 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 3484 return false; 3485 3486 const Expr *Arg = getDataArg(argIndex); 3487 if (!Arg) 3488 return true; 3489 3490 return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); 3491 } 3492 3493 static bool requiresParensToAddCast(const Expr *E) { 3494 // FIXME: We should have a general way to reason about operator 3495 // precedence and whether parens are actually needed here. 3496 // Take care of a few common cases where they aren't. 3497 const Expr *Inside = E->IgnoreImpCasts(); 3498 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) 3499 Inside = POE->getSyntacticForm()->IgnoreImpCasts(); 3500 3501 switch (Inside->getStmtClass()) { 3502 case Stmt::ArraySubscriptExprClass: 3503 case Stmt::CallExprClass: 3504 case Stmt::CharacterLiteralClass: 3505 case Stmt::CXXBoolLiteralExprClass: 3506 case Stmt::DeclRefExprClass: 3507 case Stmt::FloatingLiteralClass: 3508 case Stmt::IntegerLiteralClass: 3509 case Stmt::MemberExprClass: 3510 case Stmt::ObjCArrayLiteralClass: 3511 case Stmt::ObjCBoolLiteralExprClass: 3512 case Stmt::ObjCBoxedExprClass: 3513 case Stmt::ObjCDictionaryLiteralClass: 3514 case Stmt::ObjCEncodeExprClass: 3515 case Stmt::ObjCIvarRefExprClass: 3516 case Stmt::ObjCMessageExprClass: 3517 case Stmt::ObjCPropertyRefExprClass: 3518 case Stmt::ObjCStringLiteralClass: 3519 case Stmt::ObjCSubscriptRefExprClass: 3520 case Stmt::ParenExprClass: 3521 case Stmt::StringLiteralClass: 3522 case Stmt::UnaryOperatorClass: 3523 return false; 3524 default: 3525 return true; 3526 } 3527 } 3528 3529 static std::pair<QualType, StringRef> 3530 shouldNotPrintDirectly(const ASTContext &Context, 3531 QualType IntendedTy, 3532 const Expr *E) { 3533 // Use a 'while' to peel off layers of typedefs. 3534 QualType TyTy = IntendedTy; 3535 while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) { 3536 StringRef Name = UserTy->getDecl()->getName(); 3537 QualType CastTy = llvm::StringSwitch<QualType>(Name) 3538 .Case("NSInteger", Context.LongTy) 3539 .Case("NSUInteger", Context.UnsignedLongTy) 3540 .Case("SInt32", Context.IntTy) 3541 .Case("UInt32", Context.UnsignedIntTy) 3542 .Default(QualType()); 3543 3544 if (!CastTy.isNull()) 3545 return std::make_pair(CastTy, Name); 3546 3547 TyTy = UserTy->desugar(); 3548 } 3549 3550 // Strip parens if necessary. 3551 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 3552 return shouldNotPrintDirectly(Context, 3553 PE->getSubExpr()->getType(), 3554 PE->getSubExpr()); 3555 3556 // If this is a conditional expression, then its result type is constructed 3557 // via usual arithmetic conversions and thus there might be no necessary 3558 // typedef sugar there. Recurse to operands to check for NSInteger & 3559 // Co. usage condition. 3560 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 3561 QualType TrueTy, FalseTy; 3562 StringRef TrueName, FalseName; 3563 3564 std::tie(TrueTy, TrueName) = 3565 shouldNotPrintDirectly(Context, 3566 CO->getTrueExpr()->getType(), 3567 CO->getTrueExpr()); 3568 std::tie(FalseTy, FalseName) = 3569 shouldNotPrintDirectly(Context, 3570 CO->getFalseExpr()->getType(), 3571 CO->getFalseExpr()); 3572 3573 if (TrueTy == FalseTy) 3574 return std::make_pair(TrueTy, TrueName); 3575 else if (TrueTy.isNull()) 3576 return std::make_pair(FalseTy, FalseName); 3577 else if (FalseTy.isNull()) 3578 return std::make_pair(TrueTy, TrueName); 3579 } 3580 3581 return std::make_pair(QualType(), StringRef()); 3582 } 3583 3584 bool 3585 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, 3586 const char *StartSpecifier, 3587 unsigned SpecifierLen, 3588 const Expr *E) { 3589 using namespace analyze_format_string; 3590 using namespace analyze_printf; 3591 // Now type check the data expression that matches the 3592 // format specifier. 3593 const analyze_printf::ArgType &AT = FS.getArgType(S.Context, 3594 ObjCContext); 3595 if (!AT.isValid()) 3596 return true; 3597 3598 QualType ExprTy = E->getType(); 3599 while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) { 3600 ExprTy = TET->getUnderlyingExpr()->getType(); 3601 } 3602 3603 if (AT.matchesType(S.Context, ExprTy)) 3604 return true; 3605 3606 // Look through argument promotions for our error message's reported type. 3607 // This includes the integral and floating promotions, but excludes array 3608 // and function pointer decay; seeing that an argument intended to be a 3609 // string has type 'char [6]' is probably more confusing than 'char *'. 3610 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 3611 if (ICE->getCastKind() == CK_IntegralCast || 3612 ICE->getCastKind() == CK_FloatingCast) { 3613 E = ICE->getSubExpr(); 3614 ExprTy = E->getType(); 3615 3616 // Check if we didn't match because of an implicit cast from a 'char' 3617 // or 'short' to an 'int'. This is done because printf is a varargs 3618 // function. 3619 if (ICE->getType() == S.Context.IntTy || 3620 ICE->getType() == S.Context.UnsignedIntTy) { 3621 // All further checking is done on the subexpression. 3622 if (AT.matchesType(S.Context, ExprTy)) 3623 return true; 3624 } 3625 } 3626 } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { 3627 // Special case for 'a', which has type 'int' in C. 3628 // Note, however, that we do /not/ want to treat multibyte constants like 3629 // 'MooV' as characters! This form is deprecated but still exists. 3630 if (ExprTy == S.Context.IntTy) 3631 if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) 3632 ExprTy = S.Context.CharTy; 3633 } 3634 3635 // Look through enums to their underlying type. 3636 bool IsEnum = false; 3637 if (auto EnumTy = ExprTy->getAs<EnumType>()) { 3638 ExprTy = EnumTy->getDecl()->getIntegerType(); 3639 IsEnum = true; 3640 } 3641 3642 // %C in an Objective-C context prints a unichar, not a wchar_t. 3643 // If the argument is an integer of some kind, believe the %C and suggest 3644 // a cast instead of changing the conversion specifier. 3645 QualType IntendedTy = ExprTy; 3646 if (ObjCContext && 3647 FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) { 3648 if (ExprTy->isIntegralOrUnscopedEnumerationType() && 3649 !ExprTy->isCharType()) { 3650 // 'unichar' is defined as a typedef of unsigned short, but we should 3651 // prefer using the typedef if it is visible. 3652 IntendedTy = S.Context.UnsignedShortTy; 3653 3654 // While we are here, check if the value is an IntegerLiteral that happens 3655 // to be within the valid range. 3656 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) { 3657 const llvm::APInt &V = IL->getValue(); 3658 if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy)) 3659 return true; 3660 } 3661 3662 LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(), 3663 Sema::LookupOrdinaryName); 3664 if (S.LookupName(Result, S.getCurScope())) { 3665 NamedDecl *ND = Result.getFoundDecl(); 3666 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND)) 3667 if (TD->getUnderlyingType() == IntendedTy) 3668 IntendedTy = S.Context.getTypedefType(TD); 3669 } 3670 } 3671 } 3672 3673 // Special-case some of Darwin's platform-independence types by suggesting 3674 // casts to primitive types that are known to be large enough. 3675 bool ShouldNotPrintDirectly = false; StringRef CastTyName; 3676 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { 3677 QualType CastTy; 3678 std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E); 3679 if (!CastTy.isNull()) { 3680 IntendedTy = CastTy; 3681 ShouldNotPrintDirectly = true; 3682 } 3683 } 3684 3685 // We may be able to offer a FixItHint if it is a supported type. 3686 PrintfSpecifier fixedFS = FS; 3687 bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(), 3688 S.Context, ObjCContext); 3689 3690 if (success) { 3691 // Get the fix string from the fixed format specifier 3692 SmallString<16> buf; 3693 llvm::raw_svector_ostream os(buf); 3694 fixedFS.toString(os); 3695 3696 CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); 3697 3698 if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) { 3699 // In this case, the specifier is wrong and should be changed to match 3700 // the argument. 3701 EmitFormatDiagnostic( 3702 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 3703 << AT.getRepresentativeTypeName(S.Context) << IntendedTy << IsEnum 3704 << E->getSourceRange(), 3705 E->getLocStart(), 3706 /*IsStringLocation*/false, 3707 SpecRange, 3708 FixItHint::CreateReplacement(SpecRange, os.str())); 3709 3710 } else { 3711 // The canonical type for formatting this value is different from the 3712 // actual type of the expression. (This occurs, for example, with Darwin's 3713 // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but 3714 // should be printed as 'long' for 64-bit compatibility.) 3715 // Rather than emitting a normal format/argument mismatch, we want to 3716 // add a cast to the recommended type (and correct the format string 3717 // if necessary). 3718 SmallString<16> CastBuf; 3719 llvm::raw_svector_ostream CastFix(CastBuf); 3720 CastFix << "("; 3721 IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); 3722 CastFix << ")"; 3723 3724 SmallVector<FixItHint,4> Hints; 3725 if (!AT.matchesType(S.Context, IntendedTy)) 3726 Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); 3727 3728 if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { 3729 // If there's already a cast present, just replace it. 3730 SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); 3731 Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); 3732 3733 } else if (!requiresParensToAddCast(E)) { 3734 // If the expression has high enough precedence, 3735 // just write the C-style cast. 3736 Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), 3737 CastFix.str())); 3738 } else { 3739 // Otherwise, add parens around the expression as well as the cast. 3740 CastFix << "("; 3741 Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), 3742 CastFix.str())); 3743 3744 SourceLocation After = S.getLocForEndOfToken(E->getLocEnd()); 3745 Hints.push_back(FixItHint::CreateInsertion(After, ")")); 3746 } 3747 3748 if (ShouldNotPrintDirectly) { 3749 // The expression has a type that should not be printed directly. 3750 // We extract the name from the typedef because we don't want to show 3751 // the underlying type in the diagnostic. 3752 StringRef Name; 3753 if (const TypedefType *TypedefTy = dyn_cast<TypedefType>(ExprTy)) 3754 Name = TypedefTy->getDecl()->getName(); 3755 else 3756 Name = CastTyName; 3757 EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast) 3758 << Name << IntendedTy << IsEnum 3759 << E->getSourceRange(), 3760 E->getLocStart(), /*IsStringLocation=*/false, 3761 SpecRange, Hints); 3762 } else { 3763 // In this case, the expression could be printed using a different 3764 // specifier, but we've decided that the specifier is probably correct 3765 // and we should cast instead. Just use the normal warning message. 3766 EmitFormatDiagnostic( 3767 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 3768 << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum 3769 << E->getSourceRange(), 3770 E->getLocStart(), /*IsStringLocation*/false, 3771 SpecRange, Hints); 3772 } 3773 } 3774 } else { 3775 const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, 3776 SpecifierLen); 3777 // Since the warning for passing non-POD types to variadic functions 3778 // was deferred until now, we emit a warning for non-POD 3779 // arguments here. 3780 switch (S.isValidVarArgType(ExprTy)) { 3781 case Sema::VAK_Valid: 3782 case Sema::VAK_ValidInCXX11: 3783 EmitFormatDiagnostic( 3784 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 3785 << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum 3786 << CSR 3787 << E->getSourceRange(), 3788 E->getLocStart(), /*IsStringLocation*/false, CSR); 3789 break; 3790 3791 case Sema::VAK_Undefined: 3792 case Sema::VAK_MSVCUndefined: 3793 EmitFormatDiagnostic( 3794 S.PDiag(diag::warn_non_pod_vararg_with_format_string) 3795 << S.getLangOpts().CPlusPlus11 3796 << ExprTy 3797 << CallType 3798 << AT.getRepresentativeTypeName(S.Context) 3799 << CSR 3800 << E->getSourceRange(), 3801 E->getLocStart(), /*IsStringLocation*/false, CSR); 3802 checkForCStrMembers(AT, E); 3803 break; 3804 3805 case Sema::VAK_Invalid: 3806 if (ExprTy->isObjCObjectType()) 3807 EmitFormatDiagnostic( 3808 S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format) 3809 << S.getLangOpts().CPlusPlus11 3810 << ExprTy 3811 << CallType 3812 << AT.getRepresentativeTypeName(S.Context) 3813 << CSR 3814 << E->getSourceRange(), 3815 E->getLocStart(), /*IsStringLocation*/false, CSR); 3816 else 3817 // FIXME: If this is an initializer list, suggest removing the braces 3818 // or inserting a cast to the target type. 3819 S.Diag(E->getLocStart(), diag::err_cannot_pass_to_vararg_format) 3820 << isa<InitListExpr>(E) << ExprTy << CallType 3821 << AT.getRepresentativeTypeName(S.Context) 3822 << E->getSourceRange(); 3823 break; 3824 } 3825 3826 assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() && 3827 "format string specifier index out of range"); 3828 CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true; 3829 } 3830 3831 return true; 3832 } 3833 3834 //===--- CHECK: Scanf format string checking ------------------------------===// 3835 3836 namespace { 3837 class CheckScanfHandler : public CheckFormatHandler { 3838 public: 3839 CheckScanfHandler(Sema &s, const StringLiteral *fexpr, 3840 const Expr *origFormatExpr, unsigned firstDataArg, 3841 unsigned numDataArgs, const char *beg, bool hasVAListArg, 3842 ArrayRef<const Expr *> Args, 3843 unsigned formatIdx, bool inFunctionCall, 3844 Sema::VariadicCallType CallType, 3845 llvm::SmallBitVector &CheckedVarArgs) 3846 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, 3847 numDataArgs, beg, hasVAListArg, 3848 Args, formatIdx, inFunctionCall, CallType, 3849 CheckedVarArgs) 3850 {} 3851 3852 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, 3853 const char *startSpecifier, 3854 unsigned specifierLen) override; 3855 3856 bool HandleInvalidScanfConversionSpecifier( 3857 const analyze_scanf::ScanfSpecifier &FS, 3858 const char *startSpecifier, 3859 unsigned specifierLen) override; 3860 3861 void HandleIncompleteScanList(const char *start, const char *end) override; 3862 }; 3863 } 3864 3865 void CheckScanfHandler::HandleIncompleteScanList(const char *start, 3866 const char *end) { 3867 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), 3868 getLocationOfByte(end), /*IsStringLocation*/true, 3869 getSpecifierRange(start, end - start)); 3870 } 3871 3872 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( 3873 const analyze_scanf::ScanfSpecifier &FS, 3874 const char *startSpecifier, 3875 unsigned specifierLen) { 3876 3877 const analyze_scanf::ScanfConversionSpecifier &CS = 3878 FS.getConversionSpecifier(); 3879 3880 return HandleInvalidConversionSpecifier(FS.getArgIndex(), 3881 getLocationOfByte(CS.getStart()), 3882 startSpecifier, specifierLen, 3883 CS.getStart(), CS.getLength()); 3884 } 3885 3886 bool CheckScanfHandler::HandleScanfSpecifier( 3887 const analyze_scanf::ScanfSpecifier &FS, 3888 const char *startSpecifier, 3889 unsigned specifierLen) { 3890 3891 using namespace analyze_scanf; 3892 using namespace analyze_format_string; 3893 3894 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); 3895 3896 // Handle case where '%' and '*' don't consume an argument. These shouldn't 3897 // be used to decide if we are using positional arguments consistently. 3898 if (FS.consumesDataArgument()) { 3899 if (atFirstArg) { 3900 atFirstArg = false; 3901 usesPositionalArgs = FS.usesPositionalArg(); 3902 } 3903 else if (usesPositionalArgs != FS.usesPositionalArg()) { 3904 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), 3905 startSpecifier, specifierLen); 3906 return false; 3907 } 3908 } 3909 3910 // Check if the field with is non-zero. 3911 const OptionalAmount &Amt = FS.getFieldWidth(); 3912 if (Amt.getHowSpecified() == OptionalAmount::Constant) { 3913 if (Amt.getConstantAmount() == 0) { 3914 const CharSourceRange &R = getSpecifierRange(Amt.getStart(), 3915 Amt.getConstantLength()); 3916 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), 3917 getLocationOfByte(Amt.getStart()), 3918 /*IsStringLocation*/true, R, 3919 FixItHint::CreateRemoval(R)); 3920 } 3921 } 3922 3923 if (!FS.consumesDataArgument()) { 3924 // FIXME: Technically specifying a precision or field width here 3925 // makes no sense. Worth issuing a warning at some point. 3926 return true; 3927 } 3928 3929 // Consume the argument. 3930 unsigned argIndex = FS.getArgIndex(); 3931 if (argIndex < NumDataArgs) { 3932 // The check to see if the argIndex is valid will come later. 3933 // We set the bit here because we may exit early from this 3934 // function if we encounter some other error. 3935 CoveredArgs.set(argIndex); 3936 } 3937 3938 // Check the length modifier is valid with the given conversion specifier. 3939 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) 3940 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 3941 diag::warn_format_nonsensical_length); 3942 else if (!FS.hasStandardLengthModifier()) 3943 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); 3944 else if (!FS.hasStandardLengthConversionCombination()) 3945 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, 3946 diag::warn_format_non_standard_conversion_spec); 3947 3948 if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) 3949 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); 3950 3951 // The remaining checks depend on the data arguments. 3952 if (HasVAListArg) 3953 return true; 3954 3955 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) 3956 return false; 3957 3958 // Check that the argument type matches the format specifier. 3959 const Expr *Ex = getDataArg(argIndex); 3960 if (!Ex) 3961 return true; 3962 3963 const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); 3964 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) { 3965 ScanfSpecifier fixedFS = FS; 3966 bool success = fixedFS.fixType(Ex->getType(), 3967 Ex->IgnoreImpCasts()->getType(), 3968 S.getLangOpts(), S.Context); 3969 3970 if (success) { 3971 // Get the fix string from the fixed format specifier. 3972 SmallString<128> buf; 3973 llvm::raw_svector_ostream os(buf); 3974 fixedFS.toString(os); 3975 3976 EmitFormatDiagnostic( 3977 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 3978 << AT.getRepresentativeTypeName(S.Context) << Ex->getType() << false 3979 << Ex->getSourceRange(), 3980 Ex->getLocStart(), 3981 /*IsStringLocation*/false, 3982 getSpecifierRange(startSpecifier, specifierLen), 3983 FixItHint::CreateReplacement( 3984 getSpecifierRange(startSpecifier, specifierLen), 3985 os.str())); 3986 } else { 3987 EmitFormatDiagnostic( 3988 S.PDiag(diag::warn_format_conversion_argument_type_mismatch) 3989 << AT.getRepresentativeTypeName(S.Context) << Ex->getType() << false 3990 << Ex->getSourceRange(), 3991 Ex->getLocStart(), 3992 /*IsStringLocation*/false, 3993 getSpecifierRange(startSpecifier, specifierLen)); 3994 } 3995 } 3996 3997 return true; 3998 } 3999 4000 void Sema::CheckFormatString(const StringLiteral *FExpr, 4001 const Expr *OrigFormatExpr, 4002 ArrayRef<const Expr *> Args, 4003 bool HasVAListArg, unsigned format_idx, 4004 unsigned firstDataArg, FormatStringType Type, 4005 bool inFunctionCall, VariadicCallType CallType, 4006 llvm::SmallBitVector &CheckedVarArgs) { 4007 4008 // CHECK: is the format string a wide literal? 4009 if (!FExpr->isAscii() && !FExpr->isUTF8()) { 4010 CheckFormatHandler::EmitFormatDiagnostic( 4011 *this, inFunctionCall, Args[format_idx], 4012 PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), 4013 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); 4014 return; 4015 } 4016 4017 // Str - The format string. NOTE: this is NOT null-terminated! 4018 StringRef StrRef = FExpr->getString(); 4019 const char *Str = StrRef.data(); 4020 // Account for cases where the string literal is truncated in a declaration. 4021 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); 4022 assert(T && "String literal not of constant array type!"); 4023 size_t TypeSize = T->getSize().getZExtValue(); 4024 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 4025 const unsigned numDataArgs = Args.size() - firstDataArg; 4026 4027 // Emit a warning if the string literal is truncated and does not contain an 4028 // embedded null character. 4029 if (TypeSize <= StrRef.size() && 4030 StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) { 4031 CheckFormatHandler::EmitFormatDiagnostic( 4032 *this, inFunctionCall, Args[format_idx], 4033 PDiag(diag::warn_printf_format_string_not_null_terminated), 4034 FExpr->getLocStart(), 4035 /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange()); 4036 return; 4037 } 4038 4039 // CHECK: empty format string? 4040 if (StrLen == 0 && numDataArgs > 0) { 4041 CheckFormatHandler::EmitFormatDiagnostic( 4042 *this, inFunctionCall, Args[format_idx], 4043 PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), 4044 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); 4045 return; 4046 } 4047 4048 if (Type == FST_Printf || Type == FST_NSString || 4049 Type == FST_FreeBSDKPrintf) { 4050 CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, 4051 numDataArgs, (Type == FST_NSString), 4052 Str, HasVAListArg, Args, format_idx, 4053 inFunctionCall, CallType, CheckedVarArgs); 4054 4055 if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, 4056 getLangOpts(), 4057 Context.getTargetInfo(), 4058 Type == FST_FreeBSDKPrintf)) 4059 H.DoneProcessing(); 4060 } else if (Type == FST_Scanf) { 4061 CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, 4062 Str, HasVAListArg, Args, format_idx, 4063 inFunctionCall, CallType, CheckedVarArgs); 4064 4065 if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, 4066 getLangOpts(), 4067 Context.getTargetInfo())) 4068 H.DoneProcessing(); 4069 } // TODO: handle other formats 4070 } 4071 4072 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) { 4073 // Str - The format string. NOTE: this is NOT null-terminated! 4074 StringRef StrRef = FExpr->getString(); 4075 const char *Str = StrRef.data(); 4076 // Account for cases where the string literal is truncated in a declaration. 4077 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType()); 4078 assert(T && "String literal not of constant array type!"); 4079 size_t TypeSize = T->getSize().getZExtValue(); 4080 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size()); 4081 return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen, 4082 getLangOpts(), 4083 Context.getTargetInfo()); 4084 } 4085 4086 //===--- CHECK: Warn on use of wrong absolute value function. -------------===// 4087 4088 // Returns the related absolute value function that is larger, of 0 if one 4089 // does not exist. 4090 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) { 4091 switch (AbsFunction) { 4092 default: 4093 return 0; 4094 4095 case Builtin::BI__builtin_abs: 4096 return Builtin::BI__builtin_labs; 4097 case Builtin::BI__builtin_labs: 4098 return Builtin::BI__builtin_llabs; 4099 case Builtin::BI__builtin_llabs: 4100 return 0; 4101 4102 case Builtin::BI__builtin_fabsf: 4103 return Builtin::BI__builtin_fabs; 4104 case Builtin::BI__builtin_fabs: 4105 return Builtin::BI__builtin_fabsl; 4106 case Builtin::BI__builtin_fabsl: 4107 return 0; 4108 4109 case Builtin::BI__builtin_cabsf: 4110 return Builtin::BI__builtin_cabs; 4111 case Builtin::BI__builtin_cabs: 4112 return Builtin::BI__builtin_cabsl; 4113 case Builtin::BI__builtin_cabsl: 4114 return 0; 4115 4116 case Builtin::BIabs: 4117 return Builtin::BIlabs; 4118 case Builtin::BIlabs: 4119 return Builtin::BIllabs; 4120 case Builtin::BIllabs: 4121 return 0; 4122 4123 case Builtin::BIfabsf: 4124 return Builtin::BIfabs; 4125 case Builtin::BIfabs: 4126 return Builtin::BIfabsl; 4127 case Builtin::BIfabsl: 4128 return 0; 4129 4130 case Builtin::BIcabsf: 4131 return Builtin::BIcabs; 4132 case Builtin::BIcabs: 4133 return Builtin::BIcabsl; 4134 case Builtin::BIcabsl: 4135 return 0; 4136 } 4137 } 4138 4139 // Returns the argument type of the absolute value function. 4140 static QualType getAbsoluteValueArgumentType(ASTContext &Context, 4141 unsigned AbsType) { 4142 if (AbsType == 0) 4143 return QualType(); 4144 4145 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None; 4146 QualType BuiltinType = Context.GetBuiltinType(AbsType, Error); 4147 if (Error != ASTContext::GE_None) 4148 return QualType(); 4149 4150 const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>(); 4151 if (!FT) 4152 return QualType(); 4153 4154 if (FT->getNumParams() != 1) 4155 return QualType(); 4156 4157 return FT->getParamType(0); 4158 } 4159 4160 // Returns the best absolute value function, or zero, based on type and 4161 // current absolute value function. 4162 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType, 4163 unsigned AbsFunctionKind) { 4164 unsigned BestKind = 0; 4165 uint64_t ArgSize = Context.getTypeSize(ArgType); 4166 for (unsigned Kind = AbsFunctionKind; Kind != 0; 4167 Kind = getLargerAbsoluteValueFunction(Kind)) { 4168 QualType ParamType = getAbsoluteValueArgumentType(Context, Kind); 4169 if (Context.getTypeSize(ParamType) >= ArgSize) { 4170 if (BestKind == 0) 4171 BestKind = Kind; 4172 else if (Context.hasSameType(ParamType, ArgType)) { 4173 BestKind = Kind; 4174 break; 4175 } 4176 } 4177 } 4178 return BestKind; 4179 } 4180 4181 enum AbsoluteValueKind { 4182 AVK_Integer, 4183 AVK_Floating, 4184 AVK_Complex 4185 }; 4186 4187 static AbsoluteValueKind getAbsoluteValueKind(QualType T) { 4188 if (T->isIntegralOrEnumerationType()) 4189 return AVK_Integer; 4190 if (T->isRealFloatingType()) 4191 return AVK_Floating; 4192 if (T->isAnyComplexType()) 4193 return AVK_Complex; 4194 4195 llvm_unreachable("Type not integer, floating, or complex"); 4196 } 4197 4198 // Changes the absolute value function to a different type. Preserves whether 4199 // the function is a builtin. 4200 static unsigned changeAbsFunction(unsigned AbsKind, 4201 AbsoluteValueKind ValueKind) { 4202 switch (ValueKind) { 4203 case AVK_Integer: 4204 switch (AbsKind) { 4205 default: 4206 return 0; 4207 case Builtin::BI__builtin_fabsf: 4208 case Builtin::BI__builtin_fabs: 4209 case Builtin::BI__builtin_fabsl: 4210 case Builtin::BI__builtin_cabsf: 4211 case Builtin::BI__builtin_cabs: 4212 case Builtin::BI__builtin_cabsl: 4213 return Builtin::BI__builtin_abs; 4214 case Builtin::BIfabsf: 4215 case Builtin::BIfabs: 4216 case Builtin::BIfabsl: 4217 case Builtin::BIcabsf: 4218 case Builtin::BIcabs: 4219 case Builtin::BIcabsl: 4220 return Builtin::BIabs; 4221 } 4222 case AVK_Floating: 4223 switch (AbsKind) { 4224 default: 4225 return 0; 4226 case Builtin::BI__builtin_abs: 4227 case Builtin::BI__builtin_labs: 4228 case Builtin::BI__builtin_llabs: 4229 case Builtin::BI__builtin_cabsf: 4230 case Builtin::BI__builtin_cabs: 4231 case Builtin::BI__builtin_cabsl: 4232 return Builtin::BI__builtin_fabsf; 4233 case Builtin::BIabs: 4234 case Builtin::BIlabs: 4235 case Builtin::BIllabs: 4236 case Builtin::BIcabsf: 4237 case Builtin::BIcabs: 4238 case Builtin::BIcabsl: 4239 return Builtin::BIfabsf; 4240 } 4241 case AVK_Complex: 4242 switch (AbsKind) { 4243 default: 4244 return 0; 4245 case Builtin::BI__builtin_abs: 4246 case Builtin::BI__builtin_labs: 4247 case Builtin::BI__builtin_llabs: 4248 case Builtin::BI__builtin_fabsf: 4249 case Builtin::BI__builtin_fabs: 4250 case Builtin::BI__builtin_fabsl: 4251 return Builtin::BI__builtin_cabsf; 4252 case Builtin::BIabs: 4253 case Builtin::BIlabs: 4254 case Builtin::BIllabs: 4255 case Builtin::BIfabsf: 4256 case Builtin::BIfabs: 4257 case Builtin::BIfabsl: 4258 return Builtin::BIcabsf; 4259 } 4260 } 4261 llvm_unreachable("Unable to convert function"); 4262 } 4263 4264 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) { 4265 const IdentifierInfo *FnInfo = FDecl->getIdentifier(); 4266 if (!FnInfo) 4267 return 0; 4268 4269 switch (FDecl->getBuiltinID()) { 4270 default: 4271 return 0; 4272 case Builtin::BI__builtin_abs: 4273 case Builtin::BI__builtin_fabs: 4274 case Builtin::BI__builtin_fabsf: 4275 case Builtin::BI__builtin_fabsl: 4276 case Builtin::BI__builtin_labs: 4277 case Builtin::BI__builtin_llabs: 4278 case Builtin::BI__builtin_cabs: 4279 case Builtin::BI__builtin_cabsf: 4280 case Builtin::BI__builtin_cabsl: 4281 case Builtin::BIabs: 4282 case Builtin::BIlabs: 4283 case Builtin::BIllabs: 4284 case Builtin::BIfabs: 4285 case Builtin::BIfabsf: 4286 case Builtin::BIfabsl: 4287 case Builtin::BIcabs: 4288 case Builtin::BIcabsf: 4289 case Builtin::BIcabsl: 4290 return FDecl->getBuiltinID(); 4291 } 4292 llvm_unreachable("Unknown Builtin type"); 4293 } 4294 4295 // If the replacement is valid, emit a note with replacement function. 4296 // Additionally, suggest including the proper header if not already included. 4297 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range, 4298 unsigned AbsKind, QualType ArgType) { 4299 bool EmitHeaderHint = true; 4300 const char *HeaderName = nullptr; 4301 const char *FunctionName = nullptr; 4302 if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) { 4303 FunctionName = "std::abs"; 4304 if (ArgType->isIntegralOrEnumerationType()) { 4305 HeaderName = "cstdlib"; 4306 } else if (ArgType->isRealFloatingType()) { 4307 HeaderName = "cmath"; 4308 } else { 4309 llvm_unreachable("Invalid Type"); 4310 } 4311 4312 // Lookup all std::abs 4313 if (NamespaceDecl *Std = S.getStdNamespace()) { 4314 LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName); 4315 R.suppressDiagnostics(); 4316 S.LookupQualifiedName(R, Std); 4317 4318 for (const auto *I : R) { 4319 const FunctionDecl *FDecl = nullptr; 4320 if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) { 4321 FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl()); 4322 } else { 4323 FDecl = dyn_cast<FunctionDecl>(I); 4324 } 4325 if (!FDecl) 4326 continue; 4327 4328 // Found std::abs(), check that they are the right ones. 4329 if (FDecl->getNumParams() != 1) 4330 continue; 4331 4332 // Check that the parameter type can handle the argument. 4333 QualType ParamType = FDecl->getParamDecl(0)->getType(); 4334 if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) && 4335 S.Context.getTypeSize(ArgType) <= 4336 S.Context.getTypeSize(ParamType)) { 4337 // Found a function, don't need the header hint. 4338 EmitHeaderHint = false; 4339 break; 4340 } 4341 } 4342 } 4343 } else { 4344 FunctionName = S.Context.BuiltinInfo.GetName(AbsKind); 4345 HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind); 4346 4347 if (HeaderName) { 4348 DeclarationName DN(&S.Context.Idents.get(FunctionName)); 4349 LookupResult R(S, DN, Loc, Sema::LookupAnyName); 4350 R.suppressDiagnostics(); 4351 S.LookupName(R, S.getCurScope()); 4352 4353 if (R.isSingleResult()) { 4354 FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl()); 4355 if (FD && FD->getBuiltinID() == AbsKind) { 4356 EmitHeaderHint = false; 4357 } else { 4358 return; 4359 } 4360 } else if (!R.empty()) { 4361 return; 4362 } 4363 } 4364 } 4365 4366 S.Diag(Loc, diag::note_replace_abs_function) 4367 << FunctionName << FixItHint::CreateReplacement(Range, FunctionName); 4368 4369 if (!HeaderName) 4370 return; 4371 4372 if (!EmitHeaderHint) 4373 return; 4374 4375 S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName 4376 << FunctionName; 4377 } 4378 4379 static bool IsFunctionStdAbs(const FunctionDecl *FDecl) { 4380 if (!FDecl) 4381 return false; 4382 4383 if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr("abs")) 4384 return false; 4385 4386 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(FDecl->getDeclContext()); 4387 4388 while (ND && ND->isInlineNamespace()) { 4389 ND = dyn_cast<NamespaceDecl>(ND->getDeclContext()); 4390 } 4391 4392 if (!ND || !ND->getIdentifier() || !ND->getIdentifier()->isStr("std")) 4393 return false; 4394 4395 if (!isa<TranslationUnitDecl>(ND->getDeclContext())) 4396 return false; 4397 4398 return true; 4399 } 4400 4401 // Warn when using the wrong abs() function. 4402 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call, 4403 const FunctionDecl *FDecl, 4404 IdentifierInfo *FnInfo) { 4405 if (Call->getNumArgs() != 1) 4406 return; 4407 4408 unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl); 4409 bool IsStdAbs = IsFunctionStdAbs(FDecl); 4410 if (AbsKind == 0 && !IsStdAbs) 4411 return; 4412 4413 QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType(); 4414 QualType ParamType = Call->getArg(0)->getType(); 4415 4416 // Unsigned types cannot be negative. Suggest removing the absolute value 4417 // function call. 4418 if (ArgType->isUnsignedIntegerType()) { 4419 const char *FunctionName = 4420 IsStdAbs ? "std::abs" : Context.BuiltinInfo.GetName(AbsKind); 4421 Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType; 4422 Diag(Call->getExprLoc(), diag::note_remove_abs) 4423 << FunctionName 4424 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange()); 4425 return; 4426 } 4427 4428 // std::abs has overloads which prevent most of the absolute value problems 4429 // from occurring. 4430 if (IsStdAbs) 4431 return; 4432 4433 AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType); 4434 AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType); 4435 4436 // The argument and parameter are the same kind. Check if they are the right 4437 // size. 4438 if (ArgValueKind == ParamValueKind) { 4439 if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType)) 4440 return; 4441 4442 unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind); 4443 Diag(Call->getExprLoc(), diag::warn_abs_too_small) 4444 << FDecl << ArgType << ParamType; 4445 4446 if (NewAbsKind == 0) 4447 return; 4448 4449 emitReplacement(*this, Call->getExprLoc(), 4450 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 4451 return; 4452 } 4453 4454 // ArgValueKind != ParamValueKind 4455 // The wrong type of absolute value function was used. Attempt to find the 4456 // proper one. 4457 unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind); 4458 NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind); 4459 if (NewAbsKind == 0) 4460 return; 4461 4462 Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type) 4463 << FDecl << ParamValueKind << ArgValueKind; 4464 4465 emitReplacement(*this, Call->getExprLoc(), 4466 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType); 4467 return; 4468 } 4469 4470 //===--- CHECK: Standard memory functions ---------------------------------===// 4471 4472 /// \brief Takes the expression passed to the size_t parameter of functions 4473 /// such as memcmp, strncat, etc and warns if it's a comparison. 4474 /// 4475 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`. 4476 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E, 4477 IdentifierInfo *FnName, 4478 SourceLocation FnLoc, 4479 SourceLocation RParenLoc) { 4480 const BinaryOperator *Size = dyn_cast<BinaryOperator>(E); 4481 if (!Size) 4482 return false; 4483 4484 // if E is binop and op is >, <, >=, <=, ==, &&, ||: 4485 if (!Size->isComparisonOp() && !Size->isEqualityOp() && !Size->isLogicalOp()) 4486 return false; 4487 4488 SourceRange SizeRange = Size->getSourceRange(); 4489 S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison) 4490 << SizeRange << FnName; 4491 S.Diag(FnLoc, diag::note_memsize_comparison_paren) 4492 << FnName << FixItHint::CreateInsertion( 4493 S.getLocForEndOfToken(Size->getLHS()->getLocEnd()), ")") 4494 << FixItHint::CreateRemoval(RParenLoc); 4495 S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence) 4496 << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(") 4497 << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()), 4498 ")"); 4499 4500 return true; 4501 } 4502 4503 /// \brief Determine whether the given type is or contains a dynamic class type 4504 /// (e.g., whether it has a vtable). 4505 static const CXXRecordDecl *getContainedDynamicClass(QualType T, 4506 bool &IsContained) { 4507 // Look through array types while ignoring qualifiers. 4508 const Type *Ty = T->getBaseElementTypeUnsafe(); 4509 IsContained = false; 4510 4511 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); 4512 RD = RD ? RD->getDefinition() : nullptr; 4513 if (!RD) 4514 return nullptr; 4515 4516 if (RD->isDynamicClass()) 4517 return RD; 4518 4519 // Check all the fields. If any bases were dynamic, the class is dynamic. 4520 // It's impossible for a class to transitively contain itself by value, so 4521 // infinite recursion is impossible. 4522 for (auto *FD : RD->fields()) { 4523 bool SubContained; 4524 if (const CXXRecordDecl *ContainedRD = 4525 getContainedDynamicClass(FD->getType(), SubContained)) { 4526 IsContained = true; 4527 return ContainedRD; 4528 } 4529 } 4530 4531 return nullptr; 4532 } 4533 4534 /// \brief If E is a sizeof expression, returns its argument expression, 4535 /// otherwise returns NULL. 4536 static const Expr *getSizeOfExprArg(const Expr* E) { 4537 if (const UnaryExprOrTypeTraitExpr *SizeOf = 4538 dyn_cast<UnaryExprOrTypeTraitExpr>(E)) 4539 if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) 4540 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); 4541 4542 return nullptr; 4543 } 4544 4545 /// \brief If E is a sizeof expression, returns its argument type. 4546 static QualType getSizeOfArgType(const Expr* E) { 4547 if (const UnaryExprOrTypeTraitExpr *SizeOf = 4548 dyn_cast<UnaryExprOrTypeTraitExpr>(E)) 4549 if (SizeOf->getKind() == clang::UETT_SizeOf) 4550 return SizeOf->getTypeOfArgument(); 4551 4552 return QualType(); 4553 } 4554 4555 /// \brief Check for dangerous or invalid arguments to memset(). 4556 /// 4557 /// This issues warnings on known problematic, dangerous or unspecified 4558 /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' 4559 /// function calls. 4560 /// 4561 /// \param Call The call expression to diagnose. 4562 void Sema::CheckMemaccessArguments(const CallExpr *Call, 4563 unsigned BId, 4564 IdentifierInfo *FnName) { 4565 assert(BId != 0); 4566 4567 // It is possible to have a non-standard definition of memset. Validate 4568 // we have enough arguments, and if not, abort further checking. 4569 unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); 4570 if (Call->getNumArgs() < ExpectedNumArgs) 4571 return; 4572 4573 unsigned LastArg = (BId == Builtin::BImemset || 4574 BId == Builtin::BIstrndup ? 1 : 2); 4575 unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); 4576 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); 4577 4578 if (CheckMemorySizeofForComparison(*this, LenExpr, FnName, 4579 Call->getLocStart(), Call->getRParenLoc())) 4580 return; 4581 4582 // We have special checking when the length is a sizeof expression. 4583 QualType SizeOfArgTy = getSizeOfArgType(LenExpr); 4584 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); 4585 llvm::FoldingSetNodeID SizeOfArgID; 4586 4587 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { 4588 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); 4589 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); 4590 4591 QualType DestTy = Dest->getType(); 4592 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { 4593 QualType PointeeTy = DestPtrTy->getPointeeType(); 4594 4595 // Never warn about void type pointers. This can be used to suppress 4596 // false positives. 4597 if (PointeeTy->isVoidType()) 4598 continue; 4599 4600 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by 4601 // actually comparing the expressions for equality. Because computing the 4602 // expression IDs can be expensive, we only do this if the diagnostic is 4603 // enabled. 4604 if (SizeOfArg && 4605 !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, 4606 SizeOfArg->getExprLoc())) { 4607 // We only compute IDs for expressions if the warning is enabled, and 4608 // cache the sizeof arg's ID. 4609 if (SizeOfArgID == llvm::FoldingSetNodeID()) 4610 SizeOfArg->Profile(SizeOfArgID, Context, true); 4611 llvm::FoldingSetNodeID DestID; 4612 Dest->Profile(DestID, Context, true); 4613 if (DestID == SizeOfArgID) { 4614 // TODO: For strncpy() and friends, this could suggest sizeof(dst) 4615 // over sizeof(src) as well. 4616 unsigned ActionIdx = 0; // Default is to suggest dereferencing. 4617 StringRef ReadableName = FnName->getName(); 4618 4619 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) 4620 if (UnaryOp->getOpcode() == UO_AddrOf) 4621 ActionIdx = 1; // If its an address-of operator, just remove it. 4622 if (!PointeeTy->isIncompleteType() && 4623 (Context.getTypeSize(PointeeTy) == Context.getCharWidth())) 4624 ActionIdx = 2; // If the pointee's size is sizeof(char), 4625 // suggest an explicit length. 4626 4627 // If the function is defined as a builtin macro, do not show macro 4628 // expansion. 4629 SourceLocation SL = SizeOfArg->getExprLoc(); 4630 SourceRange DSR = Dest->getSourceRange(); 4631 SourceRange SSR = SizeOfArg->getSourceRange(); 4632 SourceManager &SM = getSourceManager(); 4633 4634 if (SM.isMacroArgExpansion(SL)) { 4635 ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); 4636 SL = SM.getSpellingLoc(SL); 4637 DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), 4638 SM.getSpellingLoc(DSR.getEnd())); 4639 SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), 4640 SM.getSpellingLoc(SSR.getEnd())); 4641 } 4642 4643 DiagRuntimeBehavior(SL, SizeOfArg, 4644 PDiag(diag::warn_sizeof_pointer_expr_memaccess) 4645 << ReadableName 4646 << PointeeTy 4647 << DestTy 4648 << DSR 4649 << SSR); 4650 DiagRuntimeBehavior(SL, SizeOfArg, 4651 PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) 4652 << ActionIdx 4653 << SSR); 4654 4655 break; 4656 } 4657 } 4658 4659 // Also check for cases where the sizeof argument is the exact same 4660 // type as the memory argument, and where it points to a user-defined 4661 // record type. 4662 if (SizeOfArgTy != QualType()) { 4663 if (PointeeTy->isRecordType() && 4664 Context.typesAreCompatible(SizeOfArgTy, DestTy)) { 4665 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, 4666 PDiag(diag::warn_sizeof_pointer_type_memaccess) 4667 << FnName << SizeOfArgTy << ArgIdx 4668 << PointeeTy << Dest->getSourceRange() 4669 << LenExpr->getSourceRange()); 4670 break; 4671 } 4672 } 4673 4674 // Always complain about dynamic classes. 4675 bool IsContained; 4676 if (const CXXRecordDecl *ContainedRD = 4677 getContainedDynamicClass(PointeeTy, IsContained)) { 4678 4679 unsigned OperationType = 0; 4680 // "overwritten" if we're warning about the destination for any call 4681 // but memcmp; otherwise a verb appropriate to the call. 4682 if (ArgIdx != 0 || BId == Builtin::BImemcmp) { 4683 if (BId == Builtin::BImemcpy) 4684 OperationType = 1; 4685 else if(BId == Builtin::BImemmove) 4686 OperationType = 2; 4687 else if (BId == Builtin::BImemcmp) 4688 OperationType = 3; 4689 } 4690 4691 DiagRuntimeBehavior( 4692 Dest->getExprLoc(), Dest, 4693 PDiag(diag::warn_dyn_class_memaccess) 4694 << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) 4695 << FnName << IsContained << ContainedRD << OperationType 4696 << Call->getCallee()->getSourceRange()); 4697 } else if (PointeeTy.hasNonTrivialObjCLifetime() && 4698 BId != Builtin::BImemset) 4699 DiagRuntimeBehavior( 4700 Dest->getExprLoc(), Dest, 4701 PDiag(diag::warn_arc_object_memaccess) 4702 << ArgIdx << FnName << PointeeTy 4703 << Call->getCallee()->getSourceRange()); 4704 else 4705 continue; 4706 4707 DiagRuntimeBehavior( 4708 Dest->getExprLoc(), Dest, 4709 PDiag(diag::note_bad_memaccess_silence) 4710 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); 4711 break; 4712 } 4713 } 4714 } 4715 4716 // A little helper routine: ignore addition and subtraction of integer literals. 4717 // This intentionally does not ignore all integer constant expressions because 4718 // we don't want to remove sizeof(). 4719 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { 4720 Ex = Ex->IgnoreParenCasts(); 4721 4722 for (;;) { 4723 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); 4724 if (!BO || !BO->isAdditiveOp()) 4725 break; 4726 4727 const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); 4728 const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); 4729 4730 if (isa<IntegerLiteral>(RHS)) 4731 Ex = LHS; 4732 else if (isa<IntegerLiteral>(LHS)) 4733 Ex = RHS; 4734 else 4735 break; 4736 } 4737 4738 return Ex; 4739 } 4740 4741 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, 4742 ASTContext &Context) { 4743 // Only handle constant-sized or VLAs, but not flexible members. 4744 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { 4745 // Only issue the FIXIT for arrays of size > 1. 4746 if (CAT->getSize().getSExtValue() <= 1) 4747 return false; 4748 } else if (!Ty->isVariableArrayType()) { 4749 return false; 4750 } 4751 return true; 4752 } 4753 4754 // Warn if the user has made the 'size' argument to strlcpy or strlcat 4755 // be the size of the source, instead of the destination. 4756 void Sema::CheckStrlcpycatArguments(const CallExpr *Call, 4757 IdentifierInfo *FnName) { 4758 4759 // Don't crash if the user has the wrong number of arguments 4760 unsigned NumArgs = Call->getNumArgs(); 4761 if ((NumArgs != 3) && (NumArgs != 4)) 4762 return; 4763 4764 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); 4765 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); 4766 const Expr *CompareWithSrc = nullptr; 4767 4768 if (CheckMemorySizeofForComparison(*this, SizeArg, FnName, 4769 Call->getLocStart(), Call->getRParenLoc())) 4770 return; 4771 4772 // Look for 'strlcpy(dst, x, sizeof(x))' 4773 if (const Expr *Ex = getSizeOfExprArg(SizeArg)) 4774 CompareWithSrc = Ex; 4775 else { 4776 // Look for 'strlcpy(dst, x, strlen(x))' 4777 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { 4778 if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen && 4779 SizeCall->getNumArgs() == 1) 4780 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); 4781 } 4782 } 4783 4784 if (!CompareWithSrc) 4785 return; 4786 4787 // Determine if the argument to sizeof/strlen is equal to the source 4788 // argument. In principle there's all kinds of things you could do 4789 // here, for instance creating an == expression and evaluating it with 4790 // EvaluateAsBooleanCondition, but this uses a more direct technique: 4791 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); 4792 if (!SrcArgDRE) 4793 return; 4794 4795 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); 4796 if (!CompareWithSrcDRE || 4797 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) 4798 return; 4799 4800 const Expr *OriginalSizeArg = Call->getArg(2); 4801 Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) 4802 << OriginalSizeArg->getSourceRange() << FnName; 4803 4804 // Output a FIXIT hint if the destination is an array (rather than a 4805 // pointer to an array). This could be enhanced to handle some 4806 // pointers if we know the actual size, like if DstArg is 'array+2' 4807 // we could say 'sizeof(array)-2'. 4808 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); 4809 if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) 4810 return; 4811 4812 SmallString<128> sizeString; 4813 llvm::raw_svector_ostream OS(sizeString); 4814 OS << "sizeof("; 4815 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 4816 OS << ")"; 4817 4818 Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) 4819 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), 4820 OS.str()); 4821 } 4822 4823 /// Check if two expressions refer to the same declaration. 4824 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { 4825 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) 4826 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) 4827 return D1->getDecl() == D2->getDecl(); 4828 return false; 4829 } 4830 4831 static const Expr *getStrlenExprArg(const Expr *E) { 4832 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 4833 const FunctionDecl *FD = CE->getDirectCallee(); 4834 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) 4835 return nullptr; 4836 return CE->getArg(0)->IgnoreParenCasts(); 4837 } 4838 return nullptr; 4839 } 4840 4841 // Warn on anti-patterns as the 'size' argument to strncat. 4842 // The correct size argument should look like following: 4843 // strncat(dst, src, sizeof(dst) - strlen(dest) - 1); 4844 void Sema::CheckStrncatArguments(const CallExpr *CE, 4845 IdentifierInfo *FnName) { 4846 // Don't crash if the user has the wrong number of arguments. 4847 if (CE->getNumArgs() < 3) 4848 return; 4849 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); 4850 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); 4851 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); 4852 4853 if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getLocStart(), 4854 CE->getRParenLoc())) 4855 return; 4856 4857 // Identify common expressions, which are wrongly used as the size argument 4858 // to strncat and may lead to buffer overflows. 4859 unsigned PatternType = 0; 4860 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { 4861 // - sizeof(dst) 4862 if (referToTheSameDecl(SizeOfArg, DstArg)) 4863 PatternType = 1; 4864 // - sizeof(src) 4865 else if (referToTheSameDecl(SizeOfArg, SrcArg)) 4866 PatternType = 2; 4867 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { 4868 if (BE->getOpcode() == BO_Sub) { 4869 const Expr *L = BE->getLHS()->IgnoreParenCasts(); 4870 const Expr *R = BE->getRHS()->IgnoreParenCasts(); 4871 // - sizeof(dst) - strlen(dst) 4872 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && 4873 referToTheSameDecl(DstArg, getStrlenExprArg(R))) 4874 PatternType = 1; 4875 // - sizeof(src) - (anything) 4876 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) 4877 PatternType = 2; 4878 } 4879 } 4880 4881 if (PatternType == 0) 4882 return; 4883 4884 // Generate the diagnostic. 4885 SourceLocation SL = LenArg->getLocStart(); 4886 SourceRange SR = LenArg->getSourceRange(); 4887 SourceManager &SM = getSourceManager(); 4888 4889 // If the function is defined as a builtin macro, do not show macro expansion. 4890 if (SM.isMacroArgExpansion(SL)) { 4891 SL = SM.getSpellingLoc(SL); 4892 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), 4893 SM.getSpellingLoc(SR.getEnd())); 4894 } 4895 4896 // Check if the destination is an array (rather than a pointer to an array). 4897 QualType DstTy = DstArg->getType(); 4898 bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, 4899 Context); 4900 if (!isKnownSizeArray) { 4901 if (PatternType == 1) 4902 Diag(SL, diag::warn_strncat_wrong_size) << SR; 4903 else 4904 Diag(SL, diag::warn_strncat_src_size) << SR; 4905 return; 4906 } 4907 4908 if (PatternType == 1) 4909 Diag(SL, diag::warn_strncat_large_size) << SR; 4910 else 4911 Diag(SL, diag::warn_strncat_src_size) << SR; 4912 4913 SmallString<128> sizeString; 4914 llvm::raw_svector_ostream OS(sizeString); 4915 OS << "sizeof("; 4916 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 4917 OS << ") - "; 4918 OS << "strlen("; 4919 DstArg->printPretty(OS, nullptr, getPrintingPolicy()); 4920 OS << ") - 1"; 4921 4922 Diag(SL, diag::note_strncat_wrong_size) 4923 << FixItHint::CreateReplacement(SR, OS.str()); 4924 } 4925 4926 //===--- CHECK: Return Address of Stack Variable --------------------------===// 4927 4928 static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, 4929 Decl *ParentDecl); 4930 static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, 4931 Decl *ParentDecl); 4932 4933 /// CheckReturnStackAddr - Check if a return statement returns the address 4934 /// of a stack variable. 4935 static void 4936 CheckReturnStackAddr(Sema &S, Expr *RetValExp, QualType lhsType, 4937 SourceLocation ReturnLoc) { 4938 4939 Expr *stackE = nullptr; 4940 SmallVector<DeclRefExpr *, 8> refVars; 4941 4942 // Perform checking for returned stack addresses, local blocks, 4943 // label addresses or references to temporaries. 4944 if (lhsType->isPointerType() || 4945 (!S.getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { 4946 stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/nullptr); 4947 } else if (lhsType->isReferenceType()) { 4948 stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/nullptr); 4949 } 4950 4951 if (!stackE) 4952 return; // Nothing suspicious was found. 4953 4954 SourceLocation diagLoc; 4955 SourceRange diagRange; 4956 if (refVars.empty()) { 4957 diagLoc = stackE->getLocStart(); 4958 diagRange = stackE->getSourceRange(); 4959 } else { 4960 // We followed through a reference variable. 'stackE' contains the 4961 // problematic expression but we will warn at the return statement pointing 4962 // at the reference variable. We will later display the "trail" of 4963 // reference variables using notes. 4964 diagLoc = refVars[0]->getLocStart(); 4965 diagRange = refVars[0]->getSourceRange(); 4966 } 4967 4968 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. 4969 S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref 4970 : diag::warn_ret_stack_addr) 4971 << DR->getDecl()->getDeclName() << diagRange; 4972 } else if (isa<BlockExpr>(stackE)) { // local block. 4973 S.Diag(diagLoc, diag::err_ret_local_block) << diagRange; 4974 } else if (isa<AddrLabelExpr>(stackE)) { // address of label. 4975 S.Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; 4976 } else { // local temporary. 4977 S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref 4978 : diag::warn_ret_local_temp_addr) 4979 << diagRange; 4980 } 4981 4982 // Display the "trail" of reference variables that we followed until we 4983 // found the problematic expression using notes. 4984 for (unsigned i = 0, e = refVars.size(); i != e; ++i) { 4985 VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); 4986 // If this var binds to another reference var, show the range of the next 4987 // var, otherwise the var binds to the problematic expression, in which case 4988 // show the range of the expression. 4989 SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() 4990 : stackE->getSourceRange(); 4991 S.Diag(VD->getLocation(), diag::note_ref_var_local_bind) 4992 << VD->getDeclName() << range; 4993 } 4994 } 4995 4996 /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that 4997 /// check if the expression in a return statement evaluates to an address 4998 /// to a location on the stack, a local block, an address of a label, or a 4999 /// reference to local temporary. The recursion is used to traverse the 5000 /// AST of the return expression, with recursion backtracking when we 5001 /// encounter a subexpression that (1) clearly does not lead to one of the 5002 /// above problematic expressions (2) is something we cannot determine leads to 5003 /// a problematic expression based on such local checking. 5004 /// 5005 /// Both EvalAddr and EvalVal follow through reference variables to evaluate 5006 /// the expression that they point to. Such variables are added to the 5007 /// 'refVars' vector so that we know what the reference variable "trail" was. 5008 /// 5009 /// EvalAddr processes expressions that are pointers that are used as 5010 /// references (and not L-values). EvalVal handles all other values. 5011 /// At the base case of the recursion is a check for the above problematic 5012 /// expressions. 5013 /// 5014 /// This implementation handles: 5015 /// 5016 /// * pointer-to-pointer casts 5017 /// * implicit conversions from array references to pointers 5018 /// * taking the address of fields 5019 /// * arbitrary interplay between "&" and "*" operators 5020 /// * pointer arithmetic from an address of a stack variable 5021 /// * taking the address of an array element where the array is on the stack 5022 static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, 5023 Decl *ParentDecl) { 5024 if (E->isTypeDependent()) 5025 return nullptr; 5026 5027 // We should only be called for evaluating pointer expressions. 5028 assert((E->getType()->isAnyPointerType() || 5029 E->getType()->isBlockPointerType() || 5030 E->getType()->isObjCQualifiedIdType()) && 5031 "EvalAddr only works on pointers"); 5032 5033 E = E->IgnoreParens(); 5034 5035 // Our "symbolic interpreter" is just a dispatch off the currently 5036 // viewed AST node. We then recursively traverse the AST by calling 5037 // EvalAddr and EvalVal appropriately. 5038 switch (E->getStmtClass()) { 5039 case Stmt::DeclRefExprClass: { 5040 DeclRefExpr *DR = cast<DeclRefExpr>(E); 5041 5042 // If we leave the immediate function, the lifetime isn't about to end. 5043 if (DR->refersToEnclosingVariableOrCapture()) 5044 return nullptr; 5045 5046 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) 5047 // If this is a reference variable, follow through to the expression that 5048 // it points to. 5049 if (V->hasLocalStorage() && 5050 V->getType()->isReferenceType() && V->hasInit()) { 5051 // Add the reference variable to the "trail". 5052 refVars.push_back(DR); 5053 return EvalAddr(V->getInit(), refVars, ParentDecl); 5054 } 5055 5056 return nullptr; 5057 } 5058 5059 case Stmt::UnaryOperatorClass: { 5060 // The only unary operator that make sense to handle here 5061 // is AddrOf. All others don't make sense as pointers. 5062 UnaryOperator *U = cast<UnaryOperator>(E); 5063 5064 if (U->getOpcode() == UO_AddrOf) 5065 return EvalVal(U->getSubExpr(), refVars, ParentDecl); 5066 else 5067 return nullptr; 5068 } 5069 5070 case Stmt::BinaryOperatorClass: { 5071 // Handle pointer arithmetic. All other binary operators are not valid 5072 // in this context. 5073 BinaryOperator *B = cast<BinaryOperator>(E); 5074 BinaryOperatorKind op = B->getOpcode(); 5075 5076 if (op != BO_Add && op != BO_Sub) 5077 return nullptr; 5078 5079 Expr *Base = B->getLHS(); 5080 5081 // Determine which argument is the real pointer base. It could be 5082 // the RHS argument instead of the LHS. 5083 if (!Base->getType()->isPointerType()) Base = B->getRHS(); 5084 5085 assert (Base->getType()->isPointerType()); 5086 return EvalAddr(Base, refVars, ParentDecl); 5087 } 5088 5089 // For conditional operators we need to see if either the LHS or RHS are 5090 // valid DeclRefExpr*s. If one of them is valid, we return it. 5091 case Stmt::ConditionalOperatorClass: { 5092 ConditionalOperator *C = cast<ConditionalOperator>(E); 5093 5094 // Handle the GNU extension for missing LHS. 5095 // FIXME: That isn't a ConditionalOperator, so doesn't get here. 5096 if (Expr *LHSExpr = C->getLHS()) { 5097 // In C++, we can have a throw-expression, which has 'void' type. 5098 if (!LHSExpr->getType()->isVoidType()) 5099 if (Expr *LHS = EvalAddr(LHSExpr, refVars, ParentDecl)) 5100 return LHS; 5101 } 5102 5103 // In C++, we can have a throw-expression, which has 'void' type. 5104 if (C->getRHS()->getType()->isVoidType()) 5105 return nullptr; 5106 5107 return EvalAddr(C->getRHS(), refVars, ParentDecl); 5108 } 5109 5110 case Stmt::BlockExprClass: 5111 if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) 5112 return E; // local block. 5113 return nullptr; 5114 5115 case Stmt::AddrLabelExprClass: 5116 return E; // address of label. 5117 5118 case Stmt::ExprWithCleanupsClass: 5119 return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, 5120 ParentDecl); 5121 5122 // For casts, we need to handle conversions from arrays to 5123 // pointer values, and pointer-to-pointer conversions. 5124 case Stmt::ImplicitCastExprClass: 5125 case Stmt::CStyleCastExprClass: 5126 case Stmt::CXXFunctionalCastExprClass: 5127 case Stmt::ObjCBridgedCastExprClass: 5128 case Stmt::CXXStaticCastExprClass: 5129 case Stmt::CXXDynamicCastExprClass: 5130 case Stmt::CXXConstCastExprClass: 5131 case Stmt::CXXReinterpretCastExprClass: { 5132 Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); 5133 switch (cast<CastExpr>(E)->getCastKind()) { 5134 case CK_LValueToRValue: 5135 case CK_NoOp: 5136 case CK_BaseToDerived: 5137 case CK_DerivedToBase: 5138 case CK_UncheckedDerivedToBase: 5139 case CK_Dynamic: 5140 case CK_CPointerToObjCPointerCast: 5141 case CK_BlockPointerToObjCPointerCast: 5142 case CK_AnyPointerToBlockPointerCast: 5143 return EvalAddr(SubExpr, refVars, ParentDecl); 5144 5145 case CK_ArrayToPointerDecay: 5146 return EvalVal(SubExpr, refVars, ParentDecl); 5147 5148 case CK_BitCast: 5149 if (SubExpr->getType()->isAnyPointerType() || 5150 SubExpr->getType()->isBlockPointerType() || 5151 SubExpr->getType()->isObjCQualifiedIdType()) 5152 return EvalAddr(SubExpr, refVars, ParentDecl); 5153 else 5154 return nullptr; 5155 5156 default: 5157 return nullptr; 5158 } 5159 } 5160 5161 case Stmt::MaterializeTemporaryExprClass: 5162 if (Expr *Result = EvalAddr( 5163 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), 5164 refVars, ParentDecl)) 5165 return Result; 5166 5167 return E; 5168 5169 // Everything else: we simply don't reason about them. 5170 default: 5171 return nullptr; 5172 } 5173 } 5174 5175 5176 /// EvalVal - This function is complements EvalAddr in the mutual recursion. 5177 /// See the comments for EvalAddr for more details. 5178 static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, 5179 Decl *ParentDecl) { 5180 do { 5181 // We should only be called for evaluating non-pointer expressions, or 5182 // expressions with a pointer type that are not used as references but instead 5183 // are l-values (e.g., DeclRefExpr with a pointer type). 5184 5185 // Our "symbolic interpreter" is just a dispatch off the currently 5186 // viewed AST node. We then recursively traverse the AST by calling 5187 // EvalAddr and EvalVal appropriately. 5188 5189 E = E->IgnoreParens(); 5190 switch (E->getStmtClass()) { 5191 case Stmt::ImplicitCastExprClass: { 5192 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); 5193 if (IE->getValueKind() == VK_LValue) { 5194 E = IE->getSubExpr(); 5195 continue; 5196 } 5197 return nullptr; 5198 } 5199 5200 case Stmt::ExprWithCleanupsClass: 5201 return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); 5202 5203 case Stmt::DeclRefExprClass: { 5204 // When we hit a DeclRefExpr we are looking at code that refers to a 5205 // variable's name. If it's not a reference variable we check if it has 5206 // local storage within the function, and if so, return the expression. 5207 DeclRefExpr *DR = cast<DeclRefExpr>(E); 5208 5209 // If we leave the immediate function, the lifetime isn't about to end. 5210 if (DR->refersToEnclosingVariableOrCapture()) 5211 return nullptr; 5212 5213 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { 5214 // Check if it refers to itself, e.g. "int& i = i;". 5215 if (V == ParentDecl) 5216 return DR; 5217 5218 if (V->hasLocalStorage()) { 5219 if (!V->getType()->isReferenceType()) 5220 return DR; 5221 5222 // Reference variable, follow through to the expression that 5223 // it points to. 5224 if (V->hasInit()) { 5225 // Add the reference variable to the "trail". 5226 refVars.push_back(DR); 5227 return EvalVal(V->getInit(), refVars, V); 5228 } 5229 } 5230 } 5231 5232 return nullptr; 5233 } 5234 5235 case Stmt::UnaryOperatorClass: { 5236 // The only unary operator that make sense to handle here 5237 // is Deref. All others don't resolve to a "name." This includes 5238 // handling all sorts of rvalues passed to a unary operator. 5239 UnaryOperator *U = cast<UnaryOperator>(E); 5240 5241 if (U->getOpcode() == UO_Deref) 5242 return EvalAddr(U->getSubExpr(), refVars, ParentDecl); 5243 5244 return nullptr; 5245 } 5246 5247 case Stmt::ArraySubscriptExprClass: { 5248 // Array subscripts are potential references to data on the stack. We 5249 // retrieve the DeclRefExpr* for the array variable if it indeed 5250 // has local storage. 5251 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); 5252 } 5253 5254 case Stmt::ConditionalOperatorClass: { 5255 // For conditional operators we need to see if either the LHS or RHS are 5256 // non-NULL Expr's. If one is non-NULL, we return it. 5257 ConditionalOperator *C = cast<ConditionalOperator>(E); 5258 5259 // Handle the GNU extension for missing LHS. 5260 if (Expr *LHSExpr = C->getLHS()) { 5261 // In C++, we can have a throw-expression, which has 'void' type. 5262 if (!LHSExpr->getType()->isVoidType()) 5263 if (Expr *LHS = EvalVal(LHSExpr, refVars, ParentDecl)) 5264 return LHS; 5265 } 5266 5267 // In C++, we can have a throw-expression, which has 'void' type. 5268 if (C->getRHS()->getType()->isVoidType()) 5269 return nullptr; 5270 5271 return EvalVal(C->getRHS(), refVars, ParentDecl); 5272 } 5273 5274 // Accesses to members are potential references to data on the stack. 5275 case Stmt::MemberExprClass: { 5276 MemberExpr *M = cast<MemberExpr>(E); 5277 5278 // Check for indirect access. We only want direct field accesses. 5279 if (M->isArrow()) 5280 return nullptr; 5281 5282 // Check whether the member type is itself a reference, in which case 5283 // we're not going to refer to the member, but to what the member refers to. 5284 if (M->getMemberDecl()->getType()->isReferenceType()) 5285 return nullptr; 5286 5287 return EvalVal(M->getBase(), refVars, ParentDecl); 5288 } 5289 5290 case Stmt::MaterializeTemporaryExprClass: 5291 if (Expr *Result = EvalVal( 5292 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), 5293 refVars, ParentDecl)) 5294 return Result; 5295 5296 return E; 5297 5298 default: 5299 // Check that we don't return or take the address of a reference to a 5300 // temporary. This is only useful in C++. 5301 if (!E->isTypeDependent() && E->isRValue()) 5302 return E; 5303 5304 // Everything else: we simply don't reason about them. 5305 return nullptr; 5306 } 5307 } while (true); 5308 } 5309 5310 void 5311 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType, 5312 SourceLocation ReturnLoc, 5313 bool isObjCMethod, 5314 const AttrVec *Attrs, 5315 const FunctionDecl *FD) { 5316 CheckReturnStackAddr(*this, RetValExp, lhsType, ReturnLoc); 5317 5318 // Check if the return value is null but should not be. 5319 if (Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs) && 5320 CheckNonNullExpr(*this, RetValExp)) 5321 Diag(ReturnLoc, diag::warn_null_ret) 5322 << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange(); 5323 5324 // C++11 [basic.stc.dynamic.allocation]p4: 5325 // If an allocation function declared with a non-throwing 5326 // exception-specification fails to allocate storage, it shall return 5327 // a null pointer. Any other allocation function that fails to allocate 5328 // storage shall indicate failure only by throwing an exception [...] 5329 if (FD) { 5330 OverloadedOperatorKind Op = FD->getOverloadedOperator(); 5331 if (Op == OO_New || Op == OO_Array_New) { 5332 const FunctionProtoType *Proto 5333 = FD->getType()->castAs<FunctionProtoType>(); 5334 if (!Proto->isNothrow(Context, /*ResultIfDependent*/true) && 5335 CheckNonNullExpr(*this, RetValExp)) 5336 Diag(ReturnLoc, diag::warn_operator_new_returns_null) 5337 << FD << getLangOpts().CPlusPlus11; 5338 } 5339 } 5340 } 5341 5342 //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// 5343 5344 /// Check for comparisons of floating point operands using != and ==. 5345 /// Issue a warning if these are no self-comparisons, as they are not likely 5346 /// to do what the programmer intended. 5347 void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { 5348 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); 5349 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); 5350 5351 // Special case: check for x == x (which is OK). 5352 // Do not emit warnings for such cases. 5353 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) 5354 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) 5355 if (DRL->getDecl() == DRR->getDecl()) 5356 return; 5357 5358 5359 // Special case: check for comparisons against literals that can be exactly 5360 // represented by APFloat. In such cases, do not emit a warning. This 5361 // is a heuristic: often comparison against such literals are used to 5362 // detect if a value in a variable has not changed. This clearly can 5363 // lead to false negatives. 5364 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { 5365 if (FLL->isExact()) 5366 return; 5367 } else 5368 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) 5369 if (FLR->isExact()) 5370 return; 5371 5372 // Check for comparisons with builtin types. 5373 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) 5374 if (CL->getBuiltinCallee()) 5375 return; 5376 5377 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) 5378 if (CR->getBuiltinCallee()) 5379 return; 5380 5381 // Emit the diagnostic. 5382 Diag(Loc, diag::warn_floatingpoint_eq) 5383 << LHS->getSourceRange() << RHS->getSourceRange(); 5384 } 5385 5386 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// 5387 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// 5388 5389 namespace { 5390 5391 /// Structure recording the 'active' range of an integer-valued 5392 /// expression. 5393 struct IntRange { 5394 /// The number of bits active in the int. 5395 unsigned Width; 5396 5397 /// True if the int is known not to have negative values. 5398 bool NonNegative; 5399 5400 IntRange(unsigned Width, bool NonNegative) 5401 : Width(Width), NonNegative(NonNegative) 5402 {} 5403 5404 /// Returns the range of the bool type. 5405 static IntRange forBoolType() { 5406 return IntRange(1, true); 5407 } 5408 5409 /// Returns the range of an opaque value of the given integral type. 5410 static IntRange forValueOfType(ASTContext &C, QualType T) { 5411 return forValueOfCanonicalType(C, 5412 T->getCanonicalTypeInternal().getTypePtr()); 5413 } 5414 5415 /// Returns the range of an opaque value of a canonical integral type. 5416 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { 5417 assert(T->isCanonicalUnqualified()); 5418 5419 if (const VectorType *VT = dyn_cast<VectorType>(T)) 5420 T = VT->getElementType().getTypePtr(); 5421 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 5422 T = CT->getElementType().getTypePtr(); 5423 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 5424 T = AT->getValueType().getTypePtr(); 5425 5426 // For enum types, use the known bit width of the enumerators. 5427 if (const EnumType *ET = dyn_cast<EnumType>(T)) { 5428 EnumDecl *Enum = ET->getDecl(); 5429 if (!Enum->isCompleteDefinition()) 5430 return IntRange(C.getIntWidth(QualType(T, 0)), false); 5431 5432 unsigned NumPositive = Enum->getNumPositiveBits(); 5433 unsigned NumNegative = Enum->getNumNegativeBits(); 5434 5435 if (NumNegative == 0) 5436 return IntRange(NumPositive, true/*NonNegative*/); 5437 else 5438 return IntRange(std::max(NumPositive + 1, NumNegative), 5439 false/*NonNegative*/); 5440 } 5441 5442 const BuiltinType *BT = cast<BuiltinType>(T); 5443 assert(BT->isInteger()); 5444 5445 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 5446 } 5447 5448 /// Returns the "target" range of a canonical integral type, i.e. 5449 /// the range of values expressible in the type. 5450 /// 5451 /// This matches forValueOfCanonicalType except that enums have the 5452 /// full range of their type, not the range of their enumerators. 5453 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { 5454 assert(T->isCanonicalUnqualified()); 5455 5456 if (const VectorType *VT = dyn_cast<VectorType>(T)) 5457 T = VT->getElementType().getTypePtr(); 5458 if (const ComplexType *CT = dyn_cast<ComplexType>(T)) 5459 T = CT->getElementType().getTypePtr(); 5460 if (const AtomicType *AT = dyn_cast<AtomicType>(T)) 5461 T = AT->getValueType().getTypePtr(); 5462 if (const EnumType *ET = dyn_cast<EnumType>(T)) 5463 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); 5464 5465 const BuiltinType *BT = cast<BuiltinType>(T); 5466 assert(BT->isInteger()); 5467 5468 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); 5469 } 5470 5471 /// Returns the supremum of two ranges: i.e. their conservative merge. 5472 static IntRange join(IntRange L, IntRange R) { 5473 return IntRange(std::max(L.Width, R.Width), 5474 L.NonNegative && R.NonNegative); 5475 } 5476 5477 /// Returns the infinum of two ranges: i.e. their aggressive merge. 5478 static IntRange meet(IntRange L, IntRange R) { 5479 return IntRange(std::min(L.Width, R.Width), 5480 L.NonNegative || R.NonNegative); 5481 } 5482 }; 5483 5484 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, 5485 unsigned MaxWidth) { 5486 if (value.isSigned() && value.isNegative()) 5487 return IntRange(value.getMinSignedBits(), false); 5488 5489 if (value.getBitWidth() > MaxWidth) 5490 value = value.trunc(MaxWidth); 5491 5492 // isNonNegative() just checks the sign bit without considering 5493 // signedness. 5494 return IntRange(value.getActiveBits(), true); 5495 } 5496 5497 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, 5498 unsigned MaxWidth) { 5499 if (result.isInt()) 5500 return GetValueRange(C, result.getInt(), MaxWidth); 5501 5502 if (result.isVector()) { 5503 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); 5504 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { 5505 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); 5506 R = IntRange::join(R, El); 5507 } 5508 return R; 5509 } 5510 5511 if (result.isComplexInt()) { 5512 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); 5513 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); 5514 return IntRange::join(R, I); 5515 } 5516 5517 // This can happen with lossless casts to intptr_t of "based" lvalues. 5518 // Assume it might use arbitrary bits. 5519 // FIXME: The only reason we need to pass the type in here is to get 5520 // the sign right on this one case. It would be nice if APValue 5521 // preserved this. 5522 assert(result.isLValue() || result.isAddrLabelDiff()); 5523 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); 5524 } 5525 5526 static QualType GetExprType(Expr *E) { 5527 QualType Ty = E->getType(); 5528 if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>()) 5529 Ty = AtomicRHS->getValueType(); 5530 return Ty; 5531 } 5532 5533 /// Pseudo-evaluate the given integer expression, estimating the 5534 /// range of values it might take. 5535 /// 5536 /// \param MaxWidth - the width to which the value will be truncated 5537 static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { 5538 E = E->IgnoreParens(); 5539 5540 // Try a full evaluation first. 5541 Expr::EvalResult result; 5542 if (E->EvaluateAsRValue(result, C)) 5543 return GetValueRange(C, result.Val, GetExprType(E), MaxWidth); 5544 5545 // I think we only want to look through implicit casts here; if the 5546 // user has an explicit widening cast, we should treat the value as 5547 // being of the new, wider type. 5548 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { 5549 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) 5550 return GetExprRange(C, CE->getSubExpr(), MaxWidth); 5551 5552 IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE)); 5553 5554 bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); 5555 5556 // Assume that non-integer casts can span the full range of the type. 5557 if (!isIntegerCast) 5558 return OutputTypeRange; 5559 5560 IntRange SubRange 5561 = GetExprRange(C, CE->getSubExpr(), 5562 std::min(MaxWidth, OutputTypeRange.Width)); 5563 5564 // Bail out if the subexpr's range is as wide as the cast type. 5565 if (SubRange.Width >= OutputTypeRange.Width) 5566 return OutputTypeRange; 5567 5568 // Otherwise, we take the smaller width, and we're non-negative if 5569 // either the output type or the subexpr is. 5570 return IntRange(SubRange.Width, 5571 SubRange.NonNegative || OutputTypeRange.NonNegative); 5572 } 5573 5574 if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 5575 // If we can fold the condition, just take that operand. 5576 bool CondResult; 5577 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) 5578 return GetExprRange(C, CondResult ? CO->getTrueExpr() 5579 : CO->getFalseExpr(), 5580 MaxWidth); 5581 5582 // Otherwise, conservatively merge. 5583 IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); 5584 IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); 5585 return IntRange::join(L, R); 5586 } 5587 5588 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 5589 switch (BO->getOpcode()) { 5590 5591 // Boolean-valued operations are single-bit and positive. 5592 case BO_LAnd: 5593 case BO_LOr: 5594 case BO_LT: 5595 case BO_GT: 5596 case BO_LE: 5597 case BO_GE: 5598 case BO_EQ: 5599 case BO_NE: 5600 return IntRange::forBoolType(); 5601 5602 // The type of the assignments is the type of the LHS, so the RHS 5603 // is not necessarily the same type. 5604 case BO_MulAssign: 5605 case BO_DivAssign: 5606 case BO_RemAssign: 5607 case BO_AddAssign: 5608 case BO_SubAssign: 5609 case BO_XorAssign: 5610 case BO_OrAssign: 5611 // TODO: bitfields? 5612 return IntRange::forValueOfType(C, GetExprType(E)); 5613 5614 // Simple assignments just pass through the RHS, which will have 5615 // been coerced to the LHS type. 5616 case BO_Assign: 5617 // TODO: bitfields? 5618 return GetExprRange(C, BO->getRHS(), MaxWidth); 5619 5620 // Operations with opaque sources are black-listed. 5621 case BO_PtrMemD: 5622 case BO_PtrMemI: 5623 return IntRange::forValueOfType(C, GetExprType(E)); 5624 5625 // Bitwise-and uses the *infinum* of the two source ranges. 5626 case BO_And: 5627 case BO_AndAssign: 5628 return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), 5629 GetExprRange(C, BO->getRHS(), MaxWidth)); 5630 5631 // Left shift gets black-listed based on a judgement call. 5632 case BO_Shl: 5633 // ...except that we want to treat '1 << (blah)' as logically 5634 // positive. It's an important idiom. 5635 if (IntegerLiteral *I 5636 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { 5637 if (I->getValue() == 1) { 5638 IntRange R = IntRange::forValueOfType(C, GetExprType(E)); 5639 return IntRange(R.Width, /*NonNegative*/ true); 5640 } 5641 } 5642 // fallthrough 5643 5644 case BO_ShlAssign: 5645 return IntRange::forValueOfType(C, GetExprType(E)); 5646 5647 // Right shift by a constant can narrow its left argument. 5648 case BO_Shr: 5649 case BO_ShrAssign: { 5650 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); 5651 5652 // If the shift amount is a positive constant, drop the width by 5653 // that much. 5654 llvm::APSInt shift; 5655 if (BO->getRHS()->isIntegerConstantExpr(shift, C) && 5656 shift.isNonNegative()) { 5657 unsigned zext = shift.getZExtValue(); 5658 if (zext >= L.Width) 5659 L.Width = (L.NonNegative ? 0 : 1); 5660 else 5661 L.Width -= zext; 5662 } 5663 5664 return L; 5665 } 5666 5667 // Comma acts as its right operand. 5668 case BO_Comma: 5669 return GetExprRange(C, BO->getRHS(), MaxWidth); 5670 5671 // Black-list pointer subtractions. 5672 case BO_Sub: 5673 if (BO->getLHS()->getType()->isPointerType()) 5674 return IntRange::forValueOfType(C, GetExprType(E)); 5675 break; 5676 5677 // The width of a division result is mostly determined by the size 5678 // of the LHS. 5679 case BO_Div: { 5680 // Don't 'pre-truncate' the operands. 5681 unsigned opWidth = C.getIntWidth(GetExprType(E)); 5682 IntRange L = GetExprRange(C, BO->getLHS(), opWidth); 5683 5684 // If the divisor is constant, use that. 5685 llvm::APSInt divisor; 5686 if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { 5687 unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) 5688 if (log2 >= L.Width) 5689 L.Width = (L.NonNegative ? 0 : 1); 5690 else 5691 L.Width = std::min(L.Width - log2, MaxWidth); 5692 return L; 5693 } 5694 5695 // Otherwise, just use the LHS's width. 5696 IntRange R = GetExprRange(C, BO->getRHS(), opWidth); 5697 return IntRange(L.Width, L.NonNegative && R.NonNegative); 5698 } 5699 5700 // The result of a remainder can't be larger than the result of 5701 // either side. 5702 case BO_Rem: { 5703 // Don't 'pre-truncate' the operands. 5704 unsigned opWidth = C.getIntWidth(GetExprType(E)); 5705 IntRange L = GetExprRange(C, BO->getLHS(), opWidth); 5706 IntRange R = GetExprRange(C, BO->getRHS(), opWidth); 5707 5708 IntRange meet = IntRange::meet(L, R); 5709 meet.Width = std::min(meet.Width, MaxWidth); 5710 return meet; 5711 } 5712 5713 // The default behavior is okay for these. 5714 case BO_Mul: 5715 case BO_Add: 5716 case BO_Xor: 5717 case BO_Or: 5718 break; 5719 } 5720 5721 // The default case is to treat the operation as if it were closed 5722 // on the narrowest type that encompasses both operands. 5723 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); 5724 IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); 5725 return IntRange::join(L, R); 5726 } 5727 5728 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 5729 switch (UO->getOpcode()) { 5730 // Boolean-valued operations are white-listed. 5731 case UO_LNot: 5732 return IntRange::forBoolType(); 5733 5734 // Operations with opaque sources are black-listed. 5735 case UO_Deref: 5736 case UO_AddrOf: // should be impossible 5737 return IntRange::forValueOfType(C, GetExprType(E)); 5738 5739 default: 5740 return GetExprRange(C, UO->getSubExpr(), MaxWidth); 5741 } 5742 } 5743 5744 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) 5745 return GetExprRange(C, OVE->getSourceExpr(), MaxWidth); 5746 5747 if (FieldDecl *BitField = E->getSourceBitField()) 5748 return IntRange(BitField->getBitWidthValue(C), 5749 BitField->getType()->isUnsignedIntegerOrEnumerationType()); 5750 5751 return IntRange::forValueOfType(C, GetExprType(E)); 5752 } 5753 5754 static IntRange GetExprRange(ASTContext &C, Expr *E) { 5755 return GetExprRange(C, E, C.getIntWidth(GetExprType(E))); 5756 } 5757 5758 /// Checks whether the given value, which currently has the given 5759 /// source semantics, has the same value when coerced through the 5760 /// target semantics. 5761 static bool IsSameFloatAfterCast(const llvm::APFloat &value, 5762 const llvm::fltSemantics &Src, 5763 const llvm::fltSemantics &Tgt) { 5764 llvm::APFloat truncated = value; 5765 5766 bool ignored; 5767 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); 5768 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); 5769 5770 return truncated.bitwiseIsEqual(value); 5771 } 5772 5773 /// Checks whether the given value, which currently has the given 5774 /// source semantics, has the same value when coerced through the 5775 /// target semantics. 5776 /// 5777 /// The value might be a vector of floats (or a complex number). 5778 static bool IsSameFloatAfterCast(const APValue &value, 5779 const llvm::fltSemantics &Src, 5780 const llvm::fltSemantics &Tgt) { 5781 if (value.isFloat()) 5782 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); 5783 5784 if (value.isVector()) { 5785 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) 5786 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) 5787 return false; 5788 return true; 5789 } 5790 5791 assert(value.isComplexFloat()); 5792 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && 5793 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); 5794 } 5795 5796 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); 5797 5798 static bool IsZero(Sema &S, Expr *E) { 5799 // Suppress cases where we are comparing against an enum constant. 5800 if (const DeclRefExpr *DR = 5801 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) 5802 if (isa<EnumConstantDecl>(DR->getDecl())) 5803 return false; 5804 5805 // Suppress cases where the '0' value is expanded from a macro. 5806 if (E->getLocStart().isMacroID()) 5807 return false; 5808 5809 llvm::APSInt Value; 5810 return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; 5811 } 5812 5813 static bool HasEnumType(Expr *E) { 5814 // Strip off implicit integral promotions. 5815 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 5816 if (ICE->getCastKind() != CK_IntegralCast && 5817 ICE->getCastKind() != CK_NoOp) 5818 break; 5819 E = ICE->getSubExpr(); 5820 } 5821 5822 return E->getType()->isEnumeralType(); 5823 } 5824 5825 static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { 5826 // Disable warning in template instantiations. 5827 if (!S.ActiveTemplateInstantiations.empty()) 5828 return; 5829 5830 BinaryOperatorKind op = E->getOpcode(); 5831 if (E->isValueDependent()) 5832 return; 5833 5834 if (op == BO_LT && IsZero(S, E->getRHS())) { 5835 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) 5836 << "< 0" << "false" << HasEnumType(E->getLHS()) 5837 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 5838 } else if (op == BO_GE && IsZero(S, E->getRHS())) { 5839 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) 5840 << ">= 0" << "true" << HasEnumType(E->getLHS()) 5841 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 5842 } else if (op == BO_GT && IsZero(S, E->getLHS())) { 5843 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) 5844 << "0 >" << "false" << HasEnumType(E->getRHS()) 5845 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 5846 } else if (op == BO_LE && IsZero(S, E->getLHS())) { 5847 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) 5848 << "0 <=" << "true" << HasEnumType(E->getRHS()) 5849 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); 5850 } 5851 } 5852 5853 static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E, 5854 Expr *Constant, Expr *Other, 5855 llvm::APSInt Value, 5856 bool RhsConstant) { 5857 // Disable warning in template instantiations. 5858 if (!S.ActiveTemplateInstantiations.empty()) 5859 return; 5860 5861 // TODO: Investigate using GetExprRange() to get tighter bounds 5862 // on the bit ranges. 5863 QualType OtherT = Other->getType(); 5864 if (const AtomicType *AT = dyn_cast<AtomicType>(OtherT)) 5865 OtherT = AT->getValueType(); 5866 IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT); 5867 unsigned OtherWidth = OtherRange.Width; 5868 5869 bool OtherIsBooleanType = Other->isKnownToHaveBooleanValue(); 5870 5871 // 0 values are handled later by CheckTrivialUnsignedComparison(). 5872 if ((Value == 0) && (!OtherIsBooleanType)) 5873 return; 5874 5875 BinaryOperatorKind op = E->getOpcode(); 5876 bool IsTrue = true; 5877 5878 // Used for diagnostic printout. 5879 enum { 5880 LiteralConstant = 0, 5881 CXXBoolLiteralTrue, 5882 CXXBoolLiteralFalse 5883 } LiteralOrBoolConstant = LiteralConstant; 5884 5885 if (!OtherIsBooleanType) { 5886 QualType ConstantT = Constant->getType(); 5887 QualType CommonT = E->getLHS()->getType(); 5888 5889 if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT)) 5890 return; 5891 assert((OtherT->isIntegerType() && ConstantT->isIntegerType()) && 5892 "comparison with non-integer type"); 5893 5894 bool ConstantSigned = ConstantT->isSignedIntegerType(); 5895 bool CommonSigned = CommonT->isSignedIntegerType(); 5896 5897 bool EqualityOnly = false; 5898 5899 if (CommonSigned) { 5900 // The common type is signed, therefore no signed to unsigned conversion. 5901 if (!OtherRange.NonNegative) { 5902 // Check that the constant is representable in type OtherT. 5903 if (ConstantSigned) { 5904 if (OtherWidth >= Value.getMinSignedBits()) 5905 return; 5906 } else { // !ConstantSigned 5907 if (OtherWidth >= Value.getActiveBits() + 1) 5908 return; 5909 } 5910 } else { // !OtherSigned 5911 // Check that the constant is representable in type OtherT. 5912 // Negative values are out of range. 5913 if (ConstantSigned) { 5914 if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits()) 5915 return; 5916 } else { // !ConstantSigned 5917 if (OtherWidth >= Value.getActiveBits()) 5918 return; 5919 } 5920 } 5921 } else { // !CommonSigned 5922 if (OtherRange.NonNegative) { 5923 if (OtherWidth >= Value.getActiveBits()) 5924 return; 5925 } else { // OtherSigned 5926 assert(!ConstantSigned && 5927 "Two signed types converted to unsigned types."); 5928 // Check to see if the constant is representable in OtherT. 5929 if (OtherWidth > Value.getActiveBits()) 5930 return; 5931 // Check to see if the constant is equivalent to a negative value 5932 // cast to CommonT. 5933 if (S.Context.getIntWidth(ConstantT) == 5934 S.Context.getIntWidth(CommonT) && 5935 Value.isNegative() && Value.getMinSignedBits() <= OtherWidth) 5936 return; 5937 // The constant value rests between values that OtherT can represent 5938 // after conversion. Relational comparison still works, but equality 5939 // comparisons will be tautological. 5940 EqualityOnly = true; 5941 } 5942 } 5943 5944 bool PositiveConstant = !ConstantSigned || Value.isNonNegative(); 5945 5946 if (op == BO_EQ || op == BO_NE) { 5947 IsTrue = op == BO_NE; 5948 } else if (EqualityOnly) { 5949 return; 5950 } else if (RhsConstant) { 5951 if (op == BO_GT || op == BO_GE) 5952 IsTrue = !PositiveConstant; 5953 else // op == BO_LT || op == BO_LE 5954 IsTrue = PositiveConstant; 5955 } else { 5956 if (op == BO_LT || op == BO_LE) 5957 IsTrue = !PositiveConstant; 5958 else // op == BO_GT || op == BO_GE 5959 IsTrue = PositiveConstant; 5960 } 5961 } else { 5962 // Other isKnownToHaveBooleanValue 5963 enum CompareBoolWithConstantResult { AFals, ATrue, Unkwn }; 5964 enum ConstantValue { LT_Zero, Zero, One, GT_One, SizeOfConstVal }; 5965 enum ConstantSide { Lhs, Rhs, SizeOfConstSides }; 5966 5967 static const struct LinkedConditions { 5968 CompareBoolWithConstantResult BO_LT_OP[SizeOfConstSides][SizeOfConstVal]; 5969 CompareBoolWithConstantResult BO_GT_OP[SizeOfConstSides][SizeOfConstVal]; 5970 CompareBoolWithConstantResult BO_LE_OP[SizeOfConstSides][SizeOfConstVal]; 5971 CompareBoolWithConstantResult BO_GE_OP[SizeOfConstSides][SizeOfConstVal]; 5972 CompareBoolWithConstantResult BO_EQ_OP[SizeOfConstSides][SizeOfConstVal]; 5973 CompareBoolWithConstantResult BO_NE_OP[SizeOfConstSides][SizeOfConstVal]; 5974 5975 } TruthTable = { 5976 // Constant on LHS. | Constant on RHS. | 5977 // LT_Zero| Zero | One |GT_One| LT_Zero| Zero | One |GT_One| 5978 { { ATrue, Unkwn, AFals, AFals }, { AFals, AFals, Unkwn, ATrue } }, 5979 { { AFals, AFals, Unkwn, ATrue }, { ATrue, Unkwn, AFals, AFals } }, 5980 { { ATrue, ATrue, Unkwn, AFals }, { AFals, Unkwn, ATrue, ATrue } }, 5981 { { AFals, Unkwn, ATrue, ATrue }, { ATrue, ATrue, Unkwn, AFals } }, 5982 { { AFals, Unkwn, Unkwn, AFals }, { AFals, Unkwn, Unkwn, AFals } }, 5983 { { ATrue, Unkwn, Unkwn, ATrue }, { ATrue, Unkwn, Unkwn, ATrue } } 5984 }; 5985 5986 bool ConstantIsBoolLiteral = isa<CXXBoolLiteralExpr>(Constant); 5987 5988 enum ConstantValue ConstVal = Zero; 5989 if (Value.isUnsigned() || Value.isNonNegative()) { 5990 if (Value == 0) { 5991 LiteralOrBoolConstant = 5992 ConstantIsBoolLiteral ? CXXBoolLiteralFalse : LiteralConstant; 5993 ConstVal = Zero; 5994 } else if (Value == 1) { 5995 LiteralOrBoolConstant = 5996 ConstantIsBoolLiteral ? CXXBoolLiteralTrue : LiteralConstant; 5997 ConstVal = One; 5998 } else { 5999 LiteralOrBoolConstant = LiteralConstant; 6000 ConstVal = GT_One; 6001 } 6002 } else { 6003 ConstVal = LT_Zero; 6004 } 6005 6006 CompareBoolWithConstantResult CmpRes; 6007 6008 switch (op) { 6009 case BO_LT: 6010 CmpRes = TruthTable.BO_LT_OP[RhsConstant][ConstVal]; 6011 break; 6012 case BO_GT: 6013 CmpRes = TruthTable.BO_GT_OP[RhsConstant][ConstVal]; 6014 break; 6015 case BO_LE: 6016 CmpRes = TruthTable.BO_LE_OP[RhsConstant][ConstVal]; 6017 break; 6018 case BO_GE: 6019 CmpRes = TruthTable.BO_GE_OP[RhsConstant][ConstVal]; 6020 break; 6021 case BO_EQ: 6022 CmpRes = TruthTable.BO_EQ_OP[RhsConstant][ConstVal]; 6023 break; 6024 case BO_NE: 6025 CmpRes = TruthTable.BO_NE_OP[RhsConstant][ConstVal]; 6026 break; 6027 default: 6028 CmpRes = Unkwn; 6029 break; 6030 } 6031 6032 if (CmpRes == AFals) { 6033 IsTrue = false; 6034 } else if (CmpRes == ATrue) { 6035 IsTrue = true; 6036 } else { 6037 return; 6038 } 6039 } 6040 6041 // If this is a comparison to an enum constant, include that 6042 // constant in the diagnostic. 6043 const EnumConstantDecl *ED = nullptr; 6044 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant)) 6045 ED = dyn_cast<EnumConstantDecl>(DR->getDecl()); 6046 6047 SmallString<64> PrettySourceValue; 6048 llvm::raw_svector_ostream OS(PrettySourceValue); 6049 if (ED) 6050 OS << '\'' << *ED << "' (" << Value << ")"; 6051 else 6052 OS << Value; 6053 6054 S.DiagRuntimeBehavior( 6055 E->getOperatorLoc(), E, 6056 S.PDiag(diag::warn_out_of_range_compare) 6057 << OS.str() << LiteralOrBoolConstant 6058 << OtherT << (OtherIsBooleanType && !OtherT->isBooleanType()) << IsTrue 6059 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange()); 6060 } 6061 6062 /// Analyze the operands of the given comparison. Implements the 6063 /// fallback case from AnalyzeComparison. 6064 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { 6065 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 6066 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 6067 } 6068 6069 /// \brief Implements -Wsign-compare. 6070 /// 6071 /// \param E the binary operator to check for warnings 6072 static void AnalyzeComparison(Sema &S, BinaryOperator *E) { 6073 // The type the comparison is being performed in. 6074 QualType T = E->getLHS()->getType(); 6075 6076 // Only analyze comparison operators where both sides have been converted to 6077 // the same type. 6078 if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())) 6079 return AnalyzeImpConvsInComparison(S, E); 6080 6081 // Don't analyze value-dependent comparisons directly. 6082 if (E->isValueDependent()) 6083 return AnalyzeImpConvsInComparison(S, E); 6084 6085 Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); 6086 Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); 6087 6088 bool IsComparisonConstant = false; 6089 6090 // Check whether an integer constant comparison results in a value 6091 // of 'true' or 'false'. 6092 if (T->isIntegralType(S.Context)) { 6093 llvm::APSInt RHSValue; 6094 bool IsRHSIntegralLiteral = 6095 RHS->isIntegerConstantExpr(RHSValue, S.Context); 6096 llvm::APSInt LHSValue; 6097 bool IsLHSIntegralLiteral = 6098 LHS->isIntegerConstantExpr(LHSValue, S.Context); 6099 if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral) 6100 DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true); 6101 else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral) 6102 DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false); 6103 else 6104 IsComparisonConstant = 6105 (IsRHSIntegralLiteral && IsLHSIntegralLiteral); 6106 } else if (!T->hasUnsignedIntegerRepresentation()) 6107 IsComparisonConstant = E->isIntegerConstantExpr(S.Context); 6108 6109 // We don't do anything special if this isn't an unsigned integral 6110 // comparison: we're only interested in integral comparisons, and 6111 // signed comparisons only happen in cases we don't care to warn about. 6112 // 6113 // We also don't care about value-dependent expressions or expressions 6114 // whose result is a constant. 6115 if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant) 6116 return AnalyzeImpConvsInComparison(S, E); 6117 6118 // Check to see if one of the (unmodified) operands is of different 6119 // signedness. 6120 Expr *signedOperand, *unsignedOperand; 6121 if (LHS->getType()->hasSignedIntegerRepresentation()) { 6122 assert(!RHS->getType()->hasSignedIntegerRepresentation() && 6123 "unsigned comparison between two signed integer expressions?"); 6124 signedOperand = LHS; 6125 unsignedOperand = RHS; 6126 } else if (RHS->getType()->hasSignedIntegerRepresentation()) { 6127 signedOperand = RHS; 6128 unsignedOperand = LHS; 6129 } else { 6130 CheckTrivialUnsignedComparison(S, E); 6131 return AnalyzeImpConvsInComparison(S, E); 6132 } 6133 6134 // Otherwise, calculate the effective range of the signed operand. 6135 IntRange signedRange = GetExprRange(S.Context, signedOperand); 6136 6137 // Go ahead and analyze implicit conversions in the operands. Note 6138 // that we skip the implicit conversions on both sides. 6139 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); 6140 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); 6141 6142 // If the signed range is non-negative, -Wsign-compare won't fire, 6143 // but we should still check for comparisons which are always true 6144 // or false. 6145 if (signedRange.NonNegative) 6146 return CheckTrivialUnsignedComparison(S, E); 6147 6148 // For (in)equality comparisons, if the unsigned operand is a 6149 // constant which cannot collide with a overflowed signed operand, 6150 // then reinterpreting the signed operand as unsigned will not 6151 // change the result of the comparison. 6152 if (E->isEqualityOp()) { 6153 unsigned comparisonWidth = S.Context.getIntWidth(T); 6154 IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); 6155 6156 // We should never be unable to prove that the unsigned operand is 6157 // non-negative. 6158 assert(unsignedRange.NonNegative && "unsigned range includes negative?"); 6159 6160 if (unsignedRange.Width < comparisonWidth) 6161 return; 6162 } 6163 6164 S.DiagRuntimeBehavior(E->getOperatorLoc(), E, 6165 S.PDiag(diag::warn_mixed_sign_comparison) 6166 << LHS->getType() << RHS->getType() 6167 << LHS->getSourceRange() << RHS->getSourceRange()); 6168 } 6169 6170 /// Analyzes an attempt to assign the given value to a bitfield. 6171 /// 6172 /// Returns true if there was something fishy about the attempt. 6173 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, 6174 SourceLocation InitLoc) { 6175 assert(Bitfield->isBitField()); 6176 if (Bitfield->isInvalidDecl()) 6177 return false; 6178 6179 // White-list bool bitfields. 6180 if (Bitfield->getType()->isBooleanType()) 6181 return false; 6182 6183 // Ignore value- or type-dependent expressions. 6184 if (Bitfield->getBitWidth()->isValueDependent() || 6185 Bitfield->getBitWidth()->isTypeDependent() || 6186 Init->isValueDependent() || 6187 Init->isTypeDependent()) 6188 return false; 6189 6190 Expr *OriginalInit = Init->IgnoreParenImpCasts(); 6191 6192 llvm::APSInt Value; 6193 if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) 6194 return false; 6195 6196 unsigned OriginalWidth = Value.getBitWidth(); 6197 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); 6198 6199 if (OriginalWidth <= FieldWidth) 6200 return false; 6201 6202 // Compute the value which the bitfield will contain. 6203 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); 6204 TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); 6205 6206 // Check whether the stored value is equal to the original value. 6207 TruncatedValue = TruncatedValue.extend(OriginalWidth); 6208 if (llvm::APSInt::isSameValue(Value, TruncatedValue)) 6209 return false; 6210 6211 // Special-case bitfields of width 1: booleans are naturally 0/1, and 6212 // therefore don't strictly fit into a signed bitfield of width 1. 6213 if (FieldWidth == 1 && Value == 1) 6214 return false; 6215 6216 std::string PrettyValue = Value.toString(10); 6217 std::string PrettyTrunc = TruncatedValue.toString(10); 6218 6219 S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) 6220 << PrettyValue << PrettyTrunc << OriginalInit->getType() 6221 << Init->getSourceRange(); 6222 6223 return true; 6224 } 6225 6226 /// Analyze the given simple or compound assignment for warning-worthy 6227 /// operations. 6228 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { 6229 // Just recurse on the LHS. 6230 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); 6231 6232 // We want to recurse on the RHS as normal unless we're assigning to 6233 // a bitfield. 6234 if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) { 6235 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), 6236 E->getOperatorLoc())) { 6237 // Recurse, ignoring any implicit conversions on the RHS. 6238 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), 6239 E->getOperatorLoc()); 6240 } 6241 } 6242 6243 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); 6244 } 6245 6246 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 6247 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, 6248 SourceLocation CContext, unsigned diag, 6249 bool pruneControlFlow = false) { 6250 if (pruneControlFlow) { 6251 S.DiagRuntimeBehavior(E->getExprLoc(), E, 6252 S.PDiag(diag) 6253 << SourceType << T << E->getSourceRange() 6254 << SourceRange(CContext)); 6255 return; 6256 } 6257 S.Diag(E->getExprLoc(), diag) 6258 << SourceType << T << E->getSourceRange() << SourceRange(CContext); 6259 } 6260 6261 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. 6262 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, 6263 SourceLocation CContext, unsigned diag, 6264 bool pruneControlFlow = false) { 6265 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); 6266 } 6267 6268 /// Diagnose an implicit cast from a literal expression. Does not warn when the 6269 /// cast wouldn't lose information. 6270 void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, 6271 SourceLocation CContext) { 6272 // Try to convert the literal exactly to an integer. If we can, don't warn. 6273 bool isExact = false; 6274 const llvm::APFloat &Value = FL->getValue(); 6275 llvm::APSInt IntegerValue(S.Context.getIntWidth(T), 6276 T->hasUnsignedIntegerRepresentation()); 6277 if (Value.convertToInteger(IntegerValue, 6278 llvm::APFloat::rmTowardZero, &isExact) 6279 == llvm::APFloat::opOK && isExact) 6280 return; 6281 6282 // FIXME: Force the precision of the source value down so we don't print 6283 // digits which are usually useless (we don't really care here if we 6284 // truncate a digit by accident in edge cases). Ideally, APFloat::toString 6285 // would automatically print the shortest representation, but it's a bit 6286 // tricky to implement. 6287 SmallString<16> PrettySourceValue; 6288 unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics()); 6289 precision = (precision * 59 + 195) / 196; 6290 Value.toString(PrettySourceValue, precision); 6291 6292 SmallString<16> PrettyTargetValue; 6293 if (T->isSpecificBuiltinType(BuiltinType::Bool)) 6294 PrettyTargetValue = IntegerValue == 0 ? "false" : "true"; 6295 else 6296 IntegerValue.toString(PrettyTargetValue); 6297 6298 S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) 6299 << FL->getType() << T.getUnqualifiedType() << PrettySourceValue 6300 << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); 6301 } 6302 6303 std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { 6304 if (!Range.Width) return "0"; 6305 6306 llvm::APSInt ValueInRange = Value; 6307 ValueInRange.setIsSigned(!Range.NonNegative); 6308 ValueInRange = ValueInRange.trunc(Range.Width); 6309 return ValueInRange.toString(10); 6310 } 6311 6312 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { 6313 if (!isa<ImplicitCastExpr>(Ex)) 6314 return false; 6315 6316 Expr *InnerE = Ex->IgnoreParenImpCasts(); 6317 const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); 6318 const Type *Source = 6319 S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); 6320 if (Target->isDependentType()) 6321 return false; 6322 6323 const BuiltinType *FloatCandidateBT = 6324 dyn_cast<BuiltinType>(ToBool ? Source : Target); 6325 const Type *BoolCandidateType = ToBool ? Target : Source; 6326 6327 return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && 6328 FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); 6329 } 6330 6331 void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, 6332 SourceLocation CC) { 6333 unsigned NumArgs = TheCall->getNumArgs(); 6334 for (unsigned i = 0; i < NumArgs; ++i) { 6335 Expr *CurrA = TheCall->getArg(i); 6336 if (!IsImplicitBoolFloatConversion(S, CurrA, true)) 6337 continue; 6338 6339 bool IsSwapped = ((i > 0) && 6340 IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); 6341 IsSwapped |= ((i < (NumArgs - 1)) && 6342 IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); 6343 if (IsSwapped) { 6344 // Warn on this floating-point to bool conversion. 6345 DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), 6346 CurrA->getType(), CC, 6347 diag::warn_impcast_floating_point_to_bool); 6348 } 6349 } 6350 } 6351 6352 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T, 6353 SourceLocation CC) { 6354 if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer, 6355 E->getExprLoc())) 6356 return; 6357 6358 // Check for NULL (GNUNull) or nullptr (CXX11_nullptr). 6359 const Expr::NullPointerConstantKind NullKind = 6360 E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull); 6361 if (NullKind != Expr::NPCK_GNUNull && NullKind != Expr::NPCK_CXX11_nullptr) 6362 return; 6363 6364 // Return if target type is a safe conversion. 6365 if (T->isAnyPointerType() || T->isBlockPointerType() || 6366 T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType()) 6367 return; 6368 6369 SourceLocation Loc = E->getSourceRange().getBegin(); 6370 6371 // __null is usually wrapped in a macro. Go up a macro if that is the case. 6372 if (NullKind == Expr::NPCK_GNUNull) { 6373 if (Loc.isMacroID()) 6374 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; 6375 } 6376 6377 // Only warn if the null and context location are in the same macro expansion. 6378 if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC)) 6379 return; 6380 6381 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) 6382 << (NullKind == Expr::NPCK_CXX11_nullptr) << T << clang::SourceRange(CC) 6383 << FixItHint::CreateReplacement(Loc, 6384 S.getFixItZeroLiteralForType(T, Loc)); 6385 } 6386 6387 void CheckImplicitConversion(Sema &S, Expr *E, QualType T, 6388 SourceLocation CC, bool *ICContext = nullptr) { 6389 if (E->isTypeDependent() || E->isValueDependent()) return; 6390 6391 const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); 6392 const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); 6393 if (Source == Target) return; 6394 if (Target->isDependentType()) return; 6395 6396 // If the conversion context location is invalid don't complain. We also 6397 // don't want to emit a warning if the issue occurs from the expansion of 6398 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we 6399 // delay this check as long as possible. Once we detect we are in that 6400 // scenario, we just return. 6401 if (CC.isInvalid()) 6402 return; 6403 6404 // Diagnose implicit casts to bool. 6405 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { 6406 if (isa<StringLiteral>(E)) 6407 // Warn on string literal to bool. Checks for string literals in logical 6408 // and expressions, for instance, assert(0 && "error here"), are 6409 // prevented by a check in AnalyzeImplicitConversions(). 6410 return DiagnoseImpCast(S, E, T, CC, 6411 diag::warn_impcast_string_literal_to_bool); 6412 if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) || 6413 isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) { 6414 // This covers the literal expressions that evaluate to Objective-C 6415 // objects. 6416 return DiagnoseImpCast(S, E, T, CC, 6417 diag::warn_impcast_objective_c_literal_to_bool); 6418 } 6419 if (Source->isPointerType() || Source->canDecayToPointerType()) { 6420 // Warn on pointer to bool conversion that is always true. 6421 S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false, 6422 SourceRange(CC)); 6423 } 6424 } 6425 6426 // Strip vector types. 6427 if (isa<VectorType>(Source)) { 6428 if (!isa<VectorType>(Target)) { 6429 if (S.SourceMgr.isInSystemMacro(CC)) 6430 return; 6431 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); 6432 } 6433 6434 // If the vector cast is cast between two vectors of the same size, it is 6435 // a bitcast, not a conversion. 6436 if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) 6437 return; 6438 6439 Source = cast<VectorType>(Source)->getElementType().getTypePtr(); 6440 Target = cast<VectorType>(Target)->getElementType().getTypePtr(); 6441 } 6442 if (auto VecTy = dyn_cast<VectorType>(Target)) 6443 Target = VecTy->getElementType().getTypePtr(); 6444 6445 // Strip complex types. 6446 if (isa<ComplexType>(Source)) { 6447 if (!isa<ComplexType>(Target)) { 6448 if (S.SourceMgr.isInSystemMacro(CC)) 6449 return; 6450 6451 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); 6452 } 6453 6454 Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); 6455 Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); 6456 } 6457 6458 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); 6459 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); 6460 6461 // If the source is floating point... 6462 if (SourceBT && SourceBT->isFloatingPoint()) { 6463 // ...and the target is floating point... 6464 if (TargetBT && TargetBT->isFloatingPoint()) { 6465 // ...then warn if we're dropping FP rank. 6466 6467 // Builtin FP kinds are ordered by increasing FP rank. 6468 if (SourceBT->getKind() > TargetBT->getKind()) { 6469 // Don't warn about float constants that are precisely 6470 // representable in the target type. 6471 Expr::EvalResult result; 6472 if (E->EvaluateAsRValue(result, S.Context)) { 6473 // Value might be a float, a float vector, or a float complex. 6474 if (IsSameFloatAfterCast(result.Val, 6475 S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), 6476 S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) 6477 return; 6478 } 6479 6480 if (S.SourceMgr.isInSystemMacro(CC)) 6481 return; 6482 6483 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); 6484 } 6485 return; 6486 } 6487 6488 // If the target is integral, always warn. 6489 if (TargetBT && TargetBT->isInteger()) { 6490 if (S.SourceMgr.isInSystemMacro(CC)) 6491 return; 6492 6493 Expr *InnerE = E->IgnoreParenImpCasts(); 6494 // We also want to warn on, e.g., "int i = -1.234" 6495 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) 6496 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) 6497 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); 6498 6499 if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { 6500 DiagnoseFloatingLiteralImpCast(S, FL, T, CC); 6501 } else { 6502 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); 6503 } 6504 } 6505 6506 // If the target is bool, warn if expr is a function or method call. 6507 if (Target->isSpecificBuiltinType(BuiltinType::Bool) && 6508 isa<CallExpr>(E)) { 6509 // Check last argument of function call to see if it is an 6510 // implicit cast from a type matching the type the result 6511 // is being cast to. 6512 CallExpr *CEx = cast<CallExpr>(E); 6513 unsigned NumArgs = CEx->getNumArgs(); 6514 if (NumArgs > 0) { 6515 Expr *LastA = CEx->getArg(NumArgs - 1); 6516 Expr *InnerE = LastA->IgnoreParenImpCasts(); 6517 const Type *InnerType = 6518 S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); 6519 if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) { 6520 // Warn on this floating-point to bool conversion 6521 DiagnoseImpCast(S, E, T, CC, 6522 diag::warn_impcast_floating_point_to_bool); 6523 } 6524 } 6525 } 6526 return; 6527 } 6528 6529 DiagnoseNullConversion(S, E, T, CC); 6530 6531 if (!Source->isIntegerType() || !Target->isIntegerType()) 6532 return; 6533 6534 // TODO: remove this early return once the false positives for constant->bool 6535 // in templates, macros, etc, are reduced or removed. 6536 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) 6537 return; 6538 6539 IntRange SourceRange = GetExprRange(S.Context, E); 6540 IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); 6541 6542 if (SourceRange.Width > TargetRange.Width) { 6543 // If the source is a constant, use a default-on diagnostic. 6544 // TODO: this should happen for bitfield stores, too. 6545 llvm::APSInt Value(32); 6546 if (E->isIntegerConstantExpr(Value, S.Context)) { 6547 if (S.SourceMgr.isInSystemMacro(CC)) 6548 return; 6549 6550 std::string PrettySourceValue = Value.toString(10); 6551 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); 6552 6553 S.DiagRuntimeBehavior(E->getExprLoc(), E, 6554 S.PDiag(diag::warn_impcast_integer_precision_constant) 6555 << PrettySourceValue << PrettyTargetValue 6556 << E->getType() << T << E->getSourceRange() 6557 << clang::SourceRange(CC)); 6558 return; 6559 } 6560 6561 // People want to build with -Wshorten-64-to-32 and not -Wconversion. 6562 if (S.SourceMgr.isInSystemMacro(CC)) 6563 return; 6564 6565 if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) 6566 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, 6567 /* pruneControlFlow */ true); 6568 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); 6569 } 6570 6571 if ((TargetRange.NonNegative && !SourceRange.NonNegative) || 6572 (!TargetRange.NonNegative && SourceRange.NonNegative && 6573 SourceRange.Width == TargetRange.Width)) { 6574 6575 if (S.SourceMgr.isInSystemMacro(CC)) 6576 return; 6577 6578 unsigned DiagID = diag::warn_impcast_integer_sign; 6579 6580 // Traditionally, gcc has warned about this under -Wsign-compare. 6581 // We also want to warn about it in -Wconversion. 6582 // So if -Wconversion is off, use a completely identical diagnostic 6583 // in the sign-compare group. 6584 // The conditional-checking code will 6585 if (ICContext) { 6586 DiagID = diag::warn_impcast_integer_sign_conditional; 6587 *ICContext = true; 6588 } 6589 6590 return DiagnoseImpCast(S, E, T, CC, DiagID); 6591 } 6592 6593 // Diagnose conversions between different enumeration types. 6594 // In C, we pretend that the type of an EnumConstantDecl is its enumeration 6595 // type, to give us better diagnostics. 6596 QualType SourceType = E->getType(); 6597 if (!S.getLangOpts().CPlusPlus) { 6598 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 6599 if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { 6600 EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); 6601 SourceType = S.Context.getTypeDeclType(Enum); 6602 Source = S.Context.getCanonicalType(SourceType).getTypePtr(); 6603 } 6604 } 6605 6606 if (const EnumType *SourceEnum = Source->getAs<EnumType>()) 6607 if (const EnumType *TargetEnum = Target->getAs<EnumType>()) 6608 if (SourceEnum->getDecl()->hasNameForLinkage() && 6609 TargetEnum->getDecl()->hasNameForLinkage() && 6610 SourceEnum != TargetEnum) { 6611 if (S.SourceMgr.isInSystemMacro(CC)) 6612 return; 6613 6614 return DiagnoseImpCast(S, E, SourceType, T, CC, 6615 diag::warn_impcast_different_enum_types); 6616 } 6617 6618 return; 6619 } 6620 6621 void CheckConditionalOperator(Sema &S, ConditionalOperator *E, 6622 SourceLocation CC, QualType T); 6623 6624 void CheckConditionalOperand(Sema &S, Expr *E, QualType T, 6625 SourceLocation CC, bool &ICContext) { 6626 E = E->IgnoreParenImpCasts(); 6627 6628 if (isa<ConditionalOperator>(E)) 6629 return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); 6630 6631 AnalyzeImplicitConversions(S, E, CC); 6632 if (E->getType() != T) 6633 return CheckImplicitConversion(S, E, T, CC, &ICContext); 6634 return; 6635 } 6636 6637 void CheckConditionalOperator(Sema &S, ConditionalOperator *E, 6638 SourceLocation CC, QualType T) { 6639 AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc()); 6640 6641 bool Suspicious = false; 6642 CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); 6643 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); 6644 6645 // If -Wconversion would have warned about either of the candidates 6646 // for a signedness conversion to the context type... 6647 if (!Suspicious) return; 6648 6649 // ...but it's currently ignored... 6650 if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC)) 6651 return; 6652 6653 // ...then check whether it would have warned about either of the 6654 // candidates for a signedness conversion to the condition type. 6655 if (E->getType() == T) return; 6656 6657 Suspicious = false; 6658 CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), 6659 E->getType(), CC, &Suspicious); 6660 if (!Suspicious) 6661 CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), 6662 E->getType(), CC, &Suspicious); 6663 } 6664 6665 /// CheckBoolLikeConversion - Check conversion of given expression to boolean. 6666 /// Input argument E is a logical expression. 6667 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) { 6668 if (S.getLangOpts().Bool) 6669 return; 6670 CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC); 6671 } 6672 6673 /// AnalyzeImplicitConversions - Find and report any interesting 6674 /// implicit conversions in the given expression. There are a couple 6675 /// of competing diagnostics here, -Wconversion and -Wsign-compare. 6676 void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { 6677 QualType T = OrigE->getType(); 6678 Expr *E = OrigE->IgnoreParenImpCasts(); 6679 6680 if (E->isTypeDependent() || E->isValueDependent()) 6681 return; 6682 6683 // For conditional operators, we analyze the arguments as if they 6684 // were being fed directly into the output. 6685 if (isa<ConditionalOperator>(E)) { 6686 ConditionalOperator *CO = cast<ConditionalOperator>(E); 6687 CheckConditionalOperator(S, CO, CC, T); 6688 return; 6689 } 6690 6691 // Check implicit argument conversions for function calls. 6692 if (CallExpr *Call = dyn_cast<CallExpr>(E)) 6693 CheckImplicitArgumentConversions(S, Call, CC); 6694 6695 // Go ahead and check any implicit conversions we might have skipped. 6696 // The non-canonical typecheck is just an optimization; 6697 // CheckImplicitConversion will filter out dead implicit conversions. 6698 if (E->getType() != T) 6699 CheckImplicitConversion(S, E, T, CC); 6700 6701 // Now continue drilling into this expression. 6702 6703 if (PseudoObjectExpr * POE = dyn_cast<PseudoObjectExpr>(E)) { 6704 if (POE->getResultExpr()) 6705 E = POE->getResultExpr(); 6706 } 6707 6708 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) 6709 return AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC); 6710 6711 // Skip past explicit casts. 6712 if (isa<ExplicitCastExpr>(E)) { 6713 E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); 6714 return AnalyzeImplicitConversions(S, E, CC); 6715 } 6716 6717 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 6718 // Do a somewhat different check with comparison operators. 6719 if (BO->isComparisonOp()) 6720 return AnalyzeComparison(S, BO); 6721 6722 // And with simple assignments. 6723 if (BO->getOpcode() == BO_Assign) 6724 return AnalyzeAssignment(S, BO); 6725 } 6726 6727 // These break the otherwise-useful invariant below. Fortunately, 6728 // we don't really need to recurse into them, because any internal 6729 // expressions should have been analyzed already when they were 6730 // built into statements. 6731 if (isa<StmtExpr>(E)) return; 6732 6733 // Don't descend into unevaluated contexts. 6734 if (isa<UnaryExprOrTypeTraitExpr>(E)) return; 6735 6736 // Now just recurse over the expression's children. 6737 CC = E->getExprLoc(); 6738 BinaryOperator *BO = dyn_cast<BinaryOperator>(E); 6739 bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd; 6740 for (Stmt::child_range I = E->children(); I; ++I) { 6741 Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); 6742 if (!ChildExpr) 6743 continue; 6744 6745 if (IsLogicalAndOperator && 6746 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) 6747 // Ignore checking string literals that are in logical and operators. 6748 // This is a common pattern for asserts. 6749 continue; 6750 AnalyzeImplicitConversions(S, ChildExpr, CC); 6751 } 6752 6753 if (BO && BO->isLogicalOp()) { 6754 Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts(); 6755 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 6756 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 6757 6758 SubExpr = BO->getRHS()->IgnoreParenImpCasts(); 6759 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr)) 6760 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc()); 6761 } 6762 6763 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E)) 6764 if (U->getOpcode() == UO_LNot) 6765 ::CheckBoolLikeConversion(S, U->getSubExpr(), CC); 6766 } 6767 6768 } // end anonymous namespace 6769 6770 enum { 6771 AddressOf, 6772 FunctionPointer, 6773 ArrayPointer 6774 }; 6775 6776 // Helper function for Sema::DiagnoseAlwaysNonNullPointer. 6777 // Returns true when emitting a warning about taking the address of a reference. 6778 static bool CheckForReference(Sema &SemaRef, const Expr *E, 6779 PartialDiagnostic PD) { 6780 E = E->IgnoreParenImpCasts(); 6781 6782 const FunctionDecl *FD = nullptr; 6783 6784 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 6785 if (!DRE->getDecl()->getType()->isReferenceType()) 6786 return false; 6787 } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) { 6788 if (!M->getMemberDecl()->getType()->isReferenceType()) 6789 return false; 6790 } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) { 6791 if (!Call->getCallReturnType()->isReferenceType()) 6792 return false; 6793 FD = Call->getDirectCallee(); 6794 } else { 6795 return false; 6796 } 6797 6798 SemaRef.Diag(E->getExprLoc(), PD); 6799 6800 // If possible, point to location of function. 6801 if (FD) { 6802 SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD; 6803 } 6804 6805 return true; 6806 } 6807 6808 // Returns true if the SourceLocation is expanded from any macro body. 6809 // Returns false if the SourceLocation is invalid, is from not in a macro 6810 // expansion, or is from expanded from a top-level macro argument. 6811 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) { 6812 if (Loc.isInvalid()) 6813 return false; 6814 6815 while (Loc.isMacroID()) { 6816 if (SM.isMacroBodyExpansion(Loc)) 6817 return true; 6818 Loc = SM.getImmediateMacroCallerLoc(Loc); 6819 } 6820 6821 return false; 6822 } 6823 6824 /// \brief Diagnose pointers that are always non-null. 6825 /// \param E the expression containing the pointer 6826 /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is 6827 /// compared to a null pointer 6828 /// \param IsEqual True when the comparison is equal to a null pointer 6829 /// \param Range Extra SourceRange to highlight in the diagnostic 6830 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E, 6831 Expr::NullPointerConstantKind NullKind, 6832 bool IsEqual, SourceRange Range) { 6833 if (!E) 6834 return; 6835 6836 // Don't warn inside macros. 6837 if (E->getExprLoc().isMacroID()) { 6838 const SourceManager &SM = getSourceManager(); 6839 if (IsInAnyMacroBody(SM, E->getExprLoc()) || 6840 IsInAnyMacroBody(SM, Range.getBegin())) 6841 return; 6842 } 6843 E = E->IgnoreImpCasts(); 6844 6845 const bool IsCompare = NullKind != Expr::NPCK_NotNull; 6846 6847 if (isa<CXXThisExpr>(E)) { 6848 unsigned DiagID = IsCompare ? diag::warn_this_null_compare 6849 : diag::warn_this_bool_conversion; 6850 Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual; 6851 return; 6852 } 6853 6854 bool IsAddressOf = false; 6855 6856 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 6857 if (UO->getOpcode() != UO_AddrOf) 6858 return; 6859 IsAddressOf = true; 6860 E = UO->getSubExpr(); 6861 } 6862 6863 if (IsAddressOf) { 6864 unsigned DiagID = IsCompare 6865 ? diag::warn_address_of_reference_null_compare 6866 : diag::warn_address_of_reference_bool_conversion; 6867 PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range 6868 << IsEqual; 6869 if (CheckForReference(*this, E, PD)) { 6870 return; 6871 } 6872 } 6873 6874 // Expect to find a single Decl. Skip anything more complicated. 6875 ValueDecl *D = nullptr; 6876 if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) { 6877 D = R->getDecl(); 6878 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { 6879 D = M->getMemberDecl(); 6880 } 6881 6882 // Weak Decls can be null. 6883 if (!D || D->isWeak()) 6884 return; 6885 6886 // Check for parameter decl with nonnull attribute 6887 if (const ParmVarDecl* PV = dyn_cast<ParmVarDecl>(D)) { 6888 if (getCurFunction() && !getCurFunction()->ModifiedNonNullParams.count(PV)) 6889 if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { 6890 unsigned NumArgs = FD->getNumParams(); 6891 llvm::SmallBitVector AttrNonNull(NumArgs); 6892 for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) { 6893 if (!NonNull->args_size()) { 6894 AttrNonNull.set(0, NumArgs); 6895 break; 6896 } 6897 for (unsigned Val : NonNull->args()) { 6898 if (Val >= NumArgs) 6899 continue; 6900 AttrNonNull.set(Val); 6901 } 6902 } 6903 if (!AttrNonNull.empty()) 6904 for (unsigned i = 0; i < NumArgs; ++i) 6905 if (FD->getParamDecl(i) == PV && 6906 (AttrNonNull[i] || PV->hasAttr<NonNullAttr>())) { 6907 std::string Str; 6908 llvm::raw_string_ostream S(Str); 6909 E->printPretty(S, nullptr, getPrintingPolicy()); 6910 unsigned DiagID = IsCompare ? diag::warn_nonnull_parameter_compare 6911 : diag::warn_cast_nonnull_to_bool; 6912 Diag(E->getExprLoc(), DiagID) << S.str() << E->getSourceRange() 6913 << Range << IsEqual; 6914 return; 6915 } 6916 } 6917 } 6918 6919 QualType T = D->getType(); 6920 const bool IsArray = T->isArrayType(); 6921 const bool IsFunction = T->isFunctionType(); 6922 6923 // Address of function is used to silence the function warning. 6924 if (IsAddressOf && IsFunction) { 6925 return; 6926 } 6927 6928 // Found nothing. 6929 if (!IsAddressOf && !IsFunction && !IsArray) 6930 return; 6931 6932 // Pretty print the expression for the diagnostic. 6933 std::string Str; 6934 llvm::raw_string_ostream S(Str); 6935 E->printPretty(S, nullptr, getPrintingPolicy()); 6936 6937 unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare 6938 : diag::warn_impcast_pointer_to_bool; 6939 unsigned DiagType; 6940 if (IsAddressOf) 6941 DiagType = AddressOf; 6942 else if (IsFunction) 6943 DiagType = FunctionPointer; 6944 else if (IsArray) 6945 DiagType = ArrayPointer; 6946 else 6947 llvm_unreachable("Could not determine diagnostic."); 6948 Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange() 6949 << Range << IsEqual; 6950 6951 if (!IsFunction) 6952 return; 6953 6954 // Suggest '&' to silence the function warning. 6955 Diag(E->getExprLoc(), diag::note_function_warning_silence) 6956 << FixItHint::CreateInsertion(E->getLocStart(), "&"); 6957 6958 // Check to see if '()' fixit should be emitted. 6959 QualType ReturnType; 6960 UnresolvedSet<4> NonTemplateOverloads; 6961 tryExprAsCall(*E, ReturnType, NonTemplateOverloads); 6962 if (ReturnType.isNull()) 6963 return; 6964 6965 if (IsCompare) { 6966 // There are two cases here. If there is null constant, the only suggest 6967 // for a pointer return type. If the null is 0, then suggest if the return 6968 // type is a pointer or an integer type. 6969 if (!ReturnType->isPointerType()) { 6970 if (NullKind == Expr::NPCK_ZeroExpression || 6971 NullKind == Expr::NPCK_ZeroLiteral) { 6972 if (!ReturnType->isIntegerType()) 6973 return; 6974 } else { 6975 return; 6976 } 6977 } 6978 } else { // !IsCompare 6979 // For function to bool, only suggest if the function pointer has bool 6980 // return type. 6981 if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) 6982 return; 6983 } 6984 Diag(E->getExprLoc(), diag::note_function_to_function_call) 6985 << FixItHint::CreateInsertion(getLocForEndOfToken(E->getLocEnd()), "()"); 6986 } 6987 6988 6989 /// Diagnoses "dangerous" implicit conversions within the given 6990 /// expression (which is a full expression). Implements -Wconversion 6991 /// and -Wsign-compare. 6992 /// 6993 /// \param CC the "context" location of the implicit conversion, i.e. 6994 /// the most location of the syntactic entity requiring the implicit 6995 /// conversion 6996 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { 6997 // Don't diagnose in unevaluated contexts. 6998 if (isUnevaluatedContext()) 6999 return; 7000 7001 // Don't diagnose for value- or type-dependent expressions. 7002 if (E->isTypeDependent() || E->isValueDependent()) 7003 return; 7004 7005 // Check for array bounds violations in cases where the check isn't triggered 7006 // elsewhere for other Expr types (like BinaryOperators), e.g. when an 7007 // ArraySubscriptExpr is on the RHS of a variable initialization. 7008 CheckArrayAccess(E); 7009 7010 // This is not the right CC for (e.g.) a variable initialization. 7011 AnalyzeImplicitConversions(*this, E, CC); 7012 } 7013 7014 /// CheckBoolLikeConversion - Check conversion of given expression to boolean. 7015 /// Input argument E is a logical expression. 7016 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { 7017 ::CheckBoolLikeConversion(*this, E, CC); 7018 } 7019 7020 /// Diagnose when expression is an integer constant expression and its evaluation 7021 /// results in integer overflow 7022 void Sema::CheckForIntOverflow (Expr *E) { 7023 if (isa<BinaryOperator>(E->IgnoreParenCasts())) 7024 E->IgnoreParenCasts()->EvaluateForOverflow(Context); 7025 } 7026 7027 namespace { 7028 /// \brief Visitor for expressions which looks for unsequenced operations on the 7029 /// same object. 7030 class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> { 7031 typedef EvaluatedExprVisitor<SequenceChecker> Base; 7032 7033 /// \brief A tree of sequenced regions within an expression. Two regions are 7034 /// unsequenced if one is an ancestor or a descendent of the other. When we 7035 /// finish processing an expression with sequencing, such as a comma 7036 /// expression, we fold its tree nodes into its parent, since they are 7037 /// unsequenced with respect to nodes we will visit later. 7038 class SequenceTree { 7039 struct Value { 7040 explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {} 7041 unsigned Parent : 31; 7042 bool Merged : 1; 7043 }; 7044 SmallVector<Value, 8> Values; 7045 7046 public: 7047 /// \brief A region within an expression which may be sequenced with respect 7048 /// to some other region. 7049 class Seq { 7050 explicit Seq(unsigned N) : Index(N) {} 7051 unsigned Index; 7052 friend class SequenceTree; 7053 public: 7054 Seq() : Index(0) {} 7055 }; 7056 7057 SequenceTree() { Values.push_back(Value(0)); } 7058 Seq root() const { return Seq(0); } 7059 7060 /// \brief Create a new sequence of operations, which is an unsequenced 7061 /// subset of \p Parent. This sequence of operations is sequenced with 7062 /// respect to other children of \p Parent. 7063 Seq allocate(Seq Parent) { 7064 Values.push_back(Value(Parent.Index)); 7065 return Seq(Values.size() - 1); 7066 } 7067 7068 /// \brief Merge a sequence of operations into its parent. 7069 void merge(Seq S) { 7070 Values[S.Index].Merged = true; 7071 } 7072 7073 /// \brief Determine whether two operations are unsequenced. This operation 7074 /// is asymmetric: \p Cur should be the more recent sequence, and \p Old 7075 /// should have been merged into its parent as appropriate. 7076 bool isUnsequenced(Seq Cur, Seq Old) { 7077 unsigned C = representative(Cur.Index); 7078 unsigned Target = representative(Old.Index); 7079 while (C >= Target) { 7080 if (C == Target) 7081 return true; 7082 C = Values[C].Parent; 7083 } 7084 return false; 7085 } 7086 7087 private: 7088 /// \brief Pick a representative for a sequence. 7089 unsigned representative(unsigned K) { 7090 if (Values[K].Merged) 7091 // Perform path compression as we go. 7092 return Values[K].Parent = representative(Values[K].Parent); 7093 return K; 7094 } 7095 }; 7096 7097 /// An object for which we can track unsequenced uses. 7098 typedef NamedDecl *Object; 7099 7100 /// Different flavors of object usage which we track. We only track the 7101 /// least-sequenced usage of each kind. 7102 enum UsageKind { 7103 /// A read of an object. Multiple unsequenced reads are OK. 7104 UK_Use, 7105 /// A modification of an object which is sequenced before the value 7106 /// computation of the expression, such as ++n in C++. 7107 UK_ModAsValue, 7108 /// A modification of an object which is not sequenced before the value 7109 /// computation of the expression, such as n++. 7110 UK_ModAsSideEffect, 7111 7112 UK_Count = UK_ModAsSideEffect + 1 7113 }; 7114 7115 struct Usage { 7116 Usage() : Use(nullptr), Seq() {} 7117 Expr *Use; 7118 SequenceTree::Seq Seq; 7119 }; 7120 7121 struct UsageInfo { 7122 UsageInfo() : Diagnosed(false) {} 7123 Usage Uses[UK_Count]; 7124 /// Have we issued a diagnostic for this variable already? 7125 bool Diagnosed; 7126 }; 7127 typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap; 7128 7129 Sema &SemaRef; 7130 /// Sequenced regions within the expression. 7131 SequenceTree Tree; 7132 /// Declaration modifications and references which we have seen. 7133 UsageInfoMap UsageMap; 7134 /// The region we are currently within. 7135 SequenceTree::Seq Region; 7136 /// Filled in with declarations which were modified as a side-effect 7137 /// (that is, post-increment operations). 7138 SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect; 7139 /// Expressions to check later. We defer checking these to reduce 7140 /// stack usage. 7141 SmallVectorImpl<Expr *> &WorkList; 7142 7143 /// RAII object wrapping the visitation of a sequenced subexpression of an 7144 /// expression. At the end of this process, the side-effects of the evaluation 7145 /// become sequenced with respect to the value computation of the result, so 7146 /// we downgrade any UK_ModAsSideEffect within the evaluation to 7147 /// UK_ModAsValue. 7148 struct SequencedSubexpression { 7149 SequencedSubexpression(SequenceChecker &Self) 7150 : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) { 7151 Self.ModAsSideEffect = &ModAsSideEffect; 7152 } 7153 ~SequencedSubexpression() { 7154 for (auto MI = ModAsSideEffect.rbegin(), ME = ModAsSideEffect.rend(); 7155 MI != ME; ++MI) { 7156 UsageInfo &U = Self.UsageMap[MI->first]; 7157 auto &SideEffectUsage = U.Uses[UK_ModAsSideEffect]; 7158 Self.addUsage(U, MI->first, SideEffectUsage.Use, UK_ModAsValue); 7159 SideEffectUsage = MI->second; 7160 } 7161 Self.ModAsSideEffect = OldModAsSideEffect; 7162 } 7163 7164 SequenceChecker &Self; 7165 SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect; 7166 SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect; 7167 }; 7168 7169 /// RAII object wrapping the visitation of a subexpression which we might 7170 /// choose to evaluate as a constant. If any subexpression is evaluated and 7171 /// found to be non-constant, this allows us to suppress the evaluation of 7172 /// the outer expression. 7173 class EvaluationTracker { 7174 public: 7175 EvaluationTracker(SequenceChecker &Self) 7176 : Self(Self), Prev(Self.EvalTracker), EvalOK(true) { 7177 Self.EvalTracker = this; 7178 } 7179 ~EvaluationTracker() { 7180 Self.EvalTracker = Prev; 7181 if (Prev) 7182 Prev->EvalOK &= EvalOK; 7183 } 7184 7185 bool evaluate(const Expr *E, bool &Result) { 7186 if (!EvalOK || E->isValueDependent()) 7187 return false; 7188 EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context); 7189 return EvalOK; 7190 } 7191 7192 private: 7193 SequenceChecker &Self; 7194 EvaluationTracker *Prev; 7195 bool EvalOK; 7196 } *EvalTracker; 7197 7198 /// \brief Find the object which is produced by the specified expression, 7199 /// if any. 7200 Object getObject(Expr *E, bool Mod) const { 7201 E = E->IgnoreParenCasts(); 7202 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 7203 if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec)) 7204 return getObject(UO->getSubExpr(), Mod); 7205 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 7206 if (BO->getOpcode() == BO_Comma) 7207 return getObject(BO->getRHS(), Mod); 7208 if (Mod && BO->isAssignmentOp()) 7209 return getObject(BO->getLHS(), Mod); 7210 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { 7211 // FIXME: Check for more interesting cases, like "x.n = ++x.n". 7212 if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts())) 7213 return ME->getMemberDecl(); 7214 } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 7215 // FIXME: If this is a reference, map through to its value. 7216 return DRE->getDecl(); 7217 return nullptr; 7218 } 7219 7220 /// \brief Note that an object was modified or used by an expression. 7221 void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) { 7222 Usage &U = UI.Uses[UK]; 7223 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) { 7224 if (UK == UK_ModAsSideEffect && ModAsSideEffect) 7225 ModAsSideEffect->push_back(std::make_pair(O, U)); 7226 U.Use = Ref; 7227 U.Seq = Region; 7228 } 7229 } 7230 /// \brief Check whether a modification or use conflicts with a prior usage. 7231 void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind, 7232 bool IsModMod) { 7233 if (UI.Diagnosed) 7234 return; 7235 7236 const Usage &U = UI.Uses[OtherKind]; 7237 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) 7238 return; 7239 7240 Expr *Mod = U.Use; 7241 Expr *ModOrUse = Ref; 7242 if (OtherKind == UK_Use) 7243 std::swap(Mod, ModOrUse); 7244 7245 SemaRef.Diag(Mod->getExprLoc(), 7246 IsModMod ? diag::warn_unsequenced_mod_mod 7247 : diag::warn_unsequenced_mod_use) 7248 << O << SourceRange(ModOrUse->getExprLoc()); 7249 UI.Diagnosed = true; 7250 } 7251 7252 void notePreUse(Object O, Expr *Use) { 7253 UsageInfo &U = UsageMap[O]; 7254 // Uses conflict with other modifications. 7255 checkUsage(O, U, Use, UK_ModAsValue, false); 7256 } 7257 void notePostUse(Object O, Expr *Use) { 7258 UsageInfo &U = UsageMap[O]; 7259 checkUsage(O, U, Use, UK_ModAsSideEffect, false); 7260 addUsage(U, O, Use, UK_Use); 7261 } 7262 7263 void notePreMod(Object O, Expr *Mod) { 7264 UsageInfo &U = UsageMap[O]; 7265 // Modifications conflict with other modifications and with uses. 7266 checkUsage(O, U, Mod, UK_ModAsValue, true); 7267 checkUsage(O, U, Mod, UK_Use, false); 7268 } 7269 void notePostMod(Object O, Expr *Use, UsageKind UK) { 7270 UsageInfo &U = UsageMap[O]; 7271 checkUsage(O, U, Use, UK_ModAsSideEffect, true); 7272 addUsage(U, O, Use, UK); 7273 } 7274 7275 public: 7276 SequenceChecker(Sema &S, Expr *E, SmallVectorImpl<Expr *> &WorkList) 7277 : Base(S.Context), SemaRef(S), Region(Tree.root()), 7278 ModAsSideEffect(nullptr), WorkList(WorkList), EvalTracker(nullptr) { 7279 Visit(E); 7280 } 7281 7282 void VisitStmt(Stmt *S) { 7283 // Skip all statements which aren't expressions for now. 7284 } 7285 7286 void VisitExpr(Expr *E) { 7287 // By default, just recurse to evaluated subexpressions. 7288 Base::VisitStmt(E); 7289 } 7290 7291 void VisitCastExpr(CastExpr *E) { 7292 Object O = Object(); 7293 if (E->getCastKind() == CK_LValueToRValue) 7294 O = getObject(E->getSubExpr(), false); 7295 7296 if (O) 7297 notePreUse(O, E); 7298 VisitExpr(E); 7299 if (O) 7300 notePostUse(O, E); 7301 } 7302 7303 void VisitBinComma(BinaryOperator *BO) { 7304 // C++11 [expr.comma]p1: 7305 // Every value computation and side effect associated with the left 7306 // expression is sequenced before every value computation and side 7307 // effect associated with the right expression. 7308 SequenceTree::Seq LHS = Tree.allocate(Region); 7309 SequenceTree::Seq RHS = Tree.allocate(Region); 7310 SequenceTree::Seq OldRegion = Region; 7311 7312 { 7313 SequencedSubexpression SeqLHS(*this); 7314 Region = LHS; 7315 Visit(BO->getLHS()); 7316 } 7317 7318 Region = RHS; 7319 Visit(BO->getRHS()); 7320 7321 Region = OldRegion; 7322 7323 // Forget that LHS and RHS are sequenced. They are both unsequenced 7324 // with respect to other stuff. 7325 Tree.merge(LHS); 7326 Tree.merge(RHS); 7327 } 7328 7329 void VisitBinAssign(BinaryOperator *BO) { 7330 // The modification is sequenced after the value computation of the LHS 7331 // and RHS, so check it before inspecting the operands and update the 7332 // map afterwards. 7333 Object O = getObject(BO->getLHS(), true); 7334 if (!O) 7335 return VisitExpr(BO); 7336 7337 notePreMod(O, BO); 7338 7339 // C++11 [expr.ass]p7: 7340 // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated 7341 // only once. 7342 // 7343 // Therefore, for a compound assignment operator, O is considered used 7344 // everywhere except within the evaluation of E1 itself. 7345 if (isa<CompoundAssignOperator>(BO)) 7346 notePreUse(O, BO); 7347 7348 Visit(BO->getLHS()); 7349 7350 if (isa<CompoundAssignOperator>(BO)) 7351 notePostUse(O, BO); 7352 7353 Visit(BO->getRHS()); 7354 7355 // C++11 [expr.ass]p1: 7356 // the assignment is sequenced [...] before the value computation of the 7357 // assignment expression. 7358 // C11 6.5.16/3 has no such rule. 7359 notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 7360 : UK_ModAsSideEffect); 7361 } 7362 void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) { 7363 VisitBinAssign(CAO); 7364 } 7365 7366 void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 7367 void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); } 7368 void VisitUnaryPreIncDec(UnaryOperator *UO) { 7369 Object O = getObject(UO->getSubExpr(), true); 7370 if (!O) 7371 return VisitExpr(UO); 7372 7373 notePreMod(O, UO); 7374 Visit(UO->getSubExpr()); 7375 // C++11 [expr.pre.incr]p1: 7376 // the expression ++x is equivalent to x+=1 7377 notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue 7378 : UK_ModAsSideEffect); 7379 } 7380 7381 void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 7382 void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); } 7383 void VisitUnaryPostIncDec(UnaryOperator *UO) { 7384 Object O = getObject(UO->getSubExpr(), true); 7385 if (!O) 7386 return VisitExpr(UO); 7387 7388 notePreMod(O, UO); 7389 Visit(UO->getSubExpr()); 7390 notePostMod(O, UO, UK_ModAsSideEffect); 7391 } 7392 7393 /// Don't visit the RHS of '&&' or '||' if it might not be evaluated. 7394 void VisitBinLOr(BinaryOperator *BO) { 7395 // The side-effects of the LHS of an '&&' are sequenced before the 7396 // value computation of the RHS, and hence before the value computation 7397 // of the '&&' itself, unless the LHS evaluates to zero. We treat them 7398 // as if they were unconditionally sequenced. 7399 EvaluationTracker Eval(*this); 7400 { 7401 SequencedSubexpression Sequenced(*this); 7402 Visit(BO->getLHS()); 7403 } 7404 7405 bool Result; 7406 if (Eval.evaluate(BO->getLHS(), Result)) { 7407 if (!Result) 7408 Visit(BO->getRHS()); 7409 } else { 7410 // Check for unsequenced operations in the RHS, treating it as an 7411 // entirely separate evaluation. 7412 // 7413 // FIXME: If there are operations in the RHS which are unsequenced 7414 // with respect to operations outside the RHS, and those operations 7415 // are unconditionally evaluated, diagnose them. 7416 WorkList.push_back(BO->getRHS()); 7417 } 7418 } 7419 void VisitBinLAnd(BinaryOperator *BO) { 7420 EvaluationTracker Eval(*this); 7421 { 7422 SequencedSubexpression Sequenced(*this); 7423 Visit(BO->getLHS()); 7424 } 7425 7426 bool Result; 7427 if (Eval.evaluate(BO->getLHS(), Result)) { 7428 if (Result) 7429 Visit(BO->getRHS()); 7430 } else { 7431 WorkList.push_back(BO->getRHS()); 7432 } 7433 } 7434 7435 // Only visit the condition, unless we can be sure which subexpression will 7436 // be chosen. 7437 void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) { 7438 EvaluationTracker Eval(*this); 7439 { 7440 SequencedSubexpression Sequenced(*this); 7441 Visit(CO->getCond()); 7442 } 7443 7444 bool Result; 7445 if (Eval.evaluate(CO->getCond(), Result)) 7446 Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr()); 7447 else { 7448 WorkList.push_back(CO->getTrueExpr()); 7449 WorkList.push_back(CO->getFalseExpr()); 7450 } 7451 } 7452 7453 void VisitCallExpr(CallExpr *CE) { 7454 // C++11 [intro.execution]p15: 7455 // When calling a function [...], every value computation and side effect 7456 // associated with any argument expression, or with the postfix expression 7457 // designating the called function, is sequenced before execution of every 7458 // expression or statement in the body of the function [and thus before 7459 // the value computation of its result]. 7460 SequencedSubexpression Sequenced(*this); 7461 Base::VisitCallExpr(CE); 7462 7463 // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions. 7464 } 7465 7466 void VisitCXXConstructExpr(CXXConstructExpr *CCE) { 7467 // This is a call, so all subexpressions are sequenced before the result. 7468 SequencedSubexpression Sequenced(*this); 7469 7470 if (!CCE->isListInitialization()) 7471 return VisitExpr(CCE); 7472 7473 // In C++11, list initializations are sequenced. 7474 SmallVector<SequenceTree::Seq, 32> Elts; 7475 SequenceTree::Seq Parent = Region; 7476 for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(), 7477 E = CCE->arg_end(); 7478 I != E; ++I) { 7479 Region = Tree.allocate(Parent); 7480 Elts.push_back(Region); 7481 Visit(*I); 7482 } 7483 7484 // Forget that the initializers are sequenced. 7485 Region = Parent; 7486 for (unsigned I = 0; I < Elts.size(); ++I) 7487 Tree.merge(Elts[I]); 7488 } 7489 7490 void VisitInitListExpr(InitListExpr *ILE) { 7491 if (!SemaRef.getLangOpts().CPlusPlus11) 7492 return VisitExpr(ILE); 7493 7494 // In C++11, list initializations are sequenced. 7495 SmallVector<SequenceTree::Seq, 32> Elts; 7496 SequenceTree::Seq Parent = Region; 7497 for (unsigned I = 0; I < ILE->getNumInits(); ++I) { 7498 Expr *E = ILE->getInit(I); 7499 if (!E) continue; 7500 Region = Tree.allocate(Parent); 7501 Elts.push_back(Region); 7502 Visit(E); 7503 } 7504 7505 // Forget that the initializers are sequenced. 7506 Region = Parent; 7507 for (unsigned I = 0; I < Elts.size(); ++I) 7508 Tree.merge(Elts[I]); 7509 } 7510 }; 7511 } 7512 7513 void Sema::CheckUnsequencedOperations(Expr *E) { 7514 SmallVector<Expr *, 8> WorkList; 7515 WorkList.push_back(E); 7516 while (!WorkList.empty()) { 7517 Expr *Item = WorkList.pop_back_val(); 7518 SequenceChecker(*this, Item, WorkList); 7519 } 7520 } 7521 7522 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, 7523 bool IsConstexpr) { 7524 CheckImplicitConversions(E, CheckLoc); 7525 CheckUnsequencedOperations(E); 7526 if (!IsConstexpr && !E->isValueDependent()) 7527 CheckForIntOverflow(E); 7528 } 7529 7530 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, 7531 FieldDecl *BitField, 7532 Expr *Init) { 7533 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); 7534 } 7535 7536 /// CheckParmsForFunctionDef - Check that the parameters of the given 7537 /// function are appropriate for the definition of a function. This 7538 /// takes care of any checks that cannot be performed on the 7539 /// declaration itself, e.g., that the types of each of the function 7540 /// parameters are complete. 7541 bool Sema::CheckParmsForFunctionDef(ParmVarDecl *const *P, 7542 ParmVarDecl *const *PEnd, 7543 bool CheckParameterNames) { 7544 bool HasInvalidParm = false; 7545 for (; P != PEnd; ++P) { 7546 ParmVarDecl *Param = *P; 7547 7548 // C99 6.7.5.3p4: the parameters in a parameter type list in a 7549 // function declarator that is part of a function definition of 7550 // that function shall not have incomplete type. 7551 // 7552 // This is also C++ [dcl.fct]p6. 7553 if (!Param->isInvalidDecl() && 7554 RequireCompleteType(Param->getLocation(), Param->getType(), 7555 diag::err_typecheck_decl_incomplete_type)) { 7556 Param->setInvalidDecl(); 7557 HasInvalidParm = true; 7558 } 7559 7560 // C99 6.9.1p5: If the declarator includes a parameter type list, the 7561 // declaration of each parameter shall include an identifier. 7562 if (CheckParameterNames && 7563 Param->getIdentifier() == nullptr && 7564 !Param->isImplicit() && 7565 !getLangOpts().CPlusPlus) 7566 Diag(Param->getLocation(), diag::err_parameter_name_omitted); 7567 7568 // C99 6.7.5.3p12: 7569 // If the function declarator is not part of a definition of that 7570 // function, parameters may have incomplete type and may use the [*] 7571 // notation in their sequences of declarator specifiers to specify 7572 // variable length array types. 7573 QualType PType = Param->getOriginalType(); 7574 while (const ArrayType *AT = Context.getAsArrayType(PType)) { 7575 if (AT->getSizeModifier() == ArrayType::Star) { 7576 // FIXME: This diagnostic should point the '[*]' if source-location 7577 // information is added for it. 7578 Diag(Param->getLocation(), diag::err_array_star_in_function_definition); 7579 break; 7580 } 7581 PType= AT->getElementType(); 7582 } 7583 7584 // MSVC destroys objects passed by value in the callee. Therefore a 7585 // function definition which takes such a parameter must be able to call the 7586 // object's destructor. However, we don't perform any direct access check 7587 // on the dtor. 7588 if (getLangOpts().CPlusPlus && Context.getTargetInfo() 7589 .getCXXABI() 7590 .areArgsDestroyedLeftToRightInCallee()) { 7591 if (!Param->isInvalidDecl()) { 7592 if (const RecordType *RT = Param->getType()->getAs<RecordType>()) { 7593 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 7594 if (!ClassDecl->isInvalidDecl() && 7595 !ClassDecl->hasIrrelevantDestructor() && 7596 !ClassDecl->isDependentContext()) { 7597 CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); 7598 MarkFunctionReferenced(Param->getLocation(), Destructor); 7599 DiagnoseUseOfDecl(Destructor, Param->getLocation()); 7600 } 7601 } 7602 } 7603 } 7604 } 7605 7606 return HasInvalidParm; 7607 } 7608 7609 /// CheckCastAlign - Implements -Wcast-align, which warns when a 7610 /// pointer cast increases the alignment requirements. 7611 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { 7612 // This is actually a lot of work to potentially be doing on every 7613 // cast; don't do it if we're ignoring -Wcast_align (as is the default). 7614 if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin())) 7615 return; 7616 7617 // Ignore dependent types. 7618 if (T->isDependentType() || Op->getType()->isDependentType()) 7619 return; 7620 7621 // Require that the destination be a pointer type. 7622 const PointerType *DestPtr = T->getAs<PointerType>(); 7623 if (!DestPtr) return; 7624 7625 // If the destination has alignment 1, we're done. 7626 QualType DestPointee = DestPtr->getPointeeType(); 7627 if (DestPointee->isIncompleteType()) return; 7628 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); 7629 if (DestAlign.isOne()) return; 7630 7631 // Require that the source be a pointer type. 7632 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); 7633 if (!SrcPtr) return; 7634 QualType SrcPointee = SrcPtr->getPointeeType(); 7635 7636 // Whitelist casts from cv void*. We already implicitly 7637 // whitelisted casts to cv void*, since they have alignment 1. 7638 // Also whitelist casts involving incomplete types, which implicitly 7639 // includes 'void'. 7640 if (SrcPointee->isIncompleteType()) return; 7641 7642 CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); 7643 if (SrcAlign >= DestAlign) return; 7644 7645 Diag(TRange.getBegin(), diag::warn_cast_align) 7646 << Op->getType() << T 7647 << static_cast<unsigned>(SrcAlign.getQuantity()) 7648 << static_cast<unsigned>(DestAlign.getQuantity()) 7649 << TRange << Op->getSourceRange(); 7650 } 7651 7652 static const Type* getElementType(const Expr *BaseExpr) { 7653 const Type* EltType = BaseExpr->getType().getTypePtr(); 7654 if (EltType->isAnyPointerType()) 7655 return EltType->getPointeeType().getTypePtr(); 7656 else if (EltType->isArrayType()) 7657 return EltType->getBaseElementTypeUnsafe(); 7658 return EltType; 7659 } 7660 7661 /// \brief Check whether this array fits the idiom of a size-one tail padded 7662 /// array member of a struct. 7663 /// 7664 /// We avoid emitting out-of-bounds access warnings for such arrays as they are 7665 /// commonly used to emulate flexible arrays in C89 code. 7666 static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, 7667 const NamedDecl *ND) { 7668 if (Size != 1 || !ND) return false; 7669 7670 const FieldDecl *FD = dyn_cast<FieldDecl>(ND); 7671 if (!FD) return false; 7672 7673 // Don't consider sizes resulting from macro expansions or template argument 7674 // substitution to form C89 tail-padded arrays. 7675 7676 TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); 7677 while (TInfo) { 7678 TypeLoc TL = TInfo->getTypeLoc(); 7679 // Look through typedefs. 7680 if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) { 7681 const TypedefNameDecl *TDL = TTL.getTypedefNameDecl(); 7682 TInfo = TDL->getTypeSourceInfo(); 7683 continue; 7684 } 7685 if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) { 7686 const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); 7687 if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) 7688 return false; 7689 } 7690 break; 7691 } 7692 7693 const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); 7694 if (!RD) return false; 7695 if (RD->isUnion()) return false; 7696 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 7697 if (!CRD->isStandardLayout()) return false; 7698 } 7699 7700 // See if this is the last field decl in the record. 7701 const Decl *D = FD; 7702 while ((D = D->getNextDeclInContext())) 7703 if (isa<FieldDecl>(D)) 7704 return false; 7705 return true; 7706 } 7707 7708 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, 7709 const ArraySubscriptExpr *ASE, 7710 bool AllowOnePastEnd, bool IndexNegated) { 7711 IndexExpr = IndexExpr->IgnoreParenImpCasts(); 7712 if (IndexExpr->isValueDependent()) 7713 return; 7714 7715 const Type *EffectiveType = getElementType(BaseExpr); 7716 BaseExpr = BaseExpr->IgnoreParenCasts(); 7717 const ConstantArrayType *ArrayTy = 7718 Context.getAsConstantArrayType(BaseExpr->getType()); 7719 if (!ArrayTy) 7720 return; 7721 7722 llvm::APSInt index; 7723 if (!IndexExpr->EvaluateAsInt(index, Context)) 7724 return; 7725 if (IndexNegated) 7726 index = -index; 7727 7728 const NamedDecl *ND = nullptr; 7729 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 7730 ND = dyn_cast<NamedDecl>(DRE->getDecl()); 7731 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) 7732 ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); 7733 7734 if (index.isUnsigned() || !index.isNegative()) { 7735 llvm::APInt size = ArrayTy->getSize(); 7736 if (!size.isStrictlyPositive()) 7737 return; 7738 7739 const Type* BaseType = getElementType(BaseExpr); 7740 if (BaseType != EffectiveType) { 7741 // Make sure we're comparing apples to apples when comparing index to size 7742 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); 7743 uint64_t array_typesize = Context.getTypeSize(BaseType); 7744 // Handle ptrarith_typesize being zero, such as when casting to void* 7745 if (!ptrarith_typesize) ptrarith_typesize = 1; 7746 if (ptrarith_typesize != array_typesize) { 7747 // There's a cast to a different size type involved 7748 uint64_t ratio = array_typesize / ptrarith_typesize; 7749 // TODO: Be smarter about handling cases where array_typesize is not a 7750 // multiple of ptrarith_typesize 7751 if (ptrarith_typesize * ratio == array_typesize) 7752 size *= llvm::APInt(size.getBitWidth(), ratio); 7753 } 7754 } 7755 7756 if (size.getBitWidth() > index.getBitWidth()) 7757 index = index.zext(size.getBitWidth()); 7758 else if (size.getBitWidth() < index.getBitWidth()) 7759 size = size.zext(index.getBitWidth()); 7760 7761 // For array subscripting the index must be less than size, but for pointer 7762 // arithmetic also allow the index (offset) to be equal to size since 7763 // computing the next address after the end of the array is legal and 7764 // commonly done e.g. in C++ iterators and range-based for loops. 7765 if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) 7766 return; 7767 7768 // Also don't warn for arrays of size 1 which are members of some 7769 // structure. These are often used to approximate flexible arrays in C89 7770 // code. 7771 if (IsTailPaddedMemberArray(*this, size, ND)) 7772 return; 7773 7774 // Suppress the warning if the subscript expression (as identified by the 7775 // ']' location) and the index expression are both from macro expansions 7776 // within a system header. 7777 if (ASE) { 7778 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( 7779 ASE->getRBracketLoc()); 7780 if (SourceMgr.isInSystemHeader(RBracketLoc)) { 7781 SourceLocation IndexLoc = SourceMgr.getSpellingLoc( 7782 IndexExpr->getLocStart()); 7783 if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc)) 7784 return; 7785 } 7786 } 7787 7788 unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; 7789 if (ASE) 7790 DiagID = diag::warn_array_index_exceeds_bounds; 7791 7792 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, 7793 PDiag(DiagID) << index.toString(10, true) 7794 << size.toString(10, true) 7795 << (unsigned)size.getLimitedValue(~0U) 7796 << IndexExpr->getSourceRange()); 7797 } else { 7798 unsigned DiagID = diag::warn_array_index_precedes_bounds; 7799 if (!ASE) { 7800 DiagID = diag::warn_ptr_arith_precedes_bounds; 7801 if (index.isNegative()) index = -index; 7802 } 7803 7804 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, 7805 PDiag(DiagID) << index.toString(10, true) 7806 << IndexExpr->getSourceRange()); 7807 } 7808 7809 if (!ND) { 7810 // Try harder to find a NamedDecl to point at in the note. 7811 while (const ArraySubscriptExpr *ASE = 7812 dyn_cast<ArraySubscriptExpr>(BaseExpr)) 7813 BaseExpr = ASE->getBase()->IgnoreParenCasts(); 7814 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) 7815 ND = dyn_cast<NamedDecl>(DRE->getDecl()); 7816 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) 7817 ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); 7818 } 7819 7820 if (ND) 7821 DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, 7822 PDiag(diag::note_array_index_out_of_bounds) 7823 << ND->getDeclName()); 7824 } 7825 7826 void Sema::CheckArrayAccess(const Expr *expr) { 7827 int AllowOnePastEnd = 0; 7828 while (expr) { 7829 expr = expr->IgnoreParenImpCasts(); 7830 switch (expr->getStmtClass()) { 7831 case Stmt::ArraySubscriptExprClass: { 7832 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); 7833 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, 7834 AllowOnePastEnd > 0); 7835 return; 7836 } 7837 case Stmt::UnaryOperatorClass: { 7838 // Only unwrap the * and & unary operators 7839 const UnaryOperator *UO = cast<UnaryOperator>(expr); 7840 expr = UO->getSubExpr(); 7841 switch (UO->getOpcode()) { 7842 case UO_AddrOf: 7843 AllowOnePastEnd++; 7844 break; 7845 case UO_Deref: 7846 AllowOnePastEnd--; 7847 break; 7848 default: 7849 return; 7850 } 7851 break; 7852 } 7853 case Stmt::ConditionalOperatorClass: { 7854 const ConditionalOperator *cond = cast<ConditionalOperator>(expr); 7855 if (const Expr *lhs = cond->getLHS()) 7856 CheckArrayAccess(lhs); 7857 if (const Expr *rhs = cond->getRHS()) 7858 CheckArrayAccess(rhs); 7859 return; 7860 } 7861 default: 7862 return; 7863 } 7864 } 7865 } 7866 7867 //===--- CHECK: Objective-C retain cycles ----------------------------------// 7868 7869 namespace { 7870 struct RetainCycleOwner { 7871 RetainCycleOwner() : Variable(nullptr), Indirect(false) {} 7872 VarDecl *Variable; 7873 SourceRange Range; 7874 SourceLocation Loc; 7875 bool Indirect; 7876 7877 void setLocsFrom(Expr *e) { 7878 Loc = e->getExprLoc(); 7879 Range = e->getSourceRange(); 7880 } 7881 }; 7882 } 7883 7884 /// Consider whether capturing the given variable can possibly lead to 7885 /// a retain cycle. 7886 static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { 7887 // In ARC, it's captured strongly iff the variable has __strong 7888 // lifetime. In MRR, it's captured strongly if the variable is 7889 // __block and has an appropriate type. 7890 if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 7891 return false; 7892 7893 owner.Variable = var; 7894 if (ref) 7895 owner.setLocsFrom(ref); 7896 return true; 7897 } 7898 7899 static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { 7900 while (true) { 7901 e = e->IgnoreParens(); 7902 if (CastExpr *cast = dyn_cast<CastExpr>(e)) { 7903 switch (cast->getCastKind()) { 7904 case CK_BitCast: 7905 case CK_LValueBitCast: 7906 case CK_LValueToRValue: 7907 case CK_ARCReclaimReturnedObject: 7908 e = cast->getSubExpr(); 7909 continue; 7910 7911 default: 7912 return false; 7913 } 7914 } 7915 7916 if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { 7917 ObjCIvarDecl *ivar = ref->getDecl(); 7918 if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 7919 return false; 7920 7921 // Try to find a retain cycle in the base. 7922 if (!findRetainCycleOwner(S, ref->getBase(), owner)) 7923 return false; 7924 7925 if (ref->isFreeIvar()) owner.setLocsFrom(ref); 7926 owner.Indirect = true; 7927 return true; 7928 } 7929 7930 if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { 7931 VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); 7932 if (!var) return false; 7933 return considerVariable(var, ref, owner); 7934 } 7935 7936 if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { 7937 if (member->isArrow()) return false; 7938 7939 // Don't count this as an indirect ownership. 7940 e = member->getBase(); 7941 continue; 7942 } 7943 7944 if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 7945 // Only pay attention to pseudo-objects on property references. 7946 ObjCPropertyRefExpr *pre 7947 = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() 7948 ->IgnoreParens()); 7949 if (!pre) return false; 7950 if (pre->isImplicitProperty()) return false; 7951 ObjCPropertyDecl *property = pre->getExplicitProperty(); 7952 if (!property->isRetaining() && 7953 !(property->getPropertyIvarDecl() && 7954 property->getPropertyIvarDecl()->getType() 7955 .getObjCLifetime() == Qualifiers::OCL_Strong)) 7956 return false; 7957 7958 owner.Indirect = true; 7959 if (pre->isSuperReceiver()) { 7960 owner.Variable = S.getCurMethodDecl()->getSelfDecl(); 7961 if (!owner.Variable) 7962 return false; 7963 owner.Loc = pre->getLocation(); 7964 owner.Range = pre->getSourceRange(); 7965 return true; 7966 } 7967 e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) 7968 ->getSourceExpr()); 7969 continue; 7970 } 7971 7972 // Array ivars? 7973 7974 return false; 7975 } 7976 } 7977 7978 namespace { 7979 struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { 7980 FindCaptureVisitor(ASTContext &Context, VarDecl *variable) 7981 : EvaluatedExprVisitor<FindCaptureVisitor>(Context), 7982 Context(Context), Variable(variable), Capturer(nullptr), 7983 VarWillBeReased(false) {} 7984 ASTContext &Context; 7985 VarDecl *Variable; 7986 Expr *Capturer; 7987 bool VarWillBeReased; 7988 7989 void VisitDeclRefExpr(DeclRefExpr *ref) { 7990 if (ref->getDecl() == Variable && !Capturer) 7991 Capturer = ref; 7992 } 7993 7994 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { 7995 if (Capturer) return; 7996 Visit(ref->getBase()); 7997 if (Capturer && ref->isFreeIvar()) 7998 Capturer = ref; 7999 } 8000 8001 void VisitBlockExpr(BlockExpr *block) { 8002 // Look inside nested blocks 8003 if (block->getBlockDecl()->capturesVariable(Variable)) 8004 Visit(block->getBlockDecl()->getBody()); 8005 } 8006 8007 void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) { 8008 if (Capturer) return; 8009 if (OVE->getSourceExpr()) 8010 Visit(OVE->getSourceExpr()); 8011 } 8012 void VisitBinaryOperator(BinaryOperator *BinOp) { 8013 if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign) 8014 return; 8015 Expr *LHS = BinOp->getLHS(); 8016 if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) { 8017 if (DRE->getDecl() != Variable) 8018 return; 8019 if (Expr *RHS = BinOp->getRHS()) { 8020 RHS = RHS->IgnoreParenCasts(); 8021 llvm::APSInt Value; 8022 VarWillBeReased = 8023 (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0); 8024 } 8025 } 8026 } 8027 }; 8028 } 8029 8030 /// Check whether the given argument is a block which captures a 8031 /// variable. 8032 static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { 8033 assert(owner.Variable && owner.Loc.isValid()); 8034 8035 e = e->IgnoreParenCasts(); 8036 8037 // Look through [^{...} copy] and Block_copy(^{...}). 8038 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) { 8039 Selector Cmd = ME->getSelector(); 8040 if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") { 8041 e = ME->getInstanceReceiver(); 8042 if (!e) 8043 return nullptr; 8044 e = e->IgnoreParenCasts(); 8045 } 8046 } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) { 8047 if (CE->getNumArgs() == 1) { 8048 FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl()); 8049 if (Fn) { 8050 const IdentifierInfo *FnI = Fn->getIdentifier(); 8051 if (FnI && FnI->isStr("_Block_copy")) { 8052 e = CE->getArg(0)->IgnoreParenCasts(); 8053 } 8054 } 8055 } 8056 } 8057 8058 BlockExpr *block = dyn_cast<BlockExpr>(e); 8059 if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) 8060 return nullptr; 8061 8062 FindCaptureVisitor visitor(S.Context, owner.Variable); 8063 visitor.Visit(block->getBlockDecl()->getBody()); 8064 return visitor.VarWillBeReased ? nullptr : visitor.Capturer; 8065 } 8066 8067 static void diagnoseRetainCycle(Sema &S, Expr *capturer, 8068 RetainCycleOwner &owner) { 8069 assert(capturer); 8070 assert(owner.Variable && owner.Loc.isValid()); 8071 8072 S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) 8073 << owner.Variable << capturer->getSourceRange(); 8074 S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) 8075 << owner.Indirect << owner.Range; 8076 } 8077 8078 /// Check for a keyword selector that starts with the word 'add' or 8079 /// 'set'. 8080 static bool isSetterLikeSelector(Selector sel) { 8081 if (sel.isUnarySelector()) return false; 8082 8083 StringRef str = sel.getNameForSlot(0); 8084 while (!str.empty() && str.front() == '_') str = str.substr(1); 8085 if (str.startswith("set")) 8086 str = str.substr(3); 8087 else if (str.startswith("add")) { 8088 // Specially whitelist 'addOperationWithBlock:'. 8089 if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) 8090 return false; 8091 str = str.substr(3); 8092 } 8093 else 8094 return false; 8095 8096 if (str.empty()) return true; 8097 return !isLowercase(str.front()); 8098 } 8099 8100 /// Check a message send to see if it's likely to cause a retain cycle. 8101 void Sema::checkRetainCycles(ObjCMessageExpr *msg) { 8102 // Only check instance methods whose selector looks like a setter. 8103 if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) 8104 return; 8105 8106 // Try to find a variable that the receiver is strongly owned by. 8107 RetainCycleOwner owner; 8108 if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { 8109 if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) 8110 return; 8111 } else { 8112 assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); 8113 owner.Variable = getCurMethodDecl()->getSelfDecl(); 8114 owner.Loc = msg->getSuperLoc(); 8115 owner.Range = msg->getSuperLoc(); 8116 } 8117 8118 // Check whether the receiver is captured by any of the arguments. 8119 for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) 8120 if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) 8121 return diagnoseRetainCycle(*this, capturer, owner); 8122 } 8123 8124 /// Check a property assign to see if it's likely to cause a retain cycle. 8125 void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { 8126 RetainCycleOwner owner; 8127 if (!findRetainCycleOwner(*this, receiver, owner)) 8128 return; 8129 8130 if (Expr *capturer = findCapturingExpr(*this, argument, owner)) 8131 diagnoseRetainCycle(*this, capturer, owner); 8132 } 8133 8134 void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) { 8135 RetainCycleOwner Owner; 8136 if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner)) 8137 return; 8138 8139 // Because we don't have an expression for the variable, we have to set the 8140 // location explicitly here. 8141 Owner.Loc = Var->getLocation(); 8142 Owner.Range = Var->getSourceRange(); 8143 8144 if (Expr *Capturer = findCapturingExpr(*this, Init, Owner)) 8145 diagnoseRetainCycle(*this, Capturer, Owner); 8146 } 8147 8148 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc, 8149 Expr *RHS, bool isProperty) { 8150 // Check if RHS is an Objective-C object literal, which also can get 8151 // immediately zapped in a weak reference. Note that we explicitly 8152 // allow ObjCStringLiterals, since those are designed to never really die. 8153 RHS = RHS->IgnoreParenImpCasts(); 8154 8155 // This enum needs to match with the 'select' in 8156 // warn_objc_arc_literal_assign (off-by-1). 8157 Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS); 8158 if (Kind == Sema::LK_String || Kind == Sema::LK_None) 8159 return false; 8160 8161 S.Diag(Loc, diag::warn_arc_literal_assign) 8162 << (unsigned) Kind 8163 << (isProperty ? 0 : 1) 8164 << RHS->getSourceRange(); 8165 8166 return true; 8167 } 8168 8169 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc, 8170 Qualifiers::ObjCLifetime LT, 8171 Expr *RHS, bool isProperty) { 8172 // Strip off any implicit cast added to get to the one ARC-specific. 8173 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 8174 if (cast->getCastKind() == CK_ARCConsumeObject) { 8175 S.Diag(Loc, diag::warn_arc_retained_assign) 8176 << (LT == Qualifiers::OCL_ExplicitNone) 8177 << (isProperty ? 0 : 1) 8178 << RHS->getSourceRange(); 8179 return true; 8180 } 8181 RHS = cast->getSubExpr(); 8182 } 8183 8184 if (LT == Qualifiers::OCL_Weak && 8185 checkUnsafeAssignLiteral(S, Loc, RHS, isProperty)) 8186 return true; 8187 8188 return false; 8189 } 8190 8191 bool Sema::checkUnsafeAssigns(SourceLocation Loc, 8192 QualType LHS, Expr *RHS) { 8193 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); 8194 8195 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) 8196 return false; 8197 8198 if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false)) 8199 return true; 8200 8201 return false; 8202 } 8203 8204 void Sema::checkUnsafeExprAssigns(SourceLocation Loc, 8205 Expr *LHS, Expr *RHS) { 8206 QualType LHSType; 8207 // PropertyRef on LHS type need be directly obtained from 8208 // its declaration as it has a PseudoType. 8209 ObjCPropertyRefExpr *PRE 8210 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); 8211 if (PRE && !PRE->isImplicitProperty()) { 8212 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 8213 if (PD) 8214 LHSType = PD->getType(); 8215 } 8216 8217 if (LHSType.isNull()) 8218 LHSType = LHS->getType(); 8219 8220 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); 8221 8222 if (LT == Qualifiers::OCL_Weak) { 8223 if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc)) 8224 getCurFunction()->markSafeWeakUse(LHS); 8225 } 8226 8227 if (checkUnsafeAssigns(Loc, LHSType, RHS)) 8228 return; 8229 8230 // FIXME. Check for other life times. 8231 if (LT != Qualifiers::OCL_None) 8232 return; 8233 8234 if (PRE) { 8235 if (PRE->isImplicitProperty()) 8236 return; 8237 const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); 8238 if (!PD) 8239 return; 8240 8241 unsigned Attributes = PD->getPropertyAttributes(); 8242 if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { 8243 // when 'assign' attribute was not explicitly specified 8244 // by user, ignore it and rely on property type itself 8245 // for lifetime info. 8246 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); 8247 if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && 8248 LHSType->isObjCRetainableType()) 8249 return; 8250 8251 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { 8252 if (cast->getCastKind() == CK_ARCConsumeObject) { 8253 Diag(Loc, diag::warn_arc_retained_property_assign) 8254 << RHS->getSourceRange(); 8255 return; 8256 } 8257 RHS = cast->getSubExpr(); 8258 } 8259 } 8260 else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { 8261 if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true)) 8262 return; 8263 } 8264 } 8265 } 8266 8267 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// 8268 8269 namespace { 8270 bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, 8271 SourceLocation StmtLoc, 8272 const NullStmt *Body) { 8273 // Do not warn if the body is a macro that expands to nothing, e.g: 8274 // 8275 // #define CALL(x) 8276 // if (condition) 8277 // CALL(0); 8278 // 8279 if (Body->hasLeadingEmptyMacro()) 8280 return false; 8281 8282 // Get line numbers of statement and body. 8283 bool StmtLineInvalid; 8284 unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc, 8285 &StmtLineInvalid); 8286 if (StmtLineInvalid) 8287 return false; 8288 8289 bool BodyLineInvalid; 8290 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), 8291 &BodyLineInvalid); 8292 if (BodyLineInvalid) 8293 return false; 8294 8295 // Warn if null statement and body are on the same line. 8296 if (StmtLine != BodyLine) 8297 return false; 8298 8299 return true; 8300 } 8301 } // Unnamed namespace 8302 8303 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, 8304 const Stmt *Body, 8305 unsigned DiagID) { 8306 // Since this is a syntactic check, don't emit diagnostic for template 8307 // instantiations, this just adds noise. 8308 if (CurrentInstantiationScope) 8309 return; 8310 8311 // The body should be a null statement. 8312 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 8313 if (!NBody) 8314 return; 8315 8316 // Do the usual checks. 8317 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 8318 return; 8319 8320 Diag(NBody->getSemiLoc(), DiagID); 8321 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 8322 } 8323 8324 void Sema::DiagnoseEmptyLoopBody(const Stmt *S, 8325 const Stmt *PossibleBody) { 8326 assert(!CurrentInstantiationScope); // Ensured by caller 8327 8328 SourceLocation StmtLoc; 8329 const Stmt *Body; 8330 unsigned DiagID; 8331 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { 8332 StmtLoc = FS->getRParenLoc(); 8333 Body = FS->getBody(); 8334 DiagID = diag::warn_empty_for_body; 8335 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { 8336 StmtLoc = WS->getCond()->getSourceRange().getEnd(); 8337 Body = WS->getBody(); 8338 DiagID = diag::warn_empty_while_body; 8339 } else 8340 return; // Neither `for' nor `while'. 8341 8342 // The body should be a null statement. 8343 const NullStmt *NBody = dyn_cast<NullStmt>(Body); 8344 if (!NBody) 8345 return; 8346 8347 // Skip expensive checks if diagnostic is disabled. 8348 if (Diags.isIgnored(DiagID, NBody->getSemiLoc())) 8349 return; 8350 8351 // Do the usual checks. 8352 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) 8353 return; 8354 8355 // `for(...);' and `while(...);' are popular idioms, so in order to keep 8356 // noise level low, emit diagnostics only if for/while is followed by a 8357 // CompoundStmt, e.g.: 8358 // for (int i = 0; i < n; i++); 8359 // { 8360 // a(i); 8361 // } 8362 // or if for/while is followed by a statement with more indentation 8363 // than for/while itself: 8364 // for (int i = 0; i < n; i++); 8365 // a(i); 8366 bool ProbableTypo = isa<CompoundStmt>(PossibleBody); 8367 if (!ProbableTypo) { 8368 bool BodyColInvalid; 8369 unsigned BodyCol = SourceMgr.getPresumedColumnNumber( 8370 PossibleBody->getLocStart(), 8371 &BodyColInvalid); 8372 if (BodyColInvalid) 8373 return; 8374 8375 bool StmtColInvalid; 8376 unsigned StmtCol = SourceMgr.getPresumedColumnNumber( 8377 S->getLocStart(), 8378 &StmtColInvalid); 8379 if (StmtColInvalid) 8380 return; 8381 8382 if (BodyCol > StmtCol) 8383 ProbableTypo = true; 8384 } 8385 8386 if (ProbableTypo) { 8387 Diag(NBody->getSemiLoc(), DiagID); 8388 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); 8389 } 8390 } 8391 8392 //===--- CHECK: Warn on self move with std::move. -------------------------===// 8393 8394 /// DiagnoseSelfMove - Emits a warning if a value is moved to itself. 8395 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr, 8396 SourceLocation OpLoc) { 8397 8398 if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc)) 8399 return; 8400 8401 if (!ActiveTemplateInstantiations.empty()) 8402 return; 8403 8404 // Strip parens and casts away. 8405 LHSExpr = LHSExpr->IgnoreParenImpCasts(); 8406 RHSExpr = RHSExpr->IgnoreParenImpCasts(); 8407 8408 // Check for a call expression 8409 const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr); 8410 if (!CE || CE->getNumArgs() != 1) 8411 return; 8412 8413 // Check for a call to std::move 8414 const FunctionDecl *FD = CE->getDirectCallee(); 8415 if (!FD || !FD->isInStdNamespace() || !FD->getIdentifier() || 8416 !FD->getIdentifier()->isStr("move")) 8417 return; 8418 8419 // Get argument from std::move 8420 RHSExpr = CE->getArg(0); 8421 8422 const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); 8423 const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); 8424 8425 // Two DeclRefExpr's, check that the decls are the same. 8426 if (LHSDeclRef && RHSDeclRef) { 8427 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 8428 return; 8429 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 8430 RHSDeclRef->getDecl()->getCanonicalDecl()) 8431 return; 8432 8433 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() 8434 << LHSExpr->getSourceRange() 8435 << RHSExpr->getSourceRange(); 8436 return; 8437 } 8438 8439 // Member variables require a different approach to check for self moves. 8440 // MemberExpr's are the same if every nested MemberExpr refers to the same 8441 // Decl and that the base Expr's are DeclRefExpr's with the same Decl or 8442 // the base Expr's are CXXThisExpr's. 8443 const Expr *LHSBase = LHSExpr; 8444 const Expr *RHSBase = RHSExpr; 8445 const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr); 8446 const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr); 8447 if (!LHSME || !RHSME) 8448 return; 8449 8450 while (LHSME && RHSME) { 8451 if (LHSME->getMemberDecl()->getCanonicalDecl() != 8452 RHSME->getMemberDecl()->getCanonicalDecl()) 8453 return; 8454 8455 LHSBase = LHSME->getBase(); 8456 RHSBase = RHSME->getBase(); 8457 LHSME = dyn_cast<MemberExpr>(LHSBase); 8458 RHSME = dyn_cast<MemberExpr>(RHSBase); 8459 } 8460 8461 LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase); 8462 RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase); 8463 if (LHSDeclRef && RHSDeclRef) { 8464 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl()) 8465 return; 8466 if (LHSDeclRef->getDecl()->getCanonicalDecl() != 8467 RHSDeclRef->getDecl()->getCanonicalDecl()) 8468 return; 8469 8470 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() 8471 << LHSExpr->getSourceRange() 8472 << RHSExpr->getSourceRange(); 8473 return; 8474 } 8475 8476 if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase)) 8477 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType() 8478 << LHSExpr->getSourceRange() 8479 << RHSExpr->getSourceRange(); 8480 } 8481 8482 //===--- Layout compatibility ----------------------------------------------// 8483 8484 namespace { 8485 8486 bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); 8487 8488 /// \brief Check if two enumeration types are layout-compatible. 8489 bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { 8490 // C++11 [dcl.enum] p8: 8491 // Two enumeration types are layout-compatible if they have the same 8492 // underlying type. 8493 return ED1->isComplete() && ED2->isComplete() && 8494 C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); 8495 } 8496 8497 /// \brief Check if two fields are layout-compatible. 8498 bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) { 8499 if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) 8500 return false; 8501 8502 if (Field1->isBitField() != Field2->isBitField()) 8503 return false; 8504 8505 if (Field1->isBitField()) { 8506 // Make sure that the bit-fields are the same length. 8507 unsigned Bits1 = Field1->getBitWidthValue(C); 8508 unsigned Bits2 = Field2->getBitWidthValue(C); 8509 8510 if (Bits1 != Bits2) 8511 return false; 8512 } 8513 8514 return true; 8515 } 8516 8517 /// \brief Check if two standard-layout structs are layout-compatible. 8518 /// (C++11 [class.mem] p17) 8519 bool isLayoutCompatibleStruct(ASTContext &C, 8520 RecordDecl *RD1, 8521 RecordDecl *RD2) { 8522 // If both records are C++ classes, check that base classes match. 8523 if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { 8524 // If one of records is a CXXRecordDecl we are in C++ mode, 8525 // thus the other one is a CXXRecordDecl, too. 8526 const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); 8527 // Check number of base classes. 8528 if (D1CXX->getNumBases() != D2CXX->getNumBases()) 8529 return false; 8530 8531 // Check the base classes. 8532 for (CXXRecordDecl::base_class_const_iterator 8533 Base1 = D1CXX->bases_begin(), 8534 BaseEnd1 = D1CXX->bases_end(), 8535 Base2 = D2CXX->bases_begin(); 8536 Base1 != BaseEnd1; 8537 ++Base1, ++Base2) { 8538 if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) 8539 return false; 8540 } 8541 } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { 8542 // If only RD2 is a C++ class, it should have zero base classes. 8543 if (D2CXX->getNumBases() > 0) 8544 return false; 8545 } 8546 8547 // Check the fields. 8548 RecordDecl::field_iterator Field2 = RD2->field_begin(), 8549 Field2End = RD2->field_end(), 8550 Field1 = RD1->field_begin(), 8551 Field1End = RD1->field_end(); 8552 for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { 8553 if (!isLayoutCompatible(C, *Field1, *Field2)) 8554 return false; 8555 } 8556 if (Field1 != Field1End || Field2 != Field2End) 8557 return false; 8558 8559 return true; 8560 } 8561 8562 /// \brief Check if two standard-layout unions are layout-compatible. 8563 /// (C++11 [class.mem] p18) 8564 bool isLayoutCompatibleUnion(ASTContext &C, 8565 RecordDecl *RD1, 8566 RecordDecl *RD2) { 8567 llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; 8568 for (auto *Field2 : RD2->fields()) 8569 UnmatchedFields.insert(Field2); 8570 8571 for (auto *Field1 : RD1->fields()) { 8572 llvm::SmallPtrSet<FieldDecl *, 8>::iterator 8573 I = UnmatchedFields.begin(), 8574 E = UnmatchedFields.end(); 8575 8576 for ( ; I != E; ++I) { 8577 if (isLayoutCompatible(C, Field1, *I)) { 8578 bool Result = UnmatchedFields.erase(*I); 8579 (void) Result; 8580 assert(Result); 8581 break; 8582 } 8583 } 8584 if (I == E) 8585 return false; 8586 } 8587 8588 return UnmatchedFields.empty(); 8589 } 8590 8591 bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) { 8592 if (RD1->isUnion() != RD2->isUnion()) 8593 return false; 8594 8595 if (RD1->isUnion()) 8596 return isLayoutCompatibleUnion(C, RD1, RD2); 8597 else 8598 return isLayoutCompatibleStruct(C, RD1, RD2); 8599 } 8600 8601 /// \brief Check if two types are layout-compatible in C++11 sense. 8602 bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { 8603 if (T1.isNull() || T2.isNull()) 8604 return false; 8605 8606 // C++11 [basic.types] p11: 8607 // If two types T1 and T2 are the same type, then T1 and T2 are 8608 // layout-compatible types. 8609 if (C.hasSameType(T1, T2)) 8610 return true; 8611 8612 T1 = T1.getCanonicalType().getUnqualifiedType(); 8613 T2 = T2.getCanonicalType().getUnqualifiedType(); 8614 8615 const Type::TypeClass TC1 = T1->getTypeClass(); 8616 const Type::TypeClass TC2 = T2->getTypeClass(); 8617 8618 if (TC1 != TC2) 8619 return false; 8620 8621 if (TC1 == Type::Enum) { 8622 return isLayoutCompatible(C, 8623 cast<EnumType>(T1)->getDecl(), 8624 cast<EnumType>(T2)->getDecl()); 8625 } else if (TC1 == Type::Record) { 8626 if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) 8627 return false; 8628 8629 return isLayoutCompatible(C, 8630 cast<RecordType>(T1)->getDecl(), 8631 cast<RecordType>(T2)->getDecl()); 8632 } 8633 8634 return false; 8635 } 8636 } 8637 8638 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// 8639 8640 namespace { 8641 /// \brief Given a type tag expression find the type tag itself. 8642 /// 8643 /// \param TypeExpr Type tag expression, as it appears in user's code. 8644 /// 8645 /// \param VD Declaration of an identifier that appears in a type tag. 8646 /// 8647 /// \param MagicValue Type tag magic value. 8648 bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, 8649 const ValueDecl **VD, uint64_t *MagicValue) { 8650 while(true) { 8651 if (!TypeExpr) 8652 return false; 8653 8654 TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); 8655 8656 switch (TypeExpr->getStmtClass()) { 8657 case Stmt::UnaryOperatorClass: { 8658 const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); 8659 if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { 8660 TypeExpr = UO->getSubExpr(); 8661 continue; 8662 } 8663 return false; 8664 } 8665 8666 case Stmt::DeclRefExprClass: { 8667 const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); 8668 *VD = DRE->getDecl(); 8669 return true; 8670 } 8671 8672 case Stmt::IntegerLiteralClass: { 8673 const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); 8674 llvm::APInt MagicValueAPInt = IL->getValue(); 8675 if (MagicValueAPInt.getActiveBits() <= 64) { 8676 *MagicValue = MagicValueAPInt.getZExtValue(); 8677 return true; 8678 } else 8679 return false; 8680 } 8681 8682 case Stmt::BinaryConditionalOperatorClass: 8683 case Stmt::ConditionalOperatorClass: { 8684 const AbstractConditionalOperator *ACO = 8685 cast<AbstractConditionalOperator>(TypeExpr); 8686 bool Result; 8687 if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) { 8688 if (Result) 8689 TypeExpr = ACO->getTrueExpr(); 8690 else 8691 TypeExpr = ACO->getFalseExpr(); 8692 continue; 8693 } 8694 return false; 8695 } 8696 8697 case Stmt::BinaryOperatorClass: { 8698 const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); 8699 if (BO->getOpcode() == BO_Comma) { 8700 TypeExpr = BO->getRHS(); 8701 continue; 8702 } 8703 return false; 8704 } 8705 8706 default: 8707 return false; 8708 } 8709 } 8710 } 8711 8712 /// \brief Retrieve the C type corresponding to type tag TypeExpr. 8713 /// 8714 /// \param TypeExpr Expression that specifies a type tag. 8715 /// 8716 /// \param MagicValues Registered magic values. 8717 /// 8718 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong 8719 /// kind. 8720 /// 8721 /// \param TypeInfo Information about the corresponding C type. 8722 /// 8723 /// \returns true if the corresponding C type was found. 8724 bool GetMatchingCType( 8725 const IdentifierInfo *ArgumentKind, 8726 const Expr *TypeExpr, const ASTContext &Ctx, 8727 const llvm::DenseMap<Sema::TypeTagMagicValue, 8728 Sema::TypeTagData> *MagicValues, 8729 bool &FoundWrongKind, 8730 Sema::TypeTagData &TypeInfo) { 8731 FoundWrongKind = false; 8732 8733 // Variable declaration that has type_tag_for_datatype attribute. 8734 const ValueDecl *VD = nullptr; 8735 8736 uint64_t MagicValue; 8737 8738 if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue)) 8739 return false; 8740 8741 if (VD) { 8742 if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) { 8743 if (I->getArgumentKind() != ArgumentKind) { 8744 FoundWrongKind = true; 8745 return false; 8746 } 8747 TypeInfo.Type = I->getMatchingCType(); 8748 TypeInfo.LayoutCompatible = I->getLayoutCompatible(); 8749 TypeInfo.MustBeNull = I->getMustBeNull(); 8750 return true; 8751 } 8752 return false; 8753 } 8754 8755 if (!MagicValues) 8756 return false; 8757 8758 llvm::DenseMap<Sema::TypeTagMagicValue, 8759 Sema::TypeTagData>::const_iterator I = 8760 MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); 8761 if (I == MagicValues->end()) 8762 return false; 8763 8764 TypeInfo = I->second; 8765 return true; 8766 } 8767 } // unnamed namespace 8768 8769 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, 8770 uint64_t MagicValue, QualType Type, 8771 bool LayoutCompatible, 8772 bool MustBeNull) { 8773 if (!TypeTagForDatatypeMagicValues) 8774 TypeTagForDatatypeMagicValues.reset( 8775 new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); 8776 8777 TypeTagMagicValue Magic(ArgumentKind, MagicValue); 8778 (*TypeTagForDatatypeMagicValues)[Magic] = 8779 TypeTagData(Type, LayoutCompatible, MustBeNull); 8780 } 8781 8782 namespace { 8783 bool IsSameCharType(QualType T1, QualType T2) { 8784 const BuiltinType *BT1 = T1->getAs<BuiltinType>(); 8785 if (!BT1) 8786 return false; 8787 8788 const BuiltinType *BT2 = T2->getAs<BuiltinType>(); 8789 if (!BT2) 8790 return false; 8791 8792 BuiltinType::Kind T1Kind = BT1->getKind(); 8793 BuiltinType::Kind T2Kind = BT2->getKind(); 8794 8795 return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) || 8796 (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) || 8797 (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || 8798 (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); 8799 } 8800 } // unnamed namespace 8801 8802 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, 8803 const Expr * const *ExprArgs) { 8804 const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); 8805 bool IsPointerAttr = Attr->getIsPointer(); 8806 8807 const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()]; 8808 bool FoundWrongKind; 8809 TypeTagData TypeInfo; 8810 if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, 8811 TypeTagForDatatypeMagicValues.get(), 8812 FoundWrongKind, TypeInfo)) { 8813 if (FoundWrongKind) 8814 Diag(TypeTagExpr->getExprLoc(), 8815 diag::warn_type_tag_for_datatype_wrong_kind) 8816 << TypeTagExpr->getSourceRange(); 8817 return; 8818 } 8819 8820 const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()]; 8821 if (IsPointerAttr) { 8822 // Skip implicit cast of pointer to `void *' (as a function argument). 8823 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) 8824 if (ICE->getType()->isVoidPointerType() && 8825 ICE->getCastKind() == CK_BitCast) 8826 ArgumentExpr = ICE->getSubExpr(); 8827 } 8828 QualType ArgumentType = ArgumentExpr->getType(); 8829 8830 // Passing a `void*' pointer shouldn't trigger a warning. 8831 if (IsPointerAttr && ArgumentType->isVoidPointerType()) 8832 return; 8833 8834 if (TypeInfo.MustBeNull) { 8835 // Type tag with matching void type requires a null pointer. 8836 if (!ArgumentExpr->isNullPointerConstant(Context, 8837 Expr::NPC_ValueDependentIsNotNull)) { 8838 Diag(ArgumentExpr->getExprLoc(), 8839 diag::warn_type_safety_null_pointer_required) 8840 << ArgumentKind->getName() 8841 << ArgumentExpr->getSourceRange() 8842 << TypeTagExpr->getSourceRange(); 8843 } 8844 return; 8845 } 8846 8847 QualType RequiredType = TypeInfo.Type; 8848 if (IsPointerAttr) 8849 RequiredType = Context.getPointerType(RequiredType); 8850 8851 bool mismatch = false; 8852 if (!TypeInfo.LayoutCompatible) { 8853 mismatch = !Context.hasSameType(ArgumentType, RequiredType); 8854 8855 // C++11 [basic.fundamental] p1: 8856 // Plain char, signed char, and unsigned char are three distinct types. 8857 // 8858 // But we treat plain `char' as equivalent to `signed char' or `unsigned 8859 // char' depending on the current char signedness mode. 8860 if (mismatch) 8861 if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), 8862 RequiredType->getPointeeType())) || 8863 (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) 8864 mismatch = false; 8865 } else 8866 if (IsPointerAttr) 8867 mismatch = !isLayoutCompatible(Context, 8868 ArgumentType->getPointeeType(), 8869 RequiredType->getPointeeType()); 8870 else 8871 mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); 8872 8873 if (mismatch) 8874 Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) 8875 << ArgumentType << ArgumentKind 8876 << TypeInfo.LayoutCompatible << RequiredType 8877 << ArgumentExpr->getSourceRange() 8878 << TypeTagExpr->getSourceRange(); 8879 } 8880 8881