Skip to content

Commit ceb9b62

Browse files
committed
csgrep --set-scan-prop: override the specified scan property
Fixes: #82 Closes: #85
1 parent c944725 commit ceb9b62

5 files changed

+188
-0
lines changed

src/csgrep.cc

+54
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,54 @@ class DropScanProps: public GenericAbstractFilter {
388388
const TScanProps emp_;
389389
};
390390

391+
class ScanPropSetter: public GenericAbstractFilter {
392+
public:
393+
ScanPropSetter(AbstractWriter *agent, const TStringList &propList);
394+
395+
/// override specified scan properties
396+
void setScanProps(const TScanProps &origProps) override;
397+
398+
private:
399+
// key/val pairs are stored in a vector
400+
using TItem = std::pair<std::string, std::string>;
401+
using TList = std::vector<TItem>;
402+
TList itemList_;
403+
};
404+
405+
ScanPropSetter::ScanPropSetter(
406+
AbstractWriter *agent,
407+
const TStringList &propList):
408+
GenericAbstractFilter(agent)
409+
{
410+
// iterate over the given NAME:VALUE strings
411+
for (const std::string &str : propList) {
412+
// split the string by the first occurrence of ':'
413+
size_t ddAt = str.find(':');
414+
if (std::string::npos == ddAt) {
415+
const auto msg = "missing ':' in " + str;
416+
throw std::runtime_error(msg);
417+
}
418+
419+
// store the key/val pair into the vector
420+
itemList_.emplace_back(
421+
/* key */ str.substr(0, ddAt),
422+
/* val */ str.substr(ddAt + 1));
423+
}
424+
}
425+
426+
void ScanPropSetter::setScanProps(const TScanProps &origProps)
427+
{
428+
// we need to copy the given map
429+
TScanProps props = origProps;
430+
431+
// apply specified changes
432+
for (const auto &item : itemList_)
433+
props[item./* key */first] = item./* val */second;
434+
435+
// forward the result
436+
agent_->setScanProps(props);
437+
}
438+
391439
class DuplicateFilter: public AbstractFilter {
392440
public:
393441
DuplicateFilter(AbstractWriter *agent):
@@ -632,6 +680,7 @@ int main(int argc, char *argv[])
632680
("embed-context,U", po::value<int>(), "embed a number of lines of context from the source file for the key event")
633681
("prune-events", po::value<int>(), "event is preserved if its verbosity level is below the given number")
634682
("remove-duplicates,u", "remove defects that are not unique by their key event")
683+
("set-scan-prop", po::value<TStringList>(), "NAME:VALUE pair to override the specified scan property")
635684
("strip-path-prefix", po::value<string>(), "string prefix to strip from path (applied after all filters)")
636685

637686
("ignore-case,i", "ignore case when matching regular expressions")
@@ -712,6 +761,11 @@ int main(int argc, char *argv[])
712761
"strip-path-prefix"))
713762
return 1;
714763

764+
// insert ScanPropSetter into the chain if requested
765+
if (!chainDecoratorGeneric<ScanPropSetter, TStringList>(&eng, vm,
766+
"set-scan-prop"))
767+
return 1;
768+
715769
// chain all filters
716770
if (!chainFilters(&eng, vm))
717771
// an error message already printed out
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--mode=sarif --set-scan-prop=tool:shellcheck --set-scan-prop=tool-version:2.1 --set-scan-prop=tool-url:https://www.shellcheck.net/
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./contrib/guide/get_started/00-cleanup.sh:6:1: warning: Use 'cd ... || exit' or 'cd ... || return' in case cd fails. [SC2164]
2+
./contrib/guide/get_started/12-datadir-create.sh:4:7: warning: When used with -p, -m only applies to the deepest directory. [SC2174]
+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
3+
"version": "",
4+
"inlineExternalProperties": [
5+
{
6+
"externalizedProperties": {
7+
"tool": "shellcheck",
8+
"tool-url": "https://www.shellcheck.net/",
9+
"tool-version": "2.1"
10+
}
11+
}
12+
],
13+
"runs": [
14+
{
15+
"tool": {
16+
"driver": {
17+
"name": "shellcheck",
18+
"version": "",
19+
"informationUri": "https://www.shellcheck.net/"
20+
}
21+
},
22+
"results": [
23+
{
24+
"ruleId": "SHELLCHECK_WARNING: warning[SC2164]",
25+
"level": "warning",
26+
"locations": [
27+
{
28+
"id": 0,
29+
"physicalLocation": {
30+
"artifactLocation": {
31+
"uri": "./contrib/guide/get_started/00-cleanup.sh"
32+
},
33+
"region": {
34+
"startLine": 6,
35+
"startColumn": 1
36+
}
37+
}
38+
}
39+
],
40+
"message": {
41+
"text": "Use 'cd ... || exit' or 'cd ... || return' in case cd fails."
42+
},
43+
"codeFlows": [
44+
{
45+
"threadFlows": [
46+
{
47+
"locations": [
48+
{
49+
"location": {
50+
"id": 0,
51+
"physicalLocation": {
52+
"artifactLocation": {
53+
"uri": "./contrib/guide/get_started/00-cleanup.sh"
54+
},
55+
"region": {
56+
"startLine": 6,
57+
"startColumn": 1
58+
}
59+
},
60+
"message": {
61+
"text": "Use 'cd ... || exit' or 'cd ... || return' in case cd fails."
62+
}
63+
},
64+
"nestingLevel": 0,
65+
"kinds": [
66+
"warning[SC2164]"
67+
]
68+
}
69+
]
70+
}
71+
]
72+
}
73+
]
74+
},
75+
{
76+
"ruleId": "SHELLCHECK_WARNING: warning[SC2174]",
77+
"level": "warning",
78+
"locations": [
79+
{
80+
"id": 0,
81+
"physicalLocation": {
82+
"artifactLocation": {
83+
"uri": "./contrib/guide/get_started/12-datadir-create.sh"
84+
},
85+
"region": {
86+
"startLine": 4,
87+
"startColumn": 7
88+
}
89+
}
90+
}
91+
],
92+
"message": {
93+
"text": "When used with -p, -m only applies to the deepest directory."
94+
},
95+
"codeFlows": [
96+
{
97+
"threadFlows": [
98+
{
99+
"locations": [
100+
{
101+
"location": {
102+
"id": 0,
103+
"physicalLocation": {
104+
"artifactLocation": {
105+
"uri": "./contrib/guide/get_started/12-datadir-create.sh"
106+
},
107+
"region": {
108+
"startLine": 4,
109+
"startColumn": 7
110+
}
111+
},
112+
"message": {
113+
"text": "When used with -p, -m only applies to the deepest directory."
114+
}
115+
},
116+
"nestingLevel": 0,
117+
"kinds": [
118+
"warning[SC2174]"
119+
]
120+
}
121+
]
122+
}
123+
]
124+
}
125+
]
126+
}
127+
]
128+
}
129+
]
130+
}

tests/csgrep/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ test_csgrep("93-csgrep-tool-predicate" )
140140
test_csgrep("94-gcc-json" )
141141
test_csgrep("95-gcc-sarif" )
142142
test_csgrep("96-sarif-levels" )
143+
test_csgrep("97-sarif-set-tool" )

0 commit comments

Comments
 (0)