Debian Codegraph @

CodeLite/entry.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : entry.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "entry.h"
27
28 #include "CompletionHelper.hpp"
29 #include "CxxScannerTokens.h"
30 #include "CxxTokenizer.h"
31 #include "code_completion_api.h"
32 #include "comment_parser.h"
33 #include "ctags_manager.h"
34 #include "language.h"
35 #include "macros.h"
36 #include "pptable.h"
37 #include "precompiled_header.h"
38 #include "tokenizer.h"
39 #include "wxStringHash.h"
40
41 #include <wx/regex.h>
42 #include <wx/tokenzr.h>
43
44 TagEntry::TagEntry()
45 : m_path(wxEmptyString)
46 , m_file(wxEmptyString)
47 , m_lineNumber(-1)
48 , m_pattern(wxEmptyString)
49 , m_kind("<unknown>")
50 , m_parent(wxEmptyString)
51 , m_name(wxEmptyString)
52 , m_id(wxNOT_FOUND)
53 , m_scope(wxEmptyString)
54 , m_flags(0)
55 {
56 }
57
58 TagEntry::~TagEntry() {}
59
60 TagEntry::TagEntry(const TagEntry& rhs) { *this = rhs; }
61
62 TagEntry& TagEntry::operator=(const TagEntry& rhs)
63 {
64 m_id = rhs.m_id;
65 m_file = rhs.m_file.c_str();
66 m_kind = rhs.m_kind.c_str();
67 m_parent = rhs.m_parent.c_str();
68 m_pattern = rhs.m_pattern.c_str();
69 m_lineNumber = rhs.m_lineNumber;
70 m_name = rhs.m_name.c_str();
71 m_path = rhs.m_path.c_str();
72 #if wxUSE_GUI
73 m_hti = rhs.m_hti;
74 #endif
75 m_scope = rhs.m_scope.c_str();
76 m_flags = rhs.m_flags;
77
78 // loop over the map and copy item by item
79 // we use the c_str() method to force our own copy of the string and to avoid
80 // ref counting which may cause crash when sharing wxString among threads
81 m_extFields.clear();
82 wxStringMap_t::const_iterator iter = rhs.m_extFields.begin();
83 for(; iter != rhs.m_extFields.end(); iter++) {
84 m_extFields[iter->first.c_str()] = iter->second.c_str();
85 }
86 m_comment = rhs.m_comment;
87 return *this;
88 }
89
90 bool TagEntry::operator==(const TagEntry& rhs)
91 {
92 // Note: tree item id is not used in this function!
93 bool res = m_scope == rhs.m_scope && m_file == rhs.m_file && m_kind == rhs.m_kind && m_parent == rhs.m_parent &&
94 m_pattern == rhs.m_pattern && m_name == rhs.m_name && m_path == rhs.m_path &&
95 m_lineNumber == rhs.m_lineNumber && GetInheritsAsString() == rhs.GetInheritsAsString() &&
96 GetAccess() == rhs.GetAccess() && GetSignature() == rhs.GetSignature();
97 return res;
98 }
99
100 void TagEntry::Create(const wxString& fileName, const wxString& name, int lineNumber, const wxString& pattern,
101 const wxString& kind, wxStringMap_t& extFields)
102 {
103 m_flags = 0;
104 m_extFields = extFields;
105 SetName(name);
106 SetLine(lineNumber);
107 SetKind(kind.IsEmpty() ? "<unknown>" : kind);
108 SetPattern(pattern);
109 SetFile(wxFileName(fileName).GetFullPath());
110 SetId(-1);
111
112 wxString path;
113
114 // Check if we can get full name (including path)
115 static std::vector<wxString> scope_fields = { "class", "struct", "namespace", "interface", "enum", "function" };
116 for(const wxString& scope_field : scope_fields) {
117 path = GetExtField(scope_field);
118 if(!path.IsEmpty()) {
119 UpdatePath(path);
120 break;
121 }
122 }
123
124 // still no path?
125 // try union
126 if(path.empty()) {
127 path = GetExtField("union");
128 wxString tmpname = path.AfterLast(':');
129 if(!path.IsEmpty()) {
130 if(!tmpname.StartsWith("__anon")) {
131 UpdatePath(path);
132 } else {
133 // anonymouse union, remove the anonymous part from its name
134 path = path.BeforeLast(':');
135 path = path.BeforeLast(':');
136 UpdatePath(path);
137 }
138 }
139 }
140
141 // update the method properties
142 SetTagProperties(GetExtField("properties"));
143 if(!path.IsEmpty()) {
144 SetScope(path);
145 } else {
146 SetScope("<global>");
147 }
148
149 // If there is no path, path is set to name
150 if(GetPath().IsEmpty()) {
151 SetPath(GetName());
152 }
153
154 // Get the parent name
155 StringTokenizer tok(GetPath(), "::");
156 wxString parent;
157
158 (tok.Count() < 2) ? parent = "<global>" : parent = tok[tok.Count() - 2];
159 SetParent(parent);
160
161 // parse auto
162 m_assignment = TypenameFromPattern(this);
163 if(IsAuto(this)) {
164 m_tag_properties_flags |= TAG_PROP_AUTO_VARIABLE;
165 }
166 if(IsFunction() && GetName().StartsWith("__anon")) {
167 m_tag_properties_flags |= TAG_PROP_INLINE;
168 }
169 }
170
171 void TagEntry::Print()
172 {
173 std::cout << "======================================" << std::endl;
174 std::cout << "Name:\t\t" << GetName() << std::endl;
175 std::cout << "File:\t\t" << GetFile() << std::endl;
176 std::cout << "Line:\t\t" << GetLine() << std::endl;
177 std::cout << "Pattern\t\t" << GetPattern() << std::endl;
178 std::cout << "Kind:\t\t" << GetKind() << std::endl;
179 std::cout << "Parent:\t\t" << GetParent() << std::endl;
180
181 std::cout << " ---- Ext fields: ---- " << std::endl;
182 wxStringMap_t::const_iterator iter = m_extFields.begin();
183 for(; iter != m_extFields.end(); iter++) {
184 std::cout << iter->first << ":\t\t" << iter->second << std::endl;
185 }
186 std::cout << "======================================" << std::endl;
187 }
188
189 wxString TagEntry::Key() const
190 {
191 wxString key;
192 if(IsPrototype() || IsMacro()) {
193 key << GetKind() << ": ";
194 }
195
196 key << GetPath() << GetSignature();
197 return key;
198 }
199
200 wxString TagEntry::GetDisplayName() const
201 {
202 wxString name;
203 name << GetName() << GetSignature();
204 return name;
205 }
206
207 wxString TagEntry::GetFullDisplayName() const
208 {
209 wxString name;
210
211 if(GetParent() == "<global>") {
212 name << GetDisplayName();
213 } else {
214 name << GetParent() << "::" << GetName() << GetSignature();
215 }
216
217 return name;
218 }
219
220 bool TagEntry::IsClassTemplate() const
221 {
222 return m_extFields.count("template") && !m_extFields.find("template")->second.empty();
223 }
224
225 //----------------------------------------------------------------------------
226 // Database operations
227 //----------------------------------------------------------------------------
228
229 wxString TagEntry::GetScopeName() const { return GetScope(); }
230
231 wxString TagEntry::GetKind() const
232 {
233 wxString kind(m_kind);
234 kind.Trim();
235 return kind;
236 }
237
238 const bool TagEntry::IsContainer() const
239 {
240 return IsClass() || IsStruct() || IsUnion() || IsNamespace() || IsEnumClass();
241 }
242
243 void TagEntry::UpdatePath(wxString& path)
244 {
245 if(!path.IsEmpty()) {
246 wxString name(path);
247 name += "::";
248 name += GetName();
249 SetPath(name);
250 }
251 }
252
253 const wxString& TagEntry::GetExtField(const wxString& extField) const
254 {
255 static wxString empty_string;
256 if(m_extFields.count(extField) == 0) {
257 return empty_string;
258 }
259 return m_extFields.find(extField)->second;
260 }
261
262 wxString TagEntry::GetPattern() const
263 {
264 wxString pattern(m_pattern);
265 // since ctags's pattern is regex, forward slashes are escaped. ('/' becomes '\/')
266 pattern.Replace("\\\\", "\\");
267 pattern.Replace("\\/", "/");
268 return pattern;
269 }
270
271 void TagEntry::FromLine(const wxString& line)
272 {
273 // label C:\src\wxCustomControls\clTreeCtrl\clChoice.cpp /^ const wxString& label = m_choices[i];$/;"
274 // local line:116 type:constwxString
275 wxString pattern, kind;
276 wxString strLine = line;
277 long lineNumber = wxNOT_FOUND;
278 wxStringMap_t extFields;
279
280 // get the token name
281 wxString name = strLine.BeforeFirst('\t');
282 strLine = strLine.AfterFirst('\t');
283
284 // get the file name
285 wxString fileName = strLine.BeforeFirst('\t');
286 strLine = strLine.AfterFirst('\t');
287
288 // here we can get two options:
289 // pattern followed by ;"
290 // or
291 // line number followed by ;"
292 int end = strLine.Find(";\"");
293 if(end == wxNOT_FOUND) {
294 // invalid pattern found
295 return;
296 }
297
298 if(strLine.StartsWith("/^")) {
299 // regular expression pattern found
300 pattern = strLine.Mid(0, end);
301 strLine = strLine.Right(strLine.Length() - (end + 2));
302 } else {
303 // line number pattern found, this is usually the case when
304 // dealing with macros in C++
305 pattern = strLine.Mid(0, end);
306 strLine = strLine.Right(strLine.Length() - (end + 2));
307
308 pattern = pattern.Trim();
309 pattern = pattern.Trim(false);
310 pattern.ToLong(&lineNumber);
311 }
312
313 // next is the kind of the token
314 if(strLine.StartsWith("\t")) {
315 strLine = strLine.AfterFirst('\t');
316 }
317
318 kind = strLine.BeforeFirst('\t');
319 strLine = strLine.AfterFirst('\t');
320
321 if(strLine.IsEmpty() == false) {
322 wxStringTokenizer tkz(strLine, '\t');
323 while(tkz.HasMoreTokens()) {
324 wxString token = tkz.NextToken();
325 wxString key = token.BeforeFirst(':');
326 wxString val = token.AfterFirst(':');
327 key = key.Trim();
328 key = key.Trim(false);
329
330 val = val.Trim();
331 val = val.Trim(false);
332 if(key == "line" && !val.IsEmpty()) {
333 val.ToLong(&lineNumber);
334 } else {
335 if(key == "union" || key == "struct") {
336
337 // remove the anonymous part of the struct / union
338 if(!val.StartsWith("__anon")) {
339 // an internal anonymous union / struct
340 // remove all parts of the
341 wxArrayString scopeArr;
342 wxString tmp, new_val;
343
344 scopeArr = wxStringTokenize(val, ":", wxTOKEN_STRTOK);
345 for(size_t i = 0; i < scopeArr.GetCount(); i++) {
346 if(scopeArr.Item(i).StartsWith("__anon") == false) {
347 tmp << scopeArr.Item(i) << "::";
348 }
349 }
350
351 tmp.EndsWith("::", &new_val);
352 val.swap(new_val);
353 }
354 }
355 extFields.insert({ key, val });
356 }
357 }
358 }
359
360 kind = kind.Trim();
361 name = name.Trim();
362 fileName = fileName.Trim();
363 pattern = pattern.Trim();
364 Create(fileName, name, lineNumber, pattern, kind, extFields);
365 }
366
367 bool TagEntry::IsConstructor() const
368 {
369 if(GetKind() != "function" && GetKind() != "prototype") {
370 return false;
371 }
372 return GetName() == GetScope();
373 }
374
375 bool TagEntry::IsDestructor() const
376 {
377 if(GetKind() != "function" && GetKind() != "prototype") {
378 return false;
379 }
380 return GetName().StartsWith("~");
381 }
382
383 wxString TagEntry::GetTemplateDefinition() const
384 {
385 // template:<class T>
386 wxString definition = GetExtField("template");
387 definition.Trim().Trim(false);
388 return definition;
389 }
390
391 void TagEntry::SetTypename(const wxString& val) { m_extFields["typeref"] = "typename:" + val; }
392
393 wxString TagEntry::GetTypename() const
394 {
395 // typeref:typename:void
396 const wxString& returnValue = GetExtField("typeref");
397 return returnValue.AfterFirst(':');
398 }
399
400 bool TagEntry::IsKeyword() const { return m_tag_kind == eTagKind::TAG_KIND_KEYWORD; }
401 bool TagEntry::IsEnum() const { return m_tag_kind == eTagKind::TAG_KIND_ENUM; }
402 bool TagEntry::IsEnumerator() const { return m_tag_kind == eTagKind::TAG_KIND_ENUMERATOR; }
403 bool TagEntry::IsParameter() const { return m_tag_kind == eTagKind::TAG_KIND_PARAMETER; }
404 bool TagEntry::IsVariable() const { return m_tag_kind == eTagKind::TAG_KIND_VARIABLE; }
405 bool TagEntry::IsEnumClass() const { return m_tag_kind == eTagKind::TAG_KIND_CENUM; }
406 bool TagEntry::IsLocalVariable() const { return m_tag_kind == eTagKind::TAG_KIND_LOCAL; }
407 bool TagEntry::IsMember() const { return m_tag_kind == eTagKind::TAG_KIND_MEMBER; }
408 bool TagEntry::IsNamespace() const { return m_tag_kind == eTagKind::TAG_KIND_NAMESPACE; }
409 bool TagEntry::IsFunction() const { return m_tag_kind == eTagKind::TAG_KIND_FUNCTION; }
410 bool TagEntry::IsPrototype() const { return m_tag_kind == eTagKind::TAG_KIND_PROTOTYPE; }
411 bool TagEntry::IsClass() const { return m_tag_kind == eTagKind::TAG_KIND_CLASS; }
412 bool TagEntry::IsMacro() const { return m_tag_kind == eTagKind::TAG_KIND_MACRO; }
413 bool TagEntry::IsStruct() const { return m_tag_kind == eTagKind::TAG_KIND_STRUCT; }
414 bool TagEntry::IsUnion() const { return m_tag_kind == eTagKind::TAG_KIND_UNION; }
415 bool TagEntry::IsTypedef() const { return m_tag_kind == eTagKind::TAG_KIND_TYPEDEF; }
416 bool TagEntry::IsMethod() const { return IsPrototype() || IsFunction(); }
417 bool TagEntry::IsScopeGlobal() const { return GetScope().IsEmpty() || GetScope() == "<global>"; }
418
419 wxString TagEntry::GetInheritsAsString() const { return GetExtField(_T("inherits")); }
420
421 wxArrayString TagEntry::GetInheritsAsArrayNoTemplates() const
422 {
423 // Parse the input string
424 wxString inherits = GetInheritsAsString();
425 wxString parent;
426 wxArrayString parentsArr;
427
428 int depth(0);
429 for(size_t i = 0; i < inherits.Length(); i++) {
430 wxChar ch = inherits.GetChar(i);
431
432 switch(ch) {
433 case '<':
434 if(depth == 0 && parent.IsEmpty() == false) {
435 parent.Trim().Trim(false);
436 parentsArr.Add(parent);
437 parent.Clear();
438 }
439 depth++;
440 break;
441
442 case '>':
443 depth--;
444 break;
445
446 case ',':
447 if(depth == 0 && parent.IsEmpty() == false) {
448 parent.Trim().Trim(false);
449 parentsArr.Add(parent);
450 parent.Clear();
451 }
452 break;
453
454 default:
455 if(depth == 0) {
456 parent << ch;
457 }
458 break;
459 }
460 }
461
462 if(parent.IsEmpty() == false) {
463 parent.Trim().Trim(false);
464 parentsArr.Add(parent);
465 }
466 return parentsArr;
467 }
468
469 wxArrayString TagEntry::GetInheritsAsArrayWithTemplates() const
470 {
471 // Parse the input string
472 wxString inherits = GetInheritsAsString();
473 wxString parent;
474 wxArrayString parentsArr;
475
476 int depth(0);
477 for(size_t i = 0; i < inherits.Length(); i++) {
478 wxChar ch = inherits.GetChar(i);
479
480 switch(ch) {
481 case '<':
482 depth++;
483 parent << ch;
484 break;
485
486 case '>':
487 depth--;
488 parent << ch;
489 break;
490
491 case ',':
492 if(depth == 0 && parent.IsEmpty() == false) {
493 parent.Trim().Trim(false);
494 parentsArr.Add(parent);
495 parent.Clear();
496
497 } else if(depth != 0) {
498 parent << ch;
499 }
500 break;
501
502 default:
503 parent << ch;
504 break;
505 }
506 }
507
508 if(parent.IsEmpty() == false) {
509 parent.Trim().Trim(false);
510 parentsArr.Add(parent);
511 }
512 return parentsArr;
513 }
514
515 TagEntryPtr TagEntry::ReplaceSimpleMacro()
516 {
517 if(IsMacro()) {
518 PPToken tok = TagsManagerST::Get()->GetDatabase()->GetMacro(GetName());
519 if(tok.flags & PPToken::IsValid && !(tok.flags & PPToken::IsFunctionLike)) {
520 std::vector<TagEntryPtr> tags;
521 TagsManagerST::Get()->FindByNameAndScope(tok.replacement, GetScopeName(), tags);
522 if(tags.size() == 1) {
523 // replace the current tag content with the new match
524 return tags.at(0);
525 }
526 }
527 }
528 return NULL;
529 }
530
531 bool TagEntry::IsTemplateFunction() const
532 {
533 wxString pattern = GetPatternClean();
534 pattern.Trim().Trim(false);
535 return IsMethod() && pattern.StartsWith("template ");
536 }
537
538 wxString TagEntry::GetPatternClean() const
539 {
540 wxString p = GetPattern();
541 p.Trim();
542 if(p.StartsWith("/^")) {
543 p.Replace("/^", "");
544 }
545
546 if(p.EndsWith("$/")) {
547 p.Replace("$/", "");
548 }
549 return p;
550 }
551
552 wxString TagEntry::GetLocalType() const { return GetExtField("type"); }
553
554 namespace
555 {
556 inline void enable_function_flag_if_exists(const wxStringSet_t& S, const wxString& propname, const size_t flag,
557 size_t& flags)
558 {
559 if(S.count(propname)) {
560 flags |= flag;
561 } else {
562 flags &= ~flag;
563 }
564 }
565 } // namespace
566
567 void TagEntry::SetTagProperties(const wxString& props)
568 {
569 m_tag_properties = props;
570 auto tokens = wxStringTokenize(m_tag_properties, ",", wxTOKEN_STRTOK);
571 wxStringSet_t S;
572 for(auto& token : tokens) {
573 token.Trim().Trim(false);
574 S.insert(token);
575 }
576
577 enable_function_flag_if_exists(S, "const", TAG_PROP_CONST, m_tag_properties_flags);
578 enable_function_flag_if_exists(S, "virtual", TAG_PROP_VIRTUAL, m_tag_properties_flags);
579 enable_function_flag_if_exists(S, "default", TAG_PROP_DEFAULT, m_tag_properties_flags);
580 enable_function_flag_if_exists(S, "delete", TAG_PROP_DELETED, m_tag_properties_flags);
581 enable_function_flag_if_exists(S, "static", TAG_PROP_STATIC, m_tag_properties_flags);
582 enable_function_flag_if_exists(S, "inline", TAG_PROP_INLINE, m_tag_properties_flags);
583 enable_function_flag_if_exists(S, "override", TAG_PROP_OVERRIDE, m_tag_properties_flags);
584 enable_function_flag_if_exists(S, "pure", TAG_PROP_PURE, m_tag_properties_flags);
585 enable_function_flag_if_exists(S, "scopedenum", TAG_PROP_SCOPEDENUM, m_tag_properties_flags);
586
587 // change the kind to "enum class"
588 if(is_scoped_enum()) {
589 m_tag_kind = eTagKind::TAG_KIND_CENUM;
590 }
591 }
592
593 bool TagEntry::is_func_virtual() const { return m_tag_properties_flags & TAG_PROP_VIRTUAL; }
594 bool TagEntry::is_func_default() const { return m_tag_properties_flags & TAG_PROP_DEFAULT; }
595 bool TagEntry::is_func_override() const { return m_tag_properties_flags & TAG_PROP_OVERRIDE; }
596 bool TagEntry::is_func_deleted() const { return m_tag_properties_flags & TAG_PROP_DELETED; }
597 bool TagEntry::is_func_inline() const { return m_tag_properties_flags & TAG_PROP_INLINE; }
598 bool TagEntry::is_func_pure() const { return m_tag_properties_flags & TAG_PROP_PURE; }
599 bool TagEntry::is_scoped_enum() const { return m_tag_properties_flags & TAG_PROP_SCOPEDENUM; }
600 bool TagEntry::is_const() const { return m_tag_properties_flags & TAG_PROP_CONST; }
601 bool TagEntry::is_static() const { return m_tag_properties_flags & TAG_PROP_STATIC; }
602 bool TagEntry::is_auto() const { return m_tag_properties_flags & TAG_PROP_AUTO_VARIABLE; }
603 bool TagEntry::is_lambda() const { return m_tag_properties_flags & TAG_PROP_LAMBDA; }
604
605 wxString TagEntry::GetFunctionDeclaration() const
606 {
607 if(!IsMethod()) {
608 return wxEmptyString;
609 }
610 wxString decl;
611 if(is_func_inline()) {
612 decl << "inline ";
613 }
614 if(is_func_virtual()) {
615 decl << "virtual ";
616 }
617 decl << GetTypename() << " ";
618 if(!GetScope().empty()) {
619 decl << GetScope() << "::";
620 }
621 decl << GetName() << GetSignature();
622 if(is_const()) {
623 decl << " const";
624 }
625 if(is_func_pure()) {
626 decl << " = 0";
627 }
628 decl << ";";
629 return decl;
630 }
631
632 wxString TagEntry::GetFunctionDefinition() const
633 {
634 wxString impl;
635 if(!IsMethod()) {
636 return wxEmptyString;
637 }
638 impl << GetTypename() << " ";
639 if(!GetScope().empty()) {
640 impl << GetScope() << "::";
641 }
642
643 CompletionHelper helper;
644 impl << helper.normalize_function(this, CompletionHelper::STRIP_NO_DEFAULT_VALUES);
645 // release the pointer from the smart-ptr
646 // this ensure that `this` is not deleted
647 return impl;
648 }
649
650 namespace
651 {
652 std::unordered_map<wxString, eTagKind> g_kind_table = {
653 { "class", eTagKind::TAG_KIND_CLASS }, { "struct", eTagKind::TAG_KIND_STRUCT },
654 { "namespace", eTagKind::TAG_KIND_NAMESPACE }, { "union", eTagKind::TAG_KIND_UNION },
655 { "enum", eTagKind::TAG_KIND_ENUM }, { "member", eTagKind::TAG_KIND_MEMBER },
656 { "variable", eTagKind::TAG_KIND_VARIABLE }, { "macro", eTagKind::TAG_KIND_MACRO },
657 { "typedef", eTagKind::TAG_KIND_TYPEDEF }, { "local", eTagKind::TAG_KIND_LOCAL },
658 { "parameter", eTagKind::TAG_KIND_PARAMETER }, { "prototype", eTagKind::TAG_KIND_PROTOTYPE },
659 { "cpp_keyword", eTagKind::TAG_KIND_KEYWORD }, { "keyword", eTagKind::TAG_KIND_KEYWORD },
660 { "function", eTagKind::TAG_KIND_FUNCTION }, { "enumerator", eTagKind::TAG_KIND_ENUMERATOR },
661 };
662 }
663
664 void TagEntry::SetKind(const wxString& kind)
665 {
666 // set the string kind
667 m_kind = kind;
668 // turn on bits
669 m_tag_kind = eTagKind::TAG_KIND_UNKNOWN;
670 if(g_kind_table.count(m_kind)) {
671 m_tag_kind = g_kind_table[m_kind];
672 }
673 }
674
675 namespace
676 {
677 void read_until_find(CxxTokenizer& tokenizer, CxxLexerToken& token, int type_1, int type_2, int* what_was_found,
678 wxString* consumed)
679 {
680 // search until we find the `=`
681 int depth = 0;
682 consumed->clear();
683 *what_was_found = 0;
684 consumed->reserve(256); // 256 bytes should be enough for most cases
685
686 while(tokenizer.NextToken(token)) {
687 if(depth == 0 && token.GetType() == type_1) {
688 *what_was_found = type_1;
689 consumed->Trim().Trim(false);
690 return;
691 } else if(depth == 0 && token.GetType() == type_2) {
692 *what_was_found = type_2;
693 consumed->Trim().Trim(false);
694 return;
695 }
696
697 if(token.is_keyword() || token.is_builtin_type()) {
698 consumed->Append(token.GetWXString() + " ");
699 continue;
700 } else if(token.is_pp_keyword()) {
701 continue;
702 }
703
704 // append it
705 consumed->Append(token.GetWXString());
706 switch(token.GetType()) {
707 case '<':
708 case '{':
709 case '[':
710 case '(':
711 depth++;
712 break;
713 case '>':
714 case '}':
715 case ']':
716 case ')':
717 depth--;
718 break;
719 default:
720 break;
721 }
722 }
723
724 // eof
725 consumed->Trim().Trim(false);
726 }
727 } // namespace
728
729 #define CHECK_FOUND(What, Expected) \
730 if(What != Expected) \
731 return wxEmptyString
732
733 wxString TagEntry::TypenameFromPattern(const TagEntry* tag)
734 {
735 if(!tag->IsLocalVariable() && !tag->IsVariable()) {
736 return wxEmptyString;
737 }
738 CxxTokenizer tokenizer;
739 CxxLexerToken token;
740
741 tokenizer.Reset(tag->GetPatternClean());
742
743 // search until we find the `=`
744 int what_was_found = 0;
745 wxString consumed;
746 read_until_find(tokenizer, token, T_FOR, '=', &what_was_found, &consumed);
747 if(what_was_found == 0) {
748 return wxEmptyString;
749 }
750
751 if(what_was_found == '=') {
752 // read until we reach eof or ';'
753 read_until_find(tokenizer, token, ';', 0, &what_was_found, &consumed);
754 return consumed;
755 } else {
756 // we are in a for loop
757 // search for the ':'
758 read_until_find(tokenizer, token, '(', 0, &what_was_found, &consumed);
759 CHECK_FOUND('(', what_was_found);
760
761 read_until_find(tokenizer, token, ':', 0, &what_was_found, &consumed);
762 CHECK_FOUND(':', what_was_found);
763
764 // we found the ':', now read until we find the ')'
765 read_until_find(tokenizer, token, ')', 0, &what_was_found, &consumed);
766 CHECK_FOUND(')', what_was_found);
767 // for(... : expression)
768 // we return expression.begin()
769 consumed += ".begin()";
770 return consumed;
771 }
772 return wxEmptyString;
773 }
774
775 bool TagEntry::IsAuto(const TagEntry* tag)
776 {
777 CxxTokenizer tokenizer;
778 CxxLexerToken token;
779
780 tokenizer.Reset(tag->GetTypename());
781 int found = 0;
782 wxString consumed;
783 read_until_find(tokenizer, token, T_AUTO, 0, &found, &consumed);
784 return found == T_AUTO;
785 }
786
787 wxString TagEntry::GetMacrodef() const { return GetExtField("macrodef"); }
788
789 void TagEntry::SetMacrodef(const wxString& value) { set_extra_field("macrodef", value); }
790
791 void TagEntry::set_extra_field(const wxString& name, const wxString& value)
792 {
793 if(m_extFields.count(name)) {
794 m_extFields.erase(name);
795 }
796 m_extFields.insert({ name, value });
797 }