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