Skip to content

Commit 1fc2a8a

Browse files
committed
Update simplecpp to 1.9.0
1 parent 569f3d6 commit 1fc2a8a

12 files changed

Lines changed: 255 additions & 75 deletions

externals/simplecpp/simplecpp.cpp

Lines changed: 159 additions & 27 deletions
Large diffs are not rendered by default.

externals/simplecpp/simplecpp.h

Lines changed: 53 additions & 35 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. */
@@ -578,9 +590,15 @@ namespace simplecpp {
578590
/** Returns the C version a given standard */
579591
SIMPLECPP_LIB cstd_t getCStd(const std::string &std);
580592

593+
/** Returns the name of a C standard */
594+
SIMPLECPP_LIB const char *getCStdName(cstd_t std);
595+
581596
/** Returns the C++ version a given standard */
582597
SIMPLECPP_LIB cppstd_t getCppStd(const std::string &std);
583598

599+
/** Returns the name of a C++ standard */
600+
SIMPLECPP_LIB const char *getCppStdName(cppstd_t std);
601+
584602
/** Returns the __STDC_VERSION__ value for a given standard */
585603
SIMPLECPP_LIB std::string getCStdString(const std::string &std);
586604
SIMPLECPP_LIB std::string getCStdString(cstd_t std);

lib/cppcheck.cpp

Lines changed: 8 additions & 4 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
}

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/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/tokenlist.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ bool TokenList::createTokensFromBufferInternal(const char* data, size_t size, co
354354
#endif
355355

356356
simplecpp::OutputList outputList;
357-
simplecpp::TokenList tokens({data, size}, mFiles, file0, &outputList);
357+
simplecpp::DUI dui;
358+
dui.std = mSettings.standards.getStdForLanguage(mLang);
359+
simplecpp::TokenList tokens({data, size}, mFiles, file0, dui, &outputList);
358360

359361
createTokens(std::move(tokens));
360362

test/helpers.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ ScopedFile::~ScopedFile() {
113113

114114
void SimpleTokenizer2::preprocess(const char* code, std::size_t size, std::vector<std::string> &files, const std::string& file0, Tokenizer& tokenizer, ErrorLogger& errorlogger)
115115
{
116+
const Standards &standards = tokenizer.getSettings().standards;
117+
const Standards::Language language = tokenizer.isCPP() ? Standards::CPP : Standards::C;
116118
simplecpp::OutputList outputList;
117-
simplecpp::TokenList tokens1({code, size}, files, file0, &outputList);
119+
simplecpp::DUI dui;
120+
dui.std = standards.getStdForLanguage(language);
121+
simplecpp::TokenList tokens1({code, size}, files, file0, dui, &outputList);
118122

119123
Preprocessor preprocessor(tokens1, tokenizer.getSettings(), errorlogger, Path::identify(tokens1.getFiles()[0], false));
120124
(void)preprocessor.loadFiles(files); // TODO: check result

test/testcppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class TestCppcheck : public TestFixture {
490490
";\n";
491491

492492
simplecpp::OutputList outputList;
493-
const simplecpp::TokenList tokens2(code, files, "", &outputList);
493+
const simplecpp::TokenList tokens2(code, files, "", {}, &outputList);
494494
const std::string expected2 = " <rawtokens>\n"
495495
" <file index=\"0\" name=\"test.c\"/>\n"
496496
" <file index=\"1\" name=\"\"/>\n"

test/testpreprocessor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TestPreprocessor : public TestFixture {
5757
std::string expandMacros_(const char* file, int line, const char (&code)[size], ErrorLogger &errorLogger) const {
5858
simplecpp::OutputList outputList;
5959
std::vector<std::string> files;
60-
simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", &outputList);
60+
simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", {}, &outputList);
6161
Preprocessor p(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
6262
ASSERT_LOC(p.loadFiles(files), file, line);
6363
simplecpp::TokenList tokens2 = p.preprocess("", files, outputList);
@@ -75,7 +75,7 @@ class TestPreprocessor : public TestFixture {
7575
throw std::runtime_error("token list not empty");
7676

7777
simplecpp::OutputList outputList;
78-
const simplecpp::TokenList tokens1(code, files, file0, &outputList);
78+
const simplecpp::TokenList tokens1(code, files, file0, dui, &outputList);
7979

8080
// Preprocess..
8181
simplecpp::TokenList tokens2(files);
@@ -124,7 +124,7 @@ class TestPreprocessor : public TestFixture {
124124
simplecpp::OutputList outputList;
125125
std::vector<std::string> files;
126126

127-
simplecpp::TokenList tokens({code, size}, files, Path::simplifyPath(filename), &outputList);
127+
simplecpp::TokenList tokens({code, size}, files, Path::simplifyPath(filename), {}, &outputList);
128128

129129
// TODO: we should be using the actual Preprocessor implementation
130130
Preprocessor preprocessor(tokens, settings, errorlogger, Path::identify(tokens.getFiles()[0], false));
@@ -399,7 +399,8 @@ class TestPreprocessor : public TestFixture {
399399
ASSERT(settings.library.load("", library, false).errorcode == Library::ErrorCode::OK);
400400
std::vector<std::string> files;
401401
simplecpp::OutputList outputList;
402-
simplecpp::TokenList tokens(code,files,"test.c",&outputList);
402+
simplecpp::DUI dui;
403+
simplecpp::TokenList tokens(code,files,"test.c",dui,&outputList);
403404
Preprocessor preprocessor(tokens, settings, *this, Standards::Language::C);
404405
std::set<std::string> configs = { "" };
405406
std::set<std::string> configDefines = { "__cplusplus" };

0 commit comments

Comments
 (0)