diff options
author | Kristof Umann <dkszelethus@gmail.com> | 2019-07-05 09:52:00 +0000 |
---|---|---|
committer | Kristof Umann <dkszelethus@gmail.com> | 2019-07-05 09:52:00 +0000 |
commit | 8024c1cef4e177c0789a9b721ae997427325964a (patch) | |
tree | 5217db81fbe5ffcae2a46cf10fe91cd41f02b9f1 /unittests/Analysis | |
parent | dc2f89f72d6ac47fb9584e2a437350f74fb95e29 (diff) | |
download | clang-8024c1cef4e177c0789a9b721ae997427325964a.tar.gz |
[CFG] Add a new function to get the proper condition of a CFGBlock
getTerminatorCondition() returned a condition that may be outside of the
block, while the new function returns the proper one:
if (A && B && C) {}
Return C instead of A && B && C.
Differential Revision: https://reviews.llvm.org/D63538
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@365177 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Analysis')
-rw-r--r-- | unittests/Analysis/CFGTest.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/unittests/Analysis/CFGTest.cpp b/unittests/Analysis/CFGTest.cpp index 2c2522d262..69cf78b973 100644 --- a/unittests/Analysis/CFGTest.cpp +++ b/unittests/Analysis/CFGTest.cpp @@ -117,6 +117,58 @@ TEST(CFG, IsLinear) { expectLinear(true, "void foo() { foo(); }"); // Recursion is not our problem. } +TEST(CFG, ConditionExpr) { + const char *Code = R"(void f(bool A, bool B, bool C) { + if (A && B && C) + int x; + })"; + BuildResult Result = BuildCFG(Code); + EXPECT_EQ(BuildResult::BuiltCFG, Result.getStatus()); + + // [B5 (ENTRY)] -> [B4] -> [B3] -> [B2] -> [B1] -> [B0 (EXIT)] + // \ \ \ / + // -------------------------------> + + CFG *cfg = Result.getCFG(); + + auto GetBlock = [cfg] (unsigned Index) -> CFGBlock * { + assert(Index < cfg->size()); + return *(cfg->begin() + Index); + }; + + auto GetExprText = [] (const Expr *E) -> std::string { + // It's very awkward trying to recover the actual expression text without + // a real source file, so use this as a workaround. We know that the + // condition expression looks like this: + // + // ImplicitCastExpr 0xd07bf8 '_Bool' <LValueToRValue> + // `-DeclRefExpr 0xd07bd8 '_Bool' lvalue ParmVar 0xd07960 'C' '_Bool' + + assert(isa<ImplicitCastExpr>(E)); + assert(++E->child_begin() == E->child_end()); + const auto *D = dyn_cast<DeclRefExpr>(*E->child_begin()); + return D->getFoundDecl()->getNameAsString(); + }; + + EXPECT_EQ(GetBlock(1)->getLastCondition(), nullptr); + EXPECT_EQ(GetExprText(GetBlock(4)->getLastCondition()), "A"); + EXPECT_EQ(GetExprText(GetBlock(3)->getLastCondition()), "B"); + EXPECT_EQ(GetExprText(GetBlock(2)->getLastCondition()), "C"); + + //===--------------------------------------------------------------------===// + + Code = R"(void foo(int x, int y) { + (void)(x + y); + })"; + Result = BuildCFG(Code); + EXPECT_EQ(BuildResult::BuiltCFG, Result.getStatus()); + + // [B2 (ENTRY)] -> [B1] -> [B0 (EXIT)] + + cfg = Result.getCFG(); + EXPECT_EQ(GetBlock(1)->getLastCondition(), nullptr); +} + } // namespace } // namespace analysis } // namespace clang |