Skip to content

Commit c431221

Browse files
committed
[Diagnostics] Emit generated code buffers into diagnostic files.
Extend the serialized diagnostics file format to also support providing the contents of source files, which can include a reference to the "original source range" whose text is conceptually replaced with the contents of the serialized diagnostics file, e.g., due to macro expansion.
1 parent 497a8dc commit c431221

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

lib/Frontend/SerializedDiagnosticConsumer.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ class SerializedDiagnosticConsumer : public DiagnosticConsumer {
188188
}
189189

190190
// Record identifier for the file.
191-
unsigned getEmitFile(StringRef Filename);
191+
unsigned getEmitFile(
192+
SourceManager &SM, StringRef Filename, unsigned bufferID
193+
);
192194

193195
// Record identifier for the category.
194196
unsigned getEmitCategory(StringRef Category);
@@ -217,7 +219,9 @@ namespace serialized_diagnostics {
217219
} // namespace serialized_diagnostics
218220
} // namespace swift
219221

220-
unsigned SerializedDiagnosticConsumer::getEmitFile(StringRef Filename) {
222+
unsigned SerializedDiagnosticConsumer::getEmitFile(
223+
SourceManager &SM, StringRef Filename, unsigned bufferID
224+
) {
221225
// NOTE: Using Filename.data() here relies on SourceMgr using
222226
// const char* as buffer identifiers. This is fast, but may
223227
// be brittle. We can always switch over to using a StringMap.
@@ -238,6 +242,32 @@ unsigned SerializedDiagnosticConsumer::getEmitFile(StringRef Filename) {
238242
State->Stream.EmitRecordWithBlob(State->Abbrevs.get(RECORD_FILENAME),
239243
Record, Filename.data());
240244

245+
// If the buffer contains code that was synthesized by the compiler,
246+
// emit the contents of the buffer.
247+
auto generatedInfo = SM.getGeneratedSourceInfo(bufferID);
248+
if (!generatedInfo)
249+
return entry;
250+
251+
Record.clear();
252+
Record.push_back(RECORD_SOURCE_FILE_CONTENTS);
253+
Record.push_back(entry);
254+
255+
// The source range that this buffer was generated from, expressed as
256+
// offsets into the original buffer.
257+
auto originalFilename = SM.getDisplayNameForLoc(generatedInfo->originalSourceRange.Start);
258+
addRangeToRecord(
259+
Lexer::getCharSourceRangeFromSourceRange(
260+
SM, generatedInfo->originalSourceRange),
261+
SM, originalFilename, Record
262+
);
263+
264+
// Contents of the buffer.
265+
auto sourceText = SM.getEntireTextForBuffer(bufferID);
266+
Record.push_back(sourceText.size());
267+
State->Stream.EmitRecordWithBlob(
268+
State->Abbrevs.get(RECORD_SOURCE_FILE_CONTENTS),
269+
Record, sourceText);
270+
241271
return entry;
242272
}
243273

@@ -275,7 +305,7 @@ void SerializedDiagnosticConsumer::addLocToRecord(SourceLoc Loc,
275305
unsigned line, col;
276306
std::tie(line, col) = SM.getPresumedLineAndColumnForLoc(Loc);
277307

278-
Record.push_back(getEmitFile(Filename));
308+
Record.push_back(getEmitFile(SM, Filename, bufferId));
279309
Record.push_back(line);
280310
Record.push_back(col);
281311
Record.push_back(SM.getLocOffsetInBuffer(Loc, bufferId));
@@ -411,6 +441,8 @@ void SerializedDiagnosticConsumer::emitBlockInfoBlock() {
411441
emitRecordID(RECORD_DIAG_FLAG, "DiagFlag", Stream, Record);
412442
emitRecordID(RECORD_FILENAME, "FileName", Stream, Record);
413443
emitRecordID(RECORD_FIXIT, "FixIt", Stream, Record);
444+
emitRecordID(
445+
RECORD_SOURCE_FILE_CONTENTS, "SourceFileContents", Stream, Record);
414446

415447
// Emit abbreviation for RECORD_DIAG.
416448
Abbrev = std::make_shared<BitCodeAbbrev>();
@@ -467,6 +499,16 @@ void SerializedDiagnosticConsumer::emitBlockInfoBlock() {
467499
Abbrevs.set(RECORD_FIXIT, Stream.EmitBlockInfoAbbrev(BLOCK_DIAG,
468500
Abbrev));
469501

502+
// Emit the abbreviation for RECORD_SOURCE_FILE_CONTENTS.
503+
Abbrev = std::make_shared<BitCodeAbbrev>();
504+
Abbrev->Add(BitCodeAbbrevOp(RECORD_SOURCE_FILE_CONTENTS));
505+
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10)); // File ID.
506+
addRangeLocationAbbrev(Abbrev);
507+
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // File size.
508+
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File contents.
509+
Abbrevs.set(RECORD_SOURCE_FILE_CONTENTS,
510+
Stream.EmitBlockInfoAbbrev(BLOCK_DIAG, Abbrev));
511+
470512
Stream.ExitBlock();
471513
}
472514

test/Macros/macro_expand.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
33
// RUNx: %target-swift-frontend -dump-ast -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser 2>&1 | %FileCheck --check-prefix CHECK-AST %s
4+
5+
// Diagnostics testing
46
// RUN: %target-typecheck-verify-swift -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS
7+
8+
// RUN: not %target-swift-frontend -typecheck -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s
9+
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK-DIAGS %s
10+
11+
// Execution testing
512
// RUN: %target-build-swift -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser
613
// RUN: %target-run %t/main | %FileCheck %s
714
// REQUIRES: executable_test
@@ -75,6 +82,11 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
7582
// expected-note@-1{{in expansion of macro 'addBlocker' here}}
7683
// expected-note@-2{{use '-'}}{{22-23=-}}
7784

85+
// CHECK-DIAGS: macro_expand.swift:81:7-81:27:1:4: error: binary operator '-' cannot be applied to two 'OnlyAdds' operands [] []
86+
// CHECK-DIAGS: CONTENTS OF FILE{{.*}}addBlocker
87+
// CHECK-DIAGS-NEXT: Original source range: {{.*}}macro_expand.swift:81:7 - {{.*}}macro_expand.swift:81:27
88+
// CHECK-DIAGS-NEXT: oa - oa
89+
// CHECK-DIAGS-NEXT: END CONTENTS OF FILE
7890
// Check recursion.
7991
#recurse(false) // okay
8092
#recurse(true) // expected-note{{in expansion of macro 'recurse' here}}

0 commit comments

Comments
 (0)