[Sema] Avoid sending a dependent expression to the constant evaluator.
authorErik Pilkington <erik.pilkington@gmail.com>
Fri, 29 Mar 2019 19:53:41 +0000 (19:53 +0000)
committerErik Pilkington <erik.pilkington@gmail.com>
Fri, 29 Mar 2019 19:53:41 +0000 (19:53 +0000)
Fixes llvm.org/PR41286

llvm-svn: 357304

clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/warn-fortify-source.c

index 024d78b..5a9b3c3 100644 (file)
@@ -307,6 +307,9 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
   //  - Analyze the format string of sprintf to see how much of buffer is used.
   //  - Evaluate strlen of strcpy arguments, use as object size.
 
+  if (TheCall->isValueDependent() || TheCall->isTypeDependent())
+    return;
+
   unsigned BuiltinID = FD->getBuiltinID(/*ConsiderWrappers=*/true);
   if (!BuiltinID)
     return;
index 3cd939a..d9c21c0 100644 (file)
@@ -1,9 +1,16 @@
 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify
 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_PASS_OBJECT_SIZE
 // RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS
+// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify
+// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_PASS_OBJECT_SIZE
+// RUN: %clang_cc1 -xc++ -triple x86_64-apple-macosx10.14.0 %s -verify -DUSE_BUILTINS
 
 typedef unsigned long size_t;
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #if defined(USE_PASS_OBJECT_SIZE)
 void *memcpy(void *dst, const void *src, size_t c);
 static void *memcpy(void *dst __attribute__((pass_object_size(1))), const void *src, size_t c) __attribute__((overloadable)) __asm__("merp");
@@ -16,6 +23,10 @@ static void *memcpy(void *const dst __attribute__((pass_object_size(1))), const
 void *memcpy(void *dst, const void *src, size_t c);
 #endif
 
+#ifdef __cplusplus
+}
+#endif
+
 void call_memcpy() {
   char dst[10];
   char src[20];
@@ -84,3 +95,25 @@ void call_vsnprintf() {
   __builtin_vsnprintf(buf, 10, "merp", list);
   __builtin_vsnprintf(buf, 11, "merp", list); // expected-warning {{'vsnprintf' size argument is too large; destination buffer has size 10, but size argument is 11}}
 }
+
+#ifdef __cplusplus
+template <class> struct S {
+  void mf() const {
+    __builtin_memset(const_cast<char *>(mv), 0, 0);
+  }
+
+  char mv[10];
+};
+
+template <int A, int B>
+void call_memcpy_dep() {
+  char bufferA[A];
+  char bufferB[B];
+  memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}}
+}
+
+void call_call_memcpy() {
+  call_memcpy_dep<10, 9>();
+  call_memcpy_dep<9, 10>(); // expected-note {{in instantiation of function template specialization 'call_memcpy_dep<9, 10>' requested here}}
+}
+#endif