xref: /llvm-project/lldb/source/Core/SourceManager.cpp (revision 841be9854c496adb1944fbf33af055814366ec86)
1 //===-- SourceManager.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/SourceManager.h"
10 
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/AddressRange.h"
13 #include "lldb/Core/Debugger.h"
14 #include "lldb/Core/FormatEntity.h"
15 #include "lldb/Core/Highlighter.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/ModuleList.h"
18 #include "lldb/Host/FileSystem.h"
19 #include "lldb/Symbol/CompileUnit.h"
20 #include "lldb/Symbol/Function.h"
21 #include "lldb/Symbol/LineEntry.h"
22 #include "lldb/Symbol/SymbolContext.h"
23 #include "lldb/Target/PathMappingList.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/AnsiTerminal.h"
26 #include "lldb/Utility/ConstString.h"
27 #include "lldb/Utility/DataBuffer.h"
28 #include "lldb/Utility/DataBufferLLVM.h"
29 #include "lldb/Utility/RegularExpression.h"
30 #include "lldb/Utility/Stream.h"
31 #include "lldb/lldb-enumerations.h"
32 
33 #include "llvm/ADT/Twine.h"
34 
35 #include <memory>
36 #include <utility>
37 
38 #include <assert.h>
39 #include <stdio.h>
40 
41 namespace lldb_private {
42 class ExecutionContext;
43 }
44 namespace lldb_private {
45 class ValueObject;
46 }
47 
48 using namespace lldb;
49 using namespace lldb_private;
50 
51 static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
52 
53 // SourceManager constructor
54 SourceManager::SourceManager(const TargetSP &target_sp)
55     : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
56       m_target_wp(target_sp),
57       m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
58 
59 SourceManager::SourceManager(const DebuggerSP &debugger_sp)
60     : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
61       m_target_wp(), m_debugger_wp(debugger_sp) {}
62 
63 // Destructor
64 SourceManager::~SourceManager() {}
65 
66 SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
67   bool same_as_previous =
68       m_last_file_sp &&
69       FileSpec::Match(file_spec, m_last_file_sp->GetFileSpec());
70 
71   DebuggerSP debugger_sp(m_debugger_wp.lock());
72   FileSP file_sp;
73   if (same_as_previous)
74     file_sp = m_last_file_sp;
75   else if (debugger_sp)
76     file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
77 
78   TargetSP target_sp(m_target_wp.lock());
79 
80   // It the target source path map has been updated, get this file again so we
81   // can successfully remap the source file
82   if (target_sp && file_sp &&
83       file_sp->GetSourceMapModificationID() !=
84           target_sp->GetSourcePathMap().GetModificationID())
85     file_sp.reset();
86 
87   // Update the file contents if needed if we found a file
88   if (file_sp)
89     file_sp->UpdateIfNeeded();
90 
91   // If file_sp is no good or it points to a non-existent file, reset it.
92   if (!file_sp || !FileSystem::Instance().Exists(file_sp->GetFileSpec())) {
93     if (target_sp)
94       file_sp = std::make_shared<File>(file_spec, target_sp.get());
95     else
96       file_sp = std::make_shared<File>(file_spec, debugger_sp);
97 
98     if (debugger_sp)
99       debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
100   }
101   return file_sp;
102 }
103 
104 static bool should_highlight_source(DebuggerSP debugger_sp) {
105   if (!debugger_sp)
106     return false;
107 
108   // We don't use ANSI stop column formatting if the debugger doesn't think it
109   // should be using color.
110   if (!debugger_sp->GetUseColor())
111     return false;
112 
113   return debugger_sp->GetHighlightSource();
114 }
115 
116 static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
117   // We don't use ANSI stop column formatting if we can't lookup values from
118   // the debugger.
119   if (!debugger_sp)
120     return false;
121 
122   // We don't use ANSI stop column formatting if the debugger doesn't think it
123   // should be using color.
124   if (!debugger_sp->GetUseColor())
125     return false;
126 
127   // We only use ANSI stop column formatting if we're either supposed to show
128   // ANSI where available (which we know we have when we get to this point), or
129   // if we're only supposed to use ANSI.
130   const auto value = debugger_sp->GetStopShowColumn();
131   return ((value == eStopShowColumnAnsiOrCaret) ||
132           (value == eStopShowColumnAnsi));
133 }
134 
135 static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
136   // We don't use text-based stop column formatting if we can't lookup values
137   // from the debugger.
138   if (!debugger_sp)
139     return false;
140 
141   // If we're asked to show the first available of ANSI or caret, then we do
142   // show the caret when ANSI is not available.
143   const auto value = debugger_sp->GetStopShowColumn();
144   if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
145     return true;
146 
147   // The only other time we use caret is if we're explicitly asked to show
148   // caret.
149   return value == eStopShowColumnCaret;
150 }
151 
152 static bool should_show_stop_line_with_ansi(DebuggerSP debugger_sp) {
153   return debugger_sp && debugger_sp->GetUseColor();
154 }
155 
156 size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
157     uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
158     const char *current_line_cstr, Stream *s,
159     const SymbolContextList *bp_locs) {
160   if (count == 0)
161     return 0;
162 
163   Stream::ByteDelta delta(*s);
164 
165   if (start_line == 0) {
166     if (m_last_line != 0 && m_last_line != UINT32_MAX)
167       start_line = m_last_line + m_last_count;
168     else
169       start_line = 1;
170   }
171 
172   if (!m_default_set) {
173     FileSpec tmp_spec;
174     uint32_t tmp_line;
175     GetDefaultFileAndLine(tmp_spec, tmp_line);
176   }
177 
178   m_last_line = start_line;
179   m_last_count = count;
180 
181   if (m_last_file_sp.get()) {
182     const uint32_t end_line = start_line + count - 1;
183     for (uint32_t line = start_line; line <= end_line; ++line) {
184       if (!m_last_file_sp->LineIsValid(line)) {
185         m_last_line = UINT32_MAX;
186         break;
187       }
188 
189       char prefix[32] = "";
190       if (bp_locs) {
191         uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
192 
193         if (bp_count > 0)
194           ::snprintf(prefix, sizeof(prefix), "[%u] ", bp_count);
195         else
196           ::snprintf(prefix, sizeof(prefix), "    ");
197       }
198 
199       char buffer[3];
200       sprintf(buffer, "%2.2s", (line == curr_line) ? current_line_cstr : "");
201       std::string current_line_highlight(buffer);
202 
203       auto debugger_sp = m_debugger_wp.lock();
204       if (should_show_stop_line_with_ansi(debugger_sp)) {
205         current_line_highlight = ansi::FormatAnsiTerminalCodes(
206             (debugger_sp->GetStopShowLineMarkerAnsiPrefix() +
207              current_line_highlight +
208              debugger_sp->GetStopShowLineMarkerAnsiSuffix())
209                 .str());
210       }
211 
212       s->Printf("%s%s %-4u\t", prefix, current_line_highlight.c_str(), line);
213 
214       // So far we treated column 0 as a special 'no column value', but
215       // DisplaySourceLines starts counting columns from 0 (and no column is
216       // expressed by passing an empty optional).
217       llvm::Optional<size_t> columnToHighlight;
218       if (line == curr_line && column)
219         columnToHighlight = column - 1;
220 
221       size_t this_line_size =
222           m_last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s);
223       if (column != 0 && line == curr_line &&
224           should_show_stop_column_with_caret(debugger_sp)) {
225         // Display caret cursor.
226         std::string src_line;
227         m_last_file_sp->GetLine(line, src_line);
228         s->Printf("    \t");
229         // Insert a space for every non-tab character in the source line.
230         for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
231           s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
232         // Now add the caret.
233         s->Printf("^\n");
234       }
235       if (this_line_size == 0) {
236         m_last_line = UINT32_MAX;
237         break;
238       }
239     }
240   }
241   return *delta;
242 }
243 
244 size_t SourceManager::DisplaySourceLinesWithLineNumbers(
245     const FileSpec &file_spec, uint32_t line, uint32_t column,
246     uint32_t context_before, uint32_t context_after,
247     const char *current_line_cstr, Stream *s,
248     const SymbolContextList *bp_locs) {
249   FileSP file_sp(GetFile(file_spec));
250 
251   uint32_t start_line;
252   uint32_t count = context_before + context_after + 1;
253   if (line > context_before)
254     start_line = line - context_before;
255   else
256     start_line = 1;
257 
258   if (m_last_file_sp.get() != file_sp.get()) {
259     if (line == 0)
260       m_last_line = 0;
261     m_last_file_sp = file_sp;
262   }
263   return DisplaySourceLinesWithLineNumbersUsingLastFile(
264       start_line, count, line, column, current_line_cstr, s, bp_locs);
265 }
266 
267 size_t SourceManager::DisplayMoreWithLineNumbers(
268     Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
269   // If we get called before anybody has set a default file and line, then try
270   // to figure it out here.
271   const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
272   if (!m_default_set) {
273     FileSpec tmp_spec;
274     uint32_t tmp_line;
275     GetDefaultFileAndLine(tmp_spec, tmp_line);
276   }
277 
278   if (m_last_file_sp) {
279     if (m_last_line == UINT32_MAX)
280       return 0;
281 
282     if (reverse && m_last_line == 1)
283       return 0;
284 
285     if (count > 0)
286       m_last_count = count;
287     else if (m_last_count == 0)
288       m_last_count = 10;
289 
290     if (m_last_line > 0) {
291       if (reverse) {
292         // If this is the first time we've done a reverse, then back up one
293         // more time so we end up showing the chunk before the last one we've
294         // shown:
295         if (m_last_line > m_last_count)
296           m_last_line -= m_last_count;
297         else
298           m_last_line = 1;
299       } else if (have_default_file_line)
300         m_last_line += m_last_count;
301     } else
302       m_last_line = 1;
303 
304     const uint32_t column = 0;
305     return DisplaySourceLinesWithLineNumbersUsingLastFile(
306         m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
307   }
308   return 0;
309 }
310 
311 bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
312                                           uint32_t line) {
313   FileSP old_file_sp = m_last_file_sp;
314   m_last_file_sp = GetFile(file_spec);
315 
316   m_default_set = true;
317   if (m_last_file_sp) {
318     m_last_line = line;
319     return true;
320   } else {
321     m_last_file_sp = old_file_sp;
322     return false;
323   }
324 }
325 
326 bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
327   if (m_last_file_sp) {
328     file_spec = m_last_file_sp->GetFileSpec();
329     line = m_last_line;
330     return true;
331   } else if (!m_default_set) {
332     TargetSP target_sp(m_target_wp.lock());
333 
334     if (target_sp) {
335       // If nobody has set the default file and line then try here.  If there's
336       // no executable, then we will try again later when there is one.
337       // Otherwise, if we can't find it we won't look again, somebody will have
338       // to set it (for instance when we stop somewhere...)
339       Module *executable_ptr = target_sp->GetExecutableModulePointer();
340       if (executable_ptr) {
341         SymbolContextList sc_list;
342         ConstString main_name("main");
343         bool symbols_okay = false; // Force it to be a debug symbol.
344         bool inlines_okay = true;
345         executable_ptr->FindFunctions(main_name, CompilerDeclContext(),
346                                       lldb::eFunctionNameTypeBase, inlines_okay,
347                                       symbols_okay, sc_list);
348         size_t num_matches = sc_list.GetSize();
349         for (size_t idx = 0; idx < num_matches; idx++) {
350           SymbolContext sc;
351           sc_list.GetContextAtIndex(idx, sc);
352           if (sc.function) {
353             lldb_private::LineEntry line_entry;
354             if (sc.function->GetAddressRange()
355                     .GetBaseAddress()
356                     .CalculateSymbolContextLineEntry(line_entry)) {
357               SetDefaultFileAndLine(line_entry.file, line_entry.line);
358               file_spec = m_last_file_sp->GetFileSpec();
359               line = m_last_line;
360               return true;
361             }
362           }
363         }
364       }
365     }
366   }
367   return false;
368 }
369 
370 void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
371                                            RegularExpression &regex,
372                                            uint32_t start_line,
373                                            uint32_t end_line,
374                                            std::vector<uint32_t> &match_lines) {
375   match_lines.clear();
376   FileSP file_sp = GetFile(file_spec);
377   if (!file_sp)
378     return;
379   return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
380                                          match_lines);
381 }
382 
383 SourceManager::File::File(const FileSpec &file_spec,
384                           lldb::DebuggerSP debugger_sp)
385     : m_file_spec_orig(file_spec), m_file_spec(file_spec),
386       m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
387       m_debugger_wp(debugger_sp) {
388   CommonInitializer(file_spec, nullptr);
389 }
390 
391 SourceManager::File::File(const FileSpec &file_spec, Target *target)
392     : m_file_spec_orig(file_spec), m_file_spec(file_spec),
393       m_mod_time(FileSystem::Instance().GetModificationTime(file_spec)),
394       m_debugger_wp(target ? target->GetDebugger().shared_from_this()
395                            : DebuggerSP()) {
396   CommonInitializer(file_spec, target);
397 }
398 
399 void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
400                                             Target *target) {
401   if (m_mod_time == llvm::sys::TimePoint<>()) {
402     if (target) {
403       m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
404 
405       if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
406         // If this is just a file name, lets see if we can find it in the
407         // target:
408         bool check_inlines = false;
409         SymbolContextList sc_list;
410         size_t num_matches =
411             target->GetImages().ResolveSymbolContextForFilePath(
412                 file_spec.GetFilename().AsCString(), 0, check_inlines,
413                 SymbolContextItem(eSymbolContextModule |
414                                   eSymbolContextCompUnit),
415                 sc_list);
416         bool got_multiple = false;
417         if (num_matches != 0) {
418           if (num_matches > 1) {
419             SymbolContext sc;
420             CompileUnit *test_cu = nullptr;
421 
422             for (unsigned i = 0; i < num_matches; i++) {
423               sc_list.GetContextAtIndex(i, sc);
424               if (sc.comp_unit) {
425                 if (test_cu) {
426                   if (test_cu != sc.comp_unit)
427                     got_multiple = true;
428                   break;
429                 } else
430                   test_cu = sc.comp_unit;
431               }
432             }
433           }
434           if (!got_multiple) {
435             SymbolContext sc;
436             sc_list.GetContextAtIndex(0, sc);
437             if (sc.comp_unit)
438               m_file_spec = sc.comp_unit->GetPrimaryFile();
439             m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
440           }
441         }
442       }
443       // Try remapping if m_file_spec does not correspond to an existing file.
444       if (!FileSystem::Instance().Exists(m_file_spec)) {
445         FileSpec new_file_spec;
446         // Check target specific source remappings first, then fall back to
447         // modules objects can have individual path remappings that were
448         // detected when the debug info for a module was found. then
449         if (target->GetSourcePathMap().FindFile(m_file_spec, new_file_spec) ||
450             target->GetImages().FindSourceFile(m_file_spec, new_file_spec)) {
451           m_file_spec = new_file_spec;
452           m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
453         }
454       }
455     }
456   }
457 
458   if (m_mod_time != llvm::sys::TimePoint<>())
459     m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
460 }
461 
462 uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
463   if (line == 0)
464     return UINT32_MAX;
465 
466   if (line == 1)
467     return 0;
468 
469   if (CalculateLineOffsets(line)) {
470     if (line < m_offsets.size())
471       return m_offsets[line - 1]; // yes we want "line - 1" in the index
472   }
473   return UINT32_MAX;
474 }
475 
476 uint32_t SourceManager::File::GetNumLines() {
477   CalculateLineOffsets();
478   return m_offsets.size();
479 }
480 
481 const char *SourceManager::File::PeekLineData(uint32_t line) {
482   if (!LineIsValid(line))
483     return nullptr;
484 
485   size_t line_offset = GetLineOffset(line);
486   if (line_offset < m_data_sp->GetByteSize())
487     return (const char *)m_data_sp->GetBytes() + line_offset;
488   return nullptr;
489 }
490 
491 uint32_t SourceManager::File::GetLineLength(uint32_t line,
492                                             bool include_newline_chars) {
493   if (!LineIsValid(line))
494     return false;
495 
496   size_t start_offset = GetLineOffset(line);
497   size_t end_offset = GetLineOffset(line + 1);
498   if (end_offset == UINT32_MAX)
499     end_offset = m_data_sp->GetByteSize();
500 
501   if (end_offset > start_offset) {
502     uint32_t length = end_offset - start_offset;
503     if (!include_newline_chars) {
504       const char *line_start =
505           (const char *)m_data_sp->GetBytes() + start_offset;
506       while (length > 0) {
507         const char last_char = line_start[length - 1];
508         if ((last_char == '\r') || (last_char == '\n'))
509           --length;
510         else
511           break;
512       }
513     }
514     return length;
515   }
516   return 0;
517 }
518 
519 bool SourceManager::File::LineIsValid(uint32_t line) {
520   if (line == 0)
521     return false;
522 
523   if (CalculateLineOffsets(line))
524     return line < m_offsets.size();
525   return false;
526 }
527 
528 void SourceManager::File::UpdateIfNeeded() {
529   // TODO: use host API to sign up for file modifications to anything in our
530   // source cache and only update when we determine a file has been updated.
531   // For now we check each time we want to display info for the file.
532   auto curr_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
533 
534   if (curr_mod_time != llvm::sys::TimePoint<>() &&
535       m_mod_time != curr_mod_time) {
536     m_mod_time = curr_mod_time;
537     m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
538     m_offsets.clear();
539   }
540 }
541 
542 size_t SourceManager::File::DisplaySourceLines(uint32_t line,
543                                                llvm::Optional<size_t> column,
544                                                uint32_t context_before,
545                                                uint32_t context_after,
546                                                Stream *s) {
547   // Nothing to write if there's no stream.
548   if (!s)
549     return 0;
550 
551   // Sanity check m_data_sp before proceeding.
552   if (!m_data_sp)
553     return 0;
554 
555   size_t bytes_written = s->GetWrittenBytes();
556 
557   auto debugger_sp = m_debugger_wp.lock();
558 
559   HighlightStyle style;
560   // Use the default Vim style if source highlighting is enabled.
561   if (should_highlight_source(debugger_sp))
562     style = HighlightStyle::MakeVimStyle();
563 
564   // If we should mark the stop column with color codes, then copy the prefix
565   // and suffix to our color style.
566   if (should_show_stop_column_with_ansi(debugger_sp))
567     style.selected.Set(debugger_sp->GetStopShowColumnAnsiPrefix(),
568                        debugger_sp->GetStopShowColumnAnsiSuffix());
569 
570   HighlighterManager mgr;
571   std::string path = GetFileSpec().GetPath(/*denormalize*/ false);
572   // FIXME: Find a way to get the definitive language this file was written in
573   // and pass it to the highlighter.
574   const auto &h = mgr.getHighlighterFor(lldb::eLanguageTypeUnknown, path);
575 
576   const uint32_t start_line =
577       line <= context_before ? 1 : line - context_before;
578   const uint32_t start_line_offset = GetLineOffset(start_line);
579   if (start_line_offset != UINT32_MAX) {
580     const uint32_t end_line = line + context_after;
581     uint32_t end_line_offset = GetLineOffset(end_line + 1);
582     if (end_line_offset == UINT32_MAX)
583       end_line_offset = m_data_sp->GetByteSize();
584 
585     assert(start_line_offset <= end_line_offset);
586     if (start_line_offset < end_line_offset) {
587       size_t count = end_line_offset - start_line_offset;
588       const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
589 
590       auto ref = llvm::StringRef(reinterpret_cast<const char *>(cstr), count);
591 
592       h.Highlight(style, ref, column, "", *s);
593 
594       // Ensure we get an end of line character one way or another.
595       if (!is_newline_char(ref.back()))
596         s->EOL();
597     }
598   }
599   return s->GetWrittenBytes() - bytes_written;
600 }
601 
602 void SourceManager::File::FindLinesMatchingRegex(
603     RegularExpression &regex, uint32_t start_line, uint32_t end_line,
604     std::vector<uint32_t> &match_lines) {
605   match_lines.clear();
606 
607   if (!LineIsValid(start_line) ||
608       (end_line != UINT32_MAX && !LineIsValid(end_line)))
609     return;
610   if (start_line > end_line)
611     return;
612 
613   for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
614     std::string buffer;
615     if (!GetLine(line_no, buffer))
616       break;
617     if (regex.Execute(buffer)) {
618       match_lines.push_back(line_no);
619     }
620   }
621 }
622 
623 bool lldb_private::operator==(const SourceManager::File &lhs,
624                               const SourceManager::File &rhs) {
625   if (lhs.m_file_spec != rhs.m_file_spec)
626     return false;
627   return lhs.m_mod_time == rhs.m_mod_time;
628 }
629 
630 bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
631   line =
632       UINT32_MAX; // TODO: take this line out when we support partial indexing
633   if (line == UINT32_MAX) {
634     // Already done?
635     if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
636       return true;
637 
638     if (m_offsets.empty()) {
639       if (m_data_sp.get() == nullptr)
640         return false;
641 
642       const char *start = (char *)m_data_sp->GetBytes();
643       if (start) {
644         const char *end = start + m_data_sp->GetByteSize();
645 
646         // Calculate all line offsets from scratch
647 
648         // Push a 1 at index zero to indicate the file has been completely
649         // indexed.
650         m_offsets.push_back(UINT32_MAX);
651         const char *s;
652         for (s = start; s < end; ++s) {
653           char curr_ch = *s;
654           if (is_newline_char(curr_ch)) {
655             if (s + 1 < end) {
656               char next_ch = s[1];
657               if (is_newline_char(next_ch)) {
658                 if (curr_ch != next_ch)
659                   ++s;
660               }
661             }
662             m_offsets.push_back(s + 1 - start);
663           }
664         }
665         if (!m_offsets.empty()) {
666           if (m_offsets.back() < size_t(end - start))
667             m_offsets.push_back(end - start);
668         }
669         return true;
670       }
671     } else {
672       // Some lines have been populated, start where we last left off
673       assert("Not implemented yet" && false);
674     }
675 
676   } else {
677     // Calculate all line offsets up to "line"
678     assert("Not implemented yet" && false);
679   }
680   return false;
681 }
682 
683 bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
684   if (!LineIsValid(line_no))
685     return false;
686 
687   size_t start_offset = GetLineOffset(line_no);
688   size_t end_offset = GetLineOffset(line_no + 1);
689   if (end_offset == UINT32_MAX) {
690     end_offset = m_data_sp->GetByteSize();
691   }
692   buffer.assign((char *)m_data_sp->GetBytes() + start_offset,
693                 end_offset - start_offset);
694 
695   return true;
696 }
697 
698 void SourceManager::SourceFileCache::AddSourceFile(const FileSP &file_sp) {
699   FileSpec file_spec;
700   FileCache::iterator pos = m_file_cache.find(file_spec);
701   if (pos == m_file_cache.end())
702     m_file_cache[file_spec] = file_sp;
703   else {
704     if (file_sp != pos->second)
705       m_file_cache[file_spec] = file_sp;
706   }
707 }
708 
709 SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
710     const FileSpec &file_spec) const {
711   FileSP file_sp;
712   FileCache::const_iterator pos = m_file_cache.find(file_spec);
713   if (pos != m_file_cache.end())
714     file_sp = pos->second;
715   return file_sp;
716 }
717