File: CxxVariable.cpp

package info (click to toggle)
codelite 12.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (94 lines) | stat: -rw-r--r-- 2,857 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
#include "CxxScannerTokens.h"
#include "CxxVariable.h"
#include <algorithm>

CxxVariable::CxxVariable(eCxxStandard standard)
    : m_standard(standard)
    , m_isAuto(false)
{
}

CxxVariable::~CxxVariable() {}

wxString CxxVariable::GetTypeAsString(const wxStringTable_t& table) const
{
    return PackType(m_type, m_standard, false, table);
}
wxString CxxVariable::GetTypeAsCxxString(const wxStringTable_t& table) const
{
    return PackType(m_type, m_standard, true, table);
}

wxString CxxVariable::ToString(size_t flags, const wxStringTable_t& table) const
{
    wxString str;
    str << GetTypeAsString(table);

    if(!GetPointerOrReference().IsEmpty()) { str << GetPointerOrReference(); }

    if(flags & kToString_Name) { str << " " << GetName(); }

    if((flags & kToString_DefaultValue) && !GetDefaultValue().IsEmpty()) { str << " = " << GetDefaultValue(); }
    return str;
}

wxString CxxVariable::PackType(const CxxVariable::LexerToken::Vec_t& type, eCxxStandard standard, bool omitClassKeyword,
                               const wxStringTable_t& table)
{
    // Example:
    // const std::vector<std::pair<int, int> >& v
    // "strcut stat buff" if we pass "omitClassKeyword" as true, the type is set to "stat" only
    wxString s;
    std::for_each(type.begin(), type.end(), [&](const CxxVariable::LexerToken& tok) {
        // "strcut stat buff" if we pass "omitClassKeyword" as true, the type is set to "stat" only
        // we do the same for class, enum and struct
        if(s.empty() && (tok.type == T_CLASS || tok.type == T_STRUCT || tok.type == T_ENUM) && omitClassKeyword) return;

        if((!s.empty() && s.Last() == ' ') &&
           ((tok.type == ',') || (tok.type == '>') || tok.type == '(' || tok.type == ')')) {
            s.RemoveLast();
        }

        // Do we need to revert macros?
        if((tok.GetType() == T_IDENTIFIER) && !table.empty() && table.count(tok.text)) {
            s << table.find(tok.text)->second;
        } else {
            s << tok.text;
        }

        if(standard == eCxxStandard::kCxx03 && (tok.type == '>')) {
            if(s.length() > 1 && s.EndsWith(">>")) {
                s.RemoveLast(2);
                s << "> >";
            }
        }

        switch(tok.type) {
        case T_AUTO:
        case T_BOOL:
        case T_CHAR:
        case T_CHAR16_T:
        case T_CHAR32_T:
        case T_CONST:
        case T_DOUBLE:
        case T_FLOAT:
        case T_INT:
        case T_LONG:
        case T_MUTABLE:
        case T_REGISTER:
        case T_SHORT:
        case T_SIGNED:
        case T_STATIC:
        case T_UNSIGNED:
        case T_VOLATILE:
        case T_VOID:
        case T_WCHAR_T:
        case ',':
        case '>':
            s << " ";
            break;
        }
    });
    if(!s.empty() && s.EndsWith(" ")) { s.RemoveLast(); }
    return s;
}