File: lexercache.cpp

package info (click to toggle)
umbrello 4%3A25.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,212 kB
  • sloc: cpp: 144,235; php: 2,405; sh: 855; xml: 354; cs: 309; java: 91; python: 68; makefile: 11; sql: 7
file content (253 lines) | stat: -rw-r--r-- 9,855 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2006 David Nolden <david.nolden.kdevelop@art-master.de>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "lexercache.h"

#define DBG_SRC QLatin1String("LexerCache")
#include "debug_utils.h"
#include "driver.h"

DEBUG_REGISTER_DISABLED(LexerCache)

LexerCache::LexerCache(Driver* d) : m_driver(d) {}

void LexerCache::addLexedFile(const CachedLexedFilePointer& file)
{
    DEBUG() << "adding an instance of " << file->fileName().str() ;

    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++;
        }
    }
    DEBUG() << "new count of cached instances for the file: " << cnt ;
}

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)
          DEBUG() << "LexerCache: cache for file " << fileName.str() << " is not empty" ;
      else
          DEBUG() << "LexerCache: cache for file " << fileName.str() << " is empty" ;*/

    while (files.first != files.second) {
        const CachedLexedFile& file(*(*(files.first)).second);
        if (sourceChanged(file)) {
            DEBUG() <<  "cache for file " << fileName.str() << " is being discarded because the file was modified" ;
            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)) {
                        DEBUG() << "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" ;

                        //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
                    DEBUG() << "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" ;
                    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()))) {
                DEBUG() << "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" ;
                success = false;
                break;
            }
        }

        if (success) {
            DEBUG() << "Using cached file " << fileName.str() ;
            (*files.first).second->access();
            return (*files.first).second;
        }
        ++files.first;
    }
    return CachedLexedFilePointer();
}

QDateTime LexerCache::fileModificationTimeCached(const HashedString& fileName)
{
    FileModificationMap::const_iterator it = m_fileModificationCache.constFind(fileName);
    if (it != m_fileModificationCache.constEnd()) {
        ///Use the cache for 10 seconds
        if ((*it).m_readTime.secsTo(m_currentDateTime) < 10) {
            return (*it).m_modificationTime;
        }
    }

    QFileInfo fileInfo(fileName.str());
    m_fileModificationCache[fileName].m_readTime = QDateTime::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

    QDateTime modTime = fileModificationTimeCached(file.fileName());

    if (modTime != file.modificationTime())
        return true;

    for (QMap<HashedString, QDateTime>::const_iterator it = file.allModificationTimes().begin(); it != file.allModificationTimes().end(); ++it) {
        QDateTime 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 == ((const CachedLexedFile*)(node))) {
            m_files.erase(files.first);
            return;
        }
        files.first++;
    }
    DEBUG() << "Error: could not find a node in the list for file " << ((const CachedLexedFile*)(node))->fileName().str() ;
}

CachedLexedFile::CachedLexedFile(const HashedString& fileName, LexerCache* manager) : CacheNode(manager), m_fileName(fileName)
{
    QFileInfo fileInfo(fileName.str());
    m_modificationTime = fileInfo.lastModified();
    m_allModificationTimes[ fileName ] = m_modificationTime;
}

void CachedLexedFile::addDefinedMacro(const Macro& macro)
{
    DEBUG() << "defined macro " << macro.name() ;

    m_definedMacros.addMacro(macro);
    m_definedMacroNames.insert(HashedString(macro.name()));
}

void CachedLexedFile::addUsedMacro(const Macro& macro)
{
    if (!m_definedMacros.hasMacro(macro.name())) {
        DEBUG() << "used macro " << macro.name() ;
        m_usedMacros.addMacro(macro);
    }
}

void CachedLexedFile::addIncludeFile(const HashedString& file, const QDateTime& modificationTime)
{
    m_includeFiles.insert(file);
    m_allModificationTimes[file] = modificationTime;
}


QDateTime CachedLexedFile::modificationTime() const
{
    return m_modificationTime;
}

void CachedLexedFile::addProblem(const Problem& p)
{
    m_problems << p;
}

QList<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)
{
    DEBUG() << fileName().str() << ": merging " << file.fileName().str() << QLatin1Char('\n') << "defined in this: " << m_definedMacroNames.print().c_str() << QLatin1Char('\n')<< "defined macros in other: " << file.m_definedMacroNames.print().c_str() ;;

    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.
            DEBUG() << "inserting used macro " << (*it).name() ;
            m_usedMacros.addMacro(*it);
        }
    }

    m_definedMacros.merge(file.m_definedMacros);
    m_definedMacroNames += file.m_definedMacroNames;

    for (QMap<HashedString, QDateTime>::const_iterator it = file.m_allModificationTimes.begin(); it != file.m_allModificationTimes.end(); ++it)
        m_allModificationTimes[it.key()] = *it;

    DEBUG() << fileName().str() << ": defined in this after merge: " << m_definedMacroNames.print().c_str() ;

    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 = QDateTime::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.
}