[PATCH] D58729: Emit boxed expression as a compile-time constant if the expression inside the parentheses is a string literal

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 27 11:10:21 PST 2019


ahatanak created this revision.
ahatanak added reviewers: rjmccall, arphaman.
ahatanak added a project: clang.
Herald added subscribers: jdoerfert, dexonsmith, jkorous.

clang currently emits an expression like `@("abc")` as a message send to `stringWithUTF8String`. This patch makes changes so that a compile-time constant is emitted instead when the expression inside the parentheses is a string literal. I think it's possible to do the same optimization for constant expressions that evaluate to zero-terminated strings (see the example below), but I'm leaving that for future work.

  constexpr const char *foo() { return "abc"; }
  
  void test() {
    NSString *s = @(foo());
  }

The original motivation for the patch is to silence the `-Wnullable-to-nonnull-conversion` warning that clang started emitting after r317727:
The https://oleb.net/2018/@keypath/.

rdar://problem/42684601


Repository:
  rC Clang

https://reviews.llvm.org/D58729

Files:
  include/clang/AST/ExprObjC.h
  lib/AST/ExprConstant.cpp
  lib/CodeGen/CGExprConstant.cpp
  lib/CodeGen/CGObjC.cpp
  lib/Sema/SemaExprObjC.cpp
  test/CodeGenObjC/boxing.m
  test/SemaObjC/objc-literal-sig.m
  test/SemaObjC/transfer-boxed-string-nullability.m
  test/SemaObjCXX/literals.mm

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58729.188578.patch
Type: text/x-patch
Size: 10000 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190227/563b3956/attachment-0001.bin>


More information about the cfe-commits mailing list