88#define NOMINMAX
99#endif
1010
11+ #ifdef SIMPLECPP_WINDOWS
12+ #define _WIN32_WINNT 0x0602
13+ #include < windows.h>
14+ #undef ERROR
15+ #else
16+ #include < sys/stat.h>
17+ #endif
18+
1119#include " simplecpp.h"
1220
1321#include < algorithm>
4654#include < unistd.h>
4755#endif
4856
49- #ifdef SIMPLECPP_WINDOWS
50- #include < windows.h>
51- #undef ERROR
52- #else
53- #include < sys/stat.h>
54- #endif
55-
5657#if __cplusplus >= 201103L
5758#define OVERRIDE override
5859#define EXPLICIT explicit
@@ -155,7 +156,7 @@ static unsigned long long stringToULL(const std::string &s)
155156 return ret;
156157}
157158
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
159+ // 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
159160static bool startsWith_ (const std::string &s, const std::string &p)
160161{
161162 return (s.size () >= p.size ()) && std::equal (p.begin (), p.end (), s.begin ());
@@ -2568,7 +2569,8 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
25682569}
25692570
25702571
2571- static std::string currentDirectoryOSCalc () {
2572+ static std::string currentDirectoryOSCalc ()
2573+ {
25722574 const std::size_t size = 4096 ;
25732575 char currentPath[size];
25742576
@@ -2582,12 +2584,14 @@ static std::string currentDirectoryOSCalc() {
25822584 return " " ;
25832585}
25842586
2585- static const std::string& currentDirectory () {
2587+ static const std::string& currentDirectory ()
2588+ {
25862589 static const std::string curdir = simplecpp::simplifyPath (currentDirectoryOSCalc ());
25872590 return curdir;
25882591}
25892592
2590- static std::string toAbsolutePath (const std::string& path) {
2593+ static std::string toAbsolutePath (const std::string& path)
2594+ {
25912595 if (path.empty ()) {
25922596 return path;// preserve error file path that is indicated by an empty string
25932597 }
@@ -2598,22 +2602,25 @@ static std::string toAbsolutePath(const std::string& path) {
25982602 return simplecpp::simplifyPath (path);
25992603}
26002604
2601- static std::string dirPath (const std::string& path, bool withTrailingSlash=true ) {
2605+ static std::string dirPath (const std::string& path, bool withTrailingSlash=true )
2606+ {
26022607 const std::size_t lastSlash = path.find_last_of (" \\ /" );
26032608 if (lastSlash == std::string::npos) {
26042609 return " " ;
26052610 }
26062611 return path.substr (0 , lastSlash + (withTrailingSlash ? 1U : 0U ));
26072612}
26082613
2609- static std::string omitPathTrailingSlash (const std::string& path) {
2614+ static std::string omitPathTrailingSlash (const std::string& path)
2615+ {
26102616 if (endsWith (path, " /" )) {
26112617 return path.substr (0 , path.size () - 1U );
26122618 }
26132619 return path;
26142620}
26152621
2616- static std::string extractRelativePathFromAbsolute (const std::string& absoluteSimplifiedPath, const std::string& prefixSimplifiedAbsoluteDir = currentDirectory()) {
2622+ static std::string extractRelativePathFromAbsolute (const std::string& absoluteSimplifiedPath, const std::string& prefixSimplifiedAbsoluteDir = currentDirectory())
2623+ {
26172624 const std::string normalizedAbsolutePath = omitPathTrailingSlash (absoluteSimplifiedPath);
26182625 std::string currentPrefix = omitPathTrailingSlash (prefixSimplifiedAbsoluteDir);
26192626 std::string leadingParenting;
@@ -3054,13 +3061,13 @@ static std::string getRelativeFileName(const std::string &baseFile, const std::s
30543061{
30553062 const std::string baseFileSimplified = simplecpp::simplifyPath (baseFile);
30563063 const std::string baseFileAbsolute = isAbsolutePath (baseFileSimplified) ?
3057- baseFileSimplified :
3058- simplecpp::simplifyPath (currentDirectory () + " /" + baseFileSimplified);
3064+ baseFileSimplified :
3065+ simplecpp::simplifyPath (currentDirectory () + " /" + baseFileSimplified);
30593066
30603067 const std::string headerSimplified = simplecpp::simplifyPath (header);
30613068 const std::string path = isAbsolutePath (headerSimplified) ?
3062- headerSimplified :
3063- simplecpp::simplifyPath (dirPath (baseFileAbsolute) + headerSimplified);
3069+ headerSimplified :
3070+ simplecpp::simplifyPath (dirPath (baseFileAbsolute) + headerSimplified);
30643071
30653072 return returnAbsolutePath ? toAbsolutePath (path) : extractRelativePathFromAbsolute (path);
30663073}
@@ -3116,45 +3123,25 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
31163123 return openHeaderIncludePath (f, dui, header);
31173124}
31183125
3119- simplecpp::TokenList *simplecpp::FileDataCache::get (const std::string &sourcefile, const std::string &header, std::string *header2 , const simplecpp::DUI &dui, bool systemheader)
3126+ simplecpp::FileData *simplecpp::FileDataCache::lookup (const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
31203127{
3121- if (mDataMap .empty ())
3128+ if (mData .empty ())
31223129 return nullptr ;
31233130
31243131 if (isAbsolutePath (header)) {
31253132 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);
3131-
3132- if (uniquePath != mAliasMap .end ())
3133- *header2 = uniquePath->second ;
3134- else
3135- *header2 = std::move (path);
3136- }
3133+ const auto name_it = mNameMap .find (path);
31373134
3138- return data-> second . get ();
3139- }
3135+ if (name_it != mNameMap . end ())
3136+ return name_it-> second ;
31403137 }
31413138
31423139 if (!systemheader) {
31433140 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- }
3141+ const auto name_it = mNameMap .find (path);
31553142
3156- return data-> second . get ();
3157- }
3143+ if (name_it != mNameMap . end ())
3144+ return name_it-> second ;
31583145
31593146 // If the file exists but hasn't been loaded yet then we need to stop searching here or we could get a false match
31603147 std::ifstream f;
@@ -3165,75 +3152,55 @@ simplecpp::TokenList *simplecpp::FileDataCache::get(const std::string &sourcefil
31653152
31663153 for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
31673154 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- }
3155+ const auto name_it = mNameMap .find (path);
31793156
3180- return data-> second . get ();
3181- }
3157+ if (name_it != mNameMap . end ())
3158+ return name_it-> second ;
31823159 }
31833160
31843161 return nullptr ;
31853162}
31863163
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)
3164+ 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)
31883165{
31893166 std::ifstream f;
31903167 std::string path = openHeader (f, dui, sourcefile, header, systemheader);
31913168
31923169 if (path.empty ())
3193- return nullptr ;
3170+ return std::make_pair ( false , nullptr ) ;
31943171
31953172 FileID fileId;
31963173
31973174 if (!getFileId (path, fileId))
3198- return nullptr ;
3175+ return std::make_pair ( false , nullptr ) ;
31993176
3200- const auto id = mIdMap .find (fileId);
3201- if (id != mIdMap .end ()) {
3202- const auto &data = mDataMap . at (id ->second );
3177+ const auto id_it = mIdMap .find (fileId);
3178+ if (id_it != mIdMap .end ()) {
3179+ mNameMap . insert ( std::make_pair ( std::move (path), id_it ->second ) );
32033180
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 ();
3181+ return std::make_pair (false , id_it->second );
3182+ } else {
3183+ FileData *data = new FileData {path, TokenList (f, filenames, path, outputList)};
32153184
32163185 if (dui.removeComments )
3217- tokens->removeComments ();
3218-
3219- mIdMap .insert (std::make_pair (fileId, path));
3220- mDataMap .insert (std::make_pair (path, std::move (data)));
3186+ data->tokens .removeComments ();
32213187
3222- if (header2 != nullptr )
3223- *header2 = std::move (path);
3188+ mNameMap .insert (std::make_pair (std::move (path), data));
3189+ mIdMap .insert (std::make_pair (fileId, data));
3190+ mData .push_back (std::unique_ptr<FileData>(data));
32243191
3225- return tokens ;
3192+ return std::make_pair ( true , data) ;
32263193 }
32273194}
32283195
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)
3196+ 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)
32303197{
3231- simplecpp::TokenList *tokens = get (sourcefile, header, header2 , dui, systemheader);
3198+ FileData *data = lookup (sourcefile, header, dui, systemheader);
32323199
3233- if (tokens = = nullptr )
3234- tokens = load (sourcefile, header, header2, dui, systemheader, filenames, outputList );
3200+ if (data ! = nullptr )
3201+ return std::make_pair ( false , data );
32353202
3236- return tokens ;
3203+ return load (sourcefile, header, dui, systemheader, filenames, outputList) ;
32373204}
32383205
32393206bool simplecpp::FileDataCache::getFileId (const std::string &path, FileID &id)
@@ -3278,17 +3245,19 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
32783245 nonExistingFilesCache.clear ();
32793246#endif
32803247
3281- FileDataCache filedata ;
3248+ FileDataCache cache ;
32823249
32833250 std::list<const Token *> filelist;
32843251
32853252 // -include files
32863253 for (std::list<std::string>::const_iterator it = dui.includes .begin (); it != dui.includes .end (); ++it) {
32873254 const std::string &filename = *it;
32883255
3289- TokenList *tokenlist = filedata.get_or_load (" " , filename, nullptr , dui, false , filenames, outputList);
3256+ const auto loadResult = cache.get (" " , filename, dui, false , filenames, outputList);
3257+ const bool loaded = loadResult.first ;
3258+ FileData *const filedata = loadResult.second ;
32903259
3291- if (tokenlist == nullptr ) {
3260+ if (filedata == nullptr ) {
32923261 if (outputList) {
32933262 simplecpp::Output err (filenames);
32943263 err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
@@ -3299,14 +3268,16 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
32993268 continue ;
33003269 }
33013270
3302- if (!tokenlist->front ()) {
3271+ if (!loaded)
3272+ continue ;
3273+
3274+ if (!filedata->tokens .front ())
33033275 continue ;
3304- }
33053276
33063277 if (dui.removeComments )
3307- tokenlist-> removeComments ();
3278+ filedata-> tokens . removeComments ();
33083279
3309- filelist.push_back (tokenlist-> front ());
3280+ filelist.push_back (filedata-> tokens . front ());
33103281 }
33113282
33123283 for (const Token *rawtok = rawtokens.cfront (); rawtok || !filelist.empty (); rawtok = rawtok ? rawtok->next : nullptr ) {
@@ -3331,18 +3302,18 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
33313302 const bool systemheader = (htok->str ()[0 ] == ' <' );
33323303 const std::string header (htok->str ().substr (1U , htok->str ().size () - 2U ));
33333304
3334- TokenList *tokenlist = filedata. get_or_load (sourcefile, header, nullptr , dui, systemheader, filenames, outputList);
3335- if (!tokenlist )
3305+ FileData * const filedata = cache. get (sourcefile, header, dui, systemheader, filenames, outputList). second ;
3306+ if (!filedata )
33363307 continue ;
33373308
33383309 if (dui.removeComments )
3339- tokenlist-> removeComments ();
3310+ filedata-> tokens . removeComments ();
33403311
3341- if (tokenlist-> front ())
3342- filelist.push_back (tokenlist-> front ());
3312+ if (filedata-> tokens . front ())
3313+ filelist.push_back (filedata-> tokens . front ());
33433314 }
33443315
3345- return filedata ;
3316+ return cache ;
33463317}
33473318
33483319static bool preprocessToken (simplecpp::TokenList &output, const simplecpp::Token **tok1, simplecpp::MacroMap ¯os, std::vector<std::string> &files, simplecpp::OutputList *outputList)
@@ -3398,7 +3369,7 @@ static std::string getTimeDefine(const struct tm *timep)
33983369 return std::string (" \" " ).append (buf).append (" \" " );
33993370}
34003371
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)
3372+ 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)
34023373{
34033374#ifdef SIMPLECPP_WINDOWS
34043375 if (dui.clearIncludeCache )
@@ -3490,9 +3461,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34903461
34913462 includetokenstack.push (rawtokens.cfront ());
34923463 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 ());
3464+ const FileData *const filedata = cache. get (" " , *it, dui, false , files, outputList). second ;
3465+ if (filedata != nullptr && filedata-> tokens . cfront () != nullptr )
3466+ includetokenstack.push (filedata-> tokens . cfront ());
34963467 }
34973468
34983469 std::map<std::string, std::list<Location> > maybeUsedMacros;
@@ -3614,9 +3585,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36143585
36153586 const bool systemheader = (inctok->str ()[0 ] == ' <' );
36163587 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 ) {
3588+ const FileData *const filedata = cache.get (rawtok->location .file (), header, dui, systemheader, files, outputList).second ;
3589+ if (filedata == nullptr ) {
36203590 if (outputList) {
36213591 simplecpp::Output out (files);
36223592 out.type = Output::MISSING_HEADER;
@@ -3632,9 +3602,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36323602 out.msg = " #include nested too deeply" ;
36333603 outputList->push_back (out);
36343604 }
3635- } else if (pragmaOnce.find (header2 ) == pragmaOnce.end ()) {
3605+ } else if (pragmaOnce.find (filedata-> filename ) == pragmaOnce.end ()) {
36363606 includetokenstack.push (gotoNextLine (rawtok));
3637- rawtok = includetokens ? includetokens-> cfront () : nullptr ;
3607+ rawtok = filedata-> tokens . cfront ();
36383608 continue ;
36393609 }
36403610 } else if (rawtok->str () == IF || rawtok->str () == IFDEF || rawtok->str () == IFNDEF || rawtok->str () == ELIF) {
@@ -3862,9 +3832,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
38623832 }
38633833}
38643834
3865- void simplecpp::cleanup (FileDataCache &filedata )
3835+ void simplecpp::cleanup (FileDataCache &cache )
38663836{
3867- ( void ) filedata ;
3837+ cache. clear () ;
38683838}
38693839
38703840simplecpp::cstd_t simplecpp::getCStd (const std::string &std)
0 commit comments