Merge pull request #77072 from hamishknight/complete-options

[IDE] Pass LangOptions to `ide::isSourceInputComplete`
This commit is contained in:
Hamish Knight
2024-10-30 20:17:47 +00:00
committed by GitHub
11 changed files with 52 additions and 117 deletions

View File

@@ -80,8 +80,11 @@ struct SourceCompleteResult {
};
SourceCompleteResult
isSourceInputComplete(std::unique_ptr<llvm::MemoryBuffer> MemBuf, SourceFileKind SFKind);
SourceCompleteResult isSourceInputComplete(StringRef Text, SourceFileKind SFKind);
isSourceInputComplete(std::unique_ptr<llvm::MemoryBuffer> MemBuf,
SourceFileKind SFKind, const LangOptions &LangOpts);
SourceCompleteResult isSourceInputComplete(StringRef Text,
SourceFileKind SFKind,
const LangOptions &LangOpts);
/// Visits all overridden declarations exhaustively from VD, including protocol
/// conformances and clang declarations.

View File

@@ -321,8 +321,7 @@ namespace swift {
class ParserUnit {
public:
ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID,
const LangOptions &LangOpts, const TypeCheckerOptions &TyOpts,
const SILOptions &SILOpts, StringRef ModuleName);
const LangOptions &LangOpts, StringRef ModuleName);
ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID);
ParserUnit(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID,
unsigned Offset, unsigned EndOffset);

View File

@@ -11,7 +11,6 @@ set(driver_sources_and_options
modulewrap_main.cpp
swift_api_digester_main.cpp
swift_cache_tool_main.cpp
swift_indent_main.cpp
swift_symbolgraph_extract_main.cpp
swift_synthesize_interface_main.cpp
swift_parse_test_main.cpp)

View File

@@ -1,78 +0,0 @@
//===--- swift_indent_main.cpp - Swift code formatting tool ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
//
// Formats Swift files or file ranges according to a set of parameters.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/IDE/Indenting.h"
#include "swift/Option/Options.h"
#include "swift/Subsystems.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include <string>
#include <vector>
using namespace swift;
using namespace swift::ide;
using namespace llvm::opt;
class FormatterDocument {
private:
SourceManager SM;
unsigned BufferID;
CompilerInvocation CompInv;
std::unique_ptr<ParserUnit> Parser;
class FormatterDiagConsumer : public swift::DiagnosticConsumer {
void handleDiagnostic(SourceManager &SM,
const swift::DiagnosticInfo &Info) override {
llvm::errs() << "Parse error: ";
DiagnosticEngine::formatDiagnosticText(llvm::errs(), Info.FormatString,
Info.FormatArgs);
llvm::errs() << "\n";
}
} DiagConsumer;
public:
FormatterDocument(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
// Formatting logic requires tokens on source file.
CompInv.getLangOptions().CollectParsedToken = true;
updateCode(std::move(Buffer));
}
void updateCode(std::unique_ptr<llvm::MemoryBuffer> Buffer) {
BufferID = SM.addNewSourceBuffer(std::move(Buffer));
Parser.reset(new ParserUnit(
SM, SourceFileKind::Main, BufferID, CompInv.getLangOptions(),
CompInv.getTypeCheckerOptions(), CompInv.getSILOptions(),
CompInv.getModuleName()));
Parser->getDiagnosticEngine().addConsumer(DiagConsumer);
Parser->parse();
}
std::pair<LineRange, std::string> reformat(LineRange Range,
CodeFormatOptions Options) {
return ::reformat(Range, Options, SM, Parser->getSourceFile());
}
const llvm::MemoryBuffer &memBuffer() const {
return *SM.getLLVMSourceMgr().getMemoryBuffer(BufferID);
}
};

View File

@@ -96,10 +96,10 @@ static const char *skipStringInCode(const char *p, const char *End) {
SourceCompleteResult
ide::isSourceInputComplete(std::unique_ptr<llvm::MemoryBuffer> MemBuf,
SourceFileKind SFKind) {
SourceFileKind SFKind, const LangOptions &LangOpts) {
SourceManager SM;
auto BufferID = SM.addNewSourceBuffer(std::move(MemBuf));
ParserUnit Parse(SM, SFKind, BufferID);
ParserUnit Parse(SM, SFKind, BufferID, LangOpts, "input");
Parse.parse();
SourceCompleteResult SCR;
SCR.IsComplete = !Parse.getParser().isInputIncomplete();
@@ -177,10 +177,11 @@ ide::isSourceInputComplete(std::unique_ptr<llvm::MemoryBuffer> MemBuf,
return SCR;
}
SourceCompleteResult
ide::isSourceInputComplete(StringRef Text,SourceFileKind SFKind) {
SourceCompleteResult ide::isSourceInputComplete(StringRef Text,
SourceFileKind SFKind,
const LangOptions &LangOpts) {
return ide::isSourceInputComplete(llvm::MemoryBuffer::getMemBufferCopy(Text),
SFKind);
SFKind, LangOpts);
}
template <typename FnTy>

View File

@@ -2135,7 +2135,7 @@ AvailabilityMacroMap &Parser::parseAllAvailabilityMacroArguments() {
for (unsigned bufferID: bufferIDs) {
// Create temporary parser.
swift::ParserUnit PU(SM, SourceFileKind::Main, bufferID, LangOpts,
TypeCheckerOptions(), SILOptions(), "unknown");
"unknown");
ForwardingDiagnosticConsumer PDC(Context.Diags);
PU.getDiagnosticEngine().addConsumer(PDC);

View File

@@ -1132,9 +1132,9 @@ struct ParserUnit::Implementation {
std::unique_ptr<Parser> TheParser;
Implementation(SourceManager &SM, SourceFileKind SFKind, unsigned BufferID,
const LangOptions &Opts, const TypeCheckerOptions &TyOpts,
const SILOptions &silOpts, StringRef ModuleName)
: LangOpts(Opts), TypeCheckerOpts(TyOpts), SILOpts(silOpts), Diags(SM),
const LangOptions &Opts, StringRef ModuleName)
: LangOpts(Opts), TypeCheckerOpts(TypeCheckerOptions()),
SILOpts(SILOptions()), Diags(SM),
Ctx(*ASTContext::get(LangOpts, TypeCheckerOpts, SILOpts, SearchPathOpts,
clangImporterOpts, symbolGraphOpts, CASOpts, SM,
Diags)) {
@@ -1156,23 +1156,19 @@ struct ParserUnit::Implementation {
ParserUnit::ParserUnit(SourceManager &SM, SourceFileKind SFKind,
unsigned BufferID)
: ParserUnit(SM, SFKind, BufferID, LangOptions(), TypeCheckerOptions(),
SILOptions(), "input") {}
: ParserUnit(SM, SFKind, BufferID, LangOptions(), "input") {}
ParserUnit::ParserUnit(SourceManager &SM, SourceFileKind SFKind,
unsigned BufferID, const LangOptions &LangOpts,
const TypeCheckerOptions &TypeCheckOpts,
const SILOptions &SILOpts, StringRef ModuleName)
: Impl(*new Implementation(SM, SFKind, BufferID, LangOpts, TypeCheckOpts,
SILOpts, ModuleName)) {
StringRef ModuleName)
: Impl(*new Implementation(SM, SFKind, BufferID, LangOpts, ModuleName)) {
Impl.TheParser.reset(new Parser(BufferID, *Impl.SF, /*SIL=*/nullptr,
/*PersistentState=*/nullptr));
}
ParserUnit::ParserUnit(SourceManager &SM, SourceFileKind SFKind,
unsigned BufferID, unsigned Offset, unsigned EndOffset)
: Impl(*new Implementation(SM, SFKind, BufferID, LangOptions(),
TypeCheckerOptions(), SILOptions(), "input")) {
: Impl(*new Implementation(SM, SFKind, BufferID, LangOptions(), "input")) {
std::unique_ptr<Lexer> Lex;
Lex.reset(new Lexer(Impl.LangOpts, SM,

View File

@@ -0,0 +1,19 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// Make sure we consider the below source complete.
// RUN: %swift-ide-test -test-input-complete -enable-bare-slash-regex -source-filename %t/bare-slash.swift | %FileCheck %s -check-prefix=COMPLETE
// Bare slash is currently disabled by default.
// RUN: %swift-ide-test -test-input-complete -source-filename %t/bare-slash.swift | %FileCheck %s -check-prefix=INCOMPLETE
// RUN: %swift-ide-test -test-input-complete -source-filename %t/extended.swift | %FileCheck %s -check-prefix=COMPLETE
// INCOMPLETE: IS_INCOMPLETE
// COMPLETE: IS_COMPLETE
//--- bare-slash.swift
/\(/
//--- extended.swift
#/\(/#

View File

@@ -745,9 +745,8 @@ public:
BufferID = SM.addNewSourceBuffer(std::move(BufCopy));
Parser.reset(new ParserUnit(
SM, SourceFileKind::Main, BufferID, CompInv.getLangOptions(),
CompInv.getTypeCheckerOptions(), CompInv.getSILOptions(),
Parser.reset(new ParserUnit(SM, SourceFileKind::Main, BufferID,
CompInv.getLangOptions(),
CompInv.getModuleName()));
registerTypeCheckerRequestFunctions(

View File

@@ -2000,10 +2000,8 @@ static int doSyntaxColoring(const CompilerInvocation &InitInvok,
SourceManager SM;
unsigned BufferID = SM.addNewSourceBuffer(std::move(FileBuf));
ParserUnit Parser(
SM, SourceFileKind::Main, BufferID, Invocation.getLangOptions(),
Invocation.getTypeCheckerOptions(), Invocation.getSILOptions(),
Invocation.getModuleName());
ParserUnit Parser(SM, SourceFileKind::Main, BufferID,
Invocation.getLangOptions(), Invocation.getModuleName());
registerTypeCheckerRequestFunctions(Parser.getParser().Context.evaluator);
registerClangImporterRequestFunctions(Parser.getParser().Context.evaluator);
@@ -2229,9 +2227,7 @@ static int doStructureAnnotation(const CompilerInvocation &InitInvok,
unsigned BufferID = SM.addNewSourceBuffer(std::move(FileBuf));
ParserUnit Parser(SM, SourceFileKind::Main, BufferID,
Invocation.getLangOptions(),
Invocation.getTypeCheckerOptions(),
Invocation.getSILOptions(), Invocation.getModuleName());
Invocation.getLangOptions(), Invocation.getModuleName());
registerTypeCheckerRequestFunctions(
Parser.getParser().Context.evaluator);
@@ -2512,15 +2508,17 @@ static int doSemanticAnnotation(const CompilerInvocation &InitInvok,
return 0;
}
static int doInputCompletenessTest(StringRef SourceFilename) {
static int doInputCompletenessTest(const CompilerInvocation &InitInvok,
StringRef SourceFilename) {
std::unique_ptr<llvm::MemoryBuffer> FileBuf;
if (setBufferForFile(SourceFilename, FileBuf))
return 1;
llvm::raw_ostream &OS = llvm::outs();
OS << SourceFilename << ": ";
if (isSourceInputComplete(std::move(FileBuf),
SourceFileKind::Main).IsComplete) {
if (isSourceInputComplete(std::move(FileBuf), SourceFileKind::Main,
InitInvok.getLangOptions())
.IsComplete) {
OS << "IS_COMPLETE\n";
} else {
OS << "IS_INCOMPLETE\n";
@@ -4748,7 +4746,7 @@ int main(int argc, char *argv[]) {
break;
case ActionType::TestInputCompleteness:
ExitCode = doInputCompletenessTest(options::SourceFilename);
ExitCode = doInputCompletenessTest(InitInvok, options::SourceFilename);
break;
case ActionType::PrintASTNotTypeChecked:

View File

@@ -82,8 +82,7 @@ public:
}
std::vector<Token> parseAndGetSplitTokens(unsigned BufID) {
swift::ParserUnit PU(SM, SourceFileKind::Main, BufID, LangOpts,
TypeCheckerOptions(), SILOptions(), "unknown");
swift::ParserUnit PU(SM, SourceFileKind::Main, BufID, LangOpts, "unknown");
SmallVector<ASTNode, 8> items;
PU.getParser().parseTopLevelItems(items);
return PU.getParser().getSplitTokens();