1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
//===--- DiagnosticBridge.cpp - Diagnostic Bridge to swift-syntax ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the DiagnosticBridge class.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/DiagnosticBridge.h"
#include "swift/AST/ASTBridging.h"
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Bridging/ASTGen.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
#if SWIFT_BUILD_SWIFT_SYNTAX
static BridgedDiagnosticSeverity bridgeDiagnosticSeverity(DiagnosticKind kind) {
switch (kind) {
case DiagnosticKind::Error:
return BridgedDiagnosticSeverity::BridgedError;
case DiagnosticKind::Warning:
return BridgedDiagnosticSeverity::BridgedWarning;
case DiagnosticKind::Remark:
return BridgedDiagnosticSeverity::BridgedRemark;
case DiagnosticKind::Note:
return BridgedDiagnosticSeverity::BridgedNote;
}
}
/// Enqueue a diagnostic with ASTGen's diagnostic rendering.
static void addQueueDiagnostic(void *queuedDiagnostics,
void *perFrontendState,
const DiagnosticInfo &info, SourceManager &SM) {
llvm::SmallString<256> text;
{
llvm::raw_svector_ostream out(text);
DiagnosticEngine::formatDiagnosticText(out, info.FormatString,
info.FormatArgs);
}
BridgedDiagnosticSeverity severity = bridgeDiagnosticSeverity(info.Kind);
// Map the highlight ranges.
SmallVector<BridgedCharSourceRange, 2> highlightRanges;
for (const auto &range : info.Ranges) {
if (range.isValid())
highlightRanges.push_back(range);
}
StringRef documentationPath = info.CategoryDocumentationURL;
SmallVector<BridgedFixIt, 2> fixIts;
for (const auto &fixIt : info.FixIts) {
fixIts.push_back(BridgedFixIt{ fixIt.getRange(), fixIt.getText() });
}
swift_ASTGen_addQueuedDiagnostic(
queuedDiagnostics, perFrontendState,
text.str(),
severity, info.Loc,
info.Category,
documentationPath,
highlightRanges.data(), highlightRanges.size(),
llvm::ArrayRef<BridgedFixIt>(fixIts));
// TODO: A better way to do this would be to pass the notes as an
// argument to `swift_ASTGen_addQueuedDiagnostic` but that requires
// bridging of `Note` structure and new serialization.
for (auto *childNote : info.ChildDiagnosticInfo) {
addQueueDiagnostic(queuedDiagnostics, perFrontendState, *childNote, SM);
}
}
void DiagnosticBridge::emitDiagnosticWithoutLocation(
const DiagnosticInfo &info, llvm::raw_ostream &out, bool forceColors) {
ASSERT(queuedDiagnostics == nullptr);
// If we didn't have per-frontend state before, create it now.
if (!perFrontendState) {
perFrontendState = swift_ASTGen_createPerFrontendDiagnosticState();
}
llvm::SmallString<256> text;
{
llvm::raw_svector_ostream out(text);
DiagnosticEngine::formatDiagnosticText(out, info.FormatString,
info.FormatArgs);
}
BridgedDiagnosticSeverity severity = bridgeDiagnosticSeverity(info.Kind);
BridgedStringRef bridgedRenderedString{nullptr, 0};
swift_ASTGen_renderSingleDiagnostic(
perFrontendState, text.str(), severity, info.Category,
llvm::StringRef(info.CategoryDocumentationURL), forceColors ? 1 : 0,
&bridgedRenderedString);
auto renderedString = bridgedRenderedString.unbridged();
if (renderedString.data()) {
out << "<unknown>:0: ";
out.write(renderedString.data(), renderedString.size());
swift_ASTGen_freeBridgedString(renderedString);
out << "\n";
}
}
void DiagnosticBridge::enqueueDiagnostic(SourceManager &SM,
const DiagnosticInfo &Info,
unsigned innermostBufferID) {
// If we didn't have per-frontend state before, create it now.
if (!perFrontendState) {
perFrontendState = swift_ASTGen_createPerFrontendDiagnosticState();
}
// If there are no enqueued diagnostics, or we have hit a non-note
// diagnostic, flush any enqueued diagnostics and start fresh.
if (!queuedDiagnostics)
queuedDiagnostics = swift_ASTGen_createQueuedDiagnostics();
queueBuffer(SM, innermostBufferID);
addQueueDiagnostic(queuedDiagnostics, perFrontendState, Info, SM);
}
void DiagnosticBridge::flush(llvm::raw_ostream &OS, bool includeTrailingBreak,
bool forceColors) {
if (!queuedDiagnostics)
return;
BridgedStringRef bridgedRenderedString{nullptr, 0};
swift_ASTGen_renderQueuedDiagnostics(queuedDiagnostics, /*contextSize=*/2,
forceColors ? 1 : 0,
&bridgedRenderedString);
auto renderedString = bridgedRenderedString.unbridged();
if (renderedString.data()) {
OS.write(renderedString.data(), renderedString.size());
swift_ASTGen_freeBridgedString(renderedString);
}
swift_ASTGen_destroyQueuedDiagnostics(queuedDiagnostics);
queuedDiagnostics = nullptr;
queuedBuffers.clear();
if (includeTrailingBreak)
OS << "\n";
}
void DiagnosticBridge::printCategoryFootnotes(llvm::raw_ostream &os,
bool forceColors) {
if (!perFrontendState)
return;
BridgedStringRef bridgedRenderedString{nullptr, 0};
swift_ASTGen_renderCategoryFootnotes(
perFrontendState, forceColors ? 1 : 0, &bridgedRenderedString);
auto renderedString = bridgedRenderedString.unbridged();
if (auto renderedData = renderedString.data()) {
os.write(renderedData, renderedString.size());
swift_ASTGen_freeBridgedString(renderedString);
}
}
void *DiagnosticBridge::getSourceFileSyntax(SourceManager &sourceMgr,
unsigned bufferID,
StringRef displayName) {
auto known = sourceFileSyntax.find({&sourceMgr, bufferID});
if (known != sourceFileSyntax.end())
return known->second;
auto bufferContents = sourceMgr.getEntireTextForBuffer(bufferID);
auto sourceFile = swift_ASTGen_parseSourceFile(
bufferContents, StringRef{"module"}, displayName,
/*declContextPtr=*/nullptr, BridgedGeneratedSourceFileKindNone);
sourceFileSyntax[{&sourceMgr, bufferID}] = sourceFile;
return sourceFile;
}
void DiagnosticBridge::queueBuffer(SourceManager &sourceMgr,
unsigned bufferID) {
QueuedBuffer knownSourceFile = queuedBuffers[bufferID];
if (knownSourceFile)
return;
// Find the parent and position in parent, if there is one.
int parentID = -1;
int positionInParent = 0;
std::string displayName;
auto generatedSourceInfo = sourceMgr.getGeneratedSourceInfo(bufferID);
if (generatedSourceInfo) {
SourceLoc parentLoc = generatedSourceInfo->originalSourceRange.getEnd();
if (parentLoc.isValid()) {
parentID = sourceMgr.findBufferContainingLoc(parentLoc);
positionInParent = sourceMgr.getLocOffsetInBuffer(parentLoc, parentID);
// Queue the parent buffer.
queueBuffer(sourceMgr, parentID);
}
if (!generatedSourceInfo->macroName.empty()) {
if (generatedSourceInfo->attachedMacroCustomAttr)
displayName = "macro expansion @" + generatedSourceInfo->macroName;
else
displayName = "macro expansion #" + generatedSourceInfo->macroName;
}
}
if (displayName.empty()) {
displayName =
sourceMgr.getDisplayNameForLoc(sourceMgr.getLocForBufferStart(bufferID))
.str();
}
auto sourceFile = getSourceFileSyntax(sourceMgr, bufferID, displayName);
swift_ASTGen_addQueuedSourceFile(queuedDiagnostics, bufferID, sourceFile,
(const uint8_t *)displayName.data(),
displayName.size(), parentID,
positionInParent);
queuedBuffers[bufferID] = sourceFile;
}
SmallVector<unsigned, 1>
DiagnosticBridge::getSourceBufferStack(SourceManager &sourceMgr,
SourceLoc loc) {
SmallVector<unsigned, 1> stack;
while (true) {
if (loc.isInvalid())
return stack;
unsigned bufferID = sourceMgr.findBufferContainingLoc(loc);
stack.push_back(bufferID);
auto generatedSourceInfo = sourceMgr.getGeneratedSourceInfo(bufferID);
if (!generatedSourceInfo)
return stack;
loc = generatedSourceInfo->originalSourceRange.getStart();
}
}
DiagnosticBridge::~DiagnosticBridge() {
if (perFrontendState) {
swift_ASTGen_destroyPerFrontendDiagnosticState(perFrontendState);
}
assert(!queuedDiagnostics && "unflushed diagnostics");
for (const auto &sourceFileSyntax : sourceFileSyntax) {
swift_ASTGen_destroySourceFile(sourceFileSyntax.second);
}
}
#endif // SWIFT_BUILD_SWIFT_SYNTAX
|