From 7b27b283f91c47c83e3b0c9b605c023ceb6883f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= Date: Fri, 5 Nov 2021 17:58:53 +0100 Subject: [PATCH] Implement --debug-only option in order to easily dump LLVM debug output For example: `cling --debug-only="orc,jitlink"` --- include/cling/Interpreter/ClingOptions.inc | 6 ++++ lib/Interpreter/InvocationOptions.cpp | 32 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/cling/Interpreter/ClingOptions.inc b/include/cling/Interpreter/ClingOptions.inc index 13d329a9..7a9fd61a 100644 --- a/include/cling/Interpreter/ClingOptions.inc +++ b/include/cling/Interpreter/ClingOptions.inc @@ -18,6 +18,12 @@ PREFIX(prefix_2, {"--" COMMA 0}) OPTION(prefix_0, "", INPUT, Input, INVALID, INVALID, 0, 0, 0, 0, 0, 0) OPTION(prefix_0, "", UNKNOWN, Unknown, INVALID, INVALID, 0, 0, 0, 0, 0, 0) +#ifndef NDEBUG +OPTION(prefix_2, "debug-only=", _debugFlags_EQ, Joined, INVALID, INVALID, 0, 0, 0, + "Debug flags to enable", 0, 0) +OPTION(prefix_2, "debug-only", _debugFlags, Separate, INVALID, INVALID, 0, 0, 0, + "Debug flags to enable", 0, 0) +#endif OPTION(prefix_2, "errorout", _errorout, Flag, INVALID, INVALID, 0, 0, 0, "Do not recover from input errors", 0, 0) // Re-implement to forward to our help diff --git a/lib/Interpreter/InvocationOptions.cpp b/lib/Interpreter/InvocationOptions.cpp index 12cec5bc..8ebe19be 100644 --- a/lib/Interpreter/InvocationOptions.cpp +++ b/lib/Interpreter/InvocationOptions.cpp @@ -19,6 +19,7 @@ #include "llvm/Option/Option.h" #include "llvm/Option/OptTable.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" #include @@ -69,6 +70,27 @@ static const char kNoStdInc[] = "-nostdinc"; return new ClingOptTable(); } +#ifndef NDEBUG + void setDebugOnlyTypes(const char *FlagsStr) { + llvm::SmallString<64> MutFlagsStr(FlagsStr); + + // Collect single flags and replace seperators with null-terminators + std::vector DebugTypes; + char *Flag = MutFlagsStr.data(); + do { + DebugTypes.push_back(Flag); + while (*Flag != ',' && *Flag != ';' && *Flag != '\0') + ++Flag; + if (*Flag == '\0') + break; + *(Flag++) = '\0'; + } while (true); + + DebugFlag = true; + setCurrentDebugTypes(DebugTypes.data(), DebugTypes.size()); + } +#endif + static void ParseStartupOpts(cling::InvocationOptions& Opts, InputArgList& Args) { Opts.ErrorOut = Args.hasArg(OPT__errorout); @@ -84,6 +106,16 @@ static const char kNoStdInc[] = "-nostdinc"; Opts.MetaString = "."; } } +#ifndef NDEBUG + if (Arg* DebugFlagsArg = Args.getLastArg(OPT__debugFlags, OPT__debugFlags_EQ)) { + const char *FlagsStr = DebugFlagsArg->getValue(); + if (*FlagsStr == '\0') { + cling::errs() << "ERROR: --debug-only argument must be non-empty! Ignoring it.\n"; + } else { + setDebugOnlyTypes(FlagsStr); + } + } +#endif } static void Extend(std::vector& A, std::vector B) {