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

Re-request: Feature add linter slang #387

Merged
merged 6 commits into from
Jan 30, 2023
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
31 changes: 30 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,11 @@
"iverilog",
"verilator",
"modelsim",
"slang",
"none"
],
"default": "none",
"description": "Select the verilog linter. Possible values are 'iverilog', 'verilator', xvlog' or 'none'."
"description": "Select the verilog linter. Possible values are 'iverilog', 'verilator', 'modelsim', 'xvlog', 'slang' or 'none'."
},
"verilog.linting.iverilog.arguments": {
"scope": "window",
Expand Down Expand Up @@ -321,6 +322,34 @@
"default": false,
"description": "If enabled, run verilator in WSL."
},

"verilog.linting.slang.arguments": {
"scope": "window",
"type": "string",
"default": "",
"description": "Add Slang arguments here (like macros). They will be added to Slang while linting (The command \"--single-unit -Weverything --strict-driver-checking +incdir+<document folder>\" will be added by the linter by default)."
},
"verilog.linting.slang.includePath": {
"scope": "window",
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"description": "A list of directory paths to use while Slang linting."
},
"verilog.linting.slang.runAtFileLocation": {
"scope": "window",
"type": "boolean",
"default": false,
"description": "If enabled, Slang will be run at the file location for linting. Else it will be run at workspace folder. Disabled by default."
},
"verilog.linting.slang.useWSL": {
"scope": "window",
"type": "boolean",
"default": false,
"description": "If enabled, run slang in WSL."
},
"verilog.linting.xvlog.arguments": {
"scope": "window",
"type": "string",
Expand Down
7 changes: 7 additions & 0 deletions src/linter/LintManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BaseLinter from './BaseLinter';
import IcarusLinter from './IcarusLinter';
import ModelsimLinter from './ModelsimLinter';
import VerilatorLinter from './VerilatorLinter';
import SlangLinter from './SlangLinter';
import XvlogLinter from './XvlogLinter';

export default class LintManager {
Expand Down Expand Up @@ -38,6 +39,8 @@ export default class LintManager {
return new ModelsimLinter(this.diagnosticCollection, this.logger);
case 'verilator':
return new VerilatorLinter(this.diagnosticCollection, this.logger);
case 'slang':
return new SlangLinter(this.diagnosticCollection, this.logger);
default:
return null;
}
Expand Down Expand Up @@ -111,6 +114,10 @@ export default class LintManager {
label: 'verilator',
description: 'Verilator',
},
{
label: 'slang',
description: 'Slang',
},
],
{
matchOnDescription: true,
Expand Down
123 changes: 123 additions & 0 deletions src/linter/SlangLinter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import * as vscode from 'vscode';
import * as child from 'child_process';
import * as path from 'path';
import * as process from 'process';
import BaseLinter from './BaseLinter';

let isWindows = process.platform === 'win32';

export default class SlangLinter extends BaseLinter {
private configuration: vscode.WorkspaceConfiguration;
private linterInstalledPath: string;
private arguments: string;
private includePath: string[];
private runAtFileLocation: boolean;
private useWSL: boolean;

constructor(diagnosticCollection: vscode.DiagnosticCollection, logger: vscode.LogOutputChannel) {
super('slang', diagnosticCollection, logger);
vscode.workspace.onDidChangeConfiguration(() => {
this.updateConfig();
});
this.updateConfig();
}

private updateConfig() {
this.linterInstalledPath = <string>(
vscode.workspace.getConfiguration().get('verilog.linting.path')
);
this.configuration = vscode.workspace.getConfiguration('verilog.linting.slang');
this.arguments = <string>this.configuration.get('arguments');
let path = <string[]>this.configuration.get('includePath');
this.includePath = path.map((includePath: string) => this.resolvePath(includePath));
this.runAtFileLocation = <boolean>this.configuration.get('runAtFileLocation');
this.useWSL = <boolean>this.configuration.get('useWSL');
}

protected convertToSeverity(severityString: string) {
if (severityString.startsWith('error')) {
return vscode.DiagnosticSeverity.Error;
} else if (severityString.startsWith('warning')) {
return vscode.DiagnosticSeverity.Warning;
}
return vscode.DiagnosticSeverity.Information;
}

private convertToWslPath(inputPath: string): string {
let cmd: string = `wsl wslpath '${inputPath}'`;
return child.execSync(cmd, {}).toString().replace(/\r?\n/g, '');
}

protected lint(doc: vscode.TextDocument) {
let docUri: string = doc.uri.fsPath;
let docFolder: string = path.dirname(docUri);
if (isWindows) {
if (this.useWSL) {
docUri = this.convertToWslPath(docUri);
this.logger.info(`Rewrote docUri to ${docUri} for WSL`);

docFolder = this.convertToWslPath(docFolder);
this.logger.info(`Rewrote docFolder to ${docFolder} for WSL`);
} else {
docUri = docUri.replace(/\\/g, '/');
docFolder = docFolder.replace(/\\/g, '/');
}
}

let slang: string = isWindows
? this.useWSL
? 'wsl slang'
: 'slang.exe'
: 'slang';

let binPath = path.join(this.linterInstalledPath, slang);
let args: string[] = [];
args.push(`-I ${docFolder}`);
args = args.concat(this.includePath.map((path: string) => `-I ${path}`));
args.push(this.arguments);
args.push(`"${docUri}"`);
let command: string = binPath + ' ' + args.join(' ');

let cwd: string = this.runAtFileLocation
? docFolder
: vscode.workspace.workspaceFolders[0].uri.fsPath;

this.logger.info('[slang] Execute');
this.logger.info('[slang] command: ' + command);
this.logger.info('[slang] cwd : ' + cwd);


var _: child.ChildProcess = child.exec(
command,
{ cwd: cwd },
(_error: Error, _stdout: string, stderr: string) => {
let diagnostics: vscode.Diagnostic[] = [];
const re = /(.+?):(\d+):(\d+):\s(note|warning|error):\s([^[\]]*)(\[-W(.*)\])?/;
stderr.split(/\r?\n/g).forEach((line, _) => {
if (line.search(re) === -1) {
return;
}

let rex = line.match(re);

if (rex && rex[0].length > 0) {
let lineNum = Number(rex[2]) - 1;
let colNum = Number(rex[3]) - 1;

diagnostics.push({
severity: this.convertToSeverity(rex[4]),
range: new vscode.Range(lineNum, colNum, lineNum, Number.MAX_VALUE),
message: rex[5],
code: rex[7] ? rex[7] : 'error',
source: 'slang',
});
return;
}
this.logger.warn('[slang] failed to parse error: ' + line);
});
this.logger.info(`[slang] ${diagnostics.length} errors/warnings returned`);
this.diagnosticCollection.set(doc.uri, diagnostics);
}
);
}
}