51 const clang::Token &token, llvm::StringRef tok_str,
53 using namespace clang;
55 if (token.is(tok::comment)) {
57 in_pp_directive =
false;
59 }
else if (in_pp_directive || token.getKind() == tok::hash) {
61 in_pp_directive =
true;
64 }
else if (tok::isStringLiteral(token.getKind()))
66 else if (tok::isLiteral(token.getKind()))
71 switch (token.getKind()) {
72 case tok::raw_identifier:
100 case tok::minusminus:
101 case tok::minusequal:
104 case tok::exclaimequal:
106 case tok::slashequal:
108 case tok::percentequal:
112 case tok::lesslessequal:
115 case tok::greatergreater:
116 case tok::greaterequal:
117 case tok::greatergreaterequal:
119 case tok::caretequal:
125 case tok::equalequal:
134 llvm::StringRef line,
135 std::optional<size_t> cursor_pos,
136 llvm::StringRef previous_lines,
138 using namespace clang;
140 FileSystemOptions file_opts;
141 FileManager file_mgr(file_opts,
150 llvm::StringRef line_ending =
"";
153 if (line.consume_back(
"\r\n"))
154 line_ending =
"\r\n";
155 else if (line.consume_back(
"\n"))
157 else if (line.consume_back(
"\r"))
160 unsigned line_number = previous_lines.count(
'\n') + 1U;
164 std::string full_source = previous_lines.str() + line.str();
165 DiagnosticOptions diags_opts;
166 DiagnosticsEngine diags(DiagnosticIDs::create(), diags_opts);
167 clang::SourceManager SM(diags, file_mgr);
168 auto buf = llvm::MemoryBuffer::getMemBuffer(full_source);
170 FileID FID = SM.createFileID(buf->getMemBufferRef());
177 Opts.CPlusPlus17 =
true;
178 Opts.LineComment =
true;
180 Lexer lex(FID, buf->getMemBufferRef(), SM, Opts);
182 lex.SetKeepWhitespaceMode(
true);
185 bool in_pp_directive =
false;
188 bool found_user_line =
false;
191 bool highlighted_cursor =
false;
196 exit = lex.LexFromRawLexer(token);
198 bool invalid =
false;
199 unsigned current_line_number =
200 SM.getSpellingLineNumber(token.getLocation(), &invalid);
201 if (current_line_number != line_number)
203 found_user_line =
true;
211 unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);
218 if (token.isAnnotation())
222 llvm::StringRef tok_str = line.substr(start, token.getLength());
230 llvm::StringRef to_print = tok_str;
232 auto end = start + token.getLength();
233 if (cursor_pos && end > *cursor_pos && !highlighted_cursor) {
234 highlighted_cursor =
true;
243 color.
Apply(result, to_print);
247 result << line_ending;
253 if (!found_user_line) {
255 assert(
false &&
"We couldn't find the user line in the input file?");
static HighlightStyle::ColorStyle determineClangStyle(const ClangHighlighter &highlighter, const clang::Token &token, llvm::StringRef tok_str, const HighlightStyle &options, bool &in_pp_directive)
Determines which style should be applied to the given token.
void Highlight(const HighlightStyle &options, llvm::StringRef line, std::optional< size_t > cursor_pos, llvm::StringRef previous_lines, Stream &s) const override
Highlights the given line.