Skip to content

Commit d142236

Browse files
committed
Add validation to avoid mixed versions of generated protogen code
1 parent 910ca8c commit d142236

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ set(PROTOGEN_MAJOR 0)
66
set(PROTOGEN_MINOR 4)
77
set(PROTOGEN_PATCH 0)
88

9+
add_definitions(-DPROTOGEN_MAJOR=${PROTOGEN_MAJOR})
10+
add_definitions(-DPROTOGEN_MINOR=${PROTOGEN_MINOR})
11+
add_definitions(-DPROTOGEN_PATCH=${PROTOGEN_PATCH})
912
add_definitions(-DPROTOGEN_VERSION="${PROTOGEN_MAJOR}.${PROTOGEN_MINOR}.${PROTOGEN_PATCH}")
1013

1114
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")

compiler/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int main( int argc, char **argv )
4040
protogen::Proto3 proto;
4141
try
4242
{
43-
protogen::Proto3::parse(proto, input, argv[1]);
43+
protogen::Proto3::parse(proto, input, fullPath);
4444
protogen::CppGenerator gen;
4545
gen.generate(proto, output);
4646
} catch (protogen::exception &ex)

library/cppgen.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ static std::string makeGuard( const std::string &fileName )
434434

435435
static void generateModel( Printer &printer, const Proto3 &proto )
436436
{
437+
char version[8] = { 0 };
438+
snprintf(version, sizeof(version) - 1, "%02X%02X%02X",
439+
(int) PROTOGEN_MAJOR, (int) PROTOGEN_MINOR, (int) PROTOGEN_PATCH);
440+
437441
std::string guard = makeGuard(proto.fileName);
438442
printer(
439443
"// Generated by the protogen $1$ compiler. DO NOT EDIT!\n"
@@ -445,8 +449,14 @@ static void generateModel( Printer &printer, const Proto3 &proto )
445449
"#include <iterator>\n"
446450
"#include <sstream>\n\n"
447451
"#pragma GCC diagnostic ignored \"-Wunused-function\"\n\n"
452+
"#ifndef PROTOGEN_VERSION\n"
453+
" #define PROTOGEN_VERSION 0x$5$ // $1$\n"
454+
"#endif\n"
455+
"#if (PROTOGEN_VERSION != 0x$5$) // $1$\n"
456+
" #error Mixed versions of protogen!\n"
457+
"#endif\n"
448458
// base template
449-
"\n$2$\n", PROTOGEN_VERSION, BASE_TEMPLATE, proto.fileName, guard);
459+
"\n$2$\n", PROTOGEN_VERSION, BASE_TEMPLATE, proto.fileName, guard, version);
450460

451461
// forward declarations
452462
printer("\n// forward declarations\n");

library/proto3.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ template <typename I> class InputStream
169169

170170
public:
171171
InputStream( const I& first, const I& last ) : cur_(first), end_(last),
172-
last_(-1), ungot_(false), line_(1), column_(0)
172+
last_(-1), line_(1), column_(0), ungot_(false)
173173
{
174174
}
175175

0 commit comments

Comments
 (0)