Skip to content

Commit

Permalink
[ELF] - Minor refactor of LinkerScript file
Browse files Browse the repository at this point in the history
* Else-ifs in ScriptParser::run() replaced with std::function + map
* Reordered members of ScriptParser

Differential revision: http://reviews.llvm.org/D17256

llvm-svn: 261317
  • Loading branch information
George Rimar committed Feb 19, 2016
1 parent 31088a9 commit f23b232
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions lld/ELF/LinkerScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,20 @@ template <class ELFT> bool SectionRule::match(InputSectionBase<ELFT> *S) {
class elf2::ScriptParser {
public:
ScriptParser(BumpPtrAllocator *A, StringRef S, bool B)
: Saver(*A), Tokens(tokenize(S)), IsUnderSysroot(B) {}
: Saver(*A), Tokens(tokenize(S)), IsUnderSysroot(B) {
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 run();

private:
Expand Down Expand Up @@ -114,37 +127,21 @@ class elf2::ScriptParser {

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::run() {
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();
auto It = Cmd.find(Tok);
if (It != Cmd.end()) {
std::function<void(ScriptParser &)> &Handler = It->second;
Handler(*this);
} else {
setError("unknown directive: " + Tok);
return;
}
}
}
Expand Down

0 comments on commit f23b232

Please sign in to comment.