|
| 1 | +import {workspace, window, Disposable, Range, TextDocument, Diagnostic, DiagnosticSeverity, DiagnosticCollection, languages} from "vscode"; |
| 2 | +import * as child from 'child_process'; |
| 3 | +import BaseLinter from "./BaseLinter"; |
| 4 | +import { join } from "path"; |
| 5 | + |
| 6 | +var isWindows = process.platform === "win32"; |
| 7 | + |
| 8 | +export default class VerilatorLinter extends BaseLinter { |
| 9 | + private verilatorArgs: string; |
| 10 | + private runAtFileLocation: boolean; |
| 11 | + |
| 12 | + constructor() { |
| 13 | + super("verilator"); |
| 14 | + |
| 15 | + workspace.onDidChangeConfiguration(() => { |
| 16 | + this.getConfig(); |
| 17 | + }) |
| 18 | + this.getConfig(); |
| 19 | + } |
| 20 | + |
| 21 | + private getConfig() { |
| 22 | + this.verilatorArgs = <string>workspace.getConfiguration().get('verilog.linting.verilator.arguments', ''); |
| 23 | + this.runAtFileLocation = <boolean>workspace.getConfiguration().get('verilog.linting.verilator.runAtFileLocation') |
| 24 | + } |
| 25 | + |
| 26 | + protected splitTerms(line: string){ |
| 27 | + let terms = line.split(':'); |
| 28 | + |
| 29 | + for (var i = 0; i < terms.length; i++) { |
| 30 | + if (terms[i] == ' ') { |
| 31 | + terms.splice(i, 1); |
| 32 | + i--; |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + terms[i] = terms[i].trim(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return terms; |
| 41 | + } |
| 42 | + |
| 43 | + protected getSeverity(severityString: string){ |
| 44 | + let result = DiagnosticSeverity.Information; |
| 45 | + |
| 46 | + if (severityString.startsWith('Error')) |
| 47 | + { |
| 48 | + result = DiagnosticSeverity.Error; |
| 49 | + } |
| 50 | + else if (severityString.startsWith('Warning')) |
| 51 | + { |
| 52 | + result = DiagnosticSeverity.Warning; |
| 53 | + } |
| 54 | + |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + protected lint(doc: TextDocument) { |
| 59 | + let docUri: string = doc.uri.fsPath //path of current doc |
| 60 | + let lastIndex:number = (isWindows == true)? docUri.lastIndexOf("\\") : docUri.lastIndexOf("/"); |
| 61 | + let docFolder = docUri.substr(0, lastIndex); //folder of current doc |
| 62 | + let runLocation: string = (this.runAtFileLocation == true)? docFolder : workspace.rootPath; //choose correct location to run |
| 63 | + let command: string = 'verilator --lint-only -I'+docFolder+ ' ' + this.verilatorArgs + ' \"' + doc.fileName +'\"'; //command to execute |
| 64 | + var foo: child.ChildProcess = child.exec(command,{cwd:runLocation},(error:Error, stdout:string, stderr:string) => { |
| 65 | + let diagnostics: Diagnostic[] = []; |
| 66 | + let lines = stderr.split(/\r?\n/g); |
| 67 | + |
| 68 | + // Parse output lines |
| 69 | + lines.forEach((line, i) => { |
| 70 | + |
| 71 | + if(line.startsWith('%')){ |
| 72 | + // remove the % |
| 73 | + line = line.substr(1) |
| 74 | + |
| 75 | + // was it for a submodule |
| 76 | + if (line.search(doc.fileName) > 0) |
| 77 | + { |
| 78 | + // remove the filename |
| 79 | + line = line.replace(doc.fileName, ''); |
| 80 | + line = line.replace(/\s+/g,' ').trim(); |
| 81 | + |
| 82 | + let terms = this.splitTerms(line); |
| 83 | + let severity = this.getSeverity(terms[0]); |
| 84 | + let message = terms.slice(2).join(' ') |
| 85 | + let lineNum = parseInt(terms[1].trim()) - 1; |
| 86 | + |
| 87 | + if (lineNum != NaN) |
| 88 | + { |
| 89 | + console.log(terms[1].trim() + ' ' + message); |
| 90 | + |
| 91 | + diagnostics.push({ |
| 92 | + severity: severity, |
| 93 | + range:new Range(lineNum, 0, lineNum, Number.MAX_VALUE), |
| 94 | + message: message, |
| 95 | + code: 'verilator', |
| 96 | + source: 'verilator' |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + this.diagnostic_collection.set(doc.uri, diagnostics) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments