1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
---|---|
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "clangcursor.h" |
6 | |
7 | |
8 | ClangCursor::ClangCursor(const CXCursor &_cursor) |
9 | : cursor(_cursor) |
10 | { |
11 | } |
12 | |
13 | QString toCString(const CXString &cxString) |
14 | { |
15 | std::string cstring = clang_getCString(cxString); |
16 | clang_disposeString(cxString); |
17 | return cstring.c_str() == nullptr ? "": cstring.c_str(); |
18 | } |
19 | |
20 | QString ClangCursor::kindName() const |
21 | { |
22 | CXCursorKind cursorKind = clang_getCursorKind(cursor); |
23 | CXString cxKindName = clang_getCursorKindSpelling(cursorKind); |
24 | return toCString(cxKindName); |
25 | } |
26 | |
27 | QString ClangCursor::spelling() const |
28 | { |
29 | CXString cxCursorSpelling = clang_getCursorSpelling(cursor); |
30 | return toCString(cxCursorSpelling); |
31 | } |
32 | |
33 | QString ClangCursor::displayName() const |
34 | { |
35 | CXString diaplay = clang_getCursorDisplayName(cursor); |
36 | return toCString(diaplay); |
37 | } |
38 | |
39 | void ClangCursor::location(QString &file, uint &line, uint &column) const |
40 | { |
41 | CXSourceLocation location = clang_getCursorLocation(cursor); |
42 | |
43 | CXString cxFilename; |
44 | clang_getPresumedLocation(location, &cxFilename, &line, &column); |
45 | file = toCString(cxFilename); |
46 | } |
47 | |
48 | QString ClangCursor::typeName() const |
49 | { |
50 | CXType type = clang_getCursorType(cursor); |
51 | CXString cxTypedefName = clang_getTypedefName(type); |
52 | return toCString(cxTypedefName); |
53 | } |
54 | |
55 | QString ClangCursor::typeSpelling() const |
56 | { |
57 | CXType type = clang_getCursorType(cursor); |
58 | CXString cxSpelling = clang_getTypeSpelling(type); |
59 | return toCString(cxSpelling); |
60 | } |
61 | |
62 | QString ClangCursor::typeKindName() const |
63 | { |
64 | CXType cursorType = clang_getCursorType(cursor); |
65 | auto kindName = toCString(clang_getTypeKindSpelling(cursorType.kind)); |
66 | return kindName; |
67 | } |
68 | |
69 | ClangCursor ClangCursor::sematicParent() const |
70 | { |
71 | return ClangCursor(clang_getCursorSemanticParent(cursor)); |
72 | } |
73 | |
74 | QString ClangCursor::translationUnitSpelling() const |
75 | { |
76 | CXTranslationUnit unit = clang_Cursor_getTranslationUnit(cursor); |
77 | CXString unitSpelling =clang_getTranslationUnitSpelling(unit); |
78 | return toCString(unitSpelling); |
79 | } |
80 | |
81 | bool ClangCursor::isValid() const |
82 | { |
83 | CXCursorKind kind = clang_getCursorKind(cursor); |
84 | return !clang_isInvalid(kind); |
85 | } |
86 |