File: gnatcheck.py

package info (click to toggle)
gnat-gps 18-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,716 kB
  • sloc: ada: 362,679; python: 31,031; xml: 9,597; makefile: 1,030; ansic: 917; sh: 264; java: 17
file content (453 lines) | stat: -rw-r--r-- 14,949 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"""gnatcheck support for GPS

This plugin adds support for gnatcheck, a coding standard checker
"""

###########################################################################
# No user customization below this line
###########################################################################

import GPS
import os
import os.path
import re
import six
import traceback
import os_utils
from gi.repository import Gtk
import gps_utils
from gps_utils import interactive, hook
from gps_utils.gnatcheck_rules_editor import rulesEditor, get_supported_rules

gnatcheck = None


class rulesSelector(Gtk.Dialog):
    """
    Dialog used to select a coding standard file before launching gnatcheck.
    """

    def __init__(self, projectname, defaultfile):
        Gtk.Dialog.__init__(
            self,
            title="Select a coding standard file",
            parent=GPS.MDI.current().pywidget().get_toplevel(),
            flags=Gtk.DialogFlags.MODAL
        )

        # OK - Cancel buttons
        self.okButton = self.add_button('OK', Gtk.ResponseType.OK)
        self.cancelButton = self.add_button('Cancel', Gtk.ResponseType.CANCEL)

        label = Gtk.Label(
            label="No check switches are defined for project {}"
                  "\nPlease enter a coding standard file containing the"
                  " desired gnatcheck rules:".format(projectname))
        label.show()
        self.vbox.pack_start(label, False, False, 0)

        hbox = Gtk.HBox()
        hbox.show()
        self.vbox.pack_start(hbox, False, False, 0)

        self.fileEntry = Gtk.Entry()
        self.fileEntry.set_editable(True)
        self.fileEntry.show()
        hbox.pack_start(self.fileEntry, True, True, 0)

        if defaultfile is not None:
            self.fileEntry.set_text(defaultfile.path)
        self.fileEntry.connect('changed', self.on_file_entry_changed)
        self.on_file_entry_changed()

        button = Gtk.Button('Browse')
        button.connect('clicked', self.on_coding_standard_file_browse)
        button.show()
        hbox.pack_start(button, False, False, 0)

    def get_file(self):
        return GPS.File(self.fileEntry.get_text())

    def on_file_entry_changed(self, *args):
        """Callback when the file entry changed"""
        name = self.fileEntry.get_text()
        if name == "":
            self.okButton.set_sensitive(False)
        else:
            self.okButton.set_sensitive(True)

    def on_coding_standard_file_browse(self, *args):
        """Callback to coding standard 'Browse' button"""
        file = GPS.MDI.file_selector()
        if file.path != "":
            self.fileEntry.set_text(file.path)


class gnatCheckProc:

    """This class controls the gnatcheck execution"""

    def __init__(self):
        self.rules_file = None
        self.rules = None

        self.locations_string = "Coding Standard violations"
        self.gnatCmd = ""
        self.full_output = ""

        self.ruleseditor = None   # The GUI to edit rules

    def updateGnatCmd(self):
        self.gnatCmd = gps_utils.get_gnat_driver_cmd()

        if self.gnatCmd == "":
            self.gnatCmd = "gnat"

        if self.gnatCmd == "":
            GPS.Console("Messages").write(
                "Error: 'gnat' is not in the path.\n")
            GPS.Console("Messages").write(
                "Error: Could not initialize the gnatcheck module.\n")

    def onResponse(self, dialog, response_id):
        if response_id == Gtk.ResponseType.APPLY:
            fname = self.ruleseditor.get_filename()
            if fname != "":
                self.rules_file = fname

        self.ruleseditor.destroy()
        self.ruleseditor = None

    def edit(self):
        if self.ruleseditor:
            # Already opened
            return

        prev_cmd = self.gnatCmd
        self.updateGnatCmd()

        if self.gnatCmd == "":
            return

        # gnat check command changed: we reinitialize the rules list
        if prev_cmd != self.gnatCmd or self.rules is None:
            self.rules = get_supported_rules(self.gnatCmd)

        # we retrieve the coding standard file from the project
        for opt in GPS.Project.root().get_attribute_as_list(
            "default_switches", package="check", index="ada"
        ):
            res = re.split("^\-from\=(.*)$", opt)
            if len(res) > 1:
                self.rules_file = GPS.File(res[1])

        self.ruleseditor = rulesEditor(self.rules, self.rules_file)
        self.ruleseditor.connect('response', self.onResponse)

    def parse_output(self, msg):
        # gnatcheck sometimes displays incorrectly formatted warnings (not
        # handled by GPS correctly then)
        # let's reformat those here:
        # expecting "file.ext:nnn:nnn: msg"
        # receiving "file.ext:nnn:nnn msg"
        res = re.split("^([^:]*[:][0-9]+:[0-9]+)([^:0-9].*)$", msg)
        if len(res) > 3:
            msg = res[1] + ":" + res[2]
        GPS.Locations.parse(msg, self.locations_string)

        # Aggregate output in self.full_output: CodeFix needs to be looking at
        # the whole output in one go.
        self.full_output += msg + "\n"

    def on_match(self, process, matched, unmatched):
        if unmatched == "\n":
            GPS.Console("Messages").write(self.msg + unmatched)
            self.parse_output(self.msg)
            self.msg = ""
        self.msg += matched

    def on_exit(self, process, status, remaining_output):
        if self.msg != "":
            GPS.Console("Messages").write(self.msg)
            GPS.Locations.parse(self.msg, self.locations_string)
            self.parse_output(self.msg)
            self.msg = ""

        if self.full_output:
            # There is a full output: run CodeFix.
            GPS.Codefix.parse(self.locations_string, self.full_output)

    def on_spawn(self, filestr, project, recursive):
        """
        Spawn gnatcheck.
        Must be called only after we have set the rules file
        """

        self.updateGnatCmd()

        if self.gnatCmd == "":
            GPS.Console("Messages").write("Error: could not find gnatcheck")
            return

        # launch gnat check with specified project
        cmd = [self.gnatCmd, 'check',
               '-P', project.file().name("Tools_Server"),
               '-dd']   # progress

        # also analyse subprojects ?
        if recursive:
            cmd.append('-U')

        # define the scenario variables
        scenario = GPS.Project.scenario_variables()
        if scenario is not None:
            for i, j in six.iteritems (scenario):
                cmd.append('-X%s=%s' % (i, j))

        # now specify the files to check
        cmd.append(filestr)

        if self.rules_file:
            cmd.extend(
                ['-rules', '-from=%s' % self.rules_file.name("Tools_Server")])

        # clear the Checks category in the Locations view
        if GPS.Locations.list_categories().count(self.locations_string) > 0:
            GPS.Locations.remove_category(self.locations_string)

        self.msg = ""
        GPS.Process(
            cmd, "^.+$",
            on_match=self.on_match,
            on_exit=self.on_exit,
            progress_regexp="^ *completed (\d*) out of (\d*) .*$",
            progress_current=1,
            progress_total=2,
            remote_server="Tools_Server",
            show_command=True)

    def internalSpawn(self, filestr, project, recursive=False):
        if GPS.Preference('General-Auto-Save').get():
            # Force, since otherwise we get a modal dialog while within
            # a GPS action, which gtk+ doesn't like
            modified = GPS.Project.root().is_modified(recursive=True)
            GPS.MDI.save_all(force=True)
            if modified:
                GPS.Project.root().recompute()

        self.full_output = ""
        opts_project = project
        opts = opts_project.get_attribute_as_list(
            "switches", package="check", index="ada")
        if len(opts) == 0:
            opts = opts_project.get_attribute_as_list(
                "default_switches", package="check", index="ada")
        if len(opts) == 0:
            opts_project = GPS.Project.root()
            opts = opts_project.get_attribute_as_list(
                "switches", package="check", index="ada")
        if len(opts) == 0:
            opts = opts_project.get_attribute_as_list(
                "default_switches", package="check", index="ada")

        # We need a rules file if none was specified in the project.
        need_rules_file = True
        if len(opts) != 0:
            for opt in opts:
                res = re.split("^\-from\=(.*)$", opt)
                if len(res) > 1:
                    # we cd to the project's dir before creating the file,
                    # as this will then correctly resolve if the file is
                    # relative to the project's dir
                    olddir = GPS.pwd()
                    GPS.cd(opts_project.file().directory())
                    self.rules_file = GPS.File(res[1])
                    GPS.cd(olddir)
                    need_rules_file = False

        if need_rules_file:
            # Display a dialog, but without using run(), since we are
            # running a GPS action in the task manager and that would
            # crash GPS on some platforms

            selector = rulesSelector(project.name(), self.rules_file)

            def on_response(dialog, response_id):
                if response_id == Gtk.ResponseType.OK:
                    self.rules_file = selector.get_file()
                    dialog.destroy()
                    self.on_spawn(filestr, project, recursive)
                else:
                    dialog.destroy()

            selector.connect('response', on_response)
            selector.show_all()
        else:
            self.on_spawn(filestr, project, recursive)

    def check_project(self, project, recursive=False):
        try:
            self.internalSpawn("", project, recursive)
        except:
            GPS.Console("Messages").write(
                "Unexpected exception in gnatcheck.py:\n%s\n" % (
                    traceback.format_exc()))

    def check_file(self, file):
        try:
            self.internalSpawn(file.name("Tools_Server"), GPS.Project.root())
        except:
            GPS.Console("Messages").write(
                "Unexpected exception in gnatcheck.py:\n%s\n" % (
                    traceback.format_exc()))

    def check_files(self, files):
        try:
            filestr = ""
            for f in files:
                filestr += '"""' + f.name("Tools_Server") + '""" '
            self.internalSpawn(filestr, GPS.Project.root())
        except:
            GPS.Console("Messages").write(
                "Unexpected exception in gnatcheck.py:\n%s\n" % (
                    traceback.format_exc()))

# Contextual menu for checking files
# The filter does some computation, and caches the result in the context so
# that we do not need to recompute it if the action is executed


class __contextualMenuData(object):
    pass


def __contextualMenuFilter(context):
    global gnatcheckproc
    data = __contextualMenuData()
    context.gnatcheck = data

    data.desttype = "none"

    data.file = context.file()
    if data.file:
        data.desttype = "file"
        if data.file.language().lower() != "ada":
            return False

        # Does this file belong to the project tree ?
        return data.file.project(False) is not None

    data.dir = context.directory()
    if data.dir:
        data.desttype = "dir"
        # check this directory contains ada sources
        srcs = GPS.Project.root().sources(True)
        found = False
        data.files = []
        for f in srcs:
            filename = f.path
            if filename.find(data.dir) == 0:
                if f.language().lower() == "ada":
                    data.files.append(f)
                    found = True
        return found

    data.project = context.project()
    if data.project:
        data.desttype = "project"
        srcs = data.project.sources(recursive=False)
        found = False
        data.files = []
        for f in srcs:
            if f.language().lower() == "ada":
                data.files.append(f)
                found = True
        return found

    return False


def __contextualMenuLabel(context):
    data = context.gnatcheck
    if data.desttype == "file":
        fmt = "Check Coding standard of <b>{}</b>"
        name = os.path.basename(data.file.path)
    elif data.desttype == "dir":
        fmt = "Check Coding standard of files in <b>{}</b>"
        name = os.path.basename(os.path.dirname(data.dir))
    elif data.desttype == "project":
        fmt = "Check Coding standard of files in <b>{}</b>"
        name = data.project.name()
    else:
        return ""
    return fmt.format(os_utils.display_name(name))


@interactive(
    name='Check Coding Standard',
    contextual=__contextualMenuLabel,
    filter=__contextualMenuFilter,
    static_path="Check Coding standard")
def on_activate():
    context = GPS.contextual_context()
    data = context.gnatcheck
    global gnatcheckproc
    if data.desttype == "file":
        gnatcheckproc.check_file(data.file)
    elif data.desttype == "project":
        gnatcheckproc.check_project(data.project)
    else:
        gnatcheckproc.check_files(data.files)


# create the menus instances.

gnatcheckproc = gnatCheckProc()


@interactive(name='gnatcheck root project',
             category='Coding Standard')
def check_root_project():
    "Check coding standard of the root project"
    gnatcheckproc.check_project(GPS.Project.root())


@interactive(name='gnatcheck root project recursive',
             category='Coding Standard')
def check_root_project_recursive():
    "Check coding standard for the root project and its subprojects"
    gnatcheckproc.check_project(GPS.Project.root(), True)


@interactive(name='gnatcheck file',
             filter='Source editor',
             category='Coding Standard')
def check_file():
    "Check coding standard of the selected file"
    gnatcheckproc.check_file(GPS.EditorBuffer.get().file())


@interactive(name='edit gnatcheck rules',
             category='Coding Standard')
def edit_gnatcheck_rules():
    "Edit the coding standard file"
    gnatcheckproc.edit()


@hook('gps_started')
def __on_gps_started():
    GPS.parse_xml("""
  <tool name="GnatCheck" package="Check" index="Ada" override="false"
        attribute="Default_Switches">
     <language>Ada</language>
     <switches sections="-rules">
        <check label="process RTL units" switch="-a" line="1"/>
        <check label="debug mode" switch="-d" line="1"/>
        <field label="Coding standard file"
               switch="-from"
               separator="="
               as-file="true"
               line="1"
               section="-rules"/>
     </switches>
  </tool>""")