llvm/clang/unittests/Analysis/FlowSensitive/MultiVarConstantPropagationTest.cpp

//===- unittests/Analysis/FlowSensitive/MultiVarConstantPropagation.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
//
//===----------------------------------------------------------------------===//
//
//  This file defines a simplistic version of Constant Propagation as an example
//  of a forward, monotonic dataflow analysis. The analysis tracks all
//  variables in the scope, but lacks escape analysis.
//
//===----------------------------------------------------------------------===//

#include "TestingSupport.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Stmt.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
#include "clang/Analysis/FlowSensitive/MapLattice.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Error.h"
#include "llvm/Testing/ADT/StringMapEntry.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cstdint>
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <utility>

namespace clang {
namespace dataflow {
namespace {
usingnamespaceast_matchers;

// Models the value of an expression at a program point, for all paths through
// the program.
struct ValueLattice {};

std::ostream &operator<<(std::ostream &OS, const ValueLattice &L) {}

ConstantPropagationLattice;

constexpr char kDecl[] =;
constexpr char kVar[] =;
constexpr char kInit[] =;
constexpr char kJustAssignment[] =;
constexpr char kAssignment[] =;
constexpr char kRHS[] =;

auto refToVar() {}

// N.B. This analysis is deliberately simplistic, leaving out many important
// details needed for a real analysis. Most notably, the transfer function does
// not account for the variable's address possibly escaping, which would
// invalidate the analysis. It also could be optimized to drop out-of-scope
// variables from the map.
class ConstantPropagationAnalysis
    : public DataflowAnalysis<ConstantPropagationAnalysis,
                              ConstantPropagationLattice> {};

AnalysisInputs;
AnalysisOutputs;
checkDataflow;
IsStringMapEntry;
Pair;
UnorderedElementsAre;

MATCHER_P(Var, name,
          (llvm::Twine(negation ? "isn't" : "is") + " a variable named `" +
           name + "`")
              .str()) {}

MATCHER_P(HasConstantVal, v, "") {}

MATCHER(Varies, "") {}

MATCHER_P(HoldsCPLattice, m,
          ((negation ? "doesn't hold" : "holds") +
           llvm::StringRef(" a lattice element that ") +
           ::testing::DescribeMatcher<ConstantPropagationLattice>(m, negation))
              .str()) {}

template <typename Matcher>
void RunDataflow(llvm::StringRef Code, Matcher Expectations) {}

TEST(MultiVarConstantPropagationTest, JustInit) {}

TEST(MultiVarConstantPropagationTest, Assignment) {}

TEST(MultiVarConstantPropagationTest, AssignmentCall) {}

TEST(MultiVarConstantPropagationTest, AssignmentBinOp) {}

TEST(MultiVarConstantPropagationTest, PlusAssignment) {}

TEST(MultiVarConstantPropagationTest, SameAssignmentInBranches) {}

// Verifies that the analysis tracks multiple variables simultaneously.
TEST(MultiVarConstantPropagationTest, TwoVariables) {}

TEST(MultiVarConstantPropagationTest, TwoVariablesInBranches) {}

TEST(MultiVarConstantPropagationTest, SameAssignmentInBranch) {}

TEST(MultiVarConstantPropagationTest, NewVarInBranch) {}

TEST(MultiVarConstantPropagationTest, DifferentAssignmentInBranches) {}

TEST(MultiVarConstantPropagationTest, DifferentAssignmentInBranch) {}

} // namespace
} // namespace dataflow
} // namespace clang