Skip to content

Commit 91a5344

Browse files
author
liuguangzhao
committed
test prepared ast ok
1 parent 31297db commit 91a5344

File tree

8 files changed

+102
-1
lines changed

8 files changed

+102
-1
lines changed

data/.keep

Whitespace-only changes.

entry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ VALUE x_Qt_meta_class_init_jit(int argc, VALUE *argv, VALUE self)
370370

371371
// test_fe();
372372
// test_parse_class();
373+
// test_parse_ast();
373374
// exit(-1);
374375

375376
auto code_templater = [] () -> QString * {

frontengine.cpp

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ bool FrontEngine::initCompiler()
5353
(char*)"myjitqtrunner", (char*)"flycode.cxx",
5454
(char*)"-fPIC", (char*)"-x", (char*)"c++",
5555
(char*)"-I/usr/include/qt", (char*)"-I/usr/include/qt/QtCore",
56+
(char*)"-I/usr/include/qt/QtGui", (char*)"-I/usr/include/qt/QtWidgets",
57+
(char*)"-I/usr/include/qt/QtNetwork",
5658
(char*)"-I/usr/lib/clang/3.5.0/include",
5759
};
58-
static int argc = 8;
60+
static int argc = 11;
5961
char **targv = argv;
6062

6163
llvm::SmallVector<const char*, 16> drv_args(targv, targv + argc);
@@ -107,10 +109,74 @@ bool FrontEngine::parseClass(QString klass)
107109
return true;
108110
}
109111

112+
// from ast file
113+
bool FrontEngine::parseHeader()
114+
{
115+
std::string astfile = "qthdrsrc.ast";
116+
clang::IntrusiveRefCntPtr<clang::DiagnosticsEngine> mydiag;
117+
clang::FileSystemOptions fsopts;
118+
119+
QDateTime btime = QDateTime::currentDateTime();
120+
clang::ASTUnit *unit = clang::ASTUnit::LoadFromASTFile(astfile, mydiag, fsopts);
121+
// clang::ASTUnit::LoadFromASTFile(const std::string &Filename,
122+
// IntrusiveRefCntPtr<clang::DiagnosticsEngine> Diags,
123+
// const clang::FileSystemOptions &FileSystemOpts,
124+
// bool OnlyLocalDecls,
125+
// ArrayRef<RemappedFile> RemappedFiles);
126+
QDateTime etime = QDateTime::currentDateTime();
127+
clang::ASTContext &tctx = unit->getASTContext();
128+
clang::SourceManager &srcman = unit->getSourceManager();
129+
clang::FileManager &fman = unit->getFileManager();
130+
clang::TranslationUnitDecl *trud = tctx.getTranslationUnitDecl();
131+
// bool bret = unit->Reparse();
132+
// unit->Save("abc.ast");
133+
tctx.PrintStats();
134+
srcman.PrintStats();
135+
fman.PrintStats();
136+
137+
unit->getStartOfMainFileID();
138+
139+
// unit->loadModule(unit->getStartOfMainFileID());
140+
141+
int fic = 0;
142+
for (auto it = srcman.fileinfo_begin(); it != srcman.fileinfo_end(); it++) {
143+
fic ++;
144+
const clang::FileEntry *fe = it->first;
145+
qDebug()<<it->first<<it->second<<fe->getName()<<fe->getSize();
146+
}
147+
int dic = 0;
148+
for (auto it = trud->decls_begin(); it != trud->decls_end(); it++) {
149+
dic++;
150+
clang::Decl *d = *it;
151+
if (dic % 500 == 1) {
152+
qDebug()<<d->getDeclKindName();
153+
}
154+
}
155+
qDebug()<<"fic:"<<fic<<trud<<trud->decls_empty()<<"decls count:"<<dic;
156+
157+
// 遍历一次用时120ms,加载5,6ms,很快了。3M内存???
158+
qDebug()<<"time "<<etime.msecsTo(btime)
159+
<<tctx.getASTAllocatedMemory()
160+
<<tctx.getSideTableAllocatedMemory();
161+
162+
qDebug()<<unit<<unit->isMainFileAST()<<unit->getOriginalSourceFileName().data()
163+
<<unit->getASTFileName().data()
164+
<<unit->getMainFileName().data()
165+
<<unit->top_level_size()
166+
<<unit->top_level_empty()
167+
<<unit->isModuleFile()
168+
<<unit->getPCHFile()
169+
;
170+
171+
172+
return true;
173+
}
174+
110175
bool FrontEngine::parseHeader(QString path)
111176
{
112177
qDebug()<<path;
113178

179+
path = "./qthdrsrc.h";
114180
QFile fp(path);
115181
fp.open(QIODevice::ReadOnly);
116182
QByteArray ba = fp.readAll();
@@ -129,6 +195,8 @@ bool FrontEngine::parseHeader(QString path)
129195
clang::ASTContext &tctx = pastu->getASTContext();
130196
QDateTime etime = QDateTime::currentDateTime();
131197

198+
pastu->Save("abc.ast");
199+
132200
// about 3.5M for qstring.h, 310ms
133201
// about 9.3M for qwidget.h, 770ms
134202
// about 8.8M for qapplication.h, 700ms

frontengine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class FrontEngine
4141
bool init();
4242
bool initCompiler();
4343

44+
bool parseHeader();
4445
bool parseHeader(QString path);
4546
bool parseClass(QString klass);
4647
bool get_method_default_args(QString klass, QString method, QString symbol_name

qthdrsrc.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef QTHDRSRC_H
2+
#define QTHDRSRC_H
3+
4+
5+
6+
7+
#include <QtCore>
8+
#include <QtGui>
9+
#include <QtWidgets>
10+
#include <QtNetwork>
11+
12+
#endif /* QTHDRSRC_H */
13+
14+

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ JIT的另一个选择?libjit from gnu.(other google c++ jit compiler)
7676

7777

7878
现在的基本框架已经试验完成,还需要解决的实用问题,
79+
更多的功能测试。
7980
线程安全。
8081
jit虚拟机的优化。
8182
生成的IR代码的优化。

tests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// usage: 在其他位置直接插入#include代码。
33
// #include "tests.cpp"
44

5+
#include <QtCore>
6+
57
#include "tests.h"
68

79
#include <stdio.h>
@@ -64,4 +66,13 @@ void test_parse_class()
6466
fe->parseClass("QTcpServer");
6567
}
6668

69+
void test_parse_ast()
70+
{
71+
FrontEngine *fe = new FrontEngine();
72+
QDateTime btime = QDateTime::currentDateTime();
73+
fe->parseHeader();
74+
QDateTime etime = QDateTime::currentDateTime();
75+
qDebug()<<"use time:"<<btime.msecsTo(etime);
76+
}
77+
6778
// test_fe();

tools/gen_qt_meta_class.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,8 @@ for module in $MODULES ; do
163163

164164
done # end for module
165165

166+
### generator ast file
167+
ast_file="./data/qthdrsrc.ast"
168+
clang++ -x c++ -S -emit-ast "./qthdrsrc.h" -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore \
169+
-I/usr/include/qt/QtGui -I/usr/include/qt/QtWidgets -I/usr/include/qt/QtNetwork
170+
mv -v "qthdrsrc.ast" "${ast_file}"

0 commit comments

Comments
 (0)