Skip to content

Commit a64fc3b

Browse files
authored
Merge pull request #63931 from DougGregor/swift-syntax-grouped-diagnostics
[Diagnostics] Switch swift-syntax diagnostic style to grouped diagnostics
2 parents 91989ec + 0d6de9e commit a64fc3b

File tree

9 files changed

+306
-114
lines changed

9 files changed

+306
-114
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,18 @@ extern "C" {
118118
/// information and then must be finished via \c SwiftDiagnostic_finish.
119119
BridgedDiagnostic SwiftDiagnostic_create(
120120
void *diagnosticEngine, BridgedDiagnosticSeverity severity,
121-
void *_Nullable sourceLoc,
121+
const void *_Nullable sourceLoc,
122122
const uint8_t *_Nullable text, long textLen);
123123

124124
/// Highlight a source range as part of the diagnostic.
125125
void SwiftDiagnostic_highlight(
126-
BridgedDiagnostic diag, void *_Nullable startLoc, void *_Nullable endLoc);
126+
BridgedDiagnostic diag, const void *_Nullable startLoc, const void *_Nullable endLoc);
127127

128128
/// Add a Fix-It to replace a source range as part of the diagnostic.
129129
void SwiftDiagnostic_fixItReplace(
130130
BridgedDiagnostic diag,
131-
void *_Nullable replaceStartLoc, void *_Nullable replaceEndLoc,
131+
const void *_Nullable replaceStartLoc,
132+
const void *_Nullable replaceEndLoc,
132133
const uint8_t *_Nullable newText, long newTextLen);
133134

134135
/// Finish the given diagnostic and emit it.

include/swift/AST/DiagnosticEngine.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace swift {
4141
class Decl;
4242
class DeclAttribute;
4343
class DiagnosticEngine;
44+
class GeneratedSourceInfo;
4445
class SourceManager;
4546
class ValueDecl;
4647
class SourceFile;
@@ -1478,6 +1479,10 @@ namespace swift {
14781479
std::pair<unsigned, DeclName>
14791480
getAccessorKindAndNameForDiagnostics(const ValueDecl *D);
14801481

1482+
/// Retrieve the macro name for a generated source info that represents
1483+
/// a macro expansion.
1484+
DeclName getGeneratedSourceInfoMacroName(const GeneratedSourceInfo &info);
1485+
14811486
} // end namespace swift
14821487

14831488
#endif

include/swift/Frontend/PrintingDiagnosticConsumer.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/Basic/DiagnosticOptions.h"
2323
#include "swift/Basic/LLVM.h"
2424

25+
#include "llvm/ADT/DenseMap.h"
2526
#include "llvm/Support/raw_ostream.h"
2627
#include "llvm/Support/Process.h"
2728

@@ -47,10 +48,14 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
4748
bool SuppressOutput = false;
4849

4950
/// swift-syntax rendering
51+
52+
/// A queued up source file known to the queued diagnostics.
53+
using QueuedBuffer = void *;
54+
55+
/// The queued diagnostics structure.
5056
void *queuedDiagnostics = nullptr;
51-
void *queuedSourceFile = nullptr;
52-
unsigned queuedDiagnosticsBufferID;
53-
StringRef queuedBufferName;
57+
llvm::DenseMap<unsigned, QueuedBuffer> queuedBuffers;
58+
unsigned queuedDiagnosticsOutermostBufferID;
5459

5560
public:
5661
PrintingDiagnosticConsumer(llvm::raw_ostream &stream = llvm::errs());
@@ -91,6 +96,7 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
9196
}
9297

9398
private:
99+
void queueBuffer(SourceManager &sourceMgr, unsigned bufferID);
94100
void printDiagnostic(SourceManager &SM, const DiagnosticInfo &Info);
95101
};
96102

lib/AST/CASTBridging.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ inline llvm::ArrayRef<T> getArrayRef(BridgedArrayRef bridged) {
2020
return {static_cast<const T *>(bridged.data), size_t(bridged.numElements)};
2121
}
2222

23-
static SourceLoc getSourceLocFromPointer(void *loc) {
23+
static SourceLoc getSourceLocFromPointer(const void *loc) {
2424
auto smLoc = llvm::SMLoc::getFromPointer((const char *)loc);
2525
return SourceLoc(smLoc);
2626
}
@@ -46,7 +46,7 @@ namespace {
4646

4747
BridgedDiagnostic SwiftDiagnostic_create(
4848
void *diagnosticEngine, BridgedDiagnosticSeverity severity,
49-
void *sourceLocPtr,
49+
const void *sourceLocPtr,
5050
const uint8_t *textPtr, long textLen
5151
) {
5252
StringRef origText{
@@ -81,7 +81,7 @@ BridgedDiagnostic SwiftDiagnostic_create(
8181

8282
/// Highlight a source range as part of the diagnostic.
8383
void SwiftDiagnostic_highlight(
84-
BridgedDiagnostic diagPtr, void *startLocPtr, void *endLocPtr
84+
BridgedDiagnostic diagPtr, const void *startLocPtr, const void *endLocPtr
8585
) {
8686
SourceLoc startLoc = getSourceLocFromPointer(startLocPtr);
8787
SourceLoc endLoc = getSourceLocFromPointer(endLocPtr);
@@ -92,7 +92,8 @@ void SwiftDiagnostic_highlight(
9292

9393
/// Add a Fix-It to replace a source range as part of the diagnostic.
9494
void SwiftDiagnostic_fixItReplace(
95-
BridgedDiagnostic diagPtr, void *replaceStartLocPtr, void *replaceEndLocPtr,
95+
BridgedDiagnostic diagPtr,
96+
const void *replaceStartLocPtr, const void *replaceEndLocPtr,
9697
const uint8_t *newTextPtr, long newTextLen) {
9798

9899
SourceLoc startLoc = getSourceLocFromPointer(replaceStartLocPtr);

lib/AST/DiagnosticEngine.cpp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,19 +1316,7 @@ std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
13161316
case GeneratedSourceInfo::PeerMacroExpansion:
13171317
case GeneratedSourceInfo::ConformanceMacroExpansion: {
13181318
SourceRange origRange = expansionNode.getSourceRange();
1319-
DeclName macroName;
1320-
if (auto customAttr = generatedInfo->attachedMacroCustomAttr) {
1321-
// FIXME: How will we handle deserialized custom attributes like this?
1322-
auto declRefType = dyn_cast<DeclRefTypeRepr>(customAttr->getTypeRepr());
1323-
macroName = declRefType->getNameRef().getFullName();
1324-
} else if (auto expansionExpr = dyn_cast_or_null<MacroExpansionExpr>(
1325-
expansionNode.dyn_cast<Expr *>())) {
1326-
macroName = expansionExpr->getMacroName().getFullName();
1327-
} else {
1328-
auto expansionDecl =
1329-
cast<MacroExpansionDecl>(expansionNode.get<Decl *>());
1330-
macroName = expansionDecl->getMacroName().getFullName();
1331-
}
1319+
DeclName macroName = getGeneratedSourceInfoMacroName(*generatedInfo);
13321320

13331321
Diagnostic expansionNote(diag::in_macro_expansion, macroName);
13341322
expansionNote.setLoc(origRange.Start);
@@ -1510,3 +1498,37 @@ swift::getAccessorKindAndNameForDiagnostics(const ValueDecl *D) {
15101498

15111499
return {NOT_ACCESSOR_INDEX, D->getName()};
15121500
}
1501+
1502+
DeclName
1503+
swift::getGeneratedSourceInfoMacroName(const GeneratedSourceInfo &info) {
1504+
ASTNode expansionNode = ASTNode::getFromOpaqueValue(info.astNode);
1505+
switch (info.kind) {
1506+
case GeneratedSourceInfo::ExpressionMacroExpansion:
1507+
case GeneratedSourceInfo::FreestandingDeclMacroExpansion:
1508+
case GeneratedSourceInfo::AccessorMacroExpansion:
1509+
case GeneratedSourceInfo::MemberAttributeMacroExpansion:
1510+
case GeneratedSourceInfo::MemberMacroExpansion:
1511+
case GeneratedSourceInfo::PeerMacroExpansion:
1512+
case GeneratedSourceInfo::ConformanceMacroExpansion: {
1513+
DeclName macroName;
1514+
if (auto customAttr = info.attachedMacroCustomAttr) {
1515+
// FIXME: How will we handle deserialized custom attributes like this?
1516+
auto declRefType = cast<DeclRefTypeRepr>(customAttr->getTypeRepr());
1517+
return declRefType->getNameRef().getFullName();
1518+
}
1519+
1520+
if (auto expansionExpr = dyn_cast_or_null<MacroExpansionExpr>(
1521+
expansionNode.dyn_cast<Expr *>())) {
1522+
return expansionExpr->getMacroName().getFullName();
1523+
}
1524+
1525+
auto expansionDecl =
1526+
cast<MacroExpansionDecl>(expansionNode.get<Decl *>());
1527+
return expansionDecl->getMacroName().getFullName();
1528+
}
1529+
1530+
case GeneratedSourceInfo::PrettyPrinted:
1531+
case GeneratedSourceInfo::ReplacedFunctionBody:
1532+
return DeclName();
1533+
}
1534+
}

0 commit comments

Comments
 (0)