@@ -9117,24 +9117,74 @@ bool AbstractFunctionDecl::hasBody() const {
91179117 }
91189118}
91199119
9120+ // / Expand all preamble macros attached to the given function declaration.
9121+ static std::vector<ASTNode> expandPreamble (AbstractFunctionDecl *func) {
9122+ std::vector<ASTNode> preamble;
91209123
9124+ ASTContext &ctx = func->getASTContext ();
9125+ ExpandPreambleMacroRequest request{func};
9126+ auto module = func->getParentModule ();
9127+ for (auto bufferID : evaluateOrDefault (ctx.evaluator , request, { })) {
9128+ auto bufferStart = ctx.SourceMgr .getLocForBufferStart (bufferID);
9129+ auto preambleSF = module ->getSourceFileContainingLocation (bufferStart);
9130+ preamble.insert (preamble.end (),
9131+ preambleSF->getTopLevelItems ().begin (),
9132+ preambleSF->getTopLevelItems ().end ());
9133+ }
9134+
9135+ return preamble;
9136+ }
9137+
9138+ // / Expand body macros and produce the resulting body.
91219139static BraceStmt *expandBodyMacro (AbstractFunctionDecl *fn) {
91229140 ASTContext &ctx = fn->getASTContext ();
91239141
9124- auto bufferID = evaluateOrDefault (
9125- ctx.evaluator , ExpandBodyMacroRequest{fn}, llvm::None);
9126- if (!bufferID) {
9142+ // Expand the preamble.
9143+ auto preamble = expandPreamble (fn);
9144+
9145+ // Expand a body macro, if there is one.
9146+ if (auto bufferID = evaluateOrDefault (
9147+ ctx.evaluator , ExpandBodyMacroRequest{fn}, llvm::None)) {
9148+ CharSourceRange bufferRange = ctx.SourceMgr .getRangeForBuffer (*bufferID);
9149+ auto bufferStart = bufferRange.getStart ();
9150+ auto module = fn->getParentModule ();
9151+ auto macroSourceFile = module ->getSourceFileContainingLocation (bufferStart);
9152+
9153+ // When there is no preamble, adopt the body itself.
9154+ if (preamble.empty ()) {
9155+ return BraceStmt::create (
9156+ ctx, bufferRange.getStart (), macroSourceFile->getTopLevelItems (),
9157+ bufferRange.getEnd ());
9158+ }
9159+
9160+ // Merge the preamble into the macro-produced body.
9161+ auto contents = std::move (preamble);
9162+ contents.insert (
9163+ contents.end (),
9164+ macroSourceFile->getTopLevelItems ().begin (),
9165+ macroSourceFile->getTopLevelItems ().end ());
9166+ return BraceStmt::create (
9167+ ctx, bufferRange.getStart (), contents, bufferRange.getEnd ());
9168+ }
9169+
9170+ // There is no body macro. If there's no preamble, either, then there is
9171+ // nothing to do.
9172+ if (preamble.empty ())
91279173 return nullptr ;
9128- }
91299174
9130- CharSourceRange bufferRange = ctx.SourceMgr .getRangeForBuffer (*bufferID);
9131- auto bufferStart = bufferRange.getStart ();
9132- auto module = fn->getParentModule ();
9133- auto macroSourceFile = module ->getSourceFileContainingLocation (bufferStart);
9175+ // If there is no body, the preamble has nowhere to go.
9176+ auto body = fn->getBody (/* canSynthesize=*/ true );
9177+ if (!body) {
9178+ // FIXME: diagnose this
9179+ return nullptr ;
9180+ }
91349181
9182+ // Merge the preamble into the existing body.
9183+ auto contents = std::move (preamble);
9184+ contents.insert (
9185+ contents.end (), body->getElements ().begin (), body->getElements ().end ());
91359186 return BraceStmt::create (
9136- ctx, bufferRange.getStart (), macroSourceFile->getTopLevelItems (),
9137- bufferRange.getEnd ());
9187+ ctx, body->getLBraceLoc (), contents, body->getRBraceLoc ());
91389188}
91399189
91409190BraceStmt *AbstractFunctionDecl::getMacroExpandedBody () const {
0 commit comments