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