diff options
author | Thomas Young <wenzhang5800@gmail.com> | 2021-04-11 21:28:32 +0800 |
---|---|---|
committer | Thomas Young <wenzhang5800@gmail.com> | 2021-04-12 22:11:19 +0800 |
commit | 5b9f4f94b83d8a02e119dcdf16eb870282a50d71 (patch) | |
tree | b0a44413df2f3c96b8da1cb4d0a7e043cafd7eab /gcc | |
parent | edc4a9549145861a2e71c552f06e53c860e39d9a (diff) | |
download | gcc-5b9f4f94b83d8a02e119dcdf16eb870282a50d71.zip gcc-5b9f4f94b83d8a02e119dcdf16eb870282a50d71.tar.gz gcc-5b9f4f94b83d8a02e119dcdf16eb870282a50d71.tar.bz2 |
add more path to collect live symbols
Addresses #363
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/analysis/rust-hir-liveness.cc | 83 | ||||
-rw-r--r-- | gcc/rust/analysis/rust-hir-liveness.h | 102 | ||||
-rw-r--r-- | gcc/rust/analysis/rust-hir-scan-deadcode.h | 2 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-expr.h | 1 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.h | 8 | ||||
-rw-r--r-- | gcc/testsuite/rust.test/compile/unused.rs | 4 |
6 files changed, 164 insertions, 36 deletions
diff --git a/gcc/rust/analysis/rust-hir-liveness.cc b/gcc/rust/analysis/rust-hir-liveness.cc index d99d8e3..e65c1eb 100644 --- a/gcc/rust/analysis/rust-hir-liveness.cc +++ b/gcc/rust/analysis/rust-hir-liveness.cc @@ -65,34 +65,30 @@ Liveness::Analysis (HIR::Crate &crate) void Liveness::go (HIR::Crate &crate) { + CrateNum crateNum = crate.get_mappings ().get_crate_num (); while (!worklist.empty ()) { HirId hirId = worklist.back (); worklist.pop_back (); scannedSymbols.emplace (hirId); - HIR::Item *item - = mappings->lookup_hir_item (crate.get_mappings ().get_crate_num (), - hirId); - if (item == nullptr) - continue; + HIR::Item *item = mappings->lookup_hir_item (crateNum, hirId); liveSymbols.emplace (hirId); - item->accept_vis (*this); + if (item != nullptr) + { + item->accept_vis (*this); + } + else + { // the item maybe inside a trait impl + HirId parent_impl_id = UNKNOWN_HIRID; + HIR::InherentImplItem *implItem + = mappings->lookup_hir_implitem (crateNum, hirId, &parent_impl_id); + if (implItem != nullptr) + implItem->accept_vis (*this); + } } } void -Liveness::visit (HIR::ExprStmtWithoutBlock &stmt) -{ - stmt.get_expr ()->accept_vis (*this); -} - -void -Liveness::visit (HIR::CallExpr &expr) -{ - expr.get_fnexpr ()->accept_vis (*this); -} - -void Liveness::visit (HIR::PathInExpression &expr) { NodeId ast_node_id = expr.get_mappings ().get_nodeid (); @@ -114,7 +110,7 @@ Liveness::visit (HIR::PathInExpression &expr) rust_error_at (expr.get_locus (), "reverse lookup failure"); return; } - if (scannedSymbols.find (ref) != scannedSymbols.end ()) + if (scannedSymbols.find (ref) == scannedSymbols.end ()) { worklist.push_back (ref); } @@ -123,18 +119,47 @@ Liveness::visit (HIR::PathInExpression &expr) } void -Liveness::visit (HIR::Function &function) +Liveness::visit (HIR::IdentifierExpr &expr) { - function.get_definition ().get ()->accept_vis (*this); -} + NodeId ast_node_id = expr.get_mappings ().get_nodeid (); -void -Liveness::visit (HIR::BlockExpr &expr) -{ - expr.iterate_stmts ([&] (HIR::Stmt *s) mutable -> bool { - s->accept_vis (*this); - return true; - }); + // then lookup the reference_node_id + NodeId ref_node_id = UNKNOWN_NODEID; + if (resolver->lookup_resolved_name (ast_node_id, &ref_node_id)) + { + // these ref_node_ids will resolve to a pattern declaration but we are + // interested in the definition that this refers to get the parent 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; + } + + if (ref_node_id == UNKNOWN_NODEID) + { + rust_error_at (expr.get_locus (), "unresolved node: %s", + expr.as_string ().c_str ()); + return; + } + + // node back to HIR + 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); } } // namespace Analysis diff --git a/gcc/rust/analysis/rust-hir-liveness.h b/gcc/rust/analysis/rust-hir-liveness.h index ae80859..aa65991 100644 --- a/gcc/rust/analysis/rust-hir-liveness.h +++ b/gcc/rust/analysis/rust-hir-liveness.h @@ -36,11 +36,105 @@ public: static std::set<HirId> Analysis (HIR::Crate &crate); void go (HIR::Crate &crate); - void visit (HIR::ExprStmtWithoutBlock &stmt) override; - void visit (HIR::CallExpr &expr) override; - void visit (HIR::Function &function) override; - void visit (HIR::BlockExpr &expr) override; void visit (HIR::PathInExpression &expr) override; + void visit (HIR::IdentifierExpr &expr) override; + + void visit (HIR::BlockExpr &expr) override + { + expr.iterate_stmts ([&] (HIR::Stmt *s) mutable -> bool { + s->accept_vis (*this); + return true; + }); + if (expr.has_expr ()) + { + expr.get_final_expr ().get ()->accept_vis (*this); + } + } + void visit (HIR::Function &function) override + { + function.get_definition ().get ()->accept_vis (*this); + } + + void visit (HIR::ExprStmtWithoutBlock &stmt) override + { + stmt.get_expr ()->accept_vis (*this); + } + + void visit (HIR::ExprStmtWithBlock &stmt) override + { + stmt.get_expr ()->accept_vis (*this); + } + + void visit (HIR::CallExpr &expr) override + { + expr.get_fnexpr ()->accept_vis (*this); + } + + void visit (HIR::ArithmeticOrLogicalExpr &expr) override + { + expr.visit_lhs (*this); + expr.visit_rhs (*this); + } + void visit (HIR::ComparisonExpr &expr) override + { + expr.get_lhs ()->accept_vis (*this); + expr.get_rhs ()->accept_vis (*this); + } + void visit (HIR::AssignmentExpr &expr) override + { + expr.visit_lhs (*this); + expr.visit_rhs (*this); + } + void visit (HIR::Method &method) override + { + method.get_definition ().get ()->accept_vis (*this); + } + void visit (HIR::TraitItemFunc &item) override + { + item.get_block_expr ()->accept_vis (*this); + } + void visit (HIR::TraitItemMethod &item) override + { + item.get_block_expr ()->accept_vis (*this); + } + void visit (HIR::InherentImpl &impl) override + { + for (auto &&item : impl.get_impl_items ()) + { + item.get ()->accept_vis (*this); + } + } + void visit (HIR::TraitImpl &impl) override + { + for (auto &&item : impl.get_impl_items ()) + { + item.get ()->accept_vis (*this); + } + } + void visit (HIR::LetStmt &stmt) override + { + if (stmt.has_init_expr ()) + { + stmt.get_init_expr ()->accept_vis (*this); + } + } + + void visit (HIR::StructExprStructFields &stct) override + { + stct.iterate ([&] (HIR::StructExprField *field) -> bool { + field->accept_vis (*this); + return true; + }); + if (stct.has_struct_base ()) + { + stct.struct_base->base_struct.get ()->accept_vis (*this); + } + } + + void visit (HIR::StructExprStructBase &stct) override + { + stct.get_struct_base ()->base_struct.get ()->accept_vis (*this); + } private: std::vector<HirId> worklist; diff --git a/gcc/rust/analysis/rust-hir-scan-deadcode.h b/gcc/rust/analysis/rust-hir-scan-deadcode.h index 3f488e8..4c42245 100644 --- a/gcc/rust/analysis/rust-hir-scan-deadcode.h +++ b/gcc/rust/analysis/rust-hir-scan-deadcode.h @@ -49,7 +49,7 @@ public: if (live_symbols.find (hirId) == live_symbols.end ()) { rust_warning_at (function.get_locus (), 0, - "function is never used: `[%s]`", + "function is never used: `%s`", function.get_function_name ().c_str ()); return; } diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index d8f13bf..1748c6a 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -1635,6 +1635,7 @@ public: {} void accept_vis (HIRVisitor &vis) override; + StructBase *get_struct_base () { return &struct_base; } protected: /* Use covariance to implement clone function as returning this object rather diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index a117333..ae22008 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -2506,6 +2506,8 @@ public: void accept_vis (HIRVisitor &vis) override; + BlockExpr *get_block_expr () { return block_expr.get (); } + protected: // Clone function implementation as (not pure) virtual method TraitItemFunc *clone_trait_item_impl () const override @@ -2652,6 +2654,8 @@ public: void accept_vis (HIRVisitor &vis) override; + BlockExpr *get_block_expr () { return block_expr.get (); } + protected: // Clone function implementation as (not pure) virtual method TraitItemMethod *clone_trait_item_impl () const override @@ -3130,6 +3134,10 @@ public: TraitImpl &operator= (TraitImpl &&other) = default; void accept_vis (HIRVisitor &vis) override; + std::vector<std::unique_ptr<TraitImplItem> > &get_impl_items () + { + return impl_items; + }; protected: /* Use covariance to implement clone function as returning this object diff --git a/gcc/testsuite/rust.test/compile/unused.rs b/gcc/testsuite/rust.test/compile/unused.rs index 1197033..c5ba0a9 100644 --- a/gcc/testsuite/rust.test/compile/unused.rs +++ b/gcc/testsuite/rust.test/compile/unused.rs @@ -1,9 +1,9 @@ -fn bar() { // {dg-warning "function is never used: `bar`"} +fn bar() { foo(); } -fn foo() { // {dg-warning "function is never used: `foo`"} +fn foo() { bar(); } |