| 1 |
#include "CxxPreProcessorCache.h" |
| 2 |
#include "CxxLexerAPI.h" |
| 3 |
#include "CxxScannerTokens.h" |
| 4 |
|
| 5 |
CxxPreProcessorCache::CxxPreProcessorCache() {} |
| 6 |
|
| 7 |
CxxPreProcessorCache::~CxxPreProcessorCache() {} |
| 8 |
|
| 9 |
bool CxxPreProcessorCache::Find(const wxString& filename, |
| 10 |
const wxString& project, |
| 11 |
const wxString& config, |
| 12 |
wxArrayString& definitions) |
| 13 |
{ |
| 14 |
wxString key; |
| 15 |
key << project << "|" << config << "|" << filename; |
| 16 |
CxxPreProcessorCache::Map_t::iterator iter = m_impl.find(key); |
| 17 |
if(iter == m_impl.end()) return false; |
| 18 |
|
| 19 |
if(wxFileName(filename).GetModificationTime().GetTicks() > iter->second.lastUpdated) { |
| 20 |
// the file file was saved since we last cached it |
| 21 |
// Next: |
| 22 |
// Check if the cached entry preabmle is the same as the current file |
| 23 |
wxString currentPreamble = GetPreamble(filename); |
| 24 |
wxString cachedPreamble = iter->second.preamble; |
| 25 |
if(cachedPreamble == currentPreamble) { |
| 26 |
// no change was done to the preamble |
| 27 |
definitions = iter->second.definitions; |
| 28 |
return true; |
| 29 |
} else { |
| 30 |
// preamble was modified - we need to re-cache it |
| 31 |
// remove the current cache and return false |
| 32 |
m_impl.erase(iter); |
| 33 |
return false; |
| 34 |
} |
| 35 |
} else { |
| 36 |
// the file was not saved since we cached it |
| 37 |
definitions = iter->second.definitions; |
| 38 |
return false; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
void CxxPreProcessorCache::Clear() { m_impl.clear(); } |
| 43 |
|
| 44 |
wxString CxxPreProcessorCache::GetPreamble(const wxString& filename) const |
| 45 |
{ |
| 46 |
Scanner_t scanner = ::LexerNew(filename, 0); |
| 47 |
if(!scanner) return { "" }; |
| 48 |
|
| 49 |
CxxLexerToken token; |
| 50 |
wxString preamble; |
| 51 |
while(::LexerNext(scanner, token)) { |
| 52 |
if(token.GetType() == T_PP_INCLUDE_FILENAME) { |
| 53 |
preamble << token.GetText() << "\n"; |
| 54 |
} |
| 55 |
} |
| 56 |
::LexerDestroy(&scanner); |
| 57 |
|
| 58 |
preamble.Trim(); |
| 59 |
return preamble; |
| 60 |
} |
| 61 |
|
| 62 |
bool CxxPreProcessorCache::Insert(const wxString& filename, |
| 63 |
const wxString& project, |
| 64 |
const wxString& config, |
| 65 |
const wxArrayString& definitions) |
| 66 |
{ |
| 67 |
wxString key; |
| 68 |
key << project << "|" << config << "|" << filename; |
| 69 |
|
| 70 |
// remove old entry |
| 71 |
CxxPreProcessorCache::Map_t::iterator iter = m_impl.find(key); |
| 72 |
if(iter != m_impl.end()) m_impl.erase(iter); |
| 73 |
|
| 74 |
CacheEntry entry; |
| 75 |
entry.config = config; |
| 76 |
entry.definitions = definitions; |
| 77 |
entry.lastUpdated = time(NULL); // Now |
| 78 |
entry.filename = filename; |
| 79 |
entry.project = project; |
| 80 |
entry.preamble = GetPreamble(filename); |
| 81 |
m_impl.insert(std::make_pair(key, entry)); |
| 82 |
return true; |
| 83 |
} |