File: clangpch_cache.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (147 lines) | stat: -rw-r--r-- 5,230 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : clangpch_cache.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    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.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#if HAS_LIBCLANG

#include "clangpch_cache.h"
#include "file_logger.h"
#include "fileutils.h"
#include <wx/dir.h>
#include <wx/log.h>
#include <wx/stdpaths.h>

ClangTUCache::ClangTUCache()
    : m_maxItems(10)
{
}

ClangTUCache::~ClangTUCache() {}

ClangCacheEntry ClangTUCache::GetPCH(const wxString& filename)
{
    std::map<wxString, ClangCacheEntry>::iterator iter = m_cache.find(filename);
    if(iter == m_cache.end()) { return ClangCacheEntry(); }
    // Remove this entry from the cache. It is up to the caller to place it back!
    ClangCacheEntry entry = iter->second;
    m_cache.erase(iter);
    return entry;
}

void ClangTUCache::AddPCH(ClangCacheEntry entry)
{
    // See if we already have a cache entry for this file name
    std::map<wxString, ClangCacheEntry>::iterator iter = m_cache.find(entry.sourceFile);
    if(iter != m_cache.end() && iter->second.TU == entry.TU) {
        // the entry in the cache is the same as this one
        // Just update the access-time
        iter->second.lastAccessed = time(NULL);
        return;
    }

    entry.lastAccessed = time(NULL);
    m_cache.insert(std::make_pair(entry.sourceFile, entry));

    if(m_cache.size() > m_maxItems) {
        CL_DEBUG1(wxT("clang PCH cache size reached its maximum size, removing last accessed entry"));
        time_t oldest_timestamp = time(NULL);
        // iterate over the cache and remove the oldest item from the cache
        std::map<wxString, ClangCacheEntry>::iterator it = m_cache.begin();
        wxString key_to_remove;
        for(; it != m_cache.end(); it++) {
            if(it->second.lastAccessed <= oldest_timestamp) {
                key_to_remove = it->first;
                oldest_timestamp = it->second.lastAccessed;
            }
        }

        if(key_to_remove.IsEmpty() == false) {
            CL_DEBUG(wxT("Removing entry for key: %s"), key_to_remove.c_str());
            RemoveEntry(key_to_remove);
        }
    }
}

void ClangTUCache::Clear()
{
    CL_DEBUG(wxT("clang PCH cache cleared!"));
    std::map<wxString, ClangCacheEntry>::iterator it = m_cache.begin();
    for(; it != m_cache.end(); it++) {
        if(it->second.TU) {
            CL_DEBUG(wxT("Deleting TU: %p"), (void*)it->second.TU);
            clang_disposeTranslationUnit(it->second.TU);
        }
    }
    m_cache.clear();

    // Clear the TU from the file system
    // if(WorkspaceST::Get()->IsOpen()) {
    //    wxString clangFileSystemCacheDir;
    //    clangFileSystemCacheDir << WorkspaceST::Get()->GetWorkspaceFileName().GetPath()
    //                            << wxFileName::GetPathSeparator()
    //                            << wxT(".clang");
    //    DeleteDirectoryContent(clangFileSystemCacheDir);
    //}
}

void ClangTUCache::RemoveEntry(const wxString& filename)
{
    std::map<wxString, ClangCacheEntry>::iterator iter = m_cache.find(filename);
    if(iter != m_cache.end()) {
        CL_DEBUG(wxT("clang_disposeTranslationUnit for TU: %p"), (void*)iter->second.TU);
        clang_disposeTranslationUnit(iter->second.TU);
        {
            wxLogNull nolog;
            clRemoveFile(iter->second.fileTU);
        }
        // it is now safe to erase the iter
        m_cache.erase(iter);
    }
}

bool ClangTUCache::Contains(const wxString& filename) const { return m_cache.find(filename) != m_cache.end(); }

wxString ClangTUCache::GetTuFileName(const wxString& sourceFile) const
{
    std::map<wxString, ClangCacheEntry>::const_iterator iter = m_cache.find(sourceFile);
    if(iter != m_cache.end()) return iter->second.fileTU;
    return wxT("");
}

void ClangTUCache::DeleteDirectoryContent(const wxString& directory)
{
    wxArrayString files;
    wxDir::GetAllFiles(directory, &files, wxT("*.TU"));
    wxLogNull nolog;

    CL_DEBUG(wxT("Clearing clang TU cache from %s"), directory.c_str());
    CL_DEBUG(wxT("Total of %d files"), (int)files.GetCount());

    for(size_t i = 0; i < files.GetCount(); i++) {
        clRemoveFile(files.Item(i));
        CL_DEBUG(wxT("Deleting .. %s"), files.Item(i).c_str());
    }
}

#endif // HAS_LIBCLANG