Skip to content

Commit 2859834

Browse files
Balajiganapathiaxic
authored andcommitted
Suggest alternatives when identifier not found.
1 parent 8f8ad38 commit 2859834

7 files changed

+183
-2
lines changed

Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Features:
88
* Inline Assembly: Issue warning for using jump labels (already existed for jump instructions).
99
* Inline Assembly: Support some restricted tokens (return, byte, address) as identifiers in Julia mode.
1010
* Optimiser: Replace `x % 2**i` by `x & (2**i-1)`.
11+
* Resolver: Suggest alternative identifiers if a given identifier is not found.
1112
* SMT Checker: If-else branch conditions are taken into account in the SMT encoding of the program
1213
variables.
1314
* Syntax Checker: Deprecate the ``var`` keyword (and mark it an error as experimental 0.5.0 feature).

libsolidity/analysis/DeclarationContainer.cpp

+62-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool DeclarationContainer::registerDeclaration(
105105
return true;
106106
}
107107

108-
std::vector<Declaration const*> DeclarationContainer::resolveName(ASTString const& _name, bool _recursive) const
108+
vector<Declaration const*> DeclarationContainer::resolveName(ASTString const& _name, bool _recursive) const
109109
{
110110
solAssert(!_name.empty(), "Attempt to resolve empty name.");
111111
auto result = m_declarations.find(_name);
@@ -115,3 +115,64 @@ std::vector<Declaration const*> DeclarationContainer::resolveName(ASTString cons
115115
return m_enclosingContainer->resolveName(_name, true);
116116
return vector<Declaration const*>({});
117117
}
118+
119+
vector<ASTString> DeclarationContainer::similarNames(ASTString const& _name) const
120+
{
121+
vector<ASTString> similar;
122+
123+
for (auto const& declaration: m_declarations)
124+
{
125+
string const& declarationName = declaration.first;
126+
if (DeclarationContainer::areSimilarNames(_name, declarationName))
127+
similar.push_back(declarationName);
128+
}
129+
130+
if (m_enclosingContainer)
131+
{
132+
vector<ASTString> enclosingSimilar = m_enclosingContainer->similarNames(_name);
133+
similar.insert(similar.end(), enclosingSimilar.begin(), enclosingSimilar.end());
134+
}
135+
136+
return similar;
137+
}
138+
139+
bool DeclarationContainer::areSimilarNames(ASTString const& _name1, ASTString const& _name2)
140+
{
141+
if (_name1 == _name2)
142+
return true;
143+
144+
size_t n1 = _name1.size(), n2 = _name2.size();
145+
vector<vector<size_t>> dp(n1 + 1, vector<size_t>(n2 + 1));
146+
147+
// In this dp formulation of Damerau–Levenshtein distance we are assuming that the strings are 1-based to make base case storage easier.
148+
// So index accesser to _name1 and _name2 have to be adjusted accordingly
149+
for (size_t i1 = 0; i1 <= n1; ++i1)
150+
{
151+
for (size_t i2 = 0; i2 <= n2; ++i2)
152+
{
153+
if (min(i1, i2) == 0) // base case
154+
dp[i1][i2] = max(i1, i2);
155+
else
156+
{
157+
dp[i1][i2] = min(dp[i1-1][i2] + 1, dp[i1][i2-1] + 1);
158+
// Deletion and insertion
159+
if (_name1[i1-1] == _name2[i2-1])
160+
// Same chars, can skip
161+
dp[i1][i2] = min(dp[i1][i2], dp[i1-1][i2-1]);
162+
else
163+
// Different chars so try substitution
164+
dp[i1][i2] = min(dp[i1][i2], dp[i1-1][i2-1] + 1);
165+
166+
if (i1 > 1 && i2 > 1 && _name1[i1-1] == _name2[i2-2] && _name1[i1-2] == _name2[i2-1])
167+
// Try transposing
168+
dp[i1][i2] = min(dp[i1][i2], dp[i1-2][i2-2] + 1);
169+
}
170+
}
171+
}
172+
173+
size_t distance = dp[n1][n2];
174+
175+
// If distance is not greater than MAXIMUM_DISTANCE, and distance is strictly less than length of both names,
176+
// they can be considered similar this is to avoid irrelevant suggestions
177+
return distance <= MAXIMUM_DISTANCE && distance < n1 && distance < n2;
178+
}

libsolidity/analysis/DeclarationContainer.h

+8
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,19 @@ class DeclarationContainer
5858
/// @returns whether declaration is valid, and if not also returns previous declaration.
5959
Declaration const* conflictingDeclaration(Declaration const& _declaration, ASTString const* _name = nullptr) const;
6060

61+
/// @returns existing declaration names similar to @a _name.
62+
std::vector<ASTString> similarNames(ASTString const& _name) const;
63+
64+
6165
private:
6266
ASTNode const* m_enclosingNode;
6367
DeclarationContainer const* m_enclosingContainer;
6468
std::map<ASTString, std::vector<Declaration const*>> m_declarations;
6569
std::map<ASTString, std::vector<Declaration const*>> m_invisibleDeclarations;
70+
71+
// Calculates the Damerau–Levenshtein distance and decides whether the two names are similar
72+
static bool areSimilarNames(ASTString const& _name1, ASTString const& _name2);
73+
static size_t const MAXIMUM_DISTANCE = 2;
6674
};
6775

6876
}

libsolidity/analysis/NameAndTypeResolver.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,23 @@ vector<_T const*> NameAndTypeResolver::cThreeMerge(list<list<_T const*>>& _toMer
425425
return result;
426426
}
427427

428+
string NameAndTypeResolver::similarNameSuggestions(ASTString const& _name) const
429+
{
430+
vector<ASTString> suggestions = m_currentScope->similarNames(_name);
431+
if (suggestions.empty())
432+
return "";
433+
if (suggestions.size() == 1)
434+
return suggestions.front();
435+
436+
string choices = suggestions.front();
437+
for (size_t i = 1; i + 1 < suggestions.size(); ++i)
438+
choices += ", " + suggestions[i];
439+
440+
choices += " or " + suggestions.back();
441+
442+
return choices;
443+
}
444+
428445
DeclarationRegistrationHelper::DeclarationRegistrationHelper(
429446
map<ASTNode const*, shared_ptr<DeclarationContainer>>& _scopes,
430447
ASTNode& _astRoot,

libsolidity/analysis/NameAndTypeResolver.h

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class NameAndTypeResolver: private boost::noncopyable
9393
/// Generate and store warnings about variables that are named like instructions.
9494
void warnVariablesNamedLikeInstructions();
9595

96+
/// @returns a list of similar identifiers in the current and enclosing scopes. May return empty string if no suggestions.
97+
std::string similarNameSuggestions(ASTString const& _name) const;
98+
9699
private:
97100
/// Internal version of @a resolveNamesAndTypes (called from there) throws exceptions on fatal errors.
98101
bool resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode = true);

libsolidity/analysis/ReferencesResolver.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ bool ReferencesResolver::visit(Identifier const& _identifier)
4747
{
4848
auto declarations = m_resolver.nameFromCurrentScope(_identifier.name());
4949
if (declarations.empty())
50-
declarationError(_identifier.location(), "Undeclared identifier.");
50+
{
51+
string const& suggestions = m_resolver.similarNameSuggestions(_identifier.name());
52+
string errorMessage = "Undeclared identifier." + (suggestions.empty()? "": " Did you mean " + suggestions + "?");
53+
declarationError(_identifier.location(), errorMessage);
54+
}
5155
else if (declarations.size() == 1)
5256
_identifier.annotation().referencedDeclaration = declarations.front();
5357
else

test/libsolidity/SolidityNameAndTypeResolution.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,93 @@ BOOST_AUTO_TEST_CASE(external_visibility)
20812081
CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier.");
20822082
}
20832083

2084+
BOOST_AUTO_TEST_CASE(similar_name_suggestions_expected)
2085+
{
2086+
char const* sourceCode = R"(
2087+
contract c {
2088+
function func() {}
2089+
function g() public { fun(); }
2090+
}
2091+
)";
2092+
CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean func?");
2093+
}
2094+
2095+
BOOST_AUTO_TEST_CASE(no_name_suggestion)
2096+
{
2097+
char const* sourceCode = R"(
2098+
contract c {
2099+
function g() public { fun(); }
2100+
}
2101+
)";
2102+
CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier.");
2103+
}
2104+
2105+
BOOST_AUTO_TEST_CASE(multiple_similar_suggestions)
2106+
{
2107+
char const* sourceCode = R"(
2108+
contract c {
2109+
function g() public {
2110+
uint var1 = 1;
2111+
uint var2 = 1;
2112+
uint var3 = 1;
2113+
uint var4 = 1;
2114+
uint var5 = varx;
2115+
}
2116+
}
2117+
)";
2118+
CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean var1, var2, var3, var4 or var5?");
2119+
}
2120+
2121+
BOOST_AUTO_TEST_CASE(multiple_scopes_suggestions)
2122+
{
2123+
char const* sourceCode = R"(
2124+
contract c {
2125+
uint log9 = 2;
2126+
function g() public {
2127+
uint log8 = 3;
2128+
uint var1 = lgox;
2129+
}
2130+
}
2131+
)";
2132+
CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean log8, log9, log0, log1, log2, log3 or log4?");
2133+
}
2134+
2135+
BOOST_AUTO_TEST_CASE(inheritence_suggestions)
2136+
{
2137+
char const* sourceCode = R"(
2138+
contract a { function func() public {} }
2139+
contract c is a {
2140+
function g() public {
2141+
uint var1 = fun();
2142+
}
2143+
}
2144+
)";
2145+
CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier. Did you mean func?");
2146+
}
2147+
2148+
BOOST_AUTO_TEST_CASE(no_spurious_suggestions)
2149+
{
2150+
char const* sourceCode = R"(
2151+
contract c {
2152+
function g() public {
2153+
uint va = 1;
2154+
uint vb = vaxyz;
2155+
}
2156+
}
2157+
)";
2158+
CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier.");
2159+
2160+
sourceCode = R"(
2161+
contract c {
2162+
function g() public {
2163+
uint va = 1;
2164+
uint vb = x;
2165+
}
2166+
}
2167+
)";
2168+
CHECK_ERROR(sourceCode, DeclarationError, "Undeclared identifier.");
2169+
}
2170+
20842171
BOOST_AUTO_TEST_CASE(external_base_visibility)
20852172
{
20862173
char const* sourceCode = R"(

0 commit comments

Comments
 (0)