Skip to content

Commit

Permalink
Merge pull request #10386 from deannagarcia/protocVersion
Browse files Browse the repository at this point in the history
Use protoc version for --version
  • Loading branch information
fowles committed Aug 11, 2022
2 parents ed5c57a + b8712d1 commit d0fb49d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/command_line_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ CommandLineInterface::InterpretArgument(const std::string& name,
if (!version_info_.empty()) {
std::cout << version_info_ << std::endl;
}
std::cout << "libprotoc " << internal::VersionString(PROTOBUF_VERSION)
std::cout << "libprotoc " << internal::ProtocVersionString(PROTOBUF_VERSION)
<< PROTOBUF_VERSION_SUFFIX << std::endl;
return PARSE_ARGUMENT_DONE_AND_EXIT; // Exit without running compiler.

Expand Down
16 changes: 16 additions & 0 deletions src/google/protobuf/stubs/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ std::string VersionString(int version) {
return buffer;
}

std::string ProtocVersionString(int version) {
int minor = (version / 1000) % 1000;
int micro = version % 1000;

// 128 bytes should always be enough, but we use snprintf() anyway to be
// safe.
char buffer[128];
snprintf(buffer, sizeof(buffer), "%d.%d", minor, micro);

// Guard against broken MSVC snprintf().
buffer[sizeof(buffer)-1] = '\0';

return buffer;

}

} // namespace internal

// ===================================================================
Expand Down
3 changes: 3 additions & 0 deletions src/google/protobuf/stubs/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
// Converts a numeric version number to a string.
std::string PROTOBUF_EXPORT VersionString(int version);

// Prints the protoc compiler version (no major version)
std::string PROTOBUF_EXPORT ProtocVersionString(int version);

} // namespace internal

// Place this macro in your main() function (or somewhere before you attempt
Expand Down

0 comments on commit d0fb49d

Please sign in to comment.