From b68e39930d06ed81a2b431dc09e4cb97e5c0d57a Mon Sep 17 00:00:00 2001
From: Douglas Gregor <dgregor@apple.com>
Date: Tue, 21 Dec 2010 19:47:46 +0000
Subject: [PATCH] In C++, if the user redeclares a builtin function with a type
 that is 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 |  4 ++++
 lib/Basic/Builtins.cpp         |  4 ++++
 lib/Sema/SemaDecl.cpp          | 12 ++++++++++++
 test/CodeGenCXX/builtins.cpp   |  9 +++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 test/CodeGenCXX/builtins.cpp

diff --git a/include/clang/Basic/Builtins.h b/include/clang/Basic/Builtins.h
index 76f21c842b..4df4c8f953 100644
--- a/include/clang/Basic/Builtins.h
+++ b/include/clang/Basic/Builtins.h
@@ -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 {
diff --git a/lib/Basic/Builtins.cpp b/lib/Basic/Builtins.cpp
index 3eacb3aa61..9ecb0c0c8b 100644
--- a/lib/Basic/Builtins.cpp
+++ b/lib/Basic/Builtins.cpp
@@ -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) {
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index ad0c077aad..bc91eb0439 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -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
index 0000000000..0629c31015
--- /dev/null
+++ b/test/CodeGenCXX/builtins.cpp
@@ -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();
+}
-- 
2.40.0