1 //===-------------------------- CodeRegion.cpp -----------------*- C++ -* -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements methods from the CodeRegions interface.
13 //===----------------------------------------------------------------------===//
15 #include "CodeRegion.h"
21 bool CodeRegion::isLocInRange(SMLoc Loc) const {
22 if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
24 if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
29 void CodeRegions::beginRegion(StringRef Description, SMLoc Loc) {
30 assert(!Regions.empty() && "Missing Default region");
31 const CodeRegion &CurrentRegion = *Regions.back();
32 if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
33 SM.PrintMessage(Loc, SourceMgr::DK_Warning,
34 "Ignoring invalid region start");
38 // Remove the default region if there are user defined regions.
39 if (!CurrentRegion.startLoc().isValid())
40 Regions.erase(Regions.begin());
41 addRegion(Description, Loc);
44 void CodeRegions::endRegion(SMLoc Loc) {
45 assert(!Regions.empty() && "Missing Default region");
46 CodeRegion &CurrentRegion = *Regions.back();
47 if (CurrentRegion.endLoc().isValid()) {
48 SM.PrintMessage(Loc, SourceMgr::DK_Warning, "Ignoring invalid region end");
52 CurrentRegion.setEndLocation(Loc);
55 void CodeRegions::addInstruction(std::unique_ptr<const MCInst> Instruction) {
56 const SMLoc &Loc = Instruction->getLoc();
58 std::find_if(Regions.rbegin(), Regions.rend(),
59 [Loc](const std::unique_ptr<CodeRegion> &Region) {
60 return Region->isLocInRange(Loc);
62 if (It != Regions.rend())
63 (*It)->addInstruction(std::move(Instruction));