blob: e417949013f87754c7e82eaddc3c15bdec74b684 [file] [log] [blame]
Chris Lattner59907c42007-08-10 20:18:51 +00001//===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner59907c42007-08-10 20:18:51 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This file implements extra semantic analysis beyond what is enforced
Chris Lattner59907c42007-08-10 20:18:51 +000011// by the C type system.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Chris Lattner59907c42007-08-10 20:18:51 +000016#include "clang/AST/ASTContext.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000017#include "clang/AST/CharUnits.h"
John McCall384aff82010-08-25 07:42:41 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/EvaluatedExprVisitor.h"
David Blaikiebe0ee872012-05-15 16:56:36 +000021#include "clang/AST/Expr.h"
Ted Kremenek23245122007-08-20 16:18:38 +000022#include "clang/AST/ExprCXX.h"
Ted Kremenek7ff22b22008-06-16 18:00:42 +000023#include "clang/AST/ExprObjC.h"
Mike Stumpf8c49212010-01-21 03:59:47 +000024#include "clang/AST/StmtCXX.h"
25#include "clang/AST/StmtObjC.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000026#include "clang/Analysis/Analyses/FormatString.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Eric Christopher691ebc32010-04-17 02:26:23 +000028#include "clang/Basic/TargetBuiltins.h"
Nate Begeman26a31422010-06-08 02:47:44 +000029#include "clang/Basic/TargetInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "clang/Lex/Preprocessor.h"
31#include "clang/Sema/Initialization.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000032#include "clang/Sema/Lookup.h"
33#include "clang/Sema/ScopeInfo.h"
34#include "clang/Sema/Sema.h"
35#include "llvm/ADT/BitVector.h"
36#include "llvm/ADT/STLExtras.h"
37#include "llvm/ADT/SmallString.h"
Dmitri Gribenkocb5620c2013-01-30 12:06:08 +000038#include "llvm/Support/ConvertUTF.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000039#include "llvm/Support/raw_ostream.h"
Zhongxing Xua1f3dba2009-05-20 01:55:10 +000040#include <limits>
Chris Lattner59907c42007-08-10 20:18:51 +000041using namespace clang;
John McCall781472f2010-08-25 08:40:02 +000042using namespace sema;
Chris Lattner59907c42007-08-10 20:18:51 +000043
Chris Lattner60800082009-02-18 17:49:48 +000044SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
45 unsigned ByteNo) const {
Chris Lattner08f92e32010-11-17 07:37:15 +000046 return SL->getLocationOfByte(ByteNo, PP.getSourceManager(),
David Blaikie4e4d0842012-03-11 07:00:24 +000047 PP.getLangOpts(), PP.getTargetInfo());
Chris Lattner60800082009-02-18 17:49:48 +000048}
49
John McCall8e10f3b2011-02-26 05:39:39 +000050/// Checks that a call expression's argument count is the desired number.
51/// This is useful when doing custom type-checking. Returns true on error.
52static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
53 unsigned argCount = call->getNumArgs();
54 if (argCount == desiredArgCount) return false;
55
56 if (argCount < desiredArgCount)
57 return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args)
58 << 0 /*function call*/ << desiredArgCount << argCount
59 << call->getSourceRange();
60
61 // Highlight all the excess arguments.
62 SourceRange range(call->getArg(desiredArgCount)->getLocStart(),
63 call->getArg(argCount - 1)->getLocEnd());
64
65 return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
66 << 0 /*function call*/ << desiredArgCount << argCount
67 << call->getArg(1)->getSourceRange();
68}
69
Julien Lerougee5939212012-04-28 17:39:16 +000070/// Check that the first argument to __builtin_annotation is an integer
71/// and the second argument is a non-wide string literal.
72static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
73 if (checkArgCount(S, TheCall, 2))
74 return true;
75
76 // First argument should be an integer.
77 Expr *ValArg = TheCall->getArg(0);
78 QualType Ty = ValArg->getType();
79 if (!Ty->isIntegerType()) {
80 S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg)
81 << ValArg->getSourceRange();
Julien Lerouge77f68bb2011-09-09 22:41:49 +000082 return true;
83 }
Julien Lerougee5939212012-04-28 17:39:16 +000084
85 // Second argument should be a constant string.
86 Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
87 StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
88 if (!Literal || !Literal->isAscii()) {
89 S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg)
90 << StrArg->getSourceRange();
91 return true;
92 }
93
94 TheCall->setType(Ty);
Julien Lerouge77f68bb2011-09-09 22:41:49 +000095 return false;
96}
97
Richard Smith5154dce2013-07-11 02:27:57 +000098/// Check that the argument to __builtin_addressof is a glvalue, and set the
99/// result type to the corresponding pointer type.
100static bool SemaBuiltinAddressof(Sema &S, CallExpr *TheCall) {
101 if (checkArgCount(S, TheCall, 1))
102 return true;
103
104 ExprResult Arg(S.Owned(TheCall->getArg(0)));
105 QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getLocStart());
106 if (ResultType.isNull())
107 return true;
108
109 TheCall->setArg(0, Arg.take());
110 TheCall->setType(ResultType);
111 return false;
112}
113
John McCall60d7b3a2010-08-24 06:29:42 +0000114ExprResult
Anders Carlssond406bf02009-08-16 01:56:34 +0000115Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
John McCall60d7b3a2010-08-24 06:29:42 +0000116 ExprResult TheCallResult(Owned(TheCall));
Douglas Gregor2def4832008-11-17 20:34:05 +0000117
Chris Lattner946928f2010-10-01 23:23:24 +0000118 // Find out if any arguments are required to be integer constant expressions.
119 unsigned ICEArguments = 0;
120 ASTContext::GetBuiltinTypeError Error;
121 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
122 if (Error != ASTContext::GE_None)
123 ICEArguments = 0; // Don't diagnose previously diagnosed errors.
124
125 // If any arguments are required to be ICE's, check and diagnose.
126 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
127 // Skip arguments not required to be ICE's.
128 if ((ICEArguments & (1 << ArgNo)) == 0) continue;
129
130 llvm::APSInt Result;
131 if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
132 return true;
133 ICEArguments &= ~(1 << ArgNo);
134 }
135
Anders Carlssond406bf02009-08-16 01:56:34 +0000136 switch (BuiltinID) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000137 case Builtin::BI__builtin___CFStringMakeConstantString:
Chris Lattner925e60d2007-12-28 05:29:59 +0000138 assert(TheCall->getNumArgs() == 1 &&
Chris Lattner1b9a0792007-12-20 00:26:33 +0000139 "Wrong # arguments to builtin CFStringMakeConstantString");
Chris Lattner69039812009-02-18 06:01:06 +0000140 if (CheckObjCString(TheCall->getArg(0)))
Sebastian Redl0eb23302009-01-19 00:08:26 +0000141 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000142 break;
Ted Kremenek49ff7a12008-07-09 17:58:53 +0000143 case Builtin::BI__builtin_stdarg_start:
Chris Lattner30ce3442007-12-19 23:59:04 +0000144 case Builtin::BI__builtin_va_start:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000145 if (SemaBuiltinVAStart(TheCall))
146 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000147 break;
Chris Lattner1b9a0792007-12-20 00:26:33 +0000148 case Builtin::BI__builtin_isgreater:
149 case Builtin::BI__builtin_isgreaterequal:
150 case Builtin::BI__builtin_isless:
151 case Builtin::BI__builtin_islessequal:
152 case Builtin::BI__builtin_islessgreater:
153 case Builtin::BI__builtin_isunordered:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000154 if (SemaBuiltinUnorderedCompare(TheCall))
155 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000156 break;
Benjamin Kramere771a7a2010-02-15 22:42:31 +0000157 case Builtin::BI__builtin_fpclassify:
158 if (SemaBuiltinFPClassification(TheCall, 6))
159 return ExprError();
160 break;
Eli Friedman9ac6f622009-08-31 20:06:00 +0000161 case Builtin::BI__builtin_isfinite:
162 case Builtin::BI__builtin_isinf:
163 case Builtin::BI__builtin_isinf_sign:
164 case Builtin::BI__builtin_isnan:
165 case Builtin::BI__builtin_isnormal:
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +0000166 if (SemaBuiltinFPClassification(TheCall, 1))
Eli Friedman9ac6f622009-08-31 20:06:00 +0000167 return ExprError();
168 break;
Eli Friedmand38617c2008-05-14 19:38:39 +0000169 case Builtin::BI__builtin_shufflevector:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000170 return SemaBuiltinShuffleVector(TheCall);
171 // TheCall will be freed by the smart pointer here, but that's fine, since
172 // SemaBuiltinShuffleVector guts it, but then doesn't release it.
Daniel Dunbar4493f792008-07-21 22:59:13 +0000173 case Builtin::BI__builtin_prefetch:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000174 if (SemaBuiltinPrefetch(TheCall))
175 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000176 break;
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000177 case Builtin::BI__builtin_object_size:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000178 if (SemaBuiltinObjectSize(TheCall))
179 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000180 break;
Eli Friedmand875fed2009-05-03 04:46:36 +0000181 case Builtin::BI__builtin_longjmp:
182 if (SemaBuiltinLongjmp(TheCall))
183 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000184 break;
John McCall8e10f3b2011-02-26 05:39:39 +0000185
186 case Builtin::BI__builtin_classify_type:
187 if (checkArgCount(*this, TheCall, 1)) return true;
188 TheCall->setType(Context.IntTy);
189 break;
Chris Lattner75c29a02010-10-12 17:47:42 +0000190 case Builtin::BI__builtin_constant_p:
John McCall8e10f3b2011-02-26 05:39:39 +0000191 if (checkArgCount(*this, TheCall, 1)) return true;
192 TheCall->setType(Context.IntTy);
Chris Lattner75c29a02010-10-12 17:47:42 +0000193 break;
Chris Lattner5caa3702009-05-08 06:58:22 +0000194 case Builtin::BI__sync_fetch_and_add:
Douglas Gregora9766412011-11-28 16:30:08 +0000195 case Builtin::BI__sync_fetch_and_add_1:
196 case Builtin::BI__sync_fetch_and_add_2:
197 case Builtin::BI__sync_fetch_and_add_4:
198 case Builtin::BI__sync_fetch_and_add_8:
199 case Builtin::BI__sync_fetch_and_add_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000200 case Builtin::BI__sync_fetch_and_sub:
Douglas Gregora9766412011-11-28 16:30:08 +0000201 case Builtin::BI__sync_fetch_and_sub_1:
202 case Builtin::BI__sync_fetch_and_sub_2:
203 case Builtin::BI__sync_fetch_and_sub_4:
204 case Builtin::BI__sync_fetch_and_sub_8:
205 case Builtin::BI__sync_fetch_and_sub_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000206 case Builtin::BI__sync_fetch_and_or:
Douglas Gregora9766412011-11-28 16:30:08 +0000207 case Builtin::BI__sync_fetch_and_or_1:
208 case Builtin::BI__sync_fetch_and_or_2:
209 case Builtin::BI__sync_fetch_and_or_4:
210 case Builtin::BI__sync_fetch_and_or_8:
211 case Builtin::BI__sync_fetch_and_or_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000212 case Builtin::BI__sync_fetch_and_and:
Douglas Gregora9766412011-11-28 16:30:08 +0000213 case Builtin::BI__sync_fetch_and_and_1:
214 case Builtin::BI__sync_fetch_and_and_2:
215 case Builtin::BI__sync_fetch_and_and_4:
216 case Builtin::BI__sync_fetch_and_and_8:
217 case Builtin::BI__sync_fetch_and_and_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000218 case Builtin::BI__sync_fetch_and_xor:
Douglas Gregora9766412011-11-28 16:30:08 +0000219 case Builtin::BI__sync_fetch_and_xor_1:
220 case Builtin::BI__sync_fetch_and_xor_2:
221 case Builtin::BI__sync_fetch_and_xor_4:
222 case Builtin::BI__sync_fetch_and_xor_8:
223 case Builtin::BI__sync_fetch_and_xor_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000224 case Builtin::BI__sync_add_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000225 case Builtin::BI__sync_add_and_fetch_1:
226 case Builtin::BI__sync_add_and_fetch_2:
227 case Builtin::BI__sync_add_and_fetch_4:
228 case Builtin::BI__sync_add_and_fetch_8:
229 case Builtin::BI__sync_add_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000230 case Builtin::BI__sync_sub_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000231 case Builtin::BI__sync_sub_and_fetch_1:
232 case Builtin::BI__sync_sub_and_fetch_2:
233 case Builtin::BI__sync_sub_and_fetch_4:
234 case Builtin::BI__sync_sub_and_fetch_8:
235 case Builtin::BI__sync_sub_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000236 case Builtin::BI__sync_and_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000237 case Builtin::BI__sync_and_and_fetch_1:
238 case Builtin::BI__sync_and_and_fetch_2:
239 case Builtin::BI__sync_and_and_fetch_4:
240 case Builtin::BI__sync_and_and_fetch_8:
241 case Builtin::BI__sync_and_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000242 case Builtin::BI__sync_or_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000243 case Builtin::BI__sync_or_and_fetch_1:
244 case Builtin::BI__sync_or_and_fetch_2:
245 case Builtin::BI__sync_or_and_fetch_4:
246 case Builtin::BI__sync_or_and_fetch_8:
247 case Builtin::BI__sync_or_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000248 case Builtin::BI__sync_xor_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000249 case Builtin::BI__sync_xor_and_fetch_1:
250 case Builtin::BI__sync_xor_and_fetch_2:
251 case Builtin::BI__sync_xor_and_fetch_4:
252 case Builtin::BI__sync_xor_and_fetch_8:
253 case Builtin::BI__sync_xor_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000254 case Builtin::BI__sync_val_compare_and_swap:
Douglas Gregora9766412011-11-28 16:30:08 +0000255 case Builtin::BI__sync_val_compare_and_swap_1:
256 case Builtin::BI__sync_val_compare_and_swap_2:
257 case Builtin::BI__sync_val_compare_and_swap_4:
258 case Builtin::BI__sync_val_compare_and_swap_8:
259 case Builtin::BI__sync_val_compare_and_swap_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000260 case Builtin::BI__sync_bool_compare_and_swap:
Douglas Gregora9766412011-11-28 16:30:08 +0000261 case Builtin::BI__sync_bool_compare_and_swap_1:
262 case Builtin::BI__sync_bool_compare_and_swap_2:
263 case Builtin::BI__sync_bool_compare_and_swap_4:
264 case Builtin::BI__sync_bool_compare_and_swap_8:
265 case Builtin::BI__sync_bool_compare_and_swap_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000266 case Builtin::BI__sync_lock_test_and_set:
Douglas Gregora9766412011-11-28 16:30:08 +0000267 case Builtin::BI__sync_lock_test_and_set_1:
268 case Builtin::BI__sync_lock_test_and_set_2:
269 case Builtin::BI__sync_lock_test_and_set_4:
270 case Builtin::BI__sync_lock_test_and_set_8:
271 case Builtin::BI__sync_lock_test_and_set_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000272 case Builtin::BI__sync_lock_release:
Douglas Gregora9766412011-11-28 16:30:08 +0000273 case Builtin::BI__sync_lock_release_1:
274 case Builtin::BI__sync_lock_release_2:
275 case Builtin::BI__sync_lock_release_4:
276 case Builtin::BI__sync_lock_release_8:
277 case Builtin::BI__sync_lock_release_16:
Chris Lattner23aa9c82011-04-09 03:57:26 +0000278 case Builtin::BI__sync_swap:
Douglas Gregora9766412011-11-28 16:30:08 +0000279 case Builtin::BI__sync_swap_1:
280 case Builtin::BI__sync_swap_2:
281 case Builtin::BI__sync_swap_4:
282 case Builtin::BI__sync_swap_8:
283 case Builtin::BI__sync_swap_16:
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000284 return SemaBuiltinAtomicOverloaded(TheCallResult);
Richard Smithff34d402012-04-12 05:08:17 +0000285#define BUILTIN(ID, TYPE, ATTRS)
286#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
287 case Builtin::BI##ID: \
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000288 return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);
Richard Smithff34d402012-04-12 05:08:17 +0000289#include "clang/Basic/Builtins.def"
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000290 case Builtin::BI__builtin_annotation:
Julien Lerougee5939212012-04-28 17:39:16 +0000291 if (SemaBuiltinAnnotation(*this, TheCall))
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000292 return ExprError();
293 break;
Richard Smith5154dce2013-07-11 02:27:57 +0000294 case Builtin::BI__builtin_addressof:
295 if (SemaBuiltinAddressof(*this, TheCall))
296 return ExprError();
297 break;
Nate Begeman26a31422010-06-08 02:47:44 +0000298 }
299
300 // Since the target specific builtins for each arch overlap, only check those
301 // of the arch we are compiling for.
302 if (BuiltinID >= Builtin::FirstTSBuiltin) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000303 switch (Context.getTargetInfo().getTriple().getArch()) {
Nate Begeman26a31422010-06-08 02:47:44 +0000304 case llvm::Triple::arm:
305 case llvm::Triple::thumb:
306 if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
307 return ExprError();
308 break;
Simon Atanasyanfad0a322012-07-08 09:30:00 +0000309 case llvm::Triple::mips:
310 case llvm::Triple::mipsel:
311 case llvm::Triple::mips64:
312 case llvm::Triple::mips64el:
313 if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall))
314 return ExprError();
315 break;
Nate Begeman26a31422010-06-08 02:47:44 +0000316 default:
317 break;
318 }
319 }
320
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000321 return TheCallResult;
Nate Begeman26a31422010-06-08 02:47:44 +0000322}
323
Nate Begeman61eecf52010-06-14 05:21:25 +0000324// Get the valid immediate range for the specified NEON type code.
325static unsigned RFT(unsigned t, bool shift = false) {
Bob Wilsonda95f732011-11-08 01:16:11 +0000326 NeonTypeFlags Type(t);
327 int IsQuad = Type.isQuad();
328 switch (Type.getEltType()) {
329 case NeonTypeFlags::Int8:
330 case NeonTypeFlags::Poly8:
331 return shift ? 7 : (8 << IsQuad) - 1;
332 case NeonTypeFlags::Int16:
333 case NeonTypeFlags::Poly16:
334 return shift ? 15 : (4 << IsQuad) - 1;
335 case NeonTypeFlags::Int32:
336 return shift ? 31 : (2 << IsQuad) - 1;
337 case NeonTypeFlags::Int64:
338 return shift ? 63 : (1 << IsQuad) - 1;
339 case NeonTypeFlags::Float16:
340 assert(!shift && "cannot shift float types!");
341 return (4 << IsQuad) - 1;
342 case NeonTypeFlags::Float32:
343 assert(!shift && "cannot shift float types!");
344 return (2 << IsQuad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000345 }
David Blaikie7530c032012-01-17 06:56:22 +0000346 llvm_unreachable("Invalid NeonTypeFlag!");
Nate Begeman61eecf52010-06-14 05:21:25 +0000347}
348
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000349/// getNeonEltType - Return the QualType corresponding to the elements of
350/// the vector type specified by the NeonTypeFlags. This is used to check
351/// the pointer arguments for Neon load/store intrinsics.
352static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) {
353 switch (Flags.getEltType()) {
354 case NeonTypeFlags::Int8:
355 return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy;
356 case NeonTypeFlags::Int16:
357 return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy;
358 case NeonTypeFlags::Int32:
359 return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy;
360 case NeonTypeFlags::Int64:
361 return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy;
362 case NeonTypeFlags::Poly8:
363 return Context.SignedCharTy;
364 case NeonTypeFlags::Poly16:
365 return Context.ShortTy;
366 case NeonTypeFlags::Float16:
367 return Context.UnsignedShortTy;
368 case NeonTypeFlags::Float32:
369 return Context.FloatTy;
370 }
David Blaikie7530c032012-01-17 06:56:22 +0000371 llvm_unreachable("Invalid NeonTypeFlag!");
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000372}
373
Tim Northover09df2b02013-07-16 09:47:53 +0000374bool Sema::CheckARMBuiltinExclusiveCall(unsigned BuiltinID, CallExpr *TheCall) {
375 assert((BuiltinID == ARM::BI__builtin_arm_ldrex ||
376 BuiltinID == ARM::BI__builtin_arm_strex) &&
377 "unexpected ARM builtin");
378 bool IsLdrex = BuiltinID == ARM::BI__builtin_arm_ldrex;
379
380 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
381
382 // Ensure that we have the proper number of arguments.
383 if (checkArgCount(*this, TheCall, IsLdrex ? 1 : 2))
384 return true;
385
386 // Inspect the pointer argument of the atomic builtin. This should always be
387 // a pointer type, whose element is an integral scalar or pointer type.
388 // Because it is a pointer type, we don't have to worry about any implicit
389 // casts here.
390 Expr *PointerArg = TheCall->getArg(IsLdrex ? 0 : 1);
391 ExprResult PointerArgRes = DefaultFunctionArrayLvalueConversion(PointerArg);
392 if (PointerArgRes.isInvalid())
393 return true;
394 PointerArg = PointerArgRes.take();
395
396 const PointerType *pointerType = PointerArg->getType()->getAs<PointerType>();
397 if (!pointerType) {
398 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
399 << PointerArg->getType() << PointerArg->getSourceRange();
400 return true;
401 }
402
403 // ldrex takes a "const volatile T*" and strex takes a "volatile T*". Our next
404 // task is to insert the appropriate casts into the AST. First work out just
405 // what the appropriate type is.
406 QualType ValType = pointerType->getPointeeType();
407 QualType AddrType = ValType.getUnqualifiedType().withVolatile();
408 if (IsLdrex)
409 AddrType.addConst();
410
411 // Issue a warning if the cast is dodgy.
412 CastKind CastNeeded = CK_NoOp;
413 if (!AddrType.isAtLeastAsQualifiedAs(ValType)) {
414 CastNeeded = CK_BitCast;
415 Diag(DRE->getLocStart(), diag::ext_typecheck_convert_discards_qualifiers)
416 << PointerArg->getType()
417 << Context.getPointerType(AddrType)
418 << AA_Passing << PointerArg->getSourceRange();
419 }
420
421 // Finally, do the cast and replace the argument with the corrected version.
422 AddrType = Context.getPointerType(AddrType);
423 PointerArgRes = ImpCastExprToType(PointerArg, AddrType, CastNeeded);
424 if (PointerArgRes.isInvalid())
425 return true;
426 PointerArg = PointerArgRes.take();
427
428 TheCall->setArg(IsLdrex ? 0 : 1, PointerArg);
429
430 // In general, we allow ints, floats and pointers to be loaded and stored.
431 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
432 !ValType->isBlockPointerType() && !ValType->isFloatingType()) {
433 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intfltptr)
434 << PointerArg->getType() << PointerArg->getSourceRange();
435 return true;
436 }
437
438 // But ARM doesn't have instructions to deal with 128-bit versions.
439 if (Context.getTypeSize(ValType) > 64) {
440 Diag(DRE->getLocStart(), diag::err_atomic_exclusive_builtin_pointer_size)
441 << PointerArg->getType() << PointerArg->getSourceRange();
442 return true;
443 }
444
445 switch (ValType.getObjCLifetime()) {
446 case Qualifiers::OCL_None:
447 case Qualifiers::OCL_ExplicitNone:
448 // okay
449 break;
450
451 case Qualifiers::OCL_Weak:
452 case Qualifiers::OCL_Strong:
453 case Qualifiers::OCL_Autoreleasing:
454 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
455 << ValType << PointerArg->getSourceRange();
456 return true;
457 }
458
459
460 if (IsLdrex) {
461 TheCall->setType(ValType);
462 return false;
463 }
464
465 // Initialize the argument to be stored.
466 ExprResult ValArg = TheCall->getArg(0);
467 InitializedEntity Entity = InitializedEntity::InitializeParameter(
468 Context, ValType, /*consume*/ false);
469 ValArg = PerformCopyInitialization(Entity, SourceLocation(), ValArg);
470 if (ValArg.isInvalid())
471 return true;
472
473 TheCall->setArg(0, ValArg.get());
474 return false;
475}
476
Nate Begeman26a31422010-06-08 02:47:44 +0000477bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000478 llvm::APSInt Result;
479
Tim Northover09df2b02013-07-16 09:47:53 +0000480 if (BuiltinID == ARM::BI__builtin_arm_ldrex ||
481 BuiltinID == ARM::BI__builtin_arm_strex) {
482 return CheckARMBuiltinExclusiveCall(BuiltinID, TheCall);
483 }
484
Richard Smithf8ee6bc2012-08-14 01:28:02 +0000485 uint64_t mask = 0;
Nate Begeman61eecf52010-06-14 05:21:25 +0000486 unsigned TV = 0;
Bob Wilson46482552011-11-16 21:32:23 +0000487 int PtrArgNum = -1;
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000488 bool HasConstPtr = false;
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000489 switch (BuiltinID) {
Nate Begemana23326b2010-06-17 04:17:01 +0000490#define GET_NEON_OVERLOAD_CHECK
491#include "clang/Basic/arm_neon.inc"
492#undef GET_NEON_OVERLOAD_CHECK
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000493 }
494
Nate Begeman0d15c532010-06-13 04:47:52 +0000495 // For NEON intrinsics which are overloaded on vector element type, validate
496 // the immediate which specifies which variant to emit.
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000497 unsigned ImmArg = TheCall->getNumArgs()-1;
Nate Begeman0d15c532010-06-13 04:47:52 +0000498 if (mask) {
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000499 if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
Nate Begeman0d15c532010-06-13 04:47:52 +0000500 return true;
501
Bob Wilsonda95f732011-11-08 01:16:11 +0000502 TV = Result.getLimitedValue(64);
Richard Smithf8ee6bc2012-08-14 01:28:02 +0000503 if ((TV > 63) || (mask & (1ULL << TV)) == 0)
Nate Begeman0d15c532010-06-13 04:47:52 +0000504 return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000505 << TheCall->getArg(ImmArg)->getSourceRange();
506 }
507
Bob Wilson46482552011-11-16 21:32:23 +0000508 if (PtrArgNum >= 0) {
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000509 // Check that pointer arguments have the specified type.
Bob Wilson46482552011-11-16 21:32:23 +0000510 Expr *Arg = TheCall->getArg(PtrArgNum);
511 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
512 Arg = ICE->getSubExpr();
513 ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
514 QualType RHSTy = RHS.get()->getType();
515 QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context);
516 if (HasConstPtr)
517 EltTy = EltTy.withConst();
518 QualType LHSTy = Context.getPointerType(EltTy);
519 AssignConvertType ConvTy;
520 ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
521 if (RHS.isInvalid())
522 return true;
523 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy,
524 RHS.get(), AA_Assigning))
525 return true;
Nate Begeman0d15c532010-06-13 04:47:52 +0000526 }
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000527
Nate Begeman0d15c532010-06-13 04:47:52 +0000528 // For NEON intrinsics which take an immediate value as part of the
529 // instruction, range check them here.
Nate Begeman61eecf52010-06-14 05:21:25 +0000530 unsigned i = 0, l = 0, u = 0;
Nate Begeman0d15c532010-06-13 04:47:52 +0000531 switch (BuiltinID) {
532 default: return false;
Nate Begemanbb37f502010-07-29 22:48:34 +0000533 case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break;
534 case ARM::BI__builtin_arm_usat: i = 1; u = 31; break;
Nate Begeman99c40bb2010-08-03 21:32:34 +0000535 case ARM::BI__builtin_arm_vcvtr_f:
536 case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break;
Nate Begemana23326b2010-06-17 04:17:01 +0000537#define GET_NEON_IMMEDIATE_CHECK
538#include "clang/Basic/arm_neon.inc"
539#undef GET_NEON_IMMEDIATE_CHECK
Nate Begeman0d15c532010-06-13 04:47:52 +0000540 };
541
Douglas Gregor592a4232012-06-29 01:05:22 +0000542 // We can't check the value of a dependent argument.
543 if (TheCall->getArg(i)->isTypeDependent() ||
544 TheCall->getArg(i)->isValueDependent())
545 return false;
546
Nate Begeman61eecf52010-06-14 05:21:25 +0000547 // Check that the immediate argument is actually a constant.
Nate Begeman0d15c532010-06-13 04:47:52 +0000548 if (SemaBuiltinConstantArg(TheCall, i, Result))
549 return true;
550
Nate Begeman61eecf52010-06-14 05:21:25 +0000551 // Range check against the upper/lower values for this isntruction.
Nate Begeman0d15c532010-06-13 04:47:52 +0000552 unsigned Val = Result.getZExtValue();
Nate Begeman61eecf52010-06-14 05:21:25 +0000553 if (Val < l || Val > (u + l))
Nate Begeman0d15c532010-06-13 04:47:52 +0000554 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000555 << l << u+l << TheCall->getArg(i)->getSourceRange();
Nate Begeman0d15c532010-06-13 04:47:52 +0000556
Nate Begeman99c40bb2010-08-03 21:32:34 +0000557 // FIXME: VFP Intrinsics should error if VFP not present.
Nate Begeman26a31422010-06-08 02:47:44 +0000558 return false;
Anders Carlssond406bf02009-08-16 01:56:34 +0000559}
Daniel Dunbarde454282008-10-02 18:44:07 +0000560
Simon Atanasyanfad0a322012-07-08 09:30:00 +0000561bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
562 unsigned i = 0, l = 0, u = 0;
563 switch (BuiltinID) {
564 default: return false;
565 case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break;
566 case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break;
Simon Atanasyanbe22cb82012-08-27 12:29:20 +0000567 case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break;
568 case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break;
569 case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break;
570 case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break;
571 case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break;
Simon Atanasyanfad0a322012-07-08 09:30:00 +0000572 };
573
574 // We can't check the value of a dependent argument.
575 if (TheCall->getArg(i)->isTypeDependent() ||
576 TheCall->getArg(i)->isValueDependent())
577 return false;
578
579 // Check that the immediate argument is actually a constant.
580 llvm::APSInt Result;
581 if (SemaBuiltinConstantArg(TheCall, i, Result))
582 return true;
583
584 // Range check against the upper/lower values for this instruction.
585 unsigned Val = Result.getZExtValue();
586 if (Val < l || Val > u)
587 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
588 << l << u << TheCall->getArg(i)->getSourceRange();
589
590 return false;
591}
592
Richard Smith831421f2012-06-25 20:30:08 +0000593/// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo
594/// parameter with the FormatAttr's correct format_idx and firstDataArg.
595/// Returns true when the format fits the function and the FormatStringInfo has
596/// been populated.
597bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember,
598 FormatStringInfo *FSI) {
599 FSI->HasVAListArg = Format->getFirstArg() == 0;
600 FSI->FormatIdx = Format->getFormatIdx() - 1;
601 FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1;
Anders Carlssond406bf02009-08-16 01:56:34 +0000602
Richard Smith831421f2012-06-25 20:30:08 +0000603 // The way the format attribute works in GCC, the implicit this argument
604 // of member functions is counted. However, it doesn't appear in our own
605 // lists, so decrement format_idx in that case.
606 if (IsCXXMember) {
607 if(FSI->FormatIdx == 0)
608 return false;
609 --FSI->FormatIdx;
610 if (FSI->FirstDataArg != 0)
611 --FSI->FirstDataArg;
612 }
613 return true;
614}
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Richard Smith831421f2012-06-25 20:30:08 +0000616/// Handles the checks for format strings, non-POD arguments to vararg
617/// functions, and NULL arguments passed to non-NULL parameters.
Dmitri Gribenko1c030e92013-01-13 20:46:02 +0000618void Sema::checkCall(NamedDecl *FDecl,
619 ArrayRef<const Expr *> Args,
Richard Smith831421f2012-06-25 20:30:08 +0000620 unsigned NumProtoArgs,
621 bool IsMemberFunction,
622 SourceLocation Loc,
623 SourceRange Range,
624 VariadicCallType CallType) {
Jordan Rose66360e22012-10-02 01:49:54 +0000625 if (CurContext->isDependentContext())
626 return;
Daniel Dunbarde454282008-10-02 18:44:07 +0000627
Ted Kremenekc82faca2010-09-09 04:33:05 +0000628 // Printf and scanf checking.
Richard Smith831421f2012-06-25 20:30:08 +0000629 bool HandledFormatString = false;
Richard Trieu0538f0e2013-06-22 00:20:41 +0000630 if (FDecl)
631 for (specific_attr_iterator<FormatAttr>
632 I = FDecl->specific_attr_begin<FormatAttr>(),
633 E = FDecl->specific_attr_end<FormatAttr>(); I != E ; ++I)
634 if (CheckFormatArguments(*I, Args, IsMemberFunction, CallType, Loc,
635 Range))
636 HandledFormatString = true;
Richard Smith831421f2012-06-25 20:30:08 +0000637
638 // Refuse POD arguments that weren't caught by the format string
639 // checks above.
640 if (!HandledFormatString && CallType != VariadicDoesNotApply)
Dmitri Gribenko1c030e92013-01-13 20:46:02 +0000641 for (unsigned ArgIdx = NumProtoArgs; ArgIdx < Args.size(); ++ArgIdx) {
Ted Kremenek0234bfa2012-10-11 19:06:43 +0000642 // Args[ArgIdx] can be null in malformed code.
Dmitri Gribenko1c030e92013-01-13 20:46:02 +0000643 if (const Expr *Arg = Args[ArgIdx])
Ted Kremenek0234bfa2012-10-11 19:06:43 +0000644 variadicArgumentPODCheck(Arg, CallType);
645 }
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Richard Trieu0538f0e2013-06-22 00:20:41 +0000647 if (FDecl) {
648 for (specific_attr_iterator<NonNullAttr>
649 I = FDecl->specific_attr_begin<NonNullAttr>(),
650 E = FDecl->specific_attr_end<NonNullAttr>(); I != E; ++I)
651 CheckNonNullArguments(*I, Args.data(), Loc);
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000652
Richard Trieu0538f0e2013-06-22 00:20:41 +0000653 // Type safety checking.
654 for (specific_attr_iterator<ArgumentWithTypeTagAttr>
655 i = FDecl->specific_attr_begin<ArgumentWithTypeTagAttr>(),
656 e = FDecl->specific_attr_end<ArgumentWithTypeTagAttr>();
657 i != e; ++i) {
658 CheckArgumentWithTypeTag(*i, Args.data());
659 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000660 }
Richard Smith831421f2012-06-25 20:30:08 +0000661}
662
663/// CheckConstructorCall - Check a constructor call for correctness and safety
664/// properties not enforced by the C type system.
Dmitri Gribenko1c030e92013-01-13 20:46:02 +0000665void Sema::CheckConstructorCall(FunctionDecl *FDecl,
666 ArrayRef<const Expr *> Args,
Richard Smith831421f2012-06-25 20:30:08 +0000667 const FunctionProtoType *Proto,
668 SourceLocation Loc) {
669 VariadicCallType CallType =
670 Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply;
Dmitri Gribenko1c030e92013-01-13 20:46:02 +0000671 checkCall(FDecl, Args, Proto->getNumArgs(),
Richard Smith831421f2012-06-25 20:30:08 +0000672 /*IsMemberFunction=*/true, Loc, SourceRange(), CallType);
673}
674
675/// CheckFunctionCall - Check a direct function call for various correctness
676/// and safety properties not strictly enforced by the C type system.
677bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
678 const FunctionProtoType *Proto) {
Eli Friedman2edcde82012-10-11 00:30:58 +0000679 bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) &&
680 isa<CXXMethodDecl>(FDecl);
681 bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) ||
682 IsMemberOperatorCall;
Richard Smith831421f2012-06-25 20:30:08 +0000683 VariadicCallType CallType = getVariadicCallType(FDecl, Proto,
684 TheCall->getCallee());
685 unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0;
Eli Friedman2edcde82012-10-11 00:30:58 +0000686 Expr** Args = TheCall->getArgs();
687 unsigned NumArgs = TheCall->getNumArgs();
Eli Friedmandf75b0c2012-10-11 00:34:15 +0000688 if (IsMemberOperatorCall) {
Eli Friedman2edcde82012-10-11 00:30:58 +0000689 // If this is a call to a member operator, hide the first argument
690 // from checkCall.
691 // FIXME: Our choice of AST representation here is less than ideal.
692 ++Args;
693 --NumArgs;
694 }
Dmitri Gribenko1c030e92013-01-13 20:46:02 +0000695 checkCall(FDecl, llvm::makeArrayRef<const Expr *>(Args, NumArgs),
696 NumProtoArgs,
Richard Smith831421f2012-06-25 20:30:08 +0000697 IsMemberFunction, TheCall->getRParenLoc(),
698 TheCall->getCallee()->getSourceRange(), CallType);
699
700 IdentifierInfo *FnInfo = FDecl->getIdentifier();
701 // None of the checks below are needed for functions that don't have
702 // simple names (e.g., C++ conversion functions).
703 if (!FnInfo)
704 return false;
Sebastian Redl0eb23302009-01-19 00:08:26 +0000705
Anna Zaks0a151a12012-01-17 00:37:07 +0000706 unsigned CMId = FDecl->getMemoryFunctionKind();
707 if (CMId == 0)
Anna Zaksd9b859a2012-01-13 21:52:01 +0000708 return false;
Ted Kremenekbd5da9d2011-08-18 20:55:45 +0000709
Anna Zaksd9b859a2012-01-13 21:52:01 +0000710 // Handle memory setting and copying functions.
Anna Zaks0a151a12012-01-17 00:37:07 +0000711 if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat)
Ted Kremenekbd5da9d2011-08-18 20:55:45 +0000712 CheckStrlcpycatArguments(TheCall, FnInfo);
Anna Zaksc36bedc2012-02-01 19:08:57 +0000713 else if (CMId == Builtin::BIstrncat)
714 CheckStrncatArguments(TheCall, FnInfo);
Anna Zaksd9b859a2012-01-13 21:52:01 +0000715 else
Anna Zaks0a151a12012-01-17 00:37:07 +0000716 CheckMemaccessArguments(TheCall, CMId, FnInfo);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +0000717
Anders Carlssond406bf02009-08-16 01:56:34 +0000718 return false;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000719}
720
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +0000721bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac,
Dmitri Gribenko287f24d2013-05-05 19:42:09 +0000722 ArrayRef<const Expr *> Args) {
Richard Smith831421f2012-06-25 20:30:08 +0000723 VariadicCallType CallType =
724 Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply;
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +0000725
Dmitri Gribenko287f24d2013-05-05 19:42:09 +0000726 checkCall(Method, Args, Method->param_size(),
Richard Smith831421f2012-06-25 20:30:08 +0000727 /*IsMemberFunction=*/false,
728 lbrac, Method->getSourceRange(), CallType);
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +0000729
730 return false;
731}
732
Richard Trieuf462b012013-06-20 21:03:13 +0000733bool Sema::CheckPointerCall(NamedDecl *NDecl, CallExpr *TheCall,
734 const FunctionProtoType *Proto) {
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000735 const VarDecl *V = dyn_cast<VarDecl>(NDecl);
736 if (!V)
Anders Carlssond406bf02009-08-16 01:56:34 +0000737 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000739 QualType Ty = V->getType();
Richard Trieuf462b012013-06-20 21:03:13 +0000740 if (!Ty->isBlockPointerType() && !Ty->isFunctionPointerType())
Anders Carlssond406bf02009-08-16 01:56:34 +0000741 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Richard Trieuf462b012013-06-20 21:03:13 +0000743 VariadicCallType CallType;
Richard Trieua4993772013-06-20 23:21:54 +0000744 if (!Proto || !Proto->isVariadic()) {
Richard Trieuf462b012013-06-20 21:03:13 +0000745 CallType = VariadicDoesNotApply;
746 } else if (Ty->isBlockPointerType()) {
747 CallType = VariadicBlock;
748 } else { // Ty->isFunctionPointerType()
749 CallType = VariadicFunction;
750 }
Richard Smith831421f2012-06-25 20:30:08 +0000751 unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0;
Anders Carlssond406bf02009-08-16 01:56:34 +0000752
Dmitri Gribenko1c030e92013-01-13 20:46:02 +0000753 checkCall(NDecl,
754 llvm::makeArrayRef<const Expr *>(TheCall->getArgs(),
755 TheCall->getNumArgs()),
Richard Smith831421f2012-06-25 20:30:08 +0000756 NumProtoArgs, /*IsMemberFunction=*/false,
757 TheCall->getRParenLoc(),
758 TheCall->getCallee()->getSourceRange(), CallType);
759
Anders Carlssond406bf02009-08-16 01:56:34 +0000760 return false;
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000761}
762
Richard Trieu0538f0e2013-06-22 00:20:41 +0000763/// Checks function calls when a FunctionDecl or a NamedDecl is not available,
764/// such as function pointers returned from functions.
765bool Sema::CheckOtherCall(CallExpr *TheCall, const FunctionProtoType *Proto) {
766 VariadicCallType CallType = getVariadicCallType(/*FDecl=*/0, Proto,
767 TheCall->getCallee());
768 unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0;
769
770 checkCall(/*FDecl=*/0,
771 llvm::makeArrayRef<const Expr *>(TheCall->getArgs(),
772 TheCall->getNumArgs()),
773 NumProtoArgs, /*IsMemberFunction=*/false,
774 TheCall->getRParenLoc(),
775 TheCall->getCallee()->getSourceRange(), CallType);
776
777 return false;
778}
779
Richard Smithff34d402012-04-12 05:08:17 +0000780ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
781 AtomicExpr::AtomicOp Op) {
Eli Friedman276b0612011-10-11 02:20:01 +0000782 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
783 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
Eli Friedman276b0612011-10-11 02:20:01 +0000784
Richard Smithff34d402012-04-12 05:08:17 +0000785 // All these operations take one of the following forms:
786 enum {
787 // C __c11_atomic_init(A *, C)
788 Init,
789 // C __c11_atomic_load(A *, int)
790 Load,
791 // void __atomic_load(A *, CP, int)
792 Copy,
793 // C __c11_atomic_add(A *, M, int)
794 Arithmetic,
795 // C __atomic_exchange_n(A *, CP, int)
796 Xchg,
797 // void __atomic_exchange(A *, C *, CP, int)
798 GNUXchg,
799 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
800 C11CmpXchg,
801 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
802 GNUCmpXchg
803 } Form = Init;
804 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 };
805 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 };
806 // where:
807 // C is an appropriate type,
808 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
809 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
810 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and
811 // the int parameters are for orderings.
Eli Friedman276b0612011-10-11 02:20:01 +0000812
Richard Smithff34d402012-04-12 05:08:17 +0000813 assert(AtomicExpr::AO__c11_atomic_init == 0 &&
814 AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load
815 && "need to update code for modified C11 atomics");
816 bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init &&
817 Op <= AtomicExpr::AO__c11_atomic_fetch_xor;
818 bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
819 Op == AtomicExpr::AO__atomic_store_n ||
820 Op == AtomicExpr::AO__atomic_exchange_n ||
821 Op == AtomicExpr::AO__atomic_compare_exchange_n;
822 bool IsAddSub = false;
823
824 switch (Op) {
825 case AtomicExpr::AO__c11_atomic_init:
826 Form = Init;
827 break;
828
829 case AtomicExpr::AO__c11_atomic_load:
830 case AtomicExpr::AO__atomic_load_n:
831 Form = Load;
832 break;
833
834 case AtomicExpr::AO__c11_atomic_store:
835 case AtomicExpr::AO__atomic_load:
836 case AtomicExpr::AO__atomic_store:
837 case AtomicExpr::AO__atomic_store_n:
838 Form = Copy;
839 break;
840
841 case AtomicExpr::AO__c11_atomic_fetch_add:
842 case AtomicExpr::AO__c11_atomic_fetch_sub:
843 case AtomicExpr::AO__atomic_fetch_add:
844 case AtomicExpr::AO__atomic_fetch_sub:
845 case AtomicExpr::AO__atomic_add_fetch:
846 case AtomicExpr::AO__atomic_sub_fetch:
847 IsAddSub = true;
848 // Fall through.
849 case AtomicExpr::AO__c11_atomic_fetch_and:
850 case AtomicExpr::AO__c11_atomic_fetch_or:
851 case AtomicExpr::AO__c11_atomic_fetch_xor:
852 case AtomicExpr::AO__atomic_fetch_and:
853 case AtomicExpr::AO__atomic_fetch_or:
854 case AtomicExpr::AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +0000855 case AtomicExpr::AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +0000856 case AtomicExpr::AO__atomic_and_fetch:
857 case AtomicExpr::AO__atomic_or_fetch:
858 case AtomicExpr::AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +0000859 case AtomicExpr::AO__atomic_nand_fetch:
Richard Smithff34d402012-04-12 05:08:17 +0000860 Form = Arithmetic;
861 break;
862
863 case AtomicExpr::AO__c11_atomic_exchange:
864 case AtomicExpr::AO__atomic_exchange_n:
865 Form = Xchg;
866 break;
867
868 case AtomicExpr::AO__atomic_exchange:
869 Form = GNUXchg;
870 break;
871
872 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
873 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
874 Form = C11CmpXchg;
875 break;
876
877 case AtomicExpr::AO__atomic_compare_exchange:
878 case AtomicExpr::AO__atomic_compare_exchange_n:
879 Form = GNUCmpXchg;
880 break;
881 }
882
883 // Check we have the right number of arguments.
884 if (TheCall->getNumArgs() < NumArgs[Form]) {
Eli Friedman276b0612011-10-11 02:20:01 +0000885 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Richard Smithff34d402012-04-12 05:08:17 +0000886 << 0 << NumArgs[Form] << TheCall->getNumArgs()
Eli Friedman276b0612011-10-11 02:20:01 +0000887 << TheCall->getCallee()->getSourceRange();
888 return ExprError();
Richard Smithff34d402012-04-12 05:08:17 +0000889 } else if (TheCall->getNumArgs() > NumArgs[Form]) {
890 Diag(TheCall->getArg(NumArgs[Form])->getLocStart(),
Eli Friedman276b0612011-10-11 02:20:01 +0000891 diag::err_typecheck_call_too_many_args)
Richard Smithff34d402012-04-12 05:08:17 +0000892 << 0 << NumArgs[Form] << TheCall->getNumArgs()
Eli Friedman276b0612011-10-11 02:20:01 +0000893 << TheCall->getCallee()->getSourceRange();
894 return ExprError();
895 }
896
Richard Smithff34d402012-04-12 05:08:17 +0000897 // Inspect the first argument of the atomic operation.
Eli Friedmandfa64ba2011-10-14 22:48:56 +0000898 Expr *Ptr = TheCall->getArg(0);
Eli Friedman276b0612011-10-11 02:20:01 +0000899 Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get();
900 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
901 if (!pointerType) {
Richard Smithff34d402012-04-12 05:08:17 +0000902 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
Eli Friedman276b0612011-10-11 02:20:01 +0000903 << Ptr->getType() << Ptr->getSourceRange();
904 return ExprError();
905 }
906
Richard Smithff34d402012-04-12 05:08:17 +0000907 // For a __c11 builtin, this should be a pointer to an _Atomic type.
908 QualType AtomTy = pointerType->getPointeeType(); // 'A'
909 QualType ValType = AtomTy; // 'C'
910 if (IsC11) {
911 if (!AtomTy->isAtomicType()) {
912 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic)
913 << Ptr->getType() << Ptr->getSourceRange();
914 return ExprError();
915 }
Richard Smithbc57b102012-09-15 06:09:58 +0000916 if (AtomTy.isConstQualified()) {
917 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic)
918 << Ptr->getType() << Ptr->getSourceRange();
919 return ExprError();
920 }
Richard Smithff34d402012-04-12 05:08:17 +0000921 ValType = AtomTy->getAs<AtomicType>()->getValueType();
Eli Friedman276b0612011-10-11 02:20:01 +0000922 }
Eli Friedman276b0612011-10-11 02:20:01 +0000923
Richard Smithff34d402012-04-12 05:08:17 +0000924 // For an arithmetic operation, the implied arithmetic must be well-formed.
925 if (Form == Arithmetic) {
926 // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
927 if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) {
928 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
929 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
930 return ExprError();
931 }
932 if (!IsAddSub && !ValType->isIntegerType()) {
933 Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int)
934 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
935 return ExprError();
936 }
937 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
938 // For __atomic_*_n operations, the value type must be a scalar integral or
939 // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
Eli Friedman276b0612011-10-11 02:20:01 +0000940 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
Richard Smithff34d402012-04-12 05:08:17 +0000941 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
942 return ExprError();
943 }
944
945 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) {
946 // For GNU atomics, require a trivially-copyable type. This is not part of
947 // the GNU atomics specification, but we enforce it for sanity.
948 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy)
Eli Friedman276b0612011-10-11 02:20:01 +0000949 << Ptr->getType() << Ptr->getSourceRange();
950 return ExprError();
951 }
952
Richard Smithff34d402012-04-12 05:08:17 +0000953 // FIXME: For any builtin other than a load, the ValType must not be
954 // const-qualified.
Eli Friedman276b0612011-10-11 02:20:01 +0000955
956 switch (ValType.getObjCLifetime()) {
957 case Qualifiers::OCL_None:
958 case Qualifiers::OCL_ExplicitNone:
959 // okay
960 break;
961
962 case Qualifiers::OCL_Weak:
963 case Qualifiers::OCL_Strong:
964 case Qualifiers::OCL_Autoreleasing:
Richard Smithff34d402012-04-12 05:08:17 +0000965 // FIXME: Can this happen? By this point, ValType should be known
966 // to be trivially copyable.
Eli Friedman276b0612011-10-11 02:20:01 +0000967 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
968 << ValType << Ptr->getSourceRange();
969 return ExprError();
970 }
971
972 QualType ResultType = ValType;
Richard Smithff34d402012-04-12 05:08:17 +0000973 if (Form == Copy || Form == GNUXchg || Form == Init)
Eli Friedman276b0612011-10-11 02:20:01 +0000974 ResultType = Context.VoidTy;
Richard Smithff34d402012-04-12 05:08:17 +0000975 else if (Form == C11CmpXchg || Form == GNUCmpXchg)
Eli Friedman276b0612011-10-11 02:20:01 +0000976 ResultType = Context.BoolTy;
977
Richard Smithff34d402012-04-12 05:08:17 +0000978 // The type of a parameter passed 'by value'. In the GNU atomics, such
979 // arguments are actually passed as pointers.
980 QualType ByValType = ValType; // 'CP'
981 if (!IsC11 && !IsN)
982 ByValType = Ptr->getType();
983
Eli Friedman276b0612011-10-11 02:20:01 +0000984 // The first argument --- the pointer --- has a fixed type; we
985 // deduce the types of the rest of the arguments accordingly. Walk
986 // the remaining arguments, converting them to the deduced value type.
Richard Smithff34d402012-04-12 05:08:17 +0000987 for (unsigned i = 1; i != NumArgs[Form]; ++i) {
Eli Friedman276b0612011-10-11 02:20:01 +0000988 QualType Ty;
Richard Smithff34d402012-04-12 05:08:17 +0000989 if (i < NumVals[Form] + 1) {
990 switch (i) {
991 case 1:
992 // The second argument is the non-atomic operand. For arithmetic, this
993 // is always passed by value, and for a compare_exchange it is always
994 // passed by address. For the rest, GNU uses by-address and C11 uses
995 // by-value.
996 assert(Form != Load);
997 if (Form == Init || (Form == Arithmetic && ValType->isIntegerType()))
998 Ty = ValType;
999 else if (Form == Copy || Form == Xchg)
1000 Ty = ByValType;
1001 else if (Form == Arithmetic)
1002 Ty = Context.getPointerDiffType();
1003 else
1004 Ty = Context.getPointerType(ValType.getUnqualifiedType());
1005 break;
1006 case 2:
1007 // The third argument to compare_exchange / GNU exchange is a
1008 // (pointer to a) desired value.
1009 Ty = ByValType;
1010 break;
1011 case 3:
1012 // The fourth argument to GNU compare_exchange is a 'weak' flag.
1013 Ty = Context.BoolTy;
1014 break;
1015 }
Eli Friedman276b0612011-10-11 02:20:01 +00001016 } else {
1017 // The order(s) are always converted to int.
1018 Ty = Context.IntTy;
1019 }
Richard Smithff34d402012-04-12 05:08:17 +00001020
Eli Friedman276b0612011-10-11 02:20:01 +00001021 InitializedEntity Entity =
1022 InitializedEntity::InitializeParameter(Context, Ty, false);
Richard Smithff34d402012-04-12 05:08:17 +00001023 ExprResult Arg = TheCall->getArg(i);
Eli Friedman276b0612011-10-11 02:20:01 +00001024 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
1025 if (Arg.isInvalid())
1026 return true;
1027 TheCall->setArg(i, Arg.get());
1028 }
1029
Richard Smithff34d402012-04-12 05:08:17 +00001030 // Permute the arguments into a 'consistent' order.
Eli Friedmandfa64ba2011-10-14 22:48:56 +00001031 SmallVector<Expr*, 5> SubExprs;
1032 SubExprs.push_back(Ptr);
Richard Smithff34d402012-04-12 05:08:17 +00001033 switch (Form) {
1034 case Init:
1035 // Note, AtomicExpr::getVal1() has a special case for this atomic.
David Chisnall7a7ee302012-01-16 17:27:18 +00001036 SubExprs.push_back(TheCall->getArg(1)); // Val1
Richard Smithff34d402012-04-12 05:08:17 +00001037 break;
1038 case Load:
1039 SubExprs.push_back(TheCall->getArg(1)); // Order
1040 break;
1041 case Copy:
1042 case Arithmetic:
1043 case Xchg:
Eli Friedmandfa64ba2011-10-14 22:48:56 +00001044 SubExprs.push_back(TheCall->getArg(2)); // Order
1045 SubExprs.push_back(TheCall->getArg(1)); // Val1
Richard Smithff34d402012-04-12 05:08:17 +00001046 break;
1047 case GNUXchg:
1048 // Note, AtomicExpr::getVal2() has a special case for this atomic.
1049 SubExprs.push_back(TheCall->getArg(3)); // Order
1050 SubExprs.push_back(TheCall->getArg(1)); // Val1
1051 SubExprs.push_back(TheCall->getArg(2)); // Val2
1052 break;
1053 case C11CmpXchg:
Eli Friedmandfa64ba2011-10-14 22:48:56 +00001054 SubExprs.push_back(TheCall->getArg(3)); // Order
1055 SubExprs.push_back(TheCall->getArg(1)); // Val1
Eli Friedmandfa64ba2011-10-14 22:48:56 +00001056 SubExprs.push_back(TheCall->getArg(4)); // OrderFail
David Chisnall2ebb98a2012-03-29 17:58:59 +00001057 SubExprs.push_back(TheCall->getArg(2)); // Val2
Richard Smithff34d402012-04-12 05:08:17 +00001058 break;
1059 case GNUCmpXchg:
1060 SubExprs.push_back(TheCall->getArg(4)); // Order
1061 SubExprs.push_back(TheCall->getArg(1)); // Val1
1062 SubExprs.push_back(TheCall->getArg(5)); // OrderFail
1063 SubExprs.push_back(TheCall->getArg(2)); // Val2
1064 SubExprs.push_back(TheCall->getArg(3)); // Weak
1065 break;
Eli Friedman276b0612011-10-11 02:20:01 +00001066 }
Fariborz Jahanian538bbe52013-05-28 17:37:39 +00001067
1068 AtomicExpr *AE = new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(),
1069 SubExprs, ResultType, Op,
1070 TheCall->getRParenLoc());
1071
1072 if ((Op == AtomicExpr::AO__c11_atomic_load ||
1073 (Op == AtomicExpr::AO__c11_atomic_store)) &&
1074 Context.AtomicUsesUnsupportedLibcall(AE))
1075 Diag(AE->getLocStart(), diag::err_atomic_load_store_uses_lib) <<
1076 ((Op == AtomicExpr::AO__c11_atomic_load) ? 0 : 1);
Eli Friedmandfa64ba2011-10-14 22:48:56 +00001077
Fariborz Jahanian538bbe52013-05-28 17:37:39 +00001078 return Owned(AE);
Eli Friedman276b0612011-10-11 02:20:01 +00001079}
1080
1081
John McCall5f8d6042011-08-27 01:09:30 +00001082/// checkBuiltinArgument - Given a call to a builtin function, perform
1083/// normal type-checking on the given argument, updating the call in
1084/// place. This is useful when a builtin function requires custom
1085/// type-checking for some of its arguments but not necessarily all of
1086/// them.
1087///
1088/// Returns true on error.
1089static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
1090 FunctionDecl *Fn = E->getDirectCallee();
1091 assert(Fn && "builtin call without direct callee!");
1092
1093 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
1094 InitializedEntity Entity =
1095 InitializedEntity::InitializeParameter(S.Context, Param);
1096
1097 ExprResult Arg = E->getArg(0);
1098 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
1099 if (Arg.isInvalid())
1100 return true;
1101
1102 E->setArg(ArgIndex, Arg.take());
1103 return false;
1104}
1105
Chris Lattner5caa3702009-05-08 06:58:22 +00001106/// SemaBuiltinAtomicOverloaded - We have a call to a function like
1107/// __sync_fetch_and_add, which is an overloaded function based on the pointer
1108/// type of its first argument. The main ActOnCallExpr routines have already
1109/// promoted the types of arguments because all of these calls are prototyped as
1110/// void(...).
1111///
1112/// This function goes through and does final semantic checking for these
1113/// builtins,
John McCall60d7b3a2010-08-24 06:29:42 +00001114ExprResult
1115Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
Chandler Carruthd2014572010-07-09 18:59:35 +00001116 CallExpr *TheCall = (CallExpr *)TheCallResult.get();
Chris Lattner5caa3702009-05-08 06:58:22 +00001117 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1118 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
1119
1120 // Ensure that we have at least one argument to do type inference from.
Chandler Carruthd2014572010-07-09 18:59:35 +00001121 if (TheCall->getNumArgs() < 1) {
1122 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
1123 << 0 << 1 << TheCall->getNumArgs()
1124 << TheCall->getCallee()->getSourceRange();
1125 return ExprError();
1126 }
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Chris Lattner5caa3702009-05-08 06:58:22 +00001128 // Inspect the first argument of the atomic builtin. This should always be
1129 // a pointer type, whose element is an integral scalar or pointer type.
1130 // Because it is a pointer type, we don't have to worry about any implicit
1131 // casts here.
Chandler Carruthd2014572010-07-09 18:59:35 +00001132 // FIXME: We don't allow floating point scalars as input.
Chris Lattner5caa3702009-05-08 06:58:22 +00001133 Expr *FirstArg = TheCall->getArg(0);
Eli Friedman8c382062012-01-23 02:35:22 +00001134 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
1135 if (FirstArgResult.isInvalid())
1136 return ExprError();
1137 FirstArg = FirstArgResult.take();
1138 TheCall->setArg(0, FirstArg);
1139
John McCallf85e1932011-06-15 23:02:42 +00001140 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
1141 if (!pointerType) {
Chandler Carruthd2014572010-07-09 18:59:35 +00001142 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
1143 << FirstArg->getType() << FirstArg->getSourceRange();
1144 return ExprError();
1145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
John McCallf85e1932011-06-15 23:02:42 +00001147 QualType ValType = pointerType->getPointeeType();
Chris Lattnerdd5fa7a2010-09-17 21:12:38 +00001148 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
Chandler Carruthd2014572010-07-09 18:59:35 +00001149 !ValType->isBlockPointerType()) {
1150 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
1151 << FirstArg->getType() << FirstArg->getSourceRange();
1152 return ExprError();
1153 }
Chris Lattner5caa3702009-05-08 06:58:22 +00001154
John McCallf85e1932011-06-15 23:02:42 +00001155 switch (ValType.getObjCLifetime()) {
1156 case Qualifiers::OCL_None:
1157 case Qualifiers::OCL_ExplicitNone:
1158 // okay
1159 break;
1160
1161 case Qualifiers::OCL_Weak:
1162 case Qualifiers::OCL_Strong:
1163 case Qualifiers::OCL_Autoreleasing:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00001164 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
John McCallf85e1932011-06-15 23:02:42 +00001165 << ValType << FirstArg->getSourceRange();
1166 return ExprError();
1167 }
1168
John McCallb45ae252011-10-05 07:41:44 +00001169 // Strip any qualifiers off ValType.
1170 ValType = ValType.getUnqualifiedType();
1171
Chandler Carruth8d13d222010-07-18 20:54:12 +00001172 // The majority of builtins return a value, but a few have special return
1173 // types, so allow them to override appropriately below.
1174 QualType ResultType = ValType;
1175
Chris Lattner5caa3702009-05-08 06:58:22 +00001176 // We need to figure out which concrete builtin this maps onto. For example,
1177 // __sync_fetch_and_add with a 2 byte object turns into
1178 // __sync_fetch_and_add_2.
1179#define BUILTIN_ROW(x) \
1180 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
1181 Builtin::BI##x##_8, Builtin::BI##x##_16 }
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Chris Lattner5caa3702009-05-08 06:58:22 +00001183 static const unsigned BuiltinIndices[][5] = {
1184 BUILTIN_ROW(__sync_fetch_and_add),
1185 BUILTIN_ROW(__sync_fetch_and_sub),
1186 BUILTIN_ROW(__sync_fetch_and_or),
1187 BUILTIN_ROW(__sync_fetch_and_and),
1188 BUILTIN_ROW(__sync_fetch_and_xor),
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Chris Lattner5caa3702009-05-08 06:58:22 +00001190 BUILTIN_ROW(__sync_add_and_fetch),
1191 BUILTIN_ROW(__sync_sub_and_fetch),
1192 BUILTIN_ROW(__sync_and_and_fetch),
1193 BUILTIN_ROW(__sync_or_and_fetch),
1194 BUILTIN_ROW(__sync_xor_and_fetch),
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Chris Lattner5caa3702009-05-08 06:58:22 +00001196 BUILTIN_ROW(__sync_val_compare_and_swap),
1197 BUILTIN_ROW(__sync_bool_compare_and_swap),
1198 BUILTIN_ROW(__sync_lock_test_and_set),
Chris Lattner23aa9c82011-04-09 03:57:26 +00001199 BUILTIN_ROW(__sync_lock_release),
1200 BUILTIN_ROW(__sync_swap)
Chris Lattner5caa3702009-05-08 06:58:22 +00001201 };
Mike Stump1eb44332009-09-09 15:08:12 +00001202#undef BUILTIN_ROW
1203
Chris Lattner5caa3702009-05-08 06:58:22 +00001204 // Determine the index of the size.
1205 unsigned SizeIndex;
Ken Dyck199c3d62010-01-11 17:06:35 +00001206 switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
Chris Lattner5caa3702009-05-08 06:58:22 +00001207 case 1: SizeIndex = 0; break;
1208 case 2: SizeIndex = 1; break;
1209 case 4: SizeIndex = 2; break;
1210 case 8: SizeIndex = 3; break;
1211 case 16: SizeIndex = 4; break;
1212 default:
Chandler Carruthd2014572010-07-09 18:59:35 +00001213 Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
1214 << FirstArg->getType() << FirstArg->getSourceRange();
1215 return ExprError();
Chris Lattner5caa3702009-05-08 06:58:22 +00001216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Chris Lattner5caa3702009-05-08 06:58:22 +00001218 // Each of these builtins has one pointer argument, followed by some number of
1219 // values (0, 1 or 2) followed by a potentially empty varags list of stuff
1220 // that we ignore. Find out which row of BuiltinIndices to read from as well
1221 // as the number of fixed args.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001222 unsigned BuiltinID = FDecl->getBuiltinID();
Chris Lattner5caa3702009-05-08 06:58:22 +00001223 unsigned BuiltinIndex, NumFixed = 1;
1224 switch (BuiltinID) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001225 default: llvm_unreachable("Unknown overloaded atomic builtin!");
Douglas Gregora9766412011-11-28 16:30:08 +00001226 case Builtin::BI__sync_fetch_and_add:
1227 case Builtin::BI__sync_fetch_and_add_1:
1228 case Builtin::BI__sync_fetch_and_add_2:
1229 case Builtin::BI__sync_fetch_and_add_4:
1230 case Builtin::BI__sync_fetch_and_add_8:
1231 case Builtin::BI__sync_fetch_and_add_16:
1232 BuiltinIndex = 0;
1233 break;
1234
1235 case Builtin::BI__sync_fetch_and_sub:
1236 case Builtin::BI__sync_fetch_and_sub_1:
1237 case Builtin::BI__sync_fetch_and_sub_2:
1238 case Builtin::BI__sync_fetch_and_sub_4:
1239 case Builtin::BI__sync_fetch_and_sub_8:
1240 case Builtin::BI__sync_fetch_and_sub_16:
1241 BuiltinIndex = 1;
1242 break;
1243
1244 case Builtin::BI__sync_fetch_and_or:
1245 case Builtin::BI__sync_fetch_and_or_1:
1246 case Builtin::BI__sync_fetch_and_or_2:
1247 case Builtin::BI__sync_fetch_and_or_4:
1248 case Builtin::BI__sync_fetch_and_or_8:
1249 case Builtin::BI__sync_fetch_and_or_16:
1250 BuiltinIndex = 2;
1251 break;
1252
1253 case Builtin::BI__sync_fetch_and_and:
1254 case Builtin::BI__sync_fetch_and_and_1:
1255 case Builtin::BI__sync_fetch_and_and_2:
1256 case Builtin::BI__sync_fetch_and_and_4:
1257 case Builtin::BI__sync_fetch_and_and_8:
1258 case Builtin::BI__sync_fetch_and_and_16:
1259 BuiltinIndex = 3;
1260 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Douglas Gregora9766412011-11-28 16:30:08 +00001262 case Builtin::BI__sync_fetch_and_xor:
1263 case Builtin::BI__sync_fetch_and_xor_1:
1264 case Builtin::BI__sync_fetch_and_xor_2:
1265 case Builtin::BI__sync_fetch_and_xor_4:
1266 case Builtin::BI__sync_fetch_and_xor_8:
1267 case Builtin::BI__sync_fetch_and_xor_16:
1268 BuiltinIndex = 4;
1269 break;
1270
1271 case Builtin::BI__sync_add_and_fetch:
1272 case Builtin::BI__sync_add_and_fetch_1:
1273 case Builtin::BI__sync_add_and_fetch_2:
1274 case Builtin::BI__sync_add_and_fetch_4:
1275 case Builtin::BI__sync_add_and_fetch_8:
1276 case Builtin::BI__sync_add_and_fetch_16:
1277 BuiltinIndex = 5;
1278 break;
1279
1280 case Builtin::BI__sync_sub_and_fetch:
1281 case Builtin::BI__sync_sub_and_fetch_1:
1282 case Builtin::BI__sync_sub_and_fetch_2:
1283 case Builtin::BI__sync_sub_and_fetch_4:
1284 case Builtin::BI__sync_sub_and_fetch_8:
1285 case Builtin::BI__sync_sub_and_fetch_16:
1286 BuiltinIndex = 6;
1287 break;
1288
1289 case Builtin::BI__sync_and_and_fetch:
1290 case Builtin::BI__sync_and_and_fetch_1:
1291 case Builtin::BI__sync_and_and_fetch_2:
1292 case Builtin::BI__sync_and_and_fetch_4:
1293 case Builtin::BI__sync_and_and_fetch_8:
1294 case Builtin::BI__sync_and_and_fetch_16:
1295 BuiltinIndex = 7;
1296 break;
1297
1298 case Builtin::BI__sync_or_and_fetch:
1299 case Builtin::BI__sync_or_and_fetch_1:
1300 case Builtin::BI__sync_or_and_fetch_2:
1301 case Builtin::BI__sync_or_and_fetch_4:
1302 case Builtin::BI__sync_or_and_fetch_8:
1303 case Builtin::BI__sync_or_and_fetch_16:
1304 BuiltinIndex = 8;
1305 break;
1306
1307 case Builtin::BI__sync_xor_and_fetch:
1308 case Builtin::BI__sync_xor_and_fetch_1:
1309 case Builtin::BI__sync_xor_and_fetch_2:
1310 case Builtin::BI__sync_xor_and_fetch_4:
1311 case Builtin::BI__sync_xor_and_fetch_8:
1312 case Builtin::BI__sync_xor_and_fetch_16:
1313 BuiltinIndex = 9;
1314 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Chris Lattner5caa3702009-05-08 06:58:22 +00001316 case Builtin::BI__sync_val_compare_and_swap:
Douglas Gregora9766412011-11-28 16:30:08 +00001317 case Builtin::BI__sync_val_compare_and_swap_1:
1318 case Builtin::BI__sync_val_compare_and_swap_2:
1319 case Builtin::BI__sync_val_compare_and_swap_4:
1320 case Builtin::BI__sync_val_compare_and_swap_8:
1321 case Builtin::BI__sync_val_compare_and_swap_16:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +00001322 BuiltinIndex = 10;
Chris Lattner5caa3702009-05-08 06:58:22 +00001323 NumFixed = 2;
1324 break;
Douglas Gregora9766412011-11-28 16:30:08 +00001325
Chris Lattner5caa3702009-05-08 06:58:22 +00001326 case Builtin::BI__sync_bool_compare_and_swap:
Douglas Gregora9766412011-11-28 16:30:08 +00001327 case Builtin::BI__sync_bool_compare_and_swap_1:
1328 case Builtin::BI__sync_bool_compare_and_swap_2:
1329 case Builtin::BI__sync_bool_compare_and_swap_4:
1330 case Builtin::BI__sync_bool_compare_and_swap_8:
1331 case Builtin::BI__sync_bool_compare_and_swap_16:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +00001332 BuiltinIndex = 11;
Chris Lattner5caa3702009-05-08 06:58:22 +00001333 NumFixed = 2;
Chandler Carruth8d13d222010-07-18 20:54:12 +00001334 ResultType = Context.BoolTy;
Chris Lattner5caa3702009-05-08 06:58:22 +00001335 break;
Douglas Gregora9766412011-11-28 16:30:08 +00001336
1337 case Builtin::BI__sync_lock_test_and_set:
1338 case Builtin::BI__sync_lock_test_and_set_1:
1339 case Builtin::BI__sync_lock_test_and_set_2:
1340 case Builtin::BI__sync_lock_test_and_set_4:
1341 case Builtin::BI__sync_lock_test_and_set_8:
1342 case Builtin::BI__sync_lock_test_and_set_16:
1343 BuiltinIndex = 12;
1344 break;
1345
Chris Lattner5caa3702009-05-08 06:58:22 +00001346 case Builtin::BI__sync_lock_release:
Douglas Gregora9766412011-11-28 16:30:08 +00001347 case Builtin::BI__sync_lock_release_1:
1348 case Builtin::BI__sync_lock_release_2:
1349 case Builtin::BI__sync_lock_release_4:
1350 case Builtin::BI__sync_lock_release_8:
1351 case Builtin::BI__sync_lock_release_16:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +00001352 BuiltinIndex = 13;
Chris Lattner5caa3702009-05-08 06:58:22 +00001353 NumFixed = 0;
Chandler Carruth8d13d222010-07-18 20:54:12 +00001354 ResultType = Context.VoidTy;
Chris Lattner5caa3702009-05-08 06:58:22 +00001355 break;
Douglas Gregora9766412011-11-28 16:30:08 +00001356
1357 case Builtin::BI__sync_swap:
1358 case Builtin::BI__sync_swap_1:
1359 case Builtin::BI__sync_swap_2:
1360 case Builtin::BI__sync_swap_4:
1361 case Builtin::BI__sync_swap_8:
1362 case Builtin::BI__sync_swap_16:
1363 BuiltinIndex = 14;
1364 break;
Chris Lattner5caa3702009-05-08 06:58:22 +00001365 }
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Chris Lattner5caa3702009-05-08 06:58:22 +00001367 // Now that we know how many fixed arguments we expect, first check that we
1368 // have at least that many.
Chandler Carruthd2014572010-07-09 18:59:35 +00001369 if (TheCall->getNumArgs() < 1+NumFixed) {
1370 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
1371 << 0 << 1+NumFixed << TheCall->getNumArgs()
1372 << TheCall->getCallee()->getSourceRange();
1373 return ExprError();
1374 }
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Chris Lattnere7ac0a92009-05-08 15:36:58 +00001376 // Get the decl for the concrete builtin from this, we can tell what the
1377 // concrete integer type we should convert to is.
1378 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
1379 const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID);
Abramo Bagnara2ad11cd2012-09-22 09:05:22 +00001380 FunctionDecl *NewBuiltinDecl;
1381 if (NewBuiltinID == BuiltinID)
1382 NewBuiltinDecl = FDecl;
1383 else {
1384 // Perform builtin lookup to avoid redeclaring it.
1385 DeclarationName DN(&Context.Idents.get(NewBuiltinName));
1386 LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName);
1387 LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true);
1388 assert(Res.getFoundDecl());
1389 NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
1390 if (NewBuiltinDecl == 0)
1391 return ExprError();
1392 }
Chandler Carruthd2014572010-07-09 18:59:35 +00001393
John McCallf871d0c2010-08-07 06:22:56 +00001394 // The first argument --- the pointer --- has a fixed type; we
1395 // deduce the types of the rest of the arguments accordingly. Walk
1396 // the remaining arguments, converting them to the deduced value type.
Chris Lattner5caa3702009-05-08 06:58:22 +00001397 for (unsigned i = 0; i != NumFixed; ++i) {
John Wiegley429bb272011-04-08 18:41:53 +00001398 ExprResult Arg = TheCall->getArg(i+1);
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Chris Lattner5caa3702009-05-08 06:58:22 +00001400 // GCC does an implicit conversion to the pointer or integer ValType. This
1401 // can fail in some cases (1i -> int**), check for this error case now.
John McCallb45ae252011-10-05 07:41:44 +00001402 // Initialize the argument.
1403 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
1404 ValType, /*consume*/ false);
1405 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
John Wiegley429bb272011-04-08 18:41:53 +00001406 if (Arg.isInvalid())
Chandler Carruthd2014572010-07-09 18:59:35 +00001407 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Chris Lattner5caa3702009-05-08 06:58:22 +00001409 // Okay, we have something that *can* be converted to the right type. Check
1410 // to see if there is a potentially weird extension going on here. This can
1411 // happen when you do an atomic operation on something like an char* and
1412 // pass in 42. The 42 gets converted to char. This is even more strange
1413 // for things like 45.123 -> char, etc.
Mike Stump1eb44332009-09-09 15:08:12 +00001414 // FIXME: Do this check.
John McCallb45ae252011-10-05 07:41:44 +00001415 TheCall->setArg(i+1, Arg.take());
Chris Lattner5caa3702009-05-08 06:58:22 +00001416 }
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +00001418 ASTContext& Context = this->getASTContext();
1419
1420 // Create a new DeclRefExpr to refer to the new decl.
1421 DeclRefExpr* NewDRE = DeclRefExpr::Create(
1422 Context,
1423 DRE->getQualifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001424 SourceLocation(),
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +00001425 NewBuiltinDecl,
John McCallf4b88a42012-03-10 09:33:50 +00001426 /*enclosing*/ false,
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +00001427 DRE->getLocation(),
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001428 Context.BuiltinFnTy,
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +00001429 DRE->getValueKind());
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Chris Lattner5caa3702009-05-08 06:58:22 +00001431 // Set the callee in the CallExpr.
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001432 // FIXME: This loses syntactic information.
1433 QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType());
1434 ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy,
1435 CK_BuiltinFnToFnPtr);
John Wiegley429bb272011-04-08 18:41:53 +00001436 TheCall->setCallee(PromotedCall.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Chandler Carruthdb4325b2010-07-18 07:23:17 +00001438 // Change the result type of the call to match the original value type. This
1439 // is arbitrary, but the codegen for these builtins ins design to handle it
1440 // gracefully.
Chandler Carruth8d13d222010-07-18 20:54:12 +00001441 TheCall->setType(ResultType);
Chandler Carruthd2014572010-07-09 18:59:35 +00001442
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001443 return TheCallResult;
Chris Lattner5caa3702009-05-08 06:58:22 +00001444}
1445
Chris Lattner69039812009-02-18 06:01:06 +00001446/// CheckObjCString - Checks that the argument to the builtin
Anders Carlsson71993dd2007-08-17 05:31:46 +00001447/// CFString constructor is correct
Steve Narofffd942622009-04-13 20:26:29 +00001448/// Note: It might also make sense to do the UTF-16 conversion here (would
1449/// simplify the backend).
Chris Lattner69039812009-02-18 06:01:06 +00001450bool Sema::CheckObjCString(Expr *Arg) {
Chris Lattner56f34942008-02-13 01:02:39 +00001451 Arg = Arg->IgnoreParenCasts();
Anders Carlsson71993dd2007-08-17 05:31:46 +00001452 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
1453
Douglas Gregor5cee1192011-07-27 05:40:30 +00001454 if (!Literal || !Literal->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001455 Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
1456 << Arg->getSourceRange();
Anders Carlsson9cdc4d32007-08-17 15:44:17 +00001457 return true;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001458 }
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Fariborz Jahanian7da71022010-09-07 19:38:13 +00001460 if (Literal->containsNonAsciiOrNull()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001461 StringRef String = Literal->getString();
Fariborz Jahanian7da71022010-09-07 19:38:13 +00001462 unsigned NumBytes = String.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001463 SmallVector<UTF16, 128> ToBuf(NumBytes);
Roman Divacky31ba6132012-09-06 15:59:27 +00001464 const UTF8 *FromPtr = (const UTF8 *)String.data();
Fariborz Jahanian7da71022010-09-07 19:38:13 +00001465 UTF16 *ToPtr = &ToBuf[0];
1466
1467 ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
1468 &ToPtr, ToPtr + NumBytes,
1469 strictConversion);
1470 // Check for conversion failure.
1471 if (Result != conversionOK)
1472 Diag(Arg->getLocStart(),
1473 diag::warn_cfstring_truncated) << Arg->getSourceRange();
1474 }
Anders Carlsson9cdc4d32007-08-17 15:44:17 +00001475 return false;
Chris Lattner59907c42007-08-10 20:18:51 +00001476}
1477
Chris Lattnerc27c6652007-12-20 00:05:45 +00001478/// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
1479/// Emit an error and return true on failure, return false on success.
Chris Lattner925e60d2007-12-28 05:29:59 +00001480bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
1481 Expr *Fn = TheCall->getCallee();
1482 if (TheCall->getNumArgs() > 2) {
Chris Lattner2c21a072008-11-21 18:44:24 +00001483 Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001484 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +00001485 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1486 << Fn->getSourceRange()
Mike Stump1eb44332009-09-09 15:08:12 +00001487 << SourceRange(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001488 (*(TheCall->arg_end()-1))->getLocEnd());
Chris Lattner30ce3442007-12-19 23:59:04 +00001489 return true;
1490 }
Eli Friedman56f20ae2008-12-15 22:05:35 +00001491
1492 if (TheCall->getNumArgs() < 2) {
Eric Christopherd77b9a22010-04-16 04:48:22 +00001493 return Diag(TheCall->getLocEnd(),
1494 diag::err_typecheck_call_too_few_args_at_least)
1495 << 0 /*function call*/ << 2 << TheCall->getNumArgs();
Eli Friedman56f20ae2008-12-15 22:05:35 +00001496 }
1497
John McCall5f8d6042011-08-27 01:09:30 +00001498 // Type-check the first argument normally.
1499 if (checkBuiltinArgument(*this, TheCall, 0))
1500 return true;
1501
Chris Lattnerc27c6652007-12-20 00:05:45 +00001502 // Determine whether the current function is variadic or not.
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001503 BlockScopeInfo *CurBlock = getCurBlock();
Chris Lattnerc27c6652007-12-20 00:05:45 +00001504 bool isVariadic;
Steve Naroffcd9c5142009-04-15 19:33:47 +00001505 if (CurBlock)
John McCallc71a4912010-06-04 19:02:56 +00001506 isVariadic = CurBlock->TheDecl->isVariadic();
Ted Kremenek9498d382010-04-29 16:49:01 +00001507 else if (FunctionDecl *FD = getCurFunctionDecl())
1508 isVariadic = FD->isVariadic();
1509 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001510 isVariadic = getCurMethodDecl()->isVariadic();
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Chris Lattnerc27c6652007-12-20 00:05:45 +00001512 if (!isVariadic) {
Chris Lattner30ce3442007-12-19 23:59:04 +00001513 Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
1514 return true;
1515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Chris Lattner30ce3442007-12-19 23:59:04 +00001517 // Verify that the second argument to the builtin is the last argument of the
1518 // current function or method.
1519 bool SecondArgIsLastNamedArgument = false;
Anders Carlssone2c14102008-02-13 01:22:59 +00001520 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Nico Weberb07d4482013-05-24 23:31:57 +00001522 // These are valid if SecondArgIsLastNamedArgument is false after the next
1523 // block.
1524 QualType Type;
1525 SourceLocation ParamLoc;
1526
Anders Carlsson88cf2262008-02-11 04:20:54 +00001527 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
1528 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
Chris Lattner30ce3442007-12-19 23:59:04 +00001529 // FIXME: This isn't correct for methods (results in bogus warning).
1530 // Get the last formal in the current function.
Anders Carlsson88cf2262008-02-11 04:20:54 +00001531 const ParmVarDecl *LastArg;
Steve Naroffcd9c5142009-04-15 19:33:47 +00001532 if (CurBlock)
1533 LastArg = *(CurBlock->TheDecl->param_end()-1);
1534 else if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner371f2582008-12-04 23:50:19 +00001535 LastArg = *(FD->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +00001536 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001537 LastArg = *(getCurMethodDecl()->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +00001538 SecondArgIsLastNamedArgument = PV == LastArg;
Nico Weberb07d4482013-05-24 23:31:57 +00001539
1540 Type = PV->getType();
1541 ParamLoc = PV->getLocation();
Chris Lattner30ce3442007-12-19 23:59:04 +00001542 }
1543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Chris Lattner30ce3442007-12-19 23:59:04 +00001545 if (!SecondArgIsLastNamedArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001546 Diag(TheCall->getArg(1)->getLocStart(),
Chris Lattner30ce3442007-12-19 23:59:04 +00001547 diag::warn_second_parameter_of_va_start_not_last_named_argument);
Nico Weberb07d4482013-05-24 23:31:57 +00001548 else if (Type->isReferenceType()) {
1549 Diag(Arg->getLocStart(),
1550 diag::warn_va_start_of_reference_type_is_undefined);
1551 Diag(ParamLoc, diag::note_parameter_type) << Type;
1552 }
1553
Chris Lattner30ce3442007-12-19 23:59:04 +00001554 return false;
Eli Friedman6cfda232008-05-20 08:23:37 +00001555}
Chris Lattner30ce3442007-12-19 23:59:04 +00001556
Chris Lattner1b9a0792007-12-20 00:26:33 +00001557/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
1558/// friends. This is declared to take (...), so we have to check everything.
Chris Lattner925e60d2007-12-28 05:29:59 +00001559bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
1560 if (TheCall->getNumArgs() < 2)
Chris Lattner2c21a072008-11-21 18:44:24 +00001561 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Eric Christopherd77b9a22010-04-16 04:48:22 +00001562 << 0 << 2 << TheCall->getNumArgs()/*function call*/;
Chris Lattner925e60d2007-12-28 05:29:59 +00001563 if (TheCall->getNumArgs() > 2)
Mike Stump1eb44332009-09-09 15:08:12 +00001564 return Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001565 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +00001566 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001567 << SourceRange(TheCall->getArg(2)->getLocStart(),
1568 (*(TheCall->arg_end()-1))->getLocEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001569
John Wiegley429bb272011-04-08 18:41:53 +00001570 ExprResult OrigArg0 = TheCall->getArg(0);
1571 ExprResult OrigArg1 = TheCall->getArg(1);
Douglas Gregorcde01732009-05-19 22:10:17 +00001572
Chris Lattner1b9a0792007-12-20 00:26:33 +00001573 // Do standard promotions between the two arguments, returning their common
1574 // type.
Chris Lattner925e60d2007-12-28 05:29:59 +00001575 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
John Wiegley429bb272011-04-08 18:41:53 +00001576 if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
1577 return true;
Daniel Dunbar403bc2b2009-02-19 19:28:43 +00001578
1579 // Make sure any conversions are pushed back into the call; this is
1580 // type safe since unordered compare builtins are declared as "_Bool
1581 // foo(...)".
John Wiegley429bb272011-04-08 18:41:53 +00001582 TheCall->setArg(0, OrigArg0.get());
1583 TheCall->setArg(1, OrigArg1.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001584
John Wiegley429bb272011-04-08 18:41:53 +00001585 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
Douglas Gregorcde01732009-05-19 22:10:17 +00001586 return false;
1587
Chris Lattner1b9a0792007-12-20 00:26:33 +00001588 // If the common type isn't a real floating type, then the arguments were
1589 // invalid for this operation.
Eli Friedman860a3192012-06-16 02:19:17 +00001590 if (Res.isNull() || !Res->isRealFloatingType())
John Wiegley429bb272011-04-08 18:41:53 +00001591 return Diag(OrigArg0.get()->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001592 diag::err_typecheck_call_invalid_ordered_compare)
John Wiegley429bb272011-04-08 18:41:53 +00001593 << OrigArg0.get()->getType() << OrigArg1.get()->getType()
1594 << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Chris Lattner1b9a0792007-12-20 00:26:33 +00001596 return false;
1597}
1598
Benjamin Kramere771a7a2010-02-15 22:42:31 +00001599/// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
1600/// __builtin_isnan and friends. This is declared to take (...), so we have
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +00001601/// to check everything. We expect the last argument to be a floating point
1602/// value.
1603bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
1604 if (TheCall->getNumArgs() < NumArgs)
Eli Friedman9ac6f622009-08-31 20:06:00 +00001605 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Eric Christopherd77b9a22010-04-16 04:48:22 +00001606 << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +00001607 if (TheCall->getNumArgs() > NumArgs)
1608 return Diag(TheCall->getArg(NumArgs)->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +00001609 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +00001610 << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +00001611 << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +00001612 (*(TheCall->arg_end()-1))->getLocEnd());
1613
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +00001614 Expr *OrigArg = TheCall->getArg(NumArgs-1);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Eli Friedman9ac6f622009-08-31 20:06:00 +00001616 if (OrigArg->isTypeDependent())
1617 return false;
1618
Chris Lattner81368fb2010-05-06 05:50:07 +00001619 // This operation requires a non-_Complex floating-point number.
Eli Friedman9ac6f622009-08-31 20:06:00 +00001620 if (!OrigArg->getType()->isRealFloatingType())
Mike Stump1eb44332009-09-09 15:08:12 +00001621 return Diag(OrigArg->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +00001622 diag::err_typecheck_call_invalid_unary_fp)
1623 << OrigArg->getType() << OrigArg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Chris Lattner81368fb2010-05-06 05:50:07 +00001625 // If this is an implicit conversion from float -> double, remove it.
1626 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
1627 Expr *CastArg = Cast->getSubExpr();
1628 if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
1629 assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) &&
1630 "promotion from float to double is the only expected cast here");
1631 Cast->setSubExpr(0);
Chris Lattner81368fb2010-05-06 05:50:07 +00001632 TheCall->setArg(NumArgs-1, CastArg);
Chris Lattner81368fb2010-05-06 05:50:07 +00001633 }
1634 }
1635
Eli Friedman9ac6f622009-08-31 20:06:00 +00001636 return false;
1637}
1638
Eli Friedmand38617c2008-05-14 19:38:39 +00001639/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
1640// This is declared to take (...), so we have to check everything.
John McCall60d7b3a2010-08-24 06:29:42 +00001641ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
Nate Begeman37b6a572010-06-08 00:16:34 +00001642 if (TheCall->getNumArgs() < 2)
Sebastian Redl0eb23302009-01-19 00:08:26 +00001643 return ExprError(Diag(TheCall->getLocEnd(),
Eric Christopherd77b9a22010-04-16 04:48:22 +00001644 diag::err_typecheck_call_too_few_args_at_least)
Craig Topperb44545a2013-07-28 21:50:10 +00001645 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1646 << TheCall->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +00001647
Nate Begeman37b6a572010-06-08 00:16:34 +00001648 // Determine which of the following types of shufflevector we're checking:
1649 // 1) unary, vector mask: (lhs, mask)
1650 // 2) binary, vector mask: (lhs, rhs, mask)
1651 // 3) binary, scalar mask: (lhs, rhs, index, ..., index)
1652 QualType resType = TheCall->getArg(0)->getType();
1653 unsigned numElements = 0;
Craig Toppere3fbbe92013-07-19 04:46:31 +00001654
Douglas Gregorcde01732009-05-19 22:10:17 +00001655 if (!TheCall->getArg(0)->isTypeDependent() &&
1656 !TheCall->getArg(1)->isTypeDependent()) {
Nate Begeman37b6a572010-06-08 00:16:34 +00001657 QualType LHSType = TheCall->getArg(0)->getType();
1658 QualType RHSType = TheCall->getArg(1)->getType();
Craig Toppere3fbbe92013-07-19 04:46:31 +00001659
Craig Topperbbe759c2013-07-29 06:47:04 +00001660 if (!LHSType->isVectorType() || !RHSType->isVectorType())
1661 return ExprError(Diag(TheCall->getLocStart(),
1662 diag::err_shufflevector_non_vector)
1663 << SourceRange(TheCall->getArg(0)->getLocStart(),
1664 TheCall->getArg(1)->getLocEnd()));
Craig Toppere3fbbe92013-07-19 04:46:31 +00001665
Nate Begeman37b6a572010-06-08 00:16:34 +00001666 numElements = LHSType->getAs<VectorType>()->getNumElements();
1667 unsigned numResElements = TheCall->getNumArgs() - 2;
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Nate Begeman37b6a572010-06-08 00:16:34 +00001669 // Check to see if we have a call with 2 vector arguments, the unary shuffle
1670 // with mask. If so, verify that RHS is an integer vector type with the
1671 // same number of elts as lhs.
1672 if (TheCall->getNumArgs() == 2) {
Sylvestre Ledru4cb3d902013-07-06 08:00:09 +00001673 if (!RHSType->hasIntegerRepresentation() ||
Nate Begeman37b6a572010-06-08 00:16:34 +00001674 RHSType->getAs<VectorType>()->getNumElements() != numElements)
Craig Topperbbe759c2013-07-29 06:47:04 +00001675 return ExprError(Diag(TheCall->getLocStart(),
1676 diag::err_shufflevector_incompatible_vector)
1677 << SourceRange(TheCall->getArg(1)->getLocStart(),
1678 TheCall->getArg(1)->getLocEnd()));
Craig Toppere3fbbe92013-07-19 04:46:31 +00001679 } else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
Craig Topperbbe759c2013-07-29 06:47:04 +00001680 return ExprError(Diag(TheCall->getLocStart(),
1681 diag::err_shufflevector_incompatible_vector)
1682 << SourceRange(TheCall->getArg(0)->getLocStart(),
1683 TheCall->getArg(1)->getLocEnd()));
Nate Begeman37b6a572010-06-08 00:16:34 +00001684 } else if (numElements != numResElements) {
1685 QualType eltType = LHSType->getAs<VectorType>()->getElementType();
Chris Lattner788b0fd2010-06-23 06:00:24 +00001686 resType = Context.getVectorType(eltType, numResElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00001687 VectorType::GenericVector);
Douglas Gregorcde01732009-05-19 22:10:17 +00001688 }
Eli Friedmand38617c2008-05-14 19:38:39 +00001689 }
1690
1691 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
Douglas Gregorcde01732009-05-19 22:10:17 +00001692 if (TheCall->getArg(i)->isTypeDependent() ||
1693 TheCall->getArg(i)->isValueDependent())
1694 continue;
1695
Nate Begeman37b6a572010-06-08 00:16:34 +00001696 llvm::APSInt Result(32);
1697 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
1698 return ExprError(Diag(TheCall->getLocStart(),
Craig Topperb44545a2013-07-28 21:50:10 +00001699 diag::err_shufflevector_nonconstant_argument)
1700 << TheCall->getArg(i)->getSourceRange());
Sebastian Redl0eb23302009-01-19 00:08:26 +00001701
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +00001702 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
Sebastian Redl0eb23302009-01-19 00:08:26 +00001703 return ExprError(Diag(TheCall->getLocStart(),
Craig Topperb44545a2013-07-28 21:50:10 +00001704 diag::err_shufflevector_argument_too_large)
1705 << TheCall->getArg(i)->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +00001706 }
1707
Chris Lattner5f9e2722011-07-23 10:55:15 +00001708 SmallVector<Expr*, 32> exprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001709
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +00001710 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
Eli Friedmand38617c2008-05-14 19:38:39 +00001711 exprs.push_back(TheCall->getArg(i));
1712 TheCall->setArg(i, 0);
1713 }
1714
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001715 return Owned(new (Context) ShuffleVectorExpr(Context, exprs, resType,
Ted Kremenek8189cde2009-02-07 01:47:29 +00001716 TheCall->getCallee()->getLocStart(),
1717 TheCall->getRParenLoc()));
Eli Friedmand38617c2008-05-14 19:38:39 +00001718}
Chris Lattner30ce3442007-12-19 23:59:04 +00001719
Daniel Dunbar4493f792008-07-21 22:59:13 +00001720/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
1721// This is declared to take (const void*, ...) and can take two
1722// optional constant int args.
1723bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001724 unsigned NumArgs = TheCall->getNumArgs();
Daniel Dunbar4493f792008-07-21 22:59:13 +00001725
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001726 if (NumArgs > 3)
Eric Christopherccfa9632010-04-16 04:56:46 +00001727 return Diag(TheCall->getLocEnd(),
1728 diag::err_typecheck_call_too_many_args_at_most)
1729 << 0 /*function call*/ << 3 << NumArgs
1730 << TheCall->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +00001731
1732 // Argument 0 is checked for us and the remaining arguments must be
1733 // constant integers.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001734 for (unsigned i = 1; i != NumArgs; ++i) {
Daniel Dunbar4493f792008-07-21 22:59:13 +00001735 Expr *Arg = TheCall->getArg(i);
Douglas Gregor592a4232012-06-29 01:05:22 +00001736
1737 // We can't check the value of a dependent argument.
1738 if (Arg->isTypeDependent() || Arg->isValueDependent())
1739 continue;
1740
Eli Friedman9aef7262009-12-04 00:30:06 +00001741 llvm::APSInt Result;
Eric Christopher691ebc32010-04-17 02:26:23 +00001742 if (SemaBuiltinConstantArg(TheCall, i, Result))
1743 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Daniel Dunbar4493f792008-07-21 22:59:13 +00001745 // FIXME: gcc issues a warning and rewrites these to 0. These
1746 // seems especially odd for the third argument since the default
1747 // is 3.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001748 if (i == 1) {
Eli Friedman9aef7262009-12-04 00:30:06 +00001749 if (Result.getLimitedValue() > 1)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001750 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Chris Lattner21fb98e2009-09-23 06:06:36 +00001751 << "0" << "1" << Arg->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +00001752 } else {
Eli Friedman9aef7262009-12-04 00:30:06 +00001753 if (Result.getLimitedValue() > 3)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001754 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Chris Lattner21fb98e2009-09-23 06:06:36 +00001755 << "0" << "3" << Arg->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +00001756 }
1757 }
1758
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001759 return false;
Daniel Dunbar4493f792008-07-21 22:59:13 +00001760}
1761
Eric Christopher691ebc32010-04-17 02:26:23 +00001762/// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
1763/// TheCall is a constant expression.
1764bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
1765 llvm::APSInt &Result) {
1766 Expr *Arg = TheCall->getArg(ArgNum);
1767 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1768 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
1769
1770 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
1771
1772 if (!Arg->isIntegerConstantExpr(Result, Context))
1773 return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
Eric Christopher5e896552010-04-19 18:23:02 +00001774 << FDecl->getDeclName() << Arg->getSourceRange();
Eric Christopher691ebc32010-04-17 02:26:23 +00001775
Chris Lattner21fb98e2009-09-23 06:06:36 +00001776 return false;
1777}
1778
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001779/// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
1780/// int type). This simply type checks that type is one of the defined
1781/// constants (0-3).
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001782// For compatibility check 0-3, llvm only handles 0 and 2.
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001783bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
Eric Christopher691ebc32010-04-17 02:26:23 +00001784 llvm::APSInt Result;
Douglas Gregor592a4232012-06-29 01:05:22 +00001785
1786 // We can't check the value of a dependent argument.
1787 if (TheCall->getArg(1)->isTypeDependent() ||
1788 TheCall->getArg(1)->isValueDependent())
1789 return false;
1790
Eric Christopher691ebc32010-04-17 02:26:23 +00001791 // Check constant-ness first.
1792 if (SemaBuiltinConstantArg(TheCall, 1, Result))
1793 return true;
1794
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001795 Expr *Arg = TheCall->getArg(1);
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001796 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001797 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
1798 << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001799 }
1800
1801 return false;
1802}
1803
Eli Friedman586d6a82009-05-03 06:04:26 +00001804/// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
Eli Friedmand875fed2009-05-03 04:46:36 +00001805/// This checks that val is a constant 1.
1806bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
1807 Expr *Arg = TheCall->getArg(1);
Eric Christopher691ebc32010-04-17 02:26:23 +00001808 llvm::APSInt Result;
Douglas Gregorcde01732009-05-19 22:10:17 +00001809
Eric Christopher691ebc32010-04-17 02:26:23 +00001810 // TODO: This is less than ideal. Overload this to take a value.
1811 if (SemaBuiltinConstantArg(TheCall, 1, Result))
1812 return true;
1813
1814 if (Result != 1)
Eli Friedmand875fed2009-05-03 04:46:36 +00001815 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
1816 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
1817
1818 return false;
1819}
1820
Richard Smith831421f2012-06-25 20:30:08 +00001821// Determine if an expression is a string literal or constant string.
1822// If this function returns false on the arguments to a function expecting a
1823// format string, we will usually need to emit a warning.
1824// True string literals are then checked by CheckFormatString.
1825Sema::StringLiteralCheckType
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00001826Sema::checkFormatStringExpr(const Expr *E, ArrayRef<const Expr *> Args,
1827 bool HasVAListArg,
Richard Smith831421f2012-06-25 20:30:08 +00001828 unsigned format_idx, unsigned firstDataArg,
Jordan Roseddcfbc92012-07-19 18:10:23 +00001829 FormatStringType Type, VariadicCallType CallType,
1830 bool inFunctionCall) {
Ted Kremenek4fe64412010-09-09 03:51:39 +00001831 tryAgain:
Douglas Gregorcde01732009-05-19 22:10:17 +00001832 if (E->isTypeDependent() || E->isValueDependent())
Richard Smith831421f2012-06-25 20:30:08 +00001833 return SLCT_NotALiteral;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001834
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001835 E = E->IgnoreParenCasts();
Peter Collingbournef111d932011-04-15 00:35:48 +00001836
David Blaikiea73cdcb2012-02-10 21:07:25 +00001837 if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
1838 // Technically -Wformat-nonliteral does not warn about this case.
1839 // The behavior of printf and friends in this case is implementation
1840 // dependent. Ideally if the format string cannot be null then
1841 // it should have a 'nonnull' attribute in the function prototype.
Richard Smith831421f2012-06-25 20:30:08 +00001842 return SLCT_CheckedLiteral;
David Blaikiea73cdcb2012-02-10 21:07:25 +00001843
Ted Kremenekd30ef872009-01-12 23:09:09 +00001844 switch (E->getStmtClass()) {
John McCall56ca35d2011-02-17 10:25:35 +00001845 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenekd30ef872009-01-12 23:09:09 +00001846 case Stmt::ConditionalOperatorClass: {
Richard Smith831421f2012-06-25 20:30:08 +00001847 // The expression is a literal if both sub-expressions were, and it was
1848 // completely checked only if both sub-expressions were checked.
1849 const AbstractConditionalOperator *C =
1850 cast<AbstractConditionalOperator>(E);
1851 StringLiteralCheckType Left =
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00001852 checkFormatStringExpr(C->getTrueExpr(), Args,
Richard Smith831421f2012-06-25 20:30:08 +00001853 HasVAListArg, format_idx, firstDataArg,
Jordan Roseddcfbc92012-07-19 18:10:23 +00001854 Type, CallType, inFunctionCall);
Richard Smith831421f2012-06-25 20:30:08 +00001855 if (Left == SLCT_NotALiteral)
1856 return SLCT_NotALiteral;
1857 StringLiteralCheckType Right =
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00001858 checkFormatStringExpr(C->getFalseExpr(), Args,
Richard Smith831421f2012-06-25 20:30:08 +00001859 HasVAListArg, format_idx, firstDataArg,
Jordan Roseddcfbc92012-07-19 18:10:23 +00001860 Type, CallType, inFunctionCall);
Richard Smith831421f2012-06-25 20:30:08 +00001861 return Left < Right ? Left : Right;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001862 }
1863
1864 case Stmt::ImplicitCastExprClass: {
Ted Kremenek4fe64412010-09-09 03:51:39 +00001865 E = cast<ImplicitCastExpr>(E)->getSubExpr();
1866 goto tryAgain;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001867 }
1868
John McCall56ca35d2011-02-17 10:25:35 +00001869 case Stmt::OpaqueValueExprClass:
1870 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
1871 E = src;
1872 goto tryAgain;
1873 }
Richard Smith831421f2012-06-25 20:30:08 +00001874 return SLCT_NotALiteral;
John McCall56ca35d2011-02-17 10:25:35 +00001875
Ted Kremenekb43e8ad2011-02-24 23:03:04 +00001876 case Stmt::PredefinedExprClass:
1877 // While __func__, etc., are technically not string literals, they
1878 // cannot contain format specifiers and thus are not a security
1879 // liability.
Richard Smith831421f2012-06-25 20:30:08 +00001880 return SLCT_UncheckedLiteral;
Ted Kremenekb43e8ad2011-02-24 23:03:04 +00001881
Ted Kremenek082d9362009-03-20 21:35:28 +00001882 case Stmt::DeclRefExprClass: {
1883 const DeclRefExpr *DR = cast<DeclRefExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Ted Kremenek082d9362009-03-20 21:35:28 +00001885 // As an exception, do not flag errors for variables binding to
1886 // const string literals.
1887 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1888 bool isConstant = false;
1889 QualType T = DR->getType();
Ted Kremenekd30ef872009-01-12 23:09:09 +00001890
Ted Kremenek082d9362009-03-20 21:35:28 +00001891 if (const ArrayType *AT = Context.getAsArrayType(T)) {
1892 isConstant = AT->getElementType().isConstant(Context);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001893 } else if (const PointerType *PT = T->getAs<PointerType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001894 isConstant = T.isConstant(Context) &&
Ted Kremenek082d9362009-03-20 21:35:28 +00001895 PT->getPointeeType().isConstant(Context);
Jean-Daniel Dupase98e5b52012-01-25 10:35:33 +00001896 } else if (T->isObjCObjectPointerType()) {
1897 // In ObjC, there is usually no "const ObjectPointer" type,
1898 // so don't check if the pointee type is constant.
1899 isConstant = T.isConstant(Context);
Ted Kremenek082d9362009-03-20 21:35:28 +00001900 }
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Ted Kremenek082d9362009-03-20 21:35:28 +00001902 if (isConstant) {
Matt Beaumont-Gaye2c60662012-05-11 22:10:59 +00001903 if (const Expr *Init = VD->getAnyInitializer()) {
1904 // Look through initializers like const char c[] = { "foo" }
1905 if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) {
1906 if (InitList->isStringLiteralInit())
1907 Init = InitList->getInit(0)->IgnoreParenImpCasts();
1908 }
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00001909 return checkFormatStringExpr(Init, Args,
Richard Smith831421f2012-06-25 20:30:08 +00001910 HasVAListArg, format_idx,
Jordan Roseddcfbc92012-07-19 18:10:23 +00001911 firstDataArg, Type, CallType,
Richard Smith831421f2012-06-25 20:30:08 +00001912 /*inFunctionCall*/false);
Matt Beaumont-Gaye2c60662012-05-11 22:10:59 +00001913 }
Ted Kremenek082d9362009-03-20 21:35:28 +00001914 }
Mike Stump1eb44332009-09-09 15:08:12 +00001915
Anders Carlssond966a552009-06-28 19:55:58 +00001916 // For vprintf* functions (i.e., HasVAListArg==true), we add a
1917 // special check to see if the format string is a function parameter
1918 // of the function calling the printf function. If the function
1919 // has an attribute indicating it is a printf-like function, then we
1920 // should suppress warnings concerning non-literals being used in a call
1921 // to a vprintf function. For example:
1922 //
1923 // void
1924 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
1925 // va_list ap;
1926 // va_start(ap, fmt);
1927 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt".
1928 // ...
1929 //
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +00001930 if (HasVAListArg) {
1931 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) {
1932 if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) {
1933 int PVIndex = PV->getFunctionScopeIndex() + 1;
1934 for (specific_attr_iterator<FormatAttr>
1935 i = ND->specific_attr_begin<FormatAttr>(),
1936 e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) {
1937 FormatAttr *PVFormat = *i;
1938 // adjust for implicit parameter
1939 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1940 if (MD->isInstance())
1941 ++PVIndex;
1942 // We also check if the formats are compatible.
1943 // We can't pass a 'scanf' string to a 'printf' function.
1944 if (PVIndex == PVFormat->getFormatIdx() &&
1945 Type == GetFormatStringType(PVFormat))
Richard Smith831421f2012-06-25 20:30:08 +00001946 return SLCT_UncheckedLiteral;
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +00001947 }
1948 }
1949 }
1950 }
Ted Kremenek082d9362009-03-20 21:35:28 +00001951 }
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Richard Smith831421f2012-06-25 20:30:08 +00001953 return SLCT_NotALiteral;
Ted Kremenek082d9362009-03-20 21:35:28 +00001954 }
Ted Kremenekd30ef872009-01-12 23:09:09 +00001955
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +00001956 case Stmt::CallExprClass:
1957 case Stmt::CXXMemberCallExprClass: {
Anders Carlsson8f031b32009-06-27 04:05:33 +00001958 const CallExpr *CE = cast<CallExpr>(E);
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +00001959 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
1960 if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
1961 unsigned ArgIndex = FA->getFormatIdx();
1962 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1963 if (MD->isInstance())
1964 --ArgIndex;
1965 const Expr *Arg = CE->getArg(ArgIndex - 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00001967 return checkFormatStringExpr(Arg, Args,
Richard Smith831421f2012-06-25 20:30:08 +00001968 HasVAListArg, format_idx, firstDataArg,
Jordan Roseddcfbc92012-07-19 18:10:23 +00001969 Type, CallType, inFunctionCall);
Jordan Rose50687312012-06-04 23:52:23 +00001970 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1971 unsigned BuiltinID = FD->getBuiltinID();
1972 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
1973 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) {
1974 const Expr *Arg = CE->getArg(0);
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00001975 return checkFormatStringExpr(Arg, Args,
Richard Smith831421f2012-06-25 20:30:08 +00001976 HasVAListArg, format_idx,
Jordan Roseddcfbc92012-07-19 18:10:23 +00001977 firstDataArg, Type, CallType,
1978 inFunctionCall);
Jordan Rose50687312012-06-04 23:52:23 +00001979 }
Anders Carlsson8f031b32009-06-27 04:05:33 +00001980 }
1981 }
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Richard Smith831421f2012-06-25 20:30:08 +00001983 return SLCT_NotALiteral;
Anders Carlsson8f031b32009-06-27 04:05:33 +00001984 }
Ted Kremenek082d9362009-03-20 21:35:28 +00001985 case Stmt::ObjCStringLiteralClass:
1986 case Stmt::StringLiteralClass: {
1987 const StringLiteral *StrE = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Ted Kremenek082d9362009-03-20 21:35:28 +00001989 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
Ted Kremenekd30ef872009-01-12 23:09:09 +00001990 StrE = ObjCFExpr->getString();
1991 else
Ted Kremenek082d9362009-03-20 21:35:28 +00001992 StrE = cast<StringLiteral>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Ted Kremenekd30ef872009-01-12 23:09:09 +00001994 if (StrE) {
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00001995 CheckFormatString(StrE, E, Args, HasVAListArg, format_idx,
Jordan Roseddcfbc92012-07-19 18:10:23 +00001996 firstDataArg, Type, inFunctionCall, CallType);
Richard Smith831421f2012-06-25 20:30:08 +00001997 return SLCT_CheckedLiteral;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001998 }
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Richard Smith831421f2012-06-25 20:30:08 +00002000 return SLCT_NotALiteral;
Ted Kremenekd30ef872009-01-12 23:09:09 +00002001 }
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Ted Kremenek082d9362009-03-20 21:35:28 +00002003 default:
Richard Smith831421f2012-06-25 20:30:08 +00002004 return SLCT_NotALiteral;
Ted Kremenekd30ef872009-01-12 23:09:09 +00002005 }
2006}
2007
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00002008void
Mike Stump1eb44332009-09-09 15:08:12 +00002009Sema::CheckNonNullArguments(const NonNullAttr *NonNull,
Nick Lewycky909a70d2011-03-25 01:44:32 +00002010 const Expr * const *ExprArgs,
2011 SourceLocation CallSiteLoc) {
Sean Huntcf807c42010-08-18 23:23:40 +00002012 for (NonNullAttr::args_iterator i = NonNull->args_begin(),
2013 e = NonNull->args_end();
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00002014 i != e; ++i) {
Nick Lewycky909a70d2011-03-25 01:44:32 +00002015 const Expr *ArgExpr = ExprArgs[*i];
Nick Lewycky3edf3872013-01-23 05:08:29 +00002016
2017 // As a special case, transparent unions initialized with zero are
2018 // considered null for the purposes of the nonnull attribute.
2019 if (const RecordType *UT = ArgExpr->getType()->getAsUnionType()) {
2020 if (UT->getDecl()->hasAttr<TransparentUnionAttr>())
2021 if (const CompoundLiteralExpr *CLE =
2022 dyn_cast<CompoundLiteralExpr>(ArgExpr))
2023 if (const InitListExpr *ILE =
2024 dyn_cast<InitListExpr>(CLE->getInitializer()))
2025 ArgExpr = ILE->getInit(0);
2026 }
2027
2028 bool Result;
2029 if (ArgExpr->EvaluateAsBooleanCondition(Result, Context) && !Result)
Nick Lewycky909a70d2011-03-25 01:44:32 +00002030 Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00002031 }
2032}
Ted Kremenekd30ef872009-01-12 23:09:09 +00002033
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00002034Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
2035 return llvm::StringSwitch<FormatStringType>(Format->getType())
2036 .Case("scanf", FST_Scanf)
2037 .Cases("printf", "printf0", FST_Printf)
2038 .Cases("NSString", "CFString", FST_NSString)
2039 .Case("strftime", FST_Strftime)
2040 .Case("strfmon", FST_Strfmon)
2041 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
2042 .Default(FST_Unknown);
2043}
2044
Jordan Roseddcfbc92012-07-19 18:10:23 +00002045/// CheckFormatArguments - Check calls to printf and scanf (and similar
Ted Kremenek826a3452010-07-16 02:11:22 +00002046/// functions) for correct use of format strings.
Richard Smith831421f2012-06-25 20:30:08 +00002047/// Returns true if a format string has been fully checked.
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002048bool Sema::CheckFormatArguments(const FormatAttr *Format,
2049 ArrayRef<const Expr *> Args,
2050 bool IsCXXMember,
Jordan Roseddcfbc92012-07-19 18:10:23 +00002051 VariadicCallType CallType,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002052 SourceLocation Loc, SourceRange Range) {
Richard Smith831421f2012-06-25 20:30:08 +00002053 FormatStringInfo FSI;
2054 if (getFormatStringInfo(Format, IsCXXMember, &FSI))
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002055 return CheckFormatArguments(Args, FSI.HasVAListArg, FSI.FormatIdx,
Richard Smith831421f2012-06-25 20:30:08 +00002056 FSI.FirstDataArg, GetFormatStringType(Format),
Jordan Roseddcfbc92012-07-19 18:10:23 +00002057 CallType, Loc, Range);
Richard Smith831421f2012-06-25 20:30:08 +00002058 return false;
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002059}
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002060
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002061bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00002062 bool HasVAListArg, unsigned format_idx,
2063 unsigned firstDataArg, FormatStringType Type,
Jordan Roseddcfbc92012-07-19 18:10:23 +00002064 VariadicCallType CallType,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00002065 SourceLocation Loc, SourceRange Range) {
Ted Kremenek826a3452010-07-16 02:11:22 +00002066 // CHECK: printf/scanf-like function is called with no format string.
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002067 if (format_idx >= Args.size()) {
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002068 Diag(Loc, diag::warn_missing_format_string) << Range;
Richard Smith831421f2012-06-25 20:30:08 +00002069 return false;
Ted Kremenek71895b92007-08-14 17:39:48 +00002070 }
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002072 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Chris Lattner59907c42007-08-10 20:18:51 +00002074 // CHECK: format string is not a string literal.
Mike Stump1eb44332009-09-09 15:08:12 +00002075 //
Ted Kremenek71895b92007-08-14 17:39:48 +00002076 // Dynamically generated format strings are difficult to
2077 // automatically vet at compile time. Requiring that format strings
2078 // are string literals: (1) permits the checking of format strings by
2079 // the compiler and thereby (2) can practically remove the source of
2080 // many format string exploits.
Ted Kremenek7ff22b22008-06-16 18:00:42 +00002081
Mike Stump1eb44332009-09-09 15:08:12 +00002082 // Format string can be either ObjC string (e.g. @"%d") or
Ted Kremenek7ff22b22008-06-16 18:00:42 +00002083 // C string (e.g. "%d")
Mike Stump1eb44332009-09-09 15:08:12 +00002084 // ObjC string uses the same format specifiers as C string, so we can use
Ted Kremenek7ff22b22008-06-16 18:00:42 +00002085 // the same format string checking logic for both ObjC and C strings.
Richard Smith831421f2012-06-25 20:30:08 +00002086 StringLiteralCheckType CT =
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002087 checkFormatStringExpr(OrigFormatExpr, Args, HasVAListArg,
Jordan Roseddcfbc92012-07-19 18:10:23 +00002088 format_idx, firstDataArg, Type, CallType);
Richard Smith831421f2012-06-25 20:30:08 +00002089 if (CT != SLCT_NotALiteral)
2090 // Literal format string found, check done!
2091 return CT == SLCT_CheckedLiteral;
Ted Kremenek7ff22b22008-06-16 18:00:42 +00002092
Jean-Daniel Dupas2837a2f2012-02-07 23:10:53 +00002093 // Strftime is particular as it always uses a single 'time' argument,
2094 // so it is safe to pass a non-literal string.
2095 if (Type == FST_Strftime)
Richard Smith831421f2012-06-25 20:30:08 +00002096 return false;
Jean-Daniel Dupas2837a2f2012-02-07 23:10:53 +00002097
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +00002098 // Do not emit diag when the string param is a macro expansion and the
2099 // format is either NSString or CFString. This is a hack to prevent
2100 // diag when using the NSLocalizedString and CFCopyLocalizedString macros
2101 // which are usually used in place of NS and CF string literals.
Jean-Daniel Dupasdc170202012-05-04 21:08:08 +00002102 if (Type == FST_NSString &&
2103 SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart()))
Richard Smith831421f2012-06-25 20:30:08 +00002104 return false;
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +00002105
Chris Lattner655f1412009-04-29 04:59:47 +00002106 // If there are no arguments specified, warn with -Wformat-security, otherwise
2107 // warn only with -Wformat-nonliteral.
Eli Friedman2243e782013-06-18 18:10:01 +00002108 if (Args.size() == firstDataArg)
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002109 Diag(Args[format_idx]->getLocStart(),
Ted Kremenek826a3452010-07-16 02:11:22 +00002110 diag::warn_format_nonliteral_noargs)
Chris Lattner655f1412009-04-29 04:59:47 +00002111 << OrigFormatExpr->getSourceRange();
2112 else
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002113 Diag(Args[format_idx]->getLocStart(),
Ted Kremenek826a3452010-07-16 02:11:22 +00002114 diag::warn_format_nonliteral)
Chris Lattner655f1412009-04-29 04:59:47 +00002115 << OrigFormatExpr->getSourceRange();
Richard Smith831421f2012-06-25 20:30:08 +00002116 return false;
Ted Kremenekd30ef872009-01-12 23:09:09 +00002117}
Ted Kremenek71895b92007-08-14 17:39:48 +00002118
Ted Kremeneke0e53132010-01-28 23:39:18 +00002119namespace {
Ted Kremenek826a3452010-07-16 02:11:22 +00002120class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
2121protected:
Ted Kremeneke0e53132010-01-28 23:39:18 +00002122 Sema &S;
2123 const StringLiteral *FExpr;
2124 const Expr *OrigFormatExpr;
Ted Kremenek6ee76532010-03-25 03:59:12 +00002125 const unsigned FirstDataArg;
Ted Kremeneke0e53132010-01-28 23:39:18 +00002126 const unsigned NumDataArgs;
Ted Kremeneke0e53132010-01-28 23:39:18 +00002127 const char *Beg; // Start of format string.
Ted Kremenek0d277352010-01-29 01:06:55 +00002128 const bool HasVAListArg;
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002129 ArrayRef<const Expr *> Args;
Ted Kremenek0d277352010-01-29 01:06:55 +00002130 unsigned FormatIdx;
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002131 llvm::BitVector CoveredArgs;
Ted Kremenekefaff192010-02-27 01:41:03 +00002132 bool usesPositionalArgs;
2133 bool atFirstArg;
Richard Trieu55733de2011-10-28 00:41:25 +00002134 bool inFunctionCall;
Jordan Roseddcfbc92012-07-19 18:10:23 +00002135 Sema::VariadicCallType CallType;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002136public:
Ted Kremenek826a3452010-07-16 02:11:22 +00002137 CheckFormatHandler(Sema &s, const StringLiteral *fexpr,
Ted Kremenek6ee76532010-03-25 03:59:12 +00002138 const Expr *origFormatExpr, unsigned firstDataArg,
Jordan Rose50687312012-06-04 23:52:23 +00002139 unsigned numDataArgs, const char *beg, bool hasVAListArg,
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002140 ArrayRef<const Expr *> Args,
Jordan Roseddcfbc92012-07-19 18:10:23 +00002141 unsigned formatIdx, bool inFunctionCall,
2142 Sema::VariadicCallType callType)
Ted Kremeneke0e53132010-01-28 23:39:18 +00002143 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr),
Jordan Rose50687312012-06-04 23:52:23 +00002144 FirstDataArg(firstDataArg), NumDataArgs(numDataArgs),
2145 Beg(beg), HasVAListArg(hasVAListArg),
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002146 Args(Args), FormatIdx(formatIdx),
Richard Trieu55733de2011-10-28 00:41:25 +00002147 usesPositionalArgs(false), atFirstArg(true),
Jordan Roseddcfbc92012-07-19 18:10:23 +00002148 inFunctionCall(inFunctionCall), CallType(callType) {
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002149 CoveredArgs.resize(numDataArgs);
2150 CoveredArgs.reset();
2151 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002152
Ted Kremenek07d161f2010-01-29 01:50:07 +00002153 void DoneProcessing();
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002154
Ted Kremenek826a3452010-07-16 02:11:22 +00002155 void HandleIncompleteSpecifier(const char *startSpecifier,
2156 unsigned specifierLen);
Hans Wennborg76517422012-02-22 10:17:01 +00002157
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002158 void HandleInvalidLengthModifier(
2159 const analyze_format_string::FormatSpecifier &FS,
2160 const analyze_format_string::ConversionSpecifier &CS,
Jordan Rose8be066e2012-09-08 04:00:12 +00002161 const char *startSpecifier, unsigned specifierLen, unsigned DiagID);
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002162
Hans Wennborg76517422012-02-22 10:17:01 +00002163 void HandleNonStandardLengthModifier(
Jordan Rose8be066e2012-09-08 04:00:12 +00002164 const analyze_format_string::FormatSpecifier &FS,
Hans Wennborg76517422012-02-22 10:17:01 +00002165 const char *startSpecifier, unsigned specifierLen);
2166
2167 void HandleNonStandardConversionSpecifier(
2168 const analyze_format_string::ConversionSpecifier &CS,
2169 const char *startSpecifier, unsigned specifierLen);
2170
Hans Wennborgf8562642012-03-09 10:10:54 +00002171 virtual void HandlePosition(const char *startPos, unsigned posLen);
2172
Ted Kremenekefaff192010-02-27 01:41:03 +00002173 virtual void HandleInvalidPosition(const char *startSpecifier,
2174 unsigned specifierLen,
Ted Kremenek826a3452010-07-16 02:11:22 +00002175 analyze_format_string::PositionContext p);
Ted Kremenekefaff192010-02-27 01:41:03 +00002176
2177 virtual void HandleZeroPosition(const char *startPos, unsigned posLen);
2178
Ted Kremeneke0e53132010-01-28 23:39:18 +00002179 void HandleNullChar(const char *nullCharacter);
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002180
Richard Trieu55733de2011-10-28 00:41:25 +00002181 template <typename Range>
2182 static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall,
2183 const Expr *ArgumentExpr,
2184 PartialDiagnostic PDiag,
2185 SourceLocation StringLoc,
2186 bool IsStringLocation, Range StringRange,
Dmitri Gribenko55431692013-05-05 00:41:58 +00002187 ArrayRef<FixItHint> Fixit = None);
Richard Trieu55733de2011-10-28 00:41:25 +00002188
Ted Kremenek826a3452010-07-16 02:11:22 +00002189protected:
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002190 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
2191 const char *startSpec,
2192 unsigned specifierLen,
2193 const char *csStart, unsigned csLen);
Richard Trieu55733de2011-10-28 00:41:25 +00002194
2195 void HandlePositionalNonpositionalArgs(SourceLocation Loc,
2196 const char *startSpec,
2197 unsigned specifierLen);
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002198
Ted Kremenekf88c8e02010-01-29 20:55:36 +00002199 SourceRange getFormatStringRange();
Ted Kremenek826a3452010-07-16 02:11:22 +00002200 CharSourceRange getSpecifierRange(const char *startSpecifier,
2201 unsigned specifierLen);
Ted Kremeneke0e53132010-01-28 23:39:18 +00002202 SourceLocation getLocationOfByte(const char *x);
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002203
Ted Kremenek0d277352010-01-29 01:06:55 +00002204 const Expr *getDataArg(unsigned i) const;
Ted Kremenek666a1972010-07-26 19:45:42 +00002205
2206 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
2207 const analyze_format_string::ConversionSpecifier &CS,
2208 const char *startSpecifier, unsigned specifierLen,
2209 unsigned argIndex);
Richard Trieu55733de2011-10-28 00:41:25 +00002210
2211 template <typename Range>
2212 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
2213 bool IsStringLocation, Range StringRange,
Dmitri Gribenko55431692013-05-05 00:41:58 +00002214 ArrayRef<FixItHint> Fixit = None);
Richard Trieu55733de2011-10-28 00:41:25 +00002215
2216 void CheckPositionalAndNonpositionalArgs(
2217 const analyze_format_string::FormatSpecifier *FS);
Ted Kremeneke0e53132010-01-28 23:39:18 +00002218};
2219}
2220
Ted Kremenek826a3452010-07-16 02:11:22 +00002221SourceRange CheckFormatHandler::getFormatStringRange() {
Ted Kremeneke0e53132010-01-28 23:39:18 +00002222 return OrigFormatExpr->getSourceRange();
2223}
2224
Ted Kremenek826a3452010-07-16 02:11:22 +00002225CharSourceRange CheckFormatHandler::
2226getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
Tom Care45f9b7e2010-06-21 21:21:01 +00002227 SourceLocation Start = getLocationOfByte(startSpecifier);
2228 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1);
2229
2230 // Advance the end SourceLocation by one due to half-open ranges.
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00002231 End = End.getLocWithOffset(1);
Tom Care45f9b7e2010-06-21 21:21:01 +00002232
2233 return CharSourceRange::getCharRange(Start, End);
Ted Kremenekf88c8e02010-01-29 20:55:36 +00002234}
2235
Ted Kremenek826a3452010-07-16 02:11:22 +00002236SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002237 return S.getLocationOfStringLiteralByte(FExpr, x - Beg);
Ted Kremeneke0e53132010-01-28 23:39:18 +00002238}
2239
Ted Kremenek826a3452010-07-16 02:11:22 +00002240void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
2241 unsigned specifierLen){
Richard Trieu55733de2011-10-28 00:41:25 +00002242 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
2243 getLocationOfByte(startSpecifier),
2244 /*IsStringLocation*/true,
2245 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek808015a2010-01-29 03:16:21 +00002246}
2247
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002248void CheckFormatHandler::HandleInvalidLengthModifier(
2249 const analyze_format_string::FormatSpecifier &FS,
2250 const analyze_format_string::ConversionSpecifier &CS,
Jordan Rose8be066e2012-09-08 04:00:12 +00002251 const char *startSpecifier, unsigned specifierLen, unsigned DiagID) {
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002252 using namespace analyze_format_string;
2253
2254 const LengthModifier &LM = FS.getLengthModifier();
2255 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
2256
2257 // See if we know how to fix this length modifier.
David Blaikiedc84cd52013-02-20 22:23:23 +00002258 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002259 if (FixedLM) {
Jordan Rose8be066e2012-09-08 04:00:12 +00002260 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002261 getLocationOfByte(LM.getStart()),
2262 /*IsStringLocation*/true,
2263 getSpecifierRange(startSpecifier, specifierLen));
2264
2265 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
2266 << FixedLM->toString()
2267 << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
2268
2269 } else {
Jordan Rose8be066e2012-09-08 04:00:12 +00002270 FixItHint Hint;
2271 if (DiagID == diag::warn_format_nonsensical_length)
2272 Hint = FixItHint::CreateRemoval(LMRange);
2273
2274 EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(),
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002275 getLocationOfByte(LM.getStart()),
2276 /*IsStringLocation*/true,
2277 getSpecifierRange(startSpecifier, specifierLen),
Jordan Rose8be066e2012-09-08 04:00:12 +00002278 Hint);
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002279 }
2280}
2281
Hans Wennborg76517422012-02-22 10:17:01 +00002282void CheckFormatHandler::HandleNonStandardLengthModifier(
Jordan Rose8be066e2012-09-08 04:00:12 +00002283 const analyze_format_string::FormatSpecifier &FS,
Hans Wennborg76517422012-02-22 10:17:01 +00002284 const char *startSpecifier, unsigned specifierLen) {
Jordan Rose8be066e2012-09-08 04:00:12 +00002285 using namespace analyze_format_string;
2286
2287 const LengthModifier &LM = FS.getLengthModifier();
2288 CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength());
2289
2290 // See if we know how to fix this length modifier.
David Blaikiedc84cd52013-02-20 22:23:23 +00002291 Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier();
Jordan Rose8be066e2012-09-08 04:00:12 +00002292 if (FixedLM) {
2293 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
2294 << LM.toString() << 0,
2295 getLocationOfByte(LM.getStart()),
2296 /*IsStringLocation*/true,
2297 getSpecifierRange(startSpecifier, specifierLen));
2298
2299 S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier)
2300 << FixedLM->toString()
2301 << FixItHint::CreateReplacement(LMRange, FixedLM->toString());
2302
2303 } else {
2304 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
2305 << LM.toString() << 0,
2306 getLocationOfByte(LM.getStart()),
2307 /*IsStringLocation*/true,
2308 getSpecifierRange(startSpecifier, specifierLen));
2309 }
Hans Wennborg76517422012-02-22 10:17:01 +00002310}
2311
2312void CheckFormatHandler::HandleNonStandardConversionSpecifier(
2313 const analyze_format_string::ConversionSpecifier &CS,
2314 const char *startSpecifier, unsigned specifierLen) {
Jordan Rose670941c2012-09-13 02:11:15 +00002315 using namespace analyze_format_string;
2316
2317 // See if we know how to fix this conversion specifier.
David Blaikiedc84cd52013-02-20 22:23:23 +00002318 Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier();
Jordan Rose670941c2012-09-13 02:11:15 +00002319 if (FixedCS) {
2320 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
2321 << CS.toString() << /*conversion specifier*/1,
2322 getLocationOfByte(CS.getStart()),
2323 /*IsStringLocation*/true,
2324 getSpecifierRange(startSpecifier, specifierLen));
2325
2326 CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength());
2327 S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier)
2328 << FixedCS->toString()
2329 << FixItHint::CreateReplacement(CSRange, FixedCS->toString());
2330 } else {
2331 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard)
2332 << CS.toString() << /*conversion specifier*/1,
2333 getLocationOfByte(CS.getStart()),
2334 /*IsStringLocation*/true,
2335 getSpecifierRange(startSpecifier, specifierLen));
2336 }
Hans Wennborg76517422012-02-22 10:17:01 +00002337}
2338
Hans Wennborgf8562642012-03-09 10:10:54 +00002339void CheckFormatHandler::HandlePosition(const char *startPos,
2340 unsigned posLen) {
2341 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
2342 getLocationOfByte(startPos),
2343 /*IsStringLocation*/true,
2344 getSpecifierRange(startPos, posLen));
2345}
2346
Ted Kremenekefaff192010-02-27 01:41:03 +00002347void
Ted Kremenek826a3452010-07-16 02:11:22 +00002348CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
2349 analyze_format_string::PositionContext p) {
Richard Trieu55733de2011-10-28 00:41:25 +00002350 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier)
2351 << (unsigned) p,
2352 getLocationOfByte(startPos), /*IsStringLocation*/true,
2353 getSpecifierRange(startPos, posLen));
Ted Kremenekefaff192010-02-27 01:41:03 +00002354}
2355
Ted Kremenek826a3452010-07-16 02:11:22 +00002356void CheckFormatHandler::HandleZeroPosition(const char *startPos,
Ted Kremenekefaff192010-02-27 01:41:03 +00002357 unsigned posLen) {
Richard Trieu55733de2011-10-28 00:41:25 +00002358 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
2359 getLocationOfByte(startPos),
2360 /*IsStringLocation*/true,
2361 getSpecifierRange(startPos, posLen));
Ted Kremenekefaff192010-02-27 01:41:03 +00002362}
2363
Ted Kremenek826a3452010-07-16 02:11:22 +00002364void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
Jordan Rose50687312012-06-04 23:52:23 +00002365 if (!isa<ObjCStringLiteral>(OrigFormatExpr)) {
Ted Kremenek0c069442011-03-15 21:18:48 +00002366 // The presence of a null character is likely an error.
Richard Trieu55733de2011-10-28 00:41:25 +00002367 EmitFormatDiagnostic(
2368 S.PDiag(diag::warn_printf_format_string_contains_null_char),
2369 getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
2370 getFormatStringRange());
Ted Kremenek0c069442011-03-15 21:18:48 +00002371 }
Ted Kremenek826a3452010-07-16 02:11:22 +00002372}
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002373
Jordan Rose48716662012-07-19 18:10:08 +00002374// Note that this may return NULL if there was an error parsing or building
2375// one of the argument expressions.
Ted Kremenek826a3452010-07-16 02:11:22 +00002376const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002377 return Args[FirstDataArg + i];
Ted Kremenek826a3452010-07-16 02:11:22 +00002378}
2379
2380void CheckFormatHandler::DoneProcessing() {
2381 // Does the number of data arguments exceed the number of
2382 // format conversions in the format string?
2383 if (!HasVAListArg) {
2384 // Find any arguments that weren't covered.
2385 CoveredArgs.flip();
2386 signed notCoveredArg = CoveredArgs.find_first();
2387 if (notCoveredArg >= 0) {
2388 assert((unsigned)notCoveredArg < NumDataArgs);
Jordan Rose48716662012-07-19 18:10:08 +00002389 if (const Expr *E = getDataArg((unsigned) notCoveredArg)) {
2390 SourceLocation Loc = E->getLocStart();
2391 if (!S.getSourceManager().isInSystemMacro(Loc)) {
2392 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used),
2393 Loc, /*IsStringLocation*/false,
2394 getFormatStringRange());
2395 }
Bob Wilsonc03f2df2012-05-03 19:47:19 +00002396 }
Ted Kremenek826a3452010-07-16 02:11:22 +00002397 }
2398 }
2399}
2400
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002401bool
2402CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
2403 SourceLocation Loc,
2404 const char *startSpec,
2405 unsigned specifierLen,
2406 const char *csStart,
2407 unsigned csLen) {
2408
2409 bool keepGoing = true;
2410 if (argIndex < NumDataArgs) {
2411 // Consider the argument coverered, even though the specifier doesn't
2412 // make sense.
2413 CoveredArgs.set(argIndex);
2414 }
2415 else {
2416 // If argIndex exceeds the number of data arguments we
2417 // don't issue a warning because that is just a cascade of warnings (and
2418 // they may have intended '%%' anyway). We don't want to continue processing
2419 // the format string after this point, however, as we will like just get
2420 // gibberish when trying to match arguments.
2421 keepGoing = false;
2422 }
2423
Richard Trieu55733de2011-10-28 00:41:25 +00002424 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion)
2425 << StringRef(csStart, csLen),
2426 Loc, /*IsStringLocation*/true,
2427 getSpecifierRange(startSpec, specifierLen));
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002428
2429 return keepGoing;
2430}
2431
Richard Trieu55733de2011-10-28 00:41:25 +00002432void
2433CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
2434 const char *startSpec,
2435 unsigned specifierLen) {
2436 EmitFormatDiagnostic(
2437 S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
2438 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
2439}
2440
Ted Kremenek666a1972010-07-26 19:45:42 +00002441bool
2442CheckFormatHandler::CheckNumArgs(
2443 const analyze_format_string::FormatSpecifier &FS,
2444 const analyze_format_string::ConversionSpecifier &CS,
2445 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
2446
2447 if (argIndex >= NumDataArgs) {
Richard Trieu55733de2011-10-28 00:41:25 +00002448 PartialDiagnostic PDiag = FS.usesPositionalArg()
2449 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
2450 << (argIndex+1) << NumDataArgs)
2451 : S.PDiag(diag::warn_printf_insufficient_data_args);
2452 EmitFormatDiagnostic(
2453 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
2454 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek666a1972010-07-26 19:45:42 +00002455 return false;
2456 }
2457 return true;
2458}
2459
Richard Trieu55733de2011-10-28 00:41:25 +00002460template<typename Range>
2461void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
2462 SourceLocation Loc,
2463 bool IsStringLocation,
2464 Range StringRange,
Jordan Roseec087352012-09-05 22:56:26 +00002465 ArrayRef<FixItHint> FixIt) {
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002466 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
Richard Trieu55733de2011-10-28 00:41:25 +00002467 Loc, IsStringLocation, StringRange, FixIt);
2468}
2469
2470/// \brief If the format string is not within the funcion call, emit a note
2471/// so that the function call and string are in diagnostic messages.
2472///
Dmitri Gribenko70517ca2012-08-23 17:58:28 +00002473/// \param InFunctionCall if true, the format string is within the function
Richard Trieu55733de2011-10-28 00:41:25 +00002474/// call and only one diagnostic message will be produced. Otherwise, an
2475/// extra note will be emitted pointing to location of the format string.
2476///
2477/// \param ArgumentExpr the expression that is passed as the format string
2478/// argument in the function call. Used for getting locations when two
2479/// diagnostics are emitted.
2480///
2481/// \param PDiag the callee should already have provided any strings for the
2482/// diagnostic message. This function only adds locations and fixits
2483/// to diagnostics.
2484///
2485/// \param Loc primary location for diagnostic. If two diagnostics are
2486/// required, one will be at Loc and a new SourceLocation will be created for
2487/// the other one.
2488///
2489/// \param IsStringLocation if true, Loc points to the format string should be
2490/// used for the note. Otherwise, Loc points to the argument list and will
2491/// be used with PDiag.
2492///
2493/// \param StringRange some or all of the string to highlight. This is
2494/// templated so it can accept either a CharSourceRange or a SourceRange.
2495///
Dmitri Gribenko70517ca2012-08-23 17:58:28 +00002496/// \param FixIt optional fix it hint for the format string.
Richard Trieu55733de2011-10-28 00:41:25 +00002497template<typename Range>
2498void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall,
2499 const Expr *ArgumentExpr,
2500 PartialDiagnostic PDiag,
2501 SourceLocation Loc,
2502 bool IsStringLocation,
2503 Range StringRange,
Jordan Roseec087352012-09-05 22:56:26 +00002504 ArrayRef<FixItHint> FixIt) {
2505 if (InFunctionCall) {
2506 const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag);
2507 D << StringRange;
2508 for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end();
2509 I != E; ++I) {
2510 D << *I;
2511 }
2512 } else {
Richard Trieu55733de2011-10-28 00:41:25 +00002513 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
2514 << ArgumentExpr->getSourceRange();
Jordan Roseec087352012-09-05 22:56:26 +00002515
2516 const Sema::SemaDiagnosticBuilder &Note =
2517 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
2518 diag::note_format_string_defined);
2519
2520 Note << StringRange;
2521 for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end();
2522 I != E; ++I) {
2523 Note << *I;
2524 }
Richard Trieu55733de2011-10-28 00:41:25 +00002525 }
2526}
2527
Ted Kremenek826a3452010-07-16 02:11:22 +00002528//===--- CHECK: Printf format string checking ------------------------------===//
2529
2530namespace {
2531class CheckPrintfHandler : public CheckFormatHandler {
Jordan Rose50687312012-06-04 23:52:23 +00002532 bool ObjCContext;
Ted Kremenek826a3452010-07-16 02:11:22 +00002533public:
2534 CheckPrintfHandler(Sema &s, const StringLiteral *fexpr,
2535 const Expr *origFormatExpr, unsigned firstDataArg,
Jordan Rose50687312012-06-04 23:52:23 +00002536 unsigned numDataArgs, bool isObjC,
Ted Kremenek826a3452010-07-16 02:11:22 +00002537 const char *beg, bool hasVAListArg,
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002538 ArrayRef<const Expr *> Args,
Jordan Roseddcfbc92012-07-19 18:10:23 +00002539 unsigned formatIdx, bool inFunctionCall,
2540 Sema::VariadicCallType CallType)
Ted Kremenek826a3452010-07-16 02:11:22 +00002541 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00002542 numDataArgs, beg, hasVAListArg, Args,
Jordan Roseddcfbc92012-07-19 18:10:23 +00002543 formatIdx, inFunctionCall, CallType), ObjCContext(isObjC)
2544 {}
2545
Ted Kremenek826a3452010-07-16 02:11:22 +00002546
2547 bool HandleInvalidPrintfConversionSpecifier(
2548 const analyze_printf::PrintfSpecifier &FS,
2549 const char *startSpecifier,
2550 unsigned specifierLen);
2551
2552 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
2553 const char *startSpecifier,
2554 unsigned specifierLen);
Richard Smith831421f2012-06-25 20:30:08 +00002555 bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
2556 const char *StartSpecifier,
2557 unsigned SpecifierLen,
2558 const Expr *E);
2559
Ted Kremenek826a3452010-07-16 02:11:22 +00002560 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
2561 const char *startSpecifier, unsigned specifierLen);
2562 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
2563 const analyze_printf::OptionalAmount &Amt,
2564 unsigned type,
2565 const char *startSpecifier, unsigned specifierLen);
2566 void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
2567 const analyze_printf::OptionalFlag &flag,
2568 const char *startSpecifier, unsigned specifierLen);
2569 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
2570 const analyze_printf::OptionalFlag &ignoredFlag,
2571 const analyze_printf::OptionalFlag &flag,
2572 const char *startSpecifier, unsigned specifierLen);
Hans Wennborgf3749f42012-08-07 08:11:26 +00002573 bool checkForCStrMembers(const analyze_printf::ArgType &AT,
Richard Smith831421f2012-06-25 20:30:08 +00002574 const Expr *E, const CharSourceRange &CSR);
2575
Ted Kremenek826a3452010-07-16 02:11:22 +00002576};
2577}
2578
2579bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
2580 const analyze_printf::PrintfSpecifier &FS,
2581 const char *startSpecifier,
2582 unsigned specifierLen) {
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002583 const analyze_printf::PrintfConversionSpecifier &CS =
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002584 FS.getConversionSpecifier();
Ted Kremenek826a3452010-07-16 02:11:22 +00002585
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002586 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
2587 getLocationOfByte(CS.getStart()),
2588 startSpecifier, specifierLen,
2589 CS.getStart(), CS.getLength());
Ted Kremenek26ac2e02010-01-29 02:40:24 +00002590}
2591
Ted Kremenek826a3452010-07-16 02:11:22 +00002592bool CheckPrintfHandler::HandleAmount(
2593 const analyze_format_string::OptionalAmount &Amt,
2594 unsigned k, const char *startSpecifier,
2595 unsigned specifierLen) {
Ted Kremenek0d277352010-01-29 01:06:55 +00002596
2597 if (Amt.hasDataArgument()) {
Ted Kremenek0d277352010-01-29 01:06:55 +00002598 if (!HasVAListArg) {
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002599 unsigned argIndex = Amt.getArgIndex();
2600 if (argIndex >= NumDataArgs) {
Richard Trieu55733de2011-10-28 00:41:25 +00002601 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
2602 << k,
2603 getLocationOfByte(Amt.getStart()),
2604 /*IsStringLocation*/true,
2605 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek0d277352010-01-29 01:06:55 +00002606 // Don't do any more checking. We will just emit
2607 // spurious errors.
2608 return false;
2609 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002610
Ted Kremenek0d277352010-01-29 01:06:55 +00002611 // Type check the data argument. It should be an 'int'.
Ted Kremenek31f8e322010-01-29 23:32:22 +00002612 // Although not in conformance with C99, we also allow the argument to be
2613 // an 'unsigned int' as that is a reasonably safe case. GCC also
2614 // doesn't emit a warning for that case.
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002615 CoveredArgs.set(argIndex);
2616 const Expr *Arg = getDataArg(argIndex);
Jordan Rose48716662012-07-19 18:10:08 +00002617 if (!Arg)
2618 return false;
2619
Ted Kremenek0d277352010-01-29 01:06:55 +00002620 QualType T = Arg->getType();
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002621
Hans Wennborgf3749f42012-08-07 08:11:26 +00002622 const analyze_printf::ArgType &AT = Amt.getArgType(S.Context);
2623 assert(AT.isValid());
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002624
Hans Wennborgf3749f42012-08-07 08:11:26 +00002625 if (!AT.matchesType(S.Context, T)) {
Richard Trieu55733de2011-10-28 00:41:25 +00002626 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
Hans Wennborgf3749f42012-08-07 08:11:26 +00002627 << k << AT.getRepresentativeTypeName(S.Context)
Richard Trieu55733de2011-10-28 00:41:25 +00002628 << T << Arg->getSourceRange(),
2629 getLocationOfByte(Amt.getStart()),
2630 /*IsStringLocation*/true,
2631 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek0d277352010-01-29 01:06:55 +00002632 // Don't do any more checking. We will just emit
2633 // spurious errors.
2634 return false;
2635 }
2636 }
2637 }
2638 return true;
2639}
Ted Kremenek0d277352010-01-29 01:06:55 +00002640
Tom Caree4ee9662010-06-17 19:00:27 +00002641void CheckPrintfHandler::HandleInvalidAmount(
Ted Kremenek826a3452010-07-16 02:11:22 +00002642 const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00002643 const analyze_printf::OptionalAmount &Amt,
2644 unsigned type,
2645 const char *startSpecifier,
2646 unsigned specifierLen) {
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002647 const analyze_printf::PrintfConversionSpecifier &CS =
2648 FS.getConversionSpecifier();
Tom Caree4ee9662010-06-17 19:00:27 +00002649
Richard Trieu55733de2011-10-28 00:41:25 +00002650 FixItHint fixit =
2651 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
2652 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
2653 Amt.getConstantLength()))
2654 : FixItHint();
2655
2656 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
2657 << type << CS.toString(),
2658 getLocationOfByte(Amt.getStart()),
2659 /*IsStringLocation*/true,
2660 getSpecifierRange(startSpecifier, specifierLen),
2661 fixit);
Tom Caree4ee9662010-06-17 19:00:27 +00002662}
2663
Ted Kremenek826a3452010-07-16 02:11:22 +00002664void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00002665 const analyze_printf::OptionalFlag &flag,
2666 const char *startSpecifier,
2667 unsigned specifierLen) {
2668 // Warn about pointless flag with a fixit removal.
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002669 const analyze_printf::PrintfConversionSpecifier &CS =
2670 FS.getConversionSpecifier();
Richard Trieu55733de2011-10-28 00:41:25 +00002671 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
2672 << flag.toString() << CS.toString(),
2673 getLocationOfByte(flag.getPosition()),
2674 /*IsStringLocation*/true,
2675 getSpecifierRange(startSpecifier, specifierLen),
2676 FixItHint::CreateRemoval(
2677 getSpecifierRange(flag.getPosition(), 1)));
Tom Caree4ee9662010-06-17 19:00:27 +00002678}
2679
2680void CheckPrintfHandler::HandleIgnoredFlag(
Ted Kremenek826a3452010-07-16 02:11:22 +00002681 const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00002682 const analyze_printf::OptionalFlag &ignoredFlag,
2683 const analyze_printf::OptionalFlag &flag,
2684 const char *startSpecifier,
2685 unsigned specifierLen) {
2686 // Warn about ignored flag with a fixit removal.
Richard Trieu55733de2011-10-28 00:41:25 +00002687 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
2688 << ignoredFlag.toString() << flag.toString(),
2689 getLocationOfByte(ignoredFlag.getPosition()),
2690 /*IsStringLocation*/true,
2691 getSpecifierRange(startSpecifier, specifierLen),
2692 FixItHint::CreateRemoval(
2693 getSpecifierRange(ignoredFlag.getPosition(), 1)));
Tom Caree4ee9662010-06-17 19:00:27 +00002694}
2695
Richard Smith831421f2012-06-25 20:30:08 +00002696// Determines if the specified is a C++ class or struct containing
2697// a member with the specified name and kind (e.g. a CXXMethodDecl named
2698// "c_str()").
2699template<typename MemberKind>
2700static llvm::SmallPtrSet<MemberKind*, 1>
2701CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
2702 const RecordType *RT = Ty->getAs<RecordType>();
2703 llvm::SmallPtrSet<MemberKind*, 1> Results;
2704
2705 if (!RT)
2706 return Results;
2707 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
2708 if (!RD)
2709 return Results;
2710
2711 LookupResult R(S, &S.PP.getIdentifierTable().get(Name), SourceLocation(),
2712 Sema::LookupMemberName);
2713
2714 // We just need to include all members of the right kind turned up by the
2715 // filter, at this point.
2716 if (S.LookupQualifiedName(R, RT->getDecl()))
2717 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
2718 NamedDecl *decl = (*I)->getUnderlyingDecl();
2719 if (MemberKind *FK = dyn_cast<MemberKind>(decl))
2720 Results.insert(FK);
2721 }
2722 return Results;
2723}
2724
2725// Check if a (w)string was passed when a (w)char* was needed, and offer a
Hans Wennborgf3749f42012-08-07 08:11:26 +00002726// better diagnostic if so. AT is assumed to be valid.
Richard Smith831421f2012-06-25 20:30:08 +00002727// Returns true when a c_str() conversion method is found.
2728bool CheckPrintfHandler::checkForCStrMembers(
Hans Wennborgf3749f42012-08-07 08:11:26 +00002729 const analyze_printf::ArgType &AT, const Expr *E,
Richard Smith831421f2012-06-25 20:30:08 +00002730 const CharSourceRange &CSR) {
2731 typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet;
2732
2733 MethodSet Results =
2734 CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType());
2735
2736 for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
2737 MI != ME; ++MI) {
2738 const CXXMethodDecl *Method = *MI;
2739 if (Method->getNumParams() == 0 &&
Hans Wennborgf3749f42012-08-07 08:11:26 +00002740 AT.matchesType(S.Context, Method->getResultType())) {
Richard Smith831421f2012-06-25 20:30:08 +00002741 // FIXME: Suggest parens if the expression needs them.
2742 SourceLocation EndLoc =
2743 S.getPreprocessor().getLocForEndOfToken(E->getLocEnd());
2744 S.Diag(E->getLocStart(), diag::note_printf_c_str)
2745 << "c_str()"
2746 << FixItHint::CreateInsertion(EndLoc, ".c_str()");
2747 return true;
2748 }
2749 }
2750
2751 return false;
2752}
2753
Ted Kremeneke0e53132010-01-28 23:39:18 +00002754bool
Ted Kremenek826a3452010-07-16 02:11:22 +00002755CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
Ted Kremenek5c41ee82010-02-11 09:27:41 +00002756 &FS,
Ted Kremeneke0e53132010-01-28 23:39:18 +00002757 const char *startSpecifier,
2758 unsigned specifierLen) {
2759
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002760 using namespace analyze_format_string;
Ted Kremenekefaff192010-02-27 01:41:03 +00002761 using namespace analyze_printf;
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002762 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
Ted Kremeneke0e53132010-01-28 23:39:18 +00002763
Ted Kremenekbaa40062010-07-19 22:01:06 +00002764 if (FS.consumesDataArgument()) {
2765 if (atFirstArg) {
2766 atFirstArg = false;
2767 usesPositionalArgs = FS.usesPositionalArg();
2768 }
2769 else if (usesPositionalArgs != FS.usesPositionalArg()) {
Richard Trieu55733de2011-10-28 00:41:25 +00002770 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
2771 startSpecifier, specifierLen);
Ted Kremenekbaa40062010-07-19 22:01:06 +00002772 return false;
2773 }
Ted Kremenek0d277352010-01-29 01:06:55 +00002774 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002775
Ted Kremenekefaff192010-02-27 01:41:03 +00002776 // First check if the field width, precision, and conversion specifier
2777 // have matching data arguments.
2778 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
2779 startSpecifier, specifierLen)) {
2780 return false;
2781 }
2782
2783 if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
2784 startSpecifier, specifierLen)) {
Ted Kremenek0d277352010-01-29 01:06:55 +00002785 return false;
2786 }
2787
Ted Kremenekf88c8e02010-01-29 20:55:36 +00002788 if (!CS.consumesDataArgument()) {
2789 // FIXME: Technically specifying a precision or field width here
2790 // makes no sense. Worth issuing a warning at some point.
Ted Kremenek0e5675d2010-02-10 02:16:30 +00002791 return true;
Ted Kremenekf88c8e02010-01-29 20:55:36 +00002792 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002793
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002794 // Consume the argument.
2795 unsigned argIndex = FS.getArgIndex();
Ted Kremeneke3fc5472010-02-27 08:34:51 +00002796 if (argIndex < NumDataArgs) {
2797 // The check to see if the argIndex is valid will come later.
2798 // We set the bit here because we may exit early from this
2799 // function if we encounter some other error.
2800 CoveredArgs.set(argIndex);
2801 }
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002802
2803 // Check for using an Objective-C specific conversion specifier
2804 // in a non-ObjC literal.
Jordan Rose50687312012-06-04 23:52:23 +00002805 if (!ObjCContext && CS.isObjCArg()) {
Ted Kremenek826a3452010-07-16 02:11:22 +00002806 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
2807 specifierLen);
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002808 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002809
Tom Caree4ee9662010-06-17 19:00:27 +00002810 // Check for invalid use of field width
2811 if (!FS.hasValidFieldWidth()) {
Tom Care45f9b7e2010-06-21 21:21:01 +00002812 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
Tom Caree4ee9662010-06-17 19:00:27 +00002813 startSpecifier, specifierLen);
2814 }
2815
2816 // Check for invalid use of precision
2817 if (!FS.hasValidPrecision()) {
2818 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
2819 startSpecifier, specifierLen);
2820 }
2821
2822 // Check each flag does not conflict with any other component.
Ted Kremenek65197b42011-01-08 05:28:46 +00002823 if (!FS.hasValidThousandsGroupingPrefix())
2824 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00002825 if (!FS.hasValidLeadingZeros())
2826 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
2827 if (!FS.hasValidPlusPrefix())
2828 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
Tom Care45f9b7e2010-06-21 21:21:01 +00002829 if (!FS.hasValidSpacePrefix())
2830 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00002831 if (!FS.hasValidAlternativeForm())
2832 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
2833 if (!FS.hasValidLeftJustified())
2834 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
2835
2836 // Check that flags are not ignored by another flag
Tom Care45f9b7e2010-06-21 21:21:01 +00002837 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
2838 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
2839 startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00002840 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
2841 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
2842 startSpecifier, specifierLen);
2843
2844 // Check the length modifier is valid with the given conversion specifier.
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002845 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
Jordan Rose8be066e2012-09-08 04:00:12 +00002846 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
2847 diag::warn_format_nonsensical_length);
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002848 else if (!FS.hasStandardLengthModifier())
Jordan Rose8be066e2012-09-08 04:00:12 +00002849 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002850 else if (!FS.hasStandardLengthConversionCombination())
Jordan Rose8be066e2012-09-08 04:00:12 +00002851 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
2852 diag::warn_format_non_standard_conversion_spec);
Tom Caree4ee9662010-06-17 19:00:27 +00002853
Jordan Rosebbb6bb42012-09-08 04:00:03 +00002854 if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
2855 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
2856
Ted Kremenekda51f0d2010-01-29 01:43:31 +00002857 // The remaining checks depend on the data arguments.
2858 if (HasVAListArg)
2859 return true;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002860
Ted Kremenek666a1972010-07-26 19:45:42 +00002861 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
Ted Kremenekda51f0d2010-01-29 01:43:31 +00002862 return false;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002863
Jordan Rose48716662012-07-19 18:10:08 +00002864 const Expr *Arg = getDataArg(argIndex);
2865 if (!Arg)
2866 return true;
2867
2868 return checkFormatExpr(FS, startSpecifier, specifierLen, Arg);
Richard Smith831421f2012-06-25 20:30:08 +00002869}
2870
Jordan Roseec087352012-09-05 22:56:26 +00002871static bool requiresParensToAddCast(const Expr *E) {
2872 // FIXME: We should have a general way to reason about operator
2873 // precedence and whether parens are actually needed here.
2874 // Take care of a few common cases where they aren't.
2875 const Expr *Inside = E->IgnoreImpCasts();
2876 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside))
2877 Inside = POE->getSyntacticForm()->IgnoreImpCasts();
2878
2879 switch (Inside->getStmtClass()) {
2880 case Stmt::ArraySubscriptExprClass:
2881 case Stmt::CallExprClass:
Jordan Rose17ddc542012-12-05 18:44:44 +00002882 case Stmt::CharacterLiteralClass:
2883 case Stmt::CXXBoolLiteralExprClass:
Jordan Roseec087352012-09-05 22:56:26 +00002884 case Stmt::DeclRefExprClass:
Jordan Rose17ddc542012-12-05 18:44:44 +00002885 case Stmt::FloatingLiteralClass:
2886 case Stmt::IntegerLiteralClass:
Jordan Roseec087352012-09-05 22:56:26 +00002887 case Stmt::MemberExprClass:
Jordan Rose17ddc542012-12-05 18:44:44 +00002888 case Stmt::ObjCArrayLiteralClass:
2889 case Stmt::ObjCBoolLiteralExprClass:
2890 case Stmt::ObjCBoxedExprClass:
2891 case Stmt::ObjCDictionaryLiteralClass:
2892 case Stmt::ObjCEncodeExprClass:
Jordan Roseec087352012-09-05 22:56:26 +00002893 case Stmt::ObjCIvarRefExprClass:
2894 case Stmt::ObjCMessageExprClass:
2895 case Stmt::ObjCPropertyRefExprClass:
Jordan Rose17ddc542012-12-05 18:44:44 +00002896 case Stmt::ObjCStringLiteralClass:
2897 case Stmt::ObjCSubscriptRefExprClass:
Jordan Roseec087352012-09-05 22:56:26 +00002898 case Stmt::ParenExprClass:
Jordan Rose17ddc542012-12-05 18:44:44 +00002899 case Stmt::StringLiteralClass:
Jordan Roseec087352012-09-05 22:56:26 +00002900 case Stmt::UnaryOperatorClass:
2901 return false;
2902 default:
2903 return true;
2904 }
2905}
2906
Richard Smith831421f2012-06-25 20:30:08 +00002907bool
2908CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
2909 const char *StartSpecifier,
2910 unsigned SpecifierLen,
2911 const Expr *E) {
2912 using namespace analyze_format_string;
2913 using namespace analyze_printf;
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002914 // Now type check the data expression that matches the
2915 // format specifier.
Hans Wennborgf3749f42012-08-07 08:11:26 +00002916 const analyze_printf::ArgType &AT = FS.getArgType(S.Context,
2917 ObjCContext);
Jordan Rose614a8652012-09-05 22:56:19 +00002918 if (!AT.isValid())
2919 return true;
Jordan Roseec087352012-09-05 22:56:26 +00002920
Jordan Rose448ac3e2012-12-05 18:44:40 +00002921 QualType ExprTy = E->getType();
Ted Kremenek02be9682013-04-10 06:26:26 +00002922 while (const TypeOfExprType *TET = dyn_cast<TypeOfExprType>(ExprTy)) {
2923 ExprTy = TET->getUnderlyingExpr()->getType();
2924 }
2925
Jordan Rose448ac3e2012-12-05 18:44:40 +00002926 if (AT.matchesType(S.Context, ExprTy))
Jordan Rose614a8652012-09-05 22:56:19 +00002927 return true;
Jordan Roseee0259d2012-06-04 22:48:57 +00002928
Jordan Rose614a8652012-09-05 22:56:19 +00002929 // Look through argument promotions for our error message's reported type.
2930 // This includes the integral and floating promotions, but excludes array
2931 // and function pointer decay; seeing that an argument intended to be a
2932 // string has type 'char [6]' is probably more confusing than 'char *'.
2933 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2934 if (ICE->getCastKind() == CK_IntegralCast ||
2935 ICE->getCastKind() == CK_FloatingCast) {
2936 E = ICE->getSubExpr();
Jordan Rose448ac3e2012-12-05 18:44:40 +00002937 ExprTy = E->getType();
Jordan Rose614a8652012-09-05 22:56:19 +00002938
2939 // Check if we didn't match because of an implicit cast from a 'char'
2940 // or 'short' to an 'int'. This is done because printf is a varargs
2941 // function.
2942 if (ICE->getType() == S.Context.IntTy ||
2943 ICE->getType() == S.Context.UnsignedIntTy) {
2944 // All further checking is done on the subexpression.
Jordan Rose448ac3e2012-12-05 18:44:40 +00002945 if (AT.matchesType(S.Context, ExprTy))
Jordan Rose614a8652012-09-05 22:56:19 +00002946 return true;
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +00002947 }
Jordan Roseee0259d2012-06-04 22:48:57 +00002948 }
Jordan Rose448ac3e2012-12-05 18:44:40 +00002949 } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) {
2950 // Special case for 'a', which has type 'int' in C.
2951 // Note, however, that we do /not/ want to treat multibyte constants like
2952 // 'MooV' as characters! This form is deprecated but still exists.
2953 if (ExprTy == S.Context.IntTy)
2954 if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue()))
2955 ExprTy = S.Context.CharTy;
Jordan Rose614a8652012-09-05 22:56:19 +00002956 }
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002957
Jordan Rose2cd34402012-12-05 18:44:49 +00002958 // %C in an Objective-C context prints a unichar, not a wchar_t.
2959 // If the argument is an integer of some kind, believe the %C and suggest
2960 // a cast instead of changing the conversion specifier.
Jordan Rose448ac3e2012-12-05 18:44:40 +00002961 QualType IntendedTy = ExprTy;
Jordan Rose2cd34402012-12-05 18:44:49 +00002962 if (ObjCContext &&
2963 FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
2964 if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
2965 !ExprTy->isCharType()) {
2966 // 'unichar' is defined as a typedef of unsigned short, but we should
2967 // prefer using the typedef if it is visible.
2968 IntendedTy = S.Context.UnsignedShortTy;
2969
2970 LookupResult Result(S, &S.Context.Idents.get("unichar"), E->getLocStart(),
2971 Sema::LookupOrdinaryName);
2972 if (S.LookupName(Result, S.getCurScope())) {
2973 NamedDecl *ND = Result.getFoundDecl();
2974 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(ND))
2975 if (TD->getUnderlyingType() == IntendedTy)
2976 IntendedTy = S.Context.getTypedefType(TD);
2977 }
2978 }
2979 }
2980
2981 // Special-case some of Darwin's platform-independence types by suggesting
2982 // casts to primitive types that are known to be large enough.
2983 bool ShouldNotPrintDirectly = false;
Jordan Roseec087352012-09-05 22:56:26 +00002984 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Ted Kremenek6edb0292013-03-25 22:28:37 +00002985 // Use a 'while' to peel off layers of typedefs.
2986 QualType TyTy = IntendedTy;
2987 while (const TypedefType *UserTy = TyTy->getAs<TypedefType>()) {
Jordan Roseec087352012-09-05 22:56:26 +00002988 StringRef Name = UserTy->getDecl()->getName();
Jordan Rose2cd34402012-12-05 18:44:49 +00002989 QualType CastTy = llvm::StringSwitch<QualType>(Name)
Jordan Roseec087352012-09-05 22:56:26 +00002990 .Case("NSInteger", S.Context.LongTy)
2991 .Case("NSUInteger", S.Context.UnsignedLongTy)
2992 .Case("SInt32", S.Context.IntTy)
2993 .Case("UInt32", S.Context.UnsignedIntTy)
Jordan Rose2cd34402012-12-05 18:44:49 +00002994 .Default(QualType());
2995
2996 if (!CastTy.isNull()) {
2997 ShouldNotPrintDirectly = true;
2998 IntendedTy = CastTy;
Ted Kremenek6edb0292013-03-25 22:28:37 +00002999 break;
Jordan Rose2cd34402012-12-05 18:44:49 +00003000 }
Ted Kremenek6edb0292013-03-25 22:28:37 +00003001 TyTy = UserTy->desugar();
Jordan Roseec087352012-09-05 22:56:26 +00003002 }
3003 }
3004
Jordan Rose614a8652012-09-05 22:56:19 +00003005 // We may be able to offer a FixItHint if it is a supported type.
3006 PrintfSpecifier fixedFS = FS;
Jordan Roseec087352012-09-05 22:56:26 +00003007 bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(),
Jordan Rose614a8652012-09-05 22:56:19 +00003008 S.Context, ObjCContext);
Michael J. Spencer96827eb2010-07-27 04:46:02 +00003009
Jordan Rose614a8652012-09-05 22:56:19 +00003010 if (success) {
3011 // Get the fix string from the fixed format specifier
3012 SmallString<16> buf;
3013 llvm::raw_svector_ostream os(buf);
3014 fixedFS.toString(os);
Michael J. Spencer96827eb2010-07-27 04:46:02 +00003015
Jordan Roseec087352012-09-05 22:56:26 +00003016 CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
3017
Jordan Rose2cd34402012-12-05 18:44:49 +00003018 if (IntendedTy == ExprTy) {
3019 // In this case, the specifier is wrong and should be changed to match
3020 // the argument.
3021 EmitFormatDiagnostic(
3022 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
3023 << AT.getRepresentativeTypeName(S.Context) << IntendedTy
3024 << E->getSourceRange(),
3025 E->getLocStart(),
3026 /*IsStringLocation*/false,
3027 SpecRange,
3028 FixItHint::CreateReplacement(SpecRange, os.str()));
3029
3030 } else {
Jordan Roseec087352012-09-05 22:56:26 +00003031 // The canonical type for formatting this value is different from the
3032 // actual type of the expression. (This occurs, for example, with Darwin's
3033 // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but
3034 // should be printed as 'long' for 64-bit compatibility.)
3035 // Rather than emitting a normal format/argument mismatch, we want to
3036 // add a cast to the recommended type (and correct the format string
3037 // if necessary).
3038 SmallString<16> CastBuf;
3039 llvm::raw_svector_ostream CastFix(CastBuf);
3040 CastFix << "(";
3041 IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
3042 CastFix << ")";
3043
3044 SmallVector<FixItHint,4> Hints;
3045 if (!AT.matchesType(S.Context, IntendedTy))
3046 Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
3047
3048 if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {
3049 // If there's already a cast present, just replace it.
3050 SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc());
3051 Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str()));
3052
3053 } else if (!requiresParensToAddCast(E)) {
3054 // If the expression has high enough precedence,
3055 // just write the C-style cast.
3056 Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
3057 CastFix.str()));
3058 } else {
3059 // Otherwise, add parens around the expression as well as the cast.
3060 CastFix << "(";
3061 Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(),
3062 CastFix.str()));
3063
3064 SourceLocation After = S.PP.getLocForEndOfToken(E->getLocEnd());
3065 Hints.push_back(FixItHint::CreateInsertion(After, ")"));
3066 }
3067
Jordan Rose2cd34402012-12-05 18:44:49 +00003068 if (ShouldNotPrintDirectly) {
3069 // The expression has a type that should not be printed directly.
3070 // We extract the name from the typedef because we don't want to show
3071 // the underlying type in the diagnostic.
3072 StringRef Name = cast<TypedefType>(ExprTy)->getDecl()->getName();
Jordan Roseec087352012-09-05 22:56:26 +00003073
Jordan Rose2cd34402012-12-05 18:44:49 +00003074 EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast)
3075 << Name << IntendedTy
3076 << E->getSourceRange(),
3077 E->getLocStart(), /*IsStringLocation=*/false,
3078 SpecRange, Hints);
3079 } else {
3080 // In this case, the expression could be printed using a different
3081 // specifier, but we've decided that the specifier is probably correct
3082 // and we should cast instead. Just use the normal warning message.
3083 EmitFormatDiagnostic(
3084 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
3085 << AT.getRepresentativeTypeName(S.Context) << ExprTy
3086 << E->getSourceRange(),
3087 E->getLocStart(), /*IsStringLocation*/false,
3088 SpecRange, Hints);
3089 }
Jordan Roseec087352012-09-05 22:56:26 +00003090 }
Jordan Rose614a8652012-09-05 22:56:19 +00003091 } else {
3092 const CharSourceRange &CSR = getSpecifierRange(StartSpecifier,
3093 SpecifierLen);
3094 // Since the warning for passing non-POD types to variadic functions
3095 // was deferred until now, we emit a warning for non-POD
3096 // arguments here.
Jordan Rose448ac3e2012-12-05 18:44:40 +00003097 if (S.isValidVarArgType(ExprTy) == Sema::VAK_Invalid) {
Jordan Rose614a8652012-09-05 22:56:19 +00003098 unsigned DiagKind;
Jordan Rose448ac3e2012-12-05 18:44:40 +00003099 if (ExprTy->isObjCObjectType())
Jordan Rose614a8652012-09-05 22:56:19 +00003100 DiagKind = diag::err_cannot_pass_objc_interface_to_vararg_format;
3101 else
3102 DiagKind = diag::warn_non_pod_vararg_with_format_string;
3103
3104 EmitFormatDiagnostic(
3105 S.PDiag(DiagKind)
Richard Smith80ad52f2013-01-02 11:42:31 +00003106 << S.getLangOpts().CPlusPlus11
Jordan Rose448ac3e2012-12-05 18:44:40 +00003107 << ExprTy
Jordan Rose614a8652012-09-05 22:56:19 +00003108 << CallType
3109 << AT.getRepresentativeTypeName(S.Context)
3110 << CSR
3111 << E->getSourceRange(),
3112 E->getLocStart(), /*IsStringLocation*/false, CSR);
3113
3114 checkForCStrMembers(AT, E, CSR);
3115 } else
Richard Trieu55733de2011-10-28 00:41:25 +00003116 EmitFormatDiagnostic(
3117 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
Jordan Rose448ac3e2012-12-05 18:44:40 +00003118 << AT.getRepresentativeTypeName(S.Context) << ExprTy
Jordan Rose614a8652012-09-05 22:56:19 +00003119 << CSR
Richard Smith831421f2012-06-25 20:30:08 +00003120 << E->getSourceRange(),
Jordan Rose614a8652012-09-05 22:56:19 +00003121 E->getLocStart(), /*IsStringLocation*/false, CSR);
Michael J. Spencer96827eb2010-07-27 04:46:02 +00003122 }
3123
Ted Kremeneke0e53132010-01-28 23:39:18 +00003124 return true;
3125}
3126
Ted Kremenek826a3452010-07-16 02:11:22 +00003127//===--- CHECK: Scanf format string checking ------------------------------===//
3128
3129namespace {
3130class CheckScanfHandler : public CheckFormatHandler {
3131public:
3132 CheckScanfHandler(Sema &s, const StringLiteral *fexpr,
3133 const Expr *origFormatExpr, unsigned firstDataArg,
Jordan Rose50687312012-06-04 23:52:23 +00003134 unsigned numDataArgs, const char *beg, bool hasVAListArg,
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00003135 ArrayRef<const Expr *> Args,
Jordan Roseddcfbc92012-07-19 18:10:23 +00003136 unsigned formatIdx, bool inFunctionCall,
3137 Sema::VariadicCallType CallType)
Ted Kremenek826a3452010-07-16 02:11:22 +00003138 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
Jordan Rose50687312012-06-04 23:52:23 +00003139 numDataArgs, beg, hasVAListArg,
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00003140 Args, formatIdx, inFunctionCall, CallType)
Jordan Roseddcfbc92012-07-19 18:10:23 +00003141 {}
Ted Kremenek826a3452010-07-16 02:11:22 +00003142
3143 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
3144 const char *startSpecifier,
3145 unsigned specifierLen);
Ted Kremenekc09b6a52010-07-19 21:25:57 +00003146
3147 bool HandleInvalidScanfConversionSpecifier(
3148 const analyze_scanf::ScanfSpecifier &FS,
3149 const char *startSpecifier,
3150 unsigned specifierLen);
Ted Kremenekb7c21012010-07-16 18:28:03 +00003151
3152 void HandleIncompleteScanList(const char *start, const char *end);
Ted Kremenek826a3452010-07-16 02:11:22 +00003153};
Ted Kremenek07d161f2010-01-29 01:50:07 +00003154}
Ted Kremeneke0e53132010-01-28 23:39:18 +00003155
Ted Kremenekb7c21012010-07-16 18:28:03 +00003156void CheckScanfHandler::HandleIncompleteScanList(const char *start,
3157 const char *end) {
Richard Trieu55733de2011-10-28 00:41:25 +00003158 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
3159 getLocationOfByte(end), /*IsStringLocation*/true,
3160 getSpecifierRange(start, end - start));
Ted Kremenekb7c21012010-07-16 18:28:03 +00003161}
3162
Ted Kremenekc09b6a52010-07-19 21:25:57 +00003163bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
3164 const analyze_scanf::ScanfSpecifier &FS,
3165 const char *startSpecifier,
3166 unsigned specifierLen) {
3167
Ted Kremenek6ecb9502010-07-20 20:04:27 +00003168 const analyze_scanf::ScanfConversionSpecifier &CS =
Ted Kremenekc09b6a52010-07-19 21:25:57 +00003169 FS.getConversionSpecifier();
3170
3171 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
3172 getLocationOfByte(CS.getStart()),
3173 startSpecifier, specifierLen,
3174 CS.getStart(), CS.getLength());
3175}
3176
Ted Kremenek826a3452010-07-16 02:11:22 +00003177bool CheckScanfHandler::HandleScanfSpecifier(
3178 const analyze_scanf::ScanfSpecifier &FS,
3179 const char *startSpecifier,
3180 unsigned specifierLen) {
3181
3182 using namespace analyze_scanf;
3183 using namespace analyze_format_string;
3184
Ted Kremenek6ecb9502010-07-20 20:04:27 +00003185 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
Ted Kremenek826a3452010-07-16 02:11:22 +00003186
Ted Kremenekbaa40062010-07-19 22:01:06 +00003187 // Handle case where '%' and '*' don't consume an argument. These shouldn't
3188 // be used to decide if we are using positional arguments consistently.
3189 if (FS.consumesDataArgument()) {
3190 if (atFirstArg) {
3191 atFirstArg = false;
3192 usesPositionalArgs = FS.usesPositionalArg();
3193 }
3194 else if (usesPositionalArgs != FS.usesPositionalArg()) {
Richard Trieu55733de2011-10-28 00:41:25 +00003195 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
3196 startSpecifier, specifierLen);
Ted Kremenekbaa40062010-07-19 22:01:06 +00003197 return false;
3198 }
Ted Kremenek826a3452010-07-16 02:11:22 +00003199 }
3200
3201 // Check if the field with is non-zero.
3202 const OptionalAmount &Amt = FS.getFieldWidth();
3203 if (Amt.getHowSpecified() == OptionalAmount::Constant) {
3204 if (Amt.getConstantAmount() == 0) {
3205 const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
3206 Amt.getConstantLength());
Richard Trieu55733de2011-10-28 00:41:25 +00003207 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
3208 getLocationOfByte(Amt.getStart()),
3209 /*IsStringLocation*/true, R,
3210 FixItHint::CreateRemoval(R));
Ted Kremenek826a3452010-07-16 02:11:22 +00003211 }
3212 }
3213
3214 if (!FS.consumesDataArgument()) {
3215 // FIXME: Technically specifying a precision or field width here
3216 // makes no sense. Worth issuing a warning at some point.
3217 return true;
3218 }
3219
3220 // Consume the argument.
3221 unsigned argIndex = FS.getArgIndex();
3222 if (argIndex < NumDataArgs) {
3223 // The check to see if the argIndex is valid will come later.
3224 // We set the bit here because we may exit early from this
3225 // function if we encounter some other error.
3226 CoveredArgs.set(argIndex);
3227 }
3228
Ted Kremenek1e51c202010-07-20 20:04:47 +00003229 // Check the length modifier is valid with the given conversion specifier.
Jordan Rosebbb6bb42012-09-08 04:00:03 +00003230 if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo()))
Jordan Rose8be066e2012-09-08 04:00:12 +00003231 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
3232 diag::warn_format_nonsensical_length);
Jordan Rosebbb6bb42012-09-08 04:00:03 +00003233 else if (!FS.hasStandardLengthModifier())
Jordan Rose8be066e2012-09-08 04:00:12 +00003234 HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen);
Jordan Rosebbb6bb42012-09-08 04:00:03 +00003235 else if (!FS.hasStandardLengthConversionCombination())
Jordan Rose8be066e2012-09-08 04:00:12 +00003236 HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen,
3237 diag::warn_format_non_standard_conversion_spec);
Hans Wennborg76517422012-02-22 10:17:01 +00003238
Jordan Rosebbb6bb42012-09-08 04:00:03 +00003239 if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
3240 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
3241
Ted Kremenek826a3452010-07-16 02:11:22 +00003242 // The remaining checks depend on the data arguments.
3243 if (HasVAListArg)
3244 return true;
3245
Ted Kremenek666a1972010-07-26 19:45:42 +00003246 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
Ted Kremenek826a3452010-07-16 02:11:22 +00003247 return false;
Ted Kremenek826a3452010-07-16 02:11:22 +00003248
Hans Wennborg6fcd9322011-12-10 13:20:11 +00003249 // Check that the argument type matches the format specifier.
3250 const Expr *Ex = getDataArg(argIndex);
Jordan Rose48716662012-07-19 18:10:08 +00003251 if (!Ex)
3252 return true;
3253
Hans Wennborg58e1e542012-08-07 08:59:46 +00003254 const analyze_format_string::ArgType &AT = FS.getArgType(S.Context);
3255 if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) {
Hans Wennborg6fcd9322011-12-10 13:20:11 +00003256 ScanfSpecifier fixedFS = FS;
David Blaikie4e4d0842012-03-11 07:00:24 +00003257 bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(),
Hans Wennborgbe6126a2012-02-15 09:59:46 +00003258 S.Context);
Hans Wennborg6fcd9322011-12-10 13:20:11 +00003259
3260 if (success) {
3261 // Get the fix string from the fixed format specifier.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003262 SmallString<128> buf;
Hans Wennborg6fcd9322011-12-10 13:20:11 +00003263 llvm::raw_svector_ostream os(buf);
3264 fixedFS.toString(os);
3265
3266 EmitFormatDiagnostic(
3267 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
Hans Wennborg58e1e542012-08-07 08:59:46 +00003268 << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
Hans Wennborg6fcd9322011-12-10 13:20:11 +00003269 << Ex->getSourceRange(),
Matt Beaumont-Gayabf145a2012-05-17 00:03:16 +00003270 Ex->getLocStart(),
3271 /*IsStringLocation*/false,
Hans Wennborg6fcd9322011-12-10 13:20:11 +00003272 getSpecifierRange(startSpecifier, specifierLen),
3273 FixItHint::CreateReplacement(
3274 getSpecifierRange(startSpecifier, specifierLen),
3275 os.str()));
3276 } else {
Jean-Daniel Dupas220947b2012-01-31 18:12:08 +00003277 EmitFormatDiagnostic(
3278 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
Hans Wennborg58e1e542012-08-07 08:59:46 +00003279 << AT.getRepresentativeTypeName(S.Context) << Ex->getType()
Jean-Daniel Dupas220947b2012-01-31 18:12:08 +00003280 << Ex->getSourceRange(),
Matt Beaumont-Gayabf145a2012-05-17 00:03:16 +00003281 Ex->getLocStart(),
3282 /*IsStringLocation*/false,
Jean-Daniel Dupas220947b2012-01-31 18:12:08 +00003283 getSpecifierRange(startSpecifier, specifierLen));
Hans Wennborg6fcd9322011-12-10 13:20:11 +00003284 }
3285 }
3286
Ted Kremenek826a3452010-07-16 02:11:22 +00003287 return true;
3288}
3289
3290void Sema::CheckFormatString(const StringLiteral *FExpr,
Ted Kremenek0e5675d2010-02-10 02:16:30 +00003291 const Expr *OrigFormatExpr,
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00003292 ArrayRef<const Expr *> Args,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00003293 bool HasVAListArg, unsigned format_idx,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00003294 unsigned firstDataArg, FormatStringType Type,
Jordan Roseddcfbc92012-07-19 18:10:23 +00003295 bool inFunctionCall, VariadicCallType CallType) {
Ted Kremenek826a3452010-07-16 02:11:22 +00003296
Ted Kremeneke0e53132010-01-28 23:39:18 +00003297 // CHECK: is the format string a wide literal?
Richard Smithdf9ef1b2012-06-13 05:37:23 +00003298 if (!FExpr->isAscii() && !FExpr->isUTF8()) {
Richard Trieu55733de2011-10-28 00:41:25 +00003299 CheckFormatHandler::EmitFormatDiagnostic(
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00003300 *this, inFunctionCall, Args[format_idx],
Richard Trieu55733de2011-10-28 00:41:25 +00003301 PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(),
3302 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
Ted Kremeneke0e53132010-01-28 23:39:18 +00003303 return;
3304 }
Ted Kremenek826a3452010-07-16 02:11:22 +00003305
Ted Kremeneke0e53132010-01-28 23:39:18 +00003306 // Str - The format string. NOTE: this is NOT null-terminated!
Chris Lattner5f9e2722011-07-23 10:55:15 +00003307 StringRef StrRef = FExpr->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +00003308 const char *Str = StrRef.data();
3309 unsigned StrLen = StrRef.size();
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00003310 const unsigned numDataArgs = Args.size() - firstDataArg;
Ted Kremenek826a3452010-07-16 02:11:22 +00003311
Ted Kremeneke0e53132010-01-28 23:39:18 +00003312 // CHECK: empty format string?
Ted Kremenek4cd57912011-09-29 05:52:16 +00003313 if (StrLen == 0 && numDataArgs > 0) {
Richard Trieu55733de2011-10-28 00:41:25 +00003314 CheckFormatHandler::EmitFormatDiagnostic(
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00003315 *this, inFunctionCall, Args[format_idx],
Richard Trieu55733de2011-10-28 00:41:25 +00003316 PDiag(diag::warn_empty_format_string), FExpr->getLocStart(),
3317 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
Ted Kremeneke0e53132010-01-28 23:39:18 +00003318 return;
3319 }
Ted Kremenek826a3452010-07-16 02:11:22 +00003320
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00003321 if (Type == FST_Printf || Type == FST_NSString) {
Ted Kremenek826a3452010-07-16 02:11:22 +00003322 CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
Jordan Rose50687312012-06-04 23:52:23 +00003323 numDataArgs, (Type == FST_NSString),
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00003324 Str, HasVAListArg, Args, format_idx,
Jordan Roseddcfbc92012-07-19 18:10:23 +00003325 inFunctionCall, CallType);
Ted Kremenek826a3452010-07-16 02:11:22 +00003326
Hans Wennborgd02deeb2011-12-15 10:25:47 +00003327 if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
Jordan Rose275b6f52012-09-13 02:11:03 +00003328 getLangOpts(),
3329 Context.getTargetInfo()))
Ted Kremenek826a3452010-07-16 02:11:22 +00003330 H.DoneProcessing();
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00003331 } else if (Type == FST_Scanf) {
Jordan Rose50687312012-06-04 23:52:23 +00003332 CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs,
Dmitri Gribenko1c030e92013-01-13 20:46:02 +00003333 Str, HasVAListArg, Args, format_idx,
Jordan Roseddcfbc92012-07-19 18:10:23 +00003334 inFunctionCall, CallType);
Ted Kremenek826a3452010-07-16 02:11:22 +00003335
Hans Wennborgd02deeb2011-12-15 10:25:47 +00003336 if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen,
Jordan Rose275b6f52012-09-13 02:11:03 +00003337 getLangOpts(),
3338 Context.getTargetInfo()))
Ted Kremenek826a3452010-07-16 02:11:22 +00003339 H.DoneProcessing();
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00003340 } // TODO: handle other formats
Ted Kremenekce7024e2010-01-28 01:18:22 +00003341}
3342
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00003343//===--- CHECK: Standard memory functions ---------------------------------===//
3344
Douglas Gregor2a053a32011-05-03 20:05:22 +00003345/// \brief Determine whether the given type is a dynamic class type (e.g.,
3346/// whether it has a vtable).
3347static bool isDynamicClassType(QualType T) {
3348 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl())
3349 if (CXXRecordDecl *Definition = Record->getDefinition())
3350 if (Definition->isDynamicClass())
3351 return true;
3352
3353 return false;
3354}
3355
Chandler Carrutha72a12f2011-06-21 23:04:20 +00003356/// \brief If E is a sizeof expression, returns its argument expression,
Chandler Carruth000d4282011-06-16 09:09:40 +00003357/// otherwise returns NULL.
3358static const Expr *getSizeOfExprArg(const Expr* E) {
Nico Webere4a1c642011-06-14 16:14:58 +00003359 if (const UnaryExprOrTypeTraitExpr *SizeOf =
Chandler Carruth000d4282011-06-16 09:09:40 +00003360 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
3361 if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType())
3362 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
Nico Webere4a1c642011-06-14 16:14:58 +00003363
Chandler Carruth000d4282011-06-16 09:09:40 +00003364 return 0;
3365}
3366
Chandler Carrutha72a12f2011-06-21 23:04:20 +00003367/// \brief If E is a sizeof expression, returns its argument type.
Chandler Carruth000d4282011-06-16 09:09:40 +00003368static QualType getSizeOfArgType(const Expr* E) {
3369 if (const UnaryExprOrTypeTraitExpr *SizeOf =
3370 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
3371 if (SizeOf->getKind() == clang::UETT_SizeOf)
3372 return SizeOf->getTypeOfArgument();
3373
3374 return QualType();
Nico Webere4a1c642011-06-14 16:14:58 +00003375}
3376
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00003377/// \brief Check for dangerous or invalid arguments to memset().
3378///
Chandler Carruth929f0132011-06-03 06:23:57 +00003379/// This issues warnings on known problematic, dangerous or unspecified
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00003380/// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
3381/// function calls.
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00003382///
3383/// \param Call The call expression to diagnose.
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00003384void Sema::CheckMemaccessArguments(const CallExpr *Call,
Anna Zaks0a151a12012-01-17 00:37:07 +00003385 unsigned BId,
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00003386 IdentifierInfo *FnName) {
Anna Zaks0a151a12012-01-17 00:37:07 +00003387 assert(BId != 0);
3388
Ted Kremenek1d59f7f2011-04-28 01:38:02 +00003389 // It is possible to have a non-standard definition of memset. Validate
Douglas Gregor707a23e2011-06-16 17:56:04 +00003390 // we have enough arguments, and if not, abort further checking.
Anna Zaks0a151a12012-01-17 00:37:07 +00003391 unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3);
Nico Webercda57822011-10-13 22:30:23 +00003392 if (Call->getNumArgs() < ExpectedNumArgs)
Ted Kremenek1d59f7f2011-04-28 01:38:02 +00003393 return;
3394
Anna Zaks0a151a12012-01-17 00:37:07 +00003395 unsigned LastArg = (BId == Builtin::BImemset ||
3396 BId == Builtin::BIstrndup ? 1 : 2);
3397 unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2);
Nico Webercda57822011-10-13 22:30:23 +00003398 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
Chandler Carruth000d4282011-06-16 09:09:40 +00003399
3400 // We have special checking when the length is a sizeof expression.
3401 QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
3402 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
3403 llvm::FoldingSetNodeID SizeOfArgID;
3404
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00003405 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
3406 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
Nico Webere4a1c642011-06-14 16:14:58 +00003407 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00003408
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00003409 QualType DestTy = Dest->getType();
3410 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
3411 QualType PointeeTy = DestPtrTy->getPointeeType();
John McCallf85e1932011-06-15 23:02:42 +00003412
Chandler Carruth000d4282011-06-16 09:09:40 +00003413 // Never warn about void type pointers. This can be used to suppress
3414 // false positives.
3415 if (PointeeTy->isVoidType())
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00003416 continue;
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00003417
Chandler Carruth000d4282011-06-16 09:09:40 +00003418 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
3419 // actually comparing the expressions for equality. Because computing the
3420 // expression IDs can be expensive, we only do this if the diagnostic is
3421 // enabled.
3422 if (SizeOfArg &&
3423 Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess,
3424 SizeOfArg->getExprLoc())) {
3425 // We only compute IDs for expressions if the warning is enabled, and
3426 // cache the sizeof arg's ID.
3427 if (SizeOfArgID == llvm::FoldingSetNodeID())
3428 SizeOfArg->Profile(SizeOfArgID, Context, true);
3429 llvm::FoldingSetNodeID DestID;
3430 Dest->Profile(DestID, Context, true);
3431 if (DestID == SizeOfArgID) {
Nico Webercda57822011-10-13 22:30:23 +00003432 // TODO: For strncpy() and friends, this could suggest sizeof(dst)
3433 // over sizeof(src) as well.
Chandler Carruth000d4282011-06-16 09:09:40 +00003434 unsigned ActionIdx = 0; // Default is to suggest dereferencing.
Anna Zaks6fcb3722012-05-30 00:34:21 +00003435 StringRef ReadableName = FnName->getName();
3436
Chandler Carruth000d4282011-06-16 09:09:40 +00003437 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
Anna Zaks90c78322012-05-30 23:14:52 +00003438 if (UnaryOp->getOpcode() == UO_AddrOf)
Chandler Carruth000d4282011-06-16 09:09:40 +00003439 ActionIdx = 1; // If its an address-of operator, just remove it.
Fariborz Jahanian7adf4172013-01-30 01:12:44 +00003440 if (!PointeeTy->isIncompleteType() &&
3441 (Context.getTypeSize(PointeeTy) == Context.getCharWidth()))
Chandler Carruth000d4282011-06-16 09:09:40 +00003442 ActionIdx = 2; // If the pointee's size is sizeof(char),
3443 // suggest an explicit length.
Anna Zaks6fcb3722012-05-30 00:34:21 +00003444
3445 // If the function is defined as a builtin macro, do not show macro
3446 // expansion.
3447 SourceLocation SL = SizeOfArg->getExprLoc();
3448 SourceRange DSR = Dest->getSourceRange();
3449 SourceRange SSR = SizeOfArg->getSourceRange();
3450 SourceManager &SM = PP.getSourceManager();
3451
3452 if (SM.isMacroArgExpansion(SL)) {
3453 ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts);
3454 SL = SM.getSpellingLoc(SL);
3455 DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()),
3456 SM.getSpellingLoc(DSR.getEnd()));
3457 SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()),
3458 SM.getSpellingLoc(SSR.getEnd()));
3459 }
3460
Anna Zaks90c78322012-05-30 23:14:52 +00003461 DiagRuntimeBehavior(SL, SizeOfArg,
Chandler Carruth000d4282011-06-16 09:09:40 +00003462 PDiag(diag::warn_sizeof_pointer_expr_memaccess)
Anna Zaks6fcb3722012-05-30 00:34:21 +00003463 << ReadableName
Anna Zaks90c78322012-05-30 23:14:52 +00003464 << PointeeTy
3465 << DestTy
Anna Zaks6fcb3722012-05-30 00:34:21 +00003466 << DSR
Anna Zaks90c78322012-05-30 23:14:52 +00003467 << SSR);
3468 DiagRuntimeBehavior(SL, SizeOfArg,
3469 PDiag(diag::warn_sizeof_pointer_expr_memaccess_note)
3470 << ActionIdx
3471 << SSR);
3472
Chandler Carruth000d4282011-06-16 09:09:40 +00003473 break;
3474 }
3475 }
3476
3477 // Also check for cases where the sizeof argument is the exact same
3478 // type as the memory argument, and where it points to a user-defined
3479 // record type.
3480 if (SizeOfArgTy != QualType()) {
3481 if (PointeeTy->isRecordType() &&
3482 Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
3483 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
3484 PDiag(diag::warn_sizeof_pointer_type_memaccess)
3485 << FnName << SizeOfArgTy << ArgIdx
3486 << PointeeTy << Dest->getSourceRange()
3487 << LenExpr->getSourceRange());
3488 break;
3489 }
Nico Webere4a1c642011-06-14 16:14:58 +00003490 }
3491
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00003492 // Always complain about dynamic classes.
Anna Zaks0a151a12012-01-17 00:37:07 +00003493 if (isDynamicClassType(PointeeTy)) {
3494
3495 unsigned OperationType = 0;
3496 // "overwritten" if we're warning about the destination for any call
3497 // but memcmp; otherwise a verb appropriate to the call.
3498 if (ArgIdx != 0 || BId == Builtin::BImemcmp) {
3499 if (BId == Builtin::BImemcpy)
3500 OperationType = 1;
3501 else if(BId == Builtin::BImemmove)
3502 OperationType = 2;
3503 else if (BId == Builtin::BImemcmp)
3504 OperationType = 3;
3505 }
3506
Matt Beaumont-Gay5c5218e2011-08-19 20:40:18 +00003507 DiagRuntimeBehavior(
3508 Dest->getExprLoc(), Dest,
3509 PDiag(diag::warn_dyn_class_memaccess)
Anna Zaks0a151a12012-01-17 00:37:07 +00003510 << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx)
Anna Zaksd9b859a2012-01-13 21:52:01 +00003511 << FnName << PointeeTy
Anna Zaks0a151a12012-01-17 00:37:07 +00003512 << OperationType
Matt Beaumont-Gay5c5218e2011-08-19 20:40:18 +00003513 << Call->getCallee()->getSourceRange());
Anna Zaks0a151a12012-01-17 00:37:07 +00003514 } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
3515 BId != Builtin::BImemset)
Matt Beaumont-Gay5c5218e2011-08-19 20:40:18 +00003516 DiagRuntimeBehavior(
3517 Dest->getExprLoc(), Dest,
3518 PDiag(diag::warn_arc_object_memaccess)
3519 << ArgIdx << FnName << PointeeTy
3520 << Call->getCallee()->getSourceRange());
John McCallf85e1932011-06-15 23:02:42 +00003521 else
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00003522 continue;
John McCallf85e1932011-06-15 23:02:42 +00003523
3524 DiagRuntimeBehavior(
3525 Dest->getExprLoc(), Dest,
Chandler Carruth929f0132011-06-03 06:23:57 +00003526 PDiag(diag::note_bad_memaccess_silence)
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00003527 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
3528 break;
3529 }
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00003530 }
3531}
3532
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00003533// A little helper routine: ignore addition and subtraction of integer literals.
3534// This intentionally does not ignore all integer constant expressions because
3535// we don't want to remove sizeof().
3536static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
3537 Ex = Ex->IgnoreParenCasts();
3538
3539 for (;;) {
3540 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
3541 if (!BO || !BO->isAdditiveOp())
3542 break;
3543
3544 const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
3545 const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
3546
3547 if (isa<IntegerLiteral>(RHS))
3548 Ex = LHS;
3549 else if (isa<IntegerLiteral>(LHS))
3550 Ex = RHS;
3551 else
3552 break;
3553 }
3554
3555 return Ex;
3556}
3557
Anna Zaks0f38ace2012-08-08 21:42:23 +00003558static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty,
3559 ASTContext &Context) {
3560 // Only handle constant-sized or VLAs, but not flexible members.
3561 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) {
3562 // Only issue the FIXIT for arrays of size > 1.
3563 if (CAT->getSize().getSExtValue() <= 1)
3564 return false;
3565 } else if (!Ty->isVariableArrayType()) {
3566 return false;
3567 }
3568 return true;
3569}
3570
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00003571// Warn if the user has made the 'size' argument to strlcpy or strlcat
3572// be the size of the source, instead of the destination.
3573void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
3574 IdentifierInfo *FnName) {
3575
3576 // Don't crash if the user has the wrong number of arguments
3577 if (Call->getNumArgs() != 3)
3578 return;
3579
3580 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
3581 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
3582 const Expr *CompareWithSrc = NULL;
3583
3584 // Look for 'strlcpy(dst, x, sizeof(x))'
3585 if (const Expr *Ex = getSizeOfExprArg(SizeArg))
3586 CompareWithSrc = Ex;
3587 else {
3588 // Look for 'strlcpy(dst, x, strlen(x))'
3589 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
Richard Smith180f4792011-11-10 06:34:14 +00003590 if (SizeCall->isBuiltinCall() == Builtin::BIstrlen
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00003591 && SizeCall->getNumArgs() == 1)
3592 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
3593 }
3594 }
3595
3596 if (!CompareWithSrc)
3597 return;
3598
3599 // Determine if the argument to sizeof/strlen is equal to the source
3600 // argument. In principle there's all kinds of things you could do
3601 // here, for instance creating an == expression and evaluating it with
3602 // EvaluateAsBooleanCondition, but this uses a more direct technique:
3603 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
3604 if (!SrcArgDRE)
3605 return;
3606
3607 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
3608 if (!CompareWithSrcDRE ||
3609 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
3610 return;
3611
3612 const Expr *OriginalSizeArg = Call->getArg(2);
3613 Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size)
3614 << OriginalSizeArg->getSourceRange() << FnName;
3615
3616 // Output a FIXIT hint if the destination is an array (rather than a
3617 // pointer to an array). This could be enhanced to handle some
3618 // pointers if we know the actual size, like if DstArg is 'array+2'
3619 // we could say 'sizeof(array)-2'.
3620 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
Anna Zaks0f38ace2012-08-08 21:42:23 +00003621 if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context))
Ted Kremenek8f746222011-08-18 22:48:41 +00003622 return;
Ted Kremenek8f746222011-08-18 22:48:41 +00003623
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003624 SmallString<128> sizeString;
Ted Kremenek8f746222011-08-18 22:48:41 +00003625 llvm::raw_svector_ostream OS(sizeString);
3626 OS << "sizeof(";
Richard Smithd1420c62012-08-16 03:56:14 +00003627 DstArg->printPretty(OS, 0, getPrintingPolicy());
Ted Kremenek8f746222011-08-18 22:48:41 +00003628 OS << ")";
3629
3630 Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
3631 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
3632 OS.str());
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00003633}
3634
Anna Zaksc36bedc2012-02-01 19:08:57 +00003635/// Check if two expressions refer to the same declaration.
3636static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
3637 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
3638 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
3639 return D1->getDecl() == D2->getDecl();
3640 return false;
3641}
3642
3643static const Expr *getStrlenExprArg(const Expr *E) {
3644 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
3645 const FunctionDecl *FD = CE->getDirectCallee();
3646 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
3647 return 0;
3648 return CE->getArg(0)->IgnoreParenCasts();
3649 }
3650 return 0;
3651}
3652
3653// Warn on anti-patterns as the 'size' argument to strncat.
3654// The correct size argument should look like following:
3655// strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
3656void Sema::CheckStrncatArguments(const CallExpr *CE,
3657 IdentifierInfo *FnName) {
3658 // Don't crash if the user has the wrong number of arguments.
3659 if (CE->getNumArgs() < 3)
3660 return;
3661 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
3662 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
3663 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
3664
3665 // Identify common expressions, which are wrongly used as the size argument
3666 // to strncat and may lead to buffer overflows.
3667 unsigned PatternType = 0;
3668 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
3669 // - sizeof(dst)
3670 if (referToTheSameDecl(SizeOfArg, DstArg))
3671 PatternType = 1;
3672 // - sizeof(src)
3673 else if (referToTheSameDecl(SizeOfArg, SrcArg))
3674 PatternType = 2;
3675 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
3676 if (BE->getOpcode() == BO_Sub) {
3677 const Expr *L = BE->getLHS()->IgnoreParenCasts();
3678 const Expr *R = BE->getRHS()->IgnoreParenCasts();
3679 // - sizeof(dst) - strlen(dst)
3680 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
3681 referToTheSameDecl(DstArg, getStrlenExprArg(R)))
3682 PatternType = 1;
3683 // - sizeof(src) - (anything)
3684 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
3685 PatternType = 2;
3686 }
3687 }
3688
3689 if (PatternType == 0)
3690 return;
3691
Anna Zaksafdb0412012-02-03 01:27:37 +00003692 // Generate the diagnostic.
3693 SourceLocation SL = LenArg->getLocStart();
3694 SourceRange SR = LenArg->getSourceRange();
3695 SourceManager &SM = PP.getSourceManager();
3696
3697 // If the function is defined as a builtin macro, do not show macro expansion.
3698 if (SM.isMacroArgExpansion(SL)) {
3699 SL = SM.getSpellingLoc(SL);
3700 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
3701 SM.getSpellingLoc(SR.getEnd()));
3702 }
3703
Anna Zaks0f38ace2012-08-08 21:42:23 +00003704 // Check if the destination is an array (rather than a pointer to an array).
3705 QualType DstTy = DstArg->getType();
3706 bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy,
3707 Context);
3708 if (!isKnownSizeArray) {
3709 if (PatternType == 1)
3710 Diag(SL, diag::warn_strncat_wrong_size) << SR;
3711 else
3712 Diag(SL, diag::warn_strncat_src_size) << SR;
3713 return;
3714 }
3715
Anna Zaksc36bedc2012-02-01 19:08:57 +00003716 if (PatternType == 1)
Anna Zaksafdb0412012-02-03 01:27:37 +00003717 Diag(SL, diag::warn_strncat_large_size) << SR;
Anna Zaksc36bedc2012-02-01 19:08:57 +00003718 else
Anna Zaksafdb0412012-02-03 01:27:37 +00003719 Diag(SL, diag::warn_strncat_src_size) << SR;
Anna Zaksc36bedc2012-02-01 19:08:57 +00003720
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003721 SmallString<128> sizeString;
Anna Zaksc36bedc2012-02-01 19:08:57 +00003722 llvm::raw_svector_ostream OS(sizeString);
3723 OS << "sizeof(";
Richard Smithd1420c62012-08-16 03:56:14 +00003724 DstArg->printPretty(OS, 0, getPrintingPolicy());
Anna Zaksc36bedc2012-02-01 19:08:57 +00003725 OS << ") - ";
3726 OS << "strlen(";
Richard Smithd1420c62012-08-16 03:56:14 +00003727 DstArg->printPretty(OS, 0, getPrintingPolicy());
Anna Zaksc36bedc2012-02-01 19:08:57 +00003728 OS << ") - 1";
3729
Anna Zaksafdb0412012-02-03 01:27:37 +00003730 Diag(SL, diag::note_strncat_wrong_size)
3731 << FixItHint::CreateReplacement(SR, OS.str());
Anna Zaksc36bedc2012-02-01 19:08:57 +00003732}
3733
Ted Kremenek06de2762007-08-17 16:46:58 +00003734//===--- CHECK: Return Address of Stack Variable --------------------------===//
3735
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003736static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
3737 Decl *ParentDecl);
3738static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars,
3739 Decl *ParentDecl);
Ted Kremenek06de2762007-08-17 16:46:58 +00003740
3741/// CheckReturnStackAddr - Check if a return statement returns the address
3742/// of a stack variable.
3743void
3744Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
3745 SourceLocation ReturnLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00003746
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003747 Expr *stackE = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003748 SmallVector<DeclRefExpr *, 8> refVars;
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003749
3750 // Perform checking for returned stack addresses, local blocks,
3751 // label addresses or references to temporaries.
John McCallf85e1932011-06-15 23:02:42 +00003752 if (lhsType->isPointerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003753 (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) {
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003754 stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/0);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00003755 } else if (lhsType->isReferenceType()) {
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003756 stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/0);
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003757 }
3758
3759 if (stackE == 0)
3760 return; // Nothing suspicious was found.
3761
3762 SourceLocation diagLoc;
3763 SourceRange diagRange;
3764 if (refVars.empty()) {
3765 diagLoc = stackE->getLocStart();
3766 diagRange = stackE->getSourceRange();
3767 } else {
3768 // We followed through a reference variable. 'stackE' contains the
3769 // problematic expression but we will warn at the return statement pointing
3770 // at the reference variable. We will later display the "trail" of
3771 // reference variables using notes.
3772 diagLoc = refVars[0]->getLocStart();
3773 diagRange = refVars[0]->getSourceRange();
3774 }
3775
3776 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var.
3777 Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref
3778 : diag::warn_ret_stack_addr)
3779 << DR->getDecl()->getDeclName() << diagRange;
3780 } else if (isa<BlockExpr>(stackE)) { // local block.
3781 Diag(diagLoc, diag::err_ret_local_block) << diagRange;
3782 } else if (isa<AddrLabelExpr>(stackE)) { // address of label.
3783 Diag(diagLoc, diag::warn_ret_addr_label) << diagRange;
3784 } else { // local temporary.
3785 Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref
3786 : diag::warn_ret_local_temp_addr)
3787 << diagRange;
3788 }
3789
3790 // Display the "trail" of reference variables that we followed until we
3791 // found the problematic expression using notes.
3792 for (unsigned i = 0, e = refVars.size(); i != e; ++i) {
3793 VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl());
3794 // If this var binds to another reference var, show the range of the next
3795 // var, otherwise the var binds to the problematic expression, in which case
3796 // show the range of the expression.
3797 SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange()
3798 : stackE->getSourceRange();
3799 Diag(VD->getLocation(), diag::note_ref_var_local_bind)
3800 << VD->getDeclName() << range;
Ted Kremenek06de2762007-08-17 16:46:58 +00003801 }
3802}
3803
3804/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
3805/// check if the expression in a return statement evaluates to an address
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003806/// to a location on the stack, a local block, an address of a label, or a
3807/// reference to local temporary. The recursion is used to traverse the
Ted Kremenek06de2762007-08-17 16:46:58 +00003808/// AST of the return expression, with recursion backtracking when we
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003809/// encounter a subexpression that (1) clearly does not lead to one of the
3810/// above problematic expressions (2) is something we cannot determine leads to
3811/// a problematic expression based on such local checking.
3812///
3813/// Both EvalAddr and EvalVal follow through reference variables to evaluate
3814/// the expression that they point to. Such variables are added to the
3815/// 'refVars' vector so that we know what the reference variable "trail" was.
Ted Kremenek06de2762007-08-17 16:46:58 +00003816///
Ted Kremeneke8c600f2007-08-28 17:02:55 +00003817/// EvalAddr processes expressions that are pointers that are used as
3818/// references (and not L-values). EvalVal handles all other values.
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003819/// At the base case of the recursion is a check for the above problematic
3820/// expressions.
Ted Kremenek06de2762007-08-17 16:46:58 +00003821///
3822/// This implementation handles:
3823///
3824/// * pointer-to-pointer casts
3825/// * implicit conversions from array references to pointers
3826/// * taking the address of fields
3827/// * arbitrary interplay between "&" and "*" operators
3828/// * pointer arithmetic from an address of a stack variable
3829/// * taking the address of an array element where the array is on the stack
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003830static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
3831 Decl *ParentDecl) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003832 if (E->isTypeDependent())
3833 return NULL;
3834
Ted Kremenek06de2762007-08-17 16:46:58 +00003835 // We should only be called for evaluating pointer expressions.
David Chisnall0f436562009-08-17 16:35:33 +00003836 assert((E->getType()->isAnyPointerType() ||
Steve Naroffdd972f22008-09-05 22:11:13 +00003837 E->getType()->isBlockPointerType() ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003838 E->getType()->isObjCQualifiedIdType()) &&
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003839 "EvalAddr only works on pointers");
Mike Stump1eb44332009-09-09 15:08:12 +00003840
Peter Collingbournef111d932011-04-15 00:35:48 +00003841 E = E->IgnoreParens();
3842
Ted Kremenek06de2762007-08-17 16:46:58 +00003843 // Our "symbolic interpreter" is just a dispatch off the currently
3844 // viewed AST node. We then recursively traverse the AST by calling
3845 // EvalAddr and EvalVal appropriately.
3846 switch (E->getStmtClass()) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003847 case Stmt::DeclRefExprClass: {
3848 DeclRefExpr *DR = cast<DeclRefExpr>(E);
3849
3850 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
3851 // If this is a reference variable, follow through to the expression that
3852 // it points to.
3853 if (V->hasLocalStorage() &&
3854 V->getType()->isReferenceType() && V->hasInit()) {
3855 // Add the reference variable to the "trail".
3856 refVars.push_back(DR);
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003857 return EvalAddr(V->getInit(), refVars, ParentDecl);
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003858 }
3859
3860 return NULL;
3861 }
Ted Kremenek06de2762007-08-17 16:46:58 +00003862
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003863 case Stmt::UnaryOperatorClass: {
3864 // The only unary operator that make sense to handle here
3865 // is AddrOf. All others don't make sense as pointers.
3866 UnaryOperator *U = cast<UnaryOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003867
John McCall2de56d12010-08-25 11:45:40 +00003868 if (U->getOpcode() == UO_AddrOf)
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003869 return EvalVal(U->getSubExpr(), refVars, ParentDecl);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003870 else
Ted Kremenek06de2762007-08-17 16:46:58 +00003871 return NULL;
3872 }
Mike Stump1eb44332009-09-09 15:08:12 +00003873
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003874 case Stmt::BinaryOperatorClass: {
3875 // Handle pointer arithmetic. All other binary operators are not valid
3876 // in this context.
3877 BinaryOperator *B = cast<BinaryOperator>(E);
John McCall2de56d12010-08-25 11:45:40 +00003878 BinaryOperatorKind op = B->getOpcode();
Mike Stump1eb44332009-09-09 15:08:12 +00003879
John McCall2de56d12010-08-25 11:45:40 +00003880 if (op != BO_Add && op != BO_Sub)
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003881 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00003882
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003883 Expr *Base = B->getLHS();
3884
3885 // Determine which argument is the real pointer base. It could be
3886 // the RHS argument instead of the LHS.
3887 if (!Base->getType()->isPointerType()) Base = B->getRHS();
Mike Stump1eb44332009-09-09 15:08:12 +00003888
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003889 assert (Base->getType()->isPointerType());
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003890 return EvalAddr(Base, refVars, ParentDecl);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003891 }
Steve Naroff61f40a22008-09-10 19:17:48 +00003892
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003893 // For conditional operators we need to see if either the LHS or RHS are
3894 // valid DeclRefExpr*s. If one of them is valid, we return it.
3895 case Stmt::ConditionalOperatorClass: {
3896 ConditionalOperator *C = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003897
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003898 // Handle the GNU extension for missing LHS.
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00003899 if (Expr *lhsExpr = C->getLHS()) {
3900 // In C++, we can have a throw-expression, which has 'void' type.
3901 if (!lhsExpr->getType()->isVoidType())
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003902 if (Expr* LHS = EvalAddr(lhsExpr, refVars, ParentDecl))
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00003903 return LHS;
3904 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003905
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00003906 // In C++, we can have a throw-expression, which has 'void' type.
3907 if (C->getRHS()->getType()->isVoidType())
3908 return NULL;
3909
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003910 return EvalAddr(C->getRHS(), refVars, ParentDecl);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003911 }
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003912
3913 case Stmt::BlockExprClass:
John McCall469a1eb2011-02-02 13:00:07 +00003914 if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003915 return E; // local block.
3916 return NULL;
3917
3918 case Stmt::AddrLabelExprClass:
3919 return E; // address of label.
Mike Stump1eb44332009-09-09 15:08:12 +00003920
John McCall80ee6e82011-11-10 05:35:25 +00003921 case Stmt::ExprWithCleanupsClass:
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003922 return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,
3923 ParentDecl);
John McCall80ee6e82011-11-10 05:35:25 +00003924
Ted Kremenek54b52742008-08-07 00:49:01 +00003925 // For casts, we need to handle conversions from arrays to
3926 // pointer values, and pointer-to-pointer conversions.
Douglas Gregor49badde2008-10-27 19:41:14 +00003927 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00003928 case Stmt::CStyleCastExprClass:
John McCallf85e1932011-06-15 23:02:42 +00003929 case Stmt::CXXFunctionalCastExprClass:
Eli Friedman8b9414e2012-02-23 23:04:32 +00003930 case Stmt::ObjCBridgedCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00003931 case Stmt::CXXStaticCastExprClass:
3932 case Stmt::CXXDynamicCastExprClass:
Douglas Gregor49badde2008-10-27 19:41:14 +00003933 case Stmt::CXXConstCastExprClass:
3934 case Stmt::CXXReinterpretCastExprClass: {
Eli Friedman8b9414e2012-02-23 23:04:32 +00003935 Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
3936 switch (cast<CastExpr>(E)->getCastKind()) {
3937 case CK_BitCast:
3938 case CK_LValueToRValue:
3939 case CK_NoOp:
3940 case CK_BaseToDerived:
3941 case CK_DerivedToBase:
3942 case CK_UncheckedDerivedToBase:
3943 case CK_Dynamic:
3944 case CK_CPointerToObjCPointerCast:
3945 case CK_BlockPointerToObjCPointerCast:
3946 case CK_AnyPointerToBlockPointerCast:
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003947 return EvalAddr(SubExpr, refVars, ParentDecl);
Eli Friedman8b9414e2012-02-23 23:04:32 +00003948
3949 case CK_ArrayToPointerDecay:
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003950 return EvalVal(SubExpr, refVars, ParentDecl);
Eli Friedman8b9414e2012-02-23 23:04:32 +00003951
3952 default:
3953 return 0;
3954 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003955 }
Mike Stump1eb44332009-09-09 15:08:12 +00003956
Douglas Gregor03e80032011-06-21 17:03:29 +00003957 case Stmt::MaterializeTemporaryExprClass:
3958 if (Expr *Result = EvalAddr(
3959 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003960 refVars, ParentDecl))
Douglas Gregor03e80032011-06-21 17:03:29 +00003961 return Result;
3962
3963 return E;
3964
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003965 // Everything else: we simply don't reason about them.
3966 default:
3967 return NULL;
3968 }
Ted Kremenek06de2762007-08-17 16:46:58 +00003969}
Mike Stump1eb44332009-09-09 15:08:12 +00003970
Ted Kremenek06de2762007-08-17 16:46:58 +00003971
3972/// EvalVal - This function is complements EvalAddr in the mutual recursion.
3973/// See the comments for EvalAddr for more details.
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003974static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars,
3975 Decl *ParentDecl) {
Ted Kremenek68957a92010-08-04 20:01:07 +00003976do {
Ted Kremeneke8c600f2007-08-28 17:02:55 +00003977 // We should only be called for evaluating non-pointer expressions, or
3978 // expressions with a pointer type that are not used as references but instead
3979 // are l-values (e.g., DeclRefExpr with a pointer type).
Mike Stump1eb44332009-09-09 15:08:12 +00003980
Ted Kremenek06de2762007-08-17 16:46:58 +00003981 // Our "symbolic interpreter" is just a dispatch off the currently
3982 // viewed AST node. We then recursively traverse the AST by calling
3983 // EvalAddr and EvalVal appropriately.
Peter Collingbournef111d932011-04-15 00:35:48 +00003984
3985 E = E->IgnoreParens();
Ted Kremenek06de2762007-08-17 16:46:58 +00003986 switch (E->getStmtClass()) {
Ted Kremenek68957a92010-08-04 20:01:07 +00003987 case Stmt::ImplicitCastExprClass: {
3988 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
John McCall5baba9d2010-08-25 10:28:54 +00003989 if (IE->getValueKind() == VK_LValue) {
Ted Kremenek68957a92010-08-04 20:01:07 +00003990 E = IE->getSubExpr();
3991 continue;
3992 }
3993 return NULL;
3994 }
3995
John McCall80ee6e82011-11-10 05:35:25 +00003996 case Stmt::ExprWithCleanupsClass:
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00003997 return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl);
John McCall80ee6e82011-11-10 05:35:25 +00003998
Douglas Gregora2813ce2009-10-23 18:54:35 +00003999 case Stmt::DeclRefExprClass: {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00004000 // When we hit a DeclRefExpr we are looking at code that refers to a
4001 // variable's name. If it's not a reference variable we check if it has
4002 // local storage within the function, and if so, return the expression.
Ted Kremenek06de2762007-08-17 16:46:58 +00004003 DeclRefExpr *DR = cast<DeclRefExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00004004
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004005 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) {
4006 // Check if it refers to itself, e.g. "int& i = i;".
4007 if (V == ParentDecl)
4008 return DR;
4009
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00004010 if (V->hasLocalStorage()) {
4011 if (!V->getType()->isReferenceType())
4012 return DR;
4013
4014 // Reference variable, follow through to the expression that
4015 // it points to.
4016 if (V->hasInit()) {
4017 // Add the reference variable to the "trail".
4018 refVars.push_back(DR);
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004019 return EvalVal(V->getInit(), refVars, V);
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00004020 }
4021 }
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004022 }
Mike Stump1eb44332009-09-09 15:08:12 +00004023
Ted Kremenek06de2762007-08-17 16:46:58 +00004024 return NULL;
4025 }
Mike Stump1eb44332009-09-09 15:08:12 +00004026
Ted Kremenek06de2762007-08-17 16:46:58 +00004027 case Stmt::UnaryOperatorClass: {
4028 // The only unary operator that make sense to handle here
4029 // is Deref. All others don't resolve to a "name." This includes
4030 // handling all sorts of rvalues passed to a unary operator.
4031 UnaryOperator *U = cast<UnaryOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00004032
John McCall2de56d12010-08-25 11:45:40 +00004033 if (U->getOpcode() == UO_Deref)
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004034 return EvalAddr(U->getSubExpr(), refVars, ParentDecl);
Ted Kremenek06de2762007-08-17 16:46:58 +00004035
4036 return NULL;
4037 }
Mike Stump1eb44332009-09-09 15:08:12 +00004038
Ted Kremenek06de2762007-08-17 16:46:58 +00004039 case Stmt::ArraySubscriptExprClass: {
4040 // Array subscripts are potential references to data on the stack. We
4041 // retrieve the DeclRefExpr* for the array variable if it indeed
4042 // has local storage.
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004043 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl);
Ted Kremenek06de2762007-08-17 16:46:58 +00004044 }
Mike Stump1eb44332009-09-09 15:08:12 +00004045
Ted Kremenek06de2762007-08-17 16:46:58 +00004046 case Stmt::ConditionalOperatorClass: {
4047 // For conditional operators we need to see if either the LHS or RHS are
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00004048 // non-NULL Expr's. If one is non-NULL, we return it.
Ted Kremenek06de2762007-08-17 16:46:58 +00004049 ConditionalOperator *C = cast<ConditionalOperator>(E);
4050
Anders Carlsson39073232007-11-30 19:04:31 +00004051 // Handle the GNU extension for missing LHS.
4052 if (Expr *lhsExpr = C->getLHS())
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004053 if (Expr *LHS = EvalVal(lhsExpr, refVars, ParentDecl))
Anders Carlsson39073232007-11-30 19:04:31 +00004054 return LHS;
4055
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004056 return EvalVal(C->getRHS(), refVars, ParentDecl);
Ted Kremenek06de2762007-08-17 16:46:58 +00004057 }
Mike Stump1eb44332009-09-09 15:08:12 +00004058
Ted Kremenek06de2762007-08-17 16:46:58 +00004059 // Accesses to members are potential references to data on the stack.
Douglas Gregor83f6faf2009-08-31 23:41:50 +00004060 case Stmt::MemberExprClass: {
Ted Kremenek06de2762007-08-17 16:46:58 +00004061 MemberExpr *M = cast<MemberExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00004062
Ted Kremenek06de2762007-08-17 16:46:58 +00004063 // Check for indirect access. We only want direct field accesses.
Ted Kremeneka423e812010-09-02 01:12:13 +00004064 if (M->isArrow())
Ted Kremenek06de2762007-08-17 16:46:58 +00004065 return NULL;
Ted Kremeneka423e812010-09-02 01:12:13 +00004066
4067 // Check whether the member type is itself a reference, in which case
4068 // we're not going to refer to the member, but to what the member refers to.
4069 if (M->getMemberDecl()->getType()->isReferenceType())
4070 return NULL;
4071
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004072 return EvalVal(M->getBase(), refVars, ParentDecl);
Ted Kremenek06de2762007-08-17 16:46:58 +00004073 }
Mike Stump1eb44332009-09-09 15:08:12 +00004074
Douglas Gregor03e80032011-06-21 17:03:29 +00004075 case Stmt::MaterializeTemporaryExprClass:
4076 if (Expr *Result = EvalVal(
4077 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
Argyrios Kyrtzidise720ce72012-04-30 23:23:55 +00004078 refVars, ParentDecl))
Douglas Gregor03e80032011-06-21 17:03:29 +00004079 return Result;
4080
4081 return E;
4082
Ted Kremenek06de2762007-08-17 16:46:58 +00004083 default:
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00004084 // Check that we don't return or take the address of a reference to a
4085 // temporary. This is only useful in C++.
4086 if (!E->isTypeDependent() && E->isRValue())
4087 return E;
4088
4089 // Everything else: we simply don't reason about them.
Ted Kremenek06de2762007-08-17 16:46:58 +00004090 return NULL;
4091 }
Ted Kremenek68957a92010-08-04 20:01:07 +00004092} while (true);
Ted Kremenek06de2762007-08-17 16:46:58 +00004093}
Ted Kremenek588e5eb2007-11-25 00:58:00 +00004094
4095//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
4096
4097/// Check for comparisons of floating point operands using != and ==.
4098/// Issue a warning if these are no self-comparisons, as they are not likely
4099/// to do what the programmer intended.
Richard Trieudd225092011-09-15 21:56:47 +00004100void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
Richard Trieudd225092011-09-15 21:56:47 +00004101 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
4102 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
Ted Kremenek588e5eb2007-11-25 00:58:00 +00004103
4104 // Special case: check for x == x (which is OK).
4105 // Do not emit warnings for such cases.
4106 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
4107 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
4108 if (DRL->getDecl() == DRR->getDecl())
David Blaikie980343b2012-07-16 20:47:22 +00004109 return;
Mike Stump1eb44332009-09-09 15:08:12 +00004110
4111
Ted Kremenek1b500bb2007-11-29 00:59:04 +00004112 // Special case: check for comparisons against literals that can be exactly
4113 // represented by APFloat. In such cases, do not emit a warning. This
4114 // is a heuristic: often comparison against such literals are used to
4115 // detect if a value in a variable has not changed. This clearly can
4116 // lead to false negatives.
David Blaikie980343b2012-07-16 20:47:22 +00004117 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
4118 if (FLL->isExact())
4119 return;
4120 } else
4121 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen))
4122 if (FLR->isExact())
4123 return;
Mike Stump1eb44332009-09-09 15:08:12 +00004124
Ted Kremenek588e5eb2007-11-25 00:58:00 +00004125 // Check for comparisons with builtin types.
David Blaikie980343b2012-07-16 20:47:22 +00004126 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
4127 if (CL->isBuiltinCall())
4128 return;
Mike Stump1eb44332009-09-09 15:08:12 +00004129
David Blaikie980343b2012-07-16 20:47:22 +00004130 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
4131 if (CR->isBuiltinCall())
4132 return;
Mike Stump1eb44332009-09-09 15:08:12 +00004133
Ted Kremenek588e5eb2007-11-25 00:58:00 +00004134 // Emit the diagnostic.
David Blaikie980343b2012-07-16 20:47:22 +00004135 Diag(Loc, diag::warn_floatingpoint_eq)
4136 << LHS->getSourceRange() << RHS->getSourceRange();
Ted Kremenek588e5eb2007-11-25 00:58:00 +00004137}
John McCallba26e582010-01-04 23:21:16 +00004138
John McCallf2370c92010-01-06 05:24:50 +00004139//===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
4140//===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
John McCallba26e582010-01-04 23:21:16 +00004141
John McCallf2370c92010-01-06 05:24:50 +00004142namespace {
John McCallba26e582010-01-04 23:21:16 +00004143
John McCallf2370c92010-01-06 05:24:50 +00004144/// Structure recording the 'active' range of an integer-valued
4145/// expression.
4146struct IntRange {
4147 /// The number of bits active in the int.
4148 unsigned Width;
John McCallba26e582010-01-04 23:21:16 +00004149
John McCallf2370c92010-01-06 05:24:50 +00004150 /// True if the int is known not to have negative values.
4151 bool NonNegative;
John McCallba26e582010-01-04 23:21:16 +00004152
John McCallf2370c92010-01-06 05:24:50 +00004153 IntRange(unsigned Width, bool NonNegative)
4154 : Width(Width), NonNegative(NonNegative)
4155 {}
John McCallba26e582010-01-04 23:21:16 +00004156
John McCall1844a6e2010-11-10 23:38:19 +00004157 /// Returns the range of the bool type.
John McCallf2370c92010-01-06 05:24:50 +00004158 static IntRange forBoolType() {
4159 return IntRange(1, true);
John McCall51313c32010-01-04 23:31:57 +00004160 }
4161
John McCall1844a6e2010-11-10 23:38:19 +00004162 /// Returns the range of an opaque value of the given integral type.
4163 static IntRange forValueOfType(ASTContext &C, QualType T) {
4164 return forValueOfCanonicalType(C,
4165 T->getCanonicalTypeInternal().getTypePtr());
John McCall51313c32010-01-04 23:31:57 +00004166 }
4167
John McCall1844a6e2010-11-10 23:38:19 +00004168 /// Returns the range of an opaque value of a canonical integral type.
4169 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
John McCallf2370c92010-01-06 05:24:50 +00004170 assert(T->isCanonicalUnqualified());
4171
4172 if (const VectorType *VT = dyn_cast<VectorType>(T))
4173 T = VT->getElementType().getTypePtr();
4174 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
4175 T = CT->getElementType().getTypePtr();
John McCall323ed742010-05-06 08:58:33 +00004176
David Majnemerf9eaf982013-06-07 22:07:20 +00004177 // For enum types, use the known bit width of the enumerators.
John McCall323ed742010-05-06 08:58:33 +00004178 if (const EnumType *ET = dyn_cast<EnumType>(T)) {
David Majnemerf9eaf982013-06-07 22:07:20 +00004179 EnumDecl *Enum = ET->getDecl();
4180 if (!Enum->isCompleteDefinition())
4181 return IntRange(C.getIntWidth(QualType(T, 0)), false);
John McCall091f23f2010-11-09 22:22:12 +00004182
David Majnemerf9eaf982013-06-07 22:07:20 +00004183 unsigned NumPositive = Enum->getNumPositiveBits();
4184 unsigned NumNegative = Enum->getNumNegativeBits();
John McCall323ed742010-05-06 08:58:33 +00004185
David Majnemerf9eaf982013-06-07 22:07:20 +00004186 if (NumNegative == 0)
4187 return IntRange(NumPositive, true/*NonNegative*/);
4188 else
4189 return IntRange(std::max(NumPositive + 1, NumNegative),
4190 false/*NonNegative*/);
John McCall323ed742010-05-06 08:58:33 +00004191 }
John McCallf2370c92010-01-06 05:24:50 +00004192
4193 const BuiltinType *BT = cast<BuiltinType>(T);
4194 assert(BT->isInteger());
4195
4196 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
4197 }
4198
John McCall1844a6e2010-11-10 23:38:19 +00004199 /// Returns the "target" range of a canonical integral type, i.e.
4200 /// the range of values expressible in the type.
4201 ///
4202 /// This matches forValueOfCanonicalType except that enums have the
4203 /// full range of their type, not the range of their enumerators.
4204 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
4205 assert(T->isCanonicalUnqualified());
4206
4207 if (const VectorType *VT = dyn_cast<VectorType>(T))
4208 T = VT->getElementType().getTypePtr();
4209 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
4210 T = CT->getElementType().getTypePtr();
4211 if (const EnumType *ET = dyn_cast<EnumType>(T))
Douglas Gregor69ff26b2011-09-08 23:29:05 +00004212 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
John McCall1844a6e2010-11-10 23:38:19 +00004213
4214 const BuiltinType *BT = cast<BuiltinType>(T);
4215 assert(BT->isInteger());
4216
4217 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
4218 }
4219
4220 /// Returns the supremum of two ranges: i.e. their conservative merge.
John McCallc0cd21d2010-02-23 19:22:29 +00004221 static IntRange join(IntRange L, IntRange R) {
John McCallf2370c92010-01-06 05:24:50 +00004222 return IntRange(std::max(L.Width, R.Width),
John McCall60fad452010-01-06 22:07:33 +00004223 L.NonNegative && R.NonNegative);
4224 }
4225
John McCall1844a6e2010-11-10 23:38:19 +00004226 /// Returns the infinum of two ranges: i.e. their aggressive merge.
John McCallc0cd21d2010-02-23 19:22:29 +00004227 static IntRange meet(IntRange L, IntRange R) {
John McCall60fad452010-01-06 22:07:33 +00004228 return IntRange(std::min(L.Width, R.Width),
4229 L.NonNegative || R.NonNegative);
John McCallf2370c92010-01-06 05:24:50 +00004230 }
4231};
4232
Ted Kremenek0692a192012-01-31 05:37:37 +00004233static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
4234 unsigned MaxWidth) {
John McCallf2370c92010-01-06 05:24:50 +00004235 if (value.isSigned() && value.isNegative())
4236 return IntRange(value.getMinSignedBits(), false);
4237
4238 if (value.getBitWidth() > MaxWidth)
Jay Foad9f71a8f2010-12-07 08:25:34 +00004239 value = value.trunc(MaxWidth);
John McCallf2370c92010-01-06 05:24:50 +00004240
4241 // isNonNegative() just checks the sign bit without considering
4242 // signedness.
4243 return IntRange(value.getActiveBits(), true);
4244}
4245
Ted Kremenek0692a192012-01-31 05:37:37 +00004246static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
4247 unsigned MaxWidth) {
John McCallf2370c92010-01-06 05:24:50 +00004248 if (result.isInt())
4249 return GetValueRange(C, result.getInt(), MaxWidth);
4250
4251 if (result.isVector()) {
John McCall0acc3112010-01-06 22:57:21 +00004252 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
4253 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
4254 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
4255 R = IntRange::join(R, El);
4256 }
John McCallf2370c92010-01-06 05:24:50 +00004257 return R;
4258 }
4259
4260 if (result.isComplexInt()) {
4261 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
4262 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
4263 return IntRange::join(R, I);
John McCall51313c32010-01-04 23:31:57 +00004264 }
4265
4266 // This can happen with lossless casts to intptr_t of "based" lvalues.
4267 // Assume it might use arbitrary bits.
John McCall0acc3112010-01-06 22:57:21 +00004268 // FIXME: The only reason we need to pass the type in here is to get
4269 // the sign right on this one case. It would be nice if APValue
4270 // preserved this.
Eli Friedman65639282012-01-04 23:13:47 +00004271 assert(result.isLValue() || result.isAddrLabelDiff());
Douglas Gregor5e9ebb32011-05-21 16:28:01 +00004272 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
John McCall51313c32010-01-04 23:31:57 +00004273}
John McCallf2370c92010-01-06 05:24:50 +00004274
Eli Friedman09bddcf2013-07-08 20:20:06 +00004275static QualType GetExprType(Expr *E) {
4276 QualType Ty = E->getType();
4277 if (const AtomicType *AtomicRHS = Ty->getAs<AtomicType>())
4278 Ty = AtomicRHS->getValueType();
4279 return Ty;
4280}
4281
John McCallf2370c92010-01-06 05:24:50 +00004282/// Pseudo-evaluate the given integer expression, estimating the
4283/// range of values it might take.
4284///
4285/// \param MaxWidth - the width to which the value will be truncated
Ted Kremenek0692a192012-01-31 05:37:37 +00004286static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) {
John McCallf2370c92010-01-06 05:24:50 +00004287 E = E->IgnoreParens();
4288
4289 // Try a full evaluation first.
4290 Expr::EvalResult result;
Richard Smith51f47082011-10-29 00:50:52 +00004291 if (E->EvaluateAsRValue(result, C))
Eli Friedman09bddcf2013-07-08 20:20:06 +00004292 return GetValueRange(C, result.Val, GetExprType(E), MaxWidth);
John McCallf2370c92010-01-06 05:24:50 +00004293
4294 // I think we only want to look through implicit casts here; if the
4295 // user has an explicit widening cast, we should treat the value as
4296 // being of the new, wider type.
4297 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
Eli Friedmanb17ee5b2011-12-15 02:41:52 +00004298 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
John McCallf2370c92010-01-06 05:24:50 +00004299 return GetExprRange(C, CE->getSubExpr(), MaxWidth);
4300
Eli Friedman09bddcf2013-07-08 20:20:06 +00004301 IntRange OutputTypeRange = IntRange::forValueOfType(C, GetExprType(CE));
John McCallf2370c92010-01-06 05:24:50 +00004302
John McCall2de56d12010-08-25 11:45:40 +00004303 bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast);
John McCall60fad452010-01-06 22:07:33 +00004304
John McCallf2370c92010-01-06 05:24:50 +00004305 // Assume that non-integer casts can span the full range of the type.
John McCall60fad452010-01-06 22:07:33 +00004306 if (!isIntegerCast)
John McCallf2370c92010-01-06 05:24:50 +00004307 return OutputTypeRange;
4308
4309 IntRange SubRange
4310 = GetExprRange(C, CE->getSubExpr(),
4311 std::min(MaxWidth, OutputTypeRange.Width));
4312
4313 // Bail out if the subexpr's range is as wide as the cast type.
4314 if (SubRange.Width >= OutputTypeRange.Width)
4315 return OutputTypeRange;
4316
4317 // Otherwise, we take the smaller width, and we're non-negative if
4318 // either the output type or the subexpr is.
4319 return IntRange(SubRange.Width,
4320 SubRange.NonNegative || OutputTypeRange.NonNegative);
4321 }
4322
4323 if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
4324 // If we can fold the condition, just take that operand.
4325 bool CondResult;
4326 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
4327 return GetExprRange(C, CondResult ? CO->getTrueExpr()
4328 : CO->getFalseExpr(),
4329 MaxWidth);
4330
4331 // Otherwise, conservatively merge.
4332 IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
4333 IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
4334 return IntRange::join(L, R);
4335 }
4336
4337 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
4338 switch (BO->getOpcode()) {
4339
4340 // Boolean-valued operations are single-bit and positive.
John McCall2de56d12010-08-25 11:45:40 +00004341 case BO_LAnd:
4342 case BO_LOr:
4343 case BO_LT:
4344 case BO_GT:
4345 case BO_LE:
4346 case BO_GE:
4347 case BO_EQ:
4348 case BO_NE:
John McCallf2370c92010-01-06 05:24:50 +00004349 return IntRange::forBoolType();
4350
John McCall862ff872011-07-13 06:35:24 +00004351 // The type of the assignments is the type of the LHS, so the RHS
4352 // is not necessarily the same type.
John McCall2de56d12010-08-25 11:45:40 +00004353 case BO_MulAssign:
4354 case BO_DivAssign:
4355 case BO_RemAssign:
4356 case BO_AddAssign:
4357 case BO_SubAssign:
John McCall862ff872011-07-13 06:35:24 +00004358 case BO_XorAssign:
4359 case BO_OrAssign:
4360 // TODO: bitfields?
Eli Friedman09bddcf2013-07-08 20:20:06 +00004361 return IntRange::forValueOfType(C, GetExprType(E));
John McCallc0cd21d2010-02-23 19:22:29 +00004362
John McCall862ff872011-07-13 06:35:24 +00004363 // Simple assignments just pass through the RHS, which will have
4364 // been coerced to the LHS type.
4365 case BO_Assign:
4366 // TODO: bitfields?
4367 return GetExprRange(C, BO->getRHS(), MaxWidth);
4368
John McCallf2370c92010-01-06 05:24:50 +00004369 // Operations with opaque sources are black-listed.
John McCall2de56d12010-08-25 11:45:40 +00004370 case BO_PtrMemD:
4371 case BO_PtrMemI:
Eli Friedman09bddcf2013-07-08 20:20:06 +00004372 return IntRange::forValueOfType(C, GetExprType(E));
John McCallf2370c92010-01-06 05:24:50 +00004373
John McCall60fad452010-01-06 22:07:33 +00004374 // Bitwise-and uses the *infinum* of the two source ranges.
John McCall2de56d12010-08-25 11:45:40 +00004375 case BO_And:
4376 case BO_AndAssign:
John McCall60fad452010-01-06 22:07:33 +00004377 return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
4378 GetExprRange(C, BO->getRHS(), MaxWidth));
4379
John McCallf2370c92010-01-06 05:24:50 +00004380 // Left shift gets black-listed based on a judgement call.
John McCall2de56d12010-08-25 11:45:40 +00004381 case BO_Shl:
John McCall3aae6092010-04-07 01:14:35 +00004382 // ...except that we want to treat '1 << (blah)' as logically
4383 // positive. It's an important idiom.
4384 if (IntegerLiteral *I
4385 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
4386 if (I->getValue() == 1) {
Eli Friedman09bddcf2013-07-08 20:20:06 +00004387 IntRange R = IntRange::forValueOfType(C, GetExprType(E));
John McCall3aae6092010-04-07 01:14:35 +00004388 return IntRange(R.Width, /*NonNegative*/ true);
4389 }
4390 }
4391 // fallthrough
4392
John McCall2de56d12010-08-25 11:45:40 +00004393 case BO_ShlAssign:
Eli Friedman09bddcf2013-07-08 20:20:06 +00004394 return IntRange::forValueOfType(C, GetExprType(E));
John McCallf2370c92010-01-06 05:24:50 +00004395
John McCall60fad452010-01-06 22:07:33 +00004396 // Right shift by a constant can narrow its left argument.
John McCall2de56d12010-08-25 11:45:40 +00004397 case BO_Shr:
4398 case BO_ShrAssign: {
John McCall60fad452010-01-06 22:07:33 +00004399 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
4400
4401 // If the shift amount is a positive constant, drop the width by
4402 // that much.
4403 llvm::APSInt shift;
4404 if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
4405 shift.isNonNegative()) {
4406 unsigned zext = shift.getZExtValue();
4407 if (zext >= L.Width)
4408 L.Width = (L.NonNegative ? 0 : 1);
4409 else
4410 L.Width -= zext;
4411 }
4412
4413 return L;
4414 }
4415
4416 // Comma acts as its right operand.
John McCall2de56d12010-08-25 11:45:40 +00004417 case BO_Comma:
John McCallf2370c92010-01-06 05:24:50 +00004418 return GetExprRange(C, BO->getRHS(), MaxWidth);
4419
John McCall60fad452010-01-06 22:07:33 +00004420 // Black-list pointer subtractions.
John McCall2de56d12010-08-25 11:45:40 +00004421 case BO_Sub:
John McCallf2370c92010-01-06 05:24:50 +00004422 if (BO->getLHS()->getType()->isPointerType())
Eli Friedman09bddcf2013-07-08 20:20:06 +00004423 return IntRange::forValueOfType(C, GetExprType(E));
John McCall00fe7612011-07-14 22:39:48 +00004424 break;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00004425
John McCall00fe7612011-07-14 22:39:48 +00004426 // The width of a division result is mostly determined by the size
4427 // of the LHS.
4428 case BO_Div: {
4429 // Don't 'pre-truncate' the operands.
Eli Friedman09bddcf2013-07-08 20:20:06 +00004430 unsigned opWidth = C.getIntWidth(GetExprType(E));
John McCall00fe7612011-07-14 22:39:48 +00004431 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
4432
4433 // If the divisor is constant, use that.
4434 llvm::APSInt divisor;
4435 if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
4436 unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
4437 if (log2 >= L.Width)
4438 L.Width = (L.NonNegative ? 0 : 1);
4439 else
4440 L.Width = std::min(L.Width - log2, MaxWidth);
4441 return L;
4442 }
4443
4444 // Otherwise, just use the LHS's width.
4445 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
4446 return IntRange(L.Width, L.NonNegative && R.NonNegative);
4447 }
4448
4449 // The result of a remainder can't be larger than the result of
4450 // either side.
4451 case BO_Rem: {
4452 // Don't 'pre-truncate' the operands.
Eli Friedman09bddcf2013-07-08 20:20:06 +00004453 unsigned opWidth = C.getIntWidth(GetExprType(E));
John McCall00fe7612011-07-14 22:39:48 +00004454 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
4455 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
4456
4457 IntRange meet = IntRange::meet(L, R);
4458 meet.Width = std::min(meet.Width, MaxWidth);
4459 return meet;
4460 }
4461
4462 // The default behavior is okay for these.
4463 case BO_Mul:
4464 case BO_Add:
4465 case BO_Xor:
4466 case BO_Or:
John McCallf2370c92010-01-06 05:24:50 +00004467 break;
4468 }
4469
John McCall00fe7612011-07-14 22:39:48 +00004470 // The default case is to treat the operation as if it were closed
4471 // on the narrowest type that encompasses both operands.
John McCallf2370c92010-01-06 05:24:50 +00004472 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
4473 IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
4474 return IntRange::join(L, R);
4475 }
4476
4477 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
4478 switch (UO->getOpcode()) {
4479 // Boolean-valued operations are white-listed.
John McCall2de56d12010-08-25 11:45:40 +00004480 case UO_LNot:
John McCallf2370c92010-01-06 05:24:50 +00004481 return IntRange::forBoolType();
4482
4483 // Operations with opaque sources are black-listed.
John McCall2de56d12010-08-25 11:45:40 +00004484 case UO_Deref:
4485 case UO_AddrOf: // should be impossible
Eli Friedman09bddcf2013-07-08 20:20:06 +00004486 return IntRange::forValueOfType(C, GetExprType(E));
John McCallf2370c92010-01-06 05:24:50 +00004487
4488 default:
4489 return GetExprRange(C, UO->getSubExpr(), MaxWidth);
4490 }
4491 }
4492
John McCall993f43f2013-05-06 21:39:12 +00004493 if (FieldDecl *BitField = E->getSourceBitField())
Richard Smitha6b8b2c2011-10-10 18:28:20 +00004494 return IntRange(BitField->getBitWidthValue(C),
Douglas Gregor5e9ebb32011-05-21 16:28:01 +00004495 BitField->getType()->isUnsignedIntegerOrEnumerationType());
John McCallf2370c92010-01-06 05:24:50 +00004496
Eli Friedman09bddcf2013-07-08 20:20:06 +00004497 return IntRange::forValueOfType(C, GetExprType(E));
John McCallf2370c92010-01-06 05:24:50 +00004498}
John McCall51313c32010-01-04 23:31:57 +00004499
Ted Kremenek0692a192012-01-31 05:37:37 +00004500static IntRange GetExprRange(ASTContext &C, Expr *E) {
Eli Friedman09bddcf2013-07-08 20:20:06 +00004501 return GetExprRange(C, E, C.getIntWidth(GetExprType(E)));
John McCall323ed742010-05-06 08:58:33 +00004502}
4503
John McCall51313c32010-01-04 23:31:57 +00004504/// Checks whether the given value, which currently has the given
4505/// source semantics, has the same value when coerced through the
4506/// target semantics.
Ted Kremenek0692a192012-01-31 05:37:37 +00004507static bool IsSameFloatAfterCast(const llvm::APFloat &value,
4508 const llvm::fltSemantics &Src,
4509 const llvm::fltSemantics &Tgt) {
John McCall51313c32010-01-04 23:31:57 +00004510 llvm::APFloat truncated = value;
4511
4512 bool ignored;
4513 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
4514 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
4515
4516 return truncated.bitwiseIsEqual(value);
4517}
4518
4519/// Checks whether the given value, which currently has the given
4520/// source semantics, has the same value when coerced through the
4521/// target semantics.
4522///
4523/// The value might be a vector of floats (or a complex number).
Ted Kremenek0692a192012-01-31 05:37:37 +00004524static bool IsSameFloatAfterCast(const APValue &value,
4525 const llvm::fltSemantics &Src,
4526 const llvm::fltSemantics &Tgt) {
John McCall51313c32010-01-04 23:31:57 +00004527 if (value.isFloat())
4528 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
4529
4530 if (value.isVector()) {
4531 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
4532 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
4533 return false;
4534 return true;
4535 }
4536
4537 assert(value.isComplexFloat());
4538 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
4539 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
4540}
4541
Ted Kremenek0692a192012-01-31 05:37:37 +00004542static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
John McCall323ed742010-05-06 08:58:33 +00004543
Ted Kremeneke3b159c2010-09-23 21:43:44 +00004544static bool IsZero(Sema &S, Expr *E) {
4545 // Suppress cases where we are comparing against an enum constant.
4546 if (const DeclRefExpr *DR =
4547 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
4548 if (isa<EnumConstantDecl>(DR->getDecl()))
4549 return false;
4550
4551 // Suppress cases where the '0' value is expanded from a macro.
4552 if (E->getLocStart().isMacroID())
4553 return false;
4554
John McCall323ed742010-05-06 08:58:33 +00004555 llvm::APSInt Value;
4556 return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
4557}
4558
John McCall372e1032010-10-06 00:25:24 +00004559static bool HasEnumType(Expr *E) {
4560 // Strip off implicit integral promotions.
4561 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Argyrios Kyrtzidis63b57ae2010-10-07 21:52:18 +00004562 if (ICE->getCastKind() != CK_IntegralCast &&
4563 ICE->getCastKind() != CK_NoOp)
John McCall372e1032010-10-06 00:25:24 +00004564 break;
Argyrios Kyrtzidis63b57ae2010-10-07 21:52:18 +00004565 E = ICE->getSubExpr();
John McCall372e1032010-10-06 00:25:24 +00004566 }
4567
4568 return E->getType()->isEnumeralType();
4569}
4570
Ted Kremenek0692a192012-01-31 05:37:37 +00004571static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00004572 BinaryOperatorKind op = E->getOpcode();
Douglas Gregor14af91a2010-12-21 07:22:56 +00004573 if (E->isValueDependent())
4574 return;
4575
John McCall2de56d12010-08-25 11:45:40 +00004576 if (op == BO_LT && IsZero(S, E->getRHS())) {
John McCall323ed742010-05-06 08:58:33 +00004577 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00004578 << "< 0" << "false" << HasEnumType(E->getLHS())
John McCall323ed742010-05-06 08:58:33 +00004579 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00004580 } else if (op == BO_GE && IsZero(S, E->getRHS())) {
John McCall323ed742010-05-06 08:58:33 +00004581 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00004582 << ">= 0" << "true" << HasEnumType(E->getLHS())
John McCall323ed742010-05-06 08:58:33 +00004583 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00004584 } else if (op == BO_GT && IsZero(S, E->getLHS())) {
John McCall323ed742010-05-06 08:58:33 +00004585 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00004586 << "0 >" << "false" << HasEnumType(E->getRHS())
John McCall323ed742010-05-06 08:58:33 +00004587 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00004588 } else if (op == BO_LE && IsZero(S, E->getLHS())) {
John McCall323ed742010-05-06 08:58:33 +00004589 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00004590 << "0 <=" << "true" << HasEnumType(E->getRHS())
John McCall323ed742010-05-06 08:58:33 +00004591 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
4592 }
4593}
4594
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004595static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004596 Expr *Constant, Expr *Other,
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004597 llvm::APSInt Value,
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004598 bool RhsConstant) {
Richard Trieu526e6272012-11-14 22:50:24 +00004599 // 0 values are handled later by CheckTrivialUnsignedComparison().
4600 if (Value == 0)
4601 return;
4602
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004603 BinaryOperatorKind op = E->getOpcode();
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004604 QualType OtherT = Other->getType();
4605 QualType ConstantT = Constant->getType();
Richard Trieu526e6272012-11-14 22:50:24 +00004606 QualType CommonT = E->getLHS()->getType();
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004607 if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT))
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004608 return;
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004609 assert((OtherT->isIntegerType() && ConstantT->isIntegerType())
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004610 && "comparison with non-integer type");
Richard Trieu526e6272012-11-14 22:50:24 +00004611
4612 bool ConstantSigned = ConstantT->isSignedIntegerType();
Richard Trieu526e6272012-11-14 22:50:24 +00004613 bool CommonSigned = CommonT->isSignedIntegerType();
4614
4615 bool EqualityOnly = false;
4616
4617 // TODO: Investigate using GetExprRange() to get tighter bounds on
4618 // on the bit ranges.
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004619 IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT);
Richard Trieu526e6272012-11-14 22:50:24 +00004620 unsigned OtherWidth = OtherRange.Width;
4621
4622 if (CommonSigned) {
4623 // The common type is signed, therefore no signed to unsigned conversion.
Eli Friedmand87de7b2012-11-30 23:09:29 +00004624 if (!OtherRange.NonNegative) {
Richard Trieu526e6272012-11-14 22:50:24 +00004625 // Check that the constant is representable in type OtherT.
4626 if (ConstantSigned) {
4627 if (OtherWidth >= Value.getMinSignedBits())
4628 return;
4629 } else { // !ConstantSigned
4630 if (OtherWidth >= Value.getActiveBits() + 1)
4631 return;
4632 }
4633 } else { // !OtherSigned
4634 // Check that the constant is representable in type OtherT.
4635 // Negative values are out of range.
4636 if (ConstantSigned) {
4637 if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits())
4638 return;
4639 } else { // !ConstantSigned
4640 if (OtherWidth >= Value.getActiveBits())
4641 return;
4642 }
4643 }
4644 } else { // !CommonSigned
Eli Friedmand87de7b2012-11-30 23:09:29 +00004645 if (OtherRange.NonNegative) {
Richard Trieu526e6272012-11-14 22:50:24 +00004646 if (OtherWidth >= Value.getActiveBits())
4647 return;
Eli Friedmand87de7b2012-11-30 23:09:29 +00004648 } else if (!OtherRange.NonNegative && !ConstantSigned) {
Richard Trieu526e6272012-11-14 22:50:24 +00004649 // Check to see if the constant is representable in OtherT.
4650 if (OtherWidth > Value.getActiveBits())
4651 return;
4652 // Check to see if the constant is equivalent to a negative value
4653 // cast to CommonT.
4654 if (S.Context.getIntWidth(ConstantT) == S.Context.getIntWidth(CommonT) &&
Richard Trieu5d1cf4f2012-11-15 03:43:50 +00004655 Value.isNegative() && Value.getMinSignedBits() <= OtherWidth)
Richard Trieu526e6272012-11-14 22:50:24 +00004656 return;
4657 // The constant value rests between values that OtherT can represent after
4658 // conversion. Relational comparison still works, but equality
4659 // comparisons will be tautological.
4660 EqualityOnly = true;
4661 } else { // OtherSigned && ConstantSigned
4662 assert(0 && "Two signed types converted to unsigned types.");
4663 }
4664 }
4665
4666 bool PositiveConstant = !ConstantSigned || Value.isNonNegative();
4667
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004668 bool IsTrue = true;
Richard Trieu526e6272012-11-14 22:50:24 +00004669 if (op == BO_EQ || op == BO_NE) {
4670 IsTrue = op == BO_NE;
4671 } else if (EqualityOnly) {
4672 return;
4673 } else if (RhsConstant) {
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004674 if (op == BO_GT || op == BO_GE)
Richard Trieu526e6272012-11-14 22:50:24 +00004675 IsTrue = !PositiveConstant;
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004676 else // op == BO_LT || op == BO_LE
Richard Trieu526e6272012-11-14 22:50:24 +00004677 IsTrue = PositiveConstant;
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004678 } else {
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004679 if (op == BO_LT || op == BO_LE)
Richard Trieu526e6272012-11-14 22:50:24 +00004680 IsTrue = !PositiveConstant;
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004681 else // op == BO_GT || op == BO_GE
Richard Trieu526e6272012-11-14 22:50:24 +00004682 IsTrue = PositiveConstant;
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004683 }
Ted Kremenek7adf3a92013-03-15 21:50:10 +00004684
4685 // If this is a comparison to an enum constant, include that
4686 // constant in the diagnostic.
4687 const EnumConstantDecl *ED = 0;
4688 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
4689 ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
4690
4691 SmallString<64> PrettySourceValue;
4692 llvm::raw_svector_ostream OS(PrettySourceValue);
4693 if (ED)
Ted Kremenek9de50942013-03-15 22:02:46 +00004694 OS << '\'' << *ED << "' (" << Value << ")";
Ted Kremenek7adf3a92013-03-15 21:50:10 +00004695 else
4696 OS << Value;
4697
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004698 S.Diag(E->getOperatorLoc(), diag::warn_out_of_range_compare)
Ted Kremenek7adf3a92013-03-15 21:50:10 +00004699 << OS.str() << OtherT << IsTrue
Richard Trieu526e6272012-11-14 22:50:24 +00004700 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004701}
4702
John McCall323ed742010-05-06 08:58:33 +00004703/// Analyze the operands of the given comparison. Implements the
4704/// fallback case from AnalyzeComparison.
Ted Kremenek0692a192012-01-31 05:37:37 +00004705static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
John McCallb4eb64d2010-10-08 02:01:28 +00004706 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
4707 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
John McCall323ed742010-05-06 08:58:33 +00004708}
John McCall51313c32010-01-04 23:31:57 +00004709
John McCallba26e582010-01-04 23:21:16 +00004710/// \brief Implements -Wsign-compare.
4711///
Richard Trieudd225092011-09-15 21:56:47 +00004712/// \param E the binary operator to check for warnings
Ted Kremenek0692a192012-01-31 05:37:37 +00004713static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
John McCall323ed742010-05-06 08:58:33 +00004714 // The type the comparison is being performed in.
4715 QualType T = E->getLHS()->getType();
4716 assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())
4717 && "comparison with mismatched types");
Fariborz Jahanianab4702f2012-09-18 17:46:26 +00004718 if (E->isValueDependent())
4719 return AnalyzeImpConvsInComparison(S, E);
John McCallba26e582010-01-04 23:21:16 +00004720
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004721 Expr *LHS = E->getLHS()->IgnoreParenImpCasts();
4722 Expr *RHS = E->getRHS()->IgnoreParenImpCasts();
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004723
4724 bool IsComparisonConstant = false;
4725
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004726 // Check whether an integer constant comparison results in a value
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004727 // of 'true' or 'false'.
4728 if (T->isIntegralType(S.Context)) {
4729 llvm::APSInt RHSValue;
4730 bool IsRHSIntegralLiteral =
4731 RHS->isIntegerConstantExpr(RHSValue, S.Context);
4732 llvm::APSInt LHSValue;
4733 bool IsLHSIntegralLiteral =
4734 LHS->isIntegerConstantExpr(LHSValue, S.Context);
4735 if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral)
4736 DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true);
4737 else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral)
4738 DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false);
4739 else
4740 IsComparisonConstant =
4741 (IsRHSIntegralLiteral && IsLHSIntegralLiteral);
Fariborz Jahaniana193f202012-09-20 19:36:41 +00004742 } else if (!T->hasUnsignedIntegerRepresentation())
4743 IsComparisonConstant = E->isIntegerConstantExpr(S.Context);
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004744
John McCall323ed742010-05-06 08:58:33 +00004745 // We don't do anything special if this isn't an unsigned integral
4746 // comparison: we're only interested in integral comparisons, and
4747 // signed comparisons only happen in cases we don't care to warn about.
Douglas Gregor3e026e32011-02-19 22:34:59 +00004748 //
4749 // We also don't care about value-dependent expressions or expressions
4750 // whose result is a constant.
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004751 if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant)
John McCall323ed742010-05-06 08:58:33 +00004752 return AnalyzeImpConvsInComparison(S, E);
Fariborz Jahanian15a93562012-09-18 17:37:21 +00004753
John McCall323ed742010-05-06 08:58:33 +00004754 // Check to see if one of the (unmodified) operands is of different
4755 // signedness.
4756 Expr *signedOperand, *unsignedOperand;
Richard Trieudd225092011-09-15 21:56:47 +00004757 if (LHS->getType()->hasSignedIntegerRepresentation()) {
4758 assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
John McCall323ed742010-05-06 08:58:33 +00004759 "unsigned comparison between two signed integer expressions?");
Richard Trieudd225092011-09-15 21:56:47 +00004760 signedOperand = LHS;
4761 unsignedOperand = RHS;
4762 } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
4763 signedOperand = RHS;
4764 unsignedOperand = LHS;
John McCallba26e582010-01-04 23:21:16 +00004765 } else {
John McCall323ed742010-05-06 08:58:33 +00004766 CheckTrivialUnsignedComparison(S, E);
4767 return AnalyzeImpConvsInComparison(S, E);
John McCallba26e582010-01-04 23:21:16 +00004768 }
4769
John McCall323ed742010-05-06 08:58:33 +00004770 // Otherwise, calculate the effective range of the signed operand.
4771 IntRange signedRange = GetExprRange(S.Context, signedOperand);
John McCallf2370c92010-01-06 05:24:50 +00004772
John McCall323ed742010-05-06 08:58:33 +00004773 // Go ahead and analyze implicit conversions in the operands. Note
4774 // that we skip the implicit conversions on both sides.
Richard Trieudd225092011-09-15 21:56:47 +00004775 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
4776 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
John McCallba26e582010-01-04 23:21:16 +00004777
John McCall323ed742010-05-06 08:58:33 +00004778 // If the signed range is non-negative, -Wsign-compare won't fire,
4779 // but we should still check for comparisons which are always true
4780 // or false.
4781 if (signedRange.NonNegative)
4782 return CheckTrivialUnsignedComparison(S, E);
John McCallba26e582010-01-04 23:21:16 +00004783
4784 // For (in)equality comparisons, if the unsigned operand is a
4785 // constant which cannot collide with a overflowed signed operand,
4786 // then reinterpreting the signed operand as unsigned will not
4787 // change the result of the comparison.
John McCall323ed742010-05-06 08:58:33 +00004788 if (E->isEqualityOp()) {
4789 unsigned comparisonWidth = S.Context.getIntWidth(T);
4790 IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
John McCallba26e582010-01-04 23:21:16 +00004791
John McCall323ed742010-05-06 08:58:33 +00004792 // We should never be unable to prove that the unsigned operand is
4793 // non-negative.
4794 assert(unsignedRange.NonNegative && "unsigned range includes negative?");
4795
4796 if (unsignedRange.Width < comparisonWidth)
4797 return;
4798 }
4799
Douglas Gregor6d3b93d2012-05-01 01:53:49 +00004800 S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
4801 S.PDiag(diag::warn_mixed_sign_comparison)
4802 << LHS->getType() << RHS->getType()
4803 << LHS->getSourceRange() << RHS->getSourceRange());
John McCallba26e582010-01-04 23:21:16 +00004804}
4805
John McCall15d7d122010-11-11 03:21:53 +00004806/// Analyzes an attempt to assign the given value to a bitfield.
4807///
4808/// Returns true if there was something fishy about the attempt.
Ted Kremenek0692a192012-01-31 05:37:37 +00004809static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
4810 SourceLocation InitLoc) {
John McCall15d7d122010-11-11 03:21:53 +00004811 assert(Bitfield->isBitField());
4812 if (Bitfield->isInvalidDecl())
4813 return false;
4814
John McCall91b60142010-11-11 05:33:51 +00004815 // White-list bool bitfields.
4816 if (Bitfield->getType()->isBooleanType())
4817 return false;
4818
Douglas Gregor46ff3032011-02-04 13:09:01 +00004819 // Ignore value- or type-dependent expressions.
4820 if (Bitfield->getBitWidth()->isValueDependent() ||
4821 Bitfield->getBitWidth()->isTypeDependent() ||
4822 Init->isValueDependent() ||
4823 Init->isTypeDependent())
4824 return false;
4825
John McCall15d7d122010-11-11 03:21:53 +00004826 Expr *OriginalInit = Init->IgnoreParenImpCasts();
4827
Richard Smith80d4b552011-12-28 19:48:30 +00004828 llvm::APSInt Value;
4829 if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects))
John McCall15d7d122010-11-11 03:21:53 +00004830 return false;
4831
John McCall15d7d122010-11-11 03:21:53 +00004832 unsigned OriginalWidth = Value.getBitWidth();
Richard Smitha6b8b2c2011-10-10 18:28:20 +00004833 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
John McCall15d7d122010-11-11 03:21:53 +00004834
4835 if (OriginalWidth <= FieldWidth)
4836 return false;
4837
Eli Friedman3a643af2012-01-26 23:11:39 +00004838 // Compute the value which the bitfield will contain.
Jay Foad9f71a8f2010-12-07 08:25:34 +00004839 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
Eli Friedman3a643af2012-01-26 23:11:39 +00004840 TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType());
John McCall15d7d122010-11-11 03:21:53 +00004841
Eli Friedman3a643af2012-01-26 23:11:39 +00004842 // Check whether the stored value is equal to the original value.
4843 TruncatedValue = TruncatedValue.extend(OriginalWidth);
Richard Trieue1ecdc12012-07-23 20:21:35 +00004844 if (llvm::APSInt::isSameValue(Value, TruncatedValue))
John McCall15d7d122010-11-11 03:21:53 +00004845 return false;
4846
Eli Friedman3a643af2012-01-26 23:11:39 +00004847 // Special-case bitfields of width 1: booleans are naturally 0/1, and
Eli Friedman34ff0622012-02-02 00:40:20 +00004848 // therefore don't strictly fit into a signed bitfield of width 1.
4849 if (FieldWidth == 1 && Value == 1)
Eli Friedman3a643af2012-01-26 23:11:39 +00004850 return false;
4851
John McCall15d7d122010-11-11 03:21:53 +00004852 std::string PrettyValue = Value.toString(10);
4853 std::string PrettyTrunc = TruncatedValue.toString(10);
4854
4855 S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
4856 << PrettyValue << PrettyTrunc << OriginalInit->getType()
4857 << Init->getSourceRange();
4858
4859 return true;
4860}
4861
John McCallbeb22aa2010-11-09 23:24:47 +00004862/// Analyze the given simple or compound assignment for warning-worthy
4863/// operations.
Ted Kremenek0692a192012-01-31 05:37:37 +00004864static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
John McCallbeb22aa2010-11-09 23:24:47 +00004865 // Just recurse on the LHS.
4866 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
4867
4868 // We want to recurse on the RHS as normal unless we're assigning to
4869 // a bitfield.
John McCall993f43f2013-05-06 21:39:12 +00004870 if (FieldDecl *Bitfield = E->getLHS()->getSourceBitField()) {
Timur Iskhodzhanovdff2be82013-03-29 00:22:03 +00004871 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
John McCall15d7d122010-11-11 03:21:53 +00004872 E->getOperatorLoc())) {
4873 // Recurse, ignoring any implicit conversions on the RHS.
4874 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
4875 E->getOperatorLoc());
John McCallbeb22aa2010-11-09 23:24:47 +00004876 }
4877 }
4878
4879 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
4880}
4881
John McCall51313c32010-01-04 23:31:57 +00004882/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
Ted Kremenek0692a192012-01-31 05:37:37 +00004883static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
Anna Zaksc36bedc2012-02-01 19:08:57 +00004884 SourceLocation CContext, unsigned diag,
4885 bool pruneControlFlow = false) {
4886 if (pruneControlFlow) {
4887 S.DiagRuntimeBehavior(E->getExprLoc(), E,
4888 S.PDiag(diag)
4889 << SourceType << T << E->getSourceRange()
4890 << SourceRange(CContext));
4891 return;
4892 }
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00004893 S.Diag(E->getExprLoc(), diag)
4894 << SourceType << T << E->getSourceRange() << SourceRange(CContext);
4895}
4896
Chandler Carruthe1b02e02011-04-05 06:47:57 +00004897/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
Ted Kremenek0692a192012-01-31 05:37:37 +00004898static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
Anna Zaksc36bedc2012-02-01 19:08:57 +00004899 SourceLocation CContext, unsigned diag,
4900 bool pruneControlFlow = false) {
4901 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
Chandler Carruthe1b02e02011-04-05 06:47:57 +00004902}
4903
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004904/// Diagnose an implicit cast from a literal expression. Does not warn when the
4905/// cast wouldn't lose information.
Chandler Carruthf65076e2011-04-10 08:36:24 +00004906void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
4907 SourceLocation CContext) {
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004908 // Try to convert the literal exactly to an integer. If we can, don't warn.
Chandler Carruthf65076e2011-04-10 08:36:24 +00004909 bool isExact = false;
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004910 const llvm::APFloat &Value = FL->getValue();
Jeffrey Yasskin3e1ef782011-07-15 17:03:07 +00004911 llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
4912 T->hasUnsignedIntegerRepresentation());
4913 if (Value.convertToInteger(IntegerValue,
Chandler Carruthf65076e2011-04-10 08:36:24 +00004914 llvm::APFloat::rmTowardZero, &isExact)
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004915 == llvm::APFloat::opOK && isExact)
Chandler Carruthf65076e2011-04-10 08:36:24 +00004916 return;
4917
David Blaikiebe0ee872012-05-15 16:56:36 +00004918 SmallString<16> PrettySourceValue;
4919 Value.toString(PrettySourceValue);
David Blaikiede7e7b82012-05-15 17:18:27 +00004920 SmallString<16> PrettyTargetValue;
David Blaikiebe0ee872012-05-15 16:56:36 +00004921 if (T->isSpecificBuiltinType(BuiltinType::Bool))
4922 PrettyTargetValue = IntegerValue == 0 ? "false" : "true";
4923 else
David Blaikiede7e7b82012-05-15 17:18:27 +00004924 IntegerValue.toString(PrettyTargetValue);
David Blaikiebe0ee872012-05-15 16:56:36 +00004925
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004926 S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer)
David Blaikiebe0ee872012-05-15 16:56:36 +00004927 << FL->getType() << T.getUnqualifiedType() << PrettySourceValue
4928 << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext);
Chandler Carruthf65076e2011-04-10 08:36:24 +00004929}
4930
John McCall091f23f2010-11-09 22:22:12 +00004931std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
4932 if (!Range.Width) return "0";
4933
4934 llvm::APSInt ValueInRange = Value;
4935 ValueInRange.setIsSigned(!Range.NonNegative);
Jay Foad9f71a8f2010-12-07 08:25:34 +00004936 ValueInRange = ValueInRange.trunc(Range.Width);
John McCall091f23f2010-11-09 22:22:12 +00004937 return ValueInRange.toString(10);
4938}
4939
Hans Wennborg88617a22012-08-28 15:44:30 +00004940static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) {
4941 if (!isa<ImplicitCastExpr>(Ex))
4942 return false;
4943
4944 Expr *InnerE = Ex->IgnoreParenImpCasts();
4945 const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr();
4946 const Type *Source =
4947 S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
4948 if (Target->isDependentType())
4949 return false;
4950
4951 const BuiltinType *FloatCandidateBT =
4952 dyn_cast<BuiltinType>(ToBool ? Source : Target);
4953 const Type *BoolCandidateType = ToBool ? Target : Source;
4954
4955 return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) &&
4956 FloatCandidateBT && (FloatCandidateBT->isFloatingPoint()));
4957}
4958
4959void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall,
4960 SourceLocation CC) {
4961 unsigned NumArgs = TheCall->getNumArgs();
4962 for (unsigned i = 0; i < NumArgs; ++i) {
4963 Expr *CurrA = TheCall->getArg(i);
4964 if (!IsImplicitBoolFloatConversion(S, CurrA, true))
4965 continue;
4966
4967 bool IsSwapped = ((i > 0) &&
4968 IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false));
4969 IsSwapped |= ((i < (NumArgs - 1)) &&
4970 IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false));
4971 if (IsSwapped) {
4972 // Warn on this floating-point to bool conversion.
4973 DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(),
4974 CurrA->getType(), CC,
4975 diag::warn_impcast_floating_point_to_bool);
4976 }
4977 }
4978}
4979
John McCall323ed742010-05-06 08:58:33 +00004980void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
Timur Iskhodzhanovdff2be82013-03-29 00:22:03 +00004981 SourceLocation CC, bool *ICContext = 0) {
John McCall323ed742010-05-06 08:58:33 +00004982 if (E->isTypeDependent() || E->isValueDependent()) return;
John McCall51313c32010-01-04 23:31:57 +00004983
John McCall323ed742010-05-06 08:58:33 +00004984 const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
4985 const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
4986 if (Source == Target) return;
4987 if (Target->isDependentType()) return;
John McCall51313c32010-01-04 23:31:57 +00004988
Chandler Carruth108f7562011-07-26 05:40:03 +00004989 // If the conversion context location is invalid don't complain. We also
4990 // don't want to emit a warning if the issue occurs from the expansion of
4991 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
4992 // delay this check as long as possible. Once we detect we are in that
4993 // scenario, we just return.
Ted Kremenekef9ff882011-03-10 20:03:42 +00004994 if (CC.isInvalid())
John McCallb4eb64d2010-10-08 02:01:28 +00004995 return;
4996
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00004997 // Diagnose implicit casts to bool.
4998 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
4999 if (isa<StringLiteral>(E))
5000 // Warn on string literal to bool. Checks for string literals in logical
5001 // expressions, for instances, assert(0 && "error here"), is prevented
5002 // by a check in AnalyzeImplicitConversions().
5003 return DiagnoseImpCast(S, E, T, CC,
5004 diag::warn_impcast_string_literal_to_bool);
Lang Hamese14ca9f2011-12-05 20:49:50 +00005005 if (Source->isFunctionType()) {
5006 // Warn on function to bool. Checks free functions and static member
5007 // functions. Weakly imported functions are excluded from the check,
5008 // since it's common to test their value to check whether the linker
5009 // found a definition for them.
5010 ValueDecl *D = 0;
5011 if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) {
5012 D = R->getDecl();
5013 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
5014 D = M->getMemberDecl();
5015 }
5016
5017 if (D && !D->isWeak()) {
Richard Trieu26b45d82011-12-06 04:48:01 +00005018 if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) {
5019 S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool)
5020 << F << E->getSourceRange() << SourceRange(CC);
David Blaikie2def7732011-12-09 21:42:37 +00005021 S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence)
5022 << FixItHint::CreateInsertion(E->getExprLoc(), "&");
5023 QualType ReturnType;
5024 UnresolvedSet<4> NonTemplateOverloads;
David Blaikiec8fa5252013-06-21 23:54:45 +00005025 S.tryExprAsCall(*E, ReturnType, NonTemplateOverloads);
David Blaikie2def7732011-12-09 21:42:37 +00005026 if (!ReturnType.isNull()
5027 && ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
5028 S.Diag(E->getExprLoc(), diag::note_function_to_bool_call)
5029 << FixItHint::CreateInsertion(
5030 S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()");
Richard Trieu26b45d82011-12-06 04:48:01 +00005031 return;
5032 }
Lang Hamese14ca9f2011-12-05 20:49:50 +00005033 }
5034 }
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00005035 }
John McCall51313c32010-01-04 23:31:57 +00005036
5037 // Strip vector types.
5038 if (isa<VectorType>(Source)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00005039 if (!isa<VectorType>(Target)) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00005040 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00005041 return;
John McCallb4eb64d2010-10-08 02:01:28 +00005042 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
Ted Kremenekef9ff882011-03-10 20:03:42 +00005043 }
Chris Lattnerb792b302011-06-14 04:51:15 +00005044
5045 // If the vector cast is cast between two vectors of the same size, it is
5046 // a bitcast, not a conversion.
5047 if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
5048 return;
John McCall51313c32010-01-04 23:31:57 +00005049
5050 Source = cast<VectorType>(Source)->getElementType().getTypePtr();
5051 Target = cast<VectorType>(Target)->getElementType().getTypePtr();
5052 }
5053
5054 // Strip complex types.
5055 if (isa<ComplexType>(Source)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00005056 if (!isa<ComplexType>(Target)) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00005057 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00005058 return;
5059
John McCallb4eb64d2010-10-08 02:01:28 +00005060 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar);
Ted Kremenekef9ff882011-03-10 20:03:42 +00005061 }
John McCall51313c32010-01-04 23:31:57 +00005062
5063 Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
5064 Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
5065 }
5066
5067 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
5068 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
5069
5070 // If the source is floating point...
5071 if (SourceBT && SourceBT->isFloatingPoint()) {
5072 // ...and the target is floating point...
5073 if (TargetBT && TargetBT->isFloatingPoint()) {
5074 // ...then warn if we're dropping FP rank.
5075
5076 // Builtin FP kinds are ordered by increasing FP rank.
5077 if (SourceBT->getKind() > TargetBT->getKind()) {
5078 // Don't warn about float constants that are precisely
5079 // representable in the target type.
5080 Expr::EvalResult result;
Richard Smith51f47082011-10-29 00:50:52 +00005081 if (E->EvaluateAsRValue(result, S.Context)) {
John McCall51313c32010-01-04 23:31:57 +00005082 // Value might be a float, a float vector, or a float complex.
5083 if (IsSameFloatAfterCast(result.Val,
John McCall323ed742010-05-06 08:58:33 +00005084 S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
5085 S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
John McCall51313c32010-01-04 23:31:57 +00005086 return;
5087 }
5088
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00005089 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00005090 return;
5091
John McCallb4eb64d2010-10-08 02:01:28 +00005092 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
John McCall51313c32010-01-04 23:31:57 +00005093 }
5094 return;
5095 }
5096
Ted Kremenekef9ff882011-03-10 20:03:42 +00005097 // If the target is integral, always warn.
David Blaikiebe0ee872012-05-15 16:56:36 +00005098 if (TargetBT && TargetBT->isInteger()) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00005099 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00005100 return;
5101
Chandler Carrutha5b93322011-02-17 11:05:49 +00005102 Expr *InnerE = E->IgnoreParenImpCasts();
Matt Beaumont-Gay634c8af2011-09-08 22:30:47 +00005103 // We also want to warn on, e.g., "int i = -1.234"
5104 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
5105 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
5106 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
5107
Chandler Carruthf65076e2011-04-10 08:36:24 +00005108 if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) {
5109 DiagnoseFloatingLiteralImpCast(S, FL, T, CC);
Chandler Carrutha5b93322011-02-17 11:05:49 +00005110 } else {
5111 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
5112 }
5113 }
John McCall51313c32010-01-04 23:31:57 +00005114
Hans Wennborg88617a22012-08-28 15:44:30 +00005115 // If the target is bool, warn if expr is a function or method call.
5116 if (Target->isSpecificBuiltinType(BuiltinType::Bool) &&
5117 isa<CallExpr>(E)) {
5118 // Check last argument of function call to see if it is an
5119 // implicit cast from a type matching the type the result
5120 // is being cast to.
5121 CallExpr *CEx = cast<CallExpr>(E);
5122 unsigned NumArgs = CEx->getNumArgs();
5123 if (NumArgs > 0) {
5124 Expr *LastA = CEx->getArg(NumArgs - 1);
5125 Expr *InnerE = LastA->IgnoreParenImpCasts();
5126 const Type *InnerType =
5127 S.Context.getCanonicalType(InnerE->getType()).getTypePtr();
5128 if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) {
5129 // Warn on this floating-point to bool conversion
5130 DiagnoseImpCast(S, E, T, CC,
5131 diag::warn_impcast_floating_point_to_bool);
5132 }
5133 }
5134 }
John McCall51313c32010-01-04 23:31:57 +00005135 return;
5136 }
5137
Richard Trieu1838ca52011-05-29 19:59:02 +00005138 if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)
David Blaikieb26331b2012-06-19 21:19:06 +00005139 == Expr::NPCK_GNUNull) && !Target->isAnyPointerType()
David Blaikiee81b43b2012-11-08 00:41:20 +00005140 && !Target->isBlockPointerType() && !Target->isMemberPointerType()
David Blaikie896c7dd2013-02-16 00:56:22 +00005141 && Target->isScalarType() && !Target->isNullPtrType()) {
David Blaikieb1360492012-03-16 20:30:12 +00005142 SourceLocation Loc = E->getSourceRange().getBegin();
5143 if (Loc.isMacroID())
5144 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
David Blaikie9fb1ac52012-05-15 21:57:38 +00005145 if (!Loc.isMacroID() || CC.isMacroID())
5146 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
5147 << T << clang::SourceRange(CC)
5148 << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T));
Richard Trieu1838ca52011-05-29 19:59:02 +00005149 }
5150
David Blaikieb26331b2012-06-19 21:19:06 +00005151 if (!Source->isIntegerType() || !Target->isIntegerType())
5152 return;
5153
David Blaikiebe0ee872012-05-15 16:56:36 +00005154 // TODO: remove this early return once the false positives for constant->bool
5155 // in templates, macros, etc, are reduced or removed.
5156 if (Target->isSpecificBuiltinType(BuiltinType::Bool))
5157 return;
5158
John McCall323ed742010-05-06 08:58:33 +00005159 IntRange SourceRange = GetExprRange(S.Context, E);
John McCall1844a6e2010-11-10 23:38:19 +00005160 IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
John McCallf2370c92010-01-06 05:24:50 +00005161
Timur Iskhodzhanovdff2be82013-03-29 00:22:03 +00005162 if (SourceRange.Width > TargetRange.Width) {
Sam Panzer25ffbef2013-03-28 19:07:11 +00005163 // If the source is a constant, use a default-on diagnostic.
Timur Iskhodzhanovdff2be82013-03-29 00:22:03 +00005164 // TODO: this should happen for bitfield stores, too.
5165 llvm::APSInt Value(32);
5166 if (E->isIntegerConstantExpr(Value, S.Context)) {
5167 if (S.SourceMgr.isInSystemMacro(CC))
5168 return;
5169
John McCall091f23f2010-11-09 22:22:12 +00005170 std::string PrettySourceValue = Value.toString(10);
5171 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
Timur Iskhodzhanovdff2be82013-03-29 00:22:03 +00005172
Ted Kremenek5e745da2011-10-22 02:37:33 +00005173 S.DiagRuntimeBehavior(E->getExprLoc(), E,
5174 S.PDiag(diag::warn_impcast_integer_precision_constant)
5175 << PrettySourceValue << PrettyTargetValue
5176 << E->getType() << T << E->getSourceRange()
5177 << clang::SourceRange(CC));
John McCall091f23f2010-11-09 22:22:12 +00005178 return;
5179 }
5180
Timur Iskhodzhanovdff2be82013-03-29 00:22:03 +00005181 // People want to build with -Wshorten-64-to-32 and not -Wconversion.
5182 if (S.SourceMgr.isInSystemMacro(CC))
5183 return;
5184
David Blaikie37050842012-04-12 22:40:54 +00005185 if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64)
Anna Zaksc36bedc2012-02-01 19:08:57 +00005186 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32,
5187 /* pruneControlFlow */ true);
John McCallb4eb64d2010-10-08 02:01:28 +00005188 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
John McCall323ed742010-05-06 08:58:33 +00005189 }
5190
5191 if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
5192 (!TargetRange.NonNegative && SourceRange.NonNegative &&
5193 SourceRange.Width == TargetRange.Width)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00005194
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00005195 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00005196 return;
5197
John McCall323ed742010-05-06 08:58:33 +00005198 unsigned DiagID = diag::warn_impcast_integer_sign;
5199
5200 // Traditionally, gcc has warned about this under -Wsign-compare.
5201 // We also want to warn about it in -Wconversion.
5202 // So if -Wconversion is off, use a completely identical diagnostic
5203 // in the sign-compare group.
5204 // The conditional-checking code will
5205 if (ICContext) {
5206 DiagID = diag::warn_impcast_integer_sign_conditional;
5207 *ICContext = true;
5208 }
5209
John McCallb4eb64d2010-10-08 02:01:28 +00005210 return DiagnoseImpCast(S, E, T, CC, DiagID);
John McCall51313c32010-01-04 23:31:57 +00005211 }
5212
Douglas Gregor284cc8d2011-02-22 02:45:07 +00005213 // Diagnose conversions between different enumeration types.
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00005214 // In C, we pretend that the type of an EnumConstantDecl is its enumeration
5215 // type, to give us better diagnostics.
5216 QualType SourceType = E->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00005217 if (!S.getLangOpts().CPlusPlus) {
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00005218 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
5219 if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
5220 EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
5221 SourceType = S.Context.getTypeDeclType(Enum);
5222 Source = S.Context.getCanonicalType(SourceType).getTypePtr();
5223 }
5224 }
5225
Douglas Gregor284cc8d2011-02-22 02:45:07 +00005226 if (const EnumType *SourceEnum = Source->getAs<EnumType>())
5227 if (const EnumType *TargetEnum = Target->getAs<EnumType>())
John McCall83972f12013-03-09 00:54:27 +00005228 if (SourceEnum->getDecl()->hasNameForLinkage() &&
5229 TargetEnum->getDecl()->hasNameForLinkage() &&
Ted Kremenekef9ff882011-03-10 20:03:42 +00005230 SourceEnum != TargetEnum) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00005231 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00005232 return;
5233
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00005234 return DiagnoseImpCast(S, E, SourceType, T, CC,
Douglas Gregor284cc8d2011-02-22 02:45:07 +00005235 diag::warn_impcast_different_enum_types);
Ted Kremenekef9ff882011-03-10 20:03:42 +00005236 }
Douglas Gregor284cc8d2011-02-22 02:45:07 +00005237
John McCall51313c32010-01-04 23:31:57 +00005238 return;
5239}
5240
David Blaikie9fb1ac52012-05-15 21:57:38 +00005241void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
5242 SourceLocation CC, QualType T);
John McCall323ed742010-05-06 08:58:33 +00005243
5244void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
John McCallb4eb64d2010-10-08 02:01:28 +00005245 SourceLocation CC, bool &ICContext) {
John McCall323ed742010-05-06 08:58:33 +00005246 E = E->IgnoreParenImpCasts();
5247
5248 if (isa<ConditionalOperator>(E))
David Blaikie9fb1ac52012-05-15 21:57:38 +00005249 return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T);
John McCall323ed742010-05-06 08:58:33 +00005250
John McCallb4eb64d2010-10-08 02:01:28 +00005251 AnalyzeImplicitConversions(S, E, CC);
John McCall323ed742010-05-06 08:58:33 +00005252 if (E->getType() != T)
John McCallb4eb64d2010-10-08 02:01:28 +00005253 return CheckImplicitConversion(S, E, T, CC, &ICContext);
John McCall323ed742010-05-06 08:58:33 +00005254 return;
5255}
5256
David Blaikie9fb1ac52012-05-15 21:57:38 +00005257void CheckConditionalOperator(Sema &S, ConditionalOperator *E,
5258 SourceLocation CC, QualType T) {
John McCallb4eb64d2010-10-08 02:01:28 +00005259 AnalyzeImplicitConversions(S, E->getCond(), CC);
John McCall323ed742010-05-06 08:58:33 +00005260
5261 bool Suspicious = false;
John McCallb4eb64d2010-10-08 02:01:28 +00005262 CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious);
5263 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
John McCall323ed742010-05-06 08:58:33 +00005264
5265 // If -Wconversion would have warned about either of the candidates
5266 // for a signedness conversion to the context type...
5267 if (!Suspicious) return;
5268
5269 // ...but it's currently ignored...
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00005270 if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional,
5271 CC))
John McCall323ed742010-05-06 08:58:33 +00005272 return;
5273
John McCall323ed742010-05-06 08:58:33 +00005274 // ...then check whether it would have warned about either of the
5275 // candidates for a signedness conversion to the condition type.
Richard Trieu52541612011-07-21 02:46:28 +00005276 if (E->getType() == T) return;
5277
5278 Suspicious = false;
5279 CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
5280 E->getType(), CC, &Suspicious);
5281 if (!Suspicious)
5282 CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
John McCallb4eb64d2010-10-08 02:01:28 +00005283 E->getType(), CC, &Suspicious);
John McCall323ed742010-05-06 08:58:33 +00005284}
5285
5286/// AnalyzeImplicitConversions - Find and report any interesting
5287/// implicit conversions in the given expression. There are a couple
5288/// of competing diagnostics here, -Wconversion and -Wsign-compare.
John McCallb4eb64d2010-10-08 02:01:28 +00005289void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) {
John McCall323ed742010-05-06 08:58:33 +00005290 QualType T = OrigE->getType();
5291 Expr *E = OrigE->IgnoreParenImpCasts();
5292
Douglas Gregorf8b6e152011-10-10 17:38:18 +00005293 if (E->isTypeDependent() || E->isValueDependent())
5294 return;
5295
John McCall323ed742010-05-06 08:58:33 +00005296 // For conditional operators, we analyze the arguments as if they
5297 // were being fed directly into the output.
5298 if (isa<ConditionalOperator>(E)) {
5299 ConditionalOperator *CO = cast<ConditionalOperator>(E);
David Blaikie9fb1ac52012-05-15 21:57:38 +00005300 CheckConditionalOperator(S, CO, CC, T);
John McCall323ed742010-05-06 08:58:33 +00005301 return;
5302 }
5303
Hans Wennborg88617a22012-08-28 15:44:30 +00005304 // Check implicit argument conversions for function calls.
5305 if (CallExpr *Call = dyn_cast<CallExpr>(E))
5306 CheckImplicitArgumentConversions(S, Call, CC);
5307
John McCall323ed742010-05-06 08:58:33 +00005308 // Go ahead and check any implicit conversions we might have skipped.
5309 // The non-canonical typecheck is just an optimization;
5310 // CheckImplicitConversion will filter out dead implicit conversions.
5311 if (E->getType() != T)
John McCallb4eb64d2010-10-08 02:01:28 +00005312 CheckImplicitConversion(S, E, T, CC);
John McCall323ed742010-05-06 08:58:33 +00005313
5314 // Now continue drilling into this expression.
Fariborz Jahanian6f2a9fa2013-05-15 19:03:04 +00005315
5316 if (PseudoObjectExpr * POE = dyn_cast<PseudoObjectExpr>(E)) {
Fariborz Jahaniana1bfe1c2013-05-15 22:25:03 +00005317 if (POE->getResultExpr())
5318 E = POE->getResultExpr();
Fariborz Jahanian6f2a9fa2013-05-15 19:03:04 +00005319 }
5320
Fariborz Jahaniana1bfe1c2013-05-15 22:25:03 +00005321 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
5322 return AnalyzeImplicitConversions(S, OVE->getSourceExpr(), CC);
5323
John McCall323ed742010-05-06 08:58:33 +00005324 // Skip past explicit casts.
5325 if (isa<ExplicitCastExpr>(E)) {
5326 E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
John McCallb4eb64d2010-10-08 02:01:28 +00005327 return AnalyzeImplicitConversions(S, E, CC);
John McCall323ed742010-05-06 08:58:33 +00005328 }
5329
John McCallbeb22aa2010-11-09 23:24:47 +00005330 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
5331 // Do a somewhat different check with comparison operators.
5332 if (BO->isComparisonOp())
5333 return AnalyzeComparison(S, BO);
5334
Timur Iskhodzhanovdff2be82013-03-29 00:22:03 +00005335 // And with simple assignments.
5336 if (BO->getOpcode() == BO_Assign)
John McCallbeb22aa2010-11-09 23:24:47 +00005337 return AnalyzeAssignment(S, BO);
5338 }
John McCall323ed742010-05-06 08:58:33 +00005339
5340 // These break the otherwise-useful invariant below. Fortunately,
5341 // we don't really need to recurse into them, because any internal
5342 // expressions should have been analyzed already when they were
5343 // built into statements.
5344 if (isa<StmtExpr>(E)) return;
5345
5346 // Don't descend into unevaluated contexts.
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005347 if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
John McCall323ed742010-05-06 08:58:33 +00005348
5349 // Now just recurse over the expression's children.
John McCallb4eb64d2010-10-08 02:01:28 +00005350 CC = E->getExprLoc();
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00005351 BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
5352 bool IsLogicalOperator = BO && BO->isLogicalOp();
5353 for (Stmt::child_range I = E->children(); I; ++I) {
Douglas Gregor54042f12012-02-09 10:18:50 +00005354 Expr *ChildExpr = dyn_cast_or_null<Expr>(*I);
Douglas Gregor503384f2012-02-09 00:47:04 +00005355 if (!ChildExpr)
5356 continue;
5357
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00005358 if (IsLogicalOperator &&
5359 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
5360 // Ignore checking string literals that are in logical operators.
5361 continue;
5362 AnalyzeImplicitConversions(S, ChildExpr, CC);
5363 }
John McCall323ed742010-05-06 08:58:33 +00005364}
5365
5366} // end anonymous namespace
5367
5368/// Diagnoses "dangerous" implicit conversions within the given
5369/// expression (which is a full expression). Implements -Wconversion
5370/// and -Wsign-compare.
John McCallb4eb64d2010-10-08 02:01:28 +00005371///
5372/// \param CC the "context" location of the implicit conversion, i.e.
5373/// the most location of the syntactic entity requiring the implicit
5374/// conversion
5375void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
John McCall323ed742010-05-06 08:58:33 +00005376 // Don't diagnose in unevaluated contexts.
David Blaikie71f55f72012-08-06 22:47:24 +00005377 if (isUnevaluatedContext())
John McCall323ed742010-05-06 08:58:33 +00005378 return;
5379
5380 // Don't diagnose for value- or type-dependent expressions.
5381 if (E->isTypeDependent() || E->isValueDependent())
5382 return;
5383
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00005384 // Check for array bounds violations in cases where the check isn't triggered
5385 // elsewhere for other Expr types (like BinaryOperators), e.g. when an
5386 // ArraySubscriptExpr is on the RHS of a variable initialization.
5387 CheckArrayAccess(E);
5388
John McCallb4eb64d2010-10-08 02:01:28 +00005389 // This is not the right CC for (e.g.) a variable initialization.
5390 AnalyzeImplicitConversions(*this, E, CC);
John McCall323ed742010-05-06 08:58:33 +00005391}
5392
Fariborz Jahanianad48a502013-01-24 22:11:45 +00005393/// Diagnose when expression is an integer constant expression and its evaluation
5394/// results in integer overflow
5395void Sema::CheckForIntOverflow (Expr *E) {
Fariborz Jahanian1fd8d462013-03-15 20:47:07 +00005396 if (isa<BinaryOperator>(E->IgnoreParens())) {
Fariborz Jahanianad48a502013-01-24 22:11:45 +00005397 llvm::SmallVector<PartialDiagnosticAt, 4> Diags;
5398 E->EvaluateForOverflow(Context, &Diags);
5399 }
5400}
5401
Richard Smith6c3af3d2013-01-17 01:17:56 +00005402namespace {
5403/// \brief Visitor for expressions which looks for unsequenced operations on the
5404/// same object.
5405class SequenceChecker : public EvaluatedExprVisitor<SequenceChecker> {
Richard Smith0c0b3902013-06-30 10:40:20 +00005406 typedef EvaluatedExprVisitor<SequenceChecker> Base;
5407
Richard Smith6c3af3d2013-01-17 01:17:56 +00005408 /// \brief A tree of sequenced regions within an expression. Two regions are
5409 /// unsequenced if one is an ancestor or a descendent of the other. When we
5410 /// finish processing an expression with sequencing, such as a comma
5411 /// expression, we fold its tree nodes into its parent, since they are
5412 /// unsequenced with respect to nodes we will visit later.
5413 class SequenceTree {
5414 struct Value {
5415 explicit Value(unsigned Parent) : Parent(Parent), Merged(false) {}
5416 unsigned Parent : 31;
5417 bool Merged : 1;
5418 };
5419 llvm::SmallVector<Value, 8> Values;
5420
5421 public:
5422 /// \brief A region within an expression which may be sequenced with respect
5423 /// to some other region.
5424 class Seq {
5425 explicit Seq(unsigned N) : Index(N) {}
5426 unsigned Index;
5427 friend class SequenceTree;
5428 public:
5429 Seq() : Index(0) {}
5430 };
5431
5432 SequenceTree() { Values.push_back(Value(0)); }
5433 Seq root() const { return Seq(0); }
5434
5435 /// \brief Create a new sequence of operations, which is an unsequenced
5436 /// subset of \p Parent. This sequence of operations is sequenced with
5437 /// respect to other children of \p Parent.
5438 Seq allocate(Seq Parent) {
5439 Values.push_back(Value(Parent.Index));
5440 return Seq(Values.size() - 1);
5441 }
5442
5443 /// \brief Merge a sequence of operations into its parent.
5444 void merge(Seq S) {
5445 Values[S.Index].Merged = true;
5446 }
5447
5448 /// \brief Determine whether two operations are unsequenced. This operation
5449 /// is asymmetric: \p Cur should be the more recent sequence, and \p Old
5450 /// should have been merged into its parent as appropriate.
5451 bool isUnsequenced(Seq Cur, Seq Old) {
5452 unsigned C = representative(Cur.Index);
5453 unsigned Target = representative(Old.Index);
5454 while (C >= Target) {
5455 if (C == Target)
5456 return true;
5457 C = Values[C].Parent;
5458 }
5459 return false;
5460 }
5461
5462 private:
5463 /// \brief Pick a representative for a sequence.
5464 unsigned representative(unsigned K) {
5465 if (Values[K].Merged)
5466 // Perform path compression as we go.
5467 return Values[K].Parent = representative(Values[K].Parent);
5468 return K;
5469 }
5470 };
5471
5472 /// An object for which we can track unsequenced uses.
5473 typedef NamedDecl *Object;
5474
5475 /// Different flavors of object usage which we track. We only track the
5476 /// least-sequenced usage of each kind.
5477 enum UsageKind {
5478 /// A read of an object. Multiple unsequenced reads are OK.
5479 UK_Use,
5480 /// A modification of an object which is sequenced before the value
Richard Smith418dd3e2013-06-26 23:16:51 +00005481 /// computation of the expression, such as ++n in C++.
Richard Smith6c3af3d2013-01-17 01:17:56 +00005482 UK_ModAsValue,
5483 /// A modification of an object which is not sequenced before the value
5484 /// computation of the expression, such as n++.
5485 UK_ModAsSideEffect,
5486
5487 UK_Count = UK_ModAsSideEffect + 1
5488 };
5489
5490 struct Usage {
5491 Usage() : Use(0), Seq() {}
5492 Expr *Use;
5493 SequenceTree::Seq Seq;
5494 };
5495
5496 struct UsageInfo {
5497 UsageInfo() : Diagnosed(false) {}
5498 Usage Uses[UK_Count];
5499 /// Have we issued a diagnostic for this variable already?
5500 bool Diagnosed;
5501 };
5502 typedef llvm::SmallDenseMap<Object, UsageInfo, 16> UsageInfoMap;
5503
5504 Sema &SemaRef;
5505 /// Sequenced regions within the expression.
5506 SequenceTree Tree;
5507 /// Declaration modifications and references which we have seen.
5508 UsageInfoMap UsageMap;
5509 /// The region we are currently within.
5510 SequenceTree::Seq Region;
5511 /// Filled in with declarations which were modified as a side-effect
5512 /// (that is, post-increment operations).
5513 llvm::SmallVectorImpl<std::pair<Object, Usage> > *ModAsSideEffect;
Richard Smith1a2dcd52013-01-17 23:18:09 +00005514 /// Expressions to check later. We defer checking these to reduce
5515 /// stack usage.
5516 llvm::SmallVectorImpl<Expr*> &WorkList;
Richard Smith6c3af3d2013-01-17 01:17:56 +00005517
5518 /// RAII object wrapping the visitation of a sequenced subexpression of an
5519 /// expression. At the end of this process, the side-effects of the evaluation
5520 /// become sequenced with respect to the value computation of the result, so
5521 /// we downgrade any UK_ModAsSideEffect within the evaluation to
5522 /// UK_ModAsValue.
5523 struct SequencedSubexpression {
5524 SequencedSubexpression(SequenceChecker &Self)
5525 : Self(Self), OldModAsSideEffect(Self.ModAsSideEffect) {
5526 Self.ModAsSideEffect = &ModAsSideEffect;
5527 }
5528 ~SequencedSubexpression() {
5529 for (unsigned I = 0, E = ModAsSideEffect.size(); I != E; ++I) {
5530 UsageInfo &U = Self.UsageMap[ModAsSideEffect[I].first];
5531 U.Uses[UK_ModAsSideEffect] = ModAsSideEffect[I].second;
5532 Self.addUsage(U, ModAsSideEffect[I].first,
5533 ModAsSideEffect[I].second.Use, UK_ModAsValue);
5534 }
5535 Self.ModAsSideEffect = OldModAsSideEffect;
5536 }
5537
5538 SequenceChecker &Self;
5539 llvm::SmallVector<std::pair<Object, Usage>, 4> ModAsSideEffect;
5540 llvm::SmallVectorImpl<std::pair<Object, Usage> > *OldModAsSideEffect;
5541 };
5542
Richard Smith67470052013-06-20 22:21:56 +00005543 /// RAII object wrapping the visitation of a subexpression which we might
5544 /// choose to evaluate as a constant. If any subexpression is evaluated and
5545 /// found to be non-constant, this allows us to suppress the evaluation of
5546 /// the outer expression.
5547 class EvaluationTracker {
5548 public:
5549 EvaluationTracker(SequenceChecker &Self)
5550 : Self(Self), Prev(Self.EvalTracker), EvalOK(true) {
5551 Self.EvalTracker = this;
5552 }
5553 ~EvaluationTracker() {
5554 Self.EvalTracker = Prev;
5555 if (Prev)
5556 Prev->EvalOK &= EvalOK;
5557 }
5558
5559 bool evaluate(const Expr *E, bool &Result) {
5560 if (!EvalOK || E->isValueDependent())
5561 return false;
5562 EvalOK = E->EvaluateAsBooleanCondition(Result, Self.SemaRef.Context);
5563 return EvalOK;
5564 }
5565
5566 private:
5567 SequenceChecker &Self;
5568 EvaluationTracker *Prev;
5569 bool EvalOK;
5570 } *EvalTracker;
5571
Richard Smith6c3af3d2013-01-17 01:17:56 +00005572 /// \brief Find the object which is produced by the specified expression,
5573 /// if any.
5574 Object getObject(Expr *E, bool Mod) const {
5575 E = E->IgnoreParenCasts();
5576 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
5577 if (Mod && (UO->getOpcode() == UO_PreInc || UO->getOpcode() == UO_PreDec))
5578 return getObject(UO->getSubExpr(), Mod);
5579 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
5580 if (BO->getOpcode() == BO_Comma)
5581 return getObject(BO->getRHS(), Mod);
5582 if (Mod && BO->isAssignmentOp())
5583 return getObject(BO->getLHS(), Mod);
5584 } else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
5585 // FIXME: Check for more interesting cases, like "x.n = ++x.n".
5586 if (isa<CXXThisExpr>(ME->getBase()->IgnoreParenCasts()))
5587 return ME->getMemberDecl();
5588 } else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
5589 // FIXME: If this is a reference, map through to its value.
5590 return DRE->getDecl();
5591 return 0;
5592 }
5593
5594 /// \brief Note that an object was modified or used by an expression.
5595 void addUsage(UsageInfo &UI, Object O, Expr *Ref, UsageKind UK) {
5596 Usage &U = UI.Uses[UK];
5597 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq)) {
5598 if (UK == UK_ModAsSideEffect && ModAsSideEffect)
5599 ModAsSideEffect->push_back(std::make_pair(O, U));
5600 U.Use = Ref;
5601 U.Seq = Region;
5602 }
5603 }
5604 /// \brief Check whether a modification or use conflicts with a prior usage.
5605 void checkUsage(Object O, UsageInfo &UI, Expr *Ref, UsageKind OtherKind,
5606 bool IsModMod) {
5607 if (UI.Diagnosed)
5608 return;
5609
5610 const Usage &U = UI.Uses[OtherKind];
5611 if (!U.Use || !Tree.isUnsequenced(Region, U.Seq))
5612 return;
5613
5614 Expr *Mod = U.Use;
5615 Expr *ModOrUse = Ref;
5616 if (OtherKind == UK_Use)
5617 std::swap(Mod, ModOrUse);
5618
5619 SemaRef.Diag(Mod->getExprLoc(),
5620 IsModMod ? diag::warn_unsequenced_mod_mod
5621 : diag::warn_unsequenced_mod_use)
5622 << O << SourceRange(ModOrUse->getExprLoc());
5623 UI.Diagnosed = true;
5624 }
5625
5626 void notePreUse(Object O, Expr *Use) {
5627 UsageInfo &U = UsageMap[O];
5628 // Uses conflict with other modifications.
5629 checkUsage(O, U, Use, UK_ModAsValue, false);
5630 }
5631 void notePostUse(Object O, Expr *Use) {
5632 UsageInfo &U = UsageMap[O];
5633 checkUsage(O, U, Use, UK_ModAsSideEffect, false);
5634 addUsage(U, O, Use, UK_Use);
5635 }
5636
5637 void notePreMod(Object O, Expr *Mod) {
5638 UsageInfo &U = UsageMap[O];
5639 // Modifications conflict with other modifications and with uses.
5640 checkUsage(O, U, Mod, UK_ModAsValue, true);
5641 checkUsage(O, U, Mod, UK_Use, false);
5642 }
5643 void notePostMod(Object O, Expr *Use, UsageKind UK) {
5644 UsageInfo &U = UsageMap[O];
5645 checkUsage(O, U, Use, UK_ModAsSideEffect, true);
5646 addUsage(U, O, Use, UK);
5647 }
5648
5649public:
Richard Smith1a2dcd52013-01-17 23:18:09 +00005650 SequenceChecker(Sema &S, Expr *E,
5651 llvm::SmallVectorImpl<Expr*> &WorkList)
Richard Smith0c0b3902013-06-30 10:40:20 +00005652 : Base(S.Context), SemaRef(S), Region(Tree.root()),
5653 ModAsSideEffect(0), WorkList(WorkList), EvalTracker(0) {
Richard Smith6c3af3d2013-01-17 01:17:56 +00005654 Visit(E);
5655 }
5656
5657 void VisitStmt(Stmt *S) {
5658 // Skip all statements which aren't expressions for now.
5659 }
5660
5661 void VisitExpr(Expr *E) {
5662 // By default, just recurse to evaluated subexpressions.
Richard Smith0c0b3902013-06-30 10:40:20 +00005663 Base::VisitStmt(E);
Richard Smith6c3af3d2013-01-17 01:17:56 +00005664 }
5665
5666 void VisitCastExpr(CastExpr *E) {
5667 Object O = Object();
5668 if (E->getCastKind() == CK_LValueToRValue)
5669 O = getObject(E->getSubExpr(), false);
5670
5671 if (O)
5672 notePreUse(O, E);
5673 VisitExpr(E);
5674 if (O)
5675 notePostUse(O, E);
5676 }
5677
5678 void VisitBinComma(BinaryOperator *BO) {
5679 // C++11 [expr.comma]p1:
5680 // Every value computation and side effect associated with the left
5681 // expression is sequenced before every value computation and side
5682 // effect associated with the right expression.
5683 SequenceTree::Seq LHS = Tree.allocate(Region);
5684 SequenceTree::Seq RHS = Tree.allocate(Region);
5685 SequenceTree::Seq OldRegion = Region;
5686
5687 {
5688 SequencedSubexpression SeqLHS(*this);
5689 Region = LHS;
5690 Visit(BO->getLHS());
5691 }
5692
5693 Region = RHS;
5694 Visit(BO->getRHS());
5695
5696 Region = OldRegion;
5697
5698 // Forget that LHS and RHS are sequenced. They are both unsequenced
5699 // with respect to other stuff.
5700 Tree.merge(LHS);
5701 Tree.merge(RHS);
5702 }
5703
5704 void VisitBinAssign(BinaryOperator *BO) {
5705 // The modification is sequenced after the value computation of the LHS
5706 // and RHS, so check it before inspecting the operands and update the
5707 // map afterwards.
5708 Object O = getObject(BO->getLHS(), true);
5709 if (!O)
5710 return VisitExpr(BO);
5711
5712 notePreMod(O, BO);
5713
5714 // C++11 [expr.ass]p7:
5715 // E1 op= E2 is equivalent to E1 = E1 op E2, except that E1 is evaluated
5716 // only once.
5717 //
5718 // Therefore, for a compound assignment operator, O is considered used
5719 // everywhere except within the evaluation of E1 itself.
5720 if (isa<CompoundAssignOperator>(BO))
5721 notePreUse(O, BO);
5722
5723 Visit(BO->getLHS());
5724
5725 if (isa<CompoundAssignOperator>(BO))
5726 notePostUse(O, BO);
5727
5728 Visit(BO->getRHS());
5729
Richard Smith418dd3e2013-06-26 23:16:51 +00005730 // C++11 [expr.ass]p1:
5731 // the assignment is sequenced [...] before the value computation of the
5732 // assignment expression.
5733 // C11 6.5.16/3 has no such rule.
5734 notePostMod(O, BO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
5735 : UK_ModAsSideEffect);
Richard Smith6c3af3d2013-01-17 01:17:56 +00005736 }
5737 void VisitCompoundAssignOperator(CompoundAssignOperator *CAO) {
5738 VisitBinAssign(CAO);
5739 }
5740
5741 void VisitUnaryPreInc(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
5742 void VisitUnaryPreDec(UnaryOperator *UO) { VisitUnaryPreIncDec(UO); }
5743 void VisitUnaryPreIncDec(UnaryOperator *UO) {
5744 Object O = getObject(UO->getSubExpr(), true);
5745 if (!O)
5746 return VisitExpr(UO);
5747
5748 notePreMod(O, UO);
5749 Visit(UO->getSubExpr());
Richard Smith418dd3e2013-06-26 23:16:51 +00005750 // C++11 [expr.pre.incr]p1:
5751 // the expression ++x is equivalent to x+=1
5752 notePostMod(O, UO, SemaRef.getLangOpts().CPlusPlus ? UK_ModAsValue
5753 : UK_ModAsSideEffect);
Richard Smith6c3af3d2013-01-17 01:17:56 +00005754 }
5755
5756 void VisitUnaryPostInc(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
5757 void VisitUnaryPostDec(UnaryOperator *UO) { VisitUnaryPostIncDec(UO); }
5758 void VisitUnaryPostIncDec(UnaryOperator *UO) {
5759 Object O = getObject(UO->getSubExpr(), true);
5760 if (!O)
5761 return VisitExpr(UO);
5762
5763 notePreMod(O, UO);
5764 Visit(UO->getSubExpr());
5765 notePostMod(O, UO, UK_ModAsSideEffect);
5766 }
5767
5768 /// Don't visit the RHS of '&&' or '||' if it might not be evaluated.
5769 void VisitBinLOr(BinaryOperator *BO) {
5770 // The side-effects of the LHS of an '&&' are sequenced before the
5771 // value computation of the RHS, and hence before the value computation
5772 // of the '&&' itself, unless the LHS evaluates to zero. We treat them
5773 // as if they were unconditionally sequenced.
Richard Smith67470052013-06-20 22:21:56 +00005774 EvaluationTracker Eval(*this);
Richard Smith6c3af3d2013-01-17 01:17:56 +00005775 {
5776 SequencedSubexpression Sequenced(*this);
5777 Visit(BO->getLHS());
5778 }
5779
5780 bool Result;
Richard Smith67470052013-06-20 22:21:56 +00005781 if (Eval.evaluate(BO->getLHS(), Result)) {
Richard Smith995e4a72013-01-17 22:06:26 +00005782 if (!Result)
5783 Visit(BO->getRHS());
5784 } else {
5785 // Check for unsequenced operations in the RHS, treating it as an
5786 // entirely separate evaluation.
5787 //
5788 // FIXME: If there are operations in the RHS which are unsequenced
5789 // with respect to operations outside the RHS, and those operations
5790 // are unconditionally evaluated, diagnose them.
Richard Smith1a2dcd52013-01-17 23:18:09 +00005791 WorkList.push_back(BO->getRHS());
Richard Smith995e4a72013-01-17 22:06:26 +00005792 }
Richard Smith6c3af3d2013-01-17 01:17:56 +00005793 }
5794 void VisitBinLAnd(BinaryOperator *BO) {
Richard Smith67470052013-06-20 22:21:56 +00005795 EvaluationTracker Eval(*this);
Richard Smith6c3af3d2013-01-17 01:17:56 +00005796 {
5797 SequencedSubexpression Sequenced(*this);
5798 Visit(BO->getLHS());
5799 }
5800
5801 bool Result;
Richard Smith67470052013-06-20 22:21:56 +00005802 if (Eval.evaluate(BO->getLHS(), Result)) {
Richard Smith995e4a72013-01-17 22:06:26 +00005803 if (Result)
5804 Visit(BO->getRHS());
5805 } else {
Richard Smith1a2dcd52013-01-17 23:18:09 +00005806 WorkList.push_back(BO->getRHS());
Richard Smith995e4a72013-01-17 22:06:26 +00005807 }
Richard Smith6c3af3d2013-01-17 01:17:56 +00005808 }
5809
5810 // Only visit the condition, unless we can be sure which subexpression will
5811 // be chosen.
5812 void VisitAbstractConditionalOperator(AbstractConditionalOperator *CO) {
Richard Smith67470052013-06-20 22:21:56 +00005813 EvaluationTracker Eval(*this);
Richard Smith418dd3e2013-06-26 23:16:51 +00005814 {
5815 SequencedSubexpression Sequenced(*this);
5816 Visit(CO->getCond());
5817 }
Richard Smith6c3af3d2013-01-17 01:17:56 +00005818
5819 bool Result;
Richard Smith67470052013-06-20 22:21:56 +00005820 if (Eval.evaluate(CO->getCond(), Result))
Richard Smith6c3af3d2013-01-17 01:17:56 +00005821 Visit(Result ? CO->getTrueExpr() : CO->getFalseExpr());
Richard Smith995e4a72013-01-17 22:06:26 +00005822 else {
Richard Smith1a2dcd52013-01-17 23:18:09 +00005823 WorkList.push_back(CO->getTrueExpr());
5824 WorkList.push_back(CO->getFalseExpr());
Richard Smith995e4a72013-01-17 22:06:26 +00005825 }
Richard Smith6c3af3d2013-01-17 01:17:56 +00005826 }
5827
Richard Smith0c0b3902013-06-30 10:40:20 +00005828 void VisitCallExpr(CallExpr *CE) {
5829 // C++11 [intro.execution]p15:
5830 // When calling a function [...], every value computation and side effect
5831 // associated with any argument expression, or with the postfix expression
5832 // designating the called function, is sequenced before execution of every
5833 // expression or statement in the body of the function [and thus before
5834 // the value computation of its result].
5835 SequencedSubexpression Sequenced(*this);
5836 Base::VisitCallExpr(CE);
5837
5838 // FIXME: CXXNewExpr and CXXDeleteExpr implicitly call functions.
5839 }
5840
Richard Smith6c3af3d2013-01-17 01:17:56 +00005841 void VisitCXXConstructExpr(CXXConstructExpr *CCE) {
Richard Smith0c0b3902013-06-30 10:40:20 +00005842 // This is a call, so all subexpressions are sequenced before the result.
5843 SequencedSubexpression Sequenced(*this);
5844
Richard Smith6c3af3d2013-01-17 01:17:56 +00005845 if (!CCE->isListInitialization())
5846 return VisitExpr(CCE);
5847
5848 // In C++11, list initializations are sequenced.
5849 llvm::SmallVector<SequenceTree::Seq, 32> Elts;
5850 SequenceTree::Seq Parent = Region;
5851 for (CXXConstructExpr::arg_iterator I = CCE->arg_begin(),
5852 E = CCE->arg_end();
5853 I != E; ++I) {
5854 Region = Tree.allocate(Parent);
5855 Elts.push_back(Region);
5856 Visit(*I);
5857 }
5858
5859 // Forget that the initializers are sequenced.
5860 Region = Parent;
5861 for (unsigned I = 0; I < Elts.size(); ++I)
5862 Tree.merge(Elts[I]);
5863 }
5864
5865 void VisitInitListExpr(InitListExpr *ILE) {
5866 if (!SemaRef.getLangOpts().CPlusPlus11)
5867 return VisitExpr(ILE);
5868
5869 // In C++11, list initializations are sequenced.
5870 llvm::SmallVector<SequenceTree::Seq, 32> Elts;
5871 SequenceTree::Seq Parent = Region;
5872 for (unsigned I = 0; I < ILE->getNumInits(); ++I) {
5873 Expr *E = ILE->getInit(I);
5874 if (!E) continue;
5875 Region = Tree.allocate(Parent);
5876 Elts.push_back(Region);
5877 Visit(E);
5878 }
5879
5880 // Forget that the initializers are sequenced.
5881 Region = Parent;
5882 for (unsigned I = 0; I < Elts.size(); ++I)
5883 Tree.merge(Elts[I]);
5884 }
5885};
5886}
5887
5888void Sema::CheckUnsequencedOperations(Expr *E) {
Richard Smith1a2dcd52013-01-17 23:18:09 +00005889 llvm::SmallVector<Expr*, 8> WorkList;
5890 WorkList.push_back(E);
5891 while (!WorkList.empty()) {
5892 Expr *Item = WorkList.back();
5893 WorkList.pop_back();
5894 SequenceChecker(*this, Item, WorkList);
5895 }
Richard Smith6c3af3d2013-01-17 01:17:56 +00005896}
5897
Fariborz Jahanianad48a502013-01-24 22:11:45 +00005898void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc,
5899 bool IsConstexpr) {
Richard Smith6c3af3d2013-01-17 01:17:56 +00005900 CheckImplicitConversions(E, CheckLoc);
5901 CheckUnsequencedOperations(E);
Fariborz Jahanianad48a502013-01-24 22:11:45 +00005902 if (!IsConstexpr && !E->isValueDependent())
5903 CheckForIntOverflow(E);
Richard Smith6c3af3d2013-01-17 01:17:56 +00005904}
5905
John McCall15d7d122010-11-11 03:21:53 +00005906void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
5907 FieldDecl *BitField,
5908 Expr *Init) {
5909 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
5910}
5911
Mike Stumpf8c49212010-01-21 03:59:47 +00005912/// CheckParmsForFunctionDef - Check that the parameters of the given
5913/// function are appropriate for the definition of a function. This
5914/// takes care of any checks that cannot be performed on the
5915/// declaration itself, e.g., that the types of each of the function
5916/// parameters are complete.
Reid Kleckner8c0501c2013-06-24 14:38:26 +00005917bool Sema::CheckParmsForFunctionDef(ParmVarDecl *const *P,
5918 ParmVarDecl *const *PEnd,
Douglas Gregor82aa7132010-11-01 18:37:59 +00005919 bool CheckParameterNames) {
Mike Stumpf8c49212010-01-21 03:59:47 +00005920 bool HasInvalidParm = false;
Douglas Gregor82aa7132010-11-01 18:37:59 +00005921 for (; P != PEnd; ++P) {
5922 ParmVarDecl *Param = *P;
5923
Mike Stumpf8c49212010-01-21 03:59:47 +00005924 // C99 6.7.5.3p4: the parameters in a parameter type list in a
5925 // function declarator that is part of a function definition of
5926 // that function shall not have incomplete type.
5927 //
5928 // This is also C++ [dcl.fct]p6.
5929 if (!Param->isInvalidDecl() &&
5930 RequireCompleteType(Param->getLocation(), Param->getType(),
Douglas Gregord10099e2012-05-04 16:32:21 +00005931 diag::err_typecheck_decl_incomplete_type)) {
Mike Stumpf8c49212010-01-21 03:59:47 +00005932 Param->setInvalidDecl();
5933 HasInvalidParm = true;
5934 }
5935
5936 // C99 6.9.1p5: If the declarator includes a parameter type list, the
5937 // declaration of each parameter shall include an identifier.
Douglas Gregor82aa7132010-11-01 18:37:59 +00005938 if (CheckParameterNames &&
5939 Param->getIdentifier() == 0 &&
Mike Stumpf8c49212010-01-21 03:59:47 +00005940 !Param->isImplicit() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00005941 !getLangOpts().CPlusPlus)
Mike Stumpf8c49212010-01-21 03:59:47 +00005942 Diag(Param->getLocation(), diag::err_parameter_name_omitted);
Sam Weinigd17e3402010-02-01 05:02:49 +00005943
5944 // C99 6.7.5.3p12:
5945 // If the function declarator is not part of a definition of that
5946 // function, parameters may have incomplete type and may use the [*]
5947 // notation in their sequences of declarator specifiers to specify
5948 // variable length array types.
5949 QualType PType = Param->getOriginalType();
Fariborz Jahaniand237d2e2013-04-29 22:01:25 +00005950 while (const ArrayType *AT = Context.getAsArrayType(PType)) {
Sam Weinigd17e3402010-02-01 05:02:49 +00005951 if (AT->getSizeModifier() == ArrayType::Star) {
Stefanus Du Toitfc093362013-03-01 21:41:22 +00005952 // FIXME: This diagnostic should point the '[*]' if source-location
Sam Weinigd17e3402010-02-01 05:02:49 +00005953 // information is added for it.
5954 Diag(Param->getLocation(), diag::err_array_star_in_function_definition);
Fariborz Jahaniand237d2e2013-04-29 22:01:25 +00005955 break;
Sam Weinigd17e3402010-02-01 05:02:49 +00005956 }
Fariborz Jahaniand237d2e2013-04-29 22:01:25 +00005957 PType= AT->getElementType();
Sam Weinigd17e3402010-02-01 05:02:49 +00005958 }
Reid Kleckner9b601952013-06-21 12:45:15 +00005959
5960 // MSVC destroys objects passed by value in the callee. Therefore a
5961 // function definition which takes such a parameter must be able to call the
5962 // object's destructor.
5963 if (getLangOpts().CPlusPlus &&
5964 Context.getTargetInfo().getCXXABI().isArgumentDestroyedByCallee()) {
5965 if (const RecordType *RT = Param->getType()->getAs<RecordType>())
5966 FinalizeVarWithDestructor(Param, RT);
5967 }
Mike Stumpf8c49212010-01-21 03:59:47 +00005968 }
5969
5970 return HasInvalidParm;
5971}
John McCallb7f4ffe2010-08-12 21:44:57 +00005972
5973/// CheckCastAlign - Implements -Wcast-align, which warns when a
5974/// pointer cast increases the alignment requirements.
5975void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
5976 // This is actually a lot of work to potentially be doing on every
5977 // cast; don't do it if we're ignoring -Wcast_align (as is the default).
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00005978 if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align,
5979 TRange.getBegin())
David Blaikied6471f72011-09-25 23:23:43 +00005980 == DiagnosticsEngine::Ignored)
John McCallb7f4ffe2010-08-12 21:44:57 +00005981 return;
5982
5983 // Ignore dependent types.
5984 if (T->isDependentType() || Op->getType()->isDependentType())
5985 return;
5986
5987 // Require that the destination be a pointer type.
5988 const PointerType *DestPtr = T->getAs<PointerType>();
5989 if (!DestPtr) return;
5990
5991 // If the destination has alignment 1, we're done.
5992 QualType DestPointee = DestPtr->getPointeeType();
5993 if (DestPointee->isIncompleteType()) return;
5994 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
5995 if (DestAlign.isOne()) return;
5996
5997 // Require that the source be a pointer type.
5998 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
5999 if (!SrcPtr) return;
6000 QualType SrcPointee = SrcPtr->getPointeeType();
6001
6002 // Whitelist casts from cv void*. We already implicitly
6003 // whitelisted casts to cv void*, since they have alignment 1.
6004 // Also whitelist casts involving incomplete types, which implicitly
6005 // includes 'void'.
6006 if (SrcPointee->isIncompleteType()) return;
6007
6008 CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
6009 if (SrcAlign >= DestAlign) return;
6010
6011 Diag(TRange.getBegin(), diag::warn_cast_align)
6012 << Op->getType() << T
6013 << static_cast<unsigned>(SrcAlign.getQuantity())
6014 << static_cast<unsigned>(DestAlign.getQuantity())
6015 << TRange << Op->getSourceRange();
6016}
6017
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006018static const Type* getElementType(const Expr *BaseExpr) {
6019 const Type* EltType = BaseExpr->getType().getTypePtr();
6020 if (EltType->isAnyPointerType())
6021 return EltType->getPointeeType().getTypePtr();
6022 else if (EltType->isArrayType())
6023 return EltType->getBaseElementTypeUnsafe();
6024 return EltType;
6025}
6026
Chandler Carruthc2684342011-08-05 09:10:50 +00006027/// \brief Check whether this array fits the idiom of a size-one tail padded
6028/// array member of a struct.
6029///
6030/// We avoid emitting out-of-bounds access warnings for such arrays as they are
6031/// commonly used to emulate flexible arrays in C89 code.
6032static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
6033 const NamedDecl *ND) {
6034 if (Size != 1 || !ND) return false;
6035
6036 const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
6037 if (!FD) return false;
6038
6039 // Don't consider sizes resulting from macro expansions or template argument
6040 // substitution to form C89 tail-padded arrays.
Sean Callanand2cf3482012-05-04 18:22:53 +00006041
6042 TypeSourceInfo *TInfo = FD->getTypeSourceInfo();
Ted Kremenek00e1f6f2012-05-09 05:35:08 +00006043 while (TInfo) {
6044 TypeLoc TL = TInfo->getTypeLoc();
6045 // Look through typedefs.
David Blaikie39e6ab42013-02-18 22:06:02 +00006046 if (TypedefTypeLoc TTL = TL.getAs<TypedefTypeLoc>()) {
6047 const TypedefNameDecl *TDL = TTL.getTypedefNameDecl();
Ted Kremenek00e1f6f2012-05-09 05:35:08 +00006048 TInfo = TDL->getTypeSourceInfo();
6049 continue;
6050 }
David Blaikie39e6ab42013-02-18 22:06:02 +00006051 if (ConstantArrayTypeLoc CTL = TL.getAs<ConstantArrayTypeLoc>()) {
6052 const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr());
Chad Rosier5e253012013-02-06 00:58:34 +00006053 if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
6054 return false;
6055 }
Ted Kremenek00e1f6f2012-05-09 05:35:08 +00006056 break;
Sean Callanand2cf3482012-05-04 18:22:53 +00006057 }
Chandler Carruthc2684342011-08-05 09:10:50 +00006058
6059 const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
Matt Beaumont-Gay381711c2011-11-29 22:43:53 +00006060 if (!RD) return false;
6061 if (RD->isUnion()) return false;
6062 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
6063 if (!CRD->isStandardLayout()) return false;
6064 }
Chandler Carruthc2684342011-08-05 09:10:50 +00006065
Benjamin Kramer22d4fed2011-08-06 03:04:42 +00006066 // See if this is the last field decl in the record.
6067 const Decl *D = FD;
6068 while ((D = D->getNextDeclInContext()))
6069 if (isa<FieldDecl>(D))
6070 return false;
6071 return true;
Chandler Carruthc2684342011-08-05 09:10:50 +00006072}
6073
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006074void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00006075 const ArraySubscriptExpr *ASE,
Richard Smith25b009a2011-12-16 19:31:14 +00006076 bool AllowOnePastEnd, bool IndexNegated) {
Eli Friedman92b670e2012-02-27 21:21:40 +00006077 IndexExpr = IndexExpr->IgnoreParenImpCasts();
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00006078 if (IndexExpr->isValueDependent())
6079 return;
6080
Matt Beaumont-Gay8ef8f432011-12-12 22:35:02 +00006081 const Type *EffectiveType = getElementType(BaseExpr);
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006082 BaseExpr = BaseExpr->IgnoreParenCasts();
Chandler Carruth34064582011-02-17 20:55:08 +00006083 const ConstantArrayType *ArrayTy =
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006084 Context.getAsConstantArrayType(BaseExpr->getType());
Chandler Carruth34064582011-02-17 20:55:08 +00006085 if (!ArrayTy)
Ted Kremeneka0125d82011-02-16 01:57:07 +00006086 return;
Chandler Carruth35001ca2011-02-17 21:10:52 +00006087
Chandler Carruth34064582011-02-17 20:55:08 +00006088 llvm::APSInt index;
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00006089 if (!IndexExpr->EvaluateAsInt(index, Context))
Ted Kremeneka0125d82011-02-16 01:57:07 +00006090 return;
Richard Smith25b009a2011-12-16 19:31:14 +00006091 if (IndexNegated)
6092 index = -index;
Ted Kremenek8fd0a5d2011-02-16 04:01:44 +00006093
Chandler Carruthba447122011-08-05 08:07:29 +00006094 const NamedDecl *ND = NULL;
Chandler Carruthba447122011-08-05 08:07:29 +00006095 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
6096 ND = dyn_cast<NamedDecl>(DRE->getDecl());
Chandler Carruthc2684342011-08-05 09:10:50 +00006097 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
Chandler Carruthba447122011-08-05 08:07:29 +00006098 ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
Chandler Carruthba447122011-08-05 08:07:29 +00006099
Ted Kremenek9e060ca2011-02-23 23:06:04 +00006100 if (index.isUnsigned() || !index.isNegative()) {
Ted Kremenek25b3b842011-02-18 02:27:00 +00006101 llvm::APInt size = ArrayTy->getSize();
Chandler Carruth35001ca2011-02-17 21:10:52 +00006102 if (!size.isStrictlyPositive())
6103 return;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006104
6105 const Type* BaseType = getElementType(BaseExpr);
Nico Weberde5998f2011-09-17 22:59:41 +00006106 if (BaseType != EffectiveType) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006107 // Make sure we're comparing apples to apples when comparing index to size
6108 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
6109 uint64_t array_typesize = Context.getTypeSize(BaseType);
Kaelyn Uhraind10f4bc2011-08-10 19:47:25 +00006110 // Handle ptrarith_typesize being zero, such as when casting to void*
Kaelyn Uhrain18f16972011-08-10 18:49:28 +00006111 if (!ptrarith_typesize) ptrarith_typesize = 1;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006112 if (ptrarith_typesize != array_typesize) {
6113 // There's a cast to a different size type involved
6114 uint64_t ratio = array_typesize / ptrarith_typesize;
6115 // TODO: Be smarter about handling cases where array_typesize is not a
6116 // multiple of ptrarith_typesize
6117 if (ptrarith_typesize * ratio == array_typesize)
6118 size *= llvm::APInt(size.getBitWidth(), ratio);
6119 }
6120 }
6121
Chandler Carruth34064582011-02-17 20:55:08 +00006122 if (size.getBitWidth() > index.getBitWidth())
Eli Friedman92b670e2012-02-27 21:21:40 +00006123 index = index.zext(size.getBitWidth());
Ted Kremenek25b3b842011-02-18 02:27:00 +00006124 else if (size.getBitWidth() < index.getBitWidth())
Eli Friedman92b670e2012-02-27 21:21:40 +00006125 size = size.zext(index.getBitWidth());
Ted Kremenek25b3b842011-02-18 02:27:00 +00006126
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006127 // For array subscripting the index must be less than size, but for pointer
6128 // arithmetic also allow the index (offset) to be equal to size since
6129 // computing the next address after the end of the array is legal and
6130 // commonly done e.g. in C++ iterators and range-based for loops.
Eli Friedman92b670e2012-02-27 21:21:40 +00006131 if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
Chandler Carruthba447122011-08-05 08:07:29 +00006132 return;
6133
6134 // Also don't warn for arrays of size 1 which are members of some
6135 // structure. These are often used to approximate flexible arrays in C89
6136 // code.
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006137 if (IsTailPaddedMemberArray(*this, size, ND))
Ted Kremenek8fd0a5d2011-02-16 04:01:44 +00006138 return;
Chandler Carruth34064582011-02-17 20:55:08 +00006139
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00006140 // Suppress the warning if the subscript expression (as identified by the
6141 // ']' location) and the index expression are both from macro expansions
6142 // within a system header.
6143 if (ASE) {
6144 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
6145 ASE->getRBracketLoc());
6146 if (SourceMgr.isInSystemHeader(RBracketLoc)) {
6147 SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
6148 IndexExpr->getLocStart());
6149 if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc))
6150 return;
6151 }
6152 }
6153
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006154 unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00006155 if (ASE)
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006156 DiagID = diag::warn_array_index_exceeds_bounds;
6157
6158 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
6159 PDiag(DiagID) << index.toString(10, true)
6160 << size.toString(10, true)
6161 << (unsigned)size.getLimitedValue(~0U)
6162 << IndexExpr->getSourceRange());
Chandler Carruth34064582011-02-17 20:55:08 +00006163 } else {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006164 unsigned DiagID = diag::warn_array_index_precedes_bounds;
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00006165 if (!ASE) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006166 DiagID = diag::warn_ptr_arith_precedes_bounds;
6167 if (index.isNegative()) index = -index;
6168 }
6169
6170 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
6171 PDiag(DiagID) << index.toString(10, true)
6172 << IndexExpr->getSourceRange());
Ted Kremeneka0125d82011-02-16 01:57:07 +00006173 }
Chandler Carruth35001ca2011-02-17 21:10:52 +00006174
Matt Beaumont-Gaycfbc5b52011-11-29 19:27:11 +00006175 if (!ND) {
6176 // Try harder to find a NamedDecl to point at in the note.
6177 while (const ArraySubscriptExpr *ASE =
6178 dyn_cast<ArraySubscriptExpr>(BaseExpr))
6179 BaseExpr = ASE->getBase()->IgnoreParenCasts();
6180 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
6181 ND = dyn_cast<NamedDecl>(DRE->getDecl());
6182 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
6183 ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
6184 }
6185
Chandler Carruth35001ca2011-02-17 21:10:52 +00006186 if (ND)
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006187 DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
6188 PDiag(diag::note_array_index_out_of_bounds)
6189 << ND->getDeclName());
Ted Kremeneka0125d82011-02-16 01:57:07 +00006190}
6191
Ted Kremenek3aea4da2011-03-01 18:41:00 +00006192void Sema::CheckArrayAccess(const Expr *expr) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006193 int AllowOnePastEnd = 0;
6194 while (expr) {
6195 expr = expr->IgnoreParenImpCasts();
Ted Kremenek3aea4da2011-03-01 18:41:00 +00006196 switch (expr->getStmtClass()) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006197 case Stmt::ArraySubscriptExprClass: {
6198 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00006199 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006200 AllowOnePastEnd > 0);
Ted Kremenek3aea4da2011-03-01 18:41:00 +00006201 return;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00006202 }
6203 case Stmt::UnaryOperatorClass: {
6204 // Only unwrap the * and & unary operators
6205 const UnaryOperator *UO = cast<UnaryOperator>(expr);
6206 expr = UO->getSubExpr();
6207 switch (UO->getOpcode()) {
6208 case UO_AddrOf:
6209 AllowOnePastEnd++;
6210 break;
6211 case UO_Deref:
6212 AllowOnePastEnd--;
6213 break;
6214 default:
6215 return;
6216 }
6217 break;
6218 }
Ted Kremenek3aea4da2011-03-01 18:41:00 +00006219 case Stmt::ConditionalOperatorClass: {
6220 const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
6221 if (const Expr *lhs = cond->getLHS())
6222 CheckArrayAccess(lhs);
6223 if (const Expr *rhs = cond->getRHS())
6224 CheckArrayAccess(rhs);
6225 return;
6226 }
6227 default:
6228 return;
6229 }
Peter Collingbournef111d932011-04-15 00:35:48 +00006230 }
Ted Kremenek3aea4da2011-03-01 18:41:00 +00006231}
John McCallf85e1932011-06-15 23:02:42 +00006232
6233//===--- CHECK: Objective-C retain cycles ----------------------------------//
6234
6235namespace {
6236 struct RetainCycleOwner {
6237 RetainCycleOwner() : Variable(0), Indirect(false) {}
6238 VarDecl *Variable;
6239 SourceRange Range;
6240 SourceLocation Loc;
6241 bool Indirect;
6242
6243 void setLocsFrom(Expr *e) {
6244 Loc = e->getExprLoc();
6245 Range = e->getSourceRange();
6246 }
6247 };
6248}
6249
6250/// Consider whether capturing the given variable can possibly lead to
6251/// a retain cycle.
6252static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00006253 // In ARC, it's captured strongly iff the variable has __strong
John McCallf85e1932011-06-15 23:02:42 +00006254 // lifetime. In MRR, it's captured strongly if the variable is
6255 // __block and has an appropriate type.
6256 if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
6257 return false;
6258
6259 owner.Variable = var;
Jordan Rosee10f4d32012-09-15 02:48:31 +00006260 if (ref)
6261 owner.setLocsFrom(ref);
John McCallf85e1932011-06-15 23:02:42 +00006262 return true;
6263}
6264
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00006265static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
John McCallf85e1932011-06-15 23:02:42 +00006266 while (true) {
6267 e = e->IgnoreParens();
6268 if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
6269 switch (cast->getCastKind()) {
6270 case CK_BitCast:
6271 case CK_LValueBitCast:
6272 case CK_LValueToRValue:
John McCall33e56f32011-09-10 06:18:15 +00006273 case CK_ARCReclaimReturnedObject:
John McCallf85e1932011-06-15 23:02:42 +00006274 e = cast->getSubExpr();
6275 continue;
6276
John McCallf85e1932011-06-15 23:02:42 +00006277 default:
6278 return false;
6279 }
6280 }
6281
6282 if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
6283 ObjCIvarDecl *ivar = ref->getDecl();
6284 if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
6285 return false;
6286
6287 // Try to find a retain cycle in the base.
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00006288 if (!findRetainCycleOwner(S, ref->getBase(), owner))
John McCallf85e1932011-06-15 23:02:42 +00006289 return false;
6290
6291 if (ref->isFreeIvar()) owner.setLocsFrom(ref);
6292 owner.Indirect = true;
6293 return true;
6294 }
6295
6296 if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
6297 VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
6298 if (!var) return false;
6299 return considerVariable(var, ref, owner);
6300 }
6301
John McCallf85e1932011-06-15 23:02:42 +00006302 if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
6303 if (member->isArrow()) return false;
6304
6305 // Don't count this as an indirect ownership.
6306 e = member->getBase();
6307 continue;
6308 }
6309
John McCall4b9c2d22011-11-06 09:01:30 +00006310 if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
6311 // Only pay attention to pseudo-objects on property references.
6312 ObjCPropertyRefExpr *pre
6313 = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm()
6314 ->IgnoreParens());
6315 if (!pre) return false;
6316 if (pre->isImplicitProperty()) return false;
6317 ObjCPropertyDecl *property = pre->getExplicitProperty();
6318 if (!property->isRetaining() &&
6319 !(property->getPropertyIvarDecl() &&
6320 property->getPropertyIvarDecl()->getType()
6321 .getObjCLifetime() == Qualifiers::OCL_Strong))
6322 return false;
6323
6324 owner.Indirect = true;
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00006325 if (pre->isSuperReceiver()) {
6326 owner.Variable = S.getCurMethodDecl()->getSelfDecl();
6327 if (!owner.Variable)
6328 return false;
6329 owner.Loc = pre->getLocation();
6330 owner.Range = pre->getSourceRange();
6331 return true;
6332 }
John McCall4b9c2d22011-11-06 09:01:30 +00006333 e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase())
6334 ->getSourceExpr());
6335 continue;
6336 }
6337
John McCallf85e1932011-06-15 23:02:42 +00006338 // Array ivars?
6339
6340 return false;
6341 }
6342}
6343
6344namespace {
6345 struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
6346 FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
6347 : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
6348 Variable(variable), Capturer(0) {}
6349
6350 VarDecl *Variable;
6351 Expr *Capturer;
6352
6353 void VisitDeclRefExpr(DeclRefExpr *ref) {
6354 if (ref->getDecl() == Variable && !Capturer)
6355 Capturer = ref;
6356 }
6357
John McCallf85e1932011-06-15 23:02:42 +00006358 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
6359 if (Capturer) return;
6360 Visit(ref->getBase());
6361 if (Capturer && ref->isFreeIvar())
6362 Capturer = ref;
6363 }
6364
6365 void VisitBlockExpr(BlockExpr *block) {
6366 // Look inside nested blocks
6367 if (block->getBlockDecl()->capturesVariable(Variable))
6368 Visit(block->getBlockDecl()->getBody());
6369 }
Fariborz Jahanian7e2e4c32012-08-31 20:04:47 +00006370
6371 void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) {
6372 if (Capturer) return;
6373 if (OVE->getSourceExpr())
6374 Visit(OVE->getSourceExpr());
6375 }
John McCallf85e1932011-06-15 23:02:42 +00006376 };
6377}
6378
6379/// Check whether the given argument is a block which captures a
6380/// variable.
6381static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
6382 assert(owner.Variable && owner.Loc.isValid());
6383
6384 e = e->IgnoreParenCasts();
Jordan Rose1fac58a2012-09-17 17:54:30 +00006385
6386 // Look through [^{...} copy] and Block_copy(^{...}).
6387 if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) {
6388 Selector Cmd = ME->getSelector();
6389 if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") {
6390 e = ME->getInstanceReceiver();
6391 if (!e)
6392 return 0;
6393 e = e->IgnoreParenCasts();
6394 }
6395 } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) {
6396 if (CE->getNumArgs() == 1) {
6397 FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl());
Ted Kremenekd13eff62012-10-02 04:36:54 +00006398 if (Fn) {
6399 const IdentifierInfo *FnI = Fn->getIdentifier();
6400 if (FnI && FnI->isStr("_Block_copy")) {
6401 e = CE->getArg(0)->IgnoreParenCasts();
6402 }
6403 }
Jordan Rose1fac58a2012-09-17 17:54:30 +00006404 }
6405 }
6406
John McCallf85e1932011-06-15 23:02:42 +00006407 BlockExpr *block = dyn_cast<BlockExpr>(e);
6408 if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
6409 return 0;
6410
6411 FindCaptureVisitor visitor(S.Context, owner.Variable);
6412 visitor.Visit(block->getBlockDecl()->getBody());
6413 return visitor.Capturer;
6414}
6415
6416static void diagnoseRetainCycle(Sema &S, Expr *capturer,
6417 RetainCycleOwner &owner) {
6418 assert(capturer);
6419 assert(owner.Variable && owner.Loc.isValid());
6420
6421 S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
6422 << owner.Variable << capturer->getSourceRange();
6423 S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
6424 << owner.Indirect << owner.Range;
6425}
6426
6427/// Check for a keyword selector that starts with the word 'add' or
6428/// 'set'.
6429static bool isSetterLikeSelector(Selector sel) {
6430 if (sel.isUnarySelector()) return false;
6431
Chris Lattner5f9e2722011-07-23 10:55:15 +00006432 StringRef str = sel.getNameForSlot(0);
John McCallf85e1932011-06-15 23:02:42 +00006433 while (!str.empty() && str.front() == '_') str = str.substr(1);
Ted Kremenek968a0ee2011-12-01 00:59:21 +00006434 if (str.startswith("set"))
John McCallf85e1932011-06-15 23:02:42 +00006435 str = str.substr(3);
Ted Kremenek968a0ee2011-12-01 00:59:21 +00006436 else if (str.startswith("add")) {
6437 // Specially whitelist 'addOperationWithBlock:'.
6438 if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock"))
6439 return false;
6440 str = str.substr(3);
6441 }
John McCallf85e1932011-06-15 23:02:42 +00006442 else
6443 return false;
6444
6445 if (str.empty()) return true;
Jordan Rose3f6f51e2013-02-08 22:30:41 +00006446 return !isLowercase(str.front());
John McCallf85e1932011-06-15 23:02:42 +00006447}
6448
6449/// Check a message send to see if it's likely to cause a retain cycle.
6450void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
6451 // Only check instance methods whose selector looks like a setter.
6452 if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
6453 return;
6454
6455 // Try to find a variable that the receiver is strongly owned by.
6456 RetainCycleOwner owner;
6457 if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00006458 if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner))
John McCallf85e1932011-06-15 23:02:42 +00006459 return;
6460 } else {
6461 assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
6462 owner.Variable = getCurMethodDecl()->getSelfDecl();
6463 owner.Loc = msg->getSuperLoc();
6464 owner.Range = msg->getSuperLoc();
6465 }
6466
6467 // Check whether the receiver is captured by any of the arguments.
6468 for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i)
6469 if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner))
6470 return diagnoseRetainCycle(*this, capturer, owner);
6471}
6472
6473/// Check a property assign to see if it's likely to cause a retain cycle.
6474void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
6475 RetainCycleOwner owner;
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00006476 if (!findRetainCycleOwner(*this, receiver, owner))
John McCallf85e1932011-06-15 23:02:42 +00006477 return;
6478
6479 if (Expr *capturer = findCapturingExpr(*this, argument, owner))
6480 diagnoseRetainCycle(*this, capturer, owner);
6481}
6482
Jordan Rosee10f4d32012-09-15 02:48:31 +00006483void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) {
6484 RetainCycleOwner Owner;
6485 if (!considerVariable(Var, /*DeclRefExpr=*/0, Owner))
6486 return;
6487
6488 // Because we don't have an expression for the variable, we have to set the
6489 // location explicitly here.
6490 Owner.Loc = Var->getLocation();
6491 Owner.Range = Var->getSourceRange();
6492
6493 if (Expr *Capturer = findCapturingExpr(*this, Init, Owner))
6494 diagnoseRetainCycle(*this, Capturer, Owner);
6495}
6496
Ted Kremenek9d084012012-12-21 08:04:28 +00006497static bool checkUnsafeAssignLiteral(Sema &S, SourceLocation Loc,
6498 Expr *RHS, bool isProperty) {
6499 // Check if RHS is an Objective-C object literal, which also can get
6500 // immediately zapped in a weak reference. Note that we explicitly
6501 // allow ObjCStringLiterals, since those are designed to never really die.
6502 RHS = RHS->IgnoreParenImpCasts();
Ted Kremenekf530ff72012-12-21 21:59:39 +00006503
Ted Kremenekd3292c82012-12-21 22:46:35 +00006504 // This enum needs to match with the 'select' in
6505 // warn_objc_arc_literal_assign (off-by-1).
6506 Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS);
6507 if (Kind == Sema::LK_String || Kind == Sema::LK_None)
6508 return false;
Ted Kremenekf530ff72012-12-21 21:59:39 +00006509
6510 S.Diag(Loc, diag::warn_arc_literal_assign)
Ted Kremenekd3292c82012-12-21 22:46:35 +00006511 << (unsigned) Kind
Ted Kremenek9d084012012-12-21 08:04:28 +00006512 << (isProperty ? 0 : 1)
6513 << RHS->getSourceRange();
Ted Kremenekf530ff72012-12-21 21:59:39 +00006514
6515 return true;
Ted Kremenek9d084012012-12-21 08:04:28 +00006516}
6517
Ted Kremenekb29b30f2012-12-21 19:45:30 +00006518static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc,
6519 Qualifiers::ObjCLifetime LT,
6520 Expr *RHS, bool isProperty) {
6521 // Strip off any implicit cast added to get to the one ARC-specific.
6522 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
6523 if (cast->getCastKind() == CK_ARCConsumeObject) {
6524 S.Diag(Loc, diag::warn_arc_retained_assign)
6525 << (LT == Qualifiers::OCL_ExplicitNone)
6526 << (isProperty ? 0 : 1)
6527 << RHS->getSourceRange();
6528 return true;
6529 }
6530 RHS = cast->getSubExpr();
6531 }
6532
6533 if (LT == Qualifiers::OCL_Weak &&
6534 checkUnsafeAssignLiteral(S, Loc, RHS, isProperty))
6535 return true;
6536
6537 return false;
6538}
6539
Ted Kremenekb1ea5102012-12-21 08:04:20 +00006540bool Sema::checkUnsafeAssigns(SourceLocation Loc,
6541 QualType LHS, Expr *RHS) {
6542 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
6543
6544 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
6545 return false;
6546
6547 if (checkUnsafeAssignObject(*this, Loc, LT, RHS, false))
6548 return true;
6549
6550 return false;
6551}
6552
Fariborz Jahanian921c1432011-06-24 18:25:34 +00006553void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
6554 Expr *LHS, Expr *RHS) {
Fariborz Jahanian87eaf722012-01-17 22:58:16 +00006555 QualType LHSType;
6556 // PropertyRef on LHS type need be directly obtained from
6557 // its declaration as it has a PsuedoType.
6558 ObjCPropertyRefExpr *PRE
6559 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
6560 if (PRE && !PRE->isImplicitProperty()) {
6561 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
6562 if (PD)
6563 LHSType = PD->getType();
6564 }
6565
6566 if (LHSType.isNull())
6567 LHSType = LHS->getType();
Jordan Rose7a270482012-09-28 22:21:35 +00006568
6569 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
6570
6571 if (LT == Qualifiers::OCL_Weak) {
6572 DiagnosticsEngine::Level Level =
6573 Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, Loc);
6574 if (Level != DiagnosticsEngine::Ignored)
6575 getCurFunction()->markSafeWeakUse(LHS);
6576 }
6577
Fariborz Jahanian921c1432011-06-24 18:25:34 +00006578 if (checkUnsafeAssigns(Loc, LHSType, RHS))
6579 return;
Jordan Rose7a270482012-09-28 22:21:35 +00006580
Fariborz Jahanian921c1432011-06-24 18:25:34 +00006581 // FIXME. Check for other life times.
6582 if (LT != Qualifiers::OCL_None)
6583 return;
6584
Fariborz Jahanian87eaf722012-01-17 22:58:16 +00006585 if (PRE) {
Fariborz Jahanian921c1432011-06-24 18:25:34 +00006586 if (PRE->isImplicitProperty())
6587 return;
6588 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
6589 if (!PD)
6590 return;
6591
Bill Wendlingad017fa2012-12-20 19:22:21 +00006592 unsigned Attributes = PD->getPropertyAttributes();
6593 if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) {
Fariborz Jahanian87eaf722012-01-17 22:58:16 +00006594 // when 'assign' attribute was not explicitly specified
6595 // by user, ignore it and rely on property type itself
6596 // for lifetime info.
6597 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
6598 if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) &&
6599 LHSType->isObjCRetainableType())
6600 return;
6601
Fariborz Jahanian921c1432011-06-24 18:25:34 +00006602 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
John McCall33e56f32011-09-10 06:18:15 +00006603 if (cast->getCastKind() == CK_ARCConsumeObject) {
Fariborz Jahanian921c1432011-06-24 18:25:34 +00006604 Diag(Loc, diag::warn_arc_retained_property_assign)
6605 << RHS->getSourceRange();
6606 return;
6607 }
6608 RHS = cast->getSubExpr();
6609 }
Fariborz Jahanian87eaf722012-01-17 22:58:16 +00006610 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00006611 else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) {
Ted Kremenekb1ea5102012-12-21 08:04:20 +00006612 if (checkUnsafeAssignObject(*this, Loc, Qualifiers::OCL_Weak, RHS, true))
6613 return;
Fariborz Jahanianbd2e27e2012-07-06 21:09:27 +00006614 }
Fariborz Jahanian921c1432011-06-24 18:25:34 +00006615 }
6616}
Dmitri Gribenko625bb562012-02-14 22:14:32 +00006617
6618//===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
6619
6620namespace {
6621bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
6622 SourceLocation StmtLoc,
6623 const NullStmt *Body) {
6624 // Do not warn if the body is a macro that expands to nothing, e.g:
6625 //
6626 // #define CALL(x)
6627 // if (condition)
6628 // CALL(0);
6629 //
6630 if (Body->hasLeadingEmptyMacro())
6631 return false;
6632
6633 // Get line numbers of statement and body.
6634 bool StmtLineInvalid;
6635 unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc,
6636 &StmtLineInvalid);
6637 if (StmtLineInvalid)
6638 return false;
6639
6640 bool BodyLineInvalid;
6641 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
6642 &BodyLineInvalid);
6643 if (BodyLineInvalid)
6644 return false;
6645
6646 // Warn if null statement and body are on the same line.
6647 if (StmtLine != BodyLine)
6648 return false;
6649
6650 return true;
6651}
6652} // Unnamed namespace
6653
6654void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
6655 const Stmt *Body,
6656 unsigned DiagID) {
6657 // Since this is a syntactic check, don't emit diagnostic for template
6658 // instantiations, this just adds noise.
6659 if (CurrentInstantiationScope)
6660 return;
6661
6662 // The body should be a null statement.
6663 const NullStmt *NBody = dyn_cast<NullStmt>(Body);
6664 if (!NBody)
6665 return;
6666
6667 // Do the usual checks.
6668 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
6669 return;
6670
6671 Diag(NBody->getSemiLoc(), DiagID);
6672 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
6673}
6674
6675void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
6676 const Stmt *PossibleBody) {
6677 assert(!CurrentInstantiationScope); // Ensured by caller
6678
6679 SourceLocation StmtLoc;
6680 const Stmt *Body;
6681 unsigned DiagID;
6682 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
6683 StmtLoc = FS->getRParenLoc();
6684 Body = FS->getBody();
6685 DiagID = diag::warn_empty_for_body;
6686 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
6687 StmtLoc = WS->getCond()->getSourceRange().getEnd();
6688 Body = WS->getBody();
6689 DiagID = diag::warn_empty_while_body;
6690 } else
6691 return; // Neither `for' nor `while'.
6692
6693 // The body should be a null statement.
6694 const NullStmt *NBody = dyn_cast<NullStmt>(Body);
6695 if (!NBody)
6696 return;
6697
6698 // Skip expensive checks if diagnostic is disabled.
6699 if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) ==
6700 DiagnosticsEngine::Ignored)
6701 return;
6702
6703 // Do the usual checks.
6704 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
6705 return;
6706
6707 // `for(...);' and `while(...);' are popular idioms, so in order to keep
6708 // noise level low, emit diagnostics only if for/while is followed by a
6709 // CompoundStmt, e.g.:
6710 // for (int i = 0; i < n; i++);
6711 // {
6712 // a(i);
6713 // }
6714 // or if for/while is followed by a statement with more indentation
6715 // than for/while itself:
6716 // for (int i = 0; i < n; i++);
6717 // a(i);
6718 bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
6719 if (!ProbableTypo) {
6720 bool BodyColInvalid;
6721 unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
6722 PossibleBody->getLocStart(),
6723 &BodyColInvalid);
6724 if (BodyColInvalid)
6725 return;
6726
6727 bool StmtColInvalid;
6728 unsigned StmtCol = SourceMgr.getPresumedColumnNumber(
6729 S->getLocStart(),
6730 &StmtColInvalid);
6731 if (StmtColInvalid)
6732 return;
6733
6734 if (BodyCol > StmtCol)
6735 ProbableTypo = true;
6736 }
6737
6738 if (ProbableTypo) {
6739 Diag(NBody->getSemiLoc(), DiagID);
6740 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
6741 }
6742}
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00006743
6744//===--- Layout compatibility ----------------------------------------------//
6745
6746namespace {
6747
6748bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2);
6749
6750/// \brief Check if two enumeration types are layout-compatible.
6751bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) {
6752 // C++11 [dcl.enum] p8:
6753 // Two enumeration types are layout-compatible if they have the same
6754 // underlying type.
6755 return ED1->isComplete() && ED2->isComplete() &&
6756 C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType());
6757}
6758
6759/// \brief Check if two fields are layout-compatible.
6760bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) {
6761 if (!isLayoutCompatible(C, Field1->getType(), Field2->getType()))
6762 return false;
6763
6764 if (Field1->isBitField() != Field2->isBitField())
6765 return false;
6766
6767 if (Field1->isBitField()) {
6768 // Make sure that the bit-fields are the same length.
6769 unsigned Bits1 = Field1->getBitWidthValue(C);
6770 unsigned Bits2 = Field2->getBitWidthValue(C);
6771
6772 if (Bits1 != Bits2)
6773 return false;
6774 }
6775
6776 return true;
6777}
6778
6779/// \brief Check if two standard-layout structs are layout-compatible.
6780/// (C++11 [class.mem] p17)
6781bool isLayoutCompatibleStruct(ASTContext &C,
6782 RecordDecl *RD1,
6783 RecordDecl *RD2) {
6784 // If both records are C++ classes, check that base classes match.
6785 if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) {
6786 // If one of records is a CXXRecordDecl we are in C++ mode,
6787 // thus the other one is a CXXRecordDecl, too.
6788 const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2);
6789 // Check number of base classes.
6790 if (D1CXX->getNumBases() != D2CXX->getNumBases())
6791 return false;
6792
6793 // Check the base classes.
6794 for (CXXRecordDecl::base_class_const_iterator
6795 Base1 = D1CXX->bases_begin(),
6796 BaseEnd1 = D1CXX->bases_end(),
6797 Base2 = D2CXX->bases_begin();
6798 Base1 != BaseEnd1;
6799 ++Base1, ++Base2) {
6800 if (!isLayoutCompatible(C, Base1->getType(), Base2->getType()))
6801 return false;
6802 }
6803 } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) {
6804 // If only RD2 is a C++ class, it should have zero base classes.
6805 if (D2CXX->getNumBases() > 0)
6806 return false;
6807 }
6808
6809 // Check the fields.
6810 RecordDecl::field_iterator Field2 = RD2->field_begin(),
6811 Field2End = RD2->field_end(),
6812 Field1 = RD1->field_begin(),
6813 Field1End = RD1->field_end();
6814 for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) {
6815 if (!isLayoutCompatible(C, *Field1, *Field2))
6816 return false;
6817 }
6818 if (Field1 != Field1End || Field2 != Field2End)
6819 return false;
6820
6821 return true;
6822}
6823
6824/// \brief Check if two standard-layout unions are layout-compatible.
6825/// (C++11 [class.mem] p18)
6826bool isLayoutCompatibleUnion(ASTContext &C,
6827 RecordDecl *RD1,
6828 RecordDecl *RD2) {
6829 llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields;
6830 for (RecordDecl::field_iterator Field2 = RD2->field_begin(),
6831 Field2End = RD2->field_end();
6832 Field2 != Field2End; ++Field2) {
6833 UnmatchedFields.insert(*Field2);
6834 }
6835
6836 for (RecordDecl::field_iterator Field1 = RD1->field_begin(),
6837 Field1End = RD1->field_end();
6838 Field1 != Field1End; ++Field1) {
6839 llvm::SmallPtrSet<FieldDecl *, 8>::iterator
6840 I = UnmatchedFields.begin(),
6841 E = UnmatchedFields.end();
6842
6843 for ( ; I != E; ++I) {
6844 if (isLayoutCompatible(C, *Field1, *I)) {
6845 bool Result = UnmatchedFields.erase(*I);
6846 (void) Result;
6847 assert(Result);
6848 break;
6849 }
6850 }
6851 if (I == E)
6852 return false;
6853 }
6854
6855 return UnmatchedFields.empty();
6856}
6857
6858bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) {
6859 if (RD1->isUnion() != RD2->isUnion())
6860 return false;
6861
6862 if (RD1->isUnion())
6863 return isLayoutCompatibleUnion(C, RD1, RD2);
6864 else
6865 return isLayoutCompatibleStruct(C, RD1, RD2);
6866}
6867
6868/// \brief Check if two types are layout-compatible in C++11 sense.
6869bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) {
6870 if (T1.isNull() || T2.isNull())
6871 return false;
6872
6873 // C++11 [basic.types] p11:
6874 // If two types T1 and T2 are the same type, then T1 and T2 are
6875 // layout-compatible types.
6876 if (C.hasSameType(T1, T2))
6877 return true;
6878
6879 T1 = T1.getCanonicalType().getUnqualifiedType();
6880 T2 = T2.getCanonicalType().getUnqualifiedType();
6881
6882 const Type::TypeClass TC1 = T1->getTypeClass();
6883 const Type::TypeClass TC2 = T2->getTypeClass();
6884
6885 if (TC1 != TC2)
6886 return false;
6887
6888 if (TC1 == Type::Enum) {
6889 return isLayoutCompatible(C,
6890 cast<EnumType>(T1)->getDecl(),
6891 cast<EnumType>(T2)->getDecl());
6892 } else if (TC1 == Type::Record) {
6893 if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType())
6894 return false;
6895
6896 return isLayoutCompatible(C,
6897 cast<RecordType>(T1)->getDecl(),
6898 cast<RecordType>(T2)->getDecl());
6899 }
6900
6901 return false;
6902}
6903}
6904
6905//===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----//
6906
6907namespace {
6908/// \brief Given a type tag expression find the type tag itself.
6909///
6910/// \param TypeExpr Type tag expression, as it appears in user's code.
6911///
6912/// \param VD Declaration of an identifier that appears in a type tag.
6913///
6914/// \param MagicValue Type tag magic value.
6915bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx,
6916 const ValueDecl **VD, uint64_t *MagicValue) {
6917 while(true) {
6918 if (!TypeExpr)
6919 return false;
6920
6921 TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts();
6922
6923 switch (TypeExpr->getStmtClass()) {
6924 case Stmt::UnaryOperatorClass: {
6925 const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr);
6926 if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) {
6927 TypeExpr = UO->getSubExpr();
6928 continue;
6929 }
6930 return false;
6931 }
6932
6933 case Stmt::DeclRefExprClass: {
6934 const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr);
6935 *VD = DRE->getDecl();
6936 return true;
6937 }
6938
6939 case Stmt::IntegerLiteralClass: {
6940 const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr);
6941 llvm::APInt MagicValueAPInt = IL->getValue();
6942 if (MagicValueAPInt.getActiveBits() <= 64) {
6943 *MagicValue = MagicValueAPInt.getZExtValue();
6944 return true;
6945 } else
6946 return false;
6947 }
6948
6949 case Stmt::BinaryConditionalOperatorClass:
6950 case Stmt::ConditionalOperatorClass: {
6951 const AbstractConditionalOperator *ACO =
6952 cast<AbstractConditionalOperator>(TypeExpr);
6953 bool Result;
6954 if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) {
6955 if (Result)
6956 TypeExpr = ACO->getTrueExpr();
6957 else
6958 TypeExpr = ACO->getFalseExpr();
6959 continue;
6960 }
6961 return false;
6962 }
6963
6964 case Stmt::BinaryOperatorClass: {
6965 const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr);
6966 if (BO->getOpcode() == BO_Comma) {
6967 TypeExpr = BO->getRHS();
6968 continue;
6969 }
6970 return false;
6971 }
6972
6973 default:
6974 return false;
6975 }
6976 }
6977}
6978
6979/// \brief Retrieve the C type corresponding to type tag TypeExpr.
6980///
6981/// \param TypeExpr Expression that specifies a type tag.
6982///
6983/// \param MagicValues Registered magic values.
6984///
6985/// \param FoundWrongKind Set to true if a type tag was found, but of a wrong
6986/// kind.
6987///
6988/// \param TypeInfo Information about the corresponding C type.
6989///
6990/// \returns true if the corresponding C type was found.
6991bool GetMatchingCType(
6992 const IdentifierInfo *ArgumentKind,
6993 const Expr *TypeExpr, const ASTContext &Ctx,
6994 const llvm::DenseMap<Sema::TypeTagMagicValue,
6995 Sema::TypeTagData> *MagicValues,
6996 bool &FoundWrongKind,
6997 Sema::TypeTagData &TypeInfo) {
6998 FoundWrongKind = false;
6999
7000 // Variable declaration that has type_tag_for_datatype attribute.
7001 const ValueDecl *VD = NULL;
7002
7003 uint64_t MagicValue;
7004
7005 if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue))
7006 return false;
7007
7008 if (VD) {
7009 for (specific_attr_iterator<TypeTagForDatatypeAttr>
7010 I = VD->specific_attr_begin<TypeTagForDatatypeAttr>(),
7011 E = VD->specific_attr_end<TypeTagForDatatypeAttr>();
7012 I != E; ++I) {
7013 if (I->getArgumentKind() != ArgumentKind) {
7014 FoundWrongKind = true;
7015 return false;
7016 }
7017 TypeInfo.Type = I->getMatchingCType();
7018 TypeInfo.LayoutCompatible = I->getLayoutCompatible();
7019 TypeInfo.MustBeNull = I->getMustBeNull();
7020 return true;
7021 }
7022 return false;
7023 }
7024
7025 if (!MagicValues)
7026 return false;
7027
7028 llvm::DenseMap<Sema::TypeTagMagicValue,
7029 Sema::TypeTagData>::const_iterator I =
7030 MagicValues->find(std::make_pair(ArgumentKind, MagicValue));
7031 if (I == MagicValues->end())
7032 return false;
7033
7034 TypeInfo = I->second;
7035 return true;
7036}
7037} // unnamed namespace
7038
7039void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind,
7040 uint64_t MagicValue, QualType Type,
7041 bool LayoutCompatible,
7042 bool MustBeNull) {
7043 if (!TypeTagForDatatypeMagicValues)
7044 TypeTagForDatatypeMagicValues.reset(
7045 new llvm::DenseMap<TypeTagMagicValue, TypeTagData>);
7046
7047 TypeTagMagicValue Magic(ArgumentKind, MagicValue);
7048 (*TypeTagForDatatypeMagicValues)[Magic] =
7049 TypeTagData(Type, LayoutCompatible, MustBeNull);
7050}
7051
7052namespace {
7053bool IsSameCharType(QualType T1, QualType T2) {
7054 const BuiltinType *BT1 = T1->getAs<BuiltinType>();
7055 if (!BT1)
7056 return false;
7057
7058 const BuiltinType *BT2 = T2->getAs<BuiltinType>();
7059 if (!BT2)
7060 return false;
7061
7062 BuiltinType::Kind T1Kind = BT1->getKind();
7063 BuiltinType::Kind T2Kind = BT2->getKind();
7064
7065 return (T1Kind == BuiltinType::SChar && T2Kind == BuiltinType::Char_S) ||
7066 (T1Kind == BuiltinType::UChar && T2Kind == BuiltinType::Char_U) ||
7067 (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) ||
7068 (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar);
7069}
7070} // unnamed namespace
7071
7072void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr,
7073 const Expr * const *ExprArgs) {
7074 const IdentifierInfo *ArgumentKind = Attr->getArgumentKind();
7075 bool IsPointerAttr = Attr->getIsPointer();
7076
7077 const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()];
7078 bool FoundWrongKind;
7079 TypeTagData TypeInfo;
7080 if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context,
7081 TypeTagForDatatypeMagicValues.get(),
7082 FoundWrongKind, TypeInfo)) {
7083 if (FoundWrongKind)
7084 Diag(TypeTagExpr->getExprLoc(),
7085 diag::warn_type_tag_for_datatype_wrong_kind)
7086 << TypeTagExpr->getSourceRange();
7087 return;
7088 }
7089
7090 const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()];
7091 if (IsPointerAttr) {
7092 // Skip implicit cast of pointer to `void *' (as a function argument).
7093 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr))
Dmitri Gribenko5a249802012-11-03 16:07:49 +00007094 if (ICE->getType()->isVoidPointerType() &&
Dmitri Gribenkob57ce4e2012-11-03 22:10:18 +00007095 ICE->getCastKind() == CK_BitCast)
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00007096 ArgumentExpr = ICE->getSubExpr();
7097 }
7098 QualType ArgumentType = ArgumentExpr->getType();
7099
7100 // Passing a `void*' pointer shouldn't trigger a warning.
7101 if (IsPointerAttr && ArgumentType->isVoidPointerType())
7102 return;
7103
7104 if (TypeInfo.MustBeNull) {
7105 // Type tag with matching void type requires a null pointer.
7106 if (!ArgumentExpr->isNullPointerConstant(Context,
7107 Expr::NPC_ValueDependentIsNotNull)) {
7108 Diag(ArgumentExpr->getExprLoc(),
7109 diag::warn_type_safety_null_pointer_required)
7110 << ArgumentKind->getName()
7111 << ArgumentExpr->getSourceRange()
7112 << TypeTagExpr->getSourceRange();
7113 }
7114 return;
7115 }
7116
7117 QualType RequiredType = TypeInfo.Type;
7118 if (IsPointerAttr)
7119 RequiredType = Context.getPointerType(RequiredType);
7120
7121 bool mismatch = false;
7122 if (!TypeInfo.LayoutCompatible) {
7123 mismatch = !Context.hasSameType(ArgumentType, RequiredType);
7124
7125 // C++11 [basic.fundamental] p1:
7126 // Plain char, signed char, and unsigned char are three distinct types.
7127 //
7128 // But we treat plain `char' as equivalent to `signed char' or `unsigned
7129 // char' depending on the current char signedness mode.
7130 if (mismatch)
7131 if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(),
7132 RequiredType->getPointeeType())) ||
7133 (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType)))
7134 mismatch = false;
7135 } else
7136 if (IsPointerAttr)
7137 mismatch = !isLayoutCompatible(Context,
7138 ArgumentType->getPointeeType(),
7139 RequiredType->getPointeeType());
7140 else
7141 mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType);
7142
7143 if (mismatch)
7144 Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch)
7145 << ArgumentType << ArgumentKind->getName()
7146 << TypeInfo.LayoutCompatible << RequiredType
7147 << ArgumentExpr->getSourceRange()
7148 << TypeTagExpr->getSourceRange();
7149}