blob: d5d0bafe664cdf36f48acfa7785b8fe0e996b578 [file] [log] [blame]
Eugene Zelenkobc324332018-03-13 21:32:01 +00001//===- PostOrderCFGView.cpp - Post order view of CFG blocks ---------------===//
Ted Kremenek5abde7c2011-10-22 02:14:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Artyom Skrobov27720762014-09-23 08:34:41 +000010// This file implements post order view of the blocks in a CFG.
Ted Kremenek5abde7c2011-10-22 02:14:23 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/Analyses/PostOrderCFGView.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000015#include "clang/Analysis/AnalysisDeclContext.h"
16#include "clang/Analysis/CFG.h"
Ted Kremenek5abde7c2011-10-22 02:14:23 +000017
18using namespace clang;
19
Eugene Zelenkobc324332018-03-13 21:32:01 +000020void PostOrderCFGView::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000021
Artyom Skrobov27720762014-09-23 08:34:41 +000022PostOrderCFGView::PostOrderCFGView(const CFG *cfg) {
Ted Kremenek5abde7c2011-10-22 02:14:23 +000023 Blocks.reserve(cfg->getNumBlockIDs());
24 CFGBlockSet BSet(cfg);
Fangrui Song6907ce22018-07-30 19:24:48 +000025
Ted Kremenek5abde7c2011-10-22 02:14:23 +000026 for (po_iterator I = po_iterator::begin(cfg, BSet),
27 E = po_iterator::end(cfg, BSet); I != E; ++I) {
Artyom Skrobov27720762014-09-23 08:34:41 +000028 BlockOrder[*I] = Blocks.size() + 1;
Fangrui Song6907ce22018-07-30 19:24:48 +000029 Blocks.push_back(*I);
Ted Kremenek5abde7c2011-10-22 02:14:23 +000030 }
31}
32
Ted Kremenek81ce1c82011-10-24 01:32:45 +000033PostOrderCFGView *PostOrderCFGView::create(AnalysisDeclContext &ctx) {
Ted Kremenek5abde7c2011-10-22 02:14:23 +000034 const CFG *cfg = ctx.getCFG();
35 if (!cfg)
Craig Topper25542942014-05-20 04:30:07 +000036 return nullptr;
Ted Kremenek5abde7c2011-10-22 02:14:23 +000037 return new PostOrderCFGView(cfg);
38}
39
40const void *PostOrderCFGView::getTag() { static int x; return &x; }
41
42bool PostOrderCFGView::BlockOrderCompare::operator()(const CFGBlock *b1,
43 const CFGBlock *b2) const {
44 PostOrderCFGView::BlockOrderTy::const_iterator b1It = POV.BlockOrder.find(b1);
45 PostOrderCFGView::BlockOrderTy::const_iterator b2It = POV.BlockOrder.find(b2);
Fangrui Song6907ce22018-07-30 19:24:48 +000046
Ted Kremenek5abde7c2011-10-22 02:14:23 +000047 unsigned b1V = (b1It == POV.BlockOrder.end()) ? 0 : b1It->second;
48 unsigned b2V = (b2It == POV.BlockOrder.end()) ? 0 : b2It->second;
49 return b1V > b2V;
50}