-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathCxxSquidConfiguration.java
576 lines (533 loc) · 19.6 KB
/
CxxSquidConfiguration.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* C++ Community Plugin (cxx plugin)
* Copyright (C) 2010-2022 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.config;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Verifier;
import org.jdom2.filter.Filters;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import org.sonar.api.utils.PathUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.squidbridge.api.SquidConfiguration;
/**
* Database for compile options.
*
* To analyze source code additional information like defines and includes are needed. Only then it is possible for the
* preprocessor and parser to generate an complete abstract syntax tree.
*
* The class allows to store information as key/value pairs. The information is also arranged hierarchically. If an
* information is not found on one level, the next higher level is searched. Additional information can be e.g. on file
* level (translation unit), global or from sonar-project.properties.
*
* Pre-defined hierarchy (levels): PredefinedMacros, SonarProjectProperties, Global, Units
*
* With {@code add} the key/value pairs are added to the database. The level parameter defines the level on which the
* data should be inserted. For level a predefined name can be used or a new one can be defined. If level is an
* identifier, the information is created in an element with the level-name directly under root. If level is a path, the
* information is stored on Units level.
*
* With {@code get} and {@code getValues} the information is read out again afterwards. {@code get} returns the first
* found value for key, whereby the search starts on level. {@code getValues} collects all found values over all levels.
* It starts with the given level and further found values are added to the end of the list.
*/
public class CxxSquidConfiguration extends SquidConfiguration {
// Levels
public static final String PREDEFINED_MACROS = "PredefinedMacros";
public static final String SONAR_PROJECT_PROPERTIES = "SonarProjectProperties";
public static final String GLOBAL = "Global";
public static final String UNITS = "Units";
// name of 'Value' elements
public static final String VALUE = "Value";
// SonarProjectProperties
public static final String ERROR_RECOVERY_ENABLED = "ErrorRecoveryEnabled";
public static final String CPD_IGNORE_LITERALS = "CpdIgnoreLiterals";
public static final String CPD_IGNORE_IDENTIFIERS = "CpdIgnoreIdentifiers";
public static final String FUNCTION_COMPLEXITY_THRESHOLD = "FunctionComplexityThreshold";
public static final String FUNCTION_SIZE_THRESHOLD = "FunctionSizeThreshold";
public static final String API_FILE_SUFFIXES = "ApiFileSuffixes";
public static final String JSON_COMPILATION_DATABASE = "JsonCompilationDatabase";
// Global/File Properties
public static final String DEFINES = "Defines";
public static final String INCLUDE_DIRECTORIES = "IncludeDirectories";
public static final String FORCE_INCLUDES = "ForceIncludes";
private static final Logger LOG = Loggers.get(CxxSquidConfiguration.class);
private final XPathFactory xFactory = XPathFactory.instance();
private final LinkedList<Element> parentList = new LinkedList<>();
private Document document;
private String baseDir = "";
public CxxSquidConfiguration() {
this("", Charset.defaultCharset());
}
public CxxSquidConfiguration(String baseDir) {
this(baseDir, Charset.defaultCharset());
}
/**
* Ctor.
*
* Creates the initial hierarchy for the data storage.
*/
public CxxSquidConfiguration(String baseDir, Charset encoding) {
super(encoding);
this.baseDir = baseDir;
var root = new Element("CompilationDatabase");
root.setAttribute(new Attribute("version", "1.0"));
document = new Document(root);
var element = new Element(PREDEFINED_MACROS);
root.addContent(element);
parentList.addFirst(element);
element = new Element(SONAR_PROJECT_PROPERTIES);
root.addContent(element);
parentList.addFirst(element);
element = new Element(GLOBAL);
root.addContent(element);
parentList.addFirst(element);
// UNITS must be first one in the list
element = new Element(UNITS);
root.addContent(element);
parentList.addFirst(element);
}
/**
* Does items on Units level exist?
*
* @return false if empty
*/
public boolean isUnitsEmpty() {
return parentList.getFirst().getContentSize() == 0;
}
/**
* Add a single key/value pair (property) to the database.
*
* @param level The level parameter defines the level on which the data should be inserted. For level a predefined
* name can be used or a new one can be defined. <br>
* - If level is an identifier, the information is created in an element with the level-name directly under root.<br>
* - If level is a path, the information is stored on Units level. In that case the level-string is normalized and
* converted to lower case letters to simplify the following search.
* @param key the key to be placed into the database.
* @param value the value corresponding to key. Several values can be assigned to one key. Internally a value-list for
* key is created. The method can be called several times for this, but more effective is the method
* {@code add(String, String, List<String>)}.
*/
public void add(String level, String key, @Nullable String value) {
if (value != null && !value.isEmpty()) {
Element eKey = getKey(level, key);
setValue(eKey, value);
}
}
/**
* Add a single key/value pair (property) to the database.
*
* Same as {@code add(String, String, String)} for an {@code Optional<String>}.
*
* @param level defines the level on which the data should be inserted
* @param key the key to be placed into the database
* @param value the value corresponding to key
*/
public void add(String level, String key, Optional<String> value) {
if (value.isPresent()) {
Element eKey = getKey(level, key);
setValue(eKey, value.get());
}
}
/**
* Add key/value pairs (properties) from an array to the database.
*
* Same as {@code add(String, String, String)} for an array of values.
*
* @param level defines the level on which the data should be inserted
* @param key the key to be placed into the database
* @param values the values corresponding to key
*/
public void add(String level, String key, @Nullable String[] values) {
if (values != null) {
Element eKey = getKey(level, key);
for (var value : values) {
setValue(eKey, value);
}
}
}
/**
* Add key/value pairs (properties) from a list to the database.
*
* Same as {@code add(String, String, String)} for a list of values.
*
* @param level defines the level on which the data should be inserted
* @param key the key to be placed into the database
* @param values the values corresponding to key
*/
public void add(String level, String key, List<String> values) {
if (!values.isEmpty()) {
Element eKey = getKey(level, key);
for (var value : values) {
setValue(eKey, value);
}
}
}
/**
* Searches for the property with the specified key.
*
* The first occurrence of a single value is searched for. The search is started at the specified level and if no
* entry is found, it is continued to the higher level. The method can return {@code Optional#empty()} if the property
* is not set.
*
* @param level level at which the search is started
* @param key the property key
* @return The value in this property list with the specified key value. Can return {@code Optional#empty()} if the
* property is not set.
*/
public Optional<String> get(String level, String key) {
Element eLevel = findLevel(level, parentList.getFirst());
do {
if (eLevel != null) {
Element eKey = eLevel.getChild(key);
if (eKey != null) {
return Optional.of(eKey.getChildText(VALUE));
}
}
eLevel = getParentElement(eLevel);
} while (eLevel != null);
return Optional.empty();
}
/**
* Used to read multi-valued properties from one level.
*
* The method can return an empty list if the property is not set.
*
* @param level level to read
* @param key key that is searched for
* @return the values with the specified key value
*/
public List<String> getLevelValues(String level, String key) {
List<String> result = new ArrayList<>();
Element eLevel = findLevel(level, null);
if (eLevel != null) {
Element eKey = eLevel.getChild(key);
if (eKey != null) {
for (var value : eKey.getChildren(VALUE)) {
result.add(value.getText());
}
}
}
return result;
}
/**
* Used to read multi-valued properties.
*
* Collects all found values over all levels. It starts with the given level and further found values in parent levels
* are added to the end of the list. The method can return an empty list if the property is not set.
*
* @param level level at which the search is started
* @param key key that is searched for
* @return the values with the specified key value
*/
public List<String> getValues(String level, String key) {
List<String> result = new ArrayList<>();
Element eLevel = findLevel(level, parentList.getFirst());
do {
if (eLevel != null) {
Element eKey = eLevel.getChild(key);
if (eKey != null) {
for (var value : eKey.getChildren(VALUE)) {
result.add(value.getText());
}
}
}
eLevel = getParentElement(eLevel);
} while (eLevel != null);
return result;
}
/**
* Used to read multi-valued properties.
*
* Collects all found values over all children. Further found values in parent levels are added to the end of the
* list. The method can return an empty list if the property is not set.
*
* @param level start level from which the values of all children are returned
* @param key property key that is searched for in all children
* @return the values with the specified key value
*/
public List<String> getChildrenValues(String level, String key) {
List<String> result = new ArrayList<>();
Element eLevel = findLevel(level, parentList.getFirst());
if (eLevel != null) {
for (var child : eLevel.getChildren()) {
Element eKey = child.getChild(key);
if (eKey != null) {
for (var value : eKey.getChildren(VALUE)) {
result.add(value.getText());
}
}
}
}
// add content of shared parents only once at the end
eLevel = getParentElement(eLevel);
if (eLevel != null) {
result.addAll(getValues(eLevel.getName(), key));
}
return result;
}
/**
* Effective value as boolean.
*
* @return {@code true} if the effective value is {@code "true"}, {@code false} for any other non empty value. If the
* property does not have value nor default value, then {@code empty} is returned.
*/
public Optional<Boolean> getBoolean(String level, String key) {
return get(level, key).map(String::trim).map(Boolean::parseBoolean);
}
/**
* Effective value as {@code int}.
*
* @return the value as {@code int}. If the property does not have value nor default value, then {@code empty} is
* returned.
* @throws IllegalStateException if value is not empty and is not a parsable integer
*/
public Optional<Integer> getInt(String level, String key) {
try {
return get(level, key).map(String::trim).map(Integer::parseInt);
} catch (NumberFormatException e) {
throw new IllegalStateException(
String.format("The property '%s' is not an int value: %s", key, e.getMessage())
);
}
}
/**
* Effective value as {@code long}.
*
* @return the value as {@code long}. If the property does not have value nor default value, then {@code empty} is
* returned.
* @throws IllegalStateException if value is not empty and is not a parsable {@code long}
*/
public Optional<Long> getLong(String level, String key) {
try {
return get(level, key).map(String::trim).map(Long::parseLong);
} catch (NumberFormatException e) {
throw new IllegalStateException(
String.format("The property '%s' is not an long value: %s", key, e.getMessage())
);
}
}
/**
* Effective value as {@code Float}.
*
* @return the value as {@code Float}. If the property does not have value nor default value, then {@code empty} is
* returned.
* @throws IllegalStateException if value is not empty and is not a parsable number
*/
public Optional<Float> getFloat(String level, String key) {
try {
return get(level, key).map(String::trim).map(Float::valueOf);
} catch (NumberFormatException e) {
throw new IllegalStateException(
String.format("The property '%s' is not an float value: %s", key, e.getMessage())
);
}
}
/**
* Effective value as {@code Double}.
*
* @return the value as {@code Double}. If the property does not have value nor default value, then {@code empty} is
* returned.
* @throws IllegalStateException if value is not empty and is not a parsable number
*/
public Optional<Double> getDouble(String level, String key) {
try {
return get(level, key).map(String::trim).map(Double::valueOf);
} catch (NumberFormatException e) {
throw new IllegalStateException(
String.format("The property '%s' is not an double value: %s", key, e.getMessage())
);
}
}
/**
* Write object to a stream: XML/UTF-8 encoded.
*
* @param out OutputStream to use.
* @throws IllegalStateException in case of IOException
*/
public void save(OutputStream out) {
try {
var xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(document, out);
} catch (IOException e) {
throw new IllegalStateException("Can't save XML document", e);
}
}
/**
* Returns a string representation of the object: XML/UTF-8 encoded.
*
* @return object XML encoded
*/
@Override
public String toString() {
var stream = new ByteArrayOutputStream();
save(stream);
return stream.toString(StandardCharsets.UTF_8);
}
public String getBaseDir() {
return baseDir;
}
public void readMsBuildFiles(List<File> logFiles, String charsetName) {
MsBuild msBuild = new MsBuild(this);
for (var logFile : logFiles) {
if (logFile.exists()) {
msBuild.parse(logFile, baseDir, charsetName);
} else {
LOG.error("MsBuild log file not found: '{}'", logFile.getAbsolutePath());
}
}
}
public void readJsonCompilationDb() {
var jsonDbFile = get(CxxSquidConfiguration.SONAR_PROJECT_PROPERTIES,
CxxSquidConfiguration.JSON_COMPILATION_DATABASE);
if (jsonDbFile.isPresent()) {
try {
var jsonDb = new JsonCompilationDatabase(this);
jsonDb.parse(new File(jsonDbFile.get()));
} catch (IOException e) {
LOG.error("Cannot access Json DB File: " + e.getMessage());
}
}
}
/**
* Create uniform notation of path names.
*
* Normalize path and replace file separators by forward slash. Resulting string is converted to lower case.
*
* @param path to unify
* @return unified path
*/
private static String unifyPath(String path) {
String result = PathUtils.sanitize(path);
if (result == null) {
result = "unknown";
}
return result.toLowerCase(Locale.getDefault());
}
/**
* Method that returns any parent {@code Element} for this Element, or null if the Element is unattached or is a root
* Element.
*
* The method first searches in {@code parentList}. If the own Element is found here, the next entry (parent) is
* returned. If nothing is found here, the method returns parent of Element.
*
* @param element to be searched for parent
* @return the containing Element or null if unattached or a root Element
*/
@CheckForNull
private Element getParentElement(@Nullable Element element) {
var parentIterator = parentList.iterator();
while (parentIterator.hasNext()) {
var next = parentIterator.next();
if (next.equals(element)) {
if (parentIterator.hasNext()) {
return parentIterator.next();
} else {
break;
}
}
}
return element != null ? element.getParentElement() : null;
}
/**
* Searches for Element associated with level.
*
* If level is an identifier, level element is searched for. Otherwise it is searched for a File element with path
* attribute level.
*
* @param level to search for
* @param defaultElement Element to return if no item was found
* @return found Element or defaultElement
*/
@CheckForNull
private Element findLevel(String level, @Nullable Element defaultElement) {
String xpath;
if (Verifier.checkElementName(level) == null) {
xpath = "/CompilationDatabase/" + level;
} else {
// handle special case 'UNITS empty' no need to search in tree
if (isUnitsEmpty()) {
return defaultElement;
}
xpath = "//" + UNITS + "/File[@path='" + unifyPath(level) + "']";
}
XPathExpression<Element> expr = xFactory.compile(xpath, Filters.element());
List<Element> elements = expr.evaluate(document);
if (!elements.isEmpty()) {
return elements.get(0);
}
return defaultElement;
}
/**
* Add or reuse an key Element.
*
* @param level for key
* @param key identifier of key
* @return existing or new Element for key
*/
private Element getKey(String level, String key) {
Element eLevel = findLevel(level, null);
if (eLevel == null) {
if (Verifier.checkElementName(level) == null) {
eLevel = new Element(level);
document.getRootElement().addContent(eLevel);
} else {
eLevel = new Element("File");
eLevel.setAttribute(new Attribute("path", unifyPath(level)));
parentList.getFirst().addContent(eLevel);
}
}
Element eKey = eLevel.getChild(key);
if (eKey == null) {
eKey = new Element(key);
eLevel.addContent(eKey);
}
return eKey;
}
/**
* Add a value to a key.
*
* @param key to add the value
* @param value to add
*/
static private void setValue(Element key, String value) {
var eValue = new Element(VALUE);
eValue.setText(value);
key.addContent(eValue);
}
}