Skip to content

Commit a1aa78a

Browse files
authored
Merge pull request #31 from jonas/ensure-positive-array-size
Clean up encoding of natural numbers used in array sizes
2 parents 3e5508d + 7334f2b commit a1aa78a

File tree

8 files changed

+27
-53
lines changed

8 files changed

+27
-53
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ find_package(Clang REQUIRED CONFIG)
66
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
77
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
88

9-
include_directories(${LLVM_INCLUDE_DIRS})
9+
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
1010
add_definitions(${LLVM_DEFINITIONS})
1111

12-
add_compile_options(-fexceptions -std=c++11)
12+
add_compile_options(-fexceptions -std=c++11 -Wall -Wconversion -Werror)
1313

1414
add_executable(bindgen
1515
Main.cpp

TypeTranslator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ std::string TypeTranslator::TranslateEnum(const clang::QualType& qtpe){
116116
}
117117

118118
std::string TypeTranslator::TranslateConstantArray(const clang::ConstantArrayType* ar, const std::string* avoid){
119-
const llvm::APInt& size = ar->getSize();
120-
return "native.CArray[" + Translate(ar->getElementType(), avoid) + ", " + intToScalaNat((int)size.roundToDouble()) + "]";
119+
const uint64_t size = ar->getSize().getZExtValue();
120+
const std::string nat = uint64ToScalaNat(size);
121+
return "native.CArray[" + Translate(ar->getElementType(), avoid) + ", " + nat + "]";
121122
}
122123

123124
std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::string* avoid){

Utils.h

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,17 @@ inline std::string basename(const std::string& pathname) {
1515
pathname.end()};
1616
}
1717

18-
inline std::string intToScalaNat(int v, std::string accumulator = ""){
19-
20-
if(v > 0){
21-
int last_digit = v % 10;
22-
int rest = v / 10;
23-
24-
if(accumulator == ""){
25-
return intToScalaNat(rest, "_" + std::to_string(last_digit));
26-
} else{
27-
return intToScalaNat(rest, "Digit[_" + std::to_string(last_digit) + ", " + accumulator + "]");
28-
}
29-
} else{
18+
inline std::string uint64ToScalaNat(uint64_t v, std::string accumulator = "") {
19+
if (v == 0)
3020
return accumulator;
31-
}
32-
}
3321

34-
inline std::string uint64ToScalaNat(uint64_t v, std::string accumulator = ""){
22+
auto last_digit = v % 10;
23+
auto rest = v / 10;
3524

36-
if(v > 0){
37-
int last_digit = v % 10;
38-
int rest = v / 10;
39-
40-
if(accumulator == ""){
41-
return uint64ToScalaNat(rest, "_" + std::to_string(last_digit));
42-
} else{
43-
return uint64ToScalaNat(rest, "Digit[_" + std::to_string(last_digit) + ", " + accumulator + "]");
44-
}
45-
} else{
46-
return accumulator;
25+
if (accumulator.empty()) {
26+
return uint64ToScalaNat(rest, "_" + std::to_string(last_digit));
27+
} else {
28+
return uint64ToScalaNat(rest, "Digit[_" + std::to_string(last_digit) + ", " + accumulator + "]");
4729
}
4830
}
4931

@@ -62,11 +44,11 @@ inline bool typeEquals(const clang::Type* tpe1, const std::string* tpe2){
6244
return false;
6345
}
6446

65-
static std::array<std::string, 39> reserved_words = {"abstract", "case", "catch", "class", "def", "do", "else", "extends",
47+
static std::array<std::string, 39> reserved_words = {{"abstract", "case", "catch", "class", "def", "do", "else", "extends",
6648
"false", "final", "finally", "for", "forSome", "if", "implicit", "import",
6749
"lazy", "match", "new", "null", "object", "override", "package", "private",
6850
"protected", "return", "sealed", "super", "this", "throw", "trait", "try",
69-
"true", "type", "val", "var", "while", "with", "yield"};
51+
"true", "type", "val", "var", "while", "with", "yield"}};
7052

7153
inline std::string handleReservedWords(std::string name, std::string suffix = "") {
7254
auto found = std::find(reserved_words.begin(), reserved_words.end(), name);

ir/Function.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ Function::Function(std::string name, std::vector<Parameter> parameters,
1313
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
1414
s << " def " << handleReservedWords(func.name)
1515
<< "(";
16-
auto paramsCount = static_cast<int>(func.parameters.size());
17-
for (int i = 0; i < paramsCount; ++i) {
18-
const Parameter &param = func.parameters[i];
19-
s << handleReservedWords(param.getName())
16+
std::string sep = "";
17+
for (const auto &param : func.parameters) {
18+
s << sep
19+
<< handleReservedWords(param.getName())
2020
<< ": "
2121
<< param.getType();
22-
if (i < paramsCount - 1) {
23-
s << ", ";
24-
}
22+
sep = ", ";
2523
}
2624
if (func.isVariadic) {
2725
/* the C Iso require at least one argument in a variadic function, so the comma is fine */

ir/Struct.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ TypeDef Struct::generateTypeDef() const {
2424

2525
std::string Struct::getFieldsTypes() const {
2626
std::stringstream s;
27-
auto fieldsCount = static_cast<int>(fields.size());
28-
for (int i = 0; i < fieldsCount; i++) {
29-
s << fields[i].getType();
30-
if (i < fieldsCount - 1) {
31-
s << ", ";
32-
}
27+
std::string sep = "";
28+
for (const auto &field : fields) {
29+
s << sep << field.getType();
30+
sep = ", ";
3331
}
3432
return s.str();
3533
}

scripts/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ if [[ ! -e target/.llvm-version ]] || [[ "$(<target/.llvm-version)" != "${LLVM_V
1010
mkdir -p target
1111
echo "${LLVM_VERSION:-}" > target/.llvm-version
1212
(cd target && cmake ..)
13-
make -C target
1413
fi
1514

15+
make -C target
16+
1617
cd tests
1718
sbt "${@:-test}"

visitor/TreeConsumer.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ class TreeConsumer : public clang::ASTConsumer {
1717

1818
clang::SourceManager &smanager;
1919

20-
/* pointer to ir that is stored in ScalaFrontendActionFactory */
21-
IR *ir;
22-
2320
public:
2421

2522
explicit TreeConsumer(clang::CompilerInstance *CI, IR *ir)
2623
: visitor(CI, ir),
27-
smanager(CI->getASTContext().getSourceManager()),
28-
ir(ir) {}
24+
smanager(CI->getASTContext().getSourceManager()) {}
2925

3026
bool HandleTopLevelDecl(clang::DeclGroupRef DG) override {
3127
// a DeclGroupRef may have multiple Decls, so we iterate through each one

visitor/TreeVisitor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
109109
fields.push_back(Field(fname, ftype));
110110
}
111111

112-
auto usize = intToScalaNat(static_cast<int>(maxSize));
113-
114112
ir->addUnion(name, fields, maxSize);
115113
}
116114

0 commit comments

Comments
 (0)