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"
20 bool CodeRegion::isLocInRange(llvm::SMLoc Loc) const {
21 if (RangeEnd.isValid() && Loc.getPointer() > RangeEnd.getPointer())
23 if (RangeStart.isValid() && Loc.getPointer() < RangeStart.getPointer())
28 void CodeRegions::beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) {
29 assert(!Regions.empty() && "Missing Default region");
30 const CodeRegion &CurrentRegion = *Regions.back();
31 if (CurrentRegion.startLoc().isValid() && !CurrentRegion.endLoc().isValid()) {
32 SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
33 "Ignoring invalid region start");
37 // Remove the default region if there are user defined regions.
38 if (!CurrentRegion.startLoc().isValid())
39 Regions.erase(Regions.begin());
40 addRegion(Description, Loc);
43 void CodeRegions::endRegion(llvm::SMLoc Loc) {
44 assert(!Regions.empty() && "Missing Default region");
45 CodeRegion &CurrentRegion = *Regions.back();
46 if (CurrentRegion.endLoc().isValid()) {
47 SM.PrintMessage(Loc, llvm::SourceMgr::DK_Warning,
48 "Ignoring invalid region end");
52 CurrentRegion.setEndLocation(Loc);
55 void CodeRegions::addInstruction(const llvm::MCInst &Instruction) {
56 const llvm::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(Instruction);