From bd689849f28ba98b9b2581605234a2b52fb2e392 Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 10 Jul 2019 00:27:23 +0200 Subject: nim styleChecker: implemented all the missing features (bugfix) --- compiler/linter.nim | 96 +++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 43 deletions(-) (limited to 'compiler/linter.nim') diff --git a/compiler/linter.nim b/compiler/linter.nim index a881f2711..d1d3140c0 100644 --- a/compiler/linter.nim +++ b/compiler/linter.nim @@ -25,17 +25,12 @@ proc identLen*(line: string, start: int): int = type StyleCheck* {.pure.} = enum None, Warn, Auto -var - gOverWrite* = true - gStyleCheck*: StyleCheck - gCheckExtern*, gOnlyMainfile*: bool - proc overwriteFiles*(conf: ConfigRef) = let doStrip = options.getConfigVar(conf, "pretty.strip").normalize == "on" for i in 0 .. high(conf.m.fileInfos): if conf.m.fileInfos[i].dirty and - (not gOnlyMainfile or FileIndex(i) == conf.projectMainIdx): - let newFile = if gOverWrite: conf.m.fileInfos[i].fullpath + (FileIndex(i) == conf.projectMainIdx): + let newFile = if false: conf.m.fileInfos[i].fullpath else: conf.m.fileInfos[i].fullpath.changeFileExt(".pretty.nim") try: var f = open(newFile.string, fmWrite) @@ -69,7 +64,7 @@ proc beautifyName(s: string, k: TSymKind): string = "pointer", "float", "csize", "cdouble", "cchar", "cschar", "cshort", "cu", "nil", "typedesc", "auto", "any", "range", "openarray", "varargs", "set", "cfloat", "ref", "ptr", - "untyped", "typed", "static", "sink", "lent", "type"]: + "untyped", "typed", "static", "sink", "lent", "type", "owned"]: result.add s[i] else: result.add toUpperAscii(s[i]) @@ -98,46 +93,23 @@ proc beautifyName(s: string, k: TSymKind): string = result.add s[i] inc i -proc differ*(line: string, a, b: int, x: string): bool = +proc differ*(line: string, a, b: int, x: string): string = let y = line[a..b] - result = cmpIgnoreStyle(y, x) == 0 and y != x - -proc replaceInFile(conf: ConfigRef; info: TLineInfo; newName: string) = - let line = conf.m.fileInfos[info.fileIndex.int].lines[info.line.int-1] - var first = min(info.col.int, line.len) - if first < 0: return - #inc first, skipIgnoreCase(line, "proc ", first) - while first > 0 and line[first-1] in Letters: dec first - if first < 0: return - if line[first] == '`': inc first - - let last = first+identLen(line, first)-1 - if differ(line, first, last, newName): - # last-first+1 != newName.len or - var x = line.substr(0, first-1) & newName & line.substr(last+1) - system.shallowCopy(conf.m.fileInfos[info.fileIndex.int].lines[info.line.int-1], x) - conf.m.fileInfos[info.fileIndex.int].dirty = true - -proc lintReport(conf: ConfigRef; info: TLineInfo, beau: string) = - if optStyleError in conf.globalOptions: - localError(conf, info, "name should be: '$1'" % beau) + if cmpIgnoreStyle(y, x) == 0 and y != x: + result = y else: - message(conf, info, hintName, beau) + result = "" proc checkStyle(conf: ConfigRef; cache: IdentCache; info: TLineInfo, s: string, k: TSymKind; sym: PSym) = let beau = beautifyName(s, k) if s != beau: - if gStyleCheck == StyleCheck.Auto: - sym.name = getIdent(cache, beau) - replaceInFile(conf, info, beau) - else: - lintReport(conf, info, beau) + lintReport(conf, info, beau, s) proc styleCheckDefImpl(conf: ConfigRef; cache: IdentCache; info: TLineInfo; s: PSym; k: TSymKind) = # operators stay as they are: if k in {skResult, skTemp} or s.name.s[0] notin Letters: return if k in {skType, skGenericParam} and sfAnon in s.flags: return - if {sfImportc, sfExportc} * s.flags == {} or gCheckExtern: + if {sfImportc, sfExportc} * s.flags == {}: checkStyle(conf, cache, info, s.name.s, k, s) proc nep1CheckDefImpl(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) = @@ -148,7 +120,7 @@ proc nep1CheckDefImpl(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) = if {sfImportc, sfExportc} * s.flags != {}: return let beau = beautifyName(s.name.s, k) if s.name.s != beau: - lintReport(conf, info, beau) + lintReport(conf, info, beau, s.name.s) template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym; k: TSymKind) = if {optStyleHint, optStyleError} * conf.globalOptions != {}: @@ -161,19 +133,57 @@ template styleCheckDef*(conf: ConfigRef; info: TLineInfo; s: PSym) = template styleCheckDef*(conf: ConfigRef; s: PSym) = styleCheckDef(conf, s.info, s, s.kind) -proc styleCheckUseImpl(conf: ConfigRef; info: TLineInfo; s: PSym) = +proc differs(conf: ConfigRef; info: TLineInfo; newName: string): string = + let line = sourceLine(conf, info) + var first = min(info.col.int, line.len) + if first < 0: return + #inc first, skipIgnoreCase(line, "proc ", first) + while first > 0 and line[first-1] in Letters: dec first + if first < 0: return + if line[first] == '`': inc first + + let last = first+identLen(line, first)-1 + result = differ(line, first, last, newName) + +proc styleCheckUse*(conf: ConfigRef; info: TLineInfo; s: PSym) = if info.fileIndex.int < 0: return # we simply convert it to what it looks like in the definition # for consistency # operators stay as they are: - if s.kind in {skResult, skTemp} or s.name.s[0] notin Letters: + if s.kind == skTemp or s.name.s[0] notin Letters or sfAnon in s.flags: return - if s.kind in {skType, skGenericParam} and sfAnon in s.flags: return + let newName = s.name.s + let oldName = differs(conf, info, newName) + if oldName.len > 0: + lintReport(conf, info, newName, oldName) + +proc checkPragmaUse*(conf: ConfigRef; info: TLineInfo; pragmaName: string) = + const inMixedCase = [ + "noSideEffect", "importCompilerProc", "incompleteStruct", "requiresInit", + "sideEffect", "compilerProc", "lineDir", "stackTrace", "lineTrace", + "rangeChecks", "boundChecks", + "overflowChecks", "nilChecks", + "floatChecks", "nanChecks", "infChecks", "moveChecks", + "nonReloadable", "executeOnReload", + "deadCodeElim", + "compileTime", "noInit", "fieldChecks", + "linearScanEnd", + "computedGoto", "injectStmt", + "asmNoStackframe", "implicitStatic", "codegenDecl", "liftLocals" + ] + let name = pragmaName.normalize + for x in inMixedCase: + if x.normalize == name: + if pragmaName != x: + lintReport(conf, info, x, pragmaName) + return + + let wanted = pragmaName.toLowerAscii.replace("_", "") + if pragmaName != wanted: + lintReport(conf, info, wanted, pragmaName) - replaceInFile(conf, info, newName) - #if newName == "File": writeStackTrace() template styleCheckUse*(info: TLineInfo; s: PSym) = when defined(nimfix): -- cgit 1.4.1-2-gfad0 ub <noreply@github.com> 2021-05-30 22:39:17 +0200 upgrade ubuntu 16.04 (not supported starting dec 2021) => 18.04; revive Linux_i386 (#18107)' href='/ahoang/Nim/commit/azure-pipelines.yml?h=devel&id=4274e6e4d700ef72275344afed44d0b658729761'>4274e6e4d ^
d5f011d9e ^
8ef509b85 ^
d5f011d9e ^

8ef509b85 ^
d5f011d9e ^

90808877c ^
d5f011d9e ^

90808877c ^









d5f011d9e ^









fb02b5695 ^
d5f011d9e ^

fb02b5695 ^


dbb053492 ^




fb02b5695 ^
d5f011d9e ^
d5f011d9e ^

3c622d799 ^

dbb053492 ^
d5f011d9e ^

ddc5f8fbc ^


d5f011d9e ^
ddc5f8fbc ^
d5f011d9e ^

dbb053492 ^
d5f011d9e ^

ddc5f8fbc ^


c292c57e4 ^
d5f011d9e ^
ddc5f8fbc ^
d5f011d9e ^
c292c57e4 ^
d5f011d9e ^

d5f011d9e ^

97825805e ^

e70294dff ^

d5f011d9e ^
ddc5f8fbc ^
d5f011d9e ^
e70294dff ^
d5f011d9e ^





ddc5f8fbc ^
d5f011d9e ^





ddc5f8fbc ^

d5f011d9e ^

dbb053492 ^
d5f011d9e ^


dbb053492 ^
d5f011d9e ^

ddc5f8fbc ^

c49562825 ^
ddc5f8fbc ^
d5f011d9e ^

dbb053492 ^
d5f011d9e ^

dbb053492 ^
d5f011d9e ^

3c622d799 ^
dbb053492 ^
d5f011d9e ^

dce0b3b00 ^
dbb053492 ^
dce0b3b00 ^








d5f011d9e ^
c49562825 ^

dbb053492 ^
c49562825 ^
d5f011d9e ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168