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
|
/***************************************************************************
copyright : (C) 2006 by David Nolden
email : david.nolden.kdevelop@art-master.de
***************************************************************************/
/***************************************************************************
* *
* 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 "lexercache.h"
#include "driver.h"
#include <kdebug.h>
LexerCache::LexerCache( Driver* d ) : m_driver( d ) {}
void LexerCache::addLexedFile( const CachedLexedFilePointer& file ) {
//kdDebug( 9007 ) << "LexerCache: adding an instance of " << file->fileName().str() << endl;
std::pair< CachedLexedFileMap::iterator, CachedLexedFileMap::iterator> files = m_files.equal_range( file->fileName() );
if ( files.first == files.second ) {
m_files.insert( std::make_pair( file->fileName(), file ) );
} else {
//Make sure newer files appear first
m_files.insert( files.first, std::make_pair( file->fileName(), file ) );
}
int cnt = 0;
while ( files.first != files.second ) {
if ( sourceChanged( *( *( files.first ) ).second ) ) {
m_files.erase( files.first++ );
} else {
cnt++;
files.first++;
}
}
//kdDebug( 9007 ) << "LexerCache: new count of cached instances for the file: " << cnt << endl;
}
CachedLexedFilePointer LexerCache::lexedFile( const HashedString& fileName ) {
initFileModificationCache();
std::pair< CachedLexedFileMap::iterator, CachedLexedFileMap::iterator> files = m_files.equal_range( fileName );
///@todo optimize with standard-algorithms(by first computing the intersection)
/* if( files.first != files.second )
//kdDebug( 9007 ) << "LexerCache: cache for file " << fileName.str() << " is not empty" << endl;
else
//kdDebug( 9007 ) << "LexerCache: cache for file " << fileName.str() << " is empty" << endl;*/
while ( files.first != files.second ) {
const CachedLexedFile& file( *( *( files.first ) ).second );
if ( sourceChanged( file ) ) {
//kdDebug( 9007 ) << "LexerCache: cache for file " << fileName.str() << " is being discarded because the file was modified" << endl;
m_files.erase( files.first++ );
continue;
}
bool success = true;
//Make sure that none of the macros stored in the driver affect the file in a different way than the one before
Driver::MacroMap::const_iterator end = m_driver->macros().end();
for ( Driver::MacroMap::const_iterator rit = m_driver->macros().begin(); rit != end; ) {
Driver::MacroMap::const_iterator it = rit;
++rit;
if ( rit != end && ( *it ).first == ( *rit ).first ) continue; //Always only use the last macro of the same name for comparison, it is on top of the macro-stack
if (( *it ).second.isUndef() ) continue; //Undef-macros theoretically don't exist
if ( file.hasString(( *it ).first ) ) {
if ( file.m_usedMacros.hasMacro(( *it ).first ) ) {
Macro m( file.m_usedMacros.macro(( *it ).first.str() ) );
if ( !( m == ( *it ).second ) ) {
//kdDebug( 9007 ) << "LexerCache: The cached file " << fileName.str() << " depends on the string \"" << ( *it ).first.str() << "\" and used a macro for it with the body \"" << m.body() << "\"(from " << m.fileName() << "), but the driver contains the same macro with body \"" << ( *it ).second.body() << "\"(from " << ( *it ).second.fileName() << "), cache is not used" << endl;
//Macro with the same name was used, but it is different
success = false;
break;
}
} else {
//There is a macro that affects the file, but was not used while the previous parse
//kdDebug( 9007 ) << "LexerCache: The cached file " << fileName.str() << " depends on the string \"" << ( *it ).first.str() << "\" and the driver contains a macro of that name with body \"" << ( *it ).second.body() << "\"(from " << ( *it ).second.fileName() << "), the cached file is not used" << endl;
success = false;
break;
}
}
}
//Make sure that all external macros used by the file now exist too
MacroSet::Macros::const_iterator end2 = file.usedMacros().macros().end();
for ( MacroSet::Macros::const_iterator it = file.usedMacros().macros().begin(); it != end2; ++it ) {
if ( !m_driver->hasMacro( HashedString(( *it ).name() ) ) ) {
//kdDebug( 9007 ) << "LexerCache: The cached file " << fileName.str() << " used a macro called \"" << it->name() << "\"(from " << it->fileName() << "), but the driver does not contain that macro, the cached file is not used" << endl;
success = false;
break;
}
}
if ( success ) {
//kdDebug( 9007 ) << "LexerCache: Using cached file " << fileName.str() << endl;
(*files.first).second->access();
return ( *files.first ).second;
}
++files.first;
}
return CachedLexedFilePointer();
}
TQDateTime LexerCache::fileModificationTimeCached( const HashedString& fileName ) {
FileModificationMap::const_iterator it = m_fileModificationCache.find( fileName );
if( it != m_fileModificationCache.end() ) {
///Use the cache for 10 seconds
if( (*it).second.m_readTime.secsTo( m_currentDateTime ) < 10 ) {
return (*it).second.m_modificationTime;
}
}
TQFileInfo fileInfo( fileName.str() );
m_fileModificationCache[fileName].m_readTime = TQDateTime::currentDateTime();
m_fileModificationCache[fileName].m_modificationTime = fileInfo.lastModified();
return fileInfo.lastModified();
}
//Should be cached too!
bool LexerCache::sourceChanged( const CachedLexedFile& file ) {
//@todo Check if any of the dependencies changed
TQDateTime modTime = fileModificationTimeCached( file.fileName() );
if ( modTime != file.modificationTime() )
return true;
for( TQMap<HashedString, TQDateTime>::const_iterator it = file.allModificationTimes().begin(); it != file.allModificationTimes().end(); ++it ) {
TQDateTime modTime = fileModificationTimeCached( it.key() );
if( modTime != *it )
return true;
}
return false;
}
void LexerCache::clear() {
m_files.clear();
m_totalStringSet.clear();
m_fileModificationCache.clear();
}
void LexerCache::erase( const CacheNode* node ) {
std::pair< CachedLexedFileMap::iterator, CachedLexedFileMap::iterator> files = m_files.equal_range( ((const CachedLexedFile*)(node))->fileName() );
while ( files.first != files.second ) {
if( (*files.first).second.data() == ((const CachedLexedFile*)(node)) ) {
m_files.erase( files.first );
return;
}
files.first++;
}
//kdDebug( 9007 ) << "Error: could not find a node in the list for file " << ((const CachedLexedFile*)(node))->fileName().str() << endl;
}
CachedLexedFile::CachedLexedFile( const HashedString& fileName, LexerCache* manager ) : CacheNode( manager ), m_fileName( fileName ) {
TQFileInfo fileInfo( fileName.str() );
m_modificationTime = fileInfo.lastModified();
m_allModificationTimes[ fileName ] = m_modificationTime;
}
void CachedLexedFile::addDefinedMacro( const Macro& macro ) {
#ifdef LEXERCACHE_DEBUG
//kdDebug( 9007 ) << "defined macro " << macro.name() << endl;
#endif
m_definedMacros.addMacro( macro );
m_definedMacroNames.insert( HashedString( macro.name() ) );
}
void CachedLexedFile::addUsedMacro( const Macro& macro ) {
if ( !m_definedMacros.hasMacro( macro.name() ) ) {
#ifdef LEXERCACHE_DEBUG
//kdDebug( 9007 ) << "used macro " << macro.name() << endl;
#endif
m_usedMacros.addMacro( macro );
}
}
void CachedLexedFile::addIncludeFile( const HashedString& file, const TQDateTime& modificationTime ) {
m_includeFiles.insert( file );
m_allModificationTimes[file] = modificationTime;
}
TQDateTime CachedLexedFile::modificationTime() const {
return m_modificationTime;
}
void CachedLexedFile::addProblem( const Problem& p ) {
m_problems << p;
}
TQValueList<Problem> CachedLexedFile::problems() const {
return m_problems;
}
//The parameter should be a CachedLexedFile that was lexed AFTER the content of this file
void CachedLexedFile::merge( const CachedLexedFile& file ) {
#ifdef LEXERCACHE_DEBUG
//kdDebug( 9007 ) << fileName().str() << ": merging " << file.fileName().str() << endl << "defined in this: " << m_definedMacroNames.print().c_str() << endl << "defined macros in other: " << file.m_definedMacroNames.print().c_str() << endl;;
#endif
HashedStringSet tempStrings = file.m_strings;
tempStrings -= m_definedMacroNames;
m_strings += tempStrings;
m_includeFiles += file.m_includeFiles;
//Only add macros to the usedMacros-list that were not defined locally
for ( MacroSet::Macros::const_iterator it = file.m_usedMacros.macros().begin(); it != file.m_usedMacros.macros().end(); ++it ) {
if ( !m_definedMacros.hasMacro(( *it ).name() ) ) {///If the macro was not defined locally, add it to the macros-list.
#ifdef LEXERCACHE_DEBUG
//kdDebug( 9007 ) << "inserting used macro " << ( *it ).name() << endl;
#endif
m_usedMacros.addMacro( *it );
}
}
m_definedMacros.merge( file.m_definedMacros );
m_definedMacroNames += file.m_definedMacroNames;
for( TQMap<HashedString, TQDateTime>::const_iterator it = file.m_allModificationTimes.begin(); it != file.m_allModificationTimes.end(); ++it )
m_allModificationTimes[it.key()] = *it;
#ifdef LEXERCACHE_DEBUG
//kdDebug( 9007 ) << fileName().str() << ": defined in this after merge: " << m_definedMacroNames.print().c_str() << endl;
#endif
m_problems += file.m_problems;
}
size_t CachedLexedFile::hash() const {
return m_usedMacros.valueHash() + m_usedMacros.idHash() + m_definedMacros.idHash() + m_definedMacros.valueHash() + m_strings.hash();
}
void LexerCache::initFileModificationCache() {
m_currentDateTime = TQDateTime::currentDateTime();
}
void LexerCache::saveMemory() {
m_fileModificationCache.clear();
m_totalStringSet.clear(); ///it's unclear how often this should be emptied. It may happen that completely unused strings end up in this set, then deleting it will save us memory.
}
|