├── .python-version ├── CHANGELOG.md ├── LICENSE ├── README.md ├── linter.py ├── menus └── Main.sublime-menu ├── messages.json ├── messages └── update_message.md └── miscellaneous └── demo.cpp /.python-version: -------------------------------------------------------------------------------- 1 | 3.8 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # SublimeLinter-gcc 2 | 3 | ## 3.0.0 4 | 5 | This is the first release which runs on the Python 3.8 plugin host in Sublime Text 4. 6 | 7 | ## 2.0.1 8 | 9 | No changes. Just to annotate that this is the last version for Sublime Text 3. 10 | The next release will only run in Sublime Text 4 (or later) with its Python 3.8 plugin host. 11 | 12 | ## 2.0.0 13 | 14 | This is a BC break version. Please read the new settings format from the 15 | [README](https://github.com/SublimeLinter/SublimeLinter-gcc/blob/2.0.0/README.md#settings). 16 | 17 | - `gcc` and `g++` are separated into two linters. 18 | - Drop support for SublimeLinter 3. 19 | - Adapt SublimeLinter 4 APIs and settings. 20 | 21 | ## 1.3.9 22 | 23 | - Fix a typo. 24 | 25 | ## 1.3.8 26 | 27 | - SublimeLinter 4.3.1 compatible. 28 | 29 | ## 1.3.7 30 | 31 | - Automatically append the directory of the current file to the include directory. 32 | 33 | ## 1.3.5 34 | 35 | - Change package names from `SublimeLinter-contrib-gcc` to `SublimeLinter-gcc`. 36 | 37 | ## 1.3.4 38 | 39 | - Fix SublimeLinter version checking. 40 | 41 | ## 1.3.3 42 | 43 | - Fix typos. 44 | 45 | ## 1.3.2 46 | 47 | - Get SublimeLinter's version number via SublimeLinter 4 API. 48 | 49 | ## 1.3.1 50 | 51 | - Allow `extra_flags` to be a list. 52 | 53 | ## 1.3.0 54 | 55 | - Compatible with SublimeLinter 4. 56 | 57 | ## 1.2.2 58 | 59 | - Remove `-fsyntax-only` from common flags. 60 | See https://github.com/jfcherng/SublimeLinter-contrib-gcc/issues/4 for details. 61 | 62 | ## 1.2.1 63 | 64 | - Fix `col` is not always presented in `gcc` output. 65 | 66 | ## 1.2.0 67 | 68 | - Add `C` or `C++` specific settings. 69 | 70 | ## 1.1.0 71 | 72 | - Add a new setting: `executable`. 73 | 74 | ## 1.0.1 75 | 76 | - Fix linting of unsaved files. 77 | 78 | ## 1.0.0 79 | 80 | - Initial release. 81 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2024 Jack Cherng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SublimeLinter-gcc 2 | 3 | [![Package Control](https://img.shields.io/packagecontrol/dt/SublimeLinter-gcc?style=flat-square)](https://packagecontrol.io/packages/SublimeLinter-gcc) 4 | [![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/SublimeLinter/SublimeLinter-gcc?style=flat-square&logo=github)](https://github.com/SublimeLinter/SublimeLinter-gcc/tags) 5 | [![Project license](https://img.shields.io/github/license/SublimeLinter/SublimeLinter-gcc?style=flat-square&logo=github)](https://github.com/SublimeLinter/SublimeLinter-gcc/blob/master/LICENSE) 6 | [![GitHub stars](https://img.shields.io/github/stars/SublimeLinter/SublimeLinter-gcc?style=flat-square&logo=github)](https://github.com/SublimeLinter/SublimeLinter-gcc/stargazers) 7 | [![Donate to this project using Paypal](https://img.shields.io/badge/paypal-donate-blue.svg?style=flat-square&logo=paypal)](https://www.paypal.me/jfcherng/5usd) 8 | 9 | This linter plugin for [SublimeLinter](https://github.com/SublimeLinter/SublimeLinter) 10 | provides an interface to [gcc](https://gcc.gnu.org/) or other gcc-like (cross-)compiler. 11 | It will be used with files that have the C/C++ syntax. 12 | If you are using [clang](https://clang.llvm.org), you may want to check 13 | [SublimeLinter-clang](https://github.com/SublimeLinter/SublimeLinter-clang). 14 | 15 | 16 | ## Installation 17 | 18 | SublimeLinter must be installed in order to use this plugin. 19 | If SublimeLinter is not installed, please follow the instructions 20 | [here](https://sublimelinter.readthedocs.org/en/stable/installation.html). 21 | 22 | 23 | ### Linter installation 24 | 25 | Before using this plugin, you must ensure that `gcc` or other gcc-like compiler is installed on your system. 26 | 27 | You may install `gcc` with the following method: 28 | 29 | - Mac OS X: [OSX GCC Installer](https://github.com/kennethreitz/osx-gcc-installer) 30 | - Linux: `gcc` could be installed by using most package managers. 31 | - Windows: [MinGW-w64](https://sourceforge.net/projects/mingw-w64) 32 | 33 | Once `gcc` is installed, you must ensure it is in your system PATH so that SublimeLinter can find it. 34 | This may not be as straightforward as you think, so please read [Debugging PATH problems](https://sublimelinter.readthedocs.org/en/stable/troubleshooting.html#debugging-path-problems) in the documentation. 35 | 36 | 37 | ### Plugin installation 38 | 39 | Please use [Package Control](https://sublime.wbond.net/installation) to install the linter plugin. 40 | This will ensure that the plugin will be updated when new versions are available. 41 | If you want to install from source so you can modify the source code, 42 | you probably know what you are doing so we won't cover that here. 43 | 44 | To install via Package Control, do the following: 45 | 46 | 1. Within Sublime Text, bring up the `Command Palette` by Ctrl + Shift + P and type `install`. 47 | Among the commands you should see `Package Control: Install Package`. 48 | If that command is not highlighted, use the keyboard or mouse to select it. 49 | There will be a pause of a few seconds while Package Control fetches the list of available plugins. 50 | 51 | 1. When the plugin list appears, type `gcc`. Among the entries you should see `SublimeLinter-gcc`. 52 | If that entry is not highlighted, use the keyboard or mouse to select it. 53 | 54 | 55 | ## Settings 56 | 57 | Here are some most frequently used custom settings. 58 | 59 | | Setting | Description | 60 | | :------ | :---------- | 61 | | executable | The compiler's binary path. This is `["gcc"]` or `["g++"]` by default. If you are not using them, you have to set this to your compiler binary such as `["arm-none-eabi-gcc"]`. | 62 | | I | A list of directories to be added to the header's searching paths. I.e., paths for `-I` flags. | 63 | | args | A list of extra flags to be passed to the compiler. These should be used carefully as they may cause linting to fail. | 64 | 65 | 66 | Here is an example settings: 67 | 68 | ```javascript 69 | { 70 | "linters": 71 | { 72 | "gcc": { 73 | "disable": false, 74 | "executable": ["gcc"], 75 | "args": ["-fsyntax-only", "-std=c90"], 76 | "I": [ 77 | "${file_path}/include", 78 | "${folder}/include", 79 | "/usr/local/include", 80 | ], 81 | "excludes": [], 82 | }, 83 | "g++": { 84 | "disable": false, 85 | "executable": ["g++"], 86 | "args": ["-fsyntax-only", "-std=c++20"], 87 | "I": [ 88 | "${file_path}/include", 89 | "${folder}/include", 90 | "/usr/local/include", 91 | ], 92 | "excludes": [], 93 | }, 94 | }, 95 | } 96 | ``` 97 | 98 | Here are some useful docs for SublimeLinter settings. 99 | 100 | - [General information on how SublimeLinter works with settings](https://sublimelinter.readthedocs.org/en/stable/settings.html). 101 | - [Variables that can be used in settings](https://sublimelinter.readthedocs.org/en/stable/settings.html#settings-expansion). 102 | - [Information on generic linter settings](https://sublimelinter.readthedocs.org/en/stable/linter_settings.html). 103 | 104 | 105 | ## Notes 106 | 107 | - [Here](https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Warning-Options.html#Warning-Options) 108 | is the official list of warning options in gcc 13.2.0. I prefer turn on all warnings 109 | via `-Wall` (this is default for this plugin) and then suppress unwanted warnings via `-Wno-` prefix. 110 | 111 | - Use the `-fsyntax-only` flag in `args` gives a much faster syntax-only checking but 112 | [some warnings](https://github.com/SublimeLinter/SublimeLinter-gcc/issues/4) 113 | which are emitted in the code optimization phase would not be caught. 114 | 115 | 116 | ## Demo 117 | 118 | ![linting_example](https://raw.githubusercontent.com/SublimeLinter/SublimeLinter-gcc/gh-pages/images/linting_example_sl4.png) 119 | 120 | 121 | ## Troubleshooting 122 | 123 | C/C++ linting is not always straightforward. 124 | A few things to try when there's (almost) no linting information available: 125 | 126 | - Try to compile from the command line, and verify it works. 127 | - The linter might be missing some header files. They can be added with settings `I`. 128 | - Sometimes gcc fails to locate the C/C++ standard library headers. 129 | 130 | Assuming the compilation works when executed via command line, try to compile with `g++ -v`. 131 | This will display all of the hidden flags that gcc uses. 132 | As a last resort, they can all be added in settings `args`. 133 | 134 | 135 | ## Contributing 136 | 137 | If you would like to contribute enhancements or fixes, please do the following: 138 | 139 | 1. Fork the plugin repository. 140 | 1. Hack on a separate topic branch created from the latest `master`. 141 | 1. Commit and push the topic branch. 142 | 1. Make sure your modification could pass unittests. 143 | 1. Make a pull request. 144 | 1. Be patient. 145 | 146 | Please note that modifications should follow these coding guidelines: 147 | 148 | - Indent is 4 spaces. 149 | - Code should pass flake8 and pep257 linters. 150 | - Probably format codes with [black](https://github.com/psf/black) code formatter. 151 | - Vertical whitespace helps readability, don’t be afraid to use it. 152 | - Please use descriptive variable names, no abbreviations unless they are very well known. 153 | 154 | Thank you for helping out! 155 | -------------------------------------------------------------------------------- /linter.py: -------------------------------------------------------------------------------- 1 | # 2 | # linter.py 3 | # Linter for SublimeLinter4, a code checking framework for Sublime Text 3 4 | # 5 | # Written by Jack Cherng 6 | # Copyright (c) 2017-2024 jfcherng 7 | # 8 | # License: MIT 9 | # 10 | 11 | from __future__ import annotations 12 | 13 | import os 14 | import re 15 | import tempfile 16 | 17 | import sublime 18 | import sublime_plugin 19 | from SublimeLinter.lint import Linter 20 | 21 | OUTPUT_RE = re.compile( 22 | r":(?P\d+):((?P\d+):)?\s*" 23 | + r".*?((?Perror)|(?Pwarning|note)):\s*" 24 | + r"(?P.+)", 25 | re.MULTILINE, 26 | ) 27 | 28 | 29 | def get_garbabge_file_path() -> str: 30 | """ 31 | @brief Get the path for generated garbabge file. 32 | 33 | Some checks are not performed when flag "-fsyntax-only" is given. 34 | To perform those checks in optimization phase, we must do a real compilation. 35 | This garbage file path is just a dummy output file for that compilation. 36 | 37 | @ref https://github.com/SublimeLinter/SublimeLinter-gcc/issues/4 38 | @return string The garbabge file path. 39 | """ 40 | 41 | if sublime.platform() == "windows": 42 | return os.path.join(tempfile.gettempdir(), "SublimeLinter-gcc.o") 43 | return "/dev/null" 44 | 45 | 46 | class Gcc(Linter): 47 | name = "gcc" 48 | cmd = "gcc ${args} -" 49 | regex = OUTPUT_RE 50 | multiline = True 51 | on_stderr = None 52 | 53 | defaults = { 54 | "selector": "source.c", 55 | "args": ["-c", "-Wall", "-O0"], 56 | "-I +": [], 57 | "-x": "c", 58 | "-o": get_garbabge_file_path(), 59 | } 60 | 61 | 62 | class GPlusPlus(Linter): 63 | name = "g++" 64 | cmd = "g++ ${args} -" 65 | regex = OUTPUT_RE 66 | multiline = True 67 | on_stderr = None 68 | 69 | defaults = { 70 | "selector": "source.c++", 71 | "args": ["-c", "-Wall", "-O0"], 72 | "-I +": [], 73 | "-x": "c++", 74 | "-o": get_garbabge_file_path(), 75 | } 76 | 77 | 78 | class SublimeLinterGccRunTests(sublime_plugin.WindowCommand): 79 | """ 80 | To do unittests, run the following command in ST's console: 81 | window.run_command('sublime_linter_gcc_run_tests') 82 | """ 83 | 84 | def run(self) -> None: 85 | from .tests.regex_tests import run_tests 86 | 87 | run_tests(Gcc.regex) 88 | -------------------------------------------------------------------------------- /menus/Main.sublime-menu: -------------------------------------------------------------------------------- 1 | [{ 2 | "caption": "Preferences", 3 | "id": "preferences", 4 | "children": [{ 5 | "caption": "Package Settings", 6 | "id": "package-settings", 7 | "children": [{ 8 | "caption": "SublimeLinter-gcc", 9 | "children": [{ 10 | "caption": "-", 11 | }, { 12 | "caption": "README", 13 | "command": "open_file", 14 | "args": { 15 | "file": "${packages}/SublimeLinter-gcc/README.md", 16 | }, 17 | }, { 18 | "caption": "Changelog", 19 | "command": "open_file", 20 | "args": { 21 | "file": "${packages}/SublimeLinter-gcc/CHANGELOG.md", 22 | }, 23 | }, { 24 | "caption": "-", 25 | }, { 26 | "caption": "Open the demo file", 27 | "command": "open_file", 28 | "args": { 29 | "file": "${packages}/SublimeLinter-gcc/miscellaneous/demo.cpp", 30 | }, 31 | }, { 32 | "caption": "-", 33 | }] 34 | }] 35 | }] 36 | }] 37 | -------------------------------------------------------------------------------- /messages.json: -------------------------------------------------------------------------------- 1 | { 2 | "3.0.0": "messages/update_message.md", 3 | "install": "README.md" 4 | } 5 | -------------------------------------------------------------------------------- /messages/update_message.md: -------------------------------------------------------------------------------- 1 | SublimeLinter-gcc has been updated. To see the changelog, visit 2 | Preferences » Package Settings » SublimeLinter-gcc » Changelog 3 | 4 | 5 | ## 3.0.0 6 | 7 | This is the first release which runs on the Python 3.8 plugin host in Sublime Text 4. 8 | -------------------------------------------------------------------------------- /miscellaneous/demo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char *argv[]) { 5 | int i; 6 | std::vector v; 7 | 8 | v.puch_back("test"); 9 | 10 | return 0; 11 | } 12 | --------------------------------------------------------------------------------