diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7595163 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +node_modules +npm-debug.log diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..1412d6e --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,20 @@ +Copyright (c) 2015 Theo Willows + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3c56786 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +linter-cppcheck +=============== + +A [linter] for [Atom] using [Cppcheck]. + +## Requirements ## + +[Cppcheck] needs to be installed (or at least available). Its path can be +configured. + +Without [Linter] this package is rather useless. + +## Acknowledgements ## + +This package is really a tiny amount of glue between other great pieces of +software, namely the [Linter] package, the [atom-linter] module and — of course — +[Cppcheck]. + + [Atom]: https://atom.io + + [atom-linter]: https://www.npmjs.com/package/atom-linter + + [Cppcheck]: http://cppcheck.sourceforge.net + + [Linter]: https://atom.io/packages/linter diff --git a/lib/linter-cppcheck.js b/lib/linter-cppcheck.js new file mode 100644 index 0000000..7a608d0 --- /dev/null +++ b/lib/linter-cppcheck.js @@ -0,0 +1,166 @@ +'use babel'; +'use strict'; + +import { + exec, + parse, +} +from 'atom-linter'; + +import { + XRegExp +} +from 'xregexp'; + +export default { + + activate: (state) => {}, + + config: { + enableInformation: { + description: 'Enable information messages.', + default: true, + type: 'boolean', + }, + enableMissingInclude: { + description: 'Warn if there are missing includes.', + default: false, + type: 'boolean', + }, + enablePerformance: { + description: 'Enable performance messages.', + default: true, + type: 'boolean', + }, + enablePortability: { + description: 'Enable portability messages.', + default: true, + type: 'boolean', + }, + enableStyle: { + description: 'Enable all coding style checks. All messages with the severities *style*, *performance* and *portability* are enabled.', + default: true, + type: 'boolean', + }, + enableUnusedFunction: { + description: 'Check for unused functions.', + default: false, + type: 'boolean', + }, + enableWarning: { + description: 'Enable warning messages.', + default: true, + type: 'boolean', + }, + executable: { + default: 'cppcheck', + type: 'string', + }, + force: { + description: '`--force`', + default: false, + type: 'boolean', + }, + inconclusive: { + default: false, + description: 'Include inconclusive warnings. (`--inconclusive`)', + type: 'boolean', + }, + inlineSuppressions: { + default: false, + description: 'Enable inline suppressions. (`--inline-suppr`)', + type: 'boolean', + }, + suppress: { + description: 'Suppress specific warnings. See Cppcheck\'s documentation for details on the format. (`--suppress`)', + type: 'array', + items: { + type: 'string', + }, + }, + }, + + provideLinter: () => { + const regex = + '\\[(?.+?):(?:(?\\d+)\\]|(?\\d+)\\] -> \\[.+?:(?\\d+)\\]): \\((?\\w+)\\) (?.*)'; + + const lint = (textEditor) => { + const args = []; + + // --enable + const enables = []; + for (const enable of 'Information,MissingInclude,Performance,Portability,Style,UnusedFunction,Warning' + .split(',')) { + if (atom.config.get(`linter-cppcheck.enable${enable}`)) { + enables.push(enable.toLowerCase()); + } + } + if (enables.length) { + args.push('--enable=' + enables.join(',')); + } + + // --force + if (atom.config.get('linter-cppcheck.force')) { + args.push('--force'); + } + + // --inconclusive + if (atom.config.get('linter-cppcheck.inconclusive')) { + args.push('--inconclusive'); + } + + // --inline-suppr + if (atom.config.get('linter-cppcheck.inlineSuppressions')) { + args.push('--inline-suppr'); + } + + // --language + const grammar = textEditor.getGrammar().name; + switch (grammar) { + case 'C': + args.push('--language=c'); + break; + + case 'C++': + args.push('--language=c++'); + break; + + default: + console.warn( + `[linter-cppcheck] Unrecognised grammar ‘${grammar}’` + ); + break; + } + + // --suppress + for (const suppress of atom.config.get('linter-cppcheck.suppress') || []) { + args.push(`--suppress=${suppress}`); + } + + // + args.push(textEditor.getPath()); + + return exec( + atom.config.get('linter-cppcheck.executable'), + args, { + 'stream': 'stderr' + } + ).then((output) => { + return parse(output, regex); + }); + }; + + const provider = { + grammarScopes: ['source.c', 'source.cpp'], + lint: lint, + lintOnFly: false, + name: 'Cppcheck', + scope: 'file', + } + + return provider; + }, + + serialize: () => {}, + +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..dba23a5 --- /dev/null +++ b/package.json @@ -0,0 +1,40 @@ +{ + "activationCommands": {}, + "author": { + "name": "Theo Willows", + "email": "theo@willows.se" + }, + "bugs": { + "url": "https://github.com/Munkei/atom-linter-cppcheck/issues" + }, + "contributors": [], + "dependencies": { + "atom-linter": "^4.1.1", + "xregexp": "^3.0.0" + }, + "description": "A linter for Atom using Cppcheck", + "engines": { + "atom": ">=1.0.0 <2.0.0" + }, + "homepage": "https://github.com/Munkei/atom-linter-cppcheck", + "keywords": [ + "c", + "c++", + "linter" + ], + "license": "MIT", + "main": "./lib/linter-cppcheck", + "name": "linter-cppcheck", + "providedServices": { + "linter": { + "versions": { + "1.0.0": "provideLinter" + } + } + }, + "repository": { + "type": "git", + "url": "https://github.com/Munkei/atom-linter-cppcheck" + }, + "version": "0.0.0" +} diff --git a/project.json b/project.json new file mode 100644 index 0000000..92219c9 --- /dev/null +++ b/project.json @@ -0,0 +1,4 @@ +{ + "name": "linter-cppcheck", + "type": "atom-package" +} diff --git a/styles/linter-cppcheck.less b/styles/linter-cppcheck.less new file mode 100644 index 0000000..71b10e9 --- /dev/null +++ b/styles/linter-cppcheck.less @@ -0,0 +1,36 @@ +@import "ui-variables"; + +atom-text-editor::shadow .linter-highlight, +.linter-highlight { + &.information { + &:not(.line-number) { + background-color: @background-color-info; + color : white; + } + + .linter-gutter { + color: @background-color-info; + } + + .region { + border-bottom: 1px dashed @background-color-info; + } + } + + &.performance, + &.portability, + &.style { + &:not(.line-number) { + background-color: @background-color-warning; + color : white; + } + + .linter-gutter { + color: @background-color-warning; + } + + .region { + border-bottom: 1px dashed @background-color-warning; + } + } +}