File: clCxxFileCacheSymbols.cpp

package info (click to toggle)
codelite 12.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (196 lines) | stat: -rw-r--r-- 6,240 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "clCxxFileCacheSymbols.h"
#include "codelite_events.h"
#include "ctags_manager.h"
#include "event_notifier.h"
#include "file_logger.h"
#include "parse_thread.h"
#include "worker_thread.h"
#include <algorithm>
#include <wx/tokenzr.h>

wxDEFINE_EVENT(wxEVT_CXX_SYMBOLS_CACHE_UPDATED, clCommandEvent);
wxDEFINE_EVENT(wxEVT_CXX_SYMBOLS_CACHE_INVALIDATED, clCommandEvent);

class SourceToTagsThread : public wxThread
{
    clCxxFileCacheSymbols* m_cache;
    wxMessageQueue<wxString> m_queue;

public:
    SourceToTagsThread(clCxxFileCacheSymbols* cache)
        : wxThread(wxTHREAD_JOINABLE)
        , m_cache(cache)
    {
    }

    virtual ~SourceToTagsThread() { clDEBUG1() << "SourceToTagsThread helper thread is going down" << clEndl; }
    void Stop()
    {
        // Notify the thread to exit and
        // wait for it
        if(IsAlive()) {
            Delete(NULL, wxTHREAD_WAIT_BLOCK);

        } else {
            Wait(wxTHREAD_WAIT_BLOCK);
        }
    }
    void Start()
    {
        clDEBUG1() << "SourceToTagsThread helper thread started" << clEndl;
        Create();
        SetPriority(wxPRIORITY_DEFAULT);
        Run();
    }

    virtual void* Entry()
    {
        while(true) {
            wxString filename;
            if(m_queue.ReceiveTimeout(50, filename) == wxMSGQUEUE_NO_ERROR) {
                if(TagsManagerST::Get()->IsBinaryFile(filename)) { continue; }

                wxString strTags;
                TagsManagerST::Get()->SourceToTags(filename, strTags);

                // Fire the event
                m_cache->CallAfter(&clCxxFileCacheSymbols::OnPraseCompleted, filename, strTags);
            }
            if(TestDestroy()) break;
        }
        return NULL;
    }

    void ParseFile(const wxString& filename) { m_queue.Post(filename); }
};

clCxxFileCacheSymbols::clCxxFileCacheSymbols()
{
    EventNotifier::Get()->Bind(wxEVT_FILE_SAVED, &clCxxFileCacheSymbols::OnFileSave, this);
    EventNotifier::Get()->Bind(wxEVT_WORKSPACE_CLOSED, &clCxxFileCacheSymbols::OnWorkspaceAction, this);
    EventNotifier::Get()->Bind(wxEVT_WORKSPACE_LOADED, &clCxxFileCacheSymbols::OnWorkspaceAction, this);
    m_helperThread = new SourceToTagsThread(this);
    m_helperThread->Start();
}

clCxxFileCacheSymbols::~clCxxFileCacheSymbols()
{
    Clear();
    m_helperThread->Stop();
    wxDELETE(m_helperThread);

    EventNotifier::Get()->Unbind(wxEVT_FILE_SAVED, &clCxxFileCacheSymbols::OnFileSave, this);
    EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_CLOSED, &clCxxFileCacheSymbols::OnWorkspaceAction, this);
    EventNotifier::Get()->Unbind(wxEVT_WORKSPACE_LOADED, &clCxxFileCacheSymbols::OnWorkspaceAction, this);
}

bool clCxxFileCacheSymbols::Contains(const wxFileName& filename) const { return Contains(filename.GetFullPath()); }

bool clCxxFileCacheSymbols::Contains(const wxString& filename) const
{
    // wxCriticalSectionLocker locker(m_cs);
    return (m_cache.count(filename) > 0);
}

void clCxxFileCacheSymbols::Clear()
{
    // wxCriticalSectionLocker locker(m_cs);
    m_cache.clear();
    m_pendingFiles.clear();
    clDEBUG1() << "Symbols cache cleared" << clEndl;
}

void clCxxFileCacheSymbols::Update(const wxFileName& filename, const TagEntryPtrVector_t& tags)
{
    // wxCriticalSectionLocker locker(m_cs);
    if(m_cache.count(filename.GetFullPath())) { m_cache.erase(filename.GetFullPath()); }
    m_cache[filename.GetFullPath()] = tags;
    clDEBUG1() << "Updating Symbols cache for file:" << filename << clEndl;
}

void clCxxFileCacheSymbols::Delete(const wxFileName& filename)
{
    // wxCriticalSectionLocker locker(m_cs);
    m_cache.erase(filename.GetFullPath());
    clDEBUG1() << "Deleting Symbols cache for file:" << filename << clEndl;

    // Notify that the symbols for this file were invalidated
    clCommandEvent event(wxEVT_CXX_SYMBOLS_CACHE_INVALIDATED);
    event.SetFileName(filename.GetFullPath());
    EventNotifier::Get()->AddPendingEvent(event);
}

bool clCxxFileCacheSymbols::Find(const wxFileName& filename, TagEntryPtrVector_t& tags, size_t flags)
{
    {
        // wxCriticalSectionLocker locker(m_cs);
        if(m_cache.count(filename.GetFullPath())) {
            tags = m_cache[filename.GetFullPath()];
            clDEBUG1() << "Symbols fetched from cache for file:" << filename << clEndl;
        } else {
            clDEBUG1() << "Symbols for file:" << filename << "do not exist in the cache" << clEndl;
            return false;
        }
    }

    // Apply the flags
    if((flags & kFunctions) && !tags.empty()) {
        // Remove all non functions
        TagEntryPtrVector_t::iterator iter = tags.begin();
        for(; iter != tags.end();) {
            if(!(*iter)->IsMethod()) {
                iter = tags.erase(iter);
            } else {
                ++iter;
            }
        }
    }
    return true;
}

void clCxxFileCacheSymbols::OnFileSave(clCommandEvent& e)
{
    e.Skip();
    Delete(e.GetFileName());
}

void clCxxFileCacheSymbols::OnWorkspaceAction(wxCommandEvent& e)
{
    e.Skip();
    Clear();
}

void clCxxFileCacheSymbols::RequestSymbols(const wxFileName& filename)
{
    // If we are waiting for this file parse to complete, dont ask for it again
    if(m_pendingFiles.count(filename.GetFullPath())) {
        clDEBUG1() << "Ignoring duplicate parse request for file:" << filename.GetFullPath() << clEndl;
        return;
    }

    m_helperThread->ParseFile(filename.GetFullPath());
    m_pendingFiles.insert(filename.GetFullPath());
}

void clCxxFileCacheSymbols::OnPraseCompleted(const wxString& filename, const wxString& strTags)
{
    TagEntryPtrVector_t tags;
    // Convert the string into array of tags
    wxArrayString lines = ::wxStringTokenize(strTags, "\n", wxTOKEN_STRTOK);
    for(size_t i = 0; i < lines.size(); ++i) {
        wxString& strLine = lines.Item(i);
        strLine.Trim().Trim(false);
        if(strLine.IsEmpty()) continue;

        TagEntryPtr tag(new TagEntry());
        tag->FromLine(strLine);
        tags.push_back(tag);
    }
    // Update the cache
    Update(filename, tags);
    clCommandEvent event(wxEVT_CXX_SYMBOLS_CACHE_UPDATED);
    event.SetFileName(filename);
    EventNotifier::Get()->AddPendingEvent(event);

    m_pendingFiles.erase(filename);
}