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" 150b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 160b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 170b57cec5SDimitry Andric #include "llvm/Support/ConvertUTF.h" 180b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 190b57cec5SDimitry Andric #include "llvm/Support/Locale.h" 200b57cec5SDimitry Andric #include "llvm/Support/Path.h" 210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 220b57cec5SDimitry Andric #include <algorithm> 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric using namespace clang; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric static const enum raw_ostream::Colors noteColor = 270b57cec5SDimitry Andric raw_ostream::BLACK; 280b57cec5SDimitry Andric static const enum raw_ostream::Colors remarkColor = 290b57cec5SDimitry Andric raw_ostream::BLUE; 300b57cec5SDimitry Andric static const enum raw_ostream::Colors fixitColor = 310b57cec5SDimitry Andric raw_ostream::GREEN; 320b57cec5SDimitry Andric static const enum raw_ostream::Colors caretColor = 330b57cec5SDimitry Andric raw_ostream::GREEN; 340b57cec5SDimitry Andric static const enum raw_ostream::Colors warningColor = 350b57cec5SDimitry Andric raw_ostream::MAGENTA; 360b57cec5SDimitry Andric static const enum raw_ostream::Colors templateColor = 370b57cec5SDimitry Andric raw_ostream::CYAN; 380b57cec5SDimitry Andric static const enum raw_ostream::Colors errorColor = raw_ostream::RED; 390b57cec5SDimitry Andric static const enum raw_ostream::Colors fatalColor = raw_ostream::RED; 400b57cec5SDimitry Andric // Used for changing only the bold attribute. 410b57cec5SDimitry Andric static const enum raw_ostream::Colors savedColor = 420b57cec5SDimitry Andric raw_ostream::SAVEDCOLOR; 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric /// Add highlights to differences in template strings. 450b57cec5SDimitry Andric static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str, 460b57cec5SDimitry Andric bool &Normal, bool Bold) { 4704eeddc0SDimitry Andric while (true) { 480b57cec5SDimitry Andric size_t Pos = Str.find(ToggleHighlight); 490b57cec5SDimitry Andric OS << Str.slice(0, Pos); 500b57cec5SDimitry Andric if (Pos == StringRef::npos) 510b57cec5SDimitry Andric break; 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric Str = Str.substr(Pos + 1); 540b57cec5SDimitry Andric if (Normal) 550b57cec5SDimitry Andric OS.changeColor(templateColor, true); 560b57cec5SDimitry Andric else { 570b57cec5SDimitry Andric OS.resetColor(); 580b57cec5SDimitry Andric if (Bold) 590b57cec5SDimitry Andric OS.changeColor(savedColor, true); 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric Normal = !Normal; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric /// Number of spaces to indent when word-wrapping. 660b57cec5SDimitry Andric const unsigned WordWrapIndentation = 6; 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) { 690b57cec5SDimitry Andric int bytes = 0; 700b57cec5SDimitry Andric while (0<i) { 710b57cec5SDimitry Andric if (SourceLine[--i]=='\t') 720b57cec5SDimitry Andric break; 730b57cec5SDimitry Andric ++bytes; 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric return bytes; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric /// returns a printable representation of first item from input range 790b57cec5SDimitry Andric /// 800b57cec5SDimitry Andric /// This function returns a printable representation of the next item in a line 810b57cec5SDimitry Andric /// of source. If the next byte begins a valid and printable character, that 820b57cec5SDimitry Andric /// character is returned along with 'true'. 830b57cec5SDimitry Andric /// 840b57cec5SDimitry Andric /// Otherwise, if the next byte begins a valid, but unprintable character, a 850b57cec5SDimitry Andric /// printable, escaped representation of the character is returned, along with 860b57cec5SDimitry Andric /// 'false'. Otherwise a printable, escaped representation of the next byte 870b57cec5SDimitry Andric /// is returned along with 'false'. 880b57cec5SDimitry Andric /// 890b57cec5SDimitry Andric /// \note The index is updated to be used with a subsequent call to 900b57cec5SDimitry Andric /// printableTextForNextCharacter. 910b57cec5SDimitry Andric /// 920b57cec5SDimitry Andric /// \param SourceLine The line of source 930b57cec5SDimitry Andric /// \param i Pointer to byte index, 940b57cec5SDimitry Andric /// \param TabStop used to expand tabs 950b57cec5SDimitry Andric /// \return pair(printable text, 'true' iff original text was printable) 960b57cec5SDimitry Andric /// 970b57cec5SDimitry Andric static std::pair<SmallString<16>, bool> 980b57cec5SDimitry Andric printableTextForNextCharacter(StringRef SourceLine, size_t *i, 990b57cec5SDimitry Andric unsigned TabStop) { 1000b57cec5SDimitry Andric assert(i && "i must not be null"); 1010b57cec5SDimitry Andric assert(*i<SourceLine.size() && "must point to a valid index"); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric if (SourceLine[*i]=='\t') { 1040b57cec5SDimitry Andric assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop && 1050b57cec5SDimitry Andric "Invalid -ftabstop value"); 1060b57cec5SDimitry Andric unsigned col = bytesSincePreviousTabOrLineBegin(SourceLine, *i); 1070b57cec5SDimitry Andric unsigned NumSpaces = TabStop - col%TabStop; 1080b57cec5SDimitry Andric assert(0 < NumSpaces && NumSpaces <= TabStop 1090b57cec5SDimitry Andric && "Invalid computation of space amt"); 1100b57cec5SDimitry Andric ++(*i); 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric SmallString<16> expandedTab; 1130b57cec5SDimitry Andric expandedTab.assign(NumSpaces, ' '); 1140b57cec5SDimitry Andric return std::make_pair(expandedTab, true); 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric unsigned char const *begin, *end; 1180b57cec5SDimitry Andric begin = reinterpret_cast<unsigned char const *>(&*(SourceLine.begin() + *i)); 1190b57cec5SDimitry Andric end = begin + (SourceLine.size() - *i); 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric if (llvm::isLegalUTF8Sequence(begin, end)) { 1220b57cec5SDimitry Andric llvm::UTF32 c; 1230b57cec5SDimitry Andric llvm::UTF32 *cptr = &c; 1240b57cec5SDimitry Andric unsigned char const *original_begin = begin; 1250b57cec5SDimitry Andric unsigned char const *cp_end = 1260b57cec5SDimitry Andric begin + llvm::getNumBytesForUTF8(SourceLine[*i]); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric llvm::ConversionResult res = llvm::ConvertUTF8toUTF32( 1290b57cec5SDimitry Andric &begin, cp_end, &cptr, cptr + 1, llvm::strictConversion); 1300b57cec5SDimitry Andric (void)res; 1310b57cec5SDimitry Andric assert(llvm::conversionOK == res); 1320b57cec5SDimitry Andric assert(0 < begin-original_begin 1330b57cec5SDimitry Andric && "we must be further along in the string now"); 1340b57cec5SDimitry Andric *i += begin-original_begin; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric if (!llvm::sys::locale::isPrint(c)) { 1370b57cec5SDimitry Andric // If next character is valid UTF-8, but not printable 1380b57cec5SDimitry Andric SmallString<16> expandedCP("<U+>"); 1390b57cec5SDimitry Andric while (c) { 1400b57cec5SDimitry Andric expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(c%16)); 1410b57cec5SDimitry Andric c/=16; 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric while (expandedCP.size() < 8) 1440b57cec5SDimitry Andric expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(0)); 1450b57cec5SDimitry Andric return std::make_pair(expandedCP, false); 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric // If next character is valid UTF-8, and printable 1490b57cec5SDimitry Andric return std::make_pair(SmallString<16>(original_begin, cp_end), true); 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric // If next byte is not valid UTF-8 (and therefore not printable) 1540b57cec5SDimitry Andric SmallString<16> expandedByte("<XX>"); 1550b57cec5SDimitry Andric unsigned char byte = SourceLine[*i]; 1560b57cec5SDimitry Andric expandedByte[1] = llvm::hexdigit(byte / 16); 1570b57cec5SDimitry Andric expandedByte[2] = llvm::hexdigit(byte % 16); 1580b57cec5SDimitry Andric ++(*i); 1590b57cec5SDimitry Andric return std::make_pair(expandedByte, false); 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric static void expandTabs(std::string &SourceLine, unsigned TabStop) { 1630b57cec5SDimitry Andric size_t i = SourceLine.size(); 1640b57cec5SDimitry Andric while (i>0) { 1650b57cec5SDimitry Andric i--; 1660b57cec5SDimitry Andric if (SourceLine[i]!='\t') 1670b57cec5SDimitry Andric continue; 1680b57cec5SDimitry Andric size_t tmp_i = i; 1690b57cec5SDimitry Andric std::pair<SmallString<16>,bool> res 1700b57cec5SDimitry Andric = printableTextForNextCharacter(SourceLine, &tmp_i, TabStop); 1710b57cec5SDimitry Andric SourceLine.replace(i, 1, res.first.c_str()); 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric /// This function takes a raw source line and produces a mapping from the bytes 1760b57cec5SDimitry Andric /// of the printable representation of the line to the columns those printable 1770b57cec5SDimitry Andric /// characters will appear at (numbering the first column as 0). 1780b57cec5SDimitry Andric /// 1790b57cec5SDimitry Andric /// If a byte 'i' corresponds to multiple columns (e.g. the byte contains a tab 1800b57cec5SDimitry Andric /// character) then the array will map that byte to the first column the 1810b57cec5SDimitry Andric /// tab appears at and the next value in the map will have been incremented 1820b57cec5SDimitry Andric /// more than once. 1830b57cec5SDimitry Andric /// 1840b57cec5SDimitry Andric /// If a byte is the first in a sequence of bytes that together map to a single 1850b57cec5SDimitry Andric /// entity in the output, then the array will map that byte to the appropriate 1860b57cec5SDimitry Andric /// column while the subsequent bytes will be -1. 1870b57cec5SDimitry Andric /// 1880b57cec5SDimitry Andric /// The last element in the array does not correspond to any byte in the input 1890b57cec5SDimitry Andric /// and instead is the number of columns needed to display the source 1900b57cec5SDimitry Andric /// 1910b57cec5SDimitry Andric /// example: (given a tabstop of 8) 1920b57cec5SDimitry Andric /// 1930b57cec5SDimitry Andric /// "a \t \u3042" -> {0,1,2,8,9,-1,-1,11} 1940b57cec5SDimitry Andric /// 1950b57cec5SDimitry Andric /// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to 1960b57cec5SDimitry Andric /// display) 1970b57cec5SDimitry Andric static void byteToColumn(StringRef SourceLine, unsigned TabStop, 1980b57cec5SDimitry Andric SmallVectorImpl<int> &out) { 1990b57cec5SDimitry Andric out.clear(); 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric if (SourceLine.empty()) { 2020b57cec5SDimitry Andric out.resize(1u,0); 2030b57cec5SDimitry Andric return; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric out.resize(SourceLine.size()+1, -1); 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric int columns = 0; 2090b57cec5SDimitry Andric size_t i = 0; 2100b57cec5SDimitry Andric while (i<SourceLine.size()) { 2110b57cec5SDimitry Andric out[i] = columns; 2120b57cec5SDimitry Andric std::pair<SmallString<16>,bool> res 2130b57cec5SDimitry Andric = printableTextForNextCharacter(SourceLine, &i, TabStop); 2140b57cec5SDimitry Andric columns += llvm::sys::locale::columnWidth(res.first); 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric out.back() = columns; 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric /// This function takes a raw source line and produces a mapping from columns 2200b57cec5SDimitry Andric /// to the byte of the source line that produced the character displaying at 2210b57cec5SDimitry Andric /// that column. This is the inverse of the mapping produced by byteToColumn() 2220b57cec5SDimitry Andric /// 2230b57cec5SDimitry Andric /// The last element in the array is the number of bytes in the source string 2240b57cec5SDimitry Andric /// 2250b57cec5SDimitry Andric /// example: (given a tabstop of 8) 2260b57cec5SDimitry Andric /// 2270b57cec5SDimitry Andric /// "a \t \u3042" -> {0,1,2,-1,-1,-1,-1,-1,3,4,-1,7} 2280b57cec5SDimitry Andric /// 2290b57cec5SDimitry Andric /// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to 2300b57cec5SDimitry Andric /// display) 2310b57cec5SDimitry Andric static void columnToByte(StringRef SourceLine, unsigned TabStop, 2320b57cec5SDimitry Andric SmallVectorImpl<int> &out) { 2330b57cec5SDimitry Andric out.clear(); 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric if (SourceLine.empty()) { 2360b57cec5SDimitry Andric out.resize(1u, 0); 2370b57cec5SDimitry Andric return; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric int columns = 0; 2410b57cec5SDimitry Andric size_t i = 0; 2420b57cec5SDimitry Andric while (i<SourceLine.size()) { 2430b57cec5SDimitry Andric out.resize(columns+1, -1); 2440b57cec5SDimitry Andric out.back() = i; 2450b57cec5SDimitry Andric std::pair<SmallString<16>,bool> res 2460b57cec5SDimitry Andric = printableTextForNextCharacter(SourceLine, &i, TabStop); 2470b57cec5SDimitry Andric columns += llvm::sys::locale::columnWidth(res.first); 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric out.resize(columns+1, -1); 2500b57cec5SDimitry Andric out.back() = i; 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric namespace { 2540b57cec5SDimitry Andric struct SourceColumnMap { 2550b57cec5SDimitry Andric SourceColumnMap(StringRef SourceLine, unsigned TabStop) 2560b57cec5SDimitry Andric : m_SourceLine(SourceLine) { 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric ::byteToColumn(SourceLine, TabStop, m_byteToColumn); 2590b57cec5SDimitry Andric ::columnToByte(SourceLine, TabStop, m_columnToByte); 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric assert(m_byteToColumn.size()==SourceLine.size()+1); 2620b57cec5SDimitry Andric assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size()); 2630b57cec5SDimitry Andric assert(m_byteToColumn.size() 2640b57cec5SDimitry Andric == static_cast<unsigned>(m_columnToByte.back()+1)); 2650b57cec5SDimitry Andric assert(static_cast<unsigned>(m_byteToColumn.back()+1) 2660b57cec5SDimitry Andric == m_columnToByte.size()); 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric int columns() const { return m_byteToColumn.back(); } 2690b57cec5SDimitry Andric int bytes() const { return m_columnToByte.back(); } 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric /// Map a byte to the column which it is at the start of, or return -1 2720b57cec5SDimitry Andric /// if it is not at the start of a column (for a UTF-8 trailing byte). 2730b57cec5SDimitry Andric int byteToColumn(int n) const { 2740b57cec5SDimitry Andric assert(0<=n && n<static_cast<int>(m_byteToColumn.size())); 2750b57cec5SDimitry Andric return m_byteToColumn[n]; 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric /// Map a byte to the first column which contains it. 2790b57cec5SDimitry Andric int byteToContainingColumn(int N) const { 2800b57cec5SDimitry Andric assert(0 <= N && N < static_cast<int>(m_byteToColumn.size())); 2810b57cec5SDimitry Andric while (m_byteToColumn[N] == -1) 2820b57cec5SDimitry Andric --N; 2830b57cec5SDimitry Andric return m_byteToColumn[N]; 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric /// Map a column to the byte which starts the column, or return -1 if 2870b57cec5SDimitry Andric /// the column the second or subsequent column of an expanded tab or similar 2880b57cec5SDimitry Andric /// multi-column entity. 2890b57cec5SDimitry Andric int columnToByte(int n) const { 2900b57cec5SDimitry Andric assert(0<=n && n<static_cast<int>(m_columnToByte.size())); 2910b57cec5SDimitry Andric return m_columnToByte[n]; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric /// Map from a byte index to the next byte which starts a column. 2950b57cec5SDimitry Andric int startOfNextColumn(int N) const { 2960b57cec5SDimitry Andric assert(0 <= N && N < static_cast<int>(m_byteToColumn.size() - 1)); 2970b57cec5SDimitry Andric while (byteToColumn(++N) == -1) {} 2980b57cec5SDimitry Andric return N; 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric /// Map from a byte index to the previous byte which starts a column. 3020b57cec5SDimitry Andric int startOfPreviousColumn(int N) const { 3030b57cec5SDimitry Andric assert(0 < N && N < static_cast<int>(m_byteToColumn.size())); 3040b57cec5SDimitry Andric while (byteToColumn(--N) == -1) {} 3050b57cec5SDimitry Andric return N; 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric StringRef getSourceLine() const { 3090b57cec5SDimitry Andric return m_SourceLine; 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric private: 3130b57cec5SDimitry Andric const std::string m_SourceLine; 3140b57cec5SDimitry Andric SmallVector<int,200> m_byteToColumn; 3150b57cec5SDimitry Andric SmallVector<int,200> m_columnToByte; 3160b57cec5SDimitry Andric }; 3170b57cec5SDimitry Andric } // end anonymous namespace 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric /// When the source code line we want to print is too long for 3200b57cec5SDimitry Andric /// the terminal, select the "interesting" region. 3210b57cec5SDimitry Andric static void selectInterestingSourceRegion(std::string &SourceLine, 3220b57cec5SDimitry Andric std::string &CaretLine, 3230b57cec5SDimitry Andric std::string &FixItInsertionLine, 3240b57cec5SDimitry Andric unsigned Columns, 3250b57cec5SDimitry Andric const SourceColumnMap &map) { 3260b57cec5SDimitry Andric unsigned CaretColumns = CaretLine.size(); 3270b57cec5SDimitry Andric unsigned FixItColumns = llvm::sys::locale::columnWidth(FixItInsertionLine); 3280b57cec5SDimitry Andric unsigned MaxColumns = std::max(static_cast<unsigned>(map.columns()), 3290b57cec5SDimitry Andric std::max(CaretColumns, FixItColumns)); 3300b57cec5SDimitry Andric // if the number of columns is less than the desired number we're done 3310b57cec5SDimitry Andric if (MaxColumns <= Columns) 3320b57cec5SDimitry Andric return; 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric // No special characters are allowed in CaretLine. 3350b57cec5SDimitry Andric assert(CaretLine.end() == 3360b57cec5SDimitry Andric llvm::find_if(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 4740b57cec5SDimitry Andric // output lines to show only this slice, with two-space padding 4750b57cec5SDimitry Andric // before the lines so that it looks nicer. 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric assert(CaretStart!=(unsigned)-1 && CaretEnd!=(unsigned)-1 && 4780b57cec5SDimitry Andric SourceStart!=(unsigned)-1 && SourceEnd!=(unsigned)-1); 4790b57cec5SDimitry Andric assert(SourceStart <= SourceEnd); 4800b57cec5SDimitry Andric assert(CaretStart <= CaretEnd); 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric unsigned BackColumnsRemoved 4830b57cec5SDimitry Andric = map.byteToColumn(SourceLine.size())-map.byteToColumn(SourceEnd); 4840b57cec5SDimitry Andric unsigned FrontColumnsRemoved = CaretStart; 4850b57cec5SDimitry Andric unsigned ColumnsKept = CaretEnd-CaretStart; 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric // We checked up front that the line needed truncation 4880b57cec5SDimitry Andric assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns); 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric // The line needs some truncation, and we'd prefer to keep the front 4910b57cec5SDimitry Andric // if possible, so remove the back 4920b57cec5SDimitry Andric if (BackColumnsRemoved > strlen(back_ellipse)) 4930b57cec5SDimitry Andric SourceLine.replace(SourceEnd, std::string::npos, back_ellipse); 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric // If that's enough then we're done 4960b57cec5SDimitry Andric if (FrontColumnsRemoved+ColumnsKept <= Columns) 4970b57cec5SDimitry Andric return; 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // Otherwise remove the front as well 5000b57cec5SDimitry Andric if (FrontColumnsRemoved > strlen(front_ellipse)) { 5010b57cec5SDimitry Andric SourceLine.replace(0, SourceStart, front_ellipse); 5020b57cec5SDimitry Andric CaretLine.replace(0, CaretStart, front_space); 5030b57cec5SDimitry Andric if (!FixItInsertionLine.empty()) 5040b57cec5SDimitry Andric FixItInsertionLine.replace(0, CaretStart, front_space); 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric /// Skip over whitespace in the string, starting at the given 5090b57cec5SDimitry Andric /// index. 5100b57cec5SDimitry Andric /// 5110b57cec5SDimitry Andric /// \returns The index of the first non-whitespace character that is 5120b57cec5SDimitry Andric /// greater than or equal to Idx or, if no such character exists, 5130b57cec5SDimitry Andric /// returns the end of the string. 5140b57cec5SDimitry Andric static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) { 5150b57cec5SDimitry Andric while (Idx < Length && isWhitespace(Str[Idx])) 5160b57cec5SDimitry Andric ++Idx; 5170b57cec5SDimitry Andric return Idx; 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric /// If the given character is the start of some kind of 5210b57cec5SDimitry Andric /// balanced punctuation (e.g., quotes or parentheses), return the 5220b57cec5SDimitry Andric /// character that will terminate the punctuation. 5230b57cec5SDimitry Andric /// 5240b57cec5SDimitry Andric /// \returns The ending punctuation character, if any, or the NULL 5250b57cec5SDimitry Andric /// character if the input character does not start any punctuation. 5260b57cec5SDimitry Andric static inline char findMatchingPunctuation(char c) { 5270b57cec5SDimitry Andric switch (c) { 5280b57cec5SDimitry Andric case '\'': return '\''; 5290b57cec5SDimitry Andric case '`': return '\''; 5300b57cec5SDimitry Andric case '"': return '"'; 5310b57cec5SDimitry Andric case '(': return ')'; 5320b57cec5SDimitry Andric case '[': return ']'; 5330b57cec5SDimitry Andric case '{': return '}'; 5340b57cec5SDimitry Andric default: break; 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric return 0; 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric /// Find the end of the word starting at the given offset 5410b57cec5SDimitry Andric /// within a string. 5420b57cec5SDimitry Andric /// 5430b57cec5SDimitry Andric /// \returns the index pointing one character past the end of the 5440b57cec5SDimitry Andric /// word. 5450b57cec5SDimitry Andric static unsigned findEndOfWord(unsigned Start, StringRef Str, 5460b57cec5SDimitry Andric unsigned Length, unsigned Column, 5470b57cec5SDimitry Andric unsigned Columns) { 5480b57cec5SDimitry Andric assert(Start < Str.size() && "Invalid start position!"); 5490b57cec5SDimitry Andric unsigned End = Start + 1; 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric // If we are already at the end of the string, take that as the word. 5520b57cec5SDimitry Andric if (End == Str.size()) 5530b57cec5SDimitry Andric return End; 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric // Determine if the start of the string is actually opening 5560b57cec5SDimitry Andric // punctuation, e.g., a quote or parentheses. 5570b57cec5SDimitry Andric char EndPunct = findMatchingPunctuation(Str[Start]); 5580b57cec5SDimitry Andric if (!EndPunct) { 5590b57cec5SDimitry Andric // This is a normal word. Just find the first space character. 5600b57cec5SDimitry Andric while (End < Length && !isWhitespace(Str[End])) 5610b57cec5SDimitry Andric ++End; 5620b57cec5SDimitry Andric return End; 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric // We have the start of a balanced punctuation sequence (quotes, 5660b57cec5SDimitry Andric // parentheses, etc.). Determine the full sequence is. 5670b57cec5SDimitry Andric SmallString<16> PunctuationEndStack; 5680b57cec5SDimitry Andric PunctuationEndStack.push_back(EndPunct); 5690b57cec5SDimitry Andric while (End < Length && !PunctuationEndStack.empty()) { 5700b57cec5SDimitry Andric if (Str[End] == PunctuationEndStack.back()) 5710b57cec5SDimitry Andric PunctuationEndStack.pop_back(); 5720b57cec5SDimitry Andric else if (char SubEndPunct = findMatchingPunctuation(Str[End])) 5730b57cec5SDimitry Andric PunctuationEndStack.push_back(SubEndPunct); 5740b57cec5SDimitry Andric 5750b57cec5SDimitry Andric ++End; 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric // Find the first space character after the punctuation ended. 5790b57cec5SDimitry Andric while (End < Length && !isWhitespace(Str[End])) 5800b57cec5SDimitry Andric ++End; 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric unsigned PunctWordLength = End - Start; 5830b57cec5SDimitry Andric if (// If the word fits on this line 5840b57cec5SDimitry Andric Column + PunctWordLength <= Columns || 5850b57cec5SDimitry Andric // ... or the word is "short enough" to take up the next line 5860b57cec5SDimitry Andric // without too much ugly white space 5870b57cec5SDimitry Andric PunctWordLength < Columns/3) 5880b57cec5SDimitry Andric return End; // Take the whole thing as a single "word". 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric // The whole quoted/parenthesized string is too long to print as a 5910b57cec5SDimitry Andric // single "word". Instead, find the "word" that starts just after 5920b57cec5SDimitry Andric // the punctuation and use that end-point instead. This will recurse 5930b57cec5SDimitry Andric // until it finds something small enough to consider a word. 5940b57cec5SDimitry Andric return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns); 5950b57cec5SDimitry Andric } 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric /// Print the given string to a stream, word-wrapping it to 5980b57cec5SDimitry Andric /// some number of columns in the process. 5990b57cec5SDimitry Andric /// 6000b57cec5SDimitry Andric /// \param OS the stream to which the word-wrapping string will be 6010b57cec5SDimitry Andric /// emitted. 6020b57cec5SDimitry Andric /// \param Str the string to word-wrap and output. 6030b57cec5SDimitry Andric /// \param Columns the number of columns to word-wrap to. 6040b57cec5SDimitry Andric /// \param Column the column number at which the first character of \p 6050b57cec5SDimitry Andric /// Str will be printed. This will be non-zero when part of the first 6060b57cec5SDimitry Andric /// line has already been printed. 6070b57cec5SDimitry Andric /// \param Bold if the current text should be bold 6080b57cec5SDimitry Andric /// \param Indentation the number of spaces to indent any lines beyond 6090b57cec5SDimitry Andric /// the first line. 6100b57cec5SDimitry Andric /// \returns true if word-wrapping was required, or false if the 6110b57cec5SDimitry Andric /// string fit on the first line. 6120b57cec5SDimitry Andric static bool printWordWrapped(raw_ostream &OS, StringRef Str, 6130b57cec5SDimitry Andric unsigned Columns, 6140b57cec5SDimitry Andric unsigned Column = 0, 6150b57cec5SDimitry Andric bool Bold = false, 6160b57cec5SDimitry Andric unsigned Indentation = WordWrapIndentation) { 6170b57cec5SDimitry Andric const unsigned Length = std::min(Str.find('\n'), Str.size()); 6180b57cec5SDimitry Andric bool TextNormal = true; 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric // The string used to indent each line. 6210b57cec5SDimitry Andric SmallString<16> IndentStr; 6220b57cec5SDimitry Andric IndentStr.assign(Indentation, ' '); 6230b57cec5SDimitry Andric bool Wrapped = false; 6240b57cec5SDimitry Andric for (unsigned WordStart = 0, WordEnd; WordStart < Length; 6250b57cec5SDimitry Andric WordStart = WordEnd) { 6260b57cec5SDimitry Andric // Find the beginning of the next word. 6270b57cec5SDimitry Andric WordStart = skipWhitespace(WordStart, Str, Length); 6280b57cec5SDimitry Andric if (WordStart == Length) 6290b57cec5SDimitry Andric break; 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric // Find the end of this word. 6320b57cec5SDimitry Andric WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns); 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric // Does this word fit on the current line? 6350b57cec5SDimitry Andric unsigned WordLength = WordEnd - WordStart; 6360b57cec5SDimitry Andric if (Column + WordLength < Columns) { 6370b57cec5SDimitry Andric // This word fits on the current line; print it there. 6380b57cec5SDimitry Andric if (WordStart) { 6390b57cec5SDimitry Andric OS << ' '; 6400b57cec5SDimitry Andric Column += 1; 6410b57cec5SDimitry Andric } 6420b57cec5SDimitry Andric applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), 6430b57cec5SDimitry Andric TextNormal, Bold); 6440b57cec5SDimitry Andric Column += WordLength; 6450b57cec5SDimitry Andric continue; 6460b57cec5SDimitry Andric } 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric // This word does not fit on the current line, so wrap to the next 6490b57cec5SDimitry Andric // line. 6500b57cec5SDimitry Andric OS << '\n'; 6510b57cec5SDimitry Andric OS.write(&IndentStr[0], Indentation); 6520b57cec5SDimitry Andric applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), 6530b57cec5SDimitry Andric TextNormal, Bold); 6540b57cec5SDimitry Andric Column = Indentation + WordLength; 6550b57cec5SDimitry Andric Wrapped = true; 6560b57cec5SDimitry Andric } 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric // Append any remaning text from the message with its existing formatting. 6590b57cec5SDimitry Andric applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold); 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric assert(TextNormal && "Text highlighted at end of diagnostic message."); 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric return Wrapped; 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric TextDiagnostic::TextDiagnostic(raw_ostream &OS, 6670b57cec5SDimitry Andric const LangOptions &LangOpts, 6680b57cec5SDimitry Andric DiagnosticOptions *DiagOpts) 6690b57cec5SDimitry Andric : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS) {} 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric TextDiagnostic::~TextDiagnostic() {} 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric void TextDiagnostic::emitDiagnosticMessage( 6740b57cec5SDimitry Andric FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, 6750b57cec5SDimitry Andric StringRef Message, ArrayRef<clang::CharSourceRange> Ranges, 6760b57cec5SDimitry Andric DiagOrStoredDiag D) { 6770b57cec5SDimitry Andric uint64_t StartOfLocationInfo = OS.tell(); 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andric // Emit the location of this particular diagnostic. 6800b57cec5SDimitry Andric if (Loc.isValid()) 6810b57cec5SDimitry Andric emitDiagnosticLoc(Loc, PLoc, Level, Ranges); 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric if (DiagOpts->ShowColors) 6840b57cec5SDimitry Andric OS.resetColor(); 6850b57cec5SDimitry Andric 686a7dea167SDimitry Andric if (DiagOpts->ShowLevel) 687fe6060f1SDimitry Andric printDiagnosticLevel(OS, Level, DiagOpts->ShowColors); 6880b57cec5SDimitry Andric printDiagnosticMessage(OS, 6890b57cec5SDimitry Andric /*IsSupplemental*/ Level == DiagnosticsEngine::Note, 6900b57cec5SDimitry Andric Message, OS.tell() - StartOfLocationInfo, 6910b57cec5SDimitry Andric DiagOpts->MessageLength, DiagOpts->ShowColors); 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric /*static*/ void 6950b57cec5SDimitry Andric TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, 6960b57cec5SDimitry Andric DiagnosticsEngine::Level Level, 697fe6060f1SDimitry Andric bool ShowColors) { 6980b57cec5SDimitry Andric if (ShowColors) { 6990b57cec5SDimitry Andric // Print diagnostic category in bold and color 7000b57cec5SDimitry Andric switch (Level) { 7010b57cec5SDimitry Andric case DiagnosticsEngine::Ignored: 7020b57cec5SDimitry Andric llvm_unreachable("Invalid diagnostic type"); 7030b57cec5SDimitry Andric case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break; 7040b57cec5SDimitry Andric case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break; 7050b57cec5SDimitry Andric case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break; 7060b57cec5SDimitry Andric case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break; 7070b57cec5SDimitry Andric case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break; 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric switch (Level) { 7120b57cec5SDimitry Andric case DiagnosticsEngine::Ignored: 7130b57cec5SDimitry Andric llvm_unreachable("Invalid diagnostic type"); 714fe6060f1SDimitry Andric case DiagnosticsEngine::Note: OS << "note: "; break; 715fe6060f1SDimitry Andric case DiagnosticsEngine::Remark: OS << "remark: "; break; 716fe6060f1SDimitry Andric case DiagnosticsEngine::Warning: OS << "warning: "; break; 717fe6060f1SDimitry Andric case DiagnosticsEngine::Error: OS << "error: "; break; 718fe6060f1SDimitry Andric case DiagnosticsEngine::Fatal: OS << "fatal error: "; break; 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric if (ShowColors) 7220b57cec5SDimitry Andric OS.resetColor(); 7230b57cec5SDimitry Andric } 7240b57cec5SDimitry Andric 7250b57cec5SDimitry Andric /*static*/ 7260b57cec5SDimitry Andric void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS, 7270b57cec5SDimitry Andric bool IsSupplemental, 7280b57cec5SDimitry Andric StringRef Message, 7290b57cec5SDimitry Andric unsigned CurrentColumn, 7300b57cec5SDimitry Andric unsigned Columns, bool ShowColors) { 7310b57cec5SDimitry Andric bool Bold = false; 7320b57cec5SDimitry Andric if (ShowColors && !IsSupplemental) { 7330b57cec5SDimitry Andric // Print primary diagnostic messages in bold and without color, to visually 7340b57cec5SDimitry Andric // indicate the transition from continuation notes and other output. 7350b57cec5SDimitry Andric OS.changeColor(savedColor, true); 7360b57cec5SDimitry Andric Bold = true; 7370b57cec5SDimitry Andric } 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andric if (Columns) 7400b57cec5SDimitry Andric printWordWrapped(OS, Message, Columns, CurrentColumn, Bold); 7410b57cec5SDimitry Andric else { 7420b57cec5SDimitry Andric bool Normal = true; 7430b57cec5SDimitry Andric applyTemplateHighlighting(OS, Message, Normal, Bold); 7440b57cec5SDimitry Andric assert(Normal && "Formatting should have returned to normal"); 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric if (ShowColors) 7480b57cec5SDimitry Andric OS.resetColor(); 7490b57cec5SDimitry Andric OS << '\n'; 7500b57cec5SDimitry Andric } 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) { 753480093f4SDimitry Andric #ifdef _WIN32 754480093f4SDimitry Andric SmallString<4096> TmpFilename; 755480093f4SDimitry Andric #endif 7560b57cec5SDimitry Andric if (DiagOpts->AbsolutePath) { 757480093f4SDimitry Andric auto File = SM.getFileManager().getFile(Filename); 758480093f4SDimitry Andric if (File) { 7590b57cec5SDimitry Andric // We want to print a simplified absolute path, i. e. without "dots". 7600b57cec5SDimitry Andric // 7610b57cec5SDimitry Andric // The hardest part here are the paths like "<part1>/<link>/../<part2>". 7620b57cec5SDimitry Andric // On Unix-like systems, we cannot just collapse "<link>/..", because 7630b57cec5SDimitry Andric // paths are resolved sequentially, and, thereby, the path 7640b57cec5SDimitry Andric // "<part1>/<part2>" may point to a different location. That is why 7650b57cec5SDimitry Andric // we use FileManager::getCanonicalName(), which expands all indirections 7660b57cec5SDimitry Andric // with llvm::sys::fs::real_path() and caches the result. 7670b57cec5SDimitry Andric // 7680b57cec5SDimitry Andric // On the other hand, it would be better to preserve as much of the 7690b57cec5SDimitry Andric // original path as possible, because that helps a user to recognize it. 7700b57cec5SDimitry Andric // real_path() expands all links, which sometimes too much. Luckily, 7710b57cec5SDimitry Andric // on Windows we can just use llvm::sys::path::remove_dots(), because, 7720b57cec5SDimitry Andric // on that system, both aforementioned paths point to the same place. 7730b57cec5SDimitry Andric #ifdef _WIN32 774480093f4SDimitry Andric TmpFilename = (*File)->getName(); 775480093f4SDimitry Andric llvm::sys::fs::make_absolute(TmpFilename); 776480093f4SDimitry Andric llvm::sys::path::native(TmpFilename); 777480093f4SDimitry Andric llvm::sys::path::remove_dots(TmpFilename, /* remove_dot_dot */ true); 778480093f4SDimitry Andric Filename = StringRef(TmpFilename.data(), TmpFilename.size()); 7790b57cec5SDimitry Andric #else 780480093f4SDimitry Andric Filename = SM.getFileManager().getCanonicalName(*File); 7810b57cec5SDimitry Andric #endif 7820b57cec5SDimitry Andric } 7830b57cec5SDimitry Andric } 7840b57cec5SDimitry Andric 7850b57cec5SDimitry Andric OS << Filename; 7860b57cec5SDimitry Andric } 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric /// Print out the file/line/column information and include trace. 7890b57cec5SDimitry Andric /// 7900b57cec5SDimitry Andric /// This method handlen the emission of the diagnostic location information. 7910b57cec5SDimitry Andric /// This includes extracting as much location information as is present for 7920b57cec5SDimitry Andric /// the diagnostic and printing it, as well as any include stack or source 7930b57cec5SDimitry Andric /// ranges necessary. 7940b57cec5SDimitry Andric void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, 7950b57cec5SDimitry Andric DiagnosticsEngine::Level Level, 7960b57cec5SDimitry Andric ArrayRef<CharSourceRange> Ranges) { 7970b57cec5SDimitry Andric if (PLoc.isInvalid()) { 7980b57cec5SDimitry Andric // At least print the file name if available: 7990b57cec5SDimitry Andric FileID FID = Loc.getFileID(); 8000b57cec5SDimitry Andric if (FID.isValid()) { 80181ad6265SDimitry Andric if (const FileEntry *FE = Loc.getFileEntry()) { 8020b57cec5SDimitry Andric emitFilename(FE->getName(), Loc.getManager()); 8030b57cec5SDimitry Andric OS << ": "; 8040b57cec5SDimitry Andric } 8050b57cec5SDimitry Andric } 8060b57cec5SDimitry Andric return; 8070b57cec5SDimitry Andric } 8080b57cec5SDimitry Andric unsigned LineNo = PLoc.getLine(); 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric if (!DiagOpts->ShowLocation) 8110b57cec5SDimitry Andric return; 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric if (DiagOpts->ShowColors) 8140b57cec5SDimitry Andric OS.changeColor(savedColor, true); 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andric emitFilename(PLoc.getFilename(), Loc.getManager()); 8170b57cec5SDimitry Andric switch (DiagOpts->getFormat()) { 818*fcaf7f86SDimitry Andric case DiagnosticOptions::SARIF: 819e8d8bef9SDimitry Andric case DiagnosticOptions::Clang: 820e8d8bef9SDimitry Andric if (DiagOpts->ShowLine) 821e8d8bef9SDimitry Andric OS << ':' << LineNo; 822e8d8bef9SDimitry Andric break; 8230b57cec5SDimitry Andric case DiagnosticOptions::MSVC: OS << '(' << LineNo; break; 8240b57cec5SDimitry Andric case DiagnosticOptions::Vi: OS << " +" << LineNo; break; 8250b57cec5SDimitry Andric } 8260b57cec5SDimitry Andric 8270b57cec5SDimitry Andric if (DiagOpts->ShowColumn) 8280b57cec5SDimitry Andric // Compute the column number. 8290b57cec5SDimitry Andric if (unsigned ColNo = PLoc.getColumn()) { 8300b57cec5SDimitry Andric if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) { 8310b57cec5SDimitry Andric OS << ','; 8320b57cec5SDimitry Andric // Visual Studio 2010 or earlier expects column number to be off by one 8330b57cec5SDimitry Andric if (LangOpts.MSCompatibilityVersion && 8340b57cec5SDimitry Andric !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012)) 8350b57cec5SDimitry Andric ColNo--; 8360b57cec5SDimitry Andric } else 8370b57cec5SDimitry Andric OS << ':'; 8380b57cec5SDimitry Andric OS << ColNo; 8390b57cec5SDimitry Andric } 8400b57cec5SDimitry Andric switch (DiagOpts->getFormat()) { 841*fcaf7f86SDimitry Andric case DiagnosticOptions::SARIF: 8420b57cec5SDimitry Andric case DiagnosticOptions::Clang: 8430b57cec5SDimitry Andric case DiagnosticOptions::Vi: OS << ':'; break; 8440b57cec5SDimitry Andric case DiagnosticOptions::MSVC: 8450b57cec5SDimitry Andric // MSVC2013 and before print 'file(4) : error'. MSVC2015 gets rid of the 8460b57cec5SDimitry Andric // space and prints 'file(4): error'. 8470b57cec5SDimitry Andric OS << ')'; 8480b57cec5SDimitry Andric if (LangOpts.MSCompatibilityVersion && 8490b57cec5SDimitry Andric !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)) 8500b57cec5SDimitry Andric OS << ' '; 8510b57cec5SDimitry Andric OS << ':'; 8520b57cec5SDimitry Andric break; 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric if (DiagOpts->ShowSourceRanges && !Ranges.empty()) { 8560b57cec5SDimitry Andric FileID CaretFileID = Loc.getExpansionLoc().getFileID(); 8570b57cec5SDimitry Andric bool PrintedRange = false; 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(), 8600b57cec5SDimitry Andric RE = Ranges.end(); 8610b57cec5SDimitry Andric RI != RE; ++RI) { 8620b57cec5SDimitry Andric // Ignore invalid ranges. 8630b57cec5SDimitry Andric if (!RI->isValid()) continue; 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric auto &SM = Loc.getManager(); 8660b57cec5SDimitry Andric SourceLocation B = SM.getExpansionLoc(RI->getBegin()); 8670b57cec5SDimitry Andric CharSourceRange ERange = SM.getExpansionRange(RI->getEnd()); 8680b57cec5SDimitry Andric SourceLocation E = ERange.getEnd(); 8690b57cec5SDimitry Andric bool IsTokenRange = ERange.isTokenRange(); 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B); 8720b57cec5SDimitry Andric std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E); 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric // If the start or end of the range is in another file, just discard 8750b57cec5SDimitry Andric // it. 8760b57cec5SDimitry Andric if (BInfo.first != CaretFileID || EInfo.first != CaretFileID) 8770b57cec5SDimitry Andric continue; 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric // Add in the length of the token, so that we cover multi-char 8800b57cec5SDimitry Andric // tokens. 8810b57cec5SDimitry Andric unsigned TokSize = 0; 8820b57cec5SDimitry Andric if (IsTokenRange) 8830b57cec5SDimitry Andric TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts); 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric FullSourceLoc BF(B, SM), EF(E, SM); 8860b57cec5SDimitry Andric OS << '{' 8870b57cec5SDimitry Andric << BF.getLineNumber() << ':' << BF.getColumnNumber() << '-' 8880b57cec5SDimitry Andric << EF.getLineNumber() << ':' << (EF.getColumnNumber() + TokSize) 8890b57cec5SDimitry Andric << '}'; 8900b57cec5SDimitry Andric PrintedRange = true; 8910b57cec5SDimitry Andric } 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric if (PrintedRange) 8940b57cec5SDimitry Andric OS << ':'; 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric OS << ' '; 8970b57cec5SDimitry Andric } 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric void TextDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) { 9000b57cec5SDimitry Andric if (DiagOpts->ShowLocation && PLoc.isValid()) 9010b57cec5SDimitry Andric OS << "In file included from " << PLoc.getFilename() << ':' 9020b57cec5SDimitry Andric << PLoc.getLine() << ":\n"; 9030b57cec5SDimitry Andric else 9040b57cec5SDimitry Andric OS << "In included file:\n"; 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric 9070b57cec5SDimitry Andric void TextDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, 9080b57cec5SDimitry Andric StringRef ModuleName) { 9090b57cec5SDimitry Andric if (DiagOpts->ShowLocation && PLoc.isValid()) 9100b57cec5SDimitry Andric OS << "In module '" << ModuleName << "' imported from " 9110b57cec5SDimitry Andric << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; 9120b57cec5SDimitry Andric else 9130b57cec5SDimitry Andric OS << "In module '" << ModuleName << "':\n"; 9140b57cec5SDimitry Andric } 9150b57cec5SDimitry Andric 9160b57cec5SDimitry Andric void TextDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc, 9170b57cec5SDimitry Andric PresumedLoc PLoc, 9180b57cec5SDimitry Andric StringRef ModuleName) { 9190b57cec5SDimitry Andric if (DiagOpts->ShowLocation && PLoc.isValid()) 9200b57cec5SDimitry Andric OS << "While building module '" << ModuleName << "' imported from " 9210b57cec5SDimitry Andric << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; 9220b57cec5SDimitry Andric else 9230b57cec5SDimitry Andric OS << "While building module '" << ModuleName << "':\n"; 9240b57cec5SDimitry Andric } 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric /// Find the suitable set of lines to show to include a set of ranges. 9270b57cec5SDimitry Andric static llvm::Optional<std::pair<unsigned, unsigned>> 9280b57cec5SDimitry Andric findLinesForRange(const CharSourceRange &R, FileID FID, 9290b57cec5SDimitry Andric const SourceManager &SM) { 9300b57cec5SDimitry Andric if (!R.isValid()) return None; 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric SourceLocation Begin = R.getBegin(); 9330b57cec5SDimitry Andric SourceLocation End = R.getEnd(); 9340b57cec5SDimitry Andric if (SM.getFileID(Begin) != FID || SM.getFileID(End) != FID) 9350b57cec5SDimitry Andric return None; 9360b57cec5SDimitry Andric 9370b57cec5SDimitry Andric return std::make_pair(SM.getExpansionLineNumber(Begin), 9380b57cec5SDimitry Andric SM.getExpansionLineNumber(End)); 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric /// Add as much of range B into range A as possible without exceeding a maximum 9420b57cec5SDimitry Andric /// size of MaxRange. Ranges are inclusive. 9430b57cec5SDimitry Andric static std::pair<unsigned, unsigned> 9440b57cec5SDimitry Andric maybeAddRange(std::pair<unsigned, unsigned> A, std::pair<unsigned, unsigned> B, 9450b57cec5SDimitry Andric unsigned MaxRange) { 9460b57cec5SDimitry Andric // If A is already the maximum size, we're done. 9470b57cec5SDimitry Andric unsigned Slack = MaxRange - (A.second - A.first + 1); 9480b57cec5SDimitry Andric if (Slack == 0) 9490b57cec5SDimitry Andric return A; 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric // Easy case: merge succeeds within MaxRange. 9520b57cec5SDimitry Andric unsigned Min = std::min(A.first, B.first); 9530b57cec5SDimitry Andric unsigned Max = std::max(A.second, B.second); 9540b57cec5SDimitry Andric if (Max - Min + 1 <= MaxRange) 9550b57cec5SDimitry Andric return {Min, Max}; 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric // If we can't reach B from A within MaxRange, there's nothing to do. 9580b57cec5SDimitry Andric // Don't add lines to the range that contain nothing interesting. 9590b57cec5SDimitry Andric if ((B.first > A.first && B.first - A.first + 1 > MaxRange) || 9600b57cec5SDimitry Andric (B.second < A.second && A.second - B.second + 1 > MaxRange)) 9610b57cec5SDimitry Andric return A; 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric // Otherwise, expand A towards B to produce a range of size MaxRange. We 9640b57cec5SDimitry Andric // attempt to expand by the same amount in both directions if B strictly 9650b57cec5SDimitry Andric // contains A. 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric // Expand downwards by up to half the available amount, then upwards as 9680b57cec5SDimitry Andric // much as possible, then downwards as much as possible. 9690b57cec5SDimitry Andric A.second = std::min(A.second + (Slack + 1) / 2, Max); 9700b57cec5SDimitry Andric Slack = MaxRange - (A.second - A.first + 1); 9710b57cec5SDimitry Andric A.first = std::max(Min + Slack, A.first) - Slack; 9720b57cec5SDimitry Andric A.second = std::min(A.first + MaxRange - 1, Max); 9730b57cec5SDimitry Andric return A; 9740b57cec5SDimitry Andric } 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric /// Highlight a SourceRange (with ~'s) for any characters on LineNo. 9770b57cec5SDimitry Andric static void highlightRange(const CharSourceRange &R, 9780b57cec5SDimitry Andric unsigned LineNo, FileID FID, 9790b57cec5SDimitry Andric const SourceColumnMap &map, 9800b57cec5SDimitry Andric std::string &CaretLine, 9810b57cec5SDimitry Andric const SourceManager &SM, 9820b57cec5SDimitry Andric const LangOptions &LangOpts) { 9830b57cec5SDimitry Andric if (!R.isValid()) return; 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric SourceLocation Begin = R.getBegin(); 9860b57cec5SDimitry Andric SourceLocation End = R.getEnd(); 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric unsigned StartLineNo = SM.getExpansionLineNumber(Begin); 9890b57cec5SDimitry Andric if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) 9900b57cec5SDimitry Andric return; // No intersection. 9910b57cec5SDimitry Andric 9920b57cec5SDimitry Andric unsigned EndLineNo = SM.getExpansionLineNumber(End); 9930b57cec5SDimitry Andric if (EndLineNo < LineNo || SM.getFileID(End) != FID) 9940b57cec5SDimitry Andric return; // No intersection. 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric // Compute the column number of the start. 9970b57cec5SDimitry Andric unsigned StartColNo = 0; 9980b57cec5SDimitry Andric if (StartLineNo == LineNo) { 9990b57cec5SDimitry Andric StartColNo = SM.getExpansionColumnNumber(Begin); 10000b57cec5SDimitry Andric if (StartColNo) --StartColNo; // Zero base the col #. 10010b57cec5SDimitry Andric } 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric // Compute the column number of the end. 10040b57cec5SDimitry Andric unsigned EndColNo = map.getSourceLine().size(); 10050b57cec5SDimitry Andric if (EndLineNo == LineNo) { 10060b57cec5SDimitry Andric EndColNo = SM.getExpansionColumnNumber(End); 10070b57cec5SDimitry Andric if (EndColNo) { 10080b57cec5SDimitry Andric --EndColNo; // Zero base the col #. 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric // Add in the length of the token, so that we cover multi-char tokens if 10110b57cec5SDimitry Andric // this is a token range. 10120b57cec5SDimitry Andric if (R.isTokenRange()) 10130b57cec5SDimitry Andric EndColNo += Lexer::MeasureTokenLength(End, SM, LangOpts); 10140b57cec5SDimitry Andric } else { 10150b57cec5SDimitry Andric EndColNo = CaretLine.size(); 10160b57cec5SDimitry Andric } 10170b57cec5SDimitry Andric } 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric assert(StartColNo <= EndColNo && "Invalid range!"); 10200b57cec5SDimitry Andric 10210b57cec5SDimitry Andric // Check that a token range does not highlight only whitespace. 10220b57cec5SDimitry Andric if (R.isTokenRange()) { 10230b57cec5SDimitry Andric // Pick the first non-whitespace column. 10240b57cec5SDimitry Andric while (StartColNo < map.getSourceLine().size() && 10250b57cec5SDimitry Andric (map.getSourceLine()[StartColNo] == ' ' || 10260b57cec5SDimitry Andric map.getSourceLine()[StartColNo] == '\t')) 10270b57cec5SDimitry Andric StartColNo = map.startOfNextColumn(StartColNo); 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric // Pick the last non-whitespace column. 10300b57cec5SDimitry Andric if (EndColNo > map.getSourceLine().size()) 10310b57cec5SDimitry Andric EndColNo = map.getSourceLine().size(); 10320b57cec5SDimitry Andric while (EndColNo && 10330b57cec5SDimitry Andric (map.getSourceLine()[EndColNo-1] == ' ' || 10340b57cec5SDimitry Andric map.getSourceLine()[EndColNo-1] == '\t')) 10350b57cec5SDimitry Andric EndColNo = map.startOfPreviousColumn(EndColNo); 10360b57cec5SDimitry Andric 10370b57cec5SDimitry Andric // If the start/end passed each other, then we are trying to highlight a 10380b57cec5SDimitry Andric // range that just exists in whitespace. That most likely means we have 10390b57cec5SDimitry Andric // a multi-line highlighting range that covers a blank line. 10400b57cec5SDimitry Andric if (StartColNo > EndColNo) { 10410b57cec5SDimitry Andric assert(StartLineNo != EndLineNo && "trying to highlight whitespace"); 10420b57cec5SDimitry Andric StartColNo = EndColNo; 10430b57cec5SDimitry Andric } 10440b57cec5SDimitry Andric } 10450b57cec5SDimitry Andric 10460b57cec5SDimitry Andric assert(StartColNo <= map.getSourceLine().size() && "Invalid range!"); 10470b57cec5SDimitry Andric assert(EndColNo <= map.getSourceLine().size() && "Invalid range!"); 10480b57cec5SDimitry Andric 10490b57cec5SDimitry Andric // Fill the range with ~'s. 10500b57cec5SDimitry Andric StartColNo = map.byteToContainingColumn(StartColNo); 10510b57cec5SDimitry Andric EndColNo = map.byteToContainingColumn(EndColNo); 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andric assert(StartColNo <= EndColNo && "Invalid range!"); 10540b57cec5SDimitry Andric if (CaretLine.size() < EndColNo) 10550b57cec5SDimitry Andric CaretLine.resize(EndColNo,' '); 10560b57cec5SDimitry Andric std::fill(CaretLine.begin()+StartColNo,CaretLine.begin()+EndColNo,'~'); 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric static std::string buildFixItInsertionLine(FileID FID, 10600b57cec5SDimitry Andric unsigned LineNo, 10610b57cec5SDimitry Andric const SourceColumnMap &map, 10620b57cec5SDimitry Andric ArrayRef<FixItHint> Hints, 10630b57cec5SDimitry Andric const SourceManager &SM, 10640b57cec5SDimitry Andric const DiagnosticOptions *DiagOpts) { 10650b57cec5SDimitry Andric std::string FixItInsertionLine; 10660b57cec5SDimitry Andric if (Hints.empty() || !DiagOpts->ShowFixits) 10670b57cec5SDimitry Andric return FixItInsertionLine; 10680b57cec5SDimitry Andric unsigned PrevHintEndCol = 0; 10690b57cec5SDimitry Andric 10700b57cec5SDimitry Andric for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); 10710b57cec5SDimitry Andric I != E; ++I) { 10720b57cec5SDimitry Andric if (!I->CodeToInsert.empty()) { 10730b57cec5SDimitry Andric // We have an insertion hint. Determine whether the inserted 10740b57cec5SDimitry Andric // code contains no newlines and is on the same line as the caret. 10750b57cec5SDimitry Andric std::pair<FileID, unsigned> HintLocInfo 10760b57cec5SDimitry Andric = SM.getDecomposedExpansionLoc(I->RemoveRange.getBegin()); 10770b57cec5SDimitry Andric if (FID == HintLocInfo.first && 10780b57cec5SDimitry Andric LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) && 10790b57cec5SDimitry Andric StringRef(I->CodeToInsert).find_first_of("\n\r") == StringRef::npos) { 10800b57cec5SDimitry Andric // Insert the new code into the line just below the code 10810b57cec5SDimitry Andric // that the user wrote. 10820b57cec5SDimitry Andric // Note: When modifying this function, be very careful about what is a 10830b57cec5SDimitry Andric // "column" (printed width, platform-dependent) and what is a 10840b57cec5SDimitry Andric // "byte offset" (SourceManager "column"). 10850b57cec5SDimitry Andric unsigned HintByteOffset 10860b57cec5SDimitry Andric = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1; 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric // The hint must start inside the source or right at the end 10890b57cec5SDimitry Andric assert(HintByteOffset < static_cast<unsigned>(map.bytes())+1); 10900b57cec5SDimitry Andric unsigned HintCol = map.byteToContainingColumn(HintByteOffset); 10910b57cec5SDimitry Andric 10920b57cec5SDimitry Andric // If we inserted a long previous hint, push this one forwards, and add 10930b57cec5SDimitry Andric // an extra space to show that this is not part of the previous 10940b57cec5SDimitry Andric // completion. This is sort of the best we can do when two hints appear 10950b57cec5SDimitry Andric // to overlap. 10960b57cec5SDimitry Andric // 10970b57cec5SDimitry Andric // Note that if this hint is located immediately after the previous 10980b57cec5SDimitry Andric // hint, no space will be added, since the location is more important. 10990b57cec5SDimitry Andric if (HintCol < PrevHintEndCol) 11000b57cec5SDimitry Andric HintCol = PrevHintEndCol + 1; 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andric // This should NOT use HintByteOffset, because the source might have 11030b57cec5SDimitry Andric // Unicode characters in earlier columns. 11040b57cec5SDimitry Andric unsigned NewFixItLineSize = FixItInsertionLine.size() + 11050b57cec5SDimitry Andric (HintCol - PrevHintEndCol) + I->CodeToInsert.size(); 11060b57cec5SDimitry Andric if (NewFixItLineSize > FixItInsertionLine.size()) 11070b57cec5SDimitry Andric FixItInsertionLine.resize(NewFixItLineSize, ' '); 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric std::copy(I->CodeToInsert.begin(), I->CodeToInsert.end(), 11100b57cec5SDimitry Andric FixItInsertionLine.end() - I->CodeToInsert.size()); 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric PrevHintEndCol = 11130b57cec5SDimitry Andric HintCol + llvm::sys::locale::columnWidth(I->CodeToInsert); 11140b57cec5SDimitry Andric } 11150b57cec5SDimitry Andric } 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric expandTabs(FixItInsertionLine, DiagOpts->TabStop); 11190b57cec5SDimitry Andric 11200b57cec5SDimitry Andric return FixItInsertionLine; 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric /// Emit a code snippet and caret line. 11240b57cec5SDimitry Andric /// 11250b57cec5SDimitry Andric /// This routine emits a single line's code snippet and caret line.. 11260b57cec5SDimitry Andric /// 11270b57cec5SDimitry Andric /// \param Loc The location for the caret. 11280b57cec5SDimitry Andric /// \param Ranges The underlined ranges for this code snippet. 11290b57cec5SDimitry Andric /// \param Hints The FixIt hints active for this diagnostic. 11300b57cec5SDimitry Andric void TextDiagnostic::emitSnippetAndCaret( 11310b57cec5SDimitry Andric FullSourceLoc Loc, DiagnosticsEngine::Level Level, 11320b57cec5SDimitry Andric SmallVectorImpl<CharSourceRange> &Ranges, ArrayRef<FixItHint> Hints) { 11330b57cec5SDimitry Andric assert(Loc.isValid() && "must have a valid source location here"); 11340b57cec5SDimitry Andric assert(Loc.isFileID() && "must have a file location here"); 11350b57cec5SDimitry Andric 11360b57cec5SDimitry Andric // If caret diagnostics are enabled and we have location, we want to 11370b57cec5SDimitry Andric // emit the caret. However, we only do this if the location moved 11380b57cec5SDimitry Andric // from the last diagnostic, if the last diagnostic was a note that 11390b57cec5SDimitry Andric // was part of a different warning or error diagnostic, or if the 11400b57cec5SDimitry Andric // diagnostic has ranges. We don't want to emit the same caret 11410b57cec5SDimitry Andric // multiple times if one loc has multiple diagnostics. 11420b57cec5SDimitry Andric if (!DiagOpts->ShowCarets) 11430b57cec5SDimitry Andric return; 11440b57cec5SDimitry Andric if (Loc == LastLoc && Ranges.empty() && Hints.empty() && 11450b57cec5SDimitry Andric (LastLevel != DiagnosticsEngine::Note || Level == LastLevel)) 11460b57cec5SDimitry Andric return; 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric // Decompose the location into a FID/Offset pair. 11490b57cec5SDimitry Andric std::pair<FileID, unsigned> LocInfo = Loc.getDecomposedLoc(); 11500b57cec5SDimitry Andric FileID FID = LocInfo.first; 11510b57cec5SDimitry Andric const SourceManager &SM = Loc.getManager(); 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric // Get information about the buffer it points into. 11540b57cec5SDimitry Andric bool Invalid = false; 11550b57cec5SDimitry Andric StringRef BufData = Loc.getBufferData(&Invalid); 11560b57cec5SDimitry Andric if (Invalid) 11570b57cec5SDimitry Andric return; 11580b57cec5SDimitry Andric 11590b57cec5SDimitry Andric unsigned CaretLineNo = Loc.getLineNumber(); 11600b57cec5SDimitry Andric unsigned CaretColNo = Loc.getColumnNumber(); 11610b57cec5SDimitry Andric 11620b57cec5SDimitry Andric // Arbitrarily stop showing snippets when the line is too long. 11630b57cec5SDimitry Andric static const size_t MaxLineLengthToPrint = 4096; 11640b57cec5SDimitry Andric if (CaretColNo > MaxLineLengthToPrint) 11650b57cec5SDimitry Andric return; 11660b57cec5SDimitry Andric 11670b57cec5SDimitry Andric // Find the set of lines to include. 11680b57cec5SDimitry Andric const unsigned MaxLines = DiagOpts->SnippetLineLimit; 11690b57cec5SDimitry Andric std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo}; 11700b57cec5SDimitry Andric for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(), 11710b57cec5SDimitry Andric E = Ranges.end(); 11720b57cec5SDimitry Andric I != E; ++I) 11730b57cec5SDimitry Andric if (auto OptionalRange = findLinesForRange(*I, FID, SM)) 11740b57cec5SDimitry Andric Lines = maybeAddRange(Lines, *OptionalRange, MaxLines); 11750b57cec5SDimitry Andric 11760b57cec5SDimitry Andric for (unsigned LineNo = Lines.first; LineNo != Lines.second + 1; ++LineNo) { 11770b57cec5SDimitry Andric const char *BufStart = BufData.data(); 11780b57cec5SDimitry Andric const char *BufEnd = BufStart + BufData.size(); 11790b57cec5SDimitry Andric 11800b57cec5SDimitry Andric // Rewind from the current position to the start of the line. 11810b57cec5SDimitry Andric const char *LineStart = 11820b57cec5SDimitry Andric BufStart + 11830b57cec5SDimitry Andric SM.getDecomposedLoc(SM.translateLineCol(FID, LineNo, 1)).second; 11840b57cec5SDimitry Andric if (LineStart == BufEnd) 11850b57cec5SDimitry Andric break; 11860b57cec5SDimitry Andric 11870b57cec5SDimitry Andric // Compute the line end. 11880b57cec5SDimitry Andric const char *LineEnd = LineStart; 11890b57cec5SDimitry Andric while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd) 11900b57cec5SDimitry Andric ++LineEnd; 11910b57cec5SDimitry Andric 11920b57cec5SDimitry Andric // Arbitrarily stop showing snippets when the line is too long. 11930b57cec5SDimitry Andric // FIXME: Don't print any lines in this case. 11940b57cec5SDimitry Andric if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint) 11950b57cec5SDimitry Andric return; 11960b57cec5SDimitry Andric 11970b57cec5SDimitry Andric // Trim trailing null-bytes. 11980b57cec5SDimitry Andric StringRef Line(LineStart, LineEnd - LineStart); 11990b57cec5SDimitry Andric while (!Line.empty() && Line.back() == '\0' && 12000b57cec5SDimitry Andric (LineNo != CaretLineNo || Line.size() > CaretColNo)) 12010b57cec5SDimitry Andric Line = Line.drop_back(); 12020b57cec5SDimitry Andric 12030b57cec5SDimitry Andric // Copy the line of code into an std::string for ease of manipulation. 12040b57cec5SDimitry Andric std::string SourceLine(Line.begin(), Line.end()); 12050b57cec5SDimitry Andric 12060b57cec5SDimitry Andric // Build the byte to column map. 12070b57cec5SDimitry Andric const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop); 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric // Create a line for the caret that is filled with spaces that is the same 12100b57cec5SDimitry Andric // number of columns as the line of source code. 12110b57cec5SDimitry Andric std::string CaretLine(sourceColMap.columns(), ' '); 12120b57cec5SDimitry Andric 12130b57cec5SDimitry Andric // Highlight all of the characters covered by Ranges with ~ characters. 12140b57cec5SDimitry Andric for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(), 12150b57cec5SDimitry Andric E = Ranges.end(); 12160b57cec5SDimitry Andric I != E; ++I) 12170b57cec5SDimitry Andric highlightRange(*I, LineNo, FID, sourceColMap, CaretLine, SM, LangOpts); 12180b57cec5SDimitry Andric 12190b57cec5SDimitry Andric // Next, insert the caret itself. 12200b57cec5SDimitry Andric if (CaretLineNo == LineNo) { 12210b57cec5SDimitry Andric CaretColNo = sourceColMap.byteToContainingColumn(CaretColNo - 1); 12220b57cec5SDimitry Andric if (CaretLine.size() < CaretColNo + 1) 12230b57cec5SDimitry Andric CaretLine.resize(CaretColNo + 1, ' '); 12240b57cec5SDimitry Andric CaretLine[CaretColNo] = '^'; 12250b57cec5SDimitry Andric } 12260b57cec5SDimitry Andric 12270b57cec5SDimitry Andric std::string FixItInsertionLine = buildFixItInsertionLine( 12280b57cec5SDimitry Andric FID, LineNo, sourceColMap, Hints, SM, DiagOpts.get()); 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric // If the source line is too long for our terminal, select only the 12310b57cec5SDimitry Andric // "interesting" source region within that line. 12320b57cec5SDimitry Andric unsigned Columns = DiagOpts->MessageLength; 12330b57cec5SDimitry Andric if (Columns) 12340b57cec5SDimitry Andric selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, 12350b57cec5SDimitry Andric Columns, sourceColMap); 12360b57cec5SDimitry Andric 12370b57cec5SDimitry Andric // If we are in -fdiagnostics-print-source-range-info mode, we are trying 12380b57cec5SDimitry Andric // to produce easily machine parsable output. Add a space before the 12390b57cec5SDimitry Andric // source line and the caret to make it trivial to tell the main diagnostic 12400b57cec5SDimitry Andric // line from what the user is intended to see. 12410b57cec5SDimitry Andric if (DiagOpts->ShowSourceRanges) { 12420b57cec5SDimitry Andric SourceLine = ' ' + SourceLine; 12430b57cec5SDimitry Andric CaretLine = ' ' + CaretLine; 12440b57cec5SDimitry Andric } 12450b57cec5SDimitry Andric 12460b57cec5SDimitry Andric // Finally, remove any blank spaces from the end of CaretLine. 12470b57cec5SDimitry Andric while (!CaretLine.empty() && CaretLine[CaretLine.size() - 1] == ' ') 12480b57cec5SDimitry Andric CaretLine.erase(CaretLine.end() - 1); 12490b57cec5SDimitry Andric 12500b57cec5SDimitry Andric // Emit what we have computed. 12510b57cec5SDimitry Andric emitSnippet(SourceLine); 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric if (!CaretLine.empty()) { 12540b57cec5SDimitry Andric if (DiagOpts->ShowColors) 12550b57cec5SDimitry Andric OS.changeColor(caretColor, true); 12560b57cec5SDimitry Andric OS << CaretLine << '\n'; 12570b57cec5SDimitry Andric if (DiagOpts->ShowColors) 12580b57cec5SDimitry Andric OS.resetColor(); 12590b57cec5SDimitry Andric } 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andric if (!FixItInsertionLine.empty()) { 12620b57cec5SDimitry Andric if (DiagOpts->ShowColors) 12630b57cec5SDimitry Andric // Print fixit line in color 12640b57cec5SDimitry Andric OS.changeColor(fixitColor, false); 12650b57cec5SDimitry Andric if (DiagOpts->ShowSourceRanges) 12660b57cec5SDimitry Andric OS << ' '; 12670b57cec5SDimitry Andric OS << FixItInsertionLine << '\n'; 12680b57cec5SDimitry Andric if (DiagOpts->ShowColors) 12690b57cec5SDimitry Andric OS.resetColor(); 12700b57cec5SDimitry Andric } 12710b57cec5SDimitry Andric } 12720b57cec5SDimitry Andric 12730b57cec5SDimitry Andric // Print out any parseable fixit information requested by the options. 12740b57cec5SDimitry Andric emitParseableFixits(Hints, SM); 12750b57cec5SDimitry Andric } 12760b57cec5SDimitry Andric 12770b57cec5SDimitry Andric void TextDiagnostic::emitSnippet(StringRef line) { 12780b57cec5SDimitry Andric if (line.empty()) 12790b57cec5SDimitry Andric return; 12800b57cec5SDimitry Andric 12810b57cec5SDimitry Andric size_t i = 0; 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric std::string to_print; 12840b57cec5SDimitry Andric bool print_reversed = false; 12850b57cec5SDimitry Andric 12860b57cec5SDimitry Andric while (i<line.size()) { 12870b57cec5SDimitry Andric std::pair<SmallString<16>,bool> res 12880b57cec5SDimitry Andric = printableTextForNextCharacter(line, &i, DiagOpts->TabStop); 12890b57cec5SDimitry Andric bool was_printable = res.second; 12900b57cec5SDimitry Andric 12910b57cec5SDimitry Andric if (DiagOpts->ShowColors && was_printable == print_reversed) { 12920b57cec5SDimitry Andric if (print_reversed) 12930b57cec5SDimitry Andric OS.reverseColor(); 12940b57cec5SDimitry Andric OS << to_print; 12950b57cec5SDimitry Andric to_print.clear(); 12960b57cec5SDimitry Andric if (DiagOpts->ShowColors) 12970b57cec5SDimitry Andric OS.resetColor(); 12980b57cec5SDimitry Andric } 12990b57cec5SDimitry Andric 13000b57cec5SDimitry Andric print_reversed = !was_printable; 13010b57cec5SDimitry Andric to_print += res.first.str(); 13020b57cec5SDimitry Andric } 13030b57cec5SDimitry Andric 13040b57cec5SDimitry Andric if (print_reversed && DiagOpts->ShowColors) 13050b57cec5SDimitry Andric OS.reverseColor(); 13060b57cec5SDimitry Andric OS << to_print; 13070b57cec5SDimitry Andric if (print_reversed && DiagOpts->ShowColors) 13080b57cec5SDimitry Andric OS.resetColor(); 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric OS << '\n'; 13110b57cec5SDimitry Andric } 13120b57cec5SDimitry Andric 13130b57cec5SDimitry Andric void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints, 13140b57cec5SDimitry Andric const SourceManager &SM) { 13150b57cec5SDimitry Andric if (!DiagOpts->ShowParseableFixits) 13160b57cec5SDimitry Andric return; 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric // We follow FixItRewriter's example in not (yet) handling 13190b57cec5SDimitry Andric // fix-its in macros. 13200b57cec5SDimitry Andric for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); 13210b57cec5SDimitry Andric I != E; ++I) { 13220b57cec5SDimitry Andric if (I->RemoveRange.isInvalid() || 13230b57cec5SDimitry Andric I->RemoveRange.getBegin().isMacroID() || 13240b57cec5SDimitry Andric I->RemoveRange.getEnd().isMacroID()) 13250b57cec5SDimitry Andric return; 13260b57cec5SDimitry Andric } 13270b57cec5SDimitry Andric 13280b57cec5SDimitry Andric for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); 13290b57cec5SDimitry Andric I != E; ++I) { 13300b57cec5SDimitry Andric SourceLocation BLoc = I->RemoveRange.getBegin(); 13310b57cec5SDimitry Andric SourceLocation ELoc = I->RemoveRange.getEnd(); 13320b57cec5SDimitry Andric 13330b57cec5SDimitry Andric std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc); 13340b57cec5SDimitry Andric std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc); 13350b57cec5SDimitry Andric 13360b57cec5SDimitry Andric // Adjust for token ranges. 13370b57cec5SDimitry Andric if (I->RemoveRange.isTokenRange()) 13380b57cec5SDimitry Andric EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts); 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric // We specifically do not do word-wrapping or tab-expansion here, 13410b57cec5SDimitry Andric // because this is supposed to be easy to parse. 13420b57cec5SDimitry Andric PresumedLoc PLoc = SM.getPresumedLoc(BLoc); 13430b57cec5SDimitry Andric if (PLoc.isInvalid()) 13440b57cec5SDimitry Andric break; 13450b57cec5SDimitry Andric 13460b57cec5SDimitry Andric OS << "fix-it:\""; 13470b57cec5SDimitry Andric OS.write_escaped(PLoc.getFilename()); 13480b57cec5SDimitry Andric OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second) 13490b57cec5SDimitry Andric << ':' << SM.getColumnNumber(BInfo.first, BInfo.second) 13500b57cec5SDimitry Andric << '-' << SM.getLineNumber(EInfo.first, EInfo.second) 13510b57cec5SDimitry Andric << ':' << SM.getColumnNumber(EInfo.first, EInfo.second) 13520b57cec5SDimitry Andric << "}:\""; 13530b57cec5SDimitry Andric OS.write_escaped(I->CodeToInsert); 13540b57cec5SDimitry Andric OS << "\"\n"; 13550b57cec5SDimitry Andric } 13560b57cec5SDimitry Andric } 1357