-
Notifications
You must be signed in to change notification settings - Fork 48
/
linter.py
91 lines (68 loc) · 2.4 KB
/
linter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#
# linter.py
# Linter for SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by nirm03
# Copyright (c) 2013 nirm03
#
# License: MIT
#
"""This module exports the Clang plugin class."""
import shlex
from SublimeLinter.lint import Linter, persist
import sublime
import os
import string
def get_project_folder():
proj_file = sublime.active_window().project_file_name()
if proj_file:
return os.path.dirname(proj_file)
# Use current file's folder when no project file is opened.
proj_file = sublime.active_window().active_view().file_name()
if proj_file:
return os.path.dirname(proj_file)
return '.'
def apply_template(s):
mapping = {
"project_folder": get_project_folder()
}
templ = string.Template(s)
return templ.safe_substitute(mapping)
class Clang(Linter):
"""Provides an interface to clang."""
syntax = ('c', 'c improved', 'c++', 'c++11')
executable = 'clang'
regex = (r'<stdin>:(?P<line>\d+):'
r'((?P<col>\d*): )?'# column number, colon and space are only applicable for single line messages
# several lines of anything followed by
# either error/warning/note or newline (= irrelevant backtrace content)
# (lazy quantifiers so we don’t skip what we seek)
r'(.*?((?P<error>error)|(?P<warning>warning|note)|\r?\n))+?'
r': (?P<message>.+)'# match the remaining content of the current line for output
)
multiline = True
defaults = {
'include_dirs': [],
'extra_flags': ""
}
base_cmd = (
'clang -fsyntax-only '
'-fno-caret-diagnostics -Wall '
)
def cmd(self):
"""
Return the command line to execute.
We override this method, so we can add extra flags and include paths
based on the 'include_dirs' and 'extra_flags' settings.
"""
result = self.base_cmd
if persist.get_syntax(self.view) in ['c', 'c improved']:
result += ' -x c '
elif persist.get_syntax(self.view) in ['c++', 'c++11']:
result += ' -x c++ '
settings = self.get_view_settings()
result += apply_template( settings.get('extra_flags', '') )
include_dirs = settings.get('include_dirs', [])
if include_dirs:
result += apply_template( ' '.join([' -I ' + shlex.quote(include) for include in include_dirs]) )
return result + ' -'