diff --git a/.travis.yml b/.travis.yml index 2fa3d91af..3d1b9080f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,7 +40,11 @@ before_install: # gocovmerge is used to merge all the separate unit/integration test coverage # profiles. - - go get -u github.com/wadey/gocovmerge + - go get -u -v github.com/wadey/gocovmerge + + # install gometalinter + - go get -u github.com/alecthomas/gometalinter + - gometalinter --install script: - . ./travis/$SCRIPT.sh diff --git a/main_test.go b/main_test.go index 7115a1deb..22ec2fe68 100644 --- a/main_test.go +++ b/main_test.go @@ -158,6 +158,14 @@ func TestIntegrationScripts(t *testing.T) { r := util.GetRegex("warning: no packages being tested depend on .+\n") goProgramStderr = r.ReplaceAllString(goProgramStderr, "") + // It is need only for "tests/assert.c" + // for change absolute path to local path + currentDir, err := os.Getwd() + if err != nil { + t.Fatal("Cannot get currently dir") + } + goProgramStderr = strings.Replace(goProgramStderr, currentDir+"/", "", -1) + if cProgramStderr != goProgramStderr { t.Fatalf("Expected %s\nGot: %s", cProgramStderr, goProgramStderr) } diff --git a/preprocessor/preprocessor.go b/preprocessor/preprocessor.go index 39fe6572b..adf3ee8e9 100644 --- a/preprocessor/preprocessor.go +++ b/preprocessor/preprocessor.go @@ -4,7 +4,11 @@ import ( "bufio" "bytes" "fmt" + "io/ioutil" + "os" "os/exec" + "path/filepath" + "runtime" "strings" "text/scanner" @@ -150,7 +154,8 @@ func Analyze(inputFiles, clangFlags []string) (pp []byte, comments []program.Com func analyzeFiles(inputFiles, clangFlags []string) (items []entity, err error) { // See : https://clang.llvm.org/docs/CommandGuide/clang.html // clang -E Run the preprocessor stage. - out, err := getPreprocessSources(inputFiles, clangFlags) + var out bytes.Buffer + out, err = getPreprocessSources(inputFiles, clangFlags) if err != nil { return } @@ -196,31 +201,62 @@ func analyzeFiles(inputFiles, clangFlags []string) (items []entity, err error) { // See : https://clang.llvm.org/docs/CommandGuide/clang.html // clang -E Run the preprocessor stage. func getPreprocessSources(inputFiles, clangFlags []string) (out bytes.Buffer, err error) { - var stderr bytes.Buffer - for _, inputFile := range inputFiles { - if inputFile[len(inputFile)-1] != 'c' { - continue - } + // get temp dir + dir, err := ioutil.TempDir("", "c2go-union") + if err != nil { + return + } + defer func() { _ = os.RemoveAll(dir) }() - var args []string - args = append(args, "-E", "-C") - args = append(args, clangFlags...) - args = append(args, inputFile) + // file name union file + var unionFileName = dir + "/" + "unionFileName.c" - var outFile bytes.Buffer - cmd := exec.Command("clang", args...) - cmd.Stdout = &outFile - cmd.Stderr = &stderr - err = cmd.Run() - if err != nil { - err = fmt.Errorf("preprocess for file: %s\nfailed: %v\nStdErr = %v", inputFile, err, stderr.String()) - return - } - _, err = out.Write(outFile.Bytes()) + // create a body for union file + var unionBody string + for i := range inputFiles { + var absPath string + absPath, err = filepath.Abs(inputFiles[i]) if err != nil { return } + unionBody += fmt.Sprintf("#include \"%s\"\n", absPath) + } + + // write a union file + err = ioutil.WriteFile(unionFileName, []byte(unionBody), 0644) + if err != nil { + return + } + + // Add open source defines + if runtime.GOOS == "darwin" { + clangFlags = append(clangFlags, "-D_XOPEN_SOURCE") + } else { + clangFlags = append(clangFlags, "-D_GNU_SOURCE") + } + + // preprocessor clang + var stderr bytes.Buffer + + var args []string + args = append(args, "-E", "-C") + args = append(args, clangFlags...) + args = append(args, unionFileName) // All inputFiles + + var outFile bytes.Buffer + cmd := exec.Command("clang", args...) + cmd.Stdout = &outFile + cmd.Stderr = &stderr + err = cmd.Run() + if err != nil { + err = fmt.Errorf("preprocess for file: %v\nfailed: %v\nStdErr = %v", inputFiles, err, stderr.String()) + return } + _, err = out.Write(outFile.Bytes()) + if err != nil { + return + } + return } @@ -232,6 +268,12 @@ func GetIncludeListWithUserSource(inputFiles, clangFlags []string) (lines []stri var out bytes.Buffer var stderr bytes.Buffer var args []string + for i := range inputFiles { + inputFiles[i], err = filepath.Abs(inputFiles[i]) + if err != nil { + return + } + } args = append(args, "-MM", "-c") args = append(args, inputFiles...) args = append(args, clangFlags...) diff --git a/travis/test.sh b/travis/test.sh index f3bc70b1e..664ff0aac 100755 --- a/travis/test.sh +++ b/travis/test.sh @@ -108,3 +108,14 @@ if [ "$TRAVIS_OS_NAME" == "osx" ]; then curl -H "Authorization: token ${GITHUB_API_TOKEN}" -H "Content-Type: application/json" https://api.github.com/repos/elliotchance/c2go/statuses/${TRAVIS_COMMIT} -d "{\"state\": \"success\",\"target_url\": \"https://travis-ci.org/elliotchance/c2go/builds/${TRAVIS_JOB_ID}\", \"description\": \"$(($SQLITE_WARNINGS)) warnings\", \"context\": \"c2go/sqlite3\"}" fi +# SQLITE +c2go transpile -o="$SQLITE_TEMP_FOLDER/sqlite.go" -clang-flag="-DSQLITE_THREADSAFE=0" -clang-flag="-DSQLITE_OMIT_LOAD_EXTENSION" $SQLITE_TEMP_FOLDER/$SQLITE3_FILE/shell.c $SQLITE_TEMP_FOLDER/$SQLITE3_FILE/sqlite3.c + +# Show amount "Warning": +SQLITE_WARNINGS=`cat $SQLITE_TEMP_FOLDER/sqlite.go | grep "// Warning" | wc -l` +echo "After transpiling shell.c and sqlite3.c together, have summary: $SQLITE_WARNINGS warnings." + +# Amount warning from gometalinter +echo "Calculation warnings by gometalinter" +GOMETALINTER_WARNINGS=`$GOPATH/bin/gometalinter $SQLITE_TEMP_FOLDER/sqlite.go 2>&1 | wc -l` +echo "Amount found warnings by gometalinter at 30 second : $GOMETALINTER_WARNINGS warnings."