[Macros] Add a flag -dump-macro-expansions as a debugging aid.

This commit is contained in:
Doug Gregor
2022-11-14 21:29:24 -08:00
parent 60eb0fb3f5
commit f2f974bb94
4 changed files with 17 additions and 0 deletions

View File

@@ -528,6 +528,9 @@ namespace swift {
/// Enables dumping type witness systems from associated type inference.
bool DumpTypeWitnessSystems = false;
/// Enables dumping macro expansions.
bool DumpMacroExpansions = false;
/// The model of concurrency to be used.
ConcurrencyModel ActiveConcurrencyModel = ConcurrencyModel::Standard;

View File

@@ -343,6 +343,9 @@ def dump_requirement_machine : Flag<["-"], "dump-requirement-machine">,
def debug_requirement_machine : Joined<["-"], "debug-requirement-machine=">,
HelpText<"Fine-grained debug output from the generics implementation">;
def dump_macro_expansions : Flag<["-"], "dump-macro-expansions">,
HelpText<"Dumps the results of each macro expansion">;
def analyze_requirement_machine : Flag<["-"], "analyze-requirement-machine">,
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
HelpText<"Print out requirement machine statistics at the end of the compilation job">;

View File

@@ -1042,6 +1042,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.AnalyzeRequirementMachine = Args.hasArg(
OPT_analyze_requirement_machine);
Opts.DumpMacroExpansions = Args.hasArg(
OPT_dump_macro_expansions);
if (const Arg *A = Args.getLastArg(OPT_debug_requirement_machine))
Opts.DebugRequirementMachine = A->getValue();

View File

@@ -433,6 +433,14 @@ Expr *swift::expandMacroExpr(
}
}
// Dump macro expansions to standard output, if requested.
if (ctx.LangOpts.DumpMacroExpansions) {
llvm::errs() << bufferName << " as " << expandedType.getString()
<< "\n------------------------------\n"
<< evaluatedSource
<< "\n------------------------------\n";
}
// Create a new source buffer with the contents of the expanded macro.
auto macroBuffer =
llvm::MemoryBuffer::getMemBuffer(evaluatedSource, bufferName);