blob: 87874c6b866f6d7e8031acb5e4bba36e84b28ff3 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Stream.h"
Sean Callananb6d70eb2011-10-12 02:08:07 +000022#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Symbol/CompileUnit.h"
24#include "lldb/Symbol/Function.h"
Greg Clayton176761e2011-04-19 04:19:37 +000025#include "lldb/Symbol/SymbolContext.h"
Greg Clayton7e14f912011-04-23 02:04:55 +000026#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb_private;
29
30static inline bool is_newline_char(char ch)
31{
32 return ch == '\n' || ch == '\r';
33}
34
35
36//----------------------------------------------------------------------
37// SourceManager constructor
38//----------------------------------------------------------------------
Jim Inghame37d6052011-09-13 00:29:56 +000039SourceManager::SourceManager(Target &target) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 m_last_file_sp (),
41 m_last_file_line (0),
42 m_last_file_context_before (0),
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000043 m_last_file_context_after (10),
Jim Inghamf3277752011-10-07 22:16:04 +000044 m_default_set(false),
Jim Ingham196bbc22013-01-09 03:27:33 +000045 m_first_reverse(true),
Jim Inghame37d6052011-09-13 00:29:56 +000046 m_target (&target),
47 m_debugger(NULL)
48{
49 m_debugger = &(m_target->GetDebugger());
50}
51
52SourceManager::SourceManager(Debugger &debugger) :
53 m_last_file_sp (),
54 m_last_file_line (0),
55 m_last_file_context_before (0),
56 m_last_file_context_after (10),
Jim Inghamf3277752011-10-07 22:16:04 +000057 m_default_set(false),
Jim Ingham196bbc22013-01-09 03:27:33 +000058 m_first_reverse (true),
Jim Inghame37d6052011-09-13 00:29:56 +000059 m_target (NULL),
60 m_debugger (&debugger)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061{
62}
63
64//----------------------------------------------------------------------
65// Destructor
66//----------------------------------------------------------------------
67SourceManager::~SourceManager()
68{
69}
70
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071SourceManager::FileSP
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000072SourceManager::GetFile (const FileSpec &file_spec)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073{
74 FileSP file_sp;
Jim Inghame37d6052011-09-13 00:29:56 +000075 file_sp = m_debugger->GetSourceFileCache().FindSourceFile (file_spec);
Johnny Chen64bab482011-12-12 21:59:28 +000076 // If file_sp is no good or it points to a non-existent file, reset it.
77 if (!file_sp || !file_sp->GetFileSpec().Exists())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 {
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000079 file_sp.reset (new File (file_spec, m_target));
Jim Inghame37d6052011-09-13 00:29:56 +000080
81 m_debugger->GetSourceFileCache().AddSourceFile(file_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 }
83 return file_sp;
84}
85
86size_t
87SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile
88(
89 uint32_t line,
90 uint32_t context_before,
91 uint32_t context_after,
92 const char* current_line_cstr,
Greg Clayton176761e2011-04-19 04:19:37 +000093 Stream *s,
94 const SymbolContextList *bp_locs
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095)
96{
Jim Ingham196bbc22013-01-09 03:27:33 +000097 m_first_reverse = true;
Jim Inghame37d6052011-09-13 00:29:56 +000098 size_t return_value = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099 if (line == 0)
100 {
101 if (m_last_file_line != 0
102 && m_last_file_line != UINT32_MAX)
103 line = m_last_file_line + context_before;
104 else
105 line = 1;
106 }
107
108 m_last_file_line = line + context_after + 1;
109 m_last_file_context_before = context_before;
110 m_last_file_context_after = context_after;
111
112 if (context_before == UINT32_MAX)
113 context_before = 0;
114 if (context_after == UINT32_MAX)
115 context_after = 10;
116
117 if (m_last_file_sp.get())
118 {
119 const uint32_t start_line = line <= context_before ? 1 : line - context_before;
120 const uint32_t end_line = line + context_after;
121 uint32_t curr_line;
122 for (curr_line = start_line; curr_line <= end_line; ++curr_line)
123 {
124 if (!m_last_file_sp->LineIsValid (curr_line))
125 {
126 m_last_file_line = UINT32_MAX;
127 break;
128 }
129
Greg Clayton176761e2011-04-19 04:19:37 +0000130 char prefix[32] = "";
131 if (bp_locs)
132 {
133 uint32_t bp_count = bp_locs->NumLineEntriesWithLine (curr_line);
134
135 if (bp_count > 0)
136 ::snprintf (prefix, sizeof (prefix), "[%u] ", bp_count);
137 else
138 ::snprintf (prefix, sizeof (prefix), " ");
139 }
140
Jim Inghame37d6052011-09-13 00:29:56 +0000141 return_value += s->Printf("%s%2.2s %-4u\t",
Greg Clayton176761e2011-04-19 04:19:37 +0000142 prefix,
143 curr_line == line ? current_line_cstr : "",
144 curr_line);
Jim Inghame37d6052011-09-13 00:29:56 +0000145 size_t this_line_size = m_last_file_sp->DisplaySourceLines (curr_line, 0, 0, s);
146 if (this_line_size == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 {
148 m_last_file_line = UINT32_MAX;
149 break;
150 }
Jim Inghame37d6052011-09-13 00:29:56 +0000151 else
152 return_value += this_line_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 }
154 }
Jim Inghame37d6052011-09-13 00:29:56 +0000155 return return_value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156}
157
158size_t
159SourceManager::DisplaySourceLinesWithLineNumbers
160(
161 const FileSpec &file_spec,
162 uint32_t line,
163 uint32_t context_before,
164 uint32_t context_after,
165 const char* current_line_cstr,
Greg Clayton176761e2011-04-19 04:19:37 +0000166 Stream *s,
167 const SymbolContextList *bp_locs
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168)
169{
170 bool same_as_previous = m_last_file_sp && m_last_file_sp->FileSpecMatches (file_spec);
171
172 if (!same_as_previous)
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000173 m_last_file_sp = GetFile (file_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174
175 if (line == 0)
176 {
177 if (!same_as_previous)
178 m_last_file_line = 0;
179 }
180
Greg Clayton176761e2011-04-19 04:19:37 +0000181 return DisplaySourceLinesWithLineNumbersUsingLastFile (line, context_before, context_after, current_line_cstr, s, bp_locs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182}
183
184size_t
Jim Ingham196bbc22013-01-09 03:27:33 +0000185SourceManager::DisplayMoreWithLineNumbers (Stream *s, const SymbolContextList *bp_locs, bool reverse)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186{
Jim Ingham196bbc22013-01-09 03:27:33 +0000187 // If we get called before anybody has set a default file and line, then try to figure it out here.
188 if (!m_default_set)
189 {
190 FileSpec tmp_spec;
191 uint32_t tmp_line;
192 GetDefaultFileAndLine(tmp_spec, tmp_line);
193 }
194
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 if (m_last_file_sp)
196 {
197 if (m_last_file_line == UINT32_MAX)
198 return 0;
Jim Ingham196bbc22013-01-09 03:27:33 +0000199
200 if (reverse && m_last_file_line == 1)
201 return 0;
202
203 uint32_t line;
204 uint32_t new_last_file_line = 0;
205
206 if (m_last_file_line != 0
207 && m_last_file_line != UINT32_MAX)
208 {
209 if (reverse)
210 {
211 // If this is the first time we've done a reverse, then back up one more time so we end
212 // up showing the chunk before the last one we've shown:
213 if (m_first_reverse)
214 {
215 if (m_last_file_line > m_last_file_context_after)
216 m_last_file_line -= m_last_file_context_after + 1;
217 }
218
219 if (m_last_file_line > m_last_file_context_after)
220 {
221 line = m_last_file_line - m_last_file_context_after;
222 if (line > m_last_file_context_before)
223 new_last_file_line = line - m_last_file_context_before - 1;
224 else
225 new_last_file_line = 1;
226 }
227 else
228 {
229 line = 1;
230 new_last_file_line = 1;
231 }
232 }
233 else
234 line = m_last_file_line + m_last_file_context_before;
235 }
236 else
237 line = 1;
238
239 size_t num_chars_shown = DisplaySourceLinesWithLineNumbersUsingLastFile (line, m_last_file_context_before, m_last_file_context_after, "", s, bp_locs);
240 if (new_last_file_line != 0)
241 m_last_file_line = new_last_file_line;
242 if (reverse)
243 m_first_reverse = false;
244
245 return num_chars_shown;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 }
247 return 0;
248}
249
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000250bool
251SourceManager::SetDefaultFileAndLine (const FileSpec &file_spec, uint32_t line)
252{
253 FileSP old_file_sp = m_last_file_sp;
254 m_last_file_sp = GetFile (file_spec);
Jim Inghamf3277752011-10-07 22:16:04 +0000255
256 m_default_set = true;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000257 if (m_last_file_sp)
258 {
259 m_last_file_line = line;
260 return true;
261 }
262 else
263 {
264 m_last_file_sp = old_file_sp;
265 return false;
266 }
267}
268
269bool
270SourceManager::GetDefaultFileAndLine (FileSpec &file_spec, uint32_t &line)
271{
272 if (m_last_file_sp)
273 {
274 file_spec = m_last_file_sp->GetFileSpec();
275 line = m_last_file_line;
276 return true;
277 }
Jim Inghamf3277752011-10-07 22:16:04 +0000278 else if (!m_default_set)
279 {
280 // If nobody has set the default file and line then try here. If there's no executable, then we
281 // will try again later when there is one. Otherwise, if we can't find it we won't look again,
282 // somebody will have to set it (for instance when we stop somewhere...)
283 Module *executable_ptr = m_target->GetExecutableModulePointer();
284 if (executable_ptr)
285 {
286 SymbolContextList sc_list;
287 uint32_t num_matches;
288 ConstString main_name("main");
289 bool symbols_okay = false; // Force it to be a debug symbol.
Sean Callanan9df05fb2012-02-10 22:52:19 +0000290 bool inlines_okay = true;
Jim Inghamf3277752011-10-07 22:16:04 +0000291 bool append = false;
Sean Callanan9df05fb2012-02-10 22:52:19 +0000292 num_matches = executable_ptr->FindFunctions (main_name, NULL, lldb::eFunctionNameTypeBase, inlines_okay, symbols_okay, append, sc_list);
Jim Inghamf3277752011-10-07 22:16:04 +0000293 for (uint32_t idx = 0; idx < num_matches; idx++)
294 {
295 SymbolContext sc;
296 sc_list.GetContextAtIndex(idx, sc);
Greg Clayton6f6bf262011-12-10 21:05:26 +0000297 if (sc.function)
Jim Inghamf3277752011-10-07 22:16:04 +0000298 {
Greg Clayton6f6bf262011-12-10 21:05:26 +0000299 lldb_private::LineEntry line_entry;
300 if (sc.function->GetAddressRange().GetBaseAddress().CalculateSymbolContextLineEntry (line_entry))
301 {
302 SetDefaultFileAndLine (line_entry.file,
303 line_entry.line);
304 file_spec = m_last_file_sp->GetFileSpec();
305 line = m_last_file_line;
306 return true;
307 }
Jim Inghamf3277752011-10-07 22:16:04 +0000308 }
309 }
Jim Inghamf3277752011-10-07 22:16:04 +0000310 }
Jim Inghamf3277752011-10-07 22:16:04 +0000311 }
Greg Clayton6f6bf262011-12-10 21:05:26 +0000312 return false;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000313}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314
Jim Ingham969795f2011-09-21 01:17:13 +0000315void
316SourceManager::FindLinesMatchingRegex (FileSpec &file_spec,
Greg Claytond804d282012-03-15 21:01:31 +0000317 RegularExpression& regex,
318 uint32_t start_line,
319 uint32_t end_line,
320 std::vector<uint32_t> &match_lines)
Jim Ingham969795f2011-09-21 01:17:13 +0000321{
322 match_lines.clear();
323 FileSP file_sp = GetFile (file_spec);
324 if (!file_sp)
325 return;
326 return file_sp->FindLinesMatchingRegex (regex, start_line, end_line, match_lines);
327}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328
Greg Clayton7e14f912011-04-23 02:04:55 +0000329SourceManager::File::File(const FileSpec &file_spec, Target *target) :
330 m_file_spec_orig (file_spec),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 m_file_spec(file_spec),
Greg Clayton7e14f912011-04-23 02:04:55 +0000332 m_mod_time (file_spec.GetModificationTime()),
333 m_data_sp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 m_offsets()
335{
Greg Clayton7e14f912011-04-23 02:04:55 +0000336 if (!m_mod_time.IsValid())
337 {
Jim Inghame37d6052011-09-13 00:29:56 +0000338 if (target)
339 {
340 if (!file_spec.GetDirectory() && file_spec.GetFilename())
341 {
342 // If this is just a file name, lets see if we can find it in the target:
343 bool check_inlines = false;
344 SymbolContextList sc_list;
345 size_t num_matches = target->GetImages().ResolveSymbolContextForFilePath (file_spec.GetFilename().AsCString(),
346 0,
347 check_inlines,
348 lldb::eSymbolContextModule | lldb::eSymbolContextCompUnit,
349 sc_list);
350 bool got_multiple = false;
351 if (num_matches != 0)
352 {
353 if (num_matches > 1)
354 {
355 SymbolContext sc;
356 FileSpec *test_cu_spec = NULL;
357
358 for (unsigned i = 0; i < num_matches; i++)
359 {
360 sc_list.GetContextAtIndex(i, sc);
361 if (sc.comp_unit)
362 {
363 if (test_cu_spec)
364 {
365 if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
366 got_multiple = true;
367 break;
368 }
369 else
370 test_cu_spec = sc.comp_unit;
371 }
372 }
373 }
374 if (!got_multiple)
375 {
376 SymbolContext sc;
377 sc_list.GetContextAtIndex (0, sc);
Greg Claytond804d282012-03-15 21:01:31 +0000378 m_file_spec = sc.comp_unit;
Jim Inghame37d6052011-09-13 00:29:56 +0000379 m_mod_time = m_file_spec.GetModificationTime();
380 }
381 }
382 }
Johnny Chen64bab482011-12-12 21:59:28 +0000383 // Try remapping if m_file_spec does not correspond to an existing file.
384 if (!m_file_spec.Exists())
Jim Inghame37d6052011-09-13 00:29:56 +0000385 {
Greg Claytond804d282012-03-15 21:01:31 +0000386 FileSpec new_file_spec;
387 // Check target specific source remappings first, then fall back to
388 // modules objects can have individual path remappings that were detected
389 // when the debug info for a module was found.
390 // then
391 if (target->GetSourcePathMap().FindFile (m_file_spec, new_file_spec) ||
392 target->GetImages().FindSourceFile (m_file_spec, new_file_spec))
Johnny Chen64bab482011-12-12 21:59:28 +0000393 {
Greg Claytond804d282012-03-15 21:01:31 +0000394 m_file_spec = new_file_spec;
Johnny Chen64bab482011-12-12 21:59:28 +0000395 m_mod_time = m_file_spec.GetModificationTime();
396 }
Jim Inghame37d6052011-09-13 00:29:56 +0000397 }
398 }
Greg Clayton7e14f912011-04-23 02:04:55 +0000399 }
400
401 if (m_mod_time.IsValid())
402 m_data_sp = m_file_spec.ReadFileContents ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
405SourceManager::File::~File()
406{
407}
408
409uint32_t
410SourceManager::File::GetLineOffset (uint32_t line)
411{
412 if (line == 0)
413 return UINT32_MAX;
414
415 if (line == 1)
416 return 0;
417
418 if (CalculateLineOffsets (line))
419 {
420 if (line < m_offsets.size())
421 return m_offsets[line - 1]; // yes we want "line - 1" in the index
422 }
423 return UINT32_MAX;
424}
425
426bool
427SourceManager::File::LineIsValid (uint32_t line)
428{
429 if (line == 0)
430 return false;
431
432 if (CalculateLineOffsets (line))
433 return line < m_offsets.size();
434 return false;
435}
436
437size_t
438SourceManager::File::DisplaySourceLines (uint32_t line, uint32_t context_before, uint32_t context_after, Stream *s)
439{
Greg Clayton9625d082010-12-08 20:16:12 +0000440 // TODO: use host API to sign up for file modifications to anything in our
441 // source cache and only update when we determine a file has been updated.
442 // For now we check each time we want to display info for the file.
443 TimeValue curr_mod_time (m_file_spec.GetModificationTime());
Johnny Chen64bab482011-12-12 21:59:28 +0000444
445 if (curr_mod_time.IsValid() && m_mod_time != curr_mod_time)
Greg Clayton9625d082010-12-08 20:16:12 +0000446 {
447 m_mod_time = curr_mod_time;
448 m_data_sp = m_file_spec.ReadFileContents ();
449 m_offsets.clear();
450 }
451
Johnny Chen64bab482011-12-12 21:59:28 +0000452 // Sanity check m_data_sp before proceeding.
453 if (!m_data_sp)
454 return 0;
455
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 const uint32_t start_line = line <= context_before ? 1 : line - context_before;
457 const uint32_t start_line_offset = GetLineOffset (start_line);
458 if (start_line_offset != UINT32_MAX)
459 {
460 const uint32_t end_line = line + context_after;
461 uint32_t end_line_offset = GetLineOffset (end_line + 1);
462 if (end_line_offset == UINT32_MAX)
463 end_line_offset = m_data_sp->GetByteSize();
464
465 assert (start_line_offset <= end_line_offset);
466 size_t bytes_written = 0;
467 if (start_line_offset < end_line_offset)
468 {
469 size_t count = end_line_offset - start_line_offset;
470 const uint8_t *cstr = m_data_sp->GetBytes() + start_line_offset;
471 bytes_written = s->Write(cstr, count);
472 if (!is_newline_char(cstr[count-1]))
473 bytes_written += s->EOL();
474 }
475 return bytes_written;
476 }
477 return 0;
478}
479
Jim Ingham969795f2011-09-21 01:17:13 +0000480void
481SourceManager::File::FindLinesMatchingRegex (RegularExpression& regex, uint32_t start_line, uint32_t end_line, std::vector<uint32_t> &match_lines)
482{
483 TimeValue curr_mod_time (m_file_spec.GetModificationTime());
484 if (m_mod_time != curr_mod_time)
485 {
486 m_mod_time = curr_mod_time;
487 m_data_sp = m_file_spec.ReadFileContents ();
488 m_offsets.clear();
489 }
490
491 match_lines.clear();
492
493 if (!LineIsValid(start_line) || (end_line != UINT32_MAX && !LineIsValid(end_line)))
494 return;
495 if (start_line > end_line)
496 return;
497
498 for (uint32_t line_no = start_line; line_no < end_line; line_no++)
499 {
500 std::string buffer;
501 if (!GetLine (line_no, buffer))
502 break;
503 if (regex.Execute(buffer.c_str()))
504 {
505 match_lines.push_back(line_no);
506 }
507 }
508}
509
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510bool
511SourceManager::File::FileSpecMatches (const FileSpec &file_spec)
512{
Greg Clayton644247c2011-07-07 01:59:51 +0000513 return FileSpec::Equal (m_file_spec, file_spec, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514}
515
Jim Inghame37d6052011-09-13 00:29:56 +0000516bool
517lldb_private::operator== (const SourceManager::File &lhs, const SourceManager::File &rhs)
518{
519 if (lhs.m_file_spec == rhs.m_file_spec)
520 {
521 if (lhs.m_mod_time.IsValid())
522 {
523 if (rhs.m_mod_time.IsValid())
524 return lhs.m_mod_time == rhs.m_mod_time;
525 else
526 return false;
527 }
528 else if (rhs.m_mod_time.IsValid())
529 return false;
530 else
531 return true;
532 }
533 else
534 return false;
535}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536
537bool
538SourceManager::File::CalculateLineOffsets (uint32_t line)
539{
540 line = UINT32_MAX; // TODO: take this line out when we support partial indexing
541 if (line == UINT32_MAX)
542 {
543 // Already done?
544 if (!m_offsets.empty() && m_offsets[0] == UINT32_MAX)
545 return true;
546
547 if (m_offsets.empty())
548 {
549 if (m_data_sp.get() == NULL)
550 return false;
551
552 const char *start = (char *)m_data_sp->GetBytes();
553 if (start)
554 {
555 const char *end = start + m_data_sp->GetByteSize();
556
557 // Calculate all line offsets from scratch
558
559 // Push a 1 at index zero to indicate the file has been completely indexed.
560 m_offsets.push_back(UINT32_MAX);
561 register const char *s;
562 for (s = start; s < end; ++s)
563 {
564 register char curr_ch = *s;
565 if (is_newline_char (curr_ch))
566 {
567 register char next_ch = s[1];
568 if (is_newline_char (next_ch))
569 {
570 if (curr_ch != next_ch)
571 ++s;
572 }
573 m_offsets.push_back(s + 1 - start);
574 }
575 }
576 if (!m_offsets.empty())
577 {
578 if (m_offsets.back() < end - start)
579 m_offsets.push_back(end - start);
580 }
581 return true;
582 }
583 }
584 else
585 {
586 // Some lines have been populated, start where we last left off
587 assert(!"Not implemented yet");
588 }
589
590 }
591 else
592 {
593 // Calculate all line offsets up to "line"
594 assert(!"Not implemented yet");
595 }
596 return false;
597}
Jim Inghame37d6052011-09-13 00:29:56 +0000598
Jim Ingham969795f2011-09-21 01:17:13 +0000599bool
600SourceManager::File::GetLine (uint32_t line_no, std::string &buffer)
601{
602 if (!LineIsValid(line_no))
603 return false;
604
605 uint32_t start_offset = GetLineOffset (line_no);
606 uint32_t end_offset = GetLineOffset (line_no + 1);
607 if (end_offset == UINT32_MAX)
608 {
609 end_offset = m_data_sp->GetByteSize();
610 }
611 buffer.assign((char *) m_data_sp->GetBytes() + start_offset, end_offset - start_offset);
612
613 return true;
614}
615
Jim Inghame37d6052011-09-13 00:29:56 +0000616void
617SourceManager::SourceFileCache::AddSourceFile (const FileSP &file_sp)
618{
619 FileSpec file_spec;
620 FileCache::iterator pos = m_file_cache.find(file_spec);
621 if (pos == m_file_cache.end())
622 m_file_cache[file_spec] = file_sp;
623 else
624 {
625 if (file_sp != pos->second)
626 m_file_cache[file_spec] = file_sp;
627 }
628}
629
630SourceManager::FileSP
631SourceManager::SourceFileCache::FindSourceFile (const FileSpec &file_spec) const
632{
633 FileSP file_sp;
634 FileCache::const_iterator pos = m_file_cache.find(file_spec);
635 if (pos != m_file_cache.end())
636 file_sp = pos->second;
637 return file_sp;
638}
639