xref: /src/contrib/llvm-project/lldb/source/Core/SourceManager.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- SourceManager.cpp -------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/Core/SourceManager.h"
10f034231aSEd Maste 
1194994d37SDimitry Andric #include "lldb/Core/Address.h"
1294994d37SDimitry Andric #include "lldb/Core/AddressRange.h"
13f034231aSEd Maste #include "lldb/Core/Debugger.h"
1494994d37SDimitry Andric #include "lldb/Core/FormatEntity.h"
1594994d37SDimitry Andric #include "lldb/Core/Highlighter.h"
16f034231aSEd Maste #include "lldb/Core/Module.h"
1794994d37SDimitry Andric #include "lldb/Core/ModuleList.h"
1814f1b3e8SDimitry Andric #include "lldb/Host/FileSystem.h"
19f034231aSEd Maste #include "lldb/Symbol/CompileUnit.h"
20f034231aSEd Maste #include "lldb/Symbol/Function.h"
2194994d37SDimitry Andric #include "lldb/Symbol/LineEntry.h"
22f034231aSEd Maste #include "lldb/Symbol/SymbolContext.h"
2394994d37SDimitry Andric #include "lldb/Target/PathMappingList.h"
247fa27ce4SDimitry Andric #include "lldb/Target/Process.h"
25f034231aSEd Maste #include "lldb/Target/Target.h"
26cfca06d7SDimitry Andric #include "lldb/Utility/AnsiTerminal.h"
2794994d37SDimitry Andric #include "lldb/Utility/ConstString.h"
2874a628f7SDimitry Andric #include "lldb/Utility/DataBuffer.h"
297fa27ce4SDimitry Andric #include "lldb/Utility/LLDBLog.h"
307fa27ce4SDimitry Andric #include "lldb/Utility/Log.h"
3174a628f7SDimitry Andric #include "lldb/Utility/RegularExpression.h"
3274a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
3394994d37SDimitry Andric #include "lldb/lldb-enumerations.h"
3474a628f7SDimitry Andric 
3594994d37SDimitry Andric #include "llvm/ADT/Twine.h"
3674a628f7SDimitry Andric 
3774a628f7SDimitry Andric #include <memory>
38e3b55780SDimitry Andric #include <optional>
3994994d37SDimitry Andric #include <utility>
4074a628f7SDimitry Andric 
41344a3780SDimitry Andric #include <cassert>
42344a3780SDimitry Andric #include <cstdio>
4374a628f7SDimitry Andric 
4474a628f7SDimitry Andric namespace lldb_private {
4574a628f7SDimitry Andric class ExecutionContext;
4674a628f7SDimitry Andric }
4774a628f7SDimitry Andric namespace lldb_private {
4874a628f7SDimitry Andric class ValueObject;
4974a628f7SDimitry Andric }
50f034231aSEd Maste 
51f034231aSEd Maste using namespace lldb;
52f034231aSEd Maste using namespace lldb_private;
53f034231aSEd Maste 
is_newline_char(char ch)5414f1b3e8SDimitry Andric static inline bool is_newline_char(char ch) { return ch == '\n' || ch == '\r'; }
55f034231aSEd Maste 
resolve_tilde(FileSpec & file_spec)56145449b1SDimitry Andric static void resolve_tilde(FileSpec &file_spec) {
57145449b1SDimitry Andric   if (!FileSystem::Instance().Exists(file_spec) &&
58145449b1SDimitry Andric       file_spec.GetDirectory() &&
59145449b1SDimitry Andric       file_spec.GetDirectory().GetCString()[0] == '~') {
60145449b1SDimitry Andric     FileSystem::Instance().Resolve(file_spec);
61145449b1SDimitry Andric   }
62145449b1SDimitry Andric }
63145449b1SDimitry Andric 
64f034231aSEd Maste // SourceManager constructor
SourceManager(const TargetSP & target_sp)6514f1b3e8SDimitry Andric SourceManager::SourceManager(const TargetSP &target_sp)
66cfca06d7SDimitry Andric     : m_last_line(0), m_last_count(0), m_default_set(false),
67f034231aSEd Maste       m_target_wp(target_sp),
6814f1b3e8SDimitry Andric       m_debugger_wp(target_sp->GetDebugger().shared_from_this()) {}
69f034231aSEd Maste 
SourceManager(const DebuggerSP & debugger_sp)7014f1b3e8SDimitry Andric SourceManager::SourceManager(const DebuggerSP &debugger_sp)
71cfca06d7SDimitry Andric     : m_last_line(0), m_last_count(0), m_default_set(false), m_target_wp(),
72cfca06d7SDimitry Andric       m_debugger_wp(debugger_sp) {}
73f034231aSEd Maste 
74f034231aSEd Maste // Destructor
75344a3780SDimitry Andric SourceManager::~SourceManager() = default;
76f034231aSEd Maste 
GetFile(const FileSpec & file_spec)7714f1b3e8SDimitry Andric SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
78cfca06d7SDimitry Andric   if (!file_spec)
797fa27ce4SDimitry Andric     return {};
80f034231aSEd Maste 
817fa27ce4SDimitry Andric   Log *log = GetLog(LLDBLog::Source);
82145449b1SDimitry Andric 
83f034231aSEd Maste   DebuggerSP debugger_sp(m_debugger_wp.lock());
84f034231aSEd Maste   TargetSP target_sp(m_target_wp.lock());
85f034231aSEd Maste 
867fa27ce4SDimitry Andric   if (!debugger_sp || !debugger_sp->GetUseSourceCache()) {
877fa27ce4SDimitry Andric     LLDB_LOG(log, "Source file caching disabled: creating new source file: {0}",
887fa27ce4SDimitry Andric              file_spec);
8914f1b3e8SDimitry Andric     if (target_sp)
907fa27ce4SDimitry Andric       return std::make_shared<File>(file_spec, target_sp);
917fa27ce4SDimitry Andric     return std::make_shared<File>(file_spec, debugger_sp);
92f034231aSEd Maste   }
937fa27ce4SDimitry Andric 
947fa27ce4SDimitry Andric   ProcessSP process_sp = target_sp ? target_sp->GetProcessSP() : ProcessSP();
957fa27ce4SDimitry Andric 
967fa27ce4SDimitry Andric   // Check the process source cache first. This is the fast path which avoids
977fa27ce4SDimitry Andric   // touching the file system unless the path remapping has changed.
987fa27ce4SDimitry Andric   if (process_sp) {
997fa27ce4SDimitry Andric     if (FileSP file_sp =
1007fa27ce4SDimitry Andric             process_sp->GetSourceFileCache().FindSourceFile(file_spec)) {
1017fa27ce4SDimitry Andric       LLDB_LOG(log, "Found source file in the process cache: {0}", file_spec);
1027fa27ce4SDimitry Andric       if (file_sp->PathRemappingIsStale()) {
1037fa27ce4SDimitry Andric         LLDB_LOG(log, "Path remapping is stale: removing file from caches: {0}",
1047fa27ce4SDimitry Andric                  file_spec);
1057fa27ce4SDimitry Andric 
1067fa27ce4SDimitry Andric         // Remove the file from the debugger and process cache. Otherwise we'll
1077fa27ce4SDimitry Andric         // hit the same issue again below when querying the debugger cache.
1087fa27ce4SDimitry Andric         debugger_sp->GetSourceFileCache().RemoveSourceFile(file_sp);
1097fa27ce4SDimitry Andric         process_sp->GetSourceFileCache().RemoveSourceFile(file_sp);
1107fa27ce4SDimitry Andric 
1117fa27ce4SDimitry Andric         file_sp.reset();
1127fa27ce4SDimitry Andric       } else {
1137fa27ce4SDimitry Andric         return file_sp;
1147fa27ce4SDimitry Andric       }
1157fa27ce4SDimitry Andric     }
1167fa27ce4SDimitry Andric   }
1177fa27ce4SDimitry Andric 
1187fa27ce4SDimitry Andric   // Cache miss in the process cache. Check the debugger source cache.
1197fa27ce4SDimitry Andric   FileSP file_sp = debugger_sp->GetSourceFileCache().FindSourceFile(file_spec);
1207fa27ce4SDimitry Andric 
1217fa27ce4SDimitry Andric   // We found the file in the debugger cache. Check if anything invalidated our
1227fa27ce4SDimitry Andric   // cache result.
1237fa27ce4SDimitry Andric   if (file_sp)
1247fa27ce4SDimitry Andric     LLDB_LOG(log, "Found source file in the debugger cache: {0}", file_spec);
1257fa27ce4SDimitry Andric 
1267fa27ce4SDimitry Andric   // Check if the path remapping has changed.
1277fa27ce4SDimitry Andric   if (file_sp && file_sp->PathRemappingIsStale()) {
1287fa27ce4SDimitry Andric     LLDB_LOG(log, "Path remapping is stale: {0}", file_spec);
1297fa27ce4SDimitry Andric     file_sp.reset();
1307fa27ce4SDimitry Andric   }
1317fa27ce4SDimitry Andric 
1327fa27ce4SDimitry Andric   // Check if the modification time has changed.
1337fa27ce4SDimitry Andric   if (file_sp && file_sp->ModificationTimeIsStale()) {
1347fa27ce4SDimitry Andric     LLDB_LOG(log, "Modification time is stale: {0}", file_spec);
1357fa27ce4SDimitry Andric     file_sp.reset();
1367fa27ce4SDimitry Andric   }
1377fa27ce4SDimitry Andric 
1387fa27ce4SDimitry Andric   // Check if the file exists on disk.
1397fa27ce4SDimitry Andric   if (file_sp && !FileSystem::Instance().Exists(file_sp->GetFileSpec())) {
1407fa27ce4SDimitry Andric     LLDB_LOG(log, "File doesn't exist on disk: {0}", file_spec);
1417fa27ce4SDimitry Andric     file_sp.reset();
1427fa27ce4SDimitry Andric   }
1437fa27ce4SDimitry Andric 
1447fa27ce4SDimitry Andric   // If at this point we don't have a valid file, it means we either didn't find
1457fa27ce4SDimitry Andric   // it in the debugger cache or something caused it to be invalidated.
1467fa27ce4SDimitry Andric   if (!file_sp) {
1477fa27ce4SDimitry Andric     LLDB_LOG(log, "Creating and caching new source file: {0}", file_spec);
1487fa27ce4SDimitry Andric 
1497fa27ce4SDimitry Andric     // (Re)create the file.
1507fa27ce4SDimitry Andric     if (target_sp)
1517fa27ce4SDimitry Andric       file_sp = std::make_shared<File>(file_spec, target_sp);
1527fa27ce4SDimitry Andric     else
1537fa27ce4SDimitry Andric       file_sp = std::make_shared<File>(file_spec, debugger_sp);
1547fa27ce4SDimitry Andric 
1557fa27ce4SDimitry Andric     // Add the file to the debugger and process cache. If the file was
1567fa27ce4SDimitry Andric     // invalidated, this will overwrite it.
1577fa27ce4SDimitry Andric     debugger_sp->GetSourceFileCache().AddSourceFile(file_spec, file_sp);
1587fa27ce4SDimitry Andric     if (process_sp)
1597fa27ce4SDimitry Andric       process_sp->GetSourceFileCache().AddSourceFile(file_spec, file_sp);
1607fa27ce4SDimitry Andric   }
1617fa27ce4SDimitry Andric 
162f034231aSEd Maste   return file_sp;
163f034231aSEd Maste }
164f034231aSEd Maste 
should_highlight_source(DebuggerSP debugger_sp)16594994d37SDimitry Andric static bool should_highlight_source(DebuggerSP debugger_sp) {
16694994d37SDimitry Andric   if (!debugger_sp)
16794994d37SDimitry Andric     return false;
16894994d37SDimitry Andric 
16994994d37SDimitry Andric   // We don't use ANSI stop column formatting if the debugger doesn't think it
17094994d37SDimitry Andric   // should be using color.
17194994d37SDimitry Andric   if (!debugger_sp->GetUseColor())
17294994d37SDimitry Andric     return false;
17394994d37SDimitry Andric 
17494994d37SDimitry Andric   return debugger_sp->GetHighlightSource();
17594994d37SDimitry Andric }
17694994d37SDimitry Andric 
should_show_stop_column_with_ansi(DebuggerSP debugger_sp)17714f1b3e8SDimitry Andric static bool should_show_stop_column_with_ansi(DebuggerSP debugger_sp) {
17814f1b3e8SDimitry Andric   // We don't use ANSI stop column formatting if we can't lookup values from
17914f1b3e8SDimitry Andric   // the debugger.
18014f1b3e8SDimitry Andric   if (!debugger_sp)
18114f1b3e8SDimitry Andric     return false;
18214f1b3e8SDimitry Andric 
183f73363f1SDimitry Andric   // We don't use ANSI stop column formatting if the debugger doesn't think it
184f73363f1SDimitry Andric   // should be using color.
18514f1b3e8SDimitry Andric   if (!debugger_sp->GetUseColor())
18614f1b3e8SDimitry Andric     return false;
18714f1b3e8SDimitry Andric 
18814f1b3e8SDimitry Andric   // We only use ANSI stop column formatting if we're either supposed to show
18914f1b3e8SDimitry Andric   // ANSI where available (which we know we have when we get to this point), or
19014f1b3e8SDimitry Andric   // if we're only supposed to use ANSI.
19114f1b3e8SDimitry Andric   const auto value = debugger_sp->GetStopShowColumn();
19214f1b3e8SDimitry Andric   return ((value == eStopShowColumnAnsiOrCaret) ||
19314f1b3e8SDimitry Andric           (value == eStopShowColumnAnsi));
19414f1b3e8SDimitry Andric }
19514f1b3e8SDimitry Andric 
should_show_stop_column_with_caret(DebuggerSP debugger_sp)19614f1b3e8SDimitry Andric static bool should_show_stop_column_with_caret(DebuggerSP debugger_sp) {
19714f1b3e8SDimitry Andric   // We don't use text-based stop column formatting if we can't lookup values
19814f1b3e8SDimitry Andric   // from the debugger.
19914f1b3e8SDimitry Andric   if (!debugger_sp)
20014f1b3e8SDimitry Andric     return false;
20114f1b3e8SDimitry Andric 
202f73363f1SDimitry Andric   // If we're asked to show the first available of ANSI or caret, then we do
203f73363f1SDimitry Andric   // show the caret when ANSI is not available.
20414f1b3e8SDimitry Andric   const auto value = debugger_sp->GetStopShowColumn();
20514f1b3e8SDimitry Andric   if ((value == eStopShowColumnAnsiOrCaret) && !debugger_sp->GetUseColor())
20614f1b3e8SDimitry Andric     return true;
20714f1b3e8SDimitry Andric 
20814f1b3e8SDimitry Andric   // The only other time we use caret is if we're explicitly asked to show
20914f1b3e8SDimitry Andric   // caret.
21014f1b3e8SDimitry Andric   return value == eStopShowColumnCaret;
21114f1b3e8SDimitry Andric }
21214f1b3e8SDimitry Andric 
should_show_stop_line_with_ansi(DebuggerSP debugger_sp)213cfca06d7SDimitry Andric static bool should_show_stop_line_with_ansi(DebuggerSP debugger_sp) {
214cfca06d7SDimitry Andric   return debugger_sp && debugger_sp->GetUseColor();
215cfca06d7SDimitry Andric }
216cfca06d7SDimitry 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)21714f1b3e8SDimitry Andric size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile(
21814f1b3e8SDimitry Andric     uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
21914f1b3e8SDimitry Andric     const char *current_line_cstr, Stream *s,
22014f1b3e8SDimitry Andric     const SymbolContextList *bp_locs) {
221f034231aSEd Maste   if (count == 0)
222f034231aSEd Maste     return 0;
22394994d37SDimitry Andric 
22494994d37SDimitry Andric   Stream::ByteDelta delta(*s);
22594994d37SDimitry Andric 
22614f1b3e8SDimitry Andric   if (start_line == 0) {
227f034231aSEd Maste     if (m_last_line != 0 && m_last_line != UINT32_MAX)
228f034231aSEd Maste       start_line = m_last_line + m_last_count;
229f034231aSEd Maste     else
230f034231aSEd Maste       start_line = 1;
231f034231aSEd Maste   }
232f034231aSEd Maste 
23314f1b3e8SDimitry Andric   if (!m_default_set) {
234f034231aSEd Maste     FileSpec tmp_spec;
235f034231aSEd Maste     uint32_t tmp_line;
236f034231aSEd Maste     GetDefaultFileAndLine(tmp_spec, tmp_line);
237f034231aSEd Maste   }
238f034231aSEd Maste 
239f034231aSEd Maste   m_last_line = start_line;
240f034231aSEd Maste   m_last_count = count;
241f034231aSEd Maste 
242cfca06d7SDimitry Andric   if (FileSP last_file_sp = GetLastFile()) {
243f034231aSEd Maste     const uint32_t end_line = start_line + count - 1;
24414f1b3e8SDimitry Andric     for (uint32_t line = start_line; line <= end_line; ++line) {
245cfca06d7SDimitry Andric       if (!last_file_sp->LineIsValid(line)) {
246f034231aSEd Maste         m_last_line = UINT32_MAX;
247f034231aSEd Maste         break;
248f034231aSEd Maste       }
249f034231aSEd Maste 
250b60736ecSDimitry Andric       std::string prefix;
25114f1b3e8SDimitry Andric       if (bp_locs) {
252f034231aSEd Maste         uint32_t bp_count = bp_locs->NumLineEntriesWithLine(line);
253f034231aSEd Maste 
254f034231aSEd Maste         if (bp_count > 0)
255b60736ecSDimitry Andric           prefix = llvm::formatv("[{0}]", bp_count);
256f034231aSEd Maste         else
257b60736ecSDimitry Andric           prefix = "    ";
258f034231aSEd Maste       }
259f034231aSEd Maste 
260cfca06d7SDimitry Andric       char buffer[3];
2617fa27ce4SDimitry Andric       snprintf(buffer, sizeof(buffer), "%2.2s",
2627fa27ce4SDimitry Andric                (line == curr_line) ? current_line_cstr : "");
263cfca06d7SDimitry Andric       std::string current_line_highlight(buffer);
264cfca06d7SDimitry Andric 
265cfca06d7SDimitry Andric       auto debugger_sp = m_debugger_wp.lock();
266cfca06d7SDimitry Andric       if (should_show_stop_line_with_ansi(debugger_sp)) {
267cfca06d7SDimitry Andric         current_line_highlight = ansi::FormatAnsiTerminalCodes(
268cfca06d7SDimitry Andric             (debugger_sp->GetStopShowLineMarkerAnsiPrefix() +
269cfca06d7SDimitry Andric              current_line_highlight +
270cfca06d7SDimitry Andric              debugger_sp->GetStopShowLineMarkerAnsiSuffix())
271cfca06d7SDimitry Andric                 .str());
272cfca06d7SDimitry Andric       }
273cfca06d7SDimitry Andric 
274b60736ecSDimitry Andric       s->Printf("%s%s %-4u\t", prefix.c_str(), current_line_highlight.c_str(),
275b60736ecSDimitry Andric                 line);
27694994d37SDimitry Andric 
27794994d37SDimitry Andric       // So far we treated column 0 as a special 'no column value', but
27894994d37SDimitry Andric       // DisplaySourceLines starts counting columns from 0 (and no column is
27994994d37SDimitry Andric       // expressed by passing an empty optional).
280e3b55780SDimitry Andric       std::optional<size_t> columnToHighlight;
28194994d37SDimitry Andric       if (line == curr_line && column)
28294994d37SDimitry Andric         columnToHighlight = column - 1;
28394994d37SDimitry Andric 
28494994d37SDimitry Andric       size_t this_line_size =
285cfca06d7SDimitry Andric           last_file_sp->DisplaySourceLines(line, columnToHighlight, 0, 0, s);
28614f1b3e8SDimitry Andric       if (column != 0 && line == curr_line &&
287cfca06d7SDimitry Andric           should_show_stop_column_with_caret(debugger_sp)) {
28814f1b3e8SDimitry Andric         // Display caret cursor.
28914f1b3e8SDimitry Andric         std::string src_line;
290cfca06d7SDimitry Andric         last_file_sp->GetLine(line, src_line);
29194994d37SDimitry Andric         s->Printf("    \t");
29214f1b3e8SDimitry Andric         // Insert a space for every non-tab character in the source line.
29314f1b3e8SDimitry Andric         for (size_t i = 0; i + 1 < column && i < src_line.length(); ++i)
29494994d37SDimitry Andric           s->PutChar(src_line[i] == '\t' ? '\t' : ' ');
29514f1b3e8SDimitry Andric         // Now add the caret.
29694994d37SDimitry Andric         s->Printf("^\n");
29714f1b3e8SDimitry Andric       }
29814f1b3e8SDimitry Andric       if (this_line_size == 0) {
299f034231aSEd Maste         m_last_line = UINT32_MAX;
300f034231aSEd Maste         break;
301f034231aSEd Maste       }
302f034231aSEd Maste     }
30394994d37SDimitry Andric   }
30494994d37SDimitry Andric   return *delta;
305f034231aSEd Maste }
306f034231aSEd Maste 
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)30714f1b3e8SDimitry Andric size_t SourceManager::DisplaySourceLinesWithLineNumbers(
30814f1b3e8SDimitry Andric     const FileSpec &file_spec, uint32_t line, uint32_t column,
30914f1b3e8SDimitry Andric     uint32_t context_before, uint32_t context_after,
31014f1b3e8SDimitry Andric     const char *current_line_cstr, Stream *s,
31114f1b3e8SDimitry Andric     const SymbolContextList *bp_locs) {
312f034231aSEd Maste   FileSP file_sp(GetFile(file_spec));
313f034231aSEd Maste 
314f034231aSEd Maste   uint32_t start_line;
315f034231aSEd Maste   uint32_t count = context_before + context_after + 1;
316f034231aSEd Maste   if (line > context_before)
317f034231aSEd Maste     start_line = line - context_before;
318f034231aSEd Maste   else
319f034231aSEd Maste     start_line = 1;
320f034231aSEd Maste 
321cfca06d7SDimitry Andric   FileSP last_file_sp(GetLastFile());
322cfca06d7SDimitry Andric   if (last_file_sp.get() != file_sp.get()) {
323f034231aSEd Maste     if (line == 0)
324f034231aSEd Maste       m_last_line = 0;
325cfca06d7SDimitry Andric     m_last_file_spec = file_spec;
326f034231aSEd Maste   }
32714f1b3e8SDimitry Andric   return DisplaySourceLinesWithLineNumbersUsingLastFile(
32814f1b3e8SDimitry Andric       start_line, count, line, column, current_line_cstr, s, bp_locs);
329f034231aSEd Maste }
330f034231aSEd Maste 
DisplayMoreWithLineNumbers(Stream * s,uint32_t count,bool reverse,const SymbolContextList * bp_locs)33114f1b3e8SDimitry Andric size_t SourceManager::DisplayMoreWithLineNumbers(
33214f1b3e8SDimitry Andric     Stream *s, uint32_t count, bool reverse, const SymbolContextList *bp_locs) {
33314f1b3e8SDimitry Andric   // If we get called before anybody has set a default file and line, then try
33414f1b3e8SDimitry Andric   // to figure it out here.
335cfca06d7SDimitry Andric   FileSP last_file_sp(GetLastFile());
336cfca06d7SDimitry Andric   const bool have_default_file_line = last_file_sp && m_last_line > 0;
33714f1b3e8SDimitry Andric   if (!m_default_set) {
338f034231aSEd Maste     FileSpec tmp_spec;
339f034231aSEd Maste     uint32_t tmp_line;
340f034231aSEd Maste     GetDefaultFileAndLine(tmp_spec, tmp_line);
341f034231aSEd Maste   }
342f034231aSEd Maste 
343cfca06d7SDimitry Andric   if (last_file_sp) {
344f034231aSEd Maste     if (m_last_line == UINT32_MAX)
345f034231aSEd Maste       return 0;
346f034231aSEd Maste 
347f034231aSEd Maste     if (reverse && m_last_line == 1)
348f034231aSEd Maste       return 0;
349f034231aSEd Maste 
350f034231aSEd Maste     if (count > 0)
351f034231aSEd Maste       m_last_count = count;
352f034231aSEd Maste     else if (m_last_count == 0)
353f034231aSEd Maste       m_last_count = 10;
354f034231aSEd Maste 
35514f1b3e8SDimitry Andric     if (m_last_line > 0) {
35614f1b3e8SDimitry Andric       if (reverse) {
357f73363f1SDimitry Andric         // If this is the first time we've done a reverse, then back up one
358f73363f1SDimitry Andric         // more time so we end up showing the chunk before the last one we've
359f73363f1SDimitry Andric         // shown:
360f034231aSEd Maste         if (m_last_line > m_last_count)
361f034231aSEd Maste           m_last_line -= m_last_count;
362f034231aSEd Maste         else
363f034231aSEd Maste           m_last_line = 1;
36414f1b3e8SDimitry Andric       } else if (have_default_file_line)
365f034231aSEd Maste         m_last_line += m_last_count;
36614f1b3e8SDimitry Andric     } else
367f034231aSEd Maste       m_last_line = 1;
368f034231aSEd Maste 
36914f1b3e8SDimitry Andric     const uint32_t column = 0;
37014f1b3e8SDimitry Andric     return DisplaySourceLinesWithLineNumbersUsingLastFile(
37114f1b3e8SDimitry Andric         m_last_line, m_last_count, UINT32_MAX, column, "", s, bp_locs);
372f034231aSEd Maste   }
373f034231aSEd Maste   return 0;
374f034231aSEd Maste }
375f034231aSEd Maste 
SetDefaultFileAndLine(const FileSpec & file_spec,uint32_t line)37614f1b3e8SDimitry Andric bool SourceManager::SetDefaultFileAndLine(const FileSpec &file_spec,
37714f1b3e8SDimitry Andric                                           uint32_t line) {
378f034231aSEd Maste   m_default_set = true;
379cfca06d7SDimitry Andric   FileSP file_sp(GetFile(file_spec));
380cfca06d7SDimitry Andric 
381cfca06d7SDimitry Andric   if (file_sp) {
382f034231aSEd Maste     m_last_line = line;
383cfca06d7SDimitry Andric     m_last_file_spec = file_spec;
384f034231aSEd Maste     return true;
38514f1b3e8SDimitry Andric   } else {
386f034231aSEd Maste     return false;
387f034231aSEd Maste   }
388f034231aSEd Maste }
389f034231aSEd Maste 
GetDefaultFileAndLine(FileSpec & file_spec,uint32_t & line)39014f1b3e8SDimitry Andric bool SourceManager::GetDefaultFileAndLine(FileSpec &file_spec, uint32_t &line) {
391cfca06d7SDimitry Andric   if (FileSP last_file_sp = GetLastFile()) {
392cfca06d7SDimitry Andric     file_spec = m_last_file_spec;
393f034231aSEd Maste     line = m_last_line;
394f034231aSEd Maste     return true;
39514f1b3e8SDimitry Andric   } else if (!m_default_set) {
396f034231aSEd Maste     TargetSP target_sp(m_target_wp.lock());
397f034231aSEd Maste 
39814f1b3e8SDimitry Andric     if (target_sp) {
39914f1b3e8SDimitry Andric       // If nobody has set the default file and line then try here.  If there's
400f73363f1SDimitry Andric       // no executable, then we will try again later when there is one.
401f73363f1SDimitry Andric       // Otherwise, if we can't find it we won't look again, somebody will have
402f73363f1SDimitry Andric       // to set it (for instance when we stop somewhere...)
403f034231aSEd Maste       Module *executable_ptr = target_sp->GetExecutableModulePointer();
40414f1b3e8SDimitry Andric       if (executable_ptr) {
405f034231aSEd Maste         SymbolContextList sc_list;
406f034231aSEd Maste         ConstString main_name("main");
407c0981da4SDimitry Andric 
408c0981da4SDimitry Andric         ModuleFunctionSearchOptions function_options;
409c0981da4SDimitry Andric         function_options.include_symbols =
410c0981da4SDimitry Andric             false; // Force it to be a debug symbol.
411c0981da4SDimitry Andric         function_options.include_inlines = true;
412cfca06d7SDimitry Andric         executable_ptr->FindFunctions(main_name, CompilerDeclContext(),
413c0981da4SDimitry Andric                                       lldb::eFunctionNameTypeBase,
414c0981da4SDimitry Andric                                       function_options, sc_list);
4157fa27ce4SDimitry Andric         for (const SymbolContext &sc : sc_list) {
41614f1b3e8SDimitry Andric           if (sc.function) {
417f034231aSEd Maste             lldb_private::LineEntry line_entry;
41814f1b3e8SDimitry Andric             if (sc.function->GetAddressRange()
41914f1b3e8SDimitry Andric                     .GetBaseAddress()
42014f1b3e8SDimitry Andric                     .CalculateSymbolContextLineEntry(line_entry)) {
421ac9a064cSDimitry Andric               SetDefaultFileAndLine(line_entry.GetFile(), line_entry.line);
422cfca06d7SDimitry Andric               file_spec = m_last_file_spec;
423f034231aSEd Maste               line = m_last_line;
424f034231aSEd Maste               return true;
425f034231aSEd Maste             }
426f034231aSEd Maste           }
427f034231aSEd Maste         }
428f034231aSEd Maste       }
429f034231aSEd Maste     }
430f034231aSEd Maste   }
431f034231aSEd Maste   return false;
432f034231aSEd Maste }
433f034231aSEd Maste 
FindLinesMatchingRegex(FileSpec & file_spec,RegularExpression & regex,uint32_t start_line,uint32_t end_line,std::vector<uint32_t> & match_lines)43414f1b3e8SDimitry Andric void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
435f034231aSEd Maste                                            RegularExpression &regex,
436f034231aSEd Maste                                            uint32_t start_line,
437f034231aSEd Maste                                            uint32_t end_line,
43814f1b3e8SDimitry Andric                                            std::vector<uint32_t> &match_lines) {
439f034231aSEd Maste   match_lines.clear();
440f034231aSEd Maste   FileSP file_sp = GetFile(file_spec);
441f034231aSEd Maste   if (!file_sp)
442f034231aSEd Maste     return;
44314f1b3e8SDimitry Andric   return file_sp->FindLinesMatchingRegex(regex, start_line, end_line,
44414f1b3e8SDimitry Andric                                          match_lines);
445f034231aSEd Maste }
446f034231aSEd Maste 
File(const FileSpec & file_spec,lldb::DebuggerSP debugger_sp)44714f1b3e8SDimitry Andric SourceManager::File::File(const FileSpec &file_spec,
44814f1b3e8SDimitry Andric                           lldb::DebuggerSP debugger_sp)
4497fa27ce4SDimitry Andric     : m_file_spec_orig(file_spec), m_file_spec(), m_mod_time(),
4507fa27ce4SDimitry Andric       m_debugger_wp(debugger_sp), m_target_wp(TargetSP()) {
4517fa27ce4SDimitry Andric   CommonInitializer(file_spec, {});
45214f1b3e8SDimitry Andric }
45314f1b3e8SDimitry Andric 
File(const FileSpec & file_spec,TargetSP target_sp)4547fa27ce4SDimitry Andric SourceManager::File::File(const FileSpec &file_spec, TargetSP target_sp)
4557fa27ce4SDimitry Andric     : m_file_spec_orig(file_spec), m_file_spec(), m_mod_time(),
4567fa27ce4SDimitry Andric       m_debugger_wp(target_sp ? target_sp->GetDebugger().shared_from_this()
4577fa27ce4SDimitry Andric                               : DebuggerSP()),
4587fa27ce4SDimitry Andric       m_target_wp(target_sp) {
4597fa27ce4SDimitry Andric   CommonInitializer(file_spec, target_sp);
46014f1b3e8SDimitry Andric }
46114f1b3e8SDimitry Andric 
CommonInitializer(const FileSpec & file_spec,TargetSP target_sp)46214f1b3e8SDimitry Andric void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
4637fa27ce4SDimitry Andric                                             TargetSP target_sp) {
4647fa27ce4SDimitry Andric   // Set the file and update the modification time.
4657fa27ce4SDimitry Andric   SetFileSpec(file_spec);
466f034231aSEd Maste 
4677fa27ce4SDimitry Andric   // Always update the source map modification ID if we have a target.
4687fa27ce4SDimitry Andric   if (target_sp)
4697fa27ce4SDimitry Andric     m_source_map_mod_id = target_sp->GetSourcePathMap().GetModificationID();
4707fa27ce4SDimitry Andric 
4717fa27ce4SDimitry Andric   // File doesn't exist.
4727fa27ce4SDimitry Andric   if (m_mod_time == llvm::sys::TimePoint<>()) {
4737fa27ce4SDimitry Andric     if (target_sp) {
4747fa27ce4SDimitry Andric       // If this is just a file name, try finding it in the target.
47514f1b3e8SDimitry Andric       if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
476f034231aSEd Maste         bool check_inlines = false;
477f034231aSEd Maste         SymbolContextList sc_list;
47814f1b3e8SDimitry Andric         size_t num_matches =
4797fa27ce4SDimitry Andric             target_sp->GetImages().ResolveSymbolContextForFilePath(
48014f1b3e8SDimitry Andric                 file_spec.GetFilename().AsCString(), 0, check_inlines,
48194994d37SDimitry Andric                 SymbolContextItem(eSymbolContextModule |
48294994d37SDimitry Andric                                   eSymbolContextCompUnit),
483f034231aSEd Maste                 sc_list);
484f034231aSEd Maste         bool got_multiple = false;
48514f1b3e8SDimitry Andric         if (num_matches != 0) {
48614f1b3e8SDimitry Andric           if (num_matches > 1) {
487706b4fc4SDimitry Andric             CompileUnit *test_cu = nullptr;
4887fa27ce4SDimitry Andric             for (const SymbolContext &sc : sc_list) {
48914f1b3e8SDimitry Andric               if (sc.comp_unit) {
490706b4fc4SDimitry Andric                 if (test_cu) {
491706b4fc4SDimitry Andric                   if (test_cu != sc.comp_unit)
492f034231aSEd Maste                     got_multiple = true;
493f034231aSEd Maste                   break;
49414f1b3e8SDimitry Andric                 } else
495706b4fc4SDimitry Andric                   test_cu = sc.comp_unit;
496f034231aSEd Maste               }
497f034231aSEd Maste             }
498f034231aSEd Maste           }
49914f1b3e8SDimitry Andric           if (!got_multiple) {
500f034231aSEd Maste             SymbolContext sc;
501f034231aSEd Maste             sc_list.GetContextAtIndex(0, sc);
502706b4fc4SDimitry Andric             if (sc.comp_unit)
5037fa27ce4SDimitry Andric               SetFileSpec(sc.comp_unit->GetPrimaryFile());
504f034231aSEd Maste           }
505f034231aSEd Maste         }
506f034231aSEd Maste       }
507f034231aSEd Maste 
5087fa27ce4SDimitry Andric       // Try remapping the file if it doesn't exist.
5097fa27ce4SDimitry Andric       if (!FileSystem::Instance().Exists(m_file_spec)) {
5107fa27ce4SDimitry Andric         // Check target specific source remappings (i.e., the
5117fa27ce4SDimitry Andric         // target.source-map setting), then fall back to the module
5127fa27ce4SDimitry Andric         // specific remapping (i.e., the .dSYM remapping dictionary).
5137fa27ce4SDimitry Andric         auto remapped = target_sp->GetSourcePathMap().FindFile(m_file_spec);
5147fa27ce4SDimitry Andric         if (!remapped) {
5157fa27ce4SDimitry Andric           FileSpec new_spec;
5167fa27ce4SDimitry Andric           if (target_sp->GetImages().FindSourceFile(m_file_spec, new_spec))
5177fa27ce4SDimitry Andric             remapped = new_spec;
5187fa27ce4SDimitry Andric         }
5197fa27ce4SDimitry Andric         if (remapped)
5207fa27ce4SDimitry Andric           SetFileSpec(*remapped);
5217fa27ce4SDimitry Andric       }
5227fa27ce4SDimitry Andric     }
5237fa27ce4SDimitry Andric   }
5247fa27ce4SDimitry Andric 
5257fa27ce4SDimitry Andric   // If the file exists, read in the data.
52614f1b3e8SDimitry Andric   if (m_mod_time != llvm::sys::TimePoint<>())
52794994d37SDimitry Andric     m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
528f034231aSEd Maste }
529f034231aSEd Maste 
SetFileSpec(FileSpec file_spec)5307fa27ce4SDimitry Andric void SourceManager::File::SetFileSpec(FileSpec file_spec) {
5317fa27ce4SDimitry Andric   resolve_tilde(file_spec);
5327fa27ce4SDimitry Andric   m_file_spec = std::move(file_spec);
5337fa27ce4SDimitry Andric   m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
5347fa27ce4SDimitry Andric }
5357fa27ce4SDimitry Andric 
GetLineOffset(uint32_t line)53614f1b3e8SDimitry Andric uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
537f034231aSEd Maste   if (line == 0)
538f034231aSEd Maste     return UINT32_MAX;
539f034231aSEd Maste 
540f034231aSEd Maste   if (line == 1)
541f034231aSEd Maste     return 0;
542f034231aSEd Maste 
54314f1b3e8SDimitry Andric   if (CalculateLineOffsets(line)) {
544f034231aSEd Maste     if (line < m_offsets.size())
545f034231aSEd Maste       return m_offsets[line - 1]; // yes we want "line - 1" in the index
546f034231aSEd Maste   }
547f034231aSEd Maste   return UINT32_MAX;
548f034231aSEd Maste }
549f034231aSEd Maste 
GetNumLines()55014f1b3e8SDimitry Andric uint32_t SourceManager::File::GetNumLines() {
551866dcdacSEd Maste   CalculateLineOffsets();
552866dcdacSEd Maste   return m_offsets.size();
553866dcdacSEd Maste }
554866dcdacSEd Maste 
PeekLineData(uint32_t line)55514f1b3e8SDimitry Andric const char *SourceManager::File::PeekLineData(uint32_t line) {
556866dcdacSEd Maste   if (!LineIsValid(line))
5575f29bb8aSDimitry Andric     return nullptr;
558866dcdacSEd Maste 
559866dcdacSEd Maste   size_t line_offset = GetLineOffset(line);
560866dcdacSEd Maste   if (line_offset < m_data_sp->GetByteSize())
561866dcdacSEd Maste     return (const char *)m_data_sp->GetBytes() + line_offset;
5625f29bb8aSDimitry Andric   return nullptr;
563866dcdacSEd Maste }
564866dcdacSEd Maste 
GetLineLength(uint32_t line,bool include_newline_chars)56514f1b3e8SDimitry Andric uint32_t SourceManager::File::GetLineLength(uint32_t line,
56614f1b3e8SDimitry Andric                                             bool include_newline_chars) {
567866dcdacSEd Maste   if (!LineIsValid(line))
568866dcdacSEd Maste     return false;
569866dcdacSEd Maste 
570866dcdacSEd Maste   size_t start_offset = GetLineOffset(line);
571866dcdacSEd Maste   size_t end_offset = GetLineOffset(line + 1);
572866dcdacSEd Maste   if (end_offset == UINT32_MAX)
573866dcdacSEd Maste     end_offset = m_data_sp->GetByteSize();
574866dcdacSEd Maste 
57514f1b3e8SDimitry Andric   if (end_offset > start_offset) {
576866dcdacSEd Maste     uint32_t length = end_offset - start_offset;
57794994d37SDimitry Andric     if (!include_newline_chars) {
57814f1b3e8SDimitry Andric       const char *line_start =
57914f1b3e8SDimitry Andric           (const char *)m_data_sp->GetBytes() + start_offset;
58014f1b3e8SDimitry Andric       while (length > 0) {
581866dcdacSEd Maste         const char last_char = line_start[length - 1];
582866dcdacSEd Maste         if ((last_char == '\r') || (last_char == '\n'))
583866dcdacSEd Maste           --length;
584866dcdacSEd Maste         else
585866dcdacSEd Maste           break;
586866dcdacSEd Maste       }
587866dcdacSEd Maste     }
588866dcdacSEd Maste     return length;
589866dcdacSEd Maste   }
590866dcdacSEd Maste   return 0;
591866dcdacSEd Maste }
592866dcdacSEd Maste 
LineIsValid(uint32_t line)59314f1b3e8SDimitry Andric bool SourceManager::File::LineIsValid(uint32_t line) {
594f034231aSEd Maste   if (line == 0)
595f034231aSEd Maste     return false;
596f034231aSEd Maste 
597f034231aSEd Maste   if (CalculateLineOffsets(line))
598f034231aSEd Maste     return line < m_offsets.size();
599f034231aSEd Maste   return false;
600f034231aSEd Maste }
601f034231aSEd Maste 
ModificationTimeIsStale() const6027fa27ce4SDimitry Andric bool SourceManager::File::ModificationTimeIsStale() const {
603f034231aSEd Maste   // TODO: use host API to sign up for file modifications to anything in our
604f034231aSEd Maste   // source cache and only update when we determine a file has been updated.
605f034231aSEd Maste   // For now we check each time we want to display info for the file.
60694994d37SDimitry Andric   auto curr_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
6077fa27ce4SDimitry Andric   return curr_mod_time != llvm::sys::TimePoint<>() &&
6087fa27ce4SDimitry Andric          m_mod_time != curr_mod_time;
609f034231aSEd Maste }
6107fa27ce4SDimitry Andric 
PathRemappingIsStale() const6117fa27ce4SDimitry Andric bool SourceManager::File::PathRemappingIsStale() const {
6127fa27ce4SDimitry Andric   if (TargetSP target_sp = m_target_wp.lock())
6137fa27ce4SDimitry Andric     return GetSourceMapModificationID() !=
6147fa27ce4SDimitry Andric            target_sp->GetSourcePathMap().GetModificationID();
6157fa27ce4SDimitry Andric   return false;
616e81d9d49SDimitry Andric }
617f034231aSEd Maste 
DisplaySourceLines(uint32_t line,std::optional<size_t> column,uint32_t context_before,uint32_t context_after,Stream * s)61894994d37SDimitry Andric size_t SourceManager::File::DisplaySourceLines(uint32_t line,
619e3b55780SDimitry Andric                                                std::optional<size_t> column,
62014f1b3e8SDimitry Andric                                                uint32_t context_before,
62114f1b3e8SDimitry Andric                                                uint32_t context_after,
62214f1b3e8SDimitry Andric                                                Stream *s) {
62314f1b3e8SDimitry Andric   // Nothing to write if there's no stream.
62414f1b3e8SDimitry Andric   if (!s)
62514f1b3e8SDimitry Andric     return 0;
62614f1b3e8SDimitry Andric 
627f034231aSEd Maste   // Sanity check m_data_sp before proceeding.
628f034231aSEd Maste   if (!m_data_sp)
629f034231aSEd Maste     return 0;
630f034231aSEd Maste 
63194994d37SDimitry Andric   size_t bytes_written = s->GetWrittenBytes();
63294994d37SDimitry Andric 
63394994d37SDimitry Andric   auto debugger_sp = m_debugger_wp.lock();
63494994d37SDimitry Andric 
63594994d37SDimitry Andric   HighlightStyle style;
63694994d37SDimitry Andric   // Use the default Vim style if source highlighting is enabled.
63794994d37SDimitry Andric   if (should_highlight_source(debugger_sp))
63894994d37SDimitry Andric     style = HighlightStyle::MakeVimStyle();
63994994d37SDimitry Andric 
64094994d37SDimitry Andric   // If we should mark the stop column with color codes, then copy the prefix
64194994d37SDimitry Andric   // and suffix to our color style.
64294994d37SDimitry Andric   if (should_show_stop_column_with_ansi(debugger_sp))
64394994d37SDimitry Andric     style.selected.Set(debugger_sp->GetStopShowColumnAnsiPrefix(),
64494994d37SDimitry Andric                        debugger_sp->GetStopShowColumnAnsiSuffix());
64594994d37SDimitry Andric 
64694994d37SDimitry Andric   HighlighterManager mgr;
64794994d37SDimitry Andric   std::string path = GetFileSpec().GetPath(/*denormalize*/ false);
64894994d37SDimitry Andric   // FIXME: Find a way to get the definitive language this file was written in
64994994d37SDimitry Andric   // and pass it to the highlighter.
65094994d37SDimitry Andric   const auto &h = mgr.getHighlighterFor(lldb::eLanguageTypeUnknown, path);
65194994d37SDimitry Andric 
65214f1b3e8SDimitry Andric   const uint32_t start_line =
65314f1b3e8SDimitry Andric       line <= context_before ? 1 : line - context_before;
654f034231aSEd Maste   const uint32_t start_line_offset = GetLineOffset(start_line);
65514f1b3e8SDimitry Andric   if (start_line_offset != UINT32_MAX) {
656f034231aSEd Maste     const uint32_t end_line = line + context_after;
657f034231aSEd Maste     uint32_t end_line_offset = GetLineOffset(end_line + 1);
658f034231aSEd Maste     if (end_line_offset == UINT32_MAX)
659f034231aSEd Maste       end_line_offset = m_data_sp->GetByteSize();
660f034231aSEd Maste 
661f034231aSEd Maste     assert(start_line_offset <= end_line_offset);
66214f1b3e8SDimitry Andric     if (start_line_offset < end_line_offset) {
663f034231aSEd Maste       size_t count = end_line_offset - start_line_offset;
664f034231aSEd Maste       const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
66514f1b3e8SDimitry Andric 
66694994d37SDimitry Andric       auto ref = llvm::StringRef(reinterpret_cast<const char *>(cstr), count);
66714f1b3e8SDimitry Andric 
66894994d37SDimitry Andric       h.Highlight(style, ref, column, "", *s);
66914f1b3e8SDimitry Andric 
67014f1b3e8SDimitry Andric       // Ensure we get an end of line character one way or another.
67194994d37SDimitry Andric       if (!is_newline_char(ref.back()))
67294994d37SDimitry Andric         s->EOL();
673f034231aSEd Maste     }
674f034231aSEd Maste   }
67594994d37SDimitry Andric   return s->GetWrittenBytes() - bytes_written;
676f034231aSEd Maste }
677f034231aSEd Maste 
FindLinesMatchingRegex(RegularExpression & regex,uint32_t start_line,uint32_t end_line,std::vector<uint32_t> & match_lines)67814f1b3e8SDimitry Andric void SourceManager::File::FindLinesMatchingRegex(
67914f1b3e8SDimitry Andric     RegularExpression &regex, uint32_t start_line, uint32_t end_line,
68014f1b3e8SDimitry Andric     std::vector<uint32_t> &match_lines) {
681f034231aSEd Maste   match_lines.clear();
682f034231aSEd Maste 
68314f1b3e8SDimitry Andric   if (!LineIsValid(start_line) ||
68414f1b3e8SDimitry Andric       (end_line != UINT32_MAX && !LineIsValid(end_line)))
685f034231aSEd Maste     return;
686f034231aSEd Maste   if (start_line > end_line)
687f034231aSEd Maste     return;
688f034231aSEd Maste 
68914f1b3e8SDimitry Andric   for (uint32_t line_no = start_line; line_no < end_line; line_no++) {
690f034231aSEd Maste     std::string buffer;
691f034231aSEd Maste     if (!GetLine(line_no, buffer))
692f034231aSEd Maste       break;
69314f1b3e8SDimitry Andric     if (regex.Execute(buffer)) {
694f034231aSEd Maste       match_lines.push_back(line_no);
695f034231aSEd Maste     }
696f034231aSEd Maste   }
697f034231aSEd Maste }
698f034231aSEd Maste 
operator ==(const SourceManager::File & lhs,const SourceManager::File & rhs)69914f1b3e8SDimitry Andric bool lldb_private::operator==(const SourceManager::File &lhs,
70014f1b3e8SDimitry Andric                               const SourceManager::File &rhs) {
70114f1b3e8SDimitry Andric   if (lhs.m_file_spec != rhs.m_file_spec)
70214f1b3e8SDimitry Andric     return false;
703f034231aSEd Maste   return lhs.m_mod_time == rhs.m_mod_time;
704f034231aSEd Maste }
705f034231aSEd Maste 
CalculateLineOffsets(uint32_t line)70614f1b3e8SDimitry Andric bool SourceManager::File::CalculateLineOffsets(uint32_t line) {
70714f1b3e8SDimitry Andric   line =
70814f1b3e8SDimitry Andric       UINT32_MAX; // TODO: take this line out when we support partial indexing
70914f1b3e8SDimitry Andric   if (line == UINT32_MAX) {
710f034231aSEd Maste     // Already done?
711f034231aSEd Maste     if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
712f034231aSEd Maste       return true;
713f034231aSEd Maste 
71414f1b3e8SDimitry Andric     if (m_offsets.empty()) {
7155f29bb8aSDimitry Andric       if (m_data_sp.get() == nullptr)
716f034231aSEd Maste         return false;
717f034231aSEd Maste 
718145449b1SDimitry Andric       const char *start = (const char *)m_data_sp->GetBytes();
71914f1b3e8SDimitry Andric       if (start) {
720f034231aSEd Maste         const char *end = start + m_data_sp->GetByteSize();
721f034231aSEd Maste 
722f034231aSEd Maste         // Calculate all line offsets from scratch
723f034231aSEd Maste 
72414f1b3e8SDimitry Andric         // Push a 1 at index zero to indicate the file has been completely
72514f1b3e8SDimitry Andric         // indexed.
726f034231aSEd Maste         m_offsets.push_back(UINT32_MAX);
727f21a844fSEd Maste         const char *s;
72814f1b3e8SDimitry Andric         for (s = start; s < end; ++s) {
729f21a844fSEd Maste           char curr_ch = *s;
73014f1b3e8SDimitry Andric           if (is_newline_char(curr_ch)) {
73114f1b3e8SDimitry Andric             if (s + 1 < end) {
732f21a844fSEd Maste               char next_ch = s[1];
73314f1b3e8SDimitry Andric               if (is_newline_char(next_ch)) {
734f034231aSEd Maste                 if (curr_ch != next_ch)
735f034231aSEd Maste                   ++s;
736f034231aSEd Maste               }
737f034231aSEd Maste             }
738f034231aSEd Maste             m_offsets.push_back(s + 1 - start);
739f034231aSEd Maste           }
740f034231aSEd Maste         }
74114f1b3e8SDimitry Andric         if (!m_offsets.empty()) {
74214f1b3e8SDimitry Andric           if (m_offsets.back() < size_t(end - start))
743f034231aSEd Maste             m_offsets.push_back(end - start);
744f034231aSEd Maste         }
745f034231aSEd Maste         return true;
746f034231aSEd Maste       }
74714f1b3e8SDimitry Andric     } else {
748f034231aSEd Maste       // Some lines have been populated, start where we last left off
749e81d9d49SDimitry Andric       assert("Not implemented yet" && false);
750f034231aSEd Maste     }
751f034231aSEd Maste 
75214f1b3e8SDimitry Andric   } else {
753f034231aSEd Maste     // Calculate all line offsets up to "line"
754e81d9d49SDimitry Andric     assert("Not implemented yet" && false);
755f034231aSEd Maste   }
756f034231aSEd Maste   return false;
757f034231aSEd Maste }
758f034231aSEd Maste 
GetLine(uint32_t line_no,std::string & buffer)75914f1b3e8SDimitry Andric bool SourceManager::File::GetLine(uint32_t line_no, std::string &buffer) {
760f034231aSEd Maste   if (!LineIsValid(line_no))
761f034231aSEd Maste     return false;
762f034231aSEd Maste 
763f034231aSEd Maste   size_t start_offset = GetLineOffset(line_no);
764f034231aSEd Maste   size_t end_offset = GetLineOffset(line_no + 1);
76514f1b3e8SDimitry Andric   if (end_offset == UINT32_MAX) {
766f034231aSEd Maste     end_offset = m_data_sp->GetByteSize();
767f034231aSEd Maste   }
768145449b1SDimitry Andric   buffer.assign((const char *)m_data_sp->GetBytes() + start_offset,
76914f1b3e8SDimitry Andric                 end_offset - start_offset);
770f034231aSEd Maste 
771f034231aSEd Maste   return true;
772f034231aSEd Maste }
773f034231aSEd Maste 
AddSourceFile(const FileSpec & file_spec,FileSP file_sp)7747fa27ce4SDimitry Andric void SourceManager::SourceFileCache::AddSourceFile(const FileSpec &file_spec,
7757fa27ce4SDimitry Andric                                                    FileSP file_sp) {
7767fa27ce4SDimitry Andric   llvm::sys::ScopedWriter guard(m_mutex);
7777fa27ce4SDimitry Andric 
7787fa27ce4SDimitry Andric   assert(file_sp && "invalid FileSP");
7797fa27ce4SDimitry Andric 
7807fa27ce4SDimitry Andric   AddSourceFileImpl(file_spec, file_sp);
7817fa27ce4SDimitry Andric   const FileSpec &resolved_file_spec = file_sp->GetFileSpec();
7827fa27ce4SDimitry Andric   if (file_spec != resolved_file_spec)
7837fa27ce4SDimitry Andric     AddSourceFileImpl(file_sp->GetFileSpec(), file_sp);
7847fa27ce4SDimitry Andric }
7857fa27ce4SDimitry Andric 
RemoveSourceFile(const FileSP & file_sp)7867fa27ce4SDimitry Andric void SourceManager::SourceFileCache::RemoveSourceFile(const FileSP &file_sp) {
7877fa27ce4SDimitry Andric   llvm::sys::ScopedWriter guard(m_mutex);
7887fa27ce4SDimitry Andric 
7897fa27ce4SDimitry Andric   assert(file_sp && "invalid FileSP");
7907fa27ce4SDimitry Andric 
7917fa27ce4SDimitry Andric   // Iterate over all the elements in the cache.
7927fa27ce4SDimitry Andric   // This is expensive but a relatively uncommon operation.
7937fa27ce4SDimitry Andric   auto it = m_file_cache.begin();
7947fa27ce4SDimitry Andric   while (it != m_file_cache.end()) {
7957fa27ce4SDimitry Andric     if (it->second == file_sp)
7967fa27ce4SDimitry Andric       it = m_file_cache.erase(it);
7977fa27ce4SDimitry Andric     else
7987fa27ce4SDimitry Andric       it++;
7997fa27ce4SDimitry Andric   }
8007fa27ce4SDimitry Andric }
8017fa27ce4SDimitry Andric 
AddSourceFileImpl(const FileSpec & file_spec,FileSP file_sp)8027fa27ce4SDimitry Andric void SourceManager::SourceFileCache::AddSourceFileImpl(
8037fa27ce4SDimitry Andric     const FileSpec &file_spec, FileSP file_sp) {
804f034231aSEd Maste   FileCache::iterator pos = m_file_cache.find(file_spec);
8057fa27ce4SDimitry Andric   if (pos == m_file_cache.end()) {
806f034231aSEd Maste     m_file_cache[file_spec] = file_sp;
8077fa27ce4SDimitry Andric   } else {
808f034231aSEd Maste     if (file_sp != pos->second)
809f034231aSEd Maste       m_file_cache[file_spec] = file_sp;
810f034231aSEd Maste   }
811f034231aSEd Maste }
812f034231aSEd Maste 
FindSourceFile(const FileSpec & file_spec) const81314f1b3e8SDimitry Andric SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile(
81414f1b3e8SDimitry Andric     const FileSpec &file_spec) const {
8157fa27ce4SDimitry Andric   llvm::sys::ScopedReader guard(m_mutex);
8167fa27ce4SDimitry Andric 
817f034231aSEd Maste   FileCache::const_iterator pos = m_file_cache.find(file_spec);
818f034231aSEd Maste   if (pos != m_file_cache.end())
8197fa27ce4SDimitry Andric     return pos->second;
8207fa27ce4SDimitry Andric   return {};
8217fa27ce4SDimitry Andric }
8227fa27ce4SDimitry Andric 
Dump(Stream & stream) const8237fa27ce4SDimitry Andric void SourceManager::SourceFileCache::Dump(Stream &stream) const {
8247fa27ce4SDimitry Andric   stream << "Modification time   Lines    Path\n";
8257fa27ce4SDimitry Andric   stream << "------------------- -------- --------------------------------\n";
8267fa27ce4SDimitry Andric   for (auto &entry : m_file_cache) {
8277fa27ce4SDimitry Andric     if (!entry.second)
8287fa27ce4SDimitry Andric       continue;
8297fa27ce4SDimitry Andric     FileSP file = entry.second;
8307fa27ce4SDimitry Andric     stream.Format("{0:%Y-%m-%d %H:%M:%S} {1,8:d} {2}\n", file->GetTimestamp(),
8317fa27ce4SDimitry Andric                   file->GetNumLines(), entry.first.GetPath());
8327fa27ce4SDimitry Andric   }
833f034231aSEd Maste }
834