Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang][dataflow] Add flow condition constraints to Environment
This is part of the implementation of the dataflow analysis framework. See "[RFC] A dataflow analysis framework for Clang AST" on cfe-dev. Reviewed-by: ymandel, xazax.hun Differential Revision: https://reviews.llvm.org/D120711
- Loading branch information
Showing
10 changed files
with
340 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//===-- DataflowAnalysisContext.cpp -----------------------------*- C++ -*-===// | ||
// | ||
// 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 DataflowAnalysisContext class that owns objects that | ||
// encompass the state of a program and stores context that is used during | ||
// dataflow analysis. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h" | ||
#include "clang/Analysis/FlowSensitive/Value.h" | ||
#include <cassert> | ||
#include <memory> | ||
#include <utility> | ||
|
||
namespace clang { | ||
namespace dataflow { | ||
|
||
static std::pair<BoolValue *, BoolValue *> | ||
makeCanonicalBoolValuePair(BoolValue &LHS, BoolValue &RHS) { | ||
auto Res = std::make_pair(&LHS, &RHS); | ||
if (&RHS < &LHS) | ||
std::swap(Res.first, Res.second); | ||
return Res; | ||
} | ||
|
||
BoolValue & | ||
DataflowAnalysisContext::getOrCreateConjunctionValue(BoolValue &LHS, | ||
BoolValue &RHS) { | ||
if (&LHS == &RHS) | ||
return LHS; | ||
|
||
auto Res = ConjunctionVals.try_emplace(makeCanonicalBoolValuePair(LHS, RHS), | ||
nullptr); | ||
if (Res.second) | ||
Res.first->second = | ||
&takeOwnership(std::make_unique<ConjunctionValue>(LHS, RHS)); | ||
return *Res.first->second; | ||
} | ||
|
||
BoolValue & | ||
DataflowAnalysisContext::getOrCreateDisjunctionValue(BoolValue &LHS, | ||
BoolValue &RHS) { | ||
if (&LHS == &RHS) | ||
return LHS; | ||
|
||
auto Res = DisjunctionVals.try_emplace(makeCanonicalBoolValuePair(LHS, RHS), | ||
nullptr); | ||
if (Res.second) | ||
Res.first->second = | ||
&takeOwnership(std::make_unique<DisjunctionValue>(LHS, RHS)); | ||
return *Res.first->second; | ||
} | ||
|
||
BoolValue &DataflowAnalysisContext::getOrCreateNegationValue(BoolValue &Val) { | ||
auto Res = NegationVals.try_emplace(&Val, nullptr); | ||
if (Res.second) | ||
Res.first->second = &takeOwnership(std::make_unique<NegationValue>(Val)); | ||
return *Res.first->second; | ||
} | ||
|
||
} // namespace dataflow | ||
} // namespace clang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
clang/unittests/Analysis/FlowSensitive/DataflowAnalysisContextTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//===- unittests/Analysis/FlowSensitive/DataflowAnalysisContextTest.cpp ---===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h" | ||
#include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h" | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include <memory> | ||
|
||
namespace { | ||
|
||
using namespace clang; | ||
using namespace dataflow; | ||
|
||
class DataflowAnalysisContextTest : public ::testing::Test { | ||
protected: | ||
DataflowAnalysisContextTest() | ||
: Context(std::make_unique<WatchedLiteralsSolver>()) {} | ||
|
||
DataflowAnalysisContext Context; | ||
}; | ||
|
||
TEST_F(DataflowAnalysisContextTest, | ||
CreateAtomicBoolValueReturnsDistinctValues) { | ||
auto &X = Context.createAtomicBoolValue(); | ||
auto &Y = Context.createAtomicBoolValue(); | ||
EXPECT_NE(&X, &Y); | ||
} | ||
|
||
TEST_F(DataflowAnalysisContextTest, | ||
GetOrCreateConjunctionValueReturnsSameExprGivenSameArgs) { | ||
auto &X = Context.createAtomicBoolValue(); | ||
auto &XAndX = Context.getOrCreateConjunctionValue(X, X); | ||
EXPECT_EQ(&XAndX, &X); | ||
} | ||
|
||
TEST_F(DataflowAnalysisContextTest, | ||
GetOrCreateConjunctionValueReturnsSameExprOnSubsequentCalls) { | ||
auto &X = Context.createAtomicBoolValue(); | ||
auto &Y = Context.createAtomicBoolValue(); | ||
auto &XAndY1 = Context.getOrCreateConjunctionValue(X, Y); | ||
auto &XAndY2 = Context.getOrCreateConjunctionValue(X, Y); | ||
EXPECT_EQ(&XAndY1, &XAndY2); | ||
|
||
auto &YAndX = Context.getOrCreateConjunctionValue(Y, X); | ||
EXPECT_EQ(&XAndY1, &YAndX); | ||
|
||
auto &Z = Context.createAtomicBoolValue(); | ||
auto &XAndZ = Context.getOrCreateConjunctionValue(X, Z); | ||
EXPECT_NE(&XAndY1, &XAndZ); | ||
} | ||
|
||
TEST_F(DataflowAnalysisContextTest, | ||
GetOrCreateDisjunctionValueReturnsSameExprGivenSameArgs) { | ||
auto &X = Context.createAtomicBoolValue(); | ||
auto &XOrX = Context.getOrCreateDisjunctionValue(X, X); | ||
EXPECT_EQ(&XOrX, &X); | ||
} | ||
|
||
TEST_F(DataflowAnalysisContextTest, | ||
GetOrCreateDisjunctionValueReturnsSameExprOnSubsequentCalls) { | ||
auto &X = Context.createAtomicBoolValue(); | ||
auto &Y = Context.createAtomicBoolValue(); | ||
auto &XOrY1 = Context.getOrCreateDisjunctionValue(X, Y); | ||
auto &XOrY2 = Context.getOrCreateDisjunctionValue(X, Y); | ||
EXPECT_EQ(&XOrY1, &XOrY2); | ||
|
||
auto &YOrX = Context.getOrCreateDisjunctionValue(Y, X); | ||
EXPECT_EQ(&XOrY1, &YOrX); | ||
|
||
auto &Z = Context.createAtomicBoolValue(); | ||
auto &XOrZ = Context.getOrCreateDisjunctionValue(X, Z); | ||
EXPECT_NE(&XOrY1, &XOrZ); | ||
} | ||
|
||
TEST_F(DataflowAnalysisContextTest, | ||
GetOrCreateNegationValueReturnsSameExprOnSubsequentCalls) { | ||
auto &X = Context.createAtomicBoolValue(); | ||
auto &NotX1 = Context.getOrCreateNegationValue(X); | ||
auto &NotX2 = Context.getOrCreateNegationValue(X); | ||
EXPECT_EQ(&NotX1, &NotX2); | ||
|
||
auto &Y = Context.createAtomicBoolValue(); | ||
auto &NotY = Context.getOrCreateNegationValue(Y); | ||
EXPECT_NE(&NotX1, &NotY); | ||
} | ||
|
||
} // namespace |
Oops, something went wrong.