This adds the `target dump typesystem'`command which dumps the TypeSystem of the
target itself (aka the 'scratch TypeSystem'). This is similar to `target modules
dump ast` which dumps the AST of lldb::Modules associated with a selected
target.
Unlike `target modules dump ast`, the new command is not a subcommand of `target
modules dump` as it's not touching the modules of a target at all. Also unlike
`target modules dump ast` I tried to keep the implementation language-neutral,
so this patch moves our Clang `Dump` to the `TypeSystem` interface so it will
also dump the state of any future/downstream scratch TypeSystems (e.g., Swift).
That's also why the command just refers to a 'typesystem' instead of an 'ast'
(which is only how Clang is necessarily modelling the internal TypeSystem
state).
The main motivation for this patch is that I need to write some tests that check
for duplicates in the ScratchTypeSystemClang of a target. There is currently no
way to check for this at the moment (beside measuring memory consumption of
course). It's probably also useful for debugging LLDB itself.
Reviewed By: labath
Differential Revision: https://reviews.llvm.org/D111936
lldb::opaque_compiler_type_t type, Stream *s,
lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) = 0;
+ /// Dump a textual representation of the internal TypeSystem state to the
+ /// given stream.
+ ///
+ /// This should not modify the state of the TypeSystem if possible.
+ virtual void Dump(llvm::raw_ostream &output) = 0;
+
// TODO: These methods appear unused. Should they be removed?
virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
~CommandObjectMultiwordTargetStopHooks() override = default;
};
+#pragma mark CommandObjectTargetDumpTypesystem
+
+/// Dumps the TypeSystem of the selected Target.
+class CommandObjectTargetDumpTypesystem : public CommandObjectParsed {
+public:
+ CommandObjectTargetDumpTypesystem(CommandInterpreter &interpreter)
+ : CommandObjectParsed(
+ interpreter, "target dump typesystem",
+ "Dump the state of the target's internal type system.\n"
+ "Intended to be used for debugging LLDB itself.",
+ nullptr, eCommandRequiresTarget) {}
+
+ ~CommandObjectTargetDumpTypesystem() override = default;
+
+protected:
+ bool DoExecute(Args &command, CommandReturnObject &result) override {
+ if (!command.empty()) {
+ result.AppendError("target dump typesystem doesn't take arguments.");
+ return result.Succeeded();
+ }
+
+ // Go over every scratch TypeSystem and dump to the command output.
+ for (TypeSystem *ts : GetSelectedTarget().GetScratchTypeSystems())
+ ts->Dump(result.GetOutputStream().AsRawOstream());
+
+ result.SetStatus(eReturnStatusSuccessFinishResult);
+ return result.Succeeded();
+ }
+};
+
+#pragma mark CommandObjectTargetDump
+
+/// Multi-word command for 'target dump'.
+class CommandObjectTargetDump : public CommandObjectMultiword {
+public:
+ // Constructors and Destructors
+ CommandObjectTargetDump(CommandInterpreter &interpreter)
+ : CommandObjectMultiword(
+ interpreter, "target dump",
+ "Commands for dumping information about the target.",
+ "target dump [typesystem]") {
+ LoadSubCommand(
+ "typesystem",
+ CommandObjectSP(new CommandObjectTargetDumpTypesystem(interpreter)));
+ }
+
+ ~CommandObjectTargetDump() override = default;
+};
+
#pragma mark CommandObjectMultiwordTarget
// CommandObjectMultiwordTarget
CommandObjectSP(new CommandObjectTargetCreate(interpreter)));
LoadSubCommand("delete",
CommandObjectSP(new CommandObjectTargetDelete(interpreter)));
+ LoadSubCommand("dump",
+ CommandObjectSP(new CommandObjectTargetDump(interpreter)));
LoadSubCommand("list",
CommandObjectSP(new CommandObjectTargetList(interpreter)));
LoadSubCommand("select",
llvm::dyn_cast_or_null<TypeSystemClang>(&ts_or_err.get());
if (!clang)
return;
- clang->Dump(s);
+ clang->Dump(s.AsRawOstream());
}
SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() {
return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
}
-void PdbAstBuilder::Dump(Stream &stream) { m_clang.Dump(stream); }
+void PdbAstBuilder::Dump(Stream &stream) {
+ m_clang.Dump(stream.AsRawOstream());
+}
llvm::dyn_cast_or_null<TypeSystemClang>(&type_system_or_err.get());
if (!clang_type_system)
return;
- clang_type_system->Dump(s);
+ clang_type_system->Dump(s.AsRawOstream());
}
void SymbolFilePDB::FindTypesByRegex(
}
#endif
-void TypeSystemClang::Dump(Stream &s) {
- Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
- tu->dump(s.AsRawOstream());
+void TypeSystemClang::Dump(llvm::raw_ostream &output) {
+ GetTranslationUnitDecl()->dump(output);
}
void TypeSystemClang::DumpFromSymbolFile(Stream &s,
return &scratch_ast.GetIsolatedAST(*ast_kind);
}
+/// Returns a human-readable name that uniquely identifiers the sub-AST kind.
+static llvm::StringRef
+GetNameForIsolatedASTKind(ScratchTypeSystemClang::IsolatedASTKind kind) {
+ switch (kind) {
+ case ScratchTypeSystemClang::IsolatedASTKind::CppModules:
+ return "C++ modules";
+ }
+ llvm_unreachable("Unimplemented IsolatedASTKind?");
+}
+
+void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output) {
+ // First dump the main scratch AST.
+ output << "State of scratch Clang type system:\n";
+ TypeSystemClang::Dump(output);
+
+ // Now sort the isolated sub-ASTs.
+ typedef std::pair<IsolatedASTKey, TypeSystem *> KeyAndTS;
+ std::vector<KeyAndTS> sorted_typesystems;
+ for (const auto &a : m_isolated_asts)
+ sorted_typesystems.emplace_back(a.first, a.second.get());
+ llvm::stable_sort(sorted_typesystems,
+ [](const KeyAndTS &lhs, const KeyAndTS &rhs) {
+ return lhs.first < rhs.first;
+ });
+
+ // Dump each sub-AST too.
+ for (const auto &a : sorted_typesystems) {
+ IsolatedASTKind kind =
+ static_cast<ScratchTypeSystemClang::IsolatedASTKind>(a.first);
+ output << "State of scratch Clang type subsystem "
+ << GetNameForIsolatedASTKind(kind) << ":\n";
+ a.second->Dump(output);
+ }
+}
+
UserExpression *ScratchTypeSystemClang::GetUserExpression(
llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Expression::ResultType desired_type,
LLVM_DUMP_METHOD void dump(lldb::opaque_compiler_type_t type) const override;
#endif
- void Dump(Stream &s);
+ /// \see lldb_private::TypeSystem::Dump
+ void Dump(llvm::raw_ostream &output) override;
/// Dump clang AST types from the symbol file.
///
return GetForTarget(target, InferIsolatedASTKindFromLangOpts(lang_opts));
}
+ /// \see lldb_private::TypeSystem::Dump
+ void Dump(llvm::raw_ostream &output) override;
+
UserExpression *
GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
lldb::LanguageType language,
--- /dev/null
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
--- /dev/null
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.decorators import *
+import lldbsuite.test.lldbutil as lldbutil
+
+class TestCase(TestBase):
+
+ mydir = TestBase.compute_mydir(__file__)
+
+ @no_debug_info_test
+ def test_dumping(self):
+ """ Tests dumping an empty and non-empty scratch AST. """
+ self.build()
+ self.createTestTarget()
+
+ # Make sure DummyStruct is not in the scratch AST by default.
+ self.expect("target dump typesystem", matching=False, substrs=["struct DummyStruct"])
+
+ # Move DummyStruct into the scratch AST via the expression evaluator.
+ # FIXME: Once there is an SB API for using variable paths on globals
+ # then this should be done this way.
+ self.expect_expr("s", result_type="DummyStruct")
+
+ # Dump the scratch AST and make sure DummyStruct is in there.
+ self.expect("target dump typesystem", substrs=["struct DummyStruct"])
+
+ @no_debug_info_test
+ def test_invalid_arg(self):
+ """ Test an invalid invocation on 'target dump typesystem'. """
+ self.build()
+ self.createTestTarget()
+ self.expect("target dump typesystem arg", error=True,
+ substrs=["error: target dump typesystem doesn't take arguments."])
--- /dev/null
+struct DummyStruct {
+ int i;
+};
+
+DummyStruct s;
+
+int main() { return s.i; }