Skip to content

Commit

Permalink
Merge #421
Browse files Browse the repository at this point in the history
421: internal: add missing reports and clean-up r=saem a=saem

## Summary

added missing reports for the parser:
- template missing end close
- invalid expression
- invalid filter

Misc:
- created an enum for compiler verbosity
- removed `rparName`, doesn't seem used at all
- formatting fixes


Co-authored-by: Saem Ghani <saemghani+github@gmail.com>
  • Loading branch information
bors[bot] and saem authored Aug 21, 2022
2 parents f48efd3 + 0fdcf09 commit 837238f
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 110 deletions.
50 changes: 30 additions & 20 deletions compiler/ast/lineinfos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ export FileIndex, TLineInfo

import reports

type
CompilerVerbosity* = enum
## verbosity of the compiler, number is used as an array index and the
## string matches what's passed on the CLI.
compVerbosityMin = (0, "0")
compVerbosityDefault = (1, "1")
compVerbosityHigh = (2, "2")
compVerbosityMax = (3, "3")

const
explanationsBaseUrl* = "https://nim-lang.github.io/Nim"
# was: "https://nim-lang.org/docs" but we're now usually showing devel docs
Expand All @@ -38,7 +47,7 @@ proc createDocLink*(urlSuffix: string): string =
result.add "/" & urlSuffix

proc computeNotesVerbosity(): tuple[
main: array[0..3, ReportKinds],
main: array[CompilerVerbosity, ReportKinds],
foreign: ReportKinds,
base: ReportKinds
] =
Expand Down Expand Up @@ -76,7 +85,7 @@ proc computeNotesVerbosity(): tuple[
rdbgTraceEnd # End report
}

result.main[3] = result.base + repWarningKinds + repHintKinds - {
result.main[compVerbosityMax] = result.base + repWarningKinds + repHintKinds - {
rsemObservableStores,
rsemResultUsed,
rsemAnyEnumConvert,
Expand All @@ -91,31 +100,33 @@ proc computeNotesVerbosity(): tuple[
}

if defined(release):
result.main[3].excl rintStackTrace
result.main[compVerbosityMax].excl rintStackTrace

result.main[2] = result.main[3] - {
result.main[compVerbosityHigh] = result.main[compVerbosityMax] - {
rsemUninit,
rsemExtendedContext,
rsemProcessingStmt,
rsemWarnGcUnsafe,
rextConf,
}

result.main[1] = result.main[2] - repPerformanceHints - {
rsemProveField,
rsemErrGcUnsafe,
rsemHintLibDependency,
rsemGlobalVar,
result.main[compVerbosityDefault] = result.main[compVerbosityHigh] -
repPerformanceHints -
{
rsemProveField,
rsemErrGcUnsafe,
rsemHintLibDependency,
rsemGlobalVar,

rintGCStats,
rintMsgOrigin,
rintGCStats,
rintMsgOrigin,

rextPath,
rextPath,

rlexSourceCodeFilterOutput,
}
rlexSourceCodeFilterOutput,
}

result.main[0] = result.main[1] - {
result.main[compVerbosityMin] = result.main[compVerbosityDefault] - {
rintSuccessX,
rextConf,
rsemProcessing,
Expand All @@ -138,16 +149,15 @@ proc computeNotesVerbosity(): tuple[
for idx, n in @[
result.foreign,
# result.base,
result.main[3],
result.main[2],
result.main[1],
result.main[0],
result.main[compVerbosityMax],
result.main[compVerbosityHigh],
result.main[compVerbosityDefault],
result.main[compVerbosityMin],
]:
assert rbackLinking notin n
assert rsemImplicitObjConv in n, $idx
assert rsemVmStackTrace in n, $idx


const
NotesVerbosity* = computeNotesVerbosity()

Expand Down
9 changes: 3 additions & 6 deletions compiler/ast/report_enums.nim
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ type
rparEnablePreviewDotOps = "DotLikeOps"
# warnings END !! add reports BEFORE the last enum !!

rparName = "Name" ## Linter report about used identifier

#----------------------------- Sem reports -----------------------------#
# semantic fatal
rsemFatalError
Expand Down Expand Up @@ -785,7 +783,7 @@ type
rsemUnsafeSetLen = "UnsafeSetLen"
rsemUnsafeDefault = "UnsafeDefault"
rsemBindDeprecated
rsemUncollectableRefCycle = "CycleCreated"
rsemUncollectableRefCycle = "CycleCreated"
rsemObservableStores = "ObservableStores"
rsemCaseTransition = "CaseTransition"
rsemUseOfGc = "GcMem" # last !
Expand Down Expand Up @@ -923,7 +921,7 @@ const rstWarnings* = {rbackRstTestUnsupported .. rbackRstRstStyle}

type
LexerReportKind* = range[rlexMalformedUnderscores .. rlexSourceCodeFilterOutput]
ParserReportKind* = range[rparInvalidIndentation .. rparName]
ParserReportKind* = range[rparInvalidIndentation .. rparEnablePreviewDotOps]

SemReportKind* = range[rsemFatalError .. rsemImplicitObjConv]
SemReportErrorKind* = range[rsemUserError .. rsemWrappedError]
Expand All @@ -949,7 +947,7 @@ const

#------------------------------- parser --------------------------------#
repParserKinds* = {low(ParserReportKind) .. high(ParserReportKind)}
rparHintKinds* = {rparName}
rparHintKinds* = {}
rparErrorKinds* = {rparInvalidIndentation .. rparInvalidFilter}
rparWarningKinds* = {
rparInconsistentSpacing .. rparEnablePreviewDotOps}
Expand Down Expand Up @@ -1036,7 +1034,6 @@ const
repAllKinds* = {low(ReportKind) .. high(ReportKind)}



const
rsemReportTwoSym* = {
rsemConflictingExportnims,
Expand Down
25 changes: 7 additions & 18 deletions compiler/ast/reports.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
## Not using compiler-specific types also allows this report to be easily
## reused by external tooling - custom error pretty-printers, test runners
## and so on.
##
## Debug Defines:
## `compilerDebugCompilerReportStatistics`: output stats of counts for various
## report kinds

import std/[options, packedsets]

Expand All @@ -29,7 +33,6 @@ export
options.Option,
int128.toInt128


from compiler/front/in_options import TOption, TOptions
type InstantiationInfo* = typeof(instantiationInfo())

Expand Down Expand Up @@ -180,7 +183,6 @@ type
kind*: NilTransition ## what kind of transition was that
node*: PNode ## the node of the expression


SemReport* = object of ReportBase
ast*: PNode
typ*: PType
Expand Down Expand Up @@ -353,7 +355,6 @@ type
else:
discard


func severity*(report: SemReport): ReportSeverity =
case SemReportKind(report.kind):
of rsemErrorKinds: result = rsevError
Expand Down Expand Up @@ -656,7 +657,6 @@ type
cpu*: TSystemCPU ## Target CPU
os*: TSystemOS ## Target OS


InternalReport* = object of ReportBase
## Report generated for the internal compiler workings
msg*: string
Expand Down Expand Up @@ -685,8 +685,6 @@ type
else:
discard



func severity*(report: InternalReport): ReportSeverity =
case InternalReportKind(report.kind):
of rintFatalKinds: rsevFatal
Expand All @@ -696,8 +694,6 @@ func severity*(report: InternalReport): ReportSeverity =
of rintDataPassKinds: rsevTrace




type
ReportTypes* =
LexerReport |
Expand Down Expand Up @@ -737,13 +733,12 @@ type
externalReport*: ExternalReport

static:
when false:
when defined(compilerDebugCompilerReportStatistics):
echo(
"Nimskull compiler outputs ",
ord(high(ReportKind)),
ord(high(ReportKind) + 1),
" different kinds of diagnostics")


echo "size of ReportBase ", sizeof(ReportBase)
echo "size of LexerReport ", sizeof(LexerReport)
echo "size of ParserReport ", sizeof(ParserReport)
Expand All @@ -763,7 +758,6 @@ let reportEmpty* = Report(
category: repInternal,
internalReport: InternalReport(kind: repNone))


template eachCategory*(report: Report, field: untyped): untyped =
case report.category:
of repLexer: report.lexReport.field
Expand Down Expand Up @@ -820,8 +814,6 @@ func severity*(
else:
severity(report)



func severity*(
report: Report,
asError: ReportKinds = default(ReportKinds),
Expand Down Expand Up @@ -899,7 +891,6 @@ func wrap*[R: ReportTypes](rep: sink R, iinfo: InstantiationInfo): Report =
tmp.reportInst = toReportLineInfo(iinfo)
return wrap(tmp)


func wrap*[R: ReportTypes](
rep: sink R, iinfo: ReportLineInfo, point: TLineInfo): Report =
var tmp = rep
Expand All @@ -911,14 +902,13 @@ func wrap*[R: ReportTypes](
rep: sink R, iinfo: InstantiationInfo, point: TLineInfo): Report =
wrap(rep, toReportLineInfo(iinfo), point)


func wrap*[R: ReportTypes](iinfo: InstantiationInfo, rep: sink R): Report =
wrap(rep, iinfo)


template wrap*(rep: ReportTypes): Report =
wrap(rep, toReportLineInfo(instLoc()))


func `$`*(point: ReportLineInfo): string =
point.file & "(" & $point.line & ", " & $point.col & ")"

Expand All @@ -944,7 +934,6 @@ func addReport*(list: var ReportList, report: sink Report): ReportId =
func addReport*[R: ReportTypes](list: var ReportList, report: R): ReportId =
addReport(list, wrap(report))


func getReport*(list: ReportList, id: ReportId): Report =
## Get report from the report list using it's id
list.list[int(uint32(id)) - 1]
Expand Down
14 changes: 4 additions & 10 deletions compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func wrap*(
func wrap(conf: ConfigRef, text: ColText): string =
toString(text, conf.useColor())


proc formatTrace*(conf: ConfigRef, trace: seq[StackTraceEntry]): string =
## Format stack trace entries for reporting
var paths: seq[string]
Expand Down Expand Up @@ -222,7 +221,6 @@ proc addPragmaAndCallConvMismatch*(
expectedPragmas.setLen(max(0, expectedPragmas.len - 2)) # Remove ", "
message.add "\n Pragma mismatch: got '{.$1.}', but expected '{.$2.}'." % [gotPragmas, expectedPragmas]


proc effectProblem(f, a: PType; result: var string) =
## Add effect difference annotation for `f` (aka formal/expected) and `a`
## (aka actual/provided) types
Expand Down Expand Up @@ -266,8 +264,6 @@ proc argTypeToString(arg: PNode; prefer: TPreferedDesc): string =
else:
result = arg.typ.typeToString(prefer)



proc describeArgs(conf: ConfigRef, args: seq[PNode]; prefer = preferName): string =
## Generate comma-separated list of arguments
for idx, arg in args:
Expand Down Expand Up @@ -2516,10 +2512,10 @@ proc reportBody*(conf: ConfigRef, r: ParserReport): string =
result = "invalid indentation; an export marker '*' follows the declared identifier"

of rparTemplMissingEndClose:
result = "?"
result = "'end' does not close a control flow construct"

of rparTemplInvalidExpression:
result = "?"
result = "invalid expression"

of rparInconsistentSpacing:
result = "Number of spaces around '$#' is not consistent"
Expand All @@ -2533,11 +2529,8 @@ proc reportBody*(conf: ConfigRef, r: ParserReport): string =
of rparPragmaBeforeGenericParameters:
result = "pragma must come after any generic parameter list"

of rparName:
result = "?"

of rparInvalidFilter:
result = "?"
result = "invalid filter: $1" % r.node.renderTree

proc reportFull*(conf: ConfigRef, r: ParserReport): string =
assertKind r
Expand Down Expand Up @@ -3639,6 +3632,7 @@ proc reportHook*(conf: ConfigRef, r: Report): TErrorHandling =
var indent {.global.}: int
if r.kind == rdbgTraceStep:
indent = r.debugReport.semstep.level

case r.kind
of rdbgTracerKinds:
conf.writeln(conf.reportFull(r))
Expand Down
17 changes: 9 additions & 8 deletions compiler/front/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,20 @@ proc expectNoArg(conf: ConfigRef; switch, arg: string, pass: TCmdLinePass, info:

proc processSpecificNote*(arg: string, state: TSpecialWord, pass: TCmdLinePass,
info: TLineInfo; orig: string; conf: ConfigRef) =
var id = "" # arg = key or [key] or key:val or [key]:val; with val=on|off
var i = 0
var notes: ReportKinds
var isBracket = false
var
id = "" # arg = key or [key] or key:val or [key]:val; with val=on|off
i = 0
notes: ReportKinds
isBracket = false

if i < arg.len and arg[i] == '[':
isBracket = true
inc(i)

while i < arg.len and (arg[i] notin {':', '=', ']'}):
id.add(arg[i])
inc(i)

if isBracket:
if i < arg.len and arg[i] == ']': inc(i)
else: invalidCmdLineOption(conf, pass, orig, info)
Expand All @@ -288,7 +292,6 @@ proc processSpecificNote*(arg: string, state: TSpecialWord, pass: TCmdLinePass,
let x = findStr(noteSet, id, onFail)
if x != onFail:
notes = {x}

else:
var r = ExternalReport(kind: onFail)
r.cmdlineProvided = id
Expand All @@ -312,7 +315,6 @@ proc processSpecificNote*(arg: string, state: TSpecialWord, pass: TCmdLinePass,
# `hints|warnings|warningAsErrors` for all the code they depend on.
conf.localReport ExternalReport(
kind: rextExpectedOnOrOff, cmdlineProvided: arg)

else:
let isOn = val == "on"
if isOn and id.normalize == "all":
Expand All @@ -328,7 +330,6 @@ proc processSpecificNote*(arg: string, state: TSpecialWord, pass: TCmdLinePass,
if state in {wWarningAsError, wHintAsError}:
# xxx rename warningAsErrors to noteAsErrors
conf.flip(cnWarnAsError, n, isOn)

else:
conf.flip(cnCurrent, n, isOn)
conf.flip(cnMainPackage, n, isOn)
Expand Down Expand Up @@ -1057,7 +1058,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
if verbosity notin {0 .. 3}:
conf.localReport(
info, invalidSwitchValue @["0", "1", "2", "3"])
conf.verbosity = verbosity
conf.verbosity = CompilerVerbosity(verbosity)
var verb = NotesVerbosity.main[conf.verbosity]
## We override the default `verb` by explicitly modified (set/unset) notes.
conf.notes = (conf.modifiedyNotes * conf.notes + verb) -
Expand Down
Loading

0 comments on commit 837238f

Please sign in to comment.