Skip to content

Commit

Permalink
All semver comparisons now ensure it's safe to do so (#3562)
Browse files Browse the repository at this point in the history
* All semver comparisons now ensure it's safe to do so

* Oops, use correct class

* Remove leftover import

* Leave the Zig fixes for another PR

* Fix linter

* Fix test issues

* Make linter happy, yet again

* What?

* Address PR review
  • Loading branch information
RubenRBS committed Apr 26, 2022
1 parent e3b9039 commit 4965bd6
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 117 deletions.
2 changes: 1 addition & 1 deletion lib/compilers/dart.js
Expand Up @@ -60,7 +60,7 @@ export class DartCompiler extends BaseCompiler {
filters.binary = true;

const dartCompileIntroduction = '2.10.0';
if (Semver.lt(this.compiler.semver, dartCompileIntroduction)) {
if (Semver.lt(utils.asSafeVer(this.compiler.semver), dartCompileIntroduction, true)) {
return [
'-k', 'aot',
'-o', this.filename(outputFilename),
Expand Down
2 changes: 1 addition & 1 deletion lib/compilers/ldc.js
Expand Up @@ -68,7 +68,7 @@ export class LDCCompiler extends BaseCompiler {
couldSupportASTDump(version) {
const versionRegex = /\((\d\.\d+)\.\d+/;
const versionMatch = versionRegex.exec(version);
return versionMatch ? semverParser.compare(versionMatch[1] + '.0', '1.4.0') >= 0 : false;
return versionMatch ? semverParser.compare(versionMatch[1] + '.0', '1.4.0', true) >= 0 : false;
}

async generateAST(inputFilename, options) {
Expand Down
6 changes: 5 additions & 1 deletion lib/compilers/scala.js
Expand Up @@ -22,6 +22,10 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import Semver from 'semver';

import {asSafeVer} from '../utils';

import { ScalaParser } from './argument-parsers';
import { JavaCompiler } from './java';

Expand Down Expand Up @@ -71,7 +75,7 @@ export class ScalaCompiler extends JavaCompiler {
'-deprecation',
];

return this.compiler.semver.startsWith('3') ? scala3Opts : scala2Opts;
return Semver.gte(asSafeVer(this.compiler.semver), '3.0.0', true) ? scala3Opts : scala2Opts;
}

getArgumentParser() {
Expand Down
16 changes: 10 additions & 6 deletions lib/compilers/solidity.js
Expand Up @@ -25,7 +25,10 @@
import * as fs from 'fs';
import path from 'path';

import Semver from 'semver';

import {BaseCompiler} from '../base-compiler';
import {asSafeVer} from '../utils';

import {ClangParser} from './argument-parsers';

Expand All @@ -46,7 +49,9 @@ export class SolidityCompiler extends BaseCompiler {
return [
// We use --combined-json instead of `--asm-json` to have compacted json
'--combined-json',
this.compiler.semver.split('.')[1] < 8 ? 'asm,ast' : 'asm,ast,generated-sources,generated-sources-runtime',
Semver.lt(asSafeVer(this.compiler.semver), '0.8.0', true)
? 'asm,ast'
: 'asm,ast,generated-sources,generated-sources-runtime',
'-o',
'contracts',
];
Expand Down Expand Up @@ -78,9 +83,8 @@ export class SolidityCompiler extends BaseCompiler {
}
return line;
});

let compilerVersion = {};
[compilerVersion.major, compilerVersion.minor, compilerVersion.patch] = this.compiler.semver.split('.');
const hasOldJSONLayout = Semver.lt(asSafeVer(this.compiler.semver), '0.8.0', true);
const hasGeneratedSources = Semver.gte(asSafeVer(this.compiler.semver), '0.8.0', true);

const asm = JSON.parse(result.asm);
return {
Expand All @@ -97,7 +101,7 @@ export class SolidityCompiler extends BaseCompiler {
// range of characters belongs to each function.
let contractFunctions;
// the layout of this JSON has changed between versions...
if (compilerVersion.major === 0 && compilerVersion.minor < 8) {
if (hasOldJSONLayout) {
contractFunctions = asm.sources[sourceName].AST.children
.find(node => {
return node.name === 'ContractDefinition' && node.attributes.name === contractName;
Expand Down Expand Up @@ -186,7 +190,7 @@ export class SolidityCompiler extends BaseCompiler {
// there are two sets of generated sources, one for the code which deploys
// the contract (i.e. the constructor) 'generated-sources', and the code
// which is deployed and stored on-chain 'generated-sources-runtime'
const hasGeneratedSources = compilerVersion.major > 0 || compilerVersion.minor > 7;

const generatedSources = hasGeneratedSources
? processGeneratedSources(data['generated-sources'])
: {};
Expand Down
68 changes: 39 additions & 29 deletions lib/compilers/zig.js
Expand Up @@ -27,18 +27,22 @@ import path from 'path';
import Semver from 'semver';
import _ from 'underscore';

import { BaseCompiler } from '../base-compiler';
import {BaseCompiler} from '../base-compiler';
import {asSafeVer} from '../utils';

export class ZigCompiler extends BaseCompiler {
static get key() { return 'zig'; }
static get key() {
return 'zig';
}

constructor(info, env) {
super(info, env);
this.compiler.supportsIntel = true;
this.compiler.supportsIrView = true;

this.self_hosted_cli = this.compiler.semver === 'trunk' ||
(this.compiler.semver && Semver.gt(this.compiler.semver, '0.6.0'));
this.self_hosted_cli =
this.compiler.semver === 'trunk' ||
(this.compiler.semver && Semver.gt(asSafeVer(this.compiler.semver), '0.6.0', true));

if (this.self_hosted_cli) {
this.compiler.irArg = ['-femit-llvm-ir'];
Expand All @@ -52,25 +56,28 @@ export class ZigCompiler extends BaseCompiler {
}

preProcess(source) {
const olderStdVersions = ['0.3.0', '0.4.0', '0.5.0', '0.6.0', '0.7.0', '0.7.1'];
if (this.compiler.semver === '0.2.0') {
if (Semver.eq(asSafeVer(this.compiler.semver), '0.2.0', true)) {
source += '\n';
source += 'extern fn zig_panic() noreturn;\n';
source += 'pub fn panic(msg: []const u8, error_return_trace: ' +
'?&@import("builtin").StackTrace) noreturn {\n';
source +=
'pub fn panic(msg: []const u8, error_return_trace: ?&@import("builtin").StackTrace) noreturn {\n';
source += ' zig_panic();\n';
source += '}\n';
} else if (olderStdVersions.includes(this.compiler.semver)) {
} else if (
Semver.gte(asSafeVer(this.compiler.semver), '0.3.0', true) &&
Semver.lte(asSafeVer(this.compiler.semver), '0.7.1', true)
) {
source += '\n';
source += 'extern fn zig_panic() noreturn;\n';
source += 'pub fn panic(msg: []const u8, error_return_trace: ' +
'?*@import("builtin").StackTrace) noreturn {\n';
source +=
'pub fn panic(msg: []const u8, error_return_trace: ?*@import("builtin").StackTrace) noreturn {\n';
source += ' zig_panic();\n';
source += '}\n';
} else {
source += '\n';
source += 'extern fn zig_panic() noreturn;\n';
source += 'pub fn panic(msg: []const u8, error_return_trace: ' +
source +=
'pub fn panic(msg: []const u8, error_return_trace: ' +
'?*@import("std").builtin.StackTrace) noreturn {\n';
source += ' _ = msg;\n';
source += ' _ = error_return_trace;\n';
Expand All @@ -91,8 +98,7 @@ export class ZigCompiler extends BaseCompiler {
if (this.self_hosted_cli) {
// Versions after 0.6.0 use a different command line interface.
const outputDir = path.dirname(outputFilename);
options.push('--cache-dir', outputDir,
'--name', name);
options.push('--cache-dir', outputDir, '--name', name);

if (!filters.binary) {
options.push('-fno-emit-bin', '-femit-asm=' + desiredName);
Expand All @@ -102,16 +108,19 @@ export class ZigCompiler extends BaseCompiler {
return options;
}

if (this.compiler.semver && Semver.gt(this.compiler.semver, '0.3.0')) {
if (this.compiler.semver && Semver.gt(asSafeVer(this.compiler.semver), '0.3.0', true)) {
const outputDir = path.dirname(outputFilename);
options.push('--cache-dir', outputDir,
'--output-dir', outputDir,
'--name', name);
options.push('--cache-dir', outputDir, '--output-dir', outputDir, '--name', name);
} else {
// Older versions use a different command line interface (#1304)
options.push('--cache-dir', path.dirname(outputFilename),
'--output', this.filename(outputFilename),
'--output-h', '/dev/null');
options.push(
'--cache-dir',
path.dirname(outputFilename),
'--output',
this.filename(outputFilename),
'--output-h',
'/dev/null'
);
}

if (!filters.binary) {
Expand All @@ -125,17 +134,18 @@ export class ZigCompiler extends BaseCompiler {
}

getIncludeArguments(libraries) {
return _.flatten(_.map(libraries, (selectedLib) => {
const foundVersion = this.findLibVersion(selectedLib);
if (!foundVersion) return false;
// Zig should not have more than 1 path
return ['--pkg-begin', foundVersion.name, foundVersion.path, '--pkg-end'];
}));
return _.flatten(
_.map(libraries, selectedLib => {
const foundVersion = this.findLibVersion(selectedLib);
if (!foundVersion) return false;
// Zig should not have more than 1 path
return ['--pkg-begin', foundVersion.name, foundVersion.path, '--pkg-end'];
})
);
}

getIrOutputFilename(inputFilename) {
return this.getOutputFilename(path.dirname(inputFilename), this.outputFilebase)
.replace('.s', '.ll');
return this.getOutputFilename(path.dirname(inputFilename), this.outputFilebase).replace('.s', '.ll');
}

filterUserOptions(userOptions) {
Expand Down
14 changes: 10 additions & 4 deletions lib/compilers/zigcc.js
Expand Up @@ -22,21 +22,27 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import { ClangCompiler } from './clang';
import Semver from 'semver';

import {asSafeVer} from '../utils';

import {ClangCompiler} from './clang';

export class ZigCC extends ClangCompiler {
static get key() { return 'zigcc'; }
static get key() {
return 'zigcc';
}

preProcess(source, filters) {
if (this.compiler.semver !== '0.6.0') {
if (Semver.eq(asSafeVer(this.compiler.semver), '0.6.0', true)) {
filters.binary = true;
}

return super.preProcess(source, filters);
}

optionsForFilter(filters, outputFilename) {
if (this.compiler.semver !== '0.6.0') {
if (Semver.eq(asSafeVer(this.compiler.semver), '0.6.0', true)) {
// note: zig versions > 0.6 don't emit asm, only binary works - https://github.com/ziglang/zig/issues/8153
filters.binary = true;
}
Expand Down
14 changes: 10 additions & 4 deletions lib/compilers/zigcxx.js
Expand Up @@ -22,21 +22,27 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import { ClangCompiler } from './clang';
import Semver from 'semver';

import {asSafeVer} from '../utils';

import {ClangCompiler} from './clang';

export class ZigCXX extends ClangCompiler {
static get key() { return 'zigcxx'; }
static get key() {
return 'zigcxx';
}

preProcess(source, filters) {
if (this.compiler.semver !== '0.6.0') {
if (Semver.eq(asSafeVer(this.compiler.semver), '0.6.0', true)) {
filters.binary = true;
}

return super.preProcess(source, filters);
}

optionsForFilter(filters, outputFilename) {
if (this.compiler.semver !== '0.6.0') {
if (Semver.eq(asSafeVer(this.compiler.semver), '0.6.0', true)) {
// note: zig versions > 0.6 don't emit asm, only binary works - https://github.com/ziglang/zig/issues/8153
filters.binary = true;
}
Expand Down
25 changes: 2 additions & 23 deletions lib/options-handler.js
Expand Up @@ -30,7 +30,7 @@ import _ from 'underscore';

import { logger } from './logger';
import { getToolTypeByKey } from './tooling';
import { countOccurrences, getHash, resolvePathFromAppRoot, splitArguments } from './utils';
import {asSafeVer, countOccurrences, getHash, resolvePathFromAppRoot, splitArguments} from './utils';

const HashVersion = 'Compiler Explorer Policies Version 1';

Expand Down Expand Up @@ -261,27 +261,6 @@ export class ClientOptionsHandler {
return libraries;
}

_asSafeVer(semver) {
if (semver != null) {
if (typeof semver === 'number') {
semver = `${semver}`;
}
const splits = semver.split(' ');
if (splits.length > 0) {
let interestingPart = splits[0];
let dotCount = countOccurrences(interestingPart, '.');
for (; dotCount < 2; dotCount++) {
interestingPart += '.0';
}
const validated = semverParser.valid(interestingPart, true);
if (validated != null) {
return validated;
}
}
}
return '9999999.99999.999';
}

getRemoteId(remoteUrl, language) {
const url = new URL(remoteUrl);
return url.host.replace(/\./g, '_') + '_' + language;
Expand Down Expand Up @@ -349,7 +328,7 @@ export class ClientOptionsHandler {
if (!semverGroups[compiler.group]) semverGroups[compiler.group] = [];
// Desired index which will keep the array in order
const index = _.sortedIndex(semverGroups[compiler.group], compiler.semver, (lhg) => {
return semverParser.compare(this._asSafeVer(lhg.semver), this._asSafeVer(compiler.semver));
return semverParser.compare(asSafeVer(lhg.semver), asSafeVer(compiler.semver), true);
});
semverGroups[compiler.group].splice(index, 0, compiler);
}
Expand Down

0 comments on commit 4965bd6

Please sign in to comment.