[llvm-branch-commits] [cfe-branch] r292807 - Merging r292555, r292558 and r292559:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jan 23 09:55:51 PST 2017


Author: hans
Date: Mon Jan 23 11:55:50 2017
New Revision: 292807

URL: http://llvm.org/viewvc/llvm-project?rev=292807&view=rev
Log:
Merging r292555, r292558 and r292559:
------------------------------------------------------------------------
r292555 | rsmith | 2017-01-19 16:45:35 -0800 (Thu, 19 Jan 2017) | 6 lines

P0426: Make the library implementation of constexpr char_traits a little easier
by providing a memchr builtin that returns char* instead of void*.

Also add a __has_feature flag to indicate the presence of constexpr forms of
the relevant <string> functions.

------------------------------------------------------------------------

------------------------------------------------------------------------
r292558 | rsmith | 2017-01-19 16:57:59 -0800 (Thu, 19 Jan 2017) | 2 lines

Add documentation for constexpr string builtin support.

------------------------------------------------------------------------

------------------------------------------------------------------------
r292559 | rsmith | 2017-01-19 17:08:15 -0800 (Thu, 19 Jan 2017) | 2 lines

Fix documentation typo.

------------------------------------------------------------------------

Modified:
    cfe/branches/release_40/   (props changed)
    cfe/branches/release_40/docs/LanguageExtensions.rst
    cfe/branches/release_40/include/clang/Basic/Builtins.def
    cfe/branches/release_40/lib/AST/ExprConstant.cpp
    cfe/branches/release_40/lib/CodeGen/CGBuiltin.cpp
    cfe/branches/release_40/lib/Lex/PPMacroExpansion.cpp
    cfe/branches/release_40/test/CodeGenCXX/builtins.cpp
    cfe/branches/release_40/test/Lexer/has_feature_cxx0x.cpp
    cfe/branches/release_40/test/SemaCXX/constexpr-string.cpp

Propchange: cfe/branches/release_40/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jan 23 11:55:50 2017
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:291850,291853,291865,291871,291877,291879,291881,291907,291955,291964,292032,292052,292183,292247,292265
+/cfe/trunk:291850,291853,291865,291871,291877,291879,291881,291907,291955,291964,292032,292052,292183,292247,292265,292555,292558-292559
 /cfe/trunk/test:170344
 /cfe/trunk/test/SemaTemplate:126920

Modified: cfe/branches/release_40/docs/LanguageExtensions.rst
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/docs/LanguageExtensions.rst?rev=292807&r1=292806&r2=292807&view=diff
==============================================================================
--- cfe/branches/release_40/docs/LanguageExtensions.rst (original)
+++ cfe/branches/release_40/docs/LanguageExtensions.rst Mon Jan 23 11:55:50 2017
@@ -1776,6 +1776,46 @@ numeric primitives such as frexp. See `L
 <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

Modified: cfe/branches/release_40/include/clang/Basic/Builtins.def
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/include/clang/Basic/Builtins.def?rev=292807&r1=292806&r2=292807&view=diff
==============================================================================
--- cfe/branches/release_40/include/clang/Basic/Builtins.def (original)
+++ cfe/branches/release_40/include/clang/Basic/Builtins.def Mon Jan 23 11:55:50 2017
@@ -1339,6 +1339,7 @@ BUILTIN(__builtin_smulll_overflow, "bSLL
 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")

Modified: cfe/branches/release_40/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/lib/AST/ExprConstant.cpp?rev=292807&r1=292806&r2=292807&view=diff
==============================================================================
--- cfe/branches/release_40/lib/AST/ExprConstant.cpp (original)
+++ cfe/branches/release_40/lib/AST/ExprConstant.cpp Mon Jan 23 11:55:50 2017
@@ -5683,6 +5683,7 @@ bool PointerExprEvaluator::VisitBuiltinC
   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 @@ bool PointerExprEvaluator::VisitBuiltinC
       // 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).

Modified: cfe/branches/release_40/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/lib/CodeGen/CGBuiltin.cpp?rev=292807&r1=292806&r2=292807&view=diff
==============================================================================
--- cfe/branches/release_40/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/branches/release_40/lib/CodeGen/CGBuiltin.cpp Mon Jan 23 11:55:50 2017
@@ -1189,6 +1189,10 @@ RValue CodeGenFunction::EmitBuiltinExpr(
     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;

Modified: cfe/branches/release_40/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/lib/Lex/PPMacroExpansion.cpp?rev=292807&r1=292806&r2=292807&view=diff
==============================================================================
--- cfe/branches/release_40/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/branches/release_40/lib/Lex/PPMacroExpansion.cpp Mon Jan 23 11:55:50 2017
@@ -1183,6 +1183,7 @@ static bool HasFeature(const Preprocesso
       .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)

Modified: cfe/branches/release_40/test/CodeGenCXX/builtins.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/test/CodeGenCXX/builtins.cpp?rev=292807&r1=292806&r2=292807&view=diff
==============================================================================
--- cfe/branches/release_40/test/CodeGenCXX/builtins.cpp (original)
+++ cfe/branches/release_40/test/CodeGenCXX/builtins.cpp Mon Jan 23 11:55:50 2017
@@ -26,3 +26,7 @@ int x = __builtin_abs(-2);
 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)

Modified: cfe/branches/release_40/test/Lexer/has_feature_cxx0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/test/Lexer/has_feature_cxx0x.cpp?rev=292807&r1=292806&r2=292807&view=diff
==============================================================================
--- cfe/branches/release_40/test/Lexer/has_feature_cxx0x.cpp (original)
+++ cfe/branches/release_40/test/Lexer/has_feature_cxx0x.cpp Mon Jan 23 11:55:50 2017
@@ -301,6 +301,17 @@ int no_constexpr();
 // 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

Modified: cfe/branches/release_40/test/SemaCXX/constexpr-string.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_40/test/SemaCXX/constexpr-string.cpp?rev=292807&r1=292806&r2=292807&view=diff
==============================================================================
--- cfe/branches/release_40/test/SemaCXX/constexpr-string.cpp (original)
+++ cfe/branches/release_40/test/SemaCXX/constexpr-string.cpp Mon Jan 23 11:55:50 2017
@@ -166,6 +166,27 @@ namespace StrchrEtc {
   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}}
 }




More information about the llvm-branch-commits mailing list