-
Notifications
You must be signed in to change notification settings - Fork 25
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
Better sanitizeName
#87
Comments
I tested my idea replacing changes to futhark.nim import anycase
...
proc sanitizeName(usedNames: var HashSet[string], origName: string, kind: string, renameCallback: RenameCallback, partof = ""): string {.compileTime.} =
result = origName
if not renameCallback.isNil:
result = result.renameCallback(kind, partof)
if result.startsWith("_"):
if result.startsWith("__"):
result = "compiler_" & result[2..^1]
else:
result = "internal_" & result[1..^1]
# result = result.nimIdentNormalize()
result = case kind:
of "enum", "struct", "union", "const", "typedef": result.pascal # AnyCase
of "proc", "arg", "enumval", "field": result.camel # anyCase
else: result.nimIdentNormalize() # [A|a]nycase
var renamed = false
if usedNames.contains(result) or result in builtins:
result.add kind
renamed = true
if usedNames.contains(result) or result in builtins:
result.add hash(origName).toHex
renamed = true
if renamed:
hint "Renaming \"" & origName & "\" to \"" & result & "\"" & (if partof.len != 0: " in " & partof else: "")
usedNames.incl result Results for GDAL library. The dirty part is still under user control via import std/[sugar, os, strutils]
import futhark
func renameCb(n, k: string, p = ""): string =
result = n
for prefix in ["GDAL_", "OGR_"]:
if result.startsWith prefix:
result = result.replace(prefix, "")
break
for prefix in ["GDAL", "OGR", "OCT", "OSR", "CPL", "VSI"]:
if result.startsWith prefix:
result = result.replace(prefix, prefix.toLower)
break
result = result.replace("_", "")
importc:
outputPath currentSourcePath.parentDir / "gdal_c.nim"
renameCallback renameCb
"gdal/gdal.h"
"gdal/ogr_api.h"
"gdal/ogr_srs_api.h"
"gdal/cpl_conv.h"
"gdal/cpl_vsi.h"
proc main =
var major, minor, patch: cint
assert ogrGetGEOSVersion(major.addr, minor.addr, patch.addr)
echo "GEOS version: ", major, ".", minor, ".", patch
osrGetPROJVersion(major.addr, minor.addr, patch.addr)
echo "PROJ version: ", major, ".", minor, ".", patch
gdalAllRegister()
let spatialReference = osrNewSpatialReference(nil)
assert spatialReference.osrImportFromEPSG(4326) == 0
var geometry: OgrGeometryH
var wkt = "POINT(1 2)".cstring
assert gCreateFromWkt(wkt.addr, spatialReference, geometry.addr) == 0
dump geometry.gGetGeometryType()
dump geometry.gGetGeometryName()
let json = geometry.gExportToJson()
dump json
vsiFree(json)
gDestroyGeometry(geometry)
osrDestroySpatialReference(spatialReference)
main() generated gdal_c.zip and rename hints
output
|
This code has a slight issue, it won't properly handle name collisions. For anyone who didn't follow the IRC discussion when this came up the reason why As a motivating example I wrote a test case in the #define my_var 1
#define myVar 2
#define myvar 3
#define MYVAR 4
#define MY_VAR 5
#define MyVar 6
#define My_Var 7 The current version wraps this fine and renames fields to avoid collisions as we can see in the output:
The names get appended some information before falling back to a hash of the original identifier. With your version of
As we can see it doesn't properly rename all the identifiers, and some of them are aliased together in the wrapper making |
Futhark no longer performs the |
I tried running this generation script that worked withut errors using futhark < 0.13.2 import std/[os]
import futhark
importc:
outputPath currentSourcePath.parentDir / "gdal_c.nim"
"gdal/gdal.h"
"gdal/ogr_api.h"
"gdal/ogr_srs_api.h" with 0.13.2 I'm getting /home/arkanoid/.nimble/pkgs2/futhark-0.13.2-33b5d9892d6ef514a5cc7e92585d319a15c23ab3/futhark.nim(694, 14) template/generic instantiation of `importcImpl` from here
(1, 21) Error: undeclared identifier: 'GDALRPCInfoV2_536873295'
EDIT: a quick git diff shows that the newly generated |
Yes I've seen something similar while wrapping Gtk. It generates the file properly, but for some reason the macro output is invalid. So if you run it twice so it grabs the cached file then everything works fine |
At the moment, Futhark applies
renameCallback
before other fixed renaming rules, including nimIdentNormalize pass, which nullify all the NEP1 transformations user suppliedrenameCallback
apply.I'd say
nimIdentNormalize
is not really NEP1 friendlyI've recently pushed PR to
anycase
package to support compile time string transformations https://github.com/epszaw/anycase/issues/6What about replacing
nimIdentNormalize
insanitizeName
with something that is morerenameCallback
and NEP1 friendly? Or maybe provide an additionalrenameTailCallback
to fix things after the fixed transformations?The text was updated successfully, but these errors were encountered: