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

This commit is contained in:
swift-ci
2018-04-19 13:48:19 -07:00
16 changed files with 6 additions and 888 deletions

View File

@@ -32,30 +32,11 @@ struct SwiftArguments {
};
enum class OperationKind : uint64_t {
SimpleParse = 1 << 0,
PerformSema = 1 << 1,
AnnotAndDiag = 1 << 2,
PerformSema = 1 << 0,
IndexSource = 1 << 1,
CodeCompletion = 1 << 2,
ReadSyntaxInfo = 1 << 3,
ReadDiagnostics = 1 << 4,
ReadSemanticInfo = 1 << 5,
IndexModule = 1 << 6,
IndexSource = 1 << 7,
CursorInfoForIFaceGen = 1 << 8,
CursorInfoForSource = 1 << 9,
ExpandPlaceholder = 1 << 10,
FormatText = 1 << 11,
RelatedIdents = 1 << 12,
CodeCompletion = 1 << 13,
OpenInterface = 1 << 14,
OpenHeaderInterface = 1 << 15,
CodeCompletionInit = 1 << 16,
Last = CodeCompletionInit,
Last = CodeCompletion,
All = (Last << 1) - 1
};
@@ -63,11 +44,6 @@ typedef std::vector<std::pair<std::string, std::string>> StringPairs;
struct SwiftInvocation {
SwiftArguments Args;
StringPairs Files;
void addFile(std::string FileName, std::string Text) {
Files.push_back(std::make_pair(std::move(FileName), std::move(Text)));
}
};
class TraceConsumer {

View File

@@ -803,11 +803,6 @@ ASTUnitRef ASTProducer::createASTUnit(SwiftASTManager::Implementation &MgrImpl,
for (auto &Content : Contents) {
if (Content.Snapshot)
ASTRef->Impl.Snapshots.push_back(Content.Snapshot);
if (TracedOp.enabled()) {
TraceInfo.addFile(Content.Buffer->getBufferIdentifier(),
Content.Buffer->getBuffer());
}
}
auto &CompIns = ASTRef->Impl.CompInst;
auto &Consumer = ASTRef->Impl.CollectDiagConsumer;

View File

@@ -112,21 +112,6 @@ static bool swiftCodeCompleteImpl(SwiftLangSupport &Lang,
ArrayRef<const char *> Args,
std::string &Error) {
trace::TracedOperation TracedInit(trace::OperationKind::CodeCompletionInit);
if (TracedInit.enabled()) {
trace::SwiftInvocation SwiftArgs;
trace::initTraceInfo(SwiftArgs,
UnresolvedInputFile->getBufferIdentifier(),
Args);
SwiftArgs.addFile(UnresolvedInputFile->getBufferIdentifier(),
UnresolvedInputFile->getBuffer());
TracedInit.start(SwiftArgs,
{ std::make_pair("Offset", std::to_string(Offset)),
std::make_pair("InputBufferSize",
std::to_string(UnresolvedInputFile->getBufferSize()))});
}
// Resolve symlinks for the input file; we resolve them for the input files
// in the arguments as well.
// FIXME: We need the Swift equivalent of Clang's FileEntry.
@@ -193,21 +178,9 @@ static bool swiftCodeCompleteImpl(SwiftLangSupport &Lang,
return true;
}
TracedInit.finish();
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
trace::initTraceInfo(SwiftArgs, InputFile->getBufferIdentifier(), Args);
trace::initTraceFiles(SwiftArgs, CI);
// Replace primary file with original content
std::for_each(SwiftArgs.Files.begin(), SwiftArgs.Files.end(),
[&] (trace::StringPairs::value_type &Pair) {
if (Pair.first == InputFile->getBufferIdentifier()) {
Pair.second = InputFile->getBuffer();
}
});
TracedOp.start(SwiftArgs,
{std::make_pair("OriginalOffset", std::to_string(Offset)),
std::make_pair("Offset",

View File

@@ -681,23 +681,8 @@ public:
Parser->getDiagnosticEngine().addConsumer(DiagConsumer);
}
void initArgsAndPrimaryFile(trace::SwiftInvocation &Info) {
Info.Args.PrimaryFile = PrimaryFile;
Info.Args.Args = Args;
}
void parse() {
auto &P = Parser->getParser();
trace::TracedOperation TracedOp(trace::OperationKind::SimpleParse);
if (TracedOp.enabled()) {
trace::SwiftInvocation Info;
initArgsAndPrimaryFile(Info);
auto Text = SM.getLLVMSourceMgr().getMemoryBuffer(BufferID)->getBuffer();
Info.Files.push_back(std::make_pair(PrimaryFile, Text));
TracedOp.start(Info);
}
bool Done = false;
while (!Done) {
P.parseTopLevel();
@@ -988,21 +973,10 @@ public:
}
unsigned BufferID = AstUnit->getPrimarySourceFile().getBufferID().getValue();
trace::TracedOperation TracedOp(trace::OperationKind::AnnotAndDiag);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
SemaInfoRef->getInvocation()->raw(SwiftArgs.Args.Args,
SwiftArgs.Args.PrimaryFile);
trace::initTraceFiles(SwiftArgs, CompIns);
TracedOp.start(SwiftArgs);
}
SemanticAnnotator Annotator(CompIns.getSourceMgr(), BufferID);
Annotator.walk(AstUnit->getPrimarySourceFile());
SemaToks = std::move(Annotator.SemaToks);
TracedOp.finish();
SemaInfoRef->
updateSemanticInfo(std::move(SemaToks),
std::move(Consumer.getDiagnosticsForBuffer(BufferID)),
@@ -1066,23 +1040,8 @@ struct SwiftEditorDocument::Implementation {
: LangSupport(LangSupport), FilePath(FilePath), FormatOptions(options) {
SemanticInfo = new SwiftDocumentSemanticInfo(FilePath, LangSupport);
}
void buildSwiftInv(trace::SwiftInvocation &Inv);
};
void SwiftEditorDocument::Implementation::buildSwiftInv(
trace::SwiftInvocation &Inv) {
if (SemanticInfo->getInvocation()) {
std::string PrimaryFile; // Ignored, FilePath will be used
SemanticInfo->getInvocation()->raw(Inv.Args.Args, PrimaryFile);
}
Inv.Args.PrimaryFile = FilePath;
auto &SM = SyntaxInfo->getSourceManager();
auto ID = SyntaxInfo->getBufferID();
auto Text = SM.getLLVMSourceMgr().getMemoryBuffer(ID)->getBuffer();
Inv.Files.push_back(std::make_pair(FilePath, Text));
}
namespace {
static UIdent getAccessLevelUID(AccessLevel Access) {
@@ -1816,13 +1775,6 @@ void SwiftEditorDocument::parse(ImmutableTextSnapshotRef Snapshot,
void SwiftEditorDocument::readSyntaxInfo(EditorConsumer &Consumer) {
llvm::sys::ScopedLock L(Impl.AccessMtx);
trace::TracedOperation TracedOp(trace::OperationKind::ReadSyntaxInfo);
if (TracedOp.enabled()) {
trace::SwiftInvocation Info;
Impl.buildSwiftInv(Info);
TracedOp.start(Info);
}
Impl.ParserDiagnostics = Impl.SyntaxInfo->getDiagnostics();
ide::SyntaxModelContext ModelContext(Impl.SyntaxInfo->getSourceFile());
@@ -1868,13 +1820,6 @@ void SwiftEditorDocument::readSyntaxInfo(EditorConsumer &Consumer) {
void SwiftEditorDocument::readSemanticInfo(ImmutableTextSnapshotRef Snapshot,
EditorConsumer& Consumer) {
trace::TracedOperation TracedOp(trace::OperationKind::ReadSemanticInfo);
if (TracedOp.enabled()) {
trace::SwiftInvocation Info;
Impl.buildSwiftInv(Info);
TracedOp.start(Info);
}
std::vector<SwiftSemanticToken> SemaToks;
Optional<std::vector<DiagnosticEntryInfo>> SemaDiags;
Impl.SemanticInfo->readSemanticInfo(Snapshot, SemaToks, SemaDiags,
@@ -1929,26 +1874,6 @@ void SwiftEditorDocument::formatText(unsigned Line, unsigned Length,
auto SyntaxInfo = Impl.getSyntaxInfo();
SourceFile &SF = SyntaxInfo->getSourceFile();
SourceManager &SM = SyntaxInfo->getSourceManager();
unsigned BufID = SyntaxInfo->getBufferID();
trace::TracedOperation TracedOp(trace::OperationKind::FormatText);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
// Compiler arguments do not matter
auto Buf = SM.getLLVMSourceMgr().getMemoryBuffer(BufID);
SwiftArgs.Args.PrimaryFile = Buf->getBufferIdentifier();
SwiftArgs.addFile(SwiftArgs.Args.PrimaryFile, Buf->getBuffer());
trace::StringPairs OpArgs = {
std::make_pair("Line", std::to_string(Line)),
std::make_pair("Length", std::to_string(Length)),
std::make_pair("IndentWidth",
std::to_string(Impl.FormatOptions.IndentWidth)),
std::make_pair("TabWidth",
std::to_string(Impl.FormatOptions.TabWidth)),
std::make_pair("UseTabs",
std::to_string(Impl.FormatOptions.UseTabs))};
TracedOp.start(SwiftArgs, OpArgs);
}
LineRange inputRange = LineRange(Line, Length);
CodeFormatOptions Options = getFormatOptions();
@@ -1981,18 +1906,6 @@ void SwiftEditorDocument::expandPlaceholder(unsigned Offset, unsigned Length,
return;
}
trace::TracedOperation TracedOp(trace::OperationKind::ExpandPlaceholder);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
SyntaxInfo->initArgsAndPrimaryFile(SwiftArgs);
auto Buf = SM.getLLVMSourceMgr().getMemoryBuffer(BufID);
SwiftArgs.addFile(Buf->getBufferIdentifier(), Buf->getBuffer());
trace::StringPairs OpArgs = {
std::make_pair("Offset", std::to_string(Offset)),
std::make_pair("Length", std::to_string(Length))};
TracedOp.start(SwiftArgs, OpArgs);
}
PlaceholderExpansionScanner Scanner(SM);
SourceFile &SF = SyntaxInfo->getSourceFile();

View File

@@ -707,17 +707,6 @@ void SwiftLangSupport::editorOpenInterface(EditorConsumer &Consumer,
return;
}
trace::TracedOperation TracedOp(trace::OperationKind::OpenInterface);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
SwiftArgs.Args.Args.assign(Args.begin(), Args.end());
// NOTE: do not use primary file
// NOTE: do not use files
TracedOp.start(SwiftArgs,
{std::make_pair("Name", Name),
std::make_pair("ModuleName", ModuleName)});
}
Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
std::string ErrMsg;
@@ -783,16 +772,6 @@ void SwiftLangSupport::editorOpenSwiftSourceInterface(StringRef Name,
Consumer->handleRequestError(Error.c_str());
return;
}
trace::TracedOperation TracedOp(trace::OperationKind::OpenInterface);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
SwiftArgs.Args.Args.assign(Args.begin(), Args.end());
// NOTE: do not use primary file
// NOTE: do not use files
TracedOp.start(SwiftArgs,
{std::make_pair("Name", Name),
std::make_pair("SourceName", SourceName)});
}
auto AstConsumer = std::make_shared<PrimaryFileInterfaceConsumer>(Name,
SourceName, IFaceGenContexts, Consumer, Invocation);
static const char OncePerASTToken = 0;
@@ -826,17 +805,6 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
return;
}
trace::TracedOperation TracedOp(trace::OperationKind::OpenHeaderInterface);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
SwiftArgs.Args.Args.assign(Args.begin(), Args.end());
// NOTE: do not use primary file
// NOTE: do not use files
TracedOp.start(SwiftArgs,
{std::make_pair("Name", Name),
std::make_pair("HeaderName", HeaderName)});
}
Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
if (!swiftVersion.empty()) {
auto swiftVer = version::Version::parseVersionString(swiftVersion,

View File

@@ -158,18 +158,6 @@ static void indexModule(llvm::MemoryBuffer *Input,
IndexingConsumer &IdxConsumer,
CompilerInstance &CI,
ArrayRef<const char *> Args) {
trace::TracedOperation TracedOp(trace::OperationKind::IndexModule);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
SwiftArgs.Args.Args.assign(Args.begin(), Args.end());
SwiftArgs.Args.PrimaryFile = Input->getBufferIdentifier();
SwiftArgs.addFile(Input->getBufferIdentifier(), Input->getBuffer());
trace::StringPairs OpArgs;
OpArgs.push_back(std::make_pair("ModuleName", ModuleName));
OpArgs.push_back(std::make_pair("Hash", Hash));
TracedOp.start(SwiftArgs, OpArgs);
}
ASTContext &Ctx = CI.getASTContext();
std::unique_ptr<SerializedModuleLoader> Loader;
ModuleDecl *Mod = nullptr;
@@ -217,18 +205,6 @@ void trace::initTraceInfo(trace::SwiftInvocation &SwiftArgs,
SwiftArgs.Args.PrimaryFile = InputFile;
}
void trace::initTraceFiles(trace::SwiftInvocation &SwiftArgs,
swift::CompilerInstance &CI) {
auto &SM = CI.getSourceMgr();
auto Ids = CI.getInputBufferIDs();
std::for_each(Ids.begin(), Ids.end(),
[&] (unsigned Id) {
auto Buf = SM.getLLVMSourceMgr().getMemoryBuffer(Id);
SwiftArgs.addFile(Buf->getBufferIdentifier(),
Buf->getBuffer());
});
}
void SwiftLangSupport::indexSource(StringRef InputFile,
IndexingConsumer &IdxConsumer,
ArrayRef<const char *> OrigArgs,
@@ -296,7 +272,6 @@ void SwiftLangSupport::indexSource(StringRef InputFile,
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
trace::initTraceInfo(SwiftArgs, InputFile, Args);
trace::initTraceFiles(SwiftArgs, CI);
TracedOp.start(SwiftArgs);
}

View File

@@ -516,9 +516,6 @@ namespace trace {
void initTraceInfo(trace::SwiftInvocation &SwiftArgs,
StringRef InputFile,
ArrayRef<const char *> Args);
void initTraceFiles(trace::SwiftInvocation &SwiftArgs,
swift::CompilerInstance &CI);
}
/// When we cannot build any more clang modules, close the .pcm / files to

View File

@@ -1230,15 +1230,6 @@ static void resolveCursor(SwiftLangSupport &Lang,
return;
}
trace::TracedOperation TracedOp(trace::OperationKind::CursorInfoForSource);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
ASTInvok->raw(SwiftArgs.Args.Args, SwiftArgs.Args.PrimaryFile);
trace::initTraceFiles(SwiftArgs, CompIns);
TracedOp.start(SwiftArgs,
{std::make_pair("Offset", std::to_string(Offset))});
}
// Sanitize length.
if (Length) {
SourceLoc TokEnd = Lexer::getLocForEndOfToken(SM, Loc);
@@ -1424,15 +1415,6 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
return;
}
trace::TracedOperation TracedOp(trace::OperationKind::CursorInfoForSource);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
ASTInvok->raw(SwiftArgs.Args.Args, SwiftArgs.Args.PrimaryFile);
trace::initTraceFiles(SwiftArgs, CompIns);
TracedOp.start(SwiftArgs,
{std::make_pair("Offset", std::to_string(Offset))});
}
CursorInfoResolver Resolver(AstUnit->getPrimarySourceFile());
ResolvedCursorInfo CursorInfo = Resolver.resolve(Loc);
if (CursorInfo.isInvalid()) {
@@ -1582,18 +1564,6 @@ void SwiftLangSupport::getCursorInfo(
std::function<void(const CursorInfoData &)> Receiver) {
if (auto IFaceGenRef = IFaceGenContexts.get(InputFile)) {
trace::TracedOperation TracedOp(trace::OperationKind::CursorInfoForIFaceGen);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
trace::initTraceInfo(SwiftArgs, InputFile, Args);
// Do we need to record any files? If yes -- which ones?
trace::StringPairs OpArgs {
std::make_pair("DocumentName", IFaceGenRef->getDocumentName()),
std::make_pair("ModuleOrHeaderName", IFaceGenRef->getModuleOrHeaderName()),
std::make_pair("Offset", std::to_string(Offset))};
TracedOp.start(SwiftArgs, OpArgs);
}
IFaceGenRef->accessASTAsync([this, IFaceGenRef, Offset, Actionables, Receiver] {
SwiftInterfaceGenContext::ResolvedEntity Entity;
Entity = IFaceGenRef->resolveEntityForOffset(Offset);
@@ -1663,18 +1633,6 @@ getNameInfo(StringRef InputFile, unsigned Offset, NameTranslatingInfo &Input,
std::function<void(const NameTranslatingInfo &)> Receiver) {
if (auto IFaceGenRef = IFaceGenContexts.get(InputFile)) {
trace::TracedOperation TracedOp(trace::OperationKind::CursorInfoForIFaceGen);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
trace::initTraceInfo(SwiftArgs, InputFile, Args);
// Do we need to record any files? If yes -- which ones?
trace::StringPairs OpArgs {
std::make_pair("DocumentName", IFaceGenRef->getDocumentName()),
std::make_pair("ModuleOrHeaderName", IFaceGenRef->getModuleOrHeaderName()),
std::make_pair("Offset", std::to_string(Offset))};
TracedOp.start(SwiftArgs, OpArgs);
}
IFaceGenRef->accessASTAsync([IFaceGenRef, Offset, Input, Receiver] {
SwiftInterfaceGenContext::ResolvedEntity Entity;
Entity = IFaceGenRef->resolveEntityForOffset(Offset);
@@ -1758,14 +1716,6 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
unsigned BufferID =
AstUnit->getPrimarySourceFile().getBufferID().getValue();
trace::TracedOperation TracedOp(trace::OperationKind::CursorInfoForSource);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
ASTInvok->raw(SwiftArgs.Args.Args, SwiftArgs.Args.PrimaryFile);
trace::initTraceFiles(SwiftArgs, CompIns);
TracedOp.start(SwiftArgs, {std::make_pair("USR", USR)});
}
if (USR.startswith("c:")) {
LOG_WARN_FUNC("lookup for C/C++/ObjC USRs not implemented");
Receiver(CursorInfoData());
@@ -1955,15 +1905,6 @@ void SwiftLangSupport::findRelatedIdentifiersInFile(
SmallVector<std::pair<unsigned, unsigned>, 8> Ranges;
auto Action = [&]() {
trace::TracedOperation TracedOp(trace::OperationKind::RelatedIdents);
if (TracedOp.enabled()) {
trace::SwiftInvocation SwiftArgs;
Invok->raw(SwiftArgs.Args.Args, SwiftArgs.Args.PrimaryFile);
trace::initTraceFiles(SwiftArgs, CompInst);
TracedOp.start(SwiftArgs,
{std::make_pair("Offset", std::to_string(Offset))});
}
unsigned BufferID = SrcFile.getBufferID().getValue();
SourceLoc Loc =
Lexer::getLocForStartOfToken(CompInst.getSourceMgr(), BufferID, Offset);

View File

@@ -9,7 +9,7 @@ set(EXPORTED_SYMBOL_FILE "${SOURCEKITD_SOURCE_DIR}/bin/sourcekitd.exports")
add_sourcekit_framework(sourcekitd
${public_headers}
sourcekitd.cpp tracer.cpp
sourcekitd.cpp
LINK_LIBS sourcekitdAPI
LLVM_COMPONENT_DEPENDS support
MODULEMAP module.modulemap

View File

@@ -12,7 +12,6 @@
#include "sourcekitd/Internal-XPC.h"
#include "SourceKit/Support/Logging.h"
#include "SourceKit/Support/Tracing.h"
#include "SourceKit/Support/UIdent.h"
#include "llvm/Support/ErrorHandling.h"
@@ -30,11 +29,6 @@ static UIdent gKeyNotification("key.notification");
static UIdent gKeyDuration("key.duration");
static UIdent gSemaDisableNotificationUID("source.notification.sema_disabled");
void handleTraceMessageRequest(uint64_t Session, xpc_object_t Msg);
void initializeTracing();
void persistTracingData();
bool isTracingEnabled();
static llvm::sys::Mutex GlobalHandlersMtx;
static sourcekitd_uid_handler_t UidMappingHandler;
static sourcekitd_str_from_uid_handler_t StrFromUidMappingHandler;
@@ -218,7 +212,6 @@ static void handleInternalInitRequest(xpc_object_t reply) {
size_t Delay = SemanticEditorDelaySecondsNum;
if (Delay != 0)
xpc_dictionary_set_uint64(reply, xpc::KeySemaEditorDelay, Delay);
xpc_dictionary_set_uint64(reply, xpc::KeyTracingEnabled, isTracingEnabled());
}
static void handleInternalUIDRequest(xpc_object_t XVal,
@@ -248,8 +241,6 @@ static void handleInternalUIDRequest(xpc_object_t XVal,
static void handleInterruptedConnection(xpc_object_t event, xpc_connection_t conn);
void sourcekitd::initialize() {
initializeTracing();
assert(!GlobalConn);
GlobalConn = xpc_connection_create(SOURCEKIT_XPCSERVICE_IDENTIFIER, nullptr);
@@ -311,15 +302,6 @@ void sourcekitd::initialize() {
xpc_release(reply);
break;
}
case xpc::Message::TraceMessage: {
xpc_object_t reply = xpc_dictionary_create_reply(event);
handleTraceMessageRequest(xpc_array_get_uint64(contents, 1),
xpc_array_get_value(contents, 2));
xpc_connection_send_message(GlobalConn, reply);
xpc_release(reply);
break;
}
}
}
});
@@ -444,7 +426,6 @@ static void updateSemanticEditorDelay() {
static void handleInterruptedConnection(xpc_object_t event, xpc_connection_t conn) {
ConnectionInterrupted = true;
persistTracingData();
updateSemanticEditorDelay();
// FIXME: InterruptedConnectionHandler will go away.

View File

@@ -1,377 +0,0 @@
//===--- tracer.cpp -------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "sourcekitd/Internal-XPC.h"
#include "sourcekitd/XpcTracing.h"
#include "SourceKit/Support/Logging.h"
#include "SourceKit/Support/Concurrency.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/YAMLTraits.h"
#include <xpc/xpc.h>
#include <algorithm>
#include <chrono>
#include <deque>
#include <functional>
#include <iomanip>
#include <map>
#include <set>
using namespace llvm;
using namespace sourcekitd;
//===----------------------------------------------------------------------===//
// Generic
//===----------------------------------------------------------------------===//
static std::string trace_root_dir;
bool isTracingEnabled() {
return !trace_root_dir.empty();
}
typedef SmallString<256> path_t;
static llvm::sys::Mutex FileOpMutex;
static void fsEnsureDirExists(const char *Dir) {
llvm::sys::ScopedLock L(FileOpMutex);
if (!sys::fs::exists(Dir)) {
sys::fs::create_directory(Dir);
}
}
static void fsWriteFile(const char *Path, StringRef Text) {
llvm::sys::ScopedLock L(FileOpMutex);
if (!sys::fs::exists(Path)) {
std::error_code EC;
raw_fd_ostream Outfile(Path, EC, sys::fs::F_Text);
Outfile << Text;
Outfile.close();
}
}
static void fsInitTraceRoot(path_t &RootDir, uint64_t Id) {
RootDir = trace_root_dir;
std::string DirName =
std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
std::for_each(DirName.begin(), DirName.end(),
[] (char &C) { if (!isalnum(C)) C = '-'; });
DirName += '-';
DirName += std::to_string(Id);
sys::path::append(RootDir, DirName);
}
static void fsAddFileWithRevision(path_t &Path,
const std::string &File,
uint64_t Id) {
sys::path::append(Path,
sys::path::stem(File) +
"." +
std::to_string(Id) +
sys::path::extension(File));
}
typedef SourceKit::trace::OperationKind OperationKind;
struct OperationInfo {
std::chrono::system_clock::time_point StartedAt;
OperationKind Kind;
std::string SwiftArgs;
trace::StringPairs OpArgs;
trace::StringPairs Files;
OperationInfo(OperationKind K,
std::string &&SwiftArgs,
trace::StringPairs &&Files,
trace::StringPairs &&OpArgs)
: StartedAt(std::chrono::system_clock::now()),
Kind(K), SwiftArgs(SwiftArgs), OpArgs(OpArgs), Files(Files) {}
OperationInfo() = default;
OperationInfo(OperationInfo &&) = default;
OperationInfo &operator=(OperationInfo &&) = default;
OperationInfo(const OperationInfo &) = delete;
OperationInfo &operator=(const OperationInfo &) = delete;
};
struct OpRec {
std::string StartedAt;
std::string OpName;
std::string SwiftArgs;
trace::StringPairs OpArgs;
trace::StringPairs FileMap;
};
template <>
struct llvm::yaml::MappingTraits<OpRec> {
static void mapping(IO &Io, OpRec &Rec) {
Io.mapRequired("op", Rec.OpName);
Io.mapRequired("swift-args", Rec.SwiftArgs);
Io.mapRequired("op-args", Rec.OpArgs);
Io.mapRequired("file-map", Rec.FileMap);
}
};
template <typename U, typename V>
struct llvm::yaml::MappingTraits<std::pair<U, V>> {
static void mapping(IO &Io, std::pair<U, V> &Pair) {
Io.mapRequired("first", Pair.first);
Io.mapRequired("second", Pair.second);
}
};
//===----------------------------------------------------------------------===//
// State
//===----------------------------------------------------------------------===//
class State {
typedef std::map<uint64_t, OperationInfo> OperationsType;
static sys::Mutex GlobalMutex;
sys::Mutex LocalMutex;
static std::shared_ptr<State> CurrentState;
static SourceKit::WorkQueue Queue;
static std::string CompArgsFile;
static std::string ActiveOpFile;
const uint64_t Id;
std::atomic<uint64_t> UniqueId;
std::string TraceDir;
std::set<uint64_t> ReportedOps;
std::set<uint64_t> SeenOps;
OperationsType Operations;
State(uint64_t Id) : Id(Id), UniqueId(0) {
}
void persist();
static std::shared_ptr<State> getState(uint64_t Session) {
llvm::sys::ScopedLock L(GlobalMutex);
if (!CurrentState) {
CurrentState.reset(new State(Session));
} else if (!CurrentState || CurrentState->Id != Session) {
auto S = CurrentState;
Queue.dispatch([S] {S->persist();});
CurrentState.reset(new State(Session));
}
return CurrentState;
}
public:
State(const State &) = delete;
State &operator=(const State &) = delete;
State(State &&) = delete;
State &operator=(State &&) = delete;
static void addOperation(uint64_t Session,
uint64_t OpId,
OperationKind Kind,
std::string &&SwiftArgs,
trace::StringPairs &&Files,
trace::StringPairs &&OpArgs) {
auto S = getState(Session);
llvm::sys::ScopedLock L(S->LocalMutex);
assert(S->SeenOps.find(OpId) == S->SeenOps.end());
S->Operations[OpId] = OperationInfo(
Kind, std::move(SwiftArgs), std::move(Files), std::move(OpArgs));
S->SeenOps.insert(OpId);
}
static void removeOperation(uint64_t Session,
uint64_t OpId) {
auto S = getState(Session);
llvm::sys::ScopedLock L(S->LocalMutex);
assert(S->Operations.find(OpId) != S->Operations.end());
S->Operations.erase(OpId);
}
static void persistAsync() {
llvm::sys::ScopedLock L(GlobalMutex);
auto S = CurrentState;
Queue.dispatch([S] {S->persist();});
}
};
std::shared_ptr<State> State::CurrentState;
sys::Mutex State::GlobalMutex;
std::string State::CompArgsFile("swift-args.txt");
std::string State::ActiveOpFile("active-operation.txt");
SourceKit::WorkQueue State::Queue {
SourceKit::WorkQueue::Dequeuing::Serial, "sourcekit.swift.tracer.state"
};
static std::map<OperationKind, std::string> op_kind_names {
std::make_pair(OperationKind::SimpleParse, "SimpleParse"),
std::make_pair(OperationKind::PerformSema, "PerformSema"),
std::make_pair(OperationKind::AnnotAndDiag, "AnnotAndDiag"),
std::make_pair(OperationKind::OpenInterface, "OpenInterface"),
std::make_pair(OperationKind::ReadDiagnostics, "ReadDiagnostics"),
std::make_pair(OperationKind::ReadSemanticInfo, "ReadSemanticInfo"),
std::make_pair(OperationKind::ReadSyntaxInfo, "ReadSyntaxInfo"),
std::make_pair(OperationKind::IndexModule, "IndexModule"),
std::make_pair(OperationKind::IndexSource, "IndexSource"),
std::make_pair(OperationKind::CursorInfoForIFaceGen, "CursorInfoForIFaceGen"),
std::make_pair(OperationKind::CursorInfoForSource, "CursorInfoForSource"),
std::make_pair(OperationKind::RelatedIdents, "RelatedIdents"),
std::make_pair(OperationKind::FormatText, "FormatText"),
std::make_pair(OperationKind::ExpandPlaceholder, "ExpandPlaceholder"),
std::make_pair(OperationKind::CodeCompletion, "CodeCompletion"),
std::make_pair(OperationKind::CodeCompletionInit, "CodeCompletionInit"),
};
void State::persist() {
llvm::sys::ScopedLock L(LocalMutex);
if (TraceDir.empty()) {
path_t RootDir;
fsInitTraceRoot(RootDir, Id);
TraceDir = RootDir.c_str();
}
fsEnsureDirExists(TraceDir.c_str());
path_t FilePath;
std::for_each(
Operations.begin(), Operations.end(),
[&] (OperationsType::value_type &Pair) {
if (ReportedOps.find(Pair.first) == ReportedOps.end()) {
ReportedOps.insert(Pair.first);
auto &Op = Pair.second;
OpRec Rec;
Rec.OpName = op_kind_names[Op.Kind];
Rec.OpArgs = Op.OpArgs;
// Dump Swift arguments
FilePath = TraceDir;
fsAddFileWithRevision(FilePath, CompArgsFile, ++UniqueId);
Rec.SwiftArgs = FilePath.c_str();
fsWriteFile(FilePath.c_str(), Op.SwiftArgs);
// Dump files
std::for_each(
Op.Files.begin(), Op.Files.end(),
[&] (trace::StringPairs::value_type &Pair) {
FilePath = TraceDir;
fsAddFileWithRevision(FilePath, Pair.first, ++UniqueId);
Rec.FileMap.push_back(std::make_pair(Pair.first, FilePath.c_str()));
fsWriteFile(FilePath.c_str(), Pair.second);
});
// Serialize operation info
FilePath = TraceDir;
fsAddFileWithRevision(FilePath, ActiveOpFile, Pair.first);
std::error_code EC;
raw_fd_ostream Outfile(FilePath, EC, sys::fs::F_Text);
llvm::yaml::Output YamlOutput(Outfile);
YamlOutput << Rec;
}
});
}
//===----------------------------------------------------------------------===//
// Init & trace
//===----------------------------------------------------------------------===//
void initializeTracing() {
const char *EnvOpt = ::getenv("SOURCEKIT_TRACE_ROOT");
if (EnvOpt) {
using namespace SourceKit;
LOG_WARN_FUNC("TRACE: Enabled with root: " << EnvOpt);
trace_root_dir = EnvOpt;
}
}
static uint32_t readUint64(xpc_object_t Arr, size_t Index) {
return xpc_array_get_uint64(Arr, Index);
}
static trace::ActionKind readAction(xpc_object_t Arr, size_t Index) {
return static_cast<trace::ActionKind>(readUint64(Arr, Index));
}
static trace::OperationKind readOpKind(xpc_object_t Arr, size_t Index) {
return static_cast<trace::OperationKind>(readUint64(Arr, Index));
}
static llvm::StringRef readString(xpc_object_t Arr, size_t Index) {
return xpc_array_get_string(Arr, Index);
}
static trace::StringPairs readStringPairs(xpc_object_t Arr, size_t &Index) {
trace::StringPairs Files;
auto Count = readUint64(Arr, Index++);
for (uint64_t I = 0; I < Count; I++) {
auto FileName = readString(Arr, Index++);
auto Text = readString(Arr, Index++);
Files.push_back(std::make_pair(FileName, Text));
}
return Files;
}
void handleTraceMessageRequest(uint64_t Session, xpc_object_t Msg) {
if (isTracingEnabled()) {
if (xpc_get_type(Msg) == XPC_TYPE_ARRAY) {
switch (readAction(Msg, 0)) {
case trace::ActionKind::OperationStarted: {
size_t Index = 4;
auto Files = readStringPairs(Msg, Index);
auto OpArgs = readStringPairs(Msg, Index);
State::addOperation(Session,
readUint64(Msg, 1),
readOpKind(Msg, 2),
readString(Msg, 3),
std::move(Files),
std::move(OpArgs));
break;
}
case trace::ActionKind::OperationFinished: {
State::removeOperation(Session, readUint64(Msg, 1));
break;
}
}
} else {
llvm::report_fatal_error("Unknown trace message");
}
}
}
void persistTracingData() {
if (isTracingEnabled()) {
State::persistAsync();
}
}

View File

@@ -1,6 +1,6 @@
if (NOT SOURCEKIT_INSTALLING_INPROC)
add_sourcekit_xpc_service(SourceKitService sourcekitd
XPCService.cpp XpcTracing.cpp
XPCService.cpp
LINK_LIBS SourceKitSwiftLang sourcekitdAPI
LLVM_COMPONENT_DEPENDS support coverage
)

View File

@@ -12,7 +12,6 @@
#include "sourcekitd/Internal-XPC.h"
#include "sourcekitd/Logging.h"
#include "sourcekitd/XpcTracing.h"
#include "SourceKit/Core/LLVM.h"
#include "SourceKit/Support/Concurrency.h"
@@ -277,8 +276,6 @@ static void getInitializationInfo(xpc_connection_t peer) {
assert(xpc_get_type(reply) == XPC_TYPE_DICTIONARY);
uint64_t Delay = xpc_dictionary_get_uint64(reply, xpc::KeySemaEditorDelay);
uint64_t TracingEnabled = xpc_dictionary_get_uint64(reply,
xpc::KeyTracingEnabled);
xpc_release(reply);
if (Delay != 0) {
@@ -289,10 +286,6 @@ static void getInitializationInfo(xpc_connection_t peer) {
}
setenv("SOURCEKIT_DELAY_SEMA_EDITOR", Buf.c_str(), /*overwrite=*/1);
}
if (TracingEnabled) {
sourcekitd::trace::enableXPCTracing();
}
}
static void sourcekitdServer_event_handler(xpc_connection_t peer) {
@@ -371,37 +364,3 @@ void SKUIDToUIDMap::set(sourcekitd_uid_t SKDUID, UIdent UID) {
this->Map[SKDUID] = UID;
});
}
void sourcekitd::trace::sendTraceMessage(trace::sourcekitd_trace_message_t Msg) {
xpc_connection_t Peer = MainConnection;
if (!Peer) {
trace::disableXPCTracing();
xpc_release(Msg);
return;
}
xpc_object_t Contents = xpc_array_create(nullptr, 0);
xpc_array_set_uint64(Contents, XPC_ARRAY_APPEND,
static_cast<uint64_t>(xpc::Message::TraceMessage));
xpc_array_set_uint64(Contents, XPC_ARRAY_APPEND,
trace::getTracingSession());
xpc_array_set_value(Contents, XPC_ARRAY_APPEND, Msg);
xpc_release(Msg);
xpc_object_t Message = xpc_dictionary_create(nullptr, nullptr, 0);
xpc_dictionary_set_value(Message, xpc::KeyInternalMsg, Contents);
xpc_release(Contents);
xpc_object_t Reply =
xpc_connection_send_message_with_reply_sync(Peer, Message);
xpc_release(Message);
if (xpc_get_type(Reply) == XPC_TYPE_ERROR) {
trace::disableXPCTracing();
xpc_release(Reply);
return;
}
xpc_release(Reply);
}

View File

@@ -1,139 +0,0 @@
//===--- XpcTracing.cpp ---------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "sourcekitd/XpcTracing.h"
#include "swift/Frontend/Frontend.h"
#include "llvm/Support/YAMLTraits.h"
#include <chrono>
#include <xpc/xpc.h>
using namespace sourcekitd;
using namespace sourcekitd::trace;
using namespace llvm;
//===----------------------------------------------------------------------===//
// General
//===----------------------------------------------------------------------===//
static auto tracing_session = std::chrono::system_clock::now();
uint64_t trace::getTracingSession() {
using namespace std::chrono;
time_point<system_clock, milliseconds> msec =
time_point_cast<milliseconds>(tracing_session);
return msec.time_since_epoch().count();
}
static void append(xpc_object_t Contents, uint64_t Value) {
xpc_array_set_uint64(Contents, XPC_ARRAY_APPEND, Value);
}
static void append(xpc_object_t Contents, ActionKind Value) {
append(Contents, static_cast<uint64_t>(Value));
}
static void append(xpc_object_t Contents, OperationKind Value) {
append(Contents, static_cast<uint64_t>(Value));
}
static void append(xpc_object_t Contents, llvm::StringRef Value) {
xpc_array_set_string(Contents, XPC_ARRAY_APPEND, Value.data());
}
static void append(xpc_object_t Contents, const StringPairs &Files) {
append(Contents, Files.size());
std::for_each(Files.begin(), Files.end(),
[&] (const std::pair<std::string, std::string> &File) {
append(Contents, File.first);
append(Contents, File.second);
});
}
template <>
struct llvm::yaml::MappingTraits<SwiftArguments> {
static void mapping(IO &Io, SwiftArguments &Args) {
Io.mapOptional("PrimaryFile", Args.PrimaryFile, std::string());
Io.mapRequired("CompilerArgs", Args.Args);
}
};
static std::string serializeCompilerArguments(const SwiftArguments &Args) {
// Serialize compiler instance
std::string OptionsAsYaml;
llvm::raw_string_ostream OptionsStream(OptionsAsYaml);
llvm::yaml::Output YamlOutput(OptionsStream);
YamlOutput << const_cast<SwiftArguments &>(Args);
OptionsStream.flush();
return OptionsAsYaml;
}
//===----------------------------------------------------------------------===//
// Trace consumer
//===----------------------------------------------------------------------===//
class XpcTraceConsumer : public SourceKit::trace::TraceConsumer {
public:
virtual ~XpcTraceConsumer() = default;
// Trace start of SourceKit operation
void operationStarted(uint64_t OpId, OperationKind OpKind,
const SwiftInvocation &Inv,
const StringPairs &OpArgs) override;
// Operation previously started with startXXX has finished
void operationFinished(
uint64_t OpId, OperationKind OpKind,
ArrayRef<SourceKit::DiagnosticEntryInfo> Diagnostics) override;
};
// Trace start of SourceKit operation
void XpcTraceConsumer::operationStarted(uint64_t OpId,
OperationKind OpKind,
const SwiftInvocation &Inv,
const StringPairs &OpArgs) {
xpc_object_t Contents = xpc_array_create(nullptr, 0);
append(Contents, ActionKind::OperationStarted);
append(Contents, OpId);
append(Contents, OpKind);
append(Contents, serializeCompilerArguments(Inv.Args));
append(Contents, Inv.Files);
append(Contents, OpArgs);
trace::sendTraceMessage(Contents);
}
// Operation previously started with startXXX has finished
void XpcTraceConsumer::operationFinished(
uint64_t OpId, OperationKind OpKind,
ArrayRef<SourceKit::DiagnosticEntryInfo> Diagnostics) {
xpc_object_t Contents = xpc_array_create(nullptr, 0);
append(Contents, trace::ActionKind::OperationFinished);
append(Contents, OpId);
trace::sendTraceMessage(Contents);
}
static XpcTraceConsumer Instance;
void trace::enableXPCTracing() {
SourceKit::trace::registerConsumer(&Instance);
}
void trace::disableXPCTracing() {
SourceKit::trace::unregisterConsumer(&Instance);
}

View File

@@ -27,7 +27,6 @@ enum class Message {
Initialization,
Notification,
UIDSynchronization,
TraceMessage
};
} // namespace xpc

View File

@@ -1,43 +0,0 @@
//===--- XpcTracing.h - XPC-side Tracing Interface --------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SOURCEKIT_SUPPORT_XPC_TRACING_H
#define LLVM_SOURCEKIT_SUPPORT_XPC_TRACING_H
#include "SourceKit/Support/Tracing.h"
namespace sourcekitd {
namespace trace {
enum class ActionKind : uint64_t {
OperationFinished,
OperationStarted,
};
typedef void *sourcekitd_trace_message_t;
typedef SourceKit::trace::OperationKind OperationKind;
typedef SourceKit::trace::SwiftArguments SwiftArguments;
typedef SourceKit::trace::StringPairs StringPairs;
typedef SourceKit::trace::SwiftInvocation SwiftInvocation;
void sendTraceMessage(sourcekitd_trace_message_t Msg);
uint64_t getTracingSession();
void enableXPCTracing();
void disableXPCTracing();
} // namespace sourcekitd
} // namespace trace
#endif /* defined(LLVM_SOURCEKIT_SUPPORT_XPC_TRACING_H) */