Skip to content

Commit 16755cb

Browse files
committed
Create the IR instance in the main function
1 parent b865aca commit 16755cb

File tree

8 files changed

+22
-35
lines changed

8 files changed

+22
-35
lines changed

bindgen/Main.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,15 @@ int main(int argc, char *argv[]) {
4949

5050
locations.clear();
5151

52-
ScalaFrontendActionFactory actionFactory(libName);
53-
54-
int result = Tool.run(&actionFactory);
55-
56-
IR ir = actionFactory.getIntermediateRepresentation();
57-
52+
IR ir(libName);
5853
if (!Package.empty()) {
5954
ir.setPackageName(Package.getValue());
6055
}
6156

57+
ScalaFrontendActionFactory actionFactory(ir);
58+
59+
int result = Tool.run(&actionFactory);
60+
6261
auto printLoc = PrintHeadersLocation.getValue();
6362
if (printLoc) {
6463
for (const auto &location : locations) {

bindgen/visitor/ScalaFrontendAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "ScalaFrontendAction.h"
22

3-
ScalaFrontendAction::ScalaFrontendAction(IR *ir) : ir(ir) {}
3+
ScalaFrontendAction::ScalaFrontendAction(IR &ir) : ir(ir) {}
44

55
std::unique_ptr<clang::ASTConsumer>
66
ScalaFrontendAction::CreateASTConsumer(clang::CompilerInstance &CI,

bindgen/visitor/ScalaFrontendAction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
*/
1313
class ScalaFrontendAction : public clang::ASTFrontendAction {
1414
public:
15-
explicit ScalaFrontendAction(IR *ir);
15+
explicit ScalaFrontendAction(IR &ir);
1616

1717
std::unique_ptr<clang::ASTConsumer>
1818
CreateASTConsumer(clang::CompilerInstance &CI,
1919
clang::StringRef file) override;
2020

2121
private:
22-
IR *ir;
22+
IR &ir;
2323
};
2424

2525
#endif // SCALA_NATIVE_BINDGEN_SCALAFRONTENDACTION_H
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
#include "ScalaFrontendActionFactory.h"
22
#include "ScalaFrontendAction.h"
33

4-
ScalaFrontendActionFactory::ScalaFrontendActionFactory(std::string libName)
5-
: libName(libName), ir(libName) {}
4+
ScalaFrontendActionFactory::ScalaFrontendActionFactory(IR &ir) : ir(ir) {}
65

76
clang::FrontendAction *ScalaFrontendActionFactory::create() {
87
if (!ir.libObjEmpty() || ir.hasEnums()) {
98
llvm::errs() << "IR is not empty. Please use new instance of "
109
"ScalaFrontendActionFactory.\n";
1110
llvm::errs().flush();
1211
}
13-
return new ScalaFrontendAction(
14-
&ir); // instance will be deleted by LibTooling
15-
}
16-
17-
const IR &ScalaFrontendActionFactory::getIntermediateRepresentation() const {
18-
return ir;
12+
return new ScalaFrontendAction(ir);
1913
}

bindgen/visitor/ScalaFrontendActionFactory.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313
class ScalaFrontendActionFactory
1414
: public clang::tooling::FrontendActionFactory {
1515
public:
16-
explicit ScalaFrontendActionFactory(std::string libName);
16+
explicit ScalaFrontendActionFactory(IR &ir);
1717

1818
clang::FrontendAction *create() override;
1919

20-
const IR &getIntermediateRepresentation() const;
21-
2220
private:
23-
std::string libName;
24-
25-
IR ir;
21+
IR &ir;
2622
};
2723

2824
#endif // SCALA_NATIVE_BINDGEN_SCALAFRONTENDACTIONFACTORY_H

bindgen/visitor/TreeConsumer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TreeConsumer : public clang::ASTConsumer {
1818
clang::SourceManager &smanager;
1919

2020
public:
21-
TreeConsumer(clang::CompilerInstance *CI, IR *ir)
21+
TreeConsumer(clang::CompilerInstance *CI, IR &ir)
2222
: visitor(CI, ir), smanager(CI->getASTContext().getSourceManager()) {}
2323

2424
bool HandleTopLevelDecl(clang::DeclGroupRef DG) override {

bindgen/visitor/TreeVisitor.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
2626
parameters.emplace_back(pname, ptype);
2727
}
2828

29-
ir->addFunction(funcName, parameters, retType, func->isVariadic());
29+
ir.addFunction(funcName, parameters, retType, func->isVariadic());
3030

3131
return true;
3232
}
@@ -47,7 +47,7 @@ bool TreeVisitor::VisitTypedefDecl(clang::TypedefDecl *tpdef) {
4747

4848
std::string type = handleReservedWords(
4949
typeTranslator.Translate(tpdef->getUnderlyingType()));
50-
ir->addTypeDef(name, type);
50+
ir.addTypeDef(name, type);
5151
return true;
5252
}
5353

@@ -70,8 +70,8 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl) {
7070
enumerators.emplace_back(en->getNameAsString(), value);
7171
}
7272

73-
ir->addEnum(name, typeTranslator.Translate(enumdecl->getIntegerType()),
74-
enumerators);
73+
ir.addEnum(name, typeTranslator.Translate(enumdecl->getIntegerType()),
74+
enumerators);
7575

7676
return true;
7777
}
@@ -116,7 +116,7 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
116116
fields.emplace_back(fname, ftype);
117117
}
118118

119-
ir->addUnion(name, fields, maxSize);
119+
ir.addUnion(name, fields, maxSize);
120120
}
121121

122122
void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
@@ -155,6 +155,6 @@ void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
155155
llvm::errs().flush();
156156
}
157157

158-
ir->addStruct(name, fields,
159-
astContext->getTypeSize(record->getTypeForDecl()));
158+
ir.addStruct(name, fields,
159+
astContext->getTypeSize(record->getTypeForDecl()));
160160
}

bindgen/visitor/TreeVisitor.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
2222
clang::ASTContext *astContext;
2323
TypeTranslator typeTranslator;
2424
CycleDetection cycleDetection;
25-
26-
/* pointer to ir that is stored in ScalaFrontendActionFactory */
27-
IR *ir;
25+
IR &ir;
2826

2927
void handleUnion(clang::RecordDecl *record, std::string name);
3028

3129
void handleStruct(clang::RecordDecl *record, std::string name);
3230

3331
public:
34-
TreeVisitor(clang::CompilerInstance *CI, IR *ir)
32+
TreeVisitor(clang::CompilerInstance *CI, IR &ir)
3533
: astContext(&(CI->getASTContext())), typeTranslator(astContext),
3634
cycleDetection(typeTranslator), ir(ir) {}
3735

0 commit comments

Comments
 (0)