Skip to content

Commit

Permalink
Multiple C files in one go. Fixes #362 (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin8105 authored and elliotchance committed Feb 9, 2018
1 parent dc58f69 commit cc1e2bf
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 21 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
82 changes: 62 additions & 20 deletions preprocessor/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"text/scanner"

Expand Down Expand Up @@ -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 <file> Run the preprocessor stage.
out, err := getPreprocessSources(inputFiles, clangFlags)
var out bytes.Buffer
out, err = getPreprocessSources(inputFiles, clangFlags)
if err != nil {
return
}
Expand Down Expand Up @@ -196,31 +201,62 @@ func analyzeFiles(inputFiles, clangFlags []string) (items []entity, err error) {
// See : https://clang.llvm.org/docs/CommandGuide/clang.html
// clang -E <file> 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
}

Expand All @@ -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...)
Expand Down
11 changes: 11 additions & 0 deletions travis/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."

0 comments on commit cc1e2bf

Please sign in to comment.