blob: e0b24c4be6ebacaa08c8b00d6f0115bb71f4023d [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"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
Ted Kremenek826a3452010-07-16 02:11:22 +000019#include "clang/Analysis/Analyses/FormatString.h"
Chris Lattner59907c42007-08-10 20:18:51 +000020#include "clang/AST/ASTContext.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000021#include "clang/AST/CharUnits.h"
John McCall384aff82010-08-25 07:42:41 +000022#include "clang/AST/DeclCXX.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000023#include "clang/AST/DeclObjC.h"
Ted Kremenek23245122007-08-20 16:18:38 +000024#include "clang/AST/ExprCXX.h"
Ted Kremenek7ff22b22008-06-16 18:00:42 +000025#include "clang/AST/ExprObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000026#include "clang/AST/EvaluatedExprVisitor.h"
Mike Stumpf8c49212010-01-21 03:59:47 +000027#include "clang/AST/DeclObjC.h"
28#include "clang/AST/StmtCXX.h"
29#include "clang/AST/StmtObjC.h"
Chris Lattner59907c42007-08-10 20:18:51 +000030#include "clang/Lex/Preprocessor.h"
Mike Stumpf8c49212010-01-21 03:59:47 +000031#include "llvm/ADT/BitVector.h"
32#include "llvm/ADT/STLExtras.h"
Tom Care3bfc5f42010-06-09 04:11:11 +000033#include "llvm/Support/raw_ostream.h"
Eric Christopher691ebc32010-04-17 02:26:23 +000034#include "clang/Basic/TargetBuiltins.h"
Nate Begeman26a31422010-06-08 02:47:44 +000035#include "clang/Basic/TargetInfo.h"
Fariborz Jahanian7da71022010-09-07 19:38:13 +000036#include "clang/Basic/ConvertUTF.h"
Zhongxing Xua1f3dba2009-05-20 01:55:10 +000037#include <limits>
Chris Lattner59907c42007-08-10 20:18:51 +000038using namespace clang;
John McCall781472f2010-08-25 08:40:02 +000039using namespace sema;
Chris Lattner59907c42007-08-10 20:18:51 +000040
Chris Lattner60800082009-02-18 17:49:48 +000041SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
42 unsigned ByteNo) const {
Chris Lattner08f92e32010-11-17 07:37:15 +000043 return SL->getLocationOfByte(ByteNo, PP.getSourceManager(),
44 PP.getLangOptions(), PP.getTargetInfo());
Chris Lattner60800082009-02-18 17:49:48 +000045}
Chris Lattner08f92e32010-11-17 07:37:15 +000046
Chris Lattner60800082009-02-18 17:49:48 +000047
Ryan Flynn4403a5e2009-08-06 03:00:50 +000048/// CheckablePrintfAttr - does a function call have a "printf" attribute
49/// and arguments that merit checking?
50bool Sema::CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall) {
51 if (Format->getType() == "printf") return true;
52 if (Format->getType() == "printf0") {
53 // printf0 allows null "format" string; if so don't check format/args
54 unsigned format_idx = Format->getFormatIdx() - 1;
Sebastian Redl4a2614e2009-11-17 18:02:24 +000055 // Does the index refer to the implicit object argument?
56 if (isa<CXXMemberCallExpr>(TheCall)) {
57 if (format_idx == 0)
58 return false;
59 --format_idx;
60 }
Ryan Flynn4403a5e2009-08-06 03:00:50 +000061 if (format_idx < TheCall->getNumArgs()) {
62 Expr *Format = TheCall->getArg(format_idx)->IgnoreParenCasts();
Ted Kremenekefaff192010-02-27 01:41:03 +000063 if (!Format->isNullPointerConstant(Context,
64 Expr::NPC_ValueDependentIsNull))
Ryan Flynn4403a5e2009-08-06 03:00:50 +000065 return true;
66 }
67 }
68 return false;
69}
Chris Lattner60800082009-02-18 17:49:48 +000070
John McCall8e10f3b2011-02-26 05:39:39 +000071/// Checks that a call expression's argument count is the desired number.
72/// This is useful when doing custom type-checking. Returns true on error.
73static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
74 unsigned argCount = call->getNumArgs();
75 if (argCount == desiredArgCount) return false;
76
77 if (argCount < desiredArgCount)
78 return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args)
79 << 0 /*function call*/ << desiredArgCount << argCount
80 << call->getSourceRange();
81
82 // Highlight all the excess arguments.
83 SourceRange range(call->getArg(desiredArgCount)->getLocStart(),
84 call->getArg(argCount - 1)->getLocEnd());
85
86 return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args)
87 << 0 /*function call*/ << desiredArgCount << argCount
88 << call->getArg(1)->getSourceRange();
89}
90
Julien Lerouge77f68bb2011-09-09 22:41:49 +000091/// CheckBuiltinAnnotationString - Checks that string argument to the builtin
92/// annotation is a non wide string literal.
93static bool CheckBuiltinAnnotationString(Sema &S, Expr *Arg) {
94 Arg = Arg->IgnoreParenCasts();
95 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
96 if (!Literal || !Literal->isAscii()) {
97 S.Diag(Arg->getLocStart(), diag::err_builtin_annotation_not_string_constant)
98 << Arg->getSourceRange();
99 return true;
100 }
101 return false;
102}
103
John McCall60d7b3a2010-08-24 06:29:42 +0000104ExprResult
Anders Carlssond406bf02009-08-16 01:56:34 +0000105Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
John McCall60d7b3a2010-08-24 06:29:42 +0000106 ExprResult TheCallResult(Owned(TheCall));
Douglas Gregor2def4832008-11-17 20:34:05 +0000107
Chris Lattner946928f2010-10-01 23:23:24 +0000108 // Find out if any arguments are required to be integer constant expressions.
109 unsigned ICEArguments = 0;
110 ASTContext::GetBuiltinTypeError Error;
111 Context.GetBuiltinType(BuiltinID, Error, &ICEArguments);
112 if (Error != ASTContext::GE_None)
113 ICEArguments = 0; // Don't diagnose previously diagnosed errors.
114
115 // If any arguments are required to be ICE's, check and diagnose.
116 for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) {
117 // Skip arguments not required to be ICE's.
118 if ((ICEArguments & (1 << ArgNo)) == 0) continue;
119
120 llvm::APSInt Result;
121 if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
122 return true;
123 ICEArguments &= ~(1 << ArgNo);
124 }
125
Anders Carlssond406bf02009-08-16 01:56:34 +0000126 switch (BuiltinID) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000127 case Builtin::BI__builtin___CFStringMakeConstantString:
Chris Lattner925e60d2007-12-28 05:29:59 +0000128 assert(TheCall->getNumArgs() == 1 &&
Chris Lattner1b9a0792007-12-20 00:26:33 +0000129 "Wrong # arguments to builtin CFStringMakeConstantString");
Chris Lattner69039812009-02-18 06:01:06 +0000130 if (CheckObjCString(TheCall->getArg(0)))
Sebastian Redl0eb23302009-01-19 00:08:26 +0000131 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000132 break;
Ted Kremenek49ff7a12008-07-09 17:58:53 +0000133 case Builtin::BI__builtin_stdarg_start:
Chris Lattner30ce3442007-12-19 23:59:04 +0000134 case Builtin::BI__builtin_va_start:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000135 if (SemaBuiltinVAStart(TheCall))
136 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000137 break;
Chris Lattner1b9a0792007-12-20 00:26:33 +0000138 case Builtin::BI__builtin_isgreater:
139 case Builtin::BI__builtin_isgreaterequal:
140 case Builtin::BI__builtin_isless:
141 case Builtin::BI__builtin_islessequal:
142 case Builtin::BI__builtin_islessgreater:
143 case Builtin::BI__builtin_isunordered:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000144 if (SemaBuiltinUnorderedCompare(TheCall))
145 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000146 break;
Benjamin Kramere771a7a2010-02-15 22:42:31 +0000147 case Builtin::BI__builtin_fpclassify:
148 if (SemaBuiltinFPClassification(TheCall, 6))
149 return ExprError();
150 break;
Eli Friedman9ac6f622009-08-31 20:06:00 +0000151 case Builtin::BI__builtin_isfinite:
152 case Builtin::BI__builtin_isinf:
153 case Builtin::BI__builtin_isinf_sign:
154 case Builtin::BI__builtin_isnan:
155 case Builtin::BI__builtin_isnormal:
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +0000156 if (SemaBuiltinFPClassification(TheCall, 1))
Eli Friedman9ac6f622009-08-31 20:06:00 +0000157 return ExprError();
158 break;
Eli Friedmand38617c2008-05-14 19:38:39 +0000159 case Builtin::BI__builtin_shufflevector:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000160 return SemaBuiltinShuffleVector(TheCall);
161 // TheCall will be freed by the smart pointer here, but that's fine, since
162 // SemaBuiltinShuffleVector guts it, but then doesn't release it.
Daniel Dunbar4493f792008-07-21 22:59:13 +0000163 case Builtin::BI__builtin_prefetch:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000164 if (SemaBuiltinPrefetch(TheCall))
165 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000166 break;
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000167 case Builtin::BI__builtin_object_size:
Sebastian Redl0eb23302009-01-19 00:08:26 +0000168 if (SemaBuiltinObjectSize(TheCall))
169 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000170 break;
Eli Friedmand875fed2009-05-03 04:46:36 +0000171 case Builtin::BI__builtin_longjmp:
172 if (SemaBuiltinLongjmp(TheCall))
173 return ExprError();
Anders Carlssond406bf02009-08-16 01:56:34 +0000174 break;
John McCall8e10f3b2011-02-26 05:39:39 +0000175
176 case Builtin::BI__builtin_classify_type:
177 if (checkArgCount(*this, TheCall, 1)) return true;
178 TheCall->setType(Context.IntTy);
179 break;
Chris Lattner75c29a02010-10-12 17:47:42 +0000180 case Builtin::BI__builtin_constant_p:
John McCall8e10f3b2011-02-26 05:39:39 +0000181 if (checkArgCount(*this, TheCall, 1)) return true;
182 TheCall->setType(Context.IntTy);
Chris Lattner75c29a02010-10-12 17:47:42 +0000183 break;
Chris Lattner5caa3702009-05-08 06:58:22 +0000184 case Builtin::BI__sync_fetch_and_add:
185 case Builtin::BI__sync_fetch_and_sub:
186 case Builtin::BI__sync_fetch_and_or:
187 case Builtin::BI__sync_fetch_and_and:
188 case Builtin::BI__sync_fetch_and_xor:
189 case Builtin::BI__sync_add_and_fetch:
190 case Builtin::BI__sync_sub_and_fetch:
191 case Builtin::BI__sync_and_and_fetch:
192 case Builtin::BI__sync_or_and_fetch:
193 case Builtin::BI__sync_xor_and_fetch:
194 case Builtin::BI__sync_val_compare_and_swap:
195 case Builtin::BI__sync_bool_compare_and_swap:
196 case Builtin::BI__sync_lock_test_and_set:
197 case Builtin::BI__sync_lock_release:
Chris Lattner23aa9c82011-04-09 03:57:26 +0000198 case Builtin::BI__sync_swap:
Chandler Carruthd2014572010-07-09 18:59:35 +0000199 return SemaBuiltinAtomicOverloaded(move(TheCallResult));
Julien Lerouge77f68bb2011-09-09 22:41:49 +0000200 case Builtin::BI__builtin_annotation:
201 if (CheckBuiltinAnnotationString(*this, TheCall->getArg(1)))
202 return ExprError();
203 break;
Nate Begeman26a31422010-06-08 02:47:44 +0000204 }
205
206 // Since the target specific builtins for each arch overlap, only check those
207 // of the arch we are compiling for.
208 if (BuiltinID >= Builtin::FirstTSBuiltin) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000209 switch (Context.getTargetInfo().getTriple().getArch()) {
Nate Begeman26a31422010-06-08 02:47:44 +0000210 case llvm::Triple::arm:
211 case llvm::Triple::thumb:
212 if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall))
213 return ExprError();
214 break;
Nate Begeman26a31422010-06-08 02:47:44 +0000215 default:
216 break;
217 }
218 }
219
220 return move(TheCallResult);
221}
222
Nate Begeman61eecf52010-06-14 05:21:25 +0000223// Get the valid immediate range for the specified NEON type code.
224static unsigned RFT(unsigned t, bool shift = false) {
225 bool quad = t & 0x10;
226
227 switch (t & 0x7) {
228 case 0: // i8
Nate Begemand69ec162010-06-17 02:26:59 +0000229 return shift ? 7 : (8 << (int)quad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000230 case 1: // i16
Nate Begemand69ec162010-06-17 02:26:59 +0000231 return shift ? 15 : (4 << (int)quad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000232 case 2: // i32
Nate Begemand69ec162010-06-17 02:26:59 +0000233 return shift ? 31 : (2 << (int)quad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000234 case 3: // i64
Nate Begemand69ec162010-06-17 02:26:59 +0000235 return shift ? 63 : (1 << (int)quad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000236 case 4: // f32
237 assert(!shift && "cannot shift float types!");
Nate Begemand69ec162010-06-17 02:26:59 +0000238 return (2 << (int)quad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000239 case 5: // poly8
Bob Wilson42499f92010-12-10 19:45:06 +0000240 return shift ? 7 : (8 << (int)quad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000241 case 6: // poly16
Bob Wilson42499f92010-12-10 19:45:06 +0000242 return shift ? 15 : (4 << (int)quad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000243 case 7: // float16
244 assert(!shift && "cannot shift float types!");
Nate Begemand69ec162010-06-17 02:26:59 +0000245 return (4 << (int)quad) - 1;
Nate Begeman61eecf52010-06-14 05:21:25 +0000246 }
247 return 0;
248}
249
Nate Begeman26a31422010-06-08 02:47:44 +0000250bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000251 llvm::APSInt Result;
252
Nate Begeman0d15c532010-06-13 04:47:52 +0000253 unsigned mask = 0;
Nate Begeman61eecf52010-06-14 05:21:25 +0000254 unsigned TV = 0;
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000255 switch (BuiltinID) {
Nate Begemana23326b2010-06-17 04:17:01 +0000256#define GET_NEON_OVERLOAD_CHECK
257#include "clang/Basic/arm_neon.inc"
258#undef GET_NEON_OVERLOAD_CHECK
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000259 }
260
Nate Begeman0d15c532010-06-13 04:47:52 +0000261 // For NEON intrinsics which are overloaded on vector element type, validate
262 // the immediate which specifies which variant to emit.
263 if (mask) {
264 unsigned ArgNo = TheCall->getNumArgs()-1;
265 if (SemaBuiltinConstantArg(TheCall, ArgNo, Result))
266 return true;
267
Nate Begeman61eecf52010-06-14 05:21:25 +0000268 TV = Result.getLimitedValue(32);
269 if ((TV > 31) || (mask & (1 << TV)) == 0)
Nate Begeman0d15c532010-06-13 04:47:52 +0000270 return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code)
271 << TheCall->getArg(ArgNo)->getSourceRange();
272 }
Nate Begeman1c2a88c2010-06-09 01:10:23 +0000273
Nate Begeman0d15c532010-06-13 04:47:52 +0000274 // For NEON intrinsics which take an immediate value as part of the
275 // instruction, range check them here.
Nate Begeman61eecf52010-06-14 05:21:25 +0000276 unsigned i = 0, l = 0, u = 0;
Nate Begeman0d15c532010-06-13 04:47:52 +0000277 switch (BuiltinID) {
278 default: return false;
Nate Begemanbb37f502010-07-29 22:48:34 +0000279 case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break;
280 case ARM::BI__builtin_arm_usat: i = 1; u = 31; break;
Nate Begeman99c40bb2010-08-03 21:32:34 +0000281 case ARM::BI__builtin_arm_vcvtr_f:
282 case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break;
Nate Begemana23326b2010-06-17 04:17:01 +0000283#define GET_NEON_IMMEDIATE_CHECK
284#include "clang/Basic/arm_neon.inc"
285#undef GET_NEON_IMMEDIATE_CHECK
Nate Begeman0d15c532010-06-13 04:47:52 +0000286 };
287
Nate Begeman61eecf52010-06-14 05:21:25 +0000288 // Check that the immediate argument is actually a constant.
Nate Begeman0d15c532010-06-13 04:47:52 +0000289 if (SemaBuiltinConstantArg(TheCall, i, Result))
290 return true;
291
Nate Begeman61eecf52010-06-14 05:21:25 +0000292 // Range check against the upper/lower values for this isntruction.
Nate Begeman0d15c532010-06-13 04:47:52 +0000293 unsigned Val = Result.getZExtValue();
Nate Begeman61eecf52010-06-14 05:21:25 +0000294 if (Val < l || Val > (u + l))
Nate Begeman0d15c532010-06-13 04:47:52 +0000295 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Benjamin Kramer476d8b82010-08-11 14:47:12 +0000296 << l << u+l << TheCall->getArg(i)->getSourceRange();
Nate Begeman0d15c532010-06-13 04:47:52 +0000297
Nate Begeman99c40bb2010-08-03 21:32:34 +0000298 // FIXME: VFP Intrinsics should error if VFP not present.
Nate Begeman26a31422010-06-08 02:47:44 +0000299 return false;
Anders Carlssond406bf02009-08-16 01:56:34 +0000300}
Daniel Dunbarde454282008-10-02 18:44:07 +0000301
Anders Carlssond406bf02009-08-16 01:56:34 +0000302/// CheckFunctionCall - Check a direct function call for various correctness
303/// and safety properties not strictly enforced by the C type system.
304bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) {
305 // Get the IdentifierInfo* for the called function.
306 IdentifierInfo *FnInfo = FDecl->getIdentifier();
307
308 // None of the checks below are needed for functions that don't have
309 // simple names (e.g., C++ conversion functions).
310 if (!FnInfo)
311 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Daniel Dunbarde454282008-10-02 18:44:07 +0000313 // FIXME: This mechanism should be abstracted to be less fragile and
314 // more efficient. For example, just map function ids to custom
315 // handlers.
316
Ted Kremenekc82faca2010-09-09 04:33:05 +0000317 // Printf and scanf checking.
318 for (specific_attr_iterator<FormatAttr>
319 i = FDecl->specific_attr_begin<FormatAttr>(),
320 e = FDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) {
321
322 const FormatAttr *Format = *i;
Ted Kremenek826a3452010-07-16 02:11:22 +0000323 const bool b = Format->getType() == "scanf";
324 if (b || CheckablePrintfAttr(Format, TheCall)) {
Ted Kremenek3d692df2009-02-27 17:58:43 +0000325 bool HasVAListArg = Format->getFirstArg() == 0;
Ted Kremenek826a3452010-07-16 02:11:22 +0000326 CheckPrintfScanfArguments(TheCall, HasVAListArg,
327 Format->getFormatIdx() - 1,
328 HasVAListArg ? 0 : Format->getFirstArg() - 1,
329 !b);
Douglas Gregor3c385e52009-02-14 18:57:46 +0000330 }
Chris Lattner59907c42007-08-10 20:18:51 +0000331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenekc82faca2010-09-09 04:33:05 +0000333 for (specific_attr_iterator<NonNullAttr>
334 i = FDecl->specific_attr_begin<NonNullAttr>(),
335 e = FDecl->specific_attr_end<NonNullAttr>(); i != e; ++i) {
Nick Lewycky909a70d2011-03-25 01:44:32 +0000336 CheckNonNullArguments(*i, TheCall->getArgs(),
337 TheCall->getCallee()->getLocStart());
Ted Kremenekc82faca2010-09-09 04:33:05 +0000338 }
Sebastian Redl0eb23302009-01-19 00:08:26 +0000339
Ted Kremenekbd5da9d2011-08-18 20:55:45 +0000340 // Builtin handling
Douglas Gregor707a23e2011-06-16 17:56:04 +0000341 int CMF = -1;
342 switch (FDecl->getBuiltinID()) {
343 case Builtin::BI__builtin_memset:
344 case Builtin::BI__builtin___memset_chk:
345 case Builtin::BImemset:
346 CMF = CMF_Memset;
347 break;
348
349 case Builtin::BI__builtin_memcpy:
350 case Builtin::BI__builtin___memcpy_chk:
351 case Builtin::BImemcpy:
352 CMF = CMF_Memcpy;
353 break;
354
355 case Builtin::BI__builtin_memmove:
356 case Builtin::BI__builtin___memmove_chk:
357 case Builtin::BImemmove:
358 CMF = CMF_Memmove;
359 break;
Ted Kremenekbd5da9d2011-08-18 20:55:45 +0000360
361 case Builtin::BIstrlcpy:
362 case Builtin::BIstrlcat:
363 CheckStrlcpycatArguments(TheCall, FnInfo);
364 break;
Douglas Gregor707a23e2011-06-16 17:56:04 +0000365
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +0000366 case Builtin::BI__builtin_memcmp:
367 CMF = CMF_Memcmp;
368 break;
369
Douglas Gregor707a23e2011-06-16 17:56:04 +0000370 default:
371 if (FDecl->getLinkage() == ExternalLinkage &&
372 (!getLangOptions().CPlusPlus || FDecl->isExternC())) {
373 if (FnInfo->isStr("memset"))
374 CMF = CMF_Memset;
375 else if (FnInfo->isStr("memcpy"))
376 CMF = CMF_Memcpy;
377 else if (FnInfo->isStr("memmove"))
378 CMF = CMF_Memmove;
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +0000379 else if (FnInfo->isStr("memcmp"))
380 CMF = CMF_Memcmp;
Douglas Gregor707a23e2011-06-16 17:56:04 +0000381 }
382 break;
Douglas Gregor06bc9eb2011-05-03 20:37:33 +0000383 }
Douglas Gregor707a23e2011-06-16 17:56:04 +0000384
Ted Kremenekbd5da9d2011-08-18 20:55:45 +0000385 // Memset/memcpy/memmove handling
Douglas Gregor707a23e2011-06-16 17:56:04 +0000386 if (CMF != -1)
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +0000387 CheckMemaccessArguments(TheCall, CheckedMemoryFunction(CMF), FnInfo);
Chandler Carruth7ccc95b2011-04-27 07:05:31 +0000388
Anders Carlssond406bf02009-08-16 01:56:34 +0000389 return false;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000390}
391
Anders Carlssond406bf02009-08-16 01:56:34 +0000392bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) {
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000393 // Printf checking.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000394 const FormatAttr *Format = NDecl->getAttr<FormatAttr>();
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000395 if (!Format)
Anders Carlssond406bf02009-08-16 01:56:34 +0000396 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000398 const VarDecl *V = dyn_cast<VarDecl>(NDecl);
399 if (!V)
Anders Carlssond406bf02009-08-16 01:56:34 +0000400 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000402 QualType Ty = V->getType();
403 if (!Ty->isBlockPointerType())
Anders Carlssond406bf02009-08-16 01:56:34 +0000404 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek826a3452010-07-16 02:11:22 +0000406 const bool b = Format->getType() == "scanf";
407 if (!b && !CheckablePrintfAttr(Format, TheCall))
Anders Carlssond406bf02009-08-16 01:56:34 +0000408 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Anders Carlssond406bf02009-08-16 01:56:34 +0000410 bool HasVAListArg = Format->getFirstArg() == 0;
Ted Kremenek826a3452010-07-16 02:11:22 +0000411 CheckPrintfScanfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1,
412 HasVAListArg ? 0 : Format->getFirstArg() - 1, !b);
Anders Carlssond406bf02009-08-16 01:56:34 +0000413
414 return false;
Fariborz Jahanian725165f2009-05-18 21:05:18 +0000415}
416
John McCall5f8d6042011-08-27 01:09:30 +0000417/// checkBuiltinArgument - Given a call to a builtin function, perform
418/// normal type-checking on the given argument, updating the call in
419/// place. This is useful when a builtin function requires custom
420/// type-checking for some of its arguments but not necessarily all of
421/// them.
422///
423/// Returns true on error.
424static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) {
425 FunctionDecl *Fn = E->getDirectCallee();
426 assert(Fn && "builtin call without direct callee!");
427
428 ParmVarDecl *Param = Fn->getParamDecl(ArgIndex);
429 InitializedEntity Entity =
430 InitializedEntity::InitializeParameter(S.Context, Param);
431
432 ExprResult Arg = E->getArg(0);
433 Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg);
434 if (Arg.isInvalid())
435 return true;
436
437 E->setArg(ArgIndex, Arg.take());
438 return false;
439}
440
Chris Lattner5caa3702009-05-08 06:58:22 +0000441/// SemaBuiltinAtomicOverloaded - We have a call to a function like
442/// __sync_fetch_and_add, which is an overloaded function based on the pointer
443/// type of its first argument. The main ActOnCallExpr routines have already
444/// promoted the types of arguments because all of these calls are prototyped as
445/// void(...).
446///
447/// This function goes through and does final semantic checking for these
448/// builtins,
John McCall60d7b3a2010-08-24 06:29:42 +0000449ExprResult
450Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) {
Chandler Carruthd2014572010-07-09 18:59:35 +0000451 CallExpr *TheCall = (CallExpr *)TheCallResult.get();
Chris Lattner5caa3702009-05-08 06:58:22 +0000452 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
453 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
454
455 // Ensure that we have at least one argument to do type inference from.
Chandler Carruthd2014572010-07-09 18:59:35 +0000456 if (TheCall->getNumArgs() < 1) {
457 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
458 << 0 << 1 << TheCall->getNumArgs()
459 << TheCall->getCallee()->getSourceRange();
460 return ExprError();
461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattner5caa3702009-05-08 06:58:22 +0000463 // Inspect the first argument of the atomic builtin. This should always be
464 // a pointer type, whose element is an integral scalar or pointer type.
465 // Because it is a pointer type, we don't have to worry about any implicit
466 // casts here.
Chandler Carruthd2014572010-07-09 18:59:35 +0000467 // FIXME: We don't allow floating point scalars as input.
Chris Lattner5caa3702009-05-08 06:58:22 +0000468 Expr *FirstArg = TheCall->getArg(0);
John McCallf85e1932011-06-15 23:02:42 +0000469 const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>();
470 if (!pointerType) {
Chandler Carruthd2014572010-07-09 18:59:35 +0000471 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer)
472 << FirstArg->getType() << FirstArg->getSourceRange();
473 return ExprError();
474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
John McCallf85e1932011-06-15 23:02:42 +0000476 QualType ValType = pointerType->getPointeeType();
Chris Lattnerdd5fa7a2010-09-17 21:12:38 +0000477 if (!ValType->isIntegerType() && !ValType->isAnyPointerType() &&
Chandler Carruthd2014572010-07-09 18:59:35 +0000478 !ValType->isBlockPointerType()) {
479 Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr)
480 << FirstArg->getType() << FirstArg->getSourceRange();
481 return ExprError();
482 }
Chris Lattner5caa3702009-05-08 06:58:22 +0000483
John McCallf85e1932011-06-15 23:02:42 +0000484 switch (ValType.getObjCLifetime()) {
485 case Qualifiers::OCL_None:
486 case Qualifiers::OCL_ExplicitNone:
487 // okay
488 break;
489
490 case Qualifiers::OCL_Weak:
491 case Qualifiers::OCL_Strong:
492 case Qualifiers::OCL_Autoreleasing:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000493 Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000494 << ValType << FirstArg->getSourceRange();
495 return ExprError();
496 }
497
Chandler Carruth8d13d222010-07-18 20:54:12 +0000498 // The majority of builtins return a value, but a few have special return
499 // types, so allow them to override appropriately below.
500 QualType ResultType = ValType;
501
Chris Lattner5caa3702009-05-08 06:58:22 +0000502 // We need to figure out which concrete builtin this maps onto. For example,
503 // __sync_fetch_and_add with a 2 byte object turns into
504 // __sync_fetch_and_add_2.
505#define BUILTIN_ROW(x) \
506 { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \
507 Builtin::BI##x##_8, Builtin::BI##x##_16 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner5caa3702009-05-08 06:58:22 +0000509 static const unsigned BuiltinIndices[][5] = {
510 BUILTIN_ROW(__sync_fetch_and_add),
511 BUILTIN_ROW(__sync_fetch_and_sub),
512 BUILTIN_ROW(__sync_fetch_and_or),
513 BUILTIN_ROW(__sync_fetch_and_and),
514 BUILTIN_ROW(__sync_fetch_and_xor),
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattner5caa3702009-05-08 06:58:22 +0000516 BUILTIN_ROW(__sync_add_and_fetch),
517 BUILTIN_ROW(__sync_sub_and_fetch),
518 BUILTIN_ROW(__sync_and_and_fetch),
519 BUILTIN_ROW(__sync_or_and_fetch),
520 BUILTIN_ROW(__sync_xor_and_fetch),
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Chris Lattner5caa3702009-05-08 06:58:22 +0000522 BUILTIN_ROW(__sync_val_compare_and_swap),
523 BUILTIN_ROW(__sync_bool_compare_and_swap),
524 BUILTIN_ROW(__sync_lock_test_and_set),
Chris Lattner23aa9c82011-04-09 03:57:26 +0000525 BUILTIN_ROW(__sync_lock_release),
526 BUILTIN_ROW(__sync_swap)
Chris Lattner5caa3702009-05-08 06:58:22 +0000527 };
Mike Stump1eb44332009-09-09 15:08:12 +0000528#undef BUILTIN_ROW
529
Chris Lattner5caa3702009-05-08 06:58:22 +0000530 // Determine the index of the size.
531 unsigned SizeIndex;
Ken Dyck199c3d62010-01-11 17:06:35 +0000532 switch (Context.getTypeSizeInChars(ValType).getQuantity()) {
Chris Lattner5caa3702009-05-08 06:58:22 +0000533 case 1: SizeIndex = 0; break;
534 case 2: SizeIndex = 1; break;
535 case 4: SizeIndex = 2; break;
536 case 8: SizeIndex = 3; break;
537 case 16: SizeIndex = 4; break;
538 default:
Chandler Carruthd2014572010-07-09 18:59:35 +0000539 Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size)
540 << FirstArg->getType() << FirstArg->getSourceRange();
541 return ExprError();
Chris Lattner5caa3702009-05-08 06:58:22 +0000542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattner5caa3702009-05-08 06:58:22 +0000544 // Each of these builtins has one pointer argument, followed by some number of
545 // values (0, 1 or 2) followed by a potentially empty varags list of stuff
546 // that we ignore. Find out which row of BuiltinIndices to read from as well
547 // as the number of fixed args.
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000548 unsigned BuiltinID = FDecl->getBuiltinID();
Chris Lattner5caa3702009-05-08 06:58:22 +0000549 unsigned BuiltinIndex, NumFixed = 1;
550 switch (BuiltinID) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000551 default: llvm_unreachable("Unknown overloaded atomic builtin!");
Chris Lattner5caa3702009-05-08 06:58:22 +0000552 case Builtin::BI__sync_fetch_and_add: BuiltinIndex = 0; break;
553 case Builtin::BI__sync_fetch_and_sub: BuiltinIndex = 1; break;
554 case Builtin::BI__sync_fetch_and_or: BuiltinIndex = 2; break;
555 case Builtin::BI__sync_fetch_and_and: BuiltinIndex = 3; break;
556 case Builtin::BI__sync_fetch_and_xor: BuiltinIndex = 4; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Daniel Dunbar7eff7c42010-03-25 17:13:09 +0000558 case Builtin::BI__sync_add_and_fetch: BuiltinIndex = 5; break;
559 case Builtin::BI__sync_sub_and_fetch: BuiltinIndex = 6; break;
560 case Builtin::BI__sync_and_and_fetch: BuiltinIndex = 7; break;
561 case Builtin::BI__sync_or_and_fetch: BuiltinIndex = 8; break;
562 case Builtin::BI__sync_xor_and_fetch: BuiltinIndex = 9; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattner5caa3702009-05-08 06:58:22 +0000564 case Builtin::BI__sync_val_compare_and_swap:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +0000565 BuiltinIndex = 10;
Chris Lattner5caa3702009-05-08 06:58:22 +0000566 NumFixed = 2;
567 break;
568 case Builtin::BI__sync_bool_compare_and_swap:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +0000569 BuiltinIndex = 11;
Chris Lattner5caa3702009-05-08 06:58:22 +0000570 NumFixed = 2;
Chandler Carruth8d13d222010-07-18 20:54:12 +0000571 ResultType = Context.BoolTy;
Chris Lattner5caa3702009-05-08 06:58:22 +0000572 break;
Daniel Dunbar7eff7c42010-03-25 17:13:09 +0000573 case Builtin::BI__sync_lock_test_and_set: BuiltinIndex = 12; break;
Chris Lattner5caa3702009-05-08 06:58:22 +0000574 case Builtin::BI__sync_lock_release:
Daniel Dunbar7eff7c42010-03-25 17:13:09 +0000575 BuiltinIndex = 13;
Chris Lattner5caa3702009-05-08 06:58:22 +0000576 NumFixed = 0;
Chandler Carruth8d13d222010-07-18 20:54:12 +0000577 ResultType = Context.VoidTy;
Chris Lattner5caa3702009-05-08 06:58:22 +0000578 break;
Chris Lattner23aa9c82011-04-09 03:57:26 +0000579 case Builtin::BI__sync_swap: BuiltinIndex = 14; break;
Chris Lattner5caa3702009-05-08 06:58:22 +0000580 }
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Chris Lattner5caa3702009-05-08 06:58:22 +0000582 // Now that we know how many fixed arguments we expect, first check that we
583 // have at least that many.
Chandler Carruthd2014572010-07-09 18:59:35 +0000584 if (TheCall->getNumArgs() < 1+NumFixed) {
585 Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least)
586 << 0 << 1+NumFixed << TheCall->getNumArgs()
587 << TheCall->getCallee()->getSourceRange();
588 return ExprError();
589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattnere7ac0a92009-05-08 15:36:58 +0000591 // Get the decl for the concrete builtin from this, we can tell what the
592 // concrete integer type we should convert to is.
593 unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
594 const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID);
595 IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName);
Mike Stump1eb44332009-09-09 15:08:12 +0000596 FunctionDecl *NewBuiltinDecl =
Chris Lattnere7ac0a92009-05-08 15:36:58 +0000597 cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID,
598 TUScope, false, DRE->getLocStart()));
Chandler Carruthd2014572010-07-09 18:59:35 +0000599
John McCallf871d0c2010-08-07 06:22:56 +0000600 // The first argument --- the pointer --- has a fixed type; we
601 // deduce the types of the rest of the arguments accordingly. Walk
602 // the remaining arguments, converting them to the deduced value type.
Chris Lattner5caa3702009-05-08 06:58:22 +0000603 for (unsigned i = 0; i != NumFixed; ++i) {
John Wiegley429bb272011-04-08 18:41:53 +0000604 ExprResult Arg = TheCall->getArg(i+1);
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Chris Lattner5caa3702009-05-08 06:58:22 +0000606 // If the argument is an implicit cast, then there was a promotion due to
607 // "...", just remove it now.
John Wiegley429bb272011-04-08 18:41:53 +0000608 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg.get())) {
Chris Lattner5caa3702009-05-08 06:58:22 +0000609 Arg = ICE->getSubExpr();
610 ICE->setSubExpr(0);
John Wiegley429bb272011-04-08 18:41:53 +0000611 TheCall->setArg(i+1, Arg.get());
Chris Lattner5caa3702009-05-08 06:58:22 +0000612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Chris Lattner5caa3702009-05-08 06:58:22 +0000614 // GCC does an implicit conversion to the pointer or integer ValType. This
615 // can fail in some cases (1i -> int**), check for this error case now.
John McCalldaa8e4e2010-11-15 09:13:47 +0000616 CastKind Kind = CK_Invalid;
John McCallf89e55a2010-11-18 06:31:45 +0000617 ExprValueKind VK = VK_RValue;
John McCallf871d0c2010-08-07 06:22:56 +0000618 CXXCastPath BasePath;
John McCallf85e1932011-06-15 23:02:42 +0000619 Arg = CheckCastTypes(Arg.get()->getLocStart(), Arg.get()->getSourceRange(),
620 ValType, Arg.take(), Kind, VK, BasePath);
John Wiegley429bb272011-04-08 18:41:53 +0000621 if (Arg.isInvalid())
Chandler Carruthd2014572010-07-09 18:59:35 +0000622 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Chris Lattner5caa3702009-05-08 06:58:22 +0000624 // Okay, we have something that *can* be converted to the right type. Check
625 // to see if there is a potentially weird extension going on here. This can
626 // happen when you do an atomic operation on something like an char* and
627 // pass in 42. The 42 gets converted to char. This is even more strange
628 // for things like 45.123 -> char, etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000629 // FIXME: Do this check.
John Wiegley429bb272011-04-08 18:41:53 +0000630 Arg = ImpCastExprToType(Arg.take(), ValType, Kind, VK, &BasePath);
631 TheCall->setArg(i+1, Arg.get());
Chris Lattner5caa3702009-05-08 06:58:22 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +0000634 ASTContext& Context = this->getASTContext();
635
636 // Create a new DeclRefExpr to refer to the new decl.
637 DeclRefExpr* NewDRE = DeclRefExpr::Create(
638 Context,
639 DRE->getQualifierLoc(),
640 NewBuiltinDecl,
641 DRE->getLocation(),
642 NewBuiltinDecl->getType(),
643 DRE->getValueKind());
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Chris Lattner5caa3702009-05-08 06:58:22 +0000645 // Set the callee in the CallExpr.
646 // FIXME: This leaks the original parens and implicit casts.
Douglas Gregorbbcb7ea2011-09-09 16:51:10 +0000647 ExprResult PromotedCall = UsualUnaryConversions(NewDRE);
John Wiegley429bb272011-04-08 18:41:53 +0000648 if (PromotedCall.isInvalid())
649 return ExprError();
650 TheCall->setCallee(PromotedCall.take());
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chandler Carruthdb4325b2010-07-18 07:23:17 +0000652 // Change the result type of the call to match the original value type. This
653 // is arbitrary, but the codegen for these builtins ins design to handle it
654 // gracefully.
Chandler Carruth8d13d222010-07-18 20:54:12 +0000655 TheCall->setType(ResultType);
Chandler Carruthd2014572010-07-09 18:59:35 +0000656
657 return move(TheCallResult);
Chris Lattner5caa3702009-05-08 06:58:22 +0000658}
659
Chris Lattner69039812009-02-18 06:01:06 +0000660/// CheckObjCString - Checks that the argument to the builtin
Anders Carlsson71993dd2007-08-17 05:31:46 +0000661/// CFString constructor is correct
Steve Narofffd942622009-04-13 20:26:29 +0000662/// Note: It might also make sense to do the UTF-16 conversion here (would
663/// simplify the backend).
Chris Lattner69039812009-02-18 06:01:06 +0000664bool Sema::CheckObjCString(Expr *Arg) {
Chris Lattner56f34942008-02-13 01:02:39 +0000665 Arg = Arg->IgnoreParenCasts();
Anders Carlsson71993dd2007-08-17 05:31:46 +0000666 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
667
Douglas Gregor5cee1192011-07-27 05:40:30 +0000668 if (!Literal || !Literal->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000669 Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
670 << Arg->getSourceRange();
Anders Carlsson9cdc4d32007-08-17 15:44:17 +0000671 return true;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000672 }
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Fariborz Jahanian7da71022010-09-07 19:38:13 +0000674 if (Literal->containsNonAsciiOrNull()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000675 StringRef String = Literal->getString();
Fariborz Jahanian7da71022010-09-07 19:38:13 +0000676 unsigned NumBytes = String.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000677 SmallVector<UTF16, 128> ToBuf(NumBytes);
Fariborz Jahanian7da71022010-09-07 19:38:13 +0000678 const UTF8 *FromPtr = (UTF8 *)String.data();
679 UTF16 *ToPtr = &ToBuf[0];
680
681 ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
682 &ToPtr, ToPtr + NumBytes,
683 strictConversion);
684 // Check for conversion failure.
685 if (Result != conversionOK)
686 Diag(Arg->getLocStart(),
687 diag::warn_cfstring_truncated) << Arg->getSourceRange();
688 }
Anders Carlsson9cdc4d32007-08-17 15:44:17 +0000689 return false;
Chris Lattner59907c42007-08-10 20:18:51 +0000690}
691
Chris Lattnerc27c6652007-12-20 00:05:45 +0000692/// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
693/// Emit an error and return true on failure, return false on success.
Chris Lattner925e60d2007-12-28 05:29:59 +0000694bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
695 Expr *Fn = TheCall->getCallee();
696 if (TheCall->getNumArgs() > 2) {
Chris Lattner2c21a072008-11-21 18:44:24 +0000697 Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000698 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000699 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
700 << Fn->getSourceRange()
Mike Stump1eb44332009-09-09 15:08:12 +0000701 << SourceRange(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000702 (*(TheCall->arg_end()-1))->getLocEnd());
Chris Lattner30ce3442007-12-19 23:59:04 +0000703 return true;
704 }
Eli Friedman56f20ae2008-12-15 22:05:35 +0000705
706 if (TheCall->getNumArgs() < 2) {
Eric Christopherd77b9a22010-04-16 04:48:22 +0000707 return Diag(TheCall->getLocEnd(),
708 diag::err_typecheck_call_too_few_args_at_least)
709 << 0 /*function call*/ << 2 << TheCall->getNumArgs();
Eli Friedman56f20ae2008-12-15 22:05:35 +0000710 }
711
John McCall5f8d6042011-08-27 01:09:30 +0000712 // Type-check the first argument normally.
713 if (checkBuiltinArgument(*this, TheCall, 0))
714 return true;
715
Chris Lattnerc27c6652007-12-20 00:05:45 +0000716 // Determine whether the current function is variadic or not.
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000717 BlockScopeInfo *CurBlock = getCurBlock();
Chris Lattnerc27c6652007-12-20 00:05:45 +0000718 bool isVariadic;
Steve Naroffcd9c5142009-04-15 19:33:47 +0000719 if (CurBlock)
John McCallc71a4912010-06-04 19:02:56 +0000720 isVariadic = CurBlock->TheDecl->isVariadic();
Ted Kremenek9498d382010-04-29 16:49:01 +0000721 else if (FunctionDecl *FD = getCurFunctionDecl())
722 isVariadic = FD->isVariadic();
723 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000724 isVariadic = getCurMethodDecl()->isVariadic();
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Chris Lattnerc27c6652007-12-20 00:05:45 +0000726 if (!isVariadic) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000727 Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
728 return true;
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Chris Lattner30ce3442007-12-19 23:59:04 +0000731 // Verify that the second argument to the builtin is the last argument of the
732 // current function or method.
733 bool SecondArgIsLastNamedArgument = false;
Anders Carlssone2c14102008-02-13 01:22:59 +0000734 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Anders Carlsson88cf2262008-02-11 04:20:54 +0000736 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
737 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000738 // FIXME: This isn't correct for methods (results in bogus warning).
739 // Get the last formal in the current function.
Anders Carlsson88cf2262008-02-11 04:20:54 +0000740 const ParmVarDecl *LastArg;
Steve Naroffcd9c5142009-04-15 19:33:47 +0000741 if (CurBlock)
742 LastArg = *(CurBlock->TheDecl->param_end()-1);
743 else if (FunctionDecl *FD = getCurFunctionDecl())
Chris Lattner371f2582008-12-04 23:50:19 +0000744 LastArg = *(FD->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +0000745 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000746 LastArg = *(getCurMethodDecl()->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +0000747 SecondArgIsLastNamedArgument = PV == LastArg;
748 }
749 }
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Chris Lattner30ce3442007-12-19 23:59:04 +0000751 if (!SecondArgIsLastNamedArgument)
Mike Stump1eb44332009-09-09 15:08:12 +0000752 Diag(TheCall->getArg(1)->getLocStart(),
Chris Lattner30ce3442007-12-19 23:59:04 +0000753 diag::warn_second_parameter_of_va_start_not_last_named_argument);
754 return false;
Eli Friedman6cfda232008-05-20 08:23:37 +0000755}
Chris Lattner30ce3442007-12-19 23:59:04 +0000756
Chris Lattner1b9a0792007-12-20 00:26:33 +0000757/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
758/// friends. This is declared to take (...), so we have to check everything.
Chris Lattner925e60d2007-12-28 05:29:59 +0000759bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
760 if (TheCall->getNumArgs() < 2)
Chris Lattner2c21a072008-11-21 18:44:24 +0000761 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Eric Christopherd77b9a22010-04-16 04:48:22 +0000762 << 0 << 2 << TheCall->getNumArgs()/*function call*/;
Chris Lattner925e60d2007-12-28 05:29:59 +0000763 if (TheCall->getNumArgs() > 2)
Mike Stump1eb44332009-09-09 15:08:12 +0000764 return Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000765 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000766 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000767 << SourceRange(TheCall->getArg(2)->getLocStart(),
768 (*(TheCall->arg_end()-1))->getLocEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000769
John Wiegley429bb272011-04-08 18:41:53 +0000770 ExprResult OrigArg0 = TheCall->getArg(0);
771 ExprResult OrigArg1 = TheCall->getArg(1);
Douglas Gregorcde01732009-05-19 22:10:17 +0000772
Chris Lattner1b9a0792007-12-20 00:26:33 +0000773 // Do standard promotions between the two arguments, returning their common
774 // type.
Chris Lattner925e60d2007-12-28 05:29:59 +0000775 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
John Wiegley429bb272011-04-08 18:41:53 +0000776 if (OrigArg0.isInvalid() || OrigArg1.isInvalid())
777 return true;
Daniel Dunbar403bc2b2009-02-19 19:28:43 +0000778
779 // Make sure any conversions are pushed back into the call; this is
780 // type safe since unordered compare builtins are declared as "_Bool
781 // foo(...)".
John Wiegley429bb272011-04-08 18:41:53 +0000782 TheCall->setArg(0, OrigArg0.get());
783 TheCall->setArg(1, OrigArg1.get());
Mike Stump1eb44332009-09-09 15:08:12 +0000784
John Wiegley429bb272011-04-08 18:41:53 +0000785 if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent())
Douglas Gregorcde01732009-05-19 22:10:17 +0000786 return false;
787
Chris Lattner1b9a0792007-12-20 00:26:33 +0000788 // If the common type isn't a real floating type, then the arguments were
789 // invalid for this operation.
790 if (!Res->isRealFloatingType())
John Wiegley429bb272011-04-08 18:41:53 +0000791 return Diag(OrigArg0.get()->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000792 diag::err_typecheck_call_invalid_ordered_compare)
John Wiegley429bb272011-04-08 18:41:53 +0000793 << OrigArg0.get()->getType() << OrigArg1.get()->getType()
794 << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Chris Lattner1b9a0792007-12-20 00:26:33 +0000796 return false;
797}
798
Benjamin Kramere771a7a2010-02-15 22:42:31 +0000799/// SemaBuiltinSemaBuiltinFPClassification - Handle functions like
800/// __builtin_isnan and friends. This is declared to take (...), so we have
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +0000801/// to check everything. We expect the last argument to be a floating point
802/// value.
803bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) {
804 if (TheCall->getNumArgs() < NumArgs)
Eli Friedman9ac6f622009-08-31 20:06:00 +0000805 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Eric Christopherd77b9a22010-04-16 04:48:22 +0000806 << 0 << NumArgs << TheCall->getNumArgs()/*function call*/;
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +0000807 if (TheCall->getNumArgs() > NumArgs)
808 return Diag(TheCall->getArg(NumArgs)->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +0000809 diag::err_typecheck_call_too_many_args)
Eric Christopherccfa9632010-04-16 04:56:46 +0000810 << 0 /*function call*/ << NumArgs << TheCall->getNumArgs()
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +0000811 << SourceRange(TheCall->getArg(NumArgs)->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +0000812 (*(TheCall->arg_end()-1))->getLocEnd());
813
Benjamin Kramer3b1e26b2010-02-16 10:07:31 +0000814 Expr *OrigArg = TheCall->getArg(NumArgs-1);
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Eli Friedman9ac6f622009-08-31 20:06:00 +0000816 if (OrigArg->isTypeDependent())
817 return false;
818
Chris Lattner81368fb2010-05-06 05:50:07 +0000819 // This operation requires a non-_Complex floating-point number.
Eli Friedman9ac6f622009-08-31 20:06:00 +0000820 if (!OrigArg->getType()->isRealFloatingType())
Mike Stump1eb44332009-09-09 15:08:12 +0000821 return Diag(OrigArg->getLocStart(),
Eli Friedman9ac6f622009-08-31 20:06:00 +0000822 diag::err_typecheck_call_invalid_unary_fp)
823 << OrigArg->getType() << OrigArg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Chris Lattner81368fb2010-05-06 05:50:07 +0000825 // If this is an implicit conversion from float -> double, remove it.
826 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) {
827 Expr *CastArg = Cast->getSubExpr();
828 if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) {
829 assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) &&
830 "promotion from float to double is the only expected cast here");
831 Cast->setSubExpr(0);
Chris Lattner81368fb2010-05-06 05:50:07 +0000832 TheCall->setArg(NumArgs-1, CastArg);
833 OrigArg = CastArg;
834 }
835 }
836
Eli Friedman9ac6f622009-08-31 20:06:00 +0000837 return false;
838}
839
Eli Friedmand38617c2008-05-14 19:38:39 +0000840/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
841// This is declared to take (...), so we have to check everything.
John McCall60d7b3a2010-08-24 06:29:42 +0000842ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
Nate Begeman37b6a572010-06-08 00:16:34 +0000843 if (TheCall->getNumArgs() < 2)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000844 return ExprError(Diag(TheCall->getLocEnd(),
Eric Christopherd77b9a22010-04-16 04:48:22 +0000845 diag::err_typecheck_call_too_few_args_at_least)
Nate Begeman37b6a572010-06-08 00:16:34 +0000846 << 0 /*function call*/ << 2 << TheCall->getNumArgs()
Eric Christopherd77b9a22010-04-16 04:48:22 +0000847 << TheCall->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +0000848
Nate Begeman37b6a572010-06-08 00:16:34 +0000849 // Determine which of the following types of shufflevector we're checking:
850 // 1) unary, vector mask: (lhs, mask)
851 // 2) binary, vector mask: (lhs, rhs, mask)
852 // 3) binary, scalar mask: (lhs, rhs, index, ..., index)
853 QualType resType = TheCall->getArg(0)->getType();
854 unsigned numElements = 0;
855
Douglas Gregorcde01732009-05-19 22:10:17 +0000856 if (!TheCall->getArg(0)->isTypeDependent() &&
857 !TheCall->getArg(1)->isTypeDependent()) {
Nate Begeman37b6a572010-06-08 00:16:34 +0000858 QualType LHSType = TheCall->getArg(0)->getType();
859 QualType RHSType = TheCall->getArg(1)->getType();
860
861 if (!LHSType->isVectorType() || !RHSType->isVectorType()) {
Douglas Gregorcde01732009-05-19 22:10:17 +0000862 Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector)
Mike Stump1eb44332009-09-09 15:08:12 +0000863 << SourceRange(TheCall->getArg(0)->getLocStart(),
Douglas Gregorcde01732009-05-19 22:10:17 +0000864 TheCall->getArg(1)->getLocEnd());
865 return ExprError();
866 }
Nate Begeman37b6a572010-06-08 00:16:34 +0000867
868 numElements = LHSType->getAs<VectorType>()->getNumElements();
869 unsigned numResElements = TheCall->getNumArgs() - 2;
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Nate Begeman37b6a572010-06-08 00:16:34 +0000871 // Check to see if we have a call with 2 vector arguments, the unary shuffle
872 // with mask. If so, verify that RHS is an integer vector type with the
873 // same number of elts as lhs.
874 if (TheCall->getNumArgs() == 2) {
Douglas Gregorf6094622010-07-23 15:58:24 +0000875 if (!RHSType->hasIntegerRepresentation() ||
Nate Begeman37b6a572010-06-08 00:16:34 +0000876 RHSType->getAs<VectorType>()->getNumElements() != numElements)
877 Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
878 << SourceRange(TheCall->getArg(1)->getLocStart(),
879 TheCall->getArg(1)->getLocEnd());
880 numResElements = numElements;
881 }
882 else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) {
Douglas Gregorcde01732009-05-19 22:10:17 +0000883 Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
Mike Stump1eb44332009-09-09 15:08:12 +0000884 << SourceRange(TheCall->getArg(0)->getLocStart(),
Douglas Gregorcde01732009-05-19 22:10:17 +0000885 TheCall->getArg(1)->getLocEnd());
886 return ExprError();
Nate Begeman37b6a572010-06-08 00:16:34 +0000887 } else if (numElements != numResElements) {
888 QualType eltType = LHSType->getAs<VectorType>()->getElementType();
Chris Lattner788b0fd2010-06-23 06:00:24 +0000889 resType = Context.getVectorType(eltType, numResElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +0000890 VectorType::GenericVector);
Douglas Gregorcde01732009-05-19 22:10:17 +0000891 }
Eli Friedmand38617c2008-05-14 19:38:39 +0000892 }
893
894 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
Douglas Gregorcde01732009-05-19 22:10:17 +0000895 if (TheCall->getArg(i)->isTypeDependent() ||
896 TheCall->getArg(i)->isValueDependent())
897 continue;
898
Nate Begeman37b6a572010-06-08 00:16:34 +0000899 llvm::APSInt Result(32);
900 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
901 return ExprError(Diag(TheCall->getLocStart(),
902 diag::err_shufflevector_nonconstant_argument)
903 << TheCall->getArg(i)->getSourceRange());
Sebastian Redl0eb23302009-01-19 00:08:26 +0000904
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000905 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000906 return ExprError(Diag(TheCall->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000907 diag::err_shufflevector_argument_too_large)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000908 << TheCall->getArg(i)->getSourceRange());
Eli Friedmand38617c2008-05-14 19:38:39 +0000909 }
910
Chris Lattner5f9e2722011-07-23 10:55:15 +0000911 SmallVector<Expr*, 32> exprs;
Eli Friedmand38617c2008-05-14 19:38:39 +0000912
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000913 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
Eli Friedmand38617c2008-05-14 19:38:39 +0000914 exprs.push_back(TheCall->getArg(i));
915 TheCall->setArg(i, 0);
916 }
917
Nate Begemana88dc302009-08-12 02:10:25 +0000918 return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(),
Nate Begeman37b6a572010-06-08 00:16:34 +0000919 exprs.size(), resType,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000920 TheCall->getCallee()->getLocStart(),
921 TheCall->getRParenLoc()));
Eli Friedmand38617c2008-05-14 19:38:39 +0000922}
Chris Lattner30ce3442007-12-19 23:59:04 +0000923
Daniel Dunbar4493f792008-07-21 22:59:13 +0000924/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
925// This is declared to take (const void*, ...) and can take two
926// optional constant int args.
927bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000928 unsigned NumArgs = TheCall->getNumArgs();
Daniel Dunbar4493f792008-07-21 22:59:13 +0000929
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000930 if (NumArgs > 3)
Eric Christopherccfa9632010-04-16 04:56:46 +0000931 return Diag(TheCall->getLocEnd(),
932 diag::err_typecheck_call_too_many_args_at_most)
933 << 0 /*function call*/ << 3 << NumArgs
934 << TheCall->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +0000935
936 // Argument 0 is checked for us and the remaining arguments must be
937 // constant integers.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000938 for (unsigned i = 1; i != NumArgs; ++i) {
Daniel Dunbar4493f792008-07-21 22:59:13 +0000939 Expr *Arg = TheCall->getArg(i);
Eric Christopher691ebc32010-04-17 02:26:23 +0000940
Eli Friedman9aef7262009-12-04 00:30:06 +0000941 llvm::APSInt Result;
Eric Christopher691ebc32010-04-17 02:26:23 +0000942 if (SemaBuiltinConstantArg(TheCall, i, Result))
943 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Daniel Dunbar4493f792008-07-21 22:59:13 +0000945 // FIXME: gcc issues a warning and rewrites these to 0. These
946 // seems especially odd for the third argument since the default
947 // is 3.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000948 if (i == 1) {
Eli Friedman9aef7262009-12-04 00:30:06 +0000949 if (Result.getLimitedValue() > 1)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000950 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Chris Lattner21fb98e2009-09-23 06:06:36 +0000951 << "0" << "1" << Arg->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +0000952 } else {
Eli Friedman9aef7262009-12-04 00:30:06 +0000953 if (Result.getLimitedValue() > 3)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000954 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
Chris Lattner21fb98e2009-09-23 06:06:36 +0000955 << "0" << "3" << Arg->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +0000956 }
957 }
958
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000959 return false;
Daniel Dunbar4493f792008-07-21 22:59:13 +0000960}
961
Eric Christopher691ebc32010-04-17 02:26:23 +0000962/// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
963/// TheCall is a constant expression.
964bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum,
965 llvm::APSInt &Result) {
966 Expr *Arg = TheCall->getArg(ArgNum);
967 DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
968 FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
969
970 if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
971
972 if (!Arg->isIntegerConstantExpr(Result, Context))
973 return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type)
Eric Christopher5e896552010-04-19 18:23:02 +0000974 << FDecl->getDeclName() << Arg->getSourceRange();
Eric Christopher691ebc32010-04-17 02:26:23 +0000975
Chris Lattner21fb98e2009-09-23 06:06:36 +0000976 return false;
977}
978
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000979/// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
980/// int type). This simply type checks that type is one of the defined
981/// constants (0-3).
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000982// For compatibility check 0-3, llvm only handles 0 and 2.
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000983bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
Eric Christopher691ebc32010-04-17 02:26:23 +0000984 llvm::APSInt Result;
985
986 // Check constant-ness first.
987 if (SemaBuiltinConstantArg(TheCall, 1, Result))
988 return true;
989
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000990 Expr *Arg = TheCall->getArg(1);
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000991 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000992 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
993 << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000994 }
995
996 return false;
997}
998
Eli Friedman586d6a82009-05-03 06:04:26 +0000999/// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val).
Eli Friedmand875fed2009-05-03 04:46:36 +00001000/// This checks that val is a constant 1.
1001bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) {
1002 Expr *Arg = TheCall->getArg(1);
Eric Christopher691ebc32010-04-17 02:26:23 +00001003 llvm::APSInt Result;
Douglas Gregorcde01732009-05-19 22:10:17 +00001004
Eric Christopher691ebc32010-04-17 02:26:23 +00001005 // TODO: This is less than ideal. Overload this to take a value.
1006 if (SemaBuiltinConstantArg(TheCall, 1, Result))
1007 return true;
1008
1009 if (Result != 1)
Eli Friedmand875fed2009-05-03 04:46:36 +00001010 return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val)
1011 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
1012
1013 return false;
1014}
1015
Ted Kremenekb43e8ad2011-02-24 23:03:04 +00001016// Handle i > 1 ? "x" : "y", recursively.
Ted Kremenek082d9362009-03-20 21:35:28 +00001017bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall,
1018 bool HasVAListArg,
Ted Kremenek826a3452010-07-16 02:11:22 +00001019 unsigned format_idx, unsigned firstDataArg,
1020 bool isPrintf) {
Ted Kremenek4fe64412010-09-09 03:51:39 +00001021 tryAgain:
Douglas Gregorcde01732009-05-19 22:10:17 +00001022 if (E->isTypeDependent() || E->isValueDependent())
1023 return false;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001024
Peter Collingbournef111d932011-04-15 00:35:48 +00001025 E = E->IgnoreParens();
1026
Ted Kremenekd30ef872009-01-12 23:09:09 +00001027 switch (E->getStmtClass()) {
John McCall56ca35d2011-02-17 10:25:35 +00001028 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenekd30ef872009-01-12 23:09:09 +00001029 case Stmt::ConditionalOperatorClass: {
John McCall56ca35d2011-02-17 10:25:35 +00001030 const AbstractConditionalOperator *C = cast<AbstractConditionalOperator>(E);
Ted Kremenek826a3452010-07-16 02:11:22 +00001031 return SemaCheckStringLiteral(C->getTrueExpr(), TheCall, HasVAListArg,
1032 format_idx, firstDataArg, isPrintf)
John McCall56ca35d2011-02-17 10:25:35 +00001033 && SemaCheckStringLiteral(C->getFalseExpr(), TheCall, HasVAListArg,
Ted Kremenek826a3452010-07-16 02:11:22 +00001034 format_idx, firstDataArg, isPrintf);
Ted Kremenekd30ef872009-01-12 23:09:09 +00001035 }
1036
Ted Kremenek95355bb2010-09-09 03:51:42 +00001037 case Stmt::IntegerLiteralClass:
1038 // Technically -Wformat-nonliteral does not warn about this case.
1039 // The behavior of printf and friends in this case is implementation
1040 // dependent. Ideally if the format string cannot be null then
1041 // it should have a 'nonnull' attribute in the function prototype.
1042 return true;
1043
Ted Kremenekd30ef872009-01-12 23:09:09 +00001044 case Stmt::ImplicitCastExprClass: {
Ted Kremenek4fe64412010-09-09 03:51:39 +00001045 E = cast<ImplicitCastExpr>(E)->getSubExpr();
1046 goto tryAgain;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001047 }
1048
John McCall56ca35d2011-02-17 10:25:35 +00001049 case Stmt::OpaqueValueExprClass:
1050 if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
1051 E = src;
1052 goto tryAgain;
1053 }
1054 return false;
1055
Ted Kremenekb43e8ad2011-02-24 23:03:04 +00001056 case Stmt::PredefinedExprClass:
1057 // While __func__, etc., are technically not string literals, they
1058 // cannot contain format specifiers and thus are not a security
1059 // liability.
1060 return true;
1061
Ted Kremenek082d9362009-03-20 21:35:28 +00001062 case Stmt::DeclRefExprClass: {
1063 const DeclRefExpr *DR = cast<DeclRefExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Ted Kremenek082d9362009-03-20 21:35:28 +00001065 // As an exception, do not flag errors for variables binding to
1066 // const string literals.
1067 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
1068 bool isConstant = false;
1069 QualType T = DR->getType();
Ted Kremenekd30ef872009-01-12 23:09:09 +00001070
Ted Kremenek082d9362009-03-20 21:35:28 +00001071 if (const ArrayType *AT = Context.getAsArrayType(T)) {
1072 isConstant = AT->getElementType().isConstant(Context);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001073 } else if (const PointerType *PT = T->getAs<PointerType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001074 isConstant = T.isConstant(Context) &&
Ted Kremenek082d9362009-03-20 21:35:28 +00001075 PT->getPointeeType().isConstant(Context);
1076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Ted Kremenek082d9362009-03-20 21:35:28 +00001078 if (isConstant) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001079 if (const Expr *Init = VD->getAnyInitializer())
Ted Kremenek082d9362009-03-20 21:35:28 +00001080 return SemaCheckStringLiteral(Init, TheCall,
Ted Kremenek826a3452010-07-16 02:11:22 +00001081 HasVAListArg, format_idx, firstDataArg,
1082 isPrintf);
Ted Kremenek082d9362009-03-20 21:35:28 +00001083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Anders Carlssond966a552009-06-28 19:55:58 +00001085 // For vprintf* functions (i.e., HasVAListArg==true), we add a
1086 // special check to see if the format string is a function parameter
1087 // of the function calling the printf function. If the function
1088 // has an attribute indicating it is a printf-like function, then we
1089 // should suppress warnings concerning non-literals being used in a call
1090 // to a vprintf function. For example:
1091 //
1092 // void
1093 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){
1094 // va_list ap;
1095 // va_start(ap, fmt);
1096 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt".
1097 // ...
1098 //
1099 //
1100 // FIXME: We don't have full attribute support yet, so just check to see
1101 // if the argument is a DeclRefExpr that references a parameter. We'll
1102 // add proper support for checking the attribute later.
1103 if (HasVAListArg)
1104 if (isa<ParmVarDecl>(VD))
1105 return true;
Ted Kremenek082d9362009-03-20 21:35:28 +00001106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Ted Kremenek082d9362009-03-20 21:35:28 +00001108 return false;
1109 }
Ted Kremenekd30ef872009-01-12 23:09:09 +00001110
Anders Carlsson8f031b32009-06-27 04:05:33 +00001111 case Stmt::CallExprClass: {
1112 const CallExpr *CE = cast<CallExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001113 if (const ImplicitCastExpr *ICE
Anders Carlsson8f031b32009-06-27 04:05:33 +00001114 = dyn_cast<ImplicitCastExpr>(CE->getCallee())) {
1115 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1116 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001117 if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) {
Anders Carlsson8f031b32009-06-27 04:05:33 +00001118 unsigned ArgIndex = FA->getFormatIdx();
1119 const Expr *Arg = CE->getArg(ArgIndex - 1);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
1121 return SemaCheckStringLiteral(Arg, TheCall, HasVAListArg,
Ted Kremenek826a3452010-07-16 02:11:22 +00001122 format_idx, firstDataArg, isPrintf);
Anders Carlsson8f031b32009-06-27 04:05:33 +00001123 }
1124 }
1125 }
1126 }
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Anders Carlsson8f031b32009-06-27 04:05:33 +00001128 return false;
1129 }
Ted Kremenek082d9362009-03-20 21:35:28 +00001130 case Stmt::ObjCStringLiteralClass:
1131 case Stmt::StringLiteralClass: {
1132 const StringLiteral *StrE = NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Ted Kremenek082d9362009-03-20 21:35:28 +00001134 if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E))
Ted Kremenekd30ef872009-01-12 23:09:09 +00001135 StrE = ObjCFExpr->getString();
1136 else
Ted Kremenek082d9362009-03-20 21:35:28 +00001137 StrE = cast<StringLiteral>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Ted Kremenekd30ef872009-01-12 23:09:09 +00001139 if (StrE) {
Ted Kremenek826a3452010-07-16 02:11:22 +00001140 CheckFormatString(StrE, E, TheCall, HasVAListArg, format_idx,
1141 firstDataArg, isPrintf);
Ted Kremenekd30ef872009-01-12 23:09:09 +00001142 return true;
1143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Ted Kremenekd30ef872009-01-12 23:09:09 +00001145 return false;
1146 }
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Ted Kremenek082d9362009-03-20 21:35:28 +00001148 default:
1149 return false;
Ted Kremenekd30ef872009-01-12 23:09:09 +00001150 }
1151}
1152
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00001153void
Mike Stump1eb44332009-09-09 15:08:12 +00001154Sema::CheckNonNullArguments(const NonNullAttr *NonNull,
Nick Lewycky909a70d2011-03-25 01:44:32 +00001155 const Expr * const *ExprArgs,
1156 SourceLocation CallSiteLoc) {
Sean Huntcf807c42010-08-18 23:23:40 +00001157 for (NonNullAttr::args_iterator i = NonNull->args_begin(),
1158 e = NonNull->args_end();
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00001159 i != e; ++i) {
Nick Lewycky909a70d2011-03-25 01:44:32 +00001160 const Expr *ArgExpr = ExprArgs[*i];
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001161 if (ArgExpr->isNullPointerConstant(Context,
Douglas Gregorce940492009-09-25 04:25:58 +00001162 Expr::NPC_ValueDependentIsNotNull))
Nick Lewycky909a70d2011-03-25 01:44:32 +00001163 Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
Fariborz Jahaniane898f8a2009-05-21 18:48:51 +00001164 }
1165}
Ted Kremenekd30ef872009-01-12 23:09:09 +00001166
Ted Kremenek826a3452010-07-16 02:11:22 +00001167/// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar
1168/// functions) for correct use of format strings.
Chris Lattner59907c42007-08-10 20:18:51 +00001169void
Ted Kremenek826a3452010-07-16 02:11:22 +00001170Sema::CheckPrintfScanfArguments(const CallExpr *TheCall, bool HasVAListArg,
1171 unsigned format_idx, unsigned firstDataArg,
1172 bool isPrintf) {
1173
Ted Kremenek082d9362009-03-20 21:35:28 +00001174 const Expr *Fn = TheCall->getCallee();
Chris Lattner925e60d2007-12-28 05:29:59 +00001175
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001176 // The way the format attribute works in GCC, the implicit this argument
1177 // of member functions is counted. However, it doesn't appear in our own
1178 // lists, so decrement format_idx in that case.
1179 if (isa<CXXMemberCallExpr>(TheCall)) {
Chandler Carruth9263a302010-11-16 08:49:43 +00001180 const CXXMethodDecl *method_decl =
1181 dyn_cast<CXXMethodDecl>(TheCall->getCalleeDecl());
1182 if (method_decl && method_decl->isInstance()) {
1183 // Catch a format attribute mistakenly referring to the object argument.
1184 if (format_idx == 0)
1185 return;
1186 --format_idx;
1187 if(firstDataArg != 0)
1188 --firstDataArg;
1189 }
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001190 }
1191
Ted Kremenek826a3452010-07-16 02:11:22 +00001192 // CHECK: printf/scanf-like function is called with no format string.
Chris Lattner925e60d2007-12-28 05:29:59 +00001193 if (format_idx >= TheCall->getNumArgs()) {
Ted Kremenek826a3452010-07-16 02:11:22 +00001194 Diag(TheCall->getRParenLoc(), diag::warn_missing_format_string)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001195 << Fn->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +00001196 return;
1197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Ted Kremenek082d9362009-03-20 21:35:28 +00001199 const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts();
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Chris Lattner59907c42007-08-10 20:18:51 +00001201 // CHECK: format string is not a string literal.
Mike Stump1eb44332009-09-09 15:08:12 +00001202 //
Ted Kremenek71895b92007-08-14 17:39:48 +00001203 // Dynamically generated format strings are difficult to
1204 // automatically vet at compile time. Requiring that format strings
1205 // are string literals: (1) permits the checking of format strings by
1206 // the compiler and thereby (2) can practically remove the source of
1207 // many format string exploits.
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001208
Mike Stump1eb44332009-09-09 15:08:12 +00001209 // Format string can be either ObjC string (e.g. @"%d") or
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001210 // C string (e.g. "%d")
Mike Stump1eb44332009-09-09 15:08:12 +00001211 // ObjC string uses the same format specifiers as C string, so we can use
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001212 // the same format string checking logic for both ObjC and C strings.
Chris Lattner1cd3e1f2009-04-29 04:49:34 +00001213 if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx,
Ted Kremenek826a3452010-07-16 02:11:22 +00001214 firstDataArg, isPrintf))
Chris Lattner1cd3e1f2009-04-29 04:49:34 +00001215 return; // Literal format string found, check done!
Ted Kremenek7ff22b22008-06-16 18:00:42 +00001216
Chris Lattner655f1412009-04-29 04:59:47 +00001217 // If there are no arguments specified, warn with -Wformat-security, otherwise
1218 // warn only with -Wformat-nonliteral.
1219 if (TheCall->getNumArgs() == format_idx+1)
Mike Stump1eb44332009-09-09 15:08:12 +00001220 Diag(TheCall->getArg(format_idx)->getLocStart(),
Ted Kremenek826a3452010-07-16 02:11:22 +00001221 diag::warn_format_nonliteral_noargs)
Chris Lattner655f1412009-04-29 04:59:47 +00001222 << OrigFormatExpr->getSourceRange();
1223 else
Mike Stump1eb44332009-09-09 15:08:12 +00001224 Diag(TheCall->getArg(format_idx)->getLocStart(),
Ted Kremenek826a3452010-07-16 02:11:22 +00001225 diag::warn_format_nonliteral)
Chris Lattner655f1412009-04-29 04:59:47 +00001226 << OrigFormatExpr->getSourceRange();
Ted Kremenekd30ef872009-01-12 23:09:09 +00001227}
Ted Kremenek71895b92007-08-14 17:39:48 +00001228
Ted Kremeneke0e53132010-01-28 23:39:18 +00001229namespace {
Ted Kremenek826a3452010-07-16 02:11:22 +00001230class CheckFormatHandler : public analyze_format_string::FormatStringHandler {
1231protected:
Ted Kremeneke0e53132010-01-28 23:39:18 +00001232 Sema &S;
1233 const StringLiteral *FExpr;
1234 const Expr *OrigFormatExpr;
Ted Kremenek6ee76532010-03-25 03:59:12 +00001235 const unsigned FirstDataArg;
Ted Kremeneke0e53132010-01-28 23:39:18 +00001236 const unsigned NumDataArgs;
1237 const bool IsObjCLiteral;
1238 const char *Beg; // Start of format string.
Ted Kremenek0d277352010-01-29 01:06:55 +00001239 const bool HasVAListArg;
1240 const CallExpr *TheCall;
1241 unsigned FormatIdx;
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001242 llvm::BitVector CoveredArgs;
Ted Kremenekefaff192010-02-27 01:41:03 +00001243 bool usesPositionalArgs;
1244 bool atFirstArg;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001245public:
Ted Kremenek826a3452010-07-16 02:11:22 +00001246 CheckFormatHandler(Sema &s, const StringLiteral *fexpr,
Ted Kremenek6ee76532010-03-25 03:59:12 +00001247 const Expr *origFormatExpr, unsigned firstDataArg,
Ted Kremeneke0e53132010-01-28 23:39:18 +00001248 unsigned numDataArgs, bool isObjCLiteral,
Ted Kremenek0d277352010-01-29 01:06:55 +00001249 const char *beg, bool hasVAListArg,
1250 const CallExpr *theCall, unsigned formatIdx)
Ted Kremeneke0e53132010-01-28 23:39:18 +00001251 : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr),
Ted Kremenek6ee76532010-03-25 03:59:12 +00001252 FirstDataArg(firstDataArg),
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001253 NumDataArgs(numDataArgs),
Ted Kremenek0d277352010-01-29 01:06:55 +00001254 IsObjCLiteral(isObjCLiteral), Beg(beg),
1255 HasVAListArg(hasVAListArg),
Ted Kremenekefaff192010-02-27 01:41:03 +00001256 TheCall(theCall), FormatIdx(formatIdx),
1257 usesPositionalArgs(false), atFirstArg(true) {
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001258 CoveredArgs.resize(numDataArgs);
1259 CoveredArgs.reset();
1260 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001261
Ted Kremenek07d161f2010-01-29 01:50:07 +00001262 void DoneProcessing();
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001263
Ted Kremenek826a3452010-07-16 02:11:22 +00001264 void HandleIncompleteSpecifier(const char *startSpecifier,
1265 unsigned specifierLen);
1266
Ted Kremenekefaff192010-02-27 01:41:03 +00001267 virtual void HandleInvalidPosition(const char *startSpecifier,
1268 unsigned specifierLen,
Ted Kremenek826a3452010-07-16 02:11:22 +00001269 analyze_format_string::PositionContext p);
Ted Kremenekefaff192010-02-27 01:41:03 +00001270
1271 virtual void HandleZeroPosition(const char *startPos, unsigned posLen);
1272
Ted Kremeneke0e53132010-01-28 23:39:18 +00001273 void HandleNullChar(const char *nullCharacter);
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001274
Ted Kremenek826a3452010-07-16 02:11:22 +00001275protected:
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001276 bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
1277 const char *startSpec,
1278 unsigned specifierLen,
1279 const char *csStart, unsigned csLen);
1280
Ted Kremenekf88c8e02010-01-29 20:55:36 +00001281 SourceRange getFormatStringRange();
Ted Kremenek826a3452010-07-16 02:11:22 +00001282 CharSourceRange getSpecifierRange(const char *startSpecifier,
1283 unsigned specifierLen);
Ted Kremeneke0e53132010-01-28 23:39:18 +00001284 SourceLocation getLocationOfByte(const char *x);
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001285
Ted Kremenek0d277352010-01-29 01:06:55 +00001286 const Expr *getDataArg(unsigned i) const;
Ted Kremenek666a1972010-07-26 19:45:42 +00001287
1288 bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS,
1289 const analyze_format_string::ConversionSpecifier &CS,
1290 const char *startSpecifier, unsigned specifierLen,
1291 unsigned argIndex);
Ted Kremeneke0e53132010-01-28 23:39:18 +00001292};
1293}
1294
Ted Kremenek826a3452010-07-16 02:11:22 +00001295SourceRange CheckFormatHandler::getFormatStringRange() {
Ted Kremeneke0e53132010-01-28 23:39:18 +00001296 return OrigFormatExpr->getSourceRange();
1297}
1298
Ted Kremenek826a3452010-07-16 02:11:22 +00001299CharSourceRange CheckFormatHandler::
1300getSpecifierRange(const char *startSpecifier, unsigned specifierLen) {
Tom Care45f9b7e2010-06-21 21:21:01 +00001301 SourceLocation Start = getLocationOfByte(startSpecifier);
1302 SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1);
1303
1304 // Advance the end SourceLocation by one due to half-open ranges.
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00001305 End = End.getLocWithOffset(1);
Tom Care45f9b7e2010-06-21 21:21:01 +00001306
1307 return CharSourceRange::getCharRange(Start, End);
Ted Kremenekf88c8e02010-01-29 20:55:36 +00001308}
1309
Ted Kremenek826a3452010-07-16 02:11:22 +00001310SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) {
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001311 return S.getLocationOfStringLiteralByte(FExpr, x - Beg);
Ted Kremeneke0e53132010-01-28 23:39:18 +00001312}
1313
Ted Kremenek826a3452010-07-16 02:11:22 +00001314void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier,
1315 unsigned specifierLen){
Ted Kremenek808015a2010-01-29 03:16:21 +00001316 SourceLocation Loc = getLocationOfByte(startSpecifier);
1317 S.Diag(Loc, diag::warn_printf_incomplete_specifier)
Ted Kremenek826a3452010-07-16 02:11:22 +00001318 << getSpecifierRange(startSpecifier, specifierLen);
Ted Kremenek808015a2010-01-29 03:16:21 +00001319}
1320
Ted Kremenekefaff192010-02-27 01:41:03 +00001321void
Ted Kremenek826a3452010-07-16 02:11:22 +00001322CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen,
1323 analyze_format_string::PositionContext p) {
Ted Kremenekefaff192010-02-27 01:41:03 +00001324 SourceLocation Loc = getLocationOfByte(startPos);
Ted Kremenek826a3452010-07-16 02:11:22 +00001325 S.Diag(Loc, diag::warn_format_invalid_positional_specifier)
1326 << (unsigned) p << getSpecifierRange(startPos, posLen);
Ted Kremenekefaff192010-02-27 01:41:03 +00001327}
1328
Ted Kremenek826a3452010-07-16 02:11:22 +00001329void CheckFormatHandler::HandleZeroPosition(const char *startPos,
Ted Kremenekefaff192010-02-27 01:41:03 +00001330 unsigned posLen) {
1331 SourceLocation Loc = getLocationOfByte(startPos);
Ted Kremenek826a3452010-07-16 02:11:22 +00001332 S.Diag(Loc, diag::warn_format_zero_positional_specifier)
1333 << getSpecifierRange(startPos, posLen);
Ted Kremenekefaff192010-02-27 01:41:03 +00001334}
1335
Ted Kremenek826a3452010-07-16 02:11:22 +00001336void CheckFormatHandler::HandleNullChar(const char *nullCharacter) {
Ted Kremenek0c069442011-03-15 21:18:48 +00001337 if (!IsObjCLiteral) {
1338 // The presence of a null character is likely an error.
1339 S.Diag(getLocationOfByte(nullCharacter),
1340 diag::warn_printf_format_string_contains_null_char)
1341 << getFormatStringRange();
1342 }
Ted Kremenek826a3452010-07-16 02:11:22 +00001343}
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001344
Ted Kremenek826a3452010-07-16 02:11:22 +00001345const Expr *CheckFormatHandler::getDataArg(unsigned i) const {
1346 return TheCall->getArg(FirstDataArg + i);
1347}
1348
1349void CheckFormatHandler::DoneProcessing() {
1350 // Does the number of data arguments exceed the number of
1351 // format conversions in the format string?
1352 if (!HasVAListArg) {
1353 // Find any arguments that weren't covered.
1354 CoveredArgs.flip();
1355 signed notCoveredArg = CoveredArgs.find_first();
1356 if (notCoveredArg >= 0) {
1357 assert((unsigned)notCoveredArg < NumDataArgs);
1358 S.Diag(getDataArg((unsigned) notCoveredArg)->getLocStart(),
1359 diag::warn_printf_data_arg_not_used)
1360 << getFormatStringRange();
1361 }
1362 }
1363}
1364
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001365bool
1366CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
1367 SourceLocation Loc,
1368 const char *startSpec,
1369 unsigned specifierLen,
1370 const char *csStart,
1371 unsigned csLen) {
1372
1373 bool keepGoing = true;
1374 if (argIndex < NumDataArgs) {
1375 // Consider the argument coverered, even though the specifier doesn't
1376 // make sense.
1377 CoveredArgs.set(argIndex);
1378 }
1379 else {
1380 // If argIndex exceeds the number of data arguments we
1381 // don't issue a warning because that is just a cascade of warnings (and
1382 // they may have intended '%%' anyway). We don't want to continue processing
1383 // the format string after this point, however, as we will like just get
1384 // gibberish when trying to match arguments.
1385 keepGoing = false;
1386 }
1387
1388 S.Diag(Loc, diag::warn_format_invalid_conversion)
Chris Lattner5f9e2722011-07-23 10:55:15 +00001389 << StringRef(csStart, csLen)
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001390 << getSpecifierRange(startSpec, specifierLen);
1391
1392 return keepGoing;
1393}
1394
Ted Kremenek666a1972010-07-26 19:45:42 +00001395bool
1396CheckFormatHandler::CheckNumArgs(
1397 const analyze_format_string::FormatSpecifier &FS,
1398 const analyze_format_string::ConversionSpecifier &CS,
1399 const char *startSpecifier, unsigned specifierLen, unsigned argIndex) {
1400
1401 if (argIndex >= NumDataArgs) {
1402 if (FS.usesPositionalArg()) {
1403 S.Diag(getLocationOfByte(CS.getStart()),
1404 diag::warn_printf_positional_arg_exceeds_data_args)
1405 << (argIndex+1) << NumDataArgs
1406 << getSpecifierRange(startSpecifier, specifierLen);
1407 }
1408 else {
1409 S.Diag(getLocationOfByte(CS.getStart()),
1410 diag::warn_printf_insufficient_data_args)
1411 << getSpecifierRange(startSpecifier, specifierLen);
1412 }
1413
1414 return false;
1415 }
1416 return true;
1417}
1418
Ted Kremenek826a3452010-07-16 02:11:22 +00001419//===--- CHECK: Printf format string checking ------------------------------===//
1420
1421namespace {
1422class CheckPrintfHandler : public CheckFormatHandler {
1423public:
1424 CheckPrintfHandler(Sema &s, const StringLiteral *fexpr,
1425 const Expr *origFormatExpr, unsigned firstDataArg,
1426 unsigned numDataArgs, bool isObjCLiteral,
1427 const char *beg, bool hasVAListArg,
1428 const CallExpr *theCall, unsigned formatIdx)
1429 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
1430 numDataArgs, isObjCLiteral, beg, hasVAListArg,
1431 theCall, formatIdx) {}
1432
1433
1434 bool HandleInvalidPrintfConversionSpecifier(
1435 const analyze_printf::PrintfSpecifier &FS,
1436 const char *startSpecifier,
1437 unsigned specifierLen);
1438
1439 bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS,
1440 const char *startSpecifier,
1441 unsigned specifierLen);
1442
1443 bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k,
1444 const char *startSpecifier, unsigned specifierLen);
1445 void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS,
1446 const analyze_printf::OptionalAmount &Amt,
1447 unsigned type,
1448 const char *startSpecifier, unsigned specifierLen);
1449 void HandleFlag(const analyze_printf::PrintfSpecifier &FS,
1450 const analyze_printf::OptionalFlag &flag,
1451 const char *startSpecifier, unsigned specifierLen);
1452 void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS,
1453 const analyze_printf::OptionalFlag &ignoredFlag,
1454 const analyze_printf::OptionalFlag &flag,
1455 const char *startSpecifier, unsigned specifierLen);
1456};
1457}
1458
1459bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier(
1460 const analyze_printf::PrintfSpecifier &FS,
1461 const char *startSpecifier,
1462 unsigned specifierLen) {
Ted Kremenek6ecb9502010-07-20 20:04:27 +00001463 const analyze_printf::PrintfConversionSpecifier &CS =
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001464 FS.getConversionSpecifier();
Ted Kremenek826a3452010-07-16 02:11:22 +00001465
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001466 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
1467 getLocationOfByte(CS.getStart()),
1468 startSpecifier, specifierLen,
1469 CS.getStart(), CS.getLength());
Ted Kremenek26ac2e02010-01-29 02:40:24 +00001470}
1471
Ted Kremenek826a3452010-07-16 02:11:22 +00001472bool CheckPrintfHandler::HandleAmount(
1473 const analyze_format_string::OptionalAmount &Amt,
1474 unsigned k, const char *startSpecifier,
1475 unsigned specifierLen) {
Ted Kremenek0d277352010-01-29 01:06:55 +00001476
1477 if (Amt.hasDataArgument()) {
Ted Kremenek0d277352010-01-29 01:06:55 +00001478 if (!HasVAListArg) {
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001479 unsigned argIndex = Amt.getArgIndex();
1480 if (argIndex >= NumDataArgs) {
Ted Kremenekefaff192010-02-27 01:41:03 +00001481 S.Diag(getLocationOfByte(Amt.getStart()),
1482 diag::warn_printf_asterisk_missing_arg)
Ted Kremenek826a3452010-07-16 02:11:22 +00001483 << k << getSpecifierRange(startSpecifier, specifierLen);
Ted Kremenek0d277352010-01-29 01:06:55 +00001484 // Don't do any more checking. We will just emit
1485 // spurious errors.
1486 return false;
1487 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001488
Ted Kremenek0d277352010-01-29 01:06:55 +00001489 // Type check the data argument. It should be an 'int'.
Ted Kremenek31f8e322010-01-29 23:32:22 +00001490 // Although not in conformance with C99, we also allow the argument to be
1491 // an 'unsigned int' as that is a reasonably safe case. GCC also
1492 // doesn't emit a warning for that case.
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001493 CoveredArgs.set(argIndex);
1494 const Expr *Arg = getDataArg(argIndex);
Ted Kremenek0d277352010-01-29 01:06:55 +00001495 QualType T = Arg->getType();
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001496
1497 const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context);
1498 assert(ATR.isValid());
1499
1500 if (!ATR.matchesType(S.Context, T)) {
Ted Kremenekefaff192010-02-27 01:41:03 +00001501 S.Diag(getLocationOfByte(Amt.getStart()),
1502 diag::warn_printf_asterisk_wrong_type)
1503 << k
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001504 << ATR.getRepresentativeType(S.Context) << T
Ted Kremenek826a3452010-07-16 02:11:22 +00001505 << getSpecifierRange(startSpecifier, specifierLen)
Ted Kremenekd635c5f2010-01-30 00:49:51 +00001506 << Arg->getSourceRange();
Ted Kremenek0d277352010-01-29 01:06:55 +00001507 // Don't do any more checking. We will just emit
1508 // spurious errors.
1509 return false;
1510 }
1511 }
1512 }
1513 return true;
1514}
Ted Kremenek0d277352010-01-29 01:06:55 +00001515
Tom Caree4ee9662010-06-17 19:00:27 +00001516void CheckPrintfHandler::HandleInvalidAmount(
Ted Kremenek826a3452010-07-16 02:11:22 +00001517 const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00001518 const analyze_printf::OptionalAmount &Amt,
1519 unsigned type,
1520 const char *startSpecifier,
1521 unsigned specifierLen) {
Ted Kremenek6ecb9502010-07-20 20:04:27 +00001522 const analyze_printf::PrintfConversionSpecifier &CS =
1523 FS.getConversionSpecifier();
Tom Caree4ee9662010-06-17 19:00:27 +00001524 switch (Amt.getHowSpecified()) {
1525 case analyze_printf::OptionalAmount::Constant:
1526 S.Diag(getLocationOfByte(Amt.getStart()),
1527 diag::warn_printf_nonsensical_optional_amount)
1528 << type
1529 << CS.toString()
Ted Kremenek826a3452010-07-16 02:11:22 +00001530 << getSpecifierRange(startSpecifier, specifierLen)
1531 << FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(),
Tom Caree4ee9662010-06-17 19:00:27 +00001532 Amt.getConstantLength()));
1533 break;
1534
1535 default:
1536 S.Diag(getLocationOfByte(Amt.getStart()),
1537 diag::warn_printf_nonsensical_optional_amount)
1538 << type
1539 << CS.toString()
Ted Kremenek826a3452010-07-16 02:11:22 +00001540 << getSpecifierRange(startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00001541 break;
1542 }
1543}
1544
Ted Kremenek826a3452010-07-16 02:11:22 +00001545void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00001546 const analyze_printf::OptionalFlag &flag,
1547 const char *startSpecifier,
1548 unsigned specifierLen) {
1549 // Warn about pointless flag with a fixit removal.
Ted Kremenek6ecb9502010-07-20 20:04:27 +00001550 const analyze_printf::PrintfConversionSpecifier &CS =
1551 FS.getConversionSpecifier();
Tom Caree4ee9662010-06-17 19:00:27 +00001552 S.Diag(getLocationOfByte(flag.getPosition()),
1553 diag::warn_printf_nonsensical_flag)
1554 << flag.toString() << CS.toString()
Ted Kremenek826a3452010-07-16 02:11:22 +00001555 << getSpecifierRange(startSpecifier, specifierLen)
1556 << FixItHint::CreateRemoval(getSpecifierRange(flag.getPosition(), 1));
Tom Caree4ee9662010-06-17 19:00:27 +00001557}
1558
1559void CheckPrintfHandler::HandleIgnoredFlag(
Ted Kremenek826a3452010-07-16 02:11:22 +00001560 const analyze_printf::PrintfSpecifier &FS,
Tom Caree4ee9662010-06-17 19:00:27 +00001561 const analyze_printf::OptionalFlag &ignoredFlag,
1562 const analyze_printf::OptionalFlag &flag,
1563 const char *startSpecifier,
1564 unsigned specifierLen) {
1565 // Warn about ignored flag with a fixit removal.
1566 S.Diag(getLocationOfByte(ignoredFlag.getPosition()),
1567 diag::warn_printf_ignored_flag)
1568 << ignoredFlag.toString() << flag.toString()
Ted Kremenek826a3452010-07-16 02:11:22 +00001569 << getSpecifierRange(startSpecifier, specifierLen)
1570 << FixItHint::CreateRemoval(getSpecifierRange(
Tom Caree4ee9662010-06-17 19:00:27 +00001571 ignoredFlag.getPosition(), 1));
1572}
1573
Ted Kremeneke0e53132010-01-28 23:39:18 +00001574bool
Ted Kremenek826a3452010-07-16 02:11:22 +00001575CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
Ted Kremenek5c41ee82010-02-11 09:27:41 +00001576 &FS,
Ted Kremeneke0e53132010-01-28 23:39:18 +00001577 const char *startSpecifier,
1578 unsigned specifierLen) {
1579
Ted Kremenek6ecb9502010-07-20 20:04:27 +00001580 using namespace analyze_format_string;
Ted Kremenekefaff192010-02-27 01:41:03 +00001581 using namespace analyze_printf;
Ted Kremenek6ecb9502010-07-20 20:04:27 +00001582 const PrintfConversionSpecifier &CS = FS.getConversionSpecifier();
Ted Kremeneke0e53132010-01-28 23:39:18 +00001583
Ted Kremenekbaa40062010-07-19 22:01:06 +00001584 if (FS.consumesDataArgument()) {
1585 if (atFirstArg) {
1586 atFirstArg = false;
1587 usesPositionalArgs = FS.usesPositionalArg();
1588 }
1589 else if (usesPositionalArgs != FS.usesPositionalArg()) {
1590 // Cannot mix-and-match positional and non-positional arguments.
1591 S.Diag(getLocationOfByte(CS.getStart()),
1592 diag::warn_format_mix_positional_nonpositional_args)
1593 << getSpecifierRange(startSpecifier, specifierLen);
1594 return false;
1595 }
Ted Kremenek0d277352010-01-29 01:06:55 +00001596 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001597
Ted Kremenekefaff192010-02-27 01:41:03 +00001598 // First check if the field width, precision, and conversion specifier
1599 // have matching data arguments.
1600 if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0,
1601 startSpecifier, specifierLen)) {
1602 return false;
1603 }
1604
1605 if (!HandleAmount(FS.getPrecision(), /* precision */ 1,
1606 startSpecifier, specifierLen)) {
Ted Kremenek0d277352010-01-29 01:06:55 +00001607 return false;
1608 }
1609
Ted Kremenekf88c8e02010-01-29 20:55:36 +00001610 if (!CS.consumesDataArgument()) {
1611 // FIXME: Technically specifying a precision or field width here
1612 // makes no sense. Worth issuing a warning at some point.
Ted Kremenek0e5675d2010-02-10 02:16:30 +00001613 return true;
Ted Kremenekf88c8e02010-01-29 20:55:36 +00001614 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001615
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001616 // Consume the argument.
1617 unsigned argIndex = FS.getArgIndex();
Ted Kremeneke3fc5472010-02-27 08:34:51 +00001618 if (argIndex < NumDataArgs) {
1619 // The check to see if the argIndex is valid will come later.
1620 // We set the bit here because we may exit early from this
1621 // function if we encounter some other error.
1622 CoveredArgs.set(argIndex);
1623 }
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001624
1625 // Check for using an Objective-C specific conversion specifier
1626 // in a non-ObjC literal.
1627 if (!IsObjCLiteral && CS.isObjCArg()) {
Ted Kremenek826a3452010-07-16 02:11:22 +00001628 return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier,
1629 specifierLen);
Ted Kremenek7f70dc82010-02-26 19:18:41 +00001630 }
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001631
Tom Caree4ee9662010-06-17 19:00:27 +00001632 // Check for invalid use of field width
1633 if (!FS.hasValidFieldWidth()) {
Tom Care45f9b7e2010-06-21 21:21:01 +00001634 HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0,
Tom Caree4ee9662010-06-17 19:00:27 +00001635 startSpecifier, specifierLen);
1636 }
1637
1638 // Check for invalid use of precision
1639 if (!FS.hasValidPrecision()) {
1640 HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1,
1641 startSpecifier, specifierLen);
1642 }
1643
1644 // Check each flag does not conflict with any other component.
Ted Kremenek65197b42011-01-08 05:28:46 +00001645 if (!FS.hasValidThousandsGroupingPrefix())
1646 HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00001647 if (!FS.hasValidLeadingZeros())
1648 HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen);
1649 if (!FS.hasValidPlusPrefix())
1650 HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen);
Tom Care45f9b7e2010-06-21 21:21:01 +00001651 if (!FS.hasValidSpacePrefix())
1652 HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00001653 if (!FS.hasValidAlternativeForm())
1654 HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen);
1655 if (!FS.hasValidLeftJustified())
1656 HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen);
1657
1658 // Check that flags are not ignored by another flag
Tom Care45f9b7e2010-06-21 21:21:01 +00001659 if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+'
1660 HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(),
1661 startSpecifier, specifierLen);
Tom Caree4ee9662010-06-17 19:00:27 +00001662 if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-'
1663 HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(),
1664 startSpecifier, specifierLen);
1665
1666 // Check the length modifier is valid with the given conversion specifier.
1667 const LengthModifier &LM = FS.getLengthModifier();
1668 if (!FS.hasValidLengthModifier())
1669 S.Diag(getLocationOfByte(LM.getStart()),
Ted Kremenek649aecf2010-07-20 20:03:43 +00001670 diag::warn_format_nonsensical_length)
Tom Caree4ee9662010-06-17 19:00:27 +00001671 << LM.toString() << CS.toString()
Ted Kremenek826a3452010-07-16 02:11:22 +00001672 << getSpecifierRange(startSpecifier, specifierLen)
1673 << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(),
Tom Caree4ee9662010-06-17 19:00:27 +00001674 LM.getLength()));
1675
1676 // Are we using '%n'?
Ted Kremenek35d353b2010-07-20 20:04:10 +00001677 if (CS.getKind() == ConversionSpecifier::nArg) {
Tom Caree4ee9662010-06-17 19:00:27 +00001678 // Issue a warning about this being a possible security issue.
Ted Kremeneke82d8042010-01-29 01:35:25 +00001679 S.Diag(getLocationOfByte(CS.getStart()), diag::warn_printf_write_back)
Ted Kremenek826a3452010-07-16 02:11:22 +00001680 << getSpecifierRange(startSpecifier, specifierLen);
Ted Kremeneke82d8042010-01-29 01:35:25 +00001681 // Continue checking the other format specifiers.
1682 return true;
1683 }
Ted Kremenek5c41ee82010-02-11 09:27:41 +00001684
Ted Kremenekda51f0d2010-01-29 01:43:31 +00001685 // The remaining checks depend on the data arguments.
1686 if (HasVAListArg)
1687 return true;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001688
Ted Kremenek666a1972010-07-26 19:45:42 +00001689 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
Ted Kremenekda51f0d2010-01-29 01:43:31 +00001690 return false;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00001691
Michael J. Spencer96827eb2010-07-27 04:46:02 +00001692 // Now type check the data expression that matches the
1693 // format specifier.
1694 const Expr *Ex = getDataArg(argIndex);
1695 const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context);
1696 if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) {
1697 // Check if we didn't match because of an implicit cast from a 'char'
1698 // or 'short' to an 'int'. This is done because printf is a varargs
1699 // function.
1700 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex))
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +00001701 if (ICE->getType() == S.Context.IntTy) {
1702 // All further checking is done on the subexpression.
1703 Ex = ICE->getSubExpr();
1704 if (ATR.matchesType(S.Context, Ex->getType()))
Michael J. Spencer96827eb2010-07-27 04:46:02 +00001705 return true;
Ted Kremenek4d8ae4d2010-10-21 04:00:58 +00001706 }
Michael J. Spencer96827eb2010-07-27 04:46:02 +00001707
1708 // We may be able to offer a FixItHint if it is a supported type.
1709 PrintfSpecifier fixedFS = FS;
1710 bool success = fixedFS.fixType(Ex->getType());
1711
1712 if (success) {
1713 // Get the fix string from the fixed format specifier
1714 llvm::SmallString<128> buf;
1715 llvm::raw_svector_ostream os(buf);
1716 fixedFS.toString(os);
1717
Ted Kremenek9325eaf2010-08-24 22:24:51 +00001718 // FIXME: getRepresentativeType() perhaps should return a string
1719 // instead of a QualType to better handle when the representative
1720 // type is 'wint_t' (which is defined in the system headers).
Michael J. Spencer96827eb2010-07-27 04:46:02 +00001721 S.Diag(getLocationOfByte(CS.getStart()),
1722 diag::warn_printf_conversion_argument_type_mismatch)
1723 << ATR.getRepresentativeType(S.Context) << Ex->getType()
1724 << getSpecifierRange(startSpecifier, specifierLen)
1725 << Ex->getSourceRange()
1726 << FixItHint::CreateReplacement(
1727 getSpecifierRange(startSpecifier, specifierLen),
1728 os.str());
1729 }
1730 else {
1731 S.Diag(getLocationOfByte(CS.getStart()),
1732 diag::warn_printf_conversion_argument_type_mismatch)
1733 << ATR.getRepresentativeType(S.Context) << Ex->getType()
1734 << getSpecifierRange(startSpecifier, specifierLen)
1735 << Ex->getSourceRange();
1736 }
1737 }
1738
Ted Kremeneke0e53132010-01-28 23:39:18 +00001739 return true;
1740}
1741
Ted Kremenek826a3452010-07-16 02:11:22 +00001742//===--- CHECK: Scanf format string checking ------------------------------===//
1743
1744namespace {
1745class CheckScanfHandler : public CheckFormatHandler {
1746public:
1747 CheckScanfHandler(Sema &s, const StringLiteral *fexpr,
1748 const Expr *origFormatExpr, unsigned firstDataArg,
1749 unsigned numDataArgs, bool isObjCLiteral,
1750 const char *beg, bool hasVAListArg,
1751 const CallExpr *theCall, unsigned formatIdx)
1752 : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg,
1753 numDataArgs, isObjCLiteral, beg, hasVAListArg,
1754 theCall, formatIdx) {}
1755
1756 bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
1757 const char *startSpecifier,
1758 unsigned specifierLen);
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001759
1760 bool HandleInvalidScanfConversionSpecifier(
1761 const analyze_scanf::ScanfSpecifier &FS,
1762 const char *startSpecifier,
1763 unsigned specifierLen);
Ted Kremenekb7c21012010-07-16 18:28:03 +00001764
1765 void HandleIncompleteScanList(const char *start, const char *end);
Ted Kremenek826a3452010-07-16 02:11:22 +00001766};
Ted Kremenek07d161f2010-01-29 01:50:07 +00001767}
Ted Kremeneke0e53132010-01-28 23:39:18 +00001768
Ted Kremenekb7c21012010-07-16 18:28:03 +00001769void CheckScanfHandler::HandleIncompleteScanList(const char *start,
1770 const char *end) {
1771 S.Diag(getLocationOfByte(end), diag::warn_scanf_scanlist_incomplete)
1772 << getSpecifierRange(start, end - start);
1773}
1774
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001775bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
1776 const analyze_scanf::ScanfSpecifier &FS,
1777 const char *startSpecifier,
1778 unsigned specifierLen) {
1779
Ted Kremenek6ecb9502010-07-20 20:04:27 +00001780 const analyze_scanf::ScanfConversionSpecifier &CS =
Ted Kremenekc09b6a52010-07-19 21:25:57 +00001781 FS.getConversionSpecifier();
1782
1783 return HandleInvalidConversionSpecifier(FS.getArgIndex(),
1784 getLocationOfByte(CS.getStart()),
1785 startSpecifier, specifierLen,
1786 CS.getStart(), CS.getLength());
1787}
1788
Ted Kremenek826a3452010-07-16 02:11:22 +00001789bool CheckScanfHandler::HandleScanfSpecifier(
1790 const analyze_scanf::ScanfSpecifier &FS,
1791 const char *startSpecifier,
1792 unsigned specifierLen) {
1793
1794 using namespace analyze_scanf;
1795 using namespace analyze_format_string;
1796
Ted Kremenek6ecb9502010-07-20 20:04:27 +00001797 const ScanfConversionSpecifier &CS = FS.getConversionSpecifier();
Ted Kremenek826a3452010-07-16 02:11:22 +00001798
Ted Kremenekbaa40062010-07-19 22:01:06 +00001799 // Handle case where '%' and '*' don't consume an argument. These shouldn't
1800 // be used to decide if we are using positional arguments consistently.
1801 if (FS.consumesDataArgument()) {
1802 if (atFirstArg) {
1803 atFirstArg = false;
1804 usesPositionalArgs = FS.usesPositionalArg();
1805 }
1806 else if (usesPositionalArgs != FS.usesPositionalArg()) {
1807 // Cannot mix-and-match positional and non-positional arguments.
1808 S.Diag(getLocationOfByte(CS.getStart()),
1809 diag::warn_format_mix_positional_nonpositional_args)
1810 << getSpecifierRange(startSpecifier, specifierLen);
1811 return false;
1812 }
Ted Kremenek826a3452010-07-16 02:11:22 +00001813 }
1814
1815 // Check if the field with is non-zero.
1816 const OptionalAmount &Amt = FS.getFieldWidth();
1817 if (Amt.getHowSpecified() == OptionalAmount::Constant) {
1818 if (Amt.getConstantAmount() == 0) {
1819 const CharSourceRange &R = getSpecifierRange(Amt.getStart(),
1820 Amt.getConstantLength());
1821 S.Diag(getLocationOfByte(Amt.getStart()),
1822 diag::warn_scanf_nonzero_width)
1823 << R << FixItHint::CreateRemoval(R);
1824 }
1825 }
1826
1827 if (!FS.consumesDataArgument()) {
1828 // FIXME: Technically specifying a precision or field width here
1829 // makes no sense. Worth issuing a warning at some point.
1830 return true;
1831 }
1832
1833 // Consume the argument.
1834 unsigned argIndex = FS.getArgIndex();
1835 if (argIndex < NumDataArgs) {
1836 // The check to see if the argIndex is valid will come later.
1837 // We set the bit here because we may exit early from this
1838 // function if we encounter some other error.
1839 CoveredArgs.set(argIndex);
1840 }
1841
Ted Kremenek1e51c202010-07-20 20:04:47 +00001842 // Check the length modifier is valid with the given conversion specifier.
1843 const LengthModifier &LM = FS.getLengthModifier();
1844 if (!FS.hasValidLengthModifier()) {
1845 S.Diag(getLocationOfByte(LM.getStart()),
1846 diag::warn_format_nonsensical_length)
1847 << LM.toString() << CS.toString()
1848 << getSpecifierRange(startSpecifier, specifierLen)
1849 << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(),
1850 LM.getLength()));
1851 }
1852
Ted Kremenek826a3452010-07-16 02:11:22 +00001853 // The remaining checks depend on the data arguments.
1854 if (HasVAListArg)
1855 return true;
1856
Ted Kremenek666a1972010-07-26 19:45:42 +00001857 if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex))
Ted Kremenek826a3452010-07-16 02:11:22 +00001858 return false;
Ted Kremenek826a3452010-07-16 02:11:22 +00001859
1860 // FIXME: Check that the argument type matches the format specifier.
1861
1862 return true;
1863}
1864
1865void Sema::CheckFormatString(const StringLiteral *FExpr,
Ted Kremenek0e5675d2010-02-10 02:16:30 +00001866 const Expr *OrigFormatExpr,
1867 const CallExpr *TheCall, bool HasVAListArg,
Ted Kremenek826a3452010-07-16 02:11:22 +00001868 unsigned format_idx, unsigned firstDataArg,
1869 bool isPrintf) {
1870
Ted Kremeneke0e53132010-01-28 23:39:18 +00001871 // CHECK: is the format string a wide literal?
Douglas Gregor5cee1192011-07-27 05:40:30 +00001872 if (!FExpr->isAscii()) {
Ted Kremeneke0e53132010-01-28 23:39:18 +00001873 Diag(FExpr->getLocStart(),
Ted Kremenek826a3452010-07-16 02:11:22 +00001874 diag::warn_format_string_is_wide_literal)
Ted Kremeneke0e53132010-01-28 23:39:18 +00001875 << OrigFormatExpr->getSourceRange();
1876 return;
1877 }
Ted Kremenek826a3452010-07-16 02:11:22 +00001878
Ted Kremeneke0e53132010-01-28 23:39:18 +00001879 // Str - The format string. NOTE: this is NOT null-terminated!
Chris Lattner5f9e2722011-07-23 10:55:15 +00001880 StringRef StrRef = FExpr->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +00001881 const char *Str = StrRef.data();
1882 unsigned StrLen = StrRef.size();
Ted Kremenek4cd57912011-09-29 05:52:16 +00001883 const unsigned numDataArgs = TheCall->getNumArgs() - firstDataArg;
Ted Kremenek826a3452010-07-16 02:11:22 +00001884
Ted Kremeneke0e53132010-01-28 23:39:18 +00001885 // CHECK: empty format string?
Ted Kremenek4cd57912011-09-29 05:52:16 +00001886 if (StrLen == 0 && numDataArgs > 0) {
Ted Kremenek826a3452010-07-16 02:11:22 +00001887 Diag(FExpr->getLocStart(), diag::warn_empty_format_string)
Ted Kremeneke0e53132010-01-28 23:39:18 +00001888 << OrigFormatExpr->getSourceRange();
1889 return;
1890 }
Ted Kremenek826a3452010-07-16 02:11:22 +00001891
1892 if (isPrintf) {
1893 CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
Ted Kremenek4cd57912011-09-29 05:52:16 +00001894 numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr),
1895 Str, HasVAListArg, TheCall, format_idx);
Ted Kremenek826a3452010-07-16 02:11:22 +00001896
1897 if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen))
1898 H.DoneProcessing();
1899 }
1900 else {
1901 CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
Ted Kremenek4cd57912011-09-29 05:52:16 +00001902 numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr),
1903 Str, HasVAListArg, TheCall, format_idx);
Ted Kremenek826a3452010-07-16 02:11:22 +00001904
1905 if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen))
1906 H.DoneProcessing();
1907 }
Ted Kremenekce7024e2010-01-28 01:18:22 +00001908}
1909
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00001910//===--- CHECK: Standard memory functions ---------------------------------===//
1911
Douglas Gregor2a053a32011-05-03 20:05:22 +00001912/// \brief Determine whether the given type is a dynamic class type (e.g.,
1913/// whether it has a vtable).
1914static bool isDynamicClassType(QualType T) {
1915 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl())
1916 if (CXXRecordDecl *Definition = Record->getDefinition())
1917 if (Definition->isDynamicClass())
1918 return true;
1919
1920 return false;
1921}
1922
Chandler Carrutha72a12f2011-06-21 23:04:20 +00001923/// \brief If E is a sizeof expression, returns its argument expression,
Chandler Carruth000d4282011-06-16 09:09:40 +00001924/// otherwise returns NULL.
1925static const Expr *getSizeOfExprArg(const Expr* E) {
Nico Webere4a1c642011-06-14 16:14:58 +00001926 if (const UnaryExprOrTypeTraitExpr *SizeOf =
Chandler Carruth000d4282011-06-16 09:09:40 +00001927 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
1928 if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType())
1929 return SizeOf->getArgumentExpr()->IgnoreParenImpCasts();
Nico Webere4a1c642011-06-14 16:14:58 +00001930
Chandler Carruth000d4282011-06-16 09:09:40 +00001931 return 0;
1932}
1933
Chandler Carrutha72a12f2011-06-21 23:04:20 +00001934/// \brief If E is a sizeof expression, returns its argument type.
Chandler Carruth000d4282011-06-16 09:09:40 +00001935static QualType getSizeOfArgType(const Expr* E) {
1936 if (const UnaryExprOrTypeTraitExpr *SizeOf =
1937 dyn_cast<UnaryExprOrTypeTraitExpr>(E))
1938 if (SizeOf->getKind() == clang::UETT_SizeOf)
1939 return SizeOf->getTypeOfArgument();
1940
1941 return QualType();
Nico Webere4a1c642011-06-14 16:14:58 +00001942}
1943
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00001944/// \brief Check for dangerous or invalid arguments to memset().
1945///
Chandler Carruth929f0132011-06-03 06:23:57 +00001946/// This issues warnings on known problematic, dangerous or unspecified
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00001947/// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp'
1948/// function calls.
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00001949///
1950/// \param Call The call expression to diagnose.
Matt Beaumont-Gaycc2f30c2011-08-05 00:22:34 +00001951void Sema::CheckMemaccessArguments(const CallExpr *Call,
1952 CheckedMemoryFunction CMF,
1953 IdentifierInfo *FnName) {
Ted Kremenek1d59f7f2011-04-28 01:38:02 +00001954 // It is possible to have a non-standard definition of memset. Validate
Douglas Gregor707a23e2011-06-16 17:56:04 +00001955 // we have enough arguments, and if not, abort further checking.
1956 if (Call->getNumArgs() < 3)
Ted Kremenek1d59f7f2011-04-28 01:38:02 +00001957 return;
1958
Douglas Gregor707a23e2011-06-16 17:56:04 +00001959 unsigned LastArg = (CMF == CMF_Memset? 1 : 2);
Nico Webere4a1c642011-06-14 16:14:58 +00001960 const Expr *LenExpr = Call->getArg(2)->IgnoreParenImpCasts();
Chandler Carruth000d4282011-06-16 09:09:40 +00001961
1962 // We have special checking when the length is a sizeof expression.
1963 QualType SizeOfArgTy = getSizeOfArgType(LenExpr);
1964 const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
1965 llvm::FoldingSetNodeID SizeOfArgID;
1966
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00001967 for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) {
1968 const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts();
Nico Webere4a1c642011-06-14 16:14:58 +00001969 SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange();
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00001970
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00001971 QualType DestTy = Dest->getType();
1972 if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
1973 QualType PointeeTy = DestPtrTy->getPointeeType();
John McCallf85e1932011-06-15 23:02:42 +00001974
Chandler Carruth000d4282011-06-16 09:09:40 +00001975 // Never warn about void type pointers. This can be used to suppress
1976 // false positives.
1977 if (PointeeTy->isVoidType())
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00001978 continue;
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00001979
Chandler Carruth000d4282011-06-16 09:09:40 +00001980 // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
1981 // actually comparing the expressions for equality. Because computing the
1982 // expression IDs can be expensive, we only do this if the diagnostic is
1983 // enabled.
1984 if (SizeOfArg &&
1985 Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess,
1986 SizeOfArg->getExprLoc())) {
1987 // We only compute IDs for expressions if the warning is enabled, and
1988 // cache the sizeof arg's ID.
1989 if (SizeOfArgID == llvm::FoldingSetNodeID())
1990 SizeOfArg->Profile(SizeOfArgID, Context, true);
1991 llvm::FoldingSetNodeID DestID;
1992 Dest->Profile(DestID, Context, true);
1993 if (DestID == SizeOfArgID) {
1994 unsigned ActionIdx = 0; // Default is to suggest dereferencing.
1995 if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
1996 if (UnaryOp->getOpcode() == UO_AddrOf)
1997 ActionIdx = 1; // If its an address-of operator, just remove it.
1998 if (Context.getTypeSize(PointeeTy) == Context.getCharWidth())
1999 ActionIdx = 2; // If the pointee's size is sizeof(char),
2000 // suggest an explicit length.
2001 DiagRuntimeBehavior(SizeOfArg->getExprLoc(), Dest,
2002 PDiag(diag::warn_sizeof_pointer_expr_memaccess)
2003 << FnName << ArgIdx << ActionIdx
2004 << Dest->getSourceRange()
2005 << SizeOfArg->getSourceRange());
2006 break;
2007 }
2008 }
2009
2010 // Also check for cases where the sizeof argument is the exact same
2011 // type as the memory argument, and where it points to a user-defined
2012 // record type.
2013 if (SizeOfArgTy != QualType()) {
2014 if (PointeeTy->isRecordType() &&
2015 Context.typesAreCompatible(SizeOfArgTy, DestTy)) {
2016 DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest,
2017 PDiag(diag::warn_sizeof_pointer_type_memaccess)
2018 << FnName << SizeOfArgTy << ArgIdx
2019 << PointeeTy << Dest->getSourceRange()
2020 << LenExpr->getSourceRange());
2021 break;
2022 }
Nico Webere4a1c642011-06-14 16:14:58 +00002023 }
2024
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002025 // Always complain about dynamic classes.
John McCallf85e1932011-06-15 23:02:42 +00002026 if (isDynamicClassType(PointeeTy))
Matt Beaumont-Gay5c5218e2011-08-19 20:40:18 +00002027 DiagRuntimeBehavior(
2028 Dest->getExprLoc(), Dest,
2029 PDiag(diag::warn_dyn_class_memaccess)
2030 << (CMF == CMF_Memcmp ? ArgIdx + 2 : ArgIdx) << FnName << PointeeTy
2031 // "overwritten" if we're warning about the destination for any call
2032 // but memcmp; otherwise a verb appropriate to the call.
2033 << (ArgIdx == 0 && CMF != CMF_Memcmp ? 0 : (unsigned)CMF)
2034 << Call->getCallee()->getSourceRange());
Douglas Gregor707a23e2011-06-16 17:56:04 +00002035 else if (PointeeTy.hasNonTrivialObjCLifetime() && CMF != CMF_Memset)
Matt Beaumont-Gay5c5218e2011-08-19 20:40:18 +00002036 DiagRuntimeBehavior(
2037 Dest->getExprLoc(), Dest,
2038 PDiag(diag::warn_arc_object_memaccess)
2039 << ArgIdx << FnName << PointeeTy
2040 << Call->getCallee()->getSourceRange());
John McCallf85e1932011-06-15 23:02:42 +00002041 else
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002042 continue;
John McCallf85e1932011-06-15 23:02:42 +00002043
2044 DiagRuntimeBehavior(
2045 Dest->getExprLoc(), Dest,
Chandler Carruth929f0132011-06-03 06:23:57 +00002046 PDiag(diag::note_bad_memaccess_silence)
Douglas Gregor06bc9eb2011-05-03 20:37:33 +00002047 << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)"));
2048 break;
2049 }
Chandler Carruth7ccc95b2011-04-27 07:05:31 +00002050 }
2051}
2052
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002053// A little helper routine: ignore addition and subtraction of integer literals.
2054// This intentionally does not ignore all integer constant expressions because
2055// we don't want to remove sizeof().
2056static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) {
2057 Ex = Ex->IgnoreParenCasts();
2058
2059 for (;;) {
2060 const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex);
2061 if (!BO || !BO->isAdditiveOp())
2062 break;
2063
2064 const Expr *RHS = BO->getRHS()->IgnoreParenCasts();
2065 const Expr *LHS = BO->getLHS()->IgnoreParenCasts();
2066
2067 if (isa<IntegerLiteral>(RHS))
2068 Ex = LHS;
2069 else if (isa<IntegerLiteral>(LHS))
2070 Ex = RHS;
2071 else
2072 break;
2073 }
2074
2075 return Ex;
2076}
2077
2078// Warn if the user has made the 'size' argument to strlcpy or strlcat
2079// be the size of the source, instead of the destination.
2080void Sema::CheckStrlcpycatArguments(const CallExpr *Call,
2081 IdentifierInfo *FnName) {
2082
2083 // Don't crash if the user has the wrong number of arguments
2084 if (Call->getNumArgs() != 3)
2085 return;
2086
2087 const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context);
2088 const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context);
2089 const Expr *CompareWithSrc = NULL;
2090
2091 // Look for 'strlcpy(dst, x, sizeof(x))'
2092 if (const Expr *Ex = getSizeOfExprArg(SizeArg))
2093 CompareWithSrc = Ex;
2094 else {
2095 // Look for 'strlcpy(dst, x, strlen(x))'
2096 if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) {
2097 if (SizeCall->isBuiltinCall(Context) == Builtin::BIstrlen
2098 && SizeCall->getNumArgs() == 1)
2099 CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context);
2100 }
2101 }
2102
2103 if (!CompareWithSrc)
2104 return;
2105
2106 // Determine if the argument to sizeof/strlen is equal to the source
2107 // argument. In principle there's all kinds of things you could do
2108 // here, for instance creating an == expression and evaluating it with
2109 // EvaluateAsBooleanCondition, but this uses a more direct technique:
2110 const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg);
2111 if (!SrcArgDRE)
2112 return;
2113
2114 const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc);
2115 if (!CompareWithSrcDRE ||
2116 SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl())
2117 return;
2118
2119 const Expr *OriginalSizeArg = Call->getArg(2);
2120 Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size)
2121 << OriginalSizeArg->getSourceRange() << FnName;
2122
2123 // Output a FIXIT hint if the destination is an array (rather than a
2124 // pointer to an array). This could be enhanced to handle some
2125 // pointers if we know the actual size, like if DstArg is 'array+2'
2126 // we could say 'sizeof(array)-2'.
2127 const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts();
Ted Kremenek8f746222011-08-18 22:48:41 +00002128 QualType DstArgTy = DstArg->getType();
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002129
Ted Kremenek8f746222011-08-18 22:48:41 +00002130 // Only handle constant-sized or VLAs, but not flexible members.
2131 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) {
2132 // Only issue the FIXIT for arrays of size > 1.
2133 if (CAT->getSize().getSExtValue() <= 1)
2134 return;
2135 } else if (!DstArgTy->isVariableArrayType()) {
2136 return;
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002137 }
Ted Kremenek8f746222011-08-18 22:48:41 +00002138
2139 llvm::SmallString<128> sizeString;
2140 llvm::raw_svector_ostream OS(sizeString);
2141 OS << "sizeof(";
Douglas Gregor8987b232011-09-27 23:30:47 +00002142 DstArg->printPretty(OS, Context, 0, getPrintingPolicy());
Ted Kremenek8f746222011-08-18 22:48:41 +00002143 OS << ")";
2144
2145 Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size)
2146 << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(),
2147 OS.str());
Ted Kremenekbd5da9d2011-08-18 20:55:45 +00002148}
2149
Ted Kremenek06de2762007-08-17 16:46:58 +00002150//===--- CHECK: Return Address of Stack Variable --------------------------===//
2151
Chris Lattner5f9e2722011-07-23 10:55:15 +00002152static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars);
2153static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00002154
2155/// CheckReturnStackAddr - Check if a return statement returns the address
2156/// of a stack variable.
2157void
2158Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
2159 SourceLocation ReturnLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002161 Expr *stackE = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002162 SmallVector<DeclRefExpr *, 8> refVars;
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002163
2164 // Perform checking for returned stack addresses, local blocks,
2165 // label addresses or references to temporaries.
John McCallf85e1932011-06-15 23:02:42 +00002166 if (lhsType->isPointerType() ||
2167 (!getLangOptions().ObjCAutoRefCount && lhsType->isBlockPointerType())) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002168 stackE = EvalAddr(RetValExp, refVars);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002169 } else if (lhsType->isReferenceType()) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002170 stackE = EvalVal(RetValExp, refVars);
2171 }
2172
2173 if (stackE == 0)
2174 return; // Nothing suspicious was found.
2175
2176 SourceLocation diagLoc;
2177 SourceRange diagRange;
2178 if (refVars.empty()) {
2179 diagLoc = stackE->getLocStart();
2180 diagRange = stackE->getSourceRange();
2181 } else {
2182 // We followed through a reference variable. 'stackE' contains the
2183 // problematic expression but we will warn at the return statement pointing
2184 // at the reference variable. We will later display the "trail" of
2185 // reference variables using notes.
2186 diagLoc = refVars[0]->getLocStart();
2187 diagRange = refVars[0]->getSourceRange();
2188 }
2189
2190 if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var.
2191 Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref
2192 : diag::warn_ret_stack_addr)
2193 << DR->getDecl()->getDeclName() << diagRange;
2194 } else if (isa<BlockExpr>(stackE)) { // local block.
2195 Diag(diagLoc, diag::err_ret_local_block) << diagRange;
2196 } else if (isa<AddrLabelExpr>(stackE)) { // address of label.
2197 Diag(diagLoc, diag::warn_ret_addr_label) << diagRange;
2198 } else { // local temporary.
2199 Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref
2200 : diag::warn_ret_local_temp_addr)
2201 << diagRange;
2202 }
2203
2204 // Display the "trail" of reference variables that we followed until we
2205 // found the problematic expression using notes.
2206 for (unsigned i = 0, e = refVars.size(); i != e; ++i) {
2207 VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl());
2208 // If this var binds to another reference var, show the range of the next
2209 // var, otherwise the var binds to the problematic expression, in which case
2210 // show the range of the expression.
2211 SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange()
2212 : stackE->getSourceRange();
2213 Diag(VD->getLocation(), diag::note_ref_var_local_bind)
2214 << VD->getDeclName() << range;
Ted Kremenek06de2762007-08-17 16:46:58 +00002215 }
2216}
2217
2218/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
2219/// check if the expression in a return statement evaluates to an address
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002220/// to a location on the stack, a local block, an address of a label, or a
2221/// reference to local temporary. The recursion is used to traverse the
Ted Kremenek06de2762007-08-17 16:46:58 +00002222/// AST of the return expression, with recursion backtracking when we
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002223/// encounter a subexpression that (1) clearly does not lead to one of the
2224/// above problematic expressions (2) is something we cannot determine leads to
2225/// a problematic expression based on such local checking.
2226///
2227/// Both EvalAddr and EvalVal follow through reference variables to evaluate
2228/// the expression that they point to. Such variables are added to the
2229/// 'refVars' vector so that we know what the reference variable "trail" was.
Ted Kremenek06de2762007-08-17 16:46:58 +00002230///
Ted Kremeneke8c600f2007-08-28 17:02:55 +00002231/// EvalAddr processes expressions that are pointers that are used as
2232/// references (and not L-values). EvalVal handles all other values.
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002233/// At the base case of the recursion is a check for the above problematic
2234/// expressions.
Ted Kremenek06de2762007-08-17 16:46:58 +00002235///
2236/// This implementation handles:
2237///
2238/// * pointer-to-pointer casts
2239/// * implicit conversions from array references to pointers
2240/// * taking the address of fields
2241/// * arbitrary interplay between "&" and "*" operators
2242/// * pointer arithmetic from an address of a stack variable
2243/// * taking the address of an array element where the array is on the stack
Chris Lattner5f9e2722011-07-23 10:55:15 +00002244static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002245 if (E->isTypeDependent())
2246 return NULL;
2247
Ted Kremenek06de2762007-08-17 16:46:58 +00002248 // We should only be called for evaluating pointer expressions.
David Chisnall0f436562009-08-17 16:35:33 +00002249 assert((E->getType()->isAnyPointerType() ||
Steve Naroffdd972f22008-09-05 22:11:13 +00002250 E->getType()->isBlockPointerType() ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002251 E->getType()->isObjCQualifiedIdType()) &&
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002252 "EvalAddr only works on pointers");
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Peter Collingbournef111d932011-04-15 00:35:48 +00002254 E = E->IgnoreParens();
2255
Ted Kremenek06de2762007-08-17 16:46:58 +00002256 // Our "symbolic interpreter" is just a dispatch off the currently
2257 // viewed AST node. We then recursively traverse the AST by calling
2258 // EvalAddr and EvalVal appropriately.
2259 switch (E->getStmtClass()) {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002260 case Stmt::DeclRefExprClass: {
2261 DeclRefExpr *DR = cast<DeclRefExpr>(E);
2262
2263 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
2264 // If this is a reference variable, follow through to the expression that
2265 // it points to.
2266 if (V->hasLocalStorage() &&
2267 V->getType()->isReferenceType() && V->hasInit()) {
2268 // Add the reference variable to the "trail".
2269 refVars.push_back(DR);
2270 return EvalAddr(V->getInit(), refVars);
2271 }
2272
2273 return NULL;
2274 }
Ted Kremenek06de2762007-08-17 16:46:58 +00002275
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002276 case Stmt::UnaryOperatorClass: {
2277 // The only unary operator that make sense to handle here
2278 // is AddrOf. All others don't make sense as pointers.
2279 UnaryOperator *U = cast<UnaryOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002280
John McCall2de56d12010-08-25 11:45:40 +00002281 if (U->getOpcode() == UO_AddrOf)
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002282 return EvalVal(U->getSubExpr(), refVars);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002283 else
Ted Kremenek06de2762007-08-17 16:46:58 +00002284 return NULL;
2285 }
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002287 case Stmt::BinaryOperatorClass: {
2288 // Handle pointer arithmetic. All other binary operators are not valid
2289 // in this context.
2290 BinaryOperator *B = cast<BinaryOperator>(E);
John McCall2de56d12010-08-25 11:45:40 +00002291 BinaryOperatorKind op = B->getOpcode();
Mike Stump1eb44332009-09-09 15:08:12 +00002292
John McCall2de56d12010-08-25 11:45:40 +00002293 if (op != BO_Add && op != BO_Sub)
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002294 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00002295
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002296 Expr *Base = B->getLHS();
2297
2298 // Determine which argument is the real pointer base. It could be
2299 // the RHS argument instead of the LHS.
2300 if (!Base->getType()->isPointerType()) Base = B->getRHS();
Mike Stump1eb44332009-09-09 15:08:12 +00002301
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002302 assert (Base->getType()->isPointerType());
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002303 return EvalAddr(Base, refVars);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002304 }
Steve Naroff61f40a22008-09-10 19:17:48 +00002305
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002306 // For conditional operators we need to see if either the LHS or RHS are
2307 // valid DeclRefExpr*s. If one of them is valid, we return it.
2308 case Stmt::ConditionalOperatorClass: {
2309 ConditionalOperator *C = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002310
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002311 // Handle the GNU extension for missing LHS.
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00002312 if (Expr *lhsExpr = C->getLHS()) {
2313 // In C++, we can have a throw-expression, which has 'void' type.
2314 if (!lhsExpr->getType()->isVoidType())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002315 if (Expr* LHS = EvalAddr(lhsExpr, refVars))
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00002316 return LHS;
2317 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002318
Douglas Gregor9ee5ee82010-10-21 16:21:08 +00002319 // In C++, we can have a throw-expression, which has 'void' type.
2320 if (C->getRHS()->getType()->isVoidType())
2321 return NULL;
2322
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002323 return EvalAddr(C->getRHS(), refVars);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002324 }
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002325
2326 case Stmt::BlockExprClass:
John McCall469a1eb2011-02-02 13:00:07 +00002327 if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002328 return E; // local block.
2329 return NULL;
2330
2331 case Stmt::AddrLabelExprClass:
2332 return E; // address of label.
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Ted Kremenek54b52742008-08-07 00:49:01 +00002334 // For casts, we need to handle conversions from arrays to
2335 // pointer values, and pointer-to-pointer conversions.
Douglas Gregor49badde2008-10-27 19:41:14 +00002336 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00002337 case Stmt::CStyleCastExprClass:
John McCallf85e1932011-06-15 23:02:42 +00002338 case Stmt::CXXFunctionalCastExprClass:
2339 case Stmt::ObjCBridgedCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002340 Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
Ted Kremenek54b52742008-08-07 00:49:01 +00002341 QualType T = SubExpr->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Steve Naroffdd972f22008-09-05 22:11:13 +00002343 if (SubExpr->getType()->isPointerType() ||
2344 SubExpr->getType()->isBlockPointerType() ||
2345 SubExpr->getType()->isObjCQualifiedIdType())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002346 return EvalAddr(SubExpr, refVars);
Ted Kremenek54b52742008-08-07 00:49:01 +00002347 else if (T->isArrayType())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002348 return EvalVal(SubExpr, refVars);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002349 else
Ted Kremenek54b52742008-08-07 00:49:01 +00002350 return 0;
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002351 }
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002353 // C++ casts. For dynamic casts, static casts, and const casts, we
2354 // are always converting from a pointer-to-pointer, so we just blow
Douglas Gregor49badde2008-10-27 19:41:14 +00002355 // through the cast. In the case the dynamic cast doesn't fail (and
2356 // return NULL), we take the conservative route and report cases
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002357 // where we return the address of a stack variable. For Reinterpre
Douglas Gregor49badde2008-10-27 19:41:14 +00002358 // FIXME: The comment about is wrong; we're not always converting
2359 // from pointer to pointer. I'm guessing that this code should also
Mike Stump1eb44332009-09-09 15:08:12 +00002360 // handle references to objects.
2361 case Stmt::CXXStaticCastExprClass:
2362 case Stmt::CXXDynamicCastExprClass:
Douglas Gregor49badde2008-10-27 19:41:14 +00002363 case Stmt::CXXConstCastExprClass:
2364 case Stmt::CXXReinterpretCastExprClass: {
2365 Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr();
Steve Naroffdd972f22008-09-05 22:11:13 +00002366 if (S->getType()->isPointerType() || S->getType()->isBlockPointerType())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002367 return EvalAddr(S, refVars);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002368 else
2369 return NULL;
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002370 }
Mike Stump1eb44332009-09-09 15:08:12 +00002371
Douglas Gregor03e80032011-06-21 17:03:29 +00002372 case Stmt::MaterializeTemporaryExprClass:
2373 if (Expr *Result = EvalAddr(
2374 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
2375 refVars))
2376 return Result;
2377
2378 return E;
2379
Chris Lattnerfae3f1f2007-12-28 05:31:15 +00002380 // Everything else: we simply don't reason about them.
2381 default:
2382 return NULL;
2383 }
Ted Kremenek06de2762007-08-17 16:46:58 +00002384}
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Ted Kremenek06de2762007-08-17 16:46:58 +00002386
2387/// EvalVal - This function is complements EvalAddr in the mutual recursion.
2388/// See the comments for EvalAddr for more details.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002389static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) {
Ted Kremenek68957a92010-08-04 20:01:07 +00002390do {
Ted Kremeneke8c600f2007-08-28 17:02:55 +00002391 // We should only be called for evaluating non-pointer expressions, or
2392 // expressions with a pointer type that are not used as references but instead
2393 // are l-values (e.g., DeclRefExpr with a pointer type).
Mike Stump1eb44332009-09-09 15:08:12 +00002394
Ted Kremenek06de2762007-08-17 16:46:58 +00002395 // Our "symbolic interpreter" is just a dispatch off the currently
2396 // viewed AST node. We then recursively traverse the AST by calling
2397 // EvalAddr and EvalVal appropriately.
Peter Collingbournef111d932011-04-15 00:35:48 +00002398
2399 E = E->IgnoreParens();
Ted Kremenek06de2762007-08-17 16:46:58 +00002400 switch (E->getStmtClass()) {
Ted Kremenek68957a92010-08-04 20:01:07 +00002401 case Stmt::ImplicitCastExprClass: {
2402 ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E);
John McCall5baba9d2010-08-25 10:28:54 +00002403 if (IE->getValueKind() == VK_LValue) {
Ted Kremenek68957a92010-08-04 20:01:07 +00002404 E = IE->getSubExpr();
2405 continue;
2406 }
2407 return NULL;
2408 }
2409
Douglas Gregora2813ce2009-10-23 18:54:35 +00002410 case Stmt::DeclRefExprClass: {
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002411 // When we hit a DeclRefExpr we are looking at code that refers to a
2412 // variable's name. If it's not a reference variable we check if it has
2413 // local storage within the function, and if so, return the expression.
Ted Kremenek06de2762007-08-17 16:46:58 +00002414 DeclRefExpr *DR = cast<DeclRefExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Ted Kremenek06de2762007-08-17 16:46:58 +00002416 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002417 if (V->hasLocalStorage()) {
2418 if (!V->getType()->isReferenceType())
2419 return DR;
2420
2421 // Reference variable, follow through to the expression that
2422 // it points to.
2423 if (V->hasInit()) {
2424 // Add the reference variable to the "trail".
2425 refVars.push_back(DR);
2426 return EvalVal(V->getInit(), refVars);
2427 }
2428 }
Mike Stump1eb44332009-09-09 15:08:12 +00002429
Ted Kremenek06de2762007-08-17 16:46:58 +00002430 return NULL;
2431 }
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Ted Kremenek06de2762007-08-17 16:46:58 +00002433 case Stmt::UnaryOperatorClass: {
2434 // The only unary operator that make sense to handle here
2435 // is Deref. All others don't resolve to a "name." This includes
2436 // handling all sorts of rvalues passed to a unary operator.
2437 UnaryOperator *U = cast<UnaryOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002438
John McCall2de56d12010-08-25 11:45:40 +00002439 if (U->getOpcode() == UO_Deref)
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002440 return EvalAddr(U->getSubExpr(), refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00002441
2442 return NULL;
2443 }
Mike Stump1eb44332009-09-09 15:08:12 +00002444
Ted Kremenek06de2762007-08-17 16:46:58 +00002445 case Stmt::ArraySubscriptExprClass: {
2446 // Array subscripts are potential references to data on the stack. We
2447 // retrieve the DeclRefExpr* for the array variable if it indeed
2448 // has local storage.
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002449 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00002450 }
Mike Stump1eb44332009-09-09 15:08:12 +00002451
Ted Kremenek06de2762007-08-17 16:46:58 +00002452 case Stmt::ConditionalOperatorClass: {
2453 // For conditional operators we need to see if either the LHS or RHS are
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002454 // non-NULL Expr's. If one is non-NULL, we return it.
Ted Kremenek06de2762007-08-17 16:46:58 +00002455 ConditionalOperator *C = cast<ConditionalOperator>(E);
2456
Anders Carlsson39073232007-11-30 19:04:31 +00002457 // Handle the GNU extension for missing LHS.
2458 if (Expr *lhsExpr = C->getLHS())
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002459 if (Expr *LHS = EvalVal(lhsExpr, refVars))
Anders Carlsson39073232007-11-30 19:04:31 +00002460 return LHS;
2461
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002462 return EvalVal(C->getRHS(), refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00002463 }
Mike Stump1eb44332009-09-09 15:08:12 +00002464
Ted Kremenek06de2762007-08-17 16:46:58 +00002465 // Accesses to members are potential references to data on the stack.
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002466 case Stmt::MemberExprClass: {
Ted Kremenek06de2762007-08-17 16:46:58 +00002467 MemberExpr *M = cast<MemberExpr>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Ted Kremenek06de2762007-08-17 16:46:58 +00002469 // Check for indirect access. We only want direct field accesses.
Ted Kremeneka423e812010-09-02 01:12:13 +00002470 if (M->isArrow())
Ted Kremenek06de2762007-08-17 16:46:58 +00002471 return NULL;
Ted Kremeneka423e812010-09-02 01:12:13 +00002472
2473 // Check whether the member type is itself a reference, in which case
2474 // we're not going to refer to the member, but to what the member refers to.
2475 if (M->getMemberDecl()->getType()->isReferenceType())
2476 return NULL;
2477
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002478 return EvalVal(M->getBase(), refVars);
Ted Kremenek06de2762007-08-17 16:46:58 +00002479 }
Mike Stump1eb44332009-09-09 15:08:12 +00002480
Douglas Gregor03e80032011-06-21 17:03:29 +00002481 case Stmt::MaterializeTemporaryExprClass:
2482 if (Expr *Result = EvalVal(
2483 cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(),
2484 refVars))
2485 return Result;
2486
2487 return E;
2488
Ted Kremenek06de2762007-08-17 16:46:58 +00002489 default:
Argyrios Kyrtzidis26e10be2010-11-30 22:57:32 +00002490 // Check that we don't return or take the address of a reference to a
2491 // temporary. This is only useful in C++.
2492 if (!E->isTypeDependent() && E->isRValue())
2493 return E;
2494
2495 // Everything else: we simply don't reason about them.
Ted Kremenek06de2762007-08-17 16:46:58 +00002496 return NULL;
2497 }
Ted Kremenek68957a92010-08-04 20:01:07 +00002498} while (true);
Ted Kremenek06de2762007-08-17 16:46:58 +00002499}
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002500
2501//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
2502
2503/// Check for comparisons of floating point operands using != and ==.
2504/// Issue a warning if these are no self-comparisons, as they are not likely
2505/// to do what the programmer intended.
Richard Trieudd225092011-09-15 21:56:47 +00002506void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) {
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002507 bool EmitWarning = true;
Mike Stump1eb44332009-09-09 15:08:12 +00002508
Richard Trieudd225092011-09-15 21:56:47 +00002509 Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts();
2510 Expr* RightExprSansParen = RHS->IgnoreParenImpCasts();
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002511
2512 // Special case: check for x == x (which is OK).
2513 // Do not emit warnings for such cases.
2514 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
2515 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
2516 if (DRL->getDecl() == DRR->getDecl())
2517 EmitWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002518
2519
Ted Kremenek1b500bb2007-11-29 00:59:04 +00002520 // Special case: check for comparisons against literals that can be exactly
2521 // represented by APFloat. In such cases, do not emit a warning. This
2522 // is a heuristic: often comparison against such literals are used to
2523 // detect if a value in a variable has not changed. This clearly can
2524 // lead to false negatives.
2525 if (EmitWarning) {
2526 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
2527 if (FLL->isExact())
2528 EmitWarning = false;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002529 } else
Ted Kremenek1b500bb2007-11-29 00:59:04 +00002530 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){
2531 if (FLR->isExact())
2532 EmitWarning = false;
2533 }
2534 }
Mike Stump1eb44332009-09-09 15:08:12 +00002535
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002536 // Check for comparisons with builtin types.
Sebastian Redl0eb23302009-01-19 00:08:26 +00002537 if (EmitWarning)
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002538 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
Douglas Gregor3c385e52009-02-14 18:57:46 +00002539 if (CL->isBuiltinCall(Context))
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002540 EmitWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002541
Sebastian Redl0eb23302009-01-19 00:08:26 +00002542 if (EmitWarning)
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002543 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
Douglas Gregor3c385e52009-02-14 18:57:46 +00002544 if (CR->isBuiltinCall(Context))
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002545 EmitWarning = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002547 // Emit the diagnostic.
2548 if (EmitWarning)
Richard Trieudd225092011-09-15 21:56:47 +00002549 Diag(Loc, diag::warn_floatingpoint_eq)
2550 << LHS->getSourceRange() << RHS->getSourceRange();
Ted Kremenek588e5eb2007-11-25 00:58:00 +00002551}
John McCallba26e582010-01-04 23:21:16 +00002552
John McCallf2370c92010-01-06 05:24:50 +00002553//===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===//
2554//===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===//
John McCallba26e582010-01-04 23:21:16 +00002555
John McCallf2370c92010-01-06 05:24:50 +00002556namespace {
John McCallba26e582010-01-04 23:21:16 +00002557
John McCallf2370c92010-01-06 05:24:50 +00002558/// Structure recording the 'active' range of an integer-valued
2559/// expression.
2560struct IntRange {
2561 /// The number of bits active in the int.
2562 unsigned Width;
John McCallba26e582010-01-04 23:21:16 +00002563
John McCallf2370c92010-01-06 05:24:50 +00002564 /// True if the int is known not to have negative values.
2565 bool NonNegative;
John McCallba26e582010-01-04 23:21:16 +00002566
John McCallf2370c92010-01-06 05:24:50 +00002567 IntRange(unsigned Width, bool NonNegative)
2568 : Width(Width), NonNegative(NonNegative)
2569 {}
John McCallba26e582010-01-04 23:21:16 +00002570
John McCall1844a6e2010-11-10 23:38:19 +00002571 /// Returns the range of the bool type.
John McCallf2370c92010-01-06 05:24:50 +00002572 static IntRange forBoolType() {
2573 return IntRange(1, true);
John McCall51313c32010-01-04 23:31:57 +00002574 }
2575
John McCall1844a6e2010-11-10 23:38:19 +00002576 /// Returns the range of an opaque value of the given integral type.
2577 static IntRange forValueOfType(ASTContext &C, QualType T) {
2578 return forValueOfCanonicalType(C,
2579 T->getCanonicalTypeInternal().getTypePtr());
John McCall51313c32010-01-04 23:31:57 +00002580 }
2581
John McCall1844a6e2010-11-10 23:38:19 +00002582 /// Returns the range of an opaque value of a canonical integral type.
2583 static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) {
John McCallf2370c92010-01-06 05:24:50 +00002584 assert(T->isCanonicalUnqualified());
2585
2586 if (const VectorType *VT = dyn_cast<VectorType>(T))
2587 T = VT->getElementType().getTypePtr();
2588 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
2589 T = CT->getElementType().getTypePtr();
John McCall323ed742010-05-06 08:58:33 +00002590
John McCall091f23f2010-11-09 22:22:12 +00002591 // For enum types, use the known bit width of the enumerators.
John McCall323ed742010-05-06 08:58:33 +00002592 if (const EnumType *ET = dyn_cast<EnumType>(T)) {
2593 EnumDecl *Enum = ET->getDecl();
John McCall091f23f2010-11-09 22:22:12 +00002594 if (!Enum->isDefinition())
2595 return IntRange(C.getIntWidth(QualType(T, 0)), false);
2596
John McCall323ed742010-05-06 08:58:33 +00002597 unsigned NumPositive = Enum->getNumPositiveBits();
2598 unsigned NumNegative = Enum->getNumNegativeBits();
2599
2600 return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0);
2601 }
John McCallf2370c92010-01-06 05:24:50 +00002602
2603 const BuiltinType *BT = cast<BuiltinType>(T);
2604 assert(BT->isInteger());
2605
2606 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
2607 }
2608
John McCall1844a6e2010-11-10 23:38:19 +00002609 /// Returns the "target" range of a canonical integral type, i.e.
2610 /// the range of values expressible in the type.
2611 ///
2612 /// This matches forValueOfCanonicalType except that enums have the
2613 /// full range of their type, not the range of their enumerators.
2614 static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) {
2615 assert(T->isCanonicalUnqualified());
2616
2617 if (const VectorType *VT = dyn_cast<VectorType>(T))
2618 T = VT->getElementType().getTypePtr();
2619 if (const ComplexType *CT = dyn_cast<ComplexType>(T))
2620 T = CT->getElementType().getTypePtr();
2621 if (const EnumType *ET = dyn_cast<EnumType>(T))
Douglas Gregor69ff26b2011-09-08 23:29:05 +00002622 T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr();
John McCall1844a6e2010-11-10 23:38:19 +00002623
2624 const BuiltinType *BT = cast<BuiltinType>(T);
2625 assert(BT->isInteger());
2626
2627 return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger());
2628 }
2629
2630 /// Returns the supremum of two ranges: i.e. their conservative merge.
John McCallc0cd21d2010-02-23 19:22:29 +00002631 static IntRange join(IntRange L, IntRange R) {
John McCallf2370c92010-01-06 05:24:50 +00002632 return IntRange(std::max(L.Width, R.Width),
John McCall60fad452010-01-06 22:07:33 +00002633 L.NonNegative && R.NonNegative);
2634 }
2635
John McCall1844a6e2010-11-10 23:38:19 +00002636 /// Returns the infinum of two ranges: i.e. their aggressive merge.
John McCallc0cd21d2010-02-23 19:22:29 +00002637 static IntRange meet(IntRange L, IntRange R) {
John McCall60fad452010-01-06 22:07:33 +00002638 return IntRange(std::min(L.Width, R.Width),
2639 L.NonNegative || R.NonNegative);
John McCallf2370c92010-01-06 05:24:50 +00002640 }
2641};
2642
2643IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, unsigned MaxWidth) {
2644 if (value.isSigned() && value.isNegative())
2645 return IntRange(value.getMinSignedBits(), false);
2646
2647 if (value.getBitWidth() > MaxWidth)
Jay Foad9f71a8f2010-12-07 08:25:34 +00002648 value = value.trunc(MaxWidth);
John McCallf2370c92010-01-06 05:24:50 +00002649
2650 // isNonNegative() just checks the sign bit without considering
2651 // signedness.
2652 return IntRange(value.getActiveBits(), true);
2653}
2654
John McCall0acc3112010-01-06 22:57:21 +00002655IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty,
John McCallf2370c92010-01-06 05:24:50 +00002656 unsigned MaxWidth) {
2657 if (result.isInt())
2658 return GetValueRange(C, result.getInt(), MaxWidth);
2659
2660 if (result.isVector()) {
John McCall0acc3112010-01-06 22:57:21 +00002661 IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth);
2662 for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) {
2663 IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth);
2664 R = IntRange::join(R, El);
2665 }
John McCallf2370c92010-01-06 05:24:50 +00002666 return R;
2667 }
2668
2669 if (result.isComplexInt()) {
2670 IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth);
2671 IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth);
2672 return IntRange::join(R, I);
John McCall51313c32010-01-04 23:31:57 +00002673 }
2674
2675 // This can happen with lossless casts to intptr_t of "based" lvalues.
2676 // Assume it might use arbitrary bits.
John McCall0acc3112010-01-06 22:57:21 +00002677 // FIXME: The only reason we need to pass the type in here is to get
2678 // the sign right on this one case. It would be nice if APValue
2679 // preserved this.
John McCallf2370c92010-01-06 05:24:50 +00002680 assert(result.isLValue());
Douglas Gregor5e9ebb32011-05-21 16:28:01 +00002681 return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType());
John McCall51313c32010-01-04 23:31:57 +00002682}
John McCallf2370c92010-01-06 05:24:50 +00002683
2684/// Pseudo-evaluate the given integer expression, estimating the
2685/// range of values it might take.
2686///
2687/// \param MaxWidth - the width to which the value will be truncated
2688IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) {
2689 E = E->IgnoreParens();
2690
2691 // Try a full evaluation first.
2692 Expr::EvalResult result;
2693 if (E->Evaluate(result, C))
John McCall0acc3112010-01-06 22:57:21 +00002694 return GetValueRange(C, result.Val, E->getType(), MaxWidth);
John McCallf2370c92010-01-06 05:24:50 +00002695
2696 // I think we only want to look through implicit casts here; if the
2697 // user has an explicit widening cast, we should treat the value as
2698 // being of the new, wider type.
2699 if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002700 if (CE->getCastKind() == CK_NoOp)
John McCallf2370c92010-01-06 05:24:50 +00002701 return GetExprRange(C, CE->getSubExpr(), MaxWidth);
2702
John McCall1844a6e2010-11-10 23:38:19 +00002703 IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType());
John McCallf2370c92010-01-06 05:24:50 +00002704
John McCall2de56d12010-08-25 11:45:40 +00002705 bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast);
John McCall60fad452010-01-06 22:07:33 +00002706
John McCallf2370c92010-01-06 05:24:50 +00002707 // Assume that non-integer casts can span the full range of the type.
John McCall60fad452010-01-06 22:07:33 +00002708 if (!isIntegerCast)
John McCallf2370c92010-01-06 05:24:50 +00002709 return OutputTypeRange;
2710
2711 IntRange SubRange
2712 = GetExprRange(C, CE->getSubExpr(),
2713 std::min(MaxWidth, OutputTypeRange.Width));
2714
2715 // Bail out if the subexpr's range is as wide as the cast type.
2716 if (SubRange.Width >= OutputTypeRange.Width)
2717 return OutputTypeRange;
2718
2719 // Otherwise, we take the smaller width, and we're non-negative if
2720 // either the output type or the subexpr is.
2721 return IntRange(SubRange.Width,
2722 SubRange.NonNegative || OutputTypeRange.NonNegative);
2723 }
2724
2725 if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
2726 // If we can fold the condition, just take that operand.
2727 bool CondResult;
2728 if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C))
2729 return GetExprRange(C, CondResult ? CO->getTrueExpr()
2730 : CO->getFalseExpr(),
2731 MaxWidth);
2732
2733 // Otherwise, conservatively merge.
2734 IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth);
2735 IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth);
2736 return IntRange::join(L, R);
2737 }
2738
2739 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2740 switch (BO->getOpcode()) {
2741
2742 // Boolean-valued operations are single-bit and positive.
John McCall2de56d12010-08-25 11:45:40 +00002743 case BO_LAnd:
2744 case BO_LOr:
2745 case BO_LT:
2746 case BO_GT:
2747 case BO_LE:
2748 case BO_GE:
2749 case BO_EQ:
2750 case BO_NE:
John McCallf2370c92010-01-06 05:24:50 +00002751 return IntRange::forBoolType();
2752
John McCall862ff872011-07-13 06:35:24 +00002753 // The type of the assignments is the type of the LHS, so the RHS
2754 // is not necessarily the same type.
John McCall2de56d12010-08-25 11:45:40 +00002755 case BO_MulAssign:
2756 case BO_DivAssign:
2757 case BO_RemAssign:
2758 case BO_AddAssign:
2759 case BO_SubAssign:
John McCall862ff872011-07-13 06:35:24 +00002760 case BO_XorAssign:
2761 case BO_OrAssign:
2762 // TODO: bitfields?
John McCall1844a6e2010-11-10 23:38:19 +00002763 return IntRange::forValueOfType(C, E->getType());
John McCallc0cd21d2010-02-23 19:22:29 +00002764
John McCall862ff872011-07-13 06:35:24 +00002765 // Simple assignments just pass through the RHS, which will have
2766 // been coerced to the LHS type.
2767 case BO_Assign:
2768 // TODO: bitfields?
2769 return GetExprRange(C, BO->getRHS(), MaxWidth);
2770
John McCallf2370c92010-01-06 05:24:50 +00002771 // Operations with opaque sources are black-listed.
John McCall2de56d12010-08-25 11:45:40 +00002772 case BO_PtrMemD:
2773 case BO_PtrMemI:
John McCall1844a6e2010-11-10 23:38:19 +00002774 return IntRange::forValueOfType(C, E->getType());
John McCallf2370c92010-01-06 05:24:50 +00002775
John McCall60fad452010-01-06 22:07:33 +00002776 // Bitwise-and uses the *infinum* of the two source ranges.
John McCall2de56d12010-08-25 11:45:40 +00002777 case BO_And:
2778 case BO_AndAssign:
John McCall60fad452010-01-06 22:07:33 +00002779 return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth),
2780 GetExprRange(C, BO->getRHS(), MaxWidth));
2781
John McCallf2370c92010-01-06 05:24:50 +00002782 // Left shift gets black-listed based on a judgement call.
John McCall2de56d12010-08-25 11:45:40 +00002783 case BO_Shl:
John McCall3aae6092010-04-07 01:14:35 +00002784 // ...except that we want to treat '1 << (blah)' as logically
2785 // positive. It's an important idiom.
2786 if (IntegerLiteral *I
2787 = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) {
2788 if (I->getValue() == 1) {
John McCall1844a6e2010-11-10 23:38:19 +00002789 IntRange R = IntRange::forValueOfType(C, E->getType());
John McCall3aae6092010-04-07 01:14:35 +00002790 return IntRange(R.Width, /*NonNegative*/ true);
2791 }
2792 }
2793 // fallthrough
2794
John McCall2de56d12010-08-25 11:45:40 +00002795 case BO_ShlAssign:
John McCall1844a6e2010-11-10 23:38:19 +00002796 return IntRange::forValueOfType(C, E->getType());
John McCallf2370c92010-01-06 05:24:50 +00002797
John McCall60fad452010-01-06 22:07:33 +00002798 // Right shift by a constant can narrow its left argument.
John McCall2de56d12010-08-25 11:45:40 +00002799 case BO_Shr:
2800 case BO_ShrAssign: {
John McCall60fad452010-01-06 22:07:33 +00002801 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
2802
2803 // If the shift amount is a positive constant, drop the width by
2804 // that much.
2805 llvm::APSInt shift;
2806 if (BO->getRHS()->isIntegerConstantExpr(shift, C) &&
2807 shift.isNonNegative()) {
2808 unsigned zext = shift.getZExtValue();
2809 if (zext >= L.Width)
2810 L.Width = (L.NonNegative ? 0 : 1);
2811 else
2812 L.Width -= zext;
2813 }
2814
2815 return L;
2816 }
2817
2818 // Comma acts as its right operand.
John McCall2de56d12010-08-25 11:45:40 +00002819 case BO_Comma:
John McCallf2370c92010-01-06 05:24:50 +00002820 return GetExprRange(C, BO->getRHS(), MaxWidth);
2821
John McCall60fad452010-01-06 22:07:33 +00002822 // Black-list pointer subtractions.
John McCall2de56d12010-08-25 11:45:40 +00002823 case BO_Sub:
John McCallf2370c92010-01-06 05:24:50 +00002824 if (BO->getLHS()->getType()->isPointerType())
John McCall1844a6e2010-11-10 23:38:19 +00002825 return IntRange::forValueOfType(C, E->getType());
John McCall00fe7612011-07-14 22:39:48 +00002826 break;
Ted Kremenek4e4b30e2010-02-16 01:46:59 +00002827
John McCall00fe7612011-07-14 22:39:48 +00002828 // The width of a division result is mostly determined by the size
2829 // of the LHS.
2830 case BO_Div: {
2831 // Don't 'pre-truncate' the operands.
2832 unsigned opWidth = C.getIntWidth(E->getType());
2833 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
2834
2835 // If the divisor is constant, use that.
2836 llvm::APSInt divisor;
2837 if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) {
2838 unsigned log2 = divisor.logBase2(); // floor(log_2(divisor))
2839 if (log2 >= L.Width)
2840 L.Width = (L.NonNegative ? 0 : 1);
2841 else
2842 L.Width = std::min(L.Width - log2, MaxWidth);
2843 return L;
2844 }
2845
2846 // Otherwise, just use the LHS's width.
2847 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
2848 return IntRange(L.Width, L.NonNegative && R.NonNegative);
2849 }
2850
2851 // The result of a remainder can't be larger than the result of
2852 // either side.
2853 case BO_Rem: {
2854 // Don't 'pre-truncate' the operands.
2855 unsigned opWidth = C.getIntWidth(E->getType());
2856 IntRange L = GetExprRange(C, BO->getLHS(), opWidth);
2857 IntRange R = GetExprRange(C, BO->getRHS(), opWidth);
2858
2859 IntRange meet = IntRange::meet(L, R);
2860 meet.Width = std::min(meet.Width, MaxWidth);
2861 return meet;
2862 }
2863
2864 // The default behavior is okay for these.
2865 case BO_Mul:
2866 case BO_Add:
2867 case BO_Xor:
2868 case BO_Or:
John McCallf2370c92010-01-06 05:24:50 +00002869 break;
2870 }
2871
John McCall00fe7612011-07-14 22:39:48 +00002872 // The default case is to treat the operation as if it were closed
2873 // on the narrowest type that encompasses both operands.
John McCallf2370c92010-01-06 05:24:50 +00002874 IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth);
2875 IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth);
2876 return IntRange::join(L, R);
2877 }
2878
2879 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
2880 switch (UO->getOpcode()) {
2881 // Boolean-valued operations are white-listed.
John McCall2de56d12010-08-25 11:45:40 +00002882 case UO_LNot:
John McCallf2370c92010-01-06 05:24:50 +00002883 return IntRange::forBoolType();
2884
2885 // Operations with opaque sources are black-listed.
John McCall2de56d12010-08-25 11:45:40 +00002886 case UO_Deref:
2887 case UO_AddrOf: // should be impossible
John McCall1844a6e2010-11-10 23:38:19 +00002888 return IntRange::forValueOfType(C, E->getType());
John McCallf2370c92010-01-06 05:24:50 +00002889
2890 default:
2891 return GetExprRange(C, UO->getSubExpr(), MaxWidth);
2892 }
2893 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002894
2895 if (dyn_cast<OffsetOfExpr>(E)) {
John McCall1844a6e2010-11-10 23:38:19 +00002896 IntRange::forValueOfType(C, E->getType());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002897 }
John McCallf2370c92010-01-06 05:24:50 +00002898
2899 FieldDecl *BitField = E->getBitField();
2900 if (BitField) {
2901 llvm::APSInt BitWidthAP = BitField->getBitWidth()->EvaluateAsInt(C);
2902 unsigned BitWidth = BitWidthAP.getZExtValue();
2903
Douglas Gregor5e9ebb32011-05-21 16:28:01 +00002904 return IntRange(BitWidth,
2905 BitField->getType()->isUnsignedIntegerOrEnumerationType());
John McCallf2370c92010-01-06 05:24:50 +00002906 }
2907
John McCall1844a6e2010-11-10 23:38:19 +00002908 return IntRange::forValueOfType(C, E->getType());
John McCallf2370c92010-01-06 05:24:50 +00002909}
John McCall51313c32010-01-04 23:31:57 +00002910
John McCall323ed742010-05-06 08:58:33 +00002911IntRange GetExprRange(ASTContext &C, Expr *E) {
2912 return GetExprRange(C, E, C.getIntWidth(E->getType()));
2913}
2914
John McCall51313c32010-01-04 23:31:57 +00002915/// Checks whether the given value, which currently has the given
2916/// source semantics, has the same value when coerced through the
2917/// target semantics.
John McCallf2370c92010-01-06 05:24:50 +00002918bool IsSameFloatAfterCast(const llvm::APFloat &value,
2919 const llvm::fltSemantics &Src,
2920 const llvm::fltSemantics &Tgt) {
John McCall51313c32010-01-04 23:31:57 +00002921 llvm::APFloat truncated = value;
2922
2923 bool ignored;
2924 truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored);
2925 truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored);
2926
2927 return truncated.bitwiseIsEqual(value);
2928}
2929
2930/// Checks whether the given value, which currently has the given
2931/// source semantics, has the same value when coerced through the
2932/// target semantics.
2933///
2934/// The value might be a vector of floats (or a complex number).
John McCallf2370c92010-01-06 05:24:50 +00002935bool IsSameFloatAfterCast(const APValue &value,
2936 const llvm::fltSemantics &Src,
2937 const llvm::fltSemantics &Tgt) {
John McCall51313c32010-01-04 23:31:57 +00002938 if (value.isFloat())
2939 return IsSameFloatAfterCast(value.getFloat(), Src, Tgt);
2940
2941 if (value.isVector()) {
2942 for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i)
2943 if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt))
2944 return false;
2945 return true;
2946 }
2947
2948 assert(value.isComplexFloat());
2949 return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) &&
2950 IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt));
2951}
2952
John McCallb4eb64d2010-10-08 02:01:28 +00002953void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
John McCall323ed742010-05-06 08:58:33 +00002954
Ted Kremeneke3b159c2010-09-23 21:43:44 +00002955static bool IsZero(Sema &S, Expr *E) {
2956 // Suppress cases where we are comparing against an enum constant.
2957 if (const DeclRefExpr *DR =
2958 dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
2959 if (isa<EnumConstantDecl>(DR->getDecl()))
2960 return false;
2961
2962 // Suppress cases where the '0' value is expanded from a macro.
2963 if (E->getLocStart().isMacroID())
2964 return false;
2965
John McCall323ed742010-05-06 08:58:33 +00002966 llvm::APSInt Value;
2967 return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
2968}
2969
John McCall372e1032010-10-06 00:25:24 +00002970static bool HasEnumType(Expr *E) {
2971 // Strip off implicit integral promotions.
2972 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Argyrios Kyrtzidis63b57ae2010-10-07 21:52:18 +00002973 if (ICE->getCastKind() != CK_IntegralCast &&
2974 ICE->getCastKind() != CK_NoOp)
John McCall372e1032010-10-06 00:25:24 +00002975 break;
Argyrios Kyrtzidis63b57ae2010-10-07 21:52:18 +00002976 E = ICE->getSubExpr();
John McCall372e1032010-10-06 00:25:24 +00002977 }
2978
2979 return E->getType()->isEnumeralType();
2980}
2981
John McCall323ed742010-05-06 08:58:33 +00002982void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002983 BinaryOperatorKind op = E->getOpcode();
Douglas Gregor14af91a2010-12-21 07:22:56 +00002984 if (E->isValueDependent())
2985 return;
2986
John McCall2de56d12010-08-25 11:45:40 +00002987 if (op == BO_LT && IsZero(S, E->getRHS())) {
John McCall323ed742010-05-06 08:58:33 +00002988 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00002989 << "< 0" << "false" << HasEnumType(E->getLHS())
John McCall323ed742010-05-06 08:58:33 +00002990 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00002991 } else if (op == BO_GE && IsZero(S, E->getRHS())) {
John McCall323ed742010-05-06 08:58:33 +00002992 S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00002993 << ">= 0" << "true" << HasEnumType(E->getLHS())
John McCall323ed742010-05-06 08:58:33 +00002994 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00002995 } else if (op == BO_GT && IsZero(S, E->getLHS())) {
John McCall323ed742010-05-06 08:58:33 +00002996 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00002997 << "0 >" << "false" << HasEnumType(E->getRHS())
John McCall323ed742010-05-06 08:58:33 +00002998 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
John McCall2de56d12010-08-25 11:45:40 +00002999 } else if (op == BO_LE && IsZero(S, E->getLHS())) {
John McCall323ed742010-05-06 08:58:33 +00003000 S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison)
John McCall372e1032010-10-06 00:25:24 +00003001 << "0 <=" << "true" << HasEnumType(E->getRHS())
John McCall323ed742010-05-06 08:58:33 +00003002 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
3003 }
3004}
3005
3006/// Analyze the operands of the given comparison. Implements the
3007/// fallback case from AnalyzeComparison.
3008void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) {
John McCallb4eb64d2010-10-08 02:01:28 +00003009 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
3010 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
John McCall323ed742010-05-06 08:58:33 +00003011}
John McCall51313c32010-01-04 23:31:57 +00003012
John McCallba26e582010-01-04 23:21:16 +00003013/// \brief Implements -Wsign-compare.
3014///
Richard Trieudd225092011-09-15 21:56:47 +00003015/// \param E the binary operator to check for warnings
John McCall323ed742010-05-06 08:58:33 +00003016void AnalyzeComparison(Sema &S, BinaryOperator *E) {
3017 // The type the comparison is being performed in.
3018 QualType T = E->getLHS()->getType();
3019 assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType())
3020 && "comparison with mismatched types");
John McCallba26e582010-01-04 23:21:16 +00003021
John McCall323ed742010-05-06 08:58:33 +00003022 // We don't do anything special if this isn't an unsigned integral
3023 // comparison: we're only interested in integral comparisons, and
3024 // signed comparisons only happen in cases we don't care to warn about.
Douglas Gregor3e026e32011-02-19 22:34:59 +00003025 //
3026 // We also don't care about value-dependent expressions or expressions
3027 // whose result is a constant.
3028 if (!T->hasUnsignedIntegerRepresentation()
3029 || E->isValueDependent() || E->isIntegerConstantExpr(S.Context))
John McCall323ed742010-05-06 08:58:33 +00003030 return AnalyzeImpConvsInComparison(S, E);
John McCallf2370c92010-01-06 05:24:50 +00003031
Richard Trieudd225092011-09-15 21:56:47 +00003032 Expr *LHS = E->getLHS()->IgnoreParenImpCasts();
3033 Expr *RHS = E->getRHS()->IgnoreParenImpCasts();
John McCallba26e582010-01-04 23:21:16 +00003034
John McCall323ed742010-05-06 08:58:33 +00003035 // Check to see if one of the (unmodified) operands is of different
3036 // signedness.
3037 Expr *signedOperand, *unsignedOperand;
Richard Trieudd225092011-09-15 21:56:47 +00003038 if (LHS->getType()->hasSignedIntegerRepresentation()) {
3039 assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
John McCall323ed742010-05-06 08:58:33 +00003040 "unsigned comparison between two signed integer expressions?");
Richard Trieudd225092011-09-15 21:56:47 +00003041 signedOperand = LHS;
3042 unsignedOperand = RHS;
3043 } else if (RHS->getType()->hasSignedIntegerRepresentation()) {
3044 signedOperand = RHS;
3045 unsignedOperand = LHS;
John McCallba26e582010-01-04 23:21:16 +00003046 } else {
John McCall323ed742010-05-06 08:58:33 +00003047 CheckTrivialUnsignedComparison(S, E);
3048 return AnalyzeImpConvsInComparison(S, E);
John McCallba26e582010-01-04 23:21:16 +00003049 }
3050
John McCall323ed742010-05-06 08:58:33 +00003051 // Otherwise, calculate the effective range of the signed operand.
3052 IntRange signedRange = GetExprRange(S.Context, signedOperand);
John McCallf2370c92010-01-06 05:24:50 +00003053
John McCall323ed742010-05-06 08:58:33 +00003054 // Go ahead and analyze implicit conversions in the operands. Note
3055 // that we skip the implicit conversions on both sides.
Richard Trieudd225092011-09-15 21:56:47 +00003056 AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
3057 AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc());
John McCallba26e582010-01-04 23:21:16 +00003058
John McCall323ed742010-05-06 08:58:33 +00003059 // If the signed range is non-negative, -Wsign-compare won't fire,
3060 // but we should still check for comparisons which are always true
3061 // or false.
3062 if (signedRange.NonNegative)
3063 return CheckTrivialUnsignedComparison(S, E);
John McCallba26e582010-01-04 23:21:16 +00003064
3065 // For (in)equality comparisons, if the unsigned operand is a
3066 // constant which cannot collide with a overflowed signed operand,
3067 // then reinterpreting the signed operand as unsigned will not
3068 // change the result of the comparison.
John McCall323ed742010-05-06 08:58:33 +00003069 if (E->isEqualityOp()) {
3070 unsigned comparisonWidth = S.Context.getIntWidth(T);
3071 IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand);
John McCallba26e582010-01-04 23:21:16 +00003072
John McCall323ed742010-05-06 08:58:33 +00003073 // We should never be unable to prove that the unsigned operand is
3074 // non-negative.
3075 assert(unsignedRange.NonNegative && "unsigned range includes negative?");
3076
3077 if (unsignedRange.Width < comparisonWidth)
3078 return;
3079 }
3080
3081 S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison)
Richard Trieudd225092011-09-15 21:56:47 +00003082 << LHS->getType() << RHS->getType()
3083 << LHS->getSourceRange() << RHS->getSourceRange();
John McCallba26e582010-01-04 23:21:16 +00003084}
3085
John McCall15d7d122010-11-11 03:21:53 +00003086/// Analyzes an attempt to assign the given value to a bitfield.
3087///
3088/// Returns true if there was something fishy about the attempt.
3089bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
3090 SourceLocation InitLoc) {
3091 assert(Bitfield->isBitField());
3092 if (Bitfield->isInvalidDecl())
3093 return false;
3094
John McCall91b60142010-11-11 05:33:51 +00003095 // White-list bool bitfields.
3096 if (Bitfield->getType()->isBooleanType())
3097 return false;
3098
Douglas Gregor46ff3032011-02-04 13:09:01 +00003099 // Ignore value- or type-dependent expressions.
3100 if (Bitfield->getBitWidth()->isValueDependent() ||
3101 Bitfield->getBitWidth()->isTypeDependent() ||
3102 Init->isValueDependent() ||
3103 Init->isTypeDependent())
3104 return false;
3105
John McCall15d7d122010-11-11 03:21:53 +00003106 Expr *OriginalInit = Init->IgnoreParenImpCasts();
3107
3108 llvm::APSInt Width(32);
3109 Expr::EvalResult InitValue;
3110 if (!Bitfield->getBitWidth()->isIntegerConstantExpr(Width, S.Context) ||
John McCall91b60142010-11-11 05:33:51 +00003111 !OriginalInit->Evaluate(InitValue, S.Context) ||
John McCall15d7d122010-11-11 03:21:53 +00003112 !InitValue.Val.isInt())
3113 return false;
3114
3115 const llvm::APSInt &Value = InitValue.Val.getInt();
3116 unsigned OriginalWidth = Value.getBitWidth();
3117 unsigned FieldWidth = Width.getZExtValue();
3118
3119 if (OriginalWidth <= FieldWidth)
3120 return false;
3121
Jay Foad9f71a8f2010-12-07 08:25:34 +00003122 llvm::APSInt TruncatedValue = Value.trunc(FieldWidth);
John McCall15d7d122010-11-11 03:21:53 +00003123
3124 // It's fairly common to write values into signed bitfields
3125 // that, if sign-extended, would end up becoming a different
3126 // value. We don't want to warn about that.
3127 if (Value.isSigned() && Value.isNegative())
Jay Foad9f71a8f2010-12-07 08:25:34 +00003128 TruncatedValue = TruncatedValue.sext(OriginalWidth);
John McCall15d7d122010-11-11 03:21:53 +00003129 else
Jay Foad9f71a8f2010-12-07 08:25:34 +00003130 TruncatedValue = TruncatedValue.zext(OriginalWidth);
John McCall15d7d122010-11-11 03:21:53 +00003131
3132 if (Value == TruncatedValue)
3133 return false;
3134
3135 std::string PrettyValue = Value.toString(10);
3136 std::string PrettyTrunc = TruncatedValue.toString(10);
3137
3138 S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant)
3139 << PrettyValue << PrettyTrunc << OriginalInit->getType()
3140 << Init->getSourceRange();
3141
3142 return true;
3143}
3144
John McCallbeb22aa2010-11-09 23:24:47 +00003145/// Analyze the given simple or compound assignment for warning-worthy
3146/// operations.
3147void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
3148 // Just recurse on the LHS.
3149 AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc());
3150
3151 // We want to recurse on the RHS as normal unless we're assigning to
3152 // a bitfield.
3153 if (FieldDecl *Bitfield = E->getLHS()->getBitField()) {
John McCall15d7d122010-11-11 03:21:53 +00003154 if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(),
3155 E->getOperatorLoc())) {
3156 // Recurse, ignoring any implicit conversions on the RHS.
3157 return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(),
3158 E->getOperatorLoc());
John McCallbeb22aa2010-11-09 23:24:47 +00003159 }
3160 }
3161
3162 AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
3163}
3164
John McCall51313c32010-01-04 23:31:57 +00003165/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00003166void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T,
3167 SourceLocation CContext, unsigned diag) {
3168 S.Diag(E->getExprLoc(), diag)
3169 << SourceType << T << E->getSourceRange() << SourceRange(CContext);
3170}
3171
Chandler Carruthe1b02e02011-04-05 06:47:57 +00003172/// Diagnose an implicit cast; purely a helper for CheckImplicitConversion.
3173void DiagnoseImpCast(Sema &S, Expr *E, QualType T, SourceLocation CContext,
3174 unsigned diag) {
3175 DiagnoseImpCast(S, E, E->getType(), T, CContext, diag);
3176}
3177
Chandler Carruthf65076e2011-04-10 08:36:24 +00003178/// Diagnose an implicit cast from a literal expression. Also attemps to supply
3179/// fixit hints when the cast wouldn't lose information to simply write the
3180/// expression with the expected type.
3181void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
3182 SourceLocation CContext) {
3183 // Emit the primary warning first, then try to emit a fixit hint note if
3184 // reasonable.
3185 S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer)
3186 << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext);
3187
3188 const llvm::APFloat &Value = FL->getValue();
3189
3190 // Don't attempt to fix PPC double double literals.
3191 if (&Value.getSemantics() == &llvm::APFloat::PPCDoubleDouble)
3192 return;
3193
Jeffrey Yasskin3e1ef782011-07-15 17:03:07 +00003194 // Try to convert this exactly to an integer.
Chandler Carruthf65076e2011-04-10 08:36:24 +00003195 bool isExact = false;
Jeffrey Yasskin3e1ef782011-07-15 17:03:07 +00003196 llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
3197 T->hasUnsignedIntegerRepresentation());
3198 if (Value.convertToInteger(IntegerValue,
Chandler Carruthf65076e2011-04-10 08:36:24 +00003199 llvm::APFloat::rmTowardZero, &isExact)
3200 != llvm::APFloat::opOK || !isExact)
3201 return;
3202
Jeffrey Yasskin3e1ef782011-07-15 17:03:07 +00003203 std::string LiteralValue = IntegerValue.toString(10);
Chandler Carruthf65076e2011-04-10 08:36:24 +00003204 S.Diag(FL->getExprLoc(), diag::note_fix_integral_float_as_integer)
3205 << FixItHint::CreateReplacement(FL->getSourceRange(), LiteralValue);
3206}
3207
John McCall091f23f2010-11-09 22:22:12 +00003208std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
3209 if (!Range.Width) return "0";
3210
3211 llvm::APSInt ValueInRange = Value;
3212 ValueInRange.setIsSigned(!Range.NonNegative);
Jay Foad9f71a8f2010-12-07 08:25:34 +00003213 ValueInRange = ValueInRange.trunc(Range.Width);
John McCall091f23f2010-11-09 22:22:12 +00003214 return ValueInRange.toString(10);
3215}
3216
Ted Kremenekef9ff882011-03-10 20:03:42 +00003217static bool isFromSystemMacro(Sema &S, SourceLocation loc) {
3218 SourceManager &smgr = S.Context.getSourceManager();
3219 return loc.isMacroID() && smgr.isInSystemHeader(smgr.getSpellingLoc(loc));
3220}
Chandler Carruthf65076e2011-04-10 08:36:24 +00003221
John McCall323ed742010-05-06 08:58:33 +00003222void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
John McCallb4eb64d2010-10-08 02:01:28 +00003223 SourceLocation CC, bool *ICContext = 0) {
John McCall323ed742010-05-06 08:58:33 +00003224 if (E->isTypeDependent() || E->isValueDependent()) return;
John McCall51313c32010-01-04 23:31:57 +00003225
John McCall323ed742010-05-06 08:58:33 +00003226 const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
3227 const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
3228 if (Source == Target) return;
3229 if (Target->isDependentType()) return;
John McCall51313c32010-01-04 23:31:57 +00003230
Chandler Carruth108f7562011-07-26 05:40:03 +00003231 // If the conversion context location is invalid don't complain. We also
3232 // don't want to emit a warning if the issue occurs from the expansion of
3233 // a system macro. The problem is that 'getSpellingLoc()' is slow, so we
3234 // delay this check as long as possible. Once we detect we are in that
3235 // scenario, we just return.
Ted Kremenekef9ff882011-03-10 20:03:42 +00003236 if (CC.isInvalid())
John McCallb4eb64d2010-10-08 02:01:28 +00003237 return;
3238
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00003239 // Diagnose implicit casts to bool.
3240 if (Target->isSpecificBuiltinType(BuiltinType::Bool)) {
3241 if (isa<StringLiteral>(E))
3242 // Warn on string literal to bool. Checks for string literals in logical
3243 // expressions, for instances, assert(0 && "error here"), is prevented
3244 // by a check in AnalyzeImplicitConversions().
3245 return DiagnoseImpCast(S, E, T, CC,
3246 diag::warn_impcast_string_literal_to_bool);
David Blaikiee37cdc42011-09-29 04:06:47 +00003247 return; // Other casts to bool are not checked.
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00003248 }
John McCall51313c32010-01-04 23:31:57 +00003249
3250 // Strip vector types.
3251 if (isa<VectorType>(Source)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00003252 if (!isa<VectorType>(Target)) {
3253 if (isFromSystemMacro(S, CC))
3254 return;
John McCallb4eb64d2010-10-08 02:01:28 +00003255 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar);
Ted Kremenekef9ff882011-03-10 20:03:42 +00003256 }
Chris Lattnerb792b302011-06-14 04:51:15 +00003257
3258 // If the vector cast is cast between two vectors of the same size, it is
3259 // a bitcast, not a conversion.
3260 if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target))
3261 return;
John McCall51313c32010-01-04 23:31:57 +00003262
3263 Source = cast<VectorType>(Source)->getElementType().getTypePtr();
3264 Target = cast<VectorType>(Target)->getElementType().getTypePtr();
3265 }
3266
3267 // Strip complex types.
3268 if (isa<ComplexType>(Source)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00003269 if (!isa<ComplexType>(Target)) {
3270 if (isFromSystemMacro(S, CC))
3271 return;
3272
John McCallb4eb64d2010-10-08 02:01:28 +00003273 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar);
Ted Kremenekef9ff882011-03-10 20:03:42 +00003274 }
John McCall51313c32010-01-04 23:31:57 +00003275
3276 Source = cast<ComplexType>(Source)->getElementType().getTypePtr();
3277 Target = cast<ComplexType>(Target)->getElementType().getTypePtr();
3278 }
3279
3280 const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source);
3281 const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target);
3282
3283 // If the source is floating point...
3284 if (SourceBT && SourceBT->isFloatingPoint()) {
3285 // ...and the target is floating point...
3286 if (TargetBT && TargetBT->isFloatingPoint()) {
3287 // ...then warn if we're dropping FP rank.
3288
3289 // Builtin FP kinds are ordered by increasing FP rank.
3290 if (SourceBT->getKind() > TargetBT->getKind()) {
3291 // Don't warn about float constants that are precisely
3292 // representable in the target type.
3293 Expr::EvalResult result;
John McCall323ed742010-05-06 08:58:33 +00003294 if (E->Evaluate(result, S.Context)) {
John McCall51313c32010-01-04 23:31:57 +00003295 // Value might be a float, a float vector, or a float complex.
3296 if (IsSameFloatAfterCast(result.Val,
John McCall323ed742010-05-06 08:58:33 +00003297 S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)),
3298 S.Context.getFloatTypeSemantics(QualType(SourceBT, 0))))
John McCall51313c32010-01-04 23:31:57 +00003299 return;
3300 }
3301
Ted Kremenekef9ff882011-03-10 20:03:42 +00003302 if (isFromSystemMacro(S, CC))
3303 return;
3304
John McCallb4eb64d2010-10-08 02:01:28 +00003305 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
John McCall51313c32010-01-04 23:31:57 +00003306 }
3307 return;
3308 }
3309
Ted Kremenekef9ff882011-03-10 20:03:42 +00003310 // If the target is integral, always warn.
Chandler Carrutha5b93322011-02-17 11:05:49 +00003311 if ((TargetBT && TargetBT->isInteger())) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00003312 if (isFromSystemMacro(S, CC))
3313 return;
3314
Chandler Carrutha5b93322011-02-17 11:05:49 +00003315 Expr *InnerE = E->IgnoreParenImpCasts();
Matt Beaumont-Gay634c8af2011-09-08 22:30:47 +00003316 // We also want to warn on, e.g., "int i = -1.234"
3317 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE))
3318 if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus)
3319 InnerE = UOp->getSubExpr()->IgnoreParenImpCasts();
3320
Chandler Carruthf65076e2011-04-10 08:36:24 +00003321 if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) {
3322 DiagnoseFloatingLiteralImpCast(S, FL, T, CC);
Chandler Carrutha5b93322011-02-17 11:05:49 +00003323 } else {
3324 DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
3325 }
3326 }
John McCall51313c32010-01-04 23:31:57 +00003327
3328 return;
3329 }
3330
John McCallf2370c92010-01-06 05:24:50 +00003331 if (!Source->isIntegerType() || !Target->isIntegerType())
John McCall51313c32010-01-04 23:31:57 +00003332 return;
3333
Richard Trieu1838ca52011-05-29 19:59:02 +00003334 if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull)
3335 == Expr::NPCK_GNUNull) && Target->isIntegerType()) {
3336 S.Diag(E->getExprLoc(), diag::warn_impcast_null_pointer_to_integer)
3337 << E->getSourceRange() << clang::SourceRange(CC);
3338 return;
3339 }
3340
John McCall323ed742010-05-06 08:58:33 +00003341 IntRange SourceRange = GetExprRange(S.Context, E);
John McCall1844a6e2010-11-10 23:38:19 +00003342 IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
John McCallf2370c92010-01-06 05:24:50 +00003343
3344 if (SourceRange.Width > TargetRange.Width) {
John McCall091f23f2010-11-09 22:22:12 +00003345 // If the source is a constant, use a default-on diagnostic.
3346 // TODO: this should happen for bitfield stores, too.
3347 llvm::APSInt Value(32);
3348 if (E->isIntegerConstantExpr(Value, S.Context)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00003349 if (isFromSystemMacro(S, CC))
3350 return;
3351
John McCall091f23f2010-11-09 22:22:12 +00003352 std::string PrettySourceValue = Value.toString(10);
3353 std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
3354
3355 S.Diag(E->getExprLoc(), diag::warn_impcast_integer_precision_constant)
3356 << PrettySourceValue << PrettyTargetValue
3357 << E->getType() << T << E->getSourceRange() << clang::SourceRange(CC);
3358 return;
3359 }
3360
Chris Lattnerb792b302011-06-14 04:51:15 +00003361 // People want to build with -Wshorten-64-to-32 and not -Wconversion.
Ted Kremenekef9ff882011-03-10 20:03:42 +00003362 if (isFromSystemMacro(S, CC))
3363 return;
3364
John McCallf2370c92010-01-06 05:24:50 +00003365 if (SourceRange.Width == 64 && TargetRange.Width == 32)
John McCallb4eb64d2010-10-08 02:01:28 +00003366 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32);
3367 return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision);
John McCall323ed742010-05-06 08:58:33 +00003368 }
3369
3370 if ((TargetRange.NonNegative && !SourceRange.NonNegative) ||
3371 (!TargetRange.NonNegative && SourceRange.NonNegative &&
3372 SourceRange.Width == TargetRange.Width)) {
Ted Kremenekef9ff882011-03-10 20:03:42 +00003373
3374 if (isFromSystemMacro(S, CC))
3375 return;
3376
John McCall323ed742010-05-06 08:58:33 +00003377 unsigned DiagID = diag::warn_impcast_integer_sign;
3378
3379 // Traditionally, gcc has warned about this under -Wsign-compare.
3380 // We also want to warn about it in -Wconversion.
3381 // So if -Wconversion is off, use a completely identical diagnostic
3382 // in the sign-compare group.
3383 // The conditional-checking code will
3384 if (ICContext) {
3385 DiagID = diag::warn_impcast_integer_sign_conditional;
3386 *ICContext = true;
3387 }
3388
John McCallb4eb64d2010-10-08 02:01:28 +00003389 return DiagnoseImpCast(S, E, T, CC, DiagID);
John McCall51313c32010-01-04 23:31:57 +00003390 }
3391
Douglas Gregor284cc8d2011-02-22 02:45:07 +00003392 // Diagnose conversions between different enumeration types.
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00003393 // In C, we pretend that the type of an EnumConstantDecl is its enumeration
3394 // type, to give us better diagnostics.
3395 QualType SourceType = E->getType();
3396 if (!S.getLangOptions().CPlusPlus) {
3397 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
3398 if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
3399 EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext());
3400 SourceType = S.Context.getTypeDeclType(Enum);
3401 Source = S.Context.getCanonicalType(SourceType).getTypePtr();
3402 }
3403 }
3404
Douglas Gregor284cc8d2011-02-22 02:45:07 +00003405 if (const EnumType *SourceEnum = Source->getAs<EnumType>())
3406 if (const EnumType *TargetEnum = Target->getAs<EnumType>())
3407 if ((SourceEnum->getDecl()->getIdentifier() ||
Richard Smith162e1c12011-04-15 14:24:37 +00003408 SourceEnum->getDecl()->getTypedefNameForAnonDecl()) &&
Douglas Gregor284cc8d2011-02-22 02:45:07 +00003409 (TargetEnum->getDecl()->getIdentifier() ||
Richard Smith162e1c12011-04-15 14:24:37 +00003410 TargetEnum->getDecl()->getTypedefNameForAnonDecl()) &&
Ted Kremenekef9ff882011-03-10 20:03:42 +00003411 SourceEnum != TargetEnum) {
3412 if (isFromSystemMacro(S, CC))
3413 return;
3414
Douglas Gregor5a5b38f2011-03-12 00:14:31 +00003415 return DiagnoseImpCast(S, E, SourceType, T, CC,
Douglas Gregor284cc8d2011-02-22 02:45:07 +00003416 diag::warn_impcast_different_enum_types);
Ted Kremenekef9ff882011-03-10 20:03:42 +00003417 }
Douglas Gregor284cc8d2011-02-22 02:45:07 +00003418
John McCall51313c32010-01-04 23:31:57 +00003419 return;
3420}
3421
John McCall323ed742010-05-06 08:58:33 +00003422void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T);
3423
3424void CheckConditionalOperand(Sema &S, Expr *E, QualType T,
John McCallb4eb64d2010-10-08 02:01:28 +00003425 SourceLocation CC, bool &ICContext) {
John McCall323ed742010-05-06 08:58:33 +00003426 E = E->IgnoreParenImpCasts();
3427
3428 if (isa<ConditionalOperator>(E))
3429 return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T);
3430
John McCallb4eb64d2010-10-08 02:01:28 +00003431 AnalyzeImplicitConversions(S, E, CC);
John McCall323ed742010-05-06 08:58:33 +00003432 if (E->getType() != T)
John McCallb4eb64d2010-10-08 02:01:28 +00003433 return CheckImplicitConversion(S, E, T, CC, &ICContext);
John McCall323ed742010-05-06 08:58:33 +00003434 return;
3435}
3436
3437void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) {
John McCallb4eb64d2010-10-08 02:01:28 +00003438 SourceLocation CC = E->getQuestionLoc();
3439
3440 AnalyzeImplicitConversions(S, E->getCond(), CC);
John McCall323ed742010-05-06 08:58:33 +00003441
3442 bool Suspicious = false;
John McCallb4eb64d2010-10-08 02:01:28 +00003443 CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious);
3444 CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious);
John McCall323ed742010-05-06 08:58:33 +00003445
3446 // If -Wconversion would have warned about either of the candidates
3447 // for a signedness conversion to the context type...
3448 if (!Suspicious) return;
3449
3450 // ...but it's currently ignored...
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00003451 if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional,
3452 CC))
John McCall323ed742010-05-06 08:58:33 +00003453 return;
3454
John McCall323ed742010-05-06 08:58:33 +00003455 // ...then check whether it would have warned about either of the
3456 // candidates for a signedness conversion to the condition type.
Richard Trieu52541612011-07-21 02:46:28 +00003457 if (E->getType() == T) return;
3458
3459 Suspicious = false;
3460 CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(),
3461 E->getType(), CC, &Suspicious);
3462 if (!Suspicious)
3463 CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(),
John McCallb4eb64d2010-10-08 02:01:28 +00003464 E->getType(), CC, &Suspicious);
John McCall323ed742010-05-06 08:58:33 +00003465}
3466
3467/// AnalyzeImplicitConversions - Find and report any interesting
3468/// implicit conversions in the given expression. There are a couple
3469/// of competing diagnostics here, -Wconversion and -Wsign-compare.
John McCallb4eb64d2010-10-08 02:01:28 +00003470void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) {
John McCall323ed742010-05-06 08:58:33 +00003471 QualType T = OrigE->getType();
3472 Expr *E = OrigE->IgnoreParenImpCasts();
3473
3474 // For conditional operators, we analyze the arguments as if they
3475 // were being fed directly into the output.
3476 if (isa<ConditionalOperator>(E)) {
3477 ConditionalOperator *CO = cast<ConditionalOperator>(E);
3478 CheckConditionalOperator(S, CO, T);
3479 return;
3480 }
3481
3482 // Go ahead and check any implicit conversions we might have skipped.
3483 // The non-canonical typecheck is just an optimization;
3484 // CheckImplicitConversion will filter out dead implicit conversions.
3485 if (E->getType() != T)
John McCallb4eb64d2010-10-08 02:01:28 +00003486 CheckImplicitConversion(S, E, T, CC);
John McCall323ed742010-05-06 08:58:33 +00003487
3488 // Now continue drilling into this expression.
3489
3490 // Skip past explicit casts.
3491 if (isa<ExplicitCastExpr>(E)) {
3492 E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts();
John McCallb4eb64d2010-10-08 02:01:28 +00003493 return AnalyzeImplicitConversions(S, E, CC);
John McCall323ed742010-05-06 08:58:33 +00003494 }
3495
John McCallbeb22aa2010-11-09 23:24:47 +00003496 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3497 // Do a somewhat different check with comparison operators.
3498 if (BO->isComparisonOp())
3499 return AnalyzeComparison(S, BO);
3500
3501 // And with assignments and compound assignments.
3502 if (BO->isAssignmentOp())
3503 return AnalyzeAssignment(S, BO);
3504 }
John McCall323ed742010-05-06 08:58:33 +00003505
3506 // These break the otherwise-useful invariant below. Fortunately,
3507 // we don't really need to recurse into them, because any internal
3508 // expressions should have been analyzed already when they were
3509 // built into statements.
3510 if (isa<StmtExpr>(E)) return;
3511
3512 // Don't descend into unevaluated contexts.
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003513 if (isa<UnaryExprOrTypeTraitExpr>(E)) return;
John McCall323ed742010-05-06 08:58:33 +00003514
3515 // Now just recurse over the expression's children.
John McCallb4eb64d2010-10-08 02:01:28 +00003516 CC = E->getExprLoc();
Richard Trieuf1f8b1a2011-09-23 20:10:00 +00003517 BinaryOperator *BO = dyn_cast<BinaryOperator>(E);
3518 bool IsLogicalOperator = BO && BO->isLogicalOp();
3519 for (Stmt::child_range I = E->children(); I; ++I) {
3520 Expr *ChildExpr = cast<Expr>(*I);
3521 if (IsLogicalOperator &&
3522 isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts()))
3523 // Ignore checking string literals that are in logical operators.
3524 continue;
3525 AnalyzeImplicitConversions(S, ChildExpr, CC);
3526 }
John McCall323ed742010-05-06 08:58:33 +00003527}
3528
3529} // end anonymous namespace
3530
3531/// Diagnoses "dangerous" implicit conversions within the given
3532/// expression (which is a full expression). Implements -Wconversion
3533/// and -Wsign-compare.
John McCallb4eb64d2010-10-08 02:01:28 +00003534///
3535/// \param CC the "context" location of the implicit conversion, i.e.
3536/// the most location of the syntactic entity requiring the implicit
3537/// conversion
3538void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) {
John McCall323ed742010-05-06 08:58:33 +00003539 // Don't diagnose in unevaluated contexts.
3540 if (ExprEvalContexts.back().Context == Sema::Unevaluated)
3541 return;
3542
3543 // Don't diagnose for value- or type-dependent expressions.
3544 if (E->isTypeDependent() || E->isValueDependent())
3545 return;
3546
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003547 // Check for array bounds violations in cases where the check isn't triggered
3548 // elsewhere for other Expr types (like BinaryOperators), e.g. when an
3549 // ArraySubscriptExpr is on the RHS of a variable initialization.
3550 CheckArrayAccess(E);
3551
John McCallb4eb64d2010-10-08 02:01:28 +00003552 // This is not the right CC for (e.g.) a variable initialization.
3553 AnalyzeImplicitConversions(*this, E, CC);
John McCall323ed742010-05-06 08:58:33 +00003554}
3555
John McCall15d7d122010-11-11 03:21:53 +00003556void Sema::CheckBitFieldInitialization(SourceLocation InitLoc,
3557 FieldDecl *BitField,
3558 Expr *Init) {
3559 (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc);
3560}
3561
Mike Stumpf8c49212010-01-21 03:59:47 +00003562/// CheckParmsForFunctionDef - Check that the parameters of the given
3563/// function are appropriate for the definition of a function. This
3564/// takes care of any checks that cannot be performed on the
3565/// declaration itself, e.g., that the types of each of the function
3566/// parameters are complete.
Douglas Gregor82aa7132010-11-01 18:37:59 +00003567bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd,
3568 bool CheckParameterNames) {
Mike Stumpf8c49212010-01-21 03:59:47 +00003569 bool HasInvalidParm = false;
Douglas Gregor82aa7132010-11-01 18:37:59 +00003570 for (; P != PEnd; ++P) {
3571 ParmVarDecl *Param = *P;
3572
Mike Stumpf8c49212010-01-21 03:59:47 +00003573 // C99 6.7.5.3p4: the parameters in a parameter type list in a
3574 // function declarator that is part of a function definition of
3575 // that function shall not have incomplete type.
3576 //
3577 // This is also C++ [dcl.fct]p6.
3578 if (!Param->isInvalidDecl() &&
3579 RequireCompleteType(Param->getLocation(), Param->getType(),
3580 diag::err_typecheck_decl_incomplete_type)) {
3581 Param->setInvalidDecl();
3582 HasInvalidParm = true;
3583 }
3584
3585 // C99 6.9.1p5: If the declarator includes a parameter type list, the
3586 // declaration of each parameter shall include an identifier.
Douglas Gregor82aa7132010-11-01 18:37:59 +00003587 if (CheckParameterNames &&
3588 Param->getIdentifier() == 0 &&
Mike Stumpf8c49212010-01-21 03:59:47 +00003589 !Param->isImplicit() &&
3590 !getLangOptions().CPlusPlus)
3591 Diag(Param->getLocation(), diag::err_parameter_name_omitted);
Sam Weinigd17e3402010-02-01 05:02:49 +00003592
3593 // C99 6.7.5.3p12:
3594 // If the function declarator is not part of a definition of that
3595 // function, parameters may have incomplete type and may use the [*]
3596 // notation in their sequences of declarator specifiers to specify
3597 // variable length array types.
3598 QualType PType = Param->getOriginalType();
3599 if (const ArrayType *AT = Context.getAsArrayType(PType)) {
3600 if (AT->getSizeModifier() == ArrayType::Star) {
3601 // FIXME: This diagnosic should point the the '[*]' if source-location
3602 // information is added for it.
3603 Diag(Param->getLocation(), diag::err_array_star_in_function_definition);
3604 }
3605 }
Mike Stumpf8c49212010-01-21 03:59:47 +00003606 }
3607
3608 return HasInvalidParm;
3609}
John McCallb7f4ffe2010-08-12 21:44:57 +00003610
3611/// CheckCastAlign - Implements -Wcast-align, which warns when a
3612/// pointer cast increases the alignment requirements.
3613void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
3614 // This is actually a lot of work to potentially be doing on every
3615 // cast; don't do it if we're ignoring -Wcast_align (as is the default).
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00003616 if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align,
3617 TRange.getBegin())
David Blaikied6471f72011-09-25 23:23:43 +00003618 == DiagnosticsEngine::Ignored)
John McCallb7f4ffe2010-08-12 21:44:57 +00003619 return;
3620
3621 // Ignore dependent types.
3622 if (T->isDependentType() || Op->getType()->isDependentType())
3623 return;
3624
3625 // Require that the destination be a pointer type.
3626 const PointerType *DestPtr = T->getAs<PointerType>();
3627 if (!DestPtr) return;
3628
3629 // If the destination has alignment 1, we're done.
3630 QualType DestPointee = DestPtr->getPointeeType();
3631 if (DestPointee->isIncompleteType()) return;
3632 CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
3633 if (DestAlign.isOne()) return;
3634
3635 // Require that the source be a pointer type.
3636 const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
3637 if (!SrcPtr) return;
3638 QualType SrcPointee = SrcPtr->getPointeeType();
3639
3640 // Whitelist casts from cv void*. We already implicitly
3641 // whitelisted casts to cv void*, since they have alignment 1.
3642 // Also whitelist casts involving incomplete types, which implicitly
3643 // includes 'void'.
3644 if (SrcPointee->isIncompleteType()) return;
3645
3646 CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
3647 if (SrcAlign >= DestAlign) return;
3648
3649 Diag(TRange.getBegin(), diag::warn_cast_align)
3650 << Op->getType() << T
3651 << static_cast<unsigned>(SrcAlign.getQuantity())
3652 << static_cast<unsigned>(DestAlign.getQuantity())
3653 << TRange << Op->getSourceRange();
3654}
3655
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003656static const Type* getElementType(const Expr *BaseExpr) {
3657 const Type* EltType = BaseExpr->getType().getTypePtr();
3658 if (EltType->isAnyPointerType())
3659 return EltType->getPointeeType().getTypePtr();
3660 else if (EltType->isArrayType())
3661 return EltType->getBaseElementTypeUnsafe();
3662 return EltType;
3663}
3664
Chandler Carruthc2684342011-08-05 09:10:50 +00003665/// \brief Check whether this array fits the idiom of a size-one tail padded
3666/// array member of a struct.
3667///
3668/// We avoid emitting out-of-bounds access warnings for such arrays as they are
3669/// commonly used to emulate flexible arrays in C89 code.
3670static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
3671 const NamedDecl *ND) {
3672 if (Size != 1 || !ND) return false;
3673
3674 const FieldDecl *FD = dyn_cast<FieldDecl>(ND);
3675 if (!FD) return false;
3676
3677 // Don't consider sizes resulting from macro expansions or template argument
3678 // substitution to form C89 tail-padded arrays.
3679 ConstantArrayTypeLoc TL =
3680 cast<ConstantArrayTypeLoc>(FD->getTypeSourceInfo()->getTypeLoc());
3681 const Expr *SizeExpr = dyn_cast<IntegerLiteral>(TL.getSizeExpr());
3682 if (!SizeExpr || SizeExpr->getExprLoc().isMacroID())
3683 return false;
3684
3685 const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext());
3686 if (!RD || !RD->isStruct())
3687 return false;
3688
Benjamin Kramer22d4fed2011-08-06 03:04:42 +00003689 // See if this is the last field decl in the record.
3690 const Decl *D = FD;
3691 while ((D = D->getNextDeclInContext()))
3692 if (isa<FieldDecl>(D))
3693 return false;
3694 return true;
Chandler Carruthc2684342011-08-05 09:10:50 +00003695}
3696
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003697void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
3698 bool isSubscript, bool AllowOnePastEnd) {
3699 const Type* EffectiveType = getElementType(BaseExpr);
3700 BaseExpr = BaseExpr->IgnoreParenCasts();
3701 IndexExpr = IndexExpr->IgnoreParenCasts();
3702
Chandler Carruth34064582011-02-17 20:55:08 +00003703 const ConstantArrayType *ArrayTy =
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003704 Context.getAsConstantArrayType(BaseExpr->getType());
Chandler Carruth34064582011-02-17 20:55:08 +00003705 if (!ArrayTy)
Ted Kremeneka0125d82011-02-16 01:57:07 +00003706 return;
Chandler Carruth35001ca2011-02-17 21:10:52 +00003707
Chandler Carruth34064582011-02-17 20:55:08 +00003708 if (IndexExpr->isValueDependent())
Ted Kremeneka0125d82011-02-16 01:57:07 +00003709 return;
Chandler Carruth34064582011-02-17 20:55:08 +00003710 llvm::APSInt index;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003711 if (!IndexExpr->isIntegerConstantExpr(index, Context))
Ted Kremeneka0125d82011-02-16 01:57:07 +00003712 return;
Ted Kremenek8fd0a5d2011-02-16 04:01:44 +00003713
Chandler Carruthba447122011-08-05 08:07:29 +00003714 const NamedDecl *ND = NULL;
Chandler Carruthba447122011-08-05 08:07:29 +00003715 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
3716 ND = dyn_cast<NamedDecl>(DRE->getDecl());
Chandler Carruthc2684342011-08-05 09:10:50 +00003717 if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
Chandler Carruthba447122011-08-05 08:07:29 +00003718 ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
Chandler Carruthba447122011-08-05 08:07:29 +00003719
Ted Kremenek9e060ca2011-02-23 23:06:04 +00003720 if (index.isUnsigned() || !index.isNegative()) {
Ted Kremenek25b3b842011-02-18 02:27:00 +00003721 llvm::APInt size = ArrayTy->getSize();
Chandler Carruth35001ca2011-02-17 21:10:52 +00003722 if (!size.isStrictlyPositive())
3723 return;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003724
3725 const Type* BaseType = getElementType(BaseExpr);
Nico Weberde5998f2011-09-17 22:59:41 +00003726 if (BaseType != EffectiveType) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003727 // Make sure we're comparing apples to apples when comparing index to size
3728 uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType);
3729 uint64_t array_typesize = Context.getTypeSize(BaseType);
Kaelyn Uhraind10f4bc2011-08-10 19:47:25 +00003730 // Handle ptrarith_typesize being zero, such as when casting to void*
Kaelyn Uhrain18f16972011-08-10 18:49:28 +00003731 if (!ptrarith_typesize) ptrarith_typesize = 1;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003732 if (ptrarith_typesize != array_typesize) {
3733 // There's a cast to a different size type involved
3734 uint64_t ratio = array_typesize / ptrarith_typesize;
3735 // TODO: Be smarter about handling cases where array_typesize is not a
3736 // multiple of ptrarith_typesize
3737 if (ptrarith_typesize * ratio == array_typesize)
3738 size *= llvm::APInt(size.getBitWidth(), ratio);
3739 }
3740 }
3741
Chandler Carruth34064582011-02-17 20:55:08 +00003742 if (size.getBitWidth() > index.getBitWidth())
3743 index = index.sext(size.getBitWidth());
Ted Kremenek25b3b842011-02-18 02:27:00 +00003744 else if (size.getBitWidth() < index.getBitWidth())
3745 size = size.sext(index.getBitWidth());
3746
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003747 // For array subscripting the index must be less than size, but for pointer
3748 // arithmetic also allow the index (offset) to be equal to size since
3749 // computing the next address after the end of the array is legal and
3750 // commonly done e.g. in C++ iterators and range-based for loops.
3751 if (AllowOnePastEnd ? index.sle(size) : index.slt(size))
Chandler Carruthba447122011-08-05 08:07:29 +00003752 return;
3753
3754 // Also don't warn for arrays of size 1 which are members of some
3755 // structure. These are often used to approximate flexible arrays in C89
3756 // code.
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003757 if (IsTailPaddedMemberArray(*this, size, ND))
Ted Kremenek8fd0a5d2011-02-16 04:01:44 +00003758 return;
Chandler Carruth34064582011-02-17 20:55:08 +00003759
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003760 unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds;
3761 if (isSubscript)
3762 DiagID = diag::warn_array_index_exceeds_bounds;
3763
3764 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
3765 PDiag(DiagID) << index.toString(10, true)
3766 << size.toString(10, true)
3767 << (unsigned)size.getLimitedValue(~0U)
3768 << IndexExpr->getSourceRange());
Chandler Carruth34064582011-02-17 20:55:08 +00003769 } else {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003770 unsigned DiagID = diag::warn_array_index_precedes_bounds;
3771 if (!isSubscript) {
3772 DiagID = diag::warn_ptr_arith_precedes_bounds;
3773 if (index.isNegative()) index = -index;
3774 }
3775
3776 DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr,
3777 PDiag(DiagID) << index.toString(10, true)
3778 << IndexExpr->getSourceRange());
Ted Kremeneka0125d82011-02-16 01:57:07 +00003779 }
Chandler Carruth35001ca2011-02-17 21:10:52 +00003780
Chandler Carruth35001ca2011-02-17 21:10:52 +00003781 if (ND)
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003782 DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
3783 PDiag(diag::note_array_index_out_of_bounds)
3784 << ND->getDeclName());
Ted Kremeneka0125d82011-02-16 01:57:07 +00003785}
3786
Ted Kremenek3aea4da2011-03-01 18:41:00 +00003787void Sema::CheckArrayAccess(const Expr *expr) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003788 int AllowOnePastEnd = 0;
3789 while (expr) {
3790 expr = expr->IgnoreParenImpCasts();
Ted Kremenek3aea4da2011-03-01 18:41:00 +00003791 switch (expr->getStmtClass()) {
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003792 case Stmt::ArraySubscriptExprClass: {
3793 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
3794 CheckArrayAccess(ASE->getBase(), ASE->getIdx(), true,
3795 AllowOnePastEnd > 0);
Ted Kremenek3aea4da2011-03-01 18:41:00 +00003796 return;
Kaelyn Uhraind6c88652011-08-05 23:18:04 +00003797 }
3798 case Stmt::UnaryOperatorClass: {
3799 // Only unwrap the * and & unary operators
3800 const UnaryOperator *UO = cast<UnaryOperator>(expr);
3801 expr = UO->getSubExpr();
3802 switch (UO->getOpcode()) {
3803 case UO_AddrOf:
3804 AllowOnePastEnd++;
3805 break;
3806 case UO_Deref:
3807 AllowOnePastEnd--;
3808 break;
3809 default:
3810 return;
3811 }
3812 break;
3813 }
Ted Kremenek3aea4da2011-03-01 18:41:00 +00003814 case Stmt::ConditionalOperatorClass: {
3815 const ConditionalOperator *cond = cast<ConditionalOperator>(expr);
3816 if (const Expr *lhs = cond->getLHS())
3817 CheckArrayAccess(lhs);
3818 if (const Expr *rhs = cond->getRHS())
3819 CheckArrayAccess(rhs);
3820 return;
3821 }
3822 default:
3823 return;
3824 }
Peter Collingbournef111d932011-04-15 00:35:48 +00003825 }
Ted Kremenek3aea4da2011-03-01 18:41:00 +00003826}
John McCallf85e1932011-06-15 23:02:42 +00003827
3828//===--- CHECK: Objective-C retain cycles ----------------------------------//
3829
3830namespace {
3831 struct RetainCycleOwner {
3832 RetainCycleOwner() : Variable(0), Indirect(false) {}
3833 VarDecl *Variable;
3834 SourceRange Range;
3835 SourceLocation Loc;
3836 bool Indirect;
3837
3838 void setLocsFrom(Expr *e) {
3839 Loc = e->getExprLoc();
3840 Range = e->getSourceRange();
3841 }
3842 };
3843}
3844
3845/// Consider whether capturing the given variable can possibly lead to
3846/// a retain cycle.
3847static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) {
3848 // In ARC, it's captured strongly iff the variable has __strong
3849 // lifetime. In MRR, it's captured strongly if the variable is
3850 // __block and has an appropriate type.
3851 if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
3852 return false;
3853
3854 owner.Variable = var;
3855 owner.setLocsFrom(ref);
3856 return true;
3857}
3858
3859static bool findRetainCycleOwner(Expr *e, RetainCycleOwner &owner) {
3860 while (true) {
3861 e = e->IgnoreParens();
3862 if (CastExpr *cast = dyn_cast<CastExpr>(e)) {
3863 switch (cast->getCastKind()) {
3864 case CK_BitCast:
3865 case CK_LValueBitCast:
3866 case CK_LValueToRValue:
John McCall33e56f32011-09-10 06:18:15 +00003867 case CK_ARCReclaimReturnedObject:
John McCallf85e1932011-06-15 23:02:42 +00003868 e = cast->getSubExpr();
3869 continue;
3870
3871 case CK_GetObjCProperty: {
3872 // Bail out if this isn't a strong explicit property.
3873 const ObjCPropertyRefExpr *pre = cast->getSubExpr()->getObjCProperty();
3874 if (pre->isImplicitProperty()) return false;
3875 ObjCPropertyDecl *property = pre->getExplicitProperty();
John McCall265941b2011-09-13 18:31:23 +00003876 if (!property->isRetaining() &&
John McCallf85e1932011-06-15 23:02:42 +00003877 !(property->getPropertyIvarDecl() &&
3878 property->getPropertyIvarDecl()->getType()
3879 .getObjCLifetime() == Qualifiers::OCL_Strong))
3880 return false;
3881
3882 owner.Indirect = true;
3883 e = const_cast<Expr*>(pre->getBase());
3884 continue;
3885 }
3886
3887 default:
3888 return false;
3889 }
3890 }
3891
3892 if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) {
3893 ObjCIvarDecl *ivar = ref->getDecl();
3894 if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
3895 return false;
3896
3897 // Try to find a retain cycle in the base.
3898 if (!findRetainCycleOwner(ref->getBase(), owner))
3899 return false;
3900
3901 if (ref->isFreeIvar()) owner.setLocsFrom(ref);
3902 owner.Indirect = true;
3903 return true;
3904 }
3905
3906 if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) {
3907 VarDecl *var = dyn_cast<VarDecl>(ref->getDecl());
3908 if (!var) return false;
3909 return considerVariable(var, ref, owner);
3910 }
3911
3912 if (BlockDeclRefExpr *ref = dyn_cast<BlockDeclRefExpr>(e)) {
3913 owner.Variable = ref->getDecl();
3914 owner.setLocsFrom(ref);
3915 return true;
3916 }
3917
3918 if (MemberExpr *member = dyn_cast<MemberExpr>(e)) {
3919 if (member->isArrow()) return false;
3920
3921 // Don't count this as an indirect ownership.
3922 e = member->getBase();
3923 continue;
3924 }
3925
3926 // Array ivars?
3927
3928 return false;
3929 }
3930}
3931
3932namespace {
3933 struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> {
3934 FindCaptureVisitor(ASTContext &Context, VarDecl *variable)
3935 : EvaluatedExprVisitor<FindCaptureVisitor>(Context),
3936 Variable(variable), Capturer(0) {}
3937
3938 VarDecl *Variable;
3939 Expr *Capturer;
3940
3941 void VisitDeclRefExpr(DeclRefExpr *ref) {
3942 if (ref->getDecl() == Variable && !Capturer)
3943 Capturer = ref;
3944 }
3945
3946 void VisitBlockDeclRefExpr(BlockDeclRefExpr *ref) {
3947 if (ref->getDecl() == Variable && !Capturer)
3948 Capturer = ref;
3949 }
3950
3951 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) {
3952 if (Capturer) return;
3953 Visit(ref->getBase());
3954 if (Capturer && ref->isFreeIvar())
3955 Capturer = ref;
3956 }
3957
3958 void VisitBlockExpr(BlockExpr *block) {
3959 // Look inside nested blocks
3960 if (block->getBlockDecl()->capturesVariable(Variable))
3961 Visit(block->getBlockDecl()->getBody());
3962 }
3963 };
3964}
3965
3966/// Check whether the given argument is a block which captures a
3967/// variable.
3968static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) {
3969 assert(owner.Variable && owner.Loc.isValid());
3970
3971 e = e->IgnoreParenCasts();
3972 BlockExpr *block = dyn_cast<BlockExpr>(e);
3973 if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable))
3974 return 0;
3975
3976 FindCaptureVisitor visitor(S.Context, owner.Variable);
3977 visitor.Visit(block->getBlockDecl()->getBody());
3978 return visitor.Capturer;
3979}
3980
3981static void diagnoseRetainCycle(Sema &S, Expr *capturer,
3982 RetainCycleOwner &owner) {
3983 assert(capturer);
3984 assert(owner.Variable && owner.Loc.isValid());
3985
3986 S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle)
3987 << owner.Variable << capturer->getSourceRange();
3988 S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner)
3989 << owner.Indirect << owner.Range;
3990}
3991
3992/// Check for a keyword selector that starts with the word 'add' or
3993/// 'set'.
3994static bool isSetterLikeSelector(Selector sel) {
3995 if (sel.isUnarySelector()) return false;
3996
Chris Lattner5f9e2722011-07-23 10:55:15 +00003997 StringRef str = sel.getNameForSlot(0);
John McCallf85e1932011-06-15 23:02:42 +00003998 while (!str.empty() && str.front() == '_') str = str.substr(1);
3999 if (str.startswith("set") || str.startswith("add"))
4000 str = str.substr(3);
4001 else
4002 return false;
4003
4004 if (str.empty()) return true;
4005 return !islower(str.front());
4006}
4007
4008/// Check a message send to see if it's likely to cause a retain cycle.
4009void Sema::checkRetainCycles(ObjCMessageExpr *msg) {
4010 // Only check instance methods whose selector looks like a setter.
4011 if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector()))
4012 return;
4013
4014 // Try to find a variable that the receiver is strongly owned by.
4015 RetainCycleOwner owner;
4016 if (msg->getReceiverKind() == ObjCMessageExpr::Instance) {
4017 if (!findRetainCycleOwner(msg->getInstanceReceiver(), owner))
4018 return;
4019 } else {
4020 assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance);
4021 owner.Variable = getCurMethodDecl()->getSelfDecl();
4022 owner.Loc = msg->getSuperLoc();
4023 owner.Range = msg->getSuperLoc();
4024 }
4025
4026 // Check whether the receiver is captured by any of the arguments.
4027 for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i)
4028 if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner))
4029 return diagnoseRetainCycle(*this, capturer, owner);
4030}
4031
4032/// Check a property assign to see if it's likely to cause a retain cycle.
4033void Sema::checkRetainCycles(Expr *receiver, Expr *argument) {
4034 RetainCycleOwner owner;
4035 if (!findRetainCycleOwner(receiver, owner))
4036 return;
4037
4038 if (Expr *capturer = findCapturingExpr(*this, argument, owner))
4039 diagnoseRetainCycle(*this, capturer, owner);
4040}
4041
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004042bool Sema::checkUnsafeAssigns(SourceLocation Loc,
John McCallf85e1932011-06-15 23:02:42 +00004043 QualType LHS, Expr *RHS) {
4044 Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime();
4045 if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone)
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004046 return false;
4047 // strip off any implicit cast added to get to the one arc-specific
4048 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
John McCall33e56f32011-09-10 06:18:15 +00004049 if (cast->getCastKind() == CK_ARCConsumeObject) {
John McCallf85e1932011-06-15 23:02:42 +00004050 Diag(Loc, diag::warn_arc_retained_assign)
4051 << (LT == Qualifiers::OCL_ExplicitNone)
4052 << RHS->getSourceRange();
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004053 return true;
4054 }
4055 RHS = cast->getSubExpr();
4056 }
4057 return false;
John McCallf85e1932011-06-15 23:02:42 +00004058}
4059
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004060void Sema::checkUnsafeExprAssigns(SourceLocation Loc,
4061 Expr *LHS, Expr *RHS) {
4062 QualType LHSType = LHS->getType();
4063 if (checkUnsafeAssigns(Loc, LHSType, RHS))
4064 return;
4065 Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime();
4066 // FIXME. Check for other life times.
4067 if (LT != Qualifiers::OCL_None)
4068 return;
4069
4070 if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(LHS)) {
4071 if (PRE->isImplicitProperty())
4072 return;
4073 const ObjCPropertyDecl *PD = PRE->getExplicitProperty();
4074 if (!PD)
4075 return;
4076
4077 unsigned Attributes = PD->getPropertyAttributes();
4078 if (Attributes & ObjCPropertyDecl::OBJC_PR_assign)
4079 while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) {
John McCall33e56f32011-09-10 06:18:15 +00004080 if (cast->getCastKind() == CK_ARCConsumeObject) {
Fariborz Jahanian921c1432011-06-24 18:25:34 +00004081 Diag(Loc, diag::warn_arc_retained_property_assign)
4082 << RHS->getSourceRange();
4083 return;
4084 }
4085 RHS = cast->getSubExpr();
4086 }
4087 }
4088}