TokenList: actually consider configured standard in `isKeyword()` (#5619)

This commit is contained in:
Oliver Stöneberg 2023-11-07 20:39:44 +01:00 committed by GitHub
parent d3d70dcc4e
commit 87886576fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 9 deletions

View File

@ -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();
}

View File

@ -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());
}
}
};