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