xref: /illumos-gate/usr/src/tools/smatch/src/smatch_data/db/init_constraints.pl (revision 915894ef19890baaed00080f85f6b69e225cda98)
1#!/usr/bin/perl -w
2
3use strict;
4use warnings;
5use bigint;
6use DBI;
7use Data::Dumper;
8use File::Basename;
9use Try::Tiny;
10
11my $project = shift;
12$project =~ s/.*=(.*)/$1/;
13my $warns = shift;
14my $db_file = shift;
15
16sub preserve_existing_constraints()
17{
18    if (! -e "smatch_db.sqlite") {
19        return;
20    }
21
22    my $db = DBI->connect("dbi:SQLite:$db_file", "", "",);
23    $db->do('attach "smatch_db.sqlite" as old_db');
24    $db->do('insert into constraints select * from old_db.constraints');
25    $db->disconnect();
26}
27
28my $db;
29
30sub connect_to_db($)
31{
32    my $name = shift;
33
34    $db = DBI->connect("dbi:SQLite:$name", "", "", {AutoCommit => 0});
35
36    $db->do("PRAGMA cache_size = 800000");
37    $db->do("PRAGMA journal_mode = OFF");
38    $db->do("PRAGMA count_changes = OFF");
39    $db->do("PRAGMA temp_store = MEMORY");
40    $db->do("PRAGMA locking = EXCLUSIVE");
41}
42
43sub load_manual_constraints($$)
44{
45    my $full_path = shift;
46    my $project = shift;
47    my $dir = dirname($full_path);
48
49    if ($project =~ /^$/) {
50        return;
51    }
52
53    open(FILE, "$dir/$project.constraints");
54    while (<FILE>) {
55        s/\n//;
56        $db->do("insert or ignore into constraints (str) values ('$_')");
57    }
58    close(FILE);
59
60    open(FILE, "$dir/$project.constraints_required");
61    while (<FILE>) {
62        my $limit;
63        my $dummy;
64
65        ($dummy, $dummy, $limit) = split(/,/);
66        $limit =~ s/^ +//;
67        $limit =~ s/\n//;
68        try {
69            $db->do("insert or ignore into constraints (str) values ('$limit')");
70        } catch {}
71    }
72    close(FILE);
73
74    $db->commit();
75}
76
77preserve_existing_constraints();
78
79connect_to_db($db_file);
80load_manual_constraints($0, $project);
81
82$db->commit();
83$db->disconnect();
84