Skip to content

Commit ff2c53c

Browse files
committed
gcc-parser: assign GCC_ANALYZER_WARNING checker
... for reports produced by `gcc -fanalyzer` Closes: https://github.com/kdudka/csdiff/pull/12
1 parent e9b53e4 commit ff2c53c

File tree

5 files changed

+1684
-0
lines changed

5 files changed

+1684
-0
lines changed

gcc-parser.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,56 @@ bool BasicGccParser::hasError() const {
541541
return hasError_;
542542
}
543543

544+
class PostProcessor {
545+
public:
546+
PostProcessor():
547+
reGccAnalCoreEvt_("^(.*) \\[-Wanalyzer-[A-Za-z0-9-]+\\]$"),
548+
reGccAnalCwe_("^.* \\[CWE-([0-9]+)\\]$")
549+
{
550+
}
551+
552+
/// apply final transformations on a successfully parsed defect
553+
void apply(Defect *pDef);
554+
555+
private:
556+
const boost::regex reGccAnalCoreEvt_;
557+
const boost::regex reGccAnalCwe_;
558+
559+
void transGccAnal(Defect *pDef);
560+
};
561+
562+
void PostProcessor::transGccAnal(Defect *pDef) {
563+
if ("COMPILER_WARNING" != pDef->checker)
564+
return;
565+
566+
// check for [-Wanalyzer-...] suffix in message of the key event
567+
DefEvent &keyEvt = pDef->events[pDef->keyEventIdx];
568+
boost::smatch sm;
569+
if (!boost::regex_match(keyEvt.msg, sm, reGccAnalCoreEvt_))
570+
return;
571+
572+
// COMPILER_WARNING -> GCC_ANALYZER_WARNING
573+
pDef->checker = "GCC_ANALYZER_WARNING";
574+
575+
// pick CWE number if available
576+
const std::string rawMsg = sm[/* msg */ 1];
577+
if (boost::regex_match(rawMsg, sm, reGccAnalCwe_)) {
578+
try {
579+
pDef->cwe = boost::lexical_cast<int>(sm[/* cwe */ 1]);
580+
}
581+
catch (boost::bad_lexical_cast &) {
582+
pDef->cwe = 0;
583+
}
584+
}
585+
}
586+
587+
void PostProcessor::apply(Defect *pDef) {
588+
this->transGccAnal(pDef);
589+
}
590+
544591
struct GccParser::Private {
545592
BasicGccParser core;
593+
PostProcessor postProc;
546594
Defect lastDef;
547595
const boost::regex reLocation;
548596

@@ -640,6 +688,9 @@ bool GccParser::getNext(Defect *pDef) {
640688
for (unsigned idx = 0U; idx < evtCount; ++idx)
641689
evtList[idx].verbosityLevel = (keyEventIdx != idx);
642690

691+
// apply final transformations
692+
d->postProc.apply(pDef);
693+
643694
return true;
644695
}
645696

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ test_csgrep(csgrep "53-csparser-note-events" )
173173
test_csgrep(csgrep "54-csparser-unknown-lineno" )
174174
test_csgrep(csgrep "55-csparser-pylint-copr" )
175175
test_csgrep(csgrep "56-gcc-sparser-clang-error" )
176+
test_csgrep(csgrep "57-gcc-parser-gcc-analyzer-curl")
176177
test_csparser(csparser-5.8 00)
177178
test_csparser(csparser-5.8 01)
178179
test_csparser(csparser-5.8 02)

tests/csgrep/57-gcc-parser-gcc-analyzer-curl-args.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)