Skip to content

Commit

Permalink
Add option to specify which SQL tables to generate
Browse files Browse the repository at this point in the history
  • Loading branch information
dspinellis committed Jul 12, 2024
1 parent 8fffc46 commit f23da7c
Show file tree
Hide file tree
Showing 7 changed files with 514 additions and 344 deletions.
14 changes: 12 additions & 2 deletions man/cscout.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH CSCOUT 1 "25 April 2024"
.TH CSCOUT 1 "12 July 2024"
.\"
.\" (C) Copyright 2003-2024 Diomidis Spinellis
.\"
Expand Down Expand Up @@ -29,6 +29,7 @@ cscout \- C code analyzer and refactoring browser
[\fB\-p\fP \fIport\fP]
[\fB\-R\fP \fIspecification\fP]
[\fB\-m\fP \fIspecification\fP]
[\fB\-t\fP \fIsname\fP]
[\fB\-o\fP | \fB\-s\fP \fIdb\fP]
\fIfile\fR
.SH DESCRIPTION
Expand Down Expand Up @@ -239,6 +240,15 @@ dot -Tsvg >cgraph.svg
.fi
.DE

.IP "\fB\-t\fP \fIname\fP"
Generate SQL output for the named table.
By default SQL output for all tables is generated.
If this option is provided, then output only for the specified tables
will be generated.
It is the user's responsibility to list the tables required to avoid
breaking integrity constraints.
The option can be specified multiple times.

.IP "\fB\-o\fP"
Create obfuscated versions of all the writable files of the workspace.
.PP
Expand Down Expand Up @@ -325,4 +335,4 @@ We are now ready to serve you at http://localhost:8081
.SH "SEE ALSO"
cswc(1)
.SH AUTHOR
(c) Copyright 2003-2015 Diomidis Spinellis.
(c) Copyright 2003-2024 Diomidis Spinellis.
50 changes: 27 additions & 23 deletions src/call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "mcall.h"
#include "eclass.h"
#include "sql.h"
#include "workdb.h"

// Function currently being parsed
Call *Call::current_fun = NULL;
Expand Down Expand Up @@ -253,19 +254,20 @@ Call::dumpSql(Sql *db, ostream &of)
for (const_fmap_iterator_type i = fbegin(); i != fend(); i++) {
Call *fun = i->second;
Tokid t = fun->get_site();
of << "INSERT INTO FUNCTIONS VALUES(" <<
ptr_offset(fun) << ", '" <<
fun->name << "', " <<
db->boolval(fun->is_macro()) << ',' <<
db->boolval(fun->is_defined()) << ',' <<
db->boolval(fun->is_declared()) << ',' <<
db->boolval(fun->is_file_scoped()) << ',' <<
t.get_fileid().get_id() << ',' <<
(unsigned)(t.get_streampos()) << ',' <<
fun->get_num_caller();
of << ");\n";
if (table_is_enabled(t_functions))
of << "INSERT INTO FUNCTIONS VALUES("
<< ptr_offset(fun) << ", '"
<< fun->name << "', "
<< db->boolval(fun->is_macro()) << ','
<< db->boolval(fun->is_defined()) << ','
<< db->boolval(fun->is_declared()) << ','
<< db->boolval(fun->is_file_scoped()) << ','
<< t.get_fileid().get_id() << ','
<< (unsigned)(t.get_streampos()) << ','
<< fun->get_num_caller()
<< ");\n";

if (fun->is_defined()) {
if (fun->is_defined() && table_is_enabled(t_functionmetrics)) {
of << "INSERT INTO FUNCTIONMETRICS VALUES(" << ptr_offset(fun);
for (int j = 0; j < FunMetrics::metric_max; j++)
if (!Metrics::is_internal<FunMetrics>(j))
Expand All @@ -284,10 +286,11 @@ Call::dumpSql(Sql *db, ostream &of)
int pos = 0;
while (pos < len) {
Eclass *ec = t2.get_ec();
of << "INSERT INTO FUNCTIONID VALUES(" <<
ptr_offset(fun) << ',' <<
ord << ',' <<
ptr_offset(ec) << ");\n";
if (table_is_enabled(t_functionid))
of << "INSERT INTO FUNCTIONID VALUES("
<< ptr_offset(fun) << ','
<< ord << ','
<< ptr_offset(ec) << ");\n";
pos += ec->get_len();
t2 += ec->get_len();
ord++;
Expand All @@ -297,11 +300,12 @@ Call::dumpSql(Sql *db, ostream &of)
}

// Then their calls to satisfy integrity constraints
for (const_fmap_iterator_type i = fbegin(); i != fend(); i++) {
Call *fun = i->second;
for (Call::const_fiterator_type dest = fun->call_begin(); dest != fun->call_end(); dest++)
of << "INSERT INTO FCALLS VALUES(" <<
ptr_offset(fun) << ',' <<
ptr_offset(*dest) << ");\n";
}
if (table_is_enabled(t_fcalls))
for (const_fmap_iterator_type i = fbegin(); i != fend(); i++) {
Call *fun = i->second;
for (Call::const_fiterator_type dest = fun->call_begin(); dest != fun->call_end(); dest++)
of << "INSERT INTO FCALLS VALUES(" <<
ptr_offset(fun) << ',' <<
ptr_offset(*dest) << ");\n";
}
}
13 changes: 9 additions & 4 deletions src/cscout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3276,7 +3276,7 @@ main(int argc, char *argv[])
vector<string> call_graphs;
Debug::db_read();

while ((c = getopt(argc, argv, "3bCcd:rvE:P:p:m:l:os:R:" PICO_QL_OPTIONS)) != EOF)
while ((c = getopt(argc, argv, "3bCcd:rvE:P:p:m:l:oR:s:t:" PICO_QL_OPTIONS)) != EOF)
switch (c) {
case '3':
Fchar::enable_trigraphs();
Expand Down Expand Up @@ -3367,6 +3367,11 @@ main(int argc, char *argv[])
process_mode = pm_database;
db_engine = strdup(optarg);
break;
case 't':
if (!optarg)
usage(argv[0]);
table_enable(optarg);
break;
case 'R':
if (!optarg)
usage(argv[0]);
Expand Down Expand Up @@ -3396,7 +3401,7 @@ main(int argc, char *argv[])
parse_acl();
}

if (db_engine) {
if (process_mode == pm_database) {
if (!Sql::setEngine(db_engine))
return 1;
cout << Sql::getInterface()->begin_commands();
Expand Down Expand Up @@ -3474,7 +3479,7 @@ main(int argc, char *argv[])
if (DP())
cout << "Size " << file_msum.get_total(Metrics::em_nchar) << endl;

if (Sql::getInterface()) {
if (process_mode == pm_database) {
workdb_rest(Sql::getInterface(), cout);
Call::dumpSql(Sql::getInterface(), cout);
cout << Sql::getInterface()->end_commands();
Expand Down Expand Up @@ -3660,7 +3665,7 @@ garbage_collect(Fileid root)
for (set <Fileid>::const_iterator i = touched_files.begin(); i != touched_files.end(); i++)
if (*i != root && *i != input_file_id)
root.includes(*i, /* directly included (conservatively) */ false, i->required());
if (Sql::getInterface())
if (process_mode == pm_database)
Fdep::dumpSql(Sql::getInterface(), root);
Fdep::reset();

Expand Down
66 changes: 36 additions & 30 deletions src/fdep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
#include "fileid.h"
#include "tokid.h"
#include "fdep.h"
#include "workdb.h"
#include "sql.h"
#include "workdb.h"

/*
* These are serially set for each processed file, and
Expand Down Expand Up @@ -105,37 +107,41 @@ Fdep::reset()
void
Fdep::dumpSql(Sql *db, Fileid cu)
{
for (FSFMap::const_iterator di = definers.begin(); di != definers.end(); di++) {
const set <Fileid> &defs = di->second;
for (set <Fileid>::const_iterator i = defs.begin(); i != defs.end(); i++)
cout << "INSERT INTO DEFINERS VALUES(" <<
Project::get_current_projid() << ',' <<
cu.get_id() << ',' <<
di->first.get_id() << ',' <<
i->get_id() << ");\n";
}
for (FSFMap::const_iterator ii = includers.begin(); ii != includers.end(); ii++) {
const set <Fileid> &incs = ii->second;
for (set <Fileid>::const_iterator i = incs.begin(); i != incs.end(); i++)
cout << "INSERT INTO INCLUDERS VALUES(" <<
if (table_is_enabled(t_definers))
for (FSFMap::const_iterator di = definers.begin(); di != definers.end(); di++) {
const set <Fileid> &defs = di->second;
for (set <Fileid>::const_iterator i = defs.begin(); i != defs.end(); i++)
cout << "INSERT INTO DEFINERS VALUES(" <<
Project::get_current_projid() << ',' <<
cu.get_id() << ',' <<
di->first.get_id() << ',' <<
i->get_id() << ");\n";
}
if (table_is_enabled(t_includers))
for (FSFMap::const_iterator ii = includers.begin(); ii != includers.end(); ii++) {
const set <Fileid> &incs = ii->second;
for (set <Fileid>::const_iterator i = incs.begin(); i != incs.end(); i++)
cout << "INSERT INTO INCLUDERS VALUES(" <<
Project::get_current_projid() << ',' <<
cu.get_id() << ',' <<
ii->first.get_id() << ',' <<
i->get_id() << ");\n";
}
if (table_is_enabled(t_providers))
for (set <Fileid>::const_iterator i = providers.begin(); i != providers.end(); i++)
cout << "INSERT INTO PROVIDERS VALUES(" <<
Project::get_current_projid() << ',' <<
cu.get_id() << ',' <<
ii->first.get_id() << ',' <<
i->get_id() << ");\n";
}
for (set <Fileid>::const_iterator i = providers.begin(); i != providers.end(); i++)
cout << "INSERT INTO PROVIDERS VALUES(" <<
Project::get_current_projid() << ',' <<
cu.get_id() << ',' <<
i->get_id() << ");\n";
for (ITMap::const_iterator i = include_triggers.begin(); i != include_triggers.end(); i++)
for (include_trigger_value::const_iterator j = i->second.begin(); j != i->second.end(); j++) {
cout << "INSERT INTO INCTRIGGERS VALUES(" <<
Project::get_current_projid() << ',' <<
cu.get_id() << ',' <<
i->first.second.get_id() << ',' <<
i->first.first.get_id() << ',' <<
(unsigned)(j->first) << ',' <<
j->second << ");\n";
}
if (table_is_enabled(t_inctriggers))
for (ITMap::const_iterator i = include_triggers.begin(); i != include_triggers.end(); i++)
for (include_trigger_value::const_iterator j = i->second.begin(); j != i->second.end(); j++) {
cout << "INSERT INTO INCTRIGGERS VALUES(" <<
Project::get_current_projid() << ',' <<
cu.get_id() << ',' <<
i->first.second.get_id() << ',' <<
i->first.first.get_id() << ',' <<
(unsigned)(j->first) << ',' <<
j->second << ");\n";
}
}
49 changes: 26 additions & 23 deletions src/globobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,20 @@ GlobObj::dumpSql(Sql *db, ostream &of)
for (const_fmap_iterator_type i = fbegin(); i != fend(); i++) {
Call *fun = i->second;
Tokid t = fun->get_site();
of << "INSERT INTO FUNCTIONS VALUES(" <<
ptr_offset(fun) << ", '" <<
fun->name << "', " <<
db->boolval(fun->is_macro()) << ',' <<
db->boolval(fun->is_defined()) << ',' <<
db->boolval(fun->is_declared()) << ',' <<
db->boolval(fun->is_file_scoped()) << ',' <<
t.get_fileid().get_id() << ',' <<
(unsigned)(t.get_streampos()) << ',' <<
fun->get_num_caller();
of << ");\n";
if (table_is_enabled(t_functions))
of << "INSERT INTO FUNCTIONS VALUES(" <<
ptr_offset(fun) << ", '" <<
fun->name << "', " <<
db->boolval(fun->is_macro()) << ',' <<
db->boolval(fun->is_defined()) << ',' <<
db->boolval(fun->is_declared()) << ',' <<
db->boolval(fun->is_file_scoped()) << ',' <<
t.get_fileid().get_id() << ',' <<
(unsigned)(t.get_streampos()) << ',' <<
fun->get_num_caller();
of << ");\n";

if (fun->is_defined()) {
if (fun->is_defined() && table_is_enabled(t_functionmetrics)) {
of << "INSERT INTO FUNCTIONMETRICS VALUES(" << ptr_offset(fun);
for (int j = 0; j < FunMetrics::metric_max; j++)
if (!Metrics::is_internal<FunMetrics>(j))
Expand All @@ -149,10 +150,11 @@ GlobObj::dumpSql(Sql *db, ostream &of)
int pos = 0;
while (pos < len) {
Eclass *ec = t2.get_ec();
of << "INSERT INTO FUNCTIONID VALUES(" <<
ptr_offset(fun) << ',' <<
ord << ',' <<
ptr_offset(ec) << ");\n";
if (table_is_enabled(t_functionid))
of << "INSERT INTO FUNCTIONID VALUES(" <<
ptr_offset(fun) << ',' <<
ord << ',' <<
ptr_offset(ec) << ");\n";
pos += ec->get_len();
t2 += ec->get_len();
ord++;
Expand All @@ -162,12 +164,13 @@ GlobObj::dumpSql(Sql *db, ostream &of)
}

// Then their calls to satisfy integrity constraints
for (const_fmap_iterator_type i = fbegin(); i != fend(); i++) {
Call *fun = i->second;
for (Call::const_fiterator_type dest = fun->call_begin(); dest != fun->call_end(); dest++)
of << "INSERT INTO FCALLS VALUES(" <<
ptr_offset(fun) << ',' <<
ptr_offset(*dest) << ");\n";
}
if (table_is_enabled(t_fcalls))
for (const_fmap_iterator_type i = fbegin(); i != fend(); i++) {
Call *fun = i->second;
for (Call::const_fiterator_type dest = fun->call_begin(); dest != fun->call_end(); dest++)
of << "INSERT INTO FCALLS VALUES(" <<
ptr_offset(fun) << ',' <<
ptr_offset(*dest) << ");\n";
}
}
#endif /* TODO */
Loading

0 comments on commit f23da7c

Please sign in to comment.