@@ -1654,7 +1654,8 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
16541654
16551655 if (language_plugin)
16561656 language_plugin_handled = language_plugin->GetFunctionDisplayName (
1657- sc, exe_ctx, Language::FunctionNameRepresentation::eName, ss);
1657+ sc, exe_ctx, Language::FunctionNameRepresentation::eName, ss,
1658+ entry.highlight );
16581659
16591660 if (language_plugin_handled) {
16601661 s << ss.GetString ();
@@ -1690,7 +1691,7 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
16901691 if (language_plugin)
16911692 language_plugin_handled = language_plugin->GetFunctionDisplayName (
16921693 sc, exe_ctx, Language::FunctionNameRepresentation::eNameWithNoArgs,
1693- ss);
1694+ ss, entry. highlight );
16941695
16951696 if (language_plugin_handled) {
16961697 s << ss.GetString ();
@@ -1724,7 +1725,8 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
17241725
17251726 if (language_plugin)
17261727 language_plugin_handled = language_plugin->GetFunctionDisplayName (
1727- sc, exe_ctx, Language::FunctionNameRepresentation::eNameWithArgs, ss);
1728+ sc, exe_ctx, Language::FunctionNameRepresentation::eNameWithArgs, ss,
1729+ entry.highlight );
17281730
17291731 if (language_plugin_handled) {
17301732 s << ss.GetString ();
@@ -2046,6 +2048,54 @@ static const Definition *FindEntry(const llvm::StringRef &format_str,
20462048 return parent;
20472049}
20482050
2051+ static llvm::Expected<Entry::HighlightSettings>
2052+ ParseHighlightSettings (const Entry &entry) {
2053+ // FIXME: support other function.name-XXX types as well
2054+ if (entry.type != Entry::Type::FunctionNameWithArgs)
2055+ return llvm::createStringError (
2056+ " The 'highlight_basename' format can only be used on "
2057+ " ${function.name-with-args}" );
2058+
2059+ llvm::StringRef format = entry.printf_format ;
2060+ if (!format.consume_front (" highlight_" ))
2061+ return llvm::createStringError (
2062+ " Expected 'highlight_' prefix not found in: %s." ,
2063+ entry.printf_format .c_str ());
2064+
2065+ Entry::HighlightSettings settings;
2066+ if (format.consume_front (" basename" )) {
2067+ settings.kind = Entry::HighlightSettings::Kind::Basename;
2068+ } else {
2069+ return llvm::createStringError (
2070+ " Unsupported highlight kind detected in: %s. "
2071+ " Currently supported: basename" ,
2072+ entry.printf_format .c_str ());
2073+ }
2074+
2075+ llvm::SmallVector<llvm::StringRef, 1 > matches;
2076+ // TODO: support ${ansi.XXX} syntax. ExtractVariableInfo needs
2077+ // to be adjusted to support nested '{}'.
2078+ llvm::Regex color_pattern{R"( ^\(([a-z\.]+)\)$)" };
2079+ if (!color_pattern.match (format, &matches))
2080+ return llvm::createStringError (" Couldn't find valid color variable in: %s. "
2081+ " Expected format: (ansi.some-color)" ,
2082+ entry.printf_format .c_str ());
2083+
2084+ assert (matches.size () == 2 );
2085+
2086+ std::string color_format = (" ${" + matches[1 ] + " }" ).str ();
2087+ std::string terminal_code = ansi::FormatAnsiTerminalCodes (color_format);
2088+ if (terminal_code.empty ())
2089+ return llvm::createStringError (" Invalid color variable '%s' found in: %s" ,
2090+ color_format.c_str (),
2091+ entry.printf_format .c_str ());
2092+
2093+ settings.prefix = std::move (terminal_code);
2094+ settings.suffix = ansi::FormatAnsiTerminalCodes (" ${ansi.normal}" );
2095+
2096+ return settings;
2097+ }
2098+
20492099static Status ParseInternal (llvm::StringRef &format, Entry &parent_entry,
20502100 uint32_t depth) {
20512101 Status error;
@@ -2201,6 +2251,7 @@ static Status ParseInternal(llvm::StringRef &format, Entry &parent_entry,
22012251 if (error.Fail ())
22022252 return error;
22032253 bool verify_is_thread_id = false ;
2254+ bool parse_highlight_settings = false ;
22042255 Entry entry;
22052256 if (!variable_format.empty ()) {
22062257 entry.printf_format = variable_format.str ();
@@ -2266,6 +2317,8 @@ static Status ParseInternal(llvm::StringRef &format, Entry &parent_entry,
22662317 clear_printf = true ;
22672318 } else if (entry.printf_format == " tid" ) {
22682319 verify_is_thread_id = true ;
2320+ } else if (entry.printf_format .find (" highlight_" ) == 0 ) {
2321+ parse_highlight_settings = true ;
22692322 } else {
22702323 error = Status::FromErrorStringWithFormat (
22712324 " invalid format: '%s'" , entry.printf_format .c_str ());
@@ -2307,6 +2360,14 @@ static Status ParseInternal(llvm::StringRef &format, Entry &parent_entry,
23072360 " the 'tid' format can only be used on "
23082361 " ${thread.id} and ${thread.protocol_id}" );
23092362 }
2363+ } else if (parse_highlight_settings) {
2364+ auto highlight_or_err = ParseHighlightSettings (entry);
2365+ if (highlight_or_err) {
2366+ entry.highlight = std::move (*highlight_or_err);
2367+ entry.printf_format .clear ();
2368+ } else {
2369+ error = Status::FromError (highlight_or_err.takeError ());
2370+ }
23102371 }
23112372
23122373 switch (entry.type ) {
0 commit comments