blob: 0d15ce24b0c34cb6654b07623d0f8a9231ddbc8d [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 McCall5f8d6042011-08-27 01:09:30 +000015#include "clang/Sema/Initialization.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
John McCall2d887082010-08-25 22:03:47 +000017#include "clang/Sema/SemaInternal.h"
Eli Friedman276b0612011-10-11 02:20:01 +000018#include "clang/Sema/Initialization.h"
John McCall781472f2010-08-25 08:40:02 +000019#include "clang/Sema/ScopeInfo.h"
Ted Kremenek826a3452010-07-16 02:11:22 +000020#include "clang/Analysis/Analyses/FormatString.h"
Chris Lattner59907c42007-08-10 20:18:51 +000021#include "clang/AST/ASTContext.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000022#include "clang/AST/CharUnits.h"
John McCall384aff82010-08-25 07:42:41 +000023#include "clang/AST/DeclCXX.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000024#include "clang/AST/DeclObjC.h"
Ted Kremenek23245122007-08-20 16:18:38 +000025#include "clang/AST/ExprCXX.h"
Ted Kremenek7ff22b22008-06-16 18:00:42 +000026#include "clang/AST/ExprObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000027#include "clang/AST/EvaluatedExprVisitor.h"
Mike Stumpf8c49212010-01-21 03:59:47 +000028#include "clang/AST/DeclObjC.h"
29#include "clang/AST/StmtCXX.h"
30#include "clang/AST/StmtObjC.h"
Chris Lattner59907c42007-08-10 20:18:51 +000031#include "clang/Lex/Preprocessor.h"
Mike Stumpf8c49212010-01-21 03:59:47 +000032#include "llvm/ADT/BitVector.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000033#include "llvm/ADT/SmallString.h"
Mike Stumpf8c49212010-01-21 03:59:47 +000034#include "llvm/ADT/STLExtras.h"
Tom Care3bfc5f42010-06-09 04:11:11 +000035#include "llvm/Support/raw_ostream.h"
Eric Christopher691ebc32010-04-17 02:26:23 +000036#include "clang/Basic/TargetBuiltins.h"
Nate Begeman26a31422010-06-08 02:47:44 +000037#include "clang/Basic/TargetInfo.h"
Fariborz Jahanian7da71022010-09-07 19:38:13 +000038#include "clang/Basic/ConvertUTF.h"
Zhongxing Xua1f3dba2009-05-20 01:55:10 +000039#include <limits>
Chris Lattner59907c42007-08-10 20:18:51 +000040using namespace clang;
John McCall781472f2010-08-25 08:40:02 +000041using namespace sema;
Chris Lattner59907c42007-08-10 20:18:51 +000042
Chris Lattner60800082009-02-18 17:49:48 +000043SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
44 unsigned ByteNo) const {
Chris Lattner08f92e32010-11-17 07:37:15 +000045 return SL->getLocationOfByte(ByteNo, PP.getSourceManager(),
David Blaikie4e4d0842012-03-11 07:00:24 +000046 PP.getLangOpts(), PP.getTargetInfo());
Chris Lattner60800082009-02-18 17:49:48 +000047}
48
John McCall8e10f3b2011-02-26 05:39:39 +000049/// Checks that a call expression's argument count is the desired number.
50/// This is useful when doing custom type-checking. Returns true on error.
51static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
52 unsigned argCount = call->getNumArgs();
53 if (argCount == desiredArgCount) return false;
54
55 if (argCount < desiredArgCount)
56 return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args)
57 << 0 /*function call*/ << desiredArgCount << argCount
58 << call->getSourceRange();
59
60 // Highlight all the excess arguments.
61 SourceRange range(call->getArg(desiredArgCount)->getLocStart(),
62 call->getArg(argCount - 1)->getLocEnd());
63
64 return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
65 << 0 /*function call*/ << desiredArgCount << argCount
66 << call->getArg(1)->getSourceRange();
67}
68
Julien Lerouge77f68bb2011-09-09 22:41:49 +000069/// CheckBuiltinAnnotationString - Checks that string argument to the builtin
70/// annotation is a non wide string literal.
71static bool CheckBuiltinAnnotationString(Sema &S, Expr *Arg) {
72 Arg = Arg->IgnoreParenCasts();
73 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
74 if (!Literal || !Literal->isAscii()) {
75 S.Diag(Arg->getLocStart(), diag::err_builtin_annotation_not_string_constant)
76 << Arg->getSourceRange();
77 return true;
78 }
79 return false;
80}
81
John McCall60d7b3a2010-08-24 06:29:42 +000082ExprResult
Anders Carlssond406bf02009-08-16 01:56:34 +000083Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
John McCall60d7b3a2010-08-24 06:29:42 +000084 ExprResult TheCallResult(Owned(TheCall));
Douglas Gregor2def4832008-11-17 20:34:05 +000085
Chris Lattner946928f2010-10-01 23:23:24 +000086 // Find out if any arguments are required to be integer constant expressions.
87 unsigned ICEArguments = 0;
88 ASTContext::GetBuiltinTypeError Error;
89 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
90 if (Error != ASTContext::GE_None)
91 ICEArguments = 0; // Don't diagnose previously diagnosed errors.
92
93 // If any arguments are required to be ICE's, check and diagnose.
94 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
95 // Skip arguments not required to be ICE's.
96 if ((ICEArguments & (1 << ArgNo)) == 0) continue;
97
98 llvm::APSInt Result;
99 if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
100 return true;
101 ICEArguments &= ~(1 << ArgNo);
102 }
103
Anders Carlssond406bf02009-08-16 01:56:34 +0000104 switch (BuiltinID) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000105 case Builtin::BI__builtin___CFStringMakeConstantString:
Chris Lattner925e60d2007-12-28 05:29:59 +0000106 assert(TheCall->getNumArgs() == 1 &&
Chris Lattner1b9a0792007-12-20 00:26:33 +0000107 "Wrong # arguments to builtin CFStringMakeConstantString");
Chris Lattner69039812009-02-18 06:01:06 +0000108 if (CheckObjCString(TheCall->getArg(0)))
Sebastian Redl0eb23302009-01-19 00:08:26 +0000109 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000110 break;
Ted Kremenek49ff7a12008-07-09 17:58:53 +0000111 case Builtin::BI__builtin_stdarg_start:
Chris Lattner30ce3442007-12-19 23:59:04 +0000112 case Builtin::BI__builtin_va_start:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000113 if (SemaBuiltinVAStart(TheCall))
114 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000115 break;
Chris Lattner1b9a0792007-12-20 00:26:33 +0000116 case Builtin::BI__builtin_isgreater:
117 case Builtin::BI__builtin_isgreaterequal:
118 case Builtin::BI__builtin_isless:
119 case Builtin::BI__builtin_islessequal:
120 case Builtin::BI__builtin_islessgreater:
121 case Builtin::BI__builtin_isunordered:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000122 if (SemaBuiltinUnorderedCompare(TheCall))
123 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000124 break;
Benjamin Kramere771a7a2010-02-15 22:42:31 +0000125 case Builtin::BI__builtin_fpclassify:
126 if (SemaBuiltinFPClassification(TheCall, 6))
127 return ExprError();
128 break;
Eli Friedman9ac6f622009-08-31 20:06:00 +0000129 case Builtin::BI__builtin_isfinite:
130 case Builtin::BI__builtin_isinf:
131 case Builtin::BI__builtin_isinf_sign:
132 case Builtin::BI__builtin_isnan:
133 case Builtin::BI__builtin_isnormal:
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +0000134 if (SemaBuiltinFPClassification(TheCall, 1))
Eli Friedman9ac6f622009-08-31 20:06:00 +0000135 return ExprError();
136 break;
Eli Friedmand38617c2008-05-14 19:38:39 +0000137 case Builtin::BI__builtin_shufflevector:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000138 return SemaBuiltinShuffleVector(TheCall);
139 // TheCall will be freed by the smart pointer here, but that's fine, since
140 // SemaBuiltinShuffleVector guts it, but then doesn't release it.
Daniel Dunbar4493f792008-07-21 22:59:13 +0000141 case Builtin::BI__builtin_prefetch:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000142 if (SemaBuiltinPrefetch(TheCall))
143 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000144 break;
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000145 case Builtin::BI__builtin_object_size:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000146 if (SemaBuiltinObjectSize(TheCall))
147 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000148 break;
Eli Friedmand875fed2009-05-03 04:46:36 +0000149 case Builtin::BI__builtin_longjmp:
150 if (SemaBuiltinLongjmp(TheCall))
151 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000152 break;
John McCall8e10f3b2011-02-26 05:39:39 +0000153
154 case Builtin::BI__builtin_classify_type:
155 if (checkArgCount(*this, TheCall, 1)) return true;
156 TheCall->setType(Context.IntTy);
157 break;
Chris Lattner75c29a02010-10-12 17:47:42 +0000158 case Builtin::BI__builtin_constant_p:
John McCall8e10f3b2011-02-26 05:39:39 +0000159 if (checkArgCount(*this, TheCall, 1)) return true;
160 TheCall->setType(Context.IntTy);
Chris Lattner75c29a02010-10-12 17:47:42 +0000161 break;
Chris Lattner5caa3702009-05-08 06:58:22 +0000162 case Builtin::BI__sync_fetch_and_add:
Douglas Gregora9766412011-11-28 16:30:08 +0000163 case Builtin::BI__sync_fetch_and_add_1:
164 case Builtin::BI__sync_fetch_and_add_2:
165 case Builtin::BI__sync_fetch_and_add_4:
166 case Builtin::BI__sync_fetch_and_add_8:
167 case Builtin::BI__sync_fetch_and_add_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000168 case Builtin::BI__sync_fetch_and_sub:
Douglas Gregora9766412011-11-28 16:30:08 +0000169 case Builtin::BI__sync_fetch_and_sub_1:
170 case Builtin::BI__sync_fetch_and_sub_2:
171 case Builtin::BI__sync_fetch_and_sub_4:
172 case Builtin::BI__sync_fetch_and_sub_8:
173 case Builtin::BI__sync_fetch_and_sub_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000174 case Builtin::BI__sync_fetch_and_or:
Douglas Gregora9766412011-11-28 16:30:08 +0000175 case Builtin::BI__sync_fetch_and_or_1:
176 case Builtin::BI__sync_fetch_and_or_2:
177 case Builtin::BI__sync_fetch_and_or_4:
178 case Builtin::BI__sync_fetch_and_or_8:
179 case Builtin::BI__sync_fetch_and_or_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000180 case Builtin::BI__sync_fetch_and_and:
Douglas Gregora9766412011-11-28 16:30:08 +0000181 case Builtin::BI__sync_fetch_and_and_1:
182 case Builtin::BI__sync_fetch_and_and_2:
183 case Builtin::BI__sync_fetch_and_and_4:
184 case Builtin::BI__sync_fetch_and_and_8:
185 case Builtin::BI__sync_fetch_and_and_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000186 case Builtin::BI__sync_fetch_and_xor:
Douglas Gregora9766412011-11-28 16:30:08 +0000187 case Builtin::BI__sync_fetch_and_xor_1:
188 case Builtin::BI__sync_fetch_and_xor_2:
189 case Builtin::BI__sync_fetch_and_xor_4:
190 case Builtin::BI__sync_fetch_and_xor_8:
191 case Builtin::BI__sync_fetch_and_xor_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000192 case Builtin::BI__sync_add_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000193 case Builtin::BI__sync_add_and_fetch_1:
194 case Builtin::BI__sync_add_and_fetch_2:
195 case Builtin::BI__sync_add_and_fetch_4:
196 case Builtin::BI__sync_add_and_fetch_8:
197 case Builtin::BI__sync_add_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000198 case Builtin::BI__sync_sub_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000199 case Builtin::BI__sync_sub_and_fetch_1:
200 case Builtin::BI__sync_sub_and_fetch_2:
201 case Builtin::BI__sync_sub_and_fetch_4:
202 case Builtin::BI__sync_sub_and_fetch_8:
203 case Builtin::BI__sync_sub_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000204 case Builtin::BI__sync_and_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000205 case Builtin::BI__sync_and_and_fetch_1:
206 case Builtin::BI__sync_and_and_fetch_2:
207 case Builtin::BI__sync_and_and_fetch_4:
208 case Builtin::BI__sync_and_and_fetch_8:
209 case Builtin::BI__sync_and_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000210 case Builtin::BI__sync_or_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000211 case Builtin::BI__sync_or_and_fetch_1:
212 case Builtin::BI__sync_or_and_fetch_2:
213 case Builtin::BI__sync_or_and_fetch_4:
214 case Builtin::BI__sync_or_and_fetch_8:
215 case Builtin::BI__sync_or_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000216 case Builtin::BI__sync_xor_and_fetch:
Douglas Gregora9766412011-11-28 16:30:08 +0000217 case Builtin::BI__sync_xor_and_fetch_1:
218 case Builtin::BI__sync_xor_and_fetch_2:
219 case Builtin::BI__sync_xor_and_fetch_4:
220 case Builtin::BI__sync_xor_and_fetch_8:
221 case Builtin::BI__sync_xor_and_fetch_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000222 case Builtin::BI__sync_val_compare_and_swap:
Douglas Gregora9766412011-11-28 16:30:08 +0000223 case Builtin::BI__sync_val_compare_and_swap_1:
224 case Builtin::BI__sync_val_compare_and_swap_2:
225 case Builtin::BI__sync_val_compare_and_swap_4:
226 case Builtin::BI__sync_val_compare_and_swap_8:
227 case Builtin::BI__sync_val_compare_and_swap_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000228 case Builtin::BI__sync_bool_compare_and_swap:
Douglas Gregora9766412011-11-28 16:30:08 +0000229 case Builtin::BI__sync_bool_compare_and_swap_1:
230 case Builtin::BI__sync_bool_compare_and_swap_2:
231 case Builtin::BI__sync_bool_compare_and_swap_4:
232 case Builtin::BI__sync_bool_compare_and_swap_8:
233 case Builtin::BI__sync_bool_compare_and_swap_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000234 case Builtin::BI__sync_lock_test_and_set:
Douglas Gregora9766412011-11-28 16:30:08 +0000235 case Builtin::BI__sync_lock_test_and_set_1:
236 case Builtin::BI__sync_lock_test_and_set_2:
237 case Builtin::BI__sync_lock_test_and_set_4:
238 case Builtin::BI__sync_lock_test_and_set_8:
239 case Builtin::BI__sync_lock_test_and_set_16:
Chris Lattner5caa3702009-05-08 06:58:22 +0000240 case Builtin::BI__sync_lock_release:
Douglas Gregora9766412011-11-28 16:30:08 +0000241 case Builtin::BI__sync_lock_release_1:
242 case Builtin::BI__sync_lock_release_2:
243 case Builtin::BI__sync_lock_release_4:
244 case Builtin::BI__sync_lock_release_8:
245 case Builtin::BI__sync_lock_release_16:
Chris Lattner23aa9c82011-04-09 03:57:26 +0000246 case Builtin::BI__sync_swap:
Douglas Gregora9766412011-11-28 16:30:08 +0000247 case Builtin::BI__sync_swap_1:
248 case Builtin::BI__sync_swap_2:
249 case Builtin::BI__sync_swap_4:
250 case Builtin::BI__sync_swap_8:
251 case Builtin::BI__sync_swap_16:
Chandler Carruthd2014572010-07-09 18:59:35 +0000252 return SemaBuiltinAtomicOverloaded(move(TheCallResult));
Richard Smithff34d402012-04-12 05:08:17 +0000253#define BUILTIN(ID, TYPE, ATTRS)
254#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
255 case Builtin::BI##ID: \
256 return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::AO##ID);
257#include "clang/Basic/Builtins.def"
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000258 case Builtin::BI__builtin_annotation:
259 if (CheckBuiltinAnnotationString(*this, TheCall->getArg(1)))
260 return ExprError();
261 break;
Nate Begeman26a31422010-06-08 02:47:44 +0000262 }
263
264 // Since the target specific builtins for each arch overlap, only check those
265 // of the arch we are compiling for.
266 if (BuiltinID >= Builtin::FirstTSBuiltin) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000267 switch (Context.getTargetInfo().getTriple().getArch()) {
Nate Begeman26a31422010-06-08 02:47:44 +0000268 case llvm::Triple::arm:
269 case llvm::Triple::thumb:
270 if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
271 return ExprError();
272 break;
Nate Begeman26a31422010-06-08 02:47:44 +0000273 default:
274 break;
275 }
276 }
277
278 return move(TheCallResult);
279}
280
Nate Begeman61eecf52010-06-14 05:21:25 +0000281// Get the valid immediate range for the specified NEON type code.
282static unsigned RFT(unsigned t, bool shift = false) {
Bob Wilsonda95f732011-11-08 01:16:11 +0000283 NeonTypeFlags Type(t);
284 int IsQuad = Type.isQuad();
285 switch (Type.getEltType()) {
286 case NeonTypeFlags::Int8:
287 case NeonTypeFlags::Poly8:
288 return shift ? 7 : (8 << IsQuad) - 1;
289 case NeonTypeFlags::Int16:
290 case NeonTypeFlags::Poly16:
291 return shift ? 15 : (4 << IsQuad) - 1;
292 case NeonTypeFlags::Int32:
293 return shift ? 31 : (2 << IsQuad) - 1;
294 case NeonTypeFlags::Int64:
295 return shift ? 63 : (1 << IsQuad) - 1;
296 case NeonTypeFlags::Float16:
297 assert(!shift && "cannot shift float types!");
298 return (4 << IsQuad) - 1;
299 case NeonTypeFlags::Float32:
300 assert(!shift && "cannot shift float types!");
301 return (2 << IsQuad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000302 }
David Blaikie7530c032012-01-17 06:56:22 +0000303 llvm_unreachable("Invalid NeonTypeFlag!");
Nate Begeman61eecf52010-06-14 05:21:25 +0000304}
305
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000306/// getNeonEltType - Return the QualType corresponding to the elements of
307/// the vector type specified by the NeonTypeFlags. This is used to check
308/// the pointer arguments for Neon load/store intrinsics.
309static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) {
310 switch (Flags.getEltType()) {
311 case NeonTypeFlags::Int8:
312 return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy;
313 case NeonTypeFlags::Int16:
314 return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy;
315 case NeonTypeFlags::Int32:
316 return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy;
317 case NeonTypeFlags::Int64:
318 return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy;
319 case NeonTypeFlags::Poly8:
320 return Context.SignedCharTy;
321 case NeonTypeFlags::Poly16:
322 return Context.ShortTy;
323 case NeonTypeFlags::Float16:
324 return Context.UnsignedShortTy;
325 case NeonTypeFlags::Float32:
326 return Context.FloatTy;
327 }
David Blaikie7530c032012-01-17 06:56:22 +0000328 llvm_unreachable("Invalid NeonTypeFlag!");
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000329}
330
Nate Begeman26a31422010-06-08 02:47:44 +0000331bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000332 llvm::APSInt Result;
333
Nate Begeman0d15c532010-06-13 04:47:52 +0000334 unsigned mask = 0;
Nate Begeman61eecf52010-06-14 05:21:25 +0000335 unsigned TV = 0;
Bob Wilson46482552011-11-16 21:32:23 +0000336 int PtrArgNum = -1;
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000337 bool HasConstPtr = false;
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000338 switch (BuiltinID) {
Nate Begemana23326b2010-06-17 04:17:01 +0000339#define GET_NEON_OVERLOAD_CHECK
340#include "clang/Basic/arm_neon.inc"
341#undef GET_NEON_OVERLOAD_CHECK
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000342 }
343
Nate Begeman0d15c532010-06-13 04:47:52 +0000344 // For NEON intrinsics which are overloaded on vector element type, validate
345 // the immediate which specifies which variant to emit.
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000346 unsigned ImmArg = TheCall->getNumArgs()-1;
Nate Begeman0d15c532010-06-13 04:47:52 +0000347 if (mask) {
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000348 if (SemaBuiltinConstantArg(TheCall, ImmArg, Result))
Nate Begeman0d15c532010-06-13 04:47:52 +0000349 return true;
350
Bob Wilsonda95f732011-11-08 01:16:11 +0000351 TV = Result.getLimitedValue(64);
352 if ((TV > 63) || (mask & (1 << TV)) == 0)
Nate Begeman0d15c532010-06-13 04:47:52 +0000353 return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000354 << TheCall->getArg(ImmArg)->getSourceRange();
355 }
356
Bob Wilson46482552011-11-16 21:32:23 +0000357 if (PtrArgNum >= 0) {
Bob Wilson6f9f03e2011-11-08 05:04:11 +0000358 // Check that pointer arguments have the specified type.
Bob Wilson46482552011-11-16 21:32:23 +0000359 Expr *Arg = TheCall->getArg(PtrArgNum);
360 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg))
361 Arg = ICE->getSubExpr();
362 ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg);
363 QualType RHSTy = RHS.get()->getType();
364 QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context);
365 if (HasConstPtr)
366 EltTy = EltTy.withConst();
367 QualType LHSTy = Context.getPointerType(EltTy);
368 AssignConvertType ConvTy;
369 ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS);
370 if (RHS.isInvalid())
371 return true;
372 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy,
373 RHS.get(), AA_Assigning))
374 return true;
Nate Begeman0d15c532010-06-13 04:47:52 +0000375 }
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000376
Nate Begeman0d15c532010-06-13 04:47:52 +0000377 // For NEON intrinsics which take an immediate value as part of the
378 // instruction, range check them here.
Nate Begeman61eecf52010-06-14 05:21:25 +0000379 unsigned i = 0, l = 0, u = 0;
Nate Begeman0d15c532010-06-13 04:47:52 +0000380 switch (BuiltinID) {
381 default: return false;
Nate Begemanbb37f502010-07-29 22:48:34 +0000382 case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break;
383 case ARM::BI__builtin_arm_usat: i = 1; u = 31; break;
Nate Begeman99c40bb2010-08-03 21:32:34 +0000384 case ARM::BI__builtin_arm_vcvtr_f:
385 case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break;
Nate Begemana23326b2010-06-17 04:17:01 +0000386#define GET_NEON_IMMEDIATE_CHECK
387#include "clang/Basic/arm_neon.inc"
388#undef GET_NEON_IMMEDIATE_CHECK
Nate Begeman0d15c532010-06-13 04:47:52 +0000389 };
390
Nate Begeman61eecf52010-06-14 05:21:25 +0000391 // Check that the immediate argument is actually a constant.
Nate Begeman0d15c532010-06-13 04:47:52 +0000392 if (SemaBuiltinConstantArg(TheCall, i, Result))
393 return true;
394
Nate Begeman61eecf52010-06-14 05:21:25 +0000395 // Range check against the upper/lower values for this isntruction.
Nate Begeman0d15c532010-06-13 04:47:52 +0000396 unsigned Val = Result.getZExtValue();
Nate Begeman61eecf52010-06-14 05:21:25 +0000397 if (Val < l || Val > (u + l))
Nate Begeman0d15c532010-06-13 04:47:52 +0000398 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000399 << l << u+l << TheCall->getArg(i)->getSourceRange();
Nate Begeman0d15c532010-06-13 04:47:52 +0000400
Nate Begeman99c40bb2010-08-03 21:32:34 +0000401 // FIXME: VFP Intrinsics should error if VFP not present.
Nate Begeman26a31422010-06-08 02:47:44 +0000402 return false;
Anders Carlssond406bf02009-08-16 01:56:34 +0000403}
Daniel Dunbarde454282008-10-02 18:44:07 +0000404
Anders Carlssond406bf02009-08-16 01:56:34 +0000405/// CheckFunctionCall - Check a direct function call for various correctness
406/// and safety properties not strictly enforced by the C type system.
407bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) {
408 // Get the IdentifierInfo* for the called function.
409 IdentifierInfo *FnInfo = FDecl->getIdentifier();
410
411 // None of the checks below are needed for functions that don't have
412 // simple names (e.g., C++ conversion functions).
413 if (!FnInfo)
414 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Daniel Dunbarde454282008-10-02 18:44:07 +0000416 // FIXME: This mechanism should be abstracted to be less fragile and
417 // more efficient. For example, just map function ids to custom
418 // handlers.
419
Ted Kremenekc82faca2010-09-09 04:33:05 +0000420 // Printf and scanf checking.
421 for (specific_attr_iterator<FormatAttr>
422 i = FDecl->specific_attr_begin<FormatAttr>(),
423 e = FDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) {
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +0000424 CheckFormatArguments(*i, TheCall);
Chris Lattner59907c42007-08-10 20:18:51 +0000425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenekc82faca2010-09-09 04:33:05 +0000427 for (specific_attr_iterator<NonNullAttr>
428 i = FDecl->specific_attr_begin<NonNullAttr>(),
429 e = FDecl->specific_attr_end<NonNullAttr>(); i != e; ++i) {
Nick Lewycky909a70d2011-03-25 01:44:32 +0000430 CheckNonNullArguments(*i, TheCall->getArgs(),
431 TheCall->getCallee()->getLocStart());
Ted Kremenekc82faca2010-09-09 04:33:05 +0000432 }
Sebastian Redl0eb23302009-01-19 00:08:26 +0000433
Anna Zaks0a151a12012-01-17 00:37:07 +0000434 unsigned CMId = FDecl->getMemoryFunctionKind();
435 if (CMId == 0)
Anna Zaksd9b859a2012-01-13 21:52:01 +0000436 return false;
Ted Kremenekbd5da9d2011-08-18 20:55:45 +0000437
Anna Zaksd9b859a2012-01-13 21:52:01 +0000438 // Handle memory setting and copying functions.
Anna Zaks0a151a12012-01-17 00:37:07 +0000439 if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat)
Ted Kremenekbd5da9d2011-08-18 20:55:45 +0000440 CheckStrlcpycatArguments(TheCall, FnInfo);
Anna Zaksc36bedc2012-02-01 19:08:57 +0000441 else if (CMId == Builtin::BIstrncat)
442 CheckStrncatArguments(TheCall, FnInfo);
Anna Zaksd9b859a2012-01-13 21:52:01 +0000443 else
Anna Zaks0a151a12012-01-17 00:37:07 +0000444 CheckMemaccessArguments(TheCall, CMId, FnInfo);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +0000445
Anders Carlssond406bf02009-08-16 01:56:34 +0000446 return false;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000447}
448
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +0000449bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac,
450 Expr **Args, unsigned NumArgs) {
451 for (specific_attr_iterator<FormatAttr>
452 i = Method->specific_attr_begin<FormatAttr>(),
453 e = Method->specific_attr_end<FormatAttr>(); i != e ; ++i) {
454
455 CheckFormatArguments(*i, Args, NumArgs, false, lbrac,
456 Method->getSourceRange());
457 }
458
459 // diagnose nonnull arguments.
460 for (specific_attr_iterator<NonNullAttr>
461 i = Method->specific_attr_begin<NonNullAttr>(),
462 e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) {
463 CheckNonNullArguments(*i, Args, lbrac);
464 }
465
466 return false;
467}
468
Anders Carlssond406bf02009-08-16 01:56:34 +0000469bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) {
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000470 const VarDecl *V = dyn_cast<VarDecl>(NDecl);
471 if (!V)
Anders Carlssond406bf02009-08-16 01:56:34 +0000472 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000474 QualType Ty = V->getType();
475 if (!Ty->isBlockPointerType())
Anders Carlssond406bf02009-08-16 01:56:34 +0000476 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Jean-Daniel Dupas43d12512012-01-25 00:55:11 +0000478 // format string checking.
479 for (specific_attr_iterator<FormatAttr>
480 i = NDecl->specific_attr_begin<FormatAttr>(),
481 e = NDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) {
482 CheckFormatArguments(*i, TheCall);
483 }
Anders Carlssond406bf02009-08-16 01:56:34 +0000484
485 return false;
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000486}
487
Richard Smithff34d402012-04-12 05:08:17 +0000488ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult,
489 AtomicExpr::AtomicOp Op) {
Eli Friedman276b0612011-10-11 02:20:01 +0000490 CallExpr *TheCall = cast<CallExpr>(TheCallResult.get());
491 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
Eli Friedman276b0612011-10-11 02:20:01 +0000492
Richard Smithff34d402012-04-12 05:08:17 +0000493 // All these operations take one of the following forms:
494 enum {
495 // C __c11_atomic_init(A *, C)
496 Init,
497 // C __c11_atomic_load(A *, int)
498 Load,
499 // void __atomic_load(A *, CP, int)
500 Copy,
501 // C __c11_atomic_add(A *, M, int)
502 Arithmetic,
503 // C __atomic_exchange_n(A *, CP, int)
504 Xchg,
505 // void __atomic_exchange(A *, C *, CP, int)
506 GNUXchg,
507 // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int)
508 C11CmpXchg,
509 // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int)
510 GNUCmpXchg
511 } Form = Init;
512 const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 };
513 const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 };
514 // where:
515 // C is an appropriate type,
516 // A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins,
517 // CP is C for __c11 builtins and GNU _n builtins and is C * otherwise,
518 // M is C if C is an integer, and ptrdiff_t if C is a pointer, and
519 // the int parameters are for orderings.
Eli Friedman276b0612011-10-11 02:20:01 +0000520
Richard Smithff34d402012-04-12 05:08:17 +0000521 assert(AtomicExpr::AO__c11_atomic_init == 0 &&
522 AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load
523 && "need to update code for modified C11 atomics");
524 bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init &&
525 Op <= AtomicExpr::AO__c11_atomic_fetch_xor;
526 bool IsN = Op == AtomicExpr::AO__atomic_load_n ||
527 Op == AtomicExpr::AO__atomic_store_n ||
528 Op == AtomicExpr::AO__atomic_exchange_n ||
529 Op == AtomicExpr::AO__atomic_compare_exchange_n;
530 bool IsAddSub = false;
531
532 switch (Op) {
533 case AtomicExpr::AO__c11_atomic_init:
534 Form = Init;
535 break;
536
537 case AtomicExpr::AO__c11_atomic_load:
538 case AtomicExpr::AO__atomic_load_n:
539 Form = Load;
540 break;
541
542 case AtomicExpr::AO__c11_atomic_store:
543 case AtomicExpr::AO__atomic_load:
544 case AtomicExpr::AO__atomic_store:
545 case AtomicExpr::AO__atomic_store_n:
546 Form = Copy;
547 break;
548
549 case AtomicExpr::AO__c11_atomic_fetch_add:
550 case AtomicExpr::AO__c11_atomic_fetch_sub:
551 case AtomicExpr::AO__atomic_fetch_add:
552 case AtomicExpr::AO__atomic_fetch_sub:
553 case AtomicExpr::AO__atomic_add_fetch:
554 case AtomicExpr::AO__atomic_sub_fetch:
555 IsAddSub = true;
556 // Fall through.
557 case AtomicExpr::AO__c11_atomic_fetch_and:
558 case AtomicExpr::AO__c11_atomic_fetch_or:
559 case AtomicExpr::AO__c11_atomic_fetch_xor:
560 case AtomicExpr::AO__atomic_fetch_and:
561 case AtomicExpr::AO__atomic_fetch_or:
562 case AtomicExpr::AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +0000563 case AtomicExpr::AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +0000564 case AtomicExpr::AO__atomic_and_fetch:
565 case AtomicExpr::AO__atomic_or_fetch:
566 case AtomicExpr::AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +0000567 case AtomicExpr::AO__atomic_nand_fetch:
Richard Smithff34d402012-04-12 05:08:17 +0000568 Form = Arithmetic;
569 break;
570
571 case AtomicExpr::AO__c11_atomic_exchange:
572 case AtomicExpr::AO__atomic_exchange_n:
573 Form = Xchg;
574 break;
575
576 case AtomicExpr::AO__atomic_exchange:
577 Form = GNUXchg;
578 break;
579
580 case AtomicExpr::AO__c11_atomic_compare_exchange_strong:
581 case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
582 Form = C11CmpXchg;
583 break;
584
585 case AtomicExpr::AO__atomic_compare_exchange:
586 case AtomicExpr::AO__atomic_compare_exchange_n:
587 Form = GNUCmpXchg;
588 break;
589 }
590
591 // Check we have the right number of arguments.
592 if (TheCall->getNumArgs() < NumArgs[Form]) {
Eli Friedman276b0612011-10-11 02:20:01 +0000593 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Richard Smithff34d402012-04-12 05:08:17 +0000594 << 0 << NumArgs[Form] << TheCall->getNumArgs()
Eli Friedman276b0612011-10-11 02:20:01 +0000595 << TheCall->getCallee()->getSourceRange();
596 return ExprError();
Richard Smithff34d402012-04-12 05:08:17 +0000597 } else if (TheCall->getNumArgs() > NumArgs[Form]) {
598 Diag(TheCall->getArg(NumArgs[Form])->getLocStart(),
Eli Friedman276b0612011-10-11 02:20:01 +0000599 diag::err_typecheck_call_too_many_args)
Richard Smithff34d402012-04-12 05:08:17 +0000600 << 0 << NumArgs[Form] << TheCall->getNumArgs()
Eli Friedman276b0612011-10-11 02:20:01 +0000601 << TheCall->getCallee()->getSourceRange();
602 return ExprError();
603 }
604
Richard Smithff34d402012-04-12 05:08:17 +0000605 // Inspect the first argument of the atomic operation.
Eli Friedmandfa64ba2011-10-14 22:48:56 +0000606 Expr *Ptr = TheCall->getArg(0);
Eli Friedman276b0612011-10-11 02:20:01 +0000607 Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get();
608 const PointerType *pointerType = Ptr->getType()->getAs<PointerType>();
609 if (!pointerType) {
Richard Smithff34d402012-04-12 05:08:17 +0000610 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
Eli Friedman276b0612011-10-11 02:20:01 +0000611 << Ptr->getType() << Ptr->getSourceRange();
612 return ExprError();
613 }
614
Richard Smithff34d402012-04-12 05:08:17 +0000615 // For a __c11 builtin, this should be a pointer to an _Atomic type.
616 QualType AtomTy = pointerType->getPointeeType(); // 'A'
617 QualType ValType = AtomTy; // 'C'
618 if (IsC11) {
619 if (!AtomTy->isAtomicType()) {
620 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic)
621 << Ptr->getType() << Ptr->getSourceRange();
622 return ExprError();
623 }
624 ValType = AtomTy->getAs<AtomicType>()->getValueType();
Eli Friedman276b0612011-10-11 02:20:01 +0000625 }
Eli Friedman276b0612011-10-11 02:20:01 +0000626
Richard Smithff34d402012-04-12 05:08:17 +0000627 // For an arithmetic operation, the implied arithmetic must be well-formed.
628 if (Form == Arithmetic) {
629 // gcc does not enforce these rules for GNU atomics, but we do so for sanity.
630 if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) {
631 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
632 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
633 return ExprError();
634 }
635 if (!IsAddSub && !ValType->isIntegerType()) {
636 Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int)
637 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
638 return ExprError();
639 }
640 } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) {
641 // For __atomic_*_n operations, the value type must be a scalar integral or
642 // pointer type which is 1, 2, 4, 8 or 16 bytes in length.
Eli Friedman276b0612011-10-11 02:20:01 +0000643 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr)
Richard Smithff34d402012-04-12 05:08:17 +0000644 << IsC11 << Ptr->getType() << Ptr->getSourceRange();
645 return ExprError();
646 }
647
648 if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) {
649 // For GNU atomics, require a trivially-copyable type. This is not part of
650 // the GNU atomics specification, but we enforce it for sanity.
651 Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy)
Eli Friedman276b0612011-10-11 02:20:01 +0000652 << Ptr->getType() << Ptr->getSourceRange();
653 return ExprError();
654 }
655
Richard Smithff34d402012-04-12 05:08:17 +0000656 // FIXME: For any builtin other than a load, the ValType must not be
657 // const-qualified.
Eli Friedman276b0612011-10-11 02:20:01 +0000658
659 switch (ValType.getObjCLifetime()) {
660 case Qualifiers::OCL_None:
661 case Qualifiers::OCL_ExplicitNone:
662 // okay
663 break;
664
665 case Qualifiers::OCL_Weak:
666 case Qualifiers::OCL_Strong:
667 case Qualifiers::OCL_Autoreleasing:
Richard Smithff34d402012-04-12 05:08:17 +0000668 // FIXME: Can this happen? By this point, ValType should be known
669 // to be trivially copyable.
Eli Friedman276b0612011-10-11 02:20:01 +0000670 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
671 << ValType << Ptr->getSourceRange();
672 return ExprError();
673 }
674
675 QualType ResultType = ValType;
Richard Smithff34d402012-04-12 05:08:17 +0000676 if (Form == Copy || Form == GNUXchg || Form == Init)
Eli Friedman276b0612011-10-11 02:20:01 +0000677 ResultType = Context.VoidTy;
Richard Smithff34d402012-04-12 05:08:17 +0000678 else if (Form == C11CmpXchg || Form == GNUCmpXchg)
Eli Friedman276b0612011-10-11 02:20:01 +0000679 ResultType = Context.BoolTy;
680
Richard Smithff34d402012-04-12 05:08:17 +0000681 // The type of a parameter passed 'by value'. In the GNU atomics, such
682 // arguments are actually passed as pointers.
683 QualType ByValType = ValType; // 'CP'
684 if (!IsC11 && !IsN)
685 ByValType = Ptr->getType();
686
Eli Friedman276b0612011-10-11 02:20:01 +0000687 // The first argument --- the pointer --- has a fixed type; we
688 // deduce the types of the rest of the arguments accordingly. Walk
689 // the remaining arguments, converting them to the deduced value type.
Richard Smithff34d402012-04-12 05:08:17 +0000690 for (unsigned i = 1; i != NumArgs[Form]; ++i) {
Eli Friedman276b0612011-10-11 02:20:01 +0000691 QualType Ty;
Richard Smithff34d402012-04-12 05:08:17 +0000692 if (i < NumVals[Form] + 1) {
693 switch (i) {
694 case 1:
695 // The second argument is the non-atomic operand. For arithmetic, this
696 // is always passed by value, and for a compare_exchange it is always
697 // passed by address. For the rest, GNU uses by-address and C11 uses
698 // by-value.
699 assert(Form != Load);
700 if (Form == Init || (Form == Arithmetic && ValType->isIntegerType()))
701 Ty = ValType;
702 else if (Form == Copy || Form == Xchg)
703 Ty = ByValType;
704 else if (Form == Arithmetic)
705 Ty = Context.getPointerDiffType();
706 else
707 Ty = Context.getPointerType(ValType.getUnqualifiedType());
708 break;
709 case 2:
710 // The third argument to compare_exchange / GNU exchange is a
711 // (pointer to a) desired value.
712 Ty = ByValType;
713 break;
714 case 3:
715 // The fourth argument to GNU compare_exchange is a 'weak' flag.
716 Ty = Context.BoolTy;
717 break;
718 }
Eli Friedman276b0612011-10-11 02:20:01 +0000719 } else {
720 // The order(s) are always converted to int.
721 Ty = Context.IntTy;
722 }
Richard Smithff34d402012-04-12 05:08:17 +0000723
Eli Friedman276b0612011-10-11 02:20:01 +0000724 InitializedEntity Entity =
725 InitializedEntity::InitializeParameter(Context, Ty, false);
Richard Smithff34d402012-04-12 05:08:17 +0000726 ExprResult Arg = TheCall->getArg(i);
Eli Friedman276b0612011-10-11 02:20:01 +0000727 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
728 if (Arg.isInvalid())
729 return true;
730 TheCall->setArg(i, Arg.get());
731 }
732
Richard Smithff34d402012-04-12 05:08:17 +0000733 // Permute the arguments into a 'consistent' order.
Eli Friedmandfa64ba2011-10-14 22:48:56 +0000734 SmallVector<Expr*, 5> SubExprs;
735 SubExprs.push_back(Ptr);
Richard Smithff34d402012-04-12 05:08:17 +0000736 switch (Form) {
737 case Init:
738 // Note, AtomicExpr::getVal1() has a special case for this atomic.
David Chisnall7a7ee302012-01-16 17:27:18 +0000739 SubExprs.push_back(TheCall->getArg(1)); // Val1
Richard Smithff34d402012-04-12 05:08:17 +0000740 break;
741 case Load:
742 SubExprs.push_back(TheCall->getArg(1)); // Order
743 break;
744 case Copy:
745 case Arithmetic:
746 case Xchg:
Eli Friedmandfa64ba2011-10-14 22:48:56 +0000747 SubExprs.push_back(TheCall->getArg(2)); // Order
748 SubExprs.push_back(TheCall->getArg(1)); // Val1
Richard Smithff34d402012-04-12 05:08:17 +0000749 break;
750 case GNUXchg:
751 // Note, AtomicExpr::getVal2() has a special case for this atomic.
752 SubExprs.push_back(TheCall->getArg(3)); // Order
753 SubExprs.push_back(TheCall->getArg(1)); // Val1
754 SubExprs.push_back(TheCall->getArg(2)); // Val2
755 break;
756 case C11CmpXchg:
Eli Friedmandfa64ba2011-10-14 22:48:56 +0000757 SubExprs.push_back(TheCall->getArg(3)); // Order
758 SubExprs.push_back(TheCall->getArg(1)); // Val1
Eli Friedmandfa64ba2011-10-14 22:48:56 +0000759 SubExprs.push_back(TheCall->getArg(4)); // OrderFail
David Chisnall2ebb98a2012-03-29 17:58:59 +0000760 SubExprs.push_back(TheCall->getArg(2)); // Val2
Richard Smithff34d402012-04-12 05:08:17 +0000761 break;
762 case GNUCmpXchg:
763 SubExprs.push_back(TheCall->getArg(4)); // Order
764 SubExprs.push_back(TheCall->getArg(1)); // Val1
765 SubExprs.push_back(TheCall->getArg(5)); // OrderFail
766 SubExprs.push_back(TheCall->getArg(2)); // Val2
767 SubExprs.push_back(TheCall->getArg(3)); // Weak
768 break;
Eli Friedman276b0612011-10-11 02:20:01 +0000769 }
Eli Friedmandfa64ba2011-10-14 22:48:56 +0000770
771 return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(),
772 SubExprs.data(), SubExprs.size(),
773 ResultType, Op,
774 TheCall->getRParenLoc()));
Eli Friedman276b0612011-10-11 02:20:01 +0000775}
776
777
John McCall5f8d6042011-08-27 01:09:30 +0000778/// checkBuiltinArgument - Given a call to a builtin function, perform
779/// normal type-checking on the given argument, updating the call in
780/// place. This is useful when a builtin function requires custom
781/// type-checking for some of its arguments but not necessarily all of
782/// them.
783///
784/// Returns true on error.
785static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
786 FunctionDecl *Fn = E->getDirectCallee();
787 assert(Fn && "builtin call without direct callee!");
788
789 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
790 InitializedEntity Entity =
791 InitializedEntity::InitializeParameter(S.Context, Param);
792
793 ExprResult Arg = E->getArg(0);
794 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
795 if (Arg.isInvalid())
796 return true;
797
798 E->setArg(ArgIndex, Arg.take());
799 return false;
800}
801
Chris Lattner5caa3702009-05-08 06:58:22 +0000802/// SemaBuiltinAtomicOverloaded - We have a call to a function like
803/// __sync_fetch_and_add, which is an overloaded function based on the pointer
804/// type of its first argument. The main ActOnCallExpr routines have already
805/// promoted the types of arguments because all of these calls are prototyped as
806/// void(...).
807///
808/// This function goes through and does final semantic checking for these
809/// builtins,
John McCall60d7b3a2010-08-24 06:29:42 +0000810ExprResult
811Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
Chandler Carruthd2014572010-07-09 18:59:35 +0000812 CallExpr *TheCall = (CallExpr *)TheCallResult.get();
Chris Lattner5caa3702009-05-08 06:58:22 +0000813 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
814 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
815
816 // Ensure that we have at least one argument to do type inference from.
Chandler Carruthd2014572010-07-09 18:59:35 +0000817 if (TheCall->getNumArgs() < 1) {
818 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
819 << 0 << 1 << TheCall->getNumArgs()
820 << TheCall->getCallee()->getSourceRange();
821 return ExprError();
822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Chris Lattner5caa3702009-05-08 06:58:22 +0000824 // Inspect the first argument of the atomic builtin. This should always be
825 // a pointer type, whose element is an integral scalar or pointer type.
826 // Because it is a pointer type, we don't have to worry about any implicit
827 // casts here.
Chandler Carruthd2014572010-07-09 18:59:35 +0000828 // FIXME: We don't allow floating point scalars as input.
Chris Lattner5caa3702009-05-08 06:58:22 +0000829 Expr *FirstArg = TheCall->getArg(0);
Eli Friedman8c382062012-01-23 02:35:22 +0000830 ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg);
831 if (FirstArgResult.isInvalid())
832 return ExprError();
833 FirstArg = FirstArgResult.take();
834 TheCall->setArg(0, FirstArg);
835
John McCallf85e1932011-06-15 23:02:42 +0000836 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
837 if (!pointerType) {
Chandler Carruthd2014572010-07-09 18:59:35 +0000838 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
839 << FirstArg->getType() << FirstArg->getSourceRange();
840 return ExprError();
841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
John McCallf85e1932011-06-15 23:02:42 +0000843 QualType ValType = pointerType->getPointeeType();
Chris Lattnerdd5fa7a2010-09-17 21:12:38 +0000844 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
Chandler Carruthd2014572010-07-09 18:59:35 +0000845 !ValType->isBlockPointerType()) {
846 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
847 << FirstArg->getType() << FirstArg->getSourceRange();
848 return ExprError();
849 }
Chris Lattner5caa3702009-05-08 06:58:22 +0000850
John McCallf85e1932011-06-15 23:02:42 +0000851 switch (ValType.getObjCLifetime()) {
852 case Qualifiers::OCL_None:
853 case Qualifiers::OCL_ExplicitNone:
854 // okay
855 break;
856
857 case Qualifiers::OCL_Weak:
858 case Qualifiers::OCL_Strong:
859 case Qualifiers::OCL_Autoreleasing:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000860 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000861 << ValType << FirstArg->getSourceRange();
862 return ExprError();
863 }
864
John McCallb45ae252011-10-05 07:41:44 +0000865 // Strip any qualifiers off ValType.
866 ValType = ValType.getUnqualifiedType();
867
Chandler Carruth8d13d222010-07-18 20:54:12 +0000868 // The majority of builtins return a value, but a few have special return
869 // types, so allow them to override appropriately below.
870 QualType ResultType = ValType;
871
Chris Lattner5caa3702009-05-08 06:58:22 +0000872 // We need to figure out which concrete builtin this maps onto. For example,
873 // __sync_fetch_and_add with a 2 byte object turns into
874 // __sync_fetch_and_add_2.
875#define BUILTIN_ROW(x) \
876 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
877 Builtin::BI##x##_8, Builtin::BI##x##_16 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattner5caa3702009-05-08 06:58:22 +0000879 static const unsigned BuiltinIndices[][5] = {
880 BUILTIN_ROW(__sync_fetch_and_add),
881 BUILTIN_ROW(__sync_fetch_and_sub),
882 BUILTIN_ROW(__sync_fetch_and_or),
883 BUILTIN_ROW(__sync_fetch_and_and),
884 BUILTIN_ROW(__sync_fetch_and_xor),
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Chris Lattner5caa3702009-05-08 06:58:22 +0000886 BUILTIN_ROW(__sync_add_and_fetch),
887 BUILTIN_ROW(__sync_sub_and_fetch),
888 BUILTIN_ROW(__sync_and_and_fetch),
889 BUILTIN_ROW(__sync_or_and_fetch),
890 BUILTIN_ROW(__sync_xor_and_fetch),
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner5caa3702009-05-08 06:58:22 +0000892 BUILTIN_ROW(__sync_val_compare_and_swap),
893 BUILTIN_ROW(__sync_bool_compare_and_swap),
894 BUILTIN_ROW(__sync_lock_test_and_set),
Chris Lattner23aa9c82011-04-09 03:57:26 +0000895 BUILTIN_ROW(__sync_lock_release),
896 BUILTIN_ROW(__sync_swap)
Chris Lattner5caa3702009-05-08 06:58:22 +0000897 };
Mike Stump1eb44332009-09-09 15:08:12 +0000898#undef BUILTIN_ROW
899
Chris Lattner5caa3702009-05-08 06:58:22 +0000900 // Determine the index of the size.
901 unsigned SizeIndex;
Ken Dyck199c3d62010-01-11 17:06:35 +0000902 switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
Chris Lattner5caa3702009-05-08 06:58:22 +0000903 case 1: SizeIndex = 0; break;
904 case 2: SizeIndex = 1; break;
905 case 4: SizeIndex = 2; break;
906 case 8: SizeIndex = 3; break;
907 case 16: SizeIndex = 4; break;
908 default:
Chandler Carruthd2014572010-07-09 18:59:35 +0000909 Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
910 << FirstArg->getType() << FirstArg->getSourceRange();
911 return ExprError();
Chris Lattner5caa3702009-05-08 06:58:22 +0000912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Chris Lattner5caa3702009-05-08 06:58:22 +0000914 // Each of these builtins has one pointer argument, followed by some number of
915 // values (0, 1 or 2) followed by a potentially empty varags list of stuff
916 // that we ignore. Find out which row of BuiltinIndices to read from as well
917 // as the number of fixed args.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000918 unsigned BuiltinID = FDecl->getBuiltinID();
Chris Lattner5caa3702009-05-08 06:58:22 +0000919 unsigned BuiltinIndex, NumFixed = 1;
920 switch (BuiltinID) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000921 default: llvm_unreachable("Unknown overloaded atomic builtin!");
Douglas Gregora9766412011-11-28 16:30:08 +0000922 case Builtin::BI__sync_fetch_and_add:
923 case Builtin::BI__sync_fetch_and_add_1:
924 case Builtin::BI__sync_fetch_and_add_2:
925 case Builtin::BI__sync_fetch_and_add_4:
926 case Builtin::BI__sync_fetch_and_add_8:
927 case Builtin::BI__sync_fetch_and_add_16:
928 BuiltinIndex = 0;
929 break;
930
931 case Builtin::BI__sync_fetch_and_sub:
932 case Builtin::BI__sync_fetch_and_sub_1:
933 case Builtin::BI__sync_fetch_and_sub_2:
934 case Builtin::BI__sync_fetch_and_sub_4:
935 case Builtin::BI__sync_fetch_and_sub_8:
936 case Builtin::BI__sync_fetch_and_sub_16:
937 BuiltinIndex = 1;
938 break;
939
940 case Builtin::BI__sync_fetch_and_or:
941 case Builtin::BI__sync_fetch_and_or_1:
942 case Builtin::BI__sync_fetch_and_or_2:
943 case Builtin::BI__sync_fetch_and_or_4:
944 case Builtin::BI__sync_fetch_and_or_8:
945 case Builtin::BI__sync_fetch_and_or_16:
946 BuiltinIndex = 2;
947 break;
948
949 case Builtin::BI__sync_fetch_and_and:
950 case Builtin::BI__sync_fetch_and_and_1:
951 case Builtin::BI__sync_fetch_and_and_2:
952 case Builtin::BI__sync_fetch_and_and_4:
953 case Builtin::BI__sync_fetch_and_and_8:
954 case Builtin::BI__sync_fetch_and_and_16:
955 BuiltinIndex = 3;
956 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregora9766412011-11-28 16:30:08 +0000958 case Builtin::BI__sync_fetch_and_xor:
959 case Builtin::BI__sync_fetch_and_xor_1:
960 case Builtin::BI__sync_fetch_and_xor_2:
961 case Builtin::BI__sync_fetch_and_xor_4:
962 case Builtin::BI__sync_fetch_and_xor_8:
963 case Builtin::BI__sync_fetch_and_xor_16:
964 BuiltinIndex = 4;
965 break;
966
967 case Builtin::BI__sync_add_and_fetch:
968 case Builtin::BI__sync_add_and_fetch_1:
969 case Builtin::BI__sync_add_and_fetch_2:
970 case Builtin::BI__sync_add_and_fetch_4:
971 case Builtin::BI__sync_add_and_fetch_8:
972 case Builtin::BI__sync_add_and_fetch_16:
973 BuiltinIndex = 5;
974 break;
975
976 case Builtin::BI__sync_sub_and_fetch:
977 case Builtin::BI__sync_sub_and_fetch_1:
978 case Builtin::BI__sync_sub_and_fetch_2:
979 case Builtin::BI__sync_sub_and_fetch_4:
980 case Builtin::BI__sync_sub_and_fetch_8:
981 case Builtin::BI__sync_sub_and_fetch_16:
982 BuiltinIndex = 6;
983 break;
984
985 case Builtin::BI__sync_and_and_fetch:
986 case Builtin::BI__sync_and_and_fetch_1:
987 case Builtin::BI__sync_and_and_fetch_2:
988 case Builtin::BI__sync_and_and_fetch_4:
989 case Builtin::BI__sync_and_and_fetch_8:
990 case Builtin::BI__sync_and_and_fetch_16:
991 BuiltinIndex = 7;
992 break;
993
994 case Builtin::BI__sync_or_and_fetch:
995 case Builtin::BI__sync_or_and_fetch_1:
996 case Builtin::BI__sync_or_and_fetch_2:
997 case Builtin::BI__sync_or_and_fetch_4:
998 case Builtin::BI__sync_or_and_fetch_8:
999 case Builtin::BI__sync_or_and_fetch_16:
1000 BuiltinIndex = 8;
1001 break;
1002
1003 case Builtin::BI__sync_xor_and_fetch:
1004 case Builtin::BI__sync_xor_and_fetch_1:
1005 case Builtin::BI__sync_xor_and_fetch_2:
1006 case Builtin::BI__sync_xor_and_fetch_4:
1007 case Builtin::BI__sync_xor_and_fetch_8:
1008 case Builtin::BI__sync_xor_and_fetch_16:
1009 BuiltinIndex = 9;
1010 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner5caa3702009-05-08 06:58:22 +00001012 case Builtin::BI__sync_val_compare_and_swap:
Douglas Gregora9766412011-11-28 16:30:08 +00001013 case Builtin::BI__sync_val_compare_and_swap_1:
1014 case Builtin::BI__sync_val_compare_and_swap_2:
1015 case Builtin::BI__sync_val_compare_and_swap_4:
1016 case Builtin::BI__sync_val_compare_and_swap_8:
1017 case Builtin::BI__sync_val_compare_and_swap_16:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +00001018 BuiltinIndex = 10;
Chris Lattner5caa3702009-05-08 06:58:22 +00001019 NumFixed = 2;
1020 break;
Douglas Gregora9766412011-11-28 16:30:08 +00001021
Chris Lattner5caa3702009-05-08 06:58:22 +00001022 case Builtin::BI__sync_bool_compare_and_swap:
Douglas Gregora9766412011-11-28 16:30:08 +00001023 case Builtin::BI__sync_bool_compare_and_swap_1:
1024 case Builtin::BI__sync_bool_compare_and_swap_2:
1025 case Builtin::BI__sync_bool_compare_and_swap_4:
1026 case Builtin::BI__sync_bool_compare_and_swap_8:
1027 case Builtin::BI__sync_bool_compare_and_swap_16:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +00001028 BuiltinIndex = 11;
Chris Lattner5caa3702009-05-08 06:58:22 +00001029 NumFixed = 2;
Chandler Carruth8d13d222010-07-18 20:54:12 +00001030 ResultType = Context.BoolTy;
Chris Lattner5caa3702009-05-08 06:58:22 +00001031 break;
Douglas Gregora9766412011-11-28 16:30:08 +00001032
1033 case Builtin::BI__sync_lock_test_and_set:
1034 case Builtin::BI__sync_lock_test_and_set_1:
1035 case Builtin::BI__sync_lock_test_and_set_2:
1036 case Builtin::BI__sync_lock_test_and_set_4:
1037 case Builtin::BI__sync_lock_test_and_set_8:
1038 case Builtin::BI__sync_lock_test_and_set_16:
1039 BuiltinIndex = 12;
1040 break;
1041
Chris Lattner5caa3702009-05-08 06:58:22 +00001042 case Builtin::BI__sync_lock_release:
Douglas Gregora9766412011-11-28 16:30:08 +00001043 case Builtin::BI__sync_lock_release_1:
1044 case Builtin::BI__sync_lock_release_2:
1045 case Builtin::BI__sync_lock_release_4:
1046 case Builtin::BI__sync_lock_release_8:
1047 case Builtin::BI__sync_lock_release_16:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +00001048 BuiltinIndex = 13;
Chris Lattner5caa3702009-05-08 06:58:22 +00001049 NumFixed = 0;
Chandler Carruth8d13d222010-07-18 20:54:12 +00001050 ResultType = Context.VoidTy;
Chris Lattner5caa3702009-05-08 06:58:22 +00001051 break;
Douglas Gregora9766412011-11-28 16:30:08 +00001052
1053 case Builtin::BI__sync_swap:
1054 case Builtin::BI__sync_swap_1:
1055 case Builtin::BI__sync_swap_2:
1056 case Builtin::BI__sync_swap_4:
1057 case Builtin::BI__sync_swap_8:
1058 case Builtin::BI__sync_swap_16:
1059 BuiltinIndex = 14;
1060 break;
Chris Lattner5caa3702009-05-08 06:58:22 +00001061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Chris Lattner5caa3702009-05-08 06:58:22 +00001063 // Now that we know how many fixed arguments we expect, first check that we
1064 // have at least that many.
Chandler Carruthd2014572010-07-09 18:59:35 +00001065 if (TheCall->getNumArgs() < 1+NumFixed) {
1066 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
1067 << 0 << 1+NumFixed << TheCall->getNumArgs()
1068 << TheCall->getCallee()->getSourceRange();
1069 return ExprError();
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattnere7ac0a92009-05-08 15:36:58 +00001072 // Get the decl for the concrete builtin from this, we can tell what the
1073 // concrete integer type we should convert to is.
1074 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
1075 const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID);
1076 IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName);
Mike Stump1eb44332009-09-09 15:08:12 +00001077 FunctionDecl *NewBuiltinDecl =
Chris Lattnere7ac0a92009-05-08 15:36:58 +00001078 cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID,
1079 TUScope, false, DRE->getLocStart()));
Chandler Carruthd2014572010-07-09 18:59:35 +00001080
John McCallf871d0c2010-08-07 06:22:56 +00001081 // The first argument --- the pointer --- has a fixed type; we
1082 // deduce the types of the rest of the arguments accordingly. Walk
1083 // the remaining arguments, converting them to the deduced value type.
Chris Lattner5caa3702009-05-08 06:58:22 +00001084 for (unsigned i = 0; i != NumFixed; ++i) {
John Wiegley429bb272011-04-08 18:41:53 +00001085 ExprResult Arg = TheCall->getArg(i+1);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattner5caa3702009-05-08 06:58:22 +00001087 // GCC does an implicit conversion to the pointer or integer ValType. This
1088 // can fail in some cases (1i -> int**), check for this error case now.
John McCallb45ae252011-10-05 07:41:44 +00001089 // Initialize the argument.
1090 InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
1091 ValType, /*consume*/ false);
1092 Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg);
John Wiegley429bb272011-04-08 18:41:53 +00001093 if (Arg.isInvalid())
Chandler Carruthd2014572010-07-09 18:59:35 +00001094 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Chris Lattner5caa3702009-05-08 06:58:22 +00001096 // Okay, we have something that *can* be converted to the right type. Check
1097 // to see if there is a potentially weird extension going on here. This can
1098 // happen when you do an atomic operation on something like an char* and
1099 // pass in 42. The 42 gets converted to char. This is even more strange
1100 // for things like 45.123 -> char, etc.
Mike Stump1eb44332009-09-09 15:08:12 +00001101 // FIXME: Do this check.
John McCallb45ae252011-10-05 07:41:44 +00001102 TheCall->setArg(i+1, Arg.take());
Chris Lattner5caa3702009-05-08 06:58:22 +00001103 }
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +00001105 ASTContext& Context = this->getASTContext();
1106
1107 // Create a new DeclRefExpr to refer to the new decl.
1108 DeclRefExpr* NewDRE = DeclRefExpr::Create(
1109 Context,
1110 DRE->getQualifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001111 SourceLocation(),
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +00001112 NewBuiltinDecl,
John McCallf4b88a42012-03-10 09:33:50 +00001113 /*enclosing*/ false,
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +00001114 DRE->getLocation(),
1115 NewBuiltinDecl->getType(),
1116 DRE->getValueKind());
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Chris Lattner5caa3702009-05-08 06:58:22 +00001118 // Set the callee in the CallExpr.
1119 // FIXME: This leaks the original parens and implicit casts.
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +00001120 ExprResult PromotedCall = UsualUnaryConversions(NewDRE);
John Wiegley429bb272011-04-08 18:41:53 +00001121 if (PromotedCall.isInvalid())
1122 return ExprError();
1123 TheCall->setCallee(PromotedCall.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Chandler Carruthdb4325b2010-07-18 07:23:17 +00001125 // Change the result type of the call to match the original value type. This
1126 // is arbitrary, but the codegen for these builtins ins design to handle it
1127 // gracefully.
Chandler Carruth8d13d222010-07-18 20:54:12 +00001128 TheCall->setType(ResultType);
Chandler Carruthd2014572010-07-09 18:59:35 +00001129
1130 return move(TheCallResult);
Chris Lattner5caa3702009-05-08 06:58:22 +00001131}
1132
Chris Lattner69039812009-02-18 06:01:06 +00001133/// CheckObjCString - Checks that the argument to the builtin
Anders Carlsson71993dd2007-08-17 05:31:46 +00001134/// CFString constructor is correct
Steve Narofffd942622009-04-13 20:26:29 +00001135/// Note: It might also make sense to do the UTF-16 conversion here (would
1136/// simplify the backend).
Chris Lattner69039812009-02-18 06:01:06 +00001137bool Sema::CheckObjCString(Expr *Arg) {
Chris Lattner56f34942008-02-13 01:02:39 +00001138 Arg = Arg->IgnoreParenCasts();
Anders Carlsson71993dd2007-08-17 05:31:46 +00001139 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
1140
Douglas Gregor5cee1192011-07-27 05:40:30 +00001141 if (!Literal || !Literal->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001142 Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
1143 << Arg->getSourceRange();
Anders Carlsson9cdc4d32007-08-17 15:44:17 +00001144 return true;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Fariborz Jahanian7da71022010-09-07 19:38:13 +00001147 if (Literal->containsNonAsciiOrNull()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001148 StringRef String = Literal->getString();
Fariborz Jahanian7da71022010-09-07 19:38:13 +00001149 unsigned NumBytes = String.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +00001150 SmallVector<UTF16, 128> ToBuf(NumBytes);
Fariborz Jahanian7da71022010-09-07 19:38:13 +00001151 const UTF8 *FromPtr = (UTF8 *)String.data();
1152 UTF16 *ToPtr = &ToBuf[0];
1153
1154 ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
1155 &ToPtr, ToPtr + NumBytes,
1156 strictConversion);
1157 // Check for conversion failure.
1158 if (Result != conversionOK)
1159 Diag(Arg->getLocStart(),
1160 diag::warn_cfstring_truncated) << Arg->getSourceRange();
1161 }
Anders Carlsson9cdc4d32007-08-17 15:44:17 +00001162 return false;
Chris Lattner59907c42007-08-10 20:18:51 +00001163}
1164
Chris Lattnerc27c6652007-12-20 00:05:45 +00001165/// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
1166/// Emit an error and return true on failure, return false on success.
Chris Lattner925e60d2007-12-28 05:29:59 +00001167bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
1168 Expr *Fn = TheCall->getCallee();
1169 if (TheCall->getNumArgs() > 2) {
Chris Lattner2c21a072008-11-21 18:44:24 +00001170 Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001171 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +00001172 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
1173 << Fn->getSourceRange()
Mike Stump1eb44332009-09-09 15:08:12 +00001174 << SourceRange(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001175 (*(TheCall->arg_end()-1))->getLocEnd());
Chris Lattner30ce3442007-12-19 23:59:04 +00001176 return true;
1177 }
Eli Friedman56f20ae2008-12-15 22:05:35 +00001178
1179 if (TheCall->getNumArgs() < 2) {
Eric Christopherd77b9a22010-04-16 04:48:22 +00001180 return Diag(TheCall->getLocEnd(),
1181 diag::err_typecheck_call_too_few_args_at_least)
1182 << 0 /*function call*/ << 2 << TheCall->getNumArgs();
Eli Friedman56f20ae2008-12-15 22:05:35 +00001183 }
1184
John McCall5f8d6042011-08-27 01:09:30 +00001185 // Type-check the first argument normally.
1186 if (checkBuiltinArgument(*this, TheCall, 0))
1187 return true;
1188
Chris Lattnerc27c6652007-12-20 00:05:45 +00001189 // Determine whether the current function is variadic or not.
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001190 BlockScopeInfo *CurBlock = getCurBlock();
Chris Lattnerc27c6652007-12-20 00:05:45 +00001191 bool isVariadic;
Steve Naroffcd9c5142009-04-15 19:33:47 +00001192 if (CurBlock)
John McCallc71a4912010-06-04 19:02:56 +00001193 isVariadic = CurBlock->TheDecl->isVariadic();
Ted Kremenek9498d382010-04-29 16:49:01 +00001194 else if (FunctionDecl *FD = getCurFunctionDecl())
1195 isVariadic = FD->isVariadic();
1196 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001197 isVariadic = getCurMethodDecl()->isVariadic();
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Chris Lattnerc27c6652007-12-20 00:05:45 +00001199 if (!isVariadic) {
Chris Lattner30ce3442007-12-19 23:59:04 +00001200 Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
1201 return true;
1202 }
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Chris Lattner30ce3442007-12-19 23:59:04 +00001204 // Verify that the second argument to the builtin is the last argument of the
1205 // current function or method.
1206 bool SecondArgIsLastNamedArgument = false;
Anders Carlssone2c14102008-02-13 01:22:59 +00001207 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Anders Carlsson88cf2262008-02-11 04:20:54 +00001209 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
1210 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
Chris Lattner30ce3442007-12-19 23:59:04 +00001211 // FIXME: This isn't correct for methods (results in bogus warning).
1212 // Get the last formal in the current function.
Anders Carlsson88cf2262008-02-11 04:20:54 +00001213 const ParmVarDecl *LastArg;
Steve Naroffcd9c5142009-04-15 19:33:47 +00001214 if (CurBlock)
1215 LastArg = *(CurBlock->TheDecl->param_end()-1);
1216 else if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner371f2582008-12-04 23:50:19 +00001217 LastArg = *(FD->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +00001218 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +00001219 LastArg = *(getCurMethodDecl()->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +00001220 SecondArgIsLastNamedArgument = PV == LastArg;
1221 }
1222 }
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Chris Lattner30ce3442007-12-19 23:59:04 +00001224 if (!SecondArgIsLastNamedArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001225 Diag(TheCall->getArg(1)->getLocStart(),
Chris Lattner30ce3442007-12-19 23:59:04 +00001226 diag::warn_second_parameter_of_va_start_not_last_named_argument);
1227 return false;
Eli Friedman6cfda232008-05-20 08:23:37 +00001228}
Chris Lattner30ce3442007-12-19 23:59:04 +00001229
Chris Lattner1b9a0792007-12-20 00:26:33 +00001230/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
1231/// friends. This is declared to take (...), so we have to check everything.
Chris Lattner925e60d2007-12-28 05:29:59 +00001232bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
1233 if (TheCall->getNumArgs() < 2)
Chris Lattner2c21a072008-11-21 18:44:24 +00001234 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Eric Christopherd77b9a22010-04-16 04:48:22 +00001235 << 0 << 2 << TheCall->getNumArgs()/*function call*/;
Chris Lattner925e60d2007-12-28 05:29:59 +00001236 if (TheCall->getNumArgs() > 2)
Mike Stump1eb44332009-09-09 15:08:12 +00001237 return Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001238 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +00001239 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001240 << SourceRange(TheCall->getArg(2)->getLocStart(),
1241 (*(TheCall->arg_end()-1))->getLocEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001242
John Wiegley429bb272011-04-08 18:41:53 +00001243 ExprResult OrigArg0 = TheCall->getArg(0);
1244 ExprResult OrigArg1 = TheCall->getArg(1);
Douglas Gregorcde01732009-05-19 22:10:17 +00001245
Chris Lattner1b9a0792007-12-20 00:26:33 +00001246 // Do standard promotions between the two arguments, returning their common
1247 // type.
Chris Lattner925e60d2007-12-28 05:29:59 +00001248 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
John Wiegley429bb272011-04-08 18:41:53 +00001249 if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
1250 return true;
Daniel Dunbar403bc2b2009-02-19 19:28:43 +00001251
1252 // Make sure any conversions are pushed back into the call; this is
1253 // type safe since unordered compare builtins are declared as "_Bool
1254 // foo(...)".
John Wiegley429bb272011-04-08 18:41:53 +00001255 TheCall->setArg(0, OrigArg0.get());
1256 TheCall->setArg(1, OrigArg1.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001257
John Wiegley429bb272011-04-08 18:41:53 +00001258 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
Douglas Gregorcde01732009-05-19 22:10:17 +00001259 return false;
1260
Chris Lattner1b9a0792007-12-20 00:26:33 +00001261 // If the common type isn't a real floating type, then the arguments were
1262 // invalid for this operation.
1263 if (!Res->isRealFloatingType())
John Wiegley429bb272011-04-08 18:41:53 +00001264 return Diag(OrigArg0.get()->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001265 diag::err_typecheck_call_invalid_ordered_compare)
John Wiegley429bb272011-04-08 18:41:53 +00001266 << OrigArg0.get()->getType() << OrigArg1.get()->getType()
1267 << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Chris Lattner1b9a0792007-12-20 00:26:33 +00001269 return false;
1270}
1271
Benjamin Kramere771a7a2010-02-15 22:42:31 +00001272/// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
1273/// __builtin_isnan and friends. This is declared to take (...), so we have
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +00001274/// to check everything. We expect the last argument to be a floating point
1275/// value.
1276bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
1277 if (TheCall->getNumArgs() < NumArgs)
Eli Friedman9ac6f622009-08-31 20:06:00 +00001278 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Eric Christopherd77b9a22010-04-16 04:48:22 +00001279 << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +00001280 if (TheCall->getNumArgs() > NumArgs)
1281 return Diag(TheCall->getArg(NumArgs)->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +00001282 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +00001283 << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +00001284 << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +00001285 (*(TheCall->arg_end()-1))->getLocEnd());
1286
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +00001287 Expr *OrigArg = TheCall->getArg(NumArgs-1);
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Eli Friedman9ac6f622009-08-31 20:06:00 +00001289 if (OrigArg->isTypeDependent())
1290 return false;
1291
Chris Lattner81368fb2010-05-06 05:50:07 +00001292 // This operation requires a non-_Complex floating-point number.
Eli Friedman9ac6f622009-08-31 20:06:00 +00001293 if (!OrigArg->getType()->isRealFloatingType())
Mike Stump1eb44332009-09-09 15:08:12 +00001294 return Diag(OrigArg->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +00001295 diag::err_typecheck_call_invalid_unary_fp)
1296 << OrigArg->getType() << OrigArg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Chris Lattner81368fb2010-05-06 05:50:07 +00001298 // If this is an implicit conversion from float -> double, remove it.
1299 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
1300 Expr *CastArg = Cast->getSubExpr();
1301 if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
1302 assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) &&
1303 "promotion from float to double is the only expected cast here");
1304 Cast->setSubExpr(0);
Chris Lattner81368fb2010-05-06 05:50:07 +00001305 TheCall->setArg(NumArgs-1, CastArg);
Chris Lattner81368fb2010-05-06 05:50:07 +00001306 }
1307 }
1308
Eli Friedman9ac6f622009-08-31 20:06:00 +00001309 return false;
1310}
1311
Eli Friedmand38617c2008-05-14 19:38:39 +00001312/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
1313// This is declared to take (...), so we have to check everything.
John McCall60d7b3a2010-08-24 06:29:42 +00001314ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
Nate Begeman37b6a572010-06-08 00:16:34 +00001315 if (TheCall->getNumArgs() < 2)
Sebastian Redl0eb23302009-01-19 00:08:26 +00001316 return ExprError(Diag(TheCall->getLocEnd(),
Eric Christopherd77b9a22010-04-16 04:48:22 +00001317 diag::err_typecheck_call_too_few_args_at_least)
Nate Begeman37b6a572010-06-08 00:16:34 +00001318 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
Eric Christopherd77b9a22010-04-16 04:48:22 +00001319 << TheCall->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +00001320
Nate Begeman37b6a572010-06-08 00:16:34 +00001321 // Determine which of the following types of shufflevector we're checking:
1322 // 1) unary, vector mask: (lhs, mask)
1323 // 2) binary, vector mask: (lhs, rhs, mask)
1324 // 3) binary, scalar mask: (lhs, rhs, index, ..., index)
1325 QualType resType = TheCall->getArg(0)->getType();
1326 unsigned numElements = 0;
1327
Douglas Gregorcde01732009-05-19 22:10:17 +00001328 if (!TheCall->getArg(0)->isTypeDependent() &&
1329 !TheCall->getArg(1)->isTypeDependent()) {
Nate Begeman37b6a572010-06-08 00:16:34 +00001330 QualType LHSType = TheCall->getArg(0)->getType();
1331 QualType RHSType = TheCall->getArg(1)->getType();
1332
1333 if (!LHSType->isVectorType() || !RHSType->isVectorType()) {
Douglas Gregorcde01732009-05-19 22:10:17 +00001334 Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector)
Mike Stump1eb44332009-09-09 15:08:12 +00001335 << SourceRange(TheCall->getArg(0)->getLocStart(),
Douglas Gregorcde01732009-05-19 22:10:17 +00001336 TheCall->getArg(1)->getLocEnd());
1337 return ExprError();
1338 }
Nate Begeman37b6a572010-06-08 00:16:34 +00001339
1340 numElements = LHSType->getAs<VectorType>()->getNumElements();
1341 unsigned numResElements = TheCall->getNumArgs() - 2;
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Nate Begeman37b6a572010-06-08 00:16:34 +00001343 // Check to see if we have a call with 2 vector arguments, the unary shuffle
1344 // with mask. If so, verify that RHS is an integer vector type with the
1345 // same number of elts as lhs.
1346 if (TheCall->getNumArgs() == 2) {
Douglas Gregorf6094622010-07-23 15:58:24 +00001347 if (!RHSType->hasIntegerRepresentation() ||
Nate Begeman37b6a572010-06-08 00:16:34 +00001348 RHSType->getAs<VectorType>()->getNumElements() != numElements)
1349 Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
1350 << SourceRange(TheCall->getArg(1)->getLocStart(),
1351 TheCall->getArg(1)->getLocEnd());
1352 numResElements = numElements;
1353 }
1354 else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
Douglas Gregorcde01732009-05-19 22:10:17 +00001355 Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
Mike Stump1eb44332009-09-09 15:08:12 +00001356 << SourceRange(TheCall->getArg(0)->getLocStart(),
Douglas Gregorcde01732009-05-19 22:10:17 +00001357 TheCall->getArg(1)->getLocEnd());
1358 return ExprError();
Nate Begeman37b6a572010-06-08 00:16:34 +00001359 } else if (numElements != numResElements) {
1360 QualType eltType = LHSType->getAs<VectorType>()->getElementType();
Chris Lattner788b0fd2010-06-23 06:00:24 +00001361 resType = Context.getVectorType(eltType, numResElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00001362 VectorType::GenericVector);
Douglas Gregorcde01732009-05-19 22:10:17 +00001363 }
Eli Friedmand38617c2008-05-14 19:38:39 +00001364 }
1365
1366 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
Douglas Gregorcde01732009-05-19 22:10:17 +00001367 if (TheCall->getArg(i)->isTypeDependent() ||
1368 TheCall->getArg(i)->isValueDependent())
1369 continue;
1370
Nate Begeman37b6a572010-06-08 00:16:34 +00001371 llvm::APSInt Result(32);
1372 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
1373 return ExprError(Diag(TheCall->getLocStart(),
1374 diag::err_shufflevector_nonconstant_argument)
1375 << TheCall->getArg(i)->getSourceRange());
Sebastian Redl0eb23302009-01-19 00:08:26 +00001376
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +00001377 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
Sebastian Redl0eb23302009-01-19 00:08:26 +00001378 return ExprError(Diag(TheCall->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001379 diag::err_shufflevector_argument_too_large)
Sebastian Redl0eb23302009-01-19 00:08:26 +00001380 << TheCall->getArg(i)->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +00001381 }
1382
Chris Lattner5f9e2722011-07-23 10:55:15 +00001383 SmallVector<Expr*, 32> exprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001384
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +00001385 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
Eli Friedmand38617c2008-05-14 19:38:39 +00001386 exprs.push_back(TheCall->getArg(i));
1387 TheCall->setArg(i, 0);
1388 }
1389
Nate Begemana88dc302009-08-12 02:10:25 +00001390 return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(),
Nate Begeman37b6a572010-06-08 00:16:34 +00001391 exprs.size(), resType,
Ted Kremenek8189cde2009-02-07 01:47:29 +00001392 TheCall->getCallee()->getLocStart(),
1393 TheCall->getRParenLoc()));
Eli Friedmand38617c2008-05-14 19:38:39 +00001394}
Chris Lattner30ce3442007-12-19 23:59:04 +00001395
Daniel Dunbar4493f792008-07-21 22:59:13 +00001396/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
1397// This is declared to take (const void*, ...) and can take two
1398// optional constant int args.
1399bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001400 unsigned NumArgs = TheCall->getNumArgs();
Daniel Dunbar4493f792008-07-21 22:59:13 +00001401
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001402 if (NumArgs > 3)
Eric Christopherccfa9632010-04-16 04:56:46 +00001403 return Diag(TheCall->getLocEnd(),
1404 diag::err_typecheck_call_too_many_args_at_most)
1405 << 0 /*function call*/ << 3 << NumArgs
1406 << TheCall->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +00001407
1408 // Argument 0 is checked for us and the remaining arguments must be
1409 // constant integers.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001410 for (unsigned i = 1; i != NumArgs; ++i) {
Daniel Dunbar4493f792008-07-21 22:59:13 +00001411 Expr *Arg = TheCall->getArg(i);
Eric Christopher691ebc32010-04-17 02:26:23 +00001412
Eli Friedman9aef7262009-12-04 00:30:06 +00001413 llvm::APSInt Result;
Eric Christopher691ebc32010-04-17 02:26:23 +00001414 if (SemaBuiltinConstantArg(TheCall, i, Result))
1415 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Daniel Dunbar4493f792008-07-21 22:59:13 +00001417 // FIXME: gcc issues a warning and rewrites these to 0. These
1418 // seems especially odd for the third argument since the default
1419 // is 3.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001420 if (i == 1) {
Eli Friedman9aef7262009-12-04 00:30:06 +00001421 if (Result.getLimitedValue() > 1)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001422 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Chris Lattner21fb98e2009-09-23 06:06:36 +00001423 << "0" << "1" << Arg->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +00001424 } else {
Eli Friedman9aef7262009-12-04 00:30:06 +00001425 if (Result.getLimitedValue() > 3)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001426 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Chris Lattner21fb98e2009-09-23 06:06:36 +00001427 << "0" << "3" << Arg->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +00001428 }
1429 }
1430
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001431 return false;
Daniel Dunbar4493f792008-07-21 22:59:13 +00001432}
1433
Eric Christopher691ebc32010-04-17 02:26:23 +00001434/// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
1435/// TheCall is a constant expression.
1436bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
1437 llvm::APSInt &Result) {
1438 Expr *Arg = TheCall->getArg(ArgNum);
1439 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
1440 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
1441
1442 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
1443
1444 if (!Arg->isIntegerConstantExpr(Result, Context))
1445 return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
Eric Christopher5e896552010-04-19 18:23:02 +00001446 << FDecl->getDeclName() << Arg->getSourceRange();
Eric Christopher691ebc32010-04-17 02:26:23 +00001447
Chris Lattner21fb98e2009-09-23 06:06:36 +00001448 return false;
1449}
1450
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001451/// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
1452/// int type). This simply type checks that type is one of the defined
1453/// constants (0-3).
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001454// For compatibility check 0-3, llvm only handles 0 and 2.
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001455bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
Eric Christopher691ebc32010-04-17 02:26:23 +00001456 llvm::APSInt Result;
1457
1458 // Check constant-ness first.
1459 if (SemaBuiltinConstantArg(TheCall, 1, Result))
1460 return true;
1461
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001462 Expr *Arg = TheCall->getArg(1);
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001463 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001464 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
1465 << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +00001466 }
1467
1468 return false;
1469}
1470
Eli Friedman586d6a82009-05-03 06:04:26 +00001471/// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
Eli Friedmand875fed2009-05-03 04:46:36 +00001472/// This checks that val is a constant 1.
1473bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
1474 Expr *Arg = TheCall->getArg(1);
Eric Christopher691ebc32010-04-17 02:26:23 +00001475 llvm::APSInt Result;
Douglas Gregorcde01732009-05-19 22:10:17 +00001476
Eric Christopher691ebc32010-04-17 02:26:23 +00001477 // TODO: This is less than ideal. Overload this to take a value.
1478 if (SemaBuiltinConstantArg(TheCall, 1, Result))
1479 return true;
1480
1481 if (Result != 1)
Eli Friedmand875fed2009-05-03 04:46:36 +00001482 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
1483 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
1484
1485 return false;
1486}
1487
Ted Kremenekb43e8ad2011-02-24 23:03:04 +00001488// Handle i > 1 ? "x" : "y", recursively.
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001489bool Sema::SemaCheckStringLiteral(const Expr *E, Expr **Args,
1490 unsigned NumArgs, bool HasVAListArg,
Ted Kremenek826a3452010-07-16 02:11:22 +00001491 unsigned format_idx, unsigned firstDataArg,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001492 FormatStringType Type, bool inFunctionCall) {
Ted Kremenek4fe64412010-09-09 03:51:39 +00001493 tryAgain:
Douglas Gregorcde01732009-05-19 22:10:17 +00001494 if (E->isTypeDependent() || E->isValueDependent())
1495 return false;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001496
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001497 E = E->IgnoreParenCasts();
Peter Collingbournef111d932011-04-15 00:35:48 +00001498
David Blaikiea73cdcb2012-02-10 21:07:25 +00001499 if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
1500 // Technically -Wformat-nonliteral does not warn about this case.
1501 // The behavior of printf and friends in this case is implementation
1502 // dependent. Ideally if the format string cannot be null then
1503 // it should have a 'nonnull' attribute in the function prototype.
1504 return true;
1505
Ted Kremenekd30ef872009-01-12 23:09:09 +00001506 switch (E->getStmtClass()) {
John McCall56ca35d2011-02-17 10:25:35 +00001507 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenekd30ef872009-01-12 23:09:09 +00001508 case Stmt::ConditionalOperatorClass: {
John McCall56ca35d2011-02-17 10:25:35 +00001509 const AbstractConditionalOperator *C = cast<AbstractConditionalOperator>(E);
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001510 return SemaCheckStringLiteral(C->getTrueExpr(), Args, NumArgs, HasVAListArg,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001511 format_idx, firstDataArg, Type,
Richard Trieu55733de2011-10-28 00:41:25 +00001512 inFunctionCall)
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001513 && SemaCheckStringLiteral(C->getFalseExpr(), Args, NumArgs, HasVAListArg,
1514 format_idx, firstDataArg, Type,
1515 inFunctionCall);
Ted Kremenekd30ef872009-01-12 23:09:09 +00001516 }
1517
1518 case Stmt::ImplicitCastExprClass: {
Ted Kremenek4fe64412010-09-09 03:51:39 +00001519 E = cast<ImplicitCastExpr>(E)->getSubExpr();
1520 goto tryAgain;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001521 }
1522
John McCall56ca35d2011-02-17 10:25:35 +00001523 case Stmt::OpaqueValueExprClass:
1524 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
1525 E = src;
1526 goto tryAgain;
1527 }
1528 return false;
1529
Ted Kremenekb43e8ad2011-02-24 23:03:04 +00001530 case Stmt::PredefinedExprClass:
1531 // While __func__, etc., are technically not string literals, they
1532 // cannot contain format specifiers and thus are not a security
1533 // liability.
1534 return true;
1535
Ted Kremenek082d9362009-03-20 21:35:28 +00001536 case Stmt::DeclRefExprClass: {
1537 const DeclRefExpr *DR = cast<DeclRefExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Ted Kremenek082d9362009-03-20 21:35:28 +00001539 // As an exception, do not flag errors for variables binding to
1540 // const string literals.
1541 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1542 bool isConstant = false;
1543 QualType T = DR->getType();
Ted Kremenekd30ef872009-01-12 23:09:09 +00001544
Ted Kremenek082d9362009-03-20 21:35:28 +00001545 if (const ArrayType *AT = Context.getAsArrayType(T)) {
1546 isConstant = AT->getElementType().isConstant(Context);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001547 } else if (const PointerType *PT = T->getAs<PointerType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001548 isConstant = T.isConstant(Context) &&
Ted Kremenek082d9362009-03-20 21:35:28 +00001549 PT->getPointeeType().isConstant(Context);
Jean-Daniel Dupase98e5b52012-01-25 10:35:33 +00001550 } else if (T->isObjCObjectPointerType()) {
1551 // In ObjC, there is usually no "const ObjectPointer" type,
1552 // so don't check if the pointee type is constant.
1553 isConstant = T.isConstant(Context);
Ted Kremenek082d9362009-03-20 21:35:28 +00001554 }
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Ted Kremenek082d9362009-03-20 21:35:28 +00001556 if (isConstant) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001557 if (const Expr *Init = VD->getAnyInitializer())
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001558 return SemaCheckStringLiteral(Init, Args, NumArgs,
Ted Kremenek826a3452010-07-16 02:11:22 +00001559 HasVAListArg, format_idx, firstDataArg,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001560 Type, /*inFunctionCall*/false);
Ted Kremenek082d9362009-03-20 21:35:28 +00001561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Anders Carlssond966a552009-06-28 19:55:58 +00001563 // For vprintf* functions (i.e., HasVAListArg==true), we add a
1564 // special check to see if the format string is a function parameter
1565 // of the function calling the printf function. If the function
1566 // has an attribute indicating it is a printf-like function, then we
1567 // should suppress warnings concerning non-literals being used in a call
1568 // to a vprintf function. For example:
1569 //
1570 // void
1571 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
1572 // va_list ap;
1573 // va_start(ap, fmt);
1574 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt".
1575 // ...
1576 //
Jean-Daniel Dupasf57c4132012-02-21 20:00:53 +00001577 if (HasVAListArg) {
1578 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) {
1579 if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) {
1580 int PVIndex = PV->getFunctionScopeIndex() + 1;
1581 for (specific_attr_iterator<FormatAttr>
1582 i = ND->specific_attr_begin<FormatAttr>(),
1583 e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) {
1584 FormatAttr *PVFormat = *i;
1585 // adjust for implicit parameter
1586 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1587 if (MD->isInstance())
1588 ++PVIndex;
1589 // We also check if the formats are compatible.
1590 // We can't pass a 'scanf' string to a 'printf' function.
1591 if (PVIndex == PVFormat->getFormatIdx() &&
1592 Type == GetFormatStringType(PVFormat))
1593 return true;
1594 }
1595 }
1596 }
1597 }
Ted Kremenek082d9362009-03-20 21:35:28 +00001598 }
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Ted Kremenek082d9362009-03-20 21:35:28 +00001600 return false;
1601 }
Ted Kremenekd30ef872009-01-12 23:09:09 +00001602
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +00001603 case Stmt::CallExprClass:
1604 case Stmt::CXXMemberCallExprClass: {
Anders Carlsson8f031b32009-06-27 04:05:33 +00001605 const CallExpr *CE = cast<CallExpr>(E);
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +00001606 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
1607 if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) {
1608 unsigned ArgIndex = FA->getFormatIdx();
1609 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND))
1610 if (MD->isInstance())
1611 --ArgIndex;
1612 const Expr *Arg = CE->getArg(ArgIndex - 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +00001614 return SemaCheckStringLiteral(Arg, Args, NumArgs, HasVAListArg,
1615 format_idx, firstDataArg, Type,
1616 inFunctionCall);
Anders Carlsson8f031b32009-06-27 04:05:33 +00001617 }
1618 }
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Anders Carlsson8f031b32009-06-27 04:05:33 +00001620 return false;
1621 }
Ted Kremenek082d9362009-03-20 21:35:28 +00001622 case Stmt::ObjCStringLiteralClass:
1623 case Stmt::StringLiteralClass: {
1624 const StringLiteral *StrE = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek082d9362009-03-20 21:35:28 +00001626 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
Ted Kremenekd30ef872009-01-12 23:09:09 +00001627 StrE = ObjCFExpr->getString();
1628 else
Ted Kremenek082d9362009-03-20 21:35:28 +00001629 StrE = cast<StringLiteral>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Ted Kremenekd30ef872009-01-12 23:09:09 +00001631 if (StrE) {
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001632 CheckFormatString(StrE, E, Args, NumArgs, HasVAListArg, format_idx,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001633 firstDataArg, Type, inFunctionCall);
Ted Kremenekd30ef872009-01-12 23:09:09 +00001634 return true;
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Ted Kremenekd30ef872009-01-12 23:09:09 +00001637 return false;
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Ted Kremenek082d9362009-03-20 21:35:28 +00001640 default:
1641 return false;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001642 }
1643}
1644
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00001645void
Mike Stump1eb44332009-09-09 15:08:12 +00001646Sema::CheckNonNullArguments(const NonNullAttr *NonNull,
Nick Lewycky909a70d2011-03-25 01:44:32 +00001647 const Expr * const *ExprArgs,
1648 SourceLocation CallSiteLoc) {
Sean Huntcf807c42010-08-18 23:23:40 +00001649 for (NonNullAttr::args_iterator i = NonNull->args_begin(),
1650 e = NonNull->args_end();
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00001651 i != e; ++i) {
Nick Lewycky909a70d2011-03-25 01:44:32 +00001652 const Expr *ArgExpr = ExprArgs[*i];
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001653 if (ArgExpr->isNullPointerConstant(Context,
Douglas Gregorce940492009-09-25 04:25:58 +00001654 Expr::NPC_ValueDependentIsNotNull))
Nick Lewycky909a70d2011-03-25 01:44:32 +00001655 Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00001656 }
1657}
Ted Kremenekd30ef872009-01-12 23:09:09 +00001658
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001659Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) {
1660 return llvm::StringSwitch<FormatStringType>(Format->getType())
1661 .Case("scanf", FST_Scanf)
1662 .Cases("printf", "printf0", FST_Printf)
1663 .Cases("NSString", "CFString", FST_NSString)
1664 .Case("strftime", FST_Strftime)
1665 .Case("strfmon", FST_Strfmon)
1666 .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf)
1667 .Default(FST_Unknown);
1668}
1669
Ted Kremenek826a3452010-07-16 02:11:22 +00001670/// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar
1671/// functions) for correct use of format strings.
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001672void Sema::CheckFormatArguments(const FormatAttr *Format, CallExpr *TheCall) {
1673 bool IsCXXMember = false;
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001674 // The way the format attribute works in GCC, the implicit this argument
1675 // of member functions is counted. However, it doesn't appear in our own
1676 // lists, so decrement format_idx in that case.
Jean-Daniel Dupas52aabaf2012-02-07 19:01:42 +00001677 IsCXXMember = isa<CXXMemberCallExpr>(TheCall);
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001678 CheckFormatArguments(Format, TheCall->getArgs(), TheCall->getNumArgs(),
1679 IsCXXMember, TheCall->getRParenLoc(),
1680 TheCall->getCallee()->getSourceRange());
1681}
1682
1683void Sema::CheckFormatArguments(const FormatAttr *Format, Expr **Args,
1684 unsigned NumArgs, bool IsCXXMember,
1685 SourceLocation Loc, SourceRange Range) {
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001686 bool HasVAListArg = Format->getFirstArg() == 0;
1687 unsigned format_idx = Format->getFormatIdx() - 1;
1688 unsigned firstDataArg = HasVAListArg ? 0 : Format->getFirstArg() - 1;
1689 if (IsCXXMember) {
1690 if (format_idx == 0)
1691 return;
1692 --format_idx;
1693 if(firstDataArg != 0)
1694 --firstDataArg;
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001695 }
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001696 CheckFormatArguments(Args, NumArgs, HasVAListArg, format_idx,
1697 firstDataArg, GetFormatStringType(Format), Loc, Range);
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001698}
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001699
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001700void Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs,
1701 bool HasVAListArg, unsigned format_idx,
1702 unsigned firstDataArg, FormatStringType Type,
1703 SourceLocation Loc, SourceRange Range) {
Ted Kremenek826a3452010-07-16 02:11:22 +00001704 // CHECK: printf/scanf-like function is called with no format string.
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001705 if (format_idx >= NumArgs) {
1706 Diag(Loc, diag::warn_missing_format_string) << Range;
Ted Kremenek71895b92007-08-14 17:39:48 +00001707 return;
1708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001710 const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Chris Lattner59907c42007-08-10 20:18:51 +00001712 // CHECK: format string is not a string literal.
Mike Stump1eb44332009-09-09 15:08:12 +00001713 //
Ted Kremenek71895b92007-08-14 17:39:48 +00001714 // Dynamically generated format strings are difficult to
1715 // automatically vet at compile time. Requiring that format strings
1716 // are string literals: (1) permits the checking of format strings by
1717 // the compiler and thereby (2) can practically remove the source of
1718 // many format string exploits.
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001719
Mike Stump1eb44332009-09-09 15:08:12 +00001720 // Format string can be either ObjC string (e.g. @"%d") or
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001721 // C string (e.g. "%d")
Mike Stump1eb44332009-09-09 15:08:12 +00001722 // ObjC string uses the same format specifiers as C string, so we can use
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001723 // the same format string checking logic for both ObjC and C strings.
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001724 if (SemaCheckStringLiteral(OrigFormatExpr, Args, NumArgs, HasVAListArg,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00001725 format_idx, firstDataArg, Type))
Chris Lattner1cd3e1f2009-04-29 04:49:34 +00001726 return; // Literal format string found, check done!
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001727
Jean-Daniel Dupas2837a2f2012-02-07 23:10:53 +00001728 // Strftime is particular as it always uses a single 'time' argument,
1729 // so it is safe to pass a non-literal string.
1730 if (Type == FST_Strftime)
1731 return;
1732
Jean-Daniel Dupasce3aa392012-01-30 19:46:17 +00001733 // Do not emit diag when the string param is a macro expansion and the
1734 // format is either NSString or CFString. This is a hack to prevent
1735 // diag when using the NSLocalizedString and CFCopyLocalizedString macros
1736 // which are usually used in place of NS and CF string literals.
1737 if (Type == FST_NSString && Args[format_idx]->getLocStart().isMacroID())
1738 return;
1739
Chris Lattner655f1412009-04-29 04:59:47 +00001740 // If there are no arguments specified, warn with -Wformat-security, otherwise
1741 // warn only with -Wformat-nonliteral.
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001742 if (NumArgs == format_idx+1)
1743 Diag(Args[format_idx]->getLocStart(),
Ted Kremenek826a3452010-07-16 02:11:22 +00001744 diag::warn_format_nonliteral_noargs)
Chris Lattner655f1412009-04-29 04:59:47 +00001745 << OrigFormatExpr->getSourceRange();
1746 else
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001747 Diag(Args[format_idx]->getLocStart(),
Ted Kremenek826a3452010-07-16 02:11:22 +00001748 diag::warn_format_nonliteral)
Chris Lattner655f1412009-04-29 04:59:47 +00001749 << OrigFormatExpr->getSourceRange();
Ted Kremenekd30ef872009-01-12 23:09:09 +00001750}
Ted Kremenek71895b92007-08-14 17:39:48 +00001751
Ted Kremeneke0e53132010-01-28 23:39:18 +00001752namespace {
Ted Kremenek826a3452010-07-16 02:11:22 +00001753class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
1754protected:
Ted Kremeneke0e53132010-01-28 23:39:18 +00001755 Sema &S;
1756 const StringLiteral *FExpr;
1757 const Expr *OrigFormatExpr;
Ted Kremenek6ee76532010-03-25 03:59:12 +00001758 const unsigned FirstDataArg;
Ted Kremeneke0e53132010-01-28 23:39:18 +00001759 const unsigned NumDataArgs;
1760 const bool IsObjCLiteral;
1761 const char *Beg; // Start of format string.
Ted Kremenek0d277352010-01-29 01:06:55 +00001762 const bool HasVAListArg;
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001763 const Expr * const *Args;
1764 const unsigned NumArgs;
Ted Kremenek0d277352010-01-29 01:06:55 +00001765 unsigned FormatIdx;
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001766 llvm::BitVector CoveredArgs;
Ted Kremenekefaff192010-02-27 01:41:03 +00001767 bool usesPositionalArgs;
1768 bool atFirstArg;
Richard Trieu55733de2011-10-28 00:41:25 +00001769 bool inFunctionCall;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001770public:
Ted Kremenek826a3452010-07-16 02:11:22 +00001771 CheckFormatHandler(Sema &s, const StringLiteral *fexpr,
Ted Kremenek6ee76532010-03-25 03:59:12 +00001772 const Expr *origFormatExpr, unsigned firstDataArg,
Ted Kremeneke0e53132010-01-28 23:39:18 +00001773 unsigned numDataArgs, bool isObjCLiteral,
Ted Kremenek0d277352010-01-29 01:06:55 +00001774 const char *beg, bool hasVAListArg,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001775 Expr **args, unsigned numArgs,
1776 unsigned formatIdx, bool inFunctionCall)
Ted Kremeneke0e53132010-01-28 23:39:18 +00001777 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr),
Ted Kremenek6ee76532010-03-25 03:59:12 +00001778 FirstDataArg(firstDataArg),
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001779 NumDataArgs(numDataArgs),
Ted Kremenek0d277352010-01-29 01:06:55 +00001780 IsObjCLiteral(isObjCLiteral), Beg(beg),
1781 HasVAListArg(hasVAListArg),
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001782 Args(args), NumArgs(numArgs), FormatIdx(formatIdx),
Richard Trieu55733de2011-10-28 00:41:25 +00001783 usesPositionalArgs(false), atFirstArg(true),
1784 inFunctionCall(inFunctionCall) {
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001785 CoveredArgs.resize(numDataArgs);
1786 CoveredArgs.reset();
1787 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001788
Ted Kremenek07d161f2010-01-29 01:50:07 +00001789 void DoneProcessing();
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001790
Ted Kremenek826a3452010-07-16 02:11:22 +00001791 void HandleIncompleteSpecifier(const char *startSpecifier,
1792 unsigned specifierLen);
Hans Wennborg76517422012-02-22 10:17:01 +00001793
1794 void HandleNonStandardLengthModifier(
1795 const analyze_format_string::LengthModifier &LM,
1796 const char *startSpecifier, unsigned specifierLen);
1797
1798 void HandleNonStandardConversionSpecifier(
1799 const analyze_format_string::ConversionSpecifier &CS,
1800 const char *startSpecifier, unsigned specifierLen);
1801
1802 void HandleNonStandardConversionSpecification(
1803 const analyze_format_string::LengthModifier &LM,
1804 const analyze_format_string::ConversionSpecifier &CS,
1805 const char *startSpecifier, unsigned specifierLen);
1806
Hans Wennborgf8562642012-03-09 10:10:54 +00001807 virtual void HandlePosition(const char *startPos, unsigned posLen);
1808
Ted Kremenekefaff192010-02-27 01:41:03 +00001809 virtual void HandleInvalidPosition(const char *startSpecifier,
1810 unsigned specifierLen,
Ted Kremenek826a3452010-07-16 02:11:22 +00001811 analyze_format_string::PositionContext p);
Ted Kremenekefaff192010-02-27 01:41:03 +00001812
1813 virtual void HandleZeroPosition(const char *startPos, unsigned posLen);
1814
Ted Kremeneke0e53132010-01-28 23:39:18 +00001815 void HandleNullChar(const char *nullCharacter);
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001816
Richard Trieu55733de2011-10-28 00:41:25 +00001817 template <typename Range>
1818 static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall,
1819 const Expr *ArgumentExpr,
1820 PartialDiagnostic PDiag,
1821 SourceLocation StringLoc,
1822 bool IsStringLocation, Range StringRange,
1823 FixItHint Fixit = FixItHint());
1824
Ted Kremenek826a3452010-07-16 02:11:22 +00001825protected:
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001826 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
1827 const char *startSpec,
1828 unsigned specifierLen,
1829 const char *csStart, unsigned csLen);
Richard Trieu55733de2011-10-28 00:41:25 +00001830
1831 void HandlePositionalNonpositionalArgs(SourceLocation Loc,
1832 const char *startSpec,
1833 unsigned specifierLen);
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001834
Ted Kremenekf88c8e02010-01-29 20:55:36 +00001835 SourceRange getFormatStringRange();
Ted Kremenek826a3452010-07-16 02:11:22 +00001836 CharSourceRange getSpecifierRange(const char *startSpecifier,
1837 unsigned specifierLen);
Ted Kremeneke0e53132010-01-28 23:39:18 +00001838 SourceLocation getLocationOfByte(const char *x);
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001839
Ted Kremenek0d277352010-01-29 01:06:55 +00001840 const Expr *getDataArg(unsigned i) const;
Ted Kremenek666a1972010-07-26 19:45:42 +00001841
1842 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
1843 const analyze_format_string::ConversionSpecifier &CS,
1844 const char *startSpecifier, unsigned specifierLen,
1845 unsigned argIndex);
Richard Trieu55733de2011-10-28 00:41:25 +00001846
1847 template <typename Range>
1848 void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc,
1849 bool IsStringLocation, Range StringRange,
1850 FixItHint Fixit = FixItHint());
1851
1852 void CheckPositionalAndNonpositionalArgs(
1853 const analyze_format_string::FormatSpecifier *FS);
Ted Kremeneke0e53132010-01-28 23:39:18 +00001854};
1855}
1856
Ted Kremenek826a3452010-07-16 02:11:22 +00001857SourceRange CheckFormatHandler::getFormatStringRange() {
Ted Kremeneke0e53132010-01-28 23:39:18 +00001858 return OrigFormatExpr->getSourceRange();
1859}
1860
Ted Kremenek826a3452010-07-16 02:11:22 +00001861CharSourceRange CheckFormatHandler::
1862getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
Tom Care45f9b7e2010-06-21 21:21:01 +00001863 SourceLocation Start = getLocationOfByte(startSpecifier);
1864 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1);
1865
1866 // Advance the end SourceLocation by one due to half-open ranges.
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00001867 End = End.getLocWithOffset(1);
Tom Care45f9b7e2010-06-21 21:21:01 +00001868
1869 return CharSourceRange::getCharRange(Start, End);
Ted Kremenekf88c8e02010-01-29 20:55:36 +00001870}
1871
Ted Kremenek826a3452010-07-16 02:11:22 +00001872SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001873 return S.getLocationOfStringLiteralByte(FExpr, x - Beg);
Ted Kremeneke0e53132010-01-28 23:39:18 +00001874}
1875
Ted Kremenek826a3452010-07-16 02:11:22 +00001876void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
1877 unsigned specifierLen){
Richard Trieu55733de2011-10-28 00:41:25 +00001878 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier),
1879 getLocationOfByte(startSpecifier),
1880 /*IsStringLocation*/true,
1881 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek808015a2010-01-29 03:16:21 +00001882}
1883
Hans Wennborg76517422012-02-22 10:17:01 +00001884void CheckFormatHandler::HandleNonStandardLengthModifier(
1885 const analyze_format_string::LengthModifier &LM,
1886 const char *startSpecifier, unsigned specifierLen) {
1887 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << LM.toString()
Hans Wennborgf8562642012-03-09 10:10:54 +00001888 << 0,
Hans Wennborg76517422012-02-22 10:17:01 +00001889 getLocationOfByte(LM.getStart()),
1890 /*IsStringLocation*/true,
1891 getSpecifierRange(startSpecifier, specifierLen));
1892}
1893
1894void CheckFormatHandler::HandleNonStandardConversionSpecifier(
1895 const analyze_format_string::ConversionSpecifier &CS,
1896 const char *startSpecifier, unsigned specifierLen) {
1897 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) << CS.toString()
Hans Wennborgf8562642012-03-09 10:10:54 +00001898 << 1,
Hans Wennborg76517422012-02-22 10:17:01 +00001899 getLocationOfByte(CS.getStart()),
1900 /*IsStringLocation*/true,
1901 getSpecifierRange(startSpecifier, specifierLen));
1902}
1903
1904void CheckFormatHandler::HandleNonStandardConversionSpecification(
1905 const analyze_format_string::LengthModifier &LM,
1906 const analyze_format_string::ConversionSpecifier &CS,
1907 const char *startSpecifier, unsigned specifierLen) {
1908 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_conversion_spec)
1909 << LM.toString() << CS.toString(),
1910 getLocationOfByte(LM.getStart()),
1911 /*IsStringLocation*/true,
1912 getSpecifierRange(startSpecifier, specifierLen));
1913}
1914
Hans Wennborgf8562642012-03-09 10:10:54 +00001915void CheckFormatHandler::HandlePosition(const char *startPos,
1916 unsigned posLen) {
1917 EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg),
1918 getLocationOfByte(startPos),
1919 /*IsStringLocation*/true,
1920 getSpecifierRange(startPos, posLen));
1921}
1922
Ted Kremenekefaff192010-02-27 01:41:03 +00001923void
Ted Kremenek826a3452010-07-16 02:11:22 +00001924CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
1925 analyze_format_string::PositionContext p) {
Richard Trieu55733de2011-10-28 00:41:25 +00001926 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier)
1927 << (unsigned) p,
1928 getLocationOfByte(startPos), /*IsStringLocation*/true,
1929 getSpecifierRange(startPos, posLen));
Ted Kremenekefaff192010-02-27 01:41:03 +00001930}
1931
Ted Kremenek826a3452010-07-16 02:11:22 +00001932void CheckFormatHandler::HandleZeroPosition(const char *startPos,
Ted Kremenekefaff192010-02-27 01:41:03 +00001933 unsigned posLen) {
Richard Trieu55733de2011-10-28 00:41:25 +00001934 EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier),
1935 getLocationOfByte(startPos),
1936 /*IsStringLocation*/true,
1937 getSpecifierRange(startPos, posLen));
Ted Kremenekefaff192010-02-27 01:41:03 +00001938}
1939
Ted Kremenek826a3452010-07-16 02:11:22 +00001940void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
Ted Kremenek0c069442011-03-15 21:18:48 +00001941 if (!IsObjCLiteral) {
1942 // The presence of a null character is likely an error.
Richard Trieu55733de2011-10-28 00:41:25 +00001943 EmitFormatDiagnostic(
1944 S.PDiag(diag::warn_printf_format_string_contains_null_char),
1945 getLocationOfByte(nullCharacter), /*IsStringLocation*/true,
1946 getFormatStringRange());
Ted Kremenek0c069442011-03-15 21:18:48 +00001947 }
Ted Kremenek826a3452010-07-16 02:11:22 +00001948}
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001949
Ted Kremenek826a3452010-07-16 02:11:22 +00001950const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00001951 return Args[FirstDataArg + i];
Ted Kremenek826a3452010-07-16 02:11:22 +00001952}
1953
1954void CheckFormatHandler::DoneProcessing() {
1955 // Does the number of data arguments exceed the number of
1956 // format conversions in the format string?
1957 if (!HasVAListArg) {
1958 // Find any arguments that weren't covered.
1959 CoveredArgs.flip();
1960 signed notCoveredArg = CoveredArgs.find_first();
1961 if (notCoveredArg >= 0) {
1962 assert((unsigned)notCoveredArg < NumDataArgs);
Richard Trieu55733de2011-10-28 00:41:25 +00001963 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used),
1964 getDataArg((unsigned) notCoveredArg)->getLocStart(),
1965 /*IsStringLocation*/false, getFormatStringRange());
Ted Kremenek826a3452010-07-16 02:11:22 +00001966 }
1967 }
1968}
1969
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001970bool
1971CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
1972 SourceLocation Loc,
1973 const char *startSpec,
1974 unsigned specifierLen,
1975 const char *csStart,
1976 unsigned csLen) {
1977
1978 bool keepGoing = true;
1979 if (argIndex < NumDataArgs) {
1980 // Consider the argument coverered, even though the specifier doesn't
1981 // make sense.
1982 CoveredArgs.set(argIndex);
1983 }
1984 else {
1985 // If argIndex exceeds the number of data arguments we
1986 // don't issue a warning because that is just a cascade of warnings (and
1987 // they may have intended '%%' anyway). We don't want to continue processing
1988 // the format string after this point, however, as we will like just get
1989 // gibberish when trying to match arguments.
1990 keepGoing = false;
1991 }
1992
Richard Trieu55733de2011-10-28 00:41:25 +00001993 EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion)
1994 << StringRef(csStart, csLen),
1995 Loc, /*IsStringLocation*/true,
1996 getSpecifierRange(startSpec, specifierLen));
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001997
1998 return keepGoing;
1999}
2000
Richard Trieu55733de2011-10-28 00:41:25 +00002001void
2002CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc,
2003 const char *startSpec,
2004 unsigned specifierLen) {
2005 EmitFormatDiagnostic(
2006 S.PDiag(diag::warn_format_mix_positional_nonpositional_args),
2007 Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen));
2008}
2009
Ted Kremenek666a1972010-07-26 19:45:42 +00002010bool
2011CheckFormatHandler::CheckNumArgs(
2012 const analyze_format_string::FormatSpecifier &FS,
2013 const analyze_format_string::ConversionSpecifier &CS,
2014 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
2015
2016 if (argIndex >= NumDataArgs) {
Richard Trieu55733de2011-10-28 00:41:25 +00002017 PartialDiagnostic PDiag = FS.usesPositionalArg()
2018 ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args)
2019 << (argIndex+1) << NumDataArgs)
2020 : S.PDiag(diag::warn_printf_insufficient_data_args);
2021 EmitFormatDiagnostic(
2022 PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true,
2023 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek666a1972010-07-26 19:45:42 +00002024 return false;
2025 }
2026 return true;
2027}
2028
Richard Trieu55733de2011-10-28 00:41:25 +00002029template<typename Range>
2030void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag,
2031 SourceLocation Loc,
2032 bool IsStringLocation,
2033 Range StringRange,
2034 FixItHint FixIt) {
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002035 EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag,
Richard Trieu55733de2011-10-28 00:41:25 +00002036 Loc, IsStringLocation, StringRange, FixIt);
2037}
2038
2039/// \brief If the format string is not within the funcion call, emit a note
2040/// so that the function call and string are in diagnostic messages.
2041///
2042/// \param inFunctionCall if true, the format string is within the function
2043/// call and only one diagnostic message will be produced. Otherwise, an
2044/// extra note will be emitted pointing to location of the format string.
2045///
2046/// \param ArgumentExpr the expression that is passed as the format string
2047/// argument in the function call. Used for getting locations when two
2048/// diagnostics are emitted.
2049///
2050/// \param PDiag the callee should already have provided any strings for the
2051/// diagnostic message. This function only adds locations and fixits
2052/// to diagnostics.
2053///
2054/// \param Loc primary location for diagnostic. If two diagnostics are
2055/// required, one will be at Loc and a new SourceLocation will be created for
2056/// the other one.
2057///
2058/// \param IsStringLocation if true, Loc points to the format string should be
2059/// used for the note. Otherwise, Loc points to the argument list and will
2060/// be used with PDiag.
2061///
2062/// \param StringRange some or all of the string to highlight. This is
2063/// templated so it can accept either a CharSourceRange or a SourceRange.
2064///
2065/// \param Fixit optional fix it hint for the format string.
2066template<typename Range>
2067void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall,
2068 const Expr *ArgumentExpr,
2069 PartialDiagnostic PDiag,
2070 SourceLocation Loc,
2071 bool IsStringLocation,
2072 Range StringRange,
2073 FixItHint FixIt) {
2074 if (InFunctionCall)
2075 S.Diag(Loc, PDiag) << StringRange << FixIt;
2076 else {
2077 S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag)
2078 << ArgumentExpr->getSourceRange();
2079 S.Diag(IsStringLocation ? Loc : StringRange.getBegin(),
2080 diag::note_format_string_defined)
2081 << StringRange << FixIt;
2082 }
2083}
2084
Ted Kremenek826a3452010-07-16 02:11:22 +00002085//===--- CHECK: Printf format string checking ------------------------------===//
2086
2087namespace {
2088class CheckPrintfHandler : public CheckFormatHandler {
2089public:
2090 CheckPrintfHandler(Sema &s, const StringLiteral *fexpr,
2091 const Expr *origFormatExpr, unsigned firstDataArg,
2092 unsigned numDataArgs, bool isObjCLiteral,
2093 const char *beg, bool hasVAListArg,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002094 Expr **Args, unsigned NumArgs,
2095 unsigned formatIdx, bool inFunctionCall)
Ted Kremenek826a3452010-07-16 02:11:22 +00002096 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
2097 numDataArgs, isObjCLiteral, beg, hasVAListArg,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002098 Args, NumArgs, formatIdx, inFunctionCall) {}
Ted Kremenek826a3452010-07-16 02:11:22 +00002099
2100
2101 bool HandleInvalidPrintfConversionSpecifier(
2102 const analyze_printf::PrintfSpecifier &FS,
2103 const char *startSpecifier,
2104 unsigned specifierLen);
2105
2106 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
2107 const char *startSpecifier,
2108 unsigned specifierLen);
2109
2110 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
2111 const char *startSpecifier, unsigned specifierLen);
2112 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
2113 const analyze_printf::OptionalAmount &Amt,
2114 unsigned type,
2115 const char *startSpecifier, unsigned specifierLen);
2116 void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
2117 const analyze_printf::OptionalFlag &flag,
2118 const char *startSpecifier, unsigned specifierLen);
2119 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
2120 const analyze_printf::OptionalFlag &ignoredFlag,
2121 const analyze_printf::OptionalFlag &flag,
2122 const char *startSpecifier, unsigned specifierLen);
2123};
2124}
2125
2126bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
2127 const analyze_printf::PrintfSpecifier &FS,
2128 const char *startSpecifier,
2129 unsigned specifierLen) {
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002130 const analyze_printf::PrintfConversionSpecifier &CS =
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002131 FS.getConversionSpecifier();
Ted Kremenek826a3452010-07-16 02:11:22 +00002132
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002133 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
2134 getLocationOfByte(CS.getStart()),
2135 startSpecifier, specifierLen,
2136 CS.getStart(), CS.getLength());
Ted Kremenek26ac2e02010-01-29 02:40:24 +00002137}
2138
Ted Kremenek826a3452010-07-16 02:11:22 +00002139bool CheckPrintfHandler::HandleAmount(
2140 const analyze_format_string::OptionalAmount &Amt,
2141 unsigned k, const char *startSpecifier,
2142 unsigned specifierLen) {
Ted Kremenek0d277352010-01-29 01:06:55 +00002143
2144 if (Amt.hasDataArgument()) {
Ted Kremenek0d277352010-01-29 01:06:55 +00002145 if (!HasVAListArg) {
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002146 unsigned argIndex = Amt.getArgIndex();
2147 if (argIndex >= NumDataArgs) {
Richard Trieu55733de2011-10-28 00:41:25 +00002148 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg)
2149 << k,
2150 getLocationOfByte(Amt.getStart()),
2151 /*IsStringLocation*/true,
2152 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek0d277352010-01-29 01:06:55 +00002153 // Don't do any more checking. We will just emit
2154 // spurious errors.
2155 return false;
2156 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002157
Ted Kremenek0d277352010-01-29 01:06:55 +00002158 // Type check the data argument. It should be an 'int'.
Ted Kremenek31f8e322010-01-29 23:32:22 +00002159 // Although not in conformance with C99, we also allow the argument to be
2160 // an 'unsigned int' as that is a reasonably safe case. GCC also
2161 // doesn't emit a warning for that case.
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002162 CoveredArgs.set(argIndex);
2163 const Expr *Arg = getDataArg(argIndex);
Ted Kremenek0d277352010-01-29 01:06:55 +00002164 QualType T = Arg->getType();
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002165
2166 const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context);
2167 assert(ATR.isValid());
2168
2169 if (!ATR.matchesType(S.Context, T)) {
Richard Trieu55733de2011-10-28 00:41:25 +00002170 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
Hans Wennborga792aff2011-12-07 10:33:11 +00002171 << k << ATR.getRepresentativeTypeName(S.Context)
Richard Trieu55733de2011-10-28 00:41:25 +00002172 << T << Arg->getSourceRange(),
2173 getLocationOfByte(Amt.getStart()),
2174 /*IsStringLocation*/true,
2175 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremenek0d277352010-01-29 01:06:55 +00002176 // Don't do any more checking. We will just emit
2177 // spurious errors.
2178 return false;
2179 }
2180 }
2181 }
2182 return true;
2183}
Ted Kremenek0d277352010-01-29 01:06:55 +00002184
Tom Caree4ee9662010-06-17 19:00:27 +00002185void CheckPrintfHandler::HandleInvalidAmount(
Ted Kremenek826a3452010-07-16 02:11:22 +00002186 const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00002187 const analyze_printf::OptionalAmount &Amt,
2188 unsigned type,
2189 const char *startSpecifier,
2190 unsigned specifierLen) {
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002191 const analyze_printf::PrintfConversionSpecifier &CS =
2192 FS.getConversionSpecifier();
Tom Caree4ee9662010-06-17 19:00:27 +00002193
Richard Trieu55733de2011-10-28 00:41:25 +00002194 FixItHint fixit =
2195 Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant
2196 ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
2197 Amt.getConstantLength()))
2198 : FixItHint();
2199
2200 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount)
2201 << type << CS.toString(),
2202 getLocationOfByte(Amt.getStart()),
2203 /*IsStringLocation*/true,
2204 getSpecifierRange(startSpecifier, specifierLen),
2205 fixit);
Tom Caree4ee9662010-06-17 19:00:27 +00002206}
2207
Ted Kremenek826a3452010-07-16 02:11:22 +00002208void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00002209 const analyze_printf::OptionalFlag &flag,
2210 const char *startSpecifier,
2211 unsigned specifierLen) {
2212 // Warn about pointless flag with a fixit removal.
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002213 const analyze_printf::PrintfConversionSpecifier &CS =
2214 FS.getConversionSpecifier();
Richard Trieu55733de2011-10-28 00:41:25 +00002215 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag)
2216 << flag.toString() << CS.toString(),
2217 getLocationOfByte(flag.getPosition()),
2218 /*IsStringLocation*/true,
2219 getSpecifierRange(startSpecifier, specifierLen),
2220 FixItHint::CreateRemoval(
2221 getSpecifierRange(flag.getPosition(), 1)));
Tom Caree4ee9662010-06-17 19:00:27 +00002222}
2223
2224void CheckPrintfHandler::HandleIgnoredFlag(
Ted Kremenek826a3452010-07-16 02:11:22 +00002225 const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00002226 const analyze_printf::OptionalFlag &ignoredFlag,
2227 const analyze_printf::OptionalFlag &flag,
2228 const char *startSpecifier,
2229 unsigned specifierLen) {
2230 // Warn about ignored flag with a fixit removal.
Richard Trieu55733de2011-10-28 00:41:25 +00002231 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag)
2232 << ignoredFlag.toString() << flag.toString(),
2233 getLocationOfByte(ignoredFlag.getPosition()),
2234 /*IsStringLocation*/true,
2235 getSpecifierRange(startSpecifier, specifierLen),
2236 FixItHint::CreateRemoval(
2237 getSpecifierRange(ignoredFlag.getPosition(), 1)));
Tom Caree4ee9662010-06-17 19:00:27 +00002238}
2239
Ted Kremeneke0e53132010-01-28 23:39:18 +00002240bool
Ted Kremenek826a3452010-07-16 02:11:22 +00002241CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
Ted Kremenek5c41ee82010-02-11 09:27:41 +00002242 &FS,
Ted Kremeneke0e53132010-01-28 23:39:18 +00002243 const char *startSpecifier,
2244 unsigned specifierLen) {
2245
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002246 using namespace analyze_format_string;
Ted Kremenekefaff192010-02-27 01:41:03 +00002247 using namespace analyze_printf;
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002248 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
Ted Kremeneke0e53132010-01-28 23:39:18 +00002249
Ted Kremenekbaa40062010-07-19 22:01:06 +00002250 if (FS.consumesDataArgument()) {
2251 if (atFirstArg) {
2252 atFirstArg = false;
2253 usesPositionalArgs = FS.usesPositionalArg();
2254 }
2255 else if (usesPositionalArgs != FS.usesPositionalArg()) {
Richard Trieu55733de2011-10-28 00:41:25 +00002256 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
2257 startSpecifier, specifierLen);
Ted Kremenekbaa40062010-07-19 22:01:06 +00002258 return false;
2259 }
Ted Kremenek0d277352010-01-29 01:06:55 +00002260 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002261
Ted Kremenekefaff192010-02-27 01:41:03 +00002262 // First check if the field width, precision, and conversion specifier
2263 // have matching data arguments.
2264 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
2265 startSpecifier, specifierLen)) {
2266 return false;
2267 }
2268
2269 if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
2270 startSpecifier, specifierLen)) {
Ted Kremenek0d277352010-01-29 01:06:55 +00002271 return false;
2272 }
2273
Ted Kremenekf88c8e02010-01-29 20:55:36 +00002274 if (!CS.consumesDataArgument()) {
2275 // FIXME: Technically specifying a precision or field width here
2276 // makes no sense. Worth issuing a warning at some point.
Ted Kremenek0e5675d2010-02-10 02:16:30 +00002277 return true;
Ted Kremenekf88c8e02010-01-29 20:55:36 +00002278 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002279
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002280 // Consume the argument.
2281 unsigned argIndex = FS.getArgIndex();
Ted Kremeneke3fc5472010-02-27 08:34:51 +00002282 if (argIndex < NumDataArgs) {
2283 // The check to see if the argIndex is valid will come later.
2284 // We set the bit here because we may exit early from this
2285 // function if we encounter some other error.
2286 CoveredArgs.set(argIndex);
2287 }
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002288
2289 // Check for using an Objective-C specific conversion specifier
2290 // in a non-ObjC literal.
2291 if (!IsObjCLiteral && CS.isObjCArg()) {
Ted Kremenek826a3452010-07-16 02:11:22 +00002292 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
2293 specifierLen);
Ted Kremenek7f70dc82010-02-26 19:18:41 +00002294 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002295
Tom Caree4ee9662010-06-17 19:00:27 +00002296 // Check for invalid use of field width
2297 if (!FS.hasValidFieldWidth()) {
Tom Care45f9b7e2010-06-21 21:21:01 +00002298 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
Tom Caree4ee9662010-06-17 19:00:27 +00002299 startSpecifier, specifierLen);
2300 }
2301
2302 // Check for invalid use of precision
2303 if (!FS.hasValidPrecision()) {
2304 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
2305 startSpecifier, specifierLen);
2306 }
2307
2308 // Check each flag does not conflict with any other component.
Ted Kremenek65197b42011-01-08 05:28:46 +00002309 if (!FS.hasValidThousandsGroupingPrefix())
2310 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00002311 if (!FS.hasValidLeadingZeros())
2312 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
2313 if (!FS.hasValidPlusPrefix())
2314 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
Tom Care45f9b7e2010-06-21 21:21:01 +00002315 if (!FS.hasValidSpacePrefix())
2316 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00002317 if (!FS.hasValidAlternativeForm())
2318 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
2319 if (!FS.hasValidLeftJustified())
2320 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
2321
2322 // Check that flags are not ignored by another flag
Tom Care45f9b7e2010-06-21 21:21:01 +00002323 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
2324 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
2325 startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00002326 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
2327 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
2328 startSpecifier, specifierLen);
2329
2330 // Check the length modifier is valid with the given conversion specifier.
2331 const LengthModifier &LM = FS.getLengthModifier();
2332 if (!FS.hasValidLengthModifier())
Richard Trieu55733de2011-10-28 00:41:25 +00002333 EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length)
2334 << LM.toString() << CS.toString(),
2335 getLocationOfByte(LM.getStart()),
2336 /*IsStringLocation*/true,
2337 getSpecifierRange(startSpecifier, specifierLen),
2338 FixItHint::CreateRemoval(
2339 getSpecifierRange(LM.getStart(),
2340 LM.getLength())));
Hans Wennborg76517422012-02-22 10:17:01 +00002341 if (!FS.hasStandardLengthModifier())
2342 HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen);
David Blaikie4e4d0842012-03-11 07:00:24 +00002343 if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
Hans Wennborg76517422012-02-22 10:17:01 +00002344 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
2345 if (!FS.hasStandardLengthConversionCombination())
2346 HandleNonStandardConversionSpecification(LM, CS, startSpecifier,
2347 specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00002348
2349 // Are we using '%n'?
Ted Kremenek35d353b2010-07-20 20:04:10 +00002350 if (CS.getKind() == ConversionSpecifier::nArg) {
Tom Caree4ee9662010-06-17 19:00:27 +00002351 // Issue a warning about this being a possible security issue.
Richard Trieu55733de2011-10-28 00:41:25 +00002352 EmitFormatDiagnostic(S.PDiag(diag::warn_printf_write_back),
2353 getLocationOfByte(CS.getStart()),
2354 /*IsStringLocation*/true,
2355 getSpecifierRange(startSpecifier, specifierLen));
Ted Kremeneke82d8042010-01-29 01:35:25 +00002356 // Continue checking the other format specifiers.
2357 return true;
2358 }
Ted Kremenek5c41ee82010-02-11 09:27:41 +00002359
Ted Kremenekda51f0d2010-01-29 01:43:31 +00002360 // The remaining checks depend on the data arguments.
2361 if (HasVAListArg)
2362 return true;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002363
Ted Kremenek666a1972010-07-26 19:45:42 +00002364 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
Ted Kremenekda51f0d2010-01-29 01:43:31 +00002365 return false;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002366
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002367 // Now type check the data expression that matches the
2368 // format specifier.
2369 const Expr *Ex = getDataArg(argIndex);
Nico Weber339b9072012-01-31 01:43:25 +00002370 const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context,
2371 IsObjCLiteral);
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002372 if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) {
2373 // Check if we didn't match because of an implicit cast from a 'char'
2374 // or 'short' to an 'int'. This is done because printf is a varargs
2375 // function.
2376 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex))
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +00002377 if (ICE->getType() == S.Context.IntTy) {
2378 // All further checking is done on the subexpression.
2379 Ex = ICE->getSubExpr();
2380 if (ATR.matchesType(S.Context, Ex->getType()))
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002381 return true;
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +00002382 }
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002383
2384 // We may be able to offer a FixItHint if it is a supported type.
2385 PrintfSpecifier fixedFS = FS;
David Blaikie4e4d0842012-03-11 07:00:24 +00002386 bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(),
Hans Wennborgbe6126a2012-02-15 09:59:46 +00002387 S.Context, IsObjCLiteral);
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002388
2389 if (success) {
2390 // Get the fix string from the fixed format specifier
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002391 SmallString<128> buf;
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002392 llvm::raw_svector_ostream os(buf);
2393 fixedFS.toString(os);
2394
Richard Trieu55733de2011-10-28 00:41:25 +00002395 EmitFormatDiagnostic(
2396 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
Hans Wennborga792aff2011-12-07 10:33:11 +00002397 << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
Richard Trieu55733de2011-10-28 00:41:25 +00002398 << Ex->getSourceRange(),
2399 getLocationOfByte(CS.getStart()),
2400 /*IsStringLocation*/true,
2401 getSpecifierRange(startSpecifier, specifierLen),
2402 FixItHint::CreateReplacement(
2403 getSpecifierRange(startSpecifier, specifierLen),
2404 os.str()));
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002405 }
2406 else {
Jean-Daniel Dupas220947b2012-01-31 18:12:08 +00002407 EmitFormatDiagnostic(
2408 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
2409 << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
2410 << getSpecifierRange(startSpecifier, specifierLen)
2411 << Ex->getSourceRange(),
2412 getLocationOfByte(CS.getStart()),
2413 true,
2414 getSpecifierRange(startSpecifier, specifierLen));
Michael J. Spencer96827eb2010-07-27 04:46:02 +00002415 }
2416 }
2417
Ted Kremeneke0e53132010-01-28 23:39:18 +00002418 return true;
2419}
2420
Ted Kremenek826a3452010-07-16 02:11:22 +00002421//===--- CHECK: Scanf format string checking ------------------------------===//
2422
2423namespace {
2424class CheckScanfHandler : public CheckFormatHandler {
2425public:
2426 CheckScanfHandler(Sema &s, const StringLiteral *fexpr,
2427 const Expr *origFormatExpr, unsigned firstDataArg,
2428 unsigned numDataArgs, bool isObjCLiteral,
2429 const char *beg, bool hasVAListArg,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002430 Expr **Args, unsigned NumArgs,
2431 unsigned formatIdx, bool inFunctionCall)
Ted Kremenek826a3452010-07-16 02:11:22 +00002432 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
2433 numDataArgs, isObjCLiteral, beg, hasVAListArg,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002434 Args, NumArgs, formatIdx, inFunctionCall) {}
Ted Kremenek826a3452010-07-16 02:11:22 +00002435
2436 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
2437 const char *startSpecifier,
2438 unsigned specifierLen);
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002439
2440 bool HandleInvalidScanfConversionSpecifier(
2441 const analyze_scanf::ScanfSpecifier &FS,
2442 const char *startSpecifier,
2443 unsigned specifierLen);
Ted Kremenekb7c21012010-07-16 18:28:03 +00002444
2445 void HandleIncompleteScanList(const char *start, const char *end);
Ted Kremenek826a3452010-07-16 02:11:22 +00002446};
Ted Kremenek07d161f2010-01-29 01:50:07 +00002447}
Ted Kremeneke0e53132010-01-28 23:39:18 +00002448
Ted Kremenekb7c21012010-07-16 18:28:03 +00002449void CheckScanfHandler::HandleIncompleteScanList(const char *start,
2450 const char *end) {
Richard Trieu55733de2011-10-28 00:41:25 +00002451 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete),
2452 getLocationOfByte(end), /*IsStringLocation*/true,
2453 getSpecifierRange(start, end - start));
Ted Kremenekb7c21012010-07-16 18:28:03 +00002454}
2455
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002456bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
2457 const analyze_scanf::ScanfSpecifier &FS,
2458 const char *startSpecifier,
2459 unsigned specifierLen) {
2460
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002461 const analyze_scanf::ScanfConversionSpecifier &CS =
Ted Kremenekc09b6a52010-07-19 21:25:57 +00002462 FS.getConversionSpecifier();
2463
2464 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
2465 getLocationOfByte(CS.getStart()),
2466 startSpecifier, specifierLen,
2467 CS.getStart(), CS.getLength());
2468}
2469
Ted Kremenek826a3452010-07-16 02:11:22 +00002470bool CheckScanfHandler::HandleScanfSpecifier(
2471 const analyze_scanf::ScanfSpecifier &FS,
2472 const char *startSpecifier,
2473 unsigned specifierLen) {
2474
2475 using namespace analyze_scanf;
2476 using namespace analyze_format_string;
2477
Ted Kremenek6ecb9502010-07-20 20:04:27 +00002478 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
Ted Kremenek826a3452010-07-16 02:11:22 +00002479
Ted Kremenekbaa40062010-07-19 22:01:06 +00002480 // Handle case where '%' and '*' don't consume an argument. These shouldn't
2481 // be used to decide if we are using positional arguments consistently.
2482 if (FS.consumesDataArgument()) {
2483 if (atFirstArg) {
2484 atFirstArg = false;
2485 usesPositionalArgs = FS.usesPositionalArg();
2486 }
2487 else if (usesPositionalArgs != FS.usesPositionalArg()) {
Richard Trieu55733de2011-10-28 00:41:25 +00002488 HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()),
2489 startSpecifier, specifierLen);
Ted Kremenekbaa40062010-07-19 22:01:06 +00002490 return false;
2491 }
Ted Kremenek826a3452010-07-16 02:11:22 +00002492 }
2493
2494 // Check if the field with is non-zero.
2495 const OptionalAmount &Amt = FS.getFieldWidth();
2496 if (Amt.getHowSpecified() == OptionalAmount::Constant) {
2497 if (Amt.getConstantAmount() == 0) {
2498 const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
2499 Amt.getConstantLength());
Richard Trieu55733de2011-10-28 00:41:25 +00002500 EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width),
2501 getLocationOfByte(Amt.getStart()),
2502 /*IsStringLocation*/true, R,
2503 FixItHint::CreateRemoval(R));
Ted Kremenek826a3452010-07-16 02:11:22 +00002504 }
2505 }
2506
2507 if (!FS.consumesDataArgument()) {
2508 // FIXME: Technically specifying a precision or field width here
2509 // makes no sense. Worth issuing a warning at some point.
2510 return true;
2511 }
2512
2513 // Consume the argument.
2514 unsigned argIndex = FS.getArgIndex();
2515 if (argIndex < NumDataArgs) {
2516 // The check to see if the argIndex is valid will come later.
2517 // We set the bit here because we may exit early from this
2518 // function if we encounter some other error.
2519 CoveredArgs.set(argIndex);
2520 }
2521
Ted Kremenek1e51c202010-07-20 20:04:47 +00002522 // Check the length modifier is valid with the given conversion specifier.
2523 const LengthModifier &LM = FS.getLengthModifier();
2524 if (!FS.hasValidLengthModifier()) {
Jean-Daniel Dupas220947b2012-01-31 18:12:08 +00002525 const CharSourceRange &R = getSpecifierRange(LM.getStart(), LM.getLength());
2526 EmitFormatDiagnostic(S.PDiag(diag::warn_format_nonsensical_length)
2527 << LM.toString() << CS.toString()
2528 << getSpecifierRange(startSpecifier, specifierLen),
2529 getLocationOfByte(LM.getStart()),
2530 /*IsStringLocation*/true, R,
2531 FixItHint::CreateRemoval(R));
Ted Kremenek1e51c202010-07-20 20:04:47 +00002532 }
2533
Hans Wennborg76517422012-02-22 10:17:01 +00002534 if (!FS.hasStandardLengthModifier())
2535 HandleNonStandardLengthModifier(LM, startSpecifier, specifierLen);
David Blaikie4e4d0842012-03-11 07:00:24 +00002536 if (!FS.hasStandardConversionSpecifier(S.getLangOpts()))
Hans Wennborg76517422012-02-22 10:17:01 +00002537 HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen);
2538 if (!FS.hasStandardLengthConversionCombination())
2539 HandleNonStandardConversionSpecification(LM, CS, startSpecifier,
2540 specifierLen);
2541
Ted Kremenek826a3452010-07-16 02:11:22 +00002542 // The remaining checks depend on the data arguments.
2543 if (HasVAListArg)
2544 return true;
2545
Ted Kremenek666a1972010-07-26 19:45:42 +00002546 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
Ted Kremenek826a3452010-07-16 02:11:22 +00002547 return false;
Ted Kremenek826a3452010-07-16 02:11:22 +00002548
Hans Wennborg6fcd9322011-12-10 13:20:11 +00002549 // Check that the argument type matches the format specifier.
2550 const Expr *Ex = getDataArg(argIndex);
2551 const analyze_scanf::ScanfArgTypeResult &ATR = FS.getArgType(S.Context);
2552 if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) {
2553 ScanfSpecifier fixedFS = FS;
David Blaikie4e4d0842012-03-11 07:00:24 +00002554 bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(),
Hans Wennborgbe6126a2012-02-15 09:59:46 +00002555 S.Context);
Hans Wennborg6fcd9322011-12-10 13:20:11 +00002556
2557 if (success) {
2558 // Get the fix string from the fixed format specifier.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002559 SmallString<128> buf;
Hans Wennborg6fcd9322011-12-10 13:20:11 +00002560 llvm::raw_svector_ostream os(buf);
2561 fixedFS.toString(os);
2562
2563 EmitFormatDiagnostic(
2564 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
2565 << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
2566 << Ex->getSourceRange(),
2567 getLocationOfByte(CS.getStart()),
2568 /*IsStringLocation*/true,
2569 getSpecifierRange(startSpecifier, specifierLen),
2570 FixItHint::CreateReplacement(
2571 getSpecifierRange(startSpecifier, specifierLen),
2572 os.str()));
2573 } else {
Jean-Daniel Dupas220947b2012-01-31 18:12:08 +00002574 EmitFormatDiagnostic(
2575 S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
Hans Wennborg6fcd9322011-12-10 13:20:11 +00002576 << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
Jean-Daniel Dupas220947b2012-01-31 18:12:08 +00002577 << Ex->getSourceRange(),
2578 getLocationOfByte(CS.getStart()),
2579 /*IsStringLocation*/true,
2580 getSpecifierRange(startSpecifier, specifierLen));
Hans Wennborg6fcd9322011-12-10 13:20:11 +00002581 }
2582 }
2583
Ted Kremenek826a3452010-07-16 02:11:22 +00002584 return true;
2585}
2586
2587void Sema::CheckFormatString(const StringLiteral *FExpr,
Ted Kremenek0e5675d2010-02-10 02:16:30 +00002588 const Expr *OrigFormatExpr,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002589 Expr **Args, unsigned NumArgs,
2590 bool HasVAListArg, unsigned format_idx,
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00002591 unsigned firstDataArg, FormatStringType Type,
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002592 bool inFunctionCall) {
Ted Kremenek826a3452010-07-16 02:11:22 +00002593
Ted Kremeneke0e53132010-01-28 23:39:18 +00002594 // CHECK: is the format string a wide literal?
Douglas Gregor5cee1192011-07-27 05:40:30 +00002595 if (!FExpr->isAscii()) {
Richard Trieu55733de2011-10-28 00:41:25 +00002596 CheckFormatHandler::EmitFormatDiagnostic(
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002597 *this, inFunctionCall, Args[format_idx],
Richard Trieu55733de2011-10-28 00:41:25 +00002598 PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(),
2599 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
Ted Kremeneke0e53132010-01-28 23:39:18 +00002600 return;
2601 }
Ted Kremenek826a3452010-07-16 02:11:22 +00002602
Ted Kremeneke0e53132010-01-28 23:39:18 +00002603 // Str - The format string. NOTE: this is NOT null-terminated!
Chris Lattner5f9e2722011-07-23 10:55:15 +00002604 StringRef StrRef = FExpr->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +00002605 const char *Str = StrRef.data();
2606 unsigned StrLen = StrRef.size();
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002607 const unsigned numDataArgs = NumArgs - firstDataArg;
Ted Kremenek826a3452010-07-16 02:11:22 +00002608
Ted Kremeneke0e53132010-01-28 23:39:18 +00002609 // CHECK: empty format string?
Ted Kremenek4cd57912011-09-29 05:52:16 +00002610 if (StrLen == 0 && numDataArgs > 0) {
Richard Trieu55733de2011-10-28 00:41:25 +00002611 CheckFormatHandler::EmitFormatDiagnostic(
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002612 *this, inFunctionCall, Args[format_idx],
Richard Trieu55733de2011-10-28 00:41:25 +00002613 PDiag(diag::warn_empty_format_string), FExpr->getLocStart(),
2614 /*IsStringLocation*/true, OrigFormatExpr->getSourceRange());
Ted Kremeneke0e53132010-01-28 23:39:18 +00002615 return;
2616 }
Ted Kremenek826a3452010-07-16 02:11:22 +00002617
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00002618 if (Type == FST_Printf || Type == FST_NSString) {
Ted Kremenek826a3452010-07-16 02:11:22 +00002619 CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
Ted Kremenek4cd57912011-09-29 05:52:16 +00002620 numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr),
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002621 Str, HasVAListArg, Args, NumArgs, format_idx,
Richard Trieu55733de2011-10-28 00:41:25 +00002622 inFunctionCall);
Ted Kremenek826a3452010-07-16 02:11:22 +00002623
Hans Wennborgd02deeb2011-12-15 10:25:47 +00002624 if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen,
David Blaikie4e4d0842012-03-11 07:00:24 +00002625 getLangOpts()))
Ted Kremenek826a3452010-07-16 02:11:22 +00002626 H.DoneProcessing();
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00002627 } else if (Type == FST_Scanf) {
Ted Kremenek826a3452010-07-16 02:11:22 +00002628 CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
Ted Kremenek4cd57912011-09-29 05:52:16 +00002629 numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr),
Jean-Daniel Dupas29c3f812012-01-17 20:03:31 +00002630 Str, HasVAListArg, Args, NumArgs, format_idx,
Richard Trieu55733de2011-10-28 00:41:25 +00002631 inFunctionCall);
Ted Kremenek826a3452010-07-16 02:11:22 +00002632
Hans Wennborgd02deeb2011-12-15 10:25:47 +00002633 if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen,
David Blaikie4e4d0842012-03-11 07:00:24 +00002634 getLangOpts()))
Ted Kremenek826a3452010-07-16 02:11:22 +00002635 H.DoneProcessing();
Jean-Daniel Dupas34269df2012-01-30 08:46:47 +00002636 } // TODO: handle other formats
Ted Kremenekce7024e2010-01-28 01:18:22 +00002637}
2638
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002639//===--- CHECK: Standard memory functions ---------------------------------===//
2640
Douglas Gregor2a053a32011-05-03 20:05:22 +00002641/// \brief Determine whether the given type is a dynamic class type (e.g.,
2642/// whether it has a vtable).
2643static bool isDynamicClassType(QualType T) {
2644 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl())
2645 if (CXXRecordDecl *Definition = Record->getDefinition())
2646 if (Definition->isDynamicClass())
2647 return true;
2648
2649 return false;
2650}
2651
Chandler Carrutha72a12f2011-06-21 23:04:20 +00002652/// \brief If E is a sizeof expression, returns its argument expression,
Chandler Carruth000d4282011-06-16 09:09:40 +00002653/// otherwise returns NULL.
2654static const Expr *getSizeOfExprArg(const Expr* E) {
Nico Webere4a1c642011-06-14 16:14:58 +00002655 if (const UnaryExprOrTypeTraitExpr *SizeOf =
Chandler Carruth000d4282011-06-16 09:09:40 +00002656 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
2657 if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType())
2658 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
Nico Webere4a1c642011-06-14 16:14:58 +00002659
Chandler Carruth000d4282011-06-16 09:09:40 +00002660 return 0;
2661}
2662
Chandler Carrutha72a12f2011-06-21 23:04:20 +00002663/// \brief If E is a sizeof expression, returns its argument type.
Chandler Carruth000d4282011-06-16 09:09:40 +00002664static QualType getSizeOfArgType(const Expr* E) {
2665 if (const UnaryExprOrTypeTraitExpr *SizeOf =
2666 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
2667 if (SizeOf->getKind() == clang::UETT_SizeOf)
2668 return SizeOf->getTypeOfArgument();
2669
2670 return QualType();
Nico Webere4a1c642011-06-14 16:14:58 +00002671}
2672
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002673/// \brief Check for dangerous or invalid arguments to memset().
2674///
Chandler Carruth929f0132011-06-03 06:23:57 +00002675/// This issues warnings on known problematic, dangerous or unspecified
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00002676/// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
2677/// function calls.
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002678///
2679/// \param Call The call expression to diagnose.
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00002680void Sema::CheckMemaccessArguments(const CallExpr *Call,
Anna Zaks0a151a12012-01-17 00:37:07 +00002681 unsigned BId,
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00002682 IdentifierInfo *FnName) {
Anna Zaks0a151a12012-01-17 00:37:07 +00002683 assert(BId != 0);
2684
Ted Kremenek1d59f7f2011-04-28 01:38:02 +00002685 // It is possible to have a non-standard definition of memset. Validate
Douglas Gregor707a23e2011-06-16 17:56:04 +00002686 // we have enough arguments, and if not, abort further checking.
Anna Zaks0a151a12012-01-17 00:37:07 +00002687 unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3);
Nico Webercda57822011-10-13 22:30:23 +00002688 if (Call->getNumArgs() < ExpectedNumArgs)
Ted Kremenek1d59f7f2011-04-28 01:38:02 +00002689 return;
2690
Anna Zaks0a151a12012-01-17 00:37:07 +00002691 unsigned LastArg = (BId == Builtin::BImemset ||
2692 BId == Builtin::BIstrndup ? 1 : 2);
2693 unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2);
Nico Webercda57822011-10-13 22:30:23 +00002694 const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts();
Chandler Carruth000d4282011-06-16 09:09:40 +00002695
2696 // We have special checking when the length is a sizeof expression.
2697 QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
2698 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
2699 llvm::FoldingSetNodeID SizeOfArgID;
2700
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002701 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
2702 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
Nico Webere4a1c642011-06-14 16:14:58 +00002703 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002704
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002705 QualType DestTy = Dest->getType();
2706 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
2707 QualType PointeeTy = DestPtrTy->getPointeeType();
John McCallf85e1932011-06-15 23:02:42 +00002708
Chandler Carruth000d4282011-06-16 09:09:40 +00002709 // Never warn about void type pointers. This can be used to suppress
2710 // false positives.
2711 if (PointeeTy->isVoidType())
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002712 continue;
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002713
Chandler Carruth000d4282011-06-16 09:09:40 +00002714 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
2715 // actually comparing the expressions for equality. Because computing the
2716 // expression IDs can be expensive, we only do this if the diagnostic is
2717 // enabled.
2718 if (SizeOfArg &&
2719 Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess,
2720 SizeOfArg->getExprLoc())) {
2721 // We only compute IDs for expressions if the warning is enabled, and
2722 // cache the sizeof arg's ID.
2723 if (SizeOfArgID == llvm::FoldingSetNodeID())
2724 SizeOfArg->Profile(SizeOfArgID, Context, true);
2725 llvm::FoldingSetNodeID DestID;
2726 Dest->Profile(DestID, Context, true);
2727 if (DestID == SizeOfArgID) {
Nico Webercda57822011-10-13 22:30:23 +00002728 // TODO: For strncpy() and friends, this could suggest sizeof(dst)
2729 // over sizeof(src) as well.
Chandler Carruth000d4282011-06-16 09:09:40 +00002730 unsigned ActionIdx = 0; // Default is to suggest dereferencing.
2731 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
2732 if (UnaryOp->getOpcode() == UO_AddrOf)
2733 ActionIdx = 1; // If its an address-of operator, just remove it.
2734 if (Context.getTypeSize(PointeeTy) == Context.getCharWidth())
2735 ActionIdx = 2; // If the pointee's size is sizeof(char),
2736 // suggest an explicit length.
Anna Zaksd9b859a2012-01-13 21:52:01 +00002737 unsigned DestSrcSelect =
Anna Zaks0a151a12012-01-17 00:37:07 +00002738 (BId == Builtin::BIstrndup ? 1 : ArgIdx);
Chandler Carruth000d4282011-06-16 09:09:40 +00002739 DiagRuntimeBehavior(SizeOfArg->getExprLoc(), Dest,
2740 PDiag(diag::warn_sizeof_pointer_expr_memaccess)
Nico Webercda57822011-10-13 22:30:23 +00002741 << FnName << DestSrcSelect << ActionIdx
Chandler Carruth000d4282011-06-16 09:09:40 +00002742 << Dest->getSourceRange()
2743 << SizeOfArg->getSourceRange());
2744 break;
2745 }
2746 }
2747
2748 // Also check for cases where the sizeof argument is the exact same
2749 // type as the memory argument, and where it points to a user-defined
2750 // record type.
2751 if (SizeOfArgTy != QualType()) {
2752 if (PointeeTy->isRecordType() &&
2753 Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
2754 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
2755 PDiag(diag::warn_sizeof_pointer_type_memaccess)
2756 << FnName << SizeOfArgTy << ArgIdx
2757 << PointeeTy << Dest->getSourceRange()
2758 << LenExpr->getSourceRange());
2759 break;
2760 }
Nico Webere4a1c642011-06-14 16:14:58 +00002761 }
2762
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002763 // Always complain about dynamic classes.
Anna Zaks0a151a12012-01-17 00:37:07 +00002764 if (isDynamicClassType(PointeeTy)) {
2765
2766 unsigned OperationType = 0;
2767 // "overwritten" if we're warning about the destination for any call
2768 // but memcmp; otherwise a verb appropriate to the call.
2769 if (ArgIdx != 0 || BId == Builtin::BImemcmp) {
2770 if (BId == Builtin::BImemcpy)
2771 OperationType = 1;
2772 else if(BId == Builtin::BImemmove)
2773 OperationType = 2;
2774 else if (BId == Builtin::BImemcmp)
2775 OperationType = 3;
2776 }
2777
Matt Beaumont-Gay5c5218e2011-08-19 20:40:18 +00002778 DiagRuntimeBehavior(
2779 Dest->getExprLoc(), Dest,
2780 PDiag(diag::warn_dyn_class_memaccess)
Anna Zaks0a151a12012-01-17 00:37:07 +00002781 << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx)
Anna Zaksd9b859a2012-01-13 21:52:01 +00002782 << FnName << PointeeTy
Anna Zaks0a151a12012-01-17 00:37:07 +00002783 << OperationType
Matt Beaumont-Gay5c5218e2011-08-19 20:40:18 +00002784 << Call->getCallee()->getSourceRange());
Anna Zaks0a151a12012-01-17 00:37:07 +00002785 } else if (PointeeTy.hasNonTrivialObjCLifetime() &&
2786 BId != Builtin::BImemset)
Matt Beaumont-Gay5c5218e2011-08-19 20:40:18 +00002787 DiagRuntimeBehavior(
2788 Dest->getExprLoc(), Dest,
2789 PDiag(diag::warn_arc_object_memaccess)
2790 << ArgIdx << FnName << PointeeTy
2791 << Call->getCallee()->getSourceRange());
John McCallf85e1932011-06-15 23:02:42 +00002792 else
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002793 continue;
John McCallf85e1932011-06-15 23:02:42 +00002794
2795 DiagRuntimeBehavior(
2796 Dest->getExprLoc(), Dest,
Chandler Carruth929f0132011-06-03 06:23:57 +00002797 PDiag(diag::note_bad_memaccess_silence)
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002798 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
2799 break;
2800 }
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002801 }
2802}
2803
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002804// A little helper routine: ignore addition and subtraction of integer literals.
2805// This intentionally does not ignore all integer constant expressions because
2806// we don't want to remove sizeof().
2807static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
2808 Ex = Ex->IgnoreParenCasts();
2809
2810 for (;;) {
2811 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
2812 if (!BO || !BO->isAdditiveOp())
2813 break;
2814
2815 const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
2816 const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
2817
2818 if (isa<IntegerLiteral>(RHS))
2819 Ex = LHS;
2820 else if (isa<IntegerLiteral>(LHS))
2821 Ex = RHS;
2822 else
2823 break;
2824 }
2825
2826 return Ex;
2827}
2828
2829// Warn if the user has made the 'size' argument to strlcpy or strlcat
2830// be the size of the source, instead of the destination.
2831void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
2832 IdentifierInfo *FnName) {
2833
2834 // Don't crash if the user has the wrong number of arguments
2835 if (Call->getNumArgs() != 3)
2836 return;
2837
2838 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
2839 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
2840 const Expr *CompareWithSrc = NULL;
2841
2842 // Look for 'strlcpy(dst, x, sizeof(x))'
2843 if (const Expr *Ex = getSizeOfExprArg(SizeArg))
2844 CompareWithSrc = Ex;
2845 else {
2846 // Look for 'strlcpy(dst, x, strlen(x))'
2847 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
Richard Smith180f4792011-11-10 06:34:14 +00002848 if (SizeCall->isBuiltinCall() == Builtin::BIstrlen
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002849 && SizeCall->getNumArgs() == 1)
2850 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
2851 }
2852 }
2853
2854 if (!CompareWithSrc)
2855 return;
2856
2857 // Determine if the argument to sizeof/strlen is equal to the source
2858 // argument. In principle there's all kinds of things you could do
2859 // here, for instance creating an == expression and evaluating it with
2860 // EvaluateAsBooleanCondition, but this uses a more direct technique:
2861 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
2862 if (!SrcArgDRE)
2863 return;
2864
2865 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
2866 if (!CompareWithSrcDRE ||
2867 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
2868 return;
2869
2870 const Expr *OriginalSizeArg = Call->getArg(2);
2871 Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size)
2872 << OriginalSizeArg->getSourceRange() << FnName;
2873
2874 // Output a FIXIT hint if the destination is an array (rather than a
2875 // pointer to an array). This could be enhanced to handle some
2876 // pointers if we know the actual size, like if DstArg is 'array+2'
2877 // we could say 'sizeof(array)-2'.
2878 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
Ted Kremenek8f746222011-08-18 22:48:41 +00002879 QualType DstArgTy = DstArg->getType();
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002880
Ted Kremenek8f746222011-08-18 22:48:41 +00002881 // Only handle constant-sized or VLAs, but not flexible members.
2882 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) {
2883 // Only issue the FIXIT for arrays of size > 1.
2884 if (CAT->getSize().getSExtValue() <= 1)
2885 return;
2886 } else if (!DstArgTy->isVariableArrayType()) {
2887 return;
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002888 }
Ted Kremenek8f746222011-08-18 22:48:41 +00002889
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002890 SmallString<128> sizeString;
Ted Kremenek8f746222011-08-18 22:48:41 +00002891 llvm::raw_svector_ostream OS(sizeString);
2892 OS << "sizeof(";
Douglas Gregor8987b232011-09-27 23:30:47 +00002893 DstArg->printPretty(OS, Context, 0, getPrintingPolicy());
Ted Kremenek8f746222011-08-18 22:48:41 +00002894 OS << ")";
2895
2896 Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
2897 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
2898 OS.str());
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002899}
2900
Anna Zaksc36bedc2012-02-01 19:08:57 +00002901/// Check if two expressions refer to the same declaration.
2902static bool referToTheSameDecl(const Expr *E1, const Expr *E2) {
2903 if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1))
2904 if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2))
2905 return D1->getDecl() == D2->getDecl();
2906 return false;
2907}
2908
2909static const Expr *getStrlenExprArg(const Expr *E) {
2910 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
2911 const FunctionDecl *FD = CE->getDirectCallee();
2912 if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen)
2913 return 0;
2914 return CE->getArg(0)->IgnoreParenCasts();
2915 }
2916 return 0;
2917}
2918
2919// Warn on anti-patterns as the 'size' argument to strncat.
2920// The correct size argument should look like following:
2921// strncat(dst, src, sizeof(dst) - strlen(dest) - 1);
2922void Sema::CheckStrncatArguments(const CallExpr *CE,
2923 IdentifierInfo *FnName) {
2924 // Don't crash if the user has the wrong number of arguments.
2925 if (CE->getNumArgs() < 3)
2926 return;
2927 const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts();
2928 const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts();
2929 const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts();
2930
2931 // Identify common expressions, which are wrongly used as the size argument
2932 // to strncat and may lead to buffer overflows.
2933 unsigned PatternType = 0;
2934 if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) {
2935 // - sizeof(dst)
2936 if (referToTheSameDecl(SizeOfArg, DstArg))
2937 PatternType = 1;
2938 // - sizeof(src)
2939 else if (referToTheSameDecl(SizeOfArg, SrcArg))
2940 PatternType = 2;
2941 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) {
2942 if (BE->getOpcode() == BO_Sub) {
2943 const Expr *L = BE->getLHS()->IgnoreParenCasts();
2944 const Expr *R = BE->getRHS()->IgnoreParenCasts();
2945 // - sizeof(dst) - strlen(dst)
2946 if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) &&
2947 referToTheSameDecl(DstArg, getStrlenExprArg(R)))
2948 PatternType = 1;
2949 // - sizeof(src) - (anything)
2950 else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L)))
2951 PatternType = 2;
2952 }
2953 }
2954
2955 if (PatternType == 0)
2956 return;
2957
Anna Zaksafdb0412012-02-03 01:27:37 +00002958 // Generate the diagnostic.
2959 SourceLocation SL = LenArg->getLocStart();
2960 SourceRange SR = LenArg->getSourceRange();
2961 SourceManager &SM = PP.getSourceManager();
2962
2963 // If the function is defined as a builtin macro, do not show macro expansion.
2964 if (SM.isMacroArgExpansion(SL)) {
2965 SL = SM.getSpellingLoc(SL);
2966 SR = SourceRange(SM.getSpellingLoc(SR.getBegin()),
2967 SM.getSpellingLoc(SR.getEnd()));
2968 }
2969
Anna Zaksc36bedc2012-02-01 19:08:57 +00002970 if (PatternType == 1)
Anna Zaksafdb0412012-02-03 01:27:37 +00002971 Diag(SL, diag::warn_strncat_large_size) << SR;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002972 else
Anna Zaksafdb0412012-02-03 01:27:37 +00002973 Diag(SL, diag::warn_strncat_src_size) << SR;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002974
2975 // Output a FIXIT hint if the destination is an array (rather than a
2976 // pointer to an array). This could be enhanced to handle some
2977 // pointers if we know the actual size, like if DstArg is 'array+2'
2978 // we could say 'sizeof(array)-2'.
2979 QualType DstArgTy = DstArg->getType();
2980
2981 // Only handle constant-sized or VLAs, but not flexible members.
2982 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) {
2983 // Only issue the FIXIT for arrays of size > 1.
2984 if (CAT->getSize().getSExtValue() <= 1)
2985 return;
2986 } else if (!DstArgTy->isVariableArrayType()) {
2987 return;
2988 }
2989
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002990 SmallString<128> sizeString;
Anna Zaksc36bedc2012-02-01 19:08:57 +00002991 llvm::raw_svector_ostream OS(sizeString);
2992 OS << "sizeof(";
2993 DstArg->printPretty(OS, Context, 0, getPrintingPolicy());
2994 OS << ") - ";
2995 OS << "strlen(";
2996 DstArg->printPretty(OS, Context, 0, getPrintingPolicy());
2997 OS << ") - 1";
2998
Anna Zaksafdb0412012-02-03 01:27:37 +00002999 Diag(SL, diag::note_strncat_wrong_size)
3000 << FixItHint::CreateReplacement(SR, OS.str());
Anna Zaksc36bedc2012-02-01 19:08:57 +00003001}
3002
Ted Kremenek06de2762007-08-17 16:46:58 +00003003//===--- CHECK: Return Address of Stack Variable --------------------------===//
3004
Chris Lattner5f9e2722011-07-23 10:55:15 +00003005static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars);
3006static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00003007
3008/// CheckReturnStackAddr - Check if a return statement returns the address
3009/// of a stack variable.
3010void
3011Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
3012 SourceLocation ReturnLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00003013
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003014 Expr *stackE = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003015 SmallVector<DeclRefExpr *, 8> refVars;
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003016
3017 // Perform checking for returned stack addresses, local blocks,
3018 // label addresses or references to temporaries.
John McCallf85e1932011-06-15 23:02:42 +00003019 if (lhsType->isPointerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003020 (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003021 stackE = EvalAddr(RetValExp, refVars);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00003022 } else if (lhsType->isReferenceType()) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003023 stackE = EvalVal(RetValExp, refVars);
3024 }
3025
3026 if (stackE == 0)
3027 return; // Nothing suspicious was found.
3028
3029 SourceLocation diagLoc;
3030 SourceRange diagRange;
3031 if (refVars.empty()) {
3032 diagLoc = stackE->getLocStart();
3033 diagRange = stackE->getSourceRange();
3034 } else {
3035 // We followed through a reference variable. 'stackE' contains the
3036 // problematic expression but we will warn at the return statement pointing
3037 // at the reference variable. We will later display the "trail" of
3038 // reference variables using notes.
3039 diagLoc = refVars[0]->getLocStart();
3040 diagRange = refVars[0]->getSourceRange();
3041 }
3042
3043 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var.
3044 Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref
3045 : diag::warn_ret_stack_addr)
3046 << DR->getDecl()->getDeclName() << diagRange;
3047 } else if (isa<BlockExpr>(stackE)) { // local block.
3048 Diag(diagLoc, diag::err_ret_local_block) << diagRange;
3049 } else if (isa<AddrLabelExpr>(stackE)) { // address of label.
3050 Diag(diagLoc, diag::warn_ret_addr_label) << diagRange;
3051 } else { // local temporary.
3052 Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref
3053 : diag::warn_ret_local_temp_addr)
3054 << diagRange;
3055 }
3056
3057 // Display the "trail" of reference variables that we followed until we
3058 // found the problematic expression using notes.
3059 for (unsigned i = 0, e = refVars.size(); i != e; ++i) {
3060 VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl());
3061 // If this var binds to another reference var, show the range of the next
3062 // var, otherwise the var binds to the problematic expression, in which case
3063 // show the range of the expression.
3064 SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange()
3065 : stackE->getSourceRange();
3066 Diag(VD->getLocation(), diag::note_ref_var_local_bind)
3067 << VD->getDeclName() << range;
Ted Kremenek06de2762007-08-17 16:46:58 +00003068 }
3069}
3070
3071/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
3072/// check if the expression in a return statement evaluates to an address
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003073/// to a location on the stack, a local block, an address of a label, or a
3074/// reference to local temporary. The recursion is used to traverse the
Ted Kremenek06de2762007-08-17 16:46:58 +00003075/// AST of the return expression, with recursion backtracking when we
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003076/// encounter a subexpression that (1) clearly does not lead to one of the
3077/// above problematic expressions (2) is something we cannot determine leads to
3078/// a problematic expression based on such local checking.
3079///
3080/// Both EvalAddr and EvalVal follow through reference variables to evaluate
3081/// the expression that they point to. Such variables are added to the
3082/// 'refVars' vector so that we know what the reference variable "trail" was.
Ted Kremenek06de2762007-08-17 16:46:58 +00003083///
Ted Kremeneke8c600f2007-08-28 17:02:55 +00003084/// EvalAddr processes expressions that are pointers that are used as
3085/// references (and not L-values). EvalVal handles all other values.
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003086/// At the base case of the recursion is a check for the above problematic
3087/// expressions.
Ted Kremenek06de2762007-08-17 16:46:58 +00003088///
3089/// This implementation handles:
3090///
3091/// * pointer-to-pointer casts
3092/// * implicit conversions from array references to pointers
3093/// * taking the address of fields
3094/// * arbitrary interplay between "&" and "*" operators
3095/// * pointer arithmetic from an address of a stack variable
3096/// * taking the address of an array element where the array is on the stack
Chris Lattner5f9e2722011-07-23 10:55:15 +00003097static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003098 if (E->isTypeDependent())
3099 return NULL;
3100
Ted Kremenek06de2762007-08-17 16:46:58 +00003101 // We should only be called for evaluating pointer expressions.
David Chisnall0f436562009-08-17 16:35:33 +00003102 assert((E->getType()->isAnyPointerType() ||
Steve Naroffdd972f22008-09-05 22:11:13 +00003103 E->getType()->isBlockPointerType() ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003104 E->getType()->isObjCQualifiedIdType()) &&
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003105 "EvalAddr only works on pointers");
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Peter Collingbournef111d932011-04-15 00:35:48 +00003107 E = E->IgnoreParens();
3108
Ted Kremenek06de2762007-08-17 16:46:58 +00003109 // Our "symbolic interpreter" is just a dispatch off the currently
3110 // viewed AST node. We then recursively traverse the AST by calling
3111 // EvalAddr and EvalVal appropriately.
3112 switch (E->getStmtClass()) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003113 case Stmt::DeclRefExprClass: {
3114 DeclRefExpr *DR = cast<DeclRefExpr>(E);
3115
3116 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
3117 // If this is a reference variable, follow through to the expression that
3118 // it points to.
3119 if (V->hasLocalStorage() &&
3120 V->getType()->isReferenceType() && V->hasInit()) {
3121 // Add the reference variable to the "trail".
3122 refVars.push_back(DR);
3123 return EvalAddr(V->getInit(), refVars);
3124 }
3125
3126 return NULL;
3127 }
Ted Kremenek06de2762007-08-17 16:46:58 +00003128
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003129 case Stmt::UnaryOperatorClass: {
3130 // The only unary operator that make sense to handle here
3131 // is AddrOf. All others don't make sense as pointers.
3132 UnaryOperator *U = cast<UnaryOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003133
John McCall2de56d12010-08-25 11:45:40 +00003134 if (U->getOpcode() == UO_AddrOf)
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003135 return EvalVal(U->getSubExpr(), refVars);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003136 else
Ted Kremenek06de2762007-08-17 16:46:58 +00003137 return NULL;
3138 }
Mike Stump1eb44332009-09-09 15:08:12 +00003139
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003140 case Stmt::BinaryOperatorClass: {
3141 // Handle pointer arithmetic. All other binary operators are not valid
3142 // in this context.
3143 BinaryOperator *B = cast<BinaryOperator>(E);
John McCall2de56d12010-08-25 11:45:40 +00003144 BinaryOperatorKind op = B->getOpcode();
Mike Stump1eb44332009-09-09 15:08:12 +00003145
John McCall2de56d12010-08-25 11:45:40 +00003146 if (op != BO_Add && op != BO_Sub)
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003147 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00003148
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003149 Expr *Base = B->getLHS();
3150
3151 // Determine which argument is the real pointer base. It could be
3152 // the RHS argument instead of the LHS.
3153 if (!Base->getType()->isPointerType()) Base = B->getRHS();
Mike Stump1eb44332009-09-09 15:08:12 +00003154
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003155 assert (Base->getType()->isPointerType());
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003156 return EvalAddr(Base, refVars);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003157 }
Steve Naroff61f40a22008-09-10 19:17:48 +00003158
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003159 // For conditional operators we need to see if either the LHS or RHS are
3160 // valid DeclRefExpr*s. If one of them is valid, we return it.
3161 case Stmt::ConditionalOperatorClass: {
3162 ConditionalOperator *C = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003164 // Handle the GNU extension for missing LHS.
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00003165 if (Expr *lhsExpr = C->getLHS()) {
3166 // In C++, we can have a throw-expression, which has 'void' type.
3167 if (!lhsExpr->getType()->isVoidType())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003168 if (Expr* LHS = EvalAddr(lhsExpr, refVars))
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00003169 return LHS;
3170 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003171
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00003172 // In C++, we can have a throw-expression, which has 'void' type.
3173 if (C->getRHS()->getType()->isVoidType())
3174 return NULL;
3175
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003176 return EvalAddr(C->getRHS(), refVars);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003177 }
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003178
3179 case Stmt::BlockExprClass:
John McCall469a1eb2011-02-02 13:00:07 +00003180 if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003181 return E; // local block.
3182 return NULL;
3183
3184 case Stmt::AddrLabelExprClass:
3185 return E; // address of label.
Mike Stump1eb44332009-09-09 15:08:12 +00003186
John McCall80ee6e82011-11-10 05:35:25 +00003187 case Stmt::ExprWithCleanupsClass:
3188 return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars);
3189
Ted Kremenek54b52742008-08-07 00:49:01 +00003190 // For casts, we need to handle conversions from arrays to
3191 // pointer values, and pointer-to-pointer conversions.
Douglas Gregor49badde2008-10-27 19:41:14 +00003192 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00003193 case Stmt::CStyleCastExprClass:
John McCallf85e1932011-06-15 23:02:42 +00003194 case Stmt::CXXFunctionalCastExprClass:
Eli Friedman8b9414e2012-02-23 23:04:32 +00003195 case Stmt::ObjCBridgedCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00003196 case Stmt::CXXStaticCastExprClass:
3197 case Stmt::CXXDynamicCastExprClass:
Douglas Gregor49badde2008-10-27 19:41:14 +00003198 case Stmt::CXXConstCastExprClass:
3199 case Stmt::CXXReinterpretCastExprClass: {
Eli Friedman8b9414e2012-02-23 23:04:32 +00003200 Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
3201 switch (cast<CastExpr>(E)->getCastKind()) {
3202 case CK_BitCast:
3203 case CK_LValueToRValue:
3204 case CK_NoOp:
3205 case CK_BaseToDerived:
3206 case CK_DerivedToBase:
3207 case CK_UncheckedDerivedToBase:
3208 case CK_Dynamic:
3209 case CK_CPointerToObjCPointerCast:
3210 case CK_BlockPointerToObjCPointerCast:
3211 case CK_AnyPointerToBlockPointerCast:
3212 return EvalAddr(SubExpr, refVars);
3213
3214 case CK_ArrayToPointerDecay:
3215 return EvalVal(SubExpr, refVars);
3216
3217 default:
3218 return 0;
3219 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003220 }
Mike Stump1eb44332009-09-09 15:08:12 +00003221
Douglas Gregor03e80032011-06-21 17:03:29 +00003222 case Stmt::MaterializeTemporaryExprClass:
3223 if (Expr *Result = EvalAddr(
3224 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
3225 refVars))
3226 return Result;
3227
3228 return E;
3229
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00003230 // Everything else: we simply don't reason about them.
3231 default:
3232 return NULL;
3233 }
Ted Kremenek06de2762007-08-17 16:46:58 +00003234}
Mike Stump1eb44332009-09-09 15:08:12 +00003235
Ted Kremenek06de2762007-08-17 16:46:58 +00003236
3237/// EvalVal - This function is complements EvalAddr in the mutual recursion.
3238/// See the comments for EvalAddr for more details.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003239static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) {
Ted Kremenek68957a92010-08-04 20:01:07 +00003240do {
Ted Kremeneke8c600f2007-08-28 17:02:55 +00003241 // We should only be called for evaluating non-pointer expressions, or
3242 // expressions with a pointer type that are not used as references but instead
3243 // are l-values (e.g., DeclRefExpr with a pointer type).
Mike Stump1eb44332009-09-09 15:08:12 +00003244
Ted Kremenek06de2762007-08-17 16:46:58 +00003245 // Our "symbolic interpreter" is just a dispatch off the currently
3246 // viewed AST node. We then recursively traverse the AST by calling
3247 // EvalAddr and EvalVal appropriately.
Peter Collingbournef111d932011-04-15 00:35:48 +00003248
3249 E = E->IgnoreParens();
Ted Kremenek06de2762007-08-17 16:46:58 +00003250 switch (E->getStmtClass()) {
Ted Kremenek68957a92010-08-04 20:01:07 +00003251 case Stmt::ImplicitCastExprClass: {
3252 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
John McCall5baba9d2010-08-25 10:28:54 +00003253 if (IE->getValueKind() == VK_LValue) {
Ted Kremenek68957a92010-08-04 20:01:07 +00003254 E = IE->getSubExpr();
3255 continue;
3256 }
3257 return NULL;
3258 }
3259
John McCall80ee6e82011-11-10 05:35:25 +00003260 case Stmt::ExprWithCleanupsClass:
3261 return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars);
3262
Douglas Gregora2813ce2009-10-23 18:54:35 +00003263 case Stmt::DeclRefExprClass: {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003264 // When we hit a DeclRefExpr we are looking at code that refers to a
3265 // variable's name. If it's not a reference variable we check if it has
3266 // local storage within the function, and if so, return the expression.
Ted Kremenek06de2762007-08-17 16:46:58 +00003267 DeclRefExpr *DR = cast<DeclRefExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003268
Ted Kremenek06de2762007-08-17 16:46:58 +00003269 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003270 if (V->hasLocalStorage()) {
3271 if (!V->getType()->isReferenceType())
3272 return DR;
3273
3274 // Reference variable, follow through to the expression that
3275 // it points to.
3276 if (V->hasInit()) {
3277 // Add the reference variable to the "trail".
3278 refVars.push_back(DR);
3279 return EvalVal(V->getInit(), refVars);
3280 }
3281 }
Mike Stump1eb44332009-09-09 15:08:12 +00003282
Ted Kremenek06de2762007-08-17 16:46:58 +00003283 return NULL;
3284 }
Mike Stump1eb44332009-09-09 15:08:12 +00003285
Ted Kremenek06de2762007-08-17 16:46:58 +00003286 case Stmt::UnaryOperatorClass: {
3287 // The only unary operator that make sense to handle here
3288 // is Deref. All others don't resolve to a "name." This includes
3289 // handling all sorts of rvalues passed to a unary operator.
3290 UnaryOperator *U = cast<UnaryOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003291
John McCall2de56d12010-08-25 11:45:40 +00003292 if (U->getOpcode() == UO_Deref)
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003293 return EvalAddr(U->getSubExpr(), refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00003294
3295 return NULL;
3296 }
Mike Stump1eb44332009-09-09 15:08:12 +00003297
Ted Kremenek06de2762007-08-17 16:46:58 +00003298 case Stmt::ArraySubscriptExprClass: {
3299 // Array subscripts are potential references to data on the stack. We
3300 // retrieve the DeclRefExpr* for the array variable if it indeed
3301 // has local storage.
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003302 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00003303 }
Mike Stump1eb44332009-09-09 15:08:12 +00003304
Ted Kremenek06de2762007-08-17 16:46:58 +00003305 case Stmt::ConditionalOperatorClass: {
3306 // For conditional operators we need to see if either the LHS or RHS are
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003307 // non-NULL Expr's. If one is non-NULL, we return it.
Ted Kremenek06de2762007-08-17 16:46:58 +00003308 ConditionalOperator *C = cast<ConditionalOperator>(E);
3309
Anders Carlsson39073232007-11-30 19:04:31 +00003310 // Handle the GNU extension for missing LHS.
3311 if (Expr *lhsExpr = C->getLHS())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003312 if (Expr *LHS = EvalVal(lhsExpr, refVars))
Anders Carlsson39073232007-11-30 19:04:31 +00003313 return LHS;
3314
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003315 return EvalVal(C->getRHS(), refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00003316 }
Mike Stump1eb44332009-09-09 15:08:12 +00003317
Ted Kremenek06de2762007-08-17 16:46:58 +00003318 // Accesses to members are potential references to data on the stack.
Douglas Gregor83f6faf2009-08-31 23:41:50 +00003319 case Stmt::MemberExprClass: {
Ted Kremenek06de2762007-08-17 16:46:58 +00003320 MemberExpr *M = cast<MemberExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00003321
Ted Kremenek06de2762007-08-17 16:46:58 +00003322 // Check for indirect access. We only want direct field accesses.
Ted Kremeneka423e812010-09-02 01:12:13 +00003323 if (M->isArrow())
Ted Kremenek06de2762007-08-17 16:46:58 +00003324 return NULL;
Ted Kremeneka423e812010-09-02 01:12:13 +00003325
3326 // Check whether the member type is itself a reference, in which case
3327 // we're not going to refer to the member, but to what the member refers to.
3328 if (M->getMemberDecl()->getType()->isReferenceType())
3329 return NULL;
3330
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003331 return EvalVal(M->getBase(), refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00003332 }
Mike Stump1eb44332009-09-09 15:08:12 +00003333
Douglas Gregor03e80032011-06-21 17:03:29 +00003334 case Stmt::MaterializeTemporaryExprClass:
3335 if (Expr *Result = EvalVal(
3336 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
3337 refVars))
3338 return Result;
3339
3340 return E;
3341
Ted Kremenek06de2762007-08-17 16:46:58 +00003342 default:
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00003343 // Check that we don't return or take the address of a reference to a
3344 // temporary. This is only useful in C++.
3345 if (!E->isTypeDependent() && E->isRValue())
3346 return E;
3347
3348 // Everything else: we simply don't reason about them.
Ted Kremenek06de2762007-08-17 16:46:58 +00003349 return NULL;
3350 }
Ted Kremenek68957a92010-08-04 20:01:07 +00003351} while (true);
Ted Kremenek06de2762007-08-17 16:46:58 +00003352}
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003353
3354//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
3355
3356/// Check for comparisons of floating point operands using != and ==.
3357/// Issue a warning if these are no self-comparisons, as they are not likely
3358/// to do what the programmer intended.
Richard Trieudd225092011-09-15 21:56:47 +00003359void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003360 bool EmitWarning = true;
Mike Stump1eb44332009-09-09 15:08:12 +00003361
Richard Trieudd225092011-09-15 21:56:47 +00003362 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
3363 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003364
3365 // Special case: check for x == x (which is OK).
3366 // Do not emit warnings for such cases.
3367 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
3368 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
3369 if (DRL->getDecl() == DRR->getDecl())
3370 EmitWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003371
3372
Ted Kremenek1b500bb2007-11-29 00:59:04 +00003373 // Special case: check for comparisons against literals that can be exactly
3374 // represented by APFloat. In such cases, do not emit a warning. This
3375 // is a heuristic: often comparison against such literals are used to
3376 // detect if a value in a variable has not changed. This clearly can
3377 // lead to false negatives.
3378 if (EmitWarning) {
3379 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
3380 if (FLL->isExact())
3381 EmitWarning = false;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00003382 } else
Ted Kremenek1b500bb2007-11-29 00:59:04 +00003383 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){
3384 if (FLR->isExact())
3385 EmitWarning = false;
3386 }
3387 }
Mike Stump1eb44332009-09-09 15:08:12 +00003388
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003389 // Check for comparisons with builtin types.
Sebastian Redl0eb23302009-01-19 00:08:26 +00003390 if (EmitWarning)
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003391 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
Richard Smith180f4792011-11-10 06:34:14 +00003392 if (CL->isBuiltinCall())
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003393 EmitWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003394
Sebastian Redl0eb23302009-01-19 00:08:26 +00003395 if (EmitWarning)
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003396 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
Richard Smith180f4792011-11-10 06:34:14 +00003397 if (CR->isBuiltinCall())
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003398 EmitWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003399
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003400 // Emit the diagnostic.
3401 if (EmitWarning)
Richard Trieudd225092011-09-15 21:56:47 +00003402 Diag(Loc, diag::warn_floatingpoint_eq)
3403 << LHS->getSourceRange() << RHS->getSourceRange();
Ted Kremenek588e5eb2007-11-25 00:58:00 +00003404}
John McCallba26e582010-01-04 23:21:16 +00003405
John McCallf2370c92010-01-06 05:24:50 +00003406//===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
3407//===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
John McCallba26e582010-01-04 23:21:16 +00003408
John McCallf2370c92010-01-06 05:24:50 +00003409namespace {
John McCallba26e582010-01-04 23:21:16 +00003410
John McCallf2370c92010-01-06 05:24:50 +00003411/// Structure recording the 'active' range of an integer-valued
3412/// expression.
3413struct IntRange {
3414 /// The number of bits active in the int.
3415 unsigned Width;
John McCallba26e582010-01-04 23:21:16 +00003416
John McCallf2370c92010-01-06 05:24:50 +00003417 /// True if the int is known not to have negative values.
3418 bool NonNegative;
John McCallba26e582010-01-04 23:21:16 +00003419
John McCallf2370c92010-01-06 05:24:50 +00003420 IntRange(unsigned Width, bool NonNegative)
3421 : Width(Width), NonNegative(NonNegative)
3422 {}
John McCallba26e582010-01-04 23:21:16 +00003423
John McCall1844a6e2010-11-10 23:38:19 +00003424 /// Returns the range of the bool type.
John McCallf2370c92010-01-06 05:24:50 +00003425 static IntRange forBoolType() {
3426 return IntRange(1, true);
John McCall51313c32010-01-04 23:31:57 +00003427 }
3428
John McCall1844a6e2010-11-10 23:38:19 +00003429 /// Returns the range of an opaque value of the given integral type.
3430 static IntRange forValueOfType(ASTContext &C, QualType T) {
3431 return forValueOfCanonicalType(C,
3432 T->getCanonicalTypeInternal().getTypePtr());
John McCall51313c32010-01-04 23:31:57 +00003433 }
3434
John McCall1844a6e2010-11-10 23:38:19 +00003435 /// Returns the range of an opaque value of a canonical integral type.
3436 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
John McCallf2370c92010-01-06 05:24:50 +00003437 assert(T->isCanonicalUnqualified());
3438
3439 if (const VectorType *VT = dyn_cast<VectorType>(T))
3440 T = VT->getElementType().getTypePtr();
3441 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
3442 T = CT->getElementType().getTypePtr();
John McCall323ed742010-05-06 08:58:33 +00003443
John McCall091f23f2010-11-09 22:22:12 +00003444 // For enum types, use the known bit width of the enumerators.
John McCall323ed742010-05-06 08:58:33 +00003445 if (const EnumType *ET = dyn_cast<EnumType>(T)) {
3446 EnumDecl *Enum = ET->getDecl();
John McCall5e1cdac2011-10-07 06:10:15 +00003447 if (!Enum->isCompleteDefinition())
John McCall091f23f2010-11-09 22:22:12 +00003448 return IntRange(C.getIntWidth(QualType(T, 0)), false);
3449
John McCall323ed742010-05-06 08:58:33 +00003450 unsigned NumPositive = Enum->getNumPositiveBits();
3451 unsigned NumNegative = Enum->getNumNegativeBits();
3452
3453 return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0);
3454 }
John McCallf2370c92010-01-06 05:24:50 +00003455
3456 const BuiltinType *BT = cast<BuiltinType>(T);
3457 assert(BT->isInteger());
3458
3459 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
3460 }
3461
John McCall1844a6e2010-11-10 23:38:19 +00003462 /// Returns the "target" range of a canonical integral type, i.e.
3463 /// the range of values expressible in the type.
3464 ///
3465 /// This matches forValueOfCanonicalType except that enums have the
3466 /// full range of their type, not the range of their enumerators.
3467 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
3468 assert(T->isCanonicalUnqualified());
3469
3470 if (const VectorType *VT = dyn_cast<VectorType>(T))
3471 T = VT->getElementType().getTypePtr();
3472 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
3473 T = CT->getElementType().getTypePtr();
3474 if (const EnumType *ET = dyn_cast<EnumType>(T))
Douglas Gregor69ff26b2011-09-08 23:29:05 +00003475 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
John McCall1844a6e2010-11-10 23:38:19 +00003476
3477 const BuiltinType *BT = cast<BuiltinType>(T);
3478 assert(BT->isInteger());
3479
3480 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
3481 }
3482
3483 /// Returns the supremum of two ranges: i.e. their conservative merge.
John McCallc0cd21d2010-02-23 19:22:29 +00003484 static IntRange join(IntRange L, IntRange R) {
John McCallf2370c92010-01-06 05:24:50 +00003485 return IntRange(std::max(L.Width, R.Width),
John McCall60fad452010-01-06 22:07:33 +00003486 L.NonNegative && R.NonNegative);
3487 }
3488
John McCall1844a6e2010-11-10 23:38:19 +00003489 /// Returns the infinum of two ranges: i.e. their aggressive merge.
John McCallc0cd21d2010-02-23 19:22:29 +00003490 static IntRange meet(IntRange L, IntRange R) {
John McCall60fad452010-01-06 22:07:33 +00003491 return IntRange(std::min(L.Width, R.Width),
3492 L.NonNegative || R.NonNegative);
John McCallf2370c92010-01-06 05:24:50 +00003493 }
3494};
3495
Ted Kremenek0692a192012-01-31 05:37:37 +00003496static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value,
3497 unsigned MaxWidth) {
John McCallf2370c92010-01-06 05:24:50 +00003498 if (value.isSigned() && value.isNegative())
3499 return IntRange(value.getMinSignedBits(), false);
3500
3501 if (value.getBitWidth() > MaxWidth)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003502 value = value.trunc(MaxWidth);
John McCallf2370c92010-01-06 05:24:50 +00003503
3504 // isNonNegative() just checks the sign bit without considering
3505 // signedness.
3506 return IntRange(value.getActiveBits(), true);
3507}
3508
Ted Kremenek0692a192012-01-31 05:37:37 +00003509static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
3510 unsigned MaxWidth) {
John McCallf2370c92010-01-06 05:24:50 +00003511 if (result.isInt())
3512 return GetValueRange(C, result.getInt(), MaxWidth);
3513
3514 if (result.isVector()) {
John McCall0acc3112010-01-06 22:57:21 +00003515 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
3516 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
3517 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
3518 R = IntRange::join(R, El);
3519 }
John McCallf2370c92010-01-06 05:24:50 +00003520 return R;
3521 }
3522
3523 if (result.isComplexInt()) {
3524 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
3525 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
3526 return IntRange::join(R, I);
John McCall51313c32010-01-04 23:31:57 +00003527 }
3528
3529 // This can happen with lossless casts to intptr_t of "based" lvalues.
3530 // Assume it might use arbitrary bits.
John McCall0acc3112010-01-06 22:57:21 +00003531 // FIXME: The only reason we need to pass the type in here is to get
3532 // the sign right on this one case. It would be nice if APValue
3533 // preserved this.
Eli Friedman65639282012-01-04 23:13:47 +00003534 assert(result.isLValue() || result.isAddrLabelDiff());
Douglas Gregor5e9ebb32011-05-21 16:28:01 +00003535 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
John McCall51313c32010-01-04 23:31:57 +00003536}
John McCallf2370c92010-01-06 05:24:50 +00003537
3538/// Pseudo-evaluate the given integer expression, estimating the
3539/// range of values it might take.
3540///
3541/// \param MaxWidth - the width to which the value will be truncated
Ted Kremenek0692a192012-01-31 05:37:37 +00003542static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) {
John McCallf2370c92010-01-06 05:24:50 +00003543 E = E->IgnoreParens();
3544
3545 // Try a full evaluation first.
3546 Expr::EvalResult result;
Richard Smith51f47082011-10-29 00:50:52 +00003547 if (E->EvaluateAsRValue(result, C))
John McCall0acc3112010-01-06 22:57:21 +00003548 return GetValueRange(C, result.Val, E->getType(), MaxWidth);
John McCallf2370c92010-01-06 05:24:50 +00003549
3550 // I think we only want to look through implicit casts here; if the
3551 // user has an explicit widening cast, we should treat the value as
3552 // being of the new, wider type.
3553 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
Eli Friedmanb17ee5b2011-12-15 02:41:52 +00003554 if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue)
John McCallf2370c92010-01-06 05:24:50 +00003555 return GetExprRange(C, CE->getSubExpr(), MaxWidth);
3556
John McCall1844a6e2010-11-10 23:38:19 +00003557 IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType());
John McCallf2370c92010-01-06 05:24:50 +00003558
John McCall2de56d12010-08-25 11:45:40 +00003559 bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast);
John McCall60fad452010-01-06 22:07:33 +00003560
John McCallf2370c92010-01-06 05:24:50 +00003561 // Assume that non-integer casts can span the full range of the type.
John McCall60fad452010-01-06 22:07:33 +00003562 if (!isIntegerCast)
John McCallf2370c92010-01-06 05:24:50 +00003563 return OutputTypeRange;
3564
3565 IntRange SubRange
3566 = GetExprRange(C, CE->getSubExpr(),
3567 std::min(MaxWidth, OutputTypeRange.Width));
3568
3569 // Bail out if the subexpr's range is as wide as the cast type.
3570 if (SubRange.Width >= OutputTypeRange.Width)
3571 return OutputTypeRange;
3572
3573 // Otherwise, we take the smaller width, and we're non-negative if
3574 // either the output type or the subexpr is.
3575 return IntRange(SubRange.Width,
3576 SubRange.NonNegative || OutputTypeRange.NonNegative);
3577 }
3578
3579 if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
3580 // If we can fold the condition, just take that operand.
3581 bool CondResult;
3582 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
3583 return GetExprRange(C, CondResult ? CO->getTrueExpr()
3584 : CO->getFalseExpr(),
3585 MaxWidth);
3586
3587 // Otherwise, conservatively merge.
3588 IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
3589 IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
3590 return IntRange::join(L, R);
3591 }
3592
3593 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3594 switch (BO->getOpcode()) {
3595
3596 // Boolean-valued operations are single-bit and positive.
John McCall2de56d12010-08-25 11:45:40 +00003597 case BO_LAnd:
3598 case BO_LOr:
3599 case BO_LT:
3600 case BO_GT:
3601 case BO_LE:
3602 case BO_GE:
3603 case BO_EQ:
3604 case BO_NE:
John McCallf2370c92010-01-06 05:24:50 +00003605 return IntRange::forBoolType();
3606
John McCall862ff872011-07-13 06:35:24 +00003607 // The type of the assignments is the type of the LHS, so the RHS
3608 // is not necessarily the same type.
John McCall2de56d12010-08-25 11:45:40 +00003609 case BO_MulAssign:
3610 case BO_DivAssign:
3611 case BO_RemAssign:
3612 case BO_AddAssign:
3613 case BO_SubAssign:
John McCall862ff872011-07-13 06:35:24 +00003614 case BO_XorAssign:
3615 case BO_OrAssign:
3616 // TODO: bitfields?
John McCall1844a6e2010-11-10 23:38:19 +00003617 return IntRange::forValueOfType(C, E->getType());
John McCallc0cd21d2010-02-23 19:22:29 +00003618
John McCall862ff872011-07-13 06:35:24 +00003619 // Simple assignments just pass through the RHS, which will have
3620 // been coerced to the LHS type.
3621 case BO_Assign:
3622 // TODO: bitfields?
3623 return GetExprRange(C, BO->getRHS(), MaxWidth);
3624
John McCallf2370c92010-01-06 05:24:50 +00003625 // Operations with opaque sources are black-listed.
John McCall2de56d12010-08-25 11:45:40 +00003626 case BO_PtrMemD:
3627 case BO_PtrMemI:
John McCall1844a6e2010-11-10 23:38:19 +00003628 return IntRange::forValueOfType(C, E->getType());
John McCallf2370c92010-01-06 05:24:50 +00003629
John McCall60fad452010-01-06 22:07:33 +00003630 // Bitwise-and uses the *infinum* of the two source ranges.
John McCall2de56d12010-08-25 11:45:40 +00003631 case BO_And:
3632 case BO_AndAssign:
John McCall60fad452010-01-06 22:07:33 +00003633 return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
3634 GetExprRange(C, BO->getRHS(), MaxWidth));
3635
John McCallf2370c92010-01-06 05:24:50 +00003636 // Left shift gets black-listed based on a judgement call.
John McCall2de56d12010-08-25 11:45:40 +00003637 case BO_Shl:
John McCall3aae6092010-04-07 01:14:35 +00003638 // ...except that we want to treat '1 << (blah)' as logically
3639 // positive. It's an important idiom.
3640 if (IntegerLiteral *I
3641 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
3642 if (I->getValue() == 1) {
John McCall1844a6e2010-11-10 23:38:19 +00003643 IntRange R = IntRange::forValueOfType(C, E->getType());
John McCall3aae6092010-04-07 01:14:35 +00003644 return IntRange(R.Width, /*NonNegative*/ true);
3645 }
3646 }
3647 // fallthrough
3648
John McCall2de56d12010-08-25 11:45:40 +00003649 case BO_ShlAssign:
John McCall1844a6e2010-11-10 23:38:19 +00003650 return IntRange::forValueOfType(C, E->getType());
John McCallf2370c92010-01-06 05:24:50 +00003651
John McCall60fad452010-01-06 22:07:33 +00003652 // Right shift by a constant can narrow its left argument.
John McCall2de56d12010-08-25 11:45:40 +00003653 case BO_Shr:
3654 case BO_ShrAssign: {
John McCall60fad452010-01-06 22:07:33 +00003655 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
3656
3657 // If the shift amount is a positive constant, drop the width by
3658 // that much.
3659 llvm::APSInt shift;
3660 if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
3661 shift.isNonNegative()) {
3662 unsigned zext = shift.getZExtValue();
3663 if (zext >= L.Width)
3664 L.Width = (L.NonNegative ? 0 : 1);
3665 else
3666 L.Width -= zext;
3667 }
3668
3669 return L;
3670 }
3671
3672 // Comma acts as its right operand.
John McCall2de56d12010-08-25 11:45:40 +00003673 case BO_Comma:
John McCallf2370c92010-01-06 05:24:50 +00003674 return GetExprRange(C, BO->getRHS(), MaxWidth);
3675
John McCall60fad452010-01-06 22:07:33 +00003676 // Black-list pointer subtractions.
John McCall2de56d12010-08-25 11:45:40 +00003677 case BO_Sub:
John McCallf2370c92010-01-06 05:24:50 +00003678 if (BO->getLHS()->getType()->isPointerType())
John McCall1844a6e2010-11-10 23:38:19 +00003679 return IntRange::forValueOfType(C, E->getType());
John McCall00fe7612011-07-14 22:39:48 +00003680 break;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00003681
John McCall00fe7612011-07-14 22:39:48 +00003682 // The width of a division result is mostly determined by the size
3683 // of the LHS.
3684 case BO_Div: {
3685 // Don't 'pre-truncate' the operands.
3686 unsigned opWidth = C.getIntWidth(E->getType());
3687 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
3688
3689 // If the divisor is constant, use that.
3690 llvm::APSInt divisor;
3691 if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
3692 unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
3693 if (log2 >= L.Width)
3694 L.Width = (L.NonNegative ? 0 : 1);
3695 else
3696 L.Width = std::min(L.Width - log2, MaxWidth);
3697 return L;
3698 }
3699
3700 // Otherwise, just use the LHS's width.
3701 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
3702 return IntRange(L.Width, L.NonNegative && R.NonNegative);
3703 }
3704
3705 // The result of a remainder can't be larger than the result of
3706 // either side.
3707 case BO_Rem: {
3708 // Don't 'pre-truncate' the operands.
3709 unsigned opWidth = C.getIntWidth(E->getType());
3710 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
3711 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
3712
3713 IntRange meet = IntRange::meet(L, R);
3714 meet.Width = std::min(meet.Width, MaxWidth);
3715 return meet;
3716 }
3717
3718 // The default behavior is okay for these.
3719 case BO_Mul:
3720 case BO_Add:
3721 case BO_Xor:
3722 case BO_Or:
John McCallf2370c92010-01-06 05:24:50 +00003723 break;
3724 }
3725
John McCall00fe7612011-07-14 22:39:48 +00003726 // The default case is to treat the operation as if it were closed
3727 // on the narrowest type that encompasses both operands.
John McCallf2370c92010-01-06 05:24:50 +00003728 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
3729 IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
3730 return IntRange::join(L, R);
3731 }
3732
3733 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
3734 switch (UO->getOpcode()) {
3735 // Boolean-valued operations are white-listed.
John McCall2de56d12010-08-25 11:45:40 +00003736 case UO_LNot:
John McCallf2370c92010-01-06 05:24:50 +00003737 return IntRange::forBoolType();
3738
3739 // Operations with opaque sources are black-listed.
John McCall2de56d12010-08-25 11:45:40 +00003740 case UO_Deref:
3741 case UO_AddrOf: // should be impossible
John McCall1844a6e2010-11-10 23:38:19 +00003742 return IntRange::forValueOfType(C, E->getType());
John McCallf2370c92010-01-06 05:24:50 +00003743
3744 default:
3745 return GetExprRange(C, UO->getSubExpr(), MaxWidth);
3746 }
3747 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00003748
3749 if (dyn_cast<OffsetOfExpr>(E)) {
John McCall1844a6e2010-11-10 23:38:19 +00003750 IntRange::forValueOfType(C, E->getType());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00003751 }
John McCallf2370c92010-01-06 05:24:50 +00003752
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003753 if (FieldDecl *BitField = E->getBitField())
3754 return IntRange(BitField->getBitWidthValue(C),
Douglas Gregor5e9ebb32011-05-21 16:28:01 +00003755 BitField->getType()->isUnsignedIntegerOrEnumerationType());
John McCallf2370c92010-01-06 05:24:50 +00003756
John McCall1844a6e2010-11-10 23:38:19 +00003757 return IntRange::forValueOfType(C, E->getType());
John McCallf2370c92010-01-06 05:24:50 +00003758}
John McCall51313c32010-01-04 23:31:57 +00003759
Ted Kremenek0692a192012-01-31 05:37:37 +00003760static IntRange GetExprRange(ASTContext &C, Expr *E) {
John McCall323ed742010-05-06 08:58:33 +00003761 return GetExprRange(C, E, C.getIntWidth(E->getType()));
3762}
3763
John McCall51313c32010-01-04 23:31:57 +00003764/// Checks whether the given value, which currently has the given
3765/// source semantics, has the same value when coerced through the
3766/// target semantics.
Ted Kremenek0692a192012-01-31 05:37:37 +00003767static bool IsSameFloatAfterCast(const llvm::APFloat &value,
3768 const llvm::fltSemantics &Src,
3769 const llvm::fltSemantics &Tgt) {
John McCall51313c32010-01-04 23:31:57 +00003770 llvm::APFloat truncated = value;
3771
3772 bool ignored;
3773 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
3774 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
3775
3776 return truncated.bitwiseIsEqual(value);
3777}
3778
3779/// Checks whether the given value, which currently has the given
3780/// source semantics, has the same value when coerced through the
3781/// target semantics.
3782///
3783/// The value might be a vector of floats (or a complex number).
Ted Kremenek0692a192012-01-31 05:37:37 +00003784static bool IsSameFloatAfterCast(const APValue &value,
3785 const llvm::fltSemantics &Src,
3786 const llvm::fltSemantics &Tgt) {
John McCall51313c32010-01-04 23:31:57 +00003787 if (value.isFloat())
3788 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
3789
3790 if (value.isVector()) {
3791 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
3792 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
3793 return false;
3794 return true;
3795 }
3796
3797 assert(value.isComplexFloat());
3798 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
3799 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
3800}
3801
Ted Kremenek0692a192012-01-31 05:37:37 +00003802static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
John McCall323ed742010-05-06 08:58:33 +00003803
Ted Kremeneke3b159c2010-09-23 21:43:44 +00003804static bool IsZero(Sema &S, Expr *E) {
3805 // Suppress cases where we are comparing against an enum constant.
3806 if (const DeclRefExpr *DR =
3807 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
3808 if (isa<EnumConstantDecl>(DR->getDecl()))
3809 return false;
3810
3811 // Suppress cases where the '0' value is expanded from a macro.
3812 if (E->getLocStart().isMacroID())
3813 return false;
3814
John McCall323ed742010-05-06 08:58:33 +00003815 llvm::APSInt Value;
3816 return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
3817}
3818
John McCall372e1032010-10-06 00:25:24 +00003819static bool HasEnumType(Expr *E) {
3820 // Strip off implicit integral promotions.
3821 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Argyrios Kyrtzidis63b57ae2010-10-07 21:52:18 +00003822 if (ICE->getCastKind() != CK_IntegralCast &&
3823 ICE->getCastKind() != CK_NoOp)
John McCall372e1032010-10-06 00:25:24 +00003824 break;
Argyrios Kyrtzidis63b57ae2010-10-07 21:52:18 +00003825 E = ICE->getSubExpr();
John McCall372e1032010-10-06 00:25:24 +00003826 }
3827
3828 return E->getType()->isEnumeralType();
3829}
3830
Ted Kremenek0692a192012-01-31 05:37:37 +00003831static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00003832 BinaryOperatorKind op = E->getOpcode();
Douglas Gregor14af91a2010-12-21 07:22:56 +00003833 if (E->isValueDependent())
3834 return;
3835
John McCall2de56d12010-08-25 11:45:40 +00003836 if (op == BO_LT && IsZero(S, E->getRHS())) {
John McCall323ed742010-05-06 08:58:33 +00003837 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00003838 << "< 0" << "false" << HasEnumType(E->getLHS())
John McCall323ed742010-05-06 08:58:33 +00003839 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00003840 } else if (op == BO_GE && IsZero(S, E->getRHS())) {
John McCall323ed742010-05-06 08:58:33 +00003841 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00003842 << ">= 0" << "true" << HasEnumType(E->getLHS())
John McCall323ed742010-05-06 08:58:33 +00003843 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00003844 } else if (op == BO_GT && IsZero(S, E->getLHS())) {
John McCall323ed742010-05-06 08:58:33 +00003845 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00003846 << "0 >" << "false" << HasEnumType(E->getRHS())
John McCall323ed742010-05-06 08:58:33 +00003847 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00003848 } else if (op == BO_LE && IsZero(S, E->getLHS())) {
John McCall323ed742010-05-06 08:58:33 +00003849 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00003850 << "0 <=" << "true" << HasEnumType(E->getRHS())
John McCall323ed742010-05-06 08:58:33 +00003851 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
3852 }
3853}
3854
3855/// Analyze the operands of the given comparison. Implements the
3856/// fallback case from AnalyzeComparison.
Ted Kremenek0692a192012-01-31 05:37:37 +00003857static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
John McCallb4eb64d2010-10-08 02:01:28 +00003858 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
3859 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
John McCall323ed742010-05-06 08:58:33 +00003860}
John McCall51313c32010-01-04 23:31:57 +00003861
John McCallba26e582010-01-04 23:21:16 +00003862/// \brief Implements -Wsign-compare.
3863///
Richard Trieudd225092011-09-15 21:56:47 +00003864/// \param E the binary operator to check for warnings
Ted Kremenek0692a192012-01-31 05:37:37 +00003865static void AnalyzeComparison(Sema &S, BinaryOperator *E) {
John McCall323ed742010-05-06 08:58:33 +00003866 // The type the comparison is being performed in.
3867 QualType T = E->getLHS()->getType();
3868 assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())
3869 && "comparison with mismatched types");
John McCallba26e582010-01-04 23:21:16 +00003870
John McCall323ed742010-05-06 08:58:33 +00003871 // We don't do anything special if this isn't an unsigned integral
3872 // comparison: we're only interested in integral comparisons, and
3873 // signed comparisons only happen in cases we don't care to warn about.
Douglas Gregor3e026e32011-02-19 22:34:59 +00003874 //
3875 // We also don't care about value-dependent expressions or expressions
3876 // whose result is a constant.
3877 if (!T->hasUnsignedIntegerRepresentation()
3878 || E->isValueDependent() || E->isIntegerConstantExpr(S.Context))
John McCall323ed742010-05-06 08:58:33 +00003879 return AnalyzeImpConvsInComparison(S, E);
John McCallf2370c92010-01-06 05:24:50 +00003880
Richard Trieudd225092011-09-15 21:56:47 +00003881 Expr *LHS = E->getLHS()->IgnoreParenImpCasts();
3882 Expr *RHS = E->getRHS()->IgnoreParenImpCasts();
John McCallba26e582010-01-04 23:21:16 +00003883
John McCall323ed742010-05-06 08:58:33 +00003884 // Check to see if one of the (unmodified) operands is of different
3885 // signedness.
3886 Expr *signedOperand, *unsignedOperand;
Richard Trieudd225092011-09-15 21:56:47 +00003887 if (LHS->getType()->hasSignedIntegerRepresentation()) {
3888 assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
John McCall323ed742010-05-06 08:58:33 +00003889 "unsigned comparison between two signed integer expressions?");
Richard Trieudd225092011-09-15 21:56:47 +00003890 signedOperand = LHS;
3891 unsignedOperand = RHS;
3892 } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
3893 signedOperand = RHS;
3894 unsignedOperand = LHS;
John McCallba26e582010-01-04 23:21:16 +00003895 } else {
John McCall323ed742010-05-06 08:58:33 +00003896 CheckTrivialUnsignedComparison(S, E);
3897 return AnalyzeImpConvsInComparison(S, E);
John McCallba26e582010-01-04 23:21:16 +00003898 }
3899
John McCall323ed742010-05-06 08:58:33 +00003900 // Otherwise, calculate the effective range of the signed operand.
3901 IntRange signedRange = GetExprRange(S.Context, signedOperand);
John McCallf2370c92010-01-06 05:24:50 +00003902
John McCall323ed742010-05-06 08:58:33 +00003903 // Go ahead and analyze implicit conversions in the operands. Note
3904 // that we skip the implicit conversions on both sides.
Richard Trieudd225092011-09-15 21:56:47 +00003905 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
3906 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
John McCallba26e582010-01-04 23:21:16 +00003907
John McCall323ed742010-05-06 08:58:33 +00003908 // If the signed range is non-negative, -Wsign-compare won't fire,
3909 // but we should still check for comparisons which are always true
3910 // or false.
3911 if (signedRange.NonNegative)
3912 return CheckTrivialUnsignedComparison(S, E);
John McCallba26e582010-01-04 23:21:16 +00003913
3914 // For (in)equality comparisons, if the unsigned operand is a
3915 // constant which cannot collide with a overflowed signed operand,
3916 // then reinterpreting the signed operand as unsigned will not
3917 // change the result of the comparison.
John McCall323ed742010-05-06 08:58:33 +00003918 if (E->isEqualityOp()) {
3919 unsigned comparisonWidth = S.Context.getIntWidth(T);
3920 IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
John McCallba26e582010-01-04 23:21:16 +00003921
John McCall323ed742010-05-06 08:58:33 +00003922 // We should never be unable to prove that the unsigned operand is
3923 // non-negative.
3924 assert(unsignedRange.NonNegative && "unsigned range includes negative?");
3925
3926 if (unsignedRange.Width < comparisonWidth)
3927 return;
3928 }
3929
3930 S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison)
Richard Trieudd225092011-09-15 21:56:47 +00003931 << LHS->getType() << RHS->getType()
3932 << LHS->getSourceRange() << RHS->getSourceRange();
John McCallba26e582010-01-04 23:21:16 +00003933}
3934
John McCall15d7d122010-11-11 03:21:53 +00003935/// Analyzes an attempt to assign the given value to a bitfield.
3936///
3937/// Returns true if there was something fishy about the attempt.
Ted Kremenek0692a192012-01-31 05:37:37 +00003938static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
3939 SourceLocation InitLoc) {
John McCall15d7d122010-11-11 03:21:53 +00003940 assert(Bitfield->isBitField());
3941 if (Bitfield->isInvalidDecl())
3942 return false;
3943
John McCall91b60142010-11-11 05:33:51 +00003944 // White-list bool bitfields.
3945 if (Bitfield->getType()->isBooleanType())
3946 return false;
3947
Douglas Gregor46ff3032011-02-04 13:09:01 +00003948 // Ignore value- or type-dependent expressions.
3949 if (Bitfield->getBitWidth()->isValueDependent() ||
3950 Bitfield->getBitWidth()->isTypeDependent() ||
3951 Init->isValueDependent() ||
3952 Init->isTypeDependent())
3953 return false;
3954
John McCall15d7d122010-11-11 03:21:53 +00003955 Expr *OriginalInit = Init->IgnoreParenImpCasts();
3956
Richard Smith80d4b552011-12-28 19:48:30 +00003957 llvm::APSInt Value;
3958 if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects))
John McCall15d7d122010-11-11 03:21:53 +00003959 return false;
3960
John McCall15d7d122010-11-11 03:21:53 +00003961 unsigned OriginalWidth = Value.getBitWidth();
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003962 unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
John McCall15d7d122010-11-11 03:21:53 +00003963
3964 if (OriginalWidth <= FieldWidth)
3965 return false;
3966
Eli Friedman3a643af2012-01-26 23:11:39 +00003967 // Compute the value which the bitfield will contain.
Jay Foad9f71a8f2010-12-07 08:25:34 +00003968 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
Eli Friedman3a643af2012-01-26 23:11:39 +00003969 TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType());
John McCall15d7d122010-11-11 03:21:53 +00003970
Eli Friedman3a643af2012-01-26 23:11:39 +00003971 // Check whether the stored value is equal to the original value.
3972 TruncatedValue = TruncatedValue.extend(OriginalWidth);
John McCall15d7d122010-11-11 03:21:53 +00003973 if (Value == TruncatedValue)
3974 return false;
3975
Eli Friedman3a643af2012-01-26 23:11:39 +00003976 // Special-case bitfields of width 1: booleans are naturally 0/1, and
Eli Friedman34ff0622012-02-02 00:40:20 +00003977 // therefore don't strictly fit into a signed bitfield of width 1.
3978 if (FieldWidth == 1 && Value == 1)
Eli Friedman3a643af2012-01-26 23:11:39 +00003979 return false;
3980
John McCall15d7d122010-11-11 03:21:53 +00003981 std::string PrettyValue = Value.toString(10);
3982 std::string PrettyTrunc = TruncatedValue.toString(10);
3983
3984 S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
3985 << PrettyValue << PrettyTrunc << OriginalInit->getType()
3986 << Init->getSourceRange();
3987
3988 return true;
3989}
3990
John McCallbeb22aa2010-11-09 23:24:47 +00003991/// Analyze the given simple or compound assignment for warning-worthy
3992/// operations.
Ted Kremenek0692a192012-01-31 05:37:37 +00003993static void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
John McCallbeb22aa2010-11-09 23:24:47 +00003994 // Just recurse on the LHS.
3995 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
3996
3997 // We want to recurse on the RHS as normal unless we're assigning to
3998 // a bitfield.
3999 if (FieldDecl *Bitfield = E->getLHS()->getBitField()) {
John McCall15d7d122010-11-11 03:21:53 +00004000 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
4001 E->getOperatorLoc())) {
4002 // Recurse, ignoring any implicit conversions on the RHS.
4003 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
4004 E->getOperatorLoc());
John McCallbeb22aa2010-11-09 23:24:47 +00004005 }
4006 }
4007
4008 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
4009}
4010
John McCall51313c32010-01-04 23:31:57 +00004011/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
Ted Kremenek0692a192012-01-31 05:37:37 +00004012static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
Anna Zaksc36bedc2012-02-01 19:08:57 +00004013 SourceLocation CContext, unsigned diag,
4014 bool pruneControlFlow = false) {
4015 if (pruneControlFlow) {
4016 S.DiagRuntimeBehavior(E->getExprLoc(), E,
4017 S.PDiag(diag)
4018 << SourceType << T << E->getSourceRange()
4019 << SourceRange(CContext));
4020 return;
4021 }
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00004022 S.Diag(E->getExprLoc(), diag)
4023 << SourceType << T << E->getSourceRange() << SourceRange(CContext);
4024}
4025
Chandler Carruthe1b02e02011-04-05 06:47:57 +00004026/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
Ted Kremenek0692a192012-01-31 05:37:37 +00004027static void DiagnoseImpCast(Sema &S, Expr *E, QualType T,
Anna Zaksc36bedc2012-02-01 19:08:57 +00004028 SourceLocation CContext, unsigned diag,
4029 bool pruneControlFlow = false) {
4030 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow);
Chandler Carruthe1b02e02011-04-05 06:47:57 +00004031}
4032
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004033/// Diagnose an implicit cast from a literal expression. Does not warn when the
4034/// cast wouldn't lose information.
Chandler Carruthf65076e2011-04-10 08:36:24 +00004035void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
4036 SourceLocation CContext) {
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004037 // Try to convert the literal exactly to an integer. If we can, don't warn.
Chandler Carruthf65076e2011-04-10 08:36:24 +00004038 bool isExact = false;
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004039 const llvm::APFloat &Value = FL->getValue();
Jeffrey Yasskin3e1ef782011-07-15 17:03:07 +00004040 llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
4041 T->hasUnsignedIntegerRepresentation());
4042 if (Value.convertToInteger(IntegerValue,
Chandler Carruthf65076e2011-04-10 08:36:24 +00004043 llvm::APFloat::rmTowardZero, &isExact)
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004044 == llvm::APFloat::opOK && isExact)
Chandler Carruthf65076e2011-04-10 08:36:24 +00004045 return;
4046
Matt Beaumont-Gay9ce63772011-10-14 15:36:25 +00004047 S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer)
4048 << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext);
Chandler Carruthf65076e2011-04-10 08:36:24 +00004049}
4050
John McCall091f23f2010-11-09 22:22:12 +00004051std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
4052 if (!Range.Width) return "0";
4053
4054 llvm::APSInt ValueInRange = Value;
4055 ValueInRange.setIsSigned(!Range.NonNegative);
Jay Foad9f71a8f2010-12-07 08:25:34 +00004056 ValueInRange = ValueInRange.trunc(Range.Width);
John McCall091f23f2010-11-09 22:22:12 +00004057 return ValueInRange.toString(10);
4058}
4059
John McCall323ed742010-05-06 08:58:33 +00004060void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
John McCallb4eb64d2010-10-08 02:01:28 +00004061 SourceLocation CC, bool *ICContext = 0) {
John McCall323ed742010-05-06 08:58:33 +00004062 if (E->isTypeDependent() || E->isValueDependent()) return;
John McCall51313c32010-01-04 23:31:57 +00004063
John McCall323ed742010-05-06 08:58:33 +00004064 const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
4065 const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
4066 if (Source == Target) return;
4067 if (Target->isDependentType()) return;
John McCall51313c32010-01-04 23:31:57 +00004068
Chandler Carruth108f7562011-07-26 05:40:03 +00004069 // If the conversion context location is invalid don't complain. We also
4070 // don't want to emit a warning if the issue occurs from the expansion of
4071 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
4072 // delay this check as long as possible. Once we detect we are in that
4073 // scenario, we just return.
Ted Kremenekef9ff882011-03-10 20:03:42 +00004074 if (CC.isInvalid())
John McCallb4eb64d2010-10-08 02:01:28 +00004075 return;
4076
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00004077 // Diagnose implicit casts to bool.
4078 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
4079 if (isa<StringLiteral>(E))
4080 // Warn on string literal to bool. Checks for string literals in logical
4081 // expressions, for instances, assert(0 && "error here"), is prevented
4082 // by a check in AnalyzeImplicitConversions().
4083 return DiagnoseImpCast(S, E, T, CC,
4084 diag::warn_impcast_string_literal_to_bool);
Lang Hamese14ca9f2011-12-05 20:49:50 +00004085 if (Source->isFunctionType()) {
4086 // Warn on function to bool. Checks free functions and static member
4087 // functions. Weakly imported functions are excluded from the check,
4088 // since it's common to test their value to check whether the linker
4089 // found a definition for them.
4090 ValueDecl *D = 0;
4091 if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) {
4092 D = R->getDecl();
4093 } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) {
4094 D = M->getMemberDecl();
4095 }
4096
4097 if (D && !D->isWeak()) {
Richard Trieu26b45d82011-12-06 04:48:01 +00004098 if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) {
4099 S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool)
4100 << F << E->getSourceRange() << SourceRange(CC);
David Blaikie2def7732011-12-09 21:42:37 +00004101 S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence)
4102 << FixItHint::CreateInsertion(E->getExprLoc(), "&");
4103 QualType ReturnType;
4104 UnresolvedSet<4> NonTemplateOverloads;
4105 S.isExprCallable(*E, ReturnType, NonTemplateOverloads);
4106 if (!ReturnType.isNull()
4107 && ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
4108 S.Diag(E->getExprLoc(), diag::note_function_to_bool_call)
4109 << FixItHint::CreateInsertion(
4110 S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()");
Richard Trieu26b45d82011-12-06 04:48:01 +00004111 return;
4112 }
Lang Hamese14ca9f2011-12-05 20:49:50 +00004113 }
4114 }
David Blaikiee37cdc42011-09-29 04:06:47 +00004115 return; // Other casts to bool are not checked.
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00004116 }
John McCall51313c32010-01-04 23:31:57 +00004117
4118 // Strip vector types.
4119 if (isa<VectorType>(Source)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00004120 if (!isa<VectorType>(Target)) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00004121 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00004122 return;
John McCallb4eb64d2010-10-08 02:01:28 +00004123 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
Ted Kremenekef9ff882011-03-10 20:03:42 +00004124 }
Chris Lattnerb792b302011-06-14 04:51:15 +00004125
4126 // If the vector cast is cast between two vectors of the same size, it is
4127 // a bitcast, not a conversion.
4128 if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
4129 return;
John McCall51313c32010-01-04 23:31:57 +00004130
4131 Source = cast<VectorType>(Source)->getElementType().getTypePtr();
4132 Target = cast<VectorType>(Target)->getElementType().getTypePtr();
4133 }
4134
4135 // Strip complex types.
4136 if (isa<ComplexType>(Source)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00004137 if (!isa<ComplexType>(Target)) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00004138 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00004139 return;
4140
John McCallb4eb64d2010-10-08 02:01:28 +00004141 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar);
Ted Kremenekef9ff882011-03-10 20:03:42 +00004142 }
John McCall51313c32010-01-04 23:31:57 +00004143
4144 Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
4145 Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
4146 }
4147
4148 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
4149 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
4150
4151 // If the source is floating point...
4152 if (SourceBT && SourceBT->isFloatingPoint()) {
4153 // ...and the target is floating point...
4154 if (TargetBT && TargetBT->isFloatingPoint()) {
4155 // ...then warn if we're dropping FP rank.
4156
4157 // Builtin FP kinds are ordered by increasing FP rank.
4158 if (SourceBT->getKind() > TargetBT->getKind()) {
4159 // Don't warn about float constants that are precisely
4160 // representable in the target type.
4161 Expr::EvalResult result;
Richard Smith51f47082011-10-29 00:50:52 +00004162 if (E->EvaluateAsRValue(result, S.Context)) {
John McCall51313c32010-01-04 23:31:57 +00004163 // Value might be a float, a float vector, or a float complex.
4164 if (IsSameFloatAfterCast(result.Val,
John McCall323ed742010-05-06 08:58:33 +00004165 S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
4166 S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
John McCall51313c32010-01-04 23:31:57 +00004167 return;
4168 }
4169
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00004170 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00004171 return;
4172
John McCallb4eb64d2010-10-08 02:01:28 +00004173 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
John McCall51313c32010-01-04 23:31:57 +00004174 }
4175 return;
4176 }
4177
Ted Kremenekef9ff882011-03-10 20:03:42 +00004178 // If the target is integral, always warn.
Chandler Carrutha5b93322011-02-17 11:05:49 +00004179 if ((TargetBT && TargetBT->isInteger())) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00004180 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00004181 return;
4182
Chandler Carrutha5b93322011-02-17 11:05:49 +00004183 Expr *InnerE = E->IgnoreParenImpCasts();
Matt Beaumont-Gay634c8af2011-09-08 22:30:47 +00004184 // We also want to warn on, e.g., "int i = -1.234"
4185 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
4186 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
4187 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
4188
Chandler Carruthf65076e2011-04-10 08:36:24 +00004189 if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) {
4190 DiagnoseFloatingLiteralImpCast(S, FL, T, CC);
Chandler Carrutha5b93322011-02-17 11:05:49 +00004191 } else {
4192 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
4193 }
4194 }
John McCall51313c32010-01-04 23:31:57 +00004195
4196 return;
4197 }
4198
John McCallf2370c92010-01-06 05:24:50 +00004199 if (!Source->isIntegerType() || !Target->isIntegerType())
John McCall51313c32010-01-04 23:31:57 +00004200 return;
4201
Richard Trieu1838ca52011-05-29 19:59:02 +00004202 if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)
4203 == Expr::NPCK_GNUNull) && Target->isIntegerType()) {
David Blaikieb1360492012-03-16 20:30:12 +00004204 SourceLocation Loc = E->getSourceRange().getBegin();
4205 if (Loc.isMacroID())
4206 Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first;
4207 S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
4208 << T << Loc << clang::SourceRange(CC);
Richard Trieu1838ca52011-05-29 19:59:02 +00004209 return;
4210 }
4211
John McCall323ed742010-05-06 08:58:33 +00004212 IntRange SourceRange = GetExprRange(S.Context, E);
John McCall1844a6e2010-11-10 23:38:19 +00004213 IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
John McCallf2370c92010-01-06 05:24:50 +00004214
4215 if (SourceRange.Width > TargetRange.Width) {
John McCall091f23f2010-11-09 22:22:12 +00004216 // If the source is a constant, use a default-on diagnostic.
4217 // TODO: this should happen for bitfield stores, too.
4218 llvm::APSInt Value(32);
4219 if (E->isIntegerConstantExpr(Value, S.Context)) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00004220 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00004221 return;
4222
John McCall091f23f2010-11-09 22:22:12 +00004223 std::string PrettySourceValue = Value.toString(10);
4224 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
4225
Ted Kremenek5e745da2011-10-22 02:37:33 +00004226 S.DiagRuntimeBehavior(E->getExprLoc(), E,
4227 S.PDiag(diag::warn_impcast_integer_precision_constant)
4228 << PrettySourceValue << PrettyTargetValue
4229 << E->getType() << T << E->getSourceRange()
4230 << clang::SourceRange(CC));
John McCall091f23f2010-11-09 22:22:12 +00004231 return;
4232 }
4233
Chris Lattnerb792b302011-06-14 04:51:15 +00004234 // People want to build with -Wshorten-64-to-32 and not -Wconversion.
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00004235 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00004236 return;
4237
David Blaikie37050842012-04-12 22:40:54 +00004238 if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64)
Anna Zaksc36bedc2012-02-01 19:08:57 +00004239 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32,
4240 /* pruneControlFlow */ true);
John McCallb4eb64d2010-10-08 02:01:28 +00004241 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
John McCall323ed742010-05-06 08:58:33 +00004242 }
4243
4244 if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
4245 (!TargetRange.NonNegative && SourceRange.NonNegative &&
4246 SourceRange.Width == TargetRange.Width)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00004247
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00004248 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00004249 return;
4250
John McCall323ed742010-05-06 08:58:33 +00004251 unsigned DiagID = diag::warn_impcast_integer_sign;
4252
4253 // Traditionally, gcc has warned about this under -Wsign-compare.
4254 // We also want to warn about it in -Wconversion.
4255 // So if -Wconversion is off, use a completely identical diagnostic
4256 // in the sign-compare group.
4257 // The conditional-checking code will
4258 if (ICContext) {
4259 DiagID = diag::warn_impcast_integer_sign_conditional;
4260 *ICContext = true;
4261 }
4262
John McCallb4eb64d2010-10-08 02:01:28 +00004263 return DiagnoseImpCast(S, E, T, CC, DiagID);
John McCall51313c32010-01-04 23:31:57 +00004264 }
4265
Douglas Gregor284cc8d2011-02-22 02:45:07 +00004266 // Diagnose conversions between different enumeration types.
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00004267 // In C, we pretend that the type of an EnumConstantDecl is its enumeration
4268 // type, to give us better diagnostics.
4269 QualType SourceType = E->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00004270 if (!S.getLangOpts().CPlusPlus) {
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00004271 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
4272 if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
4273 EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
4274 SourceType = S.Context.getTypeDeclType(Enum);
4275 Source = S.Context.getCanonicalType(SourceType).getTypePtr();
4276 }
4277 }
4278
Douglas Gregor284cc8d2011-02-22 02:45:07 +00004279 if (const EnumType *SourceEnum = Source->getAs<EnumType>())
4280 if (const EnumType *TargetEnum = Target->getAs<EnumType>())
4281 if ((SourceEnum->getDecl()->getIdentifier() ||
Richard Smith162e1c12011-04-15 14:24:37 +00004282 SourceEnum->getDecl()->getTypedefNameForAnonDecl()) &&
Douglas Gregor284cc8d2011-02-22 02:45:07 +00004283 (TargetEnum->getDecl()->getIdentifier() ||
Richard Smith162e1c12011-04-15 14:24:37 +00004284 TargetEnum->getDecl()->getTypedefNameForAnonDecl()) &&
Ted Kremenekef9ff882011-03-10 20:03:42 +00004285 SourceEnum != TargetEnum) {
Matt Beaumont-Gayd87a0cd2012-01-06 22:43:58 +00004286 if (S.SourceMgr.isInSystemMacro(CC))
Ted Kremenekef9ff882011-03-10 20:03:42 +00004287 return;
4288
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00004289 return DiagnoseImpCast(S, E, SourceType, T, CC,
Douglas Gregor284cc8d2011-02-22 02:45:07 +00004290 diag::warn_impcast_different_enum_types);
Ted Kremenekef9ff882011-03-10 20:03:42 +00004291 }
Douglas Gregor284cc8d2011-02-22 02:45:07 +00004292
John McCall51313c32010-01-04 23:31:57 +00004293 return;
4294}
4295
John McCall323ed742010-05-06 08:58:33 +00004296void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T);
4297
4298void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
John McCallb4eb64d2010-10-08 02:01:28 +00004299 SourceLocation CC, bool &ICContext) {
John McCall323ed742010-05-06 08:58:33 +00004300 E = E->IgnoreParenImpCasts();
4301
4302 if (isa<ConditionalOperator>(E))
4303 return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T);
4304
John McCallb4eb64d2010-10-08 02:01:28 +00004305 AnalyzeImplicitConversions(S, E, CC);
John McCall323ed742010-05-06 08:58:33 +00004306 if (E->getType() != T)
John McCallb4eb64d2010-10-08 02:01:28 +00004307 return CheckImplicitConversion(S, E, T, CC, &ICContext);
John McCall323ed742010-05-06 08:58:33 +00004308 return;
4309}
4310
4311void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) {
John McCallb4eb64d2010-10-08 02:01:28 +00004312 SourceLocation CC = E->getQuestionLoc();
4313
4314 AnalyzeImplicitConversions(S, E->getCond(), CC);
John McCall323ed742010-05-06 08:58:33 +00004315
4316 bool Suspicious = false;
John McCallb4eb64d2010-10-08 02:01:28 +00004317 CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious);
4318 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
John McCall323ed742010-05-06 08:58:33 +00004319
4320 // If -Wconversion would have warned about either of the candidates
4321 // for a signedness conversion to the context type...
4322 if (!Suspicious) return;
4323
4324 // ...but it's currently ignored...
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00004325 if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional,
4326 CC))
John McCall323ed742010-05-06 08:58:33 +00004327 return;
4328
John McCall323ed742010-05-06 08:58:33 +00004329 // ...then check whether it would have warned about either of the
4330 // candidates for a signedness conversion to the condition type.
Richard Trieu52541612011-07-21 02:46:28 +00004331 if (E->getType() == T) return;
4332
4333 Suspicious = false;
4334 CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
4335 E->getType(), CC, &Suspicious);
4336 if (!Suspicious)
4337 CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
John McCallb4eb64d2010-10-08 02:01:28 +00004338 E->getType(), CC, &Suspicious);
John McCall323ed742010-05-06 08:58:33 +00004339}
4340
4341/// AnalyzeImplicitConversions - Find and report any interesting
4342/// implicit conversions in the given expression. There are a couple
4343/// of competing diagnostics here, -Wconversion and -Wsign-compare.
John McCallb4eb64d2010-10-08 02:01:28 +00004344void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) {
John McCall323ed742010-05-06 08:58:33 +00004345 QualType T = OrigE->getType();
4346 Expr *E = OrigE->IgnoreParenImpCasts();
4347
Douglas Gregorf8b6e152011-10-10 17:38:18 +00004348 if (E->isTypeDependent() || E->isValueDependent())
4349 return;
4350
John McCall323ed742010-05-06 08:58:33 +00004351 // For conditional operators, we analyze the arguments as if they
4352 // were being fed directly into the output.
4353 if (isa<ConditionalOperator>(E)) {
4354 ConditionalOperator *CO = cast<ConditionalOperator>(E);
4355 CheckConditionalOperator(S, CO, T);
4356 return;
4357 }
4358
4359 // Go ahead and check any implicit conversions we might have skipped.
4360 // The non-canonical typecheck is just an optimization;
4361 // CheckImplicitConversion will filter out dead implicit conversions.
4362 if (E->getType() != T)
John McCallb4eb64d2010-10-08 02:01:28 +00004363 CheckImplicitConversion(S, E, T, CC);
John McCall323ed742010-05-06 08:58:33 +00004364
4365 // Now continue drilling into this expression.
4366
4367 // Skip past explicit casts.
4368 if (isa<ExplicitCastExpr>(E)) {
4369 E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
John McCallb4eb64d2010-10-08 02:01:28 +00004370 return AnalyzeImplicitConversions(S, E, CC);
John McCall323ed742010-05-06 08:58:33 +00004371 }
4372
John McCallbeb22aa2010-11-09 23:24:47 +00004373 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
4374 // Do a somewhat different check with comparison operators.
4375 if (BO->isComparisonOp())
4376 return AnalyzeComparison(S, BO);
4377
Eli Friedman0fa06382012-01-26 23:34:06 +00004378 // And with simple assignments.
4379 if (BO->getOpcode() == BO_Assign)
John McCallbeb22aa2010-11-09 23:24:47 +00004380 return AnalyzeAssignment(S, BO);
4381 }
John McCall323ed742010-05-06 08:58:33 +00004382
4383 // These break the otherwise-useful invariant below. Fortunately,
4384 // we don't really need to recurse into them, because any internal
4385 // expressions should have been analyzed already when they were
4386 // built into statements.
4387 if (isa<StmtExpr>(E)) return;
4388
4389 // Don't descend into unevaluated contexts.
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004390 if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
John McCall323ed742010-05-06 08:58:33 +00004391
4392 // Now just recurse over the expression's children.
John McCallb4eb64d2010-10-08 02:01:28 +00004393 CC = E->getExprLoc();
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00004394 BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
4395 bool IsLogicalOperator = BO && BO->isLogicalOp();
4396 for (Stmt::child_range I = E->children(); I; ++I) {
Douglas Gregor54042f12012-02-09 10:18:50 +00004397 Expr *ChildExpr = dyn_cast_or_null<Expr>(*I);
Douglas Gregor503384f2012-02-09 00:47:04 +00004398 if (!ChildExpr)
4399 continue;
4400
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00004401 if (IsLogicalOperator &&
4402 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
4403 // Ignore checking string literals that are in logical operators.
4404 continue;
4405 AnalyzeImplicitConversions(S, ChildExpr, CC);
4406 }
John McCall323ed742010-05-06 08:58:33 +00004407}
4408
4409} // end anonymous namespace
4410
4411/// Diagnoses "dangerous" implicit conversions within the given
4412/// expression (which is a full expression). Implements -Wconversion
4413/// and -Wsign-compare.
John McCallb4eb64d2010-10-08 02:01:28 +00004414///
4415/// \param CC the "context" location of the implicit conversion, i.e.
4416/// the most location of the syntactic entity requiring the implicit
4417/// conversion
4418void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
John McCall323ed742010-05-06 08:58:33 +00004419 // Don't diagnose in unevaluated contexts.
4420 if (ExprEvalContexts.back().Context == Sema::Unevaluated)
4421 return;
4422
4423 // Don't diagnose for value- or type-dependent expressions.
4424 if (E->isTypeDependent() || E->isValueDependent())
4425 return;
4426
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004427 // Check for array bounds violations in cases where the check isn't triggered
4428 // elsewhere for other Expr types (like BinaryOperators), e.g. when an
4429 // ArraySubscriptExpr is on the RHS of a variable initialization.
4430 CheckArrayAccess(E);
4431
John McCallb4eb64d2010-10-08 02:01:28 +00004432 // This is not the right CC for (e.g.) a variable initialization.
4433 AnalyzeImplicitConversions(*this, E, CC);
John McCall323ed742010-05-06 08:58:33 +00004434}
4435
John McCall15d7d122010-11-11 03:21:53 +00004436void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
4437 FieldDecl *BitField,
4438 Expr *Init) {
4439 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
4440}
4441
Mike Stumpf8c49212010-01-21 03:59:47 +00004442/// CheckParmsForFunctionDef - Check that the parameters of the given
4443/// function are appropriate for the definition of a function. This
4444/// takes care of any checks that cannot be performed on the
4445/// declaration itself, e.g., that the types of each of the function
4446/// parameters are complete.
Douglas Gregor82aa7132010-11-01 18:37:59 +00004447bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd,
4448 bool CheckParameterNames) {
Mike Stumpf8c49212010-01-21 03:59:47 +00004449 bool HasInvalidParm = false;
Douglas Gregor82aa7132010-11-01 18:37:59 +00004450 for (; P != PEnd; ++P) {
4451 ParmVarDecl *Param = *P;
4452
Mike Stumpf8c49212010-01-21 03:59:47 +00004453 // C99 6.7.5.3p4: the parameters in a parameter type list in a
4454 // function declarator that is part of a function definition of
4455 // that function shall not have incomplete type.
4456 //
4457 // This is also C++ [dcl.fct]p6.
4458 if (!Param->isInvalidDecl() &&
4459 RequireCompleteType(Param->getLocation(), Param->getType(),
4460 diag::err_typecheck_decl_incomplete_type)) {
4461 Param->setInvalidDecl();
4462 HasInvalidParm = true;
4463 }
4464
4465 // C99 6.9.1p5: If the declarator includes a parameter type list, the
4466 // declaration of each parameter shall include an identifier.
Douglas Gregor82aa7132010-11-01 18:37:59 +00004467 if (CheckParameterNames &&
4468 Param->getIdentifier() == 0 &&
Mike Stumpf8c49212010-01-21 03:59:47 +00004469 !Param->isImplicit() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00004470 !getLangOpts().CPlusPlus)
Mike Stumpf8c49212010-01-21 03:59:47 +00004471 Diag(Param->getLocation(), diag::err_parameter_name_omitted);
Sam Weinigd17e3402010-02-01 05:02:49 +00004472
4473 // C99 6.7.5.3p12:
4474 // If the function declarator is not part of a definition of that
4475 // function, parameters may have incomplete type and may use the [*]
4476 // notation in their sequences of declarator specifiers to specify
4477 // variable length array types.
4478 QualType PType = Param->getOriginalType();
4479 if (const ArrayType *AT = Context.getAsArrayType(PType)) {
4480 if (AT->getSizeModifier() == ArrayType::Star) {
4481 // FIXME: This diagnosic should point the the '[*]' if source-location
4482 // information is added for it.
4483 Diag(Param->getLocation(), diag::err_array_star_in_function_definition);
4484 }
4485 }
Mike Stumpf8c49212010-01-21 03:59:47 +00004486 }
4487
4488 return HasInvalidParm;
4489}
John McCallb7f4ffe2010-08-12 21:44:57 +00004490
4491/// CheckCastAlign - Implements -Wcast-align, which warns when a
4492/// pointer cast increases the alignment requirements.
4493void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
4494 // This is actually a lot of work to potentially be doing on every
4495 // cast; don't do it if we're ignoring -Wcast_align (as is the default).
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00004496 if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align,
4497 TRange.getBegin())
David Blaikied6471f72011-09-25 23:23:43 +00004498 == DiagnosticsEngine::Ignored)
John McCallb7f4ffe2010-08-12 21:44:57 +00004499 return;
4500
4501 // Ignore dependent types.
4502 if (T->isDependentType() || Op->getType()->isDependentType())
4503 return;
4504
4505 // Require that the destination be a pointer type.
4506 const PointerType *DestPtr = T->getAs<PointerType>();
4507 if (!DestPtr) return;
4508
4509 // If the destination has alignment 1, we're done.
4510 QualType DestPointee = DestPtr->getPointeeType();
4511 if (DestPointee->isIncompleteType()) return;
4512 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
4513 if (DestAlign.isOne()) return;
4514
4515 // Require that the source be a pointer type.
4516 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
4517 if (!SrcPtr) return;
4518 QualType SrcPointee = SrcPtr->getPointeeType();
4519
4520 // Whitelist casts from cv void*. We already implicitly
4521 // whitelisted casts to cv void*, since they have alignment 1.
4522 // Also whitelist casts involving incomplete types, which implicitly
4523 // includes 'void'.
4524 if (SrcPointee->isIncompleteType()) return;
4525
4526 CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
4527 if (SrcAlign >= DestAlign) return;
4528
4529 Diag(TRange.getBegin(), diag::warn_cast_align)
4530 << Op->getType() << T
4531 << static_cast<unsigned>(SrcAlign.getQuantity())
4532 << static_cast<unsigned>(DestAlign.getQuantity())
4533 << TRange << Op->getSourceRange();
4534}
4535
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004536static const Type* getElementType(const Expr *BaseExpr) {
4537 const Type* EltType = BaseExpr->getType().getTypePtr();
4538 if (EltType->isAnyPointerType())
4539 return EltType->getPointeeType().getTypePtr();
4540 else if (EltType->isArrayType())
4541 return EltType->getBaseElementTypeUnsafe();
4542 return EltType;
4543}
4544
Chandler Carruthc2684342011-08-05 09:10:50 +00004545/// \brief Check whether this array fits the idiom of a size-one tail padded
4546/// array member of a struct.
4547///
4548/// We avoid emitting out-of-bounds access warnings for such arrays as they are
4549/// commonly used to emulate flexible arrays in C89 code.
4550static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
4551 const NamedDecl *ND) {
4552 if (Size != 1 || !ND) return false;
4553
4554 const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
4555 if (!FD) return false;
4556
4557 // Don't consider sizes resulting from macro expansions or template argument
4558 // substitution to form C89 tail-padded arrays.
4559 ConstantArrayTypeLoc TL =
4560 cast<ConstantArrayTypeLoc>(FD->getTypeSourceInfo()->getTypeLoc());
4561 const Expr *SizeExpr = dyn_cast<IntegerLiteral>(TL.getSizeExpr());
4562 if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
4563 return false;
4564
4565 const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
Matt Beaumont-Gay381711c2011-11-29 22:43:53 +00004566 if (!RD) return false;
4567 if (RD->isUnion()) return false;
4568 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
4569 if (!CRD->isStandardLayout()) return false;
4570 }
Chandler Carruthc2684342011-08-05 09:10:50 +00004571
Benjamin Kramer22d4fed2011-08-06 03:04:42 +00004572 // See if this is the last field decl in the record.
4573 const Decl *D = FD;
4574 while ((D = D->getNextDeclInContext()))
4575 if (isa<FieldDecl>(D))
4576 return false;
4577 return true;
Chandler Carruthc2684342011-08-05 09:10:50 +00004578}
4579
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004580void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00004581 const ArraySubscriptExpr *ASE,
Richard Smith25b009a2011-12-16 19:31:14 +00004582 bool AllowOnePastEnd, bool IndexNegated) {
Eli Friedman92b670e2012-02-27 21:21:40 +00004583 IndexExpr = IndexExpr->IgnoreParenImpCasts();
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00004584 if (IndexExpr->isValueDependent())
4585 return;
4586
Matt Beaumont-Gay8ef8f432011-12-12 22:35:02 +00004587 const Type *EffectiveType = getElementType(BaseExpr);
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004588 BaseExpr = BaseExpr->IgnoreParenCasts();
Chandler Carruth34064582011-02-17 20:55:08 +00004589 const ConstantArrayType *ArrayTy =
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004590 Context.getAsConstantArrayType(BaseExpr->getType());
Chandler Carruth34064582011-02-17 20:55:08 +00004591 if (!ArrayTy)
Ted Kremeneka0125d82011-02-16 01:57:07 +00004592 return;
Chandler Carruth35001ca2011-02-17 21:10:52 +00004593
Chandler Carruth34064582011-02-17 20:55:08 +00004594 llvm::APSInt index;
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00004595 if (!IndexExpr->EvaluateAsInt(index, Context))
Ted Kremeneka0125d82011-02-16 01:57:07 +00004596 return;
Richard Smith25b009a2011-12-16 19:31:14 +00004597 if (IndexNegated)
4598 index = -index;
Ted Kremenek8fd0a5d2011-02-16 04:01:44 +00004599
Chandler Carruthba447122011-08-05 08:07:29 +00004600 const NamedDecl *ND = NULL;
Chandler Carruthba447122011-08-05 08:07:29 +00004601 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
4602 ND = dyn_cast<NamedDecl>(DRE->getDecl());
Chandler Carruthc2684342011-08-05 09:10:50 +00004603 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
Chandler Carruthba447122011-08-05 08:07:29 +00004604 ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
Chandler Carruthba447122011-08-05 08:07:29 +00004605
Ted Kremenek9e060ca2011-02-23 23:06:04 +00004606 if (index.isUnsigned() || !index.isNegative()) {
Ted Kremenek25b3b842011-02-18 02:27:00 +00004607 llvm::APInt size = ArrayTy->getSize();
Chandler Carruth35001ca2011-02-17 21:10:52 +00004608 if (!size.isStrictlyPositive())
4609 return;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004610
4611 const Type* BaseType = getElementType(BaseExpr);
Nico Weberde5998f2011-09-17 22:59:41 +00004612 if (BaseType != EffectiveType) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004613 // Make sure we're comparing apples to apples when comparing index to size
4614 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
4615 uint64_t array_typesize = Context.getTypeSize(BaseType);
Kaelyn Uhraind10f4bc2011-08-10 19:47:25 +00004616 // Handle ptrarith_typesize being zero, such as when casting to void*
Kaelyn Uhrain18f16972011-08-10 18:49:28 +00004617 if (!ptrarith_typesize) ptrarith_typesize = 1;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004618 if (ptrarith_typesize != array_typesize) {
4619 // There's a cast to a different size type involved
4620 uint64_t ratio = array_typesize / ptrarith_typesize;
4621 // TODO: Be smarter about handling cases where array_typesize is not a
4622 // multiple of ptrarith_typesize
4623 if (ptrarith_typesize * ratio == array_typesize)
4624 size *= llvm::APInt(size.getBitWidth(), ratio);
4625 }
4626 }
4627
Chandler Carruth34064582011-02-17 20:55:08 +00004628 if (size.getBitWidth() > index.getBitWidth())
Eli Friedman92b670e2012-02-27 21:21:40 +00004629 index = index.zext(size.getBitWidth());
Ted Kremenek25b3b842011-02-18 02:27:00 +00004630 else if (size.getBitWidth() < index.getBitWidth())
Eli Friedman92b670e2012-02-27 21:21:40 +00004631 size = size.zext(index.getBitWidth());
Ted Kremenek25b3b842011-02-18 02:27:00 +00004632
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004633 // For array subscripting the index must be less than size, but for pointer
4634 // arithmetic also allow the index (offset) to be equal to size since
4635 // computing the next address after the end of the array is legal and
4636 // commonly done e.g. in C++ iterators and range-based for loops.
Eli Friedman92b670e2012-02-27 21:21:40 +00004637 if (AllowOnePastEnd ? index.ule(size) : index.ult(size))
Chandler Carruthba447122011-08-05 08:07:29 +00004638 return;
4639
4640 // Also don't warn for arrays of size 1 which are members of some
4641 // structure. These are often used to approximate flexible arrays in C89
4642 // code.
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004643 if (IsTailPaddedMemberArray(*this, size, ND))
Ted Kremenek8fd0a5d2011-02-16 04:01:44 +00004644 return;
Chandler Carruth34064582011-02-17 20:55:08 +00004645
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00004646 // Suppress the warning if the subscript expression (as identified by the
4647 // ']' location) and the index expression are both from macro expansions
4648 // within a system header.
4649 if (ASE) {
4650 SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
4651 ASE->getRBracketLoc());
4652 if (SourceMgr.isInSystemHeader(RBracketLoc)) {
4653 SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
4654 IndexExpr->getLocStart());
4655 if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc))
4656 return;
4657 }
4658 }
4659
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004660 unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00004661 if (ASE)
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004662 DiagID = diag::warn_array_index_exceeds_bounds;
4663
4664 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
4665 PDiag(DiagID) << index.toString(10, true)
4666 << size.toString(10, true)
4667 << (unsigned)size.getLimitedValue(~0U)
4668 << IndexExpr->getSourceRange());
Chandler Carruth34064582011-02-17 20:55:08 +00004669 } else {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004670 unsigned DiagID = diag::warn_array_index_precedes_bounds;
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00004671 if (!ASE) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004672 DiagID = diag::warn_ptr_arith_precedes_bounds;
4673 if (index.isNegative()) index = -index;
4674 }
4675
4676 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
4677 PDiag(DiagID) << index.toString(10, true)
4678 << IndexExpr->getSourceRange());
Ted Kremeneka0125d82011-02-16 01:57:07 +00004679 }
Chandler Carruth35001ca2011-02-17 21:10:52 +00004680
Matt Beaumont-Gaycfbc5b52011-11-29 19:27:11 +00004681 if (!ND) {
4682 // Try harder to find a NamedDecl to point at in the note.
4683 while (const ArraySubscriptExpr *ASE =
4684 dyn_cast<ArraySubscriptExpr>(BaseExpr))
4685 BaseExpr = ASE->getBase()->IgnoreParenCasts();
4686 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
4687 ND = dyn_cast<NamedDecl>(DRE->getDecl());
4688 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
4689 ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
4690 }
4691
Chandler Carruth35001ca2011-02-17 21:10:52 +00004692 if (ND)
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004693 DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
4694 PDiag(diag::note_array_index_out_of_bounds)
4695 << ND->getDeclName());
Ted Kremeneka0125d82011-02-16 01:57:07 +00004696}
4697
Ted Kremenek3aea4da2011-03-01 18:41:00 +00004698void Sema::CheckArrayAccess(const Expr *expr) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004699 int AllowOnePastEnd = 0;
4700 while (expr) {
4701 expr = expr->IgnoreParenImpCasts();
Ted Kremenek3aea4da2011-03-01 18:41:00 +00004702 switch (expr->getStmtClass()) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004703 case Stmt::ArraySubscriptExprClass: {
4704 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
Matt Beaumont-Gay80fb7dd2011-12-14 16:02:15 +00004705 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE,
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004706 AllowOnePastEnd > 0);
Ted Kremenek3aea4da2011-03-01 18:41:00 +00004707 return;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00004708 }
4709 case Stmt::UnaryOperatorClass: {
4710 // Only unwrap the * and & unary operators
4711 const UnaryOperator *UO = cast<UnaryOperator>(expr);
4712 expr = UO->getSubExpr();
4713 switch (UO->getOpcode()) {
4714 case UO_AddrOf:
4715 AllowOnePastEnd++;
4716 break;
4717 case UO_Deref:
4718 AllowOnePastEnd--;
4719 break;
4720 default:
4721 return;
4722 }
4723 break;
4724 }
Ted Kremenek3aea4da2011-03-01 18:41:00 +00004725 case Stmt::ConditionalOperatorClass: {
4726 const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
4727 if (const Expr *lhs = cond->getLHS())
4728 CheckArrayAccess(lhs);
4729 if (const Expr *rhs = cond->getRHS())
4730 CheckArrayAccess(rhs);
4731 return;
4732 }
4733 default:
4734 return;
4735 }
Peter Collingbournef111d932011-04-15 00:35:48 +00004736 }
Ted Kremenek3aea4da2011-03-01 18:41:00 +00004737}
John McCallf85e1932011-06-15 23:02:42 +00004738
4739//===--- CHECK: Objective-C retain cycles ----------------------------------//
4740
4741namespace {
4742 struct RetainCycleOwner {
4743 RetainCycleOwner() : Variable(0), Indirect(false) {}
4744 VarDecl *Variable;
4745 SourceRange Range;
4746 SourceLocation Loc;
4747 bool Indirect;
4748
4749 void setLocsFrom(Expr *e) {
4750 Loc = e->getExprLoc();
4751 Range = e->getSourceRange();
4752 }
4753 };
4754}
4755
4756/// Consider whether capturing the given variable can possibly lead to
4757/// a retain cycle.
4758static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
4759 // In ARC, it's captured strongly iff the variable has __strong
4760 // lifetime. In MRR, it's captured strongly if the variable is
4761 // __block and has an appropriate type.
4762 if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
4763 return false;
4764
4765 owner.Variable = var;
4766 owner.setLocsFrom(ref);
4767 return true;
4768}
4769
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00004770static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) {
John McCallf85e1932011-06-15 23:02:42 +00004771 while (true) {
4772 e = e->IgnoreParens();
4773 if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
4774 switch (cast->getCastKind()) {
4775 case CK_BitCast:
4776 case CK_LValueBitCast:
4777 case CK_LValueToRValue:
John McCall33e56f32011-09-10 06:18:15 +00004778 case CK_ARCReclaimReturnedObject:
John McCallf85e1932011-06-15 23:02:42 +00004779 e = cast->getSubExpr();
4780 continue;
4781
John McCallf85e1932011-06-15 23:02:42 +00004782 default:
4783 return false;
4784 }
4785 }
4786
4787 if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
4788 ObjCIvarDecl *ivar = ref->getDecl();
4789 if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
4790 return false;
4791
4792 // Try to find a retain cycle in the base.
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00004793 if (!findRetainCycleOwner(S, ref->getBase(), owner))
John McCallf85e1932011-06-15 23:02:42 +00004794 return false;
4795
4796 if (ref->isFreeIvar()) owner.setLocsFrom(ref);
4797 owner.Indirect = true;
4798 return true;
4799 }
4800
4801 if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
4802 VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
4803 if (!var) return false;
4804 return considerVariable(var, ref, owner);
4805 }
4806
John McCallf85e1932011-06-15 23:02:42 +00004807 if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
4808 if (member->isArrow()) return false;
4809
4810 // Don't count this as an indirect ownership.
4811 e = member->getBase();
4812 continue;
4813 }
4814
John McCall4b9c2d22011-11-06 09:01:30 +00004815 if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
4816 // Only pay attention to pseudo-objects on property references.
4817 ObjCPropertyRefExpr *pre
4818 = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm()
4819 ->IgnoreParens());
4820 if (!pre) return false;
4821 if (pre->isImplicitProperty()) return false;
4822 ObjCPropertyDecl *property = pre->getExplicitProperty();
4823 if (!property->isRetaining() &&
4824 !(property->getPropertyIvarDecl() &&
4825 property->getPropertyIvarDecl()->getType()
4826 .getObjCLifetime() == Qualifiers::OCL_Strong))
4827 return false;
4828
4829 owner.Indirect = true;
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00004830 if (pre->isSuperReceiver()) {
4831 owner.Variable = S.getCurMethodDecl()->getSelfDecl();
4832 if (!owner.Variable)
4833 return false;
4834 owner.Loc = pre->getLocation();
4835 owner.Range = pre->getSourceRange();
4836 return true;
4837 }
John McCall4b9c2d22011-11-06 09:01:30 +00004838 e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase())
4839 ->getSourceExpr());
4840 continue;
4841 }
4842
John McCallf85e1932011-06-15 23:02:42 +00004843 // Array ivars?
4844
4845 return false;
4846 }
4847}
4848
4849namespace {
4850 struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
4851 FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
4852 : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
4853 Variable(variable), Capturer(0) {}
4854
4855 VarDecl *Variable;
4856 Expr *Capturer;
4857
4858 void VisitDeclRefExpr(DeclRefExpr *ref) {
4859 if (ref->getDecl() == Variable && !Capturer)
4860 Capturer = ref;
4861 }
4862
John McCallf85e1932011-06-15 23:02:42 +00004863 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
4864 if (Capturer) return;
4865 Visit(ref->getBase());
4866 if (Capturer && ref->isFreeIvar())
4867 Capturer = ref;
4868 }
4869
4870 void VisitBlockExpr(BlockExpr *block) {
4871 // Look inside nested blocks
4872 if (block->getBlockDecl()->capturesVariable(Variable))
4873 Visit(block->getBlockDecl()->getBody());
4874 }
4875 };
4876}
4877
4878/// Check whether the given argument is a block which captures a
4879/// variable.
4880static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
4881 assert(owner.Variable && owner.Loc.isValid());
4882
4883 e = e->IgnoreParenCasts();
4884 BlockExpr *block = dyn_cast<BlockExpr>(e);
4885 if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
4886 return 0;
4887
4888 FindCaptureVisitor visitor(S.Context, owner.Variable);
4889 visitor.Visit(block->getBlockDecl()->getBody());
4890 return visitor.Capturer;
4891}
4892
4893static void diagnoseRetainCycle(Sema &S, Expr *capturer,
4894 RetainCycleOwner &owner) {
4895 assert(capturer);
4896 assert(owner.Variable && owner.Loc.isValid());
4897
4898 S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
4899 << owner.Variable << capturer->getSourceRange();
4900 S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
4901 << owner.Indirect << owner.Range;
4902}
4903
4904/// Check for a keyword selector that starts with the word 'add' or
4905/// 'set'.
4906static bool isSetterLikeSelector(Selector sel) {
4907 if (sel.isUnarySelector()) return false;
4908
Chris Lattner5f9e2722011-07-23 10:55:15 +00004909 StringRef str = sel.getNameForSlot(0);
John McCallf85e1932011-06-15 23:02:42 +00004910 while (!str.empty() && str.front() == '_') str = str.substr(1);
Ted Kremenek968a0ee2011-12-01 00:59:21 +00004911 if (str.startswith("set"))
John McCallf85e1932011-06-15 23:02:42 +00004912 str = str.substr(3);
Ted Kremenek968a0ee2011-12-01 00:59:21 +00004913 else if (str.startswith("add")) {
4914 // Specially whitelist 'addOperationWithBlock:'.
4915 if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock"))
4916 return false;
4917 str = str.substr(3);
4918 }
John McCallf85e1932011-06-15 23:02:42 +00004919 else
4920 return false;
4921
4922 if (str.empty()) return true;
4923 return !islower(str.front());
4924}
4925
4926/// Check a message send to see if it's likely to cause a retain cycle.
4927void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
4928 // Only check instance methods whose selector looks like a setter.
4929 if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
4930 return;
4931
4932 // Try to find a variable that the receiver is strongly owned by.
4933 RetainCycleOwner owner;
4934 if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00004935 if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner))
John McCallf85e1932011-06-15 23:02:42 +00004936 return;
4937 } else {
4938 assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
4939 owner.Variable = getCurMethodDecl()->getSelfDecl();
4940 owner.Loc = msg->getSuperLoc();
4941 owner.Range = msg->getSuperLoc();
4942 }
4943
4944 // Check whether the receiver is captured by any of the arguments.
4945 for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i)
4946 if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner))
4947 return diagnoseRetainCycle(*this, capturer, owner);
4948}
4949
4950/// Check a property assign to see if it's likely to cause a retain cycle.
4951void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
4952 RetainCycleOwner owner;
Fariborz Jahanian6e6f93a2012-01-10 19:28:26 +00004953 if (!findRetainCycleOwner(*this, receiver, owner))
John McCallf85e1932011-06-15 23:02:42 +00004954 return;
4955
4956 if (Expr *capturer = findCapturingExpr(*this, argument, owner))
4957 diagnoseRetainCycle(*this, capturer, owner);
4958}
4959
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004960bool Sema::checkUnsafeAssigns(SourceLocation Loc,
John McCallf85e1932011-06-15 23:02:42 +00004961 QualType LHS, Expr *RHS) {
4962 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
4963 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004964 return false;
4965 // strip off any implicit cast added to get to the one arc-specific
4966 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
John McCall33e56f32011-09-10 06:18:15 +00004967 if (cast->getCastKind() == CK_ARCConsumeObject) {
John McCallf85e1932011-06-15 23:02:42 +00004968 Diag(Loc, diag::warn_arc_retained_assign)
4969 << (LT == Qualifiers::OCL_ExplicitNone)
4970 << RHS->getSourceRange();
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004971 return true;
4972 }
4973 RHS = cast->getSubExpr();
4974 }
4975 return false;
John McCallf85e1932011-06-15 23:02:42 +00004976}
4977
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004978void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
4979 Expr *LHS, Expr *RHS) {
Fariborz Jahanian87eaf722012-01-17 22:58:16 +00004980 QualType LHSType;
4981 // PropertyRef on LHS type need be directly obtained from
4982 // its declaration as it has a PsuedoType.
4983 ObjCPropertyRefExpr *PRE
4984 = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens());
4985 if (PRE && !PRE->isImplicitProperty()) {
4986 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
4987 if (PD)
4988 LHSType = PD->getType();
4989 }
4990
4991 if (LHSType.isNull())
4992 LHSType = LHS->getType();
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004993 if (checkUnsafeAssigns(Loc, LHSType, RHS))
4994 return;
4995 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
4996 // FIXME. Check for other life times.
4997 if (LT != Qualifiers::OCL_None)
4998 return;
4999
Fariborz Jahanian87eaf722012-01-17 22:58:16 +00005000 if (PRE) {
Fariborz Jahanian921c1432011-06-24 18:25:34 +00005001 if (PRE->isImplicitProperty())
5002 return;
5003 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
5004 if (!PD)
5005 return;
5006
5007 unsigned Attributes = PD->getPropertyAttributes();
Fariborz Jahanian87eaf722012-01-17 22:58:16 +00005008 if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) {
5009 // when 'assign' attribute was not explicitly specified
5010 // by user, ignore it and rely on property type itself
5011 // for lifetime info.
5012 unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten();
5013 if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) &&
5014 LHSType->isObjCRetainableType())
5015 return;
5016
Fariborz Jahanian921c1432011-06-24 18:25:34 +00005017 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
John McCall33e56f32011-09-10 06:18:15 +00005018 if (cast->getCastKind() == CK_ARCConsumeObject) {
Fariborz Jahanian921c1432011-06-24 18:25:34 +00005019 Diag(Loc, diag::warn_arc_retained_property_assign)
5020 << RHS->getSourceRange();
5021 return;
5022 }
5023 RHS = cast->getSubExpr();
5024 }
Fariborz Jahanian87eaf722012-01-17 22:58:16 +00005025 }
Fariborz Jahanian921c1432011-06-24 18:25:34 +00005026 }
5027}
Dmitri Gribenko625bb562012-02-14 22:14:32 +00005028
5029//===--- CHECK: Empty statement body (-Wempty-body) ---------------------===//
5030
5031namespace {
5032bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
5033 SourceLocation StmtLoc,
5034 const NullStmt *Body) {
5035 // Do not warn if the body is a macro that expands to nothing, e.g:
5036 //
5037 // #define CALL(x)
5038 // if (condition)
5039 // CALL(0);
5040 //
5041 if (Body->hasLeadingEmptyMacro())
5042 return false;
5043
5044 // Get line numbers of statement and body.
5045 bool StmtLineInvalid;
5046 unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc,
5047 &StmtLineInvalid);
5048 if (StmtLineInvalid)
5049 return false;
5050
5051 bool BodyLineInvalid;
5052 unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(),
5053 &BodyLineInvalid);
5054 if (BodyLineInvalid)
5055 return false;
5056
5057 // Warn if null statement and body are on the same line.
5058 if (StmtLine != BodyLine)
5059 return false;
5060
5061 return true;
5062}
5063} // Unnamed namespace
5064
5065void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc,
5066 const Stmt *Body,
5067 unsigned DiagID) {
5068 // Since this is a syntactic check, don't emit diagnostic for template
5069 // instantiations, this just adds noise.
5070 if (CurrentInstantiationScope)
5071 return;
5072
5073 // The body should be a null statement.
5074 const NullStmt *NBody = dyn_cast<NullStmt>(Body);
5075 if (!NBody)
5076 return;
5077
5078 // Do the usual checks.
5079 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
5080 return;
5081
5082 Diag(NBody->getSemiLoc(), DiagID);
5083 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
5084}
5085
5086void Sema::DiagnoseEmptyLoopBody(const Stmt *S,
5087 const Stmt *PossibleBody) {
5088 assert(!CurrentInstantiationScope); // Ensured by caller
5089
5090 SourceLocation StmtLoc;
5091 const Stmt *Body;
5092 unsigned DiagID;
5093 if (const ForStmt *FS = dyn_cast<ForStmt>(S)) {
5094 StmtLoc = FS->getRParenLoc();
5095 Body = FS->getBody();
5096 DiagID = diag::warn_empty_for_body;
5097 } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) {
5098 StmtLoc = WS->getCond()->getSourceRange().getEnd();
5099 Body = WS->getBody();
5100 DiagID = diag::warn_empty_while_body;
5101 } else
5102 return; // Neither `for' nor `while'.
5103
5104 // The body should be a null statement.
5105 const NullStmt *NBody = dyn_cast<NullStmt>(Body);
5106 if (!NBody)
5107 return;
5108
5109 // Skip expensive checks if diagnostic is disabled.
5110 if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) ==
5111 DiagnosticsEngine::Ignored)
5112 return;
5113
5114 // Do the usual checks.
5115 if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody))
5116 return;
5117
5118 // `for(...);' and `while(...);' are popular idioms, so in order to keep
5119 // noise level low, emit diagnostics only if for/while is followed by a
5120 // CompoundStmt, e.g.:
5121 // for (int i = 0; i < n; i++);
5122 // {
5123 // a(i);
5124 // }
5125 // or if for/while is followed by a statement with more indentation
5126 // than for/while itself:
5127 // for (int i = 0; i < n; i++);
5128 // a(i);
5129 bool ProbableTypo = isa<CompoundStmt>(PossibleBody);
5130 if (!ProbableTypo) {
5131 bool BodyColInvalid;
5132 unsigned BodyCol = SourceMgr.getPresumedColumnNumber(
5133 PossibleBody->getLocStart(),
5134 &BodyColInvalid);
5135 if (BodyColInvalid)
5136 return;
5137
5138 bool StmtColInvalid;
5139 unsigned StmtCol = SourceMgr.getPresumedColumnNumber(
5140 S->getLocStart(),
5141 &StmtColInvalid);
5142 if (StmtColInvalid)
5143 return;
5144
5145 if (BodyCol > StmtCol)
5146 ProbableTypo = true;
5147 }
5148
5149 if (ProbableTypo) {
5150 Diag(NBody->getSemiLoc(), DiagID);
5151 Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line);
5152 }
5153}