//===- ScriptLexer.cpp ----------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file defines a lexer for the linker script. // // The linker script's grammar is not complex but ambiguous due to the // lack of the formal specification of the language. What we are trying to // do in this and other files in LLD is to make a "reasonable" linker // script processor. // // Among simplicity, compatibility and efficiency, we put the most // emphasis on simplicity when we wrote this lexer. Compatibility with the // GNU linkers is important, but we did not try to clone every tiny corner // case of their lexers, as even ld.bfd and ld.gold are subtly different // in various corner cases. We do not care much about efficiency because // the time spent in parsing linker scripts is usually negligible. // // Overall, this lexer works fine for most linker scripts. There might // be room for improving compatibility, but that's probably not at the // top of our todo list. // //===----------------------------------------------------------------------===// #include "ScriptLexer.h" #include "Config.h" #include "lld/Common/ErrorHandler.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include <algorithm> usingnamespacellvm; usingnamespacelld; usingnamespacelld::elf; ScriptLexer::Buffer::Buffer(Ctx &ctx, MemoryBufferRef mb) : … { … } ScriptLexer::ScriptLexer(Ctx &ctx, MemoryBufferRef mb) : … { … } // Returns a whole line containing the current token. StringRef ScriptLexer::getLine() { … } // Returns 0-based column number of the current token. size_t ScriptLexer::getColumnNumber() { … } std::string ScriptLexer::getCurrentLocation() { … } // We don't want to record cascading errors. Keep only the first one. void ScriptLexer::setError(const Twine &msg) { … } void ScriptLexer::lex() { … } // Skip leading whitespace characters or comments. StringRef ScriptLexer::skipSpace(StringRef s) { … } // Used to determine whether to stop parsing. Treat errors like EOF. bool ScriptLexer::atEOF() { … } StringRef ScriptLexer::next() { … } StringRef ScriptLexer::peek() { … } bool ScriptLexer::consume(StringRef tok) { … } void ScriptLexer::skip() { … } void ScriptLexer::expect(StringRef expect) { … } ScriptLexer::Token ScriptLexer::till(StringRef tok) { … } // Returns true if S encloses T. static bool encloses(StringRef s, StringRef t) { … } MemoryBufferRef ScriptLexer::getCurrentMB() { … }