tbaeder created this revision.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Including a fix for the ambiguous APInt constructor.

Precommit CI should work on this now.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136941

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/ByteCodeExprGen.h
  clang/test/AST/Interp/literals.cpp
  clang/test/Lexer/char-escapes.c

Index: clang/test/Lexer/char-escapes.c
===================================================================
--- clang/test/Lexer/char-escapes.c
+++ clang/test/Lexer/char-escapes.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
+// RUN: %clang_cc1 -fsyntax-only -pedantic -fexperimental-new-constant-interpreter -verify %s
 
 int test['\\' == 92 ? 1 : -1];
 int test['\"' == 34 ? 1 : -1];
Index: clang/test/AST/Interp/literals.cpp
===================================================================
--- clang/test/AST/Interp/literals.cpp
+++ clang/test/AST/Interp/literals.cpp
@@ -278,3 +278,46 @@
   static_assert((0 | gimme(12)) == 12, "");
   static_assert((12 | true) == 13, "");
 };
+
+namespace strings {
+  constexpr const char *S = "abc";
+  static_assert(S[0] == 97, "");
+  static_assert(S[1] == 98, "");
+  static_assert(S[2] == 99, "");
+  static_assert(S[3] == 0, "");
+
+  static_assert("foobar"[2] == 'o', "");
+  static_assert(2["foobar"] == 'o', "");
+
+  constexpr const wchar_t *wide = L"bar";
+  static_assert(wide[0] == L'b', "");
+
+  constexpr const char32_t *u32 = U"abc";
+  static_assert(u32[1] == U'b', "");
+
+  constexpr char32_t c = U'\U0001F60E';
+  static_assert(c == 0x0001F60EL, "");
+
+  constexpr char k = -1;
+  static_assert(k == -1, "");
+
+  static_assert('\N{LATIN CAPITAL LETTER E}' == 'E', "");
+  static_assert('\t' == 9, "");
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmultichar"
+  constexpr int mc = 'abc';
+  static_assert(mc == 'abc', "");
+  __WCHAR_TYPE__ wm = L'abc'; // ref-error{{wide character literals may not contain multiple characters}} \
+                              // expected-error{{wide character literals may not contain multiple characters}}
+  __WCHAR_TYPE__ wu = u'abc'; // ref-error{{Unicode character literals may not contain multiple characters}} \
+                              // expected-error{{Unicode character literals may not contain multiple characters}}
+  __WCHAR_TYPE__ wU = U'abc'; // ref-error{{Unicode character literals may not contain multiple characters}} \
+                              // expected-error{{Unicode character literals may not contain multiple characters}}
+#if __cplusplus > 201103L
+  __WCHAR_TYPE__ wu8 = u8'abc'; // ref-error{{Unicode character literals may not contain multiple characters}} \
+                                // expected-error{{Unicode character literals may not contain multiple characters}}
+#endif
+
+#pragma clang diagnostic pop
+};
Index: clang/lib/AST/Interp/ByteCodeExprGen.h
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.h
+++ clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -83,6 +83,8 @@
   bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E);
   bool VisitOpaqueValueExpr(const OpaqueValueExpr *E);
   bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E);
+  bool VisitStringLiteral(const StringLiteral *E);
+  bool VisitCharacterLiteral(const CharacterLiteral *E);
 
 protected:
   bool visitExpr(const Expr *E) override;
@@ -231,7 +233,7 @@
   /// Emits an integer constant.
   template <typename T> bool emitConst(const Expr *E, T Value) {
     QualType Ty = E->getType();
-    APInt WrappedValue(getIntWidth(Ty), Value, std::is_signed<T>::value);
+    APInt WrappedValue(getIntWidth(Ty), static_cast<uint64_t>(Value), std::is_signed<T>::value);
     return emitConst(*Ctx.classify(Ty), WrappedValue, E);
   }
 
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -397,6 +397,18 @@
   return true;
 }
 
+template <class Emitter>
+bool ByteCodeExprGen<Emitter>::VisitStringLiteral(const StringLiteral *E) {
+  unsigned StringIndex = P.createGlobalString(E);
+  return this->emitGetPtrGlobal(StringIndex, E);
+}
+
+template <class Emitter>
+bool ByteCodeExprGen<Emitter>::VisitCharacterLiteral(
+    const CharacterLiteral *E) {
+  return this->emitConst(E, E->getValue());
+}
+
 template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) {
   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true);
   return this->Visit(E);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D136941: [clang][Inter... Timm Bäder via Phabricator via cfe-commits

Reply via email to