[AST] Eliminate IfConfigStmt

Resolves: https://bugs.swift.org/browse/SR-4426

* Make IfConfigDecl be able to hold ASTNodes
* Parse #if as IfConfigDecl
* Stop enclosing toplevel #if into TopLevelCodeDecl.
* Eliminate IfConfigStmt
This commit is contained in:
Rintaro Ishizaki
2017-05-09 11:59:55 +09:00
parent f1b0285bbc
commit 6fa84150c5
26 changed files with 196 additions and 297 deletions

View File

@@ -336,7 +336,7 @@ private:
bool searchForURL(CharSourceRange Range);
bool findFieldsInDocCommentLine(SyntaxNode Node);
bool findFieldsInDocCommentBlock(SyntaxNode Node);
bool isVisitedBeforeInIfConfigStmt(ASTNode Node) {
bool isVisitedBeforeInIfConfig(ASTNode Node) {
return VisitedNodesInsideIfConfig.count(Node) > 0;
}
};
@@ -438,7 +438,7 @@ void ModelASTWalker::visitSourceFile(SourceFile &SrcFile,
}
std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
if (isVisitedBeforeInIfConfigStmt(E))
if (isVisitedBeforeInIfConfig(E))
return {false, E};
if (E->isImplicit())
@@ -556,7 +556,7 @@ void ModelASTWalker::handleStmtCondition(StmtCondition cond) {
std::pair<bool, Stmt *> ModelASTWalker::walkToStmtPre(Stmt *S) {
if (isVisitedBeforeInIfConfigStmt(S)) {
if (isVisitedBeforeInIfConfig(S)) {
return {false, S};
}
auto addExprElem = [&](SyntaxStructureElementKind K, const Expr *Elem,
@@ -699,39 +699,6 @@ std::pair<bool, Stmt *> ModelASTWalker::walkToStmtPre(Stmt *S) {
pushStructureNode(SN, SW);
}
} else if (auto ConfigS = dyn_cast<IfConfigStmt>(S)) {
for (auto &Clause : ConfigS->getClauses()) {
unsigned TokLen;
if (&Clause == &*ConfigS->getClauses().begin())
TokLen = 3; // '#if'
else if (Clause.Cond == nullptr)
TokLen = 5; // '#else'
else
TokLen = 7; // '#elseif'
if (!passNonTokenNode({SyntaxNodeKind::BuildConfigKeyword,
CharSourceRange(Clause.Loc, TokLen) }))
return { false, nullptr };
if (Clause.Cond && !annotateIfConfigConditionIdentifiers(Clause.Cond))
return { false, nullptr };
for (auto &Element : Clause.Elements) {
if (auto *E = Element.dyn_cast<Expr*>()) {
E->walk(*this);
} else if (auto *S = Element.dyn_cast<Stmt*>()) {
S->walk(*this);
} else {
Element.get<Decl*>()->walk(*this);
}
VisitedNodesInsideIfConfig.insert(Element);
}
}
if (!ConfigS->hadMissingEnd())
if (!passNonTokenNode({ SyntaxNodeKind::BuildConfigKeyword,
CharSourceRange(ConfigS->getEndLoc(), 6/*'#endif'*/) }))
return { false, nullptr };
} else if (auto *DeferS = dyn_cast<DeferStmt>(S)) {
if (auto *FD = DeferS->getTempDecl()) {
auto *RetS = FD->getBody()->walk(*this);
@@ -752,7 +719,7 @@ Stmt *ModelASTWalker::walkToStmtPost(Stmt *S) {
}
bool ModelASTWalker::walkToDeclPre(Decl *D) {
if (isVisitedBeforeInIfConfigStmt(D))
if (isVisitedBeforeInIfConfig(D))
return false;
if (D->isImplicit())
return false;
@@ -916,9 +883,16 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
if (Clause.Cond && !annotateIfConfigConditionIdentifiers(Clause.Cond))
return false;
for (auto *D : Clause.Elements)
if (D->walk(*this))
return false;
for (auto &Element : Clause.Elements) {
if (auto *E = Element.dyn_cast<Expr*>()) {
E->walk(*this);
} else if (auto *S = Element.dyn_cast<Stmt*>()) {
S->walk(*this);
} else {
Element.get<Decl*>()->walk(*this);
}
VisitedNodesInsideIfConfig.insert(Element);
}
}
if (!ConfigD->hadMissingEnd())