clang 8.0.0

Functions | Variables
TextDiagnostic.cpp File Reference
`#include "clang/Frontend/TextDiagnostic.h"`
`#include "clang/Basic/CharInfo.h"`
`#include "clang/Basic/DiagnosticOptions.h"`
`#include "clang/Basic/FileManager.h"`
`#include "clang/Basic/SourceManager.h"`
`#include "clang/Lex/Lexer.h"`
`#include "llvm/ADT/SmallString.h"`
`#include "llvm/ADT/StringExtras.h"`
`#include "llvm/Support/ConvertUTF.h"`
`#include "llvm/Support/ErrorHandling.h"`
`#include "llvm/Support/Locale.h"`
`#include "llvm/Support/Path.h"`
`#include "llvm/Support/raw_ostream.h"`
`#include <algorithm>`
Include dependency graph for TextDiagnostic.cpp:

Go to the source code of this file.

## Functions
static void | applyTemplateHighlighting (raw_ostream &OS, StringRef Str, bool &Normal, bool Bold)
| Add highlights to differences in template strings. More...
static int | bytesSincePreviousTabOrLineBegin (StringRef SourceLine, size_t i)
static std::pair< SmallString< 16 >, bool > | printableTextForNextCharacter (StringRef SourceLine, size_t *i, unsigned TabStop)
| returns a printable representation of first item from input range More...
static void | expandTabs (std::string &SourceLine, unsigned TabStop)
static void | byteToColumn (StringRef SourceLine, unsigned TabStop, SmallVectorImpl< int > &out)
| This function takes a raw source line and produces a mapping from the bytes of the printable representation of the line to the columns those printable characters will appear at (numbering the first column as 0). More...
static void | columnToByte (StringRef SourceLine, unsigned TabStop, SmallVectorImpl< int > &out)
| This function takes a raw source line and produces a mapping from columns to the byte of the source line that produced the character displaying at that column. More...
static void | selectInterestingSourceRegion (std::string &SourceLine, std::string &CaretLine, std::string &FixItInsertionLine, unsigned Columns, const SourceColumnMap &map)
| When the source code line we want to print is too long for the terminal, select the "interesting" region. More...
static unsigned | skipWhitespace (unsigned Idx, StringRef Str, unsigned Length)
| Skip over whitespace in the string, starting at the given index. More...
static char | findMatchingPunctuation (char c)
| If the given character is the start of some kind of balanced punctuation (e.g., quotes or parentheses), return the character that will terminate the punctuation. More...
static unsigned | findEndOfWord (unsigned Start, StringRef Str, unsigned Length, unsigned Column, unsigned Columns)
| Find the end of the word starting at the given offset within a string. More...
static bool | printWordWrapped (raw_ostream &OS, StringRef Str, unsigned Columns, unsigned Column=0, bool Bold=false, unsigned Indentation=WordWrapIndentation)
| Print the given string to a stream, word-wrapping it to some number of columns in the process. More...
static llvm::Optional< std::pair< unsigned, unsigned > > | findLinesForRange (const CharSourceRange &R, FileID FID, const SourceManager &SM)
| Find the suitable set of lines to show to include a set of ranges. More...
static std::pair< unsigned, unsigned > | maybeAddRange (std::pair< unsigned, unsigned > A, std::pair< unsigned, unsigned > B, unsigned MaxRange)
| Add as much of range B into range A as possible without exceeding a maximum size of MaxRange. More...
static void | highlightRange (const CharSourceRange &R, unsigned LineNo, FileID FID, const SourceColumnMap &map, std::string &CaretLine, const SourceManager &SM, const LangOptions &LangOpts)
| Highlight a SourceRange (with ~'s) for any characters on LineNo. More...
static std::string | buildFixItInsertionLine (FileID FID, unsigned LineNo, const SourceColumnMap &map, ArrayRef< FixItHint > Hints, const SourceManager &SM, const DiagnosticOptions *DiagOpts)

## Variables
static enum raw_ostream::Colors | noteColor
static enum raw_ostream::Colors | remarkColor
static enum raw_ostream::Colors | fixitColor
static enum raw_ostream::Colors | caretColor
static enum raw_ostream::Colors | warningColor
static enum raw_ostream::Colors | templateColor
static enum raw_ostream::Colors | errorColor = raw_ostream::RED
static enum raw_ostream::Colors | fatalColor = raw_ostream::RED
static enum raw_ostream::Colors | savedColor
const unsigned | WordWrapIndentation = 6
| Number of spaces to indent when word-wrapping. More...

## Function Documentation

## ◆ applyTemplateHighlighting()

static void applyTemplateHighlighting | ( | raw_ostream & | OS,
|  | StringRef | Str,
|  | bool & | Normal,
|  | bool | Bold
| ) |  |
| static

Add highlights to differences in template strings.

Definition at line 46 of file TextDiagnostic.cpp.

References savedColor, templateColor, and clang::ToggleHighlight.

Referenced by clang::TextDiagnostic::printDiagnosticMessage(), and printWordWrapped().

## ◆ buildFixItInsertionLine()

static std::string buildFixItInsertionLine | ( | FileID | FID,
|  | unsigned | LineNo,
|  | const SourceColumnMap & | map,
|  | ArrayRef< FixItHint > | Hints,
|  | const SourceManager & | SM,
|  | const DiagnosticOptions * | DiagOpts
| ) |  |
| static

Definition at line 1049 of file TextDiagnostic.cpp.

References caretColor, clang::format::encoding::columnWidth(), clang::DiagnosticRenderer::DiagOpts, expandTabs(), findLinesForRange(), fixitColor, clang::FullSourceLoc::getBufferData(), clang::FullSourceLoc::getColumnNumber(), clang::SourceManager::getColumnNumber(), clang::SourceManager::getDecomposedExpansionLoc(), clang::FullSourceLoc::getDecomposedLoc(), clang::SourceManager::getDecomposedLoc(), clang::PresumedLoc::getFilename(), clang::FullSourceLoc::getLineNumber(), clang::SourceManager::getLineNumber(), clang::FullSourceLoc::getManager(), clang::SourceManager::getPresumedLoc(), highlightRange(), clang::SourceLocation::isFileID(), clang::PresumedLoc::isInvalid(), clang::SourceLocation::isValid(), clang::DiagnosticRenderer::LangOpts, clang::DiagnosticRenderer::LastLevel, clang::DiagnosticRenderer::LastLoc, Line, maybeAddRange(), clang::Lexer::MeasureTokenLength(), clang::DiagnosticsEngine::Note, printableTextForNextCharacter(), selectInterestingSourceRegion(), and clang::SourceManager::translateLineCol().

## ◆ bytesSincePreviousTabOrLineBegin()

static int bytesSincePreviousTabOrLineBegin | ( | StringRef | SourceLine,
|  | size_t | i
| ) |  |
| static

Definition at line 69 of file TextDiagnostic.cpp.

References bytes().

Referenced by printableTextForNextCharacter().

## ◆ byteToColumn()

static void byteToColumn | ( | StringRef | SourceLine,
|  | unsigned | TabStop,
|  | SmallVectorImpl< int > & | out
| ) |  |
| static

This function takes a raw source line and produces a mapping from the bytes of the printable representation of the line to the columns those printable characters will appear at (numbering the first column as 0).

If a byte 'i' corresponds to multiple columns (e.g. the byte contains a tab character) then the array will map that byte to the first column the tab appears at and the next value in the map will have been incremented more than once.

If a byte is the first in a sequence of bytes that together map to a single entity in the output, then the array will map that byte to the appropriate column while the subsequent bytes will be -1.

The last element in the array does not correspond to any byte in the input and instead is the number of columns needed to display the source

example: (given a tabstop of 8)

"a \t \u3042" -> {0,1,2,8,9,-1,-1,11}

(\u3042 is represented in UTF-8 by three bytes and takes two columns to display)

Definition at line 198 of file TextDiagnostic.cpp.

References clang::format::encoding::columnWidth(), and printableTextForNextCharacter().

Referenced by columnToByte().

## ◆ columnToByte()

static void columnToByte | ( | StringRef | SourceLine,
|  | unsigned | TabStop,
|  | SmallVectorImpl< int > & | out
| ) |  |
| static

This function takes a raw source line and produces a mapping from columns to the byte of the source line that produced the character displaying at that column.

This is the inverse of the mapping produced by byteToColumn()

The last element in the array is the number of bytes in the source string

example: (given a tabstop of 8)

"a \t \u3042" -> {0,1,2,-1,-1,-1,-1,-1,3,4,-1,7}

(\u3042 is represented in UTF-8 by three bytes and takes two columns to display)

Definition at line 232 of file TextDiagnostic.cpp.

References bytes(), byteToColumn(), clang::format::encoding::columnWidth(), and printableTextForNextCharacter().

## ◆ expandTabs()

static void expandTabs | ( | std::string & | SourceLine,
|  | unsigned | TabStop
| ) |  |
| static

Definition at line 163 of file TextDiagnostic.cpp.

References printableTextForNextCharacter().

Referenced by buildFixItInsertionLine().

## ◆ findEndOfWord()

static unsigned findEndOfWord | ( | unsigned | Start,
|  | StringRef | Str,
|  | unsigned | Length,
|  | unsigned | Column,
|  | unsigned | Columns
| ) |  |
| static

Find the end of the word starting at the given offset within a string.

Returns
  the index pointing one character past the end of the word.

Definition at line 547 of file TextDiagnostic.cpp.

References End, findMatchingPunctuation(), and clang::isWhitespace().

Referenced by printWordWrapped().

## ◆ findLinesForRange()

static llvm::Optional<std::pair<unsigned, unsigned> > findLinesForRange | ( | const CharSourceRange & | R,
|  | FileID | FID,
|  | const SourceManager & | SM
| ) |  |
| static

Find the suitable set of lines to show to include a set of ranges.

Definition at line 918 of file TextDiagnostic.cpp.

References Begin, End, clang::CharSourceRange::getBegin(), clang::CharSourceRange::getEnd(), clang::SourceManager::getExpansionLineNumber(), clang::SourceManager::getFileID(), clang::CharSourceRange::isValid(), and clang::None.

Referenced by buildFixItInsertionLine().

## ◆ findMatchingPunctuation()

static char findMatchingPunctuation | ( | char | c | ) |
| inline static

If the given character is the start of some kind of balanced punctuation (e.g., quotes or parentheses), return the character that will terminate the punctuation.

Returns
  The ending punctuation character, if any, or the NULL character if the input character does not start any punctuation.

Definition at line 528 of file TextDiagnostic.cpp.

Referenced by findEndOfWord().

## ◆ highlightRange()

static void highlightRange | ( | const CharSourceRange & | R,
|  | unsigned | LineNo,
|  | FileID | FID,
|  | const SourceColumnMap & | map,
|  | std::string & | CaretLine,
|  | const SourceManager & | SM,
|  | const LangOptions & | LangOpts
| ) |  |
| static

Highlight a SourceRange (with ~'s) for any characters on LineNo.

Definition at line 967 of file TextDiagnostic.cpp.

References Begin, End, clang::CharSourceRange::getBegin(), clang::CharSourceRange::getEnd(), clang::SourceManager::getExpansionColumnNumber(), clang::SourceManager::getExpansionLineNumber(), clang::SourceManager::getFileID(), clang::CharSourceRange::isTokenRange(), clang::CharSourceRange::isValid(), and clang::Lexer::MeasureTokenLength().

Referenced by buildFixItInsertionLine().

## ◆ maybeAddRange()

static std::pair<unsigned, unsigned> maybeAddRange | ( | std::pair< unsigned, unsigned > | A,
|  | std::pair< unsigned, unsigned > | B,
|  | unsigned | MaxRange
| ) |  |
| static

Add as much of range B into range A as possible without exceeding a maximum size of MaxRange.

Ranges are inclusive.

Definition at line 934 of file TextDiagnostic.cpp.

References max(), and min().

Referenced by buildFixItInsertionLine().

## ◆ printableTextForNextCharacter()

static std::pair<SmallString<16>, bool> printableTextForNextCharacter | ( | StringRef | SourceLine,
|  | size_t * | i,
|  | unsigned | TabStop
| ) |  |
| static

returns a printable representation of first item from input range

This function returns a printable representation of the next item in a line of source. If the next byte begins a valid and printable character, that character is returned along with 'true'.

Otherwise, if the next byte begins a valid, but unprintable character, a printable, escaped representation of the character is returned, along with 'false'. Otherwise a printable, escaped representation of the next byte is returned along with 'false'.

Note
  The index is updated to be used with a subsequent call to printableTextForNextCharacter.

Parameters

  SourceLine | The line of source
  i | Pointer to byte index,
  TabStop | used to expand tabs

Returns
  pair(printable text, 'true' iff original text was printable)

Definition at line 99 of file TextDiagnostic.cpp.

References bytesSincePreviousTabOrLineBegin(), and clang::DiagnosticOptions::MaxTabStop.

Referenced by buildFixItInsertionLine(), byteToColumn(), columnToByte(), and expandTabs().

## ◆ printWordWrapped()

static bool printWordWrapped | ( | raw_ostream & | OS,
|  | StringRef | Str,
|  | unsigned | Columns,
|  | unsigned | Column = `0`,
|  | bool | Bold = `false`,
|  | unsigned | Indentation = `WordWrapIndentation`
| ) |  |
| static

Print the given string to a stream, word-wrapping it to some number of columns in the process.

Parameters

  OS | the stream to which the word-wrapping string will be emitted.
  Str | the string to word-wrap and output.
  Columns | the number of columns to word-wrap to.
  Column | the column number at which the first character of `Str` will be printed. This will be non-zero when part of the first line has already been printed.
  Bold | if the current text should be bold
  Indentation | the number of spaces to indent any lines beyond the first line.

Returns
  true if word-wrapping was required, or false if the string fit on the first line.

Definition at line 614 of file TextDiagnostic.cpp.

References applyTemplateHighlighting(), findEndOfWord(), min(), and skipWhitespace().

Referenced by clang::TextDiagnostic::printDiagnosticMessage().

## ◆ selectInterestingSourceRegion()

static void selectInterestingSourceRegion | ( | std::string & | SourceLine,
|  | std::string & | CaretLine,
|  | std::string & | FixItInsertionLine,
|  | unsigned | Columns,
|  | const SourceColumnMap & | map
| ) |  |
| static

When the source code line we want to print is too long for the terminal, select the "interesting" region.

Definition at line 322 of file TextDiagnostic.cpp.

References clang::format::encoding::columnWidth(), clang::isWhitespace(), max(), and min().

Referenced by buildFixItInsertionLine().

## ◆ skipWhitespace()

static unsigned skipWhitespace | ( | unsigned | Idx,
|  | StringRef | Str,
|  | unsigned | Length
| ) |  |
| static

Skip over whitespace in the string, starting at the given index.

Returns
  The index of the first non-whitespace character that is greater than or equal to Idx or, if no such character exists, returns the end of the string.

Definition at line 516 of file TextDiagnostic.cpp.

References clang::isWhitespace().

Referenced by printWordWrapped().

## Variable Documentation

## ◆ caretColor

enum raw_ostream::Colors caretColor
| static

Initial value:
=
raw_ostream::GREEN

Definition at line 33 of file TextDiagnostic.cpp.

Referenced by buildFixItInsertionLine().

## ◆ errorColor

enum raw_ostream::Colors errorColor = raw_ostream::RED
| static

Definition at line 39 of file TextDiagnostic.cpp.

Referenced by clang::TextDiagnostic::printDiagnosticLevel().

## ◆ fatalColor

enum raw_ostream::Colors fatalColor = raw_ostream::RED
| static

Definition at line 40 of file TextDiagnostic.cpp.

Referenced by clang::TextDiagnostic::printDiagnosticLevel().

## ◆ fixitColor

enum raw_ostream::Colors fixitColor
| static

Initial value:
=
raw_ostream::GREEN

Definition at line 31 of file TextDiagnostic.cpp.

Referenced by buildFixItInsertionLine().

## ◆ noteColor

enum raw_ostream::Colors noteColor
| static

Initial value:
=
raw_ostream::BLACK

Definition at line 27 of file TextDiagnostic.cpp.

Referenced by clang::TextDiagnostic::printDiagnosticLevel().

## ◆ remarkColor

enum raw_ostream::Colors remarkColor
| static

Initial value:
=
raw_ostream::BLUE

Definition at line 29 of file TextDiagnostic.cpp.

Referenced by clang::TextDiagnostic::printDiagnosticLevel().

## ◆ savedColor

enum raw_ostream::Colors savedColor
| static

Initial value:
=
raw_ostream::SAVEDCOLOR

Definition at line 42 of file TextDiagnostic.cpp.

Referenced by applyTemplateHighlighting(), clang::TextDiagnostic::emitDiagnosticLoc(), and clang::TextDiagnostic::printDiagnosticMessage().

## ◆ templateColor

enum raw_ostream::Colors templateColor
| static

Initial value:
=
raw_ostream::CYAN

Definition at line 37 of file TextDiagnostic.cpp.

Referenced by applyTemplateHighlighting().

## ◆ warningColor

enum raw_ostream::Colors warningColor
| static

Initial value:
=
raw_ostream::MAGENTA

Definition at line 35 of file TextDiagnostic.cpp.

Referenced by clang::TextDiagnostic::printDiagnosticLevel().

## ◆ WordWrapIndentation

const unsigned WordWrapIndentation = 6

Number of spaces to indent when word-wrapping.

Definition at line 67 of file TextDiagnostic.cpp.