| Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1 | //===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 0bc735f | 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 | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10 | //  This file implements extra semantic analysis beyond what is enforced | 
| Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 11 | //  by the C type system. | 
|  | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" | 
| Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" | 
| Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/CharUnits.h" | 
| John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" | 
| Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" | 
| Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" | 
|  | 21 | #include "clang/AST/EvaluatedExprVisitor.h" | 
| David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 22 | #include "clang/AST/Expr.h" | 
| Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" | 
| Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" | 
| Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtCXX.h" | 
|  | 26 | #include "clang/AST/StmtObjC.h" | 
| Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "clang/Analysis/Analyses/FormatString.h" | 
|  | 28 | #include "clang/Basic/ConvertUTF.h" | 
| Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetBuiltins.h" | 
| Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" | 
| Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" | 
|  | 32 | #include "clang/Sema/Initialization.h" | 
|  | 33 | #include "clang/Sema/Initialization.h" | 
|  | 34 | #include "clang/Sema/Lookup.h" | 
|  | 35 | #include "clang/Sema/ScopeInfo.h" | 
|  | 36 | #include "clang/Sema/Sema.h" | 
|  | 37 | #include "llvm/ADT/BitVector.h" | 
|  | 38 | #include "llvm/ADT/STLExtras.h" | 
|  | 39 | #include "llvm/ADT/SmallString.h" | 
|  | 40 | #include "llvm/Support/raw_ostream.h" | 
| Zhongxing Xu | a1f3dba | 2009-05-20 01:55:10 +0000 | [diff] [blame] | 41 | #include <limits> | 
| Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 42 | using namespace clang; | 
| John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 43 | using namespace sema; | 
| Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 44 |  | 
| Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 45 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, | 
|  | 46 | unsigned ByteNo) const { | 
| Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 47 | return SL->getLocationOfByte(ByteNo, PP.getSourceManager(), | 
| David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 48 | PP.getLangOpts(), PP.getTargetInfo()); | 
| Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
| John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 51 | /// Checks that a call expression's argument count is the desired number. | 
|  | 52 | /// This is useful when doing custom type-checking.  Returns true on error. | 
|  | 53 | static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) { | 
|  | 54 | unsigned argCount = call->getNumArgs(); | 
|  | 55 | if (argCount == desiredArgCount) return false; | 
|  | 56 |  | 
|  | 57 | if (argCount < desiredArgCount) | 
|  | 58 | return S.Diag(call->getLocEnd(), diag::err_typecheck_call_too_few_args) | 
|  | 59 | << 0 /*function call*/ << desiredArgCount << argCount | 
|  | 60 | << call->getSourceRange(); | 
|  | 61 |  | 
|  | 62 | // Highlight all the excess arguments. | 
|  | 63 | SourceRange range(call->getArg(desiredArgCount)->getLocStart(), | 
|  | 64 | call->getArg(argCount - 1)->getLocEnd()); | 
|  | 65 |  | 
|  | 66 | return S.Diag(range.getBegin(), diag::err_typecheck_call_too_many_args) | 
|  | 67 | << 0 /*function call*/ << desiredArgCount << argCount | 
|  | 68 | << call->getArg(1)->getSourceRange(); | 
|  | 69 | } | 
|  | 70 |  | 
| Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 71 | /// Check that the first argument to __builtin_annotation is an integer | 
|  | 72 | /// and the second argument is a non-wide string literal. | 
|  | 73 | static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) { | 
|  | 74 | if (checkArgCount(S, TheCall, 2)) | 
|  | 75 | return true; | 
|  | 76 |  | 
|  | 77 | // First argument should be an integer. | 
|  | 78 | Expr *ValArg = TheCall->getArg(0); | 
|  | 79 | QualType Ty = ValArg->getType(); | 
|  | 80 | if (!Ty->isIntegerType()) { | 
|  | 81 | S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg) | 
|  | 82 | << ValArg->getSourceRange(); | 
| Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 83 | return true; | 
|  | 84 | } | 
| Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 85 |  | 
|  | 86 | // Second argument should be a constant string. | 
|  | 87 | Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts(); | 
|  | 88 | StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg); | 
|  | 89 | if (!Literal || !Literal->isAscii()) { | 
|  | 90 | S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg) | 
|  | 91 | << StrArg->getSourceRange(); | 
|  | 92 | return true; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | TheCall->setType(Ty); | 
| Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 96 | return false; | 
|  | 97 | } | 
|  | 98 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 99 | ExprResult | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 100 | Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 101 | ExprResult TheCallResult(Owned(TheCall)); | 
| Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 102 |  | 
| Chris Lattner | 946928f | 2010-10-01 23:23:24 +0000 | [diff] [blame] | 103 | // Find out if any arguments are required to be integer constant expressions. | 
|  | 104 | unsigned ICEArguments = 0; | 
|  | 105 | ASTContext::GetBuiltinTypeError Error; | 
|  | 106 | Context.GetBuiltinType(BuiltinID, Error, &ICEArguments); | 
|  | 107 | if (Error != ASTContext::GE_None) | 
|  | 108 | ICEArguments = 0;  // Don't diagnose previously diagnosed errors. | 
|  | 109 |  | 
|  | 110 | // If any arguments are required to be ICE's, check and diagnose. | 
|  | 111 | for (unsigned ArgNo = 0; ICEArguments != 0; ++ArgNo) { | 
|  | 112 | // Skip arguments not required to be ICE's. | 
|  | 113 | if ((ICEArguments & (1 << ArgNo)) == 0) continue; | 
|  | 114 |  | 
|  | 115 | llvm::APSInt Result; | 
|  | 116 | if (SemaBuiltinConstantArg(TheCall, ArgNo, Result)) | 
|  | 117 | return true; | 
|  | 118 | ICEArguments &= ~(1 << ArgNo); | 
|  | 119 | } | 
|  | 120 |  | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 121 | switch (BuiltinID) { | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 122 | case Builtin::BI__builtin___CFStringMakeConstantString: | 
| Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 123 | assert(TheCall->getNumArgs() == 1 && | 
| Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 124 | "Wrong # arguments to builtin CFStringMakeConstantString"); | 
| Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 125 | if (CheckObjCString(TheCall->getArg(0))) | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 126 | return ExprError(); | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 127 | break; | 
| Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 128 | case Builtin::BI__builtin_stdarg_start: | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 129 | case Builtin::BI__builtin_va_start: | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 130 | if (SemaBuiltinVAStart(TheCall)) | 
|  | 131 | return ExprError(); | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 132 | break; | 
| Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 133 | case Builtin::BI__builtin_isgreater: | 
|  | 134 | case Builtin::BI__builtin_isgreaterequal: | 
|  | 135 | case Builtin::BI__builtin_isless: | 
|  | 136 | case Builtin::BI__builtin_islessequal: | 
|  | 137 | case Builtin::BI__builtin_islessgreater: | 
|  | 138 | case Builtin::BI__builtin_isunordered: | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 139 | if (SemaBuiltinUnorderedCompare(TheCall)) | 
|  | 140 | return ExprError(); | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 141 | break; | 
| Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 142 | case Builtin::BI__builtin_fpclassify: | 
|  | 143 | if (SemaBuiltinFPClassification(TheCall, 6)) | 
|  | 144 | return ExprError(); | 
|  | 145 | break; | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 146 | case Builtin::BI__builtin_isfinite: | 
|  | 147 | case Builtin::BI__builtin_isinf: | 
|  | 148 | case Builtin::BI__builtin_isinf_sign: | 
|  | 149 | case Builtin::BI__builtin_isnan: | 
|  | 150 | case Builtin::BI__builtin_isnormal: | 
| Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 151 | if (SemaBuiltinFPClassification(TheCall, 1)) | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 152 | return ExprError(); | 
|  | 153 | break; | 
| Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 154 | case Builtin::BI__builtin_shufflevector: | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 155 | return SemaBuiltinShuffleVector(TheCall); | 
|  | 156 | // TheCall will be freed by the smart pointer here, but that's fine, since | 
|  | 157 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 158 | case Builtin::BI__builtin_prefetch: | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 159 | if (SemaBuiltinPrefetch(TheCall)) | 
|  | 160 | return ExprError(); | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 161 | break; | 
| Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 162 | case Builtin::BI__builtin_object_size: | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 163 | if (SemaBuiltinObjectSize(TheCall)) | 
|  | 164 | return ExprError(); | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 165 | break; | 
| Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 166 | case Builtin::BI__builtin_longjmp: | 
|  | 167 | if (SemaBuiltinLongjmp(TheCall)) | 
|  | 168 | return ExprError(); | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 169 | break; | 
| John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 170 |  | 
|  | 171 | case Builtin::BI__builtin_classify_type: | 
|  | 172 | if (checkArgCount(*this, TheCall, 1)) return true; | 
|  | 173 | TheCall->setType(Context.IntTy); | 
|  | 174 | break; | 
| Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 175 | case Builtin::BI__builtin_constant_p: | 
| John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 176 | if (checkArgCount(*this, TheCall, 1)) return true; | 
|  | 177 | TheCall->setType(Context.IntTy); | 
| Chris Lattner | 75c29a0 | 2010-10-12 17:47:42 +0000 | [diff] [blame] | 178 | break; | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 179 | case Builtin::BI__sync_fetch_and_add: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 180 | case Builtin::BI__sync_fetch_and_add_1: | 
|  | 181 | case Builtin::BI__sync_fetch_and_add_2: | 
|  | 182 | case Builtin::BI__sync_fetch_and_add_4: | 
|  | 183 | case Builtin::BI__sync_fetch_and_add_8: | 
|  | 184 | case Builtin::BI__sync_fetch_and_add_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 185 | case Builtin::BI__sync_fetch_and_sub: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 186 | case Builtin::BI__sync_fetch_and_sub_1: | 
|  | 187 | case Builtin::BI__sync_fetch_and_sub_2: | 
|  | 188 | case Builtin::BI__sync_fetch_and_sub_4: | 
|  | 189 | case Builtin::BI__sync_fetch_and_sub_8: | 
|  | 190 | case Builtin::BI__sync_fetch_and_sub_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 191 | case Builtin::BI__sync_fetch_and_or: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 192 | case Builtin::BI__sync_fetch_and_or_1: | 
|  | 193 | case Builtin::BI__sync_fetch_and_or_2: | 
|  | 194 | case Builtin::BI__sync_fetch_and_or_4: | 
|  | 195 | case Builtin::BI__sync_fetch_and_or_8: | 
|  | 196 | case Builtin::BI__sync_fetch_and_or_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 197 | case Builtin::BI__sync_fetch_and_and: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 198 | case Builtin::BI__sync_fetch_and_and_1: | 
|  | 199 | case Builtin::BI__sync_fetch_and_and_2: | 
|  | 200 | case Builtin::BI__sync_fetch_and_and_4: | 
|  | 201 | case Builtin::BI__sync_fetch_and_and_8: | 
|  | 202 | case Builtin::BI__sync_fetch_and_and_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 203 | case Builtin::BI__sync_fetch_and_xor: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 204 | case Builtin::BI__sync_fetch_and_xor_1: | 
|  | 205 | case Builtin::BI__sync_fetch_and_xor_2: | 
|  | 206 | case Builtin::BI__sync_fetch_and_xor_4: | 
|  | 207 | case Builtin::BI__sync_fetch_and_xor_8: | 
|  | 208 | case Builtin::BI__sync_fetch_and_xor_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 209 | case Builtin::BI__sync_add_and_fetch: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 210 | case Builtin::BI__sync_add_and_fetch_1: | 
|  | 211 | case Builtin::BI__sync_add_and_fetch_2: | 
|  | 212 | case Builtin::BI__sync_add_and_fetch_4: | 
|  | 213 | case Builtin::BI__sync_add_and_fetch_8: | 
|  | 214 | case Builtin::BI__sync_add_and_fetch_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 215 | case Builtin::BI__sync_sub_and_fetch: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 216 | case Builtin::BI__sync_sub_and_fetch_1: | 
|  | 217 | case Builtin::BI__sync_sub_and_fetch_2: | 
|  | 218 | case Builtin::BI__sync_sub_and_fetch_4: | 
|  | 219 | case Builtin::BI__sync_sub_and_fetch_8: | 
|  | 220 | case Builtin::BI__sync_sub_and_fetch_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 221 | case Builtin::BI__sync_and_and_fetch: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 222 | case Builtin::BI__sync_and_and_fetch_1: | 
|  | 223 | case Builtin::BI__sync_and_and_fetch_2: | 
|  | 224 | case Builtin::BI__sync_and_and_fetch_4: | 
|  | 225 | case Builtin::BI__sync_and_and_fetch_8: | 
|  | 226 | case Builtin::BI__sync_and_and_fetch_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 227 | case Builtin::BI__sync_or_and_fetch: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 228 | case Builtin::BI__sync_or_and_fetch_1: | 
|  | 229 | case Builtin::BI__sync_or_and_fetch_2: | 
|  | 230 | case Builtin::BI__sync_or_and_fetch_4: | 
|  | 231 | case Builtin::BI__sync_or_and_fetch_8: | 
|  | 232 | case Builtin::BI__sync_or_and_fetch_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 233 | case Builtin::BI__sync_xor_and_fetch: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 234 | case Builtin::BI__sync_xor_and_fetch_1: | 
|  | 235 | case Builtin::BI__sync_xor_and_fetch_2: | 
|  | 236 | case Builtin::BI__sync_xor_and_fetch_4: | 
|  | 237 | case Builtin::BI__sync_xor_and_fetch_8: | 
|  | 238 | case Builtin::BI__sync_xor_and_fetch_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 239 | case Builtin::BI__sync_val_compare_and_swap: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 240 | case Builtin::BI__sync_val_compare_and_swap_1: | 
|  | 241 | case Builtin::BI__sync_val_compare_and_swap_2: | 
|  | 242 | case Builtin::BI__sync_val_compare_and_swap_4: | 
|  | 243 | case Builtin::BI__sync_val_compare_and_swap_8: | 
|  | 244 | case Builtin::BI__sync_val_compare_and_swap_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 245 | case Builtin::BI__sync_bool_compare_and_swap: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 246 | case Builtin::BI__sync_bool_compare_and_swap_1: | 
|  | 247 | case Builtin::BI__sync_bool_compare_and_swap_2: | 
|  | 248 | case Builtin::BI__sync_bool_compare_and_swap_4: | 
|  | 249 | case Builtin::BI__sync_bool_compare_and_swap_8: | 
|  | 250 | case Builtin::BI__sync_bool_compare_and_swap_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 251 | case Builtin::BI__sync_lock_test_and_set: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 252 | case Builtin::BI__sync_lock_test_and_set_1: | 
|  | 253 | case Builtin::BI__sync_lock_test_and_set_2: | 
|  | 254 | case Builtin::BI__sync_lock_test_and_set_4: | 
|  | 255 | case Builtin::BI__sync_lock_test_and_set_8: | 
|  | 256 | case Builtin::BI__sync_lock_test_and_set_16: | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 257 | case Builtin::BI__sync_lock_release: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 258 | case Builtin::BI__sync_lock_release_1: | 
|  | 259 | case Builtin::BI__sync_lock_release_2: | 
|  | 260 | case Builtin::BI__sync_lock_release_4: | 
|  | 261 | case Builtin::BI__sync_lock_release_8: | 
|  | 262 | case Builtin::BI__sync_lock_release_16: | 
| Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 263 | case Builtin::BI__sync_swap: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 264 | case Builtin::BI__sync_swap_1: | 
|  | 265 | case Builtin::BI__sync_swap_2: | 
|  | 266 | case Builtin::BI__sync_swap_4: | 
|  | 267 | case Builtin::BI__sync_swap_8: | 
|  | 268 | case Builtin::BI__sync_swap_16: | 
| Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 269 | return SemaBuiltinAtomicOverloaded(TheCallResult); | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 270 | #define BUILTIN(ID, TYPE, ATTRS) | 
|  | 271 | #define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \ | 
|  | 272 | case Builtin::BI##ID: \ | 
| Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 273 | return SemaAtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID); | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 274 | #include "clang/Basic/Builtins.def" | 
| Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 275 | case Builtin::BI__builtin_annotation: | 
| Julien Lerouge | e593921 | 2012-04-28 17:39:16 +0000 | [diff] [blame] | 276 | if (SemaBuiltinAnnotation(*this, TheCall)) | 
| Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 277 | return ExprError(); | 
|  | 278 | break; | 
| Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 279 | } | 
|  | 280 |  | 
|  | 281 | // Since the target specific builtins for each arch overlap, only check those | 
|  | 282 | // of the arch we are compiling for. | 
|  | 283 | if (BuiltinID >= Builtin::FirstTSBuiltin) { | 
| Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 284 | switch (Context.getTargetInfo().getTriple().getArch()) { | 
| Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 285 | case llvm::Triple::arm: | 
|  | 286 | case llvm::Triple::thumb: | 
|  | 287 | if (CheckARMBuiltinFunctionCall(BuiltinID, TheCall)) | 
|  | 288 | return ExprError(); | 
|  | 289 | break; | 
| Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 290 | case llvm::Triple::mips: | 
|  | 291 | case llvm::Triple::mipsel: | 
|  | 292 | case llvm::Triple::mips64: | 
|  | 293 | case llvm::Triple::mips64el: | 
|  | 294 | if (CheckMipsBuiltinFunctionCall(BuiltinID, TheCall)) | 
|  | 295 | return ExprError(); | 
|  | 296 | break; | 
| Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 297 | default: | 
|  | 298 | break; | 
|  | 299 | } | 
|  | 300 | } | 
|  | 301 |  | 
| Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 302 | return TheCallResult; | 
| Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 305 | // Get the valid immediate range for the specified NEON type code. | 
|  | 306 | static unsigned RFT(unsigned t, bool shift = false) { | 
| Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 307 | NeonTypeFlags Type(t); | 
|  | 308 | int IsQuad = Type.isQuad(); | 
|  | 309 | switch (Type.getEltType()) { | 
|  | 310 | case NeonTypeFlags::Int8: | 
|  | 311 | case NeonTypeFlags::Poly8: | 
|  | 312 | return shift ? 7 : (8 << IsQuad) - 1; | 
|  | 313 | case NeonTypeFlags::Int16: | 
|  | 314 | case NeonTypeFlags::Poly16: | 
|  | 315 | return shift ? 15 : (4 << IsQuad) - 1; | 
|  | 316 | case NeonTypeFlags::Int32: | 
|  | 317 | return shift ? 31 : (2 << IsQuad) - 1; | 
|  | 318 | case NeonTypeFlags::Int64: | 
|  | 319 | return shift ? 63 : (1 << IsQuad) - 1; | 
|  | 320 | case NeonTypeFlags::Float16: | 
|  | 321 | assert(!shift && "cannot shift float types!"); | 
|  | 322 | return (4 << IsQuad) - 1; | 
|  | 323 | case NeonTypeFlags::Float32: | 
|  | 324 | assert(!shift && "cannot shift float types!"); | 
|  | 325 | return (2 << IsQuad) - 1; | 
| Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 326 | } | 
| David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 327 | llvm_unreachable("Invalid NeonTypeFlag!"); | 
| Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 328 | } | 
|  | 329 |  | 
| Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 330 | /// getNeonEltType - Return the QualType corresponding to the elements of | 
|  | 331 | /// the vector type specified by the NeonTypeFlags.  This is used to check | 
|  | 332 | /// the pointer arguments for Neon load/store intrinsics. | 
|  | 333 | static QualType getNeonEltType(NeonTypeFlags Flags, ASTContext &Context) { | 
|  | 334 | switch (Flags.getEltType()) { | 
|  | 335 | case NeonTypeFlags::Int8: | 
|  | 336 | return Flags.isUnsigned() ? Context.UnsignedCharTy : Context.SignedCharTy; | 
|  | 337 | case NeonTypeFlags::Int16: | 
|  | 338 | return Flags.isUnsigned() ? Context.UnsignedShortTy : Context.ShortTy; | 
|  | 339 | case NeonTypeFlags::Int32: | 
|  | 340 | return Flags.isUnsigned() ? Context.UnsignedIntTy : Context.IntTy; | 
|  | 341 | case NeonTypeFlags::Int64: | 
|  | 342 | return Flags.isUnsigned() ? Context.UnsignedLongLongTy : Context.LongLongTy; | 
|  | 343 | case NeonTypeFlags::Poly8: | 
|  | 344 | return Context.SignedCharTy; | 
|  | 345 | case NeonTypeFlags::Poly16: | 
|  | 346 | return Context.ShortTy; | 
|  | 347 | case NeonTypeFlags::Float16: | 
|  | 348 | return Context.UnsignedShortTy; | 
|  | 349 | case NeonTypeFlags::Float32: | 
|  | 350 | return Context.FloatTy; | 
|  | 351 | } | 
| David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 352 | llvm_unreachable("Invalid NeonTypeFlag!"); | 
| Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 353 | } | 
|  | 354 |  | 
| Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 355 | bool Sema::CheckARMBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { | 
| Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 356 | llvm::APSInt Result; | 
|  | 357 |  | 
| Richard Smith | f8ee6bc | 2012-08-14 01:28:02 +0000 | [diff] [blame] | 358 | uint64_t mask = 0; | 
| Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 359 | unsigned TV = 0; | 
| Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 360 | int PtrArgNum = -1; | 
| Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 361 | bool HasConstPtr = false; | 
| Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 362 | switch (BuiltinID) { | 
| Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 363 | #define GET_NEON_OVERLOAD_CHECK | 
|  | 364 | #include "clang/Basic/arm_neon.inc" | 
|  | 365 | #undef GET_NEON_OVERLOAD_CHECK | 
| Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 368 | // For NEON intrinsics which are overloaded on vector element type, validate | 
|  | 369 | // the immediate which specifies which variant to emit. | 
| Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 370 | unsigned ImmArg = TheCall->getNumArgs()-1; | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 371 | if (mask) { | 
| Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 372 | if (SemaBuiltinConstantArg(TheCall, ImmArg, Result)) | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 373 | return true; | 
|  | 374 |  | 
| Bob Wilson | da95f73 | 2011-11-08 01:16:11 +0000 | [diff] [blame] | 375 | TV = Result.getLimitedValue(64); | 
| Richard Smith | f8ee6bc | 2012-08-14 01:28:02 +0000 | [diff] [blame] | 376 | if ((TV > 63) || (mask & (1ULL << TV)) == 0) | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 377 | return Diag(TheCall->getLocStart(), diag::err_invalid_neon_type_code) | 
| Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 378 | << TheCall->getArg(ImmArg)->getSourceRange(); | 
|  | 379 | } | 
|  | 380 |  | 
| Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 381 | if (PtrArgNum >= 0) { | 
| Bob Wilson | 6f9f03e | 2011-11-08 05:04:11 +0000 | [diff] [blame] | 382 | // Check that pointer arguments have the specified type. | 
| Bob Wilson | 4648255 | 2011-11-16 21:32:23 +0000 | [diff] [blame] | 383 | Expr *Arg = TheCall->getArg(PtrArgNum); | 
|  | 384 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) | 
|  | 385 | Arg = ICE->getSubExpr(); | 
|  | 386 | ExprResult RHS = DefaultFunctionArrayLvalueConversion(Arg); | 
|  | 387 | QualType RHSTy = RHS.get()->getType(); | 
|  | 388 | QualType EltTy = getNeonEltType(NeonTypeFlags(TV), Context); | 
|  | 389 | if (HasConstPtr) | 
|  | 390 | EltTy = EltTy.withConst(); | 
|  | 391 | QualType LHSTy = Context.getPointerType(EltTy); | 
|  | 392 | AssignConvertType ConvTy; | 
|  | 393 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); | 
|  | 394 | if (RHS.isInvalid()) | 
|  | 395 | return true; | 
|  | 396 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), LHSTy, RHSTy, | 
|  | 397 | RHS.get(), AA_Assigning)) | 
|  | 398 | return true; | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 399 | } | 
| Nate Begeman | 1c2a88c | 2010-06-09 01:10:23 +0000 | [diff] [blame] | 400 |  | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 401 | // For NEON intrinsics which take an immediate value as part of the | 
|  | 402 | // instruction, range check them here. | 
| Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 403 | unsigned i = 0, l = 0, u = 0; | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 404 | switch (BuiltinID) { | 
|  | 405 | default: return false; | 
| Nate Begeman | bb37f50 | 2010-07-29 22:48:34 +0000 | [diff] [blame] | 406 | case ARM::BI__builtin_arm_ssat: i = 1; l = 1; u = 31; break; | 
|  | 407 | case ARM::BI__builtin_arm_usat: i = 1; u = 31; break; | 
| Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 408 | case ARM::BI__builtin_arm_vcvtr_f: | 
|  | 409 | case ARM::BI__builtin_arm_vcvtr_d: i = 1; u = 1; break; | 
| Nate Begeman | a23326b | 2010-06-17 04:17:01 +0000 | [diff] [blame] | 410 | #define GET_NEON_IMMEDIATE_CHECK | 
|  | 411 | #include "clang/Basic/arm_neon.inc" | 
|  | 412 | #undef GET_NEON_IMMEDIATE_CHECK | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 413 | }; | 
|  | 414 |  | 
| Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 415 | // We can't check the value of a dependent argument. | 
|  | 416 | if (TheCall->getArg(i)->isTypeDependent() || | 
|  | 417 | TheCall->getArg(i)->isValueDependent()) | 
|  | 418 | return false; | 
|  | 419 |  | 
| Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 420 | // Check that the immediate argument is actually a constant. | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 421 | if (SemaBuiltinConstantArg(TheCall, i, Result)) | 
|  | 422 | return true; | 
|  | 423 |  | 
| Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 424 | // Range check against the upper/lower values for this isntruction. | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 425 | unsigned Val = Result.getZExtValue(); | 
| Nate Begeman | 61eecf5 | 2010-06-14 05:21:25 +0000 | [diff] [blame] | 426 | if (Val < l || Val > (u + l)) | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 427 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) | 
| Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 428 | << l << u+l << TheCall->getArg(i)->getSourceRange(); | 
| Nate Begeman | 0d15c53 | 2010-06-13 04:47:52 +0000 | [diff] [blame] | 429 |  | 
| Nate Begeman | 99c40bb | 2010-08-03 21:32:34 +0000 | [diff] [blame] | 430 | // FIXME: VFP Intrinsics should error if VFP not present. | 
| Nate Begeman | 26a3142 | 2010-06-08 02:47:44 +0000 | [diff] [blame] | 431 | return false; | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 432 | } | 
| Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 433 |  | 
| Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 434 | bool Sema::CheckMipsBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { | 
|  | 435 | unsigned i = 0, l = 0, u = 0; | 
|  | 436 | switch (BuiltinID) { | 
|  | 437 | default: return false; | 
|  | 438 | case Mips::BI__builtin_mips_wrdsp: i = 1; l = 0; u = 63; break; | 
|  | 439 | case Mips::BI__builtin_mips_rddsp: i = 0; l = 0; u = 63; break; | 
| Simon Atanasyan | be22cb8 | 2012-08-27 12:29:20 +0000 | [diff] [blame] | 440 | case Mips::BI__builtin_mips_append: i = 2; l = 0; u = 31; break; | 
|  | 441 | case Mips::BI__builtin_mips_balign: i = 2; l = 0; u = 3; break; | 
|  | 442 | case Mips::BI__builtin_mips_precr_sra_ph_w: i = 2; l = 0; u = 31; break; | 
|  | 443 | case Mips::BI__builtin_mips_precr_sra_r_ph_w: i = 2; l = 0; u = 31; break; | 
|  | 444 | case Mips::BI__builtin_mips_prepend: i = 2; l = 0; u = 31; break; | 
| Simon Atanasyan | fad0a32 | 2012-07-08 09:30:00 +0000 | [diff] [blame] | 445 | }; | 
|  | 446 |  | 
|  | 447 | // We can't check the value of a dependent argument. | 
|  | 448 | if (TheCall->getArg(i)->isTypeDependent() || | 
|  | 449 | TheCall->getArg(i)->isValueDependent()) | 
|  | 450 | return false; | 
|  | 451 |  | 
|  | 452 | // Check that the immediate argument is actually a constant. | 
|  | 453 | llvm::APSInt Result; | 
|  | 454 | if (SemaBuiltinConstantArg(TheCall, i, Result)) | 
|  | 455 | return true; | 
|  | 456 |  | 
|  | 457 | // Range check against the upper/lower values for this instruction. | 
|  | 458 | unsigned Val = Result.getZExtValue(); | 
|  | 459 | if (Val < l || Val > u) | 
|  | 460 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) | 
|  | 461 | << l << u << TheCall->getArg(i)->getSourceRange(); | 
|  | 462 |  | 
|  | 463 | return false; | 
|  | 464 | } | 
|  | 465 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 466 | /// Given a FunctionDecl's FormatAttr, attempts to populate the FomatStringInfo | 
|  | 467 | /// parameter with the FormatAttr's correct format_idx and firstDataArg. | 
|  | 468 | /// Returns true when the format fits the function and the FormatStringInfo has | 
|  | 469 | /// been populated. | 
|  | 470 | bool Sema::getFormatStringInfo(const FormatAttr *Format, bool IsCXXMember, | 
|  | 471 | FormatStringInfo *FSI) { | 
|  | 472 | FSI->HasVAListArg = Format->getFirstArg() == 0; | 
|  | 473 | FSI->FormatIdx = Format->getFormatIdx() - 1; | 
|  | 474 | FSI->FirstDataArg = FSI->HasVAListArg ? 0 : Format->getFirstArg() - 1; | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 475 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 476 | // The way the format attribute works in GCC, the implicit this argument | 
|  | 477 | // of member functions is counted. However, it doesn't appear in our own | 
|  | 478 | // lists, so decrement format_idx in that case. | 
|  | 479 | if (IsCXXMember) { | 
|  | 480 | if(FSI->FormatIdx == 0) | 
|  | 481 | return false; | 
|  | 482 | --FSI->FormatIdx; | 
|  | 483 | if (FSI->FirstDataArg != 0) | 
|  | 484 | --FSI->FirstDataArg; | 
|  | 485 | } | 
|  | 486 | return true; | 
|  | 487 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 489 | /// Handles the checks for format strings, non-POD arguments to vararg | 
|  | 490 | /// functions, and NULL arguments passed to non-NULL parameters. | 
|  | 491 | void Sema::checkCall(NamedDecl *FDecl, Expr **Args, | 
|  | 492 | unsigned NumArgs, | 
|  | 493 | unsigned NumProtoArgs, | 
|  | 494 | bool IsMemberFunction, | 
|  | 495 | SourceLocation Loc, | 
|  | 496 | SourceRange Range, | 
|  | 497 | VariadicCallType CallType) { | 
| Jordan Rose | 66360e2 | 2012-10-02 01:49:54 +0000 | [diff] [blame] | 498 | if (CurContext->isDependentContext()) | 
|  | 499 | return; | 
| Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 500 |  | 
| Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 501 | // Printf and scanf checking. | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 502 | bool HandledFormatString = false; | 
| Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 503 | for (specific_attr_iterator<FormatAttr> | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 504 | I = FDecl->specific_attr_begin<FormatAttr>(), | 
|  | 505 | E = FDecl->specific_attr_end<FormatAttr>(); I != E ; ++I) | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 506 | if (CheckFormatArguments(*I, Args, NumArgs, IsMemberFunction, CallType, | 
|  | 507 | Loc, Range)) | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 508 | HandledFormatString = true; | 
|  | 509 |  | 
|  | 510 | // Refuse POD arguments that weren't caught by the format string | 
|  | 511 | // checks above. | 
|  | 512 | if (!HandledFormatString && CallType != VariadicDoesNotApply) | 
| Ted Kremenek | 0234bfa | 2012-10-11 19:06:43 +0000 | [diff] [blame] | 513 | for (unsigned ArgIdx = NumProtoArgs; ArgIdx < NumArgs; ++ArgIdx) { | 
|  | 514 | // Args[ArgIdx] can be null in malformed code. | 
|  | 515 | if (Expr *Arg = Args[ArgIdx]) | 
|  | 516 | variadicArgumentPODCheck(Arg, CallType); | 
|  | 517 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 |  | 
| Ted Kremenek | c82faca | 2010-09-09 04:33:05 +0000 | [diff] [blame] | 519 | for (specific_attr_iterator<NonNullAttr> | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 520 | I = FDecl->specific_attr_begin<NonNullAttr>(), | 
|  | 521 | E = FDecl->specific_attr_end<NonNullAttr>(); I != E; ++I) | 
|  | 522 | CheckNonNullArguments(*I, Args, Loc); | 
| Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 523 |  | 
|  | 524 | // Type safety checking. | 
|  | 525 | for (specific_attr_iterator<ArgumentWithTypeTagAttr> | 
|  | 526 | i = FDecl->specific_attr_begin<ArgumentWithTypeTagAttr>(), | 
|  | 527 | e = FDecl->specific_attr_end<ArgumentWithTypeTagAttr>(); i != e; ++i) { | 
|  | 528 | CheckArgumentWithTypeTag(*i, Args); | 
|  | 529 | } | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
|  | 532 | /// CheckConstructorCall - Check a constructor call for correctness and safety | 
|  | 533 | /// properties not enforced by the C type system. | 
|  | 534 | void Sema::CheckConstructorCall(FunctionDecl *FDecl, Expr **Args, | 
|  | 535 | unsigned NumArgs, | 
|  | 536 | const FunctionProtoType *Proto, | 
|  | 537 | SourceLocation Loc) { | 
|  | 538 | VariadicCallType CallType = | 
|  | 539 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; | 
|  | 540 | checkCall(FDecl, Args, NumArgs, Proto->getNumArgs(), | 
|  | 541 | /*IsMemberFunction=*/true, Loc, SourceRange(), CallType); | 
|  | 542 | } | 
|  | 543 |  | 
|  | 544 | /// CheckFunctionCall - Check a direct function call for various correctness | 
|  | 545 | /// and safety properties not strictly enforced by the C type system. | 
|  | 546 | bool Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall, | 
|  | 547 | const FunctionProtoType *Proto) { | 
| Eli Friedman | 2edcde8 | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 548 | bool IsMemberOperatorCall = isa<CXXOperatorCallExpr>(TheCall) && | 
|  | 549 | isa<CXXMethodDecl>(FDecl); | 
|  | 550 | bool IsMemberFunction = isa<CXXMemberCallExpr>(TheCall) || | 
|  | 551 | IsMemberOperatorCall; | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 552 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, | 
|  | 553 | TheCall->getCallee()); | 
|  | 554 | unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0; | 
| Eli Friedman | 2edcde8 | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 555 | Expr** Args = TheCall->getArgs(); | 
|  | 556 | unsigned NumArgs = TheCall->getNumArgs(); | 
| Eli Friedman | df75b0c | 2012-10-11 00:34:15 +0000 | [diff] [blame] | 557 | if (IsMemberOperatorCall) { | 
| Eli Friedman | 2edcde8 | 2012-10-11 00:30:58 +0000 | [diff] [blame] | 558 | // If this is a call to a member operator, hide the first argument | 
|  | 559 | // from checkCall. | 
|  | 560 | // FIXME: Our choice of AST representation here is less than ideal. | 
|  | 561 | ++Args; | 
|  | 562 | --NumArgs; | 
|  | 563 | } | 
|  | 564 | checkCall(FDecl, Args, NumArgs, NumProtoArgs, | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 565 | IsMemberFunction, TheCall->getRParenLoc(), | 
|  | 566 | TheCall->getCallee()->getSourceRange(), CallType); | 
|  | 567 |  | 
|  | 568 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); | 
|  | 569 | // None of the checks below are needed for functions that don't have | 
|  | 570 | // simple names (e.g., C++ conversion functions). | 
|  | 571 | if (!FnInfo) | 
|  | 572 | return false; | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 573 |  | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 574 | unsigned CMId = FDecl->getMemoryFunctionKind(); | 
|  | 575 | if (CMId == 0) | 
| Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 576 | return false; | 
| Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 577 |  | 
| Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 578 | // Handle memory setting and copying functions. | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 579 | if (CMId == Builtin::BIstrlcpy || CMId == Builtin::BIstrlcat) | 
| Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 580 | CheckStrlcpycatArguments(TheCall, FnInfo); | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 581 | else if (CMId == Builtin::BIstrncat) | 
|  | 582 | CheckStrncatArguments(TheCall, FnInfo); | 
| Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 583 | else | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 584 | CheckMemaccessArguments(TheCall, CMId, FnInfo); | 
| Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 585 |  | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 586 | return false; | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 587 | } | 
|  | 588 |  | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 589 | bool Sema::CheckObjCMethodCall(ObjCMethodDecl *Method, SourceLocation lbrac, | 
|  | 590 | Expr **Args, unsigned NumArgs) { | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 591 | VariadicCallType CallType = | 
|  | 592 | Method->isVariadic() ? VariadicMethod : VariadicDoesNotApply; | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 593 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 594 | checkCall(Method, Args, NumArgs, Method->param_size(), | 
|  | 595 | /*IsMemberFunction=*/false, | 
|  | 596 | lbrac, Method->getSourceRange(), CallType); | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 597 |  | 
|  | 598 | return false; | 
|  | 599 | } | 
|  | 600 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 601 | bool Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall, | 
|  | 602 | const FunctionProtoType *Proto) { | 
| Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 603 | const VarDecl *V = dyn_cast<VarDecl>(NDecl); | 
|  | 604 | if (!V) | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 605 | return false; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 |  | 
| Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 607 | QualType Ty = V->getType(); | 
|  | 608 | if (!Ty->isBlockPointerType()) | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 609 | return false; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 611 | VariadicCallType CallType = | 
|  | 612 | Proto && Proto->isVariadic() ? VariadicBlock : VariadicDoesNotApply ; | 
|  | 613 | unsigned NumProtoArgs = Proto ? Proto->getNumArgs() : 0; | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 614 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 615 | checkCall(NDecl, TheCall->getArgs(), TheCall->getNumArgs(), | 
|  | 616 | NumProtoArgs, /*IsMemberFunction=*/false, | 
|  | 617 | TheCall->getRParenLoc(), | 
|  | 618 | TheCall->getCallee()->getSourceRange(), CallType); | 
|  | 619 |  | 
| Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 620 | return false; | 
| Fariborz Jahanian | 725165f | 2009-05-18 21:05:18 +0000 | [diff] [blame] | 621 | } | 
|  | 622 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 623 | ExprResult Sema::SemaAtomicOpsOverloaded(ExprResult TheCallResult, | 
|  | 624 | AtomicExpr::AtomicOp Op) { | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 625 | CallExpr *TheCall = cast<CallExpr>(TheCallResult.get()); | 
|  | 626 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 627 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 628 | // All these operations take one of the following forms: | 
|  | 629 | enum { | 
|  | 630 | // C    __c11_atomic_init(A *, C) | 
|  | 631 | Init, | 
|  | 632 | // C    __c11_atomic_load(A *, int) | 
|  | 633 | Load, | 
|  | 634 | // void __atomic_load(A *, CP, int) | 
|  | 635 | Copy, | 
|  | 636 | // C    __c11_atomic_add(A *, M, int) | 
|  | 637 | Arithmetic, | 
|  | 638 | // C    __atomic_exchange_n(A *, CP, int) | 
|  | 639 | Xchg, | 
|  | 640 | // void __atomic_exchange(A *, C *, CP, int) | 
|  | 641 | GNUXchg, | 
|  | 642 | // bool __c11_atomic_compare_exchange_strong(A *, C *, CP, int, int) | 
|  | 643 | C11CmpXchg, | 
|  | 644 | // bool __atomic_compare_exchange(A *, C *, CP, bool, int, int) | 
|  | 645 | GNUCmpXchg | 
|  | 646 | } Form = Init; | 
|  | 647 | const unsigned NumArgs[] = { 2, 2, 3, 3, 3, 4, 5, 6 }; | 
|  | 648 | const unsigned NumVals[] = { 1, 0, 1, 1, 1, 2, 2, 3 }; | 
|  | 649 | // where: | 
|  | 650 | //   C is an appropriate type, | 
|  | 651 | //   A is volatile _Atomic(C) for __c11 builtins and is C for GNU builtins, | 
|  | 652 | //   CP is C for __c11 builtins and GNU _n builtins and is C * otherwise, | 
|  | 653 | //   M is C if C is an integer, and ptrdiff_t if C is a pointer, and | 
|  | 654 | //   the int parameters are for orderings. | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 655 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 656 | assert(AtomicExpr::AO__c11_atomic_init == 0 && | 
|  | 657 | AtomicExpr::AO__c11_atomic_fetch_xor + 1 == AtomicExpr::AO__atomic_load | 
|  | 658 | && "need to update code for modified C11 atomics"); | 
|  | 659 | bool IsC11 = Op >= AtomicExpr::AO__c11_atomic_init && | 
|  | 660 | Op <= AtomicExpr::AO__c11_atomic_fetch_xor; | 
|  | 661 | bool IsN = Op == AtomicExpr::AO__atomic_load_n || | 
|  | 662 | Op == AtomicExpr::AO__atomic_store_n || | 
|  | 663 | Op == AtomicExpr::AO__atomic_exchange_n || | 
|  | 664 | Op == AtomicExpr::AO__atomic_compare_exchange_n; | 
|  | 665 | bool IsAddSub = false; | 
|  | 666 |  | 
|  | 667 | switch (Op) { | 
|  | 668 | case AtomicExpr::AO__c11_atomic_init: | 
|  | 669 | Form = Init; | 
|  | 670 | break; | 
|  | 671 |  | 
|  | 672 | case AtomicExpr::AO__c11_atomic_load: | 
|  | 673 | case AtomicExpr::AO__atomic_load_n: | 
|  | 674 | Form = Load; | 
|  | 675 | break; | 
|  | 676 |  | 
|  | 677 | case AtomicExpr::AO__c11_atomic_store: | 
|  | 678 | case AtomicExpr::AO__atomic_load: | 
|  | 679 | case AtomicExpr::AO__atomic_store: | 
|  | 680 | case AtomicExpr::AO__atomic_store_n: | 
|  | 681 | Form = Copy; | 
|  | 682 | break; | 
|  | 683 |  | 
|  | 684 | case AtomicExpr::AO__c11_atomic_fetch_add: | 
|  | 685 | case AtomicExpr::AO__c11_atomic_fetch_sub: | 
|  | 686 | case AtomicExpr::AO__atomic_fetch_add: | 
|  | 687 | case AtomicExpr::AO__atomic_fetch_sub: | 
|  | 688 | case AtomicExpr::AO__atomic_add_fetch: | 
|  | 689 | case AtomicExpr::AO__atomic_sub_fetch: | 
|  | 690 | IsAddSub = true; | 
|  | 691 | // Fall through. | 
|  | 692 | case AtomicExpr::AO__c11_atomic_fetch_and: | 
|  | 693 | case AtomicExpr::AO__c11_atomic_fetch_or: | 
|  | 694 | case AtomicExpr::AO__c11_atomic_fetch_xor: | 
|  | 695 | case AtomicExpr::AO__atomic_fetch_and: | 
|  | 696 | case AtomicExpr::AO__atomic_fetch_or: | 
|  | 697 | case AtomicExpr::AO__atomic_fetch_xor: | 
| Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 698 | case AtomicExpr::AO__atomic_fetch_nand: | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 699 | case AtomicExpr::AO__atomic_and_fetch: | 
|  | 700 | case AtomicExpr::AO__atomic_or_fetch: | 
|  | 701 | case AtomicExpr::AO__atomic_xor_fetch: | 
| Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 702 | case AtomicExpr::AO__atomic_nand_fetch: | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 703 | Form = Arithmetic; | 
|  | 704 | break; | 
|  | 705 |  | 
|  | 706 | case AtomicExpr::AO__c11_atomic_exchange: | 
|  | 707 | case AtomicExpr::AO__atomic_exchange_n: | 
|  | 708 | Form = Xchg; | 
|  | 709 | break; | 
|  | 710 |  | 
|  | 711 | case AtomicExpr::AO__atomic_exchange: | 
|  | 712 | Form = GNUXchg; | 
|  | 713 | break; | 
|  | 714 |  | 
|  | 715 | case AtomicExpr::AO__c11_atomic_compare_exchange_strong: | 
|  | 716 | case AtomicExpr::AO__c11_atomic_compare_exchange_weak: | 
|  | 717 | Form = C11CmpXchg; | 
|  | 718 | break; | 
|  | 719 |  | 
|  | 720 | case AtomicExpr::AO__atomic_compare_exchange: | 
|  | 721 | case AtomicExpr::AO__atomic_compare_exchange_n: | 
|  | 722 | Form = GNUCmpXchg; | 
|  | 723 | break; | 
|  | 724 | } | 
|  | 725 |  | 
|  | 726 | // Check we have the right number of arguments. | 
|  | 727 | if (TheCall->getNumArgs() < NumArgs[Form]) { | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 728 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 729 | << 0 << NumArgs[Form] << TheCall->getNumArgs() | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 730 | << TheCall->getCallee()->getSourceRange(); | 
|  | 731 | return ExprError(); | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 732 | } else if (TheCall->getNumArgs() > NumArgs[Form]) { | 
|  | 733 | Diag(TheCall->getArg(NumArgs[Form])->getLocStart(), | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 734 | diag::err_typecheck_call_too_many_args) | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 735 | << 0 << NumArgs[Form] << TheCall->getNumArgs() | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 736 | << TheCall->getCallee()->getSourceRange(); | 
|  | 737 | return ExprError(); | 
|  | 738 | } | 
|  | 739 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 740 | // Inspect the first argument of the atomic operation. | 
| Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 741 | Expr *Ptr = TheCall->getArg(0); | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 742 | Ptr = DefaultFunctionArrayLvalueConversion(Ptr).get(); | 
|  | 743 | const PointerType *pointerType = Ptr->getType()->getAs<PointerType>(); | 
|  | 744 | if (!pointerType) { | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 745 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 746 | << Ptr->getType() << Ptr->getSourceRange(); | 
|  | 747 | return ExprError(); | 
|  | 748 | } | 
|  | 749 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 750 | // For a __c11 builtin, this should be a pointer to an _Atomic type. | 
|  | 751 | QualType AtomTy = pointerType->getPointeeType(); // 'A' | 
|  | 752 | QualType ValType = AtomTy; // 'C' | 
|  | 753 | if (IsC11) { | 
|  | 754 | if (!AtomTy->isAtomicType()) { | 
|  | 755 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic) | 
|  | 756 | << Ptr->getType() << Ptr->getSourceRange(); | 
|  | 757 | return ExprError(); | 
|  | 758 | } | 
| Richard Smith | bc57b10 | 2012-09-15 06:09:58 +0000 | [diff] [blame] | 759 | if (AtomTy.isConstQualified()) { | 
|  | 760 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic) | 
|  | 761 | << Ptr->getType() << Ptr->getSourceRange(); | 
|  | 762 | return ExprError(); | 
|  | 763 | } | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 764 | ValType = AtomTy->getAs<AtomicType>()->getValueType(); | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 765 | } | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 766 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 767 | // For an arithmetic operation, the implied arithmetic must be well-formed. | 
|  | 768 | if (Form == Arithmetic) { | 
|  | 769 | // gcc does not enforce these rules for GNU atomics, but we do so for sanity. | 
|  | 770 | if (IsAddSub && !ValType->isIntegerType() && !ValType->isPointerType()) { | 
|  | 771 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) | 
|  | 772 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); | 
|  | 773 | return ExprError(); | 
|  | 774 | } | 
|  | 775 | if (!IsAddSub && !ValType->isIntegerType()) { | 
|  | 776 | Diag(DRE->getLocStart(), diag::err_atomic_op_bitwise_needs_atomic_int) | 
|  | 777 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); | 
|  | 778 | return ExprError(); | 
|  | 779 | } | 
|  | 780 | } else if (IsN && !ValType->isIntegerType() && !ValType->isPointerType()) { | 
|  | 781 | // For __atomic_*_n operations, the value type must be a scalar integral or | 
|  | 782 | // pointer type which is 1, 2, 4, 8 or 16 bytes in length. | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 783 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_atomic_int_or_ptr) | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 784 | << IsC11 << Ptr->getType() << Ptr->getSourceRange(); | 
|  | 785 | return ExprError(); | 
|  | 786 | } | 
|  | 787 |  | 
|  | 788 | if (!IsC11 && !AtomTy.isTriviallyCopyableType(Context)) { | 
|  | 789 | // For GNU atomics, require a trivially-copyable type. This is not part of | 
|  | 790 | // the GNU atomics specification, but we enforce it for sanity. | 
|  | 791 | Diag(DRE->getLocStart(), diag::err_atomic_op_needs_trivial_copy) | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 792 | << Ptr->getType() << Ptr->getSourceRange(); | 
|  | 793 | return ExprError(); | 
|  | 794 | } | 
|  | 795 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 796 | // FIXME: For any builtin other than a load, the ValType must not be | 
|  | 797 | // const-qualified. | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 798 |  | 
|  | 799 | switch (ValType.getObjCLifetime()) { | 
|  | 800 | case Qualifiers::OCL_None: | 
|  | 801 | case Qualifiers::OCL_ExplicitNone: | 
|  | 802 | // okay | 
|  | 803 | break; | 
|  | 804 |  | 
|  | 805 | case Qualifiers::OCL_Weak: | 
|  | 806 | case Qualifiers::OCL_Strong: | 
|  | 807 | case Qualifiers::OCL_Autoreleasing: | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 808 | // FIXME: Can this happen? By this point, ValType should be known | 
|  | 809 | // to be trivially copyable. | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 810 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) | 
|  | 811 | << ValType << Ptr->getSourceRange(); | 
|  | 812 | return ExprError(); | 
|  | 813 | } | 
|  | 814 |  | 
|  | 815 | QualType ResultType = ValType; | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 816 | if (Form == Copy || Form == GNUXchg || Form == Init) | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 817 | ResultType = Context.VoidTy; | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 818 | else if (Form == C11CmpXchg || Form == GNUCmpXchg) | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 819 | ResultType = Context.BoolTy; | 
|  | 820 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 821 | // The type of a parameter passed 'by value'. In the GNU atomics, such | 
|  | 822 | // arguments are actually passed as pointers. | 
|  | 823 | QualType ByValType = ValType; // 'CP' | 
|  | 824 | if (!IsC11 && !IsN) | 
|  | 825 | ByValType = Ptr->getType(); | 
|  | 826 |  | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 827 | // The first argument --- the pointer --- has a fixed type; we | 
|  | 828 | // deduce the types of the rest of the arguments accordingly.  Walk | 
|  | 829 | // the remaining arguments, converting them to the deduced value type. | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 830 | for (unsigned i = 1; i != NumArgs[Form]; ++i) { | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 831 | QualType Ty; | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 832 | if (i < NumVals[Form] + 1) { | 
|  | 833 | switch (i) { | 
|  | 834 | case 1: | 
|  | 835 | // The second argument is the non-atomic operand. For arithmetic, this | 
|  | 836 | // is always passed by value, and for a compare_exchange it is always | 
|  | 837 | // passed by address. For the rest, GNU uses by-address and C11 uses | 
|  | 838 | // by-value. | 
|  | 839 | assert(Form != Load); | 
|  | 840 | if (Form == Init || (Form == Arithmetic && ValType->isIntegerType())) | 
|  | 841 | Ty = ValType; | 
|  | 842 | else if (Form == Copy || Form == Xchg) | 
|  | 843 | Ty = ByValType; | 
|  | 844 | else if (Form == Arithmetic) | 
|  | 845 | Ty = Context.getPointerDiffType(); | 
|  | 846 | else | 
|  | 847 | Ty = Context.getPointerType(ValType.getUnqualifiedType()); | 
|  | 848 | break; | 
|  | 849 | case 2: | 
|  | 850 | // The third argument to compare_exchange / GNU exchange is a | 
|  | 851 | // (pointer to a) desired value. | 
|  | 852 | Ty = ByValType; | 
|  | 853 | break; | 
|  | 854 | case 3: | 
|  | 855 | // The fourth argument to GNU compare_exchange is a 'weak' flag. | 
|  | 856 | Ty = Context.BoolTy; | 
|  | 857 | break; | 
|  | 858 | } | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 859 | } else { | 
|  | 860 | // The order(s) are always converted to int. | 
|  | 861 | Ty = Context.IntTy; | 
|  | 862 | } | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 863 |  | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 864 | InitializedEntity Entity = | 
|  | 865 | InitializedEntity::InitializeParameter(Context, Ty, false); | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 866 | ExprResult Arg = TheCall->getArg(i); | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 867 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); | 
|  | 868 | if (Arg.isInvalid()) | 
|  | 869 | return true; | 
|  | 870 | TheCall->setArg(i, Arg.get()); | 
|  | 871 | } | 
|  | 872 |  | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 873 | // Permute the arguments into a 'consistent' order. | 
| Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 874 | SmallVector<Expr*, 5> SubExprs; | 
|  | 875 | SubExprs.push_back(Ptr); | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 876 | switch (Form) { | 
|  | 877 | case Init: | 
|  | 878 | // Note, AtomicExpr::getVal1() has a special case for this atomic. | 
| David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 879 | SubExprs.push_back(TheCall->getArg(1)); // Val1 | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 880 | break; | 
|  | 881 | case Load: | 
|  | 882 | SubExprs.push_back(TheCall->getArg(1)); // Order | 
|  | 883 | break; | 
|  | 884 | case Copy: | 
|  | 885 | case Arithmetic: | 
|  | 886 | case Xchg: | 
| Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 887 | SubExprs.push_back(TheCall->getArg(2)); // Order | 
|  | 888 | SubExprs.push_back(TheCall->getArg(1)); // Val1 | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 889 | break; | 
|  | 890 | case GNUXchg: | 
|  | 891 | // Note, AtomicExpr::getVal2() has a special case for this atomic. | 
|  | 892 | SubExprs.push_back(TheCall->getArg(3)); // Order | 
|  | 893 | SubExprs.push_back(TheCall->getArg(1)); // Val1 | 
|  | 894 | SubExprs.push_back(TheCall->getArg(2)); // Val2 | 
|  | 895 | break; | 
|  | 896 | case C11CmpXchg: | 
| Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 897 | SubExprs.push_back(TheCall->getArg(3)); // Order | 
|  | 898 | SubExprs.push_back(TheCall->getArg(1)); // Val1 | 
| Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 899 | SubExprs.push_back(TheCall->getArg(4)); // OrderFail | 
| David Chisnall | 2ebb98a | 2012-03-29 17:58:59 +0000 | [diff] [blame] | 900 | SubExprs.push_back(TheCall->getArg(2)); // Val2 | 
| Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 901 | break; | 
|  | 902 | case GNUCmpXchg: | 
|  | 903 | SubExprs.push_back(TheCall->getArg(4)); // Order | 
|  | 904 | SubExprs.push_back(TheCall->getArg(1)); // Val1 | 
|  | 905 | SubExprs.push_back(TheCall->getArg(5)); // OrderFail | 
|  | 906 | SubExprs.push_back(TheCall->getArg(2)); // Val2 | 
|  | 907 | SubExprs.push_back(TheCall->getArg(3)); // Weak | 
|  | 908 | break; | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 909 | } | 
| Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 910 |  | 
|  | 911 | return Owned(new (Context) AtomicExpr(TheCall->getCallee()->getLocStart(), | 
| Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 912 | SubExprs, ResultType, Op, | 
| Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 913 | TheCall->getRParenLoc())); | 
| Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 914 | } | 
|  | 915 |  | 
|  | 916 |  | 
| John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 917 | /// checkBuiltinArgument - Given a call to a builtin function, perform | 
|  | 918 | /// normal type-checking on the given argument, updating the call in | 
|  | 919 | /// place.  This is useful when a builtin function requires custom | 
|  | 920 | /// type-checking for some of its arguments but not necessarily all of | 
|  | 921 | /// them. | 
|  | 922 | /// | 
|  | 923 | /// Returns true on error. | 
|  | 924 | static bool checkBuiltinArgument(Sema &S, CallExpr *E, unsigned ArgIndex) { | 
|  | 925 | FunctionDecl *Fn = E->getDirectCallee(); | 
|  | 926 | assert(Fn && "builtin call without direct callee!"); | 
|  | 927 |  | 
|  | 928 | ParmVarDecl *Param = Fn->getParamDecl(ArgIndex); | 
|  | 929 | InitializedEntity Entity = | 
|  | 930 | InitializedEntity::InitializeParameter(S.Context, Param); | 
|  | 931 |  | 
|  | 932 | ExprResult Arg = E->getArg(0); | 
|  | 933 | Arg = S.PerformCopyInitialization(Entity, SourceLocation(), Arg); | 
|  | 934 | if (Arg.isInvalid()) | 
|  | 935 | return true; | 
|  | 936 |  | 
|  | 937 | E->setArg(ArgIndex, Arg.take()); | 
|  | 938 | return false; | 
|  | 939 | } | 
|  | 940 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 941 | /// SemaBuiltinAtomicOverloaded - We have a call to a function like | 
|  | 942 | /// __sync_fetch_and_add, which is an overloaded function based on the pointer | 
|  | 943 | /// type of its first argument.  The main ActOnCallExpr routines have already | 
|  | 944 | /// promoted the types of arguments because all of these calls are prototyped as | 
|  | 945 | /// void(...). | 
|  | 946 | /// | 
|  | 947 | /// This function goes through and does final semantic checking for these | 
|  | 948 | /// builtins, | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 949 | ExprResult | 
|  | 950 | Sema::SemaBuiltinAtomicOverloaded(ExprResult TheCallResult) { | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 951 | CallExpr *TheCall = (CallExpr *)TheCallResult.get(); | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 952 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); | 
|  | 953 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); | 
|  | 954 |  | 
|  | 955 | // Ensure that we have at least one argument to do type inference from. | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 956 | if (TheCall->getNumArgs() < 1) { | 
|  | 957 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) | 
|  | 958 | << 0 << 1 << TheCall->getNumArgs() | 
|  | 959 | << TheCall->getCallee()->getSourceRange(); | 
|  | 960 | return ExprError(); | 
|  | 961 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 963 | // Inspect the first argument of the atomic builtin.  This should always be | 
|  | 964 | // a pointer type, whose element is an integral scalar or pointer type. | 
|  | 965 | // Because it is a pointer type, we don't have to worry about any implicit | 
|  | 966 | // casts here. | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 967 | // FIXME: We don't allow floating point scalars as input. | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 968 | Expr *FirstArg = TheCall->getArg(0); | 
| Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 969 | ExprResult FirstArgResult = DefaultFunctionArrayLvalueConversion(FirstArg); | 
|  | 970 | if (FirstArgResult.isInvalid()) | 
|  | 971 | return ExprError(); | 
|  | 972 | FirstArg = FirstArgResult.take(); | 
|  | 973 | TheCall->setArg(0, FirstArg); | 
|  | 974 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 975 | const PointerType *pointerType = FirstArg->getType()->getAs<PointerType>(); | 
|  | 976 | if (!pointerType) { | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 977 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer) | 
|  | 978 | << FirstArg->getType() << FirstArg->getSourceRange(); | 
|  | 979 | return ExprError(); | 
|  | 980 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 982 | QualType ValType = pointerType->getPointeeType(); | 
| Chris Lattner | dd5fa7a | 2010-09-17 21:12:38 +0000 | [diff] [blame] | 983 | if (!ValType->isIntegerType() && !ValType->isAnyPointerType() && | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 984 | !ValType->isBlockPointerType()) { | 
|  | 985 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_must_be_pointer_intptr) | 
|  | 986 | << FirstArg->getType() << FirstArg->getSourceRange(); | 
|  | 987 | return ExprError(); | 
|  | 988 | } | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 989 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 990 | switch (ValType.getObjCLifetime()) { | 
|  | 991 | case Qualifiers::OCL_None: | 
|  | 992 | case Qualifiers::OCL_ExplicitNone: | 
|  | 993 | // okay | 
|  | 994 | break; | 
|  | 995 |  | 
|  | 996 | case Qualifiers::OCL_Weak: | 
|  | 997 | case Qualifiers::OCL_Strong: | 
|  | 998 | case Qualifiers::OCL_Autoreleasing: | 
| Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 999 | Diag(DRE->getLocStart(), diag::err_arc_atomic_ownership) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1000 | << ValType << FirstArg->getSourceRange(); | 
|  | 1001 | return ExprError(); | 
|  | 1002 | } | 
|  | 1003 |  | 
| John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1004 | // Strip any qualifiers off ValType. | 
|  | 1005 | ValType = ValType.getUnqualifiedType(); | 
|  | 1006 |  | 
| Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1007 | // The majority of builtins return a value, but a few have special return | 
|  | 1008 | // types, so allow them to override appropriately below. | 
|  | 1009 | QualType ResultType = ValType; | 
|  | 1010 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1011 | // We need to figure out which concrete builtin this maps onto.  For example, | 
|  | 1012 | // __sync_fetch_and_add with a 2 byte object turns into | 
|  | 1013 | // __sync_fetch_and_add_2. | 
|  | 1014 | #define BUILTIN_ROW(x) \ | 
|  | 1015 | { Builtin::BI##x##_1, Builtin::BI##x##_2, Builtin::BI##x##_4, \ | 
|  | 1016 | Builtin::BI##x##_8, Builtin::BI##x##_16 } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1018 | static const unsigned BuiltinIndices[][5] = { | 
|  | 1019 | BUILTIN_ROW(__sync_fetch_and_add), | 
|  | 1020 | BUILTIN_ROW(__sync_fetch_and_sub), | 
|  | 1021 | BUILTIN_ROW(__sync_fetch_and_or), | 
|  | 1022 | BUILTIN_ROW(__sync_fetch_and_and), | 
|  | 1023 | BUILTIN_ROW(__sync_fetch_and_xor), | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1025 | BUILTIN_ROW(__sync_add_and_fetch), | 
|  | 1026 | BUILTIN_ROW(__sync_sub_and_fetch), | 
|  | 1027 | BUILTIN_ROW(__sync_and_and_fetch), | 
|  | 1028 | BUILTIN_ROW(__sync_or_and_fetch), | 
|  | 1029 | BUILTIN_ROW(__sync_xor_and_fetch), | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1031 | BUILTIN_ROW(__sync_val_compare_and_swap), | 
|  | 1032 | BUILTIN_ROW(__sync_bool_compare_and_swap), | 
|  | 1033 | BUILTIN_ROW(__sync_lock_test_and_set), | 
| Chris Lattner | 23aa9c8 | 2011-04-09 03:57:26 +0000 | [diff] [blame] | 1034 | BUILTIN_ROW(__sync_lock_release), | 
|  | 1035 | BUILTIN_ROW(__sync_swap) | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1036 | }; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | #undef BUILTIN_ROW | 
|  | 1038 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1039 | // Determine the index of the size. | 
|  | 1040 | unsigned SizeIndex; | 
| Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1041 | switch (Context.getTypeSizeInChars(ValType).getQuantity()) { | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1042 | case 1: SizeIndex = 0; break; | 
|  | 1043 | case 2: SizeIndex = 1; break; | 
|  | 1044 | case 4: SizeIndex = 2; break; | 
|  | 1045 | case 8: SizeIndex = 3; break; | 
|  | 1046 | case 16: SizeIndex = 4; break; | 
|  | 1047 | default: | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1048 | Diag(DRE->getLocStart(), diag::err_atomic_builtin_pointer_size) | 
|  | 1049 | << FirstArg->getType() << FirstArg->getSourceRange(); | 
|  | 1050 | return ExprError(); | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1051 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1053 | // Each of these builtins has one pointer argument, followed by some number of | 
|  | 1054 | // values (0, 1 or 2) followed by a potentially empty varags list of stuff | 
|  | 1055 | // that we ignore.  Find out which row of BuiltinIndices to read from as well | 
|  | 1056 | // as the number of fixed args. | 
| Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1057 | unsigned BuiltinID = FDecl->getBuiltinID(); | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1058 | unsigned BuiltinIndex, NumFixed = 1; | 
|  | 1059 | switch (BuiltinID) { | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1060 | default: llvm_unreachable("Unknown overloaded atomic builtin!"); | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1061 | case Builtin::BI__sync_fetch_and_add: | 
|  | 1062 | case Builtin::BI__sync_fetch_and_add_1: | 
|  | 1063 | case Builtin::BI__sync_fetch_and_add_2: | 
|  | 1064 | case Builtin::BI__sync_fetch_and_add_4: | 
|  | 1065 | case Builtin::BI__sync_fetch_and_add_8: | 
|  | 1066 | case Builtin::BI__sync_fetch_and_add_16: | 
|  | 1067 | BuiltinIndex = 0; | 
|  | 1068 | break; | 
|  | 1069 |  | 
|  | 1070 | case Builtin::BI__sync_fetch_and_sub: | 
|  | 1071 | case Builtin::BI__sync_fetch_and_sub_1: | 
|  | 1072 | case Builtin::BI__sync_fetch_and_sub_2: | 
|  | 1073 | case Builtin::BI__sync_fetch_and_sub_4: | 
|  | 1074 | case Builtin::BI__sync_fetch_and_sub_8: | 
|  | 1075 | case Builtin::BI__sync_fetch_and_sub_16: | 
|  | 1076 | BuiltinIndex = 1; | 
|  | 1077 | break; | 
|  | 1078 |  | 
|  | 1079 | case Builtin::BI__sync_fetch_and_or: | 
|  | 1080 | case Builtin::BI__sync_fetch_and_or_1: | 
|  | 1081 | case Builtin::BI__sync_fetch_and_or_2: | 
|  | 1082 | case Builtin::BI__sync_fetch_and_or_4: | 
|  | 1083 | case Builtin::BI__sync_fetch_and_or_8: | 
|  | 1084 | case Builtin::BI__sync_fetch_and_or_16: | 
|  | 1085 | BuiltinIndex = 2; | 
|  | 1086 | break; | 
|  | 1087 |  | 
|  | 1088 | case Builtin::BI__sync_fetch_and_and: | 
|  | 1089 | case Builtin::BI__sync_fetch_and_and_1: | 
|  | 1090 | case Builtin::BI__sync_fetch_and_and_2: | 
|  | 1091 | case Builtin::BI__sync_fetch_and_and_4: | 
|  | 1092 | case Builtin::BI__sync_fetch_and_and_8: | 
|  | 1093 | case Builtin::BI__sync_fetch_and_and_16: | 
|  | 1094 | BuiltinIndex = 3; | 
|  | 1095 | break; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 |  | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1097 | case Builtin::BI__sync_fetch_and_xor: | 
|  | 1098 | case Builtin::BI__sync_fetch_and_xor_1: | 
|  | 1099 | case Builtin::BI__sync_fetch_and_xor_2: | 
|  | 1100 | case Builtin::BI__sync_fetch_and_xor_4: | 
|  | 1101 | case Builtin::BI__sync_fetch_and_xor_8: | 
|  | 1102 | case Builtin::BI__sync_fetch_and_xor_16: | 
|  | 1103 | BuiltinIndex = 4; | 
|  | 1104 | break; | 
|  | 1105 |  | 
|  | 1106 | case Builtin::BI__sync_add_and_fetch: | 
|  | 1107 | case Builtin::BI__sync_add_and_fetch_1: | 
|  | 1108 | case Builtin::BI__sync_add_and_fetch_2: | 
|  | 1109 | case Builtin::BI__sync_add_and_fetch_4: | 
|  | 1110 | case Builtin::BI__sync_add_and_fetch_8: | 
|  | 1111 | case Builtin::BI__sync_add_and_fetch_16: | 
|  | 1112 | BuiltinIndex = 5; | 
|  | 1113 | break; | 
|  | 1114 |  | 
|  | 1115 | case Builtin::BI__sync_sub_and_fetch: | 
|  | 1116 | case Builtin::BI__sync_sub_and_fetch_1: | 
|  | 1117 | case Builtin::BI__sync_sub_and_fetch_2: | 
|  | 1118 | case Builtin::BI__sync_sub_and_fetch_4: | 
|  | 1119 | case Builtin::BI__sync_sub_and_fetch_8: | 
|  | 1120 | case Builtin::BI__sync_sub_and_fetch_16: | 
|  | 1121 | BuiltinIndex = 6; | 
|  | 1122 | break; | 
|  | 1123 |  | 
|  | 1124 | case Builtin::BI__sync_and_and_fetch: | 
|  | 1125 | case Builtin::BI__sync_and_and_fetch_1: | 
|  | 1126 | case Builtin::BI__sync_and_and_fetch_2: | 
|  | 1127 | case Builtin::BI__sync_and_and_fetch_4: | 
|  | 1128 | case Builtin::BI__sync_and_and_fetch_8: | 
|  | 1129 | case Builtin::BI__sync_and_and_fetch_16: | 
|  | 1130 | BuiltinIndex = 7; | 
|  | 1131 | break; | 
|  | 1132 |  | 
|  | 1133 | case Builtin::BI__sync_or_and_fetch: | 
|  | 1134 | case Builtin::BI__sync_or_and_fetch_1: | 
|  | 1135 | case Builtin::BI__sync_or_and_fetch_2: | 
|  | 1136 | case Builtin::BI__sync_or_and_fetch_4: | 
|  | 1137 | case Builtin::BI__sync_or_and_fetch_8: | 
|  | 1138 | case Builtin::BI__sync_or_and_fetch_16: | 
|  | 1139 | BuiltinIndex = 8; | 
|  | 1140 | break; | 
|  | 1141 |  | 
|  | 1142 | case Builtin::BI__sync_xor_and_fetch: | 
|  | 1143 | case Builtin::BI__sync_xor_and_fetch_1: | 
|  | 1144 | case Builtin::BI__sync_xor_and_fetch_2: | 
|  | 1145 | case Builtin::BI__sync_xor_and_fetch_4: | 
|  | 1146 | case Builtin::BI__sync_xor_and_fetch_8: | 
|  | 1147 | case Builtin::BI__sync_xor_and_fetch_16: | 
|  | 1148 | BuiltinIndex = 9; | 
|  | 1149 | break; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1151 | case Builtin::BI__sync_val_compare_and_swap: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1152 | case Builtin::BI__sync_val_compare_and_swap_1: | 
|  | 1153 | case Builtin::BI__sync_val_compare_and_swap_2: | 
|  | 1154 | case Builtin::BI__sync_val_compare_and_swap_4: | 
|  | 1155 | case Builtin::BI__sync_val_compare_and_swap_8: | 
|  | 1156 | case Builtin::BI__sync_val_compare_and_swap_16: | 
| Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1157 | BuiltinIndex = 10; | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1158 | NumFixed = 2; | 
|  | 1159 | break; | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1160 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1161 | case Builtin::BI__sync_bool_compare_and_swap: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1162 | case Builtin::BI__sync_bool_compare_and_swap_1: | 
|  | 1163 | case Builtin::BI__sync_bool_compare_and_swap_2: | 
|  | 1164 | case Builtin::BI__sync_bool_compare_and_swap_4: | 
|  | 1165 | case Builtin::BI__sync_bool_compare_and_swap_8: | 
|  | 1166 | case Builtin::BI__sync_bool_compare_and_swap_16: | 
| Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1167 | BuiltinIndex = 11; | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1168 | NumFixed = 2; | 
| Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1169 | ResultType = Context.BoolTy; | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1170 | break; | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1171 |  | 
|  | 1172 | case Builtin::BI__sync_lock_test_and_set: | 
|  | 1173 | case Builtin::BI__sync_lock_test_and_set_1: | 
|  | 1174 | case Builtin::BI__sync_lock_test_and_set_2: | 
|  | 1175 | case Builtin::BI__sync_lock_test_and_set_4: | 
|  | 1176 | case Builtin::BI__sync_lock_test_and_set_8: | 
|  | 1177 | case Builtin::BI__sync_lock_test_and_set_16: | 
|  | 1178 | BuiltinIndex = 12; | 
|  | 1179 | break; | 
|  | 1180 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1181 | case Builtin::BI__sync_lock_release: | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1182 | case Builtin::BI__sync_lock_release_1: | 
|  | 1183 | case Builtin::BI__sync_lock_release_2: | 
|  | 1184 | case Builtin::BI__sync_lock_release_4: | 
|  | 1185 | case Builtin::BI__sync_lock_release_8: | 
|  | 1186 | case Builtin::BI__sync_lock_release_16: | 
| Daniel Dunbar | 7eff7c4 | 2010-03-25 17:13:09 +0000 | [diff] [blame] | 1187 | BuiltinIndex = 13; | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1188 | NumFixed = 0; | 
| Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1189 | ResultType = Context.VoidTy; | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1190 | break; | 
| Douglas Gregor | a976641 | 2011-11-28 16:30:08 +0000 | [diff] [blame] | 1191 |  | 
|  | 1192 | case Builtin::BI__sync_swap: | 
|  | 1193 | case Builtin::BI__sync_swap_1: | 
|  | 1194 | case Builtin::BI__sync_swap_2: | 
|  | 1195 | case Builtin::BI__sync_swap_4: | 
|  | 1196 | case Builtin::BI__sync_swap_8: | 
|  | 1197 | case Builtin::BI__sync_swap_16: | 
|  | 1198 | BuiltinIndex = 14; | 
|  | 1199 | break; | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1200 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1202 | // Now that we know how many fixed arguments we expect, first check that we | 
|  | 1203 | // have at least that many. | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1204 | if (TheCall->getNumArgs() < 1+NumFixed) { | 
|  | 1205 | Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args_at_least) | 
|  | 1206 | << 0 << 1+NumFixed << TheCall->getNumArgs() | 
|  | 1207 | << TheCall->getCallee()->getSourceRange(); | 
|  | 1208 | return ExprError(); | 
|  | 1209 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 |  | 
| Chris Lattner | e7ac0a9 | 2009-05-08 15:36:58 +0000 | [diff] [blame] | 1211 | // Get the decl for the concrete builtin from this, we can tell what the | 
|  | 1212 | // concrete integer type we should convert to is. | 
|  | 1213 | unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex]; | 
|  | 1214 | const char *NewBuiltinName = Context.BuiltinInfo.GetName(NewBuiltinID); | 
| Abramo Bagnara | 2ad11cd | 2012-09-22 09:05:22 +0000 | [diff] [blame] | 1215 | FunctionDecl *NewBuiltinDecl; | 
|  | 1216 | if (NewBuiltinID == BuiltinID) | 
|  | 1217 | NewBuiltinDecl = FDecl; | 
|  | 1218 | else { | 
|  | 1219 | // Perform builtin lookup to avoid redeclaring it. | 
|  | 1220 | DeclarationName DN(&Context.Idents.get(NewBuiltinName)); | 
|  | 1221 | LookupResult Res(*this, DN, DRE->getLocStart(), LookupOrdinaryName); | 
|  | 1222 | LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true); | 
|  | 1223 | assert(Res.getFoundDecl()); | 
|  | 1224 | NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl()); | 
|  | 1225 | if (NewBuiltinDecl == 0) | 
|  | 1226 | return ExprError(); | 
|  | 1227 | } | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1228 |  | 
| John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1229 | // The first argument --- the pointer --- has a fixed type; we | 
|  | 1230 | // deduce the types of the rest of the arguments accordingly.  Walk | 
|  | 1231 | // the remaining arguments, converting them to the deduced value type. | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1232 | for (unsigned i = 0; i != NumFixed; ++i) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1233 | ExprResult Arg = TheCall->getArg(i+1); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1235 | // GCC does an implicit conversion to the pointer or integer ValType.  This | 
|  | 1236 | // can fail in some cases (1i -> int**), check for this error case now. | 
| John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1237 | // Initialize the argument. | 
|  | 1238 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, | 
|  | 1239 | ValType, /*consume*/ false); | 
|  | 1240 | Arg = PerformCopyInitialization(Entity, SourceLocation(), Arg); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1241 | if (Arg.isInvalid()) | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1242 | return ExprError(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1244 | // Okay, we have something that *can* be converted to the right type.  Check | 
|  | 1245 | // to see if there is a potentially weird extension going on here.  This can | 
|  | 1246 | // happen when you do an atomic operation on something like an char* and | 
|  | 1247 | // pass in 42.  The 42 gets converted to char.  This is even more strange | 
|  | 1248 | // for things like 45.123 -> char, etc. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | // FIXME: Do this check. | 
| John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 1250 | TheCall->setArg(i+1, Arg.take()); | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1251 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 |  | 
| Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1253 | ASTContext& Context = this->getASTContext(); | 
|  | 1254 |  | 
|  | 1255 | // Create a new DeclRefExpr to refer to the new decl. | 
|  | 1256 | DeclRefExpr* NewDRE = DeclRefExpr::Create( | 
|  | 1257 | Context, | 
|  | 1258 | DRE->getQualifierLoc(), | 
| Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1259 | SourceLocation(), | 
| Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1260 | NewBuiltinDecl, | 
| John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1261 | /*enclosing*/ false, | 
| Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1262 | DRE->getLocation(), | 
| Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1263 | Context.BuiltinFnTy, | 
| Douglas Gregor | bbcb7ea | 2011-09-09 16:51:10 +0000 | [diff] [blame] | 1264 | DRE->getValueKind()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 |  | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1266 | // Set the callee in the CallExpr. | 
| Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1267 | // FIXME: This loses syntactic information. | 
|  | 1268 | QualType CalleePtrTy = Context.getPointerType(NewBuiltinDecl->getType()); | 
|  | 1269 | ExprResult PromotedCall = ImpCastExprToType(NewDRE, CalleePtrTy, | 
|  | 1270 | CK_BuiltinFnToFnPtr); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1271 | TheCall->setCallee(PromotedCall.take()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 |  | 
| Chandler Carruth | db4325b | 2010-07-18 07:23:17 +0000 | [diff] [blame] | 1273 | // Change the result type of the call to match the original value type. This | 
|  | 1274 | // is arbitrary, but the codegen for these builtins ins design to handle it | 
|  | 1275 | // gracefully. | 
| Chandler Carruth | 8d13d22 | 2010-07-18 20:54:12 +0000 | [diff] [blame] | 1276 | TheCall->setType(ResultType); | 
| Chandler Carruth | d201457 | 2010-07-09 18:59:35 +0000 | [diff] [blame] | 1277 |  | 
| Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1278 | return TheCallResult; | 
| Chris Lattner | 5caa370 | 2009-05-08 06:58:22 +0000 | [diff] [blame] | 1279 | } | 
|  | 1280 |  | 
| Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1281 | /// CheckObjCString - Checks that the argument to the builtin | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1282 | /// CFString constructor is correct | 
| Steve Naroff | fd94262 | 2009-04-13 20:26:29 +0000 | [diff] [blame] | 1283 | /// Note: It might also make sense to do the UTF-16 conversion here (would | 
|  | 1284 | /// simplify the backend). | 
| Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 1285 | bool Sema::CheckObjCString(Expr *Arg) { | 
| Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1286 | Arg = Arg->IgnoreParenCasts(); | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1287 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); | 
|  | 1288 |  | 
| Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1289 | if (!Literal || !Literal->isAscii()) { | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1290 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) | 
|  | 1291 | << Arg->getSourceRange(); | 
| Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1292 | return true; | 
| Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 1293 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 |  | 
| Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1295 | if (Literal->containsNonAsciiOrNull()) { | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1296 | StringRef String = Literal->getString(); | 
| Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1297 | unsigned NumBytes = String.size(); | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1298 | SmallVector<UTF16, 128> ToBuf(NumBytes); | 
| Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1299 | const UTF8 *FromPtr = (const UTF8 *)String.data(); | 
| Fariborz Jahanian | 7da7102 | 2010-09-07 19:38:13 +0000 | [diff] [blame] | 1300 | UTF16 *ToPtr = &ToBuf[0]; | 
|  | 1301 |  | 
|  | 1302 | ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, | 
|  | 1303 | &ToPtr, ToPtr + NumBytes, | 
|  | 1304 | strictConversion); | 
|  | 1305 | // Check for conversion failure. | 
|  | 1306 | if (Result != conversionOK) | 
|  | 1307 | Diag(Arg->getLocStart(), | 
|  | 1308 | diag::warn_cfstring_truncated) << Arg->getSourceRange(); | 
|  | 1309 | } | 
| Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 1310 | return false; | 
| Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1311 | } | 
|  | 1312 |  | 
| Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1313 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. | 
|  | 1314 | /// Emit an error and return true on failure, return false on success. | 
| Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1315 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { | 
|  | 1316 | Expr *Fn = TheCall->getCallee(); | 
|  | 1317 | if (TheCall->getNumArgs() > 2) { | 
| Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1318 | Diag(TheCall->getArg(2)->getLocStart(), | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1319 | diag::err_typecheck_call_too_many_args) | 
| Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1320 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() | 
|  | 1321 | << Fn->getSourceRange() | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | << SourceRange(TheCall->getArg(2)->getLocStart(), | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1323 | (*(TheCall->arg_end()-1))->getLocEnd()); | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1324 | return true; | 
|  | 1325 | } | 
| Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1326 |  | 
|  | 1327 | if (TheCall->getNumArgs() < 2) { | 
| Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1328 | return Diag(TheCall->getLocEnd(), | 
|  | 1329 | diag::err_typecheck_call_too_few_args_at_least) | 
|  | 1330 | << 0 /*function call*/ << 2 << TheCall->getNumArgs(); | 
| Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 1331 | } | 
|  | 1332 |  | 
| John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 1333 | // Type-check the first argument normally. | 
|  | 1334 | if (checkBuiltinArgument(*this, TheCall, 0)) | 
|  | 1335 | return true; | 
|  | 1336 |  | 
| Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1337 | // Determine whether the current function is variadic or not. | 
| Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1338 | BlockScopeInfo *CurBlock = getCurBlock(); | 
| Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1339 | bool isVariadic; | 
| Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1340 | if (CurBlock) | 
| John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 1341 | isVariadic = CurBlock->TheDecl->isVariadic(); | 
| Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1342 | else if (FunctionDecl *FD = getCurFunctionDecl()) | 
|  | 1343 | isVariadic = FD->isVariadic(); | 
|  | 1344 | else | 
| Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1345 | isVariadic = getCurMethodDecl()->isVariadic(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 |  | 
| Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 1347 | if (!isVariadic) { | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1348 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); | 
|  | 1349 | return true; | 
|  | 1350 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 |  | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1352 | // Verify that the second argument to the builtin is the last argument of the | 
|  | 1353 | // current function or method. | 
|  | 1354 | bool SecondArgIsLastNamedArgument = false; | 
| Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 1355 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 |  | 
| Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1357 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { | 
|  | 1358 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1359 | // FIXME: This isn't correct for methods (results in bogus warning). | 
|  | 1360 | // Get the last formal in the current function. | 
| Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 1361 | const ParmVarDecl *LastArg; | 
| Steve Naroff | cd9c514 | 2009-04-15 19:33:47 +0000 | [diff] [blame] | 1362 | if (CurBlock) | 
|  | 1363 | LastArg = *(CurBlock->TheDecl->param_end()-1); | 
|  | 1364 | else if (FunctionDecl *FD = getCurFunctionDecl()) | 
| Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 1365 | LastArg = *(FD->param_end()-1); | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1366 | else | 
| Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 1367 | LastArg = *(getCurMethodDecl()->param_end()-1); | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1368 | SecondArgIsLastNamedArgument = PV == LastArg; | 
|  | 1369 | } | 
|  | 1370 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 |  | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1372 | if (!SecondArgIsLastNamedArgument) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | Diag(TheCall->getArg(1)->getLocStart(), | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1374 | diag::warn_second_parameter_of_va_start_not_last_named_argument); | 
|  | 1375 | return false; | 
| Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 1376 | } | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1377 |  | 
| Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1378 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and | 
|  | 1379 | /// friends.  This is declared to take (...), so we have to check everything. | 
| Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1380 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { | 
|  | 1381 | if (TheCall->getNumArgs() < 2) | 
| Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 1382 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) | 
| Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1383 | << 0 << 2 << TheCall->getNumArgs()/*function call*/; | 
| Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1384 | if (TheCall->getNumArgs() > 2) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1385 | return Diag(TheCall->getArg(2)->getLocStart(), | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1386 | diag::err_typecheck_call_too_many_args) | 
| Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1387 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1388 | << SourceRange(TheCall->getArg(2)->getLocStart(), | 
|  | 1389 | (*(TheCall->arg_end()-1))->getLocEnd()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1391 | ExprResult OrigArg0 = TheCall->getArg(0); | 
|  | 1392 | ExprResult OrigArg1 = TheCall->getArg(1); | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1393 |  | 
| Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1394 | // Do standard promotions between the two arguments, returning their common | 
|  | 1395 | // type. | 
| Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 1396 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1397 | if (OrigArg0.isInvalid() || OrigArg1.isInvalid()) | 
|  | 1398 | return true; | 
| Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 1399 |  | 
|  | 1400 | // Make sure any conversions are pushed back into the call; this is | 
|  | 1401 | // type safe since unordered compare builtins are declared as "_Bool | 
|  | 1402 | // foo(...)". | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1403 | TheCall->setArg(0, OrigArg0.get()); | 
|  | 1404 | TheCall->setArg(1, OrigArg1.get()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1406 | if (OrigArg0.get()->isTypeDependent() || OrigArg1.get()->isTypeDependent()) | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1407 | return false; | 
|  | 1408 |  | 
| Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1409 | // If the common type isn't a real floating type, then the arguments were | 
|  | 1410 | // invalid for this operation. | 
| Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1411 | if (Res.isNull() || !Res->isRealFloatingType()) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1412 | return Diag(OrigArg0.get()->getLocStart(), | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1413 | diag::err_typecheck_call_invalid_ordered_compare) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1414 | << OrigArg0.get()->getType() << OrigArg1.get()->getType() | 
|  | 1415 | << SourceRange(OrigArg0.get()->getLocStart(), OrigArg1.get()->getLocEnd()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 |  | 
| Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 1417 | return false; | 
|  | 1418 | } | 
|  | 1419 |  | 
| Benjamin Kramer | e771a7a | 2010-02-15 22:42:31 +0000 | [diff] [blame] | 1420 | /// SemaBuiltinSemaBuiltinFPClassification - Handle functions like | 
|  | 1421 | /// __builtin_isnan and friends.  This is declared to take (...), so we have | 
| Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1422 | /// to check everything. We expect the last argument to be a floating point | 
|  | 1423 | /// value. | 
|  | 1424 | bool Sema::SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs) { | 
|  | 1425 | if (TheCall->getNumArgs() < NumArgs) | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1426 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) | 
| Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1427 | << 0 << NumArgs << TheCall->getNumArgs()/*function call*/; | 
| Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1428 | if (TheCall->getNumArgs() > NumArgs) | 
|  | 1429 | return Diag(TheCall->getArg(NumArgs)->getLocStart(), | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1430 | diag::err_typecheck_call_too_many_args) | 
| Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1431 | << 0 /*function call*/ << NumArgs << TheCall->getNumArgs() | 
| Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1432 | << SourceRange(TheCall->getArg(NumArgs)->getLocStart(), | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1433 | (*(TheCall->arg_end()-1))->getLocEnd()); | 
|  | 1434 |  | 
| Benjamin Kramer | 3b1e26b | 2010-02-16 10:07:31 +0000 | [diff] [blame] | 1435 | Expr *OrigArg = TheCall->getArg(NumArgs-1); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 |  | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1437 | if (OrigArg->isTypeDependent()) | 
|  | 1438 | return false; | 
|  | 1439 |  | 
| Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1440 | // This operation requires a non-_Complex floating-point number. | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1441 | if (!OrigArg->getType()->isRealFloatingType()) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | return Diag(OrigArg->getLocStart(), | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1443 | diag::err_typecheck_call_invalid_unary_fp) | 
|  | 1444 | << OrigArg->getType() << OrigArg->getSourceRange(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 |  | 
| Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1446 | // If this is an implicit conversion from float -> double, remove it. | 
|  | 1447 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(OrigArg)) { | 
|  | 1448 | Expr *CastArg = Cast->getSubExpr(); | 
|  | 1449 | if (CastArg->getType()->isSpecificBuiltinType(BuiltinType::Float)) { | 
|  | 1450 | assert(Cast->getType()->isSpecificBuiltinType(BuiltinType::Double) && | 
|  | 1451 | "promotion from float to double is the only expected cast here"); | 
|  | 1452 | Cast->setSubExpr(0); | 
| Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1453 | TheCall->setArg(NumArgs-1, CastArg); | 
| Chris Lattner | 81368fb | 2010-05-06 05:50:07 +0000 | [diff] [blame] | 1454 | } | 
|  | 1455 | } | 
|  | 1456 |  | 
| Eli Friedman | 9ac6f62 | 2009-08-31 20:06:00 +0000 | [diff] [blame] | 1457 | return false; | 
|  | 1458 | } | 
|  | 1459 |  | 
| Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1460 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. | 
|  | 1461 | // This is declared to take (...), so we have to check everything. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1462 | ExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1463 | if (TheCall->getNumArgs() < 2) | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1464 | return ExprError(Diag(TheCall->getLocEnd(), | 
| Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1465 | diag::err_typecheck_call_too_few_args_at_least) | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1466 | << 0 /*function call*/ << 2 << TheCall->getNumArgs() | 
| Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 1467 | << TheCall->getSourceRange()); | 
| Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1468 |  | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1469 | // Determine which of the following types of shufflevector we're checking: | 
|  | 1470 | // 1) unary, vector mask: (lhs, mask) | 
|  | 1471 | // 2) binary, vector mask: (lhs, rhs, mask) | 
|  | 1472 | // 3) binary, scalar mask: (lhs, rhs, index, ..., index) | 
|  | 1473 | QualType resType = TheCall->getArg(0)->getType(); | 
|  | 1474 | unsigned numElements = 0; | 
|  | 1475 |  | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1476 | if (!TheCall->getArg(0)->isTypeDependent() && | 
|  | 1477 | !TheCall->getArg(1)->isTypeDependent()) { | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1478 | QualType LHSType = TheCall->getArg(0)->getType(); | 
|  | 1479 | QualType RHSType = TheCall->getArg(1)->getType(); | 
|  | 1480 |  | 
|  | 1481 | if (!LHSType->isVectorType() || !RHSType->isVectorType()) { | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1482 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | << SourceRange(TheCall->getArg(0)->getLocStart(), | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1484 | TheCall->getArg(1)->getLocEnd()); | 
|  | 1485 | return ExprError(); | 
|  | 1486 | } | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1487 |  | 
|  | 1488 | numElements = LHSType->getAs<VectorType>()->getNumElements(); | 
|  | 1489 | unsigned numResElements = TheCall->getNumArgs() - 2; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 |  | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1491 | // Check to see if we have a call with 2 vector arguments, the unary shuffle | 
|  | 1492 | // with mask.  If so, verify that RHS is an integer vector type with the | 
|  | 1493 | // same number of elts as lhs. | 
|  | 1494 | if (TheCall->getNumArgs() == 2) { | 
| Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1495 | if (!RHSType->hasIntegerRepresentation() || | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1496 | RHSType->getAs<VectorType>()->getNumElements() != numElements) | 
|  | 1497 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) | 
|  | 1498 | << SourceRange(TheCall->getArg(1)->getLocStart(), | 
|  | 1499 | TheCall->getArg(1)->getLocEnd()); | 
|  | 1500 | numResElements = numElements; | 
|  | 1501 | } | 
|  | 1502 | else if (!Context.hasSameUnqualifiedType(LHSType, RHSType)) { | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1503 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | << SourceRange(TheCall->getArg(0)->getLocStart(), | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1505 | TheCall->getArg(1)->getLocEnd()); | 
|  | 1506 | return ExprError(); | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1507 | } else if (numElements != numResElements) { | 
|  | 1508 | QualType eltType = LHSType->getAs<VectorType>()->getElementType(); | 
| Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1509 | resType = Context.getVectorType(eltType, numResElements, | 
| Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1510 | VectorType::GenericVector); | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1511 | } | 
| Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1512 | } | 
|  | 1513 |  | 
|  | 1514 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1515 | if (TheCall->getArg(i)->isTypeDependent() || | 
|  | 1516 | TheCall->getArg(i)->isValueDependent()) | 
|  | 1517 | continue; | 
|  | 1518 |  | 
| Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 1519 | llvm::APSInt Result(32); | 
|  | 1520 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) | 
|  | 1521 | return ExprError(Diag(TheCall->getLocStart(), | 
|  | 1522 | diag::err_shufflevector_nonconstant_argument) | 
|  | 1523 | << TheCall->getArg(i)->getSourceRange()); | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1524 |  | 
| Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1525 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1526 | return ExprError(Diag(TheCall->getLocStart(), | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1527 | diag::err_shufflevector_argument_too_large) | 
| Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1528 | << TheCall->getArg(i)->getSourceRange()); | 
| Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1529 | } | 
|  | 1530 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1531 | SmallVector<Expr*, 32> exprs; | 
| Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1532 |  | 
| Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 1533 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { | 
| Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1534 | exprs.push_back(TheCall->getArg(i)); | 
|  | 1535 | TheCall->setArg(i, 0); | 
|  | 1536 | } | 
|  | 1537 |  | 
| Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1538 | return Owned(new (Context) ShuffleVectorExpr(Context, exprs, resType, | 
| Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1539 | TheCall->getCallee()->getLocStart(), | 
|  | 1540 | TheCall->getRParenLoc())); | 
| Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1541 | } | 
| Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 1542 |  | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1543 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. | 
|  | 1544 | // This is declared to take (const void*, ...) and can take two | 
|  | 1545 | // optional constant int args. | 
|  | 1546 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1547 | unsigned NumArgs = TheCall->getNumArgs(); | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1548 |  | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1549 | if (NumArgs > 3) | 
| Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1550 | return Diag(TheCall->getLocEnd(), | 
|  | 1551 | diag::err_typecheck_call_too_many_args_at_most) | 
|  | 1552 | << 0 /*function call*/ << 3 << NumArgs | 
|  | 1553 | << TheCall->getSourceRange(); | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1554 |  | 
|  | 1555 | // Argument 0 is checked for us and the remaining arguments must be | 
|  | 1556 | // constant integers. | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1557 | for (unsigned i = 1; i != NumArgs; ++i) { | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1558 | Expr *Arg = TheCall->getArg(i); | 
| Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1559 |  | 
|  | 1560 | // We can't check the value of a dependent argument. | 
|  | 1561 | if (Arg->isTypeDependent() || Arg->isValueDependent()) | 
|  | 1562 | continue; | 
|  | 1563 |  | 
| Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1564 | llvm::APSInt Result; | 
| Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1565 | if (SemaBuiltinConstantArg(TheCall, i, Result)) | 
|  | 1566 | return true; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 |  | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1568 | // FIXME: gcc issues a warning and rewrites these to 0. These | 
|  | 1569 | // seems especially odd for the third argument since the default | 
|  | 1570 | // is 3. | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1571 | if (i == 1) { | 
| Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1572 | if (Result.getLimitedValue() > 1) | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1573 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) | 
| Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1574 | << "0" << "1" << Arg->getSourceRange(); | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1575 | } else { | 
| Eli Friedman | 9aef726 | 2009-12-04 00:30:06 +0000 | [diff] [blame] | 1576 | if (Result.getLimitedValue() > 3) | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1577 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) | 
| Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1578 | << "0" << "3" << Arg->getSourceRange(); | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1579 | } | 
|  | 1580 | } | 
|  | 1581 |  | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1582 | return false; | 
| Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 1583 | } | 
|  | 1584 |  | 
| Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1585 | /// SemaBuiltinConstantArg - Handle a check if argument ArgNum of CallExpr | 
|  | 1586 | /// TheCall is a constant expression. | 
|  | 1587 | bool Sema::SemaBuiltinConstantArg(CallExpr *TheCall, int ArgNum, | 
|  | 1588 | llvm::APSInt &Result) { | 
|  | 1589 | Expr *Arg = TheCall->getArg(ArgNum); | 
|  | 1590 | DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts()); | 
|  | 1591 | FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl()); | 
|  | 1592 |  | 
|  | 1593 | if (Arg->isTypeDependent() || Arg->isValueDependent()) return false; | 
|  | 1594 |  | 
|  | 1595 | if (!Arg->isIntegerConstantExpr(Result, Context)) | 
|  | 1596 | return Diag(TheCall->getLocStart(), diag::err_constant_integer_arg_type) | 
| Eric Christopher | 5e89655 | 2010-04-19 18:23:02 +0000 | [diff] [blame] | 1597 | << FDecl->getDeclName() <<  Arg->getSourceRange(); | 
| Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1598 |  | 
| Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 1599 | return false; | 
|  | 1600 | } | 
|  | 1601 |  | 
| Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1602 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, | 
|  | 1603 | /// int type). This simply type checks that type is one of the defined | 
|  | 1604 | /// constants (0-3). | 
| Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1605 | // For compatibility check 0-3, llvm only handles 0 and 2. | 
| Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1606 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { | 
| Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1607 | llvm::APSInt Result; | 
| Douglas Gregor | 592a423 | 2012-06-29 01:05:22 +0000 | [diff] [blame] | 1608 |  | 
|  | 1609 | // We can't check the value of a dependent argument. | 
|  | 1610 | if (TheCall->getArg(1)->isTypeDependent() || | 
|  | 1611 | TheCall->getArg(1)->isValueDependent()) | 
|  | 1612 | return false; | 
|  | 1613 |  | 
| Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1614 | // Check constant-ness first. | 
|  | 1615 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) | 
|  | 1616 | return true; | 
|  | 1617 |  | 
| Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1618 | Expr *Arg = TheCall->getArg(1); | 
| Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1619 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { | 
| Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1620 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) | 
|  | 1621 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); | 
| Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 1622 | } | 
|  | 1623 |  | 
|  | 1624 | return false; | 
|  | 1625 | } | 
|  | 1626 |  | 
| Eli Friedman | 586d6a8 | 2009-05-03 06:04:26 +0000 | [diff] [blame] | 1627 | /// SemaBuiltinLongjmp - Handle __builtin_longjmp(void *env[5], int val). | 
| Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1628 | /// This checks that val is a constant 1. | 
|  | 1629 | bool Sema::SemaBuiltinLongjmp(CallExpr *TheCall) { | 
|  | 1630 | Expr *Arg = TheCall->getArg(1); | 
| Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1631 | llvm::APSInt Result; | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1632 |  | 
| Eric Christopher | 691ebc3 | 2010-04-17 02:26:23 +0000 | [diff] [blame] | 1633 | // TODO: This is less than ideal. Overload this to take a value. | 
|  | 1634 | if (SemaBuiltinConstantArg(TheCall, 1, Result)) | 
|  | 1635 | return true; | 
|  | 1636 |  | 
|  | 1637 | if (Result != 1) | 
| Eli Friedman | d875fed | 2009-05-03 04:46:36 +0000 | [diff] [blame] | 1638 | return Diag(TheCall->getLocStart(), diag::err_builtin_longjmp_invalid_val) | 
|  | 1639 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); | 
|  | 1640 |  | 
|  | 1641 | return false; | 
|  | 1642 | } | 
|  | 1643 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1644 | // Determine if an expression is a string literal or constant string. | 
|  | 1645 | // If this function returns false on the arguments to a function expecting a | 
|  | 1646 | // format string, we will usually need to emit a warning. | 
|  | 1647 | // True string literals are then checked by CheckFormatString. | 
|  | 1648 | Sema::StringLiteralCheckType | 
|  | 1649 | Sema::checkFormatStringExpr(const Expr *E, Expr **Args, | 
|  | 1650 | unsigned NumArgs, bool HasVAListArg, | 
|  | 1651 | unsigned format_idx, unsigned firstDataArg, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1652 | FormatStringType Type, VariadicCallType CallType, | 
|  | 1653 | bool inFunctionCall) { | 
| Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1654 | tryAgain: | 
| Douglas Gregor | cde0173 | 2009-05-19 22:10:17 +0000 | [diff] [blame] | 1655 | if (E->isTypeDependent() || E->isValueDependent()) | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1656 | return SLCT_NotALiteral; | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1657 |  | 
| Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1658 | E = E->IgnoreParenCasts(); | 
| Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1659 |  | 
| David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1660 | if (E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) | 
|  | 1661 | // Technically -Wformat-nonliteral does not warn about this case. | 
|  | 1662 | // The behavior of printf and friends in this case is implementation | 
|  | 1663 | // dependent.  Ideally if the format string cannot be null then | 
|  | 1664 | // it should have a 'nonnull' attribute in the function prototype. | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1665 | return SLCT_CheckedLiteral; | 
| David Blaikie | a73cdcb | 2012-02-10 21:07:25 +0000 | [diff] [blame] | 1666 |  | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1667 | switch (E->getStmtClass()) { | 
| John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1668 | case Stmt::BinaryConditionalOperatorClass: | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1669 | case Stmt::ConditionalOperatorClass: { | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1670 | // The expression is a literal if both sub-expressions were, and it was | 
|  | 1671 | // completely checked only if both sub-expressions were checked. | 
|  | 1672 | const AbstractConditionalOperator *C = | 
|  | 1673 | cast<AbstractConditionalOperator>(E); | 
|  | 1674 | StringLiteralCheckType Left = | 
|  | 1675 | checkFormatStringExpr(C->getTrueExpr(), Args, NumArgs, | 
|  | 1676 | HasVAListArg, format_idx, firstDataArg, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1677 | Type, CallType, inFunctionCall); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1678 | if (Left == SLCT_NotALiteral) | 
|  | 1679 | return SLCT_NotALiteral; | 
|  | 1680 | StringLiteralCheckType Right = | 
|  | 1681 | checkFormatStringExpr(C->getFalseExpr(), Args, NumArgs, | 
|  | 1682 | HasVAListArg, format_idx, firstDataArg, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1683 | Type, CallType, inFunctionCall); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1684 | return Left < Right ? Left : Right; | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1685 | } | 
|  | 1686 |  | 
|  | 1687 | case Stmt::ImplicitCastExprClass: { | 
| Ted Kremenek | 4fe6441 | 2010-09-09 03:51:39 +0000 | [diff] [blame] | 1688 | E = cast<ImplicitCastExpr>(E)->getSubExpr(); | 
|  | 1689 | goto tryAgain; | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1690 | } | 
|  | 1691 |  | 
| John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1692 | case Stmt::OpaqueValueExprClass: | 
|  | 1693 | if (const Expr *src = cast<OpaqueValueExpr>(E)->getSourceExpr()) { | 
|  | 1694 | E = src; | 
|  | 1695 | goto tryAgain; | 
|  | 1696 | } | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1697 | return SLCT_NotALiteral; | 
| John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1698 |  | 
| Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1699 | case Stmt::PredefinedExprClass: | 
|  | 1700 | // While __func__, etc., are technically not string literals, they | 
|  | 1701 | // cannot contain format specifiers and thus are not a security | 
|  | 1702 | // liability. | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1703 | return SLCT_UncheckedLiteral; | 
| Ted Kremenek | b43e8ad | 2011-02-24 23:03:04 +0000 | [diff] [blame] | 1704 |  | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1705 | case Stmt::DeclRefExprClass: { | 
|  | 1706 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 |  | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1708 | // As an exception, do not flag errors for variables binding to | 
|  | 1709 | // const string literals. | 
|  | 1710 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { | 
|  | 1711 | bool isConstant = false; | 
|  | 1712 | QualType T = DR->getType(); | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1713 |  | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1714 | if (const ArrayType *AT = Context.getAsArrayType(T)) { | 
|  | 1715 | isConstant = AT->getElementType().isConstant(Context); | 
| Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1716 | } else if (const PointerType *PT = T->getAs<PointerType>()) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | isConstant = T.isConstant(Context) && | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1718 | PT->getPointeeType().isConstant(Context); | 
| Jean-Daniel Dupas | e98e5b5 | 2012-01-25 10:35:33 +0000 | [diff] [blame] | 1719 | } else if (T->isObjCObjectPointerType()) { | 
|  | 1720 | // In ObjC, there is usually no "const ObjectPointer" type, | 
|  | 1721 | // so don't check if the pointee type is constant. | 
|  | 1722 | isConstant = T.isConstant(Context); | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1723 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 |  | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1725 | if (isConstant) { | 
| Matt Beaumont-Gay | e2c6066 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 1726 | if (const Expr *Init = VD->getAnyInitializer()) { | 
|  | 1727 | // Look through initializers like const char c[] = { "foo" } | 
|  | 1728 | if (const InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { | 
|  | 1729 | if (InitList->isStringLiteralInit()) | 
|  | 1730 | Init = InitList->getInit(0)->IgnoreParenImpCasts(); | 
|  | 1731 | } | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1732 | return checkFormatStringExpr(Init, Args, NumArgs, | 
|  | 1733 | HasVAListArg, format_idx, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1734 | firstDataArg, Type, CallType, | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1735 | /*inFunctionCall*/false); | 
| Matt Beaumont-Gay | e2c6066 | 2012-05-11 22:10:59 +0000 | [diff] [blame] | 1736 | } | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1737 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 |  | 
| Anders Carlsson | d966a55 | 2009-06-28 19:55:58 +0000 | [diff] [blame] | 1739 | // For vprintf* functions (i.e., HasVAListArg==true), we add a | 
|  | 1740 | // special check to see if the format string is a function parameter | 
|  | 1741 | // of the function calling the printf function.  If the function | 
|  | 1742 | // has an attribute indicating it is a printf-like function, then we | 
|  | 1743 | // should suppress warnings concerning non-literals being used in a call | 
|  | 1744 | // to a vprintf function.  For example: | 
|  | 1745 | // | 
|  | 1746 | // void | 
|  | 1747 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ | 
|  | 1748 | //      va_list ap; | 
|  | 1749 | //      va_start(ap, fmt); | 
|  | 1750 | //      vprintf(fmt, ap);  // Do NOT emit a warning about "fmt". | 
|  | 1751 | //      ... | 
|  | 1752 | // | 
| Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1753 | if (HasVAListArg) { | 
|  | 1754 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(VD)) { | 
|  | 1755 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(PV->getDeclContext())) { | 
|  | 1756 | int PVIndex = PV->getFunctionScopeIndex() + 1; | 
|  | 1757 | for (specific_attr_iterator<FormatAttr> | 
|  | 1758 | i = ND->specific_attr_begin<FormatAttr>(), | 
|  | 1759 | e = ND->specific_attr_end<FormatAttr>(); i != e ; ++i) { | 
|  | 1760 | FormatAttr *PVFormat = *i; | 
|  | 1761 | // adjust for implicit parameter | 
|  | 1762 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) | 
|  | 1763 | if (MD->isInstance()) | 
|  | 1764 | ++PVIndex; | 
|  | 1765 | // We also check if the formats are compatible. | 
|  | 1766 | // We can't pass a 'scanf' string to a 'printf' function. | 
|  | 1767 | if (PVIndex == PVFormat->getFormatIdx() && | 
|  | 1768 | Type == GetFormatStringType(PVFormat)) | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1769 | return SLCT_UncheckedLiteral; | 
| Jean-Daniel Dupas | f57c413 | 2012-02-21 20:00:53 +0000 | [diff] [blame] | 1770 | } | 
|  | 1771 | } | 
|  | 1772 | } | 
|  | 1773 | } | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1774 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1776 | return SLCT_NotALiteral; | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1777 | } | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1778 |  | 
| Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1779 | case Stmt::CallExprClass: | 
|  | 1780 | case Stmt::CXXMemberCallExprClass: { | 
| Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1781 | const CallExpr *CE = cast<CallExpr>(E); | 
| Jean-Daniel Dupas | 52aabaf | 2012-02-07 19:01:42 +0000 | [diff] [blame] | 1782 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) { | 
|  | 1783 | if (const FormatArgAttr *FA = ND->getAttr<FormatArgAttr>()) { | 
|  | 1784 | unsigned ArgIndex = FA->getFormatIdx(); | 
|  | 1785 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) | 
|  | 1786 | if (MD->isInstance()) | 
|  | 1787 | --ArgIndex; | 
|  | 1788 | const Expr *Arg = CE->getArg(ArgIndex - 1); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1790 | return checkFormatStringExpr(Arg, Args, NumArgs, | 
|  | 1791 | HasVAListArg, format_idx, firstDataArg, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1792 | Type, CallType, inFunctionCall); | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1793 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { | 
|  | 1794 | unsigned BuiltinID = FD->getBuiltinID(); | 
|  | 1795 | if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString || | 
|  | 1796 | BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString) { | 
|  | 1797 | const Expr *Arg = CE->getArg(0); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1798 | return checkFormatStringExpr(Arg, Args, NumArgs, | 
|  | 1799 | HasVAListArg, format_idx, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1800 | firstDataArg, Type, CallType, | 
|  | 1801 | inFunctionCall); | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1802 | } | 
| Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1803 | } | 
|  | 1804 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1806 | return SLCT_NotALiteral; | 
| Anders Carlsson | 8f031b3 | 2009-06-27 04:05:33 +0000 | [diff] [blame] | 1807 | } | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1808 | case Stmt::ObjCStringLiteralClass: | 
|  | 1809 | case Stmt::StringLiteralClass: { | 
|  | 1810 | const StringLiteral *StrE = NULL; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 |  | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1812 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1813 | StrE = ObjCFExpr->getString(); | 
|  | 1814 | else | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1815 | StrE = cast<StringLiteral>(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 |  | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1817 | if (StrE) { | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1818 | CheckFormatString(StrE, E, Args, NumArgs, HasVAListArg, format_idx, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1819 | firstDataArg, Type, inFunctionCall, CallType); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1820 | return SLCT_CheckedLiteral; | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1821 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1823 | return SLCT_NotALiteral; | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1824 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 |  | 
| Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 1826 | default: | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1827 | return SLCT_NotALiteral; | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1828 | } | 
|  | 1829 | } | 
|  | 1830 |  | 
| Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1831 | void | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | Sema::CheckNonNullArguments(const NonNullAttr *NonNull, | 
| Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1833 | const Expr * const *ExprArgs, | 
|  | 1834 | SourceLocation CallSiteLoc) { | 
| Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1835 | for (NonNullAttr::args_iterator i = NonNull->args_begin(), | 
|  | 1836 | e = NonNull->args_end(); | 
| Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1837 | i != e; ++i) { | 
| Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1838 | const Expr *ArgExpr = ExprArgs[*i]; | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1839 | if (ArgExpr->isNullPointerConstant(Context, | 
| Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1840 | Expr::NPC_ValueDependentIsNotNull)) | 
| Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 1841 | Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); | 
| Fariborz Jahanian | e898f8a | 2009-05-21 18:48:51 +0000 | [diff] [blame] | 1842 | } | 
|  | 1843 | } | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1844 |  | 
| Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1845 | Sema::FormatStringType Sema::GetFormatStringType(const FormatAttr *Format) { | 
|  | 1846 | return llvm::StringSwitch<FormatStringType>(Format->getType()) | 
|  | 1847 | .Case("scanf", FST_Scanf) | 
|  | 1848 | .Cases("printf", "printf0", FST_Printf) | 
|  | 1849 | .Cases("NSString", "CFString", FST_NSString) | 
|  | 1850 | .Case("strftime", FST_Strftime) | 
|  | 1851 | .Case("strfmon", FST_Strfmon) | 
|  | 1852 | .Cases("kprintf", "cmn_err", "vcmn_err", "zcmn_err", FST_Kprintf) | 
|  | 1853 | .Default(FST_Unknown); | 
|  | 1854 | } | 
|  | 1855 |  | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1856 | /// CheckFormatArguments - Check calls to printf and scanf (and similar | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1857 | /// functions) for correct use of format strings. | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1858 | /// Returns true if a format string has been fully checked. | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1859 | bool Sema::CheckFormatArguments(const FormatAttr *Format, Expr **Args, | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1860 | unsigned NumArgs, bool IsCXXMember, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1861 | VariadicCallType CallType, | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1862 | SourceLocation Loc, SourceRange Range) { | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1863 | FormatStringInfo FSI; | 
|  | 1864 | if (getFormatStringInfo(Format, IsCXXMember, &FSI)) | 
|  | 1865 | return CheckFormatArguments(Args, NumArgs, FSI.HasVAListArg, FSI.FormatIdx, | 
|  | 1866 | FSI.FirstDataArg, GetFormatStringType(Format), | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1867 | CallType, Loc, Range); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1868 | return false; | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1869 | } | 
| Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1870 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1871 | bool Sema::CheckFormatArguments(Expr **Args, unsigned NumArgs, | 
| Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1872 | bool HasVAListArg, unsigned format_idx, | 
|  | 1873 | unsigned firstDataArg, FormatStringType Type, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1874 | VariadicCallType CallType, | 
| Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 1875 | SourceLocation Loc, SourceRange Range) { | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1876 | // CHECK: printf/scanf-like function is called with no format string. | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1877 | if (format_idx >= NumArgs) { | 
|  | 1878 | Diag(Loc, diag::warn_missing_format_string) << Range; | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1879 | return false; | 
| Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1880 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 |  | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1882 | const Expr *OrigFormatExpr = Args[format_idx]->IgnoreParenCasts(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 |  | 
| Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 1884 | // CHECK: format string is not a string literal. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | // | 
| Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1886 | // Dynamically generated format strings are difficult to | 
|  | 1887 | // automatically vet at compile time.  Requiring that format strings | 
|  | 1888 | // are string literals: (1) permits the checking of format strings by | 
|  | 1889 | // the compiler and thereby (2) can practically remove the source of | 
|  | 1890 | // many format string exploits. | 
| Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1891 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | // Format string can be either ObjC string (e.g. @"%d") or | 
| Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1893 | // C string (e.g. "%d") | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | // ObjC string uses the same format specifiers as C string, so we can use | 
| Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1895 | // the same format string checking logic for both ObjC and C strings. | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1896 | StringLiteralCheckType CT = | 
|  | 1897 | checkFormatStringExpr(OrigFormatExpr, Args, NumArgs, HasVAListArg, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1898 | format_idx, firstDataArg, Type, CallType); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1899 | if (CT != SLCT_NotALiteral) | 
|  | 1900 | // Literal format string found, check done! | 
|  | 1901 | return CT == SLCT_CheckedLiteral; | 
| Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 1902 |  | 
| Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1903 | // Strftime is particular as it always uses a single 'time' argument, | 
|  | 1904 | // so it is safe to pass a non-literal string. | 
|  | 1905 | if (Type == FST_Strftime) | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1906 | return false; | 
| Jean-Daniel Dupas | 2837a2f | 2012-02-07 23:10:53 +0000 | [diff] [blame] | 1907 |  | 
| Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1908 | // Do not emit diag when the string param is a macro expansion and the | 
|  | 1909 | // format is either NSString or CFString. This is a hack to prevent | 
|  | 1910 | // diag when using the NSLocalizedString and CFCopyLocalizedString macros | 
|  | 1911 | // which are usually used in place of NS and CF string literals. | 
| Jean-Daniel Dupas | dc17020 | 2012-05-04 21:08:08 +0000 | [diff] [blame] | 1912 | if (Type == FST_NSString && | 
|  | 1913 | SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1914 | return false; | 
| Jean-Daniel Dupas | ce3aa39 | 2012-01-30 19:46:17 +0000 | [diff] [blame] | 1915 |  | 
| Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1916 | // If there are no arguments specified, warn with -Wformat-security, otherwise | 
|  | 1917 | // warn only with -Wformat-nonliteral. | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1918 | if (NumArgs == format_idx+1) | 
|  | 1919 | Diag(Args[format_idx]->getLocStart(), | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1920 | diag::warn_format_nonliteral_noargs) | 
| Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1921 | << OrigFormatExpr->getSourceRange(); | 
|  | 1922 | else | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1923 | Diag(Args[format_idx]->getLocStart(), | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1924 | diag::warn_format_nonliteral) | 
| Chris Lattner | 655f141 | 2009-04-29 04:59:47 +0000 | [diff] [blame] | 1925 | << OrigFormatExpr->getSourceRange(); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 1926 | return false; | 
| Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 1927 | } | 
| Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 1928 |  | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1929 | namespace { | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1930 | class CheckFormatHandler : public analyze_format_string::FormatStringHandler { | 
|  | 1931 | protected: | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1932 | Sema &S; | 
|  | 1933 | const StringLiteral *FExpr; | 
|  | 1934 | const Expr *OrigFormatExpr; | 
| Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1935 | const unsigned FirstDataArg; | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1936 | const unsigned NumDataArgs; | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1937 | const char *Beg; // Start of format string. | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1938 | const bool HasVAListArg; | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1939 | const Expr * const *Args; | 
|  | 1940 | const unsigned NumArgs; | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 1941 | unsigned FormatIdx; | 
| Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1942 | llvm::BitVector CoveredArgs; | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1943 | bool usesPositionalArgs; | 
|  | 1944 | bool atFirstArg; | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1945 | bool inFunctionCall; | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1946 | Sema::VariadicCallType CallType; | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1947 | public: | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1948 | CheckFormatHandler(Sema &s, const StringLiteral *fexpr, | 
| Ted Kremenek | 6ee7653 | 2010-03-25 03:59:12 +0000 | [diff] [blame] | 1949 | const Expr *origFormatExpr, unsigned firstDataArg, | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1950 | unsigned numDataArgs, const char *beg, bool hasVAListArg, | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1951 | Expr **args, unsigned numArgs, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1952 | unsigned formatIdx, bool inFunctionCall, | 
|  | 1953 | Sema::VariadicCallType callType) | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1954 | : S(s), FExpr(fexpr), OrigFormatExpr(origFormatExpr), | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 1955 | FirstDataArg(firstDataArg), NumDataArgs(numDataArgs), | 
|  | 1956 | Beg(beg), HasVAListArg(hasVAListArg), | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1957 | Args(args), NumArgs(numArgs), FormatIdx(formatIdx), | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1958 | usesPositionalArgs(false), atFirstArg(true), | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 1959 | inFunctionCall(inFunctionCall), CallType(callType) { | 
| Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 1960 | CoveredArgs.resize(numDataArgs); | 
|  | 1961 | CoveredArgs.reset(); | 
|  | 1962 | } | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1963 |  | 
| Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 1964 | void DoneProcessing(); | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1965 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1966 | void HandleIncompleteSpecifier(const char *startSpecifier, | 
|  | 1967 | unsigned specifierLen); | 
| Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1968 |  | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 1969 | void HandleInvalidLengthModifier( | 
|  | 1970 | const analyze_format_string::FormatSpecifier &FS, | 
|  | 1971 | const analyze_format_string::ConversionSpecifier &CS, | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 1972 | const char *startSpecifier, unsigned specifierLen, unsigned DiagID); | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 1973 |  | 
| Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1974 | void HandleNonStandardLengthModifier( | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 1975 | const analyze_format_string::FormatSpecifier &FS, | 
| Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 1976 | const char *startSpecifier, unsigned specifierLen); | 
|  | 1977 |  | 
|  | 1978 | void HandleNonStandardConversionSpecifier( | 
|  | 1979 | const analyze_format_string::ConversionSpecifier &CS, | 
|  | 1980 | const char *startSpecifier, unsigned specifierLen); | 
|  | 1981 |  | 
| Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 1982 | virtual void HandlePosition(const char *startPos, unsigned posLen); | 
|  | 1983 |  | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1984 | virtual void HandleInvalidPosition(const char *startSpecifier, | 
|  | 1985 | unsigned specifierLen, | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 1986 | analyze_format_string::PositionContext p); | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 1987 |  | 
|  | 1988 | virtual void HandleZeroPosition(const char *startPos, unsigned posLen); | 
|  | 1989 |  | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 1990 | void HandleNullChar(const char *nullCharacter); | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 1991 |  | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1992 | template <typename Range> | 
|  | 1993 | static void EmitFormatDiagnostic(Sema &S, bool inFunctionCall, | 
|  | 1994 | const Expr *ArgumentExpr, | 
|  | 1995 | PartialDiagnostic PDiag, | 
|  | 1996 | SourceLocation StringLoc, | 
|  | 1997 | bool IsStringLocation, Range StringRange, | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 1998 | ArrayRef<FixItHint> Fixit = ArrayRef<FixItHint>()); | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 1999 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2000 | protected: | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2001 | bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc, | 
|  | 2002 | const char *startSpec, | 
|  | 2003 | unsigned specifierLen, | 
|  | 2004 | const char *csStart, unsigned csLen); | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2005 |  | 
|  | 2006 | void HandlePositionalNonpositionalArgs(SourceLocation Loc, | 
|  | 2007 | const char *startSpec, | 
|  | 2008 | unsigned specifierLen); | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2009 |  | 
| Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2010 | SourceRange getFormatStringRange(); | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2011 | CharSourceRange getSpecifierRange(const char *startSpecifier, | 
|  | 2012 | unsigned specifierLen); | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2013 | SourceLocation getLocationOfByte(const char *x); | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2014 |  | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2015 | const Expr *getDataArg(unsigned i) const; | 
| Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2016 |  | 
|  | 2017 | bool CheckNumArgs(const analyze_format_string::FormatSpecifier &FS, | 
|  | 2018 | const analyze_format_string::ConversionSpecifier &CS, | 
|  | 2019 | const char *startSpecifier, unsigned specifierLen, | 
|  | 2020 | unsigned argIndex); | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2021 |  | 
|  | 2022 | template <typename Range> | 
|  | 2023 | void EmitFormatDiagnostic(PartialDiagnostic PDiag, SourceLocation StringLoc, | 
|  | 2024 | bool IsStringLocation, Range StringRange, | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2025 | ArrayRef<FixItHint> Fixit = ArrayRef<FixItHint>()); | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2026 |  | 
|  | 2027 | void CheckPositionalAndNonpositionalArgs( | 
|  | 2028 | const analyze_format_string::FormatSpecifier *FS); | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2029 | }; | 
|  | 2030 | } | 
|  | 2031 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2032 | SourceRange CheckFormatHandler::getFormatStringRange() { | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2033 | return OrigFormatExpr->getSourceRange(); | 
|  | 2034 | } | 
|  | 2035 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2036 | CharSourceRange CheckFormatHandler:: | 
|  | 2037 | getSpecifierRange(const char *startSpecifier, unsigned specifierLen) { | 
| Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2038 | SourceLocation Start = getLocationOfByte(startSpecifier); | 
|  | 2039 | SourceLocation End   = getLocationOfByte(startSpecifier + specifierLen - 1); | 
|  | 2040 |  | 
|  | 2041 | // Advance the end SourceLocation by one due to half-open ranges. | 
| Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 2042 | End = End.getLocWithOffset(1); | 
| Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2043 |  | 
|  | 2044 | return CharSourceRange::getCharRange(Start, End); | 
| Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2045 | } | 
|  | 2046 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2047 | SourceLocation CheckFormatHandler::getLocationOfByte(const char *x) { | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2048 | return S.getLocationOfStringLiteralByte(FExpr, x - Beg); | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2049 | } | 
|  | 2050 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2051 | void CheckFormatHandler::HandleIncompleteSpecifier(const char *startSpecifier, | 
|  | 2052 | unsigned specifierLen){ | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2053 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_incomplete_specifier), | 
|  | 2054 | getLocationOfByte(startSpecifier), | 
|  | 2055 | /*IsStringLocation*/true, | 
|  | 2056 | getSpecifierRange(startSpecifier, specifierLen)); | 
| Ted Kremenek | 808015a | 2010-01-29 03:16:21 +0000 | [diff] [blame] | 2057 | } | 
|  | 2058 |  | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2059 | void CheckFormatHandler::HandleInvalidLengthModifier( | 
|  | 2060 | const analyze_format_string::FormatSpecifier &FS, | 
|  | 2061 | const analyze_format_string::ConversionSpecifier &CS, | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2062 | const char *startSpecifier, unsigned specifierLen, unsigned DiagID) { | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2063 | using namespace analyze_format_string; | 
|  | 2064 |  | 
|  | 2065 | const LengthModifier &LM = FS.getLengthModifier(); | 
|  | 2066 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); | 
|  | 2067 |  | 
|  | 2068 | // See if we know how to fix this length modifier. | 
|  | 2069 | llvm::Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); | 
|  | 2070 | if (FixedLM) { | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2071 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2072 | getLocationOfByte(LM.getStart()), | 
|  | 2073 | /*IsStringLocation*/true, | 
|  | 2074 | getSpecifierRange(startSpecifier, specifierLen)); | 
|  | 2075 |  | 
|  | 2076 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) | 
|  | 2077 | << FixedLM->toString() | 
|  | 2078 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); | 
|  | 2079 |  | 
|  | 2080 | } else { | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2081 | FixItHint Hint; | 
|  | 2082 | if (DiagID == diag::warn_format_nonsensical_length) | 
|  | 2083 | Hint = FixItHint::CreateRemoval(LMRange); | 
|  | 2084 |  | 
|  | 2085 | EmitFormatDiagnostic(S.PDiag(DiagID) << LM.toString() << CS.toString(), | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2086 | getLocationOfByte(LM.getStart()), | 
|  | 2087 | /*IsStringLocation*/true, | 
|  | 2088 | getSpecifierRange(startSpecifier, specifierLen), | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2089 | Hint); | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2090 | } | 
|  | 2091 | } | 
|  | 2092 |  | 
| Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2093 | void CheckFormatHandler::HandleNonStandardLengthModifier( | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2094 | const analyze_format_string::FormatSpecifier &FS, | 
| Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2095 | const char *startSpecifier, unsigned specifierLen) { | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2096 | using namespace analyze_format_string; | 
|  | 2097 |  | 
|  | 2098 | const LengthModifier &LM = FS.getLengthModifier(); | 
|  | 2099 | CharSourceRange LMRange = getSpecifierRange(LM.getStart(), LM.getLength()); | 
|  | 2100 |  | 
|  | 2101 | // See if we know how to fix this length modifier. | 
|  | 2102 | llvm::Optional<LengthModifier> FixedLM = FS.getCorrectedLengthModifier(); | 
|  | 2103 | if (FixedLM) { | 
|  | 2104 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) | 
|  | 2105 | << LM.toString() << 0, | 
|  | 2106 | getLocationOfByte(LM.getStart()), | 
|  | 2107 | /*IsStringLocation*/true, | 
|  | 2108 | getSpecifierRange(startSpecifier, specifierLen)); | 
|  | 2109 |  | 
|  | 2110 | S.Diag(getLocationOfByte(LM.getStart()), diag::note_format_fix_specifier) | 
|  | 2111 | << FixedLM->toString() | 
|  | 2112 | << FixItHint::CreateReplacement(LMRange, FixedLM->toString()); | 
|  | 2113 |  | 
|  | 2114 | } else { | 
|  | 2115 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) | 
|  | 2116 | << LM.toString() << 0, | 
|  | 2117 | getLocationOfByte(LM.getStart()), | 
|  | 2118 | /*IsStringLocation*/true, | 
|  | 2119 | getSpecifierRange(startSpecifier, specifierLen)); | 
|  | 2120 | } | 
| Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2121 | } | 
|  | 2122 |  | 
|  | 2123 | void CheckFormatHandler::HandleNonStandardConversionSpecifier( | 
|  | 2124 | const analyze_format_string::ConversionSpecifier &CS, | 
|  | 2125 | const char *startSpecifier, unsigned specifierLen) { | 
| Jordan Rose | 670941c | 2012-09-13 02:11:15 +0000 | [diff] [blame] | 2126 | using namespace analyze_format_string; | 
|  | 2127 |  | 
|  | 2128 | // See if we know how to fix this conversion specifier. | 
|  | 2129 | llvm::Optional<ConversionSpecifier> FixedCS = CS.getStandardSpecifier(); | 
|  | 2130 | if (FixedCS) { | 
|  | 2131 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) | 
|  | 2132 | << CS.toString() << /*conversion specifier*/1, | 
|  | 2133 | getLocationOfByte(CS.getStart()), | 
|  | 2134 | /*IsStringLocation*/true, | 
|  | 2135 | getSpecifierRange(startSpecifier, specifierLen)); | 
|  | 2136 |  | 
|  | 2137 | CharSourceRange CSRange = getSpecifierRange(CS.getStart(), CS.getLength()); | 
|  | 2138 | S.Diag(getLocationOfByte(CS.getStart()), diag::note_format_fix_specifier) | 
|  | 2139 | << FixedCS->toString() | 
|  | 2140 | << FixItHint::CreateReplacement(CSRange, FixedCS->toString()); | 
|  | 2141 | } else { | 
|  | 2142 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard) | 
|  | 2143 | << CS.toString() << /*conversion specifier*/1, | 
|  | 2144 | getLocationOfByte(CS.getStart()), | 
|  | 2145 | /*IsStringLocation*/true, | 
|  | 2146 | getSpecifierRange(startSpecifier, specifierLen)); | 
|  | 2147 | } | 
| Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2148 | } | 
|  | 2149 |  | 
| Hans Wennborg | f856264 | 2012-03-09 10:10:54 +0000 | [diff] [blame] | 2150 | void CheckFormatHandler::HandlePosition(const char *startPos, | 
|  | 2151 | unsigned posLen) { | 
|  | 2152 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_non_standard_positional_arg), | 
|  | 2153 | getLocationOfByte(startPos), | 
|  | 2154 | /*IsStringLocation*/true, | 
|  | 2155 | getSpecifierRange(startPos, posLen)); | 
|  | 2156 | } | 
|  | 2157 |  | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2158 | void | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2159 | CheckFormatHandler::HandleInvalidPosition(const char *startPos, unsigned posLen, | 
|  | 2160 | analyze_format_string::PositionContext p) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2161 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_positional_specifier) | 
|  | 2162 | << (unsigned) p, | 
|  | 2163 | getLocationOfByte(startPos), /*IsStringLocation*/true, | 
|  | 2164 | getSpecifierRange(startPos, posLen)); | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2165 | } | 
|  | 2166 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2167 | void CheckFormatHandler::HandleZeroPosition(const char *startPos, | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2168 | unsigned posLen) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2169 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_zero_positional_specifier), | 
|  | 2170 | getLocationOfByte(startPos), | 
|  | 2171 | /*IsStringLocation*/true, | 
|  | 2172 | getSpecifierRange(startPos, posLen)); | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2173 | } | 
|  | 2174 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2175 | void CheckFormatHandler::HandleNullChar(const char *nullCharacter) { | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2176 | if (!isa<ObjCStringLiteral>(OrigFormatExpr)) { | 
| Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2177 | // The presence of a null character is likely an error. | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2178 | EmitFormatDiagnostic( | 
|  | 2179 | S.PDiag(diag::warn_printf_format_string_contains_null_char), | 
|  | 2180 | getLocationOfByte(nullCharacter), /*IsStringLocation*/true, | 
|  | 2181 | getFormatStringRange()); | 
| Ted Kremenek | 0c06944 | 2011-03-15 21:18:48 +0000 | [diff] [blame] | 2182 | } | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2183 | } | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2184 |  | 
| Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2185 | // Note that this may return NULL if there was an error parsing or building | 
|  | 2186 | // one of the argument expressions. | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2187 | const Expr *CheckFormatHandler::getDataArg(unsigned i) const { | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2188 | return Args[FirstDataArg + i]; | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2189 | } | 
|  | 2190 |  | 
|  | 2191 | void CheckFormatHandler::DoneProcessing() { | 
|  | 2192 | // Does the number of data arguments exceed the number of | 
|  | 2193 | // format conversions in the format string? | 
|  | 2194 | if (!HasVAListArg) { | 
|  | 2195 | // Find any arguments that weren't covered. | 
|  | 2196 | CoveredArgs.flip(); | 
|  | 2197 | signed notCoveredArg = CoveredArgs.find_first(); | 
|  | 2198 | if (notCoveredArg >= 0) { | 
|  | 2199 | assert((unsigned)notCoveredArg < NumDataArgs); | 
| Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2200 | if (const Expr *E = getDataArg((unsigned) notCoveredArg)) { | 
|  | 2201 | SourceLocation Loc = E->getLocStart(); | 
|  | 2202 | if (!S.getSourceManager().isInSystemMacro(Loc)) { | 
|  | 2203 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_data_arg_not_used), | 
|  | 2204 | Loc, /*IsStringLocation*/false, | 
|  | 2205 | getFormatStringRange()); | 
|  | 2206 | } | 
| Bob Wilson | c03f2df | 2012-05-03 19:47:19 +0000 | [diff] [blame] | 2207 | } | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2208 | } | 
|  | 2209 | } | 
|  | 2210 | } | 
|  | 2211 |  | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2212 | bool | 
|  | 2213 | CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex, | 
|  | 2214 | SourceLocation Loc, | 
|  | 2215 | const char *startSpec, | 
|  | 2216 | unsigned specifierLen, | 
|  | 2217 | const char *csStart, | 
|  | 2218 | unsigned csLen) { | 
|  | 2219 |  | 
|  | 2220 | bool keepGoing = true; | 
|  | 2221 | if (argIndex < NumDataArgs) { | 
|  | 2222 | // Consider the argument coverered, even though the specifier doesn't | 
|  | 2223 | // make sense. | 
|  | 2224 | CoveredArgs.set(argIndex); | 
|  | 2225 | } | 
|  | 2226 | else { | 
|  | 2227 | // If argIndex exceeds the number of data arguments we | 
|  | 2228 | // don't issue a warning because that is just a cascade of warnings (and | 
|  | 2229 | // they may have intended '%%' anyway). We don't want to continue processing | 
|  | 2230 | // the format string after this point, however, as we will like just get | 
|  | 2231 | // gibberish when trying to match arguments. | 
|  | 2232 | keepGoing = false; | 
|  | 2233 | } | 
|  | 2234 |  | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2235 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_invalid_conversion) | 
|  | 2236 | << StringRef(csStart, csLen), | 
|  | 2237 | Loc, /*IsStringLocation*/true, | 
|  | 2238 | getSpecifierRange(startSpec, specifierLen)); | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2239 |  | 
|  | 2240 | return keepGoing; | 
|  | 2241 | } | 
|  | 2242 |  | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2243 | void | 
|  | 2244 | CheckFormatHandler::HandlePositionalNonpositionalArgs(SourceLocation Loc, | 
|  | 2245 | const char *startSpec, | 
|  | 2246 | unsigned specifierLen) { | 
|  | 2247 | EmitFormatDiagnostic( | 
|  | 2248 | S.PDiag(diag::warn_format_mix_positional_nonpositional_args), | 
|  | 2249 | Loc, /*isStringLoc*/true, getSpecifierRange(startSpec, specifierLen)); | 
|  | 2250 | } | 
|  | 2251 |  | 
| Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2252 | bool | 
|  | 2253 | CheckFormatHandler::CheckNumArgs( | 
|  | 2254 | const analyze_format_string::FormatSpecifier &FS, | 
|  | 2255 | const analyze_format_string::ConversionSpecifier &CS, | 
|  | 2256 | const char *startSpecifier, unsigned specifierLen, unsigned argIndex) { | 
|  | 2257 |  | 
|  | 2258 | if (argIndex >= NumDataArgs) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2259 | PartialDiagnostic PDiag = FS.usesPositionalArg() | 
|  | 2260 | ? (S.PDiag(diag::warn_printf_positional_arg_exceeds_data_args) | 
|  | 2261 | << (argIndex+1) << NumDataArgs) | 
|  | 2262 | : S.PDiag(diag::warn_printf_insufficient_data_args); | 
|  | 2263 | EmitFormatDiagnostic( | 
|  | 2264 | PDiag, getLocationOfByte(CS.getStart()), /*IsStringLocation*/true, | 
|  | 2265 | getSpecifierRange(startSpecifier, specifierLen)); | 
| Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2266 | return false; | 
|  | 2267 | } | 
|  | 2268 | return true; | 
|  | 2269 | } | 
|  | 2270 |  | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2271 | template<typename Range> | 
|  | 2272 | void CheckFormatHandler::EmitFormatDiagnostic(PartialDiagnostic PDiag, | 
|  | 2273 | SourceLocation Loc, | 
|  | 2274 | bool IsStringLocation, | 
|  | 2275 | Range StringRange, | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2276 | ArrayRef<FixItHint> FixIt) { | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2277 | EmitFormatDiagnostic(S, inFunctionCall, Args[FormatIdx], PDiag, | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2278 | Loc, IsStringLocation, StringRange, FixIt); | 
|  | 2279 | } | 
|  | 2280 |  | 
|  | 2281 | /// \brief If the format string is not within the funcion call, emit a note | 
|  | 2282 | /// so that the function call and string are in diagnostic messages. | 
|  | 2283 | /// | 
| Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2284 | /// \param InFunctionCall if true, the format string is within the function | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2285 | /// call and only one diagnostic message will be produced.  Otherwise, an | 
|  | 2286 | /// extra note will be emitted pointing to location of the format string. | 
|  | 2287 | /// | 
|  | 2288 | /// \param ArgumentExpr the expression that is passed as the format string | 
|  | 2289 | /// argument in the function call.  Used for getting locations when two | 
|  | 2290 | /// diagnostics are emitted. | 
|  | 2291 | /// | 
|  | 2292 | /// \param PDiag the callee should already have provided any strings for the | 
|  | 2293 | /// diagnostic message.  This function only adds locations and fixits | 
|  | 2294 | /// to diagnostics. | 
|  | 2295 | /// | 
|  | 2296 | /// \param Loc primary location for diagnostic.  If two diagnostics are | 
|  | 2297 | /// required, one will be at Loc and a new SourceLocation will be created for | 
|  | 2298 | /// the other one. | 
|  | 2299 | /// | 
|  | 2300 | /// \param IsStringLocation if true, Loc points to the format string should be | 
|  | 2301 | /// used for the note.  Otherwise, Loc points to the argument list and will | 
|  | 2302 | /// be used with PDiag. | 
|  | 2303 | /// | 
|  | 2304 | /// \param StringRange some or all of the string to highlight.  This is | 
|  | 2305 | /// templated so it can accept either a CharSourceRange or a SourceRange. | 
|  | 2306 | /// | 
| Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 2307 | /// \param FixIt optional fix it hint for the format string. | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2308 | template<typename Range> | 
|  | 2309 | void CheckFormatHandler::EmitFormatDiagnostic(Sema &S, bool InFunctionCall, | 
|  | 2310 | const Expr *ArgumentExpr, | 
|  | 2311 | PartialDiagnostic PDiag, | 
|  | 2312 | SourceLocation Loc, | 
|  | 2313 | bool IsStringLocation, | 
|  | 2314 | Range StringRange, | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2315 | ArrayRef<FixItHint> FixIt) { | 
|  | 2316 | if (InFunctionCall) { | 
|  | 2317 | const Sema::SemaDiagnosticBuilder &D = S.Diag(Loc, PDiag); | 
|  | 2318 | D << StringRange; | 
|  | 2319 | for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end(); | 
|  | 2320 | I != E; ++I) { | 
|  | 2321 | D << *I; | 
|  | 2322 | } | 
|  | 2323 | } else { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2324 | S.Diag(IsStringLocation ? ArgumentExpr->getExprLoc() : Loc, PDiag) | 
|  | 2325 | << ArgumentExpr->getSourceRange(); | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2326 |  | 
|  | 2327 | const Sema::SemaDiagnosticBuilder &Note = | 
|  | 2328 | S.Diag(IsStringLocation ? Loc : StringRange.getBegin(), | 
|  | 2329 | diag::note_format_string_defined); | 
|  | 2330 |  | 
|  | 2331 | Note << StringRange; | 
|  | 2332 | for (ArrayRef<FixItHint>::iterator I = FixIt.begin(), E = FixIt.end(); | 
|  | 2333 | I != E; ++I) { | 
|  | 2334 | Note << *I; | 
|  | 2335 | } | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2336 | } | 
|  | 2337 | } | 
|  | 2338 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2339 | //===--- CHECK: Printf format string checking ------------------------------===// | 
|  | 2340 |  | 
|  | 2341 | namespace { | 
|  | 2342 | class CheckPrintfHandler : public CheckFormatHandler { | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2343 | bool ObjCContext; | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2344 | public: | 
|  | 2345 | CheckPrintfHandler(Sema &s, const StringLiteral *fexpr, | 
|  | 2346 | const Expr *origFormatExpr, unsigned firstDataArg, | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2347 | unsigned numDataArgs, bool isObjC, | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2348 | const char *beg, bool hasVAListArg, | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2349 | Expr **Args, unsigned NumArgs, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2350 | unsigned formatIdx, bool inFunctionCall, | 
|  | 2351 | Sema::VariadicCallType CallType) | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2352 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2353 | numDataArgs, beg, hasVAListArg, Args, NumArgs, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2354 | formatIdx, inFunctionCall, CallType), ObjCContext(isObjC) | 
|  | 2355 | {} | 
|  | 2356 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2357 |  | 
|  | 2358 | bool HandleInvalidPrintfConversionSpecifier( | 
|  | 2359 | const analyze_printf::PrintfSpecifier &FS, | 
|  | 2360 | const char *startSpecifier, | 
|  | 2361 | unsigned specifierLen); | 
|  | 2362 |  | 
|  | 2363 | bool HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier &FS, | 
|  | 2364 | const char *startSpecifier, | 
|  | 2365 | unsigned specifierLen); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2366 | bool checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, | 
|  | 2367 | const char *StartSpecifier, | 
|  | 2368 | unsigned SpecifierLen, | 
|  | 2369 | const Expr *E); | 
|  | 2370 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2371 | bool HandleAmount(const analyze_format_string::OptionalAmount &Amt, unsigned k, | 
|  | 2372 | const char *startSpecifier, unsigned specifierLen); | 
|  | 2373 | void HandleInvalidAmount(const analyze_printf::PrintfSpecifier &FS, | 
|  | 2374 | const analyze_printf::OptionalAmount &Amt, | 
|  | 2375 | unsigned type, | 
|  | 2376 | const char *startSpecifier, unsigned specifierLen); | 
|  | 2377 | void HandleFlag(const analyze_printf::PrintfSpecifier &FS, | 
|  | 2378 | const analyze_printf::OptionalFlag &flag, | 
|  | 2379 | const char *startSpecifier, unsigned specifierLen); | 
|  | 2380 | void HandleIgnoredFlag(const analyze_printf::PrintfSpecifier &FS, | 
|  | 2381 | const analyze_printf::OptionalFlag &ignoredFlag, | 
|  | 2382 | const analyze_printf::OptionalFlag &flag, | 
|  | 2383 | const char *startSpecifier, unsigned specifierLen); | 
| Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2384 | bool checkForCStrMembers(const analyze_printf::ArgType &AT, | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2385 | const Expr *E, const CharSourceRange &CSR); | 
|  | 2386 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2387 | }; | 
|  | 2388 | } | 
|  | 2389 |  | 
|  | 2390 | bool CheckPrintfHandler::HandleInvalidPrintfConversionSpecifier( | 
|  | 2391 | const analyze_printf::PrintfSpecifier &FS, | 
|  | 2392 | const char *startSpecifier, | 
|  | 2393 | unsigned specifierLen) { | 
| Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2394 | const analyze_printf::PrintfConversionSpecifier &CS = | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2395 | FS.getConversionSpecifier(); | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2396 |  | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2397 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), | 
|  | 2398 | getLocationOfByte(CS.getStart()), | 
|  | 2399 | startSpecifier, specifierLen, | 
|  | 2400 | CS.getStart(), CS.getLength()); | 
| Ted Kremenek | 26ac2e0 | 2010-01-29 02:40:24 +0000 | [diff] [blame] | 2401 | } | 
|  | 2402 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2403 | bool CheckPrintfHandler::HandleAmount( | 
|  | 2404 | const analyze_format_string::OptionalAmount &Amt, | 
|  | 2405 | unsigned k, const char *startSpecifier, | 
|  | 2406 | unsigned specifierLen) { | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2407 |  | 
|  | 2408 | if (Amt.hasDataArgument()) { | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2409 | if (!HasVAListArg) { | 
| Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2410 | unsigned argIndex = Amt.getArgIndex(); | 
|  | 2411 | if (argIndex >= NumDataArgs) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2412 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_missing_arg) | 
|  | 2413 | << k, | 
|  | 2414 | getLocationOfByte(Amt.getStart()), | 
|  | 2415 | /*IsStringLocation*/true, | 
|  | 2416 | getSpecifierRange(startSpecifier, specifierLen)); | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2417 | // Don't do any more checking.  We will just emit | 
|  | 2418 | // spurious errors. | 
|  | 2419 | return false; | 
|  | 2420 | } | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2421 |  | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2422 | // Type check the data argument.  It should be an 'int'. | 
| Ted Kremenek | 31f8e32 | 2010-01-29 23:32:22 +0000 | [diff] [blame] | 2423 | // Although not in conformance with C99, we also allow the argument to be | 
|  | 2424 | // an 'unsigned int' as that is a reasonably safe case.  GCC also | 
|  | 2425 | // doesn't emit a warning for that case. | 
| Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2426 | CoveredArgs.set(argIndex); | 
|  | 2427 | const Expr *Arg = getDataArg(argIndex); | 
| Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2428 | if (!Arg) | 
|  | 2429 | return false; | 
|  | 2430 |  | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2431 | QualType T = Arg->getType(); | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2432 |  | 
| Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2433 | const analyze_printf::ArgType &AT = Amt.getArgType(S.Context); | 
|  | 2434 | assert(AT.isValid()); | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2435 |  | 
| Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2436 | if (!AT.matchesType(S.Context, T)) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2437 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type) | 
| Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2438 | << k << AT.getRepresentativeTypeName(S.Context) | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2439 | << T << Arg->getSourceRange(), | 
|  | 2440 | getLocationOfByte(Amt.getStart()), | 
|  | 2441 | /*IsStringLocation*/true, | 
|  | 2442 | getSpecifierRange(startSpecifier, specifierLen)); | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2443 | // Don't do any more checking.  We will just emit | 
|  | 2444 | // spurious errors. | 
|  | 2445 | return false; | 
|  | 2446 | } | 
|  | 2447 | } | 
|  | 2448 | } | 
|  | 2449 | return true; | 
|  | 2450 | } | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2451 |  | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2452 | void CheckPrintfHandler::HandleInvalidAmount( | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2453 | const analyze_printf::PrintfSpecifier &FS, | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2454 | const analyze_printf::OptionalAmount &Amt, | 
|  | 2455 | unsigned type, | 
|  | 2456 | const char *startSpecifier, | 
|  | 2457 | unsigned specifierLen) { | 
| Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2458 | const analyze_printf::PrintfConversionSpecifier &CS = | 
|  | 2459 | FS.getConversionSpecifier(); | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2460 |  | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2461 | FixItHint fixit = | 
|  | 2462 | Amt.getHowSpecified() == analyze_printf::OptionalAmount::Constant | 
|  | 2463 | ? FixItHint::CreateRemoval(getSpecifierRange(Amt.getStart(), | 
|  | 2464 | Amt.getConstantLength())) | 
|  | 2465 | : FixItHint(); | 
|  | 2466 |  | 
|  | 2467 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_optional_amount) | 
|  | 2468 | << type << CS.toString(), | 
|  | 2469 | getLocationOfByte(Amt.getStart()), | 
|  | 2470 | /*IsStringLocation*/true, | 
|  | 2471 | getSpecifierRange(startSpecifier, specifierLen), | 
|  | 2472 | fixit); | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2473 | } | 
|  | 2474 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2475 | void CheckPrintfHandler::HandleFlag(const analyze_printf::PrintfSpecifier &FS, | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2476 | const analyze_printf::OptionalFlag &flag, | 
|  | 2477 | const char *startSpecifier, | 
|  | 2478 | unsigned specifierLen) { | 
|  | 2479 | // Warn about pointless flag with a fixit removal. | 
| Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2480 | const analyze_printf::PrintfConversionSpecifier &CS = | 
|  | 2481 | FS.getConversionSpecifier(); | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2482 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_nonsensical_flag) | 
|  | 2483 | << flag.toString() << CS.toString(), | 
|  | 2484 | getLocationOfByte(flag.getPosition()), | 
|  | 2485 | /*IsStringLocation*/true, | 
|  | 2486 | getSpecifierRange(startSpecifier, specifierLen), | 
|  | 2487 | FixItHint::CreateRemoval( | 
|  | 2488 | getSpecifierRange(flag.getPosition(), 1))); | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2489 | } | 
|  | 2490 |  | 
|  | 2491 | void CheckPrintfHandler::HandleIgnoredFlag( | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2492 | const analyze_printf::PrintfSpecifier &FS, | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2493 | const analyze_printf::OptionalFlag &ignoredFlag, | 
|  | 2494 | const analyze_printf::OptionalFlag &flag, | 
|  | 2495 | const char *startSpecifier, | 
|  | 2496 | unsigned specifierLen) { | 
|  | 2497 | // Warn about ignored flag with a fixit removal. | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2498 | EmitFormatDiagnostic(S.PDiag(diag::warn_printf_ignored_flag) | 
|  | 2499 | << ignoredFlag.toString() << flag.toString(), | 
|  | 2500 | getLocationOfByte(ignoredFlag.getPosition()), | 
|  | 2501 | /*IsStringLocation*/true, | 
|  | 2502 | getSpecifierRange(startSpecifier, specifierLen), | 
|  | 2503 | FixItHint::CreateRemoval( | 
|  | 2504 | getSpecifierRange(ignoredFlag.getPosition(), 1))); | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2505 | } | 
|  | 2506 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2507 | // Determines if the specified is a C++ class or struct containing | 
|  | 2508 | // a member with the specified name and kind (e.g. a CXXMethodDecl named | 
|  | 2509 | // "c_str()"). | 
|  | 2510 | template<typename MemberKind> | 
|  | 2511 | static llvm::SmallPtrSet<MemberKind*, 1> | 
|  | 2512 | CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) { | 
|  | 2513 | const RecordType *RT = Ty->getAs<RecordType>(); | 
|  | 2514 | llvm::SmallPtrSet<MemberKind*, 1> Results; | 
|  | 2515 |  | 
|  | 2516 | if (!RT) | 
|  | 2517 | return Results; | 
|  | 2518 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 2519 | if (!RD) | 
|  | 2520 | return Results; | 
|  | 2521 |  | 
|  | 2522 | LookupResult R(S, &S.PP.getIdentifierTable().get(Name), SourceLocation(), | 
|  | 2523 | Sema::LookupMemberName); | 
|  | 2524 |  | 
|  | 2525 | // We just need to include all members of the right kind turned up by the | 
|  | 2526 | // filter, at this point. | 
|  | 2527 | if (S.LookupQualifiedName(R, RT->getDecl())) | 
|  | 2528 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { | 
|  | 2529 | NamedDecl *decl = (*I)->getUnderlyingDecl(); | 
|  | 2530 | if (MemberKind *FK = dyn_cast<MemberKind>(decl)) | 
|  | 2531 | Results.insert(FK); | 
|  | 2532 | } | 
|  | 2533 | return Results; | 
|  | 2534 | } | 
|  | 2535 |  | 
|  | 2536 | // Check if a (w)string was passed when a (w)char* was needed, and offer a | 
| Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2537 | // better diagnostic if so. AT is assumed to be valid. | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2538 | // Returns true when a c_str() conversion method is found. | 
|  | 2539 | bool CheckPrintfHandler::checkForCStrMembers( | 
| Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2540 | const analyze_printf::ArgType &AT, const Expr *E, | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2541 | const CharSourceRange &CSR) { | 
|  | 2542 | typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet; | 
|  | 2543 |  | 
|  | 2544 | MethodSet Results = | 
|  | 2545 | CXXRecordMembersNamed<CXXMethodDecl>("c_str", S, E->getType()); | 
|  | 2546 |  | 
|  | 2547 | for (MethodSet::iterator MI = Results.begin(), ME = Results.end(); | 
|  | 2548 | MI != ME; ++MI) { | 
|  | 2549 | const CXXMethodDecl *Method = *MI; | 
|  | 2550 | if (Method->getNumParams() == 0 && | 
| Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2551 | AT.matchesType(S.Context, Method->getResultType())) { | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2552 | // FIXME: Suggest parens if the expression needs them. | 
|  | 2553 | SourceLocation EndLoc = | 
|  | 2554 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()); | 
|  | 2555 | S.Diag(E->getLocStart(), diag::note_printf_c_str) | 
|  | 2556 | << "c_str()" | 
|  | 2557 | << FixItHint::CreateInsertion(EndLoc, ".c_str()"); | 
|  | 2558 | return true; | 
|  | 2559 | } | 
|  | 2560 | } | 
|  | 2561 |  | 
|  | 2562 | return false; | 
|  | 2563 | } | 
|  | 2564 |  | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2565 | bool | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2566 | CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier | 
| Ted Kremenek | 5c41ee8 | 2010-02-11 09:27:41 +0000 | [diff] [blame] | 2567 | &FS, | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2568 | const char *startSpecifier, | 
|  | 2569 | unsigned specifierLen) { | 
|  | 2570 |  | 
| Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2571 | using namespace analyze_format_string; | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2572 | using namespace analyze_printf; | 
| Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2573 | const PrintfConversionSpecifier &CS = FS.getConversionSpecifier(); | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2574 |  | 
| Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2575 | if (FS.consumesDataArgument()) { | 
|  | 2576 | if (atFirstArg) { | 
|  | 2577 | atFirstArg = false; | 
|  | 2578 | usesPositionalArgs = FS.usesPositionalArg(); | 
|  | 2579 | } | 
|  | 2580 | else if (usesPositionalArgs != FS.usesPositionalArg()) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2581 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), | 
|  | 2582 | startSpecifier, specifierLen); | 
| Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2583 | return false; | 
|  | 2584 | } | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2585 | } | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2586 |  | 
| Ted Kremenek | efaff19 | 2010-02-27 01:41:03 +0000 | [diff] [blame] | 2587 | // First check if the field width, precision, and conversion specifier | 
|  | 2588 | // have matching data arguments. | 
|  | 2589 | if (!HandleAmount(FS.getFieldWidth(), /* field width */ 0, | 
|  | 2590 | startSpecifier, specifierLen)) { | 
|  | 2591 | return false; | 
|  | 2592 | } | 
|  | 2593 |  | 
|  | 2594 | if (!HandleAmount(FS.getPrecision(), /* precision */ 1, | 
|  | 2595 | startSpecifier, specifierLen)) { | 
| Ted Kremenek | 0d27735 | 2010-01-29 01:06:55 +0000 | [diff] [blame] | 2596 | return false; | 
|  | 2597 | } | 
|  | 2598 |  | 
| Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2599 | if (!CS.consumesDataArgument()) { | 
|  | 2600 | // FIXME: Technically specifying a precision or field width here | 
|  | 2601 | // makes no sense.  Worth issuing a warning at some point. | 
| Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 2602 | return true; | 
| Ted Kremenek | f88c8e0 | 2010-01-29 20:55:36 +0000 | [diff] [blame] | 2603 | } | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2604 |  | 
| Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2605 | // Consume the argument. | 
|  | 2606 | unsigned argIndex = FS.getArgIndex(); | 
| Ted Kremenek | e3fc547 | 2010-02-27 08:34:51 +0000 | [diff] [blame] | 2607 | if (argIndex < NumDataArgs) { | 
|  | 2608 | // The check to see if the argIndex is valid will come later. | 
|  | 2609 | // We set the bit here because we may exit early from this | 
|  | 2610 | // function if we encounter some other error. | 
|  | 2611 | CoveredArgs.set(argIndex); | 
|  | 2612 | } | 
| Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2613 |  | 
|  | 2614 | // Check for using an Objective-C specific conversion specifier | 
|  | 2615 | // in a non-ObjC literal. | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2616 | if (!ObjCContext && CS.isObjCArg()) { | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2617 | return HandleInvalidPrintfConversionSpecifier(FS, startSpecifier, | 
|  | 2618 | specifierLen); | 
| Ted Kremenek | 7f70dc8 | 2010-02-26 19:18:41 +0000 | [diff] [blame] | 2619 | } | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2620 |  | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2621 | // Check for invalid use of field width | 
|  | 2622 | if (!FS.hasValidFieldWidth()) { | 
| Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2623 | HandleInvalidAmount(FS, FS.getFieldWidth(), /* field width */ 0, | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2624 | startSpecifier, specifierLen); | 
|  | 2625 | } | 
|  | 2626 |  | 
|  | 2627 | // Check for invalid use of precision | 
|  | 2628 | if (!FS.hasValidPrecision()) { | 
|  | 2629 | HandleInvalidAmount(FS, FS.getPrecision(), /* precision */ 1, | 
|  | 2630 | startSpecifier, specifierLen); | 
|  | 2631 | } | 
|  | 2632 |  | 
|  | 2633 | // Check each flag does not conflict with any other component. | 
| Ted Kremenek | 65197b4 | 2011-01-08 05:28:46 +0000 | [diff] [blame] | 2634 | if (!FS.hasValidThousandsGroupingPrefix()) | 
|  | 2635 | HandleFlag(FS, FS.hasThousandsGrouping(), startSpecifier, specifierLen); | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2636 | if (!FS.hasValidLeadingZeros()) | 
|  | 2637 | HandleFlag(FS, FS.hasLeadingZeros(), startSpecifier, specifierLen); | 
|  | 2638 | if (!FS.hasValidPlusPrefix()) | 
|  | 2639 | HandleFlag(FS, FS.hasPlusPrefix(), startSpecifier, specifierLen); | 
| Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2640 | if (!FS.hasValidSpacePrefix()) | 
|  | 2641 | HandleFlag(FS, FS.hasSpacePrefix(), startSpecifier, specifierLen); | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2642 | if (!FS.hasValidAlternativeForm()) | 
|  | 2643 | HandleFlag(FS, FS.hasAlternativeForm(), startSpecifier, specifierLen); | 
|  | 2644 | if (!FS.hasValidLeftJustified()) | 
|  | 2645 | HandleFlag(FS, FS.isLeftJustified(), startSpecifier, specifierLen); | 
|  | 2646 |  | 
|  | 2647 | // Check that flags are not ignored by another flag | 
| Tom Care | 45f9b7e | 2010-06-21 21:21:01 +0000 | [diff] [blame] | 2648 | if (FS.hasSpacePrefix() && FS.hasPlusPrefix()) // ' ' ignored by '+' | 
|  | 2649 | HandleIgnoredFlag(FS, FS.hasSpacePrefix(), FS.hasPlusPrefix(), | 
|  | 2650 | startSpecifier, specifierLen); | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2651 | if (FS.hasLeadingZeros() && FS.isLeftJustified()) // '0' ignored by '-' | 
|  | 2652 | HandleIgnoredFlag(FS, FS.hasLeadingZeros(), FS.isLeftJustified(), | 
|  | 2653 | startSpecifier, specifierLen); | 
|  | 2654 |  | 
|  | 2655 | // Check the length modifier is valid with the given conversion specifier. | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2656 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2657 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, | 
|  | 2658 | diag::warn_format_nonsensical_length); | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2659 | else if (!FS.hasStandardLengthModifier()) | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2660 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2661 | else if (!FS.hasStandardLengthConversionCombination()) | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2662 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, | 
|  | 2663 | diag::warn_format_non_standard_conversion_spec); | 
| Tom Care | e4ee966 | 2010-06-17 19:00:27 +0000 | [diff] [blame] | 2664 |  | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2665 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) | 
|  | 2666 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); | 
|  | 2667 |  | 
| Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2668 | // The remaining checks depend on the data arguments. | 
|  | 2669 | if (HasVAListArg) | 
|  | 2670 | return true; | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2671 |  | 
| Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2672 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) | 
| Ted Kremenek | da51f0d | 2010-01-29 01:43:31 +0000 | [diff] [blame] | 2673 | return false; | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 2674 |  | 
| Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2675 | const Expr *Arg = getDataArg(argIndex); | 
|  | 2676 | if (!Arg) | 
|  | 2677 | return true; | 
|  | 2678 |  | 
|  | 2679 | return checkFormatExpr(FS, startSpecifier, specifierLen, Arg); | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2680 | } | 
|  | 2681 |  | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2682 | static bool requiresParensToAddCast(const Expr *E) { | 
|  | 2683 | // FIXME: We should have a general way to reason about operator | 
|  | 2684 | // precedence and whether parens are actually needed here. | 
|  | 2685 | // Take care of a few common cases where they aren't. | 
|  | 2686 | const Expr *Inside = E->IgnoreImpCasts(); | 
|  | 2687 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(Inside)) | 
|  | 2688 | Inside = POE->getSyntacticForm()->IgnoreImpCasts(); | 
|  | 2689 |  | 
|  | 2690 | switch (Inside->getStmtClass()) { | 
|  | 2691 | case Stmt::ArraySubscriptExprClass: | 
|  | 2692 | case Stmt::CallExprClass: | 
|  | 2693 | case Stmt::DeclRefExprClass: | 
|  | 2694 | case Stmt::MemberExprClass: | 
|  | 2695 | case Stmt::ObjCIvarRefExprClass: | 
|  | 2696 | case Stmt::ObjCMessageExprClass: | 
|  | 2697 | case Stmt::ObjCPropertyRefExprClass: | 
|  | 2698 | case Stmt::ParenExprClass: | 
|  | 2699 | case Stmt::UnaryOperatorClass: | 
|  | 2700 | return false; | 
|  | 2701 | default: | 
|  | 2702 | return true; | 
|  | 2703 | } | 
|  | 2704 | } | 
|  | 2705 |  | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2706 | bool | 
|  | 2707 | CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, | 
|  | 2708 | const char *StartSpecifier, | 
|  | 2709 | unsigned SpecifierLen, | 
|  | 2710 | const Expr *E) { | 
|  | 2711 | using namespace analyze_format_string; | 
|  | 2712 | using namespace analyze_printf; | 
| Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2713 | // Now type check the data expression that matches the | 
|  | 2714 | // format specifier. | 
| Hans Wennborg | f3749f4 | 2012-08-07 08:11:26 +0000 | [diff] [blame] | 2715 | const analyze_printf::ArgType &AT = FS.getArgType(S.Context, | 
|  | 2716 | ObjCContext); | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2717 | if (!AT.isValid()) | 
|  | 2718 | return true; | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2719 |  | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2720 | QualType ExprTy = E->getType(); | 
|  | 2721 | if (AT.matchesType(S.Context, ExprTy)) | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2722 | return true; | 
| Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2723 |  | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2724 | // Look through argument promotions for our error message's reported type. | 
|  | 2725 | // This includes the integral and floating promotions, but excludes array | 
|  | 2726 | // and function pointer decay; seeing that an argument intended to be a | 
|  | 2727 | // string has type 'char [6]' is probably more confusing than 'char *'. | 
|  | 2728 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { | 
|  | 2729 | if (ICE->getCastKind() == CK_IntegralCast || | 
|  | 2730 | ICE->getCastKind() == CK_FloatingCast) { | 
|  | 2731 | E = ICE->getSubExpr(); | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2732 | ExprTy = E->getType(); | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2733 |  | 
|  | 2734 | // Check if we didn't match because of an implicit cast from a 'char' | 
|  | 2735 | // or 'short' to an 'int'.  This is done because printf is a varargs | 
|  | 2736 | // function. | 
|  | 2737 | if (ICE->getType() == S.Context.IntTy || | 
|  | 2738 | ICE->getType() == S.Context.UnsignedIntTy) { | 
|  | 2739 | // All further checking is done on the subexpression. | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2740 | if (AT.matchesType(S.Context, ExprTy)) | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2741 | return true; | 
| Ted Kremenek | 4d8ae4d | 2010-10-21 04:00:58 +0000 | [diff] [blame] | 2742 | } | 
| Jordan Rose | ee0259d | 2012-06-04 22:48:57 +0000 | [diff] [blame] | 2743 | } | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2744 | } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) { | 
|  | 2745 | // Special case for 'a', which has type 'int' in C. | 
|  | 2746 | // Note, however, that we do /not/ want to treat multibyte constants like | 
|  | 2747 | // 'MooV' as characters! This form is deprecated but still exists. | 
|  | 2748 | if (ExprTy == S.Context.IntTy) | 
|  | 2749 | if (llvm::isUIntN(S.Context.getCharWidth(), CL->getValue())) | 
|  | 2750 | ExprTy = S.Context.CharTy; | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2751 | } | 
| Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2752 |  | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2753 | QualType IntendedTy = ExprTy; | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2754 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { | 
|  | 2755 | // Special-case some of Darwin's platform-independence types. | 
|  | 2756 | if (const TypedefType *UserTy = IntendedTy->getAs<TypedefType>()) { | 
|  | 2757 | StringRef Name = UserTy->getDecl()->getName(); | 
|  | 2758 | IntendedTy = llvm::StringSwitch<QualType>(Name) | 
|  | 2759 | .Case("NSInteger", S.Context.LongTy) | 
|  | 2760 | .Case("NSUInteger", S.Context.UnsignedLongTy) | 
|  | 2761 | .Case("SInt32", S.Context.IntTy) | 
|  | 2762 | .Case("UInt32", S.Context.UnsignedIntTy) | 
|  | 2763 | .Default(IntendedTy); | 
|  | 2764 | } | 
|  | 2765 | } | 
|  | 2766 |  | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2767 | // We may be able to offer a FixItHint if it is a supported type. | 
|  | 2768 | PrintfSpecifier fixedFS = FS; | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2769 | bool success = fixedFS.fixType(IntendedTy, S.getLangOpts(), | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2770 | S.Context, ObjCContext); | 
| Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2771 |  | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2772 | if (success) { | 
|  | 2773 | // Get the fix string from the fixed format specifier | 
|  | 2774 | SmallString<16> buf; | 
|  | 2775 | llvm::raw_svector_ostream os(buf); | 
|  | 2776 | fixedFS.toString(os); | 
| Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2777 |  | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2778 | CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); | 
|  | 2779 |  | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2780 | if (IntendedTy != ExprTy) { | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2781 | // The canonical type for formatting this value is different from the | 
|  | 2782 | // actual type of the expression. (This occurs, for example, with Darwin's | 
|  | 2783 | // NSInteger on 32-bit platforms, where it is typedef'd as 'int', but | 
|  | 2784 | // should be printed as 'long' for 64-bit compatibility.) | 
|  | 2785 | // Rather than emitting a normal format/argument mismatch, we want to | 
|  | 2786 | // add a cast to the recommended type (and correct the format string | 
|  | 2787 | // if necessary). | 
|  | 2788 | SmallString<16> CastBuf; | 
|  | 2789 | llvm::raw_svector_ostream CastFix(CastBuf); | 
|  | 2790 | CastFix << "("; | 
|  | 2791 | IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); | 
|  | 2792 | CastFix << ")"; | 
|  | 2793 |  | 
|  | 2794 | SmallVector<FixItHint,4> Hints; | 
|  | 2795 | if (!AT.matchesType(S.Context, IntendedTy)) | 
|  | 2796 | Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); | 
|  | 2797 |  | 
|  | 2798 | if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) { | 
|  | 2799 | // If there's already a cast present, just replace it. | 
|  | 2800 | SourceRange CastRange(CCast->getLParenLoc(), CCast->getRParenLoc()); | 
|  | 2801 | Hints.push_back(FixItHint::CreateReplacement(CastRange, CastFix.str())); | 
|  | 2802 |  | 
|  | 2803 | } else if (!requiresParensToAddCast(E)) { | 
|  | 2804 | // If the expression has high enough precedence, | 
|  | 2805 | // just write the C-style cast. | 
|  | 2806 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), | 
|  | 2807 | CastFix.str())); | 
|  | 2808 | } else { | 
|  | 2809 | // Otherwise, add parens around the expression as well as the cast. | 
|  | 2810 | CastFix << "("; | 
|  | 2811 | Hints.push_back(FixItHint::CreateInsertion(E->getLocStart(), | 
|  | 2812 | CastFix.str())); | 
|  | 2813 |  | 
|  | 2814 | SourceLocation After = S.PP.getLocForEndOfToken(E->getLocEnd()); | 
|  | 2815 | Hints.push_back(FixItHint::CreateInsertion(After, ")")); | 
|  | 2816 | } | 
|  | 2817 |  | 
|  | 2818 | // We extract the name from the typedef because we don't want to show | 
|  | 2819 | // the underlying type in the diagnostic. | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2820 | const TypedefType *UserTy = cast<TypedefType>(ExprTy); | 
| Jordan Rose | ec08735 | 2012-09-05 22:56:26 +0000 | [diff] [blame] | 2821 | StringRef Name = UserTy->getDecl()->getName(); | 
|  | 2822 |  | 
|  | 2823 | // Finally, emit the diagnostic. | 
|  | 2824 | EmitFormatDiagnostic(S.PDiag(diag::warn_format_argument_needs_cast) | 
|  | 2825 | << Name << IntendedTy | 
|  | 2826 | << E->getSourceRange(), | 
|  | 2827 | E->getLocStart(), /*IsStringLocation=*/false, | 
|  | 2828 | SpecRange, Hints); | 
|  | 2829 | } else { | 
|  | 2830 | EmitFormatDiagnostic( | 
|  | 2831 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) | 
|  | 2832 | << AT.getRepresentativeTypeName(S.Context) << IntendedTy | 
|  | 2833 | << E->getSourceRange(), | 
|  | 2834 | E->getLocStart(), | 
|  | 2835 | /*IsStringLocation*/false, | 
|  | 2836 | SpecRange, | 
|  | 2837 | FixItHint::CreateReplacement(SpecRange, os.str())); | 
|  | 2838 | } | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2839 | } else { | 
|  | 2840 | const CharSourceRange &CSR = getSpecifierRange(StartSpecifier, | 
|  | 2841 | SpecifierLen); | 
|  | 2842 | // Since the warning for passing non-POD types to variadic functions | 
|  | 2843 | // was deferred until now, we emit a warning for non-POD | 
|  | 2844 | // arguments here. | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2845 | if (S.isValidVarArgType(ExprTy) == Sema::VAK_Invalid) { | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2846 | unsigned DiagKind; | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2847 | if (ExprTy->isObjCObjectType()) | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2848 | DiagKind = diag::err_cannot_pass_objc_interface_to_vararg_format; | 
|  | 2849 | else | 
|  | 2850 | DiagKind = diag::warn_non_pod_vararg_with_format_string; | 
|  | 2851 |  | 
|  | 2852 | EmitFormatDiagnostic( | 
|  | 2853 | S.PDiag(DiagKind) | 
|  | 2854 | << S.getLangOpts().CPlusPlus0x | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2855 | << ExprTy | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2856 | << CallType | 
|  | 2857 | << AT.getRepresentativeTypeName(S.Context) | 
|  | 2858 | << CSR | 
|  | 2859 | << E->getSourceRange(), | 
|  | 2860 | E->getLocStart(), /*IsStringLocation*/false, CSR); | 
|  | 2861 |  | 
|  | 2862 | checkForCStrMembers(AT, E, CSR); | 
|  | 2863 | } else | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2864 | EmitFormatDiagnostic( | 
|  | 2865 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) | 
| Jordan Rose | 448ac3e | 2012-12-05 18:44:40 +0000 | [diff] [blame^] | 2866 | << AT.getRepresentativeTypeName(S.Context) << ExprTy | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2867 | << CSR | 
| Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 2868 | << E->getSourceRange(), | 
| Jordan Rose | 614a865 | 2012-09-05 22:56:19 +0000 | [diff] [blame] | 2869 | E->getLocStart(), /*IsStringLocation*/false, CSR); | 
| Michael J. Spencer | 96827eb | 2010-07-27 04:46:02 +0000 | [diff] [blame] | 2870 | } | 
|  | 2871 |  | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2872 | return true; | 
|  | 2873 | } | 
|  | 2874 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2875 | //===--- CHECK: Scanf format string checking ------------------------------===// | 
|  | 2876 |  | 
|  | 2877 | namespace { | 
|  | 2878 | class CheckScanfHandler : public CheckFormatHandler { | 
|  | 2879 | public: | 
|  | 2880 | CheckScanfHandler(Sema &s, const StringLiteral *fexpr, | 
|  | 2881 | const Expr *origFormatExpr, unsigned firstDataArg, | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2882 | unsigned numDataArgs, const char *beg, bool hasVAListArg, | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 2883 | Expr **Args, unsigned NumArgs, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2884 | unsigned formatIdx, bool inFunctionCall, | 
|  | 2885 | Sema::VariadicCallType CallType) | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2886 | : CheckFormatHandler(s, fexpr, origFormatExpr, firstDataArg, | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 2887 | numDataArgs, beg, hasVAListArg, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 2888 | Args, NumArgs, formatIdx, inFunctionCall, CallType) | 
|  | 2889 | {} | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2890 |  | 
|  | 2891 | bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS, | 
|  | 2892 | const char *startSpecifier, | 
|  | 2893 | unsigned specifierLen); | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2894 |  | 
|  | 2895 | bool HandleInvalidScanfConversionSpecifier( | 
|  | 2896 | const analyze_scanf::ScanfSpecifier &FS, | 
|  | 2897 | const char *startSpecifier, | 
|  | 2898 | unsigned specifierLen); | 
| Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2899 |  | 
|  | 2900 | void HandleIncompleteScanList(const char *start, const char *end); | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2901 | }; | 
| Ted Kremenek | 07d161f | 2010-01-29 01:50:07 +0000 | [diff] [blame] | 2902 | } | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 2903 |  | 
| Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2904 | void CheckScanfHandler::HandleIncompleteScanList(const char *start, | 
|  | 2905 | const char *end) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2906 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_scanlist_incomplete), | 
|  | 2907 | getLocationOfByte(end), /*IsStringLocation*/true, | 
|  | 2908 | getSpecifierRange(start, end - start)); | 
| Ted Kremenek | b7c2101 | 2010-07-16 18:28:03 +0000 | [diff] [blame] | 2909 | } | 
|  | 2910 |  | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2911 | bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier( | 
|  | 2912 | const analyze_scanf::ScanfSpecifier &FS, | 
|  | 2913 | const char *startSpecifier, | 
|  | 2914 | unsigned specifierLen) { | 
|  | 2915 |  | 
| Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2916 | const analyze_scanf::ScanfConversionSpecifier &CS = | 
| Ted Kremenek | c09b6a5 | 2010-07-19 21:25:57 +0000 | [diff] [blame] | 2917 | FS.getConversionSpecifier(); | 
|  | 2918 |  | 
|  | 2919 | return HandleInvalidConversionSpecifier(FS.getArgIndex(), | 
|  | 2920 | getLocationOfByte(CS.getStart()), | 
|  | 2921 | startSpecifier, specifierLen, | 
|  | 2922 | CS.getStart(), CS.getLength()); | 
|  | 2923 | } | 
|  | 2924 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2925 | bool CheckScanfHandler::HandleScanfSpecifier( | 
|  | 2926 | const analyze_scanf::ScanfSpecifier &FS, | 
|  | 2927 | const char *startSpecifier, | 
|  | 2928 | unsigned specifierLen) { | 
|  | 2929 |  | 
|  | 2930 | using namespace analyze_scanf; | 
|  | 2931 | using namespace analyze_format_string; | 
|  | 2932 |  | 
| Ted Kremenek | 6ecb950 | 2010-07-20 20:04:27 +0000 | [diff] [blame] | 2933 | const ScanfConversionSpecifier &CS = FS.getConversionSpecifier(); | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2934 |  | 
| Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2935 | // Handle case where '%' and '*' don't consume an argument.  These shouldn't | 
|  | 2936 | // be used to decide if we are using positional arguments consistently. | 
|  | 2937 | if (FS.consumesDataArgument()) { | 
|  | 2938 | if (atFirstArg) { | 
|  | 2939 | atFirstArg = false; | 
|  | 2940 | usesPositionalArgs = FS.usesPositionalArg(); | 
|  | 2941 | } | 
|  | 2942 | else if (usesPositionalArgs != FS.usesPositionalArg()) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2943 | HandlePositionalNonpositionalArgs(getLocationOfByte(CS.getStart()), | 
|  | 2944 | startSpecifier, specifierLen); | 
| Ted Kremenek | baa4006 | 2010-07-19 22:01:06 +0000 | [diff] [blame] | 2945 | return false; | 
|  | 2946 | } | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2947 | } | 
|  | 2948 |  | 
|  | 2949 | // Check if the field with is non-zero. | 
|  | 2950 | const OptionalAmount &Amt = FS.getFieldWidth(); | 
|  | 2951 | if (Amt.getHowSpecified() == OptionalAmount::Constant) { | 
|  | 2952 | if (Amt.getConstantAmount() == 0) { | 
|  | 2953 | const CharSourceRange &R = getSpecifierRange(Amt.getStart(), | 
|  | 2954 | Amt.getConstantLength()); | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 2955 | EmitFormatDiagnostic(S.PDiag(diag::warn_scanf_nonzero_width), | 
|  | 2956 | getLocationOfByte(Amt.getStart()), | 
|  | 2957 | /*IsStringLocation*/true, R, | 
|  | 2958 | FixItHint::CreateRemoval(R)); | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2959 | } | 
|  | 2960 | } | 
|  | 2961 |  | 
|  | 2962 | if (!FS.consumesDataArgument()) { | 
|  | 2963 | // FIXME: Technically specifying a precision or field width here | 
|  | 2964 | // makes no sense.  Worth issuing a warning at some point. | 
|  | 2965 | return true; | 
|  | 2966 | } | 
|  | 2967 |  | 
|  | 2968 | // Consume the argument. | 
|  | 2969 | unsigned argIndex = FS.getArgIndex(); | 
|  | 2970 | if (argIndex < NumDataArgs) { | 
|  | 2971 | // The check to see if the argIndex is valid will come later. | 
|  | 2972 | // We set the bit here because we may exit early from this | 
|  | 2973 | // function if we encounter some other error. | 
|  | 2974 | CoveredArgs.set(argIndex); | 
|  | 2975 | } | 
|  | 2976 |  | 
| Ted Kremenek | 1e51c20 | 2010-07-20 20:04:47 +0000 | [diff] [blame] | 2977 | // Check the length modifier is valid with the given conversion specifier. | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2978 | if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo())) | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2979 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, | 
|  | 2980 | diag::warn_format_nonsensical_length); | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2981 | else if (!FS.hasStandardLengthModifier()) | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2982 | HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2983 | else if (!FS.hasStandardLengthConversionCombination()) | 
| Jordan Rose | 8be066e | 2012-09-08 04:00:12 +0000 | [diff] [blame] | 2984 | HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, | 
|  | 2985 | diag::warn_format_non_standard_conversion_spec); | 
| Hans Wennborg | 7651742 | 2012-02-22 10:17:01 +0000 | [diff] [blame] | 2986 |  | 
| Jordan Rose | bbb6bb4 | 2012-09-08 04:00:03 +0000 | [diff] [blame] | 2987 | if (!FS.hasStandardConversionSpecifier(S.getLangOpts())) | 
|  | 2988 | HandleNonStandardConversionSpecifier(CS, startSpecifier, specifierLen); | 
|  | 2989 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2990 | // The remaining checks depend on the data arguments. | 
|  | 2991 | if (HasVAListArg) | 
|  | 2992 | return true; | 
|  | 2993 |  | 
| Ted Kremenek | 666a197 | 2010-07-26 19:45:42 +0000 | [diff] [blame] | 2994 | if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2995 | return false; | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 2996 |  | 
| Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 2997 | // Check that the argument type matches the format specifier. | 
|  | 2998 | const Expr *Ex = getDataArg(argIndex); | 
| Jordan Rose | 4871666 | 2012-07-19 18:10:08 +0000 | [diff] [blame] | 2999 | if (!Ex) | 
|  | 3000 | return true; | 
|  | 3001 |  | 
| Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 3002 | const analyze_format_string::ArgType &AT = FS.getArgType(S.Context); | 
|  | 3003 | if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) { | 
| Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3004 | ScanfSpecifier fixedFS = FS; | 
| David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3005 | bool success = fixedFS.fixType(Ex->getType(), S.getLangOpts(), | 
| Hans Wennborg | be6126a | 2012-02-15 09:59:46 +0000 | [diff] [blame] | 3006 | S.Context); | 
| Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3007 |  | 
|  | 3008 | if (success) { | 
|  | 3009 | // Get the fix string from the fixed format specifier. | 
| Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3010 | SmallString<128> buf; | 
| Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3011 | llvm::raw_svector_ostream os(buf); | 
|  | 3012 | fixedFS.toString(os); | 
|  | 3013 |  | 
|  | 3014 | EmitFormatDiagnostic( | 
|  | 3015 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) | 
| Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 3016 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() | 
| Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3017 | << Ex->getSourceRange(), | 
| Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 3018 | Ex->getLocStart(), | 
|  | 3019 | /*IsStringLocation*/false, | 
| Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3020 | getSpecifierRange(startSpecifier, specifierLen), | 
|  | 3021 | FixItHint::CreateReplacement( | 
|  | 3022 | getSpecifierRange(startSpecifier, specifierLen), | 
|  | 3023 | os.str())); | 
|  | 3024 | } else { | 
| Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3025 | EmitFormatDiagnostic( | 
|  | 3026 | S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) | 
| Hans Wennborg | 58e1e54 | 2012-08-07 08:59:46 +0000 | [diff] [blame] | 3027 | << AT.getRepresentativeTypeName(S.Context) << Ex->getType() | 
| Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3028 | << Ex->getSourceRange(), | 
| Matt Beaumont-Gay | abf145a | 2012-05-17 00:03:16 +0000 | [diff] [blame] | 3029 | Ex->getLocStart(), | 
|  | 3030 | /*IsStringLocation*/false, | 
| Jean-Daniel Dupas | 220947b | 2012-01-31 18:12:08 +0000 | [diff] [blame] | 3031 | getSpecifierRange(startSpecifier, specifierLen)); | 
| Hans Wennborg | 6fcd932 | 2011-12-10 13:20:11 +0000 | [diff] [blame] | 3032 | } | 
|  | 3033 | } | 
|  | 3034 |  | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3035 | return true; | 
|  | 3036 | } | 
|  | 3037 |  | 
|  | 3038 | void Sema::CheckFormatString(const StringLiteral *FExpr, | 
| Ted Kremenek | 0e5675d | 2010-02-10 02:16:30 +0000 | [diff] [blame] | 3039 | const Expr *OrigFormatExpr, | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3040 | Expr **Args, unsigned NumArgs, | 
|  | 3041 | bool HasVAListArg, unsigned format_idx, | 
| Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3042 | unsigned firstDataArg, FormatStringType Type, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3043 | bool inFunctionCall, VariadicCallType CallType) { | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3044 |  | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3045 | // CHECK: is the format string a wide literal? | 
| Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 3046 | if (!FExpr->isAscii() && !FExpr->isUTF8()) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3047 | CheckFormatHandler::EmitFormatDiagnostic( | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3048 | *this, inFunctionCall, Args[format_idx], | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3049 | PDiag(diag::warn_format_string_is_wide_literal), FExpr->getLocStart(), | 
|  | 3050 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3051 | return; | 
|  | 3052 | } | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3053 |  | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3054 | // Str - The format string.  NOTE: this is NOT null-terminated! | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3055 | StringRef StrRef = FExpr->getString(); | 
| Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 3056 | const char *Str = StrRef.data(); | 
|  | 3057 | unsigned StrLen = StrRef.size(); | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3058 | const unsigned numDataArgs = NumArgs - firstDataArg; | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3059 |  | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3060 | // CHECK: empty format string? | 
| Ted Kremenek | 4cd5791 | 2011-09-29 05:52:16 +0000 | [diff] [blame] | 3061 | if (StrLen == 0 && numDataArgs > 0) { | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3062 | CheckFormatHandler::EmitFormatDiagnostic( | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3063 | *this, inFunctionCall, Args[format_idx], | 
| Richard Trieu | 55733de | 2011-10-28 00:41:25 +0000 | [diff] [blame] | 3064 | PDiag(diag::warn_empty_format_string), FExpr->getLocStart(), | 
|  | 3065 | /*IsStringLocation*/true, OrigFormatExpr->getSourceRange()); | 
| Ted Kremenek | e0e5313 | 2010-01-28 23:39:18 +0000 | [diff] [blame] | 3066 | return; | 
|  | 3067 | } | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3068 |  | 
| Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3069 | if (Type == FST_Printf || Type == FST_NSString) { | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3070 | CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3071 | numDataArgs, (Type == FST_NSString), | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3072 | Str, HasVAListArg, Args, NumArgs, format_idx, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3073 | inFunctionCall, CallType); | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3074 |  | 
| Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 3075 | if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen, | 
| Jordan Rose | 275b6f5 | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 3076 | getLangOpts(), | 
|  | 3077 | Context.getTargetInfo())) | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3078 | H.DoneProcessing(); | 
| Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3079 | } else if (Type == FST_Scanf) { | 
| Jordan Rose | 5068731 | 2012-06-04 23:52:23 +0000 | [diff] [blame] | 3080 | CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg, numDataArgs, | 
| Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 3081 | Str, HasVAListArg, Args, NumArgs, format_idx, | 
| Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 3082 | inFunctionCall, CallType); | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3083 |  | 
| Hans Wennborg | d02deeb | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 3084 | if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen, | 
| Jordan Rose | 275b6f5 | 2012-09-13 02:11:03 +0000 | [diff] [blame] | 3085 | getLangOpts(), | 
|  | 3086 | Context.getTargetInfo())) | 
| Ted Kremenek | 826a345 | 2010-07-16 02:11:22 +0000 | [diff] [blame] | 3087 | H.DoneProcessing(); | 
| Jean-Daniel Dupas | 34269df | 2012-01-30 08:46:47 +0000 | [diff] [blame] | 3088 | } // TODO: handle other formats | 
| Ted Kremenek | ce7024e | 2010-01-28 01:18:22 +0000 | [diff] [blame] | 3089 | } | 
|  | 3090 |  | 
| Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3091 | //===--- CHECK: Standard memory functions ---------------------------------===// | 
|  | 3092 |  | 
| Douglas Gregor | 2a053a3 | 2011-05-03 20:05:22 +0000 | [diff] [blame] | 3093 | /// \brief Determine whether the given type is a dynamic class type (e.g., | 
|  | 3094 | /// whether it has a vtable). | 
|  | 3095 | static bool isDynamicClassType(QualType T) { | 
|  | 3096 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) | 
|  | 3097 | if (CXXRecordDecl *Definition = Record->getDefinition()) | 
|  | 3098 | if (Definition->isDynamicClass()) | 
|  | 3099 | return true; | 
|  | 3100 |  | 
|  | 3101 | return false; | 
|  | 3102 | } | 
|  | 3103 |  | 
| Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 3104 | /// \brief If E is a sizeof expression, returns its argument expression, | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3105 | /// otherwise returns NULL. | 
|  | 3106 | static const Expr *getSizeOfExprArg(const Expr* E) { | 
| Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3107 | if (const UnaryExprOrTypeTraitExpr *SizeOf = | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3108 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) | 
|  | 3109 | if (SizeOf->getKind() == clang::UETT_SizeOf && !SizeOf->isArgumentType()) | 
|  | 3110 | return SizeOf->getArgumentExpr()->IgnoreParenImpCasts(); | 
| Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3111 |  | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3112 | return 0; | 
|  | 3113 | } | 
|  | 3114 |  | 
| Chandler Carruth | a72a12f | 2011-06-21 23:04:20 +0000 | [diff] [blame] | 3115 | /// \brief If E is a sizeof expression, returns its argument type. | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3116 | static QualType getSizeOfArgType(const Expr* E) { | 
|  | 3117 | if (const UnaryExprOrTypeTraitExpr *SizeOf = | 
|  | 3118 | dyn_cast<UnaryExprOrTypeTraitExpr>(E)) | 
|  | 3119 | if (SizeOf->getKind() == clang::UETT_SizeOf) | 
|  | 3120 | return SizeOf->getTypeOfArgument(); | 
|  | 3121 |  | 
|  | 3122 | return QualType(); | 
| Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3123 | } | 
|  | 3124 |  | 
| Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3125 | /// \brief Check for dangerous or invalid arguments to memset(). | 
|  | 3126 | /// | 
| Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 3127 | /// This issues warnings on known problematic, dangerous or unspecified | 
| Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 3128 | /// arguments to the standard 'memset', 'memcpy', 'memmove', and 'memcmp' | 
|  | 3129 | /// function calls. | 
| Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3130 | /// | 
|  | 3131 | /// \param Call The call expression to diagnose. | 
| Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 3132 | void Sema::CheckMemaccessArguments(const CallExpr *Call, | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3133 | unsigned BId, | 
| Matt Beaumont-Gay | cc2f30c | 2011-08-05 00:22:34 +0000 | [diff] [blame] | 3134 | IdentifierInfo *FnName) { | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3135 | assert(BId != 0); | 
|  | 3136 |  | 
| Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 3137 | // It is possible to have a non-standard definition of memset.  Validate | 
| Douglas Gregor | 707a23e | 2011-06-16 17:56:04 +0000 | [diff] [blame] | 3138 | // we have enough arguments, and if not, abort further checking. | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3139 | unsigned ExpectedNumArgs = (BId == Builtin::BIstrndup ? 2 : 3); | 
| Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 3140 | if (Call->getNumArgs() < ExpectedNumArgs) | 
| Ted Kremenek | 1d59f7f | 2011-04-28 01:38:02 +0000 | [diff] [blame] | 3141 | return; | 
|  | 3142 |  | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3143 | unsigned LastArg = (BId == Builtin::BImemset || | 
|  | 3144 | BId == Builtin::BIstrndup ? 1 : 2); | 
|  | 3145 | unsigned LenArg = (BId == Builtin::BIstrndup ? 1 : 2); | 
| Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 3146 | const Expr *LenExpr = Call->getArg(LenArg)->IgnoreParenImpCasts(); | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3147 |  | 
|  | 3148 | // We have special checking when the length is a sizeof expression. | 
|  | 3149 | QualType SizeOfArgTy = getSizeOfArgType(LenExpr); | 
|  | 3150 | const Expr *SizeOfArg = getSizeOfExprArg(LenExpr); | 
|  | 3151 | llvm::FoldingSetNodeID SizeOfArgID; | 
|  | 3152 |  | 
| Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3153 | for (unsigned ArgIdx = 0; ArgIdx != LastArg; ++ArgIdx) { | 
|  | 3154 | const Expr *Dest = Call->getArg(ArgIdx)->IgnoreParenImpCasts(); | 
| Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3155 | SourceRange ArgRange = Call->getArg(ArgIdx)->getSourceRange(); | 
| Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3156 |  | 
| Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3157 | QualType DestTy = Dest->getType(); | 
|  | 3158 | if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) { | 
|  | 3159 | QualType PointeeTy = DestPtrTy->getPointeeType(); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3160 |  | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3161 | // Never warn about void type pointers. This can be used to suppress | 
|  | 3162 | // false positives. | 
|  | 3163 | if (PointeeTy->isVoidType()) | 
| Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3164 | continue; | 
| Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3165 |  | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3166 | // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by | 
|  | 3167 | // actually comparing the expressions for equality. Because computing the | 
|  | 3168 | // expression IDs can be expensive, we only do this if the diagnostic is | 
|  | 3169 | // enabled. | 
|  | 3170 | if (SizeOfArg && | 
|  | 3171 | Diags.getDiagnosticLevel(diag::warn_sizeof_pointer_expr_memaccess, | 
|  | 3172 | SizeOfArg->getExprLoc())) { | 
|  | 3173 | // We only compute IDs for expressions if the warning is enabled, and | 
|  | 3174 | // cache the sizeof arg's ID. | 
|  | 3175 | if (SizeOfArgID == llvm::FoldingSetNodeID()) | 
|  | 3176 | SizeOfArg->Profile(SizeOfArgID, Context, true); | 
|  | 3177 | llvm::FoldingSetNodeID DestID; | 
|  | 3178 | Dest->Profile(DestID, Context, true); | 
|  | 3179 | if (DestID == SizeOfArgID) { | 
| Nico Weber | cda5782 | 2011-10-13 22:30:23 +0000 | [diff] [blame] | 3180 | // TODO: For strncpy() and friends, this could suggest sizeof(dst) | 
|  | 3181 | //       over sizeof(src) as well. | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3182 | unsigned ActionIdx = 0; // Default is to suggest dereferencing. | 
| Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3183 | StringRef ReadableName = FnName->getName(); | 
|  | 3184 |  | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3185 | if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest)) | 
| Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3186 | if (UnaryOp->getOpcode() == UO_AddrOf) | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3187 | ActionIdx = 1; // If its an address-of operator, just remove it. | 
|  | 3188 | if (Context.getTypeSize(PointeeTy) == Context.getCharWidth()) | 
|  | 3189 | ActionIdx = 2; // If the pointee's size is sizeof(char), | 
|  | 3190 | // suggest an explicit length. | 
| Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3191 |  | 
|  | 3192 | // If the function is defined as a builtin macro, do not show macro | 
|  | 3193 | // expansion. | 
|  | 3194 | SourceLocation SL = SizeOfArg->getExprLoc(); | 
|  | 3195 | SourceRange DSR = Dest->getSourceRange(); | 
|  | 3196 | SourceRange SSR = SizeOfArg->getSourceRange(); | 
|  | 3197 | SourceManager &SM  = PP.getSourceManager(); | 
|  | 3198 |  | 
|  | 3199 | if (SM.isMacroArgExpansion(SL)) { | 
|  | 3200 | ReadableName = Lexer::getImmediateMacroName(SL, SM, LangOpts); | 
|  | 3201 | SL = SM.getSpellingLoc(SL); | 
|  | 3202 | DSR = SourceRange(SM.getSpellingLoc(DSR.getBegin()), | 
|  | 3203 | SM.getSpellingLoc(DSR.getEnd())); | 
|  | 3204 | SSR = SourceRange(SM.getSpellingLoc(SSR.getBegin()), | 
|  | 3205 | SM.getSpellingLoc(SSR.getEnd())); | 
|  | 3206 | } | 
|  | 3207 |  | 
| Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3208 | DiagRuntimeBehavior(SL, SizeOfArg, | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3209 | PDiag(diag::warn_sizeof_pointer_expr_memaccess) | 
| Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3210 | << ReadableName | 
| Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3211 | << PointeeTy | 
|  | 3212 | << DestTy | 
| Anna Zaks | 6fcb372 | 2012-05-30 00:34:21 +0000 | [diff] [blame] | 3213 | << DSR | 
| Anna Zaks | 90c7832 | 2012-05-30 23:14:52 +0000 | [diff] [blame] | 3214 | << SSR); | 
|  | 3215 | DiagRuntimeBehavior(SL, SizeOfArg, | 
|  | 3216 | PDiag(diag::warn_sizeof_pointer_expr_memaccess_note) | 
|  | 3217 | << ActionIdx | 
|  | 3218 | << SSR); | 
|  | 3219 |  | 
| Chandler Carruth | 000d428 | 2011-06-16 09:09:40 +0000 | [diff] [blame] | 3220 | break; | 
|  | 3221 | } | 
|  | 3222 | } | 
|  | 3223 |  | 
|  | 3224 | // Also check for cases where the sizeof argument is the exact same | 
|  | 3225 | // type as the memory argument, and where it points to a user-defined | 
|  | 3226 | // record type. | 
|  | 3227 | if (SizeOfArgTy != QualType()) { | 
|  | 3228 | if (PointeeTy->isRecordType() && | 
|  | 3229 | Context.typesAreCompatible(SizeOfArgTy, DestTy)) { | 
|  | 3230 | DiagRuntimeBehavior(LenExpr->getExprLoc(), Dest, | 
|  | 3231 | PDiag(diag::warn_sizeof_pointer_type_memaccess) | 
|  | 3232 | << FnName << SizeOfArgTy << ArgIdx | 
|  | 3233 | << PointeeTy << Dest->getSourceRange() | 
|  | 3234 | << LenExpr->getSourceRange()); | 
|  | 3235 | break; | 
|  | 3236 | } | 
| Nico Weber | e4a1c64 | 2011-06-14 16:14:58 +0000 | [diff] [blame] | 3237 | } | 
|  | 3238 |  | 
| Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3239 | // Always complain about dynamic classes. | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3240 | if (isDynamicClassType(PointeeTy)) { | 
|  | 3241 |  | 
|  | 3242 | unsigned OperationType = 0; | 
|  | 3243 | // "overwritten" if we're warning about the destination for any call | 
|  | 3244 | // but memcmp; otherwise a verb appropriate to the call. | 
|  | 3245 | if (ArgIdx != 0 || BId == Builtin::BImemcmp) { | 
|  | 3246 | if (BId == Builtin::BImemcpy) | 
|  | 3247 | OperationType = 1; | 
|  | 3248 | else if(BId == Builtin::BImemmove) | 
|  | 3249 | OperationType = 2; | 
|  | 3250 | else if (BId == Builtin::BImemcmp) | 
|  | 3251 | OperationType = 3; | 
|  | 3252 | } | 
|  | 3253 |  | 
| Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3254 | DiagRuntimeBehavior( | 
|  | 3255 | Dest->getExprLoc(), Dest, | 
|  | 3256 | PDiag(diag::warn_dyn_class_memaccess) | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3257 | << (BId == Builtin::BImemcmp ? ArgIdx + 2 : ArgIdx) | 
| Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3258 | << FnName << PointeeTy | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3259 | << OperationType | 
| Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3260 | << Call->getCallee()->getSourceRange()); | 
| Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3261 | } else if (PointeeTy.hasNonTrivialObjCLifetime() && | 
|  | 3262 | BId != Builtin::BImemset) | 
| Matt Beaumont-Gay | 5c5218e | 2011-08-19 20:40:18 +0000 | [diff] [blame] | 3263 | DiagRuntimeBehavior( | 
|  | 3264 | Dest->getExprLoc(), Dest, | 
|  | 3265 | PDiag(diag::warn_arc_object_memaccess) | 
|  | 3266 | << ArgIdx << FnName << PointeeTy | 
|  | 3267 | << Call->getCallee()->getSourceRange()); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3268 | else | 
| Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3269 | continue; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3270 |  | 
|  | 3271 | DiagRuntimeBehavior( | 
|  | 3272 | Dest->getExprLoc(), Dest, | 
| Chandler Carruth | 929f013 | 2011-06-03 06:23:57 +0000 | [diff] [blame] | 3273 | PDiag(diag::note_bad_memaccess_silence) | 
| Douglas Gregor | 06bc9eb | 2011-05-03 20:37:33 +0000 | [diff] [blame] | 3274 | << FixItHint::CreateInsertion(ArgRange.getBegin(), "(void*)")); | 
|  | 3275 | break; | 
|  | 3276 | } | 
| Chandler Carruth | 7ccc95b | 2011-04-27 07:05:31 +0000 | [diff] [blame] | 3277 | } | 
|  | 3278 | } | 
|  | 3279 |  | 
| Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3280 | // A little helper routine: ignore addition and subtraction of integer literals. | 
|  | 3281 | // This intentionally does not ignore all integer constant expressions because | 
|  | 3282 | // we don't want to remove sizeof(). | 
|  | 3283 | static const Expr *ignoreLiteralAdditions(const Expr *Ex, ASTContext &Ctx) { | 
|  | 3284 | Ex = Ex->IgnoreParenCasts(); | 
|  | 3285 |  | 
|  | 3286 | for (;;) { | 
|  | 3287 | const BinaryOperator * BO = dyn_cast<BinaryOperator>(Ex); | 
|  | 3288 | if (!BO || !BO->isAdditiveOp()) | 
|  | 3289 | break; | 
|  | 3290 |  | 
|  | 3291 | const Expr *RHS = BO->getRHS()->IgnoreParenCasts(); | 
|  | 3292 | const Expr *LHS = BO->getLHS()->IgnoreParenCasts(); | 
|  | 3293 |  | 
|  | 3294 | if (isa<IntegerLiteral>(RHS)) | 
|  | 3295 | Ex = LHS; | 
|  | 3296 | else if (isa<IntegerLiteral>(LHS)) | 
|  | 3297 | Ex = RHS; | 
|  | 3298 | else | 
|  | 3299 | break; | 
|  | 3300 | } | 
|  | 3301 |  | 
|  | 3302 | return Ex; | 
|  | 3303 | } | 
|  | 3304 |  | 
| Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3305 | static bool isConstantSizeArrayWithMoreThanOneElement(QualType Ty, | 
|  | 3306 | ASTContext &Context) { | 
|  | 3307 | // Only handle constant-sized or VLAs, but not flexible members. | 
|  | 3308 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(Ty)) { | 
|  | 3309 | // Only issue the FIXIT for arrays of size > 1. | 
|  | 3310 | if (CAT->getSize().getSExtValue() <= 1) | 
|  | 3311 | return false; | 
|  | 3312 | } else if (!Ty->isVariableArrayType()) { | 
|  | 3313 | return false; | 
|  | 3314 | } | 
|  | 3315 | return true; | 
|  | 3316 | } | 
|  | 3317 |  | 
| Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3318 | // Warn if the user has made the 'size' argument to strlcpy or strlcat | 
|  | 3319 | // be the size of the source, instead of the destination. | 
|  | 3320 | void Sema::CheckStrlcpycatArguments(const CallExpr *Call, | 
|  | 3321 | IdentifierInfo *FnName) { | 
|  | 3322 |  | 
|  | 3323 | // Don't crash if the user has the wrong number of arguments | 
|  | 3324 | if (Call->getNumArgs() != 3) | 
|  | 3325 | return; | 
|  | 3326 |  | 
|  | 3327 | const Expr *SrcArg = ignoreLiteralAdditions(Call->getArg(1), Context); | 
|  | 3328 | const Expr *SizeArg = ignoreLiteralAdditions(Call->getArg(2), Context); | 
|  | 3329 | const Expr *CompareWithSrc = NULL; | 
|  | 3330 |  | 
|  | 3331 | // Look for 'strlcpy(dst, x, sizeof(x))' | 
|  | 3332 | if (const Expr *Ex = getSizeOfExprArg(SizeArg)) | 
|  | 3333 | CompareWithSrc = Ex; | 
|  | 3334 | else { | 
|  | 3335 | // Look for 'strlcpy(dst, x, strlen(x))' | 
|  | 3336 | if (const CallExpr *SizeCall = dyn_cast<CallExpr>(SizeArg)) { | 
| Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3337 | if (SizeCall->isBuiltinCall() == Builtin::BIstrlen | 
| Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3338 | && SizeCall->getNumArgs() == 1) | 
|  | 3339 | CompareWithSrc = ignoreLiteralAdditions(SizeCall->getArg(0), Context); | 
|  | 3340 | } | 
|  | 3341 | } | 
|  | 3342 |  | 
|  | 3343 | if (!CompareWithSrc) | 
|  | 3344 | return; | 
|  | 3345 |  | 
|  | 3346 | // Determine if the argument to sizeof/strlen is equal to the source | 
|  | 3347 | // argument.  In principle there's all kinds of things you could do | 
|  | 3348 | // here, for instance creating an == expression and evaluating it with | 
|  | 3349 | // EvaluateAsBooleanCondition, but this uses a more direct technique: | 
|  | 3350 | const DeclRefExpr *SrcArgDRE = dyn_cast<DeclRefExpr>(SrcArg); | 
|  | 3351 | if (!SrcArgDRE) | 
|  | 3352 | return; | 
|  | 3353 |  | 
|  | 3354 | const DeclRefExpr *CompareWithSrcDRE = dyn_cast<DeclRefExpr>(CompareWithSrc); | 
|  | 3355 | if (!CompareWithSrcDRE || | 
|  | 3356 | SrcArgDRE->getDecl() != CompareWithSrcDRE->getDecl()) | 
|  | 3357 | return; | 
|  | 3358 |  | 
|  | 3359 | const Expr *OriginalSizeArg = Call->getArg(2); | 
|  | 3360 | Diag(CompareWithSrcDRE->getLocStart(), diag::warn_strlcpycat_wrong_size) | 
|  | 3361 | << OriginalSizeArg->getSourceRange() << FnName; | 
|  | 3362 |  | 
|  | 3363 | // Output a FIXIT hint if the destination is an array (rather than a | 
|  | 3364 | // pointer to an array).  This could be enhanced to handle some | 
|  | 3365 | // pointers if we know the actual size, like if DstArg is 'array+2' | 
|  | 3366 | // we could say 'sizeof(array)-2'. | 
|  | 3367 | const Expr *DstArg = Call->getArg(0)->IgnoreParenImpCasts(); | 
| Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3368 | if (!isConstantSizeArrayWithMoreThanOneElement(DstArg->getType(), Context)) | 
| Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3369 | return; | 
| Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3370 |  | 
| Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3371 | SmallString<128> sizeString; | 
| Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3372 | llvm::raw_svector_ostream OS(sizeString); | 
|  | 3373 | OS << "sizeof("; | 
| Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3374 | DstArg->printPretty(OS, 0, getPrintingPolicy()); | 
| Ted Kremenek | 8f74622 | 2011-08-18 22:48:41 +0000 | [diff] [blame] | 3375 | OS << ")"; | 
|  | 3376 |  | 
|  | 3377 | Diag(OriginalSizeArg->getLocStart(), diag::note_strlcpycat_wrong_size) | 
|  | 3378 | << FixItHint::CreateReplacement(OriginalSizeArg->getSourceRange(), | 
|  | 3379 | OS.str()); | 
| Ted Kremenek | bd5da9d | 2011-08-18 20:55:45 +0000 | [diff] [blame] | 3380 | } | 
|  | 3381 |  | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3382 | /// Check if two expressions refer to the same declaration. | 
|  | 3383 | static bool referToTheSameDecl(const Expr *E1, const Expr *E2) { | 
|  | 3384 | if (const DeclRefExpr *D1 = dyn_cast_or_null<DeclRefExpr>(E1)) | 
|  | 3385 | if (const DeclRefExpr *D2 = dyn_cast_or_null<DeclRefExpr>(E2)) | 
|  | 3386 | return D1->getDecl() == D2->getDecl(); | 
|  | 3387 | return false; | 
|  | 3388 | } | 
|  | 3389 |  | 
|  | 3390 | static const Expr *getStrlenExprArg(const Expr *E) { | 
|  | 3391 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { | 
|  | 3392 | const FunctionDecl *FD = CE->getDirectCallee(); | 
|  | 3393 | if (!FD || FD->getMemoryFunctionKind() != Builtin::BIstrlen) | 
|  | 3394 | return 0; | 
|  | 3395 | return CE->getArg(0)->IgnoreParenCasts(); | 
|  | 3396 | } | 
|  | 3397 | return 0; | 
|  | 3398 | } | 
|  | 3399 |  | 
|  | 3400 | // Warn on anti-patterns as the 'size' argument to strncat. | 
|  | 3401 | // The correct size argument should look like following: | 
|  | 3402 | //   strncat(dst, src, sizeof(dst) - strlen(dest) - 1); | 
|  | 3403 | void Sema::CheckStrncatArguments(const CallExpr *CE, | 
|  | 3404 | IdentifierInfo *FnName) { | 
|  | 3405 | // Don't crash if the user has the wrong number of arguments. | 
|  | 3406 | if (CE->getNumArgs() < 3) | 
|  | 3407 | return; | 
|  | 3408 | const Expr *DstArg = CE->getArg(0)->IgnoreParenCasts(); | 
|  | 3409 | const Expr *SrcArg = CE->getArg(1)->IgnoreParenCasts(); | 
|  | 3410 | const Expr *LenArg = CE->getArg(2)->IgnoreParenCasts(); | 
|  | 3411 |  | 
|  | 3412 | // Identify common expressions, which are wrongly used as the size argument | 
|  | 3413 | // to strncat and may lead to buffer overflows. | 
|  | 3414 | unsigned PatternType = 0; | 
|  | 3415 | if (const Expr *SizeOfArg = getSizeOfExprArg(LenArg)) { | 
|  | 3416 | // - sizeof(dst) | 
|  | 3417 | if (referToTheSameDecl(SizeOfArg, DstArg)) | 
|  | 3418 | PatternType = 1; | 
|  | 3419 | // - sizeof(src) | 
|  | 3420 | else if (referToTheSameDecl(SizeOfArg, SrcArg)) | 
|  | 3421 | PatternType = 2; | 
|  | 3422 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(LenArg)) { | 
|  | 3423 | if (BE->getOpcode() == BO_Sub) { | 
|  | 3424 | const Expr *L = BE->getLHS()->IgnoreParenCasts(); | 
|  | 3425 | const Expr *R = BE->getRHS()->IgnoreParenCasts(); | 
|  | 3426 | // - sizeof(dst) - strlen(dst) | 
|  | 3427 | if (referToTheSameDecl(DstArg, getSizeOfExprArg(L)) && | 
|  | 3428 | referToTheSameDecl(DstArg, getStrlenExprArg(R))) | 
|  | 3429 | PatternType = 1; | 
|  | 3430 | // - sizeof(src) - (anything) | 
|  | 3431 | else if (referToTheSameDecl(SrcArg, getSizeOfExprArg(L))) | 
|  | 3432 | PatternType = 2; | 
|  | 3433 | } | 
|  | 3434 | } | 
|  | 3435 |  | 
|  | 3436 | if (PatternType == 0) | 
|  | 3437 | return; | 
|  | 3438 |  | 
| Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3439 | // Generate the diagnostic. | 
|  | 3440 | SourceLocation SL = LenArg->getLocStart(); | 
|  | 3441 | SourceRange SR = LenArg->getSourceRange(); | 
|  | 3442 | SourceManager &SM  = PP.getSourceManager(); | 
|  | 3443 |  | 
|  | 3444 | // If the function is defined as a builtin macro, do not show macro expansion. | 
|  | 3445 | if (SM.isMacroArgExpansion(SL)) { | 
|  | 3446 | SL = SM.getSpellingLoc(SL); | 
|  | 3447 | SR = SourceRange(SM.getSpellingLoc(SR.getBegin()), | 
|  | 3448 | SM.getSpellingLoc(SR.getEnd())); | 
|  | 3449 | } | 
|  | 3450 |  | 
| Anna Zaks | 0f38ace | 2012-08-08 21:42:23 +0000 | [diff] [blame] | 3451 | // Check if the destination is an array (rather than a pointer to an array). | 
|  | 3452 | QualType DstTy = DstArg->getType(); | 
|  | 3453 | bool isKnownSizeArray = isConstantSizeArrayWithMoreThanOneElement(DstTy, | 
|  | 3454 | Context); | 
|  | 3455 | if (!isKnownSizeArray) { | 
|  | 3456 | if (PatternType == 1) | 
|  | 3457 | Diag(SL, diag::warn_strncat_wrong_size) << SR; | 
|  | 3458 | else | 
|  | 3459 | Diag(SL, diag::warn_strncat_src_size) << SR; | 
|  | 3460 | return; | 
|  | 3461 | } | 
|  | 3462 |  | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3463 | if (PatternType == 1) | 
| Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3464 | Diag(SL, diag::warn_strncat_large_size) << SR; | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3465 | else | 
| Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3466 | Diag(SL, diag::warn_strncat_src_size) << SR; | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3467 |  | 
| Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3468 | SmallString<128> sizeString; | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3469 | llvm::raw_svector_ostream OS(sizeString); | 
|  | 3470 | OS << "sizeof("; | 
| Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3471 | DstArg->printPretty(OS, 0, getPrintingPolicy()); | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3472 | OS << ") - "; | 
|  | 3473 | OS << "strlen("; | 
| Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 3474 | DstArg->printPretty(OS, 0, getPrintingPolicy()); | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3475 | OS << ") - 1"; | 
|  | 3476 |  | 
| Anna Zaks | afdb041 | 2012-02-03 01:27:37 +0000 | [diff] [blame] | 3477 | Diag(SL, diag::note_strncat_wrong_size) | 
|  | 3478 | << FixItHint::CreateReplacement(SR, OS.str()); | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3479 | } | 
|  | 3480 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3481 | //===--- CHECK: Return Address of Stack Variable --------------------------===// | 
|  | 3482 |  | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3483 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, | 
|  | 3484 | Decl *ParentDecl); | 
|  | 3485 | static Expr *EvalAddr(Expr* E, SmallVectorImpl<DeclRefExpr *> &refVars, | 
|  | 3486 | Decl *ParentDecl); | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3487 |  | 
|  | 3488 | /// CheckReturnStackAddr - Check if a return statement returns the address | 
|  | 3489 | ///   of a stack variable. | 
|  | 3490 | void | 
|  | 3491 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, | 
|  | 3492 | SourceLocation ReturnLoc) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 |  | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3494 | Expr *stackE = 0; | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3495 | SmallVector<DeclRefExpr *, 8> refVars; | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3496 |  | 
|  | 3497 | // Perform checking for returned stack addresses, local blocks, | 
|  | 3498 | // label addresses or references to temporaries. | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3499 | if (lhsType->isPointerType() || | 
| David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3500 | (!getLangOpts().ObjCAutoRefCount && lhsType->isBlockPointerType())) { | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3501 | stackE = EvalAddr(RetValExp, refVars, /*ParentDecl=*/0); | 
| Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3502 | } else if (lhsType->isReferenceType()) { | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3503 | stackE = EvalVal(RetValExp, refVars, /*ParentDecl=*/0); | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3504 | } | 
|  | 3505 |  | 
|  | 3506 | if (stackE == 0) | 
|  | 3507 | return; // Nothing suspicious was found. | 
|  | 3508 |  | 
|  | 3509 | SourceLocation diagLoc; | 
|  | 3510 | SourceRange diagRange; | 
|  | 3511 | if (refVars.empty()) { | 
|  | 3512 | diagLoc = stackE->getLocStart(); | 
|  | 3513 | diagRange = stackE->getSourceRange(); | 
|  | 3514 | } else { | 
|  | 3515 | // We followed through a reference variable. 'stackE' contains the | 
|  | 3516 | // problematic expression but we will warn at the return statement pointing | 
|  | 3517 | // at the reference variable. We will later display the "trail" of | 
|  | 3518 | // reference variables using notes. | 
|  | 3519 | diagLoc = refVars[0]->getLocStart(); | 
|  | 3520 | diagRange = refVars[0]->getSourceRange(); | 
|  | 3521 | } | 
|  | 3522 |  | 
|  | 3523 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(stackE)) { //address of local var. | 
|  | 3524 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_stack_ref | 
|  | 3525 | : diag::warn_ret_stack_addr) | 
|  | 3526 | << DR->getDecl()->getDeclName() << diagRange; | 
|  | 3527 | } else if (isa<BlockExpr>(stackE)) { // local block. | 
|  | 3528 | Diag(diagLoc, diag::err_ret_local_block) << diagRange; | 
|  | 3529 | } else if (isa<AddrLabelExpr>(stackE)) { // address of label. | 
|  | 3530 | Diag(diagLoc, diag::warn_ret_addr_label) << diagRange; | 
|  | 3531 | } else { // local temporary. | 
|  | 3532 | Diag(diagLoc, lhsType->isReferenceType() ? diag::warn_ret_local_temp_ref | 
|  | 3533 | : diag::warn_ret_local_temp_addr) | 
|  | 3534 | << diagRange; | 
|  | 3535 | } | 
|  | 3536 |  | 
|  | 3537 | // Display the "trail" of reference variables that we followed until we | 
|  | 3538 | // found the problematic expression using notes. | 
|  | 3539 | for (unsigned i = 0, e = refVars.size(); i != e; ++i) { | 
|  | 3540 | VarDecl *VD = cast<VarDecl>(refVars[i]->getDecl()); | 
|  | 3541 | // If this var binds to another reference var, show the range of the next | 
|  | 3542 | // var, otherwise the var binds to the problematic expression, in which case | 
|  | 3543 | // show the range of the expression. | 
|  | 3544 | SourceRange range = (i < e-1) ? refVars[i+1]->getSourceRange() | 
|  | 3545 | : stackE->getSourceRange(); | 
|  | 3546 | Diag(VD->getLocation(), diag::note_ref_var_local_bind) | 
|  | 3547 | << VD->getDeclName() << range; | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3548 | } | 
|  | 3549 | } | 
|  | 3550 |  | 
|  | 3551 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that | 
|  | 3552 | ///  check if the expression in a return statement evaluates to an address | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3553 | ///  to a location on the stack, a local block, an address of a label, or a | 
|  | 3554 | ///  reference to local temporary. The recursion is used to traverse the | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3555 | ///  AST of the return expression, with recursion backtracking when we | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3556 | ///  encounter a subexpression that (1) clearly does not lead to one of the | 
|  | 3557 | ///  above problematic expressions (2) is something we cannot determine leads to | 
|  | 3558 | ///  a problematic expression based on such local checking. | 
|  | 3559 | /// | 
|  | 3560 | ///  Both EvalAddr and EvalVal follow through reference variables to evaluate | 
|  | 3561 | ///  the expression that they point to. Such variables are added to the | 
|  | 3562 | ///  'refVars' vector so that we know what the reference variable "trail" was. | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3563 | /// | 
| Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3564 | ///  EvalAddr processes expressions that are pointers that are used as | 
|  | 3565 | ///  references (and not L-values).  EvalVal handles all other values. | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3566 | ///  At the base case of the recursion is a check for the above problematic | 
|  | 3567 | ///  expressions. | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3568 | /// | 
|  | 3569 | ///  This implementation handles: | 
|  | 3570 | /// | 
|  | 3571 | ///   * pointer-to-pointer casts | 
|  | 3572 | ///   * implicit conversions from array references to pointers | 
|  | 3573 | ///   * taking the address of fields | 
|  | 3574 | ///   * arbitrary interplay between "&" and "*" operators | 
|  | 3575 | ///   * pointer arithmetic from an address of a stack variable | 
|  | 3576 | ///   * taking the address of an array element where the array is on the stack | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3577 | static Expr *EvalAddr(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, | 
|  | 3578 | Decl *ParentDecl) { | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3579 | if (E->isTypeDependent()) | 
|  | 3580 | return NULL; | 
|  | 3581 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3582 | // We should only be called for evaluating pointer expressions. | 
| David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3583 | assert((E->getType()->isAnyPointerType() || | 
| Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3584 | E->getType()->isBlockPointerType() || | 
| Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3585 | E->getType()->isObjCQualifiedIdType()) && | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3586 | "EvalAddr only works on pointers"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3587 |  | 
| Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3588 | E = E->IgnoreParens(); | 
|  | 3589 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3590 | // Our "symbolic interpreter" is just a dispatch off the currently | 
|  | 3591 | // viewed AST node.  We then recursively traverse the AST by calling | 
|  | 3592 | // EvalAddr and EvalVal appropriately. | 
|  | 3593 | switch (E->getStmtClass()) { | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3594 | case Stmt::DeclRefExprClass: { | 
|  | 3595 | DeclRefExpr *DR = cast<DeclRefExpr>(E); | 
|  | 3596 |  | 
|  | 3597 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) | 
|  | 3598 | // If this is a reference variable, follow through to the expression that | 
|  | 3599 | // it points to. | 
|  | 3600 | if (V->hasLocalStorage() && | 
|  | 3601 | V->getType()->isReferenceType() && V->hasInit()) { | 
|  | 3602 | // Add the reference variable to the "trail". | 
|  | 3603 | refVars.push_back(DR); | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3604 | return EvalAddr(V->getInit(), refVars, ParentDecl); | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3605 | } | 
|  | 3606 |  | 
|  | 3607 | return NULL; | 
|  | 3608 | } | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3609 |  | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3610 | case Stmt::UnaryOperatorClass: { | 
|  | 3611 | // The only unary operator that make sense to handle here | 
|  | 3612 | // is AddrOf.  All others don't make sense as pointers. | 
|  | 3613 | UnaryOperator *U = cast<UnaryOperator>(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 |  | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3615 | if (U->getOpcode() == UO_AddrOf) | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3616 | return EvalVal(U->getSubExpr(), refVars, ParentDecl); | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3617 | else | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3618 | return NULL; | 
|  | 3619 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3620 |  | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3621 | case Stmt::BinaryOperatorClass: { | 
|  | 3622 | // Handle pointer arithmetic.  All other binary operators are not valid | 
|  | 3623 | // in this context. | 
|  | 3624 | BinaryOperator *B = cast<BinaryOperator>(E); | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3625 | BinaryOperatorKind op = B->getOpcode(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 |  | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3627 | if (op != BO_Add && op != BO_Sub) | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3628 | return NULL; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3629 |  | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3630 | Expr *Base = B->getLHS(); | 
|  | 3631 |  | 
|  | 3632 | // Determine which argument is the real pointer base.  It could be | 
|  | 3633 | // the RHS argument instead of the LHS. | 
|  | 3634 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 |  | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3636 | assert (Base->getType()->isPointerType()); | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3637 | return EvalAddr(Base, refVars, ParentDecl); | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3638 | } | 
| Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 3639 |  | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3640 | // For conditional operators we need to see if either the LHS or RHS are | 
|  | 3641 | // valid DeclRefExpr*s.  If one of them is valid, we return it. | 
|  | 3642 | case Stmt::ConditionalOperatorClass: { | 
|  | 3643 | ConditionalOperator *C = cast<ConditionalOperator>(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3644 |  | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3645 | // Handle the GNU extension for missing LHS. | 
| Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3646 | if (Expr *lhsExpr = C->getLHS()) { | 
|  | 3647 | // In C++, we can have a throw-expression, which has 'void' type. | 
|  | 3648 | if (!lhsExpr->getType()->isVoidType()) | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3649 | if (Expr* LHS = EvalAddr(lhsExpr, refVars, ParentDecl)) | 
| Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3650 | return LHS; | 
|  | 3651 | } | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3652 |  | 
| Douglas Gregor | 9ee5ee8 | 2010-10-21 16:21:08 +0000 | [diff] [blame] | 3653 | // In C++, we can have a throw-expression, which has 'void' type. | 
|  | 3654 | if (C->getRHS()->getType()->isVoidType()) | 
|  | 3655 | return NULL; | 
|  | 3656 |  | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3657 | return EvalAddr(C->getRHS(), refVars, ParentDecl); | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3658 | } | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3659 |  | 
|  | 3660 | case Stmt::BlockExprClass: | 
| John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3661 | if (cast<BlockExpr>(E)->getBlockDecl()->hasCaptures()) | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3662 | return E; // local block. | 
|  | 3663 | return NULL; | 
|  | 3664 |  | 
|  | 3665 | case Stmt::AddrLabelExprClass: | 
|  | 3666 | return E; // address of label. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 |  | 
| John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3668 | case Stmt::ExprWithCleanupsClass: | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3669 | return EvalAddr(cast<ExprWithCleanups>(E)->getSubExpr(), refVars, | 
|  | 3670 | ParentDecl); | 
| John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3671 |  | 
| Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 3672 | // For casts, we need to handle conversions from arrays to | 
|  | 3673 | // pointer values, and pointer-to-pointer conversions. | 
| Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3674 | case Stmt::ImplicitCastExprClass: | 
| Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 3675 | case Stmt::CStyleCastExprClass: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3676 | case Stmt::CXXFunctionalCastExprClass: | 
| Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3677 | case Stmt::ObjCBridgedCastExprClass: | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | case Stmt::CXXStaticCastExprClass: | 
|  | 3679 | case Stmt::CXXDynamicCastExprClass: | 
| Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 3680 | case Stmt::CXXConstCastExprClass: | 
|  | 3681 | case Stmt::CXXReinterpretCastExprClass: { | 
| Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3682 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); | 
|  | 3683 | switch (cast<CastExpr>(E)->getCastKind()) { | 
|  | 3684 | case CK_BitCast: | 
|  | 3685 | case CK_LValueToRValue: | 
|  | 3686 | case CK_NoOp: | 
|  | 3687 | case CK_BaseToDerived: | 
|  | 3688 | case CK_DerivedToBase: | 
|  | 3689 | case CK_UncheckedDerivedToBase: | 
|  | 3690 | case CK_Dynamic: | 
|  | 3691 | case CK_CPointerToObjCPointerCast: | 
|  | 3692 | case CK_BlockPointerToObjCPointerCast: | 
|  | 3693 | case CK_AnyPointerToBlockPointerCast: | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3694 | return EvalAddr(SubExpr, refVars, ParentDecl); | 
| Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3695 |  | 
|  | 3696 | case CK_ArrayToPointerDecay: | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3697 | return EvalVal(SubExpr, refVars, ParentDecl); | 
| Eli Friedman | 8b9414e | 2012-02-23 23:04:32 +0000 | [diff] [blame] | 3698 |  | 
|  | 3699 | default: | 
|  | 3700 | return 0; | 
|  | 3701 | } | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3702 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 |  | 
| Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3704 | case Stmt::MaterializeTemporaryExprClass: | 
|  | 3705 | if (Expr *Result = EvalAddr( | 
|  | 3706 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3707 | refVars, ParentDecl)) | 
| Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3708 | return Result; | 
|  | 3709 |  | 
|  | 3710 | return E; | 
|  | 3711 |  | 
| Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 3712 | // Everything else: we simply don't reason about them. | 
|  | 3713 | default: | 
|  | 3714 | return NULL; | 
|  | 3715 | } | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3716 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3717 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3718 |  | 
|  | 3719 | ///  EvalVal - This function is complements EvalAddr in the mutual recursion. | 
|  | 3720 | ///   See the comments for EvalAddr for more details. | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3721 | static Expr *EvalVal(Expr *E, SmallVectorImpl<DeclRefExpr *> &refVars, | 
|  | 3722 | Decl *ParentDecl) { | 
| Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3723 | do { | 
| Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 3724 | // We should only be called for evaluating non-pointer expressions, or | 
|  | 3725 | // expressions with a pointer type that are not used as references but instead | 
|  | 3726 | // are l-values (e.g., DeclRefExpr with a pointer type). | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3728 | // Our "symbolic interpreter" is just a dispatch off the currently | 
|  | 3729 | // viewed AST node.  We then recursively traverse the AST by calling | 
|  | 3730 | // EvalAddr and EvalVal appropriately. | 
| Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3731 |  | 
|  | 3732 | E = E->IgnoreParens(); | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3733 | switch (E->getStmtClass()) { | 
| Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3734 | case Stmt::ImplicitCastExprClass: { | 
|  | 3735 | ImplicitCastExpr *IE = cast<ImplicitCastExpr>(E); | 
| John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3736 | if (IE->getValueKind() == VK_LValue) { | 
| Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3737 | E = IE->getSubExpr(); | 
|  | 3738 | continue; | 
|  | 3739 | } | 
|  | 3740 | return NULL; | 
|  | 3741 | } | 
|  | 3742 |  | 
| John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3743 | case Stmt::ExprWithCleanupsClass: | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3744 | return EvalVal(cast<ExprWithCleanups>(E)->getSubExpr(), refVars,ParentDecl); | 
| John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3745 |  | 
| Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3746 | case Stmt::DeclRefExprClass: { | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3747 | // When we hit a DeclRefExpr we are looking at code that refers to a | 
|  | 3748 | // variable's name. If it's not a reference variable we check if it has | 
|  | 3749 | // local storage within the function, and if so, return the expression. | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3750 | DeclRefExpr *DR = cast<DeclRefExpr>(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3751 |  | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3752 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) { | 
|  | 3753 | // Check if it refers to itself, e.g. "int& i = i;". | 
|  | 3754 | if (V == ParentDecl) | 
|  | 3755 | return DR; | 
|  | 3756 |  | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3757 | if (V->hasLocalStorage()) { | 
|  | 3758 | if (!V->getType()->isReferenceType()) | 
|  | 3759 | return DR; | 
|  | 3760 |  | 
|  | 3761 | // Reference variable, follow through to the expression that | 
|  | 3762 | // it points to. | 
|  | 3763 | if (V->hasInit()) { | 
|  | 3764 | // Add the reference variable to the "trail". | 
|  | 3765 | refVars.push_back(DR); | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3766 | return EvalVal(V->getInit(), refVars, V); | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3767 | } | 
|  | 3768 | } | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3769 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3771 | return NULL; | 
|  | 3772 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3774 | case Stmt::UnaryOperatorClass: { | 
|  | 3775 | // The only unary operator that make sense to handle here | 
|  | 3776 | // is Deref.  All others don't resolve to a "name."  This includes | 
|  | 3777 | // handling all sorts of rvalues passed to a unary operator. | 
|  | 3778 | UnaryOperator *U = cast<UnaryOperator>(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3779 |  | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3780 | if (U->getOpcode() == UO_Deref) | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3781 | return EvalAddr(U->getSubExpr(), refVars, ParentDecl); | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3782 |  | 
|  | 3783 | return NULL; | 
|  | 3784 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3785 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3786 | case Stmt::ArraySubscriptExprClass: { | 
|  | 3787 | // Array subscripts are potential references to data on the stack.  We | 
|  | 3788 | // retrieve the DeclRefExpr* for the array variable if it indeed | 
|  | 3789 | // has local storage. | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3790 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase(), refVars,ParentDecl); | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3791 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3792 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3793 | case Stmt::ConditionalOperatorClass: { | 
|  | 3794 | // For conditional operators we need to see if either the LHS or RHS are | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3795 | // non-NULL Expr's.  If one is non-NULL, we return it. | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3796 | ConditionalOperator *C = cast<ConditionalOperator>(E); | 
|  | 3797 |  | 
| Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3798 | // Handle the GNU extension for missing LHS. | 
|  | 3799 | if (Expr *lhsExpr = C->getLHS()) | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3800 | if (Expr *LHS = EvalVal(lhsExpr, refVars, ParentDecl)) | 
| Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 3801 | return LHS; | 
|  | 3802 |  | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3803 | return EvalVal(C->getRHS(), refVars, ParentDecl); | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3804 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3806 | // Accesses to members are potential references to data on the stack. | 
| Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 3807 | case Stmt::MemberExprClass: { | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3808 | MemberExpr *M = cast<MemberExpr>(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3810 | // Check for indirect access.  We only want direct field accesses. | 
| Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3811 | if (M->isArrow()) | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3812 | return NULL; | 
| Ted Kremenek | a423e81 | 2010-09-02 01:12:13 +0000 | [diff] [blame] | 3813 |  | 
|  | 3814 | // Check whether the member type is itself a reference, in which case | 
|  | 3815 | // we're not going to refer to the member, but to what the member refers to. | 
|  | 3816 | if (M->getMemberDecl()->getType()->isReferenceType()) | 
|  | 3817 | return NULL; | 
|  | 3818 |  | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3819 | return EvalVal(M->getBase(), refVars, ParentDecl); | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3820 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 |  | 
| Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3822 | case Stmt::MaterializeTemporaryExprClass: | 
|  | 3823 | if (Expr *Result = EvalVal( | 
|  | 3824 | cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr(), | 
| Argyrios Kyrtzidis | e720ce7 | 2012-04-30 23:23:55 +0000 | [diff] [blame] | 3825 | refVars, ParentDecl)) | 
| Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3826 | return Result; | 
|  | 3827 |  | 
|  | 3828 | return E; | 
|  | 3829 |  | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3830 | default: | 
| Argyrios Kyrtzidis | 26e10be | 2010-11-30 22:57:32 +0000 | [diff] [blame] | 3831 | // Check that we don't return or take the address of a reference to a | 
|  | 3832 | // temporary. This is only useful in C++. | 
|  | 3833 | if (!E->isTypeDependent() && E->isRValue()) | 
|  | 3834 | return E; | 
|  | 3835 |  | 
|  | 3836 | // Everything else: we simply don't reason about them. | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3837 | return NULL; | 
|  | 3838 | } | 
| Ted Kremenek | 68957a9 | 2010-08-04 20:01:07 +0000 | [diff] [blame] | 3839 | } while (true); | 
| Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 3840 | } | 
| Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3841 |  | 
|  | 3842 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// | 
|  | 3843 |  | 
|  | 3844 | /// Check for comparisons of floating point operands using != and ==. | 
|  | 3845 | /// Issue a warning if these are no self-comparisons, as they are not likely | 
|  | 3846 | /// to do what the programmer intended. | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3847 | void Sema::CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr *RHS) { | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3848 | Expr* LeftExprSansParen = LHS->IgnoreParenImpCasts(); | 
|  | 3849 | Expr* RightExprSansParen = RHS->IgnoreParenImpCasts(); | 
| Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3850 |  | 
|  | 3851 | // Special case: check for x == x (which is OK). | 
|  | 3852 | // Do not emit warnings for such cases. | 
|  | 3853 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) | 
|  | 3854 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) | 
|  | 3855 | if (DRL->getDecl() == DRR->getDecl()) | 
| David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3856 | return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 |  | 
|  | 3858 |  | 
| Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 3859 | // Special case: check for comparisons against literals that can be exactly | 
|  | 3860 | //  represented by APFloat.  In such cases, do not emit a warning.  This | 
|  | 3861 | //  is a heuristic: often comparison against such literals are used to | 
|  | 3862 | //  detect if a value in a variable has not changed.  This clearly can | 
|  | 3863 | //  lead to false negatives. | 
| David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3864 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { | 
|  | 3865 | if (FLL->isExact()) | 
|  | 3866 | return; | 
|  | 3867 | } else | 
|  | 3868 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)) | 
|  | 3869 | if (FLR->isExact()) | 
|  | 3870 | return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3871 |  | 
| Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3872 | // Check for comparisons with builtin types. | 
| David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3873 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) | 
|  | 3874 | if (CL->isBuiltinCall()) | 
|  | 3875 | return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3876 |  | 
| David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3877 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) | 
|  | 3878 | if (CR->isBuiltinCall()) | 
|  | 3879 | return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 |  | 
| Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3881 | // Emit the diagnostic. | 
| David Blaikie | 980343b | 2012-07-16 20:47:22 +0000 | [diff] [blame] | 3882 | Diag(Loc, diag::warn_floatingpoint_eq) | 
|  | 3883 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
| Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 3884 | } | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3885 |  | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3886 | //===--- CHECK: Integer mixed-sign comparisons (-Wsign-compare) --------===// | 
|  | 3887 | //===--- CHECK: Lossy implicit conversions (-Wconversion) --------------===// | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3888 |  | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3889 | namespace { | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3890 |  | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3891 | /// Structure recording the 'active' range of an integer-valued | 
|  | 3892 | /// expression. | 
|  | 3893 | struct IntRange { | 
|  | 3894 | /// The number of bits active in the int. | 
|  | 3895 | unsigned Width; | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3896 |  | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3897 | /// True if the int is known not to have negative values. | 
|  | 3898 | bool NonNegative; | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3899 |  | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3900 | IntRange(unsigned Width, bool NonNegative) | 
|  | 3901 | : Width(Width), NonNegative(NonNegative) | 
|  | 3902 | {} | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 3903 |  | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3904 | /// Returns the range of the bool type. | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3905 | static IntRange forBoolType() { | 
|  | 3906 | return IntRange(1, true); | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3907 | } | 
|  | 3908 |  | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3909 | /// Returns the range of an opaque value of the given integral type. | 
|  | 3910 | static IntRange forValueOfType(ASTContext &C, QualType T) { | 
|  | 3911 | return forValueOfCanonicalType(C, | 
|  | 3912 | T->getCanonicalTypeInternal().getTypePtr()); | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 3913 | } | 
|  | 3914 |  | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3915 | /// Returns the range of an opaque value of a canonical integral type. | 
|  | 3916 | static IntRange forValueOfCanonicalType(ASTContext &C, const Type *T) { | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3917 | assert(T->isCanonicalUnqualified()); | 
|  | 3918 |  | 
|  | 3919 | if (const VectorType *VT = dyn_cast<VectorType>(T)) | 
|  | 3920 | T = VT->getElementType().getTypePtr(); | 
|  | 3921 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) | 
|  | 3922 | T = CT->getElementType().getTypePtr(); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3923 |  | 
| John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3924 | // For enum types, use the known bit width of the enumerators. | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3925 | if (const EnumType *ET = dyn_cast<EnumType>(T)) { | 
|  | 3926 | EnumDecl *Enum = ET->getDecl(); | 
| John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3927 | if (!Enum->isCompleteDefinition()) | 
| John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3928 | return IntRange(C.getIntWidth(QualType(T, 0)), false); | 
|  | 3929 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3930 | unsigned NumPositive = Enum->getNumPositiveBits(); | 
|  | 3931 | unsigned NumNegative = Enum->getNumNegativeBits(); | 
|  | 3932 |  | 
| Richard Trieu | 8f50b24 | 2012-11-16 01:32:40 +0000 | [diff] [blame] | 3933 | if (NumNegative == 0) | 
|  | 3934 | return IntRange(NumPositive, true/*NonNegative*/); | 
|  | 3935 | else | 
|  | 3936 | return IntRange(std::max(NumPositive + 1, NumNegative), | 
|  | 3937 | false/*NonNegative*/); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 3938 | } | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3939 |  | 
|  | 3940 | const BuiltinType *BT = cast<BuiltinType>(T); | 
|  | 3941 | assert(BT->isInteger()); | 
|  | 3942 |  | 
|  | 3943 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); | 
|  | 3944 | } | 
|  | 3945 |  | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3946 | /// Returns the "target" range of a canonical integral type, i.e. | 
|  | 3947 | /// the range of values expressible in the type. | 
|  | 3948 | /// | 
|  | 3949 | /// This matches forValueOfCanonicalType except that enums have the | 
|  | 3950 | /// full range of their type, not the range of their enumerators. | 
|  | 3951 | static IntRange forTargetOfCanonicalType(ASTContext &C, const Type *T) { | 
|  | 3952 | assert(T->isCanonicalUnqualified()); | 
|  | 3953 |  | 
|  | 3954 | if (const VectorType *VT = dyn_cast<VectorType>(T)) | 
|  | 3955 | T = VT->getElementType().getTypePtr(); | 
|  | 3956 | if (const ComplexType *CT = dyn_cast<ComplexType>(T)) | 
|  | 3957 | T = CT->getElementType().getTypePtr(); | 
|  | 3958 | if (const EnumType *ET = dyn_cast<EnumType>(T)) | 
| Douglas Gregor | 69ff26b | 2011-09-08 23:29:05 +0000 | [diff] [blame] | 3959 | T = C.getCanonicalType(ET->getDecl()->getIntegerType()).getTypePtr(); | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3960 |  | 
|  | 3961 | const BuiltinType *BT = cast<BuiltinType>(T); | 
|  | 3962 | assert(BT->isInteger()); | 
|  | 3963 |  | 
|  | 3964 | return IntRange(C.getIntWidth(QualType(T, 0)), BT->isUnsignedInteger()); | 
|  | 3965 | } | 
|  | 3966 |  | 
|  | 3967 | /// Returns the supremum of two ranges: i.e. their conservative merge. | 
| John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3968 | static IntRange join(IntRange L, IntRange R) { | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3969 | return IntRange(std::max(L.Width, R.Width), | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3970 | L.NonNegative && R.NonNegative); | 
|  | 3971 | } | 
|  | 3972 |  | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 3973 | /// Returns the infinum of two ranges: i.e. their aggressive merge. | 
| John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 3974 | static IntRange meet(IntRange L, IntRange R) { | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 3975 | return IntRange(std::min(L.Width, R.Width), | 
|  | 3976 | L.NonNegative || R.NonNegative); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3977 | } | 
|  | 3978 | }; | 
|  | 3979 |  | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3980 | static IntRange GetValueRange(ASTContext &C, llvm::APSInt &value, | 
|  | 3981 | unsigned MaxWidth) { | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3982 | if (value.isSigned() && value.isNegative()) | 
|  | 3983 | return IntRange(value.getMinSignedBits(), false); | 
|  | 3984 |  | 
|  | 3985 | if (value.getBitWidth() > MaxWidth) | 
| Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3986 | value = value.trunc(MaxWidth); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3987 |  | 
|  | 3988 | // isNonNegative() just checks the sign bit without considering | 
|  | 3989 | // signedness. | 
|  | 3990 | return IntRange(value.getActiveBits(), true); | 
|  | 3991 | } | 
|  | 3992 |  | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 3993 | static IntRange GetValueRange(ASTContext &C, APValue &result, QualType Ty, | 
|  | 3994 | unsigned MaxWidth) { | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 3995 | if (result.isInt()) | 
|  | 3996 | return GetValueRange(C, result.getInt(), MaxWidth); | 
|  | 3997 |  | 
|  | 3998 | if (result.isVector()) { | 
| John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 3999 | IntRange R = GetValueRange(C, result.getVectorElt(0), Ty, MaxWidth); | 
|  | 4000 | for (unsigned i = 1, e = result.getVectorLength(); i != e; ++i) { | 
|  | 4001 | IntRange El = GetValueRange(C, result.getVectorElt(i), Ty, MaxWidth); | 
|  | 4002 | R = IntRange::join(R, El); | 
|  | 4003 | } | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4004 | return R; | 
|  | 4005 | } | 
|  | 4006 |  | 
|  | 4007 | if (result.isComplexInt()) { | 
|  | 4008 | IntRange R = GetValueRange(C, result.getComplexIntReal(), MaxWidth); | 
|  | 4009 | IntRange I = GetValueRange(C, result.getComplexIntImag(), MaxWidth); | 
|  | 4010 | return IntRange::join(R, I); | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4011 | } | 
|  | 4012 |  | 
|  | 4013 | // This can happen with lossless casts to intptr_t of "based" lvalues. | 
|  | 4014 | // Assume it might use arbitrary bits. | 
| John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 4015 | // FIXME: The only reason we need to pass the type in here is to get | 
|  | 4016 | // the sign right on this one case.  It would be nice if APValue | 
|  | 4017 | // preserved this. | 
| Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4018 | assert(result.isLValue() || result.isAddrLabelDiff()); | 
| Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 4019 | return IntRange(MaxWidth, Ty->isUnsignedIntegerOrEnumerationType()); | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4020 | } | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4021 |  | 
|  | 4022 | /// Pseudo-evaluate the given integer expression, estimating the | 
|  | 4023 | /// range of values it might take. | 
|  | 4024 | /// | 
|  | 4025 | /// \param MaxWidth - the width to which the value will be truncated | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4026 | static IntRange GetExprRange(ASTContext &C, Expr *E, unsigned MaxWidth) { | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4027 | E = E->IgnoreParens(); | 
|  | 4028 |  | 
|  | 4029 | // Try a full evaluation first. | 
|  | 4030 | Expr::EvalResult result; | 
| Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4031 | if (E->EvaluateAsRValue(result, C)) | 
| John McCall | 0acc311 | 2010-01-06 22:57:21 +0000 | [diff] [blame] | 4032 | return GetValueRange(C, result.Val, E->getType(), MaxWidth); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4033 |  | 
|  | 4034 | // I think we only want to look through implicit casts here; if the | 
|  | 4035 | // user has an explicit widening cast, we should treat the value as | 
|  | 4036 | // being of the new, wider type. | 
|  | 4037 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) { | 
| Eli Friedman | b17ee5b | 2011-12-15 02:41:52 +0000 | [diff] [blame] | 4038 | if (CE->getCastKind() == CK_NoOp || CE->getCastKind() == CK_LValueToRValue) | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4039 | return GetExprRange(C, CE->getSubExpr(), MaxWidth); | 
|  | 4040 |  | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4041 | IntRange OutputTypeRange = IntRange::forValueOfType(C, CE->getType()); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4042 |  | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4043 | bool isIntegerCast = (CE->getCastKind() == CK_IntegralCast); | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4044 |  | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4045 | // Assume that non-integer casts can span the full range of the type. | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4046 | if (!isIntegerCast) | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4047 | return OutputTypeRange; | 
|  | 4048 |  | 
|  | 4049 | IntRange SubRange | 
|  | 4050 | = GetExprRange(C, CE->getSubExpr(), | 
|  | 4051 | std::min(MaxWidth, OutputTypeRange.Width)); | 
|  | 4052 |  | 
|  | 4053 | // Bail out if the subexpr's range is as wide as the cast type. | 
|  | 4054 | if (SubRange.Width >= OutputTypeRange.Width) | 
|  | 4055 | return OutputTypeRange; | 
|  | 4056 |  | 
|  | 4057 | // Otherwise, we take the smaller width, and we're non-negative if | 
|  | 4058 | // either the output type or the subexpr is. | 
|  | 4059 | return IntRange(SubRange.Width, | 
|  | 4060 | SubRange.NonNegative || OutputTypeRange.NonNegative); | 
|  | 4061 | } | 
|  | 4062 |  | 
|  | 4063 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { | 
|  | 4064 | // If we can fold the condition, just take that operand. | 
|  | 4065 | bool CondResult; | 
|  | 4066 | if (CO->getCond()->EvaluateAsBooleanCondition(CondResult, C)) | 
|  | 4067 | return GetExprRange(C, CondResult ? CO->getTrueExpr() | 
|  | 4068 | : CO->getFalseExpr(), | 
|  | 4069 | MaxWidth); | 
|  | 4070 |  | 
|  | 4071 | // Otherwise, conservatively merge. | 
|  | 4072 | IntRange L = GetExprRange(C, CO->getTrueExpr(), MaxWidth); | 
|  | 4073 | IntRange R = GetExprRange(C, CO->getFalseExpr(), MaxWidth); | 
|  | 4074 | return IntRange::join(L, R); | 
|  | 4075 | } | 
|  | 4076 |  | 
|  | 4077 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { | 
|  | 4078 | switch (BO->getOpcode()) { | 
|  | 4079 |  | 
|  | 4080 | // Boolean-valued operations are single-bit and positive. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4081 | case BO_LAnd: | 
|  | 4082 | case BO_LOr: | 
|  | 4083 | case BO_LT: | 
|  | 4084 | case BO_GT: | 
|  | 4085 | case BO_LE: | 
|  | 4086 | case BO_GE: | 
|  | 4087 | case BO_EQ: | 
|  | 4088 | case BO_NE: | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4089 | return IntRange::forBoolType(); | 
|  | 4090 |  | 
| John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 4091 | // The type of the assignments is the type of the LHS, so the RHS | 
|  | 4092 | // is not necessarily the same type. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4093 | case BO_MulAssign: | 
|  | 4094 | case BO_DivAssign: | 
|  | 4095 | case BO_RemAssign: | 
|  | 4096 | case BO_AddAssign: | 
|  | 4097 | case BO_SubAssign: | 
| John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 4098 | case BO_XorAssign: | 
|  | 4099 | case BO_OrAssign: | 
|  | 4100 | // TODO: bitfields? | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4101 | return IntRange::forValueOfType(C, E->getType()); | 
| John McCall | c0cd21d | 2010-02-23 19:22:29 +0000 | [diff] [blame] | 4102 |  | 
| John McCall | 862ff87 | 2011-07-13 06:35:24 +0000 | [diff] [blame] | 4103 | // Simple assignments just pass through the RHS, which will have | 
|  | 4104 | // been coerced to the LHS type. | 
|  | 4105 | case BO_Assign: | 
|  | 4106 | // TODO: bitfields? | 
|  | 4107 | return GetExprRange(C, BO->getRHS(), MaxWidth); | 
|  | 4108 |  | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4109 | // Operations with opaque sources are black-listed. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4110 | case BO_PtrMemD: | 
|  | 4111 | case BO_PtrMemI: | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4112 | return IntRange::forValueOfType(C, E->getType()); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4113 |  | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4114 | // Bitwise-and uses the *infinum* of the two source ranges. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4115 | case BO_And: | 
|  | 4116 | case BO_AndAssign: | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4117 | return IntRange::meet(GetExprRange(C, BO->getLHS(), MaxWidth), | 
|  | 4118 | GetExprRange(C, BO->getRHS(), MaxWidth)); | 
|  | 4119 |  | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4120 | // Left shift gets black-listed based on a judgement call. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4121 | case BO_Shl: | 
| John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 4122 | // ...except that we want to treat '1 << (blah)' as logically | 
|  | 4123 | // positive.  It's an important idiom. | 
|  | 4124 | if (IntegerLiteral *I | 
|  | 4125 | = dyn_cast<IntegerLiteral>(BO->getLHS()->IgnoreParenCasts())) { | 
|  | 4126 | if (I->getValue() == 1) { | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4127 | IntRange R = IntRange::forValueOfType(C, E->getType()); | 
| John McCall | 3aae609 | 2010-04-07 01:14:35 +0000 | [diff] [blame] | 4128 | return IntRange(R.Width, /*NonNegative*/ true); | 
|  | 4129 | } | 
|  | 4130 | } | 
|  | 4131 | // fallthrough | 
|  | 4132 |  | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4133 | case BO_ShlAssign: | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4134 | return IntRange::forValueOfType(C, E->getType()); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4135 |  | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4136 | // Right shift by a constant can narrow its left argument. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4137 | case BO_Shr: | 
|  | 4138 | case BO_ShrAssign: { | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4139 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); | 
|  | 4140 |  | 
|  | 4141 | // If the shift amount is a positive constant, drop the width by | 
|  | 4142 | // that much. | 
|  | 4143 | llvm::APSInt shift; | 
|  | 4144 | if (BO->getRHS()->isIntegerConstantExpr(shift, C) && | 
|  | 4145 | shift.isNonNegative()) { | 
|  | 4146 | unsigned zext = shift.getZExtValue(); | 
|  | 4147 | if (zext >= L.Width) | 
|  | 4148 | L.Width = (L.NonNegative ? 0 : 1); | 
|  | 4149 | else | 
|  | 4150 | L.Width -= zext; | 
|  | 4151 | } | 
|  | 4152 |  | 
|  | 4153 | return L; | 
|  | 4154 | } | 
|  | 4155 |  | 
|  | 4156 | // Comma acts as its right operand. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4157 | case BO_Comma: | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4158 | return GetExprRange(C, BO->getRHS(), MaxWidth); | 
|  | 4159 |  | 
| John McCall | 60fad45 | 2010-01-06 22:07:33 +0000 | [diff] [blame] | 4160 | // Black-list pointer subtractions. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4161 | case BO_Sub: | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4162 | if (BO->getLHS()->getType()->isPointerType()) | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4163 | return IntRange::forValueOfType(C, E->getType()); | 
| John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 4164 | break; | 
| Ted Kremenek | 4e4b30e | 2010-02-16 01:46:59 +0000 | [diff] [blame] | 4165 |  | 
| John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 4166 | // The width of a division result is mostly determined by the size | 
|  | 4167 | // of the LHS. | 
|  | 4168 | case BO_Div: { | 
|  | 4169 | // Don't 'pre-truncate' the operands. | 
|  | 4170 | unsigned opWidth = C.getIntWidth(E->getType()); | 
|  | 4171 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); | 
|  | 4172 |  | 
|  | 4173 | // If the divisor is constant, use that. | 
|  | 4174 | llvm::APSInt divisor; | 
|  | 4175 | if (BO->getRHS()->isIntegerConstantExpr(divisor, C)) { | 
|  | 4176 | unsigned log2 = divisor.logBase2(); // floor(log_2(divisor)) | 
|  | 4177 | if (log2 >= L.Width) | 
|  | 4178 | L.Width = (L.NonNegative ? 0 : 1); | 
|  | 4179 | else | 
|  | 4180 | L.Width = std::min(L.Width - log2, MaxWidth); | 
|  | 4181 | return L; | 
|  | 4182 | } | 
|  | 4183 |  | 
|  | 4184 | // Otherwise, just use the LHS's width. | 
|  | 4185 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); | 
|  | 4186 | return IntRange(L.Width, L.NonNegative && R.NonNegative); | 
|  | 4187 | } | 
|  | 4188 |  | 
|  | 4189 | // The result of a remainder can't be larger than the result of | 
|  | 4190 | // either side. | 
|  | 4191 | case BO_Rem: { | 
|  | 4192 | // Don't 'pre-truncate' the operands. | 
|  | 4193 | unsigned opWidth = C.getIntWidth(E->getType()); | 
|  | 4194 | IntRange L = GetExprRange(C, BO->getLHS(), opWidth); | 
|  | 4195 | IntRange R = GetExprRange(C, BO->getRHS(), opWidth); | 
|  | 4196 |  | 
|  | 4197 | IntRange meet = IntRange::meet(L, R); | 
|  | 4198 | meet.Width = std::min(meet.Width, MaxWidth); | 
|  | 4199 | return meet; | 
|  | 4200 | } | 
|  | 4201 |  | 
|  | 4202 | // The default behavior is okay for these. | 
|  | 4203 | case BO_Mul: | 
|  | 4204 | case BO_Add: | 
|  | 4205 | case BO_Xor: | 
|  | 4206 | case BO_Or: | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4207 | break; | 
|  | 4208 | } | 
|  | 4209 |  | 
| John McCall | 00fe761 | 2011-07-14 22:39:48 +0000 | [diff] [blame] | 4210 | // The default case is to treat the operation as if it were closed | 
|  | 4211 | // on the narrowest type that encompasses both operands. | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4212 | IntRange L = GetExprRange(C, BO->getLHS(), MaxWidth); | 
|  | 4213 | IntRange R = GetExprRange(C, BO->getRHS(), MaxWidth); | 
|  | 4214 | return IntRange::join(L, R); | 
|  | 4215 | } | 
|  | 4216 |  | 
|  | 4217 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { | 
|  | 4218 | switch (UO->getOpcode()) { | 
|  | 4219 | // Boolean-valued operations are white-listed. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4220 | case UO_LNot: | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4221 | return IntRange::forBoolType(); | 
|  | 4222 |  | 
|  | 4223 | // Operations with opaque sources are black-listed. | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4224 | case UO_Deref: | 
|  | 4225 | case UO_AddrOf: // should be impossible | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4226 | return IntRange::forValueOfType(C, E->getType()); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4227 |  | 
|  | 4228 | default: | 
|  | 4229 | return GetExprRange(C, UO->getSubExpr(), MaxWidth); | 
|  | 4230 | } | 
|  | 4231 | } | 
| Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4232 |  | 
|  | 4233 | if (dyn_cast<OffsetOfExpr>(E)) { | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4234 | IntRange::forValueOfType(C, E->getType()); | 
| Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4235 | } | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4236 |  | 
| Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4237 | if (FieldDecl *BitField = E->getBitField()) | 
|  | 4238 | return IntRange(BitField->getBitWidthValue(C), | 
| Douglas Gregor | 5e9ebb3 | 2011-05-21 16:28:01 +0000 | [diff] [blame] | 4239 | BitField->getType()->isUnsignedIntegerOrEnumerationType()); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4240 |  | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4241 | return IntRange::forValueOfType(C, E->getType()); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4242 | } | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4243 |  | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4244 | static IntRange GetExprRange(ASTContext &C, Expr *E) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4245 | return GetExprRange(C, E, C.getIntWidth(E->getType())); | 
|  | 4246 | } | 
|  | 4247 |  | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4248 | /// Checks whether the given value, which currently has the given | 
|  | 4249 | /// source semantics, has the same value when coerced through the | 
|  | 4250 | /// target semantics. | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4251 | static bool IsSameFloatAfterCast(const llvm::APFloat &value, | 
|  | 4252 | const llvm::fltSemantics &Src, | 
|  | 4253 | const llvm::fltSemantics &Tgt) { | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4254 | llvm::APFloat truncated = value; | 
|  | 4255 |  | 
|  | 4256 | bool ignored; | 
|  | 4257 | truncated.convert(Src, llvm::APFloat::rmNearestTiesToEven, &ignored); | 
|  | 4258 | truncated.convert(Tgt, llvm::APFloat::rmNearestTiesToEven, &ignored); | 
|  | 4259 |  | 
|  | 4260 | return truncated.bitwiseIsEqual(value); | 
|  | 4261 | } | 
|  | 4262 |  | 
|  | 4263 | /// Checks whether the given value, which currently has the given | 
|  | 4264 | /// source semantics, has the same value when coerced through the | 
|  | 4265 | /// target semantics. | 
|  | 4266 | /// | 
|  | 4267 | /// The value might be a vector of floats (or a complex number). | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4268 | static bool IsSameFloatAfterCast(const APValue &value, | 
|  | 4269 | const llvm::fltSemantics &Src, | 
|  | 4270 | const llvm::fltSemantics &Tgt) { | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4271 | if (value.isFloat()) | 
|  | 4272 | return IsSameFloatAfterCast(value.getFloat(), Src, Tgt); | 
|  | 4273 |  | 
|  | 4274 | if (value.isVector()) { | 
|  | 4275 | for (unsigned i = 0, e = value.getVectorLength(); i != e; ++i) | 
|  | 4276 | if (!IsSameFloatAfterCast(value.getVectorElt(i), Src, Tgt)) | 
|  | 4277 | return false; | 
|  | 4278 | return true; | 
|  | 4279 | } | 
|  | 4280 |  | 
|  | 4281 | assert(value.isComplexFloat()); | 
|  | 4282 | return (IsSameFloatAfterCast(value.getComplexFloatReal(), Src, Tgt) && | 
|  | 4283 | IsSameFloatAfterCast(value.getComplexFloatImag(), Src, Tgt)); | 
|  | 4284 | } | 
|  | 4285 |  | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4286 | static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4287 |  | 
| Ted Kremenek | e3b159c | 2010-09-23 21:43:44 +0000 | [diff] [blame] | 4288 | static bool IsZero(Sema &S, Expr *E) { | 
|  | 4289 | // Suppress cases where we are comparing against an enum constant. | 
|  | 4290 | if (const DeclRefExpr *DR = | 
|  | 4291 | dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) | 
|  | 4292 | if (isa<EnumConstantDecl>(DR->getDecl())) | 
|  | 4293 | return false; | 
|  | 4294 |  | 
|  | 4295 | // Suppress cases where the '0' value is expanded from a macro. | 
|  | 4296 | if (E->getLocStart().isMacroID()) | 
|  | 4297 | return false; | 
|  | 4298 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4299 | llvm::APSInt Value; | 
|  | 4300 | return E->isIntegerConstantExpr(Value, S.Context) && Value == 0; | 
|  | 4301 | } | 
|  | 4302 |  | 
| John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4303 | static bool HasEnumType(Expr *E) { | 
|  | 4304 | // Strip off implicit integral promotions. | 
|  | 4305 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { | 
| Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 4306 | if (ICE->getCastKind() != CK_IntegralCast && | 
|  | 4307 | ICE->getCastKind() != CK_NoOp) | 
| John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4308 | break; | 
| Argyrios Kyrtzidis | 63b57ae | 2010-10-07 21:52:18 +0000 | [diff] [blame] | 4309 | E = ICE->getSubExpr(); | 
| John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4310 | } | 
|  | 4311 |  | 
|  | 4312 | return E->getType()->isEnumeralType(); | 
|  | 4313 | } | 
|  | 4314 |  | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4315 | static void CheckTrivialUnsignedComparison(Sema &S, BinaryOperator *E) { | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4316 | BinaryOperatorKind op = E->getOpcode(); | 
| Douglas Gregor | 14af91a | 2010-12-21 07:22:56 +0000 | [diff] [blame] | 4317 | if (E->isValueDependent()) | 
|  | 4318 | return; | 
|  | 4319 |  | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4320 | if (op == BO_LT && IsZero(S, E->getRHS())) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4321 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) | 
| John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4322 | << "< 0" << "false" << HasEnumType(E->getLHS()) | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4323 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4324 | } else if (op == BO_GE && IsZero(S, E->getRHS())) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4325 | S.Diag(E->getOperatorLoc(), diag::warn_lunsigned_always_true_comparison) | 
| John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4326 | << ">= 0" << "true" << HasEnumType(E->getLHS()) | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4327 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4328 | } else if (op == BO_GT && IsZero(S, E->getLHS())) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4329 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) | 
| John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4330 | << "0 >" << "false" << HasEnumType(E->getRHS()) | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4331 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4332 | } else if (op == BO_LE && IsZero(S, E->getLHS())) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4333 | S.Diag(E->getOperatorLoc(), diag::warn_runsigned_always_true_comparison) | 
| John McCall | 372e103 | 2010-10-06 00:25:24 +0000 | [diff] [blame] | 4334 | << "0 <=" << "true" << HasEnumType(E->getRHS()) | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4335 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); | 
|  | 4336 | } | 
|  | 4337 | } | 
|  | 4338 |  | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4339 | static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E, | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4340 | Expr *Constant, Expr *Other, | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4341 | llvm::APSInt Value, | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4342 | bool RhsConstant) { | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4343 | // 0 values are handled later by CheckTrivialUnsignedComparison(). | 
|  | 4344 | if (Value == 0) | 
|  | 4345 | return; | 
|  | 4346 |  | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4347 | BinaryOperatorKind op = E->getOpcode(); | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4348 | QualType OtherT = Other->getType(); | 
|  | 4349 | QualType ConstantT = Constant->getType(); | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4350 | QualType CommonT = E->getLHS()->getType(); | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4351 | if (S.Context.hasSameUnqualifiedType(OtherT, ConstantT)) | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4352 | return; | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4353 | assert((OtherT->isIntegerType() && ConstantT->isIntegerType()) | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4354 | && "comparison with non-integer type"); | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4355 |  | 
|  | 4356 | bool ConstantSigned = ConstantT->isSignedIntegerType(); | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4357 | bool CommonSigned = CommonT->isSignedIntegerType(); | 
|  | 4358 |  | 
|  | 4359 | bool EqualityOnly = false; | 
|  | 4360 |  | 
|  | 4361 | // TODO: Investigate using GetExprRange() to get tighter bounds on | 
|  | 4362 | // on the bit ranges. | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4363 | IntRange OtherRange = IntRange::forValueOfType(S.Context, OtherT); | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4364 | unsigned OtherWidth = OtherRange.Width; | 
|  | 4365 |  | 
|  | 4366 | if (CommonSigned) { | 
|  | 4367 | // The common type is signed, therefore no signed to unsigned conversion. | 
| Eli Friedman | d87de7b | 2012-11-30 23:09:29 +0000 | [diff] [blame] | 4368 | if (!OtherRange.NonNegative) { | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4369 | // Check that the constant is representable in type OtherT. | 
|  | 4370 | if (ConstantSigned) { | 
|  | 4371 | if (OtherWidth >= Value.getMinSignedBits()) | 
|  | 4372 | return; | 
|  | 4373 | } else { // !ConstantSigned | 
|  | 4374 | if (OtherWidth >= Value.getActiveBits() + 1) | 
|  | 4375 | return; | 
|  | 4376 | } | 
|  | 4377 | } else { // !OtherSigned | 
|  | 4378 | // Check that the constant is representable in type OtherT. | 
|  | 4379 | // Negative values are out of range. | 
|  | 4380 | if (ConstantSigned) { | 
|  | 4381 | if (Value.isNonNegative() && OtherWidth >= Value.getActiveBits()) | 
|  | 4382 | return; | 
|  | 4383 | } else { // !ConstantSigned | 
|  | 4384 | if (OtherWidth >= Value.getActiveBits()) | 
|  | 4385 | return; | 
|  | 4386 | } | 
|  | 4387 | } | 
|  | 4388 | } else {  // !CommonSigned | 
| Eli Friedman | d87de7b | 2012-11-30 23:09:29 +0000 | [diff] [blame] | 4389 | if (OtherRange.NonNegative) { | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4390 | if (OtherWidth >= Value.getActiveBits()) | 
|  | 4391 | return; | 
| Eli Friedman | d87de7b | 2012-11-30 23:09:29 +0000 | [diff] [blame] | 4392 | } else if (!OtherRange.NonNegative && !ConstantSigned) { | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4393 | // Check to see if the constant is representable in OtherT. | 
|  | 4394 | if (OtherWidth > Value.getActiveBits()) | 
|  | 4395 | return; | 
|  | 4396 | // Check to see if the constant is equivalent to a negative value | 
|  | 4397 | // cast to CommonT. | 
|  | 4398 | if (S.Context.getIntWidth(ConstantT) == S.Context.getIntWidth(CommonT) && | 
| Richard Trieu | 5d1cf4f | 2012-11-15 03:43:50 +0000 | [diff] [blame] | 4399 | Value.isNegative() && Value.getMinSignedBits() <= OtherWidth) | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4400 | return; | 
|  | 4401 | // The constant value rests between values that OtherT can represent after | 
|  | 4402 | // conversion.  Relational comparison still works, but equality | 
|  | 4403 | // comparisons will be tautological. | 
|  | 4404 | EqualityOnly = true; | 
|  | 4405 | } else { // OtherSigned && ConstantSigned | 
|  | 4406 | assert(0 && "Two signed types converted to unsigned types."); | 
|  | 4407 | } | 
|  | 4408 | } | 
|  | 4409 |  | 
|  | 4410 | bool PositiveConstant = !ConstantSigned || Value.isNonNegative(); | 
|  | 4411 |  | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4412 | bool IsTrue = true; | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4413 | if (op == BO_EQ || op == BO_NE) { | 
|  | 4414 | IsTrue = op == BO_NE; | 
|  | 4415 | } else if (EqualityOnly) { | 
|  | 4416 | return; | 
|  | 4417 | } else if (RhsConstant) { | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4418 | if (op == BO_GT || op == BO_GE) | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4419 | IsTrue = !PositiveConstant; | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4420 | else // op == BO_LT || op == BO_LE | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4421 | IsTrue = PositiveConstant; | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4422 | } else { | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4423 | if (op == BO_LT || op == BO_LE) | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4424 | IsTrue = !PositiveConstant; | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4425 | else // op == BO_GT || op == BO_GE | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4426 | IsTrue = PositiveConstant; | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4427 | } | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4428 | SmallString<16> PrettySourceValue(Value.toString(10)); | 
|  | 4429 | S.Diag(E->getOperatorLoc(), diag::warn_out_of_range_compare) | 
| Richard Trieu | 526e627 | 2012-11-14 22:50:24 +0000 | [diff] [blame] | 4430 | << PrettySourceValue << OtherT << IsTrue | 
|  | 4431 | << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange(); | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4432 | } | 
|  | 4433 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4434 | /// Analyze the operands of the given comparison.  Implements the | 
|  | 4435 | /// fallback case from AnalyzeComparison. | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4436 | static void AnalyzeImpConvsInComparison(Sema &S, BinaryOperator *E) { | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4437 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); | 
|  | 4438 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4439 | } | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4440 |  | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4441 | /// \brief Implements -Wsign-compare. | 
|  | 4442 | /// | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4443 | /// \param E the binary operator to check for warnings | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4444 | static void AnalyzeComparison(Sema &S, BinaryOperator *E) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4445 | // The type the comparison is being performed in. | 
|  | 4446 | QualType T = E->getLHS()->getType(); | 
|  | 4447 | assert(S.Context.hasSameUnqualifiedType(T, E->getRHS()->getType()) | 
|  | 4448 | && "comparison with mismatched types"); | 
| Fariborz Jahanian | ab4702f | 2012-09-18 17:46:26 +0000 | [diff] [blame] | 4449 | if (E->isValueDependent()) | 
|  | 4450 | return AnalyzeImpConvsInComparison(S, E); | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4451 |  | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4452 | Expr *LHS = E->getLHS()->IgnoreParenImpCasts(); | 
|  | 4453 | Expr *RHS = E->getRHS()->IgnoreParenImpCasts(); | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4454 |  | 
|  | 4455 | bool IsComparisonConstant = false; | 
|  | 4456 |  | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4457 | // Check whether an integer constant comparison results in a value | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4458 | // of 'true' or 'false'. | 
|  | 4459 | if (T->isIntegralType(S.Context)) { | 
|  | 4460 | llvm::APSInt RHSValue; | 
|  | 4461 | bool IsRHSIntegralLiteral = | 
|  | 4462 | RHS->isIntegerConstantExpr(RHSValue, S.Context); | 
|  | 4463 | llvm::APSInt LHSValue; | 
|  | 4464 | bool IsLHSIntegralLiteral = | 
|  | 4465 | LHS->isIntegerConstantExpr(LHSValue, S.Context); | 
|  | 4466 | if (IsRHSIntegralLiteral && !IsLHSIntegralLiteral) | 
|  | 4467 | DiagnoseOutOfRangeComparison(S, E, RHS, LHS, RHSValue, true); | 
|  | 4468 | else if (!IsRHSIntegralLiteral && IsLHSIntegralLiteral) | 
|  | 4469 | DiagnoseOutOfRangeComparison(S, E, LHS, RHS, LHSValue, false); | 
|  | 4470 | else | 
|  | 4471 | IsComparisonConstant = | 
|  | 4472 | (IsRHSIntegralLiteral && IsLHSIntegralLiteral); | 
| Fariborz Jahanian | a193f20 | 2012-09-20 19:36:41 +0000 | [diff] [blame] | 4473 | } else if (!T->hasUnsignedIntegerRepresentation()) | 
|  | 4474 | IsComparisonConstant = E->isIntegerConstantExpr(S.Context); | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4475 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4476 | // We don't do anything special if this isn't an unsigned integral | 
|  | 4477 | // comparison:  we're only interested in integral comparisons, and | 
|  | 4478 | // signed comparisons only happen in cases we don't care to warn about. | 
| Douglas Gregor | 3e026e3 | 2011-02-19 22:34:59 +0000 | [diff] [blame] | 4479 | // | 
|  | 4480 | // We also don't care about value-dependent expressions or expressions | 
|  | 4481 | // whose result is a constant. | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4482 | if (!T->hasUnsignedIntegerRepresentation() || IsComparisonConstant) | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4483 | return AnalyzeImpConvsInComparison(S, E); | 
| Fariborz Jahanian | 15a9356 | 2012-09-18 17:37:21 +0000 | [diff] [blame] | 4484 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4485 | // Check to see if one of the (unmodified) operands is of different | 
|  | 4486 | // signedness. | 
|  | 4487 | Expr *signedOperand, *unsignedOperand; | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4488 | if (LHS->getType()->hasSignedIntegerRepresentation()) { | 
|  | 4489 | assert(!RHS->getType()->hasSignedIntegerRepresentation() && | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4490 | "unsigned comparison between two signed integer expressions?"); | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4491 | signedOperand = LHS; | 
|  | 4492 | unsignedOperand = RHS; | 
|  | 4493 | } else if (RHS->getType()->hasSignedIntegerRepresentation()) { | 
|  | 4494 | signedOperand = RHS; | 
|  | 4495 | unsignedOperand = LHS; | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4496 | } else { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4497 | CheckTrivialUnsignedComparison(S, E); | 
|  | 4498 | return AnalyzeImpConvsInComparison(S, E); | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4499 | } | 
|  | 4500 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4501 | // Otherwise, calculate the effective range of the signed operand. | 
|  | 4502 | IntRange signedRange = GetExprRange(S.Context, signedOperand); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4503 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4504 | // Go ahead and analyze implicit conversions in the operands.  Note | 
|  | 4505 | // that we skip the implicit conversions on both sides. | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 4506 | AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc()); | 
|  | 4507 | AnalyzeImplicitConversions(S, RHS, E->getOperatorLoc()); | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4508 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4509 | // If the signed range is non-negative, -Wsign-compare won't fire, | 
|  | 4510 | // but we should still check for comparisons which are always true | 
|  | 4511 | // or false. | 
|  | 4512 | if (signedRange.NonNegative) | 
|  | 4513 | return CheckTrivialUnsignedComparison(S, E); | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4514 |  | 
|  | 4515 | // For (in)equality comparisons, if the unsigned operand is a | 
|  | 4516 | // constant which cannot collide with a overflowed signed operand, | 
|  | 4517 | // then reinterpreting the signed operand as unsigned will not | 
|  | 4518 | // change the result of the comparison. | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4519 | if (E->isEqualityOp()) { | 
|  | 4520 | unsigned comparisonWidth = S.Context.getIntWidth(T); | 
|  | 4521 | IntRange unsignedRange = GetExprRange(S.Context, unsignedOperand); | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4522 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4523 | // We should never be unable to prove that the unsigned operand is | 
|  | 4524 | // non-negative. | 
|  | 4525 | assert(unsignedRange.NonNegative && "unsigned range includes negative?"); | 
|  | 4526 |  | 
|  | 4527 | if (unsignedRange.Width < comparisonWidth) | 
|  | 4528 | return; | 
|  | 4529 | } | 
|  | 4530 |  | 
| Douglas Gregor | 6d3b93d | 2012-05-01 01:53:49 +0000 | [diff] [blame] | 4531 | S.DiagRuntimeBehavior(E->getOperatorLoc(), E, | 
|  | 4532 | S.PDiag(diag::warn_mixed_sign_comparison) | 
|  | 4533 | << LHS->getType() << RHS->getType() | 
|  | 4534 | << LHS->getSourceRange() << RHS->getSourceRange()); | 
| John McCall | ba26e58 | 2010-01-04 23:21:16 +0000 | [diff] [blame] | 4535 | } | 
|  | 4536 |  | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4537 | /// Analyzes an attempt to assign the given value to a bitfield. | 
|  | 4538 | /// | 
|  | 4539 | /// Returns true if there was something fishy about the attempt. | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4540 | static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, | 
|  | 4541 | SourceLocation InitLoc) { | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4542 | assert(Bitfield->isBitField()); | 
|  | 4543 | if (Bitfield->isInvalidDecl()) | 
|  | 4544 | return false; | 
|  | 4545 |  | 
| John McCall | 91b6014 | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 4546 | // White-list bool bitfields. | 
|  | 4547 | if (Bitfield->getType()->isBooleanType()) | 
|  | 4548 | return false; | 
|  | 4549 |  | 
| Douglas Gregor | 46ff303 | 2011-02-04 13:09:01 +0000 | [diff] [blame] | 4550 | // Ignore value- or type-dependent expressions. | 
|  | 4551 | if (Bitfield->getBitWidth()->isValueDependent() || | 
|  | 4552 | Bitfield->getBitWidth()->isTypeDependent() || | 
|  | 4553 | Init->isValueDependent() || | 
|  | 4554 | Init->isTypeDependent()) | 
|  | 4555 | return false; | 
|  | 4556 |  | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4557 | Expr *OriginalInit = Init->IgnoreParenImpCasts(); | 
|  | 4558 |  | 
| Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4559 | llvm::APSInt Value; | 
|  | 4560 | if (!OriginalInit->EvaluateAsInt(Value, S.Context, Expr::SE_AllowSideEffects)) | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4561 | return false; | 
|  | 4562 |  | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4563 | unsigned OriginalWidth = Value.getBitWidth(); | 
| Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4564 | unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4565 |  | 
|  | 4566 | if (OriginalWidth <= FieldWidth) | 
|  | 4567 | return false; | 
|  | 4568 |  | 
| Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4569 | // Compute the value which the bitfield will contain. | 
| Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4570 | llvm::APSInt TruncatedValue = Value.trunc(FieldWidth); | 
| Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4571 | TruncatedValue.setIsSigned(Bitfield->getType()->isSignedIntegerType()); | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4572 |  | 
| Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4573 | // Check whether the stored value is equal to the original value. | 
|  | 4574 | TruncatedValue = TruncatedValue.extend(OriginalWidth); | 
| Richard Trieu | e1ecdc1 | 2012-07-23 20:21:35 +0000 | [diff] [blame] | 4575 | if (llvm::APSInt::isSameValue(Value, TruncatedValue)) | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4576 | return false; | 
|  | 4577 |  | 
| Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4578 | // Special-case bitfields of width 1: booleans are naturally 0/1, and | 
| Eli Friedman | 34ff062 | 2012-02-02 00:40:20 +0000 | [diff] [blame] | 4579 | // therefore don't strictly fit into a signed bitfield of width 1. | 
|  | 4580 | if (FieldWidth == 1 && Value == 1) | 
| Eli Friedman | 3a643af | 2012-01-26 23:11:39 +0000 | [diff] [blame] | 4581 | return false; | 
|  | 4582 |  | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4583 | std::string PrettyValue = Value.toString(10); | 
|  | 4584 | std::string PrettyTrunc = TruncatedValue.toString(10); | 
|  | 4585 |  | 
|  | 4586 | S.Diag(InitLoc, diag::warn_impcast_bitfield_precision_constant) | 
|  | 4587 | << PrettyValue << PrettyTrunc << OriginalInit->getType() | 
|  | 4588 | << Init->getSourceRange(); | 
|  | 4589 |  | 
|  | 4590 | return true; | 
|  | 4591 | } | 
|  | 4592 |  | 
| John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4593 | /// Analyze the given simple or compound assignment for warning-worthy | 
|  | 4594 | /// operations. | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4595 | static void AnalyzeAssignment(Sema &S, BinaryOperator *E) { | 
| John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4596 | // Just recurse on the LHS. | 
|  | 4597 | AnalyzeImplicitConversions(S, E->getLHS(), E->getOperatorLoc()); | 
|  | 4598 |  | 
|  | 4599 | // We want to recurse on the RHS as normal unless we're assigning to | 
|  | 4600 | // a bitfield. | 
|  | 4601 | if (FieldDecl *Bitfield = E->getLHS()->getBitField()) { | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4602 | if (AnalyzeBitFieldAssignment(S, Bitfield, E->getRHS(), | 
|  | 4603 | E->getOperatorLoc())) { | 
|  | 4604 | // Recurse, ignoring any implicit conversions on the RHS. | 
|  | 4605 | return AnalyzeImplicitConversions(S, E->getRHS()->IgnoreParenImpCasts(), | 
|  | 4606 | E->getOperatorLoc()); | 
| John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 4607 | } | 
|  | 4608 | } | 
|  | 4609 |  | 
|  | 4610 | AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc()); | 
|  | 4611 | } | 
|  | 4612 |  | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4613 | /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion. | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4614 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType SourceType, QualType T, | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4615 | SourceLocation CContext, unsigned diag, | 
|  | 4616 | bool pruneControlFlow = false) { | 
|  | 4617 | if (pruneControlFlow) { | 
|  | 4618 | S.DiagRuntimeBehavior(E->getExprLoc(), E, | 
|  | 4619 | S.PDiag(diag) | 
|  | 4620 | << SourceType << T << E->getSourceRange() | 
|  | 4621 | << SourceRange(CContext)); | 
|  | 4622 | return; | 
|  | 4623 | } | 
| Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4624 | S.Diag(E->getExprLoc(), diag) | 
|  | 4625 | << SourceType << T << E->getSourceRange() << SourceRange(CContext); | 
|  | 4626 | } | 
|  | 4627 |  | 
| Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4628 | /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion. | 
| Ted Kremenek | 0692a19 | 2012-01-31 05:37:37 +0000 | [diff] [blame] | 4629 | static void DiagnoseImpCast(Sema &S, Expr *E, QualType T, | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4630 | SourceLocation CContext, unsigned diag, | 
|  | 4631 | bool pruneControlFlow = false) { | 
|  | 4632 | DiagnoseImpCast(S, E, E->getType(), T, CContext, diag, pruneControlFlow); | 
| Chandler Carruth | e1b02e0 | 2011-04-05 06:47:57 +0000 | [diff] [blame] | 4633 | } | 
|  | 4634 |  | 
| Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4635 | /// Diagnose an implicit cast from a literal expression. Does not warn when the | 
|  | 4636 | /// cast wouldn't lose information. | 
| Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4637 | void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, | 
|  | 4638 | SourceLocation CContext) { | 
| Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4639 | // Try to convert the literal exactly to an integer. If we can, don't warn. | 
| Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4640 | bool isExact = false; | 
| Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4641 | const llvm::APFloat &Value = FL->getValue(); | 
| Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 4642 | llvm::APSInt IntegerValue(S.Context.getIntWidth(T), | 
|  | 4643 | T->hasUnsignedIntegerRepresentation()); | 
|  | 4644 | if (Value.convertToInteger(IntegerValue, | 
| Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4645 | llvm::APFloat::rmTowardZero, &isExact) | 
| Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4646 | == llvm::APFloat::opOK && isExact) | 
| Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4647 | return; | 
|  | 4648 |  | 
| David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4649 | SmallString<16> PrettySourceValue; | 
|  | 4650 | Value.toString(PrettySourceValue); | 
| David Blaikie | de7e7b8 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 4651 | SmallString<16> PrettyTargetValue; | 
| David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4652 | if (T->isSpecificBuiltinType(BuiltinType::Bool)) | 
|  | 4653 | PrettyTargetValue = IntegerValue == 0 ? "false" : "true"; | 
|  | 4654 | else | 
| David Blaikie | de7e7b8 | 2012-05-15 17:18:27 +0000 | [diff] [blame] | 4655 | IntegerValue.toString(PrettyTargetValue); | 
| David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4656 |  | 
| Matt Beaumont-Gay | 9ce6377 | 2011-10-14 15:36:25 +0000 | [diff] [blame] | 4657 | S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) | 
| David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4658 | << FL->getType() << T.getUnqualifiedType() << PrettySourceValue | 
|  | 4659 | << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext); | 
| Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4660 | } | 
|  | 4661 |  | 
| John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4662 | std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { | 
|  | 4663 | if (!Range.Width) return "0"; | 
|  | 4664 |  | 
|  | 4665 | llvm::APSInt ValueInRange = Value; | 
|  | 4666 | ValueInRange.setIsSigned(!Range.NonNegative); | 
| Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4667 | ValueInRange = ValueInRange.trunc(Range.Width); | 
| John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4668 | return ValueInRange.toString(10); | 
|  | 4669 | } | 
|  | 4670 |  | 
| Hans Wennborg | 88617a2 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 4671 | static bool IsImplicitBoolFloatConversion(Sema &S, Expr *Ex, bool ToBool) { | 
|  | 4672 | if (!isa<ImplicitCastExpr>(Ex)) | 
|  | 4673 | return false; | 
|  | 4674 |  | 
|  | 4675 | Expr *InnerE = Ex->IgnoreParenImpCasts(); | 
|  | 4676 | const Type *Target = S.Context.getCanonicalType(Ex->getType()).getTypePtr(); | 
|  | 4677 | const Type *Source = | 
|  | 4678 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); | 
|  | 4679 | if (Target->isDependentType()) | 
|  | 4680 | return false; | 
|  | 4681 |  | 
|  | 4682 | const BuiltinType *FloatCandidateBT = | 
|  | 4683 | dyn_cast<BuiltinType>(ToBool ? Source : Target); | 
|  | 4684 | const Type *BoolCandidateType = ToBool ? Target : Source; | 
|  | 4685 |  | 
|  | 4686 | return (BoolCandidateType->isSpecificBuiltinType(BuiltinType::Bool) && | 
|  | 4687 | FloatCandidateBT && (FloatCandidateBT->isFloatingPoint())); | 
|  | 4688 | } | 
|  | 4689 |  | 
|  | 4690 | void CheckImplicitArgumentConversions(Sema &S, CallExpr *TheCall, | 
|  | 4691 | SourceLocation CC) { | 
|  | 4692 | unsigned NumArgs = TheCall->getNumArgs(); | 
|  | 4693 | for (unsigned i = 0; i < NumArgs; ++i) { | 
|  | 4694 | Expr *CurrA = TheCall->getArg(i); | 
|  | 4695 | if (!IsImplicitBoolFloatConversion(S, CurrA, true)) | 
|  | 4696 | continue; | 
|  | 4697 |  | 
|  | 4698 | bool IsSwapped = ((i > 0) && | 
|  | 4699 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i - 1), false)); | 
|  | 4700 | IsSwapped |= ((i < (NumArgs - 1)) && | 
|  | 4701 | IsImplicitBoolFloatConversion(S, TheCall->getArg(i + 1), false)); | 
|  | 4702 | if (IsSwapped) { | 
|  | 4703 | // Warn on this floating-point to bool conversion. | 
|  | 4704 | DiagnoseImpCast(S, CurrA->IgnoreParenImpCasts(), | 
|  | 4705 | CurrA->getType(), CC, | 
|  | 4706 | diag::warn_impcast_floating_point_to_bool); | 
|  | 4707 | } | 
|  | 4708 | } | 
|  | 4709 | } | 
|  | 4710 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4711 | void CheckImplicitConversion(Sema &S, Expr *E, QualType T, | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4712 | SourceLocation CC, bool *ICContext = 0) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4713 | if (E->isTypeDependent() || E->isValueDependent()) return; | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4714 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4715 | const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr(); | 
|  | 4716 | const Type *Target = S.Context.getCanonicalType(T).getTypePtr(); | 
|  | 4717 | if (Source == Target) return; | 
|  | 4718 | if (Target->isDependentType()) return; | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4719 |  | 
| Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 4720 | // If the conversion context location is invalid don't complain. We also | 
|  | 4721 | // don't want to emit a warning if the issue occurs from the expansion of | 
|  | 4722 | // a system macro. The problem is that 'getSpellingLoc()' is slow, so we | 
|  | 4723 | // delay this check as long as possible. Once we detect we are in that | 
|  | 4724 | // scenario, we just return. | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4725 | if (CC.isInvalid()) | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4726 | return; | 
|  | 4727 |  | 
| Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4728 | // Diagnose implicit casts to bool. | 
|  | 4729 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) { | 
|  | 4730 | if (isa<StringLiteral>(E)) | 
|  | 4731 | // Warn on string literal to bool.  Checks for string literals in logical | 
|  | 4732 | // expressions, for instances, assert(0 && "error here"), is prevented | 
|  | 4733 | // by a check in AnalyzeImplicitConversions(). | 
|  | 4734 | return DiagnoseImpCast(S, E, T, CC, | 
|  | 4735 | diag::warn_impcast_string_literal_to_bool); | 
| Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4736 | if (Source->isFunctionType()) { | 
|  | 4737 | // Warn on function to bool. Checks free functions and static member | 
|  | 4738 | // functions. Weakly imported functions are excluded from the check, | 
|  | 4739 | // since it's common to test their value to check whether the linker | 
|  | 4740 | // found a definition for them. | 
|  | 4741 | ValueDecl *D = 0; | 
|  | 4742 | if (DeclRefExpr* R = dyn_cast<DeclRefExpr>(E)) { | 
|  | 4743 | D = R->getDecl(); | 
|  | 4744 | } else if (MemberExpr *M = dyn_cast<MemberExpr>(E)) { | 
|  | 4745 | D = M->getMemberDecl(); | 
|  | 4746 | } | 
|  | 4747 |  | 
|  | 4748 | if (D && !D->isWeak()) { | 
| Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4749 | if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) { | 
|  | 4750 | S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool) | 
|  | 4751 | << F << E->getSourceRange() << SourceRange(CC); | 
| David Blaikie | 2def773 | 2011-12-09 21:42:37 +0000 | [diff] [blame] | 4752 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence) | 
|  | 4753 | << FixItHint::CreateInsertion(E->getExprLoc(), "&"); | 
|  | 4754 | QualType ReturnType; | 
|  | 4755 | UnresolvedSet<4> NonTemplateOverloads; | 
|  | 4756 | S.isExprCallable(*E, ReturnType, NonTemplateOverloads); | 
|  | 4757 | if (!ReturnType.isNull() | 
|  | 4758 | && ReturnType->isSpecificBuiltinType(BuiltinType::Bool)) | 
|  | 4759 | S.Diag(E->getExprLoc(), diag::note_function_to_bool_call) | 
|  | 4760 | << FixItHint::CreateInsertion( | 
|  | 4761 | S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()"); | 
| Richard Trieu | 26b45d8 | 2011-12-06 04:48:01 +0000 | [diff] [blame] | 4762 | return; | 
|  | 4763 | } | 
| Lang Hames | e14ca9f | 2011-12-05 20:49:50 +0000 | [diff] [blame] | 4764 | } | 
|  | 4765 | } | 
| Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4766 | } | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4767 |  | 
|  | 4768 | // Strip vector types. | 
|  | 4769 | if (isa<VectorType>(Source)) { | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4770 | if (!isa<VectorType>(Target)) { | 
| Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4771 | if (S.SourceMgr.isInSystemMacro(CC)) | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4772 | return; | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4773 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_vector_scalar); | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4774 | } | 
| Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4775 |  | 
|  | 4776 | // If the vector cast is cast between two vectors of the same size, it is | 
|  | 4777 | // a bitcast, not a conversion. | 
|  | 4778 | if (S.Context.getTypeSize(Source) == S.Context.getTypeSize(Target)) | 
|  | 4779 | return; | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4780 |  | 
|  | 4781 | Source = cast<VectorType>(Source)->getElementType().getTypePtr(); | 
|  | 4782 | Target = cast<VectorType>(Target)->getElementType().getTypePtr(); | 
|  | 4783 | } | 
|  | 4784 |  | 
|  | 4785 | // Strip complex types. | 
|  | 4786 | if (isa<ComplexType>(Source)) { | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4787 | if (!isa<ComplexType>(Target)) { | 
| Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4788 | if (S.SourceMgr.isInSystemMacro(CC)) | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4789 | return; | 
|  | 4790 |  | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4791 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_complex_scalar); | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4792 | } | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4793 |  | 
|  | 4794 | Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); | 
|  | 4795 | Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); | 
|  | 4796 | } | 
|  | 4797 |  | 
|  | 4798 | const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); | 
|  | 4799 | const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); | 
|  | 4800 |  | 
|  | 4801 | // If the source is floating point... | 
|  | 4802 | if (SourceBT && SourceBT->isFloatingPoint()) { | 
|  | 4803 | // ...and the target is floating point... | 
|  | 4804 | if (TargetBT && TargetBT->isFloatingPoint()) { | 
|  | 4805 | // ...then warn if we're dropping FP rank. | 
|  | 4806 |  | 
|  | 4807 | // Builtin FP kinds are ordered by increasing FP rank. | 
|  | 4808 | if (SourceBT->getKind() > TargetBT->getKind()) { | 
|  | 4809 | // Don't warn about float constants that are precisely | 
|  | 4810 | // representable in the target type. | 
|  | 4811 | Expr::EvalResult result; | 
| Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4812 | if (E->EvaluateAsRValue(result, S.Context)) { | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4813 | // Value might be a float, a float vector, or a float complex. | 
|  | 4814 | if (IsSameFloatAfterCast(result.Val, | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4815 | S.Context.getFloatTypeSemantics(QualType(TargetBT, 0)), | 
|  | 4816 | S.Context.getFloatTypeSemantics(QualType(SourceBT, 0)))) | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4817 | return; | 
|  | 4818 | } | 
|  | 4819 |  | 
| Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4820 | if (S.SourceMgr.isInSystemMacro(CC)) | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4821 | return; | 
|  | 4822 |  | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4823 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision); | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4824 | } | 
|  | 4825 | return; | 
|  | 4826 | } | 
|  | 4827 |  | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4828 | // If the target is integral, always warn. | 
| David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4829 | if (TargetBT && TargetBT->isInteger()) { | 
| Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4830 | if (S.SourceMgr.isInSystemMacro(CC)) | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4831 | return; | 
|  | 4832 |  | 
| Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4833 | Expr *InnerE = E->IgnoreParenImpCasts(); | 
| Matt Beaumont-Gay | 634c8af | 2011-09-08 22:30:47 +0000 | [diff] [blame] | 4834 | // We also want to warn on, e.g., "int i = -1.234" | 
|  | 4835 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(InnerE)) | 
|  | 4836 | if (UOp->getOpcode() == UO_Minus || UOp->getOpcode() == UO_Plus) | 
|  | 4837 | InnerE = UOp->getSubExpr()->IgnoreParenImpCasts(); | 
|  | 4838 |  | 
| Chandler Carruth | f65076e | 2011-04-10 08:36:24 +0000 | [diff] [blame] | 4839 | if (FloatingLiteral *FL = dyn_cast<FloatingLiteral>(InnerE)) { | 
|  | 4840 | DiagnoseFloatingLiteralImpCast(S, FL, T, CC); | 
| Chandler Carruth | a5b9332 | 2011-02-17 11:05:49 +0000 | [diff] [blame] | 4841 | } else { | 
|  | 4842 | DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer); | 
|  | 4843 | } | 
|  | 4844 | } | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4845 |  | 
| Hans Wennborg | 88617a2 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 4846 | // If the target is bool, warn if expr is a function or method call. | 
|  | 4847 | if (Target->isSpecificBuiltinType(BuiltinType::Bool) && | 
|  | 4848 | isa<CallExpr>(E)) { | 
|  | 4849 | // Check last argument of function call to see if it is an | 
|  | 4850 | // implicit cast from a type matching the type the result | 
|  | 4851 | // is being cast to. | 
|  | 4852 | CallExpr *CEx = cast<CallExpr>(E); | 
|  | 4853 | unsigned NumArgs = CEx->getNumArgs(); | 
|  | 4854 | if (NumArgs > 0) { | 
|  | 4855 | Expr *LastA = CEx->getArg(NumArgs - 1); | 
|  | 4856 | Expr *InnerE = LastA->IgnoreParenImpCasts(); | 
|  | 4857 | const Type *InnerType = | 
|  | 4858 | S.Context.getCanonicalType(InnerE->getType()).getTypePtr(); | 
|  | 4859 | if (isa<ImplicitCastExpr>(LastA) && (InnerType == Target)) { | 
|  | 4860 | // Warn on this floating-point to bool conversion | 
|  | 4861 | DiagnoseImpCast(S, E, T, CC, | 
|  | 4862 | diag::warn_impcast_floating_point_to_bool); | 
|  | 4863 | } | 
|  | 4864 | } | 
|  | 4865 | } | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4866 | return; | 
|  | 4867 | } | 
|  | 4868 |  | 
| Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4869 | if ((E->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) | 
| David Blaikie | b26331b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 4870 | == Expr::NPCK_GNUNull) && !Target->isAnyPointerType() | 
| David Blaikie | e81b43b | 2012-11-08 00:41:20 +0000 | [diff] [blame] | 4871 | && !Target->isBlockPointerType() && !Target->isMemberPointerType() | 
|  | 4872 | && Target->isScalarType()) { | 
| David Blaikie | b136049 | 2012-03-16 20:30:12 +0000 | [diff] [blame] | 4873 | SourceLocation Loc = E->getSourceRange().getBegin(); | 
|  | 4874 | if (Loc.isMacroID()) | 
|  | 4875 | Loc = S.SourceMgr.getImmediateExpansionRange(Loc).first; | 
| David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4876 | if (!Loc.isMacroID() || CC.isMacroID()) | 
|  | 4877 | S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer) | 
|  | 4878 | << T << clang::SourceRange(CC) | 
|  | 4879 | << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T)); | 
| Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4880 | } | 
|  | 4881 |  | 
| David Blaikie | b26331b | 2012-06-19 21:19:06 +0000 | [diff] [blame] | 4882 | if (!Source->isIntegerType() || !Target->isIntegerType()) | 
|  | 4883 | return; | 
|  | 4884 |  | 
| David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 4885 | // TODO: remove this early return once the false positives for constant->bool | 
|  | 4886 | // in templates, macros, etc, are reduced or removed. | 
|  | 4887 | if (Target->isSpecificBuiltinType(BuiltinType::Bool)) | 
|  | 4888 | return; | 
|  | 4889 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4890 | IntRange SourceRange = GetExprRange(S.Context, E); | 
| John McCall | 1844a6e | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 4891 | IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target); | 
| John McCall | f2370c9 | 2010-01-06 05:24:50 +0000 | [diff] [blame] | 4892 |  | 
|  | 4893 | if (SourceRange.Width > TargetRange.Width) { | 
| John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4894 | // If the source is a constant, use a default-on diagnostic. | 
|  | 4895 | // TODO: this should happen for bitfield stores, too. | 
|  | 4896 | llvm::APSInt Value(32); | 
|  | 4897 | if (E->isIntegerConstantExpr(Value, S.Context)) { | 
| Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4898 | if (S.SourceMgr.isInSystemMacro(CC)) | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4899 | return; | 
|  | 4900 |  | 
| John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4901 | std::string PrettySourceValue = Value.toString(10); | 
|  | 4902 | std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange); | 
|  | 4903 |  | 
| Ted Kremenek | 5e745da | 2011-10-22 02:37:33 +0000 | [diff] [blame] | 4904 | S.DiagRuntimeBehavior(E->getExprLoc(), E, | 
|  | 4905 | S.PDiag(diag::warn_impcast_integer_precision_constant) | 
|  | 4906 | << PrettySourceValue << PrettyTargetValue | 
|  | 4907 | << E->getType() << T << E->getSourceRange() | 
|  | 4908 | << clang::SourceRange(CC)); | 
| John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4909 | return; | 
|  | 4910 | } | 
|  | 4911 |  | 
| Chris Lattner | b792b30 | 2011-06-14 04:51:15 +0000 | [diff] [blame] | 4912 | // People want to build with -Wshorten-64-to-32 and not -Wconversion. | 
| Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4913 | if (S.SourceMgr.isInSystemMacro(CC)) | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4914 | return; | 
| David Blaikie | 0c5d005 | 2012-11-19 23:12:51 +0000 | [diff] [blame] | 4915 |  | 
| David Blaikie | 3705084 | 2012-04-12 22:40:54 +0000 | [diff] [blame] | 4916 | if (TargetRange.Width == 32 && S.Context.getIntWidth(E->getType()) == 64) | 
| Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 4917 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_64_32, | 
|  | 4918 | /* pruneControlFlow */ true); | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4919 | return DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_integer_precision); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4920 | } | 
|  | 4921 |  | 
|  | 4922 | if ((TargetRange.NonNegative && !SourceRange.NonNegative) || | 
|  | 4923 | (!TargetRange.NonNegative && SourceRange.NonNegative && | 
|  | 4924 | SourceRange.Width == TargetRange.Width)) { | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4925 |  | 
| Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4926 | if (S.SourceMgr.isInSystemMacro(CC)) | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4927 | return; | 
|  | 4928 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4929 | unsigned DiagID = diag::warn_impcast_integer_sign; | 
|  | 4930 |  | 
|  | 4931 | // Traditionally, gcc has warned about this under -Wsign-compare. | 
|  | 4932 | // We also want to warn about it in -Wconversion. | 
|  | 4933 | // So if -Wconversion is off, use a completely identical diagnostic | 
|  | 4934 | // in the sign-compare group. | 
|  | 4935 | // The conditional-checking code will | 
|  | 4936 | if (ICContext) { | 
|  | 4937 | DiagID = diag::warn_impcast_integer_sign_conditional; | 
|  | 4938 | *ICContext = true; | 
|  | 4939 | } | 
|  | 4940 |  | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4941 | return DiagnoseImpCast(S, E, T, CC, DiagID); | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4942 | } | 
|  | 4943 |  | 
| Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4944 | // Diagnose conversions between different enumeration types. | 
| Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4945 | // In C, we pretend that the type of an EnumConstantDecl is its enumeration | 
|  | 4946 | // type, to give us better diagnostics. | 
|  | 4947 | QualType SourceType = E->getType(); | 
| David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4948 | if (!S.getLangOpts().CPlusPlus) { | 
| Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4949 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) | 
|  | 4950 | if (EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { | 
|  | 4951 | EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); | 
|  | 4952 | SourceType = S.Context.getTypeDeclType(Enum); | 
|  | 4953 | Source = S.Context.getCanonicalType(SourceType).getTypePtr(); | 
|  | 4954 | } | 
|  | 4955 | } | 
|  | 4956 |  | 
| Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4957 | if (const EnumType *SourceEnum = Source->getAs<EnumType>()) | 
|  | 4958 | if (const EnumType *TargetEnum = Target->getAs<EnumType>()) | 
|  | 4959 | if ((SourceEnum->getDecl()->getIdentifier() || | 
| Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4960 | SourceEnum->getDecl()->getTypedefNameForAnonDecl()) && | 
| Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4961 | (TargetEnum->getDecl()->getIdentifier() || | 
| Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4962 | TargetEnum->getDecl()->getTypedefNameForAnonDecl()) && | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4963 | SourceEnum != TargetEnum) { | 
| Matt Beaumont-Gay | d87a0cd | 2012-01-06 22:43:58 +0000 | [diff] [blame] | 4964 | if (S.SourceMgr.isInSystemMacro(CC)) | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4965 | return; | 
|  | 4966 |  | 
| Douglas Gregor | 5a5b38f | 2011-03-12 00:14:31 +0000 | [diff] [blame] | 4967 | return DiagnoseImpCast(S, E, SourceType, T, CC, | 
| Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4968 | diag::warn_impcast_different_enum_types); | 
| Ted Kremenek | ef9ff88 | 2011-03-10 20:03:42 +0000 | [diff] [blame] | 4969 | } | 
| Douglas Gregor | 284cc8d | 2011-02-22 02:45:07 +0000 | [diff] [blame] | 4970 |  | 
| John McCall | 51313c3 | 2010-01-04 23:31:57 +0000 | [diff] [blame] | 4971 | return; | 
|  | 4972 | } | 
|  | 4973 |  | 
| David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4974 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, | 
|  | 4975 | SourceLocation CC, QualType T); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4976 |  | 
|  | 4977 | void CheckConditionalOperand(Sema &S, Expr *E, QualType T, | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4978 | SourceLocation CC, bool &ICContext) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4979 | E = E->IgnoreParenImpCasts(); | 
|  | 4980 |  | 
|  | 4981 | if (isa<ConditionalOperator>(E)) | 
| David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4982 | return CheckConditionalOperator(S, cast<ConditionalOperator>(E), CC, T); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4983 |  | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4984 | AnalyzeImplicitConversions(S, E, CC); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4985 | if (E->getType() != T) | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4986 | return CheckImplicitConversion(S, E, T, CC, &ICContext); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4987 | return; | 
|  | 4988 | } | 
|  | 4989 |  | 
| David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 4990 | void CheckConditionalOperator(Sema &S, ConditionalOperator *E, | 
|  | 4991 | SourceLocation CC, QualType T) { | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4992 | AnalyzeImplicitConversions(S, E->getCond(), CC); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4993 |  | 
|  | 4994 | bool Suspicious = false; | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 4995 | CheckConditionalOperand(S, E->getTrueExpr(), T, CC, Suspicious); | 
|  | 4996 | CheckConditionalOperand(S, E->getFalseExpr(), T, CC, Suspicious); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 4997 |  | 
|  | 4998 | // If -Wconversion would have warned about either of the candidates | 
|  | 4999 | // for a signedness conversion to the context type... | 
|  | 5000 | if (!Suspicious) return; | 
|  | 5001 |  | 
|  | 5002 | // ...but it's currently ignored... | 
| Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5003 | if (S.Diags.getDiagnosticLevel(diag::warn_impcast_integer_sign_conditional, | 
|  | 5004 | CC)) | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5005 | return; | 
|  | 5006 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5007 | // ...then check whether it would have warned about either of the | 
|  | 5008 | // candidates for a signedness conversion to the condition type. | 
| Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 5009 | if (E->getType() == T) return; | 
|  | 5010 |  | 
|  | 5011 | Suspicious = false; | 
|  | 5012 | CheckImplicitConversion(S, E->getTrueExpr()->IgnoreParenImpCasts(), | 
|  | 5013 | E->getType(), CC, &Suspicious); | 
|  | 5014 | if (!Suspicious) | 
|  | 5015 | CheckImplicitConversion(S, E->getFalseExpr()->IgnoreParenImpCasts(), | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5016 | E->getType(), CC, &Suspicious); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5017 | } | 
|  | 5018 |  | 
|  | 5019 | /// AnalyzeImplicitConversions - Find and report any interesting | 
|  | 5020 | /// implicit conversions in the given expression.  There are a couple | 
|  | 5021 | /// of competing diagnostics here, -Wconversion and -Wsign-compare. | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5022 | void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5023 | QualType T = OrigE->getType(); | 
|  | 5024 | Expr *E = OrigE->IgnoreParenImpCasts(); | 
|  | 5025 |  | 
| Douglas Gregor | f8b6e15 | 2011-10-10 17:38:18 +0000 | [diff] [blame] | 5026 | if (E->isTypeDependent() || E->isValueDependent()) | 
|  | 5027 | return; | 
|  | 5028 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5029 | // For conditional operators, we analyze the arguments as if they | 
|  | 5030 | // were being fed directly into the output. | 
|  | 5031 | if (isa<ConditionalOperator>(E)) { | 
|  | 5032 | ConditionalOperator *CO = cast<ConditionalOperator>(E); | 
| David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 5033 | CheckConditionalOperator(S, CO, CC, T); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5034 | return; | 
|  | 5035 | } | 
|  | 5036 |  | 
| Hans Wennborg | 88617a2 | 2012-08-28 15:44:30 +0000 | [diff] [blame] | 5037 | // Check implicit argument conversions for function calls. | 
|  | 5038 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) | 
|  | 5039 | CheckImplicitArgumentConversions(S, Call, CC); | 
|  | 5040 |  | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5041 | // Go ahead and check any implicit conversions we might have skipped. | 
|  | 5042 | // The non-canonical typecheck is just an optimization; | 
|  | 5043 | // CheckImplicitConversion will filter out dead implicit conversions. | 
|  | 5044 | if (E->getType() != T) | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5045 | CheckImplicitConversion(S, E, T, CC); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5046 |  | 
|  | 5047 | // Now continue drilling into this expression. | 
|  | 5048 |  | 
|  | 5049 | // Skip past explicit casts. | 
|  | 5050 | if (isa<ExplicitCastExpr>(E)) { | 
|  | 5051 | E = cast<ExplicitCastExpr>(E)->getSubExpr()->IgnoreParenImpCasts(); | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5052 | return AnalyzeImplicitConversions(S, E, CC); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5053 | } | 
|  | 5054 |  | 
| John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 5055 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { | 
|  | 5056 | // Do a somewhat different check with comparison operators. | 
|  | 5057 | if (BO->isComparisonOp()) | 
|  | 5058 | return AnalyzeComparison(S, BO); | 
|  | 5059 |  | 
| Eli Friedman | 0fa0638 | 2012-01-26 23:34:06 +0000 | [diff] [blame] | 5060 | // And with simple assignments. | 
|  | 5061 | if (BO->getOpcode() == BO_Assign) | 
| John McCall | beb22aa | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 5062 | return AnalyzeAssignment(S, BO); | 
|  | 5063 | } | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5064 |  | 
|  | 5065 | // These break the otherwise-useful invariant below.  Fortunately, | 
|  | 5066 | // we don't really need to recurse into them, because any internal | 
|  | 5067 | // expressions should have been analyzed already when they were | 
|  | 5068 | // built into statements. | 
|  | 5069 | if (isa<StmtExpr>(E)) return; | 
|  | 5070 |  | 
|  | 5071 | // Don't descend into unevaluated contexts. | 
| Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5072 | if (isa<UnaryExprOrTypeTraitExpr>(E)) return; | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5073 |  | 
|  | 5074 | // Now just recurse over the expression's children. | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5075 | CC = E->getExprLoc(); | 
| Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 5076 | BinaryOperator *BO = dyn_cast<BinaryOperator>(E); | 
|  | 5077 | bool IsLogicalOperator = BO && BO->isLogicalOp(); | 
|  | 5078 | for (Stmt::child_range I = E->children(); I; ++I) { | 
| Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 5079 | Expr *ChildExpr = dyn_cast_or_null<Expr>(*I); | 
| Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 5080 | if (!ChildExpr) | 
|  | 5081 | continue; | 
|  | 5082 |  | 
| Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 5083 | if (IsLogicalOperator && | 
|  | 5084 | isa<StringLiteral>(ChildExpr->IgnoreParenImpCasts())) | 
|  | 5085 | // Ignore checking string literals that are in logical operators. | 
|  | 5086 | continue; | 
|  | 5087 | AnalyzeImplicitConversions(S, ChildExpr, CC); | 
|  | 5088 | } | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5089 | } | 
|  | 5090 |  | 
|  | 5091 | } // end anonymous namespace | 
|  | 5092 |  | 
|  | 5093 | /// Diagnoses "dangerous" implicit conversions within the given | 
|  | 5094 | /// expression (which is a full expression).  Implements -Wconversion | 
|  | 5095 | /// and -Wsign-compare. | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5096 | /// | 
|  | 5097 | /// \param CC the "context" location of the implicit conversion, i.e. | 
|  | 5098 | ///   the most location of the syntactic entity requiring the implicit | 
|  | 5099 | ///   conversion | 
|  | 5100 | void Sema::CheckImplicitConversions(Expr *E, SourceLocation CC) { | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5101 | // Don't diagnose in unevaluated contexts. | 
| David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 5102 | if (isUnevaluatedContext()) | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5103 | return; | 
|  | 5104 |  | 
|  | 5105 | // Don't diagnose for value- or type-dependent expressions. | 
|  | 5106 | if (E->isTypeDependent() || E->isValueDependent()) | 
|  | 5107 | return; | 
|  | 5108 |  | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5109 | // Check for array bounds violations in cases where the check isn't triggered | 
|  | 5110 | // elsewhere for other Expr types (like BinaryOperators), e.g. when an | 
|  | 5111 | // ArraySubscriptExpr is on the RHS of a variable initialization. | 
|  | 5112 | CheckArrayAccess(E); | 
|  | 5113 |  | 
| John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5114 | // This is not the right CC for (e.g.) a variable initialization. | 
|  | 5115 | AnalyzeImplicitConversions(*this, E, CC); | 
| John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 5116 | } | 
|  | 5117 |  | 
| John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5118 | void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, | 
|  | 5119 | FieldDecl *BitField, | 
|  | 5120 | Expr *Init) { | 
|  | 5121 | (void) AnalyzeBitFieldAssignment(*this, BitField, Init, InitLoc); | 
|  | 5122 | } | 
|  | 5123 |  | 
| Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5124 | /// CheckParmsForFunctionDef - Check that the parameters of the given | 
|  | 5125 | /// function are appropriate for the definition of a function. This | 
|  | 5126 | /// takes care of any checks that cannot be performed on the | 
|  | 5127 | /// declaration itself, e.g., that the types of each of the function | 
|  | 5128 | /// parameters are complete. | 
| Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 5129 | bool Sema::CheckParmsForFunctionDef(ParmVarDecl **P, ParmVarDecl **PEnd, | 
|  | 5130 | bool CheckParameterNames) { | 
| Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5131 | bool HasInvalidParm = false; | 
| Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 5132 | for (; P != PEnd; ++P) { | 
|  | 5133 | ParmVarDecl *Param = *P; | 
|  | 5134 |  | 
| Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5135 | // C99 6.7.5.3p4: the parameters in a parameter type list in a | 
|  | 5136 | // function declarator that is part of a function definition of | 
|  | 5137 | // that function shall not have incomplete type. | 
|  | 5138 | // | 
|  | 5139 | // This is also C++ [dcl.fct]p6. | 
|  | 5140 | if (!Param->isInvalidDecl() && | 
|  | 5141 | RequireCompleteType(Param->getLocation(), Param->getType(), | 
| Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5142 | diag::err_typecheck_decl_incomplete_type)) { | 
| Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5143 | Param->setInvalidDecl(); | 
|  | 5144 | HasInvalidParm = true; | 
|  | 5145 | } | 
|  | 5146 |  | 
|  | 5147 | // C99 6.9.1p5: If the declarator includes a parameter type list, the | 
|  | 5148 | // declaration of each parameter shall include an identifier. | 
| Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 5149 | if (CheckParameterNames && | 
|  | 5150 | Param->getIdentifier() == 0 && | 
| Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5151 | !Param->isImplicit() && | 
| David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5152 | !getLangOpts().CPlusPlus) | 
| Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5153 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); | 
| Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 5154 |  | 
|  | 5155 | // C99 6.7.5.3p12: | 
|  | 5156 | //   If the function declarator is not part of a definition of that | 
|  | 5157 | //   function, parameters may have incomplete type and may use the [*] | 
|  | 5158 | //   notation in their sequences of declarator specifiers to specify | 
|  | 5159 | //   variable length array types. | 
|  | 5160 | QualType PType = Param->getOriginalType(); | 
|  | 5161 | if (const ArrayType *AT = Context.getAsArrayType(PType)) { | 
|  | 5162 | if (AT->getSizeModifier() == ArrayType::Star) { | 
| Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 5163 | // FIXME: This diagnosic should point the '[*]' if source-location | 
| Sam Weinig | d17e340 | 2010-02-01 05:02:49 +0000 | [diff] [blame] | 5164 | // information is added for it. | 
|  | 5165 | Diag(Param->getLocation(), diag::err_array_star_in_function_definition); | 
|  | 5166 | } | 
|  | 5167 | } | 
| Mike Stump | f8c4921 | 2010-01-21 03:59:47 +0000 | [diff] [blame] | 5168 | } | 
|  | 5169 |  | 
|  | 5170 | return HasInvalidParm; | 
|  | 5171 | } | 
| John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5172 |  | 
|  | 5173 | /// CheckCastAlign - Implements -Wcast-align, which warns when a | 
|  | 5174 | /// pointer cast increases the alignment requirements. | 
|  | 5175 | void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) { | 
|  | 5176 | // This is actually a lot of work to potentially be doing on every | 
|  | 5177 | // cast; don't do it if we're ignoring -Wcast_align (as is the default). | 
| Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 5178 | if (getDiagnostics().getDiagnosticLevel(diag::warn_cast_align, | 
|  | 5179 | TRange.getBegin()) | 
| David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5180 | == DiagnosticsEngine::Ignored) | 
| John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5181 | return; | 
|  | 5182 |  | 
|  | 5183 | // Ignore dependent types. | 
|  | 5184 | if (T->isDependentType() || Op->getType()->isDependentType()) | 
|  | 5185 | return; | 
|  | 5186 |  | 
|  | 5187 | // Require that the destination be a pointer type. | 
|  | 5188 | const PointerType *DestPtr = T->getAs<PointerType>(); | 
|  | 5189 | if (!DestPtr) return; | 
|  | 5190 |  | 
|  | 5191 | // If the destination has alignment 1, we're done. | 
|  | 5192 | QualType DestPointee = DestPtr->getPointeeType(); | 
|  | 5193 | if (DestPointee->isIncompleteType()) return; | 
|  | 5194 | CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee); | 
|  | 5195 | if (DestAlign.isOne()) return; | 
|  | 5196 |  | 
|  | 5197 | // Require that the source be a pointer type. | 
|  | 5198 | const PointerType *SrcPtr = Op->getType()->getAs<PointerType>(); | 
|  | 5199 | if (!SrcPtr) return; | 
|  | 5200 | QualType SrcPointee = SrcPtr->getPointeeType(); | 
|  | 5201 |  | 
|  | 5202 | // Whitelist casts from cv void*.  We already implicitly | 
|  | 5203 | // whitelisted casts to cv void*, since they have alignment 1. | 
|  | 5204 | // Also whitelist casts involving incomplete types, which implicitly | 
|  | 5205 | // includes 'void'. | 
|  | 5206 | if (SrcPointee->isIncompleteType()) return; | 
|  | 5207 |  | 
|  | 5208 | CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee); | 
|  | 5209 | if (SrcAlign >= DestAlign) return; | 
|  | 5210 |  | 
|  | 5211 | Diag(TRange.getBegin(), diag::warn_cast_align) | 
|  | 5212 | << Op->getType() << T | 
|  | 5213 | << static_cast<unsigned>(SrcAlign.getQuantity()) | 
|  | 5214 | << static_cast<unsigned>(DestAlign.getQuantity()) | 
|  | 5215 | << TRange << Op->getSourceRange(); | 
|  | 5216 | } | 
|  | 5217 |  | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5218 | static const Type* getElementType(const Expr *BaseExpr) { | 
|  | 5219 | const Type* EltType = BaseExpr->getType().getTypePtr(); | 
|  | 5220 | if (EltType->isAnyPointerType()) | 
|  | 5221 | return EltType->getPointeeType().getTypePtr(); | 
|  | 5222 | else if (EltType->isArrayType()) | 
|  | 5223 | return EltType->getBaseElementTypeUnsafe(); | 
|  | 5224 | return EltType; | 
|  | 5225 | } | 
|  | 5226 |  | 
| Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5227 | /// \brief Check whether this array fits the idiom of a size-one tail padded | 
|  | 5228 | /// array member of a struct. | 
|  | 5229 | /// | 
|  | 5230 | /// We avoid emitting out-of-bounds access warnings for such arrays as they are | 
|  | 5231 | /// commonly used to emulate flexible arrays in C89 code. | 
|  | 5232 | static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, | 
|  | 5233 | const NamedDecl *ND) { | 
|  | 5234 | if (Size != 1 || !ND) return false; | 
|  | 5235 |  | 
|  | 5236 | const FieldDecl *FD = dyn_cast<FieldDecl>(ND); | 
|  | 5237 | if (!FD) return false; | 
|  | 5238 |  | 
|  | 5239 | // Don't consider sizes resulting from macro expansions or template argument | 
|  | 5240 | // substitution to form C89 tail-padded arrays. | 
| Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 5241 |  | 
|  | 5242 | TypeSourceInfo *TInfo = FD->getTypeSourceInfo(); | 
| Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 5243 | while (TInfo) { | 
|  | 5244 | TypeLoc TL = TInfo->getTypeLoc(); | 
|  | 5245 | // Look through typedefs. | 
|  | 5246 | const TypedefTypeLoc *TTL = dyn_cast<TypedefTypeLoc>(&TL); | 
|  | 5247 | if (TTL) { | 
|  | 5248 | const TypedefNameDecl *TDL = TTL->getTypedefNameDecl(); | 
|  | 5249 | TInfo = TDL->getTypeSourceInfo(); | 
|  | 5250 | continue; | 
|  | 5251 | } | 
|  | 5252 | ConstantArrayTypeLoc CTL = cast<ConstantArrayTypeLoc>(TL); | 
|  | 5253 | const Expr *SizeExpr = dyn_cast<IntegerLiteral>(CTL.getSizeExpr()); | 
| Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 5254 | if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) | 
|  | 5255 | return false; | 
| Ted Kremenek | 00e1f6f | 2012-05-09 05:35:08 +0000 | [diff] [blame] | 5256 | break; | 
| Sean Callanan | d2cf348 | 2012-05-04 18:22:53 +0000 | [diff] [blame] | 5257 | } | 
| Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5258 |  | 
|  | 5259 | const RecordDecl *RD = dyn_cast<RecordDecl>(FD->getDeclContext()); | 
| Matt Beaumont-Gay | 381711c | 2011-11-29 22:43:53 +0000 | [diff] [blame] | 5260 | if (!RD) return false; | 
|  | 5261 | if (RD->isUnion()) return false; | 
|  | 5262 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { | 
|  | 5263 | if (!CRD->isStandardLayout()) return false; | 
|  | 5264 | } | 
| Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5265 |  | 
| Benjamin Kramer | 22d4fed | 2011-08-06 03:04:42 +0000 | [diff] [blame] | 5266 | // See if this is the last field decl in the record. | 
|  | 5267 | const Decl *D = FD; | 
|  | 5268 | while ((D = D->getNextDeclInContext())) | 
|  | 5269 | if (isa<FieldDecl>(D)) | 
|  | 5270 | return false; | 
|  | 5271 | return true; | 
| Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5272 | } | 
|  | 5273 |  | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5274 | void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr, | 
| Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5275 | const ArraySubscriptExpr *ASE, | 
| Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 5276 | bool AllowOnePastEnd, bool IndexNegated) { | 
| Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 5277 | IndexExpr = IndexExpr->IgnoreParenImpCasts(); | 
| Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5278 | if (IndexExpr->isValueDependent()) | 
|  | 5279 | return; | 
|  | 5280 |  | 
| Matt Beaumont-Gay | 8ef8f43 | 2011-12-12 22:35:02 +0000 | [diff] [blame] | 5281 | const Type *EffectiveType = getElementType(BaseExpr); | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5282 | BaseExpr = BaseExpr->IgnoreParenCasts(); | 
| Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5283 | const ConstantArrayType *ArrayTy = | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5284 | Context.getAsConstantArrayType(BaseExpr->getType()); | 
| Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5285 | if (!ArrayTy) | 
| Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5286 | return; | 
| Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5287 |  | 
| Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5288 | llvm::APSInt index; | 
| Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5289 | if (!IndexExpr->EvaluateAsInt(index, Context)) | 
| Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5290 | return; | 
| Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 5291 | if (IndexNegated) | 
|  | 5292 | index = -index; | 
| Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 5293 |  | 
| Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5294 | const NamedDecl *ND = NULL; | 
| Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5295 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) | 
|  | 5296 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); | 
| Chandler Carruth | c268434 | 2011-08-05 09:10:50 +0000 | [diff] [blame] | 5297 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) | 
| Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5298 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); | 
| Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5299 |  | 
| Ted Kremenek | 9e060ca | 2011-02-23 23:06:04 +0000 | [diff] [blame] | 5300 | if (index.isUnsigned() || !index.isNegative()) { | 
| Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 5301 | llvm::APInt size = ArrayTy->getSize(); | 
| Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5302 | if (!size.isStrictlyPositive()) | 
|  | 5303 | return; | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5304 |  | 
|  | 5305 | const Type* BaseType = getElementType(BaseExpr); | 
| Nico Weber | de5998f | 2011-09-17 22:59:41 +0000 | [diff] [blame] | 5306 | if (BaseType != EffectiveType) { | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5307 | // Make sure we're comparing apples to apples when comparing index to size | 
|  | 5308 | uint64_t ptrarith_typesize = Context.getTypeSize(EffectiveType); | 
|  | 5309 | uint64_t array_typesize = Context.getTypeSize(BaseType); | 
| Kaelyn Uhrain | d10f4bc | 2011-08-10 19:47:25 +0000 | [diff] [blame] | 5310 | // Handle ptrarith_typesize being zero, such as when casting to void* | 
| Kaelyn Uhrain | 18f1697 | 2011-08-10 18:49:28 +0000 | [diff] [blame] | 5311 | if (!ptrarith_typesize) ptrarith_typesize = 1; | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5312 | if (ptrarith_typesize != array_typesize) { | 
|  | 5313 | // There's a cast to a different size type involved | 
|  | 5314 | uint64_t ratio = array_typesize / ptrarith_typesize; | 
|  | 5315 | // TODO: Be smarter about handling cases where array_typesize is not a | 
|  | 5316 | // multiple of ptrarith_typesize | 
|  | 5317 | if (ptrarith_typesize * ratio == array_typesize) | 
|  | 5318 | size *= llvm::APInt(size.getBitWidth(), ratio); | 
|  | 5319 | } | 
|  | 5320 | } | 
|  | 5321 |  | 
| Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5322 | if (size.getBitWidth() > index.getBitWidth()) | 
| Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 5323 | index = index.zext(size.getBitWidth()); | 
| Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 5324 | else if (size.getBitWidth() < index.getBitWidth()) | 
| Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 5325 | size = size.zext(index.getBitWidth()); | 
| Ted Kremenek | 25b3b84 | 2011-02-18 02:27:00 +0000 | [diff] [blame] | 5326 |  | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5327 | // For array subscripting the index must be less than size, but for pointer | 
|  | 5328 | // arithmetic also allow the index (offset) to be equal to size since | 
|  | 5329 | // computing the next address after the end of the array is legal and | 
|  | 5330 | // commonly done e.g. in C++ iterators and range-based for loops. | 
| Eli Friedman | 92b670e | 2012-02-27 21:21:40 +0000 | [diff] [blame] | 5331 | if (AllowOnePastEnd ? index.ule(size) : index.ult(size)) | 
| Chandler Carruth | ba44712 | 2011-08-05 08:07:29 +0000 | [diff] [blame] | 5332 | return; | 
|  | 5333 |  | 
|  | 5334 | // Also don't warn for arrays of size 1 which are members of some | 
|  | 5335 | // structure. These are often used to approximate flexible arrays in C89 | 
|  | 5336 | // code. | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5337 | if (IsTailPaddedMemberArray(*this, size, ND)) | 
| Ted Kremenek | 8fd0a5d | 2011-02-16 04:01:44 +0000 | [diff] [blame] | 5338 | return; | 
| Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5339 |  | 
| Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5340 | // Suppress the warning if the subscript expression (as identified by the | 
|  | 5341 | // ']' location) and the index expression are both from macro expansions | 
|  | 5342 | // within a system header. | 
|  | 5343 | if (ASE) { | 
|  | 5344 | SourceLocation RBracketLoc = SourceMgr.getSpellingLoc( | 
|  | 5345 | ASE->getRBracketLoc()); | 
|  | 5346 | if (SourceMgr.isInSystemHeader(RBracketLoc)) { | 
|  | 5347 | SourceLocation IndexLoc = SourceMgr.getSpellingLoc( | 
|  | 5348 | IndexExpr->getLocStart()); | 
|  | 5349 | if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc)) | 
|  | 5350 | return; | 
|  | 5351 | } | 
|  | 5352 | } | 
|  | 5353 |  | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5354 | unsigned DiagID = diag::warn_ptr_arith_exceeds_bounds; | 
| Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5355 | if (ASE) | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5356 | DiagID = diag::warn_array_index_exceeds_bounds; | 
|  | 5357 |  | 
|  | 5358 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, | 
|  | 5359 | PDiag(DiagID) << index.toString(10, true) | 
|  | 5360 | << size.toString(10, true) | 
|  | 5361 | << (unsigned)size.getLimitedValue(~0U) | 
|  | 5362 | << IndexExpr->getSourceRange()); | 
| Chandler Carruth | 3406458 | 2011-02-17 20:55:08 +0000 | [diff] [blame] | 5363 | } else { | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5364 | unsigned DiagID = diag::warn_array_index_precedes_bounds; | 
| Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5365 | if (!ASE) { | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5366 | DiagID = diag::warn_ptr_arith_precedes_bounds; | 
|  | 5367 | if (index.isNegative()) index = -index; | 
|  | 5368 | } | 
|  | 5369 |  | 
|  | 5370 | DiagRuntimeBehavior(BaseExpr->getLocStart(), BaseExpr, | 
|  | 5371 | PDiag(DiagID) << index.toString(10, true) | 
|  | 5372 | << IndexExpr->getSourceRange()); | 
| Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5373 | } | 
| Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5374 |  | 
| Matt Beaumont-Gay | cfbc5b5 | 2011-11-29 19:27:11 +0000 | [diff] [blame] | 5375 | if (!ND) { | 
|  | 5376 | // Try harder to find a NamedDecl to point at in the note. | 
|  | 5377 | while (const ArraySubscriptExpr *ASE = | 
|  | 5378 | dyn_cast<ArraySubscriptExpr>(BaseExpr)) | 
|  | 5379 | BaseExpr = ASE->getBase()->IgnoreParenCasts(); | 
|  | 5380 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr)) | 
|  | 5381 | ND = dyn_cast<NamedDecl>(DRE->getDecl()); | 
|  | 5382 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr)) | 
|  | 5383 | ND = dyn_cast<NamedDecl>(ME->getMemberDecl()); | 
|  | 5384 | } | 
|  | 5385 |  | 
| Chandler Carruth | 35001ca | 2011-02-17 21:10:52 +0000 | [diff] [blame] | 5386 | if (ND) | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5387 | DiagRuntimeBehavior(ND->getLocStart(), BaseExpr, | 
|  | 5388 | PDiag(diag::note_array_index_out_of_bounds) | 
|  | 5389 | << ND->getDeclName()); | 
| Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 5390 | } | 
|  | 5391 |  | 
| Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5392 | void Sema::CheckArrayAccess(const Expr *expr) { | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5393 | int AllowOnePastEnd = 0; | 
|  | 5394 | while (expr) { | 
|  | 5395 | expr = expr->IgnoreParenImpCasts(); | 
| Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5396 | switch (expr->getStmtClass()) { | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5397 | case Stmt::ArraySubscriptExprClass: { | 
|  | 5398 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr); | 
| Matt Beaumont-Gay | 80fb7dd | 2011-12-14 16:02:15 +0000 | [diff] [blame] | 5399 | CheckArrayAccess(ASE->getBase(), ASE->getIdx(), ASE, | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5400 | AllowOnePastEnd > 0); | 
| Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5401 | return; | 
| Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5402 | } | 
|  | 5403 | case Stmt::UnaryOperatorClass: { | 
|  | 5404 | // Only unwrap the * and & unary operators | 
|  | 5405 | const UnaryOperator *UO = cast<UnaryOperator>(expr); | 
|  | 5406 | expr = UO->getSubExpr(); | 
|  | 5407 | switch (UO->getOpcode()) { | 
|  | 5408 | case UO_AddrOf: | 
|  | 5409 | AllowOnePastEnd++; | 
|  | 5410 | break; | 
|  | 5411 | case UO_Deref: | 
|  | 5412 | AllowOnePastEnd--; | 
|  | 5413 | break; | 
|  | 5414 | default: | 
|  | 5415 | return; | 
|  | 5416 | } | 
|  | 5417 | break; | 
|  | 5418 | } | 
| Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5419 | case Stmt::ConditionalOperatorClass: { | 
|  | 5420 | const ConditionalOperator *cond = cast<ConditionalOperator>(expr); | 
|  | 5421 | if (const Expr *lhs = cond->getLHS()) | 
|  | 5422 | CheckArrayAccess(lhs); | 
|  | 5423 | if (const Expr *rhs = cond->getRHS()) | 
|  | 5424 | CheckArrayAccess(rhs); | 
|  | 5425 | return; | 
|  | 5426 | } | 
|  | 5427 | default: | 
|  | 5428 | return; | 
|  | 5429 | } | 
| Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 5430 | } | 
| Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 5431 | } | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5432 |  | 
|  | 5433 | //===--- CHECK: Objective-C retain cycles ----------------------------------// | 
|  | 5434 |  | 
|  | 5435 | namespace { | 
|  | 5436 | struct RetainCycleOwner { | 
|  | 5437 | RetainCycleOwner() : Variable(0), Indirect(false) {} | 
|  | 5438 | VarDecl *Variable; | 
|  | 5439 | SourceRange Range; | 
|  | 5440 | SourceLocation Loc; | 
|  | 5441 | bool Indirect; | 
|  | 5442 |  | 
|  | 5443 | void setLocsFrom(Expr *e) { | 
|  | 5444 | Loc = e->getExprLoc(); | 
|  | 5445 | Range = e->getSourceRange(); | 
|  | 5446 | } | 
|  | 5447 | }; | 
|  | 5448 | } | 
|  | 5449 |  | 
|  | 5450 | /// Consider whether capturing the given variable can possibly lead to | 
|  | 5451 | /// a retain cycle. | 
|  | 5452 | static bool considerVariable(VarDecl *var, Expr *ref, RetainCycleOwner &owner) { | 
| Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 5453 | // In ARC, it's captured strongly iff the variable has __strong | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5454 | // lifetime.  In MRR, it's captured strongly if the variable is | 
|  | 5455 | // __block and has an appropriate type. | 
|  | 5456 | if (var->getType().getObjCLifetime() != Qualifiers::OCL_Strong) | 
|  | 5457 | return false; | 
|  | 5458 |  | 
|  | 5459 | owner.Variable = var; | 
| Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 5460 | if (ref) | 
|  | 5461 | owner.setLocsFrom(ref); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5462 | return true; | 
|  | 5463 | } | 
|  | 5464 |  | 
| Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5465 | static bool findRetainCycleOwner(Sema &S, Expr *e, RetainCycleOwner &owner) { | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5466 | while (true) { | 
|  | 5467 | e = e->IgnoreParens(); | 
|  | 5468 | if (CastExpr *cast = dyn_cast<CastExpr>(e)) { | 
|  | 5469 | switch (cast->getCastKind()) { | 
|  | 5470 | case CK_BitCast: | 
|  | 5471 | case CK_LValueBitCast: | 
|  | 5472 | case CK_LValueToRValue: | 
| John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5473 | case CK_ARCReclaimReturnedObject: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5474 | e = cast->getSubExpr(); | 
|  | 5475 | continue; | 
|  | 5476 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5477 | default: | 
|  | 5478 | return false; | 
|  | 5479 | } | 
|  | 5480 | } | 
|  | 5481 |  | 
|  | 5482 | if (ObjCIvarRefExpr *ref = dyn_cast<ObjCIvarRefExpr>(e)) { | 
|  | 5483 | ObjCIvarDecl *ivar = ref->getDecl(); | 
|  | 5484 | if (ivar->getType().getObjCLifetime() != Qualifiers::OCL_Strong) | 
|  | 5485 | return false; | 
|  | 5486 |  | 
|  | 5487 | // Try to find a retain cycle in the base. | 
| Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5488 | if (!findRetainCycleOwner(S, ref->getBase(), owner)) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5489 | return false; | 
|  | 5490 |  | 
|  | 5491 | if (ref->isFreeIvar()) owner.setLocsFrom(ref); | 
|  | 5492 | owner.Indirect = true; | 
|  | 5493 | return true; | 
|  | 5494 | } | 
|  | 5495 |  | 
|  | 5496 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { | 
|  | 5497 | VarDecl *var = dyn_cast<VarDecl>(ref->getDecl()); | 
|  | 5498 | if (!var) return false; | 
|  | 5499 | return considerVariable(var, ref, owner); | 
|  | 5500 | } | 
|  | 5501 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5502 | if (MemberExpr *member = dyn_cast<MemberExpr>(e)) { | 
|  | 5503 | if (member->isArrow()) return false; | 
|  | 5504 |  | 
|  | 5505 | // Don't count this as an indirect ownership. | 
|  | 5506 | e = member->getBase(); | 
|  | 5507 | continue; | 
|  | 5508 | } | 
|  | 5509 |  | 
| John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5510 | if (PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { | 
|  | 5511 | // Only pay attention to pseudo-objects on property references. | 
|  | 5512 | ObjCPropertyRefExpr *pre | 
|  | 5513 | = dyn_cast<ObjCPropertyRefExpr>(pseudo->getSyntacticForm() | 
|  | 5514 | ->IgnoreParens()); | 
|  | 5515 | if (!pre) return false; | 
|  | 5516 | if (pre->isImplicitProperty()) return false; | 
|  | 5517 | ObjCPropertyDecl *property = pre->getExplicitProperty(); | 
|  | 5518 | if (!property->isRetaining() && | 
|  | 5519 | !(property->getPropertyIvarDecl() && | 
|  | 5520 | property->getPropertyIvarDecl()->getType() | 
|  | 5521 | .getObjCLifetime() == Qualifiers::OCL_Strong)) | 
|  | 5522 | return false; | 
|  | 5523 |  | 
|  | 5524 | owner.Indirect = true; | 
| Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5525 | if (pre->isSuperReceiver()) { | 
|  | 5526 | owner.Variable = S.getCurMethodDecl()->getSelfDecl(); | 
|  | 5527 | if (!owner.Variable) | 
|  | 5528 | return false; | 
|  | 5529 | owner.Loc = pre->getLocation(); | 
|  | 5530 | owner.Range = pre->getSourceRange(); | 
|  | 5531 | return true; | 
|  | 5532 | } | 
| John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 5533 | e = const_cast<Expr*>(cast<OpaqueValueExpr>(pre->getBase()) | 
|  | 5534 | ->getSourceExpr()); | 
|  | 5535 | continue; | 
|  | 5536 | } | 
|  | 5537 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5538 | // Array ivars? | 
|  | 5539 |  | 
|  | 5540 | return false; | 
|  | 5541 | } | 
|  | 5542 | } | 
|  | 5543 |  | 
|  | 5544 | namespace { | 
|  | 5545 | struct FindCaptureVisitor : EvaluatedExprVisitor<FindCaptureVisitor> { | 
|  | 5546 | FindCaptureVisitor(ASTContext &Context, VarDecl *variable) | 
|  | 5547 | : EvaluatedExprVisitor<FindCaptureVisitor>(Context), | 
|  | 5548 | Variable(variable), Capturer(0) {} | 
|  | 5549 |  | 
|  | 5550 | VarDecl *Variable; | 
|  | 5551 | Expr *Capturer; | 
|  | 5552 |  | 
|  | 5553 | void VisitDeclRefExpr(DeclRefExpr *ref) { | 
|  | 5554 | if (ref->getDecl() == Variable && !Capturer) | 
|  | 5555 | Capturer = ref; | 
|  | 5556 | } | 
|  | 5557 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5558 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *ref) { | 
|  | 5559 | if (Capturer) return; | 
|  | 5560 | Visit(ref->getBase()); | 
|  | 5561 | if (Capturer && ref->isFreeIvar()) | 
|  | 5562 | Capturer = ref; | 
|  | 5563 | } | 
|  | 5564 |  | 
|  | 5565 | void VisitBlockExpr(BlockExpr *block) { | 
|  | 5566 | // Look inside nested blocks | 
|  | 5567 | if (block->getBlockDecl()->capturesVariable(Variable)) | 
|  | 5568 | Visit(block->getBlockDecl()->getBody()); | 
|  | 5569 | } | 
| Fariborz Jahanian | 7e2e4c3 | 2012-08-31 20:04:47 +0000 | [diff] [blame] | 5570 |  | 
|  | 5571 | void VisitOpaqueValueExpr(OpaqueValueExpr *OVE) { | 
|  | 5572 | if (Capturer) return; | 
|  | 5573 | if (OVE->getSourceExpr()) | 
|  | 5574 | Visit(OVE->getSourceExpr()); | 
|  | 5575 | } | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5576 | }; | 
|  | 5577 | } | 
|  | 5578 |  | 
|  | 5579 | /// Check whether the given argument is a block which captures a | 
|  | 5580 | /// variable. | 
|  | 5581 | static Expr *findCapturingExpr(Sema &S, Expr *e, RetainCycleOwner &owner) { | 
|  | 5582 | assert(owner.Variable && owner.Loc.isValid()); | 
|  | 5583 |  | 
|  | 5584 | e = e->IgnoreParenCasts(); | 
| Jordan Rose | 1fac58a | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 5585 |  | 
|  | 5586 | // Look through [^{...} copy] and Block_copy(^{...}). | 
|  | 5587 | if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(e)) { | 
|  | 5588 | Selector Cmd = ME->getSelector(); | 
|  | 5589 | if (Cmd.isUnarySelector() && Cmd.getNameForSlot(0) == "copy") { | 
|  | 5590 | e = ME->getInstanceReceiver(); | 
|  | 5591 | if (!e) | 
|  | 5592 | return 0; | 
|  | 5593 | e = e->IgnoreParenCasts(); | 
|  | 5594 | } | 
|  | 5595 | } else if (CallExpr *CE = dyn_cast<CallExpr>(e)) { | 
|  | 5596 | if (CE->getNumArgs() == 1) { | 
|  | 5597 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(CE->getCalleeDecl()); | 
| Ted Kremenek | d13eff6 | 2012-10-02 04:36:54 +0000 | [diff] [blame] | 5598 | if (Fn) { | 
|  | 5599 | const IdentifierInfo *FnI = Fn->getIdentifier(); | 
|  | 5600 | if (FnI && FnI->isStr("_Block_copy")) { | 
|  | 5601 | e = CE->getArg(0)->IgnoreParenCasts(); | 
|  | 5602 | } | 
|  | 5603 | } | 
| Jordan Rose | 1fac58a | 2012-09-17 17:54:30 +0000 | [diff] [blame] | 5604 | } | 
|  | 5605 | } | 
|  | 5606 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5607 | BlockExpr *block = dyn_cast<BlockExpr>(e); | 
|  | 5608 | if (!block || !block->getBlockDecl()->capturesVariable(owner.Variable)) | 
|  | 5609 | return 0; | 
|  | 5610 |  | 
|  | 5611 | FindCaptureVisitor visitor(S.Context, owner.Variable); | 
|  | 5612 | visitor.Visit(block->getBlockDecl()->getBody()); | 
|  | 5613 | return visitor.Capturer; | 
|  | 5614 | } | 
|  | 5615 |  | 
|  | 5616 | static void diagnoseRetainCycle(Sema &S, Expr *capturer, | 
|  | 5617 | RetainCycleOwner &owner) { | 
|  | 5618 | assert(capturer); | 
|  | 5619 | assert(owner.Variable && owner.Loc.isValid()); | 
|  | 5620 |  | 
|  | 5621 | S.Diag(capturer->getExprLoc(), diag::warn_arc_retain_cycle) | 
|  | 5622 | << owner.Variable << capturer->getSourceRange(); | 
|  | 5623 | S.Diag(owner.Loc, diag::note_arc_retain_cycle_owner) | 
|  | 5624 | << owner.Indirect << owner.Range; | 
|  | 5625 | } | 
|  | 5626 |  | 
|  | 5627 | /// Check for a keyword selector that starts with the word 'add' or | 
|  | 5628 | /// 'set'. | 
|  | 5629 | static bool isSetterLikeSelector(Selector sel) { | 
|  | 5630 | if (sel.isUnarySelector()) return false; | 
|  | 5631 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5632 | StringRef str = sel.getNameForSlot(0); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5633 | while (!str.empty() && str.front() == '_') str = str.substr(1); | 
| Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 5634 | if (str.startswith("set")) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5635 | str = str.substr(3); | 
| Ted Kremenek | 968a0ee | 2011-12-01 00:59:21 +0000 | [diff] [blame] | 5636 | else if (str.startswith("add")) { | 
|  | 5637 | // Specially whitelist 'addOperationWithBlock:'. | 
|  | 5638 | if (sel.getNumArgs() == 1 && str.startswith("addOperationWithBlock")) | 
|  | 5639 | return false; | 
|  | 5640 | str = str.substr(3); | 
|  | 5641 | } | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5642 | else | 
|  | 5643 | return false; | 
|  | 5644 |  | 
|  | 5645 | if (str.empty()) return true; | 
|  | 5646 | return !islower(str.front()); | 
|  | 5647 | } | 
|  | 5648 |  | 
|  | 5649 | /// Check a message send to see if it's likely to cause a retain cycle. | 
|  | 5650 | void Sema::checkRetainCycles(ObjCMessageExpr *msg) { | 
|  | 5651 | // Only check instance methods whose selector looks like a setter. | 
|  | 5652 | if (!msg->isInstanceMessage() || !isSetterLikeSelector(msg->getSelector())) | 
|  | 5653 | return; | 
|  | 5654 |  | 
|  | 5655 | // Try to find a variable that the receiver is strongly owned by. | 
|  | 5656 | RetainCycleOwner owner; | 
|  | 5657 | if (msg->getReceiverKind() == ObjCMessageExpr::Instance) { | 
| Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5658 | if (!findRetainCycleOwner(*this, msg->getInstanceReceiver(), owner)) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5659 | return; | 
|  | 5660 | } else { | 
|  | 5661 | assert(msg->getReceiverKind() == ObjCMessageExpr::SuperInstance); | 
|  | 5662 | owner.Variable = getCurMethodDecl()->getSelfDecl(); | 
|  | 5663 | owner.Loc = msg->getSuperLoc(); | 
|  | 5664 | owner.Range = msg->getSuperLoc(); | 
|  | 5665 | } | 
|  | 5666 |  | 
|  | 5667 | // Check whether the receiver is captured by any of the arguments. | 
|  | 5668 | for (unsigned i = 0, e = msg->getNumArgs(); i != e; ++i) | 
|  | 5669 | if (Expr *capturer = findCapturingExpr(*this, msg->getArg(i), owner)) | 
|  | 5670 | return diagnoseRetainCycle(*this, capturer, owner); | 
|  | 5671 | } | 
|  | 5672 |  | 
|  | 5673 | /// Check a property assign to see if it's likely to cause a retain cycle. | 
|  | 5674 | void Sema::checkRetainCycles(Expr *receiver, Expr *argument) { | 
|  | 5675 | RetainCycleOwner owner; | 
| Fariborz Jahanian | 6e6f93a | 2012-01-10 19:28:26 +0000 | [diff] [blame] | 5676 | if (!findRetainCycleOwner(*this, receiver, owner)) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5677 | return; | 
|  | 5678 |  | 
|  | 5679 | if (Expr *capturer = findCapturingExpr(*this, argument, owner)) | 
|  | 5680 | diagnoseRetainCycle(*this, capturer, owner); | 
|  | 5681 | } | 
|  | 5682 |  | 
| Jordan Rose | e10f4d3 | 2012-09-15 02:48:31 +0000 | [diff] [blame] | 5683 | void Sema::checkRetainCycles(VarDecl *Var, Expr *Init) { | 
|  | 5684 | RetainCycleOwner Owner; | 
|  | 5685 | if (!considerVariable(Var, /*DeclRefExpr=*/0, Owner)) | 
|  | 5686 | return; | 
|  | 5687 |  | 
|  | 5688 | // Because we don't have an expression for the variable, we have to set the | 
|  | 5689 | // location explicitly here. | 
|  | 5690 | Owner.Loc = Var->getLocation(); | 
|  | 5691 | Owner.Range = Var->getSourceRange(); | 
|  | 5692 |  | 
|  | 5693 | if (Expr *Capturer = findCapturingExpr(*this, Init, Owner)) | 
|  | 5694 | diagnoseRetainCycle(*this, Capturer, Owner); | 
|  | 5695 | } | 
|  | 5696 |  | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5697 | bool Sema::checkUnsafeAssigns(SourceLocation Loc, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5698 | QualType LHS, Expr *RHS) { | 
|  | 5699 | Qualifiers::ObjCLifetime LT = LHS.getObjCLifetime(); | 
|  | 5700 | if (LT != Qualifiers::OCL_Weak && LT != Qualifiers::OCL_ExplicitNone) | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5701 | return false; | 
|  | 5702 | // strip off any implicit cast added to get to the one arc-specific | 
|  | 5703 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { | 
| John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5704 | if (cast->getCastKind() == CK_ARCConsumeObject) { | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5705 | Diag(Loc, diag::warn_arc_retained_assign) | 
| Fariborz Jahanian | bd2e27e | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 5706 | << (LT == Qualifiers::OCL_ExplicitNone) << 1 | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5707 | << RHS->getSourceRange(); | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5708 | return true; | 
|  | 5709 | } | 
|  | 5710 | RHS = cast->getSubExpr(); | 
|  | 5711 | } | 
|  | 5712 | return false; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5713 | } | 
|  | 5714 |  | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5715 | void Sema::checkUnsafeExprAssigns(SourceLocation Loc, | 
|  | 5716 | Expr *LHS, Expr *RHS) { | 
| Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5717 | QualType LHSType; | 
|  | 5718 | // PropertyRef on LHS type need be directly obtained from | 
|  | 5719 | // its declaration as it has a PsuedoType. | 
|  | 5720 | ObjCPropertyRefExpr *PRE | 
|  | 5721 | = dyn_cast<ObjCPropertyRefExpr>(LHS->IgnoreParens()); | 
|  | 5722 | if (PRE && !PRE->isImplicitProperty()) { | 
|  | 5723 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); | 
|  | 5724 | if (PD) | 
|  | 5725 | LHSType = PD->getType(); | 
|  | 5726 | } | 
|  | 5727 |  | 
|  | 5728 | if (LHSType.isNull()) | 
|  | 5729 | LHSType = LHS->getType(); | 
| Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 5730 |  | 
|  | 5731 | Qualifiers::ObjCLifetime LT = LHSType.getObjCLifetime(); | 
|  | 5732 |  | 
|  | 5733 | if (LT == Qualifiers::OCL_Weak) { | 
|  | 5734 | DiagnosticsEngine::Level Level = | 
|  | 5735 | Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak, Loc); | 
|  | 5736 | if (Level != DiagnosticsEngine::Ignored) | 
|  | 5737 | getCurFunction()->markSafeWeakUse(LHS); | 
|  | 5738 | } | 
|  | 5739 |  | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5740 | if (checkUnsafeAssigns(Loc, LHSType, RHS)) | 
|  | 5741 | return; | 
| Jordan Rose | 7a27048 | 2012-09-28 22:21:35 +0000 | [diff] [blame] | 5742 |  | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5743 | // FIXME. Check for other life times. | 
|  | 5744 | if (LT != Qualifiers::OCL_None) | 
|  | 5745 | return; | 
|  | 5746 |  | 
| Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5747 | if (PRE) { | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5748 | if (PRE->isImplicitProperty()) | 
|  | 5749 | return; | 
|  | 5750 | const ObjCPropertyDecl *PD = PRE->getExplicitProperty(); | 
|  | 5751 | if (!PD) | 
|  | 5752 | return; | 
|  | 5753 |  | 
|  | 5754 | unsigned Attributes = PD->getPropertyAttributes(); | 
| Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5755 | if (Attributes & ObjCPropertyDecl::OBJC_PR_assign) { | 
|  | 5756 | // when 'assign' attribute was not explicitly specified | 
|  | 5757 | // by user, ignore it and rely on property type itself | 
|  | 5758 | // for lifetime info. | 
|  | 5759 | unsigned AsWrittenAttr = PD->getPropertyAttributesAsWritten(); | 
|  | 5760 | if (!(AsWrittenAttr & ObjCPropertyDecl::OBJC_PR_assign) && | 
|  | 5761 | LHSType->isObjCRetainableType()) | 
|  | 5762 | return; | 
|  | 5763 |  | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5764 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { | 
| John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5765 | if (cast->getCastKind() == CK_ARCConsumeObject) { | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5766 | Diag(Loc, diag::warn_arc_retained_property_assign) | 
|  | 5767 | << RHS->getSourceRange(); | 
|  | 5768 | return; | 
|  | 5769 | } | 
|  | 5770 | RHS = cast->getSubExpr(); | 
|  | 5771 | } | 
| Fariborz Jahanian | 87eaf72 | 2012-01-17 22:58:16 +0000 | [diff] [blame] | 5772 | } | 
| Fariborz Jahanian | bd2e27e | 2012-07-06 21:09:27 +0000 | [diff] [blame] | 5773 | else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { | 
|  | 5774 | while (ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(RHS)) { | 
|  | 5775 | if (cast->getCastKind() == CK_ARCConsumeObject) { | 
|  | 5776 | Diag(Loc, diag::warn_arc_retained_assign) | 
|  | 5777 | << 0 << 0<< RHS->getSourceRange(); | 
|  | 5778 | return; | 
|  | 5779 | } | 
|  | 5780 | RHS = cast->getSubExpr(); | 
|  | 5781 | } | 
|  | 5782 | } | 
| Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 5783 | } | 
|  | 5784 | } | 
| Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 5785 |  | 
|  | 5786 | //===--- CHECK: Empty statement body (-Wempty-body) ---------------------===// | 
|  | 5787 |  | 
|  | 5788 | namespace { | 
|  | 5789 | bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr, | 
|  | 5790 | SourceLocation StmtLoc, | 
|  | 5791 | const NullStmt *Body) { | 
|  | 5792 | // Do not warn if the body is a macro that expands to nothing, e.g: | 
|  | 5793 | // | 
|  | 5794 | // #define CALL(x) | 
|  | 5795 | // if (condition) | 
|  | 5796 | //   CALL(0); | 
|  | 5797 | // | 
|  | 5798 | if (Body->hasLeadingEmptyMacro()) | 
|  | 5799 | return false; | 
|  | 5800 |  | 
|  | 5801 | // Get line numbers of statement and body. | 
|  | 5802 | bool StmtLineInvalid; | 
|  | 5803 | unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc, | 
|  | 5804 | &StmtLineInvalid); | 
|  | 5805 | if (StmtLineInvalid) | 
|  | 5806 | return false; | 
|  | 5807 |  | 
|  | 5808 | bool BodyLineInvalid; | 
|  | 5809 | unsigned BodyLine = SourceMgr.getSpellingLineNumber(Body->getSemiLoc(), | 
|  | 5810 | &BodyLineInvalid); | 
|  | 5811 | if (BodyLineInvalid) | 
|  | 5812 | return false; | 
|  | 5813 |  | 
|  | 5814 | // Warn if null statement and body are on the same line. | 
|  | 5815 | if (StmtLine != BodyLine) | 
|  | 5816 | return false; | 
|  | 5817 |  | 
|  | 5818 | return true; | 
|  | 5819 | } | 
|  | 5820 | } // Unnamed namespace | 
|  | 5821 |  | 
|  | 5822 | void Sema::DiagnoseEmptyStmtBody(SourceLocation StmtLoc, | 
|  | 5823 | const Stmt *Body, | 
|  | 5824 | unsigned DiagID) { | 
|  | 5825 | // Since this is a syntactic check, don't emit diagnostic for template | 
|  | 5826 | // instantiations, this just adds noise. | 
|  | 5827 | if (CurrentInstantiationScope) | 
|  | 5828 | return; | 
|  | 5829 |  | 
|  | 5830 | // The body should be a null statement. | 
|  | 5831 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); | 
|  | 5832 | if (!NBody) | 
|  | 5833 | return; | 
|  | 5834 |  | 
|  | 5835 | // Do the usual checks. | 
|  | 5836 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) | 
|  | 5837 | return; | 
|  | 5838 |  | 
|  | 5839 | Diag(NBody->getSemiLoc(), DiagID); | 
|  | 5840 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); | 
|  | 5841 | } | 
|  | 5842 |  | 
|  | 5843 | void Sema::DiagnoseEmptyLoopBody(const Stmt *S, | 
|  | 5844 | const Stmt *PossibleBody) { | 
|  | 5845 | assert(!CurrentInstantiationScope); // Ensured by caller | 
|  | 5846 |  | 
|  | 5847 | SourceLocation StmtLoc; | 
|  | 5848 | const Stmt *Body; | 
|  | 5849 | unsigned DiagID; | 
|  | 5850 | if (const ForStmt *FS = dyn_cast<ForStmt>(S)) { | 
|  | 5851 | StmtLoc = FS->getRParenLoc(); | 
|  | 5852 | Body = FS->getBody(); | 
|  | 5853 | DiagID = diag::warn_empty_for_body; | 
|  | 5854 | } else if (const WhileStmt *WS = dyn_cast<WhileStmt>(S)) { | 
|  | 5855 | StmtLoc = WS->getCond()->getSourceRange().getEnd(); | 
|  | 5856 | Body = WS->getBody(); | 
|  | 5857 | DiagID = diag::warn_empty_while_body; | 
|  | 5858 | } else | 
|  | 5859 | return; // Neither `for' nor `while'. | 
|  | 5860 |  | 
|  | 5861 | // The body should be a null statement. | 
|  | 5862 | const NullStmt *NBody = dyn_cast<NullStmt>(Body); | 
|  | 5863 | if (!NBody) | 
|  | 5864 | return; | 
|  | 5865 |  | 
|  | 5866 | // Skip expensive checks if diagnostic is disabled. | 
|  | 5867 | if (Diags.getDiagnosticLevel(DiagID, NBody->getSemiLoc()) == | 
|  | 5868 | DiagnosticsEngine::Ignored) | 
|  | 5869 | return; | 
|  | 5870 |  | 
|  | 5871 | // Do the usual checks. | 
|  | 5872 | if (!ShouldDiagnoseEmptyStmtBody(SourceMgr, StmtLoc, NBody)) | 
|  | 5873 | return; | 
|  | 5874 |  | 
|  | 5875 | // `for(...);' and `while(...);' are popular idioms, so in order to keep | 
|  | 5876 | // noise level low, emit diagnostics only if for/while is followed by a | 
|  | 5877 | // CompoundStmt, e.g.: | 
|  | 5878 | //    for (int i = 0; i < n; i++); | 
|  | 5879 | //    { | 
|  | 5880 | //      a(i); | 
|  | 5881 | //    } | 
|  | 5882 | // or if for/while is followed by a statement with more indentation | 
|  | 5883 | // than for/while itself: | 
|  | 5884 | //    for (int i = 0; i < n; i++); | 
|  | 5885 | //      a(i); | 
|  | 5886 | bool ProbableTypo = isa<CompoundStmt>(PossibleBody); | 
|  | 5887 | if (!ProbableTypo) { | 
|  | 5888 | bool BodyColInvalid; | 
|  | 5889 | unsigned BodyCol = SourceMgr.getPresumedColumnNumber( | 
|  | 5890 | PossibleBody->getLocStart(), | 
|  | 5891 | &BodyColInvalid); | 
|  | 5892 | if (BodyColInvalid) | 
|  | 5893 | return; | 
|  | 5894 |  | 
|  | 5895 | bool StmtColInvalid; | 
|  | 5896 | unsigned StmtCol = SourceMgr.getPresumedColumnNumber( | 
|  | 5897 | S->getLocStart(), | 
|  | 5898 | &StmtColInvalid); | 
|  | 5899 | if (StmtColInvalid) | 
|  | 5900 | return; | 
|  | 5901 |  | 
|  | 5902 | if (BodyCol > StmtCol) | 
|  | 5903 | ProbableTypo = true; | 
|  | 5904 | } | 
|  | 5905 |  | 
|  | 5906 | if (ProbableTypo) { | 
|  | 5907 | Diag(NBody->getSemiLoc(), DiagID); | 
|  | 5908 | Diag(NBody->getSemiLoc(), diag::note_empty_body_on_separate_line); | 
|  | 5909 | } | 
|  | 5910 | } | 
| Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 5911 |  | 
|  | 5912 | //===--- Layout compatibility ----------------------------------------------// | 
|  | 5913 |  | 
|  | 5914 | namespace { | 
|  | 5915 |  | 
|  | 5916 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2); | 
|  | 5917 |  | 
|  | 5918 | /// \brief Check if two enumeration types are layout-compatible. | 
|  | 5919 | bool isLayoutCompatible(ASTContext &C, EnumDecl *ED1, EnumDecl *ED2) { | 
|  | 5920 | // C++11 [dcl.enum] p8: | 
|  | 5921 | // Two enumeration types are layout-compatible if they have the same | 
|  | 5922 | // underlying type. | 
|  | 5923 | return ED1->isComplete() && ED2->isComplete() && | 
|  | 5924 | C.hasSameType(ED1->getIntegerType(), ED2->getIntegerType()); | 
|  | 5925 | } | 
|  | 5926 |  | 
|  | 5927 | /// \brief Check if two fields are layout-compatible. | 
|  | 5928 | bool isLayoutCompatible(ASTContext &C, FieldDecl *Field1, FieldDecl *Field2) { | 
|  | 5929 | if (!isLayoutCompatible(C, Field1->getType(), Field2->getType())) | 
|  | 5930 | return false; | 
|  | 5931 |  | 
|  | 5932 | if (Field1->isBitField() != Field2->isBitField()) | 
|  | 5933 | return false; | 
|  | 5934 |  | 
|  | 5935 | if (Field1->isBitField()) { | 
|  | 5936 | // Make sure that the bit-fields are the same length. | 
|  | 5937 | unsigned Bits1 = Field1->getBitWidthValue(C); | 
|  | 5938 | unsigned Bits2 = Field2->getBitWidthValue(C); | 
|  | 5939 |  | 
|  | 5940 | if (Bits1 != Bits2) | 
|  | 5941 | return false; | 
|  | 5942 | } | 
|  | 5943 |  | 
|  | 5944 | return true; | 
|  | 5945 | } | 
|  | 5946 |  | 
|  | 5947 | /// \brief Check if two standard-layout structs are layout-compatible. | 
|  | 5948 | /// (C++11 [class.mem] p17) | 
|  | 5949 | bool isLayoutCompatibleStruct(ASTContext &C, | 
|  | 5950 | RecordDecl *RD1, | 
|  | 5951 | RecordDecl *RD2) { | 
|  | 5952 | // If both records are C++ classes, check that base classes match. | 
|  | 5953 | if (const CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(RD1)) { | 
|  | 5954 | // If one of records is a CXXRecordDecl we are in C++ mode, | 
|  | 5955 | // thus the other one is a CXXRecordDecl, too. | 
|  | 5956 | const CXXRecordDecl *D2CXX = cast<CXXRecordDecl>(RD2); | 
|  | 5957 | // Check number of base classes. | 
|  | 5958 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) | 
|  | 5959 | return false; | 
|  | 5960 |  | 
|  | 5961 | // Check the base classes. | 
|  | 5962 | for (CXXRecordDecl::base_class_const_iterator | 
|  | 5963 | Base1 = D1CXX->bases_begin(), | 
|  | 5964 | BaseEnd1 = D1CXX->bases_end(), | 
|  | 5965 | Base2 = D2CXX->bases_begin(); | 
|  | 5966 | Base1 != BaseEnd1; | 
|  | 5967 | ++Base1, ++Base2) { | 
|  | 5968 | if (!isLayoutCompatible(C, Base1->getType(), Base2->getType())) | 
|  | 5969 | return false; | 
|  | 5970 | } | 
|  | 5971 | } else if (const CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(RD2)) { | 
|  | 5972 | // If only RD2 is a C++ class, it should have zero base classes. | 
|  | 5973 | if (D2CXX->getNumBases() > 0) | 
|  | 5974 | return false; | 
|  | 5975 | } | 
|  | 5976 |  | 
|  | 5977 | // Check the fields. | 
|  | 5978 | RecordDecl::field_iterator Field2 = RD2->field_begin(), | 
|  | 5979 | Field2End = RD2->field_end(), | 
|  | 5980 | Field1 = RD1->field_begin(), | 
|  | 5981 | Field1End = RD1->field_end(); | 
|  | 5982 | for ( ; Field1 != Field1End && Field2 != Field2End; ++Field1, ++Field2) { | 
|  | 5983 | if (!isLayoutCompatible(C, *Field1, *Field2)) | 
|  | 5984 | return false; | 
|  | 5985 | } | 
|  | 5986 | if (Field1 != Field1End || Field2 != Field2End) | 
|  | 5987 | return false; | 
|  | 5988 |  | 
|  | 5989 | return true; | 
|  | 5990 | } | 
|  | 5991 |  | 
|  | 5992 | /// \brief Check if two standard-layout unions are layout-compatible. | 
|  | 5993 | /// (C++11 [class.mem] p18) | 
|  | 5994 | bool isLayoutCompatibleUnion(ASTContext &C, | 
|  | 5995 | RecordDecl *RD1, | 
|  | 5996 | RecordDecl *RD2) { | 
|  | 5997 | llvm::SmallPtrSet<FieldDecl *, 8> UnmatchedFields; | 
|  | 5998 | for (RecordDecl::field_iterator Field2 = RD2->field_begin(), | 
|  | 5999 | Field2End = RD2->field_end(); | 
|  | 6000 | Field2 != Field2End; ++Field2) { | 
|  | 6001 | UnmatchedFields.insert(*Field2); | 
|  | 6002 | } | 
|  | 6003 |  | 
|  | 6004 | for (RecordDecl::field_iterator Field1 = RD1->field_begin(), | 
|  | 6005 | Field1End = RD1->field_end(); | 
|  | 6006 | Field1 != Field1End; ++Field1) { | 
|  | 6007 | llvm::SmallPtrSet<FieldDecl *, 8>::iterator | 
|  | 6008 | I = UnmatchedFields.begin(), | 
|  | 6009 | E = UnmatchedFields.end(); | 
|  | 6010 |  | 
|  | 6011 | for ( ; I != E; ++I) { | 
|  | 6012 | if (isLayoutCompatible(C, *Field1, *I)) { | 
|  | 6013 | bool Result = UnmatchedFields.erase(*I); | 
|  | 6014 | (void) Result; | 
|  | 6015 | assert(Result); | 
|  | 6016 | break; | 
|  | 6017 | } | 
|  | 6018 | } | 
|  | 6019 | if (I == E) | 
|  | 6020 | return false; | 
|  | 6021 | } | 
|  | 6022 |  | 
|  | 6023 | return UnmatchedFields.empty(); | 
|  | 6024 | } | 
|  | 6025 |  | 
|  | 6026 | bool isLayoutCompatible(ASTContext &C, RecordDecl *RD1, RecordDecl *RD2) { | 
|  | 6027 | if (RD1->isUnion() != RD2->isUnion()) | 
|  | 6028 | return false; | 
|  | 6029 |  | 
|  | 6030 | if (RD1->isUnion()) | 
|  | 6031 | return isLayoutCompatibleUnion(C, RD1, RD2); | 
|  | 6032 | else | 
|  | 6033 | return isLayoutCompatibleStruct(C, RD1, RD2); | 
|  | 6034 | } | 
|  | 6035 |  | 
|  | 6036 | /// \brief Check if two types are layout-compatible in C++11 sense. | 
|  | 6037 | bool isLayoutCompatible(ASTContext &C, QualType T1, QualType T2) { | 
|  | 6038 | if (T1.isNull() || T2.isNull()) | 
|  | 6039 | return false; | 
|  | 6040 |  | 
|  | 6041 | // C++11 [basic.types] p11: | 
|  | 6042 | // If two types T1 and T2 are the same type, then T1 and T2 are | 
|  | 6043 | // layout-compatible types. | 
|  | 6044 | if (C.hasSameType(T1, T2)) | 
|  | 6045 | return true; | 
|  | 6046 |  | 
|  | 6047 | T1 = T1.getCanonicalType().getUnqualifiedType(); | 
|  | 6048 | T2 = T2.getCanonicalType().getUnqualifiedType(); | 
|  | 6049 |  | 
|  | 6050 | const Type::TypeClass TC1 = T1->getTypeClass(); | 
|  | 6051 | const Type::TypeClass TC2 = T2->getTypeClass(); | 
|  | 6052 |  | 
|  | 6053 | if (TC1 != TC2) | 
|  | 6054 | return false; | 
|  | 6055 |  | 
|  | 6056 | if (TC1 == Type::Enum) { | 
|  | 6057 | return isLayoutCompatible(C, | 
|  | 6058 | cast<EnumType>(T1)->getDecl(), | 
|  | 6059 | cast<EnumType>(T2)->getDecl()); | 
|  | 6060 | } else if (TC1 == Type::Record) { | 
|  | 6061 | if (!T1->isStandardLayoutType() || !T2->isStandardLayoutType()) | 
|  | 6062 | return false; | 
|  | 6063 |  | 
|  | 6064 | return isLayoutCompatible(C, | 
|  | 6065 | cast<RecordType>(T1)->getDecl(), | 
|  | 6066 | cast<RecordType>(T2)->getDecl()); | 
|  | 6067 | } | 
|  | 6068 |  | 
|  | 6069 | return false; | 
|  | 6070 | } | 
|  | 6071 | } | 
|  | 6072 |  | 
|  | 6073 | //===--- CHECK: pointer_with_type_tag attribute: datatypes should match ----// | 
|  | 6074 |  | 
|  | 6075 | namespace { | 
|  | 6076 | /// \brief Given a type tag expression find the type tag itself. | 
|  | 6077 | /// | 
|  | 6078 | /// \param TypeExpr Type tag expression, as it appears in user's code. | 
|  | 6079 | /// | 
|  | 6080 | /// \param VD Declaration of an identifier that appears in a type tag. | 
|  | 6081 | /// | 
|  | 6082 | /// \param MagicValue Type tag magic value. | 
|  | 6083 | bool FindTypeTagExpr(const Expr *TypeExpr, const ASTContext &Ctx, | 
|  | 6084 | const ValueDecl **VD, uint64_t *MagicValue) { | 
|  | 6085 | while(true) { | 
|  | 6086 | if (!TypeExpr) | 
|  | 6087 | return false; | 
|  | 6088 |  | 
|  | 6089 | TypeExpr = TypeExpr->IgnoreParenImpCasts()->IgnoreParenCasts(); | 
|  | 6090 |  | 
|  | 6091 | switch (TypeExpr->getStmtClass()) { | 
|  | 6092 | case Stmt::UnaryOperatorClass: { | 
|  | 6093 | const UnaryOperator *UO = cast<UnaryOperator>(TypeExpr); | 
|  | 6094 | if (UO->getOpcode() == UO_AddrOf || UO->getOpcode() == UO_Deref) { | 
|  | 6095 | TypeExpr = UO->getSubExpr(); | 
|  | 6096 | continue; | 
|  | 6097 | } | 
|  | 6098 | return false; | 
|  | 6099 | } | 
|  | 6100 |  | 
|  | 6101 | case Stmt::DeclRefExprClass: { | 
|  | 6102 | const DeclRefExpr *DRE = cast<DeclRefExpr>(TypeExpr); | 
|  | 6103 | *VD = DRE->getDecl(); | 
|  | 6104 | return true; | 
|  | 6105 | } | 
|  | 6106 |  | 
|  | 6107 | case Stmt::IntegerLiteralClass: { | 
|  | 6108 | const IntegerLiteral *IL = cast<IntegerLiteral>(TypeExpr); | 
|  | 6109 | llvm::APInt MagicValueAPInt = IL->getValue(); | 
|  | 6110 | if (MagicValueAPInt.getActiveBits() <= 64) { | 
|  | 6111 | *MagicValue = MagicValueAPInt.getZExtValue(); | 
|  | 6112 | return true; | 
|  | 6113 | } else | 
|  | 6114 | return false; | 
|  | 6115 | } | 
|  | 6116 |  | 
|  | 6117 | case Stmt::BinaryConditionalOperatorClass: | 
|  | 6118 | case Stmt::ConditionalOperatorClass: { | 
|  | 6119 | const AbstractConditionalOperator *ACO = | 
|  | 6120 | cast<AbstractConditionalOperator>(TypeExpr); | 
|  | 6121 | bool Result; | 
|  | 6122 | if (ACO->getCond()->EvaluateAsBooleanCondition(Result, Ctx)) { | 
|  | 6123 | if (Result) | 
|  | 6124 | TypeExpr = ACO->getTrueExpr(); | 
|  | 6125 | else | 
|  | 6126 | TypeExpr = ACO->getFalseExpr(); | 
|  | 6127 | continue; | 
|  | 6128 | } | 
|  | 6129 | return false; | 
|  | 6130 | } | 
|  | 6131 |  | 
|  | 6132 | case Stmt::BinaryOperatorClass: { | 
|  | 6133 | const BinaryOperator *BO = cast<BinaryOperator>(TypeExpr); | 
|  | 6134 | if (BO->getOpcode() == BO_Comma) { | 
|  | 6135 | TypeExpr = BO->getRHS(); | 
|  | 6136 | continue; | 
|  | 6137 | } | 
|  | 6138 | return false; | 
|  | 6139 | } | 
|  | 6140 |  | 
|  | 6141 | default: | 
|  | 6142 | return false; | 
|  | 6143 | } | 
|  | 6144 | } | 
|  | 6145 | } | 
|  | 6146 |  | 
|  | 6147 | /// \brief Retrieve the C type corresponding to type tag TypeExpr. | 
|  | 6148 | /// | 
|  | 6149 | /// \param TypeExpr Expression that specifies a type tag. | 
|  | 6150 | /// | 
|  | 6151 | /// \param MagicValues Registered magic values. | 
|  | 6152 | /// | 
|  | 6153 | /// \param FoundWrongKind Set to true if a type tag was found, but of a wrong | 
|  | 6154 | ///        kind. | 
|  | 6155 | /// | 
|  | 6156 | /// \param TypeInfo Information about the corresponding C type. | 
|  | 6157 | /// | 
|  | 6158 | /// \returns true if the corresponding C type was found. | 
|  | 6159 | bool GetMatchingCType( | 
|  | 6160 | const IdentifierInfo *ArgumentKind, | 
|  | 6161 | const Expr *TypeExpr, const ASTContext &Ctx, | 
|  | 6162 | const llvm::DenseMap<Sema::TypeTagMagicValue, | 
|  | 6163 | Sema::TypeTagData> *MagicValues, | 
|  | 6164 | bool &FoundWrongKind, | 
|  | 6165 | Sema::TypeTagData &TypeInfo) { | 
|  | 6166 | FoundWrongKind = false; | 
|  | 6167 |  | 
|  | 6168 | // Variable declaration that has type_tag_for_datatype attribute. | 
|  | 6169 | const ValueDecl *VD = NULL; | 
|  | 6170 |  | 
|  | 6171 | uint64_t MagicValue; | 
|  | 6172 |  | 
|  | 6173 | if (!FindTypeTagExpr(TypeExpr, Ctx, &VD, &MagicValue)) | 
|  | 6174 | return false; | 
|  | 6175 |  | 
|  | 6176 | if (VD) { | 
|  | 6177 | for (specific_attr_iterator<TypeTagForDatatypeAttr> | 
|  | 6178 | I = VD->specific_attr_begin<TypeTagForDatatypeAttr>(), | 
|  | 6179 | E = VD->specific_attr_end<TypeTagForDatatypeAttr>(); | 
|  | 6180 | I != E; ++I) { | 
|  | 6181 | if (I->getArgumentKind() != ArgumentKind) { | 
|  | 6182 | FoundWrongKind = true; | 
|  | 6183 | return false; | 
|  | 6184 | } | 
|  | 6185 | TypeInfo.Type = I->getMatchingCType(); | 
|  | 6186 | TypeInfo.LayoutCompatible = I->getLayoutCompatible(); | 
|  | 6187 | TypeInfo.MustBeNull = I->getMustBeNull(); | 
|  | 6188 | return true; | 
|  | 6189 | } | 
|  | 6190 | return false; | 
|  | 6191 | } | 
|  | 6192 |  | 
|  | 6193 | if (!MagicValues) | 
|  | 6194 | return false; | 
|  | 6195 |  | 
|  | 6196 | llvm::DenseMap<Sema::TypeTagMagicValue, | 
|  | 6197 | Sema::TypeTagData>::const_iterator I = | 
|  | 6198 | MagicValues->find(std::make_pair(ArgumentKind, MagicValue)); | 
|  | 6199 | if (I == MagicValues->end()) | 
|  | 6200 | return false; | 
|  | 6201 |  | 
|  | 6202 | TypeInfo = I->second; | 
|  | 6203 | return true; | 
|  | 6204 | } | 
|  | 6205 | } // unnamed namespace | 
|  | 6206 |  | 
|  | 6207 | void Sema::RegisterTypeTagForDatatype(const IdentifierInfo *ArgumentKind, | 
|  | 6208 | uint64_t MagicValue, QualType Type, | 
|  | 6209 | bool LayoutCompatible, | 
|  | 6210 | bool MustBeNull) { | 
|  | 6211 | if (!TypeTagForDatatypeMagicValues) | 
|  | 6212 | TypeTagForDatatypeMagicValues.reset( | 
|  | 6213 | new llvm::DenseMap<TypeTagMagicValue, TypeTagData>); | 
|  | 6214 |  | 
|  | 6215 | TypeTagMagicValue Magic(ArgumentKind, MagicValue); | 
|  | 6216 | (*TypeTagForDatatypeMagicValues)[Magic] = | 
|  | 6217 | TypeTagData(Type, LayoutCompatible, MustBeNull); | 
|  | 6218 | } | 
|  | 6219 |  | 
|  | 6220 | namespace { | 
|  | 6221 | bool IsSameCharType(QualType T1, QualType T2) { | 
|  | 6222 | const BuiltinType *BT1 = T1->getAs<BuiltinType>(); | 
|  | 6223 | if (!BT1) | 
|  | 6224 | return false; | 
|  | 6225 |  | 
|  | 6226 | const BuiltinType *BT2 = T2->getAs<BuiltinType>(); | 
|  | 6227 | if (!BT2) | 
|  | 6228 | return false; | 
|  | 6229 |  | 
|  | 6230 | BuiltinType::Kind T1Kind = BT1->getKind(); | 
|  | 6231 | BuiltinType::Kind T2Kind = BT2->getKind(); | 
|  | 6232 |  | 
|  | 6233 | return (T1Kind == BuiltinType::SChar  && T2Kind == BuiltinType::Char_S) || | 
|  | 6234 | (T1Kind == BuiltinType::UChar  && T2Kind == BuiltinType::Char_U) || | 
|  | 6235 | (T1Kind == BuiltinType::Char_U && T2Kind == BuiltinType::UChar) || | 
|  | 6236 | (T1Kind == BuiltinType::Char_S && T2Kind == BuiltinType::SChar); | 
|  | 6237 | } | 
|  | 6238 | } // unnamed namespace | 
|  | 6239 |  | 
|  | 6240 | void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr, | 
|  | 6241 | const Expr * const *ExprArgs) { | 
|  | 6242 | const IdentifierInfo *ArgumentKind = Attr->getArgumentKind(); | 
|  | 6243 | bool IsPointerAttr = Attr->getIsPointer(); | 
|  | 6244 |  | 
|  | 6245 | const Expr *TypeTagExpr = ExprArgs[Attr->getTypeTagIdx()]; | 
|  | 6246 | bool FoundWrongKind; | 
|  | 6247 | TypeTagData TypeInfo; | 
|  | 6248 | if (!GetMatchingCType(ArgumentKind, TypeTagExpr, Context, | 
|  | 6249 | TypeTagForDatatypeMagicValues.get(), | 
|  | 6250 | FoundWrongKind, TypeInfo)) { | 
|  | 6251 | if (FoundWrongKind) | 
|  | 6252 | Diag(TypeTagExpr->getExprLoc(), | 
|  | 6253 | diag::warn_type_tag_for_datatype_wrong_kind) | 
|  | 6254 | << TypeTagExpr->getSourceRange(); | 
|  | 6255 | return; | 
|  | 6256 | } | 
|  | 6257 |  | 
|  | 6258 | const Expr *ArgumentExpr = ExprArgs[Attr->getArgumentIdx()]; | 
|  | 6259 | if (IsPointerAttr) { | 
|  | 6260 | // Skip implicit cast of pointer to `void *' (as a function argument). | 
|  | 6261 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr)) | 
| Dmitri Gribenko | 5a24980 | 2012-11-03 16:07:49 +0000 | [diff] [blame] | 6262 | if (ICE->getType()->isVoidPointerType() && | 
| Dmitri Gribenko | b57ce4e | 2012-11-03 22:10:18 +0000 | [diff] [blame] | 6263 | ICE->getCastKind() == CK_BitCast) | 
| Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6264 | ArgumentExpr = ICE->getSubExpr(); | 
|  | 6265 | } | 
|  | 6266 | QualType ArgumentType = ArgumentExpr->getType(); | 
|  | 6267 |  | 
|  | 6268 | // Passing a `void*' pointer shouldn't trigger a warning. | 
|  | 6269 | if (IsPointerAttr && ArgumentType->isVoidPointerType()) | 
|  | 6270 | return; | 
|  | 6271 |  | 
|  | 6272 | if (TypeInfo.MustBeNull) { | 
|  | 6273 | // Type tag with matching void type requires a null pointer. | 
|  | 6274 | if (!ArgumentExpr->isNullPointerConstant(Context, | 
|  | 6275 | Expr::NPC_ValueDependentIsNotNull)) { | 
|  | 6276 | Diag(ArgumentExpr->getExprLoc(), | 
|  | 6277 | diag::warn_type_safety_null_pointer_required) | 
|  | 6278 | << ArgumentKind->getName() | 
|  | 6279 | << ArgumentExpr->getSourceRange() | 
|  | 6280 | << TypeTagExpr->getSourceRange(); | 
|  | 6281 | } | 
|  | 6282 | return; | 
|  | 6283 | } | 
|  | 6284 |  | 
|  | 6285 | QualType RequiredType = TypeInfo.Type; | 
|  | 6286 | if (IsPointerAttr) | 
|  | 6287 | RequiredType = Context.getPointerType(RequiredType); | 
|  | 6288 |  | 
|  | 6289 | bool mismatch = false; | 
|  | 6290 | if (!TypeInfo.LayoutCompatible) { | 
|  | 6291 | mismatch = !Context.hasSameType(ArgumentType, RequiredType); | 
|  | 6292 |  | 
|  | 6293 | // C++11 [basic.fundamental] p1: | 
|  | 6294 | // Plain char, signed char, and unsigned char are three distinct types. | 
|  | 6295 | // | 
|  | 6296 | // But we treat plain `char' as equivalent to `signed char' or `unsigned | 
|  | 6297 | // char' depending on the current char signedness mode. | 
|  | 6298 | if (mismatch) | 
|  | 6299 | if ((IsPointerAttr && IsSameCharType(ArgumentType->getPointeeType(), | 
|  | 6300 | RequiredType->getPointeeType())) || | 
|  | 6301 | (!IsPointerAttr && IsSameCharType(ArgumentType, RequiredType))) | 
|  | 6302 | mismatch = false; | 
|  | 6303 | } else | 
|  | 6304 | if (IsPointerAttr) | 
|  | 6305 | mismatch = !isLayoutCompatible(Context, | 
|  | 6306 | ArgumentType->getPointeeType(), | 
|  | 6307 | RequiredType->getPointeeType()); | 
|  | 6308 | else | 
|  | 6309 | mismatch = !isLayoutCompatible(Context, ArgumentType, RequiredType); | 
|  | 6310 |  | 
|  | 6311 | if (mismatch) | 
|  | 6312 | Diag(ArgumentExpr->getExprLoc(), diag::warn_type_safety_type_mismatch) | 
|  | 6313 | << ArgumentType << ArgumentKind->getName() | 
|  | 6314 | << TypeInfo.LayoutCompatible << RequiredType | 
|  | 6315 | << ArgumentExpr->getSourceRange() | 
|  | 6316 | << TypeTagExpr->getSourceRange(); | 
|  | 6317 | } |