Files
swift-mirror/lib/IDE/Utils.cpp
Jordan Rose cbcf17f9bd Stop leaking memory from Module and FileUnit.
Also, disallow creating Modules and FileUnits on the stack. They must always
live as long as the ASTContext.

<rdar://problem/15596964>

Swift SVN r13671
2014-02-08 02:12:57 +00:00

56 lines
1.8 KiB
C++

//===- Utils.cpp - Misc utilities -----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/IDE/Utils.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/SourceManager.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/Module.h"
#include "swift/AST/SearchPathOptions.h"
#include "swift/Parse/Parser.h"
#include "swift/Parse/PersistentParserState.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace swift;
using namespace ide;
bool ide::isSourceInputComplete(std::unique_ptr<llvm::MemoryBuffer> MemBuf) {
LangOptions LangOpts;
SearchPathOptions SearchPathOpts;
SourceManager SM;
auto BufferID = SM.addNewSourceBuffer(MemBuf.release());
DiagnosticEngine Diags(SM);
ASTContext Ctx(LangOpts, SearchPathOpts, SM, Diags);
auto ModName = Ctx.getIdentifier("input");
Module &Mod = *Module::create(ModName, Ctx);
SourceFile &SF = *new (Ctx) SourceFile(Mod, SourceFileKind::Main, BufferID);
PersistentParserState PersistentState;
Parser P(BufferID, SF, /*SIL=*/nullptr, &PersistentState);
bool Done;
do {
P.parseTopLevel();
Done = P.Tok.is(tok::eof);
} while (!Done);
return !P.isInputIncomplete();
}
bool ide::isSourceInputComplete(StringRef Text) {
std::unique_ptr<llvm::MemoryBuffer> InputBuf;
InputBuf.reset(llvm::MemoryBuffer::getMemBufferCopy(Text));
return ide::isSourceInputComplete(std::move(InputBuf));
}