blob: 648d20bcfa40c012ecedad86cd76458d1d387c73 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SourceManager.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/SourceManager.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/DataBuffer.h"
Jim Inghame37d6052011-09-13 00:29:56 +000017#include "lldb/Core/Debugger.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Core/Module.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000019#include "lldb/Core/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Stream.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Symbol/CompileUnit.h"
22#include "lldb/Symbol/Function.h"
Greg Clayton176761e2011-04-19 04:19:37 +000023#include "lldb/Symbol/SymbolContext.h"
Greg Clayton7e14f912011-04-23 02:04:55 +000024#include "lldb/Target/Target.h"
Todd Fiala9666ba72016-09-21 20:13:14 +000025#include "lldb/Utility/AnsiTerminal.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Greg Clayton9585fbf2013-03-19 00:20:55 +000027using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028using namespace lldb_private;
29
Kate Stoneb9c1b512016-09-06 20:57:50 +000030static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
32//----------------------------------------------------------------------
33// SourceManager constructor
34//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000035SourceManager::SourceManager(const TargetSP &target_sp)
36 : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
37 m_target_wp(target_sp),
38 m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
Jim Inghame37d6052011-09-13 00:29:56 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040SourceManager::SourceManager(const DebuggerSP &debugger_sp)
41 : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
42 m_target_wp(), m_debugger_wp(debugger_sp) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
44//----------------------------------------------------------------------
45// Destructor
46//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000047SourceManager::~SourceManager() {}
48
49SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
50 bool same_as_previous =
51 m_last_file_sp && m_last_file_sp->FileSpecMatches(file_spec);
52
53 DebuggerSP debugger_sp(m_debugger_wp.lock());
54 FileSP file_sp;
55 if (same_as_previous)
56 file_sp = m_last_file_sp;
57 else if (debugger_sp)
58 file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
59
60 TargetSP target_sp(m_target_wp.lock());
61
62 // It the target source path map has been updated, get this file again so we
63 // can successfully remap the source file
64 if (target_sp && file_sp &&
65 file_sp->GetSourceMapModificationID() !=
66 target_sp->GetSourcePathMap().GetModificationID())
67 file_sp.reset();
68
69 // Update the file contents if needed if we found a file
70 if (file_sp)
71 file_sp->UpdateIfNeeded();
72
73 // If file_sp is no good or it points to a non-existent file, reset it.
74 if (!file_sp || !file_sp->GetFileSpec().Exists()) {
Todd Fiala9666ba72016-09-21 20:13:14 +000075 if (target_sp)
76 file_sp.reset(new File(file_spec, target_sp.get()));
77 else
78 file_sp.reset(new File(file_spec, debugger_sp));
Kate Stoneb9c1b512016-09-06 20:57:50 +000079
80 if (debugger_sp)
81 debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
82 }
83 return file_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084}
85
Todd Fiala9666ba72016-09-21 20:13:14 +000086static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
87 // We don't use ANSI stop column formatting if we can't lookup values from
88 // the debugger.
89 if (!debugger_sp)
90 return false;
91
92 // We don't use ANSI stop column formatting if the debugger doesn't think
93 // it should be using color.
94 if (!debugger_sp->GetUseColor())
95 return false;
96
97 // We only use ANSI stop column formatting if we're either supposed to show
98 // ANSI where available (which we know we have when we get to this point), or
99 // if we're only supposed to use ANSI.
100 const auto value = debugger_sp->GetStopShowColumn();
101 return ((value == eStopShowColumnAnsiOrCaret) ||
102 (value == eStopShowColumnAnsi));
103}
104
105static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
106 // We don't use text-based stop column formatting if we can't lookup values
107 // from the debugger.
108 if (!debugger_sp)
109 return false;
110
111 // If we're asked to show the first available of ANSI or caret, then
112 // we do show the caret when ANSI is not available.
113 const auto value = debugger_sp->GetStopShowColumn();
114 if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
115 return true;
116
117 // The only other time we use caret is if we're explicitly asked to show
118 // caret.
119 return value == eStopShowColumnCaret;
120}
121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000123 uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 const char *current_line_cstr, Stream *s,
125 const SymbolContextList *bp_locs) {
126 if (count == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 size_t return_value = 0;
129 if (start_line == 0) {
130 if (m_last_line != 0 && m_last_line != UINT32_MAX)
131 start_line = m_last_line + m_last_count;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000132 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 start_line = 1;
134 }
135
136 if (!m_default_set) {
137 FileSpec tmp_spec;
138 uint32_t tmp_line;
139 GetDefaultFileAndLine(tmp_spec, tmp_line);
140 }
141
142 m_last_line = start_line;
143 m_last_count = count;
144
145 if (m_last_file_sp.get()) {
146 const uint32_t end_line = start_line + count - 1;
147 for (uint32_t line = start_line; line <= end_line; ++line) {
148 if (!m_last_file_sp->LineIsValid(line)) {
149 m_last_line = UINT32_MAX;
150 break;
151 }
152
153 char prefix[32] = "";
154 if (bp_locs) {
155 uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
156
157 if (bp_count > 0)
158 ::snprintf(prefix, sizeof(prefix), "[%u] ", bp_count);
159 else
160 ::snprintf(prefix, sizeof(prefix), " ");
161 }
162
163 return_value +=
164 s->Printf("%s%2.2s %-4u\t", prefix,
165 line == curr_line ? current_line_cstr : "", line);
Todd Fiala9666ba72016-09-21 20:13:14 +0000166 size_t this_line_size = m_last_file_sp->DisplaySourceLines(
167 line, line == curr_line ? column : 0, 0, 0, s);
168 if (column != 0 && line == curr_line &&
169 should_show_stop_column_with_caret(m_debugger_wp.lock())) {
170 // Display caret cursor.
171 std::string src_line;
172 m_last_file_sp->GetLine(line, src_line);
173 return_value += s->Printf(" \t");
174 // Insert a space for every non-tab character in the source line.
Ed Maste05092032016-09-21 22:36:51 +0000175 for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
Todd Fiala9666ba72016-09-21 20:13:14 +0000176 return_value += s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
177 // Now add the caret.
178 return_value += s->Printf("^\n");
179 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 if (this_line_size == 0) {
181 m_last_line = UINT32_MAX;
182 break;
183 } else
184 return_value += this_line_size;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000185 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 }
187 return return_value;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000188}
189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190size_t SourceManager::DisplaySourceLinesWithLineNumbers(
Todd Fiala9666ba72016-09-21 20:13:14 +0000191 const FileSpec &file_spec, uint32_t line, uint32_t column,
192 uint32_t context_before, uint32_t context_after,
193 const char *current_line_cstr, Stream *s,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 const SymbolContextList *bp_locs) {
195 FileSP file_sp(GetFile(file_spec));
Greg Clayton9585fbf2013-03-19 00:20:55 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 uint32_t start_line;
198 uint32_t count = context_before + context_after + 1;
199 if (line > context_before)
200 start_line = line - context_before;
201 else
202 start_line = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 if (m_last_file_sp.get() != file_sp.get()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 if (line == 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 m_last_line = 0;
207 m_last_file_sp = file_sp;
208 }
209 return DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000210 start_line, count, line, column, current_line_cstr, s, bp_locs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211}
212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213size_t SourceManager::DisplayMoreWithLineNumbers(
214 Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
215 // If we get called before anybody has set a default file and line, then try
216 // to figure it out here.
217 const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
218 if (!m_default_set) {
219 FileSpec tmp_spec;
220 uint32_t tmp_line;
221 GetDefaultFileAndLine(tmp_spec, tmp_line);
222 }
Greg Clayton44d93782014-01-27 23:43:24 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 if (m_last_file_sp) {
225 if (m_last_line == UINT32_MAX)
226 return 0;
Greg Clayton44d93782014-01-27 23:43:24 +0000227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 if (reverse && m_last_line == 1)
229 return 0;
Greg Clayton44d93782014-01-27 23:43:24 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 if (count > 0)
232 m_last_count = count;
233 else if (m_last_count == 0)
234 m_last_count = 10;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 if (m_last_line > 0) {
237 if (reverse) {
238 // If this is the first time we've done a reverse, then back up one more
239 // time so we end
240 // up showing the chunk before the last one we've shown:
241 if (m_last_line > m_last_count)
242 m_last_line -= m_last_count;
Jim Inghame37d6052011-09-13 00:29:56 +0000243 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 m_last_line = 1;
245 } else if (have_default_file_line)
246 m_last_line += m_last_count;
247 } else
248 m_last_line = 1;
249
Todd Fiala9666ba72016-09-21 20:13:14 +0000250 const uint32_t column = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 return DisplaySourceLinesWithLineNumbersUsingLastFile(
Todd Fiala9666ba72016-09-21 20:13:14 +0000252 m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 }
254 return 0;
Jim Inghame37d6052011-09-13 00:29:56 +0000255}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
258 uint32_t line) {
259 FileSP old_file_sp = m_last_file_sp;
260 m_last_file_sp = GetFile(file_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 m_default_set = true;
263 if (m_last_file_sp) {
264 m_last_line = line;
Jim Ingham969795f2011-09-21 01:17:13 +0000265 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 } else {
267 m_last_file_sp = old_file_sp;
268 return false;
269 }
Jim Ingham969795f2011-09-21 01:17:13 +0000270}
271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
273 if (m_last_file_sp) {
274 file_spec = m_last_file_sp->GetFileSpec();
275 line = m_last_line;
276 return true;
277 } else if (!m_default_set) {
278 TargetSP target_sp(m_target_wp.lock());
279
280 if (target_sp) {
281 // If nobody has set the default file and line then try here. If there's
282 // no executable, then we
283 // will try again later when there is one. Otherwise, if we can't find it
284 // we won't look again,
285 // somebody will have to set it (for instance when we stop somewhere...)
286 Module *executable_ptr = target_sp->GetExecutableModulePointer();
287 if (executable_ptr) {
288 SymbolContextList sc_list;
289 ConstString main_name("main");
290 bool symbols_okay = false; // Force it to be a debug symbol.
291 bool inlines_okay = true;
292 bool append = false;
293 size_t num_matches = executable_ptr->FindFunctions(
294 main_name, NULL, lldb::eFunctionNameTypeBase, inlines_okay,
295 symbols_okay, append, sc_list);
296 for (size_t idx = 0; idx < num_matches; idx++) {
297 SymbolContext sc;
298 sc_list.GetContextAtIndex(idx, sc);
299 if (sc.function) {
300 lldb_private::LineEntry line_entry;
301 if (sc.function->GetAddressRange()
302 .GetBaseAddress()
303 .CalculateSymbolContextLineEntry(line_entry)) {
304 SetDefaultFileAndLine(line_entry.file, line_entry.line);
305 file_spec = m_last_file_sp->GetFileSpec();
306 line = m_last_line;
307 return true;
308 }
309 }
310 }
311 }
Jim Inghame37d6052011-09-13 00:29:56 +0000312 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 }
314 return false;
Jim Inghame37d6052011-09-13 00:29:56 +0000315}
316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
318 RegularExpression &regex,
319 uint32_t start_line,
320 uint32_t end_line,
321 std::vector<uint32_t> &match_lines) {
322 match_lines.clear();
323 FileSP file_sp = GetFile(file_spec);
324 if (!file_sp)
325 return;
326 return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
327 match_lines);
Jim Inghame37d6052011-09-13 00:29:56 +0000328}
329
Todd Fiala9666ba72016-09-21 20:13:14 +0000330SourceManager::File::File(const FileSpec &file_spec,
331 lldb::DebuggerSP debugger_sp)
332 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
333 m_mod_time(file_spec.GetModificationTime()), m_source_map_mod_id(0),
334 m_data_sp(), m_offsets(), m_debugger_wp(debugger_sp) {
335 CommonInitializer(file_spec, nullptr);
336}
337
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338SourceManager::File::File(const FileSpec &file_spec, Target *target)
339 : m_file_spec_orig(file_spec), m_file_spec(file_spec),
340 m_mod_time(file_spec.GetModificationTime()), m_source_map_mod_id(0),
Todd Fiala9666ba72016-09-21 20:13:14 +0000341 m_data_sp(), m_offsets(),
342 m_debugger_wp(target ? target->GetDebugger().shared_from_this()
343 : DebuggerSP()) {
344 CommonInitializer(file_spec, target);
345}
346
347SourceManager::File::~File() {}
348
349void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
350 Target *target) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 if (!m_mod_time.IsValid()) {
352 if (target) {
353 m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
354
355 if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
356 // If this is just a file name, lets see if we can find it in the
357 // target:
358 bool check_inlines = false;
359 SymbolContextList sc_list;
360 size_t num_matches =
361 target->GetImages().ResolveSymbolContextForFilePath(
362 file_spec.GetFilename().AsCString(), 0, check_inlines,
363 lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit,
364 sc_list);
365 bool got_multiple = false;
366 if (num_matches != 0) {
367 if (num_matches > 1) {
368 SymbolContext sc;
369 FileSpec *test_cu_spec = NULL;
370
371 for (unsigned i = 0; i < num_matches; i++) {
372 sc_list.GetContextAtIndex(i, sc);
373 if (sc.comp_unit) {
374 if (test_cu_spec) {
375 if (test_cu_spec != static_cast<FileSpec *>(sc.comp_unit))
376 got_multiple = true;
377 break;
378 } else
379 test_cu_spec = sc.comp_unit;
380 }
381 }
382 }
383 if (!got_multiple) {
384 SymbolContext sc;
385 sc_list.GetContextAtIndex(0, sc);
386 m_file_spec = sc.comp_unit;
387 m_mod_time = m_file_spec.GetModificationTime();
388 }
389 }
390 }
391 // Try remapping if m_file_spec does not correspond to an existing file.
392 if (!m_file_spec.Exists()) {
393 FileSpec new_file_spec;
394 // Check target specific source remappings first, then fall back to
395 // modules objects can have individual path remappings that were
396 // detected
397 // when the debug info for a module was found.
398 // then
399 if (target->GetSourcePathMap().FindFile(m_file_spec, new_file_spec) ||
400 target->GetImages().FindSourceFile(m_file_spec, new_file_spec)) {
401 m_file_spec = new_file_spec;
402 m_mod_time = m_file_spec.GetModificationTime();
403 }
404 }
405 }
406 }
407
408 if (m_mod_time.IsValid())
409 m_data_sp = m_file_spec.ReadFileContents();
410}
411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
413 if (line == 0)
414 return UINT32_MAX;
415
416 if (line == 1)
417 return 0;
418
419 if (CalculateLineOffsets(line)) {
420 if (line < m_offsets.size())
421 return m_offsets[line - 1]; // yes we want "line - 1" in the index
422 }
423 return UINT32_MAX;
424}
425
426uint32_t SourceManager::File::GetNumLines() {
427 CalculateLineOffsets();
428 return m_offsets.size();
429}
430
431const char *SourceManager::File::PeekLineData(uint32_t line) {
432 if (!LineIsValid(line))
433 return NULL;
434
435 size_t line_offset = GetLineOffset(line);
436 if (line_offset < m_data_sp->GetByteSize())
437 return (const char *)m_data_sp->GetBytes() + line_offset;
438 return NULL;
439}
440
441uint32_t SourceManager::File::GetLineLength(uint32_t line,
442 bool include_newline_chars) {
443 if (!LineIsValid(line))
444 return false;
445
446 size_t start_offset = GetLineOffset(line);
447 size_t end_offset = GetLineOffset(line + 1);
448 if (end_offset == UINT32_MAX)
449 end_offset = m_data_sp->GetByteSize();
450
451 if (end_offset > start_offset) {
452 uint32_t length = end_offset - start_offset;
453 if (include_newline_chars == false) {
454 const char *line_start =
455 (const char *)m_data_sp->GetBytes() + start_offset;
456 while (length > 0) {
457 const char last_char = line_start[length - 1];
458 if ((last_char == '\r') || (last_char == '\n'))
459 --length;
460 else
461 break;
462 }
463 }
464 return length;
465 }
466 return 0;
467}
468
469bool SourceManager::File::LineIsValid(uint32_t line) {
470 if (line == 0)
471 return false;
472
473 if (CalculateLineOffsets(line))
474 return line < m_offsets.size();
475 return false;
476}
477
478void SourceManager::File::UpdateIfNeeded() {
479 // TODO: use host API to sign up for file modifications to anything in our
480 // source cache and only update when we determine a file has been updated.
481 // For now we check each time we want to display info for the file.
482 TimeValue curr_mod_time(m_file_spec.GetModificationTime());
483
484 if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time) {
485 m_mod_time = curr_mod_time;
486 m_data_sp = m_file_spec.ReadFileContents();
487 m_offsets.clear();
488 }
489}
490
Todd Fiala9666ba72016-09-21 20:13:14 +0000491size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 uint32_t context_before,
493 uint32_t context_after,
494 Stream *s) {
Todd Fiala9666ba72016-09-21 20:13:14 +0000495 // Nothing to write if there's no stream.
496 if (!s)
497 return 0;
498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 // Sanity check m_data_sp before proceeding.
500 if (!m_data_sp)
501 return 0;
502
503 const uint32_t start_line =
504 line <= context_before ? 1 : line - context_before;
505 const uint32_t start_line_offset = GetLineOffset(start_line);
506 if (start_line_offset != UINT32_MAX) {
507 const uint32_t end_line = line + context_after;
508 uint32_t end_line_offset = GetLineOffset(end_line + 1);
509 if (end_line_offset == UINT32_MAX)
510 end_line_offset = m_data_sp->GetByteSize();
511
512 assert(start_line_offset <= end_line_offset);
513 size_t bytes_written = 0;
514 if (start_line_offset < end_line_offset) {
515 size_t count = end_line_offset - start_line_offset;
516 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
Todd Fiala9666ba72016-09-21 20:13:14 +0000517
518 bool displayed_line = false;
519
520 if (column && (column < count)) {
521 auto debugger_sp = m_debugger_wp.lock();
522 if (should_show_stop_column_with_ansi(debugger_sp) && debugger_sp) {
523 // Check if we have any ANSI codes with which to mark this column.
524 // If not, no need to do this work.
525 auto ansi_prefix_entry = debugger_sp->GetStopShowColumnAnsiPrefix();
526 auto ansi_suffix_entry = debugger_sp->GetStopShowColumnAnsiSuffix();
527
528 // We only bother breaking up the line to format the marked column if
529 // there is any marking specified on both sides of the marked column.
530 // In ANSI-terminal-sequence land, there must be a post if there is a
531 // pre format, and vice versa.
532 if (ansi_prefix_entry && ansi_suffix_entry) {
533 // Mark the current column with the desired escape sequence for
534 // formatting the column (e.g. underline, inverse, etc.)
535
536 // First print the part before the column to mark.
537 bytes_written = s->Write(cstr, column - 1);
538
539 // Write the pre escape sequence.
540 const SymbolContext *sc = nullptr;
541 const ExecutionContext *exe_ctx = nullptr;
542 const Address addr = LLDB_INVALID_ADDRESS;
543 ValueObject *valobj = nullptr;
544 const bool function_changed = false;
545 const bool initial_function = false;
546
547 FormatEntity::Format(*ansi_prefix_entry, *s, sc, exe_ctx, &addr,
548 valobj, function_changed, initial_function);
549
550 // Write the marked column.
551 bytes_written += s->Write(cstr + column - 1, 1);
552
553 // Write the post escape sequence.
554 FormatEntity::Format(*ansi_suffix_entry, *s, sc, exe_ctx, &addr,
555 valobj, function_changed, initial_function);
556
557 // And finish up with the rest of the line.
558 bytes_written += s->Write(cstr + column, count - column);
559
560 // Keep track of the fact that we just wrote the line.
561 displayed_line = true;
562 }
563 }
564 }
565
566 // If we didn't end up displaying the line with ANSI codes for whatever
567 // reason, display it now sans codes.
568 if (!displayed_line)
569 bytes_written = s->Write(cstr, count);
570
571 // Ensure we get an end of line character one way or another.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572 if (!is_newline_char(cstr[count - 1]))
573 bytes_written += s->EOL();
574 }
575 return bytes_written;
576 }
577 return 0;
578}
579
580void SourceManager::File::FindLinesMatchingRegex(
581 RegularExpression &regex, uint32_t start_line, uint32_t end_line,
582 std::vector<uint32_t> &match_lines) {
583 match_lines.clear();
584
585 if (!LineIsValid(start_line) ||
586 (end_line != UINT32_MAX && !LineIsValid(end_line)))
587 return;
588 if (start_line > end_line)
589 return;
590
591 for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
592 std::string buffer;
593 if (!GetLine(line_no, buffer))
594 break;
Zachary Turner95eae422016-09-21 16:01:28 +0000595 if (regex.Execute(buffer)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 match_lines.push_back(line_no);
597 }
598 }
599}
600
601bool SourceManager::File::FileSpecMatches(const FileSpec &file_spec) {
602 return FileSpec::Equal(m_file_spec, file_spec, false);
603}
604
605bool lldb_private::operator==(const SourceManager::File &lhs,
606 const SourceManager::File &rhs) {
607 if (lhs.m_file_spec == rhs.m_file_spec) {
608 if (lhs.m_mod_time.IsValid()) {
609 if (rhs.m_mod_time.IsValid())
610 return lhs.m_mod_time == rhs.m_mod_time;
611 else
612 return false;
613 } else if (rhs.m_mod_time.IsValid())
614 return false;
615 else
616 return true;
617 } else
618 return false;
619}
620
621bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
622 line =
623 UINT32_MAX; // TODO: take this line out when we support partial indexing
624 if (line == UINT32_MAX) {
625 // Already done?
626 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
627 return true;
628
629 if (m_offsets.empty()) {
630 if (m_data_sp.get() == NULL)
631 return false;
632
633 const char *start = (char *)m_data_sp->GetBytes();
634 if (start) {
635 const char *end = start + m_data_sp->GetByteSize();
636
637 // Calculate all line offsets from scratch
638
639 // Push a 1 at index zero to indicate the file has been completely
640 // indexed.
641 m_offsets.push_back(UINT32_MAX);
642 const char *s;
643 for (s = start; s < end; ++s) {
644 char curr_ch = *s;
645 if (is_newline_char(curr_ch)) {
646 if (s + 1 < end) {
647 char next_ch = s[1];
648 if (is_newline_char(next_ch)) {
649 if (curr_ch != next_ch)
650 ++s;
651 }
652 }
653 m_offsets.push_back(s + 1 - start);
654 }
655 }
656 if (!m_offsets.empty()) {
657 if (m_offsets.back() < end - start)
658 m_offsets.push_back(end - start);
659 }
660 return true;
661 }
662 } else {
663 // Some lines have been populated, start where we last left off
664 assert("Not implemented yet" && false);
665 }
666
667 } else {
668 // Calculate all line offsets up to "line"
669 assert("Not implemented yet" && false);
670 }
671 return false;
672}
673
674bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
675 if (!LineIsValid(line_no))
676 return false;
677
678 size_t start_offset = GetLineOffset(line_no);
679 size_t end_offset = GetLineOffset(line_no + 1);
680 if (end_offset == UINT32_MAX) {
681 end_offset = m_data_sp->GetByteSize();
682 }
683 buffer.assign((char *)m_data_sp->GetBytes() + start_offset,
684 end_offset - start_offset);
685
686 return true;
687}
688
689void SourceManager::SourceFileCache::AddSourceFile(const FileSP &file_sp) {
690 FileSpec file_spec;
691 FileCache::iterator pos = m_file_cache.find(file_spec);
692 if (pos == m_file_cache.end())
693 m_file_cache[file_spec] = file_sp;
694 else {
695 if (file_sp != pos->second)
696 m_file_cache[file_spec] = file_sp;
697 }
698}
699
700SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
701 const FileSpec &file_spec) const {
702 FileSP file_sp;
703 FileCache::const_iterator pos = m_file_cache.find(file_spec);
704 if (pos != m_file_cache.end())
705 file_sp = pos->second;
706 return file_sp;
707}