forked from OSchip/llvm-project
Split getPos into getLineNumber and getColumnNumber.
llvm-svn: 288306
This commit is contained in:
parent
a7d9378761
commit
50fb82743e
|
@ -20,23 +20,22 @@ using namespace llvm;
|
||||||
using namespace lld;
|
using namespace lld;
|
||||||
using namespace lld::elf;
|
using namespace lld::elf;
|
||||||
|
|
||||||
// Returns the line that the token Tok is in.
|
// Returns a line containing a token.
|
||||||
static StringRef getLine(StringRef Data, StringRef Tok) {
|
static StringRef getLine(StringRef S, StringRef Tok) {
|
||||||
size_t Pos = Tok.data() - Data.data();
|
size_t Pos = S.rfind('\n', Tok.data() - S.data());
|
||||||
size_t Begin = Data.rfind('\n', Pos);
|
if (Pos != StringRef::npos)
|
||||||
size_t End = Data.find('\n', Pos);
|
S = S.substr(Pos + 1);
|
||||||
Begin = (Begin == StringRef::npos) ? 0 : Begin + 1;
|
return S.substr(0, S.find_first_of("\r\n"));
|
||||||
if (End == StringRef::npos)
|
|
||||||
End = Data.size();
|
|
||||||
// rtrim for DOS-style newlines.
|
|
||||||
return Data.substr(Begin, End - Begin).rtrim();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<size_t, size_t> getPos(StringRef Data, StringRef Tok) {
|
// Returns 1-based line number of a given token.
|
||||||
StringRef Line = getLine(Data, Tok);
|
static size_t getLineNumber(StringRef S, StringRef Tok) {
|
||||||
size_t LineNo =
|
return S.substr(0, Tok.data() - S.data()).count('\n') + 1;
|
||||||
StringRef(Data.data(), Tok.data() - Data.data()).count('\n') + 1;
|
}
|
||||||
return {LineNo, Tok.data() - Line.data()};
|
|
||||||
|
// Returns 0-based column number of a given token.
|
||||||
|
static size_t getColumnNumber(StringRef S, StringRef Tok) {
|
||||||
|
return Tok.data() - getLine(S, Tok).data();
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptParserBase::ScriptParserBase(MemoryBufferRef MB) { tokenize(MB); }
|
ScriptParserBase::ScriptParserBase(MemoryBufferRef MB) { tokenize(MB); }
|
||||||
|
@ -45,22 +44,23 @@ ScriptParserBase::ScriptParserBase(MemoryBufferRef MB) { tokenize(MB); }
|
||||||
void ScriptParserBase::setError(const Twine &Msg) {
|
void ScriptParserBase::setError(const Twine &Msg) {
|
||||||
if (Error)
|
if (Error)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::pair<size_t, size_t> ErrPos;
|
|
||||||
MemoryBufferRef MB = currentBuffer();
|
|
||||||
std::string Location = MB.getBufferIdentifier();
|
|
||||||
if (Pos) {
|
|
||||||
ErrPos = getPos(MB.getBuffer(), Tokens[Pos - 1]);
|
|
||||||
Location += ":";
|
|
||||||
Location += std::to_string(ErrPos.first);
|
|
||||||
}
|
|
||||||
error(Location + ": " + Msg);
|
|
||||||
if (Pos) {
|
|
||||||
error(Location + ": " + getLine(MB.getBuffer(), Tokens[Pos - 1]));
|
|
||||||
error(Location + ": " + std::string(ErrPos.second, ' ') + "^");
|
|
||||||
}
|
|
||||||
|
|
||||||
Error = true;
|
Error = true;
|
||||||
|
|
||||||
|
MemoryBufferRef MB = currentBuffer();
|
||||||
|
std::string Filename = MB.getBufferIdentifier();
|
||||||
|
|
||||||
|
if (!Pos) {
|
||||||
|
error(Filename + ": " + Msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringRef Buf = MB.getBuffer();
|
||||||
|
StringRef Tok = Tokens[Pos - 1];
|
||||||
|
std::string S = (Filename + ":" + Twine(getLineNumber(Buf, Tok))).str();
|
||||||
|
|
||||||
|
error(S + ": " + Msg);
|
||||||
|
error(S + ": " + getLine(Buf, Tok));
|
||||||
|
error(S + ": " + std::string(getColumnNumber(Buf, Tok), ' ') + "^");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split S into linker script tokens.
|
// Split S into linker script tokens.
|
||||||
|
@ -81,8 +81,7 @@ void ScriptParserBase::tokenize(MemoryBufferRef MB) {
|
||||||
if (S.startswith("\"")) {
|
if (S.startswith("\"")) {
|
||||||
size_t E = S.find("\"", 1);
|
size_t E = S.find("\"", 1);
|
||||||
if (E == StringRef::npos) {
|
if (E == StringRef::npos) {
|
||||||
auto ErrPos = getPos(Begin, S);
|
error(MB.getBufferIdentifier() + ":" + Twine(getLineNumber(Begin, S)) +
|
||||||
error(MB.getBufferIdentifier() + ":" + Twine(ErrPos.first) +
|
|
||||||
": unclosed quote");
|
": unclosed quote");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -175,7 +174,7 @@ void ScriptParserBase::expect(StringRef Expect) {
|
||||||
std::string ScriptParserBase::currentLocation() {
|
std::string ScriptParserBase::currentLocation() {
|
||||||
MemoryBufferRef MB = currentBuffer();
|
MemoryBufferRef MB = currentBuffer();
|
||||||
return (MB.getBufferIdentifier() + ":" +
|
return (MB.getBufferIdentifier() + ":" +
|
||||||
Twine(getPos(MB.getBuffer(), Tokens[Pos - 1]).first))
|
Twine(getLineNumber(MB.getBuffer(), Tokens[Pos - 1])))
|
||||||
.str();
|
.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue