@@ -349,6 +349,7 @@ struct Tok final : Managed {
349349 std::uint16_t bol : 1 = false ;
350350 std::uint16_t space : 1 = false ;
351351 std::uint16_t generated : 1 = false ;
352+ std::uint16_t isFromMacroBody : 1 = false ;
352353
353354 Tok (const Tok &other) = default ;
354355 auto operator =(const Tok &other) -> Tok & = default ;
@@ -409,14 +410,7 @@ struct Tok final : Managed {
409410 Tok () = default ;
410411
411412 Tok (const Tok *tok, const Hideset *hs) {
412- kind = tok->kind ;
413- text = tok->text ;
414- sourceFile = tok->sourceFile ;
415- bol = tok->bol ;
416- space = tok->space ;
417- generated = tok->generated ;
418- offset = tok->offset ;
419- length = tok->length ;
413+ *this = *tok;
420414 hideset = hs;
421415 }
422416};
@@ -1189,7 +1183,8 @@ struct Preprocessor::Private {
11891183 const std::vector<TokRange> &actuals)
11901184 -> std::optional<TokRange>;
11911185
1192- [[nodiscard]] auto copyLine (TokList *ts) -> TokList *;
1186+ [[nodiscard]] auto copyLine (TokList *ts, bool inMacroBody = false )
1187+ -> TokList *;
11931188
11941189 [[nodiscard]] auto constantExpression (TokList *ts) -> long;
11951190 [[nodiscard]] auto conditionalExpression (TokList *&ts) -> long;
@@ -1707,7 +1702,7 @@ auto Preprocessor::Private::parseDirective(SourceFile *source, TokList *start)
17071702
17081703 case PreprocessorDirectiveKind::T_DEFINE: {
17091704 if (skipping) break ;
1710- defineMacro (copyLine (ts));
1705+ defineMacro (copyLine (ts, /* inMacroBody= */ true ));
17111706 break ;
17121707 }
17131708
@@ -2124,8 +2119,6 @@ auto Preprocessor::Private::substitute(TokList *pointOfSubstitution,
21242119 TokList *os = nullptr ;
21252120 auto **ip = (&os);
21262121
2127- std::unordered_set<const Tok *> macroTokens;
2128-
21292122 auto appendTokens = [&](TokList *rs) {
21302123 if (!*ip) {
21312124 *ip = (rs);
@@ -2136,7 +2129,7 @@ auto Preprocessor::Private::substitute(TokList *pointOfSubstitution,
21362129 };
21372130
21382131 auto appendToken = [&](const Tok *tk) {
2139- if (macroTokens. contains (tk) ) {
2132+ if (tk-> isFromMacroBody ) {
21402133 // todo: make a copy of tk and override its location to be the same as
21412134 // the point of substitution.
21422135 auto copyTk = copy (tk);
@@ -2151,12 +2144,6 @@ auto Preprocessor::Private::substitute(TokList *pointOfSubstitution,
21512144
21522145 TokList *macroBody = getMacroBody (*macro);
21532146
2154- for (auto it = macroBody; it; it = it->next ) {
2155- macroTokens.insert (it->tok );
2156- }
2157-
2158- // std::cerr << std::format("macro body has {} tokens\n", macroTokens.size());
2159-
21602147 // set the token stream to the macro body
21612148 TokList *ts = macroBody;
21622149
@@ -2306,15 +2293,22 @@ auto Preprocessor::Private::checkHeaderProtection(TokList *ts) const
23062293 return prot;
23072294}
23082295
2309- auto Preprocessor::Private::copyLine (TokList *ts) -> TokList * {
2296+ auto Preprocessor::Private::copyLine (TokList *ts, bool inMacroBody)
2297+ -> TokList * {
23102298 assert (ts);
23112299 TokList *line = nullptr ;
23122300 auto it = &line;
23132301 auto lastTok = ts->tok ;
23142302 for (; ts && !lookat (ts, TokenKind::T_EOF_SYMBOL) && !bol (ts);
23152303 ts = ts->next ) {
2316- *it = cons (ts->tok );
2317- lastTok = ts->tok ;
2304+ auto tok = ts->tok ;
2305+ if (inMacroBody) {
2306+ auto tokCopy = copy (tok);
2307+ tokCopy->isFromMacroBody = true ;
2308+ tok = tokCopy;
2309+ }
2310+ *it = cons (tok);
2311+ lastTok = tok;
23182312 it = (&(*it)->next );
23192313 }
23202314 auto eol = gen (TokenKind::T_EOF_SYMBOL, std::string_view ());
@@ -2981,6 +2975,9 @@ void Preprocessor::defineMacro(const std::string &name,
29812975 const std::string &body) {
29822976 auto s = d->string (name + " " + body);
29832977 auto tokens = d->tokenize (s, /* sourceFile=*/ 0 , false );
2978+ for (auto it = tokens; it; it = it->next ) {
2979+ const_cast <Tok *>(it->tok )->isFromMacroBody = true ;
2980+ }
29842981 d->defineMacro (tokens);
29852982}
29862983
0 commit comments