@@ -2778,56 +2778,66 @@ SourceFile::getImportAccessLevel(const ModuleDecl *targetModule) const {
27782778 return restrictiveImport;
27792779}
27802780
2781- SmallVector<CharSourceRange, 2 >
2782- IfConfigRangeInfo::getRangesWithoutActiveBody (const SourceManager &SM) const {
2783- SmallVector<CharSourceRange, 2 > result;
2784- if (ActiveBodyRange.isValid ()) {
2785- // Split the whole range by the active range.
2786- result.emplace_back (SM, WholeRange.getStart (), ActiveBodyRange.getStart ());
2787- result.emplace_back (SM, ActiveBodyRange.getEnd (), WholeRange.getEnd ());
2788- } else {
2789- // No active body, we just return the whole range.
2790- result.push_back (WholeRange);
2791- }
2792- return result;
2781+ CharSourceRange
2782+ IfConfigClauseRangeInfo::getDirectiveRange (const SourceManager &SM) const {
2783+ return CharSourceRange (SM, DirectiveLoc, BodyLoc);
27932784}
27942785
2795- void SourceFile::recordIfConfigRangeInfo (IfConfigRangeInfo ranges) {
2796- IfConfigRanges. Ranges . push_back (ranges);
2797- IfConfigRanges. IsSorted = false ;
2786+ CharSourceRange
2787+ IfConfigClauseRangeInfo::getBodyRange ( const SourceManager &SM) const {
2788+ return CharSourceRange (SM, BodyLoc, EndLoc) ;
27982789}
27992790
2800- ArrayRef<IfConfigRangeInfo>
2801- SourceFile::getIfConfigsWithin (SourceRange outer) const {
2802- auto &SM = getASTContext ().SourceMgr ;
2803- assert (SM.getRangeForBuffer (BufferID).contains (outer.Start ) &&
2804- " Range not within this file?" );
2791+ CharSourceRange
2792+ IfConfigClauseRangeInfo::getWholeRange (const SourceManager &SM) const {
2793+ return CharSourceRange (SM, DirectiveLoc, EndLoc);
2794+ }
2795+
2796+ void SourceFile::recordIfConfigClauseRangeInfo (
2797+ const IfConfigClauseRangeInfo &range) {
2798+ IfConfigClauseRanges.Ranges .push_back (range);
2799+ IfConfigClauseRanges.IsSorted = false ;
2800+ }
28052801
2806- if (!IfConfigRanges.IsSorted ) {
2802+ ArrayRef<IfConfigClauseRangeInfo> SourceFile::getIfConfigClauseRanges () const {
2803+ if (!IfConfigClauseRanges.IsSorted ) {
2804+ auto &SM = getASTContext ().SourceMgr ;
28072805 // Sort the ranges if we need to.
2808- llvm::sort (IfConfigRanges.Ranges , [&](IfConfigRangeInfo lhs,
2809- IfConfigRangeInfo rhs) {
2810- return SM.isBeforeInBuffer (lhs.getStartLoc (), rhs.getStartLoc ());
2811- });
2806+ llvm::sort (
2807+ IfConfigClauseRanges.Ranges , [&](const IfConfigClauseRangeInfo &lhs,
2808+ const IfConfigClauseRangeInfo &rhs) {
2809+ return SM.isBeforeInBuffer (lhs.getStartLoc (), rhs.getStartLoc ());
2810+ });
28122811
28132812 // Be defensive and eliminate duplicates in case we've parsed twice.
2814- auto newEnd = std ::unique (
2815- IfConfigRanges .Ranges . begin (), IfConfigRanges. Ranges . end () ,
2816- [&]( const IfConfigRangeInfo &lhs, const IfConfigRangeInfo &rhs) {
2817- if (lhs.getWholeRange () != rhs.getWholeRange ())
2813+ auto newEnd = llvm ::unique (
2814+ IfConfigClauseRanges .Ranges , [&]( const IfConfigClauseRangeInfo &lhs ,
2815+ const IfConfigClauseRangeInfo &rhs) {
2816+ if (lhs.getStartLoc () != rhs.getStartLoc ())
28182817 return false ;
2819-
2820- assert (lhs == rhs && " Active ranges changed on a re-parse?" );
2818+ assert (lhs. getBodyRange (SM) == rhs. getBodyRange (SM) &&
2819+ " range changed on a re-parse?" );
28212820 return true ;
28222821 });
2823- IfConfigRanges.Ranges .erase (newEnd, IfConfigRanges.Ranges .end ());
2824- IfConfigRanges.IsSorted = true ;
2822+ IfConfigClauseRanges.Ranges .erase (newEnd,
2823+ IfConfigClauseRanges.Ranges .end ());
2824+ IfConfigClauseRanges.IsSorted = true ;
28252825 }
28262826
2827+ return IfConfigClauseRanges.Ranges ;
2828+ }
2829+
2830+ ArrayRef<IfConfigClauseRangeInfo>
2831+ SourceFile::getIfConfigClausesWithin (SourceRange outer) const {
2832+ auto &SM = getASTContext ().SourceMgr ;
2833+ assert (SM.getRangeForBuffer (BufferID).contains (outer.Start ) &&
2834+ " Range not within this file?" );
2835+
28272836 // First let's find the first #if that is after the outer start loc.
2828- auto ranges = llvm::ArrayRef (IfConfigRanges. Ranges );
2837+ auto ranges = getIfConfigClauseRanges ( );
28292838 auto lower = llvm::lower_bound (
2830- ranges, outer.Start , [&](IfConfigRangeInfo range, SourceLoc loc) {
2839+ ranges, outer.Start ,
2840+ [&](const IfConfigClauseRangeInfo &range, SourceLoc loc) {
28312841 return SM.isBeforeInBuffer (range.getStartLoc (), loc);
28322842 });
28332843 if (lower == ranges.end () ||
@@ -2836,7 +2846,8 @@ SourceFile::getIfConfigsWithin(SourceRange outer) const {
28362846 }
28372847 // Next let's find the first #if that's after the outer end loc.
28382848 auto upper = llvm::upper_bound (
2839- ranges, outer.End , [&](SourceLoc loc, IfConfigRangeInfo range) {
2849+ ranges, outer.End ,
2850+ [&](SourceLoc loc, const IfConfigClauseRangeInfo &range) {
28402851 return SM.isBeforeInBuffer (loc, range.getStartLoc ());
28412852 });
28422853 return llvm::ArrayRef (lower, upper - lower);
0 commit comments