aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorThomas Young <wenzhang5800@gmail.com>2021-06-28 23:08:32 +0800
committerThomas Young <wenzhang5800@gmail.com>2021-06-29 21:56:23 +0800
commite2a40e08e26f9f1d4ddbdcb088a9809814e44c2a (patch)
tree7a36c243dfd8362b260304dfd3f312e1ca86ac8f /gcc
parentbab6f6bcb299ac42096783cda32b8f11da4288ca (diff)
downloadgcc-e2a40e08e26f9f1d4ddbdcb088a9809814e44c2a.zip
gcc-e2a40e08e26f9f1d4ddbdcb088a9809814e44c2a.tar.gz
gcc-e2a40e08e26f9f1d4ddbdcb088a9809814e44c2a.tar.bz2
Marking live symbol for struct construction.
Comment iterate_type_ribs of UnusedScan in order to resolve multiple struct unused report.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/lint/rust-lint-marklive.cc58
-rw-r--r--gcc/rust/lint/rust-lint-marklive.h7
-rw-r--r--gcc/rust/lint/rust-lint-scan-deadcode.h32
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-unused.h18
-rw-r--r--gcc/testsuite/rust/compile/torture/cfg_attr.rs2
-rw-r--r--gcc/testsuite/rust/compile/torture/unused1.rs1
-rw-r--r--gcc/testsuite/rust/compile/torture/unused_struct.rs8
7 files changed, 88 insertions, 38 deletions
diff --git a/gcc/rust/lint/rust-lint-marklive.cc b/gcc/rust/lint/rust-lint-marklive.cc
index 0d4963c..5cdb5d4 100644
--- a/gcc/rust/lint/rust-lint-marklive.cc
+++ b/gcc/rust/lint/rust-lint-marklive.cc
@@ -91,31 +91,39 @@ MarkLive::go (HIR::Crate &crate)
void
MarkLive::visit (HIR::PathInExpression &expr)
{
- NodeId ast_node_id = expr.get_mappings ().get_nodeid ();
- NodeId ref_node_id = UNKNOWN_NODEID;
- if (resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
- {
- Resolver::Definition def;
- if (!resolver->lookup_definition (ref_node_id, &def))
- {
- rust_error_at (expr.get_locus (),
- "unknown reference for resolved name");
- return;
- }
- ref_node_id = def.parent;
- HirId ref;
- if (!mappings->lookup_node_to_hir (expr.get_mappings ().get_crate_num (),
- ref_node_id, &ref))
- {
- rust_error_at (expr.get_locus (), "reverse lookup failure");
- return;
- }
- if (scannedSymbols.find (ref) == scannedSymbols.end ())
- {
- worklist.push_back (ref);
- }
- liveSymbols.emplace (ref);
- }
+ expr.iterate_path_segments ([&] (HIR::PathExprSegment &seg) -> bool {
+ NodeId ast_node_id = seg.get_mappings ().get_nodeid ();
+ NodeId ref_node_id = UNKNOWN_NODEID;
+ HirId ref;
+ if (resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
+ {
+ Resolver::Definition def;
+ if (!resolver->lookup_definition (ref_node_id, &def))
+ {
+ rust_error_at (seg.get_locus (),
+ "unknown reference for resolved name");
+ return false;
+ }
+ ref_node_id = def.parent;
+ }
+ else if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id))
+ {
+ return false;
+ }
+
+ if (!mappings->lookup_node_to_hir (seg.get_mappings ().get_crate_num (),
+ ref_node_id, &ref))
+ {
+ rust_error_at (seg.get_locus (), "reverse lookup failure");
+ return false;
+ }
+ if (scannedSymbols.find (ref) == scannedSymbols.end ())
+ {
+ worklist.push_back (ref);
+ }
+ liveSymbols.emplace (ref);
+ return true;
+ });
}
void
diff --git a/gcc/rust/lint/rust-lint-marklive.h b/gcc/rust/lint/rust-lint-marklive.h
index d34587d..673639a 100644
--- a/gcc/rust/lint/rust-lint-marklive.h
+++ b/gcc/rust/lint/rust-lint-marklive.h
@@ -201,12 +201,19 @@ public:
}
}
+ void visit (HIR::StructExprStruct &stct) override
+ {
+ stct.get_struct_name ().accept_vis (*this);
+ }
+
void visit (HIR::StructExprStructFields &stct) override
{
stct.iterate ([&] (HIR::StructExprField *field) -> bool {
field->accept_vis (*this);
return true;
});
+
+ stct.get_struct_name ().accept_vis (*this);
if (stct.has_struct_base ())
{
stct.struct_base->base_struct.get ()->accept_vis (*this);
diff --git a/gcc/rust/lint/rust-lint-scan-deadcode.h b/gcc/rust/lint/rust-lint-scan-deadcode.h
index 264b753..1dee18a 100644
--- a/gcc/rust/lint/rust-lint-scan-deadcode.h
+++ b/gcc/rust/lint/rust-lint-scan-deadcode.h
@@ -46,20 +46,34 @@ public:
void visit (HIR::Function &function) override
{
HirId hirId = function.get_mappings ().get_hirid ();
- if (live_symbols.find (hirId) == live_symbols.end ())
- {
- rust_warning_at (function.get_locus (), 0,
- "function is never used: %<%s%>",
- function.get_function_name ().c_str ());
- return;
- }
+ warning (hirId, function.get_locus (), "function",
+ function.get_function_name (), "used");
+ }
+
+ void visit (HIR::StructStruct &stct) override
+ {
+ HirId hirId = stct.get_mappings ().get_hirid ();
+ warning (hirId, stct.get_locus (), "struct", stct.get_identifier (),
+ "constructed");
}
private:
std::set<HirId> live_symbols;
- // std::set<HirId> dead_codes;
+ Resolver::Resolver *resolver;
- ScanDeadcode (std::set<HirId> &live_symbols) : live_symbols (live_symbols){};
+ ScanDeadcode (std::set<HirId> &live_symbols)
+ : live_symbols (live_symbols), resolver (Resolver::Resolver::get ()){};
+
+ void warning (HirId hirId, Location loc, const std::string &span,
+ const std::string &name, const std::string &participle) const
+ {
+ if (live_symbols.find (hirId) == live_symbols.end ())
+ {
+ rust_warning_at (loc, 0, "%s is never %s: %<%s%>", span.c_str (),
+ participle.c_str (), name.c_str ());
+ return;
+ }
+ }
};
} // namespace Analysis
diff --git a/gcc/rust/resolve/rust-ast-resolve-unused.h b/gcc/rust/resolve/rust-ast-resolve-unused.h
index 432776c..f60093c 100644
--- a/gcc/rust/resolve/rust-ast-resolve-unused.h
+++ b/gcc/rust/resolve/rust-ast-resolve-unused.h
@@ -19,6 +19,7 @@
#ifndef RUST_AST_RESOLVE_UNUSED_H
#define RUST_AST_RESOLVE_UNUSED_H
+#include "rust-hir-map.h"
#include "rust-ast-resolve-base.h"
namespace Rust {
@@ -35,8 +36,20 @@ public:
bool ok = r->lookup_canonical_path (decl_node_id, &ident);
rust_assert (ok);
+ Analysis::Mappings *map = Analysis::Mappings::get ();
+ HirId decl_hir_id;
+ // ScanUnused is conflicting with the dead code analysis here on types and
+ // functions. So just ignoring the warnings of item which will be handled
+ // by dead code analysis.
+ HIR::Item *found_item = nullptr;
+ if (map->lookup_node_to_hir (r->get_crate_num (), decl_node_id,
+ &decl_hir_id))
+ {
+ found_item = map->lookup_hir_item (r->get_crate_num (), decl_hir_id);
+ }
+
if (!r->have_references_for_node (decl_node_id)
- && ident.get ().at (0) != '_')
+ && ident.get ().at (0) != '_' && !found_item)
{
rust_warning_at (locus, 0, "unused name '%s'", ident.get ().c_str ());
}
@@ -48,7 +61,8 @@ public:
{
auto resolver = Resolver::get ();
resolver->iterate_name_ribs ([&] (Rib *r) -> void { ScanRib (r); });
- resolver->iterate_type_ribs ([&] (Rib *r) -> void { ScanRib (r); });
+ // ScanUnused is conflicting with the dead code analysis here on types.
+ // resolver->iterate_type_ribs ([&] (Rib *r) -> void { ScanRib (r); });
resolver->iterate_label_ribs ([&] (Rib *r) -> void { ScanRib (r); });
}
};
diff --git a/gcc/testsuite/rust/compile/torture/cfg_attr.rs b/gcc/testsuite/rust/compile/torture/cfg_attr.rs
index 1915f34..885221f 100644
--- a/gcc/testsuite/rust/compile/torture/cfg_attr.rs
+++ b/gcc/testsuite/rust/compile/torture/cfg_attr.rs
@@ -2,7 +2,7 @@ use std::env; // Add one line so gccrs doesn't believe we're parsing a shebang
#[cfg_attr(feature = "somefeature", attribute = "someattr")]
struct Feature;
-// { dg-warning "unused name" "" { target *-*-* } .-1 }
+// { dg-warning "struct is never constructed" "" { target *-*-* } .-1 }
fn main() {
}
diff --git a/gcc/testsuite/rust/compile/torture/unused1.rs b/gcc/testsuite/rust/compile/torture/unused1.rs
index 74297e0..db7eb8f 100644
--- a/gcc/testsuite/rust/compile/torture/unused1.rs
+++ b/gcc/testsuite/rust/compile/torture/unused1.rs
@@ -4,7 +4,6 @@ fn test() -> i32 {
fn unused() -> i32 {
// { dg-warning "function is never used: 'unused'" "" { target *-*-* } .-1 }
- // { dg-warning "unused name" "" { target *-*-* } .-2 }
2
}
diff --git a/gcc/testsuite/rust/compile/torture/unused_struct.rs b/gcc/testsuite/rust/compile/torture/unused_struct.rs
new file mode 100644
index 0000000..3a20d6b
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/unused_struct.rs
@@ -0,0 +1,8 @@
+struct Foo {
+// { dg-warning "struct is never constructed" "" { target *-*-* } .-1 }
+ one: i32,
+ two: i32
+}
+
+fn main() {
+} \ No newline at end of file