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