Fixed infinite loops

This commit is contained in:
hrkrshnn 2020-04-24 15:40:04 +05:30
parent eb4cff56f7
commit 644ae320a4

View File

@ -50,7 +50,8 @@ public:
* on a textual base. They utilize regular expression to search for * on a textual base. They utilize regular expression to search for
* keywords or to determine formatting. * keywords or to determine formatting.
*/ */
class SourceAnalysis { class SourceAnalysis
{
public: public:
static bool isMultilineKeyword( static bool isMultilineKeyword(
langutil::SourceLocation const& _location, langutil::SourceLocation const& _location,
@ -103,6 +104,7 @@ public:
for (auto inheritedContract: _contracts) for (auto inheritedContract: _contracts)
overrideList += inheritedContract->name() + ","; overrideList += inheritedContract->name() + ",";
// Note: Can create incorrect replacements
return "override(" + overrideList.substr(0, overrideList.size() - 1) + ")"; return "override(" + overrideList.substr(0, overrideList.size() - 1) + ")";
} }
}; };
@ -123,11 +125,21 @@ public:
std::string const& _expression std::string const& _expression
) )
{ {
auto _regex = std::regex{"(\\b" + _keyword + "\\b)"};
if (regex_search(_location.text(), _regex))
return regex_replace( return regex_replace(
_location.text(), _location.text(),
std::regex{"(\\b" + _keyword + "\\b)"}, _regex,
_expression + " " + _keyword _expression + " " + _keyword
); );
else
solAssert(
false,
LocationHelper() << "Could not fix: " << _location.text() << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
} }
/// Searches for the keyword given and appends the expression. /// Searches for the keyword given and appends the expression.
@ -142,7 +154,17 @@ public:
std::string toAppend = isMultiline ? ("\n " + _expression) : (" " + _expression); std::string toAppend = isMultiline ? ("\n " + _expression) : (" " + _expression);
std::regex keyword{"(\\b" + _keyword + "\\b)"}; std::regex keyword{"(\\b" + _keyword + "\\b)"};
if (regex_search(_location.text(), keyword))
return regex_replace(_location.text(), keyword, _keyword + toAppend); return regex_replace(_location.text(), keyword, _keyword + toAppend);
else
solAssert(
false,
LocationHelper() << "Could not fix: " << _location.text() << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
} }
/// Searches for the first right parenthesis and appends the expression /// Searches for the first right parenthesis and appends the expression
@ -153,11 +175,21 @@ public:
std::string const& _expression std::string const& _expression
) )
{ {
auto _regex = std::regex{"(\\))"};
if (regex_search(_location.text(), _regex))
return regex_replace( return regex_replace(
_location.text(), _location.text(),
std::regex{"(\\))"}, std::regex{"(\\))"},
") " + _expression ") " + _expression
); );
else
solAssert(
false,
LocationHelper() << "Could not fix: " << _location.text() << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
} }
/// Searches for the `function` keyword and its identifier and replaces /// Searches for the `function` keyword and its identifier and replaces
@ -169,11 +201,21 @@ public:
std::string const& _expression std::string const& _expression
) )
{ {
auto _regex = std::regex{ "(\\bfunction\\s*" + _name + "\\b)"};
if (regex_search(_location.text(), _regex))
return regex_replace( return regex_replace(
_location.text(), _location.text(),
std::regex{"(\\bfunction\\s*" + _name + "\\b)"}, _regex,
_expression _expression
); );
else
solAssert(
false,
LocationHelper() << "Could not fix: " << _location.text() << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
} }
static std::string gasUpdate(langutil::SourceLocation const& _location) static std::string gasUpdate(langutil::SourceLocation const& _location)
@ -194,12 +236,10 @@ public:
else else
solAssert( solAssert(
false, false,
LocationHelper() LocationHelper() << "Could not fix: " << _location.text() << " at " << _location <<
<< "Regex count not match: " << _location.text() << " at " << _location "\nNeeds to be fixed manually."
<< "\nNeeds to be fixed manually."
); );
return ""; return "";
} }
@ -221,9 +261,8 @@ public:
else else
solAssert( solAssert(
false, false,
LocationHelper() LocationHelper() << "Could not fix: " << _location.text() << " at " << _location <<
<< "Regex count not match: " << _location.text() << " at " << _location "\nNeeds to be fixed manually."
<< "\nNeeds to be fixed manually"
); );
return ""; return "";