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