|
| 1 | +//===-- DiagnosticRendering.h -----------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLDB_SOURCE_COMMANDS_DIAGNOSTICRENDERING_H |
| 10 | +#define LLDB_SOURCE_COMMANDS_DIAGNOSTICRENDERING_H |
| 11 | + |
| 12 | +#include "lldb/Expression/DiagnosticManager.h" |
| 13 | +#include "lldb/Utility/Stream.h" |
| 14 | +#include "llvm/Support/WithColor.h" |
| 15 | + |
| 16 | +namespace lldb_private { |
| 17 | + |
| 18 | +static llvm::raw_ostream &PrintSeverity(Stream &stream, |
| 19 | + lldb::Severity severity) { |
| 20 | + llvm::HighlightColor color; |
| 21 | + llvm::StringRef text; |
| 22 | + switch (severity) { |
| 23 | + case lldb::eSeverityError: |
| 24 | + color = llvm::HighlightColor::Error; |
| 25 | + text = "error: "; |
| 26 | + break; |
| 27 | + case lldb::eSeverityWarning: |
| 28 | + color = llvm::HighlightColor::Warning; |
| 29 | + text = "warning: "; |
| 30 | + break; |
| 31 | + case lldb::eSeverityInfo: |
| 32 | + color = llvm::HighlightColor::Remark; |
| 33 | + text = "note: "; |
| 34 | + break; |
| 35 | + } |
| 36 | + return llvm::WithColor(stream.AsRawOstream(), color, llvm::ColorMode::Enable) |
| 37 | + << text; |
| 38 | +} |
| 39 | + |
| 40 | +// Public for unittesting. |
| 41 | +static void RenderDiagnosticDetails(Stream &stream, |
| 42 | + std::optional<uint16_t> offset_in_command, |
| 43 | + bool show_inline, |
| 44 | + llvm::ArrayRef<DiagnosticDetail> details) { |
| 45 | + if (details.empty()) |
| 46 | + return; |
| 47 | + |
| 48 | + if (!offset_in_command) { |
| 49 | + for (const DiagnosticDetail &detail : details) { |
| 50 | + PrintSeverity(stream, detail.severity); |
| 51 | + stream << detail.rendered << '\n'; |
| 52 | + } |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Print a line with caret indicator(s) below the lldb prompt + command. |
| 57 | + const size_t padding = *offset_in_command; |
| 58 | + stream << std::string(padding, ' '); |
| 59 | + |
| 60 | + size_t offset = 1; |
| 61 | + std::vector<DiagnosticDetail> remaining_details, other_details, |
| 62 | + hidden_details; |
| 63 | + for (const DiagnosticDetail &detail : details) { |
| 64 | + if (!show_inline || !detail.source_location) { |
| 65 | + other_details.push_back(detail); |
| 66 | + continue; |
| 67 | + } |
| 68 | + if (detail.source_location->hidden) { |
| 69 | + hidden_details.push_back(detail); |
| 70 | + continue; |
| 71 | + } |
| 72 | + if (!detail.source_location->in_user_input) { |
| 73 | + other_details.push_back(detail); |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + auto &loc = *detail.source_location; |
| 78 | + remaining_details.push_back(detail); |
| 79 | + if (offset > loc.column) |
| 80 | + continue; |
| 81 | + stream << std::string(loc.column - offset, ' ') << '^'; |
| 82 | + if (loc.length > 1) |
| 83 | + stream << std::string(loc.length - 1, '~'); |
| 84 | + offset = loc.column + 1; |
| 85 | + } |
| 86 | + stream << '\n'; |
| 87 | + |
| 88 | + // Work through each detail in reverse order using the vector/stack. |
| 89 | + bool did_print = false; |
| 90 | + for (auto detail = remaining_details.rbegin(); |
| 91 | + detail != remaining_details.rend(); |
| 92 | + ++detail, remaining_details.pop_back()) { |
| 93 | + // Get the information to print this detail and remove it from the stack. |
| 94 | + // Print all the lines for all the other messages first. |
| 95 | + stream << std::string(padding, ' '); |
| 96 | + size_t offset = 1; |
| 97 | + for (auto &remaining_detail : |
| 98 | + llvm::ArrayRef(remaining_details).drop_back(1)) { |
| 99 | + uint16_t column = remaining_detail.source_location->column; |
| 100 | + stream << std::string(column - offset, ' ') << "│"; |
| 101 | + offset = column + 1; |
| 102 | + } |
| 103 | + |
| 104 | + // Print the line connecting the ^ with the error message. |
| 105 | + uint16_t column = detail->source_location->column; |
| 106 | + if (offset <= column) |
| 107 | + stream << std::string(column - offset, ' ') << "╰─ "; |
| 108 | + |
| 109 | + // Print a colorized string based on the message's severity type. |
| 110 | + PrintSeverity(stream, detail->severity); |
| 111 | + |
| 112 | + // Finally, print the message and start a new line. |
| 113 | + stream << detail->message << '\n'; |
| 114 | + did_print = true; |
| 115 | + } |
| 116 | + |
| 117 | + // Print the non-located details. |
| 118 | + for (const DiagnosticDetail &detail : other_details) { |
| 119 | + PrintSeverity(stream, detail.severity); |
| 120 | + stream << detail.rendered << '\n'; |
| 121 | + did_print = true; |
| 122 | + } |
| 123 | + |
| 124 | + // Print the hidden details as a last resort. |
| 125 | + if (!did_print) |
| 126 | + for (const DiagnosticDetail &detail : hidden_details) { |
| 127 | + PrintSeverity(stream, detail.severity); |
| 128 | + stream << detail.rendered << '\n'; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace lldb_private |
| 133 | +#endif |
0 commit comments