This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Main.d
161 lines (142 loc) · 3.91 KB
/
Main.d
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
// Copyright (c) 2014- Facebook
// License: Boost License 1.0, http://boost.org/LICENSE_1_0.txt
// @author Andrei Alexandrescu (andrei.alexandrescu@facebook.com)
import std.conv, std.file, std.getopt, std.stdio, std.string, std.path;
import Checks, FileCategories, Ignored, Tokenizer;
bool recursive = true;
bool include_what_you_use = false;
string[] exclude_checks;
uint function(string, Token[])[string] checks;
uint function(string, Token[])[string] cppChecks;
/**
* Entry point. Reads in turn and verifies each file passed onto the
* command line.
*/
int main(string[] args) {
try {
getopt(args,
"recursive", &recursive,
"c_mode", &c_mode,
"include_what_you_use", &include_what_you_use,
"exclude", &exclude_checks);
} catch (Exception e) {
stderr.writeln(e.msg);
stderr.writeln("usage: flint " ~
"[--recursive] [--c_mode], [--include_what_you_use]" ~
"[--exclude=<rule>,...]");
}
checks = mixin(
makeHashtable!(
checkBlacklistedSequences,
checkBlacklistedIdentifiers,
checkDefinedNames,
checkIfEndifBalance,
checkIncludeGuard,
checkMemset,
checkQuestionableIncludes,
checkInlHeaderInclusions,
checkInitializeFromItself,
checkRandomUsage,
checkSleepUsage,
checkSmartPtrUsage,
checkUniquePtrUsage,
checkBannedIdentifiers,
checkOSSIncludes,
checkMultipleIncludes,
checkBreakInSynchronized,
checkBogusComparisons,
checkExitStatus,
checkAttributeArgumentUnderscores
)
);
version(facebook) {
checks["checkAngleBracketIncludes"] = &checkAngleBracketIncludes;
}
cppChecks = mixin(
makeHashtable!(
checkNamespaceScopedStatics,
checkIncludeAssociatedHeader,
checkCatchByReference,
checkConstructors,
checkVirtualDestructors,
checkThrowSpecification,
checkThrowsHeapException,
checkUsingNamespaceDirectives,
checkUsingDirectives,
checkFollyDetail,
checkFollyStringPieceByValue,
checkProtectedInheritance,
checkImplicitCast,
checkUpcaseNull,
checkExceptionInheritance,
checkMutexHolderHasName)
);
if (include_what_you_use) {
cppChecks["checkDirectStdInclude"] = &checkDirectStdInclude;
}
foreach (string s ; exclude_checks) {
string ss = strip(s);
checks.remove(ss);
cppChecks.remove(ss);
}
// Check each file
uint errors = 0;
foreach (arg; args) {
errors += checkEntry(arg);
}
return 0;
}
auto makeHashtable(T...)() {
string result = `[`;
foreach (t; T) {
string name = __traits(identifier, t);
result ~= `"` ~ name ~ `" : &` ~ name ~ ", ";
}
return result ~= `]`;
}
bool dontLintPath(string path) {
return std.path.buildPath(path, ".nolint").exists;
}
uint checkEntry(string path) {
if (!path.exists) {
return 0;
}
uint errors = 0;
if (path.isDir) {
if (!recursive || dontLintPath(path)) {
return 0;
}
foreach (entry; dirEntries(path, SpanMode.shallow)) {
if (!dontLintPath(entry)) {
errors += checkEntry(entry);
}
}
return errors;
}
if (getFileCategory(path) == FileCategory.unknown) {
return 0;
}
try {
// Get file intro memory
string file = to!string(path.read);
Token[] tokens;
// Remove code that occurs in pairs of
// "// %flint: pause" & "// %flint: resume"
file = removeIgnoredCode(file, path);
// Tokenize the file's contents
tokens = tokenize(file, path);
// *** Checks each lint rule
foreach (uint function(string, Token[]) check ; checks.byValue()) {
errors += check(path, tokens);
}
if (!c_mode) {
foreach (uint function(string, Token[]) check ; cppChecks.byValue()) {
errors += check(path, tokens);
}
}
} catch (Exception e) {
stderr.writef("Flint was unable to lint %s\n", path);
stderr.writeln(e.toString());
}
return errors;
}