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