Browse Source

Merge pull request #566 from LefterisJP/natspec_cpp11_enums

Replacing old cstyle enums with c++11 enums in natspec
cl-refactor
chriseth 10 years ago
parent
commit
fd6eb8eded
  1. 8
      libsolidity/CompilerStack.cpp
  2. 4
      libsolidity/CompilerStack.h
  3. 38
      libsolidity/InterfaceHandler.cpp
  4. 20
      libsolidity/InterfaceHandler.h
  5. 6
      solc/main.cpp
  6. 2
      test/solidityJSONInterfaceTest.cpp
  7. 4
      test/solidityNatspecJSON.cpp

8
libsolidity/CompilerStack.cpp

@ -84,7 +84,7 @@ void CompilerStack::streamAssembly(ostream& _outStream)
m_compiler->streamAssembly(_outStream); m_compiler->streamAssembly(_outStream);
} }
std::string const& CompilerStack::getJsonDocumentation(enum DocumentationType _type) std::string const& CompilerStack::getJsonDocumentation(DocumentationType _type)
{ {
if (!m_parseSuccessful) if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
@ -97,13 +97,13 @@ std::string const& CompilerStack::getJsonDocumentation(enum DocumentationType _t
switch (_type) switch (_type)
{ {
case NATSPEC_USER: case DocumentationType::NATSPEC_USER:
createDocIfNotThere(m_userDocumentation); createDocIfNotThere(m_userDocumentation);
return *m_userDocumentation; return *m_userDocumentation;
case NATSPEC_DEV: case DocumentationType::NATSPEC_DEV:
createDocIfNotThere(m_devDocumentation); createDocIfNotThere(m_devDocumentation);
return *m_devDocumentation; return *m_devDocumentation;
case ABI_INTERFACE: case DocumentationType::ABI_INTERFACE:
createDocIfNotThere(m_interface); createDocIfNotThere(m_interface);
return *m_interface; return *m_interface;
} }

4
libsolidity/CompilerStack.h

@ -37,7 +37,7 @@ class Compiler;
class GlobalContext; class GlobalContext;
class InterfaceHandler; class InterfaceHandler;
enum DocumentationType: unsigned short enum class DocumentationType: uint8_t
{ {
NATSPEC_USER = 1, NATSPEC_USER = 1,
NATSPEC_DEV, NATSPEC_DEV,
@ -74,7 +74,7 @@ public:
/// Prerequisite: Successful call to parse or compile. /// Prerequisite: Successful call to parse or compile.
/// @param type The type of the documentation to get. /// @param type The type of the documentation to get.
/// Can be one of 3 types defined at @c documentation_type /// Can be one of 3 types defined at @c documentation_type
std::string const& getJsonDocumentation(enum DocumentationType type); std::string const& getJsonDocumentation(DocumentationType type);
/// Returns the previously used scanner, useful for counting lines during error reporting. /// Returns the previously used scanner, useful for counting lines during error reporting.
Scanner const& getScanner() const { return *m_scanner; } Scanner const& getScanner() const { return *m_scanner; }

38
libsolidity/InterfaceHandler.cpp

@ -12,19 +12,19 @@ namespace solidity
InterfaceHandler::InterfaceHandler() InterfaceHandler::InterfaceHandler()
{ {
m_lastTag = DOCTAG_NONE; m_lastTag = DocTagType::NONE;
} }
std::unique_ptr<std::string> InterfaceHandler::getDocumentation(std::shared_ptr<ContractDefinition> _contractDef, std::unique_ptr<std::string> InterfaceHandler::getDocumentation(std::shared_ptr<ContractDefinition> _contractDef,
enum DocumentationType _type) DocumentationType _type)
{ {
switch(_type) switch(_type)
{ {
case NATSPEC_USER: case DocumentationType::NATSPEC_USER:
return getUserDocumentation(_contractDef); return getUserDocumentation(_contractDef);
case NATSPEC_DEV: case DocumentationType::NATSPEC_DEV:
return getDevDocumentation(_contractDef); return getDevDocumentation(_contractDef);
case ABI_INTERFACE: case DocumentationType::ABI_INTERFACE:
return getABIInterface(_contractDef); return getABIInterface(_contractDef);
} }
@ -146,7 +146,7 @@ static inline std::string::const_iterator skipLineOrEOS(std::string::const_itera
std::string::const_iterator InterfaceHandler::parseDocTagLine(std::string::const_iterator _pos, std::string::const_iterator InterfaceHandler::parseDocTagLine(std::string::const_iterator _pos,
std::string::const_iterator _end, std::string::const_iterator _end,
std::string& _tagString, std::string& _tagString,
enum DocTagType _tagType) DocTagType _tagType)
{ {
auto nlPos = std::find(_pos, _end, '\n'); auto nlPos = std::find(_pos, _end, '\n');
std::copy(_pos, nlPos, back_inserter(_tagString)); std::copy(_pos, nlPos, back_inserter(_tagString));
@ -170,7 +170,7 @@ std::string::const_iterator InterfaceHandler::parseDocTagParam(std::string::cons
auto paramDesc = std::string(currPos, nlPos); auto paramDesc = std::string(currPos, nlPos);
m_params.push_back(std::make_pair(paramName, paramDesc)); m_params.push_back(std::make_pair(paramName, paramDesc));
m_lastTag = DOCTAG_PARAM; m_lastTag = DocTagType::PARAM;
return skipLineOrEOS(nlPos, _end); return skipLineOrEOS(nlPos, _end);
} }
@ -197,14 +197,14 @@ std::string::const_iterator InterfaceHandler::parseDocTag(std::string::const_ite
{ {
// LTODO: need to check for @(start of a tag) between here and the end of line // LTODO: need to check for @(start of a tag) between here and the end of line
// for all cases // for all cases
if (m_lastTag == DOCTAG_NONE || _tag != "") if (m_lastTag == DocTagType::NONE || _tag != "")
{ {
if (_tag == "dev") if (_tag == "dev")
return parseDocTagLine(_pos, _end, m_dev, DOCTAG_DEV); return parseDocTagLine(_pos, _end, m_dev, DocTagType::DEV);
else if (_tag == "notice") else if (_tag == "notice")
return parseDocTagLine(_pos, _end, m_notice, DOCTAG_NOTICE); return parseDocTagLine(_pos, _end, m_notice, DocTagType::NOTICE);
else if (_tag == "return") else if (_tag == "return")
return parseDocTagLine(_pos, _end, m_return, DOCTAG_RETURN); return parseDocTagLine(_pos, _end, m_return, DocTagType::RETURN);
else if (_tag == "param") else if (_tag == "param")
return parseDocTagParam(_pos, _end); return parseDocTagParam(_pos, _end);
else else
@ -222,16 +222,16 @@ std::string::const_iterator InterfaceHandler::appendDocTag(std::string::const_it
{ {
switch (m_lastTag) switch (m_lastTag)
{ {
case DOCTAG_DEV: case DocTagType::DEV:
m_dev += " "; m_dev += " ";
return parseDocTagLine(_pos, _end, m_dev, DOCTAG_DEV); return parseDocTagLine(_pos, _end, m_dev, DocTagType::DEV);
case DOCTAG_NOTICE: case DocTagType::NOTICE:
m_notice += " "; m_notice += " ";
return parseDocTagLine(_pos, _end, m_notice, DOCTAG_NOTICE); return parseDocTagLine(_pos, _end, m_notice, DocTagType::NOTICE);
case DOCTAG_RETURN: case DocTagType::RETURN:
m_return += " "; m_return += " ";
return parseDocTagLine(_pos, _end, m_return, DOCTAG_RETURN); return parseDocTagLine(_pos, _end, m_return, DocTagType::RETURN);
case DOCTAG_PARAM: case DocTagType::PARAM:
return appendDocTagParam(_pos, _end); return appendDocTagParam(_pos, _end);
default: default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Internal: Illegal documentation tag type")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Internal: Illegal documentation tag type"));
@ -267,7 +267,7 @@ void InterfaceHandler::parseDocString(std::string const& _string)
currPos = parseDocTag(tagNameEndPos + 1, end, std::string(tagPos + 1, tagNameEndPos)); currPos = parseDocTag(tagNameEndPos + 1, end, std::string(tagPos + 1, tagNameEndPos));
} }
else if (m_lastTag != DOCTAG_NONE) // continuation of the previous tag else if (m_lastTag != DocTagType::NONE) // continuation of the previous tag
currPos = appendDocTag(currPos + 1, end); currPos = appendDocTag(currPos + 1, end);
else if (currPos != end) // skip the line if a newline was found else if (currPos != end) // skip the line if a newline was found
currPos = nlPos + 1; currPos = nlPos + 1;

20
libsolidity/InterfaceHandler.h

@ -37,15 +37,15 @@ namespace solidity
// Forward declarations // Forward declarations
class ContractDefinition; class ContractDefinition;
enum DocumentationType: unsigned short; enum class DocumentationType: uint8_t;
enum DocTagType enum class DocTagType: uint8_t
{ {
DOCTAG_NONE = 0, NONE = 0,
DOCTAG_DEV, DEV,
DOCTAG_NOTICE, NOTICE,
DOCTAG_PARAM, PARAM,
DOCTAG_RETURN RETURN
}; };
class InterfaceHandler class InterfaceHandler
@ -60,7 +60,7 @@ public:
/// @return A unique pointer contained string with the json /// @return A unique pointer contained string with the json
/// representation of provided type /// representation of provided type
std::unique_ptr<std::string> getDocumentation(std::shared_ptr<ContractDefinition> _contractDef, std::unique_ptr<std::string> getDocumentation(std::shared_ptr<ContractDefinition> _contractDef,
enum DocumentationType _type); DocumentationType _type);
/// Get the ABI Interface of the contract /// Get the ABI Interface 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
@ -84,7 +84,7 @@ private:
std::string::const_iterator parseDocTagLine(std::string::const_iterator _pos, std::string::const_iterator parseDocTagLine(std::string::const_iterator _pos,
std::string::const_iterator _end, std::string::const_iterator _end,
std::string& _tagString, std::string& _tagString,
enum DocTagType _tagType); DocTagType _tagType);
std::string::const_iterator parseDocTagParam(std::string::const_iterator _pos, std::string::const_iterator parseDocTagParam(std::string::const_iterator _pos,
std::string::const_iterator _end); std::string::const_iterator _end);
std::string::const_iterator appendDocTagParam(std::string::const_iterator _pos, std::string::const_iterator appendDocTagParam(std::string::const_iterator _pos,
@ -99,7 +99,7 @@ private:
Json::StyledWriter m_writer; Json::StyledWriter m_writer;
// internal state // internal state
enum DocTagType m_lastTag; DocTagType m_lastTag;
std::string m_notice; std::string m_notice;
std::string m_dev; std::string m_dev;
std::string m_return; std::string m_return;

6
solc/main.cpp

@ -135,9 +135,9 @@ int main(int argc, char** argv)
cout << "Opcodes:" << endl; cout << "Opcodes:" << endl;
cout << eth::disassemble(compiler.getBytecode()) << endl; cout << eth::disassemble(compiler.getBytecode()) << endl;
cout << "Binary: " << toHex(compiler.getBytecode()) << endl; cout << "Binary: " << toHex(compiler.getBytecode()) << endl;
cout << "Interface specification: " << compiler.getJsonDocumentation(ABI_INTERFACE) << endl; cout << "Interface specification: " << compiler.getJsonDocumentation(DocumentationType::ABI_INTERFACE) << endl;
cout << "Natspec user documentation: " << compiler.getJsonDocumentation(NATSPEC_USER) << endl; cout << "Natspec user documentation: " << compiler.getJsonDocumentation(DocumentationType::NATSPEC_USER) << endl;
cout << "Natspec developer documentation: " << compiler.getJsonDocumentation(NATSPEC_DEV) << endl; cout << "Natspec developer documentation: " << compiler.getJsonDocumentation(DocumentationType::NATSPEC_DEV) << endl;
return 0; return 0;
} }

2
test/solidityJSONInterfaceTest.cpp

@ -50,7 +50,7 @@ public:
msg += *extra; msg += *extra;
BOOST_FAIL(msg); BOOST_FAIL(msg);
} }
std::string generatedInterfaceString = m_compilerStack.getJsonDocumentation(ABI_INTERFACE); std::string generatedInterfaceString = m_compilerStack.getJsonDocumentation(DocumentationType::ABI_INTERFACE);
Json::Value generatedInterface; Json::Value generatedInterface;
m_reader.parse(generatedInterfaceString, generatedInterface); m_reader.parse(generatedInterfaceString, generatedInterface);
Json::Value expectedInterface; Json::Value expectedInterface;

4
test/solidityNatspecJSON.cpp

@ -55,9 +55,9 @@ public:
} }
if (_userDocumentation) if (_userDocumentation)
generatedDocumentationString = m_compilerStack.getJsonDocumentation(NATSPEC_USER); generatedDocumentationString = m_compilerStack.getJsonDocumentation(DocumentationType::NATSPEC_USER);
else else
generatedDocumentationString = m_compilerStack.getJsonDocumentation(NATSPEC_DEV); generatedDocumentationString = m_compilerStack.getJsonDocumentation(DocumentationType::NATSPEC_DEV);
Json::Value generatedDocumentation; Json::Value generatedDocumentation;
m_reader.parse(generatedDocumentationString, generatedDocumentation); m_reader.parse(generatedDocumentationString, generatedDocumentation);
Json::Value expectedDocumentation; Json::Value expectedDocumentation;

Loading…
Cancel
Save