From bfe022caa1c522824b4cf9cdda0865cc0dae187d Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Mon, 3 Jan 2011 09:37:44 +0000 Subject: [PATCH] When we attempt to create a built-in that involves a library type we don't have access to (e.g., fprintf, which needs the library type FILE), fail with a warning and forget about the builtin entirely. Previously, we would actually provide an error, which breaks autoconf's super-lame checks for fprintf, longjmp, etc. Fixes PR8316. llvm-svn: 122744 --- .../clang/Basic/DiagnosticSemaKinds.td | 10 ++++++---- clang/lib/Sema/SemaDecl.cpp | 4 ++-- clang/lib/Sema/SemaLookup.cpp | 19 ++++++++++++++----- clang/test/Sema/implicit-builtin-decl.c | 5 ++++- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 49c9c4b6b700..f2d3c0085cd5 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -223,10 +223,12 @@ def note_please_include_header : Note< "please include the header <%0> or explicitly provide a " "declaration for '%1'">; def note_previous_builtin_declaration : Note<"%0 is a builtin with type %1">; -def err_implicit_decl_requires_stdio : Error< - "implicit declaration of '%0' requires inclusion of the header ">; -def err_implicit_decl_requires_setjmp : Error< - "implicit declaration of '%0' requires inclusion of the header ">; +def warn_implicit_decl_requires_stdio : Warning< + "declaration of built-in function '%0' requires inclusion of the header " + "">; +def warn_implicit_decl_requires_setjmp : Warning< + "declaration of built-in function '%0' requires inclusion of the header " + "">; def warn_redecl_library_builtin : Warning< "incompatible redeclaration of library function %0">; def err_builtin_definition : Error<"definition of builtin function %0">; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index abd63198d47b..32791f9f9838 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -793,13 +793,13 @@ NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid, case ASTContext::GE_Missing_stdio: if (ForRedeclaration) - Diag(Loc, diag::err_implicit_decl_requires_stdio) + Diag(Loc, diag::warn_implicit_decl_requires_stdio) << Context.BuiltinInfo.GetName(BID); return 0; case ASTContext::GE_Missing_setjmp: if (ForRedeclaration) - Diag(Loc, diag::err_implicit_decl_requires_setjmp) + Diag(Loc, diag::warn_implicit_decl_requires_setjmp) << Context.BuiltinInfo.GetName(BID); return 0; } diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index 6972536e8c28..5ed973b1a800 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -483,12 +483,21 @@ static bool LookupBuiltin(Sema &S, LookupResult &R) { S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) return false; - NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, - S.TUScope, R.isForRedeclaration(), - R.getNameLoc()); - if (D) + if (NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II, + BuiltinID, S.TUScope, + R.isForRedeclaration(), + R.getNameLoc())) { R.addDecl(D); - return (D != NULL); + return true; + } + + if (R.isForRedeclaration()) { + // If we're redeclaring this function anyway, forget that + // this was a builtin at all. + S.Context.BuiltinInfo.ForgetBuiltin(BuiltinID, S.Context.Idents); + } + + return false; } } } diff --git a/clang/test/Sema/implicit-builtin-decl.c b/clang/test/Sema/implicit-builtin-decl.c index 9e01de1c8598..d80414d5fea0 100644 --- a/clang/test/Sema/implicit-builtin-decl.c +++ b/clang/test/Sema/implicit-builtin-decl.c @@ -22,7 +22,7 @@ void h() { } void f2() { - fprintf(0, "foo"); // expected-error{{implicit declaration of 'fprintf' requires inclusion of the header }} \ + fprintf(0, "foo"); // expected-warning{{declaration of built-in function 'fprintf' requires inclusion of the header }} \ expected-warning {{implicit declaration of function 'fprintf' is invalid in C99}} } @@ -52,3 +52,6 @@ main(int argc, char *argv[]) } void snprintf() { } + +// PR8316 +void longjmp(); // expected-warning{{declaration of built-in function 'longjmp' requires inclusion of the header }}