Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inverted the relationship between expanded macros and tokens #305

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1605,14 +1605,19 @@ namespace simplecpp {
return invalidHashHash(loc, macroName, "Combining '\\"+ tokenA->str()+ "' and '"+ strAB.substr(tokenA->str().size()) + "' yields universal character '\\" + strAB + "'. This is undefined behavior according to C standard chapter 5.1.1.2, paragraph 4.");
}
};

bool expandsTok(const Token *tok) const
{
return expandedToks.find(tok) != expandedToks.end();
}
private:
/** Create new token where Token::macro is set for replaced tokens */
Token *newMacroToken(const TokenString &str, const Location &loc, bool replaced, const Token *expandedFromToken=nullptr) const {
Token *tok = new Token(str,loc);
Token * const tok = new Token(str,loc);
if (replaced)
tok->macro = nameTokDef->str();
if (expandedFromToken)
tok->setExpandedFrom(expandedFromToken, this);
const_cast<Macro*>(this)->addExpandedTok(tok);
return tok;
}

Expand Down Expand Up @@ -2031,7 +2036,7 @@ namespace simplecpp {
return true;
for (const Token *partok = parametertokens[argnr]->next; partok != parametertokens[argnr + 1U];) {
const MacroMap::const_iterator it = macros.find(partok->str());
if (it != macros.end() && !partok->isExpandedFrom(&it->second) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end()))
if (it != macros.end() && !it->second.expandsTok(partok) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end()))
partok = it->second.expand(output, loc, partok, macros, expandedmacros);
else {
output->push_back(newMacroToken(partok->str(), loc, isReplaced(expandedmacros), partok));
Expand Down Expand Up @@ -2184,6 +2189,11 @@ namespace simplecpp {
return (it != expandedmacros.end());
}

void addExpandedTok(const Token *tok)
{
expandedToks.insert(tok);
}

/** name token in definition */
const Token *nameTokDef;

Expand All @@ -2210,6 +2220,8 @@ namespace simplecpp {

/** was the value of this macro actually defined in the code? */
bool valueDefinedInCode_;

std::set<const Token*> expandedToks;
};
}

Expand Down
8 changes: 0 additions & 8 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ namespace simplecpp {
return tok;
}

void setExpandedFrom(const Token *tok, const Macro* m) {
mExpandedFrom = tok->mExpandedFrom;
mExpandedFrom.insert(m);
}
bool isExpandedFrom(const Macro* m) const {
return mExpandedFrom.find(m) != mExpandedFrom.end();
}

void printAll() const;
void printOut() const;
private:
Expand Down
9 changes: 9 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,14 @@ static void define_define_18()
ASSERT_EQUALS("\n\n\n( ( p -> var ) ) ;", preprocess(code));
}

static void define_define_19()
{
const char code[] = "#define A0(a, b) ((a) + (b))\n"
"#define A1(a, b) ((a) > (b)) ? A0((a) - (b), (b)) : A0((b) - (a), (a))\n"
" A1(a, b);";
ASSERT_EQUALS("\n\n( ( a ) > ( b ) ) ? ( ( ( a ) - ( b ) ) + ( ( b ) ) ) : ( ( ( b ) - ( a ) ) + ( ( a ) ) ) ;", preprocess(code));
}

static void define_va_args_1()
{
const char code[] = "#define A(fmt...) dostuff(fmt)\n"
Expand Down Expand Up @@ -2662,6 +2670,7 @@ int main(int argc, char **argv)
TEST_CASE(define_define_16);
TEST_CASE(define_define_17);
TEST_CASE(define_define_18);
TEST_CASE(define_define_19);
TEST_CASE(define_va_args_1);
TEST_CASE(define_va_args_2);
TEST_CASE(define_va_args_3);
Expand Down