Skip to content

Revert recent libSyntax commits while investigating a ASAN issue. #12727

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
merged 3 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace syntax {
class SourceFileSyntax;
class SyntaxParsingContext;
class SyntaxParsingContextRoot;
struct RawSyntaxInfo;
struct RawTokenInfo;
}

/// Discriminator for file-units.
Expand Down Expand Up @@ -1092,7 +1092,7 @@ class SourceFile final : public FileUnit {
Optional<std::vector<Token>> AllCorrectedTokens;

/// All of the raw token syntax nodes in the underlying source.
std::vector<syntax::RawSyntaxInfo> AllRawTokenSyntax;
std::vector<syntax::RawTokenInfo> AllRawTokenSyntax;

SourceFileSyntaxInfo &SyntaxInfo;

Expand Down
2 changes: 1 addition & 1 deletion include/swift/Parse/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class Lexer {
}

/// Lex a full token including leading and trailing trivia.
syntax::RawSyntaxInfo fullLex();
syntax::RawTokenInfo fullLex();

bool isKeepingComments() const {
return RetainComments == CommentRetentionMode::ReturnAsTokens;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ tokenizeWithTrivia(const LangOptions &LangOpts,
void populateTokenSyntaxMap(const LangOptions &LangOpts,
const SourceManager &SM,
unsigned BufferID,
std::vector<syntax::RawSyntaxInfo> &Result);
std::vector<syntax::RawTokenInfo> &Result);
} // end namespace swift

#endif
48 changes: 15 additions & 33 deletions include/swift/Syntax/SyntaxParsingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,12 @@

namespace swift {
class SourceFile;
class Token;

namespace syntax {

/// The handler for parser to generate libSyntax entities.
struct RawSyntaxInfo {
/// Start location of this syntax node.
SourceLoc StartLoc;

/// The number of tokens belong to the syntax node.
unsigned TokCount;

/// The raw node.
RC<RawSyntax> RawNode;
RawSyntaxInfo(SourceLoc StartLoc, RC<RawSyntax> RawNode):
RawSyntaxInfo(StartLoc, 1, RawNode) {}
RawSyntaxInfo(SourceLoc StartLoc, unsigned TokCount, RC<RawSyntax> RawNode);
template <typename SyntaxNode>
SyntaxNode makeSyntax() const { return make<SyntaxNode>(RawNode); }

template <typename RawSyntaxNode>
RC<RawSyntaxNode> getRaw() const {
return RC<RawSyntaxNode>(cast<RawSyntaxNode>(RawNode));
}
struct RawTokenInfo {
SourceLoc Loc;
RC<RawTokenSyntax> Token;
};

enum class SyntaxParsingContextKind: uint8_t {
Expand All @@ -58,12 +40,11 @@ enum class SyntaxParsingContextKind: uint8_t {
/// create syntax nodes.
class SyntaxParsingContext {
protected:
SyntaxParsingContext(SourceFile &SF, unsigned BufferID, Token &Tok);
SyntaxParsingContext(bool Enabled);
SyntaxParsingContext(SyntaxParsingContext &Another);
public:
struct ContextInfo;
ContextInfo &ContextData;
const Token &Tok;

// Add a token syntax at the given source location to the context; this
// token node can be used to build more complex syntax nodes in later call
Expand All @@ -85,10 +66,12 @@ class SyntaxParsingContext {
// of all other entity-specific contexts. This is the context Parser
// has when the parser instance is firstly created.
class SyntaxParsingContextRoot: public SyntaxParsingContext {
SourceFile &File;
public:
SyntaxParsingContextRoot(SourceFile &File, unsigned BufferID, Token &Tok):
SyntaxParsingContext(File, BufferID, Tok), File(File) {}
struct GlobalInfo;

// Contains global information of the source file under parsing.
GlobalInfo &GlobalData;
SyntaxParsingContextRoot(SourceFile &SF, unsigned BufferID);
~SyntaxParsingContextRoot();
void addTokenSyntax(SourceLoc Loc) override {};
void makeNode(SyntaxKind Kind) override {};
Expand All @@ -97,22 +80,21 @@ class SyntaxParsingContextRoot: public SyntaxParsingContext {
};
};

enum class SyntaxContextKind: uint8_t{
Expr,
Decl,
};

// The base class for contexts that are created from a parent context.
// The stack instance will set the context holder when the context
// is firstly created and reset the context holder to the parent when
// it's destructed.
class SyntaxParsingContextChild: public SyntaxParsingContext {
SyntaxParsingContext *Parent;
SyntaxParsingContext *&ContextHolder;
const SyntaxContextKind Kind;
const SyntaxKind FinalKind;
public:
SyntaxParsingContextChild(SyntaxParsingContext *&ContextHolder,
SyntaxContextKind Kind);
SyntaxKind FinalKind):
SyntaxParsingContext(*ContextHolder), Parent(ContextHolder),
ContextHolder(ContextHolder), FinalKind(FinalKind) {
ContextHolder = this;
}
~SyntaxParsingContextChild();
void makeNode(SyntaxKind Kind) override;
void addTokenSyntax(SourceLoc Loc) override;
Expand Down
4 changes: 2 additions & 2 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,15 +737,15 @@ static bool rangeContainsPlaceholderEnd(const char *CurPtr,
return false;
}

syntax::RawSyntaxInfo Lexer::fullLex() {
syntax::RawTokenInfo Lexer::fullLex() {
if (NextToken.isEscapedIdentifier()) {
LeadingTrivia.push_back(syntax::TriviaPiece::backtick());
TrailingTrivia.insert(TrailingTrivia.begin(),
syntax::TriviaPiece::backtick());
}
auto Loc = NextToken.getLoc();
auto Result = syntax::RawTokenSyntax::make(NextToken.getKind(),
OwnedString(NextToken.getText()),
OwnedString(NextToken.getText()).copy(),
syntax::SourcePresence::Present,
{LeadingTrivia}, {TrailingTrivia});
LeadingTrivia.clear();
Expand Down
4 changes: 1 addition & 3 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <algorithm>

using namespace swift;
using namespace syntax;

namespace {
/// A RAII object for deciding whether this DeclKind needs special
Expand Down Expand Up @@ -2147,8 +2146,7 @@ void Parser::delayParseFromBeginningToHere(ParserPosition BeginParserPosition,
ParserResult<Decl>
Parser::parseDecl(ParseDeclOptions Flags,
llvm::function_ref<void(Decl*)> Handler) {
SyntaxParsingContextChild DeclParsingContext(SyntaxContext,
SyntaxContextKind::Decl);

if (Tok.is(tok::pound_if)) {
auto IfConfigResult = parseIfConfig(
[&](SmallVectorImpl<ASTNode> &Decls, bool IsActive) {
Expand Down
6 changes: 2 additions & 4 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ using namespace swift::syntax;
/// \param isExprBasic Whether we're only parsing an expr-basic.
ParserResult<Expr> Parser::parseExprImpl(Diag<> Message, bool isExprBasic) {
// Start a context for creating expression syntax.
SyntaxParsingContextChild ExprParsingContext(SyntaxContext,
SyntaxContextKind::Expr);
SyntaxParsingContextChild ExprParsingContext(SyntaxContext, SyntaxKind::Expr);

// If we are parsing a refutable pattern, check to see if this is the start
// of a let/var/is pattern. If so, parse it to an UnresolvedPatternExpr and
Expand Down Expand Up @@ -1805,8 +1804,7 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
// Create a syntax node for string literal.
SyntaxContext->addTokenSyntax(Tok.getLoc());
SyntaxContext->makeNode(SyntaxKind::StringLiteralExpr);
SyntaxParsingContextChild LocalContext(SyntaxContext,
SyntaxContextKind::Expr);
SyntaxParsingContextChild LocalContext(SyntaxContext, SyntaxKind::Expr);

// FIXME: Avoid creating syntax nodes for string interpolation.
LocalContext.disable();
Expand Down
8 changes: 4 additions & 4 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
syntax::AbsolutePosition>> Tokens;
syntax::AbsolutePosition RunningPos;
do {
auto ThisToken = L.fullLex().getRaw<syntax::RawTokenSyntax>();
auto ThisToken = L.fullLex().Token;
auto ThisTokenPos = ThisToken->accumulateAbsolutePosition(RunningPos);
Tokens.push_back({ThisToken, ThisTokenPos});
} while (Tokens.back().first->isNot(tok::eof));
Expand All @@ -279,15 +279,15 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
void swift::populateTokenSyntaxMap(const LangOptions &LangOpts,
const SourceManager &SM,
unsigned BufferID,
std::vector<syntax::RawSyntaxInfo> &Result) {
std::vector<syntax::RawTokenInfo> &Result) {
if (!Result.empty())
return;
Lexer L(LangOpts, SM, BufferID, /*Diags=*/nullptr, /*InSILMode=*/false,
CommentRetentionMode::AttachToNextToken,
TriviaRetentionMode::WithTrivia);
do {
Result.emplace_back(L.fullLex());
if (Result.back().getRaw<syntax::RawTokenSyntax>()->is(tok::eof))
if (Result.back().Token->is(tok::eof))
return;
} while (true);
}
Expand Down Expand Up @@ -434,7 +434,7 @@ Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
TokReceiver(SF.shouldKeepTokens() ?
new TokenRecorder(SF) :
new ConsumeTokenReceiver()),
SyntaxContext(new syntax::SyntaxParsingContextRoot(SF, L->getBufferID(), Tok)) {
SyntaxContext(new syntax::SyntaxParsingContextRoot(SF, L->getBufferID())) {

State = PersistentState;
if (!State) {
Expand Down
Loading