Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1 | //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | // This file implements extra semantic analysis beyond what is enforced |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 11 | // by the C type system. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Sema.h" |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/FormatString.h" |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 22 | #include "clang/AST/CharUnits.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | c81614d | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 27 | #include "clang/AST/EvaluatedExprVisitor.h" |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 28 | #include "clang/AST/DeclObjC.h" |
| 29 | #include "clang/AST/StmtCXX.h" |
| 30 | #include "clang/AST/StmtObjC.h" |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/BitVector.h" |
| 33 | #include "llvm/ADT/STLExtras.h" |
Tom Care | b704270 | 2010-06-09 04:11:11 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 35 | #include "clang/Basic/TargetBuiltins.h" |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 36 | #include "clang/Basic/TargetInfo.h" |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 37 | #include "clang/Basic/ConvertUTF.h" |
Zhongxing Xu | 050379b | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 38 | #include <limits> |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 39 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | using namespace sema; |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 41 | |
Chris Lattner | a26fb34 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 42 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 43 | unsigned ByteNo) const { |
Chris Lattner | e925d61 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 44 | return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), |
| 45 | PP.getLangOptions(), PP.getTargetInfo()); |
Chris Lattner | a26fb34 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | e925d61 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 47 | |
Chris Lattner | a26fb34 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 48 | |
Ryan Flynn | aa5e5fd | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 49 | /// CheckablePrintfAttr - does a function call have a "printf" attribute |
| 50 | /// and arguments that merit checking? |
| 51 | bool Sema::CheckablePrintfAttr(const FormatAttr *Format, CallExpr *TheCall) { |
| 52 | if (Format->getType() == "printf") return true; |
| 53 | if (Format->getType() == "printf0") { |
| 54 | // printf0 allows null "format" string; if so don't check format/args |
| 55 | unsigned format_idx = Format->getFormatIdx() - 1; |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 56 | // Does the index refer to the implicit object argument? |
| 57 | if (isa<CXXMemberCallExpr>(TheCall)) { |
| 58 | if (format_idx == 0) |
| 59 | return false; |
| 60 | --format_idx; |
| 61 | } |
Ryan Flynn | aa5e5fd | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 62 | if (format_idx < TheCall->getNumArgs()) { |
| 63 | Expr *Format = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 64 | if (!Format->isNullPointerConstant(Context, |
| 65 | Expr::NPC_ValueDependentIsNull)) |
Ryan Flynn | aa5e5fd | 2009-08-06 03:00:50 +0000 | [diff] [blame] | 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | return false; |
| 70 | } |
Chris Lattner | a26fb34 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 71 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 72 | /// Checks that a call expression's argument count is the desired number. |
| 73 | /// This is useful when doing custom type-checking. Returns true on error. |
| 74 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { |
| 75 | unsigned argCount = call->getNumArgs(); |
| 76 | if (argCount == desiredArgCount) return false; |
| 77 | |
| 78 | if (argCount < desiredArgCount) |
| 79 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 80 | << 0 /*function call*/ << desiredArgCount << argCount |
| 81 | << call->getSourceRange(); |
| 82 | |
| 83 | // Highlight all the excess arguments. |
| 84 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), |
| 85 | call->getArg(argCount - 1)->getLocEnd()); |
| 86 | |
| 87 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) |
| 88 | << 0 /*function call*/ << desiredArgCount << argCount |
| 89 | << call->getArg(1)->getSourceRange(); |
| 90 | } |
| 91 | |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 92 | /// CheckBuiltinAnnotationString - Checks that string argument to the builtin |
| 93 | /// annotation is a non wide string literal. |
| 94 | static bool CheckBuiltinAnnotationString(Sema &S, Expr *Arg) { |
| 95 | Arg = Arg->IgnoreParenCasts(); |
| 96 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 97 | if (!Literal || !Literal->isAscii()) { |
| 98 | S.Diag(Arg->getLocStart(), diag::err_builtin_annotation_not_string_constant) |
| 99 | << Arg->getSourceRange(); |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 105 | ExprResult |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 106 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 107 | ExprResult TheCallResult(Owned(TheCall)); |
Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 3be167f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 109 | // Find out if any arguments are required to be integer constant expressions. |
| 110 | unsigned ICEArguments = 0; |
| 111 | ASTContext::GetBuiltinTypeError Error; |
| 112 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); |
| 113 | if (Error != ASTContext::GE_None) |
| 114 | ICEArguments = 0; // Don't diagnose previously diagnosed errors. |
| 115 | |
| 116 | // If any arguments are required to be ICE's, check and diagnose. |
| 117 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { |
| 118 | // Skip arguments not required to be ICE's. |
| 119 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; |
| 120 | |
| 121 | llvm::APSInt Result; |
| 122 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 123 | return true; |
| 124 | ICEArguments &= ~(1 << ArgNo); |
| 125 | } |
| 126 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 127 | switch (BuiltinID) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 129 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 130 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 131 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 132 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 133 | break; |
Ted Kremenek | a174c52 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 134 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 135 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 136 | if (SemaBuiltinVAStart(TheCall)) |
| 137 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 138 | break; |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 139 | case Builtin::BI__builtin_isgreater: |
| 140 | case Builtin::BI__builtin_isgreaterequal: |
| 141 | case Builtin::BI__builtin_isless: |
| 142 | case Builtin::BI__builtin_islessequal: |
| 143 | case Builtin::BI__builtin_islessgreater: |
| 144 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 145 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 146 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 147 | break; |
Benjamin Kramer | 634fc10 | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 148 | case Builtin::BI__builtin_fpclassify: |
| 149 | if (SemaBuiltinFPClassification(TheCall, 6)) |
| 150 | return ExprError(); |
| 151 | break; |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 152 | case Builtin::BI__builtin_isfinite: |
| 153 | case Builtin::BI__builtin_isinf: |
| 154 | case Builtin::BI__builtin_isinf_sign: |
| 155 | case Builtin::BI__builtin_isnan: |
| 156 | case Builtin::BI__builtin_isnormal: |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 157 | if (SemaBuiltinFPClassification(TheCall, 1)) |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 158 | return ExprError(); |
| 159 | break; |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 160 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 161 | return SemaBuiltinShuffleVector(TheCall); |
| 162 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 163 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 164 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 165 | if (SemaBuiltinPrefetch(TheCall)) |
| 166 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 167 | break; |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 168 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 169 | if (SemaBuiltinObjectSize(TheCall)) |
| 170 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 171 | break; |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 172 | case Builtin::BI__builtin_longjmp: |
| 173 | if (SemaBuiltinLongjmp(TheCall)) |
| 174 | return ExprError(); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 175 | break; |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 176 | |
| 177 | case Builtin::BI__builtin_classify_type: |
| 178 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 179 | TheCall->setType(Context.IntTy); |
| 180 | break; |
Chris Lattner | 17c0eac | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 181 | case Builtin::BI__builtin_constant_p: |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 182 | if (checkArgCount(*this, TheCall, 1)) return true; |
| 183 | TheCall->setType(Context.IntTy); |
Chris Lattner | 17c0eac | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 184 | break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 185 | case Builtin::BI__sync_fetch_and_add: |
| 186 | case Builtin::BI__sync_fetch_and_sub: |
| 187 | case Builtin::BI__sync_fetch_and_or: |
| 188 | case Builtin::BI__sync_fetch_and_and: |
| 189 | case Builtin::BI__sync_fetch_and_xor: |
| 190 | case Builtin::BI__sync_add_and_fetch: |
| 191 | case Builtin::BI__sync_sub_and_fetch: |
| 192 | case Builtin::BI__sync_and_and_fetch: |
| 193 | case Builtin::BI__sync_or_and_fetch: |
| 194 | case Builtin::BI__sync_xor_and_fetch: |
| 195 | case Builtin::BI__sync_val_compare_and_swap: |
| 196 | case Builtin::BI__sync_bool_compare_and_swap: |
| 197 | case Builtin::BI__sync_lock_test_and_set: |
| 198 | case Builtin::BI__sync_lock_release: |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 199 | case Builtin::BI__sync_swap: |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 200 | return SemaBuiltinAtomicOverloaded(move(TheCallResult)); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 201 | case Builtin::BI__atomic_load: |
| 202 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Load); |
| 203 | case Builtin::BI__atomic_store: |
| 204 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Store); |
| 205 | case Builtin::BI__atomic_exchange: |
| 206 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Xchg); |
| 207 | case Builtin::BI__atomic_compare_exchange_strong: |
| 208 | return SemaAtomicOpsOverloaded(move(TheCallResult), |
| 209 | AtomicExpr::CmpXchgStrong); |
| 210 | case Builtin::BI__atomic_compare_exchange_weak: |
| 211 | return SemaAtomicOpsOverloaded(move(TheCallResult), |
| 212 | AtomicExpr::CmpXchgWeak); |
| 213 | case Builtin::BI__atomic_fetch_add: |
| 214 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Add); |
| 215 | case Builtin::BI__atomic_fetch_sub: |
| 216 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Sub); |
| 217 | case Builtin::BI__atomic_fetch_and: |
| 218 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::And); |
| 219 | case Builtin::BI__atomic_fetch_or: |
| 220 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Or); |
| 221 | case Builtin::BI__atomic_fetch_xor: |
| 222 | return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::Xor); |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 223 | case Builtin::BI__builtin_annotation: |
| 224 | if (CheckBuiltinAnnotationString(*this, TheCall->getArg(1))) |
| 225 | return ExprError(); |
| 226 | break; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Since the target specific builtins for each arch overlap, only check those |
| 230 | // of the arch we are compiling for. |
| 231 | if (BuiltinID >= Builtin::FirstTSBuiltin) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 232 | switch (Context.getTargetInfo().getTriple().getArch()) { |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 233 | case llvm::Triple::arm: |
| 234 | case llvm::Triple::thumb: |
| 235 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) |
| 236 | return ExprError(); |
| 237 | break; |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 238 | default: |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return move(TheCallResult); |
| 244 | } |
| 245 | |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 246 | // Get the valid immediate range for the specified NEON type code. |
| 247 | static unsigned RFT(unsigned t, bool shift = false) { |
| 248 | bool quad = t & 0x10; |
| 249 | |
| 250 | switch (t & 0x7) { |
| 251 | case 0: // i8 |
Nate Begeman | dbafec1 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 252 | return shift ? 7 : (8 << (int)quad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 253 | case 1: // i16 |
Nate Begeman | dbafec1 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 254 | return shift ? 15 : (4 << (int)quad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 255 | case 2: // i32 |
Nate Begeman | dbafec1 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 256 | return shift ? 31 : (2 << (int)quad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 257 | case 3: // i64 |
Nate Begeman | dbafec1 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 258 | return shift ? 63 : (1 << (int)quad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 259 | case 4: // f32 |
| 260 | assert(!shift && "cannot shift float types!"); |
Nate Begeman | dbafec1 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 261 | return (2 << (int)quad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 262 | case 5: // poly8 |
Bob Wilson | a880fa0 | 2010-12-10 19:45:06 +0000 | [diff] [blame] | 263 | return shift ? 7 : (8 << (int)quad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 264 | case 6: // poly16 |
Bob Wilson | a880fa0 | 2010-12-10 19:45:06 +0000 | [diff] [blame] | 265 | return shift ? 15 : (4 << (int)quad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 266 | case 7: // float16 |
| 267 | assert(!shift && "cannot shift float types!"); |
Nate Begeman | dbafec1 | 2010-06-17 02:26:59 +0000 | [diff] [blame] | 268 | return (4 << (int)quad) - 1; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 273 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { |
Nate Begeman | 5548309 | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 274 | llvm::APSInt Result; |
| 275 | |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 276 | unsigned mask = 0; |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 277 | unsigned TV = 0; |
Nate Begeman | 5548309 | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 278 | switch (BuiltinID) { |
Nate Begeman | 35f4c1c | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 279 | #define GET_NEON_OVERLOAD_CHECK |
| 280 | #include "clang/Basic/arm_neon.inc" |
| 281 | #undef GET_NEON_OVERLOAD_CHECK |
Nate Begeman | 5548309 | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 284 | // For NEON intrinsics which are overloaded on vector element type, validate |
| 285 | // the immediate which specifies which variant to emit. |
| 286 | if (mask) { |
| 287 | unsigned ArgNo = TheCall->getNumArgs()-1; |
| 288 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) |
| 289 | return true; |
| 290 | |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 291 | TV = Result.getLimitedValue(32); |
| 292 | if ((TV > 31) || (mask & (1 << TV)) == 0) |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 293 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) |
| 294 | << TheCall->getArg(ArgNo)->getSourceRange(); |
| 295 | } |
Nate Begeman | 5548309 | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 296 | |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 297 | // For NEON intrinsics which take an immediate value as part of the |
| 298 | // instruction, range check them here. |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 299 | unsigned i = 0, l = 0, u = 0; |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 300 | switch (BuiltinID) { |
| 301 | default: return false; |
Nate Begeman | 1194bd2 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 302 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; |
| 303 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; |
Nate Begeman | f568b07 | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 304 | case ARM::BI__builtin_arm_vcvtr_f: |
| 305 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; |
Nate Begeman | 35f4c1c | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 306 | #define GET_NEON_IMMEDIATE_CHECK |
| 307 | #include "clang/Basic/arm_neon.inc" |
| 308 | #undef GET_NEON_IMMEDIATE_CHECK |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 309 | }; |
| 310 | |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 311 | // Check that the immediate argument is actually a constant. |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 312 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 313 | return true; |
| 314 | |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 315 | // Range check against the upper/lower values for this isntruction. |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 316 | unsigned Val = Result.getZExtValue(); |
Nate Begeman | 91e1fea | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 317 | if (Val < l || Val > (u + l)) |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 318 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Benjamin Kramer | e8394df | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 319 | << l << u+l << TheCall->getArg(i)->getSourceRange(); |
Nate Begeman | d773fe6 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 320 | |
Nate Begeman | f568b07 | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 321 | // FIXME: VFP Intrinsics should error if VFP not present. |
Nate Begeman | 4904e32 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 322 | return false; |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 323 | } |
Daniel Dunbar | dd9b2d1 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 324 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 325 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 326 | /// and safety properties not strictly enforced by the C type system. |
| 327 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 328 | // Get the IdentifierInfo* for the called function. |
| 329 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
| 330 | |
| 331 | // None of the checks below are needed for functions that don't have |
| 332 | // simple names (e.g., C++ conversion functions). |
| 333 | if (!FnInfo) |
| 334 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Daniel Dunbar | dd9b2d1 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 336 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 337 | // more efficient. For example, just map function ids to custom |
| 338 | // handlers. |
| 339 | |
Ted Kremenek | b8176da | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 340 | // Printf and scanf checking. |
| 341 | for (specific_attr_iterator<FormatAttr> |
| 342 | i = FDecl->specific_attr_begin<FormatAttr>(), |
| 343 | e = FDecl->specific_attr_end<FormatAttr>(); i != e ; ++i) { |
| 344 | |
| 345 | const FormatAttr *Format = *i; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 346 | const bool b = Format->getType() == "scanf"; |
| 347 | if (b || CheckablePrintfAttr(Format, TheCall)) { |
Ted Kremenek | 9723bcf | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 348 | bool HasVAListArg = Format->getFirstArg() == 0; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 349 | CheckPrintfScanfArguments(TheCall, HasVAListArg, |
| 350 | Format->getFormatIdx() - 1, |
| 351 | HasVAListArg ? 0 : Format->getFirstArg() - 1, |
| 352 | !b); |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 353 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 354 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Ted Kremenek | b8176da | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 356 | for (specific_attr_iterator<NonNullAttr> |
| 357 | i = FDecl->specific_attr_begin<NonNullAttr>(), |
| 358 | e = FDecl->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
Nick Lewycky | d469321 | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 359 | CheckNonNullArguments(*i, TheCall->getArgs(), |
| 360 | TheCall->getCallee()->getLocStart()); |
Ted Kremenek | b8176da | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 361 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 362 | |
Peter Collingbourne | 5aa6ecb | 2011-10-16 21:17:32 +0000 | [diff] [blame] | 363 | CheckStaticArrayArguments(FDecl, TheCall->getArgs(), |
| 364 | TheCall->getCallee()->getLocStart()); |
| 365 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 366 | // Builtin handling |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 367 | int CMF = -1; |
| 368 | switch (FDecl->getBuiltinID()) { |
| 369 | case Builtin::BI__builtin_memset: |
| 370 | case Builtin::BI__builtin___memset_chk: |
| 371 | case Builtin::BImemset: |
| 372 | CMF = CMF_Memset; |
| 373 | break; |
| 374 | |
| 375 | case Builtin::BI__builtin_memcpy: |
| 376 | case Builtin::BI__builtin___memcpy_chk: |
| 377 | case Builtin::BImemcpy: |
| 378 | CMF = CMF_Memcpy; |
| 379 | break; |
| 380 | |
| 381 | case Builtin::BI__builtin_memmove: |
| 382 | case Builtin::BI__builtin___memmove_chk: |
| 383 | case Builtin::BImemmove: |
| 384 | CMF = CMF_Memmove; |
| 385 | break; |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 386 | |
| 387 | case Builtin::BIstrlcpy: |
| 388 | case Builtin::BIstrlcat: |
| 389 | CheckStrlcpycatArguments(TheCall, FnInfo); |
| 390 | break; |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 391 | |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 392 | case Builtin::BI__builtin_memcmp: |
| 393 | CMF = CMF_Memcmp; |
| 394 | break; |
| 395 | |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 396 | case Builtin::BI__builtin_strncpy: |
| 397 | case Builtin::BI__builtin___strncpy_chk: |
| 398 | case Builtin::BIstrncpy: |
| 399 | CMF = CMF_Strncpy; |
| 400 | break; |
| 401 | |
| 402 | case Builtin::BI__builtin_strncmp: |
| 403 | CMF = CMF_Strncmp; |
| 404 | break; |
| 405 | |
| 406 | case Builtin::BI__builtin_strncasecmp: |
| 407 | CMF = CMF_Strncasecmp; |
| 408 | break; |
| 409 | |
| 410 | case Builtin::BI__builtin_strncat: |
| 411 | case Builtin::BIstrncat: |
| 412 | CMF = CMF_Strncat; |
| 413 | break; |
| 414 | |
| 415 | case Builtin::BI__builtin_strndup: |
| 416 | case Builtin::BIstrndup: |
| 417 | CMF = CMF_Strndup; |
| 418 | break; |
| 419 | |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 420 | default: |
| 421 | if (FDecl->getLinkage() == ExternalLinkage && |
| 422 | (!getLangOptions().CPlusPlus || FDecl->isExternC())) { |
| 423 | if (FnInfo->isStr("memset")) |
| 424 | CMF = CMF_Memset; |
| 425 | else if (FnInfo->isStr("memcpy")) |
| 426 | CMF = CMF_Memcpy; |
| 427 | else if (FnInfo->isStr("memmove")) |
| 428 | CMF = CMF_Memmove; |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 429 | else if (FnInfo->isStr("memcmp")) |
| 430 | CMF = CMF_Memcmp; |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 431 | else if (FnInfo->isStr("strncpy")) |
| 432 | CMF = CMF_Strncpy; |
| 433 | else if (FnInfo->isStr("strncmp")) |
| 434 | CMF = CMF_Strncmp; |
| 435 | else if (FnInfo->isStr("strncasecmp")) |
| 436 | CMF = CMF_Strncasecmp; |
| 437 | else if (FnInfo->isStr("strncat")) |
| 438 | CMF = CMF_Strncat; |
| 439 | else if (FnInfo->isStr("strndup")) |
| 440 | CMF = CMF_Strndup; |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 441 | } |
| 442 | break; |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 443 | } |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 445 | // Memset/memcpy/memmove handling |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 446 | if (CMF != -1) |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 447 | CheckMemaccessArguments(TheCall, CheckedMemoryFunction(CMF), FnInfo); |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 448 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 449 | return false; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 452 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 453 | // Printf checking. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 454 | const FormatAttr *Format = NDecl->getAttr<FormatAttr>(); |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 455 | if (!Format) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 456 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 458 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); |
| 459 | if (!V) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 460 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 462 | QualType Ty = V->getType(); |
| 463 | if (!Ty->isBlockPointerType()) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 464 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 466 | const bool b = Format->getType() == "scanf"; |
| 467 | if (!b && !CheckablePrintfAttr(Format, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 468 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 470 | bool HasVAListArg = Format->getFirstArg() == 0; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 471 | CheckPrintfScanfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, |
| 472 | HasVAListArg ? 0 : Format->getFirstArg() - 1, !b); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 473 | |
| 474 | return false; |
Fariborz Jahanian | c1585be | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 477 | ExprResult |
| 478 | Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, AtomicExpr::AtomicOp Op) { |
| 479 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); |
| 480 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 481 | |
| 482 | // All these operations take one of the following four forms: |
| 483 | // T __atomic_load(_Atomic(T)*, int) (loads) |
| 484 | // T* __atomic_add(_Atomic(T*)*, ptrdiff_t, int) (pointer add/sub) |
| 485 | // int __atomic_compare_exchange_strong(_Atomic(T)*, T*, T, int, int) |
| 486 | // (cmpxchg) |
| 487 | // T __atomic_exchange(_Atomic(T)*, T, int) (everything else) |
| 488 | // where T is an appropriate type, and the int paremeterss are for orderings. |
| 489 | unsigned NumVals = 1; |
| 490 | unsigned NumOrders = 1; |
| 491 | if (Op == AtomicExpr::Load) { |
| 492 | NumVals = 0; |
| 493 | } else if (Op == AtomicExpr::CmpXchgWeak || Op == AtomicExpr::CmpXchgStrong) { |
| 494 | NumVals = 2; |
| 495 | NumOrders = 2; |
| 496 | } |
| 497 | |
| 498 | if (TheCall->getNumArgs() < NumVals+NumOrders+1) { |
| 499 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 500 | << 0 << NumVals+NumOrders+1 << TheCall->getNumArgs() |
| 501 | << TheCall->getCallee()->getSourceRange(); |
| 502 | return ExprError(); |
| 503 | } else if (TheCall->getNumArgs() > NumVals+NumOrders+1) { |
| 504 | Diag(TheCall->getArg(NumVals+NumOrders+1)->getLocStart(), |
| 505 | diag::err_typecheck_call_too_many_args) |
| 506 | << 0 << NumVals+NumOrders+1 << TheCall->getNumArgs() |
| 507 | << TheCall->getCallee()->getSourceRange(); |
| 508 | return ExprError(); |
| 509 | } |
| 510 | |
| 511 | // Inspect the first argument of the atomic operation. This should always be |
| 512 | // a pointer to an _Atomic type. |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 513 | Expr *Ptr = TheCall->getArg(0); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 514 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); |
| 515 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); |
| 516 | if (!pointerType) { |
| 517 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 518 | << Ptr->getType() << Ptr->getSourceRange(); |
| 519 | return ExprError(); |
| 520 | } |
| 521 | |
| 522 | QualType AtomTy = pointerType->getPointeeType(); |
| 523 | if (!AtomTy->isAtomicType()) { |
| 524 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) |
| 525 | << Ptr->getType() << Ptr->getSourceRange(); |
| 526 | return ExprError(); |
| 527 | } |
| 528 | QualType ValType = AtomTy->getAs<AtomicType>()->getValueType(); |
| 529 | |
| 530 | if ((Op == AtomicExpr::Add || Op == AtomicExpr::Sub) && |
| 531 | !ValType->isIntegerType() && !ValType->isPointerType()) { |
| 532 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) |
| 533 | << Ptr->getType() << Ptr->getSourceRange(); |
| 534 | return ExprError(); |
| 535 | } |
| 536 | |
| 537 | if (!ValType->isIntegerType() && |
| 538 | (Op == AtomicExpr::And || Op == AtomicExpr::Or || Op == AtomicExpr::Xor)){ |
| 539 | Diag(DRE->getLocStart(), diag::err_atomic_op_logical_needs_atomic_int) |
| 540 | << Ptr->getType() << Ptr->getSourceRange(); |
| 541 | return ExprError(); |
| 542 | } |
| 543 | |
| 544 | switch (ValType.getObjCLifetime()) { |
| 545 | case Qualifiers::OCL_None: |
| 546 | case Qualifiers::OCL_ExplicitNone: |
| 547 | // okay |
| 548 | break; |
| 549 | |
| 550 | case Qualifiers::OCL_Weak: |
| 551 | case Qualifiers::OCL_Strong: |
| 552 | case Qualifiers::OCL_Autoreleasing: |
| 553 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
| 554 | << ValType << Ptr->getSourceRange(); |
| 555 | return ExprError(); |
| 556 | } |
| 557 | |
| 558 | QualType ResultType = ValType; |
| 559 | if (Op == AtomicExpr::Store) |
| 560 | ResultType = Context.VoidTy; |
| 561 | else if (Op == AtomicExpr::CmpXchgWeak || Op == AtomicExpr::CmpXchgStrong) |
| 562 | ResultType = Context.BoolTy; |
| 563 | |
| 564 | // The first argument --- the pointer --- has a fixed type; we |
| 565 | // deduce the types of the rest of the arguments accordingly. Walk |
| 566 | // the remaining arguments, converting them to the deduced value type. |
| 567 | for (unsigned i = 1; i != NumVals+NumOrders+1; ++i) { |
| 568 | ExprResult Arg = TheCall->getArg(i); |
| 569 | QualType Ty; |
| 570 | if (i < NumVals+1) { |
| 571 | // The second argument to a cmpxchg is a pointer to the data which will |
| 572 | // be exchanged. The second argument to a pointer add/subtract is the |
| 573 | // amount to add/subtract, which must be a ptrdiff_t. The third |
| 574 | // argument to a cmpxchg and the second argument in all other cases |
| 575 | // is the type of the value. |
| 576 | if (i == 1 && (Op == AtomicExpr::CmpXchgWeak || |
| 577 | Op == AtomicExpr::CmpXchgStrong)) |
| 578 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); |
| 579 | else if (!ValType->isIntegerType() && |
| 580 | (Op == AtomicExpr::Add || Op == AtomicExpr::Sub)) |
| 581 | Ty = Context.getPointerDiffType(); |
| 582 | else |
| 583 | Ty = ValType; |
| 584 | } else { |
| 585 | // The order(s) are always converted to int. |
| 586 | Ty = Context.IntTy; |
| 587 | } |
| 588 | InitializedEntity Entity = |
| 589 | InitializedEntity::InitializeParameter(Context, Ty, false); |
| 590 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 591 | if (Arg.isInvalid()) |
| 592 | return true; |
| 593 | TheCall->setArg(i, Arg.get()); |
| 594 | } |
| 595 | |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 596 | SmallVector<Expr*, 5> SubExprs; |
| 597 | SubExprs.push_back(Ptr); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 598 | if (Op == AtomicExpr::Load) { |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 599 | SubExprs.push_back(TheCall->getArg(1)); // Order |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 600 | } else if (Op != AtomicExpr::CmpXchgWeak && Op != AtomicExpr::CmpXchgStrong) { |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 601 | SubExprs.push_back(TheCall->getArg(2)); // Order |
| 602 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 603 | } else { |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 604 | SubExprs.push_back(TheCall->getArg(3)); // Order |
| 605 | SubExprs.push_back(TheCall->getArg(1)); // Val1 |
| 606 | SubExprs.push_back(TheCall->getArg(2)); // Val2 |
| 607 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 608 | } |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 609 | |
| 610 | return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), |
| 611 | SubExprs.data(), SubExprs.size(), |
| 612 | ResultType, Op, |
| 613 | TheCall->getRParenLoc())); |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 617 | /// checkBuiltinArgument - Given a call to a builtin function, perform |
| 618 | /// normal type-checking on the given argument, updating the call in |
| 619 | /// place. This is useful when a builtin function requires custom |
| 620 | /// type-checking for some of its arguments but not necessarily all of |
| 621 | /// them. |
| 622 | /// |
| 623 | /// Returns true on error. |
| 624 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { |
| 625 | FunctionDecl *Fn = E->getDirectCallee(); |
| 626 | assert(Fn && "builtin call without direct callee!"); |
| 627 | |
| 628 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); |
| 629 | InitializedEntity Entity = |
| 630 | InitializedEntity::InitializeParameter(S.Context, Param); |
| 631 | |
| 632 | ExprResult Arg = E->getArg(0); |
| 633 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); |
| 634 | if (Arg.isInvalid()) |
| 635 | return true; |
| 636 | |
| 637 | E->setArg(ArgIndex, Arg.take()); |
| 638 | return false; |
| 639 | } |
| 640 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 641 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like |
| 642 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer |
| 643 | /// type of its first argument. The main ActOnCallExpr routines have already |
| 644 | /// promoted the types of arguments because all of these calls are prototyped as |
| 645 | /// void(...). |
| 646 | /// |
| 647 | /// This function goes through and does final semantic checking for these |
| 648 | /// builtins, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 649 | ExprResult |
| 650 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 651 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 652 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 653 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 654 | |
| 655 | // Ensure that we have at least one argument to do type inference from. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 656 | if (TheCall->getNumArgs() < 1) { |
| 657 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 658 | << 0 << 1 << TheCall->getNumArgs() |
| 659 | << TheCall->getCallee()->getSourceRange(); |
| 660 | return ExprError(); |
| 661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 663 | // Inspect the first argument of the atomic builtin. This should always be |
| 664 | // a pointer type, whose element is an integral scalar or pointer type. |
| 665 | // Because it is a pointer type, we don't have to worry about any implicit |
| 666 | // casts here. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 667 | // FIXME: We don't allow floating point scalars as input. |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 668 | Expr *FirstArg = TheCall->getArg(0); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 669 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); |
| 670 | if (!pointerType) { |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 671 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) |
| 672 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 673 | return ExprError(); |
| 674 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 676 | QualType ValType = pointerType->getPointeeType(); |
Chris Lattner | bb3bcd8 | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 677 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 678 | !ValType->isBlockPointerType()) { |
| 679 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) |
| 680 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 681 | return ExprError(); |
| 682 | } |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 683 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 684 | switch (ValType.getObjCLifetime()) { |
| 685 | case Qualifiers::OCL_None: |
| 686 | case Qualifiers::OCL_ExplicitNone: |
| 687 | // okay |
| 688 | break; |
| 689 | |
| 690 | case Qualifiers::OCL_Weak: |
| 691 | case Qualifiers::OCL_Strong: |
| 692 | case Qualifiers::OCL_Autoreleasing: |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 693 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 694 | << ValType << FirstArg->getSourceRange(); |
| 695 | return ExprError(); |
| 696 | } |
| 697 | |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 698 | // Strip any qualifiers off ValType. |
| 699 | ValType = ValType.getUnqualifiedType(); |
| 700 | |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 701 | // The majority of builtins return a value, but a few have special return |
| 702 | // types, so allow them to override appropriately below. |
| 703 | QualType ResultType = ValType; |
| 704 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 705 | // We need to figure out which concrete builtin this maps onto. For example, |
| 706 | // __sync_fetch_and_add with a 2 byte object turns into |
| 707 | // __sync_fetch_and_add_2. |
| 708 | #define BUILTIN_ROW(x) \ |
| 709 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ |
| 710 | Builtin::BI##x##_8, Builtin::BI##x##_16 } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 712 | static const unsigned BuiltinIndices[][5] = { |
| 713 | BUILTIN_ROW(__sync_fetch_and_add), |
| 714 | BUILTIN_ROW(__sync_fetch_and_sub), |
| 715 | BUILTIN_ROW(__sync_fetch_and_or), |
| 716 | BUILTIN_ROW(__sync_fetch_and_and), |
| 717 | BUILTIN_ROW(__sync_fetch_and_xor), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 719 | BUILTIN_ROW(__sync_add_and_fetch), |
| 720 | BUILTIN_ROW(__sync_sub_and_fetch), |
| 721 | BUILTIN_ROW(__sync_and_and_fetch), |
| 722 | BUILTIN_ROW(__sync_or_and_fetch), |
| 723 | BUILTIN_ROW(__sync_xor_and_fetch), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 725 | BUILTIN_ROW(__sync_val_compare_and_swap), |
| 726 | BUILTIN_ROW(__sync_bool_compare_and_swap), |
| 727 | BUILTIN_ROW(__sync_lock_test_and_set), |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 728 | BUILTIN_ROW(__sync_lock_release), |
| 729 | BUILTIN_ROW(__sync_swap) |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 730 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | #undef BUILTIN_ROW |
| 732 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 733 | // Determine the index of the size. |
| 734 | unsigned SizeIndex; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 735 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 736 | case 1: SizeIndex = 0; break; |
| 737 | case 2: SizeIndex = 1; break; |
| 738 | case 4: SizeIndex = 2; break; |
| 739 | case 8: SizeIndex = 3; break; |
| 740 | case 16: SizeIndex = 4; break; |
| 741 | default: |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 742 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) |
| 743 | << FirstArg->getType() << FirstArg->getSourceRange(); |
| 744 | return ExprError(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 745 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 747 | // Each of these builtins has one pointer argument, followed by some number of |
| 748 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff |
| 749 | // that we ignore. Find out which row of BuiltinIndices to read from as well |
| 750 | // as the number of fixed args. |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 751 | unsigned BuiltinID = FDecl->getBuiltinID(); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 752 | unsigned BuiltinIndex, NumFixed = 1; |
| 753 | switch (BuiltinID) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 754 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 755 | case Builtin::BI__sync_fetch_and_add: BuiltinIndex = 0; break; |
| 756 | case Builtin::BI__sync_fetch_and_sub: BuiltinIndex = 1; break; |
| 757 | case Builtin::BI__sync_fetch_and_or: BuiltinIndex = 2; break; |
| 758 | case Builtin::BI__sync_fetch_and_and: BuiltinIndex = 3; break; |
| 759 | case Builtin::BI__sync_fetch_and_xor: BuiltinIndex = 4; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
Daniel Dunbar | 3f540c0d | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 761 | case Builtin::BI__sync_add_and_fetch: BuiltinIndex = 5; break; |
| 762 | case Builtin::BI__sync_sub_and_fetch: BuiltinIndex = 6; break; |
| 763 | case Builtin::BI__sync_and_and_fetch: BuiltinIndex = 7; break; |
| 764 | case Builtin::BI__sync_or_and_fetch: BuiltinIndex = 8; break; |
| 765 | case Builtin::BI__sync_xor_and_fetch: BuiltinIndex = 9; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 767 | case Builtin::BI__sync_val_compare_and_swap: |
Daniel Dunbar | 3f540c0d | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 768 | BuiltinIndex = 10; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 769 | NumFixed = 2; |
| 770 | break; |
| 771 | case Builtin::BI__sync_bool_compare_and_swap: |
Daniel Dunbar | 3f540c0d | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 772 | BuiltinIndex = 11; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 773 | NumFixed = 2; |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 774 | ResultType = Context.BoolTy; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 775 | break; |
Daniel Dunbar | 3f540c0d | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 776 | case Builtin::BI__sync_lock_test_and_set: BuiltinIndex = 12; break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 777 | case Builtin::BI__sync_lock_release: |
Daniel Dunbar | 3f540c0d | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 778 | BuiltinIndex = 13; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 779 | NumFixed = 0; |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 780 | ResultType = Context.VoidTy; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 781 | break; |
Chris Lattner | 9cb59fa | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 782 | case Builtin::BI__sync_swap: BuiltinIndex = 14; break; |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 783 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 785 | // Now that we know how many fixed arguments we expect, first check that we |
| 786 | // have at least that many. |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 787 | if (TheCall->getNumArgs() < 1+NumFixed) { |
| 788 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) |
| 789 | << 0 << 1+NumFixed << TheCall->getNumArgs() |
| 790 | << TheCall->getCallee()->getSourceRange(); |
| 791 | return ExprError(); |
| 792 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Chris Lattner | 5b9241b | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 794 | // Get the decl for the concrete builtin from this, we can tell what the |
| 795 | // concrete integer type we should convert to is. |
| 796 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; |
| 797 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); |
| 798 | IdentifierInfo *NewBuiltinII = PP.getIdentifierInfo(NewBuiltinName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | FunctionDecl *NewBuiltinDecl = |
Chris Lattner | 5b9241b | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 800 | cast<FunctionDecl>(LazilyCreateBuiltin(NewBuiltinII, NewBuiltinID, |
| 801 | TUScope, false, DRE->getLocStart())); |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 802 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 803 | // The first argument --- the pointer --- has a fixed type; we |
| 804 | // deduce the types of the rest of the arguments accordingly. Walk |
| 805 | // the remaining arguments, converting them to the deduced value type. |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 806 | for (unsigned i = 0; i != NumFixed; ++i) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 807 | ExprResult Arg = TheCall->getArg(i+1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 809 | // If the argument is an implicit cast, then there was a promotion due to |
| 810 | // "...", just remove it now. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 811 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg.get())) { |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 812 | Arg = ICE->getSubExpr(); |
| 813 | ICE->setSubExpr(0); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 814 | TheCall->setArg(i+1, Arg.get()); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 815 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 817 | // GCC does an implicit conversion to the pointer or integer ValType. This |
| 818 | // can fail in some cases (1i -> int**), check for this error case now. |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 819 | // Initialize the argument. |
| 820 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 821 | ValType, /*consume*/ false); |
| 822 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 823 | if (Arg.isInvalid()) |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 824 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 826 | // Okay, we have something that *can* be converted to the right type. Check |
| 827 | // to see if there is a potentially weird extension going on here. This can |
| 828 | // happen when you do an atomic operation on something like an char* and |
| 829 | // pass in 42. The 42 gets converted to char. This is even more strange |
| 830 | // for things like 45.123 -> char, etc. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | // FIXME: Do this check. |
John McCall | b50451a | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 832 | TheCall->setArg(i+1, Arg.take()); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 833 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 835 | ASTContext& Context = this->getASTContext(); |
| 836 | |
| 837 | // Create a new DeclRefExpr to refer to the new decl. |
| 838 | DeclRefExpr* NewDRE = DeclRefExpr::Create( |
| 839 | Context, |
| 840 | DRE->getQualifierLoc(), |
| 841 | NewBuiltinDecl, |
| 842 | DRE->getLocation(), |
| 843 | NewBuiltinDecl->getType(), |
| 844 | DRE->getValueKind()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 846 | // Set the callee in the CallExpr. |
| 847 | // FIXME: This leaks the original parens and implicit casts. |
Douglas Gregor | 6b3bcf2 | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 848 | ExprResult PromotedCall = UsualUnaryConversions(NewDRE); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 849 | if (PromotedCall.isInvalid()) |
| 850 | return ExprError(); |
| 851 | TheCall->setCallee(PromotedCall.take()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Chandler Carruth | bc8cab1 | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 853 | // Change the result type of the call to match the original value type. This |
| 854 | // is arbitrary, but the codegen for these builtins ins design to handle it |
| 855 | // gracefully. |
Chandler Carruth | 3973af7 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 856 | TheCall->setType(ResultType); |
Chandler Carruth | 741e5ce | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 857 | |
| 858 | return move(TheCallResult); |
Chris Lattner | dc04654 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 861 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 862 | /// CFString constructor is correct |
Steve Naroff | fb46e86 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 863 | /// Note: It might also make sense to do the UTF-16 conversion here (would |
| 864 | /// simplify the backend). |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 865 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | f266096 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 866 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 867 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 868 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 869 | if (!Literal || !Literal->isAscii()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 870 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 871 | << Arg->getSourceRange(); |
Anders Carlsson | a3a9c43 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 872 | return true; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 873 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 874 | |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 875 | if (Literal->containsNonAsciiOrNull()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 876 | StringRef String = Literal->getString(); |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 877 | unsigned NumBytes = String.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 878 | SmallVector<UTF16, 128> ToBuf(NumBytes); |
Fariborz Jahanian | 56603ef | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 879 | const UTF8 *FromPtr = (UTF8 *)String.data(); |
| 880 | UTF16 *ToPtr = &ToBuf[0]; |
| 881 | |
| 882 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, |
| 883 | &ToPtr, ToPtr + NumBytes, |
| 884 | strictConversion); |
| 885 | // Check for conversion failure. |
| 886 | if (Result != conversionOK) |
| 887 | Diag(Arg->getLocStart(), |
| 888 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); |
| 889 | } |
Anders Carlsson | a3a9c43 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 890 | return false; |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 893 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 894 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 895 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 896 | Expr *Fn = TheCall->getCallee(); |
| 897 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | cedef8d | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 898 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 899 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 900 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
| 901 | << Fn->getSourceRange() |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 903 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 904 | return true; |
| 905 | } |
Eli Friedman | bb2b3be | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 906 | |
| 907 | if (TheCall->getNumArgs() < 2) { |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 908 | return Diag(TheCall->getLocEnd(), |
| 909 | diag::err_typecheck_call_too_few_args_at_least) |
| 910 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); |
Eli Friedman | bb2b3be | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 911 | } |
| 912 | |
John McCall | 29ad95b | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 913 | // Type-check the first argument normally. |
| 914 | if (checkBuiltinArgument(*this, TheCall, 0)) |
| 915 | return true; |
| 916 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 917 | // Determine whether the current function is variadic or not. |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 918 | BlockScopeInfo *CurBlock = getCurBlock(); |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 919 | bool isVariadic; |
Steve Naroff | 439a3e4 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 920 | if (CurBlock) |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 921 | isVariadic = CurBlock->TheDecl->isVariadic(); |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 922 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
| 923 | isVariadic = FD->isVariadic(); |
| 924 | else |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 925 | isVariadic = getCurMethodDecl()->isVariadic(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
Chris Lattner | e202e6a | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 927 | if (!isVariadic) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 928 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 929 | return true; |
| 930 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 932 | // Verify that the second argument to the builtin is the last argument of the |
| 933 | // current function or method. |
| 934 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | 73cc507 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 935 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Anders Carlsson | 6a8350b | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 937 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 938 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 939 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 940 | // Get the last formal in the current function. |
Anders Carlsson | 6a8350b | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 941 | const ParmVarDecl *LastArg; |
Steve Naroff | 439a3e4 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 942 | if (CurBlock) |
| 943 | LastArg = *(CurBlock->TheDecl->param_end()-1); |
| 944 | else if (FunctionDecl *FD = getCurFunctionDecl()) |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 945 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 946 | else |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 947 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 948 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 949 | } |
| 950 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 952 | if (!SecondArgIsLastNamedArgument) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 953 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 954 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 955 | return false; |
Eli Friedman | f835303 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 956 | } |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 957 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 958 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 959 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 960 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 961 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | cedef8d | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 962 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 963 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 964 | if (TheCall->getNumArgs() > 2) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 966 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 967 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 968 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 969 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 971 | ExprResult OrigArg0 = TheCall->getArg(0); |
| 972 | ExprResult OrigArg1 = TheCall->getArg(1); |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 974 | // Do standard promotions between the two arguments, returning their common |
| 975 | // type. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 976 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 977 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) |
| 978 | return true; |
Daniel Dunbar | 96f8677 | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 979 | |
| 980 | // Make sure any conversions are pushed back into the call; this is |
| 981 | // type safe since unordered compare builtins are declared as "_Bool |
| 982 | // foo(...)". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 983 | TheCall->setArg(0, OrigArg0.get()); |
| 984 | TheCall->setArg(1, OrigArg1.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 986 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 987 | return false; |
| 988 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 989 | // If the common type isn't a real floating type, then the arguments were |
| 990 | // invalid for this operation. |
| 991 | if (!Res->isRealFloatingType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 992 | return Diag(OrigArg0.get()->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 993 | diag::err_typecheck_call_invalid_ordered_compare) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 994 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() |
| 995 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | |
Chris Lattner | 2da14fb | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 997 | return false; |
| 998 | } |
| 999 | |
Benjamin Kramer | 634fc10 | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 1000 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like |
| 1001 | /// __builtin_isnan and friends. This is declared to take (...), so we have |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1002 | /// to check everything. We expect the last argument to be a floating point |
| 1003 | /// value. |
| 1004 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { |
| 1005 | if (TheCall->getNumArgs() < NumArgs) |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1006 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1007 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1008 | if (TheCall->getNumArgs() > NumArgs) |
| 1009 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1010 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1011 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1012 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1013 | (*(TheCall->arg_end()-1))->getLocEnd()); |
| 1014 | |
Benjamin Kramer | 64aae50 | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1015 | Expr *OrigArg = TheCall->getArg(NumArgs-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1017 | if (OrigArg->isTypeDependent()) |
| 1018 | return false; |
| 1019 | |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1020 | // This operation requires a non-_Complex floating-point number. |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1021 | if (!OrigArg->getType()->isRealFloatingType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | return Diag(OrigArg->getLocStart(), |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1023 | diag::err_typecheck_call_invalid_unary_fp) |
| 1024 | << OrigArg->getType() << OrigArg->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1026 | // If this is an implicit conversion from float -> double, remove it. |
| 1027 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { |
| 1028 | Expr *CastArg = Cast->getSubExpr(); |
| 1029 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { |
| 1030 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && |
| 1031 | "promotion from float to double is the only expected cast here"); |
| 1032 | Cast->setSubExpr(0); |
Chris Lattner | 68784ef | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1033 | TheCall->setArg(NumArgs-1, CastArg); |
| 1034 | OrigArg = CastArg; |
| 1035 | } |
| 1036 | } |
| 1037 | |
Eli Friedman | 7e4faac | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1038 | return false; |
| 1039 | } |
| 1040 | |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1041 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 1042 | // This is declared to take (...), so we have to check everything. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1043 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1044 | if (TheCall->getNumArgs() < 2) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1045 | return ExprError(Diag(TheCall->getLocEnd(), |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1046 | diag::err_typecheck_call_too_few_args_at_least) |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1047 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1048 | << TheCall->getSourceRange()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1049 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1050 | // Determine which of the following types of shufflevector we're checking: |
| 1051 | // 1) unary, vector mask: (lhs, mask) |
| 1052 | // 2) binary, vector mask: (lhs, rhs, mask) |
| 1053 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) |
| 1054 | QualType resType = TheCall->getArg(0)->getType(); |
| 1055 | unsigned numElements = 0; |
| 1056 | |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1057 | if (!TheCall->getArg(0)->isTypeDependent() && |
| 1058 | !TheCall->getArg(1)->isTypeDependent()) { |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1059 | QualType LHSType = TheCall->getArg(0)->getType(); |
| 1060 | QualType RHSType = TheCall->getArg(1)->getType(); |
| 1061 | |
| 1062 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1063 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1065 | TheCall->getArg(1)->getLocEnd()); |
| 1066 | return ExprError(); |
| 1067 | } |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1068 | |
| 1069 | numElements = LHSType->getAs<VectorType>()->getNumElements(); |
| 1070 | unsigned numResElements = TheCall->getNumArgs() - 2; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1072 | // Check to see if we have a call with 2 vector arguments, the unary shuffle |
| 1073 | // with mask. If so, verify that RHS is an integer vector type with the |
| 1074 | // same number of elts as lhs. |
| 1075 | if (TheCall->getNumArgs() == 2) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1076 | if (!RHSType->hasIntegerRepresentation() || |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1077 | RHSType->getAs<VectorType>()->getNumElements() != numElements) |
| 1078 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 1079 | << SourceRange(TheCall->getArg(1)->getLocStart(), |
| 1080 | TheCall->getArg(1)->getLocEnd()); |
| 1081 | numResElements = numElements; |
| 1082 | } |
| 1083 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1084 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1086 | TheCall->getArg(1)->getLocEnd()); |
| 1087 | return ExprError(); |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1088 | } else if (numElements != numResElements) { |
| 1089 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1090 | resType = Context.getVectorType(eltType, numResElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1091 | VectorType::GenericVector); |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1092 | } |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1096 | if (TheCall->getArg(i)->isTypeDependent() || |
| 1097 | TheCall->getArg(i)->isValueDependent()) |
| 1098 | continue; |
| 1099 | |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1100 | llvm::APSInt Result(32); |
| 1101 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
| 1102 | return ExprError(Diag(TheCall->getLocStart(), |
| 1103 | diag::err_shufflevector_nonconstant_argument) |
| 1104 | << TheCall->getArg(i)->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1105 | |
Chris Lattner | 7ab824e | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1106 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1107 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1108 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1109 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1112 | SmallVector<Expr*, 32> exprs; |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | 7ab824e | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1114 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1115 | exprs.push_back(TheCall->getArg(i)); |
| 1116 | TheCall->setArg(i, 0); |
| 1117 | } |
| 1118 | |
Nate Begeman | f485fb5 | 2009-08-12 02:10:25 +0000 | [diff] [blame] | 1119 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs.begin(), |
Nate Begeman | a011002 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1120 | exprs.size(), resType, |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1121 | TheCall->getCallee()->getLocStart(), |
| 1122 | TheCall->getRParenLoc())); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1123 | } |
Chris Lattner | 43be2e6 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1124 | |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1125 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 1126 | // This is declared to take (const void*, ...) and can take two |
| 1127 | // optional constant int args. |
| 1128 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1129 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1130 | |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1131 | if (NumArgs > 3) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1132 | return Diag(TheCall->getLocEnd(), |
| 1133 | diag::err_typecheck_call_too_many_args_at_most) |
| 1134 | << 0 /*function call*/ << 3 << NumArgs |
| 1135 | << TheCall->getSourceRange(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1136 | |
| 1137 | // Argument 0 is checked for us and the remaining arguments must be |
| 1138 | // constant integers. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1139 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1140 | Expr *Arg = TheCall->getArg(i); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1141 | |
Eli Friedman | 5efba26 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1142 | llvm::APSInt Result; |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1143 | if (SemaBuiltinConstantArg(TheCall, i, Result)) |
| 1144 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1146 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 1147 | // seems especially odd for the third argument since the default |
| 1148 | // is 3. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1149 | if (i == 1) { |
Eli Friedman | 5efba26 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1150 | if (Result.getLimitedValue() > 1) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1151 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1152 | << "0" << "1" << Arg->getSourceRange(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1153 | } else { |
Eli Friedman | 5efba26 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1154 | if (Result.getLimitedValue() > 3) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1155 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1156 | << "0" << "3" << Arg->getSourceRange(); |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1160 | return false; |
Daniel Dunbar | b725726 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1163 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr |
| 1164 | /// TheCall is a constant expression. |
| 1165 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, |
| 1166 | llvm::APSInt &Result) { |
| 1167 | Expr *Arg = TheCall->getArg(ArgNum); |
| 1168 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); |
| 1169 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); |
| 1170 | |
| 1171 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; |
| 1172 | |
| 1173 | if (!Arg->isIntegerConstantExpr(Result, Context)) |
| 1174 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) |
Eric Christopher | 63448c3 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 1175 | << FDecl->getDeclName() << Arg->getSourceRange(); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1176 | |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1177 | return false; |
| 1178 | } |
| 1179 | |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1180 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 1181 | /// int type). This simply type checks that type is one of the defined |
| 1182 | /// constants (0-3). |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1183 | // For compatibility check 0-3, llvm only handles 0 and 2. |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1184 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1185 | llvm::APSInt Result; |
| 1186 | |
| 1187 | // Check constant-ness first. |
| 1188 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1189 | return true; |
| 1190 | |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1191 | Expr *Arg = TheCall->getArg(1); |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1192 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1193 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 1194 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | b0d34c8 | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | return false; |
| 1198 | } |
| 1199 | |
Eli Friedman | c97d014 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 1200 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1201 | /// This checks that val is a constant 1. |
| 1202 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { |
| 1203 | Expr *Arg = TheCall->getArg(1); |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1204 | llvm::APSInt Result; |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1205 | |
Eric Christopher | 8d0c621 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1206 | // TODO: This is less than ideal. Overload this to take a value. |
| 1207 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) |
| 1208 | return true; |
| 1209 | |
| 1210 | if (Result != 1) |
Eli Friedman | eed8ad2 | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1211 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) |
| 1212 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
| 1213 | |
| 1214 | return false; |
| 1215 | } |
| 1216 | |
Ted Kremenek | a889083 | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1217 | // Handle i > 1 ? "x" : "y", recursively. |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1218 | bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, |
| 1219 | bool HasVAListArg, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1220 | unsigned format_idx, unsigned firstDataArg, |
| 1221 | bool isPrintf) { |
Ted Kremenek | 80882935 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1222 | tryAgain: |
Douglas Gregor | c25f766 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1223 | if (E->isTypeDependent() || E->isValueDependent()) |
| 1224 | return false; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1225 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1226 | E = E->IgnoreParens(); |
| 1227 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1228 | switch (E->getStmtClass()) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1229 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1230 | case Stmt::ConditionalOperatorClass: { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1231 | const AbstractConditionalOperator *C = cast<AbstractConditionalOperator>(E); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1232 | return SemaCheckStringLiteral(C->getTrueExpr(), TheCall, HasVAListArg, |
| 1233 | format_idx, firstDataArg, isPrintf) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1234 | && SemaCheckStringLiteral(C->getFalseExpr(), TheCall, HasVAListArg, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1235 | format_idx, firstDataArg, isPrintf); |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Ted Kremenek | 1520dae | 2010-09-09 03:51:42 +0000 | [diff] [blame] | 1238 | case Stmt::IntegerLiteralClass: |
| 1239 | // Technically -Wformat-nonliteral does not warn about this case. |
| 1240 | // The behavior of printf and friends in this case is implementation |
| 1241 | // dependent. Ideally if the format string cannot be null then |
| 1242 | // it should have a 'nonnull' attribute in the function prototype. |
| 1243 | return true; |
| 1244 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1245 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 80882935 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1246 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); |
| 1247 | goto tryAgain; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1250 | case Stmt::OpaqueValueExprClass: |
| 1251 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { |
| 1252 | E = src; |
| 1253 | goto tryAgain; |
| 1254 | } |
| 1255 | return false; |
| 1256 | |
Ted Kremenek | a889083 | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1257 | case Stmt::PredefinedExprClass: |
| 1258 | // While __func__, etc., are technically not string literals, they |
| 1259 | // cannot contain format specifiers and thus are not a security |
| 1260 | // liability. |
| 1261 | return true; |
| 1262 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1263 | case Stmt::DeclRefExprClass: { |
| 1264 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1266 | // As an exception, do not flag errors for variables binding to |
| 1267 | // const string literals. |
| 1268 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 1269 | bool isConstant = false; |
| 1270 | QualType T = DR->getType(); |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1271 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1272 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 1273 | isConstant = AT->getElementType().isConstant(Context); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1274 | } else if (const PointerType *PT = T->getAs<PointerType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | isConstant = T.isConstant(Context) && |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1276 | PT->getPointeeType().isConstant(Context); |
| 1277 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1279 | if (isConstant) { |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1280 | if (const Expr *Init = VD->getAnyInitializer()) |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1281 | return SemaCheckStringLiteral(Init, TheCall, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1282 | HasVAListArg, format_idx, firstDataArg, |
| 1283 | isPrintf); |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1284 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | |
Anders Carlsson | b012ca9 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 1286 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 1287 | // special check to see if the format string is a function parameter |
| 1288 | // of the function calling the printf function. If the function |
| 1289 | // has an attribute indicating it is a printf-like function, then we |
| 1290 | // should suppress warnings concerning non-literals being used in a call |
| 1291 | // to a vprintf function. For example: |
| 1292 | // |
| 1293 | // void |
| 1294 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ |
| 1295 | // va_list ap; |
| 1296 | // va_start(ap, fmt); |
| 1297 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 1298 | // ... |
| 1299 | // |
| 1300 | // |
| 1301 | // FIXME: We don't have full attribute support yet, so just check to see |
| 1302 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 1303 | // add proper support for checking the attribute later. |
| 1304 | if (HasVAListArg) |
| 1305 | if (isa<ParmVarDecl>(VD)) |
| 1306 | return true; |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1307 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1309 | return false; |
| 1310 | } |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1311 | |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1312 | case Stmt::CallExprClass: { |
| 1313 | const CallExpr *CE = cast<CallExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | if (const ImplicitCastExpr *ICE |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1315 | = dyn_cast<ImplicitCastExpr>(CE->getCallee())) { |
| 1316 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { |
| 1317 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1318 | if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>()) { |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1319 | unsigned ArgIndex = FA->getFormatIdx(); |
| 1320 | const Expr *Arg = CE->getArg(ArgIndex - 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | |
| 1322 | return SemaCheckStringLiteral(Arg, TheCall, HasVAListArg, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1323 | format_idx, firstDataArg, isPrintf); |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
Anders Carlsson | f0a7f3b | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1329 | return false; |
| 1330 | } |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1331 | case Stmt::ObjCStringLiteralClass: |
| 1332 | case Stmt::StringLiteralClass: { |
| 1333 | const StringLiteral *StrE = NULL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1335 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1336 | StrE = ObjCFExpr->getString(); |
| 1337 | else |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1338 | StrE = cast<StringLiteral>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1339 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1340 | if (StrE) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1341 | CheckFormatString(StrE, E, TheCall, HasVAListArg, format_idx, |
| 1342 | firstDataArg, isPrintf); |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1343 | return true; |
| 1344 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1346 | return false; |
| 1347 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1349 | default: |
| 1350 | return false; |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Fariborz Jahanian | cd1a88d | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1354 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, |
Nick Lewycky | d469321 | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1356 | const Expr * const *ExprArgs, |
| 1357 | SourceLocation CallSiteLoc) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1358 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), |
| 1359 | e = NonNull->args_end(); |
Fariborz Jahanian | cd1a88d | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1360 | i != e; ++i) { |
Nick Lewycky | d469321 | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1361 | const Expr *ArgExpr = ExprArgs[*i]; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1362 | if (ArgExpr->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1363 | Expr::NPC_ValueDependentIsNotNull)) |
Nick Lewycky | d469321 | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1364 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
Fariborz Jahanian | cd1a88d | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1367 | |
Peter Collingbourne | 5aa6ecb | 2011-10-16 21:17:32 +0000 | [diff] [blame] | 1368 | static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { |
| 1369 | TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); |
| 1370 | if (ArrayTypeLoc *ATL = dyn_cast<ArrayTypeLoc>(&TL)) |
| 1371 | S.Diag(PVD->getLocation(), diag::note_callee_static_array) |
| 1372 | << ATL->getLocalSourceRange(); |
| 1373 | } |
| 1374 | |
| 1375 | /// CheckStaticArrayArguments - Check that each argument corresponding to a |
| 1376 | /// static array parameter is non-null, and that if it is formed by |
| 1377 | /// array-to-pointer decay, the underlying array is sufficiently large. |
| 1378 | /// |
| 1379 | /// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the |
| 1380 | /// array type derivation, then for each call to the function, the value of the |
| 1381 | /// corresponding actual argument shall provide access to the first element of |
| 1382 | /// an array with at least as many elements as specified by the size expression. |
| 1383 | void |
| 1384 | Sema::CheckStaticArrayArguments(const FunctionDecl *FDecl, |
| 1385 | const Expr * const *ExprArgs, |
| 1386 | SourceLocation CallSiteLoc) { |
| 1387 | // Static array parameters are not supported in C++. |
| 1388 | if (getLangOptions().CPlusPlus) |
| 1389 | return; |
| 1390 | |
| 1391 | for (FunctionDecl::param_const_iterator i = FDecl->param_begin(), |
| 1392 | e = FDecl->param_end(); i != e; ++i, ++ExprArgs) { |
| 1393 | const Expr *ArgExpr = *ExprArgs; |
| 1394 | QualType OrigTy = (*i)->getOriginalType(); |
| 1395 | |
| 1396 | const ArrayType *AT = Context.getAsArrayType(OrigTy); |
| 1397 | if (!AT || AT->getSizeModifier() != ArrayType::Static) |
| 1398 | continue; |
| 1399 | |
| 1400 | if (ArgExpr->isNullPointerConstant(Context, |
| 1401 | Expr::NPC_NeverValueDependent)) { |
| 1402 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
| 1403 | DiagnoseCalleeStaticArrayParam(*this, *i); |
| 1404 | continue; |
| 1405 | } |
| 1406 | |
| 1407 | const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT); |
| 1408 | if (!CAT) |
| 1409 | continue; |
| 1410 | |
| 1411 | const ConstantArrayType *ArgCAT = |
| 1412 | Context.getAsConstantArrayType(ArgExpr->IgnoreParenImpCasts()->getType()); |
| 1413 | if (!ArgCAT) |
| 1414 | continue; |
| 1415 | |
| 1416 | if (ArgCAT->getSize().ult(CAT->getSize())) { |
| 1417 | Diag(CallSiteLoc, diag::warn_static_array_too_small) |
| 1418 | << ArgExpr->getSourceRange() |
| 1419 | << (unsigned) ArgCAT->getSize().getZExtValue() |
| 1420 | << (unsigned) CAT->getSize().getZExtValue(); |
| 1421 | DiagnoseCalleeStaticArrayParam(*this, *i); |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1426 | /// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar |
| 1427 | /// functions) for correct use of format strings. |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1428 | void |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1429 | Sema::CheckPrintfScanfArguments(const CallExpr *TheCall, bool HasVAListArg, |
| 1430 | unsigned format_idx, unsigned firstDataArg, |
| 1431 | bool isPrintf) { |
| 1432 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1433 | const Expr *Fn = TheCall->getCallee(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1434 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1435 | // The way the format attribute works in GCC, the implicit this argument |
| 1436 | // of member functions is counted. However, it doesn't appear in our own |
| 1437 | // lists, so decrement format_idx in that case. |
| 1438 | if (isa<CXXMemberCallExpr>(TheCall)) { |
Chandler Carruth | 1c8383d | 2010-11-16 08:49:43 +0000 | [diff] [blame] | 1439 | const CXXMethodDecl *method_decl = |
| 1440 | dyn_cast<CXXMethodDecl>(TheCall->getCalleeDecl()); |
| 1441 | if (method_decl && method_decl->isInstance()) { |
| 1442 | // Catch a format attribute mistakenly referring to the object argument. |
| 1443 | if (format_idx == 0) |
| 1444 | return; |
| 1445 | --format_idx; |
| 1446 | if(firstDataArg != 0) |
| 1447 | --firstDataArg; |
| 1448 | } |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1451 | // CHECK: printf/scanf-like function is called with no format string. |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1452 | if (format_idx >= TheCall->getNumArgs()) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1453 | Diag(TheCall->getRParenLoc(), diag::warn_missing_format_string) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1454 | << Fn->getSourceRange(); |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1455 | return; |
| 1456 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Ted Kremenek | dfd72c2 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1458 | const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1460 | // CHECK: format string is not a string literal. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | // |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1462 | // Dynamically generated format strings are difficult to |
| 1463 | // automatically vet at compile time. Requiring that format strings |
| 1464 | // are string literals: (1) permits the checking of format strings by |
| 1465 | // the compiler and thereby (2) can practically remove the source of |
| 1466 | // many format string exploits. |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1467 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | // Format string can be either ObjC string (e.g. @"%d") or |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1469 | // C string (e.g. "%d") |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | // ObjC string uses the same format specifiers as C string, so we can use |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1471 | // the same format string checking logic for both ObjC and C strings. |
Chris Lattner | e009a88 | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1472 | if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1473 | firstDataArg, isPrintf)) |
Chris Lattner | e009a88 | 2009-04-29 04:49:34 +0000 | [diff] [blame] | 1474 | return; // Literal format string found, check done! |
Ted Kremenek | 34f664d | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1475 | |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1476 | // If there are no arguments specified, warn with -Wformat-security, otherwise |
| 1477 | // warn only with -Wformat-nonliteral. |
| 1478 | if (TheCall->getNumArgs() == format_idx+1) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1480 | diag::warn_format_nonliteral_noargs) |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1481 | << OrigFormatExpr->getSourceRange(); |
| 1482 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1484 | diag::warn_format_nonliteral) |
Chris Lattner | cc5d1c2 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1485 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 6dfeb55 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1486 | } |
Ted Kremenek | e68f1aa | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1487 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1488 | namespace { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1489 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { |
| 1490 | protected: |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1491 | Sema &S; |
| 1492 | const StringLiteral *FExpr; |
| 1493 | const Expr *OrigFormatExpr; |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1494 | const unsigned FirstDataArg; |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1495 | const unsigned NumDataArgs; |
| 1496 | const bool IsObjCLiteral; |
| 1497 | const char *Beg; // Start of format string. |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1498 | const bool HasVAListArg; |
| 1499 | const CallExpr *TheCall; |
| 1500 | unsigned FormatIdx; |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1501 | llvm::BitVector CoveredArgs; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1502 | bool usesPositionalArgs; |
| 1503 | bool atFirstArg; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1504 | public: |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1505 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1506 | const Expr *origFormatExpr, unsigned firstDataArg, |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1507 | unsigned numDataArgs, bool isObjCLiteral, |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1508 | const char *beg, bool hasVAListArg, |
| 1509 | const CallExpr *theCall, unsigned formatIdx) |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1510 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), |
Ted Kremenek | 4d745dd | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1511 | FirstDataArg(firstDataArg), |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1512 | NumDataArgs(numDataArgs), |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1513 | IsObjCLiteral(isObjCLiteral), Beg(beg), |
| 1514 | HasVAListArg(hasVAListArg), |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1515 | TheCall(theCall), FormatIdx(formatIdx), |
| 1516 | usesPositionalArgs(false), atFirstArg(true) { |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1517 | CoveredArgs.resize(numDataArgs); |
| 1518 | CoveredArgs.reset(); |
| 1519 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1520 | |
Ted Kremenek | 019d224 | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1521 | void DoneProcessing(); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1522 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1523 | void HandleIncompleteSpecifier(const char *startSpecifier, |
| 1524 | unsigned specifierLen); |
| 1525 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1526 | virtual void HandleInvalidPosition(const char *startSpecifier, |
| 1527 | unsigned specifierLen, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1528 | analyze_format_string::PositionContext p); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1529 | |
| 1530 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); |
| 1531 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1532 | void HandleNullChar(const char *nullCharacter); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1533 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1534 | protected: |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1535 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, |
| 1536 | const char *startSpec, |
| 1537 | unsigned specifierLen, |
| 1538 | const char *csStart, unsigned csLen); |
| 1539 | |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1540 | SourceRange getFormatStringRange(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1541 | CharSourceRange getSpecifierRange(const char *startSpecifier, |
| 1542 | unsigned specifierLen); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1543 | SourceLocation getLocationOfByte(const char *x); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1544 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1545 | const Expr *getDataArg(unsigned i) const; |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1546 | |
| 1547 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, |
| 1548 | const analyze_format_string::ConversionSpecifier &CS, |
| 1549 | const char *startSpecifier, unsigned specifierLen, |
| 1550 | unsigned argIndex); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1551 | }; |
| 1552 | } |
| 1553 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1554 | SourceRange CheckFormatHandler::getFormatStringRange() { |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1555 | return OrigFormatExpr->getSourceRange(); |
| 1556 | } |
| 1557 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1558 | CharSourceRange CheckFormatHandler:: |
| 1559 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1560 | SourceLocation Start = getLocationOfByte(startSpecifier); |
| 1561 | SourceLocation End = getLocationOfByte(startSpecifier + specifierLen - 1); |
| 1562 | |
| 1563 | // Advance the end SourceLocation by one due to half-open ranges. |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1564 | End = End.getLocWithOffset(1); |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1565 | |
| 1566 | return CharSourceRange::getCharRange(Start, End); |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1569 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1570 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1573 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, |
| 1574 | unsigned specifierLen){ |
Ted Kremenek | c22f78d | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 1575 | SourceLocation Loc = getLocationOfByte(startSpecifier); |
| 1576 | S.Diag(Loc, diag::warn_printf_incomplete_specifier) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1577 | << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | c22f78d | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1580 | void |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1581 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, |
| 1582 | analyze_format_string::PositionContext p) { |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1583 | SourceLocation Loc = getLocationOfByte(startPos); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1584 | S.Diag(Loc, diag::warn_format_invalid_positional_specifier) |
| 1585 | << (unsigned) p << getSpecifierRange(startPos, posLen); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1588 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1589 | unsigned posLen) { |
| 1590 | SourceLocation Loc = getLocationOfByte(startPos); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1591 | S.Diag(Loc, diag::warn_format_zero_positional_specifier) |
| 1592 | << getSpecifierRange(startPos, posLen); |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1595 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { |
Ted Kremenek | 0d5b9ef | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 1596 | if (!IsObjCLiteral) { |
| 1597 | // The presence of a null character is likely an error. |
| 1598 | S.Diag(getLocationOfByte(nullCharacter), |
| 1599 | diag::warn_printf_format_string_contains_null_char) |
| 1600 | << getFormatStringRange(); |
| 1601 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1602 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1603 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1604 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { |
| 1605 | return TheCall->getArg(FirstDataArg + i); |
| 1606 | } |
| 1607 | |
| 1608 | void CheckFormatHandler::DoneProcessing() { |
| 1609 | // Does the number of data arguments exceed the number of |
| 1610 | // format conversions in the format string? |
| 1611 | if (!HasVAListArg) { |
| 1612 | // Find any arguments that weren't covered. |
| 1613 | CoveredArgs.flip(); |
| 1614 | signed notCoveredArg = CoveredArgs.find_first(); |
| 1615 | if (notCoveredArg >= 0) { |
| 1616 | assert((unsigned)notCoveredArg < NumDataArgs); |
| 1617 | S.Diag(getDataArg((unsigned) notCoveredArg)->getLocStart(), |
| 1618 | diag::warn_printf_data_arg_not_used) |
| 1619 | << getFormatStringRange(); |
| 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1624 | bool |
| 1625 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, |
| 1626 | SourceLocation Loc, |
| 1627 | const char *startSpec, |
| 1628 | unsigned specifierLen, |
| 1629 | const char *csStart, |
| 1630 | unsigned csLen) { |
| 1631 | |
| 1632 | bool keepGoing = true; |
| 1633 | if (argIndex < NumDataArgs) { |
| 1634 | // Consider the argument coverered, even though the specifier doesn't |
| 1635 | // make sense. |
| 1636 | CoveredArgs.set(argIndex); |
| 1637 | } |
| 1638 | else { |
| 1639 | // If argIndex exceeds the number of data arguments we |
| 1640 | // don't issue a warning because that is just a cascade of warnings (and |
| 1641 | // they may have intended '%%' anyway). We don't want to continue processing |
| 1642 | // the format string after this point, however, as we will like just get |
| 1643 | // gibberish when trying to match arguments. |
| 1644 | keepGoing = false; |
| 1645 | } |
| 1646 | |
| 1647 | S.Diag(Loc, diag::warn_format_invalid_conversion) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1648 | << StringRef(csStart, csLen) |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1649 | << getSpecifierRange(startSpec, specifierLen); |
| 1650 | |
| 1651 | return keepGoing; |
| 1652 | } |
| 1653 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1654 | bool |
| 1655 | CheckFormatHandler::CheckNumArgs( |
| 1656 | const analyze_format_string::FormatSpecifier &FS, |
| 1657 | const analyze_format_string::ConversionSpecifier &CS, |
| 1658 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { |
| 1659 | |
| 1660 | if (argIndex >= NumDataArgs) { |
| 1661 | if (FS.usesPositionalArg()) { |
| 1662 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1663 | diag::warn_printf_positional_arg_exceeds_data_args) |
| 1664 | << (argIndex+1) << NumDataArgs |
| 1665 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1666 | } |
| 1667 | else { |
| 1668 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1669 | diag::warn_printf_insufficient_data_args) |
| 1670 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1671 | } |
| 1672 | |
| 1673 | return false; |
| 1674 | } |
| 1675 | return true; |
| 1676 | } |
| 1677 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1678 | //===--- CHECK: Printf format string checking ------------------------------===// |
| 1679 | |
| 1680 | namespace { |
| 1681 | class CheckPrintfHandler : public CheckFormatHandler { |
| 1682 | public: |
| 1683 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, |
| 1684 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 1685 | unsigned numDataArgs, bool isObjCLiteral, |
| 1686 | const char *beg, bool hasVAListArg, |
| 1687 | const CallExpr *theCall, unsigned formatIdx) |
| 1688 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 1689 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
| 1690 | theCall, formatIdx) {} |
| 1691 | |
| 1692 | |
| 1693 | bool HandleInvalidPrintfConversionSpecifier( |
| 1694 | const analyze_printf::PrintfSpecifier &FS, |
| 1695 | const char *startSpecifier, |
| 1696 | unsigned specifierLen); |
| 1697 | |
| 1698 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, |
| 1699 | const char *startSpecifier, |
| 1700 | unsigned specifierLen); |
| 1701 | |
| 1702 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, |
| 1703 | const char *startSpecifier, unsigned specifierLen); |
| 1704 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, |
| 1705 | const analyze_printf::OptionalAmount &Amt, |
| 1706 | unsigned type, |
| 1707 | const char *startSpecifier, unsigned specifierLen); |
| 1708 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
| 1709 | const analyze_printf::OptionalFlag &flag, |
| 1710 | const char *startSpecifier, unsigned specifierLen); |
| 1711 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, |
| 1712 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 1713 | const analyze_printf::OptionalFlag &flag, |
| 1714 | const char *startSpecifier, unsigned specifierLen); |
| 1715 | }; |
| 1716 | } |
| 1717 | |
| 1718 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( |
| 1719 | const analyze_printf::PrintfSpecifier &FS, |
| 1720 | const char *startSpecifier, |
| 1721 | unsigned specifierLen) { |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1722 | const analyze_printf::PrintfConversionSpecifier &CS = |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1723 | FS.getConversionSpecifier(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1724 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 1725 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 1726 | getLocationOfByte(CS.getStart()), |
| 1727 | startSpecifier, specifierLen, |
| 1728 | CS.getStart(), CS.getLength()); |
Ted Kremenek | 94af575 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1731 | bool CheckPrintfHandler::HandleAmount( |
| 1732 | const analyze_format_string::OptionalAmount &Amt, |
| 1733 | unsigned k, const char *startSpecifier, |
| 1734 | unsigned specifierLen) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1735 | |
| 1736 | if (Amt.hasDataArgument()) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1737 | if (!HasVAListArg) { |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1738 | unsigned argIndex = Amt.getArgIndex(); |
| 1739 | if (argIndex >= NumDataArgs) { |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1740 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1741 | diag::warn_printf_asterisk_missing_arg) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1742 | << k << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1743 | // Don't do any more checking. We will just emit |
| 1744 | // spurious errors. |
| 1745 | return false; |
| 1746 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1747 | |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1748 | // Type check the data argument. It should be an 'int'. |
Ted Kremenek | 605b011 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 1749 | // Although not in conformance with C99, we also allow the argument to be |
| 1750 | // an 'unsigned int' as that is a reasonably safe case. GCC also |
| 1751 | // doesn't emit a warning for that case. |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1752 | CoveredArgs.set(argIndex); |
| 1753 | const Expr *Arg = getDataArg(argIndex); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1754 | QualType T = Arg->getType(); |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1755 | |
| 1756 | const analyze_printf::ArgTypeResult &ATR = Amt.getArgType(S.Context); |
| 1757 | assert(ATR.isValid()); |
| 1758 | |
| 1759 | if (!ATR.matchesType(S.Context, T)) { |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1760 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1761 | diag::warn_printf_asterisk_wrong_type) |
| 1762 | << k |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1763 | << ATR.getRepresentativeType(S.Context) << T |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1764 | << getSpecifierRange(startSpecifier, specifierLen) |
Ted Kremenek | c3bdff7 | 2010-01-30 00:49:51 +0000 | [diff] [blame] | 1765 | << Arg->getSourceRange(); |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1766 | // Don't do any more checking. We will just emit |
| 1767 | // spurious errors. |
| 1768 | return false; |
| 1769 | } |
| 1770 | } |
| 1771 | } |
| 1772 | return true; |
| 1773 | } |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1774 | |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1775 | void CheckPrintfHandler::HandleInvalidAmount( |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1776 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1777 | const analyze_printf::OptionalAmount &Amt, |
| 1778 | unsigned type, |
| 1779 | const char *startSpecifier, |
| 1780 | unsigned specifierLen) { |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1781 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 1782 | FS.getConversionSpecifier(); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1783 | switch (Amt.getHowSpecified()) { |
| 1784 | case analyze_printf::OptionalAmount::Constant: |
| 1785 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1786 | diag::warn_printf_nonsensical_optional_amount) |
| 1787 | << type |
| 1788 | << CS.toString() |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1789 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1790 | << FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1791 | Amt.getConstantLength())); |
| 1792 | break; |
| 1793 | |
| 1794 | default: |
| 1795 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 1796 | diag::warn_printf_nonsensical_optional_amount) |
| 1797 | << type |
| 1798 | << CS.toString() |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1799 | << getSpecifierRange(startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1800 | break; |
| 1801 | } |
| 1802 | } |
| 1803 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1804 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1805 | const analyze_printf::OptionalFlag &flag, |
| 1806 | const char *startSpecifier, |
| 1807 | unsigned specifierLen) { |
| 1808 | // Warn about pointless flag with a fixit removal. |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1809 | const analyze_printf::PrintfConversionSpecifier &CS = |
| 1810 | FS.getConversionSpecifier(); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1811 | S.Diag(getLocationOfByte(flag.getPosition()), |
| 1812 | diag::warn_printf_nonsensical_flag) |
| 1813 | << flag.toString() << CS.toString() |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1814 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1815 | << FixItHint::CreateRemoval(getSpecifierRange(flag.getPosition(), 1)); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | void CheckPrintfHandler::HandleIgnoredFlag( |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1819 | const analyze_printf::PrintfSpecifier &FS, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1820 | const analyze_printf::OptionalFlag &ignoredFlag, |
| 1821 | const analyze_printf::OptionalFlag &flag, |
| 1822 | const char *startSpecifier, |
| 1823 | unsigned specifierLen) { |
| 1824 | // Warn about ignored flag with a fixit removal. |
| 1825 | S.Diag(getLocationOfByte(ignoredFlag.getPosition()), |
| 1826 | diag::warn_printf_ignored_flag) |
| 1827 | << ignoredFlag.toString() << flag.toString() |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1828 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1829 | << FixItHint::CreateRemoval(getSpecifierRange( |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1830 | ignoredFlag.getPosition(), 1)); |
| 1831 | } |
| 1832 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1833 | bool |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1834 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier |
Ted Kremenek | d31b263 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 1835 | &FS, |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1836 | const char *startSpecifier, |
| 1837 | unsigned specifierLen) { |
| 1838 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1839 | using namespace analyze_format_string; |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1840 | using namespace analyze_printf; |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 1841 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1842 | |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 1843 | if (FS.consumesDataArgument()) { |
| 1844 | if (atFirstArg) { |
| 1845 | atFirstArg = false; |
| 1846 | usesPositionalArgs = FS.usesPositionalArg(); |
| 1847 | } |
| 1848 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
| 1849 | // Cannot mix-and-match positional and non-positional arguments. |
| 1850 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1851 | diag::warn_format_mix_positional_nonpositional_args) |
| 1852 | << getSpecifierRange(startSpecifier, specifierLen); |
| 1853 | return false; |
| 1854 | } |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1855 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1856 | |
Ted Kremenek | d166819 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1857 | // First check if the field width, precision, and conversion specifier |
| 1858 | // have matching data arguments. |
| 1859 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, |
| 1860 | startSpecifier, specifierLen)) { |
| 1861 | return false; |
| 1862 | } |
| 1863 | |
| 1864 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, |
| 1865 | startSpecifier, specifierLen)) { |
Ted Kremenek | 5739de7 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1866 | return false; |
| 1867 | } |
| 1868 | |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1869 | if (!CS.consumesDataArgument()) { |
| 1870 | // FIXME: Technically specifying a precision or field width here |
| 1871 | // makes no sense. Worth issuing a warning at some point. |
Ted Kremenek | fb45d35 | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 1872 | return true; |
Ted Kremenek | 8d9842d | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 1873 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1874 | |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1875 | // Consume the argument. |
| 1876 | unsigned argIndex = FS.getArgIndex(); |
Ted Kremenek | 09597b4 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 1877 | if (argIndex < NumDataArgs) { |
| 1878 | // The check to see if the argIndex is valid will come later. |
| 1879 | // We set the bit here because we may exit early from this |
| 1880 | // function if we encounter some other error. |
| 1881 | CoveredArgs.set(argIndex); |
| 1882 | } |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1883 | |
| 1884 | // Check for using an Objective-C specific conversion specifier |
| 1885 | // in a non-ObjC literal. |
| 1886 | if (!IsObjCLiteral && CS.isObjCArg()) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1887 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, |
| 1888 | specifierLen); |
Ted Kremenek | 4a49d98 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1889 | } |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1890 | |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1891 | // Check for invalid use of field width |
| 1892 | if (!FS.hasValidFieldWidth()) { |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1893 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1894 | startSpecifier, specifierLen); |
| 1895 | } |
| 1896 | |
| 1897 | // Check for invalid use of precision |
| 1898 | if (!FS.hasValidPrecision()) { |
| 1899 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, |
| 1900 | startSpecifier, specifierLen); |
| 1901 | } |
| 1902 | |
| 1903 | // Check each flag does not conflict with any other component. |
Ted Kremenek | bf4832c | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 1904 | if (!FS.hasValidThousandsGroupingPrefix()) |
| 1905 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1906 | if (!FS.hasValidLeadingZeros()) |
| 1907 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); |
| 1908 | if (!FS.hasValidPlusPrefix()) |
| 1909 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1910 | if (!FS.hasValidSpacePrefix()) |
| 1911 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1912 | if (!FS.hasValidAlternativeForm()) |
| 1913 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); |
| 1914 | if (!FS.hasValidLeftJustified()) |
| 1915 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); |
| 1916 | |
| 1917 | // Check that flags are not ignored by another flag |
Tom Care | 3f272b8 | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 1918 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' |
| 1919 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), |
| 1920 | startSpecifier, specifierLen); |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1921 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' |
| 1922 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), |
| 1923 | startSpecifier, specifierLen); |
| 1924 | |
| 1925 | // Check the length modifier is valid with the given conversion specifier. |
| 1926 | const LengthModifier &LM = FS.getLengthModifier(); |
| 1927 | if (!FS.hasValidLengthModifier()) |
| 1928 | S.Diag(getLocationOfByte(LM.getStart()), |
Ted Kremenek | b65a9d5 | 2010-07-20 20:03:43 +0000 | [diff] [blame] | 1929 | diag::warn_format_nonsensical_length) |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1930 | << LM.toString() << CS.toString() |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1931 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1932 | << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(), |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1933 | LM.getLength())); |
| 1934 | |
| 1935 | // Are we using '%n'? |
Ted Kremenek | 516ef22 | 2010-07-20 20:04:10 +0000 | [diff] [blame] | 1936 | if (CS.getKind() == ConversionSpecifier::nArg) { |
Tom Care | b49ec69 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 1937 | // Issue a warning about this being a possible security issue. |
Ted Kremenek | d5fd0fa | 2010-01-29 01:35:25 +0000 | [diff] [blame] | 1938 | S.Diag(getLocationOfByte(CS.getStart()), diag::warn_printf_write_back) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1939 | << getSpecifierRange(startSpecifier, specifierLen); |
Ted Kremenek | d5fd0fa | 2010-01-29 01:35:25 +0000 | [diff] [blame] | 1940 | // Continue checking the other format specifiers. |
| 1941 | return true; |
| 1942 | } |
Ted Kremenek | d31b263 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 1943 | |
Ted Kremenek | 9fcd830 | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 1944 | // The remaining checks depend on the data arguments. |
| 1945 | if (HasVAListArg) |
| 1946 | return true; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1947 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 1948 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 9fcd830 | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 1949 | return false; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1950 | |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1951 | // Now type check the data expression that matches the |
| 1952 | // format specifier. |
| 1953 | const Expr *Ex = getDataArg(argIndex); |
| 1954 | const analyze_printf::ArgTypeResult &ATR = FS.getArgType(S.Context); |
| 1955 | if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { |
| 1956 | // Check if we didn't match because of an implicit cast from a 'char' |
| 1957 | // or 'short' to an 'int'. This is done because printf is a varargs |
| 1958 | // function. |
| 1959 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex)) |
Ted Kremenek | 12a37de | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 1960 | if (ICE->getType() == S.Context.IntTy) { |
| 1961 | // All further checking is done on the subexpression. |
| 1962 | Ex = ICE->getSubExpr(); |
| 1963 | if (ATR.matchesType(S.Context, Ex->getType())) |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1964 | return true; |
Ted Kremenek | 12a37de | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 1965 | } |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1966 | |
| 1967 | // We may be able to offer a FixItHint if it is a supported type. |
| 1968 | PrintfSpecifier fixedFS = FS; |
| 1969 | bool success = fixedFS.fixType(Ex->getType()); |
| 1970 | |
| 1971 | if (success) { |
| 1972 | // Get the fix string from the fixed format specifier |
| 1973 | llvm::SmallString<128> buf; |
| 1974 | llvm::raw_svector_ostream os(buf); |
| 1975 | fixedFS.toString(os); |
| 1976 | |
Ted Kremenek | 5f0c066 | 2010-08-24 22:24:51 +0000 | [diff] [blame] | 1977 | // FIXME: getRepresentativeType() perhaps should return a string |
| 1978 | // instead of a QualType to better handle when the representative |
| 1979 | // type is 'wint_t' (which is defined in the system headers). |
Michael J. Spencer | 2c35bc1 | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 1980 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1981 | diag::warn_printf_conversion_argument_type_mismatch) |
| 1982 | << ATR.getRepresentativeType(S.Context) << Ex->getType() |
| 1983 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1984 | << Ex->getSourceRange() |
| 1985 | << FixItHint::CreateReplacement( |
| 1986 | getSpecifierRange(startSpecifier, specifierLen), |
| 1987 | os.str()); |
| 1988 | } |
| 1989 | else { |
| 1990 | S.Diag(getLocationOfByte(CS.getStart()), |
| 1991 | diag::warn_printf_conversion_argument_type_mismatch) |
| 1992 | << ATR.getRepresentativeType(S.Context) << Ex->getType() |
| 1993 | << getSpecifierRange(startSpecifier, specifierLen) |
| 1994 | << Ex->getSourceRange(); |
| 1995 | } |
| 1996 | } |
| 1997 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1998 | return true; |
| 1999 | } |
| 2000 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2001 | //===--- CHECK: Scanf format string checking ------------------------------===// |
| 2002 | |
| 2003 | namespace { |
| 2004 | class CheckScanfHandler : public CheckFormatHandler { |
| 2005 | public: |
| 2006 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, |
| 2007 | const Expr *origFormatExpr, unsigned firstDataArg, |
| 2008 | unsigned numDataArgs, bool isObjCLiteral, |
| 2009 | const char *beg, bool hasVAListArg, |
| 2010 | const CallExpr *theCall, unsigned formatIdx) |
| 2011 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, |
| 2012 | numDataArgs, isObjCLiteral, beg, hasVAListArg, |
| 2013 | theCall, formatIdx) {} |
| 2014 | |
| 2015 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, |
| 2016 | const char *startSpecifier, |
| 2017 | unsigned specifierLen); |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2018 | |
| 2019 | bool HandleInvalidScanfConversionSpecifier( |
| 2020 | const analyze_scanf::ScanfSpecifier &FS, |
| 2021 | const char *startSpecifier, |
| 2022 | unsigned specifierLen); |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2023 | |
| 2024 | void HandleIncompleteScanList(const char *start, const char *end); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2025 | }; |
Ted Kremenek | 019d224 | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2026 | } |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2027 | |
Ted Kremenek | d7b31cc | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2028 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, |
| 2029 | const char *end) { |
| 2030 | S.Diag(getLocationOfByte(end), diag::warn_scanf_scanlist_incomplete) |
| 2031 | << getSpecifierRange(start, end - start); |
| 2032 | } |
| 2033 | |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2034 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( |
| 2035 | const analyze_scanf::ScanfSpecifier &FS, |
| 2036 | const char *startSpecifier, |
| 2037 | unsigned specifierLen) { |
| 2038 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2039 | const analyze_scanf::ScanfConversionSpecifier &CS = |
Ted Kremenek | ce81542 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2040 | FS.getConversionSpecifier(); |
| 2041 | |
| 2042 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), |
| 2043 | getLocationOfByte(CS.getStart()), |
| 2044 | startSpecifier, specifierLen, |
| 2045 | CS.getStart(), CS.getLength()); |
| 2046 | } |
| 2047 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2048 | bool CheckScanfHandler::HandleScanfSpecifier( |
| 2049 | const analyze_scanf::ScanfSpecifier &FS, |
| 2050 | const char *startSpecifier, |
| 2051 | unsigned specifierLen) { |
| 2052 | |
| 2053 | using namespace analyze_scanf; |
| 2054 | using namespace analyze_format_string; |
| 2055 | |
Ted Kremenek | f03e6d85 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2056 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2057 | |
Ted Kremenek | 6cd6942 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2058 | // Handle case where '%' and '*' don't consume an argument. These shouldn't |
| 2059 | // be used to decide if we are using positional arguments consistently. |
| 2060 | if (FS.consumesDataArgument()) { |
| 2061 | if (atFirstArg) { |
| 2062 | atFirstArg = false; |
| 2063 | usesPositionalArgs = FS.usesPositionalArg(); |
| 2064 | } |
| 2065 | else if (usesPositionalArgs != FS.usesPositionalArg()) { |
| 2066 | // Cannot mix-and-match positional and non-positional arguments. |
| 2067 | S.Diag(getLocationOfByte(CS.getStart()), |
| 2068 | diag::warn_format_mix_positional_nonpositional_args) |
| 2069 | << getSpecifierRange(startSpecifier, specifierLen); |
| 2070 | return false; |
| 2071 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | // Check if the field with is non-zero. |
| 2075 | const OptionalAmount &Amt = FS.getFieldWidth(); |
| 2076 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { |
| 2077 | if (Amt.getConstantAmount() == 0) { |
| 2078 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), |
| 2079 | Amt.getConstantLength()); |
| 2080 | S.Diag(getLocationOfByte(Amt.getStart()), |
| 2081 | diag::warn_scanf_nonzero_width) |
| 2082 | << R << FixItHint::CreateRemoval(R); |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | if (!FS.consumesDataArgument()) { |
| 2087 | // FIXME: Technically specifying a precision or field width here |
| 2088 | // makes no sense. Worth issuing a warning at some point. |
| 2089 | return true; |
| 2090 | } |
| 2091 | |
| 2092 | // Consume the argument. |
| 2093 | unsigned argIndex = FS.getArgIndex(); |
| 2094 | if (argIndex < NumDataArgs) { |
| 2095 | // The check to see if the argIndex is valid will come later. |
| 2096 | // We set the bit here because we may exit early from this |
| 2097 | // function if we encounter some other error. |
| 2098 | CoveredArgs.set(argIndex); |
| 2099 | } |
| 2100 | |
Ted Kremenek | 4407ea4 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2101 | // Check the length modifier is valid with the given conversion specifier. |
| 2102 | const LengthModifier &LM = FS.getLengthModifier(); |
| 2103 | if (!FS.hasValidLengthModifier()) { |
| 2104 | S.Diag(getLocationOfByte(LM.getStart()), |
| 2105 | diag::warn_format_nonsensical_length) |
| 2106 | << LM.toString() << CS.toString() |
| 2107 | << getSpecifierRange(startSpecifier, specifierLen) |
| 2108 | << FixItHint::CreateRemoval(getSpecifierRange(LM.getStart(), |
| 2109 | LM.getLength())); |
| 2110 | } |
| 2111 | |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2112 | // The remaining checks depend on the data arguments. |
| 2113 | if (HasVAListArg) |
| 2114 | return true; |
| 2115 | |
Ted Kremenek | 6adb7e3 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2116 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2117 | return false; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2118 | |
| 2119 | // FIXME: Check that the argument type matches the format specifier. |
| 2120 | |
| 2121 | return true; |
| 2122 | } |
| 2123 | |
| 2124 | void Sema::CheckFormatString(const StringLiteral *FExpr, |
Ted Kremenek | fb45d35 | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2125 | const Expr *OrigFormatExpr, |
| 2126 | const CallExpr *TheCall, bool HasVAListArg, |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2127 | unsigned format_idx, unsigned firstDataArg, |
| 2128 | bool isPrintf) { |
| 2129 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2130 | // CHECK: is the format string a wide literal? |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2131 | if (!FExpr->isAscii()) { |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2132 | Diag(FExpr->getLocStart(), |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2133 | diag::warn_format_string_is_wide_literal) |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2134 | << OrigFormatExpr->getSourceRange(); |
| 2135 | return; |
| 2136 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2137 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2138 | // Str - The format string. NOTE: this is NOT null-terminated! |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2139 | StringRef StrRef = FExpr->getString(); |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 2140 | const char *Str = StrRef.data(); |
| 2141 | unsigned StrLen = StrRef.size(); |
Ted Kremenek | 6e302b2 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2142 | const unsigned numDataArgs = TheCall->getNumArgs() - firstDataArg; |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2143 | |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2144 | // CHECK: empty format string? |
Ted Kremenek | 6e302b2 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2145 | if (StrLen == 0 && numDataArgs > 0) { |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2146 | Diag(FExpr->getLocStart(), diag::warn_empty_format_string) |
Ted Kremenek | ab278de | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2147 | << OrigFormatExpr->getSourceRange(); |
| 2148 | return; |
| 2149 | } |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2150 | |
| 2151 | if (isPrintf) { |
| 2152 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Ted Kremenek | 6e302b2 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2153 | numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr), |
| 2154 | Str, HasVAListArg, TheCall, format_idx); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2155 | |
| 2156 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen)) |
| 2157 | H.DoneProcessing(); |
| 2158 | } |
| 2159 | else { |
| 2160 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, |
Ted Kremenek | 6e302b2 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 2161 | numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr), |
| 2162 | Str, HasVAListArg, TheCall, format_idx); |
Ted Kremenek | 0208793 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2163 | |
| 2164 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen)) |
| 2165 | H.DoneProcessing(); |
| 2166 | } |
Ted Kremenek | c70ee86 | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2169 | //===--- CHECK: Standard memory functions ---------------------------------===// |
| 2170 | |
Douglas Gregor | a74926b | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 2171 | /// \brief Determine whether the given type is a dynamic class type (e.g., |
| 2172 | /// whether it has a vtable). |
| 2173 | static bool isDynamicClassType(QualType T) { |
| 2174 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) |
| 2175 | if (CXXRecordDecl *Definition = Record->getDefinition()) |
| 2176 | if (Definition->isDynamicClass()) |
| 2177 | return true; |
| 2178 | |
| 2179 | return false; |
| 2180 | } |
| 2181 | |
Chandler Carruth | 889ed86 | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2182 | /// \brief If E is a sizeof expression, returns its argument expression, |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2183 | /// otherwise returns NULL. |
| 2184 | static const Expr *getSizeOfExprArg(const Expr* E) { |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2185 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2186 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2187 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) |
| 2188 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2189 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2190 | return 0; |
| 2191 | } |
| 2192 | |
Chandler Carruth | 889ed86 | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 2193 | /// \brief If E is a sizeof expression, returns its argument type. |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2194 | static QualType getSizeOfArgType(const Expr* E) { |
| 2195 | if (const UnaryExprOrTypeTraitExpr *SizeOf = |
| 2196 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) |
| 2197 | if (SizeOf->getKind() == clang::UETT_SizeOf) |
| 2198 | return SizeOf->getTypeOfArgument(); |
| 2199 | |
| 2200 | return QualType(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2203 | /// \brief Check for dangerous or invalid arguments to memset(). |
| 2204 | /// |
Chandler Carruth | ac68726 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 2205 | /// This issues warnings on known problematic, dangerous or unspecified |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2206 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' |
| 2207 | /// function calls. |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2208 | /// |
| 2209 | /// \param Call The call expression to diagnose. |
Matt Beaumont-Gay | 3c48990 | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 2210 | void Sema::CheckMemaccessArguments(const CallExpr *Call, |
| 2211 | CheckedMemoryFunction CMF, |
| 2212 | IdentifierInfo *FnName) { |
Ted Kremenek | b5fabb2 | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2213 | // It is possible to have a non-standard definition of memset. Validate |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 2214 | // we have enough arguments, and if not, abort further checking. |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2215 | unsigned ExpectedNumArgs = (CMF == CMF_Strndup ? 2 : 3); |
| 2216 | if (Call->getNumArgs() < ExpectedNumArgs) |
Ted Kremenek | b5fabb2 | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 2217 | return; |
| 2218 | |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2219 | unsigned LastArg = (CMF == CMF_Memset || CMF == CMF_Strndup ? 1 : 2); |
| 2220 | unsigned LenArg = (CMF == CMF_Strndup ? 1 : 2); |
| 2221 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2222 | |
| 2223 | // We have special checking when the length is a sizeof expression. |
| 2224 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); |
| 2225 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); |
| 2226 | llvm::FoldingSetNodeID SizeOfArgID; |
| 2227 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2228 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { |
| 2229 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2230 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2231 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2232 | QualType DestTy = Dest->getType(); |
| 2233 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { |
| 2234 | QualType PointeeTy = DestPtrTy->getPointeeType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2235 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2236 | // Never warn about void type pointers. This can be used to suppress |
| 2237 | // false positives. |
| 2238 | if (PointeeTy->isVoidType()) |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2239 | continue; |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2240 | |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2241 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by |
| 2242 | // actually comparing the expressions for equality. Because computing the |
| 2243 | // expression IDs can be expensive, we only do this if the diagnostic is |
| 2244 | // enabled. |
| 2245 | if (SizeOfArg && |
| 2246 | Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess, |
| 2247 | SizeOfArg->getExprLoc())) { |
| 2248 | // We only compute IDs for expressions if the warning is enabled, and |
| 2249 | // cache the sizeof arg's ID. |
| 2250 | if (SizeOfArgID == llvm::FoldingSetNodeID()) |
| 2251 | SizeOfArg->Profile(SizeOfArgID, Context, true); |
| 2252 | llvm::FoldingSetNodeID DestID; |
| 2253 | Dest->Profile(DestID, Context, true); |
| 2254 | if (DestID == SizeOfArgID) { |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2255 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) |
| 2256 | // over sizeof(src) as well. |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2257 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. |
| 2258 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) |
| 2259 | if (UnaryOp->getOpcode() == UO_AddrOf) |
| 2260 | ActionIdx = 1; // If its an address-of operator, just remove it. |
| 2261 | if (Context.getTypeSize(PointeeTy) == Context.getCharWidth()) |
| 2262 | ActionIdx = 2; // If the pointee's size is sizeof(char), |
| 2263 | // suggest an explicit length. |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2264 | unsigned DestSrcSelect = (CMF == CMF_Strndup ? 1 : ArgIdx); |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2265 | DiagRuntimeBehavior(SizeOfArg->getExprLoc(), Dest, |
| 2266 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) |
Nico Weber | 39bfed8 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 2267 | << FnName << DestSrcSelect << ActionIdx |
Chandler Carruth | 8b9e5a7 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 2268 | << Dest->getSourceRange() |
| 2269 | << SizeOfArg->getSourceRange()); |
| 2270 | break; |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | // Also check for cases where the sizeof argument is the exact same |
| 2275 | // type as the memory argument, and where it points to a user-defined |
| 2276 | // record type. |
| 2277 | if (SizeOfArgTy != QualType()) { |
| 2278 | if (PointeeTy->isRecordType() && |
| 2279 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { |
| 2280 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, |
| 2281 | PDiag(diag::warn_sizeof_pointer_type_memaccess) |
| 2282 | << FnName << SizeOfArgTy << ArgIdx |
| 2283 | << PointeeTy << Dest->getSourceRange() |
| 2284 | << LenExpr->getSourceRange()); |
| 2285 | break; |
| 2286 | } |
Nico Weber | c5e7386 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2289 | // Always complain about dynamic classes. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2290 | if (isDynamicClassType(PointeeTy)) |
Matt Beaumont-Gay | 335e653 | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 2291 | DiagRuntimeBehavior( |
| 2292 | Dest->getExprLoc(), Dest, |
| 2293 | PDiag(diag::warn_dyn_class_memaccess) |
| 2294 | << (CMF == CMF_Memcmp ? ArgIdx + 2 : ArgIdx) << FnName << PointeeTy |
| 2295 | // "overwritten" if we're warning about the destination for any call |
| 2296 | // but memcmp; otherwise a verb appropriate to the call. |
| 2297 | << (ArgIdx == 0 && CMF != CMF_Memcmp ? 0 : (unsigned)CMF) |
| 2298 | << Call->getCallee()->getSourceRange()); |
Douglas Gregor | 18739c3 | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 2299 | else if (PointeeTy.hasNonTrivialObjCLifetime() && CMF != CMF_Memset) |
Matt Beaumont-Gay | 335e653 | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 2300 | DiagRuntimeBehavior( |
| 2301 | Dest->getExprLoc(), Dest, |
| 2302 | PDiag(diag::warn_arc_object_memaccess) |
| 2303 | << ArgIdx << FnName << PointeeTy |
| 2304 | << Call->getCallee()->getSourceRange()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2305 | else |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2306 | continue; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2307 | |
| 2308 | DiagRuntimeBehavior( |
| 2309 | Dest->getExprLoc(), Dest, |
Chandler Carruth | ac68726 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 2310 | PDiag(diag::note_bad_memaccess_silence) |
Douglas Gregor | 3bb2a81 | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 2311 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); |
| 2312 | break; |
| 2313 | } |
Chandler Carruth | 53caa4d | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2317 | // A little helper routine: ignore addition and subtraction of integer literals. |
| 2318 | // This intentionally does not ignore all integer constant expressions because |
| 2319 | // we don't want to remove sizeof(). |
| 2320 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { |
| 2321 | Ex = Ex->IgnoreParenCasts(); |
| 2322 | |
| 2323 | for (;;) { |
| 2324 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); |
| 2325 | if (!BO || !BO->isAdditiveOp()) |
| 2326 | break; |
| 2327 | |
| 2328 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); |
| 2329 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); |
| 2330 | |
| 2331 | if (isa<IntegerLiteral>(RHS)) |
| 2332 | Ex = LHS; |
| 2333 | else if (isa<IntegerLiteral>(LHS)) |
| 2334 | Ex = RHS; |
| 2335 | else |
| 2336 | break; |
| 2337 | } |
| 2338 | |
| 2339 | return Ex; |
| 2340 | } |
| 2341 | |
| 2342 | // Warn if the user has made the 'size' argument to strlcpy or strlcat |
| 2343 | // be the size of the source, instead of the destination. |
| 2344 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, |
| 2345 | IdentifierInfo *FnName) { |
| 2346 | |
| 2347 | // Don't crash if the user has the wrong number of arguments |
| 2348 | if (Call->getNumArgs() != 3) |
| 2349 | return; |
| 2350 | |
| 2351 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); |
| 2352 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); |
| 2353 | const Expr *CompareWithSrc = NULL; |
| 2354 | |
| 2355 | // Look for 'strlcpy(dst, x, sizeof(x))' |
| 2356 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) |
| 2357 | CompareWithSrc = Ex; |
| 2358 | else { |
| 2359 | // Look for 'strlcpy(dst, x, strlen(x))' |
| 2360 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { |
| 2361 | if (SizeCall->isBuiltinCall(Context) == Builtin::BIstrlen |
| 2362 | && SizeCall->getNumArgs() == 1) |
| 2363 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | if (!CompareWithSrc) |
| 2368 | return; |
| 2369 | |
| 2370 | // Determine if the argument to sizeof/strlen is equal to the source |
| 2371 | // argument. In principle there's all kinds of things you could do |
| 2372 | // here, for instance creating an == expression and evaluating it with |
| 2373 | // EvaluateAsBooleanCondition, but this uses a more direct technique: |
| 2374 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); |
| 2375 | if (!SrcArgDRE) |
| 2376 | return; |
| 2377 | |
| 2378 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); |
| 2379 | if (!CompareWithSrcDRE || |
| 2380 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) |
| 2381 | return; |
| 2382 | |
| 2383 | const Expr *OriginalSizeArg = Call->getArg(2); |
| 2384 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) |
| 2385 | << OriginalSizeArg->getSourceRange() << FnName; |
| 2386 | |
| 2387 | // Output a FIXIT hint if the destination is an array (rather than a |
| 2388 | // pointer to an array). This could be enhanced to handle some |
| 2389 | // pointers if we know the actual size, like if DstArg is 'array+2' |
| 2390 | // we could say 'sizeof(array)-2'. |
| 2391 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2392 | QualType DstArgTy = DstArg->getType(); |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2393 | |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2394 | // Only handle constant-sized or VLAs, but not flexible members. |
| 2395 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(DstArgTy)) { |
| 2396 | // Only issue the FIXIT for arrays of size > 1. |
| 2397 | if (CAT->getSize().getSExtValue() <= 1) |
| 2398 | return; |
| 2399 | } else if (!DstArgTy->isVariableArrayType()) { |
| 2400 | return; |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2401 | } |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2402 | |
| 2403 | llvm::SmallString<128> sizeString; |
| 2404 | llvm::raw_svector_ostream OS(sizeString); |
| 2405 | OS << "sizeof("; |
Douglas Gregor | 75acd92 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 2406 | DstArg->printPretty(OS, Context, 0, getPrintingPolicy()); |
Ted Kremenek | 18db5d4 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 2407 | OS << ")"; |
| 2408 | |
| 2409 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) |
| 2410 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), |
| 2411 | OS.str()); |
Ted Kremenek | 6865f77 | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2414 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 2415 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2416 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars); |
| 2417 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2418 | |
| 2419 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 2420 | /// of a stack variable. |
| 2421 | void |
| 2422 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 2423 | SourceLocation ReturnLoc) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2425 | Expr *stackE = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2426 | SmallVector<DeclRefExpr *, 8> refVars; |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2427 | |
| 2428 | // Perform checking for returned stack addresses, local blocks, |
| 2429 | // label addresses or references to temporaries. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2430 | if (lhsType->isPointerType() || |
| 2431 | (!getLangOptions().ObjCAutoRefCount && lhsType->isBlockPointerType())) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2432 | stackE = EvalAddr(RetValExp, refVars); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2433 | } else if (lhsType->isReferenceType()) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2434 | stackE = EvalVal(RetValExp, refVars); |
| 2435 | } |
| 2436 | |
| 2437 | if (stackE == 0) |
| 2438 | return; // Nothing suspicious was found. |
| 2439 | |
| 2440 | SourceLocation diagLoc; |
| 2441 | SourceRange diagRange; |
| 2442 | if (refVars.empty()) { |
| 2443 | diagLoc = stackE->getLocStart(); |
| 2444 | diagRange = stackE->getSourceRange(); |
| 2445 | } else { |
| 2446 | // We followed through a reference variable. 'stackE' contains the |
| 2447 | // problematic expression but we will warn at the return statement pointing |
| 2448 | // at the reference variable. We will later display the "trail" of |
| 2449 | // reference variables using notes. |
| 2450 | diagLoc = refVars[0]->getLocStart(); |
| 2451 | diagRange = refVars[0]->getSourceRange(); |
| 2452 | } |
| 2453 | |
| 2454 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. |
| 2455 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref |
| 2456 | : diag::warn_ret_stack_addr) |
| 2457 | << DR->getDecl()->getDeclName() << diagRange; |
| 2458 | } else if (isa<BlockExpr>(stackE)) { // local block. |
| 2459 | Diag(diagLoc, diag::err_ret_local_block) << diagRange; |
| 2460 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. |
| 2461 | Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; |
| 2462 | } else { // local temporary. |
| 2463 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref |
| 2464 | : diag::warn_ret_local_temp_addr) |
| 2465 | << diagRange; |
| 2466 | } |
| 2467 | |
| 2468 | // Display the "trail" of reference variables that we followed until we |
| 2469 | // found the problematic expression using notes. |
| 2470 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { |
| 2471 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); |
| 2472 | // If this var binds to another reference var, show the range of the next |
| 2473 | // var, otherwise the var binds to the problematic expression, in which case |
| 2474 | // show the range of the expression. |
| 2475 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() |
| 2476 | : stackE->getSourceRange(); |
| 2477 | Diag(VD->getLocation(), diag::note_ref_var_local_bind) |
| 2478 | << VD->getDeclName() << range; |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 2483 | /// check if the expression in a return statement evaluates to an address |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2484 | /// to a location on the stack, a local block, an address of a label, or a |
| 2485 | /// reference to local temporary. The recursion is used to traverse the |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2486 | /// AST of the return expression, with recursion backtracking when we |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2487 | /// encounter a subexpression that (1) clearly does not lead to one of the |
| 2488 | /// above problematic expressions (2) is something we cannot determine leads to |
| 2489 | /// a problematic expression based on such local checking. |
| 2490 | /// |
| 2491 | /// Both EvalAddr and EvalVal follow through reference variables to evaluate |
| 2492 | /// the expression that they point to. Such variables are added to the |
| 2493 | /// 'refVars' vector so that we know what the reference variable "trail" was. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2494 | /// |
Ted Kremenek | e07a8cd | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 2495 | /// EvalAddr processes expressions that are pointers that are used as |
| 2496 | /// references (and not L-values). EvalVal handles all other values. |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2497 | /// At the base case of the recursion is a check for the above problematic |
| 2498 | /// expressions. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2499 | /// |
| 2500 | /// This implementation handles: |
| 2501 | /// |
| 2502 | /// * pointer-to-pointer casts |
| 2503 | /// * implicit conversions from array references to pointers |
| 2504 | /// * taking the address of fields |
| 2505 | /// * arbitrary interplay between "&" and "*" operators |
| 2506 | /// * pointer arithmetic from an address of a stack variable |
| 2507 | /// * taking the address of an array element where the array is on the stack |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2508 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2509 | if (E->isTypeDependent()) |
| 2510 | return NULL; |
| 2511 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2512 | // We should only be called for evaluating pointer expressions. |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2513 | assert((E->getType()->isAnyPointerType() || |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2514 | E->getType()->isBlockPointerType() || |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2515 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2516 | "EvalAddr only works on pointers"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2517 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2518 | E = E->IgnoreParens(); |
| 2519 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2520 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 2521 | // viewed AST node. We then recursively traverse the AST by calling |
| 2522 | // EvalAddr and EvalVal appropriately. |
| 2523 | switch (E->getStmtClass()) { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2524 | case Stmt::DeclRefExprClass: { |
| 2525 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 2526 | |
| 2527 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
| 2528 | // If this is a reference variable, follow through to the expression that |
| 2529 | // it points to. |
| 2530 | if (V->hasLocalStorage() && |
| 2531 | V->getType()->isReferenceType() && V->hasInit()) { |
| 2532 | // Add the reference variable to the "trail". |
| 2533 | refVars.push_back(DR); |
| 2534 | return EvalAddr(V->getInit(), refVars); |
| 2535 | } |
| 2536 | |
| 2537 | return NULL; |
| 2538 | } |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2539 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2540 | case Stmt::UnaryOperatorClass: { |
| 2541 | // The only unary operator that make sense to handle here |
| 2542 | // is AddrOf. All others don't make sense as pointers. |
| 2543 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2545 | if (U->getOpcode() == UO_AddrOf) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2546 | return EvalVal(U->getSubExpr(), refVars); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2547 | else |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2548 | return NULL; |
| 2549 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2551 | case Stmt::BinaryOperatorClass: { |
| 2552 | // Handle pointer arithmetic. All other binary operators are not valid |
| 2553 | // in this context. |
| 2554 | BinaryOperator *B = cast<BinaryOperator>(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2555 | BinaryOperatorKind op = B->getOpcode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2556 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2557 | if (op != BO_Add && op != BO_Sub) |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2558 | return NULL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2560 | Expr *Base = B->getLHS(); |
| 2561 | |
| 2562 | // Determine which argument is the real pointer base. It could be |
| 2563 | // the RHS argument instead of the LHS. |
| 2564 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2566 | assert (Base->getType()->isPointerType()); |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2567 | return EvalAddr(Base, refVars); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2568 | } |
Steve Naroff | 2752a17 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 2569 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2570 | // For conditional operators we need to see if either the LHS or RHS are |
| 2571 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 2572 | case Stmt::ConditionalOperatorClass: { |
| 2573 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2575 | // Handle the GNU extension for missing LHS. |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2576 | if (Expr *lhsExpr = C->getLHS()) { |
| 2577 | // In C++, we can have a throw-expression, which has 'void' type. |
| 2578 | if (!lhsExpr->getType()->isVoidType()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2579 | if (Expr* LHS = EvalAddr(lhsExpr, refVars)) |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2580 | return LHS; |
| 2581 | } |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2582 | |
Douglas Gregor | 270b2ef | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 2583 | // In C++, we can have a throw-expression, which has 'void' type. |
| 2584 | if (C->getRHS()->getType()->isVoidType()) |
| 2585 | return NULL; |
| 2586 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2587 | return EvalAddr(C->getRHS(), refVars); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2588 | } |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2589 | |
| 2590 | case Stmt::BlockExprClass: |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2591 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2592 | return E; // local block. |
| 2593 | return NULL; |
| 2594 | |
| 2595 | case Stmt::AddrLabelExprClass: |
| 2596 | return E; // address of label. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | |
Ted Kremenek | c3b4c52 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2598 | // For casts, we need to handle conversions from arrays to |
| 2599 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2600 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | f19b231 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 2601 | case Stmt::CStyleCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2602 | case Stmt::CXXFunctionalCastExprClass: |
| 2603 | case Stmt::ObjCBridgedCastExprClass: { |
Argyrios Kyrtzidis | 3bab3d2 | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 2604 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | c3b4c52 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2605 | QualType T = SubExpr->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2607 | if (SubExpr->getType()->isPointerType() || |
| 2608 | SubExpr->getType()->isBlockPointerType() || |
| 2609 | SubExpr->getType()->isObjCQualifiedIdType()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2610 | return EvalAddr(SubExpr, refVars); |
Ted Kremenek | c3b4c52 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2611 | else if (T->isArrayType()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2612 | return EvalVal(SubExpr, refVars); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2613 | else |
Ted Kremenek | c3b4c52 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 2614 | return 0; |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2615 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2617 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 2618 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2619 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 2620 | // return NULL), we take the conservative route and report cases |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2621 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2622 | // FIXME: The comment about is wrong; we're not always converting |
| 2623 | // from pointer to pointer. I'm guessing that this code should also |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | // handle references to objects. |
| 2625 | case Stmt::CXXStaticCastExprClass: |
| 2626 | case Stmt::CXXDynamicCastExprClass: |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2627 | case Stmt::CXXConstCastExprClass: |
| 2628 | case Stmt::CXXReinterpretCastExprClass: { |
| 2629 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2630 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2631 | return EvalAddr(S, refVars); |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2632 | else |
| 2633 | return NULL; |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2634 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2636 | case Stmt::MaterializeTemporaryExprClass: |
| 2637 | if (Expr *Result = EvalAddr( |
| 2638 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
| 2639 | refVars)) |
| 2640 | return Result; |
| 2641 | |
| 2642 | return E; |
| 2643 | |
Chris Lattner | 934edb2 | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 2644 | // Everything else: we simply don't reason about them. |
| 2645 | default: |
| 2646 | return NULL; |
| 2647 | } |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2648 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2650 | |
| 2651 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 2652 | /// See the comments for EvalAddr for more details. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2653 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2654 | do { |
Ted Kremenek | e07a8cd | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 2655 | // We should only be called for evaluating non-pointer expressions, or |
| 2656 | // expressions with a pointer type that are not used as references but instead |
| 2657 | // are l-values (e.g., DeclRefExpr with a pointer type). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2659 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 2660 | // viewed AST node. We then recursively traverse the AST by calling |
| 2661 | // EvalAddr and EvalVal appropriately. |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2662 | |
| 2663 | E = E->IgnoreParens(); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2664 | switch (E->getStmtClass()) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2665 | case Stmt::ImplicitCastExprClass: { |
| 2666 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2667 | if (IE->getValueKind() == VK_LValue) { |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2668 | E = IE->getSubExpr(); |
| 2669 | continue; |
| 2670 | } |
| 2671 | return NULL; |
| 2672 | } |
| 2673 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2674 | case Stmt::DeclRefExprClass: { |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2675 | // When we hit a DeclRefExpr we are looking at code that refers to a |
| 2676 | // variable's name. If it's not a reference variable we check if it has |
| 2677 | // local storage within the function, and if so, return the expression. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2678 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2680 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2681 | if (V->hasLocalStorage()) { |
| 2682 | if (!V->getType()->isReferenceType()) |
| 2683 | return DR; |
| 2684 | |
| 2685 | // Reference variable, follow through to the expression that |
| 2686 | // it points to. |
| 2687 | if (V->hasInit()) { |
| 2688 | // Add the reference variable to the "trail". |
| 2689 | refVars.push_back(DR); |
| 2690 | return EvalVal(V->getInit(), refVars); |
| 2691 | } |
| 2692 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2694 | return NULL; |
| 2695 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2697 | case Stmt::UnaryOperatorClass: { |
| 2698 | // The only unary operator that make sense to handle here |
| 2699 | // is Deref. All others don't resolve to a "name." This includes |
| 2700 | // handling all sorts of rvalues passed to a unary operator. |
| 2701 | UnaryOperator *U = cast<UnaryOperator>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2703 | if (U->getOpcode() == UO_Deref) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2704 | return EvalAddr(U->getSubExpr(), refVars); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2705 | |
| 2706 | return NULL; |
| 2707 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2709 | case Stmt::ArraySubscriptExprClass: { |
| 2710 | // Array subscripts are potential references to data on the stack. We |
| 2711 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 2712 | // has local storage. |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2713 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2714 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2716 | case Stmt::ConditionalOperatorClass: { |
| 2717 | // For conditional operators we need to see if either the LHS or RHS are |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2718 | // non-NULL Expr's. If one is non-NULL, we return it. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2719 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 2720 | |
Anders Carlsson | 801c5c7 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 2721 | // Handle the GNU extension for missing LHS. |
| 2722 | if (Expr *lhsExpr = C->getLHS()) |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2723 | if (Expr *LHS = EvalVal(lhsExpr, refVars)) |
Anders Carlsson | 801c5c7 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 2724 | return LHS; |
| 2725 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2726 | return EvalVal(C->getRHS(), refVars); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2727 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2729 | // Accesses to members are potential references to data on the stack. |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2730 | case Stmt::MemberExprClass: { |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2731 | MemberExpr *M = cast<MemberExpr>(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2733 | // Check for indirect access. We only want direct field accesses. |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 2734 | if (M->isArrow()) |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2735 | return NULL; |
Ted Kremenek | cbe6b0b | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 2736 | |
| 2737 | // Check whether the member type is itself a reference, in which case |
| 2738 | // we're not going to refer to the member, but to what the member refers to. |
| 2739 | if (M->getMemberDecl()->getType()->isReferenceType()) |
| 2740 | return NULL; |
| 2741 | |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2742 | return EvalVal(M->getBase(), refVars); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2743 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2744 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2745 | case Stmt::MaterializeTemporaryExprClass: |
| 2746 | if (Expr *Result = EvalVal( |
| 2747 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), |
| 2748 | refVars)) |
| 2749 | return Result; |
| 2750 | |
| 2751 | return E; |
| 2752 | |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2753 | default: |
Argyrios Kyrtzidis | e72f715 | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 2754 | // Check that we don't return or take the address of a reference to a |
| 2755 | // temporary. This is only useful in C++. |
| 2756 | if (!E->isTypeDependent() && E->isRValue()) |
| 2757 | return E; |
| 2758 | |
| 2759 | // Everything else: we simply don't reason about them. |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2760 | return NULL; |
| 2761 | } |
Ted Kremenek | b786156 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 2762 | } while (true); |
Ted Kremenek | cff94fa | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 2763 | } |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2764 | |
| 2765 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 2766 | |
| 2767 | /// Check for comparisons of floating point operands using != and ==. |
| 2768 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 2769 | /// to do what the programmer intended. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 2770 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2771 | bool EmitWarning = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 2773 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); |
| 2774 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2775 | |
| 2776 | // Special case: check for x == x (which is OK). |
| 2777 | // Do not emit warnings for such cases. |
| 2778 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 2779 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 2780 | if (DRL->getDecl() == DRR->getDecl()) |
| 2781 | EmitWarning = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
| 2783 | |
Ted Kremenek | eda40e2 | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 2784 | // Special case: check for comparisons against literals that can be exactly |
| 2785 | // represented by APFloat. In such cases, do not emit a warning. This |
| 2786 | // is a heuristic: often comparison against such literals are used to |
| 2787 | // detect if a value in a variable has not changed. This clearly can |
| 2788 | // lead to false negatives. |
| 2789 | if (EmitWarning) { |
| 2790 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 2791 | if (FLL->isExact()) |
| 2792 | EmitWarning = false; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2793 | } else |
Ted Kremenek | eda40e2 | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 2794 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 2795 | if (FLR->isExact()) |
| 2796 | EmitWarning = false; |
| 2797 | } |
| 2798 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2800 | // Check for comparisons with builtin types. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2801 | if (EmitWarning) |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2802 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2803 | if (CL->isBuiltinCall(Context)) |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2804 | EmitWarning = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2805 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2806 | if (EmitWarning) |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2807 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2808 | if (CR->isBuiltinCall(Context)) |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2809 | EmitWarning = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2811 | // Emit the diagnostic. |
| 2812 | if (EmitWarning) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 2813 | Diag(Loc, diag::warn_floatingpoint_eq) |
| 2814 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Ted Kremenek | 43fb8b0 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 2815 | } |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2816 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2817 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// |
| 2818 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2819 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2820 | namespace { |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2821 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2822 | /// Structure recording the 'active' range of an integer-valued |
| 2823 | /// expression. |
| 2824 | struct IntRange { |
| 2825 | /// The number of bits active in the int. |
| 2826 | unsigned Width; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2827 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2828 | /// True if the int is known not to have negative values. |
| 2829 | bool NonNegative; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2830 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2831 | IntRange(unsigned Width, bool NonNegative) |
| 2832 | : Width(Width), NonNegative(NonNegative) |
| 2833 | {} |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 2834 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2835 | /// Returns the range of the bool type. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2836 | static IntRange forBoolType() { |
| 2837 | return IntRange(1, true); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2840 | /// Returns the range of an opaque value of the given integral type. |
| 2841 | static IntRange forValueOfType(ASTContext &C, QualType T) { |
| 2842 | return forValueOfCanonicalType(C, |
| 2843 | T->getCanonicalTypeInternal().getTypePtr()); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2846 | /// Returns the range of an opaque value of a canonical integral type. |
| 2847 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2848 | assert(T->isCanonicalUnqualified()); |
| 2849 | |
| 2850 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 2851 | T = VT->getElementType().getTypePtr(); |
| 2852 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 2853 | T = CT->getElementType().getTypePtr(); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2854 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2855 | // For enum types, use the known bit width of the enumerators. |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2856 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { |
| 2857 | EnumDecl *Enum = ET->getDecl(); |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2858 | if (!Enum->isCompleteDefinition()) |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2859 | return IntRange(C.getIntWidth(QualType(T, 0)), false); |
| 2860 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 2861 | unsigned NumPositive = Enum->getNumPositiveBits(); |
| 2862 | unsigned NumNegative = Enum->getNumNegativeBits(); |
| 2863 | |
| 2864 | return IntRange(std::max(NumPositive, NumNegative), NumNegative == 0); |
| 2865 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2866 | |
| 2867 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 2868 | assert(BT->isInteger()); |
| 2869 | |
| 2870 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 2871 | } |
| 2872 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2873 | /// Returns the "target" range of a canonical integral type, i.e. |
| 2874 | /// the range of values expressible in the type. |
| 2875 | /// |
| 2876 | /// This matches forValueOfCanonicalType except that enums have the |
| 2877 | /// full range of their type, not the range of their enumerators. |
| 2878 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { |
| 2879 | assert(T->isCanonicalUnqualified()); |
| 2880 | |
| 2881 | if (const VectorType *VT = dyn_cast<VectorType>(T)) |
| 2882 | T = VT->getElementType().getTypePtr(); |
| 2883 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 2884 | T = CT->getElementType().getTypePtr(); |
| 2885 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Douglas Gregor | 3168dcf | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 2886 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2887 | |
| 2888 | const BuiltinType *BT = cast<BuiltinType>(T); |
| 2889 | assert(BT->isInteger()); |
| 2890 | |
| 2891 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); |
| 2892 | } |
| 2893 | |
| 2894 | /// Returns the supremum of two ranges: i.e. their conservative merge. |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2895 | static IntRange join(IntRange L, IntRange R) { |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2896 | return IntRange(std::max(L.Width, R.Width), |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2897 | L.NonNegative && R.NonNegative); |
| 2898 | } |
| 2899 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2900 | /// Returns the infinum of two ranges: i.e. their aggressive merge. |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 2901 | static IntRange meet(IntRange L, IntRange R) { |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2902 | return IntRange(std::min(L.Width, R.Width), |
| 2903 | L.NonNegative || R.NonNegative); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2904 | } |
| 2905 | }; |
| 2906 | |
| 2907 | IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, unsigned MaxWidth) { |
| 2908 | if (value.isSigned() && value.isNegative()) |
| 2909 | return IntRange(value.getMinSignedBits(), false); |
| 2910 | |
| 2911 | if (value.getBitWidth() > MaxWidth) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2912 | value = value.trunc(MaxWidth); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2913 | |
| 2914 | // isNonNegative() just checks the sign bit without considering |
| 2915 | // signedness. |
| 2916 | return IntRange(value.getActiveBits(), true); |
| 2917 | } |
| 2918 | |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2919 | IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2920 | unsigned MaxWidth) { |
| 2921 | if (result.isInt()) |
| 2922 | return GetValueRange(C, result.getInt(), MaxWidth); |
| 2923 | |
| 2924 | if (result.isVector()) { |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2925 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); |
| 2926 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { |
| 2927 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); |
| 2928 | R = IntRange::join(R, El); |
| 2929 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2930 | return R; |
| 2931 | } |
| 2932 | |
| 2933 | if (result.isComplexInt()) { |
| 2934 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); |
| 2935 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); |
| 2936 | return IntRange::join(R, I); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | // This can happen with lossless casts to intptr_t of "based" lvalues. |
| 2940 | // Assume it might use arbitrary bits. |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2941 | // FIXME: The only reason we need to pass the type in here is to get |
| 2942 | // the sign right on this one case. It would be nice if APValue |
| 2943 | // preserved this. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2944 | assert(result.isLValue()); |
Douglas Gregor | 61b6e49 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 2945 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 2946 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2947 | |
| 2948 | /// Pseudo-evaluate the given integer expression, estimating the |
| 2949 | /// range of values it might take. |
| 2950 | /// |
| 2951 | /// \param MaxWidth - the width to which the value will be truncated |
| 2952 | IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { |
| 2953 | E = E->IgnoreParens(); |
| 2954 | |
| 2955 | // Try a full evaluation first. |
| 2956 | Expr::EvalResult result; |
| 2957 | if (E->Evaluate(result, C)) |
John McCall | 7443052 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 2958 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2959 | |
| 2960 | // I think we only want to look through implicit casts here; if the |
| 2961 | // user has an explicit widening cast, we should treat the value as |
| 2962 | // being of the new, wider type. |
| 2963 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2964 | if (CE->getCastKind() == CK_NoOp) |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2965 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); |
| 2966 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 2967 | IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2968 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2969 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2970 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2971 | // Assume that non-integer casts can span the full range of the type. |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 2972 | if (!isIntegerCast) |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 2973 | return OutputTypeRange; |
| 2974 | |
| 2975 | IntRange SubRange |
| 2976 | = GetExprRange(C, CE->getSubExpr(), |
| 2977 | std::min(MaxWidth, OutputTypeRange.Width)); |
| 2978 | |
| 2979 | // Bail out if the subexpr's range is as wide as the cast type. |
| 2980 | if (SubRange.Width >= OutputTypeRange.Width) |
| 2981 | return OutputTypeRange; |
| 2982 | |
| 2983 | // Otherwise, we take the smaller width, and we're non-negative if |
| 2984 | // either the output type or the subexpr is. |
| 2985 | return IntRange(SubRange.Width, |
| 2986 | SubRange.NonNegative || OutputTypeRange.NonNegative); |
| 2987 | } |
| 2988 | |
| 2989 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 2990 | // If we can fold the condition, just take that operand. |
| 2991 | bool CondResult; |
| 2992 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) |
| 2993 | return GetExprRange(C, CondResult ? CO->getTrueExpr() |
| 2994 | : CO->getFalseExpr(), |
| 2995 | MaxWidth); |
| 2996 | |
| 2997 | // Otherwise, conservatively merge. |
| 2998 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); |
| 2999 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); |
| 3000 | return IntRange::join(L, R); |
| 3001 | } |
| 3002 | |
| 3003 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3004 | switch (BO->getOpcode()) { |
| 3005 | |
| 3006 | // Boolean-valued operations are single-bit and positive. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3007 | case BO_LAnd: |
| 3008 | case BO_LOr: |
| 3009 | case BO_LT: |
| 3010 | case BO_GT: |
| 3011 | case BO_LE: |
| 3012 | case BO_GE: |
| 3013 | case BO_EQ: |
| 3014 | case BO_NE: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3015 | return IntRange::forBoolType(); |
| 3016 | |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3017 | // The type of the assignments is the type of the LHS, so the RHS |
| 3018 | // is not necessarily the same type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3019 | case BO_MulAssign: |
| 3020 | case BO_DivAssign: |
| 3021 | case BO_RemAssign: |
| 3022 | case BO_AddAssign: |
| 3023 | case BO_SubAssign: |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3024 | case BO_XorAssign: |
| 3025 | case BO_OrAssign: |
| 3026 | // TODO: bitfields? |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3027 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | ff96ccd | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3028 | |
John McCall | c368838 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 3029 | // Simple assignments just pass through the RHS, which will have |
| 3030 | // been coerced to the LHS type. |
| 3031 | case BO_Assign: |
| 3032 | // TODO: bitfields? |
| 3033 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3034 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3035 | // Operations with opaque sources are black-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3036 | case BO_PtrMemD: |
| 3037 | case BO_PtrMemI: |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3038 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3039 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3040 | // Bitwise-and uses the *infinum* of the two source ranges. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3041 | case BO_And: |
| 3042 | case BO_AndAssign: |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3043 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), |
| 3044 | GetExprRange(C, BO->getRHS(), MaxWidth)); |
| 3045 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3046 | // Left shift gets black-listed based on a judgement call. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3047 | case BO_Shl: |
John McCall | 1bff993 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3048 | // ...except that we want to treat '1 << (blah)' as logically |
| 3049 | // positive. It's an important idiom. |
| 3050 | if (IntegerLiteral *I |
| 3051 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { |
| 3052 | if (I->getValue() == 1) { |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3053 | IntRange R = IntRange::forValueOfType(C, E->getType()); |
John McCall | 1bff993 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 3054 | return IntRange(R.Width, /*NonNegative*/ true); |
| 3055 | } |
| 3056 | } |
| 3057 | // fallthrough |
| 3058 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3059 | case BO_ShlAssign: |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3060 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3061 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3062 | // Right shift by a constant can narrow its left argument. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3063 | case BO_Shr: |
| 3064 | case BO_ShrAssign: { |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3065 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 3066 | |
| 3067 | // If the shift amount is a positive constant, drop the width by |
| 3068 | // that much. |
| 3069 | llvm::APSInt shift; |
| 3070 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && |
| 3071 | shift.isNonNegative()) { |
| 3072 | unsigned zext = shift.getZExtValue(); |
| 3073 | if (zext >= L.Width) |
| 3074 | L.Width = (L.NonNegative ? 0 : 1); |
| 3075 | else |
| 3076 | L.Width -= zext; |
| 3077 | } |
| 3078 | |
| 3079 | return L; |
| 3080 | } |
| 3081 | |
| 3082 | // Comma acts as its right operand. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3083 | case BO_Comma: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3084 | return GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3085 | |
John McCall | 2ce81ad | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3086 | // Black-list pointer subtractions. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3087 | case BO_Sub: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3088 | if (BO->getLHS()->getType()->isPointerType()) |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3089 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3090 | break; |
Ted Kremenek | c8b188d | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 3091 | |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3092 | // The width of a division result is mostly determined by the size |
| 3093 | // of the LHS. |
| 3094 | case BO_Div: { |
| 3095 | // Don't 'pre-truncate' the operands. |
| 3096 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3097 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3098 | |
| 3099 | // If the divisor is constant, use that. |
| 3100 | llvm::APSInt divisor; |
| 3101 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { |
| 3102 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) |
| 3103 | if (log2 >= L.Width) |
| 3104 | L.Width = (L.NonNegative ? 0 : 1); |
| 3105 | else |
| 3106 | L.Width = std::min(L.Width - log2, MaxWidth); |
| 3107 | return L; |
| 3108 | } |
| 3109 | |
| 3110 | // Otherwise, just use the LHS's width. |
| 3111 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3112 | return IntRange(L.Width, L.NonNegative && R.NonNegative); |
| 3113 | } |
| 3114 | |
| 3115 | // The result of a remainder can't be larger than the result of |
| 3116 | // either side. |
| 3117 | case BO_Rem: { |
| 3118 | // Don't 'pre-truncate' the operands. |
| 3119 | unsigned opWidth = C.getIntWidth(E->getType()); |
| 3120 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); |
| 3121 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); |
| 3122 | |
| 3123 | IntRange meet = IntRange::meet(L, R); |
| 3124 | meet.Width = std::min(meet.Width, MaxWidth); |
| 3125 | return meet; |
| 3126 | } |
| 3127 | |
| 3128 | // The default behavior is okay for these. |
| 3129 | case BO_Mul: |
| 3130 | case BO_Add: |
| 3131 | case BO_Xor: |
| 3132 | case BO_Or: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3133 | break; |
| 3134 | } |
| 3135 | |
John McCall | 5143181 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 3136 | // The default case is to treat the operation as if it were closed |
| 3137 | // on the narrowest type that encompasses both operands. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3138 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); |
| 3139 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); |
| 3140 | return IntRange::join(L, R); |
| 3141 | } |
| 3142 | |
| 3143 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 3144 | switch (UO->getOpcode()) { |
| 3145 | // Boolean-valued operations are white-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3146 | case UO_LNot: |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3147 | return IntRange::forBoolType(); |
| 3148 | |
| 3149 | // Operations with opaque sources are black-listed. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3150 | case UO_Deref: |
| 3151 | case UO_AddrOf: // should be impossible |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3152 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3153 | |
| 3154 | default: |
| 3155 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); |
| 3156 | } |
| 3157 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3158 | |
| 3159 | if (dyn_cast<OffsetOfExpr>(E)) { |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3160 | IntRange::forValueOfType(C, E->getType()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3161 | } |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3162 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3163 | if (FieldDecl *BitField = E->getBitField()) |
| 3164 | return IntRange(BitField->getBitWidthValue(C), |
Douglas Gregor | 61b6e49 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 3165 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3166 | |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3167 | return IntRange::forValueOfType(C, E->getType()); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3168 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3169 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3170 | IntRange GetExprRange(ASTContext &C, Expr *E) { |
| 3171 | return GetExprRange(C, E, C.getIntWidth(E->getType())); |
| 3172 | } |
| 3173 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3174 | /// Checks whether the given value, which currently has the given |
| 3175 | /// source semantics, has the same value when coerced through the |
| 3176 | /// target semantics. |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3177 | bool IsSameFloatAfterCast(const llvm::APFloat &value, |
| 3178 | const llvm::fltSemantics &Src, |
| 3179 | const llvm::fltSemantics &Tgt) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3180 | llvm::APFloat truncated = value; |
| 3181 | |
| 3182 | bool ignored; |
| 3183 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3184 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3185 | |
| 3186 | return truncated.bitwiseIsEqual(value); |
| 3187 | } |
| 3188 | |
| 3189 | /// Checks whether the given value, which currently has the given |
| 3190 | /// source semantics, has the same value when coerced through the |
| 3191 | /// target semantics. |
| 3192 | /// |
| 3193 | /// The value might be a vector of floats (or a complex number). |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3194 | bool IsSameFloatAfterCast(const APValue &value, |
| 3195 | const llvm::fltSemantics &Src, |
| 3196 | const llvm::fltSemantics &Tgt) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3197 | if (value.isFloat()) |
| 3198 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); |
| 3199 | |
| 3200 | if (value.isVector()) { |
| 3201 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) |
| 3202 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) |
| 3203 | return false; |
| 3204 | return true; |
| 3205 | } |
| 3206 | |
| 3207 | assert(value.isComplexFloat()); |
| 3208 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && |
| 3209 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); |
| 3210 | } |
| 3211 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3212 | void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3213 | |
Ted Kremenek | 6274be4 | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 3214 | static bool IsZero(Sema &S, Expr *E) { |
| 3215 | // Suppress cases where we are comparing against an enum constant. |
| 3216 | if (const DeclRefExpr *DR = |
| 3217 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 3218 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 3219 | return false; |
| 3220 | |
| 3221 | // Suppress cases where the '0' value is expanded from a macro. |
| 3222 | if (E->getLocStart().isMacroID()) |
| 3223 | return false; |
| 3224 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3225 | llvm::APSInt Value; |
| 3226 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; |
| 3227 | } |
| 3228 | |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3229 | static bool HasEnumType(Expr *E) { |
| 3230 | // Strip off implicit integral promotions. |
| 3231 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
Argyrios Kyrtzidis | 15a9edc | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 3232 | if (ICE->getCastKind() != CK_IntegralCast && |
| 3233 | ICE->getCastKind() != CK_NoOp) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3234 | break; |
Argyrios Kyrtzidis | 15a9edc | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 3235 | E = ICE->getSubExpr(); |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | return E->getType()->isEnumeralType(); |
| 3239 | } |
| 3240 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3241 | void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3242 | BinaryOperatorKind op = E->getOpcode(); |
Douglas Gregor | b14dbd7 | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 3243 | if (E->isValueDependent()) |
| 3244 | return; |
| 3245 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3246 | if (op == BO_LT && IsZero(S, E->getRHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3247 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3248 | << "< 0" << "false" << HasEnumType(E->getLHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3249 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3250 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3251 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3252 | << ">= 0" << "true" << HasEnumType(E->getLHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3253 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3254 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3255 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3256 | << "0 >" << "false" << HasEnumType(E->getRHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3257 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3258 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3259 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) |
John McCall | 2551c1b | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 3260 | << "0 <=" << "true" << HasEnumType(E->getRHS()) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3261 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | /// Analyze the operands of the given comparison. Implements the |
| 3266 | /// fallback case from AnalyzeComparison. |
| 3267 | void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3268 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 3269 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3270 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3271 | |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3272 | /// \brief Implements -Wsign-compare. |
| 3273 | /// |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3274 | /// \param E the binary operator to check for warnings |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3275 | void AnalyzeComparison(Sema &S, BinaryOperator *E) { |
| 3276 | // The type the comparison is being performed in. |
| 3277 | QualType T = E->getLHS()->getType(); |
| 3278 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) |
| 3279 | && "comparison with mismatched types"); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3280 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3281 | // We don't do anything special if this isn't an unsigned integral |
| 3282 | // comparison: we're only interested in integral comparisons, and |
| 3283 | // signed comparisons only happen in cases we don't care to warn about. |
Douglas Gregor | 5b05454 | 2011-02-19 22:34:59 +0000 | [diff] [blame] | 3284 | // |
| 3285 | // We also don't care about value-dependent expressions or expressions |
| 3286 | // whose result is a constant. |
| 3287 | if (!T->hasUnsignedIntegerRepresentation() |
| 3288 | || E->isValueDependent() || E->isIntegerConstantExpr(S.Context)) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3289 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3290 | |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3291 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); |
| 3292 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3293 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3294 | // Check to see if one of the (unmodified) operands is of different |
| 3295 | // signedness. |
| 3296 | Expr *signedOperand, *unsignedOperand; |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3297 | if (LHS->getType()->hasSignedIntegerRepresentation()) { |
| 3298 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3299 | "unsigned comparison between two signed integer expressions?"); |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3300 | signedOperand = LHS; |
| 3301 | unsignedOperand = RHS; |
| 3302 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { |
| 3303 | signedOperand = RHS; |
| 3304 | unsignedOperand = LHS; |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3305 | } else { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3306 | CheckTrivialUnsignedComparison(S, E); |
| 3307 | return AnalyzeImpConvsInComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3310 | // Otherwise, calculate the effective range of the signed operand. |
| 3311 | IntRange signedRange = GetExprRange(S.Context, signedOperand); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3312 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3313 | // Go ahead and analyze implicit conversions in the operands. Note |
| 3314 | // that we skip the implicit conversions on both sides. |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3315 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); |
| 3316 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3317 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3318 | // If the signed range is non-negative, -Wsign-compare won't fire, |
| 3319 | // but we should still check for comparisons which are always true |
| 3320 | // or false. |
| 3321 | if (signedRange.NonNegative) |
| 3322 | return CheckTrivialUnsignedComparison(S, E); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3323 | |
| 3324 | // For (in)equality comparisons, if the unsigned operand is a |
| 3325 | // constant which cannot collide with a overflowed signed operand, |
| 3326 | // then reinterpreting the signed operand as unsigned will not |
| 3327 | // change the result of the comparison. |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3328 | if (E->isEqualityOp()) { |
| 3329 | unsigned comparisonWidth = S.Context.getIntWidth(T); |
| 3330 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3331 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3332 | // We should never be unable to prove that the unsigned operand is |
| 3333 | // non-negative. |
| 3334 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); |
| 3335 | |
| 3336 | if (unsignedRange.Width < comparisonWidth) |
| 3337 | return; |
| 3338 | } |
| 3339 | |
| 3340 | S.Diag(E->getOperatorLoc(), diag::warn_mixed_sign_comparison) |
Richard Trieu | 82402a0 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3341 | << LHS->getType() << RHS->getType() |
| 3342 | << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | ca01b22 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3345 | /// Analyzes an attempt to assign the given value to a bitfield. |
| 3346 | /// |
| 3347 | /// Returns true if there was something fishy about the attempt. |
| 3348 | bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, |
| 3349 | SourceLocation InitLoc) { |
| 3350 | assert(Bitfield->isBitField()); |
| 3351 | if (Bitfield->isInvalidDecl()) |
| 3352 | return false; |
| 3353 | |
John McCall | deebbcf | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 3354 | // White-list bool bitfields. |
| 3355 | if (Bitfield->getType()->isBooleanType()) |
| 3356 | return false; |
| 3357 | |
Douglas Gregor | 789adec | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 3358 | // Ignore value- or type-dependent expressions. |
| 3359 | if (Bitfield->getBitWidth()->isValueDependent() || |
| 3360 | Bitfield->getBitWidth()->isTypeDependent() || |
| 3361 | Init->isValueDependent() || |
| 3362 | Init->isTypeDependent()) |
| 3363 | return false; |
| 3364 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3365 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); |
| 3366 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3367 | Expr::EvalResult InitValue; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3368 | if (!OriginalInit->Evaluate(InitValue, S.Context) || |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3369 | !InitValue.Val.isInt()) |
| 3370 | return false; |
| 3371 | |
| 3372 | const llvm::APSInt &Value = InitValue.Val.getInt(); |
| 3373 | unsigned OriginalWidth = Value.getBitWidth(); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3374 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3375 | |
| 3376 | if (OriginalWidth <= FieldWidth) |
| 3377 | return false; |
| 3378 | |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3379 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3380 | |
| 3381 | // It's fairly common to write values into signed bitfields |
| 3382 | // that, if sign-extended, would end up becoming a different |
| 3383 | // value. We don't want to warn about that. |
| 3384 | if (Value.isSigned() && Value.isNegative()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3385 | TruncatedValue = TruncatedValue.sext(OriginalWidth); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3386 | else |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3387 | TruncatedValue = TruncatedValue.zext(OriginalWidth); |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3388 | |
| 3389 | if (Value == TruncatedValue) |
| 3390 | return false; |
| 3391 | |
| 3392 | std::string PrettyValue = Value.toString(10); |
| 3393 | std::string PrettyTrunc = TruncatedValue.toString(10); |
| 3394 | |
| 3395 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) |
| 3396 | << PrettyValue << PrettyTrunc << OriginalInit->getType() |
| 3397 | << Init->getSourceRange(); |
| 3398 | |
| 3399 | return true; |
| 3400 | } |
| 3401 | |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 3402 | /// Analyze the given simple or compound assignment for warning-worthy |
| 3403 | /// operations. |
| 3404 | void AnalyzeAssignment(Sema &S, BinaryOperator *E) { |
| 3405 | // Just recurse on the LHS. |
| 3406 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); |
| 3407 | |
| 3408 | // We want to recurse on the RHS as normal unless we're assigning to |
| 3409 | // a bitfield. |
| 3410 | if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3411 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), |
| 3412 | E->getOperatorLoc())) { |
| 3413 | // Recurse, ignoring any implicit conversions on the RHS. |
| 3414 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), |
| 3415 | E->getOperatorLoc()); |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); |
| 3420 | } |
| 3421 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3422 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 3423 | void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, |
| 3424 | SourceLocation CContext, unsigned diag) { |
| 3425 | S.Diag(E->getExprLoc(), diag) |
| 3426 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); |
| 3427 | } |
| 3428 | |
Chandler Carruth | 7f3654f | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 3429 | /// Diagnose an implicit cast; purely a helper for CheckImplicitConversion. |
| 3430 | void DiagnoseImpCast(Sema &S, Expr *E, QualType T, SourceLocation CContext, |
| 3431 | unsigned diag) { |
| 3432 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag); |
| 3433 | } |
| 3434 | |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3435 | /// Diagnose an implicit cast from a literal expression. Does not warn when the |
| 3436 | /// cast wouldn't lose information. |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3437 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, |
| 3438 | SourceLocation CContext) { |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3439 | // Try to convert the literal exactly to an integer. If we can, don't warn. |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3440 | bool isExact = false; |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3441 | const llvm::APFloat &Value = FL->getValue(); |
Jeffrey Yasskin | d0f079d | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 3442 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), |
| 3443 | T->hasUnsignedIntegerRepresentation()); |
| 3444 | if (Value.convertToInteger(IntegerValue, |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3445 | llvm::APFloat::rmTowardZero, &isExact) |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3446 | == llvm::APFloat::opOK && isExact) |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3447 | return; |
| 3448 | |
Matt Beaumont-Gay | c622163 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 3449 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) |
| 3450 | << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext); |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3453 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { |
| 3454 | if (!Range.Width) return "0"; |
| 3455 | |
| 3456 | llvm::APSInt ValueInRange = Value; |
| 3457 | ValueInRange.setIsSigned(!Range.NonNegative); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3458 | ValueInRange = ValueInRange.trunc(Range.Width); |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3459 | return ValueInRange.toString(10); |
| 3460 | } |
| 3461 | |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3462 | static bool isFromSystemMacro(Sema &S, SourceLocation loc) { |
| 3463 | SourceManager &smgr = S.Context.getSourceManager(); |
| 3464 | return loc.isMacroID() && smgr.isInSystemHeader(smgr.getSpellingLoc(loc)); |
| 3465 | } |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3466 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3467 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3468 | SourceLocation CC, bool *ICContext = 0) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3469 | if (E->isTypeDependent() || E->isValueDependent()) return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3470 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3471 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); |
| 3472 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); |
| 3473 | if (Source == Target) return; |
| 3474 | if (Target->isDependentType()) return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3475 | |
Chandler Carruth | c22845a | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 3476 | // If the conversion context location is invalid don't complain. We also |
| 3477 | // don't want to emit a warning if the issue occurs from the expansion of |
| 3478 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we |
| 3479 | // delay this check as long as possible. Once we detect we are in that |
| 3480 | // scenario, we just return. |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3481 | if (CC.isInvalid()) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3482 | return; |
| 3483 | |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 3484 | // Diagnose implicit casts to bool. |
| 3485 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { |
| 3486 | if (isa<StringLiteral>(E)) |
| 3487 | // Warn on string literal to bool. Checks for string literals in logical |
| 3488 | // expressions, for instances, assert(0 && "error here"), is prevented |
| 3489 | // by a check in AnalyzeImplicitConversions(). |
| 3490 | return DiagnoseImpCast(S, E, T, CC, |
| 3491 | diag::warn_impcast_string_literal_to_bool); |
David Blaikie | 7833b7d | 2011-09-29 04:06:47 +0000 | [diff] [blame] | 3492 | return; // Other casts to bool are not checked. |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 3493 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3494 | |
| 3495 | // Strip vector types. |
| 3496 | if (isa<VectorType>(Source)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3497 | if (!isa<VectorType>(Target)) { |
| 3498 | if (isFromSystemMacro(S, CC)) |
| 3499 | return; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3500 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3501 | } |
Chris Lattner | ee7286f | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 3502 | |
| 3503 | // If the vector cast is cast between two vectors of the same size, it is |
| 3504 | // a bitcast, not a conversion. |
| 3505 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) |
| 3506 | return; |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3507 | |
| 3508 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); |
| 3509 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); |
| 3510 | } |
| 3511 | |
| 3512 | // Strip complex types. |
| 3513 | if (isa<ComplexType>(Source)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3514 | if (!isa<ComplexType>(Target)) { |
| 3515 | if (isFromSystemMacro(S, CC)) |
| 3516 | return; |
| 3517 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3518 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3519 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3520 | |
| 3521 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); |
| 3522 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); |
| 3523 | } |
| 3524 | |
| 3525 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); |
| 3526 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); |
| 3527 | |
| 3528 | // If the source is floating point... |
| 3529 | if (SourceBT && SourceBT->isFloatingPoint()) { |
| 3530 | // ...and the target is floating point... |
| 3531 | if (TargetBT && TargetBT->isFloatingPoint()) { |
| 3532 | // ...then warn if we're dropping FP rank. |
| 3533 | |
| 3534 | // Builtin FP kinds are ordered by increasing FP rank. |
| 3535 | if (SourceBT->getKind() > TargetBT->getKind()) { |
| 3536 | // Don't warn about float constants that are precisely |
| 3537 | // representable in the target type. |
| 3538 | Expr::EvalResult result; |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3539 | if (E->Evaluate(result, S.Context)) { |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3540 | // Value might be a float, a float vector, or a float complex. |
| 3541 | if (IsSameFloatAfterCast(result.Val, |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3542 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), |
| 3543 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3544 | return; |
| 3545 | } |
| 3546 | |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3547 | if (isFromSystemMacro(S, CC)) |
| 3548 | return; |
| 3549 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3550 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3551 | } |
| 3552 | return; |
| 3553 | } |
| 3554 | |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3555 | // If the target is integral, always warn. |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3556 | if ((TargetBT && TargetBT->isInteger())) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3557 | if (isFromSystemMacro(S, CC)) |
| 3558 | return; |
| 3559 | |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3560 | Expr *InnerE = E->IgnoreParenImpCasts(); |
Matt Beaumont-Gay | 042ce8e | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 3561 | // We also want to warn on, e.g., "int i = -1.234" |
| 3562 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) |
| 3563 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) |
| 3564 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); |
| 3565 | |
Chandler Carruth | 016ef40 | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 3566 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { |
| 3567 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); |
Chandler Carruth | 22c7a79 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 3568 | } else { |
| 3569 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); |
| 3570 | } |
| 3571 | } |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3572 | |
| 3573 | return; |
| 3574 | } |
| 3575 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3576 | if (!Source->isIntegerType() || !Target->isIntegerType()) |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3577 | return; |
| 3578 | |
Richard Trieu | beaf345 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 3579 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) |
| 3580 | == Expr::NPCK_GNUNull) && Target->isIntegerType()) { |
| 3581 | S.Diag(E->getExprLoc(), diag::warn_impcast_null_pointer_to_integer) |
| 3582 | << E->getSourceRange() << clang::SourceRange(CC); |
| 3583 | return; |
| 3584 | } |
| 3585 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3586 | IntRange SourceRange = GetExprRange(S.Context, E); |
John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3587 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3588 | |
| 3589 | if (SourceRange.Width > TargetRange.Width) { |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3590 | // If the source is a constant, use a default-on diagnostic. |
| 3591 | // TODO: this should happen for bitfield stores, too. |
| 3592 | llvm::APSInt Value(32); |
| 3593 | if (E->isIntegerConstantExpr(Value, S.Context)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3594 | if (isFromSystemMacro(S, CC)) |
| 3595 | return; |
| 3596 | |
John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3597 | std::string PrettySourceValue = Value.toString(10); |
| 3598 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); |
| 3599 | |
| 3600 | S.Diag(E->getExprLoc(), diag::warn_impcast_integer_precision_constant) |
| 3601 | << PrettySourceValue << PrettyTargetValue |
| 3602 | << E->getType() << T << E->getSourceRange() << clang::SourceRange(CC); |
| 3603 | return; |
| 3604 | } |
| 3605 | |
Chris Lattner | ee7286f | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 3606 | // People want to build with -Wshorten-64-to-32 and not -Wconversion. |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3607 | if (isFromSystemMacro(S, CC)) |
| 3608 | return; |
| 3609 | |
John McCall | 70aa539 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3610 | if (SourceRange.Width == 64 && TargetRange.Width == 32) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3611 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32); |
| 3612 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || |
| 3616 | (!TargetRange.NonNegative && SourceRange.NonNegative && |
| 3617 | SourceRange.Width == TargetRange.Width)) { |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3618 | |
| 3619 | if (isFromSystemMacro(S, CC)) |
| 3620 | return; |
| 3621 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3622 | unsigned DiagID = diag::warn_impcast_integer_sign; |
| 3623 | |
| 3624 | // Traditionally, gcc has warned about this under -Wsign-compare. |
| 3625 | // We also want to warn about it in -Wconversion. |
| 3626 | // So if -Wconversion is off, use a completely identical diagnostic |
| 3627 | // in the sign-compare group. |
| 3628 | // The conditional-checking code will |
| 3629 | if (ICContext) { |
| 3630 | DiagID = diag::warn_impcast_integer_sign_conditional; |
| 3631 | *ICContext = true; |
| 3632 | } |
| 3633 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3634 | return DiagnoseImpCast(S, E, T, CC, DiagID); |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3637 | // Diagnose conversions between different enumeration types. |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 3638 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration |
| 3639 | // type, to give us better diagnostics. |
| 3640 | QualType SourceType = E->getType(); |
| 3641 | if (!S.getLangOptions().CPlusPlus) { |
| 3642 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 3643 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 3644 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 3645 | SourceType = S.Context.getTypeDeclType(Enum); |
| 3646 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); |
| 3647 | } |
| 3648 | } |
| 3649 | |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3650 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) |
| 3651 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) |
| 3652 | if ((SourceEnum->getDecl()->getIdentifier() || |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3653 | SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3654 | (TargetEnum->getDecl()->getIdentifier() || |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3655 | TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3656 | SourceEnum != TargetEnum) { |
| 3657 | if (isFromSystemMacro(S, CC)) |
| 3658 | return; |
| 3659 | |
Douglas Gregor | 364f7db | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 3660 | return DiagnoseImpCast(S, E, SourceType, T, CC, |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3661 | diag::warn_impcast_different_enum_types); |
Ted Kremenek | 4c0826c | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 3662 | } |
Douglas Gregor | a78f193 | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 3663 | |
John McCall | 263a48b | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3664 | return; |
| 3665 | } |
| 3666 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3667 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T); |
| 3668 | |
| 3669 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3670 | SourceLocation CC, bool &ICContext) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3671 | E = E->IgnoreParenImpCasts(); |
| 3672 | |
| 3673 | if (isa<ConditionalOperator>(E)) |
| 3674 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), T); |
| 3675 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3676 | AnalyzeImplicitConversions(S, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3677 | if (E->getType() != T) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3678 | return CheckImplicitConversion(S, E, T, CC, &ICContext); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3679 | return; |
| 3680 | } |
| 3681 | |
| 3682 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, QualType T) { |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3683 | SourceLocation CC = E->getQuestionLoc(); |
| 3684 | |
| 3685 | AnalyzeImplicitConversions(S, E->getCond(), CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3686 | |
| 3687 | bool Suspicious = false; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3688 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); |
| 3689 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3690 | |
| 3691 | // If -Wconversion would have warned about either of the candidates |
| 3692 | // for a signedness conversion to the context type... |
| 3693 | if (!Suspicious) return; |
| 3694 | |
| 3695 | // ...but it's currently ignored... |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3696 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, |
| 3697 | CC)) |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3698 | return; |
| 3699 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3700 | // ...then check whether it would have warned about either of the |
| 3701 | // candidates for a signedness conversion to the condition type. |
Richard Trieu | bb43dec | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 3702 | if (E->getType() == T) return; |
| 3703 | |
| 3704 | Suspicious = false; |
| 3705 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), |
| 3706 | E->getType(), CC, &Suspicious); |
| 3707 | if (!Suspicious) |
| 3708 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3709 | E->getType(), CC, &Suspicious); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | /// AnalyzeImplicitConversions - Find and report any interesting |
| 3713 | /// implicit conversions in the given expression. There are a couple |
| 3714 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3715 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3716 | QualType T = OrigE->getType(); |
| 3717 | Expr *E = OrigE->IgnoreParenImpCasts(); |
| 3718 | |
Douglas Gregor | 6e8da6a | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 3719 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3720 | return; |
| 3721 | |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3722 | // For conditional operators, we analyze the arguments as if they |
| 3723 | // were being fed directly into the output. |
| 3724 | if (isa<ConditionalOperator>(E)) { |
| 3725 | ConditionalOperator *CO = cast<ConditionalOperator>(E); |
| 3726 | CheckConditionalOperator(S, CO, T); |
| 3727 | return; |
| 3728 | } |
| 3729 | |
| 3730 | // Go ahead and check any implicit conversions we might have skipped. |
| 3731 | // The non-canonical typecheck is just an optimization; |
| 3732 | // CheckImplicitConversion will filter out dead implicit conversions. |
| 3733 | if (E->getType() != T) |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3734 | CheckImplicitConversion(S, E, T, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3735 | |
| 3736 | // Now continue drilling into this expression. |
| 3737 | |
| 3738 | // Skip past explicit casts. |
| 3739 | if (isa<ExplicitCastExpr>(E)) { |
| 3740 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3741 | return AnalyzeImplicitConversions(S, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 3744 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3745 | // Do a somewhat different check with comparison operators. |
| 3746 | if (BO->isComparisonOp()) |
| 3747 | return AnalyzeComparison(S, BO); |
| 3748 | |
| 3749 | // And with assignments and compound assignments. |
| 3750 | if (BO->isAssignmentOp()) |
| 3751 | return AnalyzeAssignment(S, BO); |
| 3752 | } |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3753 | |
| 3754 | // These break the otherwise-useful invariant below. Fortunately, |
| 3755 | // we don't really need to recurse into them, because any internal |
| 3756 | // expressions should have been analyzed already when they were |
| 3757 | // built into statements. |
| 3758 | if (isa<StmtExpr>(E)) return; |
| 3759 | |
| 3760 | // Don't descend into unevaluated contexts. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3761 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3762 | |
| 3763 | // Now just recurse over the expression's children. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3764 | CC = E->getExprLoc(); |
Richard Trieu | 021baa3 | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 3765 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); |
| 3766 | bool IsLogicalOperator = BO && BO->isLogicalOp(); |
| 3767 | for (Stmt::child_range I = E->children(); I; ++I) { |
| 3768 | Expr *ChildExpr = cast<Expr>(*I); |
| 3769 | if (IsLogicalOperator && |
| 3770 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) |
| 3771 | // Ignore checking string literals that are in logical operators. |
| 3772 | continue; |
| 3773 | AnalyzeImplicitConversions(S, ChildExpr, CC); |
| 3774 | } |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | } // end anonymous namespace |
| 3778 | |
| 3779 | /// Diagnoses "dangerous" implicit conversions within the given |
| 3780 | /// expression (which is a full expression). Implements -Wconversion |
| 3781 | /// and -Wsign-compare. |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3782 | /// |
| 3783 | /// \param CC the "context" location of the implicit conversion, i.e. |
| 3784 | /// the most location of the syntactic entity requiring the implicit |
| 3785 | /// conversion |
| 3786 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3787 | // Don't diagnose in unevaluated contexts. |
| 3788 | if (ExprEvalContexts.back().Context == Sema::Unevaluated) |
| 3789 | return; |
| 3790 | |
| 3791 | // Don't diagnose for value- or type-dependent expressions. |
| 3792 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3793 | return; |
| 3794 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3795 | // Check for array bounds violations in cases where the check isn't triggered |
| 3796 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an |
| 3797 | // ArraySubscriptExpr is on the RHS of a variable initialization. |
| 3798 | CheckArrayAccess(E); |
| 3799 | |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3800 | // This is not the right CC for (e.g.) a variable initialization. |
| 3801 | AnalyzeImplicitConversions(*this, E, CC); |
John McCall | cc7e5bf | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 3804 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, |
| 3805 | FieldDecl *BitField, |
| 3806 | Expr *Init) { |
| 3807 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); |
| 3808 | } |
| 3809 | |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3810 | /// CheckParmsForFunctionDef - Check that the parameters of the given |
| 3811 | /// function are appropriate for the definition of a function. This |
| 3812 | /// takes care of any checks that cannot be performed on the |
| 3813 | /// declaration itself, e.g., that the types of each of the function |
| 3814 | /// parameters are complete. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 3815 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, |
| 3816 | bool CheckParameterNames) { |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3817 | bool HasInvalidParm = false; |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 3818 | for (; P != PEnd; ++P) { |
| 3819 | ParmVarDecl *Param = *P; |
| 3820 | |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3821 | // C99 6.7.5.3p4: the parameters in a parameter type list in a |
| 3822 | // function declarator that is part of a function definition of |
| 3823 | // that function shall not have incomplete type. |
| 3824 | // |
| 3825 | // This is also C++ [dcl.fct]p6. |
| 3826 | if (!Param->isInvalidDecl() && |
| 3827 | RequireCompleteType(Param->getLocation(), Param->getType(), |
| 3828 | diag::err_typecheck_decl_incomplete_type)) { |
| 3829 | Param->setInvalidDecl(); |
| 3830 | HasInvalidParm = true; |
| 3831 | } |
| 3832 | |
| 3833 | // C99 6.9.1p5: If the declarator includes a parameter type list, the |
| 3834 | // declaration of each parameter shall include an identifier. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 3835 | if (CheckParameterNames && |
| 3836 | Param->getIdentifier() == 0 && |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3837 | !Param->isImplicit() && |
| 3838 | !getLangOptions().CPlusPlus) |
| 3839 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
Sam Weinig | deb55d5 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 3840 | |
| 3841 | // C99 6.7.5.3p12: |
| 3842 | // If the function declarator is not part of a definition of that |
| 3843 | // function, parameters may have incomplete type and may use the [*] |
| 3844 | // notation in their sequences of declarator specifiers to specify |
| 3845 | // variable length array types. |
| 3846 | QualType PType = Param->getOriginalType(); |
| 3847 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { |
| 3848 | if (AT->getSizeModifier() == ArrayType::Star) { |
| 3849 | // FIXME: This diagnosic should point the the '[*]' if source-location |
| 3850 | // information is added for it. |
| 3851 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); |
| 3852 | } |
| 3853 | } |
Mike Stump | 0c2ec77 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | return HasInvalidParm; |
| 3857 | } |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 3858 | |
| 3859 | /// CheckCastAlign - Implements -Wcast-align, which warns when a |
| 3860 | /// pointer cast increases the alignment requirements. |
| 3861 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { |
| 3862 | // This is actually a lot of work to potentially be doing on every |
| 3863 | // cast; don't do it if we're ignoring -Wcast_align (as is the default). |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3864 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, |
| 3865 | TRange.getBegin()) |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3866 | == DiagnosticsEngine::Ignored) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 3867 | return; |
| 3868 | |
| 3869 | // Ignore dependent types. |
| 3870 | if (T->isDependentType() || Op->getType()->isDependentType()) |
| 3871 | return; |
| 3872 | |
| 3873 | // Require that the destination be a pointer type. |
| 3874 | const PointerType *DestPtr = T->getAs<PointerType>(); |
| 3875 | if (!DestPtr) return; |
| 3876 | |
| 3877 | // If the destination has alignment 1, we're done. |
| 3878 | QualType DestPointee = DestPtr->getPointeeType(); |
| 3879 | if (DestPointee->isIncompleteType()) return; |
| 3880 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); |
| 3881 | if (DestAlign.isOne()) return; |
| 3882 | |
| 3883 | // Require that the source be a pointer type. |
| 3884 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); |
| 3885 | if (!SrcPtr) return; |
| 3886 | QualType SrcPointee = SrcPtr->getPointeeType(); |
| 3887 | |
| 3888 | // Whitelist casts from cv void*. We already implicitly |
| 3889 | // whitelisted casts to cv void*, since they have alignment 1. |
| 3890 | // Also whitelist casts involving incomplete types, which implicitly |
| 3891 | // includes 'void'. |
| 3892 | if (SrcPointee->isIncompleteType()) return; |
| 3893 | |
| 3894 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); |
| 3895 | if (SrcAlign >= DestAlign) return; |
| 3896 | |
| 3897 | Diag(TRange.getBegin(), diag::warn_cast_align) |
| 3898 | << Op->getType() << T |
| 3899 | << static_cast<unsigned>(SrcAlign.getQuantity()) |
| 3900 | << static_cast<unsigned>(DestAlign.getQuantity()) |
| 3901 | << TRange << Op->getSourceRange(); |
| 3902 | } |
| 3903 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3904 | static const Type* getElementType(const Expr *BaseExpr) { |
| 3905 | const Type* EltType = BaseExpr->getType().getTypePtr(); |
| 3906 | if (EltType->isAnyPointerType()) |
| 3907 | return EltType->getPointeeType().getTypePtr(); |
| 3908 | else if (EltType->isArrayType()) |
| 3909 | return EltType->getBaseElementTypeUnsafe(); |
| 3910 | return EltType; |
| 3911 | } |
| 3912 | |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 3913 | /// \brief Check whether this array fits the idiom of a size-one tail padded |
| 3914 | /// array member of a struct. |
| 3915 | /// |
| 3916 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are |
| 3917 | /// commonly used to emulate flexible arrays in C89 code. |
| 3918 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, |
| 3919 | const NamedDecl *ND) { |
| 3920 | if (Size != 1 || !ND) return false; |
| 3921 | |
| 3922 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); |
| 3923 | if (!FD) return false; |
| 3924 | |
| 3925 | // Don't consider sizes resulting from macro expansions or template argument |
| 3926 | // substitution to form C89 tail-padded arrays. |
| 3927 | ConstantArrayTypeLoc TL = |
| 3928 | cast<ConstantArrayTypeLoc>(FD->getTypeSourceInfo()->getTypeLoc()); |
| 3929 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(TL.getSizeExpr()); |
| 3930 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) |
| 3931 | return false; |
| 3932 | |
| 3933 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); |
| 3934 | if (!RD || !RD->isStruct()) |
| 3935 | return false; |
| 3936 | |
Benjamin Kramer | 8c54367 | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 3937 | // See if this is the last field decl in the record. |
| 3938 | const Decl *D = FD; |
| 3939 | while ((D = D->getNextDeclInContext())) |
| 3940 | if (isa<FieldDecl>(D)) |
| 3941 | return false; |
| 3942 | return true; |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3945 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, |
| 3946 | bool isSubscript, bool AllowOnePastEnd) { |
| 3947 | const Type* EffectiveType = getElementType(BaseExpr); |
| 3948 | BaseExpr = BaseExpr->IgnoreParenCasts(); |
| 3949 | IndexExpr = IndexExpr->IgnoreParenCasts(); |
| 3950 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3951 | const ConstantArrayType *ArrayTy = |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3952 | Context.getAsConstantArrayType(BaseExpr->getType()); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3953 | if (!ArrayTy) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 3954 | return; |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 3955 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3956 | if (IndexExpr->isValueDependent()) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 3957 | return; |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3958 | llvm::APSInt index; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3959 | if (!IndexExpr->isIntegerConstantExpr(index, Context)) |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 3960 | return; |
Ted Kremenek | 108b2d5 | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 3961 | |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 3962 | const NamedDecl *ND = NULL; |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 3963 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) |
| 3964 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); |
Chandler Carruth | 28389f0 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 3965 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 3966 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 3967 | |
Ted Kremenek | e4b316c | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 3968 | if (index.isUnsigned() || !index.isNegative()) { |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 3969 | llvm::APInt size = ArrayTy->getSize(); |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 3970 | if (!size.isStrictlyPositive()) |
| 3971 | return; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3972 | |
| 3973 | const Type* BaseType = getElementType(BaseExpr); |
Nico Weber | 7c29980 | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 3974 | if (BaseType != EffectiveType) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3975 | // Make sure we're comparing apples to apples when comparing index to size |
| 3976 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); |
| 3977 | uint64_t array_typesize = Context.getTypeSize(BaseType); |
Kaelyn Uhrain | 0fb0bb1 | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 3978 | // Handle ptrarith_typesize being zero, such as when casting to void* |
Kaelyn Uhrain | e535376 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 3979 | if (!ptrarith_typesize) ptrarith_typesize = 1; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3980 | if (ptrarith_typesize != array_typesize) { |
| 3981 | // There's a cast to a different size type involved |
| 3982 | uint64_t ratio = array_typesize / ptrarith_typesize; |
| 3983 | // TODO: Be smarter about handling cases where array_typesize is not a |
| 3984 | // multiple of ptrarith_typesize |
| 3985 | if (ptrarith_typesize * ratio == array_typesize) |
| 3986 | size *= llvm::APInt(size.getBitWidth(), ratio); |
| 3987 | } |
| 3988 | } |
| 3989 | |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 3990 | if (size.getBitWidth() > index.getBitWidth()) |
| 3991 | index = index.sext(size.getBitWidth()); |
Ted Kremenek | a7ced2c | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 3992 | else if (size.getBitWidth() < index.getBitWidth()) |
| 3993 | size = size.sext(index.getBitWidth()); |
| 3994 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3995 | // For array subscripting the index must be less than size, but for pointer |
| 3996 | // arithmetic also allow the index (offset) to be equal to size since |
| 3997 | // computing the next address after the end of the array is legal and |
| 3998 | // commonly done e.g. in C++ iterators and range-based for loops. |
| 3999 | if (AllowOnePastEnd ? index.sle(size) : index.slt(size)) |
Chandler Carruth | 126b155 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 4000 | return; |
| 4001 | |
| 4002 | // Also don't warn for arrays of size 1 which are members of some |
| 4003 | // structure. These are often used to approximate flexible arrays in C89 |
| 4004 | // code. |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4005 | if (IsTailPaddedMemberArray(*this, size, ND)) |
Ted Kremenek | 108b2d5 | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 4006 | return; |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4007 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4008 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; |
| 4009 | if (isSubscript) |
| 4010 | DiagID = diag::warn_array_index_exceeds_bounds; |
| 4011 | |
| 4012 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4013 | PDiag(DiagID) << index.toString(10, true) |
| 4014 | << size.toString(10, true) |
| 4015 | << (unsigned)size.getLimitedValue(~0U) |
| 4016 | << IndexExpr->getSourceRange()); |
Chandler Carruth | 2a666fc | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 4017 | } else { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4018 | unsigned DiagID = diag::warn_array_index_precedes_bounds; |
| 4019 | if (!isSubscript) { |
| 4020 | DiagID = diag::warn_ptr_arith_precedes_bounds; |
| 4021 | if (index.isNegative()) index = -index; |
| 4022 | } |
| 4023 | |
| 4024 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, |
| 4025 | PDiag(DiagID) << index.toString(10, true) |
| 4026 | << IndexExpr->getSourceRange()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4027 | } |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4028 | |
Chandler Carruth | 1af88f1 | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 4029 | if (ND) |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4030 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, |
| 4031 | PDiag(diag::note_array_index_out_of_bounds) |
| 4032 | << ND->getDeclName()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4035 | void Sema::CheckArrayAccess(const Expr *expr) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4036 | int AllowOnePastEnd = 0; |
| 4037 | while (expr) { |
| 4038 | expr = expr->IgnoreParenImpCasts(); |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4039 | switch (expr->getStmtClass()) { |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4040 | case Stmt::ArraySubscriptExprClass: { |
| 4041 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); |
| 4042 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), true, |
| 4043 | AllowOnePastEnd > 0); |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4044 | return; |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 4045 | } |
| 4046 | case Stmt::UnaryOperatorClass: { |
| 4047 | // Only unwrap the * and & unary operators |
| 4048 | const UnaryOperator *UO = cast<UnaryOperator>(expr); |
| 4049 | expr = UO->getSubExpr(); |
| 4050 | switch (UO->getOpcode()) { |
| 4051 | case UO_AddrOf: |
| 4052 | AllowOnePastEnd++; |
| 4053 | break; |
| 4054 | case UO_Deref: |
| 4055 | AllowOnePastEnd--; |
| 4056 | break; |
| 4057 | default: |
| 4058 | return; |
| 4059 | } |
| 4060 | break; |
| 4061 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4062 | case Stmt::ConditionalOperatorClass: { |
| 4063 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); |
| 4064 | if (const Expr *lhs = cond->getLHS()) |
| 4065 | CheckArrayAccess(lhs); |
| 4066 | if (const Expr *rhs = cond->getRHS()) |
| 4067 | CheckArrayAccess(rhs); |
| 4068 | return; |
| 4069 | } |
| 4070 | default: |
| 4071 | return; |
| 4072 | } |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 4073 | } |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 4074 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4075 | |
| 4076 | //===--- CHECK: Objective-C retain cycles ----------------------------------// |
| 4077 | |
| 4078 | namespace { |
| 4079 | struct RetainCycleOwner { |
| 4080 | RetainCycleOwner() : Variable(0), Indirect(false) {} |
| 4081 | VarDecl *Variable; |
| 4082 | SourceRange Range; |
| 4083 | SourceLocation Loc; |
| 4084 | bool Indirect; |
| 4085 | |
| 4086 | void setLocsFrom(Expr *e) { |
| 4087 | Loc = e->getExprLoc(); |
| 4088 | Range = e->getSourceRange(); |
| 4089 | } |
| 4090 | }; |
| 4091 | } |
| 4092 | |
| 4093 | /// Consider whether capturing the given variable can possibly lead to |
| 4094 | /// a retain cycle. |
| 4095 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { |
| 4096 | // In ARC, it's captured strongly iff the variable has __strong |
| 4097 | // lifetime. In MRR, it's captured strongly if the variable is |
| 4098 | // __block and has an appropriate type. |
| 4099 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 4100 | return false; |
| 4101 | |
| 4102 | owner.Variable = var; |
| 4103 | owner.setLocsFrom(ref); |
| 4104 | return true; |
| 4105 | } |
| 4106 | |
| 4107 | static bool findRetainCycleOwner(Expr *e, RetainCycleOwner &owner) { |
| 4108 | while (true) { |
| 4109 | e = e->IgnoreParens(); |
| 4110 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { |
| 4111 | switch (cast->getCastKind()) { |
| 4112 | case CK_BitCast: |
| 4113 | case CK_LValueBitCast: |
| 4114 | case CK_LValueToRValue: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4115 | case CK_ARCReclaimReturnedObject: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4116 | e = cast->getSubExpr(); |
| 4117 | continue; |
| 4118 | |
| 4119 | case CK_GetObjCProperty: { |
| 4120 | // Bail out if this isn't a strong explicit property. |
| 4121 | const ObjCPropertyRefExpr *pre = cast->getSubExpr()->getObjCProperty(); |
| 4122 | if (pre->isImplicitProperty()) return false; |
| 4123 | ObjCPropertyDecl *property = pre->getExplicitProperty(); |
John McCall | 4319286 | 2011-09-13 18:31:23 +0000 | [diff] [blame] | 4124 | if (!property->isRetaining() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4125 | !(property->getPropertyIvarDecl() && |
| 4126 | property->getPropertyIvarDecl()->getType() |
| 4127 | .getObjCLifetime() == Qualifiers::OCL_Strong)) |
| 4128 | return false; |
| 4129 | |
| 4130 | owner.Indirect = true; |
| 4131 | e = const_cast<Expr*>(pre->getBase()); |
| 4132 | continue; |
| 4133 | } |
| 4134 | |
| 4135 | default: |
| 4136 | return false; |
| 4137 | } |
| 4138 | } |
| 4139 | |
| 4140 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { |
| 4141 | ObjCIvarDecl *ivar = ref->getDecl(); |
| 4142 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) |
| 4143 | return false; |
| 4144 | |
| 4145 | // Try to find a retain cycle in the base. |
| 4146 | if (!findRetainCycleOwner(ref->getBase(), owner)) |
| 4147 | return false; |
| 4148 | |
| 4149 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); |
| 4150 | owner.Indirect = true; |
| 4151 | return true; |
| 4152 | } |
| 4153 | |
| 4154 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 4155 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); |
| 4156 | if (!var) return false; |
| 4157 | return considerVariable(var, ref, owner); |
| 4158 | } |
| 4159 | |
| 4160 | if (BlockDeclRefExpr *ref = dyn_cast<BlockDeclRefExpr>(e)) { |
| 4161 | owner.Variable = ref->getDecl(); |
| 4162 | owner.setLocsFrom(ref); |
| 4163 | return true; |
| 4164 | } |
| 4165 | |
| 4166 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { |
| 4167 | if (member->isArrow()) return false; |
| 4168 | |
| 4169 | // Don't count this as an indirect ownership. |
| 4170 | e = member->getBase(); |
| 4171 | continue; |
| 4172 | } |
| 4173 | |
| 4174 | // Array ivars? |
| 4175 | |
| 4176 | return false; |
| 4177 | } |
| 4178 | } |
| 4179 | |
| 4180 | namespace { |
| 4181 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { |
| 4182 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) |
| 4183 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), |
| 4184 | Variable(variable), Capturer(0) {} |
| 4185 | |
| 4186 | VarDecl *Variable; |
| 4187 | Expr *Capturer; |
| 4188 | |
| 4189 | void VisitDeclRefExpr(DeclRefExpr *ref) { |
| 4190 | if (ref->getDecl() == Variable && !Capturer) |
| 4191 | Capturer = ref; |
| 4192 | } |
| 4193 | |
| 4194 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *ref) { |
| 4195 | if (ref->getDecl() == Variable && !Capturer) |
| 4196 | Capturer = ref; |
| 4197 | } |
| 4198 | |
| 4199 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { |
| 4200 | if (Capturer) return; |
| 4201 | Visit(ref->getBase()); |
| 4202 | if (Capturer && ref->isFreeIvar()) |
| 4203 | Capturer = ref; |
| 4204 | } |
| 4205 | |
| 4206 | void VisitBlockExpr(BlockExpr *block) { |
| 4207 | // Look inside nested blocks |
| 4208 | if (block->getBlockDecl()->capturesVariable(Variable)) |
| 4209 | Visit(block->getBlockDecl()->getBody()); |
| 4210 | } |
| 4211 | }; |
| 4212 | } |
| 4213 | |
| 4214 | /// Check whether the given argument is a block which captures a |
| 4215 | /// variable. |
| 4216 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { |
| 4217 | assert(owner.Variable && owner.Loc.isValid()); |
| 4218 | |
| 4219 | e = e->IgnoreParenCasts(); |
| 4220 | BlockExpr *block = dyn_cast<BlockExpr>(e); |
| 4221 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) |
| 4222 | return 0; |
| 4223 | |
| 4224 | FindCaptureVisitor visitor(S.Context, owner.Variable); |
| 4225 | visitor.Visit(block->getBlockDecl()->getBody()); |
| 4226 | return visitor.Capturer; |
| 4227 | } |
| 4228 | |
| 4229 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, |
| 4230 | RetainCycleOwner &owner) { |
| 4231 | assert(capturer); |
| 4232 | assert(owner.Variable && owner.Loc.isValid()); |
| 4233 | |
| 4234 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) |
| 4235 | << owner.Variable << capturer->getSourceRange(); |
| 4236 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) |
| 4237 | << owner.Indirect << owner.Range; |
| 4238 | } |
| 4239 | |
| 4240 | /// Check for a keyword selector that starts with the word 'add' or |
| 4241 | /// 'set'. |
| 4242 | static bool isSetterLikeSelector(Selector sel) { |
| 4243 | if (sel.isUnarySelector()) return false; |
| 4244 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4245 | StringRef str = sel.getNameForSlot(0); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4246 | while (!str.empty() && str.front() == '_') str = str.substr(1); |
| 4247 | if (str.startswith("set") || str.startswith("add")) |
| 4248 | str = str.substr(3); |
| 4249 | else |
| 4250 | return false; |
| 4251 | |
| 4252 | if (str.empty()) return true; |
| 4253 | return !islower(str.front()); |
| 4254 | } |
| 4255 | |
| 4256 | /// Check a message send to see if it's likely to cause a retain cycle. |
| 4257 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { |
| 4258 | // Only check instance methods whose selector looks like a setter. |
| 4259 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) |
| 4260 | return; |
| 4261 | |
| 4262 | // Try to find a variable that the receiver is strongly owned by. |
| 4263 | RetainCycleOwner owner; |
| 4264 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { |
| 4265 | if (!findRetainCycleOwner(msg->getInstanceReceiver(), owner)) |
| 4266 | return; |
| 4267 | } else { |
| 4268 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); |
| 4269 | owner.Variable = getCurMethodDecl()->getSelfDecl(); |
| 4270 | owner.Loc = msg->getSuperLoc(); |
| 4271 | owner.Range = msg->getSuperLoc(); |
| 4272 | } |
| 4273 | |
| 4274 | // Check whether the receiver is captured by any of the arguments. |
| 4275 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) |
| 4276 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) |
| 4277 | return diagnoseRetainCycle(*this, capturer, owner); |
| 4278 | } |
| 4279 | |
| 4280 | /// Check a property assign to see if it's likely to cause a retain cycle. |
| 4281 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { |
| 4282 | RetainCycleOwner owner; |
| 4283 | if (!findRetainCycleOwner(receiver, owner)) |
| 4284 | return; |
| 4285 | |
| 4286 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) |
| 4287 | diagnoseRetainCycle(*this, capturer, owner); |
| 4288 | } |
| 4289 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4290 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4291 | QualType LHS, Expr *RHS) { |
| 4292 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); |
| 4293 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4294 | return false; |
| 4295 | // strip off any implicit cast added to get to the one arc-specific |
| 4296 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4297 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4298 | Diag(Loc, diag::warn_arc_retained_assign) |
| 4299 | << (LT == Qualifiers::OCL_ExplicitNone) |
| 4300 | << RHS->getSourceRange(); |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4301 | return true; |
| 4302 | } |
| 4303 | RHS = cast->getSubExpr(); |
| 4304 | } |
| 4305 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4308 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, |
| 4309 | Expr *LHS, Expr *RHS) { |
| 4310 | QualType LHSType = LHS->getType(); |
| 4311 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) |
| 4312 | return; |
| 4313 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); |
| 4314 | // FIXME. Check for other life times. |
| 4315 | if (LT != Qualifiers::OCL_None) |
| 4316 | return; |
| 4317 | |
| 4318 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(LHS)) { |
| 4319 | if (PRE->isImplicitProperty()) |
| 4320 | return; |
| 4321 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); |
| 4322 | if (!PD) |
| 4323 | return; |
| 4324 | |
| 4325 | unsigned Attributes = PD->getPropertyAttributes(); |
| 4326 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) |
| 4327 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4328 | if (cast->getCastKind() == CK_ARCConsumeObject) { |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 4329 | Diag(Loc, diag::warn_arc_retained_property_assign) |
| 4330 | << RHS->getSourceRange(); |
| 4331 | return; |
| 4332 | } |
| 4333 | RHS = cast->getSubExpr(); |
| 4334 | } |
| 4335 | } |
| 4336 | } |