Skip to content

Add source buffers pretty-printed for diagnostics to serialized diagnostic files #62878

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
7 changes: 3 additions & 4 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ class GeneratedSourceInfo {

/// A new function body that is replacing an existing function body.
ReplacedFunctionBody,
} kind;

/// The buffer ID for the enclosing buffer, in which originalSourceRange
/// resides.
unsigned originalBufferID;
/// Pretty-printed declarations that have no source location.
PrettyPrinted,
} kind;

/// The source range in the enclosing buffer where this source was generated.
///
Expand Down
22 changes: 21 additions & 1 deletion lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
SmallVector<std::pair<const Decl *, uint64_t>, 8> entries;
llvm::SmallString<128> buffer;
llvm::SmallString<128> bufferName;
const Decl *ppDecl = decl;
{
// The access level of the buffer we want to print. Declarations below
// this access level will be omitted from the buffer; declarations
Expand All @@ -1152,7 +1153,6 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {

// Figure out which declaration to print. It's the top-most
// declaration (not a module).
const Decl *ppDecl = decl;
auto dc = decl->getDeclContext();

// FIXME: Horrible, horrible hackaround. We're not getting a
Expand Down Expand Up @@ -1237,6 +1237,20 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
auto bufferID = SourceMgr.addMemBufferCopy(buffer, bufferName);
auto memBufferStartLoc = SourceMgr.getLocForBufferStart(bufferID);

SourceMgr.setGeneratedSourceInfo(
bufferID,
GeneratedSourceInfo{
GeneratedSourceInfo::PrettyPrinted,
SourceRange(),
SourceRange(
memBufferStartLoc,
memBufferStartLoc.getAdvancedLoc(buffer.size())
),
ASTNode(const_cast<Decl *>(ppDecl)).getOpaqueValue(),
nullptr
}
);

// Go through all of the pretty-printed entries and record their
// locations.
for (auto entry : entries) {
Expand Down Expand Up @@ -1318,12 +1332,18 @@ std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
break;
}

case GeneratedSourceInfo::PrettyPrinted:
break;

case GeneratedSourceInfo::ReplacedFunctionBody:
return childNotes;
}

// Walk up the stack.
currentLoc = expansionNode.getStartLoc();
if (currentLoc.isInvalid())
return childNotes;

currentBufferID = SourceMgr.findBufferContainingLoc(currentLoc);
} while (true);
}
Expand Down
1 change: 1 addition & 0 deletions lib/Basic/SourceLoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ void SourceManager::setGeneratedSourceInfo(

switch (info.kind) {
case GeneratedSourceInfo::MacroExpansion:
case GeneratedSourceInfo::PrettyPrinted:
break;

case GeneratedSourceInfo::ReplacedFunctionBody:
Expand Down
1 change: 0 additions & 1 deletion lib/IDETool/CompileInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ void retypeCheckFunctionBody(AbstractFunctionDecl *func,
sliceBufferID,
GeneratedSourceInfo{
GeneratedSourceInfo::ReplacedFunctionBody,
*func->getParentSourceFile()->getBufferID(),
func->getOriginalBodySourceRange(),
newRange,
func,
Expand Down
1 change: 0 additions & 1 deletion lib/IDETool/IDEInspectionInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ bool IDEInspectionInstance::performCachedOperationIfPossible(
newBufferID,
GeneratedSourceInfo{
GeneratedSourceInfo::ReplacedFunctionBody,
*AFD->getParentSourceFile()->getBufferID(),
AFD->getOriginalBodySourceRange(),
newBodyRange,
AFD,
Expand Down
1 change: 0 additions & 1 deletion lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ Expr *swift::expandMacroExpr(
auto macroBufferRange = sourceMgr.getRangeForBuffer(macroBufferID);
GeneratedSourceInfo sourceInfo{
GeneratedSourceInfo::MacroExpansion,
*sourceFile->getBufferID(),
expr->getSourceRange(),
SourceRange(macroBufferRange.getStart(), macroBufferRange.getEnd()),
ASTNode(expr).getOpaqueValue(),
Expand Down
3 changes: 3 additions & 0 deletions test/ImportResolution/import-specific-fixits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ import struct ambiguous.funcOrVar // expected-error{{ambiguous name 'funcOrVar'
// CHECK-NEXT: Number FIXITs = 0
// CHECK-NEXT: note: found this candidate
// CHECK-NEXT: Number FIXITs = 0
// CHECK-NEXT: CONTENTS OF FILE ambiguous_right.funcOrVar:
// CHECK: public var funcOrVar: Int
// CHECK: END CONTENTS OF FILE
// CHECK-NEXT: note: found this candidate

import func ambiguous.someVar // expected-error{{ambiguous name 'someVar' in module 'ambiguous'}}
Expand Down
14 changes: 14 additions & 0 deletions test/Misc/serialized-diagnostics-prettyprint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: rm -f %t.*

// Test swift executable
// RUN: %target-swift-frontend -typecheck -serialize-diagnostics-path %t.dia %s -verify
// RUN: c-index-test -read-diagnostics %t.dia > %t.deserialized_diagnostics.txt 2>&1
// RUN: %FileCheck --input-file=%t.deserialized_diagnostics.txt %s

var x = String.init // expected-error{{ambiguous use of 'init'}}
// CHECK: {{.*[/\\]}}serialized-diagnostics-prettyprint.swift:[[@LINE-1]]:9: error: ambiguous use of 'init'

// CHECK: Swift.String:2:23: note: found this candidate
// CHECK: CONTENTS OF FILE Swift.String:
// CHECK: extension String {
// CHECK: public init(_ content: Substring.UnicodeScalarView)