CCCC - C and C++ Code Counter  9999-git
CCCC Development version (post-3.1.4)
cccc_prj.cc
Go to the documentation of this file.
1 /*
2  CCCC - C and C++ Code Counter
3  Copyright (C) 1994-2005 Tim Littlefair (tim_littlefair@hotmail.com)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 // cccc_prj.cc
20 
21 // We have some debugging messages specifically for looking at how
22 // use relationships are being handled.
23 #define DEBUG_USEREL 0
24 
25 // implementation file for class CCCC_Project
26 
27 #include "cccc.h"
28 
29 #include "cccc_itm.h"
30 #include "cccc_prj.h"
31 #include "cccc_db.h"
32 
33 CCCC_Project::CCCC_Project(const string& name)
34 {
35  // we prime the database with knowledge of the builtin base types
36  // we also add a record for the anonymous class which we will treat
37  // as the parent of all non-member functions
38  const char *builtin_type_info[]=
39  {
40  "void@builtin@<nofile>@0@builtin definition@d?????@@0@d@",
41  "int@builtin@<nofile>@0@builtin definition@d?????@@0@d@",
42  "char@builtin@<nofile>@0@builtin definition@d?????@@0@d@",
43  "long@builtin@<nofile>@0@builtin definition@d?????@@0@d@",
44  "float@builtin@<nofile>@0@builtin definition@d?????@@0@d@",
45  "double@builtin@<nofile>@0@builtin definition@d?????@@0@d@",
46  NULL
47  };
48  for(const char **ptr=builtin_type_info; *ptr!=NULL; ptr++)
49  {
50  CCCC_Item type_info(*ptr);
51  add_module(type_info);
52  }
53 }
54 
55 
56 void CCCC_Project::add_module(CCCC_Item& module_line) {
57  char linebuf[1024];
58 
59  CCCC_Module *module_ptr=new CCCC_Module;
60  CCCC_Extent *extent_ptr=new CCCC_Extent;
61 
62  if(
63  module_line.Extract(module_ptr->module_name) &&
64  module_line.Extract(module_ptr->module_type) &&
65  extent_ptr->GetFromItem(module_line)
66  )
67  {
68  CCCC_Module *lookup_module_ptr=module_table.find_or_insert(module_ptr);
69  if(lookup_module_ptr != NULL)
70  {
71  lookup_module_ptr->extent_table.find_or_insert(extent_ptr);
72 
73  if(lookup_module_ptr!=module_ptr)
74  {
75  // do some work to transfer knowledge from the new module object
76  // then delete it
77  Resolve_Fields(lookup_module_ptr->module_type,module_ptr->module_type);
78  delete module_ptr;
79  }
80  }
81  }
82  else
83  {
84  cerr << "CCCC_Project::add_module_extent: extraction failed" << endl;
85  }
86 }
87 
88 void CCCC_Project::add_member(CCCC_Item& member_data_line)
89 {
90  CCCC_Module *new_module_ptr=new CCCC_Module;
91  CCCC_Member *new_member_ptr=new CCCC_Member;
92  if(
93  member_data_line.Extract(new_module_ptr->module_name) &&
94  member_data_line.Extract(new_member_ptr->member_name) &&
95  member_data_line.Extract(new_member_ptr->member_type) &&
96  member_data_line.Extract(new_member_ptr->param_list)
97  )
98  {
99  CCCC_Module *found_module_ptr=module_table.find_or_insert(new_module_ptr);
100  if(found_module_ptr==new_module_ptr)
101  {
102  // protect the new module from deletion at the end of this function
103  new_module_ptr=NULL;
104  }
105 
106  new_member_ptr->parent=found_module_ptr;
107  CCCC_Member *found_member_ptr=member_table.find_or_insert(new_member_ptr);
108  if(found_member_ptr==new_member_ptr)
109  {
110  new_member_ptr=NULL;
111  }
112  found_member_ptr->add_extent(member_data_line);
113  }
114  else
115  {
116  cerr << "CCCC_Project::add_module extraction failed" << endl;
117  }
118 
119  // clean up newly allocated records if they have not been accepted
120  // into the database
121  delete new_module_ptr;
122  delete new_member_ptr;
123 }
124 
125 void CCCC_Project::add_userel(CCCC_Item& userel_data_line) {
126  CCCC_UseRelationship *new_userel_ptr =
127  new CCCC_UseRelationship(userel_data_line);
128  CCCC_UseRelationship *lookup_userel_ptr =
129  userel_table.find_or_insert(new_userel_ptr);
130 
131  if(lookup_userel_ptr != NULL)
132  {
133  if(new_userel_ptr != lookup_userel_ptr)
134  {
135  delete new_userel_ptr;
136  }
137  lookup_userel_ptr->add_extent(userel_data_line);
138  }
139 #if DEBUG_USEREL
140  cerr << "Adding " << lookup_userel_ptr->client << " uses "
141  << lookup_userel_ptr->supplier << endl;
142 #endif
143 }
144 
146 {
147  CCCC_Extent *new_extent=new CCCC_Extent(rejected_data_line);
149 }
150 
152 {
153  CCCC_Member *member_ptr=member_table.first_item();
154  while(member_ptr!=NULL)
155  {
156  if(member_ptr->parent!=NULL)
157  {
158  CCCC_Module::member_map_t::value_type
159  new_pair(member_ptr->key(),member_ptr);
160  member_ptr->parent->member_map.insert(new_pair);
161  }
162  else
163  {
164  cerr << "Member " << member_ptr->key() << " has no parent"
165  << endl;
166  }
167 
168  CCCC_Extent *extent_ptr=member_ptr->extent_table.first_item();
169  while(extent_ptr!=NULL)
170  {
171  Visibility extent_visibility=extent_ptr->get_visibility();
172  Visibility member_visibility=member_ptr->get_visibility();
173 
174  if(member_ptr->visibility==vDONTKNOW)
175  {
176  member_ptr->visibility=extent_visibility;
177  }
178  else if(
179  (extent_visibility!=vDONTKNOW) &&
180  (member_visibility!=extent_visibility)
181  )
182  {
183  member_ptr->visibility=vINVALID;
184  }
185 
186  extent_ptr=member_ptr->extent_table.next_item();
187  }
188 
189  member_ptr=member_table.next_item();
190  }
191 
193  while(userel_ptr!=NULL)
194  {
195  CCCC_Module *supplier_ptr=new CCCC_Module;
196  supplier_ptr->module_name=userel_ptr->supplier;
197  CCCC_Module *found_supplier_ptr=
198  module_table.find_or_insert(supplier_ptr);
199  if(found_supplier_ptr!=supplier_ptr)
200  {
201  delete supplier_ptr;
202  supplier_ptr=found_supplier_ptr;
203  }
204 
205  CCCC_Module *client_ptr=new CCCC_Module;
206  client_ptr->module_name=userel_ptr->client;
207  CCCC_Module *found_client_ptr=module_table.find_or_insert(client_ptr);
208  if(found_client_ptr!=client_ptr)
209  {
210  delete client_ptr;
211  client_ptr=found_client_ptr;
212  }
213 
214  if(
215  (userel_ptr->supplier==userel_ptr->client) ||
216  userel_ptr->supplier=="" ||
217  userel_ptr->client=="" ||
218  supplier_ptr->is_trivial() ||
219  client_ptr->is_trivial()
220  )
221  {
222 #if DEBUG_USEREL
223  cerr << "Removing relationship between "
224  << userel_ptr->supplier.c_str()
225  << " and "
226  << userel_ptr->client.c_str()
227  << endl;
228 #endif
229  userel_table.remove(userel_ptr);
230  delete userel_ptr;
231  }
232  else
233  {
234  // create links from the client and supplier modules to the
235  // relationship object
236 #if DEBUG_USEREL
237  std::cerr << "Creating links for "
238  << client_ptr->key()
239  << " (" << client_ptr << ") uses "
240  << supplier_ptr->key()
241  << " (" << supplier_ptr << ")" << std::endl;
242 #endif
243 
244  CCCC_Module::relationship_map_t::value_type
245  new_supplier_pair(supplier_ptr->key(), userel_ptr),
246  new_client_pair(client_ptr->key(), userel_ptr);
247  client_ptr->supplier_map.insert(new_supplier_pair);
248  supplier_ptr->client_map.insert(new_client_pair);
249 
250  // calculate the visibility and concreteness of the
251  // relationship
252  AugmentedBool visible=abDONTKNOW;
253  AugmentedBool concrete=abDONTKNOW;
254 
255  CCCC_Extent *extent_ptr=userel_ptr->extent_table.first_item();
256  while(extent_ptr!=NULL)
257  {
258  switch(extent_ptr->get_visibility())
259  {
260  case vPRIVATE:
261  case vIMPLEMENTATION:
262  if(visible!=abTRUE)
263  {
264  visible=abFALSE;
265  }
266  break;
267  case vPROTECTED:
268  case vPUBLIC:
269  visible=abTRUE;
270  break;
271  default:
272  // nothing to do
273  ;
274  }
275 
276  switch(extent_ptr->get_usetype())
277  {
278  case utPARBYREF:
279  case utHASBYREF:
280  if(concrete!=abTRUE)
281  {
282  concrete=abFALSE;
283  }
284  break;
285 
286  case utINHERITS:
287  case utPARBYVAL:
288  case utHASBYVAL:
289  concrete=abTRUE;
290  break;
291 
292  default:
293  // nothing to do
294  ;
295  }
296 
297  extent_ptr=userel_ptr->extent_table.next_item();
298  }
299  userel_ptr->visible=visible;
300  userel_ptr->concrete=concrete;
301  }
302 
303  userel_ptr=userel_table.next_item();
304  }
305 }
306 
307 
308 int CCCC_Project::get_count(const char* count_tag)
309 {
310  int retval=0;
311  retval+=module_table.get_count(count_tag);
312  retval+=rejected_extent_table.get_count(count_tag);
313  return retval;
314 }
315 
316 
317 int CCCC_Project::ToFile(ofstream& ofstr)
318 {
319  // this function could be rewritten much more elegantly using
320  // STL output iterators, and one day will be ...
321 
322  int retval=FALSE;
323  CCCC_Module *module_ptr=module_table.first_item();
324  while(module_ptr!=NULL)
325  {
326  module_ptr->ToFile(ofstr);
327  module_ptr=module_table.next_item();
328  }
329 
330  CCCC_Member *member_ptr=member_table.first_item();
331  while(member_ptr!=NULL)
332  {
333  member_ptr->ToFile(ofstr);
334  member_ptr=member_table.next_item();
335  }
336 
338  while(userel_ptr!=NULL)
339  {
340  userel_ptr->ToFile(ofstr);
341  userel_ptr=userel_table.next_item();
342  }
343 
345  while(rejext_ptr!=NULL)
346  {
347  CCCC_Item extent_line;
348  extent_line.Insert(REJEXT_PREFIX);
349  rejext_ptr->AddToItem(extent_line);
350  extent_line.ToFile(ofstr);
351 
352  rejext_ptr=rejected_extent_table.next_item();
353  }
354 
355  if(ofstr.good())
356  {
357  retval=TRUE;
358  }
359 
360  return retval;
361 }
362 
363 
364 int CCCC_Project::FromFile(ifstream& ifstr)
365 {
366  int retval=FALSE;
367 
368  set_active_project(this);
369 
370  while(PeekAtNextLinePrefix(ifstr,MODULE_PREFIX))
371  {
372  CCCC_Module *new_module=new CCCC_Module;
373  int fromfile_status=new_module->FromFile(ifstr);
374  DisposeOfImportRecord(new_module,fromfile_status);
375  }
376 
377  while(PeekAtNextLinePrefix(ifstr,MEMBER_PREFIX))
378  {
379  CCCC_Member *new_member=new CCCC_Member;
380  int fromfile_status=new_member->FromFile(ifstr);
381  DisposeOfImportRecord(new_member,fromfile_status);
382  }
383 
384  while(PeekAtNextLinePrefix(ifstr,USEREL_PREFIX))
385  {
387  int fromfile_status=new_userel->FromFile(ifstr);
388  DisposeOfImportRecord(new_userel,fromfile_status);
389  }
390 
391  while(PeekAtNextLinePrefix(ifstr,REJEXT_PREFIX))
392  {
393  CCCC_Extent *new_rejext=new CCCC_Extent;
394  CCCC_Item next_line;
395  next_line.FromFile(ifstr);
396  int fromfile_status=RECORD_ERROR;
397  if(
398  new_rejext->GetFromItem(next_line) &&
399  new_rejext==rejected_extent_table.find_or_insert(new_rejext)
400  )
401  {
402  fromfile_status=RECORD_ADDED;
403  }
404  DisposeOfImportRecord(new_rejext,fromfile_status);
405  }
406 
407  set_active_project(NULL);
408 
409  return retval;
410 }
411 
412 string CCCC_Project::name(int level) const
413 {
414  return "";
415 }
416 
417 
418 
419 
420 
421 
422 
CCCC_Table< CCCC_Module > module_table
Definition: cccc_prj.h:54
Visibility get_visibility()
Definition: cccc_mem.cc:245
string param_list
Definition: cccc_mem.h:38
AugmentedBool concrete
Definition: cccc_use.h:39
void DisposeOfImportRecord(T *record_ptr, int fromfile_status)
Definition: cccc_db.cc:66
virtual string key() const
Definition: cccc_rec.cc:76
relationship_map_t client_map
Definition: cccc_mod.h:54
int is_trivial()
Definition: cccc_mod.cc:181
string member_type
Definition: cccc_mem.h:38
CCCC_Module * parent
Definition: cccc_mem.h:40
CCCC_Table< CCCC_Member > member_table
Definition: cccc_prj.h:55
bool Insert(const string &s)
Definition: cccc_itm.cc:31
Visibility visibility
Definition: cccc_mem.h:39
CCCC_Table< CCCC_Extent > rejected_extent_table
Definition: cccc_prj.h:57
static const string MODULE_PREFIX
Definition: cccc_mod.h:35
string module_name
Definition: cccc_mod.h:48
void add_rejected_extent(CCCC_Item &rejected_data_line)
Definition: cccc_prj.cc:145
friend class CCCC_Module
Definition: cccc_prj.h:49
friend class CCCC_Extent
Definition: cccc_prj.h:52
int FromFile(ifstream &infile)
Definition: cccc_mem.cc:148
bool remove(T *old_item_ptr)
Definition: cccc_tbl.cc:93
virtual int get_count(const char *count_tag)
Definition: cccc_tbl.cc:52
int FromFile(ifstream &infile)
Definition: cccc_prj.cc:364
bool Extract(string &s)
Definition: cccc_itm.cc:47
string module_type
Definition: cccc_mod.h:48
void reindex()
Definition: cccc_prj.cc:151
Visibility
Definition: cccc_utl.h:52
void add_module(CCCC_Item &module_data_line)
Definition: cccc_prj.cc:56
int ToFile(ofstream &outfile)
Definition: cccc_mem.cc:64
void add_extent(CCCC_Item &)
Definition: cccc_use.cc:68
T * first_item()
Definition: cccc_tbl.cc:118
UseType get_usetype() const
Definition: cccc_ext.h:57
int GetFromItem(CCCC_Item &item)
Definition: cccc_ext.cc:84
static const string REJEXT_PREFIX
Definition: cccc_prj.h:36
static const string MEMBER_PREFIX
Definition: cccc_mem.h:27
string name(int level) const
Definition: cccc_prj.cc:412
string member_name
Definition: cccc_mem.h:38
int ToFile(ofstream &outfile)
Definition: cccc_mod.cc:198
T * find_or_insert(T *new_item_ptr)
Definition: cccc_tbl.cc:78
bool FromFile(ifstream &ifstr)
Definition: cccc_itm.cc:119
void add_member(CCCC_Item &member_data_line)
Definition: cccc_prj.cc:88
CCCC_Table< CCCC_UseRelationship > userel_table
Definition: cccc_prj.h:56
T * next_item()
Definition: cccc_tbl.cc:124
Extent_Table extent_table
Definition: cccc_rec.h:45
static void set_active_project(CCCC_Project *prj)
Definition: cccc_rec.cc:29
int ToFile(ofstream &outfile)
Definition: cccc_prj.cc:317
int AddToItem(CCCC_Item &item)
Definition: cccc_ext.cc:64
int ToFile(ofstream &outfile)
Definition: cccc_use.cc:175
Visibility get_visibility() const
Definition: cccc_ext.h:55
friend class CCCC_Member
Definition: cccc_prj.h:50
relationship_map_t supplier_map
Definition: cccc_mod.h:55
int get_count(const char *count_tag)
Definition: cccc_prj.cc:308
void Resolve_Fields(string &field1, string &field2)
Definition: cccc_db.cc:105
AugmentedBool visible
Definition: cccc_use.h:39
CCCC_Project(const string &name="")
Definition: cccc_prj.cc:33
bool PeekAtNextLinePrefix(ifstream &ifstr, string pfx)
Definition: cccc_db.cc:51
member_map_t member_map
Definition: cccc_mod.h:51
void add_userel(CCCC_Item &use_data_line)
Definition: cccc_prj.cc:125
int FromFile(ifstream &infile)
Definition: cccc_use.cc:206
AugmentedBool
Definition: cccc_utl.h:59
static const string USEREL_PREFIX
Definition: cccc_use.h:29
int FromFile(ifstream &infile)
Definition: cccc_mod.cc:228
virtual void add_extent(CCCC_Item &)
Definition: cccc_rec.cc:63
friend class CCCC_UseRelationship
Definition: cccc_prj.h:51
bool ToFile(ofstream &ofstr)
Definition: cccc_itm.cc:112