mirror of https://github.com/qt/qtgrpc.git
tools [generators]: Prefer error string over throwing
This is not a library. The tool should not recover here. Print the error and exit the program. Coverity-Id: 479436 Pick-to: 6.9 6.8 Change-Id: I97c24b325a037e19de17af9b246ba2bb2be76d78 Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
parent
f66f3daffb
commit
99d09ea8be
|
@ -7,6 +7,9 @@
|
|||
#include "options.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace QtGrpc;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
@ -14,8 +17,13 @@ int main(int argc, char *argv[])
|
|||
char *optionsPtr = getenv("QT_GRPC_OPTIONS");
|
||||
if (optionsPtr != nullptr) {
|
||||
QT_PROTOBUF_DEBUG("QT_GRPC_OPTIONS: " << optionsPtr);
|
||||
qtprotoccommon::Options::setFromString(optionsPtr,
|
||||
qtprotoccommon::Options::QtGrpcGen);
|
||||
std::string error;
|
||||
qtprotoccommon::Options::setFromString(optionsPtr, qtprotoccommon::Options::QtGrpcGen,
|
||||
&error);
|
||||
if (!error.empty()) {
|
||||
std::cerr << error << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
QGrpcGenerator generator;
|
||||
return ::google::protobuf::compiler::PluginMain(argc, argv, &generator);
|
||||
|
|
|
@ -223,6 +223,8 @@ bool QGrpcGenerator::GenerateAll(const std::vector<const FileDescriptor *> &file
|
|||
const std::string ¶meter, GeneratorContext *generatorContext,
|
||||
std::string *error) const
|
||||
{
|
||||
Options::setFromString(parameter, qtprotoccommon::Options::QtGrpcGen);
|
||||
Options::setFromString(parameter, qtprotoccommon::Options::QtGrpcGen, error);
|
||||
if (!error->empty())
|
||||
return false;
|
||||
return GeneratorBase::GenerateAll(files, parameter, generatorContext, error);
|
||||
}
|
||||
|
|
|
@ -8,13 +8,22 @@
|
|||
#include "options.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace ::QtProtobuf;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *optionsPtr = getenv("QT_PROTOBUF_OPTIONS");
|
||||
if (optionsPtr != nullptr) {
|
||||
QT_PROTOBUF_DEBUG("QT_PROTOBUF_OPTIONS: " << optionsPtr);
|
||||
qtprotoccommon::Options::setFromString(optionsPtr);
|
||||
std::string error;
|
||||
qtprotoccommon::Options::setFromString(optionsPtr, qtprotoccommon::Options::QtProtobufGen,
|
||||
&error);
|
||||
if (!error.empty()) {
|
||||
std::cerr << error << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
QProtobufGenerator generator;
|
||||
return ::google::protobuf::compiler::PluginMain(argc, argv, &generator);
|
||||
|
|
|
@ -28,7 +28,9 @@ bool GeneratorBase::GenerateAll(const std::vector<const FileDescriptor *> &files
|
|||
assert(!files.empty());
|
||||
assert(generatorContext != nullptr);
|
||||
|
||||
Options::setFromString(parameter);
|
||||
Options::setFromString(parameter, Options::QtProtobufGen, error);
|
||||
if (!error->empty())
|
||||
return false;
|
||||
if (Options::instance().generateMacroExportFile()) {
|
||||
std::string exportMacroName = Options::instance().exportMacro();
|
||||
std::string exportMacroFilename = Options::instance().exportMacroFilename();
|
||||
|
|
|
@ -3,10 +3,9 @@
|
|||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "options.h"
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include <regex>
|
||||
#include <stdexcept>
|
||||
#include <string_view>
|
||||
|
||||
using namespace ::qtprotoccommon;
|
||||
|
@ -53,7 +52,8 @@ std::string extractCompositeOptionValue(const std::string &option)
|
|||
return optionValue;
|
||||
}
|
||||
|
||||
void Options::setFromString(const std::string &options, GeneratorType)
|
||||
void Options::setFromString(const std::string &options, GeneratorType /*unused*/,
|
||||
std::string *error)
|
||||
{
|
||||
Options &instance = mutableInstance();
|
||||
for (const auto &option : utils::split(options, ";")) {
|
||||
|
@ -77,8 +77,9 @@ void Options::setFromString(const std::string &options, GeneratorType)
|
|||
if (!export_macro_values.empty()) {
|
||||
static const std::regex valid_c_identifier("[a-zA-Z_][0-9a-zA-Z_]*");
|
||||
if (!std::regex_match(export_macro_values[0], valid_c_identifier)) {
|
||||
throw std::invalid_argument("EXPORT_MACRO '" + export_macro_values[0]
|
||||
+ "' is not a valid C identifier.");
|
||||
*error = "EXPORT_MACRO '" + export_macro_values[0]
|
||||
+ "' is not a valid C identifier.";
|
||||
return;
|
||||
}
|
||||
instance.m_exportMacro = export_macro_values[0];
|
||||
QT_PROTOBUF_DEBUG("set m_exportMacro: " << instance.m_exportMacro);
|
||||
|
|
|
@ -35,7 +35,7 @@ public:
|
|||
Options &operator=(Options &&) = delete;
|
||||
|
||||
static const Options &instance();
|
||||
static void setFromString(const std::string &options, GeneratorType type = QtProtobufGen);
|
||||
static void setFromString(const std::string &options, GeneratorType type, std::string *error);
|
||||
|
||||
bool hasQml() const { return m_qml; }
|
||||
bool generateComments() const { return m_generateComments; }
|
||||
|
|
Loading…
Reference in New Issue