Skip to content

Commit d2ddc69

Browse files
committed
Revert "Revert "[analyzer] NFC: Move path diagnostic consumer implementations to libAnalysis.""
This reverts commit 5663bf2. The cyclic dependency problem is addressed now. This is the ~fifth attempt to land this change.
1 parent c3529a5 commit d2ddc69

18 files changed

+299
-178
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===- CrossTUAnalysisHelper.h - Abstraction layer for CTU ------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#ifndef LLVM_CLANG_ANALYSIS_CROSS_TU_HELPER_H
9+
#define LLVM_CLANG_ANALYSIS_CROSS_TU_HELPER_H
10+
11+
#include "llvm/ADT/Optional.h"
12+
#include "clang/Basic/SourceManager.h"
13+
14+
namespace clang {
15+
16+
class ASTUnit;
17+
18+
/// This class is an abstract interface acting as a bridge between
19+
/// an analysis that requires lookups across translation units (a user
20+
/// of that interface) and the facility that implements such lookups
21+
/// (an implementation of that interface). This is useful to break direct
22+
/// link-time dependencies between the (possibly shared) libraries in which
23+
/// the user and the implementation live.
24+
class CrossTUAnalysisHelper {
25+
public:
26+
/// Determine the original source location in the original TU for an
27+
/// imported source location.
28+
/// \p ToLoc Source location in the imported-to AST.
29+
/// \return Source location in the imported-from AST and the corresponding
30+
/// ASTUnit object (the AST was loaded from a file using an internal ASTUnit
31+
/// object that is returned here).
32+
/// If any error happens (ToLoc is a non-imported source location) empty is
33+
/// returned.
34+
virtual llvm::Optional<std::pair<SourceLocation /*FromLoc*/, Preprocessor *>>
35+
getImportedFromSourceLocationWithPreprocessor(SourceLocation ToLoc) const = 0;
36+
37+
virtual ~CrossTUAnalysisHelper() {}
38+
};
39+
} // namespace clang
40+
41+
#endif // LLVM_CLANG_ANALYSIS_CROSS_TU_HELPER_H
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- PathDiagnosticConsumers.def - Visualizing warnings ------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines the set of path diagnostic consumers - objects that
10+
// implement different representations of static analysis results.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef ANALYSIS_DIAGNOSTICS
15+
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN)
16+
#endif
17+
18+
ANALYSIS_DIAGNOSTICS(HTML, "html", "Output analysis results using HTML",
19+
createHTMLDiagnosticConsumer)
20+
21+
ANALYSIS_DIAGNOSTICS(
22+
HTML_SINGLE_FILE, "html-single-file",
23+
"Output analysis results using HTML (not allowing for multi-file bugs)",
24+
createHTMLSingleFileDiagnosticConsumer)
25+
26+
ANALYSIS_DIAGNOSTICS(PLIST, "plist", "Output analysis results using Plists",
27+
createPlistDiagnosticConsumer)
28+
29+
ANALYSIS_DIAGNOSTICS(
30+
PLIST_MULTI_FILE, "plist-multi-file",
31+
"Output analysis results using Plists (allowing for multi-file bugs)",
32+
createPlistMultiFileDiagnosticConsumer)
33+
34+
ANALYSIS_DIAGNOSTICS(PLIST_HTML, "plist-html",
35+
"Output analysis results using HTML wrapped with Plists",
36+
createPlistHTMLDiagnosticConsumer)
37+
38+
ANALYSIS_DIAGNOSTICS(SARIF, "sarif", "Output analysis results in a SARIF file",
39+
createSarifDiagnosticConsumer)
40+
41+
ANALYSIS_DIAGNOSTICS(TEXT, "text", "Text output of analysis results to stderr",
42+
createTextPathDiagnosticConsumer)
43+
44+
ANALYSIS_DIAGNOSTICS(TEXT_MINIMAL, "text-minimal",
45+
"Emits minimal diagnostics to stderr, stating only the "
46+
"warning message and the associated notes. Usually "
47+
"used in addition to other analysis types",
48+
createTextMinimalPathDiagnosticConsumer)
49+
50+
#undef ANALYSIS_DIAGNOSTICS

clang/include/clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h renamed to clang/include/clang/Analysis/PathDiagnosticConsumers.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,24 @@
1818
#include <string>
1919
#include <vector>
2020

21+
#include "clang/Analysis/PathDiagnostic.h"
22+
2123
namespace clang {
2224

2325
class AnalyzerOptions;
2426
class Preprocessor;
25-
namespace cross_tu {
26-
class CrossTranslationUnitContext;
27-
}
27+
class CrossTUAnalysisHelper;
2828

2929
namespace ento {
3030

3131
class PathDiagnosticConsumer;
32-
typedef std::vector<PathDiagnosticConsumer*> PathDiagnosticConsumers;
32+
typedef std::vector<PathDiagnosticConsumer *> PathDiagnosticConsumers;
3333

3434
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \
3535
void CREATEFN(PathDiagnosticConsumerOptions Diagopts, \
3636
PathDiagnosticConsumers &C, const std::string &Prefix, \
37-
const Preprocessor &PP, \
38-
const cross_tu::CrossTranslationUnitContext &CTU);
39-
#include "clang/StaticAnalyzer/Core/Analyses.def"
37+
const Preprocessor &PP, const CrossTUAnalysisHelper &CTU);
38+
#include "clang/Analysis/PathDiagnosticConsumers.def"
4039

4140
} // end 'ento' namespace
4241
} // end 'clang' namespace

clang/include/clang/CrossTU/CrossTranslationUnit.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
1515
#define LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H
1616

17+
#include "clang/Analysis/CrossTUAnalysisHelper.h"
1718
#include "clang/AST/ASTImporterSharedState.h"
1819
#include "clang/Basic/LLVM.h"
1920
#include "llvm/ADT/DenseMap.h"
@@ -120,10 +121,10 @@ bool containsConst(const VarDecl *VD, const ASTContext &ACtx);
120121
/// the locations of the AST files for each definition.
121122
///
122123
/// Note that this class also implements caching.
123-
class CrossTranslationUnitContext {
124+
class CrossTranslationUnitContext : public CrossTUAnalysisHelper {
124125
public:
125126
CrossTranslationUnitContext(CompilerInstance &CI);
126-
~CrossTranslationUnitContext();
127+
~CrossTranslationUnitContext() override;
127128

128129
/// This function loads a function or variable definition from an
129130
/// external AST file and merges it into the original AST.
@@ -186,12 +187,24 @@ class CrossTranslationUnitContext {
186187
/// imported source location.
187188
/// \p ToLoc Source location in the imported-to AST.
188189
/// \return Source location in the imported-from AST and the corresponding
189-
/// ASTUnit object (the AST was loaded from a file using an internal ASTUnit
190+
/// ASTUnit object (the AST was loaded from a file using an internal ASTUni
190191
/// object that is returned here).
191192
/// If any error happens (ToLoc is a non-imported source location) empty is
192193
/// returned.
193194
llvm::Optional<std::pair<SourceLocation /*FromLoc*/, ASTUnit *>>
194-
getImportedFromSourceLocation(const clang::SourceLocation &ToLoc) const;
195+
getImportedFromSourceLocation(SourceLocation ToLoc) const;
196+
197+
/// Determine the original source location in the original TU for an
198+
/// imported source location.
199+
/// \p ToLoc Source location in the imported-to AST.
200+
/// \return Source location in the imported-from AST and the Preprocessor
201+
/// corresponding to the AST unit that originally contained the imported-from
202+
/// source location.
203+
/// If any error happens (ToLoc is a non-imported source location) empty is
204+
/// returned.
205+
llvm::Optional<std::pair<SourceLocation /*FromLoc*/, Preprocessor *>>
206+
getImportedFromSourceLocationWithPreprocessor(
207+
SourceLocation ToLoc) const override;
195208

196209
private:
197210
using ImportedFileIDMap =

clang/include/clang/StaticAnalyzer/Core/Analyses.def

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,6 @@ ANALYSIS_CONSTRAINTS(RangeConstraints, "range",
2828
ANALYSIS_CONSTRAINTS(Z3Constraints, "z3", "Use Z3 contraint solver",
2929
CreateZ3ConstraintManager)
3030

31-
#ifndef ANALYSIS_DIAGNOSTICS
32-
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN)
33-
#endif
34-
35-
ANALYSIS_DIAGNOSTICS(HTML, "html", "Output analysis results using HTML",
36-
createHTMLDiagnosticConsumer)
37-
38-
ANALYSIS_DIAGNOSTICS(
39-
HTML_SINGLE_FILE, "html-single-file",
40-
"Output analysis results using HTML (not allowing for multi-file bugs)",
41-
createHTMLSingleFileDiagnosticConsumer)
42-
43-
ANALYSIS_DIAGNOSTICS(PLIST, "plist", "Output analysis results using Plists",
44-
createPlistDiagnosticConsumer)
45-
46-
ANALYSIS_DIAGNOSTICS(
47-
PLIST_MULTI_FILE, "plist-multi-file",
48-
"Output analysis results using Plists (allowing for multi-file bugs)",
49-
createPlistMultiFileDiagnosticConsumer)
50-
51-
ANALYSIS_DIAGNOSTICS(PLIST_HTML, "plist-html",
52-
"Output analysis results using HTML wrapped with Plists",
53-
createPlistHTMLDiagnosticConsumer)
54-
55-
ANALYSIS_DIAGNOSTICS(SARIF, "sarif", "Output analysis results in a SARIF file",
56-
createSarifDiagnosticConsumer)
57-
58-
ANALYSIS_DIAGNOSTICS(TEXT, "text", "Text output of analysis results to stderr",
59-
createTextPathDiagnosticConsumer)
60-
61-
ANALYSIS_DIAGNOSTICS(TEXT_MINIMAL, "text-minimal",
62-
"Emits minimal diagnostics to stderr, stating only the "
63-
"warning message and the associated notes. Usually "
64-
"used in addition to other analysis types",
65-
createTextMinimalPathDiagnosticConsumer)
66-
6731
#ifndef ANALYSIS_PURGE
6832
#define ANALYSIS_PURGE(NAME, CMDFLAG, DESC)
6933
#endif
@@ -91,7 +55,6 @@ ANALYSIS_INLINING_MODE(
9155

9256
#undef ANALYSIS_STORE
9357
#undef ANALYSIS_CONSTRAINTS
94-
#undef ANALYSIS_DIAGNOSTICS
9558
#undef ANALYSIS_PURGE
9659
#undef ANALYSIS_INLINING_MODE
9760
#undef ANALYSIS_IPA

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ NumConstraints
5858
/// analysis results.
5959
enum AnalysisDiagClients {
6060
#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN) PD_##NAME,
61-
#include "clang/StaticAnalyzer/Core/Analyses.def"
61+
#include "clang/Analysis/PathDiagnosticConsumers.def"
6262
PD_NONE,
6363
NUM_ANALYSIS_DIAG_CLIENTS
6464
};

clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#include "clang/Analysis/AnalysisDeclContext.h"
1818
#include "clang/Analysis/PathDiagnostic.h"
19+
#include "clang/Analysis/PathDiagnosticConsumers.h"
1920
#include "clang/Lex/Preprocessor.h"
2021
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
2122
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
22-
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
2323

2424
namespace clang {
2525

clang/include/clang/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Clang_Analysis {
33
umbrella "Analysis"
44

55
textual header "Analysis/Analyses/ThreadSafetyOps.def"
6+
textual header "Analysis/PathDiagnosticConsumers.def"
67

78
module * { export * }
89

clang/lib/Analysis/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ add_clang_library(clangAnalysis
1818
CodeInjector.cpp
1919
Dominators.cpp
2020
ExprMutationAnalyzer.cpp
21+
HTMLPathDiagnosticConsumer.cpp
2122
IssueHash.cpp
2223
LiveVariables.cpp
2324
ObjCNoReturn.cpp
2425
PathDiagnostic.cpp
26+
PlistPathDiagnosticConsumer.cpp
27+
PlistHTMLPathDiagnosticConsumer.cpp
2528
PostOrderCFGView.cpp
2629
ProgramPoint.cpp
2730
ReachableCode.cpp
2831
RetainSummaryManager.cpp
32+
SarifPathDiagnosticConsumer.cpp
33+
TextPathDiagnosticConsumer.cpp
2934
ThreadSafety.cpp
3035
ThreadSafetyCommon.cpp
3136
ThreadSafetyLogical.cpp
@@ -37,6 +42,8 @@ add_clang_library(clangAnalysis
3742
clangASTMatchers
3843
clangBasic
3944
clangLex
45+
clangRewrite
46+
clangToolingCore
4047

4148
DEPENDS
4249
omp_gen

0 commit comments

Comments
 (0)