10b57cec5SDimitry Andric //===--- TextDiagnostic.cpp - Text Diagnostic Pretty-Printing -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "clang/Frontend/TextDiagnostic.h"
100b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
110b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h"
120b57cec5SDimitry Andric #include "clang/Basic/FileManager.h"
130b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h"
140b57cec5SDimitry Andric #include "clang/Lex/Lexer.h"
15*0fca6ea1SDimitry Andric #include "clang/Lex/Preprocessor.h"
160b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
180b57cec5SDimitry Andric #include "llvm/Support/ConvertUTF.h"
190b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
200b57cec5SDimitry Andric #include "llvm/Support/Locale.h"
210b57cec5SDimitry Andric #include "llvm/Support/Path.h"
220b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
230b57cec5SDimitry Andric #include <algorithm>
24bdd1243dSDimitry Andric #include <optional>
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric using namespace clang;
270b57cec5SDimitry Andric
285f757f3fSDimitry Andric static const enum raw_ostream::Colors noteColor = raw_ostream::CYAN;
290b57cec5SDimitry Andric static const enum raw_ostream::Colors remarkColor =
300b57cec5SDimitry Andric raw_ostream::BLUE;
310b57cec5SDimitry Andric static const enum raw_ostream::Colors fixitColor =
320b57cec5SDimitry Andric raw_ostream::GREEN;
330b57cec5SDimitry Andric static const enum raw_ostream::Colors caretColor =
340b57cec5SDimitry Andric raw_ostream::GREEN;
350b57cec5SDimitry Andric static const enum raw_ostream::Colors warningColor =
360b57cec5SDimitry Andric raw_ostream::MAGENTA;
370b57cec5SDimitry Andric static const enum raw_ostream::Colors templateColor =
380b57cec5SDimitry Andric raw_ostream::CYAN;
390b57cec5SDimitry Andric static const enum raw_ostream::Colors errorColor = raw_ostream::RED;
400b57cec5SDimitry Andric static const enum raw_ostream::Colors fatalColor = raw_ostream::RED;
410b57cec5SDimitry Andric // Used for changing only the bold attribute.
420b57cec5SDimitry Andric static const enum raw_ostream::Colors savedColor =
430b57cec5SDimitry Andric raw_ostream::SAVEDCOLOR;
440b57cec5SDimitry Andric
45*0fca6ea1SDimitry Andric // Magenta is taken for 'warning'. Red is already 'error' and 'cyan'
46*0fca6ea1SDimitry Andric // is already taken for 'note'. Green is already used to underline
47*0fca6ea1SDimitry Andric // source ranges. White and black are bad because of the usual
48*0fca6ea1SDimitry Andric // terminal backgrounds. Which leaves us only with TWO options.
49*0fca6ea1SDimitry Andric static constexpr raw_ostream::Colors CommentColor = raw_ostream::YELLOW;
50*0fca6ea1SDimitry Andric static constexpr raw_ostream::Colors LiteralColor = raw_ostream::GREEN;
51*0fca6ea1SDimitry Andric static constexpr raw_ostream::Colors KeywordColor = raw_ostream::BLUE;
52*0fca6ea1SDimitry Andric
530b57cec5SDimitry Andric /// Add highlights to differences in template strings.
applyTemplateHighlighting(raw_ostream & OS,StringRef Str,bool & Normal,bool Bold)540b57cec5SDimitry Andric static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str,
550b57cec5SDimitry Andric bool &Normal, bool Bold) {
5604eeddc0SDimitry Andric while (true) {
570b57cec5SDimitry Andric size_t Pos = Str.find(ToggleHighlight);
580b57cec5SDimitry Andric OS << Str.slice(0, Pos);
590b57cec5SDimitry Andric if (Pos == StringRef::npos)
600b57cec5SDimitry Andric break;
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric Str = Str.substr(Pos + 1);
630b57cec5SDimitry Andric if (Normal)
640b57cec5SDimitry Andric OS.changeColor(templateColor, true);
650b57cec5SDimitry Andric else {
660b57cec5SDimitry Andric OS.resetColor();
670b57cec5SDimitry Andric if (Bold)
680b57cec5SDimitry Andric OS.changeColor(savedColor, true);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric Normal = !Normal;
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric /// Number of spaces to indent when word-wrapping.
750b57cec5SDimitry Andric const unsigned WordWrapIndentation = 6;
760b57cec5SDimitry Andric
bytesSincePreviousTabOrLineBegin(StringRef SourceLine,size_t i)770b57cec5SDimitry Andric static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) {
780b57cec5SDimitry Andric int bytes = 0;
790b57cec5SDimitry Andric while (0<i) {
800b57cec5SDimitry Andric if (SourceLine[--i]=='\t')
810b57cec5SDimitry Andric break;
820b57cec5SDimitry Andric ++bytes;
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric return bytes;
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric /// returns a printable representation of first item from input range
880b57cec5SDimitry Andric ///
890b57cec5SDimitry Andric /// This function returns a printable representation of the next item in a line
900b57cec5SDimitry Andric /// of source. If the next byte begins a valid and printable character, that
910b57cec5SDimitry Andric /// character is returned along with 'true'.
920b57cec5SDimitry Andric ///
930b57cec5SDimitry Andric /// Otherwise, if the next byte begins a valid, but unprintable character, a
940b57cec5SDimitry Andric /// printable, escaped representation of the character is returned, along with
950b57cec5SDimitry Andric /// 'false'. Otherwise a printable, escaped representation of the next byte
960b57cec5SDimitry Andric /// is returned along with 'false'.
970b57cec5SDimitry Andric ///
980b57cec5SDimitry Andric /// \note The index is updated to be used with a subsequent call to
990b57cec5SDimitry Andric /// printableTextForNextCharacter.
1000b57cec5SDimitry Andric ///
1010b57cec5SDimitry Andric /// \param SourceLine The line of source
10206c3fb27SDimitry Andric /// \param I Pointer to byte index,
1030b57cec5SDimitry Andric /// \param TabStop used to expand tabs
1040b57cec5SDimitry Andric /// \return pair(printable text, 'true' iff original text was printable)
1050b57cec5SDimitry Andric ///
1060b57cec5SDimitry Andric static std::pair<SmallString<16>, bool>
printableTextForNextCharacter(StringRef SourceLine,size_t * I,unsigned TabStop)10706c3fb27SDimitry Andric printableTextForNextCharacter(StringRef SourceLine, size_t *I,
1080b57cec5SDimitry Andric unsigned TabStop) {
10906c3fb27SDimitry Andric assert(I && "I must not be null");
11006c3fb27SDimitry Andric assert(*I < SourceLine.size() && "must point to a valid index");
1110b57cec5SDimitry Andric
11206c3fb27SDimitry Andric if (SourceLine[*I] == '\t') {
1130b57cec5SDimitry Andric assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
1140b57cec5SDimitry Andric "Invalid -ftabstop value");
11506c3fb27SDimitry Andric unsigned Col = bytesSincePreviousTabOrLineBegin(SourceLine, *I);
11606c3fb27SDimitry Andric unsigned NumSpaces = TabStop - (Col % TabStop);
1170b57cec5SDimitry Andric assert(0 < NumSpaces && NumSpaces <= TabStop
1180b57cec5SDimitry Andric && "Invalid computation of space amt");
11906c3fb27SDimitry Andric ++(*I);
1200b57cec5SDimitry Andric
12106c3fb27SDimitry Andric SmallString<16> ExpandedTab;
12206c3fb27SDimitry Andric ExpandedTab.assign(NumSpaces, ' ');
12306c3fb27SDimitry Andric return std::make_pair(ExpandedTab, true);
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric
12606c3fb27SDimitry Andric const unsigned char *Begin = SourceLine.bytes_begin() + *I;
1270b57cec5SDimitry Andric
12806c3fb27SDimitry Andric // Fast path for the common ASCII case.
12906c3fb27SDimitry Andric if (*Begin < 0x80 && llvm::sys::locale::isPrint(*Begin)) {
13006c3fb27SDimitry Andric ++(*I);
13106c3fb27SDimitry Andric return std::make_pair(SmallString<16>(Begin, Begin + 1), true);
1320b57cec5SDimitry Andric }
13306c3fb27SDimitry Andric unsigned CharSize = llvm::getNumBytesForUTF8(*Begin);
13406c3fb27SDimitry Andric const unsigned char *End = Begin + CharSize;
13506c3fb27SDimitry Andric
13606c3fb27SDimitry Andric // Convert it to UTF32 and check if it's printable.
13706c3fb27SDimitry Andric if (End <= SourceLine.bytes_end() && llvm::isLegalUTF8Sequence(Begin, End)) {
13806c3fb27SDimitry Andric llvm::UTF32 C;
13906c3fb27SDimitry Andric llvm::UTF32 *CPtr = &C;
14006c3fb27SDimitry Andric
14106c3fb27SDimitry Andric // Begin and end before conversion.
14206c3fb27SDimitry Andric unsigned char const *OriginalBegin = Begin;
14306c3fb27SDimitry Andric llvm::ConversionResult Res = llvm::ConvertUTF8toUTF32(
14406c3fb27SDimitry Andric &Begin, End, &CPtr, CPtr + 1, llvm::strictConversion);
14506c3fb27SDimitry Andric (void)Res;
14606c3fb27SDimitry Andric assert(Res == llvm::conversionOK);
14706c3fb27SDimitry Andric assert(OriginalBegin < Begin);
148*0fca6ea1SDimitry Andric assert(unsigned(Begin - OriginalBegin) == CharSize);
14906c3fb27SDimitry Andric
15006c3fb27SDimitry Andric (*I) += (Begin - OriginalBegin);
15106c3fb27SDimitry Andric
15206c3fb27SDimitry Andric // Valid, multi-byte, printable UTF8 character.
15306c3fb27SDimitry Andric if (llvm::sys::locale::isPrint(C))
15406c3fb27SDimitry Andric return std::make_pair(SmallString<16>(OriginalBegin, End), true);
15506c3fb27SDimitry Andric
15606c3fb27SDimitry Andric // Valid but not printable.
15706c3fb27SDimitry Andric SmallString<16> Str("<U+>");
15806c3fb27SDimitry Andric while (C) {
15906c3fb27SDimitry Andric Str.insert(Str.begin() + 3, llvm::hexdigit(C % 16));
16006c3fb27SDimitry Andric C /= 16;
16106c3fb27SDimitry Andric }
16206c3fb27SDimitry Andric while (Str.size() < 8)
16306c3fb27SDimitry Andric Str.insert(Str.begin() + 3, llvm::hexdigit(0));
16406c3fb27SDimitry Andric return std::make_pair(Str, false);
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric
16706c3fb27SDimitry Andric // Otherwise, not printable since it's not valid UTF8.
16806c3fb27SDimitry Andric SmallString<16> ExpandedByte("<XX>");
16906c3fb27SDimitry Andric unsigned char Byte = SourceLine[*I];
17006c3fb27SDimitry Andric ExpandedByte[1] = llvm::hexdigit(Byte / 16);
17106c3fb27SDimitry Andric ExpandedByte[2] = llvm::hexdigit(Byte % 16);
17206c3fb27SDimitry Andric ++(*I);
17306c3fb27SDimitry Andric return std::make_pair(ExpandedByte, false);
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric
expandTabs(std::string & SourceLine,unsigned TabStop)1760b57cec5SDimitry Andric static void expandTabs(std::string &SourceLine, unsigned TabStop) {
17706c3fb27SDimitry Andric size_t I = SourceLine.size();
17806c3fb27SDimitry Andric while (I > 0) {
17906c3fb27SDimitry Andric I--;
18006c3fb27SDimitry Andric if (SourceLine[I] != '\t')
1810b57cec5SDimitry Andric continue;
18206c3fb27SDimitry Andric size_t TmpI = I;
18306c3fb27SDimitry Andric auto [Str, Printable] =
18406c3fb27SDimitry Andric printableTextForNextCharacter(SourceLine, &TmpI, TabStop);
18506c3fb27SDimitry Andric SourceLine.replace(I, 1, Str.c_str());
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric
18906c3fb27SDimitry Andric /// \p BytesOut:
19006c3fb27SDimitry Andric /// A mapping from columns to the byte of the source line that produced the
19106c3fb27SDimitry Andric /// character displaying at that column. This is the inverse of \p ColumnsOut.
19206c3fb27SDimitry Andric ///
19306c3fb27SDimitry Andric /// The last element in the array is the number of bytes in the source string.
19406c3fb27SDimitry Andric ///
19506c3fb27SDimitry Andric /// example: (given a tabstop of 8)
19606c3fb27SDimitry Andric ///
19706c3fb27SDimitry Andric /// "a \t \u3042" -> {0,1,2,-1,-1,-1,-1,-1,3,4,-1,7}
19806c3fb27SDimitry Andric ///
19906c3fb27SDimitry Andric /// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to
20006c3fb27SDimitry Andric /// display)
20106c3fb27SDimitry Andric ///
20206c3fb27SDimitry Andric /// \p ColumnsOut:
20306c3fb27SDimitry Andric /// A mapping from the bytes
2040b57cec5SDimitry Andric /// of the printable representation of the line to the columns those printable
2050b57cec5SDimitry Andric /// characters will appear at (numbering the first column as 0).
2060b57cec5SDimitry Andric ///
2070b57cec5SDimitry Andric /// If a byte 'i' corresponds to multiple columns (e.g. the byte contains a tab
2080b57cec5SDimitry Andric /// character) then the array will map that byte to the first column the
2090b57cec5SDimitry Andric /// tab appears at and the next value in the map will have been incremented
2100b57cec5SDimitry Andric /// more than once.
2110b57cec5SDimitry Andric ///
2120b57cec5SDimitry Andric /// If a byte is the first in a sequence of bytes that together map to a single
2130b57cec5SDimitry Andric /// entity in the output, then the array will map that byte to the appropriate
2140b57cec5SDimitry Andric /// column while the subsequent bytes will be -1.
2150b57cec5SDimitry Andric ///
2160b57cec5SDimitry Andric /// The last element in the array does not correspond to any byte in the input
2170b57cec5SDimitry Andric /// and instead is the number of columns needed to display the source
2180b57cec5SDimitry Andric ///
2190b57cec5SDimitry Andric /// example: (given a tabstop of 8)
2200b57cec5SDimitry Andric ///
2210b57cec5SDimitry Andric /// "a \t \u3042" -> {0,1,2,8,9,-1,-1,11}
2220b57cec5SDimitry Andric ///
2230b57cec5SDimitry Andric /// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to
2240b57cec5SDimitry Andric /// display)
genColumnByteMapping(StringRef SourceLine,unsigned TabStop,SmallVectorImpl<int> & BytesOut,SmallVectorImpl<int> & ColumnsOut)22506c3fb27SDimitry Andric static void genColumnByteMapping(StringRef SourceLine, unsigned TabStop,
22606c3fb27SDimitry Andric SmallVectorImpl<int> &BytesOut,
22706c3fb27SDimitry Andric SmallVectorImpl<int> &ColumnsOut) {
22806c3fb27SDimitry Andric assert(BytesOut.empty());
22906c3fb27SDimitry Andric assert(ColumnsOut.empty());
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric if (SourceLine.empty()) {
23206c3fb27SDimitry Andric BytesOut.resize(1u, 0);
23306c3fb27SDimitry Andric ColumnsOut.resize(1u, 0);
2340b57cec5SDimitry Andric return;
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
23706c3fb27SDimitry Andric ColumnsOut.resize(SourceLine.size() + 1, -1);
2380b57cec5SDimitry Andric
23906c3fb27SDimitry Andric int Columns = 0;
24006c3fb27SDimitry Andric size_t I = 0;
24106c3fb27SDimitry Andric while (I < SourceLine.size()) {
24206c3fb27SDimitry Andric ColumnsOut[I] = Columns;
24306c3fb27SDimitry Andric BytesOut.resize(Columns + 1, -1);
24406c3fb27SDimitry Andric BytesOut.back() = I;
24506c3fb27SDimitry Andric auto [Str, Printable] =
24606c3fb27SDimitry Andric printableTextForNextCharacter(SourceLine, &I, TabStop);
24706c3fb27SDimitry Andric Columns += llvm::sys::locale::columnWidth(Str);
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric
25006c3fb27SDimitry Andric ColumnsOut.back() = Columns;
25106c3fb27SDimitry Andric BytesOut.resize(Columns + 1, -1);
25206c3fb27SDimitry Andric BytesOut.back() = I;
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric namespace {
2560b57cec5SDimitry Andric struct SourceColumnMap {
SourceColumnMap__anon2be45d930111::SourceColumnMap2570b57cec5SDimitry Andric SourceColumnMap(StringRef SourceLine, unsigned TabStop)
2580b57cec5SDimitry Andric : m_SourceLine(SourceLine) {
2590b57cec5SDimitry Andric
26006c3fb27SDimitry Andric genColumnByteMapping(SourceLine, TabStop, m_columnToByte, m_byteToColumn);
2610b57cec5SDimitry Andric
2620b57cec5SDimitry Andric assert(m_byteToColumn.size()==SourceLine.size()+1);
2630b57cec5SDimitry Andric assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size());
2640b57cec5SDimitry Andric assert(m_byteToColumn.size()
2650b57cec5SDimitry Andric == static_cast<unsigned>(m_columnToByte.back()+1));
2660b57cec5SDimitry Andric assert(static_cast<unsigned>(m_byteToColumn.back()+1)
2670b57cec5SDimitry Andric == m_columnToByte.size());
2680b57cec5SDimitry Andric }
columns__anon2be45d930111::SourceColumnMap2690b57cec5SDimitry Andric int columns() const { return m_byteToColumn.back(); }
bytes__anon2be45d930111::SourceColumnMap2700b57cec5SDimitry Andric int bytes() const { return m_columnToByte.back(); }
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric /// Map a byte to the column which it is at the start of, or return -1
2730b57cec5SDimitry Andric /// if it is not at the start of a column (for a UTF-8 trailing byte).
byteToColumn__anon2be45d930111::SourceColumnMap2740b57cec5SDimitry Andric int byteToColumn(int n) const {
2750b57cec5SDimitry Andric assert(0<=n && n<static_cast<int>(m_byteToColumn.size()));
2760b57cec5SDimitry Andric return m_byteToColumn[n];
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric /// Map a byte to the first column which contains it.
byteToContainingColumn__anon2be45d930111::SourceColumnMap2800b57cec5SDimitry Andric int byteToContainingColumn(int N) const {
2810b57cec5SDimitry Andric assert(0 <= N && N < static_cast<int>(m_byteToColumn.size()));
2820b57cec5SDimitry Andric while (m_byteToColumn[N] == -1)
2830b57cec5SDimitry Andric --N;
2840b57cec5SDimitry Andric return m_byteToColumn[N];
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric
2870b57cec5SDimitry Andric /// Map a column to the byte which starts the column, or return -1 if
2880b57cec5SDimitry Andric /// the column the second or subsequent column of an expanded tab or similar
2890b57cec5SDimitry Andric /// multi-column entity.
columnToByte__anon2be45d930111::SourceColumnMap2900b57cec5SDimitry Andric int columnToByte(int n) const {
2910b57cec5SDimitry Andric assert(0<=n && n<static_cast<int>(m_columnToByte.size()));
2920b57cec5SDimitry Andric return m_columnToByte[n];
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric /// Map from a byte index to the next byte which starts a column.
startOfNextColumn__anon2be45d930111::SourceColumnMap2960b57cec5SDimitry Andric int startOfNextColumn(int N) const {
2970b57cec5SDimitry Andric assert(0 <= N && N < static_cast<int>(m_byteToColumn.size() - 1));
2980b57cec5SDimitry Andric while (byteToColumn(++N) == -1) {}
2990b57cec5SDimitry Andric return N;
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric /// Map from a byte index to the previous byte which starts a column.
startOfPreviousColumn__anon2be45d930111::SourceColumnMap3030b57cec5SDimitry Andric int startOfPreviousColumn(int N) const {
3040b57cec5SDimitry Andric assert(0 < N && N < static_cast<int>(m_byteToColumn.size()));
3050b57cec5SDimitry Andric while (byteToColumn(--N) == -1) {}
3060b57cec5SDimitry Andric return N;
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric
getSourceLine__anon2be45d930111::SourceColumnMap3090b57cec5SDimitry Andric StringRef getSourceLine() const {
3100b57cec5SDimitry Andric return m_SourceLine;
3110b57cec5SDimitry Andric }
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric private:
3140b57cec5SDimitry Andric const std::string m_SourceLine;
3150b57cec5SDimitry Andric SmallVector<int,200> m_byteToColumn;
3160b57cec5SDimitry Andric SmallVector<int,200> m_columnToByte;
3170b57cec5SDimitry Andric };
3180b57cec5SDimitry Andric } // end anonymous namespace
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric /// When the source code line we want to print is too long for
3210b57cec5SDimitry Andric /// the terminal, select the "interesting" region.
selectInterestingSourceRegion(std::string & SourceLine,std::string & CaretLine,std::string & FixItInsertionLine,unsigned Columns,const SourceColumnMap & map)3220b57cec5SDimitry Andric static void selectInterestingSourceRegion(std::string &SourceLine,
3230b57cec5SDimitry Andric std::string &CaretLine,
3240b57cec5SDimitry Andric std::string &FixItInsertionLine,
3250b57cec5SDimitry Andric unsigned Columns,
3260b57cec5SDimitry Andric const SourceColumnMap &map) {
3270b57cec5SDimitry Andric unsigned CaretColumns = CaretLine.size();
3280b57cec5SDimitry Andric unsigned FixItColumns = llvm::sys::locale::columnWidth(FixItInsertionLine);
3290b57cec5SDimitry Andric unsigned MaxColumns = std::max(static_cast<unsigned>(map.columns()),
3300b57cec5SDimitry Andric std::max(CaretColumns, FixItColumns));
3310b57cec5SDimitry Andric // if the number of columns is less than the desired number we're done
3320b57cec5SDimitry Andric if (MaxColumns <= Columns)
3330b57cec5SDimitry Andric return;
3340b57cec5SDimitry Andric
3350b57cec5SDimitry Andric // No special characters are allowed in CaretLine.
336bdd1243dSDimitry Andric assert(llvm::none_of(CaretLine, [](char c) { return c < ' ' || '~' < c; }));
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric // Find the slice that we need to display the full caret line
3390b57cec5SDimitry Andric // correctly.
3400b57cec5SDimitry Andric unsigned CaretStart = 0, CaretEnd = CaretLine.size();
3410b57cec5SDimitry Andric for (; CaretStart != CaretEnd; ++CaretStart)
3420b57cec5SDimitry Andric if (!isWhitespace(CaretLine[CaretStart]))
3430b57cec5SDimitry Andric break;
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric for (; CaretEnd != CaretStart; --CaretEnd)
3460b57cec5SDimitry Andric if (!isWhitespace(CaretLine[CaretEnd - 1]))
3470b57cec5SDimitry Andric break;
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andric // caret has already been inserted into CaretLine so the above whitespace
3500b57cec5SDimitry Andric // check is guaranteed to include the caret
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andric // If we have a fix-it line, make sure the slice includes all of the
3530b57cec5SDimitry Andric // fix-it information.
3540b57cec5SDimitry Andric if (!FixItInsertionLine.empty()) {
3550b57cec5SDimitry Andric unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
3560b57cec5SDimitry Andric for (; FixItStart != FixItEnd; ++FixItStart)
3570b57cec5SDimitry Andric if (!isWhitespace(FixItInsertionLine[FixItStart]))
3580b57cec5SDimitry Andric break;
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andric for (; FixItEnd != FixItStart; --FixItEnd)
3610b57cec5SDimitry Andric if (!isWhitespace(FixItInsertionLine[FixItEnd - 1]))
3620b57cec5SDimitry Andric break;
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andric // We can safely use the byte offset FixItStart as the column offset
3650b57cec5SDimitry Andric // because the characters up until FixItStart are all ASCII whitespace
3660b57cec5SDimitry Andric // characters.
3670b57cec5SDimitry Andric unsigned FixItStartCol = FixItStart;
3680b57cec5SDimitry Andric unsigned FixItEndCol
3690b57cec5SDimitry Andric = llvm::sys::locale::columnWidth(FixItInsertionLine.substr(0, FixItEnd));
3700b57cec5SDimitry Andric
3710b57cec5SDimitry Andric CaretStart = std::min(FixItStartCol, CaretStart);
3720b57cec5SDimitry Andric CaretEnd = std::max(FixItEndCol, CaretEnd);
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric
3750b57cec5SDimitry Andric // CaretEnd may have been set at the middle of a character
3760b57cec5SDimitry Andric // If it's not at a character's first column then advance it past the current
3770b57cec5SDimitry Andric // character.
3780b57cec5SDimitry Andric while (static_cast<int>(CaretEnd) < map.columns() &&
3790b57cec5SDimitry Andric -1 == map.columnToByte(CaretEnd))
3800b57cec5SDimitry Andric ++CaretEnd;
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andric assert((static_cast<int>(CaretStart) > map.columns() ||
3830b57cec5SDimitry Andric -1!=map.columnToByte(CaretStart)) &&
3840b57cec5SDimitry Andric "CaretStart must not point to a column in the middle of a source"
3850b57cec5SDimitry Andric " line character");
3860b57cec5SDimitry Andric assert((static_cast<int>(CaretEnd) > map.columns() ||
3870b57cec5SDimitry Andric -1!=map.columnToByte(CaretEnd)) &&
3880b57cec5SDimitry Andric "CaretEnd must not point to a column in the middle of a source line"
3890b57cec5SDimitry Andric " character");
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric // CaretLine[CaretStart, CaretEnd) contains all of the interesting
3920b57cec5SDimitry Andric // parts of the caret line. While this slice is smaller than the
3930b57cec5SDimitry Andric // number of columns we have, try to grow the slice to encompass
3940b57cec5SDimitry Andric // more context.
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric unsigned SourceStart = map.columnToByte(std::min<unsigned>(CaretStart,
3970b57cec5SDimitry Andric map.columns()));
3980b57cec5SDimitry Andric unsigned SourceEnd = map.columnToByte(std::min<unsigned>(CaretEnd,
3990b57cec5SDimitry Andric map.columns()));
4000b57cec5SDimitry Andric
4010b57cec5SDimitry Andric unsigned CaretColumnsOutsideSource = CaretEnd-CaretStart
4020b57cec5SDimitry Andric - (map.byteToColumn(SourceEnd)-map.byteToColumn(SourceStart));
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andric char const *front_ellipse = " ...";
4050b57cec5SDimitry Andric char const *front_space = " ";
4060b57cec5SDimitry Andric char const *back_ellipse = "...";
4070b57cec5SDimitry Andric unsigned ellipses_space = strlen(front_ellipse) + strlen(back_ellipse);
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andric unsigned TargetColumns = Columns;
4100b57cec5SDimitry Andric // Give us extra room for the ellipses
4110b57cec5SDimitry Andric // and any of the caret line that extends past the source
4120b57cec5SDimitry Andric if (TargetColumns > ellipses_space+CaretColumnsOutsideSource)
4130b57cec5SDimitry Andric TargetColumns -= ellipses_space+CaretColumnsOutsideSource;
4140b57cec5SDimitry Andric
4150b57cec5SDimitry Andric while (SourceStart>0 || SourceEnd<SourceLine.size()) {
4160b57cec5SDimitry Andric bool ExpandedRegion = false;
4170b57cec5SDimitry Andric
4180b57cec5SDimitry Andric if (SourceStart>0) {
4190b57cec5SDimitry Andric unsigned NewStart = map.startOfPreviousColumn(SourceStart);
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andric // Skip over any whitespace we see here; we're looking for
4220b57cec5SDimitry Andric // another bit of interesting text.
4230b57cec5SDimitry Andric // FIXME: Detect non-ASCII whitespace characters too.
4240b57cec5SDimitry Andric while (NewStart && isWhitespace(SourceLine[NewStart]))
4250b57cec5SDimitry Andric NewStart = map.startOfPreviousColumn(NewStart);
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric // Skip over this bit of "interesting" text.
4280b57cec5SDimitry Andric while (NewStart) {
4290b57cec5SDimitry Andric unsigned Prev = map.startOfPreviousColumn(NewStart);
4300b57cec5SDimitry Andric if (isWhitespace(SourceLine[Prev]))
4310b57cec5SDimitry Andric break;
4320b57cec5SDimitry Andric NewStart = Prev;
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric
4350b57cec5SDimitry Andric assert(map.byteToColumn(NewStart) != -1);
4360b57cec5SDimitry Andric unsigned NewColumns = map.byteToColumn(SourceEnd) -
4370b57cec5SDimitry Andric map.byteToColumn(NewStart);
4380b57cec5SDimitry Andric if (NewColumns <= TargetColumns) {
4390b57cec5SDimitry Andric SourceStart = NewStart;
4400b57cec5SDimitry Andric ExpandedRegion = true;
4410b57cec5SDimitry Andric }
4420b57cec5SDimitry Andric }
4430b57cec5SDimitry Andric
4440b57cec5SDimitry Andric if (SourceEnd<SourceLine.size()) {
4450b57cec5SDimitry Andric unsigned NewEnd = map.startOfNextColumn(SourceEnd);
4460b57cec5SDimitry Andric
4470b57cec5SDimitry Andric // Skip over any whitespace we see here; we're looking for
4480b57cec5SDimitry Andric // another bit of interesting text.
4490b57cec5SDimitry Andric // FIXME: Detect non-ASCII whitespace characters too.
4500b57cec5SDimitry Andric while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd]))
4510b57cec5SDimitry Andric NewEnd = map.startOfNextColumn(NewEnd);
4520b57cec5SDimitry Andric
4530b57cec5SDimitry Andric // Skip over this bit of "interesting" text.
4540b57cec5SDimitry Andric while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd]))
4550b57cec5SDimitry Andric NewEnd = map.startOfNextColumn(NewEnd);
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andric assert(map.byteToColumn(NewEnd) != -1);
4580b57cec5SDimitry Andric unsigned NewColumns = map.byteToColumn(NewEnd) -
4590b57cec5SDimitry Andric map.byteToColumn(SourceStart);
4600b57cec5SDimitry Andric if (NewColumns <= TargetColumns) {
4610b57cec5SDimitry Andric SourceEnd = NewEnd;
4620b57cec5SDimitry Andric ExpandedRegion = true;
4630b57cec5SDimitry Andric }
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric if (!ExpandedRegion)
4670b57cec5SDimitry Andric break;
4680b57cec5SDimitry Andric }
4690b57cec5SDimitry Andric
4700b57cec5SDimitry Andric CaretStart = map.byteToColumn(SourceStart);
4710b57cec5SDimitry Andric CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andric // [CaretStart, CaretEnd) is the slice we want. Update the various
47406c3fb27SDimitry Andric // output lines to show only this slice.
4750b57cec5SDimitry Andric assert(CaretStart!=(unsigned)-1 && CaretEnd!=(unsigned)-1 &&
4760b57cec5SDimitry Andric SourceStart!=(unsigned)-1 && SourceEnd!=(unsigned)-1);
4770b57cec5SDimitry Andric assert(SourceStart <= SourceEnd);
4780b57cec5SDimitry Andric assert(CaretStart <= CaretEnd);
4790b57cec5SDimitry Andric
4800b57cec5SDimitry Andric unsigned BackColumnsRemoved
4810b57cec5SDimitry Andric = map.byteToColumn(SourceLine.size())-map.byteToColumn(SourceEnd);
4820b57cec5SDimitry Andric unsigned FrontColumnsRemoved = CaretStart;
4830b57cec5SDimitry Andric unsigned ColumnsKept = CaretEnd-CaretStart;
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andric // We checked up front that the line needed truncation
4860b57cec5SDimitry Andric assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns);
4870b57cec5SDimitry Andric
4880b57cec5SDimitry Andric // The line needs some truncation, and we'd prefer to keep the front
4890b57cec5SDimitry Andric // if possible, so remove the back
4900b57cec5SDimitry Andric if (BackColumnsRemoved > strlen(back_ellipse))
4910b57cec5SDimitry Andric SourceLine.replace(SourceEnd, std::string::npos, back_ellipse);
4920b57cec5SDimitry Andric
4930b57cec5SDimitry Andric // If that's enough then we're done
4940b57cec5SDimitry Andric if (FrontColumnsRemoved+ColumnsKept <= Columns)
4950b57cec5SDimitry Andric return;
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andric // Otherwise remove the front as well
4980b57cec5SDimitry Andric if (FrontColumnsRemoved > strlen(front_ellipse)) {
4990b57cec5SDimitry Andric SourceLine.replace(0, SourceStart, front_ellipse);
5000b57cec5SDimitry Andric CaretLine.replace(0, CaretStart, front_space);
5010b57cec5SDimitry Andric if (!FixItInsertionLine.empty())
5020b57cec5SDimitry Andric FixItInsertionLine.replace(0, CaretStart, front_space);
5030b57cec5SDimitry Andric }
5040b57cec5SDimitry Andric }
5050b57cec5SDimitry Andric
5060b57cec5SDimitry Andric /// Skip over whitespace in the string, starting at the given
5070b57cec5SDimitry Andric /// index.
5080b57cec5SDimitry Andric ///
5090b57cec5SDimitry Andric /// \returns The index of the first non-whitespace character that is
5100b57cec5SDimitry Andric /// greater than or equal to Idx or, if no such character exists,
5110b57cec5SDimitry Andric /// returns the end of the string.
skipWhitespace(unsigned Idx,StringRef Str,unsigned Length)5120b57cec5SDimitry Andric static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) {
5130b57cec5SDimitry Andric while (Idx < Length && isWhitespace(Str[Idx]))
5140b57cec5SDimitry Andric ++Idx;
5150b57cec5SDimitry Andric return Idx;
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andric /// If the given character is the start of some kind of
5190b57cec5SDimitry Andric /// balanced punctuation (e.g., quotes or parentheses), return the
5200b57cec5SDimitry Andric /// character that will terminate the punctuation.
5210b57cec5SDimitry Andric ///
5220b57cec5SDimitry Andric /// \returns The ending punctuation character, if any, or the NULL
5230b57cec5SDimitry Andric /// character if the input character does not start any punctuation.
findMatchingPunctuation(char c)5240b57cec5SDimitry Andric static inline char findMatchingPunctuation(char c) {
5250b57cec5SDimitry Andric switch (c) {
5260b57cec5SDimitry Andric case '\'': return '\'';
5270b57cec5SDimitry Andric case '`': return '\'';
5280b57cec5SDimitry Andric case '"': return '"';
5290b57cec5SDimitry Andric case '(': return ')';
5300b57cec5SDimitry Andric case '[': return ']';
5310b57cec5SDimitry Andric case '{': return '}';
5320b57cec5SDimitry Andric default: break;
5330b57cec5SDimitry Andric }
5340b57cec5SDimitry Andric
5350b57cec5SDimitry Andric return 0;
5360b57cec5SDimitry Andric }
5370b57cec5SDimitry Andric
5380b57cec5SDimitry Andric /// Find the end of the word starting at the given offset
5390b57cec5SDimitry Andric /// within a string.
5400b57cec5SDimitry Andric ///
5410b57cec5SDimitry Andric /// \returns the index pointing one character past the end of the
5420b57cec5SDimitry Andric /// word.
findEndOfWord(unsigned Start,StringRef Str,unsigned Length,unsigned Column,unsigned Columns)5430b57cec5SDimitry Andric static unsigned findEndOfWord(unsigned Start, StringRef Str,
5440b57cec5SDimitry Andric unsigned Length, unsigned Column,
5450b57cec5SDimitry Andric unsigned Columns) {
5460b57cec5SDimitry Andric assert(Start < Str.size() && "Invalid start position!");
5470b57cec5SDimitry Andric unsigned End = Start + 1;
5480b57cec5SDimitry Andric
5490b57cec5SDimitry Andric // If we are already at the end of the string, take that as the word.
5500b57cec5SDimitry Andric if (End == Str.size())
5510b57cec5SDimitry Andric return End;
5520b57cec5SDimitry Andric
5530b57cec5SDimitry Andric // Determine if the start of the string is actually opening
5540b57cec5SDimitry Andric // punctuation, e.g., a quote or parentheses.
5550b57cec5SDimitry Andric char EndPunct = findMatchingPunctuation(Str[Start]);
5560b57cec5SDimitry Andric if (!EndPunct) {
5570b57cec5SDimitry Andric // This is a normal word. Just find the first space character.
5580b57cec5SDimitry Andric while (End < Length && !isWhitespace(Str[End]))
5590b57cec5SDimitry Andric ++End;
5600b57cec5SDimitry Andric return End;
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric
5630b57cec5SDimitry Andric // We have the start of a balanced punctuation sequence (quotes,
5640b57cec5SDimitry Andric // parentheses, etc.). Determine the full sequence is.
5650b57cec5SDimitry Andric SmallString<16> PunctuationEndStack;
5660b57cec5SDimitry Andric PunctuationEndStack.push_back(EndPunct);
5670b57cec5SDimitry Andric while (End < Length && !PunctuationEndStack.empty()) {
5680b57cec5SDimitry Andric if (Str[End] == PunctuationEndStack.back())
5690b57cec5SDimitry Andric PunctuationEndStack.pop_back();
5700b57cec5SDimitry Andric else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
5710b57cec5SDimitry Andric PunctuationEndStack.push_back(SubEndPunct);
5720b57cec5SDimitry Andric
5730b57cec5SDimitry Andric ++End;
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric
5760b57cec5SDimitry Andric // Find the first space character after the punctuation ended.
5770b57cec5SDimitry Andric while (End < Length && !isWhitespace(Str[End]))
5780b57cec5SDimitry Andric ++End;
5790b57cec5SDimitry Andric
5800b57cec5SDimitry Andric unsigned PunctWordLength = End - Start;
5810b57cec5SDimitry Andric if (// If the word fits on this line
5820b57cec5SDimitry Andric Column + PunctWordLength <= Columns ||
5830b57cec5SDimitry Andric // ... or the word is "short enough" to take up the next line
5840b57cec5SDimitry Andric // without too much ugly white space
5850b57cec5SDimitry Andric PunctWordLength < Columns/3)
5860b57cec5SDimitry Andric return End; // Take the whole thing as a single "word".
5870b57cec5SDimitry Andric
5880b57cec5SDimitry Andric // The whole quoted/parenthesized string is too long to print as a
5890b57cec5SDimitry Andric // single "word". Instead, find the "word" that starts just after
5900b57cec5SDimitry Andric // the punctuation and use that end-point instead. This will recurse
5910b57cec5SDimitry Andric // until it finds something small enough to consider a word.
5920b57cec5SDimitry Andric return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric
5950b57cec5SDimitry Andric /// Print the given string to a stream, word-wrapping it to
5960b57cec5SDimitry Andric /// some number of columns in the process.
5970b57cec5SDimitry Andric ///
5980b57cec5SDimitry Andric /// \param OS the stream to which the word-wrapping string will be
5990b57cec5SDimitry Andric /// emitted.
6000b57cec5SDimitry Andric /// \param Str the string to word-wrap and output.
6010b57cec5SDimitry Andric /// \param Columns the number of columns to word-wrap to.
6020b57cec5SDimitry Andric /// \param Column the column number at which the first character of \p
6030b57cec5SDimitry Andric /// Str will be printed. This will be non-zero when part of the first
6040b57cec5SDimitry Andric /// line has already been printed.
6050b57cec5SDimitry Andric /// \param Bold if the current text should be bold
6060b57cec5SDimitry Andric /// \returns true if word-wrapping was required, or false if the
6070b57cec5SDimitry Andric /// string fit on the first line.
printWordWrapped(raw_ostream & OS,StringRef Str,unsigned Columns,unsigned Column,bool Bold)60806c3fb27SDimitry Andric static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns,
60906c3fb27SDimitry Andric unsigned Column, bool Bold) {
6100b57cec5SDimitry Andric const unsigned Length = std::min(Str.find('\n'), Str.size());
6110b57cec5SDimitry Andric bool TextNormal = true;
6120b57cec5SDimitry Andric
6130b57cec5SDimitry Andric bool Wrapped = false;
6140b57cec5SDimitry Andric for (unsigned WordStart = 0, WordEnd; WordStart < Length;
6150b57cec5SDimitry Andric WordStart = WordEnd) {
6160b57cec5SDimitry Andric // Find the beginning of the next word.
6170b57cec5SDimitry Andric WordStart = skipWhitespace(WordStart, Str, Length);
6180b57cec5SDimitry Andric if (WordStart == Length)
6190b57cec5SDimitry Andric break;
6200b57cec5SDimitry Andric
6210b57cec5SDimitry Andric // Find the end of this word.
6220b57cec5SDimitry Andric WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
6230b57cec5SDimitry Andric
6240b57cec5SDimitry Andric // Does this word fit on the current line?
6250b57cec5SDimitry Andric unsigned WordLength = WordEnd - WordStart;
6260b57cec5SDimitry Andric if (Column + WordLength < Columns) {
6270b57cec5SDimitry Andric // This word fits on the current line; print it there.
6280b57cec5SDimitry Andric if (WordStart) {
6290b57cec5SDimitry Andric OS << ' ';
6300b57cec5SDimitry Andric Column += 1;
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
6330b57cec5SDimitry Andric TextNormal, Bold);
6340b57cec5SDimitry Andric Column += WordLength;
6350b57cec5SDimitry Andric continue;
6360b57cec5SDimitry Andric }
6370b57cec5SDimitry Andric
6380b57cec5SDimitry Andric // This word does not fit on the current line, so wrap to the next
6390b57cec5SDimitry Andric // line.
6400b57cec5SDimitry Andric OS << '\n';
64106c3fb27SDimitry Andric OS.indent(WordWrapIndentation);
6420b57cec5SDimitry Andric applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
6430b57cec5SDimitry Andric TextNormal, Bold);
64406c3fb27SDimitry Andric Column = WordWrapIndentation + WordLength;
6450b57cec5SDimitry Andric Wrapped = true;
6460b57cec5SDimitry Andric }
6470b57cec5SDimitry Andric
6480b57cec5SDimitry Andric // Append any remaning text from the message with its existing formatting.
6490b57cec5SDimitry Andric applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold);
6500b57cec5SDimitry Andric
6510b57cec5SDimitry Andric assert(TextNormal && "Text highlighted at end of diagnostic message.");
6520b57cec5SDimitry Andric
6530b57cec5SDimitry Andric return Wrapped;
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric
TextDiagnostic(raw_ostream & OS,const LangOptions & LangOpts,DiagnosticOptions * DiagOpts,const Preprocessor * PP)656*0fca6ea1SDimitry Andric TextDiagnostic::TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
657*0fca6ea1SDimitry Andric DiagnosticOptions *DiagOpts,
658*0fca6ea1SDimitry Andric const Preprocessor *PP)
659*0fca6ea1SDimitry Andric : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS), PP(PP) {}
6600b57cec5SDimitry Andric
~TextDiagnostic()6610b57cec5SDimitry Andric TextDiagnostic::~TextDiagnostic() {}
6620b57cec5SDimitry Andric
emitDiagnosticMessage(FullSourceLoc Loc,PresumedLoc PLoc,DiagnosticsEngine::Level Level,StringRef Message,ArrayRef<clang::CharSourceRange> Ranges,DiagOrStoredDiag D)6630b57cec5SDimitry Andric void TextDiagnostic::emitDiagnosticMessage(
6640b57cec5SDimitry Andric FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
6650b57cec5SDimitry Andric StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
6660b57cec5SDimitry Andric DiagOrStoredDiag D) {
6670b57cec5SDimitry Andric uint64_t StartOfLocationInfo = OS.tell();
6680b57cec5SDimitry Andric
6690b57cec5SDimitry Andric // Emit the location of this particular diagnostic.
6700b57cec5SDimitry Andric if (Loc.isValid())
6710b57cec5SDimitry Andric emitDiagnosticLoc(Loc, PLoc, Level, Ranges);
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric if (DiagOpts->ShowColors)
6740b57cec5SDimitry Andric OS.resetColor();
6750b57cec5SDimitry Andric
676a7dea167SDimitry Andric if (DiagOpts->ShowLevel)
677fe6060f1SDimitry Andric printDiagnosticLevel(OS, Level, DiagOpts->ShowColors);
6780b57cec5SDimitry Andric printDiagnosticMessage(OS,
6790b57cec5SDimitry Andric /*IsSupplemental*/ Level == DiagnosticsEngine::Note,
6800b57cec5SDimitry Andric Message, OS.tell() - StartOfLocationInfo,
6810b57cec5SDimitry Andric DiagOpts->MessageLength, DiagOpts->ShowColors);
6820b57cec5SDimitry Andric }
6830b57cec5SDimitry Andric
6840b57cec5SDimitry Andric /*static*/ void
printDiagnosticLevel(raw_ostream & OS,DiagnosticsEngine::Level Level,bool ShowColors)6850b57cec5SDimitry Andric TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
6860b57cec5SDimitry Andric DiagnosticsEngine::Level Level,
687fe6060f1SDimitry Andric bool ShowColors) {
6880b57cec5SDimitry Andric if (ShowColors) {
6890b57cec5SDimitry Andric // Print diagnostic category in bold and color
6900b57cec5SDimitry Andric switch (Level) {
6910b57cec5SDimitry Andric case DiagnosticsEngine::Ignored:
6920b57cec5SDimitry Andric llvm_unreachable("Invalid diagnostic type");
6930b57cec5SDimitry Andric case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break;
6940b57cec5SDimitry Andric case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break;
6950b57cec5SDimitry Andric case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break;
6960b57cec5SDimitry Andric case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break;
6970b57cec5SDimitry Andric case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break;
6980b57cec5SDimitry Andric }
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric
7010b57cec5SDimitry Andric switch (Level) {
7020b57cec5SDimitry Andric case DiagnosticsEngine::Ignored:
7030b57cec5SDimitry Andric llvm_unreachable("Invalid diagnostic type");
704fe6060f1SDimitry Andric case DiagnosticsEngine::Note: OS << "note: "; break;
705fe6060f1SDimitry Andric case DiagnosticsEngine::Remark: OS << "remark: "; break;
706fe6060f1SDimitry Andric case DiagnosticsEngine::Warning: OS << "warning: "; break;
707fe6060f1SDimitry Andric case DiagnosticsEngine::Error: OS << "error: "; break;
708fe6060f1SDimitry Andric case DiagnosticsEngine::Fatal: OS << "fatal error: "; break;
7090b57cec5SDimitry Andric }
7100b57cec5SDimitry Andric
7110b57cec5SDimitry Andric if (ShowColors)
7120b57cec5SDimitry Andric OS.resetColor();
7130b57cec5SDimitry Andric }
7140b57cec5SDimitry Andric
7150b57cec5SDimitry Andric /*static*/
printDiagnosticMessage(raw_ostream & OS,bool IsSupplemental,StringRef Message,unsigned CurrentColumn,unsigned Columns,bool ShowColors)7160b57cec5SDimitry Andric void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
7170b57cec5SDimitry Andric bool IsSupplemental,
7180b57cec5SDimitry Andric StringRef Message,
7190b57cec5SDimitry Andric unsigned CurrentColumn,
7200b57cec5SDimitry Andric unsigned Columns, bool ShowColors) {
7210b57cec5SDimitry Andric bool Bold = false;
7220b57cec5SDimitry Andric if (ShowColors && !IsSupplemental) {
7230b57cec5SDimitry Andric // Print primary diagnostic messages in bold and without color, to visually
7240b57cec5SDimitry Andric // indicate the transition from continuation notes and other output.
7250b57cec5SDimitry Andric OS.changeColor(savedColor, true);
7260b57cec5SDimitry Andric Bold = true;
7270b57cec5SDimitry Andric }
7280b57cec5SDimitry Andric
7290b57cec5SDimitry Andric if (Columns)
7300b57cec5SDimitry Andric printWordWrapped(OS, Message, Columns, CurrentColumn, Bold);
7310b57cec5SDimitry Andric else {
7320b57cec5SDimitry Andric bool Normal = true;
7330b57cec5SDimitry Andric applyTemplateHighlighting(OS, Message, Normal, Bold);
7340b57cec5SDimitry Andric assert(Normal && "Formatting should have returned to normal");
7350b57cec5SDimitry Andric }
7360b57cec5SDimitry Andric
7370b57cec5SDimitry Andric if (ShowColors)
7380b57cec5SDimitry Andric OS.resetColor();
7390b57cec5SDimitry Andric OS << '\n';
7400b57cec5SDimitry Andric }
7410b57cec5SDimitry Andric
emitFilename(StringRef Filename,const SourceManager & SM)7420b57cec5SDimitry Andric void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) {
743480093f4SDimitry Andric #ifdef _WIN32
744480093f4SDimitry Andric SmallString<4096> TmpFilename;
745480093f4SDimitry Andric #endif
7460b57cec5SDimitry Andric if (DiagOpts->AbsolutePath) {
7475f757f3fSDimitry Andric auto File = SM.getFileManager().getOptionalFileRef(Filename);
748480093f4SDimitry Andric if (File) {
7490b57cec5SDimitry Andric // We want to print a simplified absolute path, i. e. without "dots".
7500b57cec5SDimitry Andric //
7510b57cec5SDimitry Andric // The hardest part here are the paths like "<part1>/<link>/../<part2>".
7520b57cec5SDimitry Andric // On Unix-like systems, we cannot just collapse "<link>/..", because
7530b57cec5SDimitry Andric // paths are resolved sequentially, and, thereby, the path
7540b57cec5SDimitry Andric // "<part1>/<part2>" may point to a different location. That is why
7550b57cec5SDimitry Andric // we use FileManager::getCanonicalName(), which expands all indirections
7560b57cec5SDimitry Andric // with llvm::sys::fs::real_path() and caches the result.
7570b57cec5SDimitry Andric //
7580b57cec5SDimitry Andric // On the other hand, it would be better to preserve as much of the
7590b57cec5SDimitry Andric // original path as possible, because that helps a user to recognize it.
7600b57cec5SDimitry Andric // real_path() expands all links, which sometimes too much. Luckily,
7610b57cec5SDimitry Andric // on Windows we can just use llvm::sys::path::remove_dots(), because,
7620b57cec5SDimitry Andric // on that system, both aforementioned paths point to the same place.
7630b57cec5SDimitry Andric #ifdef _WIN32
7645f757f3fSDimitry Andric TmpFilename = File->getName();
765480093f4SDimitry Andric llvm::sys::fs::make_absolute(TmpFilename);
766480093f4SDimitry Andric llvm::sys::path::native(TmpFilename);
767480093f4SDimitry Andric llvm::sys::path::remove_dots(TmpFilename, /* remove_dot_dot */ true);
768480093f4SDimitry Andric Filename = StringRef(TmpFilename.data(), TmpFilename.size());
7690b57cec5SDimitry Andric #else
770480093f4SDimitry Andric Filename = SM.getFileManager().getCanonicalName(*File);
7710b57cec5SDimitry Andric #endif
7720b57cec5SDimitry Andric }
7730b57cec5SDimitry Andric }
7740b57cec5SDimitry Andric
7750b57cec5SDimitry Andric OS << Filename;
7760b57cec5SDimitry Andric }
7770b57cec5SDimitry Andric
7780b57cec5SDimitry Andric /// Print out the file/line/column information and include trace.
7790b57cec5SDimitry Andric ///
78006c3fb27SDimitry Andric /// This method handles the emission of the diagnostic location information.
7810b57cec5SDimitry Andric /// This includes extracting as much location information as is present for
7820b57cec5SDimitry Andric /// the diagnostic and printing it, as well as any include stack or source
7830b57cec5SDimitry Andric /// ranges necessary.
emitDiagnosticLoc(FullSourceLoc Loc,PresumedLoc PLoc,DiagnosticsEngine::Level Level,ArrayRef<CharSourceRange> Ranges)7840b57cec5SDimitry Andric void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
7850b57cec5SDimitry Andric DiagnosticsEngine::Level Level,
7860b57cec5SDimitry Andric ArrayRef<CharSourceRange> Ranges) {
7870b57cec5SDimitry Andric if (PLoc.isInvalid()) {
7880b57cec5SDimitry Andric // At least print the file name if available:
78906c3fb27SDimitry Andric if (FileID FID = Loc.getFileID(); FID.isValid()) {
7905f757f3fSDimitry Andric if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) {
7910b57cec5SDimitry Andric emitFilename(FE->getName(), Loc.getManager());
7920b57cec5SDimitry Andric OS << ": ";
7930b57cec5SDimitry Andric }
7940b57cec5SDimitry Andric }
7950b57cec5SDimitry Andric return;
7960b57cec5SDimitry Andric }
7970b57cec5SDimitry Andric unsigned LineNo = PLoc.getLine();
7980b57cec5SDimitry Andric
7990b57cec5SDimitry Andric if (!DiagOpts->ShowLocation)
8000b57cec5SDimitry Andric return;
8010b57cec5SDimitry Andric
8020b57cec5SDimitry Andric if (DiagOpts->ShowColors)
8030b57cec5SDimitry Andric OS.changeColor(savedColor, true);
8040b57cec5SDimitry Andric
8050b57cec5SDimitry Andric emitFilename(PLoc.getFilename(), Loc.getManager());
8060b57cec5SDimitry Andric switch (DiagOpts->getFormat()) {
807fcaf7f86SDimitry Andric case DiagnosticOptions::SARIF:
808e8d8bef9SDimitry Andric case DiagnosticOptions::Clang:
809e8d8bef9SDimitry Andric if (DiagOpts->ShowLine)
810e8d8bef9SDimitry Andric OS << ':' << LineNo;
811e8d8bef9SDimitry Andric break;
8120b57cec5SDimitry Andric case DiagnosticOptions::MSVC: OS << '(' << LineNo; break;
8130b57cec5SDimitry Andric case DiagnosticOptions::Vi: OS << " +" << LineNo; break;
8140b57cec5SDimitry Andric }
8150b57cec5SDimitry Andric
8160b57cec5SDimitry Andric if (DiagOpts->ShowColumn)
8170b57cec5SDimitry Andric // Compute the column number.
8180b57cec5SDimitry Andric if (unsigned ColNo = PLoc.getColumn()) {
8190b57cec5SDimitry Andric if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) {
8200b57cec5SDimitry Andric OS << ',';
8210b57cec5SDimitry Andric // Visual Studio 2010 or earlier expects column number to be off by one
8220b57cec5SDimitry Andric if (LangOpts.MSCompatibilityVersion &&
8230b57cec5SDimitry Andric !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012))
8240b57cec5SDimitry Andric ColNo--;
8250b57cec5SDimitry Andric } else
8260b57cec5SDimitry Andric OS << ':';
8270b57cec5SDimitry Andric OS << ColNo;
8280b57cec5SDimitry Andric }
8290b57cec5SDimitry Andric switch (DiagOpts->getFormat()) {
830fcaf7f86SDimitry Andric case DiagnosticOptions::SARIF:
8310b57cec5SDimitry Andric case DiagnosticOptions::Clang:
8320b57cec5SDimitry Andric case DiagnosticOptions::Vi: OS << ':'; break;
8330b57cec5SDimitry Andric case DiagnosticOptions::MSVC:
8340b57cec5SDimitry Andric // MSVC2013 and before print 'file(4) : error'. MSVC2015 gets rid of the
8350b57cec5SDimitry Andric // space and prints 'file(4): error'.
8360b57cec5SDimitry Andric OS << ')';
8370b57cec5SDimitry Andric if (LangOpts.MSCompatibilityVersion &&
8380b57cec5SDimitry Andric !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
8390b57cec5SDimitry Andric OS << ' ';
8400b57cec5SDimitry Andric OS << ':';
8410b57cec5SDimitry Andric break;
8420b57cec5SDimitry Andric }
8430b57cec5SDimitry Andric
8440b57cec5SDimitry Andric if (DiagOpts->ShowSourceRanges && !Ranges.empty()) {
8450b57cec5SDimitry Andric FileID CaretFileID = Loc.getExpansionLoc().getFileID();
8460b57cec5SDimitry Andric bool PrintedRange = false;
84706c3fb27SDimitry Andric const SourceManager &SM = Loc.getManager();
8480b57cec5SDimitry Andric
84906c3fb27SDimitry Andric for (const auto &R : Ranges) {
8500b57cec5SDimitry Andric // Ignore invalid ranges.
85106c3fb27SDimitry Andric if (!R.isValid())
85206c3fb27SDimitry Andric continue;
8530b57cec5SDimitry Andric
85406c3fb27SDimitry Andric SourceLocation B = SM.getExpansionLoc(R.getBegin());
85506c3fb27SDimitry Andric CharSourceRange ERange = SM.getExpansionRange(R.getEnd());
8560b57cec5SDimitry Andric SourceLocation E = ERange.getEnd();
8570b57cec5SDimitry Andric
85806c3fb27SDimitry Andric // If the start or end of the range is in another file, just
85906c3fb27SDimitry Andric // discard it.
86006c3fb27SDimitry Andric if (SM.getFileID(B) != CaretFileID || SM.getFileID(E) != CaretFileID)
8610b57cec5SDimitry Andric continue;
8620b57cec5SDimitry Andric
8630b57cec5SDimitry Andric // Add in the length of the token, so that we cover multi-char
8640b57cec5SDimitry Andric // tokens.
8650b57cec5SDimitry Andric unsigned TokSize = 0;
86606c3fb27SDimitry Andric if (ERange.isTokenRange())
8670b57cec5SDimitry Andric TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts);
8680b57cec5SDimitry Andric
8690b57cec5SDimitry Andric FullSourceLoc BF(B, SM), EF(E, SM);
8700b57cec5SDimitry Andric OS << '{'
8710b57cec5SDimitry Andric << BF.getLineNumber() << ':' << BF.getColumnNumber() << '-'
8720b57cec5SDimitry Andric << EF.getLineNumber() << ':' << (EF.getColumnNumber() + TokSize)
8730b57cec5SDimitry Andric << '}';
8740b57cec5SDimitry Andric PrintedRange = true;
8750b57cec5SDimitry Andric }
8760b57cec5SDimitry Andric
8770b57cec5SDimitry Andric if (PrintedRange)
8780b57cec5SDimitry Andric OS << ':';
8790b57cec5SDimitry Andric }
8800b57cec5SDimitry Andric OS << ' ';
8810b57cec5SDimitry Andric }
8820b57cec5SDimitry Andric
emitIncludeLocation(FullSourceLoc Loc,PresumedLoc PLoc)8830b57cec5SDimitry Andric void TextDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) {
88406c3fb27SDimitry Andric if (DiagOpts->ShowLocation && PLoc.isValid()) {
88506c3fb27SDimitry Andric OS << "In file included from ";
88606c3fb27SDimitry Andric emitFilename(PLoc.getFilename(), Loc.getManager());
88706c3fb27SDimitry Andric OS << ':' << PLoc.getLine() << ":\n";
88806c3fb27SDimitry Andric } else
8890b57cec5SDimitry Andric OS << "In included file:\n";
8900b57cec5SDimitry Andric }
8910b57cec5SDimitry Andric
emitImportLocation(FullSourceLoc Loc,PresumedLoc PLoc,StringRef ModuleName)8920b57cec5SDimitry Andric void TextDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
8930b57cec5SDimitry Andric StringRef ModuleName) {
8940b57cec5SDimitry Andric if (DiagOpts->ShowLocation && PLoc.isValid())
8950b57cec5SDimitry Andric OS << "In module '" << ModuleName << "' imported from "
8960b57cec5SDimitry Andric << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n";
8970b57cec5SDimitry Andric else
8980b57cec5SDimitry Andric OS << "In module '" << ModuleName << "':\n";
8990b57cec5SDimitry Andric }
9000b57cec5SDimitry Andric
emitBuildingModuleLocation(FullSourceLoc Loc,PresumedLoc PLoc,StringRef ModuleName)9010b57cec5SDimitry Andric void TextDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc,
9020b57cec5SDimitry Andric PresumedLoc PLoc,
9030b57cec5SDimitry Andric StringRef ModuleName) {
9040b57cec5SDimitry Andric if (DiagOpts->ShowLocation && PLoc.isValid())
9050b57cec5SDimitry Andric OS << "While building module '" << ModuleName << "' imported from "
9060b57cec5SDimitry Andric << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n";
9070b57cec5SDimitry Andric else
9080b57cec5SDimitry Andric OS << "While building module '" << ModuleName << "':\n";
9090b57cec5SDimitry Andric }
9100b57cec5SDimitry Andric
9110b57cec5SDimitry Andric /// Find the suitable set of lines to show to include a set of ranges.
912bdd1243dSDimitry Andric static std::optional<std::pair<unsigned, unsigned>>
findLinesForRange(const CharSourceRange & R,FileID FID,const SourceManager & SM)9130b57cec5SDimitry Andric findLinesForRange(const CharSourceRange &R, FileID FID,
9140b57cec5SDimitry Andric const SourceManager &SM) {
915bdd1243dSDimitry Andric if (!R.isValid())
916bdd1243dSDimitry Andric return std::nullopt;
9170b57cec5SDimitry Andric
9180b57cec5SDimitry Andric SourceLocation Begin = R.getBegin();
9190b57cec5SDimitry Andric SourceLocation End = R.getEnd();
9200b57cec5SDimitry Andric if (SM.getFileID(Begin) != FID || SM.getFileID(End) != FID)
921bdd1243dSDimitry Andric return std::nullopt;
9220b57cec5SDimitry Andric
9230b57cec5SDimitry Andric return std::make_pair(SM.getExpansionLineNumber(Begin),
9240b57cec5SDimitry Andric SM.getExpansionLineNumber(End));
9250b57cec5SDimitry Andric }
9260b57cec5SDimitry Andric
9270b57cec5SDimitry Andric /// Add as much of range B into range A as possible without exceeding a maximum
9280b57cec5SDimitry Andric /// size of MaxRange. Ranges are inclusive.
9290b57cec5SDimitry Andric static std::pair<unsigned, unsigned>
maybeAddRange(std::pair<unsigned,unsigned> A,std::pair<unsigned,unsigned> B,unsigned MaxRange)9300b57cec5SDimitry Andric maybeAddRange(std::pair<unsigned, unsigned> A, std::pair<unsigned, unsigned> B,
9310b57cec5SDimitry Andric unsigned MaxRange) {
9320b57cec5SDimitry Andric // If A is already the maximum size, we're done.
9330b57cec5SDimitry Andric unsigned Slack = MaxRange - (A.second - A.first + 1);
9340b57cec5SDimitry Andric if (Slack == 0)
9350b57cec5SDimitry Andric return A;
9360b57cec5SDimitry Andric
9370b57cec5SDimitry Andric // Easy case: merge succeeds within MaxRange.
9380b57cec5SDimitry Andric unsigned Min = std::min(A.first, B.first);
9390b57cec5SDimitry Andric unsigned Max = std::max(A.second, B.second);
9400b57cec5SDimitry Andric if (Max - Min + 1 <= MaxRange)
9410b57cec5SDimitry Andric return {Min, Max};
9420b57cec5SDimitry Andric
9430b57cec5SDimitry Andric // If we can't reach B from A within MaxRange, there's nothing to do.
9440b57cec5SDimitry Andric // Don't add lines to the range that contain nothing interesting.
9450b57cec5SDimitry Andric if ((B.first > A.first && B.first - A.first + 1 > MaxRange) ||
9460b57cec5SDimitry Andric (B.second < A.second && A.second - B.second + 1 > MaxRange))
9470b57cec5SDimitry Andric return A;
9480b57cec5SDimitry Andric
9490b57cec5SDimitry Andric // Otherwise, expand A towards B to produce a range of size MaxRange. We
9500b57cec5SDimitry Andric // attempt to expand by the same amount in both directions if B strictly
9510b57cec5SDimitry Andric // contains A.
9520b57cec5SDimitry Andric
9530b57cec5SDimitry Andric // Expand downwards by up to half the available amount, then upwards as
9540b57cec5SDimitry Andric // much as possible, then downwards as much as possible.
9550b57cec5SDimitry Andric A.second = std::min(A.second + (Slack + 1) / 2, Max);
9560b57cec5SDimitry Andric Slack = MaxRange - (A.second - A.first + 1);
9570b57cec5SDimitry Andric A.first = std::max(Min + Slack, A.first) - Slack;
9580b57cec5SDimitry Andric A.second = std::min(A.first + MaxRange - 1, Max);
9590b57cec5SDimitry Andric return A;
9600b57cec5SDimitry Andric }
9610b57cec5SDimitry Andric
96206c3fb27SDimitry Andric struct LineRange {
96306c3fb27SDimitry Andric unsigned LineNo;
96406c3fb27SDimitry Andric unsigned StartCol;
96506c3fb27SDimitry Andric unsigned EndCol;
96606c3fb27SDimitry Andric };
9670b57cec5SDimitry Andric
96806c3fb27SDimitry Andric /// Highlight \p R (with ~'s) on the current source line.
highlightRange(const LineRange & R,const SourceColumnMap & Map,std::string & CaretLine)96906c3fb27SDimitry Andric static void highlightRange(const LineRange &R, const SourceColumnMap &Map,
97006c3fb27SDimitry Andric std::string &CaretLine) {
9710b57cec5SDimitry Andric // Pick the first non-whitespace column.
97206c3fb27SDimitry Andric unsigned StartColNo = R.StartCol;
97306c3fb27SDimitry Andric while (StartColNo < Map.getSourceLine().size() &&
97406c3fb27SDimitry Andric (Map.getSourceLine()[StartColNo] == ' ' ||
97506c3fb27SDimitry Andric Map.getSourceLine()[StartColNo] == '\t'))
97606c3fb27SDimitry Andric StartColNo = Map.startOfNextColumn(StartColNo);
9770b57cec5SDimitry Andric
9780b57cec5SDimitry Andric // Pick the last non-whitespace column.
97906c3fb27SDimitry Andric unsigned EndColNo =
98006c3fb27SDimitry Andric std::min(static_cast<size_t>(R.EndCol), Map.getSourceLine().size());
98106c3fb27SDimitry Andric while (EndColNo && (Map.getSourceLine()[EndColNo - 1] == ' ' ||
98206c3fb27SDimitry Andric Map.getSourceLine()[EndColNo - 1] == '\t'))
98306c3fb27SDimitry Andric EndColNo = Map.startOfPreviousColumn(EndColNo);
9840b57cec5SDimitry Andric
9850b57cec5SDimitry Andric // If the start/end passed each other, then we are trying to highlight a
9860b57cec5SDimitry Andric // range that just exists in whitespace. That most likely means we have
9870b57cec5SDimitry Andric // a multi-line highlighting range that covers a blank line.
98806c3fb27SDimitry Andric if (StartColNo > EndColNo)
98906c3fb27SDimitry Andric return;
9900b57cec5SDimitry Andric
9910b57cec5SDimitry Andric // Fill the range with ~'s.
99206c3fb27SDimitry Andric StartColNo = Map.byteToContainingColumn(StartColNo);
99306c3fb27SDimitry Andric EndColNo = Map.byteToContainingColumn(EndColNo);
9940b57cec5SDimitry Andric
9950b57cec5SDimitry Andric assert(StartColNo <= EndColNo && "Invalid range!");
9960b57cec5SDimitry Andric if (CaretLine.size() < EndColNo)
9970b57cec5SDimitry Andric CaretLine.resize(EndColNo, ' ');
9980b57cec5SDimitry Andric std::fill(CaretLine.begin() + StartColNo, CaretLine.begin() + EndColNo, '~');
9990b57cec5SDimitry Andric }
10000b57cec5SDimitry Andric
buildFixItInsertionLine(FileID FID,unsigned LineNo,const SourceColumnMap & map,ArrayRef<FixItHint> Hints,const SourceManager & SM,const DiagnosticOptions * DiagOpts)10010b57cec5SDimitry Andric static std::string buildFixItInsertionLine(FileID FID,
10020b57cec5SDimitry Andric unsigned LineNo,
10030b57cec5SDimitry Andric const SourceColumnMap &map,
10040b57cec5SDimitry Andric ArrayRef<FixItHint> Hints,
10050b57cec5SDimitry Andric const SourceManager &SM,
10060b57cec5SDimitry Andric const DiagnosticOptions *DiagOpts) {
10070b57cec5SDimitry Andric std::string FixItInsertionLine;
10080b57cec5SDimitry Andric if (Hints.empty() || !DiagOpts->ShowFixits)
10090b57cec5SDimitry Andric return FixItInsertionLine;
10100b57cec5SDimitry Andric unsigned PrevHintEndCol = 0;
10110b57cec5SDimitry Andric
101206c3fb27SDimitry Andric for (const auto &H : Hints) {
101306c3fb27SDimitry Andric if (H.CodeToInsert.empty())
101406c3fb27SDimitry Andric continue;
101506c3fb27SDimitry Andric
10160b57cec5SDimitry Andric // We have an insertion hint. Determine whether the inserted
10170b57cec5SDimitry Andric // code contains no newlines and is on the same line as the caret.
101806c3fb27SDimitry Andric std::pair<FileID, unsigned> HintLocInfo =
101906c3fb27SDimitry Andric SM.getDecomposedExpansionLoc(H.RemoveRange.getBegin());
10200b57cec5SDimitry Andric if (FID == HintLocInfo.first &&
10210b57cec5SDimitry Andric LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) &&
102206c3fb27SDimitry Andric StringRef(H.CodeToInsert).find_first_of("\n\r") == StringRef::npos) {
10230b57cec5SDimitry Andric // Insert the new code into the line just below the code
10240b57cec5SDimitry Andric // that the user wrote.
10250b57cec5SDimitry Andric // Note: When modifying this function, be very careful about what is a
10260b57cec5SDimitry Andric // "column" (printed width, platform-dependent) and what is a
10270b57cec5SDimitry Andric // "byte offset" (SourceManager "column").
102806c3fb27SDimitry Andric unsigned HintByteOffset =
102906c3fb27SDimitry Andric SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1;
10300b57cec5SDimitry Andric
10310b57cec5SDimitry Andric // The hint must start inside the source or right at the end
10320b57cec5SDimitry Andric assert(HintByteOffset < static_cast<unsigned>(map.bytes()) + 1);
10330b57cec5SDimitry Andric unsigned HintCol = map.byteToContainingColumn(HintByteOffset);
10340b57cec5SDimitry Andric
10350b57cec5SDimitry Andric // If we inserted a long previous hint, push this one forwards, and add
10360b57cec5SDimitry Andric // an extra space to show that this is not part of the previous
10370b57cec5SDimitry Andric // completion. This is sort of the best we can do when two hints appear
10380b57cec5SDimitry Andric // to overlap.
10390b57cec5SDimitry Andric //
10400b57cec5SDimitry Andric // Note that if this hint is located immediately after the previous
10410b57cec5SDimitry Andric // hint, no space will be added, since the location is more important.
10420b57cec5SDimitry Andric if (HintCol < PrevHintEndCol)
10430b57cec5SDimitry Andric HintCol = PrevHintEndCol + 1;
10440b57cec5SDimitry Andric
10450b57cec5SDimitry Andric // This should NOT use HintByteOffset, because the source might have
10460b57cec5SDimitry Andric // Unicode characters in earlier columns.
10470b57cec5SDimitry Andric unsigned NewFixItLineSize = FixItInsertionLine.size() +
104806c3fb27SDimitry Andric (HintCol - PrevHintEndCol) +
104906c3fb27SDimitry Andric H.CodeToInsert.size();
10500b57cec5SDimitry Andric if (NewFixItLineSize > FixItInsertionLine.size())
10510b57cec5SDimitry Andric FixItInsertionLine.resize(NewFixItLineSize, ' ');
10520b57cec5SDimitry Andric
105306c3fb27SDimitry Andric std::copy(H.CodeToInsert.begin(), H.CodeToInsert.end(),
105406c3fb27SDimitry Andric FixItInsertionLine.end() - H.CodeToInsert.size());
10550b57cec5SDimitry Andric
105606c3fb27SDimitry Andric PrevHintEndCol = HintCol + llvm::sys::locale::columnWidth(H.CodeToInsert);
10570b57cec5SDimitry Andric }
10580b57cec5SDimitry Andric }
10590b57cec5SDimitry Andric
10600b57cec5SDimitry Andric expandTabs(FixItInsertionLine, DiagOpts->TabStop);
10610b57cec5SDimitry Andric
10620b57cec5SDimitry Andric return FixItInsertionLine;
10630b57cec5SDimitry Andric }
10640b57cec5SDimitry Andric
getNumDisplayWidth(unsigned N)106506c3fb27SDimitry Andric static unsigned getNumDisplayWidth(unsigned N) {
106606c3fb27SDimitry Andric unsigned L = 1u, M = 10u;
106706c3fb27SDimitry Andric while (M <= N && ++L != std::numeric_limits<unsigned>::digits10 + 1)
106806c3fb27SDimitry Andric M *= 10u;
106906c3fb27SDimitry Andric
107006c3fb27SDimitry Andric return L;
107106c3fb27SDimitry Andric }
107206c3fb27SDimitry Andric
107306c3fb27SDimitry Andric /// Filter out invalid ranges, ranges that don't fit into the window of
107406c3fb27SDimitry Andric /// source lines we will print, and ranges from other files.
107506c3fb27SDimitry Andric ///
107606c3fb27SDimitry Andric /// For the remaining ranges, convert them to simple LineRange structs,
107706c3fb27SDimitry Andric /// which only cover one line at a time.
107806c3fb27SDimitry Andric static SmallVector<LineRange>
prepareAndFilterRanges(const SmallVectorImpl<CharSourceRange> & Ranges,const SourceManager & SM,const std::pair<unsigned,unsigned> & Lines,FileID FID,const LangOptions & LangOpts)107906c3fb27SDimitry Andric prepareAndFilterRanges(const SmallVectorImpl<CharSourceRange> &Ranges,
108006c3fb27SDimitry Andric const SourceManager &SM,
108106c3fb27SDimitry Andric const std::pair<unsigned, unsigned> &Lines, FileID FID,
108206c3fb27SDimitry Andric const LangOptions &LangOpts) {
108306c3fb27SDimitry Andric SmallVector<LineRange> LineRanges;
108406c3fb27SDimitry Andric
108506c3fb27SDimitry Andric for (const CharSourceRange &R : Ranges) {
108606c3fb27SDimitry Andric if (R.isInvalid())
108706c3fb27SDimitry Andric continue;
108806c3fb27SDimitry Andric SourceLocation Begin = R.getBegin();
108906c3fb27SDimitry Andric SourceLocation End = R.getEnd();
109006c3fb27SDimitry Andric
109106c3fb27SDimitry Andric unsigned StartLineNo = SM.getExpansionLineNumber(Begin);
109206c3fb27SDimitry Andric if (StartLineNo > Lines.second || SM.getFileID(Begin) != FID)
109306c3fb27SDimitry Andric continue;
109406c3fb27SDimitry Andric
109506c3fb27SDimitry Andric unsigned EndLineNo = SM.getExpansionLineNumber(End);
109606c3fb27SDimitry Andric if (EndLineNo < Lines.first || SM.getFileID(End) != FID)
109706c3fb27SDimitry Andric continue;
109806c3fb27SDimitry Andric
109906c3fb27SDimitry Andric unsigned StartColumn = SM.getExpansionColumnNumber(Begin);
110006c3fb27SDimitry Andric unsigned EndColumn = SM.getExpansionColumnNumber(End);
110106c3fb27SDimitry Andric if (R.isTokenRange())
110206c3fb27SDimitry Andric EndColumn += Lexer::MeasureTokenLength(End, SM, LangOpts);
110306c3fb27SDimitry Andric
110406c3fb27SDimitry Andric // Only a single line.
110506c3fb27SDimitry Andric if (StartLineNo == EndLineNo) {
110606c3fb27SDimitry Andric LineRanges.push_back({StartLineNo, StartColumn - 1, EndColumn - 1});
110706c3fb27SDimitry Andric continue;
110806c3fb27SDimitry Andric }
110906c3fb27SDimitry Andric
111006c3fb27SDimitry Andric // Start line.
111106c3fb27SDimitry Andric LineRanges.push_back({StartLineNo, StartColumn - 1, ~0u});
111206c3fb27SDimitry Andric
111306c3fb27SDimitry Andric // Middle lines.
111406c3fb27SDimitry Andric for (unsigned S = StartLineNo + 1; S != EndLineNo; ++S)
111506c3fb27SDimitry Andric LineRanges.push_back({S, 0, ~0u});
111606c3fb27SDimitry Andric
111706c3fb27SDimitry Andric // End line.
111806c3fb27SDimitry Andric LineRanges.push_back({EndLineNo, 0, EndColumn - 1});
111906c3fb27SDimitry Andric }
112006c3fb27SDimitry Andric
112106c3fb27SDimitry Andric return LineRanges;
112206c3fb27SDimitry Andric }
112306c3fb27SDimitry Andric
1124*0fca6ea1SDimitry Andric /// Creates syntax highlighting information in form of StyleRanges.
1125*0fca6ea1SDimitry Andric ///
1126*0fca6ea1SDimitry Andric /// The returned unique ptr has always exactly size
1127*0fca6ea1SDimitry Andric /// (\p EndLineNumber - \p StartLineNumber + 1). Each SmallVector in there
1128*0fca6ea1SDimitry Andric /// corresponds to syntax highlighting information in one line. In each line,
1129*0fca6ea1SDimitry Andric /// the StyleRanges are non-overlapping and sorted from start to end of the
1130*0fca6ea1SDimitry Andric /// line.
1131*0fca6ea1SDimitry Andric static std::unique_ptr<llvm::SmallVector<TextDiagnostic::StyleRange>[]>
highlightLines(StringRef FileData,unsigned StartLineNumber,unsigned EndLineNumber,const Preprocessor * PP,const LangOptions & LangOpts,bool ShowColors,FileID FID,const SourceManager & SM)1132*0fca6ea1SDimitry Andric highlightLines(StringRef FileData, unsigned StartLineNumber,
1133*0fca6ea1SDimitry Andric unsigned EndLineNumber, const Preprocessor *PP,
1134*0fca6ea1SDimitry Andric const LangOptions &LangOpts, bool ShowColors, FileID FID,
1135*0fca6ea1SDimitry Andric const SourceManager &SM) {
1136*0fca6ea1SDimitry Andric assert(StartLineNumber <= EndLineNumber);
1137*0fca6ea1SDimitry Andric auto SnippetRanges =
1138*0fca6ea1SDimitry Andric std::make_unique<SmallVector<TextDiagnostic::StyleRange>[]>(
1139*0fca6ea1SDimitry Andric EndLineNumber - StartLineNumber + 1);
1140*0fca6ea1SDimitry Andric
1141*0fca6ea1SDimitry Andric if (!PP || !ShowColors)
1142*0fca6ea1SDimitry Andric return SnippetRanges;
1143*0fca6ea1SDimitry Andric
1144*0fca6ea1SDimitry Andric // Might cause emission of another diagnostic.
1145*0fca6ea1SDimitry Andric if (PP->getIdentifierTable().getExternalIdentifierLookup())
1146*0fca6ea1SDimitry Andric return SnippetRanges;
1147*0fca6ea1SDimitry Andric
1148*0fca6ea1SDimitry Andric auto Buff = llvm::MemoryBuffer::getMemBuffer(FileData);
1149*0fca6ea1SDimitry Andric Lexer L{FID, *Buff, SM, LangOpts};
1150*0fca6ea1SDimitry Andric L.SetKeepWhitespaceMode(true);
1151*0fca6ea1SDimitry Andric
1152*0fca6ea1SDimitry Andric const char *FirstLineStart =
1153*0fca6ea1SDimitry Andric FileData.data() +
1154*0fca6ea1SDimitry Andric SM.getDecomposedLoc(SM.translateLineCol(FID, StartLineNumber, 1)).second;
1155*0fca6ea1SDimitry Andric if (const char *CheckPoint = PP->getCheckPoint(FID, FirstLineStart)) {
1156*0fca6ea1SDimitry Andric assert(CheckPoint >= Buff->getBufferStart() &&
1157*0fca6ea1SDimitry Andric CheckPoint <= Buff->getBufferEnd());
1158*0fca6ea1SDimitry Andric assert(CheckPoint <= FirstLineStart);
1159*0fca6ea1SDimitry Andric size_t Offset = CheckPoint - Buff->getBufferStart();
1160*0fca6ea1SDimitry Andric L.seek(Offset, /*IsAtStartOfLine=*/false);
1161*0fca6ea1SDimitry Andric }
1162*0fca6ea1SDimitry Andric
1163*0fca6ea1SDimitry Andric // Classify the given token and append it to the given vector.
1164*0fca6ea1SDimitry Andric auto appendStyle =
1165*0fca6ea1SDimitry Andric [PP, &LangOpts](SmallVector<TextDiagnostic::StyleRange> &Vec,
1166*0fca6ea1SDimitry Andric const Token &T, unsigned Start, unsigned Length) -> void {
1167*0fca6ea1SDimitry Andric if (T.is(tok::raw_identifier)) {
1168*0fca6ea1SDimitry Andric StringRef RawIdent = T.getRawIdentifier();
1169*0fca6ea1SDimitry Andric // Special case true/false/nullptr/... literals, since they will otherwise
1170*0fca6ea1SDimitry Andric // be treated as keywords.
1171*0fca6ea1SDimitry Andric // FIXME: It would be good to have a programmatic way of getting this
1172*0fca6ea1SDimitry Andric // list.
1173*0fca6ea1SDimitry Andric if (llvm::StringSwitch<bool>(RawIdent)
1174*0fca6ea1SDimitry Andric .Case("true", true)
1175*0fca6ea1SDimitry Andric .Case("false", true)
1176*0fca6ea1SDimitry Andric .Case("nullptr", true)
1177*0fca6ea1SDimitry Andric .Case("__func__", true)
1178*0fca6ea1SDimitry Andric .Case("__objc_yes__", true)
1179*0fca6ea1SDimitry Andric .Case("__objc_no__", true)
1180*0fca6ea1SDimitry Andric .Case("__null", true)
1181*0fca6ea1SDimitry Andric .Case("__FUNCDNAME__", true)
1182*0fca6ea1SDimitry Andric .Case("__FUNCSIG__", true)
1183*0fca6ea1SDimitry Andric .Case("__FUNCTION__", true)
1184*0fca6ea1SDimitry Andric .Case("__FUNCSIG__", true)
1185*0fca6ea1SDimitry Andric .Default(false)) {
1186*0fca6ea1SDimitry Andric Vec.emplace_back(Start, Start + Length, LiteralColor);
1187*0fca6ea1SDimitry Andric } else {
1188*0fca6ea1SDimitry Andric const IdentifierInfo *II = PP->getIdentifierInfo(RawIdent);
1189*0fca6ea1SDimitry Andric assert(II);
1190*0fca6ea1SDimitry Andric if (II->isKeyword(LangOpts))
1191*0fca6ea1SDimitry Andric Vec.emplace_back(Start, Start + Length, KeywordColor);
1192*0fca6ea1SDimitry Andric }
1193*0fca6ea1SDimitry Andric } else if (tok::isLiteral(T.getKind())) {
1194*0fca6ea1SDimitry Andric Vec.emplace_back(Start, Start + Length, LiteralColor);
1195*0fca6ea1SDimitry Andric } else {
1196*0fca6ea1SDimitry Andric assert(T.is(tok::comment));
1197*0fca6ea1SDimitry Andric Vec.emplace_back(Start, Start + Length, CommentColor);
1198*0fca6ea1SDimitry Andric }
1199*0fca6ea1SDimitry Andric };
1200*0fca6ea1SDimitry Andric
1201*0fca6ea1SDimitry Andric bool Stop = false;
1202*0fca6ea1SDimitry Andric while (!Stop) {
1203*0fca6ea1SDimitry Andric Token T;
1204*0fca6ea1SDimitry Andric Stop = L.LexFromRawLexer(T);
1205*0fca6ea1SDimitry Andric if (T.is(tok::unknown))
1206*0fca6ea1SDimitry Andric continue;
1207*0fca6ea1SDimitry Andric
1208*0fca6ea1SDimitry Andric // We are only interested in identifiers, literals and comments.
1209*0fca6ea1SDimitry Andric if (!T.is(tok::raw_identifier) && !T.is(tok::comment) &&
1210*0fca6ea1SDimitry Andric !tok::isLiteral(T.getKind()))
1211*0fca6ea1SDimitry Andric continue;
1212*0fca6ea1SDimitry Andric
1213*0fca6ea1SDimitry Andric bool Invalid = false;
1214*0fca6ea1SDimitry Andric unsigned TokenEndLine = SM.getSpellingLineNumber(T.getEndLoc(), &Invalid);
1215*0fca6ea1SDimitry Andric if (Invalid || TokenEndLine < StartLineNumber)
1216*0fca6ea1SDimitry Andric continue;
1217*0fca6ea1SDimitry Andric
1218*0fca6ea1SDimitry Andric assert(TokenEndLine >= StartLineNumber);
1219*0fca6ea1SDimitry Andric
1220*0fca6ea1SDimitry Andric unsigned TokenStartLine =
1221*0fca6ea1SDimitry Andric SM.getSpellingLineNumber(T.getLocation(), &Invalid);
1222*0fca6ea1SDimitry Andric if (Invalid)
1223*0fca6ea1SDimitry Andric continue;
1224*0fca6ea1SDimitry Andric // If this happens, we're done.
1225*0fca6ea1SDimitry Andric if (TokenStartLine > EndLineNumber)
1226*0fca6ea1SDimitry Andric break;
1227*0fca6ea1SDimitry Andric
1228*0fca6ea1SDimitry Andric unsigned StartCol =
1229*0fca6ea1SDimitry Andric SM.getSpellingColumnNumber(T.getLocation(), &Invalid) - 1;
1230*0fca6ea1SDimitry Andric if (Invalid)
1231*0fca6ea1SDimitry Andric continue;
1232*0fca6ea1SDimitry Andric
1233*0fca6ea1SDimitry Andric // Simple tokens.
1234*0fca6ea1SDimitry Andric if (TokenStartLine == TokenEndLine) {
1235*0fca6ea1SDimitry Andric SmallVector<TextDiagnostic::StyleRange> &LineRanges =
1236*0fca6ea1SDimitry Andric SnippetRanges[TokenStartLine - StartLineNumber];
1237*0fca6ea1SDimitry Andric appendStyle(LineRanges, T, StartCol, T.getLength());
1238*0fca6ea1SDimitry Andric continue;
1239*0fca6ea1SDimitry Andric }
1240*0fca6ea1SDimitry Andric assert((TokenEndLine - TokenStartLine) >= 1);
1241*0fca6ea1SDimitry Andric
1242*0fca6ea1SDimitry Andric // For tokens that span multiple lines (think multiline comments), we
1243*0fca6ea1SDimitry Andric // divide them into multiple StyleRanges.
1244*0fca6ea1SDimitry Andric unsigned EndCol = SM.getSpellingColumnNumber(T.getEndLoc(), &Invalid) - 1;
1245*0fca6ea1SDimitry Andric if (Invalid)
1246*0fca6ea1SDimitry Andric continue;
1247*0fca6ea1SDimitry Andric
1248*0fca6ea1SDimitry Andric std::string Spelling = Lexer::getSpelling(T, SM, LangOpts);
1249*0fca6ea1SDimitry Andric
1250*0fca6ea1SDimitry Andric unsigned L = TokenStartLine;
1251*0fca6ea1SDimitry Andric unsigned LineLength = 0;
1252*0fca6ea1SDimitry Andric for (unsigned I = 0; I <= Spelling.size(); ++I) {
1253*0fca6ea1SDimitry Andric // This line is done.
1254*0fca6ea1SDimitry Andric if (I == Spelling.size() || isVerticalWhitespace(Spelling[I])) {
1255*0fca6ea1SDimitry Andric SmallVector<TextDiagnostic::StyleRange> &LineRanges =
1256*0fca6ea1SDimitry Andric SnippetRanges[L - StartLineNumber];
1257*0fca6ea1SDimitry Andric
1258*0fca6ea1SDimitry Andric if (L >= StartLineNumber) {
1259*0fca6ea1SDimitry Andric if (L == TokenStartLine) // First line
1260*0fca6ea1SDimitry Andric appendStyle(LineRanges, T, StartCol, LineLength);
1261*0fca6ea1SDimitry Andric else if (L == TokenEndLine) // Last line
1262*0fca6ea1SDimitry Andric appendStyle(LineRanges, T, 0, EndCol);
1263*0fca6ea1SDimitry Andric else
1264*0fca6ea1SDimitry Andric appendStyle(LineRanges, T, 0, LineLength);
1265*0fca6ea1SDimitry Andric }
1266*0fca6ea1SDimitry Andric
1267*0fca6ea1SDimitry Andric ++L;
1268*0fca6ea1SDimitry Andric if (L > EndLineNumber)
1269*0fca6ea1SDimitry Andric break;
1270*0fca6ea1SDimitry Andric LineLength = 0;
1271*0fca6ea1SDimitry Andric continue;
1272*0fca6ea1SDimitry Andric }
1273*0fca6ea1SDimitry Andric ++LineLength;
1274*0fca6ea1SDimitry Andric }
1275*0fca6ea1SDimitry Andric }
1276*0fca6ea1SDimitry Andric
1277*0fca6ea1SDimitry Andric return SnippetRanges;
1278*0fca6ea1SDimitry Andric }
1279*0fca6ea1SDimitry Andric
12800b57cec5SDimitry Andric /// Emit a code snippet and caret line.
12810b57cec5SDimitry Andric ///
12820b57cec5SDimitry Andric /// This routine emits a single line's code snippet and caret line..
12830b57cec5SDimitry Andric ///
12840b57cec5SDimitry Andric /// \param Loc The location for the caret.
12850b57cec5SDimitry Andric /// \param Ranges The underlined ranges for this code snippet.
12860b57cec5SDimitry Andric /// \param Hints The FixIt hints active for this diagnostic.
emitSnippetAndCaret(FullSourceLoc Loc,DiagnosticsEngine::Level Level,SmallVectorImpl<CharSourceRange> & Ranges,ArrayRef<FixItHint> Hints)12870b57cec5SDimitry Andric void TextDiagnostic::emitSnippetAndCaret(
12880b57cec5SDimitry Andric FullSourceLoc Loc, DiagnosticsEngine::Level Level,
12890b57cec5SDimitry Andric SmallVectorImpl<CharSourceRange> &Ranges, ArrayRef<FixItHint> Hints) {
12900b57cec5SDimitry Andric assert(Loc.isValid() && "must have a valid source location here");
12910b57cec5SDimitry Andric assert(Loc.isFileID() && "must have a file location here");
12920b57cec5SDimitry Andric
12930b57cec5SDimitry Andric // If caret diagnostics are enabled and we have location, we want to
12940b57cec5SDimitry Andric // emit the caret. However, we only do this if the location moved
12950b57cec5SDimitry Andric // from the last diagnostic, if the last diagnostic was a note that
12960b57cec5SDimitry Andric // was part of a different warning or error diagnostic, or if the
12970b57cec5SDimitry Andric // diagnostic has ranges. We don't want to emit the same caret
12980b57cec5SDimitry Andric // multiple times if one loc has multiple diagnostics.
12990b57cec5SDimitry Andric if (!DiagOpts->ShowCarets)
13000b57cec5SDimitry Andric return;
13010b57cec5SDimitry Andric if (Loc == LastLoc && Ranges.empty() && Hints.empty() &&
13020b57cec5SDimitry Andric (LastLevel != DiagnosticsEngine::Note || Level == LastLevel))
13030b57cec5SDimitry Andric return;
13040b57cec5SDimitry Andric
130506c3fb27SDimitry Andric FileID FID = Loc.getFileID();
13060b57cec5SDimitry Andric const SourceManager &SM = Loc.getManager();
13070b57cec5SDimitry Andric
13080b57cec5SDimitry Andric // Get information about the buffer it points into.
13090b57cec5SDimitry Andric bool Invalid = false;
13100b57cec5SDimitry Andric StringRef BufData = Loc.getBufferData(&Invalid);
13110b57cec5SDimitry Andric if (Invalid)
13120b57cec5SDimitry Andric return;
131306c3fb27SDimitry Andric const char *BufStart = BufData.data();
131406c3fb27SDimitry Andric const char *BufEnd = BufStart + BufData.size();
13150b57cec5SDimitry Andric
13160b57cec5SDimitry Andric unsigned CaretLineNo = Loc.getLineNumber();
13170b57cec5SDimitry Andric unsigned CaretColNo = Loc.getColumnNumber();
13180b57cec5SDimitry Andric
13190b57cec5SDimitry Andric // Arbitrarily stop showing snippets when the line is too long.
13200b57cec5SDimitry Andric static const size_t MaxLineLengthToPrint = 4096;
13210b57cec5SDimitry Andric if (CaretColNo > MaxLineLengthToPrint)
13220b57cec5SDimitry Andric return;
13230b57cec5SDimitry Andric
13240b57cec5SDimitry Andric // Find the set of lines to include.
13250b57cec5SDimitry Andric const unsigned MaxLines = DiagOpts->SnippetLineLimit;
13260b57cec5SDimitry Andric std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo};
13274542f901SDimitry Andric unsigned DisplayLineNo = Loc.getPresumedLoc().getLine();
132806c3fb27SDimitry Andric for (const auto &I : Ranges) {
132906c3fb27SDimitry Andric if (auto OptionalRange = findLinesForRange(I, FID, SM))
13300b57cec5SDimitry Andric Lines = maybeAddRange(Lines, *OptionalRange, MaxLines);
13310b57cec5SDimitry Andric
133206c3fb27SDimitry Andric DisplayLineNo =
133306c3fb27SDimitry Andric std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin()));
133406c3fb27SDimitry Andric }
13350b57cec5SDimitry Andric
133606c3fb27SDimitry Andric // Our line numbers look like:
133706c3fb27SDimitry Andric // " [number] | "
133806c3fb27SDimitry Andric // Where [number] is MaxLineNoDisplayWidth columns
133906c3fb27SDimitry Andric // and the full thing is therefore MaxLineNoDisplayWidth + 4 columns.
134006c3fb27SDimitry Andric unsigned MaxLineNoDisplayWidth =
134106c3fb27SDimitry Andric DiagOpts->ShowLineNumbers
134206c3fb27SDimitry Andric ? std::max(4u, getNumDisplayWidth(DisplayLineNo + MaxLines))
134306c3fb27SDimitry Andric : 0;
134406c3fb27SDimitry Andric auto indentForLineNumbers = [&] {
134506c3fb27SDimitry Andric if (MaxLineNoDisplayWidth > 0)
134606c3fb27SDimitry Andric OS.indent(MaxLineNoDisplayWidth + 2) << "| ";
134706c3fb27SDimitry Andric };
134806c3fb27SDimitry Andric
1349*0fca6ea1SDimitry Andric // Prepare source highlighting information for the lines we're about to
1350*0fca6ea1SDimitry Andric // emit, starting from the first line.
1351*0fca6ea1SDimitry Andric std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
1352*0fca6ea1SDimitry Andric highlightLines(BufData, Lines.first, Lines.second, PP, LangOpts,
1353*0fca6ea1SDimitry Andric DiagOpts->ShowColors, FID, SM);
1354*0fca6ea1SDimitry Andric
135506c3fb27SDimitry Andric SmallVector<LineRange> LineRanges =
135606c3fb27SDimitry Andric prepareAndFilterRanges(Ranges, SM, Lines, FID, LangOpts);
135706c3fb27SDimitry Andric
135806c3fb27SDimitry Andric for (unsigned LineNo = Lines.first; LineNo != Lines.second + 1;
135906c3fb27SDimitry Andric ++LineNo, ++DisplayLineNo) {
13600b57cec5SDimitry Andric // Rewind from the current position to the start of the line.
13610b57cec5SDimitry Andric const char *LineStart =
13620b57cec5SDimitry Andric BufStart +
13630b57cec5SDimitry Andric SM.getDecomposedLoc(SM.translateLineCol(FID, LineNo, 1)).second;
13640b57cec5SDimitry Andric if (LineStart == BufEnd)
13650b57cec5SDimitry Andric break;
13660b57cec5SDimitry Andric
13670b57cec5SDimitry Andric // Compute the line end.
13680b57cec5SDimitry Andric const char *LineEnd = LineStart;
13690b57cec5SDimitry Andric while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd)
13700b57cec5SDimitry Andric ++LineEnd;
13710b57cec5SDimitry Andric
13720b57cec5SDimitry Andric // Arbitrarily stop showing snippets when the line is too long.
13730b57cec5SDimitry Andric // FIXME: Don't print any lines in this case.
13740b57cec5SDimitry Andric if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint)
13750b57cec5SDimitry Andric return;
13760b57cec5SDimitry Andric
13770b57cec5SDimitry Andric // Copy the line of code into an std::string for ease of manipulation.
137806c3fb27SDimitry Andric std::string SourceLine(LineStart, LineEnd);
137906c3fb27SDimitry Andric // Remove trailing null bytes.
138006c3fb27SDimitry Andric while (!SourceLine.empty() && SourceLine.back() == '\0' &&
138106c3fb27SDimitry Andric (LineNo != CaretLineNo || SourceLine.size() > CaretColNo))
138206c3fb27SDimitry Andric SourceLine.pop_back();
13830b57cec5SDimitry Andric
13840b57cec5SDimitry Andric // Build the byte to column map.
13850b57cec5SDimitry Andric const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop);
13860b57cec5SDimitry Andric
138706c3fb27SDimitry Andric std::string CaretLine;
13880b57cec5SDimitry Andric // Highlight all of the characters covered by Ranges with ~ characters.
138906c3fb27SDimitry Andric for (const auto &LR : LineRanges) {
139006c3fb27SDimitry Andric if (LR.LineNo == LineNo)
139106c3fb27SDimitry Andric highlightRange(LR, sourceColMap, CaretLine);
139206c3fb27SDimitry Andric }
13930b57cec5SDimitry Andric
13940b57cec5SDimitry Andric // Next, insert the caret itself.
13950b57cec5SDimitry Andric if (CaretLineNo == LineNo) {
139606c3fb27SDimitry Andric size_t Col = sourceColMap.byteToContainingColumn(CaretColNo - 1);
139706c3fb27SDimitry Andric CaretLine.resize(std::max(Col + 1, CaretLine.size()), ' ');
139806c3fb27SDimitry Andric CaretLine[Col] = '^';
13990b57cec5SDimitry Andric }
14000b57cec5SDimitry Andric
14010b57cec5SDimitry Andric std::string FixItInsertionLine = buildFixItInsertionLine(
14020b57cec5SDimitry Andric FID, LineNo, sourceColMap, Hints, SM, DiagOpts.get());
14030b57cec5SDimitry Andric
14040b57cec5SDimitry Andric // If the source line is too long for our terminal, select only the
14050b57cec5SDimitry Andric // "interesting" source region within that line.
14060b57cec5SDimitry Andric unsigned Columns = DiagOpts->MessageLength;
14070b57cec5SDimitry Andric if (Columns)
14080b57cec5SDimitry Andric selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
14090b57cec5SDimitry Andric Columns, sourceColMap);
14100b57cec5SDimitry Andric
14110b57cec5SDimitry Andric // If we are in -fdiagnostics-print-source-range-info mode, we are trying
14120b57cec5SDimitry Andric // to produce easily machine parsable output. Add a space before the
14130b57cec5SDimitry Andric // source line and the caret to make it trivial to tell the main diagnostic
14140b57cec5SDimitry Andric // line from what the user is intended to see.
141506c3fb27SDimitry Andric if (DiagOpts->ShowSourceRanges && !SourceLine.empty()) {
14160b57cec5SDimitry Andric SourceLine = ' ' + SourceLine;
14170b57cec5SDimitry Andric CaretLine = ' ' + CaretLine;
14180b57cec5SDimitry Andric }
14190b57cec5SDimitry Andric
14200b57cec5SDimitry Andric // Emit what we have computed.
1421*0fca6ea1SDimitry Andric emitSnippet(SourceLine, MaxLineNoDisplayWidth, LineNo, DisplayLineNo,
1422*0fca6ea1SDimitry Andric SourceStyles[LineNo - Lines.first]);
14230b57cec5SDimitry Andric
14240b57cec5SDimitry Andric if (!CaretLine.empty()) {
142506c3fb27SDimitry Andric indentForLineNumbers();
14260b57cec5SDimitry Andric if (DiagOpts->ShowColors)
14270b57cec5SDimitry Andric OS.changeColor(caretColor, true);
14280b57cec5SDimitry Andric OS << CaretLine << '\n';
14290b57cec5SDimitry Andric if (DiagOpts->ShowColors)
14300b57cec5SDimitry Andric OS.resetColor();
14310b57cec5SDimitry Andric }
14320b57cec5SDimitry Andric
14330b57cec5SDimitry Andric if (!FixItInsertionLine.empty()) {
143406c3fb27SDimitry Andric indentForLineNumbers();
14350b57cec5SDimitry Andric if (DiagOpts->ShowColors)
14360b57cec5SDimitry Andric // Print fixit line in color
14370b57cec5SDimitry Andric OS.changeColor(fixitColor, false);
14380b57cec5SDimitry Andric if (DiagOpts->ShowSourceRanges)
14390b57cec5SDimitry Andric OS << ' ';
14400b57cec5SDimitry Andric OS << FixItInsertionLine << '\n';
14410b57cec5SDimitry Andric if (DiagOpts->ShowColors)
14420b57cec5SDimitry Andric OS.resetColor();
14430b57cec5SDimitry Andric }
14440b57cec5SDimitry Andric }
14450b57cec5SDimitry Andric
14460b57cec5SDimitry Andric // Print out any parseable fixit information requested by the options.
14470b57cec5SDimitry Andric emitParseableFixits(Hints, SM);
14480b57cec5SDimitry Andric }
14490b57cec5SDimitry Andric
emitSnippet(StringRef SourceLine,unsigned MaxLineNoDisplayWidth,unsigned LineNo,unsigned DisplayLineNo,ArrayRef<StyleRange> Styles)145006c3fb27SDimitry Andric void TextDiagnostic::emitSnippet(StringRef SourceLine,
145106c3fb27SDimitry Andric unsigned MaxLineNoDisplayWidth,
1452*0fca6ea1SDimitry Andric unsigned LineNo, unsigned DisplayLineNo,
1453*0fca6ea1SDimitry Andric ArrayRef<StyleRange> Styles) {
145406c3fb27SDimitry Andric // Emit line number.
145506c3fb27SDimitry Andric if (MaxLineNoDisplayWidth > 0) {
1456*0fca6ea1SDimitry Andric unsigned LineNoDisplayWidth = getNumDisplayWidth(DisplayLineNo);
145706c3fb27SDimitry Andric OS.indent(MaxLineNoDisplayWidth - LineNoDisplayWidth + 1)
1458*0fca6ea1SDimitry Andric << DisplayLineNo << " | ";
145906c3fb27SDimitry Andric }
14600b57cec5SDimitry Andric
146106c3fb27SDimitry Andric // Print the source line one character at a time.
146206c3fb27SDimitry Andric bool PrintReversed = false;
1463*0fca6ea1SDimitry Andric std::optional<llvm::raw_ostream::Colors> CurrentColor;
146406c3fb27SDimitry Andric size_t I = 0;
146506c3fb27SDimitry Andric while (I < SourceLine.size()) {
146606c3fb27SDimitry Andric auto [Str, WasPrintable] =
146706c3fb27SDimitry Andric printableTextForNextCharacter(SourceLine, &I, DiagOpts->TabStop);
14680b57cec5SDimitry Andric
146906c3fb27SDimitry Andric // Toggle inverted colors on or off for this character.
147006c3fb27SDimitry Andric if (DiagOpts->ShowColors) {
147106c3fb27SDimitry Andric if (WasPrintable == PrintReversed) {
147206c3fb27SDimitry Andric PrintReversed = !PrintReversed;
147306c3fb27SDimitry Andric if (PrintReversed)
14740b57cec5SDimitry Andric OS.reverseColor();
1475*0fca6ea1SDimitry Andric else {
14760b57cec5SDimitry Andric OS.resetColor();
1477*0fca6ea1SDimitry Andric CurrentColor = std::nullopt;
14780b57cec5SDimitry Andric }
147906c3fb27SDimitry Andric }
1480*0fca6ea1SDimitry Andric
1481*0fca6ea1SDimitry Andric // Apply syntax highlighting information.
1482*0fca6ea1SDimitry Andric const auto *CharStyle = llvm::find_if(Styles, [I](const StyleRange &R) {
1483*0fca6ea1SDimitry Andric return (R.Start < I && R.End >= I);
1484*0fca6ea1SDimitry Andric });
1485*0fca6ea1SDimitry Andric
1486*0fca6ea1SDimitry Andric if (CharStyle != Styles.end()) {
1487*0fca6ea1SDimitry Andric if (!CurrentColor ||
1488*0fca6ea1SDimitry Andric (CurrentColor && *CurrentColor != CharStyle->Color)) {
1489*0fca6ea1SDimitry Andric OS.changeColor(CharStyle->Color, false);
1490*0fca6ea1SDimitry Andric CurrentColor = CharStyle->Color;
1491*0fca6ea1SDimitry Andric }
1492*0fca6ea1SDimitry Andric } else if (CurrentColor) {
1493*0fca6ea1SDimitry Andric OS.resetColor();
1494*0fca6ea1SDimitry Andric CurrentColor = std::nullopt;
1495*0fca6ea1SDimitry Andric }
1496*0fca6ea1SDimitry Andric }
1497*0fca6ea1SDimitry Andric
149806c3fb27SDimitry Andric OS << Str;
14990b57cec5SDimitry Andric }
15000b57cec5SDimitry Andric
150106c3fb27SDimitry Andric if (DiagOpts->ShowColors)
15020b57cec5SDimitry Andric OS.resetColor();
15030b57cec5SDimitry Andric
15040b57cec5SDimitry Andric OS << '\n';
15050b57cec5SDimitry Andric }
15060b57cec5SDimitry Andric
emitParseableFixits(ArrayRef<FixItHint> Hints,const SourceManager & SM)15070b57cec5SDimitry Andric void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints,
15080b57cec5SDimitry Andric const SourceManager &SM) {
15090b57cec5SDimitry Andric if (!DiagOpts->ShowParseableFixits)
15100b57cec5SDimitry Andric return;
15110b57cec5SDimitry Andric
15120b57cec5SDimitry Andric // We follow FixItRewriter's example in not (yet) handling
15130b57cec5SDimitry Andric // fix-its in macros.
151406c3fb27SDimitry Andric for (const auto &H : Hints) {
151506c3fb27SDimitry Andric if (H.RemoveRange.isInvalid() || H.RemoveRange.getBegin().isMacroID() ||
151606c3fb27SDimitry Andric H.RemoveRange.getEnd().isMacroID())
15170b57cec5SDimitry Andric return;
15180b57cec5SDimitry Andric }
15190b57cec5SDimitry Andric
152006c3fb27SDimitry Andric for (const auto &H : Hints) {
152106c3fb27SDimitry Andric SourceLocation BLoc = H.RemoveRange.getBegin();
152206c3fb27SDimitry Andric SourceLocation ELoc = H.RemoveRange.getEnd();
15230b57cec5SDimitry Andric
15240b57cec5SDimitry Andric std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
15250b57cec5SDimitry Andric std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
15260b57cec5SDimitry Andric
15270b57cec5SDimitry Andric // Adjust for token ranges.
152806c3fb27SDimitry Andric if (H.RemoveRange.isTokenRange())
15290b57cec5SDimitry Andric EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts);
15300b57cec5SDimitry Andric
15310b57cec5SDimitry Andric // We specifically do not do word-wrapping or tab-expansion here,
15320b57cec5SDimitry Andric // because this is supposed to be easy to parse.
15330b57cec5SDimitry Andric PresumedLoc PLoc = SM.getPresumedLoc(BLoc);
15340b57cec5SDimitry Andric if (PLoc.isInvalid())
15350b57cec5SDimitry Andric break;
15360b57cec5SDimitry Andric
15370b57cec5SDimitry Andric OS << "fix-it:\"";
15380b57cec5SDimitry Andric OS.write_escaped(PLoc.getFilename());
15390b57cec5SDimitry Andric OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second)
15400b57cec5SDimitry Andric << ':' << SM.getColumnNumber(BInfo.first, BInfo.second)
15410b57cec5SDimitry Andric << '-' << SM.getLineNumber(EInfo.first, EInfo.second)
15420b57cec5SDimitry Andric << ':' << SM.getColumnNumber(EInfo.first, EInfo.second)
15430b57cec5SDimitry Andric << "}:\"";
154406c3fb27SDimitry Andric OS.write_escaped(H.CodeToInsert);
15450b57cec5SDimitry Andric OS << "\"\n";
15460b57cec5SDimitry Andric }
15470b57cec5SDimitry Andric }
1548