1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
//===- unittests/Analysis/FlowSensitive/RecordOpsTest.cpp -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "clang/Analysis/FlowSensitive/RecordOps.h"
#include "TestingSupport.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
namespace clang {
namespace dataflow {
namespace test {
namespace {
void runDataflow(
llvm::StringRef Code,
std::function<llvm::StringMap<QualType>(QualType)> SyntheticFieldCallback,
std::function<
void(const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &,
ASTContext &)>
VerifyResults) {
ASSERT_THAT_ERROR(checkDataflowWithNoopAnalysis(
Code, ast_matchers::hasName("target"), VerifyResults,
{BuiltinOptions()}, LangStandard::lang_cxx17,
SyntheticFieldCallback),
llvm::Succeeded());
}
const FieldDecl *getFieldNamed(RecordDecl *RD, llvm::StringRef Name) {
for (const FieldDecl *FD : RD->fields())
if (FD->getName() == Name)
return FD;
assert(false);
return nullptr;
}
TEST(RecordOpsTest, CopyRecord) {
std::string Code = R"(
struct S {
int outer_int;
int &ref;
struct {
int inner_int;
} inner;
};
void target(S s1, S s2) {
(void)s1.outer_int;
(void)s1.ref;
(void)s1.inner.inner_int;
// [[p]]
}
)";
runDataflow(
Code,
[](QualType Ty) -> llvm::StringMap<QualType> {
if (Ty.getAsString() != "S")
return {};
QualType IntTy =
getFieldNamed(Ty->getAsRecordDecl(), "outer_int")->getType();
return {{"synth_int", IntTy}};
},
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
Environment Env = getEnvironmentAtAnnotation(Results, "p").fork();
const ValueDecl *OuterIntDecl = findValueDecl(ASTCtx, "outer_int");
const ValueDecl *RefDecl = findValueDecl(ASTCtx, "ref");
const ValueDecl *InnerDecl = findValueDecl(ASTCtx, "inner");
const ValueDecl *InnerIntDecl = findValueDecl(ASTCtx, "inner_int");
auto &S1 = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s1");
auto &S2 = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s2");
auto &Inner1 = *cast<RecordStorageLocation>(S1.getChild(*InnerDecl));
auto &Inner2 = *cast<RecordStorageLocation>(S2.getChild(*InnerDecl));
EXPECT_NE(getFieldValue(&S1, *OuterIntDecl, Env),
getFieldValue(&S2, *OuterIntDecl, Env));
EXPECT_NE(S1.getChild(*RefDecl), S2.getChild(*RefDecl));
EXPECT_NE(getFieldValue(&Inner1, *InnerIntDecl, Env),
getFieldValue(&Inner2, *InnerIntDecl, Env));
EXPECT_NE(Env.getValue(S1.getSyntheticField("synth_int")),
Env.getValue(S2.getSyntheticField("synth_int")));
copyRecord(S1, S2, Env);
EXPECT_EQ(getFieldValue(&S1, *OuterIntDecl, Env),
getFieldValue(&S2, *OuterIntDecl, Env));
EXPECT_EQ(S1.getChild(*RefDecl), S2.getChild(*RefDecl));
EXPECT_EQ(getFieldValue(&Inner1, *InnerIntDecl, Env),
getFieldValue(&Inner2, *InnerIntDecl, Env));
EXPECT_EQ(Env.getValue(S1.getSyntheticField("synth_int")),
Env.getValue(S2.getSyntheticField("synth_int")));
});
}
TEST(RecordOpsTest, RecordsEqual) {
std::string Code = R"(
struct S {
int outer_int;
int &ref;
struct {
int inner_int;
} inner;
};
void target(S s1, S s2) {
(void)s1.outer_int;
(void)s1.ref;
(void)s1.inner.inner_int;
// [[p]]
}
)";
runDataflow(
Code,
[](QualType Ty) -> llvm::StringMap<QualType> {
if (Ty.getAsString() != "S")
return {};
QualType IntTy =
getFieldNamed(Ty->getAsRecordDecl(), "outer_int")->getType();
return {{"synth_int", IntTy}};
},
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
Environment Env = getEnvironmentAtAnnotation(Results, "p").fork();
const ValueDecl *OuterIntDecl = findValueDecl(ASTCtx, "outer_int");
const ValueDecl *RefDecl = findValueDecl(ASTCtx, "ref");
const ValueDecl *InnerDecl = findValueDecl(ASTCtx, "inner");
const ValueDecl *InnerIntDecl = findValueDecl(ASTCtx, "inner_int");
auto &S1 = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s1");
auto &S2 = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s2");
auto &Inner2 = *cast<RecordStorageLocation>(S2.getChild(*InnerDecl));
Env.setValue(S1.getSyntheticField("synth_int"),
Env.create<IntegerValue>());
// Strategy: Create two equal records, then verify each of the various
// ways in which records can differ causes recordsEqual to return false.
// changes we can make to the record.
// This test reuses the same objects for multiple checks, which isn't
// great, but seems better than duplicating the setup code for every
// check.
copyRecord(S1, S2, Env);
EXPECT_TRUE(recordsEqual(S1, S2, Env));
// S2 has a different outer_int.
Env.setValue(*S2.getChild(*OuterIntDecl), Env.create<IntegerValue>());
EXPECT_FALSE(recordsEqual(S1, S2, Env));
copyRecord(S1, S2, Env);
EXPECT_TRUE(recordsEqual(S1, S2, Env));
// S2 doesn't have outer_int at all.
Env.clearValue(*S2.getChild(*OuterIntDecl));
EXPECT_FALSE(recordsEqual(S1, S2, Env));
copyRecord(S1, S2, Env);
EXPECT_TRUE(recordsEqual(S1, S2, Env));
// S2 has a different ref.
S2.setChild(*RefDecl, &Env.createStorageLocation(
RefDecl->getType().getNonReferenceType()));
EXPECT_FALSE(recordsEqual(S1, S2, Env));
copyRecord(S1, S2, Env);
EXPECT_TRUE(recordsEqual(S1, S2, Env));
// S2 as a different inner_int.
Env.setValue(*Inner2.getChild(*InnerIntDecl),
Env.create<IntegerValue>());
EXPECT_FALSE(recordsEqual(S1, S2, Env));
copyRecord(S1, S2, Env);
EXPECT_TRUE(recordsEqual(S1, S2, Env));
// S2 has a different synth_int.
Env.setValue(S2.getSyntheticField("synth_int"),
Env.create<IntegerValue>());
EXPECT_FALSE(recordsEqual(S1, S2, Env));
copyRecord(S1, S2, Env);
EXPECT_TRUE(recordsEqual(S1, S2, Env));
// S2 doesn't have a value for synth_int.
Env.clearValue(S2.getSyntheticField("synth_int"));
EXPECT_FALSE(recordsEqual(S1, S2, Env));
copyRecord(S1, S2, Env);
EXPECT_TRUE(recordsEqual(S1, S2, Env));
});
}
TEST(TransferTest, CopyRecordBetweenDerivedAndBase) {
std::string Code = R"(
struct A {
int i;
};
struct B : public A {
};
void target(A a, B b) {
(void)a.i;
// [[p]]
}
)";
auto SyntheticFieldCallback = [](QualType Ty) -> llvm::StringMap<QualType> {
CXXRecordDecl *ADecl = nullptr;
if (Ty.getAsString() == "A")
ADecl = Ty->getAsCXXRecordDecl();
else if (Ty.getAsString() == "B")
ADecl = Ty->getAsCXXRecordDecl()
->bases_begin()
->getType()
->getAsCXXRecordDecl();
else
return {};
QualType IntTy = getFieldNamed(ADecl, "i")->getType();
return {{"synth_int", IntTy}};
};
// Test copying derived to base class.
runDataflow(
Code, SyntheticFieldCallback,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
Environment Env = getEnvironmentAtAnnotation(Results, "p").fork();
const ValueDecl *IDecl = findValueDecl(ASTCtx, "i");
auto &A = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "a");
auto &B = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "b");
EXPECT_NE(Env.getValue(*A.getChild(*IDecl)),
Env.getValue(*B.getChild(*IDecl)));
EXPECT_NE(Env.getValue(A.getSyntheticField("synth_int")),
Env.getValue(B.getSyntheticField("synth_int")));
copyRecord(B, A, Env);
EXPECT_EQ(Env.getValue(*A.getChild(*IDecl)),
Env.getValue(*B.getChild(*IDecl)));
EXPECT_EQ(Env.getValue(A.getSyntheticField("synth_int")),
Env.getValue(B.getSyntheticField("synth_int")));
});
// Test copying base to derived class.
runDataflow(
Code, SyntheticFieldCallback,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
Environment Env = getEnvironmentAtAnnotation(Results, "p").fork();
const ValueDecl *IDecl = findValueDecl(ASTCtx, "i");
auto &A = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "a");
auto &B = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "b");
EXPECT_NE(Env.getValue(*A.getChild(*IDecl)),
Env.getValue(*B.getChild(*IDecl)));
EXPECT_NE(Env.getValue(A.getSyntheticField("synth_int")),
Env.getValue(B.getSyntheticField("synth_int")));
copyRecord(A, B, Env);
EXPECT_EQ(Env.getValue(*A.getChild(*IDecl)),
Env.getValue(*B.getChild(*IDecl)));
EXPECT_EQ(Env.getValue(A.getSyntheticField("synth_int")),
Env.getValue(B.getSyntheticField("synth_int")));
});
}
} // namespace
} // namespace test
} // namespace dataflow
} // namespace clang
|