https://github.com/JDevlieghere created https://github.com/llvm/llvm-project/pull/181094
This threads the frame's language type through the source manager to the highlighter. Previously, we'd always pass "unknown" as the language type and rely on the language plugin to figure out the language based on the file extension. This change is motivated by #170250. For languages like Swift or Rust that don't have an upstream language plugin, we need the frame's language type for syntax highlighting. >From 3d09224d5d6c0993a896ed77520b6cf36ce02d40 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere <[email protected]> Date: Wed, 11 Feb 2026 22:49:29 -0800 Subject: [PATCH] [lldb] Pass the frame's language type to the highlighter This threads the frame's language type through the source manager to the highlighter. Previously, we'd always pass "unknown" as the language type and rely on the language plugin to figure out the language based on the file extension. This change is motivated by #170250. For languages like Swift or Rust that don't have an upstream language plugin, we need the frame's language type for syntax highlighting. --- lldb/include/lldb/Core/SourceManager.h | 19 +++++++++------ lldb/source/Core/SourceManager.cpp | 33 ++++++++++++++------------ lldb/source/Target/StackFrame.cpp | 3 ++- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/lldb/include/lldb/Core/SourceManager.h b/lldb/include/lldb/Core/SourceManager.h index 5a7b51528eb97..ec229809f9caf 100644 --- a/lldb/include/lldb/Core/SourceManager.h +++ b/lldb/include/lldb/Core/SourceManager.h @@ -45,9 +45,10 @@ class SourceManager { bool ModificationTimeIsStale() const; bool PathRemappingIsStale() const; - size_t DisplaySourceLines(uint32_t line, std::optional<size_t> column, - uint32_t context_before, uint32_t context_after, - Stream *s); + size_t DisplaySourceLines( + uint32_t line, std::optional<size_t> column, uint32_t context_before, + uint32_t context_after, Stream *s, + std::optional<lldb::LanguageType> language_type = std::nullopt); void FindLinesMatchingRegex(RegularExpression ®ex, uint32_t start_line, uint32_t end_line, std::vector<uint32_t> &match_lines); @@ -166,16 +167,20 @@ class SourceManager { SupportFileNSP support_file_nsp, uint32_t line, uint32_t column, uint32_t context_before, uint32_t context_after, const char *current_line_cstr, Stream *s, - const SymbolContextList *bp_locs = nullptr); + const SymbolContextList *bp_locs = nullptr, + std::optional<lldb::LanguageType> language_type = std::nullopt); // This variant uses the last file we visited. size_t DisplaySourceLinesWithLineNumbersUsingLastFile( uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column, const char *current_line_cstr, Stream *s, - const SymbolContextList *bp_locs = nullptr); + const SymbolContextList *bp_locs = nullptr, + std::optional<lldb::LanguageType> language_type = std::nullopt); - size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse, - const SymbolContextList *bp_locs = nullptr); + size_t DisplayMoreWithLineNumbers( + Stream *s, uint32_t count, bool reverse, + const SymbolContextList *bp_locs = nullptr, + std::optional<lldb::LanguageType> language_type = std::nullopt); bool SetDefaultFileAndLine(SupportFileNSP support_file_nsp, uint32_t line); diff --git a/lldb/source/Core/SourceManager.cpp b/lldb/source/Core/SourceManager.cpp index 5f0848c0abfd9..64446f347fd5d 100644 --- a/lldb/source/Core/SourceManager.cpp +++ b/lldb/source/Core/SourceManager.cpp @@ -226,8 +226,8 @@ static bool should_show_stop_line_with_ansi(DebuggerSP debugger_sp) { size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile( uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column, - const char *current_line_cstr, Stream *s, - const SymbolContextList *bp_locs) { + const char *current_line_cstr, Stream *s, const SymbolContextList *bp_locs, + std::optional<lldb::LanguageType> language_type) { if (count == 0) return 0; @@ -288,8 +288,8 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile( if (line == curr_line && column) columnToHighlight = column - 1; - size_t this_line_size = - last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s); + size_t this_line_size = last_file_sp->DisplaySourceLines( + line, columnToHighlight, 0, 0, s, language_type); if (column != 0 && line == curr_line && should_show_stop_column_with_caret(debugger_sp)) { // Display caret cursor. @@ -326,8 +326,8 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile( size_t SourceManager::DisplaySourceLinesWithLineNumbers( SupportFileNSP support_file_nsp, uint32_t line, uint32_t column, uint32_t context_before, uint32_t context_after, - const char *current_line_cstr, Stream *s, - const SymbolContextList *bp_locs) { + const char *current_line_cstr, Stream *s, const SymbolContextList *bp_locs, + std::optional<lldb::LanguageType> language_type) { assert(support_file_nsp && "SupportFile must be valid"); FileSP file_sp(GetFile(support_file_nsp)); @@ -346,11 +346,13 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbers( } return DisplaySourceLinesWithLineNumbersUsingLastFile( - start_line, count, line, column, current_line_cstr, s, bp_locs); + start_line, count, line, column, current_line_cstr, s, bp_locs, + language_type); } size_t SourceManager::DisplayMoreWithLineNumbers( - Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) { + Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs, + std::optional<lldb::LanguageType> language_type) { // If we get called before anybody has set a default file and line, then try // to figure it out here. FileSP last_file_sp(GetLastFile()); @@ -383,7 +385,8 @@ size_t SourceManager::DisplayMoreWithLineNumbers( const uint32_t column = 0; return DisplaySourceLinesWithLineNumbersUsingLastFile( - m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs); + m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs, + language_type); } return 0; } @@ -673,11 +676,10 @@ bool SourceManager::File::PathRemappingIsStale() const { return false; } -size_t SourceManager::File::DisplaySourceLines(uint32_t line, - std::optional<size_t> column, - uint32_t context_before, - uint32_t context_after, - Stream *s) { +size_t SourceManager::File::DisplaySourceLines( + uint32_t line, std::optional<size_t> column, uint32_t context_before, + uint32_t context_after, Stream *s, + std::optional<lldb::LanguageType> language_type) { // Nothing to write if there's no stream. if (!s) return 0; @@ -706,7 +708,8 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, GetSupportFile()->GetSpecOnly().GetPath(/*denormalize*/ false); // FIXME: Find a way to get the definitive language this file was written in // and pass it to the highlighter. - const auto &h = mgr.getHighlighterFor(lldb::eLanguageTypeUnknown, path); + const auto &h = mgr.getHighlighterFor( + language_type.value_or(lldb::eLanguageTypeUnknown), path); const uint32_t start_line = line <= context_before ? 1 : line - context_before; diff --git a/lldb/source/Target/StackFrame.cpp b/lldb/source/Target/StackFrame.cpp index 340607e14abed..ad2d4e38f660f 100644 --- a/lldb/source/Target/StackFrame.cpp +++ b/lldb/source/Target/StackFrame.cpp @@ -2076,7 +2076,8 @@ bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source, size_t num_lines = target->GetSourceManager().DisplaySourceLinesWithLineNumbers( source_file_sp, start_line, m_sc.line_entry.column, - source_lines_before, source_lines_after, "->", &strm); + source_lines_before, source_lines_after, "->", &strm, + /*bp_locs=*/nullptr, GetLanguage().AsLanguageType()); if (num_lines != 0) have_source = true; // TODO: Give here a one time warning if source file is missing. _______________________________________________ lldb-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
