PostOrderCFGView.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //===- PostOrderCFGView.cpp - Post order view of CFG blocks ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements post order view of the blocks in a CFG.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Analysis/Analyses/PostOrderCFGView.h"
  13. #include "clang/Analysis/AnalysisDeclContext.h"
  14. #include "clang/Analysis/CFG.h"
  15. using namespace clang;
  16. void PostOrderCFGView::anchor() {}
  17. PostOrderCFGView::PostOrderCFGView(const CFG *cfg) {
  18. Blocks.reserve(cfg->getNumBlockIDs());
  19. CFGBlockSet BSet(cfg);
  20. for (po_iterator I = po_iterator::begin(cfg, BSet),
  21. E = po_iterator::end(cfg, BSet); I != E; ++I) {
  22. BlockOrder[*I] = Blocks.size() + 1;
  23. Blocks.push_back(*I);
  24. }
  25. }
  26. std::unique_ptr<PostOrderCFGView>
  27. PostOrderCFGView::create(AnalysisDeclContext &ctx) {
  28. const CFG *cfg = ctx.getCFG();
  29. if (!cfg)
  30. return nullptr;
  31. return std::make_unique<PostOrderCFGView>(cfg);
  32. }
  33. const void *PostOrderCFGView::getTag() { static int x; return &x; }
  34. bool PostOrderCFGView::BlockOrderCompare::operator()(const CFGBlock *b1,
  35. const CFGBlock *b2) const {
  36. PostOrderCFGView::BlockOrderTy::const_iterator b1It = POV.BlockOrder.find(b1);
  37. PostOrderCFGView::BlockOrderTy::const_iterator b2It = POV.BlockOrder.find(b2);
  38. unsigned b1V = (b1It == POV.BlockOrder.end()) ? 0 : b1It->second;
  39. unsigned b2V = (b2It == POV.BlockOrder.end()) ? 0 : b2It->second;
  40. return b1V > b2V;
  41. }