From 4f0cdf4d1ab760c7d1a8452ebf8045029ef2ddb7 Mon Sep 17 00:00:00 2001 From: Greg Date: Sat, 1 Jun 2019 15:10:51 -0400 Subject: [PATCH] Some AST tweaks and add astfile: config option to read AST from a file instead of running Clang. Attempt to port to MacOS Mojave. --- ast/alloc_size_attr.go | 10 +++--- ast/implicit_cast_expr.go | 3 +- ast/unary_operator.go | 3 +- ast/var_decl.go | 4 +++ main.go | 66 ++++++++++++++++++++++++--------------- 5 files changed, 54 insertions(+), 32 deletions(-) diff --git a/ast/alloc_size_attr.go b/ast/alloc_size_attr.go index 1d6adf3..0439eeb 100644 --- a/ast/alloc_size_attr.go +++ b/ast/alloc_size_attr.go @@ -3,7 +3,7 @@ package ast import ( "strings" - "git.wow.st/gmp/nswrap/util" + //"git.wow.st/gmp/nswrap/util" ) // AllocSizeAttr is a type of attribute that is optionally attached to a variable @@ -12,8 +12,8 @@ type AllocSizeAttr struct { Addr Address Pos Position Inherited bool - A int - B int + A string + B string ChildNodes []Node } @@ -27,8 +27,8 @@ func parseAllocSizeAttr(line string) *AllocSizeAttr { Addr: ParseAddress(groups["address"]), Pos: NewPositionFromString(groups["position"]), Inherited: len(groups["inherited"]) > 0, - A: util.Atoi(strings.TrimSpace(groups["a"])), - B: util.Atoi(strings.TrimSpace(groups["b"])), + A: strings.TrimSpace(groups["a"]), + B: strings.TrimSpace(groups["b"]), ChildNodes: []Node{}, } } diff --git a/ast/implicit_cast_expr.go b/ast/implicit_cast_expr.go index a744d34..f6d6fc1 100644 --- a/ast/implicit_cast_expr.go +++ b/ast/implicit_cast_expr.go @@ -18,7 +18,8 @@ func parseImplicitCastExpr(line string) *ImplicitCastExpr { `<(?P.*)> '(?P.*?)' (:'(?P.*?)')? - <(?P.*)>`, + <(?P.*)> + ( part_of_explicit_cast)?`, line, ) diff --git a/ast/unary_operator.go b/ast/unary_operator.go index d2de9da..6308f27 100644 --- a/ast/unary_operator.go +++ b/ast/unary_operator.go @@ -19,7 +19,8 @@ func parseUnaryOperator(line string) *UnaryOperator { (?P lvalue)? (?P prefix)? (?P postfix)? - '(?P.*?)'`, + '(?P.*?)' + ( .*)?`, line, ) diff --git a/ast/var_decl.go b/ast/var_decl.go index 29ec13b..fa6011a 100644 --- a/ast/var_decl.go +++ b/ast/var_decl.go @@ -15,6 +15,7 @@ type VarDecl struct { Type2 string IsExtern bool IsUsed bool + IsRange bool IsCInit bool IsReferenced bool IsStatic bool @@ -28,6 +29,7 @@ func parseVarDecl(line string) *VarDecl { (?:parent (?P0x[0-9a-f]+) )? <(?P.*)>(?P .+:\d+)? (?P used)? + (?P range)? (?P referenced)? (?P \w+)? '(?P.+?)' @@ -36,6 +38,7 @@ func parseVarDecl(line string) *VarDecl { (?P static)? (?P cinit)? (?P register)? + (.*) `, line, ) @@ -55,6 +58,7 @@ func parseVarDecl(line string) *VarDecl { Type2: type2, IsExtern: len(groups["extern"]) > 0, IsUsed: len(groups["used"]) > 0, + IsRange: len(groups["range"]) > 0, IsCInit: len(groups["cinit"]) > 0, IsReferenced: len(groups["referenced"]) > 0, IsStatic: len(groups["static"]) > 0, diff --git a/main.go b/main.go index e682100..1bc78ca 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ type conf struct { Positions bool Package string Inputfiles []string + Astfile string Classes []string Functions []string Enums []string @@ -157,35 +158,48 @@ func matches(x string, rs []string) bool { // Start begins transpiling an input file. func Start() (err error) { - for _, in := range Config.Inputfiles { - _, err := os.Stat(in) + astPP := []byte{} + if Config.Astfile != "" { + fmt.Printf("Reading ast file %s\n",Config.Astfile) + _, err = os.Stat(Config.Astfile) if err != nil { - return fmt.Errorf("Input file %s is not found", in) + return fmt.Errorf("Input AST file %s not found",Config.Astfile) + } + astPP, err = ioutil.ReadFile(Config.Astfile) + if err != nil { + return err + } + } else { + for _, in := range Config.Inputfiles { + _, err = os.Stat(in) + if err != nil { + return fmt.Errorf("Input file %s is not found", in) + } } - } - // Generate AST - cargs := []string{"-xobjective-c", "-Xclang", "-ast-dump", - "-fsyntax-only","-fno-color-diagnostics"} - if Config.Arc { - cargs = append(cargs,"-fobjc-arc") - } - cargs = append(cargs,Config.Inputfiles...) - fmt.Printf("Generating AST\n") - astPP, err := exec.Command("clang",cargs...).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. - //errBody, _ := exec.Command("clang", cargs...).CombinedOutput() - var txt string - switch x := err.(type) { - case *exec.ExitError: - txt = string(x.Stderr) - default: - txt = err.Error() + // Generate AST + cargs := []string{"-xobjective-c", "-Xclang", "-ast-dump", + "-fsyntax-only","-fno-color-diagnostics"} + if Config.Arc { + cargs = append(cargs,"-fobjc-arc") + } + cargs = append(cargs,Config.Inputfiles...) + fmt.Printf("Generating AST\n") + astPP, err = exec.Command("clang",cargs...).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. + //errBody, _ := exec.Command("clang", cargs...).CombinedOutput() + var txt string + switch x := err.(type) { + case *exec.ExitError: + txt = string(x.Stderr) + default: + txt = err.Error() + } + fmt.Printf("clang failed:\n%s\n", txt) + os.Exit(-1) } - fmt.Printf("clang failed:\n%s\n", txt) - os.Exit(-1) } lines := readAST(astPP) @@ -271,6 +285,8 @@ func main() { fmt.Printf("Cannot decode config file nswrap.yaml. %s\n",err) os.Exit(-1) } + fmt.Printf("Package = %s\n",Config.Package) + fmt.Printf("Astfile = %s\n",Config.Astfile) if err := Start(); err != nil { fmt.Printf("Error: %v\n", err) os.Exit(-1)