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