Skip to content

Commit 7185b10

Browse files
committed
chore: Override the position of the expanded macro
1 parent 03fbb72 commit 7185b10

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

src/frontend/cxx/frontend.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ void dumpTokens(const CLI& cli, TranslationUnit& unit, std::ostream& output) {
8787
kind = Lexer::classifyKeyword(tk.spell());
8888
}
8989

90-
output << std::format("{} '{}'{}\n", Token::name(kind), tk.spell(), flags);
90+
output << std::format("{} '{}'{}", Token::name(kind), tk.spell(), flags);
91+
92+
auto pos = unit.tokenStartPosition(loc);
93+
94+
output << std::format(" at {}:{}:{}\n", pos.fileName, pos.line, pos.column);
9195

9296
if (tk.is(TokenKind::T_EOF_SYMBOL)) break;
9397
}

src/parser/cxx/preprocessor.cc

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ struct Tok final : Managed {
378378
return tk;
379379
}
380380

381+
[[nodiscard]] static auto Copy(Arena *pool, const Tok *tok) {
382+
auto tk = new (pool) Tok();
383+
*tk = *tok;
384+
return tk;
385+
}
386+
381387
[[nodiscard]] static auto Gen(Arena *pool, TokenKind kind,
382388
const std::string_view &text,
383389
const Hideset *hideset = nullptr) -> Tok * {
@@ -826,6 +832,11 @@ struct Preprocessor::Private {
826832
return Tok::Gen(&pool_, kind, text, hideset);
827833
}
828834

835+
[[nodiscard]] auto copy(const Tok *tok) -> Tok * {
836+
auto copy = Tok::Copy(&pool_, tok);
837+
return copy;
838+
}
839+
829840
void pushState(std::tuple<bool, bool> state) {
830841
auto [skipping, evaluating] = state;
831842
skipping_.push_back(skipping);
@@ -2113,6 +2124,8 @@ auto Preprocessor::Private::substitute(TokList *pointOfSubstitution,
21132124
TokList *os = nullptr;
21142125
auto **ip = (&os);
21152126

2127+
std::unordered_set<const Tok *> macroTokens;
2128+
21162129
auto appendTokens = [&](TokList *rs) {
21172130
if (!*ip) {
21182131
*ip = (rs);
@@ -2122,9 +2135,30 @@ auto Preprocessor::Private::substitute(TokList *pointOfSubstitution,
21222135
while (*ip && (*ip)->next) ip = (&(*ip)->next);
21232136
};
21242137

2125-
auto appendToken = [&](const Tok *tk) { appendTokens(cons(tk)); };
2138+
auto appendToken = [&](const Tok *tk) {
2139+
if (macroTokens.contains(tk)) {
2140+
// todo: make a copy of tk and override its location to be the same as
2141+
// the point of substitution.
2142+
auto copyTk = copy(tk);
2143+
copyTk->sourceFile = pointOfSubstitution->tok->sourceFile;
2144+
copyTk->offset = pointOfSubstitution->tok->offset;
2145+
copyTk->length = pointOfSubstitution->tok->length;
2146+
appendTokens(cons(copyTk));
2147+
} else {
2148+
appendTokens(cons(tk));
2149+
}
2150+
};
2151+
2152+
TokList *macroBody = getMacroBody(*macro);
2153+
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());
21262159

2127-
TokList *ts = getMacroBody(*macro);
2160+
// set the token stream to the macro body
2161+
TokList *ts = macroBody;
21282162

21292163
while (ts && !lookat(ts, TokenKind::T_EOF_SYMBOL)) {
21302164
if (lookat(ts, TokenKind::T_HASH, TokenKind::T_IDENTIFIER)) {

tests/unit_tests/preprocessor/define.001.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ int main() {
77
return result;
88
}
99

10-
// CHECK: int result = 0;
10+
// CHECK: int result = 0 ;

tests/unit_tests/preprocessor/define.002.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ int main() {
99
return result;
1010
}
1111

12-
// CHECK: int result = 1;
12+
// CHECK: int result = 1 ;

tests/unit_tests/preprocessor/define.003.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
int main() { FOR_EACH(DECLARE_VAR) }
1111

12-
// CHECK: int aa;int bb;int cc;
12+
// CHECK: int aa ; int bb ; int cc ;

tests/unit_tests/preprocessor/warning.001.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
const char* platform = PLATFORM;
1111
#endif
1212

13-
// CHECK: const char* platform = "linux";
13+
// CHECK: const char* platform = "linux" ;

0 commit comments

Comments
 (0)