Code::Blocks  SVN r11506
token.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision: 10205 $
6  * $Id: token.cpp 10205 2015-04-11 05:53:28Z ollydbg $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/plugins/codecompletion/parser/token.cpp $
8  */
9 
10 #include <wx/event.h>
11 #include <wx/string.h>
12 
13 #include "tokentree.h"
14 
15 #include "token.h"
16 
17 #define CC_TOKEN_DEBUG_OUTPUT 0
18 
19 #if defined(CC_GLOBAL_DEBUG_OUTPUT)
20  #if CC_GLOBAL_DEBUG_OUTPUT == 1
21  #undef CC_TOKEN_DEBUG_OUTPUT
22  #define CC_TOKEN_DEBUG_OUTPUT 1
23  #elif CC_GLOBAL_DEBUG_OUTPUT == 2
24  #undef CC_TOKEN_DEBUG_OUTPUT
25  #define CC_TOKEN_DEBUG_OUTPUT 2
26  #endif
27 #endif
28 
29 #if CC_TOKEN_DEBUG_OUTPUT == 1
30  #define TRACE(format, args...) \
31  CCLogger::Get()->DebugLog(F(format, ##args))
32  #define TRACE2(format, args...)
33 #elif CC_TOKEN_DEBUG_OUTPUT == 2
34  #define TRACE(format, args...) \
35  do \
36  { \
37  if (g_EnableDebugTrace) \
38  CCLogger::Get()->DebugLog(F(format, ##args)); \
39  } \
40  while (false)
41  #define TRACE2(format, args...) \
42  CCLogger::Get()->DebugLog(F(format, ##args))
43 #else
44  #define TRACE(format, args...)
45  #define TRACE2(format, args...)
46 #endif
47 
48 Token::Token(const wxString& name, unsigned int file, unsigned int line, size_t ticket) :
49  m_Name(name),
50  m_FileIdx(file),
51  m_Line(line),
52  m_ImplFileIdx(0),
53  m_ImplLine(0),
54  m_ImplLineStart(0),
55  m_ImplLineEnd(0),
56  m_Scope(tsUndefined),
57  m_TokenKind(tkUndefined),
58  m_IsOperator(false),
59  m_IsLocal(false),
60  m_IsTemp(false),
61  m_IsConst(false),
62  m_IsNoExcept(false),
63  m_IsAnonymous(false),
64  m_Index(-1),
65  m_ParentIndex(-1),
66  m_UserData(0),
67  m_TokenTree(0),
68  m_Ticket(ticket)
69 {
70  //ctor
71 }
72 
74 {
75  //dtor
76  m_TemplateMap.clear();
77  m_TemplateType.clear();
78 }
79 
81 {
82  wxString result;
83  if (m_TokenKind == tkClass)
84  return result << _T("class ") << m_Name << m_BaseArgs << _T(" {...}");
85  else if (m_TokenKind == tkNamespace)
86  return result << _T("namespace ") << m_Name << _T(" {...}");
87  else if (m_TokenKind == tkEnum)
88  return result << _T("enum ") << m_Name << _T(" {...}");
89  else if (m_TokenKind == tkTypedef)
90  {
91  result << _T("typedef");
92 
93  if (!m_FullType.IsEmpty())
94  result << _T(" ") << m_FullType;
95 
96  // we support 2 cases of typedef'd function pointers, and in each case the type is stored
97  // as below:
98  // typedef void (*dMessageFunction)(int errnum, const char *msg, va_list ap);
99  // --> type is stored as: (*)
100  // typedef void (MyClass::*Function)(int);
101  // --> type is stored as: (MyClass::*)
102  // so, ensure we really have ')' as the last char.
103  if (result.Find('*', true) != wxNOT_FOUND && result.Last() == ')')
104  {
105  result.RemoveLast();
106  return result << m_Name << _T(")") << GetFormattedArgs();
107  }
108 
110  result << m_TemplateArgument;
111 
112  return result << _T(" ") << m_Name;
113  }
114  else if (m_TokenKind == tkMacroDef)
115  {
116  result << _T("#define ") << m_Name << GetFormattedArgs();
117  if (!m_FullType.IsEmpty())
118  result << _T(" ") << m_FullType;
119 
120  return result;
121  }
122 
123  // else
124  if (!m_FullType.IsEmpty())
125  result << m_FullType << m_TemplateArgument << _T(" ");
126 
127  if (m_TokenKind == tkEnumerator)
128  return result << GetNamespace() << m_Name << _T("=") << GetFormattedArgs();
129 
130  return result << GetNamespace() << m_Name << GetStrippedArgs();
131 }
132 
133 bool Token::IsValidAncestor(const wxString& ancestor)
134 {
135  switch (ancestor.Len())
136  {
137  case 3:
138  if (ancestor == _T("int"))
139  return false;
140  break;
141 
142  case 4:
143  if ( ancestor == _T("void")
144  || ancestor == _T("bool")
145  || ancestor == _T("long")
146  || ancestor == _T("char") )
147  {
148  return false;
149  }
150  break;
151 
152  case 5:
153  if ( ancestor == _T("short")
154  || ancestor == _T("float") )
155  {
156  return false;
157  }
158  break;
159 
160  case 6:
161  if ( ancestor == _T("size_t")
162  || ancestor == _T("double") )
163  {
164  return false;
165  }
166  break;
167 
168  case 10:
169  if (ancestor == _T("value_type"))
170  return false;
171  break;
172 
173  default:
174  if ( ancestor.StartsWith(_T("unsigned"))
175  || ancestor.StartsWith(_T("signed")) )
176  {
177  return false;
178  }
179  break;
180  }
181 
182  return true;
183 }
184 
186 {
187  if (!m_TokenTree)
188  return wxString(_T(""));
190 }
191 
193 {
194  if (!m_TokenTree)
195  return wxString(_T(""));
197 }
198 
200 {
201  wxString args(m_Args);
202  args.Replace(_T("\n"), wxEmptyString);
203  return args;
204 }
205 
207 {
208  // the argument should have the format (xxxx = y, ....) or just an empty string
209  // if it is empty, we just return an empty string
210  if (m_Args.IsEmpty())
211  return wxEmptyString;
212 
213  wxString args;
214  args.Alloc(m_Args.Len() + 1);
215  bool skipDefaultValue = false;
216  for (size_t i = 0; i < m_Args.Len(); ++i)
217  {
218  const wxChar ch = m_Args[i];
219  if (ch == _T('\n'))
220  continue;
221  else if (ch == _T('='))
222  {
223  skipDefaultValue = true;
224  args.Trim();
225  }
226  else if (ch == _T(','))
227  skipDefaultValue = false;
228 
229  if (!skipDefaultValue)
230  args << ch;
231  }
232 
233  if (args.Last() != _T(')'))
234  args << _T(')');
235 
236  return args;
237 }
238 
240 {
241  if (!files.size())
242  return true;
243 
244  if (!m_FileIdx && !m_ImplFileIdx)
245  return true;
246 
247  if ((m_FileIdx && files.count(m_FileIdx)) || (m_ImplFileIdx && files.count(m_ImplFileIdx)))
248  return true;
249 
250  return false;
251 }
252 
254 {
255  const wxString dcolon(_T("::"));
256  wxString res;
257  Token* parentToken = m_TokenTree->at(m_ParentIndex);
258  while (parentToken)
259  {
260  res.Prepend(dcolon);
261  res.Prepend(parentToken->m_Name);
262  parentToken = m_TokenTree->at(parentToken->m_ParentIndex);
263  }
264  return res;
265 }
266 
267 bool Token::AddChild(int childIdx)
268 {
269  if (childIdx < 0)
270  return false;
271  m_Children.insert(childIdx);
272  return true;
273 }
274 
276 {
277  if (!m_TokenTree)
278  return false;
279  for (;;)
280  {
281  TokenIdxSet::const_iterator it = m_Children.begin();
282  if (it == m_Children.end())
283  break;
284  m_TokenTree->erase(*it);
285  }
286  return true;
287 }
288 
289 bool Token::InheritsFrom(int idx) const
290 {
291  if (idx < 0 || !m_TokenTree)
292  return false;
293 
294  Token* token = m_TokenTree->at(idx);
295  if (!token)
296  return false;
297 
298  for (TokenIdxSet::const_iterator it = m_DirectAncestors.begin(); it != m_DirectAncestors.end(); it++)
299  {
300  int idx2 = *it;
301  const Token* ancestor = m_TokenTree->at(idx2);
302  if (!ancestor)
303  continue;
304 
305  if (ancestor == token || ancestor->InheritsFrom(idx)) // ##### is this intended?
306  return true;
307  }
308  return false;
309 }
310 
312 {
313  switch (m_TokenKind)
314  {
315  case tkClass: return _T("class");
316  case tkNamespace: return _T("namespace");
317  case tkTypedef: return _T("typedef");
318  case tkEnum: return _T("enum");
319  case tkEnumerator: return _T("enumerator");
320  case tkFunction: return _T("function");
321  case tkConstructor: return _T("constructor");
322  case tkDestructor: return _T("destructor");
323  case tkMacroDef: return _T("macro definition");
324  case tkMacroUse: return _T("macro usage");
325  case tkVariable: return _T("variable");
326  case tkAnyContainer: return _T("any container");
327  case tkAnyFunction: return _T("any function");
328  case tkUndefined: return _T("undefined");
329  default: return wxEmptyString; // tkUndefined
330  }
331 }
332 
334 {
335  switch (m_Scope)
336  {
337  case tsPrivate: return _T("private");
338  case tsProtected: return _T("protected");
339  case tsPublic: return _T("public");
340  case tsUndefined: return _T("undefined");
341  default: return wxEmptyString;
342  }
343 }
std::map< wxString, wxString > m_TemplateMap
a string to string map from formal template argument to actual template argument
Definition: token.h:291
destructor class member function
Definition: token.h:51
variable
Definition: token.h:57
namespace
Definition: token.h:34
int m_ParentIndex
Parent Token index.
Definition: token.h:265
constructor class member function
Definition: token.h:48
class or struct
Definition: token.h:37
wxString GetFormattedArgs() const
remove all &#39; &#39; in the original function argument string
Definition: token.cpp:199
Token * at(int idx)
Definition: tokentree.h:51
wxString m_Name
Token&#39;s name, it can be searched in the TokenTree.
Definition: token.h:188
std::set< size_t, std::less< size_t > > TokenFileSet
Definition: token.h:19
bool IsValidAncestor(const wxString &ancestor)
build in types are not valid ancestors for a type define token
Definition: token.cpp:133
typedef, note typedefs are stored as classes inheriting from the typedef&#39;d type, this takes advantage...
Definition: token.h:45
int erase(int loc)
remove the Token specified by the index
Definition: tokentree.cpp:127
container like tokens, those tokens can have children tokens
Definition: token.h:70
const wxString GetFilename(size_t fileIdx) const
Definition: tokentree.cpp:858
wxString GetFilename() const
get a full path of the file which contains the current Token
Definition: token.cpp:185
wxString m_BaseArgs
stripped arguments e.g.
Definition: token.h:197
TokenIdxSet m_Children
if it is a class kind token, then it contains all the member tokens
Definition: token.h:268
#define _T(string)
wxString GetNamespace() const
get a literal string presentation of the namespace.
Definition: token.cpp:253
TokenScope m_Scope
public? private? protected?
Definition: token.h:231
wxArrayString m_TemplateType
for a class template, this is the formal template argument list, but for a variable Token...
Definition: token.h:288
#define wxNOT_FOUND
wxString DisplayName() const
a short simple string to show the token information, this usually generate for show the tip message w...
Definition: token.cpp:80
bool MatchesFiles(const TokenFileSet &files)
see whether the current token belong to any files in the file set, both m_FileIdx and m_ImplFileIdx i...
Definition: token.cpp:239
wxUSE_UNICODE_dependent wxChar
unsigned int m_FileIdx
File index in TokenTree.
Definition: token.h:207
wxString GetStrippedArgs() const
remove all default value of the function argument string, e.g.
Definition: token.cpp:206
a symbol found in the parsed files, it can be many kinds, such as a variable, a class and so on...
Definition: token.h:82
wxString & RemoveLast(size_t n=1)
bool Alloc(size_t nLen)
size_t Replace(const wxString &strOld, const wxString &strNew, bool replaceAll=true)
bool InheritsFrom(int idx) const
check to see the current token is inherited from a specified token
Definition: token.cpp:289
wxString wxEmptyString
wxString & Trim(bool fromRight=true)
wxString m_Args
If it is a function Token, then this value is function arguments, e.g.
Definition: token.h:194
wxString GetTokenScopeString() const
the access kind string, e.g.
Definition: token.cpp:333
unsigned int m_ImplFileIdx
function implementation file index
Definition: token.h:216
wxString m_TemplateArgument
template argument list, comma separated list string
Definition: token.h:283
undefined or just "all"
Definition: token.h:76
enum
Definition: token.h:40
bool IsEmpty() const
TokenTree * m_TokenTree
a pointer to TokenTree
Definition: token.h:305
enumerator
Definition: token.h:60
size_t Len() const
any kind of functions
Definition: token.h:73
bool DeleteAllChildren()
delete all the child tokens of the current token, not only remove the relation, but also delete the T...
Definition: token.cpp:275
TokenKind m_TokenKind
See TokenKind class.
Definition: token.h:234
wxString & Prepend(const wxString &str)
general function, not constructor nor destructor
Definition: token.h:54
~Token()
destructor
Definition: token.cpp:73
bool StartsWith(const wxString &prefix, wxString *rest=NULL) const
int Find(wxUniChar ch, bool fromEnd=false) const
wxUniChar Last() const
macro definition, such as: #define AAA(x,y) f(x,y), where AAA is a token of tkMacroDef ...
Definition: token.h:63
wxString GetImplFilename() const
get a full path of the file which contains the function implementation.
Definition: token.cpp:192
const wxString dcolon(_T("::"))
Definition: token.h:26
wxString m_FullType
this is the full return value (if any): e.g.
Definition: token.h:182
wxString GetTokenKindString() const
the token kind string, e.g.
Definition: token.cpp:311
TokenIdxSet m_DirectAncestors
the nearest ancestors
Definition: token.h:274
the usage of the macro, for example: AAA(1,2)
Definition: token.h:66
bool AddChild(int childIdx)
add a child token
Definition: token.cpp:267