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