Skip to content

Commit 9dc2c3d

Browse files
authored
added DUI::clearIncludeCache to clear the non-existing files cache (#287)
1 parent 7c5b21d commit 9dc2c3d

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

simplecpp.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,11 @@ class NonExistingFilesCache {
27962796
m_pathSet.insert(path);
27972797
}
27982798

2799+
void clear() {
2800+
ScopedLock lock(m_criticalSection);
2801+
m_pathSet.clear();
2802+
}
2803+
27992804
private:
28002805
std::set<std::string> m_pathSet;
28012806
CRITICAL_SECTION m_criticalSection;
@@ -2807,22 +2812,18 @@ static NonExistingFilesCache nonExistingFilesCache;
28072812

28082813
static std::string openHeader(std::ifstream &f, const std::string &path)
28092814
{
2810-
#ifdef SIMPLECPP_WINDOWS
28112815
std::string simplePath = simplecpp::simplifyPath(path);
2816+
#ifdef SIMPLECPP_WINDOWS
28122817
if (nonExistingFilesCache.contains(simplePath))
28132818
return ""; // file is known not to exist, skip expensive file open call
2814-
2819+
#endif
28152820
f.open(simplePath.c_str());
28162821
if (f.is_open())
28172822
return simplePath;
2818-
else {
2819-
nonExistingFilesCache.add(simplePath);
2820-
return "";
2821-
}
2822-
#else
2823-
f.open(path.c_str());
2824-
return f.is_open() ? simplecpp::simplifyPath(path) : "";
2823+
#ifdef SIMPLECPP_WINDOWS
2824+
nonExistingFilesCache.add(simplePath);
28252825
#endif
2826+
return "";
28262827
}
28272828

28282829
static std::string getRelativeFileName(const std::string &sourcefile, const std::string &header)
@@ -2905,6 +2906,11 @@ static bool hasFile(const std::map<std::string, simplecpp::TokenList *> &filedat
29052906

29062907
std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::TokenList &rawtokens, std::vector<std::string> &filenames, const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
29072908
{
2909+
#ifdef SIMPLECPP_WINDOWS
2910+
if (dui.clearIncludeCache)
2911+
nonExistingFilesCache .clear();
2912+
#endif
2913+
29082914
std::map<std::string, simplecpp::TokenList*> ret;
29092915

29102916
std::list<const Token *> filelist;
@@ -3030,6 +3036,11 @@ static std::string getTimeDefine(struct tm *timep)
30303036

30313037
void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, std::map<std::string, simplecpp::TokenList *> &filedata, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond)
30323038
{
3039+
#ifdef SIMPLECPP_WINDOWS
3040+
if (dui.clearIncludeCache)
3041+
nonExistingFilesCache.clear();
3042+
#endif
3043+
30333044
std::map<std::string, std::size_t> sizeOfType(rawtokens.sizeOfType);
30343045
sizeOfType.insert(std::make_pair("char", sizeof(char)));
30353046
sizeOfType.insert(std::make_pair("short", sizeof(short)));

simplecpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,13 @@ namespace simplecpp {
314314
* On the command line these are configured by -D, -U, -I, --include, -std
315315
*/
316316
struct SIMPLECPP_LIB DUI {
317-
DUI() {}
317+
DUI() : clearIncludeCache(false) {}
318318
std::list<std::string> defines;
319319
std::set<std::string> undefined;
320320
std::list<std::string> includePaths;
321321
std::list<std::string> includes;
322322
std::string std;
323+
bool clearIncludeCache;
323324
};
324325

325326
SIMPLECPP_LIB long long characterLiteralToLL(const std::string& str);

0 commit comments

Comments
 (0)