Skip to content

Commit fdb7dda

Browse files
committed
Initial integration of Clang AST viewer GUI tool.
1 parent 1ea19d5 commit fdb7dda

23 files changed

+2374
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "build/modules/premake-qt"]
2+
path = build/modules/premake-qt
3+
url = https://github.com/dcourtois/premake-qt.git

build/modules/premake-qt

Submodule premake-qt added at 0ddc497

build/premake5.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ workspace "CppSharp"
2828
group "Libraries"
2929
include (srcdir .. "/Core")
3030
include (srcdir .. "/AST")
31+
--include (srcdir .. "/ASTViewer")
3132
include (srcdir .. "/CppParser")
3233
include (srcdir .. "/CppParser/Bindings")
3334
include (srcdir .. "/CppParser/Bootstrap")

src/ASTViewer/AstModel.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include "AstModel.h"
2+
#include <qbrush.h>
3+
4+
5+
AstModel::AstModel(GenericAstNode *data, QObject *parent):
6+
QAbstractItemModel(parent),
7+
rootItem(data)
8+
{
9+
}
10+
11+
AstModel::~AstModel()
12+
{
13+
}
14+
15+
int AstModel::columnCount(const QModelIndex &parent) const
16+
{
17+
return 1;
18+
}
19+
20+
QVariant AstModel::data(const QModelIndex &index, int role) const
21+
{
22+
if (!index.isValid())
23+
return QVariant();
24+
25+
if (role != Qt::DisplayRole && role != Qt::ForegroundRole && role != Qt::NodeRole)
26+
return QVariant();
27+
28+
auto item = static_cast<GenericAstNode*>(index.internalPointer());
29+
switch (role)
30+
{
31+
case Qt::DisplayRole:
32+
return QVariant(QString::fromStdString(item->name));
33+
case Qt::ForegroundRole:
34+
switch (item->getColor())
35+
{
36+
case 0:
37+
return QVariant(QBrush(Qt::GlobalColor::darkBlue));
38+
case 1:
39+
return QVariant(QBrush(Qt::GlobalColor::darkGreen));
40+
default:
41+
return QVariant(QBrush(Qt::GlobalColor::black));
42+
}
43+
case Qt::NodeRole:
44+
return QVariant::fromValue(item);
45+
}
46+
return QVariant(QString::fromStdString(item->name));
47+
}
48+
49+
Qt::ItemFlags AstModel::flags(const QModelIndex &index) const
50+
{
51+
if (!index.isValid())
52+
return 0;
53+
54+
return QAbstractItemModel::flags(index);
55+
}
56+
57+
QVariant AstModel::headerData(int section, Qt::Orientation orientation, int role) const
58+
{
59+
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
60+
return QVariant("Test");
61+
62+
return QVariant();
63+
}
64+
65+
QModelIndex AstModel::index(int row, int column, const QModelIndex &parent) const
66+
{
67+
if (!hasIndex(row, column, parent))
68+
return QModelIndex();
69+
70+
71+
if (!parent.isValid())
72+
{
73+
return rootIndex();
74+
}
75+
76+
auto parentItem = static_cast<GenericAstNode*>(parent.internalPointer());
77+
auto &childItem = parentItem->myChidren[row];
78+
if (childItem)
79+
return createIndex(row, column, childItem.get());
80+
else
81+
return QModelIndex();
82+
}
83+
84+
QModelIndex AstModel::rootIndex() const
85+
{
86+
return createIndex(0, 0, rootItem);
87+
}
88+
89+
QModelIndex AstModel::parent(const QModelIndex &index) const
90+
{
91+
if (!index.isValid())
92+
return QModelIndex();
93+
94+
GenericAstNode *childItem = static_cast<GenericAstNode*>(index.internalPointer());
95+
if (childItem == rootItem || childItem->myParent == nullptr)
96+
return QModelIndex();
97+
98+
GenericAstNode *parentItem = childItem->myParent;
99+
100+
if (parentItem == rootItem)
101+
return rootIndex();
102+
auto grandFather = parentItem->myParent;
103+
auto parentRow = grandFather == nullptr ?
104+
0 :
105+
grandFather->findChildIndex(parentItem);
106+
107+
return createIndex(parentRow, 0, parentItem);
108+
}
109+
110+
int AstModel::rowCount(const QModelIndex &parent) const
111+
{
112+
GenericAstNode *parentItem;
113+
if (parent.column() > 0)
114+
return 0;
115+
116+
if (parent.isValid())
117+
{
118+
parentItem = static_cast<GenericAstNode*>(parent.internalPointer());
119+
return parentItem->myChidren.size();
120+
}
121+
else
122+
{
123+
return 1;
124+
}
125+
}
126+
127+
bool AstModel::hasChildren(const QModelIndex &parent) const
128+
{
129+
GenericAstNode *parentItem;
130+
if (parent.column() > 0)
131+
return false;
132+
133+
if (parent.isValid())
134+
parentItem = static_cast<GenericAstNode*>(parent.internalPointer());
135+
else
136+
parentItem = rootItem;
137+
138+
return !parentItem->myChidren.empty();
139+
140+
}
141+

src/ASTViewer/AstModel.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <qabstractitemmodel.h>
4+
#include "AstReader.h"
5+
6+
namespace Qt
7+
{
8+
int const NodeRole = UserRole + 1;
9+
}
10+
11+
Q_DECLARE_METATYPE(GenericAstNode*)
12+
13+
class AstModel : public QAbstractItemModel
14+
{
15+
Q_OBJECT
16+
17+
public:
18+
explicit AstModel(GenericAstNode *data, QObject *parent = 0);
19+
~AstModel();
20+
21+
QVariant data(const QModelIndex &index, int role) const override;
22+
Qt::ItemFlags flags(const QModelIndex &index) const override;
23+
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
24+
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
25+
QModelIndex parent(const QModelIndex &index) const override;
26+
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
27+
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
28+
QModelIndex rootIndex() const;
29+
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
30+
31+
private:
32+
void setupModelData(const QStringList &lines, GenericAstNode *parent);
33+
34+
GenericAstNode *rootItem;
35+
};

0 commit comments

Comments
 (0)