CCCC - C and C++ Code Counter  9999-git
CCCC Development version (post-3.1.4)
cccc_utl.h
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_utl.h
20 
21 #ifndef __CCCC_UTL_H
22 #define __CCCC_UTL_H
23 
24 #include "cccc.h"
25 #include <map>
26 #include <vector>
27 #include "cccc_tok.h"
28 #include "AParser.h"
29 
30 class ANTLRAbstractToken;
31 class ANTLRTokenPtr;
32 class CCCC_Item;
33 
34 // this file declares all enumeration datatypes used in the project, and
35 // also the parse state class, which is used to capture information in the
36 // parse and transfer it to the code database for later report generation
37 
38 // for each enumeration, a single character code is defined for each member
39 // these codes are shown in the inline comments
40 
41 // the enumerations are designed to support resolution of incomplete
42 // knowledge about several sections of code which relate to the same
43 // object to give the most complete picture available
44 
45 class AST;
46 
47 // the languages which can be parsed
48 // only C and C++ are implemented as yet
51 
52 enum Visibility {
55 };
56 ostream& operator << (ostream&, Visibility);
57 istream& operator >> (istream&, Visibility&);
58 
60  abFALSE='F', abTRUE='T', abDONTKNOW='?', abDONTCARE='X', abINVALID='*'
61 };
62 ostream& operator << (ostream& os, AugmentedBool ab);
63 istream& operator >> (istream& is, AugmentedBool& ab);
64 
65 enum UseType {
66  utDECLARATION='D', utDEFINITION='d', // of methods and classes
67  utINHERITS='I', // inheritance, including Java
68  // extends and implements relations
69  utHASBYVAL='H', utHASBYREF='h', // class data member
70  utPARBYVAL='P', utPARBYREF='p', // method parameter or return value
71  utVARBYVAL='V', utVARBYREF='v', // local variable within a method
72  utTEMPLATE_NAME='T', // typedef alias for a template
73  utTEMPLATE_TYPE='t', // type over which a template is
74  // instantiated
75  utINVOKES='i', // C function invocation
76  utREJECTED='r', // for extents rejected by the parser
77  utWITH='w', // Ada 'with' keyword context
79 };
80 
81 // the parse state object consists of a number of strings representing
82 // knowledge about the identification of the source code object currently
83 // being processed, a number of flags of type AugmentedBool, and
84 // items representing knowledge about the
85 // concerning the object's nature, and also its visibility
86 
87 enum PSString {
88  pssFILE, pssRULE, pssFLAGS, // the context of the parse
89  pssMODTYPE, pssMODULE, // the syntactic class and name of the module
90  pssUTYPE, // unqualified type of the current member
91  pssINDIR, // indirection associated with the type above
92  pssITYPE, // type qualified with indirection
93  pssMEMBER, pssPARAMS, // name, parameter list of a member
94  pssDESCRIPTION, // textual description of the relationship type
95  pssLAST // used to dimension the array
96 };
97 
98 enum PSFlag {
99  psfCONST, psfSTATIC, psfEXTERN, psfVIRTUAL, // AugmentedBool
100  psfVISIBILITY, // Visibility
101  psfLAST // used to dimension the array
102 };
104 
105 #define MAX_STACK_DEPTH 1000
106 
107 // I have moved some actions originally embedded within the C++ grammar
108 // out of the grammar into the class ParseUtility defined below, so that
109 // other grammars can use them as well for consistency and efficiency.
110 // The ParseUtility::resynchronize() method provides a standardised way
111 // of 1) resynchronising the parser, and 2) reporting the parse error
112 // which caused the problem. Unfortunately, to do the resynchronisation
113 // it requires access to protected functions of ANTLRParser.
114 // The class ANTLR_Assisted_Parser below is a hack to enable ParseUtility
115 // to violate the protection of the functions required: ParseUtility is
116 // passed a pointer to a real parser which is of a subclass of ANTLRParser,
117 // and casts it to this artificial subclass, so as to give ParseUtility
118 // friend rights and to access the protected functions.
119 // This hack is necessary because the class definition we need to affect
120 // is generated by PCCTS: I am not proud of it and if anyone can suggest
121 // a way of doing without modifying PCCTS or its support code, I will be
122 // very happy to hear about it.
124 {
126  friend class ParseUtility;
127 };
128 
129 // The parse utility class is intended to assist the parser in a number
130 // of ways. In earlier versions, this class had at least two distinct
131 // roles:
132 // 1) as a place for common functions which each parser might call
133 // for diagnostics, resynchronisation etc; and
134 // 2) as a general storage area for state which needs to be remembered
135 // for any length of time during the parsing process.
136 // The class ParseStore has been added to support the second role,
137 // and it is hoped that the amount of stored state can be reduced
138 // in the near future.
140 
141  public:
143  ~ParseUtility();
144 
145  // the following methods are used to service the standard tracein/traceout
146  // and syntax error reporting calls generated by PCCTS
147  void tracein(const char *rulename, int guessing, ANTLRAbstractToken *tok);
148  void traceout(const char *rulename, int guessing, ANTLRAbstractToken *tok);
149  void syn(_ANTLRTokenPtr tok, ANTLRChar *egroup, SetWordType *eset,
150  ANTLRTokenType etok, int k);
151 
152  // this method consolidates the text of the next n tokens of lookahead
153  string lookahead_text(int n);
154 
155  // this method searches for a string of tokens at the specified nesting
156  // depth from the specified token class, and uses them as a marker to
157  // resynchronise the parser
158  void resynchronize(
159  int initial_nesting, SetWordType *resync_token_class,
160  ANTLRTokenPtr& resync_token);
161 
162  // This utility function is used to create
163  // a composite scope name from a qualifier scope
164  // and a relative name.
165  string scopeCombine(const string& baseScope, const string& name);
166 
167  // Only one instance of this class should exist at any time.
168  // This method allows the parsers and lexers to access the instance.
170 
171  private:
173 
176  static int stack_depth;
179  static string stack_rules[MAX_STACK_DEPTH];
180 
181  // copy constructor and assignment operator are private to
182  // prevent unexpected copying
183  ParseUtility(const ParseUtility&);
184  const ParseUtility& operator=(const ParseUtility&);
185 };
186 
187  // LOC, COM and MVG are all counted by the lexical analyzer,
188  // but the counts must be apportioned after the parser has
189  // identified the extents of the various declarations and definitions
190  // they belong to.
191  // This is achieved by the lexer maintaining counts of each
192  // which are reported to the ParseUtility class on a line by line
193  // basis. ParseUtility uses this data to create a store which is
194  // used to apportion counts as the parser reports extents.
196 
197 
198 // The ParseStore class encapsulates all information storage
199 // requirements related to the parser, and also manages
200 // the process of feeding that information to the database
201 // when it is complete.
202 // In particular, the class is responsible for receiving and
203 // retaining counts of the lexical metrics (LOC, COM,
204 // MVG) on a line-by-line basis. These are counted in the
205 // lexical analyzer, and the line-by-line counts must be
206 // integrated to allocate the counts to the extents identified
207 // by the parser as belonging to significant declarations and
208 // definitions.
210 {
211  public:
212  ParseStore(const string& filename);
213  ~ParseStore();
214 
216  void endOfLine(int line);
217 
218 
219  // each of the functions below writes one or more records into
220  // the database of code
221  void record_module_extent(int startLine, int endLine,
222  const string& moduleName,
223  const string& moduleType,
224  const string& description,
225  UseType ut);
226  void record_function_extent(int startLine, int endLine,
227  const string& returnType,
228  const string& moduleName,
229  const string& memberName,
230  const string& paramList,
231  const string& description,
232  Visibility visibility,
233  UseType ut);
234  void record_userel_extent(int startLine, int endLine,
235  const string& clientName,
236  const string& memberName,
237  const string& serverName,
238  const string& description,
239  Visibility visibility,
240  UseType ut);
241  void record_other_extent(int startLine, int endLine,
242  const string& description);
243  void record_file_balance_extent(string);
244 
245  // Each of the record_XXX methods above uses this function to
246  // add an extent record.
247  void insert_extent(CCCC_Item&, int, int,
248  const string&, const string&,
249  UseType, bool allocate_lexcounts);
250 
251  // the class maintains a number of strings and flags which reflect
252  // the most recently recognized module, member, type (with and without
253  // indirection) etc, and the visibility of items occuring at the current
254  // context
255  int get_flag(PSFlag) const;
256  void set_flag(PSFlag,int);
257  void set_flag(Visibility);
259  string filename();
260 
261  char *flags() { return &(*flag.begin()); }
262 
263  // We also need the automatically generated copy constructor
264  // and assignment operator to allow us to save state in the
265  // parser.
266 
267  // Only one instance of this class should exist at any time.
268  // This method allows the parsers and lexers to access the instance.
270  private:
272 
273  string theFilename;
274 
275  typedef std::vector<int> LexicalCountArray;
276  LexicalCountArray pendingLexicalCounts;
277 
278  typedef std::map<int,LexicalCountArray> LineLexicalCountMatrix;
279  LineLexicalCountMatrix lineLexicalCounts;
280 
281  typedef std::vector<char> CharArray;
282  CharArray flag;
283 
284  // copy constructor and assignment operator are private to
285  // prevent unexpected copying
286  ParseStore(const ParseStore&);
287  const ParseStore& operator=(const ParseStore&);
288 };
289 
290 #endif
291 
292 
293 
294 
295 
296 
297 
298 
299 
300 
Language
Definition: cccc_utl.h:49
LexicalCount
Definition: cccc_utl.h:195
ostream & operator<<(ostream &, Visibility)
Definition: cccc_utl.cc:79
istream & operator>>(istream &, Visibility &)
Definition: cccc_utl.cc:84
std::vector< int > LexicalCountArray
Definition: cccc_utl.h:275
Language file_language
static int stack_depth
Definition: cccc_utl.h:176
ANTLR_Assisted_Parser(ANTLRParser &parser)
Definition: cccc_utl.h:125
LineLexicalCountMatrix lineLexicalCounts
Definition: cccc_utl.h:279
LexicalCountArray pendingLexicalCounts
Definition: cccc_utl.h:276
string scopeCombine(const string &baseScope, const string &name)
Definition: cccc_utl.cc:207
void record_file_balance_extent(string)
ParseUtility(ANTLRParser *parser)
Definition: cccc_utl.cc:183
ANTLR_Assisted_Parser * parser
Definition: cccc_utl.h:174
void resynchronize(int initial_nesting, SetWordType *resync_token_class, ANTLRTokenPtr &resync_token)
Definition: cccc_utl.cc:115
void record_module_extent(int startLine, int endLine, const string &moduleName, const string &moduleType, const string &description, UseType ut)
Definition: cccc_utl.cc:349
void endOfLine(int line)
Definition: cccc_utl.cc:587
const ParseUtility & operator=(const ParseUtility &)
Visibility
Definition: cccc_utl.h:52
UseType
Definition: cccc_utl.h:65
void traceout(const char *rulename, int guessing, ANTLRAbstractToken *tok)
Definition: cccc_utl.cc:520
PSVerbosity
Definition: cccc_utl.h:103
static string stack_rules[MAX_STACK_DEPTH]
Definition: cccc_utl.h:179
int get_flag(PSFlag) const
Definition: cccc_utl.cc:261
static ParseStore * currentInstance()
Definition: cccc_utl.h:269
string lookahead_text(int n)
Definition: cccc_utl.cc:99
PSString
Definition: cccc_utl.h:87
void set_flag(PSFlag, int)
Definition: cccc_utl.cc:265
CharArray flag
Definition: cccc_utl.h:282
string filename()
Definition: cccc_utl.cc:281
Definition: cccc_utl.h:49
void insert_extent(CCCC_Item &, int, int, const string &, const string &, UseType, bool allocate_lexcounts)
Definition: cccc_utl.cc:288
static ParseUtility * currentInstance()
Definition: cccc_utl.h:169
std::vector< char > CharArray
Definition: cccc_utl.h:281
int trace_depth
Definition: cccc_utl.h:175
Language global_language
char * flags()
Definition: cccc_utl.h:261
static ParseUtility * theCurrentInstance
Definition: cccc_utl.h:172
void IncrementCount(LexicalCount lc)
Definition: cccc_utl.h:215
ParseStore(const string &filename)
Definition: cccc_utl.cc:234
void tracein(const char *rulename, int guessing, ANTLRAbstractToken *tok)
Definition: cccc_utl.cc:501
void record_other_extent(int startLine, int endLine, const string &description)
Definition: cccc_utl.cc:452
void record_function_extent(int startLine, int endLine, const string &returnType, const string &moduleName, const string &memberName, const string &paramList, const string &description, Visibility visibility, UseType ut)
Definition: cccc_utl.cc:368
Definition: cccc_utl.h:49
void record_userel_extent(int startLine, int endLine, const string &clientName, const string &memberName, const string &serverName, const string &description, Visibility visibility, UseType ut)
Definition: cccc_utl.cc:405
PSFlag
Definition: cccc_utl.h:98
const ParseStore & operator=(const ParseStore &)
static int stack_tokenline[MAX_STACK_DEPTH]
Definition: cccc_utl.h:178
static ParseStore * theCurrentInstance
Definition: cccc_utl.h:271
Visibility get_visibility()
Definition: cccc_utl.cc:276
static string stack_tokentext[MAX_STACK_DEPTH]
Definition: cccc_utl.h:177
void syn(_ANTLRTokenPtr tok, ANTLRChar *egroup, SetWordType *eset, ANTLRTokenType etok, int k)
Definition: cccc_utl.cc:549
string theFilename
Definition: cccc_utl.h:273
AugmentedBool
Definition: cccc_utl.h:59
std::map< int, LexicalCountArray > LineLexicalCountMatrix
Definition: cccc_utl.h:278
Definition: cccc_utl.h:49
ANTLRTokenType
Definition: cccc_tok.h:37
#define MAX_STACK_DEPTH
Definition: cccc_utl.h:105