aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/ApiExtractor/clangparser/clangdebugutils.cpp
blob: 3c002da9c0d9e059aac147eb4a7930670e629175 (plain)
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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "clangdebugutils.h"
#include "clangutils.h"

#include <QtCore/QDebug>
#include <QtCore/QString>

#ifndef QT_NO_DEBUG_STREAM

#ifdef Q_OS_WIN
const char pathSep = '\\';
#else
const char pathSep = '/';
#endif

static const char *baseName(const char *fileName)
{
    const char *b = std::strrchr(fileName, pathSep);
    return b ? b + 1 : fileName;
}

QDebug operator<<(QDebug s, const CXString &cs)
{
    s << clang_getCString(cs);
    return s;
}

QDebug operator<<(QDebug s, CXCursorKind cursorKind) // Enum
{
    const CXString kindName = clang_getCursorKindSpelling(cursorKind);
    s << kindName;
    clang_disposeString(kindName);
    return s;
}

static const char *accessSpecsStrings[]
{
    // CX_CXXInvalidAccessSpecifier, CX_CXXPublic, CX_CXXProtected, CX_CXXPrivate
    "invalid",  "public", "protected", "private"
};

QDebug operator<<(QDebug s, CX_CXXAccessSpecifier ac)
{
    s << accessSpecsStrings[ac];
    return s;
}

struct formatCXTypeName
{
    explicit formatCXTypeName(const CXType &type) : m_type(type) {}

    const CXType &m_type;
};

QDebug operator<<(QDebug debug, const formatCXTypeName &ft)
{
    CXString typeSpelling = clang_getTypeSpelling(ft.m_type);
    debug << typeSpelling;
    clang_disposeString(typeSpelling);
    return debug;
}

QDebug operator<<(QDebug debug, const CXType &type)
{
    QDebugStateSaver saver(debug);
    debug.nospace();
    debug.noquote();
    debug << "CXType(";
    if (type.kind == CXType_Invalid) {
        debug << "invalid)";
        return debug;
    }

    debug << type.kind;
    switch (type.kind) {
    case CXType_Unexposed:
        debug << " [unexposed]";
        break;
    case CXType_Elaborated:
        debug << " [elaborated]";
        break;
    default:
        break;
    }
    debug << ", " << formatCXTypeName(type) << ')';
    return debug;
}

QDebug operator<<(QDebug debug, const CXCursor &cursor)
{
    QDebugStateSaver saver(debug);
    debug.nospace();
    debug.noquote();
    const CXCursorKind kind = clang_getCursorKind(cursor);
    debug << "CXCursor(";
    if (kind >= CXCursor_FirstInvalid && kind <= CXCursor_LastInvalid) {
        debug << "invalid)";
        return debug;
    }

    const QString cursorSpelling = clang::getCursorSpelling(cursor);
    debug << '"' << cursorSpelling << '"';
    CXString cursorDisplay = clang_getCursorDisplayName(cursor);
    if (const char *dpy = clang_getCString(cursorDisplay)) {
        const QString display = QString::fromUtf8(dpy);
        if (display != cursorSpelling)
            debug << ", display=\"" << dpy << '"';
    }
    clang_disposeString(cursorDisplay);

    debug << ", kind=" << kind;

    const CXType type = clang_getCursorType(cursor);
    switch (kind) {
        case CXCursor_CXXAccessSpecifier:
        debug << ", " << clang_getCXXAccessSpecifier(cursor);
        break;
    case CXCursor_CXXBaseSpecifier:
         debug << ", inherits=\"" << clang::getCursorSpelling(clang_getTypeDeclaration(type)) << '"';
         break;
    case CXCursor_CXXMethod:
    case CXCursor_FunctionDecl:
    case CXCursor_ConversionFunction:
         debug << ", result type=\""
            << formatCXTypeName(clang_getCursorResultType(cursor)) << '"';
        break;
    case CXCursor_TypedefDecl:
        debug << ", underlyingType=\""
            << formatCXTypeName(clang_getTypedefDeclUnderlyingType(cursor)) << '"';
        break;
    default:
        break;
    }

    debug << ", type=\"" << formatCXTypeName(type) << '"';
    if (clang_Cursor_hasAttrs(cursor))
        debug << ", [attrs]";

    debug << ')';
    return debug;
}

QDebug operator<<(QDebug s, const CXSourceLocation &location)
{
    QDebugStateSaver saver(s);
    s.nospace();
    CXFile file; // void *
    unsigned line;
    unsigned column;
    unsigned offset;
    clang_getExpansionLocation(location, &file, &line, &column, &offset);
    const CXString cxFileName = clang_getFileName(file);
    // Has been observed to be 0 for invalid locations
    if (const char *cFileName = clang_getCString(cxFileName))
        s << baseName(cFileName) << ':';
    s << line << ':' << column;
    clang_disposeString(cxFileName);
    return s;
}

QDebug operator<<(QDebug s, const std::string_view &v)
{
    QDebugStateSaver saver(s);
    s.nospace();
    s.noquote();
    s << '"';
    for (auto c : v)
        s << c;
    s << '"';
    return s;
}

#endif // !QT_NO_DEBUG_STREAM