Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

code-path-analyzer.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /**
  2. * @fileoverview A class of the code path analyzer.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const assert = require("assert"),
  10. CodePath = require("./code-path"),
  11. CodePathSegment = require("./code-path-segment"),
  12. IdGenerator = require("./id-generator"),
  13. debug = require("./debug-helpers"),
  14. astUtils = require("../util/ast-utils");
  15. //------------------------------------------------------------------------------
  16. // Helpers
  17. //------------------------------------------------------------------------------
  18. /**
  19. * Checks whether or not a given node is a `case` node (not `default` node).
  20. *
  21. * @param {ASTNode} node - A `SwitchCase` node to check.
  22. * @returns {boolean} `true` if the node is a `case` node (not `default` node).
  23. */
  24. function isCaseNode(node) {
  25. return Boolean(node.test);
  26. }
  27. /**
  28. * Checks whether the given logical operator is taken into account for the code
  29. * path analysis.
  30. *
  31. * @param {string} operator - The operator found in the LogicalExpression node
  32. * @returns {boolean} `true` if the operator is "&&" or "||"
  33. */
  34. function isHandledLogicalOperator(operator) {
  35. return operator === "&&" || operator === "||";
  36. }
  37. /**
  38. * Checks whether or not a given logical expression node goes different path
  39. * between the `true` case and the `false` case.
  40. *
  41. * @param {ASTNode} node - A node to check.
  42. * @returns {boolean} `true` if the node is a test of a choice statement.
  43. */
  44. function isForkingByTrueOrFalse(node) {
  45. const parent = node.parent;
  46. switch (parent.type) {
  47. case "ConditionalExpression":
  48. case "IfStatement":
  49. case "WhileStatement":
  50. case "DoWhileStatement":
  51. case "ForStatement":
  52. return parent.test === node;
  53. case "LogicalExpression":
  54. return isHandledLogicalOperator(parent.operator);
  55. default:
  56. return false;
  57. }
  58. }
  59. /**
  60. * Gets the boolean value of a given literal node.
  61. *
  62. * This is used to detect infinity loops (e.g. `while (true) {}`).
  63. * Statements preceded by an infinity loop are unreachable if the loop didn't
  64. * have any `break` statement.
  65. *
  66. * @param {ASTNode} node - A node to get.
  67. * @returns {boolean|undefined} a boolean value if the node is a Literal node,
  68. * otherwise `undefined`.
  69. */
  70. function getBooleanValueIfSimpleConstant(node) {
  71. if (node.type === "Literal") {
  72. return Boolean(node.value);
  73. }
  74. return void 0;
  75. }
  76. /**
  77. * Checks that a given identifier node is a reference or not.
  78. *
  79. * This is used to detect the first throwable node in a `try` block.
  80. *
  81. * @param {ASTNode} node - An Identifier node to check.
  82. * @returns {boolean} `true` if the node is a reference.
  83. */
  84. function isIdentifierReference(node) {
  85. const parent = node.parent;
  86. switch (parent.type) {
  87. case "LabeledStatement":
  88. case "BreakStatement":
  89. case "ContinueStatement":
  90. case "ArrayPattern":
  91. case "RestElement":
  92. case "ImportSpecifier":
  93. case "ImportDefaultSpecifier":
  94. case "ImportNamespaceSpecifier":
  95. case "CatchClause":
  96. return false;
  97. case "FunctionDeclaration":
  98. case "FunctionExpression":
  99. case "ArrowFunctionExpression":
  100. case "ClassDeclaration":
  101. case "ClassExpression":
  102. case "VariableDeclarator":
  103. return parent.id !== node;
  104. case "Property":
  105. case "MethodDefinition":
  106. return (
  107. parent.key !== node ||
  108. parent.computed ||
  109. parent.shorthand
  110. );
  111. case "AssignmentPattern":
  112. return parent.key !== node;
  113. default:
  114. return true;
  115. }
  116. }
  117. /**
  118. * Updates the current segment with the head segment.
  119. * This is similar to local branches and tracking branches of git.
  120. *
  121. * To separate the current and the head is in order to not make useless segments.
  122. *
  123. * In this process, both "onCodePathSegmentStart" and "onCodePathSegmentEnd"
  124. * events are fired.
  125. *
  126. * @param {CodePathAnalyzer} analyzer - The instance.
  127. * @param {ASTNode} node - The current AST node.
  128. * @returns {void}
  129. */
  130. function forwardCurrentToHead(analyzer, node) {
  131. const codePath = analyzer.codePath;
  132. const state = CodePath.getState(codePath);
  133. const currentSegments = state.currentSegments;
  134. const headSegments = state.headSegments;
  135. const end = Math.max(currentSegments.length, headSegments.length);
  136. let i, currentSegment, headSegment;
  137. // Fires leaving events.
  138. for (i = 0; i < end; ++i) {
  139. currentSegment = currentSegments[i];
  140. headSegment = headSegments[i];
  141. if (currentSegment !== headSegment && currentSegment) {
  142. debug.dump(`onCodePathSegmentEnd ${currentSegment.id}`);
  143. if (currentSegment.reachable) {
  144. analyzer.emitter.emit(
  145. "onCodePathSegmentEnd",
  146. currentSegment,
  147. node
  148. );
  149. }
  150. }
  151. }
  152. // Update state.
  153. state.currentSegments = headSegments;
  154. // Fires entering events.
  155. for (i = 0; i < end; ++i) {
  156. currentSegment = currentSegments[i];
  157. headSegment = headSegments[i];
  158. if (currentSegment !== headSegment && headSegment) {
  159. debug.dump(`onCodePathSegmentStart ${headSegment.id}`);
  160. CodePathSegment.markUsed(headSegment);
  161. if (headSegment.reachable) {
  162. analyzer.emitter.emit(
  163. "onCodePathSegmentStart",
  164. headSegment,
  165. node
  166. );
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * Updates the current segment with empty.
  173. * This is called at the last of functions or the program.
  174. *
  175. * @param {CodePathAnalyzer} analyzer - The instance.
  176. * @param {ASTNode} node - The current AST node.
  177. * @returns {void}
  178. */
  179. function leaveFromCurrentSegment(analyzer, node) {
  180. const state = CodePath.getState(analyzer.codePath);
  181. const currentSegments = state.currentSegments;
  182. for (let i = 0; i < currentSegments.length; ++i) {
  183. const currentSegment = currentSegments[i];
  184. debug.dump(`onCodePathSegmentEnd ${currentSegment.id}`);
  185. if (currentSegment.reachable) {
  186. analyzer.emitter.emit(
  187. "onCodePathSegmentEnd",
  188. currentSegment,
  189. node
  190. );
  191. }
  192. }
  193. state.currentSegments = [];
  194. }
  195. /**
  196. * Updates the code path due to the position of a given node in the parent node
  197. * thereof.
  198. *
  199. * For example, if the node is `parent.consequent`, this creates a fork from the
  200. * current path.
  201. *
  202. * @param {CodePathAnalyzer} analyzer - The instance.
  203. * @param {ASTNode} node - The current AST node.
  204. * @returns {void}
  205. */
  206. function preprocess(analyzer, node) {
  207. const codePath = analyzer.codePath;
  208. const state = CodePath.getState(codePath);
  209. const parent = node.parent;
  210. switch (parent.type) {
  211. case "LogicalExpression":
  212. if (
  213. parent.right === node &&
  214. isHandledLogicalOperator(parent.operator)
  215. ) {
  216. state.makeLogicalRight();
  217. }
  218. break;
  219. case "ConditionalExpression":
  220. case "IfStatement":
  221. /*
  222. * Fork if this node is at `consequent`/`alternate`.
  223. * `popForkContext()` exists at `IfStatement:exit` and
  224. * `ConditionalExpression:exit`.
  225. */
  226. if (parent.consequent === node) {
  227. state.makeIfConsequent();
  228. } else if (parent.alternate === node) {
  229. state.makeIfAlternate();
  230. }
  231. break;
  232. case "SwitchCase":
  233. if (parent.consequent[0] === node) {
  234. state.makeSwitchCaseBody(false, !parent.test);
  235. }
  236. break;
  237. case "TryStatement":
  238. if (parent.handler === node) {
  239. state.makeCatchBlock();
  240. } else if (parent.finalizer === node) {
  241. state.makeFinallyBlock();
  242. }
  243. break;
  244. case "WhileStatement":
  245. if (parent.test === node) {
  246. state.makeWhileTest(getBooleanValueIfSimpleConstant(node));
  247. } else {
  248. assert(parent.body === node);
  249. state.makeWhileBody();
  250. }
  251. break;
  252. case "DoWhileStatement":
  253. if (parent.body === node) {
  254. state.makeDoWhileBody();
  255. } else {
  256. assert(parent.test === node);
  257. state.makeDoWhileTest(getBooleanValueIfSimpleConstant(node));
  258. }
  259. break;
  260. case "ForStatement":
  261. if (parent.test === node) {
  262. state.makeForTest(getBooleanValueIfSimpleConstant(node));
  263. } else if (parent.update === node) {
  264. state.makeForUpdate();
  265. } else if (parent.body === node) {
  266. state.makeForBody();
  267. }
  268. break;
  269. case "ForInStatement":
  270. case "ForOfStatement":
  271. if (parent.left === node) {
  272. state.makeForInOfLeft();
  273. } else if (parent.right === node) {
  274. state.makeForInOfRight();
  275. } else {
  276. assert(parent.body === node);
  277. state.makeForInOfBody();
  278. }
  279. break;
  280. case "AssignmentPattern":
  281. /*
  282. * Fork if this node is at `right`.
  283. * `left` is executed always, so it uses the current path.
  284. * `popForkContext()` exists at `AssignmentPattern:exit`.
  285. */
  286. if (parent.right === node) {
  287. state.pushForkContext();
  288. state.forkBypassPath();
  289. state.forkPath();
  290. }
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. /**
  297. * Updates the code path due to the type of a given node in entering.
  298. *
  299. * @param {CodePathAnalyzer} analyzer - The instance.
  300. * @param {ASTNode} node - The current AST node.
  301. * @returns {void}
  302. */
  303. function processCodePathToEnter(analyzer, node) {
  304. let codePath = analyzer.codePath;
  305. let state = codePath && CodePath.getState(codePath);
  306. const parent = node.parent;
  307. switch (node.type) {
  308. case "Program":
  309. case "FunctionDeclaration":
  310. case "FunctionExpression":
  311. case "ArrowFunctionExpression":
  312. if (codePath) {
  313. // Emits onCodePathSegmentStart events if updated.
  314. forwardCurrentToHead(analyzer, node);
  315. debug.dumpState(node, state, false);
  316. }
  317. // Create the code path of this scope.
  318. codePath = analyzer.codePath = new CodePath(
  319. analyzer.idGenerator.next(),
  320. codePath,
  321. analyzer.onLooped
  322. );
  323. state = CodePath.getState(codePath);
  324. // Emits onCodePathStart events.
  325. debug.dump(`onCodePathStart ${codePath.id}`);
  326. analyzer.emitter.emit("onCodePathStart", codePath, node);
  327. break;
  328. case "LogicalExpression":
  329. if (isHandledLogicalOperator(node.operator)) {
  330. state.pushChoiceContext(
  331. node.operator,
  332. isForkingByTrueOrFalse(node)
  333. );
  334. }
  335. break;
  336. case "ConditionalExpression":
  337. case "IfStatement":
  338. state.pushChoiceContext("test", false);
  339. break;
  340. case "SwitchStatement":
  341. state.pushSwitchContext(
  342. node.cases.some(isCaseNode),
  343. astUtils.getLabel(node)
  344. );
  345. break;
  346. case "TryStatement":
  347. state.pushTryContext(Boolean(node.finalizer));
  348. break;
  349. case "SwitchCase":
  350. /*
  351. * Fork if this node is after the 2st node in `cases`.
  352. * It's similar to `else` blocks.
  353. * The next `test` node is processed in this path.
  354. */
  355. if (parent.discriminant !== node && parent.cases[0] !== node) {
  356. state.forkPath();
  357. }
  358. break;
  359. case "WhileStatement":
  360. case "DoWhileStatement":
  361. case "ForStatement":
  362. case "ForInStatement":
  363. case "ForOfStatement":
  364. state.pushLoopContext(node.type, astUtils.getLabel(node));
  365. break;
  366. case "LabeledStatement":
  367. if (!astUtils.isBreakableStatement(node.body)) {
  368. state.pushBreakContext(false, node.label.name);
  369. }
  370. break;
  371. default:
  372. break;
  373. }
  374. // Emits onCodePathSegmentStart events if updated.
  375. forwardCurrentToHead(analyzer, node);
  376. debug.dumpState(node, state, false);
  377. }
  378. /**
  379. * Updates the code path due to the type of a given node in leaving.
  380. *
  381. * @param {CodePathAnalyzer} analyzer - The instance.
  382. * @param {ASTNode} node - The current AST node.
  383. * @returns {void}
  384. */
  385. function processCodePathToExit(analyzer, node) {
  386. const codePath = analyzer.codePath;
  387. const state = CodePath.getState(codePath);
  388. let dontForward = false;
  389. switch (node.type) {
  390. case "IfStatement":
  391. case "ConditionalExpression":
  392. state.popChoiceContext();
  393. break;
  394. case "LogicalExpression":
  395. if (isHandledLogicalOperator(node.operator)) {
  396. state.popChoiceContext();
  397. }
  398. break;
  399. case "SwitchStatement":
  400. state.popSwitchContext();
  401. break;
  402. case "SwitchCase":
  403. /*
  404. * This is the same as the process at the 1st `consequent` node in
  405. * `preprocess` function.
  406. * Must do if this `consequent` is empty.
  407. */
  408. if (node.consequent.length === 0) {
  409. state.makeSwitchCaseBody(true, !node.test);
  410. }
  411. if (state.forkContext.reachable) {
  412. dontForward = true;
  413. }
  414. break;
  415. case "TryStatement":
  416. state.popTryContext();
  417. break;
  418. case "BreakStatement":
  419. forwardCurrentToHead(analyzer, node);
  420. state.makeBreak(node.label && node.label.name);
  421. dontForward = true;
  422. break;
  423. case "ContinueStatement":
  424. forwardCurrentToHead(analyzer, node);
  425. state.makeContinue(node.label && node.label.name);
  426. dontForward = true;
  427. break;
  428. case "ReturnStatement":
  429. forwardCurrentToHead(analyzer, node);
  430. state.makeReturn();
  431. dontForward = true;
  432. break;
  433. case "ThrowStatement":
  434. forwardCurrentToHead(analyzer, node);
  435. state.makeThrow();
  436. dontForward = true;
  437. break;
  438. case "Identifier":
  439. if (isIdentifierReference(node)) {
  440. state.makeFirstThrowablePathInTryBlock();
  441. dontForward = true;
  442. }
  443. break;
  444. case "CallExpression":
  445. case "MemberExpression":
  446. case "NewExpression":
  447. state.makeFirstThrowablePathInTryBlock();
  448. break;
  449. case "WhileStatement":
  450. case "DoWhileStatement":
  451. case "ForStatement":
  452. case "ForInStatement":
  453. case "ForOfStatement":
  454. state.popLoopContext();
  455. break;
  456. case "AssignmentPattern":
  457. state.popForkContext();
  458. break;
  459. case "LabeledStatement":
  460. if (!astUtils.isBreakableStatement(node.body)) {
  461. state.popBreakContext();
  462. }
  463. break;
  464. default:
  465. break;
  466. }
  467. // Emits onCodePathSegmentStart events if updated.
  468. if (!dontForward) {
  469. forwardCurrentToHead(analyzer, node);
  470. }
  471. debug.dumpState(node, state, true);
  472. }
  473. /**
  474. * Updates the code path to finalize the current code path.
  475. *
  476. * @param {CodePathAnalyzer} analyzer - The instance.
  477. * @param {ASTNode} node - The current AST node.
  478. * @returns {void}
  479. */
  480. function postprocess(analyzer, node) {
  481. switch (node.type) {
  482. case "Program":
  483. case "FunctionDeclaration":
  484. case "FunctionExpression":
  485. case "ArrowFunctionExpression": {
  486. let codePath = analyzer.codePath;
  487. // Mark the current path as the final node.
  488. CodePath.getState(codePath).makeFinal();
  489. // Emits onCodePathSegmentEnd event of the current segments.
  490. leaveFromCurrentSegment(analyzer, node);
  491. // Emits onCodePathEnd event of this code path.
  492. debug.dump(`onCodePathEnd ${codePath.id}`);
  493. analyzer.emitter.emit("onCodePathEnd", codePath, node);
  494. debug.dumpDot(codePath);
  495. codePath = analyzer.codePath = analyzer.codePath.upper;
  496. if (codePath) {
  497. debug.dumpState(node, CodePath.getState(codePath), true);
  498. }
  499. break;
  500. }
  501. default:
  502. break;
  503. }
  504. }
  505. //------------------------------------------------------------------------------
  506. // Public Interface
  507. //------------------------------------------------------------------------------
  508. /**
  509. * The class to analyze code paths.
  510. * This class implements the EventGenerator interface.
  511. */
  512. class CodePathAnalyzer {
  513. /**
  514. * @param {EventGenerator} eventGenerator - An event generator to wrap.
  515. */
  516. constructor(eventGenerator) {
  517. this.original = eventGenerator;
  518. this.emitter = eventGenerator.emitter;
  519. this.codePath = null;
  520. this.idGenerator = new IdGenerator("s");
  521. this.currentNode = null;
  522. this.onLooped = this.onLooped.bind(this);
  523. }
  524. /**
  525. * Does the process to enter a given AST node.
  526. * This updates state of analysis and calls `enterNode` of the wrapped.
  527. *
  528. * @param {ASTNode} node - A node which is entering.
  529. * @returns {void}
  530. */
  531. enterNode(node) {
  532. this.currentNode = node;
  533. // Updates the code path due to node's position in its parent node.
  534. if (node.parent) {
  535. preprocess(this, node);
  536. }
  537. /*
  538. * Updates the code path.
  539. * And emits onCodePathStart/onCodePathSegmentStart events.
  540. */
  541. processCodePathToEnter(this, node);
  542. // Emits node events.
  543. this.original.enterNode(node);
  544. this.currentNode = null;
  545. }
  546. /**
  547. * Does the process to leave a given AST node.
  548. * This updates state of analysis and calls `leaveNode` of the wrapped.
  549. *
  550. * @param {ASTNode} node - A node which is leaving.
  551. * @returns {void}
  552. */
  553. leaveNode(node) {
  554. this.currentNode = node;
  555. /*
  556. * Updates the code path.
  557. * And emits onCodePathStart/onCodePathSegmentStart events.
  558. */
  559. processCodePathToExit(this, node);
  560. // Emits node events.
  561. this.original.leaveNode(node);
  562. // Emits the last onCodePathStart/onCodePathSegmentStart events.
  563. postprocess(this, node);
  564. this.currentNode = null;
  565. }
  566. /**
  567. * This is called on a code path looped.
  568. * Then this raises a looped event.
  569. *
  570. * @param {CodePathSegment} fromSegment - A segment of prev.
  571. * @param {CodePathSegment} toSegment - A segment of next.
  572. * @returns {void}
  573. */
  574. onLooped(fromSegment, toSegment) {
  575. if (fromSegment.reachable && toSegment.reachable) {
  576. debug.dump(`onCodePathSegmentLoop ${fromSegment.id} -> ${toSegment.id}`);
  577. this.emitter.emit(
  578. "onCodePathSegmentLoop",
  579. fromSegment,
  580. toSegment,
  581. this.currentNode
  582. );
  583. }
  584. }
  585. }
  586. module.exports = CodePathAnalyzer;