Skip to content

Commit

Permalink
[reformat] separate symbol resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
shenjunjiekoda committed Sep 13, 2024
1 parent 7ff5088 commit ccee48b
Show file tree
Hide file tree
Showing 14 changed files with 837 additions and 625 deletions.
63 changes: 63 additions & 0 deletions include/dfa/analysis/core/assign_resolver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===- assign_resolver.hpp --------------------------------------------===//
//
// Copyright (c) 2024 Junjie Shen
//
// see https://github.com/shenjunjiekoda/knight/blob/main/LICENSE for
// license information.
//
//===------------------------------------------------------------------===//
//
// This header defines the assign resolver.
//
//===------------------------------------------------------------------===//

#pragma once

#include "dfa/analysis/core/symbol_resolver.hpp"
#include "dfa/analysis_context.hpp"
namespace knight::dfa {

namespace internal {

constexpr unsigned AssignmentContextAlignment = 64U;

struct alignas(AssignmentContextAlignment)
AssignmentContext { // NOLINT(altera-struct-pack-align)
std::optional< const TypedRegion* > treg = std::nullopt;
std::optional< const clang::Stmt* > stmt = std::nullopt;
std::optional< const clang::Expr* > rhs_expr = std::nullopt;
SExprRef lhs_sexpr{};
SExprRef rhs_sexpr{};
clang::BinaryOperator::Opcode op = clang::BinaryOperator::Opcode::BO_Assign;
}; // struct AssignmentContext

} // namespace internal

class AssignResolver {
private:
AnalysisContext* m_ctx;
const SymbolResolver* m_sym_resolver;

public:
AssignResolver(AnalysisContext* ctx, const SymbolResolver* sym_resolver)
: m_ctx(ctx), m_sym_resolver(sym_resolver) {}

public:
[[nodiscard]] ProgramStateRef resolve(internal::AssignmentContext) const;

private:
void handle_int_assign(internal::AssignmentContext assign_ctx,
SymbolRef res_sym,
bool is_direct_assign,
clang::BinaryOperator::Opcode op,
ProgramStateRef& state,
SExprRef& binary_sexpr) const;
void handle_ptr_assign(internal::AssignmentContext assign_ctx,
SymbolRef res_sym,
bool is_direct_assign,
clang::BinaryOperator::Opcode op,
ProgramStateRef& state,
SExprRef& binary_sexpr) const;
}; // class AssignResolver

} // namespace knight::dfa
69 changes: 69 additions & 0 deletions include/dfa/analysis/core/binary_op_resolver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//===- binary_op_resolver.hpp ------------------------------------------===//
//
// Copyright (c) 2024 Junjie Shen
//
// see https://github.com/shenjunjiekoda/knight/blob/main/LICENSE for
// license information.
//
//===------------------------------------------------------------------===//
//
// This header defines the uanry op resolver.
//
//===------------------------------------------------------------------===//

#pragma once

#include "clang/AST/Expr.h"
#include "dfa/analysis/core/symbol_resolver.hpp"
#include "dfa/analysis/core/unary_op_resolver.hpp"
#include "dfa/analysis_context.hpp"

namespace knight::dfa {

namespace internal {

constexpr unsigned BinaryOperationContextAlignment = 128U;

struct alignas(BinaryOperationContextAlignment) BinaryOperationContext {
clang::BinaryOperator::Opcode op{};
// lhs_expr has value or lhs_sexpr has value
std::optional< const clang::Expr* > lhs_expr;
std::optional< SExprRef > lhs_sexpr;

// rhs_expr has value or rhs_sexpr has value
std::optional< const clang::Expr* > rhs_expr;
std::optional< SExprRef > rhs_sexpr;

clang::QualType result_type;
const clang::Stmt* result_stmt{};
}; // struct BinaryOperationContext

} // namespace internal

class BinaryOpResolver {
friend class UnaryOpResolver;

private:
AnalysisContext* m_ctx;
const SymbolResolver* m_sym_resolver;

public:
explicit BinaryOpResolver(AnalysisContext* ctx,
const SymbolResolver* sym_resolver)
: m_ctx(ctx), m_sym_resolver(sym_resolver) {}

public:
void resolve(const clang::BinaryOperator*) const;

private:
void handle_binary_operation(internal::BinaryOperationContext) const;
void handle_assign_binary_operation(internal::BinaryOperationContext) const;
void handle_int_binary_operation(internal::BinaryOperationContext) const;
void handle_int_non_assign_binary_operation(
internal::BinaryOperationContext) const;
void handle_ref_binary_operation(internal::BinaryOperationContext) const;
void handle_ptr_binary_operation(internal::BinaryOperationContext) const;

}; // class BinaryOpResolver

} // namespace knight::dfa
57 changes: 0 additions & 57 deletions include/dfa/analysis/core/symbol_resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@

namespace knight::dfa {

namespace internal {

constexpr unsigned BinaryOperationContextAlignment = 128U;
constexpr unsigned AssignmentContextAlignment = 64U;

} // namespace internal

class SymbolResolver
: public Analysis< SymbolResolver,
analyze::EvalStmt< clang::Stmt >,
Expand Down Expand Up @@ -75,62 +68,12 @@ class SymbolResolver
}

private:
struct alignas(internal::BinaryOperationContextAlignment)
BinaryOperationContext {
clang::BinaryOperator::Opcode op{};
// lhs_expr has value or lhs_sexpr has value
std::optional< const clang::Expr* > lhs_expr;
std::optional< SExprRef > lhs_sexpr;

// rhs_expr has value or rhs_sexpr has value
std::optional< const clang::Expr* > rhs_expr;
std::optional< SExprRef > rhs_sexpr;

clang::QualType result_type;
const clang::Stmt* result_stmt{};
}; // struct BinaryOperationContext

void handle_var_decl(const clang::VarDecl* var_decl,
ProgramStateRef& state,
SExprRef& stmt_sexpr) const;

void handle_int_unary_operation(const clang::UnaryOperator*) const;
void handle_ptr_unary_operation(const clang::UnaryOperator*) const;

void handle_binary_operation(BinaryOperationContext) const;
void handle_assign_binary_operation(BinaryOperationContext) const;
void handle_int_binary_operation(BinaryOperationContext) const;
void handle_int_non_assign_binary_operation(BinaryOperationContext) const;
void handle_ref_binary_operation(BinaryOperationContext) const;
void handle_ptr_binary_operation(BinaryOperationContext) const;

void handle_int_cond_op(const clang::ConditionalOperator*) const;

struct alignas(internal::AssignmentContextAlignment)
AssignmentContext { // NOLINT(altera-struct-pack-align)
std::optional< const TypedRegion* > treg = std::nullopt;
std::optional< const clang::Stmt* > stmt = std::nullopt;
std::optional< const clang::Expr* > rhs_expr = std::nullopt;
SExprRef lhs_sexpr{};
SExprRef rhs_sexpr{};
clang::BinaryOperator::Opcode op =
clang::BinaryOperator::Opcode::BO_Assign;
};

[[nodiscard]] ProgramStateRef handle_assign(AssignmentContext) const;
void handle_int_assign(AssignmentContext assign_ctx,
SymbolRef res_sym,
bool is_direct_assign,
clang::BinaryOperator::Opcode op,
ProgramStateRef& state,
SExprRef& binary_sexpr) const;
void handle_ptr_assign(AssignmentContext assign_ctx,
SymbolRef res_sym,
bool is_direct_assign,
clang::BinaryOperator::Opcode op,
ProgramStateRef& state,
SExprRef& binary_sexpr) const;

void handle_load(const clang::Expr* load_expr) const;
};

Expand Down
41 changes: 41 additions & 0 deletions include/dfa/analysis/core/unary_op_resolver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//===- unary_op_resolver.hpp ------------------------------------------===//
//
// Copyright (c) 2024 Junjie Shen
//
// see https://github.com/shenjunjiekoda/knight/blob/main/LICENSE for
// license information.
//
//===------------------------------------------------------------------===//
//
// This header defines the uanry op resolver.
//
//===------------------------------------------------------------------===//

#pragma once

#include "clang/AST/Expr.h"
#include "dfa/analysis/core/symbol_resolver.hpp"
#include "dfa/analysis_context.hpp"

namespace knight::dfa {

class UnaryOpResolver {
private:
AnalysisContext* m_ctx;
const SymbolResolver* m_sym_resolver;

public:
explicit UnaryOpResolver(AnalysisContext* ctx,
const SymbolResolver* sym_resolver)
: m_ctx(ctx), m_sym_resolver(sym_resolver) {}

public:
void resolve(const clang::UnaryOperator*) const;

private:
void handle_int_unary_operation(const clang::UnaryOperator*) const;
void handle_ptr_unary_operation(const clang::UnaryOperator*) const;

}; // class UnaryOpResolver

} // namespace knight::dfa
4 changes: 2 additions & 2 deletions include/dfa/domain/pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ namespace knight::dfa {

constexpr DomID PointerInfoID = get_domain_id(DomainKind::PointerInfo);

using PointToSet = DiscreteDom< RegionDef, DomainKind::PointToSetDomain >;
using PointToSet = DiscreteDom< RegionDefRef, DomainKind::PointToSetDomain >;
using RegionPointToDom =
MapDom< RegionRef, PointToSet, DomainKind::RegionPointToSetDomain >;
using StmtPointToDom = MapDom< internal::StmtRef,
RegionPointToDom,
DomainKind::StmtPointToSetDomain >;
using AliasToSet = DiscreteDom< RegionDef, DomainKind::AliasToDomain >;
using AliasToSet = DiscreteDom< RegionDefRef, DomainKind::AliasToDomain >;
using RegionAliasDom =
MapDom< RegionRef, AliasToSet, DomainKind::RegionAliasToDomain >;
using StmtAliasDom =
Expand Down
6 changes: 3 additions & 3 deletions include/dfa/program_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace knight::dfa {
using ProgramStateRef = llvm::IntrusiveRefCntPtr< const ProgramState >;
using DomValMap = llvm::DenseMap< DomID, SharedVal >;
using RegionDefMap = llvm::DenseMap< std::pair< RegionRef, const StackFrame* >,
const RegionSymVal* >;
const RegionDef* >;
using StmtSExprMap =
llvm::DenseMap< std::pair< ProcCFG::StmtRef, const StackFrame* >,
SExprRef >;
Expand Down Expand Up @@ -153,14 +153,14 @@ class ProgramState : public llvm::FoldingSetNode {

[[nodiscard]] ProgramStateRef set_region_def(RegionRef region,
const StackFrame* frame,
const RegionSymVal* def) const;
const RegionDef* def) const;
[[nodiscard]] ProgramStateRef set_stmt_sexpr(ProcCFG::StmtRef stmt,
const StackFrame* frame,
SExprRef sexpr) const;
[[nodiscard]] ProgramStateRef set_constraint_system(
const ConstraintSystem& cst_system) const;

[[nodiscard]] std::optional< const RegionSymVal* > get_region_def(
[[nodiscard]] std::optional< const RegionDef* > get_region_def(
RegionRef region, const StackFrame* frame) const;
[[nodiscard]] std::optional< SExprRef > get_stmt_sexpr(
ProcCFG::StmtRef stmt, const StackFrame* frame) const;
Expand Down
34 changes: 17 additions & 17 deletions include/dfa/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ bool is_valid_type_for_sym_expr(clang::QualType type);
class SymExpr;
class TypedRegion;
class Sym;
class RegionSymVal;
class RegionDef;

using SExprRef = const SymExpr*;
using SymbolRef = const Sym*;
using RegionRef = const TypedRegion*;
using RegionDef = const RegionSymVal*;
using RegionDefRef = const RegionDef*;

class SymIterator {
private:
Expand Down Expand Up @@ -194,12 +194,12 @@ class ScalarInt : public Scalar {
}; // class Integer

/// \brief A scalar region address
class ScalarRegion : public Scalar {
class RegionAddr : public Scalar {
private:
const TypedRegion* m_region;

public:
explicit ScalarRegion(const TypedRegion* region)
explicit RegionAddr(const TypedRegion* region)
: Scalar(SymExprKind::RegionSymbolVal), m_region(region) {}

[[nodiscard]] const TypedRegion* get_region() const;
Expand All @@ -221,7 +221,7 @@ class ScalarRegion : public Scalar {
}

void Profile(llvm::FoldingSetNodeID& id) const override { // NOLINT
ScalarRegion::profile(id, m_region);
RegionAddr::profile(id, m_region);
}
};

Expand Down Expand Up @@ -251,7 +251,7 @@ class Sym : public SymExpr {
}; // class Sym

/// \brief A symbol representing a region value.
class RegionSymVal : public Sym {
class RegionDef : public Sym {
private:
/// \brief The region that the symbol represents.
RegionRef m_region;
Expand All @@ -264,11 +264,11 @@ class RegionSymVal : public Sym {
bool m_is_external;

public:
RegionSymVal(SymID id,
RegionRef region,
const LocationContext* loc_ctx,
bool is_external);
~RegionSymVal() override = default;
RegionDef(SymID id,
RegionRef region,
const LocationContext* loc_ctx,
bool is_external);
~RegionDef() override = default;

[[gnu::returns_nonnull]] RegionRef get_region() const;
[[nodiscard]] bool is_external() const { return m_is_external; }
Expand All @@ -285,11 +285,11 @@ class RegionSymVal : public Sym {
}

void Profile(llvm::FoldingSetNodeID& profile) const override { // NOLINT
RegionSymVal::profile(profile,
get_id(),
m_region,
m_loc_ctx,
m_is_external);
RegionDef::profile(profile,
get_id(),
m_region,
m_loc_ctx,
m_is_external);
}

[[nodiscard]] llvm::StringRef get_kind_name() const override {
Expand All @@ -313,7 +313,7 @@ class RegionSymVal : public Sym {
return sym->get_kind() == SymExprKind::RegionSymbolVal;
}

}; // class RegionSymVal
}; // class RegionDef

class RegionSymExtent : public Sym {
private:
Expand Down
Loading

0 comments on commit ccee48b

Please sign in to comment.