llvm-cov: Simplify FunctionInstantiationSetCollector (NFC)

- Replace std::unordered_map with DenseMap
- Use std::pair instead of manually combining two unsigneds
- Assert if insert is called with invalid arguments
- Avoid an unnecessary copy of a std::vector

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218074 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Justin Bogner 2014-09-18 20:31:26 +00:00
parent 5052940c27
commit e3d674f88d

View File

@ -20,6 +20,7 @@
#include "SourceCoverageView.h" #include "SourceCoverageView.h"
#include "CoverageSummary.h" #include "CoverageSummary.h"
#include "CoverageReport.h" #include "CoverageReport.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallSet.h"
@ -37,49 +38,35 @@
#include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/PrettyStackTrace.h"
#include <functional> #include <functional>
#include <system_error> #include <system_error>
#include <unordered_map>
using namespace llvm; using namespace llvm;
using namespace coverage; using namespace coverage;
namespace { namespace {
/// \brief Distribute the functions into instantiation sets. /// \brief Distribute the functions into instantiation sets.
/// An instantiation set is a collection of functions ///
/// that have the same source code, e.g. /// An instantiation set is a collection of functions that have the same source
/// template functions specializations. /// code, ie, template functions specializations.
class FunctionInstantiationSetCollector { class FunctionInstantiationSetCollector {
ArrayRef<FunctionCoverageMapping> FunctionMappings; typedef DenseMap<std::pair<unsigned, unsigned>,
typedef uint64_t KeyType; std::vector<const FunctionCoverageMapping *>> MapT;
typedef std::vector<const FunctionCoverageMapping *> SetType; MapT InstantiatedFunctions;
std::unordered_map<uint64_t, SetType> InstantiatedFunctions;
static KeyType getKey(const CountedRegion &R) {
return uint64_t(R.LineStart) | uint64_t(R.ColumnStart) << 32;
}
public: public:
void insert(const FunctionCoverageMapping &Function, unsigned FileID) { void insert(const FunctionCoverageMapping &Function, unsigned FileID) {
KeyType Key = 0; auto I = Function.CountedRegions.begin(), E = Function.CountedRegions.end();
for (const auto &R : Function.CountedRegions) { while (I != E && I->FileID != FileID)
if (R.FileID == FileID) { ++I;
Key = getKey(R); assert(I != E && "function does not cover the given file");
break; auto &Functions = InstantiatedFunctions[I->startLoc()];
} Functions.push_back(&Function);
}
auto I = InstantiatedFunctions.find(Key);
if (I == InstantiatedFunctions.end()) {
SetType Set;
Set.push_back(&Function);
InstantiatedFunctions.insert(std::make_pair(Key, Set));
} else
I->second.push_back(&Function);
} }
std::unordered_map<KeyType, SetType>::iterator begin() { MapT::iterator begin() {
return InstantiatedFunctions.begin(); return InstantiatedFunctions.begin();
} }
std::unordered_map<KeyType, SetType>::iterator end() { MapT::iterator end() {
return InstantiatedFunctions.end(); return InstantiatedFunctions.end();
} }
}; };