Skip to content

Commit 46075d0

Browse files
committed
Include used events in --hashes output
1 parent 3e7c68d commit 46075d0

File tree

12 files changed

+47
-32
lines changed

12 files changed

+47
-32
lines changed

libsolidity/ast/AST.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ FunctionDefinition const* ContractDefinition::receiveFunction() const
192192
return nullptr;
193193
}
194194

195-
vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() const
195+
vector<EventDefinition const*> const& ContractDefinition::definedInterfaceEvents() const
196196
{
197197
return m_interfaceEvents.init([&]{
198198
set<string> eventsSeen;
@@ -213,11 +213,20 @@ vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() cons
213213
interfaceEvents.push_back(e);
214214
}
215215
}
216-
217216
return interfaceEvents;
218217
});
219218
}
220219

220+
vector<EventDefinition const*> const ContractDefinition::usedInterfaceEvents() const
221+
{
222+
solAssert(annotation().creationCallGraph.set(), "");
223+
224+
return convertContainer<std::vector<EventDefinition const*>>(
225+
(*annotation().creationCallGraph)->emittedEvents +
226+
(*annotation().deployedCallGraph)->emittedEvents
227+
);
228+
}
229+
221230
vector<ErrorDefinition const*> ContractDefinition::interfaceErrors(bool _requireCallGraph) const
222231
{
223232
set<ErrorDefinition const*, CompareByID> result;
@@ -227,10 +236,9 @@ vector<ErrorDefinition const*> ContractDefinition::interfaceErrors(bool _require
227236
if (_requireCallGraph)
228237
solAssert(annotation().creationCallGraph.set(), "");
229238
if (annotation().creationCallGraph.set())
230-
{
231-
result += (*annotation().creationCallGraph)->usedErrors;
232-
result += (*annotation().deployedCallGraph)->usedErrors;
233-
}
239+
result +=
240+
(*annotation().creationCallGraph)->usedErrors +
241+
(*annotation().deployedCallGraph)->usedErrors;
234242
return convertContainer<vector<ErrorDefinition const*>>(move(result));
235243
}
236244

libsolidity/ast/AST.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,8 @@ class ContractDefinition: public Declaration, public StructurallyDocumented, pub
519519
return ranges::subrange<decltype(b)>(b, e) | ranges::views::values;
520520
}
521521
std::vector<EventDefinition const*> events() const { return filteredNodes<EventDefinition>(m_subNodes); }
522-
std::vector<EventDefinition const*> const& interfaceEvents() const;
522+
std::vector<EventDefinition const*> const& definedInterfaceEvents() const;
523+
std::vector<EventDefinition const*> const usedInterfaceEvents() const;
523524
/// @returns all errors defined in this contract or any base contract
524525
/// and all errors referenced during execution.
525526
/// @param _requireCallGraph if false, do not fail if the call graph has not been computed yet.

libsolidity/interface/ABI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
101101
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
102102
abi.emplace(std::move(method));
103103
}
104-
for (auto const& it: _contractDef.interfaceEvents())
104+
for (auto const& it: _contractDef.definedInterfaceEvents())
105105
{
106106
Json::Value event{Json::objectValue};
107107
event["type"] = "event";

libsolidity/interface/CompilerStack.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181

8282
#include <boost/algorithm/string/replace.hpp>
8383

84+
#include <range/v3/view/concat.hpp>
85+
8486
#include <utility>
8587
#include <map>
8688
#include <limits>
@@ -1014,31 +1016,34 @@ Json::Value const& CompilerStack::natspecDev(Contract const& _contract) const
10141016
return _contract.devDocumentation.init([&]{ return Natspec::devDocumentation(*_contract.contract); });
10151017
}
10161018

1017-
Json::Value CompilerStack::contractIdentifiers(string const& _contractName) const
1019+
Json::Value CompilerStack::interfaceSymbols(string const& _contractName) const
10181020
{
10191021
if (m_stackState < AnalysisPerformed)
10201022
solThrow(CompilerError, "Analysis was not successful.");
10211023

1022-
Json::Value contractIdentifiers(Json::objectValue);
1024+
Json::Value interfaceSymbols(Json::objectValue);
10231025
// Always have a methods object
1024-
contractIdentifiers["methods"] = Json::objectValue;
1026+
interfaceSymbols["methods"] = Json::objectValue;
10251027

10261028
for (auto const& it: contractDefinition(_contractName).interfaceFunctions())
1027-
contractIdentifiers["methods"][it.second->externalSignature()] = it.first.hex();
1029+
interfaceSymbols["methods"][it.second->externalSignature()] = it.first.hex();
10281030
for (ErrorDefinition const* error: contractDefinition(_contractName).interfaceErrors())
10291031
{
10301032
string signature = error->functionType(true)->externalSignature();
1031-
contractIdentifiers["errors"][signature] = toHex(toCompactBigEndian(selectorFromSignature32(signature), 4));
1033+
interfaceSymbols["errors"][signature] = toHex(toCompactBigEndian(selectorFromSignature32(signature), 4));
10321034
}
10331035

1034-
for (EventDefinition const* event: contractDefinition(_contractName).interfaceEvents())
1036+
for (EventDefinition const* event: ranges::concat_view(
1037+
contractDefinition(_contractName).definedInterfaceEvents(),
1038+
contractDefinition(_contractName).usedInterfaceEvents()
1039+
))
10351040
if (!event->isAnonymous())
10361041
{
10371042
string signature = event->functionType(true)->externalSignature();
1038-
contractIdentifiers["events"][signature] = toHex(u256(h256::Arith(keccak256(signature))));
1043+
interfaceSymbols["events"][signature] = toHex(u256(h256::Arith(keccak256(signature))));
10391044
}
10401045

1041-
return contractIdentifiers;
1046+
return interfaceSymbols;
10421047
}
10431048

10441049
bytes CompilerStack::cborMetadata(string const& _contractName, bool _forIR) const

libsolidity/interface/CompilerStack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class CompilerStack: public langutil::CharStreamProvider
328328
Json::Value const& natspecDev(std::string const& _contractName) const;
329329

330330
/// @returns a JSON object with the three members ``methods``, ``events``, ``errors``. Each is a map, mapping identifiers (hashes) to function names.
331-
Json::Value contractIdentifiers(std::string const& _contractName) const;
331+
Json::Value interfaceSymbols(std::string const& _contractName) const;
332332

333333
/// @returns the Contract Metadata matching the pipeline selected using the viaIR setting.
334334
std::string const& metadata(std::string const& _contractName) const { return metadata(contract(_contractName)); }

libsolidity/interface/Natspec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Json::Value Natspec::userDocumentation(ContractDefinition const& _contractDef)
7878
doc["methods"][it.second->externalSignature()]["notice"] = value;
7979
}
8080

81-
for (auto const& event: _contractDef.interfaceEvents())
81+
for (auto const& event: _contractDef.definedInterfaceEvents())
8282
{
8383
string value = extractDoc(event->annotation().docTags, "notice");
8484
if (!value.empty())

libsolidity/interface/StandardCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
12981298
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.legacyAssembly", wildcardMatchesExperimental))
12991299
evmData["legacyAssembly"] = compilerStack.assemblyJSON(contractName);
13001300
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.methodIdentifiers", wildcardMatchesExperimental))
1301-
evmData["methodIdentifiers"] = compilerStack.contractIdentifiers(contractName)["methods"];
1301+
evmData["methodIdentifiers"] = compilerStack.interfaceSymbols(contractName)["methods"];
13021302
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.gasEstimates", wildcardMatchesExperimental))
13031303
evmData["gasEstimates"] = compilerStack.gasEstimates(contractName);
13041304

solc/CommandLineInterface.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,23 +270,23 @@ void CommandLineInterface::handleSignatureHashes(string const& _contract)
270270
if (!m_options.compiler.outputs.signatureHashes)
271271
return;
272272

273-
Json::Value contractIdentifiers = m_compiler->contractIdentifiers(_contract);
273+
Json::Value interfaceSymbols = m_compiler->interfaceSymbols(_contract);
274274
string out = "Function signatures:\n";
275-
for (auto const& name: contractIdentifiers["methods"].getMemberNames())
276-
out += contractIdentifiers["methods"][name].asString() + ": " + name + "\n";
275+
for (auto const& name: interfaceSymbols["methods"].getMemberNames())
276+
out += interfaceSymbols["methods"][name].asString() + ": " + name + "\n";
277277

278-
if (contractIdentifiers.isMember("errors"))
278+
if (interfaceSymbols.isMember("errors"))
279279
{
280280
out += "\nError signatures:\n";
281-
for (auto const& name: contractIdentifiers["errors"].getMemberNames())
282-
out += contractIdentifiers["errors"][name].asString() + ": " + name + "\n";
281+
for (auto const& name: interfaceSymbols["errors"].getMemberNames())
282+
out += interfaceSymbols["errors"][name].asString() + ": " + name + "\n";
283283
}
284284

285-
if (contractIdentifiers.isMember("events"))
285+
if (interfaceSymbols.isMember("events"))
286286
{
287287
out += "\nEvent signatures:\n";
288-
for (auto const& name: contractIdentifiers["events"].getMemberNames())
289-
out += contractIdentifiers["events"][name].asString() + ": " + name + "\n";
288+
for (auto const& name: interfaceSymbols["events"].getMemberNames())
289+
out += interfaceSymbols["events"][name].asString() + ": " + name + "\n";
290290
}
291291

292292
if (!m_options.output.dir.empty())
@@ -836,7 +836,7 @@ void CommandLineInterface::handleCombinedJSON()
836836
m_compiler->runtimeObject(contractName).functionDebugData
837837
);
838838
if (m_options.compiler.combinedJsonRequests->signatureHashes)
839-
contractData[g_strSignatureHashes] = m_compiler->contractIdentifiers(contractName)["methods"];
839+
contractData[g_strSignatureHashes] = m_compiler->interfaceSymbols(contractName)["methods"];
840840
if (m_options.compiler.combinedJsonRequests->natspecDev)
841841
contractData[g_strNatspecDev] = m_compiler->natspecDev(contractName);
842842
if (m_options.compiler.combinedJsonRequests->natspecUser)

test/cmdlineTests/hashes/output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Error signatures:
1010

1111
Event signatures:
1212
2d4dd5fe18ada5a020a9f5591539a8dc3010a5c074ba6a70e1c956659f02786a: ev(uint256)
13+
81f3fb02f88d32d3bb08c80c9a622ca3b3223292f131c6ad049811f9a8a606dc: libraryEvent(uint256)
1314

1415
======= hashes/input.sol:L =======
1516
Function signatures:

test/libsolidity/SemanticTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ TestCase::TestResult SemanticTest::runTest(
435435
{
436436
soltestAssert(
437437
m_allowNonExistingFunctions ||
438-
m_compiler.contractIdentifiers(m_compiler.lastContractName(m_sources.mainSourceFile))["methods"].isMember(test.call().signature),
438+
m_compiler.interfaceSymbols(m_compiler.lastContractName(m_sources.mainSourceFile))["methods"].isMember(test.call().signature),
439439
"The function " + test.call().signature + " is not known to the compiler"
440440
);
441441

0 commit comments

Comments
 (0)