On 08/14/2010 11:50 PM, Sean Hunt wrote:
Hey all,

This patch adds a new UDStringLiteral expression and the lexing and
parsing machinery required in order to implement this C++0x feature.

Yay!

Sean

Oops, my apologies, that was a reverse patch. Here's a corrected one.
diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h
index 184edcd..8171ed5 100644
--- a/include/clang/AST/ExprCXX.h
+++ b/include/clang/AST/ExprCXX.h
@@ -2375,6 +2375,43 @@ public:
   virtual child_iterator child_end();
 };
 
+/// UDStringLiteralExpr - An expression for a user-defined
+/// string literal (e.g. "foo"_bar)
+///
+/// Both the DeclRefExpr and the IntegerConstant are fictional expressions
+/// generated from the literal.
+class UDStringLiteral : public CallExpr {
+  StringLiteral *BaseLiteral;
+
+public:
+  UDStringLiteral(ASTContext &C, StringLiteral *SL, Expr *fn, Expr **args,
+                  unsigned numargs, QualType t)
+    : CallExpr(C, UDStringLiteralClass, fn, args, numargs, t, SourceLocation())
+    , BaseLiteral(SL) {
+    assert(numargs == 2 && "Wrong number of arguments to UDStringLiteral");
+  }
+
+  FunctionDecl *getLiteralOperator() { return getDirectCallee(); };
+  const FunctionDecl *getLiteralOperator() const { return getDirectCallee(); };
+
+  StringLiteral *getBaseLiteral() { return BaseLiteral; }
+  const StringLiteral *getBaseLiteral() const { return BaseLiteral; }
+  void setBaseLiteral(StringLiteral *SL) { BaseLiteral = SL; }
+
+  IdentifierInfo *getUDSuffix() const {
+    return getLiteralOperator()->getDeclName().getCXXLiteralIdentifier();
+  }
+
+  virtual SourceRange getSourceRange() const {
+    return getBaseLiteral()->getSourceRange();
+  }
+
+  static bool classof(const Stmt *T) {
+    return T->getStmtClass() == UDStringLiteralClass;
+  }
+  static bool classof(const UDStringLiteral *) { return true; }
+};
+
 inline ExplicitTemplateArgumentList &OverloadExpr::getExplicitTemplateArgs() {
   if (isa<UnresolvedLookupExpr>(this))
     return cast<UnresolvedLookupExpr>(this)->getExplicitTemplateArgs();
diff --git a/include/clang/AST/RecursiveASTVisitor.h b/include/clang/AST/RecursiveASTVisitor.h
index ee0bca5..e12834a 100644
--- a/include/clang/AST/RecursiveASTVisitor.h
+++ b/include/clang/AST/RecursiveASTVisitor.h
@@ -1709,6 +1709,7 @@ DEF_TRAVERSE_STMT(FloatingLiteral, { })
 DEF_TRAVERSE_STMT(ImaginaryLiteral, { })
 DEF_TRAVERSE_STMT(StringLiteral, { })
 DEF_TRAVERSE_STMT(ObjCStringLiteral, { })
+DEF_TRAVERSE_STMT(UDStringLiteral, { })
 
 // FIXME: look at the following tricky-seeming exprs to see if we
 // need to recurse on anything.  These are ones that have methods
diff --git a/include/clang/Basic/DiagnosticLexKinds.td b/include/clang/Basic/DiagnosticLexKinds.td
index 9b06fa8..d1505de 100644
--- a/include/clang/Basic/DiagnosticLexKinds.td
+++ b/include/clang/Basic/DiagnosticLexKinds.td
@@ -98,6 +98,9 @@ def ext_string_too_long : Extension<"string literal of length %0 exceeds "
   "maximum length %1 that %select{C90|ISO C99|C++}2 compilers are required to "
   "support">, InGroup<OverlengthStrings>;
   
+def err_ud_suffix_mismatch : Error<"User-defined literal suffixes on adjacent "
+  "string literal tokens do not match">;
+
 //===----------------------------------------------------------------------===//
 // PTH Diagnostics
 //===----------------------------------------------------------------------===//
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 7cdc9f4..e056454 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2928,9 +2928,13 @@ def err_operator_delete_param_type : Error<
 // C++ literal operators
 def err_literal_operator_outside_namespace : Error<
   "literal operator %0 must be in a namespace or global scope">;
-// FIXME: This diagnostic sucks
+def warn_literal_operator_no_underscore : Warning< "literal operator names not "
+  "beginning with underscores are reserved for future standardization">;
+def err_literal_operator_overload : Error<
+  "no matching literal operator function for user-defined suffix '%0'">;
+// FIXME: These diagnostics suck
 def err_literal_operator_params : Error<
-  "parameter declaration for literal operator %0 is not valid">;
+  "parameter declaration for literal operator '%0' is not valid">;
 
 // C++ conversion functions
 def err_conv_function_not_member : Error<
diff --git a/include/clang/Basic/StmtNodes.td b/include/clang/Basic/StmtNodes.td
index a2f6973..20f75e7 100644
--- a/include/clang/Basic/StmtNodes.td
+++ b/include/clang/Basic/StmtNodes.td
@@ -112,6 +112,9 @@ def OverloadExpr : DStmt<Expr, 1>;
 def UnresolvedLookupExpr : DStmt<OverloadExpr>;
 def UnresolvedMemberExpr : DStmt<OverloadExpr>;
 
+// C++0x expressions
+def UDStringLiteral : DStmt<Expr>;
+
 // Obj-C Expressions.
 def ObjCStringLiteral : DStmt<Expr>;
 def ObjCEncodeExpr : DStmt<Expr>;
diff --git a/include/clang/Lex/Lexer.h b/include/clang/Lex/Lexer.h
index 9e0fb7e..e05113d 100644
--- a/include/clang/Lex/Lexer.h
+++ b/include/clang/Lex/Lexer.h
@@ -17,6 +17,7 @@
 #include "clang/Lex/PreprocessorLexer.h"
 #include "clang/Basic/LangOptions.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Allocator.h"
 #include <string>
 #include <vector>
 #include <cassert>
@@ -67,6 +68,9 @@ class Lexer : public PreprocessorLexer {
   // line" flag set on it.
   bool IsAtStartOfLine;
 
+  // ExtraDataAllocator - An allocator for extra data on a token.
+  llvm::BumpPtrAllocator ExtraDataAllocator;
+
   Lexer(const Lexer&);          // DO NOT IMPLEMENT
   void operator=(const Lexer&); // DO NOT IMPLEMENT
   friend class Preprocessor;
diff --git a/include/clang/Lex/LiteralSupport.h b/include/clang/Lex/LiteralSupport.h
index ba46fb1..40112f5 100644
--- a/include/clang/Lex/LiteralSupport.h
+++ b/include/clang/Lex/LiteralSupport.h
@@ -27,6 +27,7 @@ class Preprocessor;
 class Token;
 class SourceLocation;
 class TargetInfo;
+class IdentifierInfo;
 
 /// NumericLiteralParser - This performs strict semantic analysis of the content
 /// of a ppnumber, classifying it as either integer, floating, or erroneous,
@@ -145,6 +146,7 @@ class StringLiteralParser {
   unsigned wchar_tByteWidth;
   llvm::SmallString<512> ResultBuf;
   char *ResultPtr; // cursor
+  IdentifierInfo *UDSuffix;
 public:
   StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
                       Preprocessor &PP, bool Complain = true);
@@ -155,6 +157,9 @@ public:
   const char *GetString() { return &ResultBuf[0]; }
   unsigned GetStringLength() const { return ResultPtr-&ResultBuf[0]; }
 
+  bool isUserDefinedLiteral() const { return UDSuffix; }
+  IdentifierInfo *getUDSuffix() const { return UDSuffix; }
+
   unsigned GetNumStringChars() const {
     if (AnyWide)
       return GetStringLength() / wchar_tByteWidth;
diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h
index 6fd1943..ad4d372 100644
--- a/include/clang/Lex/Preprocessor.h
+++ b/include/clang/Lex/Preprocessor.h
@@ -616,7 +616,7 @@ public:
   /// copy).  The caller is not allowed to modify the returned buffer pointer
   /// if an internal buffer is returned.
   unsigned getSpelling(const Token &Tok, const char *&Buffer, 
-                       bool *Invalid = 0) const;
+                       bool *Invalid = 0, bool LiteralOnly = false) const;
 
   /// getSpelling - This method is used to get the spelling of a token into a
   /// SmallVector. Note that the returned StringRef may not point to the
diff --git a/include/clang/Lex/Token.h b/include/clang/Lex/Token.h
index bd9b468..18fa9bf 100644
--- a/include/clang/Lex/Token.h
+++ b/include/clang/Lex/Token.h
@@ -14,16 +14,16 @@
 #ifndef LLVM_CLANG_TOKEN_H
 #define LLVM_CLANG_TOKEN_H
 
+#include "llvm/Support/Allocator.h"
 #include "clang/Basic/TemplateKinds.h"
 #include "clang/Basic/TokenKinds.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/OperatorKinds.h"
+#include "clang/Basic/IdentifierTable.h"
 #include <cstdlib>
 
 namespace clang {
 
-class IdentifierInfo;
-
 /// Token - This structure provides full information about a lexed token.
 /// It is not intended to be space efficient, it is intended to return as much
 /// information as possible about each returned token.  This is expected to be
@@ -34,6 +34,14 @@ class IdentifierInfo;
 /// can be represented by a single typename annotation token that carries
 /// information about the SourceRange of the tokens and the type object.
 class Token {
+  /// An extra-large structure for storing the data needed for a user-defined
+  /// literal - the raw literal, and the identifier suffix.
+  struct UDLData {
+    IdentifierInfo *II;
+    const char *LiteralData;
+    unsigned LiteralLength;
+  };
+
   /// The location of the token.
   SourceLocation Loc;
 
@@ -47,7 +55,7 @@ class Token {
   /// token.
   unsigned UintData;
 
-  /// PtrData - This is a union of four different pointer types, which depends
+  /// PtrData - This is a union of five different pointer types, which depends
   /// on what type of token this is:
   ///  Identifiers, keywords, etc:
   ///    This is an IdentifierInfo*, which contains the uniqued identifier
@@ -55,6 +63,8 @@ class Token {
   ///  Literals:  isLiteral() returns true.
   ///    This is a pointer to the start of the token in a text buffer, which
   ///    may be dirty (have trigraphs / escaped newlines).
+  ///  User-defined literals: isUserDefinedLiteral() returns true.
+  ///    This is a pointer to a UDLData.
   ///  Annotations (resolved type names, C++ scopes, etc): isAnnotation().
   ///    This is a pointer to sema-specific data for the annotation token.
   ///  Other:
@@ -71,12 +81,14 @@ class Token {
   unsigned char Flags;
 public:
 
-  // Various flags set per token:
+  /// Various flags set per token:
   enum TokenFlags {
-    StartOfLine   = 0x01,  // At start of line or only after whitespace.
-    LeadingSpace  = 0x02,  // Whitespace exists before this token.
-    DisableExpand = 0x04,  // This identifier may never be macro expanded.
-    NeedsCleaning = 0x08   // Contained an escaped newline or trigraph.
+    StartOfLine   =      0x01,  ///< At start of line or only after whitespace
+    LeadingSpace  =      0x02,  ///< Whitespace exists before this token
+    DisableExpand =      0x04,  ///< This identifier may never be macro expanded
+    NeedsCleaning =      0x08,  ///< Contained an escaped newline or trigraph
+    UserDefinedLiteral = 0x10,  ///< This literal has a ud-suffix
+    SLPortionClean =     0x20   ///< A UDL's literal portion needs no cleaning
   };
 
   tok::TokenKind getKind() const { return (tok::TokenKind)Kind; }
@@ -108,12 +120,40 @@ public:
     assert(!isAnnotation() && "Annotation tokens have no length field");
     return UintData;
   }
+  /// getLiteralLength - Return the length of the literal portion of the token,
+  /// which may not be the token length if this is a user-defined literal.
+  unsigned getLiteralLength() const {
+    assert(isLiteral() && "Using getLiteralLength on a non-literal token");
+    if (isUserDefinedLiteral())
+      return reinterpret_cast<UDLData*>(PtrData)->LiteralLength;
+    else
+      return UintData;
+  }
 
   void setLocation(SourceLocation L) { Loc = L; }
   void setLength(unsigned Len) {
     assert(!isAnnotation() && "Annotation tokens have no length field");
     UintData = Len;
   }
+  void setLiteralLength(unsigned Len) {
+    assert(isLiteral() && "Using setLiteralLength on a non-literal token");
+    if (isUserDefinedLiteral())
+      reinterpret_cast<UDLData*>(PtrData)->LiteralLength = Len;
+    else
+      UintData = Len;
+  }
+
+  /// makeUserDefinedLiteral - Set this token to be a user-defined literal
+  void makeUserDefinedLiteral(llvm::BumpPtrAllocator &Alloc) {
+    PtrData = Alloc.Allocate(sizeof(UDLData), 4);
+    setFlag(UserDefinedLiteral);
+  }
+  /// clearUserLiteralData - Deallocate the user-defined literal data
+  /// The token will be left in a garbage state.
+  void clearUserDefinedLiteral() {
+    delete reinterpret_cast<UDLData*>(PtrData);
+    PtrData = 0;
+  }
 
   SourceLocation getAnnotationEndLoc() const {
     assert(isAnnotation() && "Used AnnotEndLocID on non-annotation token");
@@ -154,11 +194,18 @@ public:
 
   IdentifierInfo *getIdentifierInfo() const {
     assert(!isAnnotation() && "Used IdentInfo on annotation token!");
-    if (isLiteral()) return 0;
-    return (IdentifierInfo*) PtrData;
+    if (isUserDefinedLiteral())
+      return reinterpret_cast<UDLData*>(PtrData)->II;
+    else if (isLiteral())
+      return 0;
+    else
+      return reinterpret_cast<IdentifierInfo*>(PtrData);
   }
   void setIdentifierInfo(IdentifierInfo *II) {
-    PtrData = (void*) II;
+    if (isUserDefinedLiteral())
+      reinterpret_cast<UDLData*>(PtrData)->II = II;
+    else
+      PtrData = (void*)II;
   }
 
   /// getLiteralData - For a literal token (numeric constant, string, etc), this
@@ -166,11 +213,17 @@ public:
   /// otherwise.
   const char *getLiteralData() const {
     assert(isLiteral() && "Cannot get literal data of non-literal");
-    return reinterpret_cast<const char*>(PtrData);
+    if (isUserDefinedLiteral())
+      return reinterpret_cast<UDLData*>(PtrData)->LiteralData;
+    else
+      return reinterpret_cast<const char*>(PtrData);
   }
   void setLiteralData(const char *Ptr) {
     assert(isLiteral() && "Cannot set literal data of non-literal");
-    PtrData = const_cast<char*>(Ptr);
+    if (isUserDefinedLiteral())
+      reinterpret_cast<UDLData*>(PtrData)->LiteralData = Ptr;
+    else
+      PtrData = const_cast<char*>(Ptr);
   }
 
   void *getAnnotationValue() const {
@@ -221,6 +274,12 @@ public:
     return (Flags & DisableExpand) ? true : false;
   }
 
+  /// isUserDefinedLiteral - Return true if this is a C++0x user-defined literal
+  /// token.
+  bool isUserDefinedLiteral() const {
+    return (Flags & UserDefinedLiteral) ? true : false;
+  }
+
   /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
   bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const;
 
@@ -229,8 +288,17 @@ public:
 
   /// needsCleaning - Return true if this token has trigraphs or escaped
   /// newlines in it.
-  ///
-  bool needsCleaning() const { return (Flags & NeedsCleaning) ? true : false; }
+  bool needsCleaning() const {
+    return (Flags & NeedsCleaning) ? true : false;
+  }
+
+  /// literalNeedsCleaning - Return true if the literal portion of this token
+  /// needs cleaning.
+  bool literalNeedsCleaning() const {
+    assert(isLiteral() && "Using literalNeedsCleaning on a non-literal token");
+    return (Flags & NeedsCleaning) ? ((Flags & SLPortionClean) ? false : true)
+                                   : false;
+  }
 };
 
 /// PPConditionalInfo - Information about the conditional stack (#if directives)
diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h
index 263dacb..c96461c 100644
--- a/include/clang/Parse/Action.h
+++ b/include/clang/Parse/Action.h
@@ -1123,7 +1123,7 @@ public:
 
   /// ActOnStringLiteral - The specified tokens were lexed as pasted string
   /// fragments (e.g. "foo" "bar" L"baz").
-  virtual OwningExprResult ActOnStringLiteral(const Token *Toks,
+  virtual OwningExprResult ActOnStringLiteral(Scope *S, const Token *Toks,
                                               unsigned NumToks) {
     return ExprEmpty();
   }
diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h
index 5ef8e5f..cbe292f 100644
--- a/include/clang/Sema/Sema.h
+++ b/include/clang/Sema/Sema.h
@@ -1985,7 +1985,7 @@ public:
 
   /// ActOnStringLiteral - The specified tokens were lexed as pasted string
   /// fragments (e.g. "foo" "bar" L"baz").
-  virtual OwningExprResult ActOnStringLiteral(const Token *Toks,
+  virtual OwningExprResult ActOnStringLiteral(Scope *S, const Token *Toks,
                                               unsigned NumToks);
 
   // Binary/Unary Operators.  'Tok' is the token for the operator.
@@ -2975,6 +2975,9 @@ public:
 
   bool CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl);
 
+  OwningExprResult BuildUDStringLiteral(Scope *S, StringLiteral *SL, unsigned L,
+                                         IdentifierInfo *II);
+
   //===--------------------------------------------------------------------===//
   // C++ Templates [C++ 14]
   //
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 3b288ec..195ed57 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -2419,7 +2419,8 @@ static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
   case Expr::UnaryTypeTraitExprClass:
     return NoDiag();
   case Expr::CallExprClass:
-  case Expr::CXXOperatorCallExprClass: {
+  case Expr::CXXOperatorCallExprClass:
+  case Expr::UDStringLiteralClass: {
     const CallExpr *CE = cast<CallExpr>(E);
     if (CE->isBuiltinCall(Ctx))
       return CheckEvalInICE(E, Ctx);
diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp
index 7f497a3..0f3ffa0 100644
--- a/lib/AST/StmtPrinter.cpp
+++ b/lib/AST/StmtPrinter.cpp
@@ -1197,6 +1197,11 @@ void StmtPrinter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *Node) {
   }
 }
 
+void StmtPrinter::VisitUDStringLiteral(UDStringLiteral *Node) {
+  VisitStringLiteral(Node->getBaseLiteral());
+  OS << Node->getUDSuffix()->getName();
+}
+
 static const char *getTypeTraitName(UnaryTypeTrait UTT) {
   switch (UTT) {
   default: assert(false && "Unknown type trait");
diff --git a/lib/AST/StmtProfile.cpp b/lib/AST/StmtProfile.cpp
index 5beefd1..dd51376 100644
--- a/lib/AST/StmtProfile.cpp
+++ b/lib/AST/StmtProfile.cpp
@@ -827,6 +827,12 @@ void StmtProfiler::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *S) {
     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
 }
 
+void StmtProfiler::VisitUDStringLiteral(UDStringLiteral *S) {
+  VisitExpr(S);
+  VisitStringLiteral(S->getBaseLiteral());
+  ID.AddString(S->getUDSuffix()->getName());
+}
+
 void StmtProfiler::VisitObjCStringLiteral(ObjCStringLiteral *S) {
   VisitExpr(S);
 }
diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp
index 9a96934..079c327 100644
--- a/lib/Lex/Lexer.cpp
+++ b/lib/Lex/Lexer.cpp
@@ -547,6 +547,11 @@ static void InitCharacterInfo() {
   isInited = true;
 }
 
+/// isIdentifierStart - Return true if this is the start character of an
+/// identifier, which is [a-zA-Z_].
+static inline bool isIdentifierStart(unsigned char c) {
+  return (CharInfo[c] & (CHAR_LETTER|CHAR_UNDER)) ? true : false;
+}
 
 /// isIdentifierBody - Return true if this is the body character of an
 /// identifier, which is [a-zA-Z0-9_].
@@ -980,8 +985,30 @@ void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
 
   // Update the location of the token as well as the BufferPtr instance var.
   const char *TokStart = BufferPtr;
-  FormTokenWithChars(Result, CurPtr,
-                     Wide ? tok::wide_string_literal : tok::string_literal);
+  tok::TokenKind Kind = Wide ? tok::wide_string_literal : tok::string_literal;
+
+  // FIXME: Handle UCNs
+  unsigned Size;
+  if (PP && PP->getLangOptions().CPlusPlus0x &&
+      isIdentifierStart(getCharAndSize(CurPtr, Size))) {
+    Result.makeUserDefinedLiteral(ExtraDataAllocator);
+    Result.setFlagValue(Token::SLPortionClean, !Result.needsCleaning());
+    Result.setKind(Kind);
+    Result.setLiteralLength(CurPtr - BufferPtr);
+
+    // FIXME: We hack around the lexer's routines a lot here.
+    BufferPtr = CurPtr;
+    bool OldRawMode = LexingRawMode;
+    LexingRawMode = true;
+    LexIdentifier(Result, ConsumeChar(CurPtr, Size, Result));
+    LexingRawMode = OldRawMode;
+    PP->LookUpIdentifierInfo(Result, CurPtr);
+
+    CurPtr = BufferPtr;
+    BufferPtr = TokStart;
+  }
+
+  FormTokenWithChars(Result, CurPtr, Kind);
   Result.setLiteralData(TokStart);
 }
 
diff --git a/lib/Lex/LiteralSupport.cpp b/lib/Lex/LiteralSupport.cpp
index a12c4ae..eb7337a 100644
--- a/lib/Lex/LiteralSupport.cpp
+++ b/lib/Lex/LiteralSupport.cpp
@@ -758,30 +758,38 @@ CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
 ///
 StringLiteralParser::
 StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
-                    Preprocessor &pp, bool Complain) : PP(pp) {
+                    Preprocessor &pp, bool Complain) : PP(pp), hadError(false) {
   // Scan all of the string portions, remember the max individual token length,
   // computing a bound on the concatenated string length, and see whether any
   // piece is a wide-string.  If any of the string portions is a wide-string
   // literal, the result is a wide-string literal [C99 6.4.5p4].
-  MaxTokenLength = StringToks[0].getLength();
-  SizeBound = StringToks[0].getLength()-2;  // -2 for "".
+  MaxTokenLength = StringToks[0].getLiteralLength();
+  SizeBound = StringToks[0].getLiteralLength()-2;  // -2 for "".
   AnyWide = StringToks[0].is(tok::wide_string_literal);
-
-  hadError = false;
+  UDSuffix = StringToks[0].getIdentifierInfo();
 
   // Implement Translation Phase #6: concatenation of string literals
   /// (C99 5.1.1.2p1).  The common case is only one string fragment.
   for (unsigned i = 1; i != NumStringToks; ++i) {
     // The string could be shorter than this if it needs cleaning, but this is a
     // reasonable bound, which is all we need.
-    SizeBound += StringToks[i].getLength()-2;  // -2 for "".
+    SizeBound += StringToks[i].getLiteralLength()-2;  // -2 for "".
 
     // Remember maximum string piece length.
-    if (StringToks[i].getLength() > MaxTokenLength)
-      MaxTokenLength = StringToks[i].getLength();
+    if (StringToks[i].getLiteralLength() > MaxTokenLength)
+      MaxTokenLength = StringToks[i].getLiteralLength();
 
     // Remember if we see any wide strings.
     AnyWide |= StringToks[i].is(tok::wide_string_literal);
+
+    if (StringToks[i].isUserDefinedLiteral()) {
+      if (UDSuffix && UDSuffix != StringToks[i].getIdentifierInfo()) {
+        // FIXME: Improve location and note previous
+        PP.Diag(StringToks[0].getLocation(), diag::err_ud_suffix_mismatch);
+        hadError = true;
+      } else if (!UDSuffix)
+        UDSuffix = StringToks[0].getIdentifierInfo();
+    }
   }
 
   // Include space for the null terminator.
@@ -823,7 +831,7 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
     // and 'spelled' tokens can only shrink.
     bool StringInvalid = false;
     unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf, 
-                                         &StringInvalid);
+                                         &StringInvalid, true);
     if (StringInvalid) {
       hadError = 1;
       continue;
@@ -938,7 +946,7 @@ unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
                                                     bool Complain) {
   // Get the spelling of the token.
   llvm::SmallString<16> SpellingBuffer;
-  SpellingBuffer.resize(Tok.getLength());
+  SpellingBuffer.resize(Tok.getLiteralLength());
 
   bool StringInvalid = false;
   const char *SpellingPtr = &SpellingBuffer[0];
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index 77b88f7..df4a22c 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -344,15 +344,25 @@ std::string Preprocessor::getSpelling(const Token &Tok, bool *Invalid) const {
 /// to point to a constant buffer with the data already in it (avoiding a
 /// copy).  The caller is not allowed to modify the returned buffer pointer
 /// if an internal buffer is returned.
-unsigned Preprocessor::getSpelling(const Token &Tok,
-                                   const char *&Buffer, bool *Invalid) const {
+///
+/// If LiteralOnly is specified, only the literal portion of the token is
+/// processed.
+unsigned Preprocessor::getSpelling(const Token &Tok, const char *&Buffer,
+                                   bool *Invalid, bool LiteralOnly) const {
   assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
+  assert((!LiteralOnly || Tok.isLiteral()) &&
+         "LiteralOnly used on a non-literal token");
+
+  unsigned (Token::*getLength) () const = LiteralOnly
+    ? &Token::getLiteralLength : &Token::getLength;
 
   // If this token is an identifier, just return the string from the identifier
   // table, which is very quick.
   if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
-    Buffer = II->getNameStart();
-    return II->getLength();
+    if (!Tok.isUserDefinedLiteral()) {
+      Buffer = II->getNameStart();
+      return II->getLength();
+    }
   }
 
   // Otherwise, compute the start of the token in the input lexer buffer.
@@ -373,20 +383,20 @@ unsigned Preprocessor::getSpelling(const Token &Tok,
   }
 
   // If this token contains nothing interesting, return it directly.
-  if (!Tok.needsCleaning()) {
+  if (!(LiteralOnly ? Tok.literalNeedsCleaning() : Tok.needsCleaning())) {
     Buffer = TokStart;
-    return Tok.getLength();
+    return (Tok.*getLength)();
   }
 
   // Otherwise, hard case, relex the characters into the string.
   char *OutBuf = const_cast<char*>(Buffer);
-  for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
+  for (const char *Ptr = TokStart, *End = TokStart+(Tok.*getLength)();
        Ptr != End; ) {
     unsigned CharSize;
     *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
     Ptr += CharSize;
   }
-  assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
+  assert(unsigned(OutBuf-Buffer) != (Tok.*getLength)() &&
          "NeedsCleaning flag set on something that didn't need cleaning!");
 
   return OutBuf-Buffer;
diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp
index 844dd3f..0cc6877 100644
--- a/lib/Parse/ParseExpr.cpp
+++ b/lib/Parse/ParseExpr.cpp
@@ -1572,7 +1572,8 @@ Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
   } while (isTokenStringLiteral());
 
   // Pass the set of string tokens, ready for concatenation, to the actions.
-  return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
+  return Actions.ActOnStringLiteral(getCurScope(), &StringToks[0],
+                                    StringToks.size());
 }
 
 /// ParseExpressionList - Used for C/C++ (argument-)expression-list.
diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp
index a83aff1..5038da5 100644
--- a/lib/Parse/ParseStmt.cpp
+++ b/lib/Parse/ParseStmt.cpp
@@ -1233,11 +1233,11 @@ Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
              Tok.isNot(tok::eof));
   }
   Token t;
+  t.startToken();
   t.setKind(tok::string_literal);
   t.setLiteralData("\"FIXME: not done\"");
-  t.clearFlag(Token::NeedsCleaning);
   t.setLength(17);
-  OwningExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
+  OwningExprResult AsmString(Actions.ActOnStringLiteral(getCurScope(), &t, 1));
   ExprVector Constraints(Actions);
   ExprVector Exprs(Actions);
   ExprVector Clobbers(Actions);
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index a1a8466..1aba0ef 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -5805,6 +5805,9 @@ bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) {
     return true;
   }
 
+  if (FnDecl->getDeclName().getCXXLiteralIdentifier()->getName()[0] != '_')
+    Diag(FnDecl->getLocation(), diag::warn_literal_operator_no_underscore);
+
   bool Valid = false;
 
   // template <char...> type operator "" name() is the only valid template
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 59d0328..b581272 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -372,7 +372,8 @@ QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
 /// string.
 ///
 Action::OwningExprResult
-Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
+Sema::ActOnStringLiteral(Scope *S, const Token *StringToks,
+                         unsigned NumStringToks) {
   assert(NumStringToks && "Must have at least one string!");
 
   StringLiteralParser Literal(StringToks, NumStringToks, PP);
@@ -398,12 +399,18 @@ Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
                                  llvm::APInt(32, Literal.GetNumStringChars()+1),
                                        ArrayType::Normal, 0);
 
+  StringLiteral *SL = StringLiteral::Create(Context, Literal.GetString(),
+                                           Literal.GetStringLength(),
+                                           Literal.AnyWide, StrTy,
+                                           &StringTokLocs[0],
+                                           StringTokLocs.size());
+
+  if (Literal.isUserDefinedLiteral())
+    return BuildUDStringLiteral(S, SL, Literal.GetNumStringChars(),
+                                Literal.getUDSuffix());
+
   // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
-  return Owned(StringLiteral::Create(Context, Literal.GetString(),
-                                     Literal.GetStringLength(),
-                                     Literal.AnyWide, StrTy,
-                                     &StringTokLocs[0],
-                                     StringTokLocs.size()));
+  return Owned(SL);
 }
 
 /// ShouldSnapshotBlockValueReference - Return true if a reference inside of
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index 35e679f..153eef3 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -3060,3 +3060,55 @@ Sema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) {
   
   return Owned(FullExpr);
 }
+
+Sema::OwningExprResult Sema::BuildUDStringLiteral(Scope *S, StringLiteral *SL,
+                                                  unsigned L,
+                                                  IdentifierInfo *II) {
+  DeclarationName DN = Context.DeclarationNames.getCXXLiteralOperatorName(II);
+
+  LookupResult R(*this, DN, SL->getLocStart(),  LookupOrdinaryName);
+  LookupName(R, S);
+
+  llvm::APInt APL(Context.getTypeSize(Context.getSizeType()), L);
+
+  Expr *Args[2];
+  Args[0] = SL;
+  Args[1] = new (Context) IntegerLiteral(APL, Context.getSizeType(),
+                                         SourceLocation());
+
+  OverloadCandidateSet CandidateSet(SL->getLocStart());
+  AddFunctionCandidates(R.asUnresolvedSet(), Args, 2, CandidateSet);
+  OverloadCandidateSet::iterator Best;
+  OverloadingResult Result = BestViableFunction(CandidateSet,
+                                                SL->getLocStart(), Best);
+
+  if (Result != OR_Success) {
+    Diag(SL->getLocStart(), diag::err_literal_operator_overload)
+      << SL->getSourceRange() << II->getName();
+    return ExprError();
+  }
+
+  assert(Best->Function && "Literal operator function not a real function");
+  FunctionDecl *FD = Best->Function;
+
+  OwningExprResult InputInit
+    = PerformCopyInitialization(InitializedEntity::InitializeParameter(
+                                                   FD->getParamDecl(0)),
+                                SourceLocation(), Owned(SL));
+  if (InputInit.isInvalid())
+    return ExprError();
+  Args[0] = InputInit.takeAs<Expr>();
+
+  QualType ResultTy = FD->getResultType().getNonReferenceType();
+  Expr *Fn = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation());
+  UsualUnaryConversions(Fn);
+
+  ExprOwningPtr<CallExpr> TheCall(this,
+    new (Context) UDStringLiteral(Context, SL, Fn, Args, 2, ResultTy));
+
+  if (CheckCallReturnType(FD->getResultType(), SL->getLocStart(),
+                          TheCall.get(), FD))
+    return ExprError();
+
+  return MaybeBindToTemporary(TheCall.release());
+}
diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h
index 2f8d075..0f12b5c 100644
--- a/lib/Sema/TreeTransform.h
+++ b/lib/Sema/TreeTransform.h
@@ -6043,6 +6043,12 @@ TreeTransform<Derived>::TransformObjCEncodeExpr(ObjCEncodeExpr *E) {
 
 template<typename Derived>
 Sema::OwningExprResult
+TreeTransform<Derived>::TransformUDStringLiteral(UDStringLiteral *E) {
+  return SemaRef.Owned(E->Retain());
+}
+
+template<typename Derived>
+Sema::OwningExprResult
 TreeTransform<Derived>::TransformObjCMessageExpr(ObjCMessageExpr *E) {
   // Transform arguments.
   bool ArgChanged = false;
diff --git a/test/Parser/cxx0x-literal-operators.cpp b/test/Parser/cxx0x-literal-operators.cpp
index 30b2903..cc979b3 100644
--- a/test/Parser/cxx0x-literal-operators.cpp
+++ b/test/Parser/cxx0x-literal-operators.cpp
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x %s
 
 void operator "" (const char *); // expected-error {{expected identifier}}
-void operator "k" foo(const char *); // expected-error {{string literal after 'operator' must be '""'}}
-void operator "" tester (const char *);
+void operator "k" _foo(const char *); // expected-error {{string literal after 'operator' must be '""'}}
+void operator "" _tester (const char *);
diff --git a/test/SemaCXX/literal-operator-dcls.cpp b/test/SemaCXX/literal-operator-dcls.cpp
new file mode 100644
index 0000000..88bf2b9
--- /dev/null
+++ b/test/SemaCXX/literal-operator-dcls.cpp
@@ -0,0 +1,46 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x %s
+
+#include <stddef.h>
+
+struct tag {
+  void operator "" _tag_bad (const char *); // expected-error {{literal operator 'operator "" _tag_bad' must be in a namespace or global scope}}
+  friend void operator "" _tag_good (const char *);
+};
+
+namespace ns { void operator "" _ns_good (const char *); }
+
+// Check extern "C++" declarations
+extern "C++" void operator "" _extern_good (const char *);
+extern "C++" { void operator "" _extern_good (const char *); }
+
+void fn () { void operator "" _fn_bad (const char *); } // expected-error {{literal operator 'operator "" _fn_bad' must be in a namespace or global scope}}
+
+// Warning name
+void operator "" warn (const char *); // expected-warning {{reserved for future standardization}}
+
+// One-param declarations (const char * was already checked)
+void operator "" _good (char);
+void operator "" _good (wchar_t);
+void operator "" _good (char16_t);
+void operator "" _good (char32_t);
+void operator "" _good (unsigned long long);
+void operator "" _good (long double);
+
+// Two-param declarations
+void operator "" _good (const char *, size_t);
+void operator "" _good (const wchar_t *, size_t);
+void operator "" _good (const char16_t *, size_t);
+void operator "" _good (const char32_t *, size_t);
+
+// Check typedef and array equivalences
+void operator "" _good (const char[]);
+typedef const char c;
+void operator "" _good (c*);
+
+// Check extra cv-qualifiers
+void operator "" _cv_good (volatile const char *, const size_t);
+
+// Template delcaration (not implemented yet)
+// template <char...> void operator "" good ();
+
+// FIXME: Test some invalid decls that might crop up.
diff --git a/test/SemaCXX/literal-operators.cpp b/test/SemaCXX/literal-operators.cpp
index ec585a6..93fd4b6 100644
--- a/test/SemaCXX/literal-operators.cpp
+++ b/test/SemaCXX/literal-operators.cpp
@@ -2,42 +2,21 @@
 
 #include <stddef.h>
 
-struct tag {
-  void operator "" tag_bad (const char *); // expected-error {{literal operator 'operator "" tag_bad' must be in a namespace or global scope}}
-  friend void operator "" tag_good (const char *);
+template <typename T, typename U> struct same_type {
+  static const bool value = false;
 };
 
-namespace ns { void operator "" ns_good (const char *); }
-
-// Check extern "C++" declarations
-extern "C++" void operator "" extern_good (const char *);
-extern "C++" { void operator "" extern_good (const char *); }
-
-void fn () { void operator "" fn_bad (const char *); } // expected-error {{literal operator 'operator "" fn_bad' must be in a namespace or global scope}}
-
-// One-param declarations (const char * was already checked)
-void operator "" good (char);
-void operator "" good (wchar_t);
-void operator "" good (char16_t);
-void operator "" good (char32_t);
-void operator "" good (unsigned long long);
-void operator "" good (long double);
+template <typename T> struct same_type<T, T> {
+  static const bool value = true;
+};
 
-// Two-param declarations
-void operator "" good (const char *, size_t);
-void operator "" good (const wchar_t *, size_t);
-void operator "" good (const char16_t *, size_t);
-void operator "" good (const char32_t *, size_t);
+int operator "" _int (const char *, size_t);
+static_assert(same_type<int, decltype(""_int)>::value, "not the same type!");
 
-// Check typedef and array equivalences
-void operator "" good (const char[]);
-typedef const char c;
-void operator "" good (c*);
+int i = ""_int;
+int j = L""_int; // expected-error {{no matching literal operator function}}
 
-// Check extra cv-qualifiers
-void operator "" cv_good (volatile const char *, const size_t);
+int operator "" _int (const wchar_t *, size_t);
 
-// Template delcaration (not implemented yet)
-// template <char...> void operator "" good ();
+int k = L""_int;
 
-// FIXME: Test some invalid decls that might crop up.
diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp
index be3623f..04d2b78 100644
--- a/tools/libclang/CXCursor.cpp
+++ b/tools/libclang/CXCursor.cpp
@@ -218,6 +218,7 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
   case Stmt::CXXMemberCallExprClass:
   case Stmt::CXXConstructExprClass:  
   case Stmt::CXXTemporaryObjectExprClass:
+  case Stmt::UDStringLiteralClass:
     // FIXME: CXXUnresolvedConstructExpr
     // FIXME: ObjCImplicitSetterGetterRefExpr?
     K = CXCursor_CallExpr;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to