comparison clang/unittests/StaticAnalyzer/Reusables.h @ 223:5f17cb93ff66 llvm-original

LLVM13 (2021/7/18)
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:43:00 +0900
parents 0572611fdcc8
children
comparison
equal deleted inserted replaced
222:81f6424ef0e3 223:5f17cb93ff66
8 8
9 #ifndef LLVM_CLANG_UNITTESTS_STATICANALYZER_REUSABLES_H 9 #ifndef LLVM_CLANG_UNITTESTS_STATICANALYZER_REUSABLES_H
10 #define LLVM_CLANG_UNITTESTS_STATICANALYZER_REUSABLES_H 10 #define LLVM_CLANG_UNITTESTS_STATICANALYZER_REUSABLES_H
11 11
12 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/CrossTU/CrossTranslationUnit.h"
13 #include "clang/Frontend/CompilerInstance.h" 14 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/CrossTU/CrossTranslationUnit.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 15 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
16 #include "gtest/gtest.h"
16 17
17 namespace clang { 18 namespace clang {
18 namespace ento { 19 namespace ento {
19 20
20 // Find a node in the current AST that matches a matcher. 21 // Find a node in the current AST that matches a matcher.
64 *C.getAnalyzerOpts()), 65 *C.getAnalyzerOpts()),
65 VisitedCallees(), FS(), 66 VisitedCallees(), FS(),
66 Eng(CTU, AMgr, &VisitedCallees, &FS, ExprEngine::Inline_Regular) {} 67 Eng(CTU, AMgr, &VisitedCallees, &FS, ExprEngine::Inline_Regular) {}
67 }; 68 };
68 69
70 struct ExpectedLocationTy {
71 unsigned Line;
72 unsigned Column;
73
74 void testEquality(SourceLocation L, SourceManager &SM) const {
75 EXPECT_EQ(SM.getSpellingLineNumber(L), Line);
76 EXPECT_EQ(SM.getSpellingColumnNumber(L), Column);
77 }
78 };
79
80 struct ExpectedRangeTy {
81 ExpectedLocationTy Begin;
82 ExpectedLocationTy End;
83
84 void testEquality(SourceRange R, SourceManager &SM) const {
85 Begin.testEquality(R.getBegin(), SM);
86 End.testEquality(R.getEnd(), SM);
87 }
88 };
89
90 struct ExpectedPieceTy {
91 ExpectedLocationTy Loc;
92 std::string Text;
93 std::vector<ExpectedRangeTy> Ranges;
94
95 void testEquality(const PathDiagnosticPiece &Piece, SourceManager &SM) {
96 Loc.testEquality(Piece.getLocation().asLocation(), SM);
97 EXPECT_EQ(Piece.getString(), Text);
98 EXPECT_EQ(Ranges.size(), Piece.getRanges().size());
99 for (const auto &RangeItem : llvm::enumerate(Piece.getRanges()))
100 Ranges[RangeItem.index()].testEquality(RangeItem.value(), SM);
101 }
102 };
103
104 struct ExpectedDiagTy {
105 ExpectedLocationTy Loc;
106 std::string VerboseDescription;
107 std::string ShortDescription;
108 std::string CheckerName;
109 std::string BugType;
110 std::string Category;
111 std::vector<ExpectedPieceTy> Path;
112
113 void testEquality(const PathDiagnostic &Diag, SourceManager &SM) {
114 Loc.testEquality(Diag.getLocation().asLocation(), SM);
115 EXPECT_EQ(Diag.getVerboseDescription(), VerboseDescription);
116 EXPECT_EQ(Diag.getShortDescription(), ShortDescription);
117 EXPECT_EQ(Diag.getCheckerName(), CheckerName);
118 EXPECT_EQ(Diag.getBugType(), BugType);
119 EXPECT_EQ(Diag.getCategory(), Category);
120
121 EXPECT_EQ(Path.size(), Diag.path.size());
122 for (const auto &PieceItem : llvm::enumerate(Diag.path)) {
123 if (PieceItem.index() < Path.size())
124 Path[PieceItem.index()].testEquality(*PieceItem.value(), SM);
125 }
126 }
127 };
128
129 using ExpectedDiagsTy = std::vector<ExpectedDiagTy>;
130
131 // A consumer to verify the generated diagnostics.
132 class VerifyPathDiagnosticConsumer : public PathDiagnosticConsumer {
133 ExpectedDiagsTy ExpectedDiags;
134 SourceManager &SM;
135
136 public:
137 VerifyPathDiagnosticConsumer(ExpectedDiagsTy &&ExpectedDiags,
138 SourceManager &SM)
139 : ExpectedDiags(ExpectedDiags), SM(SM) {}
140
141 StringRef getName() const override { return "verify test diagnostics"; }
142
143 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
144 FilesMade *filesMade) override {
145 EXPECT_EQ(Diags.size(), ExpectedDiags.size());
146 for (const auto &Item : llvm::enumerate(Diags))
147 if (Item.index() < ExpectedDiags.size())
148 ExpectedDiags[Item.index()].testEquality(*Item.value(), SM);
149 }
150 };
151
69 } // namespace ento 152 } // namespace ento
70 } // namespace clang 153 } // namespace clang
71 154
72 #endif 155 #endif