Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function definition by C include header list. Fixes #237 #627

Merged
merged 3 commits into from Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func Start(args ProgramArgs) (err error) {
fmt.Println("Running clang preprocessor...")
}

pp, comments, err := preprocessor.Analyze(args.inputFiles, args.clangFlags)
pp, comments, includes, err := preprocessor.Analyze(args.inputFiles, args.clangFlags)
if err != nil {
return err
}
Expand All @@ -218,7 +218,8 @@ func Start(args ProgramArgs) (err error) {
if args.verbose {
fmt.Println("Running clang for AST tree...")
}
astPP, err := exec.Command("clang", "-Xclang", "-ast-dump", "-fsyntax-only", "-fno-color-diagnostics", ppFilePath).Output()
astPP, err := exec.Command("clang", "-Xclang", "-ast-dump",
"-fsyntax-only", "-fno-color-diagnostics", ppFilePath).Output()
if err != nil {
// If clang fails it still prints out the AST, so we have to run it
// again to get the real error.
Expand All @@ -244,6 +245,7 @@ func Start(args ProgramArgs) (err error) {
p.Verbose = args.verbose
p.OutputAsTest = args.outputAsTest
p.Comments = comments
p.IncludeHeaders = includes

// Converting to nodes
if args.verbose {
Expand Down Expand Up @@ -285,7 +287,8 @@ func Start(args ProgramArgs) (err error) {
fmt.Println("Transpiling tree...")
}

err = transpiler.TranspileAST(args.outputFile, args.packageName, p, tree[0].(ast.Node))
err = transpiler.TranspileAST(args.outputFile, args.packageName,
p, tree[0].(ast.Node))
if err != nil {
return fmt.Errorf("cannot transpile AST : %v", err)
}
Expand Down
71 changes: 47 additions & 24 deletions preprocessor/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func (e *entity) isSame(x *entity) bool {
}

// Analyze - separation preprocessor code to part
func Analyze(inputFiles, clangFlags []string) (pp []byte, comments []program.Comment, err error) {
func Analyze(inputFiles, clangFlags []string) (pp []byte,
comments []program.Comment, includes []program.IncludeHeader, err error) {

var allItems []entity

allItems, err = analyzeFiles(inputFiles, clangFlags)
Expand All @@ -96,6 +98,14 @@ func Analyze(inputFiles, clangFlags []string) (pp []byte, comments []program.Com
if err != nil {
return
}
var all []string
all, err = GetIncludeFullList(inputFiles, clangFlags)
if err != nil {
return
}
// Generate C header list
includes = generateIncludeList(us, all)

for j := range us {
userSource[us[j]] = true
}
Expand Down Expand Up @@ -260,32 +270,31 @@ func getPreprocessSources(inputFiles, clangFlags []string) (out bytes.Buffer, er
return
}

func generateIncludeList(userList, allList []string) (
includes []program.IncludeHeader) {

for i := range allList {
var isUser bool
for j := range userList {
if allList[i] == userList[j] {
isUser = true
break
}
}
includes = append(includes, program.IncludeHeader{
HeaderName: allList[i],
IsUserSource: isUser,
})
}
return
}

// GetIncludeListWithUserSource - Get list of include files
// Example:
// $ clang -MM -c exit.c
// exit.o: exit.c tests.h
func GetIncludeListWithUserSource(inputFiles, clangFlags []string) (lines []string, err error) {
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...)
cmd := exec.Command("clang", args...)
cmd.Stdout = &out
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
err = fmt.Errorf("preprocess failed: %v\nStdErr = %v", err, stderr.String())
return
}
return parseIncludeList(out.String())
return getIncludeList(inputFiles, clangFlags, "-MM")
}

// GetIncludeFullList - Get full list of include files
Expand All @@ -297,10 +306,24 @@ func GetIncludeListWithUserSource(inputFiles, clangFlags []string) (lines []stri
// /usr/include/x86_64-linux-gnu/gnu/stubs.h \
// /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \
// / ........ and other
func GetIncludeFullList(inputFile string) (lines []string, err error) {
func GetIncludeFullList(inputFiles, clangFlags []string) (lines []string, err error) {
return getIncludeList(inputFiles, clangFlags, "-M")
}

func getIncludeList(inputFiles, clangFlags []string, flag string) (lines []string, err error) {
var out bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("clang", "-M", "-c", inputFile)
var args []string
for i := range inputFiles {
inputFiles[i], err = filepath.Abs(inputFiles[i])
if err != nil {
return
}
}
args = append(args, flag, "-c")
args = append(args, inputFiles...)
args = append(args, clangFlags...)
cmd := exec.Command("clang", args...)
cmd.Stdout = &out
cmd.Stderr = &stderr
err = cmd.Run()
Expand Down
Loading