| 1 |
//===- unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.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 "TestingSupport.h" |
| 10 |
#include "clang/AST/Decl.h" |
| 11 |
#include "clang/AST/ExprCXX.h" |
| 12 |
#include "clang/AST/OperationKinds.h" |
| 13 |
#include "clang/AST/Type.h" |
| 14 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 15 |
#include "clang/ASTMatchers/ASTMatchers.h" |
| 16 |
#include "clang/Analysis/CFG.h" |
| 17 |
#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h" |
| 18 |
#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h" |
| 19 |
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" |
| 20 |
#include "clang/Analysis/FlowSensitive/DataflowLattice.h" |
| 21 |
#include "clang/Analysis/FlowSensitive/DebugSupport.h" |
| 22 |
#include "clang/Analysis/FlowSensitive/NoopAnalysis.h" |
| 23 |
#include "clang/Analysis/FlowSensitive/Value.h" |
| 24 |
#include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h" |
| 25 |
#include "clang/Tooling/Tooling.h" |
| 26 |
#include "llvm/ADT/STLExtras.h" |
| 27 |
#include "llvm/ADT/SmallSet.h" |
| 28 |
#include "llvm/ADT/StringMap.h" |
| 29 |
#include "llvm/ADT/StringRef.h" |
| 30 |
#include "llvm/Support/Error.h" |
| 31 |
#include "llvm/Testing/ADT/StringMapEntry.h" |
| 32 |
#include "llvm/Testing/Support/Error.h" |
| 33 |
#include "gmock/gmock.h" |
| 34 |
#include "gtest/gtest.h" |
| 35 |
#include <cassert> |
| 36 |
#include <memory> |
| 37 |
#include <optional> |
| 38 |
#include <ostream> |
| 39 |
#include <string> |
| 40 |
#include <utility> |
| 41 |
#include <vector> |
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
using namespace clang; |
| 46 |
using namespace dataflow; |
| 47 |
using namespace test; |
| 48 |
using namespace ast_matchers; |
| 49 |
using llvm::IsStringMapEntry; |
| 50 |
using ::testing::DescribeMatcher; |
| 51 |
using ::testing::IsEmpty; |
| 52 |
using ::testing::NotNull; |
| 53 |
using ::testing::Test; |
| 54 |
using ::testing::UnorderedElementsAre; |
| 55 |
|
| 56 |
class DataflowAnalysisTest : public Test { |
| 57 |
protected: |
| 58 |
template <typename AnalysisT> |
| 59 |
llvm::Expected<std::vector< |
| 60 |
std::optional<DataflowAnalysisState<typename AnalysisT::Lattice>>>> |
| 61 |
runAnalysis(llvm::StringRef Code, AnalysisT (*MakeAnalysis)(ASTContext &)) { |
| 62 |
AST = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++11"}); |
| 63 |
|
| 64 |
auto *Func = selectFirst<FunctionDecl>( |
| 65 |
"func", |
| 66 |
match(functionDecl(ast_matchers::hasName("target")).bind("func"), |
| 67 |
AST->getASTContext())); |
| 68 |
assert(Func != nullptr); |
| 69 |
|
| 70 |
ACFG = |
| 71 |
std::make_unique<AdornedCFG>(llvm::cantFail(AdornedCFG::build(*Func))); |
| 72 |
|
| 73 |
AnalysisT Analysis = MakeAnalysis(AST->getASTContext()); |
| 74 |
DACtx = std::make_unique<DataflowAnalysisContext>( |
| 75 |
std::make_unique<WatchedLiteralsSolver>()); |
| 76 |
Environment Env(*DACtx, *Func); |
| 77 |
|
| 78 |
return runDataflowAnalysis(*ACFG, Analysis, Env); |
| 79 |
} |
| 80 |
|
| 81 |
/// Returns the `CFGBlock` containing `S` (and asserts that it exists). |
| 82 |
const CFGBlock *blockForStmt(const Stmt &S) { |
| 83 |
const CFGBlock *Block = ACFG->blockForStmt(S); |
| 84 |
assert(Block != nullptr); |
| 85 |
return Block; |
| 86 |
} |
| 87 |
|
| 88 |
template <typename StateT> |
| 89 |
const StateT & |
| 90 |
blockStateForStmt(const std::vector<std::optional<StateT>> &BlockStates, |
| 91 |
const Stmt &S) { |
| 92 |
const std::optional<StateT> &MaybeState = |
| 93 |
BlockStates[blockForStmt(S)->getBlockID()]; |
| 94 |
assert(MaybeState.has_value()); |
| 95 |
return *MaybeState; |
| 96 |
} |
| 97 |
|
| 98 |
/// Returns the first node that matches `Matcher` (and asserts that the match |
| 99 |
/// was successful, i.e. the returned node is not null). |
| 100 |
template <typename NodeT, typename MatcherT> |
| 101 |
const NodeT &matchNode(MatcherT Matcher) { |
| 102 |
const auto *Node = selectFirst<NodeT>( |
| 103 |
"node", match(Matcher.bind("node"), AST->getASTContext())); |
| 104 |
assert(Node != nullptr); |
| 105 |
return *Node; |
| 106 |
} |
| 107 |
|
| 108 |
std::unique_ptr<ASTUnit> AST; |
| 109 |
std::unique_ptr<AdornedCFG> ACFG; |
| 110 |
std::unique_ptr<DataflowAnalysisContext> DACtx; |
| 111 |
}; |
| 112 |
|
| 113 |
TEST_F(DataflowAnalysisTest, NoopAnalysis) { |
| 114 |
auto BlockStates = llvm::cantFail( |
| 115 |
runAnalysis<NoopAnalysis>("void target() {}", [](ASTContext &C) { |
| 116 |
return NoopAnalysis(C, |
| 117 |
// Don't use builtin transfer function. |
| 118 |
DataflowAnalysisOptions{std::nullopt}); |
| 119 |
})); |
| 120 |
EXPECT_EQ(BlockStates.size(), 2u); |
| 121 |
EXPECT_TRUE(BlockStates[0].has_value()); |
| 122 |
EXPECT_TRUE(BlockStates[1].has_value()); |
| 123 |
} |
| 124 |
|
| 125 |
// Basic test that `diagnoseFunction` calls the Diagnoser function for the |
| 126 |
// number of elements expected. |
| 127 |
TEST_F(DataflowAnalysisTest, DiagnoseFunctionDiagnoserCalledOnEachElement) { |
| 128 |
std::string Code = R"(void target() { int x = 0; ++x; })"; |
| 129 |
std::unique_ptr<ASTUnit> AST = |
| 130 |
tooling::buildASTFromCodeWithArgs(Code, {"-std=c++11"}); |
| 131 |
|
| 132 |
auto *Func = |
| 133 |
cast<FunctionDecl>(findValueDecl(AST->getASTContext(), "target")); |
| 134 |
auto Diagnoser = [](const CFGElement &Elt, ASTContext &, |
| 135 |
const TransferStateForDiagnostics<NoopLattice> &) { |
| 136 |
llvm::SmallVector<std::string> Diagnostics(1); |
| 137 |
llvm::raw_string_ostream OS(Diagnostics.front()); |
| 138 |
Elt.dumpToStream(OS); |
| 139 |
return Diagnostics; |
| 140 |
}; |
| 141 |
auto Result = diagnoseFunction<NoopAnalysis, std::string>( |
| 142 |
*Func, AST->getASTContext(), Diagnoser); |
| 143 |
// `diagnoseFunction` provides no guarantees about the order in which elements |
| 144 |
// are visited, so we use `UnorderedElementsAre`. |
| 145 |
EXPECT_THAT_EXPECTED(Result, llvm::HasValue(UnorderedElementsAre( |
| 146 |
"0\n", "int x = 0;\n", "x\n", "++x\n", |
| 147 |
" (Lifetime ends)\n"))); |
| 148 |
} |
| 149 |
|
| 150 |
TEST_F(DataflowAnalysisTest, CanAnalyzeStmt) { |
| 151 |
std::string Code = R"cc( |
| 152 |
struct S { bool b; }; |
| 153 |
void foo() { |
| 154 |
S AnS = S{true}; |
| 155 |
} |
| 156 |
)cc"; |
| 157 |
AST = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++11"}); |
| 158 |
const auto &DeclStatement = |
| 159 |
matchNode<DeclStmt>(declStmt(hasSingleDecl(varDecl(hasName("AnS"))))); |
| 160 |
const auto &Func = matchNode<FunctionDecl>(functionDecl(hasName("foo"))); |
| 161 |
|
| 162 |
ACFG = std::make_unique<AdornedCFG>(llvm::cantFail(AdornedCFG::build( |
| 163 |
Func, const_cast<DeclStmt &>(DeclStatement), AST->getASTContext()))); |
| 164 |
|
| 165 |
NoopAnalysis Analysis = NoopAnalysis(AST->getASTContext()); |
| 166 |
DACtx = std::make_unique<DataflowAnalysisContext>( |
| 167 |
std::make_unique<WatchedLiteralsSolver>()); |
| 168 |
Environment Env(*DACtx, const_cast<DeclStmt &>(DeclStatement)); |
| 169 |
|
| 170 |
llvm::Expected<std::vector<std::optional<DataflowAnalysisState<NoopLattice>>>> |
| 171 |
Results = runDataflowAnalysis(*ACFG, Analysis, Env); |
| 172 |
|
| 173 |
ASSERT_THAT_ERROR(Results.takeError(), llvm::Succeeded()); |
| 174 |
const Environment &ExitBlockEnv = Results->front()->Env; |
| 175 |
BoolValue *BoolFieldValue = cast<BoolValue>( |
| 176 |
getFieldValue(ExitBlockEnv.get<RecordStorageLocation>( |
| 177 |
*cast<VarDecl>((*DeclStatement.decl_begin()))), |
| 178 |
"b", AST->getASTContext(), ExitBlockEnv)); |
| 179 |
EXPECT_TRUE(Env.proves(BoolFieldValue->formula())); |
| 180 |
} |
| 181 |
|
| 182 |
// Tests for the statement-to-block map. |
| 183 |
using StmtToBlockTest = DataflowAnalysisTest; |
| 184 |
|
| 185 |
TEST_F(StmtToBlockTest, ConditionalOperator) { |
| 186 |
std::string Code = R"( |
| 187 |
void target(bool b) { |
| 188 |
int i = b ? 1 : 0; |
| 189 |
} |
| 190 |
)"; |
| 191 |
ASSERT_THAT_ERROR(runAnalysis<NoopAnalysis>( |
| 192 |
Code, [](ASTContext &C) { return NoopAnalysis(C); }) |
| 193 |
.takeError(), |
| 194 |
llvm::Succeeded()); |
| 195 |
|
| 196 |
const auto &IDecl = matchNode<DeclStmt>(declStmt(has(varDecl(hasName("i"))))); |
| 197 |
const auto &ConditionalOp = |
| 198 |
matchNode<ConditionalOperator>(conditionalOperator()); |
| 199 |
|
| 200 |
// The conditional operator should be associated with the same block as the |
| 201 |
// `DeclStmt` for `i`. (Specifically, the conditional operator should not be |
| 202 |
// associated with the block for which it is the terminator.) |
| 203 |
EXPECT_EQ(blockForStmt(IDecl), blockForStmt(ConditionalOp)); |
| 204 |
} |
| 205 |
|
| 206 |
TEST_F(StmtToBlockTest, LogicalAnd) { |
| 207 |
std::string Code = R"( |
| 208 |
void target(bool b1, bool b2) { |
| 209 |
bool b = b1 && b2; |
| 210 |
} |
| 211 |
)"; |
| 212 |
ASSERT_THAT_ERROR(runAnalysis<NoopAnalysis>( |
| 213 |
Code, [](ASTContext &C) { return NoopAnalysis(C); }) |
| 214 |
.takeError(), |
| 215 |
llvm::Succeeded()); |
| 216 |
|
| 217 |
const auto &BDecl = matchNode<DeclStmt>(declStmt(has(varDecl(hasName("b"))))); |
| 218 |
const auto &AndOp = |
| 219 |
matchNode<BinaryOperator>(binaryOperator(hasOperatorName("&&"))); |
| 220 |
|
| 221 |
// The `&&` operator should be associated with the same block as the |
| 222 |
// `DeclStmt` for `b`. (Specifically, the `&&` operator should not be |
| 223 |
// associated with the block for which it is the terminator.) |
| 224 |
EXPECT_EQ(blockForStmt(BDecl), blockForStmt(AndOp)); |
| 225 |
} |
| 226 |
|
| 227 |
TEST_F(StmtToBlockTest, IfStatementWithLogicalAnd) { |
| 228 |
std::string Code = R"( |
| 229 |
void target(bool b1, bool b2) { |
| 230 |
if (b1 && b2) |
| 231 |
; |
| 232 |
} |
| 233 |
)"; |
| 234 |
ASSERT_THAT_ERROR(runAnalysis<NoopAnalysis>( |
| 235 |
Code, [](ASTContext &C) { return NoopAnalysis(C); }) |
| 236 |
.takeError(), |
| 237 |
llvm::Succeeded()); |
| 238 |
|
| 239 |
const auto &If = matchNode<IfStmt>(ifStmt()); |
| 240 |
const auto &B2 = |
| 241 |
matchNode<DeclRefExpr>(declRefExpr(to(varDecl(hasName("b2"))))); |
| 242 |
const auto &AndOp = |
| 243 |
matchNode<BinaryOperator>(binaryOperator(hasOperatorName("&&"))); |
| 244 |
|
| 245 |
// The if statement is the terminator for the block that contains both `b2` |
| 246 |
// and the `&&` operator (which appears only as a terminator condition, not |
| 247 |
// as a regular `CFGElement`). |
| 248 |
const CFGBlock *IfBlock = blockForStmt(If); |
| 249 |
const CFGBlock *B2Block = blockForStmt(B2); |
| 250 |
const CFGBlock *AndOpBlock = blockForStmt(AndOp); |
| 251 |
EXPECT_EQ(IfBlock, B2Block); |
| 252 |
EXPECT_EQ(IfBlock, AndOpBlock); |
| 253 |
} |
| 254 |
|
| 255 |
// Tests that check we discard state for expressions correctly. |
| 256 |
using DiscardExprStateTest = DataflowAnalysisTest; |
| 257 |
|
| 258 |
TEST_F(DiscardExprStateTest, WhileStatement) { |
| 259 |
std::string Code = R"( |
| 260 |
void foo(int *p); |
| 261 |
void target(int *p) { |
| 262 |
while (p != nullptr) |
| 263 |
foo(p); |
| 264 |
} |
| 265 |
)"; |
| 266 |
auto BlockStates = llvm::cantFail(runAnalysis<NoopAnalysis>( |
| 267 |
Code, [](ASTContext &C) { return NoopAnalysis(C); })); |
| 268 |
|
| 269 |
const auto &NotEqOp = |
| 270 |
matchNode<BinaryOperator>(binaryOperator(hasOperatorName("!="))); |
| 271 |
const auto &CallFoo = |
| 272 |
matchNode<CallExpr>(callExpr(callee(functionDecl(hasName("foo"))))); |
| 273 |
|
| 274 |
// In the block that evaluates the expression `p != nullptr`, this expression |
| 275 |
// is associated with a value. |
| 276 |
const auto &NotEqOpState = blockStateForStmt(BlockStates, NotEqOp); |
| 277 |
EXPECT_NE(NotEqOpState.Env.getValue(NotEqOp), nullptr); |
| 278 |
|
| 279 |
// In the block that calls `foo(p)`, the value for `p != nullptr` is discarded |
| 280 |
// because it is not consumed outside the block it is in. |
| 281 |
const auto &CallFooState = blockStateForStmt(BlockStates, CallFoo); |
| 282 |
EXPECT_EQ(CallFooState.Env.getValue(NotEqOp), nullptr); |
| 283 |
} |
| 284 |
|
| 285 |
TEST_F(DiscardExprStateTest, BooleanOperator) { |
| 286 |
std::string Code = R"( |
| 287 |
void f(); |
| 288 |
void target(bool b1, bool b2) { |
| 289 |
if (b1 && b2) |
| 290 |
f(); |
| 291 |
} |
| 292 |
)"; |
| 293 |
auto BlockStates = llvm::cantFail(runAnalysis<NoopAnalysis>( |
| 294 |
Code, [](ASTContext &C) { return NoopAnalysis(C); })); |
| 295 |
|
| 296 |
const auto &AndOp = |
| 297 |
matchNode<BinaryOperator>(binaryOperator(hasOperatorName("&&"))); |
| 298 |
const auto &CallF = |
| 299 |
matchNode<CallExpr>(callExpr(callee(functionDecl(hasName("f"))))); |
| 300 |
|
| 301 |
// In the block that evaluates the LHS of the `&&` operator, the LHS is |
| 302 |
// associated with a value, while the right-hand side is not (unsurprisingly, |
| 303 |
// as it hasn't been evaluated yet). |
| 304 |
const auto &LHSState = blockStateForStmt(BlockStates, *AndOp.getLHS()); |
| 305 |
auto *LHSValue = cast<BoolValue>(LHSState.Env.getValue(*AndOp.getLHS())); |
| 306 |
EXPECT_NE(LHSValue, nullptr); |
| 307 |
EXPECT_EQ(LHSState.Env.getValue(*AndOp.getRHS()), nullptr); |
| 308 |
|
| 309 |
// In the block that evaluates the RHS, both the LHS and RHS are associated |
| 310 |
// with values, as they are both subexpressions of the `&&` operator, which |
| 311 |
// is evaluated in a later block. |
| 312 |
const auto &RHSState = blockStateForStmt(BlockStates, *AndOp.getRHS()); |
| 313 |
EXPECT_EQ(RHSState.Env.getValue(*AndOp.getLHS()), LHSValue); |
| 314 |
auto *RHSValue = RHSState.Env.get<BoolValue>(*AndOp.getRHS()); |
| 315 |
EXPECT_NE(RHSValue, nullptr); |
| 316 |
|
| 317 |
// In the block that evaluates `b1 && b2`, the `&&` as well as its operands |
| 318 |
// are associated with values. |
| 319 |
const auto &AndOpState = blockStateForStmt(BlockStates, AndOp); |
| 320 |
EXPECT_EQ(AndOpState.Env.getValue(*AndOp.getLHS()), LHSValue); |
| 321 |
EXPECT_EQ(AndOpState.Env.getValue(*AndOp.getRHS()), RHSValue); |
| 322 |
EXPECT_EQ(AndOpState.Env.getValue(AndOp), |
| 323 |
&AndOpState.Env.makeAnd(*LHSValue, *RHSValue)); |
| 324 |
|
| 325 |
// In the block that calls `f()`, none of `b1`, `b2`, or `b1 && b2` should be |
| 326 |
// associated with values. |
| 327 |
const auto &CallFState = blockStateForStmt(BlockStates, CallF); |
| 328 |
EXPECT_EQ(CallFState.Env.getValue(*AndOp.getLHS()), nullptr); |
| 329 |
EXPECT_EQ(CallFState.Env.getValue(*AndOp.getRHS()), nullptr); |
| 330 |
EXPECT_EQ(CallFState.Env.getValue(AndOp), nullptr); |
| 331 |
} |
| 332 |
|
| 333 |
TEST_F(DiscardExprStateTest, ConditionalOperator) { |
| 334 |
std::string Code = R"( |
| 335 |
void f(int*, int); |
| 336 |
void g(); |
| 337 |
bool cond(); |
| 338 |
|
| 339 |
void target() { |
| 340 |
int i = 0; |
| 341 |
if (cond()) |
| 342 |
f(&i, cond() ? 1 : 0); |
| 343 |
g(); |
| 344 |
} |
| 345 |
)"; |
| 346 |
auto BlockStates = llvm::cantFail(runAnalysis<NoopAnalysis>( |
| 347 |
Code, [](ASTContext &C) { return NoopAnalysis(C); })); |
| 348 |
|
| 349 |
const auto &AddrOfI = |
| 350 |
matchNode<UnaryOperator>(unaryOperator(hasOperatorName("&"))); |
| 351 |
const auto &CallF = |
| 352 |
matchNode<CallExpr>(callExpr(callee(functionDecl(hasName("f"))))); |
| 353 |
const auto &CallG = |
| 354 |
matchNode<CallExpr>(callExpr(callee(functionDecl(hasName("g"))))); |
| 355 |
|
| 356 |
// In the block that evaluates `&i`, it should obviously have a value. |
| 357 |
const auto &AddrOfIState = blockStateForStmt(BlockStates, AddrOfI); |
| 358 |
auto *AddrOfIVal = AddrOfIState.Env.get<PointerValue>(AddrOfI); |
| 359 |
EXPECT_NE(AddrOfIVal, nullptr); |
| 360 |
|
| 361 |
// Because of the conditional operator, the `f(...)` call is evaluated in a |
| 362 |
// different block than `&i`, but `&i` still needs to have a value here |
| 363 |
// because it's a subexpression of the call. |
| 364 |
const auto &CallFState = blockStateForStmt(BlockStates, CallF); |
| 365 |
EXPECT_NE(&CallFState, &AddrOfIState); |
| 366 |
EXPECT_EQ(CallFState.Env.get<PointerValue>(AddrOfI), AddrOfIVal); |
| 367 |
|
| 368 |
// In the block that calls `g()`, `&i` should no longer be associated with a |
| 369 |
// value. |
| 370 |
const auto &CallGState = blockStateForStmt(BlockStates, CallG); |
| 371 |
EXPECT_EQ(CallGState.Env.get<PointerValue>(AddrOfI), nullptr); |
| 372 |
} |
| 373 |
|
| 374 |
TEST_F(DiscardExprStateTest, CallWithParenExprTreatedCorrectly) { |
| 375 |
// This is a regression test. |
| 376 |
// In the CFG for `target()` below, the expression that evaluates the function |
| 377 |
// pointer for `expect` and the actual call are separated into different |
| 378 |
// baseic blocks (because of the control flow introduced by the `||` |
| 379 |
// operator). |
| 380 |
// The value for the `expect` function pointer was erroneously discarded |
| 381 |
// from the environment between these two blocks because the code that |
| 382 |
// determines whether the expression values for a block need to be preserved |
| 383 |
// did not ignore the `ParenExpr` around `(i == 1)` (which is not represented |
| 384 |
// in the CFG). |
| 385 |
std::string Code = R"( |
| 386 |
bool expect(bool, bool); |
| 387 |
void target(int i) { |
| 388 |
expect(false || (i == 1), false); |
| 389 |
} |
| 390 |
)"; |
| 391 |
auto BlockStates = llvm::cantFail(runAnalysis<NoopAnalysis>( |
| 392 |
Code, [](ASTContext &C) { return NoopAnalysis(C); })); |
| 393 |
|
| 394 |
const auto &FnToPtrDecay = matchNode<ImplicitCastExpr>( |
| 395 |
implicitCastExpr(hasCastKind(CK_FunctionToPointerDecay))); |
| 396 |
const auto &CallExpect = |
| 397 |
matchNode<CallExpr>(callExpr(callee(functionDecl(hasName("expect"))))); |
| 398 |
|
| 399 |
// In the block that evaluates the implicit cast of `expect` to a pointer, |
| 400 |
// this expression is associated with a value. |
| 401 |
const auto &FnToPtrDecayState = blockStateForStmt(BlockStates, FnToPtrDecay); |
| 402 |
EXPECT_NE(FnToPtrDecayState.Env.getValue(FnToPtrDecay), nullptr); |
| 403 |
|
| 404 |
// In the block that calls `expect()`, the implicit cast of `expect` to a |
| 405 |
// pointer is still associated with a value. |
| 406 |
const auto &CallExpectState = blockStateForStmt(BlockStates, CallExpect); |
| 407 |
EXPECT_NE(CallExpectState.Env.getValue(FnToPtrDecay), nullptr); |
| 408 |
} |
| 409 |
|
| 410 |
struct NonConvergingLattice { |
| 411 |
int State; |
| 412 |
|
| 413 |
bool operator==(const NonConvergingLattice &Other) const { |
| 414 |
return State == Other.State; |
| 415 |
} |
| 416 |
|
| 417 |
LatticeJoinEffect join(const NonConvergingLattice &Other) { |
| 418 |
if (Other.State == 0) |
| 419 |
return LatticeJoinEffect::Unchanged; |
| 420 |
State += Other.State; |
| 421 |
return LatticeJoinEffect::Changed; |
| 422 |
} |
| 423 |
}; |
| 424 |
|
| 425 |
class NonConvergingAnalysis |
| 426 |
: public DataflowAnalysis<NonConvergingAnalysis, NonConvergingLattice> { |
| 427 |
public: |
| 428 |
explicit NonConvergingAnalysis(ASTContext &Context) |
| 429 |
: DataflowAnalysis<NonConvergingAnalysis, NonConvergingLattice>( |
| 430 |
Context, |
| 431 |
// Don't apply builtin transfer function. |
| 432 |
DataflowAnalysisOptions{std::nullopt}) {} |
| 433 |
|
| 434 |
static NonConvergingLattice initialElement() { return {0}; } |
| 435 |
|
| 436 |
void transfer(const CFGElement &, NonConvergingLattice &E, Environment &) { |
| 437 |
++E.State; |
| 438 |
} |
| 439 |
}; |
| 440 |
|
| 441 |
TEST_F(DataflowAnalysisTest, NonConvergingAnalysis) { |
| 442 |
std::string Code = R"( |
| 443 |
void target() { |
| 444 |
while(true) {} |
| 445 |
} |
| 446 |
)"; |
| 447 |
auto Res = runAnalysis<NonConvergingAnalysis>( |
| 448 |
Code, [](ASTContext &C) { return NonConvergingAnalysis(C); }); |
| 449 |
EXPECT_EQ(llvm::toString(Res.takeError()), |
| 450 |
"maximum number of blocks processed"); |
| 451 |
} |
| 452 |
|
| 453 |
// Regression test for joins of bool-typed lvalue expressions. The first loop |
| 454 |
// results in two passes through the code that follows. Each pass results in a |
| 455 |
// different `StorageLocation` for the pointee of `v`. Then, the second loop |
| 456 |
// causes a join at the loop head where the two environments map expresssion |
| 457 |
// `*v` to different `StorageLocation`s. |
| 458 |
// |
| 459 |
// An earlier version crashed for this condition (for boolean-typed lvalues), so |
| 460 |
// this test only verifies that the analysis runs successfully, without |
| 461 |
// examining any details of the results. |
| 462 |
TEST_F(DataflowAnalysisTest, JoinBoolLValues) { |
| 463 |
std::string Code = R"( |
| 464 |
void target() { |
| 465 |
for (int x = 1; x; x = 0) |
| 466 |
(void)x; |
| 467 |
bool *v; |
| 468 |
if (*v) |
| 469 |
for (int x = 1; x; x = 0) |
| 470 |
(void)x; |
| 471 |
} |
| 472 |
)"; |
| 473 |
ASSERT_THAT_ERROR( |
| 474 |
runAnalysis<NoopAnalysis>(Code, |
| 475 |
[](ASTContext &C) { |
| 476 |
auto EnableBuiltIns = DataflowAnalysisOptions{ |
| 477 |
DataflowAnalysisContext::Options{}}; |
| 478 |
return NoopAnalysis(C, EnableBuiltIns); |
| 479 |
}) |
| 480 |
.takeError(), |
| 481 |
llvm::Succeeded()); |
| 482 |
} |
| 483 |
|
| 484 |
struct FunctionCallLattice { |
| 485 |
using FunctionSet = llvm::SmallSet<std::string, 8>; |
| 486 |
FunctionSet CalledFunctions; |
| 487 |
|
| 488 |
bool operator==(const FunctionCallLattice &Other) const { |
| 489 |
return CalledFunctions == Other.CalledFunctions; |
| 490 |
} |
| 491 |
|
| 492 |
LatticeJoinEffect join(const FunctionCallLattice &Other) { |
| 493 |
if (Other.CalledFunctions.empty()) |
| 494 |
return LatticeJoinEffect::Unchanged; |
| 495 |
const size_t size_before = CalledFunctions.size(); |
| 496 |
CalledFunctions.insert_range(Other.CalledFunctions); |
| 497 |
return CalledFunctions.size() == size_before ? LatticeJoinEffect::Unchanged |
| 498 |
: LatticeJoinEffect::Changed; |
| 499 |
} |
| 500 |
}; |
| 501 |
|
| 502 |
std::ostream &operator<<(std::ostream &OS, const FunctionCallLattice &L) { |
| 503 |
std::string S; |
| 504 |
llvm::raw_string_ostream ROS(S); |
| 505 |
llvm::interleaveComma(L.CalledFunctions, ROS); |
| 506 |
return OS << "{" << S << "}"; |
| 507 |
} |
| 508 |
|
| 509 |
class FunctionCallAnalysis |
| 510 |
: public DataflowAnalysis<FunctionCallAnalysis, FunctionCallLattice> { |
| 511 |
public: |
| 512 |
explicit FunctionCallAnalysis(ASTContext &Context) |
| 513 |
: DataflowAnalysis<FunctionCallAnalysis, FunctionCallLattice>(Context) {} |
| 514 |
|
| 515 |
static FunctionCallLattice initialElement() { return {}; } |
| 516 |
|
| 517 |
void transfer(const CFGElement &Elt, FunctionCallLattice &E, Environment &) { |
| 518 |
auto CS = Elt.getAs<CFGStmt>(); |
| 519 |
if (!CS) |
| 520 |
return; |
| 521 |
const auto *S = CS->getStmt(); |
| 522 |
if (auto *C = dyn_cast<CallExpr>(S)) { |
| 523 |
if (auto *F = dyn_cast<FunctionDecl>(C->getCalleeDecl())) { |
| 524 |
E.CalledFunctions.insert(F->getNameInfo().getAsString()); |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
}; |
| 529 |
|
| 530 |
class NoreturnDestructorTest : public Test { |
| 531 |
protected: |
| 532 |
template <typename Matcher> |
| 533 |
void runDataflow(llvm::StringRef Code, Matcher Expectations) { |
| 534 |
tooling::FileContentMappings FilesContents; |
| 535 |
FilesContents.push_back(std::make_pair<std::string, std::string>( |
| 536 |
"noreturn_destructor_test_defs.h", R"( |
| 537 |
int foo(); |
| 538 |
|
| 539 |
class Fatal { |
| 540 |
public: |
| 541 |
~Fatal() __attribute__((noreturn)); |
| 542 |
int bar(); |
| 543 |
int baz(); |
| 544 |
}; |
| 545 |
|
| 546 |
class NonFatal { |
| 547 |
public: |
| 548 |
~NonFatal(); |
| 549 |
int bar(); |
| 550 |
}; |
| 551 |
)")); |
| 552 |
|
| 553 |
ASSERT_THAT_ERROR( |
| 554 |
test::checkDataflow<FunctionCallAnalysis>( |
| 555 |
AnalysisInputs<FunctionCallAnalysis>( |
| 556 |
Code, ast_matchers::hasName("target"), |
| 557 |
[](ASTContext &C, Environment &) { |
| 558 |
return FunctionCallAnalysis(C); |
| 559 |
}) |
| 560 |
.withASTBuildArgs({"-fsyntax-only", "-std=c++17"}) |
| 561 |
.withASTBuildVirtualMappedFiles(std::move(FilesContents)), |
| 562 |
/*VerifyResults=*/ |
| 563 |
[&Expectations]( |
| 564 |
const llvm::StringMap< |
| 565 |
DataflowAnalysisState<FunctionCallLattice>> &Results, |
| 566 |
const AnalysisOutputs &) { |
| 567 |
EXPECT_THAT(Results, Expectations); |
| 568 |
}), |
| 569 |
llvm::Succeeded()); |
| 570 |
} |
| 571 |
}; |
| 572 |
|
| 573 |
MATCHER_P(HoldsFunctionCallLattice, m, |
| 574 |
((negation ? "doesn't hold" : "holds") + |
| 575 |
llvm::StringRef(" a lattice element that ") + |
| 576 |
DescribeMatcher<FunctionCallLattice>(m)) |
| 577 |
.str()) { |
| 578 |
return ExplainMatchResult(m, arg.Lattice, result_listener); |
| 579 |
} |
| 580 |
|
| 581 |
MATCHER_P(HasCalledFunctions, m, |
| 582 |
((negation ? "doesn't hold" : "holds") + |
| 583 |
llvm::StringRef(" a set of called functions that ") + |
| 584 |
DescribeMatcher<FunctionCallLattice::FunctionSet>(m)) |
| 585 |
.str()) { |
| 586 |
return ExplainMatchResult(m, arg.CalledFunctions, result_listener); |
| 587 |
} |
| 588 |
|
| 589 |
TEST_F(NoreturnDestructorTest, ConditionalOperatorBothBranchesReturn) { |
| 590 |
std::string Code = R"( |
| 591 |
#include "noreturn_destructor_test_defs.h" |
| 592 |
|
| 593 |
void target(bool b) { |
| 594 |
int value = b ? foo() : NonFatal().bar(); |
| 595 |
(void)0; |
| 596 |
// [[p]] |
| 597 |
} |
| 598 |
)"; |
| 599 |
runDataflow(Code, UnorderedElementsAre(IsStringMapEntry( |
| 600 |
"p", HoldsFunctionCallLattice(HasCalledFunctions( |
| 601 |
UnorderedElementsAre("foo", "bar")))))); |
| 602 |
} |
| 603 |
|
| 604 |
TEST_F(NoreturnDestructorTest, ConditionalOperatorLeftBranchReturns) { |
| 605 |
std::string Code = R"( |
| 606 |
#include "noreturn_destructor_test_defs.h" |
| 607 |
|
| 608 |
void target(bool b) { |
| 609 |
int value = b ? foo() : Fatal().bar(); |
| 610 |
(void)0; |
| 611 |
// [[p]] |
| 612 |
} |
| 613 |
)"; |
| 614 |
runDataflow(Code, UnorderedElementsAre(IsStringMapEntry( |
| 615 |
"p", HoldsFunctionCallLattice(HasCalledFunctions( |
| 616 |
UnorderedElementsAre("foo")))))); |
| 617 |
} |
| 618 |
|
| 619 |
TEST_F(NoreturnDestructorTest, |
| 620 |
ConditionalOperatorConstantCondition_LeftBranchReturns) { |
| 621 |
std::string Code = R"( |
| 622 |
#include "noreturn_destructor_test_defs.h" |
| 623 |
|
| 624 |
void target() { |
| 625 |
int value = true ? foo() : Fatal().bar(); |
| 626 |
(void)0; |
| 627 |
// [[p]] |
| 628 |
} |
| 629 |
)"; |
| 630 |
runDataflow(Code, UnorderedElementsAre(IsStringMapEntry( |
| 631 |
"p", HoldsFunctionCallLattice(HasCalledFunctions( |
| 632 |
UnorderedElementsAre("foo")))))); |
| 633 |
} |
| 634 |
|
| 635 |
TEST_F(NoreturnDestructorTest, ConditionalOperatorRightBranchReturns) { |
| 636 |
std::string Code = R"( |
| 637 |
#include "noreturn_destructor_test_defs.h" |
| 638 |
|
| 639 |
void target(bool b) { |
| 640 |
int value = b ? Fatal().bar() : foo(); |
| 641 |
(void)0; |
| 642 |
// [[p]] |
| 643 |
} |
| 644 |
)"; |
| 645 |
runDataflow(Code, UnorderedElementsAre(IsStringMapEntry( |
| 646 |
"p", HoldsFunctionCallLattice(HasCalledFunctions( |
| 647 |
UnorderedElementsAre("foo")))))); |
| 648 |
} |
| 649 |
|
| 650 |
TEST_F(NoreturnDestructorTest, |
| 651 |
ConditionalOperatorConstantCondition_RightBranchReturns) { |
| 652 |
std::string Code = R"( |
| 653 |
#include "noreturn_destructor_test_defs.h" |
| 654 |
|
| 655 |
void target() { |
| 656 |
int value = false ? Fatal().bar() : foo(); |
| 657 |
(void)0; |
| 658 |
// [[p]] |
| 659 |
} |
| 660 |
)"; |
| 661 |
runDataflow(Code, UnorderedElementsAre(IsStringMapEntry( |
| 662 |
"p", HoldsFunctionCallLattice(HasCalledFunctions( |
| 663 |
UnorderedElementsAre("foo")))))); |
| 664 |
} |
| 665 |
|
| 666 |
TEST_F(NoreturnDestructorTest, ConditionalOperatorNestedBranchesDoNotReturn) { |
| 667 |
std::string Code = R"( |
| 668 |
#include "noreturn_destructor_test_defs.h" |
| 669 |
|
| 670 |
void target(bool b1, bool b2) { |
| 671 |
int value = b1 ? foo() : (b2 ? Fatal().bar() : Fatal().baz()); |
| 672 |
(void)0; |
| 673 |
// [[p]] |
| 674 |
} |
| 675 |
)"; |
| 676 |
runDataflow(Code, IsEmpty()); |
| 677 |
// FIXME: Called functions at point `p` should contain "foo". |
| 678 |
} |
| 679 |
|
| 680 |
TEST_F(NoreturnDestructorTest, ConditionalOperatorNestedBranchReturns) { |
| 681 |
std::string Code = R"( |
| 682 |
#include "noreturn_destructor_test_defs.h" |
| 683 |
|
| 684 |
void target(bool b1, bool b2) { |
| 685 |
int value = b1 ? Fatal().bar() : (b2 ? Fatal().baz() : foo()); |
| 686 |
(void)0; |
| 687 |
// [[p]] |
| 688 |
} |
| 689 |
)"; |
| 690 |
runDataflow(Code, UnorderedElementsAre(IsStringMapEntry( |
| 691 |
"p", HoldsFunctionCallLattice(HasCalledFunctions( |
| 692 |
UnorderedElementsAre("baz", "foo")))))); |
| 693 |
// FIXME: Called functions at point `p` should contain only "foo". |
| 694 |
} |
| 695 |
|
| 696 |
class AnalyzerNoreturnTest : public Test { |
| 697 |
protected: |
| 698 |
template <typename Matcher> |
| 699 |
void runDataflow(llvm::StringRef Code, Matcher Expectations) { |
| 700 |
tooling::FileContentMappings FilesContents; |
| 701 |
FilesContents.push_back( |
| 702 |
std::make_pair<std::string, std::string>("noreturn_test_defs.h", R"( |
| 703 |
void assertionHandler() __attribute__((analyzer_noreturn)); |
| 704 |
|
| 705 |
void trap() {} |
| 706 |
)")); |
| 707 |
|
| 708 |
ASSERT_THAT_ERROR( |
| 709 |
test::checkDataflow<FunctionCallAnalysis>( |
| 710 |
AnalysisInputs<FunctionCallAnalysis>( |
| 711 |
Code, ast_matchers::hasName("target"), |
| 712 |
[](ASTContext &C, Environment &) { |
| 713 |
return FunctionCallAnalysis(C); |
| 714 |
}) |
| 715 |
.withASTBuildArgs({"-fsyntax-only", "-std=c++17"}) |
| 716 |
.withASTBuildVirtualMappedFiles(std::move(FilesContents)), |
| 717 |
/*VerifyResults=*/ |
| 718 |
[&Expectations]( |
| 719 |
const llvm::StringMap< |
| 720 |
DataflowAnalysisState<FunctionCallLattice>> &Results, |
| 721 |
const AnalysisOutputs &) { |
| 722 |
EXPECT_THAT(Results, Expectations); |
| 723 |
}), |
| 724 |
llvm::Succeeded()); |
| 725 |
} |
| 726 |
}; |
| 727 |
|
| 728 |
TEST_F(AnalyzerNoreturnTest, Breathing) { |
| 729 |
std::string Code = R"( |
| 730 |
#include "noreturn_test_defs.h" |
| 731 |
|
| 732 |
void target() { |
| 733 |
trap(); |
| 734 |
// [[p]] |
| 735 |
} |
| 736 |
)"; |
| 737 |
runDataflow(Code, UnorderedElementsAre(IsStringMapEntry( |
| 738 |
"p", HoldsFunctionCallLattice(HasCalledFunctions( |
| 739 |
UnorderedElementsAre("trap")))))); |
| 740 |
} |
| 741 |
|
| 742 |
TEST_F(AnalyzerNoreturnTest, DirectNoReturnCall) { |
| 743 |
std::string Code = R"( |
| 744 |
#include "noreturn_test_defs.h" |
| 745 |
|
| 746 |
void target() { |
| 747 |
assertionHandler(); |
| 748 |
trap(); |
| 749 |
// [[p]] |
| 750 |
} |
| 751 |
)"; |
| 752 |
runDataflow(Code, IsEmpty()); |
| 753 |
} |
| 754 |
|
| 755 |
TEST_F(AnalyzerNoreturnTest, CanonicalDeclCallCheck) { |
| 756 |
std::string Code = R"( |
| 757 |
#include "noreturn_test_defs.h" |
| 758 |
|
| 759 |
extern void assertionHandler(); |
| 760 |
|
| 761 |
void target() { |
| 762 |
assertionHandler(); |
| 763 |
trap(); |
| 764 |
// [[p]] |
| 765 |
} |
| 766 |
)"; |
| 767 |
runDataflow(Code, IsEmpty()); |
| 768 |
} |
| 769 |
|
| 770 |
// Models an analysis that uses flow conditions. |
| 771 |
class SpecialBoolAnalysis final |
| 772 |
: public DataflowAnalysis<SpecialBoolAnalysis, NoopLattice> { |
| 773 |
public: |
| 774 |
explicit SpecialBoolAnalysis(ASTContext &Context, Environment &Env) |
| 775 |
: DataflowAnalysis<SpecialBoolAnalysis, NoopLattice>(Context) { |
| 776 |
Env.getDataflowAnalysisContext().setSyntheticFieldCallback( |
| 777 |
[](QualType Ty) -> llvm::StringMap<QualType> { |
| 778 |
RecordDecl *RD = Ty->getAsRecordDecl(); |
| 779 |
if (RD == nullptr || RD->getIdentifier() == nullptr || |
| 780 |
RD->getName() != "SpecialBool") |
| 781 |
return {}; |
| 782 |
return {{"is_set", RD->getASTContext().BoolTy}}; |
| 783 |
}); |
| 784 |
} |
| 785 |
|
| 786 |
static NoopLattice initialElement() { return {}; } |
| 787 |
|
| 788 |
void transfer(const CFGElement &Elt, NoopLattice &, Environment &Env) { |
| 789 |
auto CS = Elt.getAs<CFGStmt>(); |
| 790 |
if (!CS) |
| 791 |
return; |
| 792 |
const auto *S = CS->getStmt(); |
| 793 |
auto SpecialBoolRecordDecl = recordDecl(hasName("SpecialBool")); |
| 794 |
auto HasSpecialBoolType = hasType(SpecialBoolRecordDecl); |
| 795 |
|
| 796 |
if (const auto *E = selectFirst<CXXConstructExpr>( |
| 797 |
"call", match(cxxConstructExpr(HasSpecialBoolType).bind("call"), *S, |
| 798 |
getASTContext()))) { |
| 799 |
Env.setValue(Env.getResultObjectLocation(*E).getSyntheticField("is_set"), |
| 800 |
Env.getBoolLiteralValue(false)); |
| 801 |
} else if (const auto *E = selectFirst<CXXMemberCallExpr>( |
| 802 |
"call", match(cxxMemberCallExpr(callee(cxxMethodDecl(ofClass( |
| 803 |
SpecialBoolRecordDecl)))) |
| 804 |
.bind("call"), |
| 805 |
*S, getASTContext()))) { |
| 806 |
if (RecordStorageLocation *ObjectLoc = getImplicitObjectLocation(*E, Env)) |
| 807 |
Env.setValue(ObjectLoc->getSyntheticField("is_set"), |
| 808 |
Env.getBoolLiteralValue(true)); |
| 809 |
} |
| 810 |
} |
| 811 |
}; |
| 812 |
|
| 813 |
class JoinFlowConditionsTest : public Test { |
| 814 |
protected: |
| 815 |
template <typename Matcher> |
| 816 |
void runDataflow(llvm::StringRef Code, Matcher Match) { |
| 817 |
ASSERT_THAT_ERROR( |
| 818 |
test::checkDataflow<SpecialBoolAnalysis>( |
| 819 |
AnalysisInputs<SpecialBoolAnalysis>( |
| 820 |
Code, ast_matchers::hasName("target"), |
| 821 |
[](ASTContext &Context, Environment &Env) { |
| 822 |
return SpecialBoolAnalysis(Context, Env); |
| 823 |
}) |
| 824 |
.withASTBuildArgs({"-fsyntax-only", "-std=c++17"}), |
| 825 |
/*VerifyResults=*/[&Match](const llvm::StringMap< |
| 826 |
DataflowAnalysisState<NoopLattice>> |
| 827 |
&Results, |
| 828 |
const AnalysisOutputs |
| 829 |
&AO) { Match(Results, AO.ASTCtx); }), |
| 830 |
llvm::Succeeded()); |
| 831 |
} |
| 832 |
}; |
| 833 |
|
| 834 |
TEST_F(JoinFlowConditionsTest, JoinDistinctButProvablyEquivalentValues) { |
| 835 |
std::string Code = R"( |
| 836 |
struct SpecialBool { |
| 837 |
SpecialBool() = default; |
| 838 |
void set(); |
| 839 |
}; |
| 840 |
|
| 841 |
void target(bool Cond) { |
| 842 |
SpecialBool Foo; |
| 843 |
/*[[p1]]*/ |
| 844 |
if (Cond) { |
| 845 |
Foo.set(); |
| 846 |
/*[[p2]]*/ |
| 847 |
} else { |
| 848 |
Foo.set(); |
| 849 |
/*[[p3]]*/ |
| 850 |
} |
| 851 |
(void)0; |
| 852 |
/*[[p4]]*/ |
| 853 |
} |
| 854 |
)"; |
| 855 |
runDataflow( |
| 856 |
Code, |
| 857 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 858 |
ASTContext &ASTCtx) { |
| 859 |
ASSERT_THAT(Results.keys(), |
| 860 |
UnorderedElementsAre("p1", "p2", "p3", "p4")); |
| 861 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 862 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 863 |
const Environment &Env3 = getEnvironmentAtAnnotation(Results, "p3"); |
| 864 |
const Environment &Env4 = getEnvironmentAtAnnotation(Results, "p4"); |
| 865 |
|
| 866 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 867 |
ASSERT_THAT(FooDecl, NotNull()); |
| 868 |
|
| 869 |
auto GetFoo = [FooDecl](const Environment &Env) -> const Formula & { |
| 870 |
auto *Loc = |
| 871 |
cast<RecordStorageLocation>(Env.getStorageLocation(*FooDecl)); |
| 872 |
return cast<BoolValue>(Env.getValue(Loc->getSyntheticField("is_set"))) |
| 873 |
->formula(); |
| 874 |
}; |
| 875 |
|
| 876 |
EXPECT_FALSE(Env1.proves(GetFoo(Env1))); |
| 877 |
EXPECT_TRUE(Env2.proves(GetFoo(Env2))); |
| 878 |
EXPECT_TRUE(Env3.proves(GetFoo(Env3))); |
| 879 |
EXPECT_TRUE(Env4.proves(GetFoo(Env4))); |
| 880 |
}); |
| 881 |
} |
| 882 |
|
| 883 |
class NullPointerAnalysis final |
| 884 |
: public DataflowAnalysis<NullPointerAnalysis, NoopLattice> { |
| 885 |
public: |
| 886 |
explicit NullPointerAnalysis(ASTContext &Context) |
| 887 |
: DataflowAnalysis<NullPointerAnalysis, NoopLattice>(Context) {} |
| 888 |
|
| 889 |
static NoopLattice initialElement() { return {}; } |
| 890 |
|
| 891 |
void transfer(const CFGElement &Elt, NoopLattice &, Environment &Env) { |
| 892 |
auto CS = Elt.getAs<CFGStmt>(); |
| 893 |
if (!CS) |
| 894 |
return; |
| 895 |
const Stmt *S = CS->getStmt(); |
| 896 |
const Expr *E = dyn_cast<Expr>(S); |
| 897 |
if (!E) |
| 898 |
return; |
| 899 |
|
| 900 |
if (!E->getType()->isPointerType()) |
| 901 |
return; |
| 902 |
|
| 903 |
// Make sure we have a `PointerValue` for `E`. |
| 904 |
auto *PtrVal = cast_or_null<PointerValue>(Env.getValue(*E)); |
| 905 |
if (PtrVal == nullptr) { |
| 906 |
PtrVal = cast<PointerValue>(Env.createValue(E->getType())); |
| 907 |
Env.setValue(*E, *PtrVal); |
| 908 |
} |
| 909 |
|
| 910 |
if (auto *Cast = dyn_cast<ImplicitCastExpr>(E); |
| 911 |
Cast && Cast->getCastKind() == CK_NullToPointer) |
| 912 |
PtrVal->setProperty("is_null", Env.getBoolLiteralValue(true)); |
| 913 |
else if (auto *Op = dyn_cast<UnaryOperator>(E); |
| 914 |
Op && Op->getOpcode() == UO_AddrOf) |
| 915 |
PtrVal->setProperty("is_null", Env.getBoolLiteralValue(false)); |
| 916 |
} |
| 917 |
|
| 918 |
ComparisonResult compare(QualType Type, const Value &Val1, |
| 919 |
const Environment &Env1, const Value &Val2, |
| 920 |
const Environment &Env2) override { |
| 921 |
// Nothing to say about a value that is not a pointer. |
| 922 |
if (!Type->isPointerType()) |
| 923 |
return ComparisonResult::Unknown; |
| 924 |
|
| 925 |
auto *Prop1 = Val1.getProperty("is_null"); |
| 926 |
auto *Prop2 = Val2.getProperty("is_null"); |
| 927 |
assert(Prop1 != nullptr && Prop2 != nullptr); |
| 928 |
return areEquivalentValues(*Prop1, *Prop2) ? ComparisonResult::Same |
| 929 |
: ComparisonResult::Different; |
| 930 |
} |
| 931 |
|
| 932 |
void join(QualType Type, const Value &Val1, const Environment &Env1, |
| 933 |
const Value &Val2, const Environment &Env2, Value &JoinedVal, |
| 934 |
Environment &JoinedEnv) override { |
| 935 |
// Nothing to say about a value that is not a pointer... |
| 936 |
if (!Type->isPointerType()) |
| 937 |
return; |
| 938 |
|
| 939 |
// ... or, a pointer without the `is_null` property. |
| 940 |
auto *IsNull1 = cast_or_null<BoolValue>(Val1.getProperty("is_null")); |
| 941 |
auto *IsNull2 = cast_or_null<BoolValue>(Val2.getProperty("is_null")); |
| 942 |
if (IsNull1 == nullptr || IsNull2 == nullptr) |
| 943 |
return; |
| 944 |
|
| 945 |
if (IsNull1 == IsNull2) |
| 946 |
JoinedVal.setProperty("is_null", *IsNull1); |
| 947 |
else |
| 948 |
JoinedVal.setProperty("is_null", JoinedEnv.makeTopBoolValue()); |
| 949 |
} |
| 950 |
|
| 951 |
std::optional<WidenResult> widen(QualType Type, Value &Prev, |
| 952 |
const Environment &PrevEnv, Value &Current, |
| 953 |
Environment &CurrentEnv) override { |
| 954 |
switch (compare(Type, Prev, PrevEnv, Current, CurrentEnv)) { |
| 955 |
case ComparisonResult::Same: |
| 956 |
return WidenResult{&Current, LatticeJoinEffect::Unchanged}; |
| 957 |
case ComparisonResult::Different: { |
| 958 |
auto &CurPtr = cast<PointerValue>(Current); |
| 959 |
auto &WidenedPtr = |
| 960 |
CurrentEnv.create<PointerValue>(CurPtr.getPointeeLoc()); |
| 961 |
WidenedPtr.setProperty("is_null", CurrentEnv.makeTopBoolValue()); |
| 962 |
return WidenResult{&WidenedPtr, LatticeJoinEffect::Changed}; |
| 963 |
} |
| 964 |
case ComparisonResult::Unknown: |
| 965 |
return std::nullopt; |
| 966 |
} |
| 967 |
llvm_unreachable("all cases in switch covered"); |
| 968 |
} |
| 969 |
}; |
| 970 |
|
| 971 |
class WideningTest : public Test { |
| 972 |
protected: |
| 973 |
template <typename Matcher> |
| 974 |
void runDataflow(llvm::StringRef Code, Matcher Match) { |
| 975 |
ASSERT_THAT_ERROR( |
| 976 |
checkDataflow<NullPointerAnalysis>( |
| 977 |
AnalysisInputs<NullPointerAnalysis>( |
| 978 |
Code, ast_matchers::hasName("target"), |
| 979 |
[](ASTContext &Context, Environment &Env) { |
| 980 |
return NullPointerAnalysis(Context); |
| 981 |
}) |
| 982 |
.withASTBuildArgs({"-fsyntax-only", "-std=c++17"}), |
| 983 |
/*VerifyResults=*/[&Match](const llvm::StringMap< |
| 984 |
DataflowAnalysisState<NoopLattice>> |
| 985 |
&Results, |
| 986 |
const AnalysisOutputs |
| 987 |
&AO) { Match(Results, AO.ASTCtx); }), |
| 988 |
llvm::Succeeded()); |
| 989 |
} |
| 990 |
}; |
| 991 |
|
| 992 |
TEST_F(WideningTest, JoinDistinctValuesWithDistinctProperties) { |
| 993 |
std::string Code = R"( |
| 994 |
void target(bool Cond) { |
| 995 |
int *Foo = nullptr; |
| 996 |
int i = 0; |
| 997 |
/*[[p1]]*/ |
| 998 |
if (Cond) { |
| 999 |
Foo = &i; |
| 1000 |
/*[[p2]]*/ |
| 1001 |
} |
| 1002 |
(void)0; |
| 1003 |
/*[[p3]]*/ |
| 1004 |
} |
| 1005 |
)"; |
| 1006 |
runDataflow( |
| 1007 |
Code, |
| 1008 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1009 |
ASTContext &ASTCtx) { |
| 1010 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1011 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1012 |
const Environment &Env3 = getEnvironmentAtAnnotation(Results, "p3"); |
| 1013 |
|
| 1014 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1015 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1016 |
|
| 1017 |
auto GetFooValue = [FooDecl](const Environment &Env) { |
| 1018 |
return Env.getValue(*FooDecl); |
| 1019 |
}; |
| 1020 |
|
| 1021 |
EXPECT_EQ(GetFooValue(Env1)->getProperty("is_null"), |
| 1022 |
&Env1.getBoolLiteralValue(true)); |
| 1023 |
EXPECT_EQ(GetFooValue(Env2)->getProperty("is_null"), |
| 1024 |
&Env2.getBoolLiteralValue(false)); |
| 1025 |
EXPECT_TRUE( |
| 1026 |
isa<TopBoolValue>(GetFooValue(Env3)->getProperty("is_null"))); |
| 1027 |
}); |
| 1028 |
} |
| 1029 |
|
| 1030 |
TEST_F(WideningTest, JoinDistinctValuesWithSameProperties) { |
| 1031 |
std::string Code = R"( |
| 1032 |
void target(bool Cond) { |
| 1033 |
int *Foo = nullptr; |
| 1034 |
int i1 = 0; |
| 1035 |
int i2 = 0; |
| 1036 |
/*[[p1]]*/ |
| 1037 |
if (Cond) { |
| 1038 |
Foo = &i1; |
| 1039 |
/*[[p2]]*/ |
| 1040 |
} else { |
| 1041 |
Foo = &i2; |
| 1042 |
/*[[p3]]*/ |
| 1043 |
} |
| 1044 |
(void)0; |
| 1045 |
/*[[p4]]*/ |
| 1046 |
} |
| 1047 |
)"; |
| 1048 |
runDataflow( |
| 1049 |
Code, |
| 1050 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1051 |
ASTContext &ASTCtx) { |
| 1052 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1053 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1054 |
const Environment &Env3 = getEnvironmentAtAnnotation(Results, "p3"); |
| 1055 |
const Environment &Env4 = getEnvironmentAtAnnotation(Results, "p4"); |
| 1056 |
|
| 1057 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1058 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1059 |
|
| 1060 |
auto GetFooValue = [FooDecl](const Environment &Env) { |
| 1061 |
return Env.getValue(*FooDecl); |
| 1062 |
}; |
| 1063 |
|
| 1064 |
EXPECT_EQ(GetFooValue(Env1)->getProperty("is_null"), |
| 1065 |
&Env1.getBoolLiteralValue(true)); |
| 1066 |
EXPECT_EQ(GetFooValue(Env2)->getProperty("is_null"), |
| 1067 |
&Env2.getBoolLiteralValue(false)); |
| 1068 |
EXPECT_EQ(GetFooValue(Env3)->getProperty("is_null"), |
| 1069 |
&Env3.getBoolLiteralValue(false)); |
| 1070 |
EXPECT_EQ(GetFooValue(Env4)->getProperty("is_null"), |
| 1071 |
&Env4.getBoolLiteralValue(false)); |
| 1072 |
}); |
| 1073 |
} |
| 1074 |
|
| 1075 |
TEST_F(WideningTest, DistinctPointersToTheSameLocationAreEquivalent) { |
| 1076 |
std::string Code = R"( |
| 1077 |
void target(int Foo, bool Cond) { |
| 1078 |
int *Bar = &Foo; |
| 1079 |
while (Cond) { |
| 1080 |
Bar = &Foo; |
| 1081 |
} |
| 1082 |
(void)0; |
| 1083 |
// [[p]] |
| 1084 |
} |
| 1085 |
)"; |
| 1086 |
runDataflow( |
| 1087 |
Code, |
| 1088 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1089 |
ASTContext &ASTCtx) { |
| 1090 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1091 |
const auto &FooLoc = |
| 1092 |
getLocForDecl<ScalarStorageLocation>(ASTCtx, Env, "Foo"); |
| 1093 |
const auto &BarVal = getValueForDecl<PointerValue>(ASTCtx, Env, "Bar"); |
| 1094 |
EXPECT_EQ(&BarVal.getPointeeLoc(), &FooLoc); |
| 1095 |
}); |
| 1096 |
} |
| 1097 |
|
| 1098 |
TEST_F(WideningTest, DistinctValuesWithSamePropertiesAreEquivalent) { |
| 1099 |
std::string Code = R"( |
| 1100 |
void target(bool Cond) { |
| 1101 |
int *Foo; |
| 1102 |
int i1 = 0; |
| 1103 |
int i2 = 0; |
| 1104 |
Foo = &i1; |
| 1105 |
while (Cond) { |
| 1106 |
Foo = &i2; |
| 1107 |
} |
| 1108 |
(void)0; |
| 1109 |
/*[[p]]*/ |
| 1110 |
} |
| 1111 |
)"; |
| 1112 |
runDataflow( |
| 1113 |
Code, |
| 1114 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1115 |
ASTContext &ASTCtx) { |
| 1116 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1117 |
const auto &FooVal = getValueForDecl<Value>(ASTCtx, Env, "Foo"); |
| 1118 |
EXPECT_EQ(FooVal.getProperty("is_null"), |
| 1119 |
&Env.getBoolLiteralValue(false)); |
| 1120 |
}); |
| 1121 |
} |
| 1122 |
|
| 1123 |
TEST_F(WideningTest, DistinctValuesWithDifferentPropertiesWidenedToTop) { |
| 1124 |
std::string Code = R"( |
| 1125 |
void target(bool Cond) { |
| 1126 |
int *Foo; |
| 1127 |
int i = 0; |
| 1128 |
Foo = nullptr; |
| 1129 |
while (Cond) { |
| 1130 |
Foo = &i; |
| 1131 |
} |
| 1132 |
(void)0; |
| 1133 |
/*[[p]]*/ |
| 1134 |
} |
| 1135 |
)"; |
| 1136 |
runDataflow( |
| 1137 |
Code, |
| 1138 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1139 |
ASTContext &ASTCtx) { |
| 1140 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1141 |
const auto &FooVal = getValueForDecl<Value>(ASTCtx, Env, "Foo"); |
| 1142 |
ASSERT_THAT(FooVal.getProperty("is_null"), NotNull()); |
| 1143 |
EXPECT_TRUE(areEquivalentValues(*FooVal.getProperty("is_null"), |
| 1144 |
Env.makeTopBoolValue())); |
| 1145 |
}); |
| 1146 |
} |
| 1147 |
|
| 1148 |
class FlowConditionTest : public Test { |
| 1149 |
protected: |
| 1150 |
template <typename Matcher> |
| 1151 |
void runDataflow(llvm::StringRef Code, Matcher Match) { |
| 1152 |
ASSERT_THAT_ERROR( |
| 1153 |
checkDataflow<NoopAnalysis>( |
| 1154 |
AnalysisInputs<NoopAnalysis>( |
| 1155 |
Code, ast_matchers::hasName("target"), |
| 1156 |
[](ASTContext &Context, Environment &Env) { |
| 1157 |
return NoopAnalysis(Context); |
| 1158 |
}) |
| 1159 |
.withASTBuildArgs({"-fsyntax-only", "-std=c++17"}), |
| 1160 |
/*VerifyResults=*/[&Match](const llvm::StringMap< |
| 1161 |
DataflowAnalysisState<NoopLattice>> |
| 1162 |
&Results, |
| 1163 |
const AnalysisOutputs |
| 1164 |
&AO) { Match(Results, AO.ASTCtx); }), |
| 1165 |
llvm::Succeeded()); |
| 1166 |
} |
| 1167 |
}; |
| 1168 |
|
| 1169 |
TEST_F(FlowConditionTest, IfStmtSingleVar) { |
| 1170 |
std::string Code = R"( |
| 1171 |
void target(bool Foo) { |
| 1172 |
if (Foo) { |
| 1173 |
(void)0; |
| 1174 |
/*[[p1]]*/ |
| 1175 |
} else { |
| 1176 |
(void)1; |
| 1177 |
/*[[p2]]*/ |
| 1178 |
} |
| 1179 |
} |
| 1180 |
)"; |
| 1181 |
runDataflow( |
| 1182 |
Code, |
| 1183 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1184 |
ASTContext &ASTCtx) { |
| 1185 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1186 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1187 |
|
| 1188 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1189 |
|
| 1190 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1191 |
auto &FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl))->formula(); |
| 1192 |
EXPECT_TRUE(Env1.proves(FooVal1)); |
| 1193 |
|
| 1194 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1195 |
auto &FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl))->formula(); |
| 1196 |
EXPECT_FALSE(Env2.proves(FooVal2)); |
| 1197 |
}); |
| 1198 |
} |
| 1199 |
|
| 1200 |
TEST_F(FlowConditionTest, IfStmtSingleNegatedVar) { |
| 1201 |
std::string Code = R"( |
| 1202 |
void target(bool Foo) { |
| 1203 |
if (!Foo) { |
| 1204 |
(void)0; |
| 1205 |
/*[[p1]]*/ |
| 1206 |
} else { |
| 1207 |
(void)1; |
| 1208 |
/*[[p2]]*/ |
| 1209 |
} |
| 1210 |
} |
| 1211 |
)"; |
| 1212 |
runDataflow( |
| 1213 |
Code, |
| 1214 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1215 |
ASTContext &ASTCtx) { |
| 1216 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1217 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1218 |
|
| 1219 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1220 |
|
| 1221 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1222 |
auto &FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl))->formula(); |
| 1223 |
EXPECT_FALSE(Env1.proves(FooVal1)); |
| 1224 |
|
| 1225 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1226 |
auto &FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl))->formula(); |
| 1227 |
EXPECT_TRUE(Env2.proves(FooVal2)); |
| 1228 |
}); |
| 1229 |
} |
| 1230 |
|
| 1231 |
TEST_F(FlowConditionTest, WhileStmt) { |
| 1232 |
std::string Code = R"( |
| 1233 |
void target(bool Foo) { |
| 1234 |
while (Foo) { |
| 1235 |
(void)0; |
| 1236 |
/*[[p]]*/ |
| 1237 |
} |
| 1238 |
} |
| 1239 |
)"; |
| 1240 |
runDataflow( |
| 1241 |
Code, |
| 1242 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1243 |
ASTContext &ASTCtx) { |
| 1244 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1245 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1246 |
|
| 1247 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p")); |
| 1248 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1249 |
|
| 1250 |
auto &FooVal = cast<BoolValue>(Env.getValue(*FooDecl))->formula(); |
| 1251 |
EXPECT_TRUE(Env.proves(FooVal)); |
| 1252 |
}); |
| 1253 |
} |
| 1254 |
|
| 1255 |
TEST_F(FlowConditionTest, WhileStmtWithAssignmentInCondition) { |
| 1256 |
std::string Code = R"( |
| 1257 |
void target(bool Foo) { |
| 1258 |
// This test checks whether the analysis preserves the connection between |
| 1259 |
// the value of `Foo` and the assignment expression, despite widening. |
| 1260 |
// The equality operator generates a fresh boolean variable on each |
| 1261 |
// interpretation, which forces use of widening. |
| 1262 |
while ((Foo = (3 == 4))) { |
| 1263 |
(void)0; |
| 1264 |
/*[[p]]*/ |
| 1265 |
} |
| 1266 |
} |
| 1267 |
)"; |
| 1268 |
runDataflow( |
| 1269 |
Code, |
| 1270 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1271 |
ASTContext &ASTCtx) { |
| 1272 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1273 |
auto &FooVal = getValueForDecl<BoolValue>(ASTCtx, Env, "Foo").formula(); |
| 1274 |
EXPECT_TRUE(Env.proves(FooVal)); |
| 1275 |
}); |
| 1276 |
} |
| 1277 |
|
| 1278 |
TEST_F(FlowConditionTest, Conjunction) { |
| 1279 |
std::string Code = R"( |
| 1280 |
void target(bool Foo, bool Bar) { |
| 1281 |
if (Foo && Bar) { |
| 1282 |
(void)0; |
| 1283 |
/*[[p1]]*/ |
| 1284 |
} else { |
| 1285 |
(void)1; |
| 1286 |
/*[[p2]]*/ |
| 1287 |
} |
| 1288 |
} |
| 1289 |
)"; |
| 1290 |
runDataflow(Code, [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> |
| 1291 |
&Results, |
| 1292 |
ASTContext &ASTCtx) { |
| 1293 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1294 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1295 |
|
| 1296 |
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); |
| 1297 |
ASSERT_THAT(BarDecl, NotNull()); |
| 1298 |
|
| 1299 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1300 |
|
| 1301 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1302 |
auto &FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl))->formula(); |
| 1303 |
auto &BarVal1 = cast<BoolValue>(Env1.getValue(*BarDecl))->formula(); |
| 1304 |
EXPECT_TRUE(Env1.proves(FooVal1)); |
| 1305 |
EXPECT_TRUE(Env1.proves(BarVal1)); |
| 1306 |
|
| 1307 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1308 |
auto &FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl))->formula(); |
| 1309 |
auto &BarVal2 = cast<BoolValue>(Env2.getValue(*BarDecl))->formula(); |
| 1310 |
EXPECT_FALSE(Env2.proves(FooVal2)); |
| 1311 |
EXPECT_FALSE(Env2.proves(BarVal2)); |
| 1312 |
}); |
| 1313 |
} |
| 1314 |
|
| 1315 |
TEST_F(FlowConditionTest, Disjunction) { |
| 1316 |
std::string Code = R"( |
| 1317 |
void target(bool Foo, bool Bar) { |
| 1318 |
if (Foo || Bar) { |
| 1319 |
(void)0; |
| 1320 |
/*[[p1]]*/ |
| 1321 |
} else { |
| 1322 |
(void)1; |
| 1323 |
/*[[p2]]*/ |
| 1324 |
} |
| 1325 |
} |
| 1326 |
)"; |
| 1327 |
runDataflow(Code, [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> |
| 1328 |
&Results, |
| 1329 |
ASTContext &ASTCtx) { |
| 1330 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1331 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1332 |
|
| 1333 |
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); |
| 1334 |
ASSERT_THAT(BarDecl, NotNull()); |
| 1335 |
|
| 1336 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1337 |
|
| 1338 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1339 |
auto &FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl))->formula(); |
| 1340 |
auto &BarVal1 = cast<BoolValue>(Env1.getValue(*BarDecl))->formula(); |
| 1341 |
EXPECT_FALSE(Env1.proves(FooVal1)); |
| 1342 |
EXPECT_FALSE(Env1.proves(BarVal1)); |
| 1343 |
|
| 1344 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1345 |
auto &FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl))->formula(); |
| 1346 |
auto &BarVal2 = cast<BoolValue>(Env2.getValue(*BarDecl))->formula(); |
| 1347 |
EXPECT_FALSE(Env2.proves(FooVal2)); |
| 1348 |
EXPECT_FALSE(Env2.proves(BarVal2)); |
| 1349 |
}); |
| 1350 |
} |
| 1351 |
|
| 1352 |
TEST_F(FlowConditionTest, NegatedConjunction) { |
| 1353 |
std::string Code = R"( |
| 1354 |
void target(bool Foo, bool Bar) { |
| 1355 |
if (!(Foo && Bar)) { |
| 1356 |
(void)0; |
| 1357 |
/*[[p1]]*/ |
| 1358 |
} else { |
| 1359 |
(void)1; |
| 1360 |
/*[[p2]]*/ |
| 1361 |
} |
| 1362 |
} |
| 1363 |
)"; |
| 1364 |
runDataflow(Code, [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> |
| 1365 |
&Results, |
| 1366 |
ASTContext &ASTCtx) { |
| 1367 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1368 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1369 |
|
| 1370 |
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); |
| 1371 |
ASSERT_THAT(BarDecl, NotNull()); |
| 1372 |
|
| 1373 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1374 |
|
| 1375 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1376 |
auto &FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl))->formula(); |
| 1377 |
auto &BarVal1 = cast<BoolValue>(Env1.getValue(*BarDecl))->formula(); |
| 1378 |
EXPECT_FALSE(Env1.proves(FooVal1)); |
| 1379 |
EXPECT_FALSE(Env1.proves(BarVal1)); |
| 1380 |
|
| 1381 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1382 |
auto &FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl))->formula(); |
| 1383 |
auto &BarVal2 = cast<BoolValue>(Env2.getValue(*BarDecl))->formula(); |
| 1384 |
EXPECT_TRUE(Env2.proves(FooVal2)); |
| 1385 |
EXPECT_TRUE(Env2.proves(BarVal2)); |
| 1386 |
}); |
| 1387 |
} |
| 1388 |
|
| 1389 |
TEST_F(FlowConditionTest, DeMorgan) { |
| 1390 |
std::string Code = R"( |
| 1391 |
void target(bool Foo, bool Bar) { |
| 1392 |
if (!(!Foo || !Bar)) { |
| 1393 |
(void)0; |
| 1394 |
/*[[p1]]*/ |
| 1395 |
} else { |
| 1396 |
(void)1; |
| 1397 |
/*[[p2]]*/ |
| 1398 |
} |
| 1399 |
} |
| 1400 |
)"; |
| 1401 |
runDataflow(Code, [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> |
| 1402 |
&Results, |
| 1403 |
ASTContext &ASTCtx) { |
| 1404 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1405 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1406 |
|
| 1407 |
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); |
| 1408 |
ASSERT_THAT(BarDecl, NotNull()); |
| 1409 |
|
| 1410 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1411 |
|
| 1412 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1413 |
auto &FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl))->formula(); |
| 1414 |
auto &BarVal1 = cast<BoolValue>(Env1.getValue(*BarDecl))->formula(); |
| 1415 |
EXPECT_TRUE(Env1.proves(FooVal1)); |
| 1416 |
EXPECT_TRUE(Env1.proves(BarVal1)); |
| 1417 |
|
| 1418 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1419 |
auto &FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl))->formula(); |
| 1420 |
auto &BarVal2 = cast<BoolValue>(Env2.getValue(*BarDecl))->formula(); |
| 1421 |
EXPECT_FALSE(Env2.proves(FooVal2)); |
| 1422 |
EXPECT_FALSE(Env2.proves(BarVal2)); |
| 1423 |
}); |
| 1424 |
} |
| 1425 |
|
| 1426 |
TEST_F(FlowConditionTest, Join) { |
| 1427 |
std::string Code = R"( |
| 1428 |
void target(bool Foo, bool Bar) { |
| 1429 |
if (Bar) { |
| 1430 |
if (!Foo) |
| 1431 |
return; |
| 1432 |
} else { |
| 1433 |
if (!Foo) |
| 1434 |
return; |
| 1435 |
} |
| 1436 |
(void)0; |
| 1437 |
/*[[p]]*/ |
| 1438 |
} |
| 1439 |
)"; |
| 1440 |
runDataflow( |
| 1441 |
Code, |
| 1442 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1443 |
ASTContext &ASTCtx) { |
| 1444 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p")); |
| 1445 |
|
| 1446 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1447 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1448 |
|
| 1449 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1450 |
auto &FooVal = cast<BoolValue>(Env.getValue(*FooDecl))->formula(); |
| 1451 |
EXPECT_TRUE(Env.proves(FooVal)); |
| 1452 |
}); |
| 1453 |
} |
| 1454 |
|
| 1455 |
// Verifies that flow conditions are properly constructed even when the |
| 1456 |
// condition is not meaningfully interpreted. |
| 1457 |
// |
| 1458 |
// Note: currently, arbitrary function calls are uninterpreted, so the test |
| 1459 |
// exercises this case. If and when we change that, this test will not add to |
| 1460 |
// coverage (although it may still test a valuable case). |
| 1461 |
TEST_F(FlowConditionTest, OpaqueFlowConditionJoinsToOpaqueBool) { |
| 1462 |
std::string Code = R"( |
| 1463 |
bool foo(); |
| 1464 |
|
| 1465 |
void target() { |
| 1466 |
bool Bar = true; |
| 1467 |
if (foo()) |
| 1468 |
Bar = false; |
| 1469 |
(void)0; |
| 1470 |
/*[[p]]*/ |
| 1471 |
} |
| 1472 |
)"; |
| 1473 |
runDataflow( |
| 1474 |
Code, |
| 1475 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1476 |
ASTContext &ASTCtx) { |
| 1477 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p")); |
| 1478 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1479 |
|
| 1480 |
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); |
| 1481 |
ASSERT_THAT(BarDecl, NotNull()); |
| 1482 |
|
| 1483 |
auto &BarVal = cast<BoolValue>(Env.getValue(*BarDecl))->formula(); |
| 1484 |
|
| 1485 |
EXPECT_FALSE(Env.proves(BarVal)); |
| 1486 |
}); |
| 1487 |
} |
| 1488 |
|
| 1489 |
// Verifies that flow conditions are properly constructed even when the |
| 1490 |
// condition is not meaningfully interpreted. |
| 1491 |
// |
| 1492 |
// Note: currently, fields with recursive type calls are uninterpreted (beneath |
| 1493 |
// the first instance), so the test exercises this case. If and when we change |
| 1494 |
// that, this test will not add to coverage (although it may still test a |
| 1495 |
// valuable case). |
| 1496 |
TEST_F(FlowConditionTest, OpaqueFieldFlowConditionJoinsToOpaqueBool) { |
| 1497 |
std::string Code = R"( |
| 1498 |
struct Rec { |
| 1499 |
Rec* Next; |
| 1500 |
}; |
| 1501 |
|
| 1502 |
struct Foo { |
| 1503 |
Rec* X; |
| 1504 |
}; |
| 1505 |
|
| 1506 |
void target(Foo F) { |
| 1507 |
bool Bar = true; |
| 1508 |
if (F.X->Next) |
| 1509 |
Bar = false; |
| 1510 |
(void)0; |
| 1511 |
/*[[p]]*/ |
| 1512 |
} |
| 1513 |
)"; |
| 1514 |
runDataflow( |
| 1515 |
Code, |
| 1516 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1517 |
ASTContext &ASTCtx) { |
| 1518 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p")); |
| 1519 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1520 |
|
| 1521 |
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); |
| 1522 |
ASSERT_THAT(BarDecl, NotNull()); |
| 1523 |
|
| 1524 |
auto &BarVal = cast<BoolValue>(Env.getValue(*BarDecl))->formula(); |
| 1525 |
|
| 1526 |
EXPECT_FALSE(Env.proves(BarVal)); |
| 1527 |
}); |
| 1528 |
} |
| 1529 |
|
| 1530 |
// Verifies that flow conditions are properly constructed even when the |
| 1531 |
// condition is not meaningfully interpreted. Adds to above by nesting the |
| 1532 |
// interestnig case inside a normal branch. This protects against degenerate |
| 1533 |
// solutions which only test for empty flow conditions, for example. |
| 1534 |
TEST_F(FlowConditionTest, OpaqueFlowConditionInsideBranchJoinsToOpaqueBool) { |
| 1535 |
std::string Code = R"( |
| 1536 |
bool foo(); |
| 1537 |
|
| 1538 |
void target(bool Cond) { |
| 1539 |
bool Bar = true; |
| 1540 |
if (Cond) { |
| 1541 |
if (foo()) |
| 1542 |
Bar = false; |
| 1543 |
(void)0; |
| 1544 |
/*[[p]]*/ |
| 1545 |
} |
| 1546 |
} |
| 1547 |
)"; |
| 1548 |
runDataflow( |
| 1549 |
Code, |
| 1550 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1551 |
ASTContext &ASTCtx) { |
| 1552 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p")); |
| 1553 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1554 |
|
| 1555 |
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar"); |
| 1556 |
ASSERT_THAT(BarDecl, NotNull()); |
| 1557 |
|
| 1558 |
auto &BarVal = cast<BoolValue>(Env.getValue(*BarDecl))->formula(); |
| 1559 |
|
| 1560 |
EXPECT_FALSE(Env.proves(BarVal)); |
| 1561 |
}); |
| 1562 |
} |
| 1563 |
|
| 1564 |
TEST_F(FlowConditionTest, PointerToBoolImplicitCast) { |
| 1565 |
std::string Code = R"( |
| 1566 |
void target(int *Ptr) { |
| 1567 |
bool Foo = false; |
| 1568 |
if (Ptr) { |
| 1569 |
Foo = true; |
| 1570 |
/*[[p1]]*/ |
| 1571 |
} |
| 1572 |
|
| 1573 |
(void)0; |
| 1574 |
/*[[p2]]*/ |
| 1575 |
} |
| 1576 |
)"; |
| 1577 |
runDataflow( |
| 1578 |
Code, |
| 1579 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1580 |
ASTContext &ASTCtx) { |
| 1581 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1582 |
|
| 1583 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 1584 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1585 |
|
| 1586 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1587 |
auto &FooVal1 = cast<BoolValue>(Env1.getValue(*FooDecl))->formula(); |
| 1588 |
EXPECT_TRUE(Env1.proves(FooVal1)); |
| 1589 |
|
| 1590 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1591 |
auto &FooVal2 = cast<BoolValue>(Env2.getValue(*FooDecl))->formula(); |
| 1592 |
EXPECT_FALSE(Env2.proves(FooVal2)); |
| 1593 |
}); |
| 1594 |
} |
| 1595 |
|
| 1596 |
class TopAnalysis final : public DataflowAnalysis<TopAnalysis, NoopLattice> { |
| 1597 |
public: |
| 1598 |
explicit TopAnalysis(ASTContext &Context) |
| 1599 |
: DataflowAnalysis<TopAnalysis, NoopLattice>(Context) {} |
| 1600 |
|
| 1601 |
static NoopLattice initialElement() { return {}; } |
| 1602 |
|
| 1603 |
void transfer(const CFGElement &Elt, NoopLattice &, Environment &Env) { |
| 1604 |
auto CS = Elt.getAs<CFGStmt>(); |
| 1605 |
if (!CS) |
| 1606 |
return; |
| 1607 |
const Stmt *S = CS->getStmt(); |
| 1608 |
SmallVector<BoundNodes, 1> Matches = |
| 1609 |
match(callExpr(callee(functionDecl(hasName("makeTop")))).bind("top"), |
| 1610 |
*S, getASTContext()); |
| 1611 |
if (const auto *E = selectFirst<CallExpr>("top", Matches)) { |
| 1612 |
Env.setValue(*E, Env.makeTopBoolValue()); |
| 1613 |
} |
| 1614 |
} |
| 1615 |
|
| 1616 |
ComparisonResult compare(QualType Type, const Value &Val1, |
| 1617 |
const Environment &Env1, const Value &Val2, |
| 1618 |
const Environment &Env2) override { |
| 1619 |
// Changes to a sound approximation, which allows us to test whether we can |
| 1620 |
// (soundly) converge for some loops. |
| 1621 |
return ComparisonResult::Unknown; |
| 1622 |
} |
| 1623 |
}; |
| 1624 |
|
| 1625 |
class TopTest : public Test { |
| 1626 |
protected: |
| 1627 |
template <typename Matcher> |
| 1628 |
void runDataflow(llvm::StringRef Code, Matcher VerifyResults) { |
| 1629 |
ASSERT_THAT_ERROR( |
| 1630 |
checkDataflow<TopAnalysis>( |
| 1631 |
AnalysisInputs<TopAnalysis>( |
| 1632 |
Code, ast_matchers::hasName("target"), |
| 1633 |
[](ASTContext &Context, Environment &Env) { |
| 1634 |
return TopAnalysis(Context); |
| 1635 |
}) |
| 1636 |
.withASTBuildArgs({"-fsyntax-only", "-std=c++17"}), |
| 1637 |
VerifyResults), |
| 1638 |
llvm::Succeeded()); |
| 1639 |
} |
| 1640 |
}; |
| 1641 |
|
| 1642 |
// Tests that when Top is unused it remains Top. |
| 1643 |
TEST_F(TopTest, UnusedTopInitializer) { |
| 1644 |
std::string Code = R"( |
| 1645 |
bool makeTop(); |
| 1646 |
|
| 1647 |
void target() { |
| 1648 |
bool Foo = makeTop(); |
| 1649 |
/*[[p1]]*/ |
| 1650 |
(void)0; |
| 1651 |
/*[[p2]]*/ |
| 1652 |
} |
| 1653 |
)"; |
| 1654 |
runDataflow( |
| 1655 |
Code, |
| 1656 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1657 |
const AnalysisOutputs &AO) { |
| 1658 |
ASSERT_THAT(Results.keys(), |
| 1659 |
UnorderedElementsAre("p1", "p2")); |
| 1660 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1661 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1662 |
|
| 1663 |
const ValueDecl *FooDecl = findValueDecl(AO.ASTCtx, "Foo"); |
| 1664 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1665 |
|
| 1666 |
auto GetFooValue = [FooDecl](const Environment &Env) { |
| 1667 |
return Env.getValue(*FooDecl); |
| 1668 |
}; |
| 1669 |
|
| 1670 |
Value *FooVal1 = GetFooValue(Env1); |
| 1671 |
ASSERT_THAT(FooVal1, NotNull()); |
| 1672 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal1)) |
| 1673 |
<< debugString(FooVal1->getKind()); |
| 1674 |
|
| 1675 |
Value *FooVal2 = GetFooValue(Env2); |
| 1676 |
ASSERT_THAT(FooVal2, NotNull()); |
| 1677 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal2)) |
| 1678 |
<< debugString(FooVal2->getKind()); |
| 1679 |
|
| 1680 |
EXPECT_EQ(FooVal1, FooVal2); |
| 1681 |
}); |
| 1682 |
} |
| 1683 |
|
| 1684 |
// Tests that when Top is unused it remains Top. Like above, but uses the |
| 1685 |
// assignment form rather than initialization, which uses Top as an lvalue that |
| 1686 |
// is *not* in an rvalue position. |
| 1687 |
TEST_F(TopTest, UnusedTopAssignment) { |
| 1688 |
std::string Code = R"( |
| 1689 |
bool makeTop(); |
| 1690 |
|
| 1691 |
void target() { |
| 1692 |
bool Foo; |
| 1693 |
Foo = makeTop(); |
| 1694 |
/*[[p1]]*/ |
| 1695 |
(void)0; |
| 1696 |
/*[[p2]]*/ |
| 1697 |
} |
| 1698 |
)"; |
| 1699 |
runDataflow( |
| 1700 |
Code, |
| 1701 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1702 |
const AnalysisOutputs &AO) { |
| 1703 |
ASSERT_THAT(Results.keys(), |
| 1704 |
UnorderedElementsAre("p1", "p2")); |
| 1705 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1706 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1707 |
|
| 1708 |
const ValueDecl *FooDecl = findValueDecl(AO.ASTCtx, "Foo"); |
| 1709 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1710 |
|
| 1711 |
auto GetFooValue = [FooDecl](const Environment &Env) { |
| 1712 |
return Env.getValue(*FooDecl); |
| 1713 |
}; |
| 1714 |
|
| 1715 |
Value *FooVal1 = GetFooValue(Env1); |
| 1716 |
ASSERT_THAT(FooVal1, NotNull()); |
| 1717 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal1)) |
| 1718 |
<< debugString(FooVal1->getKind()); |
| 1719 |
|
| 1720 |
Value *FooVal2 = GetFooValue(Env2); |
| 1721 |
ASSERT_THAT(FooVal2, NotNull()); |
| 1722 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal2)) |
| 1723 |
<< debugString(FooVal2->getKind()); |
| 1724 |
|
| 1725 |
EXPECT_EQ(FooVal1, FooVal2); |
| 1726 |
}); |
| 1727 |
} |
| 1728 |
|
| 1729 |
TEST_F(TopTest, UnusedTopJoinsToTop) { |
| 1730 |
std::string Code = R"( |
| 1731 |
bool makeTop(); |
| 1732 |
|
| 1733 |
void target(bool Cond, bool F) { |
| 1734 |
bool Foo = makeTop(); |
| 1735 |
// Force a new CFG block. |
| 1736 |
if (F) return; |
| 1737 |
(void)0; |
| 1738 |
/*[[p1]]*/ |
| 1739 |
|
| 1740 |
bool Zab1; |
| 1741 |
bool Zab2; |
| 1742 |
if (Cond) { |
| 1743 |
Zab1 = true; |
| 1744 |
} else { |
| 1745 |
Zab2 = true; |
| 1746 |
} |
| 1747 |
(void)0; |
| 1748 |
/*[[p2]]*/ |
| 1749 |
} |
| 1750 |
)"; |
| 1751 |
runDataflow( |
| 1752 |
Code, |
| 1753 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1754 |
const AnalysisOutputs &AO) { |
| 1755 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1756 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1757 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1758 |
|
| 1759 |
const ValueDecl *FooDecl = findValueDecl(AO.ASTCtx, "Foo"); |
| 1760 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1761 |
|
| 1762 |
auto GetFooValue = [FooDecl](const Environment &Env) { |
| 1763 |
return Env.getValue(*FooDecl); |
| 1764 |
}; |
| 1765 |
|
| 1766 |
Value *FooVal1 = GetFooValue(Env1); |
| 1767 |
ASSERT_THAT(FooVal1, NotNull()); |
| 1768 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal1)) |
| 1769 |
<< debugString(FooVal1->getKind()); |
| 1770 |
|
| 1771 |
Value *FooVal2 = GetFooValue(Env2); |
| 1772 |
ASSERT_THAT(FooVal2, NotNull()); |
| 1773 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal2)) |
| 1774 |
<< debugString(FooVal2->getKind()); |
| 1775 |
}); |
| 1776 |
} |
| 1777 |
|
| 1778 |
TEST_F(TopTest, TopUsedBeforeBranchJoinsToSameAtomicBool) { |
| 1779 |
std::string Code = R"( |
| 1780 |
bool makeTop(); |
| 1781 |
|
| 1782 |
void target(bool Cond, bool F) { |
| 1783 |
bool Foo = makeTop(); |
| 1784 |
/*[[p0]]*/ |
| 1785 |
|
| 1786 |
// Use `Top`. |
| 1787 |
bool Bar = Foo; |
| 1788 |
// Force a new CFG block. |
| 1789 |
if (F) return; |
| 1790 |
(void)0; |
| 1791 |
/*[[p1]]*/ |
| 1792 |
|
| 1793 |
bool Zab1; |
| 1794 |
bool Zab2; |
| 1795 |
if (Cond) { |
| 1796 |
Zab1 = true; |
| 1797 |
} else { |
| 1798 |
Zab2 = true; |
| 1799 |
} |
| 1800 |
(void)0; |
| 1801 |
/*[[p2]]*/ |
| 1802 |
} |
| 1803 |
)"; |
| 1804 |
runDataflow( |
| 1805 |
Code, |
| 1806 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1807 |
const AnalysisOutputs &AO) { |
| 1808 |
ASSERT_THAT(Results.keys(), |
| 1809 |
UnorderedElementsAre("p0", "p1", "p2")); |
| 1810 |
const Environment &Env0 = getEnvironmentAtAnnotation(Results, "p0"); |
| 1811 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1812 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1813 |
|
| 1814 |
const ValueDecl *FooDecl = findValueDecl(AO.ASTCtx, "Foo"); |
| 1815 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1816 |
|
| 1817 |
auto GetFooValue = [FooDecl](const Environment &Env) { |
| 1818 |
return Env.getValue(*FooDecl); |
| 1819 |
}; |
| 1820 |
|
| 1821 |
Value *FooVal0 = GetFooValue(Env0); |
| 1822 |
ASSERT_THAT(FooVal0, NotNull()); |
| 1823 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal0)) |
| 1824 |
<< debugString(FooVal0->getKind()); |
| 1825 |
|
| 1826 |
Value *FooVal1 = GetFooValue(Env1); |
| 1827 |
ASSERT_THAT(FooVal1, NotNull()); |
| 1828 |
EXPECT_TRUE(isa<AtomicBoolValue>(FooVal1)) |
| 1829 |
<< debugString(FooVal1->getKind()); |
| 1830 |
|
| 1831 |
Value *FooVal2 = GetFooValue(Env2); |
| 1832 |
ASSERT_THAT(FooVal2, NotNull()); |
| 1833 |
EXPECT_TRUE(isa<AtomicBoolValue>(FooVal2)) |
| 1834 |
<< debugString(FooVal2->getKind()); |
| 1835 |
|
| 1836 |
EXPECT_EQ(FooVal2, FooVal1); |
| 1837 |
}); |
| 1838 |
} |
| 1839 |
|
| 1840 |
TEST_F(TopTest, TopUsedInBothBranchesJoinsToAtomic) { |
| 1841 |
std::string Code = R"( |
| 1842 |
bool makeTop(); |
| 1843 |
|
| 1844 |
void target(bool Cond, bool F) { |
| 1845 |
bool Foo = makeTop(); |
| 1846 |
// Force a new CFG block. |
| 1847 |
if (F) return; |
| 1848 |
(void)0; |
| 1849 |
/*[[p1]]*/ |
| 1850 |
|
| 1851 |
bool Zab1; |
| 1852 |
bool Zab2; |
| 1853 |
if (Cond) { |
| 1854 |
Zab1 = Foo; |
| 1855 |
} else { |
| 1856 |
Zab2 = Foo; |
| 1857 |
} |
| 1858 |
(void)0; |
| 1859 |
/*[[p2]]*/ |
| 1860 |
} |
| 1861 |
)"; |
| 1862 |
runDataflow( |
| 1863 |
Code, |
| 1864 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1865 |
const AnalysisOutputs &AO) { |
| 1866 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1867 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1868 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1869 |
|
| 1870 |
const ValueDecl *FooDecl = findValueDecl(AO.ASTCtx, "Foo"); |
| 1871 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1872 |
|
| 1873 |
auto GetFooValue = [FooDecl](const Environment &Env) { |
| 1874 |
return Env.getValue(*FooDecl); |
| 1875 |
}; |
| 1876 |
|
| 1877 |
Value *FooVal1 = GetFooValue(Env1); |
| 1878 |
ASSERT_THAT(FooVal1, NotNull()); |
| 1879 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal1)) |
| 1880 |
<< debugString(FooVal1->getKind()); |
| 1881 |
|
| 1882 |
Value *FooVal2 = GetFooValue(Env2); |
| 1883 |
ASSERT_THAT(FooVal2, NotNull()); |
| 1884 |
EXPECT_TRUE(isa<AtomicBoolValue>(FooVal2)) |
| 1885 |
<< debugString(FooVal2->getKind()); |
| 1886 |
}); |
| 1887 |
} |
| 1888 |
|
| 1889 |
TEST_F(TopTest, TopUsedInBothBranchesWithoutPrecisionLoss) { |
| 1890 |
std::string Code = R"( |
| 1891 |
bool makeTop(); |
| 1892 |
|
| 1893 |
void target(bool Cond, bool F) { |
| 1894 |
bool Foo = makeTop(); |
| 1895 |
// Force a new CFG block. |
| 1896 |
if (F) return; |
| 1897 |
(void)0; |
| 1898 |
|
| 1899 |
bool Bar; |
| 1900 |
if (Cond) { |
| 1901 |
Bar = Foo; |
| 1902 |
} else { |
| 1903 |
Bar = Foo; |
| 1904 |
} |
| 1905 |
(void)0; |
| 1906 |
/*[[p]]*/ |
| 1907 |
} |
| 1908 |
)"; |
| 1909 |
runDataflow( |
| 1910 |
Code, |
| 1911 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1912 |
const AnalysisOutputs &AO) { |
| 1913 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p")); |
| 1914 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 1915 |
|
| 1916 |
const ValueDecl *FooDecl = findValueDecl(AO.ASTCtx, "Foo"); |
| 1917 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1918 |
|
| 1919 |
const ValueDecl *BarDecl = findValueDecl(AO.ASTCtx, "Bar"); |
| 1920 |
ASSERT_THAT(BarDecl, NotNull()); |
| 1921 |
|
| 1922 |
auto *FooVal = dyn_cast_or_null<BoolValue>(Env.getValue(*FooDecl)); |
| 1923 |
ASSERT_THAT(FooVal, NotNull()); |
| 1924 |
|
| 1925 |
auto *BarVal = dyn_cast_or_null<BoolValue>(Env.getValue(*BarDecl)); |
| 1926 |
ASSERT_THAT(BarVal, NotNull()); |
| 1927 |
|
| 1928 |
EXPECT_TRUE(Env.proves( |
| 1929 |
Env.arena().makeEquals(FooVal->formula(), BarVal->formula()))); |
| 1930 |
}); |
| 1931 |
} |
| 1932 |
|
| 1933 |
TEST_F(TopTest, TopUnusedBeforeLoopHeadJoinsToTop) { |
| 1934 |
std::string Code = R"( |
| 1935 |
bool makeTop(); |
| 1936 |
|
| 1937 |
void target(bool Cond, bool F) { |
| 1938 |
bool Foo = makeTop(); |
| 1939 |
// Force a new CFG block. |
| 1940 |
if (F) return; |
| 1941 |
(void)0; |
| 1942 |
/*[[p1]]*/ |
| 1943 |
|
| 1944 |
while (Cond) { |
| 1945 |
// Use `Foo`. |
| 1946 |
bool Zab = Foo; |
| 1947 |
Zab = false; |
| 1948 |
Foo = makeTop(); |
| 1949 |
} |
| 1950 |
(void)0; |
| 1951 |
/*[[p2]]*/ |
| 1952 |
} |
| 1953 |
)"; |
| 1954 |
runDataflow( |
| 1955 |
Code, |
| 1956 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 1957 |
const AnalysisOutputs &AO) { |
| 1958 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p1", "p2")); |
| 1959 |
const Environment &Env1 = getEnvironmentAtAnnotation(Results, "p1"); |
| 1960 |
const Environment &Env2 = getEnvironmentAtAnnotation(Results, "p2"); |
| 1961 |
|
| 1962 |
const ValueDecl *FooDecl = findValueDecl(AO.ASTCtx, "Foo"); |
| 1963 |
ASSERT_THAT(FooDecl, NotNull()); |
| 1964 |
|
| 1965 |
auto GetFooValue = [FooDecl](const Environment &Env) { |
| 1966 |
return Env.getValue(*FooDecl); |
| 1967 |
}; |
| 1968 |
|
| 1969 |
Value *FooVal1 = GetFooValue(Env1); |
| 1970 |
ASSERT_THAT(FooVal1, NotNull()); |
| 1971 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal1)) |
| 1972 |
<< debugString(FooVal1->getKind()); |
| 1973 |
|
| 1974 |
Value *FooVal2 = GetFooValue(Env2); |
| 1975 |
ASSERT_THAT(FooVal2, NotNull()); |
| 1976 |
EXPECT_TRUE(isa<TopBoolValue>(FooVal2)) |
| 1977 |
<< debugString(FooVal2->getKind()); |
| 1978 |
|
| 1979 |
}); |
| 1980 |
} |
| 1981 |
|
| 1982 |
TEST_F(TopTest, ForRangeStmtConverges) { |
| 1983 |
std::string Code = R"( |
| 1984 |
void target(bool Foo) { |
| 1985 |
int Ints[10]; |
| 1986 |
bool B = false; |
| 1987 |
for (int I : Ints) |
| 1988 |
B = true; |
| 1989 |
} |
| 1990 |
)"; |
| 1991 |
runDataflow(Code, |
| 1992 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &, |
| 1993 |
const AnalysisOutputs &) { |
| 1994 |
// No additional expectations. We're only checking that the |
| 1995 |
// analysis converged. |
| 1996 |
}); |
| 1997 |
} |
| 1998 |
} // namespace |