Use the Google Style for clang-format without exceptions (#8706)

This reduces the friction when merging from github and google repos by
using the exact same clang style guide.

MARKDOWN=true
This commit is contained in:
Derek Bailey
2025-09-23 21:19:33 -07:00
committed by GitHub
parent 881eaab706
commit 0e047869da
84 changed files with 8178 additions and 6653 deletions

View File

@@ -24,21 +24,21 @@
#include "flatbuffers/util.h"
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4127) // C4127: conditional expression is constant
#pragma warning(push)
#pragma warning(disable : 4127) // C4127: conditional expression is constant
#endif
namespace flatbuffers {
std::string JavaCSharpMakeRule(const bool java, const Parser &parser,
const std::string &path,
const std::string &file_name) {
std::string JavaCSharpMakeRule(const bool java, const Parser& parser,
const std::string& path,
const std::string& file_name) {
const std::string file_extension = java ? ".java" : ".cs";
std::string make_rule;
for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end();
++it) {
auto &enum_def = **it;
auto& enum_def = **it;
if (!make_rule.empty()) make_rule += " ";
std::string directory =
BaseGenerator::NamespaceDir(parser, path, *enum_def.defined_namespace);
@@ -47,7 +47,7 @@ std::string JavaCSharpMakeRule(const bool java, const Parser &parser,
for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
++it) {
auto &struct_def = **it;
auto& struct_def = **it;
if (!make_rule.empty()) make_rule += " ";
std::string directory = BaseGenerator::NamespaceDir(
parser, path, *struct_def.defined_namespace);
@@ -67,10 +67,14 @@ void CodeWriter::operator+=(std::string text) {
while (true) {
auto begin = text.find("{{");
if (begin == std::string::npos) { break; }
if (begin == std::string::npos) {
break;
}
auto end = text.find("}}");
if (end == std::string::npos || end < begin) { break; }
if (end == std::string::npos || end < begin) {
break;
}
// Write all the text before the first {{ into the stream.
stream_.write(text.c_str(), begin);
@@ -82,7 +86,7 @@ void CodeWriter::operator+=(std::string text) {
// value into the stream, otherwise write the key itself into the stream.
auto iter = value_map_.find(key);
if (iter != value_map_.end()) {
const std::string &value = iter->second;
const std::string& value = iter->second;
stream_ << value;
} else {
FLATBUFFERS_ASSERT(false && "could not find key");
@@ -102,26 +106,26 @@ void CodeWriter::operator+=(std::string text) {
}
}
void CodeWriter::AppendIdent(std::stringstream &stream) {
void CodeWriter::AppendIdent(std::stringstream& stream) {
int lvl = cur_ident_lvl_;
while (lvl--) {
stream.write(pad_.c_str(), static_cast<std::streamsize>(pad_.size()));
}
}
const char *BaseGenerator::FlatBuffersGeneratedWarning() {
const char* BaseGenerator::FlatBuffersGeneratedWarning() {
return "automatically generated by the FlatBuffers compiler,"
" do not modify";
}
std::string BaseGenerator::NamespaceDir(const Parser &parser,
const std::string &path,
const Namespace &ns,
std::string BaseGenerator::NamespaceDir(const Parser& parser,
const std::string& path,
const Namespace& ns,
const bool dasherize) {
EnsureDirExists(path);
if (parser.opts.one_file) return path;
std::string namespace_dir = path; // Either empty or ends in separator.
auto &namespaces = ns.components;
auto& namespaces = ns.components;
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
namespace_dir +=
!dasherize ? *it : ConvertCase(*it, Case::kDasher, Case::kUpperCamel);
@@ -131,15 +135,15 @@ std::string BaseGenerator::NamespaceDir(const Parser &parser,
return namespace_dir;
}
std::string BaseGenerator::NamespaceDir(const Namespace &ns,
std::string BaseGenerator::NamespaceDir(const Namespace& ns,
const bool dasherize) const {
return BaseGenerator::NamespaceDir(parser_, path_, ns, dasherize);
}
std::string BaseGenerator::FullNamespace(const char *separator,
const Namespace &ns) {
std::string BaseGenerator::FullNamespace(const char* separator,
const Namespace& ns) {
std::string namespace_name;
auto &namespaces = ns.components;
auto& namespaces = ns.components;
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
if (namespace_name.length()) namespace_name += separator;
namespace_name += *it;
@@ -147,7 +151,7 @@ std::string BaseGenerator::FullNamespace(const char *separator,
return namespace_name;
}
std::string BaseGenerator::LastNamespacePart(const Namespace &ns) {
std::string BaseGenerator::LastNamespacePart(const Namespace& ns) {
if (!ns.components.empty())
return ns.components.back();
else
@@ -155,21 +159,21 @@ std::string BaseGenerator::LastNamespacePart(const Namespace &ns) {
}
// Ensure that a type is prefixed with its namespace.
std::string BaseGenerator::WrapInNameSpace(const Namespace *ns,
const std::string &name) const {
std::string BaseGenerator::WrapInNameSpace(const Namespace* ns,
const std::string& name) const {
std::string qualified_name = qualifying_start_;
for (auto it = ns->components.begin(); it != ns->components.end(); ++it)
qualified_name += *it + qualifying_separator_;
return qualified_name + name;
}
std::string BaseGenerator::WrapInNameSpace(const Definition &def,
const std::string &suffix) const {
std::string BaseGenerator::WrapInNameSpace(const Definition& def,
const std::string& suffix) const {
return WrapInNameSpace(def.defined_namespace, def.name + suffix);
}
std::string BaseGenerator::GetNameSpace(const Definition &def) const {
const Namespace *ns = def.defined_namespace;
std::string BaseGenerator::GetNameSpace(const Definition& def) const {
const Namespace* ns = def.defined_namespace;
if (CurrentNameSpace() == ns) return "";
std::string qualified_name = qualifying_start_;
for (auto it = ns->components.begin(); it != ns->components.end(); ++it) {
@@ -182,23 +186,23 @@ std::string BaseGenerator::GetNameSpace(const Definition &def) const {
return qualified_name;
}
std::string BaseGenerator::GeneratedFileName(const std::string &path,
const std::string &file_name,
const IDLOptions &options) const {
std::string BaseGenerator::GeneratedFileName(const std::string& path,
const std::string& file_name,
const IDLOptions& options) const {
return path + file_name + options.filename_suffix + "." +
(options.filename_extension.empty() ? default_extension_
: options.filename_extension);
}
// Generate a documentation comment, if available.
void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
const CommentConfig *config, const char *prefix) {
void GenComment(const std::vector<std::string>& dc, std::string* code_ptr,
const CommentConfig* config, const char* prefix) {
if (dc.begin() == dc.end()) {
// Don't output empty comment blocks with 0 lines of comment content.
return;
}
std::string &code = *code_ptr;
std::string& code = *code_ptr;
if (config != nullptr && config->first_line != nullptr) {
code += std::string(prefix) + std::string(config->first_line) + "\n";
}
@@ -215,10 +219,10 @@ void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
}
}
template<typename T>
template <typename T>
std::string FloatConstantGenerator::GenFloatConstantImpl(
const FieldDef &field) const {
const auto &constant = field.value.constant;
const FieldDef& field) const {
const auto& constant = field.value.constant;
T v;
auto done = StringToNumber(constant.c_str(), &v);
FLATBUFFERS_ASSERT(done);
@@ -233,10 +237,12 @@ std::string FloatConstantGenerator::GenFloatConstantImpl(
}
std::string FloatConstantGenerator::GenFloatConstant(
const FieldDef &field) const {
const FieldDef& field) const {
switch (field.value.type.base_type) {
case BASE_TYPE_FLOAT: return GenFloatConstantImpl<float>(field);
case BASE_TYPE_DOUBLE: return GenFloatConstantImpl<double>(field);
case BASE_TYPE_FLOAT:
return GenFloatConstantImpl<float>(field);
case BASE_TYPE_DOUBLE:
return GenFloatConstantImpl<double>(field);
default: {
FLATBUFFERS_ASSERT(false);
return "INVALID_BASE_TYPE";
@@ -245,9 +251,9 @@ std::string FloatConstantGenerator::GenFloatConstant(
}
TypedFloatConstantGenerator::TypedFloatConstantGenerator(
const char *double_prefix, const char *single_prefix,
const char *nan_number, const char *pos_inf_number,
const char *neg_inf_number)
const char* double_prefix, const char* single_prefix,
const char* nan_number, const char* pos_inf_number,
const char* neg_inf_number)
: double_prefix_(double_prefix),
single_prefix_(single_prefix),
nan_number_(nan_number),
@@ -255,11 +261,11 @@ TypedFloatConstantGenerator::TypedFloatConstantGenerator(
neg_inf_number_(neg_inf_number) {}
std::string TypedFloatConstantGenerator::MakeNaN(
const std::string &prefix) const {
const std::string& prefix) const {
return prefix + nan_number_;
}
std::string TypedFloatConstantGenerator::MakeInf(
bool neg, const std::string &prefix) const {
bool neg, const std::string& prefix) const {
if (neg)
return !neg_inf_number_.empty() ? (prefix + neg_inf_number_)
: ("-" + prefix + pos_inf_number_);
@@ -268,7 +274,7 @@ std::string TypedFloatConstantGenerator::MakeInf(
}
std::string TypedFloatConstantGenerator::Value(double v,
const std::string &src) const {
const std::string& src) const {
(void)v;
return src;
}
@@ -283,7 +289,7 @@ std::string TypedFloatConstantGenerator::NaN(double v) const {
}
std::string TypedFloatConstantGenerator::Value(float v,
const std::string &src) const {
const std::string& src) const {
(void)v;
return src + "f";
}
@@ -298,14 +304,14 @@ std::string TypedFloatConstantGenerator::NaN(float v) const {
}
SimpleFloatConstantGenerator::SimpleFloatConstantGenerator(
const char *nan_number, const char *pos_inf_number,
const char *neg_inf_number)
const char* nan_number, const char* pos_inf_number,
const char* neg_inf_number)
: nan_number_(nan_number),
pos_inf_number_(pos_inf_number),
neg_inf_number_(neg_inf_number) {}
std::string SimpleFloatConstantGenerator::Value(double v,
const std::string &src) const {
const std::string& src) const {
(void)v;
return src;
}
@@ -320,7 +326,7 @@ std::string SimpleFloatConstantGenerator::NaN(double v) const {
}
std::string SimpleFloatConstantGenerator::Value(float v,
const std::string &src) const {
const std::string& src) const {
return this->Value(static_cast<double>(v), src);
}
@@ -335,5 +341,5 @@ std::string SimpleFloatConstantGenerator::NaN(float v) const {
} // namespace flatbuffers
#if defined(_MSC_VER)
# pragma warning(pop)
#pragma warning(pop)
#endif