Skip to content

Dependencies: Move YAML reader and writer code to swift-dependency-tool #32322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions include/swift/AST/FineGrainedDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -804,15 +804,6 @@ class SourceFileDepGraph {
forEachNode([&](SourceFileDepGraphNode *n) { delete n; });
}

/// Goes at the start of an emitted YAML file to help tools recognize it.
/// May vary in the future according to version, etc.
std::string yamlProlog(const bool hadCompilationError) const {
return std::string("# Fine-grained v0\n") +
(!hadCompilationError ? ""
: "# Dependencies are unknown because a "
"compilation error occurred.\n");
}

SourceFileDepGraphNode *getNode(size_t sequenceNumber) const;

InterfaceAndImplementationPair<SourceFileDepGraphNode>
Expand Down Expand Up @@ -1016,50 +1007,4 @@ template <typename GraphT> class DotFileEmitter {
} // end namespace fine_grained_dependencies
} // end namespace swift

//==============================================================================
// MARK: Declarations for YAMLTraits for reading/writing of SourceFileDepGraph
//==============================================================================

// This introduces a redefinition where ever std::is_same_t<size_t, uint64_t>
// holds
#if !(defined(__linux__) || defined(_WIN64))
LLVM_YAML_DECLARE_SCALAR_TRAITS(size_t, QuotingType::None)
#endif
LLVM_YAML_DECLARE_ENUM_TRAITS(swift::fine_grained_dependencies::NodeKind)
LLVM_YAML_DECLARE_ENUM_TRAITS(swift::fine_grained_dependencies::DeclAspect)
LLVM_YAML_DECLARE_MAPPING_TRAITS(
swift::fine_grained_dependencies::DependencyKey)
LLVM_YAML_DECLARE_MAPPING_TRAITS(swift::fine_grained_dependencies::DepGraphNode)

namespace llvm {
namespace yaml {
template <>
struct MappingContextTraits<
swift::fine_grained_dependencies::SourceFileDepGraphNode,
swift::fine_grained_dependencies::SourceFileDepGraph> {
using SourceFileDepGraphNode =
swift::fine_grained_dependencies::SourceFileDepGraphNode;
using SourceFileDepGraph =
swift::fine_grained_dependencies::SourceFileDepGraph;

static void mapping(IO &io, SourceFileDepGraphNode &node,
SourceFileDepGraph &g);
};

template <>
struct SequenceTraits<
std::vector<swift::fine_grained_dependencies::SourceFileDepGraphNode *>> {
using SourceFileDepGraphNode =
swift::fine_grained_dependencies::SourceFileDepGraphNode;
using NodeVec = std::vector<SourceFileDepGraphNode *>;
static size_t size(IO &, NodeVec &vec);
static SourceFileDepGraphNode &element(IO &, NodeVec &vec, size_t index);
};

} // namespace yaml
} // namespace llvm

LLVM_YAML_DECLARE_MAPPING_TRAITS(
swift::fine_grained_dependencies::SourceFileDepGraph)

#endif // SWIFT_AST_FINE_GRAINED_DEPENDENCIES_H
86 changes: 0 additions & 86 deletions lib/AST/FineGrainedDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLParser.h"


// This file holds the definitions for the fine-grained dependency system
Expand Down Expand Up @@ -371,88 +370,3 @@ void SourceFileDepGraph::emitDotFile(StringRef outputPath,
return false;
});
}

//==============================================================================
// MARK: SourceFileDepGraph YAML reading & writing
//==============================================================================

namespace llvm {
namespace yaml {
// This introduces a redefinition for Linux.
#if !(defined(__linux__) || defined(_WIN64))
void ScalarTraits<size_t>::output(const size_t &Val, void *, raw_ostream &out) {
out << Val;
}

StringRef ScalarTraits<size_t>::input(StringRef scalar, void *ctxt,
size_t &value) {
return scalar.getAsInteger(10, value) ? "could not parse size_t" : "";
}
#endif

void ScalarEnumerationTraits<swift::fine_grained_dependencies::NodeKind>::
enumeration(IO &io, swift::fine_grained_dependencies::NodeKind &value) {
using NodeKind = swift::fine_grained_dependencies::NodeKind;
io.enumCase(value, "topLevel", NodeKind::topLevel);
io.enumCase(value, "nominal", NodeKind::nominal);
io.enumCase(value, "potentialMember", NodeKind::potentialMember);
io.enumCase(value, "member", NodeKind::member);
io.enumCase(value, "dynamicLookup", NodeKind::dynamicLookup);
io.enumCase(value, "externalDepend", NodeKind::externalDepend);
io.enumCase(value, "sourceFileProvide", NodeKind::sourceFileProvide);
}

void ScalarEnumerationTraits<DeclAspect>::enumeration(
IO &io, swift::fine_grained_dependencies::DeclAspect &value) {
using DeclAspect = swift::fine_grained_dependencies::DeclAspect;
io.enumCase(value, "interface", DeclAspect::interface);
io.enumCase(value, "implementation", DeclAspect::implementation);
}

void MappingTraits<DependencyKey>::mapping(
IO &io, swift::fine_grained_dependencies::DependencyKey &key) {
io.mapRequired("kind", key.kind);
io.mapRequired("aspect", key.aspect);
io.mapRequired("context", key.context);
io.mapRequired("name", key.name);
}

void MappingTraits<DepGraphNode>::mapping(
IO &io, swift::fine_grained_dependencies::DepGraphNode &node) {
io.mapRequired("key", node.key);
io.mapOptional("fingerprint", node.fingerprint);
}

void MappingContextTraits<SourceFileDepGraphNode, SourceFileDepGraph>::mapping(
IO &io, SourceFileDepGraphNode &node, SourceFileDepGraph &g) {
MappingTraits<DepGraphNode>::mapping(io, node);
io.mapRequired("sequenceNumber", node.sequenceNumber);
std::vector<size_t> defsIDependUponVec(node.defsIDependUpon.begin(),
node.defsIDependUpon.end());
io.mapRequired("defsIDependUpon", defsIDependUponVec);
io.mapRequired("isProvides", node.isProvides);
if (!io.outputting()) {
for (size_t u : defsIDependUponVec)
node.defsIDependUpon.insert(u);
}
assert(g.getNode(node.sequenceNumber) && "Bad sequence number");
}

size_t SequenceTraits<std::vector<SourceFileDepGraphNode *>>::size(
IO &, std::vector<SourceFileDepGraphNode *> &vec) {
return vec.size();
}

SourceFileDepGraphNode &
SequenceTraits<std::vector<SourceFileDepGraphNode *>>::element(
IO &, std::vector<SourceFileDepGraphNode *> &vec, size_t index) {
while (vec.size() <= index)
vec.push_back(new SourceFileDepGraphNode());
return *vec[index];
}

void MappingTraits<SourceFileDepGraph>::mapping(IO &io, SourceFileDepGraph &g) {
io.mapRequired("allNodes", g.allNodes, g);
}
} // namespace yaml
} // namespace llvm
130 changes: 129 additions & 1 deletion tools/swift-dependency-tool/swift-dependency-tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,138 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/Support/YAMLTraits.h"

using namespace swift;
using namespace fine_grained_dependencies;

//==============================================================================
// MARK: SourceFileDepGraph YAML reading & writing
//==============================================================================

// This introduces a redefinition where ever std::is_same_t<size_t, uint64_t>
// holds
#if !(defined(__linux__) || defined(_WIN64))
LLVM_YAML_DECLARE_SCALAR_TRAITS(size_t, QuotingType::None)
#endif
LLVM_YAML_DECLARE_ENUM_TRAITS(swift::fine_grained_dependencies::NodeKind)
LLVM_YAML_DECLARE_ENUM_TRAITS(swift::fine_grained_dependencies::DeclAspect)
LLVM_YAML_DECLARE_MAPPING_TRAITS(
swift::fine_grained_dependencies::DependencyKey)
LLVM_YAML_DECLARE_MAPPING_TRAITS(swift::fine_grained_dependencies::DepGraphNode)

namespace llvm {
namespace yaml {
template <>
struct MappingContextTraits<
swift::fine_grained_dependencies::SourceFileDepGraphNode,
swift::fine_grained_dependencies::SourceFileDepGraph> {
using SourceFileDepGraphNode =
swift::fine_grained_dependencies::SourceFileDepGraphNode;
using SourceFileDepGraph =
swift::fine_grained_dependencies::SourceFileDepGraph;

static void mapping(IO &io, SourceFileDepGraphNode &node,
SourceFileDepGraph &g);
};

template <>
struct SequenceTraits<
std::vector<swift::fine_grained_dependencies::SourceFileDepGraphNode *>> {
using SourceFileDepGraphNode =
swift::fine_grained_dependencies::SourceFileDepGraphNode;
using NodeVec = std::vector<SourceFileDepGraphNode *>;
static size_t size(IO &, NodeVec &vec);
static SourceFileDepGraphNode &element(IO &, NodeVec &vec, size_t index);
};

} // namespace yaml
} // namespace llvm

LLVM_YAML_DECLARE_MAPPING_TRAITS(
swift::fine_grained_dependencies::SourceFileDepGraph)

namespace llvm {
namespace yaml {
// This introduces a redefinition for Linux.
#if !(defined(__linux__) || defined(_WIN64))
void ScalarTraits<size_t>::output(const size_t &Val, void *, raw_ostream &out) {
out << Val;
}

StringRef ScalarTraits<size_t>::input(StringRef scalar, void *ctxt,
size_t &value) {
return scalar.getAsInteger(10, value) ? "could not parse size_t" : "";
}
#endif

void ScalarEnumerationTraits<swift::fine_grained_dependencies::NodeKind>::
enumeration(IO &io, swift::fine_grained_dependencies::NodeKind &value) {
using NodeKind = swift::fine_grained_dependencies::NodeKind;
io.enumCase(value, "topLevel", NodeKind::topLevel);
io.enumCase(value, "nominal", NodeKind::nominal);
io.enumCase(value, "potentialMember", NodeKind::potentialMember);
io.enumCase(value, "member", NodeKind::member);
io.enumCase(value, "dynamicLookup", NodeKind::dynamicLookup);
io.enumCase(value, "externalDepend", NodeKind::externalDepend);
io.enumCase(value, "sourceFileProvide", NodeKind::sourceFileProvide);
}

void ScalarEnumerationTraits<DeclAspect>::enumeration(
IO &io, swift::fine_grained_dependencies::DeclAspect &value) {
using DeclAspect = swift::fine_grained_dependencies::DeclAspect;
io.enumCase(value, "interface", DeclAspect::interface);
io.enumCase(value, "implementation", DeclAspect::implementation);
}

void MappingTraits<DependencyKey>::mapping(
IO &io, swift::fine_grained_dependencies::DependencyKey &key) {
io.mapRequired("kind", key.kind);
io.mapRequired("aspect", key.aspect);
io.mapRequired("context", key.context);
io.mapRequired("name", key.name);
}

void MappingTraits<DepGraphNode>::mapping(
IO &io, swift::fine_grained_dependencies::DepGraphNode &node) {
io.mapRequired("key", node.key);
io.mapOptional("fingerprint", node.fingerprint);
}

void MappingContextTraits<SourceFileDepGraphNode, SourceFileDepGraph>::mapping(
IO &io, SourceFileDepGraphNode &node, SourceFileDepGraph &g) {
MappingTraits<DepGraphNode>::mapping(io, node);
io.mapRequired("sequenceNumber", node.sequenceNumber);
std::vector<size_t> defsIDependUponVec(node.defsIDependUpon.begin(),
node.defsIDependUpon.end());
io.mapRequired("defsIDependUpon", defsIDependUponVec);
io.mapRequired("isProvides", node.isProvides);
if (!io.outputting()) {
for (size_t u : defsIDependUponVec)
node.defsIDependUpon.insert(u);
}
assert(g.getNode(node.sequenceNumber) && "Bad sequence number");
}

size_t SequenceTraits<std::vector<SourceFileDepGraphNode *>>::size(
IO &, std::vector<SourceFileDepGraphNode *> &vec) {
return vec.size();
}

SourceFileDepGraphNode &
SequenceTraits<std::vector<SourceFileDepGraphNode *>>::element(
IO &, std::vector<SourceFileDepGraphNode *> &vec, size_t index) {
while (vec.size() <= index)
vec.push_back(new SourceFileDepGraphNode());
return *vec[index];
}

void MappingTraits<SourceFileDepGraph>::mapping(IO &io, SourceFileDepGraph &g) {
io.mapRequired("allNodes", g.allNodes, g);
}
} // namespace yaml
} // namespace llvm

enum class ActionType : unsigned {
None,
BinaryToYAML,
Expand Down Expand Up @@ -81,7 +209,7 @@ int main(int argc, char *argv[]) {
bool hadError =
withOutputFile(diags, options::OutputFilename,
[&](llvm::raw_pwrite_stream &out) {
out << fg->yamlProlog(/*hadError=*/false);
out << "# Fine-grained v0\n";
llvm::yaml::Output yamlWriter(out);
yamlWriter << *fg;
return false;
Expand Down