Merge remote-tracking branch 'origin/swift-4.0-branch' into stable
* origin/swift-4.0-branch:
Merging r292847: ------------------------------------------------------------------------ r292847 | rsmith | 2017-01-23 15:14:23 -0800 (Mon, 23 Jan 2017) | 2 lines
Merging r292800: ------------------------------------------------------------------------ r292800 | dergachev | 2017-01-23 08:57:11 -0800 (Mon, 23 Jan 2017) | 13 lines
Merging r292497: ------------------------------------------------------------------------ r292497 | arphaman | 2017-01-19 09:17:57 -0800 (Thu, 19 Jan 2017) | 6 lines
Merging r292555, r292558 and r292559: ------------------------------------------------------------------------ r292555 | rsmith | 2017-01-19 16:45:35 -0800 (Thu, 19 Jan 2017) | 6 lines
diff --git a/docs/LanguageExtensions.rst b/docs/LanguageExtensions.rst
index 64e6ffb..885ad57 100644
--- a/docs/LanguageExtensions.rst
+++ b/docs/LanguageExtensions.rst
@@ -1776,6 +1776,46 @@
<http://llvm.org/docs/LangRef.html#llvm-canonicalize-intrinsic>`_ for
more information on the semantics.
+String builtins
+---------------
+
+Clang provides constant expression evaluation support for builtins forms of
+the following functions from the C standard library ``<strings.h>`` header:
+
+* ``memchr``
+* ``memcmp``
+* ``strchr``
+* ``strcmp``
+* ``strlen``
+* ``strncmp``
+* ``wcschr``
+* ``wcscmp``
+* ``wcslen``
+* ``wcsncmp``
+* ``wmemchr``
+* ``wmemcmp``
+
+In each case, the builtin form has the name of the C library function prefixed
+by ``__builtin_``. Example:
+
+.. code-block:: c
+
+ void *p = __builtin_memchr("foobar", 'b', 5);
+
+In addition to the above, one further builtin is provided:
+
+.. code-block:: c
+
+ char *__builtin_char_memchr(const char *haystack, int needle, size_t size);
+
+``__builtin_char_memchr(a, b, c)`` is identical to
+``(char*)__builtin_memchr(a, b, c)`` except that its use is permitted within
+constant expressions in C++11 onwards (where a cast from ``void*`` to ``char*``
+is disallowed in general).
+
+Support for constant expression evaluation for the above builtins be detected
+with ``__has_feature(cxx_constexpr_string_builtins)``.
+
.. _langext-__c11_atomic:
__c11_atomic builtins
diff --git a/include/clang/Basic/Builtins.def b/include/clang/Basic/Builtins.def
index ec0a279..326a8fa 100644
--- a/include/clang/Basic/Builtins.def
+++ b/include/clang/Basic/Builtins.def
@@ -1339,6 +1339,7 @@
BUILTIN(__builtin_addressof, "v*v&", "nct")
BUILTIN(__builtin_operator_new, "v*z", "c")
BUILTIN(__builtin_operator_delete, "vv*", "n")
+BUILTIN(__builtin_char_memchr, "c*cC*iz", "n")
// Safestack builtins
BUILTIN(__builtin___get_unsafe_stack_start, "v*", "Fn")
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index a8512b2..5fab58a 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -5683,6 +5683,7 @@
case Builtin::BI__builtin_strchr:
case Builtin::BI__builtin_wcschr:
case Builtin::BI__builtin_memchr:
+ case Builtin::BI__builtin_char_memchr:
case Builtin::BI__builtin_wmemchr: {
if (!Visit(E->getArg(0)))
return false;
@@ -5720,6 +5721,7 @@
// Fall through.
case Builtin::BImemchr:
case Builtin::BI__builtin_memchr:
+ case Builtin::BI__builtin_char_memchr:
// memchr compares by converting both sides to unsigned char. That's also
// correct for strchr if we get this far (to cope with plain char being
// unsigned in the strchr case).
diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp
index 2ede1d4..b3d02f1 100644
--- a/lib/CodeGen/CGBuiltin.cpp
+++ b/lib/CodeGen/CGBuiltin.cpp
@@ -1189,6 +1189,10 @@
return RValue::get(Dest.getPointer());
}
+ case Builtin::BI__builtin_char_memchr:
+ BuiltinID = Builtin::BI__builtin_memchr;
+ break;
+
case Builtin::BI__builtin___memcpy_chk: {
// fold __builtin_memcpy_chk(x, y, cst1, cst2) to memcpy iff cst1<=cst2.
llvm::APSInt Size, DstSize;
diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp
index 9c84ffe..822ac8f 100644
--- a/lib/Lex/PPMacroExpansion.cpp
+++ b/lib/Lex/PPMacroExpansion.cpp
@@ -1185,6 +1185,7 @@
.Case("cxx_attributes", LangOpts.CPlusPlus11)
.Case("cxx_auto_type", LangOpts.CPlusPlus11)
.Case("cxx_constexpr", LangOpts.CPlusPlus11)
+ .Case("cxx_constexpr_string_builtins", LangOpts.CPlusPlus11)
.Case("cxx_decltype", LangOpts.CPlusPlus11)
.Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
.Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index c9053e8..b1bec1c 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -12386,9 +12386,9 @@
Diag(Loc, diag::err_in_class_initializer_not_yet_parsed)
<< OutermostClass << Field;
Diag(Field->getLocEnd(), diag::note_in_class_initializer_not_yet_parsed);
-
- // Don't diagnose this again.
- Field->setInvalidDecl();
+ // Recover by marking the field invalid, unless we're in a SFINAE context.
+ if (!isSFINAEContext())
+ Field->setInvalidDecl();
return ExprError();
}
diff --git a/lib/StaticAnalyzer/Core/MemRegion.cpp b/lib/StaticAnalyzer/Core/MemRegion.cpp
index c4ba2ae..54774d7 100644
--- a/lib/StaticAnalyzer/Core/MemRegion.cpp
+++ b/lib/StaticAnalyzer/Core/MemRegion.cpp
@@ -776,6 +776,22 @@
return (const StackFrameContext *)nullptr;
}
+static CanQualType getBlockPointerType(const BlockDecl *BD, ASTContext &C) {
+ // FIXME: The fallback type here is totally bogus -- though it should
+ // never be queried, it will prevent uniquing with the real
+ // BlockCodeRegion. Ideally we'd fix the AST so that we always had a
+ // signature.
+ QualType T;
+ if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten())
+ T = TSI->getType();
+ if (T.isNull())
+ T = C.VoidTy;
+ if (!T->getAs<FunctionType>())
+ T = C.getFunctionNoProtoType(T);
+ T = C.getBlockPointerType(T);
+ return C.getCanonicalType(T);
+}
+
const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
const LocationContext *LC) {
const MemRegion *sReg = nullptr;
@@ -803,7 +819,7 @@
sReg = getGlobalsRegion();
}
- // Finally handle static locals.
+ // Finally handle locals.
} else {
// FIXME: Once we implement scope handling, we will need to properly lookup
// 'D' to the proper LocationContext.
@@ -816,9 +832,22 @@
const StackFrameContext *STC = V.get<const StackFrameContext*>();
- if (!STC)
- sReg = getUnknownRegion();
- else {
+ if (!STC) {
+ if (D->isStaticLocal()) {
+ const CodeTextRegion *fReg = nullptr;
+ if (const auto *ND = dyn_cast<NamedDecl>(DC))
+ fReg = getFunctionCodeRegion(ND);
+ else if (const auto *BD = dyn_cast<BlockDecl>(DC))
+ fReg = getBlockCodeRegion(BD, getBlockPointerType(BD, getContext()),
+ LC->getAnalysisDeclContext());
+ assert(fReg && "Unable to determine code region for a static local!");
+ sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind, fReg);
+ } else {
+ // We're looking at a block-captured local variable, which may be either
+ // still local, or already moved to the heap. So we're not sure.
+ sReg = getUnknownRegion();
+ }
+ } else {
if (D->hasLocalStorage()) {
sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
@@ -831,22 +860,9 @@
sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
getFunctionCodeRegion(cast<NamedDecl>(STCD)));
else if (const BlockDecl *BD = dyn_cast<BlockDecl>(STCD)) {
- // FIXME: The fallback type here is totally bogus -- though it should
- // never be queried, it will prevent uniquing with the real
- // BlockCodeRegion. Ideally we'd fix the AST so that we always had a
- // signature.
- QualType T;
- if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten())
- T = TSI->getType();
- if (T.isNull())
- T = getContext().VoidTy;
- if (!T->getAs<FunctionType>())
- T = getContext().getFunctionNoProtoType(T);
- T = getContext().getBlockPointerType(T);
-
const BlockCodeRegion *BTR =
- getBlockCodeRegion(BD, C.getCanonicalType(T),
- STC->getAnalysisDeclContext());
+ getBlockCodeRegion(BD, getBlockPointerType(BD, getContext()),
+ STC->getAnalysisDeclContext());
sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
BTR);
}
diff --git a/test/Analysis/dispatch-once.m b/test/Analysis/dispatch-once.m
index 7d54147..2f82718 100644
--- a/test/Analysis/dispatch-once.m
+++ b/test/Analysis/dispatch-once.m
@@ -107,3 +107,10 @@
};
dispatch_once(&once, ^{}); // expected-warning{{Call to 'dispatch_once' uses the block variable 'once' for the predicate value.}}
}
+
+void test_static_var_from_outside_block() {
+ static dispatch_once_t once;
+ ^{
+ dispatch_once(&once, ^{}); // no-warning
+ };
+}
diff --git a/test/CodeGenCXX/builtins.cpp b/test/CodeGenCXX/builtins.cpp
index 98e2d1a..a49deea 100644
--- a/test/CodeGenCXX/builtins.cpp
+++ b/test/CodeGenCXX/builtins.cpp
@@ -26,3 +26,7 @@
long y = __builtin_abs(-2l);
// CHECK: [[Y:%.+]] = call i64 @_Z13__builtin_absl(i64 -2)
// CHECK: store i64 [[Y]], i64* @y, align 8
+
+extern const char char_memchr_arg[32];
+char *memchr_result = __builtin_char_memchr(char_memchr_arg, 123, 32);
+// CHECK: call i8* @memchr(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @char_memchr_arg, i32 0, i32 0), i32 123, i64 32)
diff --git a/test/Lexer/has_feature_cxx0x.cpp b/test/Lexer/has_feature_cxx0x.cpp
index 8c7ff18..9082ca8 100644
--- a/test/Lexer/has_feature_cxx0x.cpp
+++ b/test/Lexer/has_feature_cxx0x.cpp
@@ -301,6 +301,17 @@
// CHECK-11: has_constexpr
// CHECK-NO-11: no_constexpr
+#if __has_feature(cxx_constexpr_string_builtins)
+int has_constexpr_string_builtins();
+#else
+int no_constexpr_string_builtins();
+#endif
+
+// CHECK-1Z: has_constexpr_string_builtins
+// CHECK-14: has_constexpr_string_builtins
+// CHECK-11: has_constexpr_string_builtins
+// CHECK-NO-11: no_constexpr_string_builtins
+
#if __has_feature(cxx_generalized_initializers)
int has_generalized_initializers();
#else
diff --git a/test/SemaCXX/constexpr-string.cpp b/test/SemaCXX/constexpr-string.cpp
index 944038b..fba05e5 100644
--- a/test/SemaCXX/constexpr-string.cpp
+++ b/test/SemaCXX/constexpr-string.cpp
@@ -166,6 +166,27 @@
static_assert(__builtin_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
static_assert(__builtin_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this?
+ static_assert(__builtin_char_memchr(kStr, 'a', 0) == nullptr);
+ static_assert(__builtin_char_memchr(kStr, 'a', 1) == kStr);
+ static_assert(__builtin_char_memchr(kStr, '\0', 5) == nullptr);
+ static_assert(__builtin_char_memchr(kStr, '\0', 6) == kStr + 5);
+ static_assert(__builtin_char_memchr(kStr, '\xff', 8) == kStr + 4);
+ static_assert(__builtin_char_memchr(kStr, '\xff' + 256, 8) == kStr + 4);
+ static_assert(__builtin_char_memchr(kStr, '\xff' - 256, 8) == kStr + 4);
+ static_assert(__builtin_char_memchr(kFoo, 'x', 3) == nullptr);
+ static_assert(__builtin_char_memchr(kFoo, 'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
+ static_assert(__builtin_char_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
+ static_assert(__builtin_char_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this?
+
+ static_assert(*__builtin_char_memchr(kStr, '\xff', 8) == '\xff');
+ constexpr bool char_memchr_mutable() {
+ char buffer[] = "mutable";
+ *__builtin_char_memchr(buffer, 't', 8) = 'r';
+ *__builtin_char_memchr(buffer, 'm', 8) = 'd';
+ return __builtin_strcmp(buffer, "durable") == 0;
+ }
+ static_assert(char_memchr_mutable());
+
constexpr bool a = !strchr("hello", 'h'); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strchr' cannot be used in a constant expression}}
constexpr bool b = !memchr("hello", 'h', 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memchr' cannot be used in a constant expression}}
}
diff --git a/test/SemaCXX/cxx11-default-member-initializers.cpp b/test/SemaCXX/cxx11-default-member-initializers.cpp
new file mode 100644
index 0000000..9353e63
--- /dev/null
+++ b/test/SemaCXX/cxx11-default-member-initializers.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -std=c++11 -verify %s -pedantic
+
+namespace PR31692 {
+ struct A {
+ struct X { int n = 0; } x;
+ // Trigger construction of X() from a SFINAE context. This must not mark
+ // any part of X as invalid.
+ static_assert(!__is_constructible(X), "");
+ // Check that X::n is not marked invalid.
+ double &r = x.n; // expected-error {{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
+ };
+ // A::X can now be default-constructed.
+ static_assert(__is_constructible(A::X), "");
+}