Skip to content

Commit 2ba536f

Browse files
authored
Merge pull request #14582 from ethereum/fix-check-style-using-namespace-std-exclusion
Fix check_style for using namespace std exclusing in test/*
2 parents fe1f9c6 + 0d0a7fc commit 2ba536f

File tree

6 files changed

+108
-100
lines changed

6 files changed

+108
-100
lines changed

scripts/check_style.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,27 @@ FORMATERROR=$(
8585
# unqualified move()/forward() checks, i.e. make sure that std::move() and std::forward() are used instead of move() and forward()
8686
preparedGrep "move\(.+\)" | grep -v "std::move" | grep -E "[^a-z]move"
8787
preparedGrep "forward\(.+\)" | grep -v "std::forward" | grep -E "[^a-z]forward"
88+
) | grep -E -v -e "^[a-zA-Z\./]*:[0-9]*:\s*\/(\/|\*)" -e "^test/" || true
89+
)
90+
91+
# Special error handling for `using namespace std;` exclusion, since said statement can be present in the test directory
92+
# and its subdirectories, but is excluded in the above ruleset. In order to have consistent codestyle with regards to
93+
# std namespace usage, test directory must also be covered.
94+
FORMATSTDERROR=$(
95+
(
8896
# make sure `using namespace std` is not used in INCLUDE_DIRECTORIES
8997
# shellcheck disable=SC2068,SC2068
9098
grep -nIE -d skip "using namespace std;" ${NAMESPACE_STD_FREE_FILES[@]}
91-
) | grep -E -v -e "^[a-zA-Z\./]*:[0-9]*:\s*\/(\/|\*)" -e "^test/" || true
99+
) || true
92100
)
93101

94-
if [[ "$FORMATERROR" != "" ]]
102+
# Merge errors into single string
103+
FORMATEDERRORS="$FORMATERROR$FORMATSTDERROR"
104+
105+
if [[ "$FORMATEDERRORS" != "" ]]
95106
then
96107
echo "Coding style error:" | tee -a "$ERROR_LOG"
97-
echo "$FORMATERROR" | tee -a "$ERROR_LOG"
108+
echo "$FORMATEDERRORS" | tee -a "$ERROR_LOG"
98109
scripts/ci/post_style_errors_on_github.sh "$ERROR_LOG"
99110
exit 1
100111
fi

test/libsolidity/NatspecJSONTest.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929

3030
#include <vector>
3131

32-
using namespace std;
3332
using namespace solidity::frontend::test;
3433
using namespace solidity::util;
34+
using namespace std::string_literals;
3535

36-
ostream& solidity::frontend::test::operator<<(ostream& _output, NatspecJSONKind _kind)
36+
std::ostream& solidity::frontend::test::operator<<(std::ostream& _output, NatspecJSONKind _kind)
3737
{
3838
switch (_kind) {
3939
case NatspecJSONKind::Devdoc: _output << "devdoc"; break;
@@ -42,12 +42,12 @@ ostream& solidity::frontend::test::operator<<(ostream& _output, NatspecJSONKind
4242
return _output;
4343
}
4444

45-
unique_ptr<TestCase> NatspecJSONTest::create(Config const& _config)
45+
std::unique_ptr<TestCase> NatspecJSONTest::create(Config const& _config)
4646
{
47-
return make_unique<NatspecJSONTest>(_config.filename, _config.evmVersion);
47+
return std::make_unique<NatspecJSONTest>(_config.filename, _config.evmVersion);
4848
}
4949

50-
void NatspecJSONTest::parseCustomExpectations(istream& _stream)
50+
void NatspecJSONTest::parseCustomExpectations(std::istream& _stream)
5151
{
5252
soltestAssert(m_expectedNatspecJSON.empty());
5353

@@ -56,21 +56,21 @@ void NatspecJSONTest::parseCustomExpectations(istream& _stream)
5656
// // <qualified contract name> <devdoc|userdoc>
5757
// // <json>
5858

59-
string line;
59+
std::string line;
6060
while (getline(_stream, line))
6161
{
62-
string_view strippedLine = expectLinePrefix(line);
62+
std::string_view strippedLine = expectLinePrefix(line);
6363
if (strippedLine.empty())
6464
continue;
6565

6666
auto [contractName, kind] = parseExpectationHeader(strippedLine);
6767

68-
string rawJSON = extractExpectationJSON(_stream);
69-
string jsonErrors;
68+
std::string rawJSON = extractExpectationJSON(_stream);
69+
std::string jsonErrors;
7070
Json::Value parsedJSON;
7171
bool jsonParsingSuccessful = jsonParseStrict(rawJSON, parsedJSON, &jsonErrors);
7272
if (!jsonParsingSuccessful)
73-
BOOST_THROW_EXCEPTION(runtime_error(fmt::format(
73+
BOOST_THROW_EXCEPTION(std::runtime_error(fmt::format(
7474
"Malformed JSON in {} expectation for contract {}.\n"
7575
"Note that JSON expectations must be pretty-printed to be split correctly. "
7676
"The object is assumed to and at the first unindented closing brace.\n"
@@ -80,7 +80,7 @@ void NatspecJSONTest::parseCustomExpectations(istream& _stream)
8080
rawJSON
8181
)));
8282

83-
m_expectedNatspecJSON[string(contractName)][kind] = parsedJSON;
83+
m_expectedNatspecJSON[std::string(contractName)][kind] = parsedJSON;
8484
}
8585
}
8686

@@ -94,51 +94,51 @@ bool NatspecJSONTest::expectationsMatch()
9494
prettyPrinted(obtainedNatspec()) == prettyPrinted(m_expectedNatspecJSON);
9595
}
9696

97-
void NatspecJSONTest::printExpectedResult(ostream& _stream, string const& _linePrefix, bool _formatted) const
97+
void NatspecJSONTest::printExpectedResult(std::ostream& _stream, std::string const& _linePrefix, bool _formatted) const
9898
{
9999
SyntaxTest::printExpectedResult(_stream, _linePrefix, _formatted);
100100
if (!m_expectedNatspecJSON.empty())
101101
{
102-
_stream << _linePrefix << "----" << endl;
102+
_stream << _linePrefix << "----" << std::endl;
103103
printIndented(_stream, formatNatspecExpectations(m_expectedNatspecJSON), _linePrefix);
104104
}
105105
}
106106

107-
void NatspecJSONTest::printObtainedResult(ostream& _stream, string const& _linePrefix, bool _formatted) const
107+
void NatspecJSONTest::printObtainedResult(std::ostream& _stream, std::string const& _linePrefix, bool _formatted) const
108108
{
109109
SyntaxTest::printObtainedResult(_stream, _linePrefix, _formatted);
110110

111111
NatspecMap natspecJSON = obtainedNatspec();
112112
if (!natspecJSON.empty())
113113
{
114-
_stream << _linePrefix << "----" << endl;
114+
_stream << _linePrefix << "----" << std::endl;
115115
// TODO: Diff both versions and highlight differences.
116116
// We should have a helper for doing that in newly defined test cases without much effort.
117117
printIndented(_stream, formatNatspecExpectations(natspecJSON), _linePrefix);
118118
}
119119
}
120120

121-
tuple<string_view, NatspecJSONKind> NatspecJSONTest::parseExpectationHeader(string_view _line)
121+
std::tuple<std::string_view, NatspecJSONKind> NatspecJSONTest::parseExpectationHeader(std::string_view _line)
122122
{
123123
for (NatspecJSONKind kind: {NatspecJSONKind::Devdoc, NatspecJSONKind::Userdoc})
124124
{
125-
string kindSuffix = " " + toString(kind);
125+
std::string kindSuffix = " " + toString(kind);
126126
if (boost::algorithm::ends_with(_line, kindSuffix))
127127
return {_line.substr(0, _line.size() - kindSuffix.size()), kind};
128128
}
129129

130-
BOOST_THROW_EXCEPTION(runtime_error(
130+
BOOST_THROW_EXCEPTION(std::runtime_error(
131131
"Natspec kind (devdoc/userdoc) not present in the expectation: "s.append(_line)
132132
));
133133
}
134134

135-
string NatspecJSONTest::extractExpectationJSON(istream& _stream)
135+
std::string NatspecJSONTest::extractExpectationJSON(std::istream& _stream)
136136
{
137-
string rawJSON;
138-
string line;
137+
std::string rawJSON;
138+
std::string line;
139139
while (getline(_stream, line))
140140
{
141-
string_view strippedLine = expectLinePrefix(line);
141+
std::string_view strippedLine = expectLinePrefix(line);
142142
rawJSON += strippedLine;
143143
rawJSON += "\n";
144144

@@ -149,11 +149,11 @@ string NatspecJSONTest::extractExpectationJSON(istream& _stream)
149149
return rawJSON;
150150
}
151151

152-
string_view NatspecJSONTest::expectLinePrefix(string_view _line)
152+
std::string_view NatspecJSONTest::expectLinePrefix(std::string_view _line)
153153
{
154154
size_t startPosition = 0;
155155
if (!boost::algorithm::starts_with(_line, "//"))
156-
BOOST_THROW_EXCEPTION(runtime_error(
156+
BOOST_THROW_EXCEPTION(std::runtime_error(
157157
"Expectation line is not a comment: "s.append(_line)
158158
));
159159

@@ -164,9 +164,9 @@ string_view NatspecJSONTest::expectLinePrefix(string_view _line)
164164
return _line.substr(startPosition, _line.size() - startPosition);
165165
}
166166

167-
string NatspecJSONTest::formatNatspecExpectations(NatspecMap const& _expectations) const
167+
std::string NatspecJSONTest::formatNatspecExpectations(NatspecMap const& _expectations) const
168168
{
169-
string output;
169+
std::string output;
170170
bool first = true;
171171
// NOTE: Not sorting explicitly because CompilerStack seems to put contracts roughly in the
172172
// order in which they appear in the source, which is much better than alphabetical order.
@@ -190,7 +190,7 @@ NatspecMap NatspecJSONTest::obtainedNatspec() const
190190
return {};
191191

192192
NatspecMap result;
193-
for (string contractName: compiler().contractNames())
193+
for (std::string contractName: compiler().contractNames())
194194
{
195195
result[contractName][NatspecJSONKind::Devdoc] = compiler().natspecDev(contractName);
196196
result[contractName][NatspecJSONKind::Userdoc] = compiler().natspecUser(contractName);

test/libsolidity/SolidityParser.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
#include <boost/test/unit_test.hpp>
3333

34-
using namespace std;
3534
using namespace solidity::langutil;
3635

3736
namespace solidity::frontend::test
@@ -50,7 +49,7 @@ ASTPointer<ContractDefinition> parseText(std::string const& _source, ErrorList&
5049
if (!sourceUnit)
5150
return ASTPointer<ContractDefinition>();
5251
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
53-
if (ASTPointer<ContractDefinition> contract = dynamic_pointer_cast<ContractDefinition>(node))
52+
if (ASTPointer<ContractDefinition> contract = std::dynamic_pointer_cast<ContractDefinition>(node))
5453
return contract;
5554
BOOST_FAIL("No contract found in source.");
5655
return ASTPointer<ContractDefinition>();
@@ -525,7 +524,7 @@ BOOST_AUTO_TEST_CASE(keyword_is_reserved)
525524
for (auto const& keyword: keywords)
526525
{
527526
auto text = std::string("contract ") + keyword + " {}";
528-
CHECK_PARSE_ERROR(text.c_str(), string("Expected identifier but got reserved keyword '") + keyword + "'");
527+
CHECK_PARSE_ERROR(text.c_str(), std::string("Expected identifier but got reserved keyword '") + keyword + "'");
529528
}
530529
}
531530

@@ -591,7 +590,7 @@ BOOST_AUTO_TEST_CASE(inline_asm_end_location)
591590
class CheckInlineAsmLocation: public ASTConstVisitor
592591
{
593592
public:
594-
explicit CheckInlineAsmLocation(string _sourceCode): m_sourceCode(_sourceCode) {}
593+
explicit CheckInlineAsmLocation(std::string _sourceCode): m_sourceCode(_sourceCode) {}
595594
bool visited = false;
596595
bool visit(InlineAssembly const& _inlineAsm) override
597596
{
@@ -602,7 +601,7 @@ BOOST_AUTO_TEST_CASE(inline_asm_end_location)
602601

603602
return false;
604603
}
605-
string m_sourceCode;
604+
std::string m_sourceCode;
606605
};
607606

608607
CheckInlineAsmLocation visitor{sourceCode};

test/libsolidity/SolidityTypes.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <libsolutil/Keccak256.h>
2828
#include <boost/test/unit_test.hpp>
2929

30-
using namespace std;
3130
using namespace solidity::langutil;
3231

3332
namespace solidity::frontend::test
@@ -86,9 +85,9 @@ BOOST_AUTO_TEST_CASE(storage_layout_simple)
8685
BOOST_REQUIRE(members.memberStorageOffset("first") != nullptr);
8786
BOOST_REQUIRE(members.memberStorageOffset("second") != nullptr);
8887
BOOST_REQUIRE(members.memberStorageOffset("wraps") != nullptr);
89-
BOOST_CHECK(*members.memberStorageOffset("first") == make_pair(u256(0), unsigned(0)));
90-
BOOST_CHECK(*members.memberStorageOffset("second") == make_pair(u256(0), unsigned(16)));
91-
BOOST_CHECK(*members.memberStorageOffset("wraps") == make_pair(u256(1), unsigned(0)));
88+
BOOST_CHECK(*members.memberStorageOffset("first") == std::make_pair(u256(0), unsigned(0)));
89+
BOOST_CHECK(*members.memberStorageOffset("second") == std::make_pair(u256(0), unsigned(16)));
90+
BOOST_CHECK(*members.memberStorageOffset("wraps") == std::make_pair(u256(1), unsigned(0)));
9291
}
9392

9493
BOOST_AUTO_TEST_CASE(storage_layout_mapping)
@@ -114,10 +113,10 @@ BOOST_AUTO_TEST_CASE(storage_layout_mapping)
114113
BOOST_REQUIRE(members.memberStorageOffset("second") != nullptr);
115114
BOOST_REQUIRE(members.memberStorageOffset("third") != nullptr);
116115
BOOST_REQUIRE(members.memberStorageOffset("final") != nullptr);
117-
BOOST_CHECK(*members.memberStorageOffset("first") == make_pair(u256(0), unsigned(0)));
118-
BOOST_CHECK(*members.memberStorageOffset("second") == make_pair(u256(1), unsigned(0)));
119-
BOOST_CHECK(*members.memberStorageOffset("third") == make_pair(u256(2), unsigned(0)));
120-
BOOST_CHECK(*members.memberStorageOffset("final") == make_pair(u256(3), unsigned(0)));
116+
BOOST_CHECK(*members.memberStorageOffset("first") == std::make_pair(u256(0), unsigned(0)));
117+
BOOST_CHECK(*members.memberStorageOffset("second") == std::make_pair(u256(1), unsigned(0)));
118+
BOOST_CHECK(*members.memberStorageOffset("third") == std::make_pair(u256(2), unsigned(0)));
119+
BOOST_CHECK(*members.memberStorageOffset("final") == std::make_pair(u256(3), unsigned(0)));
121120
}
122121

123122
BOOST_AUTO_TEST_CASE(storage_layout_arrays)
@@ -162,7 +161,7 @@ BOOST_AUTO_TEST_CASE(type_identifiers)
162161
BOOST_CHECK_EQUAL(RationalNumberType(rational(2 * 200, 2 * 77)).identifier(), "t_rational_200_by_77");
163162
BOOST_CHECK_EQUAL(RationalNumberType(rational(-2 * 200, 2 * 77)).identifier(), "t_rational_minus_200_by_77");
164163
BOOST_CHECK_EQUAL(
165-
StringLiteralType(Literal(++id, SourceLocation{}, Token::StringLiteral, make_shared<string>("abc - def"))).identifier(),
164+
StringLiteralType(Literal(++id, SourceLocation{}, Token::StringLiteral, std::make_shared<std::string>("abc - def"))).identifier(),
166165
"t_stringliteral_196a9142ee0d40e274a6482393c762b16dd8315713207365e1e13d8d85b74fc4"
167166
);
168167
BOOST_CHECK_EQUAL(TypeProvider::fromElementaryTypeName("bytes1")->identifier(), "t_bytes1");
@@ -183,15 +182,15 @@ BOOST_AUTO_TEST_CASE(type_identifiers)
183182
Type const* multiArray = TypeProvider::array(DataLocation::Storage, stringArray);
184183
BOOST_CHECK_EQUAL(multiArray->identifier(), "t_array$_t_array$_t_string_storage_$20_storage_$dyn_storage_ptr");
185184

186-
ContractDefinition c(++id, SourceLocation{}, make_shared<string>("MyContract$"), SourceLocation{}, {}, {}, {}, ContractKind::Contract);
185+
ContractDefinition c(++id, SourceLocation{}, std::make_shared<std::string>("MyContract$"), SourceLocation{}, {}, {}, {}, ContractKind::Contract);
187186
BOOST_CHECK_EQUAL(c.type()->identifier(), "t_type$_t_contract$_MyContract$$$_$2_$");
188187
BOOST_CHECK_EQUAL(ContractType(c, true).identifier(), "t_super$_MyContract$$$_$2");
189188

190-
StructDefinition s(++id, {}, make_shared<string>("Struct"), {}, {}, {});
189+
StructDefinition s(++id, {}, std::make_shared<std::string>("Struct"), {}, {}, {});
191190
s.annotation().recursive = false;
192191
BOOST_CHECK_EQUAL(s.type()->identifier(), "t_type$_t_struct$_Struct_$3_storage_ptr_$");
193192

194-
EnumDefinition e(++id, {}, make_shared<string>("Enum"), {}, {}, {});
193+
EnumDefinition e(++id, {}, std::make_shared<std::string>("Enum"), {}, {}, {});
195194
BOOST_CHECK_EQUAL(e.type()->identifier(), "t_type$_t_enum$_Enum_$4_$");
196195

197196
TupleType t({e.type(), s.type(), stringArray, nullptr});
@@ -209,8 +208,8 @@ BOOST_AUTO_TEST_CASE(type_identifiers)
209208

210209
// TypeType is tested with contract
211210

212-
auto emptyParams = make_shared<ParameterList>(++id, SourceLocation(), std::vector<ASTPointer<VariableDeclaration>>());
213-
ModifierDefinition mod(++id, SourceLocation{}, make_shared<string>("modif"), SourceLocation{}, {}, emptyParams, {}, {}, {});
211+
auto emptyParams = std::make_shared<ParameterList>(++id, SourceLocation(), std::vector<ASTPointer<VariableDeclaration>>());
212+
ModifierDefinition mod(++id, SourceLocation{}, std::make_shared<std::string>("modif"), SourceLocation{}, {}, emptyParams, {}, {}, {});
214213
BOOST_CHECK_EQUAL(ModifierType(mod).identifier(), "t_modifier$__$");
215214

216215
SourceUnit su(++id, {}, {}, {}, {});
@@ -250,34 +249,34 @@ BOOST_AUTO_TEST_CASE(helper_bool_result)
250249
{
251250
BoolResult r1{true};
252251
BoolResult r2 = BoolResult::err("Failure.");
253-
r1.merge(r2, logical_and<bool>());
252+
r1.merge(r2, std::logical_and<bool>());
254253
BOOST_REQUIRE_EQUAL(r1.get(), false);
255254
BOOST_REQUIRE_EQUAL(r1.message(), "Failure.");
256255

257256
BoolResult r3{false};
258257
BoolResult r4{true};
259-
r3.merge(r4, logical_and<bool>());
258+
r3.merge(r4, std::logical_and<bool>());
260259
BOOST_REQUIRE_EQUAL(r3.get(), false);
261260
BOOST_REQUIRE_EQUAL(r3.message(), "");
262261

263262
BoolResult r5{true};
264263
BoolResult r6{true};
265-
r5.merge(r6, logical_and<bool>());
264+
r5.merge(r6, std::logical_and<bool>());
266265
BOOST_REQUIRE_EQUAL(r5.get(), true);
267266
BOOST_REQUIRE_EQUAL(r5.message(), "");
268267
}
269268

270269
BOOST_AUTO_TEST_CASE(helper_string_result)
271270
{
272-
using StringResult = util::Result<string>;
271+
using StringResult = util::Result<std::string>;
273272

274-
StringResult r1{string{"Success"}};
273+
StringResult r1{std::string{"Success"}};
275274
StringResult r2 = StringResult::err("Failure");
276275

277276
BOOST_REQUIRE_EQUAL(r1.get(), "Success");
278277
BOOST_REQUIRE_EQUAL(r2.get(), "");
279278

280-
r1.merge(r2, [](string const&, string const& _rhs) { return _rhs; });
279+
r1.merge(r2, [](std::string const&, std::string const& _rhs) { return _rhs; });
281280

282281
BOOST_REQUIRE_EQUAL(r1.get(), "");
283282
BOOST_REQUIRE_EQUAL(r1.message(), "Failure");

0 commit comments

Comments
 (0)