[PATCH] D17256: [ELF] - Minor refactor of LinkerScript file.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 18 02:09:02 PST 2016


grimar updated the summary for this revision.
grimar updated this revision to Diff 48291.
grimar added a comment.

Removed all changes except one because:

- Moving allocator inside ScriptParser was not good idea. Its much more clear to know it's lifetime is attached to LinkerScript class and not to parser. It did not work correctly I think.
- Restored checks of Error flag where removed. That also was incorrect change, thanks to Rui Ueyama for noticing that during review of another patch.


http://reviews.llvm.org/D17256

Files:
  ELF/LinkerScript.cpp

Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -89,6 +89,8 @@
   void run();
 
 private:
+  void init();
+
   void setError(const Twine &Msg);
   static std::vector<StringRef> tokenize(StringRef S);
   static StringRef skipSpace(StringRef S);
@@ -114,38 +116,35 @@
 
   StringSaver Saver;
   std::vector<StringRef> Tokens;
-  bool Error = false;
+  llvm::StringMap<std::function<void(ScriptParser &)>> Cmd;
   size_t Pos = 0;
   bool IsUnderSysroot;
+  bool Error = false;
 };
 
+void ScriptParser::init() {
+  Cmd["ENTRY"] = &ScriptParser::readEntry;
+  Cmd["EXTERN"] = &ScriptParser::readExtern;
+  Cmd["GROUP"] = &ScriptParser::readGroup;
+  Cmd["INCLUDE"] = &ScriptParser::readInclude;
+  Cmd["INPUT"] = &ScriptParser::readGroup;
+  Cmd["OUTPUT"] = &ScriptParser::readOutput;
+  Cmd["OUTPUT_ARCH"] = &ScriptParser::readOutputArch;
+  Cmd["OUTPUT_FORMAT"] = &ScriptParser::readOutputFormat;
+  Cmd["SEARCH_DIR"] = &ScriptParser::readSearchDir;
+  Cmd["SECTIONS"] = &ScriptParser::readSections;
+  Cmd[";"] = [](ScriptParser &) {};
+}
+
 void ScriptParser::run() {
+  init();
   while (!atEOF()) {
     StringRef Tok = next();
-    if (Tok == ";")
-      continue;
-    if (Tok == "ENTRY") {
-      readEntry();
-    } else if (Tok == "EXTERN") {
-      readExtern();
-    } else if (Tok == "GROUP" || Tok == "INPUT") {
-      readGroup();
-    } else if (Tok == "INCLUDE") {
-      readInclude();
-    } else if (Tok == "OUTPUT") {
-      readOutput();
-    } else if (Tok == "OUTPUT_ARCH") {
-      readOutputArch();
-    } else if (Tok == "OUTPUT_FORMAT") {
-      readOutputFormat();
-    } else if (Tok == "SEARCH_DIR") {
-      readSearchDir();
-    } else if (Tok == "SECTIONS") {
-      readSections();
-    } else {
+    auto C = Cmd.find(Tok);
+    if (Cmd.find(Tok) != Cmd.end())
+      C->second(*this);
+    else
       setError("unknown directive: " + Tok);
-      return;
-    }
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17256.48291.patch
Type: text/x-patch
Size: 2008 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160218/238a9170/attachment.bin>


More information about the llvm-commits mailing list