Files
swift-mirror/tools/swift-reflection-test/swift-reflection-test.cpp
David Farler 086000a198 Start the swiftReflection library
- Nearly done: TypeRefs and the mangled name decoder.

- Add the swift-reflection-test tool.
  The field reflection pipeline is roughly:
  - Decode type references
  - Substitute generic parameters
  - Calculate sizes and offsets

  There is currently only one action in the tool, which will test the
  *Decode* part of the pipeline: `dump-reflection-section`. This reads
  the *swift3_reflect section from an object file and dumps the decoded
  type references for all of the stored properties and enum cases in the
  file.
  - TODO: Write tests with various type arrangements to exercise the
    decoder - there are likely some holes in the decoder still since the
    AST mangler is quite rich in its kinds.

  TODO: The next test mode, `dump-field-types`, will do the following:
  1. Launch a swift executable with a canned stopping point
  2. Get the address of a heap object instance of interest
  3. Dump the fully substituted typerefs of all of the stored properties
  or enum case payloads.

  That test mode will be more involved since it will attach to another
  process and need to read from its address space but will test the
  entire out-of-process reflection pipeline in a controlled environment.
  We can maybe take this test a step further, with an option or a new
  test mode, that prints the entire heap reference graph rooted at that
  object of interest, in order to test the ability to detect reference
  cycles, for example.
2016-02-04 18:10:49 -08:00

135 lines
4.0 KiB
C++

//===--- swift-reflection-test.cpp - Reflection testing application -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/Demangle.h"
#include "swift/Basic/LLVMInitialize.h"
#include "swift/Reflection/ReflectionContext.h"
#include "swift/Reflection/TypeRef.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ELF.h"
#include "llvm/Support/CommandLine.h"
using llvm::dyn_cast;
using llvm::StringRef;
using namespace swift;
using namespace reflection;
using namespace Demangle;
namespace {
enum class ActionType {
None,
DumpReflectionSection,
};
} // end anonymous namespace
namespace options {
static llvm::cl::opt<ActionType>
Action(llvm::cl::desc("Mode:"),
llvm::cl::values(
clEnumValN(ActionType::DumpReflectionSection,
"dump-reflection-section",
"Dump the reflection binary section after decoding "
"property and case records."),
clEnumValEnd));
static llvm::cl::opt<std::string>
BinaryFilename("binary-filename", llvm::cl::desc("Filename of the binary file"),
llvm::cl::Required);
} // end namespace options
static int doDumpReflectionSection(StringRef BinaryFilename) {
llvm::object::SectionRef reflectionSectionRef;
auto binaryOrError = llvm::object::createBinary(BinaryFilename);
if (auto errorCode = binaryOrError.getError()) {
llvm::errs() << "swift-reflection-test: '" << BinaryFilename << '\'';
llvm::errs() << ": " << errorCode.message() << "\n";
return EXIT_FAILURE;
}
auto binary = binaryOrError.get().getBinary();
auto object = cast<llvm::object::ObjectFile>(binary);
for (auto section : object->sections()) {
StringRef sectionName;
section.getName(sectionName);
if (sectionName.equals("__swift3_reflect") ||
sectionName.equals(".swift3_reflect")) {
reflectionSectionRef = section;
break;
}
}
if (reflectionSectionRef.getObject() == nullptr) {
llvm::errs() << BinaryFilename;
llvm::errs() << " doesn't have a swift3_reflect section!\n";
return EXIT_FAILURE;
}
StringRef sectionContents;
reflectionSectionRef.getContents(sectionContents);
const ReflectionSection section {
BinaryFilename,
reinterpret_cast<const void *>(sectionContents.begin()),
reinterpret_cast<const void *>(sectionContents.end())
};
ReflectionContext RC;
for (const auto &descriptor : section) {
auto typeName = demangleTypeAsString(descriptor.getMangledTypeName());
auto mangledTypeName = descriptor.getMangledTypeName();
auto demangleTree = demangleTypeAsNode(mangledTypeName);
auto TR = decodeDemangleNode(RC, demangleTree);
llvm::errs() << typeName << '\n';
for (size_t i = 0; i < typeName.size(); ++i)
llvm::errs() << '=';
llvm::errs() << '\n';
TR->dump();
llvm::errs() << "\n";
for (auto &field : descriptor.getFieldRecords()) {
auto fieldDemangleTree = demangleTypeAsNode(field.getMangledTypeName());
auto fieldTR = decodeDemangleNode(RC, fieldDemangleTree);
auto fieldName = field.getFieldName();
llvm::errs() << "- " << fieldName << ":\n";
fieldTR->dump();
llvm::errs() << "\n";
}
llvm::errs() << '\n';
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
llvm::cl::ParseCommandLineOptions(argc, argv, "Swift Reflection Test\n");
switch (options::Action) {
case ActionType::DumpReflectionSection:
return doDumpReflectionSection(options::BinaryFilename);
break;
case ActionType::None:
llvm::cl::PrintHelpMessage();
return EXIT_FAILURE;
break;
}
}