File: ast_utils.cpp

package info (click to toggle)
umbrello 4%3A25.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,212 kB
  • sloc: cpp: 144,235; php: 2,405; sh: 855; xml: 354; cs: 309; java: 91; python: 68; makefile: 11; sql: 7
file content (129 lines) | stat: -rw-r--r-- 3,700 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2002 Roberto Raggi <roberto@kdevelop.org>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "ast_utils.h"

#include "ast.h"
#include "debug_utils.h"

#include <QRegularExpression>
#include <QStringList>

#include <KLocalizedString>

void scopeOfNode(AST* ast, QStringList& scope)
{
    if (!ast)
        return;

    if (ast->parent())
        scopeOfNode(ast->parent(), scope);

    QString s;
    switch (ast->nodeType()) {
    case NodeType_ClassSpecifier:
        if (((ClassSpecifierAST*)ast)->name()) {
            s = ((ClassSpecifierAST*)ast)->name()->text();
            s = s.isEmpty() ? QString::fromLatin1("<unnamed>") : s;
            scope.push_back(s);
        }
        break;

    case NodeType_Namespace: {
        AST* namespaceName = ((NamespaceAST*)ast)->namespaceName();
        s = namespaceName ? namespaceName->text() : QString::fromLatin1("<unnamed>");
        scope.push_back(s);
    }
    break;

    case NodeType_FunctionDefinition: {
        FunctionDefinitionAST* funDef = static_cast<FunctionDefinitionAST*>(ast);
        DeclaratorAST* d = funDef->initDeclarator()->declarator();

        // hotfix for bug #68726
        if (!d->declaratorId())
            break;

        QList<ClassOrNamespaceNameAST*> l = d->declaratorId()->classOrNamespaceNameList();
        for (int i = 0; i < l.size(); ++i) {
            AST* name = l.at(i)->name();
            scope.push_back(name->text());
        }
    }
    break;

    default:
        break;
    }
}

QString typeSpecToString(TypeSpecifierAST* typeSpec)  /// @todo remove
{
    if (!typeSpec)
        return QString();

    return typeSpec->text().replace(QRegularExpression(QLatin1String(" :: ")), QLatin1String("::"));
}

QString declaratorToString(DeclaratorAST* declarator, const QString& scope, bool skipPtrOp)
{
    if (!declarator)
        return QString();

    QString text;

    if (!skipPtrOp) {
        QList<AST*> ptrOpList = declarator->ptrOpList();
        for (int i = 0; i < ptrOpList.size(); ++i) {
            text += ptrOpList.at(i)->text();
        }
        text += QLatin1Char(' ');
    }

    text += scope;

    if (declarator->subDeclarator())
        text += QString::fromLatin1("(") + declaratorToString(declarator->subDeclarator()) + QString::fromLatin1(")");

    if (declarator->declaratorId())
        text += declarator->declaratorId()->text();

    QList<AST*> arrays = declarator->arrayDimensionList();
    for (int i = 0; i < arrays.size(); ++i) {
        text += QLatin1String("[]");
    }

    if (declarator->parameterDeclarationClause()) {
        text += QLatin1Char('(');

        ParameterDeclarationListAST* l = declarator->parameterDeclarationClause()->parameterDeclarationList();
        if (l != nullptr) {
            QList<ParameterDeclarationAST*> params = l->parameterList();
            for (int i = 0; i < params.size(); ++i) {
                QString type = typeSpecToString(params.at(i)->typeSpec());
                text += type;
                if (!type.isEmpty())
                    text += QLatin1Char(' ');
                text += declaratorToString(params.at(i)->declarator());

                if (params.at(i))
                    text += QLatin1String(", ");
            }
        }

        text += QLatin1Char(')');

        if (declarator->constant() != nullptr)
            text += QLatin1String(" const");

        if (declarator->override_() != nullptr)
            text += QLatin1String(" override");

        if (declarator->final_() != nullptr)
            text += QLatin1String(" final");
    }

    return text.replace(QRegularExpression(QLatin1String(" :: ")), QLatin1String("::")).simplified();
}