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