Skip to content

Commit e0acf44

Browse files
Merge pull request #2920 from adrian-prantl/better-diagnostics-next
Better diagnostics next
2 parents 0b4611b + 7e1ce5c commit e0acf44

File tree

7 files changed

+83
-48
lines changed

7 files changed

+83
-48
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,11 +1483,7 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
14831483
retry = true;
14841484
},
14851485
[&](const SwiftASTContextError &SACE) {
1486-
if (swift_ast_ctx->GetClangImporter())
1487-
DiagnoseSwiftASTContextError();
1488-
else
1489-
// Discard the shared scratch context and retry.
1490-
retry = true;
1486+
DiagnoseSwiftASTContextError();
14911487
},
14921488
[&](const StringError &SE) {
14931489
diagnostic_manager.PutString(eDiagnosticSeverityError,

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,52 +2859,51 @@ class StoringDiagnosticConsumer : public swift::DiagnosticConsumer {
28592859
const DiagnosticSeverity severity = SeverityForKind(diagnostic.kind);
28602860
const DiagnosticOrigin origin = eDiagnosticOriginSwift;
28612861

2862-
if (first_line > 0 && bufferID != UINT32_MAX &&
2863-
diagnostic.bufferID == bufferID && !diagnostic.bufferName.empty()) {
2864-
// Make sure the error line is in range.
2865-
if (diagnostic.line >= first_line && diagnostic.line <= last_line) {
2866-
// Need to remap the error/warning to a different line.
2867-
StreamString match;
2868-
match.Printf("%s:%u:", diagnostic.bufferName.str().c_str(),
2869-
diagnostic.line);
2870-
const size_t match_len = match.GetString().size();
2871-
size_t match_pos =
2872-
diagnostic.description.find(match.GetString().str());
2873-
if (match_pos != std::string::npos) {
2874-
// We have some <file>:<line>:" instances that need to be updated.
2875-
StreamString fixed_description;
2876-
size_t start_pos = 0;
2877-
do {
2878-
if (match_pos > start_pos)
2879-
fixed_description.Printf(
2880-
"%s", diagnostic.description.substr(start_pos, match_pos)
2881-
.c_str());
2882-
fixed_description.Printf(
2883-
"%s:%u:", diagnostic.bufferName.str().c_str(),
2884-
diagnostic.line - first_line + 1);
2885-
start_pos = match_pos + match_len;
2886-
match_pos = diagnostic.description.find(match.GetString().str(),
2887-
start_pos);
2888-
} while (match_pos != std::string::npos);
2889-
2890-
// Append any last remaining text.
2891-
if (start_pos < diagnostic.description.size())
2862+
if (first_line > 0 && bufferID != UINT32_MAX) {
2863+
// Make sure the error line is in range or in another file.
2864+
if (diagnostic.bufferID == bufferID && !diagnostic.bufferName.empty() &&
2865+
(diagnostic.line < first_line || diagnostic.line > last_line))
2866+
continue;
2867+
// Need to remap the error/warning to a different line.
2868+
StreamString match;
2869+
match.Printf("%s:%u:", diagnostic.bufferName.str().c_str(),
2870+
diagnostic.line);
2871+
const size_t match_len = match.GetString().size();
2872+
size_t match_pos = diagnostic.description.find(match.GetString().str());
2873+
if (match_pos != std::string::npos) {
2874+
// We have some <file>:<line>:" instances that need to be updated.
2875+
StreamString fixed_description;
2876+
size_t start_pos = 0;
2877+
do {
2878+
if (match_pos > start_pos)
28922879
fixed_description.Printf(
2893-
"%s", diagnostic.description
2894-
.substr(start_pos,
2895-
diagnostic.description.size() - start_pos)
2896-
.c_str());
2897-
2898-
auto new_diagnostic = std::make_unique<SwiftDiagnostic>(
2899-
fixed_description.GetData(), severity, origin, bufferID);
2900-
for (auto fixit : diagnostic.fixits)
2901-
new_diagnostic->AddFixIt(fixit);
2902-
2903-
diagnostic_manager.AddDiagnostic(std::move(new_diagnostic));
2880+
"%s",
2881+
diagnostic.description.substr(start_pos, match_pos).c_str());
2882+
fixed_description.Printf(
2883+
"%s:%u:", diagnostic.bufferName.str().c_str(),
2884+
diagnostic.line - first_line + 1);
2885+
start_pos = match_pos + match_len;
2886+
match_pos =
2887+
diagnostic.description.find(match.GetString().str(), start_pos);
2888+
} while (match_pos != std::string::npos);
2889+
2890+
// Append any last remaining text.
2891+
if (start_pos < diagnostic.description.size())
2892+
fixed_description.Printf(
2893+
"%s", diagnostic.description
2894+
.substr(start_pos,
2895+
diagnostic.description.size() - start_pos)
2896+
.c_str());
2897+
2898+
auto new_diagnostic = std::make_unique<SwiftDiagnostic>(
2899+
fixed_description.GetData(), severity, origin, bufferID);
2900+
for (auto fixit : diagnostic.fixits)
2901+
new_diagnostic->AddFixIt(fixit);
2902+
2903+
diagnostic_manager.AddDiagnostic(std::move(new_diagnostic));
2904+
if (diagnostic.kind == swift::DiagnosticKind::Error)
29042905
added_one_diagnostic = true;
29052906

2906-
continue;
2907-
}
29082907
}
29092908
}
29102909
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFTFLAGS_EXTRAS = -Xcc -I$(SRCDIR)
3+
4+
include Makefile.rules
5+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
import unittest2
6+
7+
class TestSwiftExprImport(TestBase):
8+
9+
mydir = TestBase.compute_mydir(__file__)
10+
11+
def setUp(self):
12+
TestBase.setUp(self)
13+
14+
# Don't run ClangImporter tests if Clangimporter is disabled.
15+
@skipIf(setting=('symbols.use-swift-clangimporter', 'false'))
16+
@swiftTest
17+
def test(self):
18+
"""Test error handling if the expression evaluator
19+
encounters a Clang import failure.
20+
"""
21+
self.build()
22+
23+
lldbutil.run_to_source_breakpoint(self, "break here",
24+
lldb.SBFileSpec('main.swift'))
25+
self.expect("expr -- import A", error=True,
26+
substrs=['SYNTAX_ERROR',
27+
'could', 'not', 'build', 'module'])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int i = SYNTAX_ERROR;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func f() {}
2+
3+
print("break here")
4+
f()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module A {
2+
header "a.h"
3+
}

0 commit comments

Comments
 (0)