Skip to content

Commit 81857dd

Browse files
authored
Merge pull request #23 from kornilova-l/fix-new-lines
Use '\n' and llvm::raw_ostream
2 parents 87dc6cd + b3cbb71 commit 81857dd

File tree

13 files changed

+69
-65
lines changed

13 files changed

+69
-65
lines changed

Main.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#include "visitor/ScalaFrontendAction.h"
2-
#include <sstream>
3-
#include "Utils.h"
41
#include "visitor/ScalaFrontendActionFactory.h"
52
#include <clang/Tooling/CommonOptionsParser.h>
63

@@ -20,6 +17,7 @@ int main(int argc, char *argv[]) {
2017
auto libName = LibName.getValue();
2118
if(libName.empty()){
2219
llvm::errs() << "Error: Please specify the lib name using -name parameter\n";
20+
llvm::errs().flush();
2321
return -1;
2422
}
2523

@@ -37,16 +35,14 @@ int main(int argc, char *argv[]) {
3735
IR ir = actionFactory.getIntermediateRepresentation();
3836

3937
auto printLoc = PrintHeadersLocation.getValue();
40-
41-
std::ostringstream s;
42-
43-
if(printLoc){
44-
for(const auto& location: locations) {
45-
s << location.c_str();
38+
if (printLoc) {
39+
for (const auto &location: locations) {
40+
llvm::outs() << location.c_str();
4641
}
4742
} else {
48-
s << ir.generate();
43+
ir.generate();
44+
llvm::outs() << ir;
4945
}
50-
std::cout << s.str();
46+
llvm::outs().flush();
5147
return result;
5248
}

TypeTranslator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ std::string TypeTranslator::TranslateFunctionPointer(const clang::QualType& qtpe
5858

5959
} else {
6060
llvm::errs() << "Unsupported function pointer type: " << qtpe.getAsString() << "\n";
61+
llvm::errs().flush();
6162
exit(-1);
6263
}
6364
}
@@ -125,12 +126,15 @@ std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::st
125126

126127
if(qtpe.isConstQualified() || (ctx && qtpe.isConstant(*ctx))){
127128
llvm::errs() << "Warning: Const qualifier not supported\n";
129+
llvm::errs().flush();
128130
}
129131
if(qtpe.isVolatileQualified()){
130132
llvm::errs() << "Warning: Volatile qualifier not supported\n";
133+
llvm::errs().flush();
131134
}
132135
if(qtpe.isRestrictQualified()){
133136
llvm::errs() << "Warning: Restrict qualifier not supported\n";
137+
llvm::errs().flush();
134138
}
135139

136140
const clang::Type* tpe = qtpe.getTypePtr();

ir/Enum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TypeDef Enum::generateTypeDef() const {
2121
return TypeDef("enum_" + name, "native.CInt");
2222
}
2323

24-
std::ostream &operator<<(std::ostream &s, const Enum &e) {
24+
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
2525
int i = 0;
2626
for (auto enumerator : e.enumerators) {
2727
std::string enumeratorName;
@@ -30,7 +30,7 @@ std::ostream &operator<<(std::ostream &s, const Enum &e) {
3030
} else {
3131
enumeratorName = "enum_" + enumerator.getName();
3232
}
33-
s << " final val " << enumeratorName << " = " << std::to_string(i++) << std::endl;
33+
s << " final val " << enumeratorName << " = " << std::to_string(i++) << "\n";
3434
}
3535
return s;
3636
}

ir/Enum.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66
#include <vector>
77
#include "TypeDef.h"
8+
#include <llvm/Support/raw_ostream.h>
89

910
class Enumerator {
1011
public:
@@ -26,7 +27,7 @@ class Enum {
2627

2728
TypeDef generateTypeDef() const;
2829

29-
friend std::ostream &operator<<(std::ostream &s, const Enum &e);
30+
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e);
3031

3132
private:
3233
std::string name; // might be empty

ir/Function.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Function::Function(std::string name, std::vector<Parameter> parameters,
1010
: name(std::move(name)), parameters(std::move(parameters)),
1111
retType(std::move(retType)), isVariadic(isVariadic) {}
1212

13-
std::ostream &operator<<(std::ostream &s, const Function &func) {
13+
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
1414
s << " def " << handleReservedWords(func.name)
1515
<< "(";
1616
auto paramsCount = static_cast<int>(func.parameters.size());

ir/Function.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66
#include <vector>
77
#include "TypeAndName.h"
8+
#include <llvm/Support/raw_ostream.h>
89

910
class Parameter : public TypeAndName {
1011
public:
@@ -16,7 +17,7 @@ class Function {
1617
Function(std::string name, std::vector<Parameter> parameters,
1718
std::string retType, bool isVariadic);
1819

19-
friend std::ostream &operator<<(std::ostream &s, const Function &func);
20+
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func);
2021

2122
private:
2223
std::string name;

ir/IR.cpp

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <sstream>
21
#include "IR.h"
32
#include "../Utils.h"
43

@@ -35,65 +34,65 @@ bool IR::libObjEmpty() const {
3534
structs.empty() && unions.empty();
3635
}
3736

38-
std::string IR::generate() {
39-
generateDecl();
40-
std::stringstream s;
41-
if (!libObjEmpty() || !enums.empty()) {
42-
s << "import scala.scalanative._" << std::endl
43-
<< "import scala.scalanative.native._" << std::endl
44-
<< "import scala.scalanative.native.Nat._" << std::endl << std::endl;
37+
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
38+
assert(ir.generated); // typedefs were generated
39+
40+
if (!ir.libObjEmpty() || !ir.enums.empty()) {
41+
s << "import scala.scalanative._\n"
42+
<< "import scala.scalanative.native._\n"
43+
<< "import scala.scalanative.native.Nat._\n\n";
4544
}
4645

47-
std::string libObjName = handleReservedWords(libName);
46+
std::string libObjName = handleReservedWords(ir.libName);
4847

49-
if (!libObjEmpty()) {
50-
s << "@native.link(\"" << libName << "\")" << std::endl
51-
<< "@native.extern" << std::endl
52-
<< "object " << libObjName << " {" << std::endl;
48+
if (!ir.libObjEmpty()) {
49+
s << "@native.link(\"" << ir.libName << "\")\n"
50+
<< "@native.extern\n"
51+
<< "object " << libObjName << " {\n";
5352

54-
for (const auto &typeDef : typeDefs) {
53+
for (const auto &typeDef : ir.typeDefs) {
5554
s << typeDef;
5655
}
5756

58-
for (const auto &func : functions) {
57+
for (const auto &func : ir.functions) {
5958
s << func;
6059
}
6160

62-
s << "}" << std::endl << std::endl;
61+
s << "}\n\n";
6362
}
6463

65-
if (!enums.empty() || hasHelperMethods()) {
66-
s << "import " << libObjName << "._" << std::endl << std::endl;
64+
if (!ir.enums.empty() || ir.hasHelperMethods()) {
65+
s << "import " << libObjName << "._\n\n";
6766
}
6867

69-
if (!enums.empty()) {
70-
s << "object " << libName << "Enums {" << std::endl;
68+
if (!ir.enums.empty()) {
69+
s << "object " << ir.libName << "Enums {\n";
7170

72-
for (const auto &e : enums) {
71+
for (const auto &e : ir.enums) {
7372
s << e
74-
<< std::endl; // space between groups of enums
73+
<< "\n"; // space between groups of enums
7574
}
7675

77-
s << "}" << std::endl << std::endl;
76+
s << "}\n\n";
7877
}
7978

80-
if (hasHelperMethods()) {
81-
s << "object " << libName << "Helpers {" << std::endl;
79+
if (ir.hasHelperMethods()) {
80+
s << "object " << ir.libName << "Helpers {\n";
8281

83-
for (const auto &st : structs) {
84-
s << std::endl
82+
for (const auto &st : ir.structs) {
83+
s << "\n"
8584
<< st.generateHelperClass();
8685
}
8786

88-
for (const auto &u : unions) {
89-
s << std::endl
87+
for (const auto &u : ir.unions) {
88+
s << "\n"
9089
<< u.generateHelperClass();
9190
}
9291

93-
s << "}" << std::endl << std::endl;
92+
s << "}\n\n";
9493
}
9594

96-
return s.str();
95+
return s;
9796
}
9897

9998
void IR::generateTypeDefs() {
@@ -110,7 +109,7 @@ void IR::generateTypeDefs() {
110109
}
111110
}
112111

113-
void IR::generateDecl() {
112+
void IR::generate() {
114113
if (!generated) {
115114
generateTypeDefs();
116115
generated = true;
@@ -132,5 +131,5 @@ bool IR::hasHelperMethods() const {
132131
}
133132

134133
bool IR::hasEnums() const {
135-
return enums.size() != 0;
134+
return !enums.empty();
136135
}

ir/IR.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef SCALA_NATIVE_BINDGEN_INTERMEDIATEREPRESENTATION_H
22
#define SCALA_NATIVE_BINDGEN_INTERMEDIATEREPRESENTATION_H
33

4-
#include <iostream>
54
#include "Function.h"
65
#include "Struct.h"
76
#include "TypeDef.h"
@@ -34,7 +33,9 @@ class IR {
3433

3534
bool hasEnums() const;
3635

37-
std::string generate();
36+
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir);
37+
38+
void generate();
3839

3940
private:
4041

@@ -43,8 +44,6 @@ class IR {
4344
*/
4445
void generateTypeDefs();
4546

46-
void generateDecl();
47-
4847
/**
4948
* @return true if helper methods will be generated for this library
5049
*/

ir/Struct.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ std::string Struct::generateHelperClass() const {
4242
std::stringstream s;
4343
std::string newName = "struct_" + name;
4444
s << " implicit class " << newName << "_ops(val p: native.Ptr[struct_" << name << "])"
45-
<< " extends AnyVal {" << std::endl;
45+
<< " extends AnyVal {\n";
4646
int fieldIndex = 0;
4747
for (const auto &field : fields) {
4848
if (!field.getName().empty()) {
4949
std::string fname = handleReservedWords(field.getName());
5050
std::string ftype = field.getType();
51-
s << " def " << fname << ": " << ftype << " = !p._" << std::to_string(fieldIndex + 1) << std::endl
51+
s << " def " << fname << ": " << ftype << " = !p._" << std::to_string(fieldIndex + 1) << "\n"
5252
<< " def " << fname << "_=(value: " + ftype + "):Unit = !p._" << std::to_string(fieldIndex + 1)
53-
<< " = value" << std::endl;
53+
<< " = value\n";
5454
}
5555
fieldIndex++;
5656
}
57-
s << " }" << std::endl << std::endl;
57+
s << " }\n\n";
5858

5959
/* makes struct instantiation easier */
6060
s << " def " << newName + "()(implicit z: native.Zone): native.Ptr[" + newName + "]"
61-
<< " = native.alloc[" + newName + "]" << std::endl;
61+
<< " = native.alloc[" + newName + "]\n";
6262

6363
return s.str();
6464
}
@@ -78,18 +78,18 @@ TypeDef Union::generateTypeDef() const {
7878
std::string Union::generateHelperClass() const {
7979
std::stringstream s;
8080
s << " implicit class union_" << name << "_pos"
81-
<< "(val p: native.Ptr[union_" << name << "]) extends AnyVal {" << std::endl;
81+
<< "(val p: native.Ptr[union_" << name << "]) extends AnyVal {\n";
8282
for (const auto &field : fields) {
8383
if (!field.getName().empty()) {
8484
std::string fname = handleReservedWords(field.getName());
8585
std::string ftype = field.getType();
8686
s << " def " << fname
87-
<< ": native.Ptr[" << ftype << "] = p.cast[native.Ptr[" << ftype << "]]" << std::endl;
87+
<< ": native.Ptr[" << ftype << "] = p.cast[native.Ptr[" << ftype << "]]\n";
8888

8989
s << " def " << fname
90-
<< "_=(value: " << ftype << "): Unit = !p.cast[native.Ptr[" << ftype << "]] = value" << std::endl;
90+
<< "_=(value: " << ftype << "): Unit = !p.cast[native.Ptr[" << ftype << "]] = value\n";
9191
}
9292
}
93-
s << " }" << std::endl;
93+
s << " }\n";
9494
return s.str();
9595
}

ir/TypeDef.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
TypeDef::TypeDef(std::string name, std::string type)
55
: TypeAndName(std::move(name), std::move(type)) {}
66

7-
std::ostream &operator<<(std::ostream &s, const TypeDef &typeDef) {
7+
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const TypeDef &typeDef) {
88
s << " type " + handleReservedWords(typeDef.name) + " = " + typeDef.getType() + "\n";
99
return s;
1010
}

0 commit comments

Comments
 (0)