Browse Source

contract documentation is now parsing during compilation and not by request.

cl-refactor
Liana Husikyan 10 years ago
parent
commit
e8bc2e7667
  1. 21
      libsolidity/AST.cpp
  2. 10
      libsolidity/AST.h
  3. 8
      libsolidity/CompilerStack.cpp
  4. 3
      libsolidity/CompilerStack.h
  5. 24
      libsolidity/InterfaceHandler.cpp
  6. 12
      libsolidity/InterfaceHandler.h
  7. 2
      libsolidity/Parser.cpp
  8. 5
      solc/CommandLineInterface.cpp
  9. 4
      solc/jsonCompiler.cpp

21
libsolidity/AST.cpp

@ -382,6 +382,27 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::getIn
return *m_interfaceFunctionList; return *m_interfaceFunctionList;
} }
unique_ptr<string> ContractDefinition::devDocumentation() const
{
return unique_ptr<string>(new string(m_devDocumentation));
}
void ContractDefinition::setDevDocumentation(string const& _devDocumentation)
{
m_devDocumentation = _devDocumentation;
}
std::unique_ptr<string> ContractDefinition::userDocumentation() const
{
return unique_ptr<string>(new string(m_userDocumentation));
}
void ContractDefinition::setUserDocumentation(string const& _userDocumentation)
{
m_userDocumentation = _userDocumentation;
}
vector<Declaration const*> const& ContractDefinition::getInheritableMembers() const vector<Declaration const*> const& ContractDefinition::getInheritableMembers() const
{ {
if (!m_inheritableMembers) if (!m_inheritableMembers)

10
libsolidity/AST.h

@ -281,6 +281,12 @@ public:
/// Returns the fallback function or nullptr if no fallback function was specified. /// Returns the fallback function or nullptr if no fallback function was specified.
FunctionDefinition const* getFallbackFunction() const; FunctionDefinition const* getFallbackFunction() const;
std::unique_ptr<std::string> userDocumentation() const;
void setUserDocumentation(std::string const& _userDocumentation);
std::unique_ptr<std::string> devDocumentation() const;
void setDevDocumentation(std::string const& _devDocumentation);
private: private:
/// Checks that two functions defined in this contract with the same name have different /// Checks that two functions defined in this contract with the same name have different
/// arguments and that there is at most one constructor. /// arguments and that there is at most one constructor.
@ -302,6 +308,10 @@ private:
std::vector<ASTPointer<ModifierDefinition>> m_functionModifiers; std::vector<ASTPointer<ModifierDefinition>> m_functionModifiers;
std::vector<ASTPointer<EventDefinition>> m_events; std::vector<ASTPointer<EventDefinition>> m_events;
// parsed Natspec documentation of the contract.
std::string m_userDocumentation;
std::string m_devDocumentation;
std::vector<ContractDefinition const*> m_linearizedBaseContracts; std::vector<ContractDefinition const*> m_linearizedBaseContracts;
mutable std::unique_ptr<std::vector<std::pair<FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList; mutable std::unique_ptr<std::vector<std::pair<FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList;
mutable std::unique_ptr<std::vector<ASTPointer<EventDefinition>>> m_interfaceEvents; mutable std::unique_ptr<std::vector<ASTPointer<EventDefinition>>> m_interfaceEvents;

8
libsolidity/CompilerStack.cpp

@ -124,10 +124,18 @@ void CompilerStack::parse()
resolver.updateDeclaration(*m_globalContext->getCurrentThis()); resolver.updateDeclaration(*m_globalContext->getCurrentThis());
resolver.checkTypeRequirements(*contract); resolver.checkTypeRequirements(*contract);
m_contracts[contract->getName()].contract = contract; m_contracts[contract->getName()].contract = contract;
parseNatspecDocumentation(*contract);
} }
m_parseSuccessful = true; m_parseSuccessful = true;
} }
void CompilerStack::parseNatspecDocumentation(ContractDefinition& _contract)
{
InterfaceHandler interfaceHandler;
interfaceHandler.generateDevDocumentation(_contract);
interfaceHandler.generateUserDocumentation(_contract);
}
void CompilerStack::parse(string const& _sourceCode) void CompilerStack::parse(string const& _sourceCode)
{ {
setSource(_sourceCode); setSource(_sourceCode);

3
libsolidity/CompilerStack.h

@ -148,6 +148,9 @@ public:
/// start line, start column, end line, end column /// start line, start column, end line, end column
std::tuple<int, int, int, int> positionFromSourceLocation(SourceLocation const& _sourceLocation) const; std::tuple<int, int, int, int> positionFromSourceLocation(SourceLocation const& _sourceLocation) const;
/// Parses Natspec documentations. Throws exceptions in case of wrong documented contract
void parseNatspecDocumentation(dev::solidity::ContractDefinition& _contract);
private: private:
/** /**
* Information pertaining to one source unit, filled gradually during parsing and compilation. * Information pertaining to one source unit, filled gradually during parsing and compilation.

24
libsolidity/InterfaceHandler.cpp

@ -24,9 +24,9 @@ unique_ptr<string> InterfaceHandler::getDocumentation(
switch(_type) switch(_type)
{ {
case DocumentationType::NatspecUser: case DocumentationType::NatspecUser:
return getUserDocumentation(_contractDef); return userDocumentation(_contractDef);
case DocumentationType::NatspecDev: case DocumentationType::NatspecDev:
return getDevDocumentation(_contractDef); return devDocumentation(_contractDef);
case DocumentationType::ABIInterface: case DocumentationType::ABIInterface:
return getABIInterface(_contractDef); return getABIInterface(_contractDef);
case DocumentationType::ABISolidityInterface: case DocumentationType::ABISolidityInterface:
@ -143,7 +143,7 @@ unique_ptr<string> InterfaceHandler::getABISolidityInterface(ContractDefinition
return unique_ptr<string>(new string(ret + "}")); return unique_ptr<string>(new string(ret + "}"));
} }
unique_ptr<string> InterfaceHandler::getUserDocumentation(ContractDefinition const& _contractDef) void InterfaceHandler::generateUserDocumentation(ContractDefinition& _contractDef)
{ {
Json::Value doc; Json::Value doc;
Json::Value methods(Json::objectValue); Json::Value methods(Json::objectValue);
@ -165,10 +165,20 @@ unique_ptr<string> InterfaceHandler::getUserDocumentation(ContractDefinition con
} }
doc["methods"] = methods; doc["methods"] = methods;
return unique_ptr<string>(new string(Json::FastWriter().write(doc))); _contractDef.setUserDocumentation(string(Json::FastWriter().write(doc)));
} }
unique_ptr<string> InterfaceHandler::getDevDocumentation(ContractDefinition const& _contractDef) unique_ptr<string> InterfaceHandler::userDocumentation(ContractDefinition const& _contractDef)
{
return _contractDef.userDocumentation();
}
unique_ptr<string> InterfaceHandler::devDocumentation(ContractDefinition const& _contractDef)
{
return _contractDef.devDocumentation();
}
void InterfaceHandler::generateDevDocumentation(ContractDefinition& _contractDef)
{ {
// LTODO: Somewhere in this function warnings for mismatch of param names // LTODO: Somewhere in this function warnings for mismatch of param names
// should be thrown // should be thrown
@ -212,7 +222,7 @@ unique_ptr<string> InterfaceHandler::getDevDocumentation(ContractDefinition cons
// LTODO: mismatching parameter name, throw some form of warning and not just an exception // LTODO: mismatching parameter name, throw some form of warning and not just an exception
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(
DocstringParsingError() << 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; params[pair.first] = pair.second;
} }
@ -229,7 +239,7 @@ unique_ptr<string> InterfaceHandler::getDevDocumentation(ContractDefinition cons
} }
doc["methods"] = methods; doc["methods"] = methods;
return unique_ptr<string>(new string(Json::FastWriter().write(doc))); _contractDef.setDevDocumentation(string(Json::FastWriter().write(doc)));
} }
/* -- private -- */ /* -- private -- */

12
libsolidity/InterfaceHandler.h

@ -77,16 +77,22 @@ public:
/// representation of the contract's ABI Interface /// representation of the contract's ABI Interface
std::unique_ptr<std::string> getABIInterface(ContractDefinition const& _contractDef); std::unique_ptr<std::string> getABIInterface(ContractDefinition const& _contractDef);
std::unique_ptr<std::string> getABISolidityInterface(ContractDefinition const& _contractDef); std::unique_ptr<std::string> 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 /// Get the User documentation of the contract
/// @param _contractDef The contract definition /// @param _contractDef The contract definition
/// @return A unique pointer contained string with the json /// @return A unique pointer contained string with the json
/// representation of the contract's user documentation /// representation of the contract's user documentation
std::unique_ptr<std::string> getUserDocumentation(ContractDefinition const& _contractDef); std::unique_ptr<std::string> userDocumentation(ContractDefinition const& _contractDef);
/// Get the Developer's documentation of the contract /// 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 /// @param _contractDef The contract definition
/// @return A unique pointer contained string with the json /// @return A unique pointer contained string with the json
/// representation of the contract's developer documentation /// representation of the contract's developer documentation
std::unique_ptr<std::string> getDevDocumentation(ContractDefinition const& _contractDef); std::unique_ptr<std::string> devDocumentation(ContractDefinition const& _contractDef);
private: private:
void resetUser(); void resetUser();

2
libsolidity/Parser.cpp

@ -26,6 +26,8 @@
#include <libsolidity/Parser.h> #include <libsolidity/Parser.h>
#include <libsolidity/Scanner.h> #include <libsolidity/Scanner.h>
#include <libsolidity/Exceptions.h> #include <libsolidity/Exceptions.h>
#include <libsolidity/InterfaceHandler.h>
using namespace std; using namespace std;

5
solc/CommandLineInterface.cpp

@ -449,6 +449,11 @@ bool CommandLineInterface::processInput()
<< boost::diagnostic_information(_exception); << boost::diagnostic_information(_exception);
return false; return false;
} }
catch (DocstringParsingError const& _exception)
{
cerr << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_exception) << endl;
return false;
}
catch (Exception const& _exception) catch (Exception const& _exception)
{ {
cerr << "Exception during compilation: " << boost::diagnostic_information(_exception) << endl; cerr << "Exception during compilation: " << boost::diagnostic_information(_exception) << endl;

4
solc/jsonCompiler.cpp

@ -147,6 +147,10 @@ string compile(string _input, bool _optimize)
{ {
return formatError(exception, "Internal compiler error", compiler); return formatError(exception, "Internal compiler error", compiler);
} }
catch (DocstringParsingError const& exception)
{
return formatError(exception, "Documentation parsing error", compiler);
}
catch (Exception const& exception) catch (Exception const& exception)
{ {
output["error"] = "Exception during compilation: " + boost::diagnostic_information(exception); output["error"] = "Exception during compilation: " + boost::diagnostic_information(exception);

Loading…
Cancel
Save