Make NTString nullable

This commit is contained in:
Fangrui Song 2018-02-11 09:12:22 -08:00
parent 68c5c317f0
commit 0f8734c416
5 changed files with 35 additions and 33 deletions

View File

@ -228,12 +228,12 @@ std::string ClangCursor::get_type_description() const {
return ::ToString(clang_getTypeSpelling(type));
}
std::string ClangCursor::get_comments() const {
NTString ClangCursor::get_comments() const {
if (!g_index_comments)
return "";
return {};
CXSourceRange range = clang_Cursor_getCommentRange(cx_cursor);
if (clang_Range_isNull(range))
return "";
return {};
unsigned start_column;
clang_getSpellingLocation(clang_getRangeStart(range), nullptr, nullptr,
@ -286,7 +286,9 @@ std::string ClangCursor::get_comments() const {
}
while (ret.size() && isspace(ret.back()))
ret.pop_back();
return ret;
if (ret.empty())
return {};
return static_cast<std::string_view>(ret);
}
std::string ClangCursor::ToString() const {

View File

@ -1,6 +1,7 @@
#pragma once
#include "position.h"
#include "ntstring.h"
#include <clang-c/Index.h>
#include <optional.h>
@ -79,7 +80,7 @@ class ClangCursor {
bool is_valid_kind() const;
std::string get_type_description() const;
std::string get_comments() const;
NTString get_comments() const;
std::string ToString() const;

View File

@ -15,7 +15,7 @@ REGISTER_IPC_MESSAGE(Ipc_CqueryRandom);
const double kDeclWeight = 3;
const double kDamping = 0.1;
const double kAlpha = 0.3;
const double kAlpha = 2.80777024202851936522;
template <typename Q>
struct Kind;

View File

@ -9,32 +9,26 @@
class Reader;
class Writer;
// Null-terminated string
// Nullable null-terminated string, which is null if default constructed,
// but non-null if assigned.
// This is used in Query{Func,Type,Var}::def to reduce memory footprint.
class NTString {
using size_type = std::string::size_type;
std::unique_ptr<char[]> str;
public:
NTString() : str(new char[1]()) {}
NTString(const NTString& o) : str(new char[strlen(o.c_str()) + 1]) {
strcpy(str.get(), o.c_str());
}
NTString() = default;
NTString(NTString&& o) = default;
NTString(std::string_view sv) {
*this = sv;
}
NTString(const NTString& o) { *this = o; }
NTString(std::string_view sv) { *this = sv; }
operator std::string_view() const { return str.get(); }
const char* c_str() const { return str.get(); }
std::string_view substr(size_type pos, size_type cnt) const {
return std::string_view(c_str() + pos, cnt);
}
bool empty() const { return !str || str.get()[0] == '\0'; }
size_type find(const char* s) {
const char *p = strstr(c_str(), s);
return p ? std::string::size_type(p - c_str()) : std::string::npos;
operator std::string_view() const {
if (c_str())
return c_str();
return {};
}
bool empty() const { return !str || *c_str() == '\0'; }
void operator=(std::string_view sv) {
str = std::unique_ptr<char[]>(new char[sv.size() + 1]);
@ -45,6 +39,7 @@ class NTString {
*this = static_cast<std::string_view>(o);
}
bool operator==(const NTString& o) const {
return strcmp(c_str(), o.c_str()) == 0;
return str && o.str ? strcmp(c_str(), o.c_str()) == 0
: c_str() == o.c_str();
}
};

View File

@ -40,7 +40,9 @@ optional<QueryType::Def> ToQuery(const IdMap& id_map,
result.short_name_offset = type.short_name_offset;
result.short_name_size = type.short_name_size;
result.kind = type.kind;
if (!type.hover.empty())
result.hover = type.hover;
if (!type.comments.empty())
result.comments = type.comments;
result.file = id_map.primary_file;
result.spell = id_map.ToQuery(type.spell);
@ -64,7 +66,9 @@ optional<QueryFunc::Def> ToQuery(const IdMap& id_map,
result.short_name_size = func.short_name_size;
result.kind = func.kind;
result.storage = func.storage;
if (!func.hover.empty())
result.hover = func.hover;
if (!func.comments.empty())
result.comments = func.comments;
result.file = id_map.primary_file;
result.spell = id_map.ToQuery(func.spell);
@ -84,13 +88,13 @@ optional<QueryVar::Def> ToQuery(const IdMap& id_map, const IndexVar::Def& var) {
result.detailed_name = var.detailed_name;
result.short_name_offset = var.short_name_offset;
result.short_name_size = var.short_name_size;
if (!var.hover.empty())
result.hover = var.hover;
if (!var.comments.empty())
result.comments = var.comments;
result.file = id_map.primary_file;
if (var.spell)
result.spell = id_map.ToQuery(*var.spell);
if (var.extent)
result.extent = id_map.ToQuery(*var.extent);
result.spell = id_map.ToQuery(var.spell);
result.extent = id_map.ToQuery(var.extent);
result.variable_type = id_map.ToQuery(var.variable_type);
if (result.parent_id)
switch (var.parent_kind) {