Menu

[r13316]: / trunk / src / plugins / codecompletion / systemheadersthread.cpp  Maximize  Restore  History

Download this file

344 lines (289 with data), 11.4 kB

  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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
* http://www.gnu.org/licenses/gpl-3.0.html
*
* $Revision$
* $Id$
* $HeadURL$
*/
#include <sdk.h>
#ifndef CB_PRECOMP
#include <wx/app.h> // wxPostEvent
#include <wx/dir.h> // wxDirTraverser
#include <wx/event.h>
#include <wx/filename.h>
#endif
#include "systemheadersthread.h"
#define CC_SYSTEMHEADERSTHREAD_DEBUG_OUTPUT 0
#if defined(CC_GLOBAL_DEBUG_OUTPUT)
#if CC_GLOBAL_DEBUG_OUTPUT == 1
#undef CC_SYSTEMHEADERSTHREAD_DEBUG_OUTPUT
#define CC_SYSTEMHEADERSTHREAD_DEBUG_OUTPUT 1
#elif CC_GLOBAL_DEBUG_OUTPUT == 2
#undef CC_SYSTEMHEADERSTHREAD_DEBUG_OUTPUT
#define CC_SYSTEMHEADERSTHREAD_DEBUG_OUTPUT 2
#endif
#endif
#if CC_SYSTEMHEADERSTHREAD_DEBUG_OUTPUT == 1
#define TRACE(format, args...) \
CCLogger::Get()->DebugLog(F(format, ##args))
#define TRACE2(format, args...)
#elif CC_SYSTEMHEADERSTHREAD_DEBUG_OUTPUT == 2
#define TRACE(format, args...) \
do \
{ \
if (g_EnableDebugTrace) \
CCLogger::Get()->DebugLog(F(format, ##args)); \
} \
while (false)
#define TRACE2(format, args...) \
CCLogger::Get()->DebugLog(F(format, ##args))
#else
#define TRACE(format, args...)
#define TRACE2(format, args...)
#endif
// sent message when finish traversing all the folders, before the thread dies
long idSystemHeadersThreadFinish = wxNewId();
// Message used logging.
long idSystemHeadersThreadMessage = wxNewId();
// internal class declaration of HeaderDirTraverser (implementation below)
class HeaderDirTraverser : public wxDirTraverser
{
public:
HeaderDirTraverser(wxThread* thread, wxCriticalSection* critSect,
SystemHeadersMap& headersMap, const wxString& searchDir);
~HeaderDirTraverser() override;
/** call back function when we meet a file */
wxDirTraverseResult OnFile(const wxString& filename) override;
/** call back function when we meet a dir */
wxDirTraverseResult OnDir(const wxString& dirname) override;
private:
/** this function will be called every time we meet a file or a dir, and we count the file and
* dir, we temporary leave the critical section to give other thread a change to access the file
* maps. As the traversing may take a long time, in the same time, the UI may need to show the code
* completion prompt for header files, so a critical section is used here.
*/
void AddLock(bool is_file);
wxDirTraverseResult GetStatus(const wxString &path);
private:
/* the thread call Traverse() on this instance*/
wxThread* m_Thread;
/* critical section to protect accessing m_SystemHeadersMap */
wxCriticalSection* m_SystemHeadersThreadCS;
/* dir to files map, for example, you have two dirs c:/a and c:/b
* so the map looks like: (usually the relative file path is stored
* c:/a ---> {c:/a/a1.h, c:/a/a2.h} ---> {a1.h, a2.h}
* c:/b ---> {c:/b/b1.h, c:/b/b2.h} ---> {b1.h, b2.h}
*/
const SystemHeadersMap& m_SystemHeadersMap;
#ifndef __WXMSW__
struct FileID
{
dev_t st_dev;
ino_t st_ino;
bool operator< (const FileID &f) const
{
if (st_dev == f.st_dev)
return st_ino < f.st_ino;
else
return st_dev<f.st_dev;
}
};
/// Set of already visited directories (stored as device id and inode).
std::set<FileID> m_VisitedDirsByID;
#endif // __WXMSW__
/* which top level dir we are traversing header files in this Traverser object, the folder is in absolute
* format, like c:/a
*/
const wxString& m_SearchDir;
/* string set for header files, this is actually a file set reference in the m_SystemHeadersMap
* it store all the header files in relative format, such as: a1.h, a2.h
*/
StringSet& m_Headers;
/** indicates whether the critical section is entered or not, used in AddLock() function*/
bool m_Locked;
/* numbers of dirs in the traversing */
size_t m_Dirs;
/* numbers of files in the traversing */
size_t m_Files;
};
// class SystemHeadersThread
SystemHeadersThread::SystemHeadersThread(wxEvtHandler* parent,
wxCriticalSection* critSect,
SystemHeadersMap& headersMap,
const wxArrayString& incDirs) :
wxThread(wxTHREAD_JOINABLE),
m_Parent(parent),
m_SystemHeadersThreadCS(critSect),
m_SystemHeadersMap(headersMap),
m_IncludeDirs(incDirs)
{
cbAssert(incDirs.size()>0);
Create();
SetPriority(60u);
}
SystemHeadersThread::~SystemHeadersThread()
{
TRACE(_T("SystemHeadersThread: Terminated."));
}
void* SystemHeadersThread::Entry()
{
wxArrayString dirs;
{
wxCriticalSectionLocker locker(*m_SystemHeadersThreadCS);
// check to see m_SystemHeadersMap already contains the element of m_IncludeDirs, if not,
// just add one entry in the map.
for (size_t i=0; i<m_IncludeDirs.GetCount(); ++i)
{
if (m_SystemHeadersMap.find(m_IncludeDirs[i]) == m_SystemHeadersMap.end())
{
dirs.Add(m_IncludeDirs[i]);
m_SystemHeadersMap[m_IncludeDirs[i]] = StringSet();
}
}
}
// collect header files in each dir, this is done by HeaderDirTraverser
for (size_t i=0; i<dirs.GetCount(); ++i)
{
wxStopWatch timer;
timer.Start();
if ( TestDestroy() )
break;
// Detect if this directory is for the file system root and skip it.
// The user probably doesn't want to wait for the whole disk to be scanned!
wxFileName dirName(dirs[i]);
if (dirName.IsAbsolute() && dirName.GetDirCount() == 0)
continue;
// check the dir is ready for traversing
wxDir dir(dirs[i]);
if (!dir.IsOpened())
{
CodeBlocksThreadEvent evt(wxEVT_COMMAND_MENU_SELECTED, idSystemHeadersThreadMessage);
evt.SetClientData(this);
evt.SetString(wxString::Format(_T("SystemHeadersThread: Unable to open: %s"),
dirs[i].wx_str()));
wxPostEvent(m_Parent, evt);
continue;
}
{
CodeBlocksThreadEvent evt(wxEVT_COMMAND_MENU_SELECTED, idSystemHeadersThreadMessage);
evt.SetClientData(this);
evt.SetString(wxString::Format(_T("SystemHeadersThread: Start traversing: %s"),
dirs[i].wx_str()));
wxPostEvent(m_Parent, evt);
}
HeaderDirTraverser traverser(this, m_SystemHeadersThreadCS, m_SystemHeadersMap, dirs[i]);
dir.Traverse(traverser, wxEmptyString, wxDIR_FILES | wxDIR_DIRS);
if ( TestDestroy() )
break;
CodeBlocksThreadEvent evt(wxEVT_COMMAND_MENU_SELECTED, idSystemHeadersThreadMessage);
evt.SetClientData(this);
evt.SetString(wxString::Format("SystemHeadersThread: Traversing %s finished, found %zu headers; time: %.3lf sec",
dirs[i], m_SystemHeadersMap[dirs[i]].size(), timer.Time()*0.001));
wxPostEvent(m_Parent, evt);
}
// send the idSystemHeadersThreadFinish event to notify its parent before the thread dies.
if ( !TestDestroy() )
{
CodeBlocksThreadEvent evt(wxEVT_COMMAND_MENU_SELECTED, idSystemHeadersThreadFinish);
evt.SetClientData(this);
if (!dirs.IsEmpty())
evt.SetString(wxString::Format("SystemHeadersThread: Total number of paths: %zu", dirs.GetCount()));
wxPostEvent(m_Parent, evt);
}
TRACE(_T("SystemHeadersThread: Done."));
return NULL;
}
// class HeaderDirTraverser
HeaderDirTraverser::HeaderDirTraverser(wxThread* thread,
wxCriticalSection* critSect,
SystemHeadersMap& headersMap,
const wxString& searchDir) :
m_Thread(thread),
m_SystemHeadersThreadCS(critSect),
m_SystemHeadersMap(headersMap),
m_SearchDir(searchDir),
m_Headers(headersMap[searchDir]),
m_Locked(false),
m_Dirs(0),
m_Files(0)
{
}
HeaderDirTraverser::~HeaderDirTraverser()
{
if (m_Locked)
m_SystemHeadersThreadCS->Leave();
}
wxDirTraverseResult HeaderDirTraverser::OnFile(const wxString& filename)
{
// HeaderDirTraverser is used in a worker thread, so call TestDestroy() as often as it can to
// quickly terminate the thread
if (m_Thread->TestDestroy())
return wxDIR_STOP;
AddLock(true); // true means we are adding a file
wxFileName fn(filename);
if (!fn.HasExt() || fn.GetExt().GetChar(0) == _T('h'))
{
fn.MakeRelativeTo(m_SearchDir);
wxString header(fn.GetFullPath());
header.Replace(_T("\\"), _T("/"), true); // Unix style
m_Headers.insert(header);
}
return wxDIR_CONTINUE;
}
wxDirTraverseResult HeaderDirTraverser::OnDir(const wxString& dirname)
{
// HeaderDirTraverser is used in a worker thread, so call TestDestroy() as often as it can to
// quickly terminate the thread
if (m_Thread->TestDestroy())
return wxDIR_STOP;
AddLock(false); // false means we are adding a dir
#ifndef __WXMSW__
// Use stat to identify unique files. This is needed to prevent loops caused by sym-linking.
// The st_dev and st_ino are enough to uniquely identify a file on Unix like systems.
// On windows we don't detect loops because they are lest frequent and harder to make, if at all
// possible.
struct stat s;
if (stat(dirname.utf8_str().data(), &s)==0)
{
FileID f;
f.st_dev = s.st_dev;
f.st_ino = s.st_ino;
if (m_VisitedDirsByID.find(f) != m_VisitedDirsByID.end())
return wxDIR_IGNORE;
m_VisitedDirsByID.insert(f);
}
else
return wxDIR_STOP;
#endif // __WXMSW__
wxString path = cbResolveSymLinkedDirPathRecursive(dirname);
if (path.empty())
return wxDIR_IGNORE;
if (path.Last() != wxFILE_SEP_PATH)
path.Append(wxFILE_SEP_PATH);
return GetStatus(path);
}
void HeaderDirTraverser::AddLock(bool is_file)
{
if (is_file)
m_Files++;
else
m_Dirs++;
if ((m_Files+m_Dirs) % 100 == 1)
{
TRACE(wxString::Format("HeaderDirTraverser: %zu directories and %zu files traversed. Unlocking temporarily.", m_Dirs, m_Files));
if (m_Locked)
{
m_SystemHeadersThreadCS->Leave();
m_Locked = false;
}
m_SystemHeadersThreadCS->Enter();
m_Locked = true;
}
}
wxDirTraverseResult HeaderDirTraverser::GetStatus(const wxString &path)
{
if (m_SystemHeadersMap.find(path) != m_SystemHeadersMap.end())
return wxDIR_IGNORE;
return wxDIR_CONTINUE;
}