Skip to content

Commit e5d86d4

Browse files
committed
Match diagnostic output formatting from the PrintDiagnosticConsumer
1 parent bc58ed4 commit e5d86d4

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

include/swift/Frontend/AccumulatingDiagnosticConsumer.h

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,67 @@ namespace swift {
3030
/// collection.
3131
class AccumulatingFileDiagnosticConsumer : public DiagnosticConsumer {
3232
std::vector<std::string> &Diagnostics;
33+
3334
public:
3435
AccumulatingFileDiagnosticConsumer(std::vector<std::string> &DiagBuffer)
3536
: Diagnostics(DiagBuffer) {}
3637

3738
private:
3839
void handleDiagnostic(SourceManager &SM,
3940
const DiagnosticInfo &Info) override {
40-
std::string DiagMsg;
41-
llvm::raw_string_ostream DiagOS(DiagMsg);
42-
DiagnosticEngine::formatDiagnosticText(DiagOS, Info.FormatString,
43-
Info.FormatArgs);
44-
auto LC = SM.getPresumedLineAndColumnForLoc(Info.Loc);
45-
std::ostringstream StrOS;
46-
StrOS << LC.first << ", " << LC.second << ": " << DiagOS.str();
47-
Diagnostics.push_back(StrOS.str());
41+
addDiagnostic(SM, Info);
42+
43+
for (auto ChildInfo : Info.ChildDiagnosticInfo) {
44+
addDiagnostic(SM, *ChildInfo);
45+
}
46+
}
47+
48+
// TODO: Support Swift-style diagnostic formatting
49+
void addDiagnostic(SourceManager &SM, const DiagnosticInfo &Info) {
50+
// Determine what kind of diagnostic we're emitting.
51+
llvm::SourceMgr::DiagKind SMKind;
52+
switch (Info.Kind) {
53+
case DiagnosticKind::Error:
54+
SMKind = llvm::SourceMgr::DK_Error;
55+
break;
56+
case DiagnosticKind::Warning:
57+
SMKind = llvm::SourceMgr::DK_Warning;
58+
break;
59+
60+
case DiagnosticKind::Note:
61+
SMKind = llvm::SourceMgr::DK_Note;
62+
break;
63+
64+
case DiagnosticKind::Remark:
65+
SMKind = llvm::SourceMgr::DK_Remark;
66+
break;
67+
}
68+
69+
// Translate ranges.
70+
SmallVector<llvm::SMRange, 2> Ranges;
71+
for (auto R : Info.Ranges)
72+
Ranges.push_back(getRawRange(SM, R));
73+
74+
// Translate fix-its.
75+
SmallVector<llvm::SMFixIt, 2> FixIts;
76+
for (DiagnosticInfo::FixIt F : Info.FixIts)
77+
FixIts.push_back(getRawFixIt(SM, F));
78+
79+
// Actually substitute the diagnostic arguments into the diagnostic text.
80+
llvm::SmallString<256> Text;
81+
{
82+
llvm::raw_svector_ostream Out(Text);
83+
DiagnosticEngine::formatDiagnosticText(Out, Info.FormatString,
84+
Info.FormatArgs);
85+
}
86+
87+
const llvm::SourceMgr &rawSM = SM.getLLVMSourceMgr();
88+
auto Msg = SM.GetMessage(Info.Loc, SMKind, Text, Ranges, FixIts);
89+
std::string result;
90+
llvm::raw_string_ostream os(result);
91+
rawSM.PrintMessage(os, Msg, false);
92+
os.flush();
93+
Diagnostics.push_back(result);
4894
}
4995
};
5096
}

lib/Basic/ParseableOutput.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ void parseable_output::emitFinishedMessage(
464464

465465
// Join all diagnostics produced for this file into a single output.
466466
auto PrimaryDiags = FileSpecificDiagnostics.lookup(Input.getFileName());
467-
const char *const Delim = "\n";
467+
const char *const Delim = "";
468468
std::ostringstream JoinedDiags;
469469
std::copy(PrimaryDiags.begin(), PrimaryDiags.end(),
470470
std::ostream_iterator<std::string>(JoinedDiags, Delim));

test/Frontend/parseable_output_error.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func foo() {
5454
// CHECK-NEXT: {
5555
// CHECK-NEXT: "kind": "finished",
5656
// CHECK-NEXT: "name": "compile",
57-
// CHECK-NEXT: "output": "5, 12: unexpected non-void return value in void function\n5, 12: did you mean to add a return type?\n",
57+
// CHECK-NEXT: "output": "{{.*[\\/]}}parseable_output_error.swift:5:12: error: unexpected non-void return value in void function{{.*}}return 11;{{.*[\\/]}}parseable_output_error.swift:5:12: note: did you mean to add a return type?{{.*}}return 11;
5858
// CHECK-NEXT: "process": {
5959
// CHECK-NEXT: "real_pid": [[PID]]
6060
// CHECK-NEXT: },

0 commit comments

Comments
 (0)