Added offset and length to CompilationMessage

Bug: dawn:746
Change-Id: I1811832f63a42121dfb67acb87b0b593000a90a1
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/49504
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Brandon Jones <bajones@chromium.org>
This commit is contained in:
Brandon Jones 2021-04-30 20:45:58 +00:00 committed by Commit Bot service account
parent 4043ee9c06
commit 83ae2cec69
3 changed files with 49 additions and 8 deletions

View File

@ -540,7 +540,9 @@
{"name": "message", "type": "char", "annotation": "const*", "length": "strlen", "optional": true}, {"name": "message", "type": "char", "annotation": "const*", "length": "strlen", "optional": true},
{"name": "type", "type": "compilation message type"}, {"name": "type", "type": "compilation message type"},
{"name": "line num", "type": "uint64_t"}, {"name": "line num", "type": "uint64_t"},
{"name": "line pos", "type": "uint64_t"} {"name": "line pos", "type": "uint64_t"},
{"name": "offset", "type": "uint64_t"},
{"name": "length", "type": "uint64_t"}
] ]
}, },
"compilation message type": { "compilation message type": {

View File

@ -44,27 +44,64 @@ namespace dawn_native {
void OwnedCompilationMessages::AddMessage(std::string message, void OwnedCompilationMessages::AddMessage(std::string message,
wgpu::CompilationMessageType type, wgpu::CompilationMessageType type,
uint64_t lineNum, uint64_t lineNum,
uint64_t linePos) { uint64_t linePos,
uint64_t offset,
uint64_t length) {
// Cannot add messages after GetCompilationInfo has been called. // Cannot add messages after GetCompilationInfo has been called.
ASSERT(mCompilationInfo.messages == nullptr); ASSERT(mCompilationInfo.messages == nullptr);
mMessageStrings.push_back(message); mMessageStrings.push_back(message);
mMessages.push_back( mMessages.push_back({nullptr, static_cast<WGPUCompilationMessageType>(type), lineNum,
{nullptr, static_cast<WGPUCompilationMessageType>(type), lineNum, linePos}); linePos, offset, length});
} }
void OwnedCompilationMessages::AddMessage(const tint::diag::Diagnostic& diagnostic) { void OwnedCompilationMessages::AddMessage(const tint::diag::Diagnostic& diagnostic) {
// Cannot add messages after GetCompilationInfo has been called. // Cannot add messages after GetCompilationInfo has been called.
ASSERT(mCompilationInfo.messages == nullptr); ASSERT(mCompilationInfo.messages == nullptr);
// Tint line and column values are 1-based.
uint64_t lineNum = diagnostic.source.range.begin.line;
uint64_t linePos = diagnostic.source.range.begin.column;
// The offset is 0-based.
uint64_t offset = 0;
uint64_t length = 0;
if (lineNum && linePos && diagnostic.source.file_content) {
const std::vector<std::string>& lines = diagnostic.source.file_content->lines;
size_t i = 0;
// To find the offset of the message position, loop through each of the first lineNum-1
// lines and add it's length (+1 to account for the line break) to the offset.
for (; i < lineNum - 1; ++i) {
offset += lines[i].length() + 1;
}
// If the end line is on a different line from the beginning line, add the length of the
// lines in between to the ending offset.
uint64_t endLineNum = diagnostic.source.range.end.line;
uint64_t endLinePos = diagnostic.source.range.end.column;
uint64_t endOffset = offset;
for (; i < endLineNum - 1; ++i) {
endOffset += lines[i].length() + 1;
}
// Add the line positions to the offset and endOffset to get their final positions
// within the code string.
offset += linePos - 1;
endOffset += endLinePos - 1;
// The length of the message is the difference between the starting offset and the
// ending offset.
length = endOffset - offset;
}
if (diagnostic.code) { if (diagnostic.code) {
mMessageStrings.push_back(std::string(diagnostic.code) + ": " + diagnostic.message); mMessageStrings.push_back(std::string(diagnostic.code) + ": " + diagnostic.message);
} else { } else {
mMessageStrings.push_back(diagnostic.message); mMessageStrings.push_back(diagnostic.message);
} }
mMessages.push_back({nullptr, tintSeverityToMessageType(diagnostic.severity),
diagnostic.source.range.begin.line, mMessages.push_back({nullptr, tintSeverityToMessageType(diagnostic.severity), lineNum,
diagnostic.source.range.begin.column}); linePos, offset, length});
} }
void OwnedCompilationMessages::AddMessages(const tint::diag::List& diagnostics) { void OwnedCompilationMessages::AddMessages(const tint::diag::List& diagnostics) {

View File

@ -37,7 +37,9 @@ namespace dawn_native {
void AddMessage(std::string message, void AddMessage(std::string message,
wgpu::CompilationMessageType type = wgpu::CompilationMessageType::Info, wgpu::CompilationMessageType type = wgpu::CompilationMessageType::Info,
uint64_t lineNum = 0, uint64_t lineNum = 0,
uint64_t linePos = 0); uint64_t linePos = 0,
uint64_t offset = 0,
uint64_t length = 0);
void AddMessage(const tint::diag::Diagnostic& diagnostic); void AddMessage(const tint::diag::Diagnostic& diagnostic);
void AddMessages(const tint::diag::List& diagnostics); void AddMessages(const tint::diag::List& diagnostics);
void ClearMessages(); void ClearMessages();