From 2364c55735f8e511dbed6e46f83975a867839e12 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 11 Jan 2016 19:40:15 +0100 Subject: Detect library name clashes. --- libsolidity/codegen/ExpressionCompiler.cpp | 2 -- libsolidity/interface/CompilerStack.cpp | 33 ++++++++++++++++++++++++++++++ libsolidity/interface/CompilerStack.h | 3 +++ 3 files changed, 36 insertions(+), 2 deletions(-) (limited to 'libsolidity') diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index f0dab41a..040217da 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -807,7 +807,6 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess) ); auto contract = dynamic_cast(funType->declaration().scope()); solAssert(contract && contract->isLibrary(), ""); - //@TODO library name might not be unique m_context.appendLibraryAddress(contract->name()); m_context << funType->externalIdentifier(); utils().moveIntoStack(funType->selfType()->sizeOnStack(), 2); @@ -1118,7 +1117,6 @@ void ExpressionCompiler::endVisit(Identifier const& _identifier) else if (auto contract = dynamic_cast(declaration)) { if (contract->isLibrary()) - //@todo name should be unique, change once we have module management m_context.appendLibraryAddress(contract->name()); } else if (dynamic_cast(declaration)) diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index a6f6f224..9a7d6982 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -148,6 +148,9 @@ bool CompilerStack::parse() m_contracts[contract->name()].contract = contract; } + if (!checkLibraryNameClashes()) + noErrors = false; + for (Source const* source: m_sourceOrder) for (ASTPointer const& node: source->ast->nodes()) if (ContractDefinition* contract = dynamic_cast(node.get())) @@ -400,6 +403,36 @@ void CompilerStack::resolveImports() swap(m_sourceOrder, sourceOrder); } +bool CompilerStack::checkLibraryNameClashes() +{ + bool clashFound = false; + map libraries; + for (Source const* source: m_sourceOrder) + for (ASTPointer const& node: source->ast->nodes()) + if (ContractDefinition* contract = dynamic_cast(node.get())) + if (contract->isLibrary()) + { + if (libraries.count(contract->name())) + { + auto err = make_shared(Error::Type::DeclarationError); + *err << + errinfo_sourceLocation(contract->location()) << + errinfo_comment( + "Library \"" + contract->name() + "\" declared twice " + "(will create ambiguities during linking)." + ) << + errinfo_secondarySourceLocation(SecondarySourceLocation().append( + "The other declaration is here:", libraries[contract->name()] + )); + + m_errors.push_back(err); + } + else + libraries[contract->name()] = contract->location(); + } + return !clashFound; +} + string CompilerStack::absolutePath(string const& _path, string const& _reference) const { // Anything that does not start with `.` is an absolute path. diff --git a/libsolidity/interface/CompilerStack.h b/libsolidity/interface/CompilerStack.h index 3e6dc456..6cf19db4 100644 --- a/libsolidity/interface/CompilerStack.h +++ b/libsolidity/interface/CompilerStack.h @@ -199,6 +199,9 @@ private: }; void resolveImports(); + /// Checks whether there are libraries with the same name, reports that as an error and + /// @returns false in this case. + bool checkLibraryNameClashes(); /// @returns the absolute path corresponding to @a _path relative to @a _reference. std::string absolutePath(std::string const& _path, std::string const& _reference) const; /// Compile a single contract and put the result in @a _compiledContracts. -- cgit v1.2.3 From 17199edb4f7011d062ae126f7910314ed490d2c7 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 13 Jan 2016 17:28:27 +0100 Subject: Set error flag to true. --- libsolidity/interface/CompilerStack.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'libsolidity') diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 9a7d6982..aaff09ba 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -426,6 +426,7 @@ bool CompilerStack::checkLibraryNameClashes() )); m_errors.push_back(err); + clashFound = true; } else libraries[contract->name()] = contract->location(); -- cgit v1.2.3