diff --git a/main_test.go b/main_test.go index 04af6a3ae..697772da1 100644 --- a/main_test.go +++ b/main_test.go @@ -315,7 +315,7 @@ func TestMultifileTranspilation(t *testing.T) { "./tests/multi/main1.c", "./tests/multi/main2.c", }, - "234", + "234ERROR!ERROR!ERROR!", }, } diff --git a/preprocessor/preprocessor.go b/preprocessor/preprocessor.go index a67447274..1a70f2136 100644 --- a/preprocessor/preprocessor.go +++ b/preprocessor/preprocessor.go @@ -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 @@ -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 { diff --git a/tests/multi/err.h b/tests/multi/err.h new file mode 100644 index 000000000..b038636e5 --- /dev/null +++ b/tests/multi/err.h @@ -0,0 +1,5 @@ +#include + +void ERROR_FUNC(){ + printf("ERROR!"); +} diff --git a/tests/multi/main1.c b/tests/multi/main1.c index f6ed548c3..b7b41ecaf 100644 --- a/tests/multi/main1.c +++ b/tests/multi/main1.c @@ -1,11 +1,20 @@ #include #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; }