diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index c1f4802e5..7539538b5 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -2051,14 +2051,13 @@ bool TokenList::isKeyword(const std::string &str) const if (cpp_types.find(str) != cpp_types.end()) return false; - // TODO: properly apply configured standard - if (!mSettings || mSettings->standards.cpp >= Standards::CPP20) { - static const auto& cpp20_keywords = Keywords::getAll(Standards::cppstd_t::CPP20); - return cpp20_keywords.find(str) != cpp20_keywords.end(); + if (mSettings) { + const auto &cpp_keywords = Keywords::getAll(mSettings->standards.cpp); + return cpp_keywords.find(str) != cpp_keywords.end(); } - static const auto& cpp_keywords = Keywords::getAll(Standards::cppstd_t::CPP11); - return cpp_keywords.find(str) != cpp_keywords.end(); + static const auto& latest_cpp_keywords = Keywords::getAll(Standards::cppstd_t::CPPLatest); + return latest_cpp_keywords.find(str) != latest_cpp_keywords.end(); } // TODO: integrate into Keywords? @@ -2067,7 +2066,11 @@ bool TokenList::isKeyword(const std::string &str) const if (c_types.find(str) != c_types.end()) return false; - // TODO: use configured standard - static const auto& c_keywords = Keywords::getAll(Standards::cstd_t::C99); - return c_keywords.find(str) != c_keywords.end(); + if (mSettings) { + const auto &c_keywords = Keywords::getAll(mSettings->standards.c); + return c_keywords.find(str) != c_keywords.end(); + } + + static const auto& latest_c_keywords = Keywords::getAll(Standards::cstd_t::CLatest); + return latest_c_keywords.find(str) != latest_c_keywords.end(); } diff --git a/test/testtokenlist.cpp b/test/testtokenlist.cpp index 219803683..c1dbcc4dc 100644 --- a/test/testtokenlist.cpp +++ b/test/testtokenlist.cpp @@ -109,6 +109,56 @@ private: ASSERT_EQUALS(true, tokenlist.front()->tokAt(4)->isLiteral()); ASSERT_EQUALS(false, tokenlist.front()->tokAt(4)->isControlFlowKeyword()); } + + { + const char code2[] = "_Generic"; // C11 keyword + TokenList tokenlist(nullptr); // no settings use latest standard + std::istringstream istr(code2); + tokenlist.createTokens(istr, "a.cpp"); + ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); + } + + { + const char code2[] = "_Generic"; // C11 keyword + TokenList tokenlist(nullptr); // no settings use latest standard + std::istringstream istr(code2); + tokenlist.createTokens(istr, "a.c"); + ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); + } + + { + const char code2[] = "_Generic"; // C11 keyword + const Settings s = settingsBuilder().c(Standards::C89).build(); + TokenList tokenlist(&s); + std::istringstream istr(code2); + tokenlist.createTokens(istr, "a.c"); + ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); + } + + { + const char code2[] = "co_return"; // C++20 keyword + TokenList tokenlist(nullptr); // no settings use latest standard + std::istringstream istr(code2); + tokenlist.createTokens(istr, "a.cpp"); + ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); + } + + { + const char code2[] = "co_return"; // C++20 keyword + TokenList tokenlist(nullptr); // no settings use latest standard + std::istringstream istr(code2); + tokenlist.createTokens(istr, "a.c"); + ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); + } + + { + const char code2[] = "noexcept"; // C++11 keyword + const Settings s = settingsBuilder().cpp(Standards::CPP03).build(); + TokenList tokenlist(&s); + std::istringstream istr(code2); + tokenlist.createTokens(istr, "a.cpp"); + ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); + } } };