blob: d7fdd4d923f03f90c7e2beddf28fc42312e7a0e7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SourceManager.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/SourceManager.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Core/DataBuffer.h"
Jim Inghame37d6052011-09-13 00:29:56 +000019#include "lldb/Core/Debugger.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Core/Module.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000021#include "lldb/Core/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Stream.h"
Sean Callananb6d70eb2011-10-12 02:08:07 +000023#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Symbol/CompileUnit.h"
25#include "lldb/Symbol/Function.h"
Greg Clayton176761e2011-04-19 04:19:37 +000026#include "lldb/Symbol/SymbolContext.h"
Greg Clayton7e14f912011-04-23 02:04:55 +000027#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Greg Clayton9585fbf2013-03-19 00:20:55 +000029using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030using namespace lldb_private;
31
Greg Clayton9585fbf2013-03-19 00:20:55 +000032
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033static inline bool is_newline_char(char ch)
34{
35 return ch == '\n' || ch == '\r';
36}
37
38
39//----------------------------------------------------------------------
40// SourceManager constructor
41//----------------------------------------------------------------------
Greg Clayton9585fbf2013-03-19 00:20:55 +000042SourceManager::SourceManager(const TargetSP &target_sp) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043 m_last_file_sp (),
Greg Claytone4ca5152013-03-13 18:25:49 +000044 m_last_line (0),
45 m_last_count (0),
Jim Inghamf3277752011-10-07 22:16:04 +000046 m_default_set(false),
Greg Clayton9585fbf2013-03-19 00:20:55 +000047 m_target_wp (target_sp),
48 m_debugger_wp(target_sp->GetDebugger().shared_from_this())
Jim Inghame37d6052011-09-13 00:29:56 +000049{
Jim Inghame37d6052011-09-13 00:29:56 +000050}
51
Greg Clayton9585fbf2013-03-19 00:20:55 +000052SourceManager::SourceManager(const DebuggerSP &debugger_sp) :
Jim Inghame37d6052011-09-13 00:29:56 +000053 m_last_file_sp (),
Greg Claytone4ca5152013-03-13 18:25:49 +000054 m_last_line (0),
55 m_last_count (0),
Jim Inghamf3277752011-10-07 22:16:04 +000056 m_default_set(false),
Greg Clayton9585fbf2013-03-19 00:20:55 +000057 m_target_wp (),
58 m_debugger_wp (debugger_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059{
60}
61
62//----------------------------------------------------------------------
63// Destructor
64//----------------------------------------------------------------------
65SourceManager::~SourceManager()
66{
67}
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069SourceManager::FileSP
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000070SourceManager::GetFile (const FileSpec &file_spec)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071{
Greg Clayton4a895012013-03-14 22:52:17 +000072 bool same_as_previous = m_last_file_sp && m_last_file_sp->FileSpecMatches (file_spec);
73
Greg Clayton9585fbf2013-03-19 00:20:55 +000074 DebuggerSP debugger_sp (m_debugger_wp.lock());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 FileSP file_sp;
Greg Clayton4a895012013-03-14 22:52:17 +000076 if (same_as_previous)
77 file_sp = m_last_file_sp;
Greg Clayton9585fbf2013-03-19 00:20:55 +000078 else if (debugger_sp)
79 file_sp = debugger_sp->GetSourceFileCache().FindSourceFile (file_spec);
Greg Clayton4a895012013-03-14 22:52:17 +000080
Greg Clayton9585fbf2013-03-19 00:20:55 +000081 TargetSP target_sp (m_target_wp.lock());
82
Greg Clayton4a895012013-03-14 22:52:17 +000083 // It the target source path map has been updated, get this file again so we
84 // can successfully remap the source file
Greg Clayton9585fbf2013-03-19 00:20:55 +000085 if (target_sp && file_sp && file_sp->GetSourceMapModificationID() != target_sp->GetSourcePathMap().GetModificationID())
Greg Clayton4a895012013-03-14 22:52:17 +000086 file_sp.reset();
87
Johnny Chen64bab482011-12-12 21:59:28 +000088 // If file_sp is no good or it points to a non-existent file, reset it.
89 if (!file_sp || !file_sp->GetFileSpec().Exists())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090 {
Greg Clayton9585fbf2013-03-19 00:20:55 +000091 file_sp.reset (new File (file_spec, target_sp.get()));
Jim Inghame37d6052011-09-13 00:29:56 +000092
Greg Clayton9585fbf2013-03-19 00:20:55 +000093 if (debugger_sp)
94 debugger_sp->GetSourceFileCache().AddSourceFile(file_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095 }
96 return file_sp;
97}
98
99size_t
Greg Claytone4ca5152013-03-13 18:25:49 +0000100SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile (uint32_t start_line,
101 uint32_t count,
102 uint32_t curr_line,
103 const char* current_line_cstr,
104 Stream *s,
105 const SymbolContextList *bp_locs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106{
Greg Claytone4ca5152013-03-13 18:25:49 +0000107 if (count == 0)
108 return 0;
Jim Inghame37d6052011-09-13 00:29:56 +0000109 size_t return_value = 0;
Greg Claytone4ca5152013-03-13 18:25:49 +0000110 if (start_line == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 {
Greg Claytone4ca5152013-03-13 18:25:49 +0000112 if (m_last_line != 0 && m_last_line != UINT32_MAX)
113 start_line = m_last_line + m_last_count;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114 else
Greg Claytone4ca5152013-03-13 18:25:49 +0000115 start_line = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 }
117
Greg Claytone4ca5152013-03-13 18:25:49 +0000118 if (!m_default_set)
119 {
120 FileSpec tmp_spec;
121 uint32_t tmp_line;
122 GetDefaultFileAndLine(tmp_spec, tmp_line);
123 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124
Greg Claytone4ca5152013-03-13 18:25:49 +0000125 m_last_line = start_line;
126 m_last_count = count;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127
128 if (m_last_file_sp.get())
129 {
Greg Claytone4ca5152013-03-13 18:25:49 +0000130 const uint32_t end_line = start_line + count - 1;
131 for (uint32_t line = start_line; line <= end_line; ++line)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132 {
Greg Claytone4ca5152013-03-13 18:25:49 +0000133 if (!m_last_file_sp->LineIsValid (line))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 {
Greg Claytone4ca5152013-03-13 18:25:49 +0000135 m_last_line = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136 break;
137 }
138
Greg Clayton176761e2011-04-19 04:19:37 +0000139 char prefix[32] = "";
140 if (bp_locs)
141 {
Greg Claytone4ca5152013-03-13 18:25:49 +0000142 uint32_t bp_count = bp_locs->NumLineEntriesWithLine (line);
Greg Clayton176761e2011-04-19 04:19:37 +0000143
144 if (bp_count > 0)
145 ::snprintf (prefix, sizeof (prefix), "[%u] ", bp_count);
146 else
147 ::snprintf (prefix, sizeof (prefix), " ");
148 }
149
Jim Inghame37d6052011-09-13 00:29:56 +0000150 return_value += s->Printf("%s%2.2s %-4u\t",
Greg Claytone4ca5152013-03-13 18:25:49 +0000151 prefix,
152 line == curr_line ? current_line_cstr : "",
153 line);
154 size_t this_line_size = m_last_file_sp->DisplaySourceLines (line, 0, 0, s);
Jim Inghame37d6052011-09-13 00:29:56 +0000155 if (this_line_size == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156 {
Greg Claytone4ca5152013-03-13 18:25:49 +0000157 m_last_line = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 break;
159 }
Jim Inghame37d6052011-09-13 00:29:56 +0000160 else
161 return_value += this_line_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 }
163 }
Jim Inghame37d6052011-09-13 00:29:56 +0000164 return return_value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
167size_t
168SourceManager::DisplaySourceLinesWithLineNumbers
169(
170 const FileSpec &file_spec,
171 uint32_t line,
172 uint32_t context_before,
173 uint32_t context_after,
174 const char* current_line_cstr,
Greg Clayton176761e2011-04-19 04:19:37 +0000175 Stream *s,
176 const SymbolContextList *bp_locs
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177)
178{
Greg Clayton4a895012013-03-14 22:52:17 +0000179 FileSP file_sp (GetFile (file_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180
Greg Claytone4ca5152013-03-13 18:25:49 +0000181 uint32_t start_line;
182 uint32_t count = context_before + context_after + 1;
183 if (line > context_before)
184 start_line = line - context_before;
185 else
186 start_line = 1;
187
Greg Clayton4a895012013-03-14 22:52:17 +0000188 if (m_last_file_sp.get() != file_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 {
Greg Clayton4a895012013-03-14 22:52:17 +0000190 if (line == 0)
Greg Claytone4ca5152013-03-13 18:25:49 +0000191 m_last_line = 0;
Greg Clayton4a895012013-03-14 22:52:17 +0000192 m_last_file_sp = file_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 }
Greg Claytone4ca5152013-03-13 18:25:49 +0000194 return DisplaySourceLinesWithLineNumbersUsingLastFile (start_line, count, line, current_line_cstr, s, bp_locs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195}
196
197size_t
Greg Claytone4ca5152013-03-13 18:25:49 +0000198SourceManager::DisplayMoreWithLineNumbers (Stream *s,
199 uint32_t count,
200 bool reverse,
201 const SymbolContextList *bp_locs)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202{
Jim Ingham196bbc22013-01-09 03:27:33 +0000203 // If we get called before anybody has set a default file and line, then try to figure it out here.
Greg Clayton9585fbf2013-03-19 00:20:55 +0000204 const bool have_default_file_line = m_last_file_sp && m_last_line > 0;
Jim Ingham196bbc22013-01-09 03:27:33 +0000205 if (!m_default_set)
206 {
207 FileSpec tmp_spec;
208 uint32_t tmp_line;
209 GetDefaultFileAndLine(tmp_spec, tmp_line);
210 }
211
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212 if (m_last_file_sp)
213 {
Greg Claytone4ca5152013-03-13 18:25:49 +0000214 if (m_last_line == UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215 return 0;
Jim Ingham196bbc22013-01-09 03:27:33 +0000216
Greg Claytone4ca5152013-03-13 18:25:49 +0000217 if (reverse && m_last_line == 1)
Jim Ingham196bbc22013-01-09 03:27:33 +0000218 return 0;
Greg Claytone4ca5152013-03-13 18:25:49 +0000219
220 if (count > 0)
221 m_last_count = count;
222 else if (m_last_count == 0)
223 m_last_count = 10;
224
225 if (m_last_line > 0)
Jim Ingham196bbc22013-01-09 03:27:33 +0000226 {
227 if (reverse)
228 {
229 // If this is the first time we've done a reverse, then back up one more time so we end
230 // up showing the chunk before the last one we've shown:
Greg Claytone4ca5152013-03-13 18:25:49 +0000231 if (m_last_line > m_last_count)
232 m_last_line -= m_last_count;
Jim Ingham196bbc22013-01-09 03:27:33 +0000233 else
Greg Claytone4ca5152013-03-13 18:25:49 +0000234 m_last_line = 1;
Jim Ingham196bbc22013-01-09 03:27:33 +0000235 }
Greg Clayton9585fbf2013-03-19 00:20:55 +0000236 else if (have_default_file_line)
Greg Claytone4ca5152013-03-13 18:25:49 +0000237 m_last_line += m_last_count;
Jim Ingham196bbc22013-01-09 03:27:33 +0000238 }
239 else
Greg Claytone4ca5152013-03-13 18:25:49 +0000240 m_last_line = 1;
Jim Ingham196bbc22013-01-09 03:27:33 +0000241
Greg Claytone4ca5152013-03-13 18:25:49 +0000242 return DisplaySourceLinesWithLineNumbersUsingLastFile (m_last_line, m_last_count, UINT32_MAX, "", s, bp_locs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 }
244 return 0;
245}
246
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000247bool
248SourceManager::SetDefaultFileAndLine (const FileSpec &file_spec, uint32_t line)
249{
250 FileSP old_file_sp = m_last_file_sp;
251 m_last_file_sp = GetFile (file_spec);
Jim Inghamf3277752011-10-07 22:16:04 +0000252
253 m_default_set = true;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000254 if (m_last_file_sp)
255 {
Greg Claytone4ca5152013-03-13 18:25:49 +0000256 m_last_line = line;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000257 return true;
258 }
259 else
260 {
261 m_last_file_sp = old_file_sp;
262 return false;
263 }
264}
265
266bool
267SourceManager::GetDefaultFileAndLine (FileSpec &file_spec, uint32_t &line)
268{
269 if (m_last_file_sp)
270 {
271 file_spec = m_last_file_sp->GetFileSpec();
Greg Claytone4ca5152013-03-13 18:25:49 +0000272 line = m_last_line;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000273 return true;
274 }
Jim Inghamf3277752011-10-07 22:16:04 +0000275 else if (!m_default_set)
276 {
Greg Clayton9585fbf2013-03-19 00:20:55 +0000277 TargetSP target_sp (m_target_wp.lock());
278
279 if (target_sp)
Jim Inghamf3277752011-10-07 22:16:04 +0000280 {
Greg Clayton9585fbf2013-03-19 00:20:55 +0000281 // If nobody has set the default file and line then try here. If there's no executable, then we
282 // will try again later when there is one. Otherwise, if we can't find it we won't look again,
283 // somebody will have to set it (for instance when we stop somewhere...)
284 Module *executable_ptr = target_sp->GetExecutableModulePointer();
285 if (executable_ptr)
Jim Inghamf3277752011-10-07 22:16:04 +0000286 {
Greg Clayton9585fbf2013-03-19 00:20:55 +0000287 SymbolContextList sc_list;
288 ConstString main_name("main");
289 bool symbols_okay = false; // Force it to be a debug symbol.
290 bool inlines_okay = true;
291 bool append = false;
292 size_t num_matches = executable_ptr->FindFunctions (main_name,
293 NULL,
294 lldb::eFunctionNameTypeBase,
295 inlines_okay,
296 symbols_okay,
297 append,
298 sc_list);
299 for (size_t idx = 0; idx < num_matches; idx++)
Jim Inghamf3277752011-10-07 22:16:04 +0000300 {
Greg Clayton9585fbf2013-03-19 00:20:55 +0000301 SymbolContext sc;
302 sc_list.GetContextAtIndex(idx, sc);
303 if (sc.function)
Greg Clayton6f6bf262011-12-10 21:05:26 +0000304 {
Greg Clayton9585fbf2013-03-19 00:20:55 +0000305 lldb_private::LineEntry line_entry;
306 if (sc.function->GetAddressRange().GetBaseAddress().CalculateSymbolContextLineEntry (line_entry))
307 {
308 SetDefaultFileAndLine (line_entry.file,
309 line_entry.line);
310 file_spec = m_last_file_sp->GetFileSpec();
311 line = m_last_line;
312 return true;
313 }
Greg Clayton6f6bf262011-12-10 21:05:26 +0000314 }
Jim Inghamf3277752011-10-07 22:16:04 +0000315 }
316 }
Jim Inghamf3277752011-10-07 22:16:04 +0000317 }
Jim Inghamf3277752011-10-07 22:16:04 +0000318 }
Greg Clayton6f6bf262011-12-10 21:05:26 +0000319 return false;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000320}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321
Jim Ingham969795f2011-09-21 01:17:13 +0000322void
323SourceManager::FindLinesMatchingRegex (FileSpec &file_spec,
Greg Claytond804d282012-03-15 21:01:31 +0000324 RegularExpression& regex,
325 uint32_t start_line,
326 uint32_t end_line,
327 std::vector<uint32_t> &match_lines)
Jim Ingham969795f2011-09-21 01:17:13 +0000328{
329 match_lines.clear();
330 FileSP file_sp = GetFile (file_spec);
331 if (!file_sp)
332 return;
333 return file_sp->FindLinesMatchingRegex (regex, start_line, end_line, match_lines);
334}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335
Greg Clayton7e14f912011-04-23 02:04:55 +0000336SourceManager::File::File(const FileSpec &file_spec, Target *target) :
337 m_file_spec_orig (file_spec),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338 m_file_spec(file_spec),
Greg Clayton7e14f912011-04-23 02:04:55 +0000339 m_mod_time (file_spec.GetModificationTime()),
Greg Clayton4a895012013-03-14 22:52:17 +0000340 m_source_map_mod_id (0),
Greg Clayton7e14f912011-04-23 02:04:55 +0000341 m_data_sp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342 m_offsets()
343{
Greg Clayton7e14f912011-04-23 02:04:55 +0000344 if (!m_mod_time.IsValid())
345 {
Jim Inghame37d6052011-09-13 00:29:56 +0000346 if (target)
347 {
Greg Clayton4a895012013-03-14 22:52:17 +0000348 m_source_map_mod_id = target->GetSourcePathMap().GetModificationID();
349
Jim Inghame37d6052011-09-13 00:29:56 +0000350 if (!file_spec.GetDirectory() && file_spec.GetFilename())
351 {
352 // If this is just a file name, lets see if we can find it in the target:
353 bool check_inlines = false;
354 SymbolContextList sc_list;
355 size_t num_matches = target->GetImages().ResolveSymbolContextForFilePath (file_spec.GetFilename().AsCString(),
356 0,
357 check_inlines,
358 lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit,
359 sc_list);
360 bool got_multiple = false;
361 if (num_matches != 0)
362 {
363 if (num_matches > 1)
364 {
365 SymbolContext sc;
366 FileSpec *test_cu_spec = NULL;
367
368 for (unsigned i = 0; i < num_matches; i++)
369 {
370 sc_list.GetContextAtIndex(i, sc);
371 if (sc.comp_unit)
372 {
373 if (test_cu_spec)
374 {
375 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
376 got_multiple = true;
Sylvestre Ledru75f11eb2014-08-18 14:50:34 +0000377 break;
Jim Inghame37d6052011-09-13 00:29:56 +0000378 }
379 else
380 test_cu_spec = sc.comp_unit;
381 }
382 }
383 }
384 if (!got_multiple)
385 {
386 SymbolContext sc;
387 sc_list.GetContextAtIndex (0, sc);
Greg Claytond804d282012-03-15 21:01:31 +0000388 m_file_spec = sc.comp_unit;
Jim Inghame37d6052011-09-13 00:29:56 +0000389 m_mod_time = m_file_spec.GetModificationTime();
390 }
391 }
392 }
Johnny Chen64bab482011-12-12 21:59:28 +0000393 // Try remapping if m_file_spec does not correspond to an existing file.
394 if (!m_file_spec.Exists())
Jim Inghame37d6052011-09-13 00:29:56 +0000395 {
Greg Claytond804d282012-03-15 21:01:31 +0000396 FileSpec new_file_spec;
397 // Check target specific source remappings first, then fall back to
398 // modules objects can have individual path remappings that were detected
399 // when the debug info for a module was found.
400 // then
401 if (target->GetSourcePathMap().FindFile (m_file_spec, new_file_spec) ||
402 target->GetImages().FindSourceFile (m_file_spec, new_file_spec))
Johnny Chen64bab482011-12-12 21:59:28 +0000403 {
Greg Claytond804d282012-03-15 21:01:31 +0000404 m_file_spec = new_file_spec;
Johnny Chen64bab482011-12-12 21:59:28 +0000405 m_mod_time = m_file_spec.GetModificationTime();
406 }
Jim Inghame37d6052011-09-13 00:29:56 +0000407 }
408 }
Greg Clayton7e14f912011-04-23 02:04:55 +0000409 }
410
411 if (m_mod_time.IsValid())
412 m_data_sp = m_file_spec.ReadFileContents ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413}
414
415SourceManager::File::~File()
416{
417}
418
419uint32_t
420SourceManager::File::GetLineOffset (uint32_t line)
421{
422 if (line == 0)
423 return UINT32_MAX;
424
425 if (line == 1)
426 return 0;
427
428 if (CalculateLineOffsets (line))
429 {
430 if (line < m_offsets.size())
431 return m_offsets[line - 1]; // yes we want "line - 1" in the index
432 }
433 return UINT32_MAX;
434}
435
Greg Clayton44d93782014-01-27 23:43:24 +0000436uint32_t
437SourceManager::File::GetNumLines ()
438{
439 CalculateLineOffsets();
440 return m_offsets.size();
441}
442
443const char *
444SourceManager::File::PeekLineData (uint32_t line)
445{
446 if (!LineIsValid(line))
447 return NULL;
448
449 size_t line_offset = GetLineOffset (line);
450 if (line_offset < m_data_sp->GetByteSize())
451 return (const char *)m_data_sp->GetBytes() + line_offset;
452 return NULL;
453}
454
455uint32_t
456SourceManager::File::GetLineLength (uint32_t line, bool include_newline_chars)
457{
458 if (!LineIsValid(line))
459 return false;
460
461 size_t start_offset = GetLineOffset (line);
462 size_t end_offset = GetLineOffset (line + 1);
463 if (end_offset == UINT32_MAX)
464 end_offset = m_data_sp->GetByteSize();
465
466 if (end_offset > start_offset)
467 {
468 uint32_t length = end_offset - start_offset;
469 if (include_newline_chars == false)
470 {
471 const char *line_start = (const char *)m_data_sp->GetBytes() + start_offset;
472 while (length > 0)
473 {
474 const char last_char = line_start[length-1];
475 if ((last_char == '\r') || (last_char == '\n'))
476 --length;
477 else
478 break;
479 }
480 }
481 return length;
482 }
483 return 0;
484}
485
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486bool
487SourceManager::File::LineIsValid (uint32_t line)
488{
489 if (line == 0)
490 return false;
491
492 if (CalculateLineOffsets (line))
493 return line < m_offsets.size();
494 return false;
495}
496
497size_t
498SourceManager::File::DisplaySourceLines (uint32_t line, uint32_t context_before, uint32_t context_after, Stream *s)
499{
Greg Clayton9625d082010-12-08 20:16:12 +0000500 // TODO: use host API to sign up for file modifications to anything in our
501 // source cache and only update when we determine a file has been updated.
502 // For now we check each time we want to display info for the file.
503 TimeValue curr_mod_time (m_file_spec.GetModificationTime());
Johnny Chen64bab482011-12-12 21:59:28 +0000504
505 if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time)
Greg Clayton9625d082010-12-08 20:16:12 +0000506 {
507 m_mod_time = curr_mod_time;
508 m_data_sp = m_file_spec.ReadFileContents ();
509 m_offsets.clear();
510 }
511
Johnny Chen64bab482011-12-12 21:59:28 +0000512 // Sanity check m_data_sp before proceeding.
513 if (!m_data_sp)
514 return 0;
515
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516 const uint32_t start_line = line <= context_before ? 1 : line - context_before;
517 const uint32_t start_line_offset = GetLineOffset (start_line);
518 if (start_line_offset != UINT32_MAX)
519 {
520 const uint32_t end_line = line + context_after;
521 uint32_t end_line_offset = GetLineOffset (end_line + 1);
522 if (end_line_offset == UINT32_MAX)
523 end_line_offset = m_data_sp->GetByteSize();
524
525 assert (start_line_offset <= end_line_offset);
526 size_t bytes_written = 0;
527 if (start_line_offset < end_line_offset)
528 {
529 size_t count = end_line_offset - start_line_offset;
530 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
531 bytes_written = s->Write(cstr, count);
532 if (!is_newline_char(cstr[count-1]))
533 bytes_written += s->EOL();
534 }
535 return bytes_written;
536 }
537 return 0;
538}
539
Jim Ingham969795f2011-09-21 01:17:13 +0000540void
541SourceManager::File::FindLinesMatchingRegex (RegularExpression& regex, uint32_t start_line, uint32_t end_line, std::vector<uint32_t> &match_lines)
542{
543 TimeValue curr_mod_time (m_file_spec.GetModificationTime());
544 if (m_mod_time != curr_mod_time)
545 {
546 m_mod_time = curr_mod_time;
547 m_data_sp = m_file_spec.ReadFileContents ();
548 m_offsets.clear();
549 }
550
551 match_lines.clear();
552
553 if (!LineIsValid(start_line) || (end_line != UINT32_MAX && !LineIsValid(end_line)))
554 return;
555 if (start_line > end_line)
556 return;
557
558 for (uint32_t line_no = start_line; line_no < end_line; line_no++)
559 {
560 std::string buffer;
561 if (!GetLine (line_no, buffer))
562 break;
563 if (regex.Execute(buffer.c_str()))
564 {
565 match_lines.push_back(line_no);
566 }
567 }
568}
569
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570bool
571SourceManager::File::FileSpecMatches (const FileSpec &file_spec)
572{
Greg Clayton644247c2011-07-07 01:59:51 +0000573 return FileSpec::Equal (m_file_spec, file_spec, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574}
575
Jim Inghame37d6052011-09-13 00:29:56 +0000576bool
577lldb_private::operator== (const SourceManager::File &lhs, const SourceManager::File &rhs)
578{
579 if (lhs.m_file_spec == rhs.m_file_spec)
580 {
581 if (lhs.m_mod_time.IsValid())
582 {
583 if (rhs.m_mod_time.IsValid())
584 return lhs.m_mod_time == rhs.m_mod_time;
585 else
586 return false;
587 }
588 else if (rhs.m_mod_time.IsValid())
589 return false;
590 else
591 return true;
592 }
593 else
594 return false;
595}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596
597bool
598SourceManager::File::CalculateLineOffsets (uint32_t line)
599{
600 line = UINT32_MAX; // TODO: take this line out when we support partial indexing
601 if (line == UINT32_MAX)
602 {
603 // Already done?
604 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
605 return true;
606
607 if (m_offsets.empty())
608 {
609 if (m_data_sp.get() == NULL)
610 return false;
611
612 const char *start = (char *)m_data_sp->GetBytes();
613 if (start)
614 {
615 const char *end = start + m_data_sp->GetByteSize();
616
617 // Calculate all line offsets from scratch
618
619 // Push a 1 at index zero to indicate the file has been completely indexed.
620 m_offsets.push_back(UINT32_MAX);
Eric Christopher2490f5c2013-08-30 17:50:57 +0000621 const char *s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 for (s = start; s < end; ++s)
623 {
Eric Christopher2490f5c2013-08-30 17:50:57 +0000624 char curr_ch = *s;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 if (is_newline_char (curr_ch))
626 {
Greg Claytonf6cdd122013-02-07 03:38:34 +0000627 if (s + 1 < end)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 {
Eric Christopher2490f5c2013-08-30 17:50:57 +0000629 char next_ch = s[1];
Greg Claytonf6cdd122013-02-07 03:38:34 +0000630 if (is_newline_char (next_ch))
631 {
632 if (curr_ch != next_ch)
633 ++s;
634 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 }
636 m_offsets.push_back(s + 1 - start);
637 }
638 }
639 if (!m_offsets.empty())
640 {
641 if (m_offsets.back() < end - start)
642 m_offsets.push_back(end - start);
643 }
644 return true;
645 }
646 }
647 else
648 {
649 // Some lines have been populated, start where we last left off
Greg Claytonc7bece562013-01-25 18:06:21 +0000650 assert("Not implemented yet" == NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 }
652
653 }
654 else
655 {
656 // Calculate all line offsets up to "line"
Greg Claytonc7bece562013-01-25 18:06:21 +0000657 assert("Not implemented yet" == NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 }
659 return false;
660}
Jim Inghame37d6052011-09-13 00:29:56 +0000661
Jim Ingham969795f2011-09-21 01:17:13 +0000662bool
663SourceManager::File::GetLine (uint32_t line_no, std::string &buffer)
664{
665 if (!LineIsValid(line_no))
666 return false;
667
Greg Claytonc7bece562013-01-25 18:06:21 +0000668 size_t start_offset = GetLineOffset (line_no);
669 size_t end_offset = GetLineOffset (line_no + 1);
Jim Ingham969795f2011-09-21 01:17:13 +0000670 if (end_offset == UINT32_MAX)
671 {
672 end_offset = m_data_sp->GetByteSize();
673 }
674 buffer.assign((char *) m_data_sp->GetBytes() + start_offset, end_offset - start_offset);
675
676 return true;
677}
678
Jim Inghame37d6052011-09-13 00:29:56 +0000679void
680SourceManager::SourceFileCache::AddSourceFile (const FileSP &file_sp)
681{
682 FileSpec file_spec;
683 FileCache::iterator pos = m_file_cache.find(file_spec);
684 if (pos == m_file_cache.end())
685 m_file_cache[file_spec] = file_sp;
686 else
687 {
688 if (file_sp != pos->second)
689 m_file_cache[file_spec] = file_sp;
690 }
691}
692
693SourceManager::FileSP
694SourceManager::SourceFileCache::FindSourceFile (const FileSpec &file_spec) const
695{
696 FileSP file_sp;
697 FileCache::const_iterator pos = m_file_cache.find(file_spec);
698 if (pos != m_file_cache.end())
699 file_sp = pos->second;
700 return file_sp;
701}
702