File: CxxVariableScanner.cpp

package info (click to toggle)
codelite 10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 71,364 kB
  • sloc: cpp: 415,397; ansic: 18,277; php: 9,547; lex: 4,181; yacc: 2,820; python: 2,294; sh: 383; makefile: 51; xml: 13
file content (465 lines) | stat: -rw-r--r-- 14,759 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
#include "CxxVariableScanner.h"
#include "CxxScannerTokens.h"
#include <algorithm>

CxxVariableScanner::CxxVariableScanner(const wxString& buffer)
    : m_scanner(NULL)
    , m_buffer(buffer)
    , m_eof(false)
    , m_parenthesisDepth(0)
{
    m_nativeTypes.insert(T_AUTO);
    m_nativeTypes.insert(T_BOOL);
    m_nativeTypes.insert(T_CHAR);
    m_nativeTypes.insert(T_CHAR16_T);
    m_nativeTypes.insert(T_CHAR32_T);
    m_nativeTypes.insert(T_DOUBLE);
    m_nativeTypes.insert(T_FLOAT);
    m_nativeTypes.insert(T_INT);
    m_nativeTypes.insert(T_LONG);
    m_nativeTypes.insert(T_SHORT);
    m_nativeTypes.insert(T_SIGNED);
    m_nativeTypes.insert(T_UNSIGNED);
    m_nativeTypes.insert(T_VOID);
    m_nativeTypes.insert(T_WCHAR_T);
}

CxxVariableScanner::~CxxVariableScanner() {}

CxxVariable::List_t CxxVariableScanner::GetVariables()
{
    wxString strippedBuffer, parenthesisBuffer;
    OptimizeBuffer(strippedBuffer, parenthesisBuffer);
    CxxVariable::List_t vars = DoGetVariables(strippedBuffer);
    CxxVariable::List_t args = DoGetVariables(parenthesisBuffer);
    vars.insert(vars.end(), args.begin(), args.end());
    vars.sort([&](CxxVariable::Ptr_t a, CxxVariable::Ptr_t b) { return a->GetName().CmpNoCase(b->GetName()); });
    return vars;
}

bool CxxVariableScanner::ReadType(CxxVariable::LexerToken::List_t& vartype)
{
    int depth = 0;
    CxxLexerToken token;
    while(GetNextToken(token)) {
        if(depth == 0) {
            if(vartype.empty()) {
                // a type can start the following tokens
                switch(token.type) {
                case T_CLASS:
                case T_STRUCT: {
                    // Class or struct forward decl
                    std::set<int> delims;
                    delims.insert(';'); // Forward decl
                    delims.insert('{'); // Class body
                    wxString consumed;
                    ReadUntil(delims, token, consumed);
                    return false;
                }
                case T_IDENTIFIER:
                case T_DOUBLE_COLONS:
                case T_AUTO:
                case T_BOOL:
                case T_CHAR:
                case T_CHAR16_T:
                case T_CHAR32_T:
                case T_CONST:
                case T_CONSTEXPR:
                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_USING:
                case T_WCHAR_T: {
                    vartype.push_back(CxxVariable::LexerToken(token));
                    break;
                }
                default:
                    // Not a type definition
                    return false;
                }
            } else {
                const CxxVariable::LexerToken& lastToken = vartype.back();
                switch(token.type) {
                case T_IDENTIFIER: {
                    if(TypeHasIdentifier(vartype) && (vartype.back().type != T_DOUBLE_COLONS)) {
                        // We already found the identifier for this type, its probably part of the name
                        ::LexerUnget(m_scanner);
                        return true;
                    } else if(HasTypeInList(vartype)) {
                        ::LexerUnget(m_scanner);
                        return true;
                    }
                    // Found an identifier
                    // We consider this part of the type only if the previous token was "::" or
                    // if it was T_CONST
                    switch(lastToken.type) {
                    case T_DOUBLE_COLONS:
                    case T_CONST:
                    case T_CONSTEXPR:
                    case T_REGISTER:
                    case T_MUTABLE:
                    case T_VOLATILE:
                    case T_STATIC:
                        vartype.push_back(token);
                        break;
                    default:
                        ::LexerUnget(m_scanner);
                        return true;
                    }
                    break;
                case T_DOUBLE_COLONS:
                case T_BOOL:
                case T_CHAR:
                case T_CHAR16_T:
                case T_CHAR32_T:
                case T_CONST:
                case T_CONSTEXPR:
                case T_DOUBLE:
                case T_FLOAT:
                case T_INT:
                case T_LONG:
                case T_SHORT:
                case T_SIGNED:
                case T_UNSIGNED:
                case T_VOID:
                case T_AUTO:
                case T_WCHAR_T: {
                    vartype.push_back(CxxVariable::LexerToken(token));
                    break;
                }
                }
                case '<':
                case '[':
                    vartype.push_back(CxxVariable::LexerToken(token));
                    depth++;
                    break;
                case '*':
                case '&':
                    // Part of the name
                    ::LexerUnget(m_scanner);
                    return true;
                default:
                    return false;
                }
            }
        } else {
            // Depth > 0
            vartype.push_back(token);
            if(token.type == '>' || token.type == ']') {
                --depth;
            }
        }
    }
    return false;
}

bool CxxVariableScanner::ReadName(wxString& varname, wxString& pointerOrRef, wxString& varInitialization)
{
    CxxLexerToken token;
    while(GetNextToken(token)) {
        if(token.type == T_IDENTIFIER) {
            varname = token.text;
            ConsumeInitialization(varInitialization);

            // Now that we got the name, check if have more variables to expect
            if(!GetNextToken(token)) {
                return false;
            }

            if((token.type == '{') && !varInitialization.IsEmpty()) {
                // Don't collect functions and consider them as variables
                ::LexerUnget(m_scanner);
                varname.clear();
                return false;
            }

            // If we found comma, return true
            if(token.type == ',') {
                return true;
            } else {
                ::LexerUnget(m_scanner);
                return false;
            }

        } else if(token.type == '*' || token.type == '&') {
            pointerOrRef << token.text;
        } else {
            return false;
        }
    }
    return false;
}

void CxxVariableScanner::ConsumeInitialization(wxString& consumed)
{
    CxxLexerToken token;
    wxString dummy;
    if(!GetNextToken(token)) return;
    int type = wxNOT_FOUND;
    int tokType = token.type;
    if(tokType == '(') {
        // Read the initialization
        std::set<int> delims;
        delims.insert(')');
        consumed = "(";
        if(ReadUntil(delims, token, consumed) == wxNOT_FOUND) {
            return;
        }
        // Now read until the delimiter
        delims.clear();
        delims.insert(';');
        delims.insert(',');
        delims.insert('{');
        type = ReadUntil(delims, token, dummy);

    } else if(tokType == '{') {
        // Read the initialization
        std::set<int> delims;
        delims.insert('}');
        consumed = "{";
        if(ReadUntil(delims, token, consumed) == wxNOT_FOUND) {
            return;
        }
        // Now read until the delimiter
        delims.clear();
        delims.insert(';');
        delims.insert(',');
        delims.insert('{');
        type = ReadUntil(delims, token, dummy);

    } else if(tokType == '=') {
        std::set<int> delims;
        delims.insert(';');
        delims.insert(',');
        delims.insert('{');
        type = ReadUntil(delims, token, consumed);
    } else {
        ::LexerUnget(m_scanner);
        consumed.clear();
        std::set<int> delims;
        delims.insert(';');
        delims.insert(',');
        delims.insert('{');
        type = ReadUntil(delims, token, dummy);
    }

    if(type == ',' || type == '{') {
        ::LexerUnget(m_scanner);
    }
}

int CxxVariableScanner::ReadUntil(const std::set<int>& delims, CxxLexerToken& token, wxString& consumed)
{
    // loop until we find the open brace
    int depth = 0;
    while(GetNextToken(token)) {
        consumed << token.text << " ";
        if(depth == 0) {
            if(delims.count(token.type)) {
                return token.type;
            } else {
                switch(token.type) {
                case '<':
                case '{':
                case '[':
                case '(':
                    depth++;
                    break;
                default:
                    // ignore it
                    break;
                }
            }
        } else {
            switch(token.type) {
            case '>':
            case '}':
            case ']':
            case ')':
                depth--;
                break;
            default:
                // ignore it
                break;
            }
        }
    }
    return wxNOT_FOUND;
}

bool CxxVariableScanner::GetNextToken(CxxLexerToken& token)
{
    bool res = ::LexerNext(m_scanner, token);
    m_eof = !res;
    switch(token.type) {
    case '(':
        ++m_parenthesisDepth;
        break;
    case ')':
        --m_parenthesisDepth;
        break;
    default:
        break;
    }
    return res;
}

void CxxVariableScanner::OptimizeBuffer(wxString& strippedBuffer, wxString& parenthesisBuffer)
{
    Scanner_t sc = ::LexerNew(m_buffer);
    int depth = 0;
    CxxLexerToken tok;
    CxxLexerToken lastToken;

    eState state = kNormal;
    while(::LexerNext(sc, tok)) {
        if(state == kNormal) {
            if((tok.type >= T_PP_DEFINE) && (tok.type <= T_PP_LTEQ)) {
                // In PreProcessor state
                state = kPreProcessor;
            } else {
                switch(tok.type) {
                case '(':
                    depth++;
                    strippedBuffer << "(";
                    if((lastToken.type == T_FOR) && false) {
                        state = kInForLoop;
                    } else if(lastToken.type == T_CATCH) {
                        state = kInCatch;
                    } else {
                        state = kInParen;
                        parenthesisBuffer << "(";
                    }
                    break;
                default:
                    strippedBuffer << tok.text << " ";
                    break;
                }
            }
        } else if(state == kInParen) {
            switch(tok.type) {
            case '(':
                depth++;
                parenthesisBuffer << "(";
                break;
            case ')':
                depth--;
                parenthesisBuffer << ")";
                if(depth == 0) {
                    parenthesisBuffer << ";"; // Needed to help the parser
                    state = kNormal;
                    strippedBuffer << ")";
                }
                break;
            default:
                parenthesisBuffer << tok.text << " ";
                break;
            }
        } else if((state == kInForLoop) || (state == kInCatch)) {
            // 'for' and 'catch' parenthesis content is kept in the strippedBuffer
            switch(tok.type) {
            case '(':
                depth++;
                strippedBuffer << "(";
                break;
            case ')':
                depth--;
                strippedBuffer << ")";
                if(depth == 0) {
                    state = kNormal;
                }
                break;
            default:
                strippedBuffer << tok.text << " ";
                break;
            }
        } else if(state == kPreProcessor) {
            switch(tok.type) {
            case T_PP_STATE_EXIT:
                state = kNormal;
                break;
            default:
                break;
            }
        }
        lastToken = tok;
    }
    ::LexerDestroy(&sc);
}

CxxVariable::List_t CxxVariableScanner::DoGetVariables(const wxString& buffer)
{
    // First, we strip all parenthesis content from the buffer
    m_scanner = ::LexerNew(buffer);
    m_eof = false;
    m_parenthesisDepth = 0;
    if(!m_scanner) return CxxVariable::List_t(); // Empty list

    CxxVariable::List_t vars;

    // Read the variable type
    while(!IsEof()) {
        CxxVariable::LexerToken::List_t vartype;
        if(!ReadType(vartype)) continue;

        // Get the variable(s) name
        wxString varname, pointerOrRef, varInitialization;
        bool cont = false;
        do {
            cont = ReadName(varname, pointerOrRef, varInitialization);
            CxxVariable::Ptr_t var(new CxxVariable());
            var->SetName(varname);
            var->SetType(vartype);
            if(var->IsOk()) {
                vars.push_back(var);
            } else if(!varInitialization.IsEmpty()) {
                // This means that the above was a function call
                // Parse the siganture which is placed inside the varInitialization
                CxxVariableScanner scanner(varInitialization);
                CxxVariable::List_t args = scanner.GetVariables();
                vars.insert(vars.end(), args.begin(), args.end());
                break;
            }
        } while(cont && (m_parenthesisDepth == 0) /* not inside a function */);
    }

    // Sort the variables by name
    ::LexerDestroy(&m_scanner);
    return vars;
}

bool CxxVariableScanner::TypeHasIdentifier(const CxxVariable::LexerToken::List_t& type)
{
    // do we have an identifier in the type?
    CxxVariable::LexerToken::List_t::const_iterator iter = std::find_if(
        type.begin(), type.end(), [&](const CxxVariable::LexerToken& token) { return (token.type == T_IDENTIFIER); });
    return (iter != type.end());
}

CxxVariable::Map_t CxxVariableScanner::GetVariablesMap()
{
    CxxVariable::List_t l = GetVariables();
    CxxVariable::Map_t m;
    std::for_each(l.begin(), l.end(), [&](CxxVariable::Ptr_t v) {
        if(m.count(v->GetName()) == 0) {
            m.insert(std::make_pair(v->GetName(), v));
        }
    });
    return m;
}

bool CxxVariableScanner::HasTypeInList(const CxxVariable::LexerToken::List_t& type) const
{
    CxxVariable::LexerToken::List_t::const_iterator iter = std::find_if(type.begin(), type.end(),
        [&](const CxxVariable::LexerToken& token) { return m_nativeTypes.count(token.type) != 0; });
    return (iter != type.end());
}