Merge remote-tracking branch 'origin/master' into master-next

This commit is contained in:
swift-ci
2018-02-13 16:29:32 -08:00
16 changed files with 64 additions and 37 deletions

View File

@@ -888,7 +888,8 @@ public:
ASTStage_t ASTStage = Parsing; ASTStage_t ASTStage = Parsing;
SourceFile(ModuleDecl &M, SourceFileKind K, Optional<unsigned> bufferID, SourceFile(ModuleDecl &M, SourceFileKind K, Optional<unsigned> bufferID,
ImplicitModuleImportKind ModImpKind, bool KeepSyntaxInfo); ImplicitModuleImportKind ModImpKind, bool KeepParsedTokens = false,
bool KeepSyntaxTree = false);
void void
addImports(ArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptions>> IM); addImports(ArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptions>> IM);
@@ -1077,7 +1078,9 @@ public:
ArrayRef<Token> getAllTokens() const; ArrayRef<Token> getAllTokens() const;
bool shouldKeepSyntaxInfo() const; bool shouldCollectToken() const;
bool shouldBuildSyntaxTree() const;
syntax::SourceFileSyntax getSyntaxRoot() const; syntax::SourceFileSyntax getSyntaxRoot() const;
void setSyntaxRoot(syntax::SourceFileSyntax &&Root); void setSyntaxRoot(syntax::SourceFileSyntax &&Root);

View File

@@ -248,9 +248,11 @@ namespace swift {
/// This is used to guard preemptive testing for the fix-it. /// This is used to guard preemptive testing for the fix-it.
bool FixStringToSubstringConversions = false; bool FixStringToSubstringConversions = false;
/// Whether to create and keep track of a libSyntax tree associated with /// Whether collect tokens during parsing for syntax coloring.
/// this source file. bool CollectParsedToken = false;
bool KeepSyntaxInfoInSourceFile = false;
/// Whether to parse syntax tree.
bool BuildSyntaxTree = false;
/// Whether to verify the parsed syntax tree and emit related diagnostics. /// Whether to verify the parsed syntax tree and emit related diagnostics.
bool VerifySyntaxTree = false; bool VerifySyntaxTree = false;

View File

@@ -201,11 +201,11 @@ public:
Token Tok; Token Tok;
/// \brief leading trivias for \c Tok. /// \brief leading trivias for \c Tok.
/// Always empty if !SF.shouldKeepSyntaxInfo(). /// Always empty if !SF.shouldBuildSyntaxTree().
syntax::Trivia LeadingTrivia; syntax::Trivia LeadingTrivia;
/// \brief trailing trivias for \c Tok. /// \brief trailing trivias for \c Tok.
/// Always empty if !SF.shouldKeepSyntaxInfo(). /// Always empty if !SF.shouldBuildSyntaxTree().
syntax::Trivia TrailingTrivia; syntax::Trivia TrailingTrivia;
/// \brief The receiver to collect all consumed tokens. /// \brief The receiver to collect all consumed tokens.

View File

@@ -751,8 +751,10 @@ public:
}; };
struct SourceFile::SourceFileSyntaxInfo { struct SourceFile::SourceFileSyntaxInfo {
const bool Enable;
/// The root of the syntax tree representing the source file. /// The root of the syntax tree representing the source file.
Optional<syntax::SourceFileSyntax> SyntaxRoot; Optional<syntax::SourceFileSyntax> SyntaxRoot;
SourceFileSyntaxInfo(bool Enable): Enable(Enable) {}
}; };
bool SourceFile::hasSyntaxRoot() const { bool SourceFile::hasSyntaxRoot() const {
@@ -1328,10 +1330,10 @@ static void performAutoImport(
SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K, SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
Optional<unsigned> bufferID, Optional<unsigned> bufferID,
ImplicitModuleImportKind ModImpKind, ImplicitModuleImportKind ModImpKind,
bool KeepSyntaxInfo) bool KeepParsedTokens, bool BuildSyntaxTree)
: FileUnit(FileUnitKind::Source, M), : FileUnit(FileUnitKind::Source, M),
BufferID(bufferID ? *bufferID : -1), BufferID(bufferID ? *bufferID : -1),
Kind(K), SyntaxInfo(*new SourceFileSyntaxInfo()) { Kind(K), SyntaxInfo(*new SourceFileSyntaxInfo(BuildSyntaxTree)) {
M.getASTContext().addDestructorCleanup(*this); M.getASTContext().addDestructorCleanup(*this);
performAutoImport(*this, ModImpKind); performAutoImport(*this, ModImpKind);
@@ -1340,7 +1342,7 @@ SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
assert(!problem && "multiple main files?"); assert(!problem && "multiple main files?");
(void)problem; (void)problem;
} }
if (KeepSyntaxInfo) { if (KeepParsedTokens) {
AllCorrectedTokens = std::vector<Token>(); AllCorrectedTokens = std::vector<Token>();
} }
} }
@@ -1348,16 +1350,16 @@ SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
SourceFile::~SourceFile() { delete &SyntaxInfo; } SourceFile::~SourceFile() { delete &SyntaxInfo; }
std::vector<Token> &SourceFile::getTokenVector() { std::vector<Token> &SourceFile::getTokenVector() {
assert(shouldKeepSyntaxInfo() && "Disabled"); assert(shouldCollectToken() && "Disabled");
return *AllCorrectedTokens; return *AllCorrectedTokens;
} }
ArrayRef<Token> SourceFile::getAllTokens() const { ArrayRef<Token> SourceFile::getAllTokens() const {
assert(shouldKeepSyntaxInfo() && "Disabled"); assert(shouldCollectToken() && "Disabled");
return *AllCorrectedTokens; return *AllCorrectedTokens;
} }
bool SourceFile::shouldKeepSyntaxInfo() const { bool SourceFile::shouldCollectToken() const {
switch (Kind) { switch (Kind) {
case SourceFileKind::Library: case SourceFileKind::Library:
case SourceFileKind::Main: case SourceFileKind::Main:
@@ -1368,6 +1370,17 @@ bool SourceFile::shouldKeepSyntaxInfo() const {
} }
} }
bool SourceFile::shouldBuildSyntaxTree() const {
switch (Kind) {
case SourceFileKind::Library:
case SourceFileKind::Main:
return SyntaxInfo.Enable;
case SourceFileKind::REPL:
case SourceFileKind::SIL:
return false;
}
}
bool FileUnit::walk(ASTWalker &walker) { bool FileUnit::walk(ASTWalker &walker) {
SmallVector<Decl *, 64> Decls; SmallVector<Decl *, 64> Decls;
getTopLevelDecls(Decls); getTopLevelDecls(Decls);

View File

@@ -747,9 +747,10 @@ SourceFile *CompilerInstance::createSourceFileForMainModule(
SourceFileKind fileKind, SourceFile::ImplicitModuleImportKind importKind, SourceFileKind fileKind, SourceFile::ImplicitModuleImportKind importKind,
Optional<unsigned> bufferID) { Optional<unsigned> bufferID) {
ModuleDecl *mainModule = getMainModule(); ModuleDecl *mainModule = getMainModule();
bool keepSyntaxInfo = Invocation.getLangOptions().KeepSyntaxInfoInSourceFile;
SourceFile *inputFile = new (*Context) SourceFile *inputFile = new (*Context)
SourceFile(*mainModule, fileKind, bufferID, importKind, keepSyntaxInfo); SourceFile(*mainModule, fileKind, bufferID, importKind,
Invocation.getLangOptions().CollectParsedToken,
Invocation.getLangOptions().BuildSyntaxTree);
MainModule->addFile(*inputFile); MainModule->addFile(*inputFile);
if (bufferID && isPrimaryInput(*bufferID)) { if (bufferID && isPrimaryInput(*bufferID)) {

View File

@@ -838,7 +838,7 @@ static bool performCompile(CompilerInstance &Instance,
FrontendOptions::ActionType Action = opts.RequestedAction; FrontendOptions::ActionType Action = opts.RequestedAction;
if (Action == FrontendOptions::ActionType::EmitSyntax) { if (Action == FrontendOptions::ActionType::EmitSyntax) {
Instance.getASTContext().LangOpts.KeepSyntaxInfoInSourceFile = true; Instance.getASTContext().LangOpts.BuildSyntaxTree = true;
Instance.getASTContext().LangOpts.VerifySyntaxTree = true; Instance.getASTContext().LangOpts.VerifySyntaxTree = true;
} }

View File

@@ -336,7 +336,7 @@ Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
SF.getASTContext().LangOpts.AttachCommentsToDecls SF.getASTContext().LangOpts.AttachCommentsToDecls
? CommentRetentionMode::AttachToNextToken ? CommentRetentionMode::AttachToNextToken
: CommentRetentionMode::None, : CommentRetentionMode::None,
SF.shouldKeepSyntaxInfo() SF.shouldBuildSyntaxTree()
? TriviaRetentionMode::WithTrivia ? TriviaRetentionMode::WithTrivia
: TriviaRetentionMode::WithoutTrivia)), : TriviaRetentionMode::WithoutTrivia)),
SF, SIL, PersistentState) {} SF, SIL, PersistentState) {}
@@ -465,7 +465,7 @@ Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
SIL(SIL), SIL(SIL),
CurDeclContext(&SF), CurDeclContext(&SF),
Context(SF.getASTContext()), Context(SF.getASTContext()),
TokReceiver(SF.shouldKeepSyntaxInfo() ? TokReceiver(SF.shouldCollectToken() ?
new TokenRecorder(SF) : new TokenRecorder(SF) :
new ConsumeTokenReceiver()), new ConsumeTokenReceiver()),
SyntaxContext(new SyntaxParsingContext(SyntaxContext, SF, SyntaxContext(new SyntaxParsingContext(SyntaxContext, SF,
@@ -982,7 +982,8 @@ struct ParserUnit::Implementation {
*ModuleDecl::create(Ctx.getIdentifier(ModuleName), Ctx), *ModuleDecl::create(Ctx.getIdentifier(ModuleName), Ctx),
SourceFileKind::Main, BufferID, SourceFileKind::Main, BufferID,
SourceFile::ImplicitModuleImportKind::None, SourceFile::ImplicitModuleImportKind::None,
Opts.KeepSyntaxInfoInSourceFile)) { Opts.CollectParsedToken,
Opts.BuildSyntaxTree)) {
} }
}; };

View File

@@ -39,7 +39,7 @@ SyntaxParsingContext::SyntaxParsingContext(SyntaxParsingContext *&CtxtHolder,
BufferID)), BufferID)),
CtxtHolder(CtxtHolder), Arena(SF.getASTContext().getSyntaxArena()), CtxtHolder(CtxtHolder), Arena(SF.getASTContext().getSyntaxArena()),
Storage(getRootData().Storage), Offset(0), Mode(AccumulationMode::Root), Storage(getRootData().Storage), Offset(0), Mode(AccumulationMode::Root),
Enabled(SF.shouldKeepSyntaxInfo()) { Enabled(SF.shouldBuildSyntaxTree()) {
CtxtHolder = this; CtxtHolder = this;
Storage.reserve(128); Storage.reserve(128);
} }

View File

@@ -132,7 +132,8 @@ ModuleDecl *SourceLoader::loadModule(SourceLoc importLoc,
auto *importFile = new (Ctx) SourceFile(*importMod, SourceFileKind::Library, auto *importFile = new (Ctx) SourceFile(*importMod, SourceFileKind::Library,
bufferID, implicitImportKind, bufferID, implicitImportKind,
Ctx.LangOpts.KeepSyntaxInfoInSourceFile); Ctx.LangOpts.CollectParsedToken,
Ctx.LangOpts.BuildSyntaxTree);
importMod->addFile(*importFile); importMod->addFile(*importFile);
bool done; bool done;

View File

@@ -442,7 +442,7 @@ bool SwiftASTManager::initCompilerInvocation(CompilerInvocation &Invocation,
Invocation.setSerializedDiagnosticsPath(StringRef()); Invocation.setSerializedDiagnosticsPath(StringRef());
Invocation.getLangOptions().AttachCommentsToDecls = true; Invocation.getLangOptions().AttachCommentsToDecls = true;
Invocation.getLangOptions().DiagnosticsEditorMode = true; Invocation.getLangOptions().DiagnosticsEditorMode = true;
Invocation.getLangOptions().KeepSyntaxInfoInSourceFile = true; Invocation.getLangOptions().CollectParsedToken = true;
auto &FrontendOpts = Invocation.getFrontendOptions(); auto &FrontendOpts = Invocation.getFrontendOptions();
if (FrontendOpts.PlaygroundTransform) { if (FrontendOpts.PlaygroundTransform) {
// The playground instrumenter changes the AST in ways that disrupt the // The playground instrumenter changes the AST in ways that disrupt the
@@ -817,7 +817,7 @@ ASTUnitRef ASTProducer::createASTUnit(SwiftASTManager::Implementation &MgrImpl,
InvokRef->Impl.Opts.applyToSubstitutingInputs( InvokRef->Impl.Opts.applyToSubstitutingInputs(
Invocation, convertFileContentsToInputs(Contents)); Invocation, convertFileContentsToInputs(Contents));
Invocation.getLangOptions().KeepSyntaxInfoInSourceFile = true; Invocation.getLangOptions().CollectParsedToken = true;
if (CompIns.setup(Invocation)) { if (CompIns.setup(Invocation)) {
// FIXME: Report the diagnostic. // FIXME: Report the diagnostic.

View File

@@ -1753,7 +1753,7 @@ void SwiftEditorDocument::updateSemaInfo() {
} }
void SwiftEditorDocument::parse(ImmutableTextSnapshotRef Snapshot, void SwiftEditorDocument::parse(ImmutableTextSnapshotRef Snapshot,
SwiftLangSupport &Lang) { SwiftLangSupport &Lang, bool BuildSyntexTree) {
llvm::sys::ScopedLock L(Impl.AccessMtx); llvm::sys::ScopedLock L(Impl.AccessMtx);
assert(Impl.SemanticInfo && "Impl.SemanticInfo must be set"); assert(Impl.SemanticInfo && "Impl.SemanticInfo must be set");
@@ -1773,7 +1773,7 @@ void SwiftEditorDocument::parse(ImmutableTextSnapshotRef Snapshot,
Lang.getASTManager(). Lang.getASTManager().
initCompilerInvocation(CompInv, Args, StringRef(), Error); initCompilerInvocation(CompInv, Args, StringRef(), Error);
} }
CompInv.getLangOptions().BuildSyntaxTree = BuildSyntexTree;
// Access to Impl.SyntaxInfo is guarded by Impl.AccessMtx // Access to Impl.SyntaxInfo is guarded by Impl.AccessMtx
Impl.SyntaxInfo.reset( Impl.SyntaxInfo.reset(
new SwiftDocumentSyntaxInfo(CompInv, Snapshot, Args, Impl.FilePath)); new SwiftDocumentSyntaxInfo(CompInv, Snapshot, Args, Impl.FilePath));
@@ -2095,12 +2095,12 @@ void SwiftLangSupport::editorOpen(StringRef Name, llvm::MemoryBuffer *Buf,
ArrayRef<const char *> Args) { ArrayRef<const char *> Args) {
ImmutableTextSnapshotRef Snapshot = nullptr; ImmutableTextSnapshotRef Snapshot = nullptr;
const bool BuildSyntaxTree = Consumer.syntaxTreeEnabled();
auto EditorDoc = EditorDocuments.getByUnresolvedName(Name); auto EditorDoc = EditorDocuments.getByUnresolvedName(Name);
if (!EditorDoc) { if (!EditorDoc) {
EditorDoc = new SwiftEditorDocument(Name, *this); EditorDoc = new SwiftEditorDocument(Name, *this);
Snapshot = EditorDoc->initializeText(Buf, Args); Snapshot = EditorDoc->initializeText(Buf, Args);
EditorDoc->parse(Snapshot, *this); EditorDoc->parse(Snapshot, *this, BuildSyntaxTree);
if (EditorDocuments.getOrUpdate(Name, *this, EditorDoc)) { if (EditorDocuments.getOrUpdate(Name, *this, EditorDoc)) {
// Document already exists, re-initialize it. This should only happen // Document already exists, re-initialize it. This should only happen
// if we get OPEN request while the previous document is not closed. // if we get OPEN request while the previous document is not closed.
@@ -2113,7 +2113,7 @@ void SwiftLangSupport::editorOpen(StringRef Name, llvm::MemoryBuffer *Buf,
if (!Snapshot) { if (!Snapshot) {
Snapshot = EditorDoc->initializeText(Buf, Args); Snapshot = EditorDoc->initializeText(Buf, Args);
EditorDoc->parse(Snapshot, *this); EditorDoc->parse(Snapshot, *this, BuildSyntaxTree);
} }
if (Consumer.needsSemanticInfo()) { if (Consumer.needsSemanticInfo()) {
@@ -2161,7 +2161,7 @@ void SwiftLangSupport::editorReplaceText(StringRef Name, llvm::MemoryBuffer *Buf
Snapshot = EditorDoc->replaceText(Offset, Length, Buf, Snapshot = EditorDoc->replaceText(Offset, Length, Buf,
Consumer.needsSemanticInfo()); Consumer.needsSemanticInfo());
assert(Snapshot); assert(Snapshot);
EditorDoc->parse(Snapshot, *this); EditorDoc->parse(Snapshot, *this, Consumer.syntaxTreeEnabled());
EditorDoc->readSyntaxInfo(Consumer); EditorDoc->readSyntaxInfo(Consumer);
} else { } else {
Snapshot = EditorDoc->getLatestSnapshot(); Snapshot = EditorDoc->getLatestSnapshot();

View File

@@ -91,7 +91,8 @@ public:
ImmutableTextSnapshotRef getLatestSnapshot() const; ImmutableTextSnapshotRef getLatestSnapshot() const;
void parse(ImmutableTextSnapshotRef Snapshot, SwiftLangSupport &Lang); void parse(ImmutableTextSnapshotRef Snapshot, SwiftLangSupport &Lang,
bool BuildSyntaxTree);
void readSyntaxInfo(EditorConsumer& consumer); void readSyntaxInfo(EditorConsumer& consumer);
void readSemanticInfo(ImmutableTextSnapshotRef Snapshot, void readSemanticInfo(ImmutableTextSnapshotRef Snapshot,
EditorConsumer& Consumer); EditorConsumer& Consumer);

View File

@@ -55,7 +55,7 @@ private:
public: public:
FormatterDocument(std::unique_ptr<llvm::MemoryBuffer> Buffer) { FormatterDocument(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
// Formatting logic requires tokens on source file. // Formatting logic requires tokens on source file.
CompInv.getLangOptions().KeepSyntaxInfoInSourceFile = true; CompInv.getLangOptions().CollectParsedToken = true;
updateCode(std::move(Buffer)); updateCode(std::move(Buffer));
} }

View File

@@ -965,7 +965,8 @@ static int doSyntaxColoring(const CompilerInvocation &InitInvok,
PrintingDiagnosticConsumer PrintDiags; PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags); CI.addDiagnosticConsumer(&PrintDiags);
Invocation.getLangOptions().Playground = Playground; Invocation.getLangOptions().Playground = Playground;
Invocation.getLangOptions().KeepSyntaxInfoInSourceFile = true; Invocation.getLangOptions().CollectParsedToken = true;
Invocation.getLangOptions().BuildSyntaxTree = true;
if (CI.setup(Invocation)) if (CI.setup(Invocation))
return 1; return 1;
if (!RunTypeChecker) if (!RunTypeChecker)
@@ -1178,7 +1179,8 @@ private:
static int doStructureAnnotation(const CompilerInvocation &InitInvok, static int doStructureAnnotation(const CompilerInvocation &InitInvok,
StringRef SourceFilename) { StringRef SourceFilename) {
CompilerInvocation Invocation(InitInvok); CompilerInvocation Invocation(InitInvok);
Invocation.getLangOptions().KeepSyntaxInfoInSourceFile = true; Invocation.getLangOptions().BuildSyntaxTree = true;
Invocation.getLangOptions().CollectParsedToken = true;
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFilename); Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFilename);
CompilerInstance CI; CompilerInstance CI;
@@ -2695,7 +2697,8 @@ static int doPrintRangeInfo(const CompilerInvocation &InitInvok,
CompilerInvocation Invocation(InitInvok); CompilerInvocation Invocation(InitInvok);
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFileName); Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(SourceFileName);
Invocation.getLangOptions().DisableAvailabilityChecking = false; Invocation.getLangOptions().DisableAvailabilityChecking = false;
Invocation.getLangOptions().KeepSyntaxInfoInSourceFile = true; Invocation.getLangOptions().BuildSyntaxTree = true;
Invocation.getLangOptions().CollectParsedToken = true;
CompilerInstance CI; CompilerInstance CI;
@@ -3017,7 +3020,8 @@ int main(int argc, char *argv[]) {
InitInvok.setModuleName(options::ModuleName); InitInvok.setModuleName(options::ModuleName);
InitInvok.setSDKPath(options::SDK); InitInvok.setSDKPath(options::SDK);
InitInvok.getLangOptions().KeepSyntaxInfoInSourceFile = true; InitInvok.getLangOptions().CollectParsedToken = true;
InitInvok.getLangOptions().BuildSyntaxTree = true;
if (options::DisableObjCInterop) { if (options::DisableObjCInterop) {
InitInvok.getLangOptions().EnableObjCInterop = false; InitInvok.getLangOptions().EnableObjCInterop = false;
} else if (options::EnableObjCInterop) { } else if (options::EnableObjCInterop) {

View File

@@ -236,7 +236,8 @@ int main(int argc, char *argv[]) {
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile( Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(
options::SourceFilename); options::SourceFilename);
Invocation.getLangOptions().AttachCommentsToDecls = true; Invocation.getLangOptions().AttachCommentsToDecls = true;
Invocation.getLangOptions().KeepSyntaxInfoInSourceFile = true; Invocation.getLangOptions().CollectParsedToken = true;
Invocation.getLangOptions().BuildSyntaxTree = true;
for (auto FileName : options::InputFilenames) for (auto FileName : options::InputFilenames)
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(FileName); Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(FileName);

View File

@@ -144,7 +144,7 @@ SourceFile *getSourceFile(CompilerInstance &Instance,
StringRef InputFileName, StringRef InputFileName,
const char *MainExecutablePath) { const char *MainExecutablePath) {
CompilerInvocation Invocation; CompilerInvocation Invocation;
Invocation.getLangOptions().KeepSyntaxInfoInSourceFile = true; Invocation.getLangOptions().BuildSyntaxTree = true;
Invocation.getLangOptions().VerifySyntaxTree = true; Invocation.getLangOptions().VerifySyntaxTree = true;
Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(InputFileName); Invocation.getFrontendOptions().InputsAndOutputs.addInputFile(InputFileName);
Invocation.setMainExecutablePath( Invocation.setMainExecutablePath(