Bug 59717 - better warning when using functions without including appropriate header files
Summary: better warning when using functions without including appropriate header files
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.8.2
: P3 enhancement
Target Milestone: 5.0
Assignee: Marek Polacek
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2014-01-08 07:27 UTC by Rahul Sundaram
Modified: 2014-10-07 17:50 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-01-25 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rahul Sundaram 2014-01-08 07:27:13 UTC
Clang produces a better warning suggesting the appropriate header file compared to GCC.  Test program included

$gcc testwarning.c 

testwarning.c: In function ‘main’:
testwarning.c:4:2: warning: incompatible implicit declaration of built-in function ‘strcpy’ [enabled by default]
  strcpy(teststr, "foo");
  ^

rahul@mether ~/work> clang test

testfor  testfor.c  testwarning.c  
rahul@mether ~/work> clang testwarning.c 
testwarning.c:4:2: warning: implicitly declaring library function 'strcpy' with
      type 'char *(char *, const char *)'
        strcpy(teststr, "foo");
        ^
testwarning.c:4:2: note: please include the header <string.h> or explicitly
      provide a declaration for 'strcpy'

---

cat testwarning.c 
int main (void) {

	char *teststr;
	strcpy(teststr, "foo");
---
Comment 1 Steven Bosscher 2014-01-25 13:12:34 UTC
Can be implemented by adding header names in the builtins.def files.
Comment 2 Marek Polacek 2014-10-06 16:37:00 UTC
On it.
Comment 3 Marek Polacek 2014-10-07 17:50:17 UTC
Author: mpolacek
Date: Tue Oct  7 17:49:46 2014
New Revision: 215979

URL: https://gcc.gnu.org/viewcvs?rev=215979&root=gcc&view=rev
Log:
	PR c/59717
	* c-decl.c (header_for_builtin_fn): New function.
	(implicitly_declare): Suggest which header to include.

	* gcc.dg/pr59717.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr59717.c
Modified:
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-decl.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Marek Polacek 2014-10-07 17:50:35 UTC
Fixed.