summaryrefslogtreecommitdiffstats
path: root/lldb/source/Core/SourceManager.cpp
diff options
context:
space:
mode:
authorRaphael Isemann <teemperor@gmail.com>2018-08-02 00:30:15 +0000
committerRaphael Isemann <teemperor@gmail.com>2018-08-02 00:30:15 +0000
commit566afa0ab2ce3fc753ce7490f7c1394c3f4699c0 (patch)
tree4295653b2b9482af2a8f0cbcd7cc90ad7fd54147 /lldb/source/Core/SourceManager.cpp
parentcedebd594083a54cfb0a0b766fd000b6f11216fa (diff)
downloadbcm5719-llvm-566afa0ab2ce3fc753ce7490f7c1394c3f4699c0.tar.gz
bcm5719-llvm-566afa0ab2ce3fc753ce7490f7c1394c3f4699c0.zip
[LLDB] Added syntax highlighting support
Summary: This patch adds syntax highlighting support to LLDB. When enabled (and lldb is allowed to use colors), printed source code is annotated with the ANSI color escape sequences. So far we have only one highlighter which is based on Clang and is responsible for all languages that are supported by Clang. It essentially just runs the raw lexer over the input and then surrounds the specific tokens with the configured escape sequences. Reviewers: zturner, davide Reviewed By: davide Subscribers: labath, teemperor, llvm-commits, mgorny, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D49334 llvm-svn: 338662
Diffstat (limited to 'lldb/source/Core/SourceManager.cpp')
-rw-r--r--lldb/source/Core/SourceManager.cpp45
1 files changed, 41 insertions, 4 deletions
diff --git a/lldb/source/Core/SourceManager.cpp b/lldb/source/Core/SourceManager.cpp
index a6afd64f305..0ae1d9b9f0d 100644
--- a/lldb/source/Core/SourceManager.cpp
+++ b/lldb/source/Core/SourceManager.cpp
@@ -13,6 +13,7 @@
#include "lldb/Core/AddressRange.h" // for AddressRange
#include "lldb/Core/Debugger.h"
#include "lldb/Core/FormatEntity.h" // for FormatEntity
+#include "lldb/Core/Highlighter.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleList.h" // for ModuleList
#include "lldb/Host/FileSystem.h"
@@ -103,6 +104,18 @@ SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
return file_sp;
}
+static bool should_highlight_source(DebuggerSP debugger_sp) {
+ if (!debugger_sp)
+ return false;
+
+ // We don't use ANSI stop column formatting if the debugger doesn't think it
+ // should be using color.
+ if (!debugger_sp->GetUseColor())
+ return false;
+
+ return debugger_sp->GetHighlightSource();
+}
+
static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
// We don't use ANSI stop column formatting if we can't lookup values from
// the debugger.
@@ -114,6 +127,11 @@ static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
if (!debugger_sp->GetUseColor())
return false;
+ // Don't use terminal attributes when we have highlighting enabled. This
+ // can mess up the command line.
+ if (debugger_sp->GetHighlightSource())
+ return false;
+
// We only use ANSI stop column formatting if we're either supposed to show
// ANSI where available (which we know we have when we get to this point), or
// if we're only supposed to use ANSI.
@@ -515,6 +533,16 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
if (!m_data_sp)
return 0;
+ std::string previous_content;
+
+ HighlightStyle style = HighlightStyle::MakeVimStyle();
+ HighlighterManager mgr;
+ std::string path = GetFileSpec().GetPath(/*denormalize*/ false);
+ // FIXME: Find a way to get the definitive language this file was written in
+ // and pass it to the highlighter.
+ auto &highlighter =
+ mgr.getHighlighterFor(lldb::LanguageType::eLanguageTypeUnknown, path);
+
const uint32_t start_line =
line <= context_before ? 1 : line - context_before;
const uint32_t start_line_offset = GetLineOffset(start_line);
@@ -530,10 +558,19 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
size_t count = end_line_offset - start_line_offset;
const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
+ auto ref = llvm::StringRef(reinterpret_cast<const char *>(cstr), count);
bool displayed_line = false;
- if (column && (column < count)) {
- auto debugger_sp = m_debugger_wp.lock();
+ auto debugger_sp = m_debugger_wp.lock();
+ if (should_highlight_source(debugger_sp)) {
+ bytes_written +=
+ highlighter.Highlight(style, ref, previous_content, *s);
+ displayed_line = true;
+ // Add the new line to the previous lines.
+ previous_content += ref.str();
+ }
+
+ if (!displayed_line && column && (column < count)) {
if (should_show_stop_column_with_ansi(debugger_sp) && debugger_sp) {
// Check if we have any ANSI codes with which to mark this column. If
// not, no need to do this work.
@@ -581,10 +618,10 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
// If we didn't end up displaying the line with ANSI codes for whatever
// reason, display it now sans codes.
if (!displayed_line)
- bytes_written = s->Write(cstr, count);
+ bytes_written = s->PutCString(ref);
// Ensure we get an end of line character one way or another.
- if (!is_newline_char(cstr[count - 1]))
+ if (!is_newline_char(ref.back()))
bytes_written += s->EOL();
}
return bytes_written;
OpenPOWER on IntegriCloud