Debian Codegraph @

llvm-project/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp

raw
1 //===- unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp -------===//
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 #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
10 #include "TestingSupport.h"
11 #include "clang/AST/DeclCXX.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
14 #include "clang/Analysis/FlowSensitive/NoopAnalysis.h"
15 #include "clang/Analysis/FlowSensitive/Value.h"
16 #include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include <memory>
20
21 namespace {
22
23 using namespace clang;
24 using namespace dataflow;
25 using ::testing::ElementsAre;
26 using ::testing::NotNull;
27 using ::testing::Pair;
28
29 class EnvironmentTest : public ::testing::Test {
30 protected:
31 EnvironmentTest() : DAContext(std::make_unique<WatchedLiteralsSolver>()) {}
32
33 DataflowAnalysisContext DAContext;
34 };
35
36 TEST_F(EnvironmentTest, FlowCondition) {
37 Environment Env(DAContext);
38
39 EXPECT_TRUE(Env.flowConditionImplies(Env.getBoolLiteralValue(true)));
40 EXPECT_FALSE(Env.flowConditionImplies(Env.getBoolLiteralValue(false)));
41
42 auto &X = Env.makeAtomicBoolValue();
43 EXPECT_FALSE(Env.flowConditionImplies(X));
44
45 Env.addToFlowCondition(X);
46 EXPECT_TRUE(Env.flowConditionImplies(X));
47
48 auto &NotX = Env.makeNot(X);
49 EXPECT_FALSE(Env.flowConditionImplies(NotX));
50 }
51
52 TEST_F(EnvironmentTest, CreateValueRecursiveType) {
53 using namespace ast_matchers;
54
55 std::string Code = R"cc(
56 struct Recursive {
57 bool X;
58 Recursive *R;
59 };
60 // Use both fields to force them to be created with `createValue`.
61 void Usage(Recursive R) { (void)R.X; (void)R.R; }
62 )cc";
63
64 auto Unit =
65 tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++11"});
66 auto &Context = Unit->getASTContext();
67
68 ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U);
69
70 auto Results =
71 match(qualType(hasDeclaration(recordDecl(
72 hasName("Recursive"),
73 has(fieldDecl(hasName("R")).bind("field-r")))))
74 .bind("target"),
75 Context);
76 const QualType *TyPtr = selectFirst<QualType>("target", Results);
77 ASSERT_THAT(TyPtr, NotNull());
78 QualType Ty = *TyPtr;
79 ASSERT_FALSE(Ty.isNull());
80
81 const FieldDecl *R = selectFirst<FieldDecl>("field-r", Results);
82 ASSERT_THAT(R, NotNull());
83
84 Results = match(functionDecl(hasName("Usage")).bind("fun"), Context);
85 const auto *Fun = selectFirst<FunctionDecl>("fun", Results);
86 ASSERT_THAT(Fun, NotNull());
87
88 // Verify that the struct and the field (`R`) with first appearance of the
89 // type is created successfully.
90 Environment Env(DAContext, *Fun);
91 Value *Val = Env.createValue(Ty);
92 ASSERT_NE(Val, nullptr);
93 StructValue *SVal = clang::dyn_cast<StructValue>(Val);
94 ASSERT_NE(SVal, nullptr);
95 Val = SVal->getChild(*R);
96 ASSERT_NE(Val, nullptr);
97 PointerValue *PV = clang::dyn_cast<PointerValue>(Val);
98 EXPECT_NE(PV, nullptr);
99 }
100
101 TEST_F(EnvironmentTest, InitGlobalVarsFun) {
102 using namespace ast_matchers;
103
104 std::string Code = R"cc(
105 int Global = 0;
106 int Target () { return Global; }
107 )cc";
108
109 auto Unit =
110 tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++11"});
111 auto &Context = Unit->getASTContext();
112
113 ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U);
114
115 auto Results =
116 match(decl(anyOf(varDecl(hasName("Global")).bind("global"),
117 functionDecl(hasName("Target")).bind("target"))),
118 Context);
119 const auto *Fun = selectFirst<FunctionDecl>("target", Results);
120 const auto *Var = selectFirst<VarDecl>("global", Results);
121 ASSERT_THAT(Fun, NotNull());
122 ASSERT_THAT(Var, NotNull());
123
124 // Verify the global variable is populated when we analyze `Target`.
125 Environment Env(DAContext, *Fun);
126 EXPECT_THAT(Env.getValue(*Var, SkipPast::None), NotNull());
127 }
128
129 TEST_F(EnvironmentTest, InitGlobalVarsFieldFun) {
130 using namespace ast_matchers;
131
132 std::string Code = R"cc(
133 struct S { int Bar; };
134 S Global = {0};
135 int Target () { return Global.Bar; }
136 )cc";
137
138 auto Unit =
139 tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++11"});
140 auto &Context = Unit->getASTContext();
141
142 ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U);
143
144 auto Results =
145 match(decl(anyOf(varDecl(hasName("Global")).bind("global"),
146 functionDecl(hasName("Target")).bind("target"))),
147 Context);
148 const auto *Fun = selectFirst<FunctionDecl>("target", Results);
149 const auto *GlobalDecl = selectFirst<VarDecl>("global", Results);
150 ASSERT_THAT(Fun, NotNull());
151 ASSERT_THAT(GlobalDecl, NotNull());
152
153 ASSERT_TRUE(GlobalDecl->getType()->isStructureType());
154 auto GlobalFields = GlobalDecl->getType()->getAsRecordDecl()->fields();
155
156 FieldDecl *BarDecl = nullptr;
157 for (FieldDecl *Field : GlobalFields) {
158 if (Field->getNameAsString() == "Bar") {
159 BarDecl = Field;
160 break;
161 }
162 FAIL() << "Unexpected field: " << Field->getNameAsString();
163 }
164 ASSERT_THAT(BarDecl, NotNull());
165
166 // Verify the global variable is populated when we analyze `Target`.
167 Environment Env(DAContext, *Fun);
168 const auto *GlobalLoc = cast<AggregateStorageLocation>(
169 Env.getStorageLocation(*GlobalDecl, SkipPast::None));
170 const auto *GlobalVal = cast<StructValue>(Env.getValue(*GlobalLoc));
171 const auto *BarVal = GlobalVal->getChild(*BarDecl);
172 ASSERT_THAT(BarVal, NotNull());
173 EXPECT_TRUE(isa<IntegerValue>(BarVal));
174 }
175
176 TEST_F(EnvironmentTest, InitGlobalVarsConstructor) {
177 using namespace ast_matchers;
178
179 std::string Code = R"cc(
180 int Global = 0;
181 struct Target {
182 Target() : Field(Global) {}
183 int Field;
184 };
185 )cc";
186
187 auto Unit =
188 tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++11"});
189 auto &Context = Unit->getASTContext();
190
191 ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U);
192
193 auto Results =
194 match(decl(anyOf(
195 varDecl(hasName("Global")).bind("global"),
196 cxxConstructorDecl(ofClass(hasName("Target"))).bind("target"))),
197 Context);
198 const auto *Ctor = selectFirst<CXXConstructorDecl>("target", Results);
199 const auto *Var = selectFirst<VarDecl>("global", Results);
200 ASSERT_TRUE(Ctor != nullptr);
201 ASSERT_THAT(Var, NotNull());
202
203 // Verify the global variable is populated when we analyze `Target`.
204 Environment Env(DAContext, *Ctor);
205 EXPECT_THAT(Env.getValue(*Var, SkipPast::None), NotNull());
206 }
207
208 } // namespace