-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathitems.rs
250 lines (241 loc) · 10.2 KB
/
items.rs
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
use crate::source_analysis::prelude::*;
use syn::*;
impl SourceAnalysis {
pub(crate) fn process_items(&mut self, items: &[Item], ctx: &Context) -> SubResult {
let mut res = SubResult::Ok;
for item in items.iter() {
match item {
Item::ExternCrate(i) => {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(i);
}
Item::Use(i) => {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(i);
}
Item::Mod(i) => self.visit_mod(i, ctx),
Item::Fn(i) => self.visit_fn(i, ctx, false),
Item::Struct(i) => {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(i);
}
Item::Enum(i) => {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(i);
}
Item::Union(i) => {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(i);
}
Item::Trait(i) => self.visit_trait(i, ctx),
Item::Impl(i) => self.visit_impl(i, ctx),
Item::Macro(ref i) => {
if self.visit_macro_call(&i.mac, ctx).is_unreachable() {
res = SubResult::Unreachable;
}
}
Item::Const(c) => {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(c);
}
_ => {}
}
}
res
}
fn visit_mod(&mut self, module: &ItemMod, ctx: &Context) {
let _guard = ctx.push_to_symbol_stack(module.ident.to_string());
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(module.mod_token);
let check_insides = self.check_attr_list(&module.attrs, ctx);
if check_insides {
if let Some((_, ref items)) = module.content {
self.process_items(items, ctx);
}
} else {
if let Some((ref braces, _)) = module.content {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_span(braces.span.join());
}
// Get the file or directory name of the module
let mut p = if let Some(parent) = ctx.file.parent() {
parent.join(module.ident.to_string())
} else {
PathBuf::from(module.ident.to_string())
};
if !p.exists() {
p.set_extension("rs");
}
ctx.ignore_mods.borrow_mut().insert(p);
}
}
fn visit_fn(&mut self, func: &ItemFn, ctx: &Context, force_cover: bool) {
let _guard = ctx.push_to_symbol_stack(func.sig.ident.to_string());
{
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
let span = func.span();
analysis.functions.insert(
ctx.get_qualified_name(),
(func.sig.span().start().line, span.end().line),
);
}
let mut test_func = false;
let mut ignored_attr = false;
let mut is_inline = false;
let mut ignore_span = false;
let is_generic = is_sig_generic(&func.sig);
for attr in &func.attrs {
let id = attr.path();
if id.is_ident("test") || id.segments.last().is_some_and(|seg| seg.ident == "test") {
test_func = true;
} else if id.is_ident("derive") {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_span(attr.span());
} else if id.is_ident("inline") {
is_inline = true;
} else if id.is_ident("ignore") {
ignored_attr = true;
} else if check_cfg_attr(&attr.meta) {
ignore_span = true;
break;
}
}
if ignore_span
|| (test_func && !ctx.config.include_tests())
|| (ignored_attr && !ctx.config.run_ignored)
{
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(func);
} else {
if is_inline || is_generic || force_cover {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
// We need to force cover!
analysis.cover_span(func.block.span(), Some(ctx.file_contents));
}
if self
.process_statements(&func.block.stmts, ctx)
.is_unreachable()
{
// if the whole body of the function is unreachable, that means the function itself
// cannot be called, so is unreachable as a whole
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(func);
return;
}
self.visit_generics(&func.sig.generics, ctx);
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
let line_number = func.sig.fn_token.span().start().line;
let mut start_line = line_number;
for attr in &func.attrs {
start_line = start_line.min(attr.span().start().line);
}
if start_line < line_number {
analysis.add_to_ignore(start_line..line_number);
}
analysis.ignore.remove(&Lines::Line(line_number));
// Ignore multiple lines of fn decl
let decl_start = func.sig.fn_token.span().start().line + 1;
let stmts_start = func.block.span().start().line;
let lines = decl_start..=stmts_start;
analysis.add_to_ignore(lines);
}
}
fn visit_trait(&mut self, trait_item: &ItemTrait, ctx: &Context) {
let _guard = ctx.push_to_symbol_stack(trait_item.ident.to_string());
let check_cover = self.check_attr_list(&trait_item.attrs, ctx);
if check_cover {
for item in &trait_item.items {
if let TraitItem::Fn(ref i) = *item {
if self.check_attr_list(&i.attrs, ctx) {
let item = i.clone();
if let Some(block) = item.default {
let item_fn = ItemFn {
attrs: item.attrs,
// Trait functions inherit visibility from the trait
vis: trait_item.vis.clone(),
sig: item.sig,
block: Box::new(block),
};
// We visit the function and force cover it
self.visit_fn(&item_fn, ctx, true);
} else {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(i);
}
} else {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(i);
}
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
for a in &i.attrs {
analysis.ignore_tokens(a);
}
}
}
self.visit_generics(&trait_item.generics, ctx);
} else {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(trait_item);
}
}
fn visit_impl(&mut self, impl_blk: &ItemImpl, ctx: &Context) {
let self_ty_name = impl_blk
.self_ty
.to_token_stream()
.to_string()
.replace(' ', "");
let _guard = match &impl_blk.trait_ {
Some((_, path, _)) => {
let trait_name = path
.segments
.last()
.map(|x| x.ident.to_string())
.unwrap_or_else(|| path.to_token_stream().to_string());
let name = format!("<impl {} for {}>", trait_name, self_ty_name);
ctx.push_to_symbol_stack(name)
}
None => ctx.push_to_symbol_stack(self_ty_name),
};
let check_cover = self.check_attr_list(&impl_blk.attrs, ctx);
if check_cover {
for item in &impl_blk.items {
match *item {
ImplItem::Fn(ref i) => {
let item = i.clone();
let item_fn = ItemFn {
attrs: item.attrs,
vis: item.vis,
sig: item.sig,
block: Box::new(item.block),
};
// If the impl is on a generic, we need to force cover
let force_cover = !impl_blk.generics.params.is_empty();
self.visit_fn(&item_fn, ctx, force_cover);
}
ImplItem::Type(_) => {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_span(item.span());
}
_ => {}
}
}
self.visit_generics(&impl_blk.generics, ctx);
} else {
let analysis = self.get_line_analysis(ctx.file.to_path_buf());
analysis.ignore_tokens(impl_blk);
}
}
}
fn has_generic_arg<'a>(args: impl Iterator<Item = &'a FnArg>) -> bool {
for arg in args {
if let FnArg::Typed(pat) = arg {
if matches!(*pat.ty, Type::ImplTrait(_)) {
return true;
}
}
}
false
}
fn is_sig_generic(sig: &Signature) -> bool {
!sig.generics.params.is_empty() || has_generic_arg(sig.inputs.iter())
}