Remove CharStream from SourceLocation.

This commit is contained in:
chriseth
2021-07-14 15:12:07 +02:00
parent 57d32ca252
commit f75b55071e
73 changed files with 613 additions and 560 deletions
+3 -3
View File
@@ -520,9 +520,9 @@ bool DeclarationRegistrationHelper::registerDeclaration(
Declaration const* conflictingDeclaration = _container.conflictingDeclaration(_declaration, _name);
solAssert(conflictingDeclaration, "");
bool const comparable =
_errorLocation->source &&
conflictingDeclaration->location().source &&
_errorLocation->source->name() == conflictingDeclaration->location().source->name();
_errorLocation->sourceName &&
conflictingDeclaration->location().sourceName &&
*_errorLocation->sourceName == *conflictingDeclaration->location().sourceName;
if (comparable && _errorLocation->start < conflictingDeclaration->location().start)
{
firstDeclarationLocation = *_errorLocation;
+1 -1
View File
@@ -68,7 +68,7 @@ void SyntaxChecker::endVisit(SourceUnit const& _sourceUnit)
string(";\"");
// when reporting the warning, print the source name only
m_errorReporter.warning(3420_error, {-1, -1, _sourceUnit.location().source}, errorString);
m_errorReporter.warning(3420_error, {-1, -1, _sourceUnit.location().sourceName}, errorString);
}
if (!m_sourceUnit->annotation().useABICoderV2.set())
m_sourceUnit->annotation().useABICoderV2 = true;
+2 -2
View File
@@ -110,8 +110,8 @@ void ASTJsonConverter::setJsonNode(
optional<size_t> ASTJsonConverter::sourceIndexFromLocation(SourceLocation const& _location) const
{
if (_location.source && m_sourceIndices.count(_location.source->name()))
return m_sourceIndices.at(_location.source->name());
if (_location.sourceName && m_sourceIndices.count(*_location.sourceName))
return m_sourceIndices.at(*_location.sourceName);
else
return nullopt;
}
+3 -1
View File
@@ -455,7 +455,9 @@ void CompilerContext::appendInlineAssembly(
_assembly + "\n"
"------------------ Errors: ----------------\n";
for (auto const& error: errorReporter.errors())
message += SourceReferenceFormatter::formatErrorInformation(*error);
// TODO if we have "locationOverride", it will be the wrong char stream,
// but we do not have access to the solidity scanner.
message += SourceReferenceFormatter::formatErrorInformation(*error, *scanner->charStream());
message += "-------------------------------------------\n";
solAssert(false, message);
+2 -1
View File
@@ -129,8 +129,9 @@ string IRNames::zeroValue(Type const& _type, string const& _variableName)
string sourceLocationComment(langutil::SourceLocation const& _location, IRGenerationContext const& _context)
{
solAssert(_location.sourceName, "");
return "/// @src "
+ to_string(_context.sourceIndices().at(_location.source->name()))
+ to_string(_context.sourceIndices().at(*_location.sourceName))
+ ":"
+ to_string(_location.start)
+ ","
+4 -1
View File
@@ -101,7 +101,10 @@ pair<string, string> IRGenerator::run(
{
string errorMessage;
for (auto const& error: asmStack.errors())
errorMessage += langutil::SourceReferenceFormatter::formatErrorInformation(*error);
errorMessage += langutil::SourceReferenceFormatter::formatErrorInformation(
*error,
asmStack.charStream("")
);
solAssert(false, ir + "\n\nInvalid IR generated:\n" + errorMessage + "\n");
}
asmStack.optimize();
+1 -1
View File
@@ -650,7 +650,7 @@ pair<vector<smtutil::Expression>, vector<string>> BMC::modelExpressions()
if (uf->annotation().type->isValueType())
{
expressionsToEvaluate.emplace_back(expr(*uf));
expressionNames.push_back(uf->location().text());
// TODO expressionNames.push_back(uf->location().text());
}
return {expressionsToEvaluate, expressionNames};
+3 -2
View File
@@ -200,8 +200,9 @@ string Predicate::formatSummaryCall(vector<smtutil::Expression> const& _args) co
{
solAssert(isSummary(), "");
if (auto funCall = programFunctionCall())
return funCall->location().text();
//if (auto funCall = programFunctionCall())
// return funCall->location().text();
// TODO
/// The signature of a function summary predicate is: summary(error, this, abiFunctions, cryptoFunctions, txData, preBlockChainState, preStateVars, preInputVars, postBlockchainState, postStateVars, postInputVars, outputVars).
/// Here we are interested in preInputVars to format the function call,
+5 -28
View File
@@ -926,19 +926,6 @@ map<string, unsigned> CompilerStack::sourceIndices() const
return indices;
}
map<unsigned, shared_ptr<CharStream>> CompilerStack::indicesToCharStreams() const
{
map<unsigned, shared_ptr<CharStream>> result;
unsigned index = 0;
for (auto const& s: m_sources)
result[index++] = s.second.scanner->charStream();
// NB: CompilerContext::yulUtilityFileName() does not have a source,
result[index++] = shared_ptr<CharStream>{};
return result;
}
Json::Value const& CompilerStack::contractABI(string const& _contractName) const
{
if (m_stackState < AnalysisPerformed)
@@ -1048,12 +1035,15 @@ string const& CompilerStack::metadata(Contract const& _contract) const
return _contract.metadata.init([&]{ return createMetadata(_contract); });
}
Scanner const& CompilerStack::scanner(string const& _sourceName) const
CharStream const& CompilerStack::charStream(string const& _sourceName) const
{
if (m_stackState < SourcesSet)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("No sources set."));
return *source(_sourceName).scanner;
solAssert(source(_sourceName).scanner, "");
solAssert(source(_sourceName).scanner->charStream(), "");
return *source(_sourceName).scanner->charStream();
}
SourceUnit const& CompilerStack::ast(string const& _sourceName) const
@@ -1095,19 +1085,6 @@ size_t CompilerStack::functionEntryPoint(
return 0;
}
tuple<int, int, int, int> CompilerStack::positionFromSourceLocation(SourceLocation const& _sourceLocation) const
{
int startLine;
int startColumn;
int endLine;
int endColumn;
tie(startLine, startColumn) = scanner(_sourceLocation.source->name()).translatePositionToLineColumn(_sourceLocation.start);
tie(endLine, endColumn) = scanner(_sourceLocation.source->name()).translatePositionToLineColumn(_sourceLocation.end);
return make_tuple(++startLine, ++startColumn, ++endLine, ++endColumn);
}
h256 const& CompilerStack::Source::keccak256() const
{
if (keccak256HashCached == h256{})
+6 -13
View File
@@ -38,6 +38,7 @@
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/EVMVersion.h>
#include <liblangutil/SourceLocation.h>
#include <liblangutil/CharStreamProvider.h>
#include <libevmasm/LinkerObject.h>
@@ -57,6 +58,7 @@
namespace solidity::langutil
{
class Scanner;
class CharStream;
}
@@ -87,7 +89,7 @@ class DeclarationContainer;
* If error recovery is active, it is possible to progress through the stages even when
* there are errors. In any case, producing code is only possible without errors.
*/
class CompilerStack
class CompilerStack: public langutil::CharStreamProvider
{
public:
/// Noncopyable.
@@ -120,7 +122,7 @@ public:
/// and must not emit exceptions.
explicit CompilerStack(ReadCallback::Callback _readFile = ReadCallback::Callback());
~CompilerStack();
~CompilerStack() override;
/// @returns the list of errors that occurred during parsing and type checking.
langutil::ErrorList const& errors() const { return m_errorReporter.errors(); }
@@ -239,12 +241,8 @@ public:
/// by sourceNames().
std::map<std::string, unsigned> sourceIndices() const;
/// @returns the reverse mapping of source indices to their respective
/// CharStream instances.
std::map<unsigned, std::shared_ptr<langutil::CharStream>> indicesToCharStreams() const;
/// @returns the previously used scanner, useful for counting lines during error reporting.
langutil::Scanner const& scanner(std::string const& _sourceName) const;
/// @returns the previously used character stream, useful for counting lines during error reporting.
langutil::CharStream const& charStream(std::string const& _sourceName) const override;
/// @returns the parsed source unit with the supplied name.
SourceUnit const& ast(std::string const& _sourceName) const;
@@ -253,11 +251,6 @@ public:
/// does not exist.
ContractDefinition const& contractDefinition(std::string const& _contractName) const;
/// Helper function for logs printing. Do only use in error cases, it's quite expensive.
/// line and columns are numbered starting from 1 with following order:
/// start line, start column, end line, end column
std::tuple<int, int, int, int> positionFromSourceLocation(langutil::SourceLocation const& _sourceLocation) const;
/// @returns a list of unhandled queries to the SMT solver (has to be supplied in a second run
/// by calling @a addSMTLib2Response).
std::vector<std::string> const& unhandledSMTLib2Queries() const { return m_unhandledSMTLib2Queries; }
+16 -3
View File
@@ -83,9 +83,9 @@ Json::Value formatFatalError(string const& _type, string const& _message)
Json::Value formatSourceLocation(SourceLocation const* location)
{
Json::Value sourceLocation;
if (location && location->source)
if (location && location->sourceName)
{
sourceLocation["file"] = location->source->name();
sourceLocation["file"] = *location->sourceName;
sourceLocation["start"] = location->start;
sourceLocation["end"] = location->end;
}
@@ -109,6 +109,7 @@ Json::Value formatSecondarySourceLocation(SecondarySourceLocation const* _second
}
Json::Value formatErrorWithException(
CharStreamProvider const& _charStreamProvider,
util::Exception const& _exception,
bool const& _warning,
string const& _type,
@@ -119,7 +120,11 @@ Json::Value formatErrorWithException(
{
string message;
// TODO: consider enabling color
string formattedMessage = SourceReferenceFormatter::formatExceptionInformation(_exception, _type);
string formattedMessage = SourceReferenceFormatter::formatExceptionInformation(
_exception,
_type,
_charStreamProvider
);
if (string const* description = boost::get_error_info<util::errinfo_comment>(_exception))
message = ((_message.length() > 0) ? (_message + ":") : "") + *description;
@@ -1017,6 +1022,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
Error const& err = dynamic_cast<Error const&>(*error);
errors.append(formatErrorWithException(
compilerStack,
*error,
err.type() == Error::Type::Warning,
err.typeName(),
@@ -1030,6 +1036,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
catch (Error const& _error)
{
errors.append(formatErrorWithException(
compilerStack,
_error,
false,
_error.typeName(),
@@ -1050,6 +1057,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
catch (CompilerError const& _exception)
{
errors.append(formatErrorWithException(
compilerStack,
_exception,
false,
"CompilerError",
@@ -1060,6 +1068,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
catch (InternalCompilerError const& _exception)
{
errors.append(formatErrorWithException(
compilerStack,
_exception,
false,
"InternalCompilerError",
@@ -1070,6 +1079,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
catch (UnimplementedFeatureError const& _exception)
{
errors.append(formatErrorWithException(
compilerStack,
_exception,
false,
"UnimplementedFeatureError",
@@ -1080,6 +1090,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
catch (yul::YulException const& _exception)
{
errors.append(formatErrorWithException(
compilerStack,
_exception,
false,
"YulException",
@@ -1090,6 +1101,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
catch (smtutil::SMTLogicError const& _exception)
{
errors.append(formatErrorWithException(
compilerStack,
_exception,
false,
"SMTLogicException",
@@ -1297,6 +1309,7 @@ Json::Value StandardCompiler::compileYul(InputsAndSettings _inputsAndSettings)
auto err = dynamic_pointer_cast<Error const>(error);
errors.append(formatErrorWithException(
stack,
*error,
err->type() == Error::Type::Warning,
err->typeName(),
+9 -4
View File
@@ -50,7 +50,12 @@ class Parser::ASTNodeFactory
{
public:
explicit ASTNodeFactory(Parser& _parser):
m_parser(_parser), m_location{_parser.currentLocation().start, -1, _parser.currentLocation().source} {}
m_parser(_parser), m_location{
_parser.currentLocation().start,
-1,
_parser.currentLocation().sourceName
}
{}
ASTNodeFactory(Parser& _parser, ASTPointer<ASTNode> const& _childNode):
m_parser(_parser), m_location{_childNode->location()} {}
@@ -63,7 +68,7 @@ public:
template <class NodeType, typename... Args>
ASTPointer<NodeType> createNode(Args&& ... _args)
{
solAssert(m_location.source, "");
solAssert(m_location.sourceName, "");
if (m_location.end < 0)
markEndPosition();
return make_shared<NodeType>(m_parser.nextID(), m_location, std::forward<Args>(_args)...);
@@ -2084,7 +2089,7 @@ optional<string> Parser::findLicenseString(std::vector<ASTPointer<ASTNode>> cons
else if (matches.empty())
parserWarning(
1878_error,
{-1, -1, m_scanner->charStream()},
{-1, -1, m_scanner->currentLocation().sourceName},
"SPDX license identifier not provided in source file. "
"Before publishing, consider adding a comment containing "
"\"SPDX-License-Identifier: <SPDX-License>\" to each source file. "
@@ -2094,7 +2099,7 @@ optional<string> Parser::findLicenseString(std::vector<ASTPointer<ASTNode>> cons
else
parserError(
3716_error,
{-1, -1, m_scanner->charStream()},
{-1, -1, m_scanner->currentLocation().sourceName},
"Multiple SPDX license identifiers found in source file. "
"Use \"AND\" or \"OR\" to combine multiple licenses. "
"Please see https://spdx.org for more information."