blob: 09474452a2894b6760b25e2e623757f466cb3837 [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//
10// This file implements extra semantic analysis beyond what is enforced
11// by the C type system.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
16#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Ted Kremenek23245122007-08-20 16:18:38 +000018#include "clang/AST/ExprCXX.h"
Ted Kremenek7ff22b22008-06-16 18:00:42 +000019#include "clang/AST/ExprObjC.h"
Chris Lattner59907c42007-08-10 20:18:51 +000020#include "clang/Lex/Preprocessor.h"
Chris Lattner59907c42007-08-10 20:18:51 +000021#include "clang/Basic/Diagnostic.h"
Ted Kremenek588e5eb2007-11-25 00:58:00 +000022#include "SemaUtil.h"
Chris Lattner59907c42007-08-10 20:18:51 +000023using namespace clang;
24
25/// CheckFunctionCall - Check a direct function call for various correctness
26/// and safety properties not strictly enforced by the C type system.
Eli Friedmand38617c2008-05-14 19:38:39 +000027Action::ExprResult
Eli Friedmane8018702008-05-16 17:51:27 +000028Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCallRaw) {
29 llvm::OwningPtr<CallExpr> TheCall(TheCallRaw);
Chris Lattner59907c42007-08-10 20:18:51 +000030 // Get the IdentifierInfo* for the called function.
31 IdentifierInfo *FnInfo = FDecl->getIdentifier();
Douglas Gregor2def4832008-11-17 20:34:05 +000032
33 // None of the checks below are needed for functions that don't have
34 // simple names (e.g., C++ conversion functions).
35 if (!FnInfo)
36 return TheCall.take();
37
Chris Lattner30ce3442007-12-19 23:59:04 +000038 switch (FnInfo->getBuiltinID()) {
39 case Builtin::BI__builtin___CFStringMakeConstantString:
Chris Lattner925e60d2007-12-28 05:29:59 +000040 assert(TheCall->getNumArgs() == 1 &&
Chris Lattner1b9a0792007-12-20 00:26:33 +000041 "Wrong # arguments to builtin CFStringMakeConstantString");
Eli Friedmane8018702008-05-16 17:51:27 +000042 if (CheckBuiltinCFStringArgument(TheCall->getArg(0)))
Eli Friedmand38617c2008-05-14 19:38:39 +000043 return true;
Eli Friedmane8018702008-05-16 17:51:27 +000044 return TheCall.take();
Ted Kremenek49ff7a12008-07-09 17:58:53 +000045 case Builtin::BI__builtin_stdarg_start:
Chris Lattner30ce3442007-12-19 23:59:04 +000046 case Builtin::BI__builtin_va_start:
Chris Lattnerb7cfe882008-06-30 18:32:54 +000047 if (SemaBuiltinVAStart(TheCall.get()))
Eli Friedmand38617c2008-05-14 19:38:39 +000048 return true;
Eli Friedmane8018702008-05-16 17:51:27 +000049 return TheCall.take();
Chris Lattner1b9a0792007-12-20 00:26:33 +000050 case Builtin::BI__builtin_isgreater:
51 case Builtin::BI__builtin_isgreaterequal:
52 case Builtin::BI__builtin_isless:
53 case Builtin::BI__builtin_islessequal:
54 case Builtin::BI__builtin_islessgreater:
55 case Builtin::BI__builtin_isunordered:
Eli Friedmane8018702008-05-16 17:51:27 +000056 if (SemaBuiltinUnorderedCompare(TheCall.get()))
Eli Friedmand38617c2008-05-14 19:38:39 +000057 return true;
Eli Friedmane8018702008-05-16 17:51:27 +000058 return TheCall.take();
Eli Friedman6cfda232008-05-20 08:23:37 +000059 case Builtin::BI__builtin_return_address:
60 case Builtin::BI__builtin_frame_address:
61 if (SemaBuiltinStackAddress(TheCall.get()))
62 return true;
63 return TheCall.take();
Eli Friedmand38617c2008-05-14 19:38:39 +000064 case Builtin::BI__builtin_shufflevector:
Eli Friedmane8018702008-05-16 17:51:27 +000065 return SemaBuiltinShuffleVector(TheCall.get());
Daniel Dunbar4493f792008-07-21 22:59:13 +000066 case Builtin::BI__builtin_prefetch:
67 if (SemaBuiltinPrefetch(TheCall.get()))
68 return true;
69 return TheCall.take();
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +000070 case Builtin::BI__builtin_object_size:
71 if (SemaBuiltinObjectSize(TheCall.get()))
72 return true;
Anders Carlsson71993dd2007-08-17 05:31:46 +000073 }
Daniel Dunbarde454282008-10-02 18:44:07 +000074
75 // FIXME: This mechanism should be abstracted to be less fragile and
76 // more efficient. For example, just map function ids to custom
77 // handlers.
78
Chris Lattner59907c42007-08-10 20:18:51 +000079 // Search the KnownFunctionIDs for the identifier.
80 unsigned i = 0, e = id_num_known_functions;
Ted Kremenek71895b92007-08-14 17:39:48 +000081 for (; i != e; ++i) { if (KnownFunctionIDs[i] == FnInfo) break; }
Eli Friedmane8018702008-05-16 17:51:27 +000082 if (i == e) return TheCall.take();
Chris Lattner59907c42007-08-10 20:18:51 +000083
84 // Printf checking.
85 if (i <= id_vprintf) {
Ted Kremenek71895b92007-08-14 17:39:48 +000086 // Retrieve the index of the format string parameter and determine
87 // if the function is passed a va_arg argument.
Chris Lattner59907c42007-08-10 20:18:51 +000088 unsigned format_idx = 0;
Ted Kremenek71895b92007-08-14 17:39:48 +000089 bool HasVAListArg = false;
90
Chris Lattner59907c42007-08-10 20:18:51 +000091 switch (i) {
Chris Lattner30ce3442007-12-19 23:59:04 +000092 default: assert(false && "No format string argument index.");
Daniel Dunbarde454282008-10-02 18:44:07 +000093 case id_NSLog: format_idx = 0; break;
94 case id_asprintf: format_idx = 1; break;
95 case id_fprintf: format_idx = 1; break;
96 case id_printf: format_idx = 0; break;
97 case id_snprintf: format_idx = 2; break;
98 case id_snprintf_chk: format_idx = 4; break;
99 case id_sprintf: format_idx = 1; break;
100 case id_sprintf_chk: format_idx = 3; break;
101 case id_vasprintf: format_idx = 1; HasVAListArg = true; break;
102 case id_vfprintf: format_idx = 1; HasVAListArg = true; break;
103 case id_vsnprintf: format_idx = 2; HasVAListArg = true; break;
104 case id_vsnprintf_chk: format_idx = 4; HasVAListArg = true; break;
105 case id_vsprintf: format_idx = 1; HasVAListArg = true; break;
106 case id_vsprintf_chk: format_idx = 3; HasVAListArg = true; break;
107 case id_vprintf: format_idx = 0; HasVAListArg = true; break;
Ted Kremenek71895b92007-08-14 17:39:48 +0000108 }
109
Eli Friedmane8018702008-05-16 17:51:27 +0000110 CheckPrintfArguments(TheCall.get(), HasVAListArg, format_idx);
Chris Lattner59907c42007-08-10 20:18:51 +0000111 }
Anders Carlsson71993dd2007-08-17 05:31:46 +0000112
Eli Friedmane8018702008-05-16 17:51:27 +0000113 return TheCall.take();
Anders Carlsson71993dd2007-08-17 05:31:46 +0000114}
115
116/// CheckBuiltinCFStringArgument - Checks that the argument to the builtin
117/// CFString constructor is correct
Chris Lattnercc6f65d2007-08-25 05:30:33 +0000118bool Sema::CheckBuiltinCFStringArgument(Expr* Arg) {
Chris Lattner56f34942008-02-13 01:02:39 +0000119 Arg = Arg->IgnoreParenCasts();
Anders Carlsson71993dd2007-08-17 05:31:46 +0000120
121 StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
122
123 if (!Literal || Literal->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000124 Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant)
125 << Arg->getSourceRange();
Anders Carlsson9cdc4d32007-08-17 15:44:17 +0000126 return true;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000127 }
128
129 const char *Data = Literal->getStrData();
130 unsigned Length = Literal->getByteLength();
131
132 for (unsigned i = 0; i < Length; ++i) {
133 if (!isascii(Data[i])) {
134 Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000135 diag::warn_cfstring_literal_contains_non_ascii_character)
136 << Arg->getSourceRange();
Anders Carlsson71993dd2007-08-17 05:31:46 +0000137 break;
138 }
139
140 if (!Data[i]) {
141 Diag(PP.AdvanceToTokenCharacter(Arg->getLocStart(), i + 1),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000142 diag::warn_cfstring_literal_contains_nul_character)
143 << Arg->getSourceRange();
Anders Carlsson71993dd2007-08-17 05:31:46 +0000144 break;
145 }
146 }
147
Anders Carlsson9cdc4d32007-08-17 15:44:17 +0000148 return false;
Chris Lattner59907c42007-08-10 20:18:51 +0000149}
150
Chris Lattnerc27c6652007-12-20 00:05:45 +0000151/// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity.
152/// Emit an error and return true on failure, return false on success.
Chris Lattner925e60d2007-12-28 05:29:59 +0000153bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) {
154 Expr *Fn = TheCall->getCallee();
155 if (TheCall->getNumArgs() > 2) {
Chris Lattner2c21a072008-11-21 18:44:24 +0000156 Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000157 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000158 << 0 /*function call*/ << Fn->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000159 << SourceRange(TheCall->getArg(2)->getLocStart(),
160 (*(TheCall->arg_end()-1))->getLocEnd());
Chris Lattner30ce3442007-12-19 23:59:04 +0000161 return true;
162 }
163
Chris Lattnerc27c6652007-12-20 00:05:45 +0000164 // Determine whether the current function is variadic or not.
165 bool isVariadic;
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000166 if (getCurFunctionDecl())
Chris Lattnerc27c6652007-12-20 00:05:45 +0000167 isVariadic =
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000168 cast<FunctionTypeProto>(getCurFunctionDecl()->getType())->isVariadic();
Chris Lattner30ce3442007-12-19 23:59:04 +0000169 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000170 isVariadic = getCurMethodDecl()->isVariadic();
Chris Lattner30ce3442007-12-19 23:59:04 +0000171
Chris Lattnerc27c6652007-12-20 00:05:45 +0000172 if (!isVariadic) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000173 Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
174 return true;
175 }
176
177 // Verify that the second argument to the builtin is the last argument of the
178 // current function or method.
179 bool SecondArgIsLastNamedArgument = false;
Anders Carlssone2c14102008-02-13 01:22:59 +0000180 const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts();
Anders Carlsson88cf2262008-02-11 04:20:54 +0000181
182 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) {
183 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
Chris Lattner30ce3442007-12-19 23:59:04 +0000184 // FIXME: This isn't correct for methods (results in bogus warning).
185 // Get the last formal in the current function.
Anders Carlsson88cf2262008-02-11 04:20:54 +0000186 const ParmVarDecl *LastArg;
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000187 if (getCurFunctionDecl())
188 LastArg = *(getCurFunctionDecl()->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +0000189 else
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000190 LastArg = *(getCurMethodDecl()->param_end()-1);
Chris Lattner30ce3442007-12-19 23:59:04 +0000191 SecondArgIsLastNamedArgument = PV == LastArg;
192 }
193 }
194
195 if (!SecondArgIsLastNamedArgument)
Chris Lattner925e60d2007-12-28 05:29:59 +0000196 Diag(TheCall->getArg(1)->getLocStart(),
Chris Lattner30ce3442007-12-19 23:59:04 +0000197 diag::warn_second_parameter_of_va_start_not_last_named_argument);
198 return false;
Eli Friedman6cfda232008-05-20 08:23:37 +0000199}
Chris Lattner30ce3442007-12-19 23:59:04 +0000200
Chris Lattner1b9a0792007-12-20 00:26:33 +0000201/// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and
202/// friends. This is declared to take (...), so we have to check everything.
Chris Lattner925e60d2007-12-28 05:29:59 +0000203bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) {
204 if (TheCall->getNumArgs() < 2)
Chris Lattner2c21a072008-11-21 18:44:24 +0000205 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
206 << 0 /*function call*/;
Chris Lattner925e60d2007-12-28 05:29:59 +0000207 if (TheCall->getNumArgs() > 2)
208 return Diag(TheCall->getArg(2)->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000209 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000210 << 0 /*function call*/
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000211 << SourceRange(TheCall->getArg(2)->getLocStart(),
212 (*(TheCall->arg_end()-1))->getLocEnd());
Chris Lattner1b9a0792007-12-20 00:26:33 +0000213
Chris Lattner925e60d2007-12-28 05:29:59 +0000214 Expr *OrigArg0 = TheCall->getArg(0);
215 Expr *OrigArg1 = TheCall->getArg(1);
Chris Lattner1b9a0792007-12-20 00:26:33 +0000216
217 // Do standard promotions between the two arguments, returning their common
218 // type.
Chris Lattner925e60d2007-12-28 05:29:59 +0000219 QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false);
Chris Lattner1b9a0792007-12-20 00:26:33 +0000220
221 // If the common type isn't a real floating type, then the arguments were
222 // invalid for this operation.
223 if (!Res->isRealFloatingType())
Chris Lattner925e60d2007-12-28 05:29:59 +0000224 return Diag(OrigArg0->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000225 diag::err_typecheck_call_invalid_ordered_compare)
226 << OrigArg0->getType().getAsString() << OrigArg1->getType().getAsString()
227 << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd());
Chris Lattner1b9a0792007-12-20 00:26:33 +0000228
229 return false;
230}
231
Eli Friedman6cfda232008-05-20 08:23:37 +0000232bool Sema::SemaBuiltinStackAddress(CallExpr *TheCall) {
233 // The signature for these builtins is exact; the only thing we need
234 // to check is that the argument is a constant.
235 SourceLocation Loc;
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000236 if (!TheCall->getArg(0)->isIntegerConstantExpr(Context, &Loc))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000237 return Diag(Loc, diag::err_stack_const_level) << TheCall->getSourceRange();
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000238
Eli Friedman6cfda232008-05-20 08:23:37 +0000239 return false;
240}
241
Eli Friedmand38617c2008-05-14 19:38:39 +0000242/// SemaBuiltinShuffleVector - Handle __builtin_shufflevector.
243// This is declared to take (...), so we have to check everything.
244Action::ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) {
245 if (TheCall->getNumArgs() < 3)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000246 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000247 << 0 /*function call*/ << TheCall->getSourceRange();
Eli Friedmand38617c2008-05-14 19:38:39 +0000248
249 QualType FAType = TheCall->getArg(0)->getType();
250 QualType SAType = TheCall->getArg(1)->getType();
251
252 if (!FAType->isVectorType() || !SAType->isVectorType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000253 Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector)
254 << SourceRange(TheCall->getArg(0)->getLocStart(),
255 TheCall->getArg(1)->getLocEnd());
Eli Friedmand38617c2008-05-14 19:38:39 +0000256 return true;
257 }
258
Chris Lattnerb77792e2008-07-26 22:17:49 +0000259 if (Context.getCanonicalType(FAType).getUnqualifiedType() !=
260 Context.getCanonicalType(SAType).getUnqualifiedType()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000261 Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector)
262 << SourceRange(TheCall->getArg(0)->getLocStart(),
263 TheCall->getArg(1)->getLocEnd());
Eli Friedmand38617c2008-05-14 19:38:39 +0000264 return true;
265 }
266
267 unsigned numElements = FAType->getAsVectorType()->getNumElements();
268 if (TheCall->getNumArgs() != numElements+2) {
269 if (TheCall->getNumArgs() < numElements+2)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000270 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000271 << 0 /*function call*/ << TheCall->getSourceRange();
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000272 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000273 << 0 /*function call*/ << TheCall->getSourceRange();
Eli Friedmand38617c2008-05-14 19:38:39 +0000274 }
275
276 for (unsigned i = 2; i < TheCall->getNumArgs(); i++) {
277 llvm::APSInt Result(32);
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000278 if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context))
279 return Diag(TheCall->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000280 diag::err_shufflevector_nonconstant_argument)
281 << TheCall->getArg(i)->getSourceRange();
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000282
283 if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2)
284 return Diag(TheCall->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000285 diag::err_shufflevector_argument_too_large)
286 << TheCall->getArg(i)->getSourceRange();
Eli Friedmand38617c2008-05-14 19:38:39 +0000287 }
288
289 llvm::SmallVector<Expr*, 32> exprs;
290
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000291 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) {
Eli Friedmand38617c2008-05-14 19:38:39 +0000292 exprs.push_back(TheCall->getArg(i));
293 TheCall->setArg(i, 0);
294 }
295
Chris Lattnerd1a0b6d2008-08-10 02:05:13 +0000296 return new ShuffleVectorExpr(exprs.begin(), numElements+2, FAType,
297 TheCall->getCallee()->getLocStart(),
298 TheCall->getRParenLoc());
Eli Friedmand38617c2008-05-14 19:38:39 +0000299}
Chris Lattner30ce3442007-12-19 23:59:04 +0000300
Daniel Dunbar4493f792008-07-21 22:59:13 +0000301/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
302// This is declared to take (const void*, ...) and can take two
303// optional constant int args.
304bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000305 unsigned NumArgs = TheCall->getNumArgs();
Daniel Dunbar4493f792008-07-21 22:59:13 +0000306
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000307 if (NumArgs > 3)
308 return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000309 << 0 /*function call*/ << TheCall->getSourceRange();
Daniel Dunbar4493f792008-07-21 22:59:13 +0000310
311 // Argument 0 is checked for us and the remaining arguments must be
312 // constant integers.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000313 for (unsigned i = 1; i != NumArgs; ++i) {
Daniel Dunbar4493f792008-07-21 22:59:13 +0000314 Expr *Arg = TheCall->getArg(i);
315 QualType RWType = Arg->getType();
316
317 const BuiltinType *BT = RWType->getAsBuiltinType();
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000318 llvm::APSInt Result;
Daniel Dunbar4493f792008-07-21 22:59:13 +0000319 if (!BT || BT->getKind() != BuiltinType::Int ||
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000320 !Arg->isIntegerConstantExpr(Result, Context))
321 return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument)
322 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbar4493f792008-07-21 22:59:13 +0000323
324 // FIXME: gcc issues a warning and rewrites these to 0. These
325 // seems especially odd for the third argument since the default
326 // is 3.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000327 if (i == 1) {
Daniel Dunbar4493f792008-07-21 22:59:13 +0000328 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000329 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
330 << "0" << "1" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbar4493f792008-07-21 22:59:13 +0000331 } else {
332 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000333 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
334 << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbar4493f792008-07-21 22:59:13 +0000335 }
336 }
337
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000338 return false;
Daniel Dunbar4493f792008-07-21 22:59:13 +0000339}
340
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000341/// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
342/// int type). This simply type checks that type is one of the defined
343/// constants (0-3).
344bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
345 Expr *Arg = TheCall->getArg(1);
346 QualType ArgType = Arg->getType();
347 const BuiltinType *BT = ArgType->getAsBuiltinType();
348 llvm::APSInt Result(32);
349 if (!BT || BT->getKind() != BuiltinType::Int ||
350 !Arg->isIntegerConstantExpr(Result, Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000351 return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument)
352 << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000353 }
354
355 if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000356 return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range)
357 << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd());
Daniel Dunbard5f8a4f2008-09-03 21:13:56 +0000358 }
359
360 return false;
361}
362
Chris Lattner59907c42007-08-10 20:18:51 +0000363/// CheckPrintfArguments - Check calls to printf (and similar functions) for
Ted Kremenek71895b92007-08-14 17:39:48 +0000364/// correct use of format strings.
365///
366/// HasVAListArg - A predicate indicating whether the printf-like
367/// function is passed an explicit va_arg argument (e.g., vprintf)
368///
369/// format_idx - The index into Args for the format string.
370///
371/// Improper format strings to functions in the printf family can be
372/// the source of bizarre bugs and very serious security holes. A
373/// good source of information is available in the following paper
374/// (which includes additional references):
Chris Lattner59907c42007-08-10 20:18:51 +0000375///
376/// FormatGuard: Automatic Protection From printf Format String
377/// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001.
Ted Kremenek71895b92007-08-14 17:39:48 +0000378///
379/// Functionality implemented:
380///
381/// We can statically check the following properties for string
382/// literal format strings for non v.*printf functions (where the
383/// arguments are passed directly):
384//
385/// (1) Are the number of format conversions equal to the number of
386/// data arguments?
387///
388/// (2) Does each format conversion correctly match the type of the
389/// corresponding data argument? (TODO)
390///
391/// Moreover, for all printf functions we can:
392///
393/// (3) Check for a missing format string (when not caught by type checking).
394///
395/// (4) Check for no-operation flags; e.g. using "#" with format
396/// conversion 'c' (TODO)
397///
398/// (5) Check the use of '%n', a major source of security holes.
399///
400/// (6) Check for malformed format conversions that don't specify anything.
401///
402/// (7) Check for empty format strings. e.g: printf("");
403///
404/// (8) Check that the format string is a wide literal.
405///
Ted Kremenek6d439592008-03-03 16:50:00 +0000406/// (9) Also check the arguments of functions with the __format__ attribute.
407/// (TODO).
408///
Ted Kremenek71895b92007-08-14 17:39:48 +0000409/// All of these checks can be done by parsing the format string.
410///
411/// For now, we ONLY do (1), (3), (5), (6), (7), and (8).
Chris Lattner59907c42007-08-10 20:18:51 +0000412void
Chris Lattner925e60d2007-12-28 05:29:59 +0000413Sema::CheckPrintfArguments(CallExpr *TheCall, bool HasVAListArg,
414 unsigned format_idx) {
415 Expr *Fn = TheCall->getCallee();
416
Ted Kremenek71895b92007-08-14 17:39:48 +0000417 // CHECK: printf-like function is called with no format string.
Chris Lattner925e60d2007-12-28 05:29:59 +0000418 if (format_idx >= TheCall->getNumArgs()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000419 Diag(TheCall->getRParenLoc(), diag::warn_printf_missing_format_string)
420 << Fn->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000421 return;
422 }
423
Chris Lattner56f34942008-02-13 01:02:39 +0000424 Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts();
Chris Lattner459e8482007-08-25 05:36:18 +0000425
Chris Lattner59907c42007-08-10 20:18:51 +0000426 // CHECK: format string is not a string literal.
427 //
Ted Kremenek71895b92007-08-14 17:39:48 +0000428 // Dynamically generated format strings are difficult to
429 // automatically vet at compile time. Requiring that format strings
430 // are string literals: (1) permits the checking of format strings by
431 // the compiler and thereby (2) can practically remove the source of
432 // many format string exploits.
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000433
434 // Format string can be either ObjC string (e.g. @"%d") or
435 // C string (e.g. "%d")
436 // ObjC string uses the same format specifiers as C string, so we can use
437 // the same format string checking logic for both ObjC and C strings.
438 ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(OrigFormatExpr);
439 StringLiteral *FExpr = NULL;
440
441 if(ObjCFExpr != NULL)
442 FExpr = ObjCFExpr->getString();
443 else
444 FExpr = dyn_cast<StringLiteral>(OrigFormatExpr);
445
Ted Kremenek71895b92007-08-14 17:39:48 +0000446 if (FExpr == NULL) {
Ted Kremenek4a336462007-12-17 19:03:13 +0000447 // For vprintf* functions (i.e., HasVAListArg==true), we add a
448 // special check to see if the format string is a function parameter
449 // of the function calling the printf function. If the function
450 // has an attribute indicating it is a printf-like function, then we
451 // should suppress warnings concerning non-literals being used in a call
452 // to a vprintf function. For example:
453 //
454 // void
455 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) {
456 // va_list ap;
457 // va_start(ap, fmt);
458 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt".
459 // ...
460 //
461 //
462 // FIXME: We don't have full attribute support yet, so just check to see
463 // if the argument is a DeclRefExpr that references a parameter. We'll
464 // add proper support for checking the attribute later.
465 if (HasVAListArg)
Chris Lattner998568f2007-12-28 05:38:24 +0000466 if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(OrigFormatExpr))
467 if (isa<ParmVarDecl>(DR->getDecl()))
Ted Kremenek4a336462007-12-17 19:03:13 +0000468 return;
469
Chris Lattner925e60d2007-12-28 05:29:59 +0000470 Diag(TheCall->getArg(format_idx)->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000471 diag::warn_printf_not_string_constant)
472 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000473 return;
474 }
475
476 // CHECK: is the format string a wide literal?
477 if (FExpr->isWide()) {
Chris Lattner925e60d2007-12-28 05:29:59 +0000478 Diag(FExpr->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000479 diag::warn_printf_format_string_is_wide_literal)
480 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000481 return;
482 }
483
484 // Str - The format string. NOTE: this is NOT null-terminated!
485 const char * const Str = FExpr->getStrData();
486
487 // CHECK: empty format string?
488 const unsigned StrLen = FExpr->getByteLength();
489
490 if (StrLen == 0) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000491 Diag(FExpr->getLocStart(), diag::warn_printf_empty_format_string)
492 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000493 return;
494 }
495
496 // We process the format string using a binary state machine. The
497 // current state is stored in CurrentState.
498 enum {
499 state_OrdChr,
500 state_Conversion
501 } CurrentState = state_OrdChr;
502
503 // numConversions - The number of conversions seen so far. This is
504 // incremented as we traverse the format string.
505 unsigned numConversions = 0;
506
507 // numDataArgs - The number of data arguments after the format
508 // string. This can only be determined for non vprintf-like
509 // functions. For those functions, this value is 1 (the sole
510 // va_arg argument).
Chris Lattner925e60d2007-12-28 05:29:59 +0000511 unsigned numDataArgs = TheCall->getNumArgs()-(format_idx+1);
Ted Kremenek71895b92007-08-14 17:39:48 +0000512
513 // Inspect the format string.
514 unsigned StrIdx = 0;
515
516 // LastConversionIdx - Index within the format string where we last saw
517 // a '%' character that starts a new format conversion.
518 unsigned LastConversionIdx = 0;
519
Chris Lattner925e60d2007-12-28 05:29:59 +0000520 for (; StrIdx < StrLen; ++StrIdx) {
Chris Lattner998568f2007-12-28 05:38:24 +0000521
Ted Kremenek71895b92007-08-14 17:39:48 +0000522 // Is the number of detected conversion conversions greater than
523 // the number of matching data arguments? If so, stop.
524 if (!HasVAListArg && numConversions > numDataArgs) break;
525
526 // Handle "\0"
Chris Lattner925e60d2007-12-28 05:29:59 +0000527 if (Str[StrIdx] == '\0') {
Ted Kremenek71895b92007-08-14 17:39:48 +0000528 // The string returned by getStrData() is not null-terminated,
529 // so the presence of a null character is likely an error.
Chris Lattner998568f2007-12-28 05:38:24 +0000530 Diag(PP.AdvanceToTokenCharacter(FExpr->getLocStart(), StrIdx+1),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000531 diag::warn_printf_format_string_contains_null_char)
532 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000533 return;
534 }
535
536 // Ordinary characters (not processing a format conversion).
537 if (CurrentState == state_OrdChr) {
538 if (Str[StrIdx] == '%') {
539 CurrentState = state_Conversion;
540 LastConversionIdx = StrIdx;
541 }
542 continue;
543 }
544
545 // Seen '%'. Now processing a format conversion.
546 switch (Str[StrIdx]) {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000547 // Handle dynamic precision or width specifier.
548 case '*': {
549 ++numConversions;
550
551 if (!HasVAListArg && numConversions > numDataArgs) {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000552 SourceLocation Loc = FExpr->getLocStart();
553 Loc = PP.AdvanceToTokenCharacter(Loc, StrIdx+1);
Ted Kremenek580b6642007-10-12 20:51:52 +0000554
Ted Kremenek580b6642007-10-12 20:51:52 +0000555 if (Str[StrIdx-1] == '.')
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000556 Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg)
557 << OrigFormatExpr->getSourceRange();
Ted Kremenek580b6642007-10-12 20:51:52 +0000558 else
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000559 Diag(Loc, diag::warn_printf_asterisk_width_missing_arg)
560 << OrigFormatExpr->getSourceRange();
Ted Kremenek580b6642007-10-12 20:51:52 +0000561
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000562 // Don't do any more checking. We'll just emit spurious errors.
563 return;
Ted Kremenek580b6642007-10-12 20:51:52 +0000564 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000565
566 // Perform type checking on width/precision specifier.
567 Expr *E = TheCall->getArg(format_idx+numConversions);
568 if (const BuiltinType *BT = E->getType()->getAsBuiltinType())
569 if (BT->getKind() == BuiltinType::Int)
570 break;
Ted Kremenek71895b92007-08-14 17:39:48 +0000571
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000572 SourceLocation Loc =
573 PP.AdvanceToTokenCharacter(FExpr->getLocStart(), StrIdx+1);
574
575 if (Str[StrIdx-1] == '.')
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000576 Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type)
577 << E->getType().getAsString() << E->getSourceRange();
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000578 else
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000579 Diag(Loc, diag::warn_printf_asterisk_width_wrong_type)
580 << E->getType().getAsString() << E->getSourceRange();
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000581
582 break;
583 }
584
585 // Characters which can terminate a format conversion
586 // (e.g. "%d"). Characters that specify length modifiers or
587 // other flags are handled by the default case below.
588 //
589 // FIXME: additional checks will go into the following cases.
590 case 'i':
591 case 'd':
592 case 'o':
593 case 'u':
594 case 'x':
595 case 'X':
596 case 'D':
597 case 'O':
598 case 'U':
599 case 'e':
600 case 'E':
601 case 'f':
602 case 'F':
603 case 'g':
604 case 'G':
605 case 'a':
606 case 'A':
607 case 'c':
608 case 'C':
609 case 'S':
610 case 's':
611 case 'p':
612 ++numConversions;
613 CurrentState = state_OrdChr;
614 break;
615
616 // CHECK: Are we using "%n"? Issue a warning.
617 case 'n': {
618 ++numConversions;
619 CurrentState = state_OrdChr;
620 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
621 LastConversionIdx+1);
622
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000623 Diag(Loc, diag::warn_printf_write_back)<<OrigFormatExpr->getSourceRange();
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000624 break;
625 }
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000626
627 // Handle "%@"
628 case '@':
629 // %@ is allowed in ObjC format strings only.
630 if(ObjCFExpr != NULL)
631 CurrentState = state_OrdChr;
632 else {
633 // Issue a warning: invalid format conversion.
634 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
635 LastConversionIdx+1);
636
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000637 Diag(Loc, diag::warn_printf_invalid_conversion)
638 << std::string(Str+LastConversionIdx,
639 Str+std::min(LastConversionIdx+2, StrLen))
640 << OrigFormatExpr->getSourceRange();
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000641 }
642 ++numConversions;
643 break;
644
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000645 // Handle "%%"
646 case '%':
647 // Sanity check: Was the first "%" character the previous one?
648 // If not, we will assume that we have a malformed format
649 // conversion, and that the current "%" character is the start
650 // of a new conversion.
651 if (StrIdx - LastConversionIdx == 1)
652 CurrentState = state_OrdChr;
653 else {
654 // Issue a warning: invalid format conversion.
Chris Lattner925e60d2007-12-28 05:29:59 +0000655 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
656 LastConversionIdx+1);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000657
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000658 Diag(Loc, diag::warn_printf_invalid_conversion)
659 << std::string(Str+LastConversionIdx, Str+StrIdx)
660 << OrigFormatExpr->getSourceRange();
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000661
662 // This conversion is broken. Advance to the next format
663 // conversion.
664 LastConversionIdx = StrIdx;
665 ++numConversions;
Ted Kremenek71895b92007-08-14 17:39:48 +0000666 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000667 break;
Ted Kremenek71895b92007-08-14 17:39:48 +0000668
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000669 default:
670 // This case catches all other characters: flags, widths, etc.
671 // We should eventually process those as well.
672 break;
Ted Kremenek71895b92007-08-14 17:39:48 +0000673 }
674 }
675
676 if (CurrentState == state_Conversion) {
677 // Issue a warning: invalid format conversion.
Chris Lattner925e60d2007-12-28 05:29:59 +0000678 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
679 LastConversionIdx+1);
Ted Kremenek71895b92007-08-14 17:39:48 +0000680
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000681 Diag(Loc, diag::warn_printf_invalid_conversion)
682 << std::string(Str+LastConversionIdx,
683 Str+std::min(LastConversionIdx+2, StrLen))
684 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000685 return;
686 }
687
688 if (!HasVAListArg) {
689 // CHECK: Does the number of format conversions exceed the number
690 // of data arguments?
691 if (numConversions > numDataArgs) {
Chris Lattner925e60d2007-12-28 05:29:59 +0000692 SourceLocation Loc = PP.AdvanceToTokenCharacter(FExpr->getLocStart(),
693 LastConversionIdx);
Ted Kremenek71895b92007-08-14 17:39:48 +0000694
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000695 Diag(Loc, diag::warn_printf_insufficient_data_args)
696 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000697 }
698 // CHECK: Does the number of data arguments exceed the number of
699 // format conversions in the format string?
700 else if (numConversions < numDataArgs)
Chris Lattner925e60d2007-12-28 05:29:59 +0000701 Diag(TheCall->getArg(format_idx+numConversions+1)->getLocStart(),
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000702 diag::warn_printf_too_many_data_args)
703 << OrigFormatExpr->getSourceRange();
Ted Kremenek71895b92007-08-14 17:39:48 +0000704 }
705}
Ted Kremenek06de2762007-08-17 16:46:58 +0000706
707//===--- CHECK: Return Address of Stack Variable --------------------------===//
708
709static DeclRefExpr* EvalVal(Expr *E);
710static DeclRefExpr* EvalAddr(Expr* E);
711
712/// CheckReturnStackAddr - Check if a return statement returns the address
713/// of a stack variable.
714void
715Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
716 SourceLocation ReturnLoc) {
Chris Lattner56f34942008-02-13 01:02:39 +0000717
Ted Kremenek06de2762007-08-17 16:46:58 +0000718 // Perform checking for returned stack addresses.
Steve Naroffdd972f22008-09-05 22:11:13 +0000719 if (lhsType->isPointerType() || lhsType->isBlockPointerType()) {
Ted Kremenek06de2762007-08-17 16:46:58 +0000720 if (DeclRefExpr *DR = EvalAddr(RetValExp))
Chris Lattner3c73c412008-11-19 08:23:25 +0000721 Diag(DR->getLocStart(), diag::warn_ret_stack_addr)
722 << DR->getDecl()->getIdentifier() << RetValExp->getSourceRange();
Steve Naroffc50a4a52008-09-16 22:25:10 +0000723
724 // Skip over implicit cast expressions when checking for block expressions.
725 if (ImplicitCastExpr *IcExpr =
726 dyn_cast_or_null<ImplicitCastExpr>(RetValExp))
727 RetValExp = IcExpr->getSubExpr();
728
Steve Naroff61f40a22008-09-10 19:17:48 +0000729 if (BlockExpr *C = dyn_cast_or_null<BlockExpr>(RetValExp))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000730 Diag(C->getLocStart(), diag::err_ret_local_block)
731 << C->getSourceRange();
Ted Kremenek06de2762007-08-17 16:46:58 +0000732 }
733 // Perform checking for stack values returned by reference.
734 else if (lhsType->isReferenceType()) {
Douglas Gregor49badde2008-10-27 19:41:14 +0000735 // Check for a reference to the stack
736 if (DeclRefExpr *DR = EvalVal(RetValExp))
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000737 Diag(DR->getLocStart(), diag::warn_ret_stack_ref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000738 << DR->getDecl()->getIdentifier()
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000739 << RetValExp->getSourceRange();
Ted Kremenek06de2762007-08-17 16:46:58 +0000740 }
741}
742
743/// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that
744/// check if the expression in a return statement evaluates to an address
745/// to a location on the stack. The recursion is used to traverse the
746/// AST of the return expression, with recursion backtracking when we
747/// encounter a subexpression that (1) clearly does not lead to the address
748/// of a stack variable or (2) is something we cannot determine leads to
749/// the address of a stack variable based on such local checking.
750///
Ted Kremeneke8c600f2007-08-28 17:02:55 +0000751/// EvalAddr processes expressions that are pointers that are used as
752/// references (and not L-values). EvalVal handles all other values.
Ted Kremenek06de2762007-08-17 16:46:58 +0000753/// At the base case of the recursion is a check for a DeclRefExpr* in
754/// the refers to a stack variable.
755///
756/// This implementation handles:
757///
758/// * pointer-to-pointer casts
759/// * implicit conversions from array references to pointers
760/// * taking the address of fields
761/// * arbitrary interplay between "&" and "*" operators
762/// * pointer arithmetic from an address of a stack variable
763/// * taking the address of an array element where the array is on the stack
764static DeclRefExpr* EvalAddr(Expr *E) {
Ted Kremenek06de2762007-08-17 16:46:58 +0000765 // We should only be called for evaluating pointer expressions.
Steve Naroffdd972f22008-09-05 22:11:13 +0000766 assert((E->getType()->isPointerType() ||
767 E->getType()->isBlockPointerType() ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000768 E->getType()->isObjCQualifiedIdType()) &&
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000769 "EvalAddr only works on pointers");
Ted Kremenek06de2762007-08-17 16:46:58 +0000770
771 // Our "symbolic interpreter" is just a dispatch off the currently
772 // viewed AST node. We then recursively traverse the AST by calling
773 // EvalAddr and EvalVal appropriately.
774 switch (E->getStmtClass()) {
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000775 case Stmt::ParenExprClass:
776 // Ignore parentheses.
777 return EvalAddr(cast<ParenExpr>(E)->getSubExpr());
Ted Kremenek06de2762007-08-17 16:46:58 +0000778
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000779 case Stmt::UnaryOperatorClass: {
780 // The only unary operator that make sense to handle here
781 // is AddrOf. All others don't make sense as pointers.
782 UnaryOperator *U = cast<UnaryOperator>(E);
Ted Kremenek06de2762007-08-17 16:46:58 +0000783
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000784 if (U->getOpcode() == UnaryOperator::AddrOf)
785 return EvalVal(U->getSubExpr());
786 else
Ted Kremenek06de2762007-08-17 16:46:58 +0000787 return NULL;
788 }
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000789
790 case Stmt::BinaryOperatorClass: {
791 // Handle pointer arithmetic. All other binary operators are not valid
792 // in this context.
793 BinaryOperator *B = cast<BinaryOperator>(E);
794 BinaryOperator::Opcode op = B->getOpcode();
795
796 if (op != BinaryOperator::Add && op != BinaryOperator::Sub)
797 return NULL;
798
799 Expr *Base = B->getLHS();
800
801 // Determine which argument is the real pointer base. It could be
802 // the RHS argument instead of the LHS.
803 if (!Base->getType()->isPointerType()) Base = B->getRHS();
804
805 assert (Base->getType()->isPointerType());
806 return EvalAddr(Base);
807 }
Steve Naroff61f40a22008-09-10 19:17:48 +0000808
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000809 // For conditional operators we need to see if either the LHS or RHS are
810 // valid DeclRefExpr*s. If one of them is valid, we return it.
811 case Stmt::ConditionalOperatorClass: {
812 ConditionalOperator *C = cast<ConditionalOperator>(E);
813
814 // Handle the GNU extension for missing LHS.
815 if (Expr *lhsExpr = C->getLHS())
816 if (DeclRefExpr* LHS = EvalAddr(lhsExpr))
817 return LHS;
818
819 return EvalAddr(C->getRHS());
820 }
821
Ted Kremenek54b52742008-08-07 00:49:01 +0000822 // For casts, we need to handle conversions from arrays to
823 // pointer values, and pointer-to-pointer conversions.
Douglas Gregor49badde2008-10-27 19:41:14 +0000824 case Stmt::ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000825 case Stmt::CStyleCastExprClass:
Douglas Gregor49badde2008-10-27 19:41:14 +0000826 case Stmt::CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +0000827 Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
Ted Kremenek54b52742008-08-07 00:49:01 +0000828 QualType T = SubExpr->getType();
829
Steve Naroffdd972f22008-09-05 22:11:13 +0000830 if (SubExpr->getType()->isPointerType() ||
831 SubExpr->getType()->isBlockPointerType() ||
832 SubExpr->getType()->isObjCQualifiedIdType())
Ted Kremenek54b52742008-08-07 00:49:01 +0000833 return EvalAddr(SubExpr);
834 else if (T->isArrayType())
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000835 return EvalVal(SubExpr);
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000836 else
Ted Kremenek54b52742008-08-07 00:49:01 +0000837 return 0;
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000838 }
839
840 // C++ casts. For dynamic casts, static casts, and const casts, we
841 // are always converting from a pointer-to-pointer, so we just blow
Douglas Gregor49badde2008-10-27 19:41:14 +0000842 // through the cast. In the case the dynamic cast doesn't fail (and
843 // return NULL), we take the conservative route and report cases
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000844 // where we return the address of a stack variable. For Reinterpre
Douglas Gregor49badde2008-10-27 19:41:14 +0000845 // FIXME: The comment about is wrong; we're not always converting
846 // from pointer to pointer. I'm guessing that this code should also
847 // handle references to objects.
848 case Stmt::CXXStaticCastExprClass:
849 case Stmt::CXXDynamicCastExprClass:
850 case Stmt::CXXConstCastExprClass:
851 case Stmt::CXXReinterpretCastExprClass: {
852 Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr();
Steve Naroffdd972f22008-09-05 22:11:13 +0000853 if (S->getType()->isPointerType() || S->getType()->isBlockPointerType())
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000854 return EvalAddr(S);
855 else
856 return NULL;
Chris Lattnerfae3f1f2007-12-28 05:31:15 +0000857 }
858
859 // Everything else: we simply don't reason about them.
860 default:
861 return NULL;
862 }
Ted Kremenek06de2762007-08-17 16:46:58 +0000863}
864
865
866/// EvalVal - This function is complements EvalAddr in the mutual recursion.
867/// See the comments for EvalAddr for more details.
868static DeclRefExpr* EvalVal(Expr *E) {
869
Ted Kremeneke8c600f2007-08-28 17:02:55 +0000870 // We should only be called for evaluating non-pointer expressions, or
871 // expressions with a pointer type that are not used as references but instead
872 // are l-values (e.g., DeclRefExpr with a pointer type).
873
Ted Kremenek06de2762007-08-17 16:46:58 +0000874 // Our "symbolic interpreter" is just a dispatch off the currently
875 // viewed AST node. We then recursively traverse the AST by calling
876 // EvalAddr and EvalVal appropriately.
877 switch (E->getStmtClass()) {
Ted Kremenek06de2762007-08-17 16:46:58 +0000878 case Stmt::DeclRefExprClass: {
879 // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking
880 // at code that refers to a variable's name. We check if it has local
881 // storage within the function, and if so, return the expression.
882 DeclRefExpr *DR = cast<DeclRefExpr>(E);
883
884 if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl()))
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000885 if(V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR;
Ted Kremenek06de2762007-08-17 16:46:58 +0000886
887 return NULL;
888 }
889
890 case Stmt::ParenExprClass:
891 // Ignore parentheses.
892 return EvalVal(cast<ParenExpr>(E)->getSubExpr());
893
894 case Stmt::UnaryOperatorClass: {
895 // The only unary operator that make sense to handle here
896 // is Deref. All others don't resolve to a "name." This includes
897 // handling all sorts of rvalues passed to a unary operator.
898 UnaryOperator *U = cast<UnaryOperator>(E);
899
900 if (U->getOpcode() == UnaryOperator::Deref)
901 return EvalAddr(U->getSubExpr());
902
903 return NULL;
904 }
905
906 case Stmt::ArraySubscriptExprClass: {
907 // Array subscripts are potential references to data on the stack. We
908 // retrieve the DeclRefExpr* for the array variable if it indeed
909 // has local storage.
Ted Kremenek23245122007-08-20 16:18:38 +0000910 return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase());
Ted Kremenek06de2762007-08-17 16:46:58 +0000911 }
912
913 case Stmt::ConditionalOperatorClass: {
914 // For conditional operators we need to see if either the LHS or RHS are
915 // non-NULL DeclRefExpr's. If one is non-NULL, we return it.
916 ConditionalOperator *C = cast<ConditionalOperator>(E);
917
Anders Carlsson39073232007-11-30 19:04:31 +0000918 // Handle the GNU extension for missing LHS.
919 if (Expr *lhsExpr = C->getLHS())
920 if (DeclRefExpr *LHS = EvalVal(lhsExpr))
921 return LHS;
922
923 return EvalVal(C->getRHS());
Ted Kremenek06de2762007-08-17 16:46:58 +0000924 }
925
926 // Accesses to members are potential references to data on the stack.
927 case Stmt::MemberExprClass: {
928 MemberExpr *M = cast<MemberExpr>(E);
929
930 // Check for indirect access. We only want direct field accesses.
931 if (!M->isArrow())
932 return EvalVal(M->getBase());
933 else
934 return NULL;
935 }
936
937 // Everything else: we simply don't reason about them.
938 default:
939 return NULL;
940 }
941}
Ted Kremenek588e5eb2007-11-25 00:58:00 +0000942
943//===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===//
944
945/// Check for comparisons of floating point operands using != and ==.
946/// Issue a warning if these are no self-comparisons, as they are not likely
947/// to do what the programmer intended.
948void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) {
949 bool EmitWarning = true;
950
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000951 Expr* LeftExprSansParen = lex->IgnoreParens();
Ted Kremenek32e97b62008-01-17 17:55:13 +0000952 Expr* RightExprSansParen = rex->IgnoreParens();
Ted Kremenek588e5eb2007-11-25 00:58:00 +0000953
954 // Special case: check for x == x (which is OK).
955 // Do not emit warnings for such cases.
956 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen))
957 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen))
958 if (DRL->getDecl() == DRR->getDecl())
959 EmitWarning = false;
960
Ted Kremenek1b500bb2007-11-29 00:59:04 +0000961
962 // Special case: check for comparisons against literals that can be exactly
963 // represented by APFloat. In such cases, do not emit a warning. This
964 // is a heuristic: often comparison against such literals are used to
965 // detect if a value in a variable has not changed. This clearly can
966 // lead to false negatives.
967 if (EmitWarning) {
968 if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) {
969 if (FLL->isExact())
970 EmitWarning = false;
971 }
972 else
973 if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){
974 if (FLR->isExact())
975 EmitWarning = false;
976 }
977 }
978
Ted Kremenek588e5eb2007-11-25 00:58:00 +0000979 // Check for comparisons with builtin types.
980 if (EmitWarning)
981 if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen))
982 if (isCallBuiltin(CL))
983 EmitWarning = false;
984
985 if (EmitWarning)
986 if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen))
987 if (isCallBuiltin(CR))
988 EmitWarning = false;
989
990 // Emit the diagnostic.
991 if (EmitWarning)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000992 Diag(loc, diag::warn_floatingpoint_eq)
993 << lex->getSourceRange() << rex->getSourceRange();
Ted Kremenek588e5eb2007-11-25 00:58:00 +0000994}