Skip to content

Adopt SwiftIfConfig library to help with #if clause handling #75014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
113 changes: 113 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "swift/AST/Attr.h"
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/IfConfigClauseRangeInfo.h"
#include "swift/AST/Stmt.h"
#endif

Expand Down Expand Up @@ -190,6 +191,75 @@ bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext,
SWIFT_NAME("getter:BridgedASTContext.majorLanguageVersion(self:)")
unsigned BridgedASTContext_majorLanguageVersion(BridgedASTContext cContext);

SWIFT_NAME("BridgedASTContext.langOptsCustomConditionSet(self:_:)")
bool BridgedASTContext_langOptsCustomConditionSet(BridgedASTContext cContext,
BridgedStringRef cName);

SWIFT_NAME("BridgedASTContext.langOptsHasFeatureNamed(self:_:)")
bool BridgedASTContext_langOptsHasFeatureNamed(BridgedASTContext cContext,
BridgedStringRef cName);

SWIFT_NAME("BridgedASTContext.langOptsHasAttributeNamed(self:_:)")
bool BridgedASTContext_langOptsHasAttributeNamed(BridgedASTContext cContext,
BridgedStringRef cName);

SWIFT_NAME("BridgedASTContext.langOptsIsActiveTargetOS(self:_:)")
bool BridgedASTContext_langOptsIsActiveTargetOS(BridgedASTContext cContext,
BridgedStringRef cName);

SWIFT_NAME("BridgedASTContext.langOptsIsActiveTargetArchitecture(self:_:)")
bool BridgedASTContext_langOptsIsActiveTargetArchitecture(BridgedASTContext cContext,
BridgedStringRef cName);

SWIFT_NAME("BridgedASTContext.langOptsIsActiveTargetEnvironment(self:_:)")
bool BridgedASTContext_langOptsIsActiveTargetEnvironment(BridgedASTContext cContext,
BridgedStringRef cName);

SWIFT_NAME("BridgedASTContext.langOptsIsActiveTargetRuntime(self:_:)")
bool BridgedASTContext_langOptsIsActiveTargetRuntime(BridgedASTContext cContext,
BridgedStringRef cName);

SWIFT_NAME("BridgedASTContext.langOptsIsActiveTargetPtrAuth(self:_:)")
bool BridgedASTContext_langOptsIsActiveTargetPtrAuth(BridgedASTContext cContext,
BridgedStringRef cName);

SWIFT_NAME("getter:BridgedASTContext.langOptsTargetPointerBitWidth(self:)")
unsigned BridgedASTContext_langOptsTargetPointerBitWidth(BridgedASTContext cContext);

SWIFT_NAME("BridgedASTContext.langOptsGetTargetAtomicBitWidths(self:_:)")
SwiftInt BridgedASTContext_langOptsGetTargetAtomicBitWidths(BridgedASTContext cContext,
SwiftInt* _Nullable * _Nonnull cComponents);

enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedEndianness : size_t {
EndianLittle,
EndianBig,
};

SWIFT_NAME("getter:BridgedASTContext.langOptsTargetEndianness(self:)")
BridgedEndianness BridgedASTContext_langOptsTargetEndianness(BridgedASTContext cContext);

SWIFT_NAME("BridgedASTContext.langOptsGetLanguageVersion(self:_:)")
SwiftInt BridgedASTContext_langOptsGetLanguageVersion(BridgedASTContext cContext,
SwiftInt* _Nullable * _Nonnull cComponents);

SWIFT_NAME("BridgedASTContext.langOptsGetCompilerVersion(self:_:)")
SwiftInt BridgedASTContext_langOptsGetCompilerVersion(BridgedASTContext cContext,
SwiftInt* _Nullable * _Nonnull cComponents);

enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedCanImportVersion : size_t {
CanImportUnversioned,
CanImportVersion,
CanImportUnderlyingVersion,
};

SWIFT_NAME("BridgedASTContext.canImport(self:importPath:location:versionKind:versionComponents:numVersionComponents:)")
bool BridgedASTContext_canImport(BridgedASTContext cContext,
BridgedStringRef importPath,
BridgedSourceLoc canImportLoc,
BridgedCanImportVersion versionKind,
const SwiftInt * _Nullable versionComponents,
SwiftInt numVersionComponents);

//===----------------------------------------------------------------------===//
// MARK: AST nodes
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1844,6 +1914,49 @@ BridgedParameterList BridgedParameterList_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cLeftParenLoc,
BridgedArrayRef cParameters, BridgedSourceLoc cRightParenLoc);

//===----------------------------------------------------------------------===//
// MARK: #if handling
//===----------------------------------------------------------------------===//

/// Bridged version of IfConfigClauseRangeInfo::ClauseKind.
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedIfConfigClauseKind : size_t {
IfConfigActive,
IfConfigInactive,
IfConfigEnd
};

/// Bridged version of IfConfigClauseRangeInfo.
struct BridgedIfConfigClauseRangeInfo {
BridgedSourceLoc directiveLoc;
BridgedSourceLoc bodyLoc;
BridgedSourceLoc endLoc;
BridgedIfConfigClauseKind kind;

#ifdef USED_IN_CPP_SOURCE
swift::IfConfigClauseRangeInfo unbridged() const {
swift::IfConfigClauseRangeInfo::ClauseKind clauseKind;
switch (kind) {
case IfConfigActive:
clauseKind = swift::IfConfigClauseRangeInfo::ActiveClause;
break;

case IfConfigInactive:
clauseKind = swift::IfConfigClauseRangeInfo::InactiveClause;
break;

case IfConfigEnd:
clauseKind = swift::IfConfigClauseRangeInfo::EndDirective;
break;
}

return swift::IfConfigClauseRangeInfo(directiveLoc.unbridged(),
bodyLoc.unbridged(),
endLoc.unbridged(),
clauseKind);
}
#endif
};

//===----------------------------------------------------------------------===//
// MARK: Plugins
//===----------------------------------------------------------------------===//
Expand Down
62 changes: 62 additions & 0 deletions include/swift/AST/IfConfigClauseRangeInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//===--- IfConfigClauseRangeInfo.h - header for #if clauses -=====---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_AST_IFCONFIGCLAUSERANGEINFO_H
#define SWIFT_AST_IFCONFIGCLAUSERANGEINFO_H

#include "swift/Basic/SourceLoc.h"

namespace swift {

class SourceManager;

/// Stores range information for a \c #if block in a SourceFile.
class IfConfigClauseRangeInfo final {
public:
enum ClauseKind {
// Active '#if', '#elseif', or '#else' clause.
ActiveClause,
// Inactive '#if', '#elseif', or '#else' clause.
InactiveClause,
// '#endif' directive.
EndDirective,
};

private:
/// Source location of '#if', '#elseif', etc.
SourceLoc DirectiveLoc;
/// Character source location of body starts.
SourceLoc BodyLoc;
/// Location of the end of the body.
SourceLoc EndLoc;

ClauseKind Kind;

public:
IfConfigClauseRangeInfo(SourceLoc DirectiveLoc, SourceLoc BodyLoc,
SourceLoc EndLoc, ClauseKind Kind)
: DirectiveLoc(DirectiveLoc), BodyLoc(BodyLoc), EndLoc(EndLoc),
Kind(Kind) {
assert(DirectiveLoc.isValid() && BodyLoc.isValid() && EndLoc.isValid());
}

SourceLoc getStartLoc() const { return DirectiveLoc; }
CharSourceRange getDirectiveRange(const SourceManager &SM) const;
CharSourceRange getWholeRange(const SourceManager &SM) const;
CharSourceRange getBodyRange(const SourceManager &SM) const;

ClauseKind getKind() const { return Kind; }
};

}

#endif /* SWIFT_AST_IFCONFIGCLAUSERANGEINFO_H */
39 changes: 1 addition & 38 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "swift/AST/ASTNode.h"
#include "swift/AST/FileUnit.h"
#include "swift/AST/IfConfigClauseRangeInfo.h"
#include "swift/AST/Import.h"
#include "swift/AST/SynthesizedFileUnit.h"
#include "swift/Basic/Debug.h"
Expand Down Expand Up @@ -48,44 +49,6 @@ enum class RestrictedImportKind {
/// Import that limits the access level of imported entities.
using ImportAccessLevel = std::optional<AttributedImport<ImportedModule>>;

/// Stores range information for a \c #if block in a SourceFile.
class IfConfigClauseRangeInfo final {
public:
enum ClauseKind {
// Active '#if', '#elseif', or '#else' clause.
ActiveClause,
// Inactive '#if', '#elseif', or '#else' clause.
InactiveClause,
// '#endif' directive.
EndDirective,
};

private:
/// Source location of '#if', '#elseif', etc.
SourceLoc DirectiveLoc;
/// Character source location of body starts.
SourceLoc BodyLoc;
/// Location of the end of the body.
SourceLoc EndLoc;

ClauseKind Kind;

public:
IfConfigClauseRangeInfo(SourceLoc DirectiveLoc, SourceLoc BodyLoc,
SourceLoc EndLoc, ClauseKind Kind)
: DirectiveLoc(DirectiveLoc), BodyLoc(BodyLoc), EndLoc(EndLoc),
Kind(Kind) {
assert(DirectiveLoc.isValid() && BodyLoc.isValid() && EndLoc.isValid());
}

SourceLoc getStartLoc() const { return DirectiveLoc; }
CharSourceRange getDirectiveRange(const SourceManager &SM) const;
CharSourceRange getWholeRange(const SourceManager &SM) const;
CharSourceRange getBodyRange(const SourceManager &SM) const;

ClauseKind getKind() const { return Kind; }
};

/// A file containing Swift source code.
///
/// This is a .swift or .sil file (or a virtual file, such as the contents of
Expand Down
11 changes: 11 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -730,18 +730,23 @@ namespace swift {
switch (maxWidth) {
case 128:
AtomicBitWidths.emplace_back("_128");
AtomicBitWidthValues.push_back(128);
LLVM_FALLTHROUGH;
case 64:
AtomicBitWidths.emplace_back("_64");
AtomicBitWidthValues.push_back(64);
LLVM_FALLTHROUGH;
case 32:
AtomicBitWidths.emplace_back("_32");
AtomicBitWidthValues.push_back(32);
LLVM_FALLTHROUGH;
case 16:
AtomicBitWidths.emplace_back("_16");
AtomicBitWidthValues.push_back(16);
LLVM_FALLTHROUGH;
case 8:
AtomicBitWidths.emplace_back("_8");
AtomicBitWidthValues.push_back(8);
break;
default:
return;
Expand All @@ -751,6 +756,11 @@ namespace swift {
/// Removes all atomic bit widths.
void clearAtomicBitWidths() {
AtomicBitWidths.clear();
AtomicBitWidthValues.clear();
}

llvm::ArrayRef<unsigned> getAtomicBitWidthValues() const {
return AtomicBitWidthValues;
}

/// Returns true if the given platform condition argument represents
Expand Down Expand Up @@ -791,6 +801,7 @@ namespace swift {

private:
llvm::SmallVector<std::string, 2> AtomicBitWidths;
llvm::SmallVector<unsigned, 2> AtomicBitWidthValues;
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 10>
PlatformConditionValues;
llvm::SmallVector<std::string, 2> CustomConditionalCompilationFlags;
Expand Down
7 changes: 7 additions & 0 deletions include/swift/Bridging/ASTGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ bool swift_ASTGen_parseRegexLiteral(BridgedStringRef inputPtr,
BridgedSourceLoc diagLoc,
BridgedDiagnosticEngine diagEngine);

intptr_t swift_ASTGen_configuredRegions(
BridgedASTContext astContext,
void *_Nonnull sourceFile,
BridgedIfConfigClauseRangeInfo *_Nullable *_Nonnull);
void swift_ASTGen_freeConfiguredRegions(
BridgedIfConfigClauseRangeInfo *_Nullable regions, intptr_t numRegions);

#ifdef __cplusplus
}
#endif
Loading