8
8
#define NOMINMAX
9
9
#endif
10
10
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
+
11
19
#include " simplecpp.h"
12
20
13
21
#include < algorithm>
46
54
#include < unistd.h>
47
55
#endif
48
56
49
- #ifdef SIMPLECPP_WINDOWS
50
- #include < windows.h>
51
- #undef ERROR
52
- #else
53
- #include < sys/stat.h>
54
- #endif
55
-
56
57
#if __cplusplus >= 201103L
57
58
#define OVERRIDE override
58
59
#define EXPLICIT explicit
@@ -155,7 +156,7 @@ static unsigned long long stringToULL(const std::string &s)
155
156
return ret;
156
157
}
157
158
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
159
160
static bool startsWith_ (const std::string &s, const std::string &p)
160
161
{
161
162
return (s.size () >= p.size ()) && std::equal (p.begin (), p.end (), s.begin ());
@@ -2568,7 +2569,8 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
2568
2569
}
2569
2570
2570
2571
2571
- static std::string currentDirectoryOSCalc () {
2572
+ static std::string currentDirectoryOSCalc ()
2573
+ {
2572
2574
const std::size_t size = 4096 ;
2573
2575
char currentPath[size];
2574
2576
@@ -2582,12 +2584,14 @@ static std::string currentDirectoryOSCalc() {
2582
2584
return " " ;
2583
2585
}
2584
2586
2585
- static const std::string& currentDirectory () {
2587
+ static const std::string& currentDirectory ()
2588
+ {
2586
2589
static const std::string curdir = simplecpp::simplifyPath (currentDirectoryOSCalc ());
2587
2590
return curdir;
2588
2591
}
2589
2592
2590
- static std::string toAbsolutePath (const std::string& path) {
2593
+ static std::string toAbsolutePath (const std::string& path)
2594
+ {
2591
2595
if (path.empty ()) {
2592
2596
return path;// preserve error file path that is indicated by an empty string
2593
2597
}
@@ -2598,22 +2602,25 @@ static std::string toAbsolutePath(const std::string& path) {
2598
2602
return simplecpp::simplifyPath (path);
2599
2603
}
2600
2604
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
+ {
2602
2607
const std::size_t lastSlash = path.find_last_of (" \\ /" );
2603
2608
if (lastSlash == std::string::npos) {
2604
2609
return " " ;
2605
2610
}
2606
2611
return path.substr (0 , lastSlash + (withTrailingSlash ? 1U : 0U ));
2607
2612
}
2608
2613
2609
- static std::string omitPathTrailingSlash (const std::string& path) {
2614
+ static std::string omitPathTrailingSlash (const std::string& path)
2615
+ {
2610
2616
if (endsWith (path, " /" )) {
2611
2617
return path.substr (0 , path.size () - 1U );
2612
2618
}
2613
2619
return path;
2614
2620
}
2615
2621
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
+ {
2617
2624
const std::string normalizedAbsolutePath = omitPathTrailingSlash (absoluteSimplifiedPath);
2618
2625
std::string currentPrefix = omitPathTrailingSlash (prefixSimplifiedAbsoluteDir);
2619
2626
std::string leadingParenting;
@@ -3054,13 +3061,13 @@ static std::string getRelativeFileName(const std::string &baseFile, const std::s
3054
3061
{
3055
3062
const std::string baseFileSimplified = simplecpp::simplifyPath (baseFile);
3056
3063
const std::string baseFileAbsolute = isAbsolutePath (baseFileSimplified) ?
3057
- baseFileSimplified :
3058
- simplecpp::simplifyPath (currentDirectory () + " /" + baseFileSimplified);
3064
+ baseFileSimplified :
3065
+ simplecpp::simplifyPath (currentDirectory () + " /" + baseFileSimplified);
3059
3066
3060
3067
const std::string headerSimplified = simplecpp::simplifyPath (header);
3061
3068
const std::string path = isAbsolutePath (headerSimplified) ?
3062
- headerSimplified :
3063
- simplecpp::simplifyPath (dirPath (baseFileAbsolute) + headerSimplified);
3069
+ headerSimplified :
3070
+ simplecpp::simplifyPath (dirPath (baseFileAbsolute) + headerSimplified);
3064
3071
3065
3072
return returnAbsolutePath ? toAbsolutePath (path) : extractRelativePathFromAbsolute (path);
3066
3073
}
@@ -3116,45 +3123,25 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
3116
3123
return openHeaderIncludePath (f, dui, header);
3117
3124
}
3118
3125
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)
3120
3127
{
3121
- if (mDataMap .empty ())
3128
+ if (mData .empty ())
3122
3129
return nullptr ;
3123
3130
3124
3131
if (isAbsolutePath (header)) {
3125
3132
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);
3137
3134
3138
- return data-> second . get ();
3139
- }
3135
+ if (name_it != mNameMap . end ())
3136
+ return name_it-> second ;
3140
3137
}
3141
3138
3142
3139
if (!systemheader) {
3143
3140
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);
3155
3142
3156
- return data-> second . get ();
3157
- }
3143
+ if (name_it != mNameMap . end ())
3144
+ return name_it-> second ;
3158
3145
3159
3146
// If the file exists but hasn't been loaded yet then we need to stop searching here or we could get a false match
3160
3147
std::ifstream f;
@@ -3165,75 +3152,55 @@ simplecpp::TokenList *simplecpp::FileDataCache::get(const std::string &sourcefil
3165
3152
3166
3153
for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
3167
3154
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);
3179
3156
3180
- return data-> second . get ();
3181
- }
3157
+ if (name_it != mNameMap . end ())
3158
+ return name_it-> second ;
3182
3159
}
3183
3160
3184
3161
return nullptr ;
3185
3162
}
3186
3163
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)
3188
3165
{
3189
3166
std::ifstream f;
3190
3167
std::string path = openHeader (f, dui, sourcefile, header, systemheader);
3191
3168
3192
3169
if (path.empty ())
3193
- return nullptr ;
3170
+ return std::make_pair ( false , nullptr ) ;
3194
3171
3195
3172
FileID fileId;
3196
3173
3197
3174
if (!getFileId (path, fileId))
3198
- return nullptr ;
3175
+ return std::make_pair ( false , nullptr ) ;
3199
3176
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 ) );
3203
3180
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)};
3215
3184
3216
3185
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 ();
3221
3187
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));
3224
3191
3225
- return tokens ;
3192
+ return std::make_pair ( true , data) ;
3226
3193
}
3227
3194
}
3228
3195
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)
3230
3197
{
3231
- simplecpp::TokenList *tokens = get (sourcefile, header, header2 , dui, systemheader);
3198
+ FileData *data = lookup (sourcefile, header, dui, systemheader);
3232
3199
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 );
3235
3202
3236
- return tokens ;
3203
+ return load (sourcefile, header, dui, systemheader, filenames, outputList) ;
3237
3204
}
3238
3205
3239
3206
bool simplecpp::FileDataCache::getFileId (const std::string &path, FileID &id)
@@ -3278,17 +3245,19 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
3278
3245
nonExistingFilesCache.clear ();
3279
3246
#endif
3280
3247
3281
- FileDataCache filedata ;
3248
+ FileDataCache cache ;
3282
3249
3283
3250
std::list<const Token *> filelist;
3284
3251
3285
3252
// -include files
3286
3253
for (std::list<std::string>::const_iterator it = dui.includes .begin (); it != dui.includes .end (); ++it) {
3287
3254
const std::string &filename = *it;
3288
3255
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 ;
3290
3259
3291
- if (tokenlist == nullptr ) {
3260
+ if (filedata == nullptr ) {
3292
3261
if (outputList) {
3293
3262
simplecpp::Output err (filenames);
3294
3263
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
@@ -3299,14 +3268,16 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
3299
3268
continue ;
3300
3269
}
3301
3270
3302
- if (!tokenlist->front ()) {
3271
+ if (!loaded)
3272
+ continue ;
3273
+
3274
+ if (!filedata->tokens .front ())
3303
3275
continue ;
3304
- }
3305
3276
3306
3277
if (dui.removeComments )
3307
- tokenlist-> removeComments ();
3278
+ filedata-> tokens . removeComments ();
3308
3279
3309
- filelist.push_back (tokenlist-> front ());
3280
+ filelist.push_back (filedata-> tokens . front ());
3310
3281
}
3311
3282
3312
3283
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,
3331
3302
const bool systemheader = (htok->str ()[0 ] == ' <' );
3332
3303
const std::string header (htok->str ().substr (1U , htok->str ().size () - 2U ));
3333
3304
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 )
3336
3307
continue ;
3337
3308
3338
3309
if (dui.removeComments )
3339
- tokenlist-> removeComments ();
3310
+ filedata-> tokens . removeComments ();
3340
3311
3341
- if (tokenlist-> front ())
3342
- filelist.push_back (tokenlist-> front ());
3312
+ if (filedata-> tokens . front ())
3313
+ filelist.push_back (filedata-> tokens . front ());
3343
3314
}
3344
3315
3345
- return filedata ;
3316
+ return cache ;
3346
3317
}
3347
3318
3348
3319
static 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)
3398
3369
return std::string (" \" " ).append (buf).append (" \" " );
3399
3370
}
3400
3371
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)
3402
3373
{
3403
3374
#ifdef SIMPLECPP_WINDOWS
3404
3375
if (dui.clearIncludeCache )
@@ -3490,9 +3461,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
3490
3461
3491
3462
includetokenstack.push (rawtokens.cfront ());
3492
3463
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 ());
3496
3467
}
3497
3468
3498
3469
std::map<std::string, std::list<Location> > maybeUsedMacros;
@@ -3614,9 +3585,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
3614
3585
3615
3586
const bool systemheader = (inctok->str ()[0 ] == ' <' );
3616
3587
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 ) {
3620
3590
if (outputList) {
3621
3591
simplecpp::Output out (files);
3622
3592
out.type = Output::MISSING_HEADER;
@@ -3632,9 +3602,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
3632
3602
out.msg = " #include nested too deeply" ;
3633
3603
outputList->push_back (out);
3634
3604
}
3635
- } else if (pragmaOnce.find (header2 ) == pragmaOnce.end ()) {
3605
+ } else if (pragmaOnce.find (filedata-> filename ) == pragmaOnce.end ()) {
3636
3606
includetokenstack.push (gotoNextLine (rawtok));
3637
- rawtok = includetokens ? includetokens-> cfront () : nullptr ;
3607
+ rawtok = filedata-> tokens . cfront ();
3638
3608
continue ;
3639
3609
}
3640
3610
} 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
3862
3832
}
3863
3833
}
3864
3834
3865
- void simplecpp::cleanup (FileDataCache &filedata )
3835
+ void simplecpp::cleanup (FileDataCache &cache )
3866
3836
{
3867
- ( void ) filedata ;
3837
+ cache. clear () ;
3868
3838
}
3869
3839
3870
3840
simplecpp::cstd_t simplecpp::getCStd (const std::string &std)
0 commit comments