@@ -9156,52 +9156,47 @@ static std::vector<ASTNode> expandPreamble(AbstractFunctionDecl *func) {
9156
9156
static BraceStmt *expandBodyMacro (AbstractFunctionDecl *fn) {
9157
9157
ASTContext &ctx = fn->getASTContext ();
9158
9158
9159
- // Expand the preamble.
9160
- auto preamble = expandPreamble (fn);
9161
-
9162
9159
// Expand a body macro, if there is one.
9160
+ BraceStmt *macroExpandedBody = nullptr ;
9163
9161
if (auto bufferID = evaluateOrDefault (
9164
9162
ctx.evaluator , ExpandBodyMacroRequest{fn}, llvm::None)) {
9165
9163
CharSourceRange bufferRange = ctx.SourceMgr .getRangeForBuffer (*bufferID);
9166
9164
auto bufferStart = bufferRange.getStart ();
9167
9165
auto module = fn->getParentModule ();
9168
9166
auto macroSourceFile = module ->getSourceFileContainingLocation (bufferStart);
9169
9167
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);
9175
9171
}
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 ());
9185
9172
}
9186
9173
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.
9189
9179
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 ();
9191
9185
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.
9194
9187
if (!body) {
9195
9188
// FIXME: diagnose this
9196
9189
return nullptr ;
9197
9190
}
9198
9191
9199
- // Merge the preamble into the existing body.
9192
+ // Merge the preamble into the body.
9200
9193
auto contents = std::move (preamble);
9201
9194
contents.insert (
9202
- contents.end (), body->getElements ().begin (), body->getElements ().end ());
9195
+ contents.end (),
9196
+ body->getElements ().begin (),
9197
+ body->getElements ().end ());
9203
9198
return BraceStmt::create (
9204
- ctx, body->getLBraceLoc (), contents, body->getRBraceLoc ());
9199
+ ctx, body->getStartLoc (), contents, body->getEndLoc ());
9205
9200
}
9206
9201
9207
9202
BraceStmt *AbstractFunctionDecl::getMacroExpandedBody () const {
0 commit comments