Fixed problems with code "enum class { };" (which is valid C)

This commit is contained in:
PKEuS 2014-03-18 16:22:00 +01:00
parent d035470963
commit a3f5beb75d
2 changed files with 13 additions and 5 deletions

View File

@ -7355,8 +7355,8 @@ void Tokenizer::simplifyEnum()
Token *typeTokenStart = nullptr;
Token *typeTokenEnd = nullptr;
// check for C++0x enum class
bool enumClass = Token::Match(tok->next(), "class|struct");
// check for C++11 enum class
bool enumClass = isCPP() && Token::Match(tok->next(), "class|struct");
if (enumClass)
tok->deleteNext();
@ -7522,7 +7522,7 @@ void Tokenizer::simplifyEnum()
std::string pattern;
if (!className.empty())
pattern += className + " :: ";
if (enumClass)
if (enumClass && enumType)
pattern += enumType->str() + " :: ";
int level = 0;

View File

@ -6736,14 +6736,14 @@ private:
}
// Check simplifyEnum
std::string checkSimplifyEnum(const char code[]) {
std::string checkSimplifyEnum(const char code[], bool cpp = true) {
errout.str("");
// Tokenize..
Settings settings;
settings.addEnabled("style");
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.tokenize(istr, cpp?"test.cpp":"test.c");
return tokenizer.tokens()->stringifyList(0, true);
}
@ -6907,6 +6907,14 @@ private:
const char expected[] = "unsigned long long e1 ; e1 = 0 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code));
}
{
const char code[] = "enum class { A };\n"
"int i = A;";
const char expected [] = "int i ; i = 0 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code, false)); // Compile as C code: enum has name 'class'
checkSimplifyEnum(code, true); // Compile as C++ code: Don't crash
}
}
void enum16() { // ticket #1988