Preprocessor: better handling of #define A 0 => #if A==0. ticket #3331

This commit is contained in:
Daniel Marjamäki 2011-11-17 19:19:43 +01:00
parent 994f08fdf0
commit e78131f9e6
2 changed files with 15 additions and 2 deletions

View File

@ -1320,7 +1320,11 @@ void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &c
}
if (Token::Match(tokenizer.tokens(), "( ! %var% )")) {
if (cfg.find(tokenizer.tokens()->strAt(2)) == cfg.end())
std::map<std::string,std::string>::const_iterator var = cfg.find(tokenizer.tokens()->strAt(2));
if (var == cfg.end())
condition = "1";
else if (var->second == "0")
condition = "1";
else if (match)
condition = "0";
@ -1471,7 +1475,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
// Create a map for the cfg for faster access to defines
std::map<std::string, std::string> cfgmap;
{
if (!cfg.empty()) {
std::string::size_type pos = 0;
for (;;) {
std::string::size_type pos2 = cfg.find_first_of(";=", pos);

View File

@ -202,6 +202,7 @@ private:
// define and then ifdef
TEST_CASE(define_if1);
TEST_CASE(define_if2);
TEST_CASE(define_if3);
TEST_CASE(define_ifdef);
TEST_CASE(define_ifndef1);
TEST_CASE(define_ifndef2);
@ -2483,6 +2484,14 @@ private:
ASSERT_EQUALS("\n\n\nFOO\n\n", Preprocessor::getcode(filedata,"","",NULL,NULL));
}
void define_if3() {
const char filedata[] = "#define A 0\n"
"#if (A==0)\n"
"FOO\n"
"#endif";
ASSERT_EQUALS("\n\nFOO\n\n", Preprocessor::getcode(filedata,"","",NULL,NULL));
}
void define_ifdef() {
{
const char filedata[] = "#define ABC\n"