/// Combine counts of regions which cover the same area. static ArrayRef<CountedRegion> combineRegions(MutableArrayRef<CountedRegion> Regions) { if (Regions.empty()) return Regions; auto Active = Regions.begin(); auto End = Regions.end(); for (auto I = Regions.begin() + 1; I != End; ++I) { if (Active->startLoc() != I->startLoc() || Active->endLoc() != I->endLoc()) { // Shift to the next region. ++Active; if (Active != I) *Active = *I; continue; } // Merge duplicate region. // If CodeRegions and ExpansionRegions cover the same area, it's probably // a macro which is fully expanded to another macro. In that case, we need // to accumulate counts only from CodeRegions, or else the area will be // counted twice. // On the other hand, a macro may have a nested macro in its body. If the // outer macro is used several times, the ExpansionRegion for the nested // macro will also be added several times. These ExpansionRegions cover // the same source locations and have to be combined to reach the correct // value for that area. // We add counts of the regions of the same kind as the active region // to handle the both situations. if (I->Kind == Active->Kind) Active->ExecutionCount += I->ExecutionCount; } return Regions.drop_back(std::distance(++Active, End)); }
static StringRef toUTF8(UTF32 C, MutableArrayRef<UTF8> Storage) { const UTF32 *Begin32 = &C; UTF8 *Begin8 = Storage.begin(); // The case-folded output should always be a valid unicode character, so use // strict mode here. ConversionResult CR = ConvertUTF32toUTF8(&Begin32, &C + 1, &Begin8, Storage.end(), strictConversion); assert(CR == conversionOK && "Case folding produced invalid char?"); (void)CR; return StringRef(reinterpret_cast<char *>(Storage.begin()), Begin8 - Storage.begin()); }
void SILInstruction::dropAllReferences() { MutableArrayRef<Operand> PossiblyDeadOps = getAllOperands(); for (auto OpI = PossiblyDeadOps.begin(), OpE = PossiblyDeadOps.end(); OpI != OpE; ++OpI) { OpI->drop(); } // If we have a function ref inst, we need to especially drop its function // argument so that it gets a proper ref decrement. auto *FRI = dyn_cast<FunctionRefInst>(this); if (!FRI || !FRI->getReferencedFunction()) return; FRI->dropReferencedFunction(); }
/// Sort a nested sequence of regions from a single file. static void sortNestedRegions(MutableArrayRef<CountedRegion> Regions) { std::sort(Regions.begin(), Regions.end(), [](const CountedRegion &LHS, const CountedRegion &RHS) { if (LHS.startLoc() != RHS.startLoc()) return LHS.startLoc() < RHS.startLoc(); if (LHS.endLoc() != RHS.endLoc()) // When LHS completely contains RHS, we sort LHS first. return RHS.endLoc() < LHS.endLoc(); // If LHS and RHS cover the same area, we need to sort them according // to their kinds so that the most suitable region will become "active" // in combineRegions(). Because we accumulate counter values only from // regions of the same kind as the first region of the area, prefer // CodeRegion to ExpansionRegion and ExpansionRegion to SkippedRegion. static_assert(CounterMappingRegion::CodeRegion < CounterMappingRegion::ExpansionRegion && CounterMappingRegion::ExpansionRegion < CounterMappingRegion::SkippedRegion, "Unexpected order of region kind values"); return LHS.Kind < RHS.Kind; }); }
static void sortSections(MutableArrayRef<InputSection *> Vec, SortSectionPolicy K) { if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) std::stable_sort(Vec.begin(), Vec.end(), getComparator(K)); }
/// \brief Invert the 1-[0/1] mapping of diags to group into a one to many /// mapping of groups to diags in the group. static void groupDiagnostics(const std::vector<Record*> &Diags, const std::vector<Record*> &DiagGroups, std::map<std::string, GroupInfo> &DiagsInGroup) { for (unsigned i = 0, e = Diags.size(); i != e; ++i) { const Record *R = Diags[i]; DefInit *DI = dyn_cast<DefInit>(R->getValueInit("Group")); if (!DI) continue; assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" && "Note can't be in a DiagGroup"); std::string GroupName = DI->getDef()->getValueAsString("GroupName"); DiagsInGroup[GroupName].DiagsInGroup.push_back(R); } typedef SmallPtrSet<GroupInfo *, 16> GroupSetTy; GroupSetTy ImplicitGroups; // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty // groups (these are warnings that GCC supports that clang never produces). for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) { Record *Group = DiagGroups[i]; GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")]; if (Group->isAnonymous()) { if (GI.DiagsInGroup.size() > 1) ImplicitGroups.insert(&GI); } else { if (GI.ExplicitDef) assert(GI.ExplicitDef == Group); else GI.ExplicitDef = Group; } std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups"); for (unsigned j = 0, e = SubGroups.size(); j != e; ++j) GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName")); } // Assign unique ID numbers to the groups. unsigned IDNo = 0; for (std::map<std::string, GroupInfo>::iterator I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo) I->second.IDNo = IDNo; // Sort the implicit groups, so we can warn about them deterministically. SmallVector<GroupInfo *, 16> SortedGroups(ImplicitGroups.begin(), ImplicitGroups.end()); for (SmallVectorImpl<GroupInfo *>::iterator I = SortedGroups.begin(), E = SortedGroups.end(); I != E; ++I) { MutableArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup; std::sort(GroupDiags.begin(), GroupDiags.end(), beforeThanCompare); } std::sort(SortedGroups.begin(), SortedGroups.end(), beforeThanCompareGroups); // Warn about the same group being used anonymously in multiple places. for (SmallVectorImpl<GroupInfo *>::const_iterator I = SortedGroups.begin(), E = SortedGroups.end(); I != E; ++I) { ArrayRef<const Record *> GroupDiags = (*I)->DiagsInGroup; if ((*I)->ExplicitDef) { std::string Name = (*I)->ExplicitDef->getValueAsString("GroupName"); for (ArrayRef<const Record *>::const_iterator DI = GroupDiags.begin(), DE = GroupDiags.end(); DI != DE; ++DI) { const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group")); const Record *NextDiagGroup = GroupInit->getDef(); if (NextDiagGroup == (*I)->ExplicitDef) continue; SMRange InGroupRange = findSuperClassRange(*DI, "InGroup"); SmallString<64> Replacement; if (InGroupRange.isValid()) { Replacement += "InGroup<"; Replacement += (*I)->ExplicitDef->getName(); Replacement += ">"; } SMFixIt FixIt(InGroupRange, Replacement.str()); SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(), SourceMgr::DK_Error, Twine("group '") + Name + "' is referred to anonymously", None, InGroupRange.isValid() ? FixIt : ArrayRef<SMFixIt>()); SrcMgr.PrintMessage((*I)->ExplicitDef->getLoc().front(), SourceMgr::DK_Note, "group defined here"); } } else { // If there's no existing named group, we should just warn once and use // notes to list all the other cases. ArrayRef<const Record *>::const_iterator DI = GroupDiags.begin(), DE = GroupDiags.end(); assert(DI != DE && "We only care about groups with multiple uses!"); const DefInit *GroupInit = cast<DefInit>((*DI)->getValueInit("Group")); const Record *NextDiagGroup = GroupInit->getDef(); std::string Name = NextDiagGroup->getValueAsString("GroupName"); SMRange InGroupRange = findSuperClassRange(*DI, "InGroup"); SrcMgr.PrintMessage(NextDiagGroup->getLoc().front(), SourceMgr::DK_Error, Twine("group '") + Name + "' is referred to anonymously", InGroupRange); for (++DI; DI != DE; ++DI) { GroupInit = cast<DefInit>((*DI)->getValueInit("Group")); InGroupRange = findSuperClassRange(*DI, "InGroup"); SrcMgr.PrintMessage(GroupInit->getDef()->getLoc().front(), SourceMgr::DK_Note, "also referenced here", InGroupRange); } } } }