I saw some code duplication around the use of CharLiteralParser, and
hopefully this fixes it; nothing major, it just looks like some APIs
changed and a little redundancy sprouted up.

There's some very similar duplication surrounding NumericLiteralParser and
StringLiteralParser that I'll fix up in a future patch soon, but I thought
I'd test the waters first.

I git diff'd against c568543. `make test` ran with no unexpected failures.

--Sean Silva
diff --git a/include/clang/Lex/LiteralSupport.h b/include/clang/Lex/LiteralSupport.h
index 6142f00..d43a380 100644
--- a/include/clang/Lex/LiteralSupport.h
+++ b/include/clang/Lex/LiteralSupport.h
@@ -128,10 +128,12 @@ class CharLiteralParser {
   tok::TokenKind Kind;
   bool IsMultiChar;
   bool HadError;
+
+  CharLiteralParser(uint64_t value, tok::TokenKind kind,
+                    bool is_multi_char, bool had_error);
+
 public:
-  CharLiteralParser(const char *begin, const char *end,
-                    SourceLocation Loc, Preprocessor &PP,
-                    tok::TokenKind kind);
+  CharLiteralParser(const Token& Tok, Preprocessor &PP);
 
   bool hadError() const { return HadError; }
   bool isAscii() const { return Kind == tok::char_constant; }
diff --git a/lib/Lex/LiteralSupport.cpp b/lib/Lex/LiteralSupport.cpp
index 547bd4e..32a02bd 100644
--- a/lib/Lex/LiteralSupport.cpp
+++ b/lib/Lex/LiteralSupport.cpp
@@ -763,16 +763,21 @@ NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
 ///       hex-quad:
 ///         hex-digit hex-digit hex-digit hex-digit
 ///
-CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
-                                     SourceLocation Loc, Preprocessor &PP,
-                                     tok::TokenKind kind) {
+CharLiteralParser::CharLiteralParser(const Token &Tok, Preprocessor &PP)
+  : Value(0), Kind(Tok.getKind()), IsMultiChar(false), HadError(false) {
   // At this point we know that the character matches the regex "(L|u|U)?'.*'".
-  HadError = false;
 
-  Kind = kind;
+  SmallString<16> SpellingBuffer;
+  StringRef Spelling = PP.getSpelling(Tok, SpellingBuffer, &HadError);
+  if (HadError)
+    return;
+
+  const char *begin = Spelling.begin();
+  const char *end = Spelling.end();
+  SourceLocation Loc = Tok.getLocation();
 
   // Skip over wide character determinant.
-  if (Kind != tok::char_constant) {
+  if (!isAscii()) {
     ++begin;
   }
 
@@ -803,16 +808,13 @@ CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
   // Unicode escapes representing characters that cannot be correctly
   // represented in a single code unit are disallowed in character literals
   // by this implementation.
-  uint32_t largest_character_for_kind;
-  if (tok::wide_char_constant == Kind) {
+  uint32_t largest_character_for_kind = 0x7Fu;
+  if (isWide())
     largest_character_for_kind = 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth());
-  } else if (tok::utf16_char_constant == Kind) {
+  else if (isUTF16())
     largest_character_for_kind = 0xFFFF;
-  } else if (tok::utf32_char_constant == Kind) {
+  else if (isUTF32())
     largest_character_for_kind = 0x10FFFF;
-  } else {
-    largest_character_for_kind = 0x7Fu;
-  }
 
   while (begin!=end) {
     // Is this a span of non-escape characters?
@@ -893,8 +895,7 @@ CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
     else
       PP.Diag(Loc, diag::err_multichar_utf_character_literal);
     IsMultiChar = true;
-  } else
-    IsMultiChar = false;
+  }
 
   llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
 
diff --git a/lib/Lex/PPExpressions.cpp b/lib/Lex/PPExpressions.cpp
index c4ab143..2fea97d 100644
--- a/lib/Lex/PPExpressions.cpp
+++ b/lib/Lex/PPExpressions.cpp
@@ -251,14 +251,7 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
   case tok::wide_char_constant: {   // L'x'
   case tok::utf16_char_constant:    // u'x'
   case tok::utf32_char_constant:    // U'x'
-    SmallString<32> CharBuffer;
-    bool CharInvalid = false;
-    StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
-    if (CharInvalid)
-      return true;
-
-    CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
-                              PeekTok.getLocation(), PP, PeekTok.getKind());
+    CharLiteralParser Literal(PeekTok, PP);
     if (Literal.hadError())
       return true;  // A diagnostic was already emitted.
 
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 30a02b2..b1d7e45 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -2350,14 +2350,7 @@ ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) {
 }
 
 ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
-  SmallString<16> CharBuffer;
-  bool Invalid = false;
-  StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid);
-  if (Invalid)
-    return ExprError();
-
-  CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(),
-                            PP, Tok.getKind());
+  CharLiteralParser Literal(Tok, PP);
   if (Literal.hadError())
     return ExprError();
 
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to