aboutsummaryrefslogtreecommitdiff
path: root/test/Sema/implicit-builtin-decl.c
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2009-02-13 23:20:09 +0000
committerDouglas Gregor <dgregor@apple.com>2009-02-13 23:20:09 +0000
commit3e41d60eb627dc227c770f1c1c87d06909cf05fd (patch)
tree6a67a1894695804429eb570219ae55e7e2b68555 /test/Sema/implicit-builtin-decl.c
parent67116083e70ad44799808c7472d9cd9e7e21d4d5 (diff)
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we produce the correct signature for these functions, which has two practical impacts: 1) When we're supporting the "implicit function declaration" feature of C99, these functions will be implicitly declared with the right signature rather than as a function returning "int" with no prototype. See PR3541 for the reason why this is important (hint: GCC always predeclares these functions). 2) If users attempt to redeclare one of these library functions with an incompatible signature, we produce a hard error. This patch does a little bit of work to give reasonable error messages. For example, when we hit case #1 we complain that we're implicitly declaring this function with a specific signature, and then we give a note that asks the user to include the appropriate header (e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In case #2, we show the type of the implicit builtin that was incorrectly declared, so the user can see the problem. We could do better here: for example, when displaying this latter error message we say something like: 'strcpy' was implicitly declared here with type 'char *(char *, char const *)' but we should really print out a fake code line showing the declaration, like this: 'strcpy' was implicitly declared here as: char *strcpy(char *, char const *) This would also be good for printing built-in candidates with C++ operator overloading. The set of C library functions supported by this patch includes all functions from the C99 specification's <stdlib.h> and <string.h> that (a) are predefined by GCC and (b) have signatures that could cause codegen issues if they are treated as functions with no prototype returning and int. Future work could extend this set of functions to other C library functions that we know about. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64504 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Sema/implicit-builtin-decl.c')
-rw-r--r--test/Sema/implicit-builtin-decl.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/test/Sema/implicit-builtin-decl.c b/test/Sema/implicit-builtin-decl.c
new file mode 100644
index 0000000000..3faef0b719
--- /dev/null
+++ b/test/Sema/implicit-builtin-decl.c
@@ -0,0 +1,22 @@
+// RUN: clang -fsyntax-only -verify %s
+void f() {
+ int *ptr = malloc(sizeof(int) * 10); // expected-warning{{implicitly declaring C library function 'malloc' with type}} \
+ // expected-note{{please include the header <stdlib.h> or explicitly provide a declaration for 'malloc'}} \
+ // expected-note{{'malloc' was implicitly declared here with type 'void *}}
+}
+
+void *alloca(__SIZE_TYPE__); // redeclaration okay
+
+int *calloc(__SIZE_TYPE__, __SIZE_TYPE__); // expected-error{{conflicting types for 'calloc'}} \
+ // expected-note{{'calloc' was implicitly declared here with type 'void *}}
+
+
+void g(int malloc) { // okay: these aren't functions
+ int calloc = 1;
+}
+
+void h() {
+ int malloc(int); // expected-error{{conflicting types for 'malloc'}}
+ int strcpy(int); // expected-error{{conflicting types for 'strcpy'}} \
+ // expected-note{{'strcpy' was implicitly declared here with type 'char *(char *, char const *)'}}
+}