Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flatc '--cpp_std' switch #5656

Merged
merged 8 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix --cpp_std values: c++0x, c++11, c++17
  • Loading branch information
vglavnyy committed Dec 19, 2019
commit c17e1ec15b8cd74344ad94607b4b053848a92b05
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ option(FLATBUFFERS_BUILD_CPP17
Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher."
OFF)
option(FLATBUFFERS_BUILD_LEGACY
"Run C++ code generator with '--cpp-std legacy' switch."
"Run C++ code generator with '--cpp-std c++0x' switch."
OFF)

if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
Expand Down Expand Up @@ -342,7 +342,7 @@ endif()

function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT)
if(FLATBUFFERS_BUILD_LEGACY)
set(OPT ${OPT};--cpp-std legacy)
set(OPT ${OPT};--cpp-std c++0x)
endif()
message(STATUS "Add the code-generation command for the `${SRC_FBS}` schema with '${OPT}'")
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ environment:
matrix:
- CMAKE_VS_VERSION: "10 2010"
CMAKE_OPTIONS: "-DFLATBUFFERS_BUILD_LEGACY=1"
CPP_TEST_OPTIONS: "--std-cpp legacy"
CPP_TEST_OPTIONS: "--std-cpp c++0x"
MONSTER_EXTRA: "skip"

- CMAKE_VS_VERSION: "12 2013"
Expand Down
4 changes: 2 additions & 2 deletions docs/source/Compiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ Additional options:

- `--cpp-std CPP_STD` : Generate a C++ code using features of selected C++ standard.
Supported `CPP_STD` values:
* `c++` - use a standard code generation rules;
* `legacy` - generate code compatible with old compilers (VS2010).
* `c++0x` - generate code compatible with old compilers (VS2010).
* `c++11` - use C++11 code generator (default);

- `--object-prefix` : Customise class prefix for C++ object-based API.

Expand Down
11 changes: 2 additions & 9 deletions include/flatbuffers/flatc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@

namespace flatbuffers {

class FlatCompilerLogger {
FlatCompilerLogger() {}
public:
// There is no way to notify about an error from a code-generator
// except `assert` call.
// TODO: re-design interface to code generators.
static void Warn(const std::string &warn);
static void Error(const std::string &err);
};
extern void LogCompilerWarn(const std::string &warn);
extern void LogCompilerError(const std::string &err);

class FlatCompiler {
public:
Expand Down
2 changes: 1 addition & 1 deletion reflection/generate_code.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
set buildtype=Release
if "%1"=="-b" set buildtype=%2

..\%buildtype%\flatc.exe --cpp --cpp-std legacy --no-prefix -o ../include/flatbuffers reflection.fbs || exit /b 1
..\%buildtype%\flatc.exe --cpp --cpp-std c++0x --no-prefix -o ../include/flatbuffers reflection.fbs || exit /b 1
2 changes: 1 addition & 1 deletion reflection/generate_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
set -e

../flatc -c --cpp-std legacy --no-prefix -o ../include/flatbuffers reflection.fbs
../flatc -c --cpp-std c++0x --no-prefix -o ../include/flatbuffers reflection.fbs
6 changes: 3 additions & 3 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" from Flatbuffers, but (char* + length).\n"
" --cpp-std CPP_STD Generate a C++ code using features of selected C++ standard.\n"
" Supported CPP_STD values:\n"
" * 'legacy' - generate code compatible with old compilers.\n"
" * 'c++' - use default code generation rules (default);\n"
" * 'c++17' - use C++17 features in generated code (experemental);\n"
" * 'c++0x' - generate code compatible with old compilers;\n"
" * 'c++11' - use C++11 code generator (default);\n"
" * 'c++17' - use C++17 features in generated code (experimental).\n"
" --object-prefix Customise class prefix for C++ object-based API.\n"
" --object-suffix Customise class suffix for C++ object-based API.\n"
" Default value is \"T\".\n"
Expand Down
10 changes: 5 additions & 5 deletions src/flatc_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ static void Error(const flatbuffers::FlatCompiler *flatc,
}

namespace flatbuffers {
void FlatCompilerLogger::Warn(const std::string &warn) {
::Warn(static_cast<const flatbuffers::FlatCompiler *>(nullptr), warn, true);
void LogCompilerWarn(const std::string &warn) {
Warn(static_cast<const flatbuffers::FlatCompiler *>(nullptr), warn, true);
}
void FlatCompilerLogger::Error(const std::string &err) {
::Error(static_cast<const flatbuffers::FlatCompiler *>(nullptr), err, false,
true);
void LogCompilerError(const std::string &err) {
Error(static_cast<const flatbuffers::FlatCompiler *>(nullptr), err, false,
true);
}
} // namespace flatbuffers

Expand Down
28 changes: 16 additions & 12 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ namespace cpp {
// Extension of IDLOptions for cpp-generator.
struct IDLOptionsCpp : public IDLOptions {
vglavnyy marked this conversation as resolved.
Show resolved Hide resolved
// All fields start with 'g_' prefix to distinguish from the base IDLOptions.
int g_cpp_std; // base version of C++ standard (0, 17)
bool g_only_fixed_enums;
int g_cpp_std; // Base version of C++ standard (0, 11, 17).
bool g_only_fixed_enums; // Generate underlaying type for all enums.
// clang-format off
IDLOptionsCpp(const IDLOptions &opts)
: IDLOptions(opts),
g_cpp_std(0),
g_cpp_std(11),
g_only_fixed_enums(true)
{}
// clang-format on
Expand Down Expand Up @@ -728,7 +728,7 @@ class CppGenerator : public BaseGenerator {
auto element_type = type.VectorType();
// Check if enum arrays are used in C++ without specifying --scoped-enums
if (IsEnum(element_type) && !opts_.g_only_fixed_enums) {
FlatCompilerLogger::Error(
LogCompilerError(
"--scoped-enums must be enabled to use enum arrays in C++");
FLATBUFFERS_ASSERT(true);
}
Expand Down Expand Up @@ -2985,19 +2985,23 @@ bool GenerateCPP(const Parser &parser, const std::string &path,
cpp::IDLOptionsCpp opts(parser.opts);
// The '--cpp_std' argument could be extended (like ASAN):
// Example: "flatc --cpp_std c++17:option1:option2".
if (opts.cpp_std.empty() || opts.cpp_std == "c++") {
// Use a `standard` code generator.
} else if (opts.cpp_std == "c++17") {
auto cpp_std = opts.cpp_std;
std::transform(cpp_std.begin(), cpp_std.end(), cpp_std.begin(), ToUpper);
if (cpp_std.empty() || cpp_std == "C++11") {
// Use the standard C++11 code generator.
opts.g_cpp_std = 11;
} else if (cpp_std == "C++0X") {
opts.g_cpp_std = 0;
opts.g_only_fixed_enums = false;
} else if (cpp_std == "C++17") {
opts.g_cpp_std = 17;
// With c++17 generate strong enums only.
opts.scoped_enums = true;
// By default, prefixed_enums==true, reset it.
opts.prefixed_enums = false;
} else if (opts.cpp_std == "legacy") {
opts.g_cpp_std = -1;
opts.g_only_fixed_enums = false;
} else {
FlatCompilerLogger::Error("Unknown value of the '--cpp-std' switch: " +
opts.cpp_std);
LogCompilerError("Unknown value of the '--cpp-std' switch: " +
opts.cpp_std);
return false;
}
// The opts.scoped_enums has priority.
Expand Down
2 changes: 1 addition & 1 deletion tests/generate_code.bat
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if "%1"=="-b" set buildtype=%2

set commandline=%*
set TEST_CPP_FLAGS=
if NOT "%commandline%"=="%commandline:--cpp-std legacy=%" set TEST_CPP_FLAGS=--cpp-std legacy %TEST_CPP_FLAGS%
if NOT "%commandline%"=="%commandline:--cpp-std c++0x=%" set TEST_CPP_FLAGS=--cpp-std c++0x %TEST_CPP_FLAGS%

set TEST_CPP_FLAGS=--gen-compare --cpp-ptr-type flatbuffers::unique_ptr %TEST_CPP_FLAGS%
set TEST_BASE_FLAGS=--reflect-names --gen-mutable --gen-object-api
Expand Down
6 changes: 3 additions & 3 deletions tests/generate_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ set -e

commandline="'$*'"
TEST_CPP_FLAGS=
if [[ $commandline == *"--cpp-std legacy"* ]]; then
TEST_CPP_FLAGS="--cpp-std legacy $TEST_CPP_FLAGS"
if [[ $commandline == *"--cpp-std c++0x"* ]]; then
TEST_CPP_FLAGS="--cpp-std c++0x $TEST_CPP_FLAGS"
fi

TEST_CPP_FLAGS="--gen-compare --cpp-ptr-type flatbuffers::unique_ptr $TEST_CPP_FLAGS"
Expand Down Expand Up @@ -49,4 +49,4 @@ cd ../samples
../flatc --cpp --lobster $TEST_BASE_FLAGS $TEST_CPP_FLAGS monster.fbs
../flatc -b --schema --bfbs-comments --bfbs-builtins monster.fbs
cd ../reflection
./generate_code.sh --cpp-std legacy
./generate_code.sh --cpp-std c++0x