[lld] r295022 - Rename ScriptParser.{cpp,h} -> ScriptLexer.{cpp,h}.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 13 20:47:05 PST 2017


Author: ruiu
Date: Mon Feb 13 22:47:05 2017
New Revision: 295022

URL: http://llvm.org/viewvc/llvm-project?rev=295022&view=rev
Log:
Rename ScriptParser.{cpp,h} -> ScriptLexer.{cpp,h}.

These files contain a lexer, so the new names are better.
The parser is in LinkerScript.{cpp,h}.

Added:
    lld/trunk/ELF/ScriptLexer.cpp
    lld/trunk/ELF/ScriptLexer.h
Removed:
    lld/trunk/ELF/ScriptParser.cpp
    lld/trunk/ELF/ScriptParser.h
Modified:
    lld/trunk/ELF/CMakeLists.txt
    lld/trunk/ELF/LinkerScript.cpp

Modified: lld/trunk/ELF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/CMakeLists.txt?rev=295022&r1=295021&r2=295022&view=diff
==============================================================================
--- lld/trunk/ELF/CMakeLists.txt (original)
+++ lld/trunk/ELF/CMakeLists.txt Mon Feb 13 22:47:05 2017
@@ -22,7 +22,7 @@ add_lld_library(lldELF
   Mips.cpp
   OutputSections.cpp
   Relocations.cpp
-  ScriptParser.cpp
+  ScriptLexer.cpp
   Strings.cpp
   SymbolTable.cpp
   Symbols.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=295022&r1=295021&r2=295022&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Mon Feb 13 22:47:05 2017
@@ -17,7 +17,7 @@
 #include "InputSection.h"
 #include "Memory.h"
 #include "OutputSections.h"
-#include "ScriptParser.h"
+#include "ScriptLexer.h"
 #include "Strings.h"
 #include "SymbolTable.h"
 #include "Symbols.h"
@@ -1016,12 +1016,12 @@ size_t LinkerScript<ELFT>::getPhdrIndex(
   return 0;
 }
 
-class elf::ScriptParser final : public ScriptParserBase {
+class elf::ScriptParser final : public ScriptLexer {
   typedef void (ScriptParser::*Handler)();
 
 public:
   ScriptParser(MemoryBufferRef MB)
-      : ScriptParserBase(MB),
+      : ScriptLexer(MB),
         IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
 
   void readLinkerScript();

Added: lld/trunk/ELF/ScriptLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptLexer.cpp?rev=295022&view=auto
==============================================================================
--- lld/trunk/ELF/ScriptLexer.cpp (added)
+++ lld/trunk/ELF/ScriptLexer.cpp Mon Feb 13 22:47:05 2017
@@ -0,0 +1,203 @@
+//===- ScriptLexer.cpp ----------------------------------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the base parser class for linker script and dynamic
+// list.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ScriptLexer.h"
+#include "Error.h"
+#include "llvm/ADT/Twine.h"
+
+using namespace llvm;
+using namespace lld;
+using namespace lld::elf;
+
+// Returns a whole line containing the current token.
+StringRef ScriptLexer::getLine() {
+  StringRef S = getCurrentMB().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 the current token.
+size_t ScriptLexer::getLineNumber() {
+  StringRef S = getCurrentMB().getBuffer();
+  StringRef Tok = Tokens[Pos - 1];
+  return S.substr(0, Tok.data() - S.data()).count('\n') + 1;
+}
+
+// Returns 0-based column number of the current token.
+size_t ScriptLexer::getColumnNumber() {
+  StringRef Tok = Tokens[Pos - 1];
+  return Tok.data() - getLine().data();
+}
+
+std::string ScriptLexer::getCurrentLocation() {
+  std::string Filename = getCurrentMB().getBufferIdentifier();
+  if (!Pos)
+    return Filename;
+  return (Filename + ":" + Twine(getLineNumber())).str();
+}
+
+ScriptLexer::ScriptLexer(MemoryBufferRef MB) { tokenize(MB); }
+
+// We don't want to record cascading errors. Keep only the first one.
+void ScriptLexer::setError(const Twine &Msg) {
+  if (Error)
+    return;
+  Error = true;
+
+  if (!Pos) {
+    error(getCurrentLocation() + ": " + Msg);
+    return;
+  }
+
+  std::string S = getCurrentLocation() + ": ";
+  error(S + Msg);
+  error(S + getLine());
+  error(S + std::string(getColumnNumber(), ' ') + "^");
+}
+
+// Split S into linker script tokens.
+void ScriptLexer::tokenize(MemoryBufferRef MB) {
+  std::vector<StringRef> Vec;
+  MBs.push_back(MB);
+  StringRef S = MB.getBuffer();
+  StringRef Begin = S;
+
+  for (;;) {
+    S = skipSpace(S);
+    if (S.empty())
+      break;
+
+    // Quoted token. Note that double-quote characters are parts of a token
+    // because, in a glob match context, only unquoted tokens are interpreted
+    // as glob patterns. Double-quoted tokens are literal patterns in that
+    // context.
+    if (S.startswith("\"")) {
+      size_t E = S.find("\"", 1);
+      if (E == StringRef::npos) {
+        StringRef Filename = MB.getBufferIdentifier();
+        size_t Lineno = Begin.substr(0, S.data() - Begin.data()).count('\n');
+        error(Filename + ":" + Twine(Lineno + 1) + ": unclosed quote");
+        return;
+      }
+
+      Vec.push_back(S.take_front(E + 1));
+      S = S.substr(E + 1);
+      continue;
+    }
+
+    // Unquoted token. This is more relaxed than tokens in C-like language,
+    // so that you can write "file-name.cpp" as one bare token, for example.
+    size_t Pos = S.find_first_not_of(
+        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+        "0123456789_.$/\\~=+[]*?-!<>^");
+
+    // A character that cannot start a word (which is usually a
+    // punctuation) forms a single character token.
+    if (Pos == 0)
+      Pos = 1;
+    Vec.push_back(S.substr(0, Pos));
+    S = S.substr(Pos);
+  }
+
+  Tokens.insert(Tokens.begin() + Pos, Vec.begin(), Vec.end());
+}
+
+// Skip leading whitespace characters or comments.
+StringRef ScriptLexer::skipSpace(StringRef S) {
+  for (;;) {
+    if (S.startswith("/*")) {
+      size_t E = S.find("*/", 2);
+      if (E == StringRef::npos) {
+        error("unclosed comment in a linker script");
+        return "";
+      }
+      S = S.substr(E + 2);
+      continue;
+    }
+    if (S.startswith("#")) {
+      size_t E = S.find('\n', 1);
+      if (E == StringRef::npos)
+        E = S.size() - 1;
+      S = S.substr(E + 1);
+      continue;
+    }
+    size_t Size = S.size();
+    S = S.ltrim();
+    if (S.size() == Size)
+      return S;
+  }
+}
+
+// An erroneous token is handled as if it were the last token before EOF.
+bool ScriptLexer::atEOF() { return Error || Tokens.size() == Pos; }
+
+StringRef ScriptLexer::next() {
+  if (Error)
+    return "";
+  if (atEOF()) {
+    setError("unexpected EOF");
+    return "";
+  }
+  return Tokens[Pos++];
+}
+
+StringRef ScriptLexer::peek(unsigned N) {
+  StringRef Tok;
+  for (unsigned I = 0; I <= N; ++I) {
+    Tok = next();
+    if (Error)
+      return "";
+  }
+  Pos = Pos - N - 1;
+  return Tok;
+}
+
+bool ScriptLexer::consume(StringRef Tok) {
+  if (peek() == Tok) {
+    skip();
+    return true;
+  }
+  return false;
+}
+
+void ScriptLexer::skip() { (void)next(); }
+
+void ScriptLexer::expect(StringRef Expect) {
+  if (Error)
+    return;
+  StringRef Tok = next();
+  if (Tok != Expect)
+    setError(Expect + " expected, but got " + Tok);
+}
+
+// Returns true if S encloses T.
+static bool encloses(StringRef S, StringRef T) {
+  return S.bytes_begin() <= T.bytes_begin() && T.bytes_end() <= S.bytes_end();
+}
+
+MemoryBufferRef ScriptLexer::getCurrentMB() {
+  // Find input buffer containing the current token.
+  assert(!MBs.empty());
+  if (!Pos)
+    return MBs[0];
+
+  for (MemoryBufferRef MB : MBs)
+    if (encloses(MB.getBuffer(), Tokens[Pos - 1]))
+      return MB;
+  llvm_unreachable("getCurrentMB: failed to find a token");
+}

Added: lld/trunk/ELF/ScriptLexer.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptLexer.h?rev=295022&view=auto
==============================================================================
--- lld/trunk/ELF/ScriptLexer.h (added)
+++ lld/trunk/ELF/ScriptLexer.h Mon Feb 13 22:47:05 2017
@@ -0,0 +1,53 @@
+//===- ScriptLexer.h --------------------------------------------*- C++ -*-===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_ELF_SCRIPT_LEXER_H
+#define LLD_ELF_SCRIPT_LEXER_H
+
+#include "lld/Core/LLVM.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include <utility>
+#include <vector>
+
+namespace lld {
+namespace elf {
+
+class ScriptLexer {
+public:
+  explicit ScriptLexer(MemoryBufferRef MB);
+
+  void setError(const Twine &Msg);
+  void tokenize(MemoryBufferRef MB);
+  static StringRef skipSpace(StringRef S);
+  bool atEOF();
+  StringRef next();
+  StringRef peek(unsigned N = 0);
+  void skip();
+  bool consume(StringRef Tok);
+  void expect(StringRef Expect);
+  std::string getCurrentLocation();
+
+  std::vector<MemoryBufferRef> MBs;
+  std::vector<StringRef> Tokens;
+  size_t Pos = 0;
+  bool Error = false;
+
+private:
+  StringRef getLine();
+  size_t getLineNumber();
+  size_t getColumnNumber();
+
+  MemoryBufferRef getCurrentMB();
+};
+
+} // namespace elf
+} // namespace lld
+
+#endif

Removed: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=295021&view=auto
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp (removed)
@@ -1,203 +0,0 @@
-//===- ScriptParser.cpp ---------------------------------------------------===//
-//
-//                             The LLVM Linker
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file contains the base parser class for linker script and dynamic
-// list.
-//
-//===----------------------------------------------------------------------===//
-
-#include "ScriptParser.h"
-#include "Error.h"
-#include "llvm/ADT/Twine.h"
-
-using namespace llvm;
-using namespace lld;
-using namespace lld::elf;
-
-// Returns a whole line containing the current token.
-StringRef ScriptParserBase::getLine() {
-  StringRef S = getCurrentMB().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 the current token.
-size_t ScriptParserBase::getLineNumber() {
-  StringRef S = getCurrentMB().getBuffer();
-  StringRef Tok = Tokens[Pos - 1];
-  return S.substr(0, Tok.data() - S.data()).count('\n') + 1;
-}
-
-// 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 = getCurrentMB().getBufferIdentifier();
-  if (!Pos)
-    return Filename;
-  return (Filename + ":" + Twine(getLineNumber())).str();
-}
-
-ScriptParserBase::ScriptParserBase(MemoryBufferRef MB) { tokenize(MB); }
-
-// We don't want to record cascading errors. Keep only the first one.
-void ScriptParserBase::setError(const Twine &Msg) {
-  if (Error)
-    return;
-  Error = true;
-
-  if (!Pos) {
-    error(getCurrentLocation() + ": " + Msg);
-    return;
-  }
-
-  std::string S = getCurrentLocation() + ": ";
-  error(S + Msg);
-  error(S + getLine());
-  error(S + std::string(getColumnNumber(), ' ') + "^");
-}
-
-// Split S into linker script tokens.
-void ScriptParserBase::tokenize(MemoryBufferRef MB) {
-  std::vector<StringRef> Vec;
-  MBs.push_back(MB);
-  StringRef S = MB.getBuffer();
-  StringRef Begin = S;
-
-  for (;;) {
-    S = skipSpace(S);
-    if (S.empty())
-      break;
-
-    // Quoted token. Note that double-quote characters are parts of a token
-    // because, in a glob match context, only unquoted tokens are interpreted
-    // as glob patterns. Double-quoted tokens are literal patterns in that
-    // context.
-    if (S.startswith("\"")) {
-      size_t E = S.find("\"", 1);
-      if (E == StringRef::npos) {
-        StringRef Filename = MB.getBufferIdentifier();
-        size_t Lineno = Begin.substr(0, S.data() - Begin.data()).count('\n');
-        error(Filename + ":" + Twine(Lineno + 1) + ": unclosed quote");
-        return;
-      }
-
-      Vec.push_back(S.take_front(E + 1));
-      S = S.substr(E + 1);
-      continue;
-    }
-
-    // Unquoted token. This is more relaxed than tokens in C-like language,
-    // so that you can write "file-name.cpp" as one bare token, for example.
-    size_t Pos = S.find_first_not_of(
-        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
-        "0123456789_.$/\\~=+[]*?-!<>^");
-
-    // A character that cannot start a word (which is usually a
-    // punctuation) forms a single character token.
-    if (Pos == 0)
-      Pos = 1;
-    Vec.push_back(S.substr(0, Pos));
-    S = S.substr(Pos);
-  }
-
-  Tokens.insert(Tokens.begin() + Pos, Vec.begin(), Vec.end());
-}
-
-// Skip leading whitespace characters or comments.
-StringRef ScriptParserBase::skipSpace(StringRef S) {
-  for (;;) {
-    if (S.startswith("/*")) {
-      size_t E = S.find("*/", 2);
-      if (E == StringRef::npos) {
-        error("unclosed comment in a linker script");
-        return "";
-      }
-      S = S.substr(E + 2);
-      continue;
-    }
-    if (S.startswith("#")) {
-      size_t E = S.find('\n', 1);
-      if (E == StringRef::npos)
-        E = S.size() - 1;
-      S = S.substr(E + 1);
-      continue;
-    }
-    size_t Size = S.size();
-    S = S.ltrim();
-    if (S.size() == Size)
-      return S;
-  }
-}
-
-// An erroneous token is handled as if it were the last token before EOF.
-bool ScriptParserBase::atEOF() { return Error || Tokens.size() == Pos; }
-
-StringRef ScriptParserBase::next() {
-  if (Error)
-    return "";
-  if (atEOF()) {
-    setError("unexpected EOF");
-    return "";
-  }
-  return Tokens[Pos++];
-}
-
-StringRef ScriptParserBase::peek(unsigned N) {
-  StringRef Tok;
-  for (unsigned I = 0; I <= N; ++I) {
-    Tok = next();
-    if (Error)
-      return "";
-  }
-  Pos = Pos - N - 1;
-  return Tok;
-}
-
-bool ScriptParserBase::consume(StringRef Tok) {
-  if (peek() == Tok) {
-    skip();
-    return true;
-  }
-  return false;
-}
-
-void ScriptParserBase::skip() { (void)next(); }
-
-void ScriptParserBase::expect(StringRef Expect) {
-  if (Error)
-    return;
-  StringRef Tok = next();
-  if (Tok != Expect)
-    setError(Expect + " expected, but got " + Tok);
-}
-
-// Returns true if S encloses T.
-static bool encloses(StringRef S, StringRef T) {
-  return S.bytes_begin() <= T.bytes_begin() && T.bytes_end() <= S.bytes_end();
-}
-
-MemoryBufferRef ScriptParserBase::getCurrentMB() {
-  // Find input buffer containing the current token.
-  assert(!MBs.empty());
-  if (!Pos)
-    return MBs[0];
-
-  for (MemoryBufferRef MB : MBs)
-    if (encloses(MB.getBuffer(), Tokens[Pos - 1]))
-      return MB;
-  llvm_unreachable("getCurrentMB: failed to find a token");
-}

Removed: lld/trunk/ELF/ScriptParser.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.h?rev=295021&view=auto
==============================================================================
--- lld/trunk/ELF/ScriptParser.h (original)
+++ lld/trunk/ELF/ScriptParser.h (removed)
@@ -1,53 +0,0 @@
-//===- ScriptParser.h -------------------------------------------*- C++ -*-===//
-//
-//                             The LLVM Linker
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLD_ELF_SCRIPT_PARSER_H
-#define LLD_ELF_SCRIPT_PARSER_H
-
-#include "lld/Core/LLVM.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include <utility>
-#include <vector>
-
-namespace lld {
-namespace elf {
-
-class ScriptParserBase {
-public:
-  explicit ScriptParserBase(MemoryBufferRef MB);
-
-  void setError(const Twine &Msg);
-  void tokenize(MemoryBufferRef MB);
-  static StringRef skipSpace(StringRef S);
-  bool atEOF();
-  StringRef next();
-  StringRef peek(unsigned N = 0);
-  void skip();
-  bool consume(StringRef Tok);
-  void expect(StringRef Expect);
-  std::string getCurrentLocation();
-
-  std::vector<MemoryBufferRef> MBs;
-  std::vector<StringRef> Tokens;
-  size_t Pos = 0;
-  bool Error = false;
-
-private:
-  StringRef getLine();
-  size_t getLineNumber();
-  size_t getColumnNumber();
-
-  MemoryBufferRef getCurrentMB();
-};
-
-} // namespace elf
-} // namespace lld
-
-#endif




More information about the llvm-commits mailing list