Skip to content

Commit 499bce3

Browse files
committed
Revert "Revert "[analyzer] NFC: Separate PathDiagnosticConsumer options from AnalyzerOptions.""
This reverts commit 10f1ca9. (cherry picked from commit c599fc738a70e482976c6cc0ea31bef561641279)
1 parent fa5cb4b commit 499bce3

File tree

8 files changed

+164
-116
lines changed

8 files changed

+164
-116
lines changed

clang/include/clang/Analysis/PathDiagnostic.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,47 @@ namespace ento {
5858

5959
class PathDiagnostic;
6060

61+
/// These options tweak the behavior of path diangostic consumers.
62+
/// Most of these options are currently supported by very few consumers.
63+
struct PathDiagnosticConsumerOptions {
64+
/// Run-line of the tool that produced the diagnostic.
65+
/// It can be included with the diagnostic for debugging purposes.
66+
std::string ToolInvocation;
67+
68+
/// Whether to include additional information about macro expansions
69+
/// with the diagnostics, because otherwise they can be hard to obtain
70+
/// without re-compiling the program under analysis.
71+
bool ShouldDisplayMacroExpansions;
72+
73+
/// Whether to include LLVM statistics of the process in the diagnostic.
74+
/// Useful for profiling the tool on large real-world codebases.
75+
bool ShouldSerializeStats;
76+
77+
/// If the consumer intends to produce multiple output files, should it
78+
/// use randomly generated file names for these files (with the tiny risk of
79+
/// having random collisions) or deterministic human-readable file names
80+
/// (with a larger risk of deterministic collisions or invalid characters
81+
/// in the file name). We should not really give this choice to the users
82+
/// because deterministic mode is always superior when done right, but
83+
/// for some consumers this mode is experimental and needs to be
84+
/// off by default.
85+
bool ShouldWriteStableReportFilename;
86+
87+
/// Whether the consumer should treat consumed diagnostics as hard errors.
88+
/// Useful for breaking your build when issues are found.
89+
bool ShouldDisplayWarningsAsErrors;
90+
91+
/// Whether the consumer should attempt to rewrite the source file
92+
/// with fix-it hints attached to the diagnostics it consumes.
93+
bool ShouldApplyFixIts;
94+
95+
/// Whether the consumer should present the name of the entity that emitted
96+
/// the diagnostic (eg., a checker) so that the user knew how to disable it.
97+
bool ShouldDisplayDiagnosticName;
98+
99+
PathDiagnosticConsumerOptions() = delete;
100+
};
101+
61102
class PathDiagnosticConsumer {
62103
public:
63104
class PDFileEntry : public llvm::FoldingSetNode {

clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H
1515
#define LLVM_CLANG_STATICANALYZER_CORE_ANALYZEROPTIONS_H
1616

17+
#include "clang/Analysis/PathDiagnostic.h"
1718
#include "clang/Basic/LLVM.h"
1819
#include "llvm/ADT/IntrusiveRefCntPtr.h"
1920
#include "llvm/ADT/Optional.h"
@@ -255,7 +256,7 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
255256
unsigned NoRetryExhausted : 1;
256257

257258
/// Emit analyzer warnings as errors.
258-
unsigned AnalyzerWerror : 1;
259+
bool AnalyzerWerror : 1;
259260

260261
/// The inlining stack depth limit.
261262
// Cap the stack depth at 4 calls (5 stack frames, base + 4 calls).
@@ -390,6 +391,16 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
390391
///
391392
/// \sa CXXMemberInliningMode
392393
bool mayInlineCXXMemberFunction(CXXInlineableMemberKind K) const;
394+
395+
ento::PathDiagnosticConsumerOptions getDiagOpts() const {
396+
return {FullCompilerInvocation,
397+
ShouldDisplayMacroExpansions,
398+
ShouldSerializeStats,
399+
ShouldWriteStableReportFilename,
400+
AnalyzerWerror,
401+
ShouldApplyFixIts,
402+
ShouldDisplayCheckerNameForText};
403+
}
393404
};
394405

395406
using AnalyzerOptionsRef = IntrusiveRefCntPtr<AnalyzerOptions>;

clang/include/clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHDIAGNOSTICCONSUMERS_H
1414
#define LLVM_CLANG_STATICANALYZER_CORE_PATHDIAGNOSTICCONSUMERS_H
1515

16+
#include "clang/Analysis/PathDiagnostic.h"
17+
1618
#include <string>
1719
#include <vector>
1820

@@ -30,8 +32,9 @@ class PathDiagnosticConsumer;
3032
typedef std::vector<PathDiagnosticConsumer*> PathDiagnosticConsumers;
3133

3234
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \
33-
void CREATEFN(AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, \
34-
const std::string &Prefix, const Preprocessor &PP, \
35+
void CREATEFN(PathDiagnosticConsumerOptions Diagopts, \
36+
PathDiagnosticConsumers &C, const std::string &Prefix, \
37+
const Preprocessor &PP, \
3538
const cross_tu::CrossTranslationUnitContext &CTU);
3639
#include "clang/StaticAnalyzer/Core/Analyses.def"
3740

clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "clang/Lex/Token.h"
2424
#include "clang/Rewrite/Core/HTMLRewrite.h"
2525
#include "clang/Rewrite/Core/Rewriter.h"
26-
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
2726
#include "clang/StaticAnalyzer/Core/IssueHash.h"
2827
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
2928
#include "llvm/ADT/ArrayRef.h"
@@ -58,17 +57,18 @@ using namespace ento;
5857
namespace {
5958

6059
class HTMLDiagnostics : public PathDiagnosticConsumer {
60+
PathDiagnosticConsumerOptions DiagOpts;
6161
std::string Directory;
6262
bool createdDir = false;
6363
bool noDir = false;
6464
const Preprocessor &PP;
65-
AnalyzerOptions &AnalyzerOpts;
6665
const bool SupportsCrossFileDiagnostics;
6766

6867
public:
69-
HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string &OutputDir,
70-
const Preprocessor &pp, bool supportsMultipleFiles)
71-
: Directory(OutputDir), PP(pp), AnalyzerOpts(AnalyzerOpts),
68+
HTMLDiagnostics(PathDiagnosticConsumerOptions DiagOpts,
69+
const std::string &OutputDir, const Preprocessor &pp,
70+
bool supportsMultipleFiles)
71+
: DiagOpts(std::move(DiagOpts)), Directory(OutputDir), PP(pp),
7272
SupportsCrossFileDiagnostics(supportsMultipleFiles) {}
7373

7474
~HTMLDiagnostics() override { FlushDiagnostics(nullptr); }
@@ -133,7 +133,7 @@ class HTMLDiagnostics : public PathDiagnosticConsumer {
133133
} // namespace
134134

135135
void ento::createHTMLDiagnosticConsumer(
136-
AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
136+
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
137137
const std::string &OutputDir, const Preprocessor &PP,
138138
const cross_tu::CrossTranslationUnitContext &CTU) {
139139

@@ -142,37 +142,38 @@ void ento::createHTMLDiagnosticConsumer(
142142
// output mode. This doesn't make much sense, we should have the minimal text
143143
// as our default. In the case of backward compatibility concerns, this could
144144
// be preserved with -analyzer-config-compatibility-mode=true.
145-
createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, OutputDir, PP, CTU);
145+
createTextMinimalPathDiagnosticConsumer(DiagOpts, C, OutputDir, PP, CTU);
146146

147147
// TODO: Emit an error here.
148148
if (OutputDir.empty())
149149
return;
150150

151-
C.push_back(new HTMLDiagnostics(AnalyzerOpts, OutputDir, PP, true));
151+
C.push_back(new HTMLDiagnostics(std::move(DiagOpts), OutputDir, PP, true));
152152
}
153153

154154
void ento::createHTMLSingleFileDiagnosticConsumer(
155-
AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
155+
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
156156
const std::string &OutputDir, const Preprocessor &PP,
157157
const cross_tu::CrossTranslationUnitContext &CTU) {
158+
createTextMinimalPathDiagnosticConsumer(DiagOpts, C, OutputDir, PP, CTU);
158159

159160
// TODO: Emit an error here.
160161
if (OutputDir.empty())
161162
return;
162163

163-
C.push_back(new HTMLDiagnostics(AnalyzerOpts, OutputDir, PP, false));
164-
createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, OutputDir, PP, CTU);
164+
C.push_back(new HTMLDiagnostics(std::move(DiagOpts), OutputDir, PP, false));
165165
}
166166

167167
void ento::createPlistHTMLDiagnosticConsumer(
168-
AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
168+
PathDiagnosticConsumerOptions DiagOpts, PathDiagnosticConsumers &C,
169169
const std::string &prefix, const Preprocessor &PP,
170170
const cross_tu::CrossTranslationUnitContext &CTU) {
171171
createHTMLDiagnosticConsumer(
172-
AnalyzerOpts, C, std::string(llvm::sys::path::parent_path(prefix)), PP,
172+
DiagOpts, C, std::string(llvm::sys::path::parent_path(prefix)), PP,
173173
CTU);
174-
createPlistMultiFileDiagnosticConsumer(AnalyzerOpts, C, prefix, PP, CTU);
175-
createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, prefix, PP, CTU);
174+
createPlistMultiFileDiagnosticConsumer(DiagOpts, C, prefix, PP, CTU);
175+
createTextMinimalPathDiagnosticConsumer(std::move(DiagOpts), C, prefix, PP,
176+
CTU);
176177
}
177178

178179
//===----------------------------------------------------------------------===//
@@ -245,7 +246,7 @@ void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
245246
int FD;
246247
SmallString<128> Model, ResultPath;
247248

248-
if (!AnalyzerOpts.ShouldWriteStableReportFilename) {
249+
if (!DiagOpts.ShouldWriteStableReportFilename) {
249250
llvm::sys::path::append(Model, Directory, "report-%%%%%%.html");
250251
if (std::error_code EC =
251252
llvm::sys::fs::make_absolute(Model)) {
@@ -535,7 +536,7 @@ void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic& D, Rewriter &R,
535536
<input type="checkbox" class="spoilerhider" id="showinvocation" />
536537
<label for="showinvocation" >Show analyzer invocation</label>
537538
<div class="spoiler">clang -cc1 )<<<";
538-
os << html::EscapeText(AnalyzerOpts.FullCompilerInvocation);
539+
os << html::EscapeText(DiagOpts.ToolInvocation);
539540
os << R"<<<(
540541
</div>
541542
<div id='tooltiphint' hidden="true">

0 commit comments

Comments
 (0)