-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathCxxAstScanner.java
258 lines (228 loc) · 10.1 KB
/
CxxAstScanner.java
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2018 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.GenericTokenType;
import com.sonar.sslr.api.Grammar;
import com.sonar.sslr.api.Token;
import com.sonar.sslr.impl.Parser;
import static java.lang.Math.min;
import java.util.Collection;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.cxx.api.CxxMetric;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.cxx.parser.CxxParser;
import org.sonar.cxx.visitors.CxxCharsetAwareVisitor;
import org.sonar.cxx.visitors.CxxCognitiveComplexityVisitor;
import org.sonar.cxx.visitors.CxxCyclomaticComplexityVisitor;
import org.sonar.cxx.visitors.CxxFileVisitor;
import org.sonar.cxx.visitors.CxxFunctionComplexityVisitor;
import org.sonar.cxx.visitors.CxxFunctionSizeVisitor;
import org.sonar.cxx.visitors.CxxLinesOfCodeInFunctionBodyVisitor;
import org.sonar.cxx.visitors.CxxLinesOfCodeVisitor;
import org.sonar.cxx.visitors.CxxParseErrorLoggerVisitor;
import org.sonar.cxx.visitors.CxxPublicApiVisitor;
import org.sonar.squidbridge.AstScanner;
import org.sonar.squidbridge.CommentAnalyser;
import org.sonar.squidbridge.SourceCodeBuilderCallback;
import org.sonar.squidbridge.SourceCodeBuilderVisitor;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.squidbridge.SquidAstVisitorContextImpl;
import org.sonar.squidbridge.api.SourceClass;
import org.sonar.squidbridge.api.SourceCode;
import org.sonar.squidbridge.api.SourceFile;
import org.sonar.squidbridge.api.SourceFunction;
import org.sonar.squidbridge.api.SourceProject;
import org.sonar.squidbridge.indexer.QueryByType;
import org.sonar.squidbridge.metrics.CommentsVisitor;
import org.sonar.squidbridge.metrics.ComplexityVisitor;
import org.sonar.squidbridge.metrics.CounterVisitor;
import org.sonar.squidbridge.metrics.LinesVisitor;
public final class CxxAstScanner {
private CxxAstScanner() {
}
/**
* Helper method for testing checks without having to deploy them on a Sonar instance.
*
* @param file is the file to be checked
* @param sensorContext SQ API batch side context
* @param visitors AST checks and visitors to use
* @param language CxxLanguage to use
* @return file checked with measures and issues
*/
@SafeVarargs
public static SourceFile scanSingleFile(InputFile file, SensorContext sensorContext, CxxLanguage language,
SquidAstVisitor<Grammar>... visitors) {
return scanSingleFileConfig(language, file, new CxxConfiguration(sensorContext.fileSystem().encoding()),
visitors);
}
/**
* Helper method for scanning a single file
*
* @param file is the file to be checked
* @param cxxConfig the plugin configuration
* @param visitors AST checks and visitors to use
* @param language for sensor
* @return file checked with measures and issues
*/
public static SourceFile scanSingleFileConfig(CxxLanguage language, InputFile file, CxxConfiguration cxxConfig,
SquidAstVisitor<Grammar>... visitors) {
if (!file.isFile()) {
throw new IllegalArgumentException("File '" + file + "' not found.");
}
AstScanner<Grammar> scanner = create(language, cxxConfig, visitors);
scanner.scanFile(file.file());
Collection<SourceCode> sources = scanner.getIndex().search(new QueryByType(SourceFile.class));
if (sources.size() != 1) {
throw new IllegalStateException("Only one SourceFile was expected whereas "
+ sources.size() + " has been returned.");
}
return (SourceFile) sources.iterator().next();
}
/**
* Create scanner for language
*
* @param language for sensor
* @param conf settings for sensor
* @param visitors visitors AST checks and visitors to use
* @return scanner for the given parameters
*/
@SafeVarargs
public static AstScanner<Grammar> create(CxxLanguage language, CxxConfiguration conf,
SquidAstVisitor<Grammar>... visitors) {
final SquidAstVisitorContextImpl<Grammar> context
= new SquidAstVisitorContextImpl<>(new SourceProject("Cxx Project"));
final Parser<Grammar> parser = CxxParser.create(context, conf, language);
AstScanner.Builder<Grammar> builder = AstScanner.<Grammar>builder(context).setBaseParser(parser);
/* Metrics */
builder.withMetrics(CxxMetric.values());
/* Files */
builder.setFilesMetric(CxxMetric.FILES);
/* Comments */
builder.setCommentAnalyser(
new CommentAnalyser() {
@Override
public boolean isBlank(String line) {
for (int i = 0; i < line.length(); i++) {
if (Character.isLetterOrDigit(line.charAt(i))) {
return false;
}
}
return true;
}
@Override
public String getContents(String comment) {
return "/*".equals(comment.substring(0, 2))
? comment.substring(2, comment.length() - 2)
: comment.substring(2);
}
});
/* Functions */
builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<>((SourceCode parentSourceCode, AstNode astNode) -> {
StringBuilder sb = new StringBuilder();
for (Token token : astNode.getFirstDescendant(CxxGrammarImpl.declaratorId).getTokens()) {
sb.append(token.getValue());
}
String functionName = sb.toString();
sb.setLength(0);
// todo: check if working with nested-namespace-definition
AstNode namespace = astNode.getFirstAncestor(CxxGrammarImpl.namedNamespaceDefinition);
while (namespace != null) {
if (sb.length() > 0) {
sb.insert(0, "::");
}
sb.insert(0, namespace.getFirstDescendant(GenericTokenType.IDENTIFIER).getTokenValue());
// todo: check if working with nested-namespace-definition
namespace = namespace.getFirstAncestor(CxxGrammarImpl.namedNamespaceDefinition);
}
String namespaceName = sb.length() > 0 ? sb.toString() + "::" : "";
SourceFunction function = new SourceFunction(intersectingConcatenate(namespaceName, functionName)
+ ":" + astNode.getToken().getLine());
function.setStartAtLine(astNode.getTokenLine());
return function;
}, CxxGrammarImpl.functionDefinition));
builder.withSquidAstVisitor(CounterVisitor.<Grammar>builder()
.setMetricDef(CxxMetric.FUNCTIONS)
.subscribeTo(CxxGrammarImpl.functionDefinition)
.build());
/* Classes */
builder.withSquidAstVisitor(new SourceCodeBuilderVisitor<>((SourceCode parentSourceCode, AstNode astNode) -> {
AstNode classNameAst = astNode.getFirstDescendant(CxxGrammarImpl.className);
String className = classNameAst == null ? "" : classNameAst.getFirstChild().getTokenValue();
SourceClass cls = new SourceClass(className + ":" + astNode.getToken().getLine(), className);
cls.setStartAtLine(astNode.getTokenLine());
return cls;
}, CxxGrammarImpl.classSpecifier));
builder.withSquidAstVisitor(CounterVisitor.<Grammar>builder()
.setMetricDef(CxxMetric.CLASSES)
.subscribeTo(CxxGrammarImpl.classSpecifier)
.build());
/* Metrics */
builder.withSquidAstVisitor(new LinesVisitor<>(CxxMetric.LINES));
builder.withSquidAstVisitor(new CxxLinesOfCodeVisitor<>(CxxMetric.LINES_OF_CODE));
builder.withSquidAstVisitor(new CxxLinesOfCodeInFunctionBodyVisitor<>());
builder.withSquidAstVisitor(new CxxPublicApiVisitor<>(language));
builder.withSquidAstVisitor(CommentsVisitor.<Grammar>builder().withCommentMetric(CxxMetric.COMMENT_LINES)
.withNoSonar(true)
.withIgnoreHeaderComment(conf.getIgnoreHeaderComments())
.build());
/* Statements */
builder.withSquidAstVisitor(CounterVisitor.<Grammar>builder()
.setMetricDef(CxxMetric.STATEMENTS)
.subscribeTo(CxxGrammarImpl.statement)
.build());
builder.withSquidAstVisitor(new CxxCyclomaticComplexityVisitor(ComplexityVisitor.<Grammar>builder()
.setMetricDef(CxxMetric.COMPLEXITY)
.subscribeTo(CxxComplexityConstants.getCyclomaticComplexityTypes())
.build()));
builder.withSquidAstVisitor(new CxxCognitiveComplexityVisitor<>());
builder.withSquidAstVisitor(new CxxFunctionComplexityVisitor<>(language));
builder.withSquidAstVisitor(new CxxFunctionSizeVisitor<>(language));
// to emit a 'new file' event to the internals of the plugin
builder.withSquidAstVisitor(new CxxFileVisitor<>(context));
// log syntax errors
builder.withSquidAstVisitor(new CxxParseErrorLoggerVisitor<>(context));
/* External visitors (typically Check ones) */
for (SquidAstVisitor<Grammar> visitor : visitors) {
if (visitor instanceof CxxCharsetAwareVisitor) {
((CxxCharsetAwareVisitor) visitor).setCharset(conf.getCharset());
}
builder.withSquidAstVisitor(visitor);
}
return builder.build();
}
// Concatenate two strings, but if there is overlap at the intersection,
// include the intersection/overlap only once.
public static String intersectingConcatenate(String a, String b) {
// find length of maximum possible match
int lenOfA = a.length();
int lenOfB = b.length();
int minIntersectionLen = min(lenOfB, lenOfA);
// search down from maximum match size, to get longest possible intersection
for (int size = minIntersectionLen; size > 0; size--) {
if (a.regionMatches(lenOfA - size, b, 0, size)) {
return a + b.substring(size, lenOfB);
}
}
// Didn't find any intersection. Fall back to straight concatenation.
return a + b;
}
}