Skip to content

Commit 225e562

Browse files
committed
Requestify the #if computation using SwiftIfConfig
We're not caching this now, but it lets us dodge annoying layering issues because ASTGen (where this is available) sits on top of the C++ parser, which needs to call it.
1 parent e4cf74a commit 225e562

File tree

5 files changed

+62
-22
lines changed

5 files changed

+62
-22
lines changed

include/swift/AST/ParseRequests.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ class IDEInspectionSecondPassRequest
175175
readDependencySource(const evaluator::DependencyRecorder &) const;
176176
};
177177

178+
class EvaluateIfConditionRequest
179+
: public SimpleRequest<EvaluateIfConditionRequest,
180+
std::pair<bool, bool>(SourceFile *, SourceRange conditionRange,
181+
bool shouldEvaluate),
182+
RequestFlags::Uncached> {
183+
public:
184+
using SimpleRequest::SimpleRequest;
185+
186+
private:
187+
friend SimpleRequest;
188+
189+
// Evaluation.
190+
std::pair<bool, bool> evaluate(
191+
Evaluator &evaluator, SourceFile *SF, SourceRange conditionRange,
192+
bool shouldEvaluate) const;
193+
};
194+
178195
/// The zone number for the parser.
179196
#define SWIFT_TYPEID_ZONE Parse
180197
#define SWIFT_TYPEID_HEADER "swift/AST/ParseTypeIDZone.def"

include/swift/AST/ParseTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ SWIFT_REQUEST(Parse, ParseTopLevelDeclsRequest,
3131
SWIFT_REQUEST(Parse, ExportedSourceFileRequest,
3232
void *(const SourceFile *), Cached,
3333
NoLocationInfo)
34+
SWIFT_REQUEST(Parse, EvaluateIfConditionRequest,
35+
(std::pair<bool, bool>)(SourceFile *, SourceRange, bool), Uncached,
36+
NoLocationInfo)

include/swift/Basic/SourceLoc.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class SourceRange {
163163
}
164164

165165
SWIFT_DEBUG_DUMPER(dump(const SourceManager &SM));
166+
167+
friend size_t hash_value(SourceRange range) {
168+
return llvm::hash_combine(range.Start, range.End);
169+
}
170+
171+
friend void simple_display(raw_ostream &OS, const SourceRange &loc) {
172+
// Nothing meaningful to print.
173+
}
174+
166175
};
167176

168177
/// A half-open character-based source range.

lib/Parse/ParseIfConfig.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -767,13 +767,6 @@ static Expr *findAnyLikelySimulatorEnvironmentTest(Expr *Condition) {
767767

768768
} // end anonymous namespace
769769

770-
extern "C" intptr_t swift_ASTGen_evaluatePoundIfCondition(
771-
BridgedASTContext astContext,
772-
void *_Nonnull diagEngine,
773-
BridgedStringRef sourceFileBuffer,
774-
BridgedStringRef conditionText,
775-
bool);
776-
777770
/// Call into the Swift implementation of #if condition evaluation.
778771
///
779772
/// \returns std::nullopt if the Swift implementation is not available, or
@@ -786,21 +779,10 @@ static std::optional<std::pair<bool, bool>> evaluateWithSwiftIfConfig(
786779
bool shouldEvaluate
787780
) {
788781
#if SWIFT_BUILD_SWIFT_SYNTAX
789-
// FIXME: When we migrate to SwiftParser, use the parsed syntax tree.
790-
auto &sourceMgr = parser.Context.SourceMgr;
791-
StringRef sourceFileText =
792-
sourceMgr.getEntireTextForBuffer(*parser.SF.getBufferID());
793-
StringRef conditionText =
794-
sourceMgr.extractText(Lexer::getCharSourceRangeFromSourceRange(
795-
sourceMgr, conditionRange));
796-
intptr_t evalResult = swift_ASTGen_evaluatePoundIfCondition(
797-
parser.Context, &parser.Context.Diags, sourceFileText, conditionText,
798-
shouldEvaluate
799-
);
800-
801-
bool isActive = (evalResult & 0x01) != 0;
802-
bool allowSyntaxErrors = (evalResult & 0x02) != 0;
803-
return std::pair(isActive, allowSyntaxErrors);
782+
return evaluateOrDefault(
783+
parser.Context.evaluator,
784+
EvaluateIfConditionRequest{&parser.SF, conditionRange, shouldEvaluate},
785+
std::pair(false, false));
804786
#else
805787
return std::nullopt;
806788
#endif

lib/Sema/TypeChecker.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "TypeCheckType.h"
2424
#include "CodeSynthesis.h"
2525
#include "MiscDiagnostics.h"
26+
#include "swift/AST/ASTBridging.h"
2627
#include "swift/AST/ASTWalker.h"
2728
#include "swift/AST/ASTVisitor.h"
2829
#include "swift/AST/Attr.h"
@@ -765,3 +766,31 @@ bool TypeChecker::diagnoseInvalidFunctionType(
765766

766767
return hadAnyError;
767768
}
769+
770+
extern "C" intptr_t swift_ASTGen_evaluatePoundIfCondition(
771+
BridgedASTContext astContext,
772+
void *_Nonnull diagEngine,
773+
BridgedStringRef sourceFileBuffer,
774+
BridgedStringRef conditionText,
775+
bool);
776+
777+
std::pair<bool, bool> EvaluateIfConditionRequest::evaluate(
778+
Evaluator &evaluator, SourceFile *sourceFile, SourceRange conditionRange,
779+
bool shouldEvaluate
780+
) const {
781+
// FIXME: When we migrate to SwiftParser, use the parsed syntax tree.
782+
ASTContext &ctx = sourceFile->getASTContext();
783+
auto &sourceMgr = ctx.SourceMgr;
784+
StringRef sourceFileText =
785+
sourceMgr.getEntireTextForBuffer(*sourceFile->getBufferID());
786+
StringRef conditionText =
787+
sourceMgr.extractText(Lexer::getCharSourceRangeFromSourceRange(
788+
sourceMgr, conditionRange));
789+
intptr_t evalResult = swift_ASTGen_evaluatePoundIfCondition(
790+
ctx, &ctx.Diags, sourceFileText, conditionText, shouldEvaluate
791+
);
792+
793+
bool isActive = (evalResult & 0x01) != 0;
794+
bool allowSyntaxErrors = (evalResult & 0x02) != 0;
795+
return std::pair(isActive, allowSyntaxErrors);
796+
}

0 commit comments

Comments
 (0)