reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
//===- unittests/Analysis/CFGDominatorTree.cpp - CFG tests ----------------===//
//
// 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 "CFGBuildResult.h"
#include "clang/Analysis/Analyses/Dominators.h"
#include "gtest/gtest.h"

namespace clang {
namespace analysis {
namespace {

TEST(CFGDominatorTree, DomTree) {
  const char *Code = R"(enum Kind {
                          A
                        };

                        void f() {
                          switch(Kind{}) {
                          case A:
                            break;
                          }
                        })";
  BuildResult Result = BuildCFG(Code);
  EXPECT_EQ(BuildResult::BuiltCFG, Result.getStatus());

  //  [B3 (ENTRY)]  -> [B1] -> [B2] -> [B0 (EXIT)]
  //                  switch  case A

  CFG *cfg = Result.getCFG();

  // Sanity checks.
  EXPECT_EQ(cfg->size(), 4u);

  CFGBlock *ExitBlock = *cfg->begin();
  EXPECT_EQ(ExitBlock, &cfg->getExit());

  CFGBlock *SwitchBlock = *(cfg->begin() + 1);

  CFGBlock *CaseABlock = *(cfg->begin() + 2);

  CFGBlock *EntryBlock = *(cfg->begin() + 3);
  EXPECT_EQ(EntryBlock, &cfg->getEntry());

  // Test the dominator tree.
  CFGDomTree Dom;
  Dom.buildDominatorTree(cfg);

  EXPECT_TRUE(Dom.dominates(ExitBlock, ExitBlock));
  EXPECT_FALSE(Dom.properlyDominates(ExitBlock, ExitBlock));
  EXPECT_TRUE(Dom.dominates(CaseABlock, ExitBlock));
  EXPECT_TRUE(Dom.dominates(SwitchBlock, ExitBlock));
  EXPECT_TRUE(Dom.dominates(EntryBlock, ExitBlock));

  EXPECT_TRUE(Dom.dominates(CaseABlock, CaseABlock));
  EXPECT_FALSE(Dom.properlyDominates(CaseABlock, CaseABlock));
  EXPECT_TRUE(Dom.dominates(SwitchBlock, CaseABlock));
  EXPECT_TRUE(Dom.dominates(EntryBlock, CaseABlock));

  EXPECT_TRUE(Dom.dominates(SwitchBlock, SwitchBlock));
  EXPECT_FALSE(Dom.properlyDominates(SwitchBlock, SwitchBlock));
  EXPECT_TRUE(Dom.dominates(EntryBlock, SwitchBlock));

  EXPECT_TRUE(Dom.dominates(EntryBlock, EntryBlock));
  EXPECT_FALSE(Dom.properlyDominates(EntryBlock, EntryBlock));

  // Test the post dominator tree.

  CFGPostDomTree PostDom;
  PostDom.buildDominatorTree(cfg);

  EXPECT_TRUE(PostDom.dominates(ExitBlock, EntryBlock));
  EXPECT_TRUE(PostDom.dominates(CaseABlock, EntryBlock));
  EXPECT_TRUE(PostDom.dominates(SwitchBlock, EntryBlock));
  EXPECT_TRUE(PostDom.dominates(EntryBlock, EntryBlock));
  EXPECT_FALSE(Dom.properlyDominates(EntryBlock, EntryBlock));

  EXPECT_TRUE(PostDom.dominates(ExitBlock, SwitchBlock));
  EXPECT_TRUE(PostDom.dominates(CaseABlock, SwitchBlock));
  EXPECT_TRUE(PostDom.dominates(SwitchBlock, SwitchBlock));
  EXPECT_FALSE(Dom.properlyDominates(SwitchBlock, SwitchBlock));

  EXPECT_TRUE(PostDom.dominates(ExitBlock, CaseABlock));
  EXPECT_TRUE(PostDom.dominates(CaseABlock, CaseABlock));
  EXPECT_FALSE(Dom.properlyDominates(CaseABlock, CaseABlock));

  EXPECT_TRUE(PostDom.dominates(ExitBlock, ExitBlock));
  EXPECT_FALSE(Dom.properlyDominates(ExitBlock, ExitBlock));

  // Tests for the post dominator tree's virtual root.
  EXPECT_TRUE(PostDom.dominates(nullptr, EntryBlock));
  EXPECT_TRUE(PostDom.dominates(nullptr, SwitchBlock));
  EXPECT_TRUE(PostDom.dominates(nullptr, CaseABlock));
  EXPECT_TRUE(PostDom.dominates(nullptr, ExitBlock));
}

TEST(CFGDominatorTree, ControlDependency) {
  const char *Code = R"(bool coin();

                        void funcWithBranch() {
                          int x = 0;
                          if (coin()) {
                            if (coin()) {
                              x = 5;
                            }
                            int j = 10 / x;
                            (void)j;
                          }
                        };)";
  BuildResult Result = BuildCFG(Code);
  EXPECT_EQ(BuildResult::BuiltCFG, Result.getStatus());

  //                  1st if  2nd if
  //  [B5 (ENTRY)]  -> [B4] -> [B3] -> [B2] -> [B1] -> [B0 (EXIT)]
  //                    \        \              /         /
  //                     \        ------------->         /
  //                      ------------------------------>

  CFG *cfg = Result.getCFG();

  // Sanity checks.
  EXPECT_EQ(cfg->size(), 6u);

  CFGBlock *ExitBlock = *cfg->begin();
  EXPECT_EQ(ExitBlock, &cfg->getExit());

  CFGBlock *NullDerefBlock = *(cfg->begin() + 1);

  CFGBlock *SecondThenBlock = *(cfg->begin() + 2);

  CFGBlock *SecondIfBlock = *(cfg->begin() + 3);

  CFGBlock *FirstIfBlock = *(cfg->begin() + 4);

  CFGBlock *EntryBlock = *(cfg->begin() + 5);
  EXPECT_EQ(EntryBlock, &cfg->getEntry());

  ControlDependencyCalculator Control(cfg);

  EXPECT_TRUE(Control.isControlDependent(SecondThenBlock, SecondIfBlock));
  EXPECT_TRUE(Control.isControlDependent(SecondIfBlock, FirstIfBlock));
  EXPECT_FALSE(Control.isControlDependent(NullDerefBlock, SecondIfBlock));
}

TEST(CFGDominatorTree, ControlDependencyWithLoops) {
  const char *Code = R"(int test3() {
                          int x,y,z;

                          x = y = z = 1;
                          if (x > 0) {
                            while (x >= 0){
                              while (y >= x) {
                                x = x-1;
                                y = y/2;
                              }
                            }
                          }
                          z = y;

                          return 0;
                        })";
  BuildResult Result = BuildCFG(Code);
  EXPECT_EQ(BuildResult::BuiltCFG, Result.getStatus());

  //                           <- [B2] <-
  //                          /          \
  // [B8 (ENTRY)] -> [B7] -> [B6] ---> [B5] -> [B4] -> [B3]
  //                   \       |         \              /
  //                    \      |          <-------------
  //                     \      \
  //                      --------> [B1] -> [B0 (EXIT)]

  CFG *cfg = Result.getCFG();

  ControlDependencyCalculator Control(cfg);

  auto GetBlock = [cfg] (unsigned Index) -> CFGBlock * {
    assert(Index < cfg->size());
    return *(cfg->begin() + Index);
  };

  // While not immediately obvious, the second block in fact post dominates the
  // fifth, hence B5 is not a control dependency of 2.
  EXPECT_FALSE(Control.isControlDependent(GetBlock(5), GetBlock(2)));
}


} // namespace
} // namespace analysis
} // namespace clang