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