Merge pull request #2748 from ethereum/natspec

Remove DocumentationType from natspec
This commit is contained in:
chriseth 2017-08-21 16:46:00 +02:00 committed by GitHub
commit d270879c8f
6 changed files with 59 additions and 58 deletions

View File

@ -406,39 +406,42 @@ Json::Value const& CompilerStack::contractABI(Contract const& _contract) const
return *_contract.abi; return *_contract.abi;
} }
Json::Value const& CompilerStack::natspec(string const& _contractName, DocumentationType _type) const Json::Value const& CompilerStack::natspecUser(string const& _contractName) const
{ {
return natspec(contract(_contractName), _type); return natspecUser(contract(_contractName));
} }
Json::Value const& CompilerStack::natspec(Contract const& _contract, DocumentationType _type) const Json::Value const& CompilerStack::natspecUser(Contract const& _contract) const
{ {
if (m_stackState < AnalysisSuccessful) if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
solAssert(_contract.contract, ""); solAssert(_contract.contract, "");
std::unique_ptr<Json::Value const>* doc;
// checks wheather we already have the documentation // caches the result
switch (_type) if (!_contract.userDocumentation)
{ _contract.userDocumentation.reset(new Json::Value(Natspec::userDocumentation(*_contract.contract)));
case DocumentationType::NatspecUser:
doc = &_contract.userDocumentation;
// caches the result
if (!*doc)
doc->reset(new Json::Value(Natspec::userDocumentation(*_contract.contract)));
break;
case DocumentationType::NatspecDev:
doc = &_contract.devDocumentation;
// caches the result
if (!*doc)
doc->reset(new Json::Value(Natspec::devDocumentation(*_contract.contract)));
break;
default:
solAssert(false, "Illegal documentation type.");
}
return *(*doc); return *_contract.userDocumentation;
}
Json::Value const& CompilerStack::natspecDev(string const& _contractName) const
{
return natspecDev(contract(_contractName));
}
Json::Value const& CompilerStack::natspecDev(Contract const& _contract) const
{
if (m_stackState < AnalysisSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
solAssert(_contract.contract, "");
// caches the result
if (!_contract.devDocumentation)
_contract.devDocumentation.reset(new Json::Value(Natspec::devDocumentation(*_contract.contract)));
return *_contract.devDocumentation;
} }
Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const
@ -819,8 +822,8 @@ string CompilerStack::createMetadata(Contract const& _contract) const
meta["settings"]["libraries"][library.first] = "0x" + toHex(library.second.asBytes()); meta["settings"]["libraries"][library.first] = "0x" + toHex(library.second.asBytes());
meta["output"]["abi"] = contractABI(_contract); meta["output"]["abi"] = contractABI(_contract);
meta["output"]["userdoc"] = natspec(_contract, DocumentationType::NatspecUser); meta["output"]["userdoc"] = natspecUser(_contract);
meta["output"]["devdoc"] = natspec(_contract, DocumentationType::NatspecDev); meta["output"]["devdoc"] = natspecDev(_contract);
return jsonCompactPrint(meta); return jsonCompactPrint(meta);
} }

View File

@ -63,12 +63,6 @@ class Natspec;
class Error; class Error;
class DeclarationContainer; class DeclarationContainer;
enum class DocumentationType: uint8_t
{
NatspecUser = 1,
NatspecDev
};
/** /**
* Easy to use and self-contained Solidity compiler with as few header dependencies as possible. * Easy to use and self-contained Solidity compiler with as few header dependencies as possible.
* It holds state and can be used to either step through the compilation stages (and abort e.g. * It holds state and can be used to either step through the compilation stages (and abort e.g.
@ -203,11 +197,13 @@ public:
/// Prerequisite: Successful call to parse or compile. /// Prerequisite: Successful call to parse or compile.
Json::Value const& contractABI(std::string const& _contractName = "") const; Json::Value const& contractABI(std::string const& _contractName = "") const;
/// @returns a JSON representing the contract's documentation. /// @returns a JSON representing the contract's user documentation.
/// Prerequisite: Successful call to parse or compile. /// Prerequisite: Successful call to parse or compile.
/// @param type The type of the documentation to get. Json::Value const& natspecUser(std::string const& _contractName) const;
/// Can be one of 4 types defined at @c DocumentationType
Json::Value const& natspec(std::string const& _contractName, DocumentationType _type) const; /// @returns a JSON representing the contract's developer documentation.
/// Prerequisite: Successful call to parse or compile.
Json::Value const& natspecDev(std::string const& _contractName) const;
/// @returns a JSON representing a map of method identifiers (hashes) to function names. /// @returns a JSON representing a map of method identifiers (hashes) to function names.
Json::Value methodIdentifiers(std::string const& _contractName) const; Json::Value methodIdentifiers(std::string const& _contractName) const;
@ -274,7 +270,8 @@ private:
std::string createMetadata(Contract const& _contract) const; std::string createMetadata(Contract const& _contract) const;
std::string computeSourceMapping(eth::AssemblyItems const& _items) const; std::string computeSourceMapping(eth::AssemblyItems const& _items) const;
Json::Value const& contractABI(Contract const&) const; Json::Value const& contractABI(Contract const&) const;
Json::Value const& natspec(Contract const&, DocumentationType _type) const; Json::Value const& natspecUser(Contract const&) const;
Json::Value const& natspecDev(Contract const&) const;
/// @returns the offset of the entry point of the given function into the list of assembly items /// @returns the offset of the entry point of the given function into the list of assembly items
/// or zero if it is not found or does not exist. /// or zero if it is not found or does not exist.

View File

@ -394,8 +394,8 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
Json::Value contractData(Json::objectValue); Json::Value contractData(Json::objectValue);
contractData["abi"] = m_compilerStack.contractABI(contractName); contractData["abi"] = m_compilerStack.contractABI(contractName);
contractData["metadata"] = m_compilerStack.metadata(contractName); contractData["metadata"] = m_compilerStack.metadata(contractName);
contractData["userdoc"] = m_compilerStack.natspec(contractName, DocumentationType::NatspecUser); contractData["userdoc"] = m_compilerStack.natspecUser(contractName);
contractData["devdoc"] = m_compilerStack.natspec(contractName, DocumentationType::NatspecDev); contractData["devdoc"] = m_compilerStack.natspecDev(contractName);
// EVM // EVM
Json::Value evmData(Json::objectValue); Json::Value evmData(Json::objectValue);

View File

@ -316,31 +316,32 @@ void CommandLineInterface::handleABI(string const& _contract)
cout << "Contract JSON ABI " << endl << data << endl; cout << "Contract JSON ABI " << endl << data << endl;
} }
void CommandLineInterface::handleNatspec(DocumentationType _type, string const& _contract) void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contract)
{ {
std::string argName; std::string argName;
std::string suffix; std::string suffix;
std::string title; std::string title;
switch(_type)
if (_natspecDev)
{ {
case DocumentationType::NatspecUser:
argName = g_argNatspecUser;
suffix = ".docuser";
title = "User Documentation";
break;
case DocumentationType::NatspecDev:
argName = g_argNatspecDev; argName = g_argNatspecDev;
suffix = ".docdev"; suffix = ".docdev";
title = "Developer Documentation"; title = "Developer Documentation";
break; }
default: else
// should never happen {
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); argName = g_argNatspecUser;
suffix = ".docuser";
title = "User Documentation";
} }
if (m_args.count(argName)) if (m_args.count(argName))
{ {
std::string output = dev::jsonPrettyPrint(m_compiler->natspec(_contract, _type)); std::string output = dev::jsonPrettyPrint(
_natspecDev ?
m_compiler->natspecDev(_contract) :
m_compiler->natspecUser(_contract)
);
if (m_args.count(g_argOutputDir)) if (m_args.count(g_argOutputDir))
createFile(m_compiler->filesystemFriendlyName(_contract) + suffix, output); createFile(m_compiler->filesystemFriendlyName(_contract) + suffix, output);
@ -881,9 +882,9 @@ void CommandLineInterface::handleCombinedJSON()
if (requests.count(g_strSignatureHashes)) if (requests.count(g_strSignatureHashes))
contractData[g_strSignatureHashes] = m_compiler->methodIdentifiers(contractName); contractData[g_strSignatureHashes] = m_compiler->methodIdentifiers(contractName);
if (requests.count(g_strNatspecDev)) if (requests.count(g_strNatspecDev))
contractData[g_strNatspecDev] = dev::jsonCompactPrint(m_compiler->natspec(contractName, DocumentationType::NatspecDev)); contractData[g_strNatspecDev] = dev::jsonCompactPrint(m_compiler->natspecDev(contractName));
if (requests.count(g_strNatspecUser)) if (requests.count(g_strNatspecUser))
contractData[g_strNatspecUser] = dev::jsonCompactPrint(m_compiler->natspec(contractName, DocumentationType::NatspecUser)); contractData[g_strNatspecUser] = dev::jsonCompactPrint(m_compiler->natspecUser(contractName));
output[g_strContracts][contractName] = contractData; output[g_strContracts][contractName] = contractData;
} }
@ -1170,8 +1171,8 @@ void CommandLineInterface::outputCompilationResults()
handleSignatureHashes(contract); handleSignatureHashes(contract);
handleMetadata(contract); handleMetadata(contract);
handleABI(contract); handleABI(contract);
handleNatspec(DocumentationType::NatspecDev, contract); handleNatspec(true, contract);
handleNatspec(DocumentationType::NatspecUser, contract); handleNatspec(false, contract);
} // end of contracts iteration } // end of contracts iteration
if (m_args.count(g_argFormal)) if (m_args.count(g_argFormal))

View File

@ -66,7 +66,7 @@ private:
void handleSignatureHashes(std::string const& _contract); void handleSignatureHashes(std::string const& _contract);
void handleMetadata(std::string const& _contract); void handleMetadata(std::string const& _contract);
void handleABI(std::string const& _contract); void handleABI(std::string const& _contract);
void handleNatspec(DocumentationType _type, std::string const& _contract); void handleNatspec(bool _natspecDev, std::string const& _contract);
void handleGasEstimation(std::string const& _contract); void handleGasEstimation(std::string const& _contract);
void handleFormal(); void handleFormal();

View File

@ -51,9 +51,9 @@ public:
Json::Value generatedDocumentation; Json::Value generatedDocumentation;
if (_userDocumentation) if (_userDocumentation)
generatedDocumentation = m_compilerStack.natspec("", DocumentationType::NatspecUser); generatedDocumentation = m_compilerStack.natspecUser("");
else else
generatedDocumentation = m_compilerStack.natspec("", DocumentationType::NatspecDev); generatedDocumentation = m_compilerStack.natspecDev("");
Json::Value expectedDocumentation; Json::Value expectedDocumentation;
m_reader.parse(_expectedDocumentationString, expectedDocumentation); m_reader.parse(_expectedDocumentationString, expectedDocumentation);
BOOST_CHECK_MESSAGE( BOOST_CHECK_MESSAGE(