1 //===-- FileLineResolver.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/FileLineResolver.h"
10 
11 #include "lldb/Symbol/CompileUnit.h"
12 #include "lldb/Symbol/LineTable.h"
13 #include "lldb/Utility/ConstString.h"
14 #include "lldb/Utility/FileSpecList.h"
15 #include "lldb/Utility/Stream.h"
16 
17 #include <string>
18 
19 namespace lldb_private {
20 class Address;
21 }
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 // FileLineResolver:
27 FileLineResolver::FileLineResolver(const FileSpec &file_spec, uint32_t line_no,
28                                    bool check_inlines)
29     : Searcher(), m_file_spec(file_spec), m_line_number(line_no),
30       m_inlines(check_inlines) {}
31 
32 FileLineResolver::~FileLineResolver() = default;
33 
34 Searcher::CallbackReturn
35 FileLineResolver::SearchCallback(SearchFilter &filter, SymbolContext &context,
36                                  Address *addr) {
37   CompileUnit *cu = context.comp_unit;
38 
39   if (m_inlines || m_file_spec.Compare(cu->GetPrimaryFile(), m_file_spec,
40                                        (bool)m_file_spec.GetDirectory())) {
41     uint32_t start_file_idx = 0;
42     uint32_t file_idx =
43         cu->GetSupportFiles().FindFileIndex(start_file_idx, m_file_spec, false);
44     if (file_idx != UINT32_MAX) {
45       LineTable *line_table = cu->GetLineTable();
46       if (line_table) {
47         if (m_line_number == 0) {
48           // Match all lines in a file...
49           const bool append = true;
50           while (file_idx != UINT32_MAX) {
51             line_table->FindLineEntriesForFileIndex(file_idx, append,
52                                                     m_sc_list);
53             // Get the next file index in case we have multiple file entries
54             // for the same file
55             file_idx = cu->GetSupportFiles().FindFileIndex(file_idx + 1,
56                                                            m_file_spec, false);
57           }
58         } else {
59           // Match a specific line in a file...
60         }
61       }
62     }
63   }
64   return Searcher::eCallbackReturnContinue;
65 }
66 
67 lldb::SearchDepth FileLineResolver::GetDepth() {
68   return lldb::eSearchDepthCompUnit;
69 }
70 
71 void FileLineResolver::GetDescription(Stream *s) {
72   s->Printf("File and line resolver for file: \"%s\" line: %u",
73             m_file_spec.GetPath().c_str(), m_line_number);
74 }
75 
76 void FileLineResolver::Clear() {
77   m_file_spec.Clear();
78   m_line_number = UINT32_MAX;
79   m_sc_list.Clear();
80   m_inlines = true;
81 }
82 
83 void FileLineResolver::Reset(const FileSpec &file_spec, uint32_t line,
84                              bool check_inlines) {
85   m_file_spec = file_spec;
86   m_line_number = line;
87   m_sc_list.Clear();
88   m_inlines = check_inlines;
89 }
90