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

//===- unittests/Analysis/FlowSensitive/SingleVarConstantPropagation.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 only tracks one
// variable at a time -- the one with the most recent declaration encountered.
//
//===----------------------------------------------------------------------===//

#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 "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Error.h"
#include "llvm/Testing/ADT/StringMapEntry.h"
#include "llvm/Testing/Annotations/Annotations.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;

// A semi-lattice for dataflow analysis that tracks the value of a single
// integer variable. If it can be identified with a single (constant) value,
// then that value is stored.
struct ConstantPropagationLattice {};

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

} // namespace

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

static auto refToVar() {}

namespace {
// N.B. This analysis is deliberately simplistic, leaving out many important
// details needed for a real analysis in production. Most notably, the transfer
// function does not account for the variable's address possibly escaping, which
// would invalidate the analysis.
class ConstantPropagationAnalysis
    : public DataflowAnalysis<ConstantPropagationAnalysis,
                              ConstantPropagationLattice> {};

AnalysisInputs;
AnalysisOutputs;
checkDataflow;
IsStringMapEntry;
UnorderedElementsAre;

MATCHER_P(HasConstantVal, v, "") {}

MATCHER(IsUnknown, "") {}
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(ConstantPropagationTest, JustInit) {}

// Verifies that the analysis tracks the last variable seen.
TEST(ConstantPropagationTest, TwoVariables) {}

TEST(ConstantPropagationTest, Assignment) {}

TEST(ConstantPropagationTest, AssignmentCall) {}

TEST(ConstantPropagationTest, AssignmentBinOp) {}

TEST(ConstantPropagationTest, PlusAssignment) {}

TEST(ConstantPropagationTest, SameAssignmentInBranches) {}

TEST(ConstantPropagationTest, SameAssignmentInBranch) {}

TEST(ConstantPropagationTest, NewVarInBranch) {}

TEST(ConstantPropagationTest, DifferentAssignmentInBranches) {}

TEST(ConstantPropagationTest, DifferentAssignmentInBranch) {}

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