xref: /llvm-project/lldb/source/Core/SourceManager.cpp (revision 1408bf7231c22bce70fea5470a865955ee40399b)
1 //===-- 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"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/RegularExpression.h"
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Host/FileSystem.h"
22 #include "lldb/Symbol/CompileUnit.h"
23 #include "lldb/Symbol/Function.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Utility/AnsiTerminal.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
32 
33 //----------------------------------------------------------------------
34 // SourceManager constructor
35 //----------------------------------------------------------------------
36 SourceManager::SourceManager(const TargetSP &target_sp)
37     : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
38       m_target_wp(target_sp),
39       m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
40 
41 SourceManager::SourceManager(const DebuggerSP &debugger_sp)
42     : m_last_file_sp(), m_last_line(0), m_last_count(0), m_default_set(false),
43       m_target_wp(), m_debugger_wp(debugger_sp) {}
44 
45 //----------------------------------------------------------------------
46 // Destructor
47 //----------------------------------------------------------------------
48 SourceManager::~SourceManager() {}
49 
50 SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
51   bool same_as_previous =
52       m_last_file_sp && m_last_file_sp->FileSpecMatches(file_spec);
53 
54   DebuggerSP debugger_sp(m_debugger_wp.lock());
55   FileSP file_sp;
56   if (same_as_previous)
57     file_sp = m_last_file_sp;
58   else if (debugger_sp)
59     file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
60 
61   TargetSP target_sp(m_target_wp.lock());
62 
63   // It the target source path map has been updated, get this file again so we
64   // can successfully remap the source file
65   if (target_sp && file_sp &&
66       file_sp->GetSourceMapModificationID() !=
67           target_sp->GetSourcePathMap().GetModificationID())
68     file_sp.reset();
69 
70   // Update the file contents if needed if we found a file
71   if (file_sp)
72     file_sp->UpdateIfNeeded();
73 
74   // If file_sp is no good or it points to a non-existent file, reset it.
75   if (!file_sp || !file_sp->GetFileSpec().Exists()) {
76     if (target_sp)
77       file_sp.reset(new File(file_spec, target_sp.get()));
78     else
79       file_sp.reset(new File(file_spec, debugger_sp));
80 
81     if (debugger_sp)
82       debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
83   }
84   return file_sp;
85 }
86 
87 static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
88   // We don't use ANSI stop column formatting if we can't lookup values from
89   // the debugger.
90   if (!debugger_sp)
91     return false;
92 
93   // We don't use ANSI stop column formatting if the debugger doesn't think
94   // it should be using color.
95   if (!debugger_sp->GetUseColor())
96     return false;
97 
98   // We only use ANSI stop column formatting if we're either supposed to show
99   // ANSI where available (which we know we have when we get to this point), or
100   // if we're only supposed to use ANSI.
101   const auto value = debugger_sp->GetStopShowColumn();
102   return ((value == eStopShowColumnAnsiOrCaret) ||
103           (value == eStopShowColumnAnsi));
104 }
105 
106 static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
107   // We don't use text-based stop column formatting if we can't lookup values
108   // from the debugger.
109   if (!debugger_sp)
110     return false;
111 
112   // If we're asked to show the first available of ANSI or caret, then
113   // we do show the caret when ANSI is not available.
114   const auto value = debugger_sp->GetStopShowColumn();
115   if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
116     return true;
117 
118   // The only other time we use caret is if we're explicitly asked to show
119   // caret.
120   return value == eStopShowColumnCaret;
121 }
122 
123 size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
124     uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
125     const char *current_line_cstr, Stream *s,
126     const SymbolContextList *bp_locs) {
127   if (count == 0)
128     return 0;
129   size_t return_value = 0;
130   if (start_line == 0) {
131     if (m_last_line != 0 && m_last_line != UINT32_MAX)
132       start_line = m_last_line + m_last_count;
133     else
134       start_line = 1;
135   }
136 
137   if (!m_default_set) {
138     FileSpec tmp_spec;
139     uint32_t tmp_line;
140     GetDefaultFileAndLine(tmp_spec, tmp_line);
141   }
142 
143   m_last_line = start_line;
144   m_last_count = count;
145 
146   if (m_last_file_sp.get()) {
147     const uint32_t end_line = start_line + count - 1;
148     for (uint32_t line = start_line; line <= end_line; ++line) {
149       if (!m_last_file_sp->LineIsValid(line)) {
150         m_last_line = UINT32_MAX;
151         break;
152       }
153 
154       char prefix[32] = "";
155       if (bp_locs) {
156         uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
157 
158         if (bp_count > 0)
159           ::snprintf(prefix, sizeof(prefix), "[%u] ", bp_count);
160         else
161           ::snprintf(prefix, sizeof(prefix), "    ");
162       }
163 
164       return_value +=
165           s->Printf("%s%2.2s %-4u\t", prefix,
166                     line == curr_line ? current_line_cstr : "", line);
167       size_t this_line_size = m_last_file_sp->DisplaySourceLines(
168           line, line == curr_line ? column : 0, 0, 0, s);
169       if (column != 0 && line == curr_line &&
170           should_show_stop_column_with_caret(m_debugger_wp.lock())) {
171         // Display caret cursor.
172         std::string src_line;
173         m_last_file_sp->GetLine(line, src_line);
174         return_value += s->Printf("    \t");
175         // Insert a space for every non-tab character in the source line.
176         for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
177           return_value += s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
178         // Now add the caret.
179         return_value += s->Printf("^\n");
180       }
181       if (this_line_size == 0) {
182         m_last_line = UINT32_MAX;
183         break;
184       } else
185         return_value += this_line_size;
186     }
187   }
188   return return_value;
189 }
190 
191 size_t SourceManager::DisplaySourceLinesWithLineNumbers(
192     const FileSpec &file_spec, uint32_t line, uint32_t column,
193     uint32_t context_before, uint32_t context_after,
194     const char *current_line_cstr, Stream *s,
195     const SymbolContextList *bp_locs) {
196   FileSP file_sp(GetFile(file_spec));
197 
198   uint32_t start_line;
199   uint32_t count = context_before + context_after + 1;
200   if (line > context_before)
201     start_line = line - context_before;
202   else
203     start_line = 1;
204 
205   if (m_last_file_sp.get() != file_sp.get()) {
206     if (line == 0)
207       m_last_line = 0;
208     m_last_file_sp = file_sp;
209   }
210   return DisplaySourceLinesWithLineNumbersUsingLastFile(
211       start_line, count, line, column, current_line_cstr, s, bp_locs);
212 }
213 
214 size_t SourceManager::DisplayMoreWithLineNumbers(
215     Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
216   // If we get called before anybody has set a default file and line, then try
217   // to figure it out here.
218   const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
219   if (!m_default_set) {
220     FileSpec tmp_spec;
221     uint32_t tmp_line;
222     GetDefaultFileAndLine(tmp_spec, tmp_line);
223   }
224 
225   if (m_last_file_sp) {
226     if (m_last_line == UINT32_MAX)
227       return 0;
228 
229     if (reverse && m_last_line == 1)
230       return 0;
231 
232     if (count > 0)
233       m_last_count = count;
234     else if (m_last_count == 0)
235       m_last_count = 10;
236 
237     if (m_last_line > 0) {
238       if (reverse) {
239         // If this is the first time we've done a reverse, then back up one more
240         // time so we end
241         // up showing the chunk before the last one we've shown:
242         if (m_last_line > m_last_count)
243           m_last_line -= m_last_count;
244         else
245           m_last_line = 1;
246       } else if (have_default_file_line)
247         m_last_line += m_last_count;
248     } else
249       m_last_line = 1;
250 
251     const uint32_t column = 0;
252     return DisplaySourceLinesWithLineNumbersUsingLastFile(
253         m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
254   }
255   return 0;
256 }
257 
258 bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
259                                           uint32_t line) {
260   FileSP old_file_sp = m_last_file_sp;
261   m_last_file_sp = GetFile(file_spec);
262 
263   m_default_set = true;
264   if (m_last_file_sp) {
265     m_last_line = line;
266     return true;
267   } else {
268     m_last_file_sp = old_file_sp;
269     return false;
270   }
271 }
272 
273 bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
274   if (m_last_file_sp) {
275     file_spec = m_last_file_sp->GetFileSpec();
276     line = m_last_line;
277     return true;
278   } else if (!m_default_set) {
279     TargetSP target_sp(m_target_wp.lock());
280 
281     if (target_sp) {
282       // If nobody has set the default file and line then try here.  If there's
283       // no executable, then we
284       // will try again later when there is one.  Otherwise, if we can't find it
285       // we won't look again,
286       // somebody will have to set it (for instance when we stop somewhere...)
287       Module *executable_ptr = target_sp->GetExecutableModulePointer();
288       if (executable_ptr) {
289         SymbolContextList sc_list;
290         ConstString main_name("main");
291         bool symbols_okay = false; // Force it to be a debug symbol.
292         bool inlines_okay = true;
293         bool append = false;
294         size_t num_matches = executable_ptr->FindFunctions(
295             main_name, NULL, lldb::eFunctionNameTypeBase, inlines_okay,
296             symbols_okay, append, sc_list);
297         for (size_t idx = 0; idx < num_matches; idx++) {
298           SymbolContext sc;
299           sc_list.GetContextAtIndex(idx, sc);
300           if (sc.function) {
301             lldb_private::LineEntry line_entry;
302             if (sc.function->GetAddressRange()
303                     .GetBaseAddress()
304                     .CalculateSymbolContextLineEntry(line_entry)) {
305               SetDefaultFileAndLine(line_entry.file, line_entry.line);
306               file_spec = m_last_file_sp->GetFileSpec();
307               line = m_last_line;
308               return true;
309             }
310           }
311         }
312       }
313     }
314   }
315   return false;
316 }
317 
318 void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
319                                            RegularExpression &regex,
320                                            uint32_t start_line,
321                                            uint32_t end_line,
322                                            std::vector<uint32_t> &match_lines) {
323   match_lines.clear();
324   FileSP file_sp = GetFile(file_spec);
325   if (!file_sp)
326     return;
327   return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
328                                          match_lines);
329 }
330 
331 SourceManager::File::File(const FileSpec &file_spec,
332                           lldb::DebuggerSP debugger_sp)
333     : m_file_spec_orig(file_spec), m_file_spec(file_spec),
334       m_mod_time(FileSystem::GetModificationTime(file_spec)),
335       m_debugger_wp(debugger_sp) {
336   CommonInitializer(file_spec, nullptr);
337 }
338 
339 SourceManager::File::File(const FileSpec &file_spec, Target *target)
340     : m_file_spec_orig(file_spec), m_file_spec(file_spec),
341       m_mod_time(FileSystem::GetModificationTime(file_spec)),
342       m_debugger_wp(target ? target->GetDebugger().shared_from_this()
343                            : DebuggerSP()) {
344   CommonInitializer(file_spec, target);
345 }
346 
347 void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
348                                             Target *target) {
349   if (!m_mod_time.IsValid()) {
350     if (target) {
351       m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
352 
353       if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
354         // If this is just a file name, lets see if we can find it in the
355         // target:
356         bool check_inlines = false;
357         SymbolContextList sc_list;
358         size_t num_matches =
359             target->GetImages().ResolveSymbolContextForFilePath(
360                 file_spec.GetFilename().AsCString(), 0, check_inlines,
361                 lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit,
362                 sc_list);
363         bool got_multiple = false;
364         if (num_matches != 0) {
365           if (num_matches > 1) {
366             SymbolContext sc;
367             FileSpec *test_cu_spec = NULL;
368 
369             for (unsigned i = 0; i < num_matches; i++) {
370               sc_list.GetContextAtIndex(i, sc);
371               if (sc.comp_unit) {
372                 if (test_cu_spec) {
373                   if (test_cu_spec != static_cast<FileSpec *>(sc.comp_unit))
374                     got_multiple = true;
375                   break;
376                 } else
377                   test_cu_spec = sc.comp_unit;
378               }
379             }
380           }
381           if (!got_multiple) {
382             SymbolContext sc;
383             sc_list.GetContextAtIndex(0, sc);
384             m_file_spec = sc.comp_unit;
385             m_mod_time = FileSystem::GetModificationTime(m_file_spec);
386           }
387         }
388       }
389       // Try remapping if m_file_spec does not correspond to an existing file.
390       if (!m_file_spec.Exists()) {
391         FileSpec new_file_spec;
392         // Check target specific source remappings first, then fall back to
393         // modules objects can have individual path remappings that were
394         // detected
395         // when the debug info for a module was found.
396         // then
397         if (target->GetSourcePathMap().FindFile(m_file_spec, new_file_spec) ||
398             target->GetImages().FindSourceFile(m_file_spec, new_file_spec)) {
399           m_file_spec = new_file_spec;
400           m_mod_time = FileSystem::GetModificationTime(m_file_spec);
401         }
402       }
403     }
404   }
405 
406   if (m_mod_time.IsValid())
407     m_data_sp = m_file_spec.ReadFileContents();
408 }
409 
410 uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
411   if (line == 0)
412     return UINT32_MAX;
413 
414   if (line == 1)
415     return 0;
416 
417   if (CalculateLineOffsets(line)) {
418     if (line < m_offsets.size())
419       return m_offsets[line - 1]; // yes we want "line - 1" in the index
420   }
421   return UINT32_MAX;
422 }
423 
424 uint32_t SourceManager::File::GetNumLines() {
425   CalculateLineOffsets();
426   return m_offsets.size();
427 }
428 
429 const char *SourceManager::File::PeekLineData(uint32_t line) {
430   if (!LineIsValid(line))
431     return NULL;
432 
433   size_t line_offset = GetLineOffset(line);
434   if (line_offset < m_data_sp->GetByteSize())
435     return (const char *)m_data_sp->GetBytes() + line_offset;
436   return NULL;
437 }
438 
439 uint32_t SourceManager::File::GetLineLength(uint32_t line,
440                                             bool include_newline_chars) {
441   if (!LineIsValid(line))
442     return false;
443 
444   size_t start_offset = GetLineOffset(line);
445   size_t end_offset = GetLineOffset(line + 1);
446   if (end_offset == UINT32_MAX)
447     end_offset = m_data_sp->GetByteSize();
448 
449   if (end_offset > start_offset) {
450     uint32_t length = end_offset - start_offset;
451     if (include_newline_chars == false) {
452       const char *line_start =
453           (const char *)m_data_sp->GetBytes() + start_offset;
454       while (length > 0) {
455         const char last_char = line_start[length - 1];
456         if ((last_char == '\r') || (last_char == '\n'))
457           --length;
458         else
459           break;
460       }
461     }
462     return length;
463   }
464   return 0;
465 }
466 
467 bool SourceManager::File::LineIsValid(uint32_t line) {
468   if (line == 0)
469     return false;
470 
471   if (CalculateLineOffsets(line))
472     return line < m_offsets.size();
473   return false;
474 }
475 
476 void SourceManager::File::UpdateIfNeeded() {
477   // TODO: use host API to sign up for file modifications to anything in our
478   // source cache and only update when we determine a file has been updated.
479   // For now we check each time we want to display info for the file.
480   TimeValue curr_mod_time(FileSystem::GetModificationTime(m_file_spec));
481 
482   if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time) {
483     m_mod_time = curr_mod_time;
484     m_data_sp = m_file_spec.ReadFileContents();
485     m_offsets.clear();
486   }
487 }
488 
489 size_t SourceManager::File::DisplaySourceLines(uint32_t line, uint32_t column,
490                                                uint32_t context_before,
491                                                uint32_t context_after,
492                                                Stream *s) {
493   // Nothing to write if there's no stream.
494   if (!s)
495     return 0;
496 
497   // Sanity check m_data_sp before proceeding.
498   if (!m_data_sp)
499     return 0;
500 
501   const uint32_t start_line =
502       line <= context_before ? 1 : line - context_before;
503   const uint32_t start_line_offset = GetLineOffset(start_line);
504   if (start_line_offset != UINT32_MAX) {
505     const uint32_t end_line = line + context_after;
506     uint32_t end_line_offset = GetLineOffset(end_line + 1);
507     if (end_line_offset == UINT32_MAX)
508       end_line_offset = m_data_sp->GetByteSize();
509 
510     assert(start_line_offset <= end_line_offset);
511     size_t bytes_written = 0;
512     if (start_line_offset < end_line_offset) {
513       size_t count = end_line_offset - start_line_offset;
514       const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
515 
516       bool displayed_line = false;
517 
518       if (column && (column < count)) {
519         auto debugger_sp = m_debugger_wp.lock();
520         if (should_show_stop_column_with_ansi(debugger_sp) && debugger_sp) {
521           // Check if we have any ANSI codes with which to mark this column.
522           // If not, no need to do this work.
523           auto ansi_prefix_entry = debugger_sp->GetStopShowColumnAnsiPrefix();
524           auto ansi_suffix_entry = debugger_sp->GetStopShowColumnAnsiSuffix();
525 
526           // We only bother breaking up the line to format the marked column if
527           // there is any marking specified on both sides of the marked column.
528           // In ANSI-terminal-sequence land, there must be a post if there is a
529           // pre format, and vice versa.
530           if (ansi_prefix_entry && ansi_suffix_entry) {
531             // Mark the current column with the desired escape sequence for
532             // formatting the column (e.g. underline, inverse, etc.)
533 
534             // First print the part before the column to mark.
535             bytes_written = s->Write(cstr, column - 1);
536 
537             // Write the pre escape sequence.
538             const SymbolContext *sc = nullptr;
539             const ExecutionContext *exe_ctx = nullptr;
540             const Address addr = LLDB_INVALID_ADDRESS;
541             ValueObject *valobj = nullptr;
542             const bool function_changed = false;
543             const bool initial_function = false;
544 
545             FormatEntity::Format(*ansi_prefix_entry, *s, sc, exe_ctx, &addr,
546                                  valobj, function_changed, initial_function);
547 
548             // Write the marked column.
549             bytes_written += s->Write(cstr + column - 1, 1);
550 
551             // Write the post escape sequence.
552             FormatEntity::Format(*ansi_suffix_entry, *s, sc, exe_ctx, &addr,
553                                  valobj, function_changed, initial_function);
554 
555             // And finish up with the rest of the line.
556             bytes_written += s->Write(cstr + column, count - column);
557 
558             // Keep track of the fact that we just wrote the line.
559             displayed_line = true;
560           }
561         }
562       }
563 
564       // If we didn't end up displaying the line with ANSI codes for whatever
565       // reason, display it now sans codes.
566       if (!displayed_line)
567         bytes_written = s->Write(cstr, count);
568 
569       // Ensure we get an end of line character one way or another.
570       if (!is_newline_char(cstr[count - 1]))
571         bytes_written += s->EOL();
572     }
573     return bytes_written;
574   }
575   return 0;
576 }
577 
578 void SourceManager::File::FindLinesMatchingRegex(
579     RegularExpression &regex, uint32_t start_line, uint32_t end_line,
580     std::vector<uint32_t> &match_lines) {
581   match_lines.clear();
582 
583   if (!LineIsValid(start_line) ||
584       (end_line != UINT32_MAX && !LineIsValid(end_line)))
585     return;
586   if (start_line > end_line)
587     return;
588 
589   for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
590     std::string buffer;
591     if (!GetLine(line_no, buffer))
592       break;
593     if (regex.Execute(buffer)) {
594       match_lines.push_back(line_no);
595     }
596   }
597 }
598 
599 bool SourceManager::File::FileSpecMatches(const FileSpec &file_spec) {
600   return FileSpec::Equal(m_file_spec, file_spec, false);
601 }
602 
603 bool lldb_private::operator==(const SourceManager::File &lhs,
604                               const SourceManager::File &rhs) {
605   if (lhs.m_file_spec == rhs.m_file_spec) {
606     if (lhs.m_mod_time.IsValid()) {
607       if (rhs.m_mod_time.IsValid())
608         return lhs.m_mod_time == rhs.m_mod_time;
609       else
610         return false;
611     } else if (rhs.m_mod_time.IsValid())
612       return false;
613     else
614       return true;
615   } else
616     return false;
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