Skip to content

Commit 9b643d5

Browse files
committed
[Macros] Add a flag -dump-macro-expansions as a debugging aid.
1 parent 70323dd commit 9b643d5

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@ namespace swift {
528528
/// Enables dumping type witness systems from associated type inference.
529529
bool DumpTypeWitnessSystems = false;
530530

531+
/// Enables dumping macro expansions.
532+
bool DumpMacroExpansions = false;
533+
531534
/// The model of concurrency to be used.
532535
ConcurrencyModel ActiveConcurrencyModel = ConcurrencyModel::Standard;
533536

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ def dump_requirement_machine : Flag<["-"], "dump-requirement-machine">,
343343
def debug_requirement_machine : Joined<["-"], "debug-requirement-machine=">,
344344
HelpText<"Fine-grained debug output from the generics implementation">;
345345

346+
def dump_macro_expansions : Flag<["-"], "dump-macro-expansions">,
347+
HelpText<"Dumps the results of each macro expansion">;
348+
346349
def analyze_requirement_machine : Flag<["-"], "analyze-requirement-machine">,
347350
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
348351
HelpText<"Print out requirement machine statistics at the end of the compilation job">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
10361036
Opts.AnalyzeRequirementMachine = Args.hasArg(
10371037
OPT_analyze_requirement_machine);
10381038

1039+
Opts.DumpMacroExpansions = Args.hasArg(
1040+
OPT_dump_macro_expansions);
1041+
10391042
if (const Arg *A = Args.getLastArg(OPT_debug_requirement_machine))
10401043
Opts.DebugRequirementMachine = A->getValue();
10411044

lib/Sema/TypeCheckMacros.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ Expr *swift::expandMacroExpr(
433433
}
434434
}
435435

436+
// Dump macro expansions to standard output, if requested.
437+
if (ctx.LangOpts.DumpMacroExpansions) {
438+
llvm::errs() << bufferName << " as " << expandedType.getString()
439+
<< "\n------------------------------\n"
440+
<< evaluatedSource
441+
<< "\n------------------------------\n";
442+
}
443+
436444
// Create a new source buffer with the contents of the expanded macro.
437445
auto macroBuffer =
438446
llvm::MemoryBuffer::getMemBuffer(evaluatedSource, bufferName);

0 commit comments

Comments
 (0)