├── .no-sublime-package ├── .gitignore ├── messages.json ├── .sublimelinterrc ├── messages └── install.txt ├── LICENSE ├── linter.py └── README.md /.no-sublime-package: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /messages.json: -------------------------------------------------------------------------------- 1 | { 2 | "install": "messages/install.txt" 3 | } 4 | -------------------------------------------------------------------------------- /.sublimelinterrc: -------------------------------------------------------------------------------- 1 | { 2 | "@python": 3, 3 | "linters": { 4 | "flake8": { 5 | "max-line-length": 120 6 | }, 7 | "pep8": { 8 | "max-line-length": 120 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /messages/install.txt: -------------------------------------------------------------------------------- 1 | SublimeLinter-clang 2 | ------------------------------- 3 | This linter plugin for SublimeLinter provides an interface to clang. 4 | 5 | ** IMPORTANT! ** 6 | 7 | Before this plugin will activate, you *must* 8 | follow the installation instructions here: 9 | 10 | https://github.com/nirm03/SublimeLinter-clang 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any person obtaining a copy 2 | of this software and associated documentation files (the "Software"), to deal 3 | in the Software without restriction, including without limitation the rights 4 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 5 | copies of the Software, and to permit persons to whom the Software is 6 | furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in 9 | all copies or substantial portions of the Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 17 | THE SOFTWARE. 18 | -------------------------------------------------------------------------------- /linter.py: -------------------------------------------------------------------------------- 1 | # 2 | # linter.py 3 | # Linter for SublimeLinter3, a code checking framework for Sublime Text 3 4 | # 5 | # Written by nirm03 6 | # Copyright (c) 2013 nirm03 7 | # 8 | # License: MIT 9 | # 10 | 11 | """This module exports the Clang plugin class.""" 12 | 13 | import shlex 14 | from SublimeLinter.lint import Linter, persist 15 | import sublime 16 | import os 17 | import string 18 | 19 | 20 | def get_project_folder(): 21 | proj_file = sublime.active_window().project_file_name() 22 | if proj_file: 23 | return os.path.dirname(proj_file) 24 | # Use current file's folder when no project file is opened. 25 | proj_file = sublime.active_window().active_view().file_name() 26 | if proj_file: 27 | return os.path.dirname(proj_file) 28 | return '.' 29 | 30 | 31 | def apply_template(s): 32 | mapping = { 33 | "project_folder": get_project_folder() 34 | } 35 | templ = string.Template(s) 36 | return templ.safe_substitute(mapping) 37 | 38 | 39 | class Clang(Linter): 40 | 41 | """Provides an interface to clang.""" 42 | 43 | syntax = ('c', 'c improved', 'c++', 'c++11') 44 | executable = 'clang' 45 | 46 | regex = (r':(?P\d+):' 47 | r'((?P\d*): )?'# column number, colon and space are only applicable for single line messages 48 | # several lines of anything followed by 49 | # either error/warning/note or newline (= irrelevant backtrace content) 50 | # (lazy quantifiers so we don’t skip what we seek) 51 | r'(.*?((?Perror)|(?Pwarning|note)|\r?\n))+?' 52 | r': (?P.+)'# match the remaining content of the current line for output 53 | ) 54 | 55 | multiline = True 56 | 57 | defaults = { 58 | 'include_dirs': [], 59 | 'extra_flags': "" 60 | } 61 | 62 | base_cmd = ( 63 | 'clang -fsyntax-only ' 64 | '-fno-caret-diagnostics -Wall ' 65 | ) 66 | 67 | def cmd(self): 68 | """ 69 | Return the command line to execute. 70 | 71 | We override this method, so we can add extra flags and include paths 72 | based on the 'include_dirs' and 'extra_flags' settings. 73 | 74 | """ 75 | 76 | result = self.base_cmd 77 | 78 | if persist.get_syntax(self.view) in ['c', 'c improved']: 79 | result += ' -x c ' 80 | elif persist.get_syntax(self.view) in ['c++', 'c++11']: 81 | result += ' -x c++ ' 82 | 83 | settings = self.get_view_settings() 84 | result += apply_template( settings.get('extra_flags', '') ) 85 | 86 | include_dirs = settings.get('include_dirs', []) 87 | 88 | if include_dirs: 89 | result += apply_template( ' '.join([' -I ' + shlex.quote(include) for include in include_dirs]) ) 90 | 91 | return result + ' -' 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SublimeLinter-clang 2 | ========================= 3 | 4 | This linter plugin for [SublimeLinter](https://github.com/SublimeLinter/SublimeLinter3) provides an interface to [clang](http://clang.llvm.org/). It will be used with files that have the C/C Improved/C++ syntax. 5 | 6 | ## Installation 7 | SublimeLinter 3 must be installed in order to use this plugin. If SublimeLinter 3 is not installed, please follow the instructions [here](http://sublimelinter.readthedocs.org/en/latest/installation.html). 8 | 9 | ### Linter installation 10 | Before using this plugin, you must ensure that `clang` is installed on your system. 11 | - Mac OS X: clang should be already bundled. 12 | - Linux: clang can be easily installed using most package managers. 13 | - Windows: the situation is a little trickier, especially with C++. One way to go is to install [mingw with clang](http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/rubenvb/). Both gcc and clang packages should be installed into the same directory. 14 | 15 | Once `clang` is installed, you must ensure it is in your system PATH so that SublimeLinter can find it. This may not be as straightforward as you think, so please read [How linter executables are located](http://sublimelinter.readthedocs.org/en/latest/usage.html#how-linter-executables-are-located) in the documentation. 16 | 17 | Once you have installed `clang` you can proceed to install the SublimeLinter-clang plugin if it is not yet installed. 18 | 19 | ### Plugin installation 20 | Please use [Package Control](https://sublime.wbond.net/installation) to install the linter plugin. This will ensure that the plugin will be updated when new versions are available. If you want to install from source so you can modify the source code, you probably know what you are doing so we won’t cover that here. 21 | 22 | To install via Package Control, do the following: 23 | 24 | 1. Within Sublime Text, bring up the [Command Palette](http://docs.sublimetext.info/en/sublime-text-3/extensibility/command_palette.html) and type `install`. Among the commands you should see `Package Control: Install Package`. If that command is not highlighted, use the keyboard or mouse to select it. There will be a pause of a few seconds while Package Control fetches the list of available plugins. 25 | 26 | 1. When the plugin list appears, type `clang`. Among the entries you should see `SublimeLinter-clang`. If that entry is not highlighted, use the keyboard or mouse to select it. 27 | 28 | ## Settings 29 | For general information on how SublimeLinter works with settings, please see [Settings](http://sublimelinter.readthedocs.org/en/latest/settings.html). For information on generic linter settings, please see [Linter Settings](http://sublimelinter.readthedocs.org/en/latest/linter_settings.html). 30 | 31 | In addition to the standard SublimeLinter settings, SublimeLinter-clang provides its own settings. 32 | 33 | |Setting|Description| 34 | |:------|:----------| 35 | |include_dirs|A list of directories to be added to the header search paths (-I is not needed).| 36 | |extra_flags|A string with extra flags to pass to clang. These should be used carefully, as they may cause linting to fail.| 37 | 38 | In project-specific settings, '$project_folder' or '${project_folder}' can be used to specify relative path. 39 | ``` 40 | "SublimeLinter": 41 | { 42 | "linters": 43 | { 44 | "clang": { 45 | "extra_flags": "-Wall -I${project_folder}/foo", 46 | "include_dirs": [ 47 | "${project_folder}/3rdparty/bar/include", 48 | "${project_folder}/3rdparty/baz" 49 | ] 50 | } 51 | } 52 | }, 53 | ``` 54 | 55 | ## Troubleshooting 56 | C/C++ linting is not always straightforward. A few things to try when there's (almost) no linting information available: 57 | - Try to compile from the command line, and verify it works. 58 | - The linter might be missing some header files. They can be added with "include_dirs". 59 | - Sometimes clang fails to locate the C++ standard library headers. 60 | Assuming the compilation works when executed via command line, try to compile with `clang++ -v`. 61 | This will display all of the hidden flags clang uses. As a last resort, they can all be added as "extra_flags". 62 | 63 | ## Contributing 64 | If you would like to contribute enhancements or fixes, please do the following: 65 | 66 | 1. Fork the plugin repository. 67 | 1. Hack on a separate topic branch created from the latest `master`. 68 | 1. Commit and push the topic branch. 69 | 1. Make a pull request. 70 | 1. Be patient. ;-) 71 | 72 | Please note that modifications should follow these coding guidelines: 73 | 74 | - Indent is 4 spaces. 75 | - Code should pass flake8 and pep257 linters. 76 | - Vertical whitespace helps readability, don’t be afraid to use it. 77 | - Please use descriptive variable names, no abbrevations unless they are very well known. 78 | 79 | Thank you for helping out! 80 | --------------------------------------------------------------------------------