Checks that exported symbols are valid C identifiers. Refs #800.
This commit is contained in:
parent
1a6d05515f
commit
adb390af4b
2 changed files with 24 additions and 4 deletions
|
|
@ -105,7 +105,7 @@ type
|
||||||
errThreadvarCannotInit, errWrongSymbolX, errIllegalCaptureX,
|
errThreadvarCannotInit, errWrongSymbolX, errIllegalCaptureX,
|
||||||
errXCannotBeClosure, errXMustBeCompileTime,
|
errXCannotBeClosure, errXMustBeCompileTime,
|
||||||
errCannotInferTypeOfTheLiteral,
|
errCannotInferTypeOfTheLiteral,
|
||||||
errUser,
|
errBadExport, errUser
|
||||||
warnCannotOpenFile,
|
warnCannotOpenFile,
|
||||||
warnOctalEscape, warnXIsNeverRead, warnXmightNotBeenInit,
|
warnOctalEscape, warnXIsNeverRead, warnXmightNotBeenInit,
|
||||||
warnDeprecated, warnConfigDeprecated,
|
warnDeprecated, warnConfigDeprecated,
|
||||||
|
|
@ -350,6 +350,7 @@ const
|
||||||
errXCannotBeClosure: "'$1' cannot have 'closure' calling convention",
|
errXCannotBeClosure: "'$1' cannot have 'closure' calling convention",
|
||||||
errXMustBeCompileTime: "'$1' can only be used in compile-time context",
|
errXMustBeCompileTime: "'$1' can only be used in compile-time context",
|
||||||
errCannotInferTypeOfTheLiteral: "cannot infer the type of the $1",
|
errCannotInferTypeOfTheLiteral: "cannot infer the type of the $1",
|
||||||
|
errBadExport: "invalid exported symbol, $1",
|
||||||
errUser: "$1",
|
errUser: "$1",
|
||||||
warnCannotOpenFile: "cannot open \'$1\' [CannotOpenFile]",
|
warnCannotOpenFile: "cannot open \'$1\' [CannotOpenFile]",
|
||||||
warnOctalEscape: "octal escape sequences do not exist; leading zero is ignored [OctalEscape]",
|
warnOctalEscape: "octal escape sequences do not exist; leading zero is ignored [OctalEscape]",
|
||||||
|
|
|
||||||
|
|
@ -97,8 +97,27 @@ proc makeExternImport(s: PSym, extname: string) =
|
||||||
incl(s.flags, sfImportc)
|
incl(s.flags, sfImportc)
|
||||||
excl(s.flags, sfForward)
|
excl(s.flags, sfForward)
|
||||||
|
|
||||||
proc makeExternExport(s: PSym, extname: string) =
|
const invalidIdentChars = AllChars - IdentChars
|
||||||
|
|
||||||
|
proc validateExternName(s: PSym, info: TLineInfo) =
|
||||||
|
## Validates that the symbol name in s.loc.r is a valid C identifier.
|
||||||
|
##
|
||||||
|
## Valid identifiers are those alphanumeric including the underscore not
|
||||||
|
## starting with a number. If the check fails, a friendly error will be
|
||||||
|
## displayed to the user.
|
||||||
|
let target = ropeToStr(s.loc.r)
|
||||||
|
if target.len < 1:
|
||||||
|
localError(info, errBadExport, "can't be zero length")
|
||||||
|
if not (target[0] in IdentStartChars):
|
||||||
|
localError(info, errBadExport, "'" & target & "' can't start with a number")
|
||||||
|
if not target.allCharsInSet(IdentChars):
|
||||||
|
let pos = target.find(invalidIdentChars, 1)
|
||||||
|
localError(info, errBadExport, "'" & target &
|
||||||
|
"' contains bad character at byte " & $pos)
|
||||||
|
|
||||||
|
proc makeExternExport(s: PSym, extname: string, info: TLineInfo) =
|
||||||
setExternName(s, extname)
|
setExternName(s, extname)
|
||||||
|
validateExternName(s, info)
|
||||||
incl(s.flags, sfExportc)
|
incl(s.flags, sfExportc)
|
||||||
|
|
||||||
proc processImportCompilerProc(s: PSym, extname: string) =
|
proc processImportCompilerProc(s: PSym, extname: string) =
|
||||||
|
|
@ -515,7 +534,7 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int,
|
||||||
if k in validPragmas:
|
if k in validPragmas:
|
||||||
case k
|
case k
|
||||||
of wExportc:
|
of wExportc:
|
||||||
makeExternExport(sym, getOptionalStr(c, it, "$1"))
|
makeExternExport(sym, getOptionalStr(c, it, "$1"), it.info)
|
||||||
incl(sym.flags, sfUsed) # avoid wrong hints
|
incl(sym.flags, sfUsed) # avoid wrong hints
|
||||||
of wImportc: makeExternImport(sym, getOptionalStr(c, it, "$1"))
|
of wImportc: makeExternImport(sym, getOptionalStr(c, it, "$1"))
|
||||||
of wImportCompilerProc:
|
of wImportCompilerProc:
|
||||||
|
|
@ -601,7 +620,7 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int,
|
||||||
processDynLib(c, it, sym)
|
processDynLib(c, it, sym)
|
||||||
of wCompilerproc:
|
of wCompilerproc:
|
||||||
noVal(it) # compilerproc may not get a string!
|
noVal(it) # compilerproc may not get a string!
|
||||||
makeExternExport(sym, "$1")
|
makeExternExport(sym, "$1", it.info)
|
||||||
incl(sym.flags, sfCompilerProc)
|
incl(sym.flags, sfCompilerProc)
|
||||||
incl(sym.flags, sfUsed) # suppress all those stupid warnings
|
incl(sym.flags, sfUsed) # suppress all those stupid warnings
|
||||||
registerCompilerProc(sym)
|
registerCompilerProc(sym)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue