]> granicus.if.org Git - clang/commitdiff
Some "prep" work for handling ObjC @-string constants that contain UTF-8. No function...
authorSteve Naroff <snaroff@apple.com>
Tue, 31 Mar 2009 16:53:37 +0000 (16:53 +0000)
committerSteve Naroff <snaroff@apple.com>
Tue, 31 Mar 2009 16:53:37 +0000 (16:53 +0000)
Changed GenerateConstantString() to take an ObjCStringLiteral (instead of a std::string). While this isn't strictly necessary, it seems cleaner and allows us to cache to "containsNonAscii" if necessary (to avoid checking in both Sema and CodeGen).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68114 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Expr.h
lib/CodeGen/CGExprConstant.cpp
lib/CodeGen/CGObjC.cpp
lib/CodeGen/CGObjCGNU.cpp
lib/CodeGen/CGObjCMac.cpp
lib/CodeGen/CGObjCRuntime.h

index 2271a4e6bb348fddeff846a49a95913a151096a0..832f83ca73c34e83433ad5aa17f50e2c74938783 100644 (file)
@@ -533,7 +533,12 @@ public:
   const char *getStrData() const { return StrData; }
   unsigned getByteLength() const { return ByteLength; }
   bool isWide() const { return IsWide; }
-  
+  bool containsNonAscii() const {
+    for (unsigned i = 0; i < getByteLength(); ++i)
+      if (!isascii(getStrData()[i]))
+        return true;
+    return false;
+  }
   /// getNumConcatenated - Get the number of string literal tokens that were
   /// concatenated in translation phase #6 to form this string literal.
   unsigned getNumConcatenated() const { return NumConcatenated; }
index 3f52175745f25579639d63865ef39a6fed47a5e7..89b331950c1282d479ae803774e03a60c4620ae5 100644 (file)
@@ -417,9 +417,7 @@ public:
       return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
     case Expr::ObjCStringLiteralClass: {
       ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
-      std::string S(SL->getString()->getStrData(), 
-                    SL->getString()->getByteLength());
-      llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(S);
+      llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(SL);
       return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
     }
     case Expr::PredefinedExprClass: {
@@ -445,6 +443,7 @@ public:
       const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
       const StringLiteral *Literal = cast<StringLiteral>(Arg);
       std::string S(Literal->getStrData(), Literal->getByteLength());
+      // FIXME: need to deal with UCN conversion issues.
       return CGM.GetAddrOfConstantCFString(S);
     }
     case Expr::BlockExprClass: {
index 2467a8290aa8d41f57cd6650faa6f5a910fc820a..4db29ce05d08df6ce63e274d549a490e48ba7038 100644 (file)
@@ -26,9 +26,7 @@ using namespace CodeGen;
 /// Emits an instance of NSConstantString representing the object.
 llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) 
 {
-  std::string String(E->getString()->getStrData(), 
-                     E->getString()->getByteLength());
-  llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
+  llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(E);
   // FIXME: This bitcast should just be made an invariant on the Runtime.
   return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
 }
index e6ca536c66bf9d6ce9b49a9af4486e76067e0ea1..bc713bd8cc29e4d3b04002e1a10b544a23601d92 100644 (file)
@@ -94,7 +94,7 @@ private:
       std::vector<llvm::Constant*> &V, const std::string &Name="");
 public:
   CGObjCGNU(CodeGen::CodeGenModule &cgm);
-  virtual llvm::Constant *GenerateConstantString(const std::string &String);
+  virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
   virtual CodeGen::RValue 
   GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
                       QualType ResultType,
@@ -252,7 +252,9 @@ llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
 //TODO: In case there are any crazy people still using the GNU runtime without
 //an OpenStep implementation, this should let them select their own class for
 //constant strings.
-llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
+llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
+  std::string Str(SL->getString()->getStrData(), 
+                  SL->getString()->getByteLength());
   std::vector<llvm::Constant*> Ivars;
   Ivars.push_back(NULLPtr);
   Ivars.push_back(MakeConstantString(Str));
index 05d81eaf0f1ffca3023b01250a0eb82180b1a9a6..f688029fb23a9e27f46e1783c5f09524cf01838c 100644 (file)
@@ -487,7 +487,7 @@ public:
   CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
   { }
   
-  virtual llvm::Constant *GenerateConstantString(const std::string &String);
+  virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
   
   virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
                                          const ObjCContainerDecl *CD=0);
@@ -899,8 +899,13 @@ llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
 */
 
 llvm::Constant *CGObjCCommonMac::GenerateConstantString(
-                                                  const std::string &String) {
-  return CGM.GetAddrOfConstantCFString(String);
+  const ObjCStringLiteral *SL) {
+  std::string Str(SL->getString()->getStrData(), 
+                  SL->getString()->getByteLength());
+  if (SL->getString()->containsNonAscii()) {
+    // FIXME: Convert from UTF-8 to UTF-16.
+  }
+  return CGM.GetAddrOfConstantCFString(Str);
 }
 
 /// Generates a message send where the super is the receiver.  This is
index 7c66ff6f7b3dcf632cd005323f756dbc2a46595d..65bf52c5e205a51a92fcd9e58aec9f0e09b36e8d 100644 (file)
@@ -48,6 +48,7 @@ namespace CodeGen {
   class ObjCProtocolDecl;
   class Selector;
   class ObjCIvarDecl;
+  class ObjCStringLiteral;
 
 namespace CodeGen {
   class CodeGenModule;
@@ -72,7 +73,7 @@ public:
                                    Selector Sel) = 0;
 
   /// Generate a constant string object.
-  virtual llvm::Constant *GenerateConstantString(const std::string &String) = 0;
+  virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *) = 0;
 
   /// Generate a category.  A category contains a list of methods (and
   /// accompanying metadata) and a list of protocols.