Deleted Added
sdiff udiff text old ( 195099 ) new ( 195341 )
full compact
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)) {
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);
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);
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 }
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())) {
777 if (const FormatArgAttr *FA = FD->getAttr<FormatArgAttr>(Context)) {
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
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 ---