Skip to content

[IDE] Pass LangOptions to ide::isSourceInputComplete #77072

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
Oct 30, 2024
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
7 changes: 5 additions & 2 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,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);
Expand Down
1 change: 0 additions & 1 deletion lib/DriverTool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_parse_test_main.cpp)

Expand Down
78 changes: 0 additions & 78 deletions lib/DriverTool/swift_indent_main.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change that made it into the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's something I happened to notice when changing ParserUnit, I could put it in a separate PR, but then I'd have to rebase one of them.

This file was deleted.

11 changes: 6 additions & 5 deletions lib/IDE/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 7 additions & 11 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions test/StringProcessing/Parse/regex_parse_complete.swift
Original file line number Diff line number Diff line change
@@ -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
#/\(/#
7 changes: 3 additions & 4 deletions tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,9 @@ class SwiftDocumentSyntaxInfo {

BufferID = SM.addNewSourceBuffer(std::move(BufCopy));

Parser.reset(new ParserUnit(
SM, SourceFileKind::Main, BufferID, CompInv.getLangOptions(),
CompInv.getTypeCheckerOptions(), CompInv.getSILOptions(),
CompInv.getModuleName()));
Parser.reset(new ParserUnit(SM, SourceFileKind::Main, BufferID,
CompInv.getLangOptions(),
CompInv.getModuleName()));

registerTypeCheckerRequestFunctions(
Parser->getParser().Context.evaluator);
Expand Down
20 changes: 9 additions & 11 deletions tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions unittests/Parse/TokenizerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ class TokenizerTest : public ::testing::Test {
}

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();
Expand Down