From: Daniel Dunbar Date: Fri, 3 Apr 2009 00:57:44 +0000 (+0000) Subject: Add target hook for setting symbol prefix and section of unicode X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a9668e0b4c451a1021fe650c451b54dc98c2d18d;p=clang Add target hook for setting symbol prefix and section of unicode string literals. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68363 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/TargetInfo.h b/include/clang/Basic/TargetInfo.h index d9a8bc1f50..4fd624a626 100644 --- a/include/clang/Basic/TargetInfo.h +++ b/include/clang/Basic/TargetInfo.h @@ -257,6 +257,18 @@ public: return ""; } + /// getUnicodeStringSymbolPrefix - Get the default symbol prefix to + /// use for string literals. + virtual const char *getUnicodeStringSymbolPrefix() const { + return ".str"; + } + + /// getUnicodeStringSymbolPrefix - Get the default symbol prefix to + /// use for string literals. + virtual const char *getUnicodeStringSection() const { + return 0; + } + /// getCFStringSection - Return the section to use for the CFString /// literals, or 0 if no special section is used. virtual const char *getCFStringSection() const { diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp index ce55ec8046..5f5ba60e85 100644 --- a/lib/Basic/Targets.cpp +++ b/lib/Basic/Targets.cpp @@ -658,6 +658,14 @@ public: return IsConstant ? "\01LC" : "\01lC"; } + virtual const char *getUnicodeStringSymbolPrefix() const { + return "__utf16_string_"; + } + + virtual const char *getUnicodeStringSection() const { + return "__TEXT,__ustring"; + } + virtual const char *getCFStringSymbolPrefix() const { return "\01LC"; } @@ -810,6 +818,14 @@ public: return IsConstant ? "\01LC" : "\01lC"; } + virtual const char *getUnicodeStringSymbolPrefix() const { + return "__utf16_string_"; + } + + virtual const char *getUnicodeStringSection() const { + return "__TEXT,__ustring"; + } + virtual const char *getCFStringSymbolPrefix() const { return "\01L_unnamed_cfstring_"; } diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index fa475ced6e..1132c3f721 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -1082,13 +1082,31 @@ GetAddrOfConstantCFString(const StringLiteral *Literal) { CurField = NextField; NextField = *Field++; llvm::Constant *C = llvm::ConstantArray::get(str); + + const char *Sect, *Prefix; + bool isConstant; + if (isUTF16) { + Prefix = getContext().Target.getUnicodeStringSymbolPrefix(); + Sect = getContext().Target.getUnicodeStringSection(); + // FIXME: Why does GCC not set constant here? + isConstant = false; + } else { + Prefix = getContext().Target.getStringSymbolPrefix(true); + Sect = getContext().Target.getCFStringDataSection(); + // FIXME: -fwritable-strings should probably affect this, but we + // are following gcc here. + isConstant = true; + } llvm::GlobalVariable *GV = - new llvm::GlobalVariable(C->getType(), true, + new llvm::GlobalVariable(C->getType(), isConstant, llvm::GlobalValue::InternalLinkage, - C, getContext().Target.getStringSymbolPrefix(true), - &getModule()); - if (const char *Sect = getContext().Target.getCFStringDataSection()) + C, Prefix, &getModule()); + if (Sect) GV->setSection(Sect); + if (isUTF16) { + unsigned Align = getContext().getTypeAlign(getContext().ShortTy)/8; + GV->setAlignment(Align); + } appendFieldAndPadding(*this, Fields, CurField, NextField, llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2), CFRD, STy); diff --git a/test/CodeGen/darwin-string-literals.c b/test/CodeGen/darwin-string-literals.c index 5bfb84e91d..ff245fcc7a 100644 --- a/test/CodeGen/darwin-string-literals.c +++ b/test/CodeGen/darwin-string-literals.c @@ -2,8 +2,9 @@ // RUN: grep -F '@"\01LC" = internal constant [8 x i8] c"string0\00"' %t && // RUN: grep -F '@"\01LC1" = internal constant [8 x i8] c"string1\00", section "__TEXT,__cstring,cstring_literals"' %t && - +// RUN: grep -F '@__utf16_string_ = internal global [35 x i8] c"h\00e\00l\00l\00o\00 \00\92! \00\03& \00\90! \00w\00o\00r\00l\00d\00\00", section "__TEXT,__ustring", align 2' %t && // RUN: true const char *g0 = "string0"; const void *g1 = __builtin___CFStringMakeConstantString("string1"); +const void *g2 = __builtin___CFStringMakeConstantString("hello \u2192 \u2603 \u2190 world");