-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathyaml_function_emitter.cpp
More file actions
279 lines (232 loc) · 12.8 KB
/
yaml_function_emitter.cpp
File metadata and controls
279 lines (232 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Copyright 2018 Adobe
All Rights Reserved.
NOTICE: Adobe permits you to use, modify, and distribute this file in
accordance with the terms of the Adobe license agreement accompanying
it. If you have received this file from a source other than Adobe,
then your use, modification, or distribution of it requires the prior
written permission of Adobe.
*/
// identity
#include "yaml_function_emitter.hpp"
// stdc++
#include <iostream>
// application
#include "matchers/utilities.hpp"
/**************************************************************************************************/
namespace hyde {
/**************************************************************************************************/
bool yaml_function_emitter::do_merge(const std::string& filepath,
const json& have,
const json& expected,
json& out_merged) {
bool failure{false};
failure |= check_scalar(filepath, have, expected, "", out_merged, "defined_in_file");
failure |= check_scalar_array(filepath, have, expected, "", out_merged, "namespace");
failure |= check_scalar(filepath, have, expected, "", out_merged, "is_ctor");
failure |= check_scalar(filepath, have, expected, "", out_merged, "is_dtor");
failure |= check_map(
filepath, have, expected, "", out_merged, "overloads",
[this](const std::string& filepath, const json& have, const json& expected,
const std::string& nodepath, json& out_merged) {
bool failure{false};
failure |= check_editable_scalar(filepath, have, expected, nodepath, out_merged,
"description");
failure |= check_scalar(filepath, have, expected, nodepath, out_merged,
"signature_with_names");
if (!has_json_flag(expected, "is_ctor") && !has_json_flag(expected, "is_dtor")) {
failure |=
check_editable_scalar(filepath, have, expected, nodepath, out_merged, "return");
}
if (_options._tested_by != hyde::attribute_category::disabled) {
failure |= check_editable_scalar_array(filepath, have, expected, nodepath,
out_merged, "tested_by");
}
failure |=
check_scalar_array(filepath, have, expected, nodepath, out_merged, "annotation");
failure |= check_object_array(
filepath, have, expected, nodepath, out_merged, "arguments", "name",
[this](const std::string& filepath, const json& have, const json& expected,
const std::string& nodepath, json& out_merged) {
bool failure{false};
failure |= check_scalar(filepath, have, expected, nodepath, out_merged, "name");
failure |= check_scalar(filepath, have, expected, nodepath, out_merged, "type");
failure |= check_editable_scalar(filepath, have, expected, nodepath, out_merged,
"description");
failure |=
check_scalar(filepath, have, expected, nodepath, out_merged, "unnamed");
check_inline_comments(expected, out_merged);
return failure;
});
check_inline_comments(expected, out_merged);
return failure;
});
// REVISIT (fosterbrereton) : Roll-up the owners and briefs/descriptions to see if we can derive
// a set of inline values here from inline values used in the function definition(s).
check_inline_comments(expected, out_merged);
return failure;
}
/**************************************************************************************************/
bool yaml_function_emitter::emit(const json& jsn, json& out_emitted, const json& inherited) {
std::filesystem::path dst;
std::string name;
std::string filename;
std::string defined_path;
json overloads = json::object();
bool first{true};
bool is_ctor{false};
bool is_dtor{false};
bool all_compiler_managed{true};
std::size_t count(jsn.size());
std::size_t inline_description_count{0};
json last_inline_description;
std::size_t inline_brief_count{0};
json last_inline_brief;
std::vector<std::string> overload_owners;
for (const auto& overload : jsn) {
if (first) {
dst = dst_path(overload);
// always the unqualified name, as free functions may be defined
// over different namespaces.
name = overload["short_name"];
// prefix to keep free-function from colliding with class member (e.g., `swap`)
filename = (_as_methods ? "m_" : "f_") + filename_filter(name);
defined_path = defined_in_file(overload["defined_in_file"], _src_root);
is_ctor = has_json_flag(overload, "is_ctor");
is_dtor = has_json_flag(overload, "is_dtor");
first = false;
}
const std::string& key = static_cast<const std::string&>(overload["signature"]);
auto& destination = overloads[key];
// If there are any in-source (a.k.a. Doxygen) comments, insert them into
// the node *first* so we can use them to decide if subsequent Hyde fields
// can be deferred.
insert_doxygen(overload, destination);
// The intent of these checks is to roll up brief/description details that were
// entered inline to the top-level file that documents this function and all of its
// overrides. Note that a given override does _not_ require a `brief` value, but
// _may_ require a `description` value if there is more than one override (otherwise
// the description is optional, falling back to the top-level `brief` for the functions
// documentation). I foresee *a lot* of conflation between `brief` and `description`
// as developers document their code, so we'll have to track both of these values as if
// they are the same to make it as easy as possible to bubble up information.
if (destination.count("inline") && destination["inline"].count("brief")) {
++inline_brief_count;
last_inline_brief = destination["inline"]["brief"];
}
if (destination.count("inline") && destination["inline"].count("description")) {
++inline_description_count;
last_inline_description = destination["inline"]["description"];
}
if (destination.count("inline") && destination["inline"].count("owner")) {
const auto& owners = destination["inline"]["owner"];
if (owners.is_string()) {
overload_owners.push_back(owners.get<std::string>());
} else if (owners.is_array()) {
for (const auto& owner : owners) {
overload_owners.push_back(owner.get<std::string>());
}
}
}
destination["signature_with_names"] = overload["signature_with_names"];
// description is now optional when there is a singular variant, or when the overload
// is compiler-managed (implicit, =default, or =delete)
const bool has_associated_inline = has_inline_field(destination, "description");
const bool is_implicit = has_json_flag(overload, "implicit");
const bool is_defaulted = has_json_flag(overload, "defaulted");
const bool is_deleted = has_json_flag(overload, "deleted");
const bool is_compiler_managed = is_implicit || is_defaulted || is_deleted;
const bool is_optional = count <= 1 || is_compiler_managed;
destination["description"] = has_associated_inline ? tag_value_inlined_k :
is_optional ? tag_value_optional_k :
tag_value_missing_k;
if (!is_ctor && !is_dtor) {
destination["return"] = tag_value_optional_k;
}
if (_options._tested_by != hyde::attribute_category::disabled) {
destination["tested_by"] = is_compiler_managed ? tag_value_optional_k : hyde::get_tag(_options._tested_by);
}
insert_annotations(overload, destination);
all_compiler_managed &= is_compiler_managed;
if (!overload["arguments"].empty()) {
std::size_t j{0};
auto& args = destination["arguments"];
for (const auto& arg : overload["arguments"]) {
auto& cur_arg = args[j];
const std::string& name = arg["name"];
const bool unnamed = name.empty();
cur_arg["name"] = unnamed ? "unnamed-" + std::to_string(j) : name;
cur_arg["type"] = arg["type"];
cur_arg["description"] = tag_value_optional_k;
if (unnamed) {
cur_arg["unnamed"] = true;
}
++j;
}
}
}
json node = base_emitter_node(_as_methods ? "method" : "function", name,
_as_methods ? "method" : "function",
all_compiler_managed);
insert_inherited(inherited, node["hyde"]);
// If the function being emitted is either the ctor or dtor of a class,
// the high-level `brief` is optional, as their default implementations
// should require no additional commenatary beyond that which is provided
// on an overload-by-overload basis.
if (is_ctor || is_dtor) {
node["hyde"]["brief"] = tag_value_optional_k;
}
// Here we roll-up the brief(s) and description(s) from the function overloads.
// The complication here is that `description` may be required for the overloads,
// but `brief` is not. However at the top level, `brief` _is_ required, and
// `description` is not. So if there is one brief _or_ description, use that
// as the inline brief for the top-level docs. (If there is one of each, brief
// wins.) Beyond that, if there are multiple briefs and multiple descriptions,
// we'll put some pat statement into the brief indicating as much. Finally, if
// at least one overload has an inline `brief`, then the top-level `brief` is
// marked inlined.
if (inline_brief_count == 1) {
node["hyde"]["inline"]["brief"] = last_inline_brief;
node["hyde"]["brief"] = tag_value_inlined_k;
} else if (inline_description_count == 1) {
node["hyde"]["inline"]["brief"] = last_inline_description;
node["hyde"]["brief"] = tag_value_inlined_k;
} else if (inline_brief_count > 1 || inline_description_count > 1) {
node["hyde"]["inline"]["brief"] = "_multiple descriptions_";
node["hyde"]["brief"] = tag_value_inlined_k;
}
// Round up any overload owners into an inline top-level owner field,
// and then set the hyde owner field to inlined.
if (!overload_owners.empty()) {
std::sort(overload_owners.begin(), overload_owners.end());
overload_owners.erase(std::unique(overload_owners.begin(), overload_owners.end()),
overload_owners.end());
json::array_t owners;
std::copy(overload_owners.begin(), overload_owners.end(), std::back_inserter(owners));
node["hyde"]["inline"]["owner"] = std::move(owners);
node["hyde"]["owner"] = tag_value_inlined_k;
} else if (node["hyde"].count("inline") && node["hyde"]["inline"].count("owner")) {
node["hyde"]["owner"] = tag_value_inlined_k;
}
// All overloads will have the same namespace
if (!_as_methods && jsn.size() > 0) {
for (const auto& ns : jsn.front()["namespaces"])
node["hyde"]["namespace"].push_back(static_cast<const std::string&>(ns));
}
node["hyde"]["defined_in_file"] = defined_path;
node["hyde"]["overloads"] = std::move(overloads);
if (is_ctor) node["hyde"]["is_ctor"] = true;
if (is_dtor) node["hyde"]["is_dtor"] = true;
if (_mode == yaml_mode::transcribe && !exists(dst)) {
// In this case the symbol name has changed, which has caused a change to the directory name
// we are now trying to load and reconcile with what we've created. In this case, we can
// assume the "shape" of the documentation is the same, which means that within the parent
// folder of `dst` is the actual source folder that holds the old documentation, just under
// a different name. Find that folder and rename it.
std::filesystem::rename(derive_transcription_src_path(dst, node["title"]), dst);
}
return reconcile(std::move(node), _dst_root, dst / (filename + ".md"), out_emitted);
}
/**************************************************************************************************/
} // namespace hyde
/**************************************************************************************************/