xref: /freebsd/contrib/llvm-project/lldb/source/Core/SourceManager.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15ffd83dbSDimitry Andric //===-- SourceManager.cpp -------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Core/SourceManager.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Core/Address.h"
120b57cec5SDimitry Andric #include "lldb/Core/AddressRange.h"
130b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
140b57cec5SDimitry Andric #include "lldb/Core/FormatEntity.h"
150b57cec5SDimitry Andric #include "lldb/Core/Highlighter.h"
160b57cec5SDimitry Andric #include "lldb/Core/Module.h"
170b57cec5SDimitry Andric #include "lldb/Core/ModuleList.h"
180b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h"
190b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
200b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
210b57cec5SDimitry Andric #include "lldb/Symbol/LineEntry.h"
220b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContext.h"
230b57cec5SDimitry Andric #include "lldb/Target/PathMappingList.h"
2406c3fb27SDimitry Andric #include "lldb/Target/Process.h"
250b57cec5SDimitry Andric #include "lldb/Target/Target.h"
265ffd83dbSDimitry Andric #include "lldb/Utility/AnsiTerminal.h"
270b57cec5SDimitry Andric #include "lldb/Utility/ConstString.h"
280b57cec5SDimitry Andric #include "lldb/Utility/DataBuffer.h"
2906c3fb27SDimitry Andric #include "lldb/Utility/LLDBLog.h"
3006c3fb27SDimitry Andric #include "lldb/Utility/Log.h"
310b57cec5SDimitry Andric #include "lldb/Utility/RegularExpression.h"
320b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
330b57cec5SDimitry Andric #include "lldb/lldb-enumerations.h"
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric #include <memory>
38bdd1243dSDimitry Andric #include <optional>
390b57cec5SDimitry Andric #include <utility>
400b57cec5SDimitry Andric 
41fe6060f1SDimitry Andric #include <cassert>
42fe6060f1SDimitry Andric #include <cstdio>
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric namespace lldb_private {
450b57cec5SDimitry Andric class ExecutionContext;
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric namespace lldb_private {
480b57cec5SDimitry Andric class ValueObject;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric using namespace lldb;
520b57cec5SDimitry Andric using namespace lldb_private;
530b57cec5SDimitry Andric 
is_newline_char(char ch)540b57cec5SDimitry Andric static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
550b57cec5SDimitry Andric 
resolve_tilde(FileSpec & file_spec)5681ad6265SDimitry Andric static void resolve_tilde(FileSpec &file_spec) {
5781ad6265SDimitry Andric   if (!FileSystem::Instance().Exists(file_spec) &&
5881ad6265SDimitry Andric       file_spec.GetDirectory() &&
5981ad6265SDimitry Andric       file_spec.GetDirectory().GetCString()[0] == '~') {
6081ad6265SDimitry Andric     FileSystem::Instance().Resolve(file_spec);
6181ad6265SDimitry Andric   }
6281ad6265SDimitry Andric }
6381ad6265SDimitry Andric 
640b57cec5SDimitry Andric // SourceManager constructor
SourceManager(const TargetSP & target_sp)650b57cec5SDimitry Andric SourceManager::SourceManager(const TargetSP &target_sp)
665ffd83dbSDimitry Andric     : m_last_line(0), m_last_count(0), m_default_set(false),
670b57cec5SDimitry Andric       m_target_wp(target_sp),
680b57cec5SDimitry Andric       m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
690b57cec5SDimitry Andric 
SourceManager(const DebuggerSP & debugger_sp)700b57cec5SDimitry Andric SourceManager::SourceManager(const DebuggerSP &debugger_sp)
715ffd83dbSDimitry Andric     : m_last_line(0), m_last_count(0), m_default_set(false), m_target_wp(),
725ffd83dbSDimitry Andric       m_debugger_wp(debugger_sp) {}
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric // Destructor
75fe6060f1SDimitry Andric SourceManager::~SourceManager() = default;
760b57cec5SDimitry Andric 
GetFile(const FileSpec & file_spec)770b57cec5SDimitry Andric SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
785ffd83dbSDimitry Andric   if (!file_spec)
7906c3fb27SDimitry Andric     return {};
800b57cec5SDimitry Andric 
8106c3fb27SDimitry Andric   Log *log = GetLog(LLDBLog::Source);
8281ad6265SDimitry Andric 
830b57cec5SDimitry Andric   DebuggerSP debugger_sp(m_debugger_wp.lock());
840b57cec5SDimitry Andric   TargetSP target_sp(m_target_wp.lock());
850b57cec5SDimitry Andric 
8606c3fb27SDimitry Andric   if (!debugger_sp || !debugger_sp->GetUseSourceCache()) {
8706c3fb27SDimitry Andric     LLDB_LOG(log, "Source file caching disabled: creating new source file: {0}",
8806c3fb27SDimitry Andric              file_spec);
890b57cec5SDimitry Andric     if (target_sp)
9006c3fb27SDimitry Andric       return std::make_shared<File>(file_spec, target_sp);
9106c3fb27SDimitry Andric     return std::make_shared<File>(file_spec, debugger_sp);
920b57cec5SDimitry Andric   }
9306c3fb27SDimitry Andric 
9406c3fb27SDimitry Andric   ProcessSP process_sp = target_sp ? target_sp->GetProcessSP() : ProcessSP();
9506c3fb27SDimitry Andric 
9606c3fb27SDimitry Andric   // Check the process source cache first. This is the fast path which avoids
9706c3fb27SDimitry Andric   // touching the file system unless the path remapping has changed.
9806c3fb27SDimitry Andric   if (process_sp) {
9906c3fb27SDimitry Andric     if (FileSP file_sp =
10006c3fb27SDimitry Andric             process_sp->GetSourceFileCache().FindSourceFile(file_spec)) {
10106c3fb27SDimitry Andric       LLDB_LOG(log, "Found source file in the process cache: {0}", file_spec);
10206c3fb27SDimitry Andric       if (file_sp->PathRemappingIsStale()) {
10306c3fb27SDimitry Andric         LLDB_LOG(log, "Path remapping is stale: removing file from caches: {0}",
10406c3fb27SDimitry Andric                  file_spec);
10506c3fb27SDimitry Andric 
10606c3fb27SDimitry Andric         // Remove the file from the debugger and process cache. Otherwise we'll
10706c3fb27SDimitry Andric         // hit the same issue again below when querying the debugger cache.
10806c3fb27SDimitry Andric         debugger_sp->GetSourceFileCache().RemoveSourceFile(file_sp);
10906c3fb27SDimitry Andric         process_sp->GetSourceFileCache().RemoveSourceFile(file_sp);
11006c3fb27SDimitry Andric 
11106c3fb27SDimitry Andric         file_sp.reset();
11206c3fb27SDimitry Andric       } else {
11306c3fb27SDimitry Andric         return file_sp;
11406c3fb27SDimitry Andric       }
11506c3fb27SDimitry Andric     }
11606c3fb27SDimitry Andric   }
11706c3fb27SDimitry Andric 
11806c3fb27SDimitry Andric   // Cache miss in the process cache. Check the debugger source cache.
11906c3fb27SDimitry Andric   FileSP file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
12006c3fb27SDimitry Andric 
12106c3fb27SDimitry Andric   // We found the file in the debugger cache. Check if anything invalidated our
12206c3fb27SDimitry Andric   // cache result.
12306c3fb27SDimitry Andric   if (file_sp)
12406c3fb27SDimitry Andric     LLDB_LOG(log, "Found source file in the debugger cache: {0}", file_spec);
12506c3fb27SDimitry Andric 
12606c3fb27SDimitry Andric   // Check if the path remapping has changed.
12706c3fb27SDimitry Andric   if (file_sp && file_sp->PathRemappingIsStale()) {
12806c3fb27SDimitry Andric     LLDB_LOG(log, "Path remapping is stale: {0}", file_spec);
12906c3fb27SDimitry Andric     file_sp.reset();
13006c3fb27SDimitry Andric   }
13106c3fb27SDimitry Andric 
13206c3fb27SDimitry Andric   // Check if the modification time has changed.
13306c3fb27SDimitry Andric   if (file_sp && file_sp->ModificationTimeIsStale()) {
13406c3fb27SDimitry Andric     LLDB_LOG(log, "Modification time is stale: {0}", file_spec);
13506c3fb27SDimitry Andric     file_sp.reset();
13606c3fb27SDimitry Andric   }
13706c3fb27SDimitry Andric 
13806c3fb27SDimitry Andric   // Check if the file exists on disk.
13906c3fb27SDimitry Andric   if (file_sp && !FileSystem::Instance().Exists(file_sp->GetFileSpec())) {
14006c3fb27SDimitry Andric     LLDB_LOG(log, "File doesn't exist on disk: {0}", file_spec);
14106c3fb27SDimitry Andric     file_sp.reset();
14206c3fb27SDimitry Andric   }
14306c3fb27SDimitry Andric 
14406c3fb27SDimitry Andric   // If at this point we don't have a valid file, it means we either didn't find
14506c3fb27SDimitry Andric   // it in the debugger cache or something caused it to be invalidated.
14606c3fb27SDimitry Andric   if (!file_sp) {
14706c3fb27SDimitry Andric     LLDB_LOG(log, "Creating and caching new source file: {0}", file_spec);
14806c3fb27SDimitry Andric 
14906c3fb27SDimitry Andric     // (Re)create the file.
15006c3fb27SDimitry Andric     if (target_sp)
15106c3fb27SDimitry Andric       file_sp = std::make_shared<File>(file_spec, target_sp);
15206c3fb27SDimitry Andric     else
15306c3fb27SDimitry Andric       file_sp = std::make_shared<File>(file_spec, debugger_sp);
15406c3fb27SDimitry Andric 
15506c3fb27SDimitry Andric     // Add the file to the debugger and process cache. If the file was
15606c3fb27SDimitry Andric     // invalidated, this will overwrite it.
15706c3fb27SDimitry Andric     debugger_sp->GetSourceFileCache().AddSourceFile(file_spec, file_sp);
15806c3fb27SDimitry Andric     if (process_sp)
15906c3fb27SDimitry Andric       process_sp->GetSourceFileCache().AddSourceFile(file_spec, file_sp);
16006c3fb27SDimitry Andric   }
16106c3fb27SDimitry Andric 
1620b57cec5SDimitry Andric   return file_sp;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
should_highlight_source(DebuggerSP debugger_sp)1650b57cec5SDimitry Andric static bool should_highlight_source(DebuggerSP debugger_sp) {
1660b57cec5SDimitry Andric   if (!debugger_sp)
1670b57cec5SDimitry Andric     return false;
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   // We don't use ANSI stop column formatting if the debugger doesn't think it
1700b57cec5SDimitry Andric   // should be using color.
1710b57cec5SDimitry Andric   if (!debugger_sp->GetUseColor())
1720b57cec5SDimitry Andric     return false;
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   return debugger_sp->GetHighlightSource();
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric 
should_show_stop_column_with_ansi(DebuggerSP debugger_sp)1770b57cec5SDimitry Andric static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
1780b57cec5SDimitry Andric   // We don't use ANSI stop column formatting if we can't lookup values from
1790b57cec5SDimitry Andric   // the debugger.
1800b57cec5SDimitry Andric   if (!debugger_sp)
1810b57cec5SDimitry Andric     return false;
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   // We don't use ANSI stop column formatting if the debugger doesn't think it
1840b57cec5SDimitry Andric   // should be using color.
1850b57cec5SDimitry Andric   if (!debugger_sp->GetUseColor())
1860b57cec5SDimitry Andric     return false;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   // We only use ANSI stop column formatting if we're either supposed to show
1890b57cec5SDimitry Andric   // ANSI where available (which we know we have when we get to this point), or
1900b57cec5SDimitry Andric   // if we're only supposed to use ANSI.
1910b57cec5SDimitry Andric   const auto value = debugger_sp->GetStopShowColumn();
1920b57cec5SDimitry Andric   return ((value == eStopShowColumnAnsiOrCaret) ||
1930b57cec5SDimitry Andric           (value == eStopShowColumnAnsi));
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
should_show_stop_column_with_caret(DebuggerSP debugger_sp)1960b57cec5SDimitry Andric static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
1970b57cec5SDimitry Andric   // We don't use text-based stop column formatting if we can't lookup values
1980b57cec5SDimitry Andric   // from the debugger.
1990b57cec5SDimitry Andric   if (!debugger_sp)
2000b57cec5SDimitry Andric     return false;
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   // If we're asked to show the first available of ANSI or caret, then we do
2030b57cec5SDimitry Andric   // show the caret when ANSI is not available.
2040b57cec5SDimitry Andric   const auto value = debugger_sp->GetStopShowColumn();
2050b57cec5SDimitry Andric   if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
2060b57cec5SDimitry Andric     return true;
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric   // The only other time we use caret is if we're explicitly asked to show
2090b57cec5SDimitry Andric   // caret.
2100b57cec5SDimitry Andric   return value == eStopShowColumnCaret;
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric 
should_show_stop_line_with_ansi(DebuggerSP debugger_sp)2135ffd83dbSDimitry Andric static bool should_show_stop_line_with_ansi(DebuggerSP debugger_sp) {
2145ffd83dbSDimitry Andric   return debugger_sp && debugger_sp->GetUseColor();
2155ffd83dbSDimitry Andric }
2165ffd83dbSDimitry Andric 
DisplaySourceLinesWithLineNumbersUsingLastFile(uint32_t start_line,uint32_t count,uint32_t curr_line,uint32_t column,const char * current_line_cstr,Stream * s,const SymbolContextList * bp_locs)2170b57cec5SDimitry Andric size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
2180b57cec5SDimitry Andric     uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
2190b57cec5SDimitry Andric     const char *current_line_cstr, Stream *s,
2200b57cec5SDimitry Andric     const SymbolContextList *bp_locs) {
2210b57cec5SDimitry Andric   if (count == 0)
2220b57cec5SDimitry Andric     return 0;
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   Stream::ByteDelta delta(*s);
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   if (start_line == 0) {
2270b57cec5SDimitry Andric     if (m_last_line != 0 && m_last_line != UINT32_MAX)
2280b57cec5SDimitry Andric       start_line = m_last_line + m_last_count;
2290b57cec5SDimitry Andric     else
2300b57cec5SDimitry Andric       start_line = 1;
2310b57cec5SDimitry Andric   }
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric   if (!m_default_set) {
2340b57cec5SDimitry Andric     FileSpec tmp_spec;
2350b57cec5SDimitry Andric     uint32_t tmp_line;
2360b57cec5SDimitry Andric     GetDefaultFileAndLine(tmp_spec, tmp_line);
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   m_last_line = start_line;
2400b57cec5SDimitry Andric   m_last_count = count;
2410b57cec5SDimitry Andric 
2425ffd83dbSDimitry Andric   if (FileSP last_file_sp = GetLastFile()) {
2430b57cec5SDimitry Andric     const uint32_t end_line = start_line + count - 1;
2440b57cec5SDimitry Andric     for (uint32_t line = start_line; line <= end_line; ++line) {
2455ffd83dbSDimitry Andric       if (!last_file_sp->LineIsValid(line)) {
2460b57cec5SDimitry Andric         m_last_line = UINT32_MAX;
2470b57cec5SDimitry Andric         break;
2480b57cec5SDimitry Andric       }
2490b57cec5SDimitry Andric 
250e8d8bef9SDimitry Andric       std::string prefix;
2510b57cec5SDimitry Andric       if (bp_locs) {
2520b57cec5SDimitry Andric         uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric         if (bp_count > 0)
255e8d8bef9SDimitry Andric           prefix = llvm::formatv("[{0}]", bp_count);
2560b57cec5SDimitry Andric         else
257e8d8bef9SDimitry Andric           prefix = "    ";
2580b57cec5SDimitry Andric       }
2590b57cec5SDimitry Andric 
2605ffd83dbSDimitry Andric       char buffer[3];
26106c3fb27SDimitry Andric       snprintf(buffer, sizeof(buffer), "%2.2s",
26206c3fb27SDimitry Andric                (line == curr_line) ? current_line_cstr : "");
2635ffd83dbSDimitry Andric       std::string current_line_highlight(buffer);
2645ffd83dbSDimitry Andric 
2655ffd83dbSDimitry Andric       auto debugger_sp = m_debugger_wp.lock();
2665ffd83dbSDimitry Andric       if (should_show_stop_line_with_ansi(debugger_sp)) {
2675ffd83dbSDimitry Andric         current_line_highlight = ansi::FormatAnsiTerminalCodes(
2685ffd83dbSDimitry Andric             (debugger_sp->GetStopShowLineMarkerAnsiPrefix() +
2695ffd83dbSDimitry Andric              current_line_highlight +
2705ffd83dbSDimitry Andric              debugger_sp->GetStopShowLineMarkerAnsiSuffix())
2715ffd83dbSDimitry Andric                 .str());
2725ffd83dbSDimitry Andric       }
2735ffd83dbSDimitry Andric 
274e8d8bef9SDimitry Andric       s->Printf("%s%s %-4u\t", prefix.c_str(), current_line_highlight.c_str(),
275e8d8bef9SDimitry Andric                 line);
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric       // So far we treated column 0 as a special 'no column value', but
2780b57cec5SDimitry Andric       // DisplaySourceLines starts counting columns from 0 (and no column is
2790b57cec5SDimitry Andric       // expressed by passing an empty optional).
280bdd1243dSDimitry Andric       std::optional<size_t> columnToHighlight;
2810b57cec5SDimitry Andric       if (line == curr_line && column)
2820b57cec5SDimitry Andric         columnToHighlight = column - 1;
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric       size_t this_line_size =
2855ffd83dbSDimitry Andric           last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s);
2860b57cec5SDimitry Andric       if (column != 0 && line == curr_line &&
2875ffd83dbSDimitry Andric           should_show_stop_column_with_caret(debugger_sp)) {
2880b57cec5SDimitry Andric         // Display caret cursor.
2890b57cec5SDimitry Andric         std::string src_line;
2905ffd83dbSDimitry Andric         last_file_sp->GetLine(line, src_line);
2910b57cec5SDimitry Andric         s->Printf("    \t");
2920b57cec5SDimitry Andric         // Insert a space for every non-tab character in the source line.
2930b57cec5SDimitry Andric         for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
2940b57cec5SDimitry Andric           s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
2950b57cec5SDimitry Andric         // Now add the caret.
2960b57cec5SDimitry Andric         s->Printf("^\n");
2970b57cec5SDimitry Andric       }
2980b57cec5SDimitry Andric       if (this_line_size == 0) {
2990b57cec5SDimitry Andric         m_last_line = UINT32_MAX;
3000b57cec5SDimitry Andric         break;
3010b57cec5SDimitry Andric       }
3020b57cec5SDimitry Andric     }
3030b57cec5SDimitry Andric   }
3040b57cec5SDimitry Andric   return *delta;
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric 
DisplaySourceLinesWithLineNumbers(const FileSpec & file_spec,uint32_t line,uint32_t column,uint32_t context_before,uint32_t context_after,const char * current_line_cstr,Stream * s,const SymbolContextList * bp_locs)3070b57cec5SDimitry Andric size_t SourceManager::DisplaySourceLinesWithLineNumbers(
3080b57cec5SDimitry Andric     const FileSpec &file_spec, uint32_t line, uint32_t column,
3090b57cec5SDimitry Andric     uint32_t context_before, uint32_t context_after,
3100b57cec5SDimitry Andric     const char *current_line_cstr, Stream *s,
3110b57cec5SDimitry Andric     const SymbolContextList *bp_locs) {
3120b57cec5SDimitry Andric   FileSP file_sp(GetFile(file_spec));
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   uint32_t start_line;
3150b57cec5SDimitry Andric   uint32_t count = context_before + context_after + 1;
3160b57cec5SDimitry Andric   if (line > context_before)
3170b57cec5SDimitry Andric     start_line = line - context_before;
3180b57cec5SDimitry Andric   else
3190b57cec5SDimitry Andric     start_line = 1;
3200b57cec5SDimitry Andric 
3215ffd83dbSDimitry Andric   FileSP last_file_sp(GetLastFile());
3225ffd83dbSDimitry Andric   if (last_file_sp.get() != file_sp.get()) {
3230b57cec5SDimitry Andric     if (line == 0)
3240b57cec5SDimitry Andric       m_last_line = 0;
3255ffd83dbSDimitry Andric     m_last_file_spec = file_spec;
3260b57cec5SDimitry Andric   }
3270b57cec5SDimitry Andric   return DisplaySourceLinesWithLineNumbersUsingLastFile(
3280b57cec5SDimitry Andric       start_line, count, line, column, current_line_cstr, s, bp_locs);
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
DisplayMoreWithLineNumbers(Stream * s,uint32_t count,bool reverse,const SymbolContextList * bp_locs)3310b57cec5SDimitry Andric size_t SourceManager::DisplayMoreWithLineNumbers(
3320b57cec5SDimitry Andric     Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
3330b57cec5SDimitry Andric   // If we get called before anybody has set a default file and line, then try
3340b57cec5SDimitry Andric   // to figure it out here.
3355ffd83dbSDimitry Andric   FileSP last_file_sp(GetLastFile());
3365ffd83dbSDimitry Andric   const bool have_default_file_line = last_file_sp && m_last_line > 0;
3370b57cec5SDimitry Andric   if (!m_default_set) {
3380b57cec5SDimitry Andric     FileSpec tmp_spec;
3390b57cec5SDimitry Andric     uint32_t tmp_line;
3400b57cec5SDimitry Andric     GetDefaultFileAndLine(tmp_spec, tmp_line);
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric 
3435ffd83dbSDimitry Andric   if (last_file_sp) {
3440b57cec5SDimitry Andric     if (m_last_line == UINT32_MAX)
3450b57cec5SDimitry Andric       return 0;
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric     if (reverse && m_last_line == 1)
3480b57cec5SDimitry Andric       return 0;
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric     if (count > 0)
3510b57cec5SDimitry Andric       m_last_count = count;
3520b57cec5SDimitry Andric     else if (m_last_count == 0)
3530b57cec5SDimitry Andric       m_last_count = 10;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric     if (m_last_line > 0) {
3560b57cec5SDimitry Andric       if (reverse) {
3570b57cec5SDimitry Andric         // If this is the first time we've done a reverse, then back up one
3580b57cec5SDimitry Andric         // more time so we end up showing the chunk before the last one we've
3590b57cec5SDimitry Andric         // shown:
3600b57cec5SDimitry Andric         if (m_last_line > m_last_count)
3610b57cec5SDimitry Andric           m_last_line -= m_last_count;
3620b57cec5SDimitry Andric         else
3630b57cec5SDimitry Andric           m_last_line = 1;
3640b57cec5SDimitry Andric       } else if (have_default_file_line)
3650b57cec5SDimitry Andric         m_last_line += m_last_count;
3660b57cec5SDimitry Andric     } else
3670b57cec5SDimitry Andric       m_last_line = 1;
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric     const uint32_t column = 0;
3700b57cec5SDimitry Andric     return DisplaySourceLinesWithLineNumbersUsingLastFile(
3710b57cec5SDimitry Andric         m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
3720b57cec5SDimitry Andric   }
3730b57cec5SDimitry Andric   return 0;
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric 
SetDefaultFileAndLine(const FileSpec & file_spec,uint32_t line)3760b57cec5SDimitry Andric bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
3770b57cec5SDimitry Andric                                           uint32_t line) {
3780b57cec5SDimitry Andric   m_default_set = true;
3795ffd83dbSDimitry Andric   FileSP file_sp(GetFile(file_spec));
3805ffd83dbSDimitry Andric 
3815ffd83dbSDimitry Andric   if (file_sp) {
3820b57cec5SDimitry Andric     m_last_line = line;
3835ffd83dbSDimitry Andric     m_last_file_spec = file_spec;
3840b57cec5SDimitry Andric     return true;
3850b57cec5SDimitry Andric   } else {
3860b57cec5SDimitry Andric     return false;
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric 
GetDefaultFileAndLine(FileSpec & file_spec,uint32_t & line)3900b57cec5SDimitry Andric bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
3915ffd83dbSDimitry Andric   if (FileSP last_file_sp = GetLastFile()) {
3925ffd83dbSDimitry Andric     file_spec = m_last_file_spec;
3930b57cec5SDimitry Andric     line = m_last_line;
3940b57cec5SDimitry Andric     return true;
3950b57cec5SDimitry Andric   } else if (!m_default_set) {
3960b57cec5SDimitry Andric     TargetSP target_sp(m_target_wp.lock());
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric     if (target_sp) {
3990b57cec5SDimitry Andric       // If nobody has set the default file and line then try here.  If there's
4000b57cec5SDimitry Andric       // no executable, then we will try again later when there is one.
4010b57cec5SDimitry Andric       // Otherwise, if we can't find it we won't look again, somebody will have
4020b57cec5SDimitry Andric       // to set it (for instance when we stop somewhere...)
4030b57cec5SDimitry Andric       Module *executable_ptr = target_sp->GetExecutableModulePointer();
4040b57cec5SDimitry Andric       if (executable_ptr) {
4050b57cec5SDimitry Andric         SymbolContextList sc_list;
4060b57cec5SDimitry Andric         ConstString main_name("main");
407349cc55cSDimitry Andric 
408349cc55cSDimitry Andric         ModuleFunctionSearchOptions function_options;
409349cc55cSDimitry Andric         function_options.include_symbols =
410349cc55cSDimitry Andric             false; // Force it to be a debug symbol.
411349cc55cSDimitry Andric         function_options.include_inlines = true;
4125ffd83dbSDimitry Andric         executable_ptr->FindFunctions(main_name, CompilerDeclContext(),
413349cc55cSDimitry Andric                                       lldb::eFunctionNameTypeBase,
414349cc55cSDimitry Andric                                       function_options, sc_list);
41506c3fb27SDimitry Andric         for (const SymbolContext &sc : sc_list) {
4160b57cec5SDimitry Andric           if (sc.function) {
4170b57cec5SDimitry Andric             lldb_private::LineEntry line_entry;
4180b57cec5SDimitry Andric             if (sc.function->GetAddressRange()
4190b57cec5SDimitry Andric                     .GetBaseAddress()
4200b57cec5SDimitry Andric                     .CalculateSymbolContextLineEntry(line_entry)) {
421*0fca6ea1SDimitry Andric               SetDefaultFileAndLine(line_entry.GetFile(), line_entry.line);
4225ffd83dbSDimitry Andric               file_spec = m_last_file_spec;
4230b57cec5SDimitry Andric               line = m_last_line;
4240b57cec5SDimitry Andric               return true;
4250b57cec5SDimitry Andric             }
4260b57cec5SDimitry Andric           }
4270b57cec5SDimitry Andric         }
4280b57cec5SDimitry Andric       }
4290b57cec5SDimitry Andric     }
4300b57cec5SDimitry Andric   }
4310b57cec5SDimitry Andric   return false;
4320b57cec5SDimitry Andric }
4330b57cec5SDimitry Andric 
FindLinesMatchingRegex(FileSpec & file_spec,RegularExpression & regex,uint32_t start_line,uint32_t end_line,std::vector<uint32_t> & match_lines)4340b57cec5SDimitry Andric void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
4350b57cec5SDimitry Andric                                            RegularExpression &regex,
4360b57cec5SDimitry Andric                                            uint32_t start_line,
4370b57cec5SDimitry Andric                                            uint32_t end_line,
4380b57cec5SDimitry Andric                                            std::vector<uint32_t> &match_lines) {
4390b57cec5SDimitry Andric   match_lines.clear();
4400b57cec5SDimitry Andric   FileSP file_sp = GetFile(file_spec);
4410b57cec5SDimitry Andric   if (!file_sp)
4420b57cec5SDimitry Andric     return;
4430b57cec5SDimitry Andric   return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
4440b57cec5SDimitry Andric                                          match_lines);
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric 
File(const FileSpec & file_spec,lldb::DebuggerSP debugger_sp)4470b57cec5SDimitry Andric SourceManager::File::File(const FileSpec &file_spec,
4480b57cec5SDimitry Andric                           lldb::DebuggerSP debugger_sp)
44906c3fb27SDimitry Andric     : m_file_spec_orig(file_spec), m_file_spec(), m_mod_time(),
45006c3fb27SDimitry Andric       m_debugger_wp(debugger_sp), m_target_wp(TargetSP()) {
45106c3fb27SDimitry Andric   CommonInitializer(file_spec, {});
4520b57cec5SDimitry Andric }
4530b57cec5SDimitry Andric 
File(const FileSpec & file_spec,TargetSP target_sp)45406c3fb27SDimitry Andric SourceManager::File::File(const FileSpec &file_spec, TargetSP target_sp)
45506c3fb27SDimitry Andric     : m_file_spec_orig(file_spec), m_file_spec(), m_mod_time(),
45606c3fb27SDimitry Andric       m_debugger_wp(target_sp ? target_sp->GetDebugger().shared_from_this()
45706c3fb27SDimitry Andric                               : DebuggerSP()),
45806c3fb27SDimitry Andric       m_target_wp(target_sp) {
45906c3fb27SDimitry Andric   CommonInitializer(file_spec, target_sp);
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
CommonInitializer(const FileSpec & file_spec,TargetSP target_sp)4620b57cec5SDimitry Andric void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
46306c3fb27SDimitry Andric                                             TargetSP target_sp) {
46406c3fb27SDimitry Andric   // Set the file and update the modification time.
46506c3fb27SDimitry Andric   SetFileSpec(file_spec);
4660b57cec5SDimitry Andric 
46706c3fb27SDimitry Andric   // Always update the source map modification ID if we have a target.
46806c3fb27SDimitry Andric   if (target_sp)
46906c3fb27SDimitry Andric     m_source_map_mod_id = target_sp->GetSourcePathMap().GetModificationID();
47006c3fb27SDimitry Andric 
47106c3fb27SDimitry Andric   // File doesn't exist.
47206c3fb27SDimitry Andric   if (m_mod_time == llvm::sys::TimePoint<>()) {
47306c3fb27SDimitry Andric     if (target_sp) {
47406c3fb27SDimitry Andric       // If this is just a file name, try finding it in the target.
4750b57cec5SDimitry Andric       if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
4760b57cec5SDimitry Andric         bool check_inlines = false;
4770b57cec5SDimitry Andric         SymbolContextList sc_list;
4780b57cec5SDimitry Andric         size_t num_matches =
47906c3fb27SDimitry Andric             target_sp->GetImages().ResolveSymbolContextForFilePath(
4800b57cec5SDimitry Andric                 file_spec.GetFilename().AsCString(), 0, check_inlines,
4810b57cec5SDimitry Andric                 SymbolContextItem(eSymbolContextModule |
4820b57cec5SDimitry Andric                                   eSymbolContextCompUnit),
4830b57cec5SDimitry Andric                 sc_list);
4840b57cec5SDimitry Andric         bool got_multiple = false;
4850b57cec5SDimitry Andric         if (num_matches != 0) {
4860b57cec5SDimitry Andric           if (num_matches > 1) {
487480093f4SDimitry Andric             CompileUnit *test_cu = nullptr;
48806c3fb27SDimitry Andric             for (const SymbolContext &sc : sc_list) {
4890b57cec5SDimitry Andric               if (sc.comp_unit) {
490480093f4SDimitry Andric                 if (test_cu) {
491480093f4SDimitry Andric                   if (test_cu != sc.comp_unit)
4920b57cec5SDimitry Andric                     got_multiple = true;
4930b57cec5SDimitry Andric                   break;
4940b57cec5SDimitry Andric                 } else
495480093f4SDimitry Andric                   test_cu = sc.comp_unit;
4960b57cec5SDimitry Andric               }
4970b57cec5SDimitry Andric             }
4980b57cec5SDimitry Andric           }
4990b57cec5SDimitry Andric           if (!got_multiple) {
5000b57cec5SDimitry Andric             SymbolContext sc;
5010b57cec5SDimitry Andric             sc_list.GetContextAtIndex(0, sc);
502480093f4SDimitry Andric             if (sc.comp_unit)
50306c3fb27SDimitry Andric               SetFileSpec(sc.comp_unit->GetPrimaryFile());
5040b57cec5SDimitry Andric           }
5050b57cec5SDimitry Andric         }
5060b57cec5SDimitry Andric       }
5070b57cec5SDimitry Andric 
50806c3fb27SDimitry Andric       // Try remapping the file if it doesn't exist.
50906c3fb27SDimitry Andric       if (!FileSystem::Instance().Exists(m_file_spec)) {
51006c3fb27SDimitry Andric         // Check target specific source remappings (i.e., the
51106c3fb27SDimitry Andric         // target.source-map setting), then fall back to the module
51206c3fb27SDimitry Andric         // specific remapping (i.e., the .dSYM remapping dictionary).
51306c3fb27SDimitry Andric         auto remapped = target_sp->GetSourcePathMap().FindFile(m_file_spec);
51406c3fb27SDimitry Andric         if (!remapped) {
51506c3fb27SDimitry Andric           FileSpec new_spec;
51606c3fb27SDimitry Andric           if (target_sp->GetImages().FindSourceFile(m_file_spec, new_spec))
51706c3fb27SDimitry Andric             remapped = new_spec;
51806c3fb27SDimitry Andric         }
51906c3fb27SDimitry Andric         if (remapped)
52006c3fb27SDimitry Andric           SetFileSpec(*remapped);
52106c3fb27SDimitry Andric       }
52206c3fb27SDimitry Andric     }
52306c3fb27SDimitry Andric   }
52406c3fb27SDimitry Andric 
52506c3fb27SDimitry Andric   // If the file exists, read in the data.
5260b57cec5SDimitry Andric   if (m_mod_time != llvm::sys::TimePoint<>())
5270b57cec5SDimitry Andric     m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
5280b57cec5SDimitry Andric }
5290b57cec5SDimitry Andric 
SetFileSpec(FileSpec file_spec)53006c3fb27SDimitry Andric void SourceManager::File::SetFileSpec(FileSpec file_spec) {
53106c3fb27SDimitry Andric   resolve_tilde(file_spec);
53206c3fb27SDimitry Andric   m_file_spec = std::move(file_spec);
53306c3fb27SDimitry Andric   m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
53406c3fb27SDimitry Andric }
53506c3fb27SDimitry Andric 
GetLineOffset(uint32_t line)5360b57cec5SDimitry Andric uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
5370b57cec5SDimitry Andric   if (line == 0)
5380b57cec5SDimitry Andric     return UINT32_MAX;
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric   if (line == 1)
5410b57cec5SDimitry Andric     return 0;
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric   if (CalculateLineOffsets(line)) {
5440b57cec5SDimitry Andric     if (line < m_offsets.size())
5450b57cec5SDimitry Andric       return m_offsets[line - 1]; // yes we want "line - 1" in the index
5460b57cec5SDimitry Andric   }
5470b57cec5SDimitry Andric   return UINT32_MAX;
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
GetNumLines()5500b57cec5SDimitry Andric uint32_t SourceManager::File::GetNumLines() {
5510b57cec5SDimitry Andric   CalculateLineOffsets();
5520b57cec5SDimitry Andric   return m_offsets.size();
5530b57cec5SDimitry Andric }
5540b57cec5SDimitry Andric 
PeekLineData(uint32_t line)5550b57cec5SDimitry Andric const char *SourceManager::File::PeekLineData(uint32_t line) {
5560b57cec5SDimitry Andric   if (!LineIsValid(line))
5570b57cec5SDimitry Andric     return nullptr;
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric   size_t line_offset = GetLineOffset(line);
5600b57cec5SDimitry Andric   if (line_offset < m_data_sp->GetByteSize())
5610b57cec5SDimitry Andric     return (const char *)m_data_sp->GetBytes() + line_offset;
5620b57cec5SDimitry Andric   return nullptr;
5630b57cec5SDimitry Andric }
5640b57cec5SDimitry Andric 
GetLineLength(uint32_t line,bool include_newline_chars)5650b57cec5SDimitry Andric uint32_t SourceManager::File::GetLineLength(uint32_t line,
5660b57cec5SDimitry Andric                                             bool include_newline_chars) {
5670b57cec5SDimitry Andric   if (!LineIsValid(line))
5680b57cec5SDimitry Andric     return false;
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric   size_t start_offset = GetLineOffset(line);
5710b57cec5SDimitry Andric   size_t end_offset = GetLineOffset(line + 1);
5720b57cec5SDimitry Andric   if (end_offset == UINT32_MAX)
5730b57cec5SDimitry Andric     end_offset = m_data_sp->GetByteSize();
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric   if (end_offset > start_offset) {
5760b57cec5SDimitry Andric     uint32_t length = end_offset - start_offset;
5770b57cec5SDimitry Andric     if (!include_newline_chars) {
5780b57cec5SDimitry Andric       const char *line_start =
5790b57cec5SDimitry Andric           (const char *)m_data_sp->GetBytes() + start_offset;
5800b57cec5SDimitry Andric       while (length > 0) {
5810b57cec5SDimitry Andric         const char last_char = line_start[length - 1];
5820b57cec5SDimitry Andric         if ((last_char == '\r') || (last_char == '\n'))
5830b57cec5SDimitry Andric           --length;
5840b57cec5SDimitry Andric         else
5850b57cec5SDimitry Andric           break;
5860b57cec5SDimitry Andric       }
5870b57cec5SDimitry Andric     }
5880b57cec5SDimitry Andric     return length;
5890b57cec5SDimitry Andric   }
5900b57cec5SDimitry Andric   return 0;
5910b57cec5SDimitry Andric }
5920b57cec5SDimitry Andric 
LineIsValid(uint32_t line)5930b57cec5SDimitry Andric bool SourceManager::File::LineIsValid(uint32_t line) {
5940b57cec5SDimitry Andric   if (line == 0)
5950b57cec5SDimitry Andric     return false;
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric   if (CalculateLineOffsets(line))
5980b57cec5SDimitry Andric     return line < m_offsets.size();
5990b57cec5SDimitry Andric   return false;
6000b57cec5SDimitry Andric }
6010b57cec5SDimitry Andric 
ModificationTimeIsStale() const60206c3fb27SDimitry Andric bool SourceManager::File::ModificationTimeIsStale() const {
6030b57cec5SDimitry Andric   // TODO: use host API to sign up for file modifications to anything in our
6040b57cec5SDimitry Andric   // source cache and only update when we determine a file has been updated.
6050b57cec5SDimitry Andric   // For now we check each time we want to display info for the file.
6060b57cec5SDimitry Andric   auto curr_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
60706c3fb27SDimitry Andric   return curr_mod_time != llvm::sys::TimePoint<>() &&
60806c3fb27SDimitry Andric          m_mod_time != curr_mod_time;
6090b57cec5SDimitry Andric }
61006c3fb27SDimitry Andric 
PathRemappingIsStale() const61106c3fb27SDimitry Andric bool SourceManager::File::PathRemappingIsStale() const {
61206c3fb27SDimitry Andric   if (TargetSP target_sp = m_target_wp.lock())
61306c3fb27SDimitry Andric     return GetSourceMapModificationID() !=
61406c3fb27SDimitry Andric            target_sp->GetSourcePathMap().GetModificationID();
61506c3fb27SDimitry Andric   return false;
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric 
DisplaySourceLines(uint32_t line,std::optional<size_t> column,uint32_t context_before,uint32_t context_after,Stream * s)6180b57cec5SDimitry Andric size_t SourceManager::File::DisplaySourceLines(uint32_t line,
619bdd1243dSDimitry Andric                                                std::optional<size_t> column,
6200b57cec5SDimitry Andric                                                uint32_t context_before,
6210b57cec5SDimitry Andric                                                uint32_t context_after,
6220b57cec5SDimitry Andric                                                Stream *s) {
6230b57cec5SDimitry Andric   // Nothing to write if there's no stream.
6240b57cec5SDimitry Andric   if (!s)
6250b57cec5SDimitry Andric     return 0;
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   // Sanity check m_data_sp before proceeding.
6280b57cec5SDimitry Andric   if (!m_data_sp)
6290b57cec5SDimitry Andric     return 0;
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric   size_t bytes_written = s->GetWrittenBytes();
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric   auto debugger_sp = m_debugger_wp.lock();
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric   HighlightStyle style;
6360b57cec5SDimitry Andric   // Use the default Vim style if source highlighting is enabled.
6370b57cec5SDimitry Andric   if (should_highlight_source(debugger_sp))
6380b57cec5SDimitry Andric     style = HighlightStyle::MakeVimStyle();
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   // If we should mark the stop column with color codes, then copy the prefix
6410b57cec5SDimitry Andric   // and suffix to our color style.
6420b57cec5SDimitry Andric   if (should_show_stop_column_with_ansi(debugger_sp))
6430b57cec5SDimitry Andric     style.selected.Set(debugger_sp->GetStopShowColumnAnsiPrefix(),
6440b57cec5SDimitry Andric                        debugger_sp->GetStopShowColumnAnsiSuffix());
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric   HighlighterManager mgr;
6470b57cec5SDimitry Andric   std::string path = GetFileSpec().GetPath(/*denormalize*/ false);
6480b57cec5SDimitry Andric   // FIXME: Find a way to get the definitive language this file was written in
6490b57cec5SDimitry Andric   // and pass it to the highlighter.
6500b57cec5SDimitry Andric   const auto &h = mgr.getHighlighterFor(lldb::eLanguageTypeUnknown, path);
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric   const uint32_t start_line =
6530b57cec5SDimitry Andric       line <= context_before ? 1 : line - context_before;
6540b57cec5SDimitry Andric   const uint32_t start_line_offset = GetLineOffset(start_line);
6550b57cec5SDimitry Andric   if (start_line_offset != UINT32_MAX) {
6560b57cec5SDimitry Andric     const uint32_t end_line = line + context_after;
6570b57cec5SDimitry Andric     uint32_t end_line_offset = GetLineOffset(end_line + 1);
6580b57cec5SDimitry Andric     if (end_line_offset == UINT32_MAX)
6590b57cec5SDimitry Andric       end_line_offset = m_data_sp->GetByteSize();
6600b57cec5SDimitry Andric 
6610b57cec5SDimitry Andric     assert(start_line_offset <= end_line_offset);
6620b57cec5SDimitry Andric     if (start_line_offset < end_line_offset) {
6630b57cec5SDimitry Andric       size_t count = end_line_offset - start_line_offset;
6640b57cec5SDimitry Andric       const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric       auto ref = llvm::StringRef(reinterpret_cast<const char *>(cstr), count);
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric       h.Highlight(style, ref, column, "", *s);
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric       // Ensure we get an end of line character one way or another.
6710b57cec5SDimitry Andric       if (!is_newline_char(ref.back()))
6720b57cec5SDimitry Andric         s->EOL();
6730b57cec5SDimitry Andric     }
6740b57cec5SDimitry Andric   }
6750b57cec5SDimitry Andric   return s->GetWrittenBytes() - bytes_written;
6760b57cec5SDimitry Andric }
6770b57cec5SDimitry Andric 
FindLinesMatchingRegex(RegularExpression & regex,uint32_t start_line,uint32_t end_line,std::vector<uint32_t> & match_lines)6780b57cec5SDimitry Andric void SourceManager::File::FindLinesMatchingRegex(
6790b57cec5SDimitry Andric     RegularExpression &regex, uint32_t start_line, uint32_t end_line,
6800b57cec5SDimitry Andric     std::vector<uint32_t> &match_lines) {
6810b57cec5SDimitry Andric   match_lines.clear();
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   if (!LineIsValid(start_line) ||
6840b57cec5SDimitry Andric       (end_line != UINT32_MAX && !LineIsValid(end_line)))
6850b57cec5SDimitry Andric     return;
6860b57cec5SDimitry Andric   if (start_line > end_line)
6870b57cec5SDimitry Andric     return;
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric   for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
6900b57cec5SDimitry Andric     std::string buffer;
6910b57cec5SDimitry Andric     if (!GetLine(line_no, buffer))
6920b57cec5SDimitry Andric       break;
6930b57cec5SDimitry Andric     if (regex.Execute(buffer)) {
6940b57cec5SDimitry Andric       match_lines.push_back(line_no);
6950b57cec5SDimitry Andric     }
6960b57cec5SDimitry Andric   }
6970b57cec5SDimitry Andric }
6980b57cec5SDimitry Andric 
operator ==(const SourceManager::File & lhs,const SourceManager::File & rhs)6990b57cec5SDimitry Andric bool lldb_private::operator==(const SourceManager::File &lhs,
7000b57cec5SDimitry Andric                               const SourceManager::File &rhs) {
7010b57cec5SDimitry Andric   if (lhs.m_file_spec != rhs.m_file_spec)
7020b57cec5SDimitry Andric     return false;
7030b57cec5SDimitry Andric   return lhs.m_mod_time == rhs.m_mod_time;
7040b57cec5SDimitry Andric }
7050b57cec5SDimitry Andric 
CalculateLineOffsets(uint32_t line)7060b57cec5SDimitry Andric bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
7070b57cec5SDimitry Andric   line =
7080b57cec5SDimitry Andric       UINT32_MAX; // TODO: take this line out when we support partial indexing
7090b57cec5SDimitry Andric   if (line == UINT32_MAX) {
7100b57cec5SDimitry Andric     // Already done?
7110b57cec5SDimitry Andric     if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
7120b57cec5SDimitry Andric       return true;
7130b57cec5SDimitry Andric 
7140b57cec5SDimitry Andric     if (m_offsets.empty()) {
7150b57cec5SDimitry Andric       if (m_data_sp.get() == nullptr)
7160b57cec5SDimitry Andric         return false;
7170b57cec5SDimitry Andric 
71881ad6265SDimitry Andric       const char *start = (const char *)m_data_sp->GetBytes();
7190b57cec5SDimitry Andric       if (start) {
7200b57cec5SDimitry Andric         const char *end = start + m_data_sp->GetByteSize();
7210b57cec5SDimitry Andric 
7220b57cec5SDimitry Andric         // Calculate all line offsets from scratch
7230b57cec5SDimitry Andric 
7240b57cec5SDimitry Andric         // Push a 1 at index zero to indicate the file has been completely
7250b57cec5SDimitry Andric         // indexed.
7260b57cec5SDimitry Andric         m_offsets.push_back(UINT32_MAX);
7270b57cec5SDimitry Andric         const char *s;
7280b57cec5SDimitry Andric         for (s = start; s < end; ++s) {
7290b57cec5SDimitry Andric           char curr_ch = *s;
7300b57cec5SDimitry Andric           if (is_newline_char(curr_ch)) {
7310b57cec5SDimitry Andric             if (s + 1 < end) {
7320b57cec5SDimitry Andric               char next_ch = s[1];
7330b57cec5SDimitry Andric               if (is_newline_char(next_ch)) {
7340b57cec5SDimitry Andric                 if (curr_ch != next_ch)
7350b57cec5SDimitry Andric                   ++s;
7360b57cec5SDimitry Andric               }
7370b57cec5SDimitry Andric             }
7380b57cec5SDimitry Andric             m_offsets.push_back(s + 1 - start);
7390b57cec5SDimitry Andric           }
7400b57cec5SDimitry Andric         }
7410b57cec5SDimitry Andric         if (!m_offsets.empty()) {
7420b57cec5SDimitry Andric           if (m_offsets.back() < size_t(end - start))
7430b57cec5SDimitry Andric             m_offsets.push_back(end - start);
7440b57cec5SDimitry Andric         }
7450b57cec5SDimitry Andric         return true;
7460b57cec5SDimitry Andric       }
7470b57cec5SDimitry Andric     } else {
7480b57cec5SDimitry Andric       // Some lines have been populated, start where we last left off
7490b57cec5SDimitry Andric       assert("Not implemented yet" && false);
7500b57cec5SDimitry Andric     }
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric   } else {
7530b57cec5SDimitry Andric     // Calculate all line offsets up to "line"
7540b57cec5SDimitry Andric     assert("Not implemented yet" && false);
7550b57cec5SDimitry Andric   }
7560b57cec5SDimitry Andric   return false;
7570b57cec5SDimitry Andric }
7580b57cec5SDimitry Andric 
GetLine(uint32_t line_no,std::string & buffer)7590b57cec5SDimitry Andric bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
7600b57cec5SDimitry Andric   if (!LineIsValid(line_no))
7610b57cec5SDimitry Andric     return false;
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric   size_t start_offset = GetLineOffset(line_no);
7640b57cec5SDimitry Andric   size_t end_offset = GetLineOffset(line_no + 1);
7650b57cec5SDimitry Andric   if (end_offset == UINT32_MAX) {
7660b57cec5SDimitry Andric     end_offset = m_data_sp->GetByteSize();
7670b57cec5SDimitry Andric   }
76881ad6265SDimitry Andric   buffer.assign((const char *)m_data_sp->GetBytes() + start_offset,
7690b57cec5SDimitry Andric                 end_offset - start_offset);
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric   return true;
7720b57cec5SDimitry Andric }
7730b57cec5SDimitry Andric 
AddSourceFile(const FileSpec & file_spec,FileSP file_sp)77406c3fb27SDimitry Andric void SourceManager::SourceFileCache::AddSourceFile(const FileSpec &file_spec,
77506c3fb27SDimitry Andric                                                    FileSP file_sp) {
77606c3fb27SDimitry Andric   llvm::sys::ScopedWriter guard(m_mutex);
77706c3fb27SDimitry Andric 
77806c3fb27SDimitry Andric   assert(file_sp && "invalid FileSP");
77906c3fb27SDimitry Andric 
78006c3fb27SDimitry Andric   AddSourceFileImpl(file_spec, file_sp);
78106c3fb27SDimitry Andric   const FileSpec &resolved_file_spec = file_sp->GetFileSpec();
78206c3fb27SDimitry Andric   if (file_spec != resolved_file_spec)
78306c3fb27SDimitry Andric     AddSourceFileImpl(file_sp->GetFileSpec(), file_sp);
78406c3fb27SDimitry Andric }
78506c3fb27SDimitry Andric 
RemoveSourceFile(const FileSP & file_sp)78606c3fb27SDimitry Andric void SourceManager::SourceFileCache::RemoveSourceFile(const FileSP &file_sp) {
78706c3fb27SDimitry Andric   llvm::sys::ScopedWriter guard(m_mutex);
78806c3fb27SDimitry Andric 
78906c3fb27SDimitry Andric   assert(file_sp && "invalid FileSP");
79006c3fb27SDimitry Andric 
79106c3fb27SDimitry Andric   // Iterate over all the elements in the cache.
79206c3fb27SDimitry Andric   // This is expensive but a relatively uncommon operation.
79306c3fb27SDimitry Andric   auto it = m_file_cache.begin();
79406c3fb27SDimitry Andric   while (it != m_file_cache.end()) {
79506c3fb27SDimitry Andric     if (it->second == file_sp)
79606c3fb27SDimitry Andric       it = m_file_cache.erase(it);
79706c3fb27SDimitry Andric     else
79806c3fb27SDimitry Andric       it++;
79906c3fb27SDimitry Andric   }
80006c3fb27SDimitry Andric }
80106c3fb27SDimitry Andric 
AddSourceFileImpl(const FileSpec & file_spec,FileSP file_sp)80206c3fb27SDimitry Andric void SourceManager::SourceFileCache::AddSourceFileImpl(
80306c3fb27SDimitry Andric     const FileSpec &file_spec, FileSP file_sp) {
8040b57cec5SDimitry Andric   FileCache::iterator pos = m_file_cache.find(file_spec);
80506c3fb27SDimitry Andric   if (pos == m_file_cache.end()) {
8060b57cec5SDimitry Andric     m_file_cache[file_spec] = file_sp;
80706c3fb27SDimitry Andric   } else {
8080b57cec5SDimitry Andric     if (file_sp != pos->second)
8090b57cec5SDimitry Andric       m_file_cache[file_spec] = file_sp;
8100b57cec5SDimitry Andric   }
8110b57cec5SDimitry Andric }
8120b57cec5SDimitry Andric 
FindSourceFile(const FileSpec & file_spec) const8130b57cec5SDimitry Andric SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
8140b57cec5SDimitry Andric     const FileSpec &file_spec) const {
81506c3fb27SDimitry Andric   llvm::sys::ScopedReader guard(m_mutex);
81606c3fb27SDimitry Andric 
8170b57cec5SDimitry Andric   FileCache::const_iterator pos = m_file_cache.find(file_spec);
8180b57cec5SDimitry Andric   if (pos != m_file_cache.end())
81906c3fb27SDimitry Andric     return pos->second;
82006c3fb27SDimitry Andric   return {};
82106c3fb27SDimitry Andric }
82206c3fb27SDimitry Andric 
Dump(Stream & stream) const82306c3fb27SDimitry Andric void SourceManager::SourceFileCache::Dump(Stream &stream) const {
82406c3fb27SDimitry Andric   stream << "Modification time   Lines    Path\n";
82506c3fb27SDimitry Andric   stream << "------------------- -------- --------------------------------\n";
82606c3fb27SDimitry Andric   for (auto &entry : m_file_cache) {
82706c3fb27SDimitry Andric     if (!entry.second)
82806c3fb27SDimitry Andric       continue;
82906c3fb27SDimitry Andric     FileSP file = entry.second;
83006c3fb27SDimitry Andric     stream.Format("{0:%Y-%m-%d %H:%M:%S} {1,8:d} {2}\n", file->GetTimestamp(),
83106c3fb27SDimitry Andric                   file->GetNumLines(), entry.first.GetPath());
83206c3fb27SDimitry Andric   }
8330b57cec5SDimitry Andric }
834