Skip to content

Commit

Permalink
Fixed bug with preprocessor including the same file multiple times. F…
Browse files Browse the repository at this point in the history
…ixes 363 (#369)
  • Loading branch information
Konstantin8105 authored and elliotchance committed Nov 28, 2017
1 parent 9aa8212 commit dca511c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func TestMultifileTranspilation(t *testing.T) {
"./tests/multi/main1.c",
"./tests/multi/main2.c",
},
"234",
"234ERROR!ERROR!ERROR!",
},
}

Expand Down
35 changes: 32 additions & 3 deletions preprocessor/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ type entity struct {
lines []*string
}

// isSame - check is Same entities
func (e *entity) isSame(x *entity) bool {
if e.include != x.include {
return false
}
if e.positionInSource != x.positionInSource {
return false
}
if e.other != x.other {
return false
}
if len(e.lines) != len(x.lines) {
return false
}
for k := range e.lines {
is := e.lines[k]
js := x.lines[k]
if len(*is) != len(*js) {
return false
}
if *is != *js {
return false
}
}
return true
}

// Analyze - separation preprocessor code to part
func Analyze(inputFiles, clangFlags []string) (pp []byte, err error) {
var allItems []entity
Expand All @@ -32,12 +59,14 @@ func Analyze(inputFiles, clangFlags []string) (pp []byte, err error) {
// Merge the entities
var lines []string
for i := range allItems {
// If found same part of preprocess code, then
// don't include in result buffer for transpiling
// for avoid dublicate of code
var found bool
for j := 0; j < i; j++ {
if allItems[i].include == allItems[j].include &&
allItems[i].positionInSource == allItems[j].positionInSource &&
allItems[i].other == allItems[j].other {
if allItems[i].isSame(&allItems[j]) {
found = true
break
}
}
if found {
Expand Down
5 changes: 5 additions & 0 deletions tests/multi/err.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

void ERROR_FUNC(){
printf("ERROR!");
}
9 changes: 9 additions & 0 deletions tests/multi/main1.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#include <stdio.h>
#include "header.h"

#define ERROR_FUNC error
#include "err.h"
#undef ERROR_FUNC
#define ERROR_FUNC errorANOTHER
#include "err.h"

int main() {
say_two();
say_three();
say_four();

ERROR_FUNC();
error();
errorANOTHER();
return 0;
}

Expand Down

0 comments on commit dca511c

Please sign in to comment.