@@ -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
159159static 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,55 @@ 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+ FileData *data = new FileData {path, TokenList (f, filenames, path, outputList)};
32153183
32163184 if (dui.removeComments )
3217- tokens-> removeComments ();
3185+ data-> tokens . removeComments ();
32183186
3219- mIdMap .insert (std::make_pair (fileId, path));
3220- mDataMap .insert (std::make_pair (path, std::move (data)));
3187+ mNameMap .insert (std::make_pair (std::move (path), data));
3188+ mIdMap .insert (std::make_pair (fileId, data));
3189+ mData .push_back (std::unique_ptr<FileData>(data));
32213190
3222- if (header2 != nullptr )
3223- *header2 = std::move (path);
3224-
3225- return tokens;
3191+ return std::make_pair (true , data);
32263192 }
32273193}
32283194
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)
3195+ 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)
32303196{
3231- simplecpp::TokenList *tokens = get (sourcefile, header, header2 , dui, systemheader);
3197+ FileData *data = lookup (sourcefile, header, dui, systemheader);
32323198
3233- if (tokens = = nullptr )
3234- tokens = load (sourcefile, header, header2, dui, systemheader, filenames, outputList );
3199+ if (data ! = nullptr )
3200+ return std::make_pair ( false , data );
32353201
3236- return tokens ;
3202+ return load (sourcefile, header, dui, systemheader, filenames, outputList) ;
32373203}
32383204
32393205bool simplecpp::FileDataCache::getFileId (const std::string &path, FileID &id)
@@ -3278,17 +3244,19 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
32783244 nonExistingFilesCache.clear ();
32793245#endif
32803246
3281- FileDataCache filedata ;
3247+ FileDataCache cache ;
32823248
32833249 std::list<const Token *> filelist;
32843250
32853251 // -include files
32863252 for (std::list<std::string>::const_iterator it = dui.includes .begin (); it != dui.includes .end (); ++it) {
32873253 const std::string &filename = *it;
32883254
3289- TokenList *tokenlist = filedata.get_or_load (" " , filename, nullptr , dui, false , filenames, outputList);
3255+ const auto loadResult = cache.get (" " , filename, dui, false , filenames, outputList);
3256+ const bool loaded = loadResult.first ;
3257+ FileData *const filedata = loadResult.second ;
32903258
3291- if (tokenlist == nullptr ) {
3259+ if (filedata == nullptr ) {
32923260 if (outputList) {
32933261 simplecpp::Output err (filenames);
32943262 err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
@@ -3299,14 +3267,16 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
32993267 continue ;
33003268 }
33013269
3302- if (!tokenlist->front ()) {
3270+ if (!loaded)
3271+ continue ;
3272+
3273+ if (!filedata->tokens .front ())
33033274 continue ;
3304- }
33053275
33063276 if (dui.removeComments )
3307- tokenlist-> removeComments ();
3277+ filedata-> tokens . removeComments ();
33083278
3309- filelist.push_back (tokenlist-> front ());
3279+ filelist.push_back (filedata-> tokens . front ());
33103280 }
33113281
33123282 for (const Token *rawtok = rawtokens.cfront (); rawtok || !filelist.empty (); rawtok = rawtok ? rawtok->next : nullptr ) {
@@ -3331,18 +3301,18 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
33313301 const bool systemheader = (htok->str ()[0 ] == ' <' );
33323302 const std::string header (htok->str ().substr (1U , htok->str ().size () - 2U ));
33333303
3334- TokenList *tokenlist = filedata. get_or_load (sourcefile, header, nullptr , dui, systemheader, filenames, outputList);
3335- if (!tokenlist )
3304+ FileData * const filedata = cache. get (sourcefile, header, dui, systemheader, filenames, outputList). second ;
3305+ if (!filedata )
33363306 continue ;
33373307
33383308 if (dui.removeComments )
3339- tokenlist-> removeComments ();
3309+ filedata-> tokens . removeComments ();
33403310
3341- if (tokenlist-> front ())
3342- filelist.push_back (tokenlist-> front ());
3311+ if (filedata-> tokens . front ())
3312+ filelist.push_back (filedata-> tokens . front ());
33433313 }
33443314
3345- return filedata ;
3315+ return cache ;
33463316}
33473317
33483318static bool preprocessToken (simplecpp::TokenList &output, const simplecpp::Token **tok1, simplecpp::MacroMap ¯os, std::vector<std::string> &files, simplecpp::OutputList *outputList)
@@ -3398,7 +3368,7 @@ static std::string getTimeDefine(const struct tm *timep)
33983368 return std::string (" \" " ).append (buf).append (" \" " );
33993369}
34003370
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)
3371+ 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)
34023372{
34033373#ifdef SIMPLECPP_WINDOWS
34043374 if (dui.clearIncludeCache )
@@ -3490,9 +3460,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34903460
34913461 includetokenstack.push (rawtokens.cfront ());
34923462 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 ());
3463+ const FileData *const filedata = cache. get (" " , *it, dui, false , files, outputList). second ;
3464+ if (filedata != nullptr && filedata-> tokens . cfront () != nullptr )
3465+ includetokenstack.push (filedata-> tokens . cfront ());
34963466 }
34973467
34983468 std::map<std::string, std::list<Location> > maybeUsedMacros;
@@ -3614,9 +3584,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36143584
36153585 const bool systemheader = (inctok->str ()[0 ] == ' <' );
36163586 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 ) {
3587+ const FileData *const filedata = cache.get (rawtok->location .file (), header, dui, systemheader, files, outputList).second ;
3588+ if (filedata == nullptr ) {
36203589 if (outputList) {
36213590 simplecpp::Output out (files);
36223591 out.type = Output::MISSING_HEADER;
@@ -3632,9 +3601,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36323601 out.msg = " #include nested too deeply" ;
36333602 outputList->push_back (out);
36343603 }
3635- } else if (pragmaOnce.find (header2 ) == pragmaOnce.end ()) {
3604+ } else if (pragmaOnce.find (filedata-> filename ) == pragmaOnce.end ()) {
36363605 includetokenstack.push (gotoNextLine (rawtok));
3637- rawtok = includetokens ? includetokens-> cfront () : nullptr ;
3606+ rawtok = filedata-> tokens . cfront ();
36383607 continue ;
36393608 }
36403609 } else if (rawtok->str () == IF || rawtok->str () == IFDEF || rawtok->str () == IFNDEF || rawtok->str () == ELIF) {
@@ -3862,9 +3831,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
38623831 }
38633832}
38643833
3865- void simplecpp::cleanup (FileDataCache &filedata )
3834+ void simplecpp::cleanup (FileDataCache &cache )
38663835{
3867- ( void ) filedata ;
3836+ cache. clear () ;
38683837}
38693838
38703839simplecpp::cstd_t simplecpp::getCStd (const std::string &std)
0 commit comments