Skip to content

Commit

Permalink
Add validation to avoid mixed versions of generated protogen code
Browse files Browse the repository at this point in the history
  • Loading branch information
brunexgeek committed Dec 28, 2018
1 parent 910ca8c commit d142236
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion compiler/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion library/cppgen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -445,8 +449,14 @@ static void generateModel( Printer &printer, const Proto3 &proto )
"#include <iterator>\n"
"#include <sstream>\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");
Expand Down
2 changes: 1 addition & 1 deletion library/proto3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ template <typename I> 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)
{
}

Expand Down

0 comments on commit d142236

Please sign in to comment.