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 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// 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 "entry.h"
#include "CompletionHelper.hpp"
#include "CxxScannerTokens.h"
#include "CxxTokenizer.h"
#include "code_completion_api.h"
#include "comment_parser.h"
#include "ctags_manager.h"
#include "language.h"
#include "macros.h"
#include "pptable.h"
#include "precompiled_header.h"
#include "tokenizer.h"
#include "wxStringHash.h"
#include <wx/regex.h>
#include <wx/tokenzr.h>
TagEntry::TagEntry()
: m_path(wxEmptyString)
, m_file(wxEmptyString)
, m_lineNumber(-1)
, m_pattern(wxEmptyString)
, m_kind("<unknown>")
, m_parent(wxEmptyString)
, m_name(wxEmptyString)
, m_id(wxNOT_FOUND)
, m_scope(wxEmptyString)
, m_flags(0)
{
}
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();
#if wxUSE_GUI
m_hti = rhs.m_hti;
#endif
m_scope = rhs.m_scope.c_str();
m_flags = rhs.m_flags;
// 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();
wxStringMap_t::const_iterator iter = rhs.m_extFields.begin();
for(; iter != rhs.m_extFields.end(); iter++) {
m_extFields[iter->first.c_str()] = iter->second.c_str();
}
m_comment = rhs.m_comment;
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 && GetInheritsAsString() == rhs.GetInheritsAsString() &&
GetAccess() == rhs.GetAccess() && GetSignature() == rhs.GetSignature();
return res;
}
void TagEntry::Create(const wxString& fileName, const wxString& name, int lineNumber, const wxString& pattern,
const wxString& kind, wxStringMap_t& extFields)
{
m_flags = 0;
m_extFields = extFields;
SetName(name);
SetLine(lineNumber);
SetKind(kind.IsEmpty() ? "<unknown>" : kind);
SetPattern(pattern);
SetFile(wxFileName(fileName).GetFullPath());
SetId(-1);
wxString path;
// Check if we can get full name (including path)
static std::vector<wxString> scope_fields = { "class", "struct", "namespace", "interface", "enum", "function" };
for(const wxString& scope_field : scope_fields) {
path = GetExtField(scope_field);
if(!path.IsEmpty()) {
UpdatePath(path);
break;
}
}
// still no path?
// try union
if(path.empty()) {
path = GetExtField("union");
wxString tmpname = path.AfterLast(':');
if(!path.IsEmpty()) {
if(!tmpname.StartsWith("__anon")) {
UpdatePath(path);
} else {
// anonymouse union, remove the anonymous part from its name
path = path.BeforeLast(':');
path = path.BeforeLast(':');
UpdatePath(path);
}
}
}
// update the method properties
SetTagProperties(GetExtField("properties"));
if(!path.IsEmpty()) {
SetScope(path);
} else {
SetScope("<global>");
}
// If there is no path, path is set to name
if(GetPath().IsEmpty()) {
SetPath(GetName());
}
// Get the parent name
StringTokenizer tok(GetPath(), "::");
wxString parent;
(tok.Count() < 2) ? parent = "<global>" : parent = tok[tok.Count() - 2];
SetParent(parent);
// parse auto
m_assignment = TypenameFromPattern(this);
if(IsAuto(this)) {
m_tag_properties_flags |= TAG_PROP_AUTO_VARIABLE;
}
if(IsFunction() && GetName().StartsWith("__anon")) {
m_tag_properties_flags |= TAG_PROP_INLINE;
}
}
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;
wxStringMap_t::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(IsPrototype() || IsMacro()) {
key << GetKind() << ": ";
}
key << GetPath() << GetSignature();
return key;
}
wxString TagEntry::GetDisplayName() const
{
wxString name;
name << GetName() << GetSignature();
return name;
}
wxString TagEntry::GetFullDisplayName() const
{
wxString name;
if(GetParent() == "<global>") {
name << GetDisplayName();
} else {
name << GetParent() << "::" << GetName() << GetSignature();
}
return name;
}
bool TagEntry::IsClassTemplate() const
{
return m_extFields.count("template") && !m_extFields.find("template")->second.empty();
}
//----------------------------------------------------------------------------
// 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 IsClass() || IsStruct() || IsUnion() || IsNamespace() || IsEnumClass();
}
void TagEntry::UpdatePath(wxString& path)
{
if(!path.IsEmpty()) {
wxString name(path);
name += "::";
name += GetName();
SetPath(name);
}
}
const wxString& TagEntry::GetExtField(const wxString& extField) const
{
static wxString empty_string;
if(m_extFields.count(extField) == 0) {
return empty_string;
}
return m_extFields.find(extField)->second;
}
wxString TagEntry::GetPattern() const
{
wxString pattern(m_pattern);
// since ctags's pattern is regex, forward slashes are escaped. ('/' becomes '\/')
pattern.Replace("\\\\", "\\");
pattern.Replace("\\/", "/");
return pattern;
}
void TagEntry::FromLine(const wxString& line)
{
// label C:\src\wxCustomControls\clTreeCtrl\clChoice.cpp /^ const wxString& label = m_choices[i];$/;"
// local line:116 type:constwxString
wxString pattern, kind;
wxString strLine = line;
long lineNumber = wxNOT_FOUND;
wxStringMap_t extFields;
// get the token name
wxString name = strLine.BeforeFirst('\t');
strLine = strLine.AfterFirst('\t');
// get the file name
wxString fileName = strLine.BeforeFirst('\t');
strLine = strLine.AfterFirst('\t');
// here we can get two options:
// pattern followed by ;"
// or
// line number followed by ;"
int end = strLine.Find(";\"");
if(end == wxNOT_FOUND) {
// invalid pattern found
return;
}
if(strLine.StartsWith("/^")) {
// 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("\t")) {
strLine = strLine.AfterFirst('\t');
}
kind = strLine.BeforeFirst('\t');
strLine = strLine.AfterFirst('\t');
if(strLine.IsEmpty() == false) {
wxStringTokenizer tkz(strLine, '\t');
while(tkz.HasMoreTokens()) {
wxString token = tkz.NextToken();
wxString key = token.BeforeFirst(':');
wxString val = token.AfterFirst(':');
key = key.Trim();
key = key.Trim(false);
val = val.Trim();
val = val.Trim(false);
if(key == "line" && !val.IsEmpty()) {
val.ToLong(&lineNumber);
} else {
if(key == "union" || key == "struct") {
// remove the anonymous part of the struct / union
if(!val.StartsWith("__anon")) {
// an internal anonymous union / struct
// remove all parts of the
wxArrayString scopeArr;
wxString tmp, new_val;
scopeArr = wxStringTokenize(val, ":", wxTOKEN_STRTOK);
for(size_t i = 0; i < scopeArr.GetCount(); i++) {
if(scopeArr.Item(i).StartsWith("__anon") == false) {
tmp << scopeArr.Item(i) << "::";
}
}
tmp.EndsWith("::", &new_val);
val.swap(new_val);
}
}
extFields.insert({ key, val });
}
}
}
kind = kind.Trim();
name = name.Trim();
fileName = fileName.Trim();
pattern = pattern.Trim();
Create(fileName, name, lineNumber, pattern, kind, extFields);
}
bool TagEntry::IsConstructor() const
{
if(GetKind() != "function" && GetKind() != "prototype") {
return false;
}
return GetName() == GetScope();
}
bool TagEntry::IsDestructor() const
{
if(GetKind() != "function" && GetKind() != "prototype") {
return false;
}
return GetName().StartsWith("~");
}
wxString TagEntry::GetTemplateDefinition() const
{
// template:<class T>
wxString definition = GetExtField("template");
definition.Trim().Trim(false);
return definition;
}
void TagEntry::SetTypename(const wxString& val) { m_extFields["typeref"] = "typename:" + val; }
wxString TagEntry::GetTypename() const
{
// typeref:typename:void
const wxString& returnValue = GetExtField("typeref");
return returnValue.AfterFirst(':');
}
bool TagEntry::IsKeyword() const { return m_tag_kind == eTagKind::TAG_KIND_KEYWORD; }
bool TagEntry::IsEnum() const { return m_tag_kind == eTagKind::TAG_KIND_ENUM; }
bool TagEntry::IsEnumerator() const { return m_tag_kind == eTagKind::TAG_KIND_ENUMERATOR; }
bool TagEntry::IsParameter() const { return m_tag_kind == eTagKind::TAG_KIND_PARAMETER; }
bool TagEntry::IsVariable() const { return m_tag_kind == eTagKind::TAG_KIND_VARIABLE; }
bool TagEntry::IsEnumClass() const { return m_tag_kind == eTagKind::TAG_KIND_CENUM; }
bool TagEntry::IsLocalVariable() const { return m_tag_kind == eTagKind::TAG_KIND_LOCAL; }
bool TagEntry::IsMember() const { return m_tag_kind == eTagKind::TAG_KIND_MEMBER; }
bool TagEntry::IsNamespace() const { return m_tag_kind == eTagKind::TAG_KIND_NAMESPACE; }
bool TagEntry::IsFunction() const { return m_tag_kind == eTagKind::TAG_KIND_FUNCTION; }
bool TagEntry::IsPrototype() const { return m_tag_kind == eTagKind::TAG_KIND_PROTOTYPE; }
bool TagEntry::IsClass() const { return m_tag_kind == eTagKind::TAG_KIND_CLASS; }
bool TagEntry::IsMacro() const { return m_tag_kind == eTagKind::TAG_KIND_MACRO; }
bool TagEntry::IsStruct() const { return m_tag_kind == eTagKind::TAG_KIND_STRUCT; }
bool TagEntry::IsUnion() const { return m_tag_kind == eTagKind::TAG_KIND_UNION; }
bool TagEntry::IsTypedef() const { return m_tag_kind == eTagKind::TAG_KIND_TYPEDEF; }
bool TagEntry::IsMethod() const { return IsPrototype() || IsFunction(); }
bool TagEntry::IsScopeGlobal() const { return GetScope().IsEmpty() || GetScope() == "<global>"; }
wxString TagEntry::GetInheritsAsString() const { return GetExtField(_T("inherits")); }
wxArrayString TagEntry::GetInheritsAsArrayNoTemplates() const
{
// Parse the input string
wxString inherits = GetInheritsAsString();
wxString parent;
wxArrayString parentsArr;
int depth(0);
for(size_t i = 0; i < inherits.Length(); i++) {
wxChar ch = inherits.GetChar(i);
switch(ch) {
case '<':
if(depth == 0 && parent.IsEmpty() == false) {
parent.Trim().Trim(false);
parentsArr.Add(parent);
parent.Clear();
}
depth++;
break;
case '>':
depth--;
break;
case ',':
if(depth == 0 && parent.IsEmpty() == false) {
parent.Trim().Trim(false);
parentsArr.Add(parent);
parent.Clear();
}
break;
default:
if(depth == 0) {
parent << ch;
}
break;
}
}
if(parent.IsEmpty() == false) {
parent.Trim().Trim(false);
parentsArr.Add(parent);
}
return parentsArr;
}
wxArrayString TagEntry::GetInheritsAsArrayWithTemplates() const
{
// Parse the input string
wxString inherits = GetInheritsAsString();
wxString parent;
wxArrayString parentsArr;
int depth(0);
for(size_t i = 0; i < inherits.Length(); i++) {
wxChar ch = inherits.GetChar(i);
switch(ch) {
case '<':
depth++;
parent << ch;
break;
case '>':
depth--;
parent << ch;
break;
case ',':
if(depth == 0 && parent.IsEmpty() == false) {
parent.Trim().Trim(false);
parentsArr.Add(parent);
parent.Clear();
} else if(depth != 0) {
parent << ch;
}
break;
default:
parent << ch;
break;
}
}
if(parent.IsEmpty() == false) {
parent.Trim().Trim(false);
parentsArr.Add(parent);
}
return parentsArr;
}
TagEntryPtr TagEntry::ReplaceSimpleMacro()
{
if(IsMacro()) {
PPToken tok = TagsManagerST::Get()->GetDatabase()->GetMacro(GetName());
if(tok.flags & PPToken::IsValid && !(tok.flags & PPToken::IsFunctionLike)) {
std::vector<TagEntryPtr> tags;
TagsManagerST::Get()->FindByNameAndScope(tok.replacement, GetScopeName(), tags);
if(tags.size() == 1) {
// replace the current tag content with the new match
return tags.at(0);
}
}
}
return NULL;
}
bool TagEntry::IsTemplateFunction() const
{
wxString pattern = GetPatternClean();
pattern.Trim().Trim(false);
return IsMethod() && pattern.StartsWith("template ");
}
wxString TagEntry::GetPatternClean() const
{
wxString p = GetPattern();
p.Trim();
if(p.StartsWith("/^")) {
p.Replace("/^", "");
}
if(p.EndsWith("$/")) {
p.Replace("$/", "");
}
return p;
}
wxString TagEntry::GetLocalType() const { return GetExtField("type"); }
namespace
{
inline void enable_function_flag_if_exists(const wxStringSet_t& S, const wxString& propname, const size_t flag,
size_t& flags)
{
if(S.count(propname)) {
flags |= flag;
} else {
flags &= ~flag;
}
}
} // namespace
void TagEntry::SetTagProperties(const wxString& props)
{
m_tag_properties = props;
auto tokens = wxStringTokenize(m_tag_properties, ",", wxTOKEN_STRTOK);
wxStringSet_t S;
for(auto& token : tokens) {
token.Trim().Trim(false);
S.insert(token);
}
enable_function_flag_if_exists(S, "const", TAG_PROP_CONST, m_tag_properties_flags);
enable_function_flag_if_exists(S, "virtual", TAG_PROP_VIRTUAL, m_tag_properties_flags);
enable_function_flag_if_exists(S, "default", TAG_PROP_DEFAULT, m_tag_properties_flags);
enable_function_flag_if_exists(S, "delete", TAG_PROP_DELETED, m_tag_properties_flags);
enable_function_flag_if_exists(S, "static", TAG_PROP_STATIC, m_tag_properties_flags);
enable_function_flag_if_exists(S, "inline", TAG_PROP_INLINE, m_tag_properties_flags);
enable_function_flag_if_exists(S, "override", TAG_PROP_OVERRIDE, m_tag_properties_flags);
enable_function_flag_if_exists(S, "pure", TAG_PROP_PURE, m_tag_properties_flags);
enable_function_flag_if_exists(S, "scopedenum", TAG_PROP_SCOPEDENUM, m_tag_properties_flags);
// change the kind to "enum class"
if(is_scoped_enum()) {
m_tag_kind = eTagKind::TAG_KIND_CENUM;
}
}
bool TagEntry::is_func_virtual() const { return m_tag_properties_flags & TAG_PROP_VIRTUAL; }
bool TagEntry::is_func_default() const { return m_tag_properties_flags & TAG_PROP_DEFAULT; }
bool TagEntry::is_func_override() const { return m_tag_properties_flags & TAG_PROP_OVERRIDE; }
bool TagEntry::is_func_deleted() const { return m_tag_properties_flags & TAG_PROP_DELETED; }
bool TagEntry::is_func_inline() const { return m_tag_properties_flags & TAG_PROP_INLINE; }
bool TagEntry::is_func_pure() const { return m_tag_properties_flags & TAG_PROP_PURE; }
bool TagEntry::is_scoped_enum() const { return m_tag_properties_flags & TAG_PROP_SCOPEDENUM; }
bool TagEntry::is_const() const { return m_tag_properties_flags & TAG_PROP_CONST; }
bool TagEntry::is_static() const { return m_tag_properties_flags & TAG_PROP_STATIC; }
bool TagEntry::is_auto() const { return m_tag_properties_flags & TAG_PROP_AUTO_VARIABLE; }
bool TagEntry::is_lambda() const { return m_tag_properties_flags & TAG_PROP_LAMBDA; }
wxString TagEntry::GetFunctionDeclaration() const
{
if(!IsMethod()) {
return wxEmptyString;
}
wxString decl;
if(is_func_inline()) {
decl << "inline ";
}
if(is_func_virtual()) {
decl << "virtual ";
}
decl << GetTypename() << " ";
if(!GetScope().empty()) {
decl << GetScope() << "::";
}
decl << GetName() << GetSignature();
if(is_const()) {
decl << " const";
}
if(is_func_pure()) {
decl << " = 0";
}
decl << ";";
return decl;
}
wxString TagEntry::GetFunctionDefinition() const
{
wxString impl;
if(!IsMethod()) {
return wxEmptyString;
}
impl << GetTypename() << " ";
if(!GetScope().empty()) {
impl << GetScope() << "::";
}
CompletionHelper helper;
impl << helper.normalize_function(this, CompletionHelper::STRIP_NO_DEFAULT_VALUES);
// release the pointer from the smart-ptr
// this ensure that `this` is not deleted
return impl;
}
namespace
{
std::unordered_map<wxString, eTagKind> g_kind_table = {
{ "class", eTagKind::TAG_KIND_CLASS }, { "struct", eTagKind::TAG_KIND_STRUCT },
{ "namespace", eTagKind::TAG_KIND_NAMESPACE }, { "union", eTagKind::TAG_KIND_UNION },
{ "enum", eTagKind::TAG_KIND_ENUM }, { "member", eTagKind::TAG_KIND_MEMBER },
{ "variable", eTagKind::TAG_KIND_VARIABLE }, { "macro", eTagKind::TAG_KIND_MACRO },
{ "typedef", eTagKind::TAG_KIND_TYPEDEF }, { "local", eTagKind::TAG_KIND_LOCAL },
{ "parameter", eTagKind::TAG_KIND_PARAMETER }, { "prototype", eTagKind::TAG_KIND_PROTOTYPE },
{ "cpp_keyword", eTagKind::TAG_KIND_KEYWORD }, { "keyword", eTagKind::TAG_KIND_KEYWORD },
{ "function", eTagKind::TAG_KIND_FUNCTION }, { "enumerator", eTagKind::TAG_KIND_ENUMERATOR },
};
}
void TagEntry::SetKind(const wxString& kind)
{
// set the string kind
m_kind = kind;
// turn on bits
m_tag_kind = eTagKind::TAG_KIND_UNKNOWN;
if(g_kind_table.count(m_kind)) {
m_tag_kind = g_kind_table[m_kind];
}
}
namespace
{
void read_until_find(CxxTokenizer& tokenizer, CxxLexerToken& token, int type_1, int type_2, int* what_was_found,
wxString* consumed)
{
// search until we find the `=`
int depth = 0;
consumed->clear();
*what_was_found = 0;
consumed->reserve(256); // 256 bytes should be enough for most cases
while(tokenizer.NextToken(token)) {
if(depth == 0 && token.GetType() == type_1) {
*what_was_found = type_1;
consumed->Trim().Trim(false);
return;
} else if(depth == 0 && token.GetType() == type_2) {
*what_was_found = type_2;
consumed->Trim().Trim(false);
return;
}
if(token.is_keyword() || token.is_builtin_type()) {
consumed->Append(token.GetWXString() + " ");
continue;
} else if(token.is_pp_keyword()) {
continue;
}
// append it
consumed->Append(token.GetWXString());
switch(token.GetType()) {
case '<':
case '{':
case '[':
case '(':
depth++;
break;
case '>':
case '}':
case ']':
case ')':
depth--;
break;
default:
break;
}
}
// eof
consumed->Trim().Trim(false);
}
} // namespace
#define CHECK_FOUND(What, Expected) \
if(What != Expected) \
return wxEmptyString
wxString TagEntry::TypenameFromPattern(const TagEntry* tag)
{
if(!tag->IsLocalVariable() && !tag->IsVariable()) {
return wxEmptyString;
}
CxxTokenizer tokenizer;
CxxLexerToken token;
tokenizer.Reset(tag->GetPatternClean());
// search until we find the `=`
int what_was_found = 0;
wxString consumed;
read_until_find(tokenizer, token, T_FOR, '=', &what_was_found, &consumed);
if(what_was_found == 0) {
return wxEmptyString;
}
if(what_was_found == '=') {
// read until we reach eof or ';'
read_until_find(tokenizer, token, ';', 0, &what_was_found, &consumed);
return consumed;
} else {
// we are in a for loop
// search for the ':'
read_until_find(tokenizer, token, '(', 0, &what_was_found, &consumed);
CHECK_FOUND('(', what_was_found);
read_until_find(tokenizer, token, ':', 0, &what_was_found, &consumed);
CHECK_FOUND(':', what_was_found);
// we found the ':', now read until we find the ')'
read_until_find(tokenizer, token, ')', 0, &what_was_found, &consumed);
CHECK_FOUND(')', what_was_found);
// for(... : expression)
// we return expression.begin()
consumed += ".begin()";
return consumed;
}
return wxEmptyString;
}
bool TagEntry::IsAuto(const TagEntry* tag)
{
CxxTokenizer tokenizer;
CxxLexerToken token;
tokenizer.Reset(tag->GetTypename());
int found = 0;
wxString consumed;
read_until_find(tokenizer, token, T_AUTO, 0, &found, &consumed);
return found == T_AUTO;
}
wxString TagEntry::GetMacrodef() const { return GetExtField("macrodef"); }
void TagEntry::SetMacrodef(const wxString& value) { set_extra_field("macrodef", value); }
void TagEntry::set_extra_field(const wxString& name, const wxString& value)
{
if(m_extFields.count(name)) {
m_extFields.erase(name);
}
m_extFields.insert({ name, value });
}
|