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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
/***************************************************************************
* Copyright (C) 2002 by Roberto Raggi *
* roberto@kdevelop.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "backgroundparser.h"
#include "cppsupportpart.h"
#include "cppsupport_events.h"
#include "codeinformationrepository.h"
#include "cppcodecompletion.h"
#include "ast_utils.h"
#include "kdevdeepcopy.h"
#include "kdevdriver.h"
#include <tqmutex.h>
#include <tdeparts/part.h>
#include <tdetexteditor/editinterface.h>
#include <tdetexteditor/document.h>
#include <tdetexteditor/view.h>
#include <kdevpartcontroller.h>
#include <kdevproject.h>
#include <kurl.h>
#include <kdebug.h>
#include <tdeapplication.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqtextstream.h>
#include <list>
#include <tqdatastream.h>
class BackgroundKDevDriver : public KDevDriver {
public:
BackgroundKDevDriver( CppSupportPart* cppSupport, BackgroundParser* bp ) : KDevDriver( cppSupport, false ), m_backgroundParser(bp) {
}
virtual void fileParsed( ParsedFile& fileName );
virtual void addDependence( const TQString& fileName, const Dependence& dep );
private:
BackgroundParser* m_backgroundParser;
};
class KDevSourceProvider: public SourceProvider
{
public:
//Deadlock is a mutex that is locked when KDevSourceProvider::contents(..) is used, and that should be unlocked before TQApplication is locked(that way a deadlock where the thread that holds the TQApplication-mutex and tries to lock the given mutex, while the thread that calls contents(..) and holds the given mutex and tries to lock the TQApplication-mutex, cannot happen)
KDevSourceProvider( CppSupportPart* cppSupport, TQMutex& deadlock )
: m_cppSupport( cppSupport ),
m_readFromDisk( false ),
m_deadlock(deadlock)
{}
void setReadFromDisk( bool b )
{
m_readFromDisk = b;
}
bool readFromDisk() const
{
return m_readFromDisk;
}
virtual TQString contents( const TQString& fileName )
{
TQString contents = TQString();
if ( !m_readFromDisk )
{
m_deadlock.unlock();
// GET LOCK
kapp->lock ();
//kdDebug(9007) << "-------> kapp locked" << endl;
TQPtrList<KParts::Part> parts( *m_cppSupport->partController() ->parts() );
TQPtrListIterator<KParts::Part> it( parts );
while ( it.current() )
{
KTextEditor::Document * doc = dynamic_cast<KTextEditor::Document*>( it.current() );
++it;
KTextEditor::EditInterface* editIface = dynamic_cast<KTextEditor::EditInterface*>( doc );
if ( !doc || !editIface || doc->url().path() != fileName )
continue;
contents = TQString( editIface->text().ascii() ); // deep copy
//kdDebug(9007) << "-------> kapp unlocked" << endl;
break;
}
// RELEASE LOCK
kapp->unlock();
m_deadlock.lock();
//kdDebug(9007) << "-------> kapp unlocked" << endl;
}
if( m_readFromDisk || contents == TQString() )
{
TQFile f( fileName );
if ( f.open( IO_ReadOnly ) )
{
TQTextStream stream( &f );
contents = stream.read();
f.close();
}
}
return contents;
}
virtual bool isModified( const TQString& fileName )
{
bool ret = false;
m_deadlock.unlock();
kapp->lock ();
KParts::ReadOnlyPart *part = m_cppSupport->partController()->partForURL( KURL(fileName) );
KTextEditor::Document * doc = dynamic_cast<KTextEditor::Document*>( part );
if ( doc )
ret = doc->isModified();
kapp->unlock();
m_deadlock.lock();
return ret;
}
private:
CppSupportPart* m_cppSupport;
bool m_readFromDisk;
TQMutex& m_deadlock;
private:
KDevSourceProvider( const KDevSourceProvider& source );
void operator = ( const KDevSourceProvider& source );
};
typedef std::string SafeString;
class SynchronizedFileList
{
typedef std::list< TQPair<SafeString, bool> > ListType;
public:
SynchronizedFileList()
{}
bool isEmpty() const
{
TQMutexLocker locker( &m_mutex );
return m_fileList.empty();
}
uint count() const
{
TQMutexLocker locker( &m_mutex );
return m_fileList.size();
}
TQPair<SafeString, bool> front() const
{
TQMutexLocker locker( &m_mutex );
return m_fileList.front();
}
void clear()
{
TQMutexLocker locker( &m_mutex );
m_fileList.clear();
}
void push_front( const TQString& fileName, bool readFromDisk = false )
{
SafeString s( fileName.ascii() );
TQMutexLocker locker( &m_mutex );
m_fileList.push_front( tqMakePair( s, readFromDisk ) );
}
void push_back( const TQString& fileName, bool readFromDisk = false )
{
SafeString s( fileName.ascii() );
TQMutexLocker locker( &m_mutex );
m_fileList.push_back( tqMakePair( s, readFromDisk ) );
}
void pop_front()
{
TQMutexLocker locker( &m_mutex );
m_fileList.pop_front();
}
int count( const TQString& fileName ) const {
int c = 0;
TQMutexLocker locker( &m_mutex );
ListType::const_iterator it = m_fileList.begin();
while ( it != m_fileList.end() )
{
if ( ( *it ).first.compare( fileName.ascii() ) == 0 )
++c;
++it;
}
return c;
}
TQPair<SafeString, bool> takeFront()
{
TQMutexLocker locker( &m_mutex );
TQPair<SafeString, bool> ret = m_fileList.front();
m_fileList.pop_front();
return ret;
}
bool contains( const TQString& fileName ) const
{
TQMutexLocker locker( &m_mutex );
ListType::const_iterator it = m_fileList.begin();
while ( it != m_fileList.end() )
{
if ( ( *it ).first.compare( fileName.ascii() ) == 0 )
return true;
++it;
}
return false;
}
void remove( const TQString& fileName )
{
TQMutexLocker locker( &m_mutex );
ListType::iterator it = m_fileList.begin();
while ( it != m_fileList.end() )
{
if ( ( *it ).first.compare(fileName.ascii() ) == 0 )
m_fileList.erase( it++ );
else
++it;
}
}
private:
mutable TQMutex m_mutex;
ListType m_fileList;
};
BackgroundParser::BackgroundParser( CppSupportPart* part, TQWaitCondition* consumed )
: m_consumed( consumed ), m_cppSupport( part ), m_close( false ), m_saveMemory( false )
{
m_fileList = new SynchronizedFileList();
m_driver = new BackgroundKDevDriver( m_cppSupport, this );
m_driver->setSourceProvider( new KDevSourceProvider( m_cppSupport, m_mutex ) );
TQString conf_file_name = m_cppSupport->specialHeaderName();
m_mutex.lock();
if ( TQFile::exists( conf_file_name ) )
m_driver->parseFile( conf_file_name, true, true, true );
m_mutex.unlock();
//disabled for now m_driver->setResolveDependencesEnabled( true );
}
BackgroundParser::~BackgroundParser()
{
removeAllFiles();
delete( m_driver );
m_driver = 0;
delete m_fileList;
m_fileList = 0;
}
void BackgroundParser::addFile( const TQString& fileName, bool readFromDisk )
{
TQString fn = deepCopy( fileName );
//bool added = false;
/*if ( !m_fileList->contains( fn ) )
{
m_fileList->push_back( fn, readFromDisk );
added = true;
}*/
m_fileList->push_back( fn, readFromDisk );
//if ( added )
m_canParse.wakeAll();
}
void BackgroundParser::addFileFront( const TQString& fileName, bool readFromDisk )
{
TQString fn = deepCopy( fileName );
bool added = false;
/*if ( m_fileList->contains( fn ) )
m_fileList->remove( fn );*/
m_fileList->push_front( fn, readFromDisk );
added = true;
if ( added )
m_canParse.wakeAll();
}
void BackgroundParser::removeAllFiles()
{
kdDebug( 9007 ) << "BackgroundParser::removeAllFiles()" << endl;
TQMutexLocker locker( &m_mutex );
TQMap<TQString, Unit*>::Iterator it = m_unitDict.begin();
while ( it != m_unitDict.end() )
{
Unit * unit = it.data();
++it;
delete( unit );
unit = 0;
}
m_unitDict.clear();
m_driver->reset();
m_fileList->clear();
m_isEmpty.wakeAll();
}
void BackgroundParser::removeFile( const TQString& fileName )
{
TQMutexLocker locker( &m_mutex );
Unit* unit = findUnit( fileName );
if ( unit )
{
m_driver->remove
( fileName );
m_unitDict.remove( fileName );
delete( unit );
unit = 0;
}
if ( m_fileList->isEmpty() )
m_isEmpty.wakeAll();
}
void BackgroundKDevDriver::addDependence( const TQString& fileName, const Dependence& dep ) {
//give waiting threads a chance to perform their actions
m_backgroundParser->m_mutex.unlock();
m_backgroundParser->m_mutex.lock();
KDevDriver::addDependence( fileName, dep );
}
void BackgroundKDevDriver::fileParsed( ParsedFile& fileName ) {
m_backgroundParser->fileParsed( fileName );
}
void BackgroundParser::parseFile( const TQString& fileName, bool readFromDisk, bool lock )
{
if( lock )
m_mutex.lock();
m_readFromDisk = readFromDisk;
static_cast<KDevSourceProvider*>( m_driver->sourceProvider() ) ->setReadFromDisk( readFromDisk );
m_driver->remove( fileName );
m_driver->parseFile( fileName , false, true );
if( !m_driver->isResolveDependencesEnabled() )
m_driver->removeAllMacrosInFile( fileName ); // romove all macros defined by this
// translation unit.
if ( lock )
m_mutex.unlock();
}
TQValueList<Problem> cloneProblemList( const TQValueList<Problem>& list ) {
TQValueList<Problem> ret;
for( TQValueList<Problem>::const_iterator it = list.begin(); it != list.end(); ++it ) {
ret << Problem( *it, true );
}
return ret;
}
void BackgroundParser::fileParsed( ParsedFile& file ) {
ParsedFilePointer translationUnitUnsafe = m_driver->takeTranslationUnit( file.fileName() );
//now file and translationUnitUnsafe are the same
ParsedFilePointer translationUnit;
//Since the lexer-cache keeps many TQStrings like macro-names used in the background, everything must be copied here. The safest solution is just
//serializing and deserializing the whole thing(the serialization does not respect the AST, but that can be copied later because that's safe)
TQMemArray<char> data;
{
TQDataStream stream( TQByteArray(data), IO_WriteOnly );
translationUnitUnsafe->write( stream );
}
{
TQDataStream stream( TQByteArray(data), IO_ReadOnly );
translationUnit = new ParsedFile( stream );
}
translationUnit->setTranslationUnit( translationUnitUnsafe->operator TranslationUnitAST *() ); //Copy the AST, doing that is thread-safe
translationUnitUnsafe->setTranslationUnit( 0 ); //Move the AST completely out of this thread's scope. Else it might crash on dual-core machines
file.setTranslationUnit(0); //just to be sure, set to zero on both
Unit* unit = new Unit;
unit->fileName = file.fileName();
unit->translationUnit = translationUnit;
unit->problems = cloneProblemList( m_driver->problems( file.fileName() ) );
static_cast<KDevSourceProvider*>( m_driver->sourceProvider() ) ->setReadFromDisk( false );
if ( m_unitDict.find( file.fileName() ) != m_unitDict.end() )
{
Unit * u = m_unitDict[ file.fileName() ];
m_unitDict.remove( file.fileName() );
delete( u );
u = 0;
}
m_unitDict.insert( file.fileName(), unit );
TDEApplication::postEvent( m_cppSupport, new FileParsedEvent( file.fileName(), unit->problems, m_readFromDisk ) );
m_currentFile = TQString();
if ( m_fileList->isEmpty() )
m_isEmpty.wakeAll();
}
Unit* BackgroundParser::findUnit( const TQString& fileName )
{
TQMap<TQString, Unit*>::Iterator it = m_unitDict.find( fileName );
return it != m_unitDict.end() ? *it : 0;
}
bool BackgroundParser::hasTranslationUnit( const TQString& fileName ) {
TQMap<TQString, Unit*>::Iterator it = m_unitDict.find( fileName );
return it != m_unitDict.end();
}
ParsedFilePointer BackgroundParser::translationUnit( const TQString& fileName )
{
Unit * u = findUnit( fileName );
if ( u == 0 )
{
return 0;
/*m_fileList->remove
( fileName );
u = parseFile( fileName, false );*/
}
return u->translationUnit;
}
TQValueList<Problem> BackgroundParser::problems( const TQString& fileName, bool readFromDisk, bool forceParse )
{
Q_UNUSED(readFromDisk);
Unit * u = findUnit( fileName );
if ( u == 0 || forceParse )
{
/*
m_fileList->remove
( fileName );
u = parseFile( fileName, readFromDisk ); */
}
return u ? u->problems : TQValueList<Problem>();
}
void BackgroundParser::close()
{
{
TQMutexLocker locker( &m_mutex );
m_close = true;
m_canParse.wakeAll();
}
kapp->unlock();
while ( running() )
sleep( 1 );
kapp->lock();
}
bool BackgroundParser::filesInQueue()
{
TQMutexLocker locker( &m_mutex );
return m_fileList->count() || !m_currentFile.isEmpty();
}
int BackgroundParser::countInQueue( const TQString& file ) const {
return m_fileList->count( file );
}
void BackgroundParser::updateParserConfiguration()
{
TQMutexLocker locker( &m_mutex );
m_driver->setup();
TQString conf_file_name = m_cppSupport->specialHeaderName();
m_driver->removeAllMacrosInFile( conf_file_name );
m_driver->parseFile( conf_file_name, true, true, true );
}
void BackgroundParser::run()
{
// (void) m_cppSupport->codeCompletion()->repository()->getEntriesInScope( TQStringList(), false );
while ( !m_close )
{
while ( m_fileList->isEmpty() )
{
if( m_saveMemory ) {
m_saveMemory = false;
m_driver->lexerCache()->saveMemory();
}
m_canParse.wait();
if ( m_close )
break;
}
if ( m_close )
break;
TQPair<SafeString, bool> entry = m_fileList->takeFront();
TQString fileName = entry.first.c_str();
bool readFromDisk = entry.second;
m_currentFile = deepCopy(fileName);
( void ) parseFile( fileName, readFromDisk, true );
m_currentFile = TQString();
}
kdDebug( 9007 ) << "!!!!!!!!!!!!!!!!!! BG PARSER DESTROYED !!!!!!!!!!!!" << endl;
// adymo: commented to fix #88091
// TQThread::exit();
}
void BackgroundParser::saveMemory() {
m_saveMemory = true; //Delay the operation
m_canParse.wakeAll();
}
|