Skip to content

Commit ff4d7ce

Browse files
authored
Fix #14987: Update simplecpp to 1.9.1 (#8809)
1 parent ac8c1aa commit ff4d7ce

62 files changed

Lines changed: 12805 additions & 12479 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

externals/simplecpp/simplecpp.cpp

Lines changed: 201 additions & 61 deletions
Large diffs are not rendered by default.

externals/simplecpp/simplecpp.h

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
# endif
6161
#endif
6262

63+
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 9
64+
// Hack to workaround GCC bug.
65+
// Details: https://trac.cppcheck.net/ticket/14850
66+
// seen on g++ before 10.x
67+
#define SIMPLECPP_NOEXCEPT
68+
#else
69+
#define SIMPLECPP_NOEXCEPT noexcept
70+
#endif
71+
6372
namespace simplecpp {
6473
/** C code standard */
6574
enum cstd_t : std::int8_t { CUnknown=-1, C89, C99, C11, C17, C23, C2Y };
@@ -238,7 +247,10 @@ namespace simplecpp {
238247
MISSING_HEADER,
239248
INCLUDE_NESTED_TOO_DEEPLY,
240249
SYNTAX_ERROR,
250+
DIRECTIVE_AS_MACRO_PARAMETER,
241251
PORTABILITY_BACKSLASH,
252+
PORTABILITY_LINE_DIRECTIVE,
253+
PORTABILITY_NO_EOF_NEWLINE,
242254
UNHANDLED_CHAR_ERROR,
243255
EXPLICIT_INCLUDE_NOT_FOUND,
244256
FILE_NOT_FOUND,
@@ -251,52 +263,67 @@ namespace simplecpp {
251263

252264
using OutputList = std::list<Output>;
253265

266+
/**
267+
* Command line preprocessor settings.
268+
* On the command line these are configured by -D, -U, -I, --include, -std
269+
*/
270+
struct SIMPLECPP_LIB DUI {
271+
DUI() = default;
272+
std::list<std::string> defines;
273+
std::set<std::string> undefined;
274+
std::list<std::string> includePaths;
275+
std::list<std::string> includes;
276+
std::string std;
277+
bool clearIncludeCache{};
278+
bool removeComments{}; /** remove comment tokens from included files */
279+
};
280+
254281
/** List of tokens. */
255282
class SIMPLECPP_LIB TokenList {
256283
public:
257284
class Stream;
258285

259286
explicit TokenList(std::vector<std::string> &filenames);
260287
/** generates a token list from the given std::istream parameter */
261-
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
288+
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr);
262289
/** generates a token list from the given buffer */
263290
template<size_t size>
264-
TokenList(const char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
265-
: TokenList(reinterpret_cast<const unsigned char*>(data), size-1, filenames, filename, outputList, 0)
291+
TokenList(const char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr)
292+
: TokenList(reinterpret_cast<const unsigned char*>(data), size-1, filenames, filename, dui, outputList, 0)
266293
{}
267294
/** generates a token list from the given buffer */
268295
template<size_t size>
269-
TokenList(const unsigned char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
270-
: TokenList(data, size-1, filenames, filename, outputList, 0)
296+
TokenList(const unsigned char (&data)[size], std::vector<std::string> &filenames, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr)
297+
: TokenList(data, size-1, filenames, filename, dui, outputList, 0)
271298
{}
272299
#if SIMPLECPP_TOKENLIST_ALLOW_PTR
273300
/** generates a token list from the given buffer */
274-
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
275-
: TokenList(data, size, filenames, filename, outputList, 0)
301+
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr)
302+
: TokenList(data, size, filenames, filename, dui, outputList, 0)
276303
{}
277304
/** generates a token list from the given buffer */
278-
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
279-
: TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, outputList, 0)
305+
TokenList(const char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr)
306+
: TokenList(reinterpret_cast<const unsigned char*>(data), size, filenames, filename, dui, outputList, 0)
280307
{}
281308
#endif // SIMPLECPP_TOKENLIST_ALLOW_PTR
282309
/** generates a token list from the given buffer */
283-
TokenList(View data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
284-
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
310+
TokenList(View data, std::vector<std::string> &filenames, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr)
311+
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, dui, outputList, 0)
285312
{}
286313
#ifdef __cpp_lib_span
287314
/** generates a token list from the given buffer */
288-
TokenList(std::span<const char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
289-
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, outputList, 0)
315+
TokenList(std::span<const char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr)
316+
: TokenList(reinterpret_cast<const unsigned char*>(data.data()), data.size(), filenames, filename, dui, outputList, 0)
290317
{}
291318

292319
/** generates a token list from the given buffer */
293-
TokenList(std::span<const unsigned char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr)
294-
: TokenList(data.data(), data.size(), filenames, filename, outputList, 0)
320+
TokenList(std::span<const unsigned char> data, std::vector<std::string> &filenames, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr)
321+
: TokenList(data.data(), data.size(), filenames, filename, dui, outputList, 0)
295322
{}
296323
#endif // __cpp_lib_span
297324

298325
/** generates a token list from the given filename parameter */
299-
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
326+
TokenList(const std::string &filename, std::vector<std::string> &filenames, const DUI &dui = {}, OutputList *outputList = nullptr);
300327
TokenList(const TokenList &other);
301328
TokenList(TokenList &&other);
302329
~TokenList();
@@ -312,7 +339,7 @@ namespace simplecpp {
312339
void dump(bool linenrs = false) const;
313340
std::string stringify(bool linenrs = false) const;
314341

315-
void readfile(Stream &stream, const std::string &filename=std::string(), OutputList *outputList = nullptr);
342+
void readfile(Stream &stream, const std::string &filename=std::string(), const DUI &dui = {}, OutputList *outputList = nullptr);
316343
/**
317344
* @throws std::overflow_error thrown on overflow or division by zero
318345
* @throws std::runtime_error thrown on invalid expressions
@@ -376,7 +403,7 @@ namespace simplecpp {
376403
const std::string& file(const Location& loc) const;
377404

378405
private:
379-
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, OutputList *outputList, int /*unused*/);
406+
TokenList(const unsigned char* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename, const DUI &dui, OutputList *outputList, int /*unused*/);
380407

381408
void combineOperators();
382409

@@ -425,21 +452,6 @@ namespace simplecpp {
425452
long long result; // condition result
426453
};
427454

428-
/**
429-
* Command line preprocessor settings.
430-
* On the command line these are configured by -D, -U, -I, --include, -std
431-
*/
432-
struct SIMPLECPP_LIB DUI {
433-
DUI() = default;
434-
std::list<std::string> defines;
435-
std::set<std::string> undefined;
436-
std::list<std::string> includePaths;
437-
std::list<std::string> includes;
438-
std::string std;
439-
bool clearIncludeCache{};
440-
bool removeComments{}; /** remove comment tokens from included files */
441-
};
442-
443455
struct SIMPLECPP_LIB FileData {
444456
/** The canonical filename associated with this data */
445457
std::string filename;
@@ -453,10 +465,10 @@ namespace simplecpp {
453465
~FileDataCache();
454466

455467
FileDataCache(const FileDataCache &) = delete;
456-
FileDataCache(FileDataCache &&) noexcept;
468+
FileDataCache(FileDataCache &&) SIMPLECPP_NOEXCEPT;
457469

458470
FileDataCache &operator=(const FileDataCache &) = delete;
459-
FileDataCache &operator=(FileDataCache &&) noexcept;
471+
FileDataCache &operator=(FileDataCache &&) SIMPLECPP_NOEXCEPT;
460472

461473
/** Get the cached data for a file, or load and then return it if it isn't cached.
462474
* returns the file data and true if the file was loaded, false if it was cached. */
@@ -499,7 +511,7 @@ namespace simplecpp {
499511
return mData.cend();
500512
}
501513

502-
using load_callback_type = std::function<void (FileData &)>;
514+
using load_callback_type = std::function<void (FileData &, bool)>;
503515

504516
void set_load_callback(load_callback_type cb) {
505517
mLoadCallback = std::move(cb);
@@ -512,6 +524,7 @@ namespace simplecpp {
512524
using name_map_type = std::unordered_map<std::string, FileData *>;
513525

514526
std::pair<FileData *, bool> tryload(name_map_type::iterator &name_it, const DUI &dui, std::vector<std::string> &filenames, OutputList *outputList);
527+
std::pair<FileData *, bool> get_private(const std::string &sourcefile, const std::string &header, const DUI &dui, bool systemheader, std::vector<std::string> &filenames, OutputList *outputList);
515528

516529
container_type mData;
517530
name_map_type mNameMap;
@@ -578,9 +591,15 @@ namespace simplecpp {
578591
/** Returns the C version a given standard */
579592
SIMPLECPP_LIB cstd_t getCStd(const std::string &std);
580593

594+
/** Returns the name of a C standard */
595+
SIMPLECPP_LIB const char *getCStdName(cstd_t std);
596+
581597
/** Returns the C++ version a given standard */
582598
SIMPLECPP_LIB cppstd_t getCppStd(const std::string &std);
583599

600+
/** Returns the name of a C++ standard */
601+
SIMPLECPP_LIB const char *getCppStdName(cppstd_t std);
602+
584603
/** Returns the __STDC_VERSION__ value for a given standard */
585604
SIMPLECPP_LIB std::string getCStdString(const std::string &std);
586605
SIMPLECPP_LIB std::string getCStdString(cstd_t std);

lib/cppcheck.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -894,16 +894,20 @@ std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const std:
894894

895895
unsigned int CppCheck::checkBuffer(const FileWithDetails &file, const std::string &cfgname, const char* data, std::size_t size)
896896
{
897-
const auto f = [&file, data, size](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
898-
return simplecpp::TokenList{{data, size}, files, file.spath(), outputList};
897+
const auto f = [&file, data, size, this](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
898+
simplecpp::DUI dui;
899+
dui.std = mSettings.standards.getStdForLanguage(file.lang());
900+
return simplecpp::TokenList{{data, size}, files, file.spath(), dui, outputList};
899901
};
900902
return checkInternal(file, cfgname, f);
901903
}
902904

903905
unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string &cfgname)
904906
{
905-
const auto f = [&file](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
906-
return simplecpp::TokenList{file.spath(), files, outputList};
907+
const auto f = [&file, this](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
908+
simplecpp::DUI dui;
909+
dui.std = mSettings.standards.getStdForLanguage(file.lang());
910+
return simplecpp::TokenList{file.spath(), files, dui, outputList};
907911
};
908912
return checkInternal(file, cfgname, f);
909913
}
@@ -1058,16 +1062,18 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
10581062
std::inserter(configDefines, configDefines.end()),
10591063
getDefineName);
10601064

1061-
preprocessor.setLoadCallback([&](simplecpp::FileData &data) {
1062-
// Do preprocessing on included file
1063-
mLogger->addRemarkComments(preprocessor.getRemarkComments(data.tokens));
1064-
preprocessor.inlineSuppressions(data.tokens, mSuppressions.nomsg);
1065-
Preprocessor::removeComments(data.tokens);
1066-
Preprocessor::createDirectives(data.tokens, directives);
1067-
Preprocessor::simplifyPragmaAsm(data.tokens);
1068-
// Discover new configurations from included file
1069-
if (configurations.size() < maxConfigs)
1070-
preprocessor.getConfigs(data.filename, data.tokens, configDefines, configurations);
1065+
preprocessor.setLoadCallback([&](simplecpp::FileData &data, bool loaded) {
1066+
if (loaded) {
1067+
// Do preprocessing on included file
1068+
mLogger->addRemarkComments(preprocessor.getRemarkComments(data.tokens));
1069+
preprocessor.inlineSuppressions(data.tokens, mSuppressions.nomsg);
1070+
Preprocessor::removeComments(data.tokens);
1071+
Preprocessor::createDirectives(data.tokens, directives);
1072+
Preprocessor::simplifyPragmaAsm(data.tokens);
1073+
// Discover new configurations from included file
1074+
if (configurations.size() < maxConfigs)
1075+
preprocessor.getConfigs(data.filename, data.tokens, configDefines, configurations);
1076+
}
10711077
});
10721078

10731079
preprocessor.setPlatformInfo();

lib/importproject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ namespace {
649649
}
650650

651651
static bool evalCondition(const std::string& condition, const ProjectConfiguration &p) {
652-
std::string c = '(' + condition + ")";
652+
std::string c = '(' + condition + ")\n";
653653
replaceAll(c, "$(Configuration)", p.configuration);
654654
replaceAll(c, "$(Platform)", p.platformStr);
655655

lib/library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static std::vector<std::string> getnames(const char *names)
177177

178178
static void gettokenlistfromvalid(const std::string& valid, TokenList& tokenList)
179179
{
180-
const std::string str(valid + ',');
180+
const std::string str(valid + ",\n");
181181
tokenList.createTokensFromBuffer(str.data(), str.size()); // TODO: check result?
182182
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
183183
if (Token::Match(tok,"- %num%")) {

lib/preprocessor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,8 @@ const simplecpp::Output* Preprocessor::reportOutput(const simplecpp::OutputList
930930
break;
931931
case simplecpp::Output::WARNING:
932932
case simplecpp::Output::PORTABILITY_BACKSLASH:
933+
case simplecpp::Output::PORTABILITY_LINE_DIRECTIVE:
934+
case simplecpp::Output::PORTABILITY_NO_EOF_NEWLINE:
933935
break;
934936
case simplecpp::Output::MISSING_HEADER: {
935937
// not considered an "error"
@@ -939,6 +941,7 @@ const simplecpp::Output* Preprocessor::reportOutput(const simplecpp::OutputList
939941
missingInclude(out.location, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
940942
}
941943
break;
944+
case simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER:
942945
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
943946
case simplecpp::Output::SYNTAX_ERROR:
944947
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
@@ -963,6 +966,7 @@ static std::string simplecppErrToId(simplecpp::Output::Type type)
963966
case simplecpp::Output::ERROR:
964967
return "preprocessorErrorDirective";
965968
case simplecpp::Output::SYNTAX_ERROR:
969+
case simplecpp::Output::DIRECTIVE_AS_MACRO_PARAMETER:
966970
return "syntaxError";
967971
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
968972
return "unhandledChar";
@@ -979,6 +983,8 @@ static std::string simplecppErrToId(simplecpp::Output::Type type)
979983
// no handled at all (warnings)
980984
case simplecpp::Output::WARNING:
981985
case simplecpp::Output::PORTABILITY_BACKSLASH:
986+
case simplecpp::Output::PORTABILITY_LINE_DIRECTIVE:
987+
case simplecpp::Output::PORTABILITY_NO_EOF_NEWLINE:
982988
throw std::runtime_error("unexpected simplecpp::Output type " + std::to_string(type));
983989
}
984990

lib/programmemory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ static std::shared_ptr<Token> createTokenFromExpression(const std::string& retur
19371937
{
19381938
std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(settings, cpp ? Standards::Language::CPP : Standards::Language::C);
19391939
{
1940-
const std::string code = "return " + returnValue + ";";
1940+
const std::string code = "return " + returnValue + ";\n";
19411941
if (!tokenList->createTokensFromBuffer(code.data(), code.size()))
19421942
return nullptr;
19431943
}

lib/standards.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,15 @@ bool Standards::setStd(const std::string& str)
157157
{
158158
return setC(str) || setCPP(str);
159159
}
160+
161+
std::string Standards::getStdForLanguage(Standards::Language language) const
162+
{
163+
switch (language) {
164+
case C:
165+
return getC();
166+
case CPP:
167+
return getCPP();
168+
default:
169+
return "";
170+
}
171+
}

lib/standards.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct CPPCHECKLIB Standards {
5858
static std::string getCPP(cppstd_t std);
5959
static cppstd_t getCPP(const std::string &std);
6060
bool setStd(const std::string& str);
61+
std::string getStdForLanguage(Language language) const;
6162
};
6263

6364
/// @}

lib/symboldatabase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8093,7 +8093,7 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
80938093
if (!typestr.empty()) {
80948094
ValueType valuetype;
80958095
TokenList tokenList(mSettings, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C);
8096-
const std::string str(typestr+";");
8096+
const std::string str(typestr+";\n");
80978097
tokenList.createTokensFromBuffer(str.data(), str.size()); // TODO: check result?
80988098
tokenList.simplifyStdType();
80998099
if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) {
@@ -8186,7 +8186,7 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
81868186
continue;
81878187
}
81888188
TokenList tokenList(mSettings, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C);
8189-
const std::string str(typestr+";");
8189+
const std::string str(typestr+";\n");
81908190
if (tokenList.createTokensFromBuffer(str.data(), str.size())) {
81918191
ValueType vt;
81928192
tokenList.simplifyPlatformTypes();

0 commit comments

Comments
 (0)