From b04d1815eda30f9ea11e6abf12874a5fed6cb2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 12 Apr 2019 09:10:25 +0200 Subject: [PATCH] Add -std=c++17 and allow semicolon in 'if ()' --- cli/cmdlineparser.cpp | 6 +++++- lib/standards.h | 8 ++++++-- lib/tokenize.cpp | 6 +++--- lib/tokenize.h | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 92496aeca..26409e55d 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -606,6 +606,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) mSettings->standards.cpp = Standards::CPP11; } else if (std::strcmp(argv[i], "--std=c++14") == 0) { mSettings->standards.cpp = Standards::CPP14; + } else if (std::strcmp(argv[i], "--std=c++17") == 0) { + mSettings->standards.cpp = Standards::CPP17; } // Output formatter @@ -1095,7 +1097,9 @@ void CmdLineParser::printHelp() " * c++11\n" " C++ code is C++11 compatible\n" " * c++14\n" - " C++ code is C++14 compatible (default)\n" + " C++ code is C++14 compatible\n" + " * c++17\n" + " C++ code is C++17 compatible (default)\n" " --suppress= Suppress warnings that match . The format of\n" " is:\n" " [error id]:[filename]:[line]\n" diff --git a/lib/standards.h b/lib/standards.h index cff6b1ac8..cc75c220d 100644 --- a/lib/standards.h +++ b/lib/standards.h @@ -36,10 +36,10 @@ struct Standards { enum cstd_t { C89, C99, C11, CLatest=C11 } c; /** C++ code standard */ - enum cppstd_t { CPP03, CPP11, CPP14, CPPLatest=CPP14 } cpp; + enum cppstd_t { CPP03, CPP11, CPP14, CPP17, CPPLatest=CPP17 } cpp; /** This constructor clear all the variables **/ - Standards() : c(C11), cpp(CPP14) {} + Standards() : c(C11), cpp(CPP17) {} bool setC(const std::string& str) { if (str == "c89" || str == "C89") { @@ -69,6 +69,10 @@ struct Standards { cpp = CPP14; return true; } + if (str == "c++17" || str == "C++17") { + cpp = CPP17; + return true; + } return false; } }; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 49b55c965..766bc6864 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9040,7 +9040,7 @@ void Tokenizer::findGarbageCode() const if (!Token::Match(tok->next(), "( !!)")) syntaxError(tok); if (tok->str() != "for") { - if (isGarbageExpr(tok->next(), tok->linkAt(1))) + if (isGarbageExpr(tok->next(), tok->linkAt(1), mSettings->standards.cpp>=Standards::cppstd_t::CPP17)) syntaxError(tok); } } @@ -9197,12 +9197,12 @@ void Tokenizer::findGarbageCode() const } -bool Tokenizer::isGarbageExpr(const Token *start, const Token *end) +bool Tokenizer::isGarbageExpr(const Token *start, const Token *end, bool allowSemicolon) { for (const Token *tok = start; tok != end; tok = tok->next()) { if (tok->isControlFlowKeyword()) return true; - if (tok->str() == ";") + if (!allowSemicolon && tok->str() == ";") return true; if (tok->str() == "{") tok = tok->link(); diff --git a/lib/tokenize.h b/lib/tokenize.h index 9a0e4d268..a0129689f 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -632,7 +632,7 @@ private: void findGarbageCode() const; /** Detect garbage expression */ - static bool isGarbageExpr(const Token *start, const Token *end); + static bool isGarbageExpr(const Token *start, const Token *end, bool allowSemicolon); /** * Remove __declspec()