Coverage Report

Created: 2022-01-25 06:29

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