Skip to content

Commit

Permalink
clang-format
Browse files Browse the repository at this point in the history
DEF CON 26 CTF
  • Loading branch information
MaskRay committed Oct 24, 2019
1 parent 87f36a4 commit 39787d2
Show file tree
Hide file tree
Showing 87 changed files with 2,075 additions and 2,389 deletions.
366 changes: 185 additions & 181 deletions src/clang_complete.cc

Large diffs are not rendered by default.

59 changes: 26 additions & 33 deletions src/clang_complete.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,34 @@ struct CompletionSession
};

Project::Entry file;
WorkingFiles* working_files;
WorkingFiles *working_files;

Tu completion;
Tu diagnostics;

CompletionSession(const Project::Entry& file, WorkingFiles* wfiles)
CompletionSession(const Project::Entry &file, WorkingFiles *wfiles)
: file(file), working_files(wfiles) {}
};

struct ClangCompleteManager {
using OnDiagnostic =
std::function<void(std::string path,
std::vector<lsDiagnostic> diagnostics)>;
using OnComplete =
std::function<void(const std::vector<lsCompletionItem>& results,
bool is_cached_result)>;
using OnDiagnostic = std::function<void(
std::string path, std::vector<lsDiagnostic> diagnostics)>;
using OnComplete = std::function<void(
const std::vector<lsCompletionItem> &results, bool is_cached_result)>;
using OnDropped = std::function<void(lsRequestId request_id)>;

struct PreloadRequest {
PreloadRequest(const std::string& path)
PreloadRequest(const std::string &path)
: request_time(std::chrono::high_resolution_clock::now()), path(path) {}

std::chrono::time_point<std::chrono::high_resolution_clock> request_time;
std::string path;
};
struct CompletionRequest {
CompletionRequest(const lsRequestId& id,
const lsTextDocumentIdentifier& document,
const lsPosition& position,
const OnComplete& on_complete)
: id(id),
document(document),
position(position),
CompletionRequest(const lsRequestId &id,
const lsTextDocumentIdentifier &document,
const lsPosition &position, const OnComplete &on_complete)
: id(id), document(document), position(position),
on_complete(on_complete) {}

lsRequestId id;
Expand All @@ -70,42 +65,40 @@ struct ClangCompleteManager {
lsTextDocumentIdentifier document;
};

ClangCompleteManager(Project* project,
WorkingFiles* working_files,
OnDiagnostic on_diagnostic,
OnDropped on_dropped);
ClangCompleteManager(Project *project, WorkingFiles *working_files,
OnDiagnostic on_diagnostic, OnDropped on_dropped);

// Start a code completion at the given location. |on_complete| will run when
// completion results are available. |on_complete| may run on any thread.
void CodeComplete(const lsRequestId& request_id,
const lsTextDocumentPositionParams& completion_location,
const OnComplete& on_complete);
void CodeComplete(const lsRequestId &request_id,
const lsTextDocumentPositionParams &completion_location,
const OnComplete &on_complete);
// Request a diagnostics update.
void DiagnosticsUpdate(const lsTextDocumentIdentifier& document);
void DiagnosticsUpdate(const lsTextDocumentIdentifier &document);

// Notify the completion manager that |filename| has been viewed and we
// should begin preloading completion data.
void NotifyView(const std::string& filename);
void NotifyView(const std::string &filename);
// Notify the completion manager that |filename| has been edited.
void NotifyEdit(const std::string& filename);
void NotifyEdit(const std::string &filename);
// Notify the completion manager that |filename| has been saved. This
// triggers a reparse.
void NotifySave(const std::string& filename);
void NotifySave(const std::string &filename);
// Notify the completion manager that |filename| has been closed. Any existing
// completion session will be dropped.
void NotifyClose(const std::string& filename);
void NotifyClose(const std::string &filename);

// Ensures there is a completion or preloaded session. Returns true if a new
// session was created.
bool EnsureCompletionOrCreatePreloadSession(const std::string& filename);
bool EnsureCompletionOrCreatePreloadSession(const std::string &filename);
// Tries to find an edit session for |filename|. This will move the session
// from view to edit.
std::shared_ptr<CompletionSession> TryGetSession(const std::string& filename,
std::shared_ptr<CompletionSession> TryGetSession(const std::string &filename,
bool mark_as_completion,
bool create_if_needed);

// Flushes all saved sessions with the supplied filename
void FlushSession(const std::string& filename);
void FlushSession(const std::string &filename);
// Flushes all saved sessions
void FlushAllSessions(void);

Expand All @@ -114,8 +107,8 @@ struct ClangCompleteManager {
const int kMaxCompletionSessions = 5;

// Global state.
Project* project_;
WorkingFiles* working_files_;
Project *project_;
WorkingFiles *working_files_;
OnDiagnostic on_diagnostic_;
OnDropped on_dropped_;

Expand Down
7 changes: 3 additions & 4 deletions src/clang_tu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
const std::string &filepath, const std::vector<std::string> &args,
const WorkingFiles::Snapshot &snapshot, bool diagnostic) {
std::vector<const char *> Args;
for (auto& arg : args)
for (auto &arg : args)
Args.push_back(arg.c_str());
Args.push_back("-fallow-editor-placeholders");
if (!diagnostic)
Expand Down Expand Up @@ -103,9 +103,8 @@ std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
ret->PCHCO->getRawReader().getFormat(), &ErrUnit));
};
if (!CRC.RunSafely(parse)) {
LOG_S(ERROR)
<< "clang crashed for " << filepath << "\n"
<< StringJoin(args, " ") + " -fsyntax-only";
LOG_S(ERROR) << "clang crashed for " << filepath << "\n"
<< StringJoin(args, " ") + " -fsyntax-only";
return {};
}
if (!Unit && !ErrUnit)
Expand Down
10 changes: 5 additions & 5 deletions src/clang_tu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include <llvm/Support/CrashRecoveryContext.h>

#include <memory>
#include <stdlib.h>
#include <string>
#include <vector>
#include <stdlib.h>

std::vector<clang::ASTUnit::RemappedFile>
GetRemapped(const WorkingFiles::Snapshot &snapshot);
Expand All @@ -19,12 +19,12 @@ Range FromCharSourceRange(const clang::SourceManager &SM,
clang::CharSourceRange R,
llvm::sys::fs::UniqueID *UniqueID = nullptr);

Range FromCharRange(const clang::SourceManager &SM, const clang::LangOptions &LangOpts,
clang::SourceRange R,
Range FromCharRange(const clang::SourceManager &SM,
const clang::LangOptions &LangOpts, clang::SourceRange R,
llvm::sys::fs::UniqueID *UniqueID = nullptr);

Range FromTokenRange(const clang::SourceManager &SM, const clang::LangOptions &LangOpts,
clang::SourceRange R,
Range FromTokenRange(const clang::SourceManager &SM,
const clang::LangOptions &LangOpts, clang::SourceRange R,
llvm::sys::fs::UniqueID *UniqueID = nullptr);

struct ClangTranslationUnit {
Expand Down
4 changes: 2 additions & 2 deletions src/clang_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using namespace clang;
using namespace llvm;

std::string FileName(const FileEntry& file) {
std::string FileName(const FileEntry &file) {
StringRef Name = file.tryGetRealPathName();
if (Name.empty())
Name = file.getName();
Expand All @@ -25,7 +25,7 @@ std::string FileName(const FileEntry& file) {
}

// clang::BuiltinType::getName without PrintingPolicy
const char* ClangBuiltinTypeName(int kind) {
const char *ClangBuiltinTypeName(int kind) {
switch (BuiltinType::Kind(kind)) {
case BuiltinType::Void:
return "void";
Expand Down
4 changes: 2 additions & 2 deletions src/clang_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#include <string>

// Returns the absolute path to |file|.
std::string FileName(const clang::FileEntry& file);
std::string FileName(const clang::FileEntry &file);

const char* ClangBuiltinTypeName(int);
const char *ClangBuiltinTypeName(int);
2 changes: 1 addition & 1 deletion src/config.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "config.h"

Config* g_config;
Config *g_config;
thread_local int g_thread_id;
56 changes: 15 additions & 41 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,49 +218,23 @@ struct Config {
MAKE_REFLECT_STRUCT(Config::Clang, extraArgs, resourceDir);
MAKE_REFLECT_STRUCT(Config::ClientCapability, snippetSupport);
MAKE_REFLECT_STRUCT(Config::CodeLens, localVariables);
MAKE_REFLECT_STRUCT(Config::Completion,
caseSensitivity,
dropOldRequests,
detailedLabel,
filterAndSort,
includeBlacklist,
includeMaxPathSize,
includeSuffixWhitelist,
MAKE_REFLECT_STRUCT(Config::Completion, caseSensitivity, dropOldRequests,
detailedLabel, filterAndSort, includeBlacklist,
includeMaxPathSize, includeSuffixWhitelist,
includeWhitelist);
MAKE_REFLECT_STRUCT(Config::Diagnostics,
blacklist,
frequencyMs,
onParse,
onType,
whitelist)
MAKE_REFLECT_STRUCT(Config::Diagnostics, blacklist, frequencyMs, onParse,
onType, whitelist)
MAKE_REFLECT_STRUCT(Config::Highlight, lsRanges, blacklist, whitelist)
MAKE_REFLECT_STRUCT(Config::Index,
attributeMakeCallsToCtor,
blacklist,
comments,
enabled,
onDidChange,
reparseForDependency,
threads,
whitelist);
MAKE_REFLECT_STRUCT(Config::Index, attributeMakeCallsToCtor, blacklist,
comments, enabled, onDidChange, reparseForDependency,
threads, whitelist);
MAKE_REFLECT_STRUCT(Config::WorkspaceSymbol, caseSensitivity, maxNum, sort);
MAKE_REFLECT_STRUCT(Config::Xref, container, maxNum);
MAKE_REFLECT_STRUCT(Config,
compilationDatabaseCommand,
compilationDatabaseDirectory,
cacheDirectory,
cacheFormat,

clang,
client,
codeLens,
completion,
diagnostics,
highlight,
index,
largeFileSize,
workspaceSymbol,
xref);

extern Config* g_config;
MAKE_REFLECT_STRUCT(Config, compilationDatabaseCommand,
compilationDatabaseDirectory, cacheDirectory, cacheFormat,

clang, client, codeLens, completion, diagnostics, highlight,
index, largeFileSize, workspaceSymbol, xref);

extern Config *g_config;
thread_local extern int g_thread_id;
41 changes: 21 additions & 20 deletions src/file_consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace {

std::optional<std::string> GetFileContents(
const std::string& path,
std::unordered_map<std::string, FileContents>* file_contents) {
std::optional<std::string>
GetFileContents(const std::string &path,
std::unordered_map<std::string, FileContents> *file_contents) {
auto it = file_contents->find(path);
if (it == file_contents->end()) {
std::optional<std::string> content = ReadContent(path);
Expand All @@ -21,9 +21,9 @@ std::optional<std::string> GetFileContents(
return it->second.content;
}

} // namespace
} // namespace

FileContents::FileContents(const std::string& path, const std::string& content)
FileContents::FileContents(const std::string &path, const std::string &content)
: path(path), content(content) {
line_offsets_.push_back(0);
for (size_t i = 0; i < content.size(); i++) {
Expand All @@ -43,23 +43,23 @@ std::optional<int> FileContents::ToOffset(Position p) const {

std::optional<std::string> FileContents::ContentsInRange(Range range) const {
std::optional<int> start_offset = ToOffset(range.start),
end_offset = ToOffset(range.end);
end_offset = ToOffset(range.end);
if (start_offset && end_offset && *start_offset < *end_offset)
return content.substr(*start_offset, *end_offset - *start_offset);
return std::nullopt;
}

VFS::State VFS::Get(const std::string& file) {
VFS::State VFS::Get(const std::string &file) {
std::lock_guard<std::mutex> lock(mutex);
auto it = state.find(file);
if (it != state.end())
return it->second;
return {0, 0, 0};
}

bool VFS::Mark(const std::string& file, int owner, int stage) {
bool VFS::Mark(const std::string &file, int owner, int stage) {
std::lock_guard<std::mutex> lock(mutex);
State& st = state[file];
State &st = state[file];
if (st.stage < stage) {
st.owner = owner;
st.stage = stage;
Expand All @@ -68,33 +68,33 @@ bool VFS::Mark(const std::string& file, int owner, int stage) {
return false;
}

bool VFS::Stamp(const std::string& file, int64_t ts) {
bool VFS::Stamp(const std::string &file, int64_t ts) {
std::lock_guard<std::mutex> lock(mutex);
State& st = state[file];
State &st = state[file];
if (st.timestamp < ts) {
st.timestamp = ts;
return true;
} else
return false;
}

void VFS::ResetLocked(const std::string& file) {
State& st = state[file];
void VFS::ResetLocked(const std::string &file) {
State &st = state[file];
if (st.owner == 0 || st.owner == g_thread_id)
st.stage = 0;
}

void VFS::Reset(const std::string& file) {
void VFS::Reset(const std::string &file) {
std::lock_guard<std::mutex> lock(mutex);
ResetLocked(file);
}

FileConsumer::FileConsumer(VFS* vfs, const std::string& parse_file)
FileConsumer::FileConsumer(VFS *vfs, const std::string &parse_file)
: vfs_(vfs), parse_file_(parse_file), thread_id_(g_thread_id) {}

IndexFile* FileConsumer::TryConsumeFile(
const clang::FileEntry& File,
std::unordered_map<std::string, FileContents>* file_contents_map) {
IndexFile *FileConsumer::TryConsumeFile(
const clang::FileEntry &File,
std::unordered_map<std::string, FileContents> *file_contents_map) {
auto UniqueID = File.getUniqueID();
auto it = local_.find(UniqueID);
if (it != local_.end())
Expand All @@ -115,13 +115,14 @@ IndexFile* FileConsumer::TryConsumeFile(
return nullptr;

// Build IndexFile instance.
local_[UniqueID] = std::make_unique<IndexFile>(UniqueID, file_name, *contents);
local_[UniqueID] =
std::make_unique<IndexFile>(UniqueID, file_name, *contents);
return local_[UniqueID].get();
}

std::vector<std::unique_ptr<IndexFile>> FileConsumer::TakeLocalState() {
std::vector<std::unique_ptr<IndexFile>> result;
for (auto& entry : local_) {
for (auto &entry : local_) {
if (entry.second)
result.push_back(std::move(entry.second));
}
Expand Down
Loading

0 comments on commit 39787d2

Please sign in to comment.