| 21 | namespace { |
| 22 | |
| 23 | std::string FormatError(const Error& error, |
| 24 | Location::Type location_type, |
| 25 | const Color& color, |
| 26 | LexerSourceLineFinder* line_finder, |
| 27 | int source_line_max_length, |
| 28 | int indent) { |
| 29 | std::string indent_str(indent, ' '); |
| 30 | std::string result = indent_str; |
| 31 | |
| 32 | result += color.MaybeBoldCode(); |
| 33 | |
| 34 | const Location& loc = error.loc; |
| 35 | if (!error.filename.empty()) { |
| 36 | result += error.filename; |
| 37 | result += ":"; |
| 38 | } |
| 39 | |
| 40 | if (location_type == Location::Type::Text) { |
| 41 | result += StringPrintf("%u:%u: ", loc.line, loc.first_column); |
| 42 | } else if (loc.offset != kInvalidOffset) { |
| 43 | result += StringPrintf("%07" PRIzx ": ", loc.offset); |
| 44 | } |
| 45 | |
| 46 | result += color.MaybeRedCode(); |
| 47 | result += GetErrorLevelName(error.error_level); |
| 48 | result += ": "; |
| 49 | result += color.MaybeDefaultCode(); |
| 50 | |
| 51 | result += error.message; |
| 52 | result += '\n'; |
| 53 | |
| 54 | LexerSourceLineFinder::SourceLine source_line; |
| 55 | if (line_finder) { |
| 56 | line_finder->GetSourceLine(loc, source_line_max_length, &source_line); |
| 57 | } |
| 58 | |
| 59 | if (!source_line.line.empty()) { |
| 60 | result += indent_str; |
| 61 | result += source_line.line; |
| 62 | result += '\n'; |
| 63 | result += indent_str; |
| 64 | |
| 65 | size_t num_spaces = 0; |
| 66 | if (loc.first_column > source_line.column_offset) { |
| 67 | num_spaces = (loc.first_column - 1) - source_line.column_offset; |
| 68 | } |
| 69 | size_t num_carets = loc.last_column - loc.first_column; |
| 70 | num_carets = std::min(num_carets, source_line.line.size() - num_spaces); |
| 71 | num_carets = std::max<size_t>(num_carets, 1); |
| 72 | result.append(num_spaces, ' '); |
| 73 | result += color.MaybeBoldCode(); |
| 74 | result += color.MaybeGreenCode(); |
| 75 | result.append(num_carets, '^'); |
| 76 | result += color.MaybeDefaultCode(); |
| 77 | result += '\n'; |
| 78 | } |
| 79 | |
| 80 | return result; |
no test coverage detected