Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//===----------------------------------------------------------------------===//

#include "PlistDiagnostics.h"
#include "SarifDiagnostics.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/Stmt.h"
Expand Down Expand Up @@ -169,6 +171,21 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const ArrowMap &Indices) {

} // namespace

/// Creates and registers an HTML diagnostic consumer, without any additional
/// text consumer.
static void createHTMLDiagnosticConsumerImpl(
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
const std::string &OutputDir, const Preprocessor &PP,
bool SupportMultipleFiles) {

// TODO: Emit an error here.
if (OutputDir.empty())
return;

C.emplace_back(std::make_unique<HTMLDiagnostics>(
std::move(DiagOpts), OutputDir, PP, SupportMultipleFiles));
}

void ento::createHTMLDiagnosticConsumer(
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
const std::string &OutputDir, const Preprocessor &PP,
Expand All @@ -183,12 +200,8 @@ void ento::createHTMLDiagnosticConsumer(
createTextMinimalPathDiagnosticConsumer(DiagOpts, C, OutputDir, PP, CTU,
MacroExpansions);

// TODO: Emit an error here.
if (OutputDir.empty())
return;

C.emplace_back(std::make_unique<HTMLDiagnostics>(std::move(DiagOpts),
OutputDir, PP, true));
createHTMLDiagnosticConsumerImpl(DiagOpts, C, OutputDir, PP,
/*SupportMultipleFiles=*/true);
}

void ento::createHTMLSingleFileDiagnosticConsumer(
Expand All @@ -199,24 +212,19 @@ void ento::createHTMLSingleFileDiagnosticConsumer(
createTextMinimalPathDiagnosticConsumer(DiagOpts, C, OutputDir, PP, CTU,
MacroExpansions);

// TODO: Emit an error here.
if (OutputDir.empty())
return;

C.emplace_back(std::make_unique<HTMLDiagnostics>(std::move(DiagOpts),
OutputDir, PP, false));
createHTMLDiagnosticConsumerImpl(DiagOpts, C, OutputDir, PP,
/*SupportMultipleFiles=*/false);
}

void ento::createPlistHTMLDiagnosticConsumer(
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
const std::string &prefix, const Preprocessor &PP,
const cross_tu::CrossTranslationUnitContext &CTU,
const MacroExpansionContext &MacroExpansions) {
createHTMLDiagnosticConsumer(
DiagOpts, C, std::string(llvm::sys::path::parent_path(prefix)), PP, CTU,
MacroExpansions);
createPlistMultiFileDiagnosticConsumer(DiagOpts, C, prefix, PP, CTU,
MacroExpansions);
createHTMLDiagnosticConsumerImpl(
DiagOpts, C, std::string(llvm::sys::path::parent_path(prefix)), PP, true);
createPlistDiagnosticConsumerImpl(DiagOpts, C, prefix, PP, CTU,
MacroExpansions, true);
createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, prefix, PP,
CTU, MacroExpansions);
}
Expand All @@ -226,11 +234,11 @@ void ento::createSarifHTMLDiagnosticConsumer(
const std::string &sarif_file, const Preprocessor &PP,
const cross_tu::CrossTranslationUnitContext &CTU,
const MacroExpansionContext &MacroExpansions) {
createHTMLDiagnosticConsumer(
createHTMLDiagnosticConsumerImpl(
DiagOpts, C, std::string(llvm::sys::path::parent_path(sarif_file)), PP,
CTU, MacroExpansions);
createSarifDiagnosticConsumer(DiagOpts, C, sarif_file, PP, CTU,
MacroExpansions);
true);
createSarifDiagnosticConsumerImpl(DiagOpts, C, sarif_file, PP);

createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, sarif_file,
PP, CTU, MacroExpansions);
}
Expand Down
30 changes: 20 additions & 10 deletions clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "PlistDiagnostics.h"
#include "clang/Analysis/IssueHash.h"
#include "clang/Analysis/MacroExpansionContext.h"
#include "clang/Analysis/PathDiagnostic.h"
Expand Down Expand Up @@ -528,19 +529,31 @@ PlistDiagnostics::PlistDiagnostics(
(void)this->CTU;
}

void ento::createPlistDiagnosticConsumer(
/// Creates and registers a Plist diagnostic consumer, without any additional
/// text consumer.
void ento::createPlistDiagnosticConsumerImpl(
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
const std::string &OutputFile, const Preprocessor &PP,
const cross_tu::CrossTranslationUnitContext &CTU,
const MacroExpansionContext &MacroExpansions) {
const MacroExpansionContext &MacroExpansions, bool SupportsMultipleFiles) {

// TODO: Emit an error here.
if (OutputFile.empty())
return;

C.push_back(std::make_unique<PlistDiagnostics>(
DiagOpts, OutputFile, PP, CTU, MacroExpansions,
/*supportsMultipleFiles=*/false));
DiagOpts, OutputFile, PP, CTU, MacroExpansions, SupportsMultipleFiles));
}

void ento::createPlistDiagnosticConsumer(
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
const std::string &OutputFile, const Preprocessor &PP,
const cross_tu::CrossTranslationUnitContext &CTU,
const MacroExpansionContext &MacroExpansions) {

createPlistDiagnosticConsumerImpl(DiagOpts, C, OutputFile, PP, CTU,
MacroExpansions,
/*SupportsMultipleFiles=*/false);
createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, OutputFile,
PP, CTU, MacroExpansions);
}
Expand All @@ -551,13 +564,10 @@ void ento::createPlistMultiFileDiagnosticConsumer(
const cross_tu::CrossTranslationUnitContext &CTU,
const MacroExpansionContext &MacroExpansions) {

// TODO: Emit an error here.
if (OutputFile.empty())
return;
createPlistDiagnosticConsumerImpl(DiagOpts, C, OutputFile, PP, CTU,
MacroExpansions,
/*SupportsMultipleFiles=*/true);

C.push_back(std::make_unique<PlistDiagnostics>(
DiagOpts, OutputFile, PP, CTU, MacroExpansions,
/*supportsMultipleFiles=*/true));
createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, OutputFile,
PP, CTU, MacroExpansions);
}
Expand Down
27 changes: 27 additions & 0 deletions clang/lib/StaticAnalyzer/Core/PlistDiagnostics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//==- PlistDiagnostics.h - Plist Diagnostics for Paths -------------*- C++ -*-//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_STATICANALYZER_CORE_PLISTDIAGNOSTICS_H
#define LLVM_CLANG_LIB_STATICANALYZER_CORE_PLISTDIAGNOSTICS_H

#include "clang/CrossTU/CrossTranslationUnit.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
#include <string>

namespace clang::ento {

void createPlistDiagnosticConsumerImpl(
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
const std::string &Output, const Preprocessor &PP,
const cross_tu::CrossTranslationUnitContext &CTU,
const MacroExpansionContext &MacroExpansions, bool SupportsMultipleFiles);

} // namespace clang::ento

#endif
15 changes: 13 additions & 2 deletions clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "SarifDiagnostics.h"
#include "clang/Analysis/MacroExpansionContext.h"
#include "clang/Analysis/PathDiagnostic.h"
#include "clang/Basic/Sarif.h"
Expand Down Expand Up @@ -54,14 +55,24 @@ void ento::createSarifDiagnosticConsumer(
const cross_tu::CrossTranslationUnitContext &CTU,
const MacroExpansionContext &MacroExpansions) {

createSarifDiagnosticConsumerImpl(DiagOpts, C, Output, PP);

createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, Output, PP,
CTU, MacroExpansions);
}

/// Creates and registers a SARIF diagnostic consumer, without any additional
/// text consumer.
void ento::createSarifDiagnosticConsumerImpl(
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
const std::string &Output, const Preprocessor &PP) {

// TODO: Emit an error here.
if (Output.empty())
return;

C.push_back(std::make_unique<SarifDiagnostics>(Output, PP.getLangOpts(),
PP.getSourceManager()));
createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, Output, PP,
CTU, MacroExpansions);
}

static StringRef getRuleDescription(StringRef CheckName) {
Expand Down
25 changes: 25 additions & 0 deletions clang/lib/StaticAnalyzer/Core/SarifDiagnostics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//==- SarifDiagnostics.h - SARIF Diagnostics for Paths -------------*- C++ -*-//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_STATICANALYZER_CORE_SARIFDIAGNOSTICS_H
#define LLVM_CLANG_LIB_STATICANALYZER_CORE_SARIFDIAGNOSTICS_H

#include "clang/Lex/Preprocessor.h"
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
#include <string>

namespace clang::ento {

void createSarifDiagnosticConsumerImpl(PathDiagnosticConsumerOptions DiagOpts,
PathDiagnosticConsumers &C,
const std::string &Output,
const Preprocessor &PP);

} // namespace clang::ento

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"artifacts": [
{
"length": 1071,
"length": 1152,
"location": {
"index": 0,
},
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Analysis/diagnostics/sarif-multi-diagnostic-test.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=core,optin.taint,debug.TaintTest,unix.Malloc %s -verify -analyzer-output=sarif -o - | %normalize_sarif | diff -U1 -b %S/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif -
// RUN: rm -rf %t && mkdir %t && %clang_analyze_cc1 -analyzer-checker=core,optin.taint,debug.TaintTest,unix.Malloc %s -verify -analyzer-output=sarif-html -o %t%{fs-sep}out.sarif
// RUN: cat %t%{fs-sep}out.sarif | %normalize_sarif | diff -U1 -b %S/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif -
#include "../Inputs/system-header-simulator.h"
#include "../Inputs/system-header-simulator-for-malloc.h"
#define ERR -1

int atoi(const char *nptr);

void f(void) {
Expand Down