[lld] r261317 - [ELF] - Minor refactor of LinkerScript file

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 19 08:19:35 PST 2016


On Fri, Feb 19, 2016 at 2:45 AM, George Rimar via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Author: grimar
> Date: Fri Feb 19 04:45:45 2016
> New Revision: 261317
>
> URL: http://llvm.org/viewvc/llvm-project?rev=261317&view=rev
> Log:
> [ELF] - Minor refactor of LinkerScript file
>
> * Else-ifs in ScriptParser::run() replaced with std::function + map
> * Reordered members of ScriptParser
>
> Differential revision: http://reviews.llvm.org/D17256
>
> Modified:
>     lld/trunk/ELF/LinkerScript.cpp
>
> Modified: lld/trunk/ELF/LinkerScript.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=261317&r1=261316&r2=261317&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/LinkerScript.cpp (original)
> +++ lld/trunk/ELF/LinkerScript.cpp Fri Feb 19 04:45:45 2016
> @@ -85,7 +85,20 @@ template <class ELFT> bool SectionRule::
>  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 &) {};
>

Looks like these are all just function pointers (including the last one,
actually) - would be more efficient, probably, to use a map to function
pointer, instead of std::function, to avoid all the extra std::function
overhead?

(also, you might be able to initialize this with something like:
Cmd({{"Entry", &ScriptParser::readEntry}, {"EXTERN", ...}, ...}) in the
init list, which might be a little more terse)


> +  }
> +
>    void run();
>
>  private:
> @@ -114,37 +127,21 @@ private:
>
>    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;
>      }
>    }
>  }
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160219/3b28c705/attachment.html>


More information about the llvm-commits mailing list