blob: bf95c22e99eed0d62bf52ecc5f2d26da024ac474 [file] [log] [blame]
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00001//===- SemaChecking.cpp - Extra Semantic Checking -------------------------===//
Chris Lattnerb87b1b32007-08-10 20:18:51 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb87b1b32007-08-10 20:18:51 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump11289f42009-09-09 15:08:12 +000010// This file implements extra semantic analysis beyond what is enforced
Chris Lattnerb87b1b32007-08-10 20:18:51 +000011// by the C type system.
12//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000015#include "clang/AST/APValue.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000016#include "clang/AST/ASTContext.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000017#include "clang/AST/Attr.h"
18#include "clang/AST/AttrIterator.h"
Ken Dyck40775002010-01-11 17:06:35 +000019#include "clang/AST/CharUnits.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000020#include "clang/AST/Decl.h"
21#include "clang/AST/DeclBase.h"
John McCall28a0cf72010-08-25 07:42:41 +000022#include "clang/AST/DeclCXX.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000023#include "clang/AST/DeclObjC.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000024#include "clang/AST/DeclarationName.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/AST/EvaluatedExprVisitor.h"
David Blaikie7555b6a2012-05-15 16:56:36 +000026#include "clang/AST/Expr.h"
Ted Kremenekc81614d2007-08-20 16:18:38 +000027#include "clang/AST/ExprCXX.h"
Ted Kremenek34f664d2008-06-16 18:00:42 +000028#include "clang/AST/ExprObjC.h"
Alexey Bataev1a3320e2015-08-25 14:24:04 +000029#include "clang/AST/ExprOpenMP.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000030#include "clang/AST/NSAPI.h"
Akira Hatanaka2be04412018-04-17 19:13:41 +000031#include "clang/AST/NonTrivialTypeVisitor.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000032#include "clang/AST/OperationKinds.h"
33#include "clang/AST/Stmt.h"
34#include "clang/AST/TemplateBase.h"
35#include "clang/AST/Type.h"
36#include "clang/AST/TypeLoc.h"
37#include "clang/AST/UnresolvedSet.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000038#include "clang/Analysis/Analyses/FormatString.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000039#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000040#include "clang/Basic/CharInfo.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000041#include "clang/Basic/Diagnostic.h"
42#include "clang/Basic/IdentifierTable.h"
43#include "clang/Basic/LLVM.h"
44#include "clang/Basic/LangOptions.h"
45#include "clang/Basic/OpenCLOptions.h"
46#include "clang/Basic/OperatorKinds.h"
47#include "clang/Basic/PartialDiagnostic.h"
48#include "clang/Basic/SourceLocation.h"
49#include "clang/Basic/SourceManager.h"
50#include "clang/Basic/Specifiers.h"
Yaxun Liu39195062017-08-04 18:16:31 +000051#include "clang/Basic/SyncScope.h"
Eric Christopher8d0c6212010-04-17 02:26:23 +000052#include "clang/Basic/TargetBuiltins.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000053#include "clang/Basic/TargetCXXABI.h"
Nate Begeman4904e322010-06-08 02:47:44 +000054#include "clang/Basic/TargetInfo.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000055#include "clang/Basic/TypeTraits.h"
Alp Tokerb6cc5922014-05-03 03:45:55 +000056#include "clang/Lex/Lexer.h" // TODO: Extract static functions to fix layering.
Chandler Carruth3a022472012-12-04 09:13:33 +000057#include "clang/Sema/Initialization.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000058#include "clang/Sema/Lookup.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000059#include "clang/Sema/Ownership.h"
60#include "clang/Sema/Scope.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000061#include "clang/Sema/ScopeInfo.h"
62#include "clang/Sema/Sema.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000063#include "clang/Sema/SemaInternal.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000064#include "llvm/ADT/APFloat.h"
65#include "llvm/ADT/APInt.h"
66#include "llvm/ADT/APSInt.h"
67#include "llvm/ADT/ArrayRef.h"
68#include "llvm/ADT/DenseMap.h"
69#include "llvm/ADT/FoldingSet.h"
70#include "llvm/ADT/None.h"
71#include "llvm/ADT/Optional.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000072#include "llvm/ADT/STLExtras.h"
Richard Smithd7293d72013-08-05 18:49:43 +000073#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000074#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000075#include "llvm/ADT/SmallString.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000076#include "llvm/ADT/SmallVector.h"
77#include "llvm/ADT/StringRef.h"
78#include "llvm/ADT/StringSwitch.h"
79#include "llvm/ADT/Triple.h"
80#include "llvm/Support/AtomicOrdering.h"
81#include "llvm/Support/Casting.h"
82#include "llvm/Support/Compiler.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000083#include "llvm/Support/ConvertUTF.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000084#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +000085#include "llvm/Support/Format.h"
86#include "llvm/Support/Locale.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000087#include "llvm/Support/MathExtras.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000088#include "llvm/Support/raw_ostream.h"
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000089#include <algorithm>
90#include <cassert>
91#include <cstddef>
92#include <cstdint>
93#include <functional>
94#include <limits>
95#include <string>
96#include <tuple>
97#include <utility>
Eugene Zelenko1ced5092016-02-12 22:53:10 +000098
Chris Lattnerb87b1b32007-08-10 20:18:51 +000099using namespace clang;
John McCallaab3e412010-08-25 08:40:02 +0000100using namespace sema;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000101
Chris Lattnera26fb342009-02-18 17:49:48 +0000102SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
103 unsigned ByteNo) const {
Alp Tokerb6cc5922014-05-03 03:45:55 +0000104 return SL->getLocationOfByte(ByteNo, getSourceManager(), LangOpts,
105 Context.getTargetInfo());
Chris Lattnera26fb342009-02-18 17:49:48 +0000106}
107
John McCallbebede42011-02-26 05:39:39 +0000108/// Checks that a call expression's argument count is the desired number.
109/// This is useful when doing custom type-checking. Returns true on error.
110static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
111 unsigned argCount = call->getNumArgs();
112 if (argCount == desiredArgCount) return false;
113
114 if (argCount < desiredArgCount)
115 return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args)
116 << 0 /*function call*/ << desiredArgCount << argCount
117 << call->getSourceRange();
118
119 // Highlight all the excess arguments.
120 SourceRange range(call->getArg(desiredArgCount)->getLocStart(),
121 call->getArg(argCount - 1)->getLocEnd());
Nico Weberade321e2018-04-10 18:53:28 +0000122
John McCallbebede42011-02-26 05:39:39 +0000123 return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
124 << 0 /*function call*/ << desiredArgCount << argCount
125 << call->getArg(1)->getSourceRange();
126}
127
Julien Lerouge4a5b4442012-04-28 17:39:16 +0000128/// Check that the first argument to __builtin_annotation is an integer
129/// and the second argument is a non-wide string literal.
130static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
131 if (checkArgCount(S, TheCall, 2))
132 return true;
133
134 // First argument should be an integer.
135 Expr *ValArg = TheCall->getArg(0);
136 QualType Ty = ValArg->getType();
137 if (!Ty->isIntegerType()) {
138 S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg)
139 << ValArg->getSourceRange();
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000140 return true;
141 }
Julien Lerouge4a5b4442012-04-28 17:39:16 +0000142
143 // Second argument should be a constant string.
144 Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
145 StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
146 if (!Literal || !Literal->isAscii()) {
147 S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg)
148 << StrArg->getSourceRange();
149 return true;
150 }
151
152 TheCall->setType(Ty);
Julien Lerouge5a6b6982011-09-09 22:41:49 +0000153 return false;
154}
155
Reid Kleckner30701ed2017-09-05 20:27:35 +0000156static bool SemaBuiltinMSVCAnnotation(Sema &S, CallExpr *TheCall) {
157 // We need at least one argument.
158 if (TheCall->getNumArgs() < 1) {
159 S.Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
160 << 0 << 1 << TheCall->getNumArgs()
161 << TheCall->getCallee()->getSourceRange();
162 return true;
163 }
164
165 // All arguments should be wide string literals.
166 for (Expr *Arg : TheCall->arguments()) {
167 auto *Literal = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
168 if (!Literal || !Literal->isWide()) {
169 S.Diag(Arg->getLocStart(), diag::err_msvc_annotation_wide_str)
170 << Arg->getSourceRange();
171 return true;
172 }
173 }
174
175 return false;
176}
177
Richard Smith6cbd65d2013-07-11 02:27:57 +0000178/// Check that the argument to __builtin_addressof is a glvalue, and set the
179/// result type to the corresponding pointer type.
180static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) {
181 if (checkArgCount(S, TheCall, 1))
182 return true;
183
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000184 ExprResult Arg(TheCall->getArg(0));
Richard Smith6cbd65d2013-07-11 02:27:57 +0000185 QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getLocStart());
186 if (ResultType.isNull())
187 return true;
188
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000189 TheCall->setArg(0, Arg.get());
Richard Smith6cbd65d2013-07-11 02:27:57 +0000190 TheCall->setType(ResultType);
191 return false;
192}
193
John McCall03107a42015-10-29 20:48:01 +0000194static bool SemaBuiltinOverflow(Sema &S, CallExpr *TheCall) {
195 if (checkArgCount(S, TheCall, 3))
196 return true;
197
198 // First two arguments should be integers.
199 for (unsigned I = 0; I < 2; ++I) {
200 Expr *Arg = TheCall->getArg(I);
201 QualType Ty = Arg->getType();
202 if (!Ty->isIntegerType()) {
203 S.Diag(Arg->getLocStart(), diag::err_overflow_builtin_must_be_int)
204 << Ty << Arg->getSourceRange();
205 return true;
206 }
207 }
208
209 // Third argument should be a pointer to a non-const integer.
210 // IRGen correctly handles volatile, restrict, and address spaces, and
211 // the other qualifiers aren't possible.
212 {
213 Expr *Arg = TheCall->getArg(2);
214 QualType Ty = Arg->getType();
215 const auto *PtrTy = Ty->getAs<PointerType>();
216 if (!(PtrTy && PtrTy->getPointeeType()->isIntegerType() &&
217 !PtrTy->getPointeeType().isConstQualified())) {
218 S.Diag(Arg->getLocStart(), diag::err_overflow_builtin_must_be_ptr_int)
219 << Ty << Arg->getSourceRange();
220 return true;
221 }
222 }
223
224 return false;
225}
226
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +0000227static void SemaBuiltinMemChkCall(Sema &S, FunctionDecl *FDecl,
Nico Weberade321e2018-04-10 18:53:28 +0000228 CallExpr *TheCall, unsigned SizeIdx,
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +0000229 unsigned DstSizeIdx) {
230 if (TheCall->getNumArgs() <= SizeIdx ||
231 TheCall->getNumArgs() <= DstSizeIdx)
232 return;
233
234 const Expr *SizeArg = TheCall->getArg(SizeIdx);
235 const Expr *DstSizeArg = TheCall->getArg(DstSizeIdx);
236
237 llvm::APSInt Size, DstSize;
238
239 // find out if both sizes are known at compile time
240 if (!SizeArg->EvaluateAsInt(Size, S.Context) ||
241 !DstSizeArg->EvaluateAsInt(DstSize, S.Context))
242 return;
243
244 if (Size.ule(DstSize))
245 return;
246
247 // confirmed overflow so generate the diagnostic.
248 IdentifierInfo *FnName = FDecl->getIdentifier();
249 SourceLocation SL = TheCall->getLocStart();
250 SourceRange SR = TheCall->getSourceRange();
251
252 S.Diag(SL, diag::warn_memcpy_chk_overflow) << SR << FnName;
253}
254
Peter Collingbournef7706832014-12-12 23:41:25 +0000255static bool SemaBuiltinCallWithStaticChain(Sema &S, CallExpr *BuiltinCall) {
256 if (checkArgCount(S, BuiltinCall, 2))
257 return true;
258
259 SourceLocation BuiltinLoc = BuiltinCall->getLocStart();
260 Expr *Builtin = BuiltinCall->getCallee()->IgnoreImpCasts();
261 Expr *Call = BuiltinCall->getArg(0);
262 Expr *Chain = BuiltinCall->getArg(1);
263
264 if (Call->getStmtClass() != Stmt::CallExprClass) {
265 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_not_call)
266 << Call->getSourceRange();
267 return true;
268 }
269
270 auto CE = cast<CallExpr>(Call);
271 if (CE->getCallee()->getType()->isBlockPointerType()) {
272 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_block_call)
273 << Call->getSourceRange();
274 return true;
275 }
276
277 const Decl *TargetDecl = CE->getCalleeDecl();
278 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
279 if (FD->getBuiltinID()) {
280 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_builtin_call)
281 << Call->getSourceRange();
282 return true;
283 }
284
285 if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) {
286 S.Diag(BuiltinLoc, diag::err_first_argument_to_cwsc_pdtor_call)
287 << Call->getSourceRange();
288 return true;
289 }
290
291 ExprResult ChainResult = S.UsualUnaryConversions(Chain);
292 if (ChainResult.isInvalid())
293 return true;
294 if (!ChainResult.get()->getType()->isPointerType()) {
295 S.Diag(BuiltinLoc, diag::err_second_argument_to_cwsc_not_pointer)
296 << Chain->getSourceRange();
297 return true;
298 }
299
David Majnemerced8bdf2015-02-25 17:36:15 +0000300 QualType ReturnTy = CE->getCallReturnType(S.Context);
Peter Collingbournef7706832014-12-12 23:41:25 +0000301 QualType ArgTys[2] = { ReturnTy, ChainResult.get()->getType() };
302 QualType BuiltinTy = S.Context.getFunctionType(
303 ReturnTy, ArgTys, FunctionProtoType::ExtProtoInfo());
304 QualType BuiltinPtrTy = S.Context.getPointerType(BuiltinTy);
305
306 Builtin =
307 S.ImpCastExprToType(Builtin, BuiltinPtrTy, CK_BuiltinFnToFnPtr).get();
308
309 BuiltinCall->setType(CE->getType());
310 BuiltinCall->setValueKind(CE->getValueKind());
311 BuiltinCall->setObjectKind(CE->getObjectKind());
312 BuiltinCall->setCallee(Builtin);
313 BuiltinCall->setArg(1, ChainResult.get());
314
315 return false;
316}
317
Reid Kleckner1d59f992015-01-22 01:36:17 +0000318static bool SemaBuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall,
319 Scope::ScopeFlags NeededScopeFlags,
320 unsigned DiagID) {
321 // Scopes aren't available during instantiation. Fortunately, builtin
322 // functions cannot be template args so they cannot be formed through template
323 // instantiation. Therefore checking once during the parse is sufficient.
Richard Smith51ec0cf2017-02-21 01:17:38 +0000324 if (SemaRef.inTemplateInstantiation())
Reid Kleckner1d59f992015-01-22 01:36:17 +0000325 return false;
326
327 Scope *S = SemaRef.getCurScope();
328 while (S && !S->isSEHExceptScope())
329 S = S->getParent();
330 if (!S || !(S->getFlags() & NeededScopeFlags)) {
331 auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
332 SemaRef.Diag(TheCall->getExprLoc(), DiagID)
333 << DRE->getDecl()->getIdentifier();
334 return true;
335 }
336
337 return false;
338}
339
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000340static inline bool isBlockPointer(Expr *Arg) {
341 return Arg->getType()->isBlockPointerType();
342}
343
344/// OpenCL C v2.0, s6.13.17.2 - Checks that the block parameters are all local
345/// void*, which is a requirement of device side enqueue.
346static bool checkOpenCLBlockArgs(Sema &S, Expr *BlockArg) {
347 const BlockPointerType *BPT =
348 cast<BlockPointerType>(BlockArg->getType().getCanonicalType());
349 ArrayRef<QualType> Params =
350 BPT->getPointeeType()->getAs<FunctionProtoType>()->getParamTypes();
351 unsigned ArgCounter = 0;
352 bool IllegalParams = false;
353 // Iterate through the block parameters until either one is found that is not
354 // a local void*, or the block is valid.
355 for (ArrayRef<QualType>::iterator I = Params.begin(), E = Params.end();
356 I != E; ++I, ++ArgCounter) {
357 if (!(*I)->isPointerType() || !(*I)->getPointeeType()->isVoidType() ||
358 (*I)->getPointeeType().getQualifiers().getAddressSpace() !=
359 LangAS::opencl_local) {
360 // Get the location of the error. If a block literal has been passed
361 // (BlockExpr) then we can point straight to the offending argument,
362 // else we just point to the variable reference.
363 SourceLocation ErrorLoc;
364 if (isa<BlockExpr>(BlockArg)) {
365 BlockDecl *BD = cast<BlockExpr>(BlockArg)->getBlockDecl();
366 ErrorLoc = BD->getParamDecl(ArgCounter)->getLocStart();
367 } else if (isa<DeclRefExpr>(BlockArg)) {
368 ErrorLoc = cast<DeclRefExpr>(BlockArg)->getLocStart();
369 }
370 S.Diag(ErrorLoc,
371 diag::err_opencl_enqueue_kernel_blocks_non_local_void_args);
372 IllegalParams = true;
373 }
374 }
375
376 return IllegalParams;
377}
378
Joey Gouly84ae3362017-07-31 15:15:59 +0000379static bool checkOpenCLSubgroupExt(Sema &S, CallExpr *Call) {
380 if (!S.getOpenCLOptions().isEnabled("cl_khr_subgroups")) {
381 S.Diag(Call->getLocStart(), diag::err_opencl_requires_extension)
382 << 1 << Call->getDirectCallee() << "cl_khr_subgroups";
383 return true;
384 }
385 return false;
386}
387
Joey Goulyfa76b492017-08-01 13:27:09 +0000388static bool SemaOpenCLBuiltinNDRangeAndBlock(Sema &S, CallExpr *TheCall) {
389 if (checkArgCount(S, TheCall, 2))
390 return true;
391
392 if (checkOpenCLSubgroupExt(S, TheCall))
393 return true;
394
395 // First argument is an ndrange_t type.
396 Expr *NDRangeArg = TheCall->getArg(0);
Yaxun Liub7318e02017-10-13 03:37:48 +0000397 if (NDRangeArg->getType().getUnqualifiedType().getAsString() != "ndrange_t") {
Joey Goulyfa76b492017-08-01 13:27:09 +0000398 S.Diag(NDRangeArg->getLocStart(),
399 diag::err_opencl_builtin_expected_type)
400 << TheCall->getDirectCallee() << "'ndrange_t'";
401 return true;
402 }
403
404 Expr *BlockArg = TheCall->getArg(1);
405 if (!isBlockPointer(BlockArg)) {
406 S.Diag(BlockArg->getLocStart(),
407 diag::err_opencl_builtin_expected_type)
408 << TheCall->getDirectCallee() << "block";
409 return true;
410 }
411 return checkOpenCLBlockArgs(S, BlockArg);
412}
413
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000414/// OpenCL C v2.0, s6.13.17.6 - Check the argument to the
415/// get_kernel_work_group_size
416/// and get_kernel_preferred_work_group_size_multiple builtin functions.
417static bool SemaOpenCLBuiltinKernelWorkGroupSize(Sema &S, CallExpr *TheCall) {
418 if (checkArgCount(S, TheCall, 1))
419 return true;
420
421 Expr *BlockArg = TheCall->getArg(0);
422 if (!isBlockPointer(BlockArg)) {
423 S.Diag(BlockArg->getLocStart(),
Joey Gouly6b03d952017-07-04 11:50:23 +0000424 diag::err_opencl_builtin_expected_type)
425 << TheCall->getDirectCallee() << "block";
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000426 return true;
427 }
428 return checkOpenCLBlockArgs(S, BlockArg);
429}
430
Simon Pilgrim2c518802017-03-30 14:13:19 +0000431/// Diagnose integer type and any valid implicit conversion to it.
Anastasia Stulova0df4ac32016-11-14 17:39:58 +0000432static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E,
433 const QualType &IntType);
434
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000435static bool checkOpenCLEnqueueLocalSizeArgs(Sema &S, CallExpr *TheCall,
Anastasia Stulova0df4ac32016-11-14 17:39:58 +0000436 unsigned Start, unsigned End) {
437 bool IllegalParams = false;
438 for (unsigned I = Start; I <= End; ++I)
439 IllegalParams |= checkOpenCLEnqueueIntType(S, TheCall->getArg(I),
440 S.Context.getSizeType());
441 return IllegalParams;
442}
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000443
444/// OpenCL v2.0, s6.13.17.1 - Check that sizes are provided for all
445/// 'local void*' parameter of passed block.
446static bool checkOpenCLEnqueueVariadicArgs(Sema &S, CallExpr *TheCall,
447 Expr *BlockArg,
448 unsigned NumNonVarArgs) {
449 const BlockPointerType *BPT =
450 cast<BlockPointerType>(BlockArg->getType().getCanonicalType());
451 unsigned NumBlockParams =
452 BPT->getPointeeType()->getAs<FunctionProtoType>()->getNumParams();
453 unsigned TotalNumArgs = TheCall->getNumArgs();
454
455 // For each argument passed to the block, a corresponding uint needs to
456 // be passed to describe the size of the local memory.
457 if (TotalNumArgs != NumBlockParams + NumNonVarArgs) {
458 S.Diag(TheCall->getLocStart(),
459 diag::err_opencl_enqueue_kernel_local_size_args);
460 return true;
461 }
462
463 // Check that the sizes of the local memory are specified by integers.
464 return checkOpenCLEnqueueLocalSizeArgs(S, TheCall, NumNonVarArgs,
465 TotalNumArgs - 1);
466}
467
468/// OpenCL C v2.0, s6.13.17 - Enqueue kernel function contains four different
469/// overload formats specified in Table 6.13.17.1.
470/// int enqueue_kernel(queue_t queue,
471/// kernel_enqueue_flags_t flags,
472/// const ndrange_t ndrange,
473/// void (^block)(void))
474/// int enqueue_kernel(queue_t queue,
475/// kernel_enqueue_flags_t flags,
476/// const ndrange_t ndrange,
477/// uint num_events_in_wait_list,
478/// clk_event_t *event_wait_list,
479/// clk_event_t *event_ret,
480/// void (^block)(void))
481/// int enqueue_kernel(queue_t queue,
482/// kernel_enqueue_flags_t flags,
483/// const ndrange_t ndrange,
484/// void (^block)(local void*, ...),
485/// uint size0, ...)
486/// int enqueue_kernel(queue_t queue,
487/// kernel_enqueue_flags_t flags,
488/// const ndrange_t ndrange,
489/// uint num_events_in_wait_list,
490/// clk_event_t *event_wait_list,
491/// clk_event_t *event_ret,
492/// void (^block)(local void*, ...),
493/// uint size0, ...)
494static bool SemaOpenCLBuiltinEnqueueKernel(Sema &S, CallExpr *TheCall) {
495 unsigned NumArgs = TheCall->getNumArgs();
496
497 if (NumArgs < 4) {
498 S.Diag(TheCall->getLocStart(), diag::err_typecheck_call_too_few_args);
499 return true;
500 }
501
502 Expr *Arg0 = TheCall->getArg(0);
503 Expr *Arg1 = TheCall->getArg(1);
504 Expr *Arg2 = TheCall->getArg(2);
505 Expr *Arg3 = TheCall->getArg(3);
506
507 // First argument always needs to be a queue_t type.
508 if (!Arg0->getType()->isQueueT()) {
509 S.Diag(TheCall->getArg(0)->getLocStart(),
Joey Gouly6b03d952017-07-04 11:50:23 +0000510 diag::err_opencl_builtin_expected_type)
511 << TheCall->getDirectCallee() << S.Context.OCLQueueTy;
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000512 return true;
513 }
514
515 // Second argument always needs to be a kernel_enqueue_flags_t enum value.
516 if (!Arg1->getType()->isIntegerType()) {
517 S.Diag(TheCall->getArg(1)->getLocStart(),
Joey Gouly6b03d952017-07-04 11:50:23 +0000518 diag::err_opencl_builtin_expected_type)
519 << TheCall->getDirectCallee() << "'kernel_enqueue_flags_t' (i.e. uint)";
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000520 return true;
521 }
522
523 // Third argument is always an ndrange_t type.
Anastasia Stulovab42f3c02017-04-21 15:13:24 +0000524 if (Arg2->getType().getUnqualifiedType().getAsString() != "ndrange_t") {
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000525 S.Diag(TheCall->getArg(2)->getLocStart(),
Joey Gouly6b03d952017-07-04 11:50:23 +0000526 diag::err_opencl_builtin_expected_type)
527 << TheCall->getDirectCallee() << "'ndrange_t'";
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000528 return true;
529 }
530
531 // With four arguments, there is only one form that the function could be
532 // called in: no events and no variable arguments.
533 if (NumArgs == 4) {
534 // check that the last argument is the right block type.
535 if (!isBlockPointer(Arg3)) {
Joey Gouly6b03d952017-07-04 11:50:23 +0000536 S.Diag(Arg3->getLocStart(), diag::err_opencl_builtin_expected_type)
537 << TheCall->getDirectCallee() << "block";
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000538 return true;
539 }
540 // we have a block type, check the prototype
541 const BlockPointerType *BPT =
542 cast<BlockPointerType>(Arg3->getType().getCanonicalType());
543 if (BPT->getPointeeType()->getAs<FunctionProtoType>()->getNumParams() > 0) {
544 S.Diag(Arg3->getLocStart(),
545 diag::err_opencl_enqueue_kernel_blocks_no_args);
546 return true;
547 }
548 return false;
549 }
550 // we can have block + varargs.
551 if (isBlockPointer(Arg3))
552 return (checkOpenCLBlockArgs(S, Arg3) ||
553 checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg3, 4));
554 // last two cases with either exactly 7 args or 7 args and varargs.
555 if (NumArgs >= 7) {
556 // check common block argument.
557 Expr *Arg6 = TheCall->getArg(6);
558 if (!isBlockPointer(Arg6)) {
Joey Gouly6b03d952017-07-04 11:50:23 +0000559 S.Diag(Arg6->getLocStart(), diag::err_opencl_builtin_expected_type)
560 << TheCall->getDirectCallee() << "block";
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000561 return true;
562 }
563 if (checkOpenCLBlockArgs(S, Arg6))
564 return true;
565
566 // Forth argument has to be any integer type.
567 if (!Arg3->getType()->isIntegerType()) {
568 S.Diag(TheCall->getArg(3)->getLocStart(),
Joey Gouly6b03d952017-07-04 11:50:23 +0000569 diag::err_opencl_builtin_expected_type)
570 << TheCall->getDirectCallee() << "integer";
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000571 return true;
572 }
573 // check remaining common arguments.
574 Expr *Arg4 = TheCall->getArg(4);
575 Expr *Arg5 = TheCall->getArg(5);
576
Anastasia Stulova2b461202016-11-14 15:34:01 +0000577 // Fifth argument is always passed as a pointer to clk_event_t.
578 if (!Arg4->isNullPointerConstant(S.Context,
579 Expr::NPC_ValueDependentIsNotNull) &&
580 !Arg4->getType()->getPointeeOrArrayElementType()->isClkEventT()) {
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000581 S.Diag(TheCall->getArg(4)->getLocStart(),
Joey Gouly6b03d952017-07-04 11:50:23 +0000582 diag::err_opencl_builtin_expected_type)
583 << TheCall->getDirectCallee()
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000584 << S.Context.getPointerType(S.Context.OCLClkEventTy);
585 return true;
586 }
587
Anastasia Stulova2b461202016-11-14 15:34:01 +0000588 // Sixth argument is always passed as a pointer to clk_event_t.
589 if (!Arg5->isNullPointerConstant(S.Context,
590 Expr::NPC_ValueDependentIsNotNull) &&
591 !(Arg5->getType()->isPointerType() &&
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000592 Arg5->getType()->getPointeeType()->isClkEventT())) {
593 S.Diag(TheCall->getArg(5)->getLocStart(),
Joey Gouly6b03d952017-07-04 11:50:23 +0000594 diag::err_opencl_builtin_expected_type)
595 << TheCall->getDirectCallee()
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +0000596 << S.Context.getPointerType(S.Context.OCLClkEventTy);
597 return true;
598 }
599
600 if (NumArgs == 7)
601 return false;
602
603 return checkOpenCLEnqueueVariadicArgs(S, TheCall, Arg6, 7);
604 }
605
606 // None of the specific case has been detected, give generic error
607 S.Diag(TheCall->getLocStart(),
608 diag::err_opencl_enqueue_kernel_incorrect_args);
609 return true;
610}
611
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000612/// Returns OpenCL access qual.
Xiuli Pan11e13f62016-02-26 03:13:03 +0000613static OpenCLAccessAttr *getOpenCLArgAccess(const Decl *D) {
Xiuli Pan11e13f62016-02-26 03:13:03 +0000614 return D->getAttr<OpenCLAccessAttr>();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000615}
616
617/// Returns true if pipe element type is different from the pointer.
618static bool checkOpenCLPipeArg(Sema &S, CallExpr *Call) {
619 const Expr *Arg0 = Call->getArg(0);
620 // First argument type should always be pipe.
621 if (!Arg0->getType()->isPipeType()) {
622 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_first_arg)
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000623 << Call->getDirectCallee() << Arg0->getSourceRange();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000624 return true;
625 }
Xiuli Pan11e13f62016-02-26 03:13:03 +0000626 OpenCLAccessAttr *AccessQual =
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000627 getOpenCLArgAccess(cast<DeclRefExpr>(Arg0)->getDecl());
628 // Validates the access qualifier is compatible with the call.
629 // OpenCL v2.0 s6.13.16 - The access qualifiers for pipe should only be
630 // read_only and write_only, and assumed to be read_only if no qualifier is
631 // specified.
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000632 switch (Call->getDirectCallee()->getBuiltinID()) {
633 case Builtin::BIread_pipe:
634 case Builtin::BIreserve_read_pipe:
635 case Builtin::BIcommit_read_pipe:
636 case Builtin::BIwork_group_reserve_read_pipe:
637 case Builtin::BIsub_group_reserve_read_pipe:
638 case Builtin::BIwork_group_commit_read_pipe:
639 case Builtin::BIsub_group_commit_read_pipe:
640 if (!(!AccessQual || AccessQual->isReadOnly())) {
641 S.Diag(Arg0->getLocStart(),
642 diag::err_opencl_builtin_pipe_invalid_access_modifier)
643 << "read_only" << Arg0->getSourceRange();
644 return true;
645 }
646 break;
647 case Builtin::BIwrite_pipe:
648 case Builtin::BIreserve_write_pipe:
649 case Builtin::BIcommit_write_pipe:
650 case Builtin::BIwork_group_reserve_write_pipe:
651 case Builtin::BIsub_group_reserve_write_pipe:
652 case Builtin::BIwork_group_commit_write_pipe:
653 case Builtin::BIsub_group_commit_write_pipe:
654 if (!(AccessQual && AccessQual->isWriteOnly())) {
655 S.Diag(Arg0->getLocStart(),
656 diag::err_opencl_builtin_pipe_invalid_access_modifier)
657 << "write_only" << Arg0->getSourceRange();
658 return true;
659 }
660 break;
661 default:
662 break;
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000663 }
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000664 return false;
665}
666
667/// Returns true if pipe element type is different from the pointer.
668static bool checkOpenCLPipePacketType(Sema &S, CallExpr *Call, unsigned Idx) {
669 const Expr *Arg0 = Call->getArg(0);
670 const Expr *ArgIdx = Call->getArg(Idx);
671 const PipeType *PipeTy = cast<PipeType>(Arg0->getType());
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000672 const QualType EltTy = PipeTy->getElementType();
673 const PointerType *ArgTy = ArgIdx->getType()->getAs<PointerType>();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000674 // The Idx argument should be a pointer and the type of the pointer and
675 // the type of pipe element should also be the same.
Xiuli Pan0a1c6c22016-03-30 04:46:32 +0000676 if (!ArgTy ||
677 !S.Context.hasSameType(
678 EltTy, ArgTy->getPointeeType()->getCanonicalTypeInternal())) {
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000679 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg)
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000680 << Call->getDirectCallee() << S.Context.getPointerType(EltTy)
Xiuli Pan0a1c6c22016-03-30 04:46:32 +0000681 << ArgIdx->getType() << ArgIdx->getSourceRange();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000682 return true;
683 }
684 return false;
685}
686
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000687// Performs semantic analysis for the read/write_pipe call.
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000688// \param S Reference to the semantic analyzer.
689// \param Call A pointer to the builtin call.
690// \return True if a semantic error has been found, false otherwise.
691static bool SemaBuiltinRWPipe(Sema &S, CallExpr *Call) {
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000692 // OpenCL v2.0 s6.13.16.2 - The built-in read/write
693 // functions have two forms.
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000694 switch (Call->getNumArgs()) {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +0000695 case 2:
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000696 if (checkOpenCLPipeArg(S, Call))
697 return true;
698 // The call with 2 arguments should be
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000699 // read/write_pipe(pipe T, T*).
700 // Check packet type T.
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000701 if (checkOpenCLPipePacketType(S, Call, 1))
702 return true;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +0000703 break;
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000704
705 case 4: {
706 if (checkOpenCLPipeArg(S, Call))
707 return true;
708 // The call with 4 arguments should be
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000709 // read/write_pipe(pipe T, reserve_id_t, uint, T*).
710 // Check reserve_id_t.
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000711 if (!Call->getArg(1)->getType()->isReserveIDT()) {
712 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg)
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000713 << Call->getDirectCallee() << S.Context.OCLReserveIDTy
Xiuli Pan0a1c6c22016-03-30 04:46:32 +0000714 << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000715 return true;
716 }
717
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000718 // Check the index.
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000719 const Expr *Arg2 = Call->getArg(2);
720 if (!Arg2->getType()->isIntegerType() &&
721 !Arg2->getType()->isUnsignedIntegerType()) {
722 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg)
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000723 << Call->getDirectCallee() << S.Context.UnsignedIntTy
Xiuli Pan0a1c6c22016-03-30 04:46:32 +0000724 << Arg2->getType() << Arg2->getSourceRange();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000725 return true;
726 }
727
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000728 // Check packet type T.
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000729 if (checkOpenCLPipePacketType(S, Call, 3))
730 return true;
731 } break;
732 default:
733 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_arg_num)
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000734 << Call->getDirectCallee() << Call->getSourceRange();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000735 return true;
736 }
737
738 return false;
739}
740
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000741// Performs a semantic analysis on the {work_group_/sub_group_
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000742// /_}reserve_{read/write}_pipe
743// \param S Reference to the semantic analyzer.
744// \param Call The call to the builtin function to be analyzed.
745// \return True if a semantic error was found, false otherwise.
746static bool SemaBuiltinReserveRWPipe(Sema &S, CallExpr *Call) {
747 if (checkArgCount(S, Call, 2))
748 return true;
749
750 if (checkOpenCLPipeArg(S, Call))
751 return true;
752
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000753 // Check the reserve size.
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000754 if (!Call->getArg(1)->getType()->isIntegerType() &&
755 !Call->getArg(1)->getType()->isUnsignedIntegerType()) {
756 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg)
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000757 << Call->getDirectCallee() << S.Context.UnsignedIntTy
Xiuli Pan0a1c6c22016-03-30 04:46:32 +0000758 << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000759 return true;
760 }
761
Joey Gouly922ca232017-08-09 14:52:47 +0000762 // Since return type of reserve_read/write_pipe built-in function is
763 // reserve_id_t, which is not defined in the builtin def file , we used int
764 // as return type and need to override the return type of these functions.
765 Call->setType(S.Context.OCLReserveIDTy);
766
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000767 return false;
768}
769
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000770// Performs a semantic analysis on {work_group_/sub_group_
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000771// /_}commit_{read/write}_pipe
772// \param S Reference to the semantic analyzer.
773// \param Call The call to the builtin function to be analyzed.
774// \return True if a semantic error was found, false otherwise.
775static bool SemaBuiltinCommitRWPipe(Sema &S, CallExpr *Call) {
776 if (checkArgCount(S, Call, 2))
777 return true;
778
779 if (checkOpenCLPipeArg(S, Call))
780 return true;
781
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000782 // Check reserve_id_t.
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000783 if (!Call->getArg(1)->getType()->isReserveIDT()) {
784 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg)
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000785 << Call->getDirectCallee() << S.Context.OCLReserveIDTy
Xiuli Pan0a1c6c22016-03-30 04:46:32 +0000786 << Call->getArg(1)->getType() << Call->getArg(1)->getSourceRange();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000787 return true;
788 }
789
790 return false;
791}
792
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000793// Performs a semantic analysis on the call to built-in Pipe
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000794// Query Functions.
795// \param S Reference to the semantic analyzer.
796// \param Call The call to the builtin function to be analyzed.
797// \return True if a semantic error was found, false otherwise.
798static bool SemaBuiltinPipePackets(Sema &S, CallExpr *Call) {
799 if (checkArgCount(S, Call, 1))
800 return true;
801
802 if (!Call->getArg(0)->getType()->isPipeType()) {
803 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_first_arg)
Xiuli Pan4415bdb2016-03-04 07:11:16 +0000804 << Call->getDirectCallee() << Call->getArg(0)->getSourceRange();
Xiuli Panbb4d8d32016-01-26 04:03:48 +0000805 return true;
806 }
807
808 return false;
809}
Eugene Zelenko11a7ef82017-11-15 22:00:04 +0000810
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000811// OpenCL v2.0 s6.13.9 - Address space qualifier functions.
812// Performs semantic analysis for the to_global/local/private call.
Yaxun Liuf7449a12016-05-20 19:54:38 +0000813// \param S Reference to the semantic analyzer.
814// \param BuiltinID ID of the builtin function.
815// \param Call A pointer to the builtin call.
816// \return True if a semantic error has been found, false otherwise.
817static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID,
818 CallExpr *Call) {
Yaxun Liuf7449a12016-05-20 19:54:38 +0000819 if (Call->getNumArgs() != 1) {
820 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_to_addr_arg_num)
821 << Call->getDirectCallee() << Call->getSourceRange();
822 return true;
823 }
824
825 auto RT = Call->getArg(0)->getType();
826 if (!RT->isPointerType() || RT->getPointeeType()
827 .getAddressSpace() == LangAS::opencl_constant) {
828 S.Diag(Call->getLocStart(), diag::err_opencl_builtin_to_addr_invalid_arg)
829 << Call->getArg(0) << Call->getDirectCallee() << Call->getSourceRange();
830 return true;
831 }
832
833 RT = RT->getPointeeType();
834 auto Qual = RT.getQualifiers();
835 switch (BuiltinID) {
836 case Builtin::BIto_global:
837 Qual.setAddressSpace(LangAS::opencl_global);
838 break;
839 case Builtin::BIto_local:
840 Qual.setAddressSpace(LangAS::opencl_local);
841 break;
Yaxun Liub7318e02017-10-13 03:37:48 +0000842 case Builtin::BIto_private:
843 Qual.setAddressSpace(LangAS::opencl_private);
844 break;
Yaxun Liuf7449a12016-05-20 19:54:38 +0000845 default:
Yaxun Liub7318e02017-10-13 03:37:48 +0000846 llvm_unreachable("Invalid builtin function");
Yaxun Liuf7449a12016-05-20 19:54:38 +0000847 }
848 Call->setType(S.Context.getPointerType(S.Context.getQualifiedType(
849 RT.getUnqualifiedType(), Qual)));
850
851 return false;
852}
853
John McCalldadc5752010-08-24 06:29:42 +0000854ExprResult
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +0000855Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
856 CallExpr *TheCall) {
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000857 ExprResult TheCallResult(TheCall);
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000858
Chris Lattner3be167f2010-10-01 23:23:24 +0000859 // Find out if any arguments are required to be integer constant expressions.
860 unsigned ICEArguments = 0;
861 ASTContext::GetBuiltinTypeError Error;
862 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
863 if (Error != ASTContext::GE_None)
864 ICEArguments = 0; // Don't diagnose previously diagnosed errors.
865
866 // If any arguments are required to be ICE's, check and diagnose.
867 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
868 // Skip arguments not required to be ICE's.
869 if ((ICEArguments & (1 << ArgNo)) == 0) continue;
870
871 llvm::APSInt Result;
872 if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
873 return true;
874 ICEArguments &= ~(1 << ArgNo);
875 }
876
Anders Carlssonbc4c1072009-08-16 01:56:34 +0000877 switch (BuiltinID) {
Chris Lattner43be2e62007-12-19 23:59:04 +0000878 case Builtin::BI__builtin___CFStringMakeConstantString:
Chris Lattner08464942007-12-28 05:29:59 +0000879 assert(TheCall->getNumArgs() == 1 &&
Chris Lattner2da14fb2007-12-20 00:26:33 +0000880 "Wrong # arguments to builtin CFStringMakeConstantString");
Chris Lattner6436fb62009-02-18 06:01:06 +0000881 if (CheckObjCString(TheCall->getArg(0)))
Sebastian Redlc215cfc2009-01-19 00:08:26 +0000882 return ExprError();
Anders Carlssonbc4c1072009-08-16 01:56:34 +0000883 break;
Martin Storsjo022e7822017-07-17 20:49:45 +0000884 case Builtin::BI__builtin_ms_va_start:
Ted Kremeneka174c522008-07-09 17:58:53 +0000885 case Builtin::BI__builtin_stdarg_start:
Chris Lattner43be2e62007-12-19 23:59:04 +0000886 case Builtin::BI__builtin_va_start:
Reid Kleckner2b0fa122017-05-02 20:10:03 +0000887 if (SemaBuiltinVAStart(BuiltinID, TheCall))
Sebastian Redlc215cfc2009-01-19 00:08:26 +0000888 return ExprError();
Anders Carlssonbc4c1072009-08-16 01:56:34 +0000889 break;
Saleem Abdulrasool202aac12014-07-22 02:01:04 +0000890 case Builtin::BI__va_start: {
891 switch (Context.getTargetInfo().getTriple().getArch()) {
892 case llvm::Triple::arm:
893 case llvm::Triple::thumb:
Saleem Abdulrasool3450aa72017-09-26 20:12:04 +0000894 if (SemaBuiltinVAStartARMMicrosoft(TheCall))
Saleem Abdulrasool202aac12014-07-22 02:01:04 +0000895 return ExprError();
896 break;
897 default:
Reid Kleckner2b0fa122017-05-02 20:10:03 +0000898 if (SemaBuiltinVAStart(BuiltinID, TheCall))
Saleem Abdulrasool202aac12014-07-22 02:01:04 +0000899 return ExprError();
900 break;
901 }
902 break;
903 }
Chris Lattner2da14fb2007-12-20 00:26:33 +0000904 case Builtin::BI__builtin_isgreater:
905 case Builtin::BI__builtin_isgreaterequal:
906 case Builtin::BI__builtin_isless:
907 case Builtin::BI__builtin_islessequal:
908 case Builtin::BI__builtin_islessgreater:
909 case Builtin::BI__builtin_isunordered:
Sebastian Redlc215cfc2009-01-19 00:08:26 +0000910 if (SemaBuiltinUnorderedCompare(TheCall))
911 return ExprError();
Anders Carlssonbc4c1072009-08-16 01:56:34 +0000912 break;
Benjamin Kramer634fc102010-02-15 22:42:31 +0000913 case Builtin::BI__builtin_fpclassify:
914 if (SemaBuiltinFPClassification(TheCall, 6))
915 return ExprError();
916 break;
Eli Friedman7e4faac2009-08-31 20:06:00 +0000917 case Builtin::BI__builtin_isfinite:
918 case Builtin::BI__builtin_isinf:
919 case Builtin::BI__builtin_isinf_sign:
920 case Builtin::BI__builtin_isnan:
921 case Builtin::BI__builtin_isnormal:
Benjamin Kramer64aae502010-02-16 10:07:31 +0000922 if (SemaBuiltinFPClassification(TheCall, 1))
Eli Friedman7e4faac2009-08-31 20:06:00 +0000923 return ExprError();
924 break;
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000925 case Builtin::BI__builtin_shufflevector:
Sebastian Redlc215cfc2009-01-19 00:08:26 +0000926 return SemaBuiltinShuffleVector(TheCall);
927 // TheCall will be freed by the smart pointer here, but that's fine, since
928 // SemaBuiltinShuffleVector guts it, but then doesn't release it.
Daniel Dunbarb7257262008-07-21 22:59:13 +0000929 case Builtin::BI__builtin_prefetch:
Sebastian Redlc215cfc2009-01-19 00:08:26 +0000930 if (SemaBuiltinPrefetch(TheCall))
931 return ExprError();
Anders Carlssonbc4c1072009-08-16 01:56:34 +0000932 break;
David Majnemer51169932016-10-31 05:37:48 +0000933 case Builtin::BI__builtin_alloca_with_align:
934 if (SemaBuiltinAllocaWithAlign(TheCall))
935 return ExprError();
936 break;
Hal Finkelf0417332014-07-17 14:25:55 +0000937 case Builtin::BI__assume:
Hal Finkelbcc06082014-09-07 22:58:14 +0000938 case Builtin::BI__builtin_assume:
Hal Finkelf0417332014-07-17 14:25:55 +0000939 if (SemaBuiltinAssume(TheCall))
940 return ExprError();
941 break;
Hal Finkelbcc06082014-09-07 22:58:14 +0000942 case Builtin::BI__builtin_assume_aligned:
943 if (SemaBuiltinAssumeAligned(TheCall))
944 return ExprError();
945 break;
Daniel Dunbarb0d34c82008-09-03 21:13:56 +0000946 case Builtin::BI__builtin_object_size:
Richard Sandiford28940af2014-04-16 08:47:51 +0000947 if (SemaBuiltinConstantArgRange(TheCall, 1, 0, 3))
Sebastian Redlc215cfc2009-01-19 00:08:26 +0000948 return ExprError();
Anders Carlssonbc4c1072009-08-16 01:56:34 +0000949 break;
Eli Friedmaneed8ad22009-05-03 04:46:36 +0000950 case Builtin::BI__builtin_longjmp:
951 if (SemaBuiltinLongjmp(TheCall))
952 return ExprError();
Anders Carlssonbc4c1072009-08-16 01:56:34 +0000953 break;
Joerg Sonnenberger27173282015-03-11 23:46:32 +0000954 case Builtin::BI__builtin_setjmp:
955 if (SemaBuiltinSetjmp(TheCall))
956 return ExprError();
957 break;
David Majnemerc403a1c2015-03-20 17:03:35 +0000958 case Builtin::BI_setjmp:
959 case Builtin::BI_setjmpex:
960 if (checkArgCount(*this, TheCall, 1))
961 return true;
962 break;
John McCallbebede42011-02-26 05:39:39 +0000963 case Builtin::BI__builtin_classify_type:
964 if (checkArgCount(*this, TheCall, 1)) return true;
965 TheCall->setType(Context.IntTy);
966 break;
Chris Lattner17c0eac2010-10-12 17:47:42 +0000967 case Builtin::BI__builtin_constant_p:
John McCallbebede42011-02-26 05:39:39 +0000968 if (checkArgCount(*this, TheCall, 1)) return true;
969 TheCall->setType(Context.IntTy);
Chris Lattner17c0eac2010-10-12 17:47:42 +0000970 break;
Chris Lattnerdc046542009-05-08 06:58:22 +0000971 case Builtin::BI__sync_fetch_and_add:
Douglas Gregor73722482011-11-28 16:30:08 +0000972 case Builtin::BI__sync_fetch_and_add_1:
973 case Builtin::BI__sync_fetch_and_add_2:
974 case Builtin::BI__sync_fetch_and_add_4:
975 case Builtin::BI__sync_fetch_and_add_8:
976 case Builtin::BI__sync_fetch_and_add_16:
Chris Lattnerdc046542009-05-08 06:58:22 +0000977 case Builtin::BI__sync_fetch_and_sub:
Douglas Gregor73722482011-11-28 16:30:08 +0000978 case Builtin::BI__sync_fetch_and_sub_1:
979 case Builtin::BI__sync_fetch_and_sub_2:
980 case Builtin::BI__sync_fetch_and_sub_4:
981 case Builtin::BI__sync_fetch_and_sub_8:
982 case Builtin::BI__sync_fetch_and_sub_16:
Chris Lattnerdc046542009-05-08 06:58:22 +0000983 case Builtin::BI__sync_fetch_and_or:
Douglas Gregor73722482011-11-28 16:30:08 +0000984 case Builtin::BI__sync_fetch_and_or_1:
985 case Builtin::BI__sync_fetch_and_or_2:
986 case Builtin::BI__sync_fetch_and_or_4:
987 case Builtin::BI__sync_fetch_and_or_8:
988 case Builtin::BI__sync_fetch_and_or_16:
Chris Lattnerdc046542009-05-08 06:58:22 +0000989 case Builtin::BI__sync_fetch_and_and:
Douglas Gregor73722482011-11-28 16:30:08 +0000990 case Builtin::BI__sync_fetch_and_and_1:
991 case Builtin::BI__sync_fetch_and_and_2:
992 case Builtin::BI__sync_fetch_and_and_4:
993 case Builtin::BI__sync_fetch_and_and_8:
994 case Builtin::BI__sync_fetch_and_and_16:
Chris Lattnerdc046542009-05-08 06:58:22 +0000995 case Builtin::BI__sync_fetch_and_xor:
Douglas Gregor73722482011-11-28 16:30:08 +0000996 case Builtin::BI__sync_fetch_and_xor_1:
997 case Builtin::BI__sync_fetch_and_xor_2:
998 case Builtin::BI__sync_fetch_and_xor_4:
999 case Builtin::BI__sync_fetch_and_xor_8:
1000 case Builtin::BI__sync_fetch_and_xor_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00001001 case Builtin::BI__sync_fetch_and_nand:
1002 case Builtin::BI__sync_fetch_and_nand_1:
1003 case Builtin::BI__sync_fetch_and_nand_2:
1004 case Builtin::BI__sync_fetch_and_nand_4:
1005 case Builtin::BI__sync_fetch_and_nand_8:
1006 case Builtin::BI__sync_fetch_and_nand_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001007 case Builtin::BI__sync_add_and_fetch:
Douglas Gregor73722482011-11-28 16:30:08 +00001008 case Builtin::BI__sync_add_and_fetch_1:
1009 case Builtin::BI__sync_add_and_fetch_2:
1010 case Builtin::BI__sync_add_and_fetch_4:
1011 case Builtin::BI__sync_add_and_fetch_8:
1012 case Builtin::BI__sync_add_and_fetch_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001013 case Builtin::BI__sync_sub_and_fetch:
Douglas Gregor73722482011-11-28 16:30:08 +00001014 case Builtin::BI__sync_sub_and_fetch_1:
1015 case Builtin::BI__sync_sub_and_fetch_2:
1016 case Builtin::BI__sync_sub_and_fetch_4:
1017 case Builtin::BI__sync_sub_and_fetch_8:
1018 case Builtin::BI__sync_sub_and_fetch_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001019 case Builtin::BI__sync_and_and_fetch:
Douglas Gregor73722482011-11-28 16:30:08 +00001020 case Builtin::BI__sync_and_and_fetch_1:
1021 case Builtin::BI__sync_and_and_fetch_2:
1022 case Builtin::BI__sync_and_and_fetch_4:
1023 case Builtin::BI__sync_and_and_fetch_8:
1024 case Builtin::BI__sync_and_and_fetch_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001025 case Builtin::BI__sync_or_and_fetch:
Douglas Gregor73722482011-11-28 16:30:08 +00001026 case Builtin::BI__sync_or_and_fetch_1:
1027 case Builtin::BI__sync_or_and_fetch_2:
1028 case Builtin::BI__sync_or_and_fetch_4:
1029 case Builtin::BI__sync_or_and_fetch_8:
1030 case Builtin::BI__sync_or_and_fetch_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001031 case Builtin::BI__sync_xor_and_fetch:
Douglas Gregor73722482011-11-28 16:30:08 +00001032 case Builtin::BI__sync_xor_and_fetch_1:
1033 case Builtin::BI__sync_xor_and_fetch_2:
1034 case Builtin::BI__sync_xor_and_fetch_4:
1035 case Builtin::BI__sync_xor_and_fetch_8:
1036 case Builtin::BI__sync_xor_and_fetch_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00001037 case Builtin::BI__sync_nand_and_fetch:
1038 case Builtin::BI__sync_nand_and_fetch_1:
1039 case Builtin::BI__sync_nand_and_fetch_2:
1040 case Builtin::BI__sync_nand_and_fetch_4:
1041 case Builtin::BI__sync_nand_and_fetch_8:
1042 case Builtin::BI__sync_nand_and_fetch_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001043 case Builtin::BI__sync_val_compare_and_swap:
Douglas Gregor73722482011-11-28 16:30:08 +00001044 case Builtin::BI__sync_val_compare_and_swap_1:
1045 case Builtin::BI__sync_val_compare_and_swap_2:
1046 case Builtin::BI__sync_val_compare_and_swap_4:
1047 case Builtin::BI__sync_val_compare_and_swap_8:
1048 case Builtin::BI__sync_val_compare_and_swap_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001049 case Builtin::BI__sync_bool_compare_and_swap:
Douglas Gregor73722482011-11-28 16:30:08 +00001050 case Builtin::BI__sync_bool_compare_and_swap_1:
1051 case Builtin::BI__sync_bool_compare_and_swap_2:
1052 case Builtin::BI__sync_bool_compare_and_swap_4:
1053 case Builtin::BI__sync_bool_compare_and_swap_8:
1054 case Builtin::BI__sync_bool_compare_and_swap_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001055 case Builtin::BI__sync_lock_test_and_set:
Douglas Gregor73722482011-11-28 16:30:08 +00001056 case Builtin::BI__sync_lock_test_and_set_1:
1057 case Builtin::BI__sync_lock_test_and_set_2:
1058 case Builtin::BI__sync_lock_test_and_set_4:
1059 case Builtin::BI__sync_lock_test_and_set_8:
1060 case Builtin::BI__sync_lock_test_and_set_16:
Chris Lattnerdc046542009-05-08 06:58:22 +00001061 case Builtin::BI__sync_lock_release:
Douglas Gregor73722482011-11-28 16:30:08 +00001062 case Builtin::BI__sync_lock_release_1:
1063 case Builtin::BI__sync_lock_release_2:
1064 case Builtin::BI__sync_lock_release_4:
1065 case Builtin::BI__sync_lock_release_8:
1066 case Builtin::BI__sync_lock_release_16:
Chris Lattner9cb59fa2011-04-09 03:57:26 +00001067 case Builtin::BI__sync_swap:
Douglas Gregor73722482011-11-28 16:30:08 +00001068 case Builtin::BI__sync_swap_1:
1069 case Builtin::BI__sync_swap_2:
1070 case Builtin::BI__sync_swap_4:
1071 case Builtin::BI__sync_swap_8:
1072 case Builtin::BI__sync_swap_16:
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001073 return SemaBuiltinAtomicOverloaded(TheCallResult);
Michael Zolotukhin84df1232015-09-08 23:52:33 +00001074 case Builtin::BI__builtin_nontemporal_load:
1075 case Builtin::BI__builtin_nontemporal_store:
1076 return SemaBuiltinNontemporalOverloaded(TheCallResult);
Richard Smithfeea8832012-04-12 05:08:17 +00001077#define BUILTIN(ID, TYPE, ATTRS)
1078#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
1079 case Builtin::BI##ID: \
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001080 return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
Richard Smithfeea8832012-04-12 05:08:17 +00001081#include "clang/Basic/Builtins.def"
Reid Kleckner30701ed2017-09-05 20:27:35 +00001082 case Builtin::BI__annotation:
1083 if (SemaBuiltinMSVCAnnotation(*this, TheCall))
1084 return ExprError();
1085 break;
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001086 case Builtin::BI__builtin_annotation:
Julien Lerouge4a5b4442012-04-28 17:39:16 +00001087 if (SemaBuiltinAnnotation(*this, TheCall))
Julien Lerouge5a6b6982011-09-09 22:41:49 +00001088 return ExprError();
1089 break;
Richard Smith6cbd65d2013-07-11 02:27:57 +00001090 case Builtin::BI__builtin_addressof:
1091 if (SemaBuiltinAddressof(*this, TheCall))
1092 return ExprError();
1093 break;
John McCall03107a42015-10-29 20:48:01 +00001094 case Builtin::BI__builtin_add_overflow:
1095 case Builtin::BI__builtin_sub_overflow:
1096 case Builtin::BI__builtin_mul_overflow:
Craig Toppera86e70d2015-11-07 06:16:14 +00001097 if (SemaBuiltinOverflow(*this, TheCall))
1098 return ExprError();
1099 break;
Richard Smith760520b2014-06-03 23:27:44 +00001100 case Builtin::BI__builtin_operator_new:
Eric Fiselierfa752f22018-03-21 19:19:48 +00001101 case Builtin::BI__builtin_operator_delete: {
1102 bool IsDelete = BuiltinID == Builtin::BI__builtin_operator_delete;
1103 ExprResult Res =
1104 SemaBuiltinOperatorNewDeleteOverloaded(TheCallResult, IsDelete);
1105 if (Res.isInvalid())
1106 CorrectDelayedTyposInExpr(TheCallResult.get());
1107 return Res;
1108 }
Aaron Ballman06525342018-04-10 21:58:13 +00001109 case Builtin::BI__builtin_dump_struct: {
1110 // We first want to ensure we are called with 2 arguments
1111 if (checkArgCount(*this, TheCall, 2))
1112 return ExprError();
1113 // Ensure that the first argument is of type 'struct XX *'
1114 const Expr *PtrArg = TheCall->getArg(0)->IgnoreParenImpCasts();
1115 const QualType PtrArgType = PtrArg->getType();
1116 if (!PtrArgType->isPointerType() ||
1117 !PtrArgType->getPointeeType()->isRecordType()) {
1118 Diag(PtrArg->getLocStart(), diag::err_typecheck_convert_incompatible)
1119 << PtrArgType << "structure pointer" << 1 << 0 << 3 << 1 << PtrArgType
1120 << "structure pointer";
1121 return ExprError();
1122 }
1123
1124 // Ensure that the second argument is of type 'FunctionType'
1125 const Expr *FnPtrArg = TheCall->getArg(1)->IgnoreImpCasts();
1126 const QualType FnPtrArgType = FnPtrArg->getType();
1127 if (!FnPtrArgType->isPointerType()) {
1128 Diag(FnPtrArg->getLocStart(), diag::err_typecheck_convert_incompatible)
1129 << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3
1130 << 2 << FnPtrArgType << "'int (*)(const char *, ...)'";
1131 return ExprError();
1132 }
1133
1134 const auto *FuncType =
1135 FnPtrArgType->getPointeeType()->getAs<FunctionType>();
1136
1137 if (!FuncType) {
1138 Diag(FnPtrArg->getLocStart(), diag::err_typecheck_convert_incompatible)
1139 << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3
1140 << 2 << FnPtrArgType << "'int (*)(const char *, ...)'";
1141 return ExprError();
1142 }
1143
1144 if (const auto *FT = dyn_cast<FunctionProtoType>(FuncType)) {
1145 if (!FT->getNumParams()) {
1146 Diag(FnPtrArg->getLocStart(), diag::err_typecheck_convert_incompatible)
1147 << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3
1148 << 2 << FnPtrArgType << "'int (*)(const char *, ...)'";
1149 return ExprError();
1150 }
1151 QualType PT = FT->getParamType(0);
1152 if (!FT->isVariadic() || FT->getReturnType() != Context.IntTy ||
1153 !PT->isPointerType() || !PT->getPointeeType()->isCharType() ||
1154 !PT->getPointeeType().isConstQualified()) {
1155 Diag(FnPtrArg->getLocStart(), diag::err_typecheck_convert_incompatible)
1156 << FnPtrArgType << "'int (*)(const char *, ...)'" << 1 << 0 << 3
1157 << 2 << FnPtrArgType << "'int (*)(const char *, ...)'";
1158 return ExprError();
1159 }
1160 }
1161
1162 TheCall->setType(Context.IntTy);
1163 break;
1164 }
1165
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +00001166 // check secure string manipulation functions where overflows
1167 // are detectable at compile time
1168 case Builtin::BI__builtin___memcpy_chk:
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +00001169 case Builtin::BI__builtin___memmove_chk:
1170 case Builtin::BI__builtin___memset_chk:
1171 case Builtin::BI__builtin___strlcat_chk:
1172 case Builtin::BI__builtin___strlcpy_chk:
1173 case Builtin::BI__builtin___strncat_chk:
1174 case Builtin::BI__builtin___strncpy_chk:
1175 case Builtin::BI__builtin___stpncpy_chk:
1176 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 2, 3);
1177 break;
Steven Wu566c14e2014-09-24 04:37:33 +00001178 case Builtin::BI__builtin___memccpy_chk:
1179 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 3, 4);
1180 break;
Fariborz Jahanian3e6a0be2014-09-18 17:58:27 +00001181 case Builtin::BI__builtin___snprintf_chk:
1182 case Builtin::BI__builtin___vsnprintf_chk:
1183 SemaBuiltinMemChkCall(*this, FDecl, TheCall, 1, 3);
1184 break;
Peter Collingbournef7706832014-12-12 23:41:25 +00001185 case Builtin::BI__builtin_call_with_static_chain:
1186 if (SemaBuiltinCallWithStaticChain(*this, TheCall))
1187 return ExprError();
1188 break;
Reid Kleckner1d59f992015-01-22 01:36:17 +00001189 case Builtin::BI__exception_code:
Eugene Zelenko1ced5092016-02-12 22:53:10 +00001190 case Builtin::BI_exception_code:
Reid Kleckner1d59f992015-01-22 01:36:17 +00001191 if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHExceptScope,
1192 diag::err_seh___except_block))
1193 return ExprError();
1194 break;
Reid Kleckner1d59f992015-01-22 01:36:17 +00001195 case Builtin::BI__exception_info:
Eugene Zelenko1ced5092016-02-12 22:53:10 +00001196 case Builtin::BI_exception_info:
Reid Kleckner1d59f992015-01-22 01:36:17 +00001197 if (SemaBuiltinSEHScopeCheck(*this, TheCall, Scope::SEHFilterScope,
1198 diag::err_seh___except_filter))
1199 return ExprError();
1200 break;
David Majnemerba3e5ec2015-03-13 18:26:17 +00001201 case Builtin::BI__GetExceptionInfo:
1202 if (checkArgCount(*this, TheCall, 1))
1203 return ExprError();
1204
1205 if (CheckCXXThrowOperand(
1206 TheCall->getLocStart(),
1207 Context.getExceptionObjectType(FDecl->getParamDecl(0)->getType()),
1208 TheCall))
1209 return ExprError();
1210
1211 TheCall->setType(Context.VoidPtrTy);
1212 break;
Anastasia Stulova7f8d6dc2016-07-04 16:07:18 +00001213 // OpenCL v2.0, s6.13.16 - Pipe functions
Xiuli Panbb4d8d32016-01-26 04:03:48 +00001214 case Builtin::BIread_pipe:
1215 case Builtin::BIwrite_pipe:
1216 // Since those two functions are declared with var args, we need a semantic
1217 // check for the argument.
1218 if (SemaBuiltinRWPipe(*this, TheCall))
1219 return ExprError();
Alexey Baderaf17c792016-09-07 10:32:03 +00001220 TheCall->setType(Context.IntTy);
Xiuli Panbb4d8d32016-01-26 04:03:48 +00001221 break;
1222 case Builtin::BIreserve_read_pipe:
1223 case Builtin::BIreserve_write_pipe:
1224 case Builtin::BIwork_group_reserve_read_pipe:
1225 case Builtin::BIwork_group_reserve_write_pipe:
Joey Gouly84ae3362017-07-31 15:15:59 +00001226 if (SemaBuiltinReserveRWPipe(*this, TheCall))
1227 return ExprError();
Joey Gouly84ae3362017-07-31 15:15:59 +00001228 break;
Xiuli Panbb4d8d32016-01-26 04:03:48 +00001229 case Builtin::BIsub_group_reserve_read_pipe:
1230 case Builtin::BIsub_group_reserve_write_pipe:
Joey Gouly84ae3362017-07-31 15:15:59 +00001231 if (checkOpenCLSubgroupExt(*this, TheCall) ||
1232 SemaBuiltinReserveRWPipe(*this, TheCall))
Xiuli Panbb4d8d32016-01-26 04:03:48 +00001233 return ExprError();
Xiuli Panbb4d8d32016-01-26 04:03:48 +00001234 break;
1235 case Builtin::BIcommit_read_pipe:
1236 case Builtin::BIcommit_write_pipe:
1237 case Builtin::BIwork_group_commit_read_pipe:
1238 case Builtin::BIwork_group_commit_write_pipe:
Joey Gouly84ae3362017-07-31 15:15:59 +00001239 if (SemaBuiltinCommitRWPipe(*this, TheCall))
1240 return ExprError();
1241 break;
Xiuli Panbb4d8d32016-01-26 04:03:48 +00001242 case Builtin::BIsub_group_commit_read_pipe:
1243 case Builtin::BIsub_group_commit_write_pipe:
Joey Gouly84ae3362017-07-31 15:15:59 +00001244 if (checkOpenCLSubgroupExt(*this, TheCall) ||
1245 SemaBuiltinCommitRWPipe(*this, TheCall))
Xiuli Panbb4d8d32016-01-26 04:03:48 +00001246 return ExprError();
1247 break;
1248 case Builtin::BIget_pipe_num_packets:
1249 case Builtin::BIget_pipe_max_packets:
1250 if (SemaBuiltinPipePackets(*this, TheCall))
1251 return ExprError();
Alexey Baderaf17c792016-09-07 10:32:03 +00001252 TheCall->setType(Context.UnsignedIntTy);
Xiuli Panbb4d8d32016-01-26 04:03:48 +00001253 break;
Yaxun Liuf7449a12016-05-20 19:54:38 +00001254 case Builtin::BIto_global:
1255 case Builtin::BIto_local:
1256 case Builtin::BIto_private:
1257 if (SemaOpenCLBuiltinToAddr(*this, BuiltinID, TheCall))
1258 return ExprError();
1259 break;
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +00001260 // OpenCL v2.0, s6.13.17 - Enqueue kernel functions.
1261 case Builtin::BIenqueue_kernel:
1262 if (SemaOpenCLBuiltinEnqueueKernel(*this, TheCall))
1263 return ExprError();
1264 break;
1265 case Builtin::BIget_kernel_work_group_size:
1266 case Builtin::BIget_kernel_preferred_work_group_size_multiple:
1267 if (SemaOpenCLBuiltinKernelWorkGroupSize(*this, TheCall))
1268 return ExprError();
Mehdi Amini06d367c2016-10-24 20:39:34 +00001269 break;
Joey Goulyfa76b492017-08-01 13:27:09 +00001270 case Builtin::BIget_kernel_max_sub_group_size_for_ndrange:
1271 case Builtin::BIget_kernel_sub_group_count_for_ndrange:
1272 if (SemaOpenCLBuiltinNDRangeAndBlock(*this, TheCall))
1273 return ExprError();
1274 break;
Mehdi Amini06d367c2016-10-24 20:39:34 +00001275 case Builtin::BI__builtin_os_log_format:
1276 case Builtin::BI__builtin_os_log_format_buffer_size:
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00001277 if (SemaBuiltinOSLogFormat(TheCall))
Mehdi Amini06d367c2016-10-24 20:39:34 +00001278 return ExprError();
Mehdi Amini06d367c2016-10-24 20:39:34 +00001279 break;
Nate Begeman4904e322010-06-08 02:47:44 +00001280 }
Richard Smith760520b2014-06-03 23:27:44 +00001281
Nate Begeman4904e322010-06-08 02:47:44 +00001282 // Since the target specific builtins for each arch overlap, only check those
1283 // of the arch we are compiling for.
Artem Belevich9674a642015-09-22 17:23:05 +00001284 if (Context.BuiltinInfo.isTSBuiltin(BuiltinID)) {
Douglas Gregore8bbc122011-09-02 00:18:52 +00001285 switch (Context.getTargetInfo().getTriple().getArch()) {
Nate Begeman4904e322010-06-08 02:47:44 +00001286 case llvm::Triple::arm:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00001287 case llvm::Triple::armeb:
Nate Begeman4904e322010-06-08 02:47:44 +00001288 case llvm::Triple::thumb:
Christian Pirkerf01cd6f2014-03-28 14:40:46 +00001289 case llvm::Triple::thumbeb:
Nate Begeman4904e322010-06-08 02:47:44 +00001290 if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
1291 return ExprError();
1292 break;
Tim Northover25e8a672014-05-24 12:51:25 +00001293 case llvm::Triple::aarch64:
1294 case llvm::Triple::aarch64_be:
Tim Northover573cbee2014-05-24 12:52:07 +00001295 if (CheckAArch64BuiltinFunctionCall(BuiltinID, TheCall))
Tim Northovera2ee4332014-03-29 15:09:45 +00001296 return ExprError();
1297 break;
Krzysztof Parzyszek45850682018-05-11 16:41:51 +00001298 case llvm::Triple::hexagon:
1299 if (CheckHexagonBuiltinFunctionCall(BuiltinID, TheCall))
1300 return ExprError();
1301 break;
Simon Atanasyanecedf3d2012-07-08 09:30:00 +00001302 case llvm::Triple::mips:
1303 case llvm::Triple::mipsel:
1304 case llvm::Triple::mips64:
1305 case llvm::Triple::mips64el:
1306 if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall))
1307 return ExprError();
1308 break;
Ulrich Weigand3a610eb2015-04-01 12:54:25 +00001309 case llvm::Triple::systemz:
1310 if (CheckSystemZBuiltinFunctionCall(BuiltinID, TheCall))
1311 return ExprError();
1312 break;
Warren Hunt20e4a5d2014-02-21 23:08:53 +00001313 case llvm::Triple::x86:
1314 case llvm::Triple::x86_64:
1315 if (CheckX86BuiltinFunctionCall(BuiltinID, TheCall))
1316 return ExprError();
1317 break;
Kit Bartone50adcb2015-03-30 19:40:59 +00001318 case llvm::Triple::ppc:
1319 case llvm::Triple::ppc64:
1320 case llvm::Triple::ppc64le:
1321 if (CheckPPCBuiltinFunctionCall(BuiltinID, TheCall))
1322 return ExprError();
1323 break;
Nate Begeman4904e322010-06-08 02:47:44 +00001324 default:
1325 break;
1326 }
1327 }
1328
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001329 return TheCallResult;
Nate Begeman4904e322010-06-08 02:47:44 +00001330}
1331
Nate Begeman91e1fea2010-06-14 05:21:25 +00001332// Get the valid immediate range for the specified NEON type code.
Tim Northover3402dc72014-02-12 12:04:59 +00001333static unsigned RFT(unsigned t, bool shift = false, bool ForceQuad = false) {
Bob Wilson98bc98c2011-11-08 01:16:11 +00001334 NeonTypeFlags Type(t);
Tim Northover3402dc72014-02-12 12:04:59 +00001335 int IsQuad = ForceQuad ? true : Type.isQuad();
Bob Wilson98bc98c2011-11-08 01:16:11 +00001336 switch (Type.getEltType()) {
1337 case NeonTypeFlags::Int8:
1338 case NeonTypeFlags::Poly8:
1339 return shift ? 7 : (8 << IsQuad) - 1;
1340 case NeonTypeFlags::Int16:
1341 case NeonTypeFlags::Poly16:
1342 return shift ? 15 : (4 << IsQuad) - 1;
1343 case NeonTypeFlags::Int32:
1344 return shift ? 31 : (2 << IsQuad) - 1;
1345 case NeonTypeFlags::Int64:
Kevin Qincaac85e2013-11-14 03:29:16 +00001346 case NeonTypeFlags::Poly64:
Bob Wilson98bc98c2011-11-08 01:16:11 +00001347 return shift ? 63 : (1 << IsQuad) - 1;
Kevin Qinfb79d7f2013-12-10 06:49:01 +00001348 case NeonTypeFlags::Poly128:
1349 return shift ? 127 : (1 << IsQuad) - 1;
Bob Wilson98bc98c2011-11-08 01:16:11 +00001350 case NeonTypeFlags::Float16:
1351 assert(!shift && "cannot shift float types!");
1352 return (4 << IsQuad) - 1;
1353 case NeonTypeFlags::Float32:
1354 assert(!shift && "cannot shift float types!");
1355 return (2 << IsQuad) - 1;
Tim Northover2fe823a2013-08-01 09:23:19 +00001356 case NeonTypeFlags::Float64:
1357 assert(!shift && "cannot shift float types!");
1358 return (1 << IsQuad) - 1;
Nate Begeman91e1fea2010-06-14 05:21:25 +00001359 }
David Blaikie8a40f702012-01-17 06:56:22 +00001360 llvm_unreachable("Invalid NeonTypeFlag!");
Nate Begeman91e1fea2010-06-14 05:21:25 +00001361}
1362
Bob Wilsone4d77232011-11-08 05:04:11 +00001363/// getNeonEltType - Return the QualType corresponding to the elements of
1364/// the vector type specified by the NeonTypeFlags. This is used to check
1365/// the pointer arguments for Neon load/store intrinsics.
Kevin Qincaac85e2013-11-14 03:29:16 +00001366static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context,
Tim Northovera2ee4332014-03-29 15:09:45 +00001367 bool IsPolyUnsigned, bool IsInt64Long) {
Bob Wilsone4d77232011-11-08 05:04:11 +00001368 switch (Flags.getEltType()) {
1369 case NeonTypeFlags::Int8:
1370 return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy;
1371 case NeonTypeFlags::Int16:
1372 return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy;
1373 case NeonTypeFlags::Int32:
1374 return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy;
1375 case NeonTypeFlags::Int64:
Tim Northovera2ee4332014-03-29 15:09:45 +00001376 if (IsInt64Long)
Kevin Qinad64f6d2014-02-24 02:45:03 +00001377 return Flags.isUnsigned() ? Context.UnsignedLongTy : Context.LongTy;
1378 else
1379 return Flags.isUnsigned() ? Context.UnsignedLongLongTy
1380 : Context.LongLongTy;
Bob Wilsone4d77232011-11-08 05:04:11 +00001381 case NeonTypeFlags::Poly8:
Tim Northovera2ee4332014-03-29 15:09:45 +00001382 return IsPolyUnsigned ? Context.UnsignedCharTy : Context.SignedCharTy;
Bob Wilsone4d77232011-11-08 05:04:11 +00001383 case NeonTypeFlags::Poly16:
Tim Northovera2ee4332014-03-29 15:09:45 +00001384 return IsPolyUnsigned ? Context.UnsignedShortTy : Context.ShortTy;
Kevin Qincaac85e2013-11-14 03:29:16 +00001385 case NeonTypeFlags::Poly64:
Kevin Qin78b86532015-05-14 08:18:05 +00001386 if (IsInt64Long)
1387 return Context.UnsignedLongTy;
1388 else
1389 return Context.UnsignedLongLongTy;
Kevin Qinfb79d7f2013-12-10 06:49:01 +00001390 case NeonTypeFlags::Poly128:
1391 break;
Bob Wilsone4d77232011-11-08 05:04:11 +00001392 case NeonTypeFlags::Float16:
Kevin Qincaac85e2013-11-14 03:29:16 +00001393 return Context.HalfTy;
Bob Wilsone4d77232011-11-08 05:04:11 +00001394 case NeonTypeFlags::Float32:
1395 return Context.FloatTy;
Tim Northover2fe823a2013-08-01 09:23:19 +00001396 case NeonTypeFlags::Float64:
1397 return Context.DoubleTy;
Bob Wilsone4d77232011-11-08 05:04:11 +00001398 }
David Blaikie8a40f702012-01-17 06:56:22 +00001399 llvm_unreachable("Invalid NeonTypeFlag!");
Bob Wilsone4d77232011-11-08 05:04:11 +00001400}
1401
Tim Northover12670412014-02-19 10:37:05 +00001402bool Sema::CheckNeonBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
Tim Northover2fe823a2013-08-01 09:23:19 +00001403 llvm::APSInt Result;
Tim Northover2fe823a2013-08-01 09:23:19 +00001404 uint64_t mask = 0;
1405 unsigned TV = 0;
1406 int PtrArgNum = -1;
1407 bool HasConstPtr = false;
1408 switch (BuiltinID) {
Tim Northover12670412014-02-19 10:37:05 +00001409#define GET_NEON_OVERLOAD_CHECK
Tim Northover2fe823a2013-08-01 09:23:19 +00001410#include "clang/Basic/arm_neon.inc"
Abderrazek Zaafranice8746d2018-01-19 23:11:18 +00001411#include "clang/Basic/arm_fp16.inc"
Tim Northover12670412014-02-19 10:37:05 +00001412#undef GET_NEON_OVERLOAD_CHECK
Tim Northover2fe823a2013-08-01 09:23:19 +00001413 }
1414
1415 // For NEON intrinsics which are overloaded on vector element type, validate
1416 // the immediate which specifies which variant to emit.
Tim Northover12670412014-02-19 10:37:05 +00001417 unsigned ImmArg = TheCall->getNumArgs()-1;
Tim Northover2fe823a2013-08-01 09:23:19 +00001418 if (mask) {
1419 if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
1420 return true;
1421
1422 TV = Result.getLimitedValue(64);
1423 if ((TV > 63) || (mask & (1ULL << TV)) == 0)
1424 return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
Tim Northover12670412014-02-19 10:37:05 +00001425 << TheCall->getArg(ImmArg)->getSourceRange();
Tim Northover2fe823a2013-08-01 09:23:19 +00001426 }
1427
1428 if (PtrArgNum >= 0) {
1429 // Check that pointer arguments have the specified type.
1430 Expr *Arg = TheCall->getArg(PtrArgNum);
1431 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
1432 Arg = ICE->getSubExpr();
1433 ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
1434 QualType RHSTy = RHS.get()->getType();
Tim Northover12670412014-02-19 10:37:05 +00001435
Tim Northovera2ee4332014-03-29 15:09:45 +00001436 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
Joerg Sonnenberger47006c52017-01-09 11:40:41 +00001437 bool IsPolyUnsigned = Arch == llvm::Triple::aarch64 ||
1438 Arch == llvm::Triple::aarch64_be;
Tim Northovera2ee4332014-03-29 15:09:45 +00001439 bool IsInt64Long =
1440 Context.getTargetInfo().getInt64Type() == TargetInfo::SignedLong;
1441 QualType EltTy =
1442 getNeonEltType(NeonTypeFlags(TV), Context, IsPolyUnsigned, IsInt64Long);
Tim Northover2fe823a2013-08-01 09:23:19 +00001443 if (HasConstPtr)
1444 EltTy = EltTy.withConst();
1445 QualType LHSTy = Context.getPointerType(EltTy);
1446 AssignConvertType ConvTy;
1447 ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
1448 if (RHS.isInvalid())
1449 return true;
1450 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy,
1451 RHS.get(), AA_Assigning))
1452 return true;
1453 }
1454
1455 // For NEON intrinsics which take an immediate value as part of the
1456 // instruction, range check them here.
1457 unsigned i = 0, l = 0, u = 0;
1458 switch (BuiltinID) {
1459 default:
1460 return false;
Tim Northover12670412014-02-19 10:37:05 +00001461#define GET_NEON_IMMEDIATE_CHECK
Tim Northover2fe823a2013-08-01 09:23:19 +00001462#include "clang/Basic/arm_neon.inc"
Abderrazek Zaafranice8746d2018-01-19 23:11:18 +00001463#include "clang/Basic/arm_fp16.inc"
Tim Northover12670412014-02-19 10:37:05 +00001464#undef GET_NEON_IMMEDIATE_CHECK
Tim Northover2fe823a2013-08-01 09:23:19 +00001465 }
Tim Northover2fe823a2013-08-01 09:23:19 +00001466
Richard Sandiford28940af2014-04-16 08:47:51 +00001467 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
Tim Northover2fe823a2013-08-01 09:23:19 +00001468}
1469
Tim Northovera2ee4332014-03-29 15:09:45 +00001470bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall,
1471 unsigned MaxWidth) {
Tim Northover6aacd492013-07-16 09:47:53 +00001472 assert((BuiltinID == ARM::BI__builtin_arm_ldrex ||
Tim Northover3acd6bd2014-07-02 12:56:02 +00001473 BuiltinID == ARM::BI__builtin_arm_ldaex ||
Tim Northovera2ee4332014-03-29 15:09:45 +00001474 BuiltinID == ARM::BI__builtin_arm_strex ||
Tim Northover3acd6bd2014-07-02 12:56:02 +00001475 BuiltinID == ARM::BI__builtin_arm_stlex ||
Tim Northover573cbee2014-05-24 12:52:07 +00001476 BuiltinID == AArch64::BI__builtin_arm_ldrex ||
Tim Northover3acd6bd2014-07-02 12:56:02 +00001477 BuiltinID == AArch64::BI__builtin_arm_ldaex ||
1478 BuiltinID == AArch64::BI__builtin_arm_strex ||
1479 BuiltinID == AArch64::BI__builtin_arm_stlex) &&
Tim Northover6aacd492013-07-16 09:47:53 +00001480 "unexpected ARM builtin");
Tim Northovera2ee4332014-03-29 15:09:45 +00001481 bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex ||
Tim Northover3acd6bd2014-07-02 12:56:02 +00001482 BuiltinID == ARM::BI__builtin_arm_ldaex ||
1483 BuiltinID == AArch64::BI__builtin_arm_ldrex ||
1484 BuiltinID == AArch64::BI__builtin_arm_ldaex;
Tim Northover6aacd492013-07-16 09:47:53 +00001485
1486 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1487
1488 // Ensure that we have the proper number of arguments.
1489 if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2))
1490 return true;
1491
1492 // Inspect the pointer argument of the atomic builtin. This should always be
1493 // a pointer type, whose element is an integral scalar or pointer type.
1494 // Because it is a pointer type, we don't have to worry about any implicit
1495 // casts here.
1496 Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1);
1497 ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg);
1498 if (PointerArgRes.isInvalid())
1499 return true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001500 PointerArg = PointerArgRes.get();
Tim Northover6aacd492013-07-16 09:47:53 +00001501
1502 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
1503 if (!pointerType) {
1504 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
1505 << PointerArg->getType() << PointerArg->getSourceRange();
1506 return true;
1507 }
1508
1509 // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next
1510 // task is to insert the appropriate casts into the AST. First work out just
1511 // what the appropriate type is.
1512 QualType ValType = pointerType->getPointeeType();
1513 QualType AddrType = ValType.getUnqualifiedType().withVolatile();
1514 if (IsLdrex)
1515 AddrType.addConst();
1516
1517 // Issue a warning if the cast is dodgy.
1518 CastKind CastNeeded = CK_NoOp;
1519 if (!AddrType.isAtLeastAsQualifiedAs(ValType)) {
1520 CastNeeded = CK_BitCast;
1521 Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers)
1522 << PointerArg->getType()
1523 << Context.getPointerType(AddrType)
1524 << AA_Passing << PointerArg->getSourceRange();
1525 }
1526
1527 // Finally, do the cast and replace the argument with the corrected version.
1528 AddrType = Context.getPointerType(AddrType);
1529 PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded);
1530 if (PointerArgRes.isInvalid())
1531 return true;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001532 PointerArg = PointerArgRes.get();
Tim Northover6aacd492013-07-16 09:47:53 +00001533
1534 TheCall->setArg(IsLdrex ? 0 : 1, PointerArg);
1535
1536 // In general, we allow ints, floats and pointers to be loaded and stored.
1537 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
1538 !ValType->isBlockPointerType() && !ValType->isFloatingType()) {
1539 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr)
1540 << PointerArg->getType() << PointerArg->getSourceRange();
1541 return true;
1542 }
1543
1544 // But ARM doesn't have instructions to deal with 128-bit versions.
Tim Northovera2ee4332014-03-29 15:09:45 +00001545 if (Context.getTypeSize(ValType) > MaxWidth) {
1546 assert(MaxWidth == 64 && "Diagnostic unexpectedly inaccurate");
Tim Northover6aacd492013-07-16 09:47:53 +00001547 Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size)
1548 << PointerArg->getType() << PointerArg->getSourceRange();
1549 return true;
1550 }
1551
1552 switch (ValType.getObjCLifetime()) {
1553 case Qualifiers::OCL_None:
1554 case Qualifiers::OCL_ExplicitNone:
1555 // okay
1556 break;
1557
1558 case Qualifiers::OCL_Weak:
1559 case Qualifiers::OCL_Strong:
1560 case Qualifiers::OCL_Autoreleasing:
1561 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
1562 << ValType << PointerArg->getSourceRange();
1563 return true;
1564 }
1565
Tim Northover6aacd492013-07-16 09:47:53 +00001566 if (IsLdrex) {
1567 TheCall->setType(ValType);
1568 return false;
1569 }
1570
1571 // Initialize the argument to be stored.
1572 ExprResult ValArg = TheCall->getArg(0);
1573 InitializedEntity Entity = InitializedEntity::InitializeParameter(
1574 Context, ValType, /*consume*/ false);
1575 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
1576 if (ValArg.isInvalid())
1577 return true;
Tim Northover6aacd492013-07-16 09:47:53 +00001578 TheCall->setArg(0, ValArg.get());
Tim Northover58d2bb12013-10-29 12:32:58 +00001579
1580 // __builtin_arm_strex always returns an int. It's marked as such in the .def,
1581 // but the custom checker bypasses all default analysis.
1582 TheCall->setType(Context.IntTy);
Tim Northover6aacd492013-07-16 09:47:53 +00001583 return false;
1584}
1585
Nate Begeman4904e322010-06-08 02:47:44 +00001586bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
Tim Northover6aacd492013-07-16 09:47:53 +00001587 if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
Tim Northover3acd6bd2014-07-02 12:56:02 +00001588 BuiltinID == ARM::BI__builtin_arm_ldaex ||
1589 BuiltinID == ARM::BI__builtin_arm_strex ||
1590 BuiltinID == ARM::BI__builtin_arm_stlex) {
Tim Northovera2ee4332014-03-29 15:09:45 +00001591 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 64);
Tim Northover6aacd492013-07-16 09:47:53 +00001592 }
1593
Yi Kong26d104a2014-08-13 19:18:14 +00001594 if (BuiltinID == ARM::BI__builtin_arm_prefetch) {
1595 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
1596 SemaBuiltinConstantArgRange(TheCall, 2, 0, 1);
1597 }
1598
Luke Cheeseman59b2d832015-06-15 17:51:01 +00001599 if (BuiltinID == ARM::BI__builtin_arm_rsr64 ||
1600 BuiltinID == ARM::BI__builtin_arm_wsr64)
1601 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 3, false);
1602
1603 if (BuiltinID == ARM::BI__builtin_arm_rsr ||
1604 BuiltinID == ARM::BI__builtin_arm_rsrp ||
1605 BuiltinID == ARM::BI__builtin_arm_wsr ||
1606 BuiltinID == ARM::BI__builtin_arm_wsrp)
1607 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
1608
Tim Northover12670412014-02-19 10:37:05 +00001609 if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall))
1610 return true;
Nico Weber0e6daef2013-12-26 23:38:39 +00001611
Yi Kong4efadfb2014-07-03 16:01:25 +00001612 // For intrinsics which take an immediate value as part of the instruction,
1613 // range check them here.
Sjoerd Meijer293da702017-12-07 09:54:39 +00001614 // FIXME: VFP Intrinsics should error if VFP not present.
Nate Begemand773fe62010-06-13 04:47:52 +00001615 switch (BuiltinID) {
1616 default: return false;
Sjoerd Meijer293da702017-12-07 09:54:39 +00001617 case ARM::BI__builtin_arm_ssat:
1618 return SemaBuiltinConstantArgRange(TheCall, 1, 1, 32);
1619 case ARM::BI__builtin_arm_usat:
1620 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 31);
1621 case ARM::BI__builtin_arm_ssat16:
1622 return SemaBuiltinConstantArgRange(TheCall, 1, 1, 16);
1623 case ARM::BI__builtin_arm_usat16:
1624 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15);
Nate Begemanf568b072010-08-03 21:32:34 +00001625 case ARM::BI__builtin_arm_vcvtr_f:
Sjoerd Meijer293da702017-12-07 09:54:39 +00001626 case ARM::BI__builtin_arm_vcvtr_d:
1627 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1);
Weiming Zhao87bb4922013-11-12 21:42:50 +00001628 case ARM::BI__builtin_arm_dmb:
Yi Kong4efadfb2014-07-03 16:01:25 +00001629 case ARM::BI__builtin_arm_dsb:
Yi Kong1d268af2014-08-26 12:48:06 +00001630 case ARM::BI__builtin_arm_isb:
Sjoerd Meijer293da702017-12-07 09:54:39 +00001631 case ARM::BI__builtin_arm_dbg:
1632 return SemaBuiltinConstantArgRange(TheCall, 0, 0, 15);
Richard Sandiford28940af2014-04-16 08:47:51 +00001633 }
Anders Carlssonbc4c1072009-08-16 01:56:34 +00001634}
Daniel Dunbardd9b2d12008-10-02 18:44:07 +00001635
Tim Northover573cbee2014-05-24 12:52:07 +00001636bool Sema::CheckAArch64BuiltinFunctionCall(unsigned BuiltinID,
Tim Northovera2ee4332014-03-29 15:09:45 +00001637 CallExpr *TheCall) {
Tim Northover573cbee2014-05-24 12:52:07 +00001638 if (BuiltinID == AArch64::BI__builtin_arm_ldrex ||
Tim Northover3acd6bd2014-07-02 12:56:02 +00001639 BuiltinID == AArch64::BI__builtin_arm_ldaex ||
1640 BuiltinID == AArch64::BI__builtin_arm_strex ||
1641 BuiltinID == AArch64::BI__builtin_arm_stlex) {
Tim Northovera2ee4332014-03-29 15:09:45 +00001642 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall, 128);
1643 }
1644
Yi Konga5548432014-08-13 19:18:20 +00001645 if (BuiltinID == AArch64::BI__builtin_arm_prefetch) {
1646 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
1647 SemaBuiltinConstantArgRange(TheCall, 2, 0, 2) ||
1648 SemaBuiltinConstantArgRange(TheCall, 3, 0, 1) ||
1649 SemaBuiltinConstantArgRange(TheCall, 4, 0, 1);
1650 }
1651
Luke Cheeseman59b2d832015-06-15 17:51:01 +00001652 if (BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
1653 BuiltinID == AArch64::BI__builtin_arm_wsr64)
Tim Northover54e50002016-04-13 17:08:55 +00001654 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
Luke Cheeseman59b2d832015-06-15 17:51:01 +00001655
1656 if (BuiltinID == AArch64::BI__builtin_arm_rsr ||
1657 BuiltinID == AArch64::BI__builtin_arm_rsrp ||
1658 BuiltinID == AArch64::BI__builtin_arm_wsr ||
1659 BuiltinID == AArch64::BI__builtin_arm_wsrp)
1660 return SemaBuiltinARMSpecialReg(BuiltinID, TheCall, 0, 5, true);
1661
Tim Northovera2ee4332014-03-29 15:09:45 +00001662 if (CheckNeonBuiltinFunctionCall(BuiltinID, TheCall))
1663 return true;
1664
Yi Kong19a29ac2014-07-17 10:52:06 +00001665 // For intrinsics which take an immediate value as part of the instruction,
1666 // range check them here.
1667 unsigned i = 0, l = 0, u = 0;
1668 switch (BuiltinID) {
1669 default: return false;
1670 case AArch64::BI__builtin_arm_dmb:
1671 case AArch64::BI__builtin_arm_dsb:
1672 case AArch64::BI__builtin_arm_isb: l = 0; u = 15; break;
1673 }
1674
Yi Kong19a29ac2014-07-17 10:52:06 +00001675 return SemaBuiltinConstantArgRange(TheCall, i, l, u + l);
Tim Northovera2ee4332014-03-29 15:09:45 +00001676}
1677
Krzysztof Parzyszek45850682018-05-11 16:41:51 +00001678bool Sema::CheckHexagonBuiltinFunctionCall(unsigned BuiltinID,
1679 CallExpr *TheCall) {
1680 struct ArgInfo {
1681 ArgInfo(unsigned O, bool S, unsigned W, unsigned A)
1682 : OpNum(O), IsSigned(S), BitWidth(W), Align(A) {}
1683 unsigned OpNum = 0;
1684 bool IsSigned = false;
1685 unsigned BitWidth = 0;
1686 unsigned Align = 0;
1687 };
1688
1689 static const std::map<unsigned, std::vector<ArgInfo>> Infos = {
1690 { Hexagon::BI__builtin_circ_ldd, {{ 3, true, 4, 3 }} },
1691 { Hexagon::BI__builtin_circ_ldw, {{ 3, true, 4, 2 }} },
1692 { Hexagon::BI__builtin_circ_ldh, {{ 3, true, 4, 1 }} },
1693 { Hexagon::BI__builtin_circ_lduh, {{ 3, true, 4, 0 }} },
1694 { Hexagon::BI__builtin_circ_ldb, {{ 3, true, 4, 0 }} },
1695 { Hexagon::BI__builtin_circ_ldub, {{ 3, true, 4, 0 }} },
1696 { Hexagon::BI__builtin_circ_std, {{ 3, true, 4, 3 }} },
1697 { Hexagon::BI__builtin_circ_stw, {{ 3, true, 4, 2 }} },
1698 { Hexagon::BI__builtin_circ_sth, {{ 3, true, 4, 1 }} },
1699 { Hexagon::BI__builtin_circ_sthhi, {{ 3, true, 4, 1 }} },
1700 { Hexagon::BI__builtin_circ_stb, {{ 3, true, 4, 0 }} },
1701
1702 { Hexagon::BI__builtin_HEXAGON_L2_loadrub_pci, {{ 1, true, 4, 0 }} },
1703 { Hexagon::BI__builtin_HEXAGON_L2_loadrb_pci, {{ 1, true, 4, 0 }} },
1704 { Hexagon::BI__builtin_HEXAGON_L2_loadruh_pci, {{ 1, true, 4, 1 }} },
1705 { Hexagon::BI__builtin_HEXAGON_L2_loadrh_pci, {{ 1, true, 4, 1 }} },
1706 { Hexagon::BI__builtin_HEXAGON_L2_loadri_pci, {{ 1, true, 4, 2 }} },
1707 { Hexagon::BI__builtin_HEXAGON_L2_loadrd_pci, {{ 1, true, 4, 3 }} },
1708 { Hexagon::BI__builtin_HEXAGON_S2_storerb_pci, {{ 1, true, 4, 0 }} },
1709 { Hexagon::BI__builtin_HEXAGON_S2_storerh_pci, {{ 1, true, 4, 1 }} },
1710 { Hexagon::BI__builtin_HEXAGON_S2_storerf_pci, {{ 1, true, 4, 1 }} },
1711 { Hexagon::BI__builtin_HEXAGON_S2_storeri_pci, {{ 1, true, 4, 2 }} },
1712 { Hexagon::BI__builtin_HEXAGON_S2_storerd_pci, {{ 1, true, 4, 3 }} },
1713
1714 { Hexagon::BI__builtin_HEXAGON_A2_combineii, {{ 1, true, 8, 0 }} },
1715 { Hexagon::BI__builtin_HEXAGON_A2_tfrih, {{ 1, false, 16, 0 }} },
1716 { Hexagon::BI__builtin_HEXAGON_A2_tfril, {{ 1, false, 16, 0 }} },
1717 { Hexagon::BI__builtin_HEXAGON_A2_tfrpi, {{ 0, true, 8, 0 }} },
1718 { Hexagon::BI__builtin_HEXAGON_A4_bitspliti, {{ 1, false, 5, 0 }} },
1719 { Hexagon::BI__builtin_HEXAGON_A4_cmpbeqi, {{ 1, false, 8, 0 }} },
1720 { Hexagon::BI__builtin_HEXAGON_A4_cmpbgti, {{ 1, true, 8, 0 }} },
1721 { Hexagon::BI__builtin_HEXAGON_A4_cround_ri, {{ 1, false, 5, 0 }} },
1722 { Hexagon::BI__builtin_HEXAGON_A4_round_ri, {{ 1, false, 5, 0 }} },
1723 { Hexagon::BI__builtin_HEXAGON_A4_round_ri_sat, {{ 1, false, 5, 0 }} },
1724 { Hexagon::BI__builtin_HEXAGON_A4_vcmpbeqi, {{ 1, false, 8, 0 }} },
1725 { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgti, {{ 1, true, 8, 0 }} },
1726 { Hexagon::BI__builtin_HEXAGON_A4_vcmpbgtui, {{ 1, false, 7, 0 }} },
1727 { Hexagon::BI__builtin_HEXAGON_A4_vcmpheqi, {{ 1, true, 8, 0 }} },
1728 { Hexagon::BI__builtin_HEXAGON_A4_vcmphgti, {{ 1, true, 8, 0 }} },
1729 { Hexagon::BI__builtin_HEXAGON_A4_vcmphgtui, {{ 1, false, 7, 0 }} },
1730 { Hexagon::BI__builtin_HEXAGON_A4_vcmpweqi, {{ 1, true, 8, 0 }} },
1731 { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgti, {{ 1, true, 8, 0 }} },
1732 { Hexagon::BI__builtin_HEXAGON_A4_vcmpwgtui, {{ 1, false, 7, 0 }} },
1733 { Hexagon::BI__builtin_HEXAGON_C2_bitsclri, {{ 1, false, 6, 0 }} },
1734 { Hexagon::BI__builtin_HEXAGON_C2_muxii, {{ 2, true, 8, 0 }} },
1735 { Hexagon::BI__builtin_HEXAGON_C4_nbitsclri, {{ 1, false, 6, 0 }} },
1736 { Hexagon::BI__builtin_HEXAGON_F2_dfclass, {{ 1, false, 5, 0 }} },
1737 { Hexagon::BI__builtin_HEXAGON_F2_dfimm_n, {{ 0, false, 10, 0 }} },
1738 { Hexagon::BI__builtin_HEXAGON_F2_dfimm_p, {{ 0, false, 10, 0 }} },
1739 { Hexagon::BI__builtin_HEXAGON_F2_sfclass, {{ 1, false, 5, 0 }} },
1740 { Hexagon::BI__builtin_HEXAGON_F2_sfimm_n, {{ 0, false, 10, 0 }} },
1741 { Hexagon::BI__builtin_HEXAGON_F2_sfimm_p, {{ 0, false, 10, 0 }} },
1742 { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addi, {{ 2, false, 6, 0 }} },
1743 { Hexagon::BI__builtin_HEXAGON_M4_mpyri_addr_u2, {{ 1, false, 6, 2 }} },
1744 { Hexagon::BI__builtin_HEXAGON_S2_addasl_rrri, {{ 2, false, 3, 0 }} },
1745 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_acc, {{ 2, false, 6, 0 }} },
1746 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_and, {{ 2, false, 6, 0 }} },
1747 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p, {{ 1, false, 6, 0 }} },
1748 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_nac, {{ 2, false, 6, 0 }} },
1749 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_or, {{ 2, false, 6, 0 }} },
1750 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_p_xacc, {{ 2, false, 6, 0 }} },
1751 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_acc, {{ 2, false, 5, 0 }} },
1752 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_and, {{ 2, false, 5, 0 }} },
1753 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r, {{ 1, false, 5, 0 }} },
1754 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_nac, {{ 2, false, 5, 0 }} },
1755 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_or, {{ 2, false, 5, 0 }} },
1756 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_sat, {{ 1, false, 5, 0 }} },
1757 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_r_xacc, {{ 2, false, 5, 0 }} },
1758 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vh, {{ 1, false, 4, 0 }} },
1759 { Hexagon::BI__builtin_HEXAGON_S2_asl_i_vw, {{ 1, false, 5, 0 }} },
1760 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_acc, {{ 2, false, 6, 0 }} },
1761 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_and, {{ 2, false, 6, 0 }} },
1762 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p, {{ 1, false, 6, 0 }} },
1763 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_nac, {{ 2, false, 6, 0 }} },
1764 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_or, {{ 2, false, 6, 0 }} },
1765 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd_goodsyntax,
1766 {{ 1, false, 6, 0 }} },
1767 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_p_rnd, {{ 1, false, 6, 0 }} },
1768 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_acc, {{ 2, false, 5, 0 }} },
1769 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_and, {{ 2, false, 5, 0 }} },
1770 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r, {{ 1, false, 5, 0 }} },
1771 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_nac, {{ 2, false, 5, 0 }} },
1772 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_or, {{ 2, false, 5, 0 }} },
1773 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd_goodsyntax,
1774 {{ 1, false, 5, 0 }} },
1775 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_r_rnd, {{ 1, false, 5, 0 }} },
1776 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_svw_trun, {{ 1, false, 5, 0 }} },
1777 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vh, {{ 1, false, 4, 0 }} },
1778 { Hexagon::BI__builtin_HEXAGON_S2_asr_i_vw, {{ 1, false, 5, 0 }} },
1779 { Hexagon::BI__builtin_HEXAGON_S2_clrbit_i, {{ 1, false, 5, 0 }} },
1780 { Hexagon::BI__builtin_HEXAGON_S2_extractu, {{ 1, false, 5, 0 },
1781 { 2, false, 5, 0 }} },
1782 { Hexagon::BI__builtin_HEXAGON_S2_extractup, {{ 1, false, 6, 0 },
1783 { 2, false, 6, 0 }} },
1784 { Hexagon::BI__builtin_HEXAGON_S2_insert, {{ 2, false, 5, 0 },
1785 { 3, false, 5, 0 }} },
1786 { Hexagon::BI__builtin_HEXAGON_S2_insertp, {{ 2, false, 6, 0 },
1787 { 3, false, 6, 0 }} },
1788 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_acc, {{ 2, false, 6, 0 }} },
1789 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_and, {{ 2, false, 6, 0 }} },
1790 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p, {{ 1, false, 6, 0 }} },
1791 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_nac, {{ 2, false, 6, 0 }} },
1792 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_or, {{ 2, false, 6, 0 }} },
1793 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_p_xacc, {{ 2, false, 6, 0 }} },
1794 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_acc, {{ 2, false, 5, 0 }} },
1795 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_and, {{ 2, false, 5, 0 }} },
1796 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r, {{ 1, false, 5, 0 }} },
1797 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_nac, {{ 2, false, 5, 0 }} },
1798 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_or, {{ 2, false, 5, 0 }} },
1799 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_r_xacc, {{ 2, false, 5, 0 }} },
1800 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vh, {{ 1, false, 4, 0 }} },
1801 { Hexagon::BI__builtin_HEXAGON_S2_lsr_i_vw, {{ 1, false, 5, 0 }} },
1802 { Hexagon::BI__builtin_HEXAGON_S2_setbit_i, {{ 1, false, 5, 0 }} },
1803 { Hexagon::BI__builtin_HEXAGON_S2_tableidxb_goodsyntax,
1804 {{ 2, false, 4, 0 },
1805 { 3, false, 5, 0 }} },
1806 { Hexagon::BI__builtin_HEXAGON_S2_tableidxd_goodsyntax,
1807 {{ 2, false, 4, 0 },
1808 { 3, false, 5, 0 }} },
1809 { Hexagon::BI__builtin_HEXAGON_S2_tableidxh_goodsyntax,
1810 {{ 2, false, 4, 0 },
1811 { 3, false, 5, 0 }} },
1812 { Hexagon::BI__builtin_HEXAGON_S2_tableidxw_goodsyntax,
1813 {{ 2, false, 4, 0 },
1814 { 3, false, 5, 0 }} },
1815 { Hexagon::BI__builtin_HEXAGON_S2_togglebit_i, {{ 1, false, 5, 0 }} },
1816 { Hexagon::BI__builtin_HEXAGON_S2_tstbit_i, {{ 1, false, 5, 0 }} },
1817 { Hexagon::BI__builtin_HEXAGON_S2_valignib, {{ 2, false, 3, 0 }} },
1818 { Hexagon::BI__builtin_HEXAGON_S2_vspliceib, {{ 2, false, 3, 0 }} },
1819 { Hexagon::BI__builtin_HEXAGON_S4_addi_asl_ri, {{ 2, false, 5, 0 }} },
1820 { Hexagon::BI__builtin_HEXAGON_S4_addi_lsr_ri, {{ 2, false, 5, 0 }} },
1821 { Hexagon::BI__builtin_HEXAGON_S4_andi_asl_ri, {{ 2, false, 5, 0 }} },
1822 { Hexagon::BI__builtin_HEXAGON_S4_andi_lsr_ri, {{ 2, false, 5, 0 }} },
1823 { Hexagon::BI__builtin_HEXAGON_S4_clbaddi, {{ 1, true , 6, 0 }} },
1824 { Hexagon::BI__builtin_HEXAGON_S4_clbpaddi, {{ 1, true, 6, 0 }} },
1825 { Hexagon::BI__builtin_HEXAGON_S4_extract, {{ 1, false, 5, 0 },
1826 { 2, false, 5, 0 }} },
1827 { Hexagon::BI__builtin_HEXAGON_S4_extractp, {{ 1, false, 6, 0 },
1828 { 2, false, 6, 0 }} },
1829 { Hexagon::BI__builtin_HEXAGON_S4_lsli, {{ 0, true, 6, 0 }} },
1830 { Hexagon::BI__builtin_HEXAGON_S4_ntstbit_i, {{ 1, false, 5, 0 }} },
1831 { Hexagon::BI__builtin_HEXAGON_S4_ori_asl_ri, {{ 2, false, 5, 0 }} },
1832 { Hexagon::BI__builtin_HEXAGON_S4_ori_lsr_ri, {{ 2, false, 5, 0 }} },
1833 { Hexagon::BI__builtin_HEXAGON_S4_subi_asl_ri, {{ 2, false, 5, 0 }} },
1834 { Hexagon::BI__builtin_HEXAGON_S4_subi_lsr_ri, {{ 2, false, 5, 0 }} },
1835 { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate_acc, {{ 3, false, 2, 0 }} },
1836 { Hexagon::BI__builtin_HEXAGON_S4_vrcrotate, {{ 2, false, 2, 0 }} },
1837 { Hexagon::BI__builtin_HEXAGON_S5_asrhub_rnd_sat_goodsyntax,
1838 {{ 1, false, 4, 0 }} },
1839 { Hexagon::BI__builtin_HEXAGON_S5_asrhub_sat, {{ 1, false, 4, 0 }} },
1840 { Hexagon::BI__builtin_HEXAGON_S5_vasrhrnd_goodsyntax,
1841 {{ 1, false, 4, 0 }} },
1842 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p, {{ 1, false, 6, 0 }} },
1843 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_acc, {{ 2, false, 6, 0 }} },
1844 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_and, {{ 2, false, 6, 0 }} },
1845 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_nac, {{ 2, false, 6, 0 }} },
1846 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_or, {{ 2, false, 6, 0 }} },
1847 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_p_xacc, {{ 2, false, 6, 0 }} },
1848 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r, {{ 1, false, 5, 0 }} },
1849 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_acc, {{ 2, false, 5, 0 }} },
1850 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_and, {{ 2, false, 5, 0 }} },
1851 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_nac, {{ 2, false, 5, 0 }} },
1852 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_or, {{ 2, false, 5, 0 }} },
1853 { Hexagon::BI__builtin_HEXAGON_S6_rol_i_r_xacc, {{ 2, false, 5, 0 }} },
1854 { Hexagon::BI__builtin_HEXAGON_V6_valignbi, {{ 2, false, 3, 0 }} },
1855 { Hexagon::BI__builtin_HEXAGON_V6_valignbi_128B, {{ 2, false, 3, 0 }} },
1856 { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi, {{ 2, false, 3, 0 }} },
1857 { Hexagon::BI__builtin_HEXAGON_V6_vlalignbi_128B, {{ 2, false, 3, 0 }} },
1858 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi, {{ 2, false, 1, 0 }} },
1859 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_128B, {{ 2, false, 1, 0 }} },
1860 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc, {{ 3, false, 1, 0 }} },
1861 { Hexagon::BI__builtin_HEXAGON_V6_vrmpybusi_acc_128B,
1862 {{ 3, false, 1, 0 }} },
1863 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi, {{ 2, false, 1, 0 }} },
1864 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_128B, {{ 2, false, 1, 0 }} },
1865 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc, {{ 3, false, 1, 0 }} },
1866 { Hexagon::BI__builtin_HEXAGON_V6_vrmpyubi_acc_128B,
1867 {{ 3, false, 1, 0 }} },
1868 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi, {{ 2, false, 1, 0 }} },
1869 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_128B, {{ 2, false, 1, 0 }} },
1870 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc, {{ 3, false, 1, 0 }} },
1871 { Hexagon::BI__builtin_HEXAGON_V6_vrsadubi_acc_128B,
1872 {{ 3, false, 1, 0 }} },
1873 };
1874
1875 auto F = Infos.find(BuiltinID);
1876 if (F == Infos.end())
1877 return false;
1878
1879 bool Error = false;
1880
1881 for (const ArgInfo &A : F->second) {
1882 int32_t Min = A.IsSigned ? -(1 << (A.BitWidth-1)) : 0;
1883 int32_t Max = (1 << (A.IsSigned ? A.BitWidth-1 : A.BitWidth)) - 1;
1884 if (!A.Align) {
1885 Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max);
1886 } else {
1887 unsigned M = 1 << A.Align;
1888 Min *= M;
1889 Max *= M;
1890 Error |= SemaBuiltinConstantArgRange(TheCall, A.OpNum, Min, Max) |
1891 SemaBuiltinConstantArgMultiple(TheCall, A.OpNum, M);
1892 }
1893 }
1894 return Error;
1895}
1896
Simon Dardis1f90f2d2016-10-19 17:50:52 +00001897// CheckMipsBuiltinFunctionCall - Checks the constant value passed to the
1898// intrinsic is correct. The switch statement is ordered by DSP, MSA. The
1899// ordering for DSP is unspecified. MSA is ordered by the data format used
1900// by the underlying instruction i.e., df/m, df/n and then by size.
1901//
1902// FIXME: The size tests here should instead be tablegen'd along with the
1903// definitions from include/clang/Basic/BuiltinsMips.def.
1904// FIXME: GCC is strict on signedness for some of these intrinsics, we should
1905// be too.
Simon Atanasyanecedf3d2012-07-08 09:30:00 +00001906bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
Simon Dardis1f90f2d2016-10-19 17:50:52 +00001907 unsigned i = 0, l = 0, u = 0, m = 0;
Simon Atanasyanecedf3d2012-07-08 09:30:00 +00001908 switch (BuiltinID) {
1909 default: return false;
1910 case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break;
1911 case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break;
Simon Atanasyan8f06f2f2012-08-27 12:29:20 +00001912 case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break;
1913 case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break;
1914 case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break;
1915 case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break;
1916 case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break;
Simon Dardis1f90f2d2016-10-19 17:50:52 +00001917 // MSA instrinsics. Instructions (which the intrinsics maps to) which use the
1918 // df/m field.
1919 // These intrinsics take an unsigned 3 bit immediate.
1920 case Mips::BI__builtin_msa_bclri_b:
1921 case Mips::BI__builtin_msa_bnegi_b:
1922 case Mips::BI__builtin_msa_bseti_b:
1923 case Mips::BI__builtin_msa_sat_s_b:
1924 case Mips::BI__builtin_msa_sat_u_b:
1925 case Mips::BI__builtin_msa_slli_b:
1926 case Mips::BI__builtin_msa_srai_b:
1927 case Mips::BI__builtin_msa_srari_b:
1928 case Mips::BI__builtin_msa_srli_b:
1929 case Mips::BI__builtin_msa_srlri_b: i = 1; l = 0; u = 7; break;
1930 case Mips::BI__builtin_msa_binsli_b:
1931 case Mips::BI__builtin_msa_binsri_b: i = 2; l = 0; u = 7; break;
1932 // These intrinsics take an unsigned 4 bit immediate.
1933 case Mips::BI__builtin_msa_bclri_h:
1934 case Mips::BI__builtin_msa_bnegi_h:
1935 case Mips::BI__builtin_msa_bseti_h:
1936 case Mips::BI__builtin_msa_sat_s_h:
1937 case Mips::BI__builtin_msa_sat_u_h:
1938 case Mips::BI__builtin_msa_slli_h:
1939 case Mips::BI__builtin_msa_srai_h:
1940 case Mips::BI__builtin_msa_srari_h:
1941 case Mips::BI__builtin_msa_srli_h:
1942 case Mips::BI__builtin_msa_srlri_h: i = 1; l = 0; u = 15; break;
1943 case Mips::BI__builtin_msa_binsli_h:
1944 case Mips::BI__builtin_msa_binsri_h: i = 2; l = 0; u = 15; break;
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001945 // These intrinsics take an unsigned 5 bit immediate.
Simon Dardis1f90f2d2016-10-19 17:50:52 +00001946 // The first block of intrinsics actually have an unsigned 5 bit field,
1947 // not a df/n field.
1948 case Mips::BI__builtin_msa_clei_u_b:
1949 case Mips::BI__builtin_msa_clei_u_h:
1950 case Mips::BI__builtin_msa_clei_u_w:
1951 case Mips::BI__builtin_msa_clei_u_d:
1952 case Mips::BI__builtin_msa_clti_u_b:
1953 case Mips::BI__builtin_msa_clti_u_h:
1954 case Mips::BI__builtin_msa_clti_u_w:
1955 case Mips::BI__builtin_msa_clti_u_d:
1956 case Mips::BI__builtin_msa_maxi_u_b:
1957 case Mips::BI__builtin_msa_maxi_u_h:
1958 case Mips::BI__builtin_msa_maxi_u_w:
1959 case Mips::BI__builtin_msa_maxi_u_d:
1960 case Mips::BI__builtin_msa_mini_u_b:
1961 case Mips::BI__builtin_msa_mini_u_h:
1962 case Mips::BI__builtin_msa_mini_u_w:
1963 case Mips::BI__builtin_msa_mini_u_d:
1964 case Mips::BI__builtin_msa_addvi_b:
1965 case Mips::BI__builtin_msa_addvi_h:
1966 case Mips::BI__builtin_msa_addvi_w:
1967 case Mips::BI__builtin_msa_addvi_d:
1968 case Mips::BI__builtin_msa_bclri_w:
1969 case Mips::BI__builtin_msa_bnegi_w:
1970 case Mips::BI__builtin_msa_bseti_w:
1971 case Mips::BI__builtin_msa_sat_s_w:
1972 case Mips::BI__builtin_msa_sat_u_w:
1973 case Mips::BI__builtin_msa_slli_w:
1974 case Mips::BI__builtin_msa_srai_w:
1975 case Mips::BI__builtin_msa_srari_w:
1976 case Mips::BI__builtin_msa_srli_w:
1977 case Mips::BI__builtin_msa_srlri_w:
1978 case Mips::BI__builtin_msa_subvi_b:
1979 case Mips::BI__builtin_msa_subvi_h:
1980 case Mips::BI__builtin_msa_subvi_w:
1981 case Mips::BI__builtin_msa_subvi_d: i = 1; l = 0; u = 31; break;
1982 case Mips::BI__builtin_msa_binsli_w:
1983 case Mips::BI__builtin_msa_binsri_w: i = 2; l = 0; u = 31; break;
1984 // These intrinsics take an unsigned 6 bit immediate.
1985 case Mips::BI__builtin_msa_bclri_d:
1986 case Mips::BI__builtin_msa_bnegi_d:
1987 case Mips::BI__builtin_msa_bseti_d:
1988 case Mips::BI__builtin_msa_sat_s_d:
1989 case Mips::BI__builtin_msa_sat_u_d:
1990 case Mips::BI__builtin_msa_slli_d:
1991 case Mips::BI__builtin_msa_srai_d:
1992 case Mips::BI__builtin_msa_srari_d:
1993 case Mips::BI__builtin_msa_srli_d:
1994 case Mips::BI__builtin_msa_srlri_d: i = 1; l = 0; u = 63; break;
1995 case Mips::BI__builtin_msa_binsli_d:
1996 case Mips::BI__builtin_msa_binsri_d: i = 2; l = 0; u = 63; break;
1997 // These intrinsics take a signed 5 bit immediate.
1998 case Mips::BI__builtin_msa_ceqi_b:
1999 case Mips::BI__builtin_msa_ceqi_h:
2000 case Mips::BI__builtin_msa_ceqi_w:
2001 case Mips::BI__builtin_msa_ceqi_d:
2002 case Mips::BI__builtin_msa_clti_s_b:
2003 case Mips::BI__builtin_msa_clti_s_h:
2004 case Mips::BI__builtin_msa_clti_s_w:
2005 case Mips::BI__builtin_msa_clti_s_d:
2006 case Mips::BI__builtin_msa_clei_s_b:
2007 case Mips::BI__builtin_msa_clei_s_h:
2008 case Mips::BI__builtin_msa_clei_s_w:
2009 case Mips::BI__builtin_msa_clei_s_d:
2010 case Mips::BI__builtin_msa_maxi_s_b:
2011 case Mips::BI__builtin_msa_maxi_s_h:
2012 case Mips::BI__builtin_msa_maxi_s_w:
2013 case Mips::BI__builtin_msa_maxi_s_d:
2014 case Mips::BI__builtin_msa_mini_s_b:
2015 case Mips::BI__builtin_msa_mini_s_h:
2016 case Mips::BI__builtin_msa_mini_s_w:
2017 case Mips::BI__builtin_msa_mini_s_d: i = 1; l = -16; u = 15; break;
2018 // These intrinsics take an unsigned 8 bit immediate.
2019 case Mips::BI__builtin_msa_andi_b:
2020 case Mips::BI__builtin_msa_nori_b:
2021 case Mips::BI__builtin_msa_ori_b:
2022 case Mips::BI__builtin_msa_shf_b:
2023 case Mips::BI__builtin_msa_shf_h:
2024 case Mips::BI__builtin_msa_shf_w:
2025 case Mips::BI__builtin_msa_xori_b: i = 1; l = 0; u = 255; break;
2026 case Mips::BI__builtin_msa_bseli_b:
2027 case Mips::BI__builtin_msa_bmnzi_b:
2028 case Mips::BI__builtin_msa_bmzi_b: i = 2; l = 0; u = 255; break;
2029 // df/n format
2030 // These intrinsics take an unsigned 4 bit immediate.
2031 case Mips::BI__builtin_msa_copy_s_b:
2032 case Mips::BI__builtin_msa_copy_u_b:
2033 case Mips::BI__builtin_msa_insve_b:
2034 case Mips::BI__builtin_msa_splati_b: i = 1; l = 0; u = 15; break;
Simon Dardis1f90f2d2016-10-19 17:50:52 +00002035 case Mips::BI__builtin_msa_sldi_b: i = 2; l = 0; u = 15; break;
2036 // These intrinsics take an unsigned 3 bit immediate.
2037 case Mips::BI__builtin_msa_copy_s_h:
2038 case Mips::BI__builtin_msa_copy_u_h:
2039 case Mips::BI__builtin_msa_insve_h:
2040 case Mips::BI__builtin_msa_splati_h: i = 1; l = 0; u = 7; break;
Simon Dardis1f90f2d2016-10-19 17:50:52 +00002041 case Mips::BI__builtin_msa_sldi_h: i = 2; l = 0; u = 7; break;
2042 // These intrinsics take an unsigned 2 bit immediate.
2043 case Mips::BI__builtin_msa_copy_s_w:
2044 case Mips::BI__builtin_msa_copy_u_w:
2045 case Mips::BI__builtin_msa_insve_w:
2046 case Mips::BI__builtin_msa_splati_w: i = 1; l = 0; u = 3; break;
Simon Dardis1f90f2d2016-10-19 17:50:52 +00002047 case Mips::BI__builtin_msa_sldi_w: i = 2; l = 0; u = 3; break;
2048 // These intrinsics take an unsigned 1 bit immediate.
2049 case Mips::BI__builtin_msa_copy_s_d:
2050 case Mips::BI__builtin_msa_copy_u_d:
2051 case Mips::BI__builtin_msa_insve_d:
2052 case Mips::BI__builtin_msa_splati_d: i = 1; l = 0; u = 1; break;
Simon Dardis1f90f2d2016-10-19 17:50:52 +00002053 case Mips::BI__builtin_msa_sldi_d: i = 2; l = 0; u = 1; break;
2054 // Memory offsets and immediate loads.
2055 // These intrinsics take a signed 10 bit immediate.
Petar Jovanovic9b8b9e82017-03-31 16:16:43 +00002056 case Mips::BI__builtin_msa_ldi_b: i = 0; l = -128; u = 255; break;
Simon Dardis1f90f2d2016-10-19 17:50:52 +00002057 case Mips::BI__builtin_msa_ldi_h:
2058 case Mips::BI__builtin_msa_ldi_w:
2059 case Mips::BI__builtin_msa_ldi_d: i = 0; l = -512; u = 511; break;
2060 case Mips::BI__builtin_msa_ld_b: i = 1; l = -512; u = 511; m = 16; break;
2061 case Mips::BI__builtin_msa_ld_h: i = 1; l = -1024; u = 1022; m = 16; break;
2062 case Mips::BI__builtin_msa_ld_w: i = 1; l = -2048; u = 2044; m = 16; break;
2063 case Mips::BI__builtin_msa_ld_d: i = 1; l = -4096; u = 4088; m = 16; break;
2064 case Mips::BI__builtin_msa_st_b: i = 2; l = -512; u = 511; m = 16; break;
2065 case Mips::BI__builtin_msa_st_h: i = 2; l = -1024; u = 1022; m = 16; break;
2066 case Mips::BI__builtin_msa_st_w: i = 2; l = -2048; u = 2044; m = 16; break;
2067 case Mips::BI__builtin_msa_st_d: i = 2; l = -4096; u = 4088; m = 16; break;
Richard Sandiford28940af2014-04-16 08:47:51 +00002068 }
Simon Atanasyanecedf3d2012-07-08 09:30:00 +00002069
Simon Dardis1f90f2d2016-10-19 17:50:52 +00002070 if (!m)
2071 return SemaBuiltinConstantArgRange(TheCall, i, l, u);
2072
2073 return SemaBuiltinConstantArgRange(TheCall, i, l, u) ||
2074 SemaBuiltinConstantArgMultiple(TheCall, i, m);
Simon Atanasyanecedf3d2012-07-08 09:30:00 +00002075}
2076
Kit Bartone50adcb2015-03-30 19:40:59 +00002077bool Sema::CheckPPCBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
2078 unsigned i = 0, l = 0, u = 0;
Nemanja Ivanovic239eec72015-04-09 23:58:16 +00002079 bool Is64BitBltin = BuiltinID == PPC::BI__builtin_divde ||
2080 BuiltinID == PPC::BI__builtin_divdeu ||
2081 BuiltinID == PPC::BI__builtin_bpermd;
2082 bool IsTarget64Bit = Context.getTargetInfo()
2083 .getTypeWidth(Context
2084 .getTargetInfo()
2085 .getIntPtrType()) == 64;
2086 bool IsBltinExtDiv = BuiltinID == PPC::BI__builtin_divwe ||
2087 BuiltinID == PPC::BI__builtin_divweu ||
2088 BuiltinID == PPC::BI__builtin_divde ||
2089 BuiltinID == PPC::BI__builtin_divdeu;
2090
2091 if (Is64BitBltin && !IsTarget64Bit)
2092 return Diag(TheCall->getLocStart(), diag::err_64_bit_builtin_32_bit_tgt)
2093 << TheCall->getSourceRange();
2094
2095 if ((IsBltinExtDiv && !Context.getTargetInfo().hasFeature("extdiv")) ||
2096 (BuiltinID == PPC::BI__builtin_bpermd &&
2097 !Context.getTargetInfo().hasFeature("bpermd")))
2098 return Diag(TheCall->getLocStart(), diag::err_ppc_builtin_only_on_pwr7)
2099 << TheCall->getSourceRange();
2100
Kit Bartone50adcb2015-03-30 19:40:59 +00002101 switch (BuiltinID) {
2102 default: return false;
2103 case PPC::BI__builtin_altivec_crypto_vshasigmaw:
2104 case PPC::BI__builtin_altivec_crypto_vshasigmad:
2105 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 1) ||
2106 SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
2107 case PPC::BI__builtin_tbegin:
2108 case PPC::BI__builtin_tend: i = 0; l = 0; u = 1; break;
2109 case PPC::BI__builtin_tsr: i = 0; l = 0; u = 7; break;
2110 case PPC::BI__builtin_tabortwc:
2111 case PPC::BI__builtin_tabortdc: i = 0; l = 0; u = 31; break;
2112 case PPC::BI__builtin_tabortwci:
2113 case PPC::BI__builtin_tabortdci:
2114 return SemaBuiltinConstantArgRange(TheCall, 0, 0, 31) ||
2115 SemaBuiltinConstantArgRange(TheCall, 2, 0, 31);
Tony Jiangbbc48e92017-05-24 15:13:32 +00002116 case PPC::BI__builtin_vsx_xxpermdi:
Tony Jiang9aa2c032017-05-24 15:54:13 +00002117 case PPC::BI__builtin_vsx_xxsldwi:
Tony Jiangbbc48e92017-05-24 15:13:32 +00002118 return SemaBuiltinVSX(TheCall);
Kit Bartone50adcb2015-03-30 19:40:59 +00002119 }
2120 return SemaBuiltinConstantArgRange(TheCall, i, l, u);
2121}
2122
Ulrich Weigand3a610eb2015-04-01 12:54:25 +00002123bool Sema::CheckSystemZBuiltinFunctionCall(unsigned BuiltinID,
2124 CallExpr *TheCall) {
2125 if (BuiltinID == SystemZ::BI__builtin_tabort) {
2126 Expr *Arg = TheCall->getArg(0);
2127 llvm::APSInt AbortCode(32);
2128 if (Arg->isIntegerConstantExpr(AbortCode, Context) &&
2129 AbortCode.getSExtValue() >= 0 && AbortCode.getSExtValue() < 256)
2130 return Diag(Arg->getLocStart(), diag::err_systemz_invalid_tabort_code)
2131 << Arg->getSourceRange();
2132 }
2133
Ulrich Weigand5722c0f2015-05-05 19:36:42 +00002134 // For intrinsics which take an immediate value as part of the instruction,
2135 // range check them here.
2136 unsigned i = 0, l = 0, u = 0;
2137 switch (BuiltinID) {
2138 default: return false;
2139 case SystemZ::BI__builtin_s390_lcbb: i = 1; l = 0; u = 15; break;
2140 case SystemZ::BI__builtin_s390_verimb:
2141 case SystemZ::BI__builtin_s390_verimh:
2142 case SystemZ::BI__builtin_s390_verimf:
2143 case SystemZ::BI__builtin_s390_verimg: i = 3; l = 0; u = 255; break;
2144 case SystemZ::BI__builtin_s390_vfaeb:
2145 case SystemZ::BI__builtin_s390_vfaeh:
2146 case SystemZ::BI__builtin_s390_vfaef:
2147 case SystemZ::BI__builtin_s390_vfaebs:
2148 case SystemZ::BI__builtin_s390_vfaehs:
2149 case SystemZ::BI__builtin_s390_vfaefs:
2150 case SystemZ::BI__builtin_s390_vfaezb:
2151 case SystemZ::BI__builtin_s390_vfaezh:
2152 case SystemZ::BI__builtin_s390_vfaezf:
2153 case SystemZ::BI__builtin_s390_vfaezbs:
2154 case SystemZ::BI__builtin_s390_vfaezhs:
2155 case SystemZ::BI__builtin_s390_vfaezfs: i = 2; l = 0; u = 15; break;
Ulrich Weigandcac24ab2017-07-17 17:45:57 +00002156 case SystemZ::BI__builtin_s390_vfisb:
Ulrich Weigand5722c0f2015-05-05 19:36:42 +00002157 case SystemZ::BI__builtin_s390_vfidb:
2158 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15) ||
2159 SemaBuiltinConstantArgRange(TheCall, 2, 0, 15);
Ulrich Weigandcac24ab2017-07-17 17:45:57 +00002160 case SystemZ::BI__builtin_s390_vftcisb:
Ulrich Weigand5722c0f2015-05-05 19:36:42 +00002161 case SystemZ::BI__builtin_s390_vftcidb: i = 1; l = 0; u = 4095; break;
2162 case SystemZ::BI__builtin_s390_vlbb: i = 1; l = 0; u = 15; break;
2163 case SystemZ::BI__builtin_s390_vpdi: i = 2; l = 0; u = 15; break;
2164 case SystemZ::BI__builtin_s390_vsldb: i = 2; l = 0; u = 15; break;
2165 case SystemZ::BI__builtin_s390_vstrcb:
2166 case SystemZ::BI__builtin_s390_vstrch:
2167 case SystemZ::BI__builtin_s390_vstrcf:
2168 case SystemZ::BI__builtin_s390_vstrczb:
2169 case SystemZ::BI__builtin_s390_vstrczh:
2170 case SystemZ::BI__builtin_s390_vstrczf:
2171 case SystemZ::BI__builtin_s390_vstrcbs:
2172 case SystemZ::BI__builtin_s390_vstrchs:
2173 case SystemZ::BI__builtin_s390_vstrcfs:
2174 case SystemZ::BI__builtin_s390_vstrczbs:
2175 case SystemZ::BI__builtin_s390_vstrczhs:
2176 case SystemZ::BI__builtin_s390_vstrczfs: i = 3; l = 0; u = 15; break;
Ulrich Weigandcac24ab2017-07-17 17:45:57 +00002177 case SystemZ::BI__builtin_s390_vmslg: i = 3; l = 0; u = 15; break;
2178 case SystemZ::BI__builtin_s390_vfminsb:
2179 case SystemZ::BI__builtin_s390_vfmaxsb:
2180 case SystemZ::BI__builtin_s390_vfmindb:
2181 case SystemZ::BI__builtin_s390_vfmaxdb: i = 2; l = 0; u = 15; break;
Ulrich Weigand5722c0f2015-05-05 19:36:42 +00002182 }
2183 return SemaBuiltinConstantArgRange(TheCall, i, l, u);
Ulrich Weigand3a610eb2015-04-01 12:54:25 +00002184}
2185
Craig Topper5ba2c502015-11-07 08:08:31 +00002186/// SemaBuiltinCpuSupports - Handle __builtin_cpu_supports(char *).
2187/// This checks that the target supports __builtin_cpu_supports and
2188/// that the string argument is constant and valid.
2189static bool SemaBuiltinCpuSupports(Sema &S, CallExpr *TheCall) {
2190 Expr *Arg = TheCall->getArg(0);
2191
2192 // Check if the argument is a string literal.
2193 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
2194 return S.Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal)
2195 << Arg->getSourceRange();
2196
2197 // Check the contents of the string.
2198 StringRef Feature =
2199 cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
2200 if (!S.Context.getTargetInfo().validateCpuSupports(Feature))
2201 return S.Diag(TheCall->getLocStart(), diag::err_invalid_cpu_supports)
2202 << Arg->getSourceRange();
2203 return false;
2204}
2205
Craig Topper699ae0c2017-08-10 20:28:30 +00002206/// SemaBuiltinCpuIs - Handle __builtin_cpu_is(char *).
2207/// This checks that the target supports __builtin_cpu_is and
2208/// that the string argument is constant and valid.
2209static bool SemaBuiltinCpuIs(Sema &S, CallExpr *TheCall) {
2210 Expr *Arg = TheCall->getArg(0);
2211
2212 // Check if the argument is a string literal.
2213 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
2214 return S.Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal)
2215 << Arg->getSourceRange();
2216
2217 // Check the contents of the string.
2218 StringRef Feature =
2219 cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
2220 if (!S.Context.getTargetInfo().validateCpuIs(Feature))
2221 return S.Diag(TheCall->getLocStart(), diag::err_invalid_cpu_is)
2222 << Arg->getSourceRange();
2223 return false;
2224}
2225
Craig Toppera7e253e2016-09-23 04:48:31 +00002226// Check if the rounding mode is legal.
2227bool Sema::CheckX86BuiltinRoundingOrSAE(unsigned BuiltinID, CallExpr *TheCall) {
2228 // Indicates if this instruction has rounding control or just SAE.
2229 bool HasRC = false;
2230
2231 unsigned ArgNum = 0;
2232 switch (BuiltinID) {
2233 default:
2234 return false;
2235 case X86::BI__builtin_ia32_vcvttsd2si32:
2236 case X86::BI__builtin_ia32_vcvttsd2si64:
2237 case X86::BI__builtin_ia32_vcvttsd2usi32:
2238 case X86::BI__builtin_ia32_vcvttsd2usi64:
2239 case X86::BI__builtin_ia32_vcvttss2si32:
2240 case X86::BI__builtin_ia32_vcvttss2si64:
2241 case X86::BI__builtin_ia32_vcvttss2usi32:
2242 case X86::BI__builtin_ia32_vcvttss2usi64:
2243 ArgNum = 1;
2244 break;
2245 case X86::BI__builtin_ia32_cvtps2pd512_mask:
2246 case X86::BI__builtin_ia32_cvttpd2dq512_mask:
2247 case X86::BI__builtin_ia32_cvttpd2qq512_mask:
2248 case X86::BI__builtin_ia32_cvttpd2udq512_mask:
2249 case X86::BI__builtin_ia32_cvttpd2uqq512_mask:
2250 case X86::BI__builtin_ia32_cvttps2dq512_mask:
2251 case X86::BI__builtin_ia32_cvttps2qq512_mask:
2252 case X86::BI__builtin_ia32_cvttps2udq512_mask:
2253 case X86::BI__builtin_ia32_cvttps2uqq512_mask:
2254 case X86::BI__builtin_ia32_exp2pd_mask:
2255 case X86::BI__builtin_ia32_exp2ps_mask:
2256 case X86::BI__builtin_ia32_getexppd512_mask:
2257 case X86::BI__builtin_ia32_getexpps512_mask:
2258 case X86::BI__builtin_ia32_rcp28pd_mask:
2259 case X86::BI__builtin_ia32_rcp28ps_mask:
2260 case X86::BI__builtin_ia32_rsqrt28pd_mask:
2261 case X86::BI__builtin_ia32_rsqrt28ps_mask:
2262 case X86::BI__builtin_ia32_vcomisd:
2263 case X86::BI__builtin_ia32_vcomiss:
2264 case X86::BI__builtin_ia32_vcvtph2ps512_mask:
2265 ArgNum = 3;
2266 break;
2267 case X86::BI__builtin_ia32_cmppd512_mask:
2268 case X86::BI__builtin_ia32_cmpps512_mask:
2269 case X86::BI__builtin_ia32_cmpsd_mask:
2270 case X86::BI__builtin_ia32_cmpss_mask:
Craig Topper8e066312016-11-07 07:01:09 +00002271 case X86::BI__builtin_ia32_cvtss2sd_round_mask:
Craig Toppera7e253e2016-09-23 04:48:31 +00002272 case X86::BI__builtin_ia32_getexpsd128_round_mask:
2273 case X86::BI__builtin_ia32_getexpss128_round_mask:
Craig Topper8e066312016-11-07 07:01:09 +00002274 case X86::BI__builtin_ia32_maxpd512_mask:
2275 case X86::BI__builtin_ia32_maxps512_mask:
2276 case X86::BI__builtin_ia32_maxsd_round_mask:
2277 case X86::BI__builtin_ia32_maxss_round_mask:
2278 case X86::BI__builtin_ia32_minpd512_mask:
2279 case X86::BI__builtin_ia32_minps512_mask:
2280 case X86::BI__builtin_ia32_minsd_round_mask:
2281 case X86::BI__builtin_ia32_minss_round_mask:
Craig Toppera7e253e2016-09-23 04:48:31 +00002282 case X86::BI__builtin_ia32_rcp28sd_round_mask:
2283 case X86::BI__builtin_ia32_rcp28ss_round_mask:
2284 case X86::BI__builtin_ia32_reducepd512_mask:
2285 case X86::BI__builtin_ia32_reduceps512_mask:
2286 case X86::BI__builtin_ia32_rndscalepd_mask:
2287 case X86::BI__builtin_ia32_rndscaleps_mask:
2288 case X86::BI__builtin_ia32_rsqrt28sd_round_mask:
2289 case X86::BI__builtin_ia32_rsqrt28ss_round_mask:
2290 ArgNum = 4;
2291 break;
2292 case X86::BI__builtin_ia32_fixupimmpd512_mask:
Craig Topper7609f1c2016-10-01 21:03:50 +00002293 case X86::BI__builtin_ia32_fixupimmpd512_maskz:
Craig Toppera7e253e2016-09-23 04:48:31 +00002294 case X86::BI__builtin_ia32_fixupimmps512_mask:
Craig Topper7609f1c2016-10-01 21:03:50 +00002295 case X86::BI__builtin_ia32_fixupimmps512_maskz:
Craig Toppera7e253e2016-09-23 04:48:31 +00002296 case X86::BI__builtin_ia32_fixupimmsd_mask:
Craig Topper7609f1c2016-10-01 21:03:50 +00002297 case X86::BI__builtin_ia32_fixupimmsd_maskz:
Craig Toppera7e253e2016-09-23 04:48:31 +00002298 case X86::BI__builtin_ia32_fixupimmss_mask:
Craig Topper7609f1c2016-10-01 21:03:50 +00002299 case X86::BI__builtin_ia32_fixupimmss_maskz:
Craig Toppera7e253e2016-09-23 04:48:31 +00002300 case X86::BI__builtin_ia32_rangepd512_mask:
2301 case X86::BI__builtin_ia32_rangeps512_mask:
2302 case X86::BI__builtin_ia32_rangesd128_round_mask:
2303 case X86::BI__builtin_ia32_rangess128_round_mask:
2304 case X86::BI__builtin_ia32_reducesd_mask:
2305 case X86::BI__builtin_ia32_reducess_mask:
2306 case X86::BI__builtin_ia32_rndscalesd_round_mask:
2307 case X86::BI__builtin_ia32_rndscaless_round_mask:
2308 ArgNum = 5;
2309 break;
Craig Topper7609f1c2016-10-01 21:03:50 +00002310 case X86::BI__builtin_ia32_vcvtsd2si64:
2311 case X86::BI__builtin_ia32_vcvtsd2si32:
2312 case X86::BI__builtin_ia32_vcvtsd2usi32:
2313 case X86::BI__builtin_ia32_vcvtsd2usi64:
2314 case X86::BI__builtin_ia32_vcvtss2si32:
2315 case X86::BI__builtin_ia32_vcvtss2si64:
2316 case X86::BI__builtin_ia32_vcvtss2usi32:
2317 case X86::BI__builtin_ia32_vcvtss2usi64:
2318 ArgNum = 1;
2319 HasRC = true;
2320 break;
Craig Topper8e066312016-11-07 07:01:09 +00002321 case X86::BI__builtin_ia32_cvtsi2sd64:
2322 case X86::BI__builtin_ia32_cvtsi2ss32:
2323 case X86::BI__builtin_ia32_cvtsi2ss64:
Craig Topper7609f1c2016-10-01 21:03:50 +00002324 case X86::BI__builtin_ia32_cvtusi2sd64:
2325 case X86::BI__builtin_ia32_cvtusi2ss32:
2326 case X86::BI__builtin_ia32_cvtusi2ss64:
2327 ArgNum = 2;
2328 HasRC = true;
2329 break;
2330 case X86::BI__builtin_ia32_cvtdq2ps512_mask:
2331 case X86::BI__builtin_ia32_cvtudq2ps512_mask:
2332 case X86::BI__builtin_ia32_cvtpd2ps512_mask:
2333 case X86::BI__builtin_ia32_cvtpd2qq512_mask:
2334 case X86::BI__builtin_ia32_cvtpd2uqq512_mask:
2335 case X86::BI__builtin_ia32_cvtps2qq512_mask:
2336 case X86::BI__builtin_ia32_cvtps2uqq512_mask:
2337 case X86::BI__builtin_ia32_cvtqq2pd512_mask:
2338 case X86::BI__builtin_ia32_cvtqq2ps512_mask:
2339 case X86::BI__builtin_ia32_cvtuqq2pd512_mask:
2340 case X86::BI__builtin_ia32_cvtuqq2ps512_mask:
Craig Topper8e066312016-11-07 07:01:09 +00002341 case X86::BI__builtin_ia32_sqrtpd512_mask:
2342 case X86::BI__builtin_ia32_sqrtps512_mask:
Craig Topper7609f1c2016-10-01 21:03:50 +00002343 ArgNum = 3;
2344 HasRC = true;
2345 break;
2346 case X86::BI__builtin_ia32_addpd512_mask:
2347 case X86::BI__builtin_ia32_addps512_mask:
2348 case X86::BI__builtin_ia32_divpd512_mask:
2349 case X86::BI__builtin_ia32_divps512_mask:
2350 case X86::BI__builtin_ia32_mulpd512_mask:
2351 case X86::BI__builtin_ia32_mulps512_mask:
2352 case X86::BI__builtin_ia32_subpd512_mask:
2353 case X86::BI__builtin_ia32_subps512_mask:
2354 case X86::BI__builtin_ia32_addss_round_mask:
2355 case X86::BI__builtin_ia32_addsd_round_mask:
2356 case X86::BI__builtin_ia32_divss_round_mask:
2357 case X86::BI__builtin_ia32_divsd_round_mask:
2358 case X86::BI__builtin_ia32_mulss_round_mask:
2359 case X86::BI__builtin_ia32_mulsd_round_mask:
2360 case X86::BI__builtin_ia32_subss_round_mask:
2361 case X86::BI__builtin_ia32_subsd_round_mask:
2362 case X86::BI__builtin_ia32_scalefpd512_mask:
2363 case X86::BI__builtin_ia32_scalefps512_mask:
2364 case X86::BI__builtin_ia32_scalefsd_round_mask:
2365 case X86::BI__builtin_ia32_scalefss_round_mask:
2366 case X86::BI__builtin_ia32_getmantpd512_mask:
2367 case X86::BI__builtin_ia32_getmantps512_mask:
Craig Topper8e066312016-11-07 07:01:09 +00002368 case X86::BI__builtin_ia32_cvtsd2ss_round_mask:
2369 case X86::BI__builtin_ia32_sqrtsd_round_mask:
2370 case X86::BI__builtin_ia32_sqrtss_round_mask:
Craig Topper7609f1c2016-10-01 21:03:50 +00002371 case X86::BI__builtin_ia32_vfmaddpd512_mask:
2372 case X86::BI__builtin_ia32_vfmaddpd512_mask3:
2373 case X86::BI__builtin_ia32_vfmaddpd512_maskz:
2374 case X86::BI__builtin_ia32_vfmaddps512_mask:
2375 case X86::BI__builtin_ia32_vfmaddps512_mask3:
2376 case X86::BI__builtin_ia32_vfmaddps512_maskz:
2377 case X86::BI__builtin_ia32_vfmaddsubpd512_mask:
2378 case X86::BI__builtin_ia32_vfmaddsubpd512_mask3:
2379 case X86::BI__builtin_ia32_vfmaddsubpd512_maskz:
2380 case X86::BI__builtin_ia32_vfmaddsubps512_mask:
2381 case X86::BI__builtin_ia32_vfmaddsubps512_mask3:
2382 case X86::BI__builtin_ia32_vfmaddsubps512_maskz:
2383 case X86::BI__builtin_ia32_vfmsubpd512_mask3:
2384 case X86::BI__builtin_ia32_vfmsubps512_mask3:
2385 case X86::BI__builtin_ia32_vfmsubaddpd512_mask3:
2386 case X86::BI__builtin_ia32_vfmsubaddps512_mask3:
2387 case X86::BI__builtin_ia32_vfnmaddpd512_mask:
2388 case X86::BI__builtin_ia32_vfnmaddps512_mask:
2389 case X86::BI__builtin_ia32_vfnmsubpd512_mask:
2390 case X86::BI__builtin_ia32_vfnmsubpd512_mask3:
2391 case X86::BI__builtin_ia32_vfnmsubps512_mask:
2392 case X86::BI__builtin_ia32_vfnmsubps512_mask3:
2393 case X86::BI__builtin_ia32_vfmaddsd3_mask:
2394 case X86::BI__builtin_ia32_vfmaddsd3_maskz:
2395 case X86::BI__builtin_ia32_vfmaddsd3_mask3:
2396 case X86::BI__builtin_ia32_vfmaddss3_mask:
2397 case X86::BI__builtin_ia32_vfmaddss3_maskz:
2398 case X86::BI__builtin_ia32_vfmaddss3_mask3:
2399 ArgNum = 4;
2400 HasRC = true;
2401 break;
2402 case X86::BI__builtin_ia32_getmantsd_round_mask:
2403 case X86::BI__builtin_ia32_getmantss_round_mask:
2404 ArgNum = 5;
2405 HasRC = true;
2406 break;
Craig Toppera7e253e2016-09-23 04:48:31 +00002407 }
2408
2409 llvm::APSInt Result;
2410
2411 // We can't check the value of a dependent argument.
2412 Expr *Arg = TheCall->getArg(ArgNum);
2413 if (Arg->isTypeDependent() || Arg->isValueDependent())
2414 return false;
2415
2416 // Check constant-ness first.
2417 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
2418 return true;
2419
2420 // Make sure rounding mode is either ROUND_CUR_DIRECTION or ROUND_NO_EXC bit
2421 // is set. If the intrinsic has rounding control(bits 1:0), make sure its only
2422 // combined with ROUND_NO_EXC.
2423 if (Result == 4/*ROUND_CUR_DIRECTION*/ ||
2424 Result == 8/*ROUND_NO_EXC*/ ||
2425 (HasRC && Result.getZExtValue() >= 8 && Result.getZExtValue() <= 11))
2426 return false;
2427
2428 return Diag(TheCall->getLocStart(), diag::err_x86_builtin_invalid_rounding)
2429 << Arg->getSourceRange();
2430}
2431
Craig Topperdf5beb22017-03-13 17:16:50 +00002432// Check if the gather/scatter scale is legal.
2433bool Sema::CheckX86BuiltinGatherScatterScale(unsigned BuiltinID,
2434 CallExpr *TheCall) {
2435 unsigned ArgNum = 0;
2436 switch (BuiltinID) {
2437 default:
2438 return false;
2439 case X86::BI__builtin_ia32_gatherpfdpd:
2440 case X86::BI__builtin_ia32_gatherpfdps:
2441 case X86::BI__builtin_ia32_gatherpfqpd:
2442 case X86::BI__builtin_ia32_gatherpfqps:
2443 case X86::BI__builtin_ia32_scatterpfdpd:
2444 case X86::BI__builtin_ia32_scatterpfdps:
2445 case X86::BI__builtin_ia32_scatterpfqpd:
2446 case X86::BI__builtin_ia32_scatterpfqps:
2447 ArgNum = 3;
2448 break;
2449 case X86::BI__builtin_ia32_gatherd_pd:
2450 case X86::BI__builtin_ia32_gatherd_pd256:
2451 case X86::BI__builtin_ia32_gatherq_pd:
2452 case X86::BI__builtin_ia32_gatherq_pd256:
2453 case X86::BI__builtin_ia32_gatherd_ps:
2454 case X86::BI__builtin_ia32_gatherd_ps256:
2455 case X86::BI__builtin_ia32_gatherq_ps:
2456 case X86::BI__builtin_ia32_gatherq_ps256:
2457 case X86::BI__builtin_ia32_gatherd_q:
2458 case X86::BI__builtin_ia32_gatherd_q256:
2459 case X86::BI__builtin_ia32_gatherq_q:
2460 case X86::BI__builtin_ia32_gatherq_q256:
2461 case X86::BI__builtin_ia32_gatherd_d:
2462 case X86::BI__builtin_ia32_gatherd_d256:
2463 case X86::BI__builtin_ia32_gatherq_d:
2464 case X86::BI__builtin_ia32_gatherq_d256:
2465 case X86::BI__builtin_ia32_gather3div2df:
2466 case X86::BI__builtin_ia32_gather3div2di:
2467 case X86::BI__builtin_ia32_gather3div4df:
2468 case X86::BI__builtin_ia32_gather3div4di:
2469 case X86::BI__builtin_ia32_gather3div4sf:
2470 case X86::BI__builtin_ia32_gather3div4si:
2471 case X86::BI__builtin_ia32_gather3div8sf:
2472 case X86::BI__builtin_ia32_gather3div8si:
2473 case X86::BI__builtin_ia32_gather3siv2df:
2474 case X86::BI__builtin_ia32_gather3siv2di:
2475 case X86::BI__builtin_ia32_gather3siv4df:
2476 case X86::BI__builtin_ia32_gather3siv4di:
2477 case X86::BI__builtin_ia32_gather3siv4sf:
2478 case X86::BI__builtin_ia32_gather3siv4si:
2479 case X86::BI__builtin_ia32_gather3siv8sf:
2480 case X86::BI__builtin_ia32_gather3siv8si:
2481 case X86::BI__builtin_ia32_gathersiv8df:
2482 case X86::BI__builtin_ia32_gathersiv16sf:
2483 case X86::BI__builtin_ia32_gatherdiv8df:
2484 case X86::BI__builtin_ia32_gatherdiv16sf:
2485 case X86::BI__builtin_ia32_gathersiv8di:
2486 case X86::BI__builtin_ia32_gathersiv16si:
2487 case X86::BI__builtin_ia32_gatherdiv8di:
2488 case X86::BI__builtin_ia32_gatherdiv16si:
2489 case X86::BI__builtin_ia32_scatterdiv2df:
2490 case X86::BI__builtin_ia32_scatterdiv2di:
2491 case X86::BI__builtin_ia32_scatterdiv4df:
2492 case X86::BI__builtin_ia32_scatterdiv4di:
2493 case X86::BI__builtin_ia32_scatterdiv4sf:
2494 case X86::BI__builtin_ia32_scatterdiv4si:
2495 case X86::BI__builtin_ia32_scatterdiv8sf:
2496 case X86::BI__builtin_ia32_scatterdiv8si:
2497 case X86::BI__builtin_ia32_scattersiv2df:
2498 case X86::BI__builtin_ia32_scattersiv2di:
2499 case X86::BI__builtin_ia32_scattersiv4df:
2500 case X86::BI__builtin_ia32_scattersiv4di:
2501 case X86::BI__builtin_ia32_scattersiv4sf:
2502 case X86::BI__builtin_ia32_scattersiv4si:
2503 case X86::BI__builtin_ia32_scattersiv8sf:
2504 case X86::BI__builtin_ia32_scattersiv8si:
2505 case X86::BI__builtin_ia32_scattersiv8df:
2506 case X86::BI__builtin_ia32_scattersiv16sf:
2507 case X86::BI__builtin_ia32_scatterdiv8df:
2508 case X86::BI__builtin_ia32_scatterdiv16sf:
2509 case X86::BI__builtin_ia32_scattersiv8di:
2510 case X86::BI__builtin_ia32_scattersiv16si:
2511 case X86::BI__builtin_ia32_scatterdiv8di:
2512 case X86::BI__builtin_ia32_scatterdiv16si:
2513 ArgNum = 4;
2514 break;
2515 }
2516
2517 llvm::APSInt Result;
2518
2519 // We can't check the value of a dependent argument.
2520 Expr *Arg = TheCall->getArg(ArgNum);
2521 if (Arg->isTypeDependent() || Arg->isValueDependent())
2522 return false;
2523
2524 // Check constant-ness first.
2525 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
2526 return true;
2527
2528 if (Result == 1 || Result == 2 || Result == 4 || Result == 8)
2529 return false;
2530
2531 return Diag(TheCall->getLocStart(), diag::err_x86_builtin_invalid_scale)
2532 << Arg->getSourceRange();
2533}
2534
Craig Topper51738f82018-04-26 20:14:46 +00002535static bool isX86_32Builtin(unsigned BuiltinID) {
2536 // These builtins only work on x86-32 targets.
2537 switch (BuiltinID) {
2538 case X86::BI__builtin_ia32_readeflags_u32:
2539 case X86::BI__builtin_ia32_writeeflags_u32:
2540 return true;
2541 }
2542
2543 return false;
2544}
2545
Craig Topperf0ddc892016-09-23 04:48:27 +00002546bool Sema::CheckX86BuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
2547 if (BuiltinID == X86::BI__builtin_cpu_supports)
2548 return SemaBuiltinCpuSupports(*this, TheCall);
2549
Craig Topper699ae0c2017-08-10 20:28:30 +00002550 if (BuiltinID == X86::BI__builtin_cpu_is)
2551 return SemaBuiltinCpuIs(*this, TheCall);
2552
Craig Topper51738f82018-04-26 20:14:46 +00002553 // Check for 32-bit only builtins on a 64-bit target.
2554 const llvm::Triple &TT = Context.getTargetInfo().getTriple();
2555 if (TT.getArch() != llvm::Triple::x86 && isX86_32Builtin(BuiltinID))
2556 return Diag(TheCall->getCallee()->getLocStart(),
2557 diag::err_32_bit_builtin_64_bit_tgt);
2558
Craig Toppera7e253e2016-09-23 04:48:31 +00002559 // If the intrinsic has rounding or SAE make sure its valid.
2560 if (CheckX86BuiltinRoundingOrSAE(BuiltinID, TheCall))
2561 return true;
2562
Craig Topperdf5beb22017-03-13 17:16:50 +00002563 // If the intrinsic has a gather/scatter scale immediate make sure its valid.
2564 if (CheckX86BuiltinGatherScatterScale(BuiltinID, TheCall))
2565 return true;
2566
Craig Topperf0ddc892016-09-23 04:48:27 +00002567 // For intrinsics which take an immediate value as part of the instruction,
2568 // range check them here.
2569 int i = 0, l = 0, u = 0;
2570 switch (BuiltinID) {
2571 default:
2572 return false;
Richard Trieucc3949d2016-02-18 22:34:54 +00002573 case X86::BI_mm_prefetch:
Craig Topper170de4b2017-12-21 23:50:22 +00002574 i = 1; l = 0; u = 7;
Craig Topper39c87102016-05-18 03:18:12 +00002575 break;
Richard Trieucc3949d2016-02-18 22:34:54 +00002576 case X86::BI__builtin_ia32_sha1rnds4:
Craig Topper39c87102016-05-18 03:18:12 +00002577 i = 2; l = 0; u = 3;
Richard Trieucc3949d2016-02-18 22:34:54 +00002578 break;
Craig Topper1a8b0472015-01-31 08:57:52 +00002579 case X86::BI__builtin_ia32_vpermil2pd:
2580 case X86::BI__builtin_ia32_vpermil2pd256:
2581 case X86::BI__builtin_ia32_vpermil2ps:
Richard Trieucc3949d2016-02-18 22:34:54 +00002582 case X86::BI__builtin_ia32_vpermil2ps256:
Craig Topper39c87102016-05-18 03:18:12 +00002583 i = 3; l = 0; u = 3;
Richard Trieucc3949d2016-02-18 22:34:54 +00002584 break;
Craig Topper95b0d732015-01-25 23:30:05 +00002585 case X86::BI__builtin_ia32_cmpb128_mask:
2586 case X86::BI__builtin_ia32_cmpw128_mask:
2587 case X86::BI__builtin_ia32_cmpd128_mask:
2588 case X86::BI__builtin_ia32_cmpq128_mask:
2589 case X86::BI__builtin_ia32_cmpb256_mask:
2590 case X86::BI__builtin_ia32_cmpw256_mask:
2591 case X86::BI__builtin_ia32_cmpd256_mask:
2592 case X86::BI__builtin_ia32_cmpq256_mask:
2593 case X86::BI__builtin_ia32_cmpb512_mask:
2594 case X86::BI__builtin_ia32_cmpw512_mask:
2595 case X86::BI__builtin_ia32_cmpd512_mask:
2596 case X86::BI__builtin_ia32_cmpq512_mask:
2597 case X86::BI__builtin_ia32_ucmpb128_mask:
2598 case X86::BI__builtin_ia32_ucmpw128_mask:
2599 case X86::BI__builtin_ia32_ucmpd128_mask:
2600 case X86::BI__builtin_ia32_ucmpq128_mask:
2601 case X86::BI__builtin_ia32_ucmpb256_mask:
2602 case X86::BI__builtin_ia32_ucmpw256_mask:
2603 case X86::BI__builtin_ia32_ucmpd256_mask:
2604 case X86::BI__builtin_ia32_ucmpq256_mask:
2605 case X86::BI__builtin_ia32_ucmpb512_mask:
2606 case X86::BI__builtin_ia32_ucmpw512_mask:
2607 case X86::BI__builtin_ia32_ucmpd512_mask:
Richard Trieucc3949d2016-02-18 22:34:54 +00002608 case X86::BI__builtin_ia32_ucmpq512_mask:
Craig Topper8dd7d0d2015-02-13 06:04:48 +00002609 case X86::BI__builtin_ia32_vpcomub:
2610 case X86::BI__builtin_ia32_vpcomuw:
2611 case X86::BI__builtin_ia32_vpcomud:
2612 case X86::BI__builtin_ia32_vpcomuq:
2613 case X86::BI__builtin_ia32_vpcomb:
2614 case X86::BI__builtin_ia32_vpcomw:
2615 case X86::BI__builtin_ia32_vpcomd:
Richard Trieucc3949d2016-02-18 22:34:54 +00002616 case X86::BI__builtin_ia32_vpcomq:
Craig Topper39c87102016-05-18 03:18:12 +00002617 i = 2; l = 0; u = 7;
2618 break;
2619 case X86::BI__builtin_ia32_roundps:
2620 case X86::BI__builtin_ia32_roundpd:
2621 case X86::BI__builtin_ia32_roundps256:
2622 case X86::BI__builtin_ia32_roundpd256:
Craig Topper39c87102016-05-18 03:18:12 +00002623 i = 1; l = 0; u = 15;
2624 break;
2625 case X86::BI__builtin_ia32_roundss:
2626 case X86::BI__builtin_ia32_roundsd:
2627 case X86::BI__builtin_ia32_rangepd128_mask:
2628 case X86::BI__builtin_ia32_rangepd256_mask:
2629 case X86::BI__builtin_ia32_rangepd512_mask:
2630 case X86::BI__builtin_ia32_rangeps128_mask:
2631 case X86::BI__builtin_ia32_rangeps256_mask:
2632 case X86::BI__builtin_ia32_rangeps512_mask:
2633 case X86::BI__builtin_ia32_getmantsd_round_mask:
2634 case X86::BI__builtin_ia32_getmantss_round_mask:
Craig Topper39c87102016-05-18 03:18:12 +00002635 i = 2; l = 0; u = 15;
2636 break;
2637 case X86::BI__builtin_ia32_cmpps:
2638 case X86::BI__builtin_ia32_cmpss:
2639 case X86::BI__builtin_ia32_cmppd:
2640 case X86::BI__builtin_ia32_cmpsd:
2641 case X86::BI__builtin_ia32_cmpps256:
2642 case X86::BI__builtin_ia32_cmppd256:
2643 case X86::BI__builtin_ia32_cmpps128_mask:
2644 case X86::BI__builtin_ia32_cmppd128_mask:
2645 case X86::BI__builtin_ia32_cmpps256_mask:
2646 case X86::BI__builtin_ia32_cmppd256_mask:
2647 case X86::BI__builtin_ia32_cmpps512_mask:
2648 case X86::BI__builtin_ia32_cmppd512_mask:
2649 case X86::BI__builtin_ia32_cmpsd_mask:
2650 case X86::BI__builtin_ia32_cmpss_mask:
2651 i = 2; l = 0; u = 31;
2652 break;
Craig Topper39c87102016-05-18 03:18:12 +00002653 case X86::BI__builtin_ia32_vcvtps2ph:
Craig Topper23a30222017-11-08 04:54:26 +00002654 case X86::BI__builtin_ia32_vcvtps2ph_mask:
Craig Topper39c87102016-05-18 03:18:12 +00002655 case X86::BI__builtin_ia32_vcvtps2ph256:
Craig Topper23a30222017-11-08 04:54:26 +00002656 case X86::BI__builtin_ia32_vcvtps2ph256_mask:
2657 case X86::BI__builtin_ia32_vcvtps2ph512_mask:
Craig Topper39c87102016-05-18 03:18:12 +00002658 case X86::BI__builtin_ia32_rndscaleps_128_mask:
2659 case X86::BI__builtin_ia32_rndscalepd_128_mask:
2660 case X86::BI__builtin_ia32_rndscaleps_256_mask:
2661 case X86::BI__builtin_ia32_rndscalepd_256_mask:
2662 case X86::BI__builtin_ia32_rndscaleps_mask:
2663 case X86::BI__builtin_ia32_rndscalepd_mask:
2664 case X86::BI__builtin_ia32_reducepd128_mask:
2665 case X86::BI__builtin_ia32_reducepd256_mask:
2666 case X86::BI__builtin_ia32_reducepd512_mask:
2667 case X86::BI__builtin_ia32_reduceps128_mask:
2668 case X86::BI__builtin_ia32_reduceps256_mask:
2669 case X86::BI__builtin_ia32_reduceps512_mask:
2670 case X86::BI__builtin_ia32_prold512_mask:
2671 case X86::BI__builtin_ia32_prolq512_mask:
2672 case X86::BI__builtin_ia32_prold128_mask:
2673 case X86::BI__builtin_ia32_prold256_mask:
2674 case X86::BI__builtin_ia32_prolq128_mask:
2675 case X86::BI__builtin_ia32_prolq256_mask:
2676 case X86::BI__builtin_ia32_prord128_mask:
2677 case X86::BI__builtin_ia32_prord256_mask:
2678 case X86::BI__builtin_ia32_prorq128_mask:
2679 case X86::BI__builtin_ia32_prorq256_mask:
Craig Topper39c87102016-05-18 03:18:12 +00002680 case X86::BI__builtin_ia32_fpclasspd128_mask:
2681 case X86::BI__builtin_ia32_fpclasspd256_mask:
2682 case X86::BI__builtin_ia32_fpclassps128_mask:
2683 case X86::BI__builtin_ia32_fpclassps256_mask:
2684 case X86::BI__builtin_ia32_fpclassps512_mask:
2685 case X86::BI__builtin_ia32_fpclasspd512_mask:
2686 case X86::BI__builtin_ia32_fpclasssd_mask:
2687 case X86::BI__builtin_ia32_fpclassss_mask:
Craig Topper39c87102016-05-18 03:18:12 +00002688 i = 1; l = 0; u = 255;
2689 break;
Craig Topper39c87102016-05-18 03:18:12 +00002690 case X86::BI__builtin_ia32_palignr128:
2691 case X86::BI__builtin_ia32_palignr256:
Craig Topper8e3689c2018-05-22 20:48:24 +00002692 case X86::BI__builtin_ia32_palignr512:
Craig Topper39c87102016-05-18 03:18:12 +00002693 case X86::BI__builtin_ia32_vcomisd:
2694 case X86::BI__builtin_ia32_vcomiss:
Craig Topper39c87102016-05-18 03:18:12 +00002695 case X86::BI__builtin_ia32_dbpsadbw128_mask:
2696 case X86::BI__builtin_ia32_dbpsadbw256_mask:
2697 case X86::BI__builtin_ia32_dbpsadbw512_mask:
Craig Topper91926432018-01-11 01:38:02 +00002698 case X86::BI__builtin_ia32_vpshldd128_mask:
2699 case X86::BI__builtin_ia32_vpshldd256_mask:
2700 case X86::BI__builtin_ia32_vpshldd512_mask:
2701 case X86::BI__builtin_ia32_vpshldq128_mask:
2702 case X86::BI__builtin_ia32_vpshldq256_mask:
2703 case X86::BI__builtin_ia32_vpshldq512_mask:
2704 case X86::BI__builtin_ia32_vpshldw128_mask:
2705 case X86::BI__builtin_ia32_vpshldw256_mask:
2706 case X86::BI__builtin_ia32_vpshldw512_mask:
2707 case X86::BI__builtin_ia32_vpshrdd128_mask:
2708 case X86::BI__builtin_ia32_vpshrdd256_mask:
2709 case X86::BI__builtin_ia32_vpshrdd512_mask:
2710 case X86::BI__builtin_ia32_vpshrdq128_mask:
2711 case X86::BI__builtin_ia32_vpshrdq256_mask:
2712 case X86::BI__builtin_ia32_vpshrdq512_mask:
2713 case X86::BI__builtin_ia32_vpshrdw128_mask:
2714 case X86::BI__builtin_ia32_vpshrdw256_mask:
2715 case X86::BI__builtin_ia32_vpshrdw512_mask:
Craig Topper39c87102016-05-18 03:18:12 +00002716 i = 2; l = 0; u = 255;
2717 break;
2718 case X86::BI__builtin_ia32_fixupimmpd512_mask:
2719 case X86::BI__builtin_ia32_fixupimmpd512_maskz:
2720 case X86::BI__builtin_ia32_fixupimmps512_mask:
2721 case X86::BI__builtin_ia32_fixupimmps512_maskz:
2722 case X86::BI__builtin_ia32_fixupimmsd_mask:
2723 case X86::BI__builtin_ia32_fixupimmsd_maskz:
2724 case X86::BI__builtin_ia32_fixupimmss_mask:
2725 case X86::BI__builtin_ia32_fixupimmss_maskz:
2726 case X86::BI__builtin_ia32_fixupimmpd128_mask:
2727 case X86::BI__builtin_ia32_fixupimmpd128_maskz:
2728 case X86::BI__builtin_ia32_fixupimmpd256_mask:
2729 case X86::BI__builtin_ia32_fixupimmpd256_maskz:
2730 case X86::BI__builtin_ia32_fixupimmps128_mask:
2731 case X86::BI__builtin_ia32_fixupimmps128_maskz:
2732 case X86::BI__builtin_ia32_fixupimmps256_mask:
2733 case X86::BI__builtin_ia32_fixupimmps256_maskz:
2734 case X86::BI__builtin_ia32_pternlogd512_mask:
2735 case X86::BI__builtin_ia32_pternlogd512_maskz:
2736 case X86::BI__builtin_ia32_pternlogq512_mask:
2737 case X86::BI__builtin_ia32_pternlogq512_maskz:
2738 case X86::BI__builtin_ia32_pternlogd128_mask:
2739 case X86::BI__builtin_ia32_pternlogd128_maskz:
2740 case X86::BI__builtin_ia32_pternlogd256_mask:
2741 case X86::BI__builtin_ia32_pternlogd256_maskz:
2742 case X86::BI__builtin_ia32_pternlogq128_mask:
2743 case X86::BI__builtin_ia32_pternlogq128_maskz:
2744 case X86::BI__builtin_ia32_pternlogq256_mask:
2745 case X86::BI__builtin_ia32_pternlogq256_maskz:
2746 i = 3; l = 0; u = 255;
2747 break;
Craig Topper9625db02017-03-12 22:19:10 +00002748 case X86::BI__builtin_ia32_gatherpfdpd:
2749 case X86::BI__builtin_ia32_gatherpfdps:
2750 case X86::BI__builtin_ia32_gatherpfqpd:
2751 case X86::BI__builtin_ia32_gatherpfqps:
2752 case X86::BI__builtin_ia32_scatterpfdpd:
2753 case X86::BI__builtin_ia32_scatterpfdps:
2754 case X86::BI__builtin_ia32_scatterpfqpd:
2755 case X86::BI__builtin_ia32_scatterpfqps:
Craig Topperf771f79b2017-03-31 17:22:30 +00002756 i = 4; l = 2; u = 3;
Craig Topper9625db02017-03-12 22:19:10 +00002757 break;
Craig Topper39c87102016-05-18 03:18:12 +00002758 case X86::BI__builtin_ia32_rndscalesd_round_mask:
2759 case X86::BI__builtin_ia32_rndscaless_round_mask:
2760 i = 4; l = 0; u = 255;
Richard Trieucc3949d2016-02-18 22:34:54 +00002761 break;
Warren Hunt20e4a5d2014-02-21 23:08:53 +00002762 }
Craig Topperdd84ec52014-12-27 07:00:08 +00002763 return SemaBuiltinConstantArgRange(TheCall, i, l, u);
Warren Hunt20e4a5d2014-02-21 23:08:53 +00002764}
2765
Richard Smith55ce3522012-06-25 20:30:08 +00002766/// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
2767/// parameter with the FormatAttr's correct format_idx and firstDataArg.
2768/// Returns true when the format fits the function and the FormatStringInfo has
2769/// been populated.
2770bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
2771 FormatStringInfo *FSI) {
2772 FSI->HasVAListArg = Format->getFirstArg() == 0;
2773 FSI->FormatIdx = Format->getFormatIdx() - 1;
2774 FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1;
Anders Carlssonbc4c1072009-08-16 01:56:34 +00002775
Richard Smith55ce3522012-06-25 20:30:08 +00002776 // The way the format attribute works in GCC, the implicit this argument
2777 // of member functions is counted. However, it doesn't appear in our own
2778 // lists, so decrement format_idx in that case.
2779 if (IsCXXMember) {
2780 if(FSI->FormatIdx == 0)
2781 return false;
2782 --FSI->FormatIdx;
2783 if (FSI->FirstDataArg != 0)
2784 --FSI->FirstDataArg;
2785 }
2786 return true;
2787}
Mike Stump11289f42009-09-09 15:08:12 +00002788
Ted Kremenekef9e7f82014-01-22 06:10:28 +00002789/// Checks if a the given expression evaluates to null.
2790///
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002791/// Returns true if the value evaluates to null.
George Burgess IV850269a2015-12-08 22:02:00 +00002792static bool CheckNonNullExpr(Sema &S, const Expr *Expr) {
Douglas Gregorb4866e82015-06-19 18:13:19 +00002793 // If the expression has non-null type, it doesn't evaluate to null.
2794 if (auto nullability
2795 = Expr->IgnoreImplicit()->getType()->getNullability(S.Context)) {
2796 if (*nullability == NullabilityKind::NonNull)
2797 return false;
2798 }
2799
Ted Kremeneka146db32014-01-17 06:24:47 +00002800 // As a special case, transparent unions initialized with zero are
2801 // considered null for the purposes of the nonnull attribute.
Ted Kremenekef9e7f82014-01-22 06:10:28 +00002802 if (const RecordType *UT = Expr->getType()->getAsUnionType()) {
Ted Kremeneka146db32014-01-17 06:24:47 +00002803 if (UT->getDecl()->hasAttr<TransparentUnionAttr>())
2804 if (const CompoundLiteralExpr *CLE =
Ted Kremenekef9e7f82014-01-22 06:10:28 +00002805 dyn_cast<CompoundLiteralExpr>(Expr))
Ted Kremeneka146db32014-01-17 06:24:47 +00002806 if (const InitListExpr *ILE =
2807 dyn_cast<InitListExpr>(CLE->getInitializer()))
Ted Kremenekef9e7f82014-01-22 06:10:28 +00002808 Expr = ILE->getInit(0);
Ted Kremeneka146db32014-01-17 06:24:47 +00002809 }
2810
2811 bool Result;
Artyom Skrobov9f213442014-01-24 11:10:39 +00002812 return (!Expr->isValueDependent() &&
2813 Expr->EvaluateAsBooleanCondition(Result, S.Context) &&
2814 !Result);
Ted Kremenekef9e7f82014-01-22 06:10:28 +00002815}
2816
2817static void CheckNonNullArgument(Sema &S,
2818 const Expr *ArgExpr,
2819 SourceLocation CallSiteLoc) {
2820 if (CheckNonNullExpr(S, ArgExpr))
Eric Fiselier18677d52015-10-09 00:17:57 +00002821 S.DiagRuntimeBehavior(CallSiteLoc, ArgExpr,
2822 S.PDiag(diag::warn_null_arg) << ArgExpr->getSourceRange());
Ted Kremeneka146db32014-01-17 06:24:47 +00002823}
2824
Fariborz Jahanianfba4fe62014-09-11 19:13:23 +00002825bool Sema::GetFormatNSStringIdx(const FormatAttr *Format, unsigned &Idx) {
2826 FormatStringInfo FSI;
2827 if ((GetFormatStringType(Format) == FST_NSString) &&
2828 getFormatStringInfo(Format, false, &FSI)) {
2829 Idx = FSI.FormatIdx;
2830 return true;
2831 }
2832 return false;
2833}
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00002834
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002835/// Diagnose use of %s directive in an NSString which is being passed
Fariborz Jahanian6485fe42014-09-09 23:10:54 +00002836/// as formatting string to formatting method.
2837static void
2838DiagnoseCStringFormatDirectiveInCFAPI(Sema &S,
2839 const NamedDecl *FDecl,
2840 Expr **Args,
2841 unsigned NumArgs) {
Fariborz Jahanianfba4fe62014-09-11 19:13:23 +00002842 unsigned Idx = 0;
2843 bool Format = false;
Fariborz Jahanian6485fe42014-09-09 23:10:54 +00002844 ObjCStringFormatFamily SFFamily = FDecl->getObjCFStringFormattingFamily();
2845 if (SFFamily == ObjCStringFormatFamily::SFF_CFString) {
Fariborz Jahanianfba4fe62014-09-11 19:13:23 +00002846 Idx = 2;
2847 Format = true;
2848 }
2849 else
2850 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
2851 if (S.GetFormatNSStringIdx(I, Idx)) {
2852 Format = true;
2853 break;
2854 }
Fariborz Jahanian6485fe42014-09-09 23:10:54 +00002855 }
Fariborz Jahanianfba4fe62014-09-11 19:13:23 +00002856 if (!Format || NumArgs <= Idx)
2857 return;
2858 const Expr *FormatExpr = Args[Idx];
2859 if (const CStyleCastExpr *CSCE = dyn_cast<CStyleCastExpr>(FormatExpr))
2860 FormatExpr = CSCE->getSubExpr();
2861 const StringLiteral *FormatString;
2862 if (const ObjCStringLiteral *OSL =
2863 dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts()))
2864 FormatString = OSL->getString();
2865 else
2866 FormatString = dyn_cast<StringLiteral>(FormatExpr->IgnoreParenImpCasts());
2867 if (!FormatString)
2868 return;
2869 if (S.FormatStringHasSArg(FormatString)) {
2870 S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
2871 << "%s" << 1 << 1;
2872 S.Diag(FDecl->getLocation(), diag::note_entity_declared_at)
2873 << FDecl->getDeclName();
Fariborz Jahanian6485fe42014-09-09 23:10:54 +00002874 }
2875}
2876
Douglas Gregorb4866e82015-06-19 18:13:19 +00002877/// Determine whether the given type has a non-null nullability annotation.
2878static bool isNonNullType(ASTContext &ctx, QualType type) {
2879 if (auto nullability = type->getNullability(ctx))
2880 return *nullability == NullabilityKind::NonNull;
2881
2882 return false;
2883}
2884
Ted Kremenek2bc73332014-01-17 06:24:43 +00002885static void CheckNonNullArguments(Sema &S,
Ted Kremeneka146db32014-01-17 06:24:47 +00002886 const NamedDecl *FDecl,
Douglas Gregorb4866e82015-06-19 18:13:19 +00002887 const FunctionProtoType *Proto,
Richard Smith588bd9b2014-08-27 04:59:42 +00002888 ArrayRef<const Expr *> Args,
Ted Kremenek2bc73332014-01-17 06:24:43 +00002889 SourceLocation CallSiteLoc) {
Douglas Gregorb4866e82015-06-19 18:13:19 +00002890 assert((FDecl || Proto) && "Need a function declaration or prototype");
2891
Ted Kremenek9aedc152014-01-17 06:24:56 +00002892 // Check the attributes attached to the method/function itself.
Richard Smith588bd9b2014-08-27 04:59:42 +00002893 llvm::SmallBitVector NonNullArgs;
Douglas Gregorb4866e82015-06-19 18:13:19 +00002894 if (FDecl) {
2895 // Handle the nonnull attribute on the function/method declaration itself.
2896 for (const auto *NonNull : FDecl->specific_attrs<NonNullAttr>()) {
2897 if (!NonNull->args_size()) {
2898 // Easy case: all pointer arguments are nonnull.
2899 for (const auto *Arg : Args)
2900 if (S.isValidPointerAttrType(Arg->getType()))
2901 CheckNonNullArgument(S, Arg, CallSiteLoc);
2902 return;
2903 }
Richard Smith588bd9b2014-08-27 04:59:42 +00002904
Joel E. Denny81508102018-03-13 14:51:22 +00002905 for (const ParamIdx &Idx : NonNull->args()) {
2906 unsigned IdxAST = Idx.getASTIndex();
2907 if (IdxAST >= Args.size())
Douglas Gregorb4866e82015-06-19 18:13:19 +00002908 continue;
2909 if (NonNullArgs.empty())
2910 NonNullArgs.resize(Args.size());
Joel E. Denny81508102018-03-13 14:51:22 +00002911 NonNullArgs.set(IdxAST);
Douglas Gregorb4866e82015-06-19 18:13:19 +00002912 }
Richard Smith588bd9b2014-08-27 04:59:42 +00002913 }
Ted Kremenek2bc73332014-01-17 06:24:43 +00002914 }
Ted Kremenek9aedc152014-01-17 06:24:56 +00002915
Douglas Gregorb4866e82015-06-19 18:13:19 +00002916 if (FDecl && (isa<FunctionDecl>(FDecl) || isa<ObjCMethodDecl>(FDecl))) {
2917 // Handle the nonnull attribute on the parameters of the
2918 // function/method.
2919 ArrayRef<ParmVarDecl*> parms;
2920 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FDecl))
2921 parms = FD->parameters();
2922 else
2923 parms = cast<ObjCMethodDecl>(FDecl)->parameters();
2924
2925 unsigned ParamIndex = 0;
2926 for (ArrayRef<ParmVarDecl*>::iterator I = parms.begin(), E = parms.end();
2927 I != E; ++I, ++ParamIndex) {
2928 const ParmVarDecl *PVD = *I;
2929 if (PVD->hasAttr<NonNullAttr>() ||
2930 isNonNullType(S.Context, PVD->getType())) {
2931 if (NonNullArgs.empty())
2932 NonNullArgs.resize(Args.size());
Ted Kremenek9aedc152014-01-17 06:24:56 +00002933
Douglas Gregorb4866e82015-06-19 18:13:19 +00002934 NonNullArgs.set(ParamIndex);
2935 }
2936 }
2937 } else {
2938 // If we have a non-function, non-method declaration but no
2939 // function prototype, try to dig out the function prototype.
2940 if (!Proto) {
2941 if (const ValueDecl *VD = dyn_cast<ValueDecl>(FDecl)) {
2942 QualType type = VD->getType().getNonReferenceType();
2943 if (auto pointerType = type->getAs<PointerType>())
2944 type = pointerType->getPointeeType();
2945 else if (auto blockType = type->getAs<BlockPointerType>())
2946 type = blockType->getPointeeType();
2947 // FIXME: data member pointers?
2948
2949 // Dig out the function prototype, if there is one.
2950 Proto = type->getAs<FunctionProtoType>();
2951 }
2952 }
2953
2954 // Fill in non-null argument information from the nullability
2955 // information on the parameter types (if we have them).
2956 if (Proto) {
2957 unsigned Index = 0;
2958 for (auto paramType : Proto->getParamTypes()) {
2959 if (isNonNullType(S.Context, paramType)) {
2960 if (NonNullArgs.empty())
2961 NonNullArgs.resize(Args.size());
2962
2963 NonNullArgs.set(Index);
2964 }
2965
2966 ++Index;
2967 }
2968 }
Ted Kremenek9aedc152014-01-17 06:24:56 +00002969 }
Richard Smith588bd9b2014-08-27 04:59:42 +00002970
Douglas Gregorb4866e82015-06-19 18:13:19 +00002971 // Check for non-null arguments.
2972 for (unsigned ArgIndex = 0, ArgIndexEnd = NonNullArgs.size();
2973 ArgIndex != ArgIndexEnd; ++ArgIndex) {
Richard Smith588bd9b2014-08-27 04:59:42 +00002974 if (NonNullArgs[ArgIndex])
2975 CheckNonNullArgument(S, Args[ArgIndex], CallSiteLoc);
Douglas Gregorb4866e82015-06-19 18:13:19 +00002976 }
Ted Kremenek2bc73332014-01-17 06:24:43 +00002977}
2978
Richard Smith55ce3522012-06-25 20:30:08 +00002979/// Handles the checks for format strings, non-POD arguments to vararg
George Burgess IVce6284b2017-01-28 02:19:40 +00002980/// functions, NULL arguments passed to non-NULL parameters, and diagnose_if
2981/// attributes.
Douglas Gregorb4866e82015-06-19 18:13:19 +00002982void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
George Burgess IVce6284b2017-01-28 02:19:40 +00002983 const Expr *ThisArg, ArrayRef<const Expr *> Args,
2984 bool IsMemberFunction, SourceLocation Loc,
2985 SourceRange Range, VariadicCallType CallType) {
Richard Smithd7293d72013-08-05 18:49:43 +00002986 // FIXME: We should check as much as we can in the template definition.
Jordan Rose3c14b232012-10-02 01:49:54 +00002987 if (CurContext->isDependentContext())
2988 return;
Daniel Dunbardd9b2d12008-10-02 18:44:07 +00002989
Ted Kremenekb8176da2010-09-09 04:33:05 +00002990 // Printf and scanf checking.
Richard Smithd7293d72013-08-05 18:49:43 +00002991 llvm::SmallBitVector CheckedVarArgs;
2992 if (FDecl) {
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002993 for (const auto *I : FDecl->specific_attrs<FormatAttr>()) {
Benjamin Kramer989ab8b2013-08-09 09:39:17 +00002994 // Only create vector if there are format attributes.
2995 CheckedVarArgs.resize(Args.size());
2996
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002997 CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range,
Benjamin Kramerf62e81d2013-08-08 11:08:26 +00002998 CheckedVarArgs);
Benjamin Kramer989ab8b2013-08-09 09:39:17 +00002999 }
Richard Smithd7293d72013-08-05 18:49:43 +00003000 }
Richard Smith55ce3522012-06-25 20:30:08 +00003001
3002 // Refuse POD arguments that weren't caught by the format string
3003 // checks above.
Richard Smith836de6b2016-12-19 23:59:34 +00003004 auto *FD = dyn_cast_or_null<FunctionDecl>(FDecl);
3005 if (CallType != VariadicDoesNotApply &&
3006 (!FD || FD->getBuiltinID() != Builtin::BI__noop)) {
Douglas Gregorb4866e82015-06-19 18:13:19 +00003007 unsigned NumParams = Proto ? Proto->getNumParams()
3008 : FDecl && isa<FunctionDecl>(FDecl)
3009 ? cast<FunctionDecl>(FDecl)->getNumParams()
3010 : FDecl && isa<ObjCMethodDecl>(FDecl)
3011 ? cast<ObjCMethodDecl>(FDecl)->param_size()
3012 : 0;
3013
Alp Toker9cacbab2014-01-20 20:26:09 +00003014 for (unsigned ArgIdx = NumParams; ArgIdx < Args.size(); ++ArgIdx) {
Ted Kremenek241f1ef2012-10-11 19:06:43 +00003015 // Args[ArgIdx] can be null in malformed code.
Richard Smithd7293d72013-08-05 18:49:43 +00003016 if (const Expr *Arg = Args[ArgIdx]) {
3017 if (CheckedVarArgs.empty() || !CheckedVarArgs[ArgIdx])
3018 checkVariadicArgument(Arg, CallType);
3019 }
Ted Kremenek241f1ef2012-10-11 19:06:43 +00003020 }
Richard Smithd7293d72013-08-05 18:49:43 +00003021 }
Mike Stump11289f42009-09-09 15:08:12 +00003022
Douglas Gregorb4866e82015-06-19 18:13:19 +00003023 if (FDecl || Proto) {
3024 CheckNonNullArguments(*this, FDecl, Proto, Args, Loc);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003025
Richard Trieu41bc0992013-06-22 00:20:41 +00003026 // Type safety checking.
Douglas Gregorb4866e82015-06-19 18:13:19 +00003027 if (FDecl) {
3028 for (const auto *I : FDecl->specific_attrs<ArgumentWithTypeTagAttr>())
Aaron Ballmand1f6dcd2017-11-29 23:10:14 +00003029 CheckArgumentWithTypeTag(I, Args, Loc);
Douglas Gregorb4866e82015-06-19 18:13:19 +00003030 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00003031 }
George Burgess IVce6284b2017-01-28 02:19:40 +00003032
3033 if (FD)
3034 diagnoseArgDependentDiagnoseIfAttrs(FD, ThisArg, Args, Loc);
Richard Smith55ce3522012-06-25 20:30:08 +00003035}
3036
3037/// CheckConstructorCall - Check a constructor call for correctness and safety
3038/// properties not enforced by the C type system.
Dmitri Gribenko765396f2013-01-13 20:46:02 +00003039void Sema::CheckConstructorCall(FunctionDecl *FDecl,
3040 ArrayRef<const Expr *> Args,
Richard Smith55ce3522012-06-25 20:30:08 +00003041 const FunctionProtoType *Proto,
3042 SourceLocation Loc) {
3043 VariadicCallType CallType =
3044 Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply;
George Burgess IVce6284b2017-01-28 02:19:40 +00003045 checkCall(FDecl, Proto, /*ThisArg=*/nullptr, Args, /*IsMemberFunction=*/true,
3046 Loc, SourceRange(), CallType);
Richard Smith55ce3522012-06-25 20:30:08 +00003047}
3048
3049/// CheckFunctionCall - Check a direct function call for various correctness
3050/// and safety properties not strictly enforced by the C type system.
3051bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
3052 const FunctionProtoType *Proto) {
Eli Friedman726d11c2012-10-11 00:30:58 +00003053 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
3054 isa<CXXMethodDecl>(FDecl);
3055 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
3056 IsMemberOperatorCall;
Richard Smith55ce3522012-06-25 20:30:08 +00003057 VariadicCallType CallType = getVariadicCallType(FDecl, Proto,
3058 TheCall->getCallee());
Eli Friedman726d11c2012-10-11 00:30:58 +00003059 Expr** Args = TheCall->getArgs();
3060 unsigned NumArgs = TheCall->getNumArgs();
George Burgess IVce6284b2017-01-28 02:19:40 +00003061
3062 Expr *ImplicitThis = nullptr;
Eli Friedmanadf42182012-10-11 00:34:15 +00003063 if (IsMemberOperatorCall) {
Eli Friedman726d11c2012-10-11 00:30:58 +00003064 // If this is a call to a member operator, hide the first argument
3065 // from checkCall.
3066 // FIXME: Our choice of AST representation here is less than ideal.
George Burgess IVce6284b2017-01-28 02:19:40 +00003067 ImplicitThis = Args[0];
Eli Friedman726d11c2012-10-11 00:30:58 +00003068 ++Args;
3069 --NumArgs;
George Burgess IVce6284b2017-01-28 02:19:40 +00003070 } else if (IsMemberFunction)
3071 ImplicitThis =
3072 cast<CXXMemberCallExpr>(TheCall)->getImplicitObjectArgument();
3073
3074 checkCall(FDecl, Proto, ImplicitThis, llvm::makeArrayRef(Args, NumArgs),
Richard Smith55ce3522012-06-25 20:30:08 +00003075 IsMemberFunction, TheCall->getRParenLoc(),
3076 TheCall->getCallee()->getSourceRange(), CallType);
3077
3078 IdentifierInfo *FnInfo = FDecl->getIdentifier();
3079 // None of the checks below are needed for functions that don't have
3080 // simple names (e.g., C++ conversion functions).
3081 if (!FnInfo)
3082 return false;
Sebastian Redlc215cfc2009-01-19 00:08:26 +00003083
Richard Trieua7f30b12016-12-06 01:42:28 +00003084 CheckAbsoluteValueFunction(TheCall, FDecl);
3085 CheckMaxUnsignedZero(TheCall, FDecl);
Richard Trieu67c00712016-12-05 23:41:46 +00003086
Fariborz Jahanianfba4fe62014-09-11 19:13:23 +00003087 if (getLangOpts().ObjC1)
3088 DiagnoseCStringFormatDirectiveInCFAPI(*this, FDecl, Args, NumArgs);
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00003089
Anna Zaks22122702012-01-17 00:37:07 +00003090 unsigned CMId = FDecl->getMemoryFunctionKind();
3091 if (CMId == 0)
Anna Zaks201d4892012-01-13 21:52:01 +00003092 return false;
Ted Kremenek6865f772011-08-18 20:55:45 +00003093
Anna Zaks201d4892012-01-13 21:52:01 +00003094 // Handle memory setting and copying functions.
Anna Zaks22122702012-01-17 00:37:07 +00003095 if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat)
Ted Kremenek6865f772011-08-18 20:55:45 +00003096 CheckStrlcpycatArguments(TheCall, FnInfo);
Anna Zaks314cd092012-02-01 19:08:57 +00003097 else if (CMId == Builtin::BIstrncat)
3098 CheckStrncatArguments(TheCall, FnInfo);
Anna Zaks201d4892012-01-13 21:52:01 +00003099 else
Anna Zaks22122702012-01-17 00:37:07 +00003100 CheckMemaccessArguments(TheCall, CMId, FnInfo);
Chandler Carruth53caa4d2011-04-27 07:05:31 +00003101
Anders Carlssonbc4c1072009-08-16 01:56:34 +00003102 return false;
Anders Carlsson98f07902007-08-17 05:31:46 +00003103}
3104
Jean-Daniel Dupas0ae6e672012-01-17 20:03:31 +00003105bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac,
Dmitri Gribenko1debc462013-05-05 19:42:09 +00003106 ArrayRef<const Expr *> Args) {
Richard Smith55ce3522012-06-25 20:30:08 +00003107 VariadicCallType CallType =
3108 Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply;
Jean-Daniel Dupas0ae6e672012-01-17 20:03:31 +00003109
George Burgess IVce6284b2017-01-28 02:19:40 +00003110 checkCall(Method, nullptr, /*ThisArg=*/nullptr, Args,
3111 /*IsMemberFunction=*/false, lbrac, Method->getSourceRange(),
Douglas Gregorb4866e82015-06-19 18:13:19 +00003112 CallType);
Jean-Daniel Dupas0ae6e672012-01-17 20:03:31 +00003113
3114 return false;
3115}
3116
Richard Trieu664c4c62013-06-20 21:03:13 +00003117bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
3118 const FunctionProtoType *Proto) {
Aaron Ballmanb673c652015-04-23 16:14:19 +00003119 QualType Ty;
3120 if (const auto *V = dyn_cast<VarDecl>(NDecl))
Douglas Gregorb4866e82015-06-19 18:13:19 +00003121 Ty = V->getType().getNonReferenceType();
Aaron Ballmanb673c652015-04-23 16:14:19 +00003122 else if (const auto *F = dyn_cast<FieldDecl>(NDecl))
Douglas Gregorb4866e82015-06-19 18:13:19 +00003123 Ty = F->getType().getNonReferenceType();
Aaron Ballmanb673c652015-04-23 16:14:19 +00003124 else
Anders Carlssonbc4c1072009-08-16 01:56:34 +00003125 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003126
Douglas Gregorb4866e82015-06-19 18:13:19 +00003127 if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType() &&
3128 !Ty->isFunctionProtoType())
Anders Carlssonbc4c1072009-08-16 01:56:34 +00003129 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003130
Richard Trieu664c4c62013-06-20 21:03:13 +00003131 VariadicCallType CallType;
Richard Trieu72ae1732013-06-20 23:21:54 +00003132 if (!Proto || !Proto->isVariadic()) {
Richard Trieu664c4c62013-06-20 21:03:13 +00003133 CallType = VariadicDoesNotApply;
3134 } else if (Ty->isBlockPointerType()) {
3135 CallType = VariadicBlock;
3136 } else { // Ty->isFunctionPointerType()
3137 CallType = VariadicFunction;
3138 }
Anders Carlssonbc4c1072009-08-16 01:56:34 +00003139
George Burgess IVce6284b2017-01-28 02:19:40 +00003140 checkCall(NDecl, Proto, /*ThisArg=*/nullptr,
Douglas Gregorb4866e82015-06-19 18:13:19 +00003141 llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
3142 /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
Richard Smith55ce3522012-06-25 20:30:08 +00003143 TheCall->getCallee()->getSourceRange(), CallType);
Alp Toker9cacbab2014-01-20 20:26:09 +00003144
Anders Carlssonbc4c1072009-08-16 01:56:34 +00003145 return false;
Fariborz Jahanianc1585be2009-05-18 21:05:18 +00003146}
3147
Richard Trieu41bc0992013-06-22 00:20:41 +00003148/// Checks function calls when a FunctionDecl or a NamedDecl is not available,
3149/// such as function pointers returned from functions.
3150bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) {
Craig Topperc3ec1492014-05-26 06:22:03 +00003151 VariadicCallType CallType = getVariadicCallType(/*FDecl=*/nullptr, Proto,
Richard Trieu41bc0992013-06-22 00:20:41 +00003152 TheCall->getCallee());
George Burgess IVce6284b2017-01-28 02:19:40 +00003153 checkCall(/*FDecl=*/nullptr, Proto, /*ThisArg=*/nullptr,
Craig Topper8c2a2a02014-08-30 16:55:39 +00003154 llvm::makeArrayRef(TheCall->getArgs(), TheCall->getNumArgs()),
Douglas Gregorb4866e82015-06-19 18:13:19 +00003155 /*IsMemberFunction=*/false, TheCall->getRParenLoc(),
Richard Trieu41bc0992013-06-22 00:20:41 +00003156 TheCall->getCallee()->getSourceRange(), CallType);
3157
3158 return false;
3159}
3160
Tim Northovere94a34c2014-03-11 10:49:14 +00003161static bool isValidOrderingForOp(int64_t Ordering, AtomicExpr::AtomicOp Op) {
JF Bastiendda2cb12016-04-18 18:01:49 +00003162 if (!llvm::isValidAtomicOrderingCABI(Ordering))
Tim Northovere94a34c2014-03-11 10:49:14 +00003163 return false;
3164
JF Bastiendda2cb12016-04-18 18:01:49 +00003165 auto OrderingCABI = (llvm::AtomicOrderingCABI)Ordering;
Tim Northovere94a34c2014-03-11 10:49:14 +00003166 switch (Op) {
3167 case AtomicExpr::AO__c11_atomic_init:
Yaxun Liu39195062017-08-04 18:16:31 +00003168 case AtomicExpr::AO__opencl_atomic_init:
Tim Northovere94a34c2014-03-11 10:49:14 +00003169 llvm_unreachable("There is no ordering argument for an init");
3170
3171 case AtomicExpr::AO__c11_atomic_load:
Yaxun Liu39195062017-08-04 18:16:31 +00003172 case AtomicExpr::AO__opencl_atomic_load:
Tim Northovere94a34c2014-03-11 10:49:14 +00003173 case AtomicExpr::AO__atomic_load_n:
3174 case AtomicExpr::AO__atomic_load:
JF Bastiendda2cb12016-04-18 18:01:49 +00003175 return OrderingCABI != llvm::AtomicOrderingCABI::release &&
3176 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
Tim Northovere94a34c2014-03-11 10:49:14 +00003177
3178 case AtomicExpr::AO__c11_atomic_store:
Yaxun Liu39195062017-08-04 18:16:31 +00003179 case AtomicExpr::AO__opencl_atomic_store:
Tim Northovere94a34c2014-03-11 10:49:14 +00003180 case AtomicExpr::AO__atomic_store:
3181 case AtomicExpr::AO__atomic_store_n:
JF Bastiendda2cb12016-04-18 18:01:49 +00003182 return OrderingCABI != llvm::AtomicOrderingCABI::consume &&
3183 OrderingCABI != llvm::AtomicOrderingCABI::acquire &&
3184 OrderingCABI != llvm::AtomicOrderingCABI::acq_rel;
Tim Northovere94a34c2014-03-11 10:49:14 +00003185
3186 default:
3187 return true;
3188 }
3189}
3190
Richard Smithfeea8832012-04-12 05:08:17 +00003191ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
3192 AtomicExpr::AtomicOp Op) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003193 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
3194 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003195
Yaxun Liu39195062017-08-04 18:16:31 +00003196 // All the non-OpenCL operations take one of the following forms.
3197 // The OpenCL operations take the __c11 forms with one extra argument for
3198 // synchronization scope.
Richard Smithfeea8832012-04-12 05:08:17 +00003199 enum {
3200 // C __c11_atomic_init(A *, C)
3201 Init,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003202
Richard Smithfeea8832012-04-12 05:08:17 +00003203 // C __c11_atomic_load(A *, int)
3204 Load,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003205
Richard Smithfeea8832012-04-12 05:08:17 +00003206 // void __atomic_load(A *, CP, int)
Eric Fiselier8d662442016-03-30 23:39:56 +00003207 LoadCopy,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003208
Eric Fiselier8d662442016-03-30 23:39:56 +00003209 // void __atomic_store(A *, CP, int)
Richard Smithfeea8832012-04-12 05:08:17 +00003210 Copy,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003211
Richard Smithfeea8832012-04-12 05:08:17 +00003212 // C __c11_atomic_add(A *, M, int)
3213 Arithmetic,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003214
Richard Smithfeea8832012-04-12 05:08:17 +00003215 // C __atomic_exchange_n(A *, CP, int)
3216 Xchg,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003217
Richard Smithfeea8832012-04-12 05:08:17 +00003218 // void __atomic_exchange(A *, C *, CP, int)
3219 GNUXchg,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003220
Richard Smithfeea8832012-04-12 05:08:17 +00003221 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
3222 C11CmpXchg,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003223
Richard Smithfeea8832012-04-12 05:08:17 +00003224 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
3225 GNUCmpXchg
3226 } Form = Init;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00003227
Yaxun Liu39195062017-08-04 18:16:31 +00003228 const unsigned NumForm = GNUCmpXchg + 1;
Eric Fiselier8d662442016-03-30 23:39:56 +00003229 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 3, 4, 5, 6 };
3230 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 1, 2, 2, 3 };
Richard Smithfeea8832012-04-12 05:08:17 +00003231 // where:
3232 // C is an appropriate type,
3233 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
3234 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
3235 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and
3236 // the int parameters are for orderings.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003237
Yaxun Liu39195062017-08-04 18:16:31 +00003238 static_assert(sizeof(NumArgs)/sizeof(NumArgs[0]) == NumForm
3239 && sizeof(NumVals)/sizeof(NumVals[0]) == NumForm,
3240 "need to update code for modified forms");
Gabor Horvath98bd0982015-03-16 09:59:54 +00003241 static_assert(AtomicExpr::AO__c11_atomic_init == 0 &&
3242 AtomicExpr::AO__c11_atomic_fetch_xor + 1 ==
3243 AtomicExpr::AO__atomic_load,
3244 "need to update code for modified C11 atomics");
Yaxun Liu39195062017-08-04 18:16:31 +00003245 bool IsOpenCL = Op >= AtomicExpr::AO__opencl_atomic_init &&
3246 Op <= AtomicExpr::AO__opencl_atomic_fetch_max;
3247 bool IsC11 = (Op >= AtomicExpr::AO__c11_atomic_init &&
3248 Op <= AtomicExpr::AO__c11_atomic_fetch_xor) ||
3249 IsOpenCL;
Richard Smithfeea8832012-04-12 05:08:17 +00003250 bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
3251 Op == AtomicExpr::AO__atomic_store_n ||
3252 Op == AtomicExpr::AO__atomic_exchange_n ||
3253 Op == AtomicExpr::AO__atomic_compare_exchange_n;
3254 bool IsAddSub = false;
Elena Demikhovskyd31327d2018-05-13 07:45:58 +00003255 bool IsMinMax = false;
Richard Smithfeea8832012-04-12 05:08:17 +00003256
3257 switch (Op) {
3258 case AtomicExpr::AO__c11_atomic_init:
Yaxun Liu39195062017-08-04 18:16:31 +00003259 case AtomicExpr::AO__opencl_atomic_init:
Richard Smithfeea8832012-04-12 05:08:17 +00003260 Form = Init;
3261 break;
3262
3263 case AtomicExpr::AO__c11_atomic_load:
Yaxun Liu39195062017-08-04 18:16:31 +00003264 case AtomicExpr::AO__opencl_atomic_load:
Richard Smithfeea8832012-04-12 05:08:17 +00003265 case AtomicExpr::AO__atomic_load_n:
3266 Form = Load;
3267 break;
3268
Richard Smithfeea8832012-04-12 05:08:17 +00003269 case AtomicExpr::AO__atomic_load:
Eric Fiselier8d662442016-03-30 23:39:56 +00003270 Form = LoadCopy;
3271 break;
3272
3273 case AtomicExpr::AO__c11_atomic_store:
Yaxun Liu39195062017-08-04 18:16:31 +00003274 case AtomicExpr::AO__opencl_atomic_store:
Richard Smithfeea8832012-04-12 05:08:17 +00003275 case AtomicExpr::AO__atomic_store:
3276 case AtomicExpr::AO__atomic_store_n:
3277 Form = Copy;
3278 break;
3279
3280 case AtomicExpr::AO__c11_atomic_fetch_add:
3281 case AtomicExpr::AO__c11_atomic_fetch_sub:
Yaxun Liu39195062017-08-04 18:16:31 +00003282 case AtomicExpr::AO__opencl_atomic_fetch_add:
3283 case AtomicExpr::AO__opencl_atomic_fetch_sub:
3284 case AtomicExpr::AO__opencl_atomic_fetch_min:
3285 case AtomicExpr::AO__opencl_atomic_fetch_max:
Richard Smithfeea8832012-04-12 05:08:17 +00003286 case AtomicExpr::AO__atomic_fetch_add:
3287 case AtomicExpr::AO__atomic_fetch_sub:
3288 case AtomicExpr::AO__atomic_add_fetch:
3289 case AtomicExpr::AO__atomic_sub_fetch:
3290 IsAddSub = true;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +00003291 LLVM_FALLTHROUGH;
Richard Smithfeea8832012-04-12 05:08:17 +00003292 case AtomicExpr::AO__c11_atomic_fetch_and:
3293 case AtomicExpr::AO__c11_atomic_fetch_or:
3294 case AtomicExpr::AO__c11_atomic_fetch_xor:
Yaxun Liu39195062017-08-04 18:16:31 +00003295 case AtomicExpr::AO__opencl_atomic_fetch_and:
3296 case AtomicExpr::AO__opencl_atomic_fetch_or:
3297 case AtomicExpr::AO__opencl_atomic_fetch_xor:
Richard Smithfeea8832012-04-12 05:08:17 +00003298 case AtomicExpr::AO__atomic_fetch_and:
3299 case AtomicExpr::AO__atomic_fetch_or:
3300 case AtomicExpr::AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00003301 case AtomicExpr::AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00003302 case AtomicExpr::AO__atomic_and_fetch:
3303 case AtomicExpr::AO__atomic_or_fetch:
3304 case AtomicExpr::AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00003305 case AtomicExpr::AO__atomic_nand_fetch:
Richard Smithfeea8832012-04-12 05:08:17 +00003306 Form = Arithmetic;
3307 break;
3308
Elena Demikhovskyd31327d2018-05-13 07:45:58 +00003309 case AtomicExpr::AO__atomic_fetch_min:
3310 case AtomicExpr::AO__atomic_fetch_max:
3311 IsMinMax = true;
3312 Form = Arithmetic;
3313 break;
3314
Richard Smithfeea8832012-04-12 05:08:17 +00003315 case AtomicExpr::AO__c11_atomic_exchange:
Yaxun Liu39195062017-08-04 18:16:31 +00003316 case AtomicExpr::AO__opencl_atomic_exchange:
Richard Smithfeea8832012-04-12 05:08:17 +00003317 case AtomicExpr::AO__atomic_exchange_n:
3318 Form = Xchg;
3319 break;
3320
3321 case AtomicExpr::AO__atomic_exchange:
3322 Form = GNUXchg;
3323 break;
3324
3325 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
3326 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
Yaxun Liu39195062017-08-04 18:16:31 +00003327 case AtomicExpr::AO__opencl_atomic_compare_exchange_strong:
3328 case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
Richard Smithfeea8832012-04-12 05:08:17 +00003329 Form = C11CmpXchg;
3330 break;
3331
3332 case AtomicExpr::AO__atomic_compare_exchange:
3333 case AtomicExpr::AO__atomic_compare_exchange_n:
3334 Form = GNUCmpXchg;
3335 break;
3336 }
3337
Yaxun Liu39195062017-08-04 18:16:31 +00003338 unsigned AdjustedNumArgs = NumArgs[Form];
3339 if (IsOpenCL && Op != AtomicExpr::AO__opencl_atomic_init)
3340 ++AdjustedNumArgs;
Richard Smithfeea8832012-04-12 05:08:17 +00003341 // Check we have the right number of arguments.
Yaxun Liu39195062017-08-04 18:16:31 +00003342 if (TheCall->getNumArgs() < AdjustedNumArgs) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003343 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Yaxun Liu39195062017-08-04 18:16:31 +00003344 << 0 << AdjustedNumArgs << TheCall->getNumArgs()
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003345 << TheCall->getCallee()->getSourceRange();
3346 return ExprError();
Yaxun Liu39195062017-08-04 18:16:31 +00003347 } else if (TheCall->getNumArgs() > AdjustedNumArgs) {
3348 Diag(TheCall->getArg(AdjustedNumArgs)->getLocStart(),
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003349 diag::err_typecheck_call_too_many_args)
Yaxun Liu39195062017-08-04 18:16:31 +00003350 << 0 << AdjustedNumArgs << TheCall->getNumArgs()
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003351 << TheCall->getCallee()->getSourceRange();
3352 return ExprError();
3353 }
3354
Richard Smithfeea8832012-04-12 05:08:17 +00003355 // Inspect the first argument of the atomic operation.
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003356 Expr *Ptr = TheCall->getArg(0);
George Burgess IV92b43a42016-07-21 03:28:13 +00003357 ExprResult ConvertedPtr = DefaultFunctionArrayLvalueConversion(Ptr);
3358 if (ConvertedPtr.isInvalid())
3359 return ExprError();
3360
3361 Ptr = ConvertedPtr.get();
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003362 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
3363 if (!pointerType) {
Richard Smithfeea8832012-04-12 05:08:17 +00003364 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003365 << Ptr->getType() << Ptr->getSourceRange();
3366 return ExprError();
3367 }
3368
Richard Smithfeea8832012-04-12 05:08:17 +00003369 // For a __c11 builtin, this should be a pointer to an _Atomic type.
3370 QualType AtomTy = pointerType->getPointeeType(); // 'A'
3371 QualType ValType = AtomTy; // 'C'
3372 if (IsC11) {
3373 if (!AtomTy->isAtomicType()) {
3374 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic)
3375 << Ptr->getType() << Ptr->getSourceRange();
3376 return ExprError();
3377 }
Yaxun Liu39195062017-08-04 18:16:31 +00003378 if (AtomTy.isConstQualified() ||
3379 AtomTy.getAddressSpace() == LangAS::opencl_constant) {
Richard Smithe00921a2012-09-15 06:09:58 +00003380 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic)
Yaxun Liu39195062017-08-04 18:16:31 +00003381 << (AtomTy.isConstQualified() ? 0 : 1) << Ptr->getType()
3382 << Ptr->getSourceRange();
Richard Smithe00921a2012-09-15 06:09:58 +00003383 return ExprError();
3384 }
Richard Smithfeea8832012-04-12 05:08:17 +00003385 ValType = AtomTy->getAs<AtomicType>()->getValueType();
Eric Fiselier8d662442016-03-30 23:39:56 +00003386 } else if (Form != Load && Form != LoadCopy) {
Eric Fiseliera3a7c562015-10-04 00:11:02 +00003387 if (ValType.isConstQualified()) {
3388 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_pointer)
3389 << Ptr->getType() << Ptr->getSourceRange();
3390 return ExprError();
3391 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003392 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003393
Richard Smithfeea8832012-04-12 05:08:17 +00003394 // For an arithmetic operation, the implied arithmetic must be well-formed.
3395 if (Form == Arithmetic) {
3396 // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
Elena Demikhovskyd31327d2018-05-13 07:45:58 +00003397 if (IsAddSub && !ValType->isIntegerType()
3398 && !ValType->isPointerType()) {
Richard Smithfeea8832012-04-12 05:08:17 +00003399 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
3400 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
3401 return ExprError();
3402 }
Elena Demikhovskyd31327d2018-05-13 07:45:58 +00003403 if (IsMinMax) {
3404 const BuiltinType *BT = ValType->getAs<BuiltinType>();
3405 if (!BT || (BT->getKind() != BuiltinType::Int &&
3406 BT->getKind() != BuiltinType::UInt)) {
3407 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_int32_or_ptr);
3408 return ExprError();
3409 }
3410 }
3411 if (!IsAddSub && !IsMinMax && !ValType->isIntegerType()) {
Richard Smithfeea8832012-04-12 05:08:17 +00003412 Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int)
3413 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
3414 return ExprError();
3415 }
David Majnemere85cff82015-01-28 05:48:06 +00003416 if (IsC11 && ValType->isPointerType() &&
3417 RequireCompleteType(Ptr->getLocStart(), ValType->getPointeeType(),
3418 diag::err_incomplete_type)) {
3419 return ExprError();
3420 }
Richard Smithfeea8832012-04-12 05:08:17 +00003421 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
3422 // For __atomic_*_n operations, the value type must be a scalar integral or
3423 // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003424 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
Richard Smithfeea8832012-04-12 05:08:17 +00003425 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
3426 return ExprError();
3427 }
3428
Eli Friedmanaa769812013-09-11 03:49:34 +00003429 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context) &&
3430 !AtomTy->isScalarType()) {
Richard Smithfeea8832012-04-12 05:08:17 +00003431 // For GNU atomics, require a trivially-copyable type. This is not part of
3432 // the GNU atomics specification, but we enforce it for sanity.
3433 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy)
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003434 << Ptr->getType() << Ptr->getSourceRange();
3435 return ExprError();
3436 }
3437
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003438 switch (ValType.getObjCLifetime()) {
3439 case Qualifiers::OCL_None:
3440 case Qualifiers::OCL_ExplicitNone:
3441 // okay
3442 break;
3443
3444 case Qualifiers::OCL_Weak:
3445 case Qualifiers::OCL_Strong:
3446 case Qualifiers::OCL_Autoreleasing:
Richard Smithfeea8832012-04-12 05:08:17 +00003447 // FIXME: Can this happen? By this point, ValType should be known
3448 // to be trivially copyable.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003449 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
3450 << ValType << Ptr->getSourceRange();
3451 return ExprError();
3452 }
3453
David Majnemerc6eb6502015-06-03 00:26:35 +00003454 // atomic_fetch_or takes a pointer to a volatile 'A'. We shouldn't let the
3455 // volatile-ness of the pointee-type inject itself into the result or the
Eric Fiselier8d662442016-03-30 23:39:56 +00003456 // other operands. Similarly atomic_load can take a pointer to a const 'A'.
David Majnemerc6eb6502015-06-03 00:26:35 +00003457 ValType.removeLocalVolatile();
Eric Fiselier8d662442016-03-30 23:39:56 +00003458 ValType.removeLocalConst();
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003459 QualType ResultType = ValType;
Yaxun Liu39195062017-08-04 18:16:31 +00003460 if (Form == Copy || Form == LoadCopy || Form == GNUXchg ||
3461 Form == Init)
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003462 ResultType = Context.VoidTy;
Richard Smithfeea8832012-04-12 05:08:17 +00003463 else if (Form == C11CmpXchg || Form == GNUCmpXchg)
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003464 ResultType = Context.BoolTy;
3465
Richard Smithfeea8832012-04-12 05:08:17 +00003466 // The type of a parameter passed 'by value'. In the GNU atomics, such
3467 // arguments are actually passed as pointers.
3468 QualType ByValType = ValType; // 'CP'
3469 if (!IsC11 && !IsN)
3470 ByValType = Ptr->getType();
3471
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003472 // The first argument --- the pointer --- has a fixed type; we
3473 // deduce the types of the rest of the arguments accordingly. Walk
3474 // the remaining arguments, converting them to the deduced value type.
Yaxun Liu39195062017-08-04 18:16:31 +00003475 for (unsigned i = 1; i != TheCall->getNumArgs(); ++i) {
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003476 QualType Ty;
Richard Smithfeea8832012-04-12 05:08:17 +00003477 if (i < NumVals[Form] + 1) {
3478 switch (i) {
3479 case 1:
3480 // The second argument is the non-atomic operand. For arithmetic, this
3481 // is always passed by value, and for a compare_exchange it is always
3482 // passed by address. For the rest, GNU uses by-address and C11 uses
3483 // by-value.
3484 assert(Form != Load);
3485 if (Form == Init || (Form == Arithmetic && ValType->isIntegerType()))
3486 Ty = ValType;
3487 else if (Form == Copy || Form == Xchg)
3488 Ty = ByValType;
3489 else if (Form == Arithmetic)
3490 Ty = Context.getPointerDiffType();
Anastasia Stulova76fd1052015-12-22 15:14:54 +00003491 else {
3492 Expr *ValArg = TheCall->getArg(i);
Alex Lorenz67522152016-11-23 16:57:03 +00003493 // Treat this argument as _Nonnull as we want to show a warning if
3494 // NULL is passed into it.
3495 CheckNonNullArgument(*this, ValArg, DRE->getLocStart());
Alexander Richardson6d989432017-10-15 18:48:14 +00003496 LangAS AS = LangAS::Default;
Anastasia Stulova76fd1052015-12-22 15:14:54 +00003497 // Keep address space of non-atomic pointer type.
3498 if (const PointerType *PtrTy =
3499 ValArg->getType()->getAs<PointerType>()) {
3500 AS = PtrTy->getPointeeType().getAddressSpace();
3501 }
3502 Ty = Context.getPointerType(
3503 Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS));
3504 }
Richard Smithfeea8832012-04-12 05:08:17 +00003505 break;
3506 case 2:
3507 // The third argument to compare_exchange / GNU exchange is a
3508 // (pointer to a) desired value.
3509 Ty = ByValType;
3510 break;
3511 case 3:
3512 // The fourth argument to GNU compare_exchange is a 'weak' flag.
3513 Ty = Context.BoolTy;
3514 break;
3515 }
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003516 } else {
Yaxun Liu39195062017-08-04 18:16:31 +00003517 // The order(s) and scope are always converted to int.
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003518 Ty = Context.IntTy;
3519 }
Richard Smithfeea8832012-04-12 05:08:17 +00003520
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003521 InitializedEntity Entity =
3522 InitializedEntity::InitializeParameter(Context, Ty, false);
Richard Smithfeea8832012-04-12 05:08:17 +00003523 ExprResult Arg = TheCall->getArg(i);
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003524 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
3525 if (Arg.isInvalid())
3526 return true;
3527 TheCall->setArg(i, Arg.get());
3528 }
3529
Richard Smithfeea8832012-04-12 05:08:17 +00003530 // Permute the arguments into a 'consistent' order.
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003531 SmallVector<Expr*, 5> SubExprs;
3532 SubExprs.push_back(Ptr);
Richard Smithfeea8832012-04-12 05:08:17 +00003533 switch (Form) {
3534 case Init:
3535 // Note, AtomicExpr::getVal1() has a special case for this atomic.
David Chisnallfa35df62012-01-16 17:27:18 +00003536 SubExprs.push_back(TheCall->getArg(1)); // Val1
Richard Smithfeea8832012-04-12 05:08:17 +00003537 break;
3538 case Load:
3539 SubExprs.push_back(TheCall->getArg(1)); // Order
3540 break;
Eric Fiselier8d662442016-03-30 23:39:56 +00003541 case LoadCopy:
Richard Smithfeea8832012-04-12 05:08:17 +00003542 case Copy:
3543 case Arithmetic:
3544 case Xchg:
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003545 SubExprs.push_back(TheCall->getArg(2)); // Order
3546 SubExprs.push_back(TheCall->getArg(1)); // Val1
Richard Smithfeea8832012-04-12 05:08:17 +00003547 break;
3548 case GNUXchg:
3549 // Note, AtomicExpr::getVal2() has a special case for this atomic.
3550 SubExprs.push_back(TheCall->getArg(3)); // Order
3551 SubExprs.push_back(TheCall->getArg(1)); // Val1
3552 SubExprs.push_back(TheCall->getArg(2)); // Val2
3553 break;
3554 case C11CmpXchg:
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003555 SubExprs.push_back(TheCall->getArg(3)); // Order
3556 SubExprs.push_back(TheCall->getArg(1)); // Val1
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003557 SubExprs.push_back(TheCall->getArg(4)); // OrderFail
David Chisnall891ec282012-03-29 17:58:59 +00003558 SubExprs.push_back(TheCall->getArg(2)); // Val2
Richard Smithfeea8832012-04-12 05:08:17 +00003559 break;
3560 case GNUCmpXchg:
3561 SubExprs.push_back(TheCall->getArg(4)); // Order
3562 SubExprs.push_back(TheCall->getArg(1)); // Val1
3563 SubExprs.push_back(TheCall->getArg(5)); // OrderFail
3564 SubExprs.push_back(TheCall->getArg(2)); // Val2
3565 SubExprs.push_back(TheCall->getArg(3)); // Weak
3566 break;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003567 }
Tim Northovere94a34c2014-03-11 10:49:14 +00003568
3569 if (SubExprs.size() >= 2 && Form != Init) {
3570 llvm::APSInt Result(32);
3571 if (SubExprs[1]->isIntegerConstantExpr(Result, Context) &&
3572 !isValidOrderingForOp(Result.getSExtValue(), Op))
Tim Northoverc83472e2014-03-11 11:35:10 +00003573 Diag(SubExprs[1]->getLocStart(),
3574 diag::warn_atomic_op_has_invalid_memory_order)
3575 << SubExprs[1]->getSourceRange();
Tim Northovere94a34c2014-03-11 10:49:14 +00003576 }
3577
Yaxun Liu30d652a2017-08-15 16:02:49 +00003578 if (auto ScopeModel = AtomicExpr::getScopeModel(Op)) {
3579 auto *Scope = TheCall->getArg(TheCall->getNumArgs() - 1);
3580 llvm::APSInt Result(32);
3581 if (Scope->isIntegerConstantExpr(Result, Context) &&
3582 !ScopeModel->isValid(Result.getZExtValue())) {
3583 Diag(Scope->getLocStart(), diag::err_atomic_op_has_invalid_synch_scope)
3584 << Scope->getSourceRange();
3585 }
3586 SubExprs.push_back(Scope);
3587 }
3588
Fariborz Jahanian615de762013-05-28 17:37:39 +00003589 AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(),
3590 SubExprs, ResultType, Op,
3591 TheCall->getRParenLoc());
3592
3593 if ((Op == AtomicExpr::AO__c11_atomic_load ||
Yaxun Liu39195062017-08-04 18:16:31 +00003594 Op == AtomicExpr::AO__c11_atomic_store ||
3595 Op == AtomicExpr::AO__opencl_atomic_load ||
3596 Op == AtomicExpr::AO__opencl_atomic_store ) &&
Fariborz Jahanian615de762013-05-28 17:37:39 +00003597 Context.AtomicUsesUnsupportedLibcall(AE))
Yaxun Liu39195062017-08-04 18:16:31 +00003598 Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib)
3599 << ((Op == AtomicExpr::AO__c11_atomic_load ||
3600 Op == AtomicExpr::AO__opencl_atomic_load)
3601 ? 0 : 1);
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003602
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003603 return AE;
Eli Friedmandf14b3a2011-10-11 02:20:01 +00003604}
3605
John McCall29ad95b2011-08-27 01:09:30 +00003606/// checkBuiltinArgument - Given a call to a builtin function, perform
3607/// normal type-checking on the given argument, updating the call in
3608/// place. This is useful when a builtin function requires custom
3609/// type-checking for some of its arguments but not necessarily all of
3610/// them.
3611///
3612/// Returns true on error.
3613static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
3614 FunctionDecl *Fn = E->getDirectCallee();
3615 assert(Fn && "builtin call without direct callee!");
3616
3617 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
3618 InitializedEntity Entity =
3619 InitializedEntity::InitializeParameter(S.Context, Param);
3620
3621 ExprResult Arg = E->getArg(0);
3622 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
3623 if (Arg.isInvalid())
3624 return true;
3625
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003626 E->setArg(ArgIndex, Arg.get());
John McCall29ad95b2011-08-27 01:09:30 +00003627 return false;
3628}
3629
Chris Lattnerdc046542009-05-08 06:58:22 +00003630/// SemaBuiltinAtomicOverloaded - We have a call to a function like
3631/// __sync_fetch_and_add, which is an overloaded function based on the pointer
3632/// type of its first argument. The main ActOnCallExpr routines have already
3633/// promoted the types of arguments because all of these calls are prototyped as
3634/// void(...).
3635///
3636/// This function goes through and does final semantic checking for these
3637/// builtins,
John McCalldadc5752010-08-24 06:29:42 +00003638ExprResult
3639Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003640 CallExpr *TheCall = (CallExpr *)TheCallResult.get();
Chris Lattnerdc046542009-05-08 06:58:22 +00003641 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
3642 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
3643
3644 // Ensure that we have at least one argument to do type inference from.
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003645 if (TheCall->getNumArgs() < 1) {
3646 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
3647 << 0 << 1 << TheCall->getNumArgs()
3648 << TheCall->getCallee()->getSourceRange();
3649 return ExprError();
3650 }
Mike Stump11289f42009-09-09 15:08:12 +00003651
Chris Lattnerdc046542009-05-08 06:58:22 +00003652 // Inspect the first argument of the atomic builtin. This should always be
3653 // a pointer type, whose element is an integral scalar or pointer type.
3654 // Because it is a pointer type, we don't have to worry about any implicit
3655 // casts here.
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003656 // FIXME: We don't allow floating point scalars as input.
Chris Lattnerdc046542009-05-08 06:58:22 +00003657 Expr *FirstArg = TheCall->getArg(0);
Eli Friedman844f9452012-01-23 02:35:22 +00003658 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
3659 if (FirstArgResult.isInvalid())
3660 return ExprError();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003661 FirstArg = FirstArgResult.get();
Eli Friedman844f9452012-01-23 02:35:22 +00003662 TheCall->setArg(0, FirstArg);
3663
John McCall31168b02011-06-15 23:02:42 +00003664 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
3665 if (!pointerType) {
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003666 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
3667 << FirstArg->getType() << FirstArg->getSourceRange();
3668 return ExprError();
3669 }
Mike Stump11289f42009-09-09 15:08:12 +00003670
John McCall31168b02011-06-15 23:02:42 +00003671 QualType ValType = pointerType->getPointeeType();
Chris Lattnerbb3bcd82010-09-17 21:12:38 +00003672 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003673 !ValType->isBlockPointerType()) {
3674 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
3675 << FirstArg->getType() << FirstArg->getSourceRange();
3676 return ExprError();
3677 }
Chris Lattnerdc046542009-05-08 06:58:22 +00003678
Aaron Ballmana383c942018-05-05 17:38:42 +00003679 if (ValType.isConstQualified()) {
3680 Diag(DRE->getLocStart(), diag::err_atomic_builtin_cannot_be_const)
3681 << FirstArg->getType() << FirstArg->getSourceRange();
3682 return ExprError();
3683 }
3684
John McCall31168b02011-06-15 23:02:42 +00003685 switch (ValType.getObjCLifetime()) {
3686 case Qualifiers::OCL_None:
3687 case Qualifiers::OCL_ExplicitNone:
3688 // okay
3689 break;
3690
3691 case Qualifiers::OCL_Weak:
3692 case Qualifiers::OCL_Strong:
3693 case Qualifiers::OCL_Autoreleasing:
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00003694 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
John McCall31168b02011-06-15 23:02:42 +00003695 << ValType << FirstArg->getSourceRange();
3696 return ExprError();
3697 }
3698
John McCallb50451a2011-10-05 07:41:44 +00003699 // Strip any qualifiers off ValType.
3700 ValType = ValType.getUnqualifiedType();
3701
Chandler Carruth3973af72010-07-18 20:54:12 +00003702 // The majority of builtins return a value, but a few have special return
3703 // types, so allow them to override appropriately below.
3704 QualType ResultType = ValType;
3705
Chris Lattnerdc046542009-05-08 06:58:22 +00003706 // We need to figure out which concrete builtin this maps onto. For example,
3707 // __sync_fetch_and_add with a 2 byte object turns into
3708 // __sync_fetch_and_add_2.
3709#define BUILTIN_ROW(x) \
3710 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
3711 Builtin::BI##x##_8, Builtin::BI##x##_16 }
Mike Stump11289f42009-09-09 15:08:12 +00003712
Chris Lattnerdc046542009-05-08 06:58:22 +00003713 static const unsigned BuiltinIndices[][5] = {
3714 BUILTIN_ROW(__sync_fetch_and_add),
3715 BUILTIN_ROW(__sync_fetch_and_sub),
3716 BUILTIN_ROW(__sync_fetch_and_or),
3717 BUILTIN_ROW(__sync_fetch_and_and),
3718 BUILTIN_ROW(__sync_fetch_and_xor),
Hal Finkeld2208b52014-10-02 20:53:50 +00003719 BUILTIN_ROW(__sync_fetch_and_nand),
Mike Stump11289f42009-09-09 15:08:12 +00003720
Chris Lattnerdc046542009-05-08 06:58:22 +00003721 BUILTIN_ROW(__sync_add_and_fetch),
3722 BUILTIN_ROW(__sync_sub_and_fetch),
3723 BUILTIN_ROW(__sync_and_and_fetch),
3724 BUILTIN_ROW(__sync_or_and_fetch),
3725 BUILTIN_ROW(__sync_xor_and_fetch),
Hal Finkeld2208b52014-10-02 20:53:50 +00003726 BUILTIN_ROW(__sync_nand_and_fetch),
Mike Stump11289f42009-09-09 15:08:12 +00003727
Chris Lattnerdc046542009-05-08 06:58:22 +00003728 BUILTIN_ROW(__sync_val_compare_and_swap),
3729 BUILTIN_ROW(__sync_bool_compare_and_swap),
3730 BUILTIN_ROW(__sync_lock_test_and_set),
Chris Lattner9cb59fa2011-04-09 03:57:26 +00003731 BUILTIN_ROW(__sync_lock_release),
3732 BUILTIN_ROW(__sync_swap)
Chris Lattnerdc046542009-05-08 06:58:22 +00003733 };
Mike Stump11289f42009-09-09 15:08:12 +00003734#undef BUILTIN_ROW
3735
Chris Lattnerdc046542009-05-08 06:58:22 +00003736 // Determine the index of the size.
3737 unsigned SizeIndex;
Ken Dyck40775002010-01-11 17:06:35 +00003738 switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
Chris Lattnerdc046542009-05-08 06:58:22 +00003739 case 1: SizeIndex = 0; break;
3740 case 2: SizeIndex = 1; break;
3741 case 4: SizeIndex = 2; break;
3742 case 8: SizeIndex = 3; break;
3743 case 16: SizeIndex = 4; break;
3744 default:
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003745 Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
3746 << FirstArg->getType() << FirstArg->getSourceRange();
3747 return ExprError();
Chris Lattnerdc046542009-05-08 06:58:22 +00003748 }
Mike Stump11289f42009-09-09 15:08:12 +00003749
Chris Lattnerdc046542009-05-08 06:58:22 +00003750 // Each of these builtins has one pointer argument, followed by some number of
3751 // values (0, 1 or 2) followed by a potentially empty varags list of stuff
3752 // that we ignore. Find out which row of BuiltinIndices to read from as well
3753 // as the number of fixed args.
Douglas Gregor15fc9562009-09-12 00:22:50 +00003754 unsigned BuiltinID = FDecl->getBuiltinID();
Chris Lattnerdc046542009-05-08 06:58:22 +00003755 unsigned BuiltinIndex, NumFixed = 1;
Hal Finkeld2208b52014-10-02 20:53:50 +00003756 bool WarnAboutSemanticsChange = false;
Chris Lattnerdc046542009-05-08 06:58:22 +00003757 switch (BuiltinID) {
David Blaikie83d382b2011-09-23 05:06:16 +00003758 default: llvm_unreachable("Unknown overloaded atomic builtin!");
Douglas Gregor73722482011-11-28 16:30:08 +00003759 case Builtin::BI__sync_fetch_and_add:
3760 case Builtin::BI__sync_fetch_and_add_1:
3761 case Builtin::BI__sync_fetch_and_add_2:
3762 case Builtin::BI__sync_fetch_and_add_4:
3763 case Builtin::BI__sync_fetch_and_add_8:
3764 case Builtin::BI__sync_fetch_and_add_16:
3765 BuiltinIndex = 0;
3766 break;
3767
3768 case Builtin::BI__sync_fetch_and_sub:
3769 case Builtin::BI__sync_fetch_and_sub_1:
3770 case Builtin::BI__sync_fetch_and_sub_2:
3771 case Builtin::BI__sync_fetch_and_sub_4:
3772 case Builtin::BI__sync_fetch_and_sub_8:
3773 case Builtin::BI__sync_fetch_and_sub_16:
3774 BuiltinIndex = 1;
3775 break;
3776
3777 case Builtin::BI__sync_fetch_and_or:
3778 case Builtin::BI__sync_fetch_and_or_1:
3779 case Builtin::BI__sync_fetch_and_or_2:
3780 case Builtin::BI__sync_fetch_and_or_4:
3781 case Builtin::BI__sync_fetch_and_or_8:
3782 case Builtin::BI__sync_fetch_and_or_16:
3783 BuiltinIndex = 2;
3784 break;
3785
3786 case Builtin::BI__sync_fetch_and_and:
3787 case Builtin::BI__sync_fetch_and_and_1:
3788 case Builtin::BI__sync_fetch_and_and_2:
3789 case Builtin::BI__sync_fetch_and_and_4:
3790 case Builtin::BI__sync_fetch_and_and_8:
3791 case Builtin::BI__sync_fetch_and_and_16:
3792 BuiltinIndex = 3;
3793 break;
Mike Stump11289f42009-09-09 15:08:12 +00003794
Douglas Gregor73722482011-11-28 16:30:08 +00003795 case Builtin::BI__sync_fetch_and_xor:
3796 case Builtin::BI__sync_fetch_and_xor_1:
3797 case Builtin::BI__sync_fetch_and_xor_2:
3798 case Builtin::BI__sync_fetch_and_xor_4:
3799 case Builtin::BI__sync_fetch_and_xor_8:
3800 case Builtin::BI__sync_fetch_and_xor_16:
3801 BuiltinIndex = 4;
3802 break;
3803
Hal Finkeld2208b52014-10-02 20:53:50 +00003804 case Builtin::BI__sync_fetch_and_nand:
3805 case Builtin::BI__sync_fetch_and_nand_1:
3806 case Builtin::BI__sync_fetch_and_nand_2:
3807 case Builtin::BI__sync_fetch_and_nand_4:
3808 case Builtin::BI__sync_fetch_and_nand_8:
3809 case Builtin::BI__sync_fetch_and_nand_16:
3810 BuiltinIndex = 5;
3811 WarnAboutSemanticsChange = true;
3812 break;
3813
Douglas Gregor73722482011-11-28 16:30:08 +00003814 case Builtin::BI__sync_add_and_fetch:
3815 case Builtin::BI__sync_add_and_fetch_1:
3816 case Builtin::BI__sync_add_and_fetch_2:
3817 case Builtin::BI__sync_add_and_fetch_4:
3818 case Builtin::BI__sync_add_and_fetch_8:
3819 case Builtin::BI__sync_add_and_fetch_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003820 BuiltinIndex = 6;
Douglas Gregor73722482011-11-28 16:30:08 +00003821 break;
3822
3823 case Builtin::BI__sync_sub_and_fetch:
3824 case Builtin::BI__sync_sub_and_fetch_1:
3825 case Builtin::BI__sync_sub_and_fetch_2:
3826 case Builtin::BI__sync_sub_and_fetch_4:
3827 case Builtin::BI__sync_sub_and_fetch_8:
3828 case Builtin::BI__sync_sub_and_fetch_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003829 BuiltinIndex = 7;
Douglas Gregor73722482011-11-28 16:30:08 +00003830 break;
3831
3832 case Builtin::BI__sync_and_and_fetch:
3833 case Builtin::BI__sync_and_and_fetch_1:
3834 case Builtin::BI__sync_and_and_fetch_2:
3835 case Builtin::BI__sync_and_and_fetch_4:
3836 case Builtin::BI__sync_and_and_fetch_8:
3837 case Builtin::BI__sync_and_and_fetch_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003838 BuiltinIndex = 8;
Douglas Gregor73722482011-11-28 16:30:08 +00003839 break;
3840
3841 case Builtin::BI__sync_or_and_fetch:
3842 case Builtin::BI__sync_or_and_fetch_1:
3843 case Builtin::BI__sync_or_and_fetch_2:
3844 case Builtin::BI__sync_or_and_fetch_4:
3845 case Builtin::BI__sync_or_and_fetch_8:
3846 case Builtin::BI__sync_or_and_fetch_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003847 BuiltinIndex = 9;
Douglas Gregor73722482011-11-28 16:30:08 +00003848 break;
3849
3850 case Builtin::BI__sync_xor_and_fetch:
3851 case Builtin::BI__sync_xor_and_fetch_1:
3852 case Builtin::BI__sync_xor_and_fetch_2:
3853 case Builtin::BI__sync_xor_and_fetch_4:
3854 case Builtin::BI__sync_xor_and_fetch_8:
3855 case Builtin::BI__sync_xor_and_fetch_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003856 BuiltinIndex = 10;
3857 break;
3858
3859 case Builtin::BI__sync_nand_and_fetch:
3860 case Builtin::BI__sync_nand_and_fetch_1:
3861 case Builtin::BI__sync_nand_and_fetch_2:
3862 case Builtin::BI__sync_nand_and_fetch_4:
3863 case Builtin::BI__sync_nand_and_fetch_8:
3864 case Builtin::BI__sync_nand_and_fetch_16:
3865 BuiltinIndex = 11;
3866 WarnAboutSemanticsChange = true;
Douglas Gregor73722482011-11-28 16:30:08 +00003867 break;
Mike Stump11289f42009-09-09 15:08:12 +00003868
Chris Lattnerdc046542009-05-08 06:58:22 +00003869 case Builtin::BI__sync_val_compare_and_swap:
Douglas Gregor73722482011-11-28 16:30:08 +00003870 case Builtin::BI__sync_val_compare_and_swap_1:
3871 case Builtin::BI__sync_val_compare_and_swap_2:
3872 case Builtin::BI__sync_val_compare_and_swap_4:
3873 case Builtin::BI__sync_val_compare_and_swap_8:
3874 case Builtin::BI__sync_val_compare_and_swap_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003875 BuiltinIndex = 12;
Chris Lattnerdc046542009-05-08 06:58:22 +00003876 NumFixed = 2;
3877 break;
Douglas Gregor73722482011-11-28 16:30:08 +00003878
Chris Lattnerdc046542009-05-08 06:58:22 +00003879 case Builtin::BI__sync_bool_compare_and_swap:
Douglas Gregor73722482011-11-28 16:30:08 +00003880 case Builtin::BI__sync_bool_compare_and_swap_1:
3881 case Builtin::BI__sync_bool_compare_and_swap_2:
3882 case Builtin::BI__sync_bool_compare_and_swap_4:
3883 case Builtin::BI__sync_bool_compare_and_swap_8:
3884 case Builtin::BI__sync_bool_compare_and_swap_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003885 BuiltinIndex = 13;
Chris Lattnerdc046542009-05-08 06:58:22 +00003886 NumFixed = 2;
Chandler Carruth3973af72010-07-18 20:54:12 +00003887 ResultType = Context.BoolTy;
Chris Lattnerdc046542009-05-08 06:58:22 +00003888 break;
Douglas Gregor73722482011-11-28 16:30:08 +00003889
3890 case Builtin::BI__sync_lock_test_and_set:
3891 case Builtin::BI__sync_lock_test_and_set_1:
3892 case Builtin::BI__sync_lock_test_and_set_2:
3893 case Builtin::BI__sync_lock_test_and_set_4:
3894 case Builtin::BI__sync_lock_test_and_set_8:
3895 case Builtin::BI__sync_lock_test_and_set_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003896 BuiltinIndex = 14;
Douglas Gregor73722482011-11-28 16:30:08 +00003897 break;
3898
Chris Lattnerdc046542009-05-08 06:58:22 +00003899 case Builtin::BI__sync_lock_release:
Douglas Gregor73722482011-11-28 16:30:08 +00003900 case Builtin::BI__sync_lock_release_1:
3901 case Builtin::BI__sync_lock_release_2:
3902 case Builtin::BI__sync_lock_release_4:
3903 case Builtin::BI__sync_lock_release_8:
3904 case Builtin::BI__sync_lock_release_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003905 BuiltinIndex = 15;
Chris Lattnerdc046542009-05-08 06:58:22 +00003906 NumFixed = 0;
Chandler Carruth3973af72010-07-18 20:54:12 +00003907 ResultType = Context.VoidTy;
Chris Lattnerdc046542009-05-08 06:58:22 +00003908 break;
Douglas Gregor73722482011-11-28 16:30:08 +00003909
3910 case Builtin::BI__sync_swap:
3911 case Builtin::BI__sync_swap_1:
3912 case Builtin::BI__sync_swap_2:
3913 case Builtin::BI__sync_swap_4:
3914 case Builtin::BI__sync_swap_8:
3915 case Builtin::BI__sync_swap_16:
Hal Finkeld2208b52014-10-02 20:53:50 +00003916 BuiltinIndex = 16;
Douglas Gregor73722482011-11-28 16:30:08 +00003917 break;
Chris Lattnerdc046542009-05-08 06:58:22 +00003918 }
Mike Stump11289f42009-09-09 15:08:12 +00003919
Chris Lattnerdc046542009-05-08 06:58:22 +00003920 // Now that we know how many fixed arguments we expect, first check that we
3921 // have at least that many.
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003922 if (TheCall->getNumArgs() < 1+NumFixed) {
3923 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
3924 << 0 << 1+NumFixed << TheCall->getNumArgs()
3925 << TheCall->getCallee()->getSourceRange();
3926 return ExprError();
3927 }
Mike Stump11289f42009-09-09 15:08:12 +00003928
Hal Finkeld2208b52014-10-02 20:53:50 +00003929 if (WarnAboutSemanticsChange) {
3930 Diag(TheCall->getLocEnd(), diag::warn_sync_fetch_and_nand_semantics_change)
3931 << TheCall->getCallee()->getSourceRange();
3932 }
3933
Chris Lattner5b9241b2009-05-08 15:36:58 +00003934 // Get the decl for the concrete builtin from this, we can tell what the
3935 // concrete integer type we should convert to is.
3936 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
Mehdi Amini7186a432016-10-11 19:04:24 +00003937 const char *NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID);
Abramo Bagnara6cba23a2012-09-22 09:05:22 +00003938 FunctionDecl *NewBuiltinDecl;
3939 if (NewBuiltinID == BuiltinID)
3940 NewBuiltinDecl = FDecl;
3941 else {
3942 // Perform builtin lookup to avoid redeclaring it.
3943 DeclarationName DN(&Context.Idents.get(NewBuiltinName));
3944 LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName);
3945 LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true);
3946 assert(Res.getFoundDecl());
3947 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
Craig Topperc3ec1492014-05-26 06:22:03 +00003948 if (!NewBuiltinDecl)
Abramo Bagnara6cba23a2012-09-22 09:05:22 +00003949 return ExprError();
3950 }
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003951
John McCallcf142162010-08-07 06:22:56 +00003952 // The first argument --- the pointer --- has a fixed type; we
3953 // deduce the types of the rest of the arguments accordingly. Walk
3954 // the remaining arguments, converting them to the deduced value type.
Chris Lattnerdc046542009-05-08 06:58:22 +00003955 for (unsigned i = 0; i != NumFixed; ++i) {
John Wiegley01296292011-04-08 18:41:53 +00003956 ExprResult Arg = TheCall->getArg(i+1);
Mike Stump11289f42009-09-09 15:08:12 +00003957
Chris Lattnerdc046542009-05-08 06:58:22 +00003958 // GCC does an implicit conversion to the pointer or integer ValType. This
3959 // can fail in some cases (1i -> int**), check for this error case now.
John McCallb50451a2011-10-05 07:41:44 +00003960 // Initialize the argument.
3961 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
3962 ValType, /*consume*/ false);
3963 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
John Wiegley01296292011-04-08 18:41:53 +00003964 if (Arg.isInvalid())
Chandler Carruth741e5ce2010-07-09 18:59:35 +00003965 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +00003966
Chris Lattnerdc046542009-05-08 06:58:22 +00003967 // Okay, we have something that *can* be converted to the right type. Check
3968 // to see if there is a potentially weird extension going on here. This can
3969 // happen when you do an atomic operation on something like an char* and
3970 // pass in 42. The 42 gets converted to char. This is even more strange
3971 // for things like 45.123 -> char, etc.
Mike Stump11289f42009-09-09 15:08:12 +00003972 // FIXME: Do this check.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003973 TheCall->setArg(i+1, Arg.get());
Chris Lattnerdc046542009-05-08 06:58:22 +00003974 }
Mike Stump11289f42009-09-09 15:08:12 +00003975
Douglas Gregor6b3bcf22011-09-09 16:51:10 +00003976 ASTContext& Context = this->getASTContext();
3977
3978 // Create a new DeclRefExpr to refer to the new decl.
3979 DeclRefExpr* NewDRE = DeclRefExpr::Create(
3980 Context,
3981 DRE->getQualifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00003982 SourceLocation(),
Douglas Gregor6b3bcf22011-09-09 16:51:10 +00003983 NewBuiltinDecl,
John McCall113bee02012-03-10 09:33:50 +00003984 /*enclosing*/ false,
Douglas Gregor6b3bcf22011-09-09 16:51:10 +00003985 DRE->getLocation(),
Eli Friedman34866c72012-08-31 00:14:07 +00003986 Context.BuiltinFnTy,
Douglas Gregor6b3bcf22011-09-09 16:51:10 +00003987 DRE->getValueKind());
Mike Stump11289f42009-09-09 15:08:12 +00003988
Chris Lattnerdc046542009-05-08 06:58:22 +00003989 // Set the callee in the CallExpr.
Eli Friedman34866c72012-08-31 00:14:07 +00003990 // FIXME: This loses syntactic information.
3991 QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType());
3992 ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy,
3993 CK_BuiltinFnToFnPtr);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003994 TheCall->setCallee(PromotedCall.get());
Mike Stump11289f42009-09-09 15:08:12 +00003995
Chandler Carruthbc8cab12010-07-18 07:23:17 +00003996 // Change the result type of the call to match the original value type. This
3997 // is arbitrary, but the codegen for these builtins ins design to handle it
3998 // gracefully.
Chandler Carruth3973af72010-07-18 20:54:12 +00003999 TheCall->setType(ResultType);
Chandler Carruth741e5ce2010-07-09 18:59:35 +00004000
Benjamin Kramer62b95d82012-08-23 21:35:17 +00004001 return TheCallResult;
Chris Lattnerdc046542009-05-08 06:58:22 +00004002}
4003
Michael Zolotukhin84df1232015-09-08 23:52:33 +00004004/// SemaBuiltinNontemporalOverloaded - We have a call to
4005/// __builtin_nontemporal_store or __builtin_nontemporal_load, which is an
4006/// overloaded function based on the pointer type of its last argument.
4007///
4008/// This function goes through and does final semantic checking for these
4009/// builtins.
4010ExprResult Sema::SemaBuiltinNontemporalOverloaded(ExprResult TheCallResult) {
4011 CallExpr *TheCall = (CallExpr *)TheCallResult.get();
4012 DeclRefExpr *DRE =
4013 cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
4014 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
4015 unsigned BuiltinID = FDecl->getBuiltinID();
4016 assert((BuiltinID == Builtin::BI__builtin_nontemporal_store ||
4017 BuiltinID == Builtin::BI__builtin_nontemporal_load) &&
4018 "Unexpected nontemporal load/store builtin!");
4019 bool isStore = BuiltinID == Builtin::BI__builtin_nontemporal_store;
4020 unsigned numArgs = isStore ? 2 : 1;
4021
4022 // Ensure that we have the proper number of arguments.
4023 if (checkArgCount(*this, TheCall, numArgs))
4024 return ExprError();
4025
4026 // Inspect the last argument of the nontemporal builtin. This should always
4027 // be a pointer type, from which we imply the type of the memory access.
4028 // Because it is a pointer type, we don't have to worry about any implicit
4029 // casts here.
4030 Expr *PointerArg = TheCall->getArg(numArgs - 1);
4031 ExprResult PointerArgResult =
4032 DefaultFunctionArrayLvalueConversion(PointerArg);
4033
4034 if (PointerArgResult.isInvalid())
4035 return ExprError();
4036 PointerArg = PointerArgResult.get();
4037 TheCall->setArg(numArgs - 1, PointerArg);
4038
4039 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
4040 if (!pointerType) {
4041 Diag(DRE->getLocStart(), diag::err_nontemporal_builtin_must_be_pointer)
4042 << PointerArg->getType() << PointerArg->getSourceRange();
4043 return ExprError();
4044 }
4045
4046 QualType ValType = pointerType->getPointeeType();
4047
4048 // Strip any qualifiers off ValType.
4049 ValType = ValType.getUnqualifiedType();
4050 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
4051 !ValType->isBlockPointerType() && !ValType->isFloatingType() &&
4052 !ValType->isVectorType()) {
4053 Diag(DRE->getLocStart(),
4054 diag::err_nontemporal_builtin_must_be_pointer_intfltptr_or_vector)
4055 << PointerArg->getType() << PointerArg->getSourceRange();
4056 return ExprError();
4057 }
4058
4059 if (!isStore) {
4060 TheCall->setType(ValType);
4061 return TheCallResult;
4062 }
4063
4064 ExprResult ValArg = TheCall->getArg(0);
4065 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4066 Context, ValType, /*consume*/ false);
4067 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
4068 if (ValArg.isInvalid())
4069 return ExprError();
4070
4071 TheCall->setArg(0, ValArg.get());
4072 TheCall->setType(Context.VoidTy);
4073 return TheCallResult;
4074}
4075
Chris Lattner6436fb62009-02-18 06:01:06 +00004076/// CheckObjCString - Checks that the argument to the builtin
Anders Carlsson98f07902007-08-17 05:31:46 +00004077/// CFString constructor is correct
Steve Narofffb46e862009-04-13 20:26:29 +00004078/// Note: It might also make sense to do the UTF-16 conversion here (would
4079/// simplify the backend).
Chris Lattner6436fb62009-02-18 06:01:06 +00004080bool Sema::CheckObjCString(Expr *Arg) {
Chris Lattnerf2660962008-02-13 01:02:39 +00004081 Arg = Arg->IgnoreParenCasts();
Anders Carlsson98f07902007-08-17 05:31:46 +00004082 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
4083
Douglas Gregorfb65e592011-07-27 05:40:30 +00004084 if (!Literal || !Literal->isAscii()) {
Chris Lattner3b054132008-11-19 05:08:23 +00004085 Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
4086 << Arg->getSourceRange();
Anders Carlssona3a9c432007-08-17 15:44:17 +00004087 return true;
Anders Carlsson98f07902007-08-17 05:31:46 +00004088 }
Mike Stump11289f42009-09-09 15:08:12 +00004089
Fariborz Jahanian56603ef2010-09-07 19:38:13 +00004090 if (Literal->containsNonAsciiOrNull()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004091 StringRef String = Literal->getString();
Fariborz Jahanian56603ef2010-09-07 19:38:13 +00004092 unsigned NumBytes = String.size();
Justin Lebar90910552016-09-30 00:38:45 +00004093 SmallVector<llvm::UTF16, 128> ToBuf(NumBytes);
4094 const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
4095 llvm::UTF16 *ToPtr = &ToBuf[0];
4096
4097 llvm::ConversionResult Result =
4098 llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
4099 ToPtr + NumBytes, llvm::strictConversion);
Fariborz Jahanian56603ef2010-09-07 19:38:13 +00004100 // Check for conversion failure.
Justin Lebar90910552016-09-30 00:38:45 +00004101 if (Result != llvm::conversionOK)
Fariborz Jahanian56603ef2010-09-07 19:38:13 +00004102 Diag(Arg->getLocStart(),
4103 diag::warn_cfstring_truncated) << Arg->getSourceRange();
4104 }
Anders Carlssona3a9c432007-08-17 15:44:17 +00004105 return false;
Chris Lattnerb87b1b32007-08-10 20:18:51 +00004106}
4107
Mehdi Amini06d367c2016-10-24 20:39:34 +00004108/// CheckObjCString - Checks that the format string argument to the os_log()
4109/// and os_trace() functions is correct, and converts it to const char *.
4110ExprResult Sema::CheckOSLogFormatStringArg(Expr *Arg) {
4111 Arg = Arg->IgnoreParenCasts();
4112 auto *Literal = dyn_cast<StringLiteral>(Arg);
4113 if (!Literal) {
4114 if (auto *ObjcLiteral = dyn_cast<ObjCStringLiteral>(Arg)) {
4115 Literal = ObjcLiteral->getString();
4116 }
4117 }
4118
4119 if (!Literal || (!Literal->isAscii() && !Literal->isUTF8())) {
4120 return ExprError(
4121 Diag(Arg->getLocStart(), diag::err_os_log_format_not_string_constant)
4122 << Arg->getSourceRange());
4123 }
4124
4125 ExprResult Result(Literal);
4126 QualType ResultTy = Context.getPointerType(Context.CharTy.withConst());
4127 InitializedEntity Entity =
4128 InitializedEntity::InitializeParameter(Context, ResultTy, false);
4129 Result = PerformCopyInitialization(Entity, SourceLocation(), Result);
4130 return Result;
4131}
4132
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004133/// Check that the user is calling the appropriate va_start builtin for the
4134/// target and calling convention.
4135static bool checkVAStartABI(Sema &S, unsigned BuiltinID, Expr *Fn) {
4136 const llvm::Triple &TT = S.Context.getTargetInfo().getTriple();
4137 bool IsX64 = TT.getArch() == llvm::Triple::x86_64;
Martin Storsjo022e7822017-07-17 20:49:45 +00004138 bool IsAArch64 = TT.getArch() == llvm::Triple::aarch64;
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004139 bool IsWindows = TT.isOSWindows();
Martin Storsjo022e7822017-07-17 20:49:45 +00004140 bool IsMSVAStart = BuiltinID == Builtin::BI__builtin_ms_va_start;
4141 if (IsX64 || IsAArch64) {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00004142 CallingConv CC = CC_C;
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004143 if (const FunctionDecl *FD = S.getCurFunctionDecl())
4144 CC = FD->getType()->getAs<FunctionType>()->getCallConv();
4145 if (IsMSVAStart) {
4146 // Don't allow this in System V ABI functions.
Martin Storsjo022e7822017-07-17 20:49:45 +00004147 if (CC == CC_X86_64SysV || (!IsWindows && CC != CC_Win64))
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004148 return S.Diag(Fn->getLocStart(),
4149 diag::err_ms_va_start_used_in_sysv_function);
4150 } else {
Martin Storsjo022e7822017-07-17 20:49:45 +00004151 // On x86-64/AArch64 Unix, don't allow this in Win64 ABI functions.
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004152 // On x64 Windows, don't allow this in System V ABI functions.
4153 // (Yes, that means there's no corresponding way to support variadic
4154 // System V ABI functions on Windows.)
4155 if ((IsWindows && CC == CC_X86_64SysV) ||
Martin Storsjo022e7822017-07-17 20:49:45 +00004156 (!IsWindows && CC == CC_Win64))
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004157 return S.Diag(Fn->getLocStart(),
4158 diag::err_va_start_used_in_wrong_abi_function)
4159 << !IsWindows;
4160 }
4161 return false;
4162 }
4163
4164 if (IsMSVAStart)
Martin Storsjo022e7822017-07-17 20:49:45 +00004165 return S.Diag(Fn->getLocStart(), diag::err_builtin_x64_aarch64_only);
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004166 return false;
4167}
4168
4169static bool checkVAStartIsInVariadicFunction(Sema &S, Expr *Fn,
4170 ParmVarDecl **LastParam = nullptr) {
4171 // Determine whether the current function, block, or obj-c method is variadic
4172 // and get its parameter list.
4173 bool IsVariadic = false;
4174 ArrayRef<ParmVarDecl *> Params;
Reid Klecknerf1deb832017-05-04 19:51:05 +00004175 DeclContext *Caller = S.CurContext;
4176 if (auto *Block = dyn_cast<BlockDecl>(Caller)) {
4177 IsVariadic = Block->isVariadic();
4178 Params = Block->parameters();
4179 } else if (auto *FD = dyn_cast<FunctionDecl>(Caller)) {
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004180 IsVariadic = FD->isVariadic();
4181 Params = FD->parameters();
Reid Klecknerf1deb832017-05-04 19:51:05 +00004182 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(Caller)) {
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004183 IsVariadic = MD->isVariadic();
4184 // FIXME: This isn't correct for methods (results in bogus warning).
4185 Params = MD->parameters();
Reid Klecknerf1deb832017-05-04 19:51:05 +00004186 } else if (isa<CapturedDecl>(Caller)) {
4187 // We don't support va_start in a CapturedDecl.
4188 S.Diag(Fn->getLocStart(), diag::err_va_start_captured_stmt);
4189 return true;
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004190 } else {
Reid Klecknerf1deb832017-05-04 19:51:05 +00004191 // This must be some other declcontext that parses exprs.
4192 S.Diag(Fn->getLocStart(), diag::err_va_start_outside_function);
4193 return true;
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004194 }
4195
4196 if (!IsVariadic) {
Reid Klecknerf1deb832017-05-04 19:51:05 +00004197 S.Diag(Fn->getLocStart(), diag::err_va_start_fixed_function);
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004198 return true;
4199 }
4200
4201 if (LastParam)
4202 *LastParam = Params.empty() ? nullptr : Params.back();
4203
4204 return false;
4205}
4206
Charles Davisc7d5c942015-09-17 20:55:33 +00004207/// Check the arguments to '__builtin_va_start' or '__builtin_ms_va_start'
4208/// for validity. Emit an error and return true on failure; return false
4209/// on success.
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004210bool Sema::SemaBuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) {
Chris Lattner08464942007-12-28 05:29:59 +00004211 Expr *Fn = TheCall->getCallee();
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004212
4213 if (checkVAStartABI(*this, BuiltinID, Fn))
4214 return true;
4215
Chris Lattner08464942007-12-28 05:29:59 +00004216 if (TheCall->getNumArgs() > 2) {
Chris Lattnercedef8d2008-11-21 18:44:24 +00004217 Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattner3b054132008-11-19 05:08:23 +00004218 diag::err_typecheck_call_too_many_args)
Eric Christopher2a5aaff2010-04-16 04:56:46 +00004219 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
4220 << Fn->getSourceRange()
Mike Stump11289f42009-09-09 15:08:12 +00004221 << SourceRange(TheCall->getArg(2)->getLocStart(),
Chris Lattner3b054132008-11-19 05:08:23 +00004222 (*(TheCall->arg_end()-1))->getLocEnd());
Chris Lattner43be2e62007-12-19 23:59:04 +00004223 return true;
4224 }
Eli Friedmanbb2b3be2008-12-15 22:05:35 +00004225
4226 if (TheCall->getNumArgs() < 2) {
Eric Christopherabf1e182010-04-16 04:48:22 +00004227 return Diag(TheCall->getLocEnd(),
4228 diag::err_typecheck_call_too_few_args_at_least)
4229 << 0 /*function call*/ << 2 << TheCall->getNumArgs();
Eli Friedmanbb2b3be2008-12-15 22:05:35 +00004230 }
4231
John McCall29ad95b2011-08-27 01:09:30 +00004232 // Type-check the first argument normally.
4233 if (checkBuiltinArgument(*this, TheCall, 0))
4234 return true;
4235
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004236 // Check that the current function is variadic, and get its last parameter.
4237 ParmVarDecl *LastParam;
4238 if (checkVAStartIsInVariadicFunction(*this, Fn, &LastParam))
Chris Lattner43be2e62007-12-19 23:59:04 +00004239 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004240
Chris Lattner43be2e62007-12-19 23:59:04 +00004241 // Verify that the second argument to the builtin is the last argument of the
4242 // current function or method.
4243 bool SecondArgIsLastNamedArgument = false;
Anders Carlsson73cc5072008-02-13 01:22:59 +00004244 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +00004245
Nico Weber9eea7642013-05-24 23:31:57 +00004246 // These are valid if SecondArgIsLastNamedArgument is false after the next
4247 // block.
4248 QualType Type;
4249 SourceLocation ParamLoc;
Aaron Ballman1de59c52016-04-24 13:30:21 +00004250 bool IsCRegister = false;
Nico Weber9eea7642013-05-24 23:31:57 +00004251
Anders Carlsson6a8350b2008-02-11 04:20:54 +00004252 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
4253 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004254 SecondArgIsLastNamedArgument = PV == LastParam;
Nico Weber9eea7642013-05-24 23:31:57 +00004255
4256 Type = PV->getType();
4257 ParamLoc = PV->getLocation();
Aaron Ballman1de59c52016-04-24 13:30:21 +00004258 IsCRegister =
4259 PV->getStorageClass() == SC_Register && !getLangOpts().CPlusPlus;
Chris Lattner43be2e62007-12-19 23:59:04 +00004260 }
4261 }
Mike Stump11289f42009-09-09 15:08:12 +00004262
Chris Lattner43be2e62007-12-19 23:59:04 +00004263 if (!SecondArgIsLastNamedArgument)
Mike Stump11289f42009-09-09 15:08:12 +00004264 Diag(TheCall->getArg(1)->getLocStart(),
Aaron Ballman05164812016-04-18 18:10:53 +00004265 diag::warn_second_arg_of_va_start_not_last_named_param);
Aaron Ballman1de59c52016-04-24 13:30:21 +00004266 else if (IsCRegister || Type->isReferenceType() ||
Aaron Ballmana4f597f2016-09-15 18:07:51 +00004267 Type->isSpecificBuiltinType(BuiltinType::Float) || [=] {
4268 // Promotable integers are UB, but enumerations need a bit of
4269 // extra checking to see what their promotable type actually is.
4270 if (!Type->isPromotableIntegerType())
4271 return false;
4272 if (!Type->isEnumeralType())
4273 return true;
4274 const EnumDecl *ED = Type->getAs<EnumType>()->getDecl();
4275 return !(ED &&
4276 Context.typesAreCompatible(ED->getPromotionType(), Type));
4277 }()) {
Aaron Ballman1de59c52016-04-24 13:30:21 +00004278 unsigned Reason = 0;
4279 if (Type->isReferenceType()) Reason = 1;
4280 else if (IsCRegister) Reason = 2;
4281 Diag(Arg->getLocStart(), diag::warn_va_start_type_is_undefined) << Reason;
Nico Weber9eea7642013-05-24 23:31:57 +00004282 Diag(ParamLoc, diag::note_parameter_type) << Type;
4283 }
4284
Enea Zaffanellab1b1b8a2013-11-07 08:14:26 +00004285 TheCall->setType(Context.VoidTy);
Chris Lattner43be2e62007-12-19 23:59:04 +00004286 return false;
Eli Friedmanf8353032008-05-20 08:23:37 +00004287}
Chris Lattner43be2e62007-12-19 23:59:04 +00004288
Saleem Abdulrasool3450aa72017-09-26 20:12:04 +00004289bool Sema::SemaBuiltinVAStartARMMicrosoft(CallExpr *Call) {
Saleem Abdulrasool202aac12014-07-22 02:01:04 +00004290 // void __va_start(va_list *ap, const char *named_addr, size_t slot_size,
4291 // const char *named_addr);
4292
4293 Expr *Func = Call->getCallee();
4294
4295 if (Call->getNumArgs() < 3)
4296 return Diag(Call->getLocEnd(),
4297 diag::err_typecheck_call_too_few_args_at_least)
4298 << 0 /*function call*/ << 3 << Call->getNumArgs();
4299
Saleem Abdulrasool202aac12014-07-22 02:01:04 +00004300 // Type-check the first argument normally.
4301 if (checkBuiltinArgument(*this, Call, 0))
4302 return true;
4303
Reid Kleckner2b0fa122017-05-02 20:10:03 +00004304 // Check that the current function is variadic.
4305 if (checkVAStartIsInVariadicFunction(*this, Func))
4306 return true;
4307
Saleem Abdulrasool448e8ad2017-09-26 17:44:10 +00004308 // __va_start on Windows does not validate the parameter qualifiers
Saleem Abdulrasool202aac12014-07-22 02:01:04 +00004309
Saleem Abdulrasool448e8ad2017-09-26 17:44:10 +00004310 const Expr *Arg1 = Call->getArg(1)->IgnoreParens();
4311 const Type *Arg1Ty = Arg1->getType().getCanonicalType().getTypePtr();
4312
4313 const Expr *Arg2 = Call->getArg(2)->IgnoreParens();
4314 const Type *Arg2Ty = Arg2->getType().getCanonicalType().getTypePtr();
4315
4316 const QualType &ConstCharPtrTy =
4317 Context.getPointerType(Context.CharTy.withConst());
4318 if (!Arg1Ty->isPointerType() ||
4319 Arg1Ty->getPointeeType().withoutLocalFastQualifiers() != Context.CharTy)
4320 Diag(Arg1->getLocStart(), diag::err_typecheck_convert_incompatible)
4321 << Arg1->getType() << ConstCharPtrTy
4322 << 1 /* different class */
4323 << 0 /* qualifier difference */
4324 << 3 /* parameter mismatch */
4325 << 2 << Arg1->getType() << ConstCharPtrTy;
4326
4327 const QualType SizeTy = Context.getSizeType();
4328 if (Arg2Ty->getCanonicalTypeInternal().withoutLocalFastQualifiers() != SizeTy)
4329 Diag(Arg2->getLocStart(), diag::err_typecheck_convert_incompatible)
4330 << Arg2->getType() << SizeTy
4331 << 1 /* different class */
4332 << 0 /* qualifier difference */
4333 << 3 /* parameter mismatch */
4334 << 3 << Arg2->getType() << SizeTy;
Saleem Abdulrasool202aac12014-07-22 02:01:04 +00004335
4336 return false;
4337}
4338
Chris Lattner2da14fb2007-12-20 00:26:33 +00004339/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
4340/// friends. This is declared to take (...), so we have to check everything.
Chris Lattner08464942007-12-28 05:29:59 +00004341bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
4342 if (TheCall->getNumArgs() < 2)
Chris Lattnercedef8d2008-11-21 18:44:24 +00004343 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Eric Christopherabf1e182010-04-16 04:48:22 +00004344 << 0 << 2 << TheCall->getNumArgs()/*function call*/;
Chris Lattner08464942007-12-28 05:29:59 +00004345 if (TheCall->getNumArgs() > 2)
Mike Stump11289f42009-09-09 15:08:12 +00004346 return Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattner3b054132008-11-19 05:08:23 +00004347 diag::err_typecheck_call_too_many_args)
Eric Christopher2a5aaff2010-04-16 04:56:46 +00004348 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
Chris Lattner3b054132008-11-19 05:08:23 +00004349 << SourceRange(TheCall->getArg(2)->getLocStart(),
4350 (*(TheCall->arg_end()-1))->getLocEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004351
John Wiegley01296292011-04-08 18:41:53 +00004352 ExprResult OrigArg0 = TheCall->getArg(0);
4353 ExprResult OrigArg1 = TheCall->getArg(1);
Douglas Gregorc25f7662009-05-19 22:10:17 +00004354
Chris Lattner2da14fb2007-12-20 00:26:33 +00004355 // Do standard promotions between the two arguments, returning their common
4356 // type.
Chris Lattner08464942007-12-28 05:29:59 +00004357 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
John Wiegley01296292011-04-08 18:41:53 +00004358 if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
4359 return true;
Daniel Dunbar96f86772009-02-19 19:28:43 +00004360
4361 // Make sure any conversions are pushed back into the call; this is
4362 // type safe since unordered compare builtins are declared as "_Bool
4363 // foo(...)".
John Wiegley01296292011-04-08 18:41:53 +00004364 TheCall->setArg(0, OrigArg0.get());
4365 TheCall->setArg(1, OrigArg1.get());
Mike Stump11289f42009-09-09 15:08:12 +00004366
John Wiegley01296292011-04-08 18:41:53 +00004367 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
Douglas Gregorc25f7662009-05-19 22:10:17 +00004368 return false;
4369
Chris Lattner2da14fb2007-12-20 00:26:33 +00004370 // If the common type isn't a real floating type, then the arguments were
4371 // invalid for this operation.
Eli Friedman93ee5ca2012-06-16 02:19:17 +00004372 if (Res.isNull() || !Res->isRealFloatingType())
John Wiegley01296292011-04-08 18:41:53 +00004373 return Diag(OrigArg0.get()->getLocStart(),
Chris Lattner3b054132008-11-19 05:08:23 +00004374 diag::err_typecheck_call_invalid_ordered_compare)
John Wiegley01296292011-04-08 18:41:53 +00004375 << OrigArg0.get()->getType() << OrigArg1.get()->getType()
4376 << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004377
Chris Lattner2da14fb2007-12-20 00:26:33 +00004378 return false;
4379}
4380
Benjamin Kramer634fc102010-02-15 22:42:31 +00004381/// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
4382/// __builtin_isnan and friends. This is declared to take (...), so we have
Benjamin Kramer64aae502010-02-16 10:07:31 +00004383/// to check everything. We expect the last argument to be a floating point
4384/// value.
4385bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
4386 if (TheCall->getNumArgs() < NumArgs)
Eli Friedman7e4faac2009-08-31 20:06:00 +00004387 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Eric Christopherabf1e182010-04-16 04:48:22 +00004388 << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
Benjamin Kramer64aae502010-02-16 10:07:31 +00004389 if (TheCall->getNumArgs() > NumArgs)
4390 return Diag(TheCall->getArg(NumArgs)->getLocStart(),
Eli Friedman7e4faac2009-08-31 20:06:00 +00004391 diag::err_typecheck_call_too_many_args)
Eric Christopher2a5aaff2010-04-16 04:56:46 +00004392 << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
Benjamin Kramer64aae502010-02-16 10:07:31 +00004393 << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
Eli Friedman7e4faac2009-08-31 20:06:00 +00004394 (*(TheCall->arg_end()-1))->getLocEnd());
4395
Benjamin Kramer64aae502010-02-16 10:07:31 +00004396 Expr *OrigArg = TheCall->getArg(NumArgs-1);
Mike Stump11289f42009-09-09 15:08:12 +00004397
Eli Friedman7e4faac2009-08-31 20:06:00 +00004398 if (OrigArg->isTypeDependent())
4399 return false;
4400
Chris Lattner68784ef2010-05-06 05:50:07 +00004401 // This operation requires a non-_Complex floating-point number.
Eli Friedman7e4faac2009-08-31 20:06:00 +00004402 if (!OrigArg->getType()->isRealFloatingType())
Mike Stump11289f42009-09-09 15:08:12 +00004403 return Diag(OrigArg->getLocStart(),
Eli Friedman7e4faac2009-08-31 20:06:00 +00004404 diag::err_typecheck_call_invalid_unary_fp)
4405 << OrigArg->getType() << OrigArg->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00004406
Neil Hickey88c0fac2016-12-13 16:22:50 +00004407 // If this is an implicit conversion from float -> float or double, remove it.
Chris Lattner68784ef2010-05-06 05:50:07 +00004408 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
Neil Hickey7b5ddab2016-12-14 13:18:48 +00004409 // Only remove standard FloatCasts, leaving other casts inplace
4410 if (Cast->getCastKind() == CK_FloatingCast) {
4411 Expr *CastArg = Cast->getSubExpr();
4412 if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
4413 assert((Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) ||
4414 Cast->getType()->isSpecificBuiltinType(BuiltinType::Float)) &&
4415 "promotion from float to either float or double is the only expected cast here");
4416 Cast->setSubExpr(nullptr);
4417 TheCall->setArg(NumArgs-1, CastArg);
4418 }
Chris Lattner68784ef2010-05-06 05:50:07 +00004419 }
4420 }
4421
Eli Friedman7e4faac2009-08-31 20:06:00 +00004422 return false;
4423}
4424
Tony Jiangbbc48e92017-05-24 15:13:32 +00004425// Customized Sema Checking for VSX builtins that have the following signature:
4426// vector [...] builtinName(vector [...], vector [...], const int);
4427// Which takes the same type of vectors (any legal vector type) for the first
4428// two arguments and takes compile time constant for the third argument.
4429// Example builtins are :
4430// vector double vec_xxpermdi(vector double, vector double, int);
4431// vector short vec_xxsldwi(vector short, vector short, int);
4432bool Sema::SemaBuiltinVSX(CallExpr *TheCall) {
4433 unsigned ExpectedNumArgs = 3;
4434 if (TheCall->getNumArgs() < ExpectedNumArgs)
4435 return Diag(TheCall->getLocEnd(),
4436 diag::err_typecheck_call_too_few_args_at_least)
4437 << 0 /*function call*/ << ExpectedNumArgs << TheCall->getNumArgs()
4438 << TheCall->getSourceRange();
4439
4440 if (TheCall->getNumArgs() > ExpectedNumArgs)
4441 return Diag(TheCall->getLocEnd(),
4442 diag::err_typecheck_call_too_many_args_at_most)
4443 << 0 /*function call*/ << ExpectedNumArgs << TheCall->getNumArgs()
4444 << TheCall->getSourceRange();
4445
4446 // Check the third argument is a compile time constant
4447 llvm::APSInt Value;
4448 if(!TheCall->getArg(2)->isIntegerConstantExpr(Value, Context))
4449 return Diag(TheCall->getLocStart(),
4450 diag::err_vsx_builtin_nonconstant_argument)
4451 << 3 /* argument index */ << TheCall->getDirectCallee()
4452 << SourceRange(TheCall->getArg(2)->getLocStart(),
4453 TheCall->getArg(2)->getLocEnd());
4454
4455 QualType Arg1Ty = TheCall->getArg(0)->getType();
4456 QualType Arg2Ty = TheCall->getArg(1)->getType();
4457
4458 // Check the type of argument 1 and argument 2 are vectors.
4459 SourceLocation BuiltinLoc = TheCall->getLocStart();
4460 if ((!Arg1Ty->isVectorType() && !Arg1Ty->isDependentType()) ||
4461 (!Arg2Ty->isVectorType() && !Arg2Ty->isDependentType())) {
4462 return Diag(BuiltinLoc, diag::err_vec_builtin_non_vector)
4463 << TheCall->getDirectCallee()
4464 << SourceRange(TheCall->getArg(0)->getLocStart(),
4465 TheCall->getArg(1)->getLocEnd());
4466 }
4467
4468 // Check the first two arguments are the same type.
4469 if (!Context.hasSameUnqualifiedType(Arg1Ty, Arg2Ty)) {
4470 return Diag(BuiltinLoc, diag::err_vec_builtin_incompatible_vector)
4471 << TheCall->getDirectCallee()
4472 << SourceRange(TheCall->getArg(0)->getLocStart(),
4473 TheCall->getArg(1)->getLocEnd());
4474 }
4475
4476 // When default clang type checking is turned off and the customized type
4477 // checking is used, the returning type of the function must be explicitly
4478 // set. Otherwise it is _Bool by default.
4479 TheCall->setType(Arg1Ty);
4480
4481 return false;
4482}
4483
Eli Friedmana1b4ed82008-05-14 19:38:39 +00004484/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
4485// This is declared to take (...), so we have to check everything.
John McCalldadc5752010-08-24 06:29:42 +00004486ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
Nate Begemana0110022010-06-08 00:16:34 +00004487 if (TheCall->getNumArgs() < 2)
Sebastian Redlc215cfc2009-01-19 00:08:26 +00004488 return ExprError(Diag(TheCall->getLocEnd(),
Eric Christopherabf1e182010-04-16 04:48:22 +00004489 diag::err_typecheck_call_too_few_args_at_least)
Craig Topper304602a2013-07-28 21:50:10 +00004490 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
4491 << TheCall->getSourceRange());
Eli Friedmana1b4ed82008-05-14 19:38:39 +00004492
Nate Begemana0110022010-06-08 00:16:34 +00004493 // Determine which of the following types of shufflevector we're checking:
4494 // 1) unary, vector mask: (lhs, mask)
Craig Topperb3174a82016-05-18 04:11:25 +00004495 // 2) binary, scalar mask: (lhs, rhs, index, ..., index)
Nate Begemana0110022010-06-08 00:16:34 +00004496 QualType resType = TheCall->getArg(0)->getType();
4497 unsigned numElements = 0;
Craig Topper61d01cc2013-07-19 04:46:31 +00004498
Douglas Gregorc25f7662009-05-19 22:10:17 +00004499 if (!TheCall->getArg(0)->isTypeDependent() &&
4500 !TheCall->getArg(1)->isTypeDependent()) {
Nate Begemana0110022010-06-08 00:16:34 +00004501 QualType LHSType = TheCall->getArg(0)->getType();
4502 QualType RHSType = TheCall->getArg(1)->getType();
Craig Topper61d01cc2013-07-19 04:46:31 +00004503
Craig Topperbaca3892013-07-29 06:47:04 +00004504 if (!LHSType->isVectorType() || !RHSType->isVectorType())
4505 return ExprError(Diag(TheCall->getLocStart(),
Tony Jiangedc78492017-05-24 14:45:57 +00004506 diag::err_vec_builtin_non_vector)
4507 << TheCall->getDirectCallee()
Craig Topperbaca3892013-07-29 06:47:04 +00004508 << SourceRange(TheCall->getArg(0)->getLocStart(),
4509 TheCall->getArg(1)->getLocEnd()));
Craig Topper61d01cc2013-07-19 04:46:31 +00004510
Nate Begemana0110022010-06-08 00:16:34 +00004511 numElements = LHSType->getAs<VectorType>()->getNumElements();
4512 unsigned numResElements = TheCall->getNumArgs() - 2;
Mike Stump11289f42009-09-09 15:08:12 +00004513
Nate Begemana0110022010-06-08 00:16:34 +00004514 // Check to see if we have a call with 2 vector arguments, the unary shuffle
4515 // with mask. If so, verify that RHS is an integer vector type with the
4516 // same number of elts as lhs.
4517 if (TheCall->getNumArgs() == 2) {
Sylvestre Ledru8e5d82e2013-07-06 08:00:09 +00004518 if (!RHSType->hasIntegerRepresentation() ||
Nate Begemana0110022010-06-08 00:16:34 +00004519 RHSType->getAs<VectorType>()->getNumElements() != numElements)
Craig Topperbaca3892013-07-29 06:47:04 +00004520 return ExprError(Diag(TheCall->getLocStart(),
Tony Jiangedc78492017-05-24 14:45:57 +00004521 diag::err_vec_builtin_incompatible_vector)
4522 << TheCall->getDirectCallee()
Craig Topperbaca3892013-07-29 06:47:04 +00004523 << SourceRange(TheCall->getArg(1)->getLocStart(),
4524 TheCall->getArg(1)->getLocEnd()));
Craig Topper61d01cc2013-07-19 04:46:31 +00004525 } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
Craig Topperbaca3892013-07-29 06:47:04 +00004526 return ExprError(Diag(TheCall->getLocStart(),
Tony Jiangedc78492017-05-24 14:45:57 +00004527 diag::err_vec_builtin_incompatible_vector)
4528 << TheCall->getDirectCallee()
Craig Topperbaca3892013-07-29 06:47:04 +00004529 << SourceRange(TheCall->getArg(0)->getLocStart(),
4530 TheCall->getArg(1)->getLocEnd()));
Nate Begemana0110022010-06-08 00:16:34 +00004531 } else if (numElements != numResElements) {
4532 QualType eltType = LHSType->getAs<VectorType>()->getElementType();
Chris Lattner37141f42010-06-23 06:00:24 +00004533 resType = Context.getVectorType(eltType, numResElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00004534 VectorType::GenericVector);
Douglas Gregorc25f7662009-05-19 22:10:17 +00004535 }
Eli Friedmana1b4ed82008-05-14 19:38:39 +00004536 }
4537
4538 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
Douglas Gregorc25f7662009-05-19 22:10:17 +00004539 if (TheCall->getArg(i)->isTypeDependent() ||
4540 TheCall->getArg(i)->isValueDependent())
4541 continue;
4542
Nate Begemana0110022010-06-08 00:16:34 +00004543 llvm::APSInt Result(32);
4544 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
4545 return ExprError(Diag(TheCall->getLocStart(),
Craig Topper304602a2013-07-28 21:50:10 +00004546 diag::err_shufflevector_nonconstant_argument)
4547 << TheCall->getArg(i)->getSourceRange());
Sebastian Redlc215cfc2009-01-19 00:08:26 +00004548
Craig Topper50ad5b72013-08-03 17:40:38 +00004549 // Allow -1 which will be translated to undef in the IR.
4550 if (Result.isSigned() && Result.isAllOnesValue())
4551 continue;
4552
Chris Lattner7ab824e2008-08-10 02:05:13 +00004553 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
Sebastian Redlc215cfc2009-01-19 00:08:26 +00004554 return ExprError(Diag(TheCall->getLocStart(),
Craig Topper304602a2013-07-28 21:50:10 +00004555 diag::err_shufflevector_argument_too_large)
4556 << TheCall->getArg(i)->getSourceRange());
Eli Friedmana1b4ed82008-05-14 19:38:39 +00004557 }
4558
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004559 SmallVector<Expr*, 32> exprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00004560
Chris Lattner7ab824e2008-08-10 02:05:13 +00004561 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
Eli Friedmana1b4ed82008-05-14 19:38:39 +00004562 exprs.push_back(TheCall->getArg(i));
Craig Topperc3ec1492014-05-26 06:22:03 +00004563 TheCall->setArg(i, nullptr);
Eli Friedmana1b4ed82008-05-14 19:38:39 +00004564 }
4565
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004566 return new (Context) ShuffleVectorExpr(Context, exprs, resType,
4567 TheCall->getCallee()->getLocStart(),
4568 TheCall->getRParenLoc());
Eli Friedmana1b4ed82008-05-14 19:38:39 +00004569}
Chris Lattner43be2e62007-12-19 23:59:04 +00004570
Hal Finkelc4d7c822013-09-18 03:29:45 +00004571/// SemaConvertVectorExpr - Handle __builtin_convertvector
4572ExprResult Sema::SemaConvertVectorExpr(Expr *E, TypeSourceInfo *TInfo,
4573 SourceLocation BuiltinLoc,
4574 SourceLocation RParenLoc) {
4575 ExprValueKind VK = VK_RValue;
4576 ExprObjectKind OK = OK_Ordinary;
4577 QualType DstTy = TInfo->getType();
4578 QualType SrcTy = E->getType();
4579
4580 if (!SrcTy->isVectorType() && !SrcTy->isDependentType())
4581 return ExprError(Diag(BuiltinLoc,
4582 diag::err_convertvector_non_vector)
4583 << E->getSourceRange());
4584 if (!DstTy->isVectorType() && !DstTy->isDependentType())
4585 return ExprError(Diag(BuiltinLoc,
4586 diag::err_convertvector_non_vector_type));
4587
4588 if (!SrcTy->isDependentType() && !DstTy->isDependentType()) {
4589 unsigned SrcElts = SrcTy->getAs<VectorType>()->getNumElements();
4590 unsigned DstElts = DstTy->getAs<VectorType>()->getNumElements();
4591 if (SrcElts != DstElts)
4592 return ExprError(Diag(BuiltinLoc,
4593 diag::err_convertvector_incompatible_vector)
4594 << E->getSourceRange());
4595 }
4596
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004597 return new (Context)
4598 ConvertVectorExpr(E, TInfo, DstTy, VK, OK, BuiltinLoc, RParenLoc);
Hal Finkelc4d7c822013-09-18 03:29:45 +00004599}
4600
Daniel Dunbarb7257262008-07-21 22:59:13 +00004601/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
4602// This is declared to take (const void*, ...) and can take two
4603// optional constant int args.
4604bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
Chris Lattner3b054132008-11-19 05:08:23 +00004605 unsigned NumArgs = TheCall->getNumArgs();
Daniel Dunbarb7257262008-07-21 22:59:13 +00004606
Chris Lattner3b054132008-11-19 05:08:23 +00004607 if (NumArgs > 3)
Eric Christopher2a5aaff2010-04-16 04:56:46 +00004608 return Diag(TheCall->getLocEnd(),
4609 diag::err_typecheck_call_too_many_args_at_most)
4610 << 0 /*function call*/ << 3 << NumArgs
4611 << TheCall->getSourceRange();
Daniel Dunbarb7257262008-07-21 22:59:13 +00004612
4613 // Argument 0 is checked for us and the remaining arguments must be
4614 // constant integers.
Richard Sandiford28940af2014-04-16 08:47:51 +00004615 for (unsigned i = 1; i != NumArgs; ++i)
4616 if (SemaBuiltinConstantArgRange(TheCall, i, 0, i == 1 ? 1 : 3))
Eric Christopher8d0c6212010-04-17 02:26:23 +00004617 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004618
Warren Hunt20e4a5d2014-02-21 23:08:53 +00004619 return false;
4620}
4621
Hal Finkelf0417332014-07-17 14:25:55 +00004622/// SemaBuiltinAssume - Handle __assume (MS Extension).
4623// __assume does not evaluate its arguments, and should warn if its argument
4624// has side effects.
4625bool Sema::SemaBuiltinAssume(CallExpr *TheCall) {
4626 Expr *Arg = TheCall->getArg(0);
4627 if (Arg->isInstantiationDependent()) return false;
4628
4629 if (Arg->HasSideEffects(Context))
David Majnemer51236642015-02-26 00:57:33 +00004630 Diag(Arg->getLocStart(), diag::warn_assume_side_effects)
Hal Finkelbcc06082014-09-07 22:58:14 +00004631 << Arg->getSourceRange()
4632 << cast<FunctionDecl>(TheCall->getCalleeDecl())->getIdentifier();
4633
4634 return false;
4635}
4636
David Majnemer86b1bfa2016-10-31 18:07:57 +00004637/// Handle __builtin_alloca_with_align. This is declared
David Majnemer51169932016-10-31 05:37:48 +00004638/// as (size_t, size_t) where the second size_t must be a power of 2 greater
4639/// than 8.
4640bool Sema::SemaBuiltinAllocaWithAlign(CallExpr *TheCall) {
4641 // The alignment must be a constant integer.
4642 Expr *Arg = TheCall->getArg(1);
4643
4644 // We can't check the value of a dependent argument.
4645 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
David Majnemer86b1bfa2016-10-31 18:07:57 +00004646 if (const auto *UE =
4647 dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts()))
4648 if (UE->getKind() == UETT_AlignOf)
4649 Diag(TheCall->getLocStart(), diag::warn_alloca_align_alignof)
4650 << Arg->getSourceRange();
4651
David Majnemer51169932016-10-31 05:37:48 +00004652 llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context);
4653
4654 if (!Result.isPowerOf2())
4655 return Diag(TheCall->getLocStart(),
4656 diag::err_alignment_not_power_of_two)
4657 << Arg->getSourceRange();
4658
4659 if (Result < Context.getCharWidth())
4660 return Diag(TheCall->getLocStart(), diag::err_alignment_too_small)
4661 << (unsigned)Context.getCharWidth()
4662 << Arg->getSourceRange();
4663
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00004664 if (Result > std::numeric_limits<int32_t>::max())
David Majnemer51169932016-10-31 05:37:48 +00004665 return Diag(TheCall->getLocStart(), diag::err_alignment_too_big)
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00004666 << std::numeric_limits<int32_t>::max()
David Majnemer51169932016-10-31 05:37:48 +00004667 << Arg->getSourceRange();
4668 }
4669
4670 return false;
4671}
4672
4673/// Handle __builtin_assume_aligned. This is declared
Hal Finkelbcc06082014-09-07 22:58:14 +00004674/// as (const void*, size_t, ...) and can take one optional constant int arg.
4675bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) {
4676 unsigned NumArgs = TheCall->getNumArgs();
4677
4678 if (NumArgs > 3)
4679 return Diag(TheCall->getLocEnd(),
4680 diag::err_typecheck_call_too_many_args_at_most)
4681 << 0 /*function call*/ << 3 << NumArgs
4682 << TheCall->getSourceRange();
4683
4684 // The alignment must be a constant integer.
4685 Expr *Arg = TheCall->getArg(1);
4686
4687 // We can't check the value of a dependent argument.
4688 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
4689 llvm::APSInt Result;
4690 if (SemaBuiltinConstantArg(TheCall, 1, Result))
4691 return true;
4692
4693 if (!Result.isPowerOf2())
4694 return Diag(TheCall->getLocStart(),
4695 diag::err_alignment_not_power_of_two)
4696 << Arg->getSourceRange();
4697 }
4698
4699 if (NumArgs > 2) {
4700 ExprResult Arg(TheCall->getArg(2));
4701 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
4702 Context.getSizeType(), false);
4703 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
4704 if (Arg.isInvalid()) return true;
4705 TheCall->setArg(2, Arg.get());
4706 }
Hal Finkelf0417332014-07-17 14:25:55 +00004707
4708 return false;
4709}
4710
Mehdi Amini06d367c2016-10-24 20:39:34 +00004711bool Sema::SemaBuiltinOSLogFormat(CallExpr *TheCall) {
4712 unsigned BuiltinID =
4713 cast<FunctionDecl>(TheCall->getCalleeDecl())->getBuiltinID();
4714 bool IsSizeCall = BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size;
4715
4716 unsigned NumArgs = TheCall->getNumArgs();
4717 unsigned NumRequiredArgs = IsSizeCall ? 1 : 2;
4718 if (NumArgs < NumRequiredArgs) {
4719 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
4720 << 0 /* function call */ << NumRequiredArgs << NumArgs
4721 << TheCall->getSourceRange();
4722 }
4723 if (NumArgs >= NumRequiredArgs + 0x100) {
4724 return Diag(TheCall->getLocEnd(),
4725 diag::err_typecheck_call_too_many_args_at_most)
4726 << 0 /* function call */ << (NumRequiredArgs + 0xff) << NumArgs
4727 << TheCall->getSourceRange();
4728 }
4729 unsigned i = 0;
4730
4731 // For formatting call, check buffer arg.
4732 if (!IsSizeCall) {
4733 ExprResult Arg(TheCall->getArg(i));
4734 InitializedEntity Entity = InitializedEntity::InitializeParameter(
4735 Context, Context.VoidPtrTy, false);
4736 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
4737 if (Arg.isInvalid())
4738 return true;
4739 TheCall->setArg(i, Arg.get());
4740 i++;
4741 }
4742
4743 // Check string literal arg.
4744 unsigned FormatIdx = i;
4745 {
4746 ExprResult Arg = CheckOSLogFormatStringArg(TheCall->getArg(i));
4747 if (Arg.isInvalid())
4748 return true;
4749 TheCall->setArg(i, Arg.get());
4750 i++;
4751 }
4752
4753 // Make sure variadic args are scalar.
4754 unsigned FirstDataArg = i;
4755 while (i < NumArgs) {
4756 ExprResult Arg = DefaultVariadicArgumentPromotion(
4757 TheCall->getArg(i), VariadicFunction, nullptr);
4758 if (Arg.isInvalid())
4759 return true;
4760 CharUnits ArgSize = Context.getTypeSizeInChars(Arg.get()->getType());
4761 if (ArgSize.getQuantity() >= 0x100) {
4762 return Diag(Arg.get()->getLocEnd(), diag::err_os_log_argument_too_big)
4763 << i << (int)ArgSize.getQuantity() << 0xff
4764 << TheCall->getSourceRange();
4765 }
4766 TheCall->setArg(i, Arg.get());
4767 i++;
4768 }
4769
4770 // Check formatting specifiers. NOTE: We're only doing this for the non-size
4771 // call to avoid duplicate diagnostics.
4772 if (!IsSizeCall) {
4773 llvm::SmallBitVector CheckedVarArgs(NumArgs, false);
4774 ArrayRef<const Expr *> Args(TheCall->getArgs(), TheCall->getNumArgs());
4775 bool Success = CheckFormatArguments(
4776 Args, /*HasVAListArg*/ false, FormatIdx, FirstDataArg, FST_OSLog,
4777 VariadicFunction, TheCall->getLocStart(), SourceRange(),
4778 CheckedVarArgs);
4779 if (!Success)
4780 return true;
4781 }
4782
4783 if (IsSizeCall) {
4784 TheCall->setType(Context.getSizeType());
4785 } else {
4786 TheCall->setType(Context.VoidPtrTy);
4787 }
4788 return false;
4789}
4790
Eric Christopher8d0c6212010-04-17 02:26:23 +00004791/// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
4792/// TheCall is a constant expression.
4793bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
4794 llvm::APSInt &Result) {
4795 Expr *Arg = TheCall->getArg(ArgNum);
4796 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
4797 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
4798
4799 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
4800
4801 if (!Arg->isIntegerConstantExpr(Result, Context))
4802 return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
Eric Christopher63448c32010-04-19 18:23:02 +00004803 << FDecl->getDeclName() << Arg->getSourceRange();
Eric Christopher8d0c6212010-04-17 02:26:23 +00004804
Chris Lattnerd545ad12009-09-23 06:06:36 +00004805 return false;
4806}
4807
Richard Sandiford28940af2014-04-16 08:47:51 +00004808/// SemaBuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr
4809/// TheCall is a constant expression in the range [Low, High].
4810bool Sema::SemaBuiltinConstantArgRange(CallExpr *TheCall, int ArgNum,
4811 int Low, int High) {
Eric Christopher8d0c6212010-04-17 02:26:23 +00004812 llvm::APSInt Result;
Douglas Gregor98c3cfc2012-06-29 01:05:22 +00004813
4814 // We can't check the value of a dependent argument.
Richard Sandiford28940af2014-04-16 08:47:51 +00004815 Expr *Arg = TheCall->getArg(ArgNum);
4816 if (Arg->isTypeDependent() || Arg->isValueDependent())
Douglas Gregor98c3cfc2012-06-29 01:05:22 +00004817 return false;
4818
Eric Christopher8d0c6212010-04-17 02:26:23 +00004819 // Check constant-ness first.
Richard Sandiford28940af2014-04-16 08:47:51 +00004820 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
Eric Christopher8d0c6212010-04-17 02:26:23 +00004821 return true;
4822
Richard Sandiford28940af2014-04-16 08:47:51 +00004823 if (Result.getSExtValue() < Low || Result.getSExtValue() > High)
Chris Lattner3b054132008-11-19 05:08:23 +00004824 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Richard Sandiford28940af2014-04-16 08:47:51 +00004825 << Low << High << Arg->getSourceRange();
Daniel Dunbarb0d34c82008-09-03 21:13:56 +00004826
4827 return false;
4828}
4829
Simon Dardis1f90f2d2016-10-19 17:50:52 +00004830/// SemaBuiltinConstantArgMultiple - Handle a check if argument ArgNum of CallExpr
4831/// TheCall is a constant expression is a multiple of Num..
4832bool Sema::SemaBuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum,
4833 unsigned Num) {
4834 llvm::APSInt Result;
4835
4836 // We can't check the value of a dependent argument.
4837 Expr *Arg = TheCall->getArg(ArgNum);
4838 if (Arg->isTypeDependent() || Arg->isValueDependent())
4839 return false;
4840
4841 // Check constant-ness first.
4842 if (SemaBuiltinConstantArg(TheCall, ArgNum, Result))
4843 return true;
4844
4845 if (Result.getSExtValue() % Num != 0)
4846 return Diag(TheCall->getLocStart(), diag::err_argument_not_multiple)
4847 << Num << Arg->getSourceRange();
4848
4849 return false;
4850}
4851
Luke Cheeseman59b2d832015-06-15 17:51:01 +00004852/// SemaBuiltinARMSpecialReg - Handle a check if argument ArgNum of CallExpr
4853/// TheCall is an ARM/AArch64 special register string literal.
4854bool Sema::SemaBuiltinARMSpecialReg(unsigned BuiltinID, CallExpr *TheCall,
4855 int ArgNum, unsigned ExpectedFieldNum,
4856 bool AllowName) {
4857 bool IsARMBuiltin = BuiltinID == ARM::BI__builtin_arm_rsr64 ||
4858 BuiltinID == ARM::BI__builtin_arm_wsr64 ||
4859 BuiltinID == ARM::BI__builtin_arm_rsr ||
4860 BuiltinID == ARM::BI__builtin_arm_rsrp ||
4861 BuiltinID == ARM::BI__builtin_arm_wsr ||
4862 BuiltinID == ARM::BI__builtin_arm_wsrp;
4863 bool IsAArch64Builtin = BuiltinID == AArch64::BI__builtin_arm_rsr64 ||
4864 BuiltinID == AArch64::BI__builtin_arm_wsr64 ||
4865 BuiltinID == AArch64::BI__builtin_arm_rsr ||
4866 BuiltinID == AArch64::BI__builtin_arm_rsrp ||
4867 BuiltinID == AArch64::BI__builtin_arm_wsr ||
4868 BuiltinID == AArch64::BI__builtin_arm_wsrp;
4869 assert((IsARMBuiltin || IsAArch64Builtin) && "Unexpected ARM builtin.");
4870
4871 // We can't check the value of a dependent argument.
4872 Expr *Arg = TheCall->getArg(ArgNum);
4873 if (Arg->isTypeDependent() || Arg->isValueDependent())
4874 return false;
4875
4876 // Check if the argument is a string literal.
4877 if (!isa<StringLiteral>(Arg->IgnoreParenImpCasts()))
4878 return Diag(TheCall->getLocStart(), diag::err_expr_not_string_literal)
4879 << Arg->getSourceRange();
4880
4881 // Check the type of special register given.
4882 StringRef Reg = cast<StringLiteral>(Arg->IgnoreParenImpCasts())->getString();
4883 SmallVector<StringRef, 6> Fields;
4884 Reg.split(Fields, ":");
4885
4886 if (Fields.size() != ExpectedFieldNum && !(AllowName && Fields.size() == 1))
4887 return Diag(TheCall->getLocStart(), diag::err_arm_invalid_specialreg)
4888 << Arg->getSourceRange();
4889
4890 // If the string is the name of a register then we cannot check that it is
4891 // valid here but if the string is of one the forms described in ACLE then we
4892 // can check that the supplied fields are integers and within the valid
4893 // ranges.
4894 if (Fields.size() > 1) {
4895 bool FiveFields = Fields.size() == 5;
4896
4897 bool ValidString = true;
4898 if (IsARMBuiltin) {
4899 ValidString &= Fields[0].startswith_lower("cp") ||
4900 Fields[0].startswith_lower("p");
4901 if (ValidString)
4902 Fields[0] =
4903 Fields[0].drop_front(Fields[0].startswith_lower("cp") ? 2 : 1);
4904
4905 ValidString &= Fields[2].startswith_lower("c");
4906 if (ValidString)
4907 Fields[2] = Fields[2].drop_front(1);
4908
4909 if (FiveFields) {
4910 ValidString &= Fields[3].startswith_lower("c");
4911 if (ValidString)
4912 Fields[3] = Fields[3].drop_front(1);
4913 }
4914 }
4915
4916 SmallVector<int, 5> Ranges;
4917 if (FiveFields)
Oleg Ranevskyy85d93a82016-11-18 21:00:08 +00004918 Ranges.append({IsAArch64Builtin ? 1 : 15, 7, 15, 15, 7});
Luke Cheeseman59b2d832015-06-15 17:51:01 +00004919 else
4920 Ranges.append({15, 7, 15});
4921
4922 for (unsigned i=0; i<Fields.size(); ++i) {
4923 int IntField;
4924 ValidString &= !Fields[i].getAsInteger(10, IntField);
4925 ValidString &= (IntField >= 0 && IntField <= Ranges[i]);
4926 }
4927
4928 if (!ValidString)
4929 return Diag(TheCall->getLocStart(), diag::err_arm_invalid_specialreg)
4930 << Arg->getSourceRange();
Luke Cheeseman59b2d832015-06-15 17:51:01 +00004931 } else if (IsAArch64Builtin && Fields.size() == 1) {
4932 // If the register name is one of those that appear in the condition below
4933 // and the special register builtin being used is one of the write builtins,
4934 // then we require that the argument provided for writing to the register
4935 // is an integer constant expression. This is because it will be lowered to
4936 // an MSR (immediate) instruction, so we need to know the immediate at
4937 // compile time.
4938 if (TheCall->getNumArgs() != 2)
4939 return false;
4940
4941 std::string RegLower = Reg.lower();
4942 if (RegLower != "spsel" && RegLower != "daifset" && RegLower != "daifclr" &&
4943 RegLower != "pan" && RegLower != "uao")
4944 return false;
4945
4946 return SemaBuiltinConstantArgRange(TheCall, 1, 0, 15);
4947 }
4948
4949 return false;
4950}
4951
Eli Friedmanc97d0142009-05-03 06:04:26 +00004952/// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
Joerg Sonnenberger27173282015-03-11 23:46:32 +00004953/// This checks that the target supports __builtin_longjmp and
4954/// that val is a constant 1.
Eli Friedmaneed8ad22009-05-03 04:46:36 +00004955bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
Joerg Sonnenberger27173282015-03-11 23:46:32 +00004956 if (!Context.getTargetInfo().hasSjLjLowering())
4957 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_unsupported)
4958 << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd());
4959
Eli Friedmaneed8ad22009-05-03 04:46:36 +00004960 Expr *Arg = TheCall->getArg(1);
Eric Christopher8d0c6212010-04-17 02:26:23 +00004961 llvm::APSInt Result;
Douglas Gregorc25f7662009-05-19 22:10:17 +00004962
Eric Christopher8d0c6212010-04-17 02:26:23 +00004963 // TODO: This is less than ideal. Overload this to take a value.
4964 if (SemaBuiltinConstantArg(TheCall, 1, Result))
4965 return true;
4966
4967 if (Result != 1)
Eli Friedmaneed8ad22009-05-03 04:46:36 +00004968 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
4969 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
4970
4971 return false;
4972}
4973
Joerg Sonnenberger27173282015-03-11 23:46:32 +00004974/// SemaBuiltinSetjmp - Handle __builtin_setjmp(void *env[5]).
4975/// This checks that the target supports __builtin_setjmp.
4976bool Sema::SemaBuiltinSetjmp(CallExpr *TheCall) {
4977 if (!Context.getTargetInfo().hasSjLjLowering())
4978 return Diag(TheCall->getLocStart(), diag::err_builtin_setjmp_unsupported)
4979 << SourceRange(TheCall->getLocStart(), TheCall->getLocEnd());
4980 return false;
4981}
4982
Richard Smithd7293d72013-08-05 18:49:43 +00004983namespace {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00004984
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00004985class UncoveredArgHandler {
4986 enum { Unknown = -1, AllCovered = -2 };
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00004987
4988 signed FirstUncoveredArg = Unknown;
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00004989 SmallVector<const Expr *, 4> DiagnosticExprs;
4990
4991public:
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00004992 UncoveredArgHandler() = default;
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00004993
4994 bool hasUncoveredArg() const {
4995 return (FirstUncoveredArg >= 0);
4996 }
4997
4998 unsigned getUncoveredArg() const {
4999 assert(hasUncoveredArg() && "no uncovered argument");
5000 return FirstUncoveredArg;
5001 }
5002
5003 void setAllCovered() {
5004 // A string has been found with all arguments covered, so clear out
5005 // the diagnostics.
5006 DiagnosticExprs.clear();
5007 FirstUncoveredArg = AllCovered;
5008 }
5009
5010 void Update(signed NewFirstUncoveredArg, const Expr *StrExpr) {
5011 assert(NewFirstUncoveredArg >= 0 && "Outside range");
5012
5013 // Don't update if a previous string covers all arguments.
5014 if (FirstUncoveredArg == AllCovered)
5015 return;
5016
5017 // UncoveredArgHandler tracks the highest uncovered argument index
5018 // and with it all the strings that match this index.
5019 if (NewFirstUncoveredArg == FirstUncoveredArg)
5020 DiagnosticExprs.push_back(StrExpr);
5021 else if (NewFirstUncoveredArg > FirstUncoveredArg) {
5022 DiagnosticExprs.clear();
5023 DiagnosticExprs.push_back(StrExpr);
5024 FirstUncoveredArg = NewFirstUncoveredArg;
5025 }
5026 }
5027
5028 void Diagnose(Sema &S, bool IsFunctionCall, const Expr *ArgExpr);
5029};
5030
Richard Smithd7293d72013-08-05 18:49:43 +00005031enum StringLiteralCheckType {
5032 SLCT_NotALiteral,
5033 SLCT_UncheckedLiteral,
5034 SLCT_CheckedLiteral
5035};
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005036
5037} // namespace
Richard Smithd7293d72013-08-05 18:49:43 +00005038
Stephen Hines648c3692016-09-16 01:07:04 +00005039static void sumOffsets(llvm::APSInt &Offset, llvm::APSInt Addend,
5040 BinaryOperatorKind BinOpKind,
5041 bool AddendIsRight) {
5042 unsigned BitWidth = Offset.getBitWidth();
5043 unsigned AddendBitWidth = Addend.getBitWidth();
5044 // There might be negative interim results.
5045 if (Addend.isUnsigned()) {
5046 Addend = Addend.zext(++AddendBitWidth);
5047 Addend.setIsSigned(true);
5048 }
5049 // Adjust the bit width of the APSInts.
5050 if (AddendBitWidth > BitWidth) {
5051 Offset = Offset.sext(AddendBitWidth);
5052 BitWidth = AddendBitWidth;
5053 } else if (BitWidth > AddendBitWidth) {
5054 Addend = Addend.sext(BitWidth);
5055 }
5056
5057 bool Ov = false;
5058 llvm::APSInt ResOffset = Offset;
5059 if (BinOpKind == BO_Add)
5060 ResOffset = Offset.sadd_ov(Addend, Ov);
5061 else {
5062 assert(AddendIsRight && BinOpKind == BO_Sub &&
5063 "operator must be add or sub with addend on the right");
5064 ResOffset = Offset.ssub_ov(Addend, Ov);
5065 }
5066
5067 // We add an offset to a pointer here so we should support an offset as big as
5068 // possible.
5069 if (Ov) {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005070 assert(BitWidth <= std::numeric_limits<unsigned>::max() / 2 &&
5071 "index (intermediate) result too big");
Stephen Hinesfec73ad2016-09-16 07:21:24 +00005072 Offset = Offset.sext(2 * BitWidth);
Stephen Hines648c3692016-09-16 01:07:04 +00005073 sumOffsets(Offset, Addend, BinOpKind, AddendIsRight);
5074 return;
5075 }
5076
5077 Offset = ResOffset;
5078}
5079
5080namespace {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005081
Stephen Hines648c3692016-09-16 01:07:04 +00005082// This is a wrapper class around StringLiteral to support offsetted string
5083// literals as format strings. It takes the offset into account when returning
5084// the string and its length or the source locations to display notes correctly.
5085class FormatStringLiteral {
5086 const StringLiteral *FExpr;
5087 int64_t Offset;
5088
5089 public:
5090 FormatStringLiteral(const StringLiteral *fexpr, int64_t Offset = 0)
5091 : FExpr(fexpr), Offset(Offset) {}
5092
5093 StringRef getString() const {
5094 return FExpr->getString().drop_front(Offset);
5095 }
5096
5097 unsigned getByteLength() const {
5098 return FExpr->getByteLength() - getCharByteWidth() * Offset;
5099 }
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005100
Stephen Hines648c3692016-09-16 01:07:04 +00005101 unsigned getLength() const { return FExpr->getLength() - Offset; }
5102 unsigned getCharByteWidth() const { return FExpr->getCharByteWidth(); }
5103
5104 StringLiteral::StringKind getKind() const { return FExpr->getKind(); }
5105
5106 QualType getType() const { return FExpr->getType(); }
5107
5108 bool isAscii() const { return FExpr->isAscii(); }
5109 bool isWide() const { return FExpr->isWide(); }
5110 bool isUTF8() const { return FExpr->isUTF8(); }
5111 bool isUTF16() const { return FExpr->isUTF16(); }
5112 bool isUTF32() const { return FExpr->isUTF32(); }
5113 bool isPascal() const { return FExpr->isPascal(); }
5114
5115 SourceLocation getLocationOfByte(
5116 unsigned ByteNo, const SourceManager &SM, const LangOptions &Features,
5117 const TargetInfo &Target, unsigned *StartToken = nullptr,
5118 unsigned *StartTokenByteOffset = nullptr) const {
5119 return FExpr->getLocationOfByte(ByteNo + Offset, SM, Features, Target,
5120 StartToken, StartTokenByteOffset);
5121 }
5122
5123 SourceLocation getLocStart() const LLVM_READONLY {
5124 return FExpr->getLocStart().getLocWithOffset(Offset);
5125 }
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005126
Stephen Hines648c3692016-09-16 01:07:04 +00005127 SourceLocation getLocEnd() const LLVM_READONLY { return FExpr->getLocEnd(); }
5128};
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005129
5130} // namespace
Stephen Hines648c3692016-09-16 01:07:04 +00005131
5132static void CheckFormatString(Sema &S, const FormatStringLiteral *FExpr,
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00005133 const Expr *OrigFormatExpr,
5134 ArrayRef<const Expr *> Args,
5135 bool HasVAListArg, unsigned format_idx,
5136 unsigned firstDataArg,
5137 Sema::FormatStringType Type,
5138 bool inFunctionCall,
5139 Sema::VariadicCallType CallType,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005140 llvm::SmallBitVector &CheckedVarArgs,
5141 UncoveredArgHandler &UncoveredArg);
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00005142
Richard Smith55ce3522012-06-25 20:30:08 +00005143// Determine if an expression is a string literal or constant string.
5144// If this function returns false on the arguments to a function expecting a
5145// format string, we will usually need to emit a warning.
5146// True string literals are then checked by CheckFormatString.
Richard Smithd7293d72013-08-05 18:49:43 +00005147static StringLiteralCheckType
5148checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args,
5149 bool HasVAListArg, unsigned format_idx,
5150 unsigned firstDataArg, Sema::FormatStringType Type,
5151 Sema::VariadicCallType CallType, bool InFunctionCall,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005152 llvm::SmallBitVector &CheckedVarArgs,
Stephen Hines648c3692016-09-16 01:07:04 +00005153 UncoveredArgHandler &UncoveredArg,
5154 llvm::APSInt Offset) {
Ted Kremenek808829352010-09-09 03:51:39 +00005155 tryAgain:
Stephen Hines648c3692016-09-16 01:07:04 +00005156 assert(Offset.isSigned() && "invalid offset");
5157
Douglas Gregorc25f7662009-05-19 22:10:17 +00005158 if (E->isTypeDependent() || E->isValueDependent())
Richard Smith55ce3522012-06-25 20:30:08 +00005159 return SLCT_NotALiteral;
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005160
Jean-Daniel Dupas028573e72012-01-30 08:46:47 +00005161 E = E->IgnoreParenCasts();
Peter Collingbourne91147592011-04-15 00:35:48 +00005162
Richard Smithd7293d72013-08-05 18:49:43 +00005163 if (E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
David Blaikie59fe3f82012-02-10 21:07:25 +00005164 // Technically -Wformat-nonliteral does not warn about this case.
5165 // The behavior of printf and friends in this case is implementation
5166 // dependent. Ideally if the format string cannot be null then
5167 // it should have a 'nonnull' attribute in the function prototype.
Richard Smithd7293d72013-08-05 18:49:43 +00005168 return SLCT_UncheckedLiteral;
David Blaikie59fe3f82012-02-10 21:07:25 +00005169
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005170 switch (E->getStmtClass()) {
John McCallc07a0c72011-02-17 10:25:35 +00005171 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005172 case Stmt::ConditionalOperatorClass: {
Richard Smith55ce3522012-06-25 20:30:08 +00005173 // The expression is a literal if both sub-expressions were, and it was
5174 // completely checked only if both sub-expressions were checked.
5175 const AbstractConditionalOperator *C =
5176 cast<AbstractConditionalOperator>(E);
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005177
5178 // Determine whether it is necessary to check both sub-expressions, for
5179 // example, because the condition expression is a constant that can be
5180 // evaluated at compile time.
5181 bool CheckLeft = true, CheckRight = true;
5182
5183 bool Cond;
5184 if (C->getCond()->EvaluateAsBooleanCondition(Cond, S.getASTContext())) {
5185 if (Cond)
5186 CheckRight = false;
5187 else
5188 CheckLeft = false;
5189 }
5190
Stephen Hines648c3692016-09-16 01:07:04 +00005191 // We need to maintain the offsets for the right and the left hand side
5192 // separately to check if every possible indexed expression is a valid
5193 // string literal. They might have different offsets for different string
5194 // literals in the end.
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005195 StringLiteralCheckType Left;
5196 if (!CheckLeft)
5197 Left = SLCT_UncheckedLiteral;
5198 else {
5199 Left = checkFormatStringExpr(S, C->getTrueExpr(), Args,
5200 HasVAListArg, format_idx, firstDataArg,
5201 Type, CallType, InFunctionCall,
Stephen Hines648c3692016-09-16 01:07:04 +00005202 CheckedVarArgs, UncoveredArg, Offset);
5203 if (Left == SLCT_NotALiteral || !CheckRight) {
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005204 return Left;
Stephen Hines648c3692016-09-16 01:07:04 +00005205 }
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005206 }
5207
Richard Smith55ce3522012-06-25 20:30:08 +00005208 StringLiteralCheckType Right =
Richard Smithd7293d72013-08-05 18:49:43 +00005209 checkFormatStringExpr(S, C->getFalseExpr(), Args,
Richard Smith55ce3522012-06-25 20:30:08 +00005210 HasVAListArg, format_idx, firstDataArg,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005211 Type, CallType, InFunctionCall, CheckedVarArgs,
Stephen Hines648c3692016-09-16 01:07:04 +00005212 UncoveredArg, Offset);
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005213
5214 return (CheckLeft && Left < Right) ? Left : Right;
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005215 }
5216
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005217 case Stmt::ImplicitCastExprClass:
Ted Kremenek808829352010-09-09 03:51:39 +00005218 E = cast<ImplicitCastExpr>(E)->getSubExpr();
5219 goto tryAgain;
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005220
John McCallc07a0c72011-02-17 10:25:35 +00005221 case Stmt::OpaqueValueExprClass:
5222 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
5223 E = src;
5224 goto tryAgain;
5225 }
Richard Smith55ce3522012-06-25 20:30:08 +00005226 return SLCT_NotALiteral;
John McCallc07a0c72011-02-17 10:25:35 +00005227
Ted Kremeneka8890832011-02-24 23:03:04 +00005228 case Stmt::PredefinedExprClass:
5229 // While __func__, etc., are technically not string literals, they
5230 // cannot contain format specifiers and thus are not a security
5231 // liability.
Richard Smith55ce3522012-06-25 20:30:08 +00005232 return SLCT_UncheckedLiteral;
Ted Kremeneka8890832011-02-24 23:03:04 +00005233
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005234 case Stmt::DeclRefExprClass: {
5235 const DeclRefExpr *DR = cast<DeclRefExpr>(E);
Mike Stump11289f42009-09-09 15:08:12 +00005236
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005237 // As an exception, do not flag errors for variables binding to
5238 // const string literals.
5239 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
5240 bool isConstant = false;
5241 QualType T = DR->getType();
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005242
Richard Smithd7293d72013-08-05 18:49:43 +00005243 if (const ArrayType *AT = S.Context.getAsArrayType(T)) {
5244 isConstant = AT->getElementType().isConstant(S.Context);
Mike Stump12b8ce12009-08-04 21:02:39 +00005245 } else if (const PointerType *PT = T->getAs<PointerType>()) {
Richard Smithd7293d72013-08-05 18:49:43 +00005246 isConstant = T.isConstant(S.Context) &&
5247 PT->getPointeeType().isConstant(S.Context);
Jean-Daniel Dupasd5f7ef42012-01-25 10:35:33 +00005248 } else if (T->isObjCObjectPointerType()) {
5249 // In ObjC, there is usually no "const ObjectPointer" type,
5250 // so don't check if the pointee type is constant.
Richard Smithd7293d72013-08-05 18:49:43 +00005251 isConstant = T.isConstant(S.Context);
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005252 }
Mike Stump11289f42009-09-09 15:08:12 +00005253
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005254 if (isConstant) {
Matt Beaumont-Gayd8735082012-05-11 22:10:59 +00005255 if (const Expr *Init = VD->getAnyInitializer()) {
5256 // Look through initializers like const char c[] = { "foo" }
5257 if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) {
5258 if (InitList->isStringLiteralInit())
5259 Init = InitList->getInit(0)->IgnoreParenImpCasts();
5260 }
Richard Smithd7293d72013-08-05 18:49:43 +00005261 return checkFormatStringExpr(S, Init, Args,
Richard Smith55ce3522012-06-25 20:30:08 +00005262 HasVAListArg, format_idx,
Jordan Rose3e0ec582012-07-19 18:10:23 +00005263 firstDataArg, Type, CallType,
Stephen Hines648c3692016-09-16 01:07:04 +00005264 /*InFunctionCall*/ false, CheckedVarArgs,
5265 UncoveredArg, Offset);
Matt Beaumont-Gayd8735082012-05-11 22:10:59 +00005266 }
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005267 }
Mike Stump11289f42009-09-09 15:08:12 +00005268
Anders Carlssonb012ca92009-06-28 19:55:58 +00005269 // For vprintf* functions (i.e., HasVAListArg==true), we add a
5270 // special check to see if the format string is a function parameter
5271 // of the function calling the printf function. If the function
5272 // has an attribute indicating it is a printf-like function, then we
5273 // should suppress warnings concerning non-literals being used in a call
5274 // to a vprintf function. For example:
5275 //
5276 // void
5277 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
5278 // va_list ap;
5279 // va_start(ap, fmt);
5280 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt".
5281 // ...
Richard Smithd7293d72013-08-05 18:49:43 +00005282 // }
Jean-Daniel Dupas58dab682012-02-21 20:00:53 +00005283 if (HasVAListArg) {
5284 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) {
5285 if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) {
5286 int PVIndex = PV->getFunctionScopeIndex() + 1;
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00005287 for (const auto *PVFormat : ND->specific_attrs<FormatAttr>()) {
Jean-Daniel Dupas58dab682012-02-21 20:00:53 +00005288 // adjust for implicit parameter
5289 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
5290 if (MD->isInstance())
5291 ++PVIndex;
5292 // We also check if the formats are compatible.
5293 // We can't pass a 'scanf' string to a 'printf' function.
5294 if (PVIndex == PVFormat->getFormatIdx() &&
Richard Smithd7293d72013-08-05 18:49:43 +00005295 Type == S.GetFormatStringType(PVFormat))
Richard Smith55ce3522012-06-25 20:30:08 +00005296 return SLCT_UncheckedLiteral;
Jean-Daniel Dupas58dab682012-02-21 20:00:53 +00005297 }
5298 }
5299 }
5300 }
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005301 }
Mike Stump11289f42009-09-09 15:08:12 +00005302
Richard Smith55ce3522012-06-25 20:30:08 +00005303 return SLCT_NotALiteral;
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005304 }
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005305
Jean-Daniel Dupas6255bd12012-02-07 19:01:42 +00005306 case Stmt::CallExprClass:
5307 case Stmt::CXXMemberCallExprClass: {
Anders Carlssonf0a7f3b2009-06-27 04:05:33 +00005308 const CallExpr *CE = cast<CallExpr>(E);
Jean-Daniel Dupas6255bd12012-02-07 19:01:42 +00005309 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
5310 if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
Joel E. Denny81508102018-03-13 14:51:22 +00005311 const Expr *Arg = CE->getArg(FA->getFormatIdx().getASTIndex());
Richard Smithd7293d72013-08-05 18:49:43 +00005312 return checkFormatStringExpr(S, Arg, Args,
Richard Smith55ce3522012-06-25 20:30:08 +00005313 HasVAListArg, format_idx, firstDataArg,
Richard Smithd7293d72013-08-05 18:49:43 +00005314 Type, CallType, InFunctionCall,
Stephen Hines648c3692016-09-16 01:07:04 +00005315 CheckedVarArgs, UncoveredArg, Offset);
Jordan Rose97c6f2b2012-06-04 23:52:23 +00005316 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
5317 unsigned BuiltinID = FD->getBuiltinID();
5318 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
5319 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) {
5320 const Expr *Arg = CE->getArg(0);
Richard Smithd7293d72013-08-05 18:49:43 +00005321 return checkFormatStringExpr(S, Arg, Args,
Richard Smith55ce3522012-06-25 20:30:08 +00005322 HasVAListArg, format_idx,
Jordan Rose3e0ec582012-07-19 18:10:23 +00005323 firstDataArg, Type, CallType,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005324 InFunctionCall, CheckedVarArgs,
Stephen Hines648c3692016-09-16 01:07:04 +00005325 UncoveredArg, Offset);
Jordan Rose97c6f2b2012-06-04 23:52:23 +00005326 }
Anders Carlssonf0a7f3b2009-06-27 04:05:33 +00005327 }
5328 }
Mike Stump11289f42009-09-09 15:08:12 +00005329
Richard Smith55ce3522012-06-25 20:30:08 +00005330 return SLCT_NotALiteral;
Anders Carlssonf0a7f3b2009-06-27 04:05:33 +00005331 }
Alex Lorenzd9007142016-10-24 09:42:34 +00005332 case Stmt::ObjCMessageExprClass: {
5333 const auto *ME = cast<ObjCMessageExpr>(E);
5334 if (const auto *ND = ME->getMethodDecl()) {
5335 if (const auto *FA = ND->getAttr<FormatArgAttr>()) {
Joel E. Denny81508102018-03-13 14:51:22 +00005336 const Expr *Arg = ME->getArg(FA->getFormatIdx().getASTIndex());
Alex Lorenzd9007142016-10-24 09:42:34 +00005337 return checkFormatStringExpr(
5338 S, Arg, Args, HasVAListArg, format_idx, firstDataArg, Type,
5339 CallType, InFunctionCall, CheckedVarArgs, UncoveredArg, Offset);
5340 }
5341 }
5342
5343 return SLCT_NotALiteral;
5344 }
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005345 case Stmt::ObjCStringLiteralClass:
5346 case Stmt::StringLiteralClass: {
Craig Topperc3ec1492014-05-26 06:22:03 +00005347 const StringLiteral *StrE = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00005348
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005349 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005350 StrE = ObjCFExpr->getString();
5351 else
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005352 StrE = cast<StringLiteral>(E);
Mike Stump11289f42009-09-09 15:08:12 +00005353
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005354 if (StrE) {
Stephen Hines648c3692016-09-16 01:07:04 +00005355 if (Offset.isNegative() || Offset > StrE->getLength()) {
5356 // TODO: It would be better to have an explicit warning for out of
5357 // bounds literals.
5358 return SLCT_NotALiteral;
5359 }
5360 FormatStringLiteral FStr(StrE, Offset.sextOrTrunc(64).getSExtValue());
5361 CheckFormatString(S, &FStr, E, Args, HasVAListArg, format_idx,
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00005362 firstDataArg, Type, InFunctionCall, CallType,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005363 CheckedVarArgs, UncoveredArg);
Richard Smith55ce3522012-06-25 20:30:08 +00005364 return SLCT_CheckedLiteral;
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005365 }
Mike Stump11289f42009-09-09 15:08:12 +00005366
Richard Smith55ce3522012-06-25 20:30:08 +00005367 return SLCT_NotALiteral;
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005368 }
Stephen Hines648c3692016-09-16 01:07:04 +00005369 case Stmt::BinaryOperatorClass: {
5370 llvm::APSInt LResult;
5371 llvm::APSInt RResult;
5372
5373 const BinaryOperator *BinOp = cast<BinaryOperator>(E);
5374
5375 // A string literal + an int offset is still a string literal.
5376 if (BinOp->isAdditiveOp()) {
5377 bool LIsInt = BinOp->getLHS()->EvaluateAsInt(LResult, S.Context);
5378 bool RIsInt = BinOp->getRHS()->EvaluateAsInt(RResult, S.Context);
5379
5380 if (LIsInt != RIsInt) {
5381 BinaryOperatorKind BinOpKind = BinOp->getOpcode();
5382
5383 if (LIsInt) {
5384 if (BinOpKind == BO_Add) {
5385 sumOffsets(Offset, LResult, BinOpKind, RIsInt);
5386 E = BinOp->getRHS();
5387 goto tryAgain;
5388 }
5389 } else {
5390 sumOffsets(Offset, RResult, BinOpKind, RIsInt);
5391 E = BinOp->getLHS();
5392 goto tryAgain;
5393 }
5394 }
Stephen Hines648c3692016-09-16 01:07:04 +00005395 }
George Burgess IVd273aab2016-09-22 00:00:26 +00005396
5397 return SLCT_NotALiteral;
Stephen Hines648c3692016-09-16 01:07:04 +00005398 }
5399 case Stmt::UnaryOperatorClass: {
5400 const UnaryOperator *UnaOp = cast<UnaryOperator>(E);
5401 auto ASE = dyn_cast<ArraySubscriptExpr>(UnaOp->getSubExpr());
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005402 if (UnaOp->getOpcode() == UO_AddrOf && ASE) {
Stephen Hines648c3692016-09-16 01:07:04 +00005403 llvm::APSInt IndexResult;
5404 if (ASE->getRHS()->EvaluateAsInt(IndexResult, S.Context)) {
5405 sumOffsets(Offset, IndexResult, BO_Add, /*RHS is int*/ true);
5406 E = ASE->getBase();
5407 goto tryAgain;
5408 }
5409 }
5410
5411 return SLCT_NotALiteral;
5412 }
Mike Stump11289f42009-09-09 15:08:12 +00005413
Ted Kremenekdfd72c22009-03-20 21:35:28 +00005414 default:
Richard Smith55ce3522012-06-25 20:30:08 +00005415 return SLCT_NotALiteral;
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005416 }
5417}
5418
Jean-Daniel Dupas028573e72012-01-30 08:46:47 +00005419Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
Aaron Ballmanf58070b2013-09-03 21:02:22 +00005420 return llvm::StringSwitch<FormatStringType>(Format->getType()->getName())
Mehdi Amini06d367c2016-10-24 20:39:34 +00005421 .Case("scanf", FST_Scanf)
5422 .Cases("printf", "printf0", FST_Printf)
5423 .Cases("NSString", "CFString", FST_NSString)
5424 .Case("strftime", FST_Strftime)
5425 .Case("strfmon", FST_Strfmon)
5426 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
5427 .Case("freebsd_kprintf", FST_FreeBSDKPrintf)
5428 .Case("os_trace", FST_OSLog)
5429 .Case("os_log", FST_OSLog)
5430 .Default(FST_Unknown);
Jean-Daniel Dupas028573e72012-01-30 08:46:47 +00005431}
5432
Jordan Rose3e0ec582012-07-19 18:10:23 +00005433/// CheckFormatArguments - Check calls to printf and scanf (and similar
Ted Kremenek02087932010-07-16 02:11:22 +00005434/// functions) for correct use of format strings.
Richard Smith55ce3522012-06-25 20:30:08 +00005435/// Returns true if a format string has been fully checked.
Dmitri Gribenko765396f2013-01-13 20:46:02 +00005436bool Sema::CheckFormatArguments(const FormatAttr *Format,
5437 ArrayRef<const Expr *> Args,
5438 bool IsCXXMember,
Jordan Rose3e0ec582012-07-19 18:10:23 +00005439 VariadicCallType CallType,
Richard Smithd7293d72013-08-05 18:49:43 +00005440 SourceLocation Loc, SourceRange Range,
5441 llvm::SmallBitVector &CheckedVarArgs) {
Richard Smith55ce3522012-06-25 20:30:08 +00005442 FormatStringInfo FSI;
5443 if (getFormatStringInfo(Format, IsCXXMember, &FSI))
Dmitri Gribenko765396f2013-01-13 20:46:02 +00005444 return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx,
Richard Smith55ce3522012-06-25 20:30:08 +00005445 FSI.FirstDataArg, GetFormatStringType(Format),
Richard Smithd7293d72013-08-05 18:49:43 +00005446 CallType, Loc, Range, CheckedVarArgs);
Richard Smith55ce3522012-06-25 20:30:08 +00005447 return false;
Jean-Daniel Dupas0ae6e672012-01-17 20:03:31 +00005448}
Sebastian Redl6eedcc12009-11-17 18:02:24 +00005449
Dmitri Gribenko765396f2013-01-13 20:46:02 +00005450bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
Jean-Daniel Dupas028573e72012-01-30 08:46:47 +00005451 bool HasVAListArg, unsigned format_idx,
5452 unsigned firstDataArg, FormatStringType Type,
Jordan Rose3e0ec582012-07-19 18:10:23 +00005453 VariadicCallType CallType,
Richard Smithd7293d72013-08-05 18:49:43 +00005454 SourceLocation Loc, SourceRange Range,
5455 llvm::SmallBitVector &CheckedVarArgs) {
Ted Kremenek02087932010-07-16 02:11:22 +00005456 // CHECK: printf/scanf-like function is called with no format string.
Dmitri Gribenko765396f2013-01-13 20:46:02 +00005457 if (format_idx >= Args.size()) {
Jean-Daniel Dupas0ae6e672012-01-17 20:03:31 +00005458 Diag(Loc, diag::warn_missing_format_string) << Range;
Richard Smith55ce3522012-06-25 20:30:08 +00005459 return false;
Ted Kremeneke68f1aa2007-08-14 17:39:48 +00005460 }
Mike Stump11289f42009-09-09 15:08:12 +00005461
Jean-Daniel Dupas0ae6e672012-01-17 20:03:31 +00005462 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
Mike Stump11289f42009-09-09 15:08:12 +00005463
Chris Lattnerb87b1b32007-08-10 20:18:51 +00005464 // CHECK: format string is not a string literal.
Mike Stump11289f42009-09-09 15:08:12 +00005465 //
Ted Kremeneke68f1aa2007-08-14 17:39:48 +00005466 // Dynamically generated format strings are difficult to
5467 // automatically vet at compile time. Requiring that format strings
5468 // are string literals: (1) permits the checking of format strings by
5469 // the compiler and thereby (2) can practically remove the source of
5470 // many format string exploits.
Ted Kremenek34f664d2008-06-16 18:00:42 +00005471
Mike Stump11289f42009-09-09 15:08:12 +00005472 // Format string can be either ObjC string (e.g. @"%d") or
Ted Kremenek34f664d2008-06-16 18:00:42 +00005473 // C string (e.g. "%d")
Mike Stump11289f42009-09-09 15:08:12 +00005474 // ObjC string uses the same format specifiers as C string, so we can use
Ted Kremenek34f664d2008-06-16 18:00:42 +00005475 // the same format string checking logic for both ObjC and C strings.
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005476 UncoveredArgHandler UncoveredArg;
Richard Smith55ce3522012-06-25 20:30:08 +00005477 StringLiteralCheckType CT =
Richard Smithd7293d72013-08-05 18:49:43 +00005478 checkFormatStringExpr(*this, OrigFormatExpr, Args, HasVAListArg,
5479 format_idx, firstDataArg, Type, CallType,
Stephen Hines648c3692016-09-16 01:07:04 +00005480 /*IsFunctionCall*/ true, CheckedVarArgs,
5481 UncoveredArg,
5482 /*no string offset*/ llvm::APSInt(64, false) = 0);
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005483
5484 // Generate a diagnostic where an uncovered argument is detected.
5485 if (UncoveredArg.hasUncoveredArg()) {
5486 unsigned ArgIdx = UncoveredArg.getUncoveredArg() + firstDataArg;
5487 assert(ArgIdx < Args.size() && "ArgIdx outside bounds");
5488 UncoveredArg.Diagnose(*this, /*IsFunctionCall*/true, Args[ArgIdx]);
5489 }
5490
Richard Smith55ce3522012-06-25 20:30:08 +00005491 if (CT != SLCT_NotALiteral)
5492 // Literal format string found, check done!
5493 return CT == SLCT_CheckedLiteral;
Ted Kremenek34f664d2008-06-16 18:00:42 +00005494
Jean-Daniel Dupas6567f482012-02-07 23:10:53 +00005495 // Strftime is particular as it always uses a single 'time' argument,
5496 // so it is safe to pass a non-literal string.
5497 if (Type == FST_Strftime)
Richard Smith55ce3522012-06-25 20:30:08 +00005498 return false;
Jean-Daniel Dupas6567f482012-02-07 23:10:53 +00005499
Jean-Daniel Dupas537aa1a2012-01-30 19:46:17 +00005500 // Do not emit diag when the string param is a macro expansion and the
5501 // format is either NSString or CFString. This is a hack to prevent
5502 // diag when using the NSLocalizedString and CFCopyLocalizedString macros
5503 // which are usually used in place of NS and CF string literals.
Bob Wilsoncf2cf0d2016-03-11 21:55:37 +00005504 SourceLocation FormatLoc = Args[format_idx]->getLocStart();
5505 if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc))
Richard Smith55ce3522012-06-25 20:30:08 +00005506 return false;
Jean-Daniel Dupas537aa1a2012-01-30 19:46:17 +00005507
Chris Lattnercc5d1c22009-04-29 04:59:47 +00005508 // If there are no arguments specified, warn with -Wformat-security, otherwise
5509 // warn only with -Wformat-nonliteral.
Bob Wilsoncf2cf0d2016-03-11 21:55:37 +00005510 if (Args.size() == firstDataArg) {
Bob Wilson57819fc2016-03-15 20:56:38 +00005511 Diag(FormatLoc, diag::warn_format_nonliteral_noargs)
5512 << OrigFormatExpr->getSourceRange();
Bob Wilsoncf2cf0d2016-03-11 21:55:37 +00005513 switch (Type) {
5514 default:
Bob Wilsoncf2cf0d2016-03-11 21:55:37 +00005515 break;
5516 case FST_Kprintf:
5517 case FST_FreeBSDKPrintf:
5518 case FST_Printf:
Bob Wilson57819fc2016-03-15 20:56:38 +00005519 Diag(FormatLoc, diag::note_format_security_fixit)
5520 << FixItHint::CreateInsertion(FormatLoc, "\"%s\", ");
Bob Wilsoncf2cf0d2016-03-11 21:55:37 +00005521 break;
5522 case FST_NSString:
Bob Wilson57819fc2016-03-15 20:56:38 +00005523 Diag(FormatLoc, diag::note_format_security_fixit)
5524 << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", ");
Bob Wilsoncf2cf0d2016-03-11 21:55:37 +00005525 break;
5526 }
5527 } else {
5528 Diag(FormatLoc, diag::warn_format_nonliteral)
Chris Lattnercc5d1c22009-04-29 04:59:47 +00005529 << OrigFormatExpr->getSourceRange();
Bob Wilsoncf2cf0d2016-03-11 21:55:37 +00005530 }
Richard Smith55ce3522012-06-25 20:30:08 +00005531 return false;
Ted Kremenek6dfeb552009-01-12 23:09:09 +00005532}
Ted Kremeneke68f1aa2007-08-14 17:39:48 +00005533
Ted Kremenekab278de2010-01-28 23:39:18 +00005534namespace {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005535
Ted Kremenek02087932010-07-16 02:11:22 +00005536class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
5537protected:
Ted Kremenekab278de2010-01-28 23:39:18 +00005538 Sema &S;
Stephen Hines648c3692016-09-16 01:07:04 +00005539 const FormatStringLiteral *FExpr;
Ted Kremenekab278de2010-01-28 23:39:18 +00005540 const Expr *OrigFormatExpr;
Mehdi Amini06d367c2016-10-24 20:39:34 +00005541 const Sema::FormatStringType FSType;
Ted Kremenek4d745dd2010-03-25 03:59:12 +00005542 const unsigned FirstDataArg;
Ted Kremenekab278de2010-01-28 23:39:18 +00005543 const unsigned NumDataArgs;
Ted Kremenekab278de2010-01-28 23:39:18 +00005544 const char *Beg; // Start of format string.
Ted Kremenek5739de72010-01-29 01:06:55 +00005545 const bool HasVAListArg;
Dmitri Gribenko765396f2013-01-13 20:46:02 +00005546 ArrayRef<const Expr *> Args;
Ted Kremenek5739de72010-01-29 01:06:55 +00005547 unsigned FormatIdx;
Richard Smithd7293d72013-08-05 18:49:43 +00005548 llvm::SmallBitVector CoveredArgs;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005549 bool usesPositionalArgs = false;
5550 bool atFirstArg = true;
Richard Trieu03cf7b72011-10-28 00:41:25 +00005551 bool inFunctionCall;
Jordan Rose3e0ec582012-07-19 18:10:23 +00005552 Sema::VariadicCallType CallType;
Richard Smithd7293d72013-08-05 18:49:43 +00005553 llvm::SmallBitVector &CheckedVarArgs;
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005554 UncoveredArgHandler &UncoveredArg;
Eugene Zelenko1ced5092016-02-12 22:53:10 +00005555
Ted Kremenekc8b188d2010-02-16 01:46:59 +00005556public:
Stephen Hines648c3692016-09-16 01:07:04 +00005557 CheckFormatHandler(Sema &s, const FormatStringLiteral *fexpr,
Mehdi Amini06d367c2016-10-24 20:39:34 +00005558 const Expr *origFormatExpr,
5559 const Sema::FormatStringType type, unsigned firstDataArg,
Jordan Rose97c6f2b2012-06-04 23:52:23 +00005560 unsigned numDataArgs, const char *beg, bool hasVAListArg,
Mehdi Amini06d367c2016-10-24 20:39:34 +00005561 ArrayRef<const Expr *> Args, unsigned formatIdx,
5562 bool inFunctionCall, Sema::VariadicCallType callType,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005563 llvm::SmallBitVector &CheckedVarArgs,
5564 UncoveredArgHandler &UncoveredArg)
Mehdi Amini06d367c2016-10-24 20:39:34 +00005565 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), FSType(type),
5566 FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), Beg(beg),
5567 HasVAListArg(hasVAListArg), Args(Args), FormatIdx(formatIdx),
Mehdi Amini06d367c2016-10-24 20:39:34 +00005568 inFunctionCall(inFunctionCall), CallType(callType),
5569 CheckedVarArgs(CheckedVarArgs), UncoveredArg(UncoveredArg) {
Richard Smithd7293d72013-08-05 18:49:43 +00005570 CoveredArgs.resize(numDataArgs);
5571 CoveredArgs.reset();
5572 }
Ted Kremenekc8b188d2010-02-16 01:46:59 +00005573
Ted Kremenek019d2242010-01-29 01:50:07 +00005574 void DoneProcessing();
Ted Kremenekc8b188d2010-02-16 01:46:59 +00005575
Ted Kremenek02087932010-07-16 02:11:22 +00005576 void HandleIncompleteSpecifier(const char *startSpecifier,
Craig Toppere14c0f82014-03-12 04:55:44 +00005577 unsigned specifierLen) override;
Hans Wennborgc9dd9462012-02-22 10:17:01 +00005578
Jordan Rose92303592012-09-08 04:00:03 +00005579 void HandleInvalidLengthModifier(
Craig Toppere14c0f82014-03-12 04:55:44 +00005580 const analyze_format_string::FormatSpecifier &FS,
5581 const analyze_format_string::ConversionSpecifier &CS,
5582 const char *startSpecifier, unsigned specifierLen,
5583 unsigned DiagID);
Jordan Rose92303592012-09-08 04:00:03 +00005584
Hans Wennborgc9dd9462012-02-22 10:17:01 +00005585 void HandleNonStandardLengthModifier(
Craig Toppere14c0f82014-03-12 04:55:44 +00005586 const analyze_format_string::FormatSpecifier &FS,
5587 const char *startSpecifier, unsigned specifierLen);
Hans Wennborgc9dd9462012-02-22 10:17:01 +00005588
5589 void HandleNonStandardConversionSpecifier(
Craig Toppere14c0f82014-03-12 04:55:44 +00005590 const analyze_format_string::ConversionSpecifier &CS,
5591 const char *startSpecifier, unsigned specifierLen);
Hans Wennborgc9dd9462012-02-22 10:17:01 +00005592
Craig Toppere14c0f82014-03-12 04:55:44 +00005593 void HandlePosition(const char *startPos, unsigned posLen) override;
Hans Wennborgaa8c61c2012-03-09 10:10:54 +00005594
Craig Toppere14c0f82014-03-12 04:55:44 +00005595 void HandleInvalidPosition(const char *startSpecifier,
5596 unsigned specifierLen,
5597 analyze_format_string::PositionContext p) override;
Ted Kremenekd1668192010-02-27 01:41:03 +00005598
Craig Toppere14c0f82014-03-12 04:55:44 +00005599 void HandleZeroPosition(const char *startPos, unsigned posLen) override;
Ted Kremenekd1668192010-02-27 01:41:03 +00005600
Craig Toppere14c0f82014-03-12 04:55:44 +00005601 void HandleNullChar(const char *nullCharacter) override;
Ted Kremenekc8b188d2010-02-16 01:46:59 +00005602
Richard Trieu03cf7b72011-10-28 00:41:25 +00005603 template <typename Range>
Benjamin Kramer7320b992016-06-15 14:20:56 +00005604 static void
5605 EmitFormatDiagnostic(Sema &S, bool inFunctionCall, const Expr *ArgumentExpr,
5606 const PartialDiagnostic &PDiag, SourceLocation StringLoc,
5607 bool IsStringLocation, Range StringRange,
5608 ArrayRef<FixItHint> Fixit = None);
Richard Trieu03cf7b72011-10-28 00:41:25 +00005609
Ted Kremenek02087932010-07-16 02:11:22 +00005610protected:
Ted Kremenekce815422010-07-19 21:25:57 +00005611 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
5612 const char *startSpec,
5613 unsigned specifierLen,
5614 const char *csStart, unsigned csLen);
Richard Trieu03cf7b72011-10-28 00:41:25 +00005615
5616 void HandlePositionalNonpositionalArgs(SourceLocation Loc,
5617 const char *startSpec,
5618 unsigned specifierLen);
Ted Kremenekce815422010-07-19 21:25:57 +00005619
Ted Kremenek8d9842d2010-01-29 20:55:36 +00005620 SourceRange getFormatStringRange();
Ted Kremenek02087932010-07-16 02:11:22 +00005621 CharSourceRange getSpecifierRange(const char *startSpecifier,
5622 unsigned specifierLen);
Ted Kremenekab278de2010-01-28 23:39:18 +00005623 SourceLocation getLocationOfByte(const char *x);
Ted Kremenekc8b188d2010-02-16 01:46:59 +00005624
Ted Kremenek5739de72010-01-29 01:06:55 +00005625 const Expr *getDataArg(unsigned i) const;
Ted Kremenek6adb7e32010-07-26 19:45:42 +00005626
5627 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
5628 const analyze_format_string::ConversionSpecifier &CS,
5629 const char *startSpecifier, unsigned specifierLen,
5630 unsigned argIndex);
Richard Trieu03cf7b72011-10-28 00:41:25 +00005631
5632 template <typename Range>
5633 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
5634 bool IsStringLocation, Range StringRange,
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00005635 ArrayRef<FixItHint> Fixit = None);
Ted Kremenekab278de2010-01-28 23:39:18 +00005636};
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005637
5638} // namespace
Ted Kremenekab278de2010-01-28 23:39:18 +00005639
Ted Kremenek02087932010-07-16 02:11:22 +00005640SourceRange CheckFormatHandler::getFormatStringRange() {
Ted Kremenekab278de2010-01-28 23:39:18 +00005641 return OrigFormatExpr->getSourceRange();
5642}
5643
Ted Kremenek02087932010-07-16 02:11:22 +00005644CharSourceRange CheckFormatHandler::
5645getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
Tom Care3f272b82010-06-21 21:21:01 +00005646 SourceLocation Start = getLocationOfByte(startSpecifier);
5647 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1);
5648
5649 // Advance the end SourceLocation by one due to half-open ranges.
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00005650 End = End.getLocWithOffset(1);
Tom Care3f272b82010-06-21 21:21:01 +00005651
5652 return CharSourceRange::getCharRange(Start, End);
Ted Kremenek8d9842d2010-01-29 20:55:36 +00005653}
5654
Ted Kremenek02087932010-07-16 02:11:22 +00005655SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
Stephen Hines648c3692016-09-16 01:07:04 +00005656 return FExpr->getLocationOfByte(x - Beg, S.getSourceManager(),
5657 S.getLangOpts(), S.Context.getTargetInfo());
Ted Kremenekab278de2010-01-28 23:39:18 +00005658}
5659
Ted Kremenek02087932010-07-16 02:11:22 +00005660void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
5661 unsigned specifierLen){
Richard Trieu03cf7b72011-10-28 00:41:25 +00005662 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
5663 getLocationOfByte(startSpecifier),
5664 /*IsStringLocation*/true,
5665 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenekc22f78d2010-01-29 03:16:21 +00005666}
5667
Jordan Rose92303592012-09-08 04:00:03 +00005668void CheckFormatHandler::HandleInvalidLengthModifier(
5669 const analyze_format_string::FormatSpecifier &FS,
5670 const analyze_format_string::ConversionSpecifier &CS,
Jordan Rose2f9cc042012-09-08 04:00:12 +00005671 const char *startSpecifier, unsigned specifierLen, unsigned DiagID) {
Jordan Rose92303592012-09-08 04:00:03 +00005672 using namespace analyze_format_string;
5673
5674 const LengthModifier &LM = FS.getLengthModifier();
5675 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
5676
5677 // See if we know how to fix this length modifier.
David Blaikie05785d12013-02-20 22:23:23 +00005678 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
Jordan Rose92303592012-09-08 04:00:03 +00005679 if (FixedLM) {
Jordan Rose2f9cc042012-09-08 04:00:12 +00005680 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
Jordan Rose92303592012-09-08 04:00:03 +00005681 getLocationOfByte(LM.getStart()),
5682 /*IsStringLocation*/true,
5683 getSpecifierRange(startSpecifier, specifierLen));
5684
5685 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
5686 << FixedLM->toString()
5687 << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
5688
5689 } else {
Jordan Rose2f9cc042012-09-08 04:00:12 +00005690 FixItHint Hint;
5691 if (DiagID == diag::warn_format_nonsensical_length)
5692 Hint = FixItHint::CreateRemoval(LMRange);
5693
5694 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
Jordan Rose92303592012-09-08 04:00:03 +00005695 getLocationOfByte(LM.getStart()),
5696 /*IsStringLocation*/true,
5697 getSpecifierRange(startSpecifier, specifierLen),
Jordan Rose2f9cc042012-09-08 04:00:12 +00005698 Hint);
Jordan Rose92303592012-09-08 04:00:03 +00005699 }
5700}
5701
Hans Wennborgc9dd9462012-02-22 10:17:01 +00005702void CheckFormatHandler::HandleNonStandardLengthModifier(
Jordan Rose2f9cc042012-09-08 04:00:12 +00005703 const analyze_format_string::FormatSpecifier &FS,
Hans Wennborgc9dd9462012-02-22 10:17:01 +00005704 const char *startSpecifier, unsigned specifierLen) {
Jordan Rose2f9cc042012-09-08 04:00:12 +00005705 using namespace analyze_format_string;
5706
5707 const LengthModifier &LM = FS.getLengthModifier();
5708 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
5709
5710 // See if we know how to fix this length modifier.
David Blaikie05785d12013-02-20 22:23:23 +00005711 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
Jordan Rose2f9cc042012-09-08 04:00:12 +00005712 if (FixedLM) {
5713 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
5714 << LM.toString() << 0,
5715 getLocationOfByte(LM.getStart()),
5716 /*IsStringLocation*/true,
5717 getSpecifierRange(startSpecifier, specifierLen));
5718
5719 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
5720 << FixedLM->toString()
5721 << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
5722
5723 } else {
5724 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
5725 << LM.toString() << 0,
5726 getLocationOfByte(LM.getStart()),
5727 /*IsStringLocation*/true,
5728 getSpecifierRange(startSpecifier, specifierLen));
5729 }
Hans Wennborgc9dd9462012-02-22 10:17:01 +00005730}
5731
5732void CheckFormatHandler::HandleNonStandardConversionSpecifier(
5733 const analyze_format_string::ConversionSpecifier &CS,
5734 const char *startSpecifier, unsigned specifierLen) {
Jordan Rose4c266aa2012-09-13 02:11:15 +00005735 using namespace analyze_format_string;
5736
5737 // See if we know how to fix this conversion specifier.
David Blaikie05785d12013-02-20 22:23:23 +00005738 Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier();
Jordan Rose4c266aa2012-09-13 02:11:15 +00005739 if (FixedCS) {
5740 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
5741 << CS.toString() << /*conversion specifier*/1,
5742 getLocationOfByte(CS.getStart()),
5743 /*IsStringLocation*/true,
5744 getSpecifierRange(startSpecifier, specifierLen));
5745
5746 CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength());
5747 S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier)
5748 << FixedCS->toString()
5749 << FixItHint::CreateReplacement(CSRange, FixedCS->toString());
5750 } else {
5751 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
5752 << CS.toString() << /*conversion specifier*/1,
5753 getLocationOfByte(CS.getStart()),
5754 /*IsStringLocation*/true,
5755 getSpecifierRange(startSpecifier, specifierLen));
5756 }
Hans Wennborgc9dd9462012-02-22 10:17:01 +00005757}
5758
Hans Wennborgaa8c61c2012-03-09 10:10:54 +00005759void CheckFormatHandler::HandlePosition(const char *startPos,
5760 unsigned posLen) {
5761 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
5762 getLocationOfByte(startPos),
5763 /*IsStringLocation*/true,
5764 getSpecifierRange(startPos, posLen));
5765}
5766
Ted Kremenekd1668192010-02-27 01:41:03 +00005767void
Ted Kremenek02087932010-07-16 02:11:22 +00005768CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
5769 analyze_format_string::PositionContext p) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00005770 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier)
5771 << (unsigned) p,
5772 getLocationOfByte(startPos), /*IsStringLocation*/true,
5773 getSpecifierRange(startPos, posLen));
Ted Kremenekd1668192010-02-27 01:41:03 +00005774}
5775
Ted Kremenek02087932010-07-16 02:11:22 +00005776void CheckFormatHandler::HandleZeroPosition(const char *startPos,
Ted Kremenekd1668192010-02-27 01:41:03 +00005777 unsigned posLen) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00005778 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
5779 getLocationOfByte(startPos),
5780 /*IsStringLocation*/true,
5781 getSpecifierRange(startPos, posLen));
Ted Kremenekd1668192010-02-27 01:41:03 +00005782}
5783
Ted Kremenek02087932010-07-16 02:11:22 +00005784void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
Jordan Rose97c6f2b2012-06-04 23:52:23 +00005785 if (!isa<ObjCStringLiteral>(OrigFormatExpr)) {
Ted Kremenek0d5b9ef2011-03-15 21:18:48 +00005786 // The presence of a null character is likely an error.
Richard Trieu03cf7b72011-10-28 00:41:25 +00005787 EmitFormatDiagnostic(
5788 S.PDiag(diag::warn_printf_format_string_contains_null_char),
5789 getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
5790 getFormatStringRange());
Ted Kremenek0d5b9ef2011-03-15 21:18:48 +00005791 }
Ted Kremenek02087932010-07-16 02:11:22 +00005792}
Ted Kremenekc8b188d2010-02-16 01:46:59 +00005793
Jordan Rose58bbe422012-07-19 18:10:08 +00005794// Note that this may return NULL if there was an error parsing or building
5795// one of the argument expressions.
Ted Kremenek02087932010-07-16 02:11:22 +00005796const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
Jean-Daniel Dupas0ae6e672012-01-17 20:03:31 +00005797 return Args[FirstDataArg + i];
Ted Kremenek02087932010-07-16 02:11:22 +00005798}
5799
5800void CheckFormatHandler::DoneProcessing() {
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005801 // Does the number of data arguments exceed the number of
5802 // format conversions in the format string?
Ted Kremenek02087932010-07-16 02:11:22 +00005803 if (!HasVAListArg) {
5804 // Find any arguments that weren't covered.
5805 CoveredArgs.flip();
5806 signed notCoveredArg = CoveredArgs.find_first();
5807 if (notCoveredArg >= 0) {
5808 assert((unsigned)notCoveredArg < NumDataArgs);
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005809 UncoveredArg.Update(notCoveredArg, OrigFormatExpr);
5810 } else {
5811 UncoveredArg.setAllCovered();
Ted Kremenek02087932010-07-16 02:11:22 +00005812 }
5813 }
5814}
5815
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005816void UncoveredArgHandler::Diagnose(Sema &S, bool IsFunctionCall,
5817 const Expr *ArgExpr) {
5818 assert(hasUncoveredArg() && DiagnosticExprs.size() > 0 &&
5819 "Invalid state");
5820
5821 if (!ArgExpr)
5822 return;
5823
5824 SourceLocation Loc = ArgExpr->getLocStart();
5825
5826 if (S.getSourceManager().isInSystemMacro(Loc))
5827 return;
5828
5829 PartialDiagnostic PDiag = S.PDiag(diag::warn_printf_data_arg_not_used);
5830 for (auto E : DiagnosticExprs)
5831 PDiag << E->getSourceRange();
5832
5833 CheckFormatHandler::EmitFormatDiagnostic(
5834 S, IsFunctionCall, DiagnosticExprs[0],
5835 PDiag, Loc, /*IsStringLocation*/false,
5836 DiagnosticExprs[0]->getSourceRange());
5837}
5838
Ted Kremenekce815422010-07-19 21:25:57 +00005839bool
5840CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
5841 SourceLocation Loc,
5842 const char *startSpec,
5843 unsigned specifierLen,
5844 const char *csStart,
5845 unsigned csLen) {
Ted Kremenekce815422010-07-19 21:25:57 +00005846 bool keepGoing = true;
5847 if (argIndex < NumDataArgs) {
5848 // Consider the argument coverered, even though the specifier doesn't
5849 // make sense.
5850 CoveredArgs.set(argIndex);
5851 }
5852 else {
5853 // If argIndex exceeds the number of data arguments we
5854 // don't issue a warning because that is just a cascade of warnings (and
5855 // they may have intended '%%' anyway). We don't want to continue processing
5856 // the format string after this point, however, as we will like just get
5857 // gibberish when trying to match arguments.
5858 keepGoing = false;
5859 }
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +00005860
5861 StringRef Specifier(csStart, csLen);
5862
5863 // If the specifier in non-printable, it could be the first byte of a UTF-8
5864 // sequence. In that case, print the UTF-8 code point. If not, print the byte
5865 // hex value.
5866 std::string CodePointStr;
5867 if (!llvm::sys::locale::isPrint(*csStart)) {
Justin Lebar90910552016-09-30 00:38:45 +00005868 llvm::UTF32 CodePoint;
5869 const llvm::UTF8 **B = reinterpret_cast<const llvm::UTF8 **>(&csStart);
5870 const llvm::UTF8 *E =
5871 reinterpret_cast<const llvm::UTF8 *>(csStart + csLen);
5872 llvm::ConversionResult Result =
5873 llvm::convertUTF8Sequence(B, E, &CodePoint, llvm::strictConversion);
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +00005874
Justin Lebar90910552016-09-30 00:38:45 +00005875 if (Result != llvm::conversionOK) {
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +00005876 unsigned char FirstChar = *csStart;
Justin Lebar90910552016-09-30 00:38:45 +00005877 CodePoint = (llvm::UTF32)FirstChar;
Bruno Cardoso Lopes0c18d032016-03-29 17:35:02 +00005878 }
5879
5880 llvm::raw_string_ostream OS(CodePointStr);
5881 if (CodePoint < 256)
5882 OS << "\\x" << llvm::format("%02x", CodePoint);
5883 else if (CodePoint <= 0xFFFF)
5884 OS << "\\u" << llvm::format("%04x", CodePoint);
5885 else
5886 OS << "\\U" << llvm::format("%08x", CodePoint);
5887 OS.flush();
5888 Specifier = CodePointStr;
5889 }
5890
5891 EmitFormatDiagnostic(
5892 S.PDiag(diag::warn_format_invalid_conversion) << Specifier, Loc,
5893 /*IsStringLocation*/ true, getSpecifierRange(startSpec, specifierLen));
5894
Ted Kremenekce815422010-07-19 21:25:57 +00005895 return keepGoing;
5896}
5897
Richard Trieu03cf7b72011-10-28 00:41:25 +00005898void
5899CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
5900 const char *startSpec,
5901 unsigned specifierLen) {
5902 EmitFormatDiagnostic(
5903 S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
5904 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
5905}
5906
Ted Kremenek6adb7e32010-07-26 19:45:42 +00005907bool
5908CheckFormatHandler::CheckNumArgs(
5909 const analyze_format_string::FormatSpecifier &FS,
5910 const analyze_format_string::ConversionSpecifier &CS,
5911 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
5912
5913 if (argIndex >= NumDataArgs) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00005914 PartialDiagnostic PDiag = FS.usesPositionalArg()
5915 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
5916 << (argIndex+1) << NumDataArgs)
5917 : S.PDiag(diag::warn_printf_insufficient_data_args);
5918 EmitFormatDiagnostic(
5919 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
5920 getSpecifierRange(startSpecifier, specifierLen));
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00005921
5922 // Since more arguments than conversion tokens are given, by extension
5923 // all arguments are covered, so mark this as so.
5924 UncoveredArg.setAllCovered();
Ted Kremenek6adb7e32010-07-26 19:45:42 +00005925 return false;
5926 }
5927 return true;
5928}
5929
Richard Trieu03cf7b72011-10-28 00:41:25 +00005930template<typename Range>
5931void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
5932 SourceLocation Loc,
5933 bool IsStringLocation,
5934 Range StringRange,
Jordan Roseaee34382012-09-05 22:56:26 +00005935 ArrayRef<FixItHint> FixIt) {
Jean-Daniel Dupas0ae6e672012-01-17 20:03:31 +00005936 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
Richard Trieu03cf7b72011-10-28 00:41:25 +00005937 Loc, IsStringLocation, StringRange, FixIt);
5938}
5939
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005940/// If the format string is not within the function call, emit a note
Richard Trieu03cf7b72011-10-28 00:41:25 +00005941/// so that the function call and string are in diagnostic messages.
5942///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00005943/// \param InFunctionCall if true, the format string is within the function
Richard Trieu03cf7b72011-10-28 00:41:25 +00005944/// call and only one diagnostic message will be produced. Otherwise, an
5945/// extra note will be emitted pointing to location of the format string.
5946///
5947/// \param ArgumentExpr the expression that is passed as the format string
5948/// argument in the function call. Used for getting locations when two
5949/// diagnostics are emitted.
5950///
5951/// \param PDiag the callee should already have provided any strings for the
5952/// diagnostic message. This function only adds locations and fixits
5953/// to diagnostics.
5954///
5955/// \param Loc primary location for diagnostic. If two diagnostics are
5956/// required, one will be at Loc and a new SourceLocation will be created for
5957/// the other one.
5958///
5959/// \param IsStringLocation if true, Loc points to the format string should be
5960/// used for the note. Otherwise, Loc points to the argument list and will
5961/// be used with PDiag.
5962///
5963/// \param StringRange some or all of the string to highlight. This is
5964/// templated so it can accept either a CharSourceRange or a SourceRange.
5965///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00005966/// \param FixIt optional fix it hint for the format string.
Benjamin Kramer7320b992016-06-15 14:20:56 +00005967template <typename Range>
5968void CheckFormatHandler::EmitFormatDiagnostic(
5969 Sema &S, bool InFunctionCall, const Expr *ArgumentExpr,
5970 const PartialDiagnostic &PDiag, SourceLocation Loc, bool IsStringLocation,
5971 Range StringRange, ArrayRef<FixItHint> FixIt) {
Jordan Roseaee34382012-09-05 22:56:26 +00005972 if (InFunctionCall) {
5973 const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag);
5974 D << StringRange;
Alexander Kornienkoa9b01eb2015-02-25 14:40:56 +00005975 D << FixIt;
Jordan Roseaee34382012-09-05 22:56:26 +00005976 } else {
Richard Trieu03cf7b72011-10-28 00:41:25 +00005977 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
5978 << ArgumentExpr->getSourceRange();
Jordan Roseaee34382012-09-05 22:56:26 +00005979
5980 const Sema::SemaDiagnosticBuilder &Note =
5981 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
5982 diag::note_format_string_defined);
5983
5984 Note << StringRange;
Alexander Kornienkoa9b01eb2015-02-25 14:40:56 +00005985 Note << FixIt;
Richard Trieu03cf7b72011-10-28 00:41:25 +00005986 }
5987}
5988
Ted Kremenek02087932010-07-16 02:11:22 +00005989//===--- CHECK: Printf format string checking ------------------------------===//
5990
5991namespace {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00005992
Ted Kremenek02087932010-07-16 02:11:22 +00005993class CheckPrintfHandler : public CheckFormatHandler {
5994public:
Stephen Hines648c3692016-09-16 01:07:04 +00005995 CheckPrintfHandler(Sema &s, const FormatStringLiteral *fexpr,
Mehdi Amini06d367c2016-10-24 20:39:34 +00005996 const Expr *origFormatExpr,
5997 const Sema::FormatStringType type, unsigned firstDataArg,
5998 unsigned numDataArgs, bool isObjC, const char *beg,
5999 bool hasVAListArg, ArrayRef<const Expr *> Args,
Jordan Rose3e0ec582012-07-19 18:10:23 +00006000 unsigned formatIdx, bool inFunctionCall,
Richard Smithd7293d72013-08-05 18:49:43 +00006001 Sema::VariadicCallType CallType,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00006002 llvm::SmallBitVector &CheckedVarArgs,
6003 UncoveredArgHandler &UncoveredArg)
Mehdi Amini06d367c2016-10-24 20:39:34 +00006004 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg,
6005 numDataArgs, beg, hasVAListArg, Args, formatIdx,
6006 inFunctionCall, CallType, CheckedVarArgs,
6007 UncoveredArg) {}
6008
6009 bool isObjCContext() const { return FSType == Sema::FST_NSString; }
6010
6011 /// Returns true if '%@' specifiers are allowed in the format string.
6012 bool allowsObjCArg() const {
6013 return FSType == Sema::FST_NSString || FSType == Sema::FST_OSLog ||
6014 FSType == Sema::FST_OSTrace;
6015 }
Jordan Rose3e0ec582012-07-19 18:10:23 +00006016
Ted Kremenek02087932010-07-16 02:11:22 +00006017 bool HandleInvalidPrintfConversionSpecifier(
6018 const analyze_printf::PrintfSpecifier &FS,
6019 const char *startSpecifier,
Craig Toppere14c0f82014-03-12 04:55:44 +00006020 unsigned specifierLen) override;
6021
Ted Kremenek02087932010-07-16 02:11:22 +00006022 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
6023 const char *startSpecifier,
Craig Toppere14c0f82014-03-12 04:55:44 +00006024 unsigned specifierLen) override;
Richard Smith55ce3522012-06-25 20:30:08 +00006025 bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
6026 const char *StartSpecifier,
6027 unsigned SpecifierLen,
6028 const Expr *E);
6029
Ted Kremenek02087932010-07-16 02:11:22 +00006030 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
6031 const char *startSpecifier, unsigned specifierLen);
6032 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
6033 const analyze_printf::OptionalAmount &Amt,
6034 unsigned type,
6035 const char *startSpecifier, unsigned specifierLen);
6036 void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
6037 const analyze_printf::OptionalFlag &flag,
6038 const char *startSpecifier, unsigned specifierLen);
6039 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
6040 const analyze_printf::OptionalFlag &ignoredFlag,
6041 const analyze_printf::OptionalFlag &flag,
6042 const char *startSpecifier, unsigned specifierLen);
Hans Wennborgc3b3da02012-08-07 08:11:26 +00006043 bool checkForCStrMembers(const analyze_printf::ArgType &AT,
Richard Smith2868a732014-02-28 01:36:39 +00006044 const Expr *E);
Ted Kremenek2b417712015-07-02 05:39:16 +00006045
6046 void HandleEmptyObjCModifierFlag(const char *startFlag,
6047 unsigned flagLen) override;
Richard Smith55ce3522012-06-25 20:30:08 +00006048
Ted Kremenek2b417712015-07-02 05:39:16 +00006049 void HandleInvalidObjCModifierFlag(const char *startFlag,
6050 unsigned flagLen) override;
6051
6052 void HandleObjCFlagsWithNonObjCConversion(const char *flagsStart,
6053 const char *flagsEnd,
6054 const char *conversionPosition)
6055 override;
6056};
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00006057
6058} // namespace
Ted Kremenek02087932010-07-16 02:11:22 +00006059
6060bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
6061 const analyze_printf::PrintfSpecifier &FS,
6062 const char *startSpecifier,
6063 unsigned specifierLen) {
Ted Kremenekf03e6d852010-07-20 20:04:27 +00006064 const analyze_printf::PrintfConversionSpecifier &CS =
Ted Kremenekce815422010-07-19 21:25:57 +00006065 FS.getConversionSpecifier();
Ted Kremenek02087932010-07-16 02:11:22 +00006066
Ted Kremenekce815422010-07-19 21:25:57 +00006067 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
6068 getLocationOfByte(CS.getStart()),
6069 startSpecifier, specifierLen,
6070 CS.getStart(), CS.getLength());
Ted Kremenek94af5752010-01-29 02:40:24 +00006071}
6072
Ted Kremenek02087932010-07-16 02:11:22 +00006073bool CheckPrintfHandler::HandleAmount(
6074 const analyze_format_string::OptionalAmount &Amt,
6075 unsigned k, const char *startSpecifier,
6076 unsigned specifierLen) {
Ted Kremenek5739de72010-01-29 01:06:55 +00006077 if (Amt.hasDataArgument()) {
Ted Kremenek5739de72010-01-29 01:06:55 +00006078 if (!HasVAListArg) {
Ted Kremenek4a49d982010-02-26 19:18:41 +00006079 unsigned argIndex = Amt.getArgIndex();
6080 if (argIndex >= NumDataArgs) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00006081 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
6082 << k,
6083 getLocationOfByte(Amt.getStart()),
6084 /*IsStringLocation*/true,
6085 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek5739de72010-01-29 01:06:55 +00006086 // Don't do any more checking. We will just emit
6087 // spurious errors.
6088 return false;
6089 }
Ted Kremenekc8b188d2010-02-16 01:46:59 +00006090
Ted Kremenek5739de72010-01-29 01:06:55 +00006091 // Type check the data argument. It should be an 'int'.
Ted Kremenek605b0112010-01-29 23:32:22 +00006092 // Although not in conformance with C99, we also allow the argument to be
6093 // an 'unsigned int' as that is a reasonably safe case. GCC also
6094 // doesn't emit a warning for that case.
Ted Kremenek4a49d982010-02-26 19:18:41 +00006095 CoveredArgs.set(argIndex);
6096 const Expr *Arg = getDataArg(argIndex);
Jordan Rose58bbe422012-07-19 18:10:08 +00006097 if (!Arg)
6098 return false;
6099
Ted Kremenek5739de72010-01-29 01:06:55 +00006100 QualType T = Arg->getType();
Ted Kremenekc8b188d2010-02-16 01:46:59 +00006101
Hans Wennborgc3b3da02012-08-07 08:11:26 +00006102 const analyze_printf::ArgType &AT = Amt.getArgType(S.Context);
6103 assert(AT.isValid());
Ted Kremenekc8b188d2010-02-16 01:46:59 +00006104
Hans Wennborgc3b3da02012-08-07 08:11:26 +00006105 if (!AT.matchesType(S.Context, T)) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00006106 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
Hans Wennborgc3b3da02012-08-07 08:11:26 +00006107 << k << AT.getRepresentativeTypeName(S.Context)
Richard Trieu03cf7b72011-10-28 00:41:25 +00006108 << T << Arg->getSourceRange(),
6109 getLocationOfByte(Amt.getStart()),
6110 /*IsStringLocation*/true,
6111 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek5739de72010-01-29 01:06:55 +00006112 // Don't do any more checking. We will just emit
6113 // spurious errors.
6114 return false;
6115 }
6116 }
6117 }
6118 return true;
6119}
Ted Kremenek5739de72010-01-29 01:06:55 +00006120
Tom Careb49ec692010-06-17 19:00:27 +00006121void CheckPrintfHandler::HandleInvalidAmount(
Ted Kremenek02087932010-07-16 02:11:22 +00006122 const analyze_printf::PrintfSpecifier &FS,
Tom Careb49ec692010-06-17 19:00:27 +00006123 const analyze_printf::OptionalAmount &Amt,
6124 unsigned type,
6125 const char *startSpecifier,
6126 unsigned specifierLen) {
Ted Kremenekf03e6d852010-07-20 20:04:27 +00006127 const analyze_printf::PrintfConversionSpecifier &CS =
6128 FS.getConversionSpecifier();
Tom Careb49ec692010-06-17 19:00:27 +00006129
Richard Trieu03cf7b72011-10-28 00:41:25 +00006130 FixItHint fixit =
6131 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
6132 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
6133 Amt.getConstantLength()))
6134 : FixItHint();
6135
6136 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
6137 << type << CS.toString(),
6138 getLocationOfByte(Amt.getStart()),
6139 /*IsStringLocation*/true,
6140 getSpecifierRange(startSpecifier, specifierLen),
6141 fixit);
Tom Careb49ec692010-06-17 19:00:27 +00006142}
6143
Ted Kremenek02087932010-07-16 02:11:22 +00006144void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
Tom Careb49ec692010-06-17 19:00:27 +00006145 const analyze_printf::OptionalFlag &flag,
6146 const char *startSpecifier,
6147 unsigned specifierLen) {
6148 // Warn about pointless flag with a fixit removal.
Ted Kremenekf03e6d852010-07-20 20:04:27 +00006149 const analyze_printf::PrintfConversionSpecifier &CS =
6150 FS.getConversionSpecifier();
Richard Trieu03cf7b72011-10-28 00:41:25 +00006151 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
6152 << flag.toString() << CS.toString(),
6153 getLocationOfByte(flag.getPosition()),
6154 /*IsStringLocation*/true,
6155 getSpecifierRange(startSpecifier, specifierLen),
6156 FixItHint::CreateRemoval(
6157 getSpecifierRange(flag.getPosition(), 1)));
Tom Careb49ec692010-06-17 19:00:27 +00006158}
6159
6160void CheckPrintfHandler::HandleIgnoredFlag(
Ted Kremenek02087932010-07-16 02:11:22 +00006161 const analyze_printf::PrintfSpecifier &FS,
Tom Careb49ec692010-06-17 19:00:27 +00006162 const analyze_printf::OptionalFlag &ignoredFlag,
6163 const analyze_printf::OptionalFlag &flag,
6164 const char *startSpecifier,
6165 unsigned specifierLen) {
6166 // Warn about ignored flag with a fixit removal.
Richard Trieu03cf7b72011-10-28 00:41:25 +00006167 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
6168 << ignoredFlag.toString() << flag.toString(),
6169 getLocationOfByte(ignoredFlag.getPosition()),
6170 /*IsStringLocation*/true,
6171 getSpecifierRange(startSpecifier, specifierLen),
6172 FixItHint::CreateRemoval(
6173 getSpecifierRange(ignoredFlag.getPosition(), 1)));
Tom Careb49ec692010-06-17 19:00:27 +00006174}
6175
Ted Kremenek2b417712015-07-02 05:39:16 +00006176void CheckPrintfHandler::HandleEmptyObjCModifierFlag(const char *startFlag,
6177 unsigned flagLen) {
6178 // Warn about an empty flag.
6179 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_empty_objc_flag),
6180 getLocationOfByte(startFlag),
6181 /*IsStringLocation*/true,
6182 getSpecifierRange(startFlag, flagLen));
6183}
6184
6185void CheckPrintfHandler::HandleInvalidObjCModifierFlag(const char *startFlag,
6186 unsigned flagLen) {
6187 // Warn about an invalid flag.
6188 auto Range = getSpecifierRange(startFlag, flagLen);
6189 StringRef flag(startFlag, flagLen);
6190 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_invalid_objc_flag) << flag,
6191 getLocationOfByte(startFlag),
6192 /*IsStringLocation*/true,
6193 Range, FixItHint::CreateRemoval(Range));
6194}
6195
6196void CheckPrintfHandler::HandleObjCFlagsWithNonObjCConversion(
6197 const char *flagsStart, const char *flagsEnd, const char *conversionPosition) {
6198 // Warn about using '[...]' without a '@' conversion.
6199 auto Range = getSpecifierRange(flagsStart, flagsEnd - flagsStart + 1);
6200 auto diag = diag::warn_printf_ObjCflags_without_ObjCConversion;
6201 EmitFormatDiagnostic(S.PDiag(diag) << StringRef(conversionPosition, 1),
6202 getLocationOfByte(conversionPosition),
6203 /*IsStringLocation*/true,
6204 Range, FixItHint::CreateRemoval(Range));
6205}
6206
Richard Smith55ce3522012-06-25 20:30:08 +00006207// Determines if the specified is a C++ class or struct containing
6208// a member with the specified name and kind (e.g. a CXXMethodDecl named
6209// "c_str()").
6210template<typename MemberKind>
6211static llvm::SmallPtrSet<MemberKind*, 1>
6212CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
6213 const RecordType *RT = Ty->getAs<RecordType>();
6214 llvm::SmallPtrSet<MemberKind*, 1> Results;
6215
6216 if (!RT)
6217 return Results;
6218 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
Richard Smith2868a732014-02-28 01:36:39 +00006219 if (!RD || !RD->getDefinition())
Richard Smith55ce3522012-06-25 20:30:08 +00006220 return Results;
6221
Alp Tokerb6cc5922014-05-03 03:45:55 +00006222 LookupResult R(S, &S.Context.Idents.get(Name), SourceLocation(),
Richard Smith55ce3522012-06-25 20:30:08 +00006223 Sema::LookupMemberName);
Richard Smith2868a732014-02-28 01:36:39 +00006224 R.suppressDiagnostics();
Richard Smith55ce3522012-06-25 20:30:08 +00006225
6226 // We just need to include all members of the right kind turned up by the
6227 // filter, at this point.
6228 if (S.LookupQualifiedName(R, RT->getDecl()))
6229 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
6230 NamedDecl *decl = (*I)->getUnderlyingDecl();
6231 if (MemberKind *FK = dyn_cast<MemberKind>(decl))
6232 Results.insert(FK);
6233 }
6234 return Results;
6235}
6236
Richard Smith2868a732014-02-28 01:36:39 +00006237/// Check if we could call '.c_str()' on an object.
6238///
6239/// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't
6240/// allow the call, or if it would be ambiguous).
6241bool Sema::hasCStrMethod(const Expr *E) {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00006242 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>;
6243
Richard Smith2868a732014-02-28 01:36:39 +00006244 MethodSet Results =
6245 CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType());
6246 for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
6247 MI != ME; ++MI)
6248 if ((*MI)->getMinRequiredArguments() == 0)
6249 return true;
6250 return false;
6251}
6252
Richard Smith55ce3522012-06-25 20:30:08 +00006253// Check if a (w)string was passed when a (w)char* was needed, and offer a
Hans Wennborgc3b3da02012-08-07 08:11:26 +00006254// better diagnostic if so. AT is assumed to be valid.
Richard Smith55ce3522012-06-25 20:30:08 +00006255// Returns true when a c_str() conversion method is found.
6256bool CheckPrintfHandler::checkForCStrMembers(
Richard Smith2868a732014-02-28 01:36:39 +00006257 const analyze_printf::ArgType &AT, const Expr *E) {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00006258 using MethodSet = llvm::SmallPtrSet<CXXMethodDecl *, 1>;
Richard Smith55ce3522012-06-25 20:30:08 +00006259
6260 MethodSet Results =
6261 CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType());
6262
6263 for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
6264 MI != ME; ++MI) {
6265 const CXXMethodDecl *Method = *MI;
Richard Smith2868a732014-02-28 01:36:39 +00006266 if (Method->getMinRequiredArguments() == 0 &&
Alp Toker314cc812014-01-25 16:55:45 +00006267 AT.matchesType(S.Context, Method->getReturnType())) {
Richard Smith55ce3522012-06-25 20:30:08 +00006268 // FIXME: Suggest parens if the expression needs them.
Alp Tokerb6cc5922014-05-03 03:45:55 +00006269 SourceLocation EndLoc = S.getLocForEndOfToken(E->getLocEnd());
Richard Smith55ce3522012-06-25 20:30:08 +00006270 S.Diag(E->getLocStart(), diag::note_printf_c_str)
6271 << "c_str()"
6272 << FixItHint::CreateInsertion(EndLoc, ".c_str()");
6273 return true;
6274 }
6275 }
6276
6277 return false;
6278}
6279
Ted Kremenekab278de2010-01-28 23:39:18 +00006280bool
Ted Kremenek02087932010-07-16 02:11:22 +00006281CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
Ted Kremenekd31b2632010-02-11 09:27:41 +00006282 &FS,
Ted Kremenekab278de2010-01-28 23:39:18 +00006283 const char *startSpecifier,
6284 unsigned specifierLen) {
Ted Kremenekf03e6d852010-07-20 20:04:27 +00006285 using namespace analyze_format_string;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00006286 using namespace analyze_printf;
6287
Ted Kremenekf03e6d852010-07-20 20:04:27 +00006288 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
Ted Kremenekab278de2010-01-28 23:39:18 +00006289
Ted Kremenek6cd69422010-07-19 22:01:06 +00006290 if (FS.consumesDataArgument()) {
6291 if (atFirstArg) {
6292 atFirstArg = false;
6293 usesPositionalArgs = FS.usesPositionalArg();
6294 }
6295 else if (usesPositionalArgs != FS.usesPositionalArg()) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00006296 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
6297 startSpecifier, specifierLen);
Ted Kremenek6cd69422010-07-19 22:01:06 +00006298 return false;
6299 }
Ted Kremenek5739de72010-01-29 01:06:55 +00006300 }
Ted Kremenekc8b188d2010-02-16 01:46:59 +00006301
Ted Kremenekd1668192010-02-27 01:41:03 +00006302 // First check if the field width, precision, and conversion specifier
6303 // have matching data arguments.
6304 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
6305 startSpecifier, specifierLen)) {
6306 return false;
6307 }
6308
6309 if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
6310 startSpecifier, specifierLen)) {
Ted Kremenek5739de72010-01-29 01:06:55 +00006311 return false;
6312 }
6313
Ted Kremenek8d9842d2010-01-29 20:55:36 +00006314 if (!CS.consumesDataArgument()) {
6315 // FIXME: Technically specifying a precision or field width here
6316 // makes no sense. Worth issuing a warning at some point.
Ted Kremenekfb45d352010-02-10 02:16:30 +00006317 return true;
Ted Kremenek8d9842d2010-01-29 20:55:36 +00006318 }
Ted Kremenekc8b188d2010-02-16 01:46:59 +00006319
Ted Kremenek4a49d982010-02-26 19:18:41 +00006320 // Consume the argument.
6321 unsigned argIndex = FS.getArgIndex();
Ted Kremenek09597b42010-02-27 08:34:51 +00006322 if (argIndex < NumDataArgs) {
6323 // The check to see if the argIndex is valid will come later.
6324 // We set the bit here because we may exit early from this
6325 // function if we encounter some other error.
6326 CoveredArgs.set(argIndex);
6327 }
Ted Kremenek4a49d982010-02-26 19:18:41 +00006328
Dimitry Andric6b5ed342015-02-19 22:32:33 +00006329 // FreeBSD kernel extensions.
6330 if (CS.getKind() == ConversionSpecifier::FreeBSDbArg ||
6331 CS.getKind() == ConversionSpecifier::FreeBSDDArg) {
6332 // We need at least two arguments.
6333 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex + 1))
6334 return false;
6335
6336 // Claim the second argument.
6337 CoveredArgs.set(argIndex + 1);
6338
6339 // Type check the first argument (int for %b, pointer for %D)
6340 const Expr *Ex = getDataArg(argIndex);
6341 const analyze_printf::ArgType &AT =
6342 (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ?
6343 ArgType(S.Context.IntTy) : ArgType::CPointerTy;
6344 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType()))
6345 EmitFormatDiagnostic(
6346 S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
6347 << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
6348 << false << Ex->getSourceRange(),
6349 Ex->getLocStart(), /*IsStringLocation*/false,
6350 getSpecifierRange(startSpecifier, specifierLen));
6351
6352 // Type check the second argument (char * for both %b and %D)
6353 Ex = getDataArg(argIndex + 1);
6354 const analyze_printf::ArgType &AT2 = ArgType::CStrTy;
6355 if (AT2.isValid() && !AT2.matchesType(S.Context, Ex->getType()))
6356 EmitFormatDiagnostic(
6357 S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
6358 << AT2.getRepresentativeTypeName(S.Context) << Ex->getType()
6359 << false << Ex->getSourceRange(),
6360 Ex->getLocStart(), /*IsStringLocation*/false,
6361 getSpecifierRange(startSpecifier, specifierLen));
6362
6363 return true;
6364 }
6365
Ted Kremenek4a49d982010-02-26 19:18:41 +00006366 // Check for using an Objective-C specific conversion specifier
6367 // in a non-ObjC literal.
Mehdi Amini06d367c2016-10-24 20:39:34 +00006368 if (!allowsObjCArg() && CS.isObjCArg()) {
Ted Kremenek02087932010-07-16 02:11:22 +00006369 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
6370 specifierLen);
Ted Kremenek4a49d982010-02-26 19:18:41 +00006371 }
Ted Kremenekc8b188d2010-02-16 01:46:59 +00006372
Mehdi Amini06d367c2016-10-24 20:39:34 +00006373 // %P can only be used with os_log.
6374 if (FSType != Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::PArg) {
6375 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
6376 specifierLen);
6377 }
6378
6379 // %n is not allowed with os_log.
6380 if (FSType == Sema::FST_OSLog && CS.getKind() == ConversionSpecifier::nArg) {
6381 EmitFormatDiagnostic(S.PDiag(diag::warn_os_log_format_narg),
6382 getLocationOfByte(CS.getStart()),
6383 /*IsStringLocation*/ false,
6384 getSpecifierRange(startSpecifier, specifierLen));
6385
6386 return true;
6387 }
6388
6389 // Only scalars are allowed for os_trace.
6390 if (FSType == Sema::FST_OSTrace &&
6391 (CS.getKind() == ConversionSpecifier::PArg ||
6392 CS.getKind() == ConversionSpecifier::sArg ||
6393 CS.getKind() == ConversionSpecifier::ObjCObjArg)) {
6394 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
6395 specifierLen);
6396 }
6397
6398 // Check for use of public/private annotation outside of os_log().
6399 if (FSType != Sema::FST_OSLog) {
6400 if (FS.isPublic().isSet()) {
6401 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation)
6402 << "public",
6403 getLocationOfByte(FS.isPublic().getPosition()),
6404 /*IsStringLocation*/ false,
6405 getSpecifierRange(startSpecifier, specifierLen));
6406 }
6407 if (FS.isPrivate().isSet()) {
6408 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_annotation)
6409 << "private",
6410 getLocationOfByte(FS.isPrivate().getPosition()),
6411 /*IsStringLocation*/ false,
6412 getSpecifierRange(startSpecifier, specifierLen));
6413 }
6414 }
6415
Tom Careb49ec692010-06-17 19:00:27 +00006416 // Check for invalid use of field width
6417 if (!FS.hasValidFieldWidth()) {
Tom Care3f272b82010-06-21 21:21:01 +00006418 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
Tom Careb49ec692010-06-17 19:00:27 +00006419 startSpecifier, specifierLen);
6420 }
6421
6422 // Check for invalid use of precision
6423 if (!FS.hasValidPrecision()) {
6424 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
6425 startSpecifier, specifierLen);
6426 }
6427
Mehdi Amini06d367c2016-10-24 20:39:34 +00006428 // Precision is mandatory for %P specifier.
6429 if (CS.getKind() == ConversionSpecifier::PArg &&
6430 FS.getPrecision().getHowSpecified() == OptionalAmount::NotSpecified) {
6431 EmitFormatDiagnostic(S.PDiag(diag::warn_format_P_no_precision),
6432 getLocationOfByte(startSpecifier),
6433 /*IsStringLocation*/ false,
6434 getSpecifierRange(startSpecifier, specifierLen));
6435 }
6436
Tom Careb49ec692010-06-17 19:00:27 +00006437 // Check each flag does not conflict with any other component.
Ted Kremenekbf4832c2011-01-08 05:28:46 +00006438 if (!FS.hasValidThousandsGroupingPrefix())
6439 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
Tom Careb49ec692010-06-17 19:00:27 +00006440 if (!FS.hasValidLeadingZeros())
6441 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
6442 if (!FS.hasValidPlusPrefix())
6443 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
Tom Care3f272b82010-06-21 21:21:01 +00006444 if (!FS.hasValidSpacePrefix())
6445 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
Tom Careb49ec692010-06-17 19:00:27 +00006446 if (!FS.hasValidAlternativeForm())
6447 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
6448 if (!FS.hasValidLeftJustified())
6449 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
6450
6451 // Check that flags are not ignored by another flag
Tom Care3f272b82010-06-21 21:21:01 +00006452 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
6453 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
6454 startSpecifier, specifierLen);
Tom Careb49ec692010-06-17 19:00:27 +00006455 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
6456 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
6457 startSpecifier, specifierLen);
6458
6459 // Check the length modifier is valid with the given conversion specifier.
Jordan Rose92303592012-09-08 04:00:03 +00006460 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
Jordan Rose2f9cc042012-09-08 04:00:12 +00006461 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
6462 diag::warn_format_nonsensical_length);
Jordan Rose92303592012-09-08 04:00:03 +00006463 else if (!FS.hasStandardLengthModifier())
Jordan Rose2f9cc042012-09-08 04:00:12 +00006464 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
Jordan Rose92303592012-09-08 04:00:03 +00006465 else if (!FS.hasStandardLengthConversionCombination())
Jordan Rose2f9cc042012-09-08 04:00:12 +00006466 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
6467 diag::warn_format_non_standard_conversion_spec);
Tom Careb49ec692010-06-17 19:00:27 +00006468
Jordan Rose92303592012-09-08 04:00:03 +00006469 if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
6470 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
6471
Ted Kremenek9fcd8302010-01-29 01:43:31 +00006472 // The remaining checks depend on the data arguments.
6473 if (HasVAListArg)
6474 return true;
Ted Kremenekc8b188d2010-02-16 01:46:59 +00006475
Ted Kremenek6adb7e32010-07-26 19:45:42 +00006476 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
Ted Kremenek9fcd8302010-01-29 01:43:31 +00006477 return false;
Ted Kremenekc8b188d2010-02-16 01:46:59 +00006478
Jordan Rose58bbe422012-07-19 18:10:08 +00006479 const Expr *Arg = getDataArg(argIndex);
6480 if (!Arg)
6481 return true;
6482
6483 return checkFormatExpr(FS, startSpecifier, specifierLen, Arg);
Richard Smith55ce3522012-06-25 20:30:08 +00006484}
6485
Jordan Roseaee34382012-09-05 22:56:26 +00006486static bool requiresParensToAddCast(const Expr *E) {
6487 // FIXME: We should have a general way to reason about operator
6488 // precedence and whether parens are actually needed here.
6489 // Take care of a few common cases where they aren't.
6490 const Expr *Inside = E->IgnoreImpCasts();
6491 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside))
6492 Inside = POE->getSyntacticForm()->IgnoreImpCasts();
6493
6494 switch (Inside->getStmtClass()) {
6495 case Stmt::ArraySubscriptExprClass:
6496 case Stmt::CallExprClass:
Jordan Roseea0fdfe2012-12-05 18:44:44 +00006497 case Stmt::CharacterLiteralClass:
6498 case Stmt::CXXBoolLiteralExprClass:
Jordan Roseaee34382012-09-05 22:56:26 +00006499 case Stmt::DeclRefExprClass:
Jordan Roseea0fdfe2012-12-05 18:44:44 +00006500 case Stmt::FloatingLiteralClass:
6501 case Stmt::IntegerLiteralClass:
Jordan Roseaee34382012-09-05 22:56:26 +00006502 case Stmt::MemberExprClass:
Jordan Roseea0fdfe2012-12-05 18:44:44 +00006503 case Stmt::ObjCArrayLiteralClass:
6504 case Stmt::ObjCBoolLiteralExprClass:
6505 case Stmt::ObjCBoxedExprClass:
6506 case Stmt::ObjCDictionaryLiteralClass:
6507 case Stmt::ObjCEncodeExprClass:
Jordan Roseaee34382012-09-05 22:56:26 +00006508 case Stmt::ObjCIvarRefExprClass:
6509 case Stmt::ObjCMessageExprClass:
6510 case Stmt::ObjCPropertyRefExprClass:
Jordan Roseea0fdfe2012-12-05 18:44:44 +00006511 case Stmt::ObjCStringLiteralClass:
6512 case Stmt::ObjCSubscriptRefExprClass:
Jordan Roseaee34382012-09-05 22:56:26 +00006513 case Stmt::ParenExprClass:
Jordan Roseea0fdfe2012-12-05 18:44:44 +00006514 case Stmt::StringLiteralClass:
Jordan Roseaee34382012-09-05 22:56:26 +00006515 case Stmt::UnaryOperatorClass:
6516 return false;
6517 default:
6518 return true;
6519 }
6520}
6521
Anton Korobeynikov5f951ee2014-11-14 22:09:15 +00006522static std::pair<QualType, StringRef>
6523shouldNotPrintDirectly(const ASTContext &Context,
6524 QualType IntendedTy,
6525 const Expr *E) {
6526 // Use a 'while' to peel off layers of typedefs.
6527 QualType TyTy = IntendedTy;
6528 while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) {
6529 StringRef Name = UserTy->getDecl()->getName();
6530 QualType CastTy = llvm::StringSwitch<QualType>(Name)
Saleem Abdulrasoola01ed932017-10-17 17:39:32 +00006531 .Case("CFIndex", Context.getNSIntegerType())
6532 .Case("NSInteger", Context.getNSIntegerType())
6533 .Case("NSUInteger", Context.getNSUIntegerType())
Anton Korobeynikov5f951ee2014-11-14 22:09:15 +00006534 .Case("SInt32", Context.IntTy)
6535 .Case("UInt32", Context.UnsignedIntTy)
6536 .Default(QualType());
6537
6538 if (!CastTy.isNull())
6539 return std::make_pair(CastTy, Name);
6540
6541 TyTy = UserTy->desugar();
6542 }
6543
6544 // Strip parens if necessary.
6545 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
6546 return shouldNotPrintDirectly(Context,
6547 PE->getSubExpr()->getType(),
6548 PE->getSubExpr());
6549
6550 // If this is a conditional expression, then its result type is constructed
6551 // via usual arithmetic conversions and thus there might be no necessary
6552 // typedef sugar there. Recurse to operands to check for NSInteger &
6553 // Co. usage condition.
6554 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
6555 QualType TrueTy, FalseTy;
6556 StringRef TrueName, FalseName;
6557
6558 std::tie(TrueTy, TrueName) =
6559 shouldNotPrintDirectly(Context,
6560 CO->getTrueExpr()->getType(),
6561 CO->getTrueExpr());
6562 std::tie(FalseTy, FalseName) =
6563 shouldNotPrintDirectly(Context,
6564 CO->getFalseExpr()->getType(),
6565 CO->getFalseExpr());
6566
6567 if (TrueTy == FalseTy)
6568 return std::make_pair(TrueTy, TrueName);
6569 else if (TrueTy.isNull())
6570 return std::make_pair(FalseTy, FalseName);
6571 else if (FalseTy.isNull())
6572 return std::make_pair(TrueTy, TrueName);
6573 }
6574
6575 return std::make_pair(QualType(), StringRef());
6576}
6577
Richard Smith55ce3522012-06-25 20:30:08 +00006578bool
6579CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
6580 const char *StartSpecifier,
6581 unsigned SpecifierLen,
6582 const Expr *E) {
6583 using namespace analyze_format_string;
6584 using namespace analyze_printf;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00006585
Michael J. Spencer2c35bc12010-07-27 04:46:02 +00006586 // Now type check the data expression that matches the
6587 // format specifier.
Mehdi Amini06d367c2016-10-24 20:39:34 +00006588 const analyze_printf::ArgType &AT = FS.getArgType(S.Context, isObjCContext());
Jordan Rose22b74712012-09-05 22:56:19 +00006589 if (!AT.isValid())
6590 return true;
Jordan Roseaee34382012-09-05 22:56:26 +00006591
Jordan Rose598ec092012-12-05 18:44:40 +00006592 QualType ExprTy = E->getType();
Ted Kremenek3365e522013-04-10 06:26:26 +00006593 while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) {
6594 ExprTy = TET->getUnderlyingExpr()->getType();
6595 }
6596
Seth Cantrellb4802962015-03-04 03:12:10 +00006597 analyze_printf::ArgType::MatchKind match = AT.matchesType(S.Context, ExprTy);
6598
6599 if (match == analyze_printf::ArgType::Match) {
Jordan Rose22b74712012-09-05 22:56:19 +00006600 return true;
Seth Cantrellb4802962015-03-04 03:12:10 +00006601 }
Jordan Rose98709982012-06-04 22:48:57 +00006602
Jordan Rose22b74712012-09-05 22:56:19 +00006603 // Look through argument promotions for our error message's reported type.
6604 // This includes the integral and floating promotions, but excludes array
6605 // and function pointer decay; seeing that an argument intended to be a
6606 // string has type 'char [6]' is probably more confusing than 'char *'.
6607 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
6608 if (ICE->getCastKind() == CK_IntegralCast ||
6609 ICE->getCastKind() == CK_FloatingCast) {
6610 E = ICE->getSubExpr();
Jordan Rose598ec092012-12-05 18:44:40 +00006611 ExprTy = E->getType();
Jordan Rose22b74712012-09-05 22:56:19 +00006612
6613 // Check if we didn't match because of an implicit cast from a 'char'
6614 // or 'short' to an 'int'. This is done because printf is a varargs
6615 // function.
6616 if (ICE->getType() == S.Context.IntTy ||
6617 ICE->getType() == S.Context.UnsignedIntTy) {
6618 // All further checking is done on the subexpression.
Jordan Rose598ec092012-12-05 18:44:40 +00006619 if (AT.matchesType(S.Context, ExprTy))
Jordan Rose22b74712012-09-05 22:56:19 +00006620 return true;
Ted Kremenek12a37de2010-10-21 04:00:58 +00006621 }
Jordan Rose98709982012-06-04 22:48:57 +00006622 }
Jordan Rose598ec092012-12-05 18:44:40 +00006623 } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) {
6624 // Special case for 'a', which has type 'int' in C.
6625 // Note, however, that we do /not/ want to treat multibyte constants like
6626 // 'MooV' as characters! This form is deprecated but still exists.
6627 if (ExprTy == S.Context.IntTy)
6628 if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue()))
6629 ExprTy = S.Context.CharTy;
Jordan Rose22b74712012-09-05 22:56:19 +00006630 }
Michael J. Spencer2c35bc12010-07-27 04:46:02 +00006631
Jordan Rosebc53ed12014-05-31 04:12:14 +00006632 // Look through enums to their underlying type.
6633 bool IsEnum = false;
6634 if (auto EnumTy = ExprTy->getAs<EnumType>()) {
6635 ExprTy = EnumTy->getDecl()->getIntegerType();
6636 IsEnum = true;
6637 }
6638
Jordan Rose0e5badd2012-12-05 18:44:49 +00006639 // %C in an Objective-C context prints a unichar, not a wchar_t.
6640 // If the argument is an integer of some kind, believe the %C and suggest
6641 // a cast instead of changing the conversion specifier.
Jordan Rose598ec092012-12-05 18:44:40 +00006642 QualType IntendedTy = ExprTy;
Mehdi Amini06d367c2016-10-24 20:39:34 +00006643 if (isObjCContext() &&
Jordan Rose0e5badd2012-12-05 18:44:49 +00006644 FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
6645 if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
6646 !ExprTy->isCharType()) {
6647 // 'unichar' is defined as a typedef of unsigned short, but we should
6648 // prefer using the typedef if it is visible.
6649 IntendedTy = S.Context.UnsignedShortTy;
Ted Kremenekda2f4052013-10-15 05:25:17 +00006650
6651 // While we are here, check if the value is an IntegerLiteral that happens
6652 // to be within the valid range.
6653 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(E)) {
6654 const llvm::APInt &V = IL->getValue();
6655 if (V.getActiveBits() <= S.Context.getTypeSize(IntendedTy))
6656 return true;
6657 }
6658
Jordan Rose0e5badd2012-12-05 18:44:49 +00006659 LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(),
6660 Sema::LookupOrdinaryName);
6661 if (S.LookupName(Result, S.getCurScope())) {
6662 NamedDecl *ND = Result.getFoundDecl();
6663 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
6664 if (TD->getUnderlyingType() == IntendedTy)
6665 IntendedTy = S.Context.getTypedefType(TD);
6666 }
6667 }
6668 }
6669
6670 // Special-case some of Darwin's platform-independence types by suggesting
6671 // casts to primitive types that are known to be large enough.
Anton Korobeynikov5f951ee2014-11-14 22:09:15 +00006672 bool ShouldNotPrintDirectly = false; StringRef CastTyName;
Jordan Roseaee34382012-09-05 22:56:26 +00006673 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Anton Korobeynikov5f951ee2014-11-14 22:09:15 +00006674 QualType CastTy;
6675 std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E);
6676 if (!CastTy.isNull()) {
6677 IntendedTy = CastTy;
6678 ShouldNotPrintDirectly = true;
Jordan Roseaee34382012-09-05 22:56:26 +00006679 }
6680 }
6681
Jordan Rose22b74712012-09-05 22:56:19 +00006682 // We may be able to offer a FixItHint if it is a supported type.
6683 PrintfSpecifier fixedFS = FS;
Mehdi Amini06d367c2016-10-24 20:39:34 +00006684 bool success =
6685 fixedFS.fixType(IntendedTy, S.getLangOpts(), S.Context, isObjCContext());
Michael J. Spencer2c35bc12010-07-27 04:46:02 +00006686
Jordan Rose22b74712012-09-05 22:56:19 +00006687 if (success) {
6688 // Get the fix string from the fixed format specifier
6689 SmallString<16> buf;
6690 llvm::raw_svector_ostream os(buf);
6691 fixedFS.toString(os);
Michael J. Spencer2c35bc12010-07-27 04:46:02 +00006692
Jordan Roseaee34382012-09-05 22:56:26 +00006693 CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
6694
Anton Korobeynikov5f951ee2014-11-14 22:09:15 +00006695 if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) {
Daniel Jasperad8d8492015-03-04 14:18:20 +00006696 unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
6697 if (match == analyze_format_string::ArgType::NoMatchPedantic) {
6698 diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
6699 }
Jordan Rose0e5badd2012-12-05 18:44:49 +00006700 // In this case, the specifier is wrong and should be changed to match
6701 // the argument.
Daniel Jasperad8d8492015-03-04 14:18:20 +00006702 EmitFormatDiagnostic(S.PDiag(diag)
6703 << AT.getRepresentativeTypeName(S.Context)
6704 << IntendedTy << IsEnum << E->getSourceRange(),
6705 E->getLocStart(),
6706 /*IsStringLocation*/ false, SpecRange,
6707 FixItHint::CreateReplacement(SpecRange, os.str()));
Jordan Rose0e5badd2012-12-05 18:44:49 +00006708 } else {
Jordan Roseaee34382012-09-05 22:56:26 +00006709 // The canonical type for formatting this value is different from the
6710 // actual type of the expression. (This occurs, for example, with Darwin's
6711 // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but
6712 // should be printed as 'long' for 64-bit compatibility.)
6713 // Rather than emitting a normal format/argument mismatch, we want to
6714 // add a cast to the recommended type (and correct the format string
6715 // if necessary).
6716 SmallString<16> CastBuf;
6717 llvm::raw_svector_ostream CastFix(CastBuf);
6718 CastFix << "(";
6719 IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
6720 CastFix << ")";
6721
6722 SmallVector<FixItHint,4> Hints;
Alexander Shaposhnikov1788a9b2017-09-22 18:36:06 +00006723 if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly)
Jordan Roseaee34382012-09-05 22:56:26 +00006724 Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
6725
6726 if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {
6727 // If there's already a cast present, just replace it.
6728 SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc());
6729 Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str()));
6730
6731 } else if (!requiresParensToAddCast(E)) {
6732 // If the expression has high enough precedence,
6733 // just write the C-style cast.
6734 Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
6735 CastFix.str()));
6736 } else {
6737 // Otherwise, add parens around the expression as well as the cast.
6738 CastFix << "(";
6739 Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
6740 CastFix.str()));
6741
Alp Tokerb6cc5922014-05-03 03:45:55 +00006742 SourceLocation After = S.getLocForEndOfToken(E->getLocEnd());
Jordan Roseaee34382012-09-05 22:56:26 +00006743 Hints.push_back(FixItHint::CreateInsertion(After, ")"));
6744 }
6745
Jordan Rose0e5badd2012-12-05 18:44:49 +00006746 if (ShouldNotPrintDirectly) {
6747 // The expression has a type that should not be printed directly.
6748 // We extract the name from the typedef because we don't want to show
6749 // the underlying type in the diagnostic.
Anton Korobeynikov5f951ee2014-11-14 22:09:15 +00006750 StringRef Name;
6751 if (const TypedefType *TypedefTy = dyn_cast<TypedefType>(ExprTy))
6752 Name = TypedefTy->getDecl()->getName();
6753 else
6754 Name = CastTyName;
Jordan Rose0e5badd2012-12-05 18:44:49 +00006755 EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast)
Jordan Rosebc53ed12014-05-31 04:12:14 +00006756 << Name << IntendedTy << IsEnum
Jordan Rose0e5badd2012-12-05 18:44:49 +00006757 << E->getSourceRange(),
6758 E->getLocStart(), /*IsStringLocation=*/false,
6759 SpecRange, Hints);
6760 } else {
6761 // In this case, the expression could be printed using a different
6762 // specifier, but we've decided that the specifier is probably correct
6763 // and we should cast instead. Just use the normal warning message.
6764 EmitFormatDiagnostic(
Jordan Rosebc53ed12014-05-31 04:12:14 +00006765 S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
6766 << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum
Jordan Rose0e5badd2012-12-05 18:44:49 +00006767 << E->getSourceRange(),
6768 E->getLocStart(), /*IsStringLocation*/false,
6769 SpecRange, Hints);
6770 }
Jordan Roseaee34382012-09-05 22:56:26 +00006771 }
Jordan Rose22b74712012-09-05 22:56:19 +00006772 } else {
6773 const CharSourceRange &CSR = getSpecifierRange(StartSpecifier,
6774 SpecifierLen);
6775 // Since the warning for passing non-POD types to variadic functions
6776 // was deferred until now, we emit a warning for non-POD
6777 // arguments here.
Richard Smithd7293d72013-08-05 18:49:43 +00006778 switch (S.isValidVarArgType(ExprTy)) {
6779 case Sema::VAK_Valid:
Seth Cantrellb4802962015-03-04 03:12:10 +00006780 case Sema::VAK_ValidInCXX11: {
6781 unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
6782 if (match == analyze_printf::ArgType::NoMatchPedantic) {
6783 diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
6784 }
Richard Smithd7293d72013-08-05 18:49:43 +00006785
Seth Cantrellb4802962015-03-04 03:12:10 +00006786 EmitFormatDiagnostic(
6787 S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy
6788 << IsEnum << CSR << E->getSourceRange(),
6789 E->getLocStart(), /*IsStringLocation*/ false, CSR);
6790 break;
6791 }
Richard Smithd7293d72013-08-05 18:49:43 +00006792 case Sema::VAK_Undefined:
Hans Wennborgd9dd4d22014-09-29 23:06:57 +00006793 case Sema::VAK_MSVCUndefined:
Richard Smithd7293d72013-08-05 18:49:43 +00006794 EmitFormatDiagnostic(
6795 S.PDiag(diag::warn_non_pod_vararg_with_format_string)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006796 << S.getLangOpts().CPlusPlus11
Jordan Rose598ec092012-12-05 18:44:40 +00006797 << ExprTy
Jordan Rose22b74712012-09-05 22:56:19 +00006798 << CallType
6799 << AT.getRepresentativeTypeName(S.Context)
6800 << CSR
6801 << E->getSourceRange(),
6802 E->getLocStart(), /*IsStringLocation*/false, CSR);
Richard Smith2868a732014-02-28 01:36:39 +00006803 checkForCStrMembers(AT, E);
Richard Smithd7293d72013-08-05 18:49:43 +00006804 break;
6805
6806 case Sema::VAK_Invalid:
6807 if (ExprTy->isObjCObjectType())
6808 EmitFormatDiagnostic(
6809 S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format)
6810 << S.getLangOpts().CPlusPlus11
6811 << ExprTy
6812 << CallType
6813 << AT.getRepresentativeTypeName(S.Context)
6814 << CSR
6815 << E->getSourceRange(),
6816 E->getLocStart(), /*IsStringLocation*/false, CSR);
6817 else
6818 // FIXME: If this is an initializer list, suggest removing the braces
6819 // or inserting a cast to the target type.
6820 S.Diag(E->getLocStart(), diag::err_cannot_pass_to_vararg_format)
6821 << isa<InitListExpr>(E) << ExprTy << CallType
6822 << AT.getRepresentativeTypeName(S.Context)
6823 << E->getSourceRange();
6824 break;
6825 }
6826
6827 assert(FirstDataArg + FS.getArgIndex() < CheckedVarArgs.size() &&
6828 "format string specifier index out of range");
6829 CheckedVarArgs[FirstDataArg + FS.getArgIndex()] = true;
Michael J. Spencer2c35bc12010-07-27 04:46:02 +00006830 }
6831
Ted Kremenekab278de2010-01-28 23:39:18 +00006832 return true;
6833}
6834
Ted Kremenek02087932010-07-16 02:11:22 +00006835//===--- CHECK: Scanf format string checking ------------------------------===//
6836
6837namespace {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00006838
Ted Kremenek02087932010-07-16 02:11:22 +00006839class CheckScanfHandler : public CheckFormatHandler {
6840public:
Stephen Hines648c3692016-09-16 01:07:04 +00006841 CheckScanfHandler(Sema &s, const FormatStringLiteral *fexpr,
Mehdi Amini06d367c2016-10-24 20:39:34 +00006842 const Expr *origFormatExpr, Sema::FormatStringType type,
6843 unsigned firstDataArg, unsigned numDataArgs,
6844 const char *beg, bool hasVAListArg,
6845 ArrayRef<const Expr *> Args, unsigned formatIdx,
6846 bool inFunctionCall, Sema::VariadicCallType CallType,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00006847 llvm::SmallBitVector &CheckedVarArgs,
6848 UncoveredArgHandler &UncoveredArg)
Mehdi Amini06d367c2016-10-24 20:39:34 +00006849 : CheckFormatHandler(s, fexpr, origFormatExpr, type, firstDataArg,
6850 numDataArgs, beg, hasVAListArg, Args, formatIdx,
6851 inFunctionCall, CallType, CheckedVarArgs,
6852 UncoveredArg) {}
6853
Ted Kremenek02087932010-07-16 02:11:22 +00006854 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
6855 const char *startSpecifier,
Craig Toppere14c0f82014-03-12 04:55:44 +00006856 unsigned specifierLen) override;
Ted Kremenekce815422010-07-19 21:25:57 +00006857
6858 bool HandleInvalidScanfConversionSpecifier(
6859 const analyze_scanf::ScanfSpecifier &FS,
6860 const char *startSpecifier,
Craig Toppere14c0f82014-03-12 04:55:44 +00006861 unsigned specifierLen) override;
Ted Kremenekd7b31cc2010-07-16 18:28:03 +00006862
Craig Toppere14c0f82014-03-12 04:55:44 +00006863 void HandleIncompleteScanList(const char *start, const char *end) override;
Ted Kremenek02087932010-07-16 02:11:22 +00006864};
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00006865
6866} // namespace
Ted Kremenekab278de2010-01-28 23:39:18 +00006867
Ted Kremenekd7b31cc2010-07-16 18:28:03 +00006868void CheckScanfHandler::HandleIncompleteScanList(const char *start,
6869 const char *end) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00006870 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
6871 getLocationOfByte(end), /*IsStringLocation*/true,
6872 getSpecifierRange(start, end - start));
Ted Kremenekd7b31cc2010-07-16 18:28:03 +00006873}
6874
Ted Kremenekce815422010-07-19 21:25:57 +00006875bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
6876 const analyze_scanf::ScanfSpecifier &FS,
6877 const char *startSpecifier,
6878 unsigned specifierLen) {
Ted Kremenekf03e6d852010-07-20 20:04:27 +00006879 const analyze_scanf::ScanfConversionSpecifier &CS =
Ted Kremenekce815422010-07-19 21:25:57 +00006880 FS.getConversionSpecifier();
6881
6882 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
6883 getLocationOfByte(CS.getStart()),
6884 startSpecifier, specifierLen,
6885 CS.getStart(), CS.getLength());
6886}
6887
Ted Kremenek02087932010-07-16 02:11:22 +00006888bool CheckScanfHandler::HandleScanfSpecifier(
6889 const analyze_scanf::ScanfSpecifier &FS,
6890 const char *startSpecifier,
6891 unsigned specifierLen) {
Ted Kremenek02087932010-07-16 02:11:22 +00006892 using namespace analyze_scanf;
6893 using namespace analyze_format_string;
6894
Ted Kremenekf03e6d852010-07-20 20:04:27 +00006895 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
Ted Kremenek02087932010-07-16 02:11:22 +00006896
Ted Kremenek6cd69422010-07-19 22:01:06 +00006897 // Handle case where '%' and '*' don't consume an argument. These shouldn't
6898 // be used to decide if we are using positional arguments consistently.
6899 if (FS.consumesDataArgument()) {
6900 if (atFirstArg) {
6901 atFirstArg = false;
6902 usesPositionalArgs = FS.usesPositionalArg();
6903 }
6904 else if (usesPositionalArgs != FS.usesPositionalArg()) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00006905 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
6906 startSpecifier, specifierLen);
Ted Kremenek6cd69422010-07-19 22:01:06 +00006907 return false;
6908 }
Ted Kremenek02087932010-07-16 02:11:22 +00006909 }
6910
6911 // Check if the field with is non-zero.
6912 const OptionalAmount &Amt = FS.getFieldWidth();
6913 if (Amt.getHowSpecified() == OptionalAmount::Constant) {
6914 if (Amt.getConstantAmount() == 0) {
6915 const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
6916 Amt.getConstantLength());
Richard Trieu03cf7b72011-10-28 00:41:25 +00006917 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
6918 getLocationOfByte(Amt.getStart()),
6919 /*IsStringLocation*/true, R,
6920 FixItHint::CreateRemoval(R));
Ted Kremenek02087932010-07-16 02:11:22 +00006921 }
6922 }
Seth Cantrellb4802962015-03-04 03:12:10 +00006923
Ted Kremenek02087932010-07-16 02:11:22 +00006924 if (!FS.consumesDataArgument()) {
6925 // FIXME: Technically specifying a precision or field width here
6926 // makes no sense. Worth issuing a warning at some point.
6927 return true;
6928 }
Seth Cantrellb4802962015-03-04 03:12:10 +00006929
Ted Kremenek02087932010-07-16 02:11:22 +00006930 // Consume the argument.
6931 unsigned argIndex = FS.getArgIndex();
6932 if (argIndex < NumDataArgs) {
6933 // The check to see if the argIndex is valid will come later.
6934 // We set the bit here because we may exit early from this
6935 // function if we encounter some other error.
6936 CoveredArgs.set(argIndex);
6937 }
Seth Cantrellb4802962015-03-04 03:12:10 +00006938
Ted Kremenek4407ea42010-07-20 20:04:47 +00006939 // Check the length modifier is valid with the given conversion specifier.
Jordan Rose92303592012-09-08 04:00:03 +00006940 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
Jordan Rose2f9cc042012-09-08 04:00:12 +00006941 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
6942 diag::warn_format_nonsensical_length);
Jordan Rose92303592012-09-08 04:00:03 +00006943 else if (!FS.hasStandardLengthModifier())
Jordan Rose2f9cc042012-09-08 04:00:12 +00006944 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
Jordan Rose92303592012-09-08 04:00:03 +00006945 else if (!FS.hasStandardLengthConversionCombination())
Jordan Rose2f9cc042012-09-08 04:00:12 +00006946 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
6947 diag::warn_format_non_standard_conversion_spec);
Hans Wennborgc9dd9462012-02-22 10:17:01 +00006948
Jordan Rose92303592012-09-08 04:00:03 +00006949 if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
6950 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
6951
Ted Kremenek02087932010-07-16 02:11:22 +00006952 // The remaining checks depend on the data arguments.
6953 if (HasVAListArg)
6954 return true;
Seth Cantrellb4802962015-03-04 03:12:10 +00006955
Ted Kremenek6adb7e32010-07-26 19:45:42 +00006956 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
Ted Kremenek02087932010-07-16 02:11:22 +00006957 return false;
Seth Cantrellb4802962015-03-04 03:12:10 +00006958
Hans Wennborgb1a5e092011-12-10 13:20:11 +00006959 // Check that the argument type matches the format specifier.
6960 const Expr *Ex = getDataArg(argIndex);
Jordan Rose58bbe422012-07-19 18:10:08 +00006961 if (!Ex)
6962 return true;
6963
Hans Wennborgb1ab2a82012-08-07 08:59:46 +00006964 const analyze_format_string::ArgType &AT = FS.getArgType(S.Context);
Seth Cantrell79340072015-03-04 05:58:08 +00006965
6966 if (!AT.isValid()) {
6967 return true;
6968 }
6969
Seth Cantrellb4802962015-03-04 03:12:10 +00006970 analyze_format_string::ArgType::MatchKind match =
6971 AT.matchesType(S.Context, Ex->getType());
Seth Cantrell79340072015-03-04 05:58:08 +00006972 if (match == analyze_format_string::ArgType::Match) {
6973 return true;
6974 }
Seth Cantrellb4802962015-03-04 03:12:10 +00006975
Seth Cantrell79340072015-03-04 05:58:08 +00006976 ScanfSpecifier fixedFS = FS;
6977 bool success = fixedFS.fixType(Ex->getType(), Ex->IgnoreImpCasts()->getType(),
6978 S.getLangOpts(), S.Context);
Hans Wennborgb1a5e092011-12-10 13:20:11 +00006979
Seth Cantrell79340072015-03-04 05:58:08 +00006980 unsigned diag = diag::warn_format_conversion_argument_type_mismatch;
6981 if (match == analyze_format_string::ArgType::NoMatchPedantic) {
6982 diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
6983 }
Hans Wennborgb1a5e092011-12-10 13:20:11 +00006984
Seth Cantrell79340072015-03-04 05:58:08 +00006985 if (success) {
6986 // Get the fix string from the fixed format specifier.
6987 SmallString<128> buf;
6988 llvm::raw_svector_ostream os(buf);
6989 fixedFS.toString(os);
6990
6991 EmitFormatDiagnostic(
6992 S.PDiag(diag) << AT.getRepresentativeTypeName(S.Context)
6993 << Ex->getType() << false << Ex->getSourceRange(),
6994 Ex->getLocStart(),
6995 /*IsStringLocation*/ false,
6996 getSpecifierRange(startSpecifier, specifierLen),
6997 FixItHint::CreateReplacement(
6998 getSpecifierRange(startSpecifier, specifierLen), os.str()));
6999 } else {
7000 EmitFormatDiagnostic(S.PDiag(diag)
7001 << AT.getRepresentativeTypeName(S.Context)
7002 << Ex->getType() << false << Ex->getSourceRange(),
7003 Ex->getLocStart(),
7004 /*IsStringLocation*/ false,
7005 getSpecifierRange(startSpecifier, specifierLen));
Hans Wennborgb1a5e092011-12-10 13:20:11 +00007006 }
7007
Ted Kremenek02087932010-07-16 02:11:22 +00007008 return true;
7009}
7010
Stephen Hines648c3692016-09-16 01:07:04 +00007011static void CheckFormatString(Sema &S, const FormatStringLiteral *FExpr,
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007012 const Expr *OrigFormatExpr,
7013 ArrayRef<const Expr *> Args,
7014 bool HasVAListArg, unsigned format_idx,
7015 unsigned firstDataArg,
7016 Sema::FormatStringType Type,
7017 bool inFunctionCall,
7018 Sema::VariadicCallType CallType,
Andy Gibbs9a31b3b2016-02-26 15:35:16 +00007019 llvm::SmallBitVector &CheckedVarArgs,
7020 UncoveredArgHandler &UncoveredArg) {
Ted Kremenekab278de2010-01-28 23:39:18 +00007021 // CHECK: is the format string a wide literal?
Richard Smith4060f772012-06-13 05:37:23 +00007022 if (!FExpr->isAscii() && !FExpr->isUTF8()) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00007023 CheckFormatHandler::EmitFormatDiagnostic(
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007024 S, inFunctionCall, Args[format_idx],
7025 S.PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(),
Richard Trieu03cf7b72011-10-28 00:41:25 +00007026 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
Ted Kremenekab278de2010-01-28 23:39:18 +00007027 return;
7028 }
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007029
Ted Kremenekab278de2010-01-28 23:39:18 +00007030 // Str - The format string. NOTE: this is NOT null-terminated!
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007031 StringRef StrRef = FExpr->getString();
Benjamin Kramer35b077e2010-08-17 12:54:38 +00007032 const char *Str = StrRef.data();
Benjamin Kramer6c6a4f42014-02-20 17:05:38 +00007033 // Account for cases where the string literal is truncated in a declaration.
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007034 const ConstantArrayType *T =
7035 S.Context.getAsConstantArrayType(FExpr->getType());
Benjamin Kramer6c6a4f42014-02-20 17:05:38 +00007036 assert(T && "String literal not of constant array type!");
7037 size_t TypeSize = T->getSize().getZExtValue();
7038 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
Dmitri Gribenko765396f2013-01-13 20:46:02 +00007039 const unsigned numDataArgs = Args.size() - firstDataArg;
Benjamin Kramer6c6a4f42014-02-20 17:05:38 +00007040
7041 // Emit a warning if the string literal is truncated and does not contain an
7042 // embedded null character.
7043 if (TypeSize <= StrRef.size() &&
7044 StrRef.substr(0, TypeSize).find('\0') == StringRef::npos) {
7045 CheckFormatHandler::EmitFormatDiagnostic(
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007046 S, inFunctionCall, Args[format_idx],
7047 S.PDiag(diag::warn_printf_format_string_not_null_terminated),
Benjamin Kramer6c6a4f42014-02-20 17:05:38 +00007048 FExpr->getLocStart(),
7049 /*IsStringLocation=*/true, OrigFormatExpr->getSourceRange());
7050 return;
7051 }
7052
Ted Kremenekab278de2010-01-28 23:39:18 +00007053 // CHECK: empty format string?
Ted Kremenek6e302b22011-09-29 05:52:16 +00007054 if (StrLen == 0 && numDataArgs > 0) {
Richard Trieu03cf7b72011-10-28 00:41:25 +00007055 CheckFormatHandler::EmitFormatDiagnostic(
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007056 S, inFunctionCall, Args[format_idx],
7057 S.PDiag(diag::warn_empty_format_string), FExpr->getLocStart(),
Richard Trieu03cf7b72011-10-28 00:41:25 +00007058 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
Ted Kremenekab278de2010-01-28 23:39:18 +00007059 return;
7060 }
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007061
7062 if (Type == Sema::FST_Printf || Type == Sema::FST_NSString ||
Mehdi Amini06d367c2016-10-24 20:39:34 +00007063 Type == Sema::FST_FreeBSDKPrintf || Type == Sema::FST_OSLog ||
7064 Type == Sema::FST_OSTrace) {
7065 CheckPrintfHandler H(
7066 S, FExpr, OrigFormatExpr, Type, firstDataArg, numDataArgs,
7067 (Type == Sema::FST_NSString || Type == Sema::FST_OSTrace), Str,
7068 HasVAListArg, Args, format_idx, inFunctionCall, CallType,
7069 CheckedVarArgs, UncoveredArg);
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007070
Hans Wennborg23926bd2011-12-15 10:25:47 +00007071 if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007072 S.getLangOpts(),
7073 S.Context.getTargetInfo(),
7074 Type == Sema::FST_FreeBSDKPrintf))
Ted Kremenek02087932010-07-16 02:11:22 +00007075 H.DoneProcessing();
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007076 } else if (Type == Sema::FST_Scanf) {
Mehdi Amini06d367c2016-10-24 20:39:34 +00007077 CheckScanfHandler H(S, FExpr, OrigFormatExpr, Type, firstDataArg,
7078 numDataArgs, Str, HasVAListArg, Args, format_idx,
7079 inFunctionCall, CallType, CheckedVarArgs, UncoveredArg);
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007080
Hans Wennborg23926bd2011-12-15 10:25:47 +00007081 if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen,
Andy Gibbs4b3e3c82016-02-22 13:00:43 +00007082 S.getLangOpts(),
7083 S.Context.getTargetInfo()))
Ted Kremenek02087932010-07-16 02:11:22 +00007084 H.DoneProcessing();
Jean-Daniel Dupas028573e72012-01-30 08:46:47 +00007085 } // TODO: handle other formats
Ted Kremenekc70ee862010-01-28 01:18:22 +00007086}
7087
Fariborz Jahanian6485fe42014-09-09 23:10:54 +00007088bool Sema::FormatStringHasSArg(const StringLiteral *FExpr) {
7089 // Str - The format string. NOTE: this is NOT null-terminated!
7090 StringRef StrRef = FExpr->getString();
7091 const char *Str = StrRef.data();
7092 // Account for cases where the string literal is truncated in a declaration.
7093 const ConstantArrayType *T = Context.getAsConstantArrayType(FExpr->getType());
7094 assert(T && "String literal not of constant array type!");
7095 size_t TypeSize = T->getSize().getZExtValue();
7096 size_t StrLen = std::min(std::max(TypeSize, size_t(1)) - 1, StrRef.size());
7097 return analyze_format_string::ParseFormatStringHasSArg(Str, Str + StrLen,
7098 getLangOpts(),
7099 Context.getTargetInfo());
7100}
7101
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007102//===--- CHECK: Warn on use of wrong absolute value function. -------------===//
7103
7104// Returns the related absolute value function that is larger, of 0 if one
7105// does not exist.
7106static unsigned getLargerAbsoluteValueFunction(unsigned AbsFunction) {
7107 switch (AbsFunction) {
7108 default:
7109 return 0;
7110
7111 case Builtin::BI__builtin_abs:
7112 return Builtin::BI__builtin_labs;
7113 case Builtin::BI__builtin_labs:
7114 return Builtin::BI__builtin_llabs;
7115 case Builtin::BI__builtin_llabs:
7116 return 0;
7117
7118 case Builtin::BI__builtin_fabsf:
7119 return Builtin::BI__builtin_fabs;
7120 case Builtin::BI__builtin_fabs:
7121 return Builtin::BI__builtin_fabsl;
7122 case Builtin::BI__builtin_fabsl:
7123 return 0;
7124
7125 case Builtin::BI__builtin_cabsf:
7126 return Builtin::BI__builtin_cabs;
7127 case Builtin::BI__builtin_cabs:
7128 return Builtin::BI__builtin_cabsl;
7129 case Builtin::BI__builtin_cabsl:
7130 return 0;
7131
7132 case Builtin::BIabs:
7133 return Builtin::BIlabs;
7134 case Builtin::BIlabs:
7135 return Builtin::BIllabs;
7136 case Builtin::BIllabs:
7137 return 0;
7138
7139 case Builtin::BIfabsf:
7140 return Builtin::BIfabs;
7141 case Builtin::BIfabs:
7142 return Builtin::BIfabsl;
7143 case Builtin::BIfabsl:
7144 return 0;
7145
7146 case Builtin::BIcabsf:
7147 return Builtin::BIcabs;
7148 case Builtin::BIcabs:
7149 return Builtin::BIcabsl;
7150 case Builtin::BIcabsl:
7151 return 0;
7152 }
7153}
7154
7155// Returns the argument type of the absolute value function.
7156static QualType getAbsoluteValueArgumentType(ASTContext &Context,
7157 unsigned AbsType) {
7158 if (AbsType == 0)
7159 return QualType();
7160
7161 ASTContext::GetBuiltinTypeError Error = ASTContext::GE_None;
7162 QualType BuiltinType = Context.GetBuiltinType(AbsType, Error);
7163 if (Error != ASTContext::GE_None)
7164 return QualType();
7165
7166 const FunctionProtoType *FT = BuiltinType->getAs<FunctionProtoType>();
7167 if (!FT)
7168 return QualType();
7169
7170 if (FT->getNumParams() != 1)
7171 return QualType();
7172
7173 return FT->getParamType(0);
7174}
7175
7176// Returns the best absolute value function, or zero, based on type and
7177// current absolute value function.
7178static unsigned getBestAbsFunction(ASTContext &Context, QualType ArgType,
7179 unsigned AbsFunctionKind) {
7180 unsigned BestKind = 0;
7181 uint64_t ArgSize = Context.getTypeSize(ArgType);
7182 for (unsigned Kind = AbsFunctionKind; Kind != 0;
7183 Kind = getLargerAbsoluteValueFunction(Kind)) {
7184 QualType ParamType = getAbsoluteValueArgumentType(Context, Kind);
7185 if (Context.getTypeSize(ParamType) >= ArgSize) {
7186 if (BestKind == 0)
7187 BestKind = Kind;
7188 else if (Context.hasSameType(ParamType, ArgType)) {
7189 BestKind = Kind;
7190 break;
7191 }
7192 }
7193 }
7194 return BestKind;
7195}
7196
7197enum AbsoluteValueKind {
7198 AVK_Integer,
7199 AVK_Floating,
7200 AVK_Complex
7201};
7202
7203static AbsoluteValueKind getAbsoluteValueKind(QualType T) {
7204 if (T->isIntegralOrEnumerationType())
7205 return AVK_Integer;
7206 if (T->isRealFloatingType())
7207 return AVK_Floating;
7208 if (T->isAnyComplexType())
7209 return AVK_Complex;
7210
7211 llvm_unreachable("Type not integer, floating, or complex");
7212}
7213
7214// Changes the absolute value function to a different type. Preserves whether
7215// the function is a builtin.
7216static unsigned changeAbsFunction(unsigned AbsKind,
7217 AbsoluteValueKind ValueKind) {
7218 switch (ValueKind) {
7219 case AVK_Integer:
7220 switch (AbsKind) {
7221 default:
7222 return 0;
7223 case Builtin::BI__builtin_fabsf:
7224 case Builtin::BI__builtin_fabs:
7225 case Builtin::BI__builtin_fabsl:
7226 case Builtin::BI__builtin_cabsf:
7227 case Builtin::BI__builtin_cabs:
7228 case Builtin::BI__builtin_cabsl:
7229 return Builtin::BI__builtin_abs;
7230 case Builtin::BIfabsf:
7231 case Builtin::BIfabs:
7232 case Builtin::BIfabsl:
7233 case Builtin::BIcabsf:
7234 case Builtin::BIcabs:
7235 case Builtin::BIcabsl:
7236 return Builtin::BIabs;
7237 }
7238 case AVK_Floating:
7239 switch (AbsKind) {
7240 default:
7241 return 0;
7242 case Builtin::BI__builtin_abs:
7243 case Builtin::BI__builtin_labs:
7244 case Builtin::BI__builtin_llabs:
7245 case Builtin::BI__builtin_cabsf:
7246 case Builtin::BI__builtin_cabs:
7247 case Builtin::BI__builtin_cabsl:
7248 return Builtin::BI__builtin_fabsf;
7249 case Builtin::BIabs:
7250 case Builtin::BIlabs:
7251 case Builtin::BIllabs:
7252 case Builtin::BIcabsf:
7253 case Builtin::BIcabs:
7254 case Builtin::BIcabsl:
7255 return Builtin::BIfabsf;
7256 }
7257 case AVK_Complex:
7258 switch (AbsKind) {
7259 default:
7260 return 0;
7261 case Builtin::BI__builtin_abs:
7262 case Builtin::BI__builtin_labs:
7263 case Builtin::BI__builtin_llabs:
7264 case Builtin::BI__builtin_fabsf:
7265 case Builtin::BI__builtin_fabs:
7266 case Builtin::BI__builtin_fabsl:
7267 return Builtin::BI__builtin_cabsf;
7268 case Builtin::BIabs:
7269 case Builtin::BIlabs:
7270 case Builtin::BIllabs:
7271 case Builtin::BIfabsf:
7272 case Builtin::BIfabs:
7273 case Builtin::BIfabsl:
7274 return Builtin::BIcabsf;
7275 }
7276 }
7277 llvm_unreachable("Unable to convert function");
7278}
7279
Benjamin Kramer3d6220d2014-03-01 17:21:22 +00007280static unsigned getAbsoluteValueFunctionKind(const FunctionDecl *FDecl) {
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007281 const IdentifierInfo *FnInfo = FDecl->getIdentifier();
7282 if (!FnInfo)
7283 return 0;
7284
7285 switch (FDecl->getBuiltinID()) {
7286 default:
7287 return 0;
7288 case Builtin::BI__builtin_abs:
7289 case Builtin::BI__builtin_fabs:
7290 case Builtin::BI__builtin_fabsf:
7291 case Builtin::BI__builtin_fabsl:
7292 case Builtin::BI__builtin_labs:
7293 case Builtin::BI__builtin_llabs:
7294 case Builtin::BI__builtin_cabs:
7295 case Builtin::BI__builtin_cabsf:
7296 case Builtin::BI__builtin_cabsl:
7297 case Builtin::BIabs:
7298 case Builtin::BIlabs:
7299 case Builtin::BIllabs:
7300 case Builtin::BIfabs:
7301 case Builtin::BIfabsf:
7302 case Builtin::BIfabsl:
7303 case Builtin::BIcabs:
7304 case Builtin::BIcabsf:
7305 case Builtin::BIcabsl:
7306 return FDecl->getBuiltinID();
7307 }
7308 llvm_unreachable("Unknown Builtin type");
7309}
7310
7311// If the replacement is valid, emit a note with replacement function.
7312// Additionally, suggest including the proper header if not already included.
7313static void emitReplacement(Sema &S, SourceLocation Loc, SourceRange Range,
Richard Trieubeffb832014-04-15 23:47:53 +00007314 unsigned AbsKind, QualType ArgType) {
7315 bool EmitHeaderHint = true;
Craig Topperc3ec1492014-05-26 06:22:03 +00007316 const char *HeaderName = nullptr;
Mehdi Amini7186a432016-10-11 19:04:24 +00007317 const char *FunctionName = nullptr;
Richard Trieubeffb832014-04-15 23:47:53 +00007318 if (S.getLangOpts().CPlusPlus && !ArgType->isAnyComplexType()) {
7319 FunctionName = "std::abs";
7320 if (ArgType->isIntegralOrEnumerationType()) {
7321 HeaderName = "cstdlib";
7322 } else if (ArgType->isRealFloatingType()) {
7323 HeaderName = "cmath";
7324 } else {
7325 llvm_unreachable("Invalid Type");
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007326 }
Richard Trieubeffb832014-04-15 23:47:53 +00007327
7328 // Lookup all std::abs
7329 if (NamespaceDecl *Std = S.getStdNamespace()) {
Alp Tokerb6cc5922014-05-03 03:45:55 +00007330 LookupResult R(S, &S.Context.Idents.get("abs"), Loc, Sema::LookupAnyName);
Richard Trieubeffb832014-04-15 23:47:53 +00007331 R.suppressDiagnostics();
7332 S.LookupQualifiedName(R, Std);
7333
7334 for (const auto *I : R) {
Craig Topperc3ec1492014-05-26 06:22:03 +00007335 const FunctionDecl *FDecl = nullptr;
Richard Trieubeffb832014-04-15 23:47:53 +00007336 if (const UsingShadowDecl *UsingD = dyn_cast<UsingShadowDecl>(I)) {
7337 FDecl = dyn_cast<FunctionDecl>(UsingD->getTargetDecl());
7338 } else {
7339 FDecl = dyn_cast<FunctionDecl>(I);
7340 }
7341 if (!FDecl)
7342 continue;
7343
7344 // Found std::abs(), check that they are the right ones.
7345 if (FDecl->getNumParams() != 1)
7346 continue;
7347
7348 // Check that the parameter type can handle the argument.
7349 QualType ParamType = FDecl->getParamDecl(0)->getType();
7350 if (getAbsoluteValueKind(ArgType) == getAbsoluteValueKind(ParamType) &&
7351 S.Context.getTypeSize(ArgType) <=
7352 S.Context.getTypeSize(ParamType)) {
7353 // Found a function, don't need the header hint.
7354 EmitHeaderHint = false;
7355 break;
7356 }
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007357 }
Richard Trieubeffb832014-04-15 23:47:53 +00007358 }
7359 } else {
Eric Christopher02d5d862015-08-06 01:01:12 +00007360 FunctionName = S.Context.BuiltinInfo.getName(AbsKind);
Richard Trieubeffb832014-04-15 23:47:53 +00007361 HeaderName = S.Context.BuiltinInfo.getHeaderName(AbsKind);
7362
7363 if (HeaderName) {
7364 DeclarationName DN(&S.Context.Idents.get(FunctionName));
7365 LookupResult R(S, DN, Loc, Sema::LookupAnyName);
7366 R.suppressDiagnostics();
7367 S.LookupName(R, S.getCurScope());
7368
7369 if (R.isSingleResult()) {
7370 FunctionDecl *FD = dyn_cast<FunctionDecl>(R.getFoundDecl());
7371 if (FD && FD->getBuiltinID() == AbsKind) {
7372 EmitHeaderHint = false;
7373 } else {
7374 return;
7375 }
7376 } else if (!R.empty()) {
7377 return;
7378 }
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007379 }
7380 }
7381
7382 S.Diag(Loc, diag::note_replace_abs_function)
Richard Trieubeffb832014-04-15 23:47:53 +00007383 << FunctionName << FixItHint::CreateReplacement(Range, FunctionName);
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007384
Richard Trieubeffb832014-04-15 23:47:53 +00007385 if (!HeaderName)
7386 return;
7387
7388 if (!EmitHeaderHint)
7389 return;
7390
Alp Toker5d96e0a2014-07-11 20:53:51 +00007391 S.Diag(Loc, diag::note_include_header_or_declare) << HeaderName
7392 << FunctionName;
Richard Trieubeffb832014-04-15 23:47:53 +00007393}
7394
Richard Trieua7f30b12016-12-06 01:42:28 +00007395template <std::size_t StrLen>
7396static bool IsStdFunction(const FunctionDecl *FDecl,
7397 const char (&Str)[StrLen]) {
Richard Trieubeffb832014-04-15 23:47:53 +00007398 if (!FDecl)
7399 return false;
Richard Trieua7f30b12016-12-06 01:42:28 +00007400 if (!FDecl->getIdentifier() || !FDecl->getIdentifier()->isStr(Str))
Richard Trieubeffb832014-04-15 23:47:53 +00007401 return false;
Richard Trieua7f30b12016-12-06 01:42:28 +00007402 if (!FDecl->isInStdNamespace())
Richard Trieubeffb832014-04-15 23:47:53 +00007403 return false;
7404
7405 return true;
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007406}
7407
7408// Warn when using the wrong abs() function.
7409void Sema::CheckAbsoluteValueFunction(const CallExpr *Call,
Richard Trieua7f30b12016-12-06 01:42:28 +00007410 const FunctionDecl *FDecl) {
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007411 if (Call->getNumArgs() != 1)
7412 return;
7413
7414 unsigned AbsKind = getAbsoluteValueFunctionKind(FDecl);
Richard Trieua7f30b12016-12-06 01:42:28 +00007415 bool IsStdAbs = IsStdFunction(FDecl, "abs");
Richard Trieubeffb832014-04-15 23:47:53 +00007416 if (AbsKind == 0 && !IsStdAbs)
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007417 return;
7418
7419 QualType ArgType = Call->getArg(0)->IgnoreParenImpCasts()->getType();
7420 QualType ParamType = Call->getArg(0)->getType();
7421
Alp Toker5d96e0a2014-07-11 20:53:51 +00007422 // Unsigned types cannot be negative. Suggest removing the absolute value
7423 // function call.
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007424 if (ArgType->isUnsignedIntegerType()) {
Mehdi Amini7186a432016-10-11 19:04:24 +00007425 const char *FunctionName =
Eric Christopher02d5d862015-08-06 01:01:12 +00007426 IsStdAbs ? "std::abs" : Context.BuiltinInfo.getName(AbsKind);
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007427 Diag(Call->getExprLoc(), diag::warn_unsigned_abs) << ArgType << ParamType;
7428 Diag(Call->getExprLoc(), diag::note_remove_abs)
Richard Trieubeffb832014-04-15 23:47:53 +00007429 << FunctionName
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007430 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange());
7431 return;
7432 }
7433
David Majnemer7f77eb92015-11-15 03:04:34 +00007434 // Taking the absolute value of a pointer is very suspicious, they probably
7435 // wanted to index into an array, dereference a pointer, call a function, etc.
7436 if (ArgType->isPointerType() || ArgType->canDecayToPointerType()) {
7437 unsigned DiagType = 0;
7438 if (ArgType->isFunctionType())
7439 DiagType = 1;
7440 else if (ArgType->isArrayType())
7441 DiagType = 2;
7442
7443 Diag(Call->getExprLoc(), diag::warn_pointer_abs) << DiagType << ArgType;
7444 return;
7445 }
7446
Richard Trieubeffb832014-04-15 23:47:53 +00007447 // std::abs has overloads which prevent most of the absolute value problems
7448 // from occurring.
7449 if (IsStdAbs)
7450 return;
7451
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007452 AbsoluteValueKind ArgValueKind = getAbsoluteValueKind(ArgType);
7453 AbsoluteValueKind ParamValueKind = getAbsoluteValueKind(ParamType);
7454
7455 // The argument and parameter are the same kind. Check if they are the right
7456 // size.
7457 if (ArgValueKind == ParamValueKind) {
7458 if (Context.getTypeSize(ArgType) <= Context.getTypeSize(ParamType))
7459 return;
7460
7461 unsigned NewAbsKind = getBestAbsFunction(Context, ArgType, AbsKind);
7462 Diag(Call->getExprLoc(), diag::warn_abs_too_small)
7463 << FDecl << ArgType << ParamType;
7464
7465 if (NewAbsKind == 0)
7466 return;
7467
7468 emitReplacement(*this, Call->getExprLoc(),
Richard Trieubeffb832014-04-15 23:47:53 +00007469 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007470 return;
7471 }
7472
7473 // ArgValueKind != ParamValueKind
7474 // The wrong type of absolute value function was used. Attempt to find the
7475 // proper one.
7476 unsigned NewAbsKind = changeAbsFunction(AbsKind, ArgValueKind);
7477 NewAbsKind = getBestAbsFunction(Context, ArgType, NewAbsKind);
7478 if (NewAbsKind == 0)
7479 return;
7480
7481 Diag(Call->getExprLoc(), diag::warn_wrong_absolute_value_type)
7482 << FDecl << ParamValueKind << ArgValueKind;
7483
7484 emitReplacement(*this, Call->getExprLoc(),
Richard Trieubeffb832014-04-15 23:47:53 +00007485 Call->getCallee()->getSourceRange(), NewAbsKind, ArgType);
Richard Trieu7eb0b2c2014-02-26 01:17:28 +00007486}
7487
Richard Trieu67c00712016-12-05 23:41:46 +00007488//===--- CHECK: Warn on use of std::max and unsigned zero. r---------------===//
Richard Trieua7f30b12016-12-06 01:42:28 +00007489void Sema::CheckMaxUnsignedZero(const CallExpr *Call,
7490 const FunctionDecl *FDecl) {
Richard Trieu67c00712016-12-05 23:41:46 +00007491 if (!Call || !FDecl) return;
7492
7493 // Ignore template specializations and macros.
Richard Smith51ec0cf2017-02-21 01:17:38 +00007494 if (inTemplateInstantiation()) return;
Richard Trieu67c00712016-12-05 23:41:46 +00007495 if (Call->getExprLoc().isMacroID()) return;
7496
7497 // Only care about the one template argument, two function parameter std::max
7498 if (Call->getNumArgs() != 2) return;
Richard Trieua7f30b12016-12-06 01:42:28 +00007499 if (!IsStdFunction(FDecl, "max")) return;
Richard Trieu67c00712016-12-05 23:41:46 +00007500 const auto * ArgList = FDecl->getTemplateSpecializationArgs();
7501 if (!ArgList) return;
7502 if (ArgList->size() != 1) return;
7503
7504 // Check that template type argument is unsigned integer.
7505 const auto& TA = ArgList->get(0);
7506 if (TA.getKind() != TemplateArgument::Type) return;
7507 QualType ArgType = TA.getAsType();
7508 if (!ArgType->isUnsignedIntegerType()) return;
7509
7510 // See if either argument is a literal zero.
7511 auto IsLiteralZeroArg = [](const Expr* E) -> bool {
7512 const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E);
7513 if (!MTE) return false;
7514 const auto *Num = dyn_cast<IntegerLiteral>(MTE->GetTemporaryExpr());
7515 if (!Num) return false;
7516 if (Num->getValue() != 0) return false;
7517 return true;
7518 };
7519
7520 const Expr *FirstArg = Call->getArg(0);
7521 const Expr *SecondArg = Call->getArg(1);
7522 const bool IsFirstArgZero = IsLiteralZeroArg(FirstArg);
7523 const bool IsSecondArgZero = IsLiteralZeroArg(SecondArg);
7524
7525 // Only warn when exactly one argument is zero.
7526 if (IsFirstArgZero == IsSecondArgZero) return;
7527
7528 SourceRange FirstRange = FirstArg->getSourceRange();
7529 SourceRange SecondRange = SecondArg->getSourceRange();
7530
7531 SourceRange ZeroRange = IsFirstArgZero ? FirstRange : SecondRange;
7532
7533 Diag(Call->getExprLoc(), diag::warn_max_unsigned_zero)
7534 << IsFirstArgZero << Call->getCallee()->getSourceRange() << ZeroRange;
7535
7536 // Deduce what parts to remove so that "std::max(0u, foo)" becomes "(foo)".
7537 SourceRange RemovalRange;
7538 if (IsFirstArgZero) {
7539 RemovalRange = SourceRange(FirstRange.getBegin(),
7540 SecondRange.getBegin().getLocWithOffset(-1));
7541 } else {
7542 RemovalRange = SourceRange(getLocForEndOfToken(FirstRange.getEnd()),
7543 SecondRange.getEnd());
7544 }
7545
7546 Diag(Call->getExprLoc(), diag::note_remove_max_call)
7547 << FixItHint::CreateRemoval(Call->getCallee()->getSourceRange())
7548 << FixItHint::CreateRemoval(RemovalRange);
7549}
7550
Chandler Carruth53caa4d2011-04-27 07:05:31 +00007551//===--- CHECK: Standard memory functions ---------------------------------===//
7552
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007553/// Takes the expression passed to the size_t parameter of functions
Nico Weber0e6daef2013-12-26 23:38:39 +00007554/// such as memcmp, strncat, etc and warns if it's a comparison.
7555///
7556/// This is to catch typos like `if (memcmp(&a, &b, sizeof(a) > 0))`.
7557static bool CheckMemorySizeofForComparison(Sema &S, const Expr *E,
7558 IdentifierInfo *FnName,
7559 SourceLocation FnLoc,
7560 SourceLocation RParenLoc) {
7561 const BinaryOperator *Size = dyn_cast<BinaryOperator>(E);
7562 if (!Size)
7563 return false;
7564
Richard Smithc70f1d62017-12-14 15:16:18 +00007565 // if E is binop and op is <=>, >, <, >=, <=, ==, &&, ||:
7566 if (!Size->isComparisonOp() && !Size->isLogicalOp())
Nico Weber0e6daef2013-12-26 23:38:39 +00007567 return false;
7568
Nico Weber0e6daef2013-12-26 23:38:39 +00007569 SourceRange SizeRange = Size->getSourceRange();
7570 S.Diag(Size->getOperatorLoc(), diag::warn_memsize_comparison)
7571 << SizeRange << FnName;
Alp Tokerb0869032014-05-17 01:13:18 +00007572 S.Diag(FnLoc, diag::note_memsize_comparison_paren)
Alp Tokerb6cc5922014-05-03 03:45:55 +00007573 << FnName << FixItHint::CreateInsertion(
7574 S.getLocForEndOfToken(Size->getLHS()->getLocEnd()), ")")
Nico Weber0e6daef2013-12-26 23:38:39 +00007575 << FixItHint::CreateRemoval(RParenLoc);
Alp Tokerb0869032014-05-17 01:13:18 +00007576 S.Diag(SizeRange.getBegin(), diag::note_memsize_comparison_cast_silence)
Nico Weber0e6daef2013-12-26 23:38:39 +00007577 << FixItHint::CreateInsertion(SizeRange.getBegin(), "(size_t)(")
Alp Tokerb6cc5922014-05-03 03:45:55 +00007578 << FixItHint::CreateInsertion(S.getLocForEndOfToken(SizeRange.getEnd()),
7579 ")");
Nico Weber0e6daef2013-12-26 23:38:39 +00007580
7581 return true;
7582}
7583
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007584/// Determine whether the given type is or contains a dynamic class type
Reid Kleckner5fb5b122014-06-27 23:58:21 +00007585/// (e.g., whether it has a vtable).
7586static const CXXRecordDecl *getContainedDynamicClass(QualType T,
7587 bool &IsContained) {
7588 // Look through array types while ignoring qualifiers.
7589 const Type *Ty = T->getBaseElementTypeUnsafe();
7590 IsContained = false;
7591
7592 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
7593 RD = RD ? RD->getDefinition() : nullptr;
Richard Trieu1c7237a2016-03-31 04:18:07 +00007594 if (!RD || RD->isInvalidDecl())
Reid Kleckner5fb5b122014-06-27 23:58:21 +00007595 return nullptr;
7596
7597 if (RD->isDynamicClass())
7598 return RD;
7599
7600 // Check all the fields. If any bases were dynamic, the class is dynamic.
7601 // It's impossible for a class to transitively contain itself by value, so
7602 // infinite recursion is impossible.
7603 for (auto *FD : RD->fields()) {
7604 bool SubContained;
7605 if (const CXXRecordDecl *ContainedRD =
7606 getContainedDynamicClass(FD->getType(), SubContained)) {
7607 IsContained = true;
7608 return ContainedRD;
7609 }
7610 }
7611
7612 return nullptr;
Douglas Gregora74926b2011-05-03 20:05:22 +00007613}
7614
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007615/// If E is a sizeof expression, returns its argument expression,
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007616/// otherwise returns NULL.
Nico Weberc44b35e2015-03-21 17:37:46 +00007617static const Expr *getSizeOfExprArg(const Expr *E) {
Nico Weberc5e73862011-06-14 16:14:58 +00007618 if (const UnaryExprOrTypeTraitExpr *SizeOf =
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007619 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00007620 if (SizeOf->getKind() == UETT_SizeOf && !SizeOf->isArgumentType())
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007621 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
Nico Weberc5e73862011-06-14 16:14:58 +00007622
Craig Topperc3ec1492014-05-26 06:22:03 +00007623 return nullptr;
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007624}
7625
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007626/// If E is a sizeof expression, returns its argument type.
Nico Weberc44b35e2015-03-21 17:37:46 +00007627static QualType getSizeOfArgType(const Expr *E) {
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007628 if (const UnaryExprOrTypeTraitExpr *SizeOf =
7629 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00007630 if (SizeOf->getKind() == UETT_SizeOf)
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007631 return SizeOf->getTypeOfArgument();
7632
7633 return QualType();
Nico Weberc5e73862011-06-14 16:14:58 +00007634}
7635
Akira Hatanaka2be04412018-04-17 19:13:41 +00007636namespace {
7637
7638struct SearchNonTrivialToInitializeField
7639 : DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField> {
7640 using Super =
7641 DefaultInitializedTypeVisitor<SearchNonTrivialToInitializeField>;
7642
7643 SearchNonTrivialToInitializeField(const Expr *E, Sema &S) : E(E), S(S) {}
7644
7645 void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
7646 SourceLocation SL) {
7647 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
7648 asDerived().visitArray(PDIK, AT, SL);
7649 return;
7650 }
7651
7652 Super::visitWithKind(PDIK, FT, SL);
7653 }
7654
7655 void visitARCStrong(QualType FT, SourceLocation SL) {
7656 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1);
7657 }
7658 void visitARCWeak(QualType FT, SourceLocation SL) {
7659 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 1);
7660 }
7661 void visitStruct(QualType FT, SourceLocation SL) {
7662 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields())
7663 visit(FD->getType(), FD->getLocation());
7664 }
7665 void visitArray(QualType::PrimitiveDefaultInitializeKind PDIK,
7666 const ArrayType *AT, SourceLocation SL) {
7667 visit(getContext().getBaseElementType(AT), SL);
7668 }
7669 void visitTrivial(QualType FT, SourceLocation SL) {}
7670
7671 static void diag(QualType RT, const Expr *E, Sema &S) {
7672 SearchNonTrivialToInitializeField(E, S).visitStruct(RT, SourceLocation());
7673 }
7674
7675 ASTContext &getContext() { return S.getASTContext(); }
7676
7677 const Expr *E;
7678 Sema &S;
7679};
7680
7681struct SearchNonTrivialToCopyField
7682 : CopiedTypeVisitor<SearchNonTrivialToCopyField, false> {
7683 using Super = CopiedTypeVisitor<SearchNonTrivialToCopyField, false>;
7684
7685 SearchNonTrivialToCopyField(const Expr *E, Sema &S) : E(E), S(S) {}
7686
7687 void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT,
7688 SourceLocation SL) {
7689 if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
7690 asDerived().visitArray(PCK, AT, SL);
7691 return;
7692 }
7693
7694 Super::visitWithKind(PCK, FT, SL);
7695 }
7696
7697 void visitARCStrong(QualType FT, SourceLocation SL) {
7698 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0);
7699 }
7700 void visitARCWeak(QualType FT, SourceLocation SL) {
7701 S.DiagRuntimeBehavior(SL, E, S.PDiag(diag::note_nontrivial_field) << 0);
7702 }
7703 void visitStruct(QualType FT, SourceLocation SL) {
7704 for (const FieldDecl *FD : FT->castAs<RecordType>()->getDecl()->fields())
7705 visit(FD->getType(), FD->getLocation());
7706 }
7707 void visitArray(QualType::PrimitiveCopyKind PCK, const ArrayType *AT,
7708 SourceLocation SL) {
7709 visit(getContext().getBaseElementType(AT), SL);
7710 }
7711 void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT,
7712 SourceLocation SL) {}
7713 void visitTrivial(QualType FT, SourceLocation SL) {}
7714 void visitVolatileTrivial(QualType FT, SourceLocation SL) {}
7715
7716 static void diag(QualType RT, const Expr *E, Sema &S) {
7717 SearchNonTrivialToCopyField(E, S).visitStruct(RT, SourceLocation());
7718 }
7719
7720 ASTContext &getContext() { return S.getASTContext(); }
7721
7722 const Expr *E;
7723 Sema &S;
7724};
7725
7726}
7727
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007728/// Check for dangerous or invalid arguments to memset().
Chandler Carruth53caa4d2011-04-27 07:05:31 +00007729///
Chandler Carruthac687262011-06-03 06:23:57 +00007730/// This issues warnings on known problematic, dangerous or unspecified
Matt Beaumont-Gay3c489902011-08-05 00:22:34 +00007731/// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
7732/// function calls.
Chandler Carruth53caa4d2011-04-27 07:05:31 +00007733///
7734/// \param Call The call expression to diagnose.
Matt Beaumont-Gay3c489902011-08-05 00:22:34 +00007735void Sema::CheckMemaccessArguments(const CallExpr *Call,
Anna Zaks22122702012-01-17 00:37:07 +00007736 unsigned BId,
Matt Beaumont-Gay3c489902011-08-05 00:22:34 +00007737 IdentifierInfo *FnName) {
Anna Zaks22122702012-01-17 00:37:07 +00007738 assert(BId != 0);
7739
Ted Kremenekb5fabb22011-04-28 01:38:02 +00007740 // It is possible to have a non-standard definition of memset. Validate
Douglas Gregor18739c32011-06-16 17:56:04 +00007741 // we have enough arguments, and if not, abort further checking.
Bruno Cardoso Lopes7ea9fd22016-08-10 18:34:47 +00007742 unsigned ExpectedNumArgs =
7743 (BId == Builtin::BIstrndup || BId == Builtin::BIbzero ? 2 : 3);
Nico Weber39bfed82011-10-13 22:30:23 +00007744 if (Call->getNumArgs() < ExpectedNumArgs)
Ted Kremenekb5fabb22011-04-28 01:38:02 +00007745 return;
7746
Bruno Cardoso Lopes7ea9fd22016-08-10 18:34:47 +00007747 unsigned LastArg = (BId == Builtin::BImemset || BId == Builtin::BIbzero ||
Anna Zaks22122702012-01-17 00:37:07 +00007748 BId == Builtin::BIstrndup ? 1 : 2);
Bruno Cardoso Lopes7ea9fd22016-08-10 18:34:47 +00007749 unsigned LenArg =
7750 (BId == Builtin::BIbzero || BId == Builtin::BIstrndup ? 1 : 2);
Nico Weber39bfed82011-10-13 22:30:23 +00007751 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007752
Nico Weber0e6daef2013-12-26 23:38:39 +00007753 if (CheckMemorySizeofForComparison(*this, LenExpr, FnName,
7754 Call->getLocStart(), Call->getRParenLoc()))
7755 return;
7756
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007757 // We have special checking when the length is a sizeof expression.
7758 QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
7759 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
7760 llvm::FoldingSetNodeID SizeOfArgID;
7761
Bruno Cardoso Lopesc73e4c32016-08-11 18:33:15 +00007762 // Although widely used, 'bzero' is not a standard function. Be more strict
7763 // with the argument types before allowing diagnostics and only allow the
7764 // form bzero(ptr, sizeof(...)).
7765 QualType FirstArgTy = Call->getArg(0)->IgnoreParenImpCasts()->getType();
7766 if (BId == Builtin::BIbzero && !FirstArgTy->getAs<PointerType>())
7767 return;
7768
Douglas Gregor3bb2a812011-05-03 20:37:33 +00007769 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
7770 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
Nico Weberc5e73862011-06-14 16:14:58 +00007771 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
Chandler Carruth53caa4d2011-04-27 07:05:31 +00007772
Douglas Gregor3bb2a812011-05-03 20:37:33 +00007773 QualType DestTy = Dest->getType();
Nico Weberc44b35e2015-03-21 17:37:46 +00007774 QualType PointeeTy;
Douglas Gregor3bb2a812011-05-03 20:37:33 +00007775 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
Nico Weberc44b35e2015-03-21 17:37:46 +00007776 PointeeTy = DestPtrTy->getPointeeType();
John McCall31168b02011-06-15 23:02:42 +00007777
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007778 // Never warn about void type pointers. This can be used to suppress
7779 // false positives.
7780 if (PointeeTy->isVoidType())
Douglas Gregor3bb2a812011-05-03 20:37:33 +00007781 continue;
Chandler Carruth53caa4d2011-04-27 07:05:31 +00007782
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007783 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
7784 // actually comparing the expressions for equality. Because computing the
7785 // expression IDs can be expensive, we only do this if the diagnostic is
7786 // enabled.
7787 if (SizeOfArg &&
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00007788 !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess,
7789 SizeOfArg->getExprLoc())) {
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007790 // We only compute IDs for expressions if the warning is enabled, and
7791 // cache the sizeof arg's ID.
7792 if (SizeOfArgID == llvm::FoldingSetNodeID())
7793 SizeOfArg->Profile(SizeOfArgID, Context, true);
7794 llvm::FoldingSetNodeID DestID;
7795 Dest->Profile(DestID, Context, true);
7796 if (DestID == SizeOfArgID) {
Nico Weber39bfed82011-10-13 22:30:23 +00007797 // TODO: For strncpy() and friends, this could suggest sizeof(dst)
7798 // over sizeof(src) as well.
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007799 unsigned ActionIdx = 0; // Default is to suggest dereferencing.
Anna Zaks869aecc2012-05-30 00:34:21 +00007800 StringRef ReadableName = FnName->getName();
7801
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007802 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
Anna Zaksd08d9152012-05-30 23:14:52 +00007803 if (UnaryOp->getOpcode() == UO_AddrOf)
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007804 ActionIdx = 1; // If its an address-of operator, just remove it.
Fariborz Jahanian4d365ba2013-01-30 01:12:44 +00007805 if (!PointeeTy->isIncompleteType() &&
7806 (Context.getTypeSize(PointeeTy) == Context.getCharWidth()))
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007807 ActionIdx = 2; // If the pointee's size is sizeof(char),
7808 // suggest an explicit length.
Anna Zaks869aecc2012-05-30 00:34:21 +00007809
7810 // If the function is defined as a builtin macro, do not show macro
7811 // expansion.
7812 SourceLocation SL = SizeOfArg->getExprLoc();
7813 SourceRange DSR = Dest->getSourceRange();
7814 SourceRange SSR = SizeOfArg->getSourceRange();
Alp Tokerb6cc5922014-05-03 03:45:55 +00007815 SourceManager &SM = getSourceManager();
Anna Zaks869aecc2012-05-30 00:34:21 +00007816
7817 if (SM.isMacroArgExpansion(SL)) {
7818 ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts);
7819 SL = SM.getSpellingLoc(SL);
7820 DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()),
7821 SM.getSpellingLoc(DSR.getEnd()));
7822 SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()),
7823 SM.getSpellingLoc(SSR.getEnd()));
7824 }
7825
Anna Zaksd08d9152012-05-30 23:14:52 +00007826 DiagRuntimeBehavior(SL, SizeOfArg,
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007827 PDiag(diag::warn_sizeof_pointer_expr_memaccess)
Anna Zaks869aecc2012-05-30 00:34:21 +00007828 << ReadableName
Anna Zaksd08d9152012-05-30 23:14:52 +00007829 << PointeeTy
7830 << DestTy
Anna Zaks869aecc2012-05-30 00:34:21 +00007831 << DSR
Anna Zaksd08d9152012-05-30 23:14:52 +00007832 << SSR);
7833 DiagRuntimeBehavior(SL, SizeOfArg,
7834 PDiag(diag::warn_sizeof_pointer_expr_memaccess_note)
7835 << ActionIdx
7836 << SSR);
7837
Chandler Carruth8b9e5a72011-06-16 09:09:40 +00007838 break;
7839 }
7840 }
7841
7842 // Also check for cases where the sizeof argument is the exact same
7843 // type as the memory argument, and where it points to a user-defined
7844 // record type.
7845 if (SizeOfArgTy != QualType()) {
7846 if (PointeeTy->isRecordType() &&
7847 Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
7848 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
7849 PDiag(diag::warn_sizeof_pointer_type_memaccess)
7850 << FnName << SizeOfArgTy << ArgIdx
7851 << PointeeTy << Dest->getSourceRange()
7852 << LenExpr->getSourceRange());
7853 break;
7854 }
Nico Weberc5e73862011-06-14 16:14:58 +00007855 }
Nico Weberbac8b6b2015-03-21 17:56:44 +00007856 } else if (DestTy->isArrayType()) {
7857 PointeeTy = DestTy;
Nico Weberc44b35e2015-03-21 17:37:46 +00007858 }
Nico Weberc5e73862011-06-14 16:14:58 +00007859
Nico Weberc44b35e2015-03-21 17:37:46 +00007860 if (PointeeTy == QualType())
7861 continue;
Anna Zaks22122702012-01-17 00:37:07 +00007862
Nico Weberc44b35e2015-03-21 17:37:46 +00007863 // Always complain about dynamic classes.
7864 bool IsContained;
7865 if (const CXXRecordDecl *ContainedRD =
7866 getContainedDynamicClass(PointeeTy, IsContained)) {
John McCall31168b02011-06-15 23:02:42 +00007867
Nico Weberc44b35e2015-03-21 17:37:46 +00007868 unsigned OperationType = 0;
7869 // "overwritten" if we're warning about the destination for any call
7870 // but memcmp; otherwise a verb appropriate to the call.
7871 if (ArgIdx != 0 || BId == Builtin::BImemcmp) {
7872 if (BId == Builtin::BImemcpy)
7873 OperationType = 1;
7874 else if(BId == Builtin::BImemmove)
7875 OperationType = 2;
7876 else if (BId == Builtin::BImemcmp)
7877 OperationType = 3;
7878 }
7879
John McCall31168b02011-06-15 23:02:42 +00007880 DiagRuntimeBehavior(
7881 Dest->getExprLoc(), Dest,
Nico Weberc44b35e2015-03-21 17:37:46 +00007882 PDiag(diag::warn_dyn_class_memaccess)
7883 << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx)
7884 << FnName << IsContained << ContainedRD << OperationType
7885 << Call->getCallee()->getSourceRange());
7886 } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
7887 BId != Builtin::BImemset)
7888 DiagRuntimeBehavior(
7889 Dest->getExprLoc(), Dest,
7890 PDiag(diag::warn_arc_object_memaccess)
7891 << ArgIdx << FnName << PointeeTy
7892 << Call->getCallee()->getSourceRange());
Akira Hatanaka2be04412018-04-17 19:13:41 +00007893 else if (const auto *RT = PointeeTy->getAs<RecordType>()) {
7894 if ((BId == Builtin::BImemset || BId == Builtin::BIbzero) &&
7895 RT->getDecl()->isNonTrivialToPrimitiveDefaultInitialize()) {
7896 DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
7897 PDiag(diag::warn_cstruct_memaccess)
7898 << ArgIdx << FnName << PointeeTy << 0);
7899 SearchNonTrivialToInitializeField::diag(PointeeTy, Dest, *this);
7900 } else if ((BId == Builtin::BImemcpy || BId == Builtin::BImemmove) &&
7901 RT->getDecl()->isNonTrivialToPrimitiveCopy()) {
7902 DiagRuntimeBehavior(Dest->getExprLoc(), Dest,
7903 PDiag(diag::warn_cstruct_memaccess)
7904 << ArgIdx << FnName << PointeeTy << 1);
7905 SearchNonTrivialToCopyField::diag(PointeeTy, Dest, *this);
7906 } else {
7907 continue;
7908 }
7909 } else
Nico Weberc44b35e2015-03-21 17:37:46 +00007910 continue;
7911
7912 DiagRuntimeBehavior(
7913 Dest->getExprLoc(), Dest,
7914 PDiag(diag::note_bad_memaccess_silence)
7915 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
7916 break;
Chandler Carruth53caa4d2011-04-27 07:05:31 +00007917 }
7918}
7919
Ted Kremenek6865f772011-08-18 20:55:45 +00007920// A little helper routine: ignore addition and subtraction of integer literals.
7921// This intentionally does not ignore all integer constant expressions because
7922// we don't want to remove sizeof().
7923static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
7924 Ex = Ex->IgnoreParenCasts();
7925
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00007926 while (true) {
Ted Kremenek6865f772011-08-18 20:55:45 +00007927 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
7928 if (!BO || !BO->isAdditiveOp())
7929 break;
7930
7931 const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
7932 const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
7933
7934 if (isa<IntegerLiteral>(RHS))
7935 Ex = LHS;
7936 else if (isa<IntegerLiteral>(LHS))
7937 Ex = RHS;
7938 else
7939 break;
7940 }
7941
7942 return Ex;
7943}
7944
Anna Zaks13b08572012-08-08 21:42:23 +00007945static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty,
7946 ASTContext &Context) {
7947 // Only handle constant-sized or VLAs, but not flexible members.
7948 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) {
7949 // Only issue the FIXIT for arrays of size > 1.
7950 if (CAT->getSize().getSExtValue() <= 1)
7951 return false;
7952 } else if (!Ty->isVariableArrayType()) {
7953 return false;
7954 }
7955 return true;
7956}
7957
Ted Kremenek6865f772011-08-18 20:55:45 +00007958// Warn if the user has made the 'size' argument to strlcpy or strlcat
7959// be the size of the source, instead of the destination.
7960void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
7961 IdentifierInfo *FnName) {
7962
7963 // Don't crash if the user has the wrong number of arguments
Fariborz Jahanianab4fe982014-09-12 18:44:36 +00007964 unsigned NumArgs = Call->getNumArgs();
7965 if ((NumArgs != 3) && (NumArgs != 4))
Ted Kremenek6865f772011-08-18 20:55:45 +00007966 return;
7967
7968 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
7969 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
Craig Topperc3ec1492014-05-26 06:22:03 +00007970 const Expr *CompareWithSrc = nullptr;
Nico Weber0e6daef2013-12-26 23:38:39 +00007971
7972 if (CheckMemorySizeofForComparison(*this, SizeArg, FnName,
7973 Call->getLocStart(), Call->getRParenLoc()))
7974 return;
Ted Kremenek6865f772011-08-18 20:55:45 +00007975
7976 // Look for 'strlcpy(dst, x, sizeof(x))'
7977 if (const Expr *Ex = getSizeOfExprArg(SizeArg))
7978 CompareWithSrc = Ex;
7979 else {
7980 // Look for 'strlcpy(dst, x, strlen(x))'
7981 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
Alp Tokera724cff2013-12-28 21:59:02 +00007982 if (SizeCall->getBuiltinCallee() == Builtin::BIstrlen &&
7983 SizeCall->getNumArgs() == 1)
Ted Kremenek6865f772011-08-18 20:55:45 +00007984 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
7985 }
7986 }
7987
7988 if (!CompareWithSrc)
7989 return;
7990
7991 // Determine if the argument to sizeof/strlen is equal to the source
7992 // argument. In principle there's all kinds of things you could do
7993 // here, for instance creating an == expression and evaluating it with
7994 // EvaluateAsBooleanCondition, but this uses a more direct technique:
7995 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
7996 if (!SrcArgDRE)
7997 return;
7998
7999 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
8000 if (!CompareWithSrcDRE ||
8001 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
8002 return;
8003
8004 const Expr *OriginalSizeArg = Call->getArg(2);
8005 Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size)
8006 << OriginalSizeArg->getSourceRange() << FnName;
8007
8008 // Output a FIXIT hint if the destination is an array (rather than a
8009 // pointer to an array). This could be enhanced to handle some
8010 // pointers if we know the actual size, like if DstArg is 'array+2'
8011 // we could say 'sizeof(array)-2'.
8012 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
Anna Zaks13b08572012-08-08 21:42:23 +00008013 if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context))
Ted Kremenek18db5d42011-08-18 22:48:41 +00008014 return;
Ted Kremenek18db5d42011-08-18 22:48:41 +00008015
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00008016 SmallString<128> sizeString;
Ted Kremenek18db5d42011-08-18 22:48:41 +00008017 llvm::raw_svector_ostream OS(sizeString);
8018 OS << "sizeof(";
Craig Topperc3ec1492014-05-26 06:22:03 +00008019 DstArg->printPretty(OS, nullptr, getPrintingPolicy());
Ted Kremenek18db5d42011-08-18 22:48:41 +00008020 OS << ")";
8021
8022 Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
8023 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
8024 OS.str());
Ted Kremenek6865f772011-08-18 20:55:45 +00008025}
8026
Anna Zaks314cd092012-02-01 19:08:57 +00008027/// Check if two expressions refer to the same declaration.
8028static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
8029 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
8030 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
8031 return D1->getDecl() == D2->getDecl();
8032 return false;
8033}
8034
8035static const Expr *getStrlenExprArg(const Expr *E) {
8036 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
8037 const FunctionDecl *FD = CE->getDirectCallee();
8038 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
Craig Topperc3ec1492014-05-26 06:22:03 +00008039 return nullptr;
Anna Zaks314cd092012-02-01 19:08:57 +00008040 return CE->getArg(0)->IgnoreParenCasts();
8041 }
Craig Topperc3ec1492014-05-26 06:22:03 +00008042 return nullptr;
Anna Zaks314cd092012-02-01 19:08:57 +00008043}
8044
8045// Warn on anti-patterns as the 'size' argument to strncat.
8046// The correct size argument should look like following:
8047// strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
8048void Sema::CheckStrncatArguments(const CallExpr *CE,
8049 IdentifierInfo *FnName) {
8050 // Don't crash if the user has the wrong number of arguments.
8051 if (CE->getNumArgs() < 3)
8052 return;
8053 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
8054 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
8055 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
8056
Nico Weber0e6daef2013-12-26 23:38:39 +00008057 if (CheckMemorySizeofForComparison(*this, LenArg, FnName, CE->getLocStart(),
8058 CE->getRParenLoc()))
8059 return;
8060
Anna Zaks314cd092012-02-01 19:08:57 +00008061 // Identify common expressions, which are wrongly used as the size argument
8062 // to strncat and may lead to buffer overflows.
8063 unsigned PatternType = 0;
8064 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
8065 // - sizeof(dst)
8066 if (referToTheSameDecl(SizeOfArg, DstArg))
8067 PatternType = 1;
8068 // - sizeof(src)
8069 else if (referToTheSameDecl(SizeOfArg, SrcArg))
8070 PatternType = 2;
8071 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
8072 if (BE->getOpcode() == BO_Sub) {
8073 const Expr *L = BE->getLHS()->IgnoreParenCasts();
8074 const Expr *R = BE->getRHS()->IgnoreParenCasts();
8075 // - sizeof(dst) - strlen(dst)
8076 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
8077 referToTheSameDecl(DstArg, getStrlenExprArg(R)))
8078 PatternType = 1;
8079 // - sizeof(src) - (anything)
8080 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
8081 PatternType = 2;
8082 }
8083 }
8084
8085 if (PatternType == 0)
8086 return;
8087
Anna Zaks5069aa32012-02-03 01:27:37 +00008088 // Generate the diagnostic.
8089 SourceLocation SL = LenArg->getLocStart();
8090 SourceRange SR = LenArg->getSourceRange();
Alp Tokerb6cc5922014-05-03 03:45:55 +00008091 SourceManager &SM = getSourceManager();
Anna Zaks5069aa32012-02-03 01:27:37 +00008092
8093 // If the function is defined as a builtin macro, do not show macro expansion.
8094 if (SM.isMacroArgExpansion(SL)) {
8095 SL = SM.getSpellingLoc(SL);
8096 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
8097 SM.getSpellingLoc(SR.getEnd()));
8098 }
8099
Anna Zaks13b08572012-08-08 21:42:23 +00008100 // Check if the destination is an array (rather than a pointer to an array).
8101 QualType DstTy = DstArg->getType();
8102 bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy,
8103 Context);
8104 if (!isKnownSizeArray) {
8105 if (PatternType == 1)
8106 Diag(SL, diag::warn_strncat_wrong_size) << SR;
8107 else
8108 Diag(SL, diag::warn_strncat_src_size) << SR;
8109 return;
8110 }
8111
Anna Zaks314cd092012-02-01 19:08:57 +00008112 if (PatternType == 1)
Anna Zaks5069aa32012-02-03 01:27:37 +00008113 Diag(SL, diag::warn_strncat_large_size) << SR;
Anna Zaks314cd092012-02-01 19:08:57 +00008114 else
Anna Zaks5069aa32012-02-03 01:27:37 +00008115 Diag(SL, diag::warn_strncat_src_size) << SR;
Anna Zaks314cd092012-02-01 19:08:57 +00008116
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00008117 SmallString<128> sizeString;
Anna Zaks314cd092012-02-01 19:08:57 +00008118 llvm::raw_svector_ostream OS(sizeString);
8119 OS << "sizeof(";
Craig Topperc3ec1492014-05-26 06:22:03 +00008120 DstArg->printPretty(OS, nullptr, getPrintingPolicy());
Anna Zaks314cd092012-02-01 19:08:57 +00008121 OS << ") - ";
8122 OS << "strlen(";
Craig Topperc3ec1492014-05-26 06:22:03 +00008123 DstArg->printPretty(OS, nullptr, getPrintingPolicy());
Anna Zaks314cd092012-02-01 19:08:57 +00008124 OS << ") - 1";
8125
Anna Zaks5069aa32012-02-03 01:27:37 +00008126 Diag(SL, diag::note_strncat_wrong_size)
8127 << FixItHint::CreateReplacement(SR, OS.str());
Anna Zaks314cd092012-02-01 19:08:57 +00008128}
8129
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008130//===--- CHECK: Return Address of Stack Variable --------------------------===//
8131
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008132static const Expr *EvalVal(const Expr *E,
8133 SmallVectorImpl<const DeclRefExpr *> &refVars,
8134 const Decl *ParentDecl);
8135static const Expr *EvalAddr(const Expr *E,
8136 SmallVectorImpl<const DeclRefExpr *> &refVars,
8137 const Decl *ParentDecl);
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008138
8139/// CheckReturnStackAddr - Check if a return statement returns the address
8140/// of a stack variable.
Ted Kremenekef9e7f82014-01-22 06:10:28 +00008141static void
8142CheckReturnStackAddr(Sema &S, Expr *RetValExp, QualType lhsType,
8143 SourceLocation ReturnLoc) {
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008144 const Expr *stackE = nullptr;
8145 SmallVector<const DeclRefExpr *, 8> refVars;
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008146
8147 // Perform checking for returned stack addresses, local blocks,
8148 // label addresses or references to temporaries.
John McCall31168b02011-06-15 23:02:42 +00008149 if (lhsType->isPointerType() ||
Ted Kremenekef9e7f82014-01-22 06:10:28 +00008150 (!S.getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) {
Craig Topperc3ec1492014-05-26 06:22:03 +00008151 stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/nullptr);
Mike Stump12b8ce12009-08-04 21:02:39 +00008152 } else if (lhsType->isReferenceType()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00008153 stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/nullptr);
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008154 }
8155
Craig Topperc3ec1492014-05-26 06:22:03 +00008156 if (!stackE)
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008157 return; // Nothing suspicious was found.
8158
Simon Pilgrim750bde62017-03-31 11:00:53 +00008159 // Parameters are initialized in the calling scope, so taking the address
Richard Trieu81b6c562016-08-05 23:24:47 +00008160 // of a parameter reference doesn't need a warning.
8161 for (auto *DRE : refVars)
8162 if (isa<ParmVarDecl>(DRE->getDecl()))
8163 return;
8164
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008165 SourceLocation diagLoc;
8166 SourceRange diagRange;
8167 if (refVars.empty()) {
8168 diagLoc = stackE->getLocStart();
8169 diagRange = stackE->getSourceRange();
8170 } else {
8171 // We followed through a reference variable. 'stackE' contains the
8172 // problematic expression but we will warn at the return statement pointing
8173 // at the reference variable. We will later display the "trail" of
8174 // reference variables using notes.
8175 diagLoc = refVars[0]->getLocStart();
8176 diagRange = refVars[0]->getSourceRange();
8177 }
8178
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008179 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) {
8180 // address of local var
Craig Topperda7b27f2015-11-17 05:40:09 +00008181 S.Diag(diagLoc, diag::warn_ret_stack_addr_ref) << lhsType->isReferenceType()
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008182 << DR->getDecl()->getDeclName() << diagRange;
8183 } else if (isa<BlockExpr>(stackE)) { // local block.
Ted Kremenekef9e7f82014-01-22 06:10:28 +00008184 S.Diag(diagLoc, diag::err_ret_local_block) << diagRange;
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008185 } else if (isa<AddrLabelExpr>(stackE)) { // address of label.
Ted Kremenekef9e7f82014-01-22 06:10:28 +00008186 S.Diag(diagLoc, diag::warn_ret_addr_label) << diagRange;
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008187 } else { // local temporary.
Richard Trieu81b6c562016-08-05 23:24:47 +00008188 // If there is an LValue->RValue conversion, then the value of the
8189 // reference type is used, not the reference.
8190 if (auto *ICE = dyn_cast<ImplicitCastExpr>(RetValExp)) {
8191 if (ICE->getCastKind() == CK_LValueToRValue) {
8192 return;
8193 }
8194 }
Craig Topperda7b27f2015-11-17 05:40:09 +00008195 S.Diag(diagLoc, diag::warn_ret_local_temp_addr_ref)
8196 << lhsType->isReferenceType() << diagRange;
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008197 }
8198
8199 // Display the "trail" of reference variables that we followed until we
8200 // found the problematic expression using notes.
8201 for (unsigned i = 0, e = refVars.size(); i != e; ++i) {
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008202 const VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl());
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008203 // If this var binds to another reference var, show the range of the next
8204 // var, otherwise the var binds to the problematic expression, in which case
8205 // show the range of the expression.
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008206 SourceRange range = (i < e - 1) ? refVars[i + 1]->getSourceRange()
8207 : stackE->getSourceRange();
Ted Kremenekef9e7f82014-01-22 06:10:28 +00008208 S.Diag(VD->getLocation(), diag::note_ref_var_local_bind)
8209 << VD->getDeclName() << range;
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008210 }
8211}
8212
8213/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
8214/// check if the expression in a return statement evaluates to an address
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008215/// to a location on the stack, a local block, an address of a label, or a
8216/// reference to local temporary. The recursion is used to traverse the
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008217/// AST of the return expression, with recursion backtracking when we
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008218/// encounter a subexpression that (1) clearly does not lead to one of the
8219/// above problematic expressions (2) is something we cannot determine leads to
8220/// a problematic expression based on such local checking.
8221///
8222/// Both EvalAddr and EvalVal follow through reference variables to evaluate
8223/// the expression that they point to. Such variables are added to the
8224/// 'refVars' vector so that we know what the reference variable "trail" was.
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008225///
Ted Kremeneke07a8cd2007-08-28 17:02:55 +00008226/// EvalAddr processes expressions that are pointers that are used as
8227/// references (and not L-values). EvalVal handles all other values.
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008228/// At the base case of the recursion is a check for the above problematic
8229/// expressions.
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008230///
8231/// This implementation handles:
8232///
8233/// * pointer-to-pointer casts
8234/// * implicit conversions from array references to pointers
8235/// * taking the address of fields
8236/// * arbitrary interplay between "&" and "*" operators
8237/// * pointer arithmetic from an address of a stack variable
8238/// * taking the address of an array element where the array is on the stack
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008239static const Expr *EvalAddr(const Expr *E,
8240 SmallVectorImpl<const DeclRefExpr *> &refVars,
8241 const Decl *ParentDecl) {
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008242 if (E->isTypeDependent())
Craig Topperc3ec1492014-05-26 06:22:03 +00008243 return nullptr;
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008244
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008245 // We should only be called for evaluating pointer expressions.
David Chisnall9f57c292009-08-17 16:35:33 +00008246 assert((E->getType()->isAnyPointerType() ||
Steve Naroff8de9c3a2008-09-05 22:11:13 +00008247 E->getType()->isBlockPointerType() ||
Ted Kremenek1b0ea822008-01-07 19:49:32 +00008248 E->getType()->isObjCQualifiedIdType()) &&
Chris Lattner934edb22007-12-28 05:31:15 +00008249 "EvalAddr only works on pointers");
Mike Stump11289f42009-09-09 15:08:12 +00008250
Peter Collingbourne91147592011-04-15 00:35:48 +00008251 E = E->IgnoreParens();
8252
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008253 // Our "symbolic interpreter" is just a dispatch off the currently
8254 // viewed AST node. We then recursively traverse the AST by calling
8255 // EvalAddr and EvalVal appropriately.
8256 switch (E->getStmtClass()) {
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008257 case Stmt::DeclRefExprClass: {
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008258 const DeclRefExpr *DR = cast<DeclRefExpr>(E);
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008259
Richard Smith40f08eb2014-01-30 22:05:38 +00008260 // If we leave the immediate function, the lifetime isn't about to end.
Alexey Bataev19acc3d2015-01-12 10:17:46 +00008261 if (DR->refersToEnclosingVariableOrCapture())
Craig Topperc3ec1492014-05-26 06:22:03 +00008262 return nullptr;
Richard Smith40f08eb2014-01-30 22:05:38 +00008263
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008264 if (const VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008265 // If this is a reference variable, follow through to the expression that
8266 // it points to.
8267 if (V->hasLocalStorage() &&
8268 V->getType()->isReferenceType() && V->hasInit()) {
8269 // Add the reference variable to the "trail".
8270 refVars.push_back(DR);
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008271 return EvalAddr(V->getInit(), refVars, ParentDecl);
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008272 }
8273
Craig Topperc3ec1492014-05-26 06:22:03 +00008274 return nullptr;
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008275 }
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008276
Chris Lattner934edb22007-12-28 05:31:15 +00008277 case Stmt::UnaryOperatorClass: {
8278 // The only unary operator that make sense to handle here
8279 // is AddrOf. All others don't make sense as pointers.
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008280 const UnaryOperator *U = cast<UnaryOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00008281
John McCalle3027922010-08-25 11:45:40 +00008282 if (U->getOpcode() == UO_AddrOf)
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008283 return EvalVal(U->getSubExpr(), refVars, ParentDecl);
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008284 return nullptr;
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008285 }
Mike Stump11289f42009-09-09 15:08:12 +00008286
Chris Lattner934edb22007-12-28 05:31:15 +00008287 case Stmt::BinaryOperatorClass: {
8288 // Handle pointer arithmetic. All other binary operators are not valid
8289 // in this context.
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008290 const BinaryOperator *B = cast<BinaryOperator>(E);
John McCalle3027922010-08-25 11:45:40 +00008291 BinaryOperatorKind op = B->getOpcode();
Mike Stump11289f42009-09-09 15:08:12 +00008292
John McCalle3027922010-08-25 11:45:40 +00008293 if (op != BO_Add && op != BO_Sub)
Craig Topperc3ec1492014-05-26 06:22:03 +00008294 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00008295
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008296 const Expr *Base = B->getLHS();
Chris Lattner934edb22007-12-28 05:31:15 +00008297
8298 // Determine which argument is the real pointer base. It could be
8299 // the RHS argument instead of the LHS.
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008300 if (!Base->getType()->isPointerType())
8301 Base = B->getRHS();
Mike Stump11289f42009-09-09 15:08:12 +00008302
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008303 assert(Base->getType()->isPointerType());
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008304 return EvalAddr(Base, refVars, ParentDecl);
Chris Lattner934edb22007-12-28 05:31:15 +00008305 }
Steve Naroff2752a172008-09-10 19:17:48 +00008306
Chris Lattner934edb22007-12-28 05:31:15 +00008307 // For conditional operators we need to see if either the LHS or RHS are
8308 // valid DeclRefExpr*s. If one of them is valid, we return it.
8309 case Stmt::ConditionalOperatorClass: {
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008310 const ConditionalOperator *C = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00008311
Chris Lattner934edb22007-12-28 05:31:15 +00008312 // Handle the GNU extension for missing LHS.
Richard Smith6a6a4bb2014-01-27 04:19:56 +00008313 // FIXME: That isn't a ConditionalOperator, so doesn't get here.
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008314 if (const Expr *LHSExpr = C->getLHS()) {
Richard Smith6a6a4bb2014-01-27 04:19:56 +00008315 // In C++, we can have a throw-expression, which has 'void' type.
8316 if (!LHSExpr->getType()->isVoidType())
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008317 if (const Expr *LHS = EvalAddr(LHSExpr, refVars, ParentDecl))
Douglas Gregor270b2ef2010-10-21 16:21:08 +00008318 return LHS;
8319 }
Chris Lattner934edb22007-12-28 05:31:15 +00008320
Douglas Gregor270b2ef2010-10-21 16:21:08 +00008321 // In C++, we can have a throw-expression, which has 'void' type.
8322 if (C->getRHS()->getType()->isVoidType())
Craig Topperc3ec1492014-05-26 06:22:03 +00008323 return nullptr;
Douglas Gregor270b2ef2010-10-21 16:21:08 +00008324
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008325 return EvalAddr(C->getRHS(), refVars, ParentDecl);
Chris Lattner934edb22007-12-28 05:31:15 +00008326 }
Richard Smith6a6a4bb2014-01-27 04:19:56 +00008327
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008328 case Stmt::BlockExprClass:
John McCallc63de662011-02-02 13:00:07 +00008329 if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures())
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008330 return E; // local block.
Craig Topperc3ec1492014-05-26 06:22:03 +00008331 return nullptr;
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008332
8333 case Stmt::AddrLabelExprClass:
8334 return E; // address of label.
Mike Stump11289f42009-09-09 15:08:12 +00008335
John McCall28fc7092011-11-10 05:35:25 +00008336 case Stmt::ExprWithCleanupsClass:
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008337 return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,
8338 ParentDecl);
John McCall28fc7092011-11-10 05:35:25 +00008339
Ted Kremenekc3b4c522008-08-07 00:49:01 +00008340 // For casts, we need to handle conversions from arrays to
8341 // pointer values, and pointer-to-pointer conversions.
Douglas Gregore200adc2008-10-27 19:41:14 +00008342 case Stmt::ImplicitCastExprClass:
Douglas Gregorf19b2312008-10-28 15:36:24 +00008343 case Stmt::CStyleCastExprClass:
John McCall31168b02011-06-15 23:02:42 +00008344 case Stmt::CXXFunctionalCastExprClass:
Eli Friedman8195ad72012-02-23 23:04:32 +00008345 case Stmt::ObjCBridgedCastExprClass:
Mike Stump11289f42009-09-09 15:08:12 +00008346 case Stmt::CXXStaticCastExprClass:
8347 case Stmt::CXXDynamicCastExprClass:
Douglas Gregore200adc2008-10-27 19:41:14 +00008348 case Stmt::CXXConstCastExprClass:
8349 case Stmt::CXXReinterpretCastExprClass: {
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008350 const Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
Eli Friedman8195ad72012-02-23 23:04:32 +00008351 switch (cast<CastExpr>(E)->getCastKind()) {
Eli Friedman8195ad72012-02-23 23:04:32 +00008352 case CK_LValueToRValue:
8353 case CK_NoOp:
8354 case CK_BaseToDerived:
8355 case CK_DerivedToBase:
8356 case CK_UncheckedDerivedToBase:
8357 case CK_Dynamic:
8358 case CK_CPointerToObjCPointerCast:
8359 case CK_BlockPointerToObjCPointerCast:
8360 case CK_AnyPointerToBlockPointerCast:
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008361 return EvalAddr(SubExpr, refVars, ParentDecl);
Eli Friedman8195ad72012-02-23 23:04:32 +00008362
8363 case CK_ArrayToPointerDecay:
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008364 return EvalVal(SubExpr, refVars, ParentDecl);
Eli Friedman8195ad72012-02-23 23:04:32 +00008365
Richard Trieudadefde2014-07-02 04:39:38 +00008366 case CK_BitCast:
8367 if (SubExpr->getType()->isAnyPointerType() ||
8368 SubExpr->getType()->isBlockPointerType() ||
8369 SubExpr->getType()->isObjCQualifiedIdType())
8370 return EvalAddr(SubExpr, refVars, ParentDecl);
8371 else
8372 return nullptr;
8373
Eli Friedman8195ad72012-02-23 23:04:32 +00008374 default:
Craig Topperc3ec1492014-05-26 06:22:03 +00008375 return nullptr;
Eli Friedman8195ad72012-02-23 23:04:32 +00008376 }
Chris Lattner934edb22007-12-28 05:31:15 +00008377 }
Mike Stump11289f42009-09-09 15:08:12 +00008378
Douglas Gregorfe314812011-06-21 17:03:29 +00008379 case Stmt::MaterializeTemporaryExprClass:
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008380 if (const Expr *Result =
8381 EvalAddr(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
8382 refVars, ParentDecl))
Douglas Gregorfe314812011-06-21 17:03:29 +00008383 return Result;
Douglas Gregorfe314812011-06-21 17:03:29 +00008384 return E;
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008385
Chris Lattner934edb22007-12-28 05:31:15 +00008386 // Everything else: we simply don't reason about them.
8387 default:
Craig Topperc3ec1492014-05-26 06:22:03 +00008388 return nullptr;
Chris Lattner934edb22007-12-28 05:31:15 +00008389 }
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008390}
Mike Stump11289f42009-09-09 15:08:12 +00008391
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008392/// EvalVal - This function is complements EvalAddr in the mutual recursion.
8393/// See the comments for EvalAddr for more details.
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008394static const Expr *EvalVal(const Expr *E,
8395 SmallVectorImpl<const DeclRefExpr *> &refVars,
8396 const Decl *ParentDecl) {
8397 do {
8398 // We should only be called for evaluating non-pointer expressions, or
8399 // expressions with a pointer type that are not used as references but
8400 // instead
8401 // are l-values (e.g., DeclRefExpr with a pointer type).
Mike Stump11289f42009-09-09 15:08:12 +00008402
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008403 // Our "symbolic interpreter" is just a dispatch off the currently
8404 // viewed AST node. We then recursively traverse the AST by calling
8405 // EvalAddr and EvalVal appropriately.
Peter Collingbourne91147592011-04-15 00:35:48 +00008406
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008407 E = E->IgnoreParens();
8408 switch (E->getStmtClass()) {
8409 case Stmt::ImplicitCastExprClass: {
8410 const ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
8411 if (IE->getValueKind() == VK_LValue) {
8412 E = IE->getSubExpr();
8413 continue;
8414 }
Craig Topperc3ec1492014-05-26 06:22:03 +00008415 return nullptr;
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008416 }
Richard Smith40f08eb2014-01-30 22:05:38 +00008417
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008418 case Stmt::ExprWithCleanupsClass:
8419 return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,
8420 ParentDecl);
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008421
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008422 case Stmt::DeclRefExprClass: {
8423 // When we hit a DeclRefExpr we are looking at code that refers to a
8424 // variable's name. If it's not a reference variable we check if it has
8425 // local storage within the function, and if so, return the expression.
8426 const DeclRefExpr *DR = cast<DeclRefExpr>(E);
8427
8428 // If we leave the immediate function, the lifetime isn't about to end.
8429 if (DR->refersToEnclosingVariableOrCapture())
8430 return nullptr;
8431
8432 if (const VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) {
8433 // Check if it refers to itself, e.g. "int& i = i;".
8434 if (V == ParentDecl)
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008435 return DR;
8436
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008437 if (V->hasLocalStorage()) {
8438 if (!V->getType()->isReferenceType())
8439 return DR;
8440
8441 // Reference variable, follow through to the expression that
8442 // it points to.
8443 if (V->hasInit()) {
8444 // Add the reference variable to the "trail".
8445 refVars.push_back(DR);
8446 return EvalVal(V->getInit(), refVars, V);
8447 }
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008448 }
8449 }
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008450
8451 return nullptr;
Argyrios Kyrtzidisb4015e12012-04-30 23:23:55 +00008452 }
Mike Stump11289f42009-09-09 15:08:12 +00008453
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008454 case Stmt::UnaryOperatorClass: {
8455 // The only unary operator that make sense to handle here
8456 // is Deref. All others don't resolve to a "name." This includes
8457 // handling all sorts of rvalues passed to a unary operator.
8458 const UnaryOperator *U = cast<UnaryOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00008459
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008460 if (U->getOpcode() == UO_Deref)
8461 return EvalAddr(U->getSubExpr(), refVars, ParentDecl);
Mike Stump11289f42009-09-09 15:08:12 +00008462
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008463 return nullptr;
8464 }
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008465
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008466 case Stmt::ArraySubscriptExprClass: {
8467 // Array subscripts are potential references to data on the stack. We
8468 // retrieve the DeclRefExpr* for the array variable if it indeed
8469 // has local storage.
Saleem Abdulrasoolcfd45532016-02-15 01:51:24 +00008470 const auto *ASE = cast<ArraySubscriptExpr>(E);
8471 if (ASE->isTypeDependent())
8472 return nullptr;
8473 return EvalAddr(ASE->getBase(), refVars, ParentDecl);
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008474 }
Mike Stump11289f42009-09-09 15:08:12 +00008475
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008476 case Stmt::OMPArraySectionExprClass: {
8477 return EvalAddr(cast<OMPArraySectionExpr>(E)->getBase(), refVars,
8478 ParentDecl);
8479 }
Mike Stump11289f42009-09-09 15:08:12 +00008480
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008481 case Stmt::ConditionalOperatorClass: {
8482 // For conditional operators we need to see if either the LHS or RHS are
8483 // non-NULL Expr's. If one is non-NULL, we return it.
8484 const ConditionalOperator *C = cast<ConditionalOperator>(E);
Alexey Bataev1a3320e2015-08-25 14:24:04 +00008485
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008486 // Handle the GNU extension for missing LHS.
8487 if (const Expr *LHSExpr = C->getLHS()) {
8488 // In C++, we can have a throw-expression, which has 'void' type.
8489 if (!LHSExpr->getType()->isVoidType())
8490 if (const Expr *LHS = EvalVal(LHSExpr, refVars, ParentDecl))
8491 return LHS;
8492 }
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008493
Richard Smith6a6a4bb2014-01-27 04:19:56 +00008494 // In C++, we can have a throw-expression, which has 'void' type.
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008495 if (C->getRHS()->getType()->isVoidType())
8496 return nullptr;
8497
8498 return EvalVal(C->getRHS(), refVars, ParentDecl);
Richard Smith6a6a4bb2014-01-27 04:19:56 +00008499 }
8500
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008501 // Accesses to members are potential references to data on the stack.
8502 case Stmt::MemberExprClass: {
8503 const MemberExpr *M = cast<MemberExpr>(E);
Anders Carlsson801c5c72007-11-30 19:04:31 +00008504
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008505 // Check for indirect access. We only want direct field accesses.
8506 if (M->isArrow())
8507 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00008508
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008509 // Check whether the member type is itself a reference, in which case
8510 // we're not going to refer to the member, but to what the member refers
8511 // to.
8512 if (M->getMemberDecl()->getType()->isReferenceType())
8513 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00008514
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008515 return EvalVal(M->getBase(), refVars, ParentDecl);
8516 }
Ted Kremenekcbe6b0b2010-09-02 01:12:13 +00008517
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008518 case Stmt::MaterializeTemporaryExprClass:
8519 if (const Expr *Result =
8520 EvalVal(cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
8521 refVars, ParentDecl))
8522 return Result;
Argyrios Kyrtzidise72f7152010-11-30 22:57:32 +00008523 return E;
8524
Saleem Abdulrasool768eb4a2016-02-15 00:36:49 +00008525 default:
8526 // Check that we don't return or take the address of a reference to a
8527 // temporary. This is only useful in C++.
8528 if (!E->isTypeDependent() && E->isRValue())
8529 return E;
8530
8531 // Everything else: we simply don't reason about them.
8532 return nullptr;
8533 }
8534 } while (true);
Ted Kremenekcff94fa2007-08-17 16:46:58 +00008535}
Ted Kremenek43fb8b02007-11-25 00:58:00 +00008536
Ted Kremenekef9e7f82014-01-22 06:10:28 +00008537void
8538Sema::CheckReturnValExpr(Expr *RetValExp, QualType lhsType,
8539 SourceLocation ReturnLoc,
8540 bool isObjCMethod,
Artyom Skrobov9f213442014-01-24 11:10:39 +00008541 const AttrVec *Attrs,
8542 const FunctionDecl *FD) {
Ted Kremenekef9e7f82014-01-22 06:10:28 +00008543 CheckReturnStackAddr(*this, RetValExp, lhsType, ReturnLoc);
8544
8545 // Check if the return value is null but should not be.
Douglas Gregorb4866e82015-06-19 18:13:19 +00008546 if (((Attrs && hasSpecificAttr<ReturnsNonNullAttr>(*Attrs)) ||
8547 (!isObjCMethod && isNonNullType(Context, lhsType))) &&
Benjamin Kramerae852a62014-02-23 14:34:50 +00008548 CheckNonNullExpr(*this, RetValExp))
8549 Diag(ReturnLoc, diag::warn_null_ret)
8550 << (isObjCMethod ? 1 : 0) << RetValExp->getSourceRange();
Artyom Skrobov9f213442014-01-24 11:10:39 +00008551
8552 // C++11 [basic.stc.dynamic.allocation]p4:
8553 // If an allocation function declared with a non-throwing
8554 // exception-specification fails to allocate storage, it shall return
8555 // a null pointer. Any other allocation function that fails to allocate
8556 // storage shall indicate failure only by throwing an exception [...]
8557 if (FD) {
8558 OverloadedOperatorKind Op = FD->getOverloadedOperator();
8559 if (Op == OO_New || Op == OO_Array_New) {
8560 const FunctionProtoType *Proto
8561 = FD->getType()->castAs<FunctionProtoType>();
Richard Smitheaf11ad2018-05-03 03:58:32 +00008562 if (!Proto->isNothrow(/*ResultIfDependent*/true) &&
Artyom Skrobov9f213442014-01-24 11:10:39 +00008563 CheckNonNullExpr(*this, RetValExp))
8564 Diag(ReturnLoc, diag::warn_operator_new_returns_null)
8565 << FD << getLangOpts().CPlusPlus11;
8566 }
8567 }
Ted Kremenekef9e7f82014-01-22 06:10:28 +00008568}
8569
Ted Kremenek43fb8b02007-11-25 00:58:00 +00008570//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
8571
8572/// Check for comparisons of floating point operands using != and ==.
8573/// Issue a warning if these are no self-comparisons, as they are not likely
8574/// to do what the programmer intended.
Richard Trieu82402a02011-09-15 21:56:47 +00008575void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
Richard Trieu82402a02011-09-15 21:56:47 +00008576 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
8577 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
Ted Kremenek43fb8b02007-11-25 00:58:00 +00008578
8579 // Special case: check for x == x (which is OK).
8580 // Do not emit warnings for such cases.
8581 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
8582 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
8583 if (DRL->getDecl() == DRR->getDecl())
David Blaikie1f4ff152012-07-16 20:47:22 +00008584 return;
Mike Stump11289f42009-09-09 15:08:12 +00008585
Ted Kremenekeda40e22007-11-29 00:59:04 +00008586 // Special case: check for comparisons against literals that can be exactly
8587 // represented by APFloat. In such cases, do not emit a warning. This
8588 // is a heuristic: often comparison against such literals are used to
8589 // detect if a value in a variable has not changed. This clearly can
8590 // lead to false negatives.
David Blaikie1f4ff152012-07-16 20:47:22 +00008591 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
8592 if (FLL->isExact())
8593 return;
8594 } else
8595 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
8596 if (FLR->isExact())
8597 return;
Mike Stump11289f42009-09-09 15:08:12 +00008598
Ted Kremenek43fb8b02007-11-25 00:58:00 +00008599 // Check for comparisons with builtin types.
David Blaikie1f4ff152012-07-16 20:47:22 +00008600 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
Alp Tokera724cff2013-12-28 21:59:02 +00008601 if (CL->getBuiltinCallee())
David Blaikie1f4ff152012-07-16 20:47:22 +00008602 return;
Mike Stump11289f42009-09-09 15:08:12 +00008603
David Blaikie1f4ff152012-07-16 20:47:22 +00008604 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
Alp Tokera724cff2013-12-28 21:59:02 +00008605 if (CR->getBuiltinCallee())
David Blaikie1f4ff152012-07-16 20:47:22 +00008606 return;
Mike Stump11289f42009-09-09 15:08:12 +00008607
Ted Kremenek43fb8b02007-11-25 00:58:00 +00008608 // Emit the diagnostic.
David Blaikie1f4ff152012-07-16 20:47:22 +00008609 Diag(Loc, diag::warn_floatingpoint_eq)
8610 << LHS->getSourceRange() << RHS->getSourceRange();
Ted Kremenek43fb8b02007-11-25 00:58:00 +00008611}
John McCallca01b222010-01-04 23:21:16 +00008612
John McCall70aa5392010-01-06 05:24:50 +00008613//===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
8614//===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
John McCallca01b222010-01-04 23:21:16 +00008615
John McCall70aa5392010-01-06 05:24:50 +00008616namespace {
John McCallca01b222010-01-04 23:21:16 +00008617
John McCall70aa5392010-01-06 05:24:50 +00008618/// Structure recording the 'active' range of an integer-valued
8619/// expression.
8620struct IntRange {
8621 /// The number of bits active in the int.
8622 unsigned Width;
John McCallca01b222010-01-04 23:21:16 +00008623
John McCall70aa5392010-01-06 05:24:50 +00008624 /// True if the int is known not to have negative values.
8625 bool NonNegative;
John McCallca01b222010-01-04 23:21:16 +00008626
John McCall70aa5392010-01-06 05:24:50 +00008627 IntRange(unsigned Width, bool NonNegative)
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00008628 : Width(Width), NonNegative(NonNegative) {}
John McCallca01b222010-01-04 23:21:16 +00008629
John McCall817d4af2010-11-10 23:38:19 +00008630 /// Returns the range of the bool type.
John McCall70aa5392010-01-06 05:24:50 +00008631 static IntRange forBoolType() {
8632 return IntRange(1, true);
John McCall263a48b2010-01-04 23:31:57 +00008633 }
8634
John McCall817d4af2010-11-10 23:38:19 +00008635 /// Returns the range of an opaque value of the given integral type.
8636 static IntRange forValueOfType(ASTContext &C, QualType T) {
8637 return forValueOfCanonicalType(C,
8638 T->getCanonicalTypeInternal().getTypePtr());
John McCall263a48b2010-01-04 23:31:57 +00008639 }
8640
John McCall817d4af2010-11-10 23:38:19 +00008641 /// Returns the range of an opaque value of a canonical integral type.
8642 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
John McCall70aa5392010-01-06 05:24:50 +00008643 assert(T->isCanonicalUnqualified());
8644
8645 if (const VectorType *VT = dyn_cast<VectorType>(T))
8646 T = VT->getElementType().getTypePtr();
8647 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
8648 T = CT->getElementType().getTypePtr();
Justin Bogner4f42fc42014-07-21 18:01:53 +00008649 if (const AtomicType *AT = dyn_cast<AtomicType>(T))
8650 T = AT->getValueType().getTypePtr();
John McCallcc7e5bf2010-05-06 08:58:33 +00008651
Roman Lebedevca1aaac2017-10-21 16:44:03 +00008652 if (!C.getLangOpts().CPlusPlus) {
8653 // For enum types in C code, use the underlying datatype.
8654 if (const EnumType *ET = dyn_cast<EnumType>(T))
8655 T = ET->getDecl()->getIntegerType().getDesugaredType(C).getTypePtr();
8656 } else if (const EnumType *ET = dyn_cast<EnumType>(T)) {
8657 // For enum types in C++, use the known bit width of the enumerators.
David Majnemer6a426652013-06-07 22:07:20 +00008658 EnumDecl *Enum = ET->getDecl();
Richard Smith371e9e8a2017-12-06 03:00:51 +00008659 // In C++11, enums can have a fixed underlying type. Use this type to
8660 // compute the range.
8661 if (Enum->isFixed()) {
Erich Keane69dbbb02017-09-21 19:58:55 +00008662 return IntRange(C.getIntWidth(QualType(T, 0)),
8663 !ET->isSignedIntegerOrEnumerationType());
Richard Smith371e9e8a2017-12-06 03:00:51 +00008664 }
John McCall18a2c2c2010-11-09 22:22:12 +00008665
David Majnemer6a426652013-06-07 22:07:20 +00008666 unsigned NumPositive = Enum->getNumPositiveBits();
8667 unsigned NumNegative = Enum->getNumNegativeBits();
John McCallcc7e5bf2010-05-06 08:58:33 +00008668
David Majnemer6a426652013-06-07 22:07:20 +00008669 if (NumNegative == 0)
8670 return IntRange(NumPositive, true/*NonNegative*/);
8671 else
8672 return IntRange(std::max(NumPositive + 1, NumNegative),
8673 false/*NonNegative*/);
John McCallcc7e5bf2010-05-06 08:58:33 +00008674 }
John McCall70aa5392010-01-06 05:24:50 +00008675
8676 const BuiltinType *BT = cast<BuiltinType>(T);
8677 assert(BT->isInteger());
8678
8679 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
8680 }
8681
John McCall817d4af2010-11-10 23:38:19 +00008682 /// Returns the "target" range of a canonical integral type, i.e.
8683 /// the range of values expressible in the type.
8684 ///
8685 /// This matches forValueOfCanonicalType except that enums have the
8686 /// full range of their type, not the range of their enumerators.
8687 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
8688 assert(T->isCanonicalUnqualified());
8689
8690 if (const VectorType *VT = dyn_cast<VectorType>(T))
8691 T = VT->getElementType().getTypePtr();
8692 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
8693 T = CT->getElementType().getTypePtr();
Justin Bogner4f42fc42014-07-21 18:01:53 +00008694 if (const AtomicType *AT = dyn_cast<AtomicType>(T))
8695 T = AT->getValueType().getTypePtr();
John McCall817d4af2010-11-10 23:38:19 +00008696 if (const EnumType *ET = dyn_cast<EnumType>(T))
Douglas Gregor3168dcf2011-09-08 23:29:05 +00008697 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
John McCall817d4af2010-11-10 23:38:19 +00008698
8699 const BuiltinType *BT = cast<BuiltinType>(T);
8700 assert(BT->isInteger());
8701
8702 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
8703 }
8704
8705 /// Returns the supremum of two ranges: i.e. their conservative merge.
John McCallff96ccd2010-02-23 19:22:29 +00008706 static IntRange join(IntRange L, IntRange R) {
John McCall70aa5392010-01-06 05:24:50 +00008707 return IntRange(std::max(L.Width, R.Width),
John McCall2ce81ad2010-01-06 22:07:33 +00008708 L.NonNegative && R.NonNegative);
8709 }
8710
John McCall817d4af2010-11-10 23:38:19 +00008711 /// Returns the infinum of two ranges: i.e. their aggressive merge.
John McCallff96ccd2010-02-23 19:22:29 +00008712 static IntRange meet(IntRange L, IntRange R) {
John McCall2ce81ad2010-01-06 22:07:33 +00008713 return IntRange(std::min(L.Width, R.Width),
8714 L.NonNegative || R.NonNegative);
John McCall70aa5392010-01-06 05:24:50 +00008715 }
8716};
8717
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00008718} // namespace
8719
8720static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
8721 unsigned MaxWidth) {
John McCall70aa5392010-01-06 05:24:50 +00008722 if (value.isSigned() && value.isNegative())
8723 return IntRange(value.getMinSignedBits(), false);
8724
8725 if (value.getBitWidth() > MaxWidth)
Jay Foad6d4db0c2010-12-07 08:25:34 +00008726 value = value.trunc(MaxWidth);
John McCall70aa5392010-01-06 05:24:50 +00008727
8728 // isNonNegative() just checks the sign bit without considering
8729 // signedness.
8730 return IntRange(value.getActiveBits(), true);
8731}
8732
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00008733static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
8734 unsigned MaxWidth) {
John McCall70aa5392010-01-06 05:24:50 +00008735 if (result.isInt())
8736 return GetValueRange(C, result.getInt(), MaxWidth);
8737
8738 if (result.isVector()) {
John McCall74430522010-01-06 22:57:21 +00008739 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
8740 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
8741 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
8742 R = IntRange::join(R, El);
8743 }
John McCall70aa5392010-01-06 05:24:50 +00008744 return R;
8745 }
8746
8747 if (result.isComplexInt()) {
8748 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
8749 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
8750 return IntRange::join(R, I);
John McCall263a48b2010-01-04 23:31:57 +00008751 }
8752
8753 // This can happen with lossless casts to intptr_t of "based" lvalues.
8754 // Assume it might use arbitrary bits.
John McCall74430522010-01-06 22:57:21 +00008755 // FIXME: The only reason we need to pass the type in here is to get
8756 // the sign right on this one case. It would be nice if APValue
8757 // preserved this.
Eli Friedmanfd5e54d2012-01-04 23:13:47 +00008758 assert(result.isLValue() || result.isAddrLabelDiff());
Douglas Gregor61b6e492011-05-21 16:28:01 +00008759 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
John McCall263a48b2010-01-04 23:31:57 +00008760}
John McCall70aa5392010-01-06 05:24:50 +00008761
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00008762static QualType GetExprType(const Expr *E) {
Eli Friedmane6d33952013-07-08 20:20:06 +00008763 QualType Ty = E->getType();
8764 if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>())
8765 Ty = AtomicRHS->getValueType();
8766 return Ty;
8767}
8768
John McCall70aa5392010-01-06 05:24:50 +00008769/// Pseudo-evaluate the given integer expression, estimating the
8770/// range of values it might take.
8771///
8772/// \param MaxWidth - the width to which the value will be truncated
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00008773static IntRange GetExprRange(ASTContext &C, const Expr *E, unsigned MaxWidth) {
John McCall70aa5392010-01-06 05:24:50 +00008774 E = E->IgnoreParens();
8775
8776 // Try a full evaluation first.
8777 Expr::EvalResult result;
Richard Smith7b553f12011-10-29 00:50:52 +00008778 if (E->EvaluateAsRValue(result, C))
Eli Friedmane6d33952013-07-08 20:20:06 +00008779 return GetValueRange(C, result.Val, GetExprType(E), MaxWidth);
John McCall70aa5392010-01-06 05:24:50 +00008780
8781 // I think we only want to look through implicit casts here; if the
8782 // user has an explicit widening cast, we should treat the value as
8783 // being of the new, wider type.
Daniel Marjamakid3e1ded2016-01-25 09:29:38 +00008784 if (const auto *CE = dyn_cast<ImplicitCastExpr>(E)) {
Eli Friedman8349dc12011-12-15 02:41:52 +00008785 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
John McCall70aa5392010-01-06 05:24:50 +00008786 return GetExprRange(C, CE->getSubExpr(), MaxWidth);
8787
Eli Friedmane6d33952013-07-08 20:20:06 +00008788 IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE));
John McCall70aa5392010-01-06 05:24:50 +00008789
George Burgess IVdf1ed002016-01-13 01:52:39 +00008790 bool isIntegerCast = CE->getCastKind() == CK_IntegralCast ||
8791 CE->getCastKind() == CK_BooleanToSignedIntegral;
John McCall2ce81ad2010-01-06 22:07:33 +00008792
John McCall70aa5392010-01-06 05:24:50 +00008793 // Assume that non-integer casts can span the full range of the type.
John McCall2ce81ad2010-01-06 22:07:33 +00008794 if (!isIntegerCast)
John McCall70aa5392010-01-06 05:24:50 +00008795 return OutputTypeRange;
8796
8797 IntRange SubRange
8798 = GetExprRange(C, CE->getSubExpr(),
8799 std::min(MaxWidth, OutputTypeRange.Width));
8800
8801 // Bail out if the subexpr's range is as wide as the cast type.
8802 if (SubRange.Width >= OutputTypeRange.Width)
8803 return OutputTypeRange;
8804
8805 // Otherwise, we take the smaller width, and we're non-negative if
8806 // either the output type or the subexpr is.
8807 return IntRange(SubRange.Width,
8808 SubRange.NonNegative || OutputTypeRange.NonNegative);
8809 }
8810
Daniel Marjamakid3e1ded2016-01-25 09:29:38 +00008811 if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
John McCall70aa5392010-01-06 05:24:50 +00008812 // If we can fold the condition, just take that operand.
8813 bool CondResult;
8814 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
8815 return GetExprRange(C, CondResult ? CO->getTrueExpr()
8816 : CO->getFalseExpr(),
8817 MaxWidth);
8818
8819 // Otherwise, conservatively merge.
8820 IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
8821 IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
8822 return IntRange::join(L, R);
8823 }
8824
Daniel Marjamakid3e1ded2016-01-25 09:29:38 +00008825 if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
John McCall70aa5392010-01-06 05:24:50 +00008826 switch (BO->getOpcode()) {
Richard Smithc70f1d62017-12-14 15:16:18 +00008827 case BO_Cmp:
8828 llvm_unreachable("builtin <=> should have class type");
John McCall70aa5392010-01-06 05:24:50 +00008829
8830 // Boolean-valued operations are single-bit and positive.
John McCalle3027922010-08-25 11:45:40 +00008831 case BO_LAnd:
8832 case BO_LOr:
8833 case BO_LT:
8834 case BO_GT:
8835 case BO_LE:
8836 case BO_GE:
8837 case BO_EQ:
8838 case BO_NE:
John McCall70aa5392010-01-06 05:24:50 +00008839 return IntRange::forBoolType();
8840
John McCallc3688382011-07-13 06:35:24 +00008841 // The type of the assignments is the type of the LHS, so the RHS
8842 // is not necessarily the same type.
John McCalle3027922010-08-25 11:45:40 +00008843 case BO_MulAssign:
8844 case BO_DivAssign:
8845 case BO_RemAssign:
8846 case BO_AddAssign:
8847 case BO_SubAssign:
John McCallc3688382011-07-13 06:35:24 +00008848 case BO_XorAssign:
8849 case BO_OrAssign:
8850 // TODO: bitfields?
Eli Friedmane6d33952013-07-08 20:20:06 +00008851 return IntRange::forValueOfType(C, GetExprType(E));
John McCallff96ccd2010-02-23 19:22:29 +00008852
John McCallc3688382011-07-13 06:35:24 +00008853 // Simple assignments just pass through the RHS, which will have
8854 // been coerced to the LHS type.
8855 case BO_Assign:
8856 // TODO: bitfields?
8857 return GetExprRange(C, BO->getRHS(), MaxWidth);
8858
John McCall70aa5392010-01-06 05:24:50 +00008859 // Operations with opaque sources are black-listed.
John McCalle3027922010-08-25 11:45:40 +00008860 case BO_PtrMemD:
8861 case BO_PtrMemI:
Eli Friedmane6d33952013-07-08 20:20:06 +00008862 return IntRange::forValueOfType(C, GetExprType(E));
John McCall70aa5392010-01-06 05:24:50 +00008863
John McCall2ce81ad2010-01-06 22:07:33 +00008864 // Bitwise-and uses the *infinum* of the two source ranges.
John McCalle3027922010-08-25 11:45:40 +00008865 case BO_And:
8866 case BO_AndAssign:
John McCall2ce81ad2010-01-06 22:07:33 +00008867 return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
8868 GetExprRange(C, BO->getRHS(), MaxWidth));
8869
John McCall70aa5392010-01-06 05:24:50 +00008870 // Left shift gets black-listed based on a judgement call.
John McCalle3027922010-08-25 11:45:40 +00008871 case BO_Shl:
John McCall1bff9932010-04-07 01:14:35 +00008872 // ...except that we want to treat '1 << (blah)' as logically
8873 // positive. It's an important idiom.
8874 if (IntegerLiteral *I
8875 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
8876 if (I->getValue() == 1) {
Eli Friedmane6d33952013-07-08 20:20:06 +00008877 IntRange R = IntRange::forValueOfType(C, GetExprType(E));
John McCall1bff9932010-04-07 01:14:35 +00008878 return IntRange(R.Width, /*NonNegative*/ true);
8879 }
8880 }
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +00008881 LLVM_FALLTHROUGH;
John McCall1bff9932010-04-07 01:14:35 +00008882
John McCalle3027922010-08-25 11:45:40 +00008883 case BO_ShlAssign:
Eli Friedmane6d33952013-07-08 20:20:06 +00008884 return IntRange::forValueOfType(C, GetExprType(E));
John McCall70aa5392010-01-06 05:24:50 +00008885
John McCall2ce81ad2010-01-06 22:07:33 +00008886 // Right shift by a constant can narrow its left argument.
John McCalle3027922010-08-25 11:45:40 +00008887 case BO_Shr:
8888 case BO_ShrAssign: {
John McCall2ce81ad2010-01-06 22:07:33 +00008889 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
8890
8891 // If the shift amount is a positive constant, drop the width by
8892 // that much.
8893 llvm::APSInt shift;
8894 if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
8895 shift.isNonNegative()) {
8896 unsigned zext = shift.getZExtValue();
8897 if (zext >= L.Width)
8898 L.Width = (L.NonNegative ? 0 : 1);
8899 else
8900 L.Width -= zext;
8901 }
8902
8903 return L;
8904 }
8905
8906 // Comma acts as its right operand.
John McCalle3027922010-08-25 11:45:40 +00008907 case BO_Comma:
John McCall70aa5392010-01-06 05:24:50 +00008908 return GetExprRange(C, BO->getRHS(), MaxWidth);
8909
John McCall2ce81ad2010-01-06 22:07:33 +00008910 // Black-list pointer subtractions.
John McCalle3027922010-08-25 11:45:40 +00008911 case BO_Sub:
John McCall70aa5392010-01-06 05:24:50 +00008912 if (BO->getLHS()->getType()->isPointerType())
Eli Friedmane6d33952013-07-08 20:20:06 +00008913 return IntRange::forValueOfType(C, GetExprType(E));
John McCall51431812011-07-14 22:39:48 +00008914 break;
Ted Kremenekc8b188d2010-02-16 01:46:59 +00008915
John McCall51431812011-07-14 22:39:48 +00008916 // The width of a division result is mostly determined by the size
8917 // of the LHS.
8918 case BO_Div: {
8919 // Don't 'pre-truncate' the operands.
Eli Friedmane6d33952013-07-08 20:20:06 +00008920 unsigned opWidth = C.getIntWidth(GetExprType(E));
John McCall51431812011-07-14 22:39:48 +00008921 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
8922
8923 // If the divisor is constant, use that.
8924 llvm::APSInt divisor;
8925 if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
8926 unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
8927 if (log2 >= L.Width)
8928 L.Width = (L.NonNegative ? 0 : 1);
8929 else
8930 L.Width = std::min(L.Width - log2, MaxWidth);
8931 return L;
8932 }
8933
8934 // Otherwise, just use the LHS's width.
8935 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
8936 return IntRange(L.Width, L.NonNegative && R.NonNegative);
8937 }
8938
8939 // The result of a remainder can't be larger than the result of
8940 // either side.
8941 case BO_Rem: {
8942 // Don't 'pre-truncate' the operands.
Eli Friedmane6d33952013-07-08 20:20:06 +00008943 unsigned opWidth = C.getIntWidth(GetExprType(E));
John McCall51431812011-07-14 22:39:48 +00008944 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
8945 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
8946
8947 IntRange meet = IntRange::meet(L, R);
8948 meet.Width = std::min(meet.Width, MaxWidth);
8949 return meet;
8950 }
8951
8952 // The default behavior is okay for these.
8953 case BO_Mul:
8954 case BO_Add:
8955 case BO_Xor:
8956 case BO_Or:
John McCall70aa5392010-01-06 05:24:50 +00008957 break;
8958 }
8959
John McCall51431812011-07-14 22:39:48 +00008960 // The default case is to treat the operation as if it were closed
8961 // on the narrowest type that encompasses both operands.
John McCall70aa5392010-01-06 05:24:50 +00008962 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
8963 IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
8964 return IntRange::join(L, R);
8965 }
8966
Daniel Marjamakid3e1ded2016-01-25 09:29:38 +00008967 if (const auto *UO = dyn_cast<UnaryOperator>(E)) {
John McCall70aa5392010-01-06 05:24:50 +00008968 switch (UO->getOpcode()) {
8969 // Boolean-valued operations are white-listed.
John McCalle3027922010-08-25 11:45:40 +00008970 case UO_LNot:
John McCall70aa5392010-01-06 05:24:50 +00008971 return IntRange::forBoolType();
8972
8973 // Operations with opaque sources are black-listed.
John McCalle3027922010-08-25 11:45:40 +00008974 case UO_Deref:
8975 case UO_AddrOf: // should be impossible
Eli Friedmane6d33952013-07-08 20:20:06 +00008976 return IntRange::forValueOfType(C, GetExprType(E));
John McCall70aa5392010-01-06 05:24:50 +00008977
8978 default:
8979 return GetExprRange(C, UO->getSubExpr(), MaxWidth);
8980 }
8981 }
8982
Daniel Marjamakid3e1ded2016-01-25 09:29:38 +00008983 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
Ted Kremeneka553fbf2013-10-14 18:55:27 +00008984 return GetExprRange(C, OVE->getSourceExpr(), MaxWidth);
8985
Daniel Marjamakid3e1ded2016-01-25 09:29:38 +00008986 if (const auto *BitField = E->getSourceBitField())
Richard Smithcaf33902011-10-10 18:28:20 +00008987 return IntRange(BitField->getBitWidthValue(C),
Douglas Gregor61b6e492011-05-21 16:28:01 +00008988 BitField->getType()->isUnsignedIntegerOrEnumerationType());
John McCall70aa5392010-01-06 05:24:50 +00008989
Eli Friedmane6d33952013-07-08 20:20:06 +00008990 return IntRange::forValueOfType(C, GetExprType(E));
John McCall70aa5392010-01-06 05:24:50 +00008991}
John McCall263a48b2010-01-04 23:31:57 +00008992
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00008993static IntRange GetExprRange(ASTContext &C, const Expr *E) {
Eli Friedmane6d33952013-07-08 20:20:06 +00008994 return GetExprRange(C, E, C.getIntWidth(GetExprType(E)));
John McCallcc7e5bf2010-05-06 08:58:33 +00008995}
8996
John McCall263a48b2010-01-04 23:31:57 +00008997/// Checks whether the given value, which currently has the given
8998/// source semantics, has the same value when coerced through the
8999/// target semantics.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009000static bool IsSameFloatAfterCast(const llvm::APFloat &value,
9001 const llvm::fltSemantics &Src,
9002 const llvm::fltSemantics &Tgt) {
John McCall263a48b2010-01-04 23:31:57 +00009003 llvm::APFloat truncated = value;
9004
9005 bool ignored;
9006 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
9007 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
9008
9009 return truncated.bitwiseIsEqual(value);
9010}
9011
9012/// Checks whether the given value, which currently has the given
9013/// source semantics, has the same value when coerced through the
9014/// target semantics.
9015///
9016/// The value might be a vector of floats (or a complex number).
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009017static bool IsSameFloatAfterCast(const APValue &value,
9018 const llvm::fltSemantics &Src,
9019 const llvm::fltSemantics &Tgt) {
John McCall263a48b2010-01-04 23:31:57 +00009020 if (value.isFloat())
9021 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
9022
9023 if (value.isVector()) {
9024 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
9025 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
9026 return false;
9027 return true;
9028 }
9029
9030 assert(value.isComplexFloat());
9031 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
9032 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
9033}
9034
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009035static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
John McCallcc7e5bf2010-05-06 08:58:33 +00009036
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009037static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) {
Ted Kremenek6274be42010-09-23 21:43:44 +00009038 // Suppress cases where we are comparing against an enum constant.
9039 if (const DeclRefExpr *DR =
9040 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
9041 if (isa<EnumConstantDecl>(DR->getDecl()))
Roman Lebedev6de129e2017-10-15 20:13:17 +00009042 return true;
Ted Kremenek6274be42010-09-23 21:43:44 +00009043
9044 // Suppress cases where the '0' value is expanded from a macro.
9045 if (E->getLocStart().isMacroID())
Roman Lebedev6de129e2017-10-15 20:13:17 +00009046 return true;
Ted Kremenek6274be42010-09-23 21:43:44 +00009047
Roman Lebedev6de129e2017-10-15 20:13:17 +00009048 return false;
9049}
9050
Richard Smith692f66ab2017-12-06 19:23:19 +00009051static bool isKnownToHaveUnsignedValue(Expr *E) {
9052 return E->getType()->isIntegerType() &&
Roman Lebedev6de129e2017-10-15 20:13:17 +00009053 (!E->getType()->isSignedIntegerType() ||
9054 !E->IgnoreParenImpCasts()->getType()->isSignedIntegerType());
9055}
9056
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009057namespace {
Richard Smitha5370fb2017-12-08 22:57:11 +00009058/// The promoted range of values of a type. In general this has the
9059/// following structure:
9060///
9061/// |-----------| . . . |-----------|
9062/// ^ ^ ^ ^
9063/// Min HoleMin HoleMax Max
9064///
9065/// ... where there is only a hole if a signed type is promoted to unsigned
9066/// (in which case Min and Max are the smallest and largest representable
9067/// values).
9068struct PromotedRange {
9069 // Min, or HoleMax if there is a hole.
9070 llvm::APSInt PromotedMin;
9071 // Max, or HoleMin if there is a hole.
9072 llvm::APSInt PromotedMax;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009073
Richard Smitha5370fb2017-12-08 22:57:11 +00009074 PromotedRange(IntRange R, unsigned BitWidth, bool Unsigned) {
9075 if (R.Width == 0)
9076 PromotedMin = PromotedMax = llvm::APSInt(BitWidth, Unsigned);
9077 else if (R.Width >= BitWidth && !Unsigned) {
9078 // Promotion made the type *narrower*. This happens when promoting
9079 // a < 32-bit unsigned / <= 32-bit signed bit-field to 'signed int'.
9080 // Treat all values of 'signed int' as being in range for now.
9081 PromotedMin = llvm::APSInt::getMinValue(BitWidth, Unsigned);
9082 PromotedMax = llvm::APSInt::getMaxValue(BitWidth, Unsigned);
9083 } else {
9084 PromotedMin = llvm::APSInt::getMinValue(R.Width, R.NonNegative)
9085 .extOrTrunc(BitWidth);
9086 PromotedMin.setIsUnsigned(Unsigned);
9087
9088 PromotedMax = llvm::APSInt::getMaxValue(R.Width, R.NonNegative)
9089 .extOrTrunc(BitWidth);
9090 PromotedMax.setIsUnsigned(Unsigned);
9091 }
9092 }
9093
9094 // Determine whether this range is contiguous (has no hole).
9095 bool isContiguous() const { return PromotedMin <= PromotedMax; }
9096
9097 // Where a constant value is within the range.
9098 enum ComparisonResult {
9099 LT = 0x1,
9100 LE = 0x2,
9101 GT = 0x4,
9102 GE = 0x8,
9103 EQ = 0x10,
9104 NE = 0x20,
9105 InRangeFlag = 0x40,
9106
9107 Less = LE | LT | NE,
9108 Min = LE | InRangeFlag,
9109 InRange = InRangeFlag,
9110 Max = GE | InRangeFlag,
9111 Greater = GE | GT | NE,
9112
9113 OnlyValue = LE | GE | EQ | InRangeFlag,
9114 InHole = NE
9115 };
9116
9117 ComparisonResult compare(const llvm::APSInt &Value) const {
9118 assert(Value.getBitWidth() == PromotedMin.getBitWidth() &&
9119 Value.isUnsigned() == PromotedMin.isUnsigned());
9120 if (!isContiguous()) {
9121 assert(Value.isUnsigned() && "discontiguous range for signed compare");
9122 if (Value.isMinValue()) return Min;
9123 if (Value.isMaxValue()) return Max;
9124 if (Value >= PromotedMin) return InRange;
9125 if (Value <= PromotedMax) return InRange;
9126 return InHole;
9127 }
9128
9129 switch (llvm::APSInt::compareValues(Value, PromotedMin)) {
9130 case -1: return Less;
9131 case 0: return PromotedMin == PromotedMax ? OnlyValue : Min;
9132 case 1:
9133 switch (llvm::APSInt::compareValues(Value, PromotedMax)) {
9134 case -1: return InRange;
9135 case 0: return Max;
9136 case 1: return Greater;
9137 }
9138 }
9139
9140 llvm_unreachable("impossible compare result");
9141 }
9142
Richard Smithc70f1d62017-12-14 15:16:18 +00009143 static llvm::Optional<StringRef>
9144 constantValue(BinaryOperatorKind Op, ComparisonResult R, bool ConstantOnRHS) {
9145 if (Op == BO_Cmp) {
9146 ComparisonResult LTFlag = LT, GTFlag = GT;
9147 if (ConstantOnRHS) std::swap(LTFlag, GTFlag);
9148
9149 if (R & EQ) return StringRef("'std::strong_ordering::equal'");
9150 if (R & LTFlag) return StringRef("'std::strong_ordering::less'");
9151 if (R & GTFlag) return StringRef("'std::strong_ordering::greater'");
9152 return llvm::None;
9153 }
9154
Richard Smitha5370fb2017-12-08 22:57:11 +00009155 ComparisonResult TrueFlag, FalseFlag;
9156 if (Op == BO_EQ) {
9157 TrueFlag = EQ;
9158 FalseFlag = NE;
9159 } else if (Op == BO_NE) {
9160 TrueFlag = NE;
9161 FalseFlag = EQ;
9162 } else {
9163 if ((Op == BO_LT || Op == BO_GE) ^ ConstantOnRHS) {
9164 TrueFlag = LT;
9165 FalseFlag = GE;
9166 } else {
9167 TrueFlag = GT;
9168 FalseFlag = LE;
9169 }
9170 if (Op == BO_GE || Op == BO_LE)
9171 std::swap(TrueFlag, FalseFlag);
9172 }
9173 if (R & TrueFlag)
Richard Smithc70f1d62017-12-14 15:16:18 +00009174 return StringRef("true");
Richard Smitha5370fb2017-12-08 22:57:11 +00009175 if (R & FalseFlag)
Richard Smithc70f1d62017-12-14 15:16:18 +00009176 return StringRef("false");
Richard Smitha5370fb2017-12-08 22:57:11 +00009177 return llvm::None;
9178 }
Roman Lebedev6de129e2017-10-15 20:13:17 +00009179};
John McCallcc7e5bf2010-05-06 08:58:33 +00009180}
9181
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009182static bool HasEnumType(Expr *E) {
John McCall2551c1b2010-10-06 00:25:24 +00009183 // Strip off implicit integral promotions.
9184 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Argyrios Kyrtzidis15a9edc2010-10-07 21:52:18 +00009185 if (ICE->getCastKind() != CK_IntegralCast &&
9186 ICE->getCastKind() != CK_NoOp)
John McCall2551c1b2010-10-06 00:25:24 +00009187 break;
Argyrios Kyrtzidis15a9edc2010-10-07 21:52:18 +00009188 E = ICE->getSubExpr();
John McCall2551c1b2010-10-06 00:25:24 +00009189 }
9190
9191 return E->getType()->isEnumeralType();
9192}
9193
Richard Smith692f66ab2017-12-06 19:23:19 +00009194static int classifyConstantValue(Expr *Constant) {
9195 // The values of this enumeration are used in the diagnostics
9196 // diag::warn_out_of_range_compare and diag::warn_tautological_bool_compare.
9197 enum ConstantValueKind {
9198 Miscellaneous = 0,
9199 LiteralTrue,
9200 LiteralFalse
9201 };
9202 if (auto *BL = dyn_cast<CXXBoolLiteralExpr>(Constant))
9203 return BL->getValue() ? ConstantValueKind::LiteralTrue
9204 : ConstantValueKind::LiteralFalse;
9205 return ConstantValueKind::Miscellaneous;
9206}
9207
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009208static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E,
9209 Expr *Constant, Expr *Other,
9210 const llvm::APSInt &Value,
9211 bool RhsConstant) {
Hans Wennborg5bb88e02017-12-08 05:19:12 +00009212 if (S.inTemplateInstantiation())
9213 return false;
9214
Richard Smitha5370fb2017-12-08 22:57:11 +00009215 Expr *OriginalOther = Other;
9216
Hans Wennborg5bb88e02017-12-08 05:19:12 +00009217 Constant = Constant->IgnoreParenImpCasts();
9218 Other = Other->IgnoreParenImpCasts();
9219
Richard Smitha5370fb2017-12-08 22:57:11 +00009220 // Suppress warnings on tautological comparisons between values of the same
9221 // enumeration type. There are only two ways we could warn on this:
9222 // - If the constant is outside the range of representable values of
9223 // the enumeration. In such a case, we should warn about the cast
9224 // to enumeration type, not about the comparison.
9225 // - If the constant is the maximum / minimum in-range value. For an
9226 // enumeratin type, such comparisons can be meaningful and useful.
9227 if (Constant->getType()->isEnumeralType() &&
9228 S.Context.hasSameUnqualifiedType(Constant->getType(), Other->getType()))
9229 return false;
9230
Hans Wennborg5bb88e02017-12-08 05:19:12 +00009231 // TODO: Investigate using GetExprRange() to get tighter bounds
9232 // on the bit ranges.
9233 QualType OtherT = Other->getType();
9234 if (const auto *AT = OtherT->getAs<AtomicType>())
9235 OtherT = AT->getValueType();
9236 IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT);
9237
9238 // Whether we're treating Other as being a bool because of the form of
9239 // expression despite it having another type (typically 'int' in C).
9240 bool OtherIsBooleanDespiteType =
9241 !OtherT->isBooleanType() && Other->isKnownToHaveBooleanValue();
9242 if (OtherIsBooleanDespiteType)
9243 OtherRange = IntRange::forBoolType();
9244
Richard Smitha5370fb2017-12-08 22:57:11 +00009245 // Determine the promoted range of the other type and see if a comparison of
9246 // the constant against that range is tautological.
9247 PromotedRange OtherPromotedRange(OtherRange, Value.getBitWidth(),
9248 Value.isUnsigned());
9249 auto Cmp = OtherPromotedRange.compare(Value);
9250 auto Result = PromotedRange::constantValue(E->getOpcode(), Cmp, RhsConstant);
9251 if (!Result)
9252 return false;
Hans Wennborg5791ce72017-12-08 16:54:08 +00009253
Richard Smitha5370fb2017-12-08 22:57:11 +00009254 // Suppress the diagnostic for an in-range comparison if the constant comes
9255 // from a macro or enumerator. We don't want to diagnose
9256 //
9257 // some_long_value <= INT_MAX
9258 //
9259 // when sizeof(int) == sizeof(long).
9260 bool InRange = Cmp & PromotedRange::InRangeFlag;
9261 if (InRange && IsEnumConstOrFromMacro(S, Constant))
9262 return false;
Ted Kremenekb7d7dd42013-03-15 21:50:10 +00009263
9264 // If this is a comparison to an enum constant, include that
9265 // constant in the diagnostic.
Craig Topperc3ec1492014-05-26 06:22:03 +00009266 const EnumConstantDecl *ED = nullptr;
Ted Kremenekb7d7dd42013-03-15 21:50:10 +00009267 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
9268 ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
9269
Richard Smitha5370fb2017-12-08 22:57:11 +00009270 // Should be enough for uint128 (39 decimal digits)
Ted Kremenekb7d7dd42013-03-15 21:50:10 +00009271 SmallString<64> PrettySourceValue;
9272 llvm::raw_svector_ostream OS(PrettySourceValue);
9273 if (ED)
Ted Kremeneke943ce12013-03-15 22:02:46 +00009274 OS << '\'' << *ED << "' (" << Value << ")";
Ted Kremenekb7d7dd42013-03-15 21:50:10 +00009275 else
9276 OS << Value;
9277
Richard Smitha5370fb2017-12-08 22:57:11 +00009278 // FIXME: We use a somewhat different formatting for the in-range cases and
9279 // cases involving boolean values for historical reasons. We should pick a
9280 // consistent way of presenting these diagnostics.
9281 if (!InRange || Other->isKnownToHaveBooleanValue()) {
9282 S.DiagRuntimeBehavior(
9283 E->getOperatorLoc(), E,
9284 S.PDiag(!InRange ? diag::warn_out_of_range_compare
9285 : diag::warn_tautological_bool_compare)
9286 << OS.str() << classifyConstantValue(Constant)
9287 << OtherT << OtherIsBooleanDespiteType << *Result
9288 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange());
9289 } else {
9290 unsigned Diag = (isKnownToHaveUnsignedValue(OriginalOther) && Value == 0)
9291 ? (HasEnumType(OriginalOther)
9292 ? diag::warn_unsigned_enum_always_true_comparison
9293 : diag::warn_unsigned_always_true_comparison)
9294 : diag::warn_tautological_constant_compare;
Roman Lebedev6de129e2017-10-15 20:13:17 +00009295
Richard Smitha5370fb2017-12-08 22:57:11 +00009296 S.Diag(E->getOperatorLoc(), Diag)
9297 << RhsConstant << OtherT << E->getOpcodeStr() << OS.str() << *Result
9298 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
9299 }
9300
9301 return true;
Fariborz Jahanianb1885422012-09-18 17:37:21 +00009302}
9303
John McCallcc7e5bf2010-05-06 08:58:33 +00009304/// Analyze the operands of the given comparison. Implements the
9305/// fallback case from AnalyzeComparison.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009306static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
John McCallacf0ee52010-10-08 02:01:28 +00009307 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
9308 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
John McCallcc7e5bf2010-05-06 08:58:33 +00009309}
John McCall263a48b2010-01-04 23:31:57 +00009310
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009311/// Implements -Wsign-compare.
John McCallca01b222010-01-04 23:21:16 +00009312///
Richard Trieu82402a02011-09-15 21:56:47 +00009313/// \param E the binary operator to check for warnings
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009314static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
John McCallcc7e5bf2010-05-06 08:58:33 +00009315 // The type the comparison is being performed in.
9316 QualType T = E->getLHS()->getType();
Chandler Carruthb29a7432014-10-11 11:03:30 +00009317
9318 // Only analyze comparison operators where both sides have been converted to
9319 // the same type.
9320 if (!S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()))
9321 return AnalyzeImpConvsInComparison(S, E);
9322
9323 // Don't analyze value-dependent comparisons directly.
Fariborz Jahanian282071e2012-09-18 17:46:26 +00009324 if (E->isValueDependent())
9325 return AnalyzeImpConvsInComparison(S, E);
John McCallca01b222010-01-04 23:21:16 +00009326
Roman Lebedev6de129e2017-10-15 20:13:17 +00009327 Expr *LHS = E->getLHS();
9328 Expr *RHS = E->getRHS();
9329
Fariborz Jahanianb1885422012-09-18 17:37:21 +00009330 if (T->isIntegralType(S.Context)) {
9331 llvm::APSInt RHSValue;
Fariborz Jahanianb1885422012-09-18 17:37:21 +00009332 llvm::APSInt LHSValue;
Roman Lebedev6aa34aa2017-09-07 22:14:25 +00009333
Roman Lebedev6de129e2017-10-15 20:13:17 +00009334 bool IsRHSIntegralLiteral = RHS->isIntegerConstantExpr(RHSValue, S.Context);
9335 bool IsLHSIntegralLiteral = LHS->isIntegerConstantExpr(LHSValue, S.Context);
Roman Lebedevbd1fc222017-10-12 20:16:51 +00009336
Roman Lebedev6de129e2017-10-15 20:13:17 +00009337 // We don't care about expressions whose result is a constant.
9338 if (IsRHSIntegralLiteral && IsLHSIntegralLiteral)
9339 return AnalyzeImpConvsInComparison(S, E);
Roman Lebedev6f405db2017-10-12 22:03:20 +00009340
Roman Lebedev6de129e2017-10-15 20:13:17 +00009341 // We only care about expressions where just one side is literal
9342 if (IsRHSIntegralLiteral ^ IsLHSIntegralLiteral) {
9343 // Is the constant on the RHS or LHS?
9344 const bool RhsConstant = IsRHSIntegralLiteral;
9345 Expr *Const = RhsConstant ? RHS : LHS;
9346 Expr *Other = RhsConstant ? LHS : RHS;
9347 const llvm::APSInt &Value = RhsConstant ? RHSValue : LHSValue;
9348
9349 // Check whether an integer constant comparison results in a value
9350 // of 'true' or 'false'.
Roman Lebedev6de129e2017-10-15 20:13:17 +00009351 if (CheckTautologicalComparison(S, E, Const, Other, Value, RhsConstant))
9352 return AnalyzeImpConvsInComparison(S, E);
Roman Lebedev6de129e2017-10-15 20:13:17 +00009353 }
9354 }
9355
9356 if (!T->hasUnsignedIntegerRepresentation()) {
9357 // We don't do anything special if this isn't an unsigned integral
9358 // comparison: we're only interested in integral comparisons, and
9359 // signed comparisons only happen in cases we don't care to warn about.
Roman Lebedev6f405db2017-10-12 22:03:20 +00009360 return AnalyzeImpConvsInComparison(S, E);
Roman Lebedev6de129e2017-10-15 20:13:17 +00009361 }
9362
9363 LHS = LHS->IgnoreParenImpCasts();
9364 RHS = RHS->IgnoreParenImpCasts();
Roman Lebedev6aa34aa2017-09-07 22:14:25 +00009365
Alex Lorenzb57409f2018-02-07 20:45:39 +00009366 if (!S.getLangOpts().CPlusPlus) {
9367 // Avoid warning about comparison of integers with different signs when
9368 // RHS/LHS has a `typeof(E)` type whose sign is different from the sign of
9369 // the type of `E`.
9370 if (const auto *TET = dyn_cast<TypeOfExprType>(LHS->getType()))
9371 LHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts();
9372 if (const auto *TET = dyn_cast<TypeOfExprType>(RHS->getType()))
9373 RHS = TET->getUnderlyingExpr()->IgnoreParenImpCasts();
9374 }
9375
John McCallcc7e5bf2010-05-06 08:58:33 +00009376 // Check to see if one of the (unmodified) operands is of different
9377 // signedness.
9378 Expr *signedOperand, *unsignedOperand;
Richard Trieu82402a02011-09-15 21:56:47 +00009379 if (LHS->getType()->hasSignedIntegerRepresentation()) {
9380 assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
John McCallcc7e5bf2010-05-06 08:58:33 +00009381 "unsigned comparison between two signed integer expressions?");
Richard Trieu82402a02011-09-15 21:56:47 +00009382 signedOperand = LHS;
9383 unsignedOperand = RHS;
9384 } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
9385 signedOperand = RHS;
9386 unsignedOperand = LHS;
John McCallca01b222010-01-04 23:21:16 +00009387 } else {
John McCallcc7e5bf2010-05-06 08:58:33 +00009388 return AnalyzeImpConvsInComparison(S, E);
John McCallca01b222010-01-04 23:21:16 +00009389 }
9390
John McCallcc7e5bf2010-05-06 08:58:33 +00009391 // Otherwise, calculate the effective range of the signed operand.
9392 IntRange signedRange = GetExprRange(S.Context, signedOperand);
John McCall70aa5392010-01-06 05:24:50 +00009393
John McCallcc7e5bf2010-05-06 08:58:33 +00009394 // Go ahead and analyze implicit conversions in the operands. Note
9395 // that we skip the implicit conversions on both sides.
Richard Trieu82402a02011-09-15 21:56:47 +00009396 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
9397 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
John McCallca01b222010-01-04 23:21:16 +00009398
Roman Lebedev6aa34aa2017-09-07 22:14:25 +00009399 // If the signed range is non-negative, -Wsign-compare won't fire.
John McCallcc7e5bf2010-05-06 08:58:33 +00009400 if (signedRange.NonNegative)
Roman Lebedev6aa34aa2017-09-07 22:14:25 +00009401 return;
John McCallca01b222010-01-04 23:21:16 +00009402
9403 // For (in)equality comparisons, if the unsigned operand is a
9404 // constant which cannot collide with a overflowed signed operand,
9405 // then reinterpreting the signed operand as unsigned will not
9406 // change the result of the comparison.
John McCallcc7e5bf2010-05-06 08:58:33 +00009407 if (E->isEqualityOp()) {
9408 unsigned comparisonWidth = S.Context.getIntWidth(T);
9409 IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
John McCallca01b222010-01-04 23:21:16 +00009410
John McCallcc7e5bf2010-05-06 08:58:33 +00009411 // We should never be unable to prove that the unsigned operand is
9412 // non-negative.
9413 assert(unsignedRange.NonNegative && "unsigned range includes negative?");
9414
9415 if (unsignedRange.Width < comparisonWidth)
9416 return;
9417 }
9418
Douglas Gregorbfb4a212012-05-01 01:53:49 +00009419 S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
9420 S.PDiag(diag::warn_mixed_sign_comparison)
9421 << LHS->getType() << RHS->getType()
9422 << LHS->getSourceRange() << RHS->getSourceRange());
John McCallca01b222010-01-04 23:21:16 +00009423}
9424
John McCall1f425642010-11-11 03:21:53 +00009425/// Analyzes an attempt to assign the given value to a bitfield.
9426///
9427/// Returns true if there was something fishy about the attempt.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009428static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
9429 SourceLocation InitLoc) {
John McCall1f425642010-11-11 03:21:53 +00009430 assert(Bitfield->isBitField());
9431 if (Bitfield->isInvalidDecl())
9432 return false;
9433
John McCalldeebbcf2010-11-11 05:33:51 +00009434 // White-list bool bitfields.
Reid Klecknerad425622016-11-16 23:40:00 +00009435 QualType BitfieldType = Bitfield->getType();
9436 if (BitfieldType->isBooleanType())
9437 return false;
9438
9439 if (BitfieldType->isEnumeralType()) {
9440 EnumDecl *BitfieldEnumDecl = BitfieldType->getAs<EnumType>()->getDecl();
9441 // If the underlying enum type was not explicitly specified as an unsigned
9442 // type and the enum contain only positive values, MSVC++ will cause an
9443 // inconsistency by storing this as a signed type.
9444 if (S.getLangOpts().CPlusPlus11 &&
9445 !BitfieldEnumDecl->getIntegerTypeSourceInfo() &&
9446 BitfieldEnumDecl->getNumPositiveBits() > 0 &&
9447 BitfieldEnumDecl->getNumNegativeBits() == 0) {
9448 S.Diag(InitLoc, diag::warn_no_underlying_type_specified_for_enum_bitfield)
9449 << BitfieldEnumDecl->getNameAsString();
9450 }
9451 }
9452
John McCalldeebbcf2010-11-11 05:33:51 +00009453 if (Bitfield->getType()->isBooleanType())
9454 return false;
9455
Douglas Gregor789adec2011-02-04 13:09:01 +00009456 // Ignore value- or type-dependent expressions.
9457 if (Bitfield->getBitWidth()->isValueDependent() ||
9458 Bitfield->getBitWidth()->isTypeDependent() ||
9459 Init->isValueDependent() ||
9460 Init->isTypeDependent())
9461 return false;
9462
John McCall1f425642010-11-11 03:21:53 +00009463 Expr *OriginalInit = Init->IgnoreParenImpCasts();
Reid Kleckner329f24d2017-03-14 18:01:02 +00009464 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
John McCall1f425642010-11-11 03:21:53 +00009465
Richard Smith5fab0c92011-12-28 19:48:30 +00009466 llvm::APSInt Value;
Reid Kleckner329f24d2017-03-14 18:01:02 +00009467 if (!OriginalInit->EvaluateAsInt(Value, S.Context,
9468 Expr::SE_AllowSideEffects)) {
9469 // The RHS is not constant. If the RHS has an enum type, make sure the
9470 // bitfield is wide enough to hold all the values of the enum without
9471 // truncation.
9472 if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) {
9473 EnumDecl *ED = EnumTy->getDecl();
9474 bool SignedBitfield = BitfieldType->isSignedIntegerType();
9475
9476 // Enum types are implicitly signed on Windows, so check if there are any
9477 // negative enumerators to see if the enum was intended to be signed or
9478 // not.
9479 bool SignedEnum = ED->getNumNegativeBits() > 0;
9480
9481 // Check for surprising sign changes when assigning enum values to a
9482 // bitfield of different signedness. If the bitfield is signed and we
9483 // have exactly the right number of bits to store this unsigned enum,
9484 // suggest changing the enum to an unsigned type. This typically happens
9485 // on Windows where unfixed enums always use an underlying type of 'int'.
9486 unsigned DiagID = 0;
9487 if (SignedEnum && !SignedBitfield) {
9488 DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum;
9489 } else if (SignedBitfield && !SignedEnum &&
9490 ED->getNumPositiveBits() == FieldWidth) {
9491 DiagID = diag::warn_signed_bitfield_enum_conversion;
9492 }
9493
9494 if (DiagID) {
9495 S.Diag(InitLoc, DiagID) << Bitfield << ED;
9496 TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo();
9497 SourceRange TypeRange =
9498 TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange();
9499 S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign)
9500 << SignedEnum << TypeRange;
9501 }
9502
9503 // Compute the required bitwidth. If the enum has negative values, we need
9504 // one more bit than the normal number of positive bits to represent the
9505 // sign bit.
9506 unsigned BitsNeeded = SignedEnum ? std::max(ED->getNumPositiveBits() + 1,
9507 ED->getNumNegativeBits())
9508 : ED->getNumPositiveBits();
9509
9510 // Check the bitwidth.
9511 if (BitsNeeded > FieldWidth) {
9512 Expr *WidthExpr = Bitfield->getBitWidth();
9513 S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum)
9514 << Bitfield << ED;
9515 S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield)
9516 << BitsNeeded << ED << WidthExpr->getSourceRange();
9517 }
9518 }
9519
John McCall1f425642010-11-11 03:21:53 +00009520 return false;
Reid Kleckner329f24d2017-03-14 18:01:02 +00009521 }
John McCall1f425642010-11-11 03:21:53 +00009522
John McCall1f425642010-11-11 03:21:53 +00009523 unsigned OriginalWidth = Value.getBitWidth();
John McCall1f425642010-11-11 03:21:53 +00009524
Daniel Marjamakiee5b5f52016-09-22 14:13:46 +00009525 if (!Value.isSigned() || Value.isNegative())
Richard Trieu7561ed02016-08-05 02:39:30 +00009526 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit))
Daniel Marjamakiee5b5f52016-09-22 14:13:46 +00009527 if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not)
9528 OriginalWidth = Value.getMinSignedBits();
Richard Trieu7561ed02016-08-05 02:39:30 +00009529
John McCall1f425642010-11-11 03:21:53 +00009530 if (OriginalWidth <= FieldWidth)
9531 return false;
9532
Eli Friedmanc267a322012-01-26 23:11:39 +00009533 // Compute the value which the bitfield will contain.
Jay Foad6d4db0c2010-12-07 08:25:34 +00009534 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
Reid Klecknerad425622016-11-16 23:40:00 +00009535 TruncatedValue.setIsSigned(BitfieldType->isSignedIntegerType());
John McCall1f425642010-11-11 03:21:53 +00009536
Eli Friedmanc267a322012-01-26 23:11:39 +00009537 // Check whether the stored value is equal to the original value.
9538 TruncatedValue = TruncatedValue.extend(OriginalWidth);
Richard Trieuc320c742012-07-23 20:21:35 +00009539 if (llvm::APSInt::isSameValue(Value, TruncatedValue))
John McCall1f425642010-11-11 03:21:53 +00009540 return false;
9541
Eli Friedmanc267a322012-01-26 23:11:39 +00009542 // Special-case bitfields of width 1: booleans are naturally 0/1, and
Eli Friedmane1ffd492012-02-02 00:40:20 +00009543 // therefore don't strictly fit into a signed bitfield of width 1.
9544 if (FieldWidth == 1 && Value == 1)
Eli Friedmanc267a322012-01-26 23:11:39 +00009545 return false;
9546
John McCall1f425642010-11-11 03:21:53 +00009547 std::string PrettyValue = Value.toString(10);
9548 std::string PrettyTrunc = TruncatedValue.toString(10);
9549
9550 S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
9551 << PrettyValue << PrettyTrunc << OriginalInit->getType()
9552 << Init->getSourceRange();
9553
9554 return true;
9555}
9556
John McCalld2a53122010-11-09 23:24:47 +00009557/// Analyze the given simple or compound assignment for warning-worthy
9558/// operations.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009559static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
John McCalld2a53122010-11-09 23:24:47 +00009560 // Just recurse on the LHS.
9561 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
9562
9563 // We want to recurse on the RHS as normal unless we're assigning to
9564 // a bitfield.
John McCalld25db7e2013-05-06 21:39:12 +00009565 if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) {
Timur Iskhodzhanov554bdc62013-03-29 00:22:03 +00009566 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
John McCall1f425642010-11-11 03:21:53 +00009567 E->getOperatorLoc())) {
9568 // Recurse, ignoring any implicit conversions on the RHS.
9569 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
9570 E->getOperatorLoc());
John McCalld2a53122010-11-09 23:24:47 +00009571 }
9572 }
9573
9574 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
9575}
9576
John McCall263a48b2010-01-04 23:31:57 +00009577/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009578static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
9579 SourceLocation CContext, unsigned diag,
9580 bool pruneControlFlow = false) {
Anna Zaks314cd092012-02-01 19:08:57 +00009581 if (pruneControlFlow) {
9582 S.DiagRuntimeBehavior(E->getExprLoc(), E,
9583 S.PDiag(diag)
9584 << SourceType << T << E->getSourceRange()
9585 << SourceRange(CContext));
9586 return;
9587 }
Douglas Gregor364f7db2011-03-12 00:14:31 +00009588 S.Diag(E->getExprLoc(), diag)
9589 << SourceType << T << E->getSourceRange() << SourceRange(CContext);
9590}
9591
Chandler Carruth7f3654f2011-04-05 06:47:57 +00009592/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009593static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
9594 SourceLocation CContext,
9595 unsigned diag, bool pruneControlFlow = false) {
Anna Zaks314cd092012-02-01 19:08:57 +00009596 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
Chandler Carruth7f3654f2011-04-05 06:47:57 +00009597}
9598
Andrew V. Tischenko5704dc02018-03-15 10:03:35 +00009599/// Analyze the given compound assignment for the possible losing of
9600/// floating-point precision.
9601static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) {
9602 assert(isa<CompoundAssignOperator>(E) &&
9603 "Must be compound assignment operation");
9604 // Recurse on the LHS and RHS in here
9605 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
9606 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
9607
9608 // Now check the outermost expression
9609 const auto *ResultBT = E->getLHS()->getType()->getAs<BuiltinType>();
9610 const auto *RBT = cast<CompoundAssignOperator>(E)
9611 ->getComputationResultType()
9612 ->getAs<BuiltinType>();
9613
9614 // If both source and target are floating points.
9615 if (ResultBT && ResultBT->isFloatingPoint() && RBT && RBT->isFloatingPoint())
9616 // Builtin FP kinds are ordered by increasing FP rank.
9617 if (ResultBT->getKind() < RBT->getKind())
9618 // We don't want to warn for system macro.
9619 if (!S.SourceMgr.isInSystemMacro(E->getOperatorLoc()))
9620 // warn about dropping FP rank.
9621 DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(),
9622 E->getOperatorLoc(),
9623 diag::warn_impcast_float_result_precision);
9624}
Richard Trieube234c32016-04-21 21:04:55 +00009625
9626/// Diagnose an implicit cast from a floating point value to an integer value.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009627static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T,
9628 SourceLocation CContext) {
Richard Trieube234c32016-04-21 21:04:55 +00009629 const bool IsBool = T->isSpecificBuiltinType(BuiltinType::Bool);
Richard Smith51ec0cf2017-02-21 01:17:38 +00009630 const bool PruneWarnings = S.inTemplateInstantiation();
Richard Trieube234c32016-04-21 21:04:55 +00009631
9632 Expr *InnerE = E->IgnoreParenImpCasts();
9633 // We also want to warn on, e.g., "int i = -1.234"
9634 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
9635 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
9636 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
9637
9638 const bool IsLiteral =
9639 isa<FloatingLiteral>(E) || isa<FloatingLiteral>(InnerE);
9640
9641 llvm::APFloat Value(0.0);
9642 bool IsConstant =
9643 E->EvaluateAsFloat(Value, S.Context, Expr::SE_AllowSideEffects);
9644 if (!IsConstant) {
Richard Trieu891f0f12016-04-22 22:14:32 +00009645 return DiagnoseImpCast(S, E, T, CContext,
9646 diag::warn_impcast_float_integer, PruneWarnings);
Richard Trieube234c32016-04-21 21:04:55 +00009647 }
9648
Chandler Carruth016ef402011-04-10 08:36:24 +00009649 bool isExact = false;
Richard Trieube234c32016-04-21 21:04:55 +00009650
Jeffrey Yasskind0f079d2011-07-15 17:03:07 +00009651 llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
9652 T->hasUnsignedIntegerRepresentation());
Erich Keanea4c48c62018-05-08 21:26:21 +00009653 llvm::APFloat::opStatus Result = Value.convertToInteger(
9654 IntegerValue, llvm::APFloat::rmTowardZero, &isExact);
9655
9656 if (Result == llvm::APFloat::opOK && isExact) {
Richard Trieube234c32016-04-21 21:04:55 +00009657 if (IsLiteral) return;
9658 return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer,
9659 PruneWarnings);
9660 }
9661
Erich Keanea4c48c62018-05-08 21:26:21 +00009662 // Conversion of a floating-point value to a non-bool integer where the
9663 // integral part cannot be represented by the integer type is undefined.
9664 if (!IsBool && Result == llvm::APFloat::opInvalidOp)
9665 return DiagnoseImpCast(
9666 S, E, T, CContext,
9667 IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range
Richard Trieua2b8fe62018-05-14 23:21:48 +00009668 : diag::warn_impcast_float_to_integer_out_of_range,
9669 PruneWarnings);
Erich Keanea4c48c62018-05-08 21:26:21 +00009670
Richard Trieube234c32016-04-21 21:04:55 +00009671 unsigned DiagID = 0;
Richard Trieu891f0f12016-04-22 22:14:32 +00009672 if (IsLiteral) {
Richard Trieube234c32016-04-21 21:04:55 +00009673 // Warn on floating point literal to integer.
9674 DiagID = diag::warn_impcast_literal_float_to_integer;
9675 } else if (IntegerValue == 0) {
9676 if (Value.isZero()) { // Skip -0.0 to 0 conversion.
9677 return DiagnoseImpCast(S, E, T, CContext,
9678 diag::warn_impcast_float_integer, PruneWarnings);
9679 }
9680 // Warn on non-zero to zero conversion.
9681 DiagID = diag::warn_impcast_float_to_integer_zero;
9682 } else {
9683 if (IntegerValue.isUnsigned()) {
9684 if (!IntegerValue.isMaxValue()) {
9685 return DiagnoseImpCast(S, E, T, CContext,
9686 diag::warn_impcast_float_integer, PruneWarnings);
9687 }
9688 } else { // IntegerValue.isSigned()
9689 if (!IntegerValue.isMaxSignedValue() &&
9690 !IntegerValue.isMinSignedValue()) {
9691 return DiagnoseImpCast(S, E, T, CContext,
9692 diag::warn_impcast_float_integer, PruneWarnings);
9693 }
9694 }
9695 // Warn on evaluatable floating point expression to integer conversion.
9696 DiagID = diag::warn_impcast_float_to_integer;
9697 }
Chandler Carruth016ef402011-04-10 08:36:24 +00009698
Eli Friedman07185912013-08-29 23:44:43 +00009699 // FIXME: Force the precision of the source value down so we don't print
9700 // digits which are usually useless (we don't really care here if we
9701 // truncate a digit by accident in edge cases). Ideally, APFloat::toString
9702 // would automatically print the shortest representation, but it's a bit
9703 // tricky to implement.
David Blaikie7555b6a2012-05-15 16:56:36 +00009704 SmallString<16> PrettySourceValue;
Eli Friedman07185912013-08-29 23:44:43 +00009705 unsigned precision = llvm::APFloat::semanticsPrecision(Value.getSemantics());
9706 precision = (precision * 59 + 195) / 196;
9707 Value.toString(PrettySourceValue, precision);
9708
David Blaikie9b88cc02012-05-15 17:18:27 +00009709 SmallString<16> PrettyTargetValue;
Richard Trieube234c32016-04-21 21:04:55 +00009710 if (IsBool)
Aaron Ballmandbc441e2015-12-30 14:26:07 +00009711 PrettyTargetValue = Value.isZero() ? "false" : "true";
David Blaikie7555b6a2012-05-15 16:56:36 +00009712 else
David Blaikie9b88cc02012-05-15 17:18:27 +00009713 IntegerValue.toString(PrettyTargetValue);
David Blaikie7555b6a2012-05-15 16:56:36 +00009714
Richard Trieube234c32016-04-21 21:04:55 +00009715 if (PruneWarnings) {
9716 S.DiagRuntimeBehavior(E->getExprLoc(), E,
9717 S.PDiag(DiagID)
9718 << E->getType() << T.getUnqualifiedType()
9719 << PrettySourceValue << PrettyTargetValue
9720 << E->getSourceRange() << SourceRange(CContext));
9721 } else {
9722 S.Diag(E->getExprLoc(), DiagID)
9723 << E->getType() << T.getUnqualifiedType() << PrettySourceValue
9724 << PrettyTargetValue << E->getSourceRange() << SourceRange(CContext);
9725 }
Chandler Carruth016ef402011-04-10 08:36:24 +00009726}
9727
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009728static std::string PrettyPrintInRange(const llvm::APSInt &Value,
9729 IntRange Range) {
John McCall18a2c2c2010-11-09 22:22:12 +00009730 if (!Range.Width) return "0";
9731
9732 llvm::APSInt ValueInRange = Value;
9733 ValueInRange.setIsSigned(!Range.NonNegative);
Jay Foad6d4db0c2010-12-07 08:25:34 +00009734 ValueInRange = ValueInRange.trunc(Range.Width);
John McCall18a2c2c2010-11-09 22:22:12 +00009735 return ValueInRange.toString(10);
9736}
9737
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009738static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) {
Hans Wennborgf4ad2322012-08-28 15:44:30 +00009739 if (!isa<ImplicitCastExpr>(Ex))
9740 return false;
9741
9742 Expr *InnerE = Ex->IgnoreParenImpCasts();
9743 const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr();
9744 const Type *Source =
9745 S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
9746 if (Target->isDependentType())
9747 return false;
9748
9749 const BuiltinType *FloatCandidateBT =
9750 dyn_cast<BuiltinType>(ToBool ? Source : Target);
9751 const Type *BoolCandidateType = ToBool ? Target : Source;
9752
9753 return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) &&
9754 FloatCandidateBT && (FloatCandidateBT->isFloatingPoint()));
9755}
9756
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009757static void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall,
9758 SourceLocation CC) {
Hans Wennborgf4ad2322012-08-28 15:44:30 +00009759 unsigned NumArgs = TheCall->getNumArgs();
9760 for (unsigned i = 0; i < NumArgs; ++i) {
9761 Expr *CurrA = TheCall->getArg(i);
9762 if (!IsImplicitBoolFloatConversion(S, CurrA, true))
9763 continue;
9764
9765 bool IsSwapped = ((i > 0) &&
9766 IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false));
9767 IsSwapped |= ((i < (NumArgs - 1)) &&
9768 IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false));
9769 if (IsSwapped) {
9770 // Warn on this floating-point to bool conversion.
9771 DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(),
9772 CurrA->getType(), CC,
9773 diag::warn_impcast_floating_point_to_bool);
9774 }
9775 }
9776}
9777
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009778static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
9779 SourceLocation CC) {
Richard Trieu5b993502014-10-15 03:42:06 +00009780 if (S.Diags.isIgnored(diag::warn_impcast_null_pointer_to_integer,
9781 E->getExprLoc()))
9782 return;
9783
Richard Trieu09d6b802016-01-08 23:35:06 +00009784 // Don't warn on functions which have return type nullptr_t.
9785 if (isa<CallExpr>(E))
9786 return;
9787
Richard Trieu5b993502014-10-15 03:42:06 +00009788 // Check for NULL (GNUNull) or nullptr (CXX11_nullptr).
9789 const Expr::NullPointerConstantKind NullKind =
9790 E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull);
9791 if (NullKind != Expr::NPCK_GNUNull && NullKind != Expr::NPCK_CXX11_nullptr)
9792 return;
9793
9794 // Return if target type is a safe conversion.
9795 if (T->isAnyPointerType() || T->isBlockPointerType() ||
9796 T->isMemberPointerType() || !T->isScalarType() || T->isNullPtrType())
9797 return;
9798
9799 SourceLocation Loc = E->getSourceRange().getBegin();
9800
Richard Trieu0a5e1662016-02-13 00:58:53 +00009801 // Venture through the macro stacks to get to the source of macro arguments.
9802 // The new location is a better location than the complete location that was
9803 // passed in.
George Karpenkov441e8fd2018-02-09 23:30:07 +00009804 Loc = S.SourceMgr.getTopMacroCallerLoc(Loc);
9805 CC = S.SourceMgr.getTopMacroCallerLoc(CC);
Richard Trieu0a5e1662016-02-13 00:58:53 +00009806
Richard Trieu5b993502014-10-15 03:42:06 +00009807 // __null is usually wrapped in a macro. Go up a macro if that is the case.
Richard Trieu0a5e1662016-02-13 00:58:53 +00009808 if (NullKind == Expr::NPCK_GNUNull && Loc.isMacroID()) {
9809 StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics(
9810 Loc, S.SourceMgr, S.getLangOpts());
9811 if (MacroName == "NULL")
Richard Smithb5f81712018-04-30 05:25:48 +00009812 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).getBegin();
Richard Trieu5b993502014-10-15 03:42:06 +00009813 }
9814
9815 // Only warn if the null and context location are in the same macro expansion.
9816 if (S.SourceMgr.getFileID(Loc) != S.SourceMgr.getFileID(CC))
9817 return;
9818
9819 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009820 << (NullKind == Expr::NPCK_CXX11_nullptr) << T << SourceRange(CC)
Richard Trieu5b993502014-10-15 03:42:06 +00009821 << FixItHint::CreateReplacement(Loc,
9822 S.getFixItZeroLiteralForType(T, Loc));
9823}
9824
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009825static void checkObjCArrayLiteral(Sema &S, QualType TargetType,
9826 ObjCArrayLiteral *ArrayLiteral);
9827
9828static void
9829checkObjCDictionaryLiteral(Sema &S, QualType TargetType,
9830 ObjCDictionaryLiteral *DictionaryLiteral);
Douglas Gregor5054cb02015-07-07 03:58:22 +00009831
9832/// Check a single element within a collection literal against the
9833/// target element type.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009834static void checkObjCCollectionLiteralElement(Sema &S,
9835 QualType TargetElementType,
9836 Expr *Element,
9837 unsigned ElementKind) {
Douglas Gregor5054cb02015-07-07 03:58:22 +00009838 // Skip a bitcast to 'id' or qualified 'id'.
9839 if (auto ICE = dyn_cast<ImplicitCastExpr>(Element)) {
9840 if (ICE->getCastKind() == CK_BitCast &&
9841 ICE->getSubExpr()->getType()->getAs<ObjCObjectPointerType>())
9842 Element = ICE->getSubExpr();
9843 }
9844
9845 QualType ElementType = Element->getType();
9846 ExprResult ElementResult(Element);
9847 if (ElementType->getAs<ObjCObjectPointerType>() &&
9848 S.CheckSingleAssignmentConstraints(TargetElementType,
9849 ElementResult,
9850 false, false)
9851 != Sema::Compatible) {
9852 S.Diag(Element->getLocStart(),
9853 diag::warn_objc_collection_literal_element)
9854 << ElementType << ElementKind << TargetElementType
9855 << Element->getSourceRange();
9856 }
9857
9858 if (auto ArrayLiteral = dyn_cast<ObjCArrayLiteral>(Element))
9859 checkObjCArrayLiteral(S, TargetElementType, ArrayLiteral);
9860 else if (auto DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(Element))
9861 checkObjCDictionaryLiteral(S, TargetElementType, DictionaryLiteral);
9862}
9863
9864/// Check an Objective-C array literal being converted to the given
9865/// target type.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009866static void checkObjCArrayLiteral(Sema &S, QualType TargetType,
9867 ObjCArrayLiteral *ArrayLiteral) {
Douglas Gregor5054cb02015-07-07 03:58:22 +00009868 if (!S.NSArrayDecl)
9869 return;
9870
9871 const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>();
9872 if (!TargetObjCPtr)
9873 return;
9874
9875 if (TargetObjCPtr->isUnspecialized() ||
9876 TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl()
9877 != S.NSArrayDecl->getCanonicalDecl())
9878 return;
9879
9880 auto TypeArgs = TargetObjCPtr->getTypeArgs();
9881 if (TypeArgs.size() != 1)
9882 return;
9883
9884 QualType TargetElementType = TypeArgs[0];
9885 for (unsigned I = 0, N = ArrayLiteral->getNumElements(); I != N; ++I) {
9886 checkObjCCollectionLiteralElement(S, TargetElementType,
9887 ArrayLiteral->getElement(I),
9888 0);
9889 }
9890}
9891
9892/// Check an Objective-C dictionary literal being converted to the given
9893/// target type.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009894static void
9895checkObjCDictionaryLiteral(Sema &S, QualType TargetType,
9896 ObjCDictionaryLiteral *DictionaryLiteral) {
Douglas Gregor5054cb02015-07-07 03:58:22 +00009897 if (!S.NSDictionaryDecl)
9898 return;
9899
9900 const auto *TargetObjCPtr = TargetType->getAs<ObjCObjectPointerType>();
9901 if (!TargetObjCPtr)
9902 return;
9903
9904 if (TargetObjCPtr->isUnspecialized() ||
9905 TargetObjCPtr->getInterfaceDecl()->getCanonicalDecl()
9906 != S.NSDictionaryDecl->getCanonicalDecl())
9907 return;
9908
9909 auto TypeArgs = TargetObjCPtr->getTypeArgs();
9910 if (TypeArgs.size() != 2)
9911 return;
9912
9913 QualType TargetKeyType = TypeArgs[0];
9914 QualType TargetObjectType = TypeArgs[1];
9915 for (unsigned I = 0, N = DictionaryLiteral->getNumElements(); I != N; ++I) {
9916 auto Element = DictionaryLiteral->getKeyValueElement(I);
9917 checkObjCCollectionLiteralElement(S, TargetKeyType, Element.Key, 1);
9918 checkObjCCollectionLiteralElement(S, TargetObjectType, Element.Value, 2);
9919 }
9920}
9921
Richard Trieufc404c72016-02-05 23:02:38 +00009922// Helper function to filter out cases for constant width constant conversion.
9923// Don't warn on char array initialization or for non-decimal values.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009924static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T,
9925 SourceLocation CC) {
Richard Trieufc404c72016-02-05 23:02:38 +00009926 // If initializing from a constant, and the constant starts with '0',
9927 // then it is a binary, octal, or hexadecimal. Allow these constants
9928 // to fill all the bits, even if there is a sign change.
9929 if (auto *IntLit = dyn_cast<IntegerLiteral>(E->IgnoreParenImpCasts())) {
9930 const char FirstLiteralCharacter =
9931 S.getSourceManager().getCharacterData(IntLit->getLocStart())[0];
9932 if (FirstLiteralCharacter == '0')
9933 return false;
9934 }
9935
9936 // If the CC location points to a '{', and the type is char, then assume
9937 // assume it is an array initialization.
9938 if (CC.isValid() && T->isCharType()) {
9939 const char FirstContextCharacter =
9940 S.getSourceManager().getCharacterData(CC)[0];
9941 if (FirstContextCharacter == '{')
9942 return false;
9943 }
9944
9945 return true;
9946}
9947
Eugene Zelenko11a7ef82017-11-15 22:00:04 +00009948static void
9949CheckImplicitConversion(Sema &S, Expr *E, QualType T, SourceLocation CC,
9950 bool *ICContext = nullptr) {
John McCallcc7e5bf2010-05-06 08:58:33 +00009951 if (E->isTypeDependent() || E->isValueDependent()) return;
John McCall263a48b2010-01-04 23:31:57 +00009952
John McCallcc7e5bf2010-05-06 08:58:33 +00009953 const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
9954 const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
9955 if (Source == Target) return;
9956 if (Target->isDependentType()) return;
John McCall263a48b2010-01-04 23:31:57 +00009957
Chandler Carruthc22845a2011-07-26 05:40:03 +00009958 // If the conversion context location is invalid don't complain. We also
9959 // don't want to emit a warning if the issue occurs from the expansion of
9960 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
9961 // delay this check as long as possible. Once we detect we are in that
9962 // scenario, we just return.
Ted Kremenek4c0826c2011-03-10 20:03:42 +00009963 if (CC.isInvalid())
John McCallacf0ee52010-10-08 02:01:28 +00009964 return;
9965
Richard Trieu021baa32011-09-23 20:10:00 +00009966 // Diagnose implicit casts to bool.
9967 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
9968 if (isa<StringLiteral>(E))
9969 // Warn on string literal to bool. Checks for string literals in logical
Richard Trieu955231d2014-01-25 01:10:35 +00009970 // and expressions, for instance, assert(0 && "error here"), are
9971 // prevented by a check in AnalyzeImplicitConversions().
Richard Trieu021baa32011-09-23 20:10:00 +00009972 return DiagnoseImpCast(S, E, T, CC,
9973 diag::warn_impcast_string_literal_to_bool);
Richard Trieu1e632af2014-01-28 23:40:26 +00009974 if (isa<ObjCStringLiteral>(E) || isa<ObjCArrayLiteral>(E) ||
9975 isa<ObjCDictionaryLiteral>(E) || isa<ObjCBoxedExpr>(E)) {
9976 // This covers the literal expressions that evaluate to Objective-C
9977 // objects.
9978 return DiagnoseImpCast(S, E, T, CC,
9979 diag::warn_impcast_objective_c_literal_to_bool);
9980 }
Richard Trieu3bb8b562014-02-26 02:36:06 +00009981 if (Source->isPointerType() || Source->canDecayToPointerType()) {
9982 // Warn on pointer to bool conversion that is always true.
9983 S.DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false,
9984 SourceRange(CC));
Lang Hamesdf5c1212011-12-05 20:49:50 +00009985 }
Richard Trieu021baa32011-09-23 20:10:00 +00009986 }
John McCall263a48b2010-01-04 23:31:57 +00009987
Douglas Gregor5054cb02015-07-07 03:58:22 +00009988 // Check implicit casts from Objective-C collection literals to specialized
9989 // collection types, e.g., NSArray<NSString *> *.
9990 if (auto *ArrayLiteral = dyn_cast<ObjCArrayLiteral>(E))
9991 checkObjCArrayLiteral(S, QualType(Target, 0), ArrayLiteral);
9992 else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E))
9993 checkObjCDictionaryLiteral(S, QualType(Target, 0), DictionaryLiteral);
9994
John McCall263a48b2010-01-04 23:31:57 +00009995 // Strip vector types.
9996 if (isa<VectorType>(Source)) {
Ted Kremenek4c0826c2011-03-10 20:03:42 +00009997 if (!isa<VectorType>(Target)) {
Matt Beaumont-Gay7a57ada2012-01-06 22:43:58 +00009998 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenek4c0826c2011-03-10 20:03:42 +00009999 return;
John McCallacf0ee52010-10-08 02:01:28 +000010000 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010001 }
Andrew V. Tischenko5704dc02018-03-15 10:03:35 +000010002
Chris Lattneree7286f2011-06-14 04:51:15 +000010003 // If the vector cast is cast between two vectors of the same size, it is
10004 // a bitcast, not a conversion.
10005 if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
10006 return;
John McCall263a48b2010-01-04 23:31:57 +000010007
10008 Source = cast<VectorType>(Source)->getElementType().getTypePtr();
10009 Target = cast<VectorType>(Target)->getElementType().getTypePtr();
10010 }
Stephen Canon3ba640d2014-04-03 10:33:25 +000010011 if (auto VecTy = dyn_cast<VectorType>(Target))
10012 Target = VecTy->getElementType().getTypePtr();
John McCall263a48b2010-01-04 23:31:57 +000010013
10014 // Strip complex types.
10015 if (isa<ComplexType>(Source)) {
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010016 if (!isa<ComplexType>(Target)) {
Tim Northover02416372017-08-08 23:18:05 +000010017 if (S.SourceMgr.isInSystemMacro(CC) || Target->isBooleanType())
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010018 return;
10019
Tim Northover02416372017-08-08 23:18:05 +000010020 return DiagnoseImpCast(S, E, T, CC,
10021 S.getLangOpts().CPlusPlus
10022 ? diag::err_impcast_complex_scalar
10023 : diag::warn_impcast_complex_scalar);
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010024 }
John McCall263a48b2010-01-04 23:31:57 +000010025
10026 Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
10027 Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
10028 }
10029
10030 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
10031 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
10032
10033 // If the source is floating point...
10034 if (SourceBT && SourceBT->isFloatingPoint()) {
10035 // ...and the target is floating point...
10036 if (TargetBT && TargetBT->isFloatingPoint()) {
10037 // ...then warn if we're dropping FP rank.
10038
10039 // Builtin FP kinds are ordered by increasing FP rank.
10040 if (SourceBT->getKind() > TargetBT->getKind()) {
10041 // Don't warn about float constants that are precisely
10042 // representable in the target type.
10043 Expr::EvalResult result;
Richard Smith7b553f12011-10-29 00:50:52 +000010044 if (E->EvaluateAsRValue(result, S.Context)) {
John McCall263a48b2010-01-04 23:31:57 +000010045 // Value might be a float, a float vector, or a float complex.
10046 if (IsSameFloatAfterCast(result.Val,
John McCallcc7e5bf2010-05-06 08:58:33 +000010047 S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
10048 S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
John McCall263a48b2010-01-04 23:31:57 +000010049 return;
10050 }
10051
Matt Beaumont-Gay7a57ada2012-01-06 22:43:58 +000010052 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010053 return;
10054
John McCallacf0ee52010-10-08 02:01:28 +000010055 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
George Burgess IV148e0d32015-10-29 00:28:52 +000010056 }
10057 // ... or possibly if we're increasing rank, too
10058 else if (TargetBT->getKind() > SourceBT->getKind()) {
10059 if (S.SourceMgr.isInSystemMacro(CC))
10060 return;
10061
10062 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_double_promotion);
John McCall263a48b2010-01-04 23:31:57 +000010063 }
10064 return;
10065 }
10066
Richard Trieube234c32016-04-21 21:04:55 +000010067 // If the target is integral, always warn.
David Blaikie7555b6a2012-05-15 16:56:36 +000010068 if (TargetBT && TargetBT->isInteger()) {
Matt Beaumont-Gay7a57ada2012-01-06 22:43:58 +000010069 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010070 return;
Matt Beaumont-Gay042ce8e2011-09-08 22:30:47 +000010071
Richard Trieube234c32016-04-21 21:04:55 +000010072 DiagnoseFloatingImpCast(S, E, T, CC);
Chandler Carruth22c7a792011-02-17 11:05:49 +000010073 }
John McCall263a48b2010-01-04 23:31:57 +000010074
Richard Smith54894fd2015-12-30 01:06:52 +000010075 // Detect the case where a call result is converted from floating-point to
10076 // to bool, and the final argument to the call is converted from bool, to
10077 // discover this typo:
10078 //
10079 // bool b = fabs(x < 1.0); // should be "bool b = fabs(x) < 1.0;"
10080 //
10081 // FIXME: This is an incredibly special case; is there some more general
10082 // way to detect this class of misplaced-parentheses bug?
10083 if (Target->isBooleanType() && isa<CallExpr>(E)) {
Hans Wennborgf4ad2322012-08-28 15:44:30 +000010084 // Check last argument of function call to see if it is an
10085 // implicit cast from a type matching the type the result
10086 // is being cast to.
10087 CallExpr *CEx = cast<CallExpr>(E);
Richard Smith54894fd2015-12-30 01:06:52 +000010088 if (unsigned NumArgs = CEx->getNumArgs()) {
Hans Wennborgf4ad2322012-08-28 15:44:30 +000010089 Expr *LastA = CEx->getArg(NumArgs - 1);
10090 Expr *InnerE = LastA->IgnoreParenImpCasts();
Richard Smith54894fd2015-12-30 01:06:52 +000010091 if (isa<ImplicitCastExpr>(LastA) &&
10092 InnerE->getType()->isBooleanType()) {
Hans Wennborgf4ad2322012-08-28 15:44:30 +000010093 // Warn on this floating-point to bool conversion
10094 DiagnoseImpCast(S, E, T, CC,
10095 diag::warn_impcast_floating_point_to_bool);
10096 }
10097 }
10098 }
John McCall263a48b2010-01-04 23:31:57 +000010099 return;
10100 }
10101
Richard Trieu5b993502014-10-15 03:42:06 +000010102 DiagnoseNullConversion(S, E, T, CC);
Richard Trieubeaf3452011-05-29 19:59:02 +000010103
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000010104 S.DiscardMisalignedMemberAddress(Target, E);
10105
David Blaikie9366d2b2012-06-19 21:19:06 +000010106 if (!Source->isIntegerType() || !Target->isIntegerType())
10107 return;
10108
David Blaikie7555b6a2012-05-15 16:56:36 +000010109 // TODO: remove this early return once the false positives for constant->bool
10110 // in templates, macros, etc, are reduced or removed.
10111 if (Target->isSpecificBuiltinType(BuiltinType::Bool))
10112 return;
10113
John McCallcc7e5bf2010-05-06 08:58:33 +000010114 IntRange SourceRange = GetExprRange(S.Context, E);
John McCall817d4af2010-11-10 23:38:19 +000010115 IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
John McCall70aa5392010-01-06 05:24:50 +000010116
Timur Iskhodzhanov554bdc62013-03-29 00:22:03 +000010117 if (SourceRange.Width > TargetRange.Width) {
Sam Panzer6fffec62013-03-28 19:07:11 +000010118 // If the source is a constant, use a default-on diagnostic.
Timur Iskhodzhanov554bdc62013-03-29 00:22:03 +000010119 // TODO: this should happen for bitfield stores, too.
10120 llvm::APSInt Value(32);
Richard Trieudcb55572016-01-29 23:51:16 +000010121 if (E->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) {
Timur Iskhodzhanov554bdc62013-03-29 00:22:03 +000010122 if (S.SourceMgr.isInSystemMacro(CC))
10123 return;
10124
John McCall18a2c2c2010-11-09 22:22:12 +000010125 std::string PrettySourceValue = Value.toString(10);
10126 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
Timur Iskhodzhanov554bdc62013-03-29 00:22:03 +000010127
Ted Kremenek33ba9952011-10-22 02:37:33 +000010128 S.DiagRuntimeBehavior(E->getExprLoc(), E,
10129 S.PDiag(diag::warn_impcast_integer_precision_constant)
10130 << PrettySourceValue << PrettyTargetValue
10131 << E->getType() << T << E->getSourceRange()
10132 << clang::SourceRange(CC));
John McCall18a2c2c2010-11-09 22:22:12 +000010133 return;
10134 }
10135
Timur Iskhodzhanov554bdc62013-03-29 00:22:03 +000010136 // People want to build with -Wshorten-64-to-32 and not -Wconversion.
10137 if (S.SourceMgr.isInSystemMacro(CC))
10138 return;
10139
David Blaikie9455da02012-04-12 22:40:54 +000010140 if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64)
Anna Zaks314cd092012-02-01 19:08:57 +000010141 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32,
10142 /* pruneControlFlow */ true);
John McCallacf0ee52010-10-08 02:01:28 +000010143 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
John McCallcc7e5bf2010-05-06 08:58:33 +000010144 }
10145
Richard Trieudcb55572016-01-29 23:51:16 +000010146 if (TargetRange.Width == SourceRange.Width && !TargetRange.NonNegative &&
10147 SourceRange.NonNegative && Source->isSignedIntegerType()) {
10148 // Warn when doing a signed to signed conversion, warn if the positive
10149 // source value is exactly the width of the target type, which will
10150 // cause a negative value to be stored.
10151
10152 llvm::APSInt Value;
Richard Trieufc404c72016-02-05 23:02:38 +000010153 if (E->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects) &&
10154 !S.SourceMgr.isInSystemMacro(CC)) {
10155 if (isSameWidthConstantConversion(S, E, T, CC)) {
10156 std::string PrettySourceValue = Value.toString(10);
10157 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
Richard Trieudcb55572016-01-29 23:51:16 +000010158
Richard Trieufc404c72016-02-05 23:02:38 +000010159 S.DiagRuntimeBehavior(
10160 E->getExprLoc(), E,
10161 S.PDiag(diag::warn_impcast_integer_precision_constant)
10162 << PrettySourceValue << PrettyTargetValue << E->getType() << T
10163 << E->getSourceRange() << clang::SourceRange(CC));
10164 return;
Richard Trieudcb55572016-01-29 23:51:16 +000010165 }
10166 }
Richard Trieufc404c72016-02-05 23:02:38 +000010167
Richard Trieudcb55572016-01-29 23:51:16 +000010168 // Fall through for non-constants to give a sign conversion warning.
10169 }
10170
John McCallcc7e5bf2010-05-06 08:58:33 +000010171 if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
10172 (!TargetRange.NonNegative && SourceRange.NonNegative &&
10173 SourceRange.Width == TargetRange.Width)) {
Matt Beaumont-Gay7a57ada2012-01-06 22:43:58 +000010174 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010175 return;
10176
John McCallcc7e5bf2010-05-06 08:58:33 +000010177 unsigned DiagID = diag::warn_impcast_integer_sign;
10178
10179 // Traditionally, gcc has warned about this under -Wsign-compare.
10180 // We also want to warn about it in -Wconversion.
10181 // So if -Wconversion is off, use a completely identical diagnostic
10182 // in the sign-compare group.
10183 // The conditional-checking code will
10184 if (ICContext) {
10185 DiagID = diag::warn_impcast_integer_sign_conditional;
10186 *ICContext = true;
10187 }
10188
John McCallacf0ee52010-10-08 02:01:28 +000010189 return DiagnoseImpCast(S, E, T, CC, DiagID);
John McCall263a48b2010-01-04 23:31:57 +000010190 }
10191
Douglas Gregora78f1932011-02-22 02:45:07 +000010192 // Diagnose conversions between different enumeration types.
Douglas Gregor364f7db2011-03-12 00:14:31 +000010193 // In C, we pretend that the type of an EnumConstantDecl is its enumeration
10194 // type, to give us better diagnostics.
10195 QualType SourceType = E->getType();
David Blaikiebbafb8a2012-03-11 07:00:24 +000010196 if (!S.getLangOpts().CPlusPlus) {
Douglas Gregor364f7db2011-03-12 00:14:31 +000010197 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
10198 if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
10199 EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
10200 SourceType = S.Context.getTypeDeclType(Enum);
10201 Source = S.Context.getCanonicalType(SourceType).getTypePtr();
10202 }
10203 }
10204
Douglas Gregora78f1932011-02-22 02:45:07 +000010205 if (const EnumType *SourceEnum = Source->getAs<EnumType>())
10206 if (const EnumType *TargetEnum = Target->getAs<EnumType>())
John McCall5ea95772013-03-09 00:54:27 +000010207 if (SourceEnum->getDecl()->hasNameForLinkage() &&
10208 TargetEnum->getDecl()->hasNameForLinkage() &&
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010209 SourceEnum != TargetEnum) {
Matt Beaumont-Gay7a57ada2012-01-06 22:43:58 +000010210 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010211 return;
10212
Douglas Gregor364f7db2011-03-12 00:14:31 +000010213 return DiagnoseImpCast(S, E, SourceType, T, CC,
Douglas Gregora78f1932011-02-22 02:45:07 +000010214 diag::warn_impcast_different_enum_types);
Ted Kremenek4c0826c2011-03-10 20:03:42 +000010215 }
John McCall263a48b2010-01-04 23:31:57 +000010216}
10217
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010218static void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
10219 SourceLocation CC, QualType T);
John McCallcc7e5bf2010-05-06 08:58:33 +000010220
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010221static void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
10222 SourceLocation CC, bool &ICContext) {
John McCallcc7e5bf2010-05-06 08:58:33 +000010223 E = E->IgnoreParenImpCasts();
10224
10225 if (isa<ConditionalOperator>(E))
David Blaikie18e9ac72012-05-15 21:57:38 +000010226 return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T);
John McCallcc7e5bf2010-05-06 08:58:33 +000010227
John McCallacf0ee52010-10-08 02:01:28 +000010228 AnalyzeImplicitConversions(S, E, CC);
John McCallcc7e5bf2010-05-06 08:58:33 +000010229 if (E->getType() != T)
John McCallacf0ee52010-10-08 02:01:28 +000010230 return CheckImplicitConversion(S, E, T, CC, &ICContext);
John McCallcc7e5bf2010-05-06 08:58:33 +000010231}
10232
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010233static void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
10234 SourceLocation CC, QualType T) {
Richard Trieubd3305b2014-08-07 02:09:05 +000010235 AnalyzeImplicitConversions(S, E->getCond(), E->getQuestionLoc());
John McCallcc7e5bf2010-05-06 08:58:33 +000010236
10237 bool Suspicious = false;
John McCallacf0ee52010-10-08 02:01:28 +000010238 CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious);
10239 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
John McCallcc7e5bf2010-05-06 08:58:33 +000010240
10241 // If -Wconversion would have warned about either of the candidates
10242 // for a signedness conversion to the context type...
10243 if (!Suspicious) return;
10244
10245 // ...but it's currently ignored...
Alp Tokerd4a3f0e2014-06-15 23:30:39 +000010246 if (!S.Diags.isIgnored(diag::warn_impcast_integer_sign_conditional, CC))
John McCallcc7e5bf2010-05-06 08:58:33 +000010247 return;
10248
John McCallcc7e5bf2010-05-06 08:58:33 +000010249 // ...then check whether it would have warned about either of the
10250 // candidates for a signedness conversion to the condition type.
Richard Trieubb43dec2011-07-21 02:46:28 +000010251 if (E->getType() == T) return;
10252
10253 Suspicious = false;
10254 CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
10255 E->getType(), CC, &Suspicious);
10256 if (!Suspicious)
10257 CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
John McCallacf0ee52010-10-08 02:01:28 +000010258 E->getType(), CC, &Suspicious);
John McCallcc7e5bf2010-05-06 08:58:33 +000010259}
10260
Richard Trieu65724892014-11-15 06:37:39 +000010261/// CheckBoolLikeConversion - Check conversion of given expression to boolean.
10262/// Input argument E is a logical expression.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010263static void CheckBoolLikeConversion(Sema &S, Expr *E, SourceLocation CC) {
Richard Trieu65724892014-11-15 06:37:39 +000010264 if (S.getLangOpts().Bool)
10265 return;
10266 CheckImplicitConversion(S, E->IgnoreParenImpCasts(), S.Context.BoolTy, CC);
10267}
10268
John McCallcc7e5bf2010-05-06 08:58:33 +000010269/// AnalyzeImplicitConversions - Find and report any interesting
10270/// implicit conversions in the given expression. There are a couple
10271/// of competing diagnostics here, -Wconversion and -Wsign-compare.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010272static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE,
10273 SourceLocation CC) {
Fariborz Jahanian148c8c82014-04-07 16:32:54 +000010274 QualType T = OrigE->getType();
John McCallcc7e5bf2010-05-06 08:58:33 +000010275 Expr *E = OrigE->IgnoreParenImpCasts();
10276
Douglas Gregor6e8da6a2011-10-10 17:38:18 +000010277 if (E->isTypeDependent() || E->isValueDependent())
10278 return;
Andrew V. Tischenko5704dc02018-03-15 10:03:35 +000010279
John McCallcc7e5bf2010-05-06 08:58:33 +000010280 // For conditional operators, we analyze the arguments as if they
10281 // were being fed directly into the output.
10282 if (isa<ConditionalOperator>(E)) {
10283 ConditionalOperator *CO = cast<ConditionalOperator>(E);
David Blaikie18e9ac72012-05-15 21:57:38 +000010284 CheckConditionalOperator(S, CO, CC, T);
John McCallcc7e5bf2010-05-06 08:58:33 +000010285 return;
10286 }
10287
Hans Wennborgf4ad2322012-08-28 15:44:30 +000010288 // Check implicit argument conversions for function calls.
10289 if (CallExpr *Call = dyn_cast<CallExpr>(E))
10290 CheckImplicitArgumentConversions(S, Call, CC);
10291
John McCallcc7e5bf2010-05-06 08:58:33 +000010292 // Go ahead and check any implicit conversions we might have skipped.
10293 // The non-canonical typecheck is just an optimization;
10294 // CheckImplicitConversion will filter out dead implicit conversions.
10295 if (E->getType() != T)
John McCallacf0ee52010-10-08 02:01:28 +000010296 CheckImplicitConversion(S, E, T, CC);
John McCallcc7e5bf2010-05-06 08:58:33 +000010297
10298 // Now continue drilling into this expression.
Richard Smithd7bed4d2015-11-22 02:57:17 +000010299
10300 if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
10301 // The bound subexpressions in a PseudoObjectExpr are not reachable
10302 // as transitive children.
10303 // FIXME: Use a more uniform representation for this.
10304 for (auto *SE : POE->semantics())
10305 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SE))
10306 AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC);
Fariborz Jahanian2cb4a952013-05-15 19:03:04 +000010307 }
Richard Smithd7bed4d2015-11-22 02:57:17 +000010308
John McCallcc7e5bf2010-05-06 08:58:33 +000010309 // Skip past explicit casts.
10310 if (isa<ExplicitCastExpr>(E)) {
10311 E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
John McCallacf0ee52010-10-08 02:01:28 +000010312 return AnalyzeImplicitConversions(S, E, CC);
John McCallcc7e5bf2010-05-06 08:58:33 +000010313 }
10314
John McCalld2a53122010-11-09 23:24:47 +000010315 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
10316 // Do a somewhat different check with comparison operators.
10317 if (BO->isComparisonOp())
10318 return AnalyzeComparison(S, BO);
10319
Timur Iskhodzhanov554bdc62013-03-29 00:22:03 +000010320 // And with simple assignments.
10321 if (BO->getOpcode() == BO_Assign)
John McCalld2a53122010-11-09 23:24:47 +000010322 return AnalyzeAssignment(S, BO);
Andrew V. Tischenko5704dc02018-03-15 10:03:35 +000010323 // And with compound assignments.
10324 if (BO->isAssignmentOp())
10325 return AnalyzeCompoundAssignment(S, BO);
John McCalld2a53122010-11-09 23:24:47 +000010326 }
John McCallcc7e5bf2010-05-06 08:58:33 +000010327
10328 // These break the otherwise-useful invariant below. Fortunately,
10329 // we don't really need to recurse into them, because any internal
10330 // expressions should have been analyzed already when they were
10331 // built into statements.
10332 if (isa<StmtExpr>(E)) return;
10333
10334 // Don't descend into unevaluated contexts.
Peter Collingbournee190dee2011-03-11 19:24:49 +000010335 if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
John McCallcc7e5bf2010-05-06 08:58:33 +000010336
10337 // Now just recurse over the expression's children.
John McCallacf0ee52010-10-08 02:01:28 +000010338 CC = E->getExprLoc();
Richard Trieu021baa32011-09-23 20:10:00 +000010339 BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
Richard Trieu955231d2014-01-25 01:10:35 +000010340 bool IsLogicalAndOperator = BO && BO->getOpcode() == BO_LAnd;
Benjamin Kramer642f1732015-07-02 21:03:14 +000010341 for (Stmt *SubStmt : E->children()) {
10342 Expr *ChildExpr = dyn_cast_or_null<Expr>(SubStmt);
Douglas Gregor8c50e7c2012-02-09 00:47:04 +000010343 if (!ChildExpr)
10344 continue;
10345
Richard Trieu955231d2014-01-25 01:10:35 +000010346 if (IsLogicalAndOperator &&
Richard Trieu021baa32011-09-23 20:10:00 +000010347 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
Richard Trieu955231d2014-01-25 01:10:35 +000010348 // Ignore checking string literals that are in logical and operators.
10349 // This is a common pattern for asserts.
Richard Trieu021baa32011-09-23 20:10:00 +000010350 continue;
10351 AnalyzeImplicitConversions(S, ChildExpr, CC);
10352 }
Richard Trieu791b86e2014-11-19 06:08:18 +000010353
Fariborz Jahanianb7859dd2014-11-14 17:12:50 +000010354 if (BO && BO->isLogicalOp()) {
Richard Trieu791b86e2014-11-19 06:08:18 +000010355 Expr *SubExpr = BO->getLHS()->IgnoreParenImpCasts();
10356 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
Fariborz Jahanian0fc95ad2014-12-18 23:14:51 +000010357 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
Richard Trieu791b86e2014-11-19 06:08:18 +000010358
10359 SubExpr = BO->getRHS()->IgnoreParenImpCasts();
10360 if (!IsLogicalAndOperator || !isa<StringLiteral>(SubExpr))
Fariborz Jahanian0fc95ad2014-12-18 23:14:51 +000010361 ::CheckBoolLikeConversion(S, SubExpr, BO->getExprLoc());
Fariborz Jahanianb7859dd2014-11-14 17:12:50 +000010362 }
Richard Trieu791b86e2014-11-19 06:08:18 +000010363
Fariborz Jahanianb7859dd2014-11-14 17:12:50 +000010364 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(E))
10365 if (U->getOpcode() == UO_LNot)
Richard Trieu65724892014-11-15 06:37:39 +000010366 ::CheckBoolLikeConversion(S, U->getSubExpr(), CC);
John McCallcc7e5bf2010-05-06 08:58:33 +000010367}
10368
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +000010369/// Diagnose integer type and any valid implicit conversion to it.
Anastasia Stulova0df4ac32016-11-14 17:39:58 +000010370static bool checkOpenCLEnqueueIntType(Sema &S, Expr *E, const QualType &IntT) {
10371 // Taking into account implicit conversions,
10372 // allow any integer.
10373 if (!E->getType()->isIntegerType()) {
10374 S.Diag(E->getLocStart(),
10375 diag::err_opencl_enqueue_kernel_invalid_local_size_type);
10376 return true;
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +000010377 }
Anastasia Stulova0df4ac32016-11-14 17:39:58 +000010378 // Potentially emit standard warnings for implicit conversions if enabled
10379 // using -Wconversion.
10380 CheckImplicitConversion(S, E, IntT, E->getLocStart());
10381 return false;
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +000010382}
10383
Richard Trieuc1888e02014-06-28 23:25:37 +000010384// Helper function for Sema::DiagnoseAlwaysNonNullPointer.
10385// Returns true when emitting a warning about taking the address of a reference.
10386static bool CheckForReference(Sema &SemaRef, const Expr *E,
Benjamin Kramer7320b992016-06-15 14:20:56 +000010387 const PartialDiagnostic &PD) {
Richard Trieuc1888e02014-06-28 23:25:37 +000010388 E = E->IgnoreParenImpCasts();
10389
10390 const FunctionDecl *FD = nullptr;
10391
10392 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
10393 if (!DRE->getDecl()->getType()->isReferenceType())
10394 return false;
10395 } else if (const MemberExpr *M = dyn_cast<MemberExpr>(E)) {
10396 if (!M->getMemberDecl()->getType()->isReferenceType())
10397 return false;
10398 } else if (const CallExpr *Call = dyn_cast<CallExpr>(E)) {
David Majnemerced8bdf2015-02-25 17:36:15 +000010399 if (!Call->getCallReturnType(SemaRef.Context)->isReferenceType())
Richard Trieuc1888e02014-06-28 23:25:37 +000010400 return false;
10401 FD = Call->getDirectCallee();
10402 } else {
10403 return false;
10404 }
10405
10406 SemaRef.Diag(E->getExprLoc(), PD);
10407
10408 // If possible, point to location of function.
10409 if (FD) {
10410 SemaRef.Diag(FD->getLocation(), diag::note_reference_is_return_value) << FD;
10411 }
10412
10413 return true;
10414}
10415
Richard Trieu4cbff5c2014-08-08 22:41:43 +000010416// Returns true if the SourceLocation is expanded from any macro body.
10417// Returns false if the SourceLocation is invalid, is from not in a macro
10418// expansion, or is from expanded from a top-level macro argument.
10419static bool IsInAnyMacroBody(const SourceManager &SM, SourceLocation Loc) {
10420 if (Loc.isInvalid())
10421 return false;
10422
10423 while (Loc.isMacroID()) {
10424 if (SM.isMacroBodyExpansion(Loc))
10425 return true;
10426 Loc = SM.getImmediateMacroCallerLoc(Loc);
10427 }
10428
10429 return false;
10430}
10431
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010432/// Diagnose pointers that are always non-null.
Richard Trieu3bb8b562014-02-26 02:36:06 +000010433/// \param E the expression containing the pointer
10434/// \param NullKind NPCK_NotNull if E is a cast to bool, otherwise, E is
10435/// compared to a null pointer
10436/// \param IsEqual True when the comparison is equal to a null pointer
10437/// \param Range Extra SourceRange to highlight in the diagnostic
10438void Sema::DiagnoseAlwaysNonNullPointer(Expr *E,
10439 Expr::NullPointerConstantKind NullKind,
10440 bool IsEqual, SourceRange Range) {
Richard Trieuddd01ce2014-06-09 22:53:25 +000010441 if (!E)
10442 return;
Richard Trieu3bb8b562014-02-26 02:36:06 +000010443
10444 // Don't warn inside macros.
Richard Trieu4cbff5c2014-08-08 22:41:43 +000010445 if (E->getExprLoc().isMacroID()) {
10446 const SourceManager &SM = getSourceManager();
10447 if (IsInAnyMacroBody(SM, E->getExprLoc()) ||
10448 IsInAnyMacroBody(SM, Range.getBegin()))
Richard Trieu3bb8b562014-02-26 02:36:06 +000010449 return;
Richard Trieu4cbff5c2014-08-08 22:41:43 +000010450 }
Richard Trieu3bb8b562014-02-26 02:36:06 +000010451 E = E->IgnoreImpCasts();
10452
10453 const bool IsCompare = NullKind != Expr::NPCK_NotNull;
10454
Richard Trieuf7432752014-06-06 21:39:26 +000010455 if (isa<CXXThisExpr>(E)) {
10456 unsigned DiagID = IsCompare ? diag::warn_this_null_compare
10457 : diag::warn_this_bool_conversion;
10458 Diag(E->getExprLoc(), DiagID) << E->getSourceRange() << Range << IsEqual;
10459 return;
10460 }
10461
Richard Trieu3bb8b562014-02-26 02:36:06 +000010462 bool IsAddressOf = false;
10463
10464 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
10465 if (UO->getOpcode() != UO_AddrOf)
10466 return;
10467 IsAddressOf = true;
10468 E = UO->getSubExpr();
10469 }
10470
Richard Trieuc1888e02014-06-28 23:25:37 +000010471 if (IsAddressOf) {
10472 unsigned DiagID = IsCompare
10473 ? diag::warn_address_of_reference_null_compare
10474 : diag::warn_address_of_reference_bool_conversion;
10475 PartialDiagnostic PD = PDiag(DiagID) << E->getSourceRange() << Range
10476 << IsEqual;
10477 if (CheckForReference(*this, E, PD)) {
10478 return;
10479 }
10480 }
10481
Nick Lewyckybc85ec82016-06-15 05:18:39 +000010482 auto ComplainAboutNonnullParamOrCall = [&](const Attr *NonnullAttr) {
10483 bool IsParam = isa<NonNullAttr>(NonnullAttr);
George Burgess IV850269a2015-12-08 22:02:00 +000010484 std::string Str;
10485 llvm::raw_string_ostream S(Str);
10486 E->printPretty(S, nullptr, getPrintingPolicy());
10487 unsigned DiagID = IsCompare ? diag::warn_nonnull_expr_compare
10488 : diag::warn_cast_nonnull_to_bool;
10489 Diag(E->getExprLoc(), DiagID) << IsParam << S.str()
10490 << E->getSourceRange() << Range << IsEqual;
Nick Lewyckybc85ec82016-06-15 05:18:39 +000010491 Diag(NonnullAttr->getLocation(), diag::note_declared_nonnull) << IsParam;
George Burgess IV850269a2015-12-08 22:02:00 +000010492 };
10493
10494 // If we have a CallExpr that is tagged with returns_nonnull, we can complain.
10495 if (auto *Call = dyn_cast<CallExpr>(E->IgnoreParenImpCasts())) {
10496 if (auto *Callee = Call->getDirectCallee()) {
Nick Lewyckybc85ec82016-06-15 05:18:39 +000010497 if (const Attr *A = Callee->getAttr<ReturnsNonNullAttr>()) {
10498 ComplainAboutNonnullParamOrCall(A);
George Burgess IV850269a2015-12-08 22:02:00 +000010499 return;
10500 }
10501 }
10502 }
10503
Richard Trieu3bb8b562014-02-26 02:36:06 +000010504 // Expect to find a single Decl. Skip anything more complicated.
Craig Topperc3ec1492014-05-26 06:22:03 +000010505 ValueDecl *D = nullptr;
Richard Trieu3bb8b562014-02-26 02:36:06 +000010506 if (DeclRefExpr *R = dyn_cast<DeclRefExpr>(E)) {
10507 D = R->getDecl();
10508 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
10509 D = M->getMemberDecl();
10510 }
10511
10512 // Weak Decls can be null.
10513 if (!D || D->isWeak())
10514 return;
George Burgess IV850269a2015-12-08 22:02:00 +000010515
Fariborz Jahanianef202d92014-11-18 21:57:54 +000010516 // Check for parameter decl with nonnull attribute
George Burgess IV850269a2015-12-08 22:02:00 +000010517 if (const auto* PV = dyn_cast<ParmVarDecl>(D)) {
10518 if (getCurFunction() &&
10519 !getCurFunction()->ModifiedNonNullParams.count(PV)) {
Nick Lewyckybc85ec82016-06-15 05:18:39 +000010520 if (const Attr *A = PV->getAttr<NonNullAttr>()) {
10521 ComplainAboutNonnullParamOrCall(A);
George Burgess IV850269a2015-12-08 22:02:00 +000010522 return;
10523 }
10524
10525 if (const auto *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
David Majnemera3debed2016-06-24 05:33:44 +000010526 auto ParamIter = llvm::find(FD->parameters(), PV);
George Burgess IV850269a2015-12-08 22:02:00 +000010527 assert(ParamIter != FD->param_end());
10528 unsigned ParamNo = std::distance(FD->param_begin(), ParamIter);
10529
Fariborz Jahanianef202d92014-11-18 21:57:54 +000010530 for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) {
10531 if (!NonNull->args_size()) {
Nick Lewyckybc85ec82016-06-15 05:18:39 +000010532 ComplainAboutNonnullParamOrCall(NonNull);
George Burgess IV850269a2015-12-08 22:02:00 +000010533 return;
Fariborz Jahanianef202d92014-11-18 21:57:54 +000010534 }
George Burgess IV850269a2015-12-08 22:02:00 +000010535
Joel E. Denny81508102018-03-13 14:51:22 +000010536 for (const ParamIdx &ArgNo : NonNull->args()) {
10537 if (ArgNo.getASTIndex() == ParamNo) {
Nick Lewyckybc85ec82016-06-15 05:18:39 +000010538 ComplainAboutNonnullParamOrCall(NonNull);
Fariborz Jahanianef202d92014-11-18 21:57:54 +000010539 return;
10540 }
George Burgess IV850269a2015-12-08 22:02:00 +000010541 }
10542 }
Fariborz Jahanianef202d92014-11-18 21:57:54 +000010543 }
10544 }
George Burgess IV850269a2015-12-08 22:02:00 +000010545 }
10546
Richard Trieu3bb8b562014-02-26 02:36:06 +000010547 QualType T = D->getType();
10548 const bool IsArray = T->isArrayType();
10549 const bool IsFunction = T->isFunctionType();
10550
Richard Trieuc1888e02014-06-28 23:25:37 +000010551 // Address of function is used to silence the function warning.
10552 if (IsAddressOf && IsFunction) {
10553 return;
Richard Trieu3bb8b562014-02-26 02:36:06 +000010554 }
10555
10556 // Found nothing.
10557 if (!IsAddressOf && !IsFunction && !IsArray)
10558 return;
10559
10560 // Pretty print the expression for the diagnostic.
10561 std::string Str;
10562 llvm::raw_string_ostream S(Str);
Craig Topperc3ec1492014-05-26 06:22:03 +000010563 E->printPretty(S, nullptr, getPrintingPolicy());
Richard Trieu3bb8b562014-02-26 02:36:06 +000010564
10565 unsigned DiagID = IsCompare ? diag::warn_null_pointer_compare
10566 : diag::warn_impcast_pointer_to_bool;
Craig Topperfa1340f2015-12-23 05:44:46 +000010567 enum {
10568 AddressOf,
10569 FunctionPointer,
10570 ArrayPointer
10571 } DiagType;
Richard Trieu3bb8b562014-02-26 02:36:06 +000010572 if (IsAddressOf)
10573 DiagType = AddressOf;
10574 else if (IsFunction)
10575 DiagType = FunctionPointer;
10576 else if (IsArray)
10577 DiagType = ArrayPointer;
10578 else
10579 llvm_unreachable("Could not determine diagnostic.");
10580 Diag(E->getExprLoc(), DiagID) << DiagType << S.str() << E->getSourceRange()
10581 << Range << IsEqual;
10582
10583 if (!IsFunction)
10584 return;
10585
10586 // Suggest '&' to silence the function warning.
10587 Diag(E->getExprLoc(), diag::note_function_warning_silence)
10588 << FixItHint::CreateInsertion(E->getLocStart(), "&");
10589
10590 // Check to see if '()' fixit should be emitted.
10591 QualType ReturnType;
10592 UnresolvedSet<4> NonTemplateOverloads;
10593 tryExprAsCall(*E, ReturnType, NonTemplateOverloads);
10594 if (ReturnType.isNull())
10595 return;
10596
10597 if (IsCompare) {
10598 // There are two cases here. If there is null constant, the only suggest
10599 // for a pointer return type. If the null is 0, then suggest if the return
10600 // type is a pointer or an integer type.
10601 if (!ReturnType->isPointerType()) {
10602 if (NullKind == Expr::NPCK_ZeroExpression ||
10603 NullKind == Expr::NPCK_ZeroLiteral) {
10604 if (!ReturnType->isIntegerType())
10605 return;
10606 } else {
10607 return;
10608 }
10609 }
10610 } else { // !IsCompare
10611 // For function to bool, only suggest if the function pointer has bool
10612 // return type.
10613 if (!ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
10614 return;
10615 }
10616 Diag(E->getExprLoc(), diag::note_function_to_function_call)
Alp Tokerb6cc5922014-05-03 03:45:55 +000010617 << FixItHint::CreateInsertion(getLocForEndOfToken(E->getLocEnd()), "()");
Richard Trieu3bb8b562014-02-26 02:36:06 +000010618}
10619
John McCallcc7e5bf2010-05-06 08:58:33 +000010620/// Diagnoses "dangerous" implicit conversions within the given
10621/// expression (which is a full expression). Implements -Wconversion
10622/// and -Wsign-compare.
John McCallacf0ee52010-10-08 02:01:28 +000010623///
10624/// \param CC the "context" location of the implicit conversion, i.e.
10625/// the most location of the syntactic entity requiring the implicit
10626/// conversion
10627void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
John McCallcc7e5bf2010-05-06 08:58:33 +000010628 // Don't diagnose in unevaluated contexts.
David Blaikie131fcb42012-08-06 22:47:24 +000010629 if (isUnevaluatedContext())
John McCallcc7e5bf2010-05-06 08:58:33 +000010630 return;
10631
10632 // Don't diagnose for value- or type-dependent expressions.
10633 if (E->isTypeDependent() || E->isValueDependent())
10634 return;
10635
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000010636 // Check for array bounds violations in cases where the check isn't triggered
10637 // elsewhere for other Expr types (like BinaryOperators), e.g. when an
10638 // ArraySubscriptExpr is on the RHS of a variable initialization.
10639 CheckArrayAccess(E);
10640
John McCallacf0ee52010-10-08 02:01:28 +000010641 // This is not the right CC for (e.g.) a variable initialization.
10642 AnalyzeImplicitConversions(*this, E, CC);
John McCallcc7e5bf2010-05-06 08:58:33 +000010643}
10644
Richard Trieu65724892014-11-15 06:37:39 +000010645/// CheckBoolLikeConversion - Check conversion of given expression to boolean.
10646/// Input argument E is a logical expression.
10647void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) {
10648 ::CheckBoolLikeConversion(*this, E, CC);
10649}
10650
Richard Smith9f7df0c2017-06-26 23:19:32 +000010651/// Diagnose when expression is an integer constant expression and its evaluation
10652/// results in integer overflow
10653void Sema::CheckForIntOverflow (Expr *E) {
10654 // Use a work list to deal with nested struct initializers.
10655 SmallVector<Expr *, 2> Exprs(1, E);
10656
10657 do {
Volodymyr Sapsaica7902f2018-03-27 21:29:05 +000010658 Expr *OriginalE = Exprs.pop_back_val();
10659 Expr *E = OriginalE->IgnoreParenCasts();
Richard Smith9f7df0c2017-06-26 23:19:32 +000010660
Volodymyr Sapsaica7902f2018-03-27 21:29:05 +000010661 if (isa<BinaryOperator>(E)) {
10662 E->EvaluateForOverflow(Context);
Richard Smith9f7df0c2017-06-26 23:19:32 +000010663 continue;
10664 }
10665
Volodymyr Sapsaica7902f2018-03-27 21:29:05 +000010666 if (auto InitList = dyn_cast<InitListExpr>(OriginalE))
Richard Smith9f7df0c2017-06-26 23:19:32 +000010667 Exprs.append(InitList->inits().begin(), InitList->inits().end());
Volodymyr Sapsaica7902f2018-03-27 21:29:05 +000010668 else if (isa<ObjCBoxedExpr>(OriginalE))
10669 E->EvaluateForOverflow(Context);
10670 else if (auto Call = dyn_cast<CallExpr>(E))
10671 Exprs.append(Call->arg_begin(), Call->arg_end());
10672 else if (auto Message = dyn_cast<ObjCMessageExpr>(E))
10673 Exprs.append(Message->arg_begin(), Message->arg_end());
Richard Smith9f7df0c2017-06-26 23:19:32 +000010674 } while (!Exprs.empty());
10675}
10676
Richard Smithc406cb72013-01-17 01:17:56 +000010677namespace {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010678
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010679/// Visitor for expressions which looks for unsequenced operations on the
Richard Smithc406cb72013-01-17 01:17:56 +000010680/// same object.
10681class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010682 using Base = EvaluatedExprVisitor<SequenceChecker>;
Richard Smithe3dbfe02013-06-30 10:40:20 +000010683
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010684 /// A tree of sequenced regions within an expression. Two regions are
Richard Smithc406cb72013-01-17 01:17:56 +000010685 /// unsequenced if one is an ancestor or a descendent of the other. When we
10686 /// finish processing an expression with sequencing, such as a comma
10687 /// expression, we fold its tree nodes into its parent, since they are
10688 /// unsequenced with respect to nodes we will visit later.
10689 class SequenceTree {
10690 struct Value {
10691 explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {}
10692 unsigned Parent : 31;
Aaron Ballmanaffa1c32016-07-06 18:33:01 +000010693 unsigned Merged : 1;
Richard Smithc406cb72013-01-17 01:17:56 +000010694 };
Robert Wilhelmb869a8f2013-08-10 12:33:24 +000010695 SmallVector<Value, 8> Values;
Richard Smithc406cb72013-01-17 01:17:56 +000010696
10697 public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010698 /// A region within an expression which may be sequenced with respect
Richard Smithc406cb72013-01-17 01:17:56 +000010699 /// to some other region.
10700 class Seq {
Richard Smithc406cb72013-01-17 01:17:56 +000010701 friend class SequenceTree;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010702
10703 unsigned Index = 0;
10704
10705 explicit Seq(unsigned N) : Index(N) {}
10706
Richard Smithc406cb72013-01-17 01:17:56 +000010707 public:
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010708 Seq() = default;
Richard Smithc406cb72013-01-17 01:17:56 +000010709 };
10710
10711 SequenceTree() { Values.push_back(Value(0)); }
10712 Seq root() const { return Seq(0); }
10713
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010714 /// Create a new sequence of operations, which is an unsequenced
Richard Smithc406cb72013-01-17 01:17:56 +000010715 /// subset of \p Parent. This sequence of operations is sequenced with
10716 /// respect to other children of \p Parent.
10717 Seq allocate(Seq Parent) {
10718 Values.push_back(Value(Parent.Index));
10719 return Seq(Values.size() - 1);
10720 }
10721
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010722 /// Merge a sequence of operations into its parent.
Richard Smithc406cb72013-01-17 01:17:56 +000010723 void merge(Seq S) {
10724 Values[S.Index].Merged = true;
10725 }
10726
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010727 /// Determine whether two operations are unsequenced. This operation
Richard Smithc406cb72013-01-17 01:17:56 +000010728 /// is asymmetric: \p Cur should be the more recent sequence, and \p Old
10729 /// should have been merged into its parent as appropriate.
10730 bool isUnsequenced(Seq Cur, Seq Old) {
10731 unsigned C = representative(Cur.Index);
10732 unsigned Target = representative(Old.Index);
10733 while (C >= Target) {
10734 if (C == Target)
10735 return true;
10736 C = Values[C].Parent;
10737 }
10738 return false;
10739 }
10740
10741 private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010742 /// Pick a representative for a sequence.
Richard Smithc406cb72013-01-17 01:17:56 +000010743 unsigned representative(unsigned K) {
10744 if (Values[K].Merged)
10745 // Perform path compression as we go.
10746 return Values[K].Parent = representative(Values[K].Parent);
10747 return K;
10748 }
10749 };
10750
10751 /// An object for which we can track unsequenced uses.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010752 using Object = NamedDecl *;
Richard Smithc406cb72013-01-17 01:17:56 +000010753
10754 /// Different flavors of object usage which we track. We only track the
10755 /// least-sequenced usage of each kind.
10756 enum UsageKind {
10757 /// A read of an object. Multiple unsequenced reads are OK.
10758 UK_Use,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010759
Richard Smithc406cb72013-01-17 01:17:56 +000010760 /// A modification of an object which is sequenced before the value
Richard Smith83e37bee2013-06-26 23:16:51 +000010761 /// computation of the expression, such as ++n in C++.
Richard Smithc406cb72013-01-17 01:17:56 +000010762 UK_ModAsValue,
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010763
Richard Smithc406cb72013-01-17 01:17:56 +000010764 /// A modification of an object which is not sequenced before the value
10765 /// computation of the expression, such as n++.
10766 UK_ModAsSideEffect,
10767
10768 UK_Count = UK_ModAsSideEffect + 1
10769 };
10770
10771 struct Usage {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010772 Expr *Use = nullptr;
Richard Smithc406cb72013-01-17 01:17:56 +000010773 SequenceTree::Seq Seq;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010774
10775 Usage() = default;
Richard Smithc406cb72013-01-17 01:17:56 +000010776 };
10777
10778 struct UsageInfo {
Richard Smithc406cb72013-01-17 01:17:56 +000010779 Usage Uses[UK_Count];
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010780
Richard Smithc406cb72013-01-17 01:17:56 +000010781 /// Have we issued a diagnostic for this variable already?
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010782 bool Diagnosed = false;
10783
10784 UsageInfo() = default;
Richard Smithc406cb72013-01-17 01:17:56 +000010785 };
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010786 using UsageInfoMap = llvm::SmallDenseMap<Object, UsageInfo, 16>;
Richard Smithc406cb72013-01-17 01:17:56 +000010787
10788 Sema &SemaRef;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010789
Richard Smithc406cb72013-01-17 01:17:56 +000010790 /// Sequenced regions within the expression.
10791 SequenceTree Tree;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010792
Richard Smithc406cb72013-01-17 01:17:56 +000010793 /// Declaration modifications and references which we have seen.
10794 UsageInfoMap UsageMap;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010795
Richard Smithc406cb72013-01-17 01:17:56 +000010796 /// The region we are currently within.
10797 SequenceTree::Seq Region;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010798
Richard Smithc406cb72013-01-17 01:17:56 +000010799 /// Filled in with declarations which were modified as a side-effect
10800 /// (that is, post-increment operations).
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010801 SmallVectorImpl<std::pair<Object, Usage>> *ModAsSideEffect = nullptr;
10802
Richard Smithd33f5202013-01-17 23:18:09 +000010803 /// Expressions to check later. We defer checking these to reduce
10804 /// stack usage.
Robert Wilhelmb869a8f2013-08-10 12:33:24 +000010805 SmallVectorImpl<Expr *> &WorkList;
Richard Smithc406cb72013-01-17 01:17:56 +000010806
10807 /// RAII object wrapping the visitation of a sequenced subexpression of an
10808 /// expression. At the end of this process, the side-effects of the evaluation
10809 /// become sequenced with respect to the value computation of the result, so
10810 /// we downgrade any UK_ModAsSideEffect within the evaluation to
10811 /// UK_ModAsValue.
10812 struct SequencedSubexpression {
10813 SequencedSubexpression(SequenceChecker &Self)
10814 : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) {
10815 Self.ModAsSideEffect = &ModAsSideEffect;
10816 }
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010817
Richard Smithc406cb72013-01-17 01:17:56 +000010818 ~SequencedSubexpression() {
David Majnemerf7e36092016-06-23 00:15:04 +000010819 for (auto &M : llvm::reverse(ModAsSideEffect)) {
10820 UsageInfo &U = Self.UsageMap[M.first];
Richard Smithe8efd992014-12-03 01:05:50 +000010821 auto &SideEffectUsage = U.Uses[UK_ModAsSideEffect];
David Majnemerf7e36092016-06-23 00:15:04 +000010822 Self.addUsage(U, M.first, SideEffectUsage.Use, UK_ModAsValue);
10823 SideEffectUsage = M.second;
Richard Smithc406cb72013-01-17 01:17:56 +000010824 }
10825 Self.ModAsSideEffect = OldModAsSideEffect;
10826 }
10827
10828 SequenceChecker &Self;
Robert Wilhelmb869a8f2013-08-10 12:33:24 +000010829 SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010830 SmallVectorImpl<std::pair<Object, Usage>> *OldModAsSideEffect;
Richard Smithc406cb72013-01-17 01:17:56 +000010831 };
10832
Richard Smith40238f02013-06-20 22:21:56 +000010833 /// RAII object wrapping the visitation of a subexpression which we might
10834 /// choose to evaluate as a constant. If any subexpression is evaluated and
10835 /// found to be non-constant, this allows us to suppress the evaluation of
10836 /// the outer expression.
10837 class EvaluationTracker {
10838 public:
10839 EvaluationTracker(SequenceChecker &Self)
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010840 : Self(Self), Prev(Self.EvalTracker) {
Richard Smith40238f02013-06-20 22:21:56 +000010841 Self.EvalTracker = this;
10842 }
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010843
Richard Smith40238f02013-06-20 22:21:56 +000010844 ~EvaluationTracker() {
10845 Self.EvalTracker = Prev;
10846 if (Prev)
10847 Prev->EvalOK &= EvalOK;
10848 }
10849
10850 bool evaluate(const Expr *E, bool &Result) {
10851 if (!EvalOK || E->isValueDependent())
10852 return false;
10853 EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context);
10854 return EvalOK;
10855 }
10856
10857 private:
10858 SequenceChecker &Self;
10859 EvaluationTracker *Prev;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010860 bool EvalOK = true;
10861 } *EvalTracker = nullptr;
Richard Smith40238f02013-06-20 22:21:56 +000010862
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010863 /// Find the object which is produced by the specified expression,
Richard Smithc406cb72013-01-17 01:17:56 +000010864 /// if any.
10865 Object getObject(Expr *E, bool Mod) const {
10866 E = E->IgnoreParenCasts();
10867 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
10868 if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec))
10869 return getObject(UO->getSubExpr(), Mod);
10870 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
10871 if (BO->getOpcode() == BO_Comma)
10872 return getObject(BO->getRHS(), Mod);
10873 if (Mod && BO->isAssignmentOp())
10874 return getObject(BO->getLHS(), Mod);
10875 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
10876 // FIXME: Check for more interesting cases, like "x.n = ++x.n".
10877 if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts()))
10878 return ME->getMemberDecl();
10879 } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
10880 // FIXME: If this is a reference, map through to its value.
10881 return DRE->getDecl();
Craig Topperc3ec1492014-05-26 06:22:03 +000010882 return nullptr;
Richard Smithc406cb72013-01-17 01:17:56 +000010883 }
10884
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010885 /// Note that an object was modified or used by an expression.
Richard Smithc406cb72013-01-17 01:17:56 +000010886 void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) {
10887 Usage &U = UI.Uses[UK];
10888 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) {
10889 if (UK == UK_ModAsSideEffect && ModAsSideEffect)
10890 ModAsSideEffect->push_back(std::make_pair(O, U));
10891 U.Use = Ref;
10892 U.Seq = Region;
10893 }
10894 }
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010895
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010896 /// Check whether a modification or use conflicts with a prior usage.
Richard Smithc406cb72013-01-17 01:17:56 +000010897 void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind,
10898 bool IsModMod) {
10899 if (UI.Diagnosed)
10900 return;
10901
10902 const Usage &U = UI.Uses[OtherKind];
10903 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq))
10904 return;
10905
10906 Expr *Mod = U.Use;
10907 Expr *ModOrUse = Ref;
10908 if (OtherKind == UK_Use)
10909 std::swap(Mod, ModOrUse);
10910
10911 SemaRef.Diag(Mod->getExprLoc(),
10912 IsModMod ? diag::warn_unsequenced_mod_mod
10913 : diag::warn_unsequenced_mod_use)
10914 << O << SourceRange(ModOrUse->getExprLoc());
10915 UI.Diagnosed = true;
10916 }
10917
10918 void notePreUse(Object O, Expr *Use) {
10919 UsageInfo &U = UsageMap[O];
10920 // Uses conflict with other modifications.
10921 checkUsage(O, U, Use, UK_ModAsValue, false);
10922 }
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010923
Richard Smithc406cb72013-01-17 01:17:56 +000010924 void notePostUse(Object O, Expr *Use) {
10925 UsageInfo &U = UsageMap[O];
10926 checkUsage(O, U, Use, UK_ModAsSideEffect, false);
10927 addUsage(U, O, Use, UK_Use);
10928 }
10929
10930 void notePreMod(Object O, Expr *Mod) {
10931 UsageInfo &U = UsageMap[O];
10932 // Modifications conflict with other modifications and with uses.
10933 checkUsage(O, U, Mod, UK_ModAsValue, true);
10934 checkUsage(O, U, Mod, UK_Use, false);
10935 }
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010936
Richard Smithc406cb72013-01-17 01:17:56 +000010937 void notePostMod(Object O, Expr *Use, UsageKind UK) {
10938 UsageInfo &U = UsageMap[O];
10939 checkUsage(O, U, Use, UK_ModAsSideEffect, true);
10940 addUsage(U, O, Use, UK);
10941 }
10942
10943public:
Robert Wilhelmb869a8f2013-08-10 12:33:24 +000010944 SequenceChecker(Sema &S, Expr *E, SmallVectorImpl<Expr *> &WorkList)
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000010945 : Base(S.Context), SemaRef(S), Region(Tree.root()), WorkList(WorkList) {
Richard Smithc406cb72013-01-17 01:17:56 +000010946 Visit(E);
10947 }
10948
10949 void VisitStmt(Stmt *S) {
10950 // Skip all statements which aren't expressions for now.
10951 }
10952
10953 void VisitExpr(Expr *E) {
10954 // By default, just recurse to evaluated subexpressions.
Richard Smithe3dbfe02013-06-30 10:40:20 +000010955 Base::VisitStmt(E);
Richard Smithc406cb72013-01-17 01:17:56 +000010956 }
10957
10958 void VisitCastExpr(CastExpr *E) {
10959 Object O = Object();
10960 if (E->getCastKind() == CK_LValueToRValue)
10961 O = getObject(E->getSubExpr(), false);
10962
10963 if (O)
10964 notePreUse(O, E);
10965 VisitExpr(E);
10966 if (O)
10967 notePostUse(O, E);
10968 }
10969
10970 void VisitBinComma(BinaryOperator *BO) {
10971 // C++11 [expr.comma]p1:
10972 // Every value computation and side effect associated with the left
10973 // expression is sequenced before every value computation and side
10974 // effect associated with the right expression.
10975 SequenceTree::Seq LHS = Tree.allocate(Region);
10976 SequenceTree::Seq RHS = Tree.allocate(Region);
10977 SequenceTree::Seq OldRegion = Region;
10978
10979 {
10980 SequencedSubexpression SeqLHS(*this);
10981 Region = LHS;
10982 Visit(BO->getLHS());
10983 }
10984
10985 Region = RHS;
10986 Visit(BO->getRHS());
10987
10988 Region = OldRegion;
10989
10990 // Forget that LHS and RHS are sequenced. They are both unsequenced
10991 // with respect to other stuff.
10992 Tree.merge(LHS);
10993 Tree.merge(RHS);
10994 }
10995
10996 void VisitBinAssign(BinaryOperator *BO) {
10997 // The modification is sequenced after the value computation of the LHS
10998 // and RHS, so check it before inspecting the operands and update the
10999 // map afterwards.
11000 Object O = getObject(BO->getLHS(), true);
11001 if (!O)
11002 return VisitExpr(BO);
11003
11004 notePreMod(O, BO);
11005
11006 // C++11 [expr.ass]p7:
11007 // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated
11008 // only once.
11009 //
11010 // Therefore, for a compound assignment operator, O is considered used
11011 // everywhere except within the evaluation of E1 itself.
11012 if (isa<CompoundAssignOperator>(BO))
11013 notePreUse(O, BO);
11014
11015 Visit(BO->getLHS());
11016
11017 if (isa<CompoundAssignOperator>(BO))
11018 notePostUse(O, BO);
11019
11020 Visit(BO->getRHS());
11021
Richard Smith83e37bee2013-06-26 23:16:51 +000011022 // C++11 [expr.ass]p1:
11023 // the assignment is sequenced [...] before the value computation of the
11024 // assignment expression.
11025 // C11 6.5.16/3 has no such rule.
11026 notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
11027 : UK_ModAsSideEffect);
Richard Smithc406cb72013-01-17 01:17:56 +000011028 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +000011029
Richard Smithc406cb72013-01-17 01:17:56 +000011030 void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) {
11031 VisitBinAssign(CAO);
11032 }
11033
11034 void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
11035 void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
11036 void VisitUnaryPreIncDec(UnaryOperator *UO) {
11037 Object O = getObject(UO->getSubExpr(), true);
11038 if (!O)
11039 return VisitExpr(UO);
11040
11041 notePreMod(O, UO);
11042 Visit(UO->getSubExpr());
Richard Smith83e37bee2013-06-26 23:16:51 +000011043 // C++11 [expr.pre.incr]p1:
11044 // the expression ++x is equivalent to x+=1
11045 notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
11046 : UK_ModAsSideEffect);
Richard Smithc406cb72013-01-17 01:17:56 +000011047 }
11048
11049 void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
11050 void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
11051 void VisitUnaryPostIncDec(UnaryOperator *UO) {
11052 Object O = getObject(UO->getSubExpr(), true);
11053 if (!O)
11054 return VisitExpr(UO);
11055
11056 notePreMod(O, UO);
11057 Visit(UO->getSubExpr());
11058 notePostMod(O, UO, UK_ModAsSideEffect);
11059 }
11060
11061 /// Don't visit the RHS of '&&' or '||' if it might not be evaluated.
11062 void VisitBinLOr(BinaryOperator *BO) {
11063 // The side-effects of the LHS of an '&&' are sequenced before the
11064 // value computation of the RHS, and hence before the value computation
11065 // of the '&&' itself, unless the LHS evaluates to zero. We treat them
11066 // as if they were unconditionally sequenced.
Richard Smith40238f02013-06-20 22:21:56 +000011067 EvaluationTracker Eval(*this);
Richard Smithc406cb72013-01-17 01:17:56 +000011068 {
11069 SequencedSubexpression Sequenced(*this);
11070 Visit(BO->getLHS());
11071 }
11072
11073 bool Result;
Richard Smith40238f02013-06-20 22:21:56 +000011074 if (Eval.evaluate(BO->getLHS(), Result)) {
Richard Smith01a7fba2013-01-17 22:06:26 +000011075 if (!Result)
11076 Visit(BO->getRHS());
11077 } else {
11078 // Check for unsequenced operations in the RHS, treating it as an
11079 // entirely separate evaluation.
11080 //
11081 // FIXME: If there are operations in the RHS which are unsequenced
11082 // with respect to operations outside the RHS, and those operations
11083 // are unconditionally evaluated, diagnose them.
Richard Smithd33f5202013-01-17 23:18:09 +000011084 WorkList.push_back(BO->getRHS());
Richard Smith01a7fba2013-01-17 22:06:26 +000011085 }
Richard Smithc406cb72013-01-17 01:17:56 +000011086 }
11087 void VisitBinLAnd(BinaryOperator *BO) {
Richard Smith40238f02013-06-20 22:21:56 +000011088 EvaluationTracker Eval(*this);
Richard Smithc406cb72013-01-17 01:17:56 +000011089 {
11090 SequencedSubexpression Sequenced(*this);
11091 Visit(BO->getLHS());
11092 }
11093
11094 bool Result;
Richard Smith40238f02013-06-20 22:21:56 +000011095 if (Eval.evaluate(BO->getLHS(), Result)) {
Richard Smith01a7fba2013-01-17 22:06:26 +000011096 if (Result)
11097 Visit(BO->getRHS());
11098 } else {
Richard Smithd33f5202013-01-17 23:18:09 +000011099 WorkList.push_back(BO->getRHS());
Richard Smith01a7fba2013-01-17 22:06:26 +000011100 }
Richard Smithc406cb72013-01-17 01:17:56 +000011101 }
11102
11103 // Only visit the condition, unless we can be sure which subexpression will
11104 // be chosen.
11105 void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) {
Richard Smith40238f02013-06-20 22:21:56 +000011106 EvaluationTracker Eval(*this);
Richard Smith83e37bee2013-06-26 23:16:51 +000011107 {
11108 SequencedSubexpression Sequenced(*this);
11109 Visit(CO->getCond());
11110 }
Richard Smithc406cb72013-01-17 01:17:56 +000011111
11112 bool Result;
Richard Smith40238f02013-06-20 22:21:56 +000011113 if (Eval.evaluate(CO->getCond(), Result))
Richard Smithc406cb72013-01-17 01:17:56 +000011114 Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr());
Richard Smith01a7fba2013-01-17 22:06:26 +000011115 else {
Richard Smithd33f5202013-01-17 23:18:09 +000011116 WorkList.push_back(CO->getTrueExpr());
11117 WorkList.push_back(CO->getFalseExpr());
Richard Smith01a7fba2013-01-17 22:06:26 +000011118 }
Richard Smithc406cb72013-01-17 01:17:56 +000011119 }
11120
Richard Smithe3dbfe02013-06-30 10:40:20 +000011121 void VisitCallExpr(CallExpr *CE) {
11122 // C++11 [intro.execution]p15:
11123 // When calling a function [...], every value computation and side effect
11124 // associated with any argument expression, or with the postfix expression
11125 // designating the called function, is sequenced before execution of every
11126 // expression or statement in the body of the function [and thus before
11127 // the value computation of its result].
11128 SequencedSubexpression Sequenced(*this);
11129 Base::VisitCallExpr(CE);
11130
11131 // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions.
11132 }
11133
Richard Smithc406cb72013-01-17 01:17:56 +000011134 void VisitCXXConstructExpr(CXXConstructExpr *CCE) {
Richard Smithe3dbfe02013-06-30 10:40:20 +000011135 // This is a call, so all subexpressions are sequenced before the result.
11136 SequencedSubexpression Sequenced(*this);
11137
Richard Smithc406cb72013-01-17 01:17:56 +000011138 if (!CCE->isListInitialization())
11139 return VisitExpr(CCE);
11140
11141 // In C++11, list initializations are sequenced.
Robert Wilhelmb869a8f2013-08-10 12:33:24 +000011142 SmallVector<SequenceTree::Seq, 32> Elts;
Richard Smithc406cb72013-01-17 01:17:56 +000011143 SequenceTree::Seq Parent = Region;
11144 for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(),
11145 E = CCE->arg_end();
11146 I != E; ++I) {
11147 Region = Tree.allocate(Parent);
11148 Elts.push_back(Region);
11149 Visit(*I);
11150 }
11151
11152 // Forget that the initializers are sequenced.
11153 Region = Parent;
11154 for (unsigned I = 0; I < Elts.size(); ++I)
11155 Tree.merge(Elts[I]);
11156 }
11157
11158 void VisitInitListExpr(InitListExpr *ILE) {
11159 if (!SemaRef.getLangOpts().CPlusPlus11)
11160 return VisitExpr(ILE);
11161
11162 // In C++11, list initializations are sequenced.
Robert Wilhelmb869a8f2013-08-10 12:33:24 +000011163 SmallVector<SequenceTree::Seq, 32> Elts;
Richard Smithc406cb72013-01-17 01:17:56 +000011164 SequenceTree::Seq Parent = Region;
11165 for (unsigned I = 0; I < ILE->getNumInits(); ++I) {
11166 Expr *E = ILE->getInit(I);
11167 if (!E) continue;
11168 Region = Tree.allocate(Parent);
11169 Elts.push_back(Region);
11170 Visit(E);
11171 }
11172
11173 // Forget that the initializers are sequenced.
11174 Region = Parent;
11175 for (unsigned I = 0; I < Elts.size(); ++I)
11176 Tree.merge(Elts[I]);
11177 }
11178};
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000011179
11180} // namespace
Richard Smithc406cb72013-01-17 01:17:56 +000011181
11182void Sema::CheckUnsequencedOperations(Expr *E) {
Robert Wilhelmb869a8f2013-08-10 12:33:24 +000011183 SmallVector<Expr *, 8> WorkList;
Richard Smithd33f5202013-01-17 23:18:09 +000011184 WorkList.push_back(E);
11185 while (!WorkList.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +000011186 Expr *Item = WorkList.pop_back_val();
Richard Smithd33f5202013-01-17 23:18:09 +000011187 SequenceChecker(*this, Item, WorkList);
11188 }
Richard Smithc406cb72013-01-17 01:17:56 +000011189}
11190
Fariborz Jahaniane735ff92013-01-24 22:11:45 +000011191void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc,
11192 bool IsConstexpr) {
Richard Smithc406cb72013-01-17 01:17:56 +000011193 CheckImplicitConversions(E, CheckLoc);
Richard Trieu71d74d42016-08-05 21:02:34 +000011194 if (!E->isInstantiationDependent())
11195 CheckUnsequencedOperations(E);
Fariborz Jahaniane735ff92013-01-24 22:11:45 +000011196 if (!IsConstexpr && !E->isValueDependent())
Richard Smith9f7df0c2017-06-26 23:19:32 +000011197 CheckForIntOverflow(E);
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000011198 DiagnoseMisalignedMembers();
Richard Smithc406cb72013-01-17 01:17:56 +000011199}
11200
John McCall1f425642010-11-11 03:21:53 +000011201void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
11202 FieldDecl *BitField,
11203 Expr *Init) {
11204 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
11205}
11206
David Majnemer61a5bbf2015-04-07 22:08:51 +000011207static void diagnoseArrayStarInParamType(Sema &S, QualType PType,
11208 SourceLocation Loc) {
11209 if (!PType->isVariablyModifiedType())
11210 return;
11211 if (const auto *PointerTy = dyn_cast<PointerType>(PType)) {
11212 diagnoseArrayStarInParamType(S, PointerTy->getPointeeType(), Loc);
11213 return;
11214 }
David Majnemerdf8f73f2015-04-09 19:53:25 +000011215 if (const auto *ReferenceTy = dyn_cast<ReferenceType>(PType)) {
11216 diagnoseArrayStarInParamType(S, ReferenceTy->getPointeeType(), Loc);
11217 return;
11218 }
David Majnemer61a5bbf2015-04-07 22:08:51 +000011219 if (const auto *ParenTy = dyn_cast<ParenType>(PType)) {
11220 diagnoseArrayStarInParamType(S, ParenTy->getInnerType(), Loc);
11221 return;
11222 }
11223
11224 const ArrayType *AT = S.Context.getAsArrayType(PType);
11225 if (!AT)
11226 return;
11227
11228 if (AT->getSizeModifier() != ArrayType::Star) {
11229 diagnoseArrayStarInParamType(S, AT->getElementType(), Loc);
11230 return;
11231 }
11232
11233 S.Diag(Loc, diag::err_array_star_in_function_definition);
11234}
11235
Mike Stump0c2ec772010-01-21 03:59:47 +000011236/// CheckParmsForFunctionDef - Check that the parameters of the given
11237/// function are appropriate for the definition of a function. This
11238/// takes care of any checks that cannot be performed on the
11239/// declaration itself, e.g., that the types of each of the function
11240/// parameters are complete.
David Majnemer59f77922016-06-24 04:05:48 +000011241bool Sema::CheckParmsForFunctionDef(ArrayRef<ParmVarDecl *> Parameters,
Douglas Gregorb524d902010-11-01 18:37:59 +000011242 bool CheckParameterNames) {
Mike Stump0c2ec772010-01-21 03:59:47 +000011243 bool HasInvalidParm = false;
David Majnemer59f77922016-06-24 04:05:48 +000011244 for (ParmVarDecl *Param : Parameters) {
Mike Stump0c2ec772010-01-21 03:59:47 +000011245 // C99 6.7.5.3p4: the parameters in a parameter type list in a
11246 // function declarator that is part of a function definition of
11247 // that function shall not have incomplete type.
11248 //
11249 // This is also C++ [dcl.fct]p6.
11250 if (!Param->isInvalidDecl() &&
11251 RequireCompleteType(Param->getLocation(), Param->getType(),
Douglas Gregor7bfb2d02012-05-04 16:32:21 +000011252 diag::err_typecheck_decl_incomplete_type)) {
Mike Stump0c2ec772010-01-21 03:59:47 +000011253 Param->setInvalidDecl();
11254 HasInvalidParm = true;
11255 }
11256
11257 // C99 6.9.1p5: If the declarator includes a parameter type list, the
11258 // declaration of each parameter shall include an identifier.
Douglas Gregorb524d902010-11-01 18:37:59 +000011259 if (CheckParameterNames &&
Craig Topperc3ec1492014-05-26 06:22:03 +000011260 Param->getIdentifier() == nullptr &&
Mike Stump0c2ec772010-01-21 03:59:47 +000011261 !Param->isImplicit() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +000011262 !getLangOpts().CPlusPlus)
Mike Stump0c2ec772010-01-21 03:59:47 +000011263 Diag(Param->getLocation(), diag::err_parameter_name_omitted);
Sam Weinigdeb55d52010-02-01 05:02:49 +000011264
11265 // C99 6.7.5.3p12:
11266 // If the function declarator is not part of a definition of that
11267 // function, parameters may have incomplete type and may use the [*]
11268 // notation in their sequences of declarator specifiers to specify
11269 // variable length array types.
11270 QualType PType = Param->getOriginalType();
David Majnemer61a5bbf2015-04-07 22:08:51 +000011271 // FIXME: This diagnostic should point the '[*]' if source-location
11272 // information is added for it.
11273 diagnoseArrayStarInParamType(*this, PType, Param->getLocation());
Reid Kleckner23f4c4b2013-06-21 12:45:15 +000011274
Akira Hatanaka02914dc2018-02-05 20:23:22 +000011275 // If the parameter is a c++ class type and it has to be destructed in the
11276 // callee function, declare the destructor so that it can be called by the
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +000011277 // callee function. Do not perform any direct access check on the dtor here.
Akira Hatanaka02914dc2018-02-05 20:23:22 +000011278 if (!Param->isInvalidDecl()) {
11279 if (CXXRecordDecl *ClassDecl = Param->getType()->getAsCXXRecordDecl()) {
11280 if (!ClassDecl->isInvalidDecl() &&
11281 !ClassDecl->hasIrrelevantDestructor() &&
11282 !ClassDecl->isDependentContext() &&
Akira Hatanaka85282972018-05-15 21:00:30 +000011283 ClassDecl->isParamDestroyedInCallee()) {
Akira Hatanaka02914dc2018-02-05 20:23:22 +000011284 CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl);
11285 MarkFunctionReferenced(Param->getLocation(), Destructor);
11286 DiagnoseUseOfDecl(Destructor, Param->getLocation());
Hans Wennborg0f3c10c2014-01-13 17:23:24 +000011287 }
11288 }
Reid Kleckner23f4c4b2013-06-21 12:45:15 +000011289 }
George Burgess IV3e3bb95b2015-12-02 21:58:08 +000011290
11291 // Parameters with the pass_object_size attribute only need to be marked
11292 // constant at function definitions. Because we lack information about
11293 // whether we're on a declaration or definition when we're instantiating the
11294 // attribute, we need to check for constness here.
11295 if (const auto *Attr = Param->getAttr<PassObjectSizeAttr>())
11296 if (!Param->getType().isConstQualified())
11297 Diag(Param->getLocation(), diag::err_attribute_pointers_only)
11298 << Attr->getSpelling() << 1;
Mike Stump0c2ec772010-01-21 03:59:47 +000011299 }
11300
11301 return HasInvalidParm;
11302}
John McCall2b5c1b22010-08-12 21:44:57 +000011303
Akira Hatanaka21e5fdd2016-11-30 19:42:03 +000011304/// A helper function to get the alignment of a Decl referred to by DeclRefExpr
11305/// or MemberExpr.
11306static CharUnits getDeclAlign(Expr *E, CharUnits TypeAlign,
11307 ASTContext &Context) {
11308 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
11309 return Context.getDeclAlign(DRE->getDecl());
11310
11311 if (const auto *ME = dyn_cast<MemberExpr>(E))
11312 return Context.getDeclAlign(ME->getMemberDecl());
11313
11314 return TypeAlign;
11315}
11316
John McCall2b5c1b22010-08-12 21:44:57 +000011317/// CheckCastAlign - Implements -Wcast-align, which warns when a
11318/// pointer cast increases the alignment requirements.
11319void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
11320 // This is actually a lot of work to potentially be doing on every
11321 // cast; don't do it if we're ignoring -Wcast_align (as is the default).
Alp Tokerd4a3f0e2014-06-15 23:30:39 +000011322 if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin()))
John McCall2b5c1b22010-08-12 21:44:57 +000011323 return;
11324
11325 // Ignore dependent types.
11326 if (T->isDependentType() || Op->getType()->isDependentType())
11327 return;
11328
11329 // Require that the destination be a pointer type.
11330 const PointerType *DestPtr = T->getAs<PointerType>();
11331 if (!DestPtr) return;
11332
11333 // If the destination has alignment 1, we're done.
11334 QualType DestPointee = DestPtr->getPointeeType();
11335 if (DestPointee->isIncompleteType()) return;
11336 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
11337 if (DestAlign.isOne()) return;
11338
11339 // Require that the source be a pointer type.
11340 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
11341 if (!SrcPtr) return;
11342 QualType SrcPointee = SrcPtr->getPointeeType();
11343
11344 // Whitelist casts from cv void*. We already implicitly
11345 // whitelisted casts to cv void*, since they have alignment 1.
11346 // Also whitelist casts involving incomplete types, which implicitly
11347 // includes 'void'.
11348 if (SrcPointee->isIncompleteType()) return;
11349
11350 CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
Akira Hatanaka21e5fdd2016-11-30 19:42:03 +000011351
11352 if (auto *CE = dyn_cast<CastExpr>(Op)) {
11353 if (CE->getCastKind() == CK_ArrayToPointerDecay)
11354 SrcAlign = getDeclAlign(CE->getSubExpr(), SrcAlign, Context);
11355 } else if (auto *UO = dyn_cast<UnaryOperator>(Op)) {
11356 if (UO->getOpcode() == UO_AddrOf)
11357 SrcAlign = getDeclAlign(UO->getSubExpr(), SrcAlign, Context);
11358 }
11359
John McCall2b5c1b22010-08-12 21:44:57 +000011360 if (SrcAlign >= DestAlign) return;
11361
11362 Diag(TRange.getBegin(), diag::warn_cast_align)
11363 << Op->getType() << T
11364 << static_cast<unsigned>(SrcAlign.getQuantity())
11365 << static_cast<unsigned>(DestAlign.getQuantity())
11366 << TRange << Op->getSourceRange();
11367}
11368
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011369/// Check whether this array fits the idiom of a size-one tail padded
Chandler Carruth28389f02011-08-05 09:10:50 +000011370/// array member of a struct.
11371///
11372/// We avoid emitting out-of-bounds access warnings for such arrays as they are
11373/// commonly used to emulate flexible arrays in C89 code.
Benjamin Kramer7320b992016-06-15 14:20:56 +000011374static bool IsTailPaddedMemberArray(Sema &S, const llvm::APInt &Size,
Chandler Carruth28389f02011-08-05 09:10:50 +000011375 const NamedDecl *ND) {
11376 if (Size != 1 || !ND) return false;
11377
11378 const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
11379 if (!FD) return false;
11380
11381 // Don't consider sizes resulting from macro expansions or template argument
11382 // substitution to form C89 tail-padded arrays.
Sean Callanan06a48a62012-05-04 18:22:53 +000011383
11384 TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
Ted Kremenek7ebb4932012-05-09 05:35:08 +000011385 while (TInfo) {
11386 TypeLoc TL = TInfo->getTypeLoc();
11387 // Look through typedefs.
David Blaikie6adc78e2013-02-18 22:06:02 +000011388 if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) {
11389 const TypedefNameDecl *TDL = TTL.getTypedefNameDecl();
Ted Kremenek7ebb4932012-05-09 05:35:08 +000011390 TInfo = TDL->getTypeSourceInfo();
11391 continue;
11392 }
David Blaikie6adc78e2013-02-18 22:06:02 +000011393 if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) {
11394 const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr());
Chad Rosier70299922013-02-06 00:58:34 +000011395 if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
11396 return false;
11397 }
Ted Kremenek7ebb4932012-05-09 05:35:08 +000011398 break;
Sean Callanan06a48a62012-05-04 18:22:53 +000011399 }
Chandler Carruth28389f02011-08-05 09:10:50 +000011400
11401 const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
Matt Beaumont-Gayc93b4892011-11-29 22:43:53 +000011402 if (!RD) return false;
11403 if (RD->isUnion()) return false;
11404 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
11405 if (!CRD->isStandardLayout()) return false;
11406 }
Chandler Carruth28389f02011-08-05 09:10:50 +000011407
Benjamin Kramer8c543672011-08-06 03:04:42 +000011408 // See if this is the last field decl in the record.
11409 const Decl *D = FD;
11410 while ((D = D->getNextDeclInContext()))
11411 if (isa<FieldDecl>(D))
11412 return false;
11413 return true;
Chandler Carruth28389f02011-08-05 09:10:50 +000011414}
11415
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011416void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
Matt Beaumont-Gay5533a552011-12-14 16:02:15 +000011417 const ArraySubscriptExpr *ASE,
Richard Smith13f67182011-12-16 19:31:14 +000011418 bool AllowOnePastEnd, bool IndexNegated) {
Eli Friedman84e6e5c2012-02-27 21:21:40 +000011419 IndexExpr = IndexExpr->IgnoreParenImpCasts();
Matt Beaumont-Gay5533a552011-12-14 16:02:15 +000011420 if (IndexExpr->isValueDependent())
11421 return;
11422
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +000011423 const Type *EffectiveType =
11424 BaseExpr->getType()->getPointeeOrArrayElementType();
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011425 BaseExpr = BaseExpr->IgnoreParenCasts();
Chandler Carruth2a666fc2011-02-17 20:55:08 +000011426 const ConstantArrayType *ArrayTy =
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011427 Context.getAsConstantArrayType(BaseExpr->getType());
Chandler Carruth2a666fc2011-02-17 20:55:08 +000011428 if (!ArrayTy)
Ted Kremenek64699be2011-02-16 01:57:07 +000011429 return;
Chandler Carruth1af88f12011-02-17 21:10:52 +000011430
Chandler Carruth2a666fc2011-02-17 20:55:08 +000011431 llvm::APSInt index;
Richard Smith0c6124b2015-12-03 01:36:22 +000011432 if (!IndexExpr->EvaluateAsInt(index, Context, Expr::SE_AllowSideEffects))
Ted Kremenek64699be2011-02-16 01:57:07 +000011433 return;
Richard Smith13f67182011-12-16 19:31:14 +000011434 if (IndexNegated)
11435 index = -index;
Ted Kremenek108b2d52011-02-16 04:01:44 +000011436
Craig Topperc3ec1492014-05-26 06:22:03 +000011437 const NamedDecl *ND = nullptr;
Chandler Carruth126b1552011-08-05 08:07:29 +000011438 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
George Burgess IV00f70bd2018-03-01 05:43:23 +000011439 ND = DRE->getDecl();
Chandler Carruth28389f02011-08-05 09:10:50 +000011440 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
George Burgess IV00f70bd2018-03-01 05:43:23 +000011441 ND = ME->getMemberDecl();
Chandler Carruth126b1552011-08-05 08:07:29 +000011442
Ted Kremeneke4b316c2011-02-23 23:06:04 +000011443 if (index.isUnsigned() || !index.isNegative()) {
Ted Kremeneka7ced2c2011-02-18 02:27:00 +000011444 llvm::APInt size = ArrayTy->getSize();
Chandler Carruth1af88f12011-02-17 21:10:52 +000011445 if (!size.isStrictlyPositive())
11446 return;
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011447
Anastasia Stulovadb7a31c2016-07-05 11:31:24 +000011448 const Type *BaseType = BaseExpr->getType()->getPointeeOrArrayElementType();
Nico Weber7c299802011-09-17 22:59:41 +000011449 if (BaseType != EffectiveType) {
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011450 // Make sure we're comparing apples to apples when comparing index to size
11451 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
11452 uint64_t array_typesize = Context.getTypeSize(BaseType);
Kaelyn Uhrain0fb0bb12011-08-10 19:47:25 +000011453 // Handle ptrarith_typesize being zero, such as when casting to void*
Kaelyn Uhraine5353762011-08-10 18:49:28 +000011454 if (!ptrarith_typesize) ptrarith_typesize = 1;
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011455 if (ptrarith_typesize != array_typesize) {
11456 // There's a cast to a different size type involved
11457 uint64_t ratio = array_typesize / ptrarith_typesize;
11458 // TODO: Be smarter about handling cases where array_typesize is not a
11459 // multiple of ptrarith_typesize
11460 if (ptrarith_typesize * ratio == array_typesize)
11461 size *= llvm::APInt(size.getBitWidth(), ratio);
11462 }
11463 }
11464
Chandler Carruth2a666fc2011-02-17 20:55:08 +000011465 if (size.getBitWidth() > index.getBitWidth())
Eli Friedman84e6e5c2012-02-27 21:21:40 +000011466 index = index.zext(size.getBitWidth());
Ted Kremeneka7ced2c2011-02-18 02:27:00 +000011467 else if (size.getBitWidth() < index.getBitWidth())
Eli Friedman84e6e5c2012-02-27 21:21:40 +000011468 size = size.zext(index.getBitWidth());
Ted Kremeneka7ced2c2011-02-18 02:27:00 +000011469
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011470 // For array subscripting the index must be less than size, but for pointer
11471 // arithmetic also allow the index (offset) to be equal to size since
11472 // computing the next address after the end of the array is legal and
11473 // commonly done e.g. in C++ iterators and range-based for loops.
Eli Friedman84e6e5c2012-02-27 21:21:40 +000011474 if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
Chandler Carruth126b1552011-08-05 08:07:29 +000011475 return;
11476
11477 // Also don't warn for arrays of size 1 which are members of some
11478 // structure. These are often used to approximate flexible arrays in C89
11479 // code.
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011480 if (IsTailPaddedMemberArray(*this, size, ND))
Ted Kremenek108b2d52011-02-16 04:01:44 +000011481 return;
Chandler Carruth2a666fc2011-02-17 20:55:08 +000011482
Matt Beaumont-Gay5533a552011-12-14 16:02:15 +000011483 // Suppress the warning if the subscript expression (as identified by the
11484 // ']' location) and the index expression are both from macro expansions
11485 // within a system header.
11486 if (ASE) {
11487 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
11488 ASE->getRBracketLoc());
11489 if (SourceMgr.isInSystemHeader(RBracketLoc)) {
11490 SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
11491 IndexExpr->getLocStart());
Eli Friedman5ba37d52013-08-22 00:27:10 +000011492 if (SourceMgr.isWrittenInSameFile(RBracketLoc, IndexLoc))
Matt Beaumont-Gay5533a552011-12-14 16:02:15 +000011493 return;
11494 }
11495 }
11496
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011497 unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
Matt Beaumont-Gay5533a552011-12-14 16:02:15 +000011498 if (ASE)
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011499 DiagID = diag::warn_array_index_exceeds_bounds;
11500
11501 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
11502 PDiag(DiagID) << index.toString(10, true)
11503 << size.toString(10, true)
11504 << (unsigned)size.getLimitedValue(~0U)
11505 << IndexExpr->getSourceRange());
Chandler Carruth2a666fc2011-02-17 20:55:08 +000011506 } else {
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011507 unsigned DiagID = diag::warn_array_index_precedes_bounds;
Matt Beaumont-Gay5533a552011-12-14 16:02:15 +000011508 if (!ASE) {
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011509 DiagID = diag::warn_ptr_arith_precedes_bounds;
11510 if (index.isNegative()) index = -index;
11511 }
11512
11513 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
11514 PDiag(DiagID) << index.toString(10, true)
11515 << IndexExpr->getSourceRange());
Ted Kremenek64699be2011-02-16 01:57:07 +000011516 }
Chandler Carruth1af88f12011-02-17 21:10:52 +000011517
Matt Beaumont-Gayb2339822011-11-29 19:27:11 +000011518 if (!ND) {
11519 // Try harder to find a NamedDecl to point at in the note.
11520 while (const ArraySubscriptExpr *ASE =
11521 dyn_cast<ArraySubscriptExpr>(BaseExpr))
11522 BaseExpr = ASE->getBase()->IgnoreParenCasts();
11523 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
George Burgess IV00f70bd2018-03-01 05:43:23 +000011524 ND = DRE->getDecl();
Matt Beaumont-Gayb2339822011-11-29 19:27:11 +000011525 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
George Burgess IV00f70bd2018-03-01 05:43:23 +000011526 ND = ME->getMemberDecl();
Matt Beaumont-Gayb2339822011-11-29 19:27:11 +000011527 }
11528
Chandler Carruth1af88f12011-02-17 21:10:52 +000011529 if (ND)
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011530 DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
11531 PDiag(diag::note_array_index_out_of_bounds)
11532 << ND->getDeclName());
Ted Kremenek64699be2011-02-16 01:57:07 +000011533}
11534
Ted Kremenekdf26df72011-03-01 18:41:00 +000011535void Sema::CheckArrayAccess(const Expr *expr) {
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011536 int AllowOnePastEnd = 0;
11537 while (expr) {
11538 expr = expr->IgnoreParenImpCasts();
Ted Kremenekdf26df72011-03-01 18:41:00 +000011539 switch (expr->getStmtClass()) {
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011540 case Stmt::ArraySubscriptExprClass: {
11541 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
Matt Beaumont-Gay5533a552011-12-14 16:02:15 +000011542 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011543 AllowOnePastEnd > 0);
Aaron Ballman93c6ba12018-04-24 19:21:04 +000011544 expr = ASE->getBase();
11545 break;
11546 }
11547 case Stmt::MemberExprClass: {
11548 expr = cast<MemberExpr>(expr)->getBase();
11549 break;
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011550 }
Alexey Bataev1a3320e2015-08-25 14:24:04 +000011551 case Stmt::OMPArraySectionExprClass: {
11552 const OMPArraySectionExpr *ASE = cast<OMPArraySectionExpr>(expr);
11553 if (ASE->getLowerBound())
11554 CheckArrayAccess(ASE->getBase(), ASE->getLowerBound(),
11555 /*ASE=*/nullptr, AllowOnePastEnd > 0);
11556 return;
11557 }
Kaelyn Uhrain2e7aa5a2011-08-05 23:18:04 +000011558 case Stmt::UnaryOperatorClass: {
11559 // Only unwrap the * and & unary operators
11560 const UnaryOperator *UO = cast<UnaryOperator>(expr);
11561 expr = UO->getSubExpr();
11562 switch (UO->getOpcode()) {
11563 case UO_AddrOf:
11564 AllowOnePastEnd++;
11565 break;
11566 case UO_Deref:
11567 AllowOnePastEnd--;
11568 break;
11569 default:
11570 return;
11571 }
11572 break;
11573 }
Ted Kremenekdf26df72011-03-01 18:41:00 +000011574 case Stmt::ConditionalOperatorClass: {
11575 const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
11576 if (const Expr *lhs = cond->getLHS())
11577 CheckArrayAccess(lhs);
11578 if (const Expr *rhs = cond->getRHS())
11579 CheckArrayAccess(rhs);
11580 return;
11581 }
Daniel Marjamaki20a209e2017-02-28 14:53:50 +000011582 case Stmt::CXXOperatorCallExprClass: {
11583 const auto *OCE = cast<CXXOperatorCallExpr>(expr);
11584 for (const auto *Arg : OCE->arguments())
11585 CheckArrayAccess(Arg);
11586 return;
11587 }
Ted Kremenekdf26df72011-03-01 18:41:00 +000011588 default:
11589 return;
11590 }
Peter Collingbourne91147592011-04-15 00:35:48 +000011591 }
Ted Kremenekdf26df72011-03-01 18:41:00 +000011592}
John McCall31168b02011-06-15 23:02:42 +000011593
11594//===--- CHECK: Objective-C retain cycles ----------------------------------//
11595
11596namespace {
John McCall31168b02011-06-15 23:02:42 +000011597
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000011598struct RetainCycleOwner {
11599 VarDecl *Variable = nullptr;
11600 SourceRange Range;
11601 SourceLocation Loc;
11602 bool Indirect = false;
11603
11604 RetainCycleOwner() = default;
11605
11606 void setLocsFrom(Expr *e) {
11607 Loc = e->getExprLoc();
11608 Range = e->getSourceRange();
11609 }
11610};
11611
11612} // namespace
John McCall31168b02011-06-15 23:02:42 +000011613
11614/// Consider whether capturing the given variable can possibly lead to
11615/// a retain cycle.
11616static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
Sylvestre Ledru33b5baf2012-09-27 10:16:10 +000011617 // In ARC, it's captured strongly iff the variable has __strong
John McCall31168b02011-06-15 23:02:42 +000011618 // lifetime. In MRR, it's captured strongly if the variable is
11619 // __block and has an appropriate type.
11620 if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
11621 return false;
11622
11623 owner.Variable = var;
Jordan Rosefa9e4ba2012-09-15 02:48:31 +000011624 if (ref)
11625 owner.setLocsFrom(ref);
John McCall31168b02011-06-15 23:02:42 +000011626 return true;
11627}
11628
Fariborz Jahanianedbc3452012-01-10 19:28:26 +000011629static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
John McCall31168b02011-06-15 23:02:42 +000011630 while (true) {
11631 e = e->IgnoreParens();
11632 if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
11633 switch (cast->getCastKind()) {
11634 case CK_BitCast:
11635 case CK_LValueBitCast:
11636 case CK_LValueToRValue:
John McCall2d637d22011-09-10 06:18:15 +000011637 case CK_ARCReclaimReturnedObject:
John McCall31168b02011-06-15 23:02:42 +000011638 e = cast->getSubExpr();
11639 continue;
11640
John McCall31168b02011-06-15 23:02:42 +000011641 default:
11642 return false;
11643 }
11644 }
11645
11646 if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
11647 ObjCIvarDecl *ivar = ref->getDecl();
11648 if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
11649 return false;
11650
11651 // Try to find a retain cycle in the base.
Fariborz Jahanianedbc3452012-01-10 19:28:26 +000011652 if (!findRetainCycleOwner(S, ref->getBase(), owner))
John McCall31168b02011-06-15 23:02:42 +000011653 return false;
11654
11655 if (ref->isFreeIvar()) owner.setLocsFrom(ref);
11656 owner.Indirect = true;
11657 return true;
11658 }
11659
11660 if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
11661 VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
11662 if (!var) return false;
11663 return considerVariable(var, ref, owner);
11664 }
11665
John McCall31168b02011-06-15 23:02:42 +000011666 if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
11667 if (member->isArrow()) return false;
11668
11669 // Don't count this as an indirect ownership.
11670 e = member->getBase();
11671 continue;
11672 }
11673
John McCallfe96e0b2011-11-06 09:01:30 +000011674 if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
11675 // Only pay attention to pseudo-objects on property references.
11676 ObjCPropertyRefExpr *pre
11677 = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm()
11678 ->IgnoreParens());
11679 if (!pre) return false;
11680 if (pre->isImplicitProperty()) return false;
11681 ObjCPropertyDecl *property = pre->getExplicitProperty();
11682 if (!property->isRetaining() &&
11683 !(property->getPropertyIvarDecl() &&
11684 property->getPropertyIvarDecl()->getType()
11685 .getObjCLifetime() == Qualifiers::OCL_Strong))
11686 return false;
11687
11688 owner.Indirect = true;
Fariborz Jahanianedbc3452012-01-10 19:28:26 +000011689 if (pre->isSuperReceiver()) {
11690 owner.Variable = S.getCurMethodDecl()->getSelfDecl();
11691 if (!owner.Variable)
11692 return false;
11693 owner.Loc = pre->getLocation();
11694 owner.Range = pre->getSourceRange();
11695 return true;
11696 }
John McCallfe96e0b2011-11-06 09:01:30 +000011697 e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase())
11698 ->getSourceExpr());
11699 continue;
11700 }
11701
John McCall31168b02011-06-15 23:02:42 +000011702 // Array ivars?
11703
11704 return false;
11705 }
11706}
11707
11708namespace {
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000011709
John McCall31168b02011-06-15 23:02:42 +000011710 struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
Fariborz Jahanian8df9e242014-06-12 20:57:14 +000011711 ASTContext &Context;
John McCall31168b02011-06-15 23:02:42 +000011712 VarDecl *Variable;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000011713 Expr *Capturer = nullptr;
11714 bool VarWillBeReased = false;
11715
11716 FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
11717 : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
11718 Context(Context), Variable(variable) {}
John McCall31168b02011-06-15 23:02:42 +000011719
11720 void VisitDeclRefExpr(DeclRefExpr *ref) {
11721 if (ref->getDecl() == Variable && !Capturer)
11722 Capturer = ref;
11723 }
11724
John McCall31168b02011-06-15 23:02:42 +000011725 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
11726 if (Capturer) return;
11727 Visit(ref->getBase());
11728 if (Capturer && ref->isFreeIvar())
11729 Capturer = ref;
11730 }
11731
11732 void VisitBlockExpr(BlockExpr *block) {
11733 // Look inside nested blocks
11734 if (block->getBlockDecl()->capturesVariable(Variable))
11735 Visit(block->getBlockDecl()->getBody());
11736 }
Fariborz Jahanian0e337542012-08-31 20:04:47 +000011737
11738 void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) {
11739 if (Capturer) return;
11740 if (OVE->getSourceExpr())
11741 Visit(OVE->getSourceExpr());
11742 }
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000011743
Fariborz Jahanian8df9e242014-06-12 20:57:14 +000011744 void VisitBinaryOperator(BinaryOperator *BinOp) {
11745 if (!Variable || VarWillBeReased || BinOp->getOpcode() != BO_Assign)
11746 return;
11747 Expr *LHS = BinOp->getLHS();
11748 if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(LHS)) {
11749 if (DRE->getDecl() != Variable)
11750 return;
11751 if (Expr *RHS = BinOp->getRHS()) {
11752 RHS = RHS->IgnoreParenCasts();
11753 llvm::APSInt Value;
11754 VarWillBeReased =
11755 (RHS && RHS->isIntegerConstantExpr(Value, Context) && Value == 0);
11756 }
11757 }
11758 }
John McCall31168b02011-06-15 23:02:42 +000011759 };
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000011760
11761} // namespace
John McCall31168b02011-06-15 23:02:42 +000011762
11763/// Check whether the given argument is a block which captures a
11764/// variable.
11765static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
11766 assert(owner.Variable && owner.Loc.isValid());
11767
11768 e = e->IgnoreParenCasts();
Jordan Rose67e887c2012-09-17 17:54:30 +000011769
11770 // Look through [^{...} copy] and Block_copy(^{...}).
11771 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) {
11772 Selector Cmd = ME->getSelector();
11773 if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") {
11774 e = ME->getInstanceReceiver();
11775 if (!e)
Craig Topperc3ec1492014-05-26 06:22:03 +000011776 return nullptr;
Jordan Rose67e887c2012-09-17 17:54:30 +000011777 e = e->IgnoreParenCasts();
11778 }
11779 } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) {
11780 if (CE->getNumArgs() == 1) {
11781 FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl());
Ted Kremenekb67c6cc2012-10-02 04:36:54 +000011782 if (Fn) {
11783 const IdentifierInfo *FnI = Fn->getIdentifier();
11784 if (FnI && FnI->isStr("_Block_copy")) {
11785 e = CE->getArg(0)->IgnoreParenCasts();
11786 }
11787 }
Jordan Rose67e887c2012-09-17 17:54:30 +000011788 }
11789 }
11790
John McCall31168b02011-06-15 23:02:42 +000011791 BlockExpr *block = dyn_cast<BlockExpr>(e);
11792 if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
Craig Topperc3ec1492014-05-26 06:22:03 +000011793 return nullptr;
John McCall31168b02011-06-15 23:02:42 +000011794
11795 FindCaptureVisitor visitor(S.Context, owner.Variable);
11796 visitor.Visit(block->getBlockDecl()->getBody());
Fariborz Jahanian8df9e242014-06-12 20:57:14 +000011797 return visitor.VarWillBeReased ? nullptr : visitor.Capturer;
John McCall31168b02011-06-15 23:02:42 +000011798}
11799
11800static void diagnoseRetainCycle(Sema &S, Expr *capturer,
11801 RetainCycleOwner &owner) {
11802 assert(capturer);
11803 assert(owner.Variable && owner.Loc.isValid());
11804
11805 S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
11806 << owner.Variable << capturer->getSourceRange();
11807 S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
11808 << owner.Indirect << owner.Range;
11809}
11810
11811/// Check for a keyword selector that starts with the word 'add' or
11812/// 'set'.
11813static bool isSetterLikeSelector(Selector sel) {
11814 if (sel.isUnarySelector()) return false;
11815
Chris Lattner0e62c1c2011-07-23 10:55:15 +000011816 StringRef str = sel.getNameForSlot(0);
John McCall31168b02011-06-15 23:02:42 +000011817 while (!str.empty() && str.front() == '_') str = str.substr(1);
Ted Kremenek764d63a2011-12-01 00:59:21 +000011818 if (str.startswith("set"))
John McCall31168b02011-06-15 23:02:42 +000011819 str = str.substr(3);
Ted Kremenek764d63a2011-12-01 00:59:21 +000011820 else if (str.startswith("add")) {
11821 // Specially whitelist 'addOperationWithBlock:'.
11822 if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock"))
11823 return false;
11824 str = str.substr(3);
11825 }
John McCall31168b02011-06-15 23:02:42 +000011826 else
11827 return false;
11828
11829 if (str.empty()) return true;
Jordan Rosea7d03842013-02-08 22:30:41 +000011830 return !isLowercase(str.front());
John McCall31168b02011-06-15 23:02:42 +000011831}
11832
Benjamin Kramer3a743452015-03-09 15:03:32 +000011833static Optional<int> GetNSMutableArrayArgumentIndex(Sema &S,
11834 ObjCMessageExpr *Message) {
Alex Denisov5dfac812015-08-06 04:51:14 +000011835 bool IsMutableArray = S.NSAPIObj->isSubclassOfNSClass(
11836 Message->getReceiverInterface(),
11837 NSAPI::ClassId_NSMutableArray);
11838 if (!IsMutableArray) {
Alex Denisove1d882c2015-03-04 17:55:52 +000011839 return None;
11840 }
11841
11842 Selector Sel = Message->getSelector();
11843
11844 Optional<NSAPI::NSArrayMethodKind> MKOpt =
11845 S.NSAPIObj->getNSArrayMethodKind(Sel);
11846 if (!MKOpt) {
11847 return None;
11848 }
11849
11850 NSAPI::NSArrayMethodKind MK = *MKOpt;
11851
11852 switch (MK) {
11853 case NSAPI::NSMutableArr_addObject:
11854 case NSAPI::NSMutableArr_insertObjectAtIndex:
11855 case NSAPI::NSMutableArr_setObjectAtIndexedSubscript:
11856 return 0;
11857 case NSAPI::NSMutableArr_replaceObjectAtIndex:
11858 return 1;
11859
11860 default:
11861 return None;
11862 }
11863
11864 return None;
11865}
11866
11867static
11868Optional<int> GetNSMutableDictionaryArgumentIndex(Sema &S,
11869 ObjCMessageExpr *Message) {
Alex Denisov5dfac812015-08-06 04:51:14 +000011870 bool IsMutableDictionary = S.NSAPIObj->isSubclassOfNSClass(
11871 Message->getReceiverInterface(),
11872 NSAPI::ClassId_NSMutableDictionary);
11873 if (!IsMutableDictionary) {
Alex Denisove1d882c2015-03-04 17:55:52 +000011874 return None;
11875 }
11876
11877 Selector Sel = Message->getSelector();
11878
11879 Optional<NSAPI::NSDictionaryMethodKind> MKOpt =
11880 S.NSAPIObj->getNSDictionaryMethodKind(Sel);
11881 if (!MKOpt) {
11882 return None;
11883 }
11884
11885 NSAPI::NSDictionaryMethodKind MK = *MKOpt;
11886
11887 switch (MK) {
11888 case NSAPI::NSMutableDict_setObjectForKey:
11889 case NSAPI::NSMutableDict_setValueForKey:
11890 case NSAPI::NSMutableDict_setObjectForKeyedSubscript:
11891 return 0;
11892
11893 default:
11894 return None;
11895 }
11896
11897 return None;
11898}
11899
11900static Optional<int> GetNSSetArgumentIndex(Sema &S, ObjCMessageExpr *Message) {
Alex Denisov5dfac812015-08-06 04:51:14 +000011901 bool IsMutableSet = S.NSAPIObj->isSubclassOfNSClass(
11902 Message->getReceiverInterface(),
11903 NSAPI::ClassId_NSMutableSet);
Alex Denisove1d882c2015-03-04 17:55:52 +000011904
Alex Denisov5dfac812015-08-06 04:51:14 +000011905 bool IsMutableOrderedSet = S.NSAPIObj->isSubclassOfNSClass(
11906 Message->getReceiverInterface(),
11907 NSAPI::ClassId_NSMutableOrderedSet);
11908 if (!IsMutableSet && !IsMutableOrderedSet) {
Alex Denisove1d882c2015-03-04 17:55:52 +000011909 return None;
11910 }
11911
11912 Selector Sel = Message->getSelector();
11913
11914 Optional<NSAPI::NSSetMethodKind> MKOpt = S.NSAPIObj->getNSSetMethodKind(Sel);
11915 if (!MKOpt) {
11916 return None;
11917 }
11918
11919 NSAPI::NSSetMethodKind MK = *MKOpt;
11920
11921 switch (MK) {
11922 case NSAPI::NSMutableSet_addObject:
11923 case NSAPI::NSOrderedSet_setObjectAtIndex:
11924 case NSAPI::NSOrderedSet_setObjectAtIndexedSubscript:
11925 case NSAPI::NSOrderedSet_insertObjectAtIndex:
11926 return 0;
11927 case NSAPI::NSOrderedSet_replaceObjectAtIndexWithObject:
11928 return 1;
11929 }
11930
11931 return None;
11932}
11933
11934void Sema::CheckObjCCircularContainer(ObjCMessageExpr *Message) {
11935 if (!Message->isInstanceMessage()) {
11936 return;
11937 }
11938
11939 Optional<int> ArgOpt;
11940
11941 if (!(ArgOpt = GetNSMutableArrayArgumentIndex(*this, Message)) &&
11942 !(ArgOpt = GetNSMutableDictionaryArgumentIndex(*this, Message)) &&
11943 !(ArgOpt = GetNSSetArgumentIndex(*this, Message))) {
11944 return;
11945 }
11946
11947 int ArgIndex = *ArgOpt;
11948
Alex Denisove1d882c2015-03-04 17:55:52 +000011949 Expr *Arg = Message->getArg(ArgIndex)->IgnoreImpCasts();
11950 if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Arg)) {
11951 Arg = OE->getSourceExpr()->IgnoreImpCasts();
11952 }
11953
Alex Denisov5dfac812015-08-06 04:51:14 +000011954 if (Message->getReceiverKind() == ObjCMessageExpr::SuperInstance) {
Alex Denisove1d882c2015-03-04 17:55:52 +000011955 if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) {
Alex Denisov5dfac812015-08-06 04:51:14 +000011956 if (ArgRE->isObjCSelfExpr()) {
Alex Denisove1d882c2015-03-04 17:55:52 +000011957 Diag(Message->getSourceRange().getBegin(),
11958 diag::warn_objc_circular_container)
Richard Trieub4025802018-03-28 04:16:13 +000011959 << ArgRE->getDecl() << StringRef("'super'");
Alex Denisove1d882c2015-03-04 17:55:52 +000011960 }
11961 }
Alex Denisov5dfac812015-08-06 04:51:14 +000011962 } else {
11963 Expr *Receiver = Message->getInstanceReceiver()->IgnoreImpCasts();
11964
11965 if (OpaqueValueExpr *OE = dyn_cast<OpaqueValueExpr>(Receiver)) {
11966 Receiver = OE->getSourceExpr()->IgnoreImpCasts();
11967 }
11968
11969 if (DeclRefExpr *ReceiverRE = dyn_cast<DeclRefExpr>(Receiver)) {
11970 if (DeclRefExpr *ArgRE = dyn_cast<DeclRefExpr>(Arg)) {
11971 if (ReceiverRE->getDecl() == ArgRE->getDecl()) {
11972 ValueDecl *Decl = ReceiverRE->getDecl();
11973 Diag(Message->getSourceRange().getBegin(),
11974 diag::warn_objc_circular_container)
Richard Trieub4025802018-03-28 04:16:13 +000011975 << Decl << Decl;
Alex Denisov5dfac812015-08-06 04:51:14 +000011976 if (!ArgRE->isObjCSelfExpr()) {
11977 Diag(Decl->getLocation(),
11978 diag::note_objc_circular_container_declared_here)
Richard Trieub4025802018-03-28 04:16:13 +000011979 << Decl;
Alex Denisov5dfac812015-08-06 04:51:14 +000011980 }
11981 }
11982 }
11983 } else if (ObjCIvarRefExpr *IvarRE = dyn_cast<ObjCIvarRefExpr>(Receiver)) {
11984 if (ObjCIvarRefExpr *IvarArgRE = dyn_cast<ObjCIvarRefExpr>(Arg)) {
11985 if (IvarRE->getDecl() == IvarArgRE->getDecl()) {
11986 ObjCIvarDecl *Decl = IvarRE->getDecl();
11987 Diag(Message->getSourceRange().getBegin(),
11988 diag::warn_objc_circular_container)
Richard Trieub4025802018-03-28 04:16:13 +000011989 << Decl << Decl;
Alex Denisov5dfac812015-08-06 04:51:14 +000011990 Diag(Decl->getLocation(),
11991 diag::note_objc_circular_container_declared_here)
Richard Trieub4025802018-03-28 04:16:13 +000011992 << Decl;
Alex Denisov5dfac812015-08-06 04:51:14 +000011993 }
Alex Denisove1d882c2015-03-04 17:55:52 +000011994 }
11995 }
11996 }
Alex Denisove1d882c2015-03-04 17:55:52 +000011997}
11998
John McCall31168b02011-06-15 23:02:42 +000011999/// Check a message send to see if it's likely to cause a retain cycle.
12000void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
12001 // Only check instance methods whose selector looks like a setter.
12002 if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
12003 return;
12004
12005 // Try to find a variable that the receiver is strongly owned by.
12006 RetainCycleOwner owner;
12007 if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
Fariborz Jahanianedbc3452012-01-10 19:28:26 +000012008 if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner))
John McCall31168b02011-06-15 23:02:42 +000012009 return;
12010 } else {
12011 assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
12012 owner.Variable = getCurMethodDecl()->getSelfDecl();
12013 owner.Loc = msg->getSuperLoc();
12014 owner.Range = msg->getSuperLoc();
12015 }
12016
12017 // Check whether the receiver is captured by any of the arguments.
Alex Lorenz42a97a92017-11-17 20:44:25 +000012018 const ObjCMethodDecl *MD = msg->getMethodDecl();
12019 for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) {
12020 if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) {
12021 // noescape blocks should not be retained by the method.
12022 if (MD && MD->parameters()[i]->hasAttr<NoEscapeAttr>())
12023 continue;
John McCall31168b02011-06-15 23:02:42 +000012024 return diagnoseRetainCycle(*this, capturer, owner);
Alex Lorenz42a97a92017-11-17 20:44:25 +000012025 }
12026 }
John McCall31168b02011-06-15 23:02:42 +000012027}
12028
12029/// Check a property assign to see if it's likely to cause a retain cycle.
12030void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
12031 RetainCycleOwner owner;
Fariborz Jahanianedbc3452012-01-10 19:28:26 +000012032 if (!findRetainCycleOwner(*this, receiver, owner))
John McCall31168b02011-06-15 23:02:42 +000012033 return;
12034
12035 if (Expr *capturer = findCapturingExpr(*this, argument, owner))
12036 diagnoseRetainCycle(*this, capturer, owner);
12037}
12038
Jordan Rosefa9e4ba2012-09-15 02:48:31 +000012039void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) {
12040 RetainCycleOwner Owner;
Craig Topperc3ec1492014-05-26 06:22:03 +000012041 if (!considerVariable(Var, /*DeclRefExpr=*/nullptr, Owner))
Jordan Rosefa9e4ba2012-09-15 02:48:31 +000012042 return;
12043
12044 // Because we don't have an expression for the variable, we have to set the
12045 // location explicitly here.
12046 Owner.Loc = Var->getLocation();
12047 Owner.Range = Var->getSourceRange();
12048
12049 if (Expr *Capturer = findCapturingExpr(*this, Init, Owner))
12050 diagnoseRetainCycle(*this, Capturer, Owner);
12051}
12052
Ted Kremenek9304da92012-12-21 08:04:28 +000012053static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc,
12054 Expr *RHS, bool isProperty) {
12055 // Check if RHS is an Objective-C object literal, which also can get
12056 // immediately zapped in a weak reference. Note that we explicitly
12057 // allow ObjCStringLiterals, since those are designed to never really die.
12058 RHS = RHS->IgnoreParenImpCasts();
Ted Kremenek44c2a2a2012-12-21 21:59:39 +000012059
Ted Kremenek64873352012-12-21 22:46:35 +000012060 // This enum needs to match with the 'select' in
12061 // warn_objc_arc_literal_assign (off-by-1).
12062 Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS);
12063 if (Kind == Sema::LK_String || Kind == Sema::LK_None)
12064 return false;
Ted Kremenek44c2a2a2012-12-21 21:59:39 +000012065
12066 S.Diag(Loc, diag::warn_arc_literal_assign)
Ted Kremenek64873352012-12-21 22:46:35 +000012067 << (unsigned) Kind
Ted Kremenek9304da92012-12-21 08:04:28 +000012068 << (isProperty ? 0 : 1)
12069 << RHS->getSourceRange();
Ted Kremenek44c2a2a2012-12-21 21:59:39 +000012070
12071 return true;
Ted Kremenek9304da92012-12-21 08:04:28 +000012072}
12073
Ted Kremenekc1f014a2012-12-21 19:45:30 +000012074static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc,
12075 Qualifiers::ObjCLifetime LT,
12076 Expr *RHS, bool isProperty) {
12077 // Strip off any implicit cast added to get to the one ARC-specific.
12078 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
12079 if (cast->getCastKind() == CK_ARCConsumeObject) {
12080 S.Diag(Loc, diag::warn_arc_retained_assign)
12081 << (LT == Qualifiers::OCL_ExplicitNone)
12082 << (isProperty ? 0 : 1)
12083 << RHS->getSourceRange();
12084 return true;
12085 }
12086 RHS = cast->getSubExpr();
12087 }
12088
12089 if (LT == Qualifiers::OCL_Weak &&
12090 checkUnsafeAssignLiteral(S, Loc, RHS, isProperty))
12091 return true;
12092
12093 return false;
12094}
12095
Ted Kremenekb36234d2012-12-21 08:04:20 +000012096bool Sema::checkUnsafeAssigns(SourceLocation Loc,
12097 QualType LHS, Expr *RHS) {
12098 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
12099
12100 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
12101 return false;
12102
12103 if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false))
12104 return true;
12105
12106 return false;
12107}
12108
Fariborz Jahanian5f98da02011-06-24 18:25:34 +000012109void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
12110 Expr *LHS, Expr *RHS) {
Fariborz Jahanianc72a8072012-01-17 22:58:16 +000012111 QualType LHSType;
12112 // PropertyRef on LHS type need be directly obtained from
Alp Tokerf6a24ce2013-12-05 16:25:25 +000012113 // its declaration as it has a PseudoType.
Fariborz Jahanianc72a8072012-01-17 22:58:16 +000012114 ObjCPropertyRefExpr *PRE
12115 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
12116 if (PRE && !PRE->isImplicitProperty()) {
12117 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
12118 if (PD)
12119 LHSType = PD->getType();
12120 }
12121
12122 if (LHSType.isNull())
12123 LHSType = LHS->getType();
Jordan Rose657b5f42012-09-28 22:21:35 +000012124
12125 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
12126
12127 if (LT == Qualifiers::OCL_Weak) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +000012128 if (!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, Loc))
Jordan Rose657b5f42012-09-28 22:21:35 +000012129 getCurFunction()->markSafeWeakUse(LHS);
12130 }
12131
Fariborz Jahanian5f98da02011-06-24 18:25:34 +000012132 if (checkUnsafeAssigns(Loc, LHSType, RHS))
12133 return;
Jordan Rose657b5f42012-09-28 22:21:35 +000012134
Fariborz Jahanian5f98da02011-06-24 18:25:34 +000012135 // FIXME. Check for other life times.
12136 if (LT != Qualifiers::OCL_None)
12137 return;
12138
Fariborz Jahanianc72a8072012-01-17 22:58:16 +000012139 if (PRE) {
Fariborz Jahanian5f98da02011-06-24 18:25:34 +000012140 if (PRE->isImplicitProperty())
12141 return;
12142 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
12143 if (!PD)
12144 return;
12145
Bill Wendling44426052012-12-20 19:22:21 +000012146 unsigned Attributes = PD->getPropertyAttributes();
12147 if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) {
Fariborz Jahanianc72a8072012-01-17 22:58:16 +000012148 // when 'assign' attribute was not explicitly specified
12149 // by user, ignore it and rely on property type itself
12150 // for lifetime info.
12151 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
12152 if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) &&
12153 LHSType->isObjCRetainableType())
12154 return;
12155
Fariborz Jahanian5f98da02011-06-24 18:25:34 +000012156 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
John McCall2d637d22011-09-10 06:18:15 +000012157 if (cast->getCastKind() == CK_ARCConsumeObject) {
Fariborz Jahanian5f98da02011-06-24 18:25:34 +000012158 Diag(Loc, diag::warn_arc_retained_property_assign)
12159 << RHS->getSourceRange();
12160 return;
12161 }
12162 RHS = cast->getSubExpr();
12163 }
Fariborz Jahanianc72a8072012-01-17 22:58:16 +000012164 }
Bill Wendling44426052012-12-20 19:22:21 +000012165 else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) {
Ted Kremenekb36234d2012-12-21 08:04:20 +000012166 if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true))
12167 return;
Fariborz Jahaniandabd1332012-07-06 21:09:27 +000012168 }
Fariborz Jahanian5f98da02011-06-24 18:25:34 +000012169 }
12170}
Dmitri Gribenko800ddf32012-02-14 22:14:32 +000012171
12172//===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
12173
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012174static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
12175 SourceLocation StmtLoc,
12176 const NullStmt *Body) {
Dmitri Gribenko800ddf32012-02-14 22:14:32 +000012177 // Do not warn if the body is a macro that expands to nothing, e.g:
12178 //
12179 // #define CALL(x)
12180 // if (condition)
12181 // CALL(0);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +000012182 if (Body->hasLeadingEmptyMacro())
12183 return false;
12184
12185 // Get line numbers of statement and body.
12186 bool StmtLineInvalid;
Hans Wennborg95419752017-11-20 17:38:16 +000012187 unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc,
Dmitri Gribenko800ddf32012-02-14 22:14:32 +000012188 &StmtLineInvalid);
12189 if (StmtLineInvalid)
12190 return false;
12191
12192 bool BodyLineInvalid;
12193 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
12194 &BodyLineInvalid);
12195 if (BodyLineInvalid)
12196 return false;
12197
12198 // Warn if null statement and body are on the same line.
12199 if (StmtLine != BodyLine)
12200 return false;
12201
12202 return true;
12203}
Dmitri Gribenko800ddf32012-02-14 22:14:32 +000012204
12205void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
12206 const Stmt *Body,
12207 unsigned DiagID) {
12208 // Since this is a syntactic check, don't emit diagnostic for template
12209 // instantiations, this just adds noise.
12210 if (CurrentInstantiationScope)
12211 return;
12212
12213 // The body should be a null statement.
12214 const NullStmt *NBody = dyn_cast<NullStmt>(Body);
12215 if (!NBody)
12216 return;
12217
12218 // Do the usual checks.
12219 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
12220 return;
12221
12222 Diag(NBody->getSemiLoc(), DiagID);
12223 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
12224}
12225
12226void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
12227 const Stmt *PossibleBody) {
12228 assert(!CurrentInstantiationScope); // Ensured by caller
12229
12230 SourceLocation StmtLoc;
12231 const Stmt *Body;
12232 unsigned DiagID;
12233 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
12234 StmtLoc = FS->getRParenLoc();
12235 Body = FS->getBody();
12236 DiagID = diag::warn_empty_for_body;
12237 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
12238 StmtLoc = WS->getCond()->getSourceRange().getEnd();
12239 Body = WS->getBody();
12240 DiagID = diag::warn_empty_while_body;
12241 } else
12242 return; // Neither `for' nor `while'.
12243
12244 // The body should be a null statement.
12245 const NullStmt *NBody = dyn_cast<NullStmt>(Body);
12246 if (!NBody)
12247 return;
12248
12249 // Skip expensive checks if diagnostic is disabled.
Alp Tokerd4a3f0e2014-06-15 23:30:39 +000012250 if (Diags.isIgnored(DiagID, NBody->getSemiLoc()))
Dmitri Gribenko800ddf32012-02-14 22:14:32 +000012251 return;
12252
12253 // Do the usual checks.
12254 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
12255 return;
12256
12257 // `for(...);' and `while(...);' are popular idioms, so in order to keep
12258 // noise level low, emit diagnostics only if for/while is followed by a
12259 // CompoundStmt, e.g.:
12260 // for (int i = 0; i < n; i++);
12261 // {
12262 // a(i);
12263 // }
12264 // or if for/while is followed by a statement with more indentation
12265 // than for/while itself:
12266 // for (int i = 0; i < n; i++);
12267 // a(i);
12268 bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
12269 if (!ProbableTypo) {
12270 bool BodyColInvalid;
12271 unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
12272 PossibleBody->getLocStart(),
12273 &BodyColInvalid);
12274 if (BodyColInvalid)
12275 return;
12276
12277 bool StmtColInvalid;
12278 unsigned StmtCol = SourceMgr.getPresumedColumnNumber(
12279 S->getLocStart(),
12280 &StmtColInvalid);
12281 if (StmtColInvalid)
12282 return;
12283
12284 if (BodyCol > StmtCol)
12285 ProbableTypo = true;
12286 }
12287
12288 if (ProbableTypo) {
12289 Diag(NBody->getSemiLoc(), DiagID);
12290 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
12291 }
12292}
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012293
Richard Trieu36d0b2b2015-01-13 02:32:02 +000012294//===--- CHECK: Warn on self move with std::move. -------------------------===//
12295
12296/// DiagnoseSelfMove - Emits a warning if a value is moved to itself.
12297void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr,
12298 SourceLocation OpLoc) {
Richard Trieu36d0b2b2015-01-13 02:32:02 +000012299 if (Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess, OpLoc))
12300 return;
12301
Richard Smith51ec0cf2017-02-21 01:17:38 +000012302 if (inTemplateInstantiation())
Richard Trieu36d0b2b2015-01-13 02:32:02 +000012303 return;
12304
12305 // Strip parens and casts away.
12306 LHSExpr = LHSExpr->IgnoreParenImpCasts();
12307 RHSExpr = RHSExpr->IgnoreParenImpCasts();
12308
12309 // Check for a call expression
12310 const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr);
12311 if (!CE || CE->getNumArgs() != 1)
12312 return;
12313
12314 // Check for a call to std::move
Nico Weberb688d132017-09-28 16:16:39 +000012315 if (!CE->isCallToStdMove())
Richard Trieu36d0b2b2015-01-13 02:32:02 +000012316 return;
12317
12318 // Get argument from std::move
12319 RHSExpr = CE->getArg(0);
12320
12321 const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr);
12322 const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr);
12323
12324 // Two DeclRefExpr's, check that the decls are the same.
12325 if (LHSDeclRef && RHSDeclRef) {
12326 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
12327 return;
12328 if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
12329 RHSDeclRef->getDecl()->getCanonicalDecl())
12330 return;
12331
12332 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
12333 << LHSExpr->getSourceRange()
12334 << RHSExpr->getSourceRange();
12335 return;
12336 }
12337
12338 // Member variables require a different approach to check for self moves.
12339 // MemberExpr's are the same if every nested MemberExpr refers to the same
12340 // Decl and that the base Expr's are DeclRefExpr's with the same Decl or
12341 // the base Expr's are CXXThisExpr's.
12342 const Expr *LHSBase = LHSExpr;
12343 const Expr *RHSBase = RHSExpr;
12344 const MemberExpr *LHSME = dyn_cast<MemberExpr>(LHSExpr);
12345 const MemberExpr *RHSME = dyn_cast<MemberExpr>(RHSExpr);
12346 if (!LHSME || !RHSME)
12347 return;
12348
12349 while (LHSME && RHSME) {
12350 if (LHSME->getMemberDecl()->getCanonicalDecl() !=
12351 RHSME->getMemberDecl()->getCanonicalDecl())
12352 return;
12353
12354 LHSBase = LHSME->getBase();
12355 RHSBase = RHSME->getBase();
12356 LHSME = dyn_cast<MemberExpr>(LHSBase);
12357 RHSME = dyn_cast<MemberExpr>(RHSBase);
12358 }
12359
12360 LHSDeclRef = dyn_cast<DeclRefExpr>(LHSBase);
12361 RHSDeclRef = dyn_cast<DeclRefExpr>(RHSBase);
12362 if (LHSDeclRef && RHSDeclRef) {
12363 if (!LHSDeclRef->getDecl() || !RHSDeclRef->getDecl())
12364 return;
12365 if (LHSDeclRef->getDecl()->getCanonicalDecl() !=
12366 RHSDeclRef->getDecl()->getCanonicalDecl())
12367 return;
12368
12369 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
12370 << LHSExpr->getSourceRange()
12371 << RHSExpr->getSourceRange();
12372 return;
12373 }
12374
12375 if (isa<CXXThisExpr>(LHSBase) && isa<CXXThisExpr>(RHSBase))
12376 Diag(OpLoc, diag::warn_self_move) << LHSExpr->getType()
12377 << LHSExpr->getSourceRange()
12378 << RHSExpr->getSourceRange();
12379}
12380
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012381//===--- Layout compatibility ----------------------------------------------//
12382
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012383static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012384
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012385/// Check if two enumeration types are layout-compatible.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012386static bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012387 // C++11 [dcl.enum] p8:
12388 // Two enumeration types are layout-compatible if they have the same
12389 // underlying type.
12390 return ED1->isComplete() && ED2->isComplete() &&
12391 C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType());
12392}
12393
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012394/// Check if two fields are layout-compatible.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012395static bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1,
12396 FieldDecl *Field2) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012397 if (!isLayoutCompatible(C, Field1->getType(), Field2->getType()))
12398 return false;
12399
12400 if (Field1->isBitField() != Field2->isBitField())
12401 return false;
12402
12403 if (Field1->isBitField()) {
12404 // Make sure that the bit-fields are the same length.
12405 unsigned Bits1 = Field1->getBitWidthValue(C);
12406 unsigned Bits2 = Field2->getBitWidthValue(C);
12407
12408 if (Bits1 != Bits2)
12409 return false;
12410 }
12411
12412 return true;
12413}
12414
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012415/// Check if two standard-layout structs are layout-compatible.
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012416/// (C++11 [class.mem] p17)
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012417static bool isLayoutCompatibleStruct(ASTContext &C, RecordDecl *RD1,
12418 RecordDecl *RD2) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012419 // If both records are C++ classes, check that base classes match.
12420 if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) {
12421 // If one of records is a CXXRecordDecl we are in C++ mode,
12422 // thus the other one is a CXXRecordDecl, too.
12423 const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2);
12424 // Check number of base classes.
12425 if (D1CXX->getNumBases() != D2CXX->getNumBases())
12426 return false;
12427
12428 // Check the base classes.
12429 for (CXXRecordDecl::base_class_const_iterator
12430 Base1 = D1CXX->bases_begin(),
12431 BaseEnd1 = D1CXX->bases_end(),
12432 Base2 = D2CXX->bases_begin();
12433 Base1 != BaseEnd1;
12434 ++Base1, ++Base2) {
12435 if (!isLayoutCompatible(C, Base1->getType(), Base2->getType()))
12436 return false;
12437 }
12438 } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) {
12439 // If only RD2 is a C++ class, it should have zero base classes.
12440 if (D2CXX->getNumBases() > 0)
12441 return false;
12442 }
12443
12444 // Check the fields.
12445 RecordDecl::field_iterator Field2 = RD2->field_begin(),
12446 Field2End = RD2->field_end(),
12447 Field1 = RD1->field_begin(),
12448 Field1End = RD1->field_end();
12449 for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) {
12450 if (!isLayoutCompatible(C, *Field1, *Field2))
12451 return false;
12452 }
12453 if (Field1 != Field1End || Field2 != Field2End)
12454 return false;
12455
12456 return true;
12457}
12458
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012459/// Check if two standard-layout unions are layout-compatible.
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012460/// (C++11 [class.mem] p18)
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012461static bool isLayoutCompatibleUnion(ASTContext &C, RecordDecl *RD1,
12462 RecordDecl *RD2) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012463 llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields;
Aaron Ballmane8a8bae2014-03-08 20:12:42 +000012464 for (auto *Field2 : RD2->fields())
12465 UnmatchedFields.insert(Field2);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012466
Aaron Ballmane8a8bae2014-03-08 20:12:42 +000012467 for (auto *Field1 : RD1->fields()) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012468 llvm::SmallPtrSet<FieldDecl *, 8>::iterator
12469 I = UnmatchedFields.begin(),
12470 E = UnmatchedFields.end();
12471
12472 for ( ; I != E; ++I) {
Aaron Ballmane8a8bae2014-03-08 20:12:42 +000012473 if (isLayoutCompatible(C, Field1, *I)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012474 bool Result = UnmatchedFields.erase(*I);
12475 (void) Result;
12476 assert(Result);
12477 break;
12478 }
12479 }
12480 if (I == E)
12481 return false;
12482 }
12483
12484 return UnmatchedFields.empty();
12485}
12486
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012487static bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1,
12488 RecordDecl *RD2) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012489 if (RD1->isUnion() != RD2->isUnion())
12490 return false;
12491
12492 if (RD1->isUnion())
12493 return isLayoutCompatibleUnion(C, RD1, RD2);
12494 else
12495 return isLayoutCompatibleStruct(C, RD1, RD2);
12496}
12497
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012498/// Check if two types are layout-compatible in C++11 sense.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012499static bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012500 if (T1.isNull() || T2.isNull())
12501 return false;
12502
12503 // C++11 [basic.types] p11:
12504 // If two types T1 and T2 are the same type, then T1 and T2 are
12505 // layout-compatible types.
12506 if (C.hasSameType(T1, T2))
12507 return true;
12508
12509 T1 = T1.getCanonicalType().getUnqualifiedType();
12510 T2 = T2.getCanonicalType().getUnqualifiedType();
12511
12512 const Type::TypeClass TC1 = T1->getTypeClass();
12513 const Type::TypeClass TC2 = T2->getTypeClass();
12514
12515 if (TC1 != TC2)
12516 return false;
12517
12518 if (TC1 == Type::Enum) {
12519 return isLayoutCompatible(C,
12520 cast<EnumType>(T1)->getDecl(),
12521 cast<EnumType>(T2)->getDecl());
12522 } else if (TC1 == Type::Record) {
12523 if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType())
12524 return false;
12525
12526 return isLayoutCompatible(C,
12527 cast<RecordType>(T1)->getDecl(),
12528 cast<RecordType>(T2)->getDecl());
12529 }
12530
12531 return false;
12532}
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012533
12534//===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----//
12535
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012536/// Given a type tag expression find the type tag itself.
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012537///
12538/// \param TypeExpr Type tag expression, as it appears in user's code.
12539///
12540/// \param VD Declaration of an identifier that appears in a type tag.
12541///
12542/// \param MagicValue Type tag magic value.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012543static bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx,
12544 const ValueDecl **VD, uint64_t *MagicValue) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012545 while(true) {
12546 if (!TypeExpr)
12547 return false;
12548
12549 TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts();
12550
12551 switch (TypeExpr->getStmtClass()) {
12552 case Stmt::UnaryOperatorClass: {
12553 const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr);
12554 if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) {
12555 TypeExpr = UO->getSubExpr();
12556 continue;
12557 }
12558 return false;
12559 }
12560
12561 case Stmt::DeclRefExprClass: {
12562 const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr);
12563 *VD = DRE->getDecl();
12564 return true;
12565 }
12566
12567 case Stmt::IntegerLiteralClass: {
12568 const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr);
12569 llvm::APInt MagicValueAPInt = IL->getValue();
12570 if (MagicValueAPInt.getActiveBits() <= 64) {
12571 *MagicValue = MagicValueAPInt.getZExtValue();
12572 return true;
12573 } else
12574 return false;
12575 }
12576
12577 case Stmt::BinaryConditionalOperatorClass:
12578 case Stmt::ConditionalOperatorClass: {
12579 const AbstractConditionalOperator *ACO =
12580 cast<AbstractConditionalOperator>(TypeExpr);
12581 bool Result;
12582 if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) {
12583 if (Result)
12584 TypeExpr = ACO->getTrueExpr();
12585 else
12586 TypeExpr = ACO->getFalseExpr();
12587 continue;
12588 }
12589 return false;
12590 }
12591
12592 case Stmt::BinaryOperatorClass: {
12593 const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr);
12594 if (BO->getOpcode() == BO_Comma) {
12595 TypeExpr = BO->getRHS();
12596 continue;
12597 }
12598 return false;
12599 }
12600
12601 default:
12602 return false;
12603 }
12604 }
12605}
12606
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012607/// Retrieve the C type corresponding to type tag TypeExpr.
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012608///
12609/// \param TypeExpr Expression that specifies a type tag.
12610///
12611/// \param MagicValues Registered magic values.
12612///
12613/// \param FoundWrongKind Set to true if a type tag was found, but of a wrong
12614/// kind.
12615///
12616/// \param TypeInfo Information about the corresponding C type.
12617///
12618/// \returns true if the corresponding C type was found.
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012619static bool GetMatchingCType(
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012620 const IdentifierInfo *ArgumentKind,
12621 const Expr *TypeExpr, const ASTContext &Ctx,
12622 const llvm::DenseMap<Sema::TypeTagMagicValue,
12623 Sema::TypeTagData> *MagicValues,
12624 bool &FoundWrongKind,
12625 Sema::TypeTagData &TypeInfo) {
12626 FoundWrongKind = false;
12627
12628 // Variable declaration that has type_tag_for_datatype attribute.
Craig Topperc3ec1492014-05-26 06:22:03 +000012629 const ValueDecl *VD = nullptr;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012630
12631 uint64_t MagicValue;
12632
12633 if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue))
12634 return false;
12635
12636 if (VD) {
Benjamin Kramerae852a62014-02-23 14:34:50 +000012637 if (TypeTagForDatatypeAttr *I = VD->getAttr<TypeTagForDatatypeAttr>()) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012638 if (I->getArgumentKind() != ArgumentKind) {
12639 FoundWrongKind = true;
12640 return false;
12641 }
12642 TypeInfo.Type = I->getMatchingCType();
12643 TypeInfo.LayoutCompatible = I->getLayoutCompatible();
12644 TypeInfo.MustBeNull = I->getMustBeNull();
12645 return true;
12646 }
12647 return false;
12648 }
12649
12650 if (!MagicValues)
12651 return false;
12652
12653 llvm::DenseMap<Sema::TypeTagMagicValue,
12654 Sema::TypeTagData>::const_iterator I =
12655 MagicValues->find(std::make_pair(ArgumentKind, MagicValue));
12656 if (I == MagicValues->end())
12657 return false;
12658
12659 TypeInfo = I->second;
12660 return true;
12661}
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012662
12663void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind,
12664 uint64_t MagicValue, QualType Type,
12665 bool LayoutCompatible,
12666 bool MustBeNull) {
12667 if (!TypeTagForDatatypeMagicValues)
12668 TypeTagForDatatypeMagicValues.reset(
12669 new llvm::DenseMap<TypeTagMagicValue, TypeTagData>);
12670
12671 TypeTagMagicValue Magic(ArgumentKind, MagicValue);
12672 (*TypeTagForDatatypeMagicValues)[Magic] =
12673 TypeTagData(Type, LayoutCompatible, MustBeNull);
12674}
12675
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012676static bool IsSameCharType(QualType T1, QualType T2) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012677 const BuiltinType *BT1 = T1->getAs<BuiltinType>();
12678 if (!BT1)
12679 return false;
12680
12681 const BuiltinType *BT2 = T2->getAs<BuiltinType>();
12682 if (!BT2)
12683 return false;
12684
12685 BuiltinType::Kind T1Kind = BT1->getKind();
12686 BuiltinType::Kind T2Kind = BT2->getKind();
12687
12688 return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) ||
12689 (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) ||
12690 (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) ||
12691 (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar);
12692}
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012693
12694void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr,
Aaron Ballmand1f6dcd2017-11-29 23:10:14 +000012695 const ArrayRef<const Expr *> ExprArgs,
12696 SourceLocation CallSiteLoc) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012697 const IdentifierInfo *ArgumentKind = Attr->getArgumentKind();
12698 bool IsPointerAttr = Attr->getIsPointer();
12699
Aaron Ballmand1f6dcd2017-11-29 23:10:14 +000012700 // Retrieve the argument representing the 'type_tag'.
Joel E. Denny81508102018-03-13 14:51:22 +000012701 unsigned TypeTagIdxAST = Attr->getTypeTagIdx().getASTIndex();
12702 if (TypeTagIdxAST >= ExprArgs.size()) {
Aaron Ballmand1f6dcd2017-11-29 23:10:14 +000012703 Diag(CallSiteLoc, diag::err_tag_index_out_of_range)
Joel E. Denny81508102018-03-13 14:51:22 +000012704 << 0 << Attr->getTypeTagIdx().getSourceIndex();
Aaron Ballmand1f6dcd2017-11-29 23:10:14 +000012705 return;
12706 }
Joel E. Denny81508102018-03-13 14:51:22 +000012707 const Expr *TypeTagExpr = ExprArgs[TypeTagIdxAST];
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012708 bool FoundWrongKind;
12709 TypeTagData TypeInfo;
12710 if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context,
12711 TypeTagForDatatypeMagicValues.get(),
12712 FoundWrongKind, TypeInfo)) {
12713 if (FoundWrongKind)
12714 Diag(TypeTagExpr->getExprLoc(),
12715 diag::warn_type_tag_for_datatype_wrong_kind)
12716 << TypeTagExpr->getSourceRange();
12717 return;
12718 }
12719
Aaron Ballmand1f6dcd2017-11-29 23:10:14 +000012720 // Retrieve the argument representing the 'arg_idx'.
Joel E. Denny81508102018-03-13 14:51:22 +000012721 unsigned ArgumentIdxAST = Attr->getArgumentIdx().getASTIndex();
12722 if (ArgumentIdxAST >= ExprArgs.size()) {
Aaron Ballmand1f6dcd2017-11-29 23:10:14 +000012723 Diag(CallSiteLoc, diag::err_tag_index_out_of_range)
Joel E. Denny81508102018-03-13 14:51:22 +000012724 << 1 << Attr->getArgumentIdx().getSourceIndex();
Aaron Ballmand1f6dcd2017-11-29 23:10:14 +000012725 return;
12726 }
Joel E. Denny81508102018-03-13 14:51:22 +000012727 const Expr *ArgumentExpr = ExprArgs[ArgumentIdxAST];
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012728 if (IsPointerAttr) {
12729 // Skip implicit cast of pointer to `void *' (as a function argument).
12730 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr))
Dmitri Gribenko5ac744e2012-11-03 16:07:49 +000012731 if (ICE->getType()->isVoidPointerType() &&
Dmitri Gribenkof21203b2012-11-03 22:10:18 +000012732 ICE->getCastKind() == CK_BitCast)
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012733 ArgumentExpr = ICE->getSubExpr();
12734 }
12735 QualType ArgumentType = ArgumentExpr->getType();
12736
12737 // Passing a `void*' pointer shouldn't trigger a warning.
12738 if (IsPointerAttr && ArgumentType->isVoidPointerType())
12739 return;
12740
12741 if (TypeInfo.MustBeNull) {
12742 // Type tag with matching void type requires a null pointer.
12743 if (!ArgumentExpr->isNullPointerConstant(Context,
12744 Expr::NPC_ValueDependentIsNotNull)) {
12745 Diag(ArgumentExpr->getExprLoc(),
12746 diag::warn_type_safety_null_pointer_required)
12747 << ArgumentKind->getName()
12748 << ArgumentExpr->getSourceRange()
12749 << TypeTagExpr->getSourceRange();
12750 }
12751 return;
12752 }
12753
12754 QualType RequiredType = TypeInfo.Type;
12755 if (IsPointerAttr)
12756 RequiredType = Context.getPointerType(RequiredType);
12757
12758 bool mismatch = false;
12759 if (!TypeInfo.LayoutCompatible) {
12760 mismatch = !Context.hasSameType(ArgumentType, RequiredType);
12761
12762 // C++11 [basic.fundamental] p1:
12763 // Plain char, signed char, and unsigned char are three distinct types.
12764 //
12765 // But we treat plain `char' as equivalent to `signed char' or `unsigned
12766 // char' depending on the current char signedness mode.
12767 if (mismatch)
12768 if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(),
12769 RequiredType->getPointeeType())) ||
12770 (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType)))
12771 mismatch = false;
12772 } else
12773 if (IsPointerAttr)
12774 mismatch = !isLayoutCompatible(Context,
12775 ArgumentType->getPointeeType(),
12776 RequiredType->getPointeeType());
12777 else
12778 mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType);
12779
12780 if (mismatch)
12781 Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch)
Aaron Ballman25dc1e12014-01-03 02:14:08 +000012782 << ArgumentType << ArgumentKind
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +000012783 << TypeInfo.LayoutCompatible << RequiredType
12784 << ArgumentExpr->getSourceRange()
12785 << TypeTagExpr->getSourceRange();
12786}
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012787
12788void Sema::AddPotentialMisalignedMembers(Expr *E, RecordDecl *RD, ValueDecl *MD,
12789 CharUnits Alignment) {
12790 MisalignedMembers.emplace_back(E, RD, MD, Alignment);
12791}
12792
12793void Sema::DiagnoseMisalignedMembers() {
12794 for (MisalignedMember &m : MisalignedMembers) {
Alex Lorenz014181e2016-10-05 09:27:48 +000012795 const NamedDecl *ND = m.RD;
12796 if (ND->getName().empty()) {
12797 if (const TypedefNameDecl *TD = m.RD->getTypedefNameForAnonDecl())
12798 ND = TD;
12799 }
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012800 Diag(m.E->getLocStart(), diag::warn_taking_address_of_packed_member)
Alex Lorenz014181e2016-10-05 09:27:48 +000012801 << m.MD << ND << m.E->getSourceRange();
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012802 }
12803 MisalignedMembers.clear();
12804}
12805
12806void Sema::DiscardMisalignedMemberAddress(const Type *T, Expr *E) {
Roger Ferrer Ibaneze3d80262016-11-14 08:53:27 +000012807 E = E->IgnoreParens();
12808 if (!T->isPointerType() && !T->isIntegerType())
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012809 return;
12810 if (isa<UnaryOperator>(E) &&
12811 cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf) {
12812 auto *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
12813 if (isa<MemberExpr>(Op)) {
12814 auto MA = std::find(MisalignedMembers.begin(), MisalignedMembers.end(),
12815 MisalignedMember(Op));
12816 if (MA != MisalignedMembers.end() &&
Roger Ferrer Ibaneze3d80262016-11-14 08:53:27 +000012817 (T->isIntegerType() ||
Roger Ferrer Ibanezd80d6c52017-12-07 09:23:50 +000012818 (T->isPointerType() && (T->getPointeeType()->isIncompleteType() ||
12819 Context.getTypeAlignInChars(
12820 T->getPointeeType()) <= MA->Alignment))))
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012821 MisalignedMembers.erase(MA);
12822 }
12823 }
12824}
12825
12826void Sema::RefersToMemberWithReducedAlignment(
12827 Expr *E,
Benjamin Kramera8c3e672016-12-12 14:41:19 +000012828 llvm::function_ref<void(Expr *, RecordDecl *, FieldDecl *, CharUnits)>
12829 Action) {
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012830 const auto *ME = dyn_cast<MemberExpr>(E);
Roger Ferrer Ibaneze3d80262016-11-14 08:53:27 +000012831 if (!ME)
12832 return;
12833
Roger Ferrer Ibanez9f963472017-03-13 13:18:21 +000012834 // No need to check expressions with an __unaligned-qualified type.
12835 if (E->getType().getQualifiers().hasUnaligned())
12836 return;
12837
Roger Ferrer Ibaneze3d80262016-11-14 08:53:27 +000012838 // For a chain of MemberExpr like "a.b.c.d" this list
12839 // will keep FieldDecl's like [d, c, b].
12840 SmallVector<FieldDecl *, 4> ReverseMemberChain;
12841 const MemberExpr *TopME = nullptr;
12842 bool AnyIsPacked = false;
12843 do {
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012844 QualType BaseType = ME->getBase()->getType();
12845 if (ME->isArrow())
12846 BaseType = BaseType->getPointeeType();
12847 RecordDecl *RD = BaseType->getAs<RecordType>()->getDecl();
Olivier Goffart67049f02017-07-07 09:38:59 +000012848 if (RD->isInvalidDecl())
12849 return;
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012850
12851 ValueDecl *MD = ME->getMemberDecl();
Roger Ferrer Ibaneze3d80262016-11-14 08:53:27 +000012852 auto *FD = dyn_cast<FieldDecl>(MD);
12853 // We do not care about non-data members.
12854 if (!FD || FD->isInvalidDecl())
12855 return;
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012856
Roger Ferrer Ibaneze3d80262016-11-14 08:53:27 +000012857 AnyIsPacked =
12858 AnyIsPacked || (RD->hasAttr<PackedAttr>() || MD->hasAttr<PackedAttr>());
12859 ReverseMemberChain.push_back(FD);
12860
12861 TopME = ME;
12862 ME = dyn_cast<MemberExpr>(ME->getBase()->IgnoreParens());
12863 } while (ME);
12864 assert(TopME && "We did not compute a topmost MemberExpr!");
12865
12866 // Not the scope of this diagnostic.
12867 if (!AnyIsPacked)
12868 return;
12869
12870 const Expr *TopBase = TopME->getBase()->IgnoreParenImpCasts();
12871 const auto *DRE = dyn_cast<DeclRefExpr>(TopBase);
12872 // TODO: The innermost base of the member expression may be too complicated.
12873 // For now, just disregard these cases. This is left for future
12874 // improvement.
12875 if (!DRE && !isa<CXXThisExpr>(TopBase))
12876 return;
12877
12878 // Alignment expected by the whole expression.
12879 CharUnits ExpectedAlignment = Context.getTypeAlignInChars(E->getType());
12880
12881 // No need to do anything else with this case.
12882 if (ExpectedAlignment.isOne())
12883 return;
12884
12885 // Synthesize offset of the whole access.
12886 CharUnits Offset;
12887 for (auto I = ReverseMemberChain.rbegin(); I != ReverseMemberChain.rend();
12888 I++) {
12889 Offset += Context.toCharUnitsFromBits(Context.getFieldOffset(*I));
12890 }
12891
12892 // Compute the CompleteObjectAlignment as the alignment of the whole chain.
12893 CharUnits CompleteObjectAlignment = Context.getTypeAlignInChars(
12894 ReverseMemberChain.back()->getParent()->getTypeForDecl());
12895
12896 // The base expression of the innermost MemberExpr may give
12897 // stronger guarantees than the class containing the member.
12898 if (DRE && !TopME->isArrow()) {
12899 const ValueDecl *VD = DRE->getDecl();
12900 if (!VD->getType()->isReferenceType())
12901 CompleteObjectAlignment =
12902 std::max(CompleteObjectAlignment, Context.getDeclAlign(VD));
12903 }
12904
12905 // Check if the synthesized offset fulfills the alignment.
12906 if (Offset % ExpectedAlignment != 0 ||
12907 // It may fulfill the offset it but the effective alignment may still be
12908 // lower than the expected expression alignment.
12909 CompleteObjectAlignment < ExpectedAlignment) {
12910 // If this happens, we want to determine a sensible culprit of this.
12911 // Intuitively, watching the chain of member expressions from right to
12912 // left, we start with the required alignment (as required by the field
12913 // type) but some packed attribute in that chain has reduced the alignment.
12914 // It may happen that another packed structure increases it again. But if
12915 // we are here such increase has not been enough. So pointing the first
12916 // FieldDecl that either is packed or else its RecordDecl is,
12917 // seems reasonable.
12918 FieldDecl *FD = nullptr;
12919 CharUnits Alignment;
12920 for (FieldDecl *FDI : ReverseMemberChain) {
12921 if (FDI->hasAttr<PackedAttr>() ||
12922 FDI->getParent()->hasAttr<PackedAttr>()) {
12923 FD = FDI;
12924 Alignment = std::min(
12925 Context.getTypeAlignInChars(FD->getType()),
12926 Context.getTypeAlignInChars(FD->getParent()->getTypeForDecl()));
12927 break;
12928 }
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012929 }
Roger Ferrer Ibaneze3d80262016-11-14 08:53:27 +000012930 assert(FD && "We did not find a packed FieldDecl!");
12931 Action(E, FD->getParent(), FD, Alignment);
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012932 }
12933}
12934
12935void Sema::CheckAddressOfPackedMember(Expr *rhs) {
12936 using namespace std::placeholders;
Eugene Zelenko11a7ef82017-11-15 22:00:04 +000012937
Roger Ferrer Ibanez722a4db2016-08-12 08:04:13 +000012938 RefersToMemberWithReducedAlignment(
12939 rhs, std::bind(&Sema::AddPotentialMisalignedMembers, std::ref(*this), _1,
12940 _2, _3, _4));
12941}