Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions clang/lib/Format/MacroExpander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ MacroExpander::expand(FormatToken *ID,
if (Result.size() > 1) {
++Result[0]->MacroCtx->StartOfExpansion;
++Result[Result.size() - 2]->MacroCtx->EndOfExpansion;
} else {
// If the macro expansion is empty, mark the start and end.
Result[0]->MacroCtx->StartOfExpansion = 1;
Result[0]->MacroCtx->EndOfExpansion = 1;
}
return Result;
}
Expand Down
29 changes: 28 additions & 1 deletion clang/unittests/Format/MacroCallReconstructorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class Expansion {
}
Unexpanded[ID] = std::move(UnexpandedLine);

auto Expanded = uneof(Macros.expand(ID, Args));
auto Expanded = Macros.expand(ID, Args);
if (Expanded.size() > 1)
Expanded = uneof(Expanded);
Tokens.append(Expanded.begin(), Expanded.end());

TokenList UnexpandedTokens;
Expand Down Expand Up @@ -217,6 +219,31 @@ TEST_F(MacroCallReconstructorTest, Identifier) {
EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(line(U.consume("X"))));
}

TEST_F(MacroCallReconstructorTest, IdentifierObject) {
auto Macros = createExpander({"X"});
Expansion Exp(Lex, *Macros);
TokenList Call = Exp.expand("X");

MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
Unexp.addLine(line(Exp.getTokens()));
EXPECT_TRUE(Unexp.finished());
Matcher U(Call, Lex);
EXPECT_THAT(std::move(Unexp).takeResult(), matchesLine(line(U.consume("X"))));
}

TEST_F(MacroCallReconstructorTest, EmptyExpansion) {
auto Macros = createExpander({"A(x)=x"});
Expansion Exp(Lex, *Macros);
TokenList Call = Exp.expand("A", {""});

MacroCallReconstructor Unexp(0, Exp.getUnexpanded());
Unexp.addLine(line(Exp.getTokens()));
EXPECT_TRUE(Unexp.finished());
Matcher U(Call, Lex);
EXPECT_THAT(std::move(Unexp).takeResult(),
matchesLine(line(U.consume("A()"))));
}

TEST_F(MacroCallReconstructorTest, NestedLineWithinCall) {
auto Macros = createExpander({"C(a)=class X { a; };"});
Expansion Exp(Lex, *Macros);
Expand Down
Loading