SemaChecking.cpp (195099) | SemaChecking.cpp (195341) |
---|---|
1//===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// --- 152 unchanged lines hidden (view full) --- 161 return move(TheCallResult); 162 } 163 164 // FIXME: This mechanism should be abstracted to be less fragile and 165 // more efficient. For example, just map function ids to custom 166 // handlers. 167 168 // Printf checking. | 1//===--- SemaChecking.cpp - Extra Semantic Checking -----------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// --- 152 unchanged lines hidden (view full) --- 161 return move(TheCallResult); 162 } 163 164 // FIXME: This mechanism should be abstracted to be less fragile and 165 // more efficient. For example, just map function ids to custom 166 // handlers. 167 168 // Printf checking. |
169 if (const FormatAttr *Format = FDecl->getAttr<FormatAttr>(Context)) { | 169 if (const FormatAttr *Format = FDecl->getAttr |
170 if (Format->getType() == "printf") { 171 bool HasVAListArg = Format->getFirstArg() == 0; 172 if (!HasVAListArg) { 173 if (const FunctionProtoType *Proto 174 = FDecl->getType()->getAsFunctionProtoType()) 175 HasVAListArg = !Proto->isVariadic(); 176 } 177 CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, 178 HasVAListArg ? 0 : Format->getFirstArg() - 1); 179 } 180 } | 170 if (Format->getType() == "printf") { 171 bool HasVAListArg = Format->getFirstArg() == 0; 172 if (!HasVAListArg) { 173 if (const FunctionProtoType *Proto 174 = FDecl->getType()->getAsFunctionProtoType()) 175 HasVAListArg = !Proto->isVariadic(); 176 } 177 CheckPrintfArguments(TheCall, HasVAListArg, Format->getFormatIdx() - 1, 178 HasVAListArg ? 0 : Format->getFirstArg() - 1); 179 } 180 } |
181 for (const Attr *attr = FDecl->getAttrs(Context); | 181 for (const Attr *attr = FDecl->getAttrs(); |
182 attr; attr = attr->getNext()) { 183 if (const NonNullAttr *NonNull = dyn_cast<NonNullAttr>(attr)) 184 CheckNonNullArguments(NonNull, TheCall); 185 } 186 187 return move(TheCallResult); 188} 189 190Action::OwningExprResult 191Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { 192 193 OwningExprResult TheCallResult(Owned(TheCall)); 194 // Printf checking. | 182 attr; attr = attr->getNext()) { 183 if (const NonNullAttr *NonNull = dyn_cast<NonNullAttr>(attr)) 184 CheckNonNullArguments(NonNull, TheCall); 185 } 186 187 return move(TheCallResult); 188} 189 190Action::OwningExprResult 191Sema::CheckBlockCall(NamedDecl *NDecl, CallExpr *TheCall) { 192 193 OwningExprResult TheCallResult(Owned(TheCall)); 194 // Printf checking. |
195 const FormatAttr *Format = NDecl->getAttr<FormatAttr>(Context); | 195 const FormatAttr *Format = NDecl->getAttr |
196 if (!Format) 197 return move(TheCallResult); 198 const VarDecl *V = dyn_cast<VarDecl>(NDecl); 199 if (!V) 200 return move(TheCallResult); 201 QualType Ty = V->getType(); 202 if (!Ty->isBlockPointerType()) 203 return move(TheCallResult); --- 508 unchanged lines hidden (view full) --- 712 713// Handle i > 1 ? "x" : "y", recursivelly 714bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, 715 bool HasVAListArg, 716 unsigned format_idx, unsigned firstDataArg) { 717 if (E->isTypeDependent() || E->isValueDependent()) 718 return false; 719 | 196 if (!Format) 197 return move(TheCallResult); 198 const VarDecl *V = dyn_cast<VarDecl>(NDecl); 199 if (!V) 200 return move(TheCallResult); 201 QualType Ty = V->getType(); 202 if (!Ty->isBlockPointerType()) 203 return move(TheCallResult); --- 508 unchanged lines hidden (view full) --- 712 713// Handle i > 1 ? "x" : "y", recursivelly 714bool Sema::SemaCheckStringLiteral(const Expr *E, const CallExpr *TheCall, 715 bool HasVAListArg, 716 unsigned format_idx, unsigned firstDataArg) { 717 if (E->isTypeDependent() || E->isValueDependent()) 718 return false; 719 |
720 E = E->IgnoreParenCasts(); 721 | |
722 switch (E->getStmtClass()) { 723 case Stmt::ConditionalOperatorClass: { 724 const ConditionalOperator *C = cast<ConditionalOperator>(E); 725 return SemaCheckStringLiteral(C->getLHS(), TheCall, 726 HasVAListArg, format_idx, firstDataArg) 727 && SemaCheckStringLiteral(C->getRHS(), TheCall, 728 HasVAListArg, format_idx, firstDataArg); 729 } --- 28 unchanged lines hidden (view full) --- 758 } 759 760 if (isConstant) { 761 const VarDecl *Def = 0; 762 if (const Expr *Init = VD->getDefinition(Def)) 763 return SemaCheckStringLiteral(Init, TheCall, 764 HasVAListArg, format_idx, firstDataArg); 765 } | 720 switch (E->getStmtClass()) { 721 case Stmt::ConditionalOperatorClass: { 722 const ConditionalOperator *C = cast<ConditionalOperator>(E); 723 return SemaCheckStringLiteral(C->getLHS(), TheCall, 724 HasVAListArg, format_idx, firstDataArg) 725 && SemaCheckStringLiteral(C->getRHS(), TheCall, 726 HasVAListArg, format_idx, firstDataArg); 727 } --- 28 unchanged lines hidden (view full) --- 756 } 757 758 if (isConstant) { 759 const VarDecl *Def = 0; 760 if (const Expr *Init = VD->getDefinition(Def)) 761 return SemaCheckStringLiteral(Init, TheCall, 762 HasVAListArg, format_idx, firstDataArg); 763 } |
764 765 // For vprintf* functions (i.e., HasVAListArg==true), we add a 766 // special check to see if the format string is a function parameter 767 // of the function calling the printf function. If the function 768 // has an attribute indicating it is a printf-like function, then we 769 // should suppress warnings concerning non-literals being used in a call 770 // to a vprintf function. For example: 771 // 772 // void 773 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...){ 774 // va_list ap; 775 // va_start(ap, fmt); 776 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". 777 // ... 778 // 779 // 780 // FIXME: We don't have full attribute support yet, so just check to see 781 // if the argument is a DeclRefExpr that references a parameter. We'll 782 // add proper support for checking the attribute later. 783 if (HasVAListArg) 784 if (isa<ParmVarDecl>(VD)) 785 return true; |
|
766 } 767 768 return false; 769 } 770 771 case Stmt::CallExprClass: { 772 const CallExpr *CE = cast<CallExpr>(E); 773 if (const ImplicitCastExpr *ICE 774 = dyn_cast<ImplicitCastExpr>(CE->getCallee())) { 775 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { 776 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { | 786 } 787 788 return false; 789 } 790 791 case Stmt::CallExprClass: { 792 const CallExpr *CE = cast<CallExpr>(E); 793 if (const ImplicitCastExpr *ICE 794 = dyn_cast<ImplicitCastExpr>(CE->getCallee())) { 795 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) { 796 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
777 if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>(Context)) { | 797 if (const FormatArgAttr *FA = FD->getAttr |
778 unsigned ArgIndex = FA->getFormatIdx(); 779 const Expr *Arg = CE->getArg(ArgIndex - 1); 780 781 return SemaCheckStringLiteral(Arg, TheCall, HasVAListArg, 782 format_idx, firstDataArg); 783 } 784 } 785 } --- 110 unchanged lines hidden (view full) --- 896 // Format string can be either ObjC string (e.g. @"%d") or 897 // C string (e.g. "%d") 898 // ObjC string uses the same format specifiers as C string, so we can use 899 // the same format string checking logic for both ObjC and C strings. 900 if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx, 901 firstDataArg)) 902 return; // Literal format string found, check done! 903 | 798 unsigned ArgIndex = FA->getFormatIdx(); 799 const Expr *Arg = CE->getArg(ArgIndex - 1); 800 801 return SemaCheckStringLiteral(Arg, TheCall, HasVAListArg, 802 format_idx, firstDataArg); 803 } 804 } 805 } --- 110 unchanged lines hidden (view full) --- 916 // Format string can be either ObjC string (e.g. @"%d") or 917 // C string (e.g. "%d") 918 // ObjC string uses the same format specifiers as C string, so we can use 919 // the same format string checking logic for both ObjC and C strings. 920 if (SemaCheckStringLiteral(OrigFormatExpr, TheCall, HasVAListArg, format_idx, 921 firstDataArg)) 922 return; // Literal format string found, check done! 923 |
904 // For vprintf* functions (i.e., HasVAListArg==true), we add a 905 // special check to see if the format string is a function parameter 906 // of the function calling the printf function. If the function 907 // has an attribute indicating it is a printf-like function, then we 908 // should suppress warnings concerning non-literals being used in a call 909 // to a vprintf function. For example: 910 // 911 // void 912 // logmessage(char const *fmt __attribute__ (format (printf, 1, 2)), ...) { 913 // va_list ap; 914 // va_start(ap, fmt); 915 // vprintf(fmt, ap); // Do NOT emit a warning about "fmt". 916 // ... 917 // 918 // 919 // FIXME: We don't have full attribute support yet, so just check to see 920 // if the argument is a DeclRefExpr that references a parameter. We'll 921 // add proper support for checking the attribute later. 922 if (HasVAListArg) 923 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OrigFormatExpr)) 924 if (isa<ParmVarDecl>(DR->getDecl())) 925 return; 926 | |
927 // If there are no arguments specified, warn with -Wformat-security, otherwise 928 // warn only with -Wformat-nonliteral. 929 if (TheCall->getNumArgs() == format_idx+1) 930 Diag(TheCall->getArg(format_idx)->getLocStart(), 931 diag::warn_printf_nonliteral_noargs) 932 << OrigFormatExpr->getSourceRange(); 933 else 934 Diag(TheCall->getArg(format_idx)->getLocStart(), --- 537 unchanged lines hidden --- | 924 // If there are no arguments specified, warn with -Wformat-security, otherwise 925 // warn only with -Wformat-nonliteral. 926 if (TheCall->getNumArgs() == format_idx+1) 927 Diag(TheCall->getArg(format_idx)->getLocStart(), 928 diag::warn_printf_nonliteral_noargs) 929 << OrigFormatExpr->getSourceRange(); 930 else 931 Diag(TheCall->getArg(format_idx)->getLocStart(), --- 537 unchanged lines hidden --- |