Perform a cycle check on the <use> target up front

Rather than deferring the check until ExpandUseElementsInShadowTree
(which will fail if the initial target is an ancestor of the <use>),
just check for cycles directly in BuildShadowAndInstanceTree(), and
don't build the initial shadow tree if the cycle check fails.
Stop cleaning up the partial tree after ExpandUseElementsInShadowTree(),
and adjust HasCycleUseReferencing such that it detects cycles earlier
(not requiring a clone into the shadow tree before a cycle can be

Behavior-wise this means that we now don't clone a target subtree if it
will create a cycle. We will however abort cloning of nested <use> after
a cycle is detected.

Bug: 397525
Cq-Include-Trybots: luci.chromium.try:linux_layout_tests_slimming_paint_v2;master.tryserver.blink:linux_trusty_blink_rel
Change-Id: I1b2c22bacb63d788509ec7984ac35357b84a3020
Commit-Queue: Fredrik Söderquist <>
Reviewed-by: Philip Rogers <>
Cr-Commit-Position: refs/heads/master@{#596652}
35 files changed