[llvm] r244295 - MIR Parser: Simplify the token's string value handling.

Alex Lorenz via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 6 16:17:43 PDT 2015


Author: arphaman
Date: Thu Aug  6 18:17:42 2015
New Revision: 244295

URL: http://llvm.org/viewvc/llvm-project?rev=244295&view=rev
Log:
MIR Parser: Simplify the token's string value handling.

This commit removes the 'StringOffset' and 'HasStringValue' fields from the
MIToken struct and simplifies the 'stringValue' method which now returns
the new 'StringValue' field.

This commit also adopts a different way of initializing the lexed tokens -
instead of constructing a new MIToken instance, the lexer resets the old token
using the new 'reset' method and sets its attributes using the new
'setStringValue', 'setOwnedStringValue', and 'setIntegerValue' methods.

Reviewers: Sean Silva

Differential Revision: http://reviews.llvm.org/D11792

Modified:
    llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp
    llvm/trunk/lib/CodeGen/MIRParser/MILexer.h
    llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp

Modified: llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp?rev=244295&r1=244294&r2=244295&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MILexer.cpp Thu Aug  6 18:17:42 2015
@@ -55,6 +55,28 @@ public:
 
 } // end anonymous namespace
 
+MIToken &MIToken::reset(TokenKind Kind, StringRef Range) {
+  this->Kind = Kind;
+  this->Range = Range;
+  return *this;
+}
+
+MIToken &MIToken::setStringValue(StringRef StrVal) {
+  StringValue = StrVal;
+  return *this;
+}
+
+MIToken &MIToken::setOwnedStringValue(std::string StrVal) {
+  StringValueStorage = std::move(StrVal);
+  StringValue = StringValueStorage;
+  return *this;
+}
+
+MIToken &MIToken::setIntegerValue(APSInt IntVal) {
+  this->IntVal = std::move(IntVal);
+  return *this;
+}
+
 /// Skip the leading whitespace characters and return the updated cursor.
 static Cursor skipWhitespace(Cursor C) {
   while (isspace(C.peek()))
@@ -124,17 +146,18 @@ static Cursor lexName(
   if (C.peek() == '"') {
     if (Cursor R = lexStringConstant(C, ErrorCallback)) {
       StringRef String = Range.upto(R);
-      Token = MIToken(Type, String,
-                      unescapeQuotedString(String.drop_front(PrefixLength)),
-                      PrefixLength);
+      Token.reset(Type, String)
+          .setOwnedStringValue(
+              unescapeQuotedString(String.drop_front(PrefixLength)));
       return R;
     }
-    Token = MIToken(MIToken::Error, Range.remaining());
+    Token.reset(MIToken::Error, Range.remaining());
     return Range;
   }
   while (isIdentifierChar(C.peek()))
     C.advance();
-  Token = MIToken(Type, Range.upto(C), PrefixLength);
+  Token.reset(Type, Range.upto(C))
+      .setStringValue(Range.upto(C).drop_front(PrefixLength));
   return C;
 }
 
@@ -145,7 +168,7 @@ static Cursor maybeLexIntegerType(Cursor
   C.advance(); // Skip 'i'
   while (isdigit(C.peek()))
     C.advance();
-  Token = MIToken(MIToken::IntegerType, Range.upto(C));
+  Token.reset(MIToken::IntegerType, Range.upto(C));
   return C;
 }
 
@@ -187,7 +210,8 @@ static Cursor maybeLexIdentifier(Cursor
   while (isIdentifierChar(C.peek()))
     C.advance();
   auto Identifier = Range.upto(C);
-  Token = MIToken(getIdentifierKind(Identifier), Identifier);
+  Token.reset(getIdentifierKind(Identifier), Identifier)
+      .setStringValue(Identifier);
   return C;
 }
 
@@ -199,7 +223,7 @@ static Cursor maybeLexMachineBasicBlock(
   auto Range = C;
   C.advance(4); // Skip '%bb.'
   if (!isdigit(C.peek())) {
-    Token = MIToken(MIToken::Error, C.remaining());
+    Token.reset(MIToken::Error, C.remaining());
     ErrorCallback(C.location(), "expected a number after '%bb.'");
     return C;
   }
@@ -214,8 +238,9 @@ static Cursor maybeLexMachineBasicBlock(
     while (isIdentifierChar(C.peek()))
       C.advance();
   }
-  Token = MIToken(MIToken::MachineBasicBlock, Range.upto(C), APSInt(Number),
-                  StringOffset);
+  Token.reset(MIToken::MachineBasicBlock, Range.upto(C))
+      .setIntegerValue(APSInt(Number))
+      .setStringValue(Range.upto(C).drop_front(StringOffset));
   return C;
 }
 
@@ -228,7 +253,7 @@ static Cursor maybeLexIndex(Cursor C, MI
   auto NumberRange = C;
   while (isdigit(C.peek()))
     C.advance();
-  Token = MIToken(Kind, Range.upto(C), APSInt(NumberRange.upto(C)));
+  Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
   return C;
 }
 
@@ -249,7 +274,9 @@ static Cursor maybeLexIndexAndName(Curso
     while (isIdentifierChar(C.peek()))
       C.advance();
   }
-  Token = MIToken(Kind, Range.upto(C), APSInt(Number), StringOffset);
+  Token.reset(Kind, Range.upto(C))
+      .setIntegerValue(APSInt(Number))
+      .setStringValue(Range.upto(C).drop_front(StringOffset));
   return C;
 }
 
@@ -295,8 +322,8 @@ static Cursor lexVirtualRegister(Cursor
   auto NumberRange = C;
   while (isdigit(C.peek()))
     C.advance();
-  Token = MIToken(MIToken::VirtualRegister, Range.upto(C),
-                  APSInt(NumberRange.upto(C)));
+  Token.reset(MIToken::VirtualRegister, Range.upto(C))
+      .setIntegerValue(APSInt(NumberRange.upto(C)));
   return C;
 }
 
@@ -309,8 +336,8 @@ static Cursor maybeLexRegister(Cursor C,
   C.advance(); // Skip '%'
   while (isIdentifierChar(C.peek()))
     C.advance();
-  Token = MIToken(MIToken::NamedRegister, Range.upto(C),
-                  /*StringOffset=*/1); // Drop the '%'
+  Token.reset(MIToken::NamedRegister, Range.upto(C))
+      .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
   return C;
 }
 
@@ -327,8 +354,8 @@ static Cursor maybeLexGlobalValue(
   auto NumberRange = C;
   while (isdigit(C.peek()))
     C.advance();
-  Token =
-      MIToken(MIToken::GlobalValue, Range.upto(C), APSInt(NumberRange.upto(C)));
+  Token.reset(MIToken::GlobalValue, Range.upto(C))
+      .setIntegerValue(APSInt(NumberRange.upto(C)));
   return C;
 }
 
@@ -354,7 +381,7 @@ static Cursor maybeLexHexFloatingPointLi
     C.advance();
   while (isxdigit(C.peek()))
     C.advance();
-  Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C));
+  Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
   return C;
 }
 
@@ -370,7 +397,7 @@ static Cursor lexFloatingPointLiteral(Cu
     while (isdigit(C.peek()))
       C.advance();
   }
-  Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C));
+  Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
   return C;
 }
 
@@ -384,7 +411,7 @@ static Cursor maybeLexNumericalLiteral(C
   if (C.peek() == '.')
     return lexFloatingPointLiteral(Range, C, Token);
   StringRef StrVal = Range.upto(C);
-  Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal));
+  Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
   return C;
 }
 
@@ -423,7 +450,7 @@ static Cursor maybeLexSymbol(Cursor C, M
     return None;
   auto Range = C;
   C.advance(Length);
-  Token = MIToken(Kind, Range.upto(C));
+  Token.reset(Kind, Range.upto(C));
   return C;
 }
 
@@ -432,7 +459,7 @@ StringRef llvm::lexMIToken(
     function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
   auto C = skipWhitespace(Cursor(Source));
   if (C.isEOF()) {
-    Token = MIToken(MIToken::Eof, C.remaining());
+    Token.reset(MIToken::Eof, C.remaining());
     return C.remaining();
   }
 
@@ -467,7 +494,7 @@ StringRef llvm::lexMIToken(
   if (Cursor R = maybeLexSymbol(C, Token))
     return R.remaining();
 
-  Token = MIToken(MIToken::Error, C.remaining());
+  Token.reset(MIToken::Error, C.remaining());
   ErrorCallback(C.location(),
                 Twine("unexpected character '") + Twine(C.peek()) + "'");
   return C.remaining();

Modified: llvm/trunk/lib/CodeGen/MIRParser/MILexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MILexer.h?rev=244295&r1=244294&r2=244295&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MILexer.h (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MILexer.h Thu Aug  6 18:17:42 2015
@@ -94,26 +94,19 @@ struct MIToken {
 
 private:
   TokenKind Kind;
-  unsigned StringOffset;
-  bool HasStringValue;
   StringRef Range;
-  std::string StringValue;
+  StringRef StringValue;
+  std::string StringValueStorage;
   APSInt IntVal;
 
 public:
-  MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
-      : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
-        Range(Range) {}
-
-  MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
-          unsigned StringOffset = 0)
-      : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
-        Range(Range), StringValue(std::move(StringValue)) {}
-
-  MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
-          unsigned StringOffset = 0)
-      : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
-        Range(Range), IntVal(IntVal) {}
+  MIToken() : Kind(Error) {}
+
+  MIToken &reset(TokenKind Kind, StringRef Range);
+
+  MIToken &setStringValue(StringRef StrVal);
+  MIToken &setOwnedStringValue(std::string StrVal);
+  MIToken &setIntegerValue(APSInt IntVal);
 
   TokenKind kind() const { return Kind; }
 
@@ -141,17 +134,10 @@ public:
 
   StringRef::iterator location() const { return Range.begin(); }
 
-  /// Return the token's raw string value.
-  ///
-  /// If the string value is quoted, this method returns that quoted string as
-  /// it is, without unescaping the string value.
-  StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
+  StringRef range() const { return Range; }
 
   /// Return the token's string value.
-  StringRef stringValue() const {
-    return HasStringValue ? StringRef(StringValue)
-                          : Range.drop_front(StringOffset);
-  }
+  StringRef stringValue() const { return StringValue; }
 
   const APSInt &integerValue() const { return IntVal; }
 

Modified: llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp?rev=244295&r1=244294&r2=244295&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp Thu Aug  6 18:17:42 2015
@@ -200,7 +200,7 @@ MIParser::MIParser(SourceMgr &SM, Machin
                    StringRef Source, const PerFunctionMIParsingState &PFS,
                    const SlotMapping &IRSlots)
     : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source),
-      Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {}
+      PFS(PFS), IRSlots(IRSlots) {}
 
 void MIParser::lex() {
   CurrentSource = lexMIToken(
@@ -571,7 +571,7 @@ bool MIParser::parseImmediateOperand(Mac
 }
 
 bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
-  auto Source = StringRef(Loc, Token.stringValue().end() - Loc).str();
+  auto Source = StringRef(Loc, Token.range().end() - Loc).str();
   lex();
   SMDiagnostic Err;
   C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent());
@@ -681,8 +681,8 @@ bool MIParser::parseGlobalValue(GlobalVa
     const Module *M = MF.getFunction()->getParent();
     GV = M->getNamedValue(Token.stringValue());
     if (!GV)
-      return error(Twine("use of undefined global value '@") +
-                   Token.rawStringValue() + "'");
+      return error(Twine("use of undefined global value '") + Token.range() +
+                   "'");
     break;
   }
   case MIToken::GlobalValue: {
@@ -851,8 +851,7 @@ bool MIParser::parseIRBlock(BasicBlock *
     BB = dyn_cast_or_null<BasicBlock>(
         F.getValueSymbolTable().lookup(Token.stringValue()));
     if (!BB)
-      return error(Twine("use of undefined IR block '%ir-block.") +
-                   Token.rawStringValue() + "'");
+      return error(Twine("use of undefined IR block '") + Token.range() + "'");
     break;
   }
   case MIToken::IRBlock: {
@@ -1019,7 +1018,7 @@ bool MIParser::parseMachineOperandAndTar
 bool MIParser::parseOperandsOffset(MachineOperand &Op) {
   if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
     return false;
-  StringRef Sign = Token.stringValue();
+  StringRef Sign = Token.range();
   bool IsNegative = Token.is(MIToken::minus);
   lex();
   if (Token.isNot(MIToken::IntegerLiteral))
@@ -1039,8 +1038,7 @@ bool MIParser::parseIRValue(Value *&V) {
   case MIToken::NamedIRValue: {
     V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
     if (!V)
-      return error(Twine("use of undefined IR value '%ir.") +
-                   Token.rawStringValue() + "'");
+      return error(Twine("use of undefined IR value '") + Token.range() + "'");
     break;
   }
   // TODO: Parse unnamed IR value references.




More information about the llvm-commits mailing list