Skip to content

Commit 8c7ee96

Browse files
a-mrtimotheecournarimiran
authored
rst: add missing line/column info for some warnings (#18383)
* rst: add missing line/column info for some warnings * add workaround * use TLineInfo/FileIndex for storing file names * fix blank lines in include file (rm harmful strip) * don't use ref TLineInfo * return `hasToc` as output parameter for uniformity * Update compiler/docgen.nim Co-authored-by: Timothee Cour <[email protected]> * Update compiler/docgen.nim Co-authored-by: Timothee Cour <[email protected]> * Update lib/packages/docutils/rst.nim Co-authored-by: Timothee Cour <[email protected]> * address review - stylistic things * Update compiler/docgen.nim Co-authored-by: Timothee Cour <[email protected]> * unify RST warnings/errors names * doAssert + minor name change * fix a bug caught by doAssert * apply strbasics.strip to final HTML/Latex * rm redundant filename * fix test after rebase * delete `order` from rnFootnoteRef, also display errors/warnings properly when footnote references are from different files * Update compiler/lineinfos.nim Co-authored-by: Timothee Cour <[email protected]> * Update lib/packages/docutils/rstast.nim Co-authored-by: Timothee Cour <[email protected]> * Update lib/packages/docutils/rstast.nim Co-authored-by: Timothee Cour <[email protected]> * Update lib/packages/docutils/rstast.nim Co-authored-by: Timothee Cour <[email protected]> * revert because of error: Error: cannot prove that it's safe to initialize 'info' with the runtime value for the discriminator 'kind' * Update lib/packages/docutils/rstgen.nim Co-authored-by: Timothee Cour <[email protected]> * apply suggestion * Update lib/packages/docutils/rst.nim Co-authored-by: Timothee Cour <[email protected]> * add Table for string->file name mapping * do not import compiler/lineinfos * fix ambiguous calls Co-authored-by: Timothee Cour <[email protected]> Co-authored-by: narimiran <[email protected]>
1 parent 44c5afe commit 8c7ee96

File tree

8 files changed

+344
-195
lines changed

8 files changed

+344
-195
lines changed

compiler/docgen.nim

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
import
1414
ast, strutils, strtabs, options, msgs, os, idents,
15-
wordrecg, syntaxes, renderer, lexer, packages/docutils/rstast,
15+
wordrecg, syntaxes, renderer, lexer,
1616
packages/docutils/rst, packages/docutils/rstgen,
1717
json, xmltree, trees, types,
1818
typesrenderer, astalgo, lineinfos, intsets,
1919
pathutils, tables, nimpaths, renderverbatim, osproc
20+
import packages/docutils/rstast except FileIndex, TLineInfo
2021

2122
from uri import encodeUrl
2223
from std/private/globs import nativeToUnixPath
@@ -159,17 +160,18 @@ template declareClosures =
159160
case msgKind
160161
of meCannotOpenFile: k = errCannotOpenFile
161162
of meExpected: k = errXExpected
162-
of meGridTableNotImplemented: k = errGridTableNotImplemented
163-
of meMarkdownIllformedTable: k = errMarkdownIllformedTable
164-
of meNewSectionExpected: k = errNewSectionExpected
165-
of meGeneralParseError: k = errGeneralParseError
166-
of meInvalidDirective: k = errInvalidDirectiveX
167-
of meInvalidRstField: k = errInvalidRstField
168-
of meFootnoteMismatch: k = errFootnoteMismatch
169-
of mwRedefinitionOfLabel: k = warnRedefinitionOfLabel
170-
of mwUnknownSubstitution: k = warnUnknownSubstitutionX
171-
of mwUnsupportedLanguage: k = warnLanguageXNotSupported
172-
of mwUnsupportedField: k = warnFieldXNotSupported
163+
of meGridTableNotImplemented: k = errRstGridTableNotImplemented
164+
of meMarkdownIllformedTable: k = errRstMarkdownIllformedTable
165+
of meNewSectionExpected: k = errRstNewSectionExpected
166+
of meGeneralParseError: k = errRstGeneralParseError
167+
of meInvalidDirective: k = errRstInvalidDirectiveX
168+
of meInvalidField: k = errRstInvalidField
169+
of meFootnoteMismatch: k = errRstFootnoteMismatch
170+
of mwRedefinitionOfLabel: k = warnRstRedefinitionOfLabel
171+
of mwUnknownSubstitution: k = warnRstUnknownSubstitutionX
172+
of mwBrokenLink: k = warnRstBrokenLink
173+
of mwUnsupportedLanguage: k = warnRstLanguageXNotSupported
174+
of mwUnsupportedField: k = warnRstFieldXNotSupported
173175
of mwRstStyle: k = warnRstStyle
174176
{.gcsafe.}:
175177
globalError(conf, newLineInfo(conf, AbsoluteFile filename, line, col), k, arg)
@@ -182,11 +184,9 @@ template declareClosures =
182184

183185
proc parseRst(text, filename: string,
184186
line, column: int,
185-
rstOptions: RstParseOptions;
186187
conf: ConfigRef, sharedState: PRstSharedState): PRstNode =
187188
declareClosures()
188-
result = rstParsePass1(text, filename, line, column, rstOptions,
189-
sharedState)
189+
result = rstParsePass1(text, line, column, sharedState)
190190

191191
proc getOutFile2(conf: ConfigRef; filename: RelativeFile,
192192
ext: string, guessTarget: bool): AbsoluteFile =
@@ -202,20 +202,22 @@ proc getOutFile2(conf: ConfigRef; filename: RelativeFile,
202202
proc isLatexCmd(conf: ConfigRef): bool = conf.cmd in {cmdRst2tex, cmdDoc2tex}
203203

204204
proc newDocumentor*(filename: AbsoluteFile; cache: IdentCache; conf: ConfigRef,
205-
outExt: string = HtmlExt, module: PSym = nil): PDoc =
205+
outExt: string = HtmlExt, module: PSym = nil,
206+
isPureRst = false): PDoc =
206207
declareClosures()
207208
new(result)
208209
result.module = module
209210
result.conf = conf
210211
result.cache = cache
211212
result.outDir = conf.outDir.string
212-
const options = {roSupportRawDirective, roSupportMarkdown,
213-
roPreferMarkdown, roNimFile}
213+
result.isPureRst = isPureRst
214+
var options= {roSupportRawDirective, roSupportMarkdown, roPreferMarkdown}
215+
if not isPureRst: options.incl roNimFile
214216
result.sharedState = newRstSharedState(
215217
options, filename.string,
216218
docgenFindFile, compilerMsgHandler)
217219
initRstGenerator(result[], (if conf.isLatexCmd: outLatex else: outHtml),
218-
conf.configVars, filename.string, options,
220+
conf.configVars, filename.string,
219221
docgenFindFile, compilerMsgHandler)
220222

221223
if conf.configVars.hasKey("doc.googleAnalytics"):
@@ -299,8 +301,7 @@ proc genComment(d: PDoc, n: PNode): PRstNode =
299301
result = parseRst(n.comment, toFullPath(d.conf, n.info),
300302
toLinenumber(n.info),
301303
toColumn(n.info) + DocColOffset,
302-
d.options, d.conf,
303-
d.sharedState)
304+
d.conf, d.sharedState)
304305

305306
proc genRecCommentAux(d: PDoc, n: PNode): PRstNode =
306307
if n == nil: return nil
@@ -1123,6 +1124,9 @@ proc generateDoc*(d: PDoc, n, orig: PNode, docFlags: DocFlags = kDefault) =
11231124

11241125
proc finishGenerateDoc*(d: var PDoc) =
11251126
## Perform 2nd RST pass for resolution of links/footnotes/headings...
1127+
# copy file map `filenames` to ``rstgen.nim`` for its warnings
1128+
d.filenames = d.sharedState.filenames
1129+
11261130
# Main title/subtitle are allowed only in the first RST fragment of document
11271131
var firstRst = PRstNode(nil)
11281132
for fragment in d.modDescPre:
@@ -1417,14 +1421,10 @@ proc commandDoc*(cache: IdentCache, conf: ConfigRef) =
14171421
proc commandRstAux(cache: IdentCache, conf: ConfigRef;
14181422
filename: AbsoluteFile, outExt: string) =
14191423
var filen = addFileExt(filename, "txt")
1420-
var d = newDocumentor(filen, cache, conf, outExt)
1421-
1422-
d.isPureRst = true
1424+
var d = newDocumentor(filen, cache, conf, outExt, isPureRst = true)
14231425
let rst = parseRst(readFile(filen.string), filen.string,
14241426
line=LineRstInit, column=ColRstInit,
1425-
{roSupportRawDirective, roSupportMarkdown,
1426-
roPreferMarkdown}, conf,
1427-
d.sharedState)
1427+
conf, d.sharedState)
14281428
d.modDescPre = @[ItemFragment(isRst: true, rst: rst)]
14291429
finishGenerateDoc(d)
14301430
writeOutput(d)

compiler/lineinfos.nim

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ type
3232
# non-fatal errors
3333
errIllFormedAstX, errCannotOpenFile,
3434
errXExpected,
35-
errGridTableNotImplemented,
36-
errMarkdownIllformedTable,
37-
errGeneralParseError,
38-
errNewSectionExpected,
39-
errInvalidDirectiveX,
40-
errInvalidRstField,
41-
errFootnoteMismatch,
35+
errRstGridTableNotImplemented,
36+
errRstMarkdownIllformedTable,
37+
errRstNewSectionExpected,
38+
errRstGeneralParseError,
39+
errRstInvalidDirectiveX,
40+
errRstInvalidField,
41+
errRstFootnoteMismatch,
4242
errProveInit, # deadcode
4343
errGenerated,
4444
errUser,
@@ -47,10 +47,13 @@ type
4747
warnXIsNeverRead = "XIsNeverRead", warnXmightNotBeenInit = "XmightNotBeenInit",
4848
warnDeprecated = "Deprecated", warnConfigDeprecated = "ConfigDeprecated",
4949
warnSmallLshouldNotBeUsed = "SmallLshouldNotBeUsed", warnUnknownMagic = "UnknownMagic",
50-
warnRedefinitionOfLabel = "RedefinitionOfLabel", warnUnknownSubstitutionX = "UnknownSubstitutionX",
51-
warnLanguageXNotSupported = "LanguageXNotSupported",
52-
warnFieldXNotSupported = "FieldXNotSupported",
53-
warnRstStyle = "warnRstStyle", warnCommentXIgnored = "CommentXIgnored",
50+
warnRstRedefinitionOfLabel = "RedefinitionOfLabel",
51+
warnRstUnknownSubstitutionX = "UnknownSubstitutionX",
52+
warnRstBrokenLink = "BrokenLink",
53+
warnRstLanguageXNotSupported = "LanguageXNotSupported",
54+
warnRstFieldXNotSupported = "FieldXNotSupported",
55+
warnRstStyle = "warnRstStyle",
56+
warnCommentXIgnored = "CommentXIgnored",
5457
warnTypelessParam = "TypelessParam",
5558
warnUseBase = "UseBase", warnWriteToForeignHeap = "WriteToForeignHeap",
5659
warnUnsafeCode = "UnsafeCode", warnUnusedImportX = "UnusedImport",
@@ -93,13 +96,13 @@ const
9396
errIllFormedAstX: "illformed AST: $1",
9497
errCannotOpenFile: "cannot open '$1'",
9598
errXExpected: "'$1' expected",
96-
errGridTableNotImplemented: "grid table is not implemented",
97-
errMarkdownIllformedTable: "illformed delimiter row of a markdown table",
98-
errGeneralParseError: "general parse error",
99-
errNewSectionExpected: "new section expected $1",
100-
errInvalidDirectiveX: "invalid directive: '$1'",
101-
errInvalidRstField: "invalid field: $1",
102-
errFootnoteMismatch: "number of footnotes and their references don't match: $1",
99+
errRstGridTableNotImplemented: "grid table is not implemented",
100+
errRstMarkdownIllformedTable: "illformed delimiter row of a markdown table",
101+
errRstNewSectionExpected: "new section expected $1",
102+
errRstGeneralParseError: "general parse error",
103+
errRstInvalidDirectiveX: "invalid directive: '$1'",
104+
errRstInvalidField: "invalid field: $1",
105+
errRstFootnoteMismatch: "number of footnotes and their references don't match: $1",
103106
errProveInit: "Cannot prove that '$1' is initialized.", # deadcode
104107
errGenerated: "$1",
105108
errUser: "$1",
@@ -111,10 +114,11 @@ const
111114
warnConfigDeprecated: "config file '$1' is deprecated",
112115
warnSmallLshouldNotBeUsed: "'l' should not be used as an identifier; may look like '1' (one)",
113116
warnUnknownMagic: "unknown magic '$1' might crash the compiler",
114-
warnRedefinitionOfLabel: "redefinition of label '$1'",
115-
warnUnknownSubstitutionX: "unknown substitution '$1'",
116-
warnLanguageXNotSupported: "language '$1' not supported",
117-
warnFieldXNotSupported: "field '$1' not supported",
117+
warnRstRedefinitionOfLabel: "redefinition of label '$1'",
118+
warnRstUnknownSubstitutionX: "unknown substitution '$1'",
119+
warnRstBrokenLink: "broken link '$1'",
120+
warnRstLanguageXNotSupported: "language '$1' not supported",
121+
warnRstFieldXNotSupported: "field '$1' not supported",
118122
warnRstStyle: "RST style: $1",
119123
warnCommentXIgnored: "comment '$1' ignored",
120124
warnTypelessParam: "", # deadcode
@@ -196,6 +200,7 @@ const
196200
warnMax* = pred(hintSuccess)
197201
hintMin* = hintSuccess
198202
hintMax* = high(TMsgKind)
203+
rstWarnings* = {warnRstRedefinitionOfLabel..warnRstStyle}
199204

200205
type
201206
TNoteKind* = range[warnMin..hintMax] # "notes" are warnings or hints

compiler/main.nim

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ proc mainCommand*(graph: ModuleGraph) =
285285
of cmdDoc:
286286
docLikeCmd():
287287
conf.setNoteDefaults(warnLockLevel, false) # issue #13218
288-
conf.setNoteDefaults(warnRedefinitionOfLabel, false) # issue #13218
288+
conf.setNoteDefaults(warnRstRedefinitionOfLabel, false) # issue #13218
289289
# because currently generates lots of false positives due to conflation
290290
# of labels links in doc comments, e.g. for random.rand:
291291
# ## * `rand proc<#rand,Rand,Natural>`_ that returns an integer
@@ -295,19 +295,16 @@ proc mainCommand*(graph: ModuleGraph) =
295295
commandBuildIndex(conf, $conf.outDir)
296296
of cmdRst2html:
297297
# XXX: why are warnings disabled by default for rst2html and rst2tex?
298-
for warn in [warnUnknownSubstitutionX, warnLanguageXNotSupported,
299-
warnFieldXNotSupported, warnRstStyle]:
298+
for warn in rstWarnings:
300299
conf.setNoteDefaults(warn, true)
301-
conf.setNoteDefaults(warnRedefinitionOfLabel, false) # similar to issue #13218
300+
conf.setNoteDefaults(warnRstRedefinitionOfLabel, false) # similar to issue #13218
302301
when defined(leanCompiler):
303302
conf.quitOrRaise "compiler wasn't built with documentation generator"
304303
else:
305304
loadConfigs(DocConfig, cache, conf, graph.idgen)
306305
commandRst2Html(cache, conf)
307306
of cmdRst2tex, cmdDoc2tex:
308-
for warn in [warnRedefinitionOfLabel, warnUnknownSubstitutionX,
309-
warnLanguageXNotSupported,
310-
warnFieldXNotSupported, warnRstStyle]:
307+
for warn in rstWarnings:
311308
conf.setNoteDefaults(warn, true)
312309
when defined(leanCompiler):
313310
conf.quitOrRaise "compiler wasn't built with documentation generator"

0 commit comments

Comments
 (0)