Skip to content

Commit 0c9a8a8

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 ddbbb5a commit 0c9a8a8

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"
@@ -2968,76 +2967,6 @@ void SourceFile::recordIfConfigClauseRangeInfo(
29682967
#endif
29692968
}
29702969

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

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)