Skip to content

Commit

Permalink
Analyze macro parameters for non-object values
Browse files Browse the repository at this point in the history
(WIP)
  • Loading branch information
dspinellis committed Jul 28, 2024
1 parent b6ba0c3 commit deb53eb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ Call::contains(Eclass *e) const
return false;
}

void
populate_namers()
{
for (Call::const_fmap_iterator_type i = Call::fbegin(); i != Call::fend(); i++) {
}
}

void
Call::clear_visit_flags()
{
Expand Down
2 changes: 1 addition & 1 deletion src/call.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Pltoken;
class Call {
private:

// Container for storing all declared functions
// Container for storing called and calling functions
typedef set <Call *> fun_container;
/*
* When processing the program a Call * is stored with each Id
Expand Down
69 changes: 64 additions & 5 deletions src/cscout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ file_analyze(Fileid fi)
const string &fname = fi.get_path();
int line_number = 0;


FCallSet &fc = Filedetails::get_functions(fi); // File's functions
FCallSet::iterator fci = fc.begin(); // Iterator through them
Call *cfun = NULL; // Current function
Expand All @@ -297,6 +298,15 @@ file_analyze(Fileid fi)
perror(fname.c_str());
exit(1);
}

bool analyze_param = false; // True if analyzing macro parameters
int bracket_nesting = 0; // Nesting during the analysis
int non_obj_param = 0; // Number of non-object parameters
// Check this or next parameter token for prohibited values
bool check_param_token = false;
bool check_next_param_token = false;
vector <Eclass *> macro_name_ecs;

// Go through the file character by character
for (;;) {
Tokid ti;
Expand All @@ -318,6 +328,7 @@ file_analyze(Fileid fi)
fun_nesting.pop();
}
}
// See if entering a new function
if (fci != fc.end() && ti >= (*fci)->get_begin().get_tokid()) {
if (cfun)
fun_nesting.push(cfun);
Expand All @@ -328,6 +339,36 @@ file_analyze(Fileid fi)
char c = (char)val;
mapTokidEclass::iterator ei;
enum e_cfile_state cstate = Filedetails::get_pre_cpp_metrics(fi).get_state();

/*
* Analyze macro parameters for arguments that make them
* unsuitable for converting into a C function.
*/
if (analyze_param &&
cstate != s_block_comment &&
cstate != s_string &&
cstate != s_cpp_comment) {
// cout << "Got " << c << "\n";
if (check_next_param_token)
check_param_token = true;
check_next_param_token = false;
if (c == '(') {
if (bracket_nesting == 0)
check_next_param_token = true;
bracket_nesting++;
} else if (c == ')') {
bracket_nesting--;
if (bracket_nesting == 0) {
analyze_param = false;
Call* macro = Call::get_macro(macr_name_ecs);
if (macro)
macro->get_pre_cpp_metrics().add_metric(FunMetrics::em_nnoparam, non_obj_param);

} else if (c == ',' && bracket_nesting == 1)
check_next_param_token = true;
}

// Mark identifiers
if (cstate != s_block_comment &&
cstate != s_string &&
cstate != s_cpp_comment &&
Expand All @@ -343,15 +384,17 @@ file_analyze(Fileid fi)
continue;
}
}

string s(1, c);
int len = ec->get_len();
for (int j = 1; j < len; j++)
s += (char)in.get();

// Identifiers we can mark
if (ec->is_identifier()) {
// Update metrics
id_msum.add_pre_cpp_id(ec);
// Add to the map
string s(1, c);
int len = ec->get_len();
for (int j = 1; j < len; j++)
s += (char)in.get();
Filedetails::get_pre_cpp_metrics(fi).process_identifier(s, ec);
if (cfun)
cfun->get_pre_cpp_metrics().process_identifier(s, ec);
Expand All @@ -367,7 +410,10 @@ file_analyze(Fileid fi)
has_unused = true;
else
; // TODO fi.set_associated_files(ec);
continue;

if (check_param_token
&& ec->get_attribute(is_macro)) {

} else {
/*
* This equivalence class is not needed.
Expand All @@ -377,7 +423,20 @@ file_analyze(Fileid fi)
*/
ec->remove_from_tokid_map();
delete ec;
ec = NULL;
}


if (ec && !analyze_param
&& ec->get_attribute(is_macro)) {
cout << "Macro " << *ec << "\n";
analyze_param = true;
bracket_nesting = 0;
non_obj_param = 0;
macro_ecs.clear();
}
if (ec && analyze_param)
macro_name_ecs.push_back(ec);
}
Filedetails::get_pre_cpp_metrics(fi).process_char((char)val);
if (cfun)
Expand Down
9 changes: 9 additions & 0 deletions src/ctoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ parse_lex_real()
}
}

int
Ctoken::lookup_keyword(const string& s)
{
auto ik = keymap.find(s);
if (ik == keymap.end())
return -1;
return ik->second;
}

// Lexical analysis function for yacc
// Used for debugging
int
Expand Down
4 changes: 4 additions & 0 deletions src/ctoken.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class Ctoken: public Token {
Ctoken() {}
Ctoken(Pdtoken& t) :
Token(t) {};
/*
* Return the string's keyword token value or -1 if the string
* isn't a keyword.
*/
static int lookup_keyword(const string& s);
void getnext();
};
Expand Down
2 changes: 2 additions & 0 deletions src/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ class Metrics {
// Return metric i (by lookup or calculation)
double get_metric(int n) const;
void set_metric(int n, int val) { count[n] = val; }

void add_metric(int n, int val) { count[n] += val; }
void summarize_operators();

// Update the maxumum level of statement nesting
Expand Down

0 comments on commit deb53eb

Please sign in to comment.