[cfe-commits] r64526 - in /cfe/trunk: include/clang/AST/Builtins.def include/clang/AST/Builtins.h include/clang/Basic/DiagnosticSemaKinds.def lib/AST/Builtins.cpp lib/CodeGen/CodeGenModule.cpp lib/Sema/Sema.cpp lib/Sema/Sema.h lib/Sema/SemaChecking.cpp lib/Sema/SemaDecl.cpp test/Sema/implicit-builtin-decl.c
Douglas Gregor
dgregor at apple.com
Fri Feb 13 17:52:54 PST 2009
Author: dgregor
Date: Fri Feb 13 19:52:53 2009
New Revision: 64526
URL: http://llvm.org/viewvc/llvm-project?rev=64526&view=rev
Log:
Make it possible for builtins to expression FILE* arguments, so that
we can define builtins such as fprintf, vfprintf, and
__builtin___fprintf_chk. Give a nice error message when we need to
implicitly declare a function like fprintf.
Modified:
cfe/trunk/include/clang/AST/Builtins.def
cfe/trunk/include/clang/AST/Builtins.h
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
cfe/trunk/lib/AST/Builtins.cpp
cfe/trunk/lib/CodeGen/CodeGenModule.cpp
cfe/trunk/lib/Sema/Sema.cpp
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaChecking.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/test/Sema/implicit-builtin-decl.c
Modified: cfe/trunk/include/clang/AST/Builtins.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Builtins.def?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Builtins.def (original)
+++ cfe/trunk/include/clang/AST/Builtins.def Fri Feb 13 19:52:53 2009
@@ -35,6 +35,7 @@
// a -> __builtin_va_list
// A -> "reference" to __builtin_va_list
// V -> Vector, following num elements and a base type.
+// P -> FILE
// . -> "...". This may only occur at the end of the function list.
//
// Types maybe prefixed with the following modifiers:
@@ -151,10 +152,10 @@
BUILTIN(__builtin___sprintf_chk, "ic*izcC*.", "Fp:3:")
BUILTIN(__builtin___vsnprintf_chk, "ic*zizcC*a", "FP:4:")
BUILTIN(__builtin___vsprintf_chk, "ic*izcC*a", "FP:3:")
-//BUILTIN(__builtin___fprintf_chk, "i(FIXME:FILEPTR)icC*.", "F") // FIXME: format printf attribute
-BUILTIN(__builtin___printf_chk, "iicC*.", "F") // FIXME: format printf attribute
-//BUILTIN(__builtin___vfprintf_chk, "i(FIXME:FILEPTR)icC*a", "F") // FIXME: format printf attribute
-BUILTIN(__builtin___vprintf_chk, "iicC*a", "F") // FIXME: format printf attribute
+BUILTIN(__builtin___fprintf_chk, "iP*icC*.", "Fp:2:")
+BUILTIN(__builtin___printf_chk, "iicC*.", "Fp:1:")
+BUILTIN(__builtin___vfprintf_chk, "iP*icC*a", "FP:2:")
+BUILTIN(__builtin___vprintf_chk, "iicC*a", "FP:1:")
BUILTIN(__builtin_expect, "iii" , "nc")
BUILTIN(__builtin_prefetch, "vCv*.", "nc")
@@ -199,11 +200,11 @@
BUILTIN(strspn, "zcC*cC*", "f:string.h:")
BUILTIN(strstr, "c*cC*cC*", "f:string.h:")
BUILTIN(printf, "icC*.", "f:stdio.h:p:0:")
-//BUILTIN(fprintf, "i<FIXME:FILEPTR>cC*.", "f:stdio.h:p:1:")
+BUILTIN(fprintf, "iP*cC*.", "f:stdio.h:p:1:")
BUILTIN(snprintf, "ic*zcC*.", "f:stdio.h:p:2:")
BUILTIN(sprintf, "ic*cC*.", "f:stdio.h:p:1:")
BUILTIN(vprintf, "icC*a", "f:stdio.h:P:0:")
-//BUILTIN(vfprintf, "i<FIXME:FILEPTR>cC*a", "f:stdio.h:P:1:")
+BUILTIN(vfprintf, "iP*cC*a", "f:stdio.h:P:1:")
BUILTIN(vsnprintf, "ic*zcC*a", "f:stdio.h:P:2:")
BUILTIN(vsprintf, "ic*cC*a", "f:stdio.h:P:1:")
Modified: cfe/trunk/include/clang/AST/Builtins.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Builtins.h?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Builtins.h (original)
+++ cfe/trunk/include/clang/AST/Builtins.h Fri Feb 13 19:52:53 2009
@@ -102,7 +102,12 @@
}
/// GetBuiltinType - Return the type for the specified builtin.
- QualType GetBuiltinType(unsigned ID, ASTContext &Context) const;
+ enum GetBuiltinTypeError {
+ GE_None, //< No error
+ GE_Missing_FILE //< Missing the FILE type from <stdio.h>
+ };
+ QualType GetBuiltinType(unsigned ID, ASTContext &Context,
+ GetBuiltinTypeError &Error) const;
private:
const Info &GetRecord(unsigned ID) const;
};
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def Fri Feb 13 19:52:53 2009
@@ -91,6 +91,8 @@
"please include the header <%0> or explicitly provide a declaration for '%1'")
DIAG(note_previous_builtin_declaration, NOTE,
"%0 was implicitly declared here with type %1")
+DIAG(err_implicit_decl_requires_stdio, ERROR,
+ "implicit declaration of '%0' requires inclusion of the header <stdio.h>")
/// parser diagnostics
DIAG(ext_typedef_without_a_name, EXTWARN,
Modified: cfe/trunk/lib/AST/Builtins.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Builtins.cpp?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Builtins.cpp (original)
+++ cfe/trunk/lib/AST/Builtins.cpp Fri Feb 13 19:52:53 2009
@@ -87,6 +87,7 @@
/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
/// pointer over the consumed characters. This returns the resultant type.
static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
+ Builtin::Context::GetBuiltinTypeError &Error,
bool AllowTypeModifiers = true) {
// Modifiers.
bool Long = false, LongLong = false, Signed = false, Unsigned = false;
@@ -202,10 +203,23 @@
Str = End;
- QualType ElementType = DecodeTypeFromStr(Str, Context, false);
+ QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
Type = Context.getVectorType(ElementType, NumElements);
break;
}
+ case 'P': {
+ IdentifierInfo *II = &Context.Idents.get("FILE");
+ DeclContext::lookup_result Lookup
+ = Context.getTranslationUnitDecl()->lookup(II);
+ if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) {
+ Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first));
+ break;
+ }
+ else {
+ Error = Builtin::Context::GE_Missing_FILE;
+ return QualType();
+ }
+ }
}
if (!AllowTypeModifiers)
@@ -231,16 +245,21 @@
}
/// GetBuiltinType - Return the type for the specified builtin.
-QualType Builtin::Context::GetBuiltinType(unsigned id,
- ASTContext &Context) const {
+QualType Builtin::Context::GetBuiltinType(unsigned id, ASTContext &Context,
+ GetBuiltinTypeError &Error) const {
const char *TypeStr = GetRecord(id).Type;
llvm::SmallVector<QualType, 8> ArgTypes;
- QualType ResType = DecodeTypeFromStr(TypeStr, Context);
+ Error = GE_None;
+ QualType ResType = DecodeTypeFromStr(TypeStr, Context, Error);
+ if (Error != GE_None)
+ return QualType();
while (TypeStr[0] && TypeStr[0] != '.') {
- QualType Ty = DecodeTypeFromStr(TypeStr, Context);
-
+ QualType Ty = DecodeTypeFromStr(TypeStr, Context, Error);
+ if (Error != GE_None)
+ return QualType();
+
// Do array -> pointer decay. The builtin should use the decayed type.
if (Ty->isArrayType())
Ty = Context.getArrayDecayedType(Ty);
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Fri Feb 13 19:52:53 2009
@@ -852,7 +852,10 @@
Name += 10;
// Get the type for the builtin.
- QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context);
+ Builtin::Context::GetBuiltinTypeError Error;
+ QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error);
+ assert(Error == Builtin::Context::GE_None && "Can't get builtin type");
+
const llvm::FunctionType *Ty =
cast<llvm::FunctionType>(getTypes().ConvertType(Type));
Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Fri Feb 13 19:52:53 2009
@@ -133,9 +133,7 @@
KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
- KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
- KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
StdNamespace = 0;
TUScope = 0;
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Fri Feb 13 19:52:53 2009
@@ -182,9 +182,7 @@
enum {
id_NSLog,
id_asprintf,
- id_fprintf,
id_vasprintf,
- id_vfprintf,
id_num_known_functions
};
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Feb 13 19:52:53 2009
@@ -87,12 +87,10 @@
} else if (FnInfo == KnownFunctionIDs[id_NSLog]) {
format_idx = 0;
HasVAListArg = false;
- } else if (FnInfo == KnownFunctionIDs[id_asprintf] ||
- FnInfo == KnownFunctionIDs[id_fprintf]) {
+ } else if (FnInfo == KnownFunctionIDs[id_asprintf]) {
format_idx = 1;
HasVAListArg = false;
- } else if (FnInfo == KnownFunctionIDs[id_vasprintf] ||
- FnInfo == KnownFunctionIDs[id_vfprintf]) {
+ } else if (FnInfo == KnownFunctionIDs[id_vasprintf]) {
format_idx = 1;
HasVAListArg = true;
} else {
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Feb 13 19:52:53 2009
@@ -298,7 +298,19 @@
if (Context.BuiltinInfo.hasVAListUse(BID))
InitBuiltinVaListType();
- QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context);
+ Builtin::Context::GetBuiltinTypeError Error;
+ QualType R = Context.BuiltinInfo.GetBuiltinType(BID, Context, Error);
+ switch (Error) {
+ case Builtin::Context::GE_None:
+ // Okay
+ break;
+
+ case Builtin::Context::GE_Missing_FILE:
+ if (ForRedeclaration)
+ Diag(Loc, diag::err_implicit_decl_requires_stdio)
+ << Context.BuiltinInfo.GetName(BID);
+ return 0;
+ }
if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(BID)) {
Diag(Loc, diag::ext_implicit_lib_function_decl)
Modified: cfe/trunk/test/Sema/implicit-builtin-decl.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/implicit-builtin-decl.c?rev=64526&r1=64525&r2=64526&view=diff
==============================================================================
--- cfe/trunk/test/Sema/implicit-builtin-decl.c (original)
+++ cfe/trunk/test/Sema/implicit-builtin-decl.c Fri Feb 13 19:52:53 2009
@@ -20,3 +20,7 @@
int strcpy(int); // expected-error{{conflicting types for 'strcpy'}} \
// expected-note{{'strcpy' was implicitly declared here with type 'char *(char *, char const *)'}}
}
+
+void f2() {
+ fprintf(0, "foo"); // expected-error{{implicit declaration of 'fprintf' requires inclusion of the header <stdio.h>}}
+}
More information about the cfe-commits
mailing list