Handle tserror in static inline, wrap headers only once

This commit is contained in:
Ganesh Viswanathan 2020-07-02 20:11:11 -05:00
commit c3f4f3e487
5 changed files with 37 additions and 17 deletions

View file

@ -55,6 +55,7 @@ type
identifiers*: TableRef[string, string] # Symbols that have been declared so far indexed by nimName identifiers*: TableRef[string, string] # Symbols that have been declared so far indexed by nimName
skippedSyms*: HashSet[string] # Symbols that have been skipped due to being unwrappable or skippedSyms*: HashSet[string] # Symbols that have been skipped due to being unwrappable or
# the user provided override is blank # the user provided override is blank
headersProcessed*: HashSet[string] # Headers already processed directly or recursively
# Nim compiler objects # Nim compiler objects
constSection*, enumSection*, pragmaSection*, procSection*, typeSection*, varSection*: PNode constSection*, enumSection*, pragmaSection*, procSection*, typeSection*, varSection*: PNode

View file

@ -136,7 +136,11 @@ proc main(
gecho preMainOut gecho preMainOut
gState.initNim() gState.initNim()
for src in source: for src in source:
gState.process(src.expandSymlinkAbs()) let
src = src.expandSymlinkAbs()
if src notin gState.headersProcessed:
gState.process(src)
gState.headersProcessed.incl src
if gState.pnim: if gState.pnim:
printNim(gState) printNim(gState)

View file

@ -137,7 +137,7 @@ proc newConstDef(gState: State, node: TSNode, fname = "", fval = ""): PNode =
# In case symbol was skipped earlier # In case symbol was skipped earlier
gState.skippedSyms.excl origname gState.skippedSyms.excl origname
else: else:
gecho &"# const '{origname}' is duplicate, skipped" decho &"const '{origname}' is duplicate, skipped"
else: else:
gecho &"# const '{origname}' has unsupported value '{val.strip()}'" gecho &"# const '{origname}' has unsupported value '{val.strip()}'"
gState.skippedSyms.incl origname gState.skippedSyms.incl origname
@ -391,7 +391,7 @@ proc newXIdent(gState: State, node: TSNode, kind = nskType, fname = "", pragmas:
gState.identifierNodes[name] = result gState.identifierNodes[name] = result
else: else:
gecho &"# {getKeyword(kind)} '{origname}' is duplicate, skipped" decho &"{getKeyword(kind)} '{origname}' is duplicate, skipped"
proc newArrayTree(gState: State, node: TSNode, typ, size: PNode = nil): PNode = proc newArrayTree(gState: State, node: TSNode, typ, size: PNode = nil): PNode =
# Create nkBracketExpr tree depending on input # Create nkBracketExpr tree depending on input
@ -1706,6 +1706,9 @@ proc addDef(gState: State, node: TSNode) =
fdecl = node[start+1].firstChildInTree("function_declarator") fdecl = node[start+1].firstChildInTree("function_declarator")
if not fdecl.isNil: if not fdecl.isNil:
if gState.getNodeError(fdecl):
return
if not gState.noHeader: if not gState.noHeader:
gState.addProc(node[start+1], node[start], commentNodes) gState.addProc(node[start+1], node[start], commentNodes)
else: else:
@ -1716,19 +1719,14 @@ proc processNode(gState: State, node: TSNode): Status =
const const
known = ["preproc_def", "type_definition", known = ["preproc_def", "type_definition",
"struct_specifier", "union_specifier", "enum_specifier", "struct_specifier", "union_specifier", "enum_specifier",
"declaration", "function_definition"].toHashSet() "declaration"].toHashSet()
result = success result = success
let let
name = node.getName() name = node.getName()
if name in known: if name in known:
# Recognized top-level nodes # Recognized top-level nodes
let if gState.getNodeError(node):
err = node.anyChildInTree("ERROR")
if not err.isNil:
# Bail on errors
gState.printDebug(node)
gecho &"# tree-sitter parse error: '{gState.getNodeVal(node).splitLines()[0]}', skipped"
result = Status.error result = Status.error
else: else:
# Process nodes # Process nodes
@ -1750,8 +1748,11 @@ proc processNode(gState: State, node: TSNode): Status =
gState.addEnum(node) gState.addEnum(node)
of "declaration": of "declaration":
gState.addDecl(node) gState.addDecl(node)
of "function_definition": elif name == "function_definition":
gState.addDef(node) # Separate since we only need to check function_declarator for errors and
# not the compound_statement which could have errors but does not impact
# wrapper generation
gState.addDef(node)
else: else:
# Unknown, will check child nodes # Unknown, will check child nodes
result = unknown result = unknown

View file

@ -315,6 +315,7 @@ proc getPreprocessor*(gState: State, fullpath: string) =
p = startProcess(getCompiler(), args = args, options = {poStdErrToStdOut, poUsePath}) p = startProcess(getCompiler(), args = args, options = {poStdErrToStdOut, poUsePath})
outp = p.outputStream() outp = p.outputStream()
line = "" line = ""
newHeaders: HashSet[string]
# Include content only from file # Include content only from file
gState.code = "" gState.code = ""
@ -323,17 +324,19 @@ proc getPreprocessor*(gState: State, fullpath: string) =
# We want to keep blank lines here for comment processing # We want to keep blank lines here for comment processing
if line.len > 1 and line[0] == '#' and line[1] == ' ': if line.len > 1 and line[0] == '#' and line[1] == ' ':
start = false start = false
line = line.sanitizePath(noQuote = true) line = line.split('"')[1].sanitizePath(noQuote = true)
if sfile in line or if sfile == line or
(DirSep notin line and sfileName in line): (DirSep notin line and sfileName == line):
start = true start = true
elif gState.recurse: elif gState.recurse:
if pDir.Bl or pDir in line: if (pDir.Bl or pDir in line) and line notin gState.headersProcessed:
start = true start = true
newHeaders.incl line
else: else:
for inc in includeDirs: for inc in includeDirs:
if inc in line: if line.startsWith(inc) and line notin gState.headersProcessed:
start = true start = true
newHeaders.incl line
break break
else: else:
if start: if start:
@ -342,6 +345,7 @@ proc getPreprocessor*(gState: State, fullpath: string) =
gState.code.add line & "\n" gState.code.add line & "\n"
elif not p.running(): break elif not p.running(): break
p.close() p.close()
gState.headersProcessed.incl newHeaders
# Plugin related # Plugin related
@ -393,3 +397,4 @@ proc expandSymlinkAbs*(path: string): string =
result = path.expandFilename().normalizedPath() result = path.expandFilename().normalizedPath()
except: except:
result = path result = path
result = result.sanitizePath(noQuote = true)

View file

@ -399,3 +399,12 @@ proc getTSNodeNamedChildNames*(node: TSNode): seq[string] =
if name != "comment": if name != "comment":
result.add(name) result.add(name)
proc getNodeError*(gState: State, node: TSNode): bool =
let
err = node.anyChildInTree("ERROR")
if not err.isNil:
# Bail on errors
gState.printDebug(node)
gecho &"# tree-sitter parse error: '{gState.getNodeVal(node).splitLines()[0]}', skipped"
result = true