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 | // |
| 10 | // This file implements extra semantic analysis beyond what is enforced |
| 11 | // by the C type system. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Sema.h" |
| 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LiteralSupport.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 24 | /// getLocationOfStringLiteralByte - Return a source location that points to the |
| 25 | /// specified byte of the specified string literal. |
| 26 | /// |
| 27 | /// Strings are amazingly complex. They can be formed from multiple tokens and |
| 28 | /// can have escape sequences in them in addition to the usual trigraph and |
| 29 | /// escaped newline business. This routine handles this complexity. |
| 30 | /// |
| 31 | SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, |
| 32 | unsigned ByteNo) const { |
| 33 | assert(!SL->isWide() && "This doesn't work for wide strings yet"); |
| 34 | |
| 35 | // Loop over all of the tokens in this string until we find the one that |
| 36 | // contains the byte we're looking for. |
| 37 | unsigned TokNo = 0; |
| 38 | while (1) { |
| 39 | assert(TokNo < SL->getNumConcatenated() && "Invalid byte number!"); |
| 40 | SourceLocation StrTokLoc = SL->getStrTokenLoc(TokNo); |
| 41 | |
| 42 | // Get the spelling of the string so that we can get the data that makes up |
| 43 | // the string literal, not the identifier for the macro it is potentially |
| 44 | // expanded through. |
| 45 | SourceLocation StrTokSpellingLoc = SourceMgr.getSpellingLoc(StrTokLoc); |
| 46 | |
| 47 | // Re-lex the token to get its length and original spelling. |
| 48 | std::pair<FileID, unsigned> LocInfo = |
| 49 | SourceMgr.getDecomposedLoc(StrTokSpellingLoc); |
| 50 | std::pair<const char *,const char *> Buffer = |
| 51 | SourceMgr.getBufferData(LocInfo.first); |
| 52 | const char *StrData = Buffer.first+LocInfo.second; |
| 53 | |
| 54 | // Create a langops struct and enable trigraphs. This is sufficient for |
| 55 | // relexing tokens. |
| 56 | LangOptions LangOpts; |
| 57 | LangOpts.Trigraphs = true; |
| 58 | |
| 59 | // Create a lexer starting at the beginning of this token. |
| 60 | Lexer TheLexer(StrTokSpellingLoc, LangOpts, Buffer.first, StrData, |
| 61 | Buffer.second); |
| 62 | Token TheTok; |
| 63 | TheLexer.LexFromRawLexer(TheTok); |
| 64 | |
Chris Lattner | 443e53c | 2009-02-18 19:26:42 +0000 | [diff] [blame] | 65 | // Use the StringLiteralParser to compute the length of the string in bytes. |
| 66 | StringLiteralParser SLP(&TheTok, 1, PP); |
| 67 | unsigned TokNumBytes = SLP.GetStringLength(); |
Chris Lattner | d0d082f | 2009-02-18 18:34:12 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 2197c96 | 2009-02-18 18:52:52 +0000 | [diff] [blame] | 69 | // If the byte is in this token, return the location of the byte. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 70 | if (ByteNo < TokNumBytes || |
| 71 | (ByteNo == TokNumBytes && TokNo == SL->getNumConcatenated())) { |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 72 | unsigned Offset = |
| 73 | StringLiteralParser::getOffsetOfStringByte(TheTok, ByteNo, PP); |
| 74 | |
| 75 | // Now that we know the offset of the token in the spelling, use the |
| 76 | // preprocessor to get the offset in the original source. |
| 77 | return PP.AdvanceToTokenCharacter(StrTokLoc, Offset); |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Move to the next string token. |
| 81 | ++TokNo; |
| 82 | ByteNo -= TokNumBytes; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 87 | /// CheckFunctionCall - Check a direct function call for various correctness |
| 88 | /// and safety properties not strictly enforced by the C type system. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 89 | Action::OwningExprResult |
| 90 | Sema::CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall) { |
| 91 | OwningExprResult TheCallResult(Owned(TheCall)); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 92 | // Get the IdentifierInfo* for the called function. |
| 93 | IdentifierInfo *FnInfo = FDecl->getIdentifier(); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 94 | |
| 95 | // None of the checks below are needed for functions that don't have |
| 96 | // simple names (e.g., C++ conversion functions). |
| 97 | if (!FnInfo) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 98 | return move(TheCallResult); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 99 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 100 | switch (FDecl->getBuiltinID(Context)) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 101 | case Builtin::BI__builtin___CFStringMakeConstantString: |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 102 | assert(TheCall->getNumArgs() == 1 && |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 103 | "Wrong # arguments to builtin CFStringMakeConstantString"); |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 104 | if (CheckObjCString(TheCall->getArg(0))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 105 | return ExprError(); |
| 106 | return move(TheCallResult); |
Ted Kremenek | 49ff7a1 | 2008-07-09 17:58:53 +0000 | [diff] [blame] | 107 | case Builtin::BI__builtin_stdarg_start: |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 108 | case Builtin::BI__builtin_va_start: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 109 | if (SemaBuiltinVAStart(TheCall)) |
| 110 | return ExprError(); |
| 111 | return move(TheCallResult); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 112 | case Builtin::BI__builtin_isgreater: |
| 113 | case Builtin::BI__builtin_isgreaterequal: |
| 114 | case Builtin::BI__builtin_isless: |
| 115 | case Builtin::BI__builtin_islessequal: |
| 116 | case Builtin::BI__builtin_islessgreater: |
| 117 | case Builtin::BI__builtin_isunordered: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 118 | if (SemaBuiltinUnorderedCompare(TheCall)) |
| 119 | return ExprError(); |
| 120 | return move(TheCallResult); |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 121 | case Builtin::BI__builtin_return_address: |
| 122 | case Builtin::BI__builtin_frame_address: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 123 | if (SemaBuiltinStackAddress(TheCall)) |
| 124 | return ExprError(); |
| 125 | return move(TheCallResult); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 126 | case Builtin::BI__builtin_shufflevector: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 127 | return SemaBuiltinShuffleVector(TheCall); |
| 128 | // TheCall will be freed by the smart pointer here, but that's fine, since |
| 129 | // SemaBuiltinShuffleVector guts it, but then doesn't release it. |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 130 | case Builtin::BI__builtin_prefetch: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 131 | if (SemaBuiltinPrefetch(TheCall)) |
| 132 | return ExprError(); |
| 133 | return move(TheCallResult); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 134 | case Builtin::BI__builtin_object_size: |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 135 | if (SemaBuiltinObjectSize(TheCall)) |
| 136 | return ExprError(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 137 | } |
Daniel Dunbar | de45428 | 2008-10-02 18:44:07 +0000 | [diff] [blame] | 138 | |
| 139 | // FIXME: This mechanism should be abstracted to be less fragile and |
| 140 | // more efficient. For example, just map function ids to custom |
| 141 | // handlers. |
| 142 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 143 | // Printf checking. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 144 | if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>()) { |
| 145 | if (Format->getType() == "printf") { |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 146 | bool HasVAListArg = Format->getFirstArg() == 0; |
| 147 | if (!HasVAListArg) { |
| 148 | if (const FunctionProtoType *Proto |
| 149 | = FDecl->getType()->getAsFunctionProtoType()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 150 | HasVAListArg = !Proto->isVariadic(); |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 151 | } |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 152 | CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, |
Ted Kremenek | 3d692df | 2009-02-27 17:58:43 +0000 | [diff] [blame] | 153 | HasVAListArg ? 0 : Format->getFirstArg() - 1); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 155 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 156 | |
| 157 | return move(TheCallResult); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 160 | /// CheckObjCString - Checks that the argument to the builtin |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 161 | /// CFString constructor is correct |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 162 | bool Sema::CheckObjCString(Expr *Arg) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 163 | Arg = Arg->IgnoreParenCasts(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 164 | StringLiteral *Literal = dyn_cast<StringLiteral>(Arg); |
| 165 | |
| 166 | if (!Literal || Literal->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 167 | Diag(Arg->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 168 | << Arg->getSourceRange(); |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 169 | return true; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | const char *Data = Literal->getStrData(); |
| 173 | unsigned Length = Literal->getByteLength(); |
| 174 | |
| 175 | for (unsigned i = 0; i < Length; ++i) { |
| 176 | if (!isascii(Data[i])) { |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 177 | Diag(getLocationOfStringLiteralByte(Literal, i), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 178 | diag::warn_cfstring_literal_contains_non_ascii_character) |
| 179 | << Arg->getSourceRange(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | |
| 183 | if (!Data[i]) { |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 184 | Diag(getLocationOfStringLiteralByte(Literal, i), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 185 | diag::warn_cfstring_literal_contains_nul_character) |
| 186 | << Arg->getSourceRange(); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 191 | return false; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 194 | /// SemaBuiltinVAStart - Check the arguments to __builtin_va_start for validity. |
| 195 | /// Emit an error and return true on failure, return false on success. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 196 | bool Sema::SemaBuiltinVAStart(CallExpr *TheCall) { |
| 197 | Expr *Fn = TheCall->getCallee(); |
| 198 | if (TheCall->getNumArgs() > 2) { |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 199 | Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 200 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 201 | << 0 /*function call*/ << Fn->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 202 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 203 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 204 | return true; |
| 205 | } |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 206 | |
| 207 | if (TheCall->getNumArgs() < 2) { |
| 208 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 209 | << 0 /*function call*/; |
| 210 | } |
| 211 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 212 | // Determine whether the current function is variadic or not. |
| 213 | bool isVariadic; |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 214 | if (getCurFunctionDecl()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 215 | if (FunctionProtoType* FTP = |
| 216 | dyn_cast<FunctionProtoType>(getCurFunctionDecl()->getType())) |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 217 | isVariadic = FTP->isVariadic(); |
| 218 | else |
| 219 | isVariadic = false; |
| 220 | } else { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 221 | isVariadic = getCurMethodDecl()->isVariadic(); |
Eli Friedman | 56f20ae | 2008-12-15 22:05:35 +0000 | [diff] [blame] | 222 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 223 | |
Chris Lattner | c27c665 | 2007-12-20 00:05:45 +0000 | [diff] [blame] | 224 | if (!isVariadic) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 225 | Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function); |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | // Verify that the second argument to the builtin is the last argument of the |
| 230 | // current function or method. |
| 231 | bool SecondArgIsLastNamedArgument = false; |
Anders Carlsson | e2c1410 | 2008-02-13 01:22:59 +0000 | [diff] [blame] | 232 | const Expr *Arg = TheCall->getArg(1)->IgnoreParenCasts(); |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 233 | |
| 234 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Arg)) { |
| 235 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) { |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 236 | // FIXME: This isn't correct for methods (results in bogus warning). |
| 237 | // Get the last formal in the current function. |
Anders Carlsson | 88cf226 | 2008-02-11 04:20:54 +0000 | [diff] [blame] | 238 | const ParmVarDecl *LastArg; |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 239 | if (FunctionDecl *FD = getCurFunctionDecl()) |
| 240 | LastArg = *(FD->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 241 | else |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 242 | LastArg = *(getCurMethodDecl()->param_end()-1); |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 243 | SecondArgIsLastNamedArgument = PV == LastArg; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (!SecondArgIsLastNamedArgument) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 248 | Diag(TheCall->getArg(1)->getLocStart(), |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 249 | diag::warn_second_parameter_of_va_start_not_last_named_argument); |
| 250 | return false; |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 253 | /// SemaBuiltinUnorderedCompare - Handle functions like __builtin_isgreater and |
| 254 | /// friends. This is declared to take (...), so we have to check everything. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 255 | bool Sema::SemaBuiltinUnorderedCompare(CallExpr *TheCall) { |
| 256 | if (TheCall->getNumArgs() < 2) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 257 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_few_args) |
| 258 | << 0 /*function call*/; |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 259 | if (TheCall->getNumArgs() > 2) |
| 260 | return Diag(TheCall->getArg(2)->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 261 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 262 | << 0 /*function call*/ |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 263 | << SourceRange(TheCall->getArg(2)->getLocStart(), |
| 264 | (*(TheCall->arg_end()-1))->getLocEnd()); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 266 | Expr *OrigArg0 = TheCall->getArg(0); |
| 267 | Expr *OrigArg1 = TheCall->getArg(1); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 268 | |
| 269 | // Do standard promotions between the two arguments, returning their common |
| 270 | // type. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 271 | QualType Res = UsualArithmeticConversions(OrigArg0, OrigArg1, false); |
Daniel Dunbar | 403bc2b | 2009-02-19 19:28:43 +0000 | [diff] [blame] | 272 | |
| 273 | // Make sure any conversions are pushed back into the call; this is |
| 274 | // type safe since unordered compare builtins are declared as "_Bool |
| 275 | // foo(...)". |
| 276 | TheCall->setArg(0, OrigArg0); |
| 277 | TheCall->setArg(1, OrigArg1); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 278 | |
| 279 | // If the common type isn't a real floating type, then the arguments were |
| 280 | // invalid for this operation. |
| 281 | if (!Res->isRealFloatingType()) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 282 | return Diag(OrigArg0->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 283 | diag::err_typecheck_call_invalid_ordered_compare) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 284 | << OrigArg0->getType() << OrigArg1->getType() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 285 | << SourceRange(OrigArg0->getLocStart(), OrigArg1->getLocEnd()); |
Chris Lattner | 1b9a079 | 2007-12-20 00:26:33 +0000 | [diff] [blame] | 286 | |
| 287 | return false; |
| 288 | } |
| 289 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 290 | bool Sema::SemaBuiltinStackAddress(CallExpr *TheCall) { |
| 291 | // The signature for these builtins is exact; the only thing we need |
| 292 | // to check is that the argument is a constant. |
| 293 | SourceLocation Loc; |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 294 | if (!TheCall->getArg(0)->isIntegerConstantExpr(Context, &Loc)) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 295 | return Diag(Loc, diag::err_stack_const_level) << TheCall->getSourceRange(); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 296 | |
Eli Friedman | 6cfda23 | 2008-05-20 08:23:37 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 300 | /// SemaBuiltinShuffleVector - Handle __builtin_shufflevector. |
| 301 | // This is declared to take (...), so we have to check everything. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 302 | Action::OwningExprResult Sema::SemaBuiltinShuffleVector(CallExpr *TheCall) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 303 | if (TheCall->getNumArgs() < 3) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 304 | return ExprError(Diag(TheCall->getLocEnd(), |
| 305 | diag::err_typecheck_call_too_few_args) |
| 306 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 307 | |
| 308 | QualType FAType = TheCall->getArg(0)->getType(); |
| 309 | QualType SAType = TheCall->getArg(1)->getType(); |
| 310 | |
| 311 | if (!FAType->isVectorType() || !SAType->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 312 | Diag(TheCall->getLocStart(), diag::err_shufflevector_non_vector) |
| 313 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 314 | TheCall->getArg(1)->getLocEnd()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 315 | return ExprError(); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 318 | if (Context.getCanonicalType(FAType).getUnqualifiedType() != |
| 319 | Context.getCanonicalType(SAType).getUnqualifiedType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 320 | Diag(TheCall->getLocStart(), diag::err_shufflevector_incompatible_vector) |
| 321 | << SourceRange(TheCall->getArg(0)->getLocStart(), |
| 322 | TheCall->getArg(1)->getLocEnd()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 323 | return ExprError(); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | unsigned numElements = FAType->getAsVectorType()->getNumElements(); |
| 327 | if (TheCall->getNumArgs() != numElements+2) { |
| 328 | if (TheCall->getNumArgs() < numElements+2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 329 | return ExprError(Diag(TheCall->getLocEnd(), |
| 330 | diag::err_typecheck_call_too_few_args) |
| 331 | << 0 /*function call*/ << TheCall->getSourceRange()); |
| 332 | return ExprError(Diag(TheCall->getLocEnd(), |
| 333 | diag::err_typecheck_call_too_many_args) |
| 334 | << 0 /*function call*/ << TheCall->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | for (unsigned i = 2; i < TheCall->getNumArgs(); i++) { |
| 338 | llvm::APSInt Result(32); |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 339 | if (!TheCall->getArg(i)->isIntegerConstantExpr(Result, Context)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 340 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 341 | diag::err_shufflevector_nonconstant_argument) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 342 | << TheCall->getArg(i)->getSourceRange()); |
| 343 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 344 | if (Result.getActiveBits() > 64 || Result.getZExtValue() >= numElements*2) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 345 | return ExprError(Diag(TheCall->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 346 | diag::err_shufflevector_argument_too_large) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 347 | << TheCall->getArg(i)->getSourceRange()); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | llvm::SmallVector<Expr*, 32> exprs; |
| 351 | |
Chris Lattner | d1a0b6d | 2008-08-10 02:05:13 +0000 | [diff] [blame] | 352 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; i++) { |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 353 | exprs.push_back(TheCall->getArg(i)); |
| 354 | TheCall->setArg(i, 0); |
| 355 | } |
| 356 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 357 | return Owned(new (Context) ShuffleVectorExpr(exprs.begin(), numElements+2, |
| 358 | FAType, |
| 359 | TheCall->getCallee()->getLocStart(), |
| 360 | TheCall->getRParenLoc())); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 361 | } |
Chris Lattner | 30ce344 | 2007-12-19 23:59:04 +0000 | [diff] [blame] | 362 | |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 363 | /// SemaBuiltinPrefetch - Handle __builtin_prefetch. |
| 364 | // This is declared to take (const void*, ...) and can take two |
| 365 | // optional constant int args. |
| 366 | bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 367 | unsigned NumArgs = TheCall->getNumArgs(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 368 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 369 | if (NumArgs > 3) |
| 370 | return Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 371 | << 0 /*function call*/ << TheCall->getSourceRange(); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 372 | |
| 373 | // Argument 0 is checked for us and the remaining arguments must be |
| 374 | // constant integers. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 375 | for (unsigned i = 1; i != NumArgs; ++i) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 376 | Expr *Arg = TheCall->getArg(i); |
| 377 | QualType RWType = Arg->getType(); |
| 378 | |
| 379 | const BuiltinType *BT = RWType->getAsBuiltinType(); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 380 | llvm::APSInt Result; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 381 | if (!BT || BT->getKind() != BuiltinType::Int || |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 382 | !Arg->isIntegerConstantExpr(Result, Context)) |
| 383 | return Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument) |
| 384 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 385 | |
| 386 | // FIXME: gcc issues a warning and rewrites these to 0. These |
| 387 | // seems especially odd for the third argument since the default |
| 388 | // is 3. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 389 | if (i == 1) { |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 390 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 391 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 392 | << "0" << "1" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 393 | } else { |
| 394 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 395 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 396 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 400 | return false; |
Daniel Dunbar | 4493f79 | 2008-07-21 22:59:13 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 403 | /// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr, |
| 404 | /// int type). This simply type checks that type is one of the defined |
| 405 | /// constants (0-3). |
| 406 | bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) { |
| 407 | Expr *Arg = TheCall->getArg(1); |
| 408 | QualType ArgType = Arg->getType(); |
| 409 | const BuiltinType *BT = ArgType->getAsBuiltinType(); |
| 410 | llvm::APSInt Result(32); |
| 411 | if (!BT || BT->getKind() != BuiltinType::Int || |
| 412 | !Arg->isIntegerConstantExpr(Result, Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 413 | return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument) |
| 414 | << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 418 | return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range) |
| 419 | << "0" << "3" << SourceRange(Arg->getLocStart(), Arg->getLocEnd()); |
Daniel Dunbar | d5f8a4f | 2008-09-03 21:13:56 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return false; |
| 423 | } |
| 424 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 425 | // Handle i > 1 ? "x" : "y", recursivelly |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 426 | bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, |
| 427 | bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 428 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 429 | |
| 430 | switch (E->getStmtClass()) { |
| 431 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 432 | const ConditionalOperator *C = cast<ConditionalOperator>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 433 | return SemaCheckStringLiteral(C->getLHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 434 | HasVAListArg, format_idx, firstDataArg) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 435 | && SemaCheckStringLiteral(C->getRHS(), TheCall, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 436 | HasVAListArg, format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 440 | const ImplicitCastExpr *Expr = cast<ImplicitCastExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 441 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 442 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | case Stmt::ParenExprClass: { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 446 | const ParenExpr *Expr = cast<ParenExpr>(E); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 447 | return SemaCheckStringLiteral(Expr->getSubExpr(), TheCall, HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 448 | format_idx, firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 449 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 450 | |
| 451 | case Stmt::DeclRefExprClass: { |
| 452 | const DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 453 | |
| 454 | // As an exception, do not flag errors for variables binding to |
| 455 | // const string literals. |
| 456 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 457 | bool isConstant = false; |
| 458 | QualType T = DR->getType(); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 460 | if (const ArrayType *AT = Context.getAsArrayType(T)) { |
| 461 | isConstant = AT->getElementType().isConstant(Context); |
| 462 | } |
| 463 | else if (const PointerType *PT = T->getAsPointerType()) { |
| 464 | isConstant = T.isConstant(Context) && |
| 465 | PT->getPointeeType().isConstant(Context); |
| 466 | } |
| 467 | |
| 468 | if (isConstant) { |
| 469 | const VarDecl *Def = 0; |
| 470 | if (const Expr *Init = VD->getDefinition(Def)) |
| 471 | return SemaCheckStringLiteral(Init, TheCall, |
| 472 | HasVAListArg, format_idx, firstDataArg); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return false; |
| 477 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 478 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 479 | case Stmt::ObjCStringLiteralClass: |
| 480 | case Stmt::StringLiteralClass: { |
| 481 | const StringLiteral *StrE = NULL; |
| 482 | |
| 483 | if (const ObjCStringLiteral *ObjCFExpr = dyn_cast<ObjCStringLiteral>(E)) |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 484 | StrE = ObjCFExpr->getString(); |
| 485 | else |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 486 | StrE = cast<StringLiteral>(E); |
| 487 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 488 | if (StrE) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 489 | CheckPrintfString(StrE, E, TheCall, HasVAListArg, format_idx, |
| 490 | firstDataArg); |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | |
| 494 | return false; |
| 495 | } |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 496 | |
| 497 | default: |
| 498 | return false; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 503 | /// CheckPrintfArguments - Check calls to printf (and similar functions) for |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 504 | /// correct use of format strings. |
| 505 | /// |
| 506 | /// HasVAListArg - A predicate indicating whether the printf-like |
| 507 | /// function is passed an explicit va_arg argument (e.g., vprintf) |
| 508 | /// |
| 509 | /// format_idx - The index into Args for the format string. |
| 510 | /// |
| 511 | /// Improper format strings to functions in the printf family can be |
| 512 | /// the source of bizarre bugs and very serious security holes. A |
| 513 | /// good source of information is available in the following paper |
| 514 | /// (which includes additional references): |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 515 | /// |
| 516 | /// FormatGuard: Automatic Protection From printf Format String |
| 517 | /// Vulnerabilities, Proceedings of the 10th USENIX Security Symposium, 2001. |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 518 | /// |
| 519 | /// Functionality implemented: |
| 520 | /// |
| 521 | /// We can statically check the following properties for string |
| 522 | /// literal format strings for non v.*printf functions (where the |
| 523 | /// arguments are passed directly): |
| 524 | // |
| 525 | /// (1) Are the number of format conversions equal to the number of |
| 526 | /// data arguments? |
| 527 | /// |
| 528 | /// (2) Does each format conversion correctly match the type of the |
| 529 | /// corresponding data argument? (TODO) |
| 530 | /// |
| 531 | /// Moreover, for all printf functions we can: |
| 532 | /// |
| 533 | /// (3) Check for a missing format string (when not caught by type checking). |
| 534 | /// |
| 535 | /// (4) Check for no-operation flags; e.g. using "#" with format |
| 536 | /// conversion 'c' (TODO) |
| 537 | /// |
| 538 | /// (5) Check the use of '%n', a major source of security holes. |
| 539 | /// |
| 540 | /// (6) Check for malformed format conversions that don't specify anything. |
| 541 | /// |
| 542 | /// (7) Check for empty format strings. e.g: printf(""); |
| 543 | /// |
| 544 | /// (8) Check that the format string is a wide literal. |
| 545 | /// |
Ted Kremenek | 6d43959 | 2008-03-03 16:50:00 +0000 | [diff] [blame] | 546 | /// (9) Also check the arguments of functions with the __format__ attribute. |
| 547 | /// (TODO). |
| 548 | /// |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 549 | /// All of these checks can be done by parsing the format string. |
| 550 | /// |
| 551 | /// For now, we ONLY do (1), (3), (5), (6), (7), and (8). |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 552 | void |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 553 | Sema::CheckPrintfArguments(const CallExpr *TheCall, bool HasVAListArg, |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 554 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 555 | const Expr *Fn = TheCall->getCallee(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 556 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 557 | // CHECK: printf-like function is called with no format string. |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 558 | if (format_idx >= TheCall->getNumArgs()) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 559 | Diag(TheCall->getRParenLoc(), diag::warn_printf_missing_format_string) |
| 560 | << Fn->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 561 | return; |
| 562 | } |
| 563 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 564 | const Expr *OrigFormatExpr = TheCall->getArg(format_idx)->IgnoreParenCasts(); |
Chris Lattner | 459e848 | 2007-08-25 05:36:18 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 566 | // CHECK: format string is not a string literal. |
| 567 | // |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 568 | // Dynamically generated format strings are difficult to |
| 569 | // automatically vet at compile time. Requiring that format strings |
| 570 | // are string literals: (1) permits the checking of format strings by |
| 571 | // the compiler and thereby (2) can practically remove the source of |
| 572 | // many format string exploits. |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 573 | |
| 574 | // Format string can be either ObjC string (e.g. @"%d") or |
| 575 | // C string (e.g. "%d") |
| 576 | // ObjC string uses the same format specifiers as C string, so we can use |
| 577 | // the same format string checking logic for both ObjC and C strings. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 578 | bool isFExpr = SemaCheckStringLiteral(OrigFormatExpr, TheCall, |
| 579 | HasVAListArg, format_idx, |
| 580 | firstDataArg); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 581 | |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 582 | if (!isFExpr) { |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 583 | // For vprintf* functions (i.e., HasVAListArg==true), we add a |
| 584 | // special check to see if the format string is a function parameter |
| 585 | // of the function calling the printf function. If the function |
| 586 | // has an attribute indicating it is a printf-like function, then we |
| 587 | // should suppress warnings concerning non-literals being used in a call |
| 588 | // to a vprintf function. For example: |
| 589 | // |
| 590 | // void |
| 591 | // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) { |
| 592 | // va_list ap; |
| 593 | // va_start(ap, fmt); |
| 594 | // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". |
| 595 | // ... |
| 596 | // |
| 597 | // |
| 598 | // FIXME: We don't have full attribute support yet, so just check to see |
| 599 | // if the argument is a DeclRefExpr that references a parameter. We'll |
| 600 | // add proper support for checking the attribute later. |
| 601 | if (HasVAListArg) |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 602 | if (const DeclRefExpr* DR = dyn_cast<DeclRefExpr>(OrigFormatExpr)) |
Chris Lattner | 998568f | 2007-12-28 05:38:24 +0000 | [diff] [blame] | 603 | if (isa<ParmVarDecl>(DR->getDecl())) |
Ted Kremenek | 4a33646 | 2007-12-17 19:03:13 +0000 | [diff] [blame] | 604 | return; |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 606 | Diag(TheCall->getArg(format_idx)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 607 | diag::warn_printf_not_string_constant) |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 608 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 609 | return; |
| 610 | } |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 611 | } |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 612 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 613 | void Sema::CheckPrintfString(const StringLiteral *FExpr, |
| 614 | const Expr *OrigFormatExpr, |
| 615 | const CallExpr *TheCall, bool HasVAListArg, |
| 616 | unsigned format_idx, unsigned firstDataArg) { |
Ted Kremenek | d30ef87 | 2009-01-12 23:09:09 +0000 | [diff] [blame] | 617 | |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 618 | const ObjCStringLiteral *ObjCFExpr = |
| 619 | dyn_cast<ObjCStringLiteral>(OrigFormatExpr); |
| 620 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 621 | // CHECK: is the format string a wide literal? |
| 622 | if (FExpr->isWide()) { |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 623 | Diag(FExpr->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 624 | diag::warn_printf_format_string_is_wide_literal) |
| 625 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 626 | return; |
| 627 | } |
| 628 | |
| 629 | // Str - The format string. NOTE: this is NOT null-terminated! |
| 630 | const char * const Str = FExpr->getStrData(); |
| 631 | |
| 632 | // CHECK: empty format string? |
| 633 | const unsigned StrLen = FExpr->getByteLength(); |
| 634 | |
| 635 | if (StrLen == 0) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 636 | Diag(FExpr->getLocStart(), diag::warn_printf_empty_format_string) |
| 637 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 638 | return; |
| 639 | } |
| 640 | |
| 641 | // We process the format string using a binary state machine. The |
| 642 | // current state is stored in CurrentState. |
| 643 | enum { |
| 644 | state_OrdChr, |
| 645 | state_Conversion |
| 646 | } CurrentState = state_OrdChr; |
| 647 | |
| 648 | // numConversions - The number of conversions seen so far. This is |
| 649 | // incremented as we traverse the format string. |
| 650 | unsigned numConversions = 0; |
| 651 | |
| 652 | // numDataArgs - The number of data arguments after the format |
| 653 | // string. This can only be determined for non vprintf-like |
| 654 | // functions. For those functions, this value is 1 (the sole |
| 655 | // va_arg argument). |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 656 | unsigned numDataArgs = TheCall->getNumArgs()-firstDataArg; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 657 | |
| 658 | // Inspect the format string. |
| 659 | unsigned StrIdx = 0; |
| 660 | |
| 661 | // LastConversionIdx - Index within the format string where we last saw |
| 662 | // a '%' character that starts a new format conversion. |
| 663 | unsigned LastConversionIdx = 0; |
| 664 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 665 | for (; StrIdx < StrLen; ++StrIdx) { |
Chris Lattner | 998568f | 2007-12-28 05:38:24 +0000 | [diff] [blame] | 666 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 667 | // Is the number of detected conversion conversions greater than |
| 668 | // the number of matching data arguments? If so, stop. |
| 669 | if (!HasVAListArg && numConversions > numDataArgs) break; |
| 670 | |
| 671 | // Handle "\0" |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 672 | if (Str[StrIdx] == '\0') { |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 673 | // The string returned by getStrData() is not null-terminated, |
| 674 | // so the presence of a null character is likely an error. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 675 | Diag(getLocationOfStringLiteralByte(FExpr, StrIdx), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 676 | diag::warn_printf_format_string_contains_null_char) |
| 677 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | |
| 681 | // Ordinary characters (not processing a format conversion). |
| 682 | if (CurrentState == state_OrdChr) { |
| 683 | if (Str[StrIdx] == '%') { |
| 684 | CurrentState = state_Conversion; |
| 685 | LastConversionIdx = StrIdx; |
| 686 | } |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | // Seen '%'. Now processing a format conversion. |
| 691 | switch (Str[StrIdx]) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 692 | // Handle dynamic precision or width specifier. |
| 693 | case '*': { |
| 694 | ++numConversions; |
| 695 | |
| 696 | if (!HasVAListArg && numConversions > numDataArgs) { |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 697 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 698 | |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 699 | if (Str[StrIdx-1] == '.') |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 700 | Diag(Loc, diag::warn_printf_asterisk_precision_missing_arg) |
| 701 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 702 | else |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 703 | Diag(Loc, diag::warn_printf_asterisk_width_missing_arg) |
| 704 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 705 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 706 | // Don't do any more checking. We'll just emit spurious errors. |
| 707 | return; |
Ted Kremenek | 580b664 | 2007-10-12 20:51:52 +0000 | [diff] [blame] | 708 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 709 | |
| 710 | // Perform type checking on width/precision specifier. |
Ted Kremenek | 082d936 | 2009-03-20 21:35:28 +0000 | [diff] [blame] | 711 | const Expr *E = TheCall->getArg(format_idx+numConversions); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 712 | if (const BuiltinType *BT = E->getType()->getAsBuiltinType()) |
| 713 | if (BT->getKind() == BuiltinType::Int) |
| 714 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 715 | |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 716 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, StrIdx); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 717 | |
| 718 | if (Str[StrIdx-1] == '.') |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 719 | Diag(Loc, diag::warn_printf_asterisk_precision_wrong_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 720 | << E->getType() << E->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 721 | else |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 722 | Diag(Loc, diag::warn_printf_asterisk_width_wrong_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 723 | << E->getType() << E->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 724 | |
| 725 | break; |
| 726 | } |
| 727 | |
| 728 | // Characters which can terminate a format conversion |
| 729 | // (e.g. "%d"). Characters that specify length modifiers or |
| 730 | // other flags are handled by the default case below. |
| 731 | // |
| 732 | // FIXME: additional checks will go into the following cases. |
| 733 | case 'i': |
| 734 | case 'd': |
| 735 | case 'o': |
| 736 | case 'u': |
| 737 | case 'x': |
| 738 | case 'X': |
| 739 | case 'D': |
| 740 | case 'O': |
| 741 | case 'U': |
| 742 | case 'e': |
| 743 | case 'E': |
| 744 | case 'f': |
| 745 | case 'F': |
| 746 | case 'g': |
| 747 | case 'G': |
| 748 | case 'a': |
| 749 | case 'A': |
| 750 | case 'c': |
| 751 | case 'C': |
| 752 | case 'S': |
| 753 | case 's': |
| 754 | case 'p': |
| 755 | ++numConversions; |
| 756 | CurrentState = state_OrdChr; |
| 757 | break; |
| 758 | |
| 759 | // CHECK: Are we using "%n"? Issue a warning. |
| 760 | case 'n': { |
| 761 | ++numConversions; |
| 762 | CurrentState = state_OrdChr; |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 763 | SourceLocation Loc = getLocationOfStringLiteralByte(FExpr, |
| 764 | LastConversionIdx); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 765 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 766 | Diag(Loc, diag::warn_printf_write_back)<<OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 767 | break; |
| 768 | } |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 769 | |
| 770 | // Handle "%@" |
| 771 | case '@': |
| 772 | // %@ is allowed in ObjC format strings only. |
| 773 | if(ObjCFExpr != NULL) |
| 774 | CurrentState = state_OrdChr; |
| 775 | else { |
| 776 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 777 | SourceLocation Loc = |
| 778 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 779 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 780 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 781 | << std::string(Str+LastConversionIdx, |
| 782 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 783 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 784 | } |
| 785 | ++numConversions; |
| 786 | break; |
| 787 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 788 | // Handle "%%" |
| 789 | case '%': |
| 790 | // Sanity check: Was the first "%" character the previous one? |
| 791 | // If not, we will assume that we have a malformed format |
| 792 | // conversion, and that the current "%" character is the start |
| 793 | // of a new conversion. |
| 794 | if (StrIdx - LastConversionIdx == 1) |
| 795 | CurrentState = state_OrdChr; |
| 796 | else { |
| 797 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 798 | SourceLocation Loc = |
| 799 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 800 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 801 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 802 | << std::string(Str+LastConversionIdx, Str+StrIdx) |
| 803 | << OrigFormatExpr->getSourceRange(); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 804 | |
| 805 | // This conversion is broken. Advance to the next format |
| 806 | // conversion. |
| 807 | LastConversionIdx = StrIdx; |
| 808 | ++numConversions; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 809 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 810 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 811 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 812 | default: |
| 813 | // This case catches all other characters: flags, widths, etc. |
| 814 | // We should eventually process those as well. |
| 815 | break; |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
| 819 | if (CurrentState == state_Conversion) { |
| 820 | // Issue a warning: invalid format conversion. |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 821 | SourceLocation Loc = |
| 822 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 823 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 824 | Diag(Loc, diag::warn_printf_invalid_conversion) |
| 825 | << std::string(Str+LastConversionIdx, |
| 826 | Str+std::min(LastConversionIdx+2, StrLen)) |
| 827 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 828 | return; |
| 829 | } |
| 830 | |
| 831 | if (!HasVAListArg) { |
| 832 | // CHECK: Does the number of format conversions exceed the number |
| 833 | // of data arguments? |
| 834 | if (numConversions > numDataArgs) { |
Chris Lattner | 6080008 | 2009-02-18 17:49:48 +0000 | [diff] [blame] | 835 | SourceLocation Loc = |
| 836 | getLocationOfStringLiteralByte(FExpr, LastConversionIdx); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 837 | |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 838 | Diag(Loc, diag::warn_printf_insufficient_data_args) |
| 839 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 840 | } |
| 841 | // CHECK: Does the number of data arguments exceed the number of |
| 842 | // format conversions in the format string? |
| 843 | else if (numConversions < numDataArgs) |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 844 | Diag(TheCall->getArg(format_idx+numConversions+1)->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 845 | diag::warn_printf_too_many_data_args) |
| 846 | << OrigFormatExpr->getSourceRange(); |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 847 | } |
| 848 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 849 | |
| 850 | //===--- CHECK: Return Address of Stack Variable --------------------------===// |
| 851 | |
| 852 | static DeclRefExpr* EvalVal(Expr *E); |
| 853 | static DeclRefExpr* EvalAddr(Expr* E); |
| 854 | |
| 855 | /// CheckReturnStackAddr - Check if a return statement returns the address |
| 856 | /// of a stack variable. |
| 857 | void |
| 858 | Sema::CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 859 | SourceLocation ReturnLoc) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 860 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 861 | // Perform checking for returned stack addresses. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 862 | if (lhsType->isPointerType() || lhsType->isBlockPointerType()) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 863 | if (DeclRefExpr *DR = EvalAddr(RetValExp)) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 864 | Diag(DR->getLocStart(), diag::warn_ret_stack_addr) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 865 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Steve Naroff | c50a4a5 | 2008-09-16 22:25:10 +0000 | [diff] [blame] | 866 | |
| 867 | // Skip over implicit cast expressions when checking for block expressions. |
| 868 | if (ImplicitCastExpr *IcExpr = |
| 869 | dyn_cast_or_null<ImplicitCastExpr>(RetValExp)) |
| 870 | RetValExp = IcExpr->getSubExpr(); |
| 871 | |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 872 | if (BlockExpr *C = dyn_cast_or_null<BlockExpr>(RetValExp)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 873 | Diag(C->getLocStart(), diag::err_ret_local_block) |
| 874 | << C->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 875 | } |
| 876 | // Perform checking for stack values returned by reference. |
| 877 | else if (lhsType->isReferenceType()) { |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 878 | // Check for a reference to the stack |
| 879 | if (DeclRefExpr *DR = EvalVal(RetValExp)) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 880 | Diag(DR->getLocStart(), diag::warn_ret_stack_ref) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 881 | << DR->getDecl()->getDeclName() << RetValExp->getSourceRange(); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
| 885 | /// EvalAddr - EvalAddr and EvalVal are mutually recursive functions that |
| 886 | /// check if the expression in a return statement evaluates to an address |
| 887 | /// to a location on the stack. The recursion is used to traverse the |
| 888 | /// AST of the return expression, with recursion backtracking when we |
| 889 | /// encounter a subexpression that (1) clearly does not lead to the address |
| 890 | /// of a stack variable or (2) is something we cannot determine leads to |
| 891 | /// the address of a stack variable based on such local checking. |
| 892 | /// |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 893 | /// EvalAddr processes expressions that are pointers that are used as |
| 894 | /// references (and not L-values). EvalVal handles all other values. |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 895 | /// At the base case of the recursion is a check for a DeclRefExpr* in |
| 896 | /// the refers to a stack variable. |
| 897 | /// |
| 898 | /// This implementation handles: |
| 899 | /// |
| 900 | /// * pointer-to-pointer casts |
| 901 | /// * implicit conversions from array references to pointers |
| 902 | /// * taking the address of fields |
| 903 | /// * arbitrary interplay between "&" and "*" operators |
| 904 | /// * pointer arithmetic from an address of a stack variable |
| 905 | /// * taking the address of an array element where the array is on the stack |
| 906 | static DeclRefExpr* EvalAddr(Expr *E) { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 907 | // We should only be called for evaluating pointer expressions. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 908 | assert((E->getType()->isPointerType() || |
| 909 | E->getType()->isBlockPointerType() || |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 910 | E->getType()->isObjCQualifiedIdType()) && |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 911 | "EvalAddr only works on pointers"); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 912 | |
| 913 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 914 | // viewed AST node. We then recursively traverse the AST by calling |
| 915 | // EvalAddr and EvalVal appropriately. |
| 916 | switch (E->getStmtClass()) { |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 917 | case Stmt::ParenExprClass: |
| 918 | // Ignore parentheses. |
| 919 | return EvalAddr(cast<ParenExpr>(E)->getSubExpr()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 920 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 921 | case Stmt::UnaryOperatorClass: { |
| 922 | // The only unary operator that make sense to handle here |
| 923 | // is AddrOf. All others don't make sense as pointers. |
| 924 | UnaryOperator *U = cast<UnaryOperator>(E); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 925 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 926 | if (U->getOpcode() == UnaryOperator::AddrOf) |
| 927 | return EvalVal(U->getSubExpr()); |
| 928 | else |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 929 | return NULL; |
| 930 | } |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 931 | |
| 932 | case Stmt::BinaryOperatorClass: { |
| 933 | // Handle pointer arithmetic. All other binary operators are not valid |
| 934 | // in this context. |
| 935 | BinaryOperator *B = cast<BinaryOperator>(E); |
| 936 | BinaryOperator::Opcode op = B->getOpcode(); |
| 937 | |
| 938 | if (op != BinaryOperator::Add && op != BinaryOperator::Sub) |
| 939 | return NULL; |
| 940 | |
| 941 | Expr *Base = B->getLHS(); |
| 942 | |
| 943 | // Determine which argument is the real pointer base. It could be |
| 944 | // the RHS argument instead of the LHS. |
| 945 | if (!Base->getType()->isPointerType()) Base = B->getRHS(); |
| 946 | |
| 947 | assert (Base->getType()->isPointerType()); |
| 948 | return EvalAddr(Base); |
| 949 | } |
Steve Naroff | 61f40a2 | 2008-09-10 19:17:48 +0000 | [diff] [blame] | 950 | |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 951 | // For conditional operators we need to see if either the LHS or RHS are |
| 952 | // valid DeclRefExpr*s. If one of them is valid, we return it. |
| 953 | case Stmt::ConditionalOperatorClass: { |
| 954 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 955 | |
| 956 | // Handle the GNU extension for missing LHS. |
| 957 | if (Expr *lhsExpr = C->getLHS()) |
| 958 | if (DeclRefExpr* LHS = EvalAddr(lhsExpr)) |
| 959 | return LHS; |
| 960 | |
| 961 | return EvalAddr(C->getRHS()); |
| 962 | } |
| 963 | |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 964 | // For casts, we need to handle conversions from arrays to |
| 965 | // pointer values, and pointer-to-pointer conversions. |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 966 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 967 | case Stmt::CStyleCastExprClass: |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 968 | case Stmt::CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 969 | Expr* SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 970 | QualType T = SubExpr->getType(); |
| 971 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 972 | if (SubExpr->getType()->isPointerType() || |
| 973 | SubExpr->getType()->isBlockPointerType() || |
| 974 | SubExpr->getType()->isObjCQualifiedIdType()) |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 975 | return EvalAddr(SubExpr); |
| 976 | else if (T->isArrayType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 977 | return EvalVal(SubExpr); |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 978 | else |
Ted Kremenek | 54b5274 | 2008-08-07 00:49:01 +0000 | [diff] [blame] | 979 | return 0; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | // C++ casts. For dynamic casts, static casts, and const casts, we |
| 983 | // are always converting from a pointer-to-pointer, so we just blow |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 984 | // through the cast. In the case the dynamic cast doesn't fail (and |
| 985 | // return NULL), we take the conservative route and report cases |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 986 | // where we return the address of a stack variable. For Reinterpre |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 987 | // FIXME: The comment about is wrong; we're not always converting |
| 988 | // from pointer to pointer. I'm guessing that this code should also |
| 989 | // handle references to objects. |
| 990 | case Stmt::CXXStaticCastExprClass: |
| 991 | case Stmt::CXXDynamicCastExprClass: |
| 992 | case Stmt::CXXConstCastExprClass: |
| 993 | case Stmt::CXXReinterpretCastExprClass: { |
| 994 | Expr *S = cast<CXXNamedCastExpr>(E)->getSubExpr(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 995 | if (S->getType()->isPointerType() || S->getType()->isBlockPointerType()) |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 996 | return EvalAddr(S); |
| 997 | else |
| 998 | return NULL; |
Chris Lattner | fae3f1f | 2007-12-28 05:31:15 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | // Everything else: we simply don't reason about them. |
| 1002 | default: |
| 1003 | return NULL; |
| 1004 | } |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | |
| 1008 | /// EvalVal - This function is complements EvalAddr in the mutual recursion. |
| 1009 | /// See the comments for EvalAddr for more details. |
| 1010 | static DeclRefExpr* EvalVal(Expr *E) { |
| 1011 | |
Ted Kremenek | e8c600f | 2007-08-28 17:02:55 +0000 | [diff] [blame] | 1012 | // We should only be called for evaluating non-pointer expressions, or |
| 1013 | // expressions with a pointer type that are not used as references but instead |
| 1014 | // are l-values (e.g., DeclRefExpr with a pointer type). |
| 1015 | |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1016 | // Our "symbolic interpreter" is just a dispatch off the currently |
| 1017 | // viewed AST node. We then recursively traverse the AST by calling |
| 1018 | // EvalAddr and EvalVal appropriately. |
| 1019 | switch (E->getStmtClass()) { |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1020 | case Stmt::DeclRefExprClass: |
| 1021 | case Stmt::QualifiedDeclRefExprClass: { |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1022 | // DeclRefExpr: the base case. When we hit a DeclRefExpr we are looking |
| 1023 | // at code that refers to a variable's name. We check if it has local |
| 1024 | // storage within the function, and if so, return the expression. |
| 1025 | DeclRefExpr *DR = cast<DeclRefExpr>(E); |
| 1026 | |
| 1027 | if (VarDecl *V = dyn_cast<VarDecl>(DR->getDecl())) |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1028 | if(V->hasLocalStorage() && !V->getType()->isReferenceType()) return DR; |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1029 | |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
| 1033 | case Stmt::ParenExprClass: |
| 1034 | // Ignore parentheses. |
| 1035 | return EvalVal(cast<ParenExpr>(E)->getSubExpr()); |
| 1036 | |
| 1037 | case Stmt::UnaryOperatorClass: { |
| 1038 | // The only unary operator that make sense to handle here |
| 1039 | // is Deref. All others don't resolve to a "name." This includes |
| 1040 | // handling all sorts of rvalues passed to a unary operator. |
| 1041 | UnaryOperator *U = cast<UnaryOperator>(E); |
| 1042 | |
| 1043 | if (U->getOpcode() == UnaryOperator::Deref) |
| 1044 | return EvalAddr(U->getSubExpr()); |
| 1045 | |
| 1046 | return NULL; |
| 1047 | } |
| 1048 | |
| 1049 | case Stmt::ArraySubscriptExprClass: { |
| 1050 | // Array subscripts are potential references to data on the stack. We |
| 1051 | // retrieve the DeclRefExpr* for the array variable if it indeed |
| 1052 | // has local storage. |
Ted Kremenek | 2324512 | 2007-08-20 16:18:38 +0000 | [diff] [blame] | 1053 | return EvalAddr(cast<ArraySubscriptExpr>(E)->getBase()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | case Stmt::ConditionalOperatorClass: { |
| 1057 | // For conditional operators we need to see if either the LHS or RHS are |
| 1058 | // non-NULL DeclRefExpr's. If one is non-NULL, we return it. |
| 1059 | ConditionalOperator *C = cast<ConditionalOperator>(E); |
| 1060 | |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1061 | // Handle the GNU extension for missing LHS. |
| 1062 | if (Expr *lhsExpr = C->getLHS()) |
| 1063 | if (DeclRefExpr *LHS = EvalVal(lhsExpr)) |
| 1064 | return LHS; |
| 1065 | |
| 1066 | return EvalVal(C->getRHS()); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | // Accesses to members are potential references to data on the stack. |
| 1070 | case Stmt::MemberExprClass: { |
| 1071 | MemberExpr *M = cast<MemberExpr>(E); |
| 1072 | |
| 1073 | // Check for indirect access. We only want direct field accesses. |
| 1074 | if (!M->isArrow()) |
| 1075 | return EvalVal(M->getBase()); |
| 1076 | else |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | |
| 1080 | // Everything else: we simply don't reason about them. |
| 1081 | default: |
| 1082 | return NULL; |
| 1083 | } |
| 1084 | } |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1085 | |
| 1086 | //===--- CHECK: Floating-Point comparisons (-Wfloat-equal) ---------------===// |
| 1087 | |
| 1088 | /// Check for comparisons of floating point operands using != and ==. |
| 1089 | /// Issue a warning if these are no self-comparisons, as they are not likely |
| 1090 | /// to do what the programmer intended. |
| 1091 | void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) { |
| 1092 | bool EmitWarning = true; |
| 1093 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1094 | Expr* LeftExprSansParen = lex->IgnoreParens(); |
Ted Kremenek | 32e97b6 | 2008-01-17 17:55:13 +0000 | [diff] [blame] | 1095 | Expr* RightExprSansParen = rex->IgnoreParens(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1096 | |
| 1097 | // Special case: check for x == x (which is OK). |
| 1098 | // Do not emit warnings for such cases. |
| 1099 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LeftExprSansParen)) |
| 1100 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RightExprSansParen)) |
| 1101 | if (DRL->getDecl() == DRR->getDecl()) |
| 1102 | EmitWarning = false; |
| 1103 | |
Ted Kremenek | 1b500bb | 2007-11-29 00:59:04 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Special case: check for comparisons against literals that can be exactly |
| 1106 | // represented by APFloat. In such cases, do not emit a warning. This |
| 1107 | // is a heuristic: often comparison against such literals are used to |
| 1108 | // detect if a value in a variable has not changed. This clearly can |
| 1109 | // lead to false negatives. |
| 1110 | if (EmitWarning) { |
| 1111 | if (FloatingLiteral* FLL = dyn_cast<FloatingLiteral>(LeftExprSansParen)) { |
| 1112 | if (FLL->isExact()) |
| 1113 | EmitWarning = false; |
| 1114 | } |
| 1115 | else |
| 1116 | if (FloatingLiteral* FLR = dyn_cast<FloatingLiteral>(RightExprSansParen)){ |
| 1117 | if (FLR->isExact()) |
| 1118 | EmitWarning = false; |
| 1119 | } |
| 1120 | } |
| 1121 | |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1122 | // Check for comparisons with builtin types. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1123 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1124 | if (CallExpr* CL = dyn_cast<CallExpr>(LeftExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1125 | if (CL->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1126 | EmitWarning = false; |
| 1127 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1128 | if (EmitWarning) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1129 | if (CallExpr* CR = dyn_cast<CallExpr>(RightExprSansParen)) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1130 | if (CR->isBuiltinCall(Context)) |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1131 | EmitWarning = false; |
| 1132 | |
| 1133 | // Emit the diagnostic. |
| 1134 | if (EmitWarning) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1135 | Diag(loc, diag::warn_floatingpoint_eq) |
| 1136 | << lex->getSourceRange() << rex->getSourceRange(); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1137 | } |