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