File: entry.cpp

package info (click to toggle)
codelite 2.6.0.4189~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 30,868 kB
  • ctags: 32,563
  • sloc: cpp: 237,275; ansic: 20,775; lex: 2,114; yacc: 2,007; xml: 1,274; sh: 1,064; makefile: 566; python: 163
file content (532 lines) | stat: -rw-r--r-- 14,566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : entry.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "precompiled_header.h"

#include "entry.h"
#include <wx/tokenzr.h>
#include "tokenizer.h"
#include "language.h"
#include "code_completion_api.h"

TagEntry::TagEntry(const tagEntry& entry)
{
	Create(entry);
}

TagEntry::TagEntry()
		: m_path(wxEmptyString)
		, m_file(wxEmptyString)
		, m_lineNumber(-1)
		, m_pattern(wxEmptyString)
		, m_kind(wxT("<unknown>"))
		, m_parent(wxEmptyString)
		, m_name(wxEmptyString)
		, m_id(wxNOT_FOUND)
		, m_scope(wxEmptyString)
		, m_differOnByLineNumber(false)
{
}

TagEntry::~TagEntry()
{
}

TagEntry::TagEntry(const TagEntry& rhs)
{
	*this = rhs;
}

TagEntry& TagEntry::operator=(const TagEntry& rhs)
{
	m_id = rhs.m_id;
	m_file = rhs.m_file.c_str();
	m_kind = rhs.m_kind.c_str();
	m_parent = rhs.m_parent.c_str();
	m_pattern = rhs.m_pattern.c_str();
	m_lineNumber = rhs.m_lineNumber;
	m_name = rhs.m_name.c_str();
	m_path = rhs.m_path.c_str();
	m_hti = rhs.m_hti;
	m_scope = rhs.m_scope.c_str();
	m_differOnByLineNumber = rhs.m_differOnByLineNumber;

	// loop over the map and copy item by item
	// we use the c_str() method to force our own copy of the string and to avoid
	// ref counting which may cause crash when sharing wxString among threads
	m_extFields.clear();
	std::map<wxString, wxString>::const_iterator iter = rhs.m_extFields.begin();
	for ( ; iter != rhs.m_extFields.end(); iter ++ ) {
		m_extFields[iter->first.c_str()] = iter->second.c_str();
	}
	return *this;
}

bool TagEntry::operator ==(const TagEntry& rhs)
{
	//Note: tree item id is not used in this function!
	bool res =
	    m_scope == rhs.m_scope &&
	    m_file == rhs.m_file &&
	    m_kind == rhs.m_kind &&
	    m_parent == rhs.m_parent &&
	    m_pattern == rhs.m_pattern &&
	    m_name == rhs.m_name &&
	    m_path == rhs.m_path &&
	    m_lineNumber == rhs.m_lineNumber &&
	    GetInherits() == rhs.GetInherits() &&
	    GetAccess() == rhs.GetAccess() &&
	    GetSignature() == rhs.GetSignature() &&
	    GetTyperef() == rhs.GetTyperef();

	bool res2 = m_scope == rhs.m_scope &&
	            m_file == rhs.m_file &&
	            m_kind == rhs.m_kind &&
	            m_parent == rhs.m_parent &&
	            m_pattern == rhs.m_pattern &&
	            m_name == rhs.m_name &&
	            m_path == rhs.m_path &&
	            GetInherits() == rhs.GetInherits() &&
	            GetAccess() == rhs.GetAccess() &&
	            GetSignature() == rhs.GetSignature() &&
	            GetTyperef() == rhs.GetTyperef();

	if (res2 && !res) {
		// the entries are differs only in the line numbers
		m_differOnByLineNumber = true;
	}
	return res;
}

void TagEntry::Create(const wxString &fileName,
                      const wxString &name,
                      int lineNumber,
                      const wxString &pattern,
                      const wxString &kind,
                      std::map<wxString, wxString>& extFields)
{
	SetName( name );
	SetLine( lineNumber );
	SetKind( kind.IsEmpty() ? wxT("<unknown>") : kind );
	SetPattern( pattern );
	SetFile( fileName );
	SetId(-1);
	m_extFields = extFields;
	wxString path;

	// Check if we can get full name (including path)
	path = GetExtField(wxT("class"));
	if (!path.IsEmpty()) {
		UpdatePath( path ) ;
	} else {
		path = GetExtField(wxT("struct"));
		if (!path.IsEmpty()) {
			UpdatePath( path ) ;
		} else {
			path = GetExtField(wxT("namespace"));
			if (!path.IsEmpty()) {
				UpdatePath( path ) ;
			} else {
				path = GetExtField(wxT("interface"));
				if (!path.IsEmpty()) {
					UpdatePath( path ) ;
				} else {
					path = GetExtField(wxT("enum"));
					if (!path.IsEmpty()) {
						UpdatePath( path ) ;
					} else {
						path = GetExtField(wxT("union"));
						wxString tmpname = path.AfterLast(wxT(':'));
						if (!path.IsEmpty()) {
							if (!tmpname.StartsWith(wxT("__anon"))) {
								UpdatePath( path ) ;
							} else {
								// anonymouse union, remove the anonymous part from its name
								path = path.BeforeLast(wxT(':'));
								path = path.BeforeLast(wxT(':'));
								UpdatePath( path ) ;
							}
						}
					}
				}
			}
		}
	}

	if (!path.IsEmpty()) {
		SetScope(path);
	} else {
		SetScope(wxT("<global>"));
	}

	// If there is no path, path is set to name
	if ( GetPath().IsEmpty() )
		SetPath( GetName() );

	// Get the parent name
	StringTokenizer tok(GetPath(), wxT("::"));
	wxString parent;

	(tok.Count() < 2) ? parent = wxT("<global>") : parent = tok[tok.Count()-2];
	SetParent(parent);
}

void TagEntry::Create(const tagEntry& entry)
{
	// Get other information from the string data and store it into map
	for (int i = 0;  i < entry.fields.count;  ++i) {
		wxString key = _U(entry.fields.list[i].key);
		wxString value = _U(entry.fields.list[i].value);
		m_extFields[key] = value;
	}
	Create(	_U(entry.file),
	        _U(entry.name),
	        entry.address.lineNumber,
	        _U(entry.address.pattern),
	        _U(entry.kind),
	        m_extFields);
}

void TagEntry::Print()
{
	std::cout << "======================================" << std::endl;
	std::cout << "Name:\t\t" << GetName() << std::endl;
	std::cout << "File:\t\t" << GetFile() << std::endl;
	std::cout << "Line:\t\t" << GetLine() << std::endl;
	std::cout << "Pattern\t\t" << GetPattern() << std::endl;
	std::cout << "Kind:\t\t" << GetKind() << std::endl;
	std::cout << "Parent:\t\t" << GetParent() << std::endl;

	std::cout << " ---- Ext fields: ---- " << std::endl;
	std::map<wxString, wxString>::const_iterator iter = m_extFields.begin();
	for (; iter != m_extFields.end(); iter++)
		std::cout << iter->first << ":\t\t" << iter->second << std::endl;
	std::cout << "======================================" << std::endl;
}

wxString TagEntry::Key() const
{
	wxString key;
	if (GetKind() == wxT("prototype") || GetKind() == wxT("macro")) {
		key << GetKind() << wxT(": ");
	}

	key << GetPath() << GetSignature();
	return key;
}

wxString TagEntry::GetDisplayName() const
{
	wxString name;
	name << GetName() << GetSignature();
	return name;
}

wxString TagEntry::GetFullDisplayName() const
{
	wxString name;

	if ( GetParent() == wxT("<global>") ) {
		name << GetDisplayName();
	} else {
		name << GetParent() << wxT("::") << GetName() << GetSignature();
	}

	return name;
}

//----------------------------------------------------------------------------
// Database operations
//----------------------------------------------------------------------------

wxString TagEntry::GetScopeName() const
{
	return GetScope();
}

wxString TagEntry::GetKind() const
{
	wxString kind(m_kind);
	kind.Trim();
	return kind;
}


const bool TagEntry::IsContainer() const
{
	return	GetKind() == wxT("class")  ||
	       GetKind() == wxT("struct") ||
	       GetKind() == wxT("union")  ||
	       GetKind() == wxT("namespace") ||
	       GetKind() == wxT("project");
}

void TagEntry::UpdatePath(wxString & path)
{
	if (!path.IsEmpty()) {
		wxString name(path);
		name += wxT("::");
		name += GetName();
		SetPath(name);
	}
}

wxString TagEntry::TypeFromTyperef() const
{
	wxString typeref = GetTyperef();
	if ( typeref.IsEmpty() == false ) {
		wxString name = typeref.BeforeFirst(wxT(':'));
		return name;
	}
	return wxEmptyString;
}


wxString TagEntry::NameFromTyperef(wxString &templateInitList)
{
	wxString typeref = GetTyperef();
	if ( typeref.IsEmpty() == false ) {
		wxString name = typeref.AfterFirst(wxT(':'));
		return name;
	}

	// incase our entry is a typedef, and it is not marked as typeref,
	// try to get the real name from the pattern
	if ( GetKind() == wxT("typedef")) {
		wxString name;
		if (TypedefFromPattern(GetPattern(), GetName(),name, templateInitList))
			return name;
	}
	return wxEmptyString;
}

bool TagEntry::TypedefFromPattern(const wxString &tagPattern, const wxString &typedefName, wxString &name, wxString &templateInit)
{
	wxString pattern(tagPattern);
	
	pattern.StartsWith(wxT("/^"), &pattern);
	const wxCharBuffer cdata = pattern.mb_str(wxConvUTF8);
	
	clTypedefList li;
	get_typedefs(cdata.data(), li);
	
	if(li.size() == 1) {
		clTypedef td = *li.begin();
		templateInit = _U(td.m_realType.m_templateDecl.c_str());
		if(td.m_realType.m_typeScope.empty() == false)
			name << _U(td.m_realType.m_typeScope.c_str()) << wxT("::");
			
		name         << _U(td.m_realType.m_type.c_str());
		return true;
	}
	return false;
}

wxString TagEntry::GetPattern() const
{
	wxString pattern ( m_pattern );
	//since ctags's pattern is regex, forward slashes are escaped. ('/' becomes '\/')
	pattern.Replace(wxT("\\\\"), wxT("\\"));
	pattern.Replace(wxT("\\/"), wxT("/"));
	return pattern;
}

void TagEntry::FromLine(const wxString& line)
{
	wxString pattern, kind;
	wxString strLine = line;
	long lineNumber = wxNOT_FOUND;
	std::map<wxString, wxString> extFields;

	//get the token name
	wxString name = strLine.BeforeFirst(wxT('\t'));
	strLine	= strLine.AfterFirst(wxT('\t'));

	//get the file name
	wxString fileName = strLine.BeforeFirst(wxT('\t'));
	strLine	= strLine.AfterFirst(wxT('\t'));

	//here we can get two options:
	//pattern followed by ;"
	//or
	//line number followed by ;"
	int end = strLine.Find(wxT(";\""));
	if (end == wxNOT_FOUND) {
		//invalid pattern found
		return;
	}

	if (strLine.StartsWith(wxT("/^"))) {
		//regular expression pattern found
		pattern = strLine.Mid(0, end);
		strLine	= strLine.Right(strLine.Length() - (end + 2));
	} else {
		//line number pattern found, this is usually the case when
		//dealing with macros in C++
		pattern = strLine.Mid(0, end);
		strLine	= strLine.Right(strLine.Length() - (end + 2));

		pattern = pattern.Trim();
		pattern = pattern.Trim(false);
		pattern.ToLong(&lineNumber);
	}

	//next is the kind of the token
	if (strLine.StartsWith(wxT("\t"))) {
		strLine	= strLine.AfterFirst(wxT('\t'));
	}

	kind = strLine.BeforeFirst(wxT('\t'));
	strLine	= strLine.AfterFirst(wxT('\t'));

	if (strLine.IsEmpty() == false) {
		wxStringTokenizer tkz(strLine, wxT('\t'));
		while (tkz.HasMoreTokens()) {
			wxString token = tkz.NextToken();
			wxString key = token.BeforeFirst(wxT(':'));
			wxString val = token.AfterFirst(wxT(':'));
			key = key.Trim();
			key = key.Trim(false);

			val = val.Trim();
			val = val.Trim(false);
			if (key == wxT("line") && !val.IsEmpty()) {
				val.ToLong(&lineNumber);
			} else {
				if (key == wxT("union") || key == wxT("struct")) {

					// remove the anonymous part of the struct / union
					if (!val.StartsWith(wxT("__anon"))) {
						// an internal anonymous union / struct
						// remove all parts of the
						wxArrayString scopeArr;
						wxString tmp, new_val;

						scopeArr = wxStringTokenize(val, wxT(":"), wxTOKEN_STRTOK);
						for (size_t i=0; i<scopeArr.GetCount(); i++) {
							if (scopeArr.Item(i).StartsWith(wxT("__anon")) == false) {
								tmp << scopeArr.Item(i) << wxT("::");
							}
						}

						tmp.EndsWith(wxT("::"), &new_val);
						val = new_val;
					}
				}

				extFields[key] = val;
			}
		}
	}

	kind = kind.Trim();
	name = name.Trim();
	fileName = fileName.Trim();
	pattern = pattern.Trim();

	if (kind == wxT("enumerator")) {
		// enums are specials, they are not really a scope so they should appear when I type:
		// enumName::
		// they should be member of their parent (which can be <global>, or class)
		// but we want to know the "enum" type they belong to, so save that in typeref,
		// then patch the enum field to lift the enumerator into the enclosing scope.
		// watch out for anonymous enums -- leave their typeref field blank.
		std::map<wxString,wxString>::iterator e = extFields.find(wxT("enum"));
		if (e != extFields.end()) {
			wxString typeref = e->second;
			e->second = e->second.BeforeLast(wxT(':')).BeforeLast(wxT(':'));
			if (!typeref.AfterLast(wxT(':')).StartsWith(wxT("__anon"))) {
				extFields[wxT("typeref")] = typeref;
			}
		}
	}

	this->Create(fileName, name, lineNumber, pattern, kind, extFields);
}

bool TagEntry::IsConstructor() const
{
	if(GetKind() != wxT("function") && GetKind() != wxT("prototype"))
		return false;

	return GetName() == GetScope();
}

bool TagEntry::IsDestructor() const
{
	if(GetKind() != wxT("function") && GetKind() != wxT("prototype"))
		return false;

	return GetName().StartsWith(wxT("~"));
}


wxString TagEntry::GetReturnValue() const
{
	wxString returnValue = GetExtField(_T("returns"));
	returnValue.Trim().Trim(false);
	returnValue.Replace(wxT("virtual"), wxT(""));
	return returnValue;
}

bool TagEntry::IsFunction() const
{
	return GetKind() == wxT("function");
}

bool TagEntry::IsMethod() const
{
	return IsPrototype() || IsFunction();
}

bool TagEntry::IsPrototype() const
{
	return GetKind() == wxT("prototype") ;
}


bool TagEntry::IsClass() const
{
	return GetKind() == wxT("class");
}

bool TagEntry::IsMacro() const
{
	return GetKind() == wxT("macro");
}

bool TagEntry::IsStruct() const
{
	return GetKind() == wxT("struct");
}

bool TagEntry::IsScopeGlobal() const
{
	return GetScope().IsEmpty() || GetScope() == wxT("<global>");
}

bool TagEntry::IsTypedef() const
{
	return GetKind() == wxT("typedef");
}