From 43398adc99b1e05c80dd31d44d742b4851c8771d Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Mon, 15 Jun 2015 15:21:23 +0200 Subject: [PATCH] contract documentation is now parsing during compilation and not by request. --- AST.cpp | 21 +++++++++++++++++++++ AST.h | 10 ++++++++++ CompilerStack.cpp | 8 ++++++++ CompilerStack.h | 3 +++ InterfaceHandler.cpp | 24 +++++++++++++++++------- InterfaceHandler.h | 12 +++++++++--- Parser.cpp | 2 ++ 7 files changed, 70 insertions(+), 10 deletions(-) diff --git a/AST.cpp b/AST.cpp index 3a83058c1..270ec3ed7 100644 --- a/AST.cpp +++ b/AST.cpp @@ -382,6 +382,27 @@ vector, FunctionTypePointer>> const& ContractDefinition::getIn return *m_interfaceFunctionList; } +unique_ptr ContractDefinition::devDocumentation() const +{ + return unique_ptr(new string(m_devDocumentation)); +} + +void ContractDefinition::setDevDocumentation(string const& _devDocumentation) +{ + m_devDocumentation = _devDocumentation; +} + +std::unique_ptr ContractDefinition::userDocumentation() const +{ + return unique_ptr(new string(m_userDocumentation)); +} + +void ContractDefinition::setUserDocumentation(string const& _userDocumentation) +{ + m_userDocumentation = _userDocumentation; +} + + vector const& ContractDefinition::getInheritableMembers() const { if (!m_inheritableMembers) diff --git a/AST.h b/AST.h index 658b3de9f..38262d009 100644 --- a/AST.h +++ b/AST.h @@ -281,6 +281,12 @@ public: /// Returns the fallback function or nullptr if no fallback function was specified. FunctionDefinition const* getFallbackFunction() const; + std::unique_ptr userDocumentation() const; + void setUserDocumentation(std::string const& _userDocumentation); + + std::unique_ptr devDocumentation() const; + void setDevDocumentation(std::string const& _devDocumentation); + private: /// Checks that two functions defined in this contract with the same name have different /// arguments and that there is at most one constructor. @@ -302,6 +308,10 @@ private: std::vector> m_functionModifiers; std::vector> m_events; + // parsed Natspec documentation of the contract. + std::string m_userDocumentation; + std::string m_devDocumentation; + std::vector m_linearizedBaseContracts; mutable std::unique_ptr, FunctionTypePointer>>> m_interfaceFunctionList; mutable std::unique_ptr>> m_interfaceEvents; diff --git a/CompilerStack.cpp b/CompilerStack.cpp index a3399823e..7cd8316c7 100644 --- a/CompilerStack.cpp +++ b/CompilerStack.cpp @@ -124,10 +124,18 @@ void CompilerStack::parse() resolver.updateDeclaration(*m_globalContext->getCurrentThis()); resolver.checkTypeRequirements(*contract); m_contracts[contract->getName()].contract = contract; + parseNatspecDocumentation(*contract); } m_parseSuccessful = true; } +void CompilerStack::parseNatspecDocumentation(ContractDefinition& _contract) +{ + InterfaceHandler interfaceHandler; + interfaceHandler.generateDevDocumentation(_contract); + interfaceHandler.generateUserDocumentation(_contract); +} + void CompilerStack::parse(string const& _sourceCode) { setSource(_sourceCode); diff --git a/CompilerStack.h b/CompilerStack.h index a7c6ea3ba..fdf2562bc 100644 --- a/CompilerStack.h +++ b/CompilerStack.h @@ -148,6 +148,9 @@ public: /// start line, start column, end line, end column std::tuple positionFromSourceLocation(SourceLocation const& _sourceLocation) const; + /// Parses Natspec documentations. Throws exceptions in case of wrong documented contract + void parseNatspecDocumentation(dev::solidity::ContractDefinition& _contract); + private: /** * Information pertaining to one source unit, filled gradually during parsing and compilation. diff --git a/InterfaceHandler.cpp b/InterfaceHandler.cpp index 23b7ff82f..36940d278 100644 --- a/InterfaceHandler.cpp +++ b/InterfaceHandler.cpp @@ -24,9 +24,9 @@ unique_ptr InterfaceHandler::getDocumentation( switch(_type) { case DocumentationType::NatspecUser: - return getUserDocumentation(_contractDef); + return userDocumentation(_contractDef); case DocumentationType::NatspecDev: - return getDevDocumentation(_contractDef); + return devDocumentation(_contractDef); case DocumentationType::ABIInterface: return getABIInterface(_contractDef); case DocumentationType::ABISolidityInterface: @@ -143,7 +143,7 @@ unique_ptr InterfaceHandler::getABISolidityInterface(ContractDefinition return unique_ptr(new string(ret + "}")); } -unique_ptr InterfaceHandler::getUserDocumentation(ContractDefinition const& _contractDef) +void InterfaceHandler::generateUserDocumentation(ContractDefinition& _contractDef) { Json::Value doc; Json::Value methods(Json::objectValue); @@ -165,10 +165,20 @@ unique_ptr InterfaceHandler::getUserDocumentation(ContractDefinition con } doc["methods"] = methods; - return unique_ptr(new string(Json::FastWriter().write(doc))); + _contractDef.setUserDocumentation(string(Json::FastWriter().write(doc))); } -unique_ptr InterfaceHandler::getDevDocumentation(ContractDefinition const& _contractDef) +unique_ptr InterfaceHandler::userDocumentation(ContractDefinition const& _contractDef) +{ + return _contractDef.userDocumentation(); +} + +unique_ptr InterfaceHandler::devDocumentation(ContractDefinition const& _contractDef) +{ + return _contractDef.devDocumentation(); +} + +void InterfaceHandler::generateDevDocumentation(ContractDefinition& _contractDef) { // LTODO: Somewhere in this function warnings for mismatch of param names // should be thrown @@ -212,7 +222,7 @@ unique_ptr InterfaceHandler::getDevDocumentation(ContractDefinition cons // LTODO: mismatching parameter name, throw some form of warning and not just an exception BOOST_THROW_EXCEPTION( DocstringParsingError() << - errinfo_comment("documented parameter \"" + pair.first + "\" not found found in the function") + errinfo_comment("documented parameter \"" + pair.first + "\" not found in the parameter list of the function.") ); params[pair.first] = pair.second; } @@ -229,7 +239,7 @@ unique_ptr InterfaceHandler::getDevDocumentation(ContractDefinition cons } doc["methods"] = methods; - return unique_ptr(new string(Json::FastWriter().write(doc))); + _contractDef.setDevDocumentation(string(Json::FastWriter().write(doc))); } /* -- private -- */ diff --git a/InterfaceHandler.h b/InterfaceHandler.h index ca9807d7e..1cc20c444 100644 --- a/InterfaceHandler.h +++ b/InterfaceHandler.h @@ -77,16 +77,22 @@ public: /// representation of the contract's ABI Interface std::unique_ptr getABIInterface(ContractDefinition const& _contractDef); std::unique_ptr getABISolidityInterface(ContractDefinition const& _contractDef); + /// Generate the User documentation of the contract + /// @param _contractDef The contract definition + void generateUserDocumentation(ContractDefinition& _contractDef); /// Get the User documentation of the contract /// @param _contractDef The contract definition /// @return A unique pointer contained string with the json /// representation of the contract's user documentation - std::unique_ptr getUserDocumentation(ContractDefinition const& _contractDef); - /// Get the Developer's documentation of the contract + std::unique_ptr userDocumentation(ContractDefinition const& _contractDef); + /// Genereates the Developer's documentation of the contract + /// @param _contractDef The contract definition + void generateDevDocumentation(ContractDefinition& _contractDef); + /// Genereates the Developer's documentation of the contract /// @param _contractDef The contract definition /// @return A unique pointer contained string with the json /// representation of the contract's developer documentation - std::unique_ptr getDevDocumentation(ContractDefinition const& _contractDef); + std::unique_ptr devDocumentation(ContractDefinition const& _contractDef); private: void resetUser(); diff --git a/Parser.cpp b/Parser.cpp index 548b7dc21..7ca1a40d3 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -26,6 +26,8 @@ #include #include #include +#include + using namespace std;