Skip to content

Commit 73dd801

Browse files
committed
Stuff
1 parent de99c5d commit 73dd801

3 files changed

Lines changed: 208 additions & 222 deletions

File tree

simplecpp.cpp

Lines changed: 76 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static unsigned long long stringToULL(const std::string &s)
155155
return ret;
156156
}
157157

158-
// TODO: added an undercore since this conflicts with a function of the same name in utils.h from Cppcheck source when building Cppcheck with MSBuild
158+
// TODO: added an undercore since this conflicts with a function of the same name in utils.h from Cppcheck source when building Cppcheck with MSBuild
159159
static bool startsWith_(const std::string &s, const std::string &p)
160160
{
161161
return (s.size() >= p.size()) && std::equal(p.begin(), p.end(), s.begin());
@@ -2568,7 +2568,8 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
25682568
}
25692569

25702570

2571-
static std::string currentDirectoryOSCalc() {
2571+
static std::string currentDirectoryOSCalc()
2572+
{
25722573
const std::size_t size = 4096;
25732574
char currentPath[size];
25742575

@@ -2582,12 +2583,14 @@ static std::string currentDirectoryOSCalc() {
25822583
return "";
25832584
}
25842585

2585-
static const std::string& currentDirectory() {
2586+
static const std::string& currentDirectory()
2587+
{
25862588
static const std::string curdir = simplecpp::simplifyPath(currentDirectoryOSCalc());
25872589
return curdir;
25882590
}
25892591

2590-
static std::string toAbsolutePath(const std::string& path) {
2592+
static std::string toAbsolutePath(const std::string& path)
2593+
{
25912594
if (path.empty()) {
25922595
return path;// preserve error file path that is indicated by an empty string
25932596
}
@@ -2598,22 +2601,25 @@ static std::string toAbsolutePath(const std::string& path) {
25982601
return simplecpp::simplifyPath(path);
25992602
}
26002603

2601-
static std::string dirPath(const std::string& path, bool withTrailingSlash=true) {
2604+
static std::string dirPath(const std::string& path, bool withTrailingSlash=true)
2605+
{
26022606
const std::size_t lastSlash = path.find_last_of("\\/");
26032607
if (lastSlash == std::string::npos) {
26042608
return "";
26052609
}
26062610
return path.substr(0, lastSlash + (withTrailingSlash ? 1U : 0U));
26072611
}
26082612

2609-
static std::string omitPathTrailingSlash(const std::string& path) {
2613+
static std::string omitPathTrailingSlash(const std::string& path)
2614+
{
26102615
if (endsWith(path, "/")) {
26112616
return path.substr(0, path.size() - 1U);
26122617
}
26132618
return path;
26142619
}
26152620

2616-
static std::string extractRelativePathFromAbsolute(const std::string& absoluteSimplifiedPath, const std::string& prefixSimplifiedAbsoluteDir = currentDirectory()) {
2621+
static std::string extractRelativePathFromAbsolute(const std::string& absoluteSimplifiedPath, const std::string& prefixSimplifiedAbsoluteDir = currentDirectory())
2622+
{
26172623
const std::string normalizedAbsolutePath = omitPathTrailingSlash(absoluteSimplifiedPath);
26182624
std::string currentPrefix = omitPathTrailingSlash(prefixSimplifiedAbsoluteDir);
26192625
std::string leadingParenting;
@@ -3054,13 +3060,13 @@ static std::string getRelativeFileName(const std::string &baseFile, const std::s
30543060
{
30553061
const std::string baseFileSimplified = simplecpp::simplifyPath(baseFile);
30563062
const std::string baseFileAbsolute = isAbsolutePath(baseFileSimplified) ?
3057-
baseFileSimplified :
3058-
simplecpp::simplifyPath(currentDirectory() + "/" + baseFileSimplified);
3063+
baseFileSimplified :
3064+
simplecpp::simplifyPath(currentDirectory() + "/" + baseFileSimplified);
30593065

30603066
const std::string headerSimplified = simplecpp::simplifyPath(header);
30613067
const std::string path = isAbsolutePath(headerSimplified) ?
3062-
headerSimplified :
3063-
simplecpp::simplifyPath(dirPath(baseFileAbsolute) + headerSimplified);
3068+
headerSimplified :
3069+
simplecpp::simplifyPath(dirPath(baseFileAbsolute) + headerSimplified);
30643070

30653071
return returnAbsolutePath ? toAbsolutePath(path) : extractRelativePathFromAbsolute(path);
30663072
}
@@ -3116,45 +3122,25 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
31163122
return openHeaderIncludePath(f, dui, header);
31173123
}
31183124

3119-
simplecpp::TokenList *simplecpp::FileDataCache::get(const std::string &sourcefile, const std::string &header, std::string *header2, const simplecpp::DUI &dui, bool systemheader)
3125+
simplecpp::FileData *simplecpp::FileDataCache::lookup(const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
31203126
{
3121-
if (mDataMap.empty())
3127+
if (mData.empty())
31223128
return nullptr;
31233129

31243130
if (isAbsolutePath(header)) {
31253131
std::string path = simplecpp::simplifyPath(header);
3126-
const auto data = mDataMap.find(path);
3127-
3128-
if (data != mDataMap.end()) {
3129-
if (header2 != nullptr) {
3130-
const auto uniquePath = mAliasMap.find(path);
3132+
const auto name_it = mNameMap.find(path);
31313133

3132-
if (uniquePath != mAliasMap.end())
3133-
*header2 = uniquePath->second;
3134-
else
3135-
*header2 = std::move(path);
3136-
}
3137-
3138-
return data->second.get();
3139-
}
3134+
if (name_it != mNameMap.end())
3135+
return name_it->second;
31403136
}
31413137

31423138
if (!systemheader) {
31433139
std::string path = getRelativeFileName(sourcefile, header, true);
3144-
const auto data = mDataMap.find(path);
3145-
3146-
if (data != mDataMap.end()) {
3147-
if (header2 != nullptr) {
3148-
const auto uniquePath = mAliasMap.find(path);
3149-
3150-
if (uniquePath != mAliasMap.end())
3151-
*header2 = uniquePath->second;
3152-
else
3153-
*header2 = std::move(path);
3154-
}
3140+
const auto name_it = mNameMap.find(path);
31553141

3156-
return data->second.get();
3157-
}
3142+
if (name_it != mNameMap.end())
3143+
return name_it->second;
31583144

31593145
// If the file exists but hasn't been loaded yet then we need to stop searching here or we could get a false match
31603146
std::ifstream f;
@@ -3165,75 +3151,57 @@ simplecpp::TokenList *simplecpp::FileDataCache::get(const std::string &sourcefil
31653151

31663152
for (std::list<std::string>::const_iterator it = dui.includePaths.begin(); it != dui.includePaths.end(); ++it) {
31673153
std::string path = getIncludePathFileName(*it, header);
3168-
const auto data = mDataMap.find(path);
3169-
3170-
if (data != mDataMap.end()) {
3171-
if (header2 != nullptr) {
3172-
const auto uniquePath = mAliasMap.find(path);
3173-
3174-
if (uniquePath != mAliasMap.end())
3175-
*header2 = uniquePath->second;
3176-
else
3177-
*header2 = std::move(path);
3178-
}
3154+
const auto name_it = mNameMap.find(path);
31793155

3180-
return data->second.get();
3181-
}
3156+
if (name_it != mNameMap.end())
3157+
return name_it->second;
31823158
}
31833159

31843160
return nullptr;
31853161
}
31863162

3187-
simplecpp::TokenList *simplecpp::FileDataCache::load(const std::string &sourcefile, const std::string &header, std::string *header2, const simplecpp::DUI &dui, bool systemheader, std::vector<std::string> &filenames, simplecpp::OutputList *outputList)
3163+
std::pair<bool, simplecpp::FileData *> simplecpp::FileDataCache::load(const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader, std::vector<std::string> &filenames, simplecpp::OutputList *outputList)
31883164
{
31893165
std::ifstream f;
31903166
std::string path = openHeader(f, dui, sourcefile, header, systemheader);
31913167

31923168
if (path.empty())
3193-
return nullptr;
3169+
return std::make_pair(false, nullptr);
31943170

31953171
FileID fileId;
31963172

31973173
if (!getFileId(path, fileId))
3198-
return nullptr;
3174+
return std::make_pair(false, nullptr);
31993175

3200-
const auto id = mIdMap.find(fileId);
3201-
if (id != mIdMap.end()) {
3202-
const auto &data = mDataMap.at(id->second);
3176+
const auto id_it = mIdMap.find(fileId);
3177+
if (id_it != mIdMap.end()) {
3178+
mNameMap.insert(std::make_pair(std::move(path), id_it->second));
32033179

3204-
mAliasMap.insert(std::make_pair(path, id->second));
3205-
mDataMap.insert(std::make_pair(path, data));
3206-
3207-
if (header2 != nullptr)
3208-
*header2 = id->second;
3209-
3210-
return data.get();
3211-
}
3212-
else {
3213-
auto data = std::make_shared<simplecpp::TokenList>(f, filenames, path, outputList);
3214-
simplecpp::TokenList *const tokens = data.get();
3180+
return std::make_pair(false, id_it->second);
3181+
} else {
3182+
/* filename must be in place before constructing token list, so allocate FileData first */
3183+
FileData *data = new FileData {std::move(path), TokenList(filenames)};
3184+
data->tokens = TokenList(f, filenames, data->filename, outputList);
32153185

32163186
if (dui.removeComments)
3217-
tokens->removeComments();
3187+
data->tokens.removeComments();
32183188

3219-
mIdMap.insert(std::make_pair(fileId, path));
3220-
mDataMap.insert(std::make_pair(path, std::move(data)));
3189+
mNameMap.insert(std::make_pair(data->filename, data));
3190+
mIdMap.insert(std::make_pair(fileId, data));
3191+
mData.push_back(std::unique_ptr<FileData>(data));
32213192

3222-
if (header2 != nullptr)
3223-
*header2 = std::move(path);
3224-
3225-
return tokens;
3193+
return std::make_pair(true, data);
32263194
}
32273195
}
32283196

3229-
simplecpp::TokenList *simplecpp::FileDataCache::get_or_load(const std::string &sourcefile, const std::string &header, std::string *header2, const simplecpp::DUI &dui, bool systemheader, std::vector<std::string> &filenames, simplecpp::OutputList *outputList)
3197+
std::pair<bool, simplecpp::FileData *> simplecpp::FileDataCache::get(const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader, std::vector<std::string> &filenames, simplecpp::OutputList *outputList)
32303198
{
3231-
simplecpp::TokenList *tokens = get(sourcefile, header, header2, dui, systemheader);
3199+
FileData *data = lookup(sourcefile, header, dui, systemheader);
32323200

3233-
if (tokens == nullptr)
3234-
tokens = load(sourcefile, header, header2, dui, systemheader, filenames, outputList);
3201+
if (data != nullptr)
3202+
return std::make_pair(false, data);
32353203

3236-
return tokens;
3204+
return load(sourcefile, header, dui, systemheader, filenames, outputList);
32373205
}
32383206

32393207
bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id)
@@ -3278,17 +3246,19 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
32783246
nonExistingFilesCache.clear();
32793247
#endif
32803248

3281-
FileDataCache filedata;
3249+
FileDataCache cache;
32823250

32833251
std::list<const Token *> filelist;
32843252

32853253
// -include files
32863254
for (std::list<std::string>::const_iterator it = dui.includes.begin(); it != dui.includes.end(); ++it) {
32873255
const std::string &filename = *it;
32883256

3289-
TokenList *tokenlist = filedata.get_or_load("", filename, nullptr, dui, false, filenames, outputList);
3257+
const auto loadResult = cache.get("", filename, dui, false, filenames, outputList);
3258+
const bool loaded = loadResult.first;
3259+
FileData *const filedata = loadResult.second;
32903260

3291-
if (tokenlist == nullptr) {
3261+
if (filedata == nullptr) {
32923262
if (outputList) {
32933263
simplecpp::Output err(filenames);
32943264
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
@@ -3299,14 +3269,16 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
32993269
continue;
33003270
}
33013271

3302-
if (!tokenlist->front()) {
3272+
if (!loaded)
3273+
continue;
3274+
3275+
if (!filedata->tokens.front())
33033276
continue;
3304-
}
33053277

33063278
if (dui.removeComments)
3307-
tokenlist->removeComments();
3279+
filedata->tokens.removeComments();
33083280

3309-
filelist.push_back(tokenlist->front());
3281+
filelist.push_back(filedata->tokens.front());
33103282
}
33113283

33123284
for (const Token *rawtok = rawtokens.cfront(); rawtok || !filelist.empty(); rawtok = rawtok ? rawtok->next : nullptr) {
@@ -3331,18 +3303,18 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
33313303
const bool systemheader = (htok->str()[0] == '<');
33323304
const std::string header(htok->str().substr(1U, htok->str().size() - 2U));
33333305

3334-
TokenList *tokenlist = filedata.get_or_load(sourcefile, header, nullptr, dui, systemheader, filenames, outputList);
3335-
if (!tokenlist)
3306+
FileData *const filedata = cache.get(sourcefile, header, dui, systemheader, filenames, outputList).second;
3307+
if (!filedata)
33363308
continue;
33373309

33383310
if (dui.removeComments)
3339-
tokenlist->removeComments();
3311+
filedata->tokens.removeComments();
33403312

3341-
if (tokenlist->front())
3342-
filelist.push_back(tokenlist->front());
3313+
if (filedata->tokens.front())
3314+
filelist.push_back(filedata->tokens.front());
33433315
}
33443316

3345-
return filedata;
3317+
return cache;
33463318
}
33473319

33483320
static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token **tok1, simplecpp::MacroMap &macros, std::vector<std::string> &files, simplecpp::OutputList *outputList)
@@ -3398,7 +3370,7 @@ static std::string getTimeDefine(const struct tm *timep)
33983370
return std::string("\"").append(buf).append("\"");
33993371
}
34003372

3401-
void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, simplecpp::FileDataCache &filedata, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond)
3373+
void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, simplecpp::FileDataCache &cache, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond)
34023374
{
34033375
#ifdef SIMPLECPP_WINDOWS
34043376
if (dui.clearIncludeCache)
@@ -3490,9 +3462,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34903462

34913463
includetokenstack.push(rawtokens.cfront());
34923464
for (std::list<std::string>::const_iterator it = dui.includes.begin(); it != dui.includes.end(); ++it) {
3493-
const TokenList *const includetokens = filedata.get_or_load("", *it, nullptr, dui, false, files, outputList);
3494-
if (includetokens != nullptr)
3495-
includetokenstack.push(includetokens->cfront());
3465+
const FileData *const filedata = cache.get("", *it, dui, false, files, outputList).second;
3466+
if (filedata != nullptr && filedata->tokens.cfront() != nullptr)
3467+
includetokenstack.push(filedata->tokens.cfront());
34963468
}
34973469

34983470
std::map<std::string, std::list<Location> > maybeUsedMacros;
@@ -3614,9 +3586,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36143586

36153587
const bool systemheader = (inctok->str()[0] == '<');
36163588
const std::string header(inctok->str().substr(1U, inctok->str().size() - 2U));
3617-
std::string header2;
3618-
const TokenList *const includetokens = filedata.get_or_load(rawtok->location.file(), header, &header2, dui, systemheader, files, outputList);
3619-
if (includetokens == nullptr) {
3589+
const FileData *const filedata = cache.get(rawtok->location.file(), header, dui, systemheader, files, outputList).second;
3590+
if (filedata == nullptr) {
36203591
if (outputList) {
36213592
simplecpp::Output out(files);
36223593
out.type = Output::MISSING_HEADER;
@@ -3632,9 +3603,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36323603
out.msg = "#include nested too deeply";
36333604
outputList->push_back(out);
36343605
}
3635-
} else if (pragmaOnce.find(header2) == pragmaOnce.end()) {
3606+
} else if (pragmaOnce.find(filedata->filename) == pragmaOnce.end()) {
36363607
includetokenstack.push(gotoNextLine(rawtok));
3637-
rawtok = includetokens ? includetokens->cfront() : nullptr;
3608+
rawtok = filedata->tokens.cfront();
36383609
continue;
36393610
}
36403611
} else if (rawtok->str() == IF || rawtok->str() == IFDEF || rawtok->str() == IFNDEF || rawtok->str() == ELIF) {
@@ -3862,9 +3833,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
38623833
}
38633834
}
38643835

3865-
void simplecpp::cleanup(FileDataCache &filedata)
3836+
void simplecpp::cleanup(FileDataCache &cache)
38663837
{
3867-
(void) filedata;
3838+
cache.clear();
38683839
}
38693840

38703841
simplecpp::cstd_t simplecpp::getCStd(const std::string &std)

0 commit comments

Comments
 (0)