Skip to content

Commit 1d21f0d

Browse files
authored
Merge pull request #7 from SublimeLinter/cleanup
Cleanup
2 parents e3504c2 + 925fc11 commit 1d21f0d

File tree

5 files changed

+85
-68
lines changed

5 files changed

+85
-68
lines changed

README.md

+25-15
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,48 @@ Once `clang` is installed, ensure it is in your system PATH so that SublimeLinte
1818
The docs cover [troubleshooting PATH configuration](http://sublimelinter.com/en/latest/troubleshooting.html#finding-a-linter-executable)
1919

2020
## Settings
21-
- SublimeLinter settings: http://sublimelinter.com/en/latest/settings.html
22-
- Linter settings: http://sublimelinter.com/en/latest/linter_settings.html
2321

24-
Additional settings for SublimeLinter-clang:
22+
We have two settings sections. 'clang' for c files, and 'clang++' to configure the linter for c++ files. E.g.
2523

26-
|Setting|Description|
27-
|:------|:----------|
28-
|include_dirs|A list of directories to be added to the header search paths (-I is not needed).|
29-
|extra_flags|A string with extra flags to pass to clang. These should be used carefully, as they may cause linting to fail.|
30-
31-
In project-specific settings, SublimeLinter allows [expansion variables](http://sublimelinter.readthedocs.io/en/latest/settings.html#settings-expansion). For example, in project-specific settings, '${project_path}' can be used to specify a path relative to the project folder.
3224
```
33-
"SublimeLinter":
3425
{
3526
"linters":
3627
{
3728
"clang": {
38-
"extra_flags": "-Wall -I${project_path}/foo",
39-
"include_dirs": [
40-
"${project_path}/3rdparty/bar/include",
41-
"${project_path}/3rdparty/baz"
29+
"args": "-fvery-important",
30+
"I": [
31+
"${folder}/3rdparty/bar/include",
32+
"${folder}/3rdparty/baz"
4233
]
34+
},
35+
"clang++": {
36+
"args": "-falso-important"
4337
}
4438
}
4539
},
4640
```
4741

42+
Note: 'args' has the default value '-Wall -fsyntax-only -fno-caret-diagnostics', so make sure to include them when overriding 'args'.
43+
44+
All common settings information can be found here:
45+
46+
- SublimeLinter settings: http://sublimelinter.com/en/latest/settings.html
47+
- Linter settings: http://sublimelinter.com/en/latest/linter_settings.html
48+
49+
Additional settings for SublimeLinter-clang:
50+
51+
|Setting|Description|
52+
|:------|:----------|
53+
|I|A list of directories to be added to the header search paths.|
54+
|x|Automatically set depending on the file type.|
55+
56+
SublimeLinter allows [expansion variables](http://sublimelinter.readthedocs.io/en/latest/settings.html#settings-expansion). For example, '${folder}' can be used to specify a path relative to the project folder.
57+
4858
## Troubleshooting
4959
C/C++ linting is not always straightforward. A few things to try when there's (almost) no linting information available:
5060
- Try to compile from the command line, and verify it works.
5161
- The linter might be missing some header files. They can be added with "include_dirs".
5262
- Sometimes clang fails to locate the C++ standard library headers.
5363
Assuming the compilation works when executed via command line, try to compile with `clang++ -v`.
54-
This will display all of the hidden flags clang uses. As a last resort, they can all be added as "extra_flags".
64+
This will display all of the hidden flags clang uses. As a last resort, they can all be added as "args".
5565

linter.py

+32-51
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,44 @@
1010

1111
"""This module exports the Clang plugin class."""
1212

13-
import shlex
14-
from SublimeLinter.lint import Linter, util
13+
import re
14+
from SublimeLinter.lint import Linter
1515

1616

17-
class Clang(Linter):
18-
19-
"""Provides an interface to clang."""
20-
21-
executable = 'clang'
17+
OUTPUT_RE = re.compile(
18+
r'<stdin>:(?P<line>\d+):'
19+
r'((?P<col>\d*): )?' # column number, colon and space are only applicable for single line messages
20+
# several lines of anything followed by
21+
# either error/warning/note or newline (= irrelevant backtrace content)
22+
# (lazy quantifiers so we don’t skip what we seek)
23+
r'(.*?((?P<error>error)|(?P<warning>warning|note)|\r?\n))+?'
24+
r': (?P<message>.+)', # match the remaining content of the current line for output
25+
re.MULTILINE
26+
)
2227

23-
regex = (
24-
r'<stdin>:(?P<line>\d+):'
25-
r'((?P<col>\d*): )?' # column number, colon and space are only applicable for single line messages
26-
# several lines of anything followed by
27-
# either error/warning/note or newline (= irrelevant backtrace content)
28-
# (lazy quantifiers so we don’t skip what we seek)
29-
r'(.*?((?P<error>error)|(?P<warning>warning|note)|\r?\n))+?'
30-
r': (?P<message>.+)' # match the remaining content of the current line for output
31-
)
32-
33-
multiline = True
3428

29+
class Clang(Linter):
30+
cmd = 'clang ${args} -'
3531
defaults = {
36-
'include_dirs': [],
37-
'extra_flags': "",
38-
'selector': 'source.c, source.c++'
32+
'selector': 'source.c',
33+
'args': '-Wall -fsyntax-only -fno-caret-diagnostics',
34+
'-I +': [],
35+
'-x': 'c'
3936
}
40-
41-
base_cmd = (
42-
'clang -fsyntax-only '
43-
'-fno-caret-diagnostics -Wall '
44-
)
45-
37+
regex = OUTPUT_RE
38+
multiline = True
4639
on_stderr = None
4740

48-
def cmd(self):
49-
"""
50-
Return the command line to execute.
51-
52-
We override this method, so we can add extra flags and include paths
53-
based on the 'include_dirs' and 'extra_flags' settings.
5441

55-
"""
56-
57-
result = self.base_cmd
58-
59-
if util.get_syntax(self.view) in ['c', 'c improved']:
60-
result += ' -x c '
61-
elif util.get_syntax(self.view) in ['c++', 'c++11']:
62-
result += ' -x c++ '
63-
64-
settings = self.get_view_settings()
65-
result += settings.get('extra_flags', '')
66-
67-
include_dirs = settings.get('include_dirs', [])
68-
69-
if include_dirs:
70-
result += ' '.join([' -I ' + shlex.quote(include) for include in include_dirs])
71-
72-
return result + ' -'
42+
class ClangPlus(Linter):
43+
name = 'clang++'
44+
cmd = 'clang ${args} -'
45+
defaults = {
46+
'selector': 'source.c++',
47+
'args': '-Wall -fsyntax-only -fno-caret-diagnostics',
48+
'-I +': [],
49+
'-x': 'c++'
50+
}
51+
regex = OUTPUT_RE
52+
multiline = True
53+
on_stderr = None

messages.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"install": "messages/install.txt"
2+
"install": "messages/install.txt",
3+
"2.0.0": "messages/2.0.0.txt"
34
}

messages/2.0.0.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
SublimeLinter-clang 2.0.0
2+
-------------------------
3+
4+
5+
We revamped the settings, and you probably have to make changes.
6+
7+
* If you used 'include_dirs', it is now called 'I' (just like the arg
8+
on the command line).
9+
10+
* 'extra_flags' is now 'args' (which is the standard name in the
11+
SublimeLinter world). Note that 'args' has the default value
12+
'-Wall -fsyntax-only -fno-caret-diagnostics'.
13+
14+
'c' and 'c++' files now have different settings sections.
15+
16+
{
17+
"linters": {
18+
"clang": { // <- for c files
19+
20+
},
21+
"clang++": { // <- for c++ files
22+
23+
}
24+
}
25+
}

messages/install.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ This linter plugin for SublimeLinter provides an interface to clang.
44

55
Please read the installation instructions at:
66

7-
https://github.com/nirm03/SublimeLinter-clang
7+
https://github.com/SublimeLinter/SublimeLinter-clang

0 commit comments

Comments
 (0)