1 //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements extra semantic analysis beyond what is enforced
11 // by the C type system.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Sema/SemaInternal.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/CharUnits.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/EvaluatedExprVisitor.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/ExprCXX.h"
23 #include "clang/AST/ExprObjC.h"
24 #include "clang/AST/StmtCXX.h"
25 #include "clang/AST/StmtObjC.h"
26 #include "clang/Analysis/Analyses/FormatString.h"
27 #include "clang/Basic/CharInfo.h"
28 #include "clang/Basic/TargetBuiltins.h"
29 #include "clang/Basic/TargetInfo.h"
30 #include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering.
31 #include "clang/Sema/Initialization.h"
32 #include "clang/Sema/Lookup.h"
33 #include "clang/Sema/ScopeInfo.h"
34 #include "clang/Sema/Sema.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/ADT/SmallBitVector.h"
37 #include "llvm/ADT/SmallString.h"
38 #include "llvm/Support/ConvertUTF.h"
39 #include "llvm/Support/raw_ostream.h"
41 using namespace clang;
44 SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
45 unsigned ByteNo) const {
46 return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts,
47 Context.getTargetInfo());
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;
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();
61 // Highlight all the excess arguments.
62 SourceRange range(call->getArg(desiredArgCount)->getLocStart(),
63 call->getArg(argCount - 1)->getLocEnd());
65 return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
66 << 0 /*function call*/ << desiredArgCount << argCount
67 << call->getArg(1)->getSourceRange();
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))
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();
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();
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))
104 ExprResult Arg(TheCall->getArg(0));
105 QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getLocStart());
106 if (ResultType.isNull())
109 TheCall->setArg(0, Arg.get());
110 TheCall->setType(ResultType);
114 static void SemaBuiltinMemChkCall(Sema &S, FunctionDecl *FDecl,
115 CallExpr *TheCall, unsigned SizeIdx,
116 unsigned DstSizeIdx) {
117 if (TheCall->getNumArgs() <= SizeIdx ||
118 TheCall->getNumArgs() <= DstSizeIdx)
121 const Expr *SizeArg = TheCall->getArg(SizeIdx);
122 const Expr *DstSizeArg = TheCall->getArg(DstSizeIdx);
124 llvm::APSInt Size, DstSize;
126 // find out if both sizes are known at compile time
127 if (!SizeArg->EvaluateAsInt(Size, S.Context) ||
128 !DstSizeArg->EvaluateAsInt(DstSize, S.Context))
131 if (Size.ule(DstSize))
134 // confirmed overflow so generate the diagnostic.
135 IdentifierInfo *FnName = FDecl->getIdentifier();
136 SourceLocation SL = TheCall->getLocStart();
137 SourceRange SR = TheCall->getSourceRange();
139 S.Diag(SL, diag::warn_memcpy_chk_overflow) << SR << FnName;
142 static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) {
143 if (checkArgCount(S, BuiltinCall, 2))
146 SourceLocation BuiltinLoc = BuiltinCall->getLocStart();
147 Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts();
148 Expr *Call = BuiltinCall->getArg(0);
149 Expr *Chain = BuiltinCall->getArg(1);
151 if (Call->getStmtClass() != Stmt::CallExprClass) {
152 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call)
153 << Call->getSourceRange();
157 auto CE = cast<CallExpr>(Call);
158 if (CE->getCallee()->getType()->isBlockPointerType()) {
159 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call)
160 << Call->getSourceRange();
164 const Decl *TargetDecl = CE->getCalleeDecl();
165 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
166 if (FD->getBuiltinID()) {
167 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call)
168 << Call->getSourceRange();
172 if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) {
173 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call)
174 << Call->getSourceRange();
178 ExprResult ChainResult = S.UsualUnaryConversions(Chain);
179 if (ChainResult.isInvalid())
181 if (!ChainResult.get()->getType()->isPointerType()) {
182 S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer)
183 << Chain->getSourceRange();
187 QualType ReturnTy = CE->getCallReturnType(S.Context);
188 QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() };
189 QualType BuiltinTy = S.Context.getFunctionType(
190 ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo());
191 QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy);
194 S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get();
196 BuiltinCall->setType(CE->getType());
197 BuiltinCall->setValueKind(CE->getValueKind());
198 BuiltinCall->setObjectKind(CE->getObjectKind());
199 BuiltinCall->setCallee(Builtin);
200 BuiltinCall->setArg(1, ChainResult.get());
205 static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
206 Scope::ScopeFlags NeededScopeFlags,
208 // Scopes aren't available during instantiation. Fortunately, builtin
209 // functions cannot be template args so they cannot be formed through template
210 // instantiation. Therefore checking once during the parse is sufficient.
211 if (!SemaRef.ActiveTemplateInstantiations.empty())
214 Scope *S = SemaRef.getCurScope();
215 while (S && !S->isSEHExceptScope())
217 if (!S || !(S->getFlags() & NeededScopeFlags)) {
218 auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
219 SemaRef.Diag(TheCall->getExprLoc(), DiagID)
220 << DRE->getDecl()->getIdentifier();
228 Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
230 ExprResult TheCallResult(TheCall);
232 // Find out if any arguments are required to be integer constant expressions.
233 unsigned ICEArguments = 0;
234 ASTContext::GetBuiltinTypeError Error;
235 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
236 if (Error != ASTContext::GE_None)
237 ICEArguments = 0; // Don't diagnose previously diagnosed errors.
239 // If any arguments are required to be ICE's, check and diagnose.
240 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
241 // Skip arguments not required to be ICE's.
242 if ((ICEArguments & (1 << ArgNo)) == 0) continue;
245 if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
247 ICEArguments &= ~(1 << ArgNo);
251 case Builtin::BI__builtin___CFStringMakeConstantString:
252 assert(TheCall->getNumArgs() == 1 &&
253 "Wrong # arguments to builtin CFStringMakeConstantString");
254 if (CheckObjCString(TheCall->getArg(0)))
257 case Builtin::BI__builtin_stdarg_start:
258 case Builtin::BI__builtin_va_start:
259 if (SemaBuiltinVAStart(TheCall))
262 case Builtin::BI__va_start: {
263 switch (Context.getTargetInfo().getTriple().getArch()) {
264 case llvm::Triple::arm:
265 case llvm::Triple::thumb:
266 if (SemaBuiltinVAStartARM(TheCall))
270 if (SemaBuiltinVAStart(TheCall))
276 case Builtin::BI__builtin_isgreater:
277 case Builtin::BI__builtin_isgreaterequal:
278 case Builtin::BI__builtin_isless:
279 case Builtin::BI__builtin_islessequal:
280 case Builtin::BI__builtin_islessgreater:
281 case Builtin::BI__builtin_isunordered:
282 if (SemaBuiltinUnorderedCompare(TheCall))
285 case Builtin::BI__builtin_fpclassify:
286 if (SemaBuiltinFPClassification(TheCall, 6))
289 case Builtin::BI__builtin_isfinite:
290 case Builtin::BI__builtin_isinf:
291 case Builtin::BI__builtin_isinf_sign:
292 case Builtin::BI__builtin_isnan:
293 case Builtin::BI__builtin_isnormal:
294 if (SemaBuiltinFPClassification(TheCall, 1))
297 case Builtin::BI__builtin_shufflevector:
298 return SemaBuiltinShuffleVector(TheCall);
299 // TheCall will be freed by the smart pointer here, but that's fine, since
300 // SemaBuiltinShuffleVector guts it, but then doesn't release it.
301 case Builtin::BI__builtin_prefetch:
302 if (SemaBuiltinPrefetch(TheCall))
305 case Builtin::BI__assume:
306 case Builtin::BI__builtin_assume:
307 if (SemaBuiltinAssume(TheCall))
310 case Builtin::BI__builtin_assume_aligned:
311 if (SemaBuiltinAssumeAligned(TheCall))
314 case Builtin::BI__builtin_object_size:
315 if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3))
318 case Builtin::BI__builtin_longjmp:
319 if (SemaBuiltinLongjmp(TheCall))
322 case Builtin::BI__builtin_setjmp:
323 if (SemaBuiltinSetjmp(TheCall))
326 case Builtin::BI_setjmp:
327 case Builtin::BI_setjmpex:
328 if (checkArgCount(*this, TheCall, 1))
332 case Builtin::BI__builtin_classify_type:
333 if (checkArgCount(*this, TheCall, 1)) return true;
334 TheCall->setType(Context.IntTy);
336 case Builtin::BI__builtin_constant_p:
337 if (checkArgCount(*this, TheCall, 1)) return true;
338 TheCall->setType(Context.IntTy);
340 case Builtin::BI__sync_fetch_and_add:
341 case Builtin::BI__sync_fetch_and_add_1:
342 case Builtin::BI__sync_fetch_and_add_2:
343 case Builtin::BI__sync_fetch_and_add_4:
344 case Builtin::BI__sync_fetch_and_add_8:
345 case Builtin::BI__sync_fetch_and_add_16:
346 case Builtin::BI__sync_fetch_and_sub:
347 case Builtin::BI__sync_fetch_and_sub_1:
348 case Builtin::BI__sync_fetch_and_sub_2:
349 case Builtin::BI__sync_fetch_and_sub_4:
350 case Builtin::BI__sync_fetch_and_sub_8:
351 case Builtin::BI__sync_fetch_and_sub_16:
352 case Builtin::BI__sync_fetch_and_or:
353 case Builtin::BI__sync_fetch_and_or_1:
354 case Builtin::BI__sync_fetch_and_or_2:
355 case Builtin::BI__sync_fetch_and_or_4:
356 case Builtin::BI__sync_fetch_and_or_8:
357 case Builtin::BI__sync_fetch_and_or_16:
358 case Builtin::BI__sync_fetch_and_and:
359 case Builtin::BI__sync_fetch_and_and_1:
360 case Builtin::BI__sync_fetch_and_and_2:
361 case Builtin::BI__sync_fetch_and_and_4:
362 case Builtin::BI__sync_fetch_and_and_8:
363 case Builtin::BI__sync_fetch_and_and_16:
364 case Builtin::BI__sync_fetch_and_xor:
365 case Builtin::BI__sync_fetch_and_xor_1:
366 case Builtin::BI__sync_fetch_and_xor_2:
367 case Builtin::BI__sync_fetch_and_xor_4:
368 case Builtin::BI__sync_fetch_and_xor_8:
369 case Builtin::BI__sync_fetch_and_xor_16:
370 case Builtin::BI__sync_fetch_and_nand:
371 case Builtin::BI__sync_fetch_and_nand_1:
372 case Builtin::BI__sync_fetch_and_nand_2:
373 case Builtin::BI__sync_fetch_and_nand_4:
374 case Builtin::BI__sync_fetch_and_nand_8:
375 case Builtin::BI__sync_fetch_and_nand_16:
376 case Builtin::BI__sync_add_and_fetch:
377 case Builtin::BI__sync_add_and_fetch_1:
378 case Builtin::BI__sync_add_and_fetch_2:
379 case Builtin::BI__sync_add_and_fetch_4:
380 case Builtin::BI__sync_add_and_fetch_8:
381 case Builtin::BI__sync_add_and_fetch_16:
382 case Builtin::BI__sync_sub_and_fetch:
383 case Builtin::BI__sync_sub_and_fetch_1:
384 case Builtin::BI__sync_sub_and_fetch_2:
385 case Builtin::BI__sync_sub_and_fetch_4:
386 case Builtin::BI__sync_sub_and_fetch_8:
387 case Builtin::BI__sync_sub_and_fetch_16:
388 case Builtin::BI__sync_and_and_fetch:
389 case Builtin::BI__sync_and_and_fetch_1:
390 case Builtin::BI__sync_and_and_fetch_2:
391 case Builtin::BI__sync_and_and_fetch_4:
392 case Builtin::BI__sync_and_and_fetch_8:
393 case Builtin::BI__sync_and_and_fetch_16:
394 case Builtin::BI__sync_or_and_fetch:
395 case Builtin::BI__sync_or_and_fetch_1:
396 case Builtin::BI__sync_or_and_fetch_2:
397 case Builtin::BI__sync_or_and_fetch_4:
398 case Builtin::BI__sync_or_and_fetch_8:
399 case Builtin::BI__sync_or_and_fetch_16:
400 case Builtin::BI__sync_xor_and_fetch:
401 case Builtin::BI__sync_xor_and_fetch_1:
402 case Builtin::BI__sync_xor_and_fetch_2:
403 case Builtin::BI__sync_xor_and_fetch_4:
404 case Builtin::BI__sync_xor_and_fetch_8:
405 case Builtin::BI__sync_xor_and_fetch_16:
406 case Builtin::BI__sync_nand_and_fetch:
407 case Builtin::BI__sync_nand_and_fetch_1:
408 case Builtin::BI__sync_nand_and_fetch_2:
409 case Builtin::BI__sync_nand_and_fetch_4:
410 case Builtin::BI__sync_nand_and_fetch_8:
411 case Builtin::BI__sync_nand_and_fetch_16:
412 case Builtin::BI__sync_val_compare_and_swap:
413 case Builtin::BI__sync_val_compare_and_swap_1:
414 case Builtin::BI__sync_val_compare_and_swap_2:
415 case Builtin::BI__sync_val_compare_and_swap_4:
416 case Builtin::BI__sync_val_compare_and_swap_8:
417 case Builtin::BI__sync_val_compare_and_swap_16:
418 case Builtin::BI__sync_bool_compare_and_swap:
419 case Builtin::BI__sync_bool_compare_and_swap_1:
420 case Builtin::BI__sync_bool_compare_and_swap_2:
421 case Builtin::BI__sync_bool_compare_and_swap_4:
422 case Builtin::BI__sync_bool_compare_and_swap_8:
423 case Builtin::BI__sync_bool_compare_and_swap_16:
424 case Builtin::BI__sync_lock_test_and_set:
425 case Builtin::BI__sync_lock_test_and_set_1:
426 case Builtin::BI__sync_lock_test_and_set_2:
427 case Builtin::BI__sync_lock_test_and_set_4:
428 case Builtin::BI__sync_lock_test_and_set_8:
429 case Builtin::BI__sync_lock_test_and_set_16:
430 case Builtin::BI__sync_lock_release:
431 case Builtin::BI__sync_lock_release_1:
432 case Builtin::BI__sync_lock_release_2:
433 case Builtin::BI__sync_lock_release_4:
434 case Builtin::BI__sync_lock_release_8:
435 case Builtin::BI__sync_lock_release_16:
436 case Builtin::BI__sync_swap:
437 case Builtin::BI__sync_swap_1:
438 case Builtin::BI__sync_swap_2:
439 case Builtin::BI__sync_swap_4:
440 case Builtin::BI__sync_swap_8:
441 case Builtin::BI__sync_swap_16:
442 return SemaBuiltinAtomicOverloaded(TheCallResult);
443 #define BUILTIN(ID, TYPE, ATTRS)
444 #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
445 case Builtin::BI##ID: \
446 return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
447 #include "clang/Basic/Builtins.def"
448 case Builtin::BI__builtin_annotation:
449 if (SemaBuiltinAnnotation(*this, TheCall))
452 case Builtin::BI__builtin_addressof:
453 if (SemaBuiltinAddressof(*this, TheCall))
456 case Builtin::BI__builtin_operator_new:
457 case Builtin::BI__builtin_operator_delete:
458 if (!getLangOpts().CPlusPlus) {
459 Diag(TheCall->getExprLoc(), diag::err_builtin_requires_language)
460 << (BuiltinID == Builtin::BI__builtin_operator_new
461 ? "__builtin_operator_new"
462 : "__builtin_operator_delete")
466 // CodeGen assumes it can find the global new and delete to call,
467 // so ensure that they are declared.
468 DeclareGlobalNewDelete();
471 // check secure string manipulation functions where overflows
472 // are detectable at compile time
473 case Builtin::BI__builtin___memcpy_chk:
474 case Builtin::BI__builtin___memmove_chk:
475 case Builtin::BI__builtin___memset_chk:
476 case Builtin::BI__builtin___strlcat_chk:
477 case Builtin::BI__builtin___strlcpy_chk:
478 case Builtin::BI__builtin___strncat_chk:
479 case Builtin::BI__builtin___strncpy_chk:
480 case Builtin::BI__builtin___stpncpy_chk:
481 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 2, 3);
483 case Builtin::BI__builtin___memccpy_chk:
484 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 3, 4);
486 case Builtin::BI__builtin___snprintf_chk:
487 case Builtin::BI__builtin___vsnprintf_chk:
488 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 1, 3);
491 case Builtin::BI__builtin_call_with_static_chain:
492 if (SemaBuiltinCallWithStaticChain(*this, TheCall))
496 case Builtin::BI__exception_code:
497 case Builtin::BI_exception_code: {
498 if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope,
499 diag::err_seh___except_block))
503 case Builtin::BI__exception_info:
504 case Builtin::BI_exception_info: {
505 if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope,
506 diag::err_seh___except_filter))
511 case Builtin::BI__GetExceptionInfo:
512 if (checkArgCount(*this, TheCall, 1))
515 if (CheckCXXThrowOperand(
516 TheCall->getLocStart(),
517 Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()),
521 TheCall->setType(Context.VoidPtrTy);
526 // Since the target specific builtins for each arch overlap, only check those
527 // of the arch we are compiling for.
528 if (BuiltinID >= Builtin::FirstTSBuiltin) {
529 switch (Context.getTargetInfo().getTriple().getArch()) {
530 case llvm::Triple::arm:
531 case llvm::Triple::armeb:
532 case llvm::Triple::thumb:
533 case llvm::Triple::thumbeb:
534 if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
537 case llvm::Triple::aarch64:
538 case llvm::Triple::aarch64_be:
539 if (CheckAArch64BuiltinFunctionCall(BuiltinID, TheCall))
542 case llvm::Triple::mips:
543 case llvm::Triple::mipsel:
544 case llvm::Triple::mips64:
545 case llvm::Triple::mips64el:
546 if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall))
549 case llvm::Triple::systemz:
550 if (CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall))
553 case llvm::Triple::x86:
554 case llvm::Triple::x86_64:
555 if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall))
558 case llvm::Triple::ppc:
559 case llvm::Triple::ppc64:
560 case llvm::Triple::ppc64le:
561 if (CheckPPCBuiltinFunctionCall(BuiltinID, TheCall))
569 return TheCallResult;
572 // Get the valid immediate range for the specified NEON type code.
573 static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) {
574 NeonTypeFlags Type(t);
575 int IsQuad = ForceQuad ? true : Type.isQuad();
576 switch (Type.getEltType()) {
577 case NeonTypeFlags::Int8:
578 case NeonTypeFlags::Poly8:
579 return shift ? 7 : (8 << IsQuad) - 1;
580 case NeonTypeFlags::Int16:
581 case NeonTypeFlags::Poly16:
582 return shift ? 15 : (4 << IsQuad) - 1;
583 case NeonTypeFlags::Int32:
584 return shift ? 31 : (2 << IsQuad) - 1;
585 case NeonTypeFlags::Int64:
586 case NeonTypeFlags::Poly64:
587 return shift ? 63 : (1 << IsQuad) - 1;
588 case NeonTypeFlags::Poly128:
589 return shift ? 127 : (1 << IsQuad) - 1;
590 case NeonTypeFlags::Float16:
591 assert(!shift && "cannot shift float types!");
592 return (4 << IsQuad) - 1;
593 case NeonTypeFlags::Float32:
594 assert(!shift && "cannot shift float types!");
595 return (2 << IsQuad) - 1;
596 case NeonTypeFlags::Float64:
597 assert(!shift && "cannot shift float types!");
598 return (1 << IsQuad) - 1;
600 llvm_unreachable("Invalid NeonTypeFlag!");
603 /// getNeonEltType - Return the QualType corresponding to the elements of
604 /// the vector type specified by the NeonTypeFlags. This is used to check
605 /// the pointer arguments for Neon load/store intrinsics.
606 static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context,
607 bool IsPolyUnsigned, bool IsInt64Long) {
608 switch (Flags.getEltType()) {
609 case NeonTypeFlags::Int8:
610 return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy;
611 case NeonTypeFlags::Int16:
612 return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy;
613 case NeonTypeFlags::Int32:
614 return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy;
615 case NeonTypeFlags::Int64:
617 return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy;
619 return Flags.isUnsigned() ? Context.UnsignedLongLongTy
620 : Context.LongLongTy;
621 case NeonTypeFlags::Poly8:
622 return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy;
623 case NeonTypeFlags::Poly16:
624 return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy;
625 case NeonTypeFlags::Poly64:
627 return Context.UnsignedLongTy;
629 return Context.UnsignedLongLongTy;
630 case NeonTypeFlags::Poly128:
632 case NeonTypeFlags::Float16:
633 return Context.HalfTy;
634 case NeonTypeFlags::Float32:
635 return Context.FloatTy;
636 case NeonTypeFlags::Float64:
637 return Context.DoubleTy;
639 llvm_unreachable("Invalid NeonTypeFlag!");
642 bool Sema::CheckNeonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
647 bool HasConstPtr = false;
649 #define GET_NEON_OVERLOAD_CHECK
650 #include "clang/Basic/arm_neon.inc"
651 #undef GET_NEON_OVERLOAD_CHECK
654 // For NEON intrinsics which are overloaded on vector element type, validate
655 // the immediate which specifies which variant to emit.
656 unsigned ImmArg = TheCall->getNumArgs()-1;
658 if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
661 TV = Result.getLimitedValue(64);
662 if ((TV > 63) || (mask & (1ULL << TV)) == 0)
663 return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
664 << TheCall->getArg(ImmArg)->getSourceRange();
667 if (PtrArgNum >= 0) {
668 // Check that pointer arguments have the specified type.
669 Expr *Arg = TheCall->getArg(PtrArgNum);
670 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
671 Arg = ICE->getSubExpr();
672 ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
673 QualType RHSTy = RHS.get()->getType();
675 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
676 bool IsPolyUnsigned = Arch == llvm::Triple::aarch64;
678 Context.getTargetInfo().getInt64Type() == TargetInfo::SignedLong;
680 getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long);
682 EltTy = EltTy.withConst();
683 QualType LHSTy = Context.getPointerType(EltTy);
684 AssignConvertType ConvTy;
685 ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
688 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy,
689 RHS.get(), AA_Assigning))
693 // For NEON intrinsics which take an immediate value as part of the
694 // instruction, range check them here.
695 unsigned i = 0, l = 0, u = 0;
699 #define GET_NEON_IMMEDIATE_CHECK
700 #include "clang/Basic/arm_neon.inc"
701 #undef GET_NEON_IMMEDIATE_CHECK
704 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
707 bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall,
709 assert((BuiltinID == ARM::BI__builtin_arm_ldrex ||
710 BuiltinID == ARM::BI__builtin_arm_ldaex ||
711 BuiltinID == ARM::BI__builtin_arm_strex ||
712 BuiltinID == ARM::BI__builtin_arm_stlex ||
713 BuiltinID == AArch64::BI__builtin_arm_ldrex ||
714 BuiltinID == AArch64::BI__builtin_arm_ldaex ||
715 BuiltinID == AArch64::BI__builtin_arm_strex ||
716 BuiltinID == AArch64::BI__builtin_arm_stlex) &&
717 "unexpected ARM builtin");
718 bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex ||
719 BuiltinID == ARM::BI__builtin_arm_ldaex ||
720 BuiltinID == AArch64::BI__builtin_arm_ldrex ||
721 BuiltinID == AArch64::BI__builtin_arm_ldaex;
723 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
725 // Ensure that we have the proper number of arguments.
726 if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2))
729 // Inspect the pointer argument of the atomic builtin. This should always be
730 // a pointer type, whose element is an integral scalar or pointer type.
731 // Because it is a pointer type, we don't have to worry about any implicit
733 Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1);
734 ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg);
735 if (PointerArgRes.isInvalid())
737 PointerArg = PointerArgRes.get();
739 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
741 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
742 << PointerArg->getType() << PointerArg->getSourceRange();
746 // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next
747 // task is to insert the appropriate casts into the AST. First work out just
748 // what the appropriate type is.
749 QualType ValType = pointerType->getPointeeType();
750 QualType AddrType = ValType.getUnqualifiedType().withVolatile();
754 // Issue a warning if the cast is dodgy.
755 CastKind CastNeeded = CK_NoOp;
756 if (!AddrType.isAtLeastAsQualifiedAs(ValType)) {
757 CastNeeded = CK_BitCast;
758 Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers)
759 << PointerArg->getType()
760 << Context.getPointerType(AddrType)
761 << AA_Passing << PointerArg->getSourceRange();
764 // Finally, do the cast and replace the argument with the corrected version.
765 AddrType = Context.getPointerType(AddrType);
766 PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded);
767 if (PointerArgRes.isInvalid())
769 PointerArg = PointerArgRes.get();
771 TheCall->setArg(IsLdrex ? 0 : 1, PointerArg);
773 // In general, we allow ints, floats and pointers to be loaded and stored.
774 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
775 !ValType->isBlockPointerType() && !ValType->isFloatingType()) {
776 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr)
777 << PointerArg->getType() << PointerArg->getSourceRange();
781 // But ARM doesn't have instructions to deal with 128-bit versions.
782 if (Context.getTypeSize(ValType) > MaxWidth) {
783 assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate");
784 Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size)
785 << PointerArg->getType() << PointerArg->getSourceRange();
789 switch (ValType.getObjCLifetime()) {
790 case Qualifiers::OCL_None:
791 case Qualifiers::OCL_ExplicitNone:
795 case Qualifiers::OCL_Weak:
796 case Qualifiers::OCL_Strong:
797 case Qualifiers::OCL_Autoreleasing:
798 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
799 << ValType << PointerArg->getSourceRange();
805 TheCall->setType(ValType);
809 // Initialize the argument to be stored.
810 ExprResult ValArg = TheCall->getArg(0);
811 InitializedEntity Entity = InitializedEntity::InitializeParameter(
812 Context, ValType, /*consume*/ false);
813 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
814 if (ValArg.isInvalid())
816 TheCall->setArg(0, ValArg.get());
818 // __builtin_arm_strex always returns an int. It's marked as such in the .def,
819 // but the custom checker bypasses all default analysis.
820 TheCall->setType(Context.IntTy);
824 bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
827 if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
828 BuiltinID == ARM::BI__builtin_arm_ldaex ||
829 BuiltinID == ARM::BI__builtin_arm_strex ||
830 BuiltinID == ARM::BI__builtin_arm_stlex) {
831 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64);
834 if (BuiltinID == ARM::BI__builtin_arm_prefetch) {
835 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
836 SemaBuiltinConstantArgRange(TheCall, 2, 0, 1);
839 if (BuiltinID == ARM::BI__builtin_arm_rsr64 ||
840 BuiltinID == ARM::BI__builtin_arm_wsr64)
841 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 3, false);
843 if (BuiltinID == ARM::BI__builtin_arm_rsr ||
844 BuiltinID == ARM::BI__builtin_arm_rsrp ||
845 BuiltinID == ARM::BI__builtin_arm_wsr ||
846 BuiltinID == ARM::BI__builtin_arm_wsrp)
847 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
849 if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall))
852 // For intrinsics which take an immediate value as part of the instruction,
853 // range check them here.
854 unsigned i = 0, l = 0, u = 0;
856 default: return false;
857 case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break;
858 case ARM::BI__builtin_arm_usat: i = 1; u = 31; break;
859 case ARM::BI__builtin_arm_vcvtr_f:
860 case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break;
861 case ARM::BI__builtin_arm_dmb:
862 case ARM::BI__builtin_arm_dsb:
863 case ARM::BI__builtin_arm_isb:
864 case ARM::BI__builtin_arm_dbg: l = 0; u = 15; break;
867 // FIXME: VFP Intrinsics should error if VFP not present.
868 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
871 bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID,
875 if (BuiltinID == AArch64::BI__builtin_arm_ldrex ||
876 BuiltinID == AArch64::BI__builtin_arm_ldaex ||
877 BuiltinID == AArch64::BI__builtin_arm_strex ||
878 BuiltinID == AArch64::BI__builtin_arm_stlex) {
879 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128);
882 if (BuiltinID == AArch64::BI__builtin_arm_prefetch) {
883 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
884 SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) ||
885 SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) ||
886 SemaBuiltinConstantArgRange(TheCall, 4, 0, 1);
889 if (BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
890 BuiltinID == AArch64::BI__builtin_arm_wsr64)
891 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, false);
893 if (BuiltinID == AArch64::BI__builtin_arm_rsr ||
894 BuiltinID == AArch64::BI__builtin_arm_rsrp ||
895 BuiltinID == AArch64::BI__builtin_arm_wsr ||
896 BuiltinID == AArch64::BI__builtin_arm_wsrp)
897 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
899 if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall))
902 // For intrinsics which take an immediate value as part of the instruction,
903 // range check them here.
904 unsigned i = 0, l = 0, u = 0;
906 default: return false;
907 case AArch64::BI__builtin_arm_dmb:
908 case AArch64::BI__builtin_arm_dsb:
909 case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break;
912 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
915 bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
916 unsigned i = 0, l = 0, u = 0;
918 default: return false;
919 case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break;
920 case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break;
921 case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break;
922 case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break;
923 case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break;
924 case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break;
925 case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break;
928 return SemaBuiltinConstantArgRange(TheCall, i, l, u);
931 bool Sema::CheckPPCBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
932 unsigned i = 0, l = 0, u = 0;
933 bool Is64BitBltin = BuiltinID == PPC::BI__builtin_divde ||
934 BuiltinID == PPC::BI__builtin_divdeu ||
935 BuiltinID == PPC::BI__builtin_bpermd;
936 bool IsTarget64Bit = Context.getTargetInfo()
937 .getTypeWidth(Context
939 .getIntPtrType()) == 64;
940 bool IsBltinExtDiv = BuiltinID == PPC::BI__builtin_divwe ||
941 BuiltinID == PPC::BI__builtin_divweu ||
942 BuiltinID == PPC::BI__builtin_divde ||
943 BuiltinID == PPC::BI__builtin_divdeu;
945 if (Is64BitBltin && !IsTarget64Bit)
946 return Diag(TheCall->getLocStart(), diag::err_64_bit_builtin_32_bit_tgt)
947 << TheCall->getSourceRange();
949 if ((IsBltinExtDiv && !Context.getTargetInfo().hasFeature("extdiv")) ||
950 (BuiltinID == PPC::BI__builtin_bpermd &&
951 !Context.getTargetInfo().hasFeature("bpermd")))
952 return Diag(TheCall->getLocStart(), diag::err_ppc_builtin_only_on_pwr7)
953 << TheCall->getSourceRange();
956 default: return false;
957 case PPC::BI__builtin_altivec_crypto_vshasigmaw:
958 case PPC::BI__builtin_altivec_crypto_vshasigmad:
959 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
960 SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
961 case PPC::BI__builtin_tbegin:
962 case PPC::BI__builtin_tend: i = 0; l = 0; u = 1; break;
963 case PPC::BI__builtin_tsr: i = 0; l = 0; u = 7; break;
964 case PPC::BI__builtin_tabortwc:
965 case PPC::BI__builtin_tabortdc: i = 0; l = 0; u = 31; break;
966 case PPC::BI__builtin_tabortwci:
967 case PPC::BI__builtin_tabortdci:
968 return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) ||
969 SemaBuiltinConstantArgRange(TheCall, 2, 0, 31);
971 return SemaBuiltinConstantArgRange(TheCall, i, l, u);
974 bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID,
976 if (BuiltinID == SystemZ::BI__builtin_tabort) {
977 Expr *Arg = TheCall->getArg(0);
978 llvm::APSInt AbortCode(32);
979 if (Arg->isIntegerConstantExpr(AbortCode, Context) &&
980 AbortCode.getSExtValue() >= 0 && AbortCode.getSExtValue() < 256)
981 return Diag(Arg->getLocStart(), diag::err_systemz_invalid_tabort_code)
982 << Arg->getSourceRange();
985 // For intrinsics which take an immediate value as part of the instruction,
986 // range check them here.
987 unsigned i = 0, l = 0, u = 0;
989 default: return false;
990 case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break;
991 case SystemZ::BI__builtin_s390_verimb:
992 case SystemZ::BI__builtin_s390_verimh:
993 case SystemZ::BI__builtin_s390_verimf:
994 case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break;
995 case SystemZ::BI__builtin_s390_vfaeb:
996 case SystemZ::BI__builtin_s390_vfaeh:
997 case SystemZ::BI__builtin_s390_vfaef:
998 case SystemZ::BI__builtin_s390_vfaebs:
999 case SystemZ::BI__builtin_s390_vfaehs:
1000 case SystemZ::BI__builtin_s390_vfaefs:
1001 case SystemZ::BI__builtin_s390_vfaezb:
1002 case SystemZ::BI__builtin_s390_vfaezh:
1003 case SystemZ::BI__builtin_s390_vfaezf:
1004 case SystemZ::BI__builtin_s390_vfaezbs:
1005 case SystemZ::BI__builtin_s390_vfaezhs:
1006 case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break;
1007 case SystemZ::BI__builtin_s390_vfidb:
1008 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) ||
1009 SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
1010 case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break;
1011 case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break;
1012 case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break;
1013 case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break;
1014 case SystemZ::BI__builtin_s390_vstrcb:
1015 case SystemZ::BI__builtin_s390_vstrch:
1016 case SystemZ::BI__builtin_s390_vstrcf:
1017 case SystemZ::BI__builtin_s390_vstrczb:
1018 case SystemZ::BI__builtin_s390_vstrczh:
1019 case SystemZ::BI__builtin_s390_vstrczf:
1020 case SystemZ::BI__builtin_s390_vstrcbs:
1021 case SystemZ::BI__builtin_s390_vstrchs:
1022 case SystemZ::BI__builtin_s390_vstrcfs:
1023 case SystemZ::BI__builtin_s390_vstrczbs:
1024 case SystemZ::BI__builtin_s390_vstrczhs:
1025 case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break;
1027 return SemaBuiltinConstantArgRange(TheCall, i, l, u);
1030 bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
1031 unsigned i = 0, l = 0, u = 0;
1032 switch (BuiltinID) {
1033 default: return false;
1034 case X86::BI__builtin_cpu_supports:
1035 return SemaBuiltinCpuSupports(TheCall);
1036 case X86::BI_mm_prefetch: i = 1; l = 0; u = 3; break;
1037 case X86::BI__builtin_ia32_sha1rnds4: i = 2, l = 0; u = 3; break;
1038 case X86::BI__builtin_ia32_vpermil2pd:
1039 case X86::BI__builtin_ia32_vpermil2pd256:
1040 case X86::BI__builtin_ia32_vpermil2ps:
1041 case X86::BI__builtin_ia32_vpermil2ps256: i = 3, l = 0; u = 3; break;
1042 case X86::BI__builtin_ia32_cmpb128_mask:
1043 case X86::BI__builtin_ia32_cmpw128_mask:
1044 case X86::BI__builtin_ia32_cmpd128_mask:
1045 case X86::BI__builtin_ia32_cmpq128_mask:
1046 case X86::BI__builtin_ia32_cmpb256_mask:
1047 case X86::BI__builtin_ia32_cmpw256_mask:
1048 case X86::BI__builtin_ia32_cmpd256_mask:
1049 case X86::BI__builtin_ia32_cmpq256_mask:
1050 case X86::BI__builtin_ia32_cmpb512_mask:
1051 case X86::BI__builtin_ia32_cmpw512_mask:
1052 case X86::BI__builtin_ia32_cmpd512_mask:
1053 case X86::BI__builtin_ia32_cmpq512_mask:
1054 case X86::BI__builtin_ia32_ucmpb128_mask:
1055 case X86::BI__builtin_ia32_ucmpw128_mask:
1056 case X86::BI__builtin_ia32_ucmpd128_mask:
1057 case X86::BI__builtin_ia32_ucmpq128_mask:
1058 case X86::BI__builtin_ia32_ucmpb256_mask:
1059 case X86::BI__builtin_ia32_ucmpw256_mask:
1060 case X86::BI__builtin_ia32_ucmpd256_mask:
1061 case X86::BI__builtin_ia32_ucmpq256_mask:
1062 case X86::BI__builtin_ia32_ucmpb512_mask:
1063 case X86::BI__builtin_ia32_ucmpw512_mask:
1064 case X86::BI__builtin_ia32_ucmpd512_mask:
1065 case X86::BI__builtin_ia32_ucmpq512_mask: i = 2; l = 0; u = 7; break;
1066 case X86::BI__builtin_ia32_roundps:
1067 case X86::BI__builtin_ia32_roundpd:
1068 case X86::BI__builtin_ia32_roundps256:
1069 case X86::BI__builtin_ia32_roundpd256: i = 1, l = 0; u = 15; break;
1070 case X86::BI__builtin_ia32_roundss:
1071 case X86::BI__builtin_ia32_roundsd: i = 2, l = 0; u = 15; break;
1072 case X86::BI__builtin_ia32_cmpps:
1073 case X86::BI__builtin_ia32_cmpss:
1074 case X86::BI__builtin_ia32_cmppd:
1075 case X86::BI__builtin_ia32_cmpsd:
1076 case X86::BI__builtin_ia32_cmpps256:
1077 case X86::BI__builtin_ia32_cmppd256:
1078 case X86::BI__builtin_ia32_cmpps512_mask:
1079 case X86::BI__builtin_ia32_cmppd512_mask: i = 2; l = 0; u = 31; break;
1080 case X86::BI__builtin_ia32_vpcomub:
1081 case X86::BI__builtin_ia32_vpcomuw:
1082 case X86::BI__builtin_ia32_vpcomud:
1083 case X86::BI__builtin_ia32_vpcomuq:
1084 case X86::BI__builtin_ia32_vpcomb:
1085 case X86::BI__builtin_ia32_vpcomw:
1086 case X86::BI__builtin_ia32_vpcomd:
1087 case X86::BI__builtin_ia32_vpcomq: i = 2; l = 0; u = 7; break;
1089 return SemaBuiltinConstantArgRange(TheCall, i, l, u);
1092 /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
1093 /// parameter with the FormatAttr's correct format_idx and firstDataArg.
1094 /// Returns true when the format fits the function and the FormatStringInfo has
1096 bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
1097 FormatStringInfo *FSI) {
1098 FSI->HasVAListArg = Format->getFirstArg() == 0;
1099 FSI->FormatIdx = Format->getFormatIdx() - 1;
1100 FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1;
1102 // The way the format attribute works in GCC, the implicit this argument
1103 // of member functions is counted. However, it doesn't appear in our own
1104 // lists, so decrement format_idx in that case.
1106 if(FSI->FormatIdx == 0)
1109 if (FSI->FirstDataArg != 0)
1110 --FSI->FirstDataArg;
1115 /// Checks if a the given expression evaluates to null.
1117 /// \brief Returns true if the value evaluates to null.
1118 static bool CheckNonNullExpr(Sema &S,
1120 // If the expression has non-null type, it doesn't evaluate to null.
1121 if (auto nullability
1122 = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) {
1123 if (*nullability == NullabilityKind::NonNull)
1127 // As a special case, transparent unions initialized with zero are
1128 // considered null for the purposes of the nonnull attribute.
1129 if (const RecordType *UT = Expr->getType()->getAsUnionType()) {
1130 if (UT->getDecl()->hasAttr<TransparentUnionAttr>())
1131 if (const CompoundLiteralExpr *CLE =
1132 dyn_cast<CompoundLiteralExpr>(Expr))
1133 if (const InitListExpr *ILE =
1134 dyn_cast<InitListExpr>(CLE->getInitializer()))
1135 Expr = ILE->getInit(0);
1139 return (!Expr->isValueDependent() &&
1140 Expr->EvaluateAsBooleanCondition(Result, S.Context) &&
1144 static void CheckNonNullArgument(Sema &S,
1145 const Expr *ArgExpr,
1146 SourceLocation CallSiteLoc) {
1147 if (CheckNonNullExpr(S, ArgExpr))
1148 S.Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
1151 bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) {
1152 FormatStringInfo FSI;
1153 if ((GetFormatStringType(Format) == FST_NSString) &&
1154 getFormatStringInfo(Format, false, &FSI)) {
1155 Idx = FSI.FormatIdx;
1160 /// \brief Diagnose use of %s directive in an NSString which is being passed
1161 /// as formatting string to formatting method.
1163 DiagnoseCStringFormatDirectiveInCFAPI(Sema &S,
1164 const NamedDecl *FDecl,
1168 bool Format = false;
1169 ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily();
1170 if (SFFamily == ObjCStringFormatFamily::SFF_CFString) {
1175 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
1176 if (S.GetFormatNSStringIdx(I, Idx)) {
1181 if (!Format || NumArgs <= Idx)
1183 const Expr *FormatExpr = Args[Idx];
1184 if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr))
1185 FormatExpr = CSCE->getSubExpr();
1186 const StringLiteral *FormatString;
1187 if (const ObjCStringLiteral *OSL =
1188 dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts()))
1189 FormatString = OSL->getString();
1191 FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts());
1194 if (S.FormatStringHasSArg(FormatString)) {
1195 S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
1197 S.Diag(FDecl->getLocation(), diag::note_entity_declared_at)
1198 << FDecl->getDeclName();
1202 /// Determine whether the given type has a non-null nullability annotation.
1203 static bool isNonNullType(ASTContext &ctx, QualType type) {
1204 if (auto nullability = type->getNullability(ctx))
1205 return *nullability == NullabilityKind::NonNull;
1210 static void CheckNonNullArguments(Sema &S,
1211 const NamedDecl *FDecl,
1212 const FunctionProtoType *Proto,
1213 ArrayRef<const Expr *> Args,
1214 SourceLocation CallSiteLoc) {
1215 assert((FDecl || Proto) && "Need a function declaration or prototype");
1217 // Check the attributes attached to the method/function itself.
1218 llvm::SmallBitVector NonNullArgs;
1220 // Handle the nonnull attribute on the function/method declaration itself.
1221 for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) {
1222 if (!NonNull->args_size()) {
1223 // Easy case: all pointer arguments are nonnull.
1224 for (const auto *Arg : Args)
1225 if (S.isValidPointerAttrType(Arg->getType()))
1226 CheckNonNullArgument(S, Arg, CallSiteLoc);
1230 for (unsigned Val : NonNull->args()) {
1231 if (Val >= Args.size())
1233 if (NonNullArgs.empty())
1234 NonNullArgs.resize(Args.size());
1235 NonNullArgs.set(Val);
1240 if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) {
1241 // Handle the nonnull attribute on the parameters of the
1243 ArrayRef<ParmVarDecl*> parms;
1244 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl))
1245 parms = FD->parameters();
1247 parms = cast<ObjCMethodDecl>(FDecl)->parameters();
1249 unsigned ParamIndex = 0;
1250 for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end();
1251 I != E; ++I, ++ParamIndex) {
1252 const ParmVarDecl *PVD = *I;
1253 if (PVD->hasAttr<NonNullAttr>() ||
1254 isNonNullType(S.Context, PVD->getType())) {
1255 if (NonNullArgs.empty())
1256 NonNullArgs.resize(Args.size());
1258 NonNullArgs.set(ParamIndex);
1262 // If we have a non-function, non-method declaration but no
1263 // function prototype, try to dig out the function prototype.
1265 if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) {
1266 QualType type = VD->getType().getNonReferenceType();
1267 if (auto pointerType = type->getAs<PointerType>())
1268 type = pointerType->getPointeeType();
1269 else if (auto blockType = type->getAs<BlockPointerType>())
1270 type = blockType->getPointeeType();
1271 // FIXME: data member pointers?
1273 // Dig out the function prototype, if there is one.
1274 Proto = type->getAs<FunctionProtoType>();
1278 // Fill in non-null argument information from the nullability
1279 // information on the parameter types (if we have them).
1282 for (auto paramType : Proto->getParamTypes()) {
1283 if (isNonNullType(S.Context, paramType)) {
1284 if (NonNullArgs.empty())
1285 NonNullArgs.resize(Args.size());
1287 NonNullArgs.set(Index);
1295 // Check for non-null arguments.
1296 for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size();
1297 ArgIndex != ArgIndexEnd; ++ArgIndex) {
1298 if (NonNullArgs[ArgIndex])
1299 CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc);
1303 /// Handles the checks for format strings, non-POD arguments to vararg
1304 /// functions, and NULL arguments passed to non-NULL parameters.
1305 void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
1306 ArrayRef<const Expr *> Args, bool IsMemberFunction,
1307 SourceLocation Loc, SourceRange Range,
1308 VariadicCallType CallType) {
1309 // FIXME: We should check as much as we can in the template definition.
1310 if (CurContext->isDependentContext())
1313 // Printf and scanf checking.
1314 llvm::SmallBitVector CheckedVarArgs;
1316 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
1317 // Only create vector if there are format attributes.
1318 CheckedVarArgs.resize(Args.size());
1320 CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range,
1325 // Refuse POD arguments that weren't caught by the format string
1327 if (CallType != VariadicDoesNotApply) {
1328 unsigned NumParams = Proto ? Proto->getNumParams()
1329 : FDecl && isa<FunctionDecl>(FDecl)
1330 ? cast<FunctionDecl>(FDecl)->getNumParams()
1331 : FDecl && isa<ObjCMethodDecl>(FDecl)
1332 ? cast<ObjCMethodDecl>(FDecl)->param_size()
1335 for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) {
1336 // Args[ArgIdx] can be null in malformed code.
1337 if (const Expr *Arg = Args[ArgIdx]) {
1338 if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx])
1339 checkVariadicArgument(Arg, CallType);
1344 if (FDecl || Proto) {
1345 CheckNonNullArguments(*this, FDecl, Proto, Args, Loc);
1347 // Type safety checking.
1349 for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>())
1350 CheckArgumentWithTypeTag(I, Args.data());
1355 /// CheckConstructorCall - Check a constructor call for correctness and safety
1356 /// properties not enforced by the C type system.
1357 void Sema::CheckConstructorCall(FunctionDecl *FDecl,
1358 ArrayRef<const Expr *> Args,
1359 const FunctionProtoType *Proto,
1360 SourceLocation Loc) {
1361 VariadicCallType CallType =
1362 Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply;
1363 checkCall(FDecl, Proto, Args, /*IsMemberFunction=*/true, Loc, SourceRange(),
1367 /// CheckFunctionCall - Check a direct function call for various correctness
1368 /// and safety properties not strictly enforced by the C type system.
1369 bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
1370 const FunctionProtoType *Proto) {
1371 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
1372 isa<CXXMethodDecl>(FDecl);
1373 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
1374 IsMemberOperatorCall;
1375 VariadicCallType CallType = getVariadicCallType(FDecl, Proto,
1376 TheCall->getCallee());
1377 Expr** Args = TheCall->getArgs();
1378 unsigned NumArgs = TheCall->getNumArgs();
1379 if (IsMemberOperatorCall) {
1380 // If this is a call to a member operator, hide the first argument
1382 // FIXME: Our choice of AST representation here is less than ideal.
1386 checkCall(FDecl, Proto, llvm::makeArrayRef(Args, NumArgs),
1387 IsMemberFunction, TheCall->getRParenLoc(),
1388 TheCall->getCallee()->getSourceRange(), CallType);
1390 IdentifierInfo *FnInfo = FDecl->getIdentifier();
1391 // None of the checks below are needed for functions that don't have
1392 // simple names (e.g., C++ conversion functions).
1396 CheckAbsoluteValueFunction(TheCall, FDecl, FnInfo);
1397 if (getLangOpts().ObjC1)
1398 DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs);
1400 unsigned CMId = FDecl->getMemoryFunctionKind();
1404 // Handle memory setting and copying functions.
1405 if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat)
1406 CheckStrlcpycatArguments(TheCall, FnInfo);
1407 else if (CMId == Builtin::BIstrncat)
1408 CheckStrncatArguments(TheCall, FnInfo);
1410 CheckMemaccessArguments(TheCall, CMId, FnInfo);
1415 bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac,
1416 ArrayRef<const Expr *> Args) {
1417 VariadicCallType CallType =
1418 Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply;
1420 checkCall(Method, nullptr, Args,
1421 /*IsMemberFunction=*/false, lbrac, Method->getSourceRange(),
1427 bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
1428 const FunctionProtoType *Proto) {
1430 if (const auto *V = dyn_cast<VarDecl>(NDecl))
1431 Ty = V->getType().getNonReferenceType();
1432 else if (const auto *F = dyn_cast<FieldDecl>(NDecl))
1433 Ty = F->getType().getNonReferenceType();
1437 if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() &&
1438 !Ty->isFunctionProtoType())
1441 VariadicCallType CallType;
1442 if (!Proto || !Proto->isVariadic()) {
1443 CallType = VariadicDoesNotApply;
1444 } else if (Ty->isBlockPointerType()) {
1445 CallType = VariadicBlock;
1446 } else { // Ty->isFunctionPointerType()
1447 CallType = VariadicFunction;
1450 checkCall(NDecl, Proto,
1451 llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
1452 /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
1453 TheCall->getCallee()->getSourceRange(), CallType);
1458 /// Checks function calls when a FunctionDecl or a NamedDecl is not available,
1459 /// such as function pointers returned from functions.
1460 bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) {
1461 VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto,
1462 TheCall->getCallee());
1463 checkCall(/*FDecl=*/nullptr, Proto,
1464 llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
1465 /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
1466 TheCall->getCallee()->getSourceRange(), CallType);
1471 static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) {
1472 if (Ordering < AtomicExpr::AO_ABI_memory_order_relaxed ||
1473 Ordering > AtomicExpr::AO_ABI_memory_order_seq_cst)
1477 case AtomicExpr::AO__c11_atomic_init:
1478 llvm_unreachable("There is no ordering argument for an init");
1480 case AtomicExpr::AO__c11_atomic_load:
1481 case AtomicExpr::AO__atomic_load_n:
1482 case AtomicExpr::AO__atomic_load:
1483 return Ordering != AtomicExpr::AO_ABI_memory_order_release &&
1484 Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel;
1486 case AtomicExpr::AO__c11_atomic_store:
1487 case AtomicExpr::AO__atomic_store:
1488 case AtomicExpr::AO__atomic_store_n:
1489 return Ordering != AtomicExpr::AO_ABI_memory_order_consume &&
1490 Ordering != AtomicExpr::AO_ABI_memory_order_acquire &&
1491 Ordering != AtomicExpr::AO_ABI_memory_order_acq_rel;
1498 ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
1499 AtomicExpr::AtomicOp Op) {
1500 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
1501 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1503 // All these operations take one of the following forms:
1505 // C __c11_atomic_init(A *, C)
1507 // C __c11_atomic_load(A *, int)
1509 // void __atomic_load(A *, CP, int)
1511 // C __c11_atomic_add(A *, M, int)
1513 // C __atomic_exchange_n(A *, CP, int)
1515 // void __atomic_exchange(A *, C *, CP, int)
1517 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
1519 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
1522 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 };
1523 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 };
1525 // C is an appropriate type,
1526 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
1527 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
1528 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and
1529 // the int parameters are for orderings.
1531 static_assert(AtomicExpr::AO__c11_atomic_init == 0 &&
1532 AtomicExpr::AO__c11_atomic_fetch_xor + 1 ==
1533 AtomicExpr::AO__atomic_load,
1534 "need to update code for modified C11 atomics");
1535 bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init &&
1536 Op <= AtomicExpr::AO__c11_atomic_fetch_xor;
1537 bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
1538 Op == AtomicExpr::AO__atomic_store_n ||
1539 Op == AtomicExpr::AO__atomic_exchange_n ||
1540 Op == AtomicExpr::AO__atomic_compare_exchange_n;
1541 bool IsAddSub = false;
1544 case AtomicExpr::AO__c11_atomic_init:
1548 case AtomicExpr::AO__c11_atomic_load:
1549 case AtomicExpr::AO__atomic_load_n:
1553 case AtomicExpr::AO__c11_atomic_store:
1554 case AtomicExpr::AO__atomic_load:
1555 case AtomicExpr::AO__atomic_store:
1556 case AtomicExpr::AO__atomic_store_n:
1560 case AtomicExpr::AO__c11_atomic_fetch_add:
1561 case AtomicExpr::AO__c11_atomic_fetch_sub:
1562 case AtomicExpr::AO__atomic_fetch_add:
1563 case AtomicExpr::AO__atomic_fetch_sub:
1564 case AtomicExpr::AO__atomic_add_fetch:
1565 case AtomicExpr::AO__atomic_sub_fetch:
1568 case AtomicExpr::AO__c11_atomic_fetch_and:
1569 case AtomicExpr::AO__c11_atomic_fetch_or:
1570 case AtomicExpr::AO__c11_atomic_fetch_xor:
1571 case AtomicExpr::AO__atomic_fetch_and:
1572 case AtomicExpr::AO__atomic_fetch_or:
1573 case AtomicExpr::AO__atomic_fetch_xor:
1574 case AtomicExpr::AO__atomic_fetch_nand:
1575 case AtomicExpr::AO__atomic_and_fetch:
1576 case AtomicExpr::AO__atomic_or_fetch:
1577 case AtomicExpr::AO__atomic_xor_fetch:
1578 case AtomicExpr::AO__atomic_nand_fetch:
1582 case AtomicExpr::AO__c11_atomic_exchange:
1583 case AtomicExpr::AO__atomic_exchange_n:
1587 case AtomicExpr::AO__atomic_exchange:
1591 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
1592 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
1596 case AtomicExpr::AO__atomic_compare_exchange:
1597 case AtomicExpr::AO__atomic_compare_exchange_n:
1602 // Check we have the right number of arguments.
1603 if (TheCall->getNumArgs() < NumArgs[Form]) {
1604 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
1605 << 0 << NumArgs[Form] << TheCall->getNumArgs()
1606 << TheCall->getCallee()->getSourceRange();
1608 } else if (TheCall->getNumArgs() > NumArgs[Form]) {
1609 Diag(TheCall->getArg(NumArgs[Form])->getLocStart(),
1610 diag::err_typecheck_call_too_many_args)
1611 << 0 << NumArgs[Form] << TheCall->getNumArgs()
1612 << TheCall->getCallee()->getSourceRange();
1616 // Inspect the first argument of the atomic operation.
1617 Expr *Ptr = TheCall->getArg(0);
1618 Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get();
1619 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
1621 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
1622 << Ptr->getType() << Ptr->getSourceRange();
1626 // For a __c11 builtin, this should be a pointer to an _Atomic type.
1627 QualType AtomTy = pointerType->getPointeeType(); // 'A'
1628 QualType ValType = AtomTy; // 'C'
1630 if (!AtomTy->isAtomicType()) {
1631 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic)
1632 << Ptr->getType() << Ptr->getSourceRange();
1635 if (AtomTy.isConstQualified()) {
1636 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic)
1637 << Ptr->getType() << Ptr->getSourceRange();
1640 ValType = AtomTy->getAs<AtomicType>()->getValueType();
1643 // For an arithmetic operation, the implied arithmetic must be well-formed.
1644 if (Form == Arithmetic) {
1645 // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
1646 if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) {
1647 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
1648 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
1651 if (!IsAddSub && !ValType->isIntegerType()) {
1652 Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int)
1653 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
1656 if (IsC11 && ValType->isPointerType() &&
1657 RequireCompleteType(Ptr->getLocStart(), ValType->getPointeeType(),
1658 diag::err_incomplete_type)) {
1661 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
1662 // For __atomic_*_n operations, the value type must be a scalar integral or
1663 // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
1664 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
1665 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
1669 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) &&
1670 !AtomTy->isScalarType()) {
1671 // For GNU atomics, require a trivially-copyable type. This is not part of
1672 // the GNU atomics specification, but we enforce it for sanity.
1673 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy)
1674 << Ptr->getType() << Ptr->getSourceRange();
1678 // FIXME: For any builtin other than a load, the ValType must not be
1681 switch (ValType.getObjCLifetime()) {
1682 case Qualifiers::OCL_None:
1683 case Qualifiers::OCL_ExplicitNone:
1687 case Qualifiers::OCL_Weak:
1688 case Qualifiers::OCL_Strong:
1689 case Qualifiers::OCL_Autoreleasing:
1690 // FIXME: Can this happen? By this point, ValType should be known
1691 // to be trivially copyable.
1692 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
1693 << ValType << Ptr->getSourceRange();
1697 // atomic_fetch_or takes a pointer to a volatile 'A'. We shouldn't let the
1698 // volatile-ness of the pointee-type inject itself into the result or the
1700 ValType.removeLocalVolatile();
1701 QualType ResultType = ValType;
1702 if (Form == Copy || Form == GNUXchg || Form == Init)
1703 ResultType = Context.VoidTy;
1704 else if (Form == C11CmpXchg || Form == GNUCmpXchg)
1705 ResultType = Context.BoolTy;
1707 // The type of a parameter passed 'by value'. In the GNU atomics, such
1708 // arguments are actually passed as pointers.
1709 QualType ByValType = ValType; // 'CP'
1711 ByValType = Ptr->getType();
1713 // The first argument --- the pointer --- has a fixed type; we
1714 // deduce the types of the rest of the arguments accordingly. Walk
1715 // the remaining arguments, converting them to the deduced value type.
1716 for (unsigned i = 1; i != NumArgs[Form]; ++i) {
1718 if (i < NumVals[Form] + 1) {
1721 // The second argument is the non-atomic operand. For arithmetic, this
1722 // is always passed by value, and for a compare_exchange it is always
1723 // passed by address. For the rest, GNU uses by-address and C11 uses
1725 assert(Form != Load);
1726 if (Form == Init || (Form == Arithmetic && ValType->isIntegerType()))
1728 else if (Form == Copy || Form == Xchg)
1730 else if (Form == Arithmetic)
1731 Ty = Context.getPointerDiffType();
1733 Ty = Context.getPointerType(ValType.getUnqualifiedType());
1736 // The third argument to compare_exchange / GNU exchange is a
1737 // (pointer to a) desired value.
1741 // The fourth argument to GNU compare_exchange is a 'weak' flag.
1742 Ty = Context.BoolTy;
1746 // The order(s) are always converted to int.
1750 InitializedEntity Entity =
1751 InitializedEntity::InitializeParameter(Context, Ty, false);
1752 ExprResult Arg = TheCall->getArg(i);
1753 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
1754 if (Arg.isInvalid())
1756 TheCall->setArg(i, Arg.get());
1759 // Permute the arguments into a 'consistent' order.
1760 SmallVector<Expr*, 5> SubExprs;
1761 SubExprs.push_back(Ptr);
1764 // Note, AtomicExpr::getVal1() has a special case for this atomic.
1765 SubExprs.push_back(TheCall->getArg(1)); // Val1
1768 SubExprs.push_back(TheCall->getArg(1)); // Order
1773 SubExprs.push_back(TheCall->getArg(2)); // Order
1774 SubExprs.push_back(TheCall->getArg(1)); // Val1
1777 // Note, AtomicExpr::getVal2() has a special case for this atomic.
1778 SubExprs.push_back(TheCall->getArg(3)); // Order
1779 SubExprs.push_back(TheCall->getArg(1)); // Val1
1780 SubExprs.push_back(TheCall->getArg(2)); // Val2
1783 SubExprs.push_back(TheCall->getArg(3)); // Order
1784 SubExprs.push_back(TheCall->getArg(1)); // Val1
1785 SubExprs.push_back(TheCall->getArg(4)); // OrderFail
1786 SubExprs.push_back(TheCall->getArg(2)); // Val2
1789 SubExprs.push_back(TheCall->getArg(4)); // Order
1790 SubExprs.push_back(TheCall->getArg(1)); // Val1
1791 SubExprs.push_back(TheCall->getArg(5)); // OrderFail
1792 SubExprs.push_back(TheCall->getArg(2)); // Val2
1793 SubExprs.push_back(TheCall->getArg(3)); // Weak
1797 if (SubExprs.size() >= 2 && Form != Init) {
1798 llvm::APSInt Result(32);
1799 if (SubExprs[1]->isIntegerConstantExpr(Result, Context) &&
1800 !isValidOrderingForOp(Result.getSExtValue(), Op))
1801 Diag(SubExprs[1]->getLocStart(),
1802 diag::warn_atomic_op_has_invalid_memory_order)
1803 << SubExprs[1]->getSourceRange();
1806 AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(),
1807 SubExprs, ResultType, Op,
1808 TheCall->getRParenLoc());
1810 if ((Op == AtomicExpr::AO__c11_atomic_load ||
1811 (Op == AtomicExpr::AO__c11_atomic_store)) &&
1812 Context.AtomicUsesUnsupportedLibcall(AE))
1813 Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib) <<
1814 ((Op == AtomicExpr::AO__c11_atomic_load) ? 0 : 1);
1820 /// checkBuiltinArgument - Given a call to a builtin function, perform
1821 /// normal type-checking on the given argument, updating the call in
1822 /// place. This is useful when a builtin function requires custom
1823 /// type-checking for some of its arguments but not necessarily all of
1826 /// Returns true on error.
1827 static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
1828 FunctionDecl *Fn = E->getDirectCallee();
1829 assert(Fn && "builtin call without direct callee!");
1831 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
1832 InitializedEntity Entity =
1833 InitializedEntity::InitializeParameter(S.Context, Param);
1835 ExprResult Arg = E->getArg(0);
1836 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
1837 if (Arg.isInvalid())
1840 E->setArg(ArgIndex, Arg.get());
1844 /// SemaBuiltinAtomicOverloaded - We have a call to a function like
1845 /// __sync_fetch_and_add, which is an overloaded function based on the pointer
1846 /// type of its first argument. The main ActOnCallExpr routines have already
1847 /// promoted the types of arguments because all of these calls are prototyped as
1850 /// This function goes through and does final semantic checking for these
1853 Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
1854 CallExpr *TheCall = (CallExpr *)TheCallResult.get();
1855 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1856 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
1858 // Ensure that we have at least one argument to do type inference from.
1859 if (TheCall->getNumArgs() < 1) {
1860 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
1861 << 0 << 1 << TheCall->getNumArgs()
1862 << TheCall->getCallee()->getSourceRange();
1866 // Inspect the first argument of the atomic builtin. This should always be
1867 // a pointer type, whose element is an integral scalar or pointer type.
1868 // Because it is a pointer type, we don't have to worry about any implicit
1870 // FIXME: We don't allow floating point scalars as input.
1871 Expr *FirstArg = TheCall->getArg(0);
1872 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
1873 if (FirstArgResult.isInvalid())
1875 FirstArg = FirstArgResult.get();
1876 TheCall->setArg(0, FirstArg);
1878 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
1880 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
1881 << FirstArg->getType() << FirstArg->getSourceRange();
1885 QualType ValType = pointerType->getPointeeType();
1886 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
1887 !ValType->isBlockPointerType()) {
1888 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
1889 << FirstArg->getType() << FirstArg->getSourceRange();
1893 switch (ValType.getObjCLifetime()) {
1894 case Qualifiers::OCL_None:
1895 case Qualifiers::OCL_ExplicitNone:
1899 case Qualifiers::OCL_Weak:
1900 case Qualifiers::OCL_Strong:
1901 case Qualifiers::OCL_Autoreleasing:
1902 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
1903 << ValType << FirstArg->getSourceRange();
1907 // Strip any qualifiers off ValType.
1908 ValType = ValType.getUnqualifiedType();
1910 // The majority of builtins return a value, but a few have special return
1911 // types, so allow them to override appropriately below.
1912 QualType ResultType = ValType;
1914 // We need to figure out which concrete builtin this maps onto. For example,
1915 // __sync_fetch_and_add with a 2 byte object turns into
1916 // __sync_fetch_and_add_2.
1917 #define BUILTIN_ROW(x) \
1918 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
1919 Builtin::BI##x##_8, Builtin::BI##x##_16 }
1921 static const unsigned BuiltinIndices[][5] = {
1922 BUILTIN_ROW(__sync_fetch_and_add),
1923 BUILTIN_ROW(__sync_fetch_and_sub),
1924 BUILTIN_ROW(__sync_fetch_and_or),
1925 BUILTIN_ROW(__sync_fetch_and_and),
1926 BUILTIN_ROW(__sync_fetch_and_xor),
1927 BUILTIN_ROW(__sync_fetch_and_nand),
1929 BUILTIN_ROW(__sync_add_and_fetch),
1930 BUILTIN_ROW(__sync_sub_and_fetch),
1931 BUILTIN_ROW(__sync_and_and_fetch),
1932 BUILTIN_ROW(__sync_or_and_fetch),
1933 BUILTIN_ROW(__sync_xor_and_fetch),
1934 BUILTIN_ROW(__sync_nand_and_fetch),
1936 BUILTIN_ROW(__sync_val_compare_and_swap),
1937 BUILTIN_ROW(__sync_bool_compare_and_swap),
1938 BUILTIN_ROW(__sync_lock_test_and_set),
1939 BUILTIN_ROW(__sync_lock_release),
1940 BUILTIN_ROW(__sync_swap)
1944 // Determine the index of the size.
1946 switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
1947 case 1: SizeIndex = 0; break;
1948 case 2: SizeIndex = 1; break;
1949 case 4: SizeIndex = 2; break;
1950 case 8: SizeIndex = 3; break;
1951 case 16: SizeIndex = 4; break;
1953 Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
1954 << FirstArg->getType() << FirstArg->getSourceRange();
1958 // Each of these builtins has one pointer argument, followed by some number of
1959 // values (0, 1 or 2) followed by a potentially empty varags list of stuff
1960 // that we ignore. Find out which row of BuiltinIndices to read from as well
1961 // as the number of fixed args.
1962 unsigned BuiltinID = FDecl->getBuiltinID();
1963 unsigned BuiltinIndex, NumFixed = 1;
1964 bool WarnAboutSemanticsChange = false;
1965 switch (BuiltinID) {
1966 default: llvm_unreachable("Unknown overloaded atomic builtin!");
1967 case Builtin::BI__sync_fetch_and_add:
1968 case Builtin::BI__sync_fetch_and_add_1:
1969 case Builtin::BI__sync_fetch_and_add_2:
1970 case Builtin::BI__sync_fetch_and_add_4:
1971 case Builtin::BI__sync_fetch_and_add_8:
1972 case Builtin::BI__sync_fetch_and_add_16:
1976 case Builtin::BI__sync_fetch_and_sub:
1977 case Builtin::BI__sync_fetch_and_sub_1:
1978 case Builtin::BI__sync_fetch_and_sub_2:
1979 case Builtin::BI__sync_fetch_and_sub_4:
1980 case Builtin::BI__sync_fetch_and_sub_8:
1981 case Builtin::BI__sync_fetch_and_sub_16:
1985 case Builtin::BI__sync_fetch_and_or:
1986 case Builtin::BI__sync_fetch_and_or_1:
1987 case Builtin::BI__sync_fetch_and_or_2:
1988 case Builtin::BI__sync_fetch_and_or_4:
1989 case Builtin::BI__sync_fetch_and_or_8:
1990 case Builtin::BI__sync_fetch_and_or_16:
1994 case Builtin::BI__sync_fetch_and_and:
1995 case Builtin::BI__sync_fetch_and_and_1:
1996 case Builtin::BI__sync_fetch_and_and_2:
1997 case Builtin::BI__sync_fetch_and_and_4:
1998 case Builtin::BI__sync_fetch_and_and_8:
1999 case Builtin::BI__sync_fetch_and_and_16:
2003 case Builtin::BI__sync_fetch_and_xor:
2004 case Builtin::BI__sync_fetch_and_xor_1:
2005 case Builtin::BI__sync_fetch_and_xor_2:
2006 case Builtin::BI__sync_fetch_and_xor_4:
2007 case Builtin::BI__sync_fetch_and_xor_8:
2008 case Builtin::BI__sync_fetch_and_xor_16:
2012 case Builtin::BI__sync_fetch_and_nand:
2013 case Builtin::BI__sync_fetch_and_nand_1:
2014 case Builtin::BI__sync_fetch_and_nand_2:
2015 case Builtin::BI__sync_fetch_and_nand_4:
2016 case Builtin::BI__sync_fetch_and_nand_8:
2017 case Builtin::BI__sync_fetch_and_nand_16:
2019 WarnAboutSemanticsChange = true;
2022 case Builtin::BI__sync_add_and_fetch:
2023 case Builtin::BI__sync_add_and_fetch_1:
2024 case Builtin::BI__sync_add_and_fetch_2:
2025 case Builtin::BI__sync_add_and_fetch_4:
2026 case Builtin::BI__sync_add_and_fetch_8:
2027 case Builtin::BI__sync_add_and_fetch_16:
2031 case Builtin::BI__sync_sub_and_fetch:
2032 case Builtin::BI__sync_sub_and_fetch_1:
2033 case Builtin::BI__sync_sub_and_fetch_2:
2034 case Builtin::BI__sync_sub_and_fetch_4:
2035 case Builtin::BI__sync_sub_and_fetch_8:
2036 case Builtin::BI__sync_sub_and_fetch_16:
2040 case Builtin::BI__sync_and_and_fetch:
2041 case Builtin::BI__sync_and_and_fetch_1:
2042 case Builtin::BI__sync_and_and_fetch_2:
2043 case Builtin::BI__sync_and_and_fetch_4:
2044 case Builtin::BI__sync_and_and_fetch_8:
2045 case Builtin::BI__sync_and_and_fetch_16:
2049 case Builtin::BI__sync_or_and_fetch:
2050 case Builtin::BI__sync_or_and_fetch_1:
2051 case Builtin::BI__sync_or_and_fetch_2:
2052 case Builtin::BI__sync_or_and_fetch_4:
2053 case Builtin::BI__sync_or_and_fetch_8:
2054 case Builtin::BI__sync_or_and_fetch_16:
2058 case Builtin::BI__sync_xor_and_fetch:
2059 case Builtin::BI__sync_xor_and_fetch_1:
2060 case Builtin::BI__sync_xor_and_fetch_2:
2061 case Builtin::BI__sync_xor_and_fetch_4:
2062 case Builtin::BI__sync_xor_and_fetch_8:
2063 case Builtin::BI__sync_xor_and_fetch_16:
2067 case Builtin::BI__sync_nand_and_fetch:
2068 case Builtin::BI__sync_nand_and_fetch_1:
2069 case Builtin::BI__sync_nand_and_fetch_2:
2070 case Builtin::BI__sync_nand_and_fetch_4:
2071 case Builtin::BI__sync_nand_and_fetch_8:
2072 case Builtin::BI__sync_nand_and_fetch_16:
2074 WarnAboutSemanticsChange = true;
2077 case Builtin::BI__sync_val_compare_and_swap:
2078 case Builtin::BI__sync_val_compare_and_swap_1:
2079 case Builtin::BI__sync_val_compare_and_swap_2:
2080 case Builtin::BI__sync_val_compare_and_swap_4:
2081 case Builtin::BI__sync_val_compare_and_swap_8:
2082 case Builtin::BI__sync_val_compare_and_swap_16:
2087 case Builtin::BI__sync_bool_compare_and_swap:
2088 case Builtin::BI__sync_bool_compare_and_swap_1:
2089 case Builtin::BI__sync_bool_compare_and_swap_2:
2090 case Builtin::BI__sync_bool_compare_and_swap_4:
2091 case Builtin::BI__sync_bool_compare_and_swap_8:
2092 case Builtin::BI__sync_bool_compare_and_swap_16:
2095 ResultType = Context.BoolTy;
2098 case Builtin::BI__sync_lock_test_and_set:
2099 case Builtin::BI__sync_lock_test_and_set_1:
2100 case Builtin::BI__sync_lock_test_and_set_2:
2101 case Builtin::BI__sync_lock_test_and_set_4:
2102 case Builtin::BI__sync_lock_test_and_set_8:
2103 case Builtin::BI__sync_lock_test_and_set_16:
2107 case Builtin::BI__sync_lock_release:
2108 case Builtin::BI__sync_lock_release_1:
2109 case Builtin::BI__sync_lock_release_2:
2110 case Builtin::BI__sync_lock_release_4:
2111 case Builtin::BI__sync_lock_release_8:
2112 case Builtin::BI__sync_lock_release_16:
2115 ResultType = Context.VoidTy;
2118 case Builtin::BI__sync_swap:
2119 case Builtin::BI__sync_swap_1:
2120 case Builtin::BI__sync_swap_2:
2121 case Builtin::BI__sync_swap_4:
2122 case Builtin::BI__sync_swap_8:
2123 case Builtin::BI__sync_swap_16:
2128 // Now that we know how many fixed arguments we expect, first check that we
2129 // have at least that many.
2130 if (TheCall->getNumArgs() < 1+NumFixed) {
2131 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
2132 << 0 << 1+NumFixed << TheCall->getNumArgs()
2133 << TheCall->getCallee()->getSourceRange();
2137 if (WarnAboutSemanticsChange) {
2138 Diag(TheCall->getLocEnd(), diag::warn_sync_fetch_and_nand_semantics_change)
2139 << TheCall->getCallee()->getSourceRange();
2142 // Get the decl for the concrete builtin from this, we can tell what the
2143 // concrete integer type we should convert to is.
2144 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
2145 const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID);
2146 FunctionDecl *NewBuiltinDecl;
2147 if (NewBuiltinID == BuiltinID)
2148 NewBuiltinDecl = FDecl;
2150 // Perform builtin lookup to avoid redeclaring it.
2151 DeclarationName DN(&Context.Idents.get(NewBuiltinName));
2152 LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName);
2153 LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true);
2154 assert(Res.getFoundDecl());
2155 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
2156 if (!NewBuiltinDecl)
2160 // The first argument --- the pointer --- has a fixed type; we
2161 // deduce the types of the rest of the arguments accordingly. Walk
2162 // the remaining arguments, converting them to the deduced value type.
2163 for (unsigned i = 0; i != NumFixed; ++i) {
2164 ExprResult Arg = TheCall->getArg(i+1);
2166 // GCC does an implicit conversion to the pointer or integer ValType. This
2167 // can fail in some cases (1i -> int**), check for this error case now.
2168 // Initialize the argument.
2169 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
2170 ValType, /*consume*/ false);
2171 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
2172 if (Arg.isInvalid())
2175 // Okay, we have something that *can* be converted to the right type. Check
2176 // to see if there is a potentially weird extension going on here. This can
2177 // happen when you do an atomic operation on something like an char* and
2178 // pass in 42. The 42 gets converted to char. This is even more strange
2179 // for things like 45.123 -> char, etc.
2180 // FIXME: Do this check.
2181 TheCall->setArg(i+1, Arg.get());
2184 ASTContext& Context = this->getASTContext();
2186 // Create a new DeclRefExpr to refer to the new decl.
2187 DeclRefExpr* NewDRE = DeclRefExpr::Create(
2189 DRE->getQualifierLoc(),
2192 /*enclosing*/ false,
2194 Context.BuiltinFnTy,
2195 DRE->getValueKind());
2197 // Set the callee in the CallExpr.
2198 // FIXME: This loses syntactic information.
2199 QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType());
2200 ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy,
2201 CK_BuiltinFnToFnPtr);
2202 TheCall->setCallee(PromotedCall.get());
2204 // Change the result type of the call to match the original value type. This
2205 // is arbitrary, but the codegen for these builtins ins design to handle it
2207 TheCall->setType(ResultType);
2209 return TheCallResult;
2212 /// CheckObjCString - Checks that the argument to the builtin
2213 /// CFString constructor is correct
2214 /// Note: It might also make sense to do the UTF-16 conversion here (would
2215 /// simplify the backend).
2216 bool Sema::CheckObjCString(Expr *Arg) {
2217 Arg = Arg->IgnoreParenCasts();
2218 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
2220 if (!Literal || !Literal->isAscii()) {
2221 Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
2222 << Arg->getSourceRange();
2226 if (Literal->containsNonAsciiOrNull()) {
2227 StringRef String = Literal->getString();
2228 unsigned NumBytes = String.size();
2229 SmallVector<UTF16, 128> ToBuf(NumBytes);
2230 const UTF8 *FromPtr = (const UTF8 *)String.data();
2231 UTF16 *ToPtr = &ToBuf[0];
2233 ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
2234 &ToPtr, ToPtr + NumBytes,
2236 // Check for conversion failure.
2237 if (Result != conversionOK)
2238 Diag(Arg->getLocStart(),
2239 diag::warn_cfstring_truncated) << Arg->getSourceRange();
2244 /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
2245 /// Emit an error and return true on failure, return false on success.
2246 bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
2247 Expr *Fn = TheCall->getCallee();
2248 if (TheCall->getNumArgs() > 2) {
2249 Diag(TheCall->getArg(2)->getLocStart(),
2250 diag::err_typecheck_call_too_many_args)
2251 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
2252 << Fn->getSourceRange()
2253 << SourceRange(TheCall->getArg(2)->getLocStart(),
2254 (*(TheCall->arg_end()-1))->getLocEnd());
2258 if (TheCall->getNumArgs() < 2) {
2259 return Diag(TheCall->getLocEnd(),
2260 diag::err_typecheck_call_too_few_args_at_least)
2261 << 0 /*function call*/ << 2 << TheCall->getNumArgs();
2264 // Type-check the first argument normally.
2265 if (checkBuiltinArgument(*this, TheCall, 0))
2268 // Determine whether the current function is variadic or not.
2269 BlockScopeInfo *CurBlock = getCurBlock();
2272 isVariadic = CurBlock->TheDecl->isVariadic();
2273 else if (FunctionDecl *FD = getCurFunctionDecl())
2274 isVariadic = FD->isVariadic();
2276 isVariadic = getCurMethodDecl()->isVariadic();
2279 Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
2283 // Verify that the second argument to the builtin is the last argument of the
2284 // current function or method.
2285 bool SecondArgIsLastNamedArgument = false;
2286 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
2288 // These are valid if SecondArgIsLastNamedArgument is false after the next
2291 SourceLocation ParamLoc;
2293 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
2294 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
2295 // FIXME: This isn't correct for methods (results in bogus warning).
2296 // Get the last formal in the current function.
2297 const ParmVarDecl *LastArg;
2299 LastArg = *(CurBlock->TheDecl->param_end()-1);
2300 else if (FunctionDecl *FD = getCurFunctionDecl())
2301 LastArg = *(FD->param_end()-1);
2303 LastArg = *(getCurMethodDecl()->param_end()-1);
2304 SecondArgIsLastNamedArgument = PV == LastArg;
2306 Type = PV->getType();
2307 ParamLoc = PV->getLocation();
2311 if (!SecondArgIsLastNamedArgument)
2312 Diag(TheCall->getArg(1)->getLocStart(),
2313 diag::warn_second_parameter_of_va_start_not_last_named_argument);
2314 else if (Type->isReferenceType()) {
2315 Diag(Arg->getLocStart(),
2316 diag::warn_va_start_of_reference_type_is_undefined);
2317 Diag(ParamLoc, diag::note_parameter_type) << Type;
2320 TheCall->setType(Context.VoidTy);
2324 bool Sema::SemaBuiltinVAStartARM(CallExpr *Call) {
2325 // void __va_start(va_list *ap, const char *named_addr, size_t slot_size,
2326 // const char *named_addr);
2328 Expr *Func = Call->getCallee();
2330 if (Call->getNumArgs() < 3)
2331 return Diag(Call->getLocEnd(),
2332 diag::err_typecheck_call_too_few_args_at_least)
2333 << 0 /*function call*/ << 3 << Call->getNumArgs();
2335 // Determine whether the current function is variadic or not.
2337 if (BlockScopeInfo *CurBlock = getCurBlock())
2338 IsVariadic = CurBlock->TheDecl->isVariadic();
2339 else if (FunctionDecl *FD = getCurFunctionDecl())
2340 IsVariadic = FD->isVariadic();
2341 else if (ObjCMethodDecl *MD = getCurMethodDecl())
2342 IsVariadic = MD->isVariadic();
2344 llvm_unreachable("unexpected statement type");
2347 Diag(Func->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
2351 // Type-check the first argument normally.
2352 if (checkBuiltinArgument(*this, Call, 0))
2358 } ArgumentTypes[] = {
2359 { 1, Context.getPointerType(Context.CharTy.withConst()) },
2360 { 2, Context.getSizeType() },
2363 for (const auto &AT : ArgumentTypes) {
2364 const Expr *Arg = Call->getArg(AT.ArgNo)->IgnoreParens();
2365 if (Arg->getType().getCanonicalType() == AT.Type.getCanonicalType())
2367 Diag(Arg->getLocStart(), diag::err_typecheck_convert_incompatible)
2368 << Arg->getType() << AT.Type << 1 /* different class */
2369 << 0 /* qualifier difference */ << 3 /* parameter mismatch */
2370 << AT.ArgNo + 1 << Arg->getType() << AT.Type;
2376 /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
2377 /// friends. This is declared to take (...), so we have to check everything.
2378 bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
2379 if (TheCall->getNumArgs() < 2)
2380 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
2381 << 0 << 2 << TheCall->getNumArgs()/*function call*/;
2382 if (TheCall->getNumArgs() > 2)
2383 return Diag(TheCall->getArg(2)->getLocStart(),
2384 diag::err_typecheck_call_too_many_args)
2385 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
2386 << SourceRange(TheCall->getArg(2)->getLocStart(),
2387 (*(TheCall->arg_end()-1))->getLocEnd());
2389 ExprResult OrigArg0 = TheCall->getArg(0);
2390 ExprResult OrigArg1 = TheCall->getArg(1);
2392 // Do standard promotions between the two arguments, returning their common
2394 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
2395 if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
2398 // Make sure any conversions are pushed back into the call; this is
2399 // type safe since unordered compare builtins are declared as "_Bool
2401 TheCall->setArg(0, OrigArg0.get());
2402 TheCall->setArg(1, OrigArg1.get());
2404 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
2407 // If the common type isn't a real floating type, then the arguments were
2408 // invalid for this operation.
2409 if (Res.isNull() || !Res->isRealFloatingType())
2410 return Diag(OrigArg0.get()->getLocStart(),
2411 diag::err_typecheck_call_invalid_ordered_compare)
2412 << OrigArg0.get()->getType() << OrigArg1.get()->getType()
2413 << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd());
2418 /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
2419 /// __builtin_isnan and friends. This is declared to take (...), so we have
2420 /// to check everything. We expect the last argument to be a floating point
2422 bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
2423 if (TheCall->getNumArgs() < NumArgs)
2424 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
2425 << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
2426 if (TheCall->getNumArgs() > NumArgs)
2427 return Diag(TheCall->getArg(NumArgs)->getLocStart(),
2428 diag::err_typecheck_call_too_many_args)
2429 << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
2430 << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
2431 (*(TheCall->arg_end()-1))->getLocEnd());
2433 Expr *OrigArg = TheCall->getArg(NumArgs-1);
2435 if (OrigArg->isTypeDependent())
2438 // This operation requires a non-_Complex floating-point number.
2439 if (!OrigArg->getType()->isRealFloatingType())
2440 return Diag(OrigArg->getLocStart(),
2441 diag::err_typecheck_call_invalid_unary_fp)
2442 << OrigArg->getType() << OrigArg->getSourceRange();
2444 // If this is an implicit conversion from float -> double, remove it.
2445 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
2446 Expr *CastArg = Cast->getSubExpr();
2447 if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
2448 assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) &&
2449 "promotion from float to double is the only expected cast here");
2450 Cast->setSubExpr(nullptr);
2451 TheCall->setArg(NumArgs-1, CastArg);
2458 /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
2459 // This is declared to take (...), so we have to check everything.
2460 ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
2461 if (TheCall->getNumArgs() < 2)
2462 return ExprError(Diag(TheCall->getLocEnd(),
2463 diag::err_typecheck_call_too_few_args_at_least)
2464 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
2465 << TheCall->getSourceRange());
2467 // Determine which of the following types of shufflevector we're checking:
2468 // 1) unary, vector mask: (lhs, mask)
2469 // 2) binary, vector mask: (lhs, rhs, mask)
2470 // 3) binary, scalar mask: (lhs, rhs, index, ..., index)
2471 QualType resType = TheCall->getArg(0)->getType();
2472 unsigned numElements = 0;
2474 if (!TheCall->getArg(0)->isTypeDependent() &&
2475 !TheCall->getArg(1)->isTypeDependent()) {
2476 QualType LHSType = TheCall->getArg(0)->getType();
2477 QualType RHSType = TheCall->getArg(1)->getType();
2479 if (!LHSType->isVectorType() || !RHSType->isVectorType())
2480 return ExprError(Diag(TheCall->getLocStart(),
2481 diag::err_shufflevector_non_vector)
2482 << SourceRange(TheCall->getArg(0)->getLocStart(),
2483 TheCall->getArg(1)->getLocEnd()));
2485 numElements = LHSType->getAs<VectorType>()->getNumElements();
2486 unsigned numResElements = TheCall->getNumArgs() - 2;
2488 // Check to see if we have a call with 2 vector arguments, the unary shuffle
2489 // with mask. If so, verify that RHS is an integer vector type with the
2490 // same number of elts as lhs.
2491 if (TheCall->getNumArgs() == 2) {
2492 if (!RHSType->hasIntegerRepresentation() ||
2493 RHSType->getAs<VectorType>()->getNumElements() != numElements)
2494 return ExprError(Diag(TheCall->getLocStart(),
2495 diag::err_shufflevector_incompatible_vector)
2496 << SourceRange(TheCall->getArg(1)->getLocStart(),
2497 TheCall->getArg(1)->getLocEnd()));
2498 } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
2499 return ExprError(Diag(TheCall->getLocStart(),
2500 diag::err_shufflevector_incompatible_vector)
2501 << SourceRange(TheCall->getArg(0)->getLocStart(),
2502 TheCall->getArg(1)->getLocEnd()));
2503 } else if (numElements != numResElements) {
2504 QualType eltType = LHSType->getAs<VectorType>()->getElementType();
2505 resType = Context.getVectorType(eltType, numResElements,
2506 VectorType::GenericVector);
2510 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
2511 if (TheCall->getArg(i)->isTypeDependent() ||
2512 TheCall->getArg(i)->isValueDependent())
2515 llvm::APSInt Result(32);
2516 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
2517 return ExprError(Diag(TheCall->getLocStart(),
2518 diag::err_shufflevector_nonconstant_argument)
2519 << TheCall->getArg(i)->getSourceRange());
2521 // Allow -1 which will be translated to undef in the IR.
2522 if (Result.isSigned() && Result.isAllOnesValue())
2525 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
2526 return ExprError(Diag(TheCall->getLocStart(),
2527 diag::err_shufflevector_argument_too_large)
2528 << TheCall->getArg(i)->getSourceRange());
2531 SmallVector<Expr*, 32> exprs;
2533 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
2534 exprs.push_back(TheCall->getArg(i));
2535 TheCall->setArg(i, nullptr);
2538 return new (Context) ShuffleVectorExpr(Context, exprs, resType,
2539 TheCall->getCallee()->getLocStart(),
2540 TheCall->getRParenLoc());
2543 /// SemaConvertVectorExpr - Handle __builtin_convertvector
2544 ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo,
2545 SourceLocation BuiltinLoc,
2546 SourceLocation RParenLoc) {
2547 ExprValueKind VK = VK_RValue;
2548 ExprObjectKind OK = OK_Ordinary;
2549 QualType DstTy = TInfo->getType();
2550 QualType SrcTy = E->getType();
2552 if (!SrcTy->isVectorType() && !SrcTy->isDependentType())
2553 return ExprError(Diag(BuiltinLoc,
2554 diag::err_convertvector_non_vector)
2555 << E->getSourceRange());
2556 if (!DstTy->isVectorType() && !DstTy->isDependentType())
2557 return ExprError(Diag(BuiltinLoc,
2558 diag::err_convertvector_non_vector_type));
2560 if (!SrcTy->isDependentType() && !DstTy->isDependentType()) {
2561 unsigned SrcElts = SrcTy->getAs<VectorType>()->getNumElements();
2562 unsigned DstElts = DstTy->getAs<VectorType>()->getNumElements();
2563 if (SrcElts != DstElts)
2564 return ExprError(Diag(BuiltinLoc,
2565 diag::err_convertvector_incompatible_vector)
2566 << E->getSourceRange());
2569 return new (Context)
2570 ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc);
2573 /// SemaBuiltinPrefetch - Handle __builtin_prefetch.
2574 // This is declared to take (const void*, ...) and can take two
2575 // optional constant int args.
2576 bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
2577 unsigned NumArgs = TheCall->getNumArgs();
2580 return Diag(TheCall->getLocEnd(),
2581 diag::err_typecheck_call_too_many_args_at_most)
2582 << 0 /*function call*/ << 3 << NumArgs
2583 << TheCall->getSourceRange();
2585 // Argument 0 is checked for us and the remaining arguments must be
2586 // constant integers.
2587 for (unsigned i = 1; i != NumArgs; ++i)
2588 if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3))
2594 /// SemaBuiltinAssume - Handle __assume (MS Extension).
2595 // __assume does not evaluate its arguments, and should warn if its argument
2596 // has side effects.
2597 bool Sema::SemaBuiltinAssume(CallExpr *TheCall) {
2598 Expr *Arg = TheCall->getArg(0);
2599 if (Arg->isInstantiationDependent()) return false;
2601 if (Arg->HasSideEffects(Context))
2602 Diag(Arg->getLocStart(), diag::warn_assume_side_effects)
2603 << Arg->getSourceRange()
2604 << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier();
2609 /// Handle __builtin_assume_aligned. This is declared
2610 /// as (const void*, size_t, ...) and can take one optional constant int arg.
2611 bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) {
2612 unsigned NumArgs = TheCall->getNumArgs();
2615 return Diag(TheCall->getLocEnd(),
2616 diag::err_typecheck_call_too_many_args_at_most)
2617 << 0 /*function call*/ << 3 << NumArgs
2618 << TheCall->getSourceRange();
2620 // The alignment must be a constant integer.
2621 Expr *Arg = TheCall->getArg(1);
2623 // We can't check the value of a dependent argument.
2624 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
2625 llvm::APSInt Result;
2626 if (SemaBuiltinConstantArg(TheCall, 1, Result))
2629 if (!Result.isPowerOf2())
2630 return Diag(TheCall->getLocStart(),
2631 diag::err_alignment_not_power_of_two)
2632 << Arg->getSourceRange();
2636 ExprResult Arg(TheCall->getArg(2));
2637 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
2638 Context.getSizeType(), false);
2639 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
2640 if (Arg.isInvalid()) return true;
2641 TheCall->setArg(2, Arg.get());
2647 /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
2648 /// TheCall is a constant expression.
2649 bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
2650 llvm::APSInt &Result) {
2651 Expr *Arg = TheCall->getArg(ArgNum);
2652 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
2653 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
2655 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
2657 if (!Arg->isIntegerConstantExpr(Result, Context))
2658 return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
2659 << FDecl->getDeclName() << Arg->getSourceRange();
2664 /// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr
2665 /// TheCall is a constant expression in the range [Low, High].
2666 bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum,
2667 int Low, int High) {
2668 llvm::APSInt Result;
2670 // We can't check the value of a dependent argument.
2671 Expr *Arg = TheCall->getArg(ArgNum);
2672 if (Arg->isTypeDependent() || Arg->isValueDependent())
2675 // Check constant-ness first.
2676 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
2679 if (Result.getSExtValue() < Low || Result.getSExtValue() > High)
2680 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
2681 << Low << High << Arg->getSourceRange();
2686 /// SemaBuiltinARMSpecialReg - Handle a check if argument ArgNum of CallExpr
2687 /// TheCall is an ARM/AArch64 special register string literal.
2688 bool Sema::SemaBuiltinARMSpecialReg(unsigned BuiltinID, CallExpr *TheCall,
2689 int ArgNum, unsigned ExpectedFieldNum,
2691 bool IsARMBuiltin = BuiltinID == ARM::BI__builtin_arm_rsr64 ||
2692 BuiltinID == ARM::BI__builtin_arm_wsr64 ||
2693 BuiltinID == ARM::BI__builtin_arm_rsr ||
2694 BuiltinID == ARM::BI__builtin_arm_rsrp ||
2695 BuiltinID == ARM::BI__builtin_arm_wsr ||
2696 BuiltinID == ARM::BI__builtin_arm_wsrp;
2697 bool IsAArch64Builtin = BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
2698 BuiltinID == AArch64::BI__builtin_arm_wsr64 ||
2699 BuiltinID == AArch64::BI__builtin_arm_rsr ||
2700 BuiltinID == AArch64::BI__builtin_arm_rsrp ||
2701 BuiltinID == AArch64::BI__builtin_arm_wsr ||
2702 BuiltinID == AArch64::BI__builtin_arm_wsrp;
2703 assert((IsARMBuiltin || IsAArch64Builtin) && "Unexpected ARM builtin.");
2705 // We can't check the value of a dependent argument.
2706 Expr *Arg = TheCall->getArg(ArgNum);
2707 if (Arg->isTypeDependent() || Arg->isValueDependent())
2710 // Check if the argument is a string literal.
2711 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
2712 return Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal)
2713 << Arg->getSourceRange();
2715 // Check the type of special register given.
2716 StringRef Reg = cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
2717 SmallVector<StringRef, 6> Fields;
2718 Reg.split(Fields, ":");
2720 if (Fields.size() != ExpectedFieldNum && !(AllowName && Fields.size() == 1))
2721 return Diag(TheCall->getLocStart(), diag::err_arm_invalid_specialreg)
2722 << Arg->getSourceRange();
2724 // If the string is the name of a register then we cannot check that it is
2725 // valid here but if the string is of one the forms described in ACLE then we
2726 // can check that the supplied fields are integers and within the valid
2728 if (Fields.size() > 1) {
2729 bool FiveFields = Fields.size() == 5;
2731 bool ValidString = true;
2733 ValidString &= Fields[0].startswith_lower("cp") ||
2734 Fields[0].startswith_lower("p");
2737 Fields[0].drop_front(Fields[0].startswith_lower("cp") ? 2 : 1);
2739 ValidString &= Fields[2].startswith_lower("c");
2741 Fields[2] = Fields[2].drop_front(1);
2744 ValidString &= Fields[3].startswith_lower("c");
2746 Fields[3] = Fields[3].drop_front(1);
2750 SmallVector<int, 5> Ranges;
2752 Ranges.append({IsAArch64Builtin ? 1 : 15, 7, 7, 15, 15});
2754 Ranges.append({15, 7, 15});
2756 for (unsigned i=0; i<Fields.size(); ++i) {
2758 ValidString &= !Fields[i].getAsInteger(10, IntField);
2759 ValidString &= (IntField >= 0 && IntField <= Ranges[i]);
2763 return Diag(TheCall->getLocStart(), diag::err_arm_invalid_specialreg)
2764 << Arg->getSourceRange();
2766 } else if (IsAArch64Builtin && Fields.size() == 1) {
2767 // If the register name is one of those that appear in the condition below
2768 // and the special register builtin being used is one of the write builtins,
2769 // then we require that the argument provided for writing to the register
2770 // is an integer constant expression. This is because it will be lowered to
2771 // an MSR (immediate) instruction, so we need to know the immediate at
2773 if (TheCall->getNumArgs() != 2)
2776 std::string RegLower = Reg.lower();
2777 if (RegLower != "spsel" && RegLower != "daifset" && RegLower != "daifclr" &&
2778 RegLower != "pan" && RegLower != "uao")
2781 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15);
2787 /// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *).
2788 /// This checks that the target supports __builtin_cpu_supports and
2789 /// that the string argument is constant and valid.
2790 bool Sema::SemaBuiltinCpuSupports(CallExpr *TheCall) {
2791 Expr *Arg = TheCall->getArg(0);
2793 // Check if the argument is a string literal.
2794 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
2795 return Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal)
2796 << Arg->getSourceRange();
2798 // Check the contents of the string.
2800 cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
2801 if (!Context.getTargetInfo().validateCpuSupports(Feature))
2802 return Diag(TheCall->getLocStart(), diag::err_invalid_cpu_supports)
2803 << Arg->getSourceRange();
2807 /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
2808 /// This checks that the target supports __builtin_longjmp and
2809 /// that val is a constant 1.
2810 bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
2811 if (!Context.getTargetInfo().hasSjLjLowering())
2812 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_unsupported)
2813 << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd());
2815 Expr *Arg = TheCall->getArg(1);
2816 llvm::APSInt Result;
2818 // TODO: This is less than ideal. Overload this to take a value.
2819 if (SemaBuiltinConstantArg(TheCall, 1, Result))
2823 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
2824 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
2830 /// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]).
2831 /// This checks that the target supports __builtin_setjmp.
2832 bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) {
2833 if (!Context.getTargetInfo().hasSjLjLowering())
2834 return Diag(TheCall->getLocStart(), diag::err_builtin_setjmp_unsupported)
2835 << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd());
2840 enum StringLiteralCheckType {
2842 SLCT_UncheckedLiteral,
2847 // Determine if an expression is a string literal or constant string.
2848 // If this function returns false on the arguments to a function expecting a
2849 // format string, we will usually need to emit a warning.
2850 // True string literals are then checked by CheckFormatString.
2851 static StringLiteralCheckType
2852 checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args,
2853 bool HasVAListArg, unsigned format_idx,
2854 unsigned firstDataArg, Sema::FormatStringType Type,
2855 Sema::VariadicCallType CallType, bool InFunctionCall,
2856 llvm::SmallBitVector &CheckedVarArgs) {
2858 if (E->isTypeDependent() || E->isValueDependent())
2859 return SLCT_NotALiteral;
2861 E = E->IgnoreParenCasts();
2863 if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
2864 // Technically -Wformat-nonliteral does not warn about this case.
2865 // The behavior of printf and friends in this case is implementation
2866 // dependent. Ideally if the format string cannot be null then
2867 // it should have a 'nonnull' attribute in the function prototype.
2868 return SLCT_UncheckedLiteral;
2870 switch (E->getStmtClass()) {
2871 case Stmt::BinaryConditionalOperatorClass:
2872 case Stmt::ConditionalOperatorClass: {
2873 // The expression is a literal if both sub-expressions were, and it was
2874 // completely checked only if both sub-expressions were checked.
2875 const AbstractConditionalOperator *C =
2876 cast<AbstractConditionalOperator>(E);
2877 StringLiteralCheckType Left =
2878 checkFormatStringExpr(S, C->getTrueExpr(), Args,
2879 HasVAListArg, format_idx, firstDataArg,
2880 Type, CallType, InFunctionCall, CheckedVarArgs);
2881 if (Left == SLCT_NotALiteral)
2882 return SLCT_NotALiteral;
2883 StringLiteralCheckType Right =
2884 checkFormatStringExpr(S, C->getFalseExpr(), Args,
2885 HasVAListArg, format_idx, firstDataArg,
2886 Type, CallType, InFunctionCall, CheckedVarArgs);
2887 return Left < Right ? Left : Right;
2890 case Stmt::ImplicitCastExprClass: {
2891 E = cast<ImplicitCastExpr>(E)->getSubExpr();
2895 case Stmt::OpaqueValueExprClass:
2896 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
2900 return SLCT_NotALiteral;
2902 case Stmt::PredefinedExprClass:
2903 // While __func__, etc., are technically not string literals, they
2904 // cannot contain format specifiers and thus are not a security
2906 return SLCT_UncheckedLiteral;
2908 case Stmt::DeclRefExprClass: {
2909 const DeclRefExpr *DR = cast<DeclRefExpr>(E);
2911 // As an exception, do not flag errors for variables binding to
2912 // const string literals.
2913 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
2914 bool isConstant = false;
2915 QualType T = DR->getType();
2917 if (const ArrayType *AT = S.Context.getAsArrayType(T)) {
2918 isConstant = AT->getElementType().isConstant(S.Context);
2919 } else if (const PointerType *PT = T->getAs<PointerType>()) {
2920 isConstant = T.isConstant(S.Context) &&
2921 PT->getPointeeType().isConstant(S.Context);
2922 } else if (T->isObjCObjectPointerType()) {
2923 // In ObjC, there is usually no "const ObjectPointer" type,
2924 // so don't check if the pointee type is constant.
2925 isConstant = T.isConstant(S.Context);
2929 if (const Expr *Init = VD->getAnyInitializer()) {
2930 // Look through initializers like const char c[] = { "foo" }
2931 if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) {
2932 if (InitList->isStringLiteralInit())
2933 Init = InitList->getInit(0)->IgnoreParenImpCasts();
2935 return checkFormatStringExpr(S, Init, Args,
2936 HasVAListArg, format_idx,
2937 firstDataArg, Type, CallType,
2938 /*InFunctionCall*/false, CheckedVarArgs);
2942 // For vprintf* functions (i.e., HasVAListArg==true), we add a
2943 // special check to see if the format string is a function parameter
2944 // of the function calling the printf function. If the function
2945 // has an attribute indicating it is a printf-like function, then we
2946 // should suppress warnings concerning non-literals being used in a call
2947 // to a vprintf function. For example:
2950 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
2952 // va_start(ap, fmt);
2953 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt".
2957 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) {
2958 if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) {
2959 int PVIndex = PV->getFunctionScopeIndex() + 1;
2960 for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) {
2961 // adjust for implicit parameter
2962 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
2963 if (MD->isInstance())
2965 // We also check if the formats are compatible.
2966 // We can't pass a 'scanf' string to a 'printf' function.
2967 if (PVIndex == PVFormat->getFormatIdx() &&
2968 Type == S.GetFormatStringType(PVFormat))
2969 return SLCT_UncheckedLiteral;
2976 return SLCT_NotALiteral;
2979 case Stmt::CallExprClass:
2980 case Stmt::CXXMemberCallExprClass: {
2981 const CallExpr *CE = cast<CallExpr>(E);
2982 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
2983 if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
2984 unsigned ArgIndex = FA->getFormatIdx();
2985 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
2986 if (MD->isInstance())
2988 const Expr *Arg = CE->getArg(ArgIndex - 1);
2990 return checkFormatStringExpr(S, Arg, Args,
2991 HasVAListArg, format_idx, firstDataArg,
2992 Type, CallType, InFunctionCall,
2994 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2995 unsigned BuiltinID = FD->getBuiltinID();
2996 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
2997 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) {
2998 const Expr *Arg = CE->getArg(0);
2999 return checkFormatStringExpr(S, Arg, Args,
3000 HasVAListArg, format_idx,
3001 firstDataArg, Type, CallType,
3002 InFunctionCall, CheckedVarArgs);
3007 return SLCT_NotALiteral;
3009 case Stmt::ObjCStringLiteralClass:
3010 case Stmt::StringLiteralClass: {
3011 const StringLiteral *StrE = nullptr;
3013 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
3014 StrE = ObjCFExpr->getString();
3016 StrE = cast<StringLiteral>(E);
3019 S.CheckFormatString(StrE, E, Args, HasVAListArg, format_idx, firstDataArg,
3020 Type, InFunctionCall, CallType, CheckedVarArgs);
3021 return SLCT_CheckedLiteral;
3024 return SLCT_NotALiteral;
3028 return SLCT_NotALiteral;
3032 Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
3033 return llvm::StringSwitch<FormatStringType>(Format->getType()->getName())
3034 .Case("scanf", FST_Scanf)
3035 .Cases("printf", "printf0", FST_Printf)
3036 .Cases("NSString", "CFString", FST_NSString)
3037 .Case("strftime", FST_Strftime)
3038 .Case("strfmon", FST_Strfmon)
3039 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
3040 .Case("freebsd_kprintf", FST_FreeBSDKPrintf)
3041 .Case("os_trace", FST_OSTrace)
3042 .Default(FST_Unknown);
3045 /// CheckFormatArguments - Check calls to printf and scanf (and similar
3046 /// functions) for correct use of format strings.
3047 /// Returns true if a format string has been fully checked.
3048 bool Sema::CheckFormatArguments(const FormatAttr *Format,
3049 ArrayRef<const Expr *> Args,
3051 VariadicCallType CallType,
3052 SourceLocation Loc, SourceRange Range,
3053 llvm::SmallBitVector &CheckedVarArgs) {
3054 FormatStringInfo FSI;
3055 if (getFormatStringInfo(Format, IsCXXMember, &FSI))
3056 return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx,
3057 FSI.FirstDataArg, GetFormatStringType(Format),
3058 CallType, Loc, Range, CheckedVarArgs);
3062 bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
3063 bool HasVAListArg, unsigned format_idx,
3064 unsigned firstDataArg, FormatStringType Type,
3065 VariadicCallType CallType,
3066 SourceLocation Loc, SourceRange Range,
3067 llvm::SmallBitVector &CheckedVarArgs) {
3068 // CHECK: printf/scanf-like function is called with no format string.
3069 if (format_idx >= Args.size()) {
3070 Diag(Loc, diag::warn_missing_format_string) << Range;
3074 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
3076 // CHECK: format string is not a string literal.
3078 // Dynamically generated format strings are difficult to
3079 // automatically vet at compile time. Requiring that format strings
3080 // are string literals: (1) permits the checking of format strings by
3081 // the compiler and thereby (2) can practically remove the source of
3082 // many format string exploits.
3084 // Format string can be either ObjC string (e.g. @"%d") or
3085 // C string (e.g. "%d")
3086 // ObjC string uses the same format specifiers as C string, so we can use
3087 // the same format string checking logic for both ObjC and C strings.
3088 StringLiteralCheckType CT =
3089 checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg,
3090 format_idx, firstDataArg, Type, CallType,
3091 /*IsFunctionCall*/true, CheckedVarArgs);
3092 if (CT != SLCT_NotALiteral)
3093 // Literal format string found, check done!
3094 return CT == SLCT_CheckedLiteral;
3096 // Strftime is particular as it always uses a single 'time' argument,
3097 // so it is safe to pass a non-literal string.
3098 if (Type == FST_Strftime)
3101 // Do not emit diag when the string param is a macro expansion and the
3102 // format is either NSString or CFString. This is a hack to prevent
3103 // diag when using the NSLocalizedString and CFCopyLocalizedString macros
3104 // which are usually used in place of NS and CF string literals.
3105 if (Type == FST_NSString &&
3106 SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart()))
3109 // If there are no arguments specified, warn with -Wformat-security, otherwise
3110 // warn only with -Wformat-nonliteral.
3111 if (Args.size() == firstDataArg)
3112 Diag(Args[format_idx]->getLocStart(),
3113 diag::warn_format_nonliteral_noargs)
3114 << OrigFormatExpr->getSourceRange();
3116 Diag(Args[format_idx]->getLocStart(),
3117 diag::warn_format_nonliteral)
3118 << OrigFormatExpr->getSourceRange();
3123 class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
3126 const StringLiteral *FExpr;
3127 const Expr *OrigFormatExpr;
3128 const unsigned FirstDataArg;
3129 const unsigned NumDataArgs;
3130 const char *Beg; // Start of format string.
3131 const bool HasVAListArg;
3132 ArrayRef<const Expr *> Args;
3134 llvm::SmallBitVector CoveredArgs;
3135 bool usesPositionalArgs;
3137 bool inFunctionCall;
3138 Sema::VariadicCallType CallType;
3139 llvm::SmallBitVector &CheckedVarArgs;
3141 CheckFormatHandler(Sema &s, const StringLiteral *fexpr,
3142 const Expr *origFormatExpr, unsigned firstDataArg,
3143 unsigned numDataArgs, const char *beg, bool hasVAListArg,
3144 ArrayRef<const Expr *> Args,
3145 unsigned formatIdx, bool inFunctionCall,
3146 Sema::VariadicCallType callType,
3147 llvm::SmallBitVector &CheckedVarArgs)
3148 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr),
3149 FirstDataArg(firstDataArg), NumDataArgs(numDataArgs),
3150 Beg(beg), HasVAListArg(hasVAListArg),
3151 Args(Args), FormatIdx(formatIdx),
3152 usesPositionalArgs(false), atFirstArg(true),
3153 inFunctionCall(inFunctionCall), CallType(callType),
3154 CheckedVarArgs(CheckedVarArgs) {
3155 CoveredArgs.resize(numDataArgs);
3156 CoveredArgs.reset();
3159 void DoneProcessing();
3161 void HandleIncompleteSpecifier(const char *startSpecifier,
3162 unsigned specifierLen) override;
3164 void HandleInvalidLengthModifier(
3165 const analyze_format_string::FormatSpecifier &FS,
3166 const analyze_format_string::ConversionSpecifier &CS,
3167 const char *startSpecifier, unsigned specifierLen,
3170 void HandleNonStandardLengthModifier(
3171 const analyze_format_string::FormatSpecifier &FS,
3172 const char *startSpecifier, unsigned specifierLen);
3174 void HandleNonStandardConversionSpecifier(
3175 const analyze_format_string::ConversionSpecifier &CS,
3176 const char *startSpecifier, unsigned specifierLen);
3178 void HandlePosition(const char *startPos, unsigned posLen) override;
3180 void HandleInvalidPosition(const char *startSpecifier,
3181 unsigned specifierLen,
3182 analyze_format_string::PositionContext p) override;
3184 void HandleZeroPosition(const char *startPos, unsigned posLen) override;
3186 void HandleNullChar(const char *nullCharacter) override;
3188 template <typename Range>
3189 static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall,
3190 const Expr *ArgumentExpr,
3191 PartialDiagnostic PDiag,
3192 SourceLocation StringLoc,
3193 bool IsStringLocation, Range StringRange,
3194 ArrayRef<FixItHint> Fixit = None);
3197 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
3198 const char *startSpec,
3199 unsigned specifierLen,
3200 const char *csStart, unsigned csLen);
3202 void HandlePositionalNonpositionalArgs(SourceLocation Loc,
3203 const char *startSpec,
3204 unsigned specifierLen);
3206 SourceRange getFormatStringRange();
3207 CharSourceRange getSpecifierRange(const char *startSpecifier,
3208 unsigned specifierLen);
3209 SourceLocation getLocationOfByte(const char *x);
3211 const Expr *getDataArg(unsigned i) const;
3213 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
3214 const analyze_format_string::ConversionSpecifier &CS,
3215 const char *startSpecifier, unsigned specifierLen,
3218 template <typename Range>
3219 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
3220 bool IsStringLocation, Range StringRange,
3221 ArrayRef<FixItHint> Fixit = None);
3225 SourceRange CheckFormatHandler::getFormatStringRange() {
3226 return OrigFormatExpr->getSourceRange();
3229 CharSourceRange CheckFormatHandler::
3230 getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
3231 SourceLocation Start = getLocationOfByte(startSpecifier);
3232 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1);
3234 // Advance the end SourceLocation by one due to half-open ranges.
3235 End = End.getLocWithOffset(1);
3237 return CharSourceRange::getCharRange(Start, End);
3240 SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
3241 return S.getLocationOfStringLiteralByte(FExpr, x - Beg);
3244 void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
3245 unsigned specifierLen){
3246 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
3247 getLocationOfByte(startSpecifier),
3248 /*IsStringLocation*/true,
3249 getSpecifierRange(startSpecifier, specifierLen));
3252 void CheckFormatHandler::HandleInvalidLengthModifier(
3253 const analyze_format_string::FormatSpecifier &FS,
3254 const analyze_format_string::ConversionSpecifier &CS,
3255 const char *startSpecifier, unsigned specifierLen, unsigned DiagID) {
3256 using namespace analyze_format_string;
3258 const LengthModifier &LM = FS.getLengthModifier();
3259 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
3261 // See if we know how to fix this length modifier.
3262 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
3264 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
3265 getLocationOfByte(LM.getStart()),
3266 /*IsStringLocation*/true,
3267 getSpecifierRange(startSpecifier, specifierLen));
3269 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
3270 << FixedLM->toString()
3271 << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
3275 if (DiagID == diag::warn_format_nonsensical_length)
3276 Hint = FixItHint::CreateRemoval(LMRange);
3278 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
3279 getLocationOfByte(LM.getStart()),
3280 /*IsStringLocation*/true,
3281 getSpecifierRange(startSpecifier, specifierLen),
3286 void CheckFormatHandler::HandleNonStandardLengthModifier(
3287 const analyze_format_string::FormatSpecifier &FS,
3288 const char *startSpecifier, unsigned specifierLen) {
3289 using namespace analyze_format_string;
3291 const LengthModifier &LM = FS.getLengthModifier();
3292 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
3294 // See if we know how to fix this length modifier.
3295 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
3297 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
3298 << LM.toString() << 0,
3299 getLocationOfByte(LM.getStart()),
3300 /*IsStringLocation*/true,
3301 getSpecifierRange(startSpecifier, specifierLen));
3303 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
3304 << FixedLM->toString()
3305 << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
3308 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
3309 << LM.toString() << 0,
3310 getLocationOfByte(LM.getStart()),
3311 /*IsStringLocation*/true,
3312 getSpecifierRange(startSpecifier, specifierLen));
3316 void CheckFormatHandler::HandleNonStandardConversionSpecifier(
3317 const analyze_format_string::ConversionSpecifier &CS,
3318 const char *startSpecifier, unsigned specifierLen) {
3319 using namespace analyze_format_string;
3321 // See if we know how to fix this conversion specifier.
3322 Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier();
3324 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
3325 << CS.toString() << /*conversion specifier*/1,
3326 getLocationOfByte(CS.getStart()),
3327 /*IsStringLocation*/true,
3328 getSpecifierRange(startSpecifier, specifierLen));
3330 CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength());
3331 S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier)
3332 << FixedCS->toString()
3333 << FixItHint::CreateReplacement(CSRange, FixedCS->toString());
3335 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
3336 << CS.toString() << /*conversion specifier*/1,
3337 getLocationOfByte(CS.getStart()),
3338 /*IsStringLocation*/true,
3339 getSpecifierRange(startSpecifier, specifierLen));
3343 void CheckFormatHandler::HandlePosition(const char *startPos,
3345 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
3346 getLocationOfByte(startPos),
3347 /*IsStringLocation*/true,
3348 getSpecifierRange(startPos, posLen));
3352 CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
3353 analyze_format_string::PositionContext p) {
3354 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier)
3356 getLocationOfByte(startPos), /*IsStringLocation*/true,
3357 getSpecifierRange(startPos, posLen));
3360 void CheckFormatHandler::HandleZeroPosition(const char *startPos,
3362 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
3363 getLocationOfByte(startPos),
3364 /*IsStringLocation*/true,
3365 getSpecifierRange(startPos, posLen));
3368 void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
3369 if (!isa<ObjCStringLiteral>(OrigFormatExpr)) {
3370 // The presence of a null character is likely an error.
3371 EmitFormatDiagnostic(
3372 S.PDiag(diag::warn_printf_format_string_contains_null_char),
3373 getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
3374 getFormatStringRange());
3378 // Note that this may return NULL if there was an error parsing or building
3379 // one of the argument expressions.
3380 const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
3381 return Args[FirstDataArg + i];
3384 void CheckFormatHandler::DoneProcessing() {
3385 // Does the number of data arguments exceed the number of
3386 // format conversions in the format string?
3387 if (!HasVAListArg) {
3388 // Find any arguments that weren't covered.
3390 signed notCoveredArg = CoveredArgs.find_first();
3391 if (notCoveredArg >= 0) {
3392 assert((unsigned)notCoveredArg < NumDataArgs);
3393 if (const Expr *E = getDataArg((unsigned) notCoveredArg)) {
3394 SourceLocation Loc = E->getLocStart();
3395 if (!S.getSourceManager().isInSystemMacro(Loc)) {
3396 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used),
3397 Loc, /*IsStringLocation*/false,
3398 getFormatStringRange());
3406 CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
3408 const char *startSpec,
3409 unsigned specifierLen,
3410 const char *csStart,
3413 bool keepGoing = true;
3414 if (argIndex < NumDataArgs) {
3415 // Consider the argument coverered, even though the specifier doesn't
3417 CoveredArgs.set(argIndex);
3420 // If argIndex exceeds the number of data arguments we
3421 // don't issue a warning because that is just a cascade of warnings (and
3422 // they may have intended '%%' anyway). We don't want to continue processing
3423 // the format string after this point, however, as we will like just get
3424 // gibberish when trying to match arguments.
3428 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion)
3429 << StringRef(csStart, csLen),
3430 Loc, /*IsStringLocation*/true,
3431 getSpecifierRange(startSpec, specifierLen));
3437 CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
3438 const char *startSpec,
3439 unsigned specifierLen) {
3440 EmitFormatDiagnostic(
3441 S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
3442 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
3446 CheckFormatHandler::CheckNumArgs(
3447 const analyze_format_string::FormatSpecifier &FS,
3448 const analyze_format_string::ConversionSpecifier &CS,
3449 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
3451 if (argIndex >= NumDataArgs) {
3452 PartialDiagnostic PDiag = FS.usesPositionalArg()
3453 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
3454 << (argIndex+1) << NumDataArgs)
3455 : S.PDiag(diag::warn_printf_insufficient_data_args);
3456 EmitFormatDiagnostic(
3457 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
3458 getSpecifierRange(startSpecifier, specifierLen));
3464 template<typename Range>
3465 void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
3467 bool IsStringLocation,
3469 ArrayRef<FixItHint> FixIt) {
3470 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
3471 Loc, IsStringLocation, StringRange, FixIt);
3474 /// \brief If the format string is not within the funcion call, emit a note
3475 /// so that the function call and string are in diagnostic messages.
3477 /// \param InFunctionCall if true, the format string is within the function
3478 /// call and only one diagnostic message will be produced. Otherwise, an
3479 /// extra note will be emitted pointing to location of the format string.
3481 /// \param ArgumentExpr the expression that is passed as the format string
3482 /// argument in the function call. Used for getting locations when two
3483 /// diagnostics are emitted.
3485 /// \param PDiag the callee should already have provided any strings for the
3486 /// diagnostic message. This function only adds locations and fixits
3489 /// \param Loc primary location for diagnostic. If two diagnostics are
3490 /// required, one will be at Loc and a new SourceLocation will be created for
3493 /// \param IsStringLocation if true, Loc points to the format string should be
3494 /// used for the note. Otherwise, Loc points to the argument list and will
3495 /// be used with PDiag.
3497 /// \param StringRange some or all of the string to highlight. This is
3498 /// templated so it can accept either a CharSourceRange or a SourceRange.
3500 /// \param FixIt optional fix it hint for the format string.
3501 template<typename Range>
3502 void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall,
3503 const Expr *ArgumentExpr,
3504 PartialDiagnostic PDiag,
3506 bool IsStringLocation,
3508 ArrayRef<FixItHint> FixIt) {
3509 if (InFunctionCall) {
3510 const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag);
3514 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
3515 << ArgumentExpr->getSourceRange();
3517 const Sema::SemaDiagnosticBuilder &Note =
3518 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
3519 diag::note_format_string_defined);
3521 Note << StringRange;
3526 //===--- CHECK: Printf format string checking ------------------------------===//
3529 class CheckPrintfHandler : public CheckFormatHandler {
3532 CheckPrintfHandler(Sema &s, const StringLiteral *fexpr,
3533 const Expr *origFormatExpr, unsigned firstDataArg,
3534 unsigned numDataArgs, bool isObjC,
3535 const char *beg, bool hasVAListArg,
3536 ArrayRef<const Expr *> Args,
3537 unsigned formatIdx, bool inFunctionCall,
3538 Sema::VariadicCallType CallType,
3539 llvm::SmallBitVector &CheckedVarArgs)
3540 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
3541 numDataArgs, beg, hasVAListArg, Args,
3542 formatIdx, inFunctionCall, CallType, CheckedVarArgs),
3547 bool HandleInvalidPrintfConversionSpecifier(
3548 const analyze_printf::PrintfSpecifier &FS,
3549 const char *startSpecifier,
3550 unsigned specifierLen) override;
3552 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
3553 const char *startSpecifier,
3554 unsigned specifierLen) override;
3555 bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
3556 const char *StartSpecifier,
3557 unsigned SpecifierLen,
3560 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
3561 const char *startSpecifier, unsigned specifierLen);
3562 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
3563 const analyze_printf::OptionalAmount &Amt,
3565 const char *startSpecifier, unsigned specifierLen);
3566 void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
3567 const analyze_printf::OptionalFlag &flag,
3568 const char *startSpecifier, unsigned specifierLen);
3569 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
3570 const analyze_printf::OptionalFlag &ignoredFlag,
3571 const analyze_printf::OptionalFlag &flag,
3572 const char *startSpecifier, unsigned specifierLen);
3573 bool checkForCStrMembers(const analyze_printf::ArgType &AT,
3576 void HandleEmptyObjCModifierFlag(const char *startFlag,
3577 unsigned flagLen) override;
3579 void HandleInvalidObjCModifierFlag(const char *startFlag,
3580 unsigned flagLen) override;
3582 void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart,
3583 const char *flagsEnd,
3584 const char *conversionPosition)
3589 bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
3590 const analyze_printf::PrintfSpecifier &FS,
3591 const char *startSpecifier,
3592 unsigned specifierLen) {
3593 const analyze_printf::PrintfConversionSpecifier &CS =
3594 FS.getConversionSpecifier();
3596 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
3597 getLocationOfByte(CS.getStart()),
3598 startSpecifier, specifierLen,
3599 CS.getStart(), CS.getLength());
3602 bool CheckPrintfHandler::HandleAmount(
3603 const analyze_format_string::OptionalAmount &Amt,
3604 unsigned k, const char *startSpecifier,
3605 unsigned specifierLen) {
3607 if (Amt.hasDataArgument()) {
3608 if (!HasVAListArg) {
3609 unsigned argIndex = Amt.getArgIndex();
3610 if (argIndex >= NumDataArgs) {
3611 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
3613 getLocationOfByte(Amt.getStart()),
3614 /*IsStringLocation*/true,
3615 getSpecifierRange(startSpecifier, specifierLen));
3616 // Don't do any more checking. We will just emit
3621 // Type check the data argument. It should be an 'int'.
3622 // Although not in conformance with C99, we also allow the argument to be
3623 // an 'unsigned int' as that is a reasonably safe case. GCC also
3624 // doesn't emit a warning for that case.
3625 CoveredArgs.set(argIndex);
3626 const Expr *Arg = getDataArg(argIndex);
3630 QualType T = Arg->getType();
3632 const analyze_printf::ArgType &AT = Amt.getArgType(S.Context);
3633 assert(AT.isValid());
3635 if (!AT.matchesType(S.Context, T)) {
3636 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
3637 << k << AT.getRepresentativeTypeName(S.Context)
3638 << T << Arg->getSourceRange(),
3639 getLocationOfByte(Amt.getStart()),
3640 /*IsStringLocation*/true,
3641 getSpecifierRange(startSpecifier, specifierLen));
3642 // Don't do any more checking. We will just emit
3651 void CheckPrintfHandler::HandleInvalidAmount(
3652 const analyze_printf::PrintfSpecifier &FS,
3653 const analyze_printf::OptionalAmount &Amt,
3655 const char *startSpecifier,
3656 unsigned specifierLen) {
3657 const analyze_printf::PrintfConversionSpecifier &CS =
3658 FS.getConversionSpecifier();
3661 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
3662 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
3663 Amt.getConstantLength()))
3666 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
3667 << type << CS.toString(),
3668 getLocationOfByte(Amt.getStart()),
3669 /*IsStringLocation*/true,
3670 getSpecifierRange(startSpecifier, specifierLen),
3674 void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
3675 const analyze_printf::OptionalFlag &flag,
3676 const char *startSpecifier,
3677 unsigned specifierLen) {
3678 // Warn about pointless flag with a fixit removal.
3679 const analyze_printf::PrintfConversionSpecifier &CS =
3680 FS.getConversionSpecifier();
3681 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
3682 << flag.toString() << CS.toString(),
3683 getLocationOfByte(flag.getPosition()),
3684 /*IsStringLocation*/true,
3685 getSpecifierRange(startSpecifier, specifierLen),
3686 FixItHint::CreateRemoval(
3687 getSpecifierRange(flag.getPosition(), 1)));
3690 void CheckPrintfHandler::HandleIgnoredFlag(
3691 const analyze_printf::PrintfSpecifier &FS,
3692 const analyze_printf::OptionalFlag &ignoredFlag,
3693 const analyze_printf::OptionalFlag &flag,
3694 const char *startSpecifier,
3695 unsigned specifierLen) {
3696 // Warn about ignored flag with a fixit removal.
3697 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
3698 << ignoredFlag.toString() << flag.toString(),
3699 getLocationOfByte(ignoredFlag.getPosition()),
3700 /*IsStringLocation*/true,
3701 getSpecifierRange(startSpecifier, specifierLen),
3702 FixItHint::CreateRemoval(
3703 getSpecifierRange(ignoredFlag.getPosition(), 1)));
3706 // void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
3707 // bool IsStringLocation, Range StringRange,
3708 // ArrayRef<FixItHint> Fixit = None);
3710 void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag,
3712 // Warn about an empty flag.
3713 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag),
3714 getLocationOfByte(startFlag),
3715 /*IsStringLocation*/true,
3716 getSpecifierRange(startFlag, flagLen));
3719 void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag,
3721 // Warn about an invalid flag.
3722 auto Range = getSpecifierRange(startFlag, flagLen);
3723 StringRef flag(startFlag, flagLen);
3724 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag,
3725 getLocationOfByte(startFlag),
3726 /*IsStringLocation*/true,
3727 Range, FixItHint::CreateRemoval(Range));
3730 void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion(
3731 const char *flagsStart, const char *flagsEnd, const char *conversionPosition) {
3732 // Warn about using '[...]' without a '@' conversion.
3733 auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1);
3734 auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion;
3735 EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1),
3736 getLocationOfByte(conversionPosition),
3737 /*IsStringLocation*/true,
3738 Range, FixItHint::CreateRemoval(Range));
3741 // Determines if the specified is a C++ class or struct containing
3742 // a member with the specified name and kind (e.g. a CXXMethodDecl named
3744 template<typename MemberKind>
3745 static llvm::SmallPtrSet<MemberKind*, 1>
3746 CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
3747 const RecordType *RT = Ty->getAs<RecordType>();
3748 llvm::SmallPtrSet<MemberKind*, 1> Results;
3752 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
3753 if (!RD || !RD->getDefinition())
3756 LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(),
3757 Sema::LookupMemberName);
3758 R.suppressDiagnostics();
3760 // We just need to include all members of the right kind turned up by the
3761 // filter, at this point.
3762 if (S.LookupQualifiedName(R, RT->getDecl()))
3763 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
3764 NamedDecl *decl = (*I)->getUnderlyingDecl();
3765 if (MemberKind *FK = dyn_cast<MemberKind>(decl))
3771 /// Check if we could call '.c_str()' on an object.
3773 /// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't
3774 /// allow the call, or if it would be ambiguous).
3775 bool Sema::hasCStrMethod(const Expr *E) {
3776 typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet;
3778 CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType());
3779 for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
3781 if ((*MI)->getMinRequiredArguments() == 0)
3786 // Check if a (w)string was passed when a (w)char* was needed, and offer a
3787 // better diagnostic if so. AT is assumed to be valid.
3788 // Returns true when a c_str() conversion method is found.
3789 bool CheckPrintfHandler::checkForCStrMembers(
3790 const analyze_printf::ArgType &AT, const Expr *E) {
3791 typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet;
3794 CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType());
3796 for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
3798 const CXXMethodDecl *Method = *MI;
3799 if (Method->getMinRequiredArguments() == 0 &&
3800 AT.matchesType(S.Context, Method->getReturnType())) {
3801 // FIXME: Suggest parens if the expression needs them.
3802 SourceLocation EndLoc = S.getLocForEndOfToken(E->getLocEnd());
3803 S.Diag(E->getLocStart(), diag::note_printf_c_str)
3805 << FixItHint::CreateInsertion(EndLoc, ".c_str()");
3814 CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
3816 const char *startSpecifier,
3817 unsigned specifierLen) {
3819 using namespace analyze_format_string;
3820 using namespace analyze_printf;
3821 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
3823 if (FS.consumesDataArgument()) {
3826 usesPositionalArgs = FS.usesPositionalArg();
3828 else if (usesPositionalArgs != FS.usesPositionalArg()) {
3829 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
3830 startSpecifier, specifierLen);
3835 // First check if the field width, precision, and conversion specifier
3836 // have matching data arguments.
3837 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
3838 startSpecifier, specifierLen)) {
3842 if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
3843 startSpecifier, specifierLen)) {
3847 if (!CS.consumesDataArgument()) {
3848 // FIXME: Technically specifying a precision or field width here
3849 // makes no sense. Worth issuing a warning at some point.
3853 // Consume the argument.
3854 unsigned argIndex = FS.getArgIndex();
3855 if (argIndex < NumDataArgs) {
3856 // The check to see if the argIndex is valid will come later.
3857 // We set the bit here because we may exit early from this
3858 // function if we encounter some other error.
3859 CoveredArgs.set(argIndex);
3862 // FreeBSD kernel extensions.
3863 if (CS.getKind() == ConversionSpecifier::FreeBSDbArg ||
3864 CS.getKind() == ConversionSpecifier::FreeBSDDArg) {
3865 // We need at least two arguments.
3866 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1))
3869 // Claim the second argument.
3870 CoveredArgs.set(argIndex + 1);
3872 // Type check the first argument (int for %b, pointer for %D)
3873 const Expr *Ex = getDataArg(argIndex);
3874 const analyze_printf::ArgType &AT =
3875 (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ?
3876 ArgType(S.Context.IntTy) : ArgType::CPointerTy;
3877 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType()))
3878 EmitFormatDiagnostic(
3879 S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
3880 << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
3881 << false << Ex->getSourceRange(),
3882 Ex->getLocStart(), /*IsStringLocation*/false,
3883 getSpecifierRange(startSpecifier, specifierLen));
3885 // Type check the second argument (char * for both %b and %D)
3886 Ex = getDataArg(argIndex + 1);
3887 const analyze_printf::ArgType &AT2 = ArgType::CStrTy;
3888 if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType()))
3889 EmitFormatDiagnostic(
3890 S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
3891 << AT2.getRepresentativeTypeName(S.Context) << Ex->getType()
3892 << false << Ex->getSourceRange(),
3893 Ex->getLocStart(), /*IsStringLocation*/false,
3894 getSpecifierRange(startSpecifier, specifierLen));
3899 // Check for using an Objective-C specific conversion specifier
3900 // in a non-ObjC literal.
3901 if (!ObjCContext && CS.isObjCArg()) {
3902 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
3906 // Check for invalid use of field width
3907 if (!FS.hasValidFieldWidth()) {
3908 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
3909 startSpecifier, specifierLen);
3912 // Check for invalid use of precision
3913 if (!FS.hasValidPrecision()) {
3914 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
3915 startSpecifier, specifierLen);
3918 // Check each flag does not conflict with any other component.
3919 if (!FS.hasValidThousandsGroupingPrefix())
3920 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
3921 if (!FS.hasValidLeadingZeros())
3922 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
3923 if (!FS.hasValidPlusPrefix())
3924 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
3925 if (!FS.hasValidSpacePrefix())
3926 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
3927 if (!FS.hasValidAlternativeForm())
3928 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
3929 if (!FS.hasValidLeftJustified())
3930 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
3932 // Check that flags are not ignored by another flag
3933 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
3934 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
3935 startSpecifier, specifierLen);
3936 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
3937 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
3938 startSpecifier, specifierLen);
3940 // Check the length modifier is valid with the given conversion specifier.
3941 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
3942 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
3943 diag::warn_format_nonsensical_length);
3944 else if (!FS.hasStandardLengthModifier())
3945 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
3946 else if (!FS.hasStandardLengthConversionCombination())
3947 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
3948 diag::warn_format_non_standard_conversion_spec);
3950 if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
3951 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
3953 // The remaining checks depend on the data arguments.
3957 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
3960 const Expr *Arg = getDataArg(argIndex);
3964 return checkFormatExpr(FS, startSpecifier, specifierLen, Arg);
3967 static bool requiresParensToAddCast(const Expr *E) {
3968 // FIXME: We should have a general way to reason about operator
3969 // precedence and whether parens are actually needed here.
3970 // Take care of a few common cases where they aren't.
3971 const Expr *Inside = E->IgnoreImpCasts();
3972 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside))
3973 Inside = POE->getSyntacticForm()->IgnoreImpCasts();
3975 switch (Inside->getStmtClass()) {
3976 case Stmt::ArraySubscriptExprClass:
3977 case Stmt::CallExprClass:
3978 case Stmt::CharacterLiteralClass:
3979 case Stmt::CXXBoolLiteralExprClass:
3980 case Stmt::DeclRefExprClass:
3981 case Stmt::FloatingLiteralClass:
3982 case Stmt::IntegerLiteralClass:
3983 case Stmt::MemberExprClass:
3984 case Stmt::ObjCArrayLiteralClass:
3985 case Stmt::ObjCBoolLiteralExprClass:
3986 case Stmt::ObjCBoxedExprClass:
3987 case Stmt::ObjCDictionaryLiteralClass:
3988 case Stmt::ObjCEncodeExprClass:
3989 case Stmt::ObjCIvarRefExprClass:
3990 case Stmt::ObjCMessageExprClass:
3991 case Stmt::ObjCPropertyRefExprClass:
3992 case Stmt::ObjCStringLiteralClass:
3993 case Stmt::ObjCSubscriptRefExprClass:
3994 case Stmt::ParenExprClass:
3995 case Stmt::StringLiteralClass:
3996 case Stmt::UnaryOperatorClass:
4003 static std::pair<QualType, StringRef>
4004 shouldNotPrintDirectly(const ASTContext &Context,
4005 QualType IntendedTy,
4007 // Use a 'while' to peel off layers of typedefs.
4008 QualType TyTy = IntendedTy;
4009 while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) {
4010 StringRef Name = UserTy->getDecl()->getName();
4011 QualType CastTy = llvm::StringSwitch<QualType>(Name)
4012 .Case("NSInteger", Context.LongTy)
4013 .Case("NSUInteger", Context.UnsignedLongTy)
4014 .Case("SInt32", Context.IntTy)
4015 .Case("UInt32", Context.UnsignedIntTy)
4016 .Default(QualType());
4018 if (!CastTy.isNull())
4019 return std::make_pair(CastTy, Name);
4021 TyTy = UserTy->desugar();
4024 // Strip parens if necessary.
4025 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
4026 return shouldNotPrintDirectly(Context,
4027 PE->getSubExpr()->getType(),
4030 // If this is a conditional expression, then its result type is constructed
4031 // via usual arithmetic conversions and thus there might be no necessary
4032 // typedef sugar there. Recurse to operands to check for NSInteger &
4033 // Co. usage condition.
4034 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
4035 QualType TrueTy, FalseTy;
4036 StringRef TrueName, FalseName;
4038 std::tie(TrueTy, TrueName) =
4039 shouldNotPrintDirectly(Context,
4040 CO->getTrueExpr()->getType(),
4042 std::tie(FalseTy, FalseName) =
4043 shouldNotPrintDirectly(Context,
4044 CO->getFalseExpr()->getType(),
4045 CO->getFalseExpr());
4047 if (TrueTy == FalseTy)
4048 return std::make_pair(TrueTy, TrueName);
4049 else if (TrueTy.isNull())
4050 return std::make_pair(FalseTy, FalseName);
4051 else if (FalseTy.isNull())
4052 return std::make_pair(TrueTy, TrueName);
4055 return std::make_pair(QualType(), StringRef());
4059 CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
4060 const char *StartSpecifier,
4061 unsigned SpecifierLen,
4063 using namespace analyze_format_string;
4064 using namespace analyze_printf;
4065 // Now type check the data expression that matches the
4066 // format specifier.
4067 const analyze_printf::ArgType &AT = FS.getArgType(S.Context,
4072 QualType ExprTy = E->getType();
4073 while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) {
4074 ExprTy = TET->getUnderlyingExpr()->getType();
4077 analyze_printf::ArgType::MatchKind match = AT.matchesType(S.Context, ExprTy);
4079 if (match == analyze_printf::ArgType::Match) {
4083 // Look through argument promotions for our error message's reported type.
4084 // This includes the integral and floating promotions, but excludes array
4085 // and function pointer decay; seeing that an argument intended to be a
4086 // string has type 'char [6]' is probably more confusing than 'char *'.
4087 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
4088 if (ICE->getCastKind() == CK_IntegralCast ||
4089 ICE->getCastKind() == CK_FloatingCast) {
4090 E = ICE->getSubExpr();
4091 ExprTy = E->getType();
4093 // Check if we didn't match because of an implicit cast from a 'char'
4094 // or 'short' to an 'int'. This is done because printf is a varargs
4096 if (ICE->getType() == S.Context.IntTy ||
4097 ICE->getType() == S.Context.UnsignedIntTy) {
4098 // All further checking is done on the subexpression.
4099 if (AT.matchesType(S.Context, ExprTy))
4103 } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) {
4104 // Special case for 'a', which has type 'int' in C.
4105 // Note, however, that we do /not/ want to treat multibyte constants like
4106 // 'MooV' as characters! This form is deprecated but still exists.
4107 if (ExprTy == S.Context.IntTy)
4108 if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue()))
4109 ExprTy = S.Context.CharTy;
4112 // Look through enums to their underlying type.
4113 bool IsEnum = false;
4114 if (auto EnumTy = ExprTy->getAs<EnumType>()) {
4115 ExprTy = EnumTy->getDecl()->getIntegerType();
4119 // %C in an Objective-C context prints a unichar, not a wchar_t.
4120 // If the argument is an integer of some kind, believe the %C and suggest
4121 // a cast instead of changing the conversion specifier.
4122 QualType IntendedTy = ExprTy;
4124 FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
4125 if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
4126 !ExprTy->isCharType()) {
4127 // 'unichar' is defined as a typedef of unsigned short, but we should
4128 // prefer using the typedef if it is visible.
4129 IntendedTy = S.Context.UnsignedShortTy;
4131 // While we are here, check if the value is an IntegerLiteral that happens
4132 // to be within the valid range.
4133 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) {
4134 const llvm::APInt &V = IL->getValue();
4135 if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy))
4139 LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(),
4140 Sema::LookupOrdinaryName);
4141 if (S.LookupName(Result, S.getCurScope())) {
4142 NamedDecl *ND = Result.getFoundDecl();
4143 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
4144 if (TD->getUnderlyingType() == IntendedTy)
4145 IntendedTy = S.Context.getTypedefType(TD);
4150 // Special-case some of Darwin's platform-independence types by suggesting
4151 // casts to primitive types that are known to be large enough.
4152 bool ShouldNotPrintDirectly = false; StringRef CastTyName;
4153 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
4155 std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E);
4156 if (!CastTy.isNull()) {
4157 IntendedTy = CastTy;
4158 ShouldNotPrintDirectly = true;
4162 // We may be able to offer a FixItHint if it is a supported type.
4163 PrintfSpecifier fixedFS = FS;
4164 bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(),
4165 S.Context, ObjCContext);
4168 // Get the fix string from the fixed format specifier
4169 SmallString<16> buf;
4170 llvm::raw_svector_ostream os(buf);
4171 fixedFS.toString(os);
4173 CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
4175 if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) {
4176 unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
4177 if (match == analyze_format_string::ArgType::NoMatchPedantic) {
4178 diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
4180 // In this case, the specifier is wrong and should be changed to match
4182 EmitFormatDiagnostic(S.PDiag(diag)
4183 << AT.getRepresentativeTypeName(S.Context)
4184 << IntendedTy << IsEnum << E->getSourceRange(),
4186 /*IsStringLocation*/ false, SpecRange,
4187 FixItHint::CreateReplacement(SpecRange, os.str()));
4190 // The canonical type for formatting this value is different from the
4191 // actual type of the expression. (This occurs, for example, with Darwin's
4192 // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but
4193 // should be printed as 'long' for 64-bit compatibility.)
4194 // Rather than emitting a normal format/argument mismatch, we want to
4195 // add a cast to the recommended type (and correct the format string
4197 SmallString<16> CastBuf;
4198 llvm::raw_svector_ostream CastFix(CastBuf);
4200 IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
4203 SmallVector<FixItHint,4> Hints;
4204 if (!AT.matchesType(S.Context, IntendedTy))
4205 Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
4207 if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {
4208 // If there's already a cast present, just replace it.
4209 SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc());
4210 Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str()));
4212 } else if (!requiresParensToAddCast(E)) {
4213 // If the expression has high enough precedence,
4214 // just write the C-style cast.
4215 Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
4218 // Otherwise, add parens around the expression as well as the cast.
4220 Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
4223 SourceLocation After = S.getLocForEndOfToken(E->getLocEnd());
4224 Hints.push_back(FixItHint::CreateInsertion(After, ")"));
4227 if (ShouldNotPrintDirectly) {
4228 // The expression has a type that should not be printed directly.
4229 // We extract the name from the typedef because we don't want to show
4230 // the underlying type in the diagnostic.
4232 if (const TypedefType *TypedefTy = dyn_cast<TypedefType>(ExprTy))
4233 Name = TypedefTy->getDecl()->getName();
4236 EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast)
4237 << Name << IntendedTy << IsEnum
4238 << E->getSourceRange(),
4239 E->getLocStart(), /*IsStringLocation=*/false,
4242 // In this case, the expression could be printed using a different
4243 // specifier, but we've decided that the specifier is probably correct
4244 // and we should cast instead. Just use the normal warning message.
4245 EmitFormatDiagnostic(
4246 S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
4247 << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum
4248 << E->getSourceRange(),
4249 E->getLocStart(), /*IsStringLocation*/false,
4254 const CharSourceRange &CSR = getSpecifierRange(StartSpecifier,
4256 // Since the warning for passing non-POD types to variadic functions
4257 // was deferred until now, we emit a warning for non-POD
4259 switch (S.isValidVarArgType(ExprTy)) {
4260 case Sema::VAK_Valid:
4261 case Sema::VAK_ValidInCXX11: {
4262 unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
4263 if (match == analyze_printf::ArgType::NoMatchPedantic) {
4264 diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
4267 EmitFormatDiagnostic(
4268 S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy
4269 << IsEnum << CSR << E->getSourceRange(),
4270 E->getLocStart(), /*IsStringLocation*/ false, CSR);
4273 case Sema::VAK_Undefined:
4274 case Sema::VAK_MSVCUndefined:
4275 EmitFormatDiagnostic(
4276 S.PDiag(diag::warn_non_pod_vararg_with_format_string)
4277 << S.getLangOpts().CPlusPlus11
4280 << AT.getRepresentativeTypeName(S.Context)
4282 << E->getSourceRange(),
4283 E->getLocStart(), /*IsStringLocation*/false, CSR);
4284 checkForCStrMembers(AT, E);
4287 case Sema::VAK_Invalid:
4288 if (ExprTy->isObjCObjectType())
4289 EmitFormatDiagnostic(
4290 S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format)
4291 << S.getLangOpts().CPlusPlus11
4294 << AT.getRepresentativeTypeName(S.Context)
4296 << E->getSourceRange(),
4297 E->getLocStart(), /*IsStringLocation*/false, CSR);
4299 // FIXME: If this is an initializer list, suggest removing the braces
4300 // or inserting a cast to the target type.
4301 S.Diag(E->getLocStart(), diag::err_cannot_pass_to_vararg_format)
4302 << isa<InitListExpr>(E) << ExprTy << CallType
4303 << AT.getRepresentativeTypeName(S.Context)
4304 << E->getSourceRange();
4308 assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() &&
4309 "format string specifier index out of range");
4310 CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true;
4316 //===--- CHECK: Scanf format string checking ------------------------------===//
4319 class CheckScanfHandler : public CheckFormatHandler {
4321 CheckScanfHandler(Sema &s, const StringLiteral *fexpr,
4322 const Expr *origFormatExpr, unsigned firstDataArg,
4323 unsigned numDataArgs, const char *beg, bool hasVAListArg,
4324 ArrayRef<const Expr *> Args,
4325 unsigned formatIdx, bool inFunctionCall,
4326 Sema::VariadicCallType CallType,
4327 llvm::SmallBitVector &CheckedVarArgs)
4328 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
4329 numDataArgs, beg, hasVAListArg,
4330 Args, formatIdx, inFunctionCall, CallType,
4334 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
4335 const char *startSpecifier,
4336 unsigned specifierLen) override;
4338 bool HandleInvalidScanfConversionSpecifier(
4339 const analyze_scanf::ScanfSpecifier &FS,
4340 const char *startSpecifier,
4341 unsigned specifierLen) override;
4343 void HandleIncompleteScanList(const char *start, const char *end) override;
4347 void CheckScanfHandler::HandleIncompleteScanList(const char *start,
4349 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
4350 getLocationOfByte(end), /*IsStringLocation*/true,
4351 getSpecifierRange(start, end - start));
4354 bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
4355 const analyze_scanf::ScanfSpecifier &FS,
4356 const char *startSpecifier,
4357 unsigned specifierLen) {
4359 const analyze_scanf::ScanfConversionSpecifier &CS =
4360 FS.getConversionSpecifier();
4362 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
4363 getLocationOfByte(CS.getStart()),
4364 startSpecifier, specifierLen,
4365 CS.getStart(), CS.getLength());
4368 bool CheckScanfHandler::HandleScanfSpecifier(
4369 const analyze_scanf::ScanfSpecifier &FS,
4370 const char *startSpecifier,
4371 unsigned specifierLen) {
4373 using namespace analyze_scanf;
4374 using namespace analyze_format_string;
4376 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
4378 // Handle case where '%' and '*' don't consume an argument. These shouldn't
4379 // be used to decide if we are using positional arguments consistently.
4380 if (FS.consumesDataArgument()) {
4383 usesPositionalArgs = FS.usesPositionalArg();
4385 else if (usesPositionalArgs != FS.usesPositionalArg()) {
4386 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
4387 startSpecifier, specifierLen);
4392 // Check if the field with is non-zero.
4393 const OptionalAmount &Amt = FS.getFieldWidth();
4394 if (Amt.getHowSpecified() == OptionalAmount::Constant) {
4395 if (Amt.getConstantAmount() == 0) {
4396 const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
4397 Amt.getConstantLength());
4398 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
4399 getLocationOfByte(Amt.getStart()),
4400 /*IsStringLocation*/true, R,
4401 FixItHint::CreateRemoval(R));
4405 if (!FS.consumesDataArgument()) {
4406 // FIXME: Technically specifying a precision or field width here
4407 // makes no sense. Worth issuing a warning at some point.
4411 // Consume the argument.
4412 unsigned argIndex = FS.getArgIndex();
4413 if (argIndex < NumDataArgs) {
4414 // The check to see if the argIndex is valid will come later.
4415 // We set the bit here because we may exit early from this
4416 // function if we encounter some other error.
4417 CoveredArgs.set(argIndex);
4420 // Check the length modifier is valid with the given conversion specifier.
4421 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
4422 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
4423 diag::warn_format_nonsensical_length);
4424 else if (!FS.hasStandardLengthModifier())
4425 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
4426 else if (!FS.hasStandardLengthConversionCombination())
4427 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
4428 diag::warn_format_non_standard_conversion_spec);
4430 if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
4431 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
4433 // The remaining checks depend on the data arguments.
4437 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
4440 // Check that the argument type matches the format specifier.
4441 const Expr *Ex = getDataArg(argIndex);
4445 const analyze_format_string::ArgType &AT = FS.getArgType(S.Context);
4447 if (!AT.isValid()) {
4451 analyze_format_string::ArgType::MatchKind match =
4452 AT.matchesType(S.Context, Ex->getType());
4453 if (match == analyze_format_string::ArgType::Match) {
4457 ScanfSpecifier fixedFS = FS;
4458 bool success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(),
4459 S.getLangOpts(), S.Context);
4461 unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
4462 if (match == analyze_format_string::ArgType::NoMatchPedantic) {
4463 diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
4467 // Get the fix string from the fixed format specifier.
4468 SmallString<128> buf;
4469 llvm::raw_svector_ostream os(buf);
4470 fixedFS.toString(os);
4472 EmitFormatDiagnostic(
4473 S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context)
4474 << Ex->getType() << false << Ex->getSourceRange(),
4476 /*IsStringLocation*/ false,
4477 getSpecifierRange(startSpecifier, specifierLen),
4478 FixItHint::CreateReplacement(
4479 getSpecifierRange(startSpecifier, specifierLen), os.str()));
4481 EmitFormatDiagnostic(S.PDiag(diag)
4482 << AT.getRepresentativeTypeName(S.Context)
4483 << Ex->getType() << false << Ex->getSourceRange(),
4485 /*IsStringLocation*/ false,
4486 getSpecifierRange(startSpecifier, specifierLen));
4492 void Sema::CheckFormatString(const StringLiteral *FExpr,
4493 const Expr *OrigFormatExpr,
4494 ArrayRef<const Expr *> Args,
4495 bool HasVAListArg, unsigned format_idx,
4496 unsigned firstDataArg, FormatStringType Type,
4497 bool inFunctionCall, VariadicCallType CallType,
4498 llvm::SmallBitVector &CheckedVarArgs) {
4500 // CHECK: is the format string a wide literal?
4501 if (!FExpr->isAscii() && !FExpr->isUTF8()) {
4502 CheckFormatHandler::EmitFormatDiagnostic(
4503 *this, inFunctionCall, Args[format_idx],
4504 PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(),
4505 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
4509 // Str - The format string. NOTE: this is NOT null-terminated!
4510 StringRef StrRef = FExpr->getString();
4511 const char *Str = StrRef.data();
4512 // Account for cases where the string literal is truncated in a declaration.
4513 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType());
4514 assert(T && "String literal not of constant array type!");
4515 size_t TypeSize = T->getSize().getZExtValue();
4516 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
4517 const unsigned numDataArgs = Args.size() - firstDataArg;
4519 // Emit a warning if the string literal is truncated and does not contain an
4520 // embedded null character.
4521 if (TypeSize <= StrRef.size() &&
4522 StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) {
4523 CheckFormatHandler::EmitFormatDiagnostic(
4524 *this, inFunctionCall, Args[format_idx],
4525 PDiag(diag::warn_printf_format_string_not_null_terminated),
4526 FExpr->getLocStart(),
4527 /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange());
4531 // CHECK: empty format string?
4532 if (StrLen == 0 && numDataArgs > 0) {
4533 CheckFormatHandler::EmitFormatDiagnostic(
4534 *this, inFunctionCall, Args[format_idx],
4535 PDiag(diag::warn_empty_format_string), FExpr->getLocStart(),
4536 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
4540 if (Type == FST_Printf || Type == FST_NSString ||
4541 Type == FST_FreeBSDKPrintf || Type == FST_OSTrace) {
4542 CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
4543 numDataArgs, (Type == FST_NSString || Type == FST_OSTrace),
4544 Str, HasVAListArg, Args, format_idx,
4545 inFunctionCall, CallType, CheckedVarArgs);
4547 if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
4549 Context.getTargetInfo(),
4550 Type == FST_FreeBSDKPrintf))
4552 } else if (Type == FST_Scanf) {
4553 CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs,
4554 Str, HasVAListArg, Args, format_idx,
4555 inFunctionCall, CallType, CheckedVarArgs);
4557 if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen,
4559 Context.getTargetInfo()))
4561 } // TODO: handle other formats
4564 bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) {
4565 // Str - The format string. NOTE: this is NOT null-terminated!
4566 StringRef StrRef = FExpr->getString();
4567 const char *Str = StrRef.data();
4568 // Account for cases where the string literal is truncated in a declaration.
4569 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType());
4570 assert(T && "String literal not of constant array type!");
4571 size_t TypeSize = T->getSize().getZExtValue();
4572 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
4573 return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen,
4575 Context.getTargetInfo());
4578 //===--- CHECK: Warn on use of wrong absolute value function. -------------===//
4580 // Returns the related absolute value function that is larger, of 0 if one
4582 static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) {
4583 switch (AbsFunction) {
4587 case Builtin::BI__builtin_abs:
4588 return Builtin::BI__builtin_labs;
4589 case Builtin::BI__builtin_labs:
4590 return Builtin::BI__builtin_llabs;
4591 case Builtin::BI__builtin_llabs:
4594 case Builtin::BI__builtin_fabsf:
4595 return Builtin::BI__builtin_fabs;
4596 case Builtin::BI__builtin_fabs:
4597 return Builtin::BI__builtin_fabsl;
4598 case Builtin::BI__builtin_fabsl:
4601 case Builtin::BI__builtin_cabsf:
4602 return Builtin::BI__builtin_cabs;
4603 case Builtin::BI__builtin_cabs:
4604 return Builtin::BI__builtin_cabsl;
4605 case Builtin::BI__builtin_cabsl:
4608 case Builtin::BIabs:
4609 return Builtin::BIlabs;
4610 case Builtin::BIlabs:
4611 return Builtin::BIllabs;
4612 case Builtin::BIllabs:
4615 case Builtin::BIfabsf:
4616 return Builtin::BIfabs;
4617 case Builtin::BIfabs:
4618 return Builtin::BIfabsl;
4619 case Builtin::BIfabsl:
4622 case Builtin::BIcabsf:
4623 return Builtin::BIcabs;
4624 case Builtin::BIcabs:
4625 return Builtin::BIcabsl;
4626 case Builtin::BIcabsl:
4631 // Returns the argument type of the absolute value function.
4632 static QualType getAbsoluteValueArgumentType(ASTContext &Context,
4637 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
4638 QualType BuiltinType = Context.GetBuiltinType(AbsType, Error);
4639 if (Error != ASTContext::GE_None)
4642 const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>();
4646 if (FT->getNumParams() != 1)
4649 return FT->getParamType(0);
4652 // Returns the best absolute value function, or zero, based on type and
4653 // current absolute value function.
4654 static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType,
4655 unsigned AbsFunctionKind) {
4656 unsigned BestKind = 0;
4657 uint64_t ArgSize = Context.getTypeSize(ArgType);
4658 for (unsigned Kind = AbsFunctionKind; Kind != 0;
4659 Kind = getLargerAbsoluteValueFunction(Kind)) {
4660 QualType ParamType = getAbsoluteValueArgumentType(Context, Kind);
4661 if (Context.getTypeSize(ParamType) >= ArgSize) {
4664 else if (Context.hasSameType(ParamType, ArgType)) {
4673 enum AbsoluteValueKind {
4679 static AbsoluteValueKind getAbsoluteValueKind(QualType T) {
4680 if (T->isIntegralOrEnumerationType())
4682 if (T->isRealFloatingType())
4683 return AVK_Floating;
4684 if (T->isAnyComplexType())
4687 llvm_unreachable("Type not integer, floating, or complex");
4690 // Changes the absolute value function to a different type. Preserves whether
4691 // the function is a builtin.
4692 static unsigned changeAbsFunction(unsigned AbsKind,
4693 AbsoluteValueKind ValueKind) {
4694 switch (ValueKind) {
4699 case Builtin::BI__builtin_fabsf:
4700 case Builtin::BI__builtin_fabs:
4701 case Builtin::BI__builtin_fabsl:
4702 case Builtin::BI__builtin_cabsf:
4703 case Builtin::BI__builtin_cabs:
4704 case Builtin::BI__builtin_cabsl:
4705 return Builtin::BI__builtin_abs;
4706 case Builtin::BIfabsf:
4707 case Builtin::BIfabs:
4708 case Builtin::BIfabsl:
4709 case Builtin::BIcabsf:
4710 case Builtin::BIcabs:
4711 case Builtin::BIcabsl:
4712 return Builtin::BIabs;
4718 case Builtin::BI__builtin_abs:
4719 case Builtin::BI__builtin_labs:
4720 case Builtin::BI__builtin_llabs:
4721 case Builtin::BI__builtin_cabsf:
4722 case Builtin::BI__builtin_cabs:
4723 case Builtin::BI__builtin_cabsl:
4724 return Builtin::BI__builtin_fabsf;
4725 case Builtin::BIabs:
4726 case Builtin::BIlabs:
4727 case Builtin::BIllabs:
4728 case Builtin::BIcabsf:
4729 case Builtin::BIcabs:
4730 case Builtin::BIcabsl:
4731 return Builtin::BIfabsf;
4737 case Builtin::BI__builtin_abs:
4738 case Builtin::BI__builtin_labs:
4739 case Builtin::BI__builtin_llabs:
4740 case Builtin::BI__builtin_fabsf:
4741 case Builtin::BI__builtin_fabs:
4742 case Builtin::BI__builtin_fabsl:
4743 return Builtin::BI__builtin_cabsf;
4744 case Builtin::BIabs:
4745 case Builtin::BIlabs:
4746 case Builtin::BIllabs:
4747 case Builtin::BIfabsf:
4748 case Builtin::BIfabs:
4749 case Builtin::BIfabsl:
4750 return Builtin::BIcabsf;
4753 llvm_unreachable("Unable to convert function");
4756 static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) {
4757 const IdentifierInfo *FnInfo = FDecl->getIdentifier();
4761 switch (FDecl->getBuiltinID()) {
4764 case Builtin::BI__builtin_abs:
4765 case Builtin::BI__builtin_fabs:
4766 case Builtin::BI__builtin_fabsf:
4767 case Builtin::BI__builtin_fabsl:
4768 case Builtin::BI__builtin_labs:
4769 case Builtin::BI__builtin_llabs:
4770 case Builtin::BI__builtin_cabs:
4771 case Builtin::BI__builtin_cabsf:
4772 case Builtin::BI__builtin_cabsl:
4773 case Builtin::BIabs:
4774 case Builtin::BIlabs:
4775 case Builtin::BIllabs:
4776 case Builtin::BIfabs:
4777 case Builtin::BIfabsf:
4778 case Builtin::BIfabsl:
4779 case Builtin::BIcabs:
4780 case Builtin::BIcabsf:
4781 case Builtin::BIcabsl:
4782 return FDecl->getBuiltinID();
4784 llvm_unreachable("Unknown Builtin type");
4787 // If the replacement is valid, emit a note with replacement function.
4788 // Additionally, suggest including the proper header if not already included.
4789 static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range,
4790 unsigned AbsKind, QualType ArgType) {
4791 bool EmitHeaderHint = true;
4792 const char *HeaderName = nullptr;
4793 const char *FunctionName = nullptr;
4794 if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) {
4795 FunctionName = "std::abs";
4796 if (ArgType->isIntegralOrEnumerationType()) {
4797 HeaderName = "cstdlib";
4798 } else if (ArgType->isRealFloatingType()) {
4799 HeaderName = "cmath";
4801 llvm_unreachable("Invalid Type");
4804 // Lookup all std::abs
4805 if (NamespaceDecl *Std = S.getStdNamespace()) {
4806 LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName);
4807 R.suppressDiagnostics();
4808 S.LookupQualifiedName(R, Std);
4810 for (const auto *I : R) {
4811 const FunctionDecl *FDecl = nullptr;
4812 if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) {
4813 FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl());
4815 FDecl = dyn_cast<FunctionDecl>(I);
4820 // Found std::abs(), check that they are the right ones.
4821 if (FDecl->getNumParams() != 1)
4824 // Check that the parameter type can handle the argument.
4825 QualType ParamType = FDecl->getParamDecl(0)->getType();
4826 if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) &&
4827 S.Context.getTypeSize(ArgType) <=
4828 S.Context.getTypeSize(ParamType)) {
4829 // Found a function, don't need the header hint.
4830 EmitHeaderHint = false;
4836 FunctionName = S.Context.BuiltinInfo.GetName(AbsKind);
4837 HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind);
4840 DeclarationName DN(&S.Context.Idents.get(FunctionName));
4841 LookupResult R(S, DN, Loc, Sema::LookupAnyName);
4842 R.suppressDiagnostics();
4843 S.LookupName(R, S.getCurScope());
4845 if (R.isSingleResult()) {
4846 FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl());
4847 if (FD && FD->getBuiltinID() == AbsKind) {
4848 EmitHeaderHint = false;
4852 } else if (!R.empty()) {
4858 S.Diag(Loc, diag::note_replace_abs_function)
4859 << FunctionName << FixItHint::CreateReplacement(Range, FunctionName);
4864 if (!EmitHeaderHint)
4867 S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName
4871 static bool IsFunctionStdAbs(const FunctionDecl *FDecl) {
4875 if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr("abs"))
4878 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(FDecl->getDeclContext());
4880 while (ND && ND->isInlineNamespace()) {
4881 ND = dyn_cast<NamespaceDecl>(ND->getDeclContext());
4884 if (!ND || !ND->getIdentifier() || !ND->getIdentifier()->isStr("std"))
4887 if (!isa<TranslationUnitDecl>(ND->getDeclContext()))
4893 // Warn when using the wrong abs() function.
4894 void Sema::CheckAbsoluteValueFunction(const CallExpr *Call,
4895 const FunctionDecl *FDecl,
4896 IdentifierInfo *FnInfo) {
4897 if (Call->getNumArgs() != 1)
4900 unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl);
4901 bool IsStdAbs = IsFunctionStdAbs(FDecl);
4902 if (AbsKind == 0 && !IsStdAbs)
4905 QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType();
4906 QualType ParamType = Call->getArg(0)->getType();
4908 // Unsigned types cannot be negative. Suggest removing the absolute value
4910 if (ArgType->isUnsignedIntegerType()) {
4911 const char *FunctionName =
4912 IsStdAbs ? "std::abs" : Context.BuiltinInfo.GetName(AbsKind);
4913 Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType;
4914 Diag(Call->getExprLoc(), diag::note_remove_abs)
4916 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange());
4920 // std::abs has overloads which prevent most of the absolute value problems
4925 AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType);
4926 AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType);
4928 // The argument and parameter are the same kind. Check if they are the right
4930 if (ArgValueKind == ParamValueKind) {
4931 if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType))
4934 unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind);
4935 Diag(Call->getExprLoc(), diag::warn_abs_too_small)
4936 << FDecl << ArgType << ParamType;
4938 if (NewAbsKind == 0)
4941 emitReplacement(*this, Call->getExprLoc(),
4942 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
4946 // ArgValueKind != ParamValueKind
4947 // The wrong type of absolute value function was used. Attempt to find the
4949 unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind);
4950 NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind);
4951 if (NewAbsKind == 0)
4954 Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type)
4955 << FDecl << ParamValueKind << ArgValueKind;
4957 emitReplacement(*this, Call->getExprLoc(),
4958 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
4962 //===--- CHECK: Standard memory functions ---------------------------------===//
4964 /// \brief Takes the expression passed to the size_t parameter of functions
4965 /// such as memcmp, strncat, etc and warns if it's a comparison.
4967 /// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`.
4968 static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E,
4969 IdentifierInfo *FnName,
4970 SourceLocation FnLoc,
4971 SourceLocation RParenLoc) {
4972 const BinaryOperator *Size = dyn_cast<BinaryOperator>(E);
4976 // if E is binop and op is >, <, >=, <=, ==, &&, ||:
4977 if (!Size->isComparisonOp() && !Size->isEqualityOp() && !Size->isLogicalOp())
4980 SourceRange SizeRange = Size->getSourceRange();
4981 S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison)
4982 << SizeRange << FnName;
4983 S.Diag(FnLoc, diag::note_memsize_comparison_paren)
4984 << FnName << FixItHint::CreateInsertion(
4985 S.getLocForEndOfToken(Size->getLHS()->getLocEnd()), ")")
4986 << FixItHint::CreateRemoval(RParenLoc);
4987 S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence)
4988 << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(")
4989 << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()),
4995 /// \brief Determine whether the given type is or contains a dynamic class type
4996 /// (e.g., whether it has a vtable).
4997 static const CXXRecordDecl *getContainedDynamicClass(QualType T,
4998 bool &IsContained) {
4999 // Look through array types while ignoring qualifiers.
5000 const Type *Ty = T->getBaseElementTypeUnsafe();
5001 IsContained = false;
5003 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
5004 RD = RD ? RD->getDefinition() : nullptr;
5008 if (RD->isDynamicClass())
5011 // Check all the fields. If any bases were dynamic, the class is dynamic.
5012 // It's impossible for a class to transitively contain itself by value, so
5013 // infinite recursion is impossible.
5014 for (auto *FD : RD->fields()) {
5016 if (const CXXRecordDecl *ContainedRD =
5017 getContainedDynamicClass(FD->getType(), SubContained)) {
5026 /// \brief If E is a sizeof expression, returns its argument expression,
5027 /// otherwise returns NULL.
5028 static const Expr *getSizeOfExprArg(const Expr *E) {
5029 if (const UnaryExprOrTypeTraitExpr *SizeOf =
5030 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
5031 if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType())
5032 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
5037 /// \brief If E is a sizeof expression, returns its argument type.
5038 static QualType getSizeOfArgType(const Expr *E) {
5039 if (const UnaryExprOrTypeTraitExpr *SizeOf =
5040 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
5041 if (SizeOf->getKind() == clang::UETT_SizeOf)
5042 return SizeOf->getTypeOfArgument();
5047 /// \brief Check for dangerous or invalid arguments to memset().
5049 /// This issues warnings on known problematic, dangerous or unspecified
5050 /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
5053 /// \param Call The call expression to diagnose.
5054 void Sema::CheckMemaccessArguments(const CallExpr *Call,
5056 IdentifierInfo *FnName) {
5059 // It is possible to have a non-standard definition of memset. Validate
5060 // we have enough arguments, and if not, abort further checking.
5061 unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3);
5062 if (Call->getNumArgs() < ExpectedNumArgs)
5065 unsigned LastArg = (BId == Builtin::BImemset ||
5066 BId == Builtin::BIstrndup ? 1 : 2);
5067 unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2);
5068 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
5070 if (CheckMemorySizeofForComparison(*this, LenExpr, FnName,
5071 Call->getLocStart(), Call->getRParenLoc()))
5074 // We have special checking when the length is a sizeof expression.
5075 QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
5076 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
5077 llvm::FoldingSetNodeID SizeOfArgID;
5079 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
5080 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
5081 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
5083 QualType DestTy = Dest->getType();
5085 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
5086 PointeeTy = DestPtrTy->getPointeeType();
5088 // Never warn about void type pointers. This can be used to suppress
5090 if (PointeeTy->isVoidType())
5093 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
5094 // actually comparing the expressions for equality. Because computing the
5095 // expression IDs can be expensive, we only do this if the diagnostic is
5098 !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess,
5099 SizeOfArg->getExprLoc())) {
5100 // We only compute IDs for expressions if the warning is enabled, and
5101 // cache the sizeof arg's ID.
5102 if (SizeOfArgID == llvm::FoldingSetNodeID())
5103 SizeOfArg->Profile(SizeOfArgID, Context, true);
5104 llvm::FoldingSetNodeID DestID;
5105 Dest->Profile(DestID, Context, true);
5106 if (DestID == SizeOfArgID) {
5107 // TODO: For strncpy() and friends, this could suggest sizeof(dst)
5108 // over sizeof(src) as well.
5109 unsigned ActionIdx = 0; // Default is to suggest dereferencing.
5110 StringRef ReadableName = FnName->getName();
5112 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
5113 if (UnaryOp->getOpcode() == UO_AddrOf)
5114 ActionIdx = 1; // If its an address-of operator, just remove it.
5115 if (!PointeeTy->isIncompleteType() &&
5116 (Context.getTypeSize(PointeeTy) == Context.getCharWidth()))
5117 ActionIdx = 2; // If the pointee's size is sizeof(char),
5118 // suggest an explicit length.
5120 // If the function is defined as a builtin macro, do not show macro
5122 SourceLocation SL = SizeOfArg->getExprLoc();
5123 SourceRange DSR = Dest->getSourceRange();
5124 SourceRange SSR = SizeOfArg->getSourceRange();
5125 SourceManager &SM = getSourceManager();
5127 if (SM.isMacroArgExpansion(SL)) {
5128 ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts);
5129 SL = SM.getSpellingLoc(SL);
5130 DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()),
5131 SM.getSpellingLoc(DSR.getEnd()));
5132 SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()),
5133 SM.getSpellingLoc(SSR.getEnd()));
5136 DiagRuntimeBehavior(SL, SizeOfArg,
5137 PDiag(diag::warn_sizeof_pointer_expr_memaccess)
5143 DiagRuntimeBehavior(SL, SizeOfArg,
5144 PDiag(diag::warn_sizeof_pointer_expr_memaccess_note)
5152 // Also check for cases where the sizeof argument is the exact same
5153 // type as the memory argument, and where it points to a user-defined
5155 if (SizeOfArgTy != QualType()) {
5156 if (PointeeTy->isRecordType() &&
5157 Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
5158 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
5159 PDiag(diag::warn_sizeof_pointer_type_memaccess)
5160 << FnName << SizeOfArgTy << ArgIdx
5161 << PointeeTy << Dest->getSourceRange()
5162 << LenExpr->getSourceRange());
5166 } else if (DestTy->isArrayType()) {
5170 if (PointeeTy == QualType())
5173 // Always complain about dynamic classes.
5175 if (const CXXRecordDecl *ContainedRD =
5176 getContainedDynamicClass(PointeeTy, IsContained)) {
5178 unsigned OperationType = 0;
5179 // "overwritten" if we're warning about the destination for any call
5180 // but memcmp; otherwise a verb appropriate to the call.
5181 if (ArgIdx != 0 || BId == Builtin::BImemcmp) {
5182 if (BId == Builtin::BImemcpy)
5184 else if(BId == Builtin::BImemmove)
5186 else if (BId == Builtin::BImemcmp)
5190 DiagRuntimeBehavior(
5191 Dest->getExprLoc(), Dest,
5192 PDiag(diag::warn_dyn_class_memaccess)
5193 << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx)
5194 << FnName << IsContained << ContainedRD << OperationType
5195 << Call->getCallee()->getSourceRange());
5196 } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
5197 BId != Builtin::BImemset)
5198 DiagRuntimeBehavior(
5199 Dest->getExprLoc(), Dest,
5200 PDiag(diag::warn_arc_object_memaccess)
5201 << ArgIdx << FnName << PointeeTy
5202 << Call->getCallee()->getSourceRange());
5206 DiagRuntimeBehavior(
5207 Dest->getExprLoc(), Dest,
5208 PDiag(diag::note_bad_memaccess_silence)
5209 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
5215 // A little helper routine: ignore addition and subtraction of integer literals.
5216 // This intentionally does not ignore all integer constant expressions because
5217 // we don't want to remove sizeof().
5218 static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
5219 Ex = Ex->IgnoreParenCasts();
5222 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
5223 if (!BO || !BO->isAdditiveOp())
5226 const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
5227 const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
5229 if (isa<IntegerLiteral>(RHS))
5231 else if (isa<IntegerLiteral>(LHS))
5240 static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty,
5241 ASTContext &Context) {
5242 // Only handle constant-sized or VLAs, but not flexible members.
5243 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) {
5244 // Only issue the FIXIT for arrays of size > 1.
5245 if (CAT->getSize().getSExtValue() <= 1)
5247 } else if (!Ty->isVariableArrayType()) {
5253 // Warn if the user has made the 'size' argument to strlcpy or strlcat
5254 // be the size of the source, instead of the destination.
5255 void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
5256 IdentifierInfo *FnName) {
5258 // Don't crash if the user has the wrong number of arguments
5259 unsigned NumArgs = Call->getNumArgs();
5260 if ((NumArgs != 3) && (NumArgs != 4))
5263 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
5264 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
5265 const Expr *CompareWithSrc = nullptr;
5267 if (CheckMemorySizeofForComparison(*this, SizeArg, FnName,
5268 Call->getLocStart(), Call->getRParenLoc()))
5271 // Look for 'strlcpy(dst, x, sizeof(x))'
5272 if (const Expr *Ex = getSizeOfExprArg(SizeArg))
5273 CompareWithSrc = Ex;
5275 // Look for 'strlcpy(dst, x, strlen(x))'
5276 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
5277 if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen &&
5278 SizeCall->getNumArgs() == 1)
5279 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
5283 if (!CompareWithSrc)
5286 // Determine if the argument to sizeof/strlen is equal to the source
5287 // argument. In principle there's all kinds of things you could do
5288 // here, for instance creating an == expression and evaluating it with
5289 // EvaluateAsBooleanCondition, but this uses a more direct technique:
5290 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
5294 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
5295 if (!CompareWithSrcDRE ||
5296 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
5299 const Expr *OriginalSizeArg = Call->getArg(2);
5300 Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size)
5301 << OriginalSizeArg->getSourceRange() << FnName;
5303 // Output a FIXIT hint if the destination is an array (rather than a
5304 // pointer to an array). This could be enhanced to handle some
5305 // pointers if we know the actual size, like if DstArg is 'array+2'
5306 // we could say 'sizeof(array)-2'.
5307 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
5308 if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context))
5311 SmallString<128> sizeString;
5312 llvm::raw_svector_ostream OS(sizeString);
5314 DstArg->printPretty(OS, nullptr, getPrintingPolicy());
5317 Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
5318 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
5322 /// Check if two expressions refer to the same declaration.
5323 static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
5324 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
5325 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
5326 return D1->getDecl() == D2->getDecl();
5330 static const Expr *getStrlenExprArg(const Expr *E) {
5331 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
5332 const FunctionDecl *FD = CE->getDirectCallee();
5333 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
5335 return CE->getArg(0)->IgnoreParenCasts();
5340 // Warn on anti-patterns as the 'size' argument to strncat.
5341 // The correct size argument should look like following:
5342 // strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
5343 void Sema::CheckStrncatArguments(const CallExpr *CE,
5344 IdentifierInfo *FnName) {
5345 // Don't crash if the user has the wrong number of arguments.
5346 if (CE->getNumArgs() < 3)
5348 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
5349 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
5350 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
5352 if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getLocStart(),
5353 CE->getRParenLoc()))
5356 // Identify common expressions, which are wrongly used as the size argument
5357 // to strncat and may lead to buffer overflows.
5358 unsigned PatternType = 0;
5359 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
5361 if (referToTheSameDecl(SizeOfArg, DstArg))
5364 else if (referToTheSameDecl(SizeOfArg, SrcArg))
5366 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
5367 if (BE->getOpcode() == BO_Sub) {
5368 const Expr *L = BE->getLHS()->IgnoreParenCasts();
5369 const Expr *R = BE->getRHS()->IgnoreParenCasts();
5370 // - sizeof(dst) - strlen(dst)
5371 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
5372 referToTheSameDecl(DstArg, getStrlenExprArg(R)))
5374 // - sizeof(src) - (anything)
5375 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
5380 if (PatternType == 0)
5383 // Generate the diagnostic.
5384 SourceLocation SL = LenArg->getLocStart();
5385 SourceRange SR = LenArg->getSourceRange();
5386 SourceManager &SM = getSourceManager();
5388 // If the function is defined as a builtin macro, do not show macro expansion.
5389 if (SM.isMacroArgExpansion(SL)) {
5390 SL = SM.getSpellingLoc(SL);
5391 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
5392 SM.getSpellingLoc(SR.getEnd()));
5395 // Check if the destination is an array (rather than a pointer to an array).
5396 QualType DstTy = DstArg->getType();
5397 bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy,
5399 if (!isKnownSizeArray) {
5400 if (PatternType == 1)
5401 Diag(SL, diag::warn_strncat_wrong_size) << SR;
5403 Diag(SL, diag::warn_strncat_src_size) << SR;
5407 if (PatternType == 1)
5408 Diag(SL, diag::warn_strncat_large_size) << SR;
5410 Diag(SL, diag::warn_strncat_src_size) << SR;
5412 SmallString<128> sizeString;
5413 llvm::raw_svector_ostream OS(sizeString);
5415 DstArg->printPretty(OS, nullptr, getPrintingPolicy());
5418 DstArg->printPretty(OS, nullptr, getPrintingPolicy());
5421 Diag(SL, diag::note_strncat_wrong_size)
5422 << FixItHint::CreateReplacement(SR, OS.str());
5425 //===--- CHECK: Return Address of Stack Variable --------------------------===//
5427 static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
5429 static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars,
5432 /// CheckReturnStackAddr - Check if a return statement returns the address
5433 /// of a stack variable.
5435 CheckReturnStackAddr(Sema &S, Expr *RetValExp, QualType lhsType,
5436 SourceLocation ReturnLoc) {
5438 Expr *stackE = nullptr;
5439 SmallVector<DeclRefExpr *, 8> refVars;
5441 // Perform checking for returned stack addresses, local blocks,
5442 // label addresses or references to temporaries.
5443 if (lhsType->isPointerType() ||
5444 (!S.getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) {
5445 stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/nullptr);
5446 } else if (lhsType->isReferenceType()) {
5447 stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/nullptr);
5451 return; // Nothing suspicious was found.
5453 SourceLocation diagLoc;
5454 SourceRange diagRange;
5455 if (refVars.empty()) {
5456 diagLoc = stackE->getLocStart();
5457 diagRange = stackE->getSourceRange();
5459 // We followed through a reference variable. 'stackE' contains the
5460 // problematic expression but we will warn at the return statement pointing
5461 // at the reference variable. We will later display the "trail" of
5462 // reference variables using notes.
5463 diagLoc = refVars[0]->getLocStart();
5464 diagRange = refVars[0]->getSourceRange();
5467 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var.
5468 S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref
5469 : diag::warn_ret_stack_addr)
5470 << DR->getDecl()->getDeclName() << diagRange;
5471 } else if (isa<BlockExpr>(stackE)) { // local block.
5472 S.Diag(diagLoc, diag::err_ret_local_block) << diagRange;
5473 } else if (isa<AddrLabelExpr>(stackE)) { // address of label.
5474 S.Diag(diagLoc, diag::warn_ret_addr_label) << diagRange;
5475 } else { // local temporary.
5476 S.Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref
5477 : diag::warn_ret_local_temp_addr)
5481 // Display the "trail" of reference variables that we followed until we
5482 // found the problematic expression using notes.
5483 for (unsigned i = 0, e = refVars.size(); i != e; ++i) {
5484 VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl());
5485 // If this var binds to another reference var, show the range of the next
5486 // var, otherwise the var binds to the problematic expression, in which case
5487 // show the range of the expression.
5488 SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange()
5489 : stackE->getSourceRange();
5490 S.Diag(VD->getLocation(), diag::note_ref_var_local_bind)
5491 << VD->getDeclName() << range;
5495 /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
5496 /// check if the expression in a return statement evaluates to an address
5497 /// to a location on the stack, a local block, an address of a label, or a
5498 /// reference to local temporary. The recursion is used to traverse the
5499 /// AST of the return expression, with recursion backtracking when we
5500 /// encounter a subexpression that (1) clearly does not lead to one of the
5501 /// above problematic expressions (2) is something we cannot determine leads to
5502 /// a problematic expression based on such local checking.
5504 /// Both EvalAddr and EvalVal follow through reference variables to evaluate
5505 /// the expression that they point to. Such variables are added to the
5506 /// 'refVars' vector so that we know what the reference variable "trail" was.
5508 /// EvalAddr processes expressions that are pointers that are used as
5509 /// references (and not L-values). EvalVal handles all other values.
5510 /// At the base case of the recursion is a check for the above problematic
5513 /// This implementation handles:
5515 /// * pointer-to-pointer casts
5516 /// * implicit conversions from array references to pointers
5517 /// * taking the address of fields
5518 /// * arbitrary interplay between "&" and "*" operators
5519 /// * pointer arithmetic from an address of a stack variable
5520 /// * taking the address of an array element where the array is on the stack
5521 static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
5523 if (E->isTypeDependent())
5526 // We should only be called for evaluating pointer expressions.
5527 assert((E->getType()->isAnyPointerType() ||
5528 E->getType()->isBlockPointerType() ||
5529 E->getType()->isObjCQualifiedIdType()) &&
5530 "EvalAddr only works on pointers");
5532 E = E->IgnoreParens();
5534 // Our "symbolic interpreter" is just a dispatch off the currently
5535 // viewed AST node. We then recursively traverse the AST by calling
5536 // EvalAddr and EvalVal appropriately.
5537 switch (E->getStmtClass()) {
5538 case Stmt::DeclRefExprClass: {
5539 DeclRefExpr *DR = cast<DeclRefExpr>(E);
5541 // If we leave the immediate function, the lifetime isn't about to end.
5542 if (DR->refersToEnclosingVariableOrCapture())
5545 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
5546 // If this is a reference variable, follow through to the expression that
5548 if (V->hasLocalStorage() &&
5549 V->getType()->isReferenceType() && V->hasInit()) {
5550 // Add the reference variable to the "trail".
5551 refVars.push_back(DR);
5552 return EvalAddr(V->getInit(), refVars, ParentDecl);
5558 case Stmt::UnaryOperatorClass: {
5559 // The only unary operator that make sense to handle here
5560 // is AddrOf. All others don't make sense as pointers.
5561 UnaryOperator *U = cast<UnaryOperator>(E);
5563 if (U->getOpcode() == UO_AddrOf)
5564 return EvalVal(U->getSubExpr(), refVars, ParentDecl);
5569 case Stmt::BinaryOperatorClass: {
5570 // Handle pointer arithmetic. All other binary operators are not valid
5572 BinaryOperator *B = cast<BinaryOperator>(E);
5573 BinaryOperatorKind op = B->getOpcode();
5575 if (op != BO_Add && op != BO_Sub)
5578 Expr *Base = B->getLHS();
5580 // Determine which argument is the real pointer base. It could be
5581 // the RHS argument instead of the LHS.
5582 if (!Base->getType()->isPointerType()) Base = B->getRHS();
5584 assert (Base->getType()->isPointerType());
5585 return EvalAddr(Base, refVars, ParentDecl);
5588 // For conditional operators we need to see if either the LHS or RHS are
5589 // valid DeclRefExpr*s. If one of them is valid, we return it.
5590 case Stmt::ConditionalOperatorClass: {
5591 ConditionalOperator *C = cast<ConditionalOperator>(E);
5593 // Handle the GNU extension for missing LHS.
5594 // FIXME: That isn't a ConditionalOperator, so doesn't get here.
5595 if (Expr *LHSExpr = C->getLHS()) {
5596 // In C++, we can have a throw-expression, which has 'void' type.
5597 if (!LHSExpr->getType()->isVoidType())
5598 if (Expr *LHS = EvalAddr(LHSExpr, refVars, ParentDecl))
5602 // In C++, we can have a throw-expression, which has 'void' type.
5603 if (C->getRHS()->getType()->isVoidType())
5606 return EvalAddr(C->getRHS(), refVars, ParentDecl);
5609 case Stmt::BlockExprClass:
5610 if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures())
5611 return E; // local block.
5614 case Stmt::AddrLabelExprClass:
5615 return E; // address of label.
5617 case Stmt::ExprWithCleanupsClass:
5618 return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,
5621 // For casts, we need to handle conversions from arrays to
5622 // pointer values, and pointer-to-pointer conversions.
5623 case Stmt::ImplicitCastExprClass:
5624 case Stmt::CStyleCastExprClass:
5625 case Stmt::CXXFunctionalCastExprClass:
5626 case Stmt::ObjCBridgedCastExprClass:
5627 case Stmt::CXXStaticCastExprClass:
5628 case Stmt::CXXDynamicCastExprClass:
5629 case Stmt::CXXConstCastExprClass:
5630 case Stmt::CXXReinterpretCastExprClass: {
5631 Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
5632 switch (cast<CastExpr>(E)->getCastKind()) {
5633 case CK_LValueToRValue:
5635 case CK_BaseToDerived:
5636 case CK_DerivedToBase:
5637 case CK_UncheckedDerivedToBase:
5639 case CK_CPointerToObjCPointerCast:
5640 case CK_BlockPointerToObjCPointerCast:
5641 case CK_AnyPointerToBlockPointerCast:
5642 return EvalAddr(SubExpr, refVars, ParentDecl);
5644 case CK_ArrayToPointerDecay:
5645 return EvalVal(SubExpr, refVars, ParentDecl);
5648 if (SubExpr->getType()->isAnyPointerType() ||
5649 SubExpr->getType()->isBlockPointerType() ||
5650 SubExpr->getType()->isObjCQualifiedIdType())
5651 return EvalAddr(SubExpr, refVars, ParentDecl);
5660 case Stmt::MaterializeTemporaryExprClass:
5661 if (Expr *Result = EvalAddr(
5662 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
5663 refVars, ParentDecl))
5668 // Everything else: we simply don't reason about them.
5675 /// EvalVal - This function is complements EvalAddr in the mutual recursion.
5676 /// See the comments for EvalAddr for more details.
5677 static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
5680 // We should only be called for evaluating non-pointer expressions, or
5681 // expressions with a pointer type that are not used as references but instead
5682 // are l-values (e.g., DeclRefExpr with a pointer type).
5684 // Our "symbolic interpreter" is just a dispatch off the currently
5685 // viewed AST node. We then recursively traverse the AST by calling
5686 // EvalAddr and EvalVal appropriately.
5688 E = E->IgnoreParens();
5689 switch (E->getStmtClass()) {
5690 case Stmt::ImplicitCastExprClass: {
5691 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
5692 if (IE->getValueKind() == VK_LValue) {
5693 E = IE->getSubExpr();
5699 case Stmt::ExprWithCleanupsClass:
5700 return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl);
5702 case Stmt::DeclRefExprClass: {
5703 // When we hit a DeclRefExpr we are looking at code that refers to a
5704 // variable's name. If it's not a reference variable we check if it has
5705 // local storage within the function, and if so, return the expression.
5706 DeclRefExpr *DR = cast<DeclRefExpr>(E);
5708 // If we leave the immediate function, the lifetime isn't about to end.
5709 if (DR->refersToEnclosingVariableOrCapture())
5712 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) {
5713 // Check if it refers to itself, e.g. "int& i = i;".
5714 if (V == ParentDecl)
5717 if (V->hasLocalStorage()) {
5718 if (!V->getType()->isReferenceType())
5721 // Reference variable, follow through to the expression that
5724 // Add the reference variable to the "trail".
5725 refVars.push_back(DR);
5726 return EvalVal(V->getInit(), refVars, V);
5734 case Stmt::UnaryOperatorClass: {
5735 // The only unary operator that make sense to handle here
5736 // is Deref. All others don't resolve to a "name." This includes
5737 // handling all sorts of rvalues passed to a unary operator.
5738 UnaryOperator *U = cast<UnaryOperator>(E);
5740 if (U->getOpcode() == UO_Deref)
5741 return EvalAddr(U->getSubExpr(), refVars, ParentDecl);
5746 case Stmt::ArraySubscriptExprClass: {
5747 // Array subscripts are potential references to data on the stack. We
5748 // retrieve the DeclRefExpr* for the array variable if it indeed
5749 // has local storage.
5750 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl);
5753 case Stmt::ConditionalOperatorClass: {
5754 // For conditional operators we need to see if either the LHS or RHS are
5755 // non-NULL Expr's. If one is non-NULL, we return it.
5756 ConditionalOperator *C = cast<ConditionalOperator>(E);
5758 // Handle the GNU extension for missing LHS.
5759 if (Expr *LHSExpr = C->getLHS()) {
5760 // In C++, we can have a throw-expression, which has 'void' type.
5761 if (!LHSExpr->getType()->isVoidType())
5762 if (Expr *LHS = EvalVal(LHSExpr, refVars, ParentDecl))
5766 // In C++, we can have a throw-expression, which has 'void' type.
5767 if (C->getRHS()->getType()->isVoidType())
5770 return EvalVal(C->getRHS(), refVars, ParentDecl);
5773 // Accesses to members are potential references to data on the stack.
5774 case Stmt::MemberExprClass: {
5775 MemberExpr *M = cast<MemberExpr>(E);
5777 // Check for indirect access. We only want direct field accesses.
5781 // Check whether the member type is itself a reference, in which case
5782 // we're not going to refer to the member, but to what the member refers to.
5783 if (M->getMemberDecl()->getType()->isReferenceType())
5786 return EvalVal(M->getBase(), refVars, ParentDecl);
5789 case Stmt::MaterializeTemporaryExprClass:
5790 if (Expr *Result = EvalVal(
5791 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
5792 refVars, ParentDecl))
5798 // Check that we don't return or take the address of a reference to a
5799 // temporary. This is only useful in C++.
5800 if (!E->isTypeDependent() && E->isRValue())
5803 // Everything else: we simply don't reason about them.
5810 Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType,
5811 SourceLocation ReturnLoc,
5813 const AttrVec *Attrs,
5814 const FunctionDecl *FD) {
5815 CheckReturnStackAddr(*this, RetValExp, lhsType, ReturnLoc);
5817 // Check if the return value is null but should not be.
5818 if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) ||
5819 (!isObjCMethod && isNonNullType(Context, lhsType))) &&
5820 CheckNonNullExpr(*this, RetValExp))
5821 Diag(ReturnLoc, diag::warn_null_ret)
5822 << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange();
5824 // C++11 [basic.stc.dynamic.allocation]p4:
5825 // If an allocation function declared with a non-throwing
5826 // exception-specification fails to allocate storage, it shall return
5827 // a null pointer. Any other allocation function that fails to allocate
5828 // storage shall indicate failure only by throwing an exception [...]
5830 OverloadedOperatorKind Op = FD->getOverloadedOperator();
5831 if (Op == OO_New || Op == OO_Array_New) {
5832 const FunctionProtoType *Proto
5833 = FD->getType()->castAs<FunctionProtoType>();
5834 if (!Proto->isNothrow(Context, /*ResultIfDependent*/true) &&
5835 CheckNonNullExpr(*this, RetValExp))
5836 Diag(ReturnLoc, diag::warn_operator_new_returns_null)
5837 << FD << getLangOpts().CPlusPlus11;
5842 //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
5844 /// Check for comparisons of floating point operands using != and ==.
5845 /// Issue a warning if these are no self-comparisons, as they are not likely
5846 /// to do what the programmer intended.
5847 void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
5848 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
5849 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
5851 // Special case: check for x == x (which is OK).
5852 // Do not emit warnings for such cases.
5853 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
5854 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
5855 if (DRL->getDecl() == DRR->getDecl())
5859 // Special case: check for comparisons against literals that can be exactly
5860 // represented by APFloat. In such cases, do not emit a warning. This
5861 // is a heuristic: often comparison against such literals are used to
5862 // detect if a value in a variable has not changed. This clearly can
5863 // lead to false negatives.
5864 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
5868 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
5872 // Check for comparisons with builtin types.
5873 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
5874 if (CL->getBuiltinCallee())
5877 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
5878 if (CR->getBuiltinCallee())
5881 // Emit the diagnostic.
5882 Diag(Loc, diag::warn_floatingpoint_eq)
5883 << LHS->getSourceRange() << RHS->getSourceRange();
5886 //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
5887 //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
5891 /// Structure recording the 'active' range of an integer-valued
5894 /// The number of bits active in the int.
5897 /// True if the int is known not to have negative values.
5900 IntRange(unsigned Width, bool NonNegative)
5901 : Width(Width), NonNegative(NonNegative)
5904 /// Returns the range of the bool type.
5905 static IntRange forBoolType() {
5906 return IntRange(1, true);
5909 /// Returns the range of an opaque value of the given integral type.
5910 static IntRange forValueOfType(ASTContext &C, QualType T) {
5911 return forValueOfCanonicalType(C,
5912 T->getCanonicalTypeInternal().getTypePtr());
5915 /// Returns the range of an opaque value of a canonical integral type.
5916 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
5917 assert(T->isCanonicalUnqualified());
5919 if (const VectorType *VT = dyn_cast<VectorType>(T))
5920 T = VT->getElementType().getTypePtr();
5921 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
5922 T = CT->getElementType().getTypePtr();
5923 if (const AtomicType *AT = dyn_cast<AtomicType>(T))
5924 T = AT->getValueType().getTypePtr();
5926 // For enum types, use the known bit width of the enumerators.
5927 if (const EnumType *ET = dyn_cast<EnumType>(T)) {
5928 EnumDecl *Enum = ET->getDecl();
5929 if (!Enum->isCompleteDefinition())
5930 return IntRange(C.getIntWidth(QualType(T, 0)), false);
5932 unsigned NumPositive = Enum->getNumPositiveBits();
5933 unsigned NumNegative = Enum->getNumNegativeBits();
5935 if (NumNegative == 0)
5936 return IntRange(NumPositive, true/*NonNegative*/);
5938 return IntRange(std::max(NumPositive + 1, NumNegative),
5939 false/*NonNegative*/);
5942 const BuiltinType *BT = cast<BuiltinType>(T);
5943 assert(BT->isInteger());
5945 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
5948 /// Returns the "target" range of a canonical integral type, i.e.
5949 /// the range of values expressible in the type.
5951 /// This matches forValueOfCanonicalType except that enums have the
5952 /// full range of their type, not the range of their enumerators.
5953 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
5954 assert(T->isCanonicalUnqualified());
5956 if (const VectorType *VT = dyn_cast<VectorType>(T))
5957 T = VT->getElementType().getTypePtr();
5958 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
5959 T = CT->getElementType().getTypePtr();
5960 if (const AtomicType *AT = dyn_cast<AtomicType>(T))
5961 T = AT->getValueType().getTypePtr();
5962 if (const EnumType *ET = dyn_cast<EnumType>(T))
5963 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
5965 const BuiltinType *BT = cast<BuiltinType>(T);
5966 assert(BT->isInteger());
5968 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
5971 /// Returns the supremum of two ranges: i.e. their conservative merge.
5972 static IntRange join(IntRange L, IntRange R) {
5973 return IntRange(std::max(L.Width, R.Width),
5974 L.NonNegative && R.NonNegative);
5977 /// Returns the infinum of two ranges: i.e. their aggressive merge.
5978 static IntRange meet(IntRange L, IntRange R) {
5979 return IntRange(std::min(L.Width, R.Width),
5980 L.NonNegative || R.NonNegative);
5984 static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
5985 unsigned MaxWidth) {
5986 if (value.isSigned() && value.isNegative())
5987 return IntRange(value.getMinSignedBits(), false);
5989 if (value.getBitWidth() > MaxWidth)
5990 value = value.trunc(MaxWidth);
5992 // isNonNegative() just checks the sign bit without considering
5994 return IntRange(value.getActiveBits(), true);
5997 static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
5998 unsigned MaxWidth) {
6000 return GetValueRange(C, result.getInt(), MaxWidth);
6002 if (result.isVector()) {
6003 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
6004 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
6005 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
6006 R = IntRange::join(R, El);
6011 if (result.isComplexInt()) {
6012 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
6013 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
6014 return IntRange::join(R, I);
6017 // This can happen with lossless casts to intptr_t of "based" lvalues.
6018 // Assume it might use arbitrary bits.
6019 // FIXME: The only reason we need to pass the type in here is to get
6020 // the sign right on this one case. It would be nice if APValue
6022 assert(result.isLValue() || result.isAddrLabelDiff());
6023 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
6026 static QualType GetExprType(Expr *E) {
6027 QualType Ty = E->getType();
6028 if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>())
6029 Ty = AtomicRHS->getValueType();
6033 /// Pseudo-evaluate the given integer expression, estimating the
6034 /// range of values it might take.
6036 /// \param MaxWidth - the width to which the value will be truncated
6037 static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) {
6038 E = E->IgnoreParens();
6040 // Try a full evaluation first.
6041 Expr::EvalResult result;
6042 if (E->EvaluateAsRValue(result, C))
6043 return GetValueRange(C, result.Val, GetExprType(E), MaxWidth);
6045 // I think we only want to look through implicit casts here; if the
6046 // user has an explicit widening cast, we should treat the value as
6047 // being of the new, wider type.
6048 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
6049 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
6050 return GetExprRange(C, CE->getSubExpr(), MaxWidth);
6052 IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE));
6054 bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast);
6056 // Assume that non-integer casts can span the full range of the type.
6058 return OutputTypeRange;
6061 = GetExprRange(C, CE->getSubExpr(),
6062 std::min(MaxWidth, OutputTypeRange.Width));
6064 // Bail out if the subexpr's range is as wide as the cast type.
6065 if (SubRange.Width >= OutputTypeRange.Width)
6066 return OutputTypeRange;
6068 // Otherwise, we take the smaller width, and we're non-negative if
6069 // either the output type or the subexpr is.
6070 return IntRange(SubRange.Width,
6071 SubRange.NonNegative || OutputTypeRange.NonNegative);
6074 if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
6075 // If we can fold the condition, just take that operand.
6077 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
6078 return GetExprRange(C, CondResult ? CO->getTrueExpr()
6079 : CO->getFalseExpr(),
6082 // Otherwise, conservatively merge.
6083 IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
6084 IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
6085 return IntRange::join(L, R);
6088 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
6089 switch (BO->getOpcode()) {
6091 // Boolean-valued operations are single-bit and positive.
6100 return IntRange::forBoolType();
6102 // The type of the assignments is the type of the LHS, so the RHS
6103 // is not necessarily the same type.
6112 return IntRange::forValueOfType(C, GetExprType(E));
6114 // Simple assignments just pass through the RHS, which will have
6115 // been coerced to the LHS type.
6118 return GetExprRange(C, BO->getRHS(), MaxWidth);
6120 // Operations with opaque sources are black-listed.
6123 return IntRange::forValueOfType(C, GetExprType(E));
6125 // Bitwise-and uses the *infinum* of the two source ranges.
6128 return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
6129 GetExprRange(C, BO->getRHS(), MaxWidth));
6131 // Left shift gets black-listed based on a judgement call.
6133 // ...except that we want to treat '1 << (blah)' as logically
6134 // positive. It's an important idiom.
6135 if (IntegerLiteral *I
6136 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
6137 if (I->getValue() == 1) {
6138 IntRange R = IntRange::forValueOfType(C, GetExprType(E));
6139 return IntRange(R.Width, /*NonNegative*/ true);
6145 return IntRange::forValueOfType(C, GetExprType(E));
6147 // Right shift by a constant can narrow its left argument.
6149 case BO_ShrAssign: {
6150 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
6152 // If the shift amount is a positive constant, drop the width by
6155 if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
6156 shift.isNonNegative()) {
6157 unsigned zext = shift.getZExtValue();
6158 if (zext >= L.Width)
6159 L.Width = (L.NonNegative ? 0 : 1);
6167 // Comma acts as its right operand.
6169 return GetExprRange(C, BO->getRHS(), MaxWidth);
6171 // Black-list pointer subtractions.
6173 if (BO->getLHS()->getType()->isPointerType())
6174 return IntRange::forValueOfType(C, GetExprType(E));
6177 // The width of a division result is mostly determined by the size
6180 // Don't 'pre-truncate' the operands.
6181 unsigned opWidth = C.getIntWidth(GetExprType(E));
6182 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
6184 // If the divisor is constant, use that.
6185 llvm::APSInt divisor;
6186 if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
6187 unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
6188 if (log2 >= L.Width)
6189 L.Width = (L.NonNegative ? 0 : 1);
6191 L.Width = std::min(L.Width - log2, MaxWidth);
6195 // Otherwise, just use the LHS's width.
6196 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
6197 return IntRange(L.Width, L.NonNegative && R.NonNegative);
6200 // The result of a remainder can't be larger than the result of
6203 // Don't 'pre-truncate' the operands.
6204 unsigned opWidth = C.getIntWidth(GetExprType(E));
6205 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
6206 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
6208 IntRange meet = IntRange::meet(L, R);
6209 meet.Width = std::min(meet.Width, MaxWidth);
6213 // The default behavior is okay for these.
6221 // The default case is to treat the operation as if it were closed
6222 // on the narrowest type that encompasses both operands.
6223 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
6224 IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
6225 return IntRange::join(L, R);
6228 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
6229 switch (UO->getOpcode()) {
6230 // Boolean-valued operations are white-listed.
6232 return IntRange::forBoolType();
6234 // Operations with opaque sources are black-listed.
6236 case UO_AddrOf: // should be impossible
6237 return IntRange::forValueOfType(C, GetExprType(E));
6240 return GetExprRange(C, UO->getSubExpr(), MaxWidth);
6244 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
6245 return GetExprRange(C, OVE->getSourceExpr(), MaxWidth);
6247 if (FieldDecl *BitField = E->getSourceBitField())
6248 return IntRange(BitField->getBitWidthValue(C),
6249 BitField->getType()->isUnsignedIntegerOrEnumerationType());
6251 return IntRange::forValueOfType(C, GetExprType(E));
6254 static IntRange GetExprRange(ASTContext &C, Expr *E) {
6255 return GetExprRange(C, E, C.getIntWidth(GetExprType(E)));
6258 /// Checks whether the given value, which currently has the given
6259 /// source semantics, has the same value when coerced through the
6260 /// target semantics.
6261 static bool IsSameFloatAfterCast(const llvm::APFloat &value,
6262 const llvm::fltSemantics &Src,
6263 const llvm::fltSemantics &Tgt) {
6264 llvm::APFloat truncated = value;
6267 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
6268 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
6270 return truncated.bitwiseIsEqual(value);
6273 /// Checks whether the given value, which currently has the given
6274 /// source semantics, has the same value when coerced through the
6275 /// target semantics.
6277 /// The value might be a vector of floats (or a complex number).
6278 static bool IsSameFloatAfterCast(const APValue &value,
6279 const llvm::fltSemantics &Src,
6280 const llvm::fltSemantics &Tgt) {
6281 if (value.isFloat())
6282 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
6284 if (value.isVector()) {
6285 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
6286 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
6291 assert(value.isComplexFloat());
6292 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
6293 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
6296 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
6298 static bool IsZero(Sema &S, Expr *E) {
6299 // Suppress cases where we are comparing against an enum constant.
6300 if (const DeclRefExpr *DR =
6301 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
6302 if (isa<EnumConstantDecl>(DR->getDecl()))
6305 // Suppress cases where the '0' value is expanded from a macro.
6306 if (E->getLocStart().isMacroID())
6310 return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
6313 static bool HasEnumType(Expr *E) {
6314 // Strip off implicit integral promotions.
6315 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
6316 if (ICE->getCastKind() != CK_IntegralCast &&
6317 ICE->getCastKind() != CK_NoOp)
6319 E = ICE->getSubExpr();
6322 return E->getType()->isEnumeralType();
6325 static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
6326 // Disable warning in template instantiations.
6327 if (!S.ActiveTemplateInstantiations.empty())
6330 BinaryOperatorKind op = E->getOpcode();
6331 if (E->isValueDependent())
6334 if (op == BO_LT && IsZero(S, E->getRHS())) {
6335 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
6336 << "< 0" << "false" << HasEnumType(E->getLHS())
6337 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
6338 } else if (op == BO_GE && IsZero(S, E->getRHS())) {
6339 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
6340 << ">= 0" << "true" << HasEnumType(E->getLHS())
6341 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
6342 } else if (op == BO_GT && IsZero(S, E->getLHS())) {
6343 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
6344 << "0 >" << "false" << HasEnumType(E->getRHS())
6345 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
6346 } else if (op == BO_LE && IsZero(S, E->getLHS())) {
6347 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
6348 << "0 <=" << "true" << HasEnumType(E->getRHS())
6349 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
6353 static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
6354 Expr *Constant, Expr *Other,
6357 // Disable warning in template instantiations.
6358 if (!S.ActiveTemplateInstantiations.empty())
6361 // TODO: Investigate using GetExprRange() to get tighter bounds
6362 // on the bit ranges.
6363 QualType OtherT = Other->getType();
6364 if (const auto *AT = OtherT->getAs<AtomicType>())
6365 OtherT = AT->getValueType();
6366 IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT);
6367 unsigned OtherWidth = OtherRange.Width;
6369 bool OtherIsBooleanType = Other->isKnownToHaveBooleanValue();
6371 // 0 values are handled later by CheckTrivialUnsignedComparison().
6372 if ((Value == 0) && (!OtherIsBooleanType))
6375 BinaryOperatorKind op = E->getOpcode();
6378 // Used for diagnostic printout.
6380 LiteralConstant = 0,
6383 } LiteralOrBoolConstant = LiteralConstant;
6385 if (!OtherIsBooleanType) {
6386 QualType ConstantT = Constant->getType();
6387 QualType CommonT = E->getLHS()->getType();
6389 if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT))
6391 assert((OtherT->isIntegerType() && ConstantT->isIntegerType()) &&
6392 "comparison with non-integer type");
6394 bool ConstantSigned = ConstantT->isSignedIntegerType();
6395 bool CommonSigned = CommonT->isSignedIntegerType();
6397 bool EqualityOnly = false;
6400 // The common type is signed, therefore no signed to unsigned conversion.
6401 if (!OtherRange.NonNegative) {
6402 // Check that the constant is representable in type OtherT.
6403 if (ConstantSigned) {
6404 if (OtherWidth >= Value.getMinSignedBits())
6406 } else { // !ConstantSigned
6407 if (OtherWidth >= Value.getActiveBits() + 1)
6410 } else { // !OtherSigned
6411 // Check that the constant is representable in type OtherT.
6412 // Negative values are out of range.
6413 if (ConstantSigned) {
6414 if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits())
6416 } else { // !ConstantSigned
6417 if (OtherWidth >= Value.getActiveBits())
6421 } else { // !CommonSigned
6422 if (OtherRange.NonNegative) {
6423 if (OtherWidth >= Value.getActiveBits())
6425 } else { // OtherSigned
6426 assert(!ConstantSigned &&
6427 "Two signed types converted to unsigned types.");
6428 // Check to see if the constant is representable in OtherT.
6429 if (OtherWidth > Value.getActiveBits())
6431 // Check to see if the constant is equivalent to a negative value
6433 if (S.Context.getIntWidth(ConstantT) ==
6434 S.Context.getIntWidth(CommonT) &&
6435 Value.isNegative() && Value.getMinSignedBits() <= OtherWidth)
6437 // The constant value rests between values that OtherT can represent
6438 // after conversion. Relational comparison still works, but equality
6439 // comparisons will be tautological.
6440 EqualityOnly = true;
6444 bool PositiveConstant = !ConstantSigned || Value.isNonNegative();
6446 if (op == BO_EQ || op == BO_NE) {
6447 IsTrue = op == BO_NE;
6448 } else if (EqualityOnly) {
6450 } else if (RhsConstant) {
6451 if (op == BO_GT || op == BO_GE)
6452 IsTrue = !PositiveConstant;
6453 else // op == BO_LT || op == BO_LE
6454 IsTrue = PositiveConstant;
6456 if (op == BO_LT || op == BO_LE)
6457 IsTrue = !PositiveConstant;
6458 else // op == BO_GT || op == BO_GE
6459 IsTrue = PositiveConstant;
6462 // Other isKnownToHaveBooleanValue
6463 enum CompareBoolWithConstantResult { AFals, ATrue, Unkwn };
6464 enum ConstantValue { LT_Zero, Zero, One, GT_One, SizeOfConstVal };
6465 enum ConstantSide { Lhs, Rhs, SizeOfConstSides };
6467 static const struct LinkedConditions {
6468 CompareBoolWithConstantResult BO_LT_OP[SizeOfConstSides][SizeOfConstVal];
6469 CompareBoolWithConstantResult BO_GT_OP[SizeOfConstSides][SizeOfConstVal];
6470 CompareBoolWithConstantResult BO_LE_OP[SizeOfConstSides][SizeOfConstVal];
6471 CompareBoolWithConstantResult BO_GE_OP[SizeOfConstSides][SizeOfConstVal];
6472 CompareBoolWithConstantResult BO_EQ_OP[SizeOfConstSides][SizeOfConstVal];
6473 CompareBoolWithConstantResult BO_NE_OP[SizeOfConstSides][SizeOfConstVal];
6476 // Constant on LHS. | Constant on RHS. |
6477 // LT_Zero| Zero | One |GT_One| LT_Zero| Zero | One |GT_One|
6478 { { ATrue, Unkwn, AFals, AFals }, { AFals, AFals, Unkwn, ATrue } },
6479 { { AFals, AFals, Unkwn, ATrue }, { ATrue, Unkwn, AFals, AFals } },
6480 { { ATrue, ATrue, Unkwn, AFals }, { AFals, Unkwn, ATrue, ATrue } },
6481 { { AFals, Unkwn, ATrue, ATrue }, { ATrue, ATrue, Unkwn, AFals } },
6482 { { AFals, Unkwn, Unkwn, AFals }, { AFals, Unkwn, Unkwn, AFals } },
6483 { { ATrue, Unkwn, Unkwn, ATrue }, { ATrue, Unkwn, Unkwn, ATrue } }
6486 bool ConstantIsBoolLiteral = isa<CXXBoolLiteralExpr>(Constant);
6488 enum ConstantValue ConstVal = Zero;
6489 if (Value.isUnsigned() || Value.isNonNegative()) {
6491 LiteralOrBoolConstant =
6492 ConstantIsBoolLiteral ? CXXBoolLiteralFalse : LiteralConstant;
6494 } else if (Value == 1) {
6495 LiteralOrBoolConstant =
6496 ConstantIsBoolLiteral ? CXXBoolLiteralTrue : LiteralConstant;
6499 LiteralOrBoolConstant = LiteralConstant;
6506 CompareBoolWithConstantResult CmpRes;
6510 CmpRes = TruthTable.BO_LT_OP[RhsConstant][ConstVal];
6513 CmpRes = TruthTable.BO_GT_OP[RhsConstant][ConstVal];
6516 CmpRes = TruthTable.BO_LE_OP[RhsConstant][ConstVal];
6519 CmpRes = TruthTable.BO_GE_OP[RhsConstant][ConstVal];
6522 CmpRes = TruthTable.BO_EQ_OP[RhsConstant][ConstVal];
6525 CmpRes = TruthTable.BO_NE_OP[RhsConstant][ConstVal];
6532 if (CmpRes == AFals) {
6534 } else if (CmpRes == ATrue) {
6541 // If this is a comparison to an enum constant, include that
6542 // constant in the diagnostic.
6543 const EnumConstantDecl *ED = nullptr;
6544 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
6545 ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
6547 SmallString<64> PrettySourceValue;
6548 llvm::raw_svector_ostream OS(PrettySourceValue);
6550 OS << '\'' << *ED << "' (" << Value << ")";
6554 S.DiagRuntimeBehavior(
6555 E->getOperatorLoc(), E,
6556 S.PDiag(diag::warn_out_of_range_compare)
6557 << OS.str() << LiteralOrBoolConstant
6558 << OtherT << (OtherIsBooleanType && !OtherT->isBooleanType()) << IsTrue
6559 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange());
6562 /// Analyze the operands of the given comparison. Implements the
6563 /// fallback case from AnalyzeComparison.
6564 static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
6565 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
6566 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
6569 /// \brief Implements -Wsign-compare.
6571 /// \param E the binary operator to check for warnings
6572 static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
6573 // The type the comparison is being performed in.
6574 QualType T = E->getLHS()->getType();
6576 // Only analyze comparison operators where both sides have been converted to
6578 if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()))
6579 return AnalyzeImpConvsInComparison(S, E);
6581 // Don't analyze value-dependent comparisons directly.
6582 if (E->isValueDependent())
6583 return AnalyzeImpConvsInComparison(S, E);
6585 Expr *LHS = E->getLHS()->IgnoreParenImpCasts();
6586 Expr *RHS = E->getRHS()->IgnoreParenImpCasts();
6588 bool IsComparisonConstant = false;
6590 // Check whether an integer constant comparison results in a value
6591 // of 'true' or 'false'.
6592 if (T->isIntegralType(S.Context)) {
6593 llvm::APSInt RHSValue;
6594 bool IsRHSIntegralLiteral =
6595 RHS->isIntegerConstantExpr(RHSValue, S.Context);
6596 llvm::APSInt LHSValue;
6597 bool IsLHSIntegralLiteral =
6598 LHS->isIntegerConstantExpr(LHSValue, S.Context);
6599 if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral)
6600 DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true);
6601 else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral)
6602 DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false);
6604 IsComparisonConstant =
6605 (IsRHSIntegralLiteral && IsLHSIntegralLiteral);
6606 } else if (!T->hasUnsignedIntegerRepresentation())
6607 IsComparisonConstant = E->isIntegerConstantExpr(S.Context);
6609 // We don't do anything special if this isn't an unsigned integral
6610 // comparison: we're only interested in integral comparisons, and
6611 // signed comparisons only happen in cases we don't care to warn about.
6613 // We also don't care about value-dependent expressions or expressions
6614 // whose result is a constant.
6615 if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant)
6616 return AnalyzeImpConvsInComparison(S, E);
6618 // Check to see if one of the (unmodified) operands is of different
6620 Expr *signedOperand, *unsignedOperand;
6621 if (LHS->getType()->hasSignedIntegerRepresentation()) {
6622 assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
6623 "unsigned comparison between two signed integer expressions?");
6624 signedOperand = LHS;
6625 unsignedOperand = RHS;
6626 } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
6627 signedOperand = RHS;
6628 unsignedOperand = LHS;
6630 CheckTrivialUnsignedComparison(S, E);
6631 return AnalyzeImpConvsInComparison(S, E);
6634 // Otherwise, calculate the effective range of the signed operand.
6635 IntRange signedRange = GetExprRange(S.Context, signedOperand);
6637 // Go ahead and analyze implicit conversions in the operands. Note
6638 // that we skip the implicit conversions on both sides.
6639 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
6640 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
6642 // If the signed range is non-negative, -Wsign-compare won't fire,
6643 // but we should still check for comparisons which are always true
6645 if (signedRange.NonNegative)
6646 return CheckTrivialUnsignedComparison(S, E);
6648 // For (in)equality comparisons, if the unsigned operand is a
6649 // constant which cannot collide with a overflowed signed operand,
6650 // then reinterpreting the signed operand as unsigned will not
6651 // change the result of the comparison.
6652 if (E->isEqualityOp()) {
6653 unsigned comparisonWidth = S.Context.getIntWidth(T);
6654 IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
6656 // We should never be unable to prove that the unsigned operand is
6658 assert(unsignedRange.NonNegative && "unsigned range includes negative?");
6660 if (unsignedRange.Width < comparisonWidth)
6664 S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
6665 S.PDiag(diag::warn_mixed_sign_comparison)
6666 << LHS->getType() << RHS->getType()
6667 << LHS->getSourceRange() << RHS->getSourceRange());
6670 /// Analyzes an attempt to assign the given value to a bitfield.
6672 /// Returns true if there was something fishy about the attempt.
6673 static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
6674 SourceLocation InitLoc) {
6675 assert(Bitfield->isBitField());
6676 if (Bitfield->isInvalidDecl())
6679 // White-list bool bitfields.
6680 if (Bitfield->getType()->isBooleanType())
6683 // Ignore value- or type-dependent expressions.
6684 if (Bitfield->getBitWidth()->isValueDependent() ||
6685 Bitfield->getBitWidth()->isTypeDependent() ||
6686 Init->isValueDependent() ||
6687 Init->isTypeDependent())
6690 Expr *OriginalInit = Init->IgnoreParenImpCasts();
6693 if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects))
6696 unsigned OriginalWidth = Value.getBitWidth();
6697 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
6699 if (OriginalWidth <= FieldWidth)
6702 // Compute the value which the bitfield will contain.
6703 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
6704 TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType());
6706 // Check whether the stored value is equal to the original value.
6707 TruncatedValue = TruncatedValue.extend(OriginalWidth);
6708 if (llvm::APSInt::isSameValue(Value, TruncatedValue))
6711 // Special-case bitfields of width 1: booleans are naturally 0/1, and
6712 // therefore don't strictly fit into a signed bitfield of width 1.
6713 if (FieldWidth == 1 && Value == 1)
6716 std::string PrettyValue = Value.toString(10);
6717 std::string PrettyTrunc = TruncatedValue.toString(10);
6719 S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
6720 << PrettyValue << PrettyTrunc << OriginalInit->getType()
6721 << Init->getSourceRange();
6726 /// Analyze the given simple or compound assignment for warning-worthy
6728 static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
6729 // Just recurse on the LHS.
6730 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
6732 // We want to recurse on the RHS as normal unless we're assigning to
6734 if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) {
6735 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
6736 E->getOperatorLoc())) {
6737 // Recurse, ignoring any implicit conversions on the RHS.
6738 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
6739 E->getOperatorLoc());
6743 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
6746 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
6747 static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
6748 SourceLocation CContext, unsigned diag,
6749 bool pruneControlFlow = false) {
6750 if (pruneControlFlow) {
6751 S.DiagRuntimeBehavior(E->getExprLoc(), E,
6753 << SourceType << T << E->getSourceRange()
6754 << SourceRange(CContext));
6757 S.Diag(E->getExprLoc(), diag)
6758 << SourceType << T << E->getSourceRange() << SourceRange(CContext);
6761 /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
6762 static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
6763 SourceLocation CContext, unsigned diag,
6764 bool pruneControlFlow = false) {
6765 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
6768 /// Diagnose an implicit cast from a literal expression. Does not warn when the
6769 /// cast wouldn't lose information.
6770 void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
6771 SourceLocation CContext) {
6772 // Try to convert the literal exactly to an integer. If we can, don't warn.
6773 bool isExact = false;
6774 const llvm::APFloat &Value = FL->getValue();
6775 llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
6776 T->hasUnsignedIntegerRepresentation());
6777 if (Value.convertToInteger(IntegerValue,
6778 llvm::APFloat::rmTowardZero, &isExact)
6779 == llvm::APFloat::opOK && isExact)
6782 // FIXME: Force the precision of the source value down so we don't print
6783 // digits which are usually useless (we don't really care here if we
6784 // truncate a digit by accident in edge cases). Ideally, APFloat::toString
6785 // would automatically print the shortest representation, but it's a bit
6786 // tricky to implement.
6787 SmallString<16> PrettySourceValue;
6788 unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics());
6789 precision = (precision * 59 + 195) / 196;
6790 Value.toString(PrettySourceValue, precision);
6792 SmallString<16> PrettyTargetValue;
6793 if (T->isSpecificBuiltinType(BuiltinType::Bool))
6794 PrettyTargetValue = IntegerValue == 0 ? "false" : "true";
6796 IntegerValue.toString(PrettyTargetValue);
6798 S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer)
6799 << FL->getType() << T.getUnqualifiedType() << PrettySourceValue
6800 << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext);
6803 std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
6804 if (!Range.Width) return "0";
6806 llvm::APSInt ValueInRange = Value;
6807 ValueInRange.setIsSigned(!Range.NonNegative);
6808 ValueInRange = ValueInRange.trunc(Range.Width);
6809 return ValueInRange.toString(10);
6812 static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) {
6813 if (!isa<ImplicitCastExpr>(Ex))
6816 Expr *InnerE = Ex->IgnoreParenImpCasts();
6817 const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr();
6818 const Type *Source =
6819 S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
6820 if (Target->isDependentType())
6823 const BuiltinType *FloatCandidateBT =
6824 dyn_cast<BuiltinType>(ToBool ? Source : Target);
6825 const Type *BoolCandidateType = ToBool ? Target : Source;
6827 return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) &&
6828 FloatCandidateBT && (FloatCandidateBT->isFloatingPoint()));
6831 void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall,
6832 SourceLocation CC) {
6833 unsigned NumArgs = TheCall->getNumArgs();
6834 for (unsigned i = 0; i < NumArgs; ++i) {
6835 Expr *CurrA = TheCall->getArg(i);
6836 if (!IsImplicitBoolFloatConversion(S, CurrA, true))
6839 bool IsSwapped = ((i > 0) &&
6840 IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false));
6841 IsSwapped |= ((i < (NumArgs - 1)) &&
6842 IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false));
6844 // Warn on this floating-point to bool conversion.
6845 DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(),
6846 CurrA->getType(), CC,
6847 diag::warn_impcast_floating_point_to_bool);
6852 static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
6853 SourceLocation CC) {
6854 if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer,
6858 // Check for NULL (GNUNull) or nullptr (CXX11_nullptr).
6859 const Expr::NullPointerConstantKind NullKind =
6860 E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull);
6861 if (NullKind != Expr::NPCK_GNUNull && NullKind != Expr::NPCK_CXX11_nullptr)
6864 // Return if target type is a safe conversion.
6865 if (T->isAnyPointerType() || T->isBlockPointerType() ||
6866 T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType())
6869 SourceLocation Loc = E->getSourceRange().getBegin();
6871 // __null is usually wrapped in a macro. Go up a macro if that is the case.
6872 if (NullKind == Expr::NPCK_GNUNull) {
6873 if (Loc.isMacroID())
6874 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
6877 // Only warn if the null and context location are in the same macro expansion.
6878 if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC))
6881 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
6882 << (NullKind == Expr::NPCK_CXX11_nullptr) << T << clang::SourceRange(CC)
6883 << FixItHint::CreateReplacement(Loc,
6884 S.getFixItZeroLiteralForType(T, Loc));
6887 static void checkObjCArrayLiteral(Sema &S, QualType TargetType,
6888 ObjCArrayLiteral *ArrayLiteral);
6889 static void checkObjCDictionaryLiteral(Sema &S, QualType TargetType,
6890 ObjCDictionaryLiteral *DictionaryLiteral);
6892 /// Check a single element within a collection literal against the
6893 /// target element type.
6894 static void checkObjCCollectionLiteralElement(Sema &S,
6895 QualType TargetElementType,
6897 unsigned ElementKind) {
6898 // Skip a bitcast to 'id' or qualified 'id'.
6899 if (auto ICE = dyn_cast<ImplicitCastExpr>(Element)) {
6900 if (ICE->getCastKind() == CK_BitCast &&
6901 ICE->getSubExpr()->getType()->getAs<ObjCObjectPointerType>())
6902 Element = ICE->getSubExpr();
6905 QualType ElementType = Element->getType();
6906 ExprResult ElementResult(Element);
6907 if (ElementType->getAs<ObjCObjectPointerType>() &&
6908 S.CheckSingleAssignmentConstraints(TargetElementType,
6911 != Sema::Compatible) {
6912 S.Diag(Element->getLocStart(),
6913 diag::warn_objc_collection_literal_element)
6914 << ElementType << ElementKind << TargetElementType
6915 << Element->getSourceRange();
6918 if (auto ArrayLiteral = dyn_cast<ObjCArrayLiteral>(Element))
6919 checkObjCArrayLiteral(S, TargetElementType, ArrayLiteral);
6920 else if (auto DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(Element))
6921 checkObjCDictionaryLiteral(S, TargetElementType, DictionaryLiteral);
6924 /// Check an Objective-C array literal being converted to the given
6926 static void checkObjCArrayLiteral(Sema &S, QualType TargetType,
6927 ObjCArrayLiteral *ArrayLiteral) {
6931 const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>();
6935 if (TargetObjCPtr->isUnspecialized() ||
6936 TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl()
6937 != S.NSArrayDecl->getCanonicalDecl())
6940 auto TypeArgs = TargetObjCPtr->getTypeArgs();
6941 if (TypeArgs.size() != 1)
6944 QualType TargetElementType = TypeArgs[0];
6945 for (unsigned I = 0, N = ArrayLiteral->getNumElements(); I != N; ++I) {
6946 checkObjCCollectionLiteralElement(S, TargetElementType,
6947 ArrayLiteral->getElement(I),
6952 /// Check an Objective-C dictionary literal being converted to the given
6954 static void checkObjCDictionaryLiteral(
6955 Sema &S, QualType TargetType,
6956 ObjCDictionaryLiteral *DictionaryLiteral) {
6957 if (!S.NSDictionaryDecl)
6960 const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>();
6964 if (TargetObjCPtr->isUnspecialized() ||
6965 TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl()
6966 != S.NSDictionaryDecl->getCanonicalDecl())
6969 auto TypeArgs = TargetObjCPtr->getTypeArgs();
6970 if (TypeArgs.size() != 2)
6973 QualType TargetKeyType = TypeArgs[0];
6974 QualType TargetObjectType = TypeArgs[1];
6975 for (unsigned I = 0, N = DictionaryLiteral->getNumElements(); I != N; ++I) {
6976 auto Element = DictionaryLiteral->getKeyValueElement(I);
6977 checkObjCCollectionLiteralElement(S, TargetKeyType, Element.Key, 1);
6978 checkObjCCollectionLiteralElement(S, TargetObjectType, Element.Value, 2);
6982 void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
6983 SourceLocation CC, bool *ICContext = nullptr) {
6984 if (E->isTypeDependent() || E->isValueDependent()) return;
6986 const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
6987 const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
6988 if (Source == Target) return;
6989 if (Target->isDependentType()) return;
6991 // If the conversion context location is invalid don't complain. We also
6992 // don't want to emit a warning if the issue occurs from the expansion of
6993 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
6994 // delay this check as long as possible. Once we detect we are in that
6995 // scenario, we just return.
6999 // Diagnose implicit casts to bool.
7000 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
7001 if (isa<StringLiteral>(E))
7002 // Warn on string literal to bool. Checks for string literals in logical
7003 // and expressions, for instance, assert(0 && "error here"), are
7004 // prevented by a check in AnalyzeImplicitConversions().
7005 return DiagnoseImpCast(S, E, T, CC,
7006 diag::warn_impcast_string_literal_to_bool);
7007 if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) ||
7008 isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) {
7009 // This covers the literal expressions that evaluate to Objective-C
7011 return DiagnoseImpCast(S, E, T, CC,
7012 diag::warn_impcast_objective_c_literal_to_bool);
7014 if (Source->isPointerType() || Source->canDecayToPointerType()) {
7015 // Warn on pointer to bool conversion that is always true.
7016 S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false,
7021 // Check implicit casts from Objective-C collection literals to specialized
7022 // collection types, e.g., NSArray<NSString *> *.
7023 if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E))
7024 checkObjCArrayLiteral(S, QualType(Target, 0), ArrayLiteral);
7025 else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E))
7026 checkObjCDictionaryLiteral(S, QualType(Target, 0), DictionaryLiteral);
7028 // Strip vector types.
7029 if (isa<VectorType>(Source)) {
7030 if (!isa<VectorType>(Target)) {
7031 if (S.SourceMgr.isInSystemMacro(CC))
7033 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
7036 // If the vector cast is cast between two vectors of the same size, it is
7037 // a bitcast, not a conversion.
7038 if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
7041 Source = cast<VectorType>(Source)->getElementType().getTypePtr();
7042 Target = cast<VectorType>(Target)->getElementType().getTypePtr();
7044 if (auto VecTy = dyn_cast<VectorType>(Target))
7045 Target = VecTy->getElementType().getTypePtr();
7047 // Strip complex types.
7048 if (isa<ComplexType>(Source)) {
7049 if (!isa<ComplexType>(Target)) {
7050 if (S.SourceMgr.isInSystemMacro(CC))
7053 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar);
7056 Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
7057 Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
7060 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
7061 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
7063 // If the source is floating point...
7064 if (SourceBT && SourceBT->isFloatingPoint()) {
7065 // ...and the target is floating point...
7066 if (TargetBT && TargetBT->isFloatingPoint()) {
7067 // ...then warn if we're dropping FP rank.
7069 // Builtin FP kinds are ordered by increasing FP rank.
7070 if (SourceBT->getKind() > TargetBT->getKind()) {
7071 // Don't warn about float constants that are precisely
7072 // representable in the target type.
7073 Expr::EvalResult result;
7074 if (E->EvaluateAsRValue(result, S.Context)) {
7075 // Value might be a float, a float vector, or a float complex.
7076 if (IsSameFloatAfterCast(result.Val,
7077 S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
7078 S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
7082 if (S.SourceMgr.isInSystemMacro(CC))
7085 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
7090 // If the target is integral, always warn.
7091 if (TargetBT && TargetBT->isInteger()) {
7092 if (S.SourceMgr.isInSystemMacro(CC))
7095 Expr *InnerE = E->IgnoreParenImpCasts();
7096 // We also want to warn on, e.g., "int i = -1.234"
7097 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
7098 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
7099 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
7101 if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) {
7102 DiagnoseFloatingLiteralImpCast(S, FL, T, CC);
7104 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
7108 // If the target is bool, warn if expr is a function or method call.
7109 if (Target->isSpecificBuiltinType(BuiltinType::Bool) &&
7111 // Check last argument of function call to see if it is an
7112 // implicit cast from a type matching the type the result
7113 // is being cast to.
7114 CallExpr *CEx = cast<CallExpr>(E);
7115 unsigned NumArgs = CEx->getNumArgs();
7117 Expr *LastA = CEx->getArg(NumArgs - 1);
7118 Expr *InnerE = LastA->IgnoreParenImpCasts();
7119 const Type *InnerType =
7120 S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
7121 if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) {
7122 // Warn on this floating-point to bool conversion
7123 DiagnoseImpCast(S, E, T, CC,
7124 diag::warn_impcast_floating_point_to_bool);
7131 DiagnoseNullConversion(S, E, T, CC);
7133 if (!Source->isIntegerType() || !Target->isIntegerType())
7136 // TODO: remove this early return once the false positives for constant->bool
7137 // in templates, macros, etc, are reduced or removed.
7138 if (Target->isSpecificBuiltinType(BuiltinType::Bool))
7141 IntRange SourceRange = GetExprRange(S.Context, E);
7142 IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
7144 if (SourceRange.Width > TargetRange.Width) {
7145 // If the source is a constant, use a default-on diagnostic.
7146 // TODO: this should happen for bitfield stores, too.
7147 llvm::APSInt Value(32);
7148 if (E->isIntegerConstantExpr(Value, S.Context)) {
7149 if (S.SourceMgr.isInSystemMacro(CC))
7152 std::string PrettySourceValue = Value.toString(10);
7153 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
7155 S.DiagRuntimeBehavior(E->getExprLoc(), E,
7156 S.PDiag(diag::warn_impcast_integer_precision_constant)
7157 << PrettySourceValue << PrettyTargetValue
7158 << E->getType() << T << E->getSourceRange()
7159 << clang::SourceRange(CC));
7163 // People want to build with -Wshorten-64-to-32 and not -Wconversion.
7164 if (S.SourceMgr.isInSystemMacro(CC))
7167 if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64)
7168 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32,
7169 /* pruneControlFlow */ true);
7170 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
7173 if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
7174 (!TargetRange.NonNegative && SourceRange.NonNegative &&
7175 SourceRange.Width == TargetRange.Width)) {
7177 if (S.SourceMgr.isInSystemMacro(CC))
7180 unsigned DiagID = diag::warn_impcast_integer_sign;
7182 // Traditionally, gcc has warned about this under -Wsign-compare.
7183 // We also want to warn about it in -Wconversion.
7184 // So if -Wconversion is off, use a completely identical diagnostic
7185 // in the sign-compare group.
7186 // The conditional-checking code will
7188 DiagID = diag::warn_impcast_integer_sign_conditional;
7192 return DiagnoseImpCast(S, E, T, CC, DiagID);
7195 // Diagnose conversions between different enumeration types.
7196 // In C, we pretend that the type of an EnumConstantDecl is its enumeration
7197 // type, to give us better diagnostics.
7198 QualType SourceType = E->getType();
7199 if (!S.getLangOpts().CPlusPlus) {
7200 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
7201 if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
7202 EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
7203 SourceType = S.Context.getTypeDeclType(Enum);
7204 Source = S.Context.getCanonicalType(SourceType).getTypePtr();
7208 if (const EnumType *SourceEnum = Source->getAs<EnumType>())
7209 if (const EnumType *TargetEnum = Target->getAs<EnumType>())
7210 if (SourceEnum->getDecl()->hasNameForLinkage() &&
7211 TargetEnum->getDecl()->hasNameForLinkage() &&
7212 SourceEnum != TargetEnum) {
7213 if (S.SourceMgr.isInSystemMacro(CC))
7216 return DiagnoseImpCast(S, E, SourceType, T, CC,
7217 diag::warn_impcast_different_enum_types);
7223 void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
7224 SourceLocation CC, QualType T);
7226 void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
7227 SourceLocation CC, bool &ICContext) {
7228 E = E->IgnoreParenImpCasts();
7230 if (isa<ConditionalOperator>(E))
7231 return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T);
7233 AnalyzeImplicitConversions(S, E, CC);
7234 if (E->getType() != T)
7235 return CheckImplicitConversion(S, E, T, CC, &ICContext);
7239 void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
7240 SourceLocation CC, QualType T) {
7241 AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc());
7243 bool Suspicious = false;
7244 CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious);
7245 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
7247 // If -Wconversion would have warned about either of the candidates
7248 // for a signedness conversion to the context type...
7249 if (!Suspicious) return;
7251 // ...but it's currently ignored...
7252 if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC))
7255 // ...then check whether it would have warned about either of the
7256 // candidates for a signedness conversion to the condition type.
7257 if (E->getType() == T) return;
7260 CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
7261 E->getType(), CC, &Suspicious);
7263 CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
7264 E->getType(), CC, &Suspicious);
7267 /// CheckBoolLikeConversion - Check conversion of given expression to boolean.
7268 /// Input argument E is a logical expression.
7269 static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) {
7270 if (S.getLangOpts().Bool)
7272 CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC);
7275 /// AnalyzeImplicitConversions - Find and report any interesting
7276 /// implicit conversions in the given expression. There are a couple
7277 /// of competing diagnostics here, -Wconversion and -Wsign-compare.
7278 void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) {
7279 QualType T = OrigE->getType();
7280 Expr *E = OrigE->IgnoreParenImpCasts();
7282 if (E->isTypeDependent() || E->isValueDependent())
7285 // For conditional operators, we analyze the arguments as if they
7286 // were being fed directly into the output.
7287 if (isa<ConditionalOperator>(E)) {
7288 ConditionalOperator *CO = cast<ConditionalOperator>(E);
7289 CheckConditionalOperator(S, CO, CC, T);
7293 // Check implicit argument conversions for function calls.
7294 if (CallExpr *Call = dyn_cast<CallExpr>(E))
7295 CheckImplicitArgumentConversions(S, Call, CC);
7297 // Go ahead and check any implicit conversions we might have skipped.
7298 // The non-canonical typecheck is just an optimization;
7299 // CheckImplicitConversion will filter out dead implicit conversions.
7300 if (E->getType() != T)
7301 CheckImplicitConversion(S, E, T, CC);
7303 // Now continue drilling into this expression.
7305 if (PseudoObjectExpr * POE = dyn_cast<PseudoObjectExpr>(E)) {
7306 if (POE->getResultExpr())
7307 E = POE->getResultExpr();
7310 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) {
7311 if (OVE->getSourceExpr())
7312 AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC);
7316 // Skip past explicit casts.
7317 if (isa<ExplicitCastExpr>(E)) {
7318 E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
7319 return AnalyzeImplicitConversions(S, E, CC);
7322 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
7323 // Do a somewhat different check with comparison operators.
7324 if (BO->isComparisonOp())
7325 return AnalyzeComparison(S, BO);
7327 // And with simple assignments.
7328 if (BO->getOpcode() == BO_Assign)
7329 return AnalyzeAssignment(S, BO);
7332 // These break the otherwise-useful invariant below. Fortunately,
7333 // we don't really need to recurse into them, because any internal
7334 // expressions should have been analyzed already when they were
7335 // built into statements.
7336 if (isa<StmtExpr>(E)) return;
7338 // Don't descend into unevaluated contexts.
7339 if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
7341 // Now just recurse over the expression's children.
7342 CC = E->getExprLoc();
7343 BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
7344 bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd;
7345 for (Stmt *SubStmt : E->children()) {
7346 Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt);
7350 if (IsLogicalAndOperator &&
7351 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
7352 // Ignore checking string literals that are in logical and operators.
7353 // This is a common pattern for asserts.
7355 AnalyzeImplicitConversions(S, ChildExpr, CC);
7358 if (BO && BO->isLogicalOp()) {
7359 Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts();
7360 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
7361 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
7363 SubExpr = BO->getRHS()->IgnoreParenImpCasts();
7364 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
7365 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
7368 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E))
7369 if (U->getOpcode() == UO_LNot)
7370 ::CheckBoolLikeConversion(S, U->getSubExpr(), CC);
7373 } // end anonymous namespace
7381 // Helper function for Sema::DiagnoseAlwaysNonNullPointer.
7382 // Returns true when emitting a warning about taking the address of a reference.
7383 static bool CheckForReference(Sema &SemaRef, const Expr *E,
7384 PartialDiagnostic PD) {
7385 E = E->IgnoreParenImpCasts();
7387 const FunctionDecl *FD = nullptr;
7389 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
7390 if (!DRE->getDecl()->getType()->isReferenceType())
7392 } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) {
7393 if (!M->getMemberDecl()->getType()->isReferenceType())
7395 } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) {
7396 if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType())
7398 FD = Call->getDirectCallee();
7403 SemaRef.Diag(E->getExprLoc(), PD);
7405 // If possible, point to location of function.
7407 SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD;
7413 // Returns true if the SourceLocation is expanded from any macro body.
7414 // Returns false if the SourceLocation is invalid, is from not in a macro
7415 // expansion, or is from expanded from a top-level macro argument.
7416 static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) {
7417 if (Loc.isInvalid())
7420 while (Loc.isMacroID()) {
7421 if (SM.isMacroBodyExpansion(Loc))
7423 Loc = SM.getImmediateMacroCallerLoc(Loc);
7429 /// \brief Diagnose pointers that are always non-null.
7430 /// \param E the expression containing the pointer
7431 /// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is
7432 /// compared to a null pointer
7433 /// \param IsEqual True when the comparison is equal to a null pointer
7434 /// \param Range Extra SourceRange to highlight in the diagnostic
7435 void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
7436 Expr::NullPointerConstantKind NullKind,
7437 bool IsEqual, SourceRange Range) {
7441 // Don't warn inside macros.
7442 if (E->getExprLoc().isMacroID()) {
7443 const SourceManager &SM = getSourceManager();
7444 if (IsInAnyMacroBody(SM, E->getExprLoc()) ||
7445 IsInAnyMacroBody(SM, Range.getBegin()))
7448 E = E->IgnoreImpCasts();
7450 const bool IsCompare = NullKind != Expr::NPCK_NotNull;
7452 if (isa<CXXThisExpr>(E)) {
7453 unsigned DiagID = IsCompare ? diag::warn_this_null_compare
7454 : diag::warn_this_bool_conversion;
7455 Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual;
7459 bool IsAddressOf = false;
7461 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
7462 if (UO->getOpcode() != UO_AddrOf)
7465 E = UO->getSubExpr();
7469 unsigned DiagID = IsCompare
7470 ? diag::warn_address_of_reference_null_compare
7471 : diag::warn_address_of_reference_bool_conversion;
7472 PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range
7474 if (CheckForReference(*this, E, PD)) {
7479 // Expect to find a single Decl. Skip anything more complicated.
7480 ValueDecl *D = nullptr;
7481 if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) {
7483 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
7484 D = M->getMemberDecl();
7487 // Weak Decls can be null.
7488 if (!D || D->isWeak())
7491 // Check for parameter decl with nonnull attribute
7492 if (const ParmVarDecl* PV = dyn_cast<ParmVarDecl>(D)) {
7493 if (getCurFunction() && !getCurFunction()->ModifiedNonNullParams.count(PV))
7494 if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
7495 unsigned NumArgs = FD->getNumParams();
7496 llvm::SmallBitVector AttrNonNull(NumArgs);
7497 for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) {
7498 if (!NonNull->args_size()) {
7499 AttrNonNull.set(0, NumArgs);
7502 for (unsigned Val : NonNull->args()) {
7505 AttrNonNull.set(Val);
7508 if (!AttrNonNull.empty())
7509 for (unsigned i = 0; i < NumArgs; ++i)
7510 if (FD->getParamDecl(i) == PV &&
7511 (AttrNonNull[i] || PV->hasAttr<NonNullAttr>())) {
7513 llvm::raw_string_ostream S(Str);
7514 E->printPretty(S, nullptr, getPrintingPolicy());
7515 unsigned DiagID = IsCompare ? diag::warn_nonnull_parameter_compare
7516 : diag::warn_cast_nonnull_to_bool;
7517 Diag(E->getExprLoc(), DiagID) << S.str() << E->getSourceRange()
7518 << Range << IsEqual;
7524 QualType T = D->getType();
7525 const bool IsArray = T->isArrayType();
7526 const bool IsFunction = T->isFunctionType();
7528 // Address of function is used to silence the function warning.
7529 if (IsAddressOf && IsFunction) {
7534 if (!IsAddressOf && !IsFunction && !IsArray)
7537 // Pretty print the expression for the diagnostic.
7539 llvm::raw_string_ostream S(Str);
7540 E->printPretty(S, nullptr, getPrintingPolicy());
7542 unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare
7543 : diag::warn_impcast_pointer_to_bool;
7546 DiagType = AddressOf;
7547 else if (IsFunction)
7548 DiagType = FunctionPointer;
7550 DiagType = ArrayPointer;
7552 llvm_unreachable("Could not determine diagnostic.");
7553 Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange()
7554 << Range << IsEqual;
7559 // Suggest '&' to silence the function warning.
7560 Diag(E->getExprLoc(), diag::note_function_warning_silence)
7561 << FixItHint::CreateInsertion(E->getLocStart(), "&");
7563 // Check to see if '()' fixit should be emitted.
7564 QualType ReturnType;
7565 UnresolvedSet<4> NonTemplateOverloads;
7566 tryExprAsCall(*E, ReturnType, NonTemplateOverloads);
7567 if (ReturnType.isNull())
7571 // There are two cases here. If there is null constant, the only suggest
7572 // for a pointer return type. If the null is 0, then suggest if the return
7573 // type is a pointer or an integer type.
7574 if (!ReturnType->isPointerType()) {
7575 if (NullKind == Expr::NPCK_ZeroExpression ||
7576 NullKind == Expr::NPCK_ZeroLiteral) {
7577 if (!ReturnType->isIntegerType())
7583 } else { // !IsCompare
7584 // For function to bool, only suggest if the function pointer has bool
7586 if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
7589 Diag(E->getExprLoc(), diag::note_function_to_function_call)
7590 << FixItHint::CreateInsertion(getLocForEndOfToken(E->getLocEnd()), "()");
7594 /// Diagnoses "dangerous" implicit conversions within the given
7595 /// expression (which is a full expression). Implements -Wconversion
7596 /// and -Wsign-compare.
7598 /// \param CC the "context" location of the implicit conversion, i.e.
7599 /// the most location of the syntactic entity requiring the implicit
7601 void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
7602 // Don't diagnose in unevaluated contexts.
7603 if (isUnevaluatedContext())
7606 // Don't diagnose for value- or type-dependent expressions.
7607 if (E->isTypeDependent() || E->isValueDependent())
7610 // Check for array bounds violations in cases where the check isn't triggered
7611 // elsewhere for other Expr types (like BinaryOperators), e.g. when an
7612 // ArraySubscriptExpr is on the RHS of a variable initialization.
7613 CheckArrayAccess(E);
7615 // This is not the right CC for (e.g.) a variable initialization.
7616 AnalyzeImplicitConversions(*this, E, CC);
7619 /// CheckBoolLikeConversion - Check conversion of given expression to boolean.
7620 /// Input argument E is a logical expression.
7621 void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) {
7622 ::CheckBoolLikeConversion(*this, E, CC);
7625 /// Diagnose when expression is an integer constant expression and its evaluation
7626 /// results in integer overflow
7627 void Sema::CheckForIntOverflow (Expr *E) {
7628 if (isa<BinaryOperator>(E->IgnoreParenCasts()))
7629 E->IgnoreParenCasts()->EvaluateForOverflow(Context);
7633 /// \brief Visitor for expressions which looks for unsequenced operations on the
7635 class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> {
7636 typedef EvaluatedExprVisitor<SequenceChecker> Base;
7638 /// \brief A tree of sequenced regions within an expression. Two regions are
7639 /// unsequenced if one is an ancestor or a descendent of the other. When we
7640 /// finish processing an expression with sequencing, such as a comma
7641 /// expression, we fold its tree nodes into its parent, since they are
7642 /// unsequenced with respect to nodes we will visit later.
7643 class SequenceTree {
7645 explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {}
7646 unsigned Parent : 31;
7649 SmallVector<Value, 8> Values;
7652 /// \brief A region within an expression which may be sequenced with respect
7653 /// to some other region.
7655 explicit Seq(unsigned N) : Index(N) {}
7657 friend class SequenceTree;
7662 SequenceTree() { Values.push_back(Value(0)); }
7663 Seq root() const { return Seq(0); }
7665 /// \brief Create a new sequence of operations, which is an unsequenced
7666 /// subset of \p Parent. This sequence of operations is sequenced with
7667 /// respect to other children of \p Parent.
7668 Seq allocate(Seq Parent) {
7669 Values.push_back(Value(Parent.Index));
7670 return Seq(Values.size() - 1);
7673 /// \brief Merge a sequence of operations into its parent.
7675 Values[S.Index].Merged = true;
7678 /// \brief Determine whether two operations are unsequenced. This operation
7679 /// is asymmetric: \p Cur should be the more recent sequence, and \p Old
7680 /// should have been merged into its parent as appropriate.
7681 bool isUnsequenced(Seq Cur, Seq Old) {
7682 unsigned C = representative(Cur.Index);
7683 unsigned Target = representative(Old.Index);
7684 while (C >= Target) {
7687 C = Values[C].Parent;
7693 /// \brief Pick a representative for a sequence.
7694 unsigned representative(unsigned K) {
7695 if (Values[K].Merged)
7696 // Perform path compression as we go.
7697 return Values[K].Parent = representative(Values[K].Parent);
7702 /// An object for which we can track unsequenced uses.
7703 typedef NamedDecl *Object;
7705 /// Different flavors of object usage which we track. We only track the
7706 /// least-sequenced usage of each kind.
7708 /// A read of an object. Multiple unsequenced reads are OK.
7710 /// A modification of an object which is sequenced before the value
7711 /// computation of the expression, such as ++n in C++.
7713 /// A modification of an object which is not sequenced before the value
7714 /// computation of the expression, such as n++.
7717 UK_Count = UK_ModAsSideEffect + 1
7721 Usage() : Use(nullptr), Seq() {}
7723 SequenceTree::Seq Seq;
7727 UsageInfo() : Diagnosed(false) {}
7728 Usage Uses[UK_Count];
7729 /// Have we issued a diagnostic for this variable already?
7732 typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap;
7735 /// Sequenced regions within the expression.
7737 /// Declaration modifications and references which we have seen.
7738 UsageInfoMap UsageMap;
7739 /// The region we are currently within.
7740 SequenceTree::Seq Region;
7741 /// Filled in with declarations which were modified as a side-effect
7742 /// (that is, post-increment operations).
7743 SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect;
7744 /// Expressions to check later. We defer checking these to reduce
7746 SmallVectorImpl<Expr *> &WorkList;
7748 /// RAII object wrapping the visitation of a sequenced subexpression of an
7749 /// expression. At the end of this process, the side-effects of the evaluation
7750 /// become sequenced with respect to the value computation of the result, so
7751 /// we downgrade any UK_ModAsSideEffect within the evaluation to
7753 struct SequencedSubexpression {
7754 SequencedSubexpression(SequenceChecker &Self)
7755 : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) {
7756 Self.ModAsSideEffect = &ModAsSideEffect;
7758 ~SequencedSubexpression() {
7759 for (auto MI = ModAsSideEffect.rbegin(), ME = ModAsSideEffect.rend();
7761 UsageInfo &U = Self.UsageMap[MI->first];
7762 auto &SideEffectUsage = U.Uses[UK_ModAsSideEffect];
7763 Self.addUsage(U, MI->first, SideEffectUsage.Use, UK_ModAsValue);
7764 SideEffectUsage = MI->second;
7766 Self.ModAsSideEffect = OldModAsSideEffect;
7769 SequenceChecker &Self;
7770 SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect;
7771 SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect;
7774 /// RAII object wrapping the visitation of a subexpression which we might
7775 /// choose to evaluate as a constant. If any subexpression is evaluated and
7776 /// found to be non-constant, this allows us to suppress the evaluation of
7777 /// the outer expression.
7778 class EvaluationTracker {
7780 EvaluationTracker(SequenceChecker &Self)
7781 : Self(Self), Prev(Self.EvalTracker), EvalOK(true) {
7782 Self.EvalTracker = this;
7784 ~EvaluationTracker() {
7785 Self.EvalTracker = Prev;
7787 Prev->EvalOK &= EvalOK;
7790 bool evaluate(const Expr *E, bool &Result) {
7791 if (!EvalOK || E->isValueDependent())
7793 EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context);
7798 SequenceChecker &Self;
7799 EvaluationTracker *Prev;
7803 /// \brief Find the object which is produced by the specified expression,
7805 Object getObject(Expr *E, bool Mod) const {
7806 E = E->IgnoreParenCasts();
7807 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
7808 if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec))
7809 return getObject(UO->getSubExpr(), Mod);
7810 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
7811 if (BO->getOpcode() == BO_Comma)
7812 return getObject(BO->getRHS(), Mod);
7813 if (Mod && BO->isAssignmentOp())
7814 return getObject(BO->getLHS(), Mod);
7815 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
7816 // FIXME: Check for more interesting cases, like "x.n = ++x.n".
7817 if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts()))
7818 return ME->getMemberDecl();
7819 } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
7820 // FIXME: If this is a reference, map through to its value.
7821 return DRE->getDecl();
7825 /// \brief Note that an object was modified or used by an expression.
7826 void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) {
7827 Usage &U = UI.Uses[UK];
7828 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) {
7829 if (UK == UK_ModAsSideEffect && ModAsSideEffect)
7830 ModAsSideEffect->push_back(std::make_pair(O, U));
7835 /// \brief Check whether a modification or use conflicts with a prior usage.
7836 void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind,
7841 const Usage &U = UI.Uses[OtherKind];
7842 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq))
7846 Expr *ModOrUse = Ref;
7847 if (OtherKind == UK_Use)
7848 std::swap(Mod, ModOrUse);
7850 SemaRef.Diag(Mod->getExprLoc(),
7851 IsModMod ? diag::warn_unsequenced_mod_mod
7852 : diag::warn_unsequenced_mod_use)
7853 << O << SourceRange(ModOrUse->getExprLoc());
7854 UI.Diagnosed = true;
7857 void notePreUse(Object O, Expr *Use) {
7858 UsageInfo &U = UsageMap[O];
7859 // Uses conflict with other modifications.
7860 checkUsage(O, U, Use, UK_ModAsValue, false);
7862 void notePostUse(Object O, Expr *Use) {
7863 UsageInfo &U = UsageMap[O];
7864 checkUsage(O, U, Use, UK_ModAsSideEffect, false);
7865 addUsage(U, O, Use, UK_Use);
7868 void notePreMod(Object O, Expr *Mod) {
7869 UsageInfo &U = UsageMap[O];
7870 // Modifications conflict with other modifications and with uses.
7871 checkUsage(O, U, Mod, UK_ModAsValue, true);
7872 checkUsage(O, U, Mod, UK_Use, false);
7874 void notePostMod(Object O, Expr *Use, UsageKind UK) {
7875 UsageInfo &U = UsageMap[O];
7876 checkUsage(O, U, Use, UK_ModAsSideEffect, true);
7877 addUsage(U, O, Use, UK);
7881 SequenceChecker(Sema &S, Expr *E, SmallVectorImpl<Expr *> &WorkList)
7882 : Base(S.Context), SemaRef(S), Region(Tree.root()),
7883 ModAsSideEffect(nullptr), WorkList(WorkList), EvalTracker(nullptr) {
7887 void VisitStmt(Stmt *S) {
7888 // Skip all statements which aren't expressions for now.
7891 void VisitExpr(Expr *E) {
7892 // By default, just recurse to evaluated subexpressions.
7896 void VisitCastExpr(CastExpr *E) {
7897 Object O = Object();
7898 if (E->getCastKind() == CK_LValueToRValue)
7899 O = getObject(E->getSubExpr(), false);
7908 void VisitBinComma(BinaryOperator *BO) {
7909 // C++11 [expr.comma]p1:
7910 // Every value computation and side effect associated with the left
7911 // expression is sequenced before every value computation and side
7912 // effect associated with the right expression.
7913 SequenceTree::Seq LHS = Tree.allocate(Region);
7914 SequenceTree::Seq RHS = Tree.allocate(Region);
7915 SequenceTree::Seq OldRegion = Region;
7918 SequencedSubexpression SeqLHS(*this);
7920 Visit(BO->getLHS());
7924 Visit(BO->getRHS());
7928 // Forget that LHS and RHS are sequenced. They are both unsequenced
7929 // with respect to other stuff.
7934 void VisitBinAssign(BinaryOperator *BO) {
7935 // The modification is sequenced after the value computation of the LHS
7936 // and RHS, so check it before inspecting the operands and update the
7938 Object O = getObject(BO->getLHS(), true);
7940 return VisitExpr(BO);
7944 // C++11 [expr.ass]p7:
7945 // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated
7948 // Therefore, for a compound assignment operator, O is considered used
7949 // everywhere except within the evaluation of E1 itself.
7950 if (isa<CompoundAssignOperator>(BO))
7953 Visit(BO->getLHS());
7955 if (isa<CompoundAssignOperator>(BO))
7958 Visit(BO->getRHS());
7960 // C++11 [expr.ass]p1:
7961 // the assignment is sequenced [...] before the value computation of the
7962 // assignment expression.
7963 // C11 6.5.16/3 has no such rule.
7964 notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
7965 : UK_ModAsSideEffect);
7967 void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) {
7968 VisitBinAssign(CAO);
7971 void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
7972 void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
7973 void VisitUnaryPreIncDec(UnaryOperator *UO) {
7974 Object O = getObject(UO->getSubExpr(), true);
7976 return VisitExpr(UO);
7979 Visit(UO->getSubExpr());
7980 // C++11 [expr.pre.incr]p1:
7981 // the expression ++x is equivalent to x+=1
7982 notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
7983 : UK_ModAsSideEffect);
7986 void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
7987 void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
7988 void VisitUnaryPostIncDec(UnaryOperator *UO) {
7989 Object O = getObject(UO->getSubExpr(), true);
7991 return VisitExpr(UO);
7994 Visit(UO->getSubExpr());
7995 notePostMod(O, UO, UK_ModAsSideEffect);
7998 /// Don't visit the RHS of '&&' or '||' if it might not be evaluated.
7999 void VisitBinLOr(BinaryOperator *BO) {
8000 // The side-effects of the LHS of an '&&' are sequenced before the
8001 // value computation of the RHS, and hence before the value computation
8002 // of the '&&' itself, unless the LHS evaluates to zero. We treat them
8003 // as if they were unconditionally sequenced.
8004 EvaluationTracker Eval(*this);
8006 SequencedSubexpression Sequenced(*this);
8007 Visit(BO->getLHS());
8011 if (Eval.evaluate(BO->getLHS(), Result)) {
8013 Visit(BO->getRHS());
8015 // Check for unsequenced operations in the RHS, treating it as an
8016 // entirely separate evaluation.
8018 // FIXME: If there are operations in the RHS which are unsequenced
8019 // with respect to operations outside the RHS, and those operations
8020 // are unconditionally evaluated, diagnose them.
8021 WorkList.push_back(BO->getRHS());
8024 void VisitBinLAnd(BinaryOperator *BO) {
8025 EvaluationTracker Eval(*this);
8027 SequencedSubexpression Sequenced(*this);
8028 Visit(BO->getLHS());
8032 if (Eval.evaluate(BO->getLHS(), Result)) {
8034 Visit(BO->getRHS());
8036 WorkList.push_back(BO->getRHS());
8040 // Only visit the condition, unless we can be sure which subexpression will
8042 void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) {
8043 EvaluationTracker Eval(*this);
8045 SequencedSubexpression Sequenced(*this);
8046 Visit(CO->getCond());
8050 if (Eval.evaluate(CO->getCond(), Result))
8051 Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr());
8053 WorkList.push_back(CO->getTrueExpr());
8054 WorkList.push_back(CO->getFalseExpr());
8058 void VisitCallExpr(CallExpr *CE) {
8059 // C++11 [intro.execution]p15:
8060 // When calling a function [...], every value computation and side effect
8061 // associated with any argument expression, or with the postfix expression
8062 // designating the called function, is sequenced before execution of every
8063 // expression or statement in the body of the function [and thus before
8064 // the value computation of its result].
8065 SequencedSubexpression Sequenced(*this);
8066 Base::VisitCallExpr(CE);
8068 // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions.
8071 void VisitCXXConstructExpr(CXXConstructExpr *CCE) {
8072 // This is a call, so all subexpressions are sequenced before the result.
8073 SequencedSubexpression Sequenced(*this);
8075 if (!CCE->isListInitialization())
8076 return VisitExpr(CCE);
8078 // In C++11, list initializations are sequenced.
8079 SmallVector<SequenceTree::Seq, 32> Elts;
8080 SequenceTree::Seq Parent = Region;
8081 for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(),
8084 Region = Tree.allocate(Parent);
8085 Elts.push_back(Region);
8089 // Forget that the initializers are sequenced.
8091 for (unsigned I = 0; I < Elts.size(); ++I)
8092 Tree.merge(Elts[I]);
8095 void VisitInitListExpr(InitListExpr *ILE) {
8096 if (!SemaRef.getLangOpts().CPlusPlus11)
8097 return VisitExpr(ILE);
8099 // In C++11, list initializations are sequenced.
8100 SmallVector<SequenceTree::Seq, 32> Elts;
8101 SequenceTree::Seq Parent = Region;
8102 for (unsigned I = 0; I < ILE->getNumInits(); ++I) {
8103 Expr *E = ILE->getInit(I);
8105 Region = Tree.allocate(Parent);
8106 Elts.push_back(Region);
8110 // Forget that the initializers are sequenced.
8112 for (unsigned I = 0; I < Elts.size(); ++I)
8113 Tree.merge(Elts[I]);
8118 void Sema::CheckUnsequencedOperations(Expr *E) {
8119 SmallVector<Expr *, 8> WorkList;
8120 WorkList.push_back(E);
8121 while (!WorkList.empty()) {
8122 Expr *Item = WorkList.pop_back_val();
8123 SequenceChecker(*this, Item, WorkList);
8127 void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc,
8129 CheckImplicitConversions(E, CheckLoc);
8130 CheckUnsequencedOperations(E);
8131 if (!IsConstexpr && !E->isValueDependent())
8132 CheckForIntOverflow(E);
8135 void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
8136 FieldDecl *BitField,
8138 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
8141 static void diagnoseArrayStarInParamType(Sema &S, QualType PType,
8142 SourceLocation Loc) {
8143 if (!PType->isVariablyModifiedType())
8145 if (const auto *PointerTy = dyn_cast<PointerType>(PType)) {
8146 diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc);
8149 if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) {
8150 diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc);
8153 if (const auto *ParenTy = dyn_cast<ParenType>(PType)) {
8154 diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc);
8158 const ArrayType *AT = S.Context.getAsArrayType(PType);
8162 if (AT->getSizeModifier() != ArrayType::Star) {
8163 diagnoseArrayStarInParamType(S, AT->getElementType(), Loc);
8167 S.Diag(Loc, diag::err_array_star_in_function_definition);
8170 /// CheckParmsForFunctionDef - Check that the parameters of the given
8171 /// function are appropriate for the definition of a function. This
8172 /// takes care of any checks that cannot be performed on the
8173 /// declaration itself, e.g., that the types of each of the function
8174 /// parameters are complete.
8175 bool Sema::CheckParmsForFunctionDef(ParmVarDecl *const *P,
8176 ParmVarDecl *const *PEnd,
8177 bool CheckParameterNames) {
8178 bool HasInvalidParm = false;
8179 for (; P != PEnd; ++P) {
8180 ParmVarDecl *Param = *P;
8182 // C99 6.7.5.3p4: the parameters in a parameter type list in a
8183 // function declarator that is part of a function definition of
8184 // that function shall not have incomplete type.
8186 // This is also C++ [dcl.fct]p6.
8187 if (!Param->isInvalidDecl() &&
8188 RequireCompleteType(Param->getLocation(), Param->getType(),
8189 diag::err_typecheck_decl_incomplete_type)) {
8190 Param->setInvalidDecl();
8191 HasInvalidParm = true;
8194 // C99 6.9.1p5: If the declarator includes a parameter type list, the
8195 // declaration of each parameter shall include an identifier.
8196 if (CheckParameterNames &&
8197 Param->getIdentifier() == nullptr &&
8198 !Param->isImplicit() &&
8199 !getLangOpts().CPlusPlus)
8200 Diag(Param->getLocation(), diag::err_parameter_name_omitted);
8203 // If the function declarator is not part of a definition of that
8204 // function, parameters may have incomplete type and may use the [*]
8205 // notation in their sequences of declarator specifiers to specify
8206 // variable length array types.
8207 QualType PType = Param->getOriginalType();
8208 // FIXME: This diagnostic should point the '[*]' if source-location
8209 // information is added for it.
8210 diagnoseArrayStarInParamType(*this, PType, Param->getLocation());
8212 // MSVC destroys objects passed by value in the callee. Therefore a
8213 // function definition which takes such a parameter must be able to call the
8214 // object's destructor. However, we don't perform any direct access check
8216 if (getLangOpts().CPlusPlus && Context.getTargetInfo()
8218 .areArgsDestroyedLeftToRightInCallee()) {
8219 if (!Param->isInvalidDecl()) {
8220 if (const RecordType *RT = Param->getType()->getAs<RecordType>()) {
8221 CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
8222 if (!ClassDecl->isInvalidDecl() &&
8223 !ClassDecl->hasIrrelevantDestructor() &&
8224 !ClassDecl->isDependentContext()) {
8225 CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl);
8226 MarkFunctionReferenced(Param->getLocation(), Destructor);
8227 DiagnoseUseOfDecl(Destructor, Param->getLocation());
8234 return HasInvalidParm;
8237 /// CheckCastAlign - Implements -Wcast-align, which warns when a
8238 /// pointer cast increases the alignment requirements.
8239 void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
8240 // This is actually a lot of work to potentially be doing on every
8241 // cast; don't do it if we're ignoring -Wcast_align (as is the default).
8242 if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin()))
8245 // Ignore dependent types.
8246 if (T->isDependentType() || Op->getType()->isDependentType())
8249 // Require that the destination be a pointer type.
8250 const PointerType *DestPtr = T->getAs<PointerType>();
8251 if (!DestPtr) return;
8253 // If the destination has alignment 1, we're done.
8254 QualType DestPointee = DestPtr->getPointeeType();
8255 if (DestPointee->isIncompleteType()) return;
8256 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
8257 if (DestAlign.isOne()) return;
8259 // Require that the source be a pointer type.
8260 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
8261 if (!SrcPtr) return;
8262 QualType SrcPointee = SrcPtr->getPointeeType();
8264 // Whitelist casts from cv void*. We already implicitly
8265 // whitelisted casts to cv void*, since they have alignment 1.
8266 // Also whitelist casts involving incomplete types, which implicitly
8268 if (SrcPointee->isIncompleteType()) return;
8270 CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
8271 if (SrcAlign >= DestAlign) return;
8273 Diag(TRange.getBegin(), diag::warn_cast_align)
8274 << Op->getType() << T
8275 << static_cast<unsigned>(SrcAlign.getQuantity())
8276 << static_cast<unsigned>(DestAlign.getQuantity())
8277 << TRange << Op->getSourceRange();
8280 static const Type* getElementType(const Expr *BaseExpr) {
8281 const Type* EltType = BaseExpr->getType().getTypePtr();
8282 if (EltType->isAnyPointerType())
8283 return EltType->getPointeeType().getTypePtr();
8284 else if (EltType->isArrayType())
8285 return EltType->getBaseElementTypeUnsafe();
8289 /// \brief Check whether this array fits the idiom of a size-one tail padded
8290 /// array member of a struct.
8292 /// We avoid emitting out-of-bounds access warnings for such arrays as they are
8293 /// commonly used to emulate flexible arrays in C89 code.
8294 static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
8295 const NamedDecl *ND) {
8296 if (Size != 1 || !ND) return false;
8298 const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
8299 if (!FD) return false;
8301 // Don't consider sizes resulting from macro expansions or template argument
8302 // substitution to form C89 tail-padded arrays.
8304 TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
8306 TypeLoc TL = TInfo->getTypeLoc();
8307 // Look through typedefs.
8308 if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) {
8309 const TypedefNameDecl *TDL = TTL.getTypedefNameDecl();
8310 TInfo = TDL->getTypeSourceInfo();
8313 if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) {
8314 const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr());
8315 if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
8321 const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
8322 if (!RD) return false;
8323 if (RD->isUnion()) return false;
8324 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
8325 if (!CRD->isStandardLayout()) return false;
8328 // See if this is the last field decl in the record.
8330 while ((D = D->getNextDeclInContext()))
8331 if (isa<FieldDecl>(D))
8336 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
8337 const ArraySubscriptExpr *ASE,
8338 bool AllowOnePastEnd, bool IndexNegated) {
8339 IndexExpr = IndexExpr->IgnoreParenImpCasts();
8340 if (IndexExpr->isValueDependent())
8343 const Type *EffectiveType = getElementType(BaseExpr);
8344 BaseExpr = BaseExpr->IgnoreParenCasts();
8345 const ConstantArrayType *ArrayTy =
8346 Context.getAsConstantArrayType(BaseExpr->getType());
8351 if (!IndexExpr->EvaluateAsInt(index, Context))
8356 const NamedDecl *ND = nullptr;
8357 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
8358 ND = dyn_cast<NamedDecl>(DRE->getDecl());
8359 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
8360 ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
8362 if (index.isUnsigned() || !index.isNegative()) {
8363 llvm::APInt size = ArrayTy->getSize();
8364 if (!size.isStrictlyPositive())
8367 const Type* BaseType = getElementType(BaseExpr);
8368 if (BaseType != EffectiveType) {
8369 // Make sure we're comparing apples to apples when comparing index to size
8370 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
8371 uint64_t array_typesize = Context.getTypeSize(BaseType);
8372 // Handle ptrarith_typesize being zero, such as when casting to void*
8373 if (!ptrarith_typesize) ptrarith_typesize = 1;
8374 if (ptrarith_typesize != array_typesize) {
8375 // There's a cast to a different size type involved
8376 uint64_t ratio = array_typesize / ptrarith_typesize;
8377 // TODO: Be smarter about handling cases where array_typesize is not a
8378 // multiple of ptrarith_typesize
8379 if (ptrarith_typesize * ratio == array_typesize)
8380 size *= llvm::APInt(size.getBitWidth(), ratio);
8384 if (size.getBitWidth() > index.getBitWidth())
8385 index = index.zext(size.getBitWidth());
8386 else if (size.getBitWidth() < index.getBitWidth())
8387 size = size.zext(index.getBitWidth());
8389 // For array subscripting the index must be less than size, but for pointer
8390 // arithmetic also allow the index (offset) to be equal to size since
8391 // computing the next address after the end of the array is legal and
8392 // commonly done e.g. in C++ iterators and range-based for loops.
8393 if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
8396 // Also don't warn for arrays of size 1 which are members of some
8397 // structure. These are often used to approximate flexible arrays in C89
8399 if (IsTailPaddedMemberArray(*this, size, ND))
8402 // Suppress the warning if the subscript expression (as identified by the
8403 // ']' location) and the index expression are both from macro expansions
8404 // within a system header.
8406 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
8407 ASE->getRBracketLoc());
8408 if (SourceMgr.isInSystemHeader(RBracketLoc)) {
8409 SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
8410 IndexExpr->getLocStart());
8411 if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc))
8416 unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
8418 DiagID = diag::warn_array_index_exceeds_bounds;
8420 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
8421 PDiag(DiagID) << index.toString(10, true)
8422 << size.toString(10, true)
8423 << (unsigned)size.getLimitedValue(~0U)
8424 << IndexExpr->getSourceRange());
8426 unsigned DiagID = diag::warn_array_index_precedes_bounds;
8428 DiagID = diag::warn_ptr_arith_precedes_bounds;
8429 if (index.isNegative()) index = -index;
8432 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
8433 PDiag(DiagID) << index.toString(10, true)
8434 << IndexExpr->getSourceRange());
8438 // Try harder to find a NamedDecl to point at in the note.
8439 while (const ArraySubscriptExpr *ASE =
8440 dyn_cast<ArraySubscriptExpr>(BaseExpr))
8441 BaseExpr = ASE->getBase()->IgnoreParenCasts();
8442 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
8443 ND = dyn_cast<NamedDecl>(DRE->getDecl());
8444 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
8445 ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
8449 DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
8450 PDiag(diag::note_array_index_out_of_bounds)
8451 << ND->getDeclName());
8454 void Sema::CheckArrayAccess(const Expr *expr) {
8455 int AllowOnePastEnd = 0;
8457 expr = expr->IgnoreParenImpCasts();
8458 switch (expr->getStmtClass()) {
8459 case Stmt::ArraySubscriptExprClass: {
8460 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
8461 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
8462 AllowOnePastEnd > 0);
8465 case Stmt::UnaryOperatorClass: {
8466 // Only unwrap the * and & unary operators
8467 const UnaryOperator *UO = cast<UnaryOperator>(expr);
8468 expr = UO->getSubExpr();
8469 switch (UO->getOpcode()) {
8481 case Stmt::ConditionalOperatorClass: {
8482 const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
8483 if (const Expr *lhs = cond->getLHS())
8484 CheckArrayAccess(lhs);
8485 if (const Expr *rhs = cond->getRHS())
8486 CheckArrayAccess(rhs);
8495 //===--- CHECK: Objective-C retain cycles ----------------------------------//
8498 struct RetainCycleOwner {
8499 RetainCycleOwner() : Variable(nullptr), Indirect(false) {}
8505 void setLocsFrom(Expr *e) {
8506 Loc = e->getExprLoc();
8507 Range = e->getSourceRange();
8512 /// Consider whether capturing the given variable can possibly lead to
8514 static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
8515 // In ARC, it's captured strongly iff the variable has __strong
8516 // lifetime. In MRR, it's captured strongly if the variable is
8517 // __block and has an appropriate type.
8518 if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
8521 owner.Variable = var;
8523 owner.setLocsFrom(ref);
8527 static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
8529 e = e->IgnoreParens();
8530 if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
8531 switch (cast->getCastKind()) {
8533 case CK_LValueBitCast:
8534 case CK_LValueToRValue:
8535 case CK_ARCReclaimReturnedObject:
8536 e = cast->getSubExpr();
8544 if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
8545 ObjCIvarDecl *ivar = ref->getDecl();
8546 if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
8549 // Try to find a retain cycle in the base.
8550 if (!findRetainCycleOwner(S, ref->getBase(), owner))
8553 if (ref->isFreeIvar()) owner.setLocsFrom(ref);
8554 owner.Indirect = true;
8558 if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
8559 VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
8560 if (!var) return false;
8561 return considerVariable(var, ref, owner);
8564 if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
8565 if (member->isArrow()) return false;
8567 // Don't count this as an indirect ownership.
8568 e = member->getBase();
8572 if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
8573 // Only pay attention to pseudo-objects on property references.
8574 ObjCPropertyRefExpr *pre
8575 = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm()
8577 if (!pre) return false;
8578 if (pre->isImplicitProperty()) return false;
8579 ObjCPropertyDecl *property = pre->getExplicitProperty();
8580 if (!property->isRetaining() &&
8581 !(property->getPropertyIvarDecl() &&
8582 property->getPropertyIvarDecl()->getType()
8583 .getObjCLifetime() == Qualifiers::OCL_Strong))
8586 owner.Indirect = true;
8587 if (pre->isSuperReceiver()) {
8588 owner.Variable = S.getCurMethodDecl()->getSelfDecl();
8589 if (!owner.Variable)
8591 owner.Loc = pre->getLocation();
8592 owner.Range = pre->getSourceRange();
8595 e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase())
8607 struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
8608 FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
8609 : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
8610 Context(Context), Variable(variable), Capturer(nullptr),
8611 VarWillBeReased(false) {}
8612 ASTContext &Context;
8615 bool VarWillBeReased;
8617 void VisitDeclRefExpr(DeclRefExpr *ref) {
8618 if (ref->getDecl() == Variable && !Capturer)
8622 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
8623 if (Capturer) return;
8624 Visit(ref->getBase());
8625 if (Capturer && ref->isFreeIvar())
8629 void VisitBlockExpr(BlockExpr *block) {
8630 // Look inside nested blocks
8631 if (block->getBlockDecl()->capturesVariable(Variable))
8632 Visit(block->getBlockDecl()->getBody());
8635 void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) {
8636 if (Capturer) return;
8637 if (OVE->getSourceExpr())
8638 Visit(OVE->getSourceExpr());
8640 void VisitBinaryOperator(BinaryOperator *BinOp) {
8641 if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign)
8643 Expr *LHS = BinOp->getLHS();
8644 if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) {
8645 if (DRE->getDecl() != Variable)
8647 if (Expr *RHS = BinOp->getRHS()) {
8648 RHS = RHS->IgnoreParenCasts();
8651 (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0);
8658 /// Check whether the given argument is a block which captures a
8660 static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
8661 assert(owner.Variable && owner.Loc.isValid());
8663 e = e->IgnoreParenCasts();
8665 // Look through [^{...} copy] and Block_copy(^{...}).
8666 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) {
8667 Selector Cmd = ME->getSelector();
8668 if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") {
8669 e = ME->getInstanceReceiver();
8672 e = e->IgnoreParenCasts();
8674 } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) {
8675 if (CE->getNumArgs() == 1) {
8676 FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl());
8678 const IdentifierInfo *FnI = Fn->getIdentifier();
8679 if (FnI && FnI->isStr("_Block_copy")) {
8680 e = CE->getArg(0)->IgnoreParenCasts();
8686 BlockExpr *block = dyn_cast<BlockExpr>(e);
8687 if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
8690 FindCaptureVisitor visitor(S.Context, owner.Variable);
8691 visitor.Visit(block->getBlockDecl()->getBody());
8692 return visitor.VarWillBeReased ? nullptr : visitor.Capturer;
8695 static void diagnoseRetainCycle(Sema &S, Expr *capturer,
8696 RetainCycleOwner &owner) {
8698 assert(owner.Variable && owner.Loc.isValid());
8700 S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
8701 << owner.Variable << capturer->getSourceRange();
8702 S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
8703 << owner.Indirect << owner.Range;
8706 /// Check for a keyword selector that starts with the word 'add' or
8708 static bool isSetterLikeSelector(Selector sel) {
8709 if (sel.isUnarySelector()) return false;
8711 StringRef str = sel.getNameForSlot(0);
8712 while (!str.empty() && str.front() == '_') str = str.substr(1);
8713 if (str.startswith("set"))
8714 str = str.substr(3);
8715 else if (str.startswith("add")) {
8716 // Specially whitelist 'addOperationWithBlock:'.
8717 if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock"))
8719 str = str.substr(3);
8724 if (str.empty()) return true;
8725 return !isLowercase(str.front());
8728 static Optional<int> GetNSMutableArrayArgumentIndex(Sema &S,
8729 ObjCMessageExpr *Message) {
8730 if (S.NSMutableArrayPointer.isNull()) {
8731 IdentifierInfo *NSMutableArrayId =
8732 S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSMutableArray);
8733 NamedDecl *IF = S.LookupSingleName(S.TUScope, NSMutableArrayId,
8734 Message->getLocStart(),
8735 Sema::LookupOrdinaryName);
8736 ObjCInterfaceDecl *InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
8737 if (!InterfaceDecl) {
8740 QualType NSMutableArrayObject =
8741 S.Context.getObjCInterfaceType(InterfaceDecl);
8742 S.NSMutableArrayPointer =
8743 S.Context.getObjCObjectPointerType(NSMutableArrayObject);
8746 if (S.NSMutableArrayPointer != Message->getReceiverType()) {
8750 Selector Sel = Message->getSelector();
8752 Optional<NSAPI::NSArrayMethodKind> MKOpt =
8753 S.NSAPIObj->getNSArrayMethodKind(Sel);
8758 NSAPI::NSArrayMethodKind MK = *MKOpt;
8761 case NSAPI::NSMutableArr_addObject:
8762 case NSAPI::NSMutableArr_insertObjectAtIndex:
8763 case NSAPI::NSMutableArr_setObjectAtIndexedSubscript:
8765 case NSAPI::NSMutableArr_replaceObjectAtIndex:
8776 Optional<int> GetNSMutableDictionaryArgumentIndex(Sema &S,
8777 ObjCMessageExpr *Message) {
8779 if (S.NSMutableDictionaryPointer.isNull()) {
8780 IdentifierInfo *NSMutableDictionaryId =
8781 S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSMutableDictionary);
8782 NamedDecl *IF = S.LookupSingleName(S.TUScope, NSMutableDictionaryId,
8783 Message->getLocStart(),
8784 Sema::LookupOrdinaryName);
8785 ObjCInterfaceDecl *InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
8786 if (!InterfaceDecl) {
8789 QualType NSMutableDictionaryObject =
8790 S.Context.getObjCInterfaceType(InterfaceDecl);
8791 S.NSMutableDictionaryPointer =
8792 S.Context.getObjCObjectPointerType(NSMutableDictionaryObject);
8795 if (S.NSMutableDictionaryPointer != Message->getReceiverType()) {
8799 Selector Sel = Message->getSelector();
8801 Optional<NSAPI::NSDictionaryMethodKind> MKOpt =
8802 S.NSAPIObj->getNSDictionaryMethodKind(Sel);
8807 NSAPI::NSDictionaryMethodKind MK = *MKOpt;
8810 case NSAPI::NSMutableDict_setObjectForKey:
8811 case NSAPI::NSMutableDict_setValueForKey:
8812 case NSAPI::NSMutableDict_setObjectForKeyedSubscript:
8822 static Optional<int> GetNSSetArgumentIndex(Sema &S, ObjCMessageExpr *Message) {
8824 ObjCInterfaceDecl *InterfaceDecl;
8825 if (S.NSMutableSetPointer.isNull()) {
8826 IdentifierInfo *NSMutableSetId =
8827 S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSMutableSet);
8828 NamedDecl *IF = S.LookupSingleName(S.TUScope, NSMutableSetId,
8829 Message->getLocStart(),
8830 Sema::LookupOrdinaryName);
8831 InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
8832 if (InterfaceDecl) {
8833 QualType NSMutableSetObject =
8834 S.Context.getObjCInterfaceType(InterfaceDecl);
8835 S.NSMutableSetPointer =
8836 S.Context.getObjCObjectPointerType(NSMutableSetObject);
8840 if (S.NSCountedSetPointer.isNull()) {
8841 IdentifierInfo *NSCountedSetId =
8842 S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSCountedSet);
8843 NamedDecl *IF = S.LookupSingleName(S.TUScope, NSCountedSetId,
8844 Message->getLocStart(),
8845 Sema::LookupOrdinaryName);
8846 InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
8847 if (InterfaceDecl) {
8848 QualType NSCountedSetObject =
8849 S.Context.getObjCInterfaceType(InterfaceDecl);
8850 S.NSCountedSetPointer =
8851 S.Context.getObjCObjectPointerType(NSCountedSetObject);
8855 if (S.NSMutableOrderedSetPointer.isNull()) {
8856 IdentifierInfo *NSOrderedSetId =
8857 S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSMutableOrderedSet);
8858 NamedDecl *IF = S.LookupSingleName(S.TUScope, NSOrderedSetId,
8859 Message->getLocStart(),
8860 Sema::LookupOrdinaryName);
8861 InterfaceDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
8862 if (InterfaceDecl) {
8863 QualType NSOrderedSetObject =
8864 S.Context.getObjCInterfaceType(InterfaceDecl);
8865 S.NSMutableOrderedSetPointer =
8866 S.Context.getObjCObjectPointerType(NSOrderedSetObject);
8870 QualType ReceiverType = Message->getReceiverType();
8872 bool IsMutableSet = !S.NSMutableSetPointer.isNull() &&
8873 ReceiverType == S.NSMutableSetPointer;
8874 bool IsMutableOrderedSet = !S.NSMutableOrderedSetPointer.isNull() &&
8875 ReceiverType == S.NSMutableOrderedSetPointer;
8876 bool IsCountedSet = !S.NSCountedSetPointer.isNull() &&
8877 ReceiverType == S.NSCountedSetPointer;
8879 if (!IsMutableSet && !IsMutableOrderedSet && !IsCountedSet) {
8883 Selector Sel = Message->getSelector();
8885 Optional<NSAPI::NSSetMethodKind> MKOpt = S.NSAPIObj->getNSSetMethodKind(Sel);
8890 NSAPI::NSSetMethodKind MK = *MKOpt;
8893 case NSAPI::NSMutableSet_addObject:
8894 case NSAPI::NSOrderedSet_setObjectAtIndex:
8895 case NSAPI::NSOrderedSet_setObjectAtIndexedSubscript:
8896 case NSAPI::NSOrderedSet_insertObjectAtIndex:
8898 case NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject:
8905 void Sema::CheckObjCCircularContainer(ObjCMessageExpr *Message) {
8906 if (!Message->isInstanceMessage()) {
8910 Optional<int> ArgOpt;
8912 if (!(ArgOpt = GetNSMutableArrayArgumentIndex(*this, Message)) &&
8913 !(ArgOpt = GetNSMutableDictionaryArgumentIndex(*this, Message)) &&
8914 !(ArgOpt = GetNSSetArgumentIndex(*this, Message))) {
8918 int ArgIndex = *ArgOpt;
8920 Expr *Receiver = Message->getInstanceReceiver()->IgnoreImpCasts();
8921 if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Receiver)) {
8922 Receiver = OE->getSourceExpr()->IgnoreImpCasts();
8925 Expr *Arg = Message->getArg(ArgIndex)->IgnoreImpCasts();
8926 if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Arg)) {
8927 Arg = OE->getSourceExpr()->IgnoreImpCasts();
8930 if (DeclRefExpr *ReceiverRE = dyn_cast<DeclRefExpr>(Receiver)) {
8931 if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) {
8932 if (ReceiverRE->getDecl() == ArgRE->getDecl()) {
8933 ValueDecl *Decl = ReceiverRE->getDecl();
8934 Diag(Message->getSourceRange().getBegin(),
8935 diag::warn_objc_circular_container)
8937 Diag(Decl->getLocation(),
8938 diag::note_objc_circular_container_declared_here)
8942 } else if (ObjCIvarRefExpr *IvarRE = dyn_cast<ObjCIvarRefExpr>(Receiver)) {
8943 if (ObjCIvarRefExpr *IvarArgRE = dyn_cast<ObjCIvarRefExpr>(Arg)) {
8944 if (IvarRE->getDecl() == IvarArgRE->getDecl()) {
8945 ObjCIvarDecl *Decl = IvarRE->getDecl();
8946 Diag(Message->getSourceRange().getBegin(),
8947 diag::warn_objc_circular_container)
8949 Diag(Decl->getLocation(),
8950 diag::note_objc_circular_container_declared_here)
8958 /// Check a message send to see if it's likely to cause a retain cycle.
8959 void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
8960 // Only check instance methods whose selector looks like a setter.
8961 if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
8964 // Try to find a variable that the receiver is strongly owned by.
8965 RetainCycleOwner owner;
8966 if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
8967 if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner))
8970 assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
8971 owner.Variable = getCurMethodDecl()->getSelfDecl();
8972 owner.Loc = msg->getSuperLoc();
8973 owner.Range = msg->getSuperLoc();
8976 // Check whether the receiver is captured by any of the arguments.
8977 for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i)
8978 if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner))
8979 return diagnoseRetainCycle(*this, capturer, owner);
8982 /// Check a property assign to see if it's likely to cause a retain cycle.
8983 void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
8984 RetainCycleOwner owner;
8985 if (!findRetainCycleOwner(*this, receiver, owner))
8988 if (Expr *capturer = findCapturingExpr(*this, argument, owner))
8989 diagnoseRetainCycle(*this, capturer, owner);
8992 void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) {
8993 RetainCycleOwner Owner;
8994 if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner))
8997 // Because we don't have an expression for the variable, we have to set the
8998 // location explicitly here.
8999 Owner.Loc = Var->getLocation();
9000 Owner.Range = Var->getSourceRange();
9002 if (Expr *Capturer = findCapturingExpr(*this, Init, Owner))
9003 diagnoseRetainCycle(*this, Capturer, Owner);
9006 static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc,
9007 Expr *RHS, bool isProperty) {
9008 // Check if RHS is an Objective-C object literal, which also can get
9009 // immediately zapped in a weak reference. Note that we explicitly
9010 // allow ObjCStringLiterals, since those are designed to never really die.
9011 RHS = RHS->IgnoreParenImpCasts();
9013 // This enum needs to match with the 'select' in
9014 // warn_objc_arc_literal_assign (off-by-1).
9015 Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS);
9016 if (Kind == Sema::LK_String || Kind == Sema::LK_None)
9019 S.Diag(Loc, diag::warn_arc_literal_assign)
9021 << (isProperty ? 0 : 1)
9022 << RHS->getSourceRange();
9027 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc,
9028 Qualifiers::ObjCLifetime LT,
9029 Expr *RHS, bool isProperty) {
9030 // Strip off any implicit cast added to get to the one ARC-specific.
9031 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
9032 if (cast->getCastKind() == CK_ARCConsumeObject) {
9033 S.Diag(Loc, diag::warn_arc_retained_assign)
9034 << (LT == Qualifiers::OCL_ExplicitNone)
9035 << (isProperty ? 0 : 1)
9036 << RHS->getSourceRange();
9039 RHS = cast->getSubExpr();
9042 if (LT == Qualifiers::OCL_Weak &&
9043 checkUnsafeAssignLiteral(S, Loc, RHS, isProperty))
9049 bool Sema::checkUnsafeAssigns(SourceLocation Loc,
9050 QualType LHS, Expr *RHS) {
9051 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
9053 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
9056 if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false))
9062 void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
9063 Expr *LHS, Expr *RHS) {
9065 // PropertyRef on LHS type need be directly obtained from
9066 // its declaration as it has a PseudoType.
9067 ObjCPropertyRefExpr *PRE
9068 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
9069 if (PRE && !PRE->isImplicitProperty()) {
9070 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
9072 LHSType = PD->getType();
9075 if (LHSType.isNull())
9076 LHSType = LHS->getType();
9078 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
9080 if (LT == Qualifiers::OCL_Weak) {
9081 if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc))
9082 getCurFunction()->markSafeWeakUse(LHS);
9085 if (checkUnsafeAssigns(Loc, LHSType, RHS))
9088 // FIXME. Check for other life times.
9089 if (LT != Qualifiers::OCL_None)
9093 if (PRE->isImplicitProperty())
9095 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
9099 unsigned Attributes = PD->getPropertyAttributes();
9100 if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) {
9101 // when 'assign' attribute was not explicitly specified
9102 // by user, ignore it and rely on property type itself
9103 // for lifetime info.
9104 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
9105 if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) &&
9106 LHSType->isObjCRetainableType())
9109 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
9110 if (cast->getCastKind() == CK_ARCConsumeObject) {
9111 Diag(Loc, diag::warn_arc_retained_property_assign)
9112 << RHS->getSourceRange();
9115 RHS = cast->getSubExpr();
9118 else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) {
9119 if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true))
9125 //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
9128 bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
9129 SourceLocation StmtLoc,
9130 const NullStmt *Body) {
9131 // Do not warn if the body is a macro that expands to nothing, e.g:
9137 if (Body->hasLeadingEmptyMacro())
9140 // Get line numbers of statement and body.
9141 bool StmtLineInvalid;
9142 unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc,
9144 if (StmtLineInvalid)
9147 bool BodyLineInvalid;
9148 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
9150 if (BodyLineInvalid)
9153 // Warn if null statement and body are on the same line.
9154 if (StmtLine != BodyLine)
9159 } // Unnamed namespace
9161 void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
9164 // Since this is a syntactic check, don't emit diagnostic for template
9165 // instantiations, this just adds noise.
9166 if (CurrentInstantiationScope)
9169 // The body should be a null statement.
9170 const NullStmt *NBody = dyn_cast<NullStmt>(Body);
9174 // Do the usual checks.
9175 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
9178 Diag(NBody->getSemiLoc(), DiagID);
9179 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
9182 void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
9183 const Stmt *PossibleBody) {
9184 assert(!CurrentInstantiationScope); // Ensured by caller
9186 SourceLocation StmtLoc;
9189 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
9190 StmtLoc = FS->getRParenLoc();
9191 Body = FS->getBody();
9192 DiagID = diag::warn_empty_for_body;
9193 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
9194 StmtLoc = WS->getCond()->getSourceRange().getEnd();
9195 Body = WS->getBody();
9196 DiagID = diag::warn_empty_while_body;
9198 return; // Neither `for' nor `while'.
9200 // The body should be a null statement.
9201 const NullStmt *NBody = dyn_cast<NullStmt>(Body);
9205 // Skip expensive checks if diagnostic is disabled.
9206 if (Diags.isIgnored(DiagID, NBody->getSemiLoc()))
9209 // Do the usual checks.
9210 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
9213 // `for(...);' and `while(...);' are popular idioms, so in order to keep
9214 // noise level low, emit diagnostics only if for/while is followed by a
9215 // CompoundStmt, e.g.:
9216 // for (int i = 0; i < n; i++);
9220 // or if for/while is followed by a statement with more indentation
9221 // than for/while itself:
9222 // for (int i = 0; i < n; i++);
9224 bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
9225 if (!ProbableTypo) {
9226 bool BodyColInvalid;
9227 unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
9228 PossibleBody->getLocStart(),
9233 bool StmtColInvalid;
9234 unsigned StmtCol = SourceMgr.getPresumedColumnNumber(
9240 if (BodyCol > StmtCol)
9241 ProbableTypo = true;
9245 Diag(NBody->getSemiLoc(), DiagID);
9246 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
9250 //===--- CHECK: Warn on self move with std::move. -------------------------===//
9252 /// DiagnoseSelfMove - Emits a warning if a value is moved to itself.
9253 void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr,
9254 SourceLocation OpLoc) {
9256 if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc))
9259 if (!ActiveTemplateInstantiations.empty())
9262 // Strip parens and casts away.
9263 LHSExpr = LHSExpr->IgnoreParenImpCasts();
9264 RHSExpr = RHSExpr->IgnoreParenImpCasts();
9266 // Check for a call expression
9267 const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr);
9268 if (!CE || CE->getNumArgs() != 1)
9271 // Check for a call to std::move
9272 const FunctionDecl *FD = CE->getDirectCallee();
9273 if (!FD || !FD->isInStdNamespace() || !FD->getIdentifier() ||
9274 !FD->getIdentifier()->isStr("move"))
9277 // Get argument from std::move
9278 RHSExpr = CE->getArg(0);
9280 const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr);
9281 const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr);
9283 // Two DeclRefExpr's, check that the decls are the same.
9284 if (LHSDeclRef && RHSDeclRef) {
9285 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
9287 if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
9288 RHSDeclRef->getDecl()->getCanonicalDecl())
9291 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
9292 << LHSExpr->getSourceRange()
9293 << RHSExpr->getSourceRange();
9297 // Member variables require a different approach to check for self moves.
9298 // MemberExpr's are the same if every nested MemberExpr refers to the same
9299 // Decl and that the base Expr's are DeclRefExpr's with the same Decl or
9300 // the base Expr's are CXXThisExpr's.
9301 const Expr *LHSBase = LHSExpr;
9302 const Expr *RHSBase = RHSExpr;
9303 const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr);
9304 const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr);
9305 if (!LHSME || !RHSME)
9308 while (LHSME && RHSME) {
9309 if (LHSME->getMemberDecl()->getCanonicalDecl() !=
9310 RHSME->getMemberDecl()->getCanonicalDecl())
9313 LHSBase = LHSME->getBase();
9314 RHSBase = RHSME->getBase();
9315 LHSME = dyn_cast<MemberExpr>(LHSBase);
9316 RHSME = dyn_cast<MemberExpr>(RHSBase);
9319 LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase);
9320 RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase);
9321 if (LHSDeclRef && RHSDeclRef) {
9322 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
9324 if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
9325 RHSDeclRef->getDecl()->getCanonicalDecl())
9328 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
9329 << LHSExpr->getSourceRange()
9330 << RHSExpr->getSourceRange();
9334 if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase))
9335 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
9336 << LHSExpr->getSourceRange()
9337 << RHSExpr->getSourceRange();
9340 //===--- Layout compatibility ----------------------------------------------//
9344 bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2);
9346 /// \brief Check if two enumeration types are layout-compatible.
9347 bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) {
9348 // C++11 [dcl.enum] p8:
9349 // Two enumeration types are layout-compatible if they have the same
9351 return ED1->isComplete() && ED2->isComplete() &&
9352 C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType());
9355 /// \brief Check if two fields are layout-compatible.
9356 bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) {
9357 if (!isLayoutCompatible(C, Field1->getType(), Field2->getType()))
9360 if (Field1->isBitField() != Field2->isBitField())
9363 if (Field1->isBitField()) {
9364 // Make sure that the bit-fields are the same length.
9365 unsigned Bits1 = Field1->getBitWidthValue(C);
9366 unsigned Bits2 = Field2->getBitWidthValue(C);
9375 /// \brief Check if two standard-layout structs are layout-compatible.
9376 /// (C++11 [class.mem] p17)
9377 bool isLayoutCompatibleStruct(ASTContext &C,
9380 // If both records are C++ classes, check that base classes match.
9381 if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) {
9382 // If one of records is a CXXRecordDecl we are in C++ mode,
9383 // thus the other one is a CXXRecordDecl, too.
9384 const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2);
9385 // Check number of base classes.
9386 if (D1CXX->getNumBases() != D2CXX->getNumBases())
9389 // Check the base classes.
9390 for (CXXRecordDecl::base_class_const_iterator
9391 Base1 = D1CXX->bases_begin(),
9392 BaseEnd1 = D1CXX->bases_end(),
9393 Base2 = D2CXX->bases_begin();
9396 if (!isLayoutCompatible(C, Base1->getType(), Base2->getType()))
9399 } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) {
9400 // If only RD2 is a C++ class, it should have zero base classes.
9401 if (D2CXX->getNumBases() > 0)
9405 // Check the fields.
9406 RecordDecl::field_iterator Field2 = RD2->field_begin(),
9407 Field2End = RD2->field_end(),
9408 Field1 = RD1->field_begin(),
9409 Field1End = RD1->field_end();
9410 for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) {
9411 if (!isLayoutCompatible(C, *Field1, *Field2))
9414 if (Field1 != Field1End || Field2 != Field2End)
9420 /// \brief Check if two standard-layout unions are layout-compatible.
9421 /// (C++11 [class.mem] p18)
9422 bool isLayoutCompatibleUnion(ASTContext &C,
9425 llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields;
9426 for (auto *Field2 : RD2->fields())
9427 UnmatchedFields.insert(Field2);
9429 for (auto *Field1 : RD1->fields()) {
9430 llvm::SmallPtrSet<FieldDecl *, 8>::iterator
9431 I = UnmatchedFields.begin(),
9432 E = UnmatchedFields.end();
9434 for ( ; I != E; ++I) {
9435 if (isLayoutCompatible(C, Field1, *I)) {
9436 bool Result = UnmatchedFields.erase(*I);
9446 return UnmatchedFields.empty();
9449 bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) {
9450 if (RD1->isUnion() != RD2->isUnion())
9454 return isLayoutCompatibleUnion(C, RD1, RD2);
9456 return isLayoutCompatibleStruct(C, RD1, RD2);
9459 /// \brief Check if two types are layout-compatible in C++11 sense.
9460 bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) {
9461 if (T1.isNull() || T2.isNull())
9464 // C++11 [basic.types] p11:
9465 // If two types T1 and T2 are the same type, then T1 and T2 are
9466 // layout-compatible types.
9467 if (C.hasSameType(T1, T2))
9470 T1 = T1.getCanonicalType().getUnqualifiedType();
9471 T2 = T2.getCanonicalType().getUnqualifiedType();
9473 const Type::TypeClass TC1 = T1->getTypeClass();
9474 const Type::TypeClass TC2 = T2->getTypeClass();
9479 if (TC1 == Type::Enum) {
9480 return isLayoutCompatible(C,
9481 cast<EnumType>(T1)->getDecl(),
9482 cast<EnumType>(T2)->getDecl());
9483 } else if (TC1 == Type::Record) {
9484 if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType())
9487 return isLayoutCompatible(C,
9488 cast<RecordType>(T1)->getDecl(),
9489 cast<RecordType>(T2)->getDecl());
9496 //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----//
9499 /// \brief Given a type tag expression find the type tag itself.
9501 /// \param TypeExpr Type tag expression, as it appears in user's code.
9503 /// \param VD Declaration of an identifier that appears in a type tag.
9505 /// \param MagicValue Type tag magic value.
9506 bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx,
9507 const ValueDecl **VD, uint64_t *MagicValue) {
9512 TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts();
9514 switch (TypeExpr->getStmtClass()) {
9515 case Stmt::UnaryOperatorClass: {
9516 const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr);
9517 if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) {
9518 TypeExpr = UO->getSubExpr();
9524 case Stmt::DeclRefExprClass: {
9525 const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr);
9526 *VD = DRE->getDecl();
9530 case Stmt::IntegerLiteralClass: {
9531 const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr);
9532 llvm::APInt MagicValueAPInt = IL->getValue();
9533 if (MagicValueAPInt.getActiveBits() <= 64) {
9534 *MagicValue = MagicValueAPInt.getZExtValue();
9540 case Stmt::BinaryConditionalOperatorClass:
9541 case Stmt::ConditionalOperatorClass: {
9542 const AbstractConditionalOperator *ACO =
9543 cast<AbstractConditionalOperator>(TypeExpr);
9545 if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) {
9547 TypeExpr = ACO->getTrueExpr();
9549 TypeExpr = ACO->getFalseExpr();
9555 case Stmt::BinaryOperatorClass: {
9556 const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr);
9557 if (BO->getOpcode() == BO_Comma) {
9558 TypeExpr = BO->getRHS();
9570 /// \brief Retrieve the C type corresponding to type tag TypeExpr.
9572 /// \param TypeExpr Expression that specifies a type tag.
9574 /// \param MagicValues Registered magic values.
9576 /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong
9579 /// \param TypeInfo Information about the corresponding C type.
9581 /// \returns true if the corresponding C type was found.
9582 bool GetMatchingCType(
9583 const IdentifierInfo *ArgumentKind,
9584 const Expr *TypeExpr, const ASTContext &Ctx,
9585 const llvm::DenseMap<Sema::TypeTagMagicValue,
9586 Sema::TypeTagData> *MagicValues,
9587 bool &FoundWrongKind,
9588 Sema::TypeTagData &TypeInfo) {
9589 FoundWrongKind = false;
9591 // Variable declaration that has type_tag_for_datatype attribute.
9592 const ValueDecl *VD = nullptr;
9594 uint64_t MagicValue;
9596 if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue))
9600 if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) {
9601 if (I->getArgumentKind() != ArgumentKind) {
9602 FoundWrongKind = true;
9605 TypeInfo.Type = I->getMatchingCType();
9606 TypeInfo.LayoutCompatible = I->getLayoutCompatible();
9607 TypeInfo.MustBeNull = I->getMustBeNull();
9616 llvm::DenseMap<Sema::TypeTagMagicValue,
9617 Sema::TypeTagData>::const_iterator I =
9618 MagicValues->find(std::make_pair(ArgumentKind, MagicValue));
9619 if (I == MagicValues->end())
9622 TypeInfo = I->second;
9625 } // unnamed namespace
9627 void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind,
9628 uint64_t MagicValue, QualType Type,
9629 bool LayoutCompatible,
9631 if (!TypeTagForDatatypeMagicValues)
9632 TypeTagForDatatypeMagicValues.reset(
9633 new llvm::DenseMap<TypeTagMagicValue, TypeTagData>);
9635 TypeTagMagicValue Magic(ArgumentKind, MagicValue);
9636 (*TypeTagForDatatypeMagicValues)[Magic] =
9637 TypeTagData(Type, LayoutCompatible, MustBeNull);
9641 bool IsSameCharType(QualType T1, QualType T2) {
9642 const BuiltinType *BT1 = T1->getAs<BuiltinType>();
9646 const BuiltinType *BT2 = T2->getAs<BuiltinType>();
9650 BuiltinType::Kind T1Kind = BT1->getKind();
9651 BuiltinType::Kind T2Kind = BT2->getKind();
9653 return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) ||
9654 (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) ||
9655 (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) ||
9656 (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar);
9658 } // unnamed namespace
9660 void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr,
9661 const Expr * const *ExprArgs) {
9662 const IdentifierInfo *ArgumentKind = Attr->getArgumentKind();
9663 bool IsPointerAttr = Attr->getIsPointer();
9665 const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()];
9666 bool FoundWrongKind;
9667 TypeTagData TypeInfo;
9668 if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context,
9669 TypeTagForDatatypeMagicValues.get(),
9670 FoundWrongKind, TypeInfo)) {
9672 Diag(TypeTagExpr->getExprLoc(),
9673 diag::warn_type_tag_for_datatype_wrong_kind)
9674 << TypeTagExpr->getSourceRange();
9678 const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()];
9679 if (IsPointerAttr) {
9680 // Skip implicit cast of pointer to `void *' (as a function argument).
9681 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr))
9682 if (ICE->getType()->isVoidPointerType() &&
9683 ICE->getCastKind() == CK_BitCast)
9684 ArgumentExpr = ICE->getSubExpr();
9686 QualType ArgumentType = ArgumentExpr->getType();
9688 // Passing a `void*' pointer shouldn't trigger a warning.
9689 if (IsPointerAttr && ArgumentType->isVoidPointerType())
9692 if (TypeInfo.MustBeNull) {
9693 // Type tag with matching void type requires a null pointer.
9694 if (!ArgumentExpr->isNullPointerConstant(Context,
9695 Expr::NPC_ValueDependentIsNotNull)) {
9696 Diag(ArgumentExpr->getExprLoc(),
9697 diag::warn_type_safety_null_pointer_required)
9698 << ArgumentKind->getName()
9699 << ArgumentExpr->getSourceRange()
9700 << TypeTagExpr->getSourceRange();
9705 QualType RequiredType = TypeInfo.Type;
9707 RequiredType = Context.getPointerType(RequiredType);
9709 bool mismatch = false;
9710 if (!TypeInfo.LayoutCompatible) {
9711 mismatch = !Context.hasSameType(ArgumentType, RequiredType);
9713 // C++11 [basic.fundamental] p1:
9714 // Plain char, signed char, and unsigned char are three distinct types.
9716 // But we treat plain `char' as equivalent to `signed char' or `unsigned
9717 // char' depending on the current char signedness mode.
9719 if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(),
9720 RequiredType->getPointeeType())) ||
9721 (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType)))
9725 mismatch = !isLayoutCompatible(Context,
9726 ArgumentType->getPointeeType(),
9727 RequiredType->getPointeeType());
9729 mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType);
9732 Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch)
9733 << ArgumentType << ArgumentKind
9734 << TypeInfo.LayoutCompatible << RequiredType
9735 << ArgumentExpr->getSourceRange()
9736 << TypeTagExpr->getSourceRange();