diff --git a/clang/include/clang/Tooling/Refactoring/Rename/SymbolOccurrences.h b/clang/include/clang/Tooling/Refactoring/Rename/SymbolOccurrences.h index 3b903cb822f396..c4bfaa9cc377b0 100644 --- a/clang/include/clang/Tooling/Refactoring/Rename/SymbolOccurrences.h +++ b/clang/include/clang/Tooling/Refactoring/Rename/SymbolOccurrences.h @@ -69,17 +69,18 @@ class SymbolOccurrence { OccurrenceKind getKind() const { return Kind; } ArrayRef getNameRanges() const { - if (MultipleRanges) { - return llvm::makeArrayRef(MultipleRanges.get(), - RangeOrNumRanges.getBegin().getRawEncoding()); - } - return RangeOrNumRanges; + if (MultipleRanges) + return llvm::makeArrayRef(MultipleRanges.get(), NumRanges); + return SingleRange; } private: OccurrenceKind Kind; std::unique_ptr MultipleRanges; - SourceRange RangeOrNumRanges; + union { + SourceRange SingleRange; + unsigned NumRanges; + }; }; using SymbolOccurrences = std::vector; diff --git a/clang/lib/Basic/SourceLocation.cpp b/clang/lib/Basic/SourceLocation.cpp index fde139932c4015..6f6412028d776d 100644 --- a/clang/lib/Basic/SourceLocation.cpp +++ b/clang/lib/Basic/SourceLocation.cpp @@ -42,6 +42,14 @@ void PrettyStackTraceLoc::print(raw_ostream &OS) const { // SourceLocation //===----------------------------------------------------------------------===// +static_assert(std::is_trivially_destructible::value, + "SourceLocation must be trivially destructible because it is " + "used in unions"); + +static_assert(std::is_trivially_destructible::value, + "SourceRange must be trivially destructible because it is " + "used in unions"); + unsigned SourceLocation::getHashValue() const { return llvm::DenseMapInfo::getHashValue(ID); } diff --git a/clang/lib/Tooling/Refactoring/Rename/SymbolOccurrences.cpp b/clang/lib/Tooling/Refactoring/Rename/SymbolOccurrences.cpp index 9e69d37e81add9..762864c953d84c 100644 --- a/clang/lib/Tooling/Refactoring/Rename/SymbolOccurrences.cpp +++ b/clang/lib/Tooling/Refactoring/Rename/SymbolOccurrences.cpp @@ -21,13 +21,12 @@ SymbolOccurrence::SymbolOccurrence(const SymbolName &Name, OccurrenceKind Kind, "mismatching number of locations and lengths"); assert(!Locations.empty() && "no locations"); if (Locations.size() == 1) { - RangeOrNumRanges = SourceRange( + new (&SingleRange) SourceRange( Locations[0], Locations[0].getLocWithOffset(NamePieces[0].size())); return; } MultipleRanges = std::make_unique(Locations.size()); - RangeOrNumRanges.setBegin( - SourceLocation::getFromRawEncoding(Locations.size())); + NumRanges = Locations.size(); for (const auto &Loc : llvm::enumerate(Locations)) { MultipleRanges[Loc.index()] = SourceRange( Loc.value(),