Permalink
Browse files

SERVER-31390 Use a templating language to generate error_codes.{h,cpp…

…,js}
  • Loading branch information...
RedBeard0531 committed Oct 4, 2017
1 parent acdd51d commit 9f8696b5ba4c9310749c5c3f1ee082c5f663b5b0
@@ -1 +1,2 @@
src/mongo/gotools/*
*.tpl.js
@@ -10,3 +10,5 @@ pylint == 1.6.5
pydocstyle == 1.1.1
# resmoke.py
-r resmokelib/requirements.txt
# generate_error_codes.py
cheetah3 == 3.0.0; python_version < "3"
@@ -4,10 +4,29 @@ Import("env")

env = env.Clone()

generateErrorCodes = env.Command(
target=['error_codes.h', 'error_codes.cpp'],
source=['generate_error_codes.py', 'error_codes.err'],
action=['$PYTHON ${SOURCES[0]} cpp ${SOURCES[1]} --cpp-header=${TARGETS[0]} --cpp-source=${TARGETS[1]}'])

# This needs to use its own env to tell scons to suppress scanning the .tpl.h and .tpl.cpp inputs
# for #includes since they aren't directly preprocessed. Scons will still scan the generated files
# to produce the correct implicit dependencies when they are compiled.
env_for_error_codes = env.Clone()
env_for_error_codes['SCANNERS'] = []
generateErrorCodes = env_for_error_codes.Command(
target=[
'error_codes.h',
'error_codes.cpp'
],
source=[
'generate_error_codes.py',
'error_codes.err',
'error_codes.tpl.h',
'error_codes.tpl.cpp',
],
action=['$PYTHON ${SOURCES[0]} ${SOURCES[1]} '
'${SOURCES[2]}=${TARGETS[0]} '
'${SOURCES[3]}=${TARGETS[1]}'
],
)

env.Alias('generated-sources', generateErrorCodes)

env.CppUnitTest('base_test',
@@ -0,0 +1,77 @@
/**
* Copyright 2017 MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects
* for all of the code used other than as permitted herein. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version. If you
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/

#include "mongo/platform/basic.h"

#include "mongo/base/error_codes.h"

#include "mongo/base/static_assert.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {

MONGO_STATIC_ASSERT(sizeof(ErrorCodes::Error) == sizeof(int));

std::string ErrorCodes::errorString(Error err) {
switch (err) {
//#for $ec in $codes
case $ec.name:
return "$ec.name";
//#end for
default:
return mongoutils::str::stream() << "Location" << int(err);
}
}

ErrorCodes::Error ErrorCodes::fromString(StringData name) {
//#for $ec in $codes
if (name == "$ec.name"_sd)
return $ec.name;
//#end for
return UnknownError;
}

std::ostream& operator<<(std::ostream& stream, ErrorCodes::Error code) {
return stream << ErrorCodes::errorString(code);
}

//#for $cat in $categories
bool ErrorCodes::is${cat.name}(Error err) {
switch (err) {
//#for $code in $cat.codes
case $code:
return true;
//#end for
default:
return false;
}
}
//#end for

} // namespace mongo
@@ -0,0 +1,82 @@
/**
* Copyright 2017 MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects
* for all of the code used other than as permitted herein. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version. If you
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/

#pragma once

#include <cstdint>
#include <iosfwd>
#include <string>

#include "mongo/base/string_data.h"

namespace mongo {

/**
* This is a generated class containing a table of error codes and their corresponding error
* strings. The class is derived from the definitions in src/mongo/base/error_codes.err file and the
* src/mongo/base/error_codes.tpl.h template.
*
* Do not update this file directly. Update src/mongo/base/error_codes.err instead.
*/
class ErrorCodes {
public:
// Explicitly 32-bits wide so that non-symbolic values,
// like uassert codes, are valid.
enum Error : std::int32_t {
//#for $ec in $codes
$ec.name = $ec.code,
//#end for
MaxError
};

static std::string errorString(Error err);

/**
* Parses an Error from its "name". Returns UnknownError if "name" is unrecognized.
*
* NOTE: Also returns UnknownError for the string "UnknownError".
*/
static Error fromString(StringData name);

/**
* Casts an integer "code" to an Error. Unrecognized codes are preserved, meaning
* that the result of a call to fromInt() may not be one of the values in the
* Error enumeration.
*/
static Error fromInt(int code) {
return static_cast<Error>(code);
}

//#for $cat in $categories
static bool is${cat.name}(Error err);
//#end for
};

std::ostream& operator<<(std::ostream& stream, ErrorCodes::Error code);

} // namespace mongo
Oops, something went wrong.

0 comments on commit 9f8696b

Please sign in to comment.