Skip to content

Better diagnostics next #2920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1481,11 +1481,7 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
retry = true;
},
[&](const SwiftASTContextError &SACE) {
if (swift_ast_ctx->GetClangImporter())
DiagnoseSwiftASTContextError();
else
// Discard the shared scratch context and retry.
retry = true;
DiagnoseSwiftASTContextError();
},
[&](const StringError &SE) {
diagnostic_manager.PutString(eDiagnosticSeverityError,
Expand Down
85 changes: 42 additions & 43 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2858,52 +2858,51 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
const DiagnosticSeverity severity = SeverityForKind(diagnostic.kind);
const DiagnosticOrigin origin = eDiagnosticOriginSwift;

if (first_line > 0 && bufferID != UINT32_MAX &&
diagnostic.bufferID == bufferID && !diagnostic.bufferName.empty()) {
// Make sure the error line is in range.
if (diagnostic.line >= first_line && diagnostic.line <= last_line) {
// Need to remap the error/warning to a different line.
StreamString match;
match.Printf("%s:%u:", diagnostic.bufferName.str().c_str(),
diagnostic.line);
const size_t match_len = match.GetString().size();
size_t match_pos =
diagnostic.description.find(match.GetString().str());
if (match_pos != std::string::npos) {
// We have some <file>:<line>:" instances that need to be updated.
StreamString fixed_description;
size_t start_pos = 0;
do {
if (match_pos > start_pos)
fixed_description.Printf(
"%s", diagnostic.description.substr(start_pos, match_pos)
.c_str());
fixed_description.Printf(
"%s:%u:", diagnostic.bufferName.str().c_str(),
diagnostic.line - first_line + 1);
start_pos = match_pos + match_len;
match_pos = diagnostic.description.find(match.GetString().str(),
start_pos);
} while (match_pos != std::string::npos);

// Append any last remaining text.
if (start_pos < diagnostic.description.size())
if (first_line > 0 && bufferID != UINT32_MAX) {
// Make sure the error line is in range or in another file.
if (diagnostic.bufferID == bufferID && !diagnostic.bufferName.empty() &&
(diagnostic.line < first_line || diagnostic.line > last_line))
continue;
// Need to remap the error/warning to a different line.
StreamString match;
match.Printf("%s:%u:", diagnostic.bufferName.str().c_str(),
diagnostic.line);
const size_t match_len = match.GetString().size();
size_t match_pos = diagnostic.description.find(match.GetString().str());
if (match_pos != std::string::npos) {
// We have some <file>:<line>:" instances that need to be updated.
StreamString fixed_description;
size_t start_pos = 0;
do {
if (match_pos > start_pos)
fixed_description.Printf(
"%s", diagnostic.description
.substr(start_pos,
diagnostic.description.size() - start_pos)
.c_str());

auto new_diagnostic = std::make_unique<SwiftDiagnostic>(
fixed_description.GetData(), severity, origin, bufferID);
for (auto fixit : diagnostic.fixits)
new_diagnostic->AddFixIt(fixit);

diagnostic_manager.AddDiagnostic(std::move(new_diagnostic));
"%s",
diagnostic.description.substr(start_pos, match_pos).c_str());
fixed_description.Printf(
"%s:%u:", diagnostic.bufferName.str().c_str(),
diagnostic.line - first_line + 1);
start_pos = match_pos + match_len;
match_pos =
diagnostic.description.find(match.GetString().str(), start_pos);
} while (match_pos != std::string::npos);

// Append any last remaining text.
if (start_pos < diagnostic.description.size())
fixed_description.Printf(
"%s", diagnostic.description
.substr(start_pos,
diagnostic.description.size() - start_pos)
.c_str());

auto new_diagnostic = std::make_unique<SwiftDiagnostic>(
fixed_description.GetData(), severity, origin, bufferID);
for (auto fixit : diagnostic.fixits)
new_diagnostic->AddFixIt(fixit);

diagnostic_manager.AddDiagnostic(std::move(new_diagnostic));
if (diagnostic.kind == swift::DiagnosticKind::Error)
added_one_diagnostic = true;

continue;
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions lldb/test/API/lang/swift/clangimporter/expr_import/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SWIFT_SOURCES := main.swift
SWIFTFLAGS_EXTRAS = -Xcc -I$(SRCDIR)

include Makefile.rules

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import lldb
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbutil as lldbutil
import unittest2

class TestSwiftExprImport(TestBase):

mydir = TestBase.compute_mydir(__file__)

def setUp(self):
TestBase.setUp(self)

# Don't run ClangImporter tests if Clangimporter is disabled.
@skipIf(setting=('symbols.use-swift-clangimporter', 'false'))
@swiftTest
def test(self):
"""Test error handling if the expression evaluator
encounters a Clang import failure.
"""
self.build()

lldbutil.run_to_source_breakpoint(self, "break here",
lldb.SBFileSpec('main.swift'))
self.expect("expr -- import A", error=True,
substrs=['SYNTAX_ERROR',
'could', 'not', 'build', 'module'])
1 change: 1 addition & 0 deletions lldb/test/API/lang/swift/clangimporter/expr_import/a.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int i = SYNTAX_ERROR;
4 changes: 4 additions & 0 deletions lldb/test/API/lang/swift/clangimporter/expr_import/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func f() {}

print("break here")
f()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module A {
header "a.h"
}