From d14223695ac9c830aa2bf03679af4527d5f1f597 Mon Sep 17 00:00:00 2001 From: Bruno Ribeiro Date: Fri, 28 Dec 2018 09:00:15 -0200 Subject: [PATCH] Add validation to avoid mixed versions of generated protogen code --- CMakeLists.txt | 3 +++ compiler/main.cc | 2 +- library/cppgen.cc | 12 +++++++++++- library/proto3.cc | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e6d4cfd..4cacf74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,9 @@ set(PROTOGEN_MAJOR 0) set(PROTOGEN_MINOR 4) set(PROTOGEN_PATCH 0) +add_definitions(-DPROTOGEN_MAJOR=${PROTOGEN_MAJOR}) +add_definitions(-DPROTOGEN_MINOR=${PROTOGEN_MINOR}) +add_definitions(-DPROTOGEN_PATCH=${PROTOGEN_PATCH}) add_definitions(-DPROTOGEN_VERSION="${PROTOGEN_MAJOR}.${PROTOGEN_MINOR}.${PROTOGEN_PATCH}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wfatal-errors -pedantic -std=c++11 -Wl,--no-undefined -fPIC -Wall -Wextra -Wconversion -Werror=return-type -Werror=implicit-function-declaration") diff --git a/compiler/main.cc b/compiler/main.cc index 860a9db..56c9f25 100644 --- a/compiler/main.cc +++ b/compiler/main.cc @@ -40,7 +40,7 @@ int main( int argc, char **argv ) protogen::Proto3 proto; try { - protogen::Proto3::parse(proto, input, argv[1]); + protogen::Proto3::parse(proto, input, fullPath); protogen::CppGenerator gen; gen.generate(proto, output); } catch (protogen::exception &ex) diff --git a/library/cppgen.cc b/library/cppgen.cc index 34fa3ae..27c2e2a 100644 --- a/library/cppgen.cc +++ b/library/cppgen.cc @@ -434,6 +434,10 @@ static std::string makeGuard( const std::string &fileName ) static void generateModel( Printer &printer, const Proto3 &proto ) { + char version[8] = { 0 }; + snprintf(version, sizeof(version) - 1, "%02X%02X%02X", + (int) PROTOGEN_MAJOR, (int) PROTOGEN_MINOR, (int) PROTOGEN_PATCH); + std::string guard = makeGuard(proto.fileName); printer( "// Generated by the protogen $1$ compiler. DO NOT EDIT!\n" @@ -445,8 +449,14 @@ static void generateModel( Printer &printer, const Proto3 &proto ) "#include \n" "#include \n\n" "#pragma GCC diagnostic ignored \"-Wunused-function\"\n\n" + "#ifndef PROTOGEN_VERSION\n" + " #define PROTOGEN_VERSION 0x$5$ // $1$\n" + "#endif\n" + "#if (PROTOGEN_VERSION != 0x$5$) // $1$\n" + " #error Mixed versions of protogen!\n" + "#endif\n" // base template - "\n$2$\n", PROTOGEN_VERSION, BASE_TEMPLATE, proto.fileName, guard); + "\n$2$\n", PROTOGEN_VERSION, BASE_TEMPLATE, proto.fileName, guard, version); // forward declarations printer("\n// forward declarations\n"); diff --git a/library/proto3.cc b/library/proto3.cc index 949f7d2..a0a6785 100644 --- a/library/proto3.cc +++ b/library/proto3.cc @@ -169,7 +169,7 @@ template class InputStream public: InputStream( const I& first, const I& last ) : cur_(first), end_(last), - last_(-1), ungot_(false), line_(1), column_(0) + last_(-1), line_(1), column_(0), ungot_(false) { }