]> granicus.if.org Git - clang/commitdiff
In C++, if the user redeclares a builtin function with a type that is
authorDouglas Gregor <dgregor@apple.com>
Tue, 21 Dec 2010 19:47:46 +0000 (19:47 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 21 Dec 2010 19:47:46 +0000 (19:47 +0000)
inconsistent with the type that the builtin *should* have, forget
about the builtin altogether: we don't want subsequence analyses,
CodeGen, etc., to think that we have a proper builtin function.

C is protected from errors here because it allows one to use a
library builtin without having a declaration, and detects inconsistent
(re-)declarations of builtins during declaration merging. C++ was
unprotected, and therefore would crash.

Fixes PR8839.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@122351 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/Builtins.h
lib/Basic/Builtins.cpp
lib/Sema/SemaDecl.cpp
test/CodeGenCXX/builtins.cpp [new file with mode: 0644]

index 76f21c842b33b456a5df1816f750ab5427b76109..4df4c8f95374ca6006f4ce7ee5ecc2f5c6ecadf6 100644 (file)
@@ -117,6 +117,10 @@ public:
     return strchr(GetRecord(ID).Attributes, 'f') != 0;
   }
 
+  /// \brief Completely forget that the given ID was ever considered a builtin,
+  /// e.g., because the user provided a conflicting signature.
+  void ForgetBuiltin(unsigned ID, IdentifierTable &Table);
+  
   /// \brief If this is a library function that comes from a specific
   /// header, retrieve that header name.
   const char *getHeaderName(unsigned ID) const {
index 3eacb3aa6118cf796958a3a9811962251169adf6..9ecb0c0c8b75e4d67f8825f749c32e184586a4be 100644 (file)
@@ -80,6 +80,10 @@ Builtin::Context::GetBuiltinNames(llvm::SmallVectorImpl<const char *> &Names,
       Names.push_back(TSRecords[i].Name);
 }
 
+void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
+  Table.get(GetRecord(ID).Name).setBuiltinID(0);
+}
+
 bool
 Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
                                bool &HasVAListArg) {
index ad0c077aad4e2a893f9d8f3147c8904f132b7df8..bc91eb043968f1ad66f96caa55d7c8811fd3545d 100644 (file)
@@ -4285,6 +4285,18 @@ void Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
     // during delayed parsing anyway.
     if (!CurContext->isRecord())
       CheckCXXDefaultArguments(NewFD);
+    
+    // If this function declares a builtin function, check the type of this
+    // declaration against the expected type for the builtin. 
+    if (unsigned BuiltinID = NewFD->getBuiltinID()) {
+      ASTContext::GetBuiltinTypeError Error;
+      QualType T = Context.GetBuiltinType(BuiltinID, Error);
+      if (!T.isNull() && !Context.hasSameType(T, NewFD->getType())) {
+        // The type of this function differs from the type of the builtin,
+        // so forget about the builtin entirely.
+        Context.BuiltinInfo.ForgetBuiltin(BuiltinID, Context.Idents);
+      }
+    }
   }
 }
 
diff --git a/test/CodeGenCXX/builtins.cpp b/test/CodeGenCXX/builtins.cpp
new file mode 100644 (file)
index 0000000..0629c31
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -emit-llvm -o - %s | FileCheck %s
+
+// PR8839
+extern "C" char memmove();
+
+int main() {
+  // CHECK: call signext i8 @memmove()
+  return memmove();
+}