@@ -9156,52 +9156,47 @@ static std::vector<ASTNode> expandPreamble(AbstractFunctionDecl *func) {
91569156static 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
92079202BraceStmt *AbstractFunctionDecl::getMacroExpandedBody () const {
0 commit comments