Skip to content

Commit a6a02f6

Browse files
committed
csgrep --mode=sarif: use SARIF output format
The writer is not yet implemented, only the output format switch. Closes: #39
1 parent 3149111 commit a6a02f6

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

src/abstract-parser.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ enum EFileFormat {
3232
FF_COVERITY, ///< what cov-format-errors produces
3333
FF_GCC, ///< GCC format
3434
FF_JSON, ///< JSON format
35-
FF_HTML ///< HTML format (output only)
35+
FF_HTML, ///< HTML format (output only)
36+
FF_SARIF ///< SARIF format (used by GitHub)
3637
};
3738

3839
// abstract class with a factory method

src/abstract-writer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ AbstractWriter* createWriter(
104104
writer = new HtmlWriter(strDst, emp, emp, spPlacement);
105105
break;
106106
}
107+
108+
case FF_SARIF:
109+
writer = new JsonWriter(strDst, FF_SARIF);
110+
break;
107111
}
108112

109113
if (!scanProps.empty())

src/csgrep.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,11 @@ class WriterFactory {
415415
}
416416

417417
static AbstractWriter* createJson() {
418-
return new JsonWriter(std::cout);
418+
return new JsonWriter(std::cout, FF_JSON);
419+
}
420+
421+
static AbstractWriter* createSarif() {
422+
return new JsonWriter(std::cout, FF_SARIF);
419423
}
420424

421425
static AbstractWriter* createKeyEventPrinter() {
@@ -435,6 +439,7 @@ class WriterFactory {
435439
tbl_["grep"] = createGrep;
436440
tbl_["grouped"] = createGrouped;
437441
tbl_["json"] = createJson;
442+
tbl_["sarif"] = createSarif;
438443
tbl_["stat"] = createStat;
439444
}
440445

@@ -593,7 +598,7 @@ int main(int argc, char *argv[])
593598
("quiet,q", "do not report any parsing errors")
594599

595600
("mode", po::value<string>(&mode)
596-
->default_value("grep"), "grep, json, evtstat, files, filestat, grouped, stat, or dig_key_events")
601+
->default_value("grep"), "grep, json, evtstat, files, filestat, grouped, sarif, stat, or dig_key_events")
597602

598603
("help", "print the usage of csgrep")
599604
("version", "print the version of csgrep");

src/json-writer.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,16 @@ struct JsonWriter::Private {
4141
}
4242
};
4343

44-
JsonWriter::JsonWriter(std::ostream &str):
44+
JsonWriter::JsonWriter(std::ostream &str, const EFileFormat format):
4545
d(new Private(str))
4646
{
47+
switch (format) {
48+
case FF_JSON:
49+
break;
50+
51+
default:
52+
throw std::runtime_error("unknown output format");
53+
}
4754
}
4855

4956
JsonWriter::~JsonWriter()

src/json-writer.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
class JsonWriter: public AbstractWriter {
2828
public:
29-
JsonWriter(std::ostream &);
29+
JsonWriter(std::ostream &, EFileFormat format = FF_JSON);
3030
virtual ~JsonWriter();
3131

3232
virtual const TScanProps& getScanProps() const;

0 commit comments

Comments
 (0)