Skip to content

Commit a48871d

Browse files
committed
Sink implementation of SourceFile::getIfConfigClauseRanges into Sema
This implementation depends on ASTGen, which isn't linked as part of the AST library.
1 parent 1350f41 commit a48871d

File tree

2 files changed

+71
-71
lines changed

2 files changed

+71
-71
lines changed

lib/AST/Module.cpp

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
#include "swift/Basic/SourceManager.h"
4848
#include "swift/Basic/Statistic.h"
4949
#include "swift/Basic/StringExtras.h"
50-
#include "swift/Bridging/ASTGen.h"
5150
#include "swift/Demangling/ManglingMacros.h"
5251
#include "swift/Parse/Token.h"
5352
#include "swift/Strings.h"
@@ -2970,76 +2969,6 @@ void SourceFile::recordIfConfigClauseRangeInfo(
29702969
#endif
29712970
}
29722971

2973-
ArrayRef<IfConfigClauseRangeInfo> SourceFile::getIfConfigClauseRanges() const {
2974-
#if SWIFT_BUILD_SWIFT_SYNTAX
2975-
if (!IfConfigClauseRanges.IsSorted) {
2976-
IfConfigClauseRanges.Ranges.clear();
2977-
2978-
BridgedIfConfigClauseRangeInfo *regions;
2979-
intptr_t numRegions = swift_ASTGen_configuredRegions(
2980-
getASTContext(), getExportedSourceFile(), &regions);
2981-
IfConfigClauseRanges.Ranges.reserve(numRegions);
2982-
for (intptr_t i = 0; i != numRegions; ++i)
2983-
IfConfigClauseRanges.Ranges.push_back(regions[i].unbridged());
2984-
free(regions);
2985-
2986-
IfConfigClauseRanges.IsSorted = true;
2987-
}
2988-
#else
2989-
if (!IfConfigClauseRanges.IsSorted) {
2990-
auto &SM = getASTContext().SourceMgr;
2991-
// Sort the ranges if we need to.
2992-
llvm::sort(
2993-
IfConfigClauseRanges.Ranges, [&](const IfConfigClauseRangeInfo &lhs,
2994-
const IfConfigClauseRangeInfo &rhs) {
2995-
return SM.isBeforeInBuffer(lhs.getStartLoc(), rhs.getStartLoc());
2996-
});
2997-
2998-
// Be defensive and eliminate duplicates in case we've parsed twice.
2999-
auto newEnd = llvm::unique(
3000-
IfConfigClauseRanges.Ranges, [&](const IfConfigClauseRangeInfo &lhs,
3001-
const IfConfigClauseRangeInfo &rhs) {
3002-
if (lhs.getStartLoc() != rhs.getStartLoc())
3003-
return false;
3004-
assert(lhs.getBodyRange(SM) == rhs.getBodyRange(SM) &&
3005-
"range changed on a re-parse?");
3006-
return true;
3007-
});
3008-
IfConfigClauseRanges.Ranges.erase(newEnd,
3009-
IfConfigClauseRanges.Ranges.end());
3010-
IfConfigClauseRanges.IsSorted = true;
3011-
}
3012-
#endif
3013-
3014-
return IfConfigClauseRanges.Ranges;
3015-
}
3016-
3017-
ArrayRef<IfConfigClauseRangeInfo>
3018-
SourceFile::getIfConfigClausesWithin(SourceRange outer) const {
3019-
auto &SM = getASTContext().SourceMgr;
3020-
assert(SM.getRangeForBuffer(BufferID).contains(outer.Start) &&
3021-
"Range not within this file?");
3022-
3023-
// First let's find the first #if that is after the outer start loc.
3024-
auto ranges = getIfConfigClauseRanges();
3025-
auto lower = llvm::lower_bound(
3026-
ranges, outer.Start,
3027-
[&](const IfConfigClauseRangeInfo &range, SourceLoc loc) {
3028-
return SM.isBeforeInBuffer(range.getStartLoc(), loc);
3029-
});
3030-
if (lower == ranges.end() ||
3031-
SM.isBeforeInBuffer(outer.End, lower->getStartLoc())) {
3032-
return {};
3033-
}
3034-
// Next let's find the first #if that's after the outer end loc.
3035-
auto upper = llvm::upper_bound(
3036-
ranges, outer.End,
3037-
[&](SourceLoc loc, const IfConfigClauseRangeInfo &range) {
3038-
return SM.isBeforeInBuffer(loc, range.getStartLoc());
3039-
});
3040-
return llvm::ArrayRef(lower, upper - lower);
3041-
}
3042-
30432972
void ModuleDecl::setPackageName(Identifier name) {
30442973
Package = PackageUnit::create(name, *this, getASTContext());
30452974
}

lib/Sema/TypeCheckDecl.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "swift/AST/TypeWalker.h"
4949
#include "swift/Basic/Assertions.h"
5050
#include "swift/Basic/Defer.h"
51+
#include "swift/Bridging/ASTGen.h"
5152
#include "swift/Parse/Lexer.h"
5253
#include "swift/Parse/Parser.h"
5354
#include "swift/Sema/IDETypeChecking.h"
@@ -3121,3 +3122,73 @@ LifetimeDependenceInfoRequest::evaluate(Evaluator &evaluator,
31213122
AbstractFunctionDecl *decl) const {
31223123
return LifetimeDependenceInfo::get(decl);
31233124
}
3125+
3126+
ArrayRef<IfConfigClauseRangeInfo> SourceFile::getIfConfigClauseRanges() const {
3127+
#if SWIFT_BUILD_SWIFT_SYNTAX
3128+
if (!IfConfigClauseRanges.IsSorted) {
3129+
IfConfigClauseRanges.Ranges.clear();
3130+
3131+
BridgedIfConfigClauseRangeInfo *regions;
3132+
intptr_t numRegions = swift_ASTGen_configuredRegions(
3133+
getASTContext(), getExportedSourceFile(), &regions);
3134+
IfConfigClauseRanges.Ranges.reserve(numRegions);
3135+
for (intptr_t i = 0; i != numRegions; ++i)
3136+
IfConfigClauseRanges.Ranges.push_back(regions[i].unbridged());
3137+
free(regions);
3138+
3139+
IfConfigClauseRanges.IsSorted = true;
3140+
}
3141+
#else
3142+
if (!IfConfigClauseRanges.IsSorted) {
3143+
auto &SM = getASTContext().SourceMgr;
3144+
// Sort the ranges if we need to.
3145+
llvm::sort(
3146+
IfConfigClauseRanges.Ranges, [&](const IfConfigClauseRangeInfo &lhs,
3147+
const IfConfigClauseRangeInfo &rhs) {
3148+
return SM.isBeforeInBuffer(lhs.getStartLoc(), rhs.getStartLoc());
3149+
});
3150+
3151+
// Be defensive and eliminate duplicates in case we've parsed twice.
3152+
auto newEnd = llvm::unique(
3153+
IfConfigClauseRanges.Ranges, [&](const IfConfigClauseRangeInfo &lhs,
3154+
const IfConfigClauseRangeInfo &rhs) {
3155+
if (lhs.getStartLoc() != rhs.getStartLoc())
3156+
return false;
3157+
assert(lhs.getBodyRange(SM) == rhs.getBodyRange(SM) &&
3158+
"range changed on a re-parse?");
3159+
return true;
3160+
});
3161+
IfConfigClauseRanges.Ranges.erase(newEnd,
3162+
IfConfigClauseRanges.Ranges.end());
3163+
IfConfigClauseRanges.IsSorted = true;
3164+
}
3165+
#endif
3166+
3167+
return IfConfigClauseRanges.Ranges;
3168+
}
3169+
3170+
ArrayRef<IfConfigClauseRangeInfo>
3171+
SourceFile::getIfConfigClausesWithin(SourceRange outer) const {
3172+
auto &SM = getASTContext().SourceMgr;
3173+
assert(SM.getRangeForBuffer(BufferID).contains(outer.Start) &&
3174+
"Range not within this file?");
3175+
3176+
// First let's find the first #if that is after the outer start loc.
3177+
auto ranges = getIfConfigClauseRanges();
3178+
auto lower = llvm::lower_bound(
3179+
ranges, outer.Start,
3180+
[&](const IfConfigClauseRangeInfo &range, SourceLoc loc) {
3181+
return SM.isBeforeInBuffer(range.getStartLoc(), loc);
3182+
});
3183+
if (lower == ranges.end() ||
3184+
SM.isBeforeInBuffer(outer.End, lower->getStartLoc())) {
3185+
return {};
3186+
}
3187+
// Next let's find the first #if that's after the outer end loc.
3188+
auto upper = llvm::upper_bound(
3189+
ranges, outer.End,
3190+
[&](SourceLoc loc, const IfConfigClauseRangeInfo &range) {
3191+
return SM.isBeforeInBuffer(loc, range.getStartLoc());
3192+
});
3193+
return llvm::ArrayRef(lower, upper - lower);
3194+
}

0 commit comments

Comments
 (0)