Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use solidity analyzer #3052

Merged
merged 3 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/hardhat-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
"@ethereumjs/vm": "^5.9.0",
"@ethersproject/abi": "^5.1.2",
"@metamask/eth-sig-util": "^4.0.0",
"@nomicfoundation/solidity-analyzer": "^0.0.3",
"@sentry/node": "^5.18.1",
"@solidity-parser/parser": "^0.14.2",
fvictorio marked this conversation as resolved.
Show resolved Hide resolved
"@types/bn.js": "^5.1.0",
"@types/lru-cache": "^5.1.0",
"abort-controller": "^3.0.0",
Expand Down
3 changes: 0 additions & 3 deletions packages/hardhat-core/src/internal/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
} from "../util/global-dir";
import { getPackageJson, PackageJson } from "../util/packageInfo";

import { applyWorkaround } from "../util/antlr-prototype-pollution-workaround";
import { saveFlamegraph } from "../core/flamegraph";
import { Analytics } from "./analytics";
import { ArgumentsParser } from "./ArgumentsParser";
Expand All @@ -46,8 +45,6 @@ import {

const log = debug("hardhat:core:cli");

applyWorkaround();

const ANALYTICS_SLOW_TASK_THRESHOLD = 300;

async function printVersionMessage(packageJson: PackageJson) {
Expand Down
3 changes: 0 additions & 3 deletions packages/hardhat-core/src/internal/lib/hardhat-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import { ERRORS } from "../core/errors-list";
import { getEnvHardhatArguments } from "../core/params/env-variables";
import { HARDHAT_PARAM_DEFINITIONS } from "../core/params/hardhat-params";
import { Environment } from "../core/runtime-environment";
import { applyWorkaround } from "../util/antlr-prototype-pollution-workaround";

let ctx: HardhatContext;
let env: HardhatRuntimeEnvironment;

applyWorkaround();

if (HardhatContext.isCreated()) {
ctx = HardhatContext.getHardhatContext();

Expand Down
74 changes: 4 additions & 70 deletions packages/hardhat-core/src/internal/solidity/parse.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import debug from "debug";
import type SolidityAnalyzerT from "@nomicfoundation/solidity-analyzer";

import { SolidityFilesCache } from "../../builtin-tasks/utils/solidity-files-cache";

const log = debug("hardhat:core:solidity:imports");

interface ParsedData {
imports: string[];
versionPragmas: string[];
Expand All @@ -29,34 +27,9 @@ export class Parser {
return cacheResult;
}

let result;
try {
const parser = require("@solidity-parser/parser");
const ast = parser.parse(fileContent, { tolerant: true });

const imports: string[] = [];
const versionPragmas: string[] = [];

parser.visit(ast, {
ImportDirective: (node: { path: string }) => imports.push(node.path),
PragmaDirective: (node: { name: string; value: string }) => {
if (node.name === "solidity") {
versionPragmas.push(node.value);
}
},
});

result = { imports, versionPragmas };
} catch (error) {
log(
"Failed to parse Solidity file to extract its imports, using regex fallback\n",
error
);
result = {
imports: findImportsWithRegexps(fileContent),
versionPragmas: findVersionPragmasWithRegexps(fileContent),
};
}
const { analyze } =
require("@nomicfoundation/solidity-analyzer") as typeof SolidityAnalyzerT;
const result = analyze(fileContent);

this._cache.set(contentHash, result);

Expand Down Expand Up @@ -94,42 +67,3 @@ export class Parser {
return { imports, versionPragmas };
}
}

function findImportsWithRegexps(fileContent: string): string[] {
const importsRegexp: RegExp =
/import\s+(?:(?:"([^;]*)"|'([^;]*)')(?:;|\s+as\s+[^;]*;)|.+from\s+(?:"(.*)"|'(.*)');)/g;

let imports: string[] = [];
let result: RegExpExecArray | null;

while (true) {
result = importsRegexp.exec(fileContent);
if (result === null) {
return imports;
}

imports = [
...imports,
...result.slice(1).filter((m: any) => m !== undefined),
];
}
}

function findVersionPragmasWithRegexps(fileContent: string): string[] {
const versionPragmasRegexp: RegExp = /pragma\s+solidity\s+(.+?);/g;

let versionPragmas: string[] = [];
let result: RegExpExecArray | null;

while (true) {
result = versionPragmasRegexp.exec(fileContent);
if (result === null) {
return versionPragmas;
}

versionPragmas = [
...versionPragmas,
...result.slice(1).filter((m: any) => m !== undefined),
];
}
}

This file was deleted.

3 changes: 0 additions & 3 deletions packages/hardhat-core/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import {
disableReplWriterShowProxy,
isNodeCalledWithoutAScript,
} from "./internal/util/console";
import { applyWorkaround } from "./internal/util/antlr-prototype-pollution-workaround";

applyWorkaround();

if (!HardhatContext.isCreated()) {
require("source-map-support/register");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pragma solidity ^0.5.1
pragma solidity ^0.5.1;
import "./B.sol";
contract A {}
contract A {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pragma solidity ^0.5.1
pragma solidity ^0.5.1;
import "./C.sol";
contract B {}
contract B {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pragma solidity ^0.5.1
contract C {};
pragma solidity ^0.5.1;
contract C {};
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pragma solidity ^0.5.1
contract C {};
pragma solidity ^0.5.1;
contract C {};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pragma solidity ^0.5.1
pragma solidity ^0.5.1;
import "./B.sol";
contract A {}
contract A {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pragma solidity ^0.5.1
pragma solidity ^0.5.1;
import "./C.sol";
contract B {}
contract B {}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pragma solidity ^0.5.1
contract C {};
pragma solidity ^0.5.1;
contract C {};
14 changes: 7 additions & 7 deletions packages/hardhat-core/test/internal/solidity/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import "lib/asd.sol";
const parser = new Parser();
const { imports } = parser.parse(
`
import * as from "./asd.sol";
import * as asd from "./asd.sol";
pragma experimental v0.5.0;
import * as from "lib/asd.sol";
import * as libAsd from "lib/asd.sol";
`,
"",
""
Expand All @@ -40,7 +40,7 @@ import * as from "lib/asd.sol";
`
import {symbol1} from "./asd.sol";
pragma experimental v0.5.0;
import {symbol1, symbol2} as from "lib/asd.sol";
import {symbol1, symbol2} from "lib/asd.sol";
`,
"",
""
Expand All @@ -53,9 +53,9 @@ import {symbol1, symbol2} as from "lib/asd.sol";
const parser = new Parser();
const { imports } = parser.parse(
`
import {symbol1 as s1} as from "./asd.sol";
import {symbol1 as s1} from "./asd.sol";
pragma experimental v0.5.0;
import {symbol1 as s1, symbol2} as from "lib/asd.sol";
import {symbol1 as s1, symbol2} from "lib/asd.sol";
`,
"",
""
Expand All @@ -68,11 +68,11 @@ import {symbol1 as s1, symbol2} as from "lib/asd.sol";
const parser = new Parser();
const { imports } = parser.parse(
`
asd
asd;
import "./asd.sol";
fgh {;

(
(}

import "./1.sol";
address a,
Expand Down
68 changes: 67 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,72 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@nomicfoundation/solidity-analyzer-darwin-arm64@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-arm64/-/solidity-analyzer-darwin-arm64-0.0.3.tgz#1d49e4ac028831a3011a9f3dca60bd1963185342"
integrity sha512-W+bIiNiZmiy+MTYFZn3nwjyPUO6wfWJ0lnXx2zZrM8xExKObMrhCh50yy8pQING24mHfpPFCn89wEB/iG7vZDw==

"@nomicfoundation/solidity-analyzer-darwin-x64@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-x64/-/solidity-analyzer-darwin-x64-0.0.3.tgz#c0fccecc5506ff5466225e41e65691abafef3dbe"
integrity sha512-HuJd1K+2MgmFIYEpx46uzwEFjvzKAI765mmoMxy4K+Aqq1p+q7hHRlsFU2kx3NB8InwotkkIq3A5FLU1sI1WDw==

"@nomicfoundation/solidity-analyzer-freebsd-x64@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-freebsd-x64/-/solidity-analyzer-freebsd-x64-0.0.3.tgz#8261d033f7172b347490cd005931ef8168ab4d73"
integrity sha512-2cR8JNy23jZaO/vZrsAnWCsO73asU7ylrHIe0fEsXbZYqBP9sMr+/+xP3CELDHJxUbzBY8zqGvQt1ULpyrG+Kw==

"@nomicfoundation/solidity-analyzer-linux-arm64-gnu@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-gnu/-/solidity-analyzer-linux-arm64-gnu-0.0.3.tgz#1ba64b1d76425f8953dedc6367bd7dd46f31dfc5"
integrity sha512-Eyv50EfYbFthoOb0I1568p+eqHGLwEUhYGOxcRNywtlTE9nj+c+MT1LA53HnxD9GsboH4YtOOmJOulrjG7KtbA==

"@nomicfoundation/solidity-analyzer-linux-arm64-musl@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-arm64-musl/-/solidity-analyzer-linux-arm64-musl-0.0.3.tgz#8d864c49b55e683f7e3b5cce9d10b628797280ac"
integrity sha512-V8grDqI+ivNrgwEt2HFdlwqV2/EQbYAdj3hbOvjrA8Qv+nq4h9jhQUxFpegYMDtpU8URJmNNlXgtfucSrAQwtQ==

"@nomicfoundation/solidity-analyzer-linux-x64-gnu@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-gnu/-/solidity-analyzer-linux-x64-gnu-0.0.3.tgz#16e769500cf1a8bb42ab9498cee3b93c30f78295"
integrity sha512-uRfVDlxtwT1vIy7MAExWAkRD4r9M79zMG7S09mCrWUn58DbLs7UFl+dZXBX0/8FTGYWHhOT/1Etw1ZpAf5DTrg==

"@nomicfoundation/solidity-analyzer-linux-x64-musl@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-linux-x64-musl/-/solidity-analyzer-linux-x64-musl-0.0.3.tgz#75f4e1a25526d54c506e4eba63b3d698b6255b8f"
integrity sha512-8HPwYdLbhcPpSwsE0yiU/aZkXV43vlXT2ycH+XlOjWOnLfH8C41z0njK8DHRtEFnp4OVN6E7E5lHBBKDZXCliA==

"@nomicfoundation/solidity-analyzer-win32-arm64-msvc@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-arm64-msvc/-/solidity-analyzer-win32-arm64-msvc-0.0.3.tgz#ef6e20cfad5eedfdb145cc34a44501644cd7d015"
integrity sha512-5WWcT6ZNvfCuxjlpZOY7tdvOqT1kIQYlDF9Q42wMpZ5aTm4PvjdCmFDDmmTvyXEBJ4WTVmY5dWNWaxy8h/E28g==

"@nomicfoundation/solidity-analyzer-win32-ia32-msvc@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-ia32-msvc/-/solidity-analyzer-win32-ia32-msvc-0.0.3.tgz#98c4e3af9cee68896220fa7e270aefdf7fc89c7b"
integrity sha512-P/LWGZwWkyjSwkzq6skvS2wRc3gabzAbk6Akqs1/Iiuggql2CqdLBkcYWL5Xfv3haynhL+2jlNkak+v2BTZI4A==

"@nomicfoundation/solidity-analyzer-win32-x64-msvc@0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-win32-x64-msvc/-/solidity-analyzer-win32-x64-msvc-0.0.3.tgz#12da288e7ef17ec14848f19c1e8561fed20d231d"
integrity sha512-4AcTtLZG1s/S5mYAIr/sdzywdNwJpOcdStGF3QMBzEt+cGn3MchMaS9b1gyhb2KKM2c39SmPF5fUuWq1oBSQZQ==

"@nomicfoundation/solidity-analyzer@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer/-/solidity-analyzer-0.0.3.tgz#d1029f872e66cb1082503b02cc8b0be12f8dd95e"
integrity sha512-VFMiOQvsw7nx5bFmrmVp2Q9rhIjw2AFST4DYvWVVO9PMHPE23BY2+kyfrQ4J3xCMFC8fcBbGLt7l4q7m1SlTqg==
optionalDependencies:
"@nomicfoundation/solidity-analyzer-darwin-arm64" "0.0.3"
"@nomicfoundation/solidity-analyzer-darwin-x64" "0.0.3"
"@nomicfoundation/solidity-analyzer-freebsd-x64" "0.0.3"
"@nomicfoundation/solidity-analyzer-linux-arm64-gnu" "0.0.3"
"@nomicfoundation/solidity-analyzer-linux-arm64-musl" "0.0.3"
"@nomicfoundation/solidity-analyzer-linux-x64-gnu" "0.0.3"
"@nomicfoundation/solidity-analyzer-linux-x64-musl" "0.0.3"
"@nomicfoundation/solidity-analyzer-win32-arm64-msvc" "0.0.3"
"@nomicfoundation/solidity-analyzer-win32-ia32-msvc" "0.0.3"
"@nomicfoundation/solidity-analyzer-win32-x64-msvc" "0.0.3"

"@nomiclabs/truffle-contract@^4.2.23":
version "4.5.10"
resolved "https://registry.yarnpkg.com/@nomiclabs/truffle-contract/-/truffle-contract-4.5.10.tgz#52adcca1068647e1c2b44bf0e6a89fc4ad7f9213"
Expand Down Expand Up @@ -1061,7 +1127,7 @@
resolved "https://registry.yarnpkg.com/@sinonjs/text-encoding/-/text-encoding-0.7.1.tgz#8da5c6530915653f3a1f38fd5f101d8c3f8079c5"
integrity sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ==

"@solidity-parser/parser@^0.14.0", "@solidity-parser/parser@^0.14.2":
"@solidity-parser/parser@^0.14.0":
version "0.14.2"
resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.14.2.tgz#2d8f2bddb217621df882ceeae7d7b42ae8664db3"
integrity sha512-10cr0s+MtRtqjEw0WFJrm2rwULN30xx7btd/v9cmqME2617/2M5MbHDkFIGIGTa7lwNw4bN9mVGfhlLzrYw8pA==
Expand Down