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