Skip to content

Commit dd80cf7

Browse files
committed
[Macros] Parse body macro expansion as a single brace statement
Now that the macro expansion machinery is wrapping the contents of a body macro in curly braces, parse it as a single brace statement. This has the advantage of giving us real locations for `{` and `}` rather than synthesizing them, and simplifies the implementation of macro body expansion somewhat.
1 parent 2b7c709 commit dd80cf7

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

lib/AST/Decl.cpp

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9156,52 +9156,47 @@ static std::vector<ASTNode> expandPreamble(AbstractFunctionDecl *func) {
91569156
static BraceStmt *expandBodyMacro(AbstractFunctionDecl *fn) {
91579157
ASTContext &ctx = fn->getASTContext();
91589158

9159-
// Expand the preamble.
9160-
auto preamble = expandPreamble(fn);
9161-
91629159
// Expand a body macro, if there is one.
9160+
BraceStmt *macroExpandedBody = nullptr;
91639161
if (auto bufferID = evaluateOrDefault(
91649162
ctx.evaluator, ExpandBodyMacroRequest{fn}, llvm::None)) {
91659163
CharSourceRange bufferRange = ctx.SourceMgr.getRangeForBuffer(*bufferID);
91669164
auto bufferStart = bufferRange.getStart();
91679165
auto module = fn->getParentModule();
91689166
auto macroSourceFile = module->getSourceFileContainingLocation(bufferStart);
91699167

9170-
// When there is no preamble, adopt the body itself.
9171-
if (preamble.empty()) {
9172-
return BraceStmt::create(
9173-
ctx, bufferRange.getStart(), macroSourceFile->getTopLevelItems(),
9174-
bufferRange.getEnd());
9168+
if (macroSourceFile->getTopLevelItems().size() == 1) {
9169+
auto stmt = macroSourceFile->getTopLevelItems()[0].dyn_cast<Stmt *>();
9170+
macroExpandedBody = dyn_cast<BraceStmt>(stmt);
91759171
}
9176-
9177-
// Merge the preamble into the macro-produced body.
9178-
auto contents = std::move(preamble);
9179-
contents.insert(
9180-
contents.end(),
9181-
macroSourceFile->getTopLevelItems().begin(),
9182-
macroSourceFile->getTopLevelItems().end());
9183-
return BraceStmt::create(
9184-
ctx, bufferRange.getStart(), contents, bufferRange.getEnd());
91859172
}
91869173

9187-
// There is no body macro. If there's no preamble, either, then there is
9188-
// nothing to do.
9174+
// Expand the preamble.
9175+
auto preamble = expandPreamble(fn);
9176+
9177+
// If there is no preamble, we're done one way or the other: return the
9178+
// macro-expanded body.
91899179
if (preamble.empty())
9190-
return nullptr;
9180+
return macroExpandedBody;
9181+
9182+
// We have a preamble. The body is either the one produced by macro expansion,
9183+
// or if not that, the one that was written.
9184+
auto body = macroExpandedBody ? macroExpandedBody : fn->getBody();
91919185

9192-
// If there is no body, the preamble has nowhere to go.
9193-
auto body = fn->getBody(/*canSynthesize=*/true);
9186+
// If there is no body at this point, the preamble has nowhere to go.
91949187
if (!body) {
91959188
// FIXME: diagnose this
91969189
return nullptr;
91979190
}
91989191

9199-
// Merge the preamble into the existing body.
9192+
// Merge the preamble into the body.
92009193
auto contents = std::move(preamble);
92019194
contents.insert(
9202-
contents.end(), body->getElements().begin(), body->getElements().end());
9195+
contents.end(),
9196+
body->getElements().begin(),
9197+
body->getElements().end());
92039198
return BraceStmt::create(
9204-
ctx, body->getLBraceLoc(), contents, body->getRBraceLoc());
9199+
ctx, body->getStartLoc(), contents, body->getEndLoc());
92059200
}
92069201

92079202
BraceStmt *AbstractFunctionDecl::getMacroExpandedBody() const {

lib/Parse/ParseRequests.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,27 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator,
182182

183183
case GeneratedSourceInfo::ExpressionMacroExpansion:
184184
case GeneratedSourceInfo::PreambleMacroExpansion:
185-
case GeneratedSourceInfo::BodyMacroExpansion:
186185
case GeneratedSourceInfo::ReplacedFunctionBody:
187186
case GeneratedSourceInfo::PrettyPrinted: {
188187
parser.parseTopLevelItems(items);
189188
break;
190189
}
191190

191+
case GeneratedSourceInfo::BodyMacroExpansion: {
192+
// Prime the lexer.
193+
if (parser.Tok.is(tok::NUM_TOKENS))
194+
parser.consumeTokenWithoutFeedingReceiver();
195+
196+
if (parser.Tok.is(tok::l_brace)) {
197+
if (auto body =
198+
parser.parseBraceItemList(diag::invalid_diagnostic)
199+
.getPtrOrNull())
200+
items.push_back(body);
201+
}
202+
203+
break;
204+
}
205+
192206
case GeneratedSourceInfo::MemberMacroExpansion: {
193207
parser.parseExpandedMemberList(items);
194208
break;

0 commit comments

Comments
 (0)