Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2000-2009 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Created by IntelliJ IDEA. |
| 19 | * User: max |
| 20 | * Date: Jan 28, 2002 |
| 21 | * Time: 10:16:39 PM |
| 22 | * To change template for new class use |
| 23 | * Code Style | Class Templates options (Tools | IDE Options). |
| 24 | */ |
| 25 | package com.intellij.codeInspection.dataFlow; |
| 26 | |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 27 | import com.intellij.codeInspection.dataFlow.instructions.*; |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 28 | import com.intellij.codeInspection.dataFlow.value.DfaValueFactory; |
| 29 | import com.intellij.codeInspection.dataFlow.value.DfaVariableValue; |
| 30 | import com.intellij.openapi.application.ApplicationManager; |
| 31 | import com.intellij.openapi.diagnostic.Logger; |
| 32 | import com.intellij.openapi.progress.ProgressManager; |
Tor Norbye | 3a2425a5 | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 33 | import com.intellij.openapi.util.Condition; |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 34 | import com.intellij.openapi.util.Key; |
| 35 | import com.intellij.openapi.util.Pair; |
| 36 | import com.intellij.psi.*; |
| 37 | import com.intellij.psi.util.PsiTreeUtil; |
| 38 | import com.intellij.psi.util.PsiUtil; |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 39 | import com.intellij.util.containers.ContainerUtil; |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 40 | import com.intellij.util.containers.MultiMap; |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 41 | import com.intellij.util.containers.MultiMapBasedOnSet; |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 42 | import org.jetbrains.annotations.NotNull; |
| 43 | import org.jetbrains.annotations.Nullable; |
| 44 | |
| 45 | import java.util.*; |
| 46 | |
| 47 | public class DataFlowRunner { |
| 48 | private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.dataFlow.DataFlowRunner"); |
| 49 | private static final Key<Integer> TOO_EXPENSIVE_HASH = Key.create("TOO_EXPENSIVE_HASH"); |
| 50 | public static final long ourTimeLimit = 1000 * 1000 * 1000; //1 sec in nanoseconds |
| 51 | |
| 52 | private Instruction[] myInstructions; |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 53 | private final MultiMap<PsiElement, DfaMemoryState> myNestedClosures = new MultiMap<PsiElement, DfaMemoryState>(); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 54 | private DfaVariableValue[] myFields; |
Tor Norbye | 3a2425a5 | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 55 | private final DfaValueFactory myValueFactory; |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 56 | |
| 57 | // Maximum allowed attempts to process instruction. Fail as too complex to process if certain instruction |
| 58 | // is executed more than this limit times. |
| 59 | public static final int MAX_STATES_PER_BRANCH = 300; |
| 60 | |
Tor Norbye | 3a2425a5 | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 61 | protected DataFlowRunner(PsiElement block) { |
| 62 | PsiElement parentConstructor = PsiTreeUtil.findFirstParent(block, new Condition<PsiElement>() { |
| 63 | @Override |
| 64 | public boolean value(PsiElement psiElement) { |
| 65 | return psiElement instanceof PsiMethod && ((PsiMethod)psiElement).isConstructor(); |
| 66 | } |
| 67 | }); |
| 68 | myValueFactory = new DfaValueFactory(parentConstructor == null); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | public DfaValueFactory getFactory() { |
| 72 | return myValueFactory; |
| 73 | } |
| 74 | |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 75 | protected void prepareAnalysis(@NotNull PsiElement psiBlock, Iterable<DfaMemoryState> initialStates) { |
| 76 | } |
| 77 | |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 78 | @Nullable |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 79 | private Collection<DfaMemoryState> createInitialStates(@NotNull PsiElement psiBlock, InstructionVisitor visitor) { |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 80 | PsiClass containingClass = PsiTreeUtil.getParentOfType(psiBlock, PsiClass.class); |
| 81 | if (containingClass != null && PsiUtil.isLocalOrAnonymousClass(containingClass)) { |
| 82 | final PsiElement parent = containingClass.getParent(); |
| 83 | final PsiCodeBlock block = DfaPsiUtil.getTopmostBlockInSameClass(parent); |
| 84 | if ((parent instanceof PsiNewExpression || parent instanceof PsiDeclarationStatement) && block != null) { |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 85 | final RunnerResult result = analyzeMethod(block, visitor); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 86 | if (result == RunnerResult.OK) { |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 87 | final Collection<DfaMemoryState> closureStates = myNestedClosures.get(DfaPsiUtil.getTopmostBlockInSameClass(psiBlock)); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 88 | if (!closureStates.isEmpty()) { |
| 89 | return closureStates; |
| 90 | } |
| 91 | } |
| 92 | return null; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return Arrays.asList(createMemoryState()); |
| 97 | } |
| 98 | |
| 99 | public final RunnerResult analyzeMethod(@NotNull PsiElement psiBlock, InstructionVisitor visitor) { |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 100 | Collection<DfaMemoryState> initialStates = createInitialStates(psiBlock, visitor); |
| 101 | return initialStates == null ? RunnerResult.NOT_APPLICABLE : analyzeMethod(psiBlock, visitor, false, initialStates); |
Tor Norbye | 9a34524 | 2013-07-08 10:50:41 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 104 | public final RunnerResult analyzeMethod(@NotNull PsiElement psiBlock, |
| 105 | InstructionVisitor visitor, |
| 106 | boolean ignoreAssertions, |
| 107 | @NotNull Collection<DfaMemoryState> initialStates) { |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 108 | try { |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 109 | prepareAnalysis(psiBlock, initialStates); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 110 | |
Tor Norbye | 9a34524 | 2013-07-08 10:50:41 -0700 | [diff] [blame] | 111 | final ControlFlow flow = createControlFlowAnalyzer().buildControlFlow(psiBlock, ignoreAssertions); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 112 | if (flow == null) return RunnerResult.NOT_APPLICABLE; |
| 113 | |
| 114 | int endOffset = flow.getInstructionCount(); |
| 115 | myInstructions = flow.getInstructions(); |
| 116 | myFields = flow.getFields(); |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 117 | myNestedClosures.clear(); |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 118 | |
| 119 | Set<Instruction> joinInstructions = ContainerUtil.newHashSet(); |
| 120 | for (Instruction instruction : myInstructions) { |
| 121 | if (instruction instanceof GotoInstruction) { |
| 122 | joinInstructions.add(myInstructions[((GotoInstruction)instruction).getOffset()]); |
| 123 | } else if (instruction instanceof ConditionalGotoInstruction) { |
| 124 | joinInstructions.add(myInstructions[((ConditionalGotoInstruction)instruction).getOffset()]); |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 127 | |
| 128 | if (LOG.isDebugEnabled()) { |
| 129 | LOG.debug("Analyzing code block: " + psiBlock.getText()); |
| 130 | for (int i = 0; i < myInstructions.length; i++) { |
Tor Norbye | 7675366 | 2013-10-15 19:49:27 -0700 | [diff] [blame] | 131 | LOG.debug(i + ": " + myInstructions[i].toString()); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
Tor Norbye | 7675366 | 2013-10-15 19:49:27 -0700 | [diff] [blame] | 134 | //for (int i = 0; i < myInstructions.length; i++) System.out.println(i + ": " + myInstructions[i].toString()); |
| 135 | |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 136 | Integer tooExpensiveHash = psiBlock.getUserData(TOO_EXPENSIVE_HASH); |
| 137 | if (tooExpensiveHash != null && tooExpensiveHash == psiBlock.getText().hashCode()) { |
| 138 | LOG.debug("Too complex because hasn't changed since being too complex already"); |
| 139 | return RunnerResult.TOO_COMPLEX; |
| 140 | } |
| 141 | |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 142 | final StateQueue queue = new StateQueue(); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 143 | for (final DfaMemoryState initialState : initialStates) { |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 144 | queue.offer(new DfaInstructionState(myInstructions[0], initialState)); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 147 | MultiMapBasedOnSet<BranchingInstruction, DfaMemoryState> processedStates = new MultiMapBasedOnSet<BranchingInstruction, DfaMemoryState>(); |
| 148 | MultiMapBasedOnSet<BranchingInstruction, DfaMemoryState> incomingStates = new MultiMapBasedOnSet<BranchingInstruction, DfaMemoryState>(); |
| 149 | |
Tor Norbye | beca983 | 2013-09-19 12:39:22 -0700 | [diff] [blame] | 150 | WorkingTimeMeasurer measurer = new WorkingTimeMeasurer(shouldCheckTimeLimit() ? ourTimeLimit : ourTimeLimit * 42); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 151 | int count = 0; |
| 152 | while (!queue.isEmpty()) { |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 153 | for (DfaInstructionState instructionState : queue.getNextInstructionStates(joinInstructions)) { |
| 154 | if (count++ % 1024 == 0 && measurer.isTimeOver()) { |
| 155 | LOG.debug("Too complex because the analysis took too long"); |
| 156 | psiBlock.putUserData(TOO_EXPENSIVE_HASH, psiBlock.getText().hashCode()); |
| 157 | return RunnerResult.TOO_COMPLEX; |
| 158 | } |
| 159 | ProgressManager.checkCanceled(); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 160 | |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 161 | if (LOG.isDebugEnabled()) { |
| 162 | LOG.debug(instructionState.toString()); |
| 163 | } |
| 164 | //System.out.println(instructionState.toString()); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 165 | |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 166 | Instruction instruction = instructionState.getInstruction(); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 167 | |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 168 | if (instruction instanceof BranchingInstruction) { |
| 169 | BranchingInstruction branching = (BranchingInstruction)instruction; |
Tor Norbye | 32218cc | 2013-10-08 09:48:09 -0700 | [diff] [blame] | 170 | Collection<DfaMemoryState> processed = processedStates.get(branching); |
| 171 | if (processed.contains(instructionState.getMemoryState())) { |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 172 | continue; |
| 173 | } |
Tor Norbye | 32218cc | 2013-10-08 09:48:09 -0700 | [diff] [blame] | 174 | if (processed.size() > MAX_STATES_PER_BRANCH) { |
Tor Norbye | f7998d0 | 2013-09-27 10:19:19 -0700 | [diff] [blame] | 175 | LOG.debug("Too complex because too many different possible states"); |
| 176 | return RunnerResult.TOO_COMPLEX; // Too complex :( |
| 177 | } |
| 178 | processedStates.putValue(branching, instructionState.getMemoryState().createCopy()); |
| 179 | } |
| 180 | |
| 181 | DfaInstructionState[] after = acceptInstruction(visitor, instructionState); |
| 182 | for (DfaInstructionState state : after) { |
| 183 | Instruction nextInstruction = state.getInstruction(); |
| 184 | if (nextInstruction.getIndex() >= endOffset) { |
| 185 | continue; |
| 186 | } |
| 187 | if (nextInstruction instanceof BranchingInstruction) { |
| 188 | BranchingInstruction branching = (BranchingInstruction)nextInstruction; |
| 189 | if (processedStates.get(branching).contains(state.getMemoryState()) || |
| 190 | incomingStates.get(branching).contains(state.getMemoryState())) { |
| 191 | continue; |
| 192 | } |
| 193 | incomingStates.putValue(branching, state.getMemoryState().createCopy()); |
| 194 | } |
| 195 | queue.offer(state); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | psiBlock.putUserData(TOO_EXPENSIVE_HASH, null); |
| 201 | LOG.debug("Analysis ok"); |
| 202 | return RunnerResult.OK; |
| 203 | } |
| 204 | catch (ArrayIndexOutOfBoundsException e) { |
Tor Norbye | 7675366 | 2013-10-15 19:49:27 -0700 | [diff] [blame] | 205 | LOG.error(psiBlock.getText(), e); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 206 | return RunnerResult.ABORTED; |
| 207 | } |
| 208 | catch (EmptyStackException e) { |
Tor Norbye | 7675366 | 2013-10-15 19:49:27 -0700 | [diff] [blame] | 209 | LOG.error(psiBlock.getText(), e); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 210 | return RunnerResult.ABORTED; |
| 211 | } |
| 212 | } |
| 213 | |
Tor Norbye | beca983 | 2013-09-19 12:39:22 -0700 | [diff] [blame] | 214 | protected boolean shouldCheckTimeLimit() { |
| 215 | return !ApplicationManager.getApplication().isUnitTestMode(); |
| 216 | } |
| 217 | |
Tor Norbye | 3a2425a5 | 2013-11-04 10:16:08 -0800 | [diff] [blame] | 218 | protected DfaInstructionState[] acceptInstruction(InstructionVisitor visitor, DfaInstructionState instructionState) { |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 219 | Instruction instruction = instructionState.getInstruction(); |
| 220 | if (instruction instanceof MethodCallInstruction) { |
| 221 | PsiCallExpression anchor = ((MethodCallInstruction)instruction).getCallExpression(); |
| 222 | if (anchor instanceof PsiNewExpression) { |
| 223 | PsiAnonymousClass anonymousClass = ((PsiNewExpression)anchor).getAnonymousClass(); |
| 224 | if (anonymousClass != null) { |
| 225 | registerNestedClosures(instructionState, anonymousClass); |
| 226 | } |
| 227 | } |
| 228 | } |
Tor Norbye | 8668e1b | 2013-12-20 09:14:04 -0800 | [diff] [blame^] | 229 | else if (instruction instanceof LambdaInstruction) { |
| 230 | PsiLambdaExpression lambdaExpression = ((LambdaInstruction)instruction).getLambdaExpression(); |
| 231 | registerNestedClosures(instructionState, lambdaExpression); |
| 232 | } |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 233 | else if (instruction instanceof EmptyInstruction) { |
| 234 | PsiElement anchor = ((EmptyInstruction)instruction).getAnchor(); |
| 235 | if (anchor instanceof PsiDeclarationStatement) { |
| 236 | for (PsiElement element : ((PsiDeclarationStatement)anchor).getDeclaredElements()) { |
| 237 | if (element instanceof PsiClass) { |
| 238 | registerNestedClosures(instructionState, (PsiClass)element); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return instruction.accept(this, instructionState.getMemoryState(), visitor); |
| 245 | } |
| 246 | |
| 247 | private void registerNestedClosures(DfaInstructionState instructionState, PsiClass nestedClass) { |
Tor Norbye | beca983 | 2013-09-19 12:39:22 -0700 | [diff] [blame] | 248 | DfaMemoryState state = instructionState.getMemoryState(); |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 249 | for (PsiMethod method : nestedClass.getMethods()) { |
| 250 | PsiCodeBlock body = method.getBody(); |
| 251 | if (body != null) { |
Tor Norbye | beca983 | 2013-09-19 12:39:22 -0700 | [diff] [blame] | 252 | myNestedClosures.putValue(body, createClosureState(state)); |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | for (PsiClassInitializer initializer : nestedClass.getInitializers()) { |
Tor Norbye | beca983 | 2013-09-19 12:39:22 -0700 | [diff] [blame] | 256 | myNestedClosures.putValue(initializer.getBody(), createClosureState(state)); |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 257 | } |
| 258 | for (PsiField field : nestedClass.getFields()) { |
Tor Norbye | beca983 | 2013-09-19 12:39:22 -0700 | [diff] [blame] | 259 | myNestedClosures.putValue(field, createClosureState(state)); |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
Tor Norbye | 8668e1b | 2013-12-20 09:14:04 -0800 | [diff] [blame^] | 262 | |
| 263 | private void registerNestedClosures(DfaInstructionState instructionState, PsiLambdaExpression expr) { |
| 264 | DfaMemoryState state = instructionState.getMemoryState(); |
| 265 | PsiElement body = expr.getBody(); |
| 266 | if (body != null) { |
| 267 | myNestedClosures.putValue(body, createClosureState(state)); |
| 268 | } |
| 269 | } |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 270 | |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 271 | protected ControlFlowAnalyzer createControlFlowAnalyzer() { |
| 272 | return new ControlFlowAnalyzer(myValueFactory); |
| 273 | } |
| 274 | |
| 275 | protected DfaMemoryState createMemoryState() { |
| 276 | return new DfaMemoryStateImpl(myValueFactory); |
| 277 | } |
| 278 | |
| 279 | public Instruction[] getInstructions() { |
| 280 | return myInstructions; |
| 281 | } |
| 282 | |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 283 | public Instruction getInstruction(int index) { |
| 284 | return myInstructions[index]; |
| 285 | } |
| 286 | |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 287 | public DfaVariableValue[] getFields() { |
| 288 | return myFields; |
| 289 | } |
| 290 | |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 291 | public MultiMap<PsiElement, DfaMemoryState> getNestedClosures() { |
| 292 | return new MultiMap<PsiElement, DfaMemoryState>(myNestedClosures); |
| 293 | } |
| 294 | |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 295 | public Pair<Set<Instruction>,Set<Instruction>> getConstConditionalExpressions() { |
| 296 | Set<Instruction> trueSet = new HashSet<Instruction>(); |
| 297 | Set<Instruction> falseSet = new HashSet<Instruction>(); |
| 298 | |
| 299 | for (Instruction instruction : myInstructions) { |
| 300 | if (instruction instanceof BranchingInstruction) { |
| 301 | BranchingInstruction branchingInstruction = (BranchingInstruction)instruction; |
| 302 | if (branchingInstruction.getPsiAnchor() != null && branchingInstruction.isConditionConst()) { |
| 303 | if (!branchingInstruction.isTrueReachable()) { |
| 304 | falseSet.add(branchingInstruction); |
| 305 | } |
| 306 | |
| 307 | if (!branchingInstruction.isFalseReachable()) { |
| 308 | trueSet.add(branchingInstruction); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | for (Instruction instruction : myInstructions) { |
| 315 | if (instruction instanceof BranchingInstruction) { |
| 316 | BranchingInstruction branchingInstruction = (BranchingInstruction)instruction; |
| 317 | if (branchingInstruction.isTrueReachable()) { |
| 318 | falseSet.remove(branchingInstruction); |
| 319 | } |
| 320 | if (branchingInstruction.isFalseReachable()) { |
| 321 | trueSet.remove(branchingInstruction); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return Pair.create(trueSet, falseSet); |
| 327 | } |
| 328 | |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 329 | private DfaMemoryStateImpl createClosureState(DfaMemoryState memState) { |
| 330 | DfaMemoryStateImpl copy = (DfaMemoryStateImpl)memState.createCopy(); |
| 331 | copy.flushFields(getFields()); |
| 332 | Set<DfaVariableValue> vars = new HashSet<DfaVariableValue>(copy.getVariableStates().keySet()); |
| 333 | for (DfaVariableValue value : vars) { |
| 334 | copy.flushDependencies(value); |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 335 | } |
Tor Norbye | beca983 | 2013-09-19 12:39:22 -0700 | [diff] [blame] | 336 | copy.emptyStack(); |
Tor Norbye | c7f983b | 2013-09-05 16:07:26 -0700 | [diff] [blame] | 337 | return copy; |
Tor Norbye | ec3fb1e | 2013-05-31 07:45:51 -0700 | [diff] [blame] | 338 | } |
| 339 | } |