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