[lld] r288308 - Make get{Line,Column}Number members of StringParser.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 30 20:36:49 PST 2016


Author: ruiu
Date: Wed Nov 30 22:36:49 2016
New Revision: 288308

URL: http://llvm.org/viewvc/llvm-project?rev=288308&view=rev
Log:
Make get{Line,Column}Number members of StringParser.

This patch also renames currentLocation getCurrentLocation.

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/ScriptParser.cpp
    lld/trunk/ELF/ScriptParser.h

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=288308&r1=288307&r2=288308&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Wed Nov 30 22:36:49 2016
@@ -1713,7 +1713,7 @@ Expr ScriptParser::readPrimary() {
     return readParenExpr();
 
   StringRef Tok = next();
-  std::string Location = currentLocation();
+  std::string Location = getCurrentLocation();
 
   if (Tok == "~") {
     Expr E = readPrimary();

Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=288308&r1=288307&r2=288308&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Wed Nov 30 22:36:49 2016
@@ -20,22 +20,35 @@ using namespace llvm;
 using namespace lld;
 using namespace lld::elf;
 
-// Returns a line containing a token.
-static StringRef getLine(StringRef S, StringRef Tok) {
+// Returns a whole line containing the current token.
+StringRef ScriptParserBase::getLine() {
+  StringRef S = currentBuffer().getBuffer();
+  StringRef Tok = Tokens[Pos - 1];
+
   size_t Pos = S.rfind('\n', Tok.data() - S.data());
   if (Pos != StringRef::npos)
     S = S.substr(Pos + 1);
   return S.substr(0, S.find_first_of("\r\n"));
 }
 
-// Returns 1-based line number of a given token.
-static size_t getLineNumber(StringRef S, StringRef Tok) {
+// Returns 1-based line number of the current token.
+size_t ScriptParserBase::getLineNumber() {
+  StringRef S = currentBuffer().getBuffer();
+  StringRef Tok = Tokens[Pos - 1];
   return S.substr(0, Tok.data() - S.data()).count('\n') + 1;
 }
 
-// Returns 0-based column number of a given token.
-static size_t getColumnNumber(StringRef S, StringRef Tok) {
-  return Tok.data() - getLine(S, Tok).data();
+// Returns 0-based column number of the current token.
+size_t ScriptParserBase::getColumnNumber() {
+  StringRef Tok = Tokens[Pos - 1];
+  return Tok.data() - getLine().data();
+}
+
+std::string ScriptParserBase::getCurrentLocation() {
+  std::string Filename = currentBuffer().getBufferIdentifier();
+  if (!Pos)
+    return Filename;
+  return (Filename + ":" + Twine(getLineNumber())).str();
 }
 
 ScriptParserBase::ScriptParserBase(MemoryBufferRef MB) { tokenize(MB); }
@@ -46,21 +59,15 @@ void ScriptParserBase::setError(const Tw
     return;
   Error = true;
 
-  MemoryBufferRef MB = currentBuffer();
-  std::string Filename = MB.getBufferIdentifier();
-
   if (!Pos) {
-    error(Filename + ": " + Msg);
+    error(getCurrentLocation() + ": " + Msg);
     return;
   }
 
-  StringRef Buf = MB.getBuffer();
-  StringRef Tok = Tokens[Pos - 1];
-  std::string S = (Filename + ":" + Twine(getLineNumber(Buf, Tok))).str();
-
-  error(S + ": " + Msg);
-  error(S + ": " + getLine(Buf, Tok));
-  error(S + ": " + std::string(getColumnNumber(Buf, Tok), ' ') + "^");
+  std::string S = getCurrentLocation() + ": ";
+  error(S + Msg);
+  error(S + getLine());
+  error(S + std::string(getColumnNumber(), ' ') + "^");
 }
 
 // Split S into linker script tokens.
@@ -69,6 +76,7 @@ void ScriptParserBase::tokenize(MemoryBu
   MBs.push_back(MB);
   StringRef S = MB.getBuffer();
   StringRef Begin = S;
+
   for (;;) {
     S = skipSpace(S);
     if (S.empty())
@@ -81,10 +89,12 @@ void ScriptParserBase::tokenize(MemoryBu
     if (S.startswith("\"")) {
       size_t E = S.find("\"", 1);
       if (E == StringRef::npos) {
-        error(MB.getBufferIdentifier() + ":" + Twine(getLineNumber(Begin, S)) +
-              ": unclosed quote");
+        StringRef Filename = MB.getBufferIdentifier();
+        size_t Lineno = Begin.substr(0, S.data() - Begin.data()).count('\n');
+        error(Filename + ":" + Twine(Lineno) + ": unclosed quote");
         return;
       }
+
       Ret.push_back(S.take_front(E + 1));
       S = S.substr(E + 1);
       continue;
@@ -103,6 +113,7 @@ void ScriptParserBase::tokenize(MemoryBu
     Ret.push_back(S.substr(0, Pos));
     S = S.substr(Pos);
   }
+
   Tokens.insert(Tokens.begin() + Pos, Ret.begin(), Ret.end());
 }
 
@@ -171,13 +182,6 @@ void ScriptParserBase::expect(StringRef
     setError(Expect + " expected, but got " + Tok);
 }
 
-std::string ScriptParserBase::currentLocation() {
-  MemoryBufferRef MB = currentBuffer();
-  return (MB.getBufferIdentifier() + ":" +
-          Twine(getLineNumber(MB.getBuffer(), Tokens[Pos - 1])))
-      .str();
-}
-
 // Returns true if string 'Bigger' contains string 'Shorter'.
 static bool containsString(StringRef Bigger, StringRef Shorter) {
   const char *BiggerEnd = Bigger.data() + Bigger.size();

Modified: lld/trunk/ELF/ScriptParser.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.h?rev=288308&r1=288307&r2=288308&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.h (original)
+++ lld/trunk/ELF/ScriptParser.h Wed Nov 30 22:36:49 2016
@@ -32,7 +32,7 @@ public:
   void skip();
   bool consume(StringRef Tok);
   void expect(StringRef Expect);
-  std::string currentLocation();
+  std::string getCurrentLocation();
 
   std::vector<MemoryBufferRef> MBs;
   std::vector<StringRef> Tokens;
@@ -40,6 +40,10 @@ public:
   bool Error = false;
 
 private:
+  StringRef getLine();
+  size_t getLineNumber();
+  size_t getColumnNumber();
+
   MemoryBufferRef currentBuffer();
 };
 




More information about the llvm-commits mailing list