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